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