This update includes the 0.20.3pre3 code
[wmaker-crm.git] / src / wdefaults.c
blob9e73149a700a54f41fa3ce23a8953321b615ca03
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 AStartHidden; /* app */
82 static proplist_t ADontSaveSession; /* app */
83 static proplist_t AEmulateAppIcon;
85 static proplist_t AStartWorkspace;
87 static proplist_t AIcon;
90 static proplist_t AnyWindow;
91 static proplist_t No;
94 static void
95 init_wdefaults(WScreen *scr)
97 AIcon = PLMakeString("Icon");
99 ANoTitlebar = PLMakeString("NoTitlebar");
100 ANoResizebar = PLMakeString("NoResizebar");
101 ANoMiniaturizeButton = PLMakeString("NoMiniaturizeButton");
102 ANoCloseButton = PLMakeString("NoCloseButton");
103 ANoHideOthers = PLMakeString("NoHideOthers");
104 ANoMouseBindings = PLMakeString("NoMouseBindings");
105 ANoKeyBindings = PLMakeString("NoKeyBindings");
106 ANoAppIcon = PLMakeString("NoAppIcon");
107 AKeepOnTop = PLMakeString("KeepOnTop");
108 AKeepOnBottom = PLMakeString("KeepOnBottom");
109 AOmnipresent = PLMakeString("Omnipresent");
110 ASkipWindowList = PLMakeString("SkipWindowList");
111 AKeepInsideScreen = PLMakeString("KeepInsideScreen");
112 AUnfocusable = PLMakeString("Unfocusable");
113 AAlwaysUserIcon = PLMakeString("AlwaysUserIcon");
114 AStartMiniaturized = PLMakeString("StartMiniaturized");
115 AStartHidden = PLMakeString("StartHidden");
116 ADontSaveSession = PLMakeString("DontSaveSession");
117 AEmulateAppIcon = PLMakeString("EmulateAppIcon");
119 AStartWorkspace = PLMakeString("StartWorkspace");
121 AnyWindow = PLMakeString("*");
122 No = PLMakeString("No");
124 if (!scr->wattribs) {
125 scr->wattribs = PLGetDomain(wAttributeDomainName);
131 static proplist_t
132 get_value(proplist_t dict_win, proplist_t dict_class, proplist_t dict_name,
133 proplist_t dict_any, proplist_t option, proplist_t default_value,
134 Bool useGlobalDefault)
136 proplist_t value;
139 if (dict_win) {
140 value = PLGetDictionaryEntry(dict_win, option);
141 if (value)
142 return value;
145 if (dict_name) {
146 value = PLGetDictionaryEntry(dict_name, option);
147 if (value)
148 return value;
151 if (dict_class) {
152 value = PLGetDictionaryEntry(dict_class, option);
153 if (value)
154 return value;
157 if (!useGlobalDefault)
158 return NULL;
160 if (dict_any) {
161 value = PLGetDictionaryEntry(dict_any, option);
162 if (value)
163 return value;
166 return default_value;
170 void
171 wDefaultFillAttributes(WScreen *scr, char *instance, char *class,
172 WWindowAttributes *attr, Bool useGlobalDefault)
174 proplist_t value;
175 proplist_t key1, key2, key3;
176 proplist_t dw, dc, dn, da;
177 char buffer[256];
180 if (class && instance)
181 key1 = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
182 else
183 key1 = NULL;
185 if (instance)
186 key2 = PLMakeString(instance);
187 else
188 key2 = NULL;
190 if (class)
191 key3 = PLMakeString(class);
192 else
193 key3 = NULL;
195 if (!ANoTitlebar) {
196 init_wdefaults(scr);
199 PLSetStringCmpHook(NULL);
201 if (WDWindowAttributes->dictionary) {
202 dw = key1 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key1) : NULL;
203 dn = key2 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key2) : NULL;
204 dc = key3 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key3) : NULL;
205 if (useGlobalDefault)
206 da = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
207 else
208 da = NULL;
209 } else {
210 dw = NULL;
211 dn = NULL;
212 dc = NULL;
213 da = NULL;
215 if (key1)
216 PLRelease(key1);
217 if (key2)
218 PLRelease(key2);
219 if (key3)
220 PLRelease(key3);
222 /* get the data */
223 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
224 if (value)
225 attr->no_titlebar = getBool(ANoTitlebar, value);
227 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
228 if (value)
229 attr->no_resizebar = getBool(ANoResizebar, value);
231 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
232 if (value)
233 attr->no_miniaturize_button = getBool(ANoMiniaturizeButton, value);
235 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
236 if (value)
237 attr->no_close_button = getBool(ANoCloseButton, value);
239 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
240 if (value)
241 attr->no_hide_others = getBool(ANoHideOthers, value);
243 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
244 if (value)
245 attr->no_bind_mouse = getBool(ANoMouseBindings, value);
247 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
248 if (value)
249 attr->no_bind_keys = getBool(ANoKeyBindings, value);
251 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
252 if (value)
253 attr->no_appicon = getBool(ANoAppIcon, value);
255 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
256 if (value)
257 attr->floating = getBool(AKeepOnTop, value);
259 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
260 if (value)
261 attr->floating = getBool(AKeepOnBottom, value);
263 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
264 if (value)
265 attr->omnipresent = getBool(AOmnipresent, value);
267 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
268 if (value)
269 attr->skip_window_list = getBool(ASkipWindowList, value);
271 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
272 if (value)
273 attr->dont_move_off = getBool(AKeepInsideScreen, value);
275 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
276 if (value)
277 attr->no_focusable = getBool(AUnfocusable, value);
279 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
280 if (value)
281 attr->always_user_icon = getBool(AAlwaysUserIcon, value);
283 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
284 if (value)
285 attr->start_miniaturized = getBool(AStartMiniaturized, value);
287 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
288 if (value)
289 attr->start_hidden = getBool(AStartHidden, value);
291 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
292 if (value)
293 attr->dont_save_session = getBool(ADontSaveSession, value);
295 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
296 if (value)
297 attr->emulate_appicon = getBool(AEmulateAppIcon, value);
299 /* clean up */
300 PLSetStringCmpHook(StringCompareHook);
305 proplist_t
306 get_generic_value(WScreen *scr, char *instance, char *class, proplist_t option,
307 Bool noDefault)
309 proplist_t value, key, dict;
310 char buffer[256];
312 value = NULL;
314 PLSetStringCmpHook(NULL);
316 if (class && instance) {
317 key = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
319 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
320 PLRelease(key);
322 if (dict) {
323 value = PLGetDictionaryEntry(dict, option);
327 if (!value && instance) {
328 key = PLMakeString(instance);
330 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
331 PLRelease(key);
333 if (dict) {
334 value = PLGetDictionaryEntry(dict, option);
338 if (!value && class) {
339 key = PLMakeString(class);
341 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
342 PLRelease(key);
344 if (dict) {
345 value = PLGetDictionaryEntry(dict, option);
349 if (!value && !noDefault) {
350 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
352 if (dict) {
353 value = PLGetDictionaryEntry(dict, option);
357 PLSetStringCmpHook(StringCompareHook);
359 return value;
363 char*
364 wDefaultGetIconFile(WScreen *scr, char *instance, char *class,
365 Bool noDefault)
367 proplist_t value;
368 char *tmp;
370 if (!ANoTitlebar) {
371 init_wdefaults(scr);
374 if (!WDWindowAttributes->dictionary)
375 return NULL;
377 value = get_generic_value(scr, instance, class, AIcon, noDefault);
379 if (!value)
380 return NULL;
382 tmp = getString(AIcon, value);
384 return tmp;
388 RImage*
389 wDefaultGetImage(WScreen *scr, char *winstance, char *wclass)
391 char *file_name;
392 char *path;
393 RImage *image;
395 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
396 if (!file_name)
397 return NULL;
399 path = FindImage(wPreferences.icon_path, file_name);
401 if (!path) {
402 wwarning(_("could not find icon file \"%s\""), file_name);
403 return NULL;
406 image = RLoadImage(scr->rcontext, path, 0);
407 if (!image) {
408 wwarning(_("error loading image file \"%s\""), path, RMessageForError(RErrorCode));
410 free(path);
412 image = wIconValidateIconSize(scr, image);
414 return image;
419 wDefaultGetStartWorkspace(WScreen *scr, char *instance, char *class)
421 proplist_t value;
422 int w, i;
423 char *tmp;
425 if (!ANoTitlebar) {
426 init_wdefaults(scr);
429 if (!WDWindowAttributes->dictionary)
430 return -1;
432 value = get_generic_value(scr, instance, class, AStartWorkspace,
433 False);
435 if (!value)
436 return -1;
438 tmp = getString(AStartWorkspace, value);
440 if (!tmp || strlen(tmp)==0)
441 return -1;
443 if (sscanf(tmp, "%i", &w)!=1) {
444 w = -1;
445 for (i=0; i < scr->workspace_count; i++) {
446 if (strcmp(scr->workspaces[i]->name, tmp)==0) {
447 w = i;
448 break;
451 } else {
452 w--;
455 return w;
459 void
460 wDefaultChangeIcon(WScreen *scr, char *instance, char* class, char *file)
462 WDDomain *db = WDWindowAttributes;
463 proplist_t icon_value=NULL, value, attr, key, def_win, def_icon=NULL;
464 proplist_t dict = db->dictionary;
465 char *buffer;
466 int same = 0;
468 if (!dict) {
469 dict = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
470 if (dict) {
471 db->dictionary = dict;
472 value = PLMakeString(db->path);
473 PLSetFilename(dict, value);
474 PLRelease(value);
476 else
477 return;
480 PLSetStringCmpHook(NULL);
482 if (instance && class) {
483 buffer = wmalloc(strlen(instance) + strlen(class) + 2);
484 strcat(strcat(strcpy(buffer, instance), "."), class);
485 key = PLMakeString(buffer);
486 if (PLGetDictionaryEntry(dict, key)==NULL) {
487 PLRelease(key);
488 key = PLMakeString(instance);
489 if (PLGetDictionaryEntry(dict, key)==NULL) {
490 PLRelease(key);
491 key = PLMakeString(class);
492 if (PLGetDictionaryEntry(dict, key)==NULL) {
493 PLRelease(key);
494 key = PLMakeString(buffer);
498 free(buffer);
499 } else if (instance) {
500 key = PLMakeString(instance);
501 } else if (class) {
502 key = PLMakeString(class);
503 } else {
504 key = PLRetain(AnyWindow);
507 if (file) {
508 value = PLMakeString(file);
509 icon_value = PLMakeDictionaryFromEntries(AIcon, value, NULL);
510 PLRelease(value);
512 if ((def_win = PLGetDictionaryEntry(dict, AnyWindow)) != NULL) {
513 def_icon = PLGetDictionaryEntry(def_win, AIcon);
516 if (def_icon && !strcmp(PLGetString(def_icon), file))
517 same = 1;
520 if((attr = PLGetDictionaryEntry(dict, key)) != NULL) {
521 if (PLIsDictionary(attr)) {
522 if (icon_value!=NULL && !same)
523 PLMergeDictionaries(attr, icon_value);
524 else
525 PLRemoveDictionaryEntry(attr, AIcon);
528 else if (icon_value!=NULL && !same) {
529 PLInsertDictionaryEntry(dict, key, icon_value);
531 if (!wPreferences.flags.noupdates)
532 PLSave(dict, YES);
534 PLRelease(key);
535 if(icon_value)
536 PLRelease(icon_value);
538 PLSetStringCmpHook(StringCompareHook);
543 /* --------------------------- Local ----------------------- */
545 static int
546 getBool(proplist_t key, proplist_t value)
548 char *val;
550 if (!PLIsString(value)) {
551 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
552 PLGetString(key), "Boolean");
553 return 0;
555 val = PLGetString(value);
557 if ((val[1]=='\0' && (val[0]=='y' || val[0]=='Y' || val[0]=='T'
558 || val[0]=='t' || val[0]=='1'))
559 || (strcasecmp(val, "YES")==0 || strcasecmp(val, "TRUE")==0)) {
561 return 1;
562 } else if ((val[1]=='\0'
563 && (val[0]=='n' || val[0]=='N' || val[0]=='F'
564 || val[0]=='f' || val[0]=='0'))
565 || (strcasecmp(val, "NO")==0 || strcasecmp(val, "FALSE")==0)) {
567 return 0;
568 } else {
569 wwarning(_("can't convert \"%s\" to boolean"), val);
570 /* We return False if we can't convert to BOOLEAN.
571 * This is because all options defaults to False.
572 * -1 is not checked and thus is interpreted as True,
573 * which is not good.*/
574 return 0;
581 * WARNING: Do not free value returned by this!!
583 static char*
584 getString(proplist_t key, proplist_t value)
586 if (!PLIsString(value)) {
587 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
588 PLGetString(key), "String");
589 return NULL;
592 return PLGetString(value);