home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgc / dashlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-06  |  4.9 KB  |  183 lines

  1. /*
  2. ** dashlist.c
  3. **
  4. ** How to make a widget to choose a dashlist.
  5. **
  6. ** NOTE: This file uses static variables.  Therefore, trying to use these
  7. **       functions to create more than one of these dashlist choice things
  8. **       will fail in a big way.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <X11/Intrinsic.h>
  13. #include <X11/StringDefs.h>
  14. #include <X11/Xaw/Form.h>
  15. #include <X11/Xaw/Label.h>
  16. #include <X11/Xaw/Toggle.h>
  17. #include "xgc.h"
  18.  
  19. static void change_dashlist();
  20. extern void interpret();
  21.  
  22. extern XStuff X;
  23.  
  24. static short dashlist = 240;    /* in binary, becomes the dashlist
  25.                    (240 = XXXX____) */
  26. static Widget *dashes;            /* the toggle widgets */
  27.  
  28. /* create_dashlist_choice(w)
  29. ** -------------------------
  30. ** Inside w (a form widget), creates a bunch of little toggle buttons
  31. ** in a row, representing the dash list.  There's also a label so
  32. ** the user knows what it is.
  33. */
  34.  
  35. void
  36. create_dashlist_choice(w)
  37.      Widget w;
  38. {
  39.   /* callback list for the toggle widgets */
  40.   static XtCallbackRec callbacklist[] = {
  41.     {(XtCallbackProc) change_dashlist, NULL},
  42.     {NULL,                             NULL}
  43.   };
  44.  
  45.   /* ArgList for the label */
  46.   static Arg labelargs[] = {
  47.     {XtNborderWidth,  (XtArgVal) 0},
  48.     {XtNjustify,      (XtArgVal) XtJustifyRight},
  49.     {XtNvertDistance, (XtArgVal) 4}
  50.   };
  51.  
  52.   /* ArgList for the toggles */
  53.   static Arg dashargs[] = {
  54.     {XtNcallback,           (XtArgVal) NULL},
  55.     {XtNhorizDistance,      (XtArgVal) NULL},
  56.     {XtNfromHoriz,          (XtArgVal) NULL},
  57.     {XtNwidth,              (XtArgVal) 10},
  58.     {XtNheight,             (XtArgVal) 10},
  59.     {XtNhighlightThickness, (XtArgVal) 1},
  60.     {XtNstate,              (XtArgVal) False},
  61.     {XtNlabel,              (XtArgVal) ""}
  62.   };
  63.  
  64.   static Widget label;        /* the label, of course */
  65.   static int *dashinfo;        /* contains integers saying which bit
  66.                    a particular button is; sent to
  67.                    change_dashlist to tell it which
  68.                    bit got changed */
  69.   int i;            /* counter */
  70.  
  71.   char name[11];
  72.  
  73.   /* allocate space for stuff that we don't know the size of yet */
  74.   dashes = (Widget *) malloc(DASHLENGTH * sizeof(Widget));
  75.   dashinfo = (int *) malloc(DASHLENGTH * sizeof(int));
  76.  
  77.   /* make the label widget */
  78.   label = XtCreateManagedWidget("dashlist",labelWidgetClass,w,
  79.                 labelargs,XtNumber(labelargs));
  80.  
  81.   dashargs[0].value = (XtArgVal) callbacklist;
  82.  
  83.   for (i=0;i<DASHLENGTH;++i) {    /* go through all the buttons */
  84.     if (i==0) {            /* offset the first one from the label */
  85.       dashargs[1].value = (XtArgVal) 10;
  86.       dashargs[2].value = (XtArgVal) label;
  87.     }
  88.     else {            /* put it directly to the right of the
  89.                    last one, no space in between */
  90.       dashargs[1].value = (XtArgVal) -1;
  91.       dashargs[2].value = (XtArgVal) dashes[i-1];
  92.     }
  93.  
  94.     /* set its original state depending on the state of that
  95.     ** bit of the dashlist */
  96.  
  97.     if (dashlist&1<<i)
  98.       dashargs[6].value = (XtArgVal) True;
  99.     else
  100.       dashargs[6].value = (XtArgVal) False;
  101.  
  102.     sprintf(name,"dashlist%d",i);
  103.  
  104.     dashinfo[i] = i;        /* which bit we're on; this is needed
  105.                    in change_dashlist (the callback) */
  106.     callbacklist[0].closure = (caddr_t) &dashinfo[i];
  107.  
  108.     dashes[i] = XtCreateManagedWidget(name,toggleWidgetClass,w,
  109.                   dashargs,XtNumber(dashargs));
  110.   }
  111. }
  112.  
  113. /* change_dashlist(w,closure,call_data)
  114. ** ------------------------------------
  115. ** This function is called when the user toggles a toggle widget.  It
  116. ** makes the appropriate change to the dashlist and sends it off
  117. ** to interpret().
  118. ** Funny args are because it's a callback.
  119. */
  120.  
  121. /*ARGSUSED*/
  122. static void
  123. change_dashlist(w,closure,call_data)
  124.      Widget w;
  125.      caddr_t closure;
  126.      caddr_t call_data;
  127. {
  128.   int num;            /* what number button it is */
  129.   Boolean on;            /* is it currently on or off? */
  130.  
  131.   char buf[80];            /* string to send to interpret() */
  132.  
  133.   static Arg args[] = {
  134.     {XtNstate,    (XtArgVal) NULL}
  135.   };
  136.  
  137.   /* set up ArgList so that 'on' will contain the state */
  138.   args[0].value = (XtArgVal) &on;
  139.  
  140.   num = * (int *) closure;    /* we put it here back in the last function */
  141.   XtGetValues(w,args,XtNumber(args));
  142.  
  143.   /* modify the dashlist as appropriate. */
  144.   if (on) {            
  145.     dashlist |= 1<<num;        
  146.   }
  147.   else {            
  148.     dashlist &= ~(1<<num);    
  149.   }
  150.  
  151.   /* now tell interpret() about it */
  152.   sprintf(buf,"dashlist %d\n",dashlist); 
  153.   interpret(buf,FALSE);
  154. }
  155.  
  156. /* update_dashlist(newdash)
  157. ** ------------------------
  158. ** Updates the display of the dashlist so that it corresponds to
  159. ** newdash.
  160. */
  161.  
  162. void
  163. update_dashlist(newdash)
  164.      int newdash;
  165. {
  166.   int i;            /* counter */
  167.   static Arg dashargs[] = {    /* Arglist for setting toggle state */
  168.     {XtNstate,   (XtArgVal) NULL}
  169.   };
  170.  
  171.   /* first set the internal representation */
  172.   dashlist = newdash;
  173.  
  174.   for (i = 0; i < DASHLENGTH; ++i) {
  175.     if (newdash & 1<<i)     /* if it's set, make it look that way */
  176.       dashargs[0].value = (XtArgVal) True;
  177.     else
  178.       dashargs[0].value = (XtArgVal) False;
  179.  
  180.     XtSetValues(dashes[i],dashargs,XtNumber(dashargs));
  181.   }
  182. }
  183.