Code update for Window Maker version 0.50.0
[wmaker-crm.git] / src / wdefaults.c
blob389729b63634e4c35943132182409a64432c4be1
1 /* wdefaults.c - window specific defaults
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 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.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <ctype.h>
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33 #include <X11/keysym.h>
35 #include <wraster.h>
38 #include "WindowMaker.h"
39 #include "window.h"
40 #include "screen.h"
41 #include "funcs.h"
42 #include "workspace.h"
43 #include "defaults.h"
44 #include "icon.h"
47 /* Global stuff */
49 extern WPreferences wPreferences;
51 extern proplist_t wAttributeDomainName;
53 extern WDDomain *WDWindowAttributes;
56 /* Local stuff */
59 /* type converters */
60 static int getBool(proplist_t, proplist_t);
62 static char* getString(proplist_t, proplist_t);
65 static proplist_t ANoTitlebar = NULL;
66 static proplist_t ANoResizebar;
67 static proplist_t ANoMiniaturizeButton;
68 static proplist_t ANoCloseButton;
69 static proplist_t ANoHideOthers;
70 static proplist_t ANoMouseBindings;
71 static proplist_t ANoKeyBindings;
72 static proplist_t ANoAppIcon; /* app */
73 static proplist_t AKeepOnTop;
74 static proplist_t AKeepOnBottom;
75 static proplist_t AOmnipresent;
76 static proplist_t ASkipWindowList;
77 static proplist_t AKeepInsideScreen;
78 static proplist_t AUnfocusable;
79 static proplist_t AAlwaysUserIcon;
80 static proplist_t AStartMiniaturized;
81 static proplist_t AStartMaximized;
82 static proplist_t AStartHidden; /* app */
83 static proplist_t ADontSaveSession; /* app */
84 static proplist_t AEmulateAppIcon;
86 static proplist_t AStartWorkspace;
88 static proplist_t AIcon;
91 static proplist_t AnyWindow;
92 static proplist_t No;
95 static void
96 init_wdefaults(WScreen *scr)
98 AIcon = PLMakeString("Icon");
100 ANoTitlebar = PLMakeString("NoTitlebar");
101 ANoResizebar = PLMakeString("NoResizebar");
102 ANoMiniaturizeButton = PLMakeString("NoMiniaturizeButton");
103 ANoCloseButton = PLMakeString("NoCloseButton");
104 ANoHideOthers = PLMakeString("NoHideOthers");
105 ANoMouseBindings = PLMakeString("NoMouseBindings");
106 ANoKeyBindings = PLMakeString("NoKeyBindings");
107 ANoAppIcon = PLMakeString("NoAppIcon");
108 AKeepOnTop = PLMakeString("KeepOnTop");
109 AKeepOnBottom = PLMakeString("KeepOnBottom");
110 AOmnipresent = PLMakeString("Omnipresent");
111 ASkipWindowList = PLMakeString("SkipWindowList");
112 AKeepInsideScreen = PLMakeString("KeepInsideScreen");
113 AUnfocusable = PLMakeString("Unfocusable");
114 AAlwaysUserIcon = PLMakeString("AlwaysUserIcon");
115 AStartMiniaturized = PLMakeString("StartMiniaturized");
116 AStartHidden = PLMakeString("StartHidden");
117 AStartMaximized = PLMakeString("StartMaximized");
118 ADontSaveSession = PLMakeString("DontSaveSession");
119 AEmulateAppIcon = PLMakeString("EmulateAppIcon");
121 AStartWorkspace = PLMakeString("StartWorkspace");
123 AnyWindow = PLMakeString("*");
124 No = PLMakeString("No");
126 if (!scr->wattribs) {
127 scr->wattribs = PLGetDomain(wAttributeDomainName);
133 static proplist_t
134 get_value(proplist_t dict_win, proplist_t dict_class, proplist_t dict_name,
135 proplist_t dict_any, proplist_t option, proplist_t default_value,
136 Bool useGlobalDefault)
138 proplist_t value;
141 if (dict_win) {
142 value = PLGetDictionaryEntry(dict_win, option);
143 if (value)
144 return value;
147 if (dict_name) {
148 value = PLGetDictionaryEntry(dict_name, option);
149 if (value)
150 return value;
153 if (dict_class) {
154 value = PLGetDictionaryEntry(dict_class, option);
155 if (value)
156 return value;
159 if (!useGlobalDefault)
160 return NULL;
162 if (dict_any) {
163 value = PLGetDictionaryEntry(dict_any, option);
164 if (value)
165 return value;
168 return default_value;
173 *----------------------------------------------------------------------
174 * wDefaultFillAttributes--
175 * Retrieves attributes for the specified instance/class and
176 * fills attr with it. Values that are actually defined are also
177 * set in mask. If useGlobalDefault is True, the default for
178 * all windows ("*") will be used for when no values are found
179 * for that instance/class.
181 *----------------------------------------------------------------------
183 void
184 wDefaultFillAttributes(WScreen *scr, char *instance, char *class,
185 WWindowAttributes *attr,
186 WWindowAttributes *mask,
187 Bool useGlobalDefault)
189 proplist_t value;
190 proplist_t key1, key2, key3;
191 proplist_t dw, dc, dn, da;
192 char buffer[256];
195 if (class && instance)
196 key1 = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
197 else
198 key1 = NULL;
200 if (instance)
201 key2 = PLMakeString(instance);
202 else
203 key2 = NULL;
205 if (class)
206 key3 = PLMakeString(class);
207 else
208 key3 = NULL;
210 if (!ANoTitlebar) {
211 init_wdefaults(scr);
214 PLSetStringCmpHook(NULL);
216 if (WDWindowAttributes->dictionary) {
217 dw = key1 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key1) : NULL;
218 dn = key2 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key2) : NULL;
219 dc = key3 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key3) : NULL;
220 if (useGlobalDefault)
221 da = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
222 else
223 da = NULL;
224 } else {
225 dw = NULL;
226 dn = NULL;
227 dc = NULL;
228 da = NULL;
230 if (key1)
231 PLRelease(key1);
232 if (key2)
233 PLRelease(key2);
234 if (key3)
235 PLRelease(key3);
237 #define APPLY_VAL(value, flag, attrib) \
238 if (value) {attr->flag = getBool(attrib, value); \
239 if (mask) mask->flag = 1;}
241 /* get the data */
242 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
243 APPLY_VAL(value, no_titlebar, ANoTitlebar);
245 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
246 APPLY_VAL(value, no_resizebar, ANoResizebar);
248 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
249 APPLY_VAL(value, no_miniaturize_button, ANoMiniaturizeButton);
251 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
252 APPLY_VAL(value, no_close_button, ANoCloseButton);
254 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
255 APPLY_VAL(value, no_hide_others, ANoHideOthers);
257 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
258 APPLY_VAL(value, no_bind_mouse, ANoMouseBindings);
260 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
261 APPLY_VAL(value, no_bind_keys, ANoKeyBindings);
263 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
264 APPLY_VAL(value, no_appicon, ANoAppIcon);
266 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
267 APPLY_VAL(value, floating, AKeepOnTop);
269 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
270 APPLY_VAL(value, sunken, AKeepOnBottom);
272 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
273 APPLY_VAL(value, omnipresent, AOmnipresent);
275 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
276 APPLY_VAL(value, skip_window_list, ASkipWindowList);
278 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
279 APPLY_VAL(value, dont_move_off, AKeepInsideScreen);
281 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
282 APPLY_VAL(value, no_focusable, AUnfocusable);
284 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
285 APPLY_VAL(value, always_user_icon, AAlwaysUserIcon);
287 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
288 APPLY_VAL(value, start_miniaturized, AStartMiniaturized);
290 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
291 APPLY_VAL(value, start_hidden, AStartHidden);
293 value = get_value(dw, dc, dn, da, AStartMaximized, No, useGlobalDefault);
294 APPLY_VAL(value, start_maximized, AStartMaximized);
296 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
297 APPLY_VAL(value, dont_save_session, ADontSaveSession);
299 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
300 APPLY_VAL(value, emulate_appicon, AEmulateAppIcon);
302 /* clean up */
303 PLSetStringCmpHook(StringCompareHook);
308 proplist_t
309 get_generic_value(WScreen *scr, char *instance, char *class, proplist_t option,
310 Bool noDefault)
312 proplist_t value, key, dict;
313 char buffer[256];
315 value = NULL;
317 PLSetStringCmpHook(NULL);
319 if (class && instance) {
320 key = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
322 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
323 PLRelease(key);
325 if (dict) {
326 value = PLGetDictionaryEntry(dict, option);
330 if (!value && instance) {
331 key = PLMakeString(instance);
333 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
334 PLRelease(key);
336 if (dict) {
337 value = PLGetDictionaryEntry(dict, option);
341 if (!value && class) {
342 key = PLMakeString(class);
344 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
345 PLRelease(key);
347 if (dict) {
348 value = PLGetDictionaryEntry(dict, option);
352 if (!value && !noDefault) {
353 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
355 if (dict) {
356 value = PLGetDictionaryEntry(dict, option);
360 PLSetStringCmpHook(StringCompareHook);
362 return value;
366 char*
367 wDefaultGetIconFile(WScreen *scr, char *instance, char *class,
368 Bool noDefault)
370 proplist_t value;
371 char *tmp;
373 if (!ANoTitlebar) {
374 init_wdefaults(scr);
377 if (!WDWindowAttributes->dictionary)
378 return NULL;
380 value = get_generic_value(scr, instance, class, AIcon, noDefault);
382 if (!value)
383 return NULL;
385 tmp = getString(AIcon, value);
387 return tmp;
391 RImage*
392 wDefaultGetImage(WScreen *scr, char *winstance, char *wclass)
394 char *file_name;
395 char *path;
396 RImage *image;
398 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
399 if (!file_name)
400 return NULL;
402 path = FindImage(wPreferences.icon_path, file_name);
404 if (!path) {
405 wwarning(_("could not find icon file \"%s\""), file_name);
406 return NULL;
409 image = RLoadImage(scr->rcontext, path, 0);
410 if (!image) {
411 wwarning(_("error loading image file \"%s\""), path, RMessageForError(RErrorCode));
413 free(path);
415 image = wIconValidateIconSize(scr, image);
417 return image;
422 wDefaultGetStartWorkspace(WScreen *scr, char *instance, char *class)
424 proplist_t value;
425 int w, i;
426 char *tmp;
428 if (!ANoTitlebar) {
429 init_wdefaults(scr);
432 if (!WDWindowAttributes->dictionary)
433 return -1;
435 value = get_generic_value(scr, instance, class, AStartWorkspace,
436 False);
438 if (!value)
439 return -1;
441 tmp = getString(AStartWorkspace, value);
443 if (!tmp || strlen(tmp)==0)
444 return -1;
446 if (sscanf(tmp, "%i", &w)!=1) {
447 w = -1;
448 for (i=0; i < scr->workspace_count; i++) {
449 if (strcmp(scr->workspaces[i]->name, tmp)==0) {
450 w = i;
451 break;
454 } else {
455 w--;
458 return w;
462 void
463 wDefaultChangeIcon(WScreen *scr, char *instance, char* class, char *file)
465 WDDomain *db = WDWindowAttributes;
466 proplist_t icon_value=NULL, value, attr, key, def_win, def_icon=NULL;
467 proplist_t dict = db->dictionary;
468 char *buffer;
469 int same = 0;
471 if (!dict) {
472 dict = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
473 if (dict) {
474 db->dictionary = dict;
475 value = PLMakeString(db->path);
476 PLSetFilename(dict, value);
477 PLRelease(value);
479 else
480 return;
483 PLSetStringCmpHook(NULL);
485 if (instance && class) {
486 buffer = wmalloc(strlen(instance) + strlen(class) + 2);
487 strcat(strcat(strcpy(buffer, instance), "."), class);
488 key = PLMakeString(buffer);
489 free(buffer);
490 } else if (instance) {
491 key = PLMakeString(instance);
492 } else if (class) {
493 key = PLMakeString(class);
494 } else {
495 key = PLRetain(AnyWindow);
498 if (file) {
499 value = PLMakeString(file);
500 icon_value = PLMakeDictionaryFromEntries(AIcon, value, NULL);
501 PLRelease(value);
503 if ((def_win = PLGetDictionaryEntry(dict, AnyWindow)) != NULL) {
504 def_icon = PLGetDictionaryEntry(def_win, AIcon);
507 if (def_icon && !strcmp(PLGetString(def_icon), file))
508 same = 1;
511 if ((attr = PLGetDictionaryEntry(dict, key)) != NULL) {
512 if (PLIsDictionary(attr)) {
513 if (icon_value!=NULL && !same)
514 PLMergeDictionaries(attr, icon_value);
515 else
516 PLRemoveDictionaryEntry(attr, AIcon);
518 } else if (icon_value!=NULL && !same) {
519 PLInsertDictionaryEntry(dict, key, icon_value);
521 if (!wPreferences.flags.noupdates)
522 PLSave(dict, YES);
524 PLRelease(key);
525 if(icon_value)
526 PLRelease(icon_value);
528 PLSetStringCmpHook(StringCompareHook);
533 /* --------------------------- Local ----------------------- */
535 static int
536 getBool(proplist_t key, proplist_t value)
538 char *val;
540 if (!PLIsString(value)) {
541 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
542 PLGetString(key), "Boolean");
543 return 0;
545 val = PLGetString(value);
547 if ((val[1]=='\0' && (val[0]=='y' || val[0]=='Y' || val[0]=='T'
548 || val[0]=='t' || val[0]=='1'))
549 || (strcasecmp(val, "YES")==0 || strcasecmp(val, "TRUE")==0)) {
551 return 1;
552 } else if ((val[1]=='\0'
553 && (val[0]=='n' || val[0]=='N' || val[0]=='F'
554 || val[0]=='f' || val[0]=='0'))
555 || (strcasecmp(val, "NO")==0 || strcasecmp(val, "FALSE")==0)) {
557 return 0;
558 } else {
559 wwarning(_("can't convert \"%s\" to boolean"), val);
560 /* We return False if we can't convert to BOOLEAN.
561 * This is because all options defaults to False.
562 * -1 is not checked and thus is interpreted as True,
563 * which is not good.*/
564 return 0;
571 * WARNING: Do not free value returned by this!!
573 static char*
574 getString(proplist_t key, proplist_t value)
576 if (!PLIsString(value)) {
577 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
578 PLGetString(key), "String");
579 return NULL;
582 return PLGetString(value);