1 /*****************************************************************************
2   FILE           : $Source: /projects/higgs1/SNNS/CVS/SNNS/xgui/sources/ui_colEdit.c,v $
3   SHORTNAME      : colEdit.c
4   SNNS VERSION   : 4.2
5 
6   PURPOSE        : functions to manipulate the text, background and
7 		   selection color
8   NOTES          : uses the fixed palette defined in ui_color.c
9 
10   AUTHOR         : Ralf Huebner
11   DATE           : 27.5.1992
12 
13   CHANGED BY     :
14   RCS VERSION    : $Revision: 2.9 $
15   LAST CHANGE    : $Date: 1998/03/03 14:10:13 $
16 
17     Copyright (c) 1990-1995  SNNS Group, IPVR, Univ. Stuttgart, FRG
18     Copyright (c) 1996-1998  SNNS Group, WSI, Univ. Tuebingen, FRG
19 
20 ******************************************************************************/
21 #include <config.h>
22 
23 
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include <X11/Xlib.h>
28 #include <X11/Intrinsic.h>
29 #include <X11/StringDefs.h>
30 #include <X11/Shell.h>
31 #include <X11/Xaw3d/Simple.h>
32 #include <X11/Xaw3d/Box.h>
33 #include <X11/Xaw3d/Form.h>
34 #include <X11/Xaw3d/Toggle.h>
35 
36 #include "ui.h"
37 #include "ui_xWidgets.h"
38 #include "ui_color.h"
39 #include "ui_mainP.h"
40 #include "ui_main.h"
41 
42 #include "ui_colEdit.ph"
43 
44 
45 /*****************************************************************************
46   FUNCTION : ui_cancelColorEditPannel
47 
48   PURPOSE  : callback if the cancel button is pressed
49   RETURNS  : void
50   NOTES    : closes the pannel
51 
52   UPDATE   :
53 ******************************************************************************/
54 
ui_cancelColorEditPannel(Widget w,Widget pannel,caddr_t call_data)55 static void ui_cancelColorEditPannel (Widget w,Widget pannel,caddr_t call_data)
56 
57 {
58     XtDestroyWidget (pannel);
59 }
60 
61 
62 /*****************************************************************************
63   FUNCTION : ui_closeColorEditPannel
64 
65   PURPOSE  : sets the selected colors in the current display
66   RETURNS  : void
67   NOTES    :
68 
69   UPDATE   :
70 ******************************************************************************/
71 
ui_closeColorEditPannel(Widget w,Widget pannel,caddr_t call_data)72 static void ui_closeColorEditPannel (Widget w, Widget pannel, caddr_t call_data)
73 
74 {
75     (ui_set_displayPtr->setup).backgroundColorIndex = ui_currentBackCol;
76     (ui_set_displayPtr->setup).selectionColorIndex = ui_currentSelCol;
77     (ui_set_displayPtr->setup).textColorIndex = ui_currentTextCol;
78     XtDestroyWidget (pannel);
79 }
80 
81 
82 /*****************************************************************************
83   FUNCTION : ui_getDisplayColors
84 
85   PURPOSE  : reads the color indices out of the display
86   RETURNS  : void
87   NOTES    :
88 
89   UPDATE   :
90 ******************************************************************************/
91 
ui_getDisplayColors(void)92 static void ui_getDisplayColors (void)
93 
94 {
95     ui_currentBackCol = (ui_set_displayPtr->setup).backgroundColorIndex;
96     ui_currentSelCol = (ui_set_displayPtr->setup).selectionColorIndex;
97     ui_currentTextCol = (ui_set_displayPtr->setup).textColorIndex;
98 }
99 
100 
101 /*****************************************************************************
102   FUNCTION : ui_xCreateColButtonItem
103 
104   PURPOSE  : creates a widget for a color button
105   RETURNS  : Widget
106   NOTES    : the color is defined in pixel
107 
108   UPDATE   :
109 ******************************************************************************/
110 
ui_xCreateColButtonItem(Widget parent,unsigned long pixel,int xsize,int ysize,Widget left,Widget top)111 static Widget ui_xCreateColButtonItem (Widget parent, unsigned long pixel,
112 	int xsize, int ysize, Widget left, Widget top)
113 
114 {
115     Cardinal n;
116     Widget   w;
117     Arg	     args[15];
118 
119     n = 0;
120     XtSetArg(args[n], XtNinternalHeight, 1); n++;
121     XtSetArg(args[n], XtNinternalWidth , 1); n++;
122     XtSetArg(args[n], XtNfromVert , top);  n++;
123     XtSetArg(args[n], XtNfromHoriz, left);  n++;
124     XtSetArg(args[n], XtNleft  , XtChainLeft); n++;
125     XtSetArg(args[n], XtNright , XtChainLeft); n++;
126     XtSetArg(args[n], XtNtop   , XtChainTop); n++;
127     XtSetArg(args[n], XtNbottom, XtChainTop); n++;
128     XtSetArg(args[n], XtNwidth, xsize); n++;
129     XtSetArg(args[n], XtNheight, ysize); n++;
130     XtSetArg(args[n], XtNbackground, pixel); n++;
131     w = XtCreateManagedWidget("", commandWidgetClass, parent, args, n);
132     return(w);
133 }
134 
135 
136 /*****************************************************************************
137   FUNCTION : ui_createColWidgets
138 
139   PURPOSE  : creates the widgets for all colors
140   RETURNS  : void
141   NOTES    :
142 
143   UPDATE   :
144 ******************************************************************************/
145 
ui_createColWidgets(Widget parent)146 static void ui_createColWidgets (Widget parent)
147 
148 {
149     Widget top, left;
150     unsigned long pixel;
151     int i, ix, iy;
152 
153     for (i=0; i<UI_MAX_EDIT_COLS; i++) {
154         ix = i MOD UI_VERT_COLS;
155         iy = i DIV UI_VERT_COLS;
156         if (ix-1 < 0)
157            left = NULL;
158         else
159            left = ui_colWidget[iy * UI_VERT_COLS + ix - 1];
160 
161         if (iy-1 < 0)
162            top = NULL;
163         else
164            top = ui_colWidget[iy * UI_VERT_COLS - 1];
165         pixel = ui_editColor[i];
166         ui_colWidget[i] = ui_xCreateColButtonItem (parent, pixel,
167                          colWidgetXsize, colWidgetYsize, left, top);
168     }
169 }
170 
171 
172 /*****************************************************************************
173   FUNCTION : ui_createTestPannel
174 
175   PURPOSE  : creates the test pannel for the selected colors
176   RETURNS  :
177   NOTES    : the sizes are absolute and depend on the
178              vars in ui_redrawColorWindow
179 
180   UPDATE   :
181 ******************************************************************************/
182 
ui_createTestPannel(Widget parent,Widget left,Widget top)183 static Widget ui_createTestPannel (Widget parent, Widget left, Widget top)
184 
185 {
186     Cardinal n;
187     Widget   w;
188     Arg	     args[15];
189 
190     n = 0;
191     XtSetArg(args[n], XtNheight, 72); n++; /* 58 */
192     XtSetArg(args[n], XtNwidth, 234); n++;
193     XtSetArg(args[n], XtNfromVert , top);  n++;
194     XtSetArg(args[n], XtNfromHoriz, left);  n++;
195     XtSetArg(args[n], XtNleft  , XtChainLeft); n++;
196     XtSetArg(args[n], XtNright , XtChainLeft); n++;
197     XtSetArg(args[n], XtNtop   , XtChainTop); n++;
198     XtSetArg(args[n], XtNbottom, XtChainTop); n++;
199     w = XtCreateManagedWidget("testPanel", boxWidgetClass,
200                                         parent, args, n);
201     return(w);
202 
203 }
204 
205 
206 /*****************************************************************************
207   FUNCTION : ui_redrawColorWindow
208 
209   PURPOSE  : draws the test pannel
210   RETURNS  :
211   NOTES    : in the first line is the colorscale from red to green
212              in the second are units in the selection color
213              the background and text color are also shown
214 
215   UPDATE   :
216 ******************************************************************************/
217 
ui_redrawColorWindow(void)218 static void ui_redrawColorWindow (void)
219 
220 {
221     static int xsize = 16;
222     static int ysize = 16;
223     static int xspace = 32;
224     static int yspace = 32;
225     static int xoffset = 14;
226     static int yoffset = 14;
227 
228     char buf[10];
229     int i;
230 
231     XSetWindowBackground(ui_display, colWindow,
232                ui_editColor[ui_currentBackCol]);
233     XClearWindow (ui_display, colWindow);
234     for (i=0; i<7; i++) {
235         XSetForeground(ui_display, colGC,
236                        ui_col_rangePixels[i*5]);
237         XFillRectangle(ui_display, colWindow, colGC,
238                        xoffset + i*xspace, yoffset, (unsigned int) xsize,                              (unsigned int) ysize);
239         XSetForeground(ui_display, colGC, ui_editColor[ui_currentTextCol]);
240         sprintf (buf, "u%d", i);
241         XDrawString(ui_display, colWindow, colGC,
242 		       xoffset + i*xspace, yoffset-2, buf, (int) strlen(buf));
243     }
244     for (i=0; i<7; i++) {
245         XSetForeground(ui_display, colGC, ui_editColor[ui_currentSelCol]);
246         XFillRectangle(ui_display, colWindow, colGC,
247                        xoffset + i*xspace, yoffset+yspace, (unsigned int) xsize,                       (unsigned int) ysize);
248         XSetForeground(ui_display, colGC, ui_editColor[ui_currentTextCol]);
249         sprintf (buf, "u%d", i+7);
250         XDrawString(ui_display, colWindow, colGC,
251 		       xoffset + i*xspace, yoffset+yspace-2, buf, (int) strlen(buf));
252     }
253 
254 }
255 
256 
257 /*****************************************************************************
258   FUNCTION : ui_colorUpdateProc
259 
260   PURPOSE  : selects which color to edit
261   RETURNS  : void
262   NOTES    :
263 
264   UPDATE   :
265 ******************************************************************************/
266 
ui_colorUpdateProc(Widget w,int color,caddr_t call_data)267 static void ui_colorUpdateProc (Widget w, int color, caddr_t call_data)
268 
269 {
270     switch (currentSelection) {
271         case UI_SELECT_BACK_COLOR: ui_currentBackCol = color;
272                                    break;
273         case UI_SELECT_SEL_COLOR : ui_currentSelCol = color;
274                                    break;
275         case UI_SELECT_TEXT_COLOR: ui_currentTextCol = color;
276                                    break;
277     }
278     ui_redrawColorWindow();
279 }
280 
281 
282 /*****************************************************************************
283   FUNCTION : ui_timerProc
284 
285   PURPOSE  : redraws the test window
286   RETURNS  : void
287   NOTES    : callback from a timer. called 1 sec after the edit pannel is
288              popped up. this is nessecary, because a redraw function can't
289              called directly
290 
291   UPDATE   :
292 ******************************************************************************/
293 
ui_timerProc(caddr_t client_data,XtIntervalId * timerId)294 static void ui_timerProc (caddr_t client_data, XtIntervalId *timerId)
295 
296 {
297     ui_redrawColorWindow();
298 }
299 
300 
301 /*****************************************************************************
302   FUNCTION : ui_colorWindowEventProc
303 
304   PURPOSE  : redraws the test window
305   RETURNS  : void
306   NOTES    : called by the event loop if redraw is nessecary
307 
308   UPDATE   :
309 ******************************************************************************/
310 
ui_colorWindowEventProc(Widget w,Display * display,XEvent * event)311 static void ui_colorWindowEventProc (Widget w, Display *display, XEvent *event)
312 
313 {
314     if ((event->type == Expose) AND (event->xexpose.count == 0))
315         ui_redrawColorWindow();
316 }
317 
318 
319 
320 
321 /*****************************************************************************
322   FUNCTION : ui_selectColorItemProc
323 
324   PURPOSE  : toggles the specified button
325   RETURNS  : void
326   NOTES    :
327 ******************************************************************************/
328 
ui_selectColorItemProc(Widget w,int value,caddr_t call_data)329 static void ui_selectColorItemProc (Widget w, int value, caddr_t call_data)
330 
331 {
332     ui_setToggles(value, &currentSelection, noOfColToggles, colorToggle);
333 }
334 
335 
336 /*****************************************************************************
337   FUNCTION : ui_createColorEditPannel
338 
339   PURPOSE  : creates the color edit pannel
340   RETURNS  : void
341   NOTES    :
342 
343   UPDATE   :
344 ******************************************************************************/
345 
ui_createColorEditPannel(Widget w,Widget button,caddr_t call_data)346 void ui_createColorEditPannel (Widget w, Widget button, caddr_t call_data)
347 
348 {
349     int n, i;
350     Arg arg[10];
351     Position xPos, yPos;
352     Dimension width, height;
353     Widget colEditPannel, pannel, border, done;
354     Widget cancel, colframe, testPannel;
355 
356     n = 0;
357     XtSetArg (arg[0], XtNwidth, &width); n++;
358     XtSetArg (arg[1], XtNheight, &height); n++;
359     XtGetValues (button, arg, (unsigned int) n);
360 
361     XtTranslateCoords (button, (Position) (width / 2), (Position) (height / 2), &xPos, &yPos);
362 
363     n = 0;
364     XtSetArg(arg[n], XtNx, xPos); n++;
365     XtSetArg(arg[n], XtNy, yPos); n++;
366 
367     colEditPannel = XtCreatePopupShell ("color edit", transientShellWidgetClass, button, arg, (unsigned int) n);
368 
369     border = XtCreateManagedWidget("border", boxWidgetClass,
370                                    colEditPannel, NULL, ZERO);
371     pannel = XtCreateManagedWidget("pannel", formWidgetClass,
372                                    border, NULL, ZERO);
373     colframe = XtCreateManagedWidget("colframe", formWidgetClass,
374                                    pannel, NULL, ZERO);
375 
376     ui_createColWidgets (colframe);
377 
378     done = ui_xCreateButtonItem ("done", border, NULL, NULL);
379     cancel = ui_xCreateButtonItem ("cancel", border, NULL, done);
380 
381     colorToggle[0] = ui_xCreateToggleItem ("text", pannel, NULL,
382                      NULL, colframe);
383     colorToggle[1] = ui_xCreateToggleItem ("background", pannel, NULL,
384                      NULL, colorToggle[0]);
385 
386     colorToggle[2] = ui_xCreateToggleItem ("selection", pannel, NULL,
387                      NULL, colorToggle[1]);
388 
389     ui_getDisplayColors();
390 
391     testPannel = ui_createTestPannel(pannel, colorToggle[0], colframe);
392 
393     XtAddCallback (done, XtNcallback, (XtCallbackProc) ui_closeColorEditPannel,
394                    (caddr_t) colEditPannel);
395     XtAddCallback (cancel, XtNcallback, (XtCallbackProc) ui_cancelColorEditPannel, (caddr_t) colEditPannel);
396 
397     XtAddCallback (colorToggle[0], XtNcallback, (XtCallbackProc) ui_selectColorItemProc, (caddr_t) UI_SELECT_TEXT_COLOR);
398     XtAddCallback (colorToggle[1], XtNcallback, (XtCallbackProc) ui_selectColorItemProc, (caddr_t) UI_SELECT_BACK_COLOR);
399     XtAddCallback (colorToggle[2], XtNcallback, (XtCallbackProc) ui_selectColorItemProc, (caddr_t) UI_SELECT_SEL_COLOR);
400 
401     for (i=0; i<UI_MAX_EDIT_COLS; i++)
402         XtAddCallback (ui_colWidget[i], XtNcallback,
403                        (XtCallbackProc) ui_colorUpdateProc,
404 		       (XtPointer) ((long)i));
405 
406     XtAddEventHandler (testPannel, ExposureMask,
407                        FALSE, (XtEventHandler) ui_colorWindowEventProc, ui_display);
408 
409     ui_checkWindowPosition(colEditPannel);
410     XtPopup (colEditPannel, XtGrabNone);
411     ui_xDontResizeWidget(colEditPannel);
412 
413     ui_setToggles(currentSelection, &currentSelection,
414                   noOfColToggles, colorToggle);
415 
416     colWindow = XtWindow(testPannel);
417 
418     colGC = XCreateGC (ui_display, colWindow, 0, 0);
419     XSetFont (ui_display, colGC, ui_fontStruct->fid);
420 
421     XtAppAddTimeOut (ui_appContext, 1000L, (XtTimerCallbackProc) ui_timerProc, NULL);
422 
423 }
424