1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 
9 #include <dxconfig.h>
10 #include "../base/defines.h"
11 
12 
13 #include <X11/Xlib.h>
14 #include <X11/Intrinsic.h>
15 #include <Xm/Xm.h>
16 #include "ColorMapEditorP.h"
17 #include <Xm/DrawingA.h>
18 #include <Xm/DrawingAP.h>
19 #include "Grid.h"
20 
21 #ifndef MIN
22 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
23 #endif
24 #ifndef MAX
25 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
26 #endif
27 
28 /* External Functions */
29 
30 void PrintAllControlPoints(XmColorMapEditorWidget cmew); /* From ControlPoint.c */
31 
32 #define NUM_TICKS 11
33 
34 static void ResizeCallback( Widget w, int color,
35 			    XmDrawingAreaCallbackStruct* cb );
36 extern GC InitGC(Widget w, Pixel color );
37 
CreateGrid(Widget parent,Pixel color,Arg * args,int num_args)38 Widget CreateGrid( Widget parent, Pixel color, Arg* args, int num_args )
39 {
40     Arg wargs[40];
41     Widget w;
42     long                 mask;
43     XWindowAttributes    getatt;
44     XSetWindowAttributes setatt;
45 
46     (void)memcpy((char *)wargs, (char *)args, num_args * sizeof(Arg));
47     XtSetArg(wargs[num_args], XmNshadowThickness, 0); num_args++;
48     w = XmCreateDrawingArea(parent, "graph", wargs, num_args);
49     XtAddCallback(w, XmNexposeCallback, (XtCallbackProc)ResizeCallback, (XtPointer)color);
50     XtAddCallback(w, XmNresizeCallback, (XtCallbackProc)ResizeCallback, (XtPointer)color);
51     XtManageChild(w);
52 
53     /*
54      * Make sure button and motion events get passed through.
55      */
56     XGetWindowAttributes(XtDisplay(w), XtWindow(w), &getatt);
57 
58     mask = ButtonPressMask | ButtonReleaseMask | ButtonMotionMask;
59 
60     setatt.event_mask            = getatt.your_event_mask & ~mask;
61     setatt.do_not_propagate_mask = getatt.do_not_propagate_mask & ~mask;
62 
63     XChangeWindowAttributes(XtDisplay(w), XtWindow(w),
64                             CWEventMask | CWDontPropagate,
65                             &setatt);
66     return w;
67 }
68 
69 /*  Subroutine:	ResizeCallback
70  *  Purpose:	Redraw the grid when window newly exposed
71  */
ResizeCallback(Widget w,int color,XmDrawingAreaCallbackStruct * cb)72 static void ResizeCallback( Widget w, int color,
73 			    XmDrawingAreaCallbackStruct* cb )
74 {
75 XmColorMapEditorWidget cmew = (XmColorMapEditorWidget)XtParent(w);
76 
77     if( !XtIsRealized(w) )
78         {
79         return;
80         }
81     else
82 	{
83 	if(cb->reason == XmCR_EXPOSE)
84 	{
85 	    /*
86 	     * We need the first expose to get things drawn the first time.
87 	     */
88 	    PrintAllControlPoints(cmew);
89 	    if(!cmew->color_map_editor.first_grid_expose)
90 		return;
91 	    cmew->color_map_editor.first_grid_expose = False;
92 	}
93 	if(cmew->color_map_editor.draw_mode == CME_GRID)
94 	    DrawGrid(w, color);
95 	if((cmew->color_map_editor.draw_mode == CME_HISTOGRAM) ||
96 	   (cmew->color_map_editor.draw_mode == CME_LOGHISTOGRAM))
97 	    DrawHistogram(w, color);
98 	}
99 }
100 /*  Subroutine:	DrawGrid
101  *  Purpose: Do the actual grid drawing
102  */
103 
DrawGrid(Widget w,Pixel color)104 void DrawGrid(Widget w, Pixel color)
105 {
106 XmColorMapEditorWidget cmew;
107 int height, width, length, ypos, long_length, short_length;
108 GC gc;
109 int i;
110 Pixmap pixmap;
111 Pixel bg;
112 
113     if ((w == NULL)  || !XtIsRealized(w))
114 	return;
115 
116     cmew = (XmColorMapEditorWidget)XtParent(w);
117 
118     XtVaGetValues(w, XmNbackground, &bg, NULL);
119     height = w->core.height;
120     width = w->core.width;
121 
122     if(cmew->color_map_editor.grid_pixmap)
123 	XFreePixmap(XtDisplay(cmew), cmew->color_map_editor.grid_pixmap);
124 
125     pixmap = XCreatePixmap(XtDisplay(w),
126 			   XtWindow(w),
127 			   width,
128 			   height,
129 			   DefaultDepth(XtDisplay(w),
130 					XScreenNumberOfScreen(XtScreen(cmew))));
131     cmew->color_map_editor.grid_pixmap = pixmap;
132     gc = InitGC( w, color);
133     XSetForeground(XtDisplay(w), gc, bg);
134     XFillRectangle(XtDisplay(w), pixmap, gc, 0, 0, width, height);
135     XSetForeground(XtDisplay(w), gc, color);
136 
137     short_length = (int) ( (float)width/5.0);
138     long_length = short_length*2.0;
139 
140     /* Draw the vertical line */
141     XDrawLine(XtDisplay(w), pixmap, gc, 0, 0, 0, height);
142     /* Draw the ticks */
143     for (i = 0; i<NUM_TICKS; i++)
144 	{
145 	ypos =  (int) (float)((height-1)*i)/(NUM_TICKS-1);
146 	if ( (i == 0) || (i == NUM_TICKS-1) || (i == NUM_TICKS/2) )
147 	    {
148 	    length = long_length;
149 	    }
150 	else
151 	    {
152 	    length = short_length;
153 	    }
154 	XDrawLine(XtDisplay(w), pixmap, gc, 1, ypos, length, ypos);
155 	}
156     XtReleaseGC(w, gc);
157     XSetWindowBackgroundPixmap(XtDisplay(w), XtWindow(w), pixmap);
158     XClearWindow(XtDisplay(w), XtWindow(w));
159     PrintAllControlPoints(cmew);
160 }
DrawHistogram(Widget w,Pixel color)161 void DrawHistogram(Widget w, Pixel color)
162 {
163 int height, width, length, ypos1, ypos2;
164 GC gc;
165 int i;
166 XmColorMapEditorWidget cmew;
167 int *bins=NULL;
168 int num_bins;
169 int max_bin;
170 Pixmap pixmap;
171 Pixel bg;
172 
173     if ((w == NULL)  || !XtIsRealized(w))
174 	return;
175 
176     cmew = (XmColorMapEditorWidget)XtParent(w);
177 
178     if(cmew->color_map_editor.draw_mode == CME_HISTOGRAM)
179 	bins = cmew->color_map_editor.bins;
180     else if(cmew->color_map_editor.draw_mode == CME_LOGHISTOGRAM)
181 	bins = cmew->color_map_editor.log_bins;
182 
183     num_bins = cmew->color_map_editor.num_bins;
184 
185     XtVaGetValues(w, XmNbackground, &bg, NULL);
186     height = w->core.height;
187     width = w->core.width;
188 
189     if(cmew->color_map_editor.grid_pixmap)
190 	XFreePixmap(XtDisplay(cmew), cmew->color_map_editor.grid_pixmap);
191 
192     pixmap = XCreatePixmap(XtDisplay(w),
193 			   XtWindow(w),
194 			   width,
195 			   height,
196 			   DefaultDepth(XtDisplay(w),
197 				XScreenNumberOfScreen(XtScreen(w))));
198     cmew->color_map_editor.grid_pixmap = pixmap;
199     gc = InitGC( w, color);
200     XSetForeground(XtDisplay(w), gc, bg);
201     XFillRectangle(XtDisplay(w), pixmap, gc, 0, 0, width, height);
202     XSetForeground(XtDisplay(w), gc, color);
203     if(!bins)
204     {
205 	DrawGrid(w, color);
206 	return;
207     }
208     max_bin = 0;
209     for(i = 0; i < num_bins; i++)
210     {
211 	max_bin = MAX(max_bin, bins[i]);
212     }
213 
214     /* Draw the bins. Note: Invert y for X windows */
215     for(i = 0; i < num_bins; i++)
216     {
217 	length = ((float)bins[i]/(float)max_bin)*(float)(width-3);
218 	ypos1 =  (int) (float)((height-1)*(num_bins - i - 1))/(num_bins);
219 	XDrawLine(XtDisplay(w), pixmap, gc, 1, ypos1, length, ypos1);
220 	ypos2 =  (int) (float)((height-1)*(num_bins - i))/(num_bins);
221 	XDrawLine(XtDisplay(w), pixmap, gc, 1, ypos2, length, ypos2);
222 	/* Draw the vertical line */
223 	XDrawLine(XtDisplay(w), pixmap, gc, length, ypos1, length, ypos2);
224     }
225     XDrawLine(XtDisplay(w), pixmap, gc, 0, 0, 0, height);
226     XtReleaseGC(w, gc);
227     XSetWindowBackgroundPixmap(XtDisplay(w), XtWindow(w), pixmap);
228     XClearWindow(XtDisplay(w), XtWindow(w));
229     PrintAllControlPoints(cmew);
230 }
231