XKB lock relate. Allow you to disable language button in advance setting.
[wmaker-crm.git] / src / wdefaults.c
blobb6279b744e6c0105dc52cd249f70b9af74e3e04c
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;
85 static proplist_t AFullMaximize;
86 #ifdef XKB_BUTTON_HINT
87 static proplist_t ANoLanguageButton;
88 #endif
90 static proplist_t AStartWorkspace;
92 static proplist_t AIcon;
95 static proplist_t AnyWindow;
96 static proplist_t No;
99 static void
100 init_wdefaults(WScreen *scr)
102 AIcon = PLMakeString("Icon");
104 ANoTitlebar = PLMakeString("NoTitlebar");
105 ANoResizebar = PLMakeString("NoResizebar");
106 ANoMiniaturizeButton = PLMakeString("NoMiniaturizeButton");
107 ANoCloseButton = PLMakeString("NoCloseButton");
108 ANoHideOthers = PLMakeString("NoHideOthers");
109 ANoMouseBindings = PLMakeString("NoMouseBindings");
110 ANoKeyBindings = PLMakeString("NoKeyBindings");
111 ANoAppIcon = PLMakeString("NoAppIcon");
112 AKeepOnTop = PLMakeString("KeepOnTop");
113 AKeepOnBottom = PLMakeString("KeepOnBottom");
114 AOmnipresent = PLMakeString("Omnipresent");
115 ASkipWindowList = PLMakeString("SkipWindowList");
116 AKeepInsideScreen = PLMakeString("KeepInsideScreen");
117 AUnfocusable = PLMakeString("Unfocusable");
118 AAlwaysUserIcon = PLMakeString("AlwaysUserIcon");
119 AStartMiniaturized = PLMakeString("StartMiniaturized");
120 AStartHidden = PLMakeString("StartHidden");
121 AStartMaximized = PLMakeString("StartMaximized");
122 ADontSaveSession = PLMakeString("DontSaveSession");
123 AEmulateAppIcon = PLMakeString("EmulateAppIcon");
124 AFullMaximize = PLMakeString("FullMaximize");
125 #ifdef XKB_BUTTON_HINT
126 ANoLanguageButton = PLMakeString("NoLanguageButton");
127 #endif
129 AStartWorkspace = PLMakeString("StartWorkspace");
131 AnyWindow = PLMakeString("*");
132 No = PLMakeString("No");
134 if (!scr->wattribs) {
135 scr->wattribs = PLGetDomain(wAttributeDomainName);
141 static proplist_t
142 get_value(proplist_t dict_win, proplist_t dict_class, proplist_t dict_name,
143 proplist_t dict_any, proplist_t option, proplist_t default_value,
144 Bool useGlobalDefault)
146 proplist_t value;
149 if (dict_win) {
150 value = PLGetDictionaryEntry(dict_win, option);
151 if (value)
152 return value;
155 if (dict_name) {
156 value = PLGetDictionaryEntry(dict_name, option);
157 if (value)
158 return value;
161 if (dict_class) {
162 value = PLGetDictionaryEntry(dict_class, option);
163 if (value)
164 return value;
167 if (!useGlobalDefault)
168 return NULL;
170 if (dict_any) {
171 value = PLGetDictionaryEntry(dict_any, option);
172 if (value)
173 return value;
176 return default_value;
181 *----------------------------------------------------------------------
182 * wDefaultFillAttributes--
183 * Retrieves attributes for the specified instance/class and
184 * fills attr with it. Values that are actually defined are also
185 * set in mask. If useGlobalDefault is True, the default for
186 * all windows ("*") will be used for when no values are found
187 * for that instance/class.
189 *----------------------------------------------------------------------
191 void
192 wDefaultFillAttributes(WScreen *scr, char *instance, char *class,
193 WWindowAttributes *attr,
194 WWindowAttributes *mask,
195 Bool useGlobalDefault)
197 proplist_t value;
198 proplist_t key1, key2, key3;
199 proplist_t dw, dc, dn, da;
200 char buffer[256];
203 if (class && instance)
204 key1 = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
205 else
206 key1 = NULL;
208 if (instance)
209 key2 = PLMakeString(instance);
210 else
211 key2 = NULL;
213 if (class)
214 key3 = PLMakeString(class);
215 else
216 key3 = NULL;
218 if (!ANoTitlebar) {
219 init_wdefaults(scr);
222 PLSetStringCmpHook(NULL);
224 if (WDWindowAttributes->dictionary) {
225 dw = key1 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key1) : NULL;
226 dn = key2 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key2) : NULL;
227 dc = key3 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key3) : NULL;
228 if (useGlobalDefault)
229 da = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
230 else
231 da = NULL;
232 } else {
233 dw = NULL;
234 dn = NULL;
235 dc = NULL;
236 da = NULL;
238 if (key1)
239 PLRelease(key1);
240 if (key2)
241 PLRelease(key2);
242 if (key3)
243 PLRelease(key3);
245 #define APPLY_VAL(value, flag, attrib) \
246 if (value) {attr->flag = getBool(attrib, value); \
247 if (mask) mask->flag = 1;}
249 /* get the data */
250 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
251 APPLY_VAL(value, no_titlebar, ANoTitlebar);
253 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
254 APPLY_VAL(value, no_resizebar, ANoResizebar);
256 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
257 APPLY_VAL(value, no_miniaturize_button, ANoMiniaturizeButton);
259 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
260 APPLY_VAL(value, no_close_button, ANoCloseButton);
262 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
263 APPLY_VAL(value, no_hide_others, ANoHideOthers);
265 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
266 APPLY_VAL(value, no_bind_mouse, ANoMouseBindings);
268 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
269 APPLY_VAL(value, no_bind_keys, ANoKeyBindings);
271 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
272 APPLY_VAL(value, no_appicon, ANoAppIcon);
274 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
275 APPLY_VAL(value, floating, AKeepOnTop);
277 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
278 APPLY_VAL(value, sunken, AKeepOnBottom);
280 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
281 APPLY_VAL(value, omnipresent, AOmnipresent);
283 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
284 APPLY_VAL(value, skip_window_list, ASkipWindowList);
286 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
287 APPLY_VAL(value, dont_move_off, AKeepInsideScreen);
289 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
290 APPLY_VAL(value, no_focusable, AUnfocusable);
292 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
293 APPLY_VAL(value, always_user_icon, AAlwaysUserIcon);
295 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
296 APPLY_VAL(value, start_miniaturized, AStartMiniaturized);
298 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
299 APPLY_VAL(value, start_hidden, AStartHidden);
301 value = get_value(dw, dc, dn, da, AStartMaximized, No, useGlobalDefault);
302 APPLY_VAL(value, start_maximized, AStartMaximized);
304 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
305 APPLY_VAL(value, dont_save_session, ADontSaveSession);
307 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
308 APPLY_VAL(value, emulate_appicon, AEmulateAppIcon);
310 value = get_value(dw, dc, dn, da, AFullMaximize, No, useGlobalDefault);
311 APPLY_VAL(value, full_maximize, AFullMaximize);
313 #ifdef XKB_BUTTON_HINT
314 value = get_value(dw, dc, dn, da, ANoLanguageButton, No, useGlobalDefault);
315 APPLY_VAL(value, no_language_button, ANoLanguageButton);
316 #endif
318 /* clean up */
319 PLSetStringCmpHook(StringCompareHook);
324 proplist_t
325 get_generic_value(WScreen *scr, char *instance, char *class, proplist_t option,
326 Bool noDefault)
328 proplist_t value, key, dict;
329 char buffer[256];
331 value = NULL;
333 PLSetStringCmpHook(NULL);
335 if (class && instance) {
336 key = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
338 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
339 PLRelease(key);
341 if (dict) {
342 value = PLGetDictionaryEntry(dict, option);
346 if (!value && instance) {
347 key = PLMakeString(instance);
349 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
350 PLRelease(key);
352 if (dict) {
353 value = PLGetDictionaryEntry(dict, option);
357 if (!value && class) {
358 key = PLMakeString(class);
360 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
361 PLRelease(key);
363 if (dict) {
364 value = PLGetDictionaryEntry(dict, option);
368 if (!value && !noDefault) {
369 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
371 if (dict) {
372 value = PLGetDictionaryEntry(dict, option);
376 PLSetStringCmpHook(StringCompareHook);
378 return value;
382 char*
383 wDefaultGetIconFile(WScreen *scr, char *instance, char *class,
384 Bool noDefault)
386 proplist_t value;
387 char *tmp;
389 if (!ANoTitlebar) {
390 init_wdefaults(scr);
393 if (!WDWindowAttributes->dictionary)
394 return NULL;
396 value = get_generic_value(scr, instance, class, AIcon, noDefault);
398 if (!value)
399 return NULL;
401 tmp = getString(AIcon, value);
403 return tmp;
407 RImage*
408 wDefaultGetImage(WScreen *scr, char *winstance, char *wclass)
410 char *file_name;
411 char *path;
412 RImage *image;
414 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
415 if (!file_name)
416 return NULL;
418 path = FindImage(wPreferences.icon_path, file_name);
420 if (!path) {
421 wwarning(_("could not find icon file \"%s\""), file_name);
422 return NULL;
425 image = RLoadImage(scr->rcontext, path, 0);
426 if (!image) {
427 wwarning(_("error loading image file \"%s\""), path, RMessageForError(RErrorCode));
429 free(path);
431 image = wIconValidateIconSize(scr, image);
433 return image;
438 wDefaultGetStartWorkspace(WScreen *scr, char *instance, char *class)
440 proplist_t value;
441 int w, i;
442 char *tmp;
444 if (!ANoTitlebar) {
445 init_wdefaults(scr);
448 if (!WDWindowAttributes->dictionary)
449 return -1;
451 value = get_generic_value(scr, instance, class, AStartWorkspace,
452 False);
454 if (!value)
455 return -1;
457 tmp = getString(AStartWorkspace, value);
459 if (!tmp || strlen(tmp)==0)
460 return -1;
462 if (sscanf(tmp, "%i", &w)!=1) {
463 w = -1;
464 for (i=0; i < scr->workspace_count; i++) {
465 if (strcmp(scr->workspaces[i]->name, tmp)==0) {
466 w = i;
467 break;
470 } else {
471 w--;
474 return w;
478 void
479 wDefaultChangeIcon(WScreen *scr, char *instance, char* class, char *file)
481 WDDomain *db = WDWindowAttributes;
482 proplist_t icon_value=NULL, value, attr, key, def_win, def_icon=NULL;
483 proplist_t dict = db->dictionary;
484 char *buffer;
485 int same = 0;
487 if (!dict) {
488 dict = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
489 if (dict) {
490 db->dictionary = dict;
491 value = PLMakeString(db->path);
492 PLSetFilename(dict, value);
493 PLRelease(value);
495 else
496 return;
499 PLSetStringCmpHook(NULL);
501 if (instance && class) {
502 buffer = wmalloc(strlen(instance) + strlen(class) + 2);
503 strcat(strcat(strcpy(buffer, instance), "."), class);
504 key = PLMakeString(buffer);
505 free(buffer);
506 } else if (instance) {
507 key = PLMakeString(instance);
508 } else if (class) {
509 key = PLMakeString(class);
510 } else {
511 key = PLRetain(AnyWindow);
514 if (file) {
515 value = PLMakeString(file);
516 icon_value = PLMakeDictionaryFromEntries(AIcon, value, NULL);
517 PLRelease(value);
519 if ((def_win = PLGetDictionaryEntry(dict, AnyWindow)) != NULL) {
520 def_icon = PLGetDictionaryEntry(def_win, AIcon);
523 if (def_icon && !strcmp(PLGetString(def_icon), file))
524 same = 1;
527 if ((attr = PLGetDictionaryEntry(dict, key)) != NULL) {
528 if (PLIsDictionary(attr)) {
529 if (icon_value!=NULL && !same)
530 PLMergeDictionaries(attr, icon_value);
531 else
532 PLRemoveDictionaryEntry(attr, AIcon);
534 } else if (icon_value!=NULL && !same) {
535 PLInsertDictionaryEntry(dict, key, icon_value);
537 if (!wPreferences.flags.noupdates)
538 PLSave(dict, YES);
540 PLRelease(key);
541 if(icon_value)
542 PLRelease(icon_value);
544 PLSetStringCmpHook(StringCompareHook);
549 /* --------------------------- Local ----------------------- */
551 static int
552 getBool(proplist_t key, proplist_t value)
554 char *val;
556 if (!PLIsString(value)) {
557 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
558 PLGetString(key), "Boolean");
559 return 0;
561 val = PLGetString(value);
563 if ((val[1]=='\0' && (val[0]=='y' || val[0]=='Y' || val[0]=='T'
564 || val[0]=='t' || val[0]=='1'))
565 || (strcasecmp(val, "YES")==0 || strcasecmp(val, "TRUE")==0)) {
567 return 1;
568 } else if ((val[1]=='\0'
569 && (val[0]=='n' || val[0]=='N' || val[0]=='F'
570 || val[0]=='f' || val[0]=='0'))
571 || (strcasecmp(val, "NO")==0 || strcasecmp(val, "FALSE")==0)) {
573 return 0;
574 } else {
575 wwarning(_("can't convert \"%s\" to boolean"), val);
576 /* We return False if we can't convert to BOOLEAN.
577 * This is because all options defaults to False.
578 * -1 is not checked and thus is interpreted as True,
579 * which is not good.*/
580 return 0;
587 * WARNING: Do not free value returned by this!!
589 static char*
590 getString(proplist_t key, proplist_t value)
592 if (!PLIsString(value)) {
593 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
594 PLGetString(key), "String");
595 return NULL;
598 return PLGetString(value);