home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / xpaint-247 / colormap.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  8KB  |  244 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1992, 1993, David Koblas (koblas@netcom.com)            | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. /* $Id: Colormap.c,v 1.3 1996/04/19 09:00:17 torsten Exp $ */
  16.  
  17. #include <math.h>
  18. #include <X11/IntrinsicP.h>
  19. #include <X11/Shell.h>
  20. #include <X11/StringDefs.h>
  21. #include "ColormapP.h"
  22.  
  23. static XtResource resources[] =
  24. {
  25. #define offset(field) XtOffset(ColormapWidget, color.field)
  26.     {XtNcallback, XtCCallback, XtRCallback, sizeof(XtPointer),
  27.      offset(callbacks), XtRCallback, (XtPointer) None},
  28.     {XtNcolor, XtCColor, XtRPixel, sizeof(Pixel),
  29.      offset(pixel), XtRImmediate, (XtPointer) None},
  30.     {XtNreadOnly, XtCReadOnly, XtRBoolean, sizeof(Boolean),
  31.      offset(editable), XtRImmediate, (XtPointer) False},
  32.     {XtNcellWidth, XtCCellWidth, XtRInt, sizeof(int),
  33.      offset(cwidth), XtRImmediate, (XtPointer) 5},
  34.     {XtNcellHeight, XtCCellHeight, XtRInt, sizeof(int),
  35.      offset(cheight), XtRImmediate, (XtPointer) 5},
  36.     {XtNthickness, XtCThickness, XtRInt, sizeof(int),
  37.      offset(thickness), XtRImmediate, (XtPointer) 1},
  38.     {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  39.      offset(foreground), XtRString, XtDefaultForeground},
  40. #undef offset
  41. };
  42.  
  43. static void ResizeProc(ColormapWidget);
  44. static void ExposeProc(ColormapWidget, XExposeEvent *);
  45. static void InitializeProc(ColormapWidget, ColormapWidget);
  46. static Boolean SetValuesProc(Widget, Widget, Widget);
  47.  
  48. static void
  49. drawSelection(ColormapWidget w, int flag)
  50. {
  51.     XGCValues values;
  52.     int x, y;
  53.     GC gc;
  54.     int t = w->color.thickness, t2 = t / 2 + 1;
  55.  
  56.     values.line_width = w->color.thickness;
  57.     if (flag) {
  58.     values.foreground = w->color.foreground;
  59.     values.background = w->core.background_pixel;
  60.     } else {
  61.     values.foreground = w->core.background_pixel;
  62.     values.background = w->color.foreground;
  63.     }
  64.  
  65.     gc = XtGetGC((Widget) w, GCForeground | GCBackground | GCLineWidth, &values);
  66.  
  67.     x = (w->color.pixel % w->color.ncwidth) * w->color.cwidth;
  68.     y = (w->color.pixel / w->color.ncwidth) * w->color.cheight;
  69.  
  70.     XDrawRectangle(XtDisplay(w), XtWindow(w), gc,
  71.            x - t2, y - t2, w->color.cwidth, w->color.cheight);
  72. }
  73.  
  74. static void
  75. ColormapSelect(ColormapWidget w, XEvent * e,
  76.            String * str, Cardinal * n)
  77. {
  78.     int pixel;
  79.     XColor col;
  80.  
  81.     if ((e->xbutton.x / w->color.cwidth) > w->color.ncwidth)
  82.     return;
  83.     if ((e->xbutton.y / w->color.cheight) > w->color.ncheight)
  84.     return;
  85.  
  86.  
  87.     pixel = (e->xbutton.y / w->color.cheight) * w->color.ncwidth;
  88.     pixel += e->xbutton.x / w->color.cwidth;
  89.  
  90.     drawSelection(w, FALSE);
  91.     w->color.pixel = pixel;
  92.     drawSelection(w, TRUE);
  93.  
  94.     col.pixel = pixel;
  95.     col.flags = DoRed | DoGreen | DoBlue;
  96.     XQueryColor(XtDisplay(w), w->core.colormap, &col);
  97.  
  98.     XtCallCallbackList((Widget) w, w->color.callbacks, (XtPointer) & col);
  99. }
  100.  
  101. static XtActionsRec actions[] =
  102. {
  103.   /* {name, procedure}, */
  104.     {"Set", (XtActionProc) ColormapSelect},
  105. };
  106.  
  107. static char translations[] =
  108. "<BtnDown>:    Set()    \n\
  109. ";
  110.  
  111. ColormapClassRec colormapClassRec =
  112. {
  113.     {                /* core fields */
  114.     /* superclass               */ (WidgetClass) & widgetClassRec,
  115.     /* class_name               */ "Colormap",
  116.     /* widget_size              */ sizeof(ColormapRec),
  117.     /* class_initialize         */ NULL,
  118.     /* class_part_initialize    */ NULL,
  119.     /* class_inited             */ FALSE,
  120.     /* initialize               */ (XtInitProc) InitializeProc,
  121.     /* initialize_hook          */ NULL,
  122.     /* realize                  */ XtInheritRealize,
  123.     /* actions                  */ actions,
  124.     /* num_actions              */ XtNumber(actions),
  125.     /* resources                */ resources,
  126.     /* num_resources            */ XtNumber(resources),
  127.     /* xrm_class                */ NULLQUARK,
  128.     /* compress_motion          */ TRUE,
  129.     /* compress_exposure        */ TRUE,
  130.     /* compress_enterleave      */ TRUE,
  131.     /* visible_interest         */ FALSE,
  132.     /* destroy                  */ NULL,
  133.     /* resize                   */ (XtWidgetProc) ResizeProc,
  134.     /* expose                   */ (XtExposeProc) ExposeProc,
  135.     /* set_values               */ (XtSetValuesFunc) SetValuesProc,
  136.     /* set_values_hook          */ NULL,
  137.     /* set_values_almost        */ XtInheritSetValuesAlmost,
  138.     /* get_values_hook          */ NULL,
  139.     /* accept_focus             */ NULL,
  140.     /* version                  */ XtVersion,
  141.     /* callback_private         */ NULL,
  142.     /* tm_table                 */ translations,
  143.     /* query_geometry           */ XtInheritQueryGeometry,
  144.     /* display_accelerator      */ XtInheritDisplayAccelerator,
  145.     /* extension                */ NULL
  146.     },
  147.     {                /* colormap fields */
  148.     /* empty                    */ 0
  149.     }
  150. };
  151.  
  152. WidgetClass colormapWidgetClass = (WidgetClass) & colormapClassRec;
  153.  
  154. static void
  155. InitializeProc(ColormapWidget w, ColormapWidget new)
  156. {
  157.     Widget shell = XtParent(w);
  158.     Visual *visual = NULL;
  159.  
  160.     while (!XtIsShell(shell))
  161.     shell = XtParent(shell);
  162.  
  163.     XtVaGetValues(shell, XtNvisual, &visual, NULL);
  164.     if (visual == NULL)
  165.     visual = DefaultVisualOfScreen(XtScreen(w));
  166.  
  167.     new->color.ncel = visual->map_entries;
  168.     new->color.ncwidth = (int) sqrt((double) new->color.ncel);
  169.     new->color.ncheight = (new->color.ncel + new->color.ncwidth - 1) /
  170.     new->color.ncwidth;
  171.     if (new->core.width == 0)
  172.     new->core.width = new->color.cwidth * new->color.ncwidth;
  173.     else
  174.     new->color.cwidth = (int) new->core.width / (int) new->color.ncwidth;
  175.     if (new->core.height == 0)
  176.     new->core.height = new->color.cheight * new->color.ncheight;
  177.     else
  178.     new->color.cheight = new->core.height / new->color.ncheight;
  179. }
  180.  
  181. static void
  182. ResizeProc(ColormapWidget w)
  183. {
  184.     w->color.cwidth = w->core.width / w->color.ncwidth;
  185.     w->color.cheight = w->core.height / w->color.ncheight;
  186. }
  187.  
  188. static void
  189. ExposeProc(ColormapWidget w, XExposeEvent * event)
  190. {
  191.     Display *dpy = XtDisplay(w);
  192.     Window win = XtWindow(w);
  193.     GC gc = XCreateGC(dpy, win, 0, NULL);
  194.     int color = 0;
  195.     int x, y, X, Y;
  196.     int nvert = w->color.ncwidth;
  197.     int nhoriz = w->color.ncheight;
  198.     int t = w->color.thickness;
  199.  
  200.     for (Y = y = 0; y < nhoriz; y++, Y += w->color.cheight) {
  201.     for (X = x = 0; x < nvert; x++, X += w->color.cwidth) {
  202.         XSetForeground(dpy, gc, color++);
  203.         XFillRectangle(dpy, win, gc, X, Y,
  204.               w->color.cwidth - 1 - t, w->color.cheight - 1 - t);
  205.     }
  206.     }
  207.  
  208.     XFreeGC(dpy, gc);
  209.     drawSelection(w, TRUE);
  210. }
  211.  
  212. static Boolean
  213. SetValuesProc(Widget curArg, Widget request, Widget newArg)
  214. {
  215.     ColormapWidget cur = (ColormapWidget) curArg;
  216.     ColormapWidget new = (ColormapWidget) newArg;
  217.  
  218.     if (cur->color.pixel != new->color.pixel) {
  219.     drawSelection(cur, FALSE);
  220.     drawSelection(new, TRUE);
  221.     }
  222.     return False;
  223. }
  224.  
  225.  
  226. void
  227. CwGetColor(Widget cw, XColor * col)
  228. {
  229.     ColormapWidget w = (ColormapWidget) cw;
  230.  
  231.     col->pixel = w->color.pixel;
  232.     col->flags = DoRed | DoGreen | DoBlue;
  233.     XQueryColor(XtDisplay(w), w->core.colormap, col);
  234. }
  235.  
  236. void
  237. CwSetColor(Widget cw, XColor * col)
  238. {
  239.     ColormapWidget w = (ColormapWidget) cw;
  240.  
  241.     col->pixel = w->color.pixel;
  242.     XStoreColor(XtDisplay(w), w->core.colormap, col);
  243. }
  244.