dtstyle: Resolve 28 compiler warnings.
[oweals/cde.git] / cde / programs / dtstyle / ColorEdit.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: ColorEdit.c /main/5 1995/10/30 13:08:03 rswiston $ */
24 /************************************<+>*************************************
25  ***************************************************************************
26  **
27  **   File:        ColorEdit.c
28  **
29  **   Project:     DT 3.0
30  **
31  **   Description: Controls the Dtstyle Color Editor dialog
32  **
33  **
34  **  (c) Copyright Hewlett-Packard Company, 1990.  
35  **
36  **
37  **
38  ****************************************************************************
39  ************************************<+>*************************************/
40 /* +++++++++++++++++++++++++++++++++++++++*/
41 /*  include files                         */
42 /* +++++++++++++++++++++++++++++++++++++++*/
43 #include <X11/cursorfont.h>
44 #include <X11/keysym.h>
45
46 #include <math.h>
47
48 #include <X11/Xlib.h>
49 #include <Xm/MwmUtil.h>
50
51 #include <Xm/XmP.h>
52 #include <Xm/LabelG.h>
53 #include <Xm/PushB.h>
54 #include <Xm/PushBG.h>
55 #include <Xm/Form.h>
56 #include <Xm/Scale.h>
57 #include <Xm/VendorSEP.h>
58
59 #include <Dt/DialogBox.h>
60 #include <Dt/TitleBox.h>
61
62 #include <Dt/UserMsg.h>
63
64 #include "Help.h"
65 #include "Main.h"
66 #include "ColorMain.h"
67 #include "ColorFile.h"
68
69 /*+++++++++++++++++++++++++++++++++++++++*/
70 /* include extern functions              */
71 /*+++++++++++++++++++++++++++++++++++++++*/
72 #include "ColorEdit.h"
73
74 /*+++++++++++++++++++++++++++++++++++++++*/
75 /* Local #defines                        */
76 /*+++++++++++++++++++++++++++++++++++++++*/
77 #define  NONE       0
78 #define  RED        1
79 #define  GREEN      2
80 #define  BLUE       3
81 #define  HUE        4
82 #define  SATURATION 5
83 #define  VALUE      6
84
85 #define MAX_STR_LEN   128
86
87 #define SCALE_LEFT_POSITION        10
88 #define BUTTON_MARGIN              10
89 #define OLD_BUTTON_LEFT_POSITION   10
90 #define SCALE_WIDTH                300
91
92 #define SATURATION_BITS                 "Dtsatur"
93 #define VALUE_BITS                      "Dtvalue"
94
95 /*+++++++++++++++++++++++++++++++++++++++*/
96 /* Internal Functions                    */
97 /*+++++++++++++++++++++++++++++++++++++++*/
98
99 static void CreateColorEditor( Widget parent) ;
100 static void sliderLayoutCB( 
101                         Widget w,
102                         XtPointer client_data,
103                         XtPointer call_data) ;
104 static void InitializeNewButton( void ) ;
105 static void InitializeOldButton( void ) ;
106 static void GenerateColors( void ) ;
107 static Pixel GetPixel( Widget widget,
108                        char *color_string ) ;
109 static void changRGB_CB( 
110                         Widget w,
111                         XtPointer client_data,
112                         XtPointer call_data) ;
113 static void changHSV_CB( 
114                         Widget w,
115                         XtPointer client_data,
116                         XtPointer call_data) ;
117 static void SetScales( XColor *rgb) ;
118 static void SetRGBHSVScales( 
119                         XColor *rgb,
120                         int h,
121                         int s,
122                         int v) ;
123 static void CopyPixelSet( 
124                         ColorSet *color_set_dest,
125                         ColorSet *color_set_src) ;
126 static void HSVtoRGB( 
127                         int h,
128                         int s,
129                         int v,
130                         unsigned short *r,
131                         unsigned short *g,
132                         unsigned short *b) ;
133 static double max( 
134                         double x,
135                         double y,
136                         double z) ;
137 static double min( 
138                         double x,
139                         double y,
140                         double z) ;
141 static void grabcolorCB( 
142                         Widget w,
143                         XtPointer client_data,
144                         XtPointer call_data) ;
145 static void dialogBoxCB( 
146                         Widget w,
147                         XtPointer client_data,
148                         XtPointer call_data) ;
149 static void _DtmapCB( 
150                         Widget w,
151                         XtPointer client_data,
152                         XtPointer call_data) ;
153
154
155 /*+++++++++++++++++++++++++++++++++++++++*/
156 /* Global Variables                      */
157 /*+++++++++++++++++++++++++++++++++++++++*/
158 EditData edit;
159
160 /*+++++++++++++++++++++++++++++++++++++++*/
161 /* Internal Variables                    */
162 /*+++++++++++++++++++++++++++++++++++++++*/
163 static saveRestore save = {FALSE, 0, };
164 static OldNewSame = False;
165
166 /************************************************************************
167  *  ColorEditor() - Create ColorEditor dialog first time up.
168  *  If has already been created, set up oldButton and newButton
169  *  colors, set the scale values for the new color and manage 
170  *  the dialog.
171  ************************************************************************/
172 void 
173 ColorEditor(
174         Widget parent,
175         ColorSet *color_set )
176 {
177     if (edit.DialogShell == NULL)
178     {
179         _DtTurnOnHourGlass(parent);  
180         edit.color_set = color_set;
181         CreateColorEditor(parent);   
182         _DtTurnOffHourGlass(parent);  
183     }
184     else 
185     {
186         if (!XtIsManaged(edit.DialogShell))
187         {
188             edit.color_set = color_set;
189             CopyPixelSet(&edit.oldButtonColor,edit.color_set);
190             InitializeNewButton();
191             SetScales(&edit.color_set->bg);
192             XtManageChild(edit.DialogShell);
193             XMapRaised(style.display, XtWindow(edit.DialogShell));
194         }
195     }
196 }
197
198
199 /************************************************************************
200  *   CreateColorEditor()
201  *           Create the Color Editor Dialog
202  ************************************************************************/
203 static void 
204 CreateColorEditor(
205         Widget parent )
206 {
207         register int     n,i;
208         Arg              args[MAX_ARGS];
209         Widget           sampleTB;
210         Widget           sampleForm;
211         Widget           sliderTB;
212         XmString         string;               /* temp Xm string */
213         Widget           widgetlist1[10];   /* main_form */
214         Widget           widgetlist2[16];   /* sliderForm */
215         Widget           widgetlist3[10];   /* sampleForm */
216         int              widget_count1 = 0;
217         int              widget_count2 = 0;
218         int              widget_count3 = 0;
219         XmString         button_string[NUM_LABELS]; 
220         Pixel            foreground, background;
221         int              height;
222         WidgetList       children;
223         Dimension        w, width_old, width_new;
224
225         edit.current_scale = NONE;
226
227         /* Set up DialogBox button labels. */
228         button_string[0] = CMPSTR(_DtOkString);
229         button_string[1] = CMPSTR(_DtCancelString);
230         button_string[2] = CMPSTR(_DtHelpString);
231
232         /* Note that save.poscnt has been initialized elsewhere.  
233          * save.posArgs may contain information from restoreColorEdit(). */
234
235         XtSetArg(args[save.poscnt], XmNchildType, XmWORK_AREA);  save.poscnt++;
236         XtSetArg(save.posArgs[save.poscnt], XmNbuttonCount, NUM_LABELS);  
237         save.poscnt++;
238         XtSetArg(save.posArgs[save.poscnt], XmNbuttonLabelStrings, 
239                   button_string);  
240         save.poscnt++;
241         XtSetArg(save.posArgs[save.poscnt], XmNdefaultPosition, False);
242         save.poscnt++;
243         edit.DialogShell = __DtCreateDialogBoxDialog(parent,"colorEditDlg", 
244                                                      save.posArgs, save.poscnt);
245         XtAddCallback(edit.DialogShell, XmNmapCallback, sliderLayoutCB, NULL);
246         XtAddCallback(edit.DialogShell, XmNcallback, dialogBoxCB, NULL);
247         XtAddCallback(edit.DialogShell, XmNmapCallback, _DtmapCB, parent);
248         XtAddCallback(edit.DialogShell, XmNhelpCallback,
249             (XtCallbackProc)HelpRequestCB, (XtPointer)HELP_MODIFY_PALETTE_DIALOG);
250
251         XmStringFree(button_string[0]);
252         XmStringFree(button_string[1]);
253         XmStringFree(button_string[2]);
254
255         widgetlist1[0] = _DtDialogBoxGetButton(edit.DialogShell,2);
256
257         n=0;
258         XtSetArg(args[n], XmNautoUnmanage, False); n++;
259         XtSetArg(args[n], XmNcancelButton, widgetlist1[0]); n++;
260         XtSetValues (edit.DialogShell, args, n);
261
262         n=0;
263         XtSetArg (args[n], XmNtitle, ((char *)GETMESSAGE(17, 16, "Style Manager - Modify Color"))); n++;
264         XtSetArg (args[n], XmNuseAsyncGeometry, True); n++;
265         XtSetArg(args[n], XmNmwmFunctions, DIALOG_MWM_FUNC); n++;
266         XtSetValues (XtParent(edit.DialogShell), args, n);
267   
268         n=0;
269         XtSetArg(args[n], XmNhorizontalSpacing, style.horizontalSpacing); n++;
270         XtSetArg(args[n], XmNverticalSpacing, style.verticalSpacing); n++;
271         edit.main_form = XmCreateForm(edit.DialogShell, "main_form", args, n);
272
273         n=0;
274         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
275         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
276         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
277         XtSetArg (args[n], XmNmarginWidth, 0);  n++;
278         XtSetArg (args[n], XmNmarginHeight, 0);  n++;
279         string = CMPSTR(((char *)GETMESSAGE(17, 4, "Color Sample")));
280         XtSetArg (args[n], XmNtitleString, string); n++;
281         sampleTB = _DtCreateTitleBox(edit.main_form, "sampleTB", args, n);
282         widgetlist1[widget_count1++] = sampleTB;
283         XmStringFree(string);
284         
285         n=0;
286         XtSetArg(args[n], XmNhorizontalSpacing, style.horizontalSpacing); n++;
287         XtSetArg(args[n], XmNverticalSpacing, style.verticalSpacing); n++;
288         sampleForm = XmCreateForm(sampleTB, "sampleForm", args, n);
289         
290         /*   Create Old and New Buttons */
291         InitializeOldButton();
292
293         if(!OldNewSame) {
294            n=0;
295            string =  CMPSTR(((char *)GETMESSAGE(17, 5, "Old")));
296            XtSetArg(args[n], XmNlabelString, string); n++;
297            XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
298            XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
299            XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM);  n++;
300            /* Set the old button color to the color of the selected button */
301            XtSetArg(args[n], XmNforeground, edit.oldButtonColor.fg.pixel); n++;
302            XtSetArg(args[n], XmNbackground, edit.oldButtonColor.bg.pixel); n++;
303            XtSetArg(args[n], XmNarmColor, edit.oldButtonColor.sc.pixel); n++;
304            XtSetArg(args[n], XmNtopShadowColor,
305                                     edit.oldButtonColor.ts.pixel); n++;
306            XtSetArg(args[n], XmNbottomShadowColor, 
307                                     edit.oldButtonColor.bs.pixel); n++;
308
309            edit.oldButton =
310                           XmCreatePushButton(sampleForm, "oldButton", args, n);
311            widgetlist3[widget_count3++] = edit.oldButton;
312            XmStringFree(string);
313         }
314
315         string =  CMPSTR(((char *)GETMESSAGE(17, 6, "New")));
316         n=0;
317         XtSetArg(args[n], XmNlabelString, string); n++;
318         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
319         if(!OldNewSame) {
320            XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET);  n++;
321            XtSetArg (args[n], XmNleftWidget, edit.oldButton);  n++;
322         }
323         else {
324            XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
325         }
326         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM);  n++;
327         edit.newButton = XmCreatePushButton(sampleForm, "newButton", args, n);
328         widgetlist3[widget_count3++] = edit.newButton;
329         XmStringFree(string);
330
331         
332         if(!OldNewSame) {
333            /* make old and new button both the size of the larger of the two */
334            width_old = XtWidth(edit.oldButton);
335            width_new = XtWidth(edit.newButton);
336            w = (width_old>width_new) ? width_old : width_new;
337
338            n=0;
339            XtSetArg (args[n], XmNrecomputeSize, False);  n++;
340            XtSetArg (args[n], XmNwidth, w+2*BUTTON_MARGIN);  n++;
341            XtSetArg (args[n], XmNheight, w+2*BUTTON_MARGIN);  n++;
342            XtSetValues(edit.oldButton, args, n);
343            XtSetValues(edit.newButton, args, n);
344         }
345         else
346         {
347            w = XtWidth(edit.newButton);
348
349            n=0;
350            XtSetArg (args[n], XmNrecomputeSize, False);  n++;
351            XtSetArg (args[n], XmNwidth, w+2*BUTTON_MARGIN);  n++;
352            XtSetArg (args[n], XmNheight, w+2*BUTTON_MARGIN);  n++;
353            XtSetValues(edit.newButton, args, n);
354
355         }
356
357         InitializeNewButton();
358
359         n=0;
360         string =  CMPSTR(((char *)GETMESSAGE(17, 7, "Grab Color")));
361         XtSetArg(args[n], XmNlabelString, string); n++;
362         XtSetArg(args[n], XmNnavigationType, XmTAB_GROUP);  n++;
363         XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
364         XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
365         XtSetArg(args[n], XmNmarginHeight, LB_MARGIN_HEIGHT);  n++;
366         XtSetArg(args[n], XmNmarginWidth, LB_MARGIN_WIDTH);  n++;
367         edit.grabColor = XmCreatePushButtonGadget(sampleForm, "grabColor", args, n);
368         widgetlist3[widget_count3++] = edit.grabColor;
369         XtAddCallback(edit.grabColor, XmNactivateCallback, 
370                       grabcolorCB, (XtPointer)NULL);    
371         XmStringFree(string);
372
373         /* Create frame for RGB/HSV scales */
374         n = 0;
375         XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
376         XtSetArg(args[n], XmNtopWidget, sampleTB); n++;
377         XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
378         XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
379         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
380         XtSetArg (args[n], XmNmarginWidth, 0);  n++;
381         XtSetArg (args[n], XmNmarginHeight, 0);  n++;
382         string =  CMPSTR(((char *)GETMESSAGE(17, 8, "Color Editor")));
383         XtSetArg (args[n], XmNtitleString, string); n++;
384         sliderTB = _DtCreateTitleBox(edit.main_form, "sliderTB", args, n);
385         widgetlist1[widget_count1++] = sliderTB;
386         XmStringFree(string);
387
388         n = 0;
389         XtSetArg(args[n], XmNhorizontalSpacing,  style.horizontalSpacing); n++;
390         XtSetArg(args[n], XmNverticalSpacing,  style.verticalSpacing); n++;
391         edit.sliderForm = XmCreateForm(sliderTB, "sliderForm", args, n);
392
393         /*
394          * Create Hue label and scale
395          */
396
397         n = 0;
398         string = CMPSTR(((char *)GETMESSAGE(17, 9, "Hue")));
399         XtSetArg(args[n], XmNlabelString, string); n++;
400         edit.hueLabel = XmCreateLabelGadget(edit.sliderForm, "hueLabel", args, n);
401         widgetlist2[widget_count2++] = edit.hueLabel;
402         XmStringFree(string);
403
404         n = 0;
405         XtSetArg(args[n], XmNshowValue, TRUE);  n++;
406         XtSetArg(args[n], XmNorientation, XmVERTICAL);  n++;
407         XtSetArg(args[n], XmNprocessingDirection, XmMAX_ON_BOTTOM);  n++;
408         XtSetArg(args[n], XmNhighlightThickness, SCALE_HIGHLIGHT_THICKNESS);  n++; 
409         XtSetArg(args[n], XmNmaximum, 359);  n++;
410         XtSetArg(args[n], XmNminimum, 0x0);  n++;
411         XtSetArg(args[n], XmNincrement, 1); n++;
412         edit.hueScale = XmCreateScale(edit.sliderForm, "hueScale", args, n);
413         widgetlist2[widget_count2++] = edit.hueScale;
414         XtAddCallback(edit.hueScale, XmNvalueChangedCallback, changHSV_CB, (XtPointer) HUE);
415         XtAddCallback(edit.hueScale, XmNdragCallback, changHSV_CB, (XtPointer) HUE );
416
417         /*
418          * Create RGB scales
419          */
420
421         string =  CMPSTR(((char *)GETMESSAGE(17, 10, "R")));
422         n = 0;
423         XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
424         XtSetArg(args[n], XmNlabelString, string); n++;
425         XtSetArg(args[n], XmNmarginHeight, 0); n++;
426         edit.redLabel = XmCreateLabelGadget(edit.sliderForm, "redLabel", args, n);
427         widgetlist2[widget_count2++] = edit.redLabel;
428         XmStringFree(string);
429
430         string =  CMPSTR(((char *)GETMESSAGE(17, 11, "G")));
431         n = 0;
432         XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
433         XtSetArg(args[n], XmNlabelString, string); n++;
434         XtSetArg(args[n], XmNmarginHeight, 0); n++;
435         edit.greenLabel = XmCreateLabelGadget(edit.sliderForm, "greenLabel", args, n);
436         widgetlist2[widget_count2++] = edit.greenLabel;
437         XmStringFree(string);
438
439         string =  CMPSTR(((char *)GETMESSAGE(17, 12, "B")));
440         n = 0;
441         XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
442         XtSetArg(args[n], XmNlabelString, string); n++;
443         XtSetArg(args[n], XmNmarginHeight, 0); n++;
444         edit.blueLabel = XmCreateLabelGadget(edit.sliderForm, "blueLabel", args, n);
445         widgetlist2[widget_count2++] = edit.blueLabel;
446         XmStringFree(string);
447
448         n = 0;
449         XtSetArg(args[n], XmNshowValue, TRUE);  n++;
450         XtSetArg(args[n], XmNorientation, XmHORIZONTAL);  n++;
451         XtSetArg(args[n], XmNprocessingDirection, XmMAX_ON_RIGHT);  n++;
452         XtSetArg(args[n], XmNhighlightThickness, SCALE_HIGHLIGHT_THICKNESS);  n++; 
453         XtSetArg(args[n], XmNmaximum, 0xff);  n++;
454         XtSetArg(args[n], XmNminimum, 0x0);  n++;
455         XtSetArg(args[n], XmNincrement, 1); n++;
456         XtSetArg(args[n], XmNscaleWidth, SCALE_WIDTH); n++; 
457         widgetlist2[widget_count2++] = edit.redScale = 
458                 XmCreateScale(edit.sliderForm, "redScale", args, n);
459         XtAddCallback(edit.redScale, XmNvalueChangedCallback, changRGB_CB, (XtPointer) RED);
460         XtAddCallback(edit.redScale, XmNdragCallback, changRGB_CB, (XtPointer) RED );
461
462         widgetlist2[widget_count2++] = edit.greenScale = 
463                 XmCreateScale(edit.sliderForm, "greenScale", args, n);
464         XtAddCallback(edit.greenScale, XmNvalueChangedCallback, changRGB_CB, (XtPointer) GREEN );
465         XtAddCallback(edit.greenScale, XmNdragCallback, changRGB_CB, (XtPointer) GREEN );
466
467         widgetlist2[widget_count2++] = edit.blueScale = 
468                 XmCreateScale(edit.sliderForm, "blueScale", args, n);
469         XtAddCallback(edit.blueScale, XmNvalueChangedCallback, changRGB_CB, (XtPointer) BLUE );
470         XtAddCallback(edit.blueScale, XmNdragCallback, changRGB_CB, (XtPointer) BLUE);
471
472         /*
473          * Create SV scales
474          */
475
476         /* create Saturation pixmap */
477         n=0;
478         XtSetArg(args[n], XmNforeground, &foreground); n++;
479         XtSetArg(args[n], XmNbackground, &background); n++;
480         XtGetValues(edit.sliderForm, args, n);
481
482         n = 0;
483         XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
484         XtSetArg(args[n], XmNlabelType, XmPIXMAP); n++;
485         XtSetArg(args[n], XmNlabelPixmap, XmGetPixmap(style.screen, SATURATION_BITS,
486                                      foreground, background)); n++;
487         XtSetArg(args[n], XmNmarginHeight, 3); n++;
488         edit.satLabel = XmCreateLabelGadget(edit.sliderForm, "satLabel", args, n);
489         widgetlist2[widget_count2++] = edit.satLabel;
490
491         /* create Value pixmap */
492         n = 0;
493         XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
494         XtSetArg(args[n], XmNlabelType, XmPIXMAP); n++;
495         XtSetArg(args[n], XmNlabelPixmap, XmGetPixmap(style.screen, VALUE_BITS,
496                                         foreground, background)); n++;
497         XtSetArg(args[n], XmNmarginHeight, 3); n++;
498         edit.valLabel = XmCreateLabelGadget(edit.sliderForm, "valLabel", args, n);
499         widgetlist2[widget_count2++] = edit.valLabel;
500
501         n = 0;
502         XtSetArg(args[n], XmNshowValue, TRUE);  n++;
503         XtSetArg(args[n], XmNorientation, XmHORIZONTAL);  n++;
504         XtSetArg(args[n], XmNprocessingDirection, XmMAX_ON_RIGHT);  n++;
505         XtSetArg(args[n], XmNhighlightThickness, SCALE_HIGHLIGHT_THICKNESS);  n++; 
506         XtSetArg(args[n], XmNmaximum, 0xff);  n++;
507         XtSetArg(args[n], XmNminimum, 0x0);  n++;
508         XtSetArg(args[n], XmNincrement, 1); n++;
509         XtSetArg(args[n], XmNscaleWidth, SCALE_WIDTH); n++; 
510         widgetlist2[widget_count2++] = edit.satScale = 
511                 XmCreateScale(edit.sliderForm, "satScale", args, n);
512         XtAddCallback(edit.satScale, XmNvalueChangedCallback, changHSV_CB, (XtPointer) SATURATION);
513         XtAddCallback(edit.satScale, XmNdragCallback, changHSV_CB, (XtPointer) SATURATION );
514
515         widgetlist2[widget_count2++] = edit.valScale = 
516                 XmCreateScale(edit.sliderForm, "valScale", args, n);
517         XtAddCallback(edit.valScale, XmNvalueChangedCallback, changHSV_CB, (XtPointer) VALUE);
518         XtAddCallback(edit.valScale, XmNdragCallback, changHSV_CB, (XtPointer) VALUE );
519
520         /* Set the scales to the bg component of the selected color */
521         SetScales(&edit.color_set->bg);
522
523         /* Do all the mapping */
524
525         XtManageChildren(widgetlist3, widget_count3);
526         XtManageChild(sampleForm);
527
528         XtManageChildren(widgetlist2, widget_count2);
529         XtManageChild(edit.sliderForm);
530
531         XtManageChildren(widgetlist1, widget_count1);
532         XtManageChild(edit.main_form);
533
534         XtManageChild(edit.DialogShell);  
535
536         if(TypeOfMonitor == XmCO_HIGH_COLOR)
537         {
538            /* set the trough colors of the RGB scales -
539               get the composite children of the scales - 
540                           child[1] is the scrollbar */
541            n=0;
542            XtSetArg(args[n], XtNchildren, &children); n++;
543            XtGetValues(edit.redScale, args, n);
544            n=0;
545            XtSetArg(args[n], XmNtroughColor, GetPixel(edit.main_form,"red"));  n++;
546            XtSetValues (children[1], args, n);
547
548            n=0;
549            XtSetArg(args[n], XtNchildren, &children); n++;
550            XtGetValues(edit.greenScale, args, n);
551            n=0;
552            XtSetArg(args[n], XmNtroughColor, GetPixel(edit.main_form,"green"));  n++; 
553            XtSetValues (children[1], args, n);
554
555            n=0;
556            XtSetArg(args[n], XtNchildren, &children); n++;
557            XtGetValues(edit.blueScale, args, n);
558            n=0;
559            XtSetArg(args[n], XmNtroughColor, GetPixel(edit.main_form,"blue"));  n++; 
560            XtSetValues (children[1], args, n);
561         }
562
563 }
564
565
566 static void 
567 sliderLayoutCB(
568         Widget w,
569         XtPointer client_data,
570         XtPointer call_data )
571 {
572         register int     n;
573         Arg              args[12];
574         Dimension        redLabelWidth;
575         Dimension        greenLabelWidth;
576         Dimension        blueLabelWidth;
577         Dimension        satLabelWidth;
578         Dimension        valLabelWidth;
579         Dimension        MaxLabelWidth;
580         Dimension        redLabelHeight;
581         Dimension        satLabelHeight;
582         Dimension        valLabelHeight;
583         Dimension        redScaleHeight;
584         Dimension        TopOffset;
585         Dimension        scale_width;
586         Dimension        pieces;
587
588
589         /* 
590          * Do the scale and label attachments
591          */
592
593         redLabelHeight = XtHeight(edit.redLabel);
594         MaxLabelWidth = redLabelWidth = XtWidth(edit.redLabel);
595
596         greenLabelWidth = XtWidth(edit.greenLabel);
597         if (greenLabelWidth > MaxLabelWidth) 
598         {
599             MaxLabelWidth = greenLabelWidth;
600         }
601         
602         blueLabelWidth = XtWidth(edit.blueLabel);
603         if (blueLabelWidth > MaxLabelWidth) 
604         {
605             MaxLabelWidth = blueLabelWidth;
606         }
607         
608         satLabelHeight = XtHeight(edit.satLabel);
609         satLabelWidth = XtWidth(edit.satLabel);
610         if (satLabelWidth > MaxLabelWidth) 
611         {
612             MaxLabelWidth = satLabelWidth;
613         }
614
615         valLabelHeight = XtHeight(edit.valLabel);
616         valLabelWidth = XtWidth(edit.valLabel);
617         if (valLabelWidth > MaxLabelWidth) 
618         {
619             MaxLabelWidth = valLabelWidth;
620         }
621         
622         redScaleHeight = XtHeight(edit.redScale);
623
624
625         /* Attach grab button to New button if they are overlapping */
626
627         if(!OldNewSame)  
628             pieces = XtWidth(edit.oldButton) +
629                      XtWidth(edit.newButton) +
630                      XtWidth(edit.grabColor) +
631                      4*style.horizontalSpacing;
632         else
633             pieces = XtWidth(edit.newButton) +
634                      XtWidth(edit.grabColor) +
635                      3*style.horizontalSpacing;
636         
637         if (XtWidth(edit.main_form) < pieces)
638         {
639             n=0;
640             XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET);  n++;
641             XtSetArg(args[n], XmNleftWidget, edit.newButton);  n++;
642             XtSetValues (edit.grabColor, args, n);
643
644             scale_width = XtWidth(edit.main_form) - MaxLabelWidth - 
645                           XtWidth(edit.hueScale) - 4*style.horizontalSpacing;
646
647             n=0;
648             XtSetArg(args[n], XmNscaleWidth, scale_width); n++; 
649             XtSetValues (edit.redScale, args, n);
650             XtSetValues (edit.greenScale, args, n);
651             XtSetValues (edit.blueScale, args, n);
652             XtSetValues (edit.satScale, args, n);
653             XtSetValues (edit.valScale, args, n);
654         }
655
656         /* Hue Label */
657         n=0;
658         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_FORM); n++;
659         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_NONE); n++;
660         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_NONE); n++;
661         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_FORM); n++;
662         XtSetValues (edit.hueLabel, args, n);
663
664         /* Hue Scale */
665         n=0;
666         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_WIDGET); n++;
667         XtSetArg(args[n], XmNtopWidget, edit.hueLabel); n++;
668         XtSetArg(args[n], XmNtopOffset, style.verticalSpacing); n++;
669         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
670         XtSetArg(args[n], XmNbottomOffset, 
671             2*(style.verticalSpacing + redScaleHeight) + 40); n++;
672         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_NONE); n++;
673         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_FORM); n++;
674         XtSetValues (edit.hueScale, args, n);
675
676         /* Red Label */
677         n=0;
678         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_WIDGET); n++;
679         XtSetArg(args[n], XmNtopWidget, edit.hueLabel); n++;
680         TopOffset = style.verticalSpacing + redScaleHeight - redLabelHeight;
681         XtSetArg(args[n], XmNtopOffset, TopOffset); n++;
682         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_NONE); n++;
683         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_FORM); n++;
684         XtSetArg(args[n], XmNleftOffset, 
685             style.horizontalSpacing + (MaxLabelWidth - redLabelWidth)); n++;
686         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_NONE); n++;
687         XtSetValues (edit.redLabel, args, n);
688
689         /* Green Label */
690         n=0;
691         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_WIDGET); n++;
692         XtSetArg(args[n], XmNtopWidget, edit.redLabel); n++;
693         XtSetArg(args[n], XmNtopOffset, TopOffset); n++;
694         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_NONE); n++;
695         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_FORM); n++;
696         XtSetArg(args[n], XmNleftOffset, 
697             style.horizontalSpacing + (MaxLabelWidth - greenLabelWidth)); n++;
698         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_NONE); n++;
699         XtSetValues (edit.greenLabel, args, n);
700
701         /* Blue Label */
702         n=0;
703         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_WIDGET); n++;
704         XtSetArg(args[n], XmNtopWidget, edit.greenLabel); n++;
705         XtSetArg(args[n], XmNtopOffset, TopOffset); n++;
706         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_NONE); n++;
707         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_FORM); n++;
708         XtSetArg(args[n], XmNleftOffset, 
709             style.horizontalSpacing + (MaxLabelWidth - blueLabelWidth)); n++;
710         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_NONE); n++;
711         XtSetValues (edit.blueLabel, args, n);
712
713         /* Saturation Label */
714         n=0;
715         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_WIDGET); n++;
716         XtSetArg(args[n], XmNtopWidget, edit.blueLabel); n++;
717         TopOffset = style.verticalSpacing + 40 + redScaleHeight - satLabelHeight;
718         XtSetArg(args[n], XmNtopOffset, TopOffset); n++;
719         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_NONE); n++;
720         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_FORM); n++;
721         XtSetArg(args[n], XmNleftOffset, 
722             style.horizontalSpacing + (MaxLabelWidth - satLabelWidth)); n++;
723         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_NONE); n++;
724         XtSetValues (edit.satLabel, args, n);
725
726         /* Value Label */
727         n=0;
728         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_WIDGET); n++;
729         XtSetArg(args[n], XmNtopWidget, edit.satLabel); n++;
730         TopOffset = style.verticalSpacing + redScaleHeight - valLabelHeight;
731         XtSetArg(args[n], XmNtopOffset, TopOffset); n++;
732         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
733         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_FORM); n++;
734         XtSetArg(args[n], XmNleftOffset, 
735             style.horizontalSpacing + (MaxLabelWidth - valLabelWidth)); n++;
736         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_NONE); n++;
737         XtSetValues (edit.valLabel, args, n);
738
739         /* Red Scale */
740         n=0;
741         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_NONE); n++;
742         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
743         XtSetArg(args[n], XmNbottomWidget, edit.redLabel); n++;
744         XtSetArg(args[n], XmNbottomOffset,  0); n++;
745         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_WIDGET); n++;
746         XtSetArg(args[n], XmNleftWidget, edit.redLabel); n++;
747         XtSetArg(args[n], XmNleftOffset,  style.horizontalSpacing); n++;
748         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_WIDGET); n++;
749         XtSetArg(args[n], XmNrightWidget, edit.hueScale); n++;
750         XtSetValues (edit.redScale, args, n);
751
752         /* Green Scale */
753         n=0;
754         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_NONE); n++;
755         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
756         XtSetArg(args[n], XmNbottomWidget, edit.greenLabel); n++;
757         XtSetArg(args[n], XmNbottomOffset,  0); n++;
758         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_WIDGET); n++;
759         XtSetArg(args[n], XmNleftWidget, edit.redLabel); n++;
760         XtSetArg(args[n], XmNleftOffset,  style.horizontalSpacing); n++;
761         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_WIDGET); n++;
762         XtSetArg(args[n], XmNrightWidget, edit.hueScale); n++;
763         XtSetValues (edit.greenScale, args, n);
764
765         /* Blue Scale */
766         n=0;
767         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_NONE); n++;
768         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
769         XtSetArg(args[n], XmNbottomWidget, edit.blueLabel); n++;
770         XtSetArg(args[n], XmNbottomOffset,  0); n++;
771         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_WIDGET); n++;
772         XtSetArg(args[n], XmNleftWidget, edit.redLabel); n++;
773         XtSetArg(args[n], XmNleftOffset,  style.horizontalSpacing); n++;
774         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_WIDGET); n++;
775         XtSetArg(args[n], XmNrightWidget, edit.hueScale); n++;
776         XtSetValues (edit.blueScale, args, n);
777
778         /* Saturation Scale */
779         n=0;
780         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_NONE); n++;
781         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
782         XtSetArg(args[n], XmNbottomWidget, edit.satLabel); n++;
783         XtSetArg(args[n], XmNbottomOffset,  0); n++;
784         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_WIDGET); n++;
785         XtSetArg(args[n], XmNleftWidget, edit.redLabel); n++;
786         XtSetArg(args[n], XmNleftOffset,  style.horizontalSpacing); n++;
787         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_WIDGET); n++;
788         XtSetArg(args[n], XmNrightWidget, edit.hueScale); n++;
789         XtSetValues (edit.satScale, args, n);
790
791         /* Value Scale */
792         n=0;
793         XtSetArg(args[n], XmNtopAttachment,    XmATTACH_NONE); n++;
794         XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
795         XtSetArg(args[n], XmNbottomWidget, edit.valLabel); n++;
796         XtSetArg(args[n], XmNbottomOffset,  0); n++;
797         XtSetArg(args[n], XmNleftAttachment,   XmATTACH_WIDGET); n++;
798         XtSetArg(args[n], XmNleftWidget, edit.redLabel); n++;
799         XtSetArg(args[n], XmNleftOffset,  style.horizontalSpacing); n++;
800         XtSetArg(args[n], XmNrightAttachment,  XmATTACH_WIDGET); n++;
801         XtSetArg(args[n], XmNrightWidget, edit.hueScale); n++;
802         XtSetValues (edit.valScale, args, n);
803
804         XtRemoveCallback(edit.DialogShell, XmNmapCallback, sliderLayoutCB, NULL);
805
806 }
807
808
809 /************************************************************************
810  *   InitializeNewButton()
811  *           set NewButton colors to point to the PixelSet being
812  *           modified so they both get updated.
813  ************************************************************************/
814 static void 
815 InitializeNewButton( void )
816 {
817         register int     n;
818         Arg              args[6];
819
820         n=0;
821         XtSetArg(args[n], XmNforeground, edit.color_set->fg.pixel); n++;
822         XtSetArg(args[n], XmNbackground, edit.color_set->bg.pixel); n++;
823         XtSetArg(args[n], XmNarmColor, edit.color_set->sc.pixel); n++;
824         if(UsePixmaps == FALSE)
825         {
826            XtSetArg(args[n], XmNtopShadowColor, 
827                                       edit.color_set->ts.pixel); n++;
828            XtSetArg(args[n], XmNbottomShadowColor, 
829                                       edit.color_set->bs.pixel); n++;
830         }
831         XtSetValues(edit.newButton,args,n);
832 }
833
834
835 /************************************************************************
836  *   InitializeOldButton()
837  *           Called only once from CreateColorEditor.  Allocates a
838  *           new PixelSet to hold onto the original ColorSet values
839  *           while the newButton gets updated.  Shouldn't be called if
840  *           monitor is B_W.
841  ************************************************************************/
842 static void 
843 InitializeOldButton( void )
844 {
845     register int     i,n, numOfPixels;
846     unsigned long    *pixels;
847     unsigned long    plane_mask;
848     int              hue, val, sat, status;
849
850     if(UsePixmaps == FALSE)
851        if(FgColor == DYNAMIC)
852           numOfPixels = 5;
853        else /* FgColor == BLACK or WHITE */
854           numOfPixels = 4;
855     else  /* UsePixmaps == TRUE */
856        if(FgColor == DYNAMIC)
857           numOfPixels = 3;
858        else /* FgColor == BLACK or WHITE */
859           numOfPixels = 2;
860
861    /* Allocate enough space to store numOfPixels. */
862      pixels = (unsigned long *)XtMalloc (numOfPixels * sizeof (unsigned long));
863
864    /* Allocate new color cells for the new button */
865      status = XAllocColorCells (style.display, style.colormap,
866                           0, &plane_mask, 0, pixels, numOfPixels);
867      
868      if(status == FALSE)
869      {
870       /* Dialog stating that can't allocate enough pixels to color 
871          the old button. Just don't create old button.*/
872          edit.oldButtonColor.bg.pixel = edit.color_set->bg.pixel;
873          edit.oldButtonColor.bg.flags = edit.color_set->bg.flags;
874          edit.oldButtonColor.fg.pixel = edit.color_set->fg.pixel;
875          edit.oldButtonColor.fg.flags = edit.color_set->fg.flags;
876          edit.oldButtonColor.sc.pixel = edit.color_set->sc.pixel;
877          edit.oldButtonColor.sc.flags = edit.color_set->sc.flags;
878          edit.oldButtonColor.bs.pixel = edit.color_set->bs.pixel;
879          edit.oldButtonColor.bs.flags = edit.color_set->bs.flags;
880          edit.oldButtonColor.ts.pixel = edit.color_set->ts.pixel;
881          edit.oldButtonColor.ts.flags = edit.color_set->ts.flags;
882          OldNewSame = True;
883      }
884      else
885      {
886
887        /*  Assign the pixels */
888          n=0;
889          edit.oldButtonColor.bg.pixel = pixels[n++];
890          edit.oldButtonColor.bg.flags = DoRed | DoGreen | DoBlue;
891          edit.oldButtonColor.sc.pixel = pixels[n++];
892          edit.oldButtonColor.sc.flags = DoRed | DoGreen | DoBlue;
893          if(FgColor == DYNAMIC)
894          {
895             edit.oldButtonColor.fg.pixel = pixels[n++];
896             edit.oldButtonColor.fg.flags = DoRed | DoGreen | DoBlue;
897          } 
898          else
899          {
900             edit.oldButtonColor.fg.pixel = edit.color_set->fg.pixel;
901             edit.oldButtonColor.fg.flags = edit.color_set->fg.flags;
902          }
903          if(UsePixmaps == FALSE)
904          {
905             edit.oldButtonColor.bs.pixel = pixels[n++];
906             edit.oldButtonColor.bs.flags = DoRed | DoGreen | DoBlue;
907             edit.oldButtonColor.ts.pixel = pixels[n];
908             edit.oldButtonColor.ts.flags = DoRed | DoGreen | DoBlue;
909          }
910          else
911          {
912             edit.oldButtonColor.bs.pixel = edit.color_set->bs.pixel;
913             edit.oldButtonColor.bs.flags = edit.color_set->bs.flags;
914             edit.oldButtonColor.ts.pixel = edit.color_set->ts.pixel;
915             edit.oldButtonColor.ts.flags = edit.color_set->ts.flags;
916          }
917      }
918
919     CopyPixelSet(&edit.oldButtonColor,edit.color_set);
920     
921     RGBtoHSV(edit.oldButtonColor.bg.red, 
922              edit.oldButtonColor.bg.green, 
923              edit.oldButtonColor.bg.blue, 
924              &hue, &sat, &val);
925     
926     XtFree((char *)pixels);
927 }
928
929 /************************************************************************
930  *   GenerateColors()
931  *           Generates new RGB values for fg, ts, bs, sc based on bg.
932  *           The Color generation routine will be exported in motif1.1
933  *           The generated colors are then used to update the pixels
934  *           of the ColorSet being edited.
935  ************************************************************************/
936 static void 
937 GenerateColors( void )
938 {
939     int hue, sat, val;
940     int         j=0;
941     XColor      colors[5];
942
943     if (edit.calcRGB == NULL) 
944         edit.calcRGB = XmGetColorCalculation();
945
946     (*edit.calcRGB) (&edit.color_set->bg, &edit.color_set->fg, 
947                      &edit.color_set->sc, &edit.color_set->ts, 
948                      &edit.color_set->bs);
949
950     RGBtoHSV(edit.color_set->bg.red, edit.color_set->bg.green,
951                 edit.color_set->bg.blue,
952                 &hue, &sat, &val);
953
954     colors[j++] =  edit.color_set->bg;
955     colors[j++] =  edit.color_set->sc;
956
957     if (FgColor == DYNAMIC)
958         colors[j++] =  edit.color_set->fg;
959
960     if(UsePixmaps == FALSE)
961     {
962        colors[j++] =  edit.color_set->ts;
963        colors[j++] =  edit.color_set->bs;
964     }
965     else
966     {
967         edit.color_set->ts.red = 65535;
968         edit.color_set->ts.green = 65535;
969         edit.color_set->ts.blue = 65535;
970
971         edit.color_set->bs.red = 0;
972         edit.color_set->bs.green = 0;
973         edit.color_set->bs.blue = 0;
974     }
975
976     XStoreColors(style.display, style.colormap, colors, j );
977  }
978
979 /************************************************************************
980  *   changeRGB_CB()
981  *           Called when one of the RGB scales is moved
982  ************************************************************************/
983 static void 
984 changRGB_CB(
985         Widget w,
986         XtPointer client_data,
987         XtPointer call_data )
988 {
989     int reason_code;
990     int value;
991     int color;
992
993     reason_code = ((XmAnyCallbackStruct *)call_data)->reason;
994     if ( reason_code == XmCR_VALUE_CHANGED || reason_code == XmCR_DRAG )
995     {
996         color = (int) client_data;
997         value = ((XmScaleCallbackStruct *)call_data)->value;
998
999         if (edit.current_scale == NONE)
1000         {
1001             edit.current_scale = color;       
1002             /*
1003              * Shift value -- to make up for scale max of only 0xff
1004              */
1005             value <<= 8;
1006             switch (color)
1007             {
1008                   case RED:
1009                       edit.color_set->bg.red = value;
1010                       break;
1011                   case GREEN:
1012                       edit.color_set->bg.green = value;
1013                       break;
1014                   case BLUE:
1015                       edit.color_set->bg.blue = value;
1016                       break;
1017                   default:
1018                       /* this case should never be hit */
1019                       return;
1020             }
1021
1022             SetScales(&edit.color_set->bg);
1023             GenerateColors();
1024             edit.current_scale = NONE;
1025         }
1026     }
1027 }
1028
1029
1030 /************************************************************************
1031  *   changHSV_CB()
1032  *           Called when one of the HSV scales is moved
1033  ************************************************************************/
1034 static void 
1035 changHSV_CB(
1036         Widget w,
1037         XtPointer client_data,
1038         XtPointer call_data )
1039 {
1040     int reason_code;
1041     int value;
1042     int scale;
1043     int hue, sat, val;
1044
1045     reason_code = ((XmAnyCallbackStruct *)call_data)->reason;
1046     if ( reason_code == XmCR_VALUE_CHANGED || reason_code == XmCR_DRAG )
1047     {
1048         scale = (int) client_data;
1049         value = ((XmScaleCallbackStruct *)call_data)->value;
1050
1051         if (edit.current_scale == NONE)
1052         {
1053             edit.current_scale = scale;       
1054             switch (scale)
1055             {
1056                   case HUE:
1057                      hue = value;
1058                      XmScaleGetValue(edit.satScale, &sat);
1059                      XmScaleGetValue(edit.valScale, &val);
1060                      break;
1061                   case SATURATION:
1062                      XmScaleGetValue(edit.hueScale, &hue);
1063                      sat = value;
1064                      XmScaleGetValue(edit.valScale, &val);
1065                      break;
1066                   case VALUE:
1067                      XmScaleGetValue(edit.hueScale, &hue);
1068                      XmScaleGetValue(edit.satScale, &sat);
1069                      val = value;
1070                      break;
1071                   default:
1072                      /* this case should never be hit */
1073                      return;
1074             }
1075
1076             HSVtoRGB(hue, sat, val, 
1077                      &edit.color_set->bg.red, 
1078                      &edit.color_set->bg.green,
1079                      &edit.color_set->bg.blue);
1080
1081             SetRGBHSVScales(&edit.color_set->bg, hue, sat, val);
1082             GenerateColors();
1083             edit.current_scale = NONE;
1084         }      
1085      }
1086 }
1087
1088
1089 /************************************************************************
1090  *   SetScales()
1091  *           passed a XColor, generates HSV values and updates all scales.
1092  ************************************************************************/
1093 static void 
1094 SetScales(
1095         XColor *rgb )
1096 {
1097         int      h, s, v;
1098
1099         RGBtoHSV(rgb->red, rgb->green, rgb->blue, &h, &s, &v);
1100
1101         SetRGBHSVScales(rgb, h, s, v);
1102 }
1103
1104
1105 /************************************************************************
1106  *   SetRGBHSVScales()
1107  *           updates RGB and HSV scales
1108  ************************************************************************/
1109 static void 
1110 SetRGBHSVScales(
1111         XColor *rgb,
1112         int h,
1113         int s,
1114         int v )
1115 {
1116         XmScaleSetValue(edit.redScale,rgb->red >> 8);
1117         XmScaleSetValue(edit.greenScale,rgb->green >> 8);
1118         XmScaleSetValue(edit.blueScale,rgb->blue >> 8);
1119
1120         XmScaleSetValue(edit.hueScale,h);
1121         XmScaleSetValue(edit.satScale,s);
1122         XmScaleSetValue(edit.valScale,v);
1123 }
1124
1125
1126 /************************************************************************
1127  *   CopyPixelSet()
1128  *   
1129  ************************************************************************/
1130 static void 
1131 CopyPixelSet(
1132         ColorSet *color_set_dest,
1133         ColorSet *color_set_src )
1134 {
1135     Arg          args[6];
1136     int          n;
1137     int              j=0;
1138     XColor           colors[5];
1139
1140     color_set_dest->bg.red = color_set_src->bg.red;
1141     color_set_dest->bg.blue = color_set_src->bg.blue;
1142     color_set_dest->bg.green = color_set_src->bg.green;
1143     if(OldNewSame)
1144        color_set_dest->bg.pixel = color_set_src->bg.pixel;
1145     colors[j++] =  color_set_dest->bg;
1146
1147     color_set_dest->sc.red = color_set_src->sc.red;
1148     color_set_dest->sc.blue = color_set_src->sc.blue;
1149     color_set_dest->sc.green = color_set_src->sc.green;
1150     if(OldNewSame)
1151        color_set_dest->bg.pixel = color_set_src->bg.pixel;
1152     colors[j++] =  color_set_dest->sc;
1153
1154     color_set_dest->fg.red = color_set_src->fg.red;
1155     color_set_dest->fg.blue = color_set_src->fg.blue;
1156     color_set_dest->fg.green = color_set_src->fg.green;
1157     if(FgColor == DYNAMIC)
1158     {
1159        if(OldNewSame)
1160           color_set_dest->bg.pixel = color_set_src->bg.pixel;
1161        colors[j++] =  color_set_dest->fg;
1162     }
1163
1164     color_set_dest->ts.red = color_set_src->ts.red;
1165     color_set_dest->ts.blue = color_set_src->ts.blue;
1166     color_set_dest->ts.green = color_set_src->ts.green;
1167     color_set_dest->bs.red = color_set_src->bs.red;
1168     color_set_dest->bs.blue = color_set_src->bs.blue;
1169     color_set_dest->bs.green = color_set_src->bs.green;
1170     if(UsePixmaps == FALSE)
1171     {
1172        if(OldNewSame)
1173           color_set_dest->bg.pixel = color_set_src->bg.pixel;
1174        colors[j++] =  color_set_dest->ts;
1175        colors[j++] =  color_set_dest->bs;
1176     }
1177     if(OldNewSame && edit.oldButton != NULL)
1178     {
1179        n=0;
1180        XtSetArg(args[n], XmNforeground, color_set_dest->fg.pixel); n++;
1181        XtSetArg(args[n], XmNbackground, color_set_dest->bg.pixel); n++;
1182        XtSetArg(args[n], XmNarmColor, color_set_dest->sc.pixel); n++;
1183        XtSetArg(args[n], XmNtopShadowColor,
1184                                     color_set_dest->ts.pixel); n++;
1185        XtSetArg(args[n], XmNbottomShadowColor, 
1186                                     color_set_dest->bs.pixel); n++;
1187
1188        XtSetValues(edit.oldButton, args, n);
1189     }
1190
1191     XStoreColors(style.display, style.colormap, colors, j );
1192
1193 }
1194
1195
1196 /************************************************************************
1197  *   RGBtoHSV()
1198  *
1199  ************************************************************************/
1200 void 
1201 RGBtoHSV(
1202 #if NeedWidePrototypes
1203         unsigned int r ,
1204         unsigned int g ,
1205         unsigned int b ,
1206 #else
1207         unsigned short r,
1208         unsigned short g,
1209         unsigned short b,
1210 #endif
1211         int *h,
1212         int *s,
1213         int *v )
1214 {
1215    double red, green, blue;
1216    double red1, green1, blue1;
1217    double hue, saturation, value;
1218    double base;
1219
1220
1221    red1 = (double)r / 65280.0;
1222    green1 = (double)g / 65280.0;
1223    blue1 = (double)b / 65280.0;
1224
1225    value = max(red1, green1, blue1);
1226    base = min(red1, green1, blue1);
1227
1228    if(value != 0.0)
1229       saturation = (value - base) / value;
1230    else
1231       saturation = 0.0;
1232
1233    hue = 0.0;
1234
1235    if( saturation != 0.0 )
1236    {
1237       red = (value - red1 ) / (value - base);
1238       green = (value - green1 ) / (value - base);
1239       blue = (value - blue1 ) / (value - base);
1240
1241       if(value == red1)
1242       {
1243          if(base == green1)
1244             hue = 5.0 + blue;
1245          else
1246             hue = 1.0 - green;
1247       }
1248       else if(value == green1)
1249       {
1250          if(base == blue1)
1251             hue = 1.0 + red;
1252          else
1253             hue = 3.0 - blue;
1254       }
1255       else
1256       {
1257          if(base == red1)
1258             hue = 3.0 + green;
1259          else
1260             hue = 5.0 - red;
1261       }
1262       hue *= 60.0;
1263       if( hue == 360.0)
1264         hue = 0.0;
1265     }
1266
1267     *h = (int)hue;
1268     *s = ((int)(saturation * 65280.0)) >> 8;
1269     *v = ((int)(value * 65280.0)) >> 8;
1270 }
1271
1272 /************************************************************************
1273  *   HSVtoRGB()
1274  *
1275  *  Converts hue, saturation, and value to RGB values.  
1276  *  Hue is in the range 0 to 360, while saturation and value
1277  *  are in the range 0 to 255
1278  ************************************************************************/
1279 static void 
1280 HSVtoRGB(
1281         int h,
1282         int s,
1283         int v,
1284         unsigned short *r,
1285         unsigned short *g,
1286         unsigned short *b )
1287 {
1288    double p1, p2, p3;
1289    double hue, sat, val;
1290    double red, green, blue;
1291    double i, f;
1292
1293    hue = (double)h / 60.0;
1294    i = floor(hue);
1295    f = hue - i;
1296
1297    val = (double)v / 255.0;
1298    sat = (double)s / 255.0;
1299
1300    p1 = val * (1.0 - sat);
1301    p2 = val * (1.0 -(sat * f));
1302    p3 = val * (1.0 -(sat * (1.0 - f)));
1303
1304    switch((int)i)
1305    {
1306      case 0:
1307        red = val;
1308        green = p3;
1309        blue = p1;
1310        break;
1311      case 1:
1312        red = p2;
1313        green = val;
1314        blue = p1;
1315        break;
1316      case 2:
1317        red = p1;
1318        green = val;
1319        blue = p3;
1320        break;
1321      case 3:
1322        red = p1;
1323        green = p2;
1324        blue = val;
1325        break;
1326      case 4:
1327        red = p3;
1328        green = p1;
1329        blue = val;
1330        break;
1331      case 5:
1332        red = val;
1333        green = p1;
1334        blue = p2;
1335        break;
1336    }
1337
1338    *r = (int)(red * 65280.0);
1339    *g = (int)(green * 65280.0);
1340    *b = (int)(blue * 65280.0);
1341
1342 }
1343
1344
1345 /************************************************************************
1346  *   max()
1347  * 
1348  ************************************************************************/
1349 static double 
1350 max(
1351         double x,
1352         double y,
1353         double z )
1354 {
1355    if(x >= y && x >= z)
1356       return(x);
1357
1358    if(y >= z && y >= x)
1359       return(y);
1360
1361    if(z >= y && z >= x)
1362       return(z);
1363 }
1364
1365 /************************************************************************
1366  *   min()
1367  *
1368  ************************************************************************/
1369 static double 
1370 min(
1371         double x,
1372         double y,
1373         double z )
1374 {
1375    if(x <= y && x <= z)
1376       return(x);
1377
1378    if(y <= z && y <= x)
1379       return(y);
1380
1381    if(z <= y && z <= x)
1382       return(z);
1383 }
1384
1385 /************************************************************************
1386  *   grabcolorCB()
1387  *           Call back routine for grabbing a color from the screen
1388  ************************************************************************/
1389 static void 
1390 grabcolorCB(
1391         Widget w,
1392         XtPointer client_data,
1393         XtPointer call_data )
1394 {
1395         Cursor cursor = XCreateFontCursor(style.display, XC_crosshair);
1396         XImage *image_ptr; 
1397         Position x,y;
1398         XEvent event;
1399         Pixel pixel;
1400         XColor colorStruct;
1401         char           color_string[MAX_STR_LEN];
1402         int status, offset;
1403         Boolean notDone=True;
1404         KeySym keySym;
1405
1406        /* grab the pointer using target cursor */
1407         status = XtGrabPointer(style.colorDialog, TRUE, 
1408                                 ButtonPressMask | ButtonReleaseMask,
1409                                 GrabModeAsync, GrabModeAsync, None, 
1410                                 cursor, CurrentTime); 
1411         if (status != GrabSuccess)
1412         {
1413                _DtSimpleError (progName, DtWarning, NULL, 
1414                     ((char *)GETMESSAGE(17, 14, "Warning, couldn't grab pointer.\n")), NULL);
1415                return;
1416         }
1417        
1418        /* grab the keyboard so we can get the ESC button press */
1419         status = XtGrabKeyboard(style.colorDialog, False, GrabModeAsync, 
1420                                           GrabModeAsync, CurrentTime);
1421         if (status != GrabSuccess)
1422         {
1423                XtUngrabPointer (style.colorDialog, CurrentTime);
1424                _DtSimpleError (progName, DtWarning, NULL, 
1425                     ((char *)GETMESSAGE(17, 15, "Warning, couldn't grab Keyboard.\n")), NULL);
1426                return;
1427         }
1428  
1429         while(notDone)
1430         {
1431            XtNextEvent(&event);
1432            
1433            switch (event.type) {
1434               case ButtonPress:
1435                  break;
1436               case ButtonRelease:
1437                  notDone = False;
1438                  break;
1439               case KeyPress:
1440                 /* look for ESC key press and stop if we get one */
1441                  if( event.xkey.state & ShiftMask)
1442                    offset = 1;
1443                  else
1444                    offset = 0;
1445                  
1446                  keySym = XLookupKeysym((XKeyEvent *)&event, offset);
1447                  if (keySym == XK_Escape)
1448                  {
1449                     XtUngrabKeyboard (style.colorDialog, CurrentTime);
1450                     XtUngrabPointer (style.colorDialog, CurrentTime);
1451                     return;
1452                  }
1453              default:
1454                  XtDispatchEvent(&event);
1455            }
1456         }
1457
1458         XtUngrabKeyboard (style.colorDialog, CurrentTime);
1459         XtUngrabPointer (style.colorDialog, CurrentTime);
1460
1461         x = (Position)event.xbutton.x_root;
1462         y = (Position)event.xbutton.y_root;
1463         image_ptr = XGetImage (style.display, style.root, x, y, 1, 1, 
1464                                AllPlanes, ZPixmap);
1465         pixel = (Pixel) XGetPixel (image_ptr, 0, 0);
1466         XDestroyImage (image_ptr);
1467
1468         colorStruct.pixel = pixel;
1469
1470         XQueryColor (style.display, style.colormap, &colorStruct);
1471
1472         edit.color_set->bg.red = colorStruct.red;
1473         edit.color_set->bg.green = colorStruct.green;
1474         edit.color_set->bg.blue = colorStruct.blue;
1475
1476         SetScales(&edit.color_set->bg);
1477         GenerateColors();
1478
1479 }
1480
1481 /************************************************************************
1482  *   GetPixel()
1483  *   
1484  ************************************************************************/
1485 static Pixel 
1486 GetPixel(
1487         Widget widget,
1488         char *color_string )
1489 {
1490     XrmValue from, to;
1491
1492     from.size = strlen(color_string) + 1;
1493     if (from.size < sizeof(String))
1494         from.size = sizeof(String);
1495     from.addr = color_string;
1496     XtConvert(widget, XmRString, &from, XmRPixel, &to);
1497
1498     return ((Pixel) *((Pixel *) to.addr));
1499 }
1500
1501
1502
1503 /*
1504 **  dialogBoxCB
1505 **      Process callback from the Ok, Cancel and Help pushButtons in the 
1506 **      DialogBox.
1507 */
1508 static void 
1509 dialogBoxCB(
1510         Widget w,
1511         XtPointer client_data,
1512         XtPointer call_data )
1513 {
1514     palette *tmp_palette;
1515     DtDialogBoxCallbackStruct *cb = (DtDialogBoxCallbackStruct *) call_data;
1516
1517     switch (cb->button_position)
1518     {
1519       case OK_BUTTON:
1520
1521           WriteOutPalette( pCurrentPalette->name);
1522
1523           if(strcmp(pCurrentPalette->name, defaultName) == 0)
1524           {
1525              UpdateDefaultPalette();
1526              SaveOrgPalette();
1527           }
1528
1529           XtUnmanageChild(edit.DialogShell);
1530           break;
1531
1532       case CANCEL_BUTTON:
1533           CopyPixelSet(edit.color_set, &edit.oldButtonColor);
1534           XtUnmanageChild(edit.DialogShell);
1535           break;
1536          
1537       case HELP_BUTTON:
1538         XtCallCallbacks(edit.DialogShell, XmNhelpCallback, (XtPointer)NULL);
1539         break;
1540
1541       default:
1542         break;
1543     }
1544 }
1545
1546
1547
1548 /************************************************************************
1549  * _DtmapCB
1550  *
1551  ************************************************************************/
1552 static void 
1553 _DtmapCB(
1554         Widget w,
1555         XtPointer client_data,
1556         XtPointer call_data )
1557 {
1558     Position     newX,newY;
1559     Position     x,y;
1560     Dimension    editWidth,editHeight;
1561     Dimension    width,height;
1562     Arg          args[6];
1563     int          n;
1564     XtWidgetGeometry reply;
1565     Widget       parent = (Widget) client_data;
1566
1567
1568     DtWsmRemoveWorkspaceFunctions(style.display, XtWindow(XtParent(w)));
1569     
1570     if (!save.restoreFlag)
1571     {
1572         /* get x,y,width,height of parent shell */
1573
1574         x = XtX(XtParent(parent));
1575         y = XtY(XtParent(parent));
1576         height = XtHeight(parent);
1577         width = XtWidth(parent);
1578
1579         editHeight = XtHeight(edit.DialogShell);
1580         editWidth = XtWidth(edit.DialogShell);
1581
1582         newX = x + width/2 - editWidth/2;
1583         if (newX < 0)
1584             newX = 0;
1585         newY = y + height/2 - editHeight;
1586         if (newY < 0)
1587             newY = y + height;
1588             
1589         n = 0;
1590         XtSetArg(args[n], XmNx, newX); n++;
1591         XtSetArg(args[n], XmNy, newY); n++;
1592         XtSetValues(edit.DialogShell,args,n);
1593
1594     }
1595
1596     XtRemoveCallback(edit.DialogShell, XmNmapCallback, _DtmapCB, NULL);
1597
1598 }
1599
1600
1601 /****************************************************
1602  * restoreColor(shell, db)
1603  * restore any state information saved with saveColor.  
1604  * This is called from restoreSession with the application shell 
1605  * and the special xrm database retrieved for restore.
1606  ****************************************************/
1607 void 
1608 restoreColorEdit(
1609         Widget shell,
1610         XrmDatabase db )
1611 {
1612
1613   XrmName xrm_name[5];
1614   XrmRepresentation rep_type;
1615   XrmValue value;
1616
1617     xrm_name [0] = XrmStringToQuark ("colorEditDlg");
1618     xrm_name [2] = 0;
1619
1620     /* get x position */
1621     xrm_name [1] = XrmStringToQuark ("x");
1622     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)){
1623       XtSetArg (save.posArgs[save.poscnt], XmNx, atoi((char *)value.addr)); save.poscnt++;
1624       save.restoreFlag = True;
1625     }
1626
1627     /* get y position */
1628     xrm_name [1] = XrmStringToQuark ("y");
1629     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)){
1630       XtSetArg (save.posArgs[save.poscnt], XmNy, atoi((char *)value.addr)); save.poscnt++;
1631     }
1632
1633     /*Collect any other parameters you saved into static variables.*/
1634
1635     xrm_name [1] = XrmStringToQuark ("ismapped");
1636     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
1637     /* Are we supposed to be mapped? */
1638     if (strcmp(value.addr, "True") == 0)
1639       XtCallCallbacks(modifyColorButton, XmNactivateCallback, NULL);
1640 }
1641
1642
1643 /****************************************************
1644  * saveColorEdit(fd)
1645  * This routine will write out to the passed file descriptor any state
1646  * information this dialog needs.  It is called from saveSessionCB with 
1647  * the file already opened.
1648  * All information is saved in xrm format.  There is no restriction
1649  * on what can be saved.  It doesn't have to be defined or be part of any
1650  * widget or Xt definition.  Just name and save it here and recover it in
1651  * restoreColor.  The suggested minimum is whether you are mapped, and your
1652  * location.
1653  *****************************************************/
1654 void 
1655 saveColorEdit(
1656         int fd )
1657 {
1658     Position x,y;
1659     char *bufr = style.tmpBigStr;     /* size=[1024], make bigger if needed */
1660     XmVendorShellExtObject  vendorExt;
1661     XmWidgetExtData         extData;
1662
1663     if (edit.DialogShell != NULL) 
1664     {
1665         if (XtIsManaged(edit.DialogShell))
1666             sprintf(bufr, "*colorEditDlg.ismapped: True\n");
1667         else
1668             sprintf(bufr, "*colorEditDlg.ismapped: False\n");
1669
1670         /* Get and write out the geometry info for our Window */
1671         x = XtX(XtParent(edit.DialogShell));
1672         y = XtY(XtParent(edit.DialogShell));
1673
1674         /* Modify x & y to take into account window mgr frames
1675          * This is pretty bogus, but I don't know a better way to do it.
1676          */
1677         extData = _XmGetWidgetExtData(style.shell, XmSHELL_EXTENSION);
1678         vendorExt = (XmVendorShellExtObject)extData->widget;
1679         x -= vendorExt->vendor.xOffset;
1680         y -= vendorExt->vendor.yOffset;
1681
1682         sprintf(bufr, "%s*colorEditDlg.x: %d\n", bufr, x);
1683         sprintf(bufr, "%s*colorEditDlg.y: %d\n", bufr, y);
1684         /*any other parameter you want to save goes here*/
1685         write (fd, bufr, strlen(bufr));
1686     }
1687 }
1688