Backdrop.c 38 KB

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