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