Make window border size configurable.
[wmaker-crm.git] / util / getstyle.c
blobe9ee696aafe0453727540717ed1d5adfa679863d
1 /* getstyle.c - outputs style related options from WindowMaker to stdout
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 <sys/types.h>
27 #include <sys/stat.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <getopt.h>
32 #include <libgen.h>
33 #include <limits.h>
34 #include <pwd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <unistd.h>
41 #include <WINGs/WUtil.h>
43 #define RETRY( x ) do { \
44 x; \
45 } while (errno == EINTR);
47 #ifndef PATH_MAX
48 #define PATH_MAX 1024
49 #endif
51 #include "../src/wconfig.h"
53 #ifndef GLOBAL_DEFAULTS_SUBDIR
54 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
55 #endif
57 /* table of style related options */
58 static char *options[] = {
59 "TitleJustify",
60 "ClipTitleFont",
61 "WindowTitleFont",
62 "MenuTitleFont",
63 "MenuTextFont",
64 "IconTitleFont",
65 "DisplayFont",
66 "LargeDisplayFont",
67 "WindowTitleExtendSpace",
68 "MenuTitleExtendSpace",
69 "MenuTextExtendSpace",
70 "HighlightColor",
71 "HighlightTextColor",
72 "ClipTitleColor",
73 "CClipTitleColor",
74 "FTitleColor",
75 "PTitleColor",
76 "UTitleColor",
77 "FTitleBack",
78 "PTitleBack",
79 "UTitleBack",
80 "ResizebarBack",
81 "MenuTitleColor",
82 "MenuTextColor",
83 "MenuDisabledColor",
84 "MenuTitleBack",
85 "MenuTextBack",
86 "IconBack",
87 "IconTitleColor",
88 "IconTitleBack",
89 "FrameBorderWidth",
90 "FrameBorderColor",
91 "FrameSelectedBorderColor",
92 "MenuStyle",
93 "WindowTitleExtendSpace",
94 "MenuTitleExtendSpace",
95 "MenuTextExtendSpace",
96 NULL
99 /* table of theme related options */
100 static char *theme_options[] = {
101 "WorkspaceBack",
102 "NormalCursor",
103 "ArrowCursor",
104 "MoveCursor",
105 "ResizeCursor",
106 "TopLeftResizeCursor",
107 "TopRightResizeCursor",
108 "BottomLeftResizeCursor",
109 "BottomRightResizeCursor",
110 "VerticalResizeCursor",
111 "HorizontalResizeCursor",
112 "WaitCursor",
113 "QuestionCursor",
114 "TextCursor",
115 "SelectCursor",
116 NULL
119 /* table of style related fonts */
121 static char *font_options[] = {
122 "ClipTitleFont",
123 "WindowTitleFont",
124 "MenuTitleFont",
125 "MenuTextFont",
126 "IconTitleFont",
127 "DisplayFont",
128 "LargeDisplayFont",
129 NULL
132 extern char *__progname;
134 WMPropList *PixmapPath = NULL;
136 char *ThemePath = NULL;
138 extern char *convertFont(char *font, Bool keepXLFD);
140 void print_help(int print_usage, int exitval)
142 printf("Usage: %s [-t] [-p] [-h] [-v] [file]\n", __progname);
143 if (print_usage) {
144 puts("Retrieves style/theme configuration and outputs to ~/GNUstep/Library/WindowMaker/Themes/file.themed/style or to stdout");
145 puts("");
146 puts(" -h, --help display this help and exit");
147 puts(" -v, --version output version information and exit");
148 puts(" -t, --theme-options output theme related options when producing a style file");
149 puts(" -p, --pack produce output as a theme pack");
151 exit(exitval);
154 static Bool isFontOption(char *option)
156 int i;
158 for (i = 0; font_options[i] != NULL; i++) {
159 if (strcasecmp(option, font_options[i]) == 0) {
160 return True;
164 return False;
167 void findCopyFile(char *dir, char *file)
169 char *fullPath;
171 fullPath = wfindfileinarray(PixmapPath, file);
172 if (!fullPath) {
173 wwarning("Could not find file %s", file);
174 if (ThemePath)
175 (void)wrmdirhier(ThemePath);
177 wcopy_file(dir, fullPath, fullPath);
178 free(fullPath);
181 void makeThemePack(WMPropList * style, char *themeName)
183 WMPropList *keys;
184 WMPropList *key;
185 WMPropList *value;
186 int i;
187 size_t themeNameLen;
188 char *themeDir, *t;
190 if ((t = wusergnusteppath()) == NULL)
191 return;
192 themeNameLen = strlen(t) + strlen(themeName) + 50;
193 themeDir = wmalloc(themeNameLen);
194 snprintf(themeDir, themeNameLen, "%s/Library/WindowMaker/Themes/%s.themed/", t, themeName);
195 ThemePath = themeDir;
197 if (!wmkdirhier(themeDir)) {
198 wwarning("Could not make theme dir %s\n", themeDir);
199 return;
202 keys = WMGetPLDictionaryKeys(style);
204 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
205 key = WMGetFromPLArray(keys, i);
207 value = WMGetFromPLDictionary(style, key);
208 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
209 WMPropList *type;
210 char *t;
212 type = WMGetFromPLArray(value, 0);
213 t = WMGetFromPLString(type);
214 if (t == NULL)
215 continue;
217 if (strcasecmp(t, "tpixmap") == 0 ||
218 strcasecmp(t, "spixmap") == 0 ||
219 strcasecmp(t, "cpixmap") == 0 ||
220 strcasecmp(t, "mpixmap") == 0 ||
221 strcasecmp(t, "tdgradient") == 0 ||
222 strcasecmp(t, "tvgradient") == 0 ||
223 strcasecmp(t, "thgradient") == 0) {
225 WMPropList *file;
226 char *p;
227 char *newPath;
229 file = WMGetFromPLArray(value, 1);
231 p = strrchr(WMGetFromPLString(file), '/');
232 if (p) {
233 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
235 newPath = wstrdup(p + 1);
236 WMDeleteFromPLArray(value, 1);
237 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
238 free(newPath);
239 } else {
240 findCopyFile(themeDir, WMGetFromPLString(file));
242 } else if (strcasecmp(t, "bitmap") == 0) {
244 WMPropList *file;
245 char *p;
246 char *newPath;
248 file = WMGetFromPLArray(value, 1);
250 p = strrchr(WMGetFromPLString(file), '/');
251 if (p) {
252 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
254 newPath = wstrdup(p + 1);
255 WMDeleteFromPLArray(value, 1);
256 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
257 free(newPath);
258 } else {
259 findCopyFile(themeDir, WMGetFromPLString(file));
262 file = WMGetFromPLArray(value, 2);
264 p = strrchr(WMGetFromPLString(file), '/');
265 if (p) {
266 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
268 newPath = wstrdup(p + 1);
269 WMDeleteFromPLArray(value, 2);
270 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
271 free(newPath);
272 } else {
273 findCopyFile(themeDir, WMGetFromPLString(file));
280 int main(int argc, char **argv)
282 WMPropList *prop, *style, *key, *val;
283 char *path;
284 int i, ch, theme_too = 0, make_pack = 0;
285 char *style_file = NULL;
287 struct option longopts[] = {
288 { "pack", no_argument, NULL, 'p' },
289 { "theme-options", no_argument, NULL, 't' },
290 { "version", no_argument, NULL, 'v' },
291 { "help", no_argument, NULL, 'h' },
292 { NULL, 0, NULL, 0 }
295 while ((ch = getopt_long(argc, argv, "ptvh", longopts, NULL)) != -1)
296 switch(ch) {
297 case 'v':
298 printf("%s (Window Maker %s)\n", __progname, VERSION);
299 return 0;
300 /* NOTREACHED */
301 case 'h':
302 print_help(1, 0);
303 /* NOTREACHED */
304 case 'p':
305 make_pack = 1;
306 theme_too = 1;
307 break;
308 case 't':
309 theme_too = 1;
310 case 0:
311 break;
312 default:
313 print_help(0, 1);
314 /* NOTREACHED */
317 /* At most one non-option ARGV-element is accepted (the theme name) */
318 if (argc - optind > 1)
319 print_help(0, 1);
321 if (argc - optind == 1)
322 style_file = argv[argc - 1];
324 if (make_pack && !style_file) {
325 printf("%s: you must supply a name for the theme pack\n", __progname);
326 return 1;
329 WMPLSetCaseSensitive(False);
331 path = wdefaultspathfordomain("WindowMaker");
333 prop = WMReadPropListFromFile(path);
334 if (!prop) {
335 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path);
336 return 1;
339 /* get global value */
340 path = wglobaldefaultspathfordomain("WindowMaker");
342 val = WMReadPropListFromFile(path);
343 if (val) {
344 WMMergePLDictionaries(val, prop, True);
345 WMReleasePropList(prop);
346 prop = val;
349 style = WMCreatePLDictionary(NULL, NULL);
351 for (i = 0; options[i] != NULL; i++) {
352 key = WMCreatePLString(options[i]);
354 val = WMGetFromPLDictionary(prop, key);
355 if (val) {
356 WMRetainPropList(val);
357 if (isFontOption(options[i])) {
358 char *newfont, *oldfont;
360 oldfont = WMGetFromPLString(val);
361 newfont = convertFont(oldfont, False);
362 /* newfont is a reference to old if conversion is not needed */
363 if (newfont != oldfont) {
364 WMReleasePropList(val);
365 val = WMCreatePLString(newfont);
366 wfree(newfont);
369 WMPutInPLDictionary(style, key, val);
370 WMReleasePropList(val);
372 WMReleasePropList(key);
375 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
376 if (val)
377 PixmapPath = val;
379 if (theme_too) {
380 for (i = 0; theme_options[i] != NULL; i++) {
381 key = WMCreatePLString(theme_options[i]);
383 val = WMGetFromPLDictionary(prop, key);
384 if (val)
385 WMPutInPLDictionary(style, key, val);
389 if (make_pack) {
390 char *path;
392 makeThemePack(style, style_file);
394 path = wmalloc(strlen(ThemePath) + 32);
395 strcpy(path, ThemePath);
396 strcat(path, "/style");
397 WMWritePropListToFile(style, path);
398 wfree(path);
399 } else {
400 if (style_file) {
401 WMWritePropListToFile(style, style_file);
402 } else {
403 puts(WMGetPropListDescription(style, True));
406 return 0;