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