Some menu entry beautification
[wmaker-crm.git] / WPrefs.app / WPrefs.c
blob76fd6c671586bde7742ce5a106cc70f50c82e8b6
1 /* WPrefs.c- main window and other basic stuff
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 #include "WPrefs.h"
25 #include <assert.h>
28 extern Panel *InitWindowHandling(WMScreen *scr, WMWindow *win);
30 extern Panel *InitKeyboardSettings(WMScreen *scr, WMWindow *win);
32 extern Panel *InitMouseSettings(WMScreen *scr, WMWindow *win);
34 extern Panel *InitKeyboardShortcuts(WMScreen *scr, WMWindow *win);
36 extern Panel *InitWorkspace(WMScreen *scr, WMWindow *win);
38 extern Panel *InitFocus(WMScreen *scr, WMWindow *win);
40 extern Panel *InitPreferences(WMScreen *scr, WMWindow *win);
42 extern Panel *InitText(WMScreen *scr, WMWindow *win);
44 extern Panel *InitConfigurations(WMScreen *scr, WMWindow *win);
46 extern Panel *InitPaths(WMScreen *scr, WMWindow *win);
48 extern Panel *InitMenu(WMScreen *scr, WMWindow *win);
50 extern Panel *InitExpert(WMScreen *scr, WMWindow *win);
52 extern Panel *InitMenuPreferences(WMScreen *scr, WMWindow *win);
54 extern Panel *InitIcons(WMScreen *scr, WMWindow *win);
56 extern Panel *InitThemes(WMScreen *scr, WMWindow *win);
58 extern Panel *InitAppearance(WMScreen *scr, WMWindow *win);
62 #define MAX_SECTIONS 16
65 typedef struct _WPrefs {
66 WMWindow *win;
68 WMScrollView *scrollV;
69 WMFrame *buttonF;
70 WMButton *sectionB[MAX_SECTIONS];
72 int sectionCount;
74 WMButton *saveBtn;
75 WMButton *closeBtn;
76 WMButton *undoBtn;
77 WMButton *undosBtn;
79 WMButton *balloonBtn;
81 WMFrame *banner;
82 WMLabel *nameL;
83 WMLabel *versionL;
84 WMLabel *creditsL;
85 WMLabel *statusL;
87 Panel *currentPanel;
88 } _WPrefs;
91 static _WPrefs WPrefs;
93 /* system wide defaults dictionary. Read-only */
94 static proplist_t GlobalDB = NULL;
95 /* user defaults dictionary */
96 static proplist_t WindowMakerDB = NULL;
99 static Bool TIFFOK = False;
102 #define INITIALIZED_PANEL (1<<0)
107 static void loadConfigurations(WMScreen *scr, WMWindow *mainw);
109 static void savePanelData(Panel *panel);
111 static void prepareForClose();
113 void
114 quit(WMWidget *w, void *data)
116 prepareForClose();
118 exit(0);
122 static void
123 save(WMWidget *w, void *data)
125 int i;
126 proplist_t p1, p2;
127 proplist_t keyList;
128 proplist_t key;
129 char *msg = "Reconfigure";
130 XEvent ev;
133 /* puts("gathering data");*/
134 for (i=0; i<WPrefs.sectionCount; i++) {
135 PanelRec *rec = WMGetHangedData(WPrefs.sectionB[i]);
136 if ((rec->callbacks.flags & INITIALIZED_PANEL))
137 savePanelData((Panel*)rec);
139 /* puts("compressing data");*/
140 /* compare the user dictionary with the global and remove redundant data */
141 keyList = PLGetAllDictionaryKeys(GlobalDB);
142 /* puts(PLGetDescription(WindowMakerDB));*/
143 for (i=0; i<PLGetNumberOfElements(keyList); i++) {
144 key = PLGetArrayElement(keyList, i);
146 /* We don't have this value anyway, so no problem.
147 * Probably a new option */
148 p1 = PLGetDictionaryEntry(WindowMakerDB, key);
149 if (!p1)
150 continue;
151 /* The global doesn't have it, so no problem either. */
152 p2 = PLGetDictionaryEntry(GlobalDB, key);
153 if (!p2)
154 continue;
155 /* If both values are the same, don't save. */
156 if (PLIsEqual(p1, p2))
157 PLRemoveDictionaryEntry(WindowMakerDB, key);
159 /* puts(PLGetDescription(WindowMakerDB));*/
160 PLRelease(keyList);
161 /* puts("storing data");*/
163 PLSave(WindowMakerDB, YES);
166 memset(&ev, 0, sizeof(XEvent));
168 ev.xclient.type = ClientMessage;
169 ev.xclient.message_type = XInternAtom(WMScreenDisplay(WMWidgetScreen(w)),
170 "_WINDOWMAKER_COMMAND", False);
171 ev.xclient.window = DefaultRootWindow(WMScreenDisplay(WMWidgetScreen(w)));
172 ev.xclient.format = 8;
174 for (i = 0; i <= strlen(msg); i++) {
175 ev.xclient.data.b[i] = msg[i];
177 XSendEvent(WMScreenDisplay(WMWidgetScreen(w)),
178 DefaultRootWindow(WMScreenDisplay(WMWidgetScreen(w))),
179 False, SubstructureRedirectMask, &ev);
180 XFlush(WMScreenDisplay(WMWidgetScreen(w)));
185 static void
186 undo(WMWidget *w, void *data)
188 PanelRec *rec = (PanelRec*)WPrefs.currentPanel;
190 if (!rec)
191 return;
193 if (rec->callbacks.undoChanges
194 && (rec->callbacks.flags & INITIALIZED_PANEL)) {
195 (*rec->callbacks.undoChanges)(WPrefs.currentPanel);
200 static void
201 undoAll(WMWidget *w, void *data)
203 int i;
205 for (i=0; i<WPrefs.sectionCount; i++) {
206 PanelRec *rec = WMGetHangedData(WPrefs.sectionB[i]);
208 if (rec->callbacks.undoChanges
209 && (rec->callbacks.flags & INITIALIZED_PANEL))
210 (*rec->callbacks.undoChanges)((Panel*)rec);
216 static void
217 prepareForClose()
219 int i;
221 for (i=0; i<WPrefs.sectionCount; i++) {
222 PanelRec *rec = WMGetHangedData(WPrefs.sectionB[i]);
224 if (rec->callbacks.prepareForClose
225 && (rec->callbacks.flags & INITIALIZED_PANEL))
226 (*rec->callbacks.prepareForClose)((Panel*)rec);
231 void
232 toggleBalloons(WMWidget *w, void *data)
234 WMUserDefaults *udb = WMGetStandardUserDefaults();
235 Bool flag;
237 flag = WMGetButtonSelected(WPrefs.balloonBtn);
239 WMSetBalloonEnabled(WMWidgetScreen(WPrefs.win), flag);
241 WMSetUDBoolForKey(udb, flag, "BalloonHelp");
245 static void
246 createMainWindow(WMScreen *scr)
248 WMScroller *scroller;
249 WMFont *font;
250 char buffer[128];
252 WPrefs.win = WMCreateWindow(scr, "wprefs");
253 WMResizeWidget(WPrefs.win, 520, 390);
254 WMSetWindowTitle(WPrefs.win, _("Window Maker Preferences"));
255 WMSetWindowCloseAction(WPrefs.win, quit, NULL);
256 WMSetWindowMaxSize(WPrefs.win, 520, 390);
257 WMSetWindowMinSize(WPrefs.win, 520, 390);
258 WMSetWindowMiniwindowTitle(WPrefs.win, "Preferences");
259 WMSetWindowMiniwindowImage(WPrefs.win, WMGetApplicationIconImage(scr));
261 WPrefs.scrollV = WMCreateScrollView(WPrefs.win);
262 WMResizeWidget(WPrefs.scrollV, 500, 87);
263 WMMoveWidget(WPrefs.scrollV, 10, 10);
264 WMSetScrollViewRelief(WPrefs.scrollV, WRSunken);
265 WMSetScrollViewHasHorizontalScroller(WPrefs.scrollV, True);
266 WMSetScrollViewHasVerticalScroller(WPrefs.scrollV, False);
267 scroller = WMGetScrollViewHorizontalScroller(WPrefs.scrollV);
268 WMSetScrollerArrowsPosition(scroller, WSANone);
270 WPrefs.buttonF = WMCreateFrame(WPrefs.win);
271 WMSetFrameRelief(WPrefs.buttonF, WRFlat);
273 WMSetScrollViewContentView(WPrefs.scrollV, WMWidgetView(WPrefs.buttonF));
275 WPrefs.undosBtn = WMCreateCommandButton(WPrefs.win);
276 WMResizeWidget(WPrefs.undosBtn, 90, 28);
277 WMMoveWidget(WPrefs.undosBtn, 135, 350);
278 WMSetButtonText(WPrefs.undosBtn, _("Revert Page"));
279 WMSetButtonAction(WPrefs.undosBtn, undo, NULL);
281 WPrefs.undoBtn = WMCreateCommandButton(WPrefs.win);
282 WMResizeWidget(WPrefs.undoBtn, 90, 28);
283 WMMoveWidget(WPrefs.undoBtn, 235, 350);
284 WMSetButtonText(WPrefs.undoBtn, _("Revert All"));
285 WMSetButtonAction(WPrefs.undoBtn, undoAll, NULL);
287 WPrefs.saveBtn = WMCreateCommandButton(WPrefs.win);
288 WMResizeWidget(WPrefs.saveBtn, 80, 28);
289 WMMoveWidget(WPrefs.saveBtn, 335, 350);
290 WMSetButtonText(WPrefs.saveBtn, _("Save"));
291 WMSetButtonAction(WPrefs.saveBtn, save, NULL);
293 WPrefs.closeBtn = WMCreateCommandButton(WPrefs.win);
294 WMResizeWidget(WPrefs.closeBtn, 80, 28);
295 WMMoveWidget(WPrefs.closeBtn, 425, 350);
296 WMSetButtonText(WPrefs.closeBtn, _("Close"));
297 WMSetButtonAction(WPrefs.closeBtn, quit, NULL);
300 WPrefs.balloonBtn = WMCreateSwitchButton(WPrefs.win);
301 WMResizeWidget(WPrefs.balloonBtn, 200, 28);
302 WMMoveWidget(WPrefs.balloonBtn, 15, 350);
303 WMSetButtonText(WPrefs.balloonBtn, _("Balloon Help"));
304 WMSetButtonAction(WPrefs.balloonBtn, toggleBalloons, NULL);
306 WMUserDefaults *udb = WMGetStandardUserDefaults();
307 Bool flag = WMGetUDBoolForKey(udb, "BalloonHelp");
309 WMSetButtonSelected(WPrefs.balloonBtn, flag);
310 WMSetBalloonEnabled(scr, flag);
313 /* banner */
314 WPrefs.banner = WMCreateFrame(WPrefs.win);
315 WMResizeWidget(WPrefs.banner, FRAME_WIDTH, FRAME_HEIGHT);
316 WMMoveWidget(WPrefs.banner, FRAME_LEFT, FRAME_TOP);
317 WMSetFrameRelief(WPrefs.banner, WRFlat);
319 font = WMCreateFont(scr, "-*-times-bold-r-*-*-24-*-*-*-*-*-*-*,"
320 "-*-fixed-medium-r-normal-*-24-*");
321 if (!font)
322 font = WMBoldSystemFontOfSize(scr, 24);
323 WPrefs.nameL = WMCreateLabel(WPrefs.banner);
324 WMSetLabelTextAlignment(WPrefs.nameL, WACenter);
325 WMResizeWidget(WPrefs.nameL, FRAME_WIDTH-20, 30);
326 WMMoveWidget(WPrefs.nameL, 10, 25);
327 WMSetLabelFont(WPrefs.nameL, font);
328 WMSetLabelText(WPrefs.nameL, _("Window Maker Preferences Utility"));
329 WMReleaseFont(font);
331 WPrefs.versionL = WMCreateLabel(WPrefs.banner);
332 WMResizeWidget(WPrefs.versionL, FRAME_WIDTH-20, 20);
333 WMMoveWidget(WPrefs.versionL, 10, 65);
334 WMSetLabelTextAlignment(WPrefs.versionL, WACenter);
335 sprintf(buffer, _("Version %s for Window Maker %s or newer"), WVERSION,
336 WMVERSION);
337 WMSetLabelText(WPrefs.versionL, buffer);
339 WPrefs.statusL = WMCreateLabel(WPrefs.banner);
340 WMResizeWidget(WPrefs.statusL, FRAME_WIDTH-20, 60);
341 WMMoveWidget(WPrefs.statusL, 10, 100);
342 WMSetLabelTextAlignment(WPrefs.statusL, WACenter);
343 WMSetLabelText(WPrefs.statusL, _("Starting..."));
345 WPrefs.creditsL = WMCreateLabel(WPrefs.banner);
346 WMResizeWidget(WPrefs.creditsL, FRAME_WIDTH-20, 60);
347 WMMoveWidget(WPrefs.creditsL, 10, FRAME_HEIGHT-60);
348 WMSetLabelTextAlignment(WPrefs.creditsL, WACenter);
349 WMSetLabelText(WPrefs.creditsL, _("Programming/Design: Alfredo K. Kojima\n"
350 "Artwork: Marco van Hylckama Vlieg\n"
351 "More Programming: James Thompson"));
354 WMMapSubwidgets(WPrefs.win);
356 WMUnmapWidget(WPrefs.undosBtn);
357 WMUnmapWidget(WPrefs.undoBtn);
358 WMUnmapWidget(WPrefs.saveBtn);
362 static void
363 showPanel(Panel *panel)
365 PanelRec *rec = (PanelRec*)panel;
367 if (!(rec->callbacks.flags & INITIALIZED_PANEL)) {
368 (*rec->callbacks.createWidgets)(panel);
369 rec->callbacks.flags |= INITIALIZED_PANEL;
372 WMSetWindowTitle(WPrefs.win, rec->sectionName);
374 WMMapWidget(rec->frame);
379 static void
380 hidePanel(Panel *panel)
382 PanelRec *rec = (PanelRec*)panel;
384 WMUnmapWidget(rec->frame);
388 static void
389 savePanelData(Panel *panel)
391 PanelRec *rec = (PanelRec*)panel;
393 if (rec->callbacks.updateDomain) {
394 (*rec->callbacks.updateDomain)(panel);
399 static void
400 changeSection(WMWidget *self, void *data)
402 if (WPrefs.banner) {
403 WMDestroyWidget(WPrefs.banner);
404 WPrefs.banner = NULL;
405 /* WMMapWidget(WPrefs.undosBtn);
406 WMMapWidget(WPrefs.undoBtn);
408 WMMapWidget(WPrefs.saveBtn);
411 showPanel(data);
413 if (WPrefs.currentPanel)
414 hidePanel(WPrefs.currentPanel);
415 WPrefs.currentPanel = data;
422 char*
423 LocateImage(char *name)
425 char *path;
426 char *tmp = wmalloc(strlen(name)+8);
428 if (TIFFOK) {
429 sprintf(tmp, "%s.tiff", name);
430 path = WMPathForResourceOfType(tmp, "tiff");
431 } else {
432 sprintf(tmp, "%s.xpm", name);
433 path = WMPathForResourceOfType(tmp, "xpm");
435 free(tmp);
436 if (!path) {
437 wwarning(_("could not locate image file %s\n"), name);
440 return path;
444 void
445 SetButtonAlphaImage(WMScreen *scr, WMButton *bPtr, char *file)
447 WMPixmap *icon;
448 RColor color;
449 char *iconPath;
451 iconPath = LocateImage(file);
453 color.red = 0xae;
454 color.green = 0xaa;
455 color.blue = 0xae;
456 color.alpha = 0;
457 if (iconPath) {
458 icon = WMCreateBlendedPixmapFromFile(scr, iconPath, &color);
459 if (!icon)
460 wwarning(_("could not load icon file %s"), iconPath);
461 } else {
462 icon = NULL;
465 WMSetButtonImage(bPtr, icon);
467 if (icon)
468 WMReleasePixmap(icon);
470 color.red = 0xff;
471 color.green = 0xff;
472 color.blue = 0xff;
473 color.alpha = 0;
474 if (iconPath) {
475 icon = WMCreateBlendedPixmapFromFile(scr, iconPath, &color);
476 if (!icon)
477 wwarning(_("could not load icon file %s"), iconPath);
478 } else {
479 icon = NULL;
482 WMSetButtonAltImage(bPtr, icon);
484 if (icon)
485 WMReleasePixmap(icon);
487 if (iconPath)
488 free(iconPath);
492 void
493 AddSection(Panel *panel, char *iconFile)
495 WMButton *bPtr;
497 assert(WPrefs.sectionCount < MAX_SECTIONS);
500 bPtr = WMCreateCustomButton(WPrefs.buttonF, WBBStateLightMask
501 |WBBStateChangeMask);
502 WMResizeWidget(bPtr, 64, 64);
503 WMMoveWidget(bPtr, WPrefs.sectionCount*64, 0);
504 WMSetButtonImagePosition(bPtr, WIPImageOnly);
505 WMSetButtonAction(bPtr, changeSection, panel);
506 WMHangData(bPtr, panel);
508 WMSetBalloonTextForView(((PanelRec*)panel)->description,
509 WMWidgetView(bPtr));
511 SetButtonAlphaImage(WMWidgetScreen(bPtr), bPtr, iconFile);
513 WMMapWidget(bPtr);
515 WPrefs.sectionB[WPrefs.sectionCount] = bPtr;
517 if (WPrefs.sectionCount > 0) {
518 WMGroupButtons(WPrefs.sectionB[0], bPtr);
521 WPrefs.sectionCount++;
523 WMResizeWidget(WPrefs.buttonF, WPrefs.sectionCount*64, 64);
527 void
528 Initialize(WMScreen *scr)
530 char **list;
531 int i;
532 char *path;
533 WMPixmap *icon;
536 list = RSupportedFileFormats();
537 for (i=0; list[i]!=NULL; i++) {
538 if (strcmp(list[i], "TIFF")==0) {
539 TIFFOK = True;
540 break;
544 if (TIFFOK)
545 path = WMPathForResourceOfType("WPrefs.tiff", NULL);
546 else
547 path = WMPathForResourceOfType("WPrefs.xpm", NULL);
548 if (path) {
549 RImage *tmp;
551 tmp = RLoadImage(WMScreenRContext(scr), path, 0);
552 if (!tmp) {
553 wwarning(_("could not load image file %s:%s"), path,
554 RMessageForError(RErrorCode));
555 } else {
556 icon = WMCreatePixmapFromRImage(scr, tmp, 0);
557 RDestroyImage(tmp);
558 if (icon) {
559 WMSetApplicationIconImage(scr, icon);
560 WMReleasePixmap(icon);
563 free(path);
566 memset(&WPrefs, 0, sizeof(_WPrefs));
567 createMainWindow(scr);
569 WMRealizeWidget(WPrefs.win);
570 WMMapWidget(WPrefs.win);
571 XFlush(WMScreenDisplay(scr));
572 WMSetLabelText(WPrefs.statusL, _("Loading Window Maker configuration files..."));
573 XFlush(WMScreenDisplay(scr));
574 loadConfigurations(scr, WPrefs.win);
576 WMSetLabelText(WPrefs.statusL, _("Initializing configuration panels..."));
578 InitWindowHandling(scr, WPrefs.win);
579 InitFocus(scr, WPrefs.win);
580 InitMenuPreferences(scr, WPrefs.win);
581 InitIcons(scr, WPrefs.win);
582 InitPreferences(scr, WPrefs.win);
584 InitPaths(scr, WPrefs.win);
585 InitWorkspace(scr, WPrefs.win);
586 InitConfigurations(scr, WPrefs.win);
588 InitMenu(scr, WPrefs.win);
590 #ifdef not_yet_fully_implemented
591 InitKeyboardSettings(scr, WPrefs.win);
592 #endif
593 InitKeyboardShortcuts(scr, WPrefs.win);
594 InitMouseSettings(scr, WPrefs.win);
596 InitAppearance(scr, WPrefs.win);
598 #ifdef not_yet_fully_implemented
599 InitText(scr, WPrefs.win);
600 InitThemes(scr, WPrefs.win);
601 #endif
602 InitExpert(scr, WPrefs.win);
604 WMRealizeWidget(WPrefs.scrollV);
606 WMSetLabelText(WPrefs.statusL,
607 _("WPrefs is free software and is distributed WITHOUT ANY\n"
608 "WARRANTY under the terms of the GNU General Public License.\n"
609 "The icons in this program are licensed through the\n"
610 "OpenContent License."));
614 WMWindow*
615 GetWindow(Panel *panel)
617 return WPrefs.win;
621 static void
622 loadConfigurations(WMScreen *scr, WMWindow *mainw)
624 proplist_t db, gdb;
625 char *path;
626 FILE *file;
627 char buffer[1024];
628 char mbuf[1024];
629 int v1, v2, v3;
631 path = wdefaultspathfordomain("WindowMaker");
633 db = PLGetProplistWithPath(path);
634 if (db) {
635 if (!PLIsDictionary(db)) {
636 PLRelease(db);
637 db = NULL;
638 sprintf(mbuf, _("Window Maker domain (%s) is corrupted!"), path);
639 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
641 } else {
642 sprintf(mbuf, _("Could not load Window Maker domain (%s) from defaults database."),
643 path);
644 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
646 free(path);
648 path = getenv("WMAKER_BIN_NAME");
649 if (!path)
650 path = "wmaker";
651 path = wstrappend(path, " --version");
653 file = popen(path, "r");
654 if (!file || !fgets(buffer, 1023, file)) {
655 wsyserror(_("could not extract version information from Window Maker"));
656 wfatal(_("Make sure wmaker is in your search path."));
658 WMRunAlertPanel(scr, mainw, _("Error"),
659 _("Could not extract version from Window Maker. Make sure it is correctly installed and is in your PATH environment variable."),
660 _("OK"), NULL, NULL);
661 exit(1);
663 if (file)
664 pclose(file);
666 if (sscanf(buffer, "Window Maker %i.%i.%i",&v1,&v2,&v3)!=3
667 && sscanf(buffer, "WindowMaker %i.%i.%i",&v1,&v2,&v3)!=3) {
668 WMRunAlertPanel(scr, mainw, _("Error"),
669 _("Could not extract version from Window Maker. "
670 "Make sure it is correctly installed and the path "
671 "where it installed is in the PATH environment "
672 "variable."), _("OK"), NULL, NULL);
673 exit(1);
675 if (v1 == 0 && (v2 < 18 || v3 < 0)) {
676 sprintf(mbuf, _("WPrefs only supports Window Maker 0.18.0 or newer.\n"
677 "The version installed is %i.%i.%i\n"), v1, v2, v3);
678 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
679 exit(1);
682 if (v1 > 1 || (v1 == 1 && (v2 > 0))) {
683 sprintf(mbuf, _("Window Maker %i.%i.%i, which is installed in your system, is not fully supported by this version of WPrefs."),
684 v1, v2, v3);
685 WMRunAlertPanel(scr, mainw, _("Warning"), mbuf, _("OK"), NULL, NULL);
688 file = popen("wmaker --global_defaults_path", "r");
689 if (!file || !fgets(buffer, 1023, file)) {
690 wsyserror(_("could not run \"wmaker --global_defaults_path\"."));
691 exit(1);
692 } else {
693 char *ptr;
694 ptr = strchr(buffer, '\n');
695 if (ptr)
696 *ptr = 0;
697 strcat(buffer, "/WindowMaker");
700 if (file)
701 pclose(file);
703 gdb = PLGetProplistWithPath(buffer);
704 if (gdb) {
705 if (!PLIsDictionary(gdb)) {
706 PLRelease(gdb);
707 gdb = NULL;
708 sprintf(mbuf, _("Window Maker domain (%s) is corrupted!"), buffer);
709 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
711 } else {
712 sprintf(mbuf, _("Could not load global Window Maker domain (%s)."),
713 buffer);
714 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
717 if (!db) {
718 db = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
720 if (!gdb) {
721 gdb = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
724 GlobalDB = gdb;
726 WindowMakerDB = db;
730 proplist_t
731 GetObjectForKey(char *defaultName)
733 proplist_t object = NULL;
734 proplist_t key = PLMakeString(defaultName);
736 object = PLGetDictionaryEntry(WindowMakerDB, key);
737 if (!object)
738 object = PLGetDictionaryEntry(GlobalDB, key);
740 PLRelease(key);
742 return object;
746 void
747 SetObjectForKey(proplist_t object, char *defaultName)
749 proplist_t key = PLMakeString(defaultName);
751 PLInsertDictionaryEntry(WindowMakerDB, key, object);
752 PLRelease(key);
756 void
757 RemoveObjectForKey(char *defaultName)
759 proplist_t key = PLMakeString(defaultName);
761 PLRemoveDictionaryEntry(WindowMakerDB, key);
763 PLRelease(key);
767 char*
768 GetStringForKey(char *defaultName)
770 proplist_t val;
772 val = GetObjectForKey(defaultName);
774 if (!val)
775 return NULL;
777 if (!PLIsString(val))
778 return NULL;
780 return PLGetString(val);
785 proplist_t
786 GetArrayForKey(char *defaultName)
788 proplist_t val;
790 val = GetObjectForKey(defaultName);
792 if (!val)
793 return NULL;
795 if (!PLIsArray(val))
796 return NULL;
798 return val;
802 proplist_t
803 GetDictionaryForKey(char *defaultName)
805 proplist_t val;
807 val = GetObjectForKey(defaultName);
809 if (!val)
810 return NULL;
812 if (!PLIsDictionary(val))
813 return NULL;
815 return val;
820 GetIntegerForKey(char *defaultName)
822 proplist_t val;
823 char *str;
824 int value;
826 val = GetObjectForKey(defaultName);
828 if (!val)
829 return 0;
831 if (!PLIsString(val))
832 return 0;
834 str = PLGetString(val);
835 if (!str)
836 return 0;
838 if (sscanf(str, "%i", &value)!=1)
839 return 0;
841 return value;
845 Bool
846 GetBoolForKey(char *defaultName)
848 int value;
849 char *str;
851 str = GetStringForKey(defaultName);
853 if (!str)
854 return False;
856 if (sscanf(str, "%i", &value)==1 && value!=0)
857 return True;
859 if (strcasecmp(str, "YES")==0)
860 return True;
862 if (strcasecmp(str, "Y")==0)
863 return True;
865 return False;
869 void
870 SetIntegerForKey(int value, char *defaultName)
872 proplist_t object;
873 char buffer[128];
875 sprintf(buffer, "%i", value);
876 object = PLMakeString(buffer);
878 SetObjectForKey(object, defaultName);
879 PLRelease(object);
884 void
885 SetStringForKey(char *value, char *defaultName)
887 proplist_t object;
889 object = PLMakeString(value);
891 SetObjectForKey(object, defaultName);
892 PLRelease(object);
896 void
897 SetBoolForKey(Bool value, char *defaultName)
899 static proplist_t yes = NULL, no = NULL;
901 if (!yes) {
902 yes = PLMakeString("YES");
903 no = PLMakeString("NO");
906 SetObjectForKey(value ? yes : no, defaultName);
910 void
911 SetSpeedForKey(int speed, char *defaultName)
913 char *str;
915 switch (speed) {
916 case 0:
917 str = "ultraslow";
918 break;
919 case 1:
920 str = "slow";
921 break;
922 case 2:
923 str = "medium";
924 break;
925 case 3:
926 str = "fast";
927 break;
928 case 4:
929 str = "ultrafast";
930 break;
931 default:
932 str = NULL;
935 if (str)
936 SetStringForKey(str, defaultName);
941 GetSpeedForKey(char *defaultName)
943 char *str;
944 int i;
946 str = GetStringForKey(defaultName);
947 if (!str)
948 return 2;
950 if (strcasecmp(str, "ultraslow")==0)
951 i = 0;
952 else if (strcasecmp(str, "slow")==0)
953 i = 1;
954 else if (strcasecmp(str, "medium")==0)
955 i = 2;
956 else if (strcasecmp(str, "fast")==0)
957 i = 3;
958 else if (strcasecmp(str, "ultrafast")==0)
959 i = 4;
960 else {
961 wwarning(_("bad speed value for option %s\n. Using default Medium"),
962 defaultName);
963 i = 2;
965 return i;