WPrefs: improve texts in the Window Handling panel
[wmaker-crm.git] / util / setstyle.c
blob1a18afd4b51cfcb2b30ba9ebd26c365767d84619
1 /* setstyle.c - loads style related options to wmaker
3 * WindowMaker 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifdef __GLIBC__
23 #define _GNU_SOURCE /* getopt_long */
24 #endif
26 #include "config.h"
28 #include <sys/stat.h>
30 #include <getopt.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <X11/Xlib.h>
39 #ifdef HAVE_STDNORETURN
40 #include <stdnoreturn.h>
41 #endif
43 #include <WINGs/WUtil.h>
45 #include "../src/wconfig.h"
47 #include "common.h"
49 #define MAX_OPTIONS 128
51 char *FontOptions[] = {
52 "IconTitleFont",
53 "ClipTitleFont",
54 "LargeDisplayFont",
55 "MenuTextFont",
56 "MenuTitleFont",
57 "WindowTitleFont",
58 NULL
61 char *CursorOptions[] = {
62 "NormalCursor",
63 "ArrowCursor",
64 "MoveCursor",
65 "ResizeCursor",
66 "TopLeftResizeCursor",
67 "TopRightResizeCursor",
68 "BottomLeftResizeCursor",
69 "BottomRightResizeCursor",
70 "VerticalResizeCursor",
71 "HorizontalResizeCursor",
72 "WaitCursor",
73 "QuestionCursor",
74 "TextCursor",
75 "SelectCursor",
76 NULL
79 static const char *prog_name;
80 int ignoreFonts = 0;
81 int ignoreCursors = 0;
83 Display *dpy;
86 static Bool isCursorOption(const char *option)
88 int i;
90 for (i = 0; CursorOptions[i] != NULL; i++) {
91 if (strcasecmp(option, CursorOptions[i]) == 0) {
92 return True;
96 return False;
99 static Bool isFontOption(const char *option)
101 int i;
103 for (i = 0; FontOptions[i] != NULL; i++) {
104 if (strcasecmp(option, FontOptions[i]) == 0) {
105 return True;
109 return False;
113 * finds elements in `texture' that reference external files,
114 * prepends `prefix' to these files. `prefix' is a path component
115 * that qualifies the external references to be absolute, possibly
116 * pending further expansion
118 static void hackPathInTexture(WMPropList * texture, const char *prefix)
120 WMPropList *type;
121 char *t;
123 /* get texture type */
124 type = WMGetFromPLArray(texture, 0);
125 t = WMGetFromPLString(type);
126 if (t == NULL)
127 return;
129 if (strcasecmp(t, "tpixmap") == 0 ||
130 strcasecmp(t, "spixmap") == 0 ||
131 strcasecmp(t, "mpixmap") == 0 ||
132 strcasecmp(t, "cpixmap") == 0 ||
133 strcasecmp(t, "tvgradient") == 0 ||
134 strcasecmp(t, "thgradient") == 0 ||
135 strcasecmp(t, "tdgradient") == 0) {
136 WMPropList *file;
137 char buffer[4018];
139 /* get pixmap file path */
140 file = WMGetFromPLArray(texture, 1);
141 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
142 /* replace path with full path */
143 WMDeleteFromPLArray(texture, 1);
144 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
146 } else if (strcasecmp(t, "bitmap") == 0) {
147 WMPropList *file;
148 char buffer[4018];
150 /* get bitmap file path */
151 file = WMGetFromPLArray(texture, 1);
152 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
153 /* replace path with full path */
154 WMDeleteFromPLArray(texture, 1);
155 WMInsertInPLArray(texture, 1, WMCreatePLString(buffer));
157 /* get mask file path */
158 file = WMGetFromPLArray(texture, 2);
159 sprintf(buffer, "%s/%s", prefix, WMGetFromPLString(file));
160 /* replace path with full path */
161 WMDeleteFromPLArray(texture, 2);
162 WMInsertInPLArray(texture, 2, WMCreatePLString(buffer));
166 static void hackPaths(WMPropList * style, const char *prefix)
168 WMPropList *keys;
169 WMPropList *key;
170 WMPropList *value;
171 int i;
173 keys = WMGetPLDictionaryKeys(style);
175 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
176 key = WMGetFromPLArray(keys, i);
178 value = WMGetFromPLDictionary(style, key);
179 if (!value)
180 continue;
182 if (strcasecmp(WMGetFromPLString(key), "WorkspaceSpecificBack") == 0) {
183 if (WMIsPLArray(value)) {
184 int j;
185 WMPropList *texture;
187 for (j = 0; j < WMGetPropListItemCount(value); j++) {
188 texture = WMGetFromPLArray(value, j);
190 if (texture && WMIsPLArray(texture)
191 && WMGetPropListItemCount(texture) > 2) {
193 hackPathInTexture(texture, prefix);
197 } else {
199 if (WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
201 hackPathInTexture(value, prefix);
206 WMReleasePropList(keys);
209 static WMPropList *getColor(WMPropList * texture)
211 WMPropList *value, *type;
212 char *str;
214 type = WMGetFromPLArray(texture, 0);
215 if (!type)
216 return NULL;
218 value = NULL;
220 str = WMGetFromPLString(type);
221 if (strcasecmp(str, "solid") == 0) {
222 value = WMGetFromPLArray(texture, 1);
223 } else if (strcasecmp(str, "dgradient") == 0
224 || strcasecmp(str, "hgradient") == 0 || strcasecmp(str, "vgradient") == 0) {
225 WMPropList *c1, *c2;
226 int r1, g1, b1, r2, g2, b2;
227 char buffer[32];
229 c1 = WMGetFromPLArray(texture, 1);
230 c2 = WMGetFromPLArray(texture, 2);
231 if (!dpy) {
232 if (sscanf(WMGetFromPLString(c1), "#%2x%2x%2x", &r1, &g1, &b1) == 3
233 && sscanf(WMGetFromPLString(c2), "#%2x%2x%2x", &r2, &g2, &b2) == 3) {
234 sprintf(buffer, "#%02x%02x%02x", (r1 + r2) / 2, (g1 + g2) / 2, (b1 + b2) / 2);
235 value = WMCreatePLString(buffer);
236 } else {
237 value = c1;
239 } else {
240 XColor color1;
241 XColor color2;
243 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c1), &color1);
244 XParseColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), WMGetFromPLString(c2), &color2);
246 sprintf(buffer, "#%02x%02x%02x",
247 (color1.red + color2.red) >> 9,
248 (color1.green + color2.green) >> 9, (color1.blue + color2.blue) >> 9);
249 value = WMCreatePLString(buffer);
251 } else if (strcasecmp(str, "mdgradient") == 0
252 || strcasecmp(str, "mhgradient") == 0 || strcasecmp(str, "mvgradient") == 0) {
254 value = WMGetFromPLArray(texture, 1);
256 } else if (strcasecmp(str, "tpixmap") == 0
257 || strcasecmp(str, "cpixmap") == 0 || strcasecmp(str, "spixmap") == 0) {
259 value = WMGetFromPLArray(texture, 2);
262 return value;
266 * since some of the options introduce incompatibilities, we will need
267 * to do a kluge here or the themes ppl will get real annoying.
268 * So, treat for the absence of the following options:
269 * IconTitleColor
270 * IconTitleBack
272 static void hackStyle(WMPropList * style)
274 WMPropList *keys, *tmp;
275 int foundIconTitle = 0, foundResizebarBack = 0;
276 int i;
278 keys = WMGetPLDictionaryKeys(style);
280 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
281 char *str;
283 tmp = WMGetFromPLArray(keys, i);
284 str = WMGetFromPLString(tmp);
285 if (str) {
286 if (ignoreFonts && isFontOption(str)) {
287 WMRemoveFromPLDictionary(style, tmp);
288 continue;
290 if (ignoreCursors && isCursorOption(str)) {
291 WMRemoveFromPLDictionary(style, tmp);
292 continue;
294 if (isFontOption(str)) {
295 WMPropList *value;
296 char *newfont, *oldfont;
298 value = WMGetFromPLDictionary(style, tmp);
299 if (value) {
300 oldfont = WMGetFromPLString(value);
301 newfont = convertFont(oldfont, False);
302 if (newfont != oldfont) {
303 value = WMCreatePLString(newfont);
304 WMPutInPLDictionary(style, tmp, value);
305 WMReleasePropList(value);
306 wfree(newfont);
310 if (strcasecmp(str, "IconTitleColor") == 0 || strcasecmp(str, "IconTitleBack") == 0) {
311 foundIconTitle = 1;
312 } else if (strcasecmp(str, "ResizebarBack") == 0) {
313 foundResizebarBack = 1;
317 WMReleasePropList(keys);
319 if (!foundIconTitle) {
320 /* set the default values */
321 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleColor"));
322 if (tmp) {
323 WMPutInPLDictionary(style, WMCreatePLString("IconTitleColor"), tmp);
326 tmp = WMGetFromPLDictionary(style, WMCreatePLString("FTitleBack"));
327 if (tmp) {
328 WMPropList *value;
330 value = getColor(tmp);
332 if (value) {
333 WMPutInPLDictionary(style, WMCreatePLString("IconTitleBack"), value);
338 if (!foundResizebarBack) {
339 /* set the default values */
340 tmp = WMGetFromPLDictionary(style, WMCreatePLString("UTitleBack"));
341 if (tmp) {
342 WMPropList *value;
344 value = getColor(tmp);
346 if (value) {
347 WMPropList *t;
349 t = WMCreatePLArray(WMCreatePLString("solid"), value, NULL);
350 WMPutInPLDictionary(style, WMCreatePLString("ResizebarBack"), t);
355 if (!WMGetFromPLDictionary(style, WMCreatePLString("MenuStyle"))) {
356 WMPutInPLDictionary(style, WMCreatePLString("MenuStyle"), WMCreatePLString("normal"));
360 static noreturn void print_help(int print_usage, int exitval)
362 printf("Usage: %s [OPTIONS] FILE\n", prog_name);
363 if (print_usage) {
364 puts("Reads style/theme configuration from FILE and updates Window Maker.");
365 puts("");
366 puts(" --no-fonts ignore font related options");
367 puts(" --no-cursors ignore cursor related options");
368 puts(" --ignore <option> ignore changes in the specified option");
369 puts(" -h, --help display this help and exit");
370 puts(" -v, --version output version information and exit");
372 exit(exitval);
375 int main(int argc, char **argv)
377 WMPropList *prop, *style;
378 char *path;
379 char *file = NULL;
380 struct stat st;
381 int i, ch, ignflag = 0;
382 int ignoreCount = 0;
383 char *ignoreList[MAX_OPTIONS];
384 XEvent ev;
386 struct option longopts[] = {
387 { "version", no_argument, NULL, 'v' },
388 { "help", no_argument, NULL, 'h' },
389 { "no-fonts", no_argument, &ignoreFonts, 1 },
390 { "no-cursors", no_argument, &ignoreCursors, 1 },
391 { "ignore", required_argument, &ignflag, 1 },
392 { NULL, 0, NULL, 0 }
395 prog_name = argv[0];
396 while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
397 switch(ch) {
398 case 'v':
399 printf("%s (Window Maker %s)\n", prog_name, VERSION);
400 return 0;
401 /* NOTREACHED */
402 case 'h':
403 print_help(1, 0);
404 /* NOTREACHED */
405 case 0:
406 if (ignflag) {
407 if (ignoreCount >= MAX_OPTIONS) {
408 printf("Maximum %d `ignore' arguments\n", MAX_OPTIONS);
409 return 1;
411 ignoreList[ignoreCount++] = optarg;
412 ignflag = 0;
414 break;
415 default:
416 print_help(0, 1);
417 /* NOTREACHED */
420 argc -= optind;
421 argv += optind;
423 if (argc != 1)
424 print_help(0, 1);
426 file = argv[0];
428 WMPLSetCaseSensitive(False);
430 path = wdefaultspathfordomain("WindowMaker");
432 prop = WMReadPropListFromFile(path);
433 if (!prop) {
434 perror(path);
435 printf("%s: could not load WindowMaker configuration file.\n", prog_name);
436 return 1;
439 if (stat(file, &st) < 0) {
440 perror(file);
441 return 1;
443 if (S_ISDIR(st.st_mode)) { /* theme pack */
444 char buf[PATH_MAX];
445 char *homedir;
447 if (realpath(file, buf) == NULL) {
448 perror(file);
449 return 1;
451 strncat(buf, "/style", sizeof(buf) - strlen(buf) - 1);
453 if (stat(buf, &st) != 0 || !S_ISREG(st.st_mode)) { /* maybe symlink too? */
454 printf("%s: %s: style file not found or not a file\n", prog_name, buf);
455 return 1;
458 style = WMReadPropListFromFile(buf);
459 if (!style) {
460 perror(buf);
461 printf("%s: could not load style file.\n", prog_name);
462 return 1;
465 buf[strlen(buf) - 6 /* strlen("/style") */] = '\0';
466 homedir = wstrdup(wgethomedir());
467 if (strlen(homedir) > 1 && /* this is insane, wgethomedir() returns `/' on error */
468 strncmp(homedir, buf, strlen(homedir)) == 0) {
469 /* theme pack is under ${HOME}; exchange ${HOME} part
470 * for `~' so it gets portable references to the user home dir */
471 *buf = '~';
472 memmove(buf + 1, buf + strlen(homedir), strlen(buf) - strlen(homedir) + 1);
474 wfree(homedir);
476 hackPaths(style, buf); /* this will prefix pixmaps in the style
477 * with absolute(ish) references */
479 } else { /* normal style file */
481 style = WMReadPropListFromFile(file);
482 if (!style) {
483 perror(file);
484 printf("%s:could not load style file.\n", prog_name);
485 return 1;
489 if (!WMIsPLDictionary(style)) {
490 printf("%s: '%s' is not a style file/theme\n", prog_name, file);
491 return 1;
494 hackStyle(style);
496 if (ignoreCount > 0) {
497 for (i = 0; i < ignoreCount; i++) {
498 WMRemoveFromPLDictionary(style, WMCreatePLString(ignoreList[i]));
502 WMMergePLDictionaries(prop, style, True);
504 WMWritePropListToFile(prop, path);
506 dpy = XOpenDisplay("");
507 if (dpy) {
508 memset(&ev, 0, sizeof(XEvent));
510 ev.xclient.type = ClientMessage;
511 ev.xclient.message_type = XInternAtom(dpy, "_WINDOWMAKER_COMMAND", False);
512 ev.xclient.window = DefaultRootWindow(dpy);
513 ev.xclient.format = 8;
514 strncpy(ev.xclient.data.b, "Reconfigure", sizeof(ev.xclient.data.b));
516 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureRedirectMask, &ev);
517 XFlush(dpy);
520 return 0;