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

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