Icon: Replace code by call to helper function in wIconCreateWithIconFile
[wmaker-crm.git] / util / getstyle.c
blobbde802dc3b13033ee55a59add39967fb419386d4
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 "MenuStyle",
90 "WindowTitleExtendSpace",
91 "MenuTitleExtendSpace",
92 "MenuTextExtendSpace",
93 NULL
96 /* table of theme related options */
97 static char *theme_options[] = {
98 "WorkspaceBack",
99 "NormalCursor",
100 "ArrowCursor",
101 "MoveCursor",
102 "ResizeCursor",
103 "TopLeftResizeCursor",
104 "TopRightResizeCursor",
105 "BottomLeftResizeCursor",
106 "BottomRightResizeCursor",
107 "VerticalResizeCursor",
108 "HorizontalResizeCursor",
109 "WaitCursor",
110 "QuestionCursor",
111 "TextCursor",
112 "SelectCursor",
113 NULL
116 /* table of style related fonts */
118 static char *font_options[] = {
119 "ClipTitleFont",
120 "WindowTitleFont",
121 "MenuTitleFont",
122 "MenuTextFont",
123 "IconTitleFont",
124 "DisplayFont",
125 "LargeDisplayFont",
126 NULL
129 extern char *__progname;
131 WMPropList *PixmapPath = NULL;
133 char *ThemePath = NULL;
135 extern char *convertFont(char *font, Bool keepXLFD);
137 void print_help(int print_usage, int exitval)
139 printf("Usage: %s [-t] [-p] [-h] [-v] [file]\n", __progname);
140 if (print_usage) {
141 puts("Retrieves style/theme configuration and outputs to ~/GNUstep/Library/WindowMaker/Themes/file.themed/style or to stdout");
142 puts("");
143 puts(" -h, --help display this help and exit");
144 puts(" -v, --version output version information and exit");
145 puts(" -t, --theme-options output theme related options when producing a style file");
146 puts(" -p, --pack produce output as a theme pack");
148 exit(exitval);
151 static Bool isFontOption(char *option)
153 int i;
155 for (i = 0; font_options[i] != NULL; i++) {
156 if (strcasecmp(option, font_options[i]) == 0) {
157 return True;
161 return False;
164 void findCopyFile(char *dir, char *file)
166 char *fullPath;
168 fullPath = wfindfileinarray(PixmapPath, file);
169 if (!fullPath) {
170 wwarning("Could not find file %s", file);
171 if (ThemePath)
172 (void)wrmdirhier(ThemePath);
174 wcopy_file(dir, fullPath, fullPath);
175 free(fullPath);
178 void makeThemePack(WMPropList * style, char *themeName)
180 WMPropList *keys;
181 WMPropList *key;
182 WMPropList *value;
183 int i;
184 size_t themeNameLen;
185 char *themeDir, *t;
187 if ((t = wusergnusteppath()) == NULL)
188 return;
189 themeNameLen = strlen(t) + strlen(themeName) + 50;
190 themeDir = wmalloc(themeNameLen);
191 snprintf(themeDir, themeNameLen, "%s/Library/WindowMaker/Themes/%s.themed/", t, themeName);
192 ThemePath = themeDir;
194 if (!wmkdirhier(themeDir)) {
195 wwarning("Could not make theme dir %s\n", themeDir);
196 return;
199 keys = WMGetPLDictionaryKeys(style);
201 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
202 key = WMGetFromPLArray(keys, i);
204 value = WMGetFromPLDictionary(style, key);
205 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
206 WMPropList *type;
207 char *t;
209 type = WMGetFromPLArray(value, 0);
210 t = WMGetFromPLString(type);
211 if (t == NULL)
212 continue;
214 if (strcasecmp(t, "tpixmap") == 0 ||
215 strcasecmp(t, "spixmap") == 0 ||
216 strcasecmp(t, "cpixmap") == 0 ||
217 strcasecmp(t, "mpixmap") == 0 ||
218 strcasecmp(t, "tdgradient") == 0 ||
219 strcasecmp(t, "tvgradient") == 0 ||
220 strcasecmp(t, "thgradient") == 0) {
222 WMPropList *file;
223 char *p;
224 char *newPath;
226 file = WMGetFromPLArray(value, 1);
228 p = strrchr(WMGetFromPLString(file), '/');
229 if (p) {
230 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
232 newPath = wstrdup(p + 1);
233 WMDeleteFromPLArray(value, 1);
234 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
235 free(newPath);
236 } else {
237 findCopyFile(themeDir, WMGetFromPLString(file));
239 } else if (strcasecmp(t, "bitmap") == 0) {
241 WMPropList *file;
242 char *p;
243 char *newPath;
245 file = WMGetFromPLArray(value, 1);
247 p = strrchr(WMGetFromPLString(file), '/');
248 if (p) {
249 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
251 newPath = wstrdup(p + 1);
252 WMDeleteFromPLArray(value, 1);
253 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
254 free(newPath);
255 } else {
256 findCopyFile(themeDir, WMGetFromPLString(file));
259 file = WMGetFromPLArray(value, 2);
261 p = strrchr(WMGetFromPLString(file), '/');
262 if (p) {
263 wcopy_file(themeDir, WMGetFromPLString(file), WMGetFromPLString(file));
265 newPath = wstrdup(p + 1);
266 WMDeleteFromPLArray(value, 2);
267 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
268 free(newPath);
269 } else {
270 findCopyFile(themeDir, WMGetFromPLString(file));
277 int main(int argc, char **argv)
279 WMPropList *prop, *style, *key, *val;
280 char *path;
281 int i, ch, theme_too = 0, make_pack = 0;
282 char *style_file = NULL;
284 struct option longopts[] = {
285 { "pack", no_argument, NULL, 'p' },
286 { "theme-options", no_argument, NULL, 't' },
287 { "version", no_argument, NULL, 'v' },
288 { "help", no_argument, NULL, 'h' },
289 { NULL, 0, NULL, 0 }
292 while ((ch = getopt_long(argc, argv, "ptvh", longopts, NULL)) != -1)
293 switch(ch) {
294 case 'v':
295 printf("%s (Window Maker %s)\n", __progname, VERSION);
296 return 0;
297 /* NOTREACHED */
298 case 'h':
299 print_help(1, 0);
300 /* NOTREACHED */
301 case 'p':
302 make_pack = 1;
303 theme_too = 1;
304 break;
305 case 't':
306 theme_too = 1;
307 case 0:
308 break;
309 default:
310 print_help(0, 1);
311 /* NOTREACHED */
314 /* At most one non-option ARGV-element is accepted (the theme name) */
315 if (argc - optind > 1)
316 print_help(0, 1);
318 if (argc - optind == 1)
319 style_file = argv[argc - 1];
321 if (make_pack && !style_file) {
322 printf("%s: you must supply a name for the theme pack\n", __progname);
323 return 1;
326 WMPLSetCaseSensitive(False);
328 path = wdefaultspathfordomain("WindowMaker");
330 prop = WMReadPropListFromFile(path);
331 if (!prop) {
332 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path);
333 return 1;
336 /* get global value */
337 path = wglobaldefaultspathfordomain("WindowMaker");
339 val = WMReadPropListFromFile(path);
340 if (val) {
341 WMMergePLDictionaries(val, prop, True);
342 WMReleasePropList(prop);
343 prop = val;
346 style = WMCreatePLDictionary(NULL, NULL);
348 for (i = 0; options[i] != NULL; i++) {
349 key = WMCreatePLString(options[i]);
351 val = WMGetFromPLDictionary(prop, key);
352 if (val) {
353 WMRetainPropList(val);
354 if (isFontOption(options[i])) {
355 char *newfont, *oldfont;
357 oldfont = WMGetFromPLString(val);
358 newfont = convertFont(oldfont, False);
359 /* newfont is a reference to old if conversion is not needed */
360 if (newfont != oldfont) {
361 WMReleasePropList(val);
362 val = WMCreatePLString(newfont);
363 wfree(newfont);
366 WMPutInPLDictionary(style, key, val);
367 WMReleasePropList(val);
369 WMReleasePropList(key);
372 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
373 if (val)
374 PixmapPath = val;
376 if (theme_too) {
377 for (i = 0; theme_options[i] != NULL; i++) {
378 key = WMCreatePLString(theme_options[i]);
380 val = WMGetFromPLDictionary(prop, key);
381 if (val)
382 WMPutInPLDictionary(style, key, val);
386 if (make_pack) {
387 char *path;
389 makeThemePack(style, style_file);
391 path = wmalloc(strlen(ThemePath) + 32);
392 strcpy(path, ThemePath);
393 strcat(path, "/style");
394 WMWritePropListToFile(style, path);
395 wfree(path);
396 } else {
397 if (style_file) {
398 WMWritePropListToFile(style, style_file);
399 } else {
400 puts(WMGetPropListDescription(style, True));
403 return 0;