added box widget
[wmaker-crm.git] / WPrefs.app / WPrefs.c
blobee534a8a9444ed66a622a47da7bef97b19dc5ad6
1 /* WPrefs.c- main window and other basic stuff
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 1998-2000 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 *InitFont(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);
63 #define ICON_TITLE_FONT "-adobe-helvetica-bold-r-*-*-10-*"
64 #define ICON_TITLE_VFONT "-adobe-helvetica-bold-r-*-*-10-[]-*"
67 #define MAX_SECTIONS 16
70 typedef struct _WPrefs {
71 WMWindow *win;
73 WMScrollView *scrollV;
74 WMFrame *buttonF;
75 WMButton *sectionB[MAX_SECTIONS];
77 int sectionCount;
79 WMButton *saveBtn;
80 WMButton *closeBtn;
81 WMButton *undoBtn;
82 WMButton *undosBtn;
84 WMButton *balloonBtn;
86 WMFrame *banner;
87 WMLabel *nameL;
88 WMLabel *versionL;
89 WMLabel *creditsL;
90 WMLabel *statusL;
92 Panel *currentPanel;
93 } _WPrefs;
96 static _WPrefs WPrefs;
98 /* system wide defaults dictionary. Read-only */
99 static proplist_t GlobalDB = NULL;
100 /* user defaults dictionary */
101 static proplist_t WindowMakerDB = NULL;
104 static Bool TIFFOK = False;
107 #define INITIALIZED_PANEL (1<<0)
112 static void loadConfigurations(WMScreen *scr, WMWindow *mainw);
114 static void savePanelData(Panel *panel);
116 static void prepareForClose();
118 void
119 quit(WMWidget *w, void *data)
121 prepareForClose();
123 exit(0);
127 static void
128 save(WMWidget *w, void *data)
130 int i;
131 proplist_t p1, p2;
132 proplist_t keyList;
133 proplist_t key;
134 char *msg = "Reconfigure";
135 XEvent ev;
138 /* puts("gathering data");*/
139 for (i=0; i<WPrefs.sectionCount; i++) {
140 PanelRec *rec = WMGetHangedData(WPrefs.sectionB[i]);
141 if ((rec->callbacks.flags & INITIALIZED_PANEL))
142 savePanelData((Panel*)rec);
144 /* puts("compressing data");*/
145 /* compare the user dictionary with the global and remove redundant data */
146 keyList = PLGetAllDictionaryKeys(GlobalDB);
147 /* puts(PLGetDescription(WindowMakerDB));*/
148 for (i=0; i<PLGetNumberOfElements(keyList); i++) {
149 key = PLGetArrayElement(keyList, i);
151 /* We don't have this value anyway, so no problem.
152 * Probably a new option */
153 p1 = PLGetDictionaryEntry(WindowMakerDB, key);
154 if (!p1)
155 continue;
156 /* The global doesn't have it, so no problem either. */
157 p2 = PLGetDictionaryEntry(GlobalDB, key);
158 if (!p2)
159 continue;
160 /* If both values are the same, don't save. */
161 if (PLIsEqual(p1, p2))
162 PLRemoveDictionaryEntry(WindowMakerDB, key);
164 /* puts(PLGetDescription(WindowMakerDB));*/
165 PLRelease(keyList);
166 /* puts("storing data");*/
168 PLSave(WindowMakerDB, YES);
171 memset(&ev, 0, sizeof(XEvent));
173 ev.xclient.type = ClientMessage;
174 ev.xclient.message_type = XInternAtom(WMScreenDisplay(WMWidgetScreen(w)),
175 "_WINDOWMAKER_COMMAND", False);
176 ev.xclient.window = DefaultRootWindow(WMScreenDisplay(WMWidgetScreen(w)));
177 ev.xclient.format = 8;
179 for (i = 0; i <= strlen(msg); i++) {
180 ev.xclient.data.b[i] = msg[i];
182 XSendEvent(WMScreenDisplay(WMWidgetScreen(w)),
183 DefaultRootWindow(WMScreenDisplay(WMWidgetScreen(w))),
184 False, SubstructureRedirectMask, &ev);
185 XFlush(WMScreenDisplay(WMWidgetScreen(w)));
190 static void
191 undo(WMWidget *w, void *data)
193 PanelRec *rec = (PanelRec*)WPrefs.currentPanel;
195 if (!rec)
196 return;
198 if (rec->callbacks.undoChanges
199 && (rec->callbacks.flags & INITIALIZED_PANEL)) {
200 (*rec->callbacks.undoChanges)(WPrefs.currentPanel);
205 static void
206 undoAll(WMWidget *w, void *data)
208 int i;
210 for (i=0; i<WPrefs.sectionCount; i++) {
211 PanelRec *rec = WMGetHangedData(WPrefs.sectionB[i]);
213 if (rec->callbacks.undoChanges
214 && (rec->callbacks.flags & INITIALIZED_PANEL))
215 (*rec->callbacks.undoChanges)((Panel*)rec);
221 static void
222 prepareForClose()
224 int i;
226 for (i=0; i<WPrefs.sectionCount; i++) {
227 PanelRec *rec = WMGetHangedData(WPrefs.sectionB[i]);
229 if (rec->callbacks.prepareForClose
230 && (rec->callbacks.flags & INITIALIZED_PANEL))
231 (*rec->callbacks.prepareForClose)((Panel*)rec);
236 void
237 toggleBalloons(WMWidget *w, void *data)
239 WMUserDefaults *udb = WMGetStandardUserDefaults();
240 Bool flag;
242 flag = WMGetButtonSelected(WPrefs.balloonBtn);
244 WMSetBalloonEnabled(WMWidgetScreen(WPrefs.win), flag);
246 WMSetUDBoolForKey(udb, flag, "BalloonHelp");
250 static void
251 createMainWindow(WMScreen *scr)
253 WMScroller *scroller;
254 WMFont *font;
255 char buffer[128];
257 WPrefs.win = WMCreateWindow(scr, "wprefs");
258 WMResizeWidget(WPrefs.win, 520, 390);
259 WMSetWindowTitle(WPrefs.win, _("Window Maker Preferences"));
260 WMSetWindowCloseAction(WPrefs.win, quit, NULL);
261 WMSetWindowMaxSize(WPrefs.win, 520, 390);
262 WMSetWindowMinSize(WPrefs.win, 520, 390);
263 WMSetWindowMiniwindowTitle(WPrefs.win, "Preferences");
264 WMSetWindowMiniwindowImage(WPrefs.win, WMGetApplicationIconImage(scr));
266 WPrefs.scrollV = WMCreateScrollView(WPrefs.win);
267 WMResizeWidget(WPrefs.scrollV, 500, 87);
268 WMMoveWidget(WPrefs.scrollV, 10, 10);
269 WMSetScrollViewRelief(WPrefs.scrollV, WRSunken);
270 WMSetScrollViewHasHorizontalScroller(WPrefs.scrollV, True);
271 WMSetScrollViewHasVerticalScroller(WPrefs.scrollV, False);
272 scroller = WMGetScrollViewHorizontalScroller(WPrefs.scrollV);
273 WMSetScrollerArrowsPosition(scroller, WSANone);
275 WPrefs.buttonF = WMCreateFrame(WPrefs.win);
276 WMSetFrameRelief(WPrefs.buttonF, WRFlat);
278 WMSetScrollViewContentView(WPrefs.scrollV, WMWidgetView(WPrefs.buttonF));
280 WPrefs.undosBtn = WMCreateCommandButton(WPrefs.win);
281 WMResizeWidget(WPrefs.undosBtn, 90, 28);
282 WMMoveWidget(WPrefs.undosBtn, 135, 350);
283 WMSetButtonText(WPrefs.undosBtn, _("Revert Page"));
284 WMSetButtonAction(WPrefs.undosBtn, undo, NULL);
286 WPrefs.undoBtn = WMCreateCommandButton(WPrefs.win);
287 WMResizeWidget(WPrefs.undoBtn, 90, 28);
288 WMMoveWidget(WPrefs.undoBtn, 235, 350);
289 WMSetButtonText(WPrefs.undoBtn, _("Revert All"));
290 WMSetButtonAction(WPrefs.undoBtn, undoAll, NULL);
292 WPrefs.saveBtn = WMCreateCommandButton(WPrefs.win);
293 WMResizeWidget(WPrefs.saveBtn, 80, 28);
294 WMMoveWidget(WPrefs.saveBtn, 335, 350);
295 WMSetButtonText(WPrefs.saveBtn, _("Save"));
296 WMSetButtonAction(WPrefs.saveBtn, save, NULL);
298 WPrefs.closeBtn = WMCreateCommandButton(WPrefs.win);
299 WMResizeWidget(WPrefs.closeBtn, 80, 28);
300 WMMoveWidget(WPrefs.closeBtn, 425, 350);
301 WMSetButtonText(WPrefs.closeBtn, _("Close"));
302 WMSetButtonAction(WPrefs.closeBtn, quit, NULL);
305 WPrefs.balloonBtn = WMCreateSwitchButton(WPrefs.win);
306 WMResizeWidget(WPrefs.balloonBtn, 200, 28);
307 WMMoveWidget(WPrefs.balloonBtn, 15, 350);
308 WMSetButtonText(WPrefs.balloonBtn, _("Balloon Help"));
309 WMSetButtonAction(WPrefs.balloonBtn, toggleBalloons, NULL);
311 WMUserDefaults *udb = WMGetStandardUserDefaults();
312 Bool flag = WMGetUDBoolForKey(udb, "BalloonHelp");
314 WMSetButtonSelected(WPrefs.balloonBtn, flag);
315 WMSetBalloonEnabled(scr, flag);
318 /* banner */
319 WPrefs.banner = WMCreateFrame(WPrefs.win);
320 WMResizeWidget(WPrefs.banner, FRAME_WIDTH, FRAME_HEIGHT);
321 WMMoveWidget(WPrefs.banner, FRAME_LEFT, FRAME_TOP);
322 WMSetFrameRelief(WPrefs.banner, WRFlat);
324 font = WMCreateFont(scr, "-*-times-bold-r-*-*-24-*-*-*-*-*-*-*,"
325 "-*-fixed-medium-r-normal-*-24-*");
326 if (!font)
327 font = WMBoldSystemFontOfSize(scr, 24);
328 WPrefs.nameL = WMCreateLabel(WPrefs.banner);
329 WMSetLabelTextAlignment(WPrefs.nameL, WACenter);
330 WMResizeWidget(WPrefs.nameL, FRAME_WIDTH-20, 30);
331 WMMoveWidget(WPrefs.nameL, 10, 25);
332 WMSetLabelFont(WPrefs.nameL, font);
333 WMSetLabelText(WPrefs.nameL, _("Window Maker Preferences Utility"));
334 WMReleaseFont(font);
336 WPrefs.versionL = WMCreateLabel(WPrefs.banner);
337 WMResizeWidget(WPrefs.versionL, FRAME_WIDTH-20, 20);
338 WMMoveWidget(WPrefs.versionL, 10, 65);
339 WMSetLabelTextAlignment(WPrefs.versionL, WACenter);
340 sprintf(buffer, _("Version %s for Window Maker %s or newer"), WVERSION,
341 WMVERSION);
342 WMSetLabelText(WPrefs.versionL, buffer);
344 WPrefs.statusL = WMCreateLabel(WPrefs.banner);
345 WMResizeWidget(WPrefs.statusL, FRAME_WIDTH-20, 60);
346 WMMoveWidget(WPrefs.statusL, 10, 100);
347 WMSetLabelTextAlignment(WPrefs.statusL, WACenter);
348 WMSetLabelText(WPrefs.statusL, _("Starting..."));
350 WPrefs.creditsL = WMCreateLabel(WPrefs.banner);
351 WMResizeWidget(WPrefs.creditsL, FRAME_WIDTH-20, 60);
352 WMMoveWidget(WPrefs.creditsL, 10, FRAME_HEIGHT-60);
353 WMSetLabelTextAlignment(WPrefs.creditsL, WACenter);
354 WMSetLabelText(WPrefs.creditsL, _("Programming/Design: Alfredo K. Kojima\n"
355 "Artwork: Marco van Hylckama Vlieg and Largo\n"
356 "More Programming: James Thompson"));
359 WMMapSubwidgets(WPrefs.win);
361 WMUnmapWidget(WPrefs.undosBtn);
362 WMUnmapWidget(WPrefs.undoBtn);
363 WMUnmapWidget(WPrefs.saveBtn);
367 static void
368 showPanel(Panel *panel)
370 PanelRec *rec = (PanelRec*)panel;
372 if (!(rec->callbacks.flags & INITIALIZED_PANEL)) {
373 (*rec->callbacks.createWidgets)(panel);
374 rec->callbacks.flags |= INITIALIZED_PANEL;
377 WMSetWindowTitle(WPrefs.win, rec->sectionName);
379 if (rec->callbacks.showPanel)
380 (*rec->callbacks.showPanel)(panel);
382 WMMapWidget(rec->frame);
387 static void
388 hidePanel(Panel *panel)
390 PanelRec *rec = (PanelRec*)panel;
392 WMUnmapWidget(rec->frame);
394 if (rec->callbacks.hidePanel)
395 (*rec->callbacks.hidePanel)(panel);
399 static void
400 savePanelData(Panel *panel)
402 PanelRec *rec = (PanelRec*)panel;
404 if (rec->callbacks.updateDomain) {
405 (*rec->callbacks.updateDomain)(panel);
410 static void
411 changeSection(WMWidget *self, void *data)
413 if (WPrefs.banner) {
414 WMDestroyWidget(WPrefs.banner);
415 WPrefs.banner = NULL;
416 /* WMMapWidget(WPrefs.undosBtn);
417 WMMapWidget(WPrefs.undoBtn);
419 WMMapWidget(WPrefs.saveBtn);
422 showPanel(data);
424 if (WPrefs.currentPanel)
425 hidePanel(WPrefs.currentPanel);
426 WPrefs.currentPanel = data;
433 char*
434 LocateImage(char *name)
436 char *path;
437 char *tmp = wmalloc(strlen(name)+8);
439 if (TIFFOK) {
440 sprintf(tmp, "%s.tiff", name);
441 path = WMPathForResourceOfType(tmp, "tiff");
442 } else {
443 sprintf(tmp, "%s.xpm", name);
444 path = WMPathForResourceOfType(tmp, "xpm");
446 wfree(tmp);
447 if (!path) {
448 wwarning(_("could not locate image file %s\n"), name);
451 return path;
456 static WMPixmap*
457 makeTitledIcon(WMScreen *scr, WMPixmap *icon, char *title1, char *title2)
459 return WMRetainPixmap(icon);
461 #if 0
462 static GC gc = NULL;
463 static XFontStruct *hfont = NULL;
464 static XFontStruct *vfont = NULL;
465 WMPixmap *tmp;
466 Pixmap pix, mask;
467 Display *dpy = WMScreenDisplay(scr);
468 WMColor *black = WMBlackColor(scr);
469 GC fgc;
470 WMSize size = WMGetPixmapSize(icon);
473 tmp = WMCreatePixmap(scr, 60, 60, WMScreenDepth(scr), True);
475 pix = WMGetPixmapXID(tmp);
476 mask = WMGetPixmapMaskXID(tmp);
478 if (gc == NULL) {
479 gc = XCreateGC(dpy, mask, 0, NULL);
481 hfont = XLoadQueryFont(dpy, ICON_TITLE_FONT);
482 vfont = XLoadQueryFont(dpy, ICON_TITLE_VFONT);
485 if (hfont == NULL) {
486 return WMRetainPixmap(icon);
489 XSetForeground(dpy, gc, 0);
490 XFillRectangle(dpy, mask, gc, 0, 0, 60, 60);
492 fgc = WMColorGC(black);
494 XSetForeground(dpy, gc, 1);
496 XCopyArea(dpy, WMGetPixmapXID(icon), pix, fgc, 0, 0,
497 size.width, size.height, 12, 12);
499 if (WMGetPixmapMaskXID(icon) != None)
500 XCopyPlane(dpy, WMGetPixmapMaskXID(icon), mask, gc, 0, 0,
501 size.width, size.height, 12, 12, 1);
502 else
503 XFillRectangle(dpy, mask, gc, 12, 12, 48, 48);
506 if (title1) {
507 XSetFont(dpy, fgc, vfont->fid);
508 XSetFont(dpy, gc, vfont->fid);
510 XDrawString(dpy, pix, fgc, 0, vfont->ascent,
511 title1, strlen(title1));
513 XDrawString(dpy, mask, gc, 0, vfont->ascent,
514 title1, strlen(title1));
517 if (title2) {
518 XSetFont(dpy, fgc, hfont->fid);
519 XSetFont(dpy, gc, hfont->fid);
521 XDrawString(dpy, pix, fgc, (title1 ? 12 : 0), hfont->ascent,
522 title2, strlen(title2));
524 XDrawString(dpy, mask, gc, (title1 ? 12 : 0), hfont->ascent,
525 title2, strlen(title2));
528 return tmp;
529 #endif
533 void
534 SetButtonAlphaImage(WMScreen *scr, WMButton *bPtr, char *file,
535 char *title1, char *title2)
537 WMPixmap *icon;
538 WMPixmap *icon2;
539 RColor color;
540 char *iconPath;
542 iconPath = LocateImage(file);
544 color.red = 0xae;
545 color.green = 0xaa;
546 color.blue = 0xae;
547 color.alpha = 0;
548 if (iconPath) {
549 icon = WMCreateBlendedPixmapFromFile(scr, iconPath, &color);
550 if (!icon)
551 wwarning(_("could not load icon file %s"), iconPath);
552 } else {
553 icon = NULL;
556 if (icon) {
557 icon2 = makeTitledIcon(scr, icon, title1, title2);
558 if (icon)
559 WMReleasePixmap(icon);
560 } else {
561 icon2 = NULL;
564 WMSetButtonImage(bPtr, icon2);
566 if (icon2)
567 WMReleasePixmap(icon2);
569 color.red = 0xff;
570 color.green = 0xff;
571 color.blue = 0xff;
572 color.alpha = 0;
573 if (iconPath) {
574 icon = WMCreateBlendedPixmapFromFile(scr, iconPath, &color);
575 if (!icon)
576 wwarning(_("could not load icon file %s"), iconPath);
577 } else {
578 icon = NULL;
581 WMSetButtonAltImage(bPtr, icon);
583 if (icon)
584 WMReleasePixmap(icon);
586 if (iconPath)
587 wfree(iconPath);
591 void
592 AddSection(Panel *panel, char *iconFile)
594 WMButton *bPtr;
596 assert(WPrefs.sectionCount < MAX_SECTIONS);
599 bPtr = WMCreateCustomButton(WPrefs.buttonF, WBBStateLightMask
600 |WBBStateChangeMask);
601 WMResizeWidget(bPtr, 64, 64);
602 WMMoveWidget(bPtr, WPrefs.sectionCount*64, 0);
603 WMSetButtonImagePosition(bPtr, WIPImageOnly);
604 WMSetButtonAction(bPtr, changeSection, panel);
605 WMHangData(bPtr, panel);
607 WMSetBalloonTextForView(((PanelRec*)panel)->description,
608 WMWidgetView(bPtr));
611 char *t1, *t2;
613 t1 = wstrdup(((PanelRec*)panel)->sectionName);
614 t2 = strchr(t1, ' ');
615 if (t2) {
616 *t2 = 0;
617 t2++;
619 SetButtonAlphaImage(WMWidgetScreen(bPtr), bPtr, iconFile,
620 t1, t2);
621 wfree(t1);
623 WMMapWidget(bPtr);
625 WPrefs.sectionB[WPrefs.sectionCount] = bPtr;
627 if (WPrefs.sectionCount > 0) {
628 WMGroupButtons(WPrefs.sectionB[0], bPtr);
631 WPrefs.sectionCount++;
633 WMResizeWidget(WPrefs.buttonF, WPrefs.sectionCount*64, 64);
637 void
638 Initialize(WMScreen *scr)
640 char **list;
641 int i;
642 char *path;
643 WMPixmap *icon;
646 list = RSupportedFileFormats();
647 for (i=0; list[i]!=NULL; i++) {
648 if (strcmp(list[i], "TIFF")==0) {
649 TIFFOK = True;
650 break;
654 if (TIFFOK)
655 path = WMPathForResourceOfType("WPrefs.tiff", NULL);
656 else
657 path = WMPathForResourceOfType("WPrefs.xpm", NULL);
658 if (path) {
659 RImage *tmp;
661 tmp = RLoadImage(WMScreenRContext(scr), path, 0);
662 if (!tmp) {
663 wwarning(_("could not load image file %s:%s"), path,
664 RMessageForError(RErrorCode));
665 } else {
666 icon = WMCreatePixmapFromRImage(scr, tmp, 0);
667 RDestroyImage(tmp);
668 if (icon) {
669 WMSetApplicationIconImage(scr, icon);
670 WMReleasePixmap(icon);
673 wfree(path);
676 memset(&WPrefs, 0, sizeof(_WPrefs));
677 createMainWindow(scr);
679 WMRealizeWidget(WPrefs.win);
680 WMMapWidget(WPrefs.win);
681 XFlush(WMScreenDisplay(scr));
682 WMSetLabelText(WPrefs.statusL, _("Loading Window Maker configuration files..."));
683 XFlush(WMScreenDisplay(scr));
684 loadConfigurations(scr, WPrefs.win);
686 WMSetLabelText(WPrefs.statusL, _("Initializing configuration panels..."));
688 InitWindowHandling(scr, WPrefs.win);
689 InitFocus(scr, WPrefs.win);
690 InitMenuPreferences(scr, WPrefs.win);
691 InitIcons(scr, WPrefs.win);
692 InitPreferences(scr, WPrefs.win);
694 InitPaths(scr, WPrefs.win);
695 InitWorkspace(scr, WPrefs.win);
696 InitConfigurations(scr, WPrefs.win);
698 InitMenu(scr, WPrefs.win);
700 #ifdef not_yet_fully_implemented
701 InitKeyboardSettings(scr, WPrefs.win);
702 #endif
703 InitKeyboardShortcuts(scr, WPrefs.win);
704 InitMouseSettings(scr, WPrefs.win);
706 InitAppearance(scr, WPrefs.win);
708 #ifdef akk
709 InitFont(scr, WPrefs.win);
710 #endif
711 #ifdef not_yet_fully_implemented
712 InitThemes(scr, WPrefs.win);
713 #endif
714 InitExpert(scr, WPrefs.win);
716 WMRealizeWidget(WPrefs.scrollV);
718 WMSetLabelText(WPrefs.statusL,
719 _("WPrefs is free software and is distributed WITHOUT ANY\n"
720 "WARRANTY under the terms of the GNU General Public License."));
724 WMWindow*
725 GetWindow(Panel *panel)
727 return WPrefs.win;
731 static void
732 loadConfigurations(WMScreen *scr, WMWindow *mainw)
734 proplist_t db, gdb;
735 char *path;
736 FILE *file;
737 char buffer[1024];
738 char mbuf[1024];
739 int v1, v2, v3;
741 path = wdefaultspathfordomain("WindowMaker");
743 db = PLGetProplistWithPath(path);
744 if (db) {
745 if (!PLIsDictionary(db)) {
746 PLRelease(db);
747 db = NULL;
748 sprintf(mbuf, _("Window Maker domain (%s) is corrupted!"), path);
749 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
751 } else {
752 sprintf(mbuf, _("Could not load Window Maker domain (%s) from defaults database."),
753 path);
754 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
756 wfree(path);
758 path = getenv("WMAKER_BIN_NAME");
759 if (!path)
760 path = "wmaker";
762 char *command;
764 command = wstrconcat(path, " --version");
765 file = popen(command, "r");
766 wfree(command);
768 if (!file || !fgets(buffer, 1023, file)) {
769 wsyserror(_("could not extract version information from Window Maker"));
770 wfatal(_("Make sure wmaker is in your search path."));
772 WMRunAlertPanel(scr, mainw, _("Error"),
773 _("Could not extract version from Window Maker. Make sure it is correctly installed and is in your PATH environment variable."),
774 _("OK"), NULL, NULL);
775 exit(1);
777 if (file)
778 pclose(file);
780 if (sscanf(buffer, "Window Maker %i.%i.%i",&v1,&v2,&v3)!=3
781 && sscanf(buffer, "WindowMaker %i.%i.%i",&v1,&v2,&v3)!=3) {
782 WMRunAlertPanel(scr, mainw, _("Error"),
783 _("Could not extract version from Window Maker. "
784 "Make sure it is correctly installed and the path "
785 "where it installed is in the PATH environment "
786 "variable."), _("OK"), NULL, NULL);
787 exit(1);
789 if (v1 == 0 && (v2 < 18 || v3 < 0)) {
790 sprintf(mbuf, _("WPrefs only supports Window Maker 0.18.0 or newer.\n"
791 "The version installed is %i.%i.%i\n"), v1, v2, v3);
792 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
793 exit(1);
796 if (v1 > 1 || (v1 == 1 && (v2 > 0))) {
797 sprintf(mbuf, _("Window Maker %i.%i.%i, which is installed in your system, is not fully supported by this version of WPrefs."),
798 v1, v2, v3);
799 WMRunAlertPanel(scr, mainw, _("Warning"), mbuf, _("OK"), NULL, NULL);
803 char *command;
805 command = wstrconcat(path, " --global_defaults_path");
806 file = popen(command, "r");
807 wfree(command);
809 if (!file || !fgets(buffer, 1023, file)) {
810 wsyserror(_("could not run \"%s --global_defaults_path\"."), path);
811 exit(1);
812 } else {
813 char *ptr;
814 ptr = strchr(buffer, '\n');
815 if (ptr)
816 *ptr = 0;
817 strcat(buffer, "/WindowMaker");
820 if (file)
821 pclose(file);
823 gdb = PLGetProplistWithPath(buffer);
824 if (gdb) {
825 if (!PLIsDictionary(gdb)) {
826 PLRelease(gdb);
827 gdb = NULL;
828 sprintf(mbuf, _("Window Maker domain (%s) is corrupted!"), buffer);
829 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
831 } else {
832 sprintf(mbuf, _("Could not load global Window Maker domain (%s)."),
833 buffer);
834 WMRunAlertPanel(scr, mainw, _("Error"), mbuf, _("OK"), NULL, NULL);
837 if (!db) {
838 db = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
840 if (!gdb) {
841 gdb = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
844 GlobalDB = gdb;
846 WindowMakerDB = db;
850 proplist_t
851 GetObjectForKey(char *defaultName)
853 proplist_t object = NULL;
854 proplist_t key = PLMakeString(defaultName);
856 object = PLGetDictionaryEntry(WindowMakerDB, key);
857 if (!object)
858 object = PLGetDictionaryEntry(GlobalDB, key);
860 PLRelease(key);
862 return object;
866 void
867 SetObjectForKey(proplist_t object, char *defaultName)
869 proplist_t key = PLMakeString(defaultName);
871 PLInsertDictionaryEntry(WindowMakerDB, key, object);
872 PLRelease(key);
876 void
877 RemoveObjectForKey(char *defaultName)
879 proplist_t key = PLMakeString(defaultName);
881 PLRemoveDictionaryEntry(WindowMakerDB, key);
883 PLRelease(key);
887 char*
888 GetStringForKey(char *defaultName)
890 proplist_t val;
892 val = GetObjectForKey(defaultName);
894 if (!val)
895 return NULL;
897 if (!PLIsString(val))
898 return NULL;
900 return PLGetString(val);
905 proplist_t
906 GetArrayForKey(char *defaultName)
908 proplist_t val;
910 val = GetObjectForKey(defaultName);
912 if (!val)
913 return NULL;
915 if (!PLIsArray(val))
916 return NULL;
918 return val;
922 proplist_t
923 GetDictionaryForKey(char *defaultName)
925 proplist_t val;
927 val = GetObjectForKey(defaultName);
929 if (!val)
930 return NULL;
932 if (!PLIsDictionary(val))
933 return NULL;
935 return val;
940 GetIntegerForKey(char *defaultName)
942 proplist_t val;
943 char *str;
944 int value;
946 val = GetObjectForKey(defaultName);
948 if (!val)
949 return 0;
951 if (!PLIsString(val))
952 return 0;
954 str = PLGetString(val);
955 if (!str)
956 return 0;
958 if (sscanf(str, "%i", &value)!=1)
959 return 0;
961 return value;
965 Bool
966 GetBoolForKey(char *defaultName)
968 int value;
969 char *str;
971 str = GetStringForKey(defaultName);
973 if (!str)
974 return False;
976 if (sscanf(str, "%i", &value)==1 && value!=0)
977 return True;
979 if (strcasecmp(str, "YES")==0)
980 return True;
982 if (strcasecmp(str, "Y")==0)
983 return True;
985 return False;
989 void
990 SetIntegerForKey(int value, char *defaultName)
992 proplist_t object;
993 char buffer[128];
995 sprintf(buffer, "%i", value);
996 object = PLMakeString(buffer);
998 SetObjectForKey(object, defaultName);
999 PLRelease(object);
1004 void
1005 SetStringForKey(char *value, char *defaultName)
1007 proplist_t object;
1009 object = PLMakeString(value);
1011 SetObjectForKey(object, defaultName);
1012 PLRelease(object);
1016 void
1017 SetBoolForKey(Bool value, char *defaultName)
1019 static proplist_t yes = NULL, no = NULL;
1021 if (!yes) {
1022 yes = PLMakeString("YES");
1023 no = PLMakeString("NO");
1026 SetObjectForKey(value ? yes : no, defaultName);
1030 void
1031 SetSpeedForKey(int speed, char *defaultName)
1033 char *str;
1035 switch (speed) {
1036 case 0:
1037 str = "ultraslow";
1038 break;
1039 case 1:
1040 str = "slow";
1041 break;
1042 case 2:
1043 str = "medium";
1044 break;
1045 case 3:
1046 str = "fast";
1047 break;
1048 case 4:
1049 str = "ultrafast";
1050 break;
1051 default:
1052 str = NULL;
1055 if (str)
1056 SetStringForKey(str, defaultName);
1061 GetSpeedForKey(char *defaultName)
1063 char *str;
1064 int i;
1066 str = GetStringForKey(defaultName);
1067 if (!str)
1068 return 2;
1070 if (strcasecmp(str, "ultraslow")==0)
1071 i = 0;
1072 else if (strcasecmp(str, "slow")==0)
1073 i = 1;
1074 else if (strcasecmp(str, "medium")==0)
1075 i = 2;
1076 else if (strcasecmp(str, "fast")==0)
1077 i = 3;
1078 else if (strcasecmp(str, "ultrafast")==0)
1079 i = 4;
1080 else {
1081 wwarning(_("bad speed value for option %s\n. Using default Medium"),
1082 defaultName);
1083 i = 2;
1085 return i;