Resolve some coverity warnings
[oweals/cde.git] / cde / programs / dtstyle / Backdrop.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 /* $TOG: Backdrop.c /main/7 1998/11/25 14:48:36 samborn $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  **
27  **   File:        Backdrop.c
28  **
29  **   Project:     DT 3.0 
30  **
31  **   Description: Controls the Dtstyle Backdrop dialog
32  **
33  **
34  **  (c) Copyright Hewlett-Packard Company, 1990, 1993.  
35  **
36  **
37  **
38  ****************************************************************************
39  ************************************<+>*************************************/
40
41 /*+++++++++++++++++++++++++++++++++++++++*/
42 /* include files                         */
43 /*+++++++++++++++++++++++++++++++++++++++*/
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <sys/types.h>
48 #include <errno.h>
49
50 #if defined(_AIX) || defined(__apollo)
51 #include <sys/dir.h>
52 #else
53 #include <dirent.h>             /* opendir(), directory(3C) */
54 #endif /* _AIX */
55
56 #ifdef __apollo
57 #include <X11/apollosys.h>      /* needed for S_ISDIR macro */
58 #endif
59
60
61 #include <locale.h>
62 #include <X11/Xlib.h>
63 #include <Xm/MwmUtil.h>
64
65 #include <Xm/Xm.h>
66 #include <Xm/XmP.h>
67 #include <Xm/DrawnB.h>
68 #include <Xm/Form.h>
69 #include <Xm/List.h>
70 #include <Xm/VendorSEP.h>
71 #include <Xm/AtomMgr.h>
72
73 #include <Dt/DialogBox.h>
74
75 #include <Dt/Message.h>
76 #include <Dt/SessionM.h>
77 #include <Dt/HourGlass.h>
78 #include <Dt/Wsm.h>
79 #include <Dt/UserMsg.h>
80
81 #include "Help.h"
82 #include "Main.h"
83 #include "SaveRestore.h"
84
85 /*+++++++++++++++++++++++++++++++++++++++*/
86 /* include extern functions              */
87 /*+++++++++++++++++++++++++++++++++++++++*/
88 #include "Backdrop.h"
89
90
91 /*+++++++++++++++++++++++++++++++++++++++*/
92 /* Local #defines                        */
93 /*+++++++++++++++++++++++++++++++++++++++*/
94 #define MAX_STR_LEN         128
95 #define B_OK_BUTTON           1
96 #define B_APPLY_BUTTON        2
97 #define B_CANCEL_BUTTON       3
98 #define B_HELP_BUTTON         4
99
100 #define ERR2   ((char *)GETMESSAGE(11, 2, "The backdrop portion of the Style Manager\n\
101 will not operate because there are no backdrop\nfiles available. Check $HOME/.dt/errorlog."))
102
103 /*+++++++++++++++++++++++++++++++++++++++*/
104 /* Internal Functions                    */
105 /*+++++++++++++++++++++++++++++++++++++++*/
106
107 static int CreateBackdropDialog( Widget parent) ;
108 static void MoreBitmaps( void ) ;
109 static ReadBitmaps( void ) ;
110 static Boolean CreatePixmaps( void ) ;
111 static ReadBitmapDirectory( char *dir ) ;
112 static void DrawBitmap( 
113                         Widget w,
114                         XtPointer client_data,
115                         XtPointer call_data) ;
116 static void SizeBitmap( 
117                         Widget w,
118                         XtPointer client_data,
119                         XtPointer call_data) ;
120 static XmString * MakeListStrings( void ) ;
121 static void FreeListStrings( XmString *listPtr) ;
122 static void ListCB( 
123                         Widget w,
124                         XtPointer client_data,
125                         XtPointer call_data) ;
126 static void ButtonCB( 
127                         Widget w,
128                         XtPointer client_data,
129                         XtPointer call_data) ;
130 static void GetColors( void ) ;
131 static void FreeAll( void ) ;
132 static void _DtMapCB( 
133                         Widget w,
134                         XtPointer client_data,
135                         XtPointer call_data) ;
136
137
138 /*+++++++++++++++++++++++++++++++++++++++*/
139 /* Internal Variables                    */
140 /*+++++++++++++++++++++++++++++++++++++++*/
141
142 typedef struct {
143     Widget   drawnButton;
144     char   **dirList;
145     int      dirCount;
146     char   **tmpBitmapNames;
147     int      tmpNumBitmaps;
148     int      tmpMaxNumBitmaps;
149     char   **bitmapNames;
150     char   **bitmapDescs;
151     Pixmap  *bitmaps;
152     int      numBitmaps;
153     int      maxNumBitmaps;
154     int      selected;
155     GC       gc;
156     int      width, height;
157     int      shadow;
158     Pixel    fg, bg;
159     char    *errStr;
160     char     noBitmaps;
161     Boolean  newColors;
162 } Backdrops, *BackdropsPtr;
163 static Backdrops backdrops; 
164
165 static saveRestore save = {FALSE, 0, };
166 char *BACKDROPSDLG = "backdropsDialog";
167
168 /*
169  *   copy of the system backdrop description file for the 
170  *   current locale in xrm form
171  */
172 static XrmDatabase sys_bd_DB = NULL;
173
174 /*
175  *   copy of the admin backdrop description file for the 
176  *   current locale in xrm form
177  */
178 static XrmDatabase adm_bd_DB = NULL;
179
180 /*
181  *   copy of the user's home backdrop description file for the 
182  *   current locale in xrm form
183  */
184 static XrmDatabase hm_bd_DB = NULL;
185
186 /*
187  *   final combination of the admin & system data bases 
188  */
189 static XrmDatabase bd_DB = NULL;
190
191
192
193 /*+++++++++++++++++++++++++++++++++++++++*/
194 /* build_dirList                         */
195 /*+++++++++++++++++++++++++++++++++++++++*/
196
197 char ** 
198 build_dirList(char * dirStr, 
199                  int * count)
200 {
201    char tokenSep[] = ":";
202    char * token;
203    char ** dirList = NULL;
204    register int i = 0;
205    char * tmpStr;
206    int len = strlen(dirStr);
207    *count = 0;
208
209    tmpStr = (char *)XtCalloc(1, len + 1);
210    strcpy(tmpStr, dirStr);
211    token = strtok(tmpStr, tokenSep);
212    while(token != NULL)
213      {
214        ++(i);
215        token = strtok(NULL, tokenSep);
216      }
217
218    if (i == 0)
219        return (NULL);
220
221    dirList = (char **) XtCalloc(1, i * sizeof(char *));
222    if( dirList )
223      {
224        strcpy(tmpStr, dirStr); 
225        token = strtok(tmpStr, tokenSep);
226        *count=0;
227        while(token != NULL)
228          {
229            dirList[*count] = (char *) XtCalloc(1, strlen( token ) + 1);
230            strcpy(dirList[*count], token);
231            token = strtok(NULL, tokenSep);
232            ++(*count);
233          }
234      }
235
236    XtFree ((char *) tmpStr);
237    return(dirList);
238  }
239
240
241
242 /*+++++++++++++++++++++++++++++++++++++++*/
243 /* free_dirList                          */
244 /*+++++++++++++++++++++++++++++++++++++++*/
245
246 void
247 free_dirList(char ** dirList, 
248                     int count)
249
250 {
251   register int   i;
252
253   if (dirList == NULL)
254     return;
255   for (i=0; i<count; i++)
256     XtFree((char *) dirList[i]);
257   
258   XtFree ((char *) dirList);
259 }
260
261
262 /************************************************************************
263  *  SelectCurrentBackdrop() - Selects current backdrop in list
264  *
265  ************************************************************************/
266 void SelectCurrentBackdrop(callback)
267 {
268     DtWsmWorkspaceInfo   *wInfo=NULL;
269     Atom             aWS;
270     Widget list;
271     char *backdropName;
272     int i;
273
274     if ((DtWsmGetCurrentWorkspace (style.display, style.root, &aWS) 
275                 != Success) ||
276         (DtWsmGetWorkspaceInfo (style.display, style.root, aWS, &wInfo)
277                 != Success))
278     {
279       return;
280     }
281       
282     list = XtNameToWidget(style.backdropDialog, "*bitmapList");
283
284     backdropName = XmGetAtomName(style.display, wInfo->backdropName);
285
286     for (i = 0; i < backdrops.numBitmaps; i++) {
287       if (strcmp(backdrops.bitmapNames[i], backdropName) == 0) {
288         XmListSelectPos (list, i + 1, callback);
289         XmListSetPos(list, i + 1);
290         backdrops.selected = i;
291       }
292     }
293
294     XtFree((char *) backdropName);
295     XtFree((char *) wInfo);
296 }
297
298 /************************************************************************
299  *  BackdropDialog() - Create backdrop selection dialog first time up.
300  *           If it has already been created, map it.
301  ************************************************************************/
302 void 
303 BackdropDialog(
304         Widget parent )
305 {
306     int i;
307     
308     if (style.backdropDialog == NULL) 
309     {
310         _DtTurnOnHourGlass(parent);  
311         if (!CreateBackdropDialog(parent)) { 
312            _DtTurnOffHourGlass(parent);  
313            return;
314         }
315         SelectCurrentBackdrop(False);
316         XtManageChild(style.backdropDialog);
317         XSync(style.display, 0);
318         XmUpdateDisplay(style.backdropDialog);
319         _DtTurnOffHourGlass(parent);  
320
321     }
322     else
323     {
324         SelectCurrentBackdrop(True);
325         XtManageChild(style.backdropDialog);
326         raiseWindow(XtWindow(XtParent(style.backdropDialog)));
327         XmUpdateDisplay(style.backdropDialog);
328     }
329 }
330
331
332 /************************************************************************
333  *   CreateBackdropDialog()
334  *           Create the Backdrop Dialog
335  ************************************************************************/
336 static int 
337 CreateBackdropDialog(
338         Widget parent )
339 {
340     register int     i, n;
341     Arg              args[20];
342     Widget           mainForm;
343     Widget           list;
344     XmString         strings[NUM_LABELS+1];
345     XmString        *listStrings;
346     char            *bd_desc;
347     char            *lang;
348
349
350     if (backdrops.noBitmaps) 
351     {
352         ErrDialog (backdrops.errStr, style.shell);
353         return 0;
354     }
355
356     /* initialize backdrop data */
357     backdrops.bitmapNames = NULL;
358     backdrops.bitmaps = NULL;
359     backdrops.numBitmaps = 0;
360     backdrops.maxNumBitmaps = 100;
361     backdrops.selected = -1;
362     backdrops.gc = NULL;
363     backdrops.errStr = NULL;
364     backdrops.shadow = 2;
365     backdrops.width = 200 - 2*backdrops.shadow;
366     backdrops.height = 200 - 2*backdrops.shadow;
367     backdrops.newColors = True;
368
369     
370     /* load the backdrop description data base for the given locale*/
371     /* from that locale's description file from the system location */
372     lang = setlocale (LC_CTYPE,NULL);
373
374 #ifdef hpux      /* hpux-specific parsing of the locale string */
375                  /* The following code is identical to the
376                     ExtractLocaleName function in WmResParse.c
377                     from dtwm
378                  */
379 #define MAXLOCALE       64      /* buffer size of locale name */
380
381 {   char           *start;
382     char           *end;
383     int             len;
384     static char     buf[MAXLOCALE];
385
386     /*  If lang has a substring ":<category>;", extract <category>
387      *  from the first such occurrence as the locale name.
388      */
389
390     start = lang;
391     if (start = strchr (lang, ':')) {
392         start++;
393         if (end = strchr (start, ';')) {
394             len = end - start;
395             strncpy(buf, start, len);
396             *(buf + len) = '\0';
397             lang = buf;
398       }
399     }
400 }
401 #endif  /* hpux */
402
403     bd_desc = (char *)XtMalloc(strlen("/usr/dt/backdrops/desc.") + strlen(lang) + 1);
404     strcpy (bd_desc,"/usr/dt/backdrops/desc.");
405     strcat (bd_desc, lang);
406     if(sys_bd_DB = XrmGetFileDatabase (bd_desc))
407         XrmMergeDatabases(sys_bd_DB, &bd_DB);
408     XtFree(bd_desc);
409     
410     /* load the backdrop description data base for the given locale*/
411     /* from that locale's description file from the admin location */
412     bd_desc = (char *)XtMalloc(strlen("/etc/dt/backdrops/desc.") + strlen(lang) + 1);
413     strcpy (bd_desc,"/etc/dt/backdrops/desc.");
414     strcat (bd_desc, lang);
415     if (adm_bd_DB = XrmGetFileDatabase (bd_desc))
416         XrmMergeDatabases(adm_bd_DB, &bd_DB);
417     XtFree(bd_desc);
418
419     /* load the backdrop description from the user's .dt/backdrops directory */
420     /* regardless of locale */
421     bd_desc = (char *)XtMalloc(strlen(style.home) + strlen("/.dt/backdrops/desc.backdrops") + 1);
422     strcpy (bd_desc, style.home);
423     strcat (bd_desc, "/.dt/backdrops/desc.backdrops");
424     if (hm_bd_DB = XrmGetFileDatabase (bd_desc))
425         XrmMergeDatabases(hm_bd_DB, &bd_DB);
426     XtFree(bd_desc);
427
428     /* Set up DialogBox button labels. */
429     strings[0] = XmStringCreateLocalized ((String) _DtOkString);
430     strings[1] = XmStringCreateLocalized ((String) _DtApplyString);
431     strings[2] = XmStringCreateLocalized ((String) _DtCloseString);
432     strings[3] = XmStringCreateLocalized ((String) _DtHelpString);
433
434     /* saveRestore
435      * Note that save.poscnt has been initialized elsewhere.
436      * save.posArgs may contain information from restoreBackdrop().*/
437
438     /* create the dialog box with shell */
439
440     XtSetArg (save.posArgs[save.poscnt], XmNbuttonCount, NUM_LABELS+1);
441                                                                  save.poscnt++;
442     XtSetArg (save.posArgs[save.poscnt], XmNbuttonLabelStrings, strings);
443                                                                  save.poscnt++;
444     XtSetArg (save.posArgs[save.poscnt], XmNdefaultPosition, False);
445                                                                  save.poscnt++;
446     XtSetArg (save.posArgs[save.poscnt], XmNallowOverlap, False);
447                                                                  save.poscnt++;
448     style.backdropDialog = __DtCreateDialogBoxDialog (parent, BACKDROPSDLG,
449                                                      save.posArgs, save.poscnt);
450     XtAddCallback(style.backdropDialog, XmNcallback, ButtonCB, NULL);
451     XtAddCallback(style.backdropDialog, XmNmapCallback, _DtMapCB, parent);
452     XtAddCallback(style.backdropDialog, XmNhelpCallback,
453             (XtCallbackProc)HelpRequestCB, (XtPointer)HELP_BACKDROP_DIALOG);
454
455     /* free compound strings now */
456     XmStringFree (strings[0]);
457     XmStringFree (strings[1]);
458     XmStringFree (strings[2]);
459     XmStringFree (strings[3]);
460
461     n = 0;
462     XtSetArg (args[n], XmNtitle, ((char *)GETMESSAGE(11, 12, "Style Manager - Backdrop"))); n++;
463     XtSetArg (args[n], XmNuseAsyncGeometry, True); n++;
464     XtSetValues (XtParent(style.backdropDialog), args, n);
465
466     /*  get bitmap data */
467     if (!ReadBitmaps())  return 0;         /* uses style.backdropDialog */
468
469     /* create the form to go in to dialog box as the work area */
470     n = 0;
471     XtSetArg(args[n], XmNhorizontalSpacing, style.horizontalSpacing); n++;
472     XtSetArg(args[n], XmNverticalSpacing, style.verticalSpacing); n++;
473     XtSetArg (args[n], XmNchildType, XmWORK_AREA);  n++;
474
475     XtSetArg (args[n], XmNallowOverlap, False);  n++;
476     mainForm = XmCreateForm (style.backdropDialog, "backdropsForm", args, n);
477
478     /* create the scrolled list of bitmap names... first create XmStrings */
479     listStrings = MakeListStrings ();
480     n = 0;
481     XtSetArg (args[n], XmNautomaticSelection, True);              n++;
482     XtSetArg (args[n], XmNselectionPolicy, XmBROWSE_SELECT);      n++;
483     XtSetArg (args[n], XmNitems, listStrings);                    n++;
484     XtSetArg (args[n], XmNitemCount, backdrops.numBitmaps);       n++;
485     list = XmCreateScrolledList (mainForm, "bitmapList", args, n);
486     XtAddCallback (list, XmNbrowseSelectionCallback, ListCB, (XtPointer)NULL);
487     FreeListStrings (listStrings);            /* after list has copied */
488
489     /* set up attachments for scrolled list itself */
490     n = 0;
491     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);          n++;
492     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);        n++;
493     XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM);       n++;
494     XtSetValues (XtParent(list), args, n);
495
496     /*   Create drawing area for the bitmap  */
497     n = 0;
498     XtSetArg (args[n], XmNshadowType, XmSHADOW_IN);                     n++;
499     XtSetArg (args[n], XmNshadowThickness, backdrops.shadow);           n++;
500     XtSetArg (args[n], XmNhighlightThickness, 0);                       n++;
501     XtSetArg (args[n], XmNrightAttachment, XmATTACH_WIDGET);            n++;
502     XtSetArg (args[n], XmNrightWidget, XtParent(list));                 n++;
503     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);                n++;
504     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);               n++;
505     XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM);             n++;
506     XtSetArg (args[n], XmNborderWidth, 0);                              n++;
507     XtSetArg (args[n], XmNwidth, backdrops.width+2*backdrops.shadow);   n++;
508     XtSetArg (args[n], XmNheight, backdrops.height+2*backdrops.shadow); n++;
509     XtSetArg (args[n], XmNtraversalOn, False);                          n++;  
510     backdrops.drawnButton = XmCreateDrawnButton (mainForm, "bitmap", args, n);
511     XtAddCallback (backdrops.drawnButton, XmNexposeCallback, DrawBitmap, NULL);
512     XtAddCallback (backdrops.drawnButton, XmNresizeCallback, SizeBitmap, NULL);
513
514     /* manage all of the widgets */
515     XtManageChild (mainForm);
516     XtManageChild (backdrops.drawnButton);
517     XtManageChild (list);
518
519     return 1;
520 }
521
522
523 /************************************************************************
524  *   MoreBitmaps()
525  *           Create space for more bitmap entries
526  ************************************************************************/
527 static void 
528 MoreBitmaps( void )
529 {
530     int   newSize;
531
532     /* allocate space for icon names */
533     newSize =  (backdrops.maxNumBitmaps + 100) * sizeof(char *);
534     backdrops.bitmapNames = (char **) XtRealloc((char *)backdrops.bitmapNames, 
535                                                 newSize);
536
537     /* now allocate new bitmap space */
538     newSize =  (backdrops.maxNumBitmaps + 100) * sizeof(Pixmap);
539     backdrops.bitmaps = (Pixmap *)XtRealloc((char *)backdrops.bitmaps, newSize);
540
541     backdrops.maxNumBitmaps += 100;
542 }
543
544 /************************************************************************
545  * cmpstringp()
546  * qsort() sort function, used for sorting bitmap names into alphabetical order
547  * can't use strcmp() due to char** rather than char*
548  ************************************************************************/
549 static int
550 cmpstringp(const void *p1, const void *p2)
551 {
552   return strcmp(*(char * const *) p1, *(char * const *) p2);
553 }
554
555 /************************************************************************
556  *   ReadBitmaps()
557  *   Create an array of bitmaps by reading backdrop directories in the
558  *   following order overriding any duplicates:
559  *   1) Read the system location /usr/dt/backdrops
560  *   2) Read the admin location /etc/dt/backdrops
561  *   3) Read the directories specified by the backdropDirectories
562  *      resource.
563  *   4) Read the user's home directory /$HOME/.dt/backdrops.
564  ************************************************************************/
565 static 
566      ReadBitmaps( void )
567 {
568   int            status;
569   Pixmap         tmpPix = 0;
570   int            width, height, x, y;
571   Window         win;
572   int            num;
573   register int   i;   
574   char          *string;
575   /* allocate space for temporary bitmap info */
576   backdrops.tmpBitmapNames = (char **)XtCalloc(100, sizeof(char *));
577   backdrops.tmpMaxNumBitmaps = 100;
578   backdrops.tmpNumBitmaps = 0;
579   
580   /* read system backdrop directory */
581     ReadBitmapDirectory("/usr/dt/backdrops");
582   /* read sys admin backdrop directory */
583   ReadBitmapDirectory("/etc/dt/backdrops");
584   
585   /* Parse the backdropDirectories resource to get the individual directories */
586   if (style.xrdb.backdropDir)
587     {
588       backdrops.dirList = build_dirList(style.xrdb.backdropDir, &backdrops.dirCount);
589       
590       /* compile the list of bitmaps */
591       for (i=0; i<backdrops.dirCount; i++)
592         ReadBitmapDirectory(backdrops.dirList[i]);
593     }
594   
595   /* read the directory $HOME/.dt/backdrops */
596   string = (char *)XtMalloc(strlen(style.home) + strlen("/.dt/backdrops") + 1);
597   if (string != NULL)
598   {
599     sprintf(string, "%s/.dt/backdrops", style.home);
600     ReadBitmapDirectory(string);
601     XtFree(string);
602   }
603
604   if (backdrops.tmpNumBitmaps == 0)
605     {
606       /* give error dialog, free space, and return */
607       backdrops.errStr = (char *)XtMalloc(strlen(ERR2) + 1);
608       sprintf(backdrops.errStr, "%s", ERR2);
609       ErrDialog (backdrops.errStr, style.shell); 
610       FreeAll();      
611       free_dirList(backdrops.dirList, backdrops.dirCount);
612       return 0;
613     }
614
615   /* Sort the list into alphanetical order */
616   qsort(backdrops.tmpBitmapNames, backdrops.tmpNumBitmaps, sizeof(char *), cmpstringp);
617   
618   /* get the fg/bg colors from Dtwm */
619   if (backdrops.newColors)
620     {
621       GetColors();
622       backdrops.newColors = False;
623     }   
624   
625     /* create all the pixmaps */
626   if (!CreatePixmaps())
627     {
628       /* give error dialog, free space, and return */
629       backdrops.errStr = (char *)XtMalloc(strlen(ERR2) + 1);
630       sprintf(backdrops.errStr, "%s", ERR2);
631       ErrDialog (backdrops.errStr, style.shell); 
632       FreeAll();  
633       free_dirList(backdrops.dirList, backdrops.dirCount);
634       return 0;
635     }
636   
637   
638   if (backdrops.selected == -1) backdrops.selected = 0;   
639   
640   return 1;
641 }
642
643
644
645 /************************************************************************
646  *   CreatePixmaps()
647  *           Create the pixmpas in the backdrop list
648              with workprocs 10 at a time
649  ************************************************************************/
650 static Boolean
651 CreatePixmaps( void )
652
653 {
654     static int     pixmapsCreated=0;
655     int            i;
656     Pixmap         tmpPixmap;
657     
658     backdrops.numBitmaps = 0;
659
660     /* allocate space for real bitmap info */
661     backdrops.bitmapNames = (char **)XtCalloc(100, sizeof(char *));
662     backdrops.bitmaps = (Pixmap *)XtCalloc(100, sizeof(Pixmap));
663
664     for (i=0; i<backdrops.tmpNumBitmaps; i++)
665     {
666         tmpPixmap = XmGetPixmap (style.screen, 
667                                  backdrops.tmpBitmapNames[i], 
668                                  backdrops.fg, backdrops.bg); 
669         if (tmpPixmap != XmUNSPECIFIED_PIXMAP)
670         {
671             if (backdrops.numBitmaps == backdrops.maxNumBitmaps)
672                 MoreBitmaps();
673
674             backdrops.bitmapNames[backdrops.numBitmaps] = 
675                     backdrops.tmpBitmapNames[i];
676             backdrops.bitmaps[backdrops.numBitmaps] = tmpPixmap;
677  
678             backdrops.numBitmaps++;
679         }
680
681     }
682     if (backdrops.numBitmaps)
683         return(True);
684     else
685         return(False);
686
687 }
688
689
690 /************************************************************************
691  *   ReadBitmapDirectory()
692  *           Create an array of bitmap names overriding duplicates
693  ************************************************************************/
694 static
695 ReadBitmapDirectory( 
696     char *dir )
697
698 {
699     DIR            *dirp;
700     struct dirent  *filep;
701     int             i;
702     Boolean         duplicate;
703     char           *name;
704     int             stat_result;
705     struct stat     stat_buf;
706     char           *statPath, *pStatPath;
707     int             newSize;
708
709     /* open the backdrops directory */
710     if ((dirp = opendir(dir)) == NULL)
711     {
712         /* print message to errorlog, free space, and return */
713       return 0;
714     }
715     
716     /* create string to contain complete path */
717     statPath = (char *) XtMalloc(strlen(dir) + MAX_STR_LEN + 2);
718     strcpy (statPath, dir);
719     strcat (statPath, "/");
720     pStatPath = statPath + strlen(statPath);
721
722     filep = readdir(dirp);
723
724     while (filep != NULL)
725     {
726         /* append filename to stat path */
727         strcpy (pStatPath, filep->d_name);
728
729         /* stat the file */
730         if ((stat_result = stat (statPath, &stat_buf)) != 0)
731         {
732             filep = readdir(dirp);
733             continue;
734         }
735             
736         /* skip directories */
737         if ((stat_buf.st_mode & S_IFMT) == S_IFDIR)
738         {
739             filep = readdir(dirp);
740             continue; 
741         }
742         
743         name = (char *) XtMalloc(strlen(filep->d_name) + 1);
744         strcpy (name, filep->d_name);
745
746         /* strip suffix off filename if it's a .pm or .bm
747          * motif requires other formats like jpg, png etc to
748          * have the extension on to work with the XmGetPixmap() calls */
749         if(strlen(name) > 3
750            && (0 == strcmp(name + strlen(name) - 3, ".pm")
751                || 0 == strcmp(name + strlen(name) - 3, ".bm")))
752         {
753           (void)strtok(name, ".");
754         }
755
756
757         /* check for duplicates */
758         duplicate = 0;
759         for (i=0; i<backdrops.tmpNumBitmaps; i++)
760         {
761             if (!strcmp(backdrops.tmpBitmapNames[i], name))
762             {
763                 duplicate = 1;
764                 break;                
765             }
766         }
767         
768         if (!duplicate)
769         {
770             /* add to the temporary bitmap list */
771
772             if (backdrops.tmpNumBitmaps == backdrops.tmpMaxNumBitmaps)
773             {
774                 /* allocate space for more temporary bitmap info */
775                 newSize =  (backdrops.tmpMaxNumBitmaps + 100) * sizeof(char *);
776                 backdrops.tmpBitmapNames = 
777                     (char **)XtRealloc((char *)backdrops.tmpBitmapNames, newSize);
778                 backdrops.tmpMaxNumBitmaps += 100;
779             }
780
781             backdrops.tmpBitmapNames[backdrops.tmpNumBitmaps] = 
782                 (char *) XtMalloc(strlen(name)+1);
783             strcpy (backdrops.tmpBitmapNames[backdrops.tmpNumBitmaps], name);
784
785             backdrops.tmpNumBitmaps++;
786         }
787
788         filep = readdir(dirp);
789         XtFree(name);
790     }
791
792     XtFree(statPath);
793
794
795     closedir (dirp);
796     return 1;
797 }
798
799
800 /************************************************************************
801  *   DrawBitmap()
802  *           This is the exposeCallback for the bitmap drawing area.
803  ************************************************************************/
804 static void 
805 DrawBitmap(
806         Widget w,
807         XtPointer client_data,
808         XtPointer call_data )
809 {
810     XGCValues     gcValues;
811     Arg           args[3];
812         
813     if (backdrops.selected == -1)
814         return;
815
816     if (backdrops.newColors)
817     {
818         GetColors();
819
820         /* we could keep track of which tile pixmaps need to be updated
821            since the last workspace change, but for now simply regenerate 
822            each pixmap as it is selected after a workspace change has 
823            occurred */
824
825         /* backdrops.newColors = False; */
826     }
827
828     if (backdrops.gc == NULL)
829     {
830         gcValues.background = backdrops.bg;
831         gcValues.foreground = backdrops.fg;
832         gcValues.fill_style = FillTiled;
833         gcValues.tile = backdrops.bitmaps[backdrops.selected];
834
835         backdrops.gc = XCreateGC (style.display, XtWindow(w), 
836                                 GCForeground | GCBackground | 
837                                 GCTile | GCFillStyle, &gcValues);
838     }
839
840     XFillRectangle (style.display, XtWindow(w), backdrops.gc, backdrops.shadow,
841                     backdrops.shadow, backdrops.width, backdrops.height);
842 }
843
844
845 /************************************************************************
846  *   SizeBitmap()
847  *           This is the resizeCallback for the bitmap drawing area.
848  ************************************************************************/
849 static void 
850 SizeBitmap(
851         Widget w,
852         XtPointer client_data,
853         XtPointer call_data )
854 {
855     backdrops.width = XtWidth(w) - 2*backdrops.shadow;
856     backdrops.height = XtHeight(w) - 2*backdrops.shadow;
857 }
858
859
860 /************************************************************************
861  *   MakeListStrings()
862  *           Make XmStrings from the bitmap descriptions, to pass into list.
863  *  
864  ************************************************************************/
865 static XmString * 
866 MakeListStrings( void )
867 {
868     int         i;
869     XmString   *list;
870     char       *name_str;
871     char       *class_str;
872     char       *str_type_return;
873     XrmValue    value_return;
874     
875     /* allocate space for bitmap descriptions */
876     backdrops.bitmapDescs = (char **)XtCalloc(backdrops.numBitmaps, sizeof(char *));
877     
878     for (i=0; i<backdrops.numBitmaps; i++)
879       {
880         if (bd_DB !=NULL)
881           {
882             name_str = (char *) XtMalloc(strlen("backdrops.") + 
883                                          strlen(backdrops.bitmapNames[i]) +
884                                          strlen(".desc") + 1);
885             
886             class_str = (char *) XtMalloc(strlen("Backdrops.") + 
887                                           strlen(backdrops.bitmapNames[i]) + 
888                                           strlen(".Desc") + 1);
889             strcpy(name_str, "backdrops.");
890             strcpy(class_str, "Backdrops.");
891             strcat(name_str, backdrops.bitmapNames[i]);
892             strcat(class_str, backdrops.bitmapNames[i]);
893             strcat(name_str, ".desc");
894             strcat(class_str, ".Desc");
895
896             if (XrmGetResource (bd_DB, name_str, class_str, &str_type_return, &value_return))
897               {
898                 /* make copy of resource value */
899                 backdrops.bitmapDescs[i] = (char *) XtMalloc(value_return.size + 1);
900                 strcpy (backdrops.bitmapDescs[i], value_return.addr);
901               }    
902             else
903               {   
904                 backdrops.bitmapDescs[i] = (char *) XtMalloc(strlen(backdrops.bitmapNames[i]) + 1);
905                 strcpy(backdrops.bitmapDescs[i], backdrops.bitmapNames[i]);
906               }
907           }
908         else
909           {       
910             backdrops.bitmapDescs[i] = (char *) XtMalloc(strlen(backdrops.bitmapNames[i]) + 1);
911             strcpy(backdrops.bitmapDescs[i], backdrops.bitmapNames[i]);
912           }
913       }
914
915     list = (XmString *) XtCalloc(backdrops.numBitmaps, sizeof(XmString));
916     
917     for (i = 0; i < backdrops.numBitmaps; i++)
918     {
919         list[i] = XmStringCreateLocalized (backdrops.bitmapDescs[i]);
920     }
921
922     return (list);
923 }
924
925
926 /************************************************************************
927  *   FreeListStrings()
928  *           Free XmStrings from the bitmap names, passed into list.
929  ************************************************************************/
930 static void 
931 FreeListStrings(
932         XmString *listPtr )
933 {
934     int         i;
935     int         n;
936     XmString   *list = listPtr;
937
938     for (i = 0; i < backdrops.numBitmaps; i++)
939     {
940         if (list[i]) XmStringFree(list[i]);
941     }
942     XtFree ((char *)list);
943 }
944
945
946 /************************************************************************
947  *   ListCB()
948  *           Get the bitmap selected from the list
949  ************************************************************************/
950 static void 
951 ListCB(
952         Widget w,
953         XtPointer client_data,
954         XtPointer call_data )
955 {
956     XmListCallbackStruct  *cb = (XmListCallbackStruct *)call_data;
957
958     backdrops.selected = cb->item_position - 1;
959
960     XSetTile (style.display, backdrops.gc, 
961               backdrops.bitmaps[backdrops.selected]);
962
963     DrawBitmap (backdrops.drawnButton, NULL, NULL);
964 }
965
966
967
968 /************************************************************************
969  *   ButtonCB()
970  *          
971  ************************************************************************/
972 static void 
973 ButtonCB(
974         Widget w,
975         XtPointer client_data,
976         XtPointer call_data )
977 {
978     int      n, num;
979     Arg      args[MAX_ARGS];
980
981     DtDialogBoxCallbackStruct *cb = (DtDialogBoxCallbackStruct *) call_data;
982
983     switch (cb->button_position)
984     {
985       case B_APPLY_BUTTON:
986           /* send message to update backdrop */
987
988           num = backdrops.selected;
989
990           _DtWsmChangeBackdrop(style.display, style.root, 
991                              backdrops.bitmapNames[num], 
992                              backdrops.bitmaps[num]);
993           break;
994
995       case B_OK_BUTTON:  
996           /* send message to update backdrop */
997
998           num = backdrops.selected;
999
1000           _DtWsmChangeBackdrop(style.display, style.root,
1001                              backdrops.bitmapNames[num],
1002                              backdrops.bitmaps[num]);
1003           XtUnmanageChild(w);
1004           break;
1005
1006       case B_CANCEL_BUTTON:            /* close */
1007           XtUnmanageChild(w);
1008           break;
1009
1010       case B_HELP_BUTTON:
1011           XtCallCallbacks(style.backdropDialog, XmNhelpCallback, (XtPointer)NULL);
1012           break;
1013
1014       default:
1015           break;
1016     }
1017 }
1018
1019
1020 /************************************************************************
1021  *   CheckWorkspace()
1022  *           Workspace may have changed, so get current workspace
1023  *           colors and draw the backdrop bitmap
1024  *          
1025  ************************************************************************/
1026 void 
1027 CheckWorkspace( void )
1028 {
1029     backdrops.newColors = True;         /* need to get new colors */
1030     if (style.backdropDialog && XtIsManaged(style.backdropDialog))
1031     {
1032         DrawBitmap (backdrops.drawnButton, NULL, NULL);
1033     }
1034 }
1035
1036
1037 /************************************************************************
1038  *   GetColors()
1039  *           Get current workspace colors, and update GC if needed
1040  *          
1041  ************************************************************************/
1042 static void 
1043 GetColors( void )
1044 {
1045     DtWsmWorkspaceInfo   *wInfo=NULL;
1046     unsigned long    num=0;
1047     Pixel            fg, bg;
1048     XGCValues        gcValues;
1049     Atom             aWS;
1050
1051     if ((DtWsmGetCurrentWorkspace (style.display, style.root, &aWS) 
1052                 == Success) &&
1053         (DtWsmGetWorkspaceInfo (style.display, style.root, aWS, &wInfo)
1054                 == Success))
1055     {
1056         backdrops.bg = wInfo->bg;
1057         backdrops.fg = wInfo->fg;
1058         DtWsmFreeWorkspaceInfo (wInfo);
1059     }
1060     else 
1061     {
1062         backdrops.bg = 0;
1063         backdrops.fg = 1;
1064     }
1065
1066     if (backdrops.gc)      /* update the gc if there is one */
1067     {
1068         gcValues.background = backdrops.bg;
1069         gcValues.foreground = backdrops.fg;
1070
1071         /* free old pixmap */
1072         XmDestroyPixmap(style.screen, 
1073                         backdrops.bitmaps[backdrops.selected]);
1074
1075         /* allocate new pixmap */
1076         backdrops.bitmaps[backdrops.selected] = 
1077             XmGetPixmap (style.screen, 
1078                          backdrops.bitmapNames[backdrops.selected], 
1079                          backdrops.fg, backdrops.bg); 
1080
1081         gcValues.tile = backdrops.bitmaps[backdrops.selected];
1082
1083         XChangeGC (style.display, backdrops.gc, 
1084                    GCForeground | GCBackground | GCTile, &gcValues);
1085     }
1086 }
1087
1088
1089 /************************************************************************
1090  * FreeAll()
1091  *        Free some space that was allocated for backdrops
1092  ************************************************************************/
1093 static void 
1094 FreeAll( void )
1095 {
1096     int i;
1097
1098     /* set no bitmaps flag, so we won't try to get them next time */ 
1099     backdrops.noBitmaps = 1;
1100
1101     /* free temporary list of backdrop names */
1102     for (i = 0; i < backdrops.tmpNumBitmaps; i++)
1103         if (backdrops.tmpBitmapNames[i]) 
1104             XtFree(backdrops.tmpBitmapNames[i]);
1105     XtFree ((char *)backdrops.tmpBitmapNames);
1106     XtFree ((char *)backdrops.bitmapNames);
1107
1108     /* free backdrop bitmaps */
1109     for (i = 0; i < backdrops.numBitmaps; i++) {
1110         if (backdrops.bitmaps[i]) 
1111             XFreePixmap (style.display, backdrops.bitmaps[i]);
1112     if (backdrops.numBitmaps)
1113         XtFree((char *)backdrops.bitmaps);  
1114     }
1115
1116     /* destory widgets (via first parent) */
1117     XtDestroyWidget (XtParent(style.backdropDialog));
1118     style.backdropDialog = NULL;
1119 }
1120
1121
1122 /************************************************************************
1123  * _DtMapCB
1124  *
1125  ************************************************************************/
1126 static void 
1127 _DtMapCB(
1128         Widget w,
1129         XtPointer client_data,
1130         XtPointer call_data )
1131 {
1132
1133     DtWsmRemoveWorkspaceFunctions(style.display, XtWindow(XtParent(w)));
1134
1135     if (!save.restoreFlag)
1136         putDialog((Widget)client_data, w);
1137
1138     XtRemoveCallback(style.backdropDialog, XmNmapCallback, _DtMapCB, NULL);
1139 }
1140
1141
1142 /************************************************************************
1143  * restoreBackdrop()
1144  *
1145  * restore any state information saved with saveBackdrop.
1146  * This is called from restoreSession with the application
1147  * shell and the special xrm database retrieved for restore.
1148  ************************************************************************/
1149 void 
1150 restoreBackdrop(
1151         Widget shell,
1152         XrmDatabase db )
1153 {
1154     XrmName xrm_name[5];
1155     XrmRepresentation rep_type;
1156     XrmValue value;
1157
1158     xrm_name [0] = XrmStringToQuark (BACKDROPSDLG);
1159     xrm_name [2] = 0;
1160
1161     /* get x position */
1162     xrm_name [1] = XrmStringToQuark ("x");
1163     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)) {
1164         XtSetArg (save.posArgs[save.poscnt], XmNx, atoi((char *)value.addr)); 
1165         save.poscnt++;
1166         save.restoreFlag = True;
1167     }
1168
1169     /* get y position */
1170     xrm_name [1] = XrmStringToQuark ("y");
1171     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)) {
1172         XtSetArg (save.posArgs[save.poscnt], XmNy, atoi((char *)value.addr)); 
1173         save.poscnt++;
1174     }
1175
1176     /* get width */
1177     xrm_name [1] = XrmStringToQuark ("width");
1178     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)) {
1179         XtSetArg(save.posArgs[save.poscnt], XmNwidth, atoi((char *)value.addr));
1180         save.poscnt++;
1181     }
1182
1183     /* get height */
1184     xrm_name [1] = XrmStringToQuark ("height");
1185     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)) {
1186         XtSetArg(save.posArgs[save.poscnt],XmNheight, atoi((char *)value.addr));
1187         save.poscnt++;
1188     }
1189
1190     xrm_name [1] = XrmStringToQuark ("ismapped");
1191     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
1192     /* Are we supposed to be mapped? */
1193     if (strcmp(value.addr, "True") == 0)
1194         BackdropDialog(shell);
1195 }
1196
1197
1198 /************************************************************************
1199  * saveBackdrop()
1200  *
1201  * This routine will write out to the passed file descriptor any state
1202  * information this dialog needs.  It is called from saveSessionCB with the
1203  * file already opened.
1204  * All information is saved in xrm format.  There is no restriction
1205  * on what can be saved.  It doesn't have to be defined or be part of any
1206  * widget or Xt definition.  Just name and save it here and recover it in
1207  * restoreBackdrop.  The suggested minimum is whether you are mapped, and your
1208  * location.
1209  ************************************************************************/
1210 void 
1211 saveBackdrop(
1212         int fd )
1213 {
1214     Position x,y;
1215     Dimension width, height;
1216     char *bufr = style.tmpBigStr;     /* size=[1024], make bigger if needed */
1217     XmVendorShellExtObject  vendorExt;
1218     XmWidgetExtData         extData;
1219
1220     if (style.backdropDialog != NULL) 
1221     {
1222         if (XtIsManaged(style.backdropDialog))
1223             sprintf(bufr, "*backdropsDialog.ismapped: True\n");
1224         else
1225             sprintf(bufr, "*backdropsDialog.ismapped: False\n");
1226
1227         /* Get and write out the geometry info for our Window */
1228         x = XtX (XtParent(style.backdropDialog));
1229         y = XtY (XtParent(style.backdropDialog));
1230         width = XtWidth (style.backdropDialog);
1231         height = XtHeight (style.backdropDialog);
1232
1233         /* Modify x & y to take into account window mgr frames
1234          * This is pretty bogus, but I don't know a better way to do it.
1235          */
1236         extData = _XmGetWidgetExtData(style.shell, XmSHELL_EXTENSION);
1237         vendorExt = (XmVendorShellExtObject)extData->widget;
1238         x -= vendorExt->vendor.xOffset;
1239         y -= vendorExt->vendor.yOffset;
1240
1241         sprintf(bufr, "%s*backdropsDialog.x: %d\n", bufr, x);
1242         sprintf(bufr, "%s*backdropsDialog.y: %d\n", bufr, y);
1243         sprintf(bufr, "%s*backdropsDialog.width: %d\n", bufr, width);
1244         sprintf(bufr, "%s*backdropsDialog.height: %d\n", bufr, height);
1245         sprintf(bufr, "%s*backdropsDialog.selectedItemNum: %d\n", bufr, 
1246                 backdrops.selected);
1247         sprintf(bufr, "%s*backdropsDialog.selectedItem: %s\n", bufr, 
1248                 backdrops.bitmapNames[backdrops.selected]);
1249         if(-1 == write (fd, bufr, strlen(bufr))) {
1250                         perror(strerror(errno));
1251                 }
1252     }
1253 }
1254
1255
1256