home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / xwhosup / part01 / Bar.c next >
Encoding:
C/C++ Source or Header  |  1992-12-30  |  10.5 KB  |  375 lines

  1. #include <X11/copyright.h>
  2.  
  3. /* $XConsortium: Bar.c,v 1.2 88/10/25 17:40:25 swick Exp $ */
  4. /* Copyright    Massachusetts Institute of Technology    1987, 1988 */
  5.  
  6. #include <X11/IntrinsicP.h>
  7. #include <X11/StringDefs.h>
  8. #include "BarP.h"
  9.  
  10. static Dimension def_dimension    = ~0;
  11. static XtOrientation def_orient = XtorientHorizontal;
  12. #define DEF_LENGTH  ((Dimension) 200)
  13. #define DEF_THICKNESS    ((Dimension) 15)
  14.  
  15. static XtResource resources[] = {
  16. #define offset(field) XtOffset(BarWidget, bar.field)
  17. #define Offset(field) XtOffset(BarWidget, field)
  18.     /* {name, class, type, size, offset, default_type, default_addr}, */
  19.     /*
  20.      * We define these core class values to set defaults, so we can
  21.      * initialize correctly
  22.      */
  23.     {XtNwidth, XtCWidth, XtRDimension, sizeof(Dimension),
  24.     Offset(core.width), XtRDimension, (caddr_t)&def_dimension},
  25.     {XtNheight, XtCHeight, XtRDimension, sizeof(Dimension),
  26.     Offset(core.height), XtRDimension, (caddr_t)&def_dimension},
  27.     /* Private bar widget resources */
  28.     {XtNlength, XtCLength, XtRDimension, sizeof(Dimension),
  29.     offset(length), XtRDimension, (caddr_t) &def_dimension},
  30.     {XtNthickness, XtCThickness, XtRDimension, sizeof(Dimension),
  31.     offset(thickness), XtRDimension, (caddr_t) &def_dimension},
  32.     {XtNorientation, XtCOrientation, XtROrientation, sizeof(XtOrientation),
  33.     offset(orientation), XtROrientation, (caddr_t) &def_orient},
  34. #undef offset
  35. #undef Offset
  36. };
  37.  
  38. static void ClassInitialize();
  39. static void Initialize();
  40. static void Resize();
  41. static void Redisplay();
  42.  
  43. BarClassRec barClassRec = {
  44.   { /* core fields */
  45.     /* superclass        */    (WidgetClass) &widgetClassRec,
  46.     /* class_name        */    "Bar",
  47.     /* widget_size        */    sizeof(BarRec),
  48.     /* class_initialize        */    ClassInitialize,
  49.     /* class_part_initialize    */    NULL,
  50.     /* class_inited        */    FALSE,
  51.     /* initialize        */    Initialize,
  52.     /* initialize_hook        */    NULL,
  53.     /* realize            */    XtInheritRealize,
  54.     /* actions            */    NULL,
  55.     /* num_actions        */    0,
  56.     /* resources        */    resources,
  57.     /* num_resources        */    XtNumber(resources),
  58.     /* xrm_class        */    NULLQUARK,
  59.     /* compress_motion        */    TRUE,
  60.     /* compress_exposure    */    TRUE,
  61.     /* compress_enterleave    */    TRUE,
  62.     /* visible_interest        */    FALSE,
  63.     /* destroy            */    NULL,
  64.     /* resize            */    Resize,
  65.     /* expose            */    Redisplay,
  66.     /* set_values        */    NULL,
  67.     /* set_values_hook        */    NULL,
  68.     /* set_values_almost    */    XtInheritSetValuesAlmost,
  69.     /* get_values_hook        */    NULL,
  70.     /* accept_focus        */    NULL,
  71.     /* version            */    XtVersion,
  72.     /* callback_private        */    NULL,
  73.     /* tm_table            */    NULL,
  74.     /* query_geometry        */    XtInheritQueryGeometry,
  75.     /* display_accelerator    */    XtInheritDisplayAccelerator,
  76.     /* extension        */    NULL
  77.   },
  78.   { /* bar fields */
  79.     /* gray            */    NULL
  80.   }
  81. };
  82.  
  83. WidgetClass barWidgetClass = (WidgetClass)&barClassRec;
  84.  
  85. #ifdef    UGLY_GRAYS
  86. static char gray0_bits[] = {
  87.    0x00, 0x00, 0x00};
  88. static char gray1_bits[] = {
  89.    0x00, 0x02, 0x00};
  90. static char gray2_bits[] = {
  91.    0x00, 0x03, 0x00};
  92. static char gray3_bits[] = {
  93.    0x00, 0x03, 0x02};
  94. static char gray4_bits[] = {
  95.    0x00, 0x07, 0x02};
  96. static char gray5_bits[] = {
  97.    0x04, 0x07, 0x02};
  98. static char gray6_bits[] = {
  99.    0x04, 0x07, 0x03};
  100. static char gray7_bits[] = {
  101.    0x05, 0x07, 0x03};
  102. static char gray8_bits[] = {
  103.    0x05, 0x07, 0x07};
  104. static char gray9_bits[] = {
  105.    0xff, 0xff, 0xff};
  106.  
  107. static char *gray_bits[MAXGRAY] = {
  108.     gray0_bits,
  109.     gray1_bits,
  110.     gray2_bits,
  111.     gray3_bits,
  112.     gray4_bits,
  113.     gray5_bits,
  114.     gray6_bits,
  115.     gray7_bits,
  116.     gray8_bits,
  117.     gray9_bits,
  118. };
  119. #else
  120. static char gray0_bits[] = {
  121.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  123.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  124. static char gray1_bits[] = {
  125.    0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
  126.    0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
  127.    0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88};
  128. static char gray2_bits[] = {
  129.    0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55,
  130.    0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55,
  131.    0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55};
  132. static char gray3_bits[] = {
  133.    0xdd, 0xdd, 0x77, 0x77, 0xdd, 0xdd, 0x77, 0x77, 0xdd, 0xdd, 0x77, 0x77,
  134.    0xdd, 0xdd, 0x77, 0x77, 0xdd, 0xdd, 0x77, 0x77, 0xdd, 0xdd, 0x77, 0x77,
  135.    0xdd, 0xdd, 0x77, 0x77, 0xdd, 0xdd, 0x77, 0x77};
  136. static char gray4_bits[] = {
  137.    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  138.    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  139.    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  140.  
  141. static char *gray_bits[MAXGRAY] = {
  142.     gray0_bits,
  143.     gray1_bits,
  144.     gray2_bits,
  145.     gray3_bits,
  146.     gray4_bits,
  147. };
  148. #endif
  149.  
  150. #define stripe_width 14
  151. #define stripe_height 14
  152. static char stripe_bits[] = {
  153.    0x0f, 0x3c, 0x07, 0x3e, 0x03, 0x3f, 0x81, 0x3f, 0xc0, 0x3f, 0xe0, 0x1f,
  154.    0xf0, 0x0f, 0xf8, 0x07, 0xfc, 0x03, 0xfe, 0x01, 0xff, 0x00, 0x7f, 0x20,
  155.    0x3f, 0x30, 0x1f, 0x38};
  156.  
  157. static void
  158. ClassInitialize()
  159. {
  160.     XtAddConverter( XtRString, XtROrientation, XmuCvtStringToOrientation,
  161.             NULL, (Cardinal)0 );
  162. }
  163.  
  164. static void
  165. SetDimensions(w)
  166. BarWidget w;
  167. {
  168.     if (w->bar.orientation == XtorientVertical) {
  169.     w->bar.length = w->core.height;
  170.     w->bar.thickness = w->core.width;
  171.     }
  172.     else {
  173.     w->bar.length = w->core.width;
  174.     w->bar.thickness = w->core.height;
  175.     }
  176. }
  177.  
  178. /* ARGSUSED */
  179. static void
  180. Initialize(request, new)
  181. Widget request;        /* what the client asked for */
  182. Widget new;        /* what we're going to give him */
  183. {
  184.     BarWidget w = (BarWidget) new;
  185.     XGCValues gcValues;
  186.     BarClassPart *bcp = &(barClassRec.bar_class);
  187.     int i;
  188.     XColor color, exact;
  189.  
  190.     if (bcp->gray == (Pixmap *) NULL) {
  191.     /*
  192.      *  We initialize this here instead of ClassInitialize because
  193.      *  we need a window to initialize this in. A cleaner way to do
  194.      *  this would be to use a separate set of Pixmaps for each
  195.      *  widget, so that widgets can be on different screen/visuals.
  196.      *  But then we'd have to fix it so this thing understood
  197.      *  colours, visuals, more resources...  Laziness wins.
  198.      */
  199.     bcp->gray = (Pixmap *) XtMalloc(MAXGRAY * sizeof(Pixmap));
  200.     for(i = 0; i < MAXGRAY; i++) {
  201.         bcp->gray[i] = XCreatePixmapFromBitmapData(XtDisplay(new),
  202.          DefaultRootWindow(XtDisplay(new)), gray_bits[i],
  203. #ifdef    UGLY_GRAYS
  204.                                   3, 3,
  205. #else
  206.                                   16, 16,
  207. #endif
  208.          WhitePixelOfScreen(XtScreen(new)),
  209.          BlackPixelOfScreen(XtScreen(new)), 1);
  210.     }
  211.     XAllocNamedColor(XtDisplay(new),
  212.              DefaultColormap(XtDisplay(new), DefaultScreen(XtDisplay(new))),
  213.              "Yellow", &color, &exact);
  214.     bcp->stripe = XCreatePixmapFromBitmapData(XtDisplay(new),
  215.          DefaultRootWindow(XtDisplay(new)), stripe_bits, stripe_width,
  216.          stripe_height, color.pixel,
  217.          BlackPixelOfScreen(XtScreen(new)),
  218.          DefaultDepth(XtDisplay(new), DefaultScreen(XtDisplay(new))));
  219.     }
  220.     gcValues.stipple = bcp->gray[0];
  221.     gcValues.fill_style = FillOpaqueStippled;
  222.     w->bar.gc = XCreateGC(XtDisplay(new), DefaultRootWindow(XtDisplay(new)),
  223.      GCFillStyle | GCStipple, &gcValues);
  224.     /* Width and height override length and thickness */
  225.     if (w->bar.length == def_dimension)
  226.     w->bar.length = DEF_LENGTH;
  227.     
  228.     if (w->bar.thickness == def_dimension)
  229.     w->bar.thickness = DEF_THICKNESS;
  230.     
  231.     if (w->core.width == def_dimension)
  232.     w->core.width = (w->bar.orientation == XtorientVertical)
  233.         ? w->bar.thickness : w->bar.length;
  234.  
  235.     if (w->core.height == def_dimension)
  236.     w->core.height = (w->bar.orientation == XtorientHorizontal)
  237.         ? w->bar.thickness : w->bar.length;
  238.     w->bar.striped = 0;
  239.     w->bar.values = NULL;
  240.     w->bar.nvalues = 0;
  241.  
  242.     SetDimensions(w);
  243. }
  244.  
  245. /* ARGSUSED */
  246. static void
  247. Resize(gw)
  248. Widget gw;
  249. {
  250.     SetDimensions((BarWidget)gw);
  251.     Redisplay(gw, (XEvent*)NULL, (Region)NULL);
  252. }
  253.  
  254. /* ARGSUSED */
  255. static void
  256. Redisplay(gw, event, region)
  257. Widget gw;
  258. XEvent *event;
  259. Region region;
  260. {
  261.     BarWidget w = (BarWidget) gw;
  262.     int x, y;
  263.     unsigned int width, height;
  264.     int maxvalue, i;
  265.     int inc_gray;
  266.     int igray; 
  267.     int vertical = (w->bar.orientation == XtorientVertical);;
  268.  
  269.     if (! XtIsRealized(gw))
  270.     return;
  271.  
  272.     if (w->bar.striped)
  273.     {
  274.     if(vertical) {
  275.         width = w->bar.thickness;
  276.         height = w->core.height;
  277.     }
  278.     else {
  279.         height = w->bar.thickness;
  280.         width = w->core.width;
  281.     }
  282.     XSetTile(XtDisplay(gw), w->bar.gc, barClassRec.bar_class.stripe);
  283.     XSetFillStyle(XtDisplay(gw), w->bar.gc, FillTiled);
  284.     XFillRectangle(XtDisplay(gw), XtWindow(gw), w->bar.gc,
  285.            0, 0, width, height);
  286.     XSetFillStyle(XtDisplay(gw), w->bar.gc, FillOpaqueStippled);
  287.     return;
  288.     }
  289.  
  290.     for(maxvalue = 0, i = 0; i < w->bar.nvalues; i++)
  291.     maxvalue += w->bar.values[i];
  292.     if(maxvalue <= 0)
  293.     return;
  294.     x = y = 0;
  295.     if(vertical)
  296.     width = w->bar.thickness;
  297.     else
  298.     height = w->bar.thickness;
  299.     inc_gray = (w->bar.nvalues > 1) ? (MAXGRAY - 1) / (w->bar.nvalues - 1) :
  300.     MAXGRAY - 1;
  301.     for(igray = 0, i = 0; i < w->bar.nvalues; i++, igray += inc_gray) {
  302.     unsigned int rectlen = (w->bar.length * w->bar.values[i] +
  303.                 maxvalue / 2) / maxvalue;
  304.     if (rectlen == 0)
  305.         continue;
  306.     if (i == w->bar.nvalues - 1) {
  307.         if (vertical)
  308.         rectlen = w->core.height - y;
  309.         else
  310.         rectlen = w->core.width - x;
  311. #ifndef    UGLY_GRAYS
  312.         igray = MAXGRAY - 1;
  313. #endif
  314.     }
  315.     XSetStipple(XtDisplay(gw), w->bar.gc, barClassRec.bar_class.gray[igray]);
  316.     if(vertical) {
  317.         XFillRectangle(XtDisplay(gw), XtWindow(gw), w->bar.gc,
  318.          x, y, width, rectlen);
  319.         y += rectlen;
  320.     } else {
  321.         XFillRectangle(XtDisplay(gw), XtWindow(gw), w->bar.gc,
  322.          x, y, rectlen, height);
  323.         x += rectlen;
  324.     }
  325.     }
  326. }
  327.  
  328. /* Public routines. */
  329. void
  330. SetBarValues(gw, values, nvalues)
  331. Widget gw;
  332. int *values;
  333. int nvalues;
  334. {
  335.     BarWidget w = (BarWidget) gw;
  336.     int i;
  337.  
  338.     if (nvalues > MAXGRAY) {
  339. #define ERRFMT "Tried to set %d values for bar - maximum is %d\n"
  340.     char errbuf[sizeof(ERRFMT) + 32];
  341.     (void) sprintf(errbuf, ERRFMT, nvalues, MAXGRAY);
  342. #undef ERRFMT
  343.     XtWarning(errbuf);
  344.     nvalues = MAXGRAY;
  345.     }
  346.     if (nvalues < 0) {
  347. #define ERRFMT "Tried to set %d values for bar - it must be > 0\n"
  348.     char errbuf[sizeof(ERRFMT) + 32];
  349.     (void) sprintf(errbuf, ERRFMT, nvalues);
  350.     XtWarning(errbuf);
  351. #undef ERRFMT
  352.     return;
  353.     }
  354.     w->bar.nvalues = nvalues;
  355.     if (w->bar.values != NULL)
  356.     XtFree((char *) w->bar.values);
  357.     if (nvalues != 0)
  358.     w->bar.values = (int *) XtMalloc(nvalues * sizeof(int));
  359.     for(i = 0; i < nvalues; i++)
  360.     w->bar.values[i] = values[i];
  361.     w->bar.striped = 0;
  362.     Redisplay( gw, (XEvent*)NULL, (Region)NULL );
  363. }
  364.  
  365. void
  366. SetBarStripe(gw)
  367. Widget gw;
  368. {
  369.     BarWidget w = (BarWidget) gw;
  370.     int i;
  371.  
  372.     w->bar.striped = 1;
  373.     Redisplay( gw, (XEvent*)NULL, (Region)NULL );
  374. }
  375.