home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume2 / xbrowser / part02 / option.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-03  |  8.5 KB  |  307 lines

  1. /* Systems Sciences Laboratory, Webster Research Center */
  2.  
  3. static char *PROGRAM_information[] =
  4. {
  5.     "Copyright (c) 1988 Xerox Corporation.  All rights reserved.",
  6.     "$Header$",
  7.     "$Locker$"
  8. }
  9. ;
  10.  
  11. /*
  12.  * Copyright protection claimed includes all forms and matters of copyrightable
  13.  * material and information now allowed by statutory or judicial lay or
  14.  * herinafter granted, including without limitation, material generated from
  15.  * the software programs which are displayed on the screen such as icons,
  16.  * screen display looks, etc.
  17.  */
  18.  
  19.  
  20. /* NOTE: This is modelled after the dialog widget!  
  21.    Rather, this is an interface to a widget.
  22.    It implements policy, and gives a (hopefully) easier-to-use interface
  23.    than just directly making your own form. */
  24.  
  25.  
  26. #include <X11/Xlib.h>
  27. #include <X11/Xos.h>
  28. #include <X11/IntrinsicP.h>
  29. #include <X11/XawMisc.h>
  30. #include <X11/StringDefs.h>
  31. #include <X11/AsciiText.h>
  32. #include <X11/Command.h>
  33. #include <X11/Label.h>
  34. #include "optionP.h"
  35. #include "toggleP.h"
  36. #include <ctype.h>
  37.  
  38. #define offset(field) XtOffset(OptionWidget, field)
  39.  
  40.  
  41. static XtResource resourcelist[] = {
  42.   {XtNlabel, XtCLabel, XtRString, sizeof(String),
  43.      offset(option.label), XtRString, NULL},
  44.   {XtNorientation, XtCOrientation, XtROrientation, sizeof(XtOrientation),
  45.      offset(option.orientation), XtRString, "orientVertical"},
  46. };
  47.  
  48. extern void Initialize(), ConstraintInitialize();
  49. extern Boolean SetValues();
  50. extern void DoOption();
  51.  
  52. OptionClassRec optionClassRec = {
  53.   { /* core_class fields */
  54.     /* superclass         */    (WidgetClass) &formClassRec,
  55.     /* class_name         */    "Option",
  56.     /* widget_size        */    sizeof(OptionRec),
  57.     /* class_initialize   */    NULL,
  58.     /* class_part init    */    NULL,
  59.     /* class_inited       */    FALSE,
  60.     /* initialize         */    Initialize,
  61.     /* initialize_hook    */    NULL,
  62.     /* realize            */    XtInheritRealize,
  63.     /* actions            */    NULL,
  64.     /* num_actions        */    0,
  65.     /* resources          */    resourcelist,
  66.     /* num_resources      */    XtNumber(resourcelist),
  67.     /* xrm_class          */    NULLQUARK,
  68.     /* compress_motion    */    TRUE,
  69.     /* compress_exposure  */    TRUE,
  70.     /* compress_enterleave*/    TRUE,
  71.     /* visible_interest   */    FALSE,
  72.     /* destroy            */    NULL,
  73.     /* resize             */    XtInheritResize,
  74.     /* expose             */    XtInheritExpose,
  75.     /* set_values         */    SetValues,
  76.     /* set_values_hook    */    NULL,
  77.     /* set_values_almost  */    XtInheritSetValuesAlmost,
  78.     /* get_values_hook    */    NULL,
  79.     /* accept_focus       */    NULL,
  80.     /* version            */    XtVersion,
  81.     /* callback_private   */    NULL,
  82.     /* tm_table           */    NULL,
  83.     /* query_geometry     */    NULL,
  84.     /* display_accelerator*/    XtInheritDisplayAccelerator,
  85.     /* extension      */    NULL,
  86.  
  87.   },
  88.   { /* composite_class fields */
  89.     /* geometry_manager   */   XtInheritGeometryManager,
  90.     /* change_managed     */   XtInheritChangeManaged,
  91.     /* insert_child       */   XtInheritInsertChild,
  92.     /* delete_child       */   XtInheritDeleteChild,
  93.     /* extension         */   NULL,
  94.   },
  95.   { /* constraint_class fields */
  96.     /* subresourses       */   NULL,
  97.     /* subresource_count  */   0,
  98.     /* constraint_size    */   sizeof(OptionConstraintsRec),
  99.     /* initialize         */   ConstraintInitialize,
  100.     /* destroy            */   NULL,
  101.     /* set_values         */   NULL
  102.   },
  103.   { /* form_class fields */
  104.     /* empty              */   0
  105.   },
  106.   { /* option_class fields */
  107.     /* empty              */   0
  108.   }
  109. };
  110.  
  111. WidgetClass optionWidgetClass = (WidgetClass)&optionClassRec;
  112.  
  113. /****************************************************************
  114.  *
  115.  * Private Procedures
  116.  *
  117.  ****************************************************************/
  118.  
  119. /* ARGSUSED */
  120. static void Initialize(request, new)
  121. Widget request, new;
  122. {
  123.     OptionWidget dw = (OptionWidget)new;
  124.     static Arg label_args[] = {
  125.        {XtNlabel, (XtArgVal)NULL},
  126.        {XtNborderWidth, (XtArgVal) 0}
  127.        };
  128.  
  129.     label_args[0].value = (XtArgVal)dw->option.label;
  130.     dw->option.labelW = XtCreateManagedWidget( "label", 
  131.                 labelWidgetClass, new,
  132.                 label_args, XtNumber(label_args) );
  133.     dw->option.select = 0;
  134. }
  135.  
  136.  
  137. /* ARGSUSED */
  138. static void ConstraintInitialize(request, new)
  139. Widget request, new;
  140. {
  141.     OptionWidget dw = (OptionWidget)new->core.parent;
  142.     WidgetList children = dw->composite.children;
  143.     OptionConstraints constraint = (OptionConstraints)new->core.constraints;
  144.     Widget *childP;
  145.  
  146.     if (!XtIsSubclass(new, toggleWidgetClass))    /* if not a toggle */
  147.     return;                    /* then just use defaults */
  148.  
  149.     constraint->form.left = constraint->form.right = XtChainLeft;
  150.     constraint->form.vert_base = dw->option.labelW;
  151.  
  152.     if (dw->composite.num_children > 1) {
  153.         for (childP = children + dw->composite.num_children - 1;
  154.          childP >= children; childP-- ) {
  155.         if (*childP == dw->option.labelW)
  156.             break;
  157.         if (XtIsManaged(*childP) &&
  158.         XtIsSubclass(*childP, toggleWidgetClass)) {
  159.             if (dw->option.orientation == XtorientHorizontal)
  160.             constraint->form.horiz_base = *childP;
  161.         else    constraint->form.vert_base = *childP;
  162.         break;
  163.         }
  164.     }
  165.     }
  166. }
  167.  
  168. /***************************
  169. *
  170. *  Action Procedures
  171. *
  172. ***************************/
  173.  
  174. static void DoOption(w, client_data, call_data)
  175. Widget w;
  176. caddr_t client_data, call_data;
  177. {
  178.     ToggleWidget tw = (ToggleWidget)w;
  179.     OptionWidget parent = (OptionWidget)tw->core.parent;
  180.     XtState newstate = (XtState)call_data;
  181.     Arg togglearg[1];
  182.     Boolean found = 0;
  183.     int i = 1;
  184.  
  185.     if (newstate == XtToggleOff) {
  186.         /* the currently selected toggle button is selected */
  187.        XtSetArg(togglearg[0], XtNstate, XtToggleOn);
  188.        XtSetValues(w, togglearg, (Cardinal)1);
  189.        return;
  190.     }
  191.     else {
  192.        XtSetArg(togglearg[0], XtNstate, XtToggleOff);
  193.  
  194.        XtSetValues(parent->composite.children[parent->option.select], 
  195.             togglearg, (Cardinal)1);
  196.     }
  197.  
  198.     while (!found && i < parent->composite.num_children) 
  199.        if ((Widget)parent->composite.children[i] == w) {
  200.         found = 1;
  201.         parent->option.select = i;
  202.         break;
  203.        }
  204.        else i++;
  205. }
  206.  
  207. /* ARGSUSED */
  208. static Boolean SetValues(current, request, new)
  209. Widget current, request, new;
  210. {
  211.     return False;
  212. }
  213.  
  214. void DoHorizontalOption(parent, left, upper)
  215. OptionWidget parent;
  216. Widget *left, *upper;
  217. {
  218.     *left = (Widget) parent->option.label;
  219.  
  220.     if ( (parent->composite.num_children > 1) )
  221.               *upper = (Widget)parent->
  222.            composite.children[parent->composite.num_children - 1];
  223.     else     *upper = (Widget)NULL;
  224. }
  225.  
  226. void DoVerticalOption(parent, left, upper)
  227. OptionWidget parent;
  228. Widget *left, *upper;
  229. {
  230.     *upper = (Widget)parent->
  231.     composite.children[parent->composite.num_children - 1];
  232.  
  233.     *left = (Widget)NULL;
  234. }
  235.  
  236. void XtOptionAddOption(option, name, set)
  237. Widget option;
  238. char *name;
  239. Boolean set;
  240. {
  241.     OptionWidget parent = (OptionWidget)option;
  242.     Widget left, upper;
  243.     Arg togglearg[1];
  244.     static XtCallbackRec callbackList[] = { {NULL, NULL}, {NULL, NULL} };
  245.  
  246.     static Arg arglist[] = {
  247.        {XtNfromHoriz, (XtArgVal) NULL},
  248.        {XtNfromVert, (XtArgVal) NULL},
  249.        {XtNstate, (XtArgVal) NULL},
  250.        {XtNcallback,(XtArgVal)callbackList},
  251.        {XtNleft, (XtArgVal) XtChainLeft},
  252.        {XtNright, (XtArgVal) XtChainLeft},
  253.        {XtNtop, (XtArgVal) XtChainTop},
  254.        {XtNbottom, (XtArgVal) XtChainTop}
  255.     };
  256.  
  257.     if (parent->option.orientation == XtorientHorizontal)
  258.         DoHorizontalOption(parent, &left, &upper);
  259.     else    DoVerticalOption(parent, &left, &upper);
  260.  
  261.     arglist[0].value = (XtArgVal)left;
  262.     arglist[1].value = (XtArgVal)upper;
  263.  
  264.     if (set && parent->option.select != 0) {
  265.        arglist[2].value = (XtArgVal)XtToggleOn;
  266.             /* reset existing toggle widget */
  267.        XtSetArg(togglearg[0], XtNstate, XtToggleOff);
  268.        XtSetValues(parent->composite.children[parent->option.select],
  269.          togglearg, (Cardinal)1);
  270.        parent->option.select = parent->composite.num_children;
  271.     }       
  272.     else if (parent->option.select == 0) {
  273.        arglist[2].value = (XtArgVal)XtToggleOn;
  274.        parent->option.select = parent->composite.num_children;
  275.     }
  276.     else arglist[2].value = (XtArgVal)XtToggleOff;    
  277.  
  278.     callbackList[0].callback = DoOption;
  279.  
  280.     XtCreateManagedWidget(name, toggleWidgetClass, option, 
  281.             arglist, XtNumber(arglist) );
  282. }
  283.  
  284.  
  285. int XtOptionGetSelection(w)
  286. Widget w;
  287. {
  288.     return ((OptionWidget)w)->option.select;
  289. }
  290.  
  291. void XtOptionSetSelection(w, index)
  292. Widget w;
  293. int index;
  294. {
  295.     Arg toggleargs[1];
  296.     OptionWidget parent = (OptionWidget)w;
  297.  
  298.     XtSetArg(toggleargs[0], XtNstate, XtToggleOff);
  299.     XtSetValues(parent->composite.children[parent->option.select],
  300.         toggleargs, XtNumber(toggleargs));
  301.  
  302.     XtSetArg(toggleargs[0], XtNstate, XtToggleOn);
  303.     XtSetValues(parent->composite.children[index],
  304.         toggleargs, XtNumber(toggleargs));
  305.     parent->option.select = index;
  306. }
  307.