home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume10 / xt-examples / part04 / Confirm1.c < prev    next >
C/C++ Source or Header  |  1990-11-04  |  8KB  |  235 lines

  1. /***********************************************************
  2. Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
  3.  
  4.                         All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute these examples for any
  7. purpose and without fee is hereby granted, provided that the above
  8. copyright notice appear in all copies and that both that copyright
  9. notice and this permission notice appear in supporting documentation,
  10. and that the name of Digital not be used in advertising or publicity
  11. pertaining to distribution of the software without specific, written
  12. prior permission.
  13.  
  14. DIGITAL AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  15. SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
  17. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  18. OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  19. OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  20. OR PERFORMANCE OF THIS SOFTWARE.
  21.  
  22. ******************************************************************/
  23.  
  24. #include <X11/IntrinsicP.h>    /* Intrinsics header file */
  25. #include <X11/StringDefs.h>    /* Resource string definitions */
  26. #include "Confirm1P.h"        /* Confirm private header file */
  27. #include "Label.h"        /* Label public header file */
  28. #include "Pushbutton.h"        /* Pushbutton public header file */
  29.  
  30. #define Offset(field) XtOffsetOf(ConfirmRec, confirm.field)
  31.  
  32. static XtResource resources[] = {
  33.     {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
  34.     Offset(margin), XtRImmediate, (XtPointer) 10},
  35.     {XtNlabel, XtCLabel, XtRString, sizeof(String),
  36.     Offset(label), XtRString, "Click to confirm"},
  37.     {XtNbuttonLabel, XtCButtonLabel, XtRString, sizeof(String),
  38.     Offset(button_label), XtRString, "OK"},
  39.     {XtNcallback, XtCCallback, XtRCallback, sizeof(XtPointer),
  40.     Offset(callback), XtRCallback, (XtPointer) NULL},
  41.     {XtNlabelWidget, XtCReadOnly, XtRWidget, sizeof(Widget),
  42.         Offset(label_widget), XtRImmediate, (XtPointer) NULL},
  43.     {XtNbuttonWidget, XtCReadOnly, XtRWidget, sizeof(Widget),
  44.         Offset(button_widget), XtRImmediate, (XtPointer) NULL}
  45. };
  46.  
  47. #undef Offset
  48.  
  49. #define LabelChild(w) (((ConfirmWidget) w)->confirm.label_widget)
  50. #define ButtonChild(w) (((ConfirmWidget) w)->confirm.button_widget)
  51.  
  52. /* Forward declarations */
  53.  
  54. static void Initialize(), PositionChildren(), Realize(), Destroy();
  55. static Boolean SetValues();
  56.  
  57. ConfirmClassRec confirmClassRec = {
  58.     /* Core class part */
  59.   {
  60.     /* superclass         */    (WidgetClass) &widgetClassRec,
  61.     /* class_name         */ "Confirm",
  62.     /* widget_size         */ sizeof(ConfirmRec),
  63.     /* class_initialize      */ NULL,
  64.     /* class_part_initialize */ NULL,
  65.     /* class_inited          */    FALSE,
  66.     /* initialize         */    Initialize,
  67.     /* initialize_hook       */    NULL,
  68.     /* realize             */    Realize,
  69.     /* actions             */    NULL,
  70.     /* num_actions         */    0,
  71.     /* resources         */    resources,
  72.     /* num_resources         */    XtNumber(resources),
  73.     /* xrm_class         */    NULLQUARK,
  74.     /* compress_motion         */    TRUE,
  75.     /* compress_exposure     */    XtExposeCompressMultiple,
  76.     /* compress_enterleave   */    TRUE,
  77.     /* visible_interest         */    FALSE,
  78.     /* destroy             */    Destroy,
  79.     /* resize             */    PositionChildren,
  80.     /* expose             */    NULL,
  81.     /* set_values         */    SetValues,
  82.     /* set_values_hook       */    NULL,            
  83.     /* set_values_almost     */    XtInheritSetValuesAlmost,  
  84.     /* get_values_hook       */    NULL,
  85.     /* accept_focus         */    NULL,
  86.     /* version             */    XtVersion,
  87.     /* callback offsets      */    NULL,
  88.     /* tm_table              */    NULL,
  89.     /* query_geometry         */    XtInheritQueryGeometry,
  90.     /* display_accelerator   */    NULL,
  91.     /* extension         */    NULL,
  92.   },
  93.    /* Confirm class part */
  94.   {
  95.     /* extension         */    NULL,
  96.   }
  97. };
  98.  
  99. WidgetClass confirmWidgetClass = (WidgetClass) &confirmClassRec;
  100.  
  101. static void Callback(w, call_data, client_data)
  102.     Widget w;
  103.     XtPointer call_data, client_data;
  104. {
  105.     ConfirmWidget confirm = (ConfirmWidget) XtParent(w);
  106.  
  107.     if (XtIsShell(XtParent(confirm))) XtPopdown(XtParent(confirm));
  108.     XtCallCallbackList(confirm, confirm->confirm.callback,
  109.         (XtPointer) NULL);
  110. }
  111.  
  112. static void CalculateSize(cw, width, height)
  113.     ConfirmWidget cw;
  114.     Dimension *width, *height;
  115. {
  116.     int label_width = LabelChild(cw)->core.width +
  117.         2 * LabelChild(cw)->core.border_width,
  118.     button_width = ButtonChild(cw)->core.width + 
  119.         2 * ButtonChild(cw)->core.border_width,
  120.     max;
  121.  
  122.     max = label_width > button_width ? label_width : button_width;
  123.  
  124.     *width = max + 2 * cw->confirm.margin;
  125.     *height = LabelChild(cw)->core.height +
  126.         ButtonChild(cw)->core.height +
  127.         2 * (LabelChild(cw)->core.border_width +
  128.             ButtonChild(cw)->core.border_width) +
  129.         3 * cw->confirm.margin;
  130. }
  131.  
  132. static void PositionChildren(cw)
  133.     ConfirmWidget cw;
  134. {
  135.     int label_width = LabelChild(cw)->core.width +
  136.         2 * LabelChild(cw)->core.border_width,
  137.     button_width = ButtonChild(cw)->core.width + 
  138.         2 * ButtonChild(cw)->core.border_width;
  139.  
  140.     XtMoveWidget(LabelChild(cw), (cw->core.width - label_width) / 2,
  141.         cw->confirm.margin);
  142.     XtMoveWidget(ButtonChild(cw), (cw->core.width - button_width) / 2,
  143.         2 * cw->confirm.margin +
  144.         2 * LabelChild(cw)->core.border_width +
  145.         LabelChild(cw)->core.height);
  146. }
  147.  
  148. static void Initialize(req, new, args, num_args)
  149.     Widget req, new;
  150.     ArgList args;
  151.     Cardinal *num_args;
  152. {
  153.     ConfirmWidget cw = (ConfirmWidget) new;
  154.     Arg arg[1];
  155.  
  156.     cw->confirm.label = XtNewString(cw->confirm.label);
  157.     XtSetArg(arg[0], XtNlabel, cw->confirm.label);
  158.     LabelChild(cw) = XtCreateWidget("label", labelWidgetClass,
  159.         new, arg, 1);
  160.  
  161.     cw->confirm.button_label = XtNewString(cw->confirm.button_label);
  162.     XtSetArg(arg[0], XtNlabel, cw->confirm.button_label);
  163.     ButtonChild(cw) = XtCreateWidget("button", pushbuttonWidgetClass,
  164.         new, arg, 1);
  165.     XtAddCallback(ButtonChild(cw), XtNcallback, Callback, NULL);
  166.  
  167.     CalculateSize(cw, &cw->core.width, &cw->core.height);
  168.     PositionChildren(cw);
  169. }
  170.  
  171. static void Realize(w, valueMask, attributes)
  172.     Widget w;
  173.     XtValueMask *valueMask;
  174.     XSetWindowAttributes *attributes;
  175. {
  176.     (*confirmWidgetClass->core_class.superclass->core_class.realize)
  177.         (w, valueMask, attributes);
  178.     XtRealizeWidget(LabelChild(w));
  179.     XtRealizeWidget(ButtonChild(w));
  180.     XMapSubwindows(XtDisplay(w), XtWindow(w));
  181. }
  182.  
  183. static void Destroy(w)
  184.     Widget w;
  185. {
  186.     ConfirmWidget cw = (ConfirmWidget) w;
  187.     
  188.     XtFree((char *) cw->confirm.label);
  189.     XtFree((char *) cw->confirm.button_label);
  190.  
  191.     XtDestroyWidget(LabelChild(w));
  192.     XtDestroyWidget(ButtonChild(w));
  193. }
  194.  
  195. static Boolean SetValues(old, req, new, args, num_args)
  196.     Widget old, req, new;
  197.     ArgList args;
  198.     Cardinal *num_args;
  199. {
  200.     register ConfirmWidget oldcw = (ConfirmWidget) old;
  201.     register ConfirmWidget newcw = (ConfirmWidget) new;
  202.     Arg arg[1];
  203.     Boolean resize = FALSE;
  204.  
  205. #define NE(field) (oldcw->field != newcw->field)
  206.  
  207.     /* We don't let these change */
  208.     LabelChild(newcw) = LabelChild(oldcw);
  209.     ButtonChild(newcw) = ButtonChild(oldcw);
  210.  
  211.     if (NE(confirm.label)) {
  212.     XtFree((char *) oldcw->confirm.label);
  213.     newcw->confirm.label = XtNewString(newcw->confirm.label);
  214.     XtSetArg(arg[0], XtNlabel, newcw->confirm.label);
  215.     XtSetValues(LabelChild(newcw), arg, 1);
  216.     resize = TRUE;
  217.     }
  218.  
  219.     if (NE(confirm.button_label)) {
  220.     XtFree((char *) oldcw->confirm.button_label);
  221.     newcw->confirm.button_label =
  222.         XtNewString(newcw->confirm.button_label);
  223.     XtSetArg(arg[0], XtNlabel, newcw->confirm.button_label);
  224.     XtSetValues(ButtonChild(newcw), arg, 1);
  225.     resize = TRUE;
  226.     }
  227.  
  228.     if (NE(confirm.margin) || resize) {
  229.     CalculateSize(newcw, &newcw->core.width, &newcw->core.height);
  230.     }
  231.  
  232.     return FALSE;
  233. #undef NE
  234. }
  235.