Dialog.c

Go to the documentation of this file.
00001 /* $XConsortium: Dialog.c,v 1.48 94/04/17 20:12:02 kaleb Exp $ */
00002 
00003 /***********************************************************
00004 
00005 Copyright (c) 1987, 1988, 1994  X Consortium
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00020 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 
00024 Except as contained in this notice, the name of the X Consortium shall not be
00025 used in advertising or otherwise to promote the sale, use or other dealings
00026 in this Software without prior written authorization from the X Consortium.
00027 
00028 
00029 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
00030 
00031                         All Rights Reserved
00032 
00033 Permission to use, copy, modify, and distribute this software and its 
00034 documentation for any purpose and without fee is hereby granted, 
00035 provided that the above copyright notice appear in all copies and that
00036 both that copyright notice and this permission notice appear in 
00037 supporting documentation, and that the name of Digital not be
00038 used in advertising or publicity pertaining to distribution of the
00039 software without specific, written prior permission.  
00040 
00041 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
00042 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
00043 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
00044 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
00045 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
00046 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00047 SOFTWARE.
00048 
00049 ******************************************************************/
00050 
00051 /* NOTE: THIS IS NOT A WIDGET!  Rather, this is an interface to a widget.
00052    It implements policy, and gives a (hopefully) easier-to-use interface
00053    than just directly making your own form. */
00054 
00055 
00056 #include <X11/IntrinsicP.h>
00057 #include <X11/Xos.h>
00058 #include <X11/StringDefs.h>
00059 #include <X11/Xmu/Misc.h>
00060 
00061 #include <X11/Xaw3d/XawInit.h>
00062 #include <X11/Xaw3d/AsciiText.h>
00063 #include <X11/Xaw3d/Command.h>  
00064 #include <X11/Xaw3d/Label.h>
00065 #include <X11/Xaw3d/DialogP.h>
00066 #include <X11/Xaw3d/Cardinals.h>
00067 
00068 /*
00069  * After we have set the string in the value widget we set the
00070  * string to a magic value.  So that when a SetValues request is made
00071  * on the dialog value we will notice it, and reset the string.
00072  */
00073 
00074 #define MAGIC_VALUE ((char *) 3)
00075 
00076 #define streq(a,b) (strcmp( (a), (b) ) == 0)
00077 
00078 static XtResource resources[] = {
00079   {XtNlabel, XtCLabel, XtRString, sizeof(String),
00080      XtOffsetOf(DialogRec, dialog.label), XtRString, NULL},
00081   {XtNvalue, XtCValue, XtRString, sizeof(String),
00082      XtOffsetOf(DialogRec, dialog.value), XtRString, NULL},
00083   {XtNicon, XtCIcon, XtRBitmap, sizeof(Pixmap),
00084      XtOffsetOf(DialogRec, dialog.icon), XtRImmediate, 0},
00085 };
00086 
00087 static void Initialize(), ConstraintInitialize(), CreateDialogValueWidget(),
00088             GetValuesHook();
00089 
00090 static Boolean SetValues();
00091 
00092 DialogClassRec dialogClassRec = {
00093   { /* core_class fields */
00094     /* superclass         */    (WidgetClass) &formClassRec,
00095     /* class_name         */    "Dialog",
00096     /* widget_size        */    sizeof(DialogRec),
00097     /* class_initialize   */    XawInitializeWidgetSet,
00098     /* class_part init    */    NULL,
00099     /* class_inited       */    FALSE,
00100     /* initialize         */    Initialize,
00101     /* initialize_hook    */    NULL,
00102     /* realize            */    XtInheritRealize,
00103     /* actions            */    NULL,
00104     /* num_actions        */    0,
00105     /* resources          */    resources,
00106     /* num_resources      */    XtNumber(resources),
00107     /* xrm_class          */    NULLQUARK,
00108     /* compress_motion    */    TRUE,
00109     /* compress_exposure  */    TRUE,
00110     /* compress_enterleave*/    TRUE,
00111     /* visible_interest   */    FALSE,
00112     /* destroy            */    NULL,
00113     /* resize             */    XtInheritResize,
00114     /* expose             */    XtInheritExpose,
00115     /* set_values         */    SetValues,
00116     /* set_values_hook    */    NULL,
00117     /* set_values_almost  */    XtInheritSetValuesAlmost,
00118     /* get_values_hook    */    GetValuesHook,
00119     /* accept_focus       */    NULL,
00120     /* version            */    XtVersion,
00121     /* callback_private   */    NULL,
00122     /* tm_table           */    NULL,
00123     /* query_geometry     */    XtInheritQueryGeometry,
00124     /* display_accelerator*/    XtInheritDisplayAccelerator,
00125     /* extension          */    NULL
00126   },
00127   { /* composite_class fields */
00128     /* geometry_manager   */   XtInheritGeometryManager,
00129     /* change_managed     */   XtInheritChangeManaged,
00130     /* insert_child       */   XtInheritInsertChild,
00131     /* delete_child       */   XtInheritDeleteChild,
00132     /* extension          */   NULL
00133   },
00134   { /* constraint_class fields */
00135     /* subresourses       */   NULL,
00136     /* subresource_count  */   0,
00137     /* constraint_size    */   sizeof(DialogConstraintsRec),
00138     /* initialize         */   ConstraintInitialize,
00139     /* destroy            */   NULL,
00140     /* set_values         */   NULL,
00141     /* extension          */   NULL
00142   },
00143   { /* form_class fields */
00144     /* layout             */   XtInheritLayout
00145   },
00146   { /* dialog_class fields */
00147     /* empty              */   0
00148   }
00149 };
00150 
00151 WidgetClass dialogWidgetClass = (WidgetClass)&dialogClassRec;
00152 
00153 /* ARGSUSED */
00154 static void Initialize(request, new, args, num_args)
00155 Widget request, new;
00156 ArgList args;
00157 Cardinal *num_args;
00158 {
00159     DialogWidget dw = (DialogWidget)new;
00160     Arg arglist[9];
00161     Cardinal arg_cnt = 0;
00162 
00163     XtSetArg(arglist[arg_cnt], XtNborderWidth, 0); arg_cnt++;
00164     XtSetArg(arglist[arg_cnt], XtNleft, XtChainLeft); arg_cnt++;
00165 
00166     if (dw->dialog.icon != (Pixmap)0) {
00167         XtSetArg(arglist[arg_cnt], XtNbitmap, dw->dialog.icon); arg_cnt++;
00168         XtSetArg(arglist[arg_cnt], XtNright, XtChainLeft); arg_cnt++;
00169         dw->dialog.iconW =
00170             XtCreateManagedWidget( "icon", labelWidgetClass,
00171                                    new, arglist, arg_cnt );
00172         arg_cnt = 2;
00173         XtSetArg(arglist[arg_cnt], XtNfromHoriz, dw->dialog.iconW);arg_cnt++;
00174     } else dw->dialog.iconW = (Widget)NULL;
00175 
00176     XtSetArg(arglist[arg_cnt], XtNlabel, dw->dialog.label); arg_cnt++;
00177     XtSetArg(arglist[arg_cnt], XtNright, XtChainRight); arg_cnt++;
00178 
00179     dw->dialog.labelW = XtCreateManagedWidget( "label", labelWidgetClass,
00180                                               new, arglist, arg_cnt);
00181 
00182     if (dw->dialog.iconW != (Widget)NULL &&
00183         (dw->dialog.labelW->core.height < dw->dialog.iconW->core.height)) {
00184         XtSetArg( arglist[0], XtNheight, dw->dialog.iconW->core.height );
00185         XtSetValues( dw->dialog.labelW, arglist, ONE );
00186     }
00187     if (dw->dialog.value != NULL) 
00188         CreateDialogValueWidget( (Widget) dw);
00189     else
00190         dw->dialog.valueW = NULL;
00191 }
00192 
00193 /* ARGSUSED */
00194 static void ConstraintInitialize(request, new, args, num_args)
00195 Widget request, new;
00196 ArgList args;
00197 Cardinal *num_args;
00198 {
00199     DialogWidget dw = (DialogWidget)new->core.parent;
00200     DialogConstraints constraint = (DialogConstraints)new->core.constraints;
00201 
00202     if (!XtIsSubclass(new, commandWidgetClass)) /* if not a button */
00203         return;                                 /* then just use defaults */
00204 
00205     constraint->form.left = constraint->form.right = XtChainLeft;
00206     if (dw->dialog.valueW == NULL) 
00207       constraint->form.vert_base = dw->dialog.labelW;
00208     else
00209       constraint->form.vert_base = dw->dialog.valueW;
00210 
00211     if (dw->composite.num_children > 1) {
00212         WidgetList children = dw->composite.children;
00213         Widget *childP;
00214         for (childP = children + dw->composite.num_children - 1;
00215              childP >= children; childP-- ) {
00216             if (*childP == dw->dialog.labelW || *childP == dw->dialog.valueW)
00217                 break;
00218             if (XtIsManaged(*childP) &&
00219                 XtIsSubclass(*childP, commandWidgetClass)) {
00220                 constraint->form.horiz_base = *childP;
00221                 break;
00222             }
00223         }
00224     }
00225 }
00226 
00227 #define ICON 0
00228 #define LABEL 1
00229 #define NUM_CHECKS 2
00230 
00231 /* ARGSUSED */
00232 static Boolean SetValues(current, request, new, in_args, in_num_args)
00233 Widget current, request, new;
00234 ArgList in_args;
00235 Cardinal *in_num_args;
00236 {
00237     DialogWidget w = (DialogWidget)new;
00238     DialogWidget old = (DialogWidget)current;
00239     Arg args[5];
00240     Cardinal num_args;
00241     int i;
00242     Boolean checks[NUM_CHECKS];
00243 
00244     for (i = 0; i < NUM_CHECKS; i++)
00245         checks[i] = FALSE;
00246 
00247     for (i = 0; i < *in_num_args; i++) {
00248         if (streq(XtNicon, in_args[i].name))
00249             checks[ICON] = TRUE;
00250         if (streq(XtNlabel, in_args[i].name))
00251             checks[LABEL] = TRUE;
00252     }
00253 
00254     if (checks[ICON]) {
00255         if (w->dialog.icon != (Pixmap)0) {
00256             XtSetArg( args[0], XtNbitmap, w->dialog.icon );
00257             if (old->dialog.iconW != (Widget)NULL) {
00258                 XtSetValues( old->dialog.iconW, args, ONE );
00259             } else {
00260                 XtSetArg( args[1], XtNborderWidth, 0);
00261                 XtSetArg( args[2], XtNleft, XtChainLeft);
00262                 XtSetArg( args[3], XtNright, XtChainLeft);
00263                 w->dialog.iconW =
00264                     XtCreateWidget( "icon", labelWidgetClass,
00265                                     new, args, FOUR );
00266                 ((DialogConstraints)w->dialog.labelW->core.constraints)->
00267                     form.horiz_base = w->dialog.iconW;
00268                 XtManageChild(w->dialog.iconW);
00269             }
00270         } else if (old->dialog.icon != (Pixmap)0) {
00271             ((DialogConstraints)w->dialog.labelW->core.constraints)->
00272                     form.horiz_base = (Widget)NULL;
00273             XtDestroyWidget(old->dialog.iconW);
00274             w->dialog.iconW = (Widget)NULL;
00275         }
00276     }
00277 
00278     if ( checks[LABEL] ) {
00279         num_args = 0;
00280         XtSetArg( args[num_args], XtNlabel, w->dialog.label ); num_args++;
00281         if (w->dialog.iconW != (Widget)NULL &&
00282             (w->dialog.labelW->core.height <= w->dialog.iconW->core.height)) {
00283             XtSetArg(args[num_args], XtNheight, w->dialog.iconW->core.height);
00284             num_args++;
00285         }
00286         XtSetValues( w->dialog.labelW, args, num_args );
00287     }
00288 
00289     if ( w->dialog.value != old->dialog.value ) {
00290         if (w->dialog.value == NULL)  /* only get here if it
00291                                           wasn't NULL before. */
00292             XtDestroyWidget(old->dialog.valueW);
00293         else if (old->dialog.value == NULL) { /* create a new value widget. */
00294             w->core.width = old->core.width;
00295             w->core.height = old->core.height;
00296 #ifdef notdef
00297 /* this would be correct if Form had the same semantics on Resize
00298  * as on MakeGeometryRequest.  Unfortunately, Form botched it, so
00299  * any subclasses will currently have to deal with the fact that
00300  * we're about to change our real size.
00301  */
00302             w->form.resize_in_layout = False; 
00303             CreateDialogValueWidget( (Widget) w);
00304             w->core.width = w->form.preferred_width;
00305             w->core.height = w->form.preferred_height;
00306             w->form.resize_in_layout = True;
00307 #else /*notdef*/
00308             CreateDialogValueWidget( (Widget) w);
00309 #endif /*notdef*/
00310         }
00311         else {                  /* Widget ok, just change string. */
00312             Arg args[1];
00313             XtSetArg(args[0], XtNstring, w->dialog.value);
00314             XtSetValues(w->dialog.valueW, args, ONE);
00315             w->dialog.value = MAGIC_VALUE;
00316         }
00317     }
00318     return False;
00319 }
00320 
00321 /*      Function Name: GetValuesHook
00322  *      Description: This is a get values hook routine that gets the
00323  *                   values in the dialog.
00324  *      Arguments: w - the Text Widget.
00325  *                 args - the argument list.
00326  *                 num_args - the number of args.
00327  *      Returns: none.
00328  */
00329 
00330 static void
00331 GetValuesHook(w, args, num_args)
00332 Widget w;
00333 ArgList args;
00334 Cardinal * num_args;
00335 {
00336   Arg a[1];
00337   String s;
00338   DialogWidget src = (DialogWidget) w;
00339   int i;
00340   
00341   for (i=0; i < *num_args; i++)
00342     if (streq(args[i].name, XtNvalue)) {
00343       XtSetArg(a[0], XtNstring, &s);
00344       XtGetValues(src->dialog.valueW, a, 1);
00345       *((char **) args[i].value) = s;
00346     }
00347 }
00348 
00349 
00350 /*      Function Name: CreateDialogValueWidget
00351  *      Description: Creates the dialog widgets value widget.
00352  *      Arguments: w - the dialog widget.
00353  *      Returns: none.
00354  *
00355  *      must be called only when w->dialog.value is non-nil.
00356  */
00357 
00358 static void
00359 CreateDialogValueWidget(w)
00360 Widget w;
00361 {
00362     DialogWidget dw = (DialogWidget) w;    
00363     Arg arglist[10];
00364     Cardinal num_args = 0;
00365 
00366 #ifdef notdef
00367     XtSetArg(arglist[num_args], XtNwidth,
00368              dw->dialog.labelW->core.width); num_args++; /* ||| hack */
00369 #endif /*notdef*/
00370     XtSetArg(arglist[num_args], XtNstring, dw->dialog.value);     num_args++;
00371     XtSetArg(arglist[num_args], XtNresizable, True);              num_args++;
00372     XtSetArg(arglist[num_args], XtNresize, XawtextResizeBoth);    num_args++;
00373     XtSetArg(arglist[num_args], XtNeditType, XawtextEdit);        num_args++;
00374     XtSetArg(arglist[num_args], XtNfromVert, dw->dialog.labelW);  num_args++;
00375     XtSetArg(arglist[num_args], XtNleft, XtChainLeft);            num_args++;
00376     XtSetArg(arglist[num_args], XtNright, XtChainRight);          num_args++;
00377 
00378     dw->dialog.valueW = XtCreateWidget("value", asciiTextWidgetClass,
00379                                      w, arglist, num_args);
00380 
00381     /* if the value widget is being added after buttons,
00382      * then the buttons need new layout constraints.
00383      */
00384     if (dw->composite.num_children > 1) {
00385         WidgetList children = dw->composite.children;
00386         Widget *childP;
00387         for (childP = children + dw->composite.num_children - 1;
00388              childP >= children; childP-- ) {
00389             if (*childP == dw->dialog.labelW || *childP == dw->dialog.valueW)
00390                 continue;
00391             if (XtIsManaged(*childP) &&
00392                 XtIsSubclass(*childP, commandWidgetClass)) {
00393                 ((DialogConstraints)(*childP)->core.constraints)->
00394                     form.vert_base = dw->dialog.valueW;
00395             }
00396         }
00397     }
00398     XtManageChild(dw->dialog.valueW);
00399 
00400 /* 
00401  * Value widget gets the keyboard focus.
00402  */
00403 
00404     XtSetKeyboardFocus(w, dw->dialog.valueW);
00405     dw->dialog.value = MAGIC_VALUE;
00406 }
00407 
00408 
00409 void
00410 #if NeedFunctionPrototypes
00411 XawDialogAddButton(Widget dialog, _Xconst char* name, XtCallbackProc function,
00412                    XtPointer param)
00413 #else
00414 XawDialogAddButton(dialog, name, function, param)
00415 Widget dialog;
00416 String name;
00417 XtCallbackProc function;
00418 XtPointer param;
00419 #endif
00420 {
00421 /*
00422  * Correct Constraints are all set in ConstraintInitialize().
00423  */
00424     Widget button;
00425 
00426     button = XtCreateManagedWidget( name, commandWidgetClass, dialog, 
00427                                     (ArgList)NULL, (Cardinal)0 );
00428 
00429     if (function != NULL)       /* don't add NULL callback func. */
00430         XtAddCallback(button, XtNcallback, function, param);
00431 }
00432 
00433 
00434 char *
00435 #if NeedFunctionPrototypes
00436 XawDialogGetValueString(Widget w)
00437 #else
00438 XawDialogGetValueString(w)
00439 Widget w;
00440 #endif
00441 {
00442     Arg args[1];
00443     char * value;
00444 
00445     XtSetArg(args[0], XtNstring, &value);
00446     XtGetValues( ((DialogWidget)w)->dialog.valueW, args, ONE);
00447     return(value);
00448 }

Generated on Sun Mar 4 15:03:55 2007 for Scilab [trunk] by  doxygen 1.5.1