home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume2 / xbrowser / part02 / toggle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-03  |  8.4 KB  |  317 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. /*
  21.  * toggle.c - Toggle widget
  22.  *
  23.  */
  24.  
  25.  
  26. #include <stdio.h>
  27. #include <X11/Xos.h>
  28. #include <ctype.h>
  29. #include <X11/StringDefs.h>
  30. #include <X11/IntrinsicP.h>
  31. #include <X11/XawMisc.h>
  32. #include "toggleP.h"
  33.  
  34. /* Private Data */
  35.  
  36. extern void Initialize();
  37. extern Boolean SetValues();
  38. extern void Toggle();
  39. extern void Unhighlight();
  40. extern void Highlight();
  41. extern void Notify();
  42. extern void ClassInitialize();
  43.  
  44.  
  45. static char toggleTranslations[] =
  46.     "<Btn1Up>:        toggle() notify()\n\
  47.      <EnterWindow>:    highlight() \n\
  48.      <LeaveWindow>:    unhighlight()";
  49.  
  50.  
  51. #define offset(field) XtOffset(ToggleWidget, field)
  52. static Dimension defwidth = 0;
  53. static XtState defState = XtToggleOff;
  54.  
  55. static XtResource resources[] = { 
  56.    {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
  57.       offset(simple.cursor), XtRString, "hand2"},
  58.    {XtNborderWidth, XtCBorderWidth, XtRDimension, sizeof(Dimension),
  59.       offset(core.border_width), XtRDimension, (caddr_t)&defwidth},
  60.    {XtNstate, XtCState, XtRState, sizeof(XtState), 
  61.       offset(toggle.state), XtRState, (caddr_t)&defState},
  62. };
  63. #undef offset
  64.  
  65. static XtActionsRec toggleactionsList[] =
  66. {
  67.   {"toggle",        Toggle},
  68.   {"notify",        Notify},
  69.   {"highlight",    Highlight},
  70.   {"unhighlight",    Unhighlight},
  71. };
  72.  
  73.   /* ...ClassData must be initialized at compile time.  Must
  74.      initialize all substructures.  (Actually, last two here
  75.      need not be initialized since not used.)
  76.   */
  77. ToggleClassRec toggleClassRec = {
  78.   {
  79.     (WidgetClass) &commandClassRec,    /* superclass          */    
  80.     "Toggle",                /* class_name          */
  81.     sizeof(ToggleRec),            /* size              */
  82.     ClassInitialize,            /* class_initialize      */
  83.     NULL,                /* class_part_initialize  */
  84.     FALSE,                /* class_inited          */
  85.     Initialize,            /* initialize          */
  86.     NULL,                /* initialize_hook      */
  87.     XtInheritRealize,            /* realize          */
  88.     toggleactionsList,            /* actions          */
  89.     XtNumber(toggleactionsList),    /* num_actions          */
  90.     resources,                /* resources          */
  91.     XtNumber(resources),        /* resource_count      */
  92.     NULLQUARK,                /* xrm_class          */
  93.     FALSE,                /* compress_motion      */
  94.     TRUE,                /* compress_exposure      */
  95.     TRUE,                /* compress_enterleave    */
  96.     FALSE,                /* visible_interest      */
  97.     NULL,                /* destroy          */
  98.     XtInheritResize,            /* resize          */
  99.     XtInheritExpose,            /* expose          */
  100.     SetValues,                /* set_values          */
  101.     NULL,                /* set_values_hook      */
  102.     XtInheritSetValuesAlmost,        /* set_values_almost      */
  103.     NULL,                /* get_values_hook      */
  104.     NULL,                /* accept_focus          */
  105.     XtVersion,                /* version          */
  106.     NULL,                /* callback_private      */
  107.     toggleTranslations,        /* tm_table          */
  108.     NULL,                /* query_geometry      */
  109.     XtInheritDisplayAccelerator,    /* display_accelerator      */
  110.     NULL,                /* extension            */
  111.   },  /* CoreClass fields initialization */
  112.   {
  113.     0,                                     /* field not used    */
  114.   },  /* LabelClass fields initialization */
  115.   {
  116.     0,                                     /* field not used    */
  117.   },  /* CommandClass fields initialization */
  118.   {
  119.     0,                                     /* field not used    */
  120.   },  /* ToggleClass fields initialization */
  121. };
  122.  
  123.   /* for public consumption */
  124. WidgetClass toggleWidgetClass = (WidgetClass) &toggleClassRec;
  125.  
  126. /****************************************************************
  127.  *
  128.  * Private Procedures
  129.  *
  130.  ****************************************************************/
  131.  
  132. static void CvtStringToState();
  133.  
  134. static XrmQuark    XrmQEtoggleon;
  135. static XrmQuark    XrmQEtoggleoff;
  136.  
  137. static void ClassInitialize()
  138. {
  139.  
  140.     XrmQEtoggleon   = XrmStringToQuark("toggleon");
  141.     XrmQEtoggleoff = XrmStringToQuark("toggleoff");
  142.  
  143.     XtAddConverter( XtRString, XtRState, CvtStringToState, NULL, 0 );
  144. } /* ClassInitialize */
  145.  
  146. /* ARGSUSED */
  147. static void CvtStringToState(args, num_args, fromVal, toVal)
  148. XrmValuePtr *args;        /* unused */
  149. Cardinal    *num_args;    /* unused */
  150. XrmValuePtr fromVal;
  151. XrmValuePtr toVal;
  152. {
  153.     static XtState e;
  154.     XrmQuark q;
  155.     char *s = (char *) fromVal->addr;
  156.     char lowerName[1000];
  157.     int i;
  158.  
  159.     if (s == NULL) return;
  160.  
  161.     for (i=0; i<=strlen(s); i++) {
  162.         char c = s[i];
  163.     lowerName[i] = isupper(c) ? (char) tolower(c) : c;
  164.     }
  165.  
  166.     q = XrmStringToQuark(lowerName);
  167.  
  168.     toVal->size = sizeof(XtState);
  169.     toVal->addr = (caddr_t) &e;
  170.  
  171.     if (q == XrmQEtoggleon)  { e = XtToggleOn; return; }
  172.     if (q == XrmQEtoggleoff) { e = XtToggleOff; return; }
  173.  
  174.     toVal->size = 0;
  175.     toVal->addr = NULL;
  176. };
  177.  
  178. /* ARGSUSED */
  179. static void Initialize(request, new)
  180. Widget request, new;
  181. {
  182.     ToggleWidget tw = (ToggleWidget)new;
  183.  
  184.     tw->command.set = (int)tw->toggle.state;
  185.     if (tw->toggle.state == XtToggleOn)
  186.        tw->label.normal_GC = tw->command.inverse_GC;
  187.  
  188. static Region HighlightRegion(cbw)
  189.     ToggleWidget cbw;
  190. {
  191.     static Region outerRegion = NULL, innerRegion, emptyRegion;
  192.     XRectangle rect;
  193.  
  194.     if (outerRegion == NULL) {
  195.     /* save time by allocating scratch regions only once. */
  196.     outerRegion = XCreateRegion();
  197.     innerRegion = XCreateRegion();
  198.     emptyRegion = XCreateRegion();
  199.     }
  200.  
  201.     rect.x = rect.y = 0;
  202.     rect.width = cbw->core.width;
  203.     rect.height = cbw->core.height;
  204.     XUnionRectWithRegion( &rect, emptyRegion, outerRegion );
  205.     rect.x = rect.y =  cbw->command.highlight_thickness;
  206.     rect.width -= cbw->command.highlight_thickness * 2;
  207.     rect.height -= cbw->command.highlight_thickness * 2;
  208.     XUnionRectWithRegion( &rect, emptyRegion, innerRegion );
  209.     XSubtractRegion( outerRegion, innerRegion, outerRegion );
  210.     return outerRegion;
  211. }
  212. /***************************
  213. *
  214. *  Action Procedures
  215. *
  216. ***************************/
  217.  
  218. /* ARGSUSED */
  219. static void Toggle(w,event,params,num_params)
  220.      Widget w;
  221.      XEvent *event;
  222.      String *params;        /* unused */
  223.      Cardinal *num_params;    /* unused */
  224. {
  225.     ToggleWidget tw = (ToggleWidget)w;
  226.  
  227.     if (tw->toggle.state == XtToggleOn) {
  228.        tw->toggle.state = XtToggleOff;
  229.        tw->command.set = FALSE;
  230.        tw->label.normal_GC = tw->command.normal_GC;
  231.        tw->command.highlighted = TRUE; 
  232.     }
  233.     else {
  234.        tw->toggle.state = XtToggleOn;
  235.        tw->command.set = TRUE;
  236.        tw->label.normal_GC = tw->command.inverse_GC;
  237.     }
  238. XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, TRUE);
  239. /*    (tw->core.widget_class->core_class.expose)(w, event, NULL);*/
  240. }
  241.  
  242. /* ARGSUSED */
  243. static void Notify(w,event,params,num_params)
  244. Widget w;
  245. XEvent *event;
  246. String *params;        /* unused */
  247. Cardinal *num_params;        /* unused */
  248. {
  249.     ToggleWidget tw = (ToggleWidget)w;
  250.     if (tw->command.callbacks != NULL)
  251.          XtCallCallbacks(w, XtNcallback, tw->toggle.state);
  252.     (tw->core.widget_class->core_class.expose)(w, event, NULL);
  253. }
  254.  
  255. /* ARGSUSED */
  256. static void Highlight(w,event,params,num_params)
  257. Widget w;
  258. XEvent *event;
  259. String *params;        /* unused */
  260. Cardinal *num_params;    /* unused */
  261. {
  262.     ToggleWidget tw = (ToggleWidget)w;
  263.  
  264.     if (tw->command.set == FALSE)  {
  265.        tw->command.highlighted = TRUE;
  266.        (tw->core.widget_class->core_class.expose)(w, event,
  267.          HighlightRegion(tw));
  268.     }
  269. }
  270.  
  271. /* ARGSUSED */
  272. static void Unhighlight(w,event,params,num_params)
  273. Widget w;
  274. XEvent *event;
  275. String *params;        /* unused */
  276. Cardinal *num_params;    /* unused */
  277. {
  278.     ToggleWidget tw = (ToggleWidget)w;
  279.  
  280.     tw->command.highlighted = FALSE;
  281.     if (tw->command.set == FALSE) {
  282.        (tw->core.widget_class->core_class.expose)(w, event,
  283.            HighlightRegion(tw));
  284.     }
  285. }
  286.  
  287.  
  288. /*
  289.  * Set specified arguments into widget
  290.  */
  291. /* ARGSUSED */
  292. static Boolean SetValues (current, request, new)
  293. Widget current, request, new;
  294. {
  295.     ToggleWidget tw = (ToggleWidget)current;
  296.     ToggleWidget tnew = (ToggleWidget)new;
  297.  
  298.     if (tnew->toggle.state != tw->toggle.state) {
  299.         /* the resource XtNstate is modified */
  300.        tnew->command.set = !(tw->command.set);
  301.     }
  302.     else if (tnew->core.sensitive != tw->core.sensitive  
  303.         && tnew->core.sensitive) {
  304.         /* the resource XtNsensitive is modified */
  305.        tnew->command.set = (Boolean)tw->toggle.state;
  306.     }
  307.     else return FALSE;
  308.  
  309.     if (tnew->toggle.state == XtToggleOn)
  310.        tnew->label.normal_GC = tnew->command.inverse_GC;
  311.     else
  312.        tnew->label.normal_GC = tnew->command.normal_GC;
  313.  
  314.     return TRUE;
  315. }
  316.