home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xaw / Dialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-21  |  13.3 KB  |  422 lines

  1. /* $XConsortium: Dialog.c,v 1.45 91/03/21 14:38:27 dave Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  5. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. /* NOTE: THIS IS NOT A WIDGET!  Rather, this is an interface to a widget.
  28.    It implements policy, and gives a (hopefully) easier-to-use interface
  29.    than just directly making your own form. */
  30.  
  31.  
  32. #include <X11/Xlib.h>
  33. #include <X11/Xos.h>
  34. #include <X11/IntrinsicP.h>
  35. #include <X11/StringDefs.h>
  36. #include <X11/Xmu/Misc.h>
  37.  
  38. #include <X11/Xaw/XawInit.h>
  39. #include <X11/Xaw/AsciiText.h>
  40. #include <X11/Xaw/Command.h>    
  41. #include <X11/Xaw/Label.h>
  42. #include <X11/Xaw/DialogP.h>
  43. #include <X11/Xaw/Cardinals.h>
  44.  
  45. /*
  46.  * After we have set the string in the value widget we set the
  47.  * string to a magic value.  So that when a SetValues request is made
  48.  * on the dialog value we will notice it, and reset the string.
  49.  */
  50.  
  51. #define MAGIC_VALUE ((char *) 3)
  52.  
  53. #define streq(a,b) (strcmp( (a), (b) ) == 0)
  54.  
  55. static XtResource resources[] = {
  56.   {XtNlabel, XtCLabel, XtRString, sizeof(String),
  57.      XtOffsetOf(DialogRec, dialog.label), XtRString, NULL},
  58.   {XtNvalue, XtCValue, XtRString, sizeof(String),
  59.      XtOffsetOf(DialogRec, dialog.value), XtRString, NULL},
  60.   {XtNicon, XtCIcon, XtRBitmap, sizeof(Pixmap),
  61.      XtOffsetOf(DialogRec, dialog.icon), XtRImmediate, 0},
  62. };
  63.  
  64. static void Initialize(), ConstraintInitialize(), CreateDialogValueWidget(),
  65.             GetValuesHook();
  66.  
  67. static Boolean SetValues();
  68.  
  69. DialogClassRec dialogClassRec = {
  70.   { /* core_class fields */
  71.     /* superclass         */    (WidgetClass) &formClassRec,
  72.     /* class_name         */    "Dialog",
  73.     /* widget_size        */    sizeof(DialogRec),
  74.     /* class_initialize   */    XawInitializeWidgetSet,
  75.     /* class_part init    */    NULL,
  76.     /* class_inited       */    FALSE,
  77.     /* initialize         */    Initialize,
  78.     /* initialize_hook    */    NULL,
  79.     /* realize            */    XtInheritRealize,
  80.     /* actions            */    NULL,
  81.     /* num_actions        */    0,
  82.     /* resources          */    resources,
  83.     /* num_resources      */    XtNumber(resources),
  84.     /* xrm_class          */    NULLQUARK,
  85.     /* compress_motion    */    TRUE,
  86.     /* compress_exposure  */    TRUE,
  87.     /* compress_enterleave*/    TRUE,
  88.     /* visible_interest   */    FALSE,
  89.     /* destroy            */    NULL,
  90.     /* resize             */    XtInheritResize,
  91.     /* expose             */    XtInheritExpose,
  92.     /* set_values         */    SetValues,
  93.     /* set_values_hook    */    NULL,
  94.     /* set_values_almost  */    XtInheritSetValuesAlmost,
  95.     /* get_values_hook    */    GetValuesHook,
  96.     /* accept_focus       */    NULL,
  97.     /* version            */    XtVersion,
  98.     /* callback_private   */    NULL,
  99.     /* tm_table           */    NULL,
  100.     /* query_geometry     */    XtInheritQueryGeometry,
  101.     /* display_accelerator*/    XtInheritDisplayAccelerator,
  102.     /* extension          */    NULL
  103.   },
  104.   { /* composite_class fields */
  105.     /* geometry_manager   */   XtInheritGeometryManager,
  106.     /* change_managed     */   XtInheritChangeManaged,
  107.     /* insert_child       */   XtInheritInsertChild,
  108.     /* delete_child       */   XtInheritDeleteChild,
  109.     /* extension          */   NULL
  110.   },
  111.   { /* constraint_class fields */
  112.     /* subresourses       */   NULL,
  113.     /* subresource_count  */   0,
  114.     /* constraint_size    */   sizeof(DialogConstraintsRec),
  115.     /* initialize         */   ConstraintInitialize,
  116.     /* destroy            */   NULL,
  117.     /* set_values         */   NULL,
  118.     /* extension          */   NULL
  119.   },
  120.   { /* form_class fields */
  121.     /* layout             */   XtInheritLayout
  122.   },
  123.   { /* dialog_class fields */
  124.     /* empty              */   0
  125.   }
  126. };
  127.  
  128. WidgetClass dialogWidgetClass = (WidgetClass)&dialogClassRec;
  129.  
  130. /* ARGSUSED */
  131. static void Initialize(request, new)
  132. Widget request, new;
  133. {
  134.     DialogWidget dw = (DialogWidget)new;
  135.     Arg arglist[9];
  136.     Cardinal num_args = 0;
  137.  
  138.     XtSetArg(arglist[num_args], XtNborderWidth, 0); num_args++;
  139.     XtSetArg(arglist[num_args], XtNleft, XtChainLeft); num_args++;
  140.  
  141.     if (dw->dialog.icon != (Pixmap)0) {
  142.     XtSetArg(arglist[num_args], XtNbitmap, dw->dialog.icon); num_args++;
  143.     XtSetArg(arglist[num_args], XtNright, XtChainLeft); num_args++;
  144.     dw->dialog.iconW =
  145.         XtCreateManagedWidget( "icon", labelWidgetClass,
  146.                    new, arglist, num_args );
  147.     num_args = 2;
  148.     XtSetArg(arglist[num_args], XtNfromHoriz, dw->dialog.iconW);num_args++;
  149.     } else dw->dialog.iconW = (Widget)NULL;
  150.  
  151.     XtSetArg(arglist[num_args], XtNlabel, dw->dialog.label); num_args++;
  152.     XtSetArg(arglist[num_args], XtNright, XtChainRight); num_args++;
  153.  
  154.     dw->dialog.labelW = XtCreateManagedWidget( "label", labelWidgetClass,
  155.                           new, arglist, num_args);
  156.  
  157.     if (dw->dialog.iconW != (Widget)NULL &&
  158.     (dw->dialog.labelW->core.height < dw->dialog.iconW->core.height)) {
  159.     XtSetArg( arglist[0], XtNheight, dw->dialog.iconW->core.height );
  160.     XtSetValues( dw->dialog.labelW, arglist, ONE );
  161.     }
  162.     if (dw->dialog.value != NULL) 
  163.         CreateDialogValueWidget( (Widget) dw);
  164.     else
  165.         dw->dialog.valueW = NULL;
  166. }
  167.  
  168. /* ARGSUSED */
  169. static void ConstraintInitialize(request, new)
  170. Widget request, new;
  171. {
  172.     DialogWidget dw = (DialogWidget)new->core.parent;
  173.     DialogConstraints constraint = (DialogConstraints)new->core.constraints;
  174.  
  175.     if (!XtIsSubclass(new, commandWidgetClass))    /* if not a button */
  176.     return;                    /* then just use defaults */
  177.  
  178.     constraint->form.left = constraint->form.right = XtChainLeft;
  179.     if (dw->dialog.valueW == NULL) 
  180.       constraint->form.vert_base = dw->dialog.labelW;
  181.     else
  182.       constraint->form.vert_base = dw->dialog.valueW;
  183.  
  184.     if (dw->composite.num_children > 1) {
  185.     WidgetList children = dw->composite.children;
  186.     Widget *childP;
  187.         for (childP = children + dw->composite.num_children - 1;
  188.          childP >= children; childP-- ) {
  189.         if (*childP == dw->dialog.labelW || *childP == dw->dialog.valueW)
  190.             break;
  191.         if (XtIsManaged(*childP) &&
  192.         XtIsSubclass(*childP, commandWidgetClass)) {
  193.             constraint->form.horiz_base = *childP;
  194.         break;
  195.         }
  196.     }
  197.     }
  198. }
  199.  
  200. #define ICON 0
  201. #define LABEL 1
  202. #define NUM_CHECKS 2
  203.  
  204. /* ARGSUSED */
  205. static Boolean SetValues(current, request, new, in_args, in_num_args)
  206. Widget current, request, new;
  207. ArgList in_args;
  208. Cardinal *in_num_args;
  209. {
  210.     DialogWidget w = (DialogWidget)new;
  211.     DialogWidget old = (DialogWidget)current;
  212.     Arg args[5];
  213.     Cardinal num_args;
  214.     int i;
  215.     Boolean checks[NUM_CHECKS];
  216.  
  217.     for (i = 0; i < NUM_CHECKS; i++)
  218.     checks[i] = FALSE;
  219.  
  220.     for (i = 0; i < *in_num_args; i++) {
  221.     if (streq(XtNicon, in_args[i].name))
  222.         checks[ICON] = TRUE;
  223.     if (streq(XtNlabel, in_args[i].name))
  224.         checks[LABEL] = TRUE;
  225.     }
  226.  
  227.     if (checks[ICON]) {
  228.     if (w->dialog.icon != (Pixmap)0) {
  229.         XtSetArg( args[0], XtNbitmap, w->dialog.icon );
  230.         if (old->dialog.iconW != (Widget)NULL) {
  231.         XtSetValues( old->dialog.iconW, args, ONE );
  232.         } else {
  233.         XtSetArg( args[1], XtNborderWidth, 0);
  234.         XtSetArg( args[2], XtNleft, XtChainLeft);
  235.         XtSetArg( args[3], XtNright, XtChainLeft);
  236.         w->dialog.iconW =
  237.             XtCreateWidget( "icon", labelWidgetClass,
  238.                     new, args, FOUR );
  239.         ((DialogConstraints)w->dialog.labelW->core.constraints)->
  240.             form.horiz_base = w->dialog.iconW;
  241.         XtManageChild(w->dialog.iconW);
  242.         }
  243.     } else if (old->dialog.icon != (Pixmap)0) {
  244.         ((DialogConstraints)w->dialog.labelW->core.constraints)->
  245.             form.horiz_base = (Widget)NULL;
  246.         XtDestroyWidget(old->dialog.iconW);
  247.         w->dialog.iconW = (Widget)NULL;
  248.     }
  249.     }
  250.  
  251.     if ( checks[LABEL] ) {
  252.         num_args = 0;
  253.         XtSetArg( args[num_args], XtNlabel, w->dialog.label ); num_args++;
  254.     if (w->dialog.iconW != (Widget)NULL &&
  255.         (w->dialog.labelW->core.height <= w->dialog.iconW->core.height)) {
  256.         XtSetArg(args[num_args], XtNheight, w->dialog.iconW->core.height);
  257.         num_args++;
  258.     }
  259.     XtSetValues( w->dialog.labelW, args, num_args );
  260.     }
  261.  
  262.     if ( w->dialog.value != old->dialog.value ) {
  263.         if (w->dialog.value == NULL)  /* only get here if it
  264.                       wasn't NULL before. */
  265.         XtDestroyWidget(old->dialog.valueW);
  266.     else if (old->dialog.value == NULL) { /* create a new value widget. */
  267.         w->core.width = old->core.width;
  268.         w->core.height = old->core.height;
  269. #ifdef notdef
  270. /* this would be correct if Form had the same semantics on Resize
  271.  * as on MakeGeometryRequest.  Unfortunately, Form botched it, so
  272.  * any subclasses will currently have to deal with the fact that
  273.  * we're about to change our real size.
  274.  */
  275.         w->form.resize_in_layout = False; 
  276.         CreateDialogValueWidget( (Widget) w);
  277.         w->core.width = w->form.preferred_width;
  278.         w->core.height = w->form.preferred_height;
  279.         w->form.resize_in_layout = True;
  280. #else /*notdef*/
  281.         CreateDialogValueWidget( (Widget) w);
  282. #endif /*notdef*/
  283.     }
  284.     else {            /* Widget ok, just change string. */
  285.         Arg args[1];
  286.         XtSetArg(args[0], XtNstring, w->dialog.value);
  287.         XtSetValues(w->dialog.valueW, args, ONE);
  288.         w->dialog.value = MAGIC_VALUE;
  289.     }
  290.     }
  291.     return False;
  292. }
  293.  
  294. /*    Function Name: GetValuesHook
  295.  *    Description: This is a get values hook routine that gets the
  296.  *                   values in the dialog.
  297.  *    Arguments: w - the Text Widget.
  298.  *                 args - the argument list.
  299.  *                 num_args - the number of args.
  300.  *    Returns: none.
  301.  */
  302.  
  303. static void
  304. GetValuesHook(w, args, num_args)
  305. Widget w;
  306. ArgList args;
  307. Cardinal * num_args;
  308. {
  309.   Arg a[1];
  310.   String s;
  311.   DialogWidget src = (DialogWidget) w;
  312.   register int i;
  313.   
  314.   for (i=0; i < *num_args; i++)
  315.     if (streq(args[i].name, XtNvalue)) {
  316.       XtSetArg(a[0], XtNstring, &s);
  317.       XtGetValues(src->dialog.valueW, a, 1);
  318.       *((char **) args[i].value) = s;
  319.     }
  320. }
  321.  
  322.  
  323. /*    Function Name: CreateDialogValueWidget
  324.  *    Description: Creates the dialog widgets value widget.
  325.  *    Arguments: w - the dialog widget.
  326.  *    Returns: none.
  327.  *
  328.  *    must be called only when w->dialog.value is non-nil.
  329.  */
  330.  
  331. static void
  332. CreateDialogValueWidget(w)
  333. Widget w;
  334. {
  335.     DialogWidget dw = (DialogWidget) w;    
  336.     Arg arglist[10];
  337.     Cardinal num_args = 0;
  338.  
  339. #ifdef notdef
  340.     XtSetArg(arglist[num_args], XtNwidth,
  341.          dw->dialog.labelW->core.width); num_args++; /* ||| hack */
  342. #endif /*notdef*/
  343.     XtSetArg(arglist[num_args], XtNstring, dw->dialog.value);     num_args++;
  344.     XtSetArg(arglist[num_args], XtNresizable, True);              num_args++;
  345.     XtSetArg(arglist[num_args], XtNresize, XawtextResizeBoth);    num_args++;
  346.     XtSetArg(arglist[num_args], XtNeditType, XawtextEdit);        num_args++;
  347.     XtSetArg(arglist[num_args], XtNfromVert, dw->dialog.labelW);  num_args++;
  348.     XtSetArg(arglist[num_args], XtNleft, XtChainLeft);            num_args++;
  349.     XtSetArg(arglist[num_args], XtNright, XtChainRight);          num_args++;
  350.  
  351.     dw->dialog.valueW = XtCreateWidget("value",asciiTextWidgetClass,
  352.                        w, arglist, num_args);
  353.  
  354.     /* if the value widget is being added after buttons,
  355.      * then the buttons need new layout constraints.
  356.      */
  357.     if (dw->composite.num_children > 1) {
  358.     WidgetList children = dw->composite.children;
  359.     Widget *childP;
  360.         for (childP = children + dw->composite.num_children - 1;
  361.          childP >= children; childP-- ) {
  362.         if (*childP == dw->dialog.labelW || *childP == dw->dialog.valueW)
  363.         continue;
  364.         if (XtIsManaged(*childP) &&
  365.         XtIsSubclass(*childP, commandWidgetClass)) {
  366.             ((DialogConstraints)(*childP)->core.constraints)->
  367.             form.vert_base = dw->dialog.valueW;
  368.         }
  369.     }
  370.     }
  371.     XtManageChild(dw->dialog.valueW);
  372.  
  373. /* 
  374.  * Value widget gets the keyboard focus.
  375.  */
  376.  
  377.     XtSetKeyboardFocus(w, dw->dialog.valueW);
  378.     dw->dialog.value = MAGIC_VALUE;
  379. }
  380.  
  381.  
  382. void
  383. #if NeedFunctionPrototypes
  384. XawDialogAddButton(Widget dialog, _Xconst char* name, XtCallbackProc function,
  385.            XtPointer param)
  386. #else
  387. XawDialogAddButton(dialog, name, function, param)
  388. Widget dialog;
  389. String name;
  390. XtCallbackProc function;
  391. XtPointer param;
  392. #endif
  393. {
  394. /*
  395.  * Correct Constraints are all set in ConstraintInitialize().
  396.  */
  397.     Widget button;
  398.  
  399.     button = XtCreateManagedWidget( name, commandWidgetClass, dialog, 
  400.                     NULL, (Cardinal) 0 );
  401.  
  402.     if (function != NULL)    /* don't add NULL callback func. */
  403.         XtAddCallback(button, XtNcallback, function, param);
  404. }
  405.  
  406.  
  407. char *
  408. #if NeedFunctionPrototypes
  409. XawDialogGetValueString(Widget w)
  410. #else
  411. XawDialogGetValueString(w)
  412. Widget w;
  413. #endif
  414. {
  415.     Arg args[1];
  416.     char * value;
  417.  
  418.     XtSetArg(args[0], XtNstring, &value);
  419.     XtGetValues( ((DialogWidget)w)->dialog.valueW, args, ONE);
  420.     return(value);
  421. }
  422.