Fix internationalized format string warnings
[wmaker-crm.git] / src / wdefaults.c
blobc7e7126bdc4237c981a96bff021b62b490946d6e
1 /* wdefaults.c - window specific defaults
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
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>
37 #include "WindowMaker.h"
38 #include "window.h"
39 #include "screen.h"
40 #include "funcs.h"
41 #include "workspace.h"
42 #include "defaults.h"
43 #include "icon.h"
45 /* Global stuff */
47 extern WPreferences wPreferences;
49 extern WMPropList *wAttributeDomainName;
51 extern WDDomain *WDWindowAttributes;
53 /* Local stuff */
55 /* type converters */
56 static int getBool(WMPropList *, WMPropList *);
58 static char *getString(WMPropList *, WMPropList *);
60 static WMPropList *ANoTitlebar = NULL;
61 static WMPropList *ANoResizebar;
62 static WMPropList *ANoMiniaturizeButton;
63 static WMPropList *ANoCloseButton;
64 static WMPropList *ANoBorder;
65 static WMPropList *ANoHideOthers;
66 static WMPropList *ANoMouseBindings;
67 static WMPropList *ANoKeyBindings;
68 static WMPropList *ANoAppIcon; /* app */
69 static WMPropList *AKeepOnTop;
70 static WMPropList *AKeepOnBottom;
71 static WMPropList *AOmnipresent;
72 static WMPropList *ASkipWindowList;
73 static WMPropList *AKeepInsideScreen;
74 static WMPropList *AUnfocusable;
75 static WMPropList *AAlwaysUserIcon;
76 static WMPropList *AStartMiniaturized;
77 static WMPropList *AStartMaximized;
78 static WMPropList *AStartHidden; /* app */
79 static WMPropList *ADontSaveSession; /* app */
80 static WMPropList *AEmulateAppIcon;
81 static WMPropList *ADontFocusAcrossWorkspace;
82 static WMPropList *AFullMaximize;
83 static WMPropList *ASharedAppIcon; /* app */
84 #ifdef XKB_BUTTON_HINT
85 static WMPropList *ANoLanguageButton;
86 #endif
88 static WMPropList *AStartWorkspace;
90 static WMPropList *AIcon;
92 static WMPropList *AnyWindow;
93 static WMPropList *No;
95 static void init_wdefaults(WScreen * scr)
97 AIcon = WMCreatePLString("Icon");
99 ANoTitlebar = WMCreatePLString("NoTitlebar");
100 ANoResizebar = WMCreatePLString("NoResizebar");
101 ANoMiniaturizeButton = WMCreatePLString("NoMiniaturizeButton");
102 ANoCloseButton = WMCreatePLString("NoCloseButton");
103 ANoBorder = WMCreatePLString("NoBorder");
104 ANoHideOthers = WMCreatePLString("NoHideOthers");
105 ANoMouseBindings = WMCreatePLString("NoMouseBindings");
106 ANoKeyBindings = WMCreatePLString("NoKeyBindings");
107 ANoAppIcon = WMCreatePLString("NoAppIcon");
108 AKeepOnTop = WMCreatePLString("KeepOnTop");
109 AKeepOnBottom = WMCreatePLString("KeepOnBottom");
110 AOmnipresent = WMCreatePLString("Omnipresent");
111 ASkipWindowList = WMCreatePLString("SkipWindowList");
112 AKeepInsideScreen = WMCreatePLString("KeepInsideScreen");
113 AUnfocusable = WMCreatePLString("Unfocusable");
114 AAlwaysUserIcon = WMCreatePLString("AlwaysUserIcon");
115 AStartMiniaturized = WMCreatePLString("StartMiniaturized");
116 AStartHidden = WMCreatePLString("StartHidden");
117 AStartMaximized = WMCreatePLString("StartMaximized");
118 ADontSaveSession = WMCreatePLString("DontSaveSession");
119 AEmulateAppIcon = WMCreatePLString("EmulateAppIcon");
120 ADontFocusAcrossWorkspace = WMCreatePLString("DontFocusAcrossWorkspace");
121 AFullMaximize = WMCreatePLString("FullMaximize");
122 ASharedAppIcon = WMCreatePLString("SharedAppIcon");
123 #ifdef XKB_BUTTON_HINT
124 ANoLanguageButton = WMCreatePLString("NoLanguageButton");
125 #endif
127 AStartWorkspace = WMCreatePLString("StartWorkspace");
129 AnyWindow = WMCreatePLString("*");
130 No = WMCreatePLString("No");
132 if (!scr->wattribs) {
133 scr->wattribs = PLGetDomain(wAttributeDomainName);
134 } */
137 static WMPropList *get_value(WMPropList * dict_win, WMPropList * dict_class, WMPropList * dict_name,
138 WMPropList * dict_any, WMPropList * option, WMPropList * default_value,
139 Bool useGlobalDefault)
141 WMPropList *value;
143 if (dict_win) {
144 value = WMGetFromPLDictionary(dict_win, option);
145 if (value)
146 return value;
149 if (dict_name) {
150 value = WMGetFromPLDictionary(dict_name, option);
151 if (value)
152 return value;
155 if (dict_class) {
156 value = WMGetFromPLDictionary(dict_class, option);
157 if (value)
158 return value;
161 if (!useGlobalDefault)
162 return NULL;
164 if (dict_any) {
165 value = WMGetFromPLDictionary(dict_any, option);
166 if (value)
167 return value;
170 return default_value;
174 *----------------------------------------------------------------------
175 * wDefaultFillAttributes--
176 * Retrieves attributes for the specified instance/class and
177 * fills attr with it. Values that are actually defined are also
178 * set in mask. If useGlobalDefault is True, the default for
179 * all windows ("*") will be used for when no values are found
180 * for that instance/class.
182 *----------------------------------------------------------------------
184 void
185 wDefaultFillAttributes(WScreen * scr, char *instance, char *class,
186 WWindowAttributes * attr, WWindowAttributes * mask, Bool useGlobalDefault)
188 WMPropList *value, *key1, *key2, *key3, *dw, *dc, *dn, *da;
190 if (class && instance) {
191 char *buffer;
193 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
194 sprintf(buffer, "%s.%s", instance, class);
195 key1 = WMCreatePLString(buffer);
196 wfree(buffer);
197 } else {
198 key1 = NULL;
201 if (instance)
202 key2 = WMCreatePLString(instance);
203 else
204 key2 = NULL;
206 if (class)
207 key3 = WMCreatePLString(class);
208 else
209 key3 = NULL;
211 if (!ANoTitlebar) {
212 init_wdefaults(scr);
215 WMPLSetCaseSensitive(True);
217 if (WDWindowAttributes->dictionary) {
218 dw = key1 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key1) : NULL;
219 dn = key2 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key2) : NULL;
220 dc = key3 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key3) : NULL;
221 if (useGlobalDefault)
222 da = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
223 else
224 da = NULL;
225 } else {
226 dw = NULL;
227 dn = NULL;
228 dc = NULL;
229 da = NULL;
231 if (key1)
232 WMReleasePropList(key1);
233 if (key2)
234 WMReleasePropList(key2);
235 if (key3)
236 WMReleasePropList(key3);
238 #define APPLY_VAL(value, flag, attrib) \
239 if (value) {attr->flag = getBool(attrib, value); \
240 if (mask) mask->flag = 1;}
242 /* get the data */
243 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
244 APPLY_VAL(value, no_titlebar, ANoTitlebar);
246 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
247 APPLY_VAL(value, no_resizebar, ANoResizebar);
249 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
250 APPLY_VAL(value, no_miniaturize_button, ANoMiniaturizeButton);
252 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
253 APPLY_VAL(value, no_close_button, ANoCloseButton);
255 value = get_value(dw, dc, dn, da, ANoBorder, No, useGlobalDefault);
256 APPLY_VAL(value, no_border, ANoBorder);
258 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
259 APPLY_VAL(value, no_hide_others, ANoHideOthers);
261 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
262 APPLY_VAL(value, no_bind_mouse, ANoMouseBindings);
264 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
265 APPLY_VAL(value, no_bind_keys, ANoKeyBindings);
267 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
268 APPLY_VAL(value, no_appicon, ANoAppIcon);
270 value = get_value(dw, dc, dn, da, ASharedAppIcon, No, useGlobalDefault);
271 APPLY_VAL(value, shared_appicon, ASharedAppIcon);
273 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
274 APPLY_VAL(value, floating, AKeepOnTop);
276 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
277 APPLY_VAL(value, sunken, AKeepOnBottom);
279 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
280 APPLY_VAL(value, omnipresent, AOmnipresent);
282 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
283 APPLY_VAL(value, skip_window_list, ASkipWindowList);
285 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
286 APPLY_VAL(value, dont_move_off, AKeepInsideScreen);
288 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
289 APPLY_VAL(value, no_focusable, AUnfocusable);
291 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
292 APPLY_VAL(value, always_user_icon, AAlwaysUserIcon);
294 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
295 APPLY_VAL(value, start_miniaturized, AStartMiniaturized);
297 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
298 APPLY_VAL(value, start_hidden, AStartHidden);
300 value = get_value(dw, dc, dn, da, AStartMaximized, No, useGlobalDefault);
301 APPLY_VAL(value, start_maximized, AStartMaximized);
303 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
304 APPLY_VAL(value, dont_save_session, ADontSaveSession);
306 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
307 APPLY_VAL(value, emulate_appicon, AEmulateAppIcon);
309 value = get_value(dw, dc, dn, da, ADontFocusAcrossWorkspace, No, useGlobalDefault);
310 APPLY_VAL(value, dont_focus_across_wksp, ADontFocusAcrossWorkspace);
312 value = get_value(dw, dc, dn, da, AFullMaximize, No, useGlobalDefault);
313 APPLY_VAL(value, full_maximize, AFullMaximize);
315 #ifdef XKB_BUTTON_HINT
316 value = get_value(dw, dc, dn, da, ANoLanguageButton, No, useGlobalDefault);
317 APPLY_VAL(value, no_language_button, ANoLanguageButton);
318 #endif
320 /* clean up */
321 WMPLSetCaseSensitive(False);
324 WMPropList *get_generic_value(WScreen * scr, char *instance, char *class, WMPropList * option, Bool noDefault)
326 WMPropList *value, *key, *dict;
328 value = NULL;
330 WMPLSetCaseSensitive(True);
332 if (class && instance) {
333 char *buffer;
335 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
336 sprintf(buffer, "%s.%s", instance, class);
337 key = WMCreatePLString(buffer);
338 wfree(buffer);
340 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
341 WMReleasePropList(key);
343 if (dict) {
344 value = WMGetFromPLDictionary(dict, option);
348 if (!value && instance) {
349 key = WMCreatePLString(instance);
351 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
352 WMReleasePropList(key);
353 if (dict) {
354 value = WMGetFromPLDictionary(dict, option);
358 if (!value && class) {
359 key = WMCreatePLString(class);
361 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
362 WMReleasePropList(key);
364 if (dict) {
365 value = WMGetFromPLDictionary(dict, option);
369 if (!value && !noDefault) {
370 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
372 if (dict) {
373 value = WMGetFromPLDictionary(dict, option);
377 WMPLSetCaseSensitive(False);
379 return value;
382 char *wDefaultGetIconFile(WScreen * scr, char *instance, char *class, Bool noDefault)
384 WMPropList *value;
385 char *tmp;
387 if (!ANoTitlebar) {
388 init_wdefaults(scr);
391 if (!WDWindowAttributes->dictionary)
392 return NULL;
394 value = get_generic_value(scr, instance, class, AIcon, noDefault);
396 if (!value)
397 return NULL;
399 tmp = getString(AIcon, value);
401 return tmp;
404 RImage *wDefaultGetImage(WScreen * scr, char *winstance, char *wclass)
406 char *file_name;
407 char *path;
408 RImage *image;
410 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
411 if (!file_name)
412 return NULL;
414 path = FindImage(wPreferences.icon_path, file_name);
416 if (!path) {
417 wwarning(_("could not find icon file \"%s\""), file_name);
418 return NULL;
421 image = RLoadImage(scr->rcontext, path, 0);
422 if (!image) {
423 wwarning(_("error loading image file \"%s\": %s"), path, RMessageForError(RErrorCode));
425 wfree(path);
427 image = wIconValidateIconSize(scr, image);
429 return image;
432 int wDefaultGetStartWorkspace(WScreen * scr, char *instance, char *class)
434 WMPropList *value;
435 int w, i;
436 char *tmp;
438 if (!ANoTitlebar) {
439 init_wdefaults(scr);
442 if (!WDWindowAttributes->dictionary)
443 return -1;
445 value = get_generic_value(scr, instance, class, AStartWorkspace, False);
447 if (!value)
448 return -1;
450 tmp = getString(AStartWorkspace, value);
452 if (!tmp || strlen(tmp) == 0)
453 return -1;
455 if (sscanf(tmp, "%i", &w) != 1) {
456 w = -1;
457 for (i = 0; i < scr->workspace_count; i++) {
458 if (strcmp(scr->workspaces[i]->name, tmp) == 0) {
459 w = i;
460 break;
463 } else {
464 w--;
467 return w;
470 void wDefaultChangeIcon(WScreen * scr, char *instance, char *class, char *file)
472 WDDomain *db = WDWindowAttributes;
473 WMPropList *icon_value = NULL, *value, *attr, *key, *def_win, *def_icon = NULL;
474 WMPropList *dict = db->dictionary;
475 int same = 0;
477 if (!dict) {
478 dict = WMCreatePLDictionary(NULL, NULL);
479 if (dict) {
480 db->dictionary = dict;
481 } else {
482 return;
486 WMPLSetCaseSensitive(True);
488 if (instance && class) {
489 char *buffer;
490 buffer = wmalloc(strlen(instance) + strlen(class) + 2);
491 sprintf(buffer, "%s.%s", instance, class);
492 key = WMCreatePLString(buffer);
493 wfree(buffer);
494 } else if (instance) {
495 key = WMCreatePLString(instance);
496 } else if (class) {
497 key = WMCreatePLString(class);
498 } else {
499 key = WMRetainPropList(AnyWindow);
502 if (file) {
503 value = WMCreatePLString(file);
504 icon_value = WMCreatePLDictionary(AIcon, value, NULL);
505 WMReleasePropList(value);
507 if ((def_win = WMGetFromPLDictionary(dict, AnyWindow)) != NULL) {
508 def_icon = WMGetFromPLDictionary(def_win, AIcon);
511 if (def_icon && !strcmp(WMGetFromPLString(def_icon), file))
512 same = 1;
515 if ((attr = WMGetFromPLDictionary(dict, key)) != NULL) {
516 if (WMIsPLDictionary(attr)) {
517 if (icon_value != NULL && !same)
518 WMMergePLDictionaries(attr, icon_value, False);
519 else
520 WMRemoveFromPLDictionary(attr, AIcon);
522 } else if (icon_value != NULL && !same) {
523 WMPutInPLDictionary(dict, key, icon_value);
525 if (!wPreferences.flags.noupdates) {
526 UpdateDomainFile(db);
529 WMReleasePropList(key);
530 if (icon_value)
531 WMReleasePropList(icon_value);
533 WMPLSetCaseSensitive(False);
536 /* --------------------------- Local ----------------------- */
538 static int getBool(WMPropList * key, WMPropList * value)
540 char *val;
542 if (!WMIsPLString(value)) {
543 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
544 WMGetFromPLString(key), "Boolean");
545 return 0;
547 val = WMGetFromPLString(value);
549 if ((val[1] == '\0' && (val[0] == 'y' || val[0] == 'Y' || val[0] == 'T' || val[0] == 't' || val[0] == '1'))
550 || (strcasecmp(val, "YES") == 0 || strcasecmp(val, "TRUE") == 0)) {
552 return 1;
553 } else if ((val[1] == '\0'
554 && (val[0] == 'n' || val[0] == 'N' || val[0] == 'F' || 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;
568 /* WARNING: Do not free the value returned by this function!! */
569 static char *getString(WMPropList * key, WMPropList * value)
571 if (!WMIsPLString(value)) {
572 wwarning(_("Wrong option format for key \"%s\". Should be %s."), WMGetFromPLString(key), "String");
573 return NULL;
576 return WMGetFromPLString(value);