dtstyle: resolve 30 compiler warnings.
[oweals/cde.git] / cde / programs / dtstyle / ColorMain.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 /************************************<+>*************************************
24  ****************************************************************************
25  **
26  **   File:        ColorMain.c
27  **
28  **   Project:     DT 3.0 
29  **
30  **   Description: Controls the Dtstyle Color dialog
31  **
32  **
33  **  (c) Copyright Hewlett-Packard Company, 1990.  
34  **
35  **
36  **
37  ****************************************************************************
38  ************************************<+>*************************************/
39 /* $TOG: ColorMain.c /main/8 1998/01/12 10:41:53 cshi $ */
40 /*+++++++++++++++++++++++++++++++++++++++*/
41 /* include files                         */
42 /*+++++++++++++++++++++++++++++++++++++++*/
43 #ifdef __apollo
44 #include  "/sys5/usr/include/limits.h"
45 #else  /* common default */
46 #include <limits.h>
47 #endif /* __apollo */
48
49 #include <locale.h>
50 #include <stdlib.h>
51 #include <errno.h>
52 #include <X11/Intrinsic.h>
53 #include <X11/Xlib.h>
54 #include <Xm/MwmUtil.h>
55
56 #include <Xm/Xm.h>
57 #include <Xm/XmP.h>
58 #include <Xm/Form.h>
59 #include <Xm/Frame.h>
60 #include <Xm/Label.h>
61 #include <Xm/List.h>
62 #include <Xm/MessageB.h>
63 #include <Xm/Protocols.h>
64 #include <Xm/PushB.h>
65 #include <Xm/PushBG.h>
66 #include <Xm/ToggleBG.h>
67 #include <Xm/RowColumn.h>
68 #include <Xm/SelectioB.h>
69 #include <Xm/TextF.h>
70 #include <Xm/VendorSEP.h>
71
72 #include <Dt/DialogBox.h>
73 #include <Dt/Icon.h>
74 #include <Dt/TitleBox.h>
75
76 #include <Dt/HourGlass.h>
77
78 #include "Help.h"
79 #include "Main.h"
80 #include "SaveRestore.h"
81 #include "ColorMain.h"
82 #include "ColorFile.h"
83 #include "ColorEdit.h"
84
85 #ifdef sun
86 #include "OWsync.h"
87 #endif
88
89 /*+++++++++++++++++++++++++++++++++++++++*/
90 /* include extern functions              */
91 /*+++++++++++++++++++++++++++++++++++++++*/
92
93
94 /*+++++++++++++++++++++++++++++++++++++++*/
95 /* Local #defines                        */
96 /*+++++++++++++++++++++++++++++++++++++++*/
97 #define DEFAULT_PALETTE    "Default"
98 #define TYPE_OF_MONITOR  "Type Of Monitor" /* also in dtsession/SrvPalette.c */
99
100 #define BORDER_WIDTH                3
101 #define COLOR_BUTTON_WIDTH         35
102 #define COLOR_BUTTON_HEIGHT        35
103 #define ADD_PALETTE_TOP_OFFSET     20
104 #define ADD_PALETTE_LEFT_POSITION  65
105 #define PALETTE_RC_RIGHT_POSITION  60
106
107 #define DEFAULT_COLOR  (XmCO_HIGH_COLOR + 1)
108
109 #define B_W_STR            "B_W"
110 #define LOW_COLOR_STR      "LOW_COLOR"
111 #define MEDIUM_COLOR_STR   "MEDIUM_COLOR"
112 #define HIGH_COLOR_STR     "HIGH_COLOR"
113 #define DEFAULT_COLOR_STR  "DEFAULT"
114
115 #define STR1 ((char *)GETMESSAGE(14, 29, "A palette named '%s' already exists.\nThis new palette will overwrite the old one.\nIs this what you want to do?"))
116 #define STR2 ((char *)GETMESSAGE(14, 23, "Delete palette '%s'?\n"))
117 #define NEXT_SESSION ((char *)GETMESSAGE(14, 28, "The selected palette will take effect\nat your next session."))
118 #define CANT_DELETE ((char *)GETMESSAGE(14, 18, "Can't delete the last palette.\n"))
119 #define COLORUSE_WHEN ((char *)GETMESSAGE(14, 27, "The new Color Use value will take effect\nat your next session."))
120
121 /*+++++++++++++++++++++++++++++++++++++++*/
122 /* Local typedefs                        */
123 /*+++++++++++++++++++++++++++++++++++++++*/
124
125 typedef struct {
126     Widget           colorForm;
127     Widget           paletteTB;
128     Widget           palettesForm;
129     Widget           addPaletteButton;
130     Widget           deletePaletteButton;
131     Widget           buttonsTB;
132     Widget           colorlabel;
133     Widget           highColorTG;
134     Widget           mediumColorTG;
135     Widget           lowColorTG;
136     Widget           blackWhiteTG;
137     Widget           defaultTG;
138     Widget           dlg;
139     int              origColorUse;
140     char             *currentColorUseStr;
141     int              currentColorUse;
142 } colorWidgets;
143
144 /*+++++++++++++++++++++++++++++++++++++++*/
145 /* Internal Functions                    */
146 /*+++++++++++++++++++++++++++++++++++++++*/
147
148 static int CreateMainWindow( Widget parent) ;
149 static void selectPaletteCB( 
150                         Widget w,
151                         XtPointer client_data,
152                         XtPointer call_data) ;
153 static void selectColorCB( 
154                         Widget w,
155                         XtPointer client_data,
156                         XtPointer call_data) ;
157 static void timeoutCB( 
158                         XtPointer client_data,
159                         XtIntervalId *id) ;
160 static void addPaletteCB( 
161                         Widget w,
162                         XtPointer client_data,
163                         XtPointer call_data) ;
164 static void addCancelCB( 
165                         Widget w,
166                         XtPointer client_data,
167                         XtPointer call_data) ;
168 static void addOkCB( 
169                         Widget w,
170                         XtPointer client_data,
171                         XtPointer call_data) ;
172 static void setDlgOkCB( 
173                         Widget w,
174                         XtPointer client_data,
175                         XtPointer call_data) ;
176 static void modifyColorCB( 
177                         Widget w,
178                         XtPointer client_data,
179                         XtPointer call_data) ;
180 static void dialogBoxCB( 
181                         Widget w,
182                         XtPointer client_data,
183                         XtPointer call_data) ;
184 static void AddName(    
185                         palette *newPalette) ;
186 static void deletePaletteCB( 
187                         Widget w,
188                         XtPointer client_data,
189                         XtPointer call_data) ;
190 static void deleteOkCB( 
191                         Widget w,
192                         XtPointer client_data,
193                         XtPointer call_data) ;
194 static void deleteCancelCB( 
195                         Widget w,
196                         XtPointer client_data,
197                         XtPointer call_data) ;
198 static void resourcesCB(
199                         Widget w,
200                         XtPointer client_data,
201                         XtPointer call_data) ;
202 static void colorUseCB(
203                         Widget w,
204                         XtPointer client_data,
205                         XtPointer call_data) ;
206 static void colorUseExitCB(
207                         Widget w,
208                         XtPointer client_data,
209                         XtPointer call_data) ;
210 static void activateCBexitColor( 
211                         Widget w,
212                         XtPointer client_data,
213                         XtPointer call_data) ;
214 static void _DtmapCB( 
215                         Widget w,
216                         XtPointer client_data,
217                         XtPointer call_data) ;
218 static void _DtmapCB_colorUse( 
219                         Widget w,
220                         XtPointer client_data,
221                         XtPointer call_data) ;
222 static void colorEditorCB(
223                         Widget w,
224                         XtPointer client_data,
225                         XtPointer call_data) ;
226
227 static void allocNewColors(void);
228 static Boolean ValidName( char *name) ;
229 void loadDatabase();
230
231
232
233 /*+++++++++++++++++++++++++++++++++++++++*/
234 /* Global Variables                      */
235 /*+++++++++++++++++++++++++++++++++++++++*/
236
237 Atom     XA_CUSTOMIZE;
238 Atom     XA_TYPE_MONITOR;
239
240 /*  Palettes exist in a linked list. */
241 palette *pHeadPalette;
242 palette *pCurrentPalette;
243 palette *pOldPalette;
244
245 Widget  modifyColorButton;
246 int     TypeOfMonitor;
247 Bool    UsePixmaps;
248 int     FgColor;
249 Widget  paletteList;
250 Widget  deleteButton;
251 char    *defaultName;
252 Bool    WaitSelection;
253 int     NumOfPalettes;
254
255 /*+++++++++++++++++++++++++++++++++++++++*/
256 /* Internal Variables                    */
257 /*+++++++++++++++++++++++++++++++++++++++*/
258
259 /* palette names without the .dp */
260 static char *WHITE_BLACK = "WhiteBlack";
261 static char *BLACK_WHITE = "BlackWhite";
262 static char *WHITE_ONLY = "White";
263 static char *BLACK_ONLY = "Black";
264
265 static char *PALETTEDLG = "paletteDlg";
266 static saveRestore save = {FALSE, 0, };
267
268 static Widget           colorButton[XmCO_MAX_NUM_COLORS];
269 static Widget           addDialog;
270 static Widget           deleteDialog;
271 static Widget           colorUseDialog;
272 static palette          OrgPalette;
273 static XtIntervalId     timeID;
274 static int              dclick_time;
275 static int              selected_button;
276 static int              selected_position;
277 static Atom             XA_WM_DELETE_WINDOW;
278 static char             defaultName_restore[50];
279 static palette         *loop_palette, *loop_palette2;
280 static int              loopcount = 0;
281 static colorWidgets     colorDialog; 
282 static Widget           gParent;
283
284
285 /*
286  *   copy of the system palette description file for the 
287  *   current locale in xrm form
288  */
289 XrmDatabase sys_pl_DB = NULL;
290
291 /*
292  *   copy of the admin palette description file for the 
293  *   current locale in xrm form
294  */
295 XrmDatabase adm_pl_DB = NULL;
296
297 /*
298  *   final combination of the admin & system dta bases 
299  */
300 XrmDatabase pl_DB = NULL;
301
302 /*
303  *   copy of the palette description file from the user's home 
304  *   directory. This database is locale independent and will always
305  *   be loaded.
306  */
307 XrmDatabase hm_pl_DB = NULL;
308
309
310 void 
311 loadDatabase(void)
312 {
313   
314   char   *lang;
315   char   *pl_desc;
316
317   /* load the palette description data base for the given locale*/
318   /* from that locale's description file from the system location */
319
320   lang = setlocale (LC_CTYPE,NULL);
321
322 #ifdef hpux      /* hpux-specific parsing of the locale string */
323                  /* The following code is identical to the 
324                     ExtractLocaleName function in WmResParse.c
325                     from dtwm
326                  */
327 #define MAXLOCALE       64      /* buffer size of locale name */
328
329 {   char           *start;
330     char           *end;
331     int             len;
332     static char     buf[MAXLOCALE];
333
334     /*  If lang has a substring ":<category>;", extract <category>
335      *  from the first such occurrence as the locale name.
336      */
337
338     start = lang;
339     if (start = strchr (lang, ':')) {
340         start++;
341         if (end = strchr (start, ';')) {
342             len = end - start;
343             strncpy(buf, start, len);
344             *(buf + len) = '\0';
345             lang = buf;
346       }
347     }
348 }
349 #endif  /* hpux */
350
351   pl_desc = (char *)XtMalloc(strlen("/usr/dt/palettes/desc.") + strlen(lang) + 1);
352   strcpy (pl_desc,"/usr/dt/palettes/desc.");
353   strcat (pl_desc, lang);
354   if(sys_pl_DB = XrmGetFileDatabase (pl_desc))
355     XrmMergeDatabases(sys_pl_DB, &pl_DB);
356   XtFree(pl_desc);
357   
358   /* load the palette description data base for the given locale*/
359   /* from that locale's description file from the admin location */
360   pl_desc = (char *)XtMalloc(strlen("/etc/dt/palettes/desc.") + strlen(lang) + 1);
361   strcpy (pl_desc,"/etc/dt/palettes/desc.");
362   strcat (pl_desc, lang);
363   if (adm_pl_DB = XrmGetFileDatabase (pl_desc))
364     XrmMergeDatabases(adm_pl_DB, &pl_DB);
365   XtFree(pl_desc);
366
367   /* load the palette description data base regardless of locale*/
368   pl_desc = (char *) XtMalloc(strlen(style.home) +(strlen("/.dt/palettes/desc.palettes") + 1));
369   strcpy (pl_desc, style.home);
370   strcat (pl_desc, "/.dt/palettes/desc.palettes");
371   if (hm_pl_DB = XrmGetFileDatabase (pl_desc))
372     XrmMergeDatabases(hm_pl_DB, &pl_DB);
373   XtFree(pl_desc);
374
375   /* load the palette description data base regardless of locale for later use*/
376   pl_desc = (char *) XtMalloc(strlen(style.home) +(strlen("/.dt/palettes/desc.palettes") + 1));
377   strcpy (pl_desc, style.home);
378   strcat (pl_desc, "/.dt/palettes/desc.palettes");
379   if (XrmCombineFileDatabase (pl_desc, &pl_DB, True))
380     /* load a separate home data base to be later saved as a file  */
381     /* if any palettes are added */
382     hm_pl_DB = XrmGetFileDatabase (pl_desc);
383   XtFree(pl_desc);
384 }
385
386
387
388 void 
389 Customize(
390         Widget shell )
391 {
392     register int     i;
393
394     /*  
395     **  Main routine does the following:
396     **   1. Creates the color dialog shell
397     **   2. Checks which monitor the customizer is running on
398     **   3. Initialize color palettes
399     **
400     */
401   
402     /* can the user access the Color portion of Style? */
403
404     /* Not if Color Server is not running */
405     if (style.colorSrv == FALSE)
406     {
407        ErrDialog(((char *)GETMESSAGE(14, 25, "The color portion of the Style Manager\nwill not operate because the color server\nis not running.  Check $HOME/.dt/errorlog.")), style.shell);
408        return;
409     }
410
411     /* Not if useColorObj resource is False.  XmeUseColorObj will return false
412        if color server is not running or if the resource UseColorObj is False.
413        If the color server is not running, that case is caught above so if
414        we get here, the resource UseColorObj must be False.  This is an
415        undocumented resource. */
416        
417     if (XmeUseColorObj() == FALSE)
418     {
419        ErrDialog(((char *)GETMESSAGE(14, 26, "The color portion of the Style Manager\nwill not operate because the resource\n'useColorObj' is set to False.")), style.shell);
420        return;
421     }
422
423     if (style.colorDialog == NULL || style.count < 12)
424     {
425         _DtTurnOnHourGlass(shell);
426
427         /* Create Main Color Dialog */
428         if(CreateMainWindow(shell) == -1)
429         {
430            _DtTurnOffHourGlass(shell);
431            return; 
432         }
433         XtManageChild(style.colorDialog);  
434
435         _DtTurnOffHourGlass(shell);
436     }
437     else 
438     {
439         XtManageChild(style.colorDialog);
440
441         raiseWindow(XtWindow(XtParent(style.colorDialog)));
442     }
443
444    /* also determine if system uses long filenames, used by add palette only */
445     CheckFileType();
446
447     SaveOrgPalette();
448 }
449
450
451 /*
452 **  This routine creates the Color Customizer Main Window.   It is passed
453 **  the top level shell.
454 */
455 static int 
456 CreateMainWindow(
457         Widget parent )
458 {
459
460     /* Create the DialogBox dialog */
461     CreateDialogBoxD(parent);
462   
463     /* add some more to the dialog box */
464     AddToDialogBox();
465
466     /* Create the top part of the color dialog */
467     CreateTopColor1();
468     CreateTopColor2();
469
470     /* read in palettes */
471     ReadInPalettes(style.xrdb.paletteDir);
472
473     if (NumOfPalettes == 0)
474     {
475         /* error dialog - no palettes */
476         ErrDialog(((char *)GETMESSAGE(14, 38, "The color portion of the Style Manager\n\
477 will not operate because there are no palette\n\
478 files available.  Check $HOME/.dt/errorlog.")), style.shell);
479         return(-1);        
480     }
481
482     /* go get the list of palettes of the color dialog */
483     InitializePaletteList(parent,paletteList, False);
484
485     /* Allocate the pixels for the Current palette. 
486      * Will get the pixel values from the color server. 
487      */
488     AllocatePaletteCells(parent);
489
490     /* go create the bottom portion of the color dialog */
491     CreateBottomColor();
492
493 /*
494 **  Create the color buttons.  Have to do it after
495 **  initialize palettes so the correct pixels would be used.
496 */
497     CreatePaletteButtons(style.buttonsForm);
498     
499     return(0);
500
501 }
502
503
504 void 
505 CreatePaletteButtons(
506         Widget parent )
507 {
508     register int     i,n;
509     Arg              args[16];
510     XmString         string;
511     Pixmap           pixmap100;
512     Widget           paletteRc;
513
514     if(style.count > 11)
515        return;
516
517     if (TypeOfMonitor == XmCO_BLACK_WHITE)      /* create pixmaps for top/bottom shadow */
518     {
519         edit.pixmap25 = XmGetPixmap (style.screen, "25_foreground",
520                        BlackPixelOfScreen(style.screen),
521                        WhitePixelOfScreen(style.screen));
522         edit.pixmap75 = XmGetPixmap (style.screen, "75_foreground",
523                        BlackPixelOfScreen(style.screen),
524                        WhitePixelOfScreen(style.screen));
525     }
526     pixmap100 = XmGetPixmap (style.screen, "background",
527              BlackPixelOfScreen(style.screen),
528              pCurrentPalette->color[pCurrentPalette->active].bg.pixel); 
529     n = 0;
530     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
531     XtSetArg (args[n], XmNtopOffset, 0);  n++;
532     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
533     XtSetArg (args[n], XmNleftOffset, 0);  n++;
534     XtSetArg (args[n], XmNrightAttachment, XmATTACH_POSITION);  n++;
535     XtSetArg (args[n], XmNrightPosition, PALETTE_RC_RIGHT_POSITION);  n++;
536     XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM);  n++;
537     XtSetArg (args[n], XmNbottomOffset, 0);  n++;
538     XtSetArg (args[n], XmNspacing, 0);  n++;
539     XtSetArg (args[n], XmNmarginWidth, style.horizontalSpacing);  n++;
540     XtSetArg (args[n], XmNmarginHeight, style.verticalSpacing);  n++;
541     XtSetArg (args[n], XmNorientation, XmHORIZONTAL);  n++;
542     XtSetArg (args[n], XmNpacking, XmPACK_COLUMN);  n++;
543     XtSetArg (args[n], XmNadjustLast, False);  n++;
544     if(TypeOfMonitor == XmCO_HIGH_COLOR)
545     {
546        XtSetArg (args[n], XmNnumColumns, 2);  n++;
547     }
548     else
549     {
550        XtSetArg (args[n], XmNnumColumns, 1);  n++;
551     }
552     paletteRc = XmCreateRowColumn(parent, "paletteRc", args, n);
553     XtManageChild(paletteRc);
554
555     for (i=0; i<XmCO_MAX_NUM_COLORS; i++)
556     {
557        n=0;
558        XtSetArg (args[n], XmNrecomputeSize, False); n++; 
559        XtSetArg (args[n], XmNwidth,  COLOR_BUTTON_WIDTH); n++; 
560        XtSetArg (args[n], XmNheight, COLOR_BUTTON_HEIGHT); n++; 
561        /* allow traversal only if editing is possible */
562        if (style.dynamicColor || style.visualClass==TrueColor
563                         || style.visualClass==DirectColor)
564        {
565            XtSetArg (args[n], XmNtraversalOn, False); n++; 
566        }
567        XtSetArg (args[n], XmNborderWidth, BORDER_WIDTH); n++; 
568        XtSetArg (args[n], XmNborderColor, 
569            pCurrentPalette->color[pCurrentPalette->secondary].bg.pixel); n++;
570        XtSetArg (args[n], XmNforeground, 
571                      pCurrentPalette->color[i].fg.pixel); n++;
572        XtSetArg (args[n], XmNbackground,
573                      pCurrentPalette->color[i].bg.pixel); n++;
574        XtSetArg (args[n], XmNarmColor, pCurrentPalette->color[i].sc.pixel); n++;
575        XtSetArg (args[n], XmNmultiClick, XmMULTICLICK_KEEP); n++;
576        if (TypeOfMonitor == XmCO_LOW_COLOR)
577        {
578            XtSetArg (args[n], XmNhighlightColor, 
579             pCurrentPalette->color[pCurrentPalette->secondary].fg.pixel); n++;
580        }
581        else
582        {
583            XtSetArg (args[n], XmNhighlightPixmap, pixmap100); n++;
584        }
585        if(UsePixmaps == FALSE && TypeOfMonitor != XmCO_BLACK_WHITE)
586        {
587           XtSetArg (args[n], XmNtopShadowColor, 
588                             pCurrentPalette->color[i].ts.pixel); n++;
589           XtSetArg (args[n], XmNbottomShadowColor, 
590                             pCurrentPalette->color[i].bs.pixel); n++;
591        }
592        else if (TypeOfMonitor == XmCO_BLACK_WHITE)
593        {
594           XtSetArg (args[n], XmNtopShadowPixmap, edit.pixmap25);        n++;
595           XtSetArg (args[n], XmNbottomShadowPixmap, edit.pixmap75);     n++;
596        }
597        string = CMPSTR("     ");
598        XtSetArg (args[n], XmNlabelString, string); n++;
599        colorButton[i] = XmCreatePushButton(paletteRc, "colorButton", args, n);
600        /* allow access to modify functionality if available */
601        if (style.dynamicColor || style.visualClass==TrueColor
602                         || style.visualClass==DirectColor)
603            XtAddCallback(colorButton[i], XmNactivateCallback, selectColorCB, 
604                          (XtPointer) (intptr_t) i);  
605        XmStringFree(string);
606     }
607     XtManageChildren(colorButton,pCurrentPalette->num_of_colors);
608
609     if(!save.restoreFlag)
610        selected_button = 0;
611
612     /* draw selection border if editing is possible */
613     if (style.dynamicColor || style.visualClass==TrueColor
614                 || style.visualClass==DirectColor)
615     {
616         n=0;
617         XtSetArg (args[n], XmNborderColor, BlackPixelOfScreen(style.screen)); n++;
618         XtSetValues(colorButton[selected_button],args,n);
619     }
620
621         if(style.visualClass==TrueColor || style.visualClass==DirectColor)
622                 allocNewColors();
623
624     style.count++;
625 }
626
627 Boolean 
628 InitializePaletteList(
629         Widget shell,
630         Widget list,
631 #if NeedWidePrototypes
632         int startup )
633 #else
634         Boolean startup )
635 #endif
636 {
637   register int     n;
638   Arg              args[4];
639   XmString         string;
640   XmStringTable    string_table;
641   char            *name_str;
642   char            *class_str;
643   char            *str_type_return;
644   XrmValue         value_return;
645   
646   if(style.count > 8)
647     return(True);
648   
649   /* 
650    **  Add the palettes read in from ReadInPalettes
651    */
652   if(loopcount == 0) {
653     if (TypeOfMonitor != XmCO_BLACK_WHITE) {
654       loop_palette = pHeadPalette;
655       loop_palette2 = pHeadPalette;
656     }
657     else {
658       loop_palette = pHeadPalette;
659       loop_palette2 = NULL;
660       while(loop_palette != NULL)
661         if(strcmp(loop_palette->name, "Black") == 0) {
662           loop_palette2 = loop_palette;
663           loop_palette = NULL;
664         }
665         else
666           loop_palette = loop_palette->next;
667       loop_palette = pHeadPalette;
668       if(loop_palette2 == NULL)
669         loop_palette2 = pHeadPalette;
670     }
671   }
672   while( loop_palette != NULL)
673     {
674       if (pl_DB !=NULL)
675         {
676           name_str = (char *) XtMalloc(strlen("palettes.") + 
677                                        strlen(loop_palette->name) +
678                                        strlen(".desc") + 1);
679           
680           class_str = (char *) XtMalloc(strlen("Palettes.") + 
681                                         strlen(loop_palette->name) + 
682                                         strlen(".Desc") + 1);
683           strcpy(name_str, "palettes.");
684           strcpy(class_str, "Palettes.");
685           strcat(name_str, loop_palette->name);
686           strcat(class_str, loop_palette->name);
687           strcat(name_str, ".desc");
688           strcat(class_str, ".Desc");
689           
690           if (XrmGetResource (pl_DB, name_str, class_str, &str_type_return, &value_return))
691             {
692               loop_palette->desc = (char *)XtMalloc(strlen (value_return.addr) + 1 );
693               strcpy(loop_palette->desc, value_return.addr);
694             }
695           else
696             {
697               loop_palette->desc = (char *)XtMalloc(strlen (loop_palette->name) + 1 );
698               strcpy(loop_palette->desc, loop_palette->name);
699             }
700           XtFree(name_str);
701           XtFree(class_str);
702         }
703       else
704         {
705           loop_palette->desc = (char *)XtMalloc(strlen (loop_palette->name) + 1 );
706           strcpy(loop_palette->desc, loop_palette->name);
707         }
708       
709       XmListAddItem(list, CMPSTR(loop_palette->desc), loop_palette->item_position);
710       
711       /* if the item is the same as the default name provided by the
712          color Server, save it */
713       if(!save.restoreFlag || defaultName_restore == NULL) {
714         if (!(strcmp(loop_palette->name, defaultName)))
715           loop_palette2 = loop_palette;
716       }
717       else {
718         if (!(strcmp(loop_palette->name, defaultName_restore)))
719           loop_palette2 = loop_palette;
720       }
721       loop_palette = loop_palette->next;
722       loopcount++;
723       if((loopcount % 5 == 0) && startup)
724         {
725
726         return(False);}
727     }
728   
729   /*
730    **  Make the palette named by the color Server the selected palette, if the
731    **  palette returned by the color server doesn't match make it the head
732    **  palette.
733    */
734   pOldPalette = NULL;
735   
736   /* the default name is the name to be selected */
737   if (loop_palette2->name == NULL)
738     string = CMPSTR("");
739   else
740     string = CMPSTR(loop_palette2->desc);
741   string_table = &string;
742   
743   n=0;
744   XtSetArg (args[n], XmNselectedItemCount, 1);  n++;
745   XtSetArg (args[n], XmNselectedItems, string_table);  n++;
746   XtSetValues (list, args, n);
747   XmStringFree(string); 
748   
749   XmListSetPos(list, loop_palette2->item_position);
750   XtManageChild(list);
751   
752   pCurrentPalette = loop_palette2;
753   selected_position = pCurrentPalette->item_position;
754   
755   style.count++;
756   return(True);
757 }
758
759 /*
760  * Allocate new pixel values and update color palette buttons.
761  * This is needed for screens without dynamicColor.
762  */
763 static void allocNewColors(void)
764 {
765         int i, n;
766         Arg args[10];
767         static unsigned long   pixels[XmCO_MAX_NUM_COLORS*5];
768         static int       count = 0;
769
770         if(count)
771         {
772                 /* free the cells from last selection */
773                 XFreeColors(style.display, style.colormap, pixels, count, 0);
774                 count=0;
775         }
776
777         for (i=0; i<pCurrentPalette->num_of_colors; i++)
778         {
779                 n=0;
780                 if (XAllocColor(style.display, style.colormap,
781                         &(pCurrentPalette->color[i].fg)) == 0) break;
782                 pixels[count++] = pCurrentPalette->color[i].fg.pixel;
783
784                 if (XAllocColor(style.display, style.colormap,
785                         &(pCurrentPalette->color[i].bg)) == 0) break;
786                 pixels[count++] = pCurrentPalette->color[i].bg.pixel;
787                 XtSetArg (args[n], XmNbackground,
788                         pCurrentPalette->color[i].bg.pixel); n++;
789
790                 if (XAllocColor(style.display, style.colormap,
791                         &(pCurrentPalette->color[i].sc)) == 0)  break;
792                 pixels[count++] = pCurrentPalette->color[i].sc.pixel;
793                 XtSetArg (args[n], XmNarmColor,
794                         pCurrentPalette->color[i].sc.pixel); n++;
795
796                 if (UsePixmaps == FALSE)
797                 {
798                         if (XAllocColor(style.display, style.colormap,
799                                 &(pCurrentPalette->color[i].ts)) == 0) break;
800                         pixels[count++] = pCurrentPalette->color[i].ts.pixel;
801                         XtSetArg (args[n], XmNtopShadowColor,
802                                 pCurrentPalette->color[i].ts.pixel); n++;
803
804                         if (XAllocColor(style.display, style.colormap,
805                                 &(pCurrentPalette->color[i].bs)) == 0) break;
806                         pixels[count++] = pCurrentPalette->color[i].bs.pixel;
807                         XtSetArg (args[n], XmNbottomShadowColor,
808                                 pCurrentPalette->color[i].bs.pixel); n++;
809                 }
810                 else    /* create pixmaps for top/bottom shadow */
811                 {
812                         XmDestroyPixmap(style.screen, edit.pixmap25);
813                         XmDestroyPixmap(style.screen, edit.pixmap75);
814
815                         edit.pixmap25 = XmGetPixmap (style.screen,
816                                 "50_foreground",pCurrentPalette->color[i].bg.pixel,
817                                 WhitePixelOfScreen(style.screen));
818
819                         edit.pixmap75 = XmGetPixmap (style.screen,
820                                 "50_foreground",pCurrentPalette->color[i].bg.pixel,
821                                 BlackPixelOfScreen(style.screen));
822
823                         XtSetArg (args[n], XmNtopShadowPixmap, edit.pixmap25); n++;
824                         XtSetArg (args[n], XmNbottomShadowPixmap, edit.pixmap75); n++;
825                 }
826                 XtSetValues(colorButton[i], args, n);
827         }
828 }
829     
830 /*
831 **  This is the selection callback for the Scrolled list.
832 **  The routine finds the item the user selected and changes 
833 **  the already allocated pixels to the colors of the selected palette.
834 */
835 static void 
836 selectPaletteCB(
837         Widget w,
838         XtPointer client_data,
839         XtPointer call_data )
840 {
841     register int     n;
842     Arg              args[10];
843     XmListCallbackStruct *cb = (XmListCallbackStruct *)call_data;
844     palette         *tmp_palette;
845     XmString         string;
846     Pixel            white, black;
847         static Boolean   First = True;
848
849     white = WhitePixelOfScreen(style.screen);
850     black = BlackPixelOfScreen(style.screen);
851
852     if (((edit.DialogShell == NULL) || (!XtIsManaged(edit.DialogShell))) &&
853                                       selected_position != cb->item_position)
854     {
855         selected_position = cb->item_position;
856
857         tmp_palette = pHeadPalette;
858         while( tmp_palette->item_position != selected_position &&
859                                                   tmp_palette != NULL)
860              tmp_palette = tmp_palette->next;
861
862         if(tmp_palette->item_position == selected_position)
863         {
864             pOldPalette = pCurrentPalette;
865             pCurrentPalette = tmp_palette;
866
867             n=0;
868             string = CMPSTR(pCurrentPalette->desc);
869             XtSetArg (args[n], XmNtitleString, string); n++;
870             XtSetValues (colorDialog.buttonsTB, args, n);
871             XmStringFree(string);
872
873             if (style.dynamicColor)
874                 ReColorPalette();
875             else 
876             {     
877                if (TypeOfMonitor != XmCO_BLACK_WHITE)
878                {
879                              /* PUT DIALOG saying can't dynamically change */
880                  if(First)
881                  {
882                     InfoDialog(NEXT_SESSION, style.colorDialog, False);
883                     First = False;
884                  }
885                  allocNewColors();
886                }
887                else  /* XmCO_BLACK_WHITE */
888                {
889                   /* set color buttons for new palette - read only cells */
890                   /* primary=color[1] secondary=color[0] */
891                   if (strcmp(pCurrentPalette->name, WHITE_BLACK) == 0)
892                   {
893                       n=0;      /* secondary color white */
894                       XtSetArg (args[n], XmNforeground, black); n++;
895                       XtSetArg (args[n], XmNbackground, white); n++;
896                       XtSetArg (args[n], XmNarmColor, white); n++;
897                       XtSetValues(colorButton[0], args, n);
898                       pCurrentPalette->color[0].fg.pixel = black;
899                       pCurrentPalette->color[0].bg.pixel = white;
900                       pCurrentPalette->color[0].sc.pixel = white;
901                       pCurrentPalette->color[0].ts.pixel = black;
902                       pCurrentPalette->color[0].bs.pixel = black;
903
904                       n=0;      /* primary color black */
905                       XtSetArg (args[n], XmNforeground, white); n++;
906                       XtSetArg (args[n], XmNbackground, black); n++;
907                       XtSetArg (args[n], XmNarmColor, black); n++;
908                       XtSetValues(colorButton[1], args, n);
909                       pCurrentPalette->color[1].fg.pixel = white;
910                       pCurrentPalette->color[1].bg.pixel = black;
911                       pCurrentPalette->color[1].sc.pixel = black;
912                       pCurrentPalette->color[1].ts.pixel = white;
913                       pCurrentPalette->color[1].bs.pixel = white;
914                   }
915                   else if (strcmp(pCurrentPalette->name, BLACK_WHITE) == 0)
916                   {
917                       n=0;      /* secondary color black */
918                       XtSetArg (args[n], XmNforeground, white); n++;
919                       XtSetArg (args[n], XmNbackground, black); n++;
920                       XtSetArg (args[n], XmNarmColor, black); n++;
921                       XtSetValues(colorButton[0], args, n);
922                       pCurrentPalette->color[0].fg.pixel = white;
923                       pCurrentPalette->color[0].bg.pixel = black;
924                       pCurrentPalette->color[0].sc.pixel = black;
925                       pCurrentPalette->color[0].ts.pixel = white;
926                       pCurrentPalette->color[0].bs.pixel = white;
927
928                       n=0;      /* primary color white */
929                       XtSetArg (args[n], XmNforeground, black); n++;
930                       XtSetArg (args[n], XmNbackground, white); n++;
931                       XtSetArg (args[n], XmNarmColor, white); n++;
932                       XtSetValues(colorButton[1], args, n);
933                       pCurrentPalette->color[1].fg.pixel = black;
934                       pCurrentPalette->color[1].bg.pixel = white;
935                       pCurrentPalette->color[1].sc.pixel = white;
936                       pCurrentPalette->color[1].ts.pixel = black;
937                       pCurrentPalette->color[1].bs.pixel = black;
938                   }
939                   else if (strcmp(pCurrentPalette->name, BLACK_ONLY) == 0)
940                   {
941                       n=0;      /* primary and secondary color black */
942                       XtSetArg (args[n], XmNforeground, white); n++;
943                       XtSetArg (args[n], XmNbackground, black); n++;
944                       XtSetArg (args[n], XmNarmColor, black); n++;
945                       XtSetValues(colorButton[0], args, n);
946                       pCurrentPalette->color[0].fg.pixel = white;
947                       pCurrentPalette->color[0].bg.pixel = black;
948                       pCurrentPalette->color[0].sc.pixel = black;
949                       pCurrentPalette->color[0].ts.pixel = white;
950                       pCurrentPalette->color[0].bs.pixel = white;
951                       XtSetValues(colorButton[1], args, n);
952                       pCurrentPalette->color[1].fg.pixel = white;
953                       pCurrentPalette->color[1].bg.pixel = black;
954                       pCurrentPalette->color[1].sc.pixel = black;
955                       pCurrentPalette->color[1].ts.pixel = white;
956                       pCurrentPalette->color[1].bs.pixel = white;
957                   }
958                   else     /* WHITE_ONLY */
959                   {
960                       n=0;      /* primary and secondary color white */
961                       XtSetArg (args[n], XmNforeground, black); n++;
962                       XtSetArg (args[n], XmNbackground, white); n++;
963                       XtSetArg (args[n], XmNarmColor, white); n++;
964                       XtSetValues(colorButton[0], args, n);
965                       pCurrentPalette->color[0].fg.pixel = black;
966                       pCurrentPalette->color[0].bg.pixel = white;
967                       pCurrentPalette->color[0].sc.pixel = white;
968                       pCurrentPalette->color[0].ts.pixel = black;
969                       pCurrentPalette->color[0].bs.pixel = black;
970                       XtSetValues(colorButton[1], args, n);
971                       pCurrentPalette->color[1].fg.pixel = black;
972                       pCurrentPalette->color[1].bg.pixel = white;
973                       pCurrentPalette->color[1].sc.pixel = white;
974                       pCurrentPalette->color[1].ts.pixel = black;
975                       pCurrentPalette->color[1].bs.pixel = black;
976                   }
977               }                  
978            }
979         }
980     }
981 }
982
983
984 static void 
985 selectColorCB(
986         Widget w,
987         XtPointer client_data,
988         XtPointer call_data )
989 {
990     intptr_t         i;
991     int              n;
992     Arg              args[4];
993     ColorSet *color_set;
994     XmPushButtonCallbackStruct *cb = (XmPushButtonCallbackStruct *)call_data;
995
996     i = (intptr_t) client_data;
997
998     /* if click_count == 1 .. first button press, set time out */
999     if(cb->click_count == 1)
1000     {
1001        timeID = XtAppAddTimeOut(XtWidgetToApplicationContext(gParent), 
1002                                 (unsigned long) dclick_time, timeoutCB, 
1003                                 (XtPointer) i);
1004        return;
1005     }
1006
1007     /* else .. second button press, remove the time out */
1008     XtRemoveTimeOut(timeID);
1009
1010     if ((edit.DialogShell == NULL) || (!XtIsManaged(edit.DialogShell)))
1011     {
1012                 Pixel bg_pixel;
1013
1014         /* make the new selected button have a border color */
1015                 n=0;
1016                 XtSetArg(args[n],XmNbackground,&bg_pixel); n++;
1017                 XtGetValues(colorDialog.colorForm,args,n);
1018
1019                 n=0;
1020         XtSetArg (args[n], XmNborderColor,bg_pixel);
1021         n++;
1022         XtSetValues(colorButton[selected_button],args,n);
1023
1024         n=0;
1025         XtSetArg (args[n], XmNborderColor, BlackPixelOfScreen(style.screen));
1026         n++;
1027         XtSetValues(colorButton[i],args,n);
1028
1029         selected_button = i;
1030
1031         color_set = (ColorSet *) &pCurrentPalette->color[selected_button];
1032         ColorEditor(style.colorDialog,color_set);
1033
1034                 if(!style.dynamicColor) /* need to update pixels */
1035                         XtAddCallback(edit.DialogShell, XmNcallback, colorEditorCB, NULL);
1036     }
1037 }
1038
1039 /*
1040  * Color editor callback for screens without dynamicColor.
1041  */
1042 static void colorEditorCB(Widget w, XtPointer client_data, XtPointer call_data)
1043 {
1044         static Boolean first = True;
1045         DtDialogBoxCallbackStruct *cb = (DtDialogBoxCallbackStruct *) call_data;
1046
1047         /* show "next session" message if first edit */
1048     if(cb->button_position==OK_BUTTON && first){
1049                 InfoDialog(NEXT_SESSION, style.colorDialog, False);
1050                 first = False;
1051         }
1052         allocNewColors();
1053         XtRemoveCallback(edit.DialogShell, XmNcallback, colorEditorCB, NULL);
1054 }
1055
1056 /*
1057 **  This is the double click timeout callback.  If this routine is called
1058 **  then there was only a single click on a colorbutton
1059 */
1060 static void 
1061 timeoutCB(
1062         XtPointer client_data,
1063         XtIntervalId *id )
1064 {
1065     register int     n;
1066     intptr_t         i;
1067     Arg              args[2];
1068         Pixel   bg_pixel;
1069
1070     if (TypeOfMonitor == XmCO_BLACK_WHITE)
1071         return;
1072
1073     i = (intptr_t) client_data;
1074
1075     if ((edit.DialogShell == NULL) || (!XtIsManaged(edit.DialogShell)))
1076     {
1077         /* make the new selected button have a border color */
1078                 n=0;
1079                 XtSetArg(args[n],XmNbackground,&bg_pixel); n++;
1080                 XtGetValues(colorDialog.colorForm,args,n);
1081
1082         n=0;
1083         XtSetArg (args[n], XmNborderColor,bg_pixel);
1084         n++;
1085         XtSetValues(colorButton[selected_button],args,n);
1086
1087         n=0;
1088         XtSetArg (args[n], XmNborderColor, BlackPixelOfScreen(style.screen));
1089         n++;
1090         XtSetValues(colorButton[i],args,n);
1091         selected_button = i;
1092     }
1093 }
1094
1095 static void 
1096 addPaletteCB(
1097         Widget w,
1098         XtPointer client_data,
1099         XtPointer call_data )
1100 {
1101
1102     register int     n;
1103     Arg              args[10];
1104     XmString         string;
1105     XmString         string1;
1106
1107     if (addDialog == NULL)
1108     {
1109         n = 0;
1110
1111         XtSetArg(args[n], XmNokLabelString, CMPSTR((String) _DtOkString)); n++;
1112         XtSetArg(args[n], XmNcancelLabelString, CMPSTR((String) _DtCancelString)); n++;
1113         XtSetArg(args[n], XmNhelpLabelString, CMPSTR((String) _DtHelpString)); n++;
1114         string =  CMPSTR(((char *)GETMESSAGE(14, 10, "New palette name:")));
1115         XtSetArg(args[n], XmNselectionLabelString, string); n++;
1116         string1 =  CMPSTR("");
1117         XtSetArg(args[n], XmNtextString, string1); n++;
1118         XtSetArg(args[n], XmNborderWidth, 3); n++;
1119         XtSetArg(args[n], XmNautoUnmanage, False); n++;
1120         addDialog = XmCreatePromptDialog(style.colorDialog,"AddDialog",
1121                                                                       args, n);
1122         XmStringFree(string);
1123         XmStringFree(string1);
1124
1125         XtAddCallback(addDialog, XmNokCallback, addOkCB, 
1126                                  (XtPointer) NULL);
1127         XtAddCallback(addDialog, XmNcancelCallback, addCancelCB, 
1128                                  (XtPointer) NULL);
1129         XtAddCallback(addDialog, XmNhelpCallback,
1130             (XtCallbackProc)HelpRequestCB, (XtPointer)HELP_ADD_PALETTE_DIALOG);
1131
1132         n = 0;
1133         XtSetArg (args[n], XmNmwmInputMode,
1134                         MWM_INPUT_PRIMARY_APPLICATION_MODAL); n++;
1135         XtSetArg (args[n], XmNuseAsyncGeometry, True); n++;
1136         XtSetArg (args[n], XmNtitle, ((char *)GETMESSAGE(14, 11, "Add Palette"))); n++;
1137         XtSetArg (args[n], XmNmwmFunctions, DIALOG_MWM_FUNC); n++;
1138         XtSetValues (XtParent (addDialog), args, n);
1139     }
1140     n=0;
1141     string =  CMPSTR("");
1142     XtSetArg(args[n], XmNtextString, string); n++;
1143     XtSetValues(addDialog, args, n);
1144     XmStringFree(string);
1145
1146     XtManageChild(addDialog);
1147
1148 }
1149
1150 static void 
1151 addCancelCB(
1152         Widget w,
1153         XtPointer client_data,
1154         XtPointer call_data )
1155 {
1156         XtUnmanageChild(addDialog);
1157 }
1158
1159 static void 
1160 addOkCB(
1161         Widget w,
1162         XtPointer client_data,
1163         XtPointer call_data )
1164 {
1165   register int     n, i;
1166   Arg              args[6];
1167   XmString         string;
1168   char             *name, *filename, *tmpstr;
1169   palette         *tmpPalette, *newPalette;
1170   int              count;
1171   int              ii, length;
1172   int              len;
1173
1174   /* Get the text from the promp dialog */
1175   name = XmTextFieldGetString( XmSelectionBoxGetChild(addDialog, XmDIALOG_TEXT));
1176   
1177   /* see if the user typed in a valid palette name */
1178   if(!ValidName(name)) {
1179     ErrDialog(((char *)GETMESSAGE(14, 12, "The palette name cannot contain\nthese characters:\n\n * : ( ) [ ] { } < > ! | \" / \\")), 
1180               style.colorDialog); 
1181     XtFree(name);
1182     return;
1183   }
1184   
1185   /* check the number of characters in name in a locale independent way */
1186   for ( ii = 0, count = 0; name[ii] != '\0'; ii += length ) {
1187     length = mblen( &(name[ii]), MB_CUR_MAX );
1188     if ( length <=0 )
1189       break;
1190     count++;
1191   }
1192   
1193   /* make sure the length of name is ok, short file names can only be 11 chars */
1194   if(count > 10 && !style.longfilename) {
1195     ErrDialog(((char *)GETMESSAGE(14, 13, "The palette name must be\n10 characters or less.\n")),style.colorDialog);
1196     XtFree(name);
1197     return;
1198   }
1199   
1200   /* Unmanage the promptDialog */
1201   XtUnmanageChild(addDialog);
1202   
1203   /* first search through palette descriptions and make sure the name to */
1204   /* add is not already in the list and go to the end of the palette list */
1205   for(tmpPalette = pHeadPalette; tmpPalette->next != NULL;
1206       tmpPalette = tmpPalette->next)
1207     {
1208       if((strcmp(tmpPalette->desc, name) == 0)) {
1209         SameName(w, tmpPalette, name);
1210         XtFree(name);
1211         return;
1212       }
1213     }
1214   
1215   /* Check the last palette */
1216   if((strcmp(tmpPalette->desc, name) == 0)) {
1217     SameName(w, tmpPalette, name);
1218     XtFree(name);
1219     return;
1220   }
1221   
1222   /* allocate space for a new palette */
1223   newPalette = (palette *)XtMalloc(sizeof(palette) + 1 );
1224   
1225   /* set the previous last palatte to this new one, it is now the last one*/
1226   tmpPalette->next = newPalette;
1227   newPalette->next = NULL;
1228   
1229   /* malloc space for the new palette desc */
1230   newPalette->desc = (char *)XtMalloc(strlen(name) + 1);
1231   for(i = 0; i < strlen(name); i++)
1232     newPalette->desc[i] = name[i];
1233   newPalette->desc[i] = '\0';
1234   
1235   /* malloc space for the new palette name directory */
1236   newPalette->directory = (char *)XtMalloc(strlen(style.home) +
1237                                            strlen(DT_PAL_DIR) + 1);
1238   strcpy(newPalette->directory, style.home);
1239   strcat(newPalette->directory, DT_PAL_DIR);
1240   
1241   /* makeup a new name for the palette */
1242   tmpstr = (char *)XtMalloc(strlen(style.home) + strlen(DT_PAL_DIR) + 
1243                             strlen("dtXXXXXX") + 1);
1244   strcpy(tmpstr, newPalette->directory);
1245   len = strlen(tmpstr);
1246   strcat(tmpstr, "dtXXXXXX");
1247   mktemp(tmpstr);
1248
1249   newPalette->name = (char *) XtMalloc(15 * sizeof(char));
1250   strcpy(newPalette->name, tmpstr + len);
1251   XtFree(tmpstr);
1252
1253   /* the new palette is the next palette .. increase NumOfPalettes by one */
1254   newPalette->item_position = NumOfPalettes + 1;
1255   NumOfPalettes++;
1256   
1257   /* set all the new palette's color parameters to the current palette */
1258   newPalette->num_of_colors = pCurrentPalette->num_of_colors;
1259   for(i = 0; i < XmCO_MAX_NUM_COLORS; i++)
1260     {
1261       newPalette->color[i].fg.pixel = pCurrentPalette->color[i].fg.pixel;
1262       newPalette->color[i].fg.red = pCurrentPalette->color[i].fg.red;
1263       newPalette->color[i].fg.green = pCurrentPalette->color[i].fg.green;
1264       newPalette->color[i].fg.blue = pCurrentPalette->color[i].fg.blue;
1265       newPalette->color[i].fg.flags = pCurrentPalette->color[i].fg.flags;
1266       
1267       newPalette->color[i].bg.pixel = pCurrentPalette->color[i].bg.pixel;
1268       newPalette->color[i].bg.red = pCurrentPalette->color[i].bg.red;
1269       newPalette->color[i].bg.green = pCurrentPalette->color[i].bg.green;
1270       newPalette->color[i].bg.blue = pCurrentPalette->color[i].bg.blue;
1271       newPalette->color[i].bg.flags = pCurrentPalette->color[i].bg.flags;
1272       
1273       newPalette->color[i].ts.pixel = pCurrentPalette->color[i].ts.pixel;
1274       newPalette->color[i].ts.red = pCurrentPalette->color[i].ts.red;
1275       newPalette->color[i].ts.green = pCurrentPalette->color[i].ts.green;
1276       newPalette->color[i].ts.blue = pCurrentPalette->color[i].ts.blue;
1277       newPalette->color[i].ts.flags = pCurrentPalette->color[i].ts.flags;
1278       
1279       newPalette->color[i].bs.pixel = pCurrentPalette->color[i].bs.pixel;
1280       newPalette->color[i].bs.red = pCurrentPalette->color[i].bs.red;
1281       newPalette->color[i].bs.green = pCurrentPalette->color[i].bs.green;
1282       newPalette->color[i].bs.blue = pCurrentPalette->color[i].bs.blue;
1283       newPalette->color[i].bs.flags = pCurrentPalette->color[i].bs.flags;
1284       
1285       newPalette->color[i].sc.pixel = pCurrentPalette->color[i].sc.pixel;
1286       newPalette->color[i].sc.red = pCurrentPalette->color[i].sc.red;
1287       newPalette->color[i].sc.green = pCurrentPalette->color[i].sc.green;
1288       newPalette->color[i].sc.blue = pCurrentPalette->color[i].sc.blue;
1289       newPalette->color[i].sc.flags = pCurrentPalette->color[i].sc.flags;
1290     }
1291   
1292   /* Write out the palette */
1293   if ((WriteOutPalette(newPalette->name)) == -1)
1294     {
1295       XtFree(name);
1296       
1297       /*  remove palette from list */
1298       tmpPalette->next = NULL;
1299       XtFree ((char *)newPalette);
1300       
1301       return;
1302     }
1303   else
1304     WriteOutDesc(newPalette);
1305       
1306     
1307   /* add the name to the scrolled window list and select it */
1308   AddName(newPalette);
1309   
1310   /* now check to see if there is a ~filename .. if there is delete it */
1311   /* use the $HOME environment varible then constuct the full file name */
1312   filename = (char *)XtMalloc(strlen(style.home) + strlen(DT_PAL_DIR) +
1313                               strlen(newPalette->name) + strlen(PALETTE_SUFFIX) + 2);
1314   
1315   /* create the full path name plus file name */
1316   strcpy(filename, style.home);
1317   strcat(filename, DT_PAL_DIR);
1318   strcat(filename, "~");
1319   strcat(filename, newPalette->name);
1320   strcat(filename, PALETTE_SUFFIX);
1321   
1322   unlink(filename);
1323   
1324   XtFree(filename);
1325   XtFree(name);
1326   
1327   /* Go write out the palette */
1328   pCurrentPalette = newPalette;
1329   
1330 }
1331
1332 static void 
1333 setDlgOkCB(
1334         Widget w,
1335         XtPointer client_data,
1336         XtPointer call_data )
1337 {
1338    palette *tmpPalette = (palette *)client_data;
1339    int i;
1340
1341   /* free the directory */
1342    XtFree(tmpPalette->directory);
1343
1344   /* put the new (users) directory there */
1345    tmpPalette->directory = (char *)XtMalloc(strlen(style.home) +
1346                                           strlen(DT_PAL_DIR) + 1);
1347    strcpy(tmpPalette->directory, style.home);
1348    strcat(tmpPalette->directory, DT_PAL_DIR);
1349
1350    for(i = 0; i < XmCO_MAX_NUM_COLORS; i++)
1351    {
1352        tmpPalette->color[i].fg.pixel = pCurrentPalette->color[i].fg.pixel;
1353        tmpPalette->color[i].fg.red = pCurrentPalette->color[i].fg.red;
1354        tmpPalette->color[i].fg.green = pCurrentPalette->color[i].fg.green;
1355        tmpPalette->color[i].fg.blue = pCurrentPalette->color[i].fg.blue;
1356        tmpPalette->color[i].fg.flags = pCurrentPalette->color[i].fg.flags;
1357
1358        tmpPalette->color[i].bg.pixel = pCurrentPalette->color[i].bg.pixel;
1359        tmpPalette->color[i].bg.red = pCurrentPalette->color[i].bg.red;
1360        tmpPalette->color[i].bg.green = pCurrentPalette->color[i].bg.green;
1361        tmpPalette->color[i].bg.blue = pCurrentPalette->color[i].bg.blue;
1362        tmpPalette->color[i].bg.flags = pCurrentPalette->color[i].bg.flags;
1363
1364        tmpPalette->color[i].ts.pixel = pCurrentPalette->color[i].ts.pixel;
1365        tmpPalette->color[i].ts.red = pCurrentPalette->color[i].ts.red;
1366        tmpPalette->color[i].ts.green = pCurrentPalette->color[i].ts.green;
1367        tmpPalette->color[i].ts.blue = pCurrentPalette->color[i].ts.blue;
1368        tmpPalette->color[i].ts.flags = pCurrentPalette->color[i].ts.flags;
1369
1370        tmpPalette->color[i].bs.pixel = pCurrentPalette->color[i].bs.pixel;
1371        tmpPalette->color[i].bs.red = pCurrentPalette->color[i].bs.red;
1372        tmpPalette->color[i].bs.green = pCurrentPalette->color[i].bs.green;
1373        tmpPalette->color[i].bs.blue = pCurrentPalette->color[i].bs.blue;
1374        tmpPalette->color[i].bs.flags = pCurrentPalette->color[i].bs.flags;
1375
1376        tmpPalette->color[i].sc.pixel = pCurrentPalette->color[i].sc.pixel;
1377        tmpPalette->color[i].sc.red = pCurrentPalette->color[i].sc.red;
1378        tmpPalette->color[i].sc.green = pCurrentPalette->color[i].sc.green;
1379        tmpPalette->color[i].sc.blue = pCurrentPalette->color[i].sc.blue;
1380        tmpPalette->color[i].sc.flags = pCurrentPalette->color[i].sc.flags;
1381    }
1382
1383   /* Write out the palette */
1384    if ((WriteOutPalette(tmpPalette->name)) == -1)
1385       return;
1386    else
1387      WriteOutDesc(tmpPalette);
1388
1389    pCurrentPalette = tmpPalette;
1390
1391   /* select item in list as if user had selected it */
1392    XmListSelectPos (paletteList, tmpPalette->item_position, TRUE);
1393    XmListSetBottomPos(paletteList, tmpPalette->item_position);
1394    selected_position = tmpPalette->item_position;
1395
1396 }
1397
1398 static void 
1399 modifyColorCB(
1400         Widget w,
1401         XtPointer client_data,
1402         XtPointer call_data )
1403 {
1404     ColorSet *color_set;
1405
1406     if(TypeOfMonitor == XmCO_BLACK_WHITE)
1407        return;
1408         
1409     color_set = (ColorSet *) &pCurrentPalette->color[selected_button];
1410     ColorEditor(style.colorDialog,color_set);
1411
1412         if(!style.dynamicColor) /* need to update pixels */
1413                 XtAddCallback(edit.DialogShell, XmNcallback, colorEditorCB, NULL);
1414 }
1415
1416
1417 /*
1418 **  dialogBoxCB
1419 **      Process callback from the Ok, Cancel and Help pushButtons in the 
1420 **      DialogBox.
1421 */
1422 static void 
1423 dialogBoxCB(
1424         Widget w,
1425         XtPointer client_data,
1426         XtPointer call_data )
1427 {
1428   palette *tmp_palette;
1429   Bool match = FALSE;
1430   DtDialogBoxCallbackStruct *cb = (DtDialogBoxCallbackStruct *) call_data;
1431   
1432   switch (cb->button_position)
1433     {
1434     case OK_BUTTON:
1435       if ((edit.DialogShell != NULL) && (XtIsManaged(edit.DialogShell)))
1436         XtUnmanageChild(edit.DialogShell);
1437       
1438       XtUnmanageChild(style.colorDialog);
1439       UpdateDefaultPalette();
1440       break;
1441       
1442     case CANCEL_BUTTON:
1443       if ((edit.DialogShell != NULL) && (XtIsManaged(edit.DialogShell)))
1444         XtUnmanageChild(edit.DialogShell);
1445       
1446       if ((addDialog != NULL) && (XtIsManaged(addDialog)))
1447         XtCallCallbacks(addDialog, XmNcancelCallback, (XtPointer)NULL);
1448       
1449       if ((deleteDialog != NULL) && (XtIsManaged(deleteDialog)))
1450         XtCallCallbacks(deleteDialog, XmNcancelCallback, (XtPointer)NULL);
1451       
1452       if ((colorUseDialog != NULL) && (XtIsManaged(colorUseDialog)))
1453         XtCallCallbacks(colorUseDialog, XmNcallback, (XtPointer)NULL);
1454       
1455       XtUnmanageChild(style.colorDialog);
1456       
1457       tmp_palette = pHeadPalette;
1458       for(tmp_palette = pHeadPalette; tmp_palette != NULL; tmp_palette = tmp_palette->next)
1459         if(!(strcmp(tmp_palette->name, defaultName)))
1460           {
1461             match = TRUE;
1462             break;
1463           }
1464       
1465       if(match == FALSE)  /* the default palette is no longer valid */
1466         UpdateDefaultPalette();
1467       else 
1468         RestoreOrgPalette();
1469       
1470       break;
1471       
1472     case HELP_BUTTON:
1473       XtCallCallbacks(style.colorDialog, XmNhelpCallback, (XtPointer)NULL);
1474       break;
1475       
1476     default:
1477       break;
1478     }
1479 }
1480
1481
1482 static void 
1483 AddName(
1484         palette *newPalette )
1485 {
1486     XmString         string;
1487
1488 /*
1489 **  Add the palette name to the list
1490 */
1491     string = CMPSTR(newPalette->desc);
1492     XmListAddItem(paletteList, string, newPalette->item_position);
1493     XmListSelectPos(paletteList, newPalette->item_position, TRUE);
1494     XmListSetBottomPos(paletteList, newPalette->item_position);
1495     selected_position = newPalette->item_position;
1496     XSync(style.display, 0);
1497     XmStringFree(string);
1498 }
1499
1500 static void 
1501 deletePaletteCB(
1502         Widget w,
1503         XtPointer client_data,
1504         XtPointer call_data )
1505 {
1506     register int     n;
1507     Arg              args[10];
1508     char            *tmpStr;
1509     palette         *tmp_palette;
1510     char            *string;
1511     char            *class_str;
1512     char            *str_type_return;
1513     XrmValue         value_return;
1514
1515     tmp_palette = pHeadPalette;
1516
1517     while( tmp_palette->item_position != selected_position &&
1518                                               tmp_palette != NULL)
1519          tmp_palette = tmp_palette->next;
1520     
1521     if (deleteDialog == NULL)
1522     {
1523         n=0;
1524         XtSetArg(args[n], XmNokLabelString, CMPSTR((String) _DtOkString)); n++;
1525         XtSetArg(args[n], XmNcancelLabelString, CMPSTR((String) _DtCancelString)); n++;
1526         XtSetArg(args[n], XmNhelpLabelString, CMPSTR((String) _DtHelpString)); n++;
1527         XtSetArg(args[n], XmNdialogType, XmDIALOG_INFORMATION);             n++;
1528         XtSetArg(args[n], XmNborderWidth, 3);                               n++;
1529         XtSetArg(args[n], XmNdefaultPosition, False);                      n++;
1530         deleteDialog = XmCreateQuestionDialog(style.colorDialog,
1531                                               "deleteDialog", args, n);
1532         XtAddCallback(deleteDialog, XmNmapCallback, CenterMsgCB,
1533                       style.colorDialog);
1534         XtAddCallback(deleteDialog, XmNcancelCallback, deleteCancelCB, NULL);
1535         XtAddCallback(deleteDialog, XmNokCallback, deleteOkCB, 
1536                       (XtPointer)(paletteList));
1537         XtAddCallback(deleteDialog, XmNhelpCallback,
1538                       (XtCallbackProc)HelpRequestCB, 
1539                       (XtPointer)HELP_DELETE_PALETTE_WARNING_DIALOG);
1540
1541
1542         n = 0;
1543         XtSetArg (args[n], XmNmwmInputMode,
1544                         MWM_INPUT_PRIMARY_APPLICATION_MODAL); n++;
1545         XtSetArg (args[n], XmNuseAsyncGeometry, True); n++;
1546         XtSetArg (args[n], XmNtitle, ((char *)GETMESSAGE(14, 16, "Delete Palette"))); n++;
1547         XtSetArg (args[n], XmNmwmFunctions, DIALOG_MWM_FUNC); n++;
1548         XtSetValues (XtParent (deleteDialog), args, n);
1549
1550     }
1551
1552     n = 0;
1553     tmpStr = XtMalloc(strlen(STR2) + strlen(tmp_palette->desc) + 1);
1554     sprintf(tmpStr, STR2, tmp_palette->desc);
1555     XtSetArg(args[n], XmNmessageString, CMPSTR(tmpStr)); n++;
1556     XtSetValues(deleteDialog, args, n);
1557     XtFree (tmpStr);
1558
1559     XtManageChild(deleteDialog);
1560 }
1561
1562 static void 
1563 deleteOkCB(
1564         Widget w,
1565         XtPointer client_data,
1566         XtPointer call_data )
1567 {
1568
1569     XtUnmanageChild(deleteDialog);
1570
1571     if(NumOfPalettes == 1)
1572     {
1573        InfoDialog(CANT_DELETE, style.colorDialog, False);
1574     } 
1575     else
1576     {
1577        if (RemovePalette() == True)
1578            DeletePaletteFromLinkList((Widget)client_data);
1579     }
1580 }
1581
1582 static void 
1583 deleteCancelCB(
1584         Widget w,
1585         XtPointer client_data,
1586         XtPointer call_data )
1587 {
1588         XtUnmanageChild(deleteDialog);
1589 }
1590
1591 static void 
1592 resourcesCB(
1593         Widget w,
1594         XtPointer client_data,
1595         XtPointer call_data )
1596 {
1597
1598     register int     n;
1599     Arg              args[12];
1600     XmString         button_string[NUM_LABELS]; 
1601     XmString         string;
1602     Widget           parent = (Widget) client_data;
1603     Widget           colorUseTB;
1604     Widget           form;
1605     Widget           widget_list[10];
1606     int              count=0;
1607     Widget           pictLabel;
1608     Widget           colorUseRC;
1609
1610     if (colorUseDialog == NULL)
1611     {
1612         n = 0;
1613
1614         /* Set up DialogBox button labels. */
1615         button_string[0] = CMPSTR((String) _DtOkString);
1616         button_string[1] = CMPSTR((String) _DtCancelString);
1617         button_string[2] = CMPSTR((String) _DtHelpString);
1618
1619         XtSetArg (args[n], XmNchildType, XmWORK_AREA);  n++;
1620         XtSetArg (args[n], XmNbuttonCount, NUM_LABELS);  n++;
1621         XtSetArg (args[n], XmNbuttonLabelStrings, button_string);  n++;
1622         XtSetArg (args[n], XmNdefaultPosition, False);  n++;
1623         colorUseDialog = __DtCreateDialogBoxDialog(parent, "colorUseDialog", 
1624                           args, n);
1625         XtAddCallback(colorUseDialog, XmNcallback, colorUseExitCB, NULL);
1626         XtAddCallback(colorUseDialog, XmNmapCallback, _DtmapCB_colorUse, NULL);
1627         XtAddCallback(colorUseDialog, XmNhelpCallback,
1628             (XtCallbackProc)HelpRequestCB, (XtPointer)HELP_COLOR_USE_DIALOG);
1629
1630         XmStringFree(button_string[0]);
1631         XmStringFree(button_string[1]);
1632         XmStringFree(button_string[2]);
1633
1634         widget_list[0] = _DtDialogBoxGetButton(colorUseDialog,2);
1635         n=0;
1636         XtSetArg(args[n], XmNautoUnmanage, False); n++;
1637         XtSetArg(args[n], XmNcancelButton, widget_list[0]); n++;
1638         XtSetValues (colorUseDialog, args, n);
1639
1640         n = 0;
1641         XtSetArg (args[n], XmNuseAsyncGeometry, True); n++;
1642         XtSetArg (args[n], XmNtitle, 
1643             ((char *)GETMESSAGE(14, 39, "Number Of Colors To Use:"))); n++;
1644         XtSetArg (args[n], XmNmwmFunctions, DIALOG_MWM_FUNC); n++;
1645         XtSetValues (XtParent(colorUseDialog), args, n);
1646
1647         n = 0;
1648         XtSetArg(args[n], XmNhorizontalSpacing, style.horizontalSpacing); n++;
1649         XtSetArg(args[n], XmNverticalSpacing, style.verticalSpacing); n++;
1650         XtSetArg(args[n], XmNallowOverlap, False); n++;
1651         XtSetArg(args[n], XmNchildType, XmWORK_AREA);  n++;
1652         form = XmCreateForm(colorUseDialog, "colorUseForm", args, n);
1653         XtManageChild(form);
1654
1655         n = 0;
1656         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
1657         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
1658         XtSetArg (args[n], XmNrightAttachment, XmATTACH_NONE);  n++;
1659         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_NONE);  n++;
1660         XtSetArg (args[n], XmNbehavior, XmICON_LABEL); n++;
1661         XtSetArg (args[n], XmNshadowThickness, 0); n++;  
1662         XtSetArg (args[n], XmNstring, NULL); n++;  
1663         XtSetArg (args[n], XmNpixmapForeground, style.primBSCol); n++;
1664         XtSetArg (args[n], XmNpixmapBackground, style.primTSCol); n++;
1665         XtSetArg (args[n], XmNimageName, COLOR_ICON); n++;  
1666         XtSetArg (args[n], XmNtraversalOn, False); n++;  
1667         pictLabel = _DtCreateIcon(form, "pictLabel", args, n);
1668         XtManageChild(pictLabel);
1669
1670         n = 0;
1671         XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET);  n++;
1672         XtSetArg (args[n], XmNtopWidget, pictLabel);  n++;
1673         XtSetArg (args[n], XmNtopOffset, style.verticalSpacing);  n++;
1674         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
1675         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
1676         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM);  n++;
1677         colorUseTB 
1678             = XmCreateFrame(form, "colorUseTB", args, n);
1679         XtManageChild(colorUseTB);
1680
1681         /* create a rowColumn for ColorUse selections */
1682         n = 0;
1683         colorUseRC = XmCreateRadioBox(colorUseTB, "colorUseRC", args, n);
1684         XtManageChild(colorUseRC);
1685
1686         n = 0;
1687         string = CMPSTR(((char *)GETMESSAGE(14, 41, "More Colors for Desktop")));
1688         XtSetArg(args[n], XmNlabelString, string); n++;
1689         widget_list[count++] = colorDialog.highColorTG 
1690             =  XmCreateToggleButtonGadget(colorUseRC,"highColorTG", args, n);
1691         XtAddCallback(colorDialog.highColorTG, XmNvalueChangedCallback, 
1692                         colorUseCB, (XtPointer)XmCO_HIGH_COLOR);  
1693         XmStringFree(string);
1694
1695         n = 0;
1696         string = CMPSTR(((char *)GETMESSAGE(14, 42, "More Colors for Applications")));
1697         XtSetArg(args[n], XmNlabelString, string); n++;
1698         widget_list[count++] = colorDialog.mediumColorTG 
1699             = XmCreateToggleButtonGadget(colorUseRC,"mediumColorTG", args, n);
1700         XmStringFree(string);
1701         XtAddCallback(colorDialog.mediumColorTG, XmNvalueChangedCallback, 
1702                         colorUseCB, (XtPointer)XmCO_MEDIUM_COLOR);  
1703
1704         n = 0;
1705         string = CMPSTR(((char *)GETMESSAGE(14, 43, "Most Colors for Applications")));
1706         XtSetArg(args[n], XmNlabelString, string); n++;
1707         widget_list[count++] = colorDialog.lowColorTG 
1708             = XmCreateToggleButtonGadget(colorUseRC,"lowColorTG", args, n);
1709         XmStringFree(string);
1710         XtAddCallback(colorDialog.lowColorTG, XmNvalueChangedCallback, 
1711                         colorUseCB, (XtPointer)XmCO_LOW_COLOR);  
1712
1713         n = 0;
1714         string = CMPSTR(((char *)GETMESSAGE(14, 34, "Black and White")));
1715         XtSetArg(args[n], XmNlabelString, string); n++;
1716         widget_list[count++] = colorDialog.blackWhiteTG 
1717             = XmCreateToggleButtonGadget(colorUseRC,"blackWhiteTG", args, n);
1718         XmStringFree(string);
1719         XtAddCallback(colorDialog.blackWhiteTG, XmNvalueChangedCallback, 
1720                         colorUseCB, (XtPointer)XmCO_BLACK_WHITE);  
1721
1722         n = 0;
1723         string = CMPSTR(((char *)GETMESSAGE(14, 35, "Default")));
1724         XtSetArg(args[n], XmNlabelString, string); n++;
1725         widget_list[count++] = colorDialog.defaultTG 
1726             = XmCreateToggleButtonGadget(colorUseRC,"defaultTG", args, n);
1727         XmStringFree(string);
1728         XtAddCallback(colorDialog.defaultTG, XmNvalueChangedCallback, 
1729                         colorUseCB, (XtPointer)DEFAULT_COLOR);  
1730
1731         XtManageChildren(widget_list,count);
1732         putDialog (XtParent(style.colorDialog), colorUseDialog); 
1733     }
1734
1735     XtManageChild(colorUseDialog);
1736 }
1737
1738 /*
1739 **  colorUseCB
1740 **      Process new ColorUse selection
1741 */
1742 static void 
1743 colorUseCB(
1744         Widget w,
1745         XtPointer client_data,
1746         XtPointer call_data )
1747 {
1748     Arg              args[4];
1749     XmToggleButtonCallbackStruct *cb = 
1750             (XmToggleButtonCallbackStruct *)call_data;
1751
1752     colorDialog.currentColorUse = (int) (intptr_t) client_data;
1753     switch (colorDialog.currentColorUse)
1754     {
1755         case XmCO_HIGH_COLOR:
1756             colorDialog.currentColorUseStr = HIGH_COLOR_STR;
1757             break;
1758
1759         case XmCO_MEDIUM_COLOR:
1760             colorDialog.currentColorUseStr = MEDIUM_COLOR_STR;
1761             break;
1762
1763         case XmCO_LOW_COLOR:
1764             colorDialog.currentColorUseStr = LOW_COLOR_STR;
1765             break;
1766
1767         case XmCO_BLACK_WHITE:
1768             colorDialog.currentColorUseStr = B_W_STR;
1769             break;
1770
1771         case DEFAULT_COLOR:
1772             colorDialog.currentColorUseStr = DEFAULT_COLOR_STR;
1773             break;
1774     }
1775
1776
1777 }
1778
1779
1780 /*
1781 **  colorUseExitCB
1782 **      Process callback from the Ok, Cancel and Help pushButtons in the 
1783 **      Configure DT Colors DialogBox.
1784 */
1785 static void 
1786 colorUseExitCB(
1787         Widget w,
1788         XtPointer client_data,
1789         XtPointer call_data )
1790 {
1791
1792     char        colorUseRes[64];
1793     DtDialogBoxCallbackStruct *cb = (DtDialogBoxCallbackStruct *) call_data;
1794
1795     switch (cb->button_position)
1796     {
1797       case HELP_BUTTON:
1798           XtCallCallbacks(colorUseDialog, XmNhelpCallback, (XtPointer)NULL);
1799           break;
1800
1801       case OK_BUTTON:
1802
1803          XtUnmanageChild(colorUseDialog);
1804
1805          if (colorDialog.origColorUse != colorDialog.currentColorUse)
1806          {
1807              InfoDialog(COLORUSE_WHEN, style.colorDialog, False); 
1808
1809              /* create the ColorUse resource spec for xrdb */
1810              /* remove ColorUse specification from database for DEFAULT_COLOR */
1811
1812              sprintf(colorUseRes, "*%d*ColorUse: %s\n", 
1813                  style.screenNum,colorDialog.currentColorUseStr);
1814
1815              switch (colorDialog.currentColorUse)
1816              {
1817                 case XmCO_MEDIUM_COLOR:
1818                     sprintf(colorUseRes+strlen(colorUseRes), 
1819                              "*HelpColorUse: GRAY_SCALE\n");
1820
1821                     break;
1822
1823                 case XmCO_LOW_COLOR:
1824                 case XmCO_BLACK_WHITE:
1825                     sprintf(colorUseRes+strlen(colorUseRes), 
1826                              "*HelpColorUse: B_W\n");
1827
1828                     break;
1829
1830                 case XmCO_HIGH_COLOR:
1831                 case DEFAULT_COLOR:
1832                 default:
1833                     sprintf(colorUseRes+strlen(colorUseRes), 
1834                              "*HelpColorUse: COLOR\n");
1835                     break;
1836              }
1837
1838              _DtAddToResource(style.display, colorUseRes);
1839
1840              colorDialog.origColorUse = colorDialog.currentColorUse;
1841          }
1842
1843          break;
1844
1845       case CANCEL_BUTTON:
1846       default:
1847
1848          XtUnmanageChild(colorUseDialog);
1849          
1850          switch (colorDialog.origColorUse)
1851          {
1852            case XmCO_HIGH_COLOR:
1853              XmToggleButtonGadgetSetState(colorDialog.highColorTG, True, True);
1854              break;
1855
1856            case XmCO_MEDIUM_COLOR:
1857              XmToggleButtonGadgetSetState(colorDialog.mediumColorTG, True, True);
1858              break;
1859
1860             case XmCO_LOW_COLOR:
1861              XmToggleButtonGadgetSetState(colorDialog.lowColorTG, True, True);
1862              break;
1863
1864             case XmCO_BLACK_WHITE:
1865              XmToggleButtonGadgetSetState(colorDialog.blackWhiteTG, True, True);
1866              break;
1867
1868             case DEFAULT_COLOR:
1869              XmToggleButtonGadgetSetState(colorDialog.defaultTG, True, True);
1870              break;
1871           }
1872           break;
1873
1874     }
1875 }
1876
1877
1878 static void 
1879 activateCBexitColor(
1880         Widget w,
1881         XtPointer client_data,
1882         XtPointer call_data )
1883 {
1884   DtDialogBoxCallbackStruct CancelBut;
1885
1886   if(style.colorDialog != NULL && XtIsManaged(style.colorDialog)) {
1887      CancelBut.button_position = CANCEL_BUTTON;
1888      XtCallCallbacks(style.colorDialog, XmNcallback, &CancelBut);
1889   }
1890 }
1891
1892 static void 
1893 _DtmapCB(
1894         Widget w,
1895         XtPointer client_data,
1896
1897         XtPointer call_data )
1898 {
1899
1900     DtWsmRemoveWorkspaceFunctions(style.display, XtWindow(XtParent(w)));
1901
1902     if (!save.restoreFlag)
1903         putDialog ((Widget)client_data, XtParent(w));
1904
1905     XtRemoveCallback(style.colorDialog, XmNmapCallback, _DtmapCB, NULL);
1906 }
1907
1908 static void 
1909 _DtmapCB_colorUse(
1910         Widget w,
1911         XtPointer client_data,
1912
1913         XtPointer call_data )
1914 {
1915
1916     char *str_type_return;
1917     XrmValue value_return;
1918     XrmValue    cvt_value;
1919     XrmDatabase db;
1920     Boolean status;
1921     char *string;
1922     char instanceString[24], nameString[24];
1923
1924     DtWsmRemoveWorkspaceFunctions(style.display, XtWindow(XtParent(w)));
1925
1926     db = XtDatabase(style.display);
1927
1928     /* Get ColorUse value */
1929     sprintf (instanceString, "dtsession*%d*colorUse",style.screenNum);
1930     sprintf (nameString, "Dtsession*%d*ColorUse",style.screenNum);
1931
1932     if (status = XrmGetResource (db, instanceString,
1933                                  nameString,
1934                                  &str_type_return, &value_return))
1935     {
1936         /* make local copy of string */
1937         string = (char *) XtMalloc( value_return.size );
1938         strcpy (string, value_return.addr);
1939
1940         if (strcmp(string, HIGH_COLOR_STR) == 0)
1941         {
1942             XmToggleButtonGadgetSetState (colorDialog.highColorTG, True, True); 
1943             colorDialog.origColorUse = XmCO_HIGH_COLOR;
1944         }
1945         else if (strcmp(string, MEDIUM_COLOR_STR) == 0)
1946         {
1947             XmToggleButtonGadgetSetState (colorDialog.mediumColorTG, True, True); 
1948             colorDialog.origColorUse = XmCO_MEDIUM_COLOR;
1949         }
1950         else if (strcmp(string, LOW_COLOR_STR) == 0)
1951         {
1952             XmToggleButtonGadgetSetState (colorDialog.lowColorTG, True, True); 
1953             colorDialog.origColorUse = XmCO_LOW_COLOR;
1954         }
1955         else if (strcmp(string, B_W_STR) == 0)
1956         {
1957             XmToggleButtonGadgetSetState (colorDialog.blackWhiteTG, True, True); 
1958             colorDialog.origColorUse = XmCO_BLACK_WHITE;
1959         }
1960         else 
1961         {
1962             XmToggleButtonGadgetSetState (colorDialog.defaultTG, True, True); 
1963             colorDialog.origColorUse = DEFAULT_COLOR;
1964         }
1965
1966         XtFree (string);
1967     }
1968     else /* ColorUse not specified */
1969     {
1970         XmToggleButtonGadgetSetState (colorDialog.defaultTG, True, True); 
1971         colorDialog.origColorUse = DEFAULT_COLOR;
1972     } 
1973
1974     XtRemoveCallback(colorUseDialog, XmNmapCallback, _DtmapCB_colorUse, NULL);
1975 }
1976
1977 /************************************************************************
1978  *
1979  *  DeletePaletteFromLinkList - routine used to delete a palette from
1980  *       the link list of palettes.  The palette which is at the current
1981  *       selected_position is the palette that is going to be deleted.
1982  *       Special things have to happen if the selected palette is at the
1983  *       head of the list.
1984  *
1985  ************************************************************************/ 
1986 void
1987 DeletePaletteFromLinkList(
1988         Widget list )
1989 {
1990     register int n;
1991     Arg args[2];
1992     int i;
1993     XmString        string; 
1994     palette        *tmp_palette, *tmp2_palette;
1995     palette        *selected_palette;
1996
1997
1998     selected_palette = pHeadPalette;
1999     while( selected_palette->item_position != selected_position &&
2000                                               selected_palette != NULL)
2001          selected_palette = selected_palette->next;
2002
2003     XmListDeletePos (list, selected_palette->item_position);
2004
2005     /* delete item from palette list structure */
2006
2007     /* If the palette is at the head .. remove the head and the next
2008          palette becomes the new selected palette */
2009     if (selected_palette->item_position == 1)
2010     {
2011         pHeadPalette = selected_palette->next;
2012         tmp_palette = pHeadPalette;
2013         tmp_palette->item_position--;
2014        /* new current palette */
2015         pCurrentPalette = tmp_palette;
2016     }
2017     else  /* find the palette just above the palette to be deleted .. it
2018              will become the new selected palette */
2019     {
2020         tmp_palette = pHeadPalette;
2021         for (i=1; i < selected_palette->item_position-1; i++)
2022             tmp_palette = tmp_palette->next;
2023
2024         tmp_palette->next = selected_palette->next;
2025
2026        /* what is CurrentPalette now? prev or next item?
2027            special case empty list or NULL entry */
2028        if (tmp_palette->next != NULL)
2029            pCurrentPalette = tmp_palette->next;
2030        else
2031            pCurrentPalette = tmp_palette;
2032     }
2033
2034     /* decrement item_positions values in remaining palette entries */
2035     tmp2_palette = tmp_palette;
2036     while ((tmp2_palette = tmp2_palette->next) != NULL)
2037     {
2038         tmp2_palette->item_position--;
2039     }
2040
2041    /* go copy the pixel numbers to the new palette */
2042     CopyPixel(selected_palette->color, pCurrentPalette->color,
2043                                       selected_palette->num_of_colors);
2044
2045     /* select item in list as if user had selected it */
2046     XmListSelectPos (list, tmp_palette->item_position, TRUE);
2047     /* Need to check to see if the first palette is being deleted if it is
2048        need to change colors and update title box */
2049     if(selected_position == tmp_palette->item_position) {
2050        pOldPalette = selected_palette;
2051
2052        n=0;
2053        string = CMPSTR(pCurrentPalette->desc);
2054        XtSetArg (args[n], XmNtitleString, string); n++;
2055        XtSetValues (colorDialog.buttonsTB, args, n);
2056        XmStringFree(string);
2057
2058        ReColorPalette();
2059     }
2060
2061     XmListSetBottomPos(paletteList, tmp_palette->item_position);
2062     selected_position = tmp_palette->item_position;
2063
2064     NumOfPalettes--;
2065
2066    /* deallocate the palette structure */
2067     XtFree(selected_palette->name);
2068     XtFree(selected_palette->desc);
2069     XtFree(selected_palette->directory);
2070     XtFree((char *)selected_palette);
2071 }
2072
2073 void
2074 CopyPixel(
2075         ColorSet srcPixels[XmCO_MAX_NUM_COLORS],
2076         ColorSet dstPixels[XmCO_MAX_NUM_COLORS],
2077         int numOfColors )
2078 {
2079    int i;
2080
2081    for(i=0; i < numOfColors; i++)
2082    {
2083       dstPixels[i].bg.pixel = srcPixels[i].bg.pixel;
2084       dstPixels[i].fg.pixel = srcPixels[i].fg.pixel;
2085       dstPixels[i].ts.pixel = srcPixels[i].ts.pixel;
2086       dstPixels[i].bs.pixel = srcPixels[i].bs.pixel;
2087       dstPixels[i].sc.pixel = srcPixels[i].sc.pixel;
2088    }
2089 }
2090
2091 void 
2092 SaveOrgPalette( void )
2093 {
2094    int i;
2095    palette  *tmp_palette, *tmp2_palette;
2096
2097    if(save.restoreFlag && defaultName_restore[0] != 0) {
2098       tmp_palette = pHeadPalette;
2099       while(tmp_palette->next != NULL )
2100          if(strcmp(tmp_palette->name, defaultName))
2101          {
2102             tmp_palette = tmp_palette->next;
2103          }
2104          else
2105          {
2106              break;
2107          }
2108       if(!strcmp(tmp_palette->name, defaultName)) {
2109          tmp2_palette = pCurrentPalette;
2110          pCurrentPalette = tmp_palette;
2111       }
2112       else
2113          tmp_palette = NULL;
2114    }
2115
2116    OrgPalette.item_position = pCurrentPalette->item_position;
2117    OrgPalette.num_of_colors = pCurrentPalette->num_of_colors;
2118    for(i = 0; i < XmCO_MAX_NUM_COLORS; i++)
2119    {
2120        OrgPalette.primary = pCurrentPalette->primary;
2121        OrgPalette.secondary = pCurrentPalette->secondary;
2122        OrgPalette.active = pCurrentPalette->active;
2123        OrgPalette.inactive = pCurrentPalette->inactive;
2124
2125        if(save.restoreFlag && defaultName_restore[0] != 0)
2126           OrgPalette.color[i].bg.pixel = tmp2_palette->color[i].bg.pixel;
2127        else
2128           OrgPalette.color[i].bg.pixel = pCurrentPalette->color[i].bg.pixel;
2129        OrgPalette.color[i].bg.red = pCurrentPalette->color[i].bg.red;
2130        OrgPalette.color[i].bg.green = pCurrentPalette->color[i].bg.green;
2131        OrgPalette.color[i].bg.blue = pCurrentPalette->color[i].bg.blue;
2132
2133        if(save.restoreFlag && defaultName_restore[0] != 0)
2134           OrgPalette.color[i].fg.pixel = tmp2_palette->color[i].fg.pixel;
2135        else
2136           OrgPalette.color[i].fg.pixel = pCurrentPalette->color[i].fg.pixel;
2137        OrgPalette.color[i].fg.red = pCurrentPalette->color[i].fg.red;
2138        OrgPalette.color[i].fg.green = pCurrentPalette->color[i].fg.green;
2139        OrgPalette.color[i].fg.blue = pCurrentPalette->color[i].fg.blue;
2140
2141        if(save.restoreFlag && defaultName_restore[0] != 0)
2142           OrgPalette.color[i].ts.pixel = tmp2_palette->color[i].ts.pixel;
2143        else
2144           OrgPalette.color[i].ts.pixel = pCurrentPalette->color[i].ts.pixel;
2145        OrgPalette.color[i].ts.red = pCurrentPalette->color[i].ts.red;
2146        OrgPalette.color[i].ts.green = pCurrentPalette->color[i].ts.green;
2147        OrgPalette.color[i].ts.blue = pCurrentPalette->color[i].ts.blue;
2148
2149        if(save.restoreFlag && defaultName_restore[0] != 0)
2150           OrgPalette.color[i].bs.pixel = tmp2_palette->color[i].bs.pixel;
2151        else
2152           OrgPalette.color[i].bs.pixel = pCurrentPalette->color[i].bs.pixel;
2153        OrgPalette.color[i].bs.red = pCurrentPalette->color[i].bs.red;
2154        OrgPalette.color[i].bs.green = pCurrentPalette->color[i].bs.green;
2155        OrgPalette.color[i].bs.blue = pCurrentPalette->color[i].bs.blue;
2156
2157        if(save.restoreFlag && defaultName_restore[0] != 0)
2158           OrgPalette.color[i].sc.pixel = tmp2_palette->color[i].sc.pixel;
2159        else
2160           OrgPalette.color[i].sc.pixel = pCurrentPalette->color[i].sc.pixel;
2161        OrgPalette.color[i].sc.red = pCurrentPalette->color[i].sc.red;
2162        OrgPalette.color[i].sc.green = pCurrentPalette->color[i].sc.green;
2163        OrgPalette.color[i].sc.blue = pCurrentPalette->color[i].sc.blue;
2164    }
2165
2166    if(save.restoreFlag && defaultName_restore[0] != 0) 
2167       if(tmp_palette != NULL)
2168          pCurrentPalette = tmp2_palette;
2169
2170 }
2171
2172 void
2173 RestoreOrgPalette( void )
2174 {
2175    int i;
2176     palette  *tmp_palette;
2177    int         j=0;
2178    XColor      colors[XmCO_MAX_NUM_COLORS * 5];
2179
2180    tmp_palette = pHeadPalette;
2181    while ( tmp_palette != NULL &&
2182            tmp_palette->item_position != OrgPalette.item_position )
2183       tmp_palette = tmp_palette->next;
2184
2185    if ( tmp_palette != NULL &&
2186         tmp_palette->item_position == OrgPalette.item_position)
2187    {
2188       pCurrentPalette = tmp_palette;
2189       OrgPalette.num_of_colors = pCurrentPalette->num_of_colors;
2190       for(i = 0; i < XmCO_MAX_NUM_COLORS; i++)
2191       {
2192           pCurrentPalette->primary = OrgPalette.primary;
2193           pCurrentPalette->secondary = OrgPalette.secondary;
2194           pCurrentPalette->inactive = OrgPalette.inactive;
2195           pCurrentPalette->active = OrgPalette.active;
2196
2197           pCurrentPalette->color[i].bg.pixel = OrgPalette.color[i].bg.pixel;
2198           pCurrentPalette->color[i].bg.red = OrgPalette.color[i].bg.red;
2199           pCurrentPalette->color[i].bg.green = OrgPalette.color[i].bg.green;
2200           pCurrentPalette->color[i].bg.blue = OrgPalette.color[i].bg.blue;
2201           if(i < OrgPalette.num_of_colors && TypeOfMonitor != XmCO_BLACK_WHITE)
2202              colors[j++] =  pCurrentPalette->color[i].bg;
2203
2204           pCurrentPalette->color[i].sc.pixel = OrgPalette.color[i].sc.pixel;
2205           pCurrentPalette->color[i].sc.red = OrgPalette.color[i].sc.red;
2206           pCurrentPalette->color[i].sc.green = OrgPalette.color[i].sc.green;
2207           pCurrentPalette->color[i].sc.blue = OrgPalette.color[i].sc.blue;
2208           if(i < OrgPalette.num_of_colors && TypeOfMonitor != XmCO_BLACK_WHITE)
2209              colors[j++] =  pCurrentPalette->color[i].sc;
2210
2211           pCurrentPalette->color[i].fg.pixel = OrgPalette.color[i].fg.pixel;
2212           pCurrentPalette->color[i].fg.red = OrgPalette.color[i].fg.red;
2213           pCurrentPalette->color[i].fg.green = OrgPalette.color[i].fg.green;
2214           pCurrentPalette->color[i].fg.blue = OrgPalette.color[i].fg.blue;
2215           if(i < OrgPalette.num_of_colors && TypeOfMonitor != XmCO_BLACK_WHITE)
2216              if(FgColor == DYNAMIC)
2217                 colors[j++] =  pCurrentPalette->color[i].fg;
2218
2219           pCurrentPalette->color[i].ts.pixel = OrgPalette.color[i].ts.pixel;
2220           pCurrentPalette->color[i].ts.red = OrgPalette.color[i].ts.red;
2221           pCurrentPalette->color[i].ts.green = OrgPalette.color[i].ts.green;
2222           pCurrentPalette->color[i].ts.blue = OrgPalette.color[i].ts.blue;
2223           if(i < OrgPalette.num_of_colors && TypeOfMonitor != XmCO_BLACK_WHITE)
2224              if(UsePixmaps == FALSE)
2225                 colors[j++] =  pCurrentPalette->color[i].ts;
2226
2227           pCurrentPalette->color[i].bs.pixel = OrgPalette.color[i].bs.pixel;
2228           pCurrentPalette->color[i].bs.red = OrgPalette.color[i].bs.red;
2229           pCurrentPalette->color[i].bs.green = OrgPalette.color[i].bs.green;
2230           pCurrentPalette->color[i].bs.blue = OrgPalette.color[i].bs.blue;
2231           if(i < OrgPalette.num_of_colors && TypeOfMonitor != XmCO_BLACK_WHITE)
2232              if(UsePixmaps == FALSE)
2233                 colors[j++] =  pCurrentPalette->color[i].bs;
2234
2235       }
2236
2237       if (style.dynamicColor)
2238           XStoreColors (style.display, style.colormap, colors, j);
2239
2240       XmListSelectPos (paletteList, OrgPalette.item_position, TRUE);
2241       XmListSetBottomPos(paletteList, OrgPalette.item_position);
2242
2243    }
2244 }
2245
2246 void 
2247      UpdateDefaultPalette( void )
2248 {
2249   int      i;
2250    char     temp[XmCO_MAX_NUM_COLORS][60];
2251    char     xrdb_string[100];
2252    XColor   saved_color[2];
2253    
2254    for(i = 0; i < XmCO_MAX_NUM_COLORS; i++)
2255      {
2256      /* put the colors of the palette in the form #RRRRGGGGBBBB */
2257      if(TypeOfMonitor == XmCO_LOW_COLOR && (i == 0 || i == 1))
2258         sprintf(temp[i],"#%04x%04x%04x\n", saved_color[i].red,
2259                  saved_color[i].green,
2260                  saved_color[i].blue);
2261       else if(TypeOfMonitor == XmCO_LOW_COLOR && i == 2)
2262         sprintf(temp[i],"#%04x%04x%04x\n",pCurrentPalette->color[1].bg.red,
2263                   pCurrentPalette->color[1].bg.green,
2264                   pCurrentPalette->color[1].bg.blue);
2265       else if(TypeOfMonitor == XmCO_LOW_COLOR && i == 3)
2266         sprintf(temp[i],"#%04x%04x%04x\n",pCurrentPalette->color[0].bg.red,
2267                   pCurrentPalette->color[0].bg.green,
2268                   pCurrentPalette->color[0].bg.blue);
2269       else
2270         sprintf(temp[i],"#%04x%04x%04x\n",pCurrentPalette->color[i].bg.red,
2271                   pCurrentPalette->color[i].bg.green,
2272                   pCurrentPalette->color[i].bg.blue);
2273    }
2274    
2275    /* update the resource manager property with the palette resource */
2276    if (TypeOfMonitor == XmCO_BLACK_WHITE)
2277       {
2278       sprintf(xrdb_string, "*%d*MonochromePalette: %s%s\n",
2279                 style.screenNum, pCurrentPalette->name, PALETTE_SUFFIX);
2280       }
2281     else
2282       {
2283       sprintf(xrdb_string, "*%d*ColorPalette: %s%s\n",
2284                 style.screenNum, pCurrentPalette->name, PALETTE_SUFFIX);
2285       }
2286    _DtAddToResource(style.display, xrdb_string);
2287    
2288    /* update the defaultName */
2289    XtFree(defaultName);
2290  defaultName = (char *)XtMalloc(strlen(pCurrentPalette->name)+1);
2291    strcpy(defaultName, pCurrentPalette->name);
2292    
2293
2294    /* update Xrdb for non Motif1.1 clients */
2295    if (style.xrdb.writeXrdbColors)
2296      {
2297 #ifdef sun
2298        OWsyncColorResources(style.display, TypeOfMonitor, 
2299                 pCurrentPalette->color);
2300 #else
2301        int chipnum = TypeOfMonitor == XmCO_HIGH_COLOR ? 4 : 1;
2302        sprintf(xrdb_string, 
2303                "*background: #%04X%04X%04X\n*foreground: #%04X%04X%04X\n",
2304                pCurrentPalette->color[chipnum].bg.red, 
2305                pCurrentPalette->color[chipnum].bg.green, 
2306                pCurrentPalette->color[chipnum].bg.blue,
2307                pCurrentPalette->color[chipnum].fg.red, 
2308                pCurrentPalette->color[chipnum].fg.green, 
2309                pCurrentPalette->color[chipnum].fg.blue);
2310        _DtAddToResource(style.display, xrdb_string);
2311 #endif
2312    }
2313 }
2314  
2315
2316
2317 void 
2318 show_selection(
2319         Widget w,
2320         XtPointer client_data,
2321         Atom *selection,
2322         Atom *type,
2323         XtPointer value,
2324         unsigned long *length,
2325         int *format )
2326 {
2327     int      dynamic_color;
2328
2329     style.colorSrv = True;
2330     if(value != NULL)
2331     {
2332        if((intptr_t) client_data == GET_TYPE_MONITOR)
2333        {
2334           sscanf ((char *)value, "%x_%x_%x_%x", (unsigned int *) &(TypeOfMonitor),
2335                                      (unsigned int *) &(UsePixmaps), (unsigned int *) &(FgColor), (unsigned int *) &dynamic_color);
2336           if(dynamic_color == FALSE)
2337              style.dynamicColor = False;
2338           else
2339              style.dynamicColor = True;
2340        }
2341
2342        WaitSelection = FALSE;
2343        free(value);
2344     }
2345     else /* no response from Color Server - it must not be there */
2346     {
2347        style.colorSrv = False;
2348        WaitSelection = FALSE;
2349     }
2350 }
2351
2352 /************************************************************************
2353  * restoreColor()
2354  *
2355  * restore any state information saved with saveBackdrop.
2356  * This is called from restoreSession with the application
2357  * shell and the special xrm database retrieved for restore.
2358  ************************************************************************/
2359 void 
2360 restoreColor(
2361         Widget shell,
2362         XrmDatabase db )
2363 {
2364     XrmName xrm_name[5];
2365     XrmRepresentation rep_type;
2366     XrmValue value;
2367     palette *tmp_palette;
2368
2369     /*"paletteDlg" is the resource name of the dialog shell we are saving for.*/
2370     xrm_name [0] = XrmStringToQuark (PALETTEDLG);
2371     xrm_name [2] = 0;
2372
2373     /* get x position */
2374     xrm_name [1] = XrmStringToQuark ("x");
2375     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)){
2376       XtSetArg (save.posArgs[save.poscnt], XmNx, atoi((char *)value.addr)); save.poscnt++;
2377     }
2378
2379     /* get y position */
2380     xrm_name [1] = XrmStringToQuark ("y");
2381     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)){
2382       XtSetArg (save.posArgs[save.poscnt], XmNy, atoi((char *)value.addr)); save.poscnt++;
2383     }
2384
2385     /* get selected palette */
2386     xrm_name [1] = XrmStringToQuark ("selected_palette");
2387     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)){
2388        strcpy(defaultName_restore, value.addr);
2389     }
2390     else
2391        defaultName_restore[0] = 0;
2392
2393     /* get selected button */
2394     xrm_name [1] = XrmStringToQuark ("selected_button");
2395     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)){
2396       selected_button = atoi((char *)value.addr);
2397     }
2398
2399    /* need to have some variables initialized before creating the
2400         Color's dialog ... */
2401     InitializeAtoms();
2402     CheckMonitor(shell);
2403     GetDefaultPal(shell);
2404
2405     xrm_name [1] = XrmStringToQuark ("ismapped");
2406     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
2407     /* Are we supposed to be mapped? */
2408     if (strcmp(value.addr, "True") == 0) {
2409       save.restoreFlag = True;
2410       Customize(shell);
2411     }
2412 }
2413
2414
2415 /************************************************************************
2416  * saveColor()
2417  *
2418  * This routine will write out to the passed file descriptor any state
2419  * information this dialog needs.  It is called from saveSessionCB with the
2420  * file already opened.
2421  * All information is saved in xrm format.  There is no restriction
2422  * on what can be saved.  It doesn't have to be defined or be part of any
2423  * widget or Xt definition.  Just name and save it here and recover it in
2424  * restoreBackdrop.  The suggested minimum is whether you are mapped, and your
2425  * location.
2426  ************************************************************************/
2427 void 
2428 saveColor(
2429         int fd )
2430 {
2431     Position x,y;
2432     char *bufr = style.tmpBigStr;     /* size=[1024], make bigger if needed */
2433     XmVendorShellExtObject  vendorExt;
2434     XmWidgetExtData         extData;
2435
2436     if (style.colorDialog != NULL) 
2437     {
2438         if (XtIsManaged(style.colorDialog))
2439           sprintf(bufr, "*paletteDlg.ismapped: True\n");
2440         else
2441           sprintf(bufr, "*paletteDlg.ismapped: False\n");
2442
2443         /* Get and write out the geometry info for our Window */
2444         x = XtX(XtParent(style.colorDialog));
2445         y = XtY(XtParent(style.colorDialog));
2446
2447         /* Modify x & y to take into account window mgr frames
2448          * This is pretty bogus, but I don't know a better way to do it.
2449          */
2450         extData = _XmGetWidgetExtData(style.shell, XmSHELL_EXTENSION);
2451         vendorExt = (XmVendorShellExtObject)extData->widget;
2452         x -= vendorExt->vendor.xOffset;
2453         y -= vendorExt->vendor.yOffset;
2454
2455         sprintf(bufr, "%s*paletteDlg.x: %d\n", bufr, x);
2456         sprintf(bufr, "%s*paletteDlg.y: %d\n", bufr, y);
2457         sprintf(bufr, "%s*paletteDlg.selected_palette: %s\n", bufr, 
2458                 pCurrentPalette->name);
2459         sprintf(bufr, "%s*paletteDlg.selected_button: %d\n", bufr, 
2460                 selected_button);
2461         if(-1 == write (fd, bufr, strlen(bufr))) {
2462                 perror(strerror(errno));
2463         }
2464     }
2465 }
2466
2467 /**************************************************************************
2468  *
2469  * SameName - procedure used by the Add palette .. if the palette desc the 
2470  *            user selects is the same name as a palette already in the
2471  *            linked list this procedure gets called.  It set up a
2472  *            Warning dialog asking the user if they really want to add
2473  *            a palette with the same name as an existing palette.
2474  *
2475  **************************************************************************/
2476 void
2477 SameName(
2478         Widget w,
2479         palette *tmpPalette,
2480         char *name )
2481 {
2482    char    *tmpStr;
2483    int n=0;
2484    Arg args[10];
2485
2486    if (colorDialog.dlg == NULL) {
2487      tmpStr = (char *)XtMalloc(strlen(STR1) + strlen(name) + 1);
2488      sprintf(tmpStr, STR1, name);
2489      XtSetArg(args[n], XmNmessageString, CMPSTR(tmpStr)); n++;
2490      XtSetArg(args[n], XmNokLabelString, CMPSTR((String) _DtOkString)); n++;
2491      XtSetArg(args[n], XmNcancelLabelString, CMPSTR((String) _DtCancelString)); n++;
2492      XtSetArg(args[n], XmNhelpLabelString, CMPSTR((String) _DtHelpString)); n++;
2493      XtSetArg(args[n], XmNmwmFunctions, DIALOG_MWM_FUNC ); n++;
2494      XtSetArg(args[n], XmNdialogTitle, CMPSTR(((char *)GETMESSAGE(14, 21, "Warning")))); n++;
2495      colorDialog.dlg = XmCreateWarningDialog(style.colorDialog, "QNotice", args, n);
2496      XtAddCallback(colorDialog.dlg, XmNokCallback, setDlgOkCB, (XtPointer)tmpPalette);
2497      XtAddCallback(colorDialog.dlg, XmNhelpCallback,
2498               (XtCallbackProc)HelpRequestCB, (XtPointer)HELP_ADD_PALETTE_WARNING_DIALOG);
2499
2500       n=0;
2501       XtSetArg (args[n], XmNmwmInputMode,
2502                         MWM_INPUT_PRIMARY_APPLICATION_MODAL); n++;
2503       XtSetValues (XtParent(colorDialog.dlg), args, n);
2504       XtFree(tmpStr);
2505    }
2506    XtManageChild(colorDialog.dlg);
2507 }
2508
2509 /****************************************************************************
2510  *
2511  * ValidName - procedure which checks to make sure the name being passed
2512  *             in is a valid filename.  Weeds out many of the non 
2513  *             alphabit characters.
2514  *
2515  ***************************************************************************/
2516 static Boolean 
2517 ValidName(
2518         char *name )
2519 {
2520    int i;
2521    Boolean valid = True;
2522 #ifdef NLS16
2523    int chlen;
2524 #endif  /* NLS16 */
2525
2526    if (!name || strlen(name) == 0)
2527    {
2528       valid = False;
2529    }
2530
2531 #ifdef NLS16
2532    chlen = mblen (&name[0], MB_CUR_MAX);
2533    for (i = 0; (i < strlen(name)) && chlen && valid; i += chlen)
2534    {
2535       chlen = mblen (&name[i], MB_CUR_MAX);
2536       if ((chlen == 1) &&
2537           ((name[i] == '*') ||
2538            (name[i] == '"') ||
2539            (name[i] == '\\') ||
2540            (name[i] == '[') ||
2541            (name[i] == ']') ||
2542            (name[i] == '{') ||
2543            (name[i] == '}') ||
2544            (name[i] == '>') ||
2545            (name[i] == '<') ||
2546            (name[i] == '(') ||
2547            (name[i] == ')') ||
2548            (name[i] == '!') ||
2549            (name[i] == '|') ||
2550            (name[i] == ':') ||
2551            (name[i] == '/'))) 
2552       {
2553            valid = False;
2554       }
2555    }
2556 #else /* NLS16 */
2557    for (i = 0; (i < strlen(name)) && valid; i++)
2558    {
2559       if (strchr ("|!(){}[]<>*:\"\\", (int) name[i]))
2560       {
2561           valid = False;
2562       }
2563    }
2564 #endif /* NLS16 */
2565    return (valid);
2566 }
2567
2568 void 
2569 InitializeAtoms( void )
2570 {
2571     char             cust_str[24];
2572
2573     sprintf(cust_str,"%s%d", XmSCUSTOMIZE_DATA, style.screenNum);
2574     XA_CUSTOMIZE = XInternAtom(style.display, cust_str, FALSE);
2575     XA_TYPE_MONITOR = XInternAtom(style.display, TYPE_OF_MONITOR, FALSE);
2576     XA_WM_DELETE_WINDOW = XInternAtom(style.display, "WM_DELETE_WINDOW", False);
2577 }
2578
2579 void 
2580 GetDefaultPal(
2581         Widget shell )
2582 {
2583     char *str_type_return;
2584     XrmValue value_return;
2585     XrmDatabase db;
2586     Boolean status;
2587     char *p;
2588     char *string;
2589     char instanceName[30], instanceClass[30];
2590
2591     /* get the current default palette from the Reource Manager Property */
2592
2593     db = XtDatabase(style.display);
2594
2595     if (TypeOfMonitor == XmCO_BLACK_WHITE)   
2596     {
2597         sprintf(instanceName,"dtsession.%d.monochromePalette", style.screenNum);
2598         sprintf(instanceClass,"Dtsession.%d.MonochromePalette", style.screenNum);
2599     }
2600     else
2601     {
2602         sprintf(instanceName,"dtsession.%d.colorPalette", style.screenNum);
2603         sprintf(instanceClass,"Dtsession.%d.ColorPalette", style.screenNum);
2604     }
2605
2606     if (status = XrmGetResource (db, instanceName, instanceClass,
2607                                      &str_type_return, &value_return))
2608     {
2609         /* copy string to defaultName */
2610         defaultName = (char *) XtMalloc( value_return.size );
2611         strcpy (defaultName, value_return.addr);
2612         p = strstr (defaultName, PALETTE_SUFFIX);
2613         if (p) *p = '\0';
2614     }
2615     else
2616     {
2617         /* set defaultName to default palette */
2618         defaultName = (char *) XtMalloc( strlen(DEFAULT_PALETTE));
2619         strcpy (defaultName, DEFAULT_PALETTE);
2620     }
2621 }
2622
2623 void 
2624 CreateDialogBoxD(
2625         Widget parent )
2626 {
2627     XmString         button_string[NUM_LABELS]; 
2628     Arg              args[2];
2629     int              n;
2630     Widget           w;
2631
2632    if(style.count > 0)
2633       return;
2634
2635     /* Get the default value of multiclick */
2636     dclick_time = XtGetMultiClickTime(style.display);
2637
2638     gParent = parent;
2639     if(!XtIsRealized(parent))
2640        XtRealizeWidget(parent);
2641
2642    /* Set up DialogBox button labels. */
2643     button_string[0] = CMPSTR((String) _DtOkString);
2644     button_string[1] = CMPSTR((String) _DtCancelString);
2645     button_string[2] = CMPSTR((String) _DtHelpString);
2646
2647    /* saveRestore
2648     * Note that save.poscnt has been initialized elsewhere.  
2649     * save.posArgs may contain information from restoreColor().*/
2650
2651     XtSetArg (save.posArgs[save.poscnt], XmNchildType, XmWORK_AREA);  save.poscnt++;
2652     XtSetArg (save.posArgs[save.poscnt], XmNbuttonCount, NUM_LABELS);  save.poscnt++;
2653     XtSetArg (save.posArgs[save.poscnt], XmNbuttonLabelStrings, button_string);  save.poscnt++;
2654     XtSetArg (save.posArgs[save.poscnt], XmNdefaultPosition, False); save.poscnt++;
2655     style.colorDialog = __DtCreateDialogBoxDialog(parent,PALETTEDLG, save.posArgs, save.poscnt);
2656     XtAddCallback(style.colorDialog, XmNcallback, dialogBoxCB, NULL);
2657     XtAddCallback(style.colorDialog, XmNmapCallback, _DtmapCB, parent);
2658     XtAddCallback(style.colorDialog, XmNhelpCallback,
2659             (XtCallbackProc)HelpRequestCB, (XtPointer)HELP_COLOR_DIALOG);
2660
2661     XmStringFree(button_string[0]);
2662     XmStringFree(button_string[1]);
2663     XmStringFree(button_string[2]);
2664
2665     w = _DtDialogBoxGetButton(style.colorDialog,2);
2666     n=0;
2667     XtSetArg(args[n], XmNautoUnmanage, False); n++;
2668     XtSetArg(args[n], XmNcancelButton, w); n++;
2669     XtSetValues (style.colorDialog, args, n);
2670
2671     style.count++;
2672 }
2673
2674 void 
2675 AddToDialogBox( void )
2676 {
2677     register int     n;
2678     Arg              args[3];
2679
2680    if(style.count > 1)
2681       return;
2682
2683     n = 0;
2684     XtSetArg (args[n], XmNtitle, ((char *)GETMESSAGE(14, 30, "Style Manager - Color"))); n++;
2685     XtSetArg (args[n], XmNuseAsyncGeometry, True); n++;
2686     XtSetArg (args[n], XmNmwmFunctions, DIALOG_MWM_FUNC); n++;
2687     XtSetValues (XtParent(style.colorDialog), args, n);
2688     
2689     /* Add save session property to the main window */
2690     XmAddWMProtocolCallback(XtParent(style.colorDialog), 
2691                            XA_WM_DELETE_WINDOW, activateCBexitColor, NULL);
2692
2693 /*
2694 **  Create a main form for color dialog
2695 */
2696     n = 0;
2697     XtSetArg(args[n], XmNhorizontalSpacing, style.horizontalSpacing); n++;
2698     XtSetArg(args[n], XmNverticalSpacing, style.verticalSpacing); n++;
2699     colorDialog.colorForm = XmCreateForm(style.colorDialog, "colorForm", args, n);
2700     XtManageChild(colorDialog.colorForm);
2701     
2702     style.count++;
2703 }
2704
2705 void 
2706 CreateTopColor1( void )
2707 {
2708     register int     n;
2709     Arg              args[6];
2710     XmString         string; 
2711
2712     if(style.count > 2)
2713        return;
2714 /*
2715 **  titlebox as child of the main form
2716 */
2717     n = 0;
2718     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
2719     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
2720     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
2721     XtSetArg (args[n], XmNmarginWidth, 0);  n++;
2722     XtSetArg (args[n], XmNmarginHeight, 0);  n++;
2723     string = CMPSTR(((char *)GETMESSAGE(14, 5, "Palettes")));
2724     XtSetArg (args[n], XmNtitleString, string); n++;
2725     colorDialog.paletteTB = _DtCreateTitleBox(colorDialog.colorForm, "paletteTB", args, n);
2726     XtManageChild(colorDialog.paletteTB);
2727     XmStringFree(string);
2728
2729 /* 
2730 **  Create a form inside palette titlebox 
2731 */
2732     n = 0;
2733     XtSetArg(args[n], XmNhorizontalSpacing, style.horizontalSpacing); n++;
2734     XtSetArg(args[n], XmNverticalSpacing, style.verticalSpacing); n++;
2735     colorDialog.palettesForm = XmCreateForm(colorDialog.paletteTB, "palettesForm", args, n);
2736     XtManageChild(colorDialog.palettesForm);
2737
2738     style.count++;
2739 }
2740
2741 void 
2742 CreateTopColor2( void )
2743 {
2744     register int     n;
2745     Arg              args[8];
2746
2747     if(style.count > 3)
2748        return;
2749 /*
2750 **  Create a scrolled list widget.  This widget will contain the list of
2751 **  palettes currently loaded (by ReadPalettes) in the customizer.
2752 */
2753     n=0;
2754     XtSetArg (args[n], XmNselectionPolicy, XmBROWSE_SELECT); n++;
2755     XtSetArg (args[n], XmNautomaticSelection, True); n++;
2756     XtSetArg (args[n], XmNvisibleItemCount, 6); n++;
2757     paletteList = XmCreateScrolledList(colorDialog.palettesForm,"paletteList",args,n);
2758     XtAddCallback(paletteList, XmNbrowseSelectionCallback,
2759                                               selectPaletteCB, NULL);
2760
2761     n=0;
2762     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
2763     XtSetArg (args[n], XmNtopOffset, style.horizontalSpacing);  n++;
2764     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
2765     XtSetArg (args[n], XmNleftOffset, style.horizontalSpacing);  n++;
2766     XtSetArg (args[n], XmNrightAttachment, XmATTACH_POSITION);  n++;
2767     XtSetArg (args[n], XmNrightPosition, 60);  n++;
2768     XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM);  n++;
2769     XtSetValues (XtParent(paletteList), args, n);
2770
2771     style.count++;
2772 }
2773
2774 void 
2775 CreateBottomColor( void )
2776 {
2777     register int     n;
2778     Arg              args[12];
2779     XmString         string; 
2780     Widget           addDeleteForm;
2781     Widget           resourcesPB;
2782
2783     colorDialog.dlg = NULL;
2784
2785     if(style.count > 10)
2786        return;
2787
2788     if(TypeOfMonitor != XmCO_BLACK_WHITE)
2789     {
2790         /* Create form for Add and Delete buttons */
2791         n = 0;
2792         XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
2793         XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET);  n++;
2794         XtSetArg(args[n], XmNleftWidget, paletteList);  n++;
2795         XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
2796         XtSetArg(args[n], XmNrightOffset, 0);  n++;
2797         XtSetArg(args[n], XmNhorizontalSpacing, style.horizontalSpacing); n++;
2798         XtSetArg(args[n], XmNverticalSpacing, style.verticalSpacing); n++;
2799         XtSetArg(args[n], XmNallowOverlap, False); n++;
2800         XtSetArg(args[n], XmNchildType, XmWORK_AREA);  n++;
2801         addDeleteForm = 
2802             XmCreateForm(colorDialog.palettesForm, "addDeleteForm", args, n);
2803         XtManageChild(addDeleteForm);
2804
2805         /* Create Add button */
2806         n = 0;
2807         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
2808         XtSetArg (args[n], XmNtopOffset, ADD_PALETTE_TOP_OFFSET);  n++;
2809         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
2810         XtSetArg (args[n], XmNleftOffset, style.horizontalSpacing);  n++;
2811         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
2812         string = CMPSTR(((char *)GETMESSAGE(14, 6, "Add...")));
2813         XtSetArg (args[n], XmNlabelString, string); n++;
2814         colorDialog.addPaletteButton =
2815                 XmCreatePushButtonGadget(addDeleteForm, "addPalette", args, n);
2816         XmStringFree(string);
2817         XtManageChild(colorDialog.addPaletteButton);
2818         XtAddCallback(colorDialog.addPaletteButton, XmNactivateCallback, addPaletteCB, 
2819                             (XtPointer) NULL);
2820
2821        /* Create Delete button */
2822         n = 0;
2823         XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET);  n++;
2824         XtSetArg (args[n], XmNtopWidget, colorDialog.addPaletteButton);  n++;
2825         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
2826         XtSetArg (args[n], XmNleftOffset, style.horizontalSpacing);  n++;
2827         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
2828         string = CMPSTR(((char *)GETMESSAGE(14, 7, "Delete...")));
2829         XtSetArg (args[n], XmNlabelString, string); n++;
2830         colorDialog.deletePaletteButton =
2831         XmCreatePushButtonGadget(addDeleteForm,"deletePalette",args,n);
2832         XmStringFree(string);
2833         XtManageChild(colorDialog.deletePaletteButton);
2834         XtAddCallback(colorDialog.deletePaletteButton, XmNactivateCallback, deletePaletteCB,
2835                                (XtPointer) NULL);
2836     }
2837     
2838 /*
2839 **  Create a title box for palette color buttons
2840 */
2841     n=0;
2842     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET);  n++;
2843     XtSetArg (args[n], XmNtopWidget, colorDialog.paletteTB);  n++;
2844     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
2845     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
2846     XtSetArg (args[n], XmNbottomAttachment, XmATTACH_NONE);  n++;
2847     XtSetArg (args[n], XmNmarginWidth, 0);  n++;
2848     XtSetArg (args[n], XmNmarginHeight, 0);  n++;
2849     string = CMPSTR(pCurrentPalette->desc);
2850     XtSetArg (args[n], XmNtitleString, string); n++;
2851     colorDialog.buttonsTB = _DtCreateTitleBox(colorDialog.colorForm, "ButtonsTB", args, n);
2852     XtManageChild(colorDialog.buttonsTB);
2853     XmStringFree(string);
2854
2855     /* Create a form inside palette buttons titlebox */
2856     n = 0;
2857     XtSetArg(args[n], XmNhorizontalSpacing, style.horizontalSpacing); n++;
2858     XtSetArg(args[n], XmNverticalSpacing, style.verticalSpacing); n++;
2859     style.buttonsForm = XmCreateForm(colorDialog.buttonsTB, "buttonsForm", args, n);
2860     XtManageChild(style.buttonsForm);
2861     
2862     /* Create Modify... button */
2863     if(TypeOfMonitor != XmCO_BLACK_WHITE)
2864     {
2865         n = 0;
2866         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
2867         XtSetArg (args[n], XmNtopOffset, style.horizontalSpacing+BORDER_WIDTH);  n++;
2868         XtSetArg (args[n], XmNleftAttachment, XmATTACH_POSITION);  n++;
2869         XtSetArg (args[n], XmNleftPosition, ADD_PALETTE_LEFT_POSITION);  n++;
2870         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
2871         string = CMPSTR(((char *)GETMESSAGE(14, 8, "Modify...")));
2872         XtSetArg (args[n], XmNlabelString, string); n++;
2873         modifyColorButton = 
2874         XmCreatePushButtonGadget(style.buttonsForm, "modifyColorButton", args, n);
2875         XmStringFree(string);
2876         XtManageChild(modifyColorButton);
2877         XtAddCallback(modifyColorButton, XmNactivateCallback, modifyColorCB, 
2878                             (XtPointer) NULL);
2879     }
2880 /*
2881 **  Create a pushbutton for configuring DT colors
2882 */
2883     n=0;
2884     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET);  n++;
2885     XtSetArg (args[n], XmNtopWidget, colorDialog.buttonsTB);  n++;
2886     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
2887     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
2888     XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM);  n++;
2889     XtSetArg (args[n], XmNnavigationType, XmTAB_GROUP);  n++;
2890     string = CMPSTR(((char *)GETMESSAGE(14, 40, "Number Of Colors...")));
2891     XtSetArg (args[n], XmNlabelString, string); n++;
2892     resourcesPB = XmCreatePushButtonGadget(colorDialog.colorForm, "resourcesPB", args, n);
2893     XtManageChild(resourcesPB);
2894     XtAddCallback(resourcesPB, XmNactivateCallback, resourcesCB,
2895                                (XtPointer) style.colorDialog);
2896     XmStringFree(string);
2897
2898     style.count++;
2899 }
2900  
2901