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