Initial update from my source tree. For 0.52.0
[wmaker-crm.git] / util / setstyle.c
blob07e09dc332a6e288663da6e08e93a0b700b429b4
1 /* setstyle.c - loads style related options to wmaker
3 * WindowMaker 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.
24 #define PROG_VERSION "setstyle (Window Maker) 0.2"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <proplist.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
32 #include <string.h>
34 #include "../src/wconfig.h"
37 char *FontOptions[] = {
38 "IconTitleFont",
39 "ClipTitleFont",
40 "DisplayFont",
41 "MenuTextFont",
42 "MenuTitleFont",
43 "WindowTitleFont",
44 NULL
49 char *ProgName;
50 int ignoreFonts = 0;
53 char*
54 defaultsPathForDomain(char *domain)
56 char path[1024];
57 char *gspath, *tmp;
59 gspath = getenv("GNUSTEP_USER_ROOT");
60 if (gspath) {
61 strcpy(path, gspath);
62 strcat(path, "/");
63 } else {
64 char *home;
66 home = getenv("HOME");
67 if (!home) {
68 printf("%s:could not get HOME environment variable!\n", ProgName);
69 exit(0);
72 strcpy(path, home);
73 strcat(path, "/GNUstep/");
75 strcat(path, DEFAULTS_DIR);
76 strcat(path, "/");
77 strcat(path, domain);
79 tmp = malloc(strlen(path)+2);
80 strcpy(tmp, path);
82 return tmp;
86 void
87 hackPaths(proplist_t style, char *prefix)
89 proplist_t keys;
90 proplist_t key;
91 proplist_t value;
92 int i;
95 keys = PLGetAllDictionaryKeys(style);
97 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
98 key = PLGetArrayElement(keys, i);
100 value = PLGetDictionaryEntry(style, key);
101 if (value && PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
102 proplist_t type;
103 char *t;
105 type = PLGetArrayElement(value, 0);
106 t = PLGetString(type);
107 if (t && (strcasecmp(t, "tpixmap")==0
108 || strcasecmp(t, "spixmap")==0
109 || strcasecmp(t, "mpixmap")==0
110 || strcasecmp(t, "cpixmap")==0
111 || strcasecmp(t, "tvgradient")==0
112 || strcasecmp(t, "thgradient")==0
113 || strcasecmp(t, "tdgradient")==0)) {
114 proplist_t file;
115 char buffer[4018];
117 file = PLGetArrayElement(value, 1);
118 sprintf(buffer, "%s/%s", prefix, PLGetString(file));
119 PLRemoveArrayElement(value, 1);
120 PLInsertArrayElement(value, PLMakeString(buffer), 1);
130 * since some of the options introduce incompatibilities, we will need
131 * to do a kluge here or the themes ppl will get real annoying.
132 * So, treat for the absence of the following options:
133 * IconTitleColor
134 * IconTitleBack
136 void
137 hackStyle(proplist_t style)
139 proplist_t keys;
140 proplist_t tmp;
141 int i;
142 int foundIconTitle = 0;
144 keys = PLGetAllDictionaryKeys(style);
146 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
147 char *str;
149 tmp = PLGetArrayElement(keys, i);
150 str = PLGetString(tmp);
151 if (str) {
152 int j, found;
154 if (ignoreFonts) {
155 for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
156 if (strcasecmp(str, FontOptions[j])==0) {
157 PLRemoveDictionaryEntry(style, tmp);
158 found = 1;
159 break;
162 if (found)
163 continue;
166 if (strcasecmp(str, "IconTitleColor")==0
167 || strcasecmp(str, "IconTitleBack")==0) {
168 foundIconTitle = 1;
173 if (!foundIconTitle) {
174 /* set the default values */
175 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleColor"));
176 if (tmp) {
177 PLInsertDictionaryEntry(style, PLMakeString("IconTitleColor"),
178 tmp);
181 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleBack"));
182 if (tmp) {
183 proplist_t type;
184 proplist_t value;
185 char *str;
187 type = PLGetArrayElement(tmp, 0);
188 if (!type)
189 return;
191 value = NULL;
193 str = PLGetString(type);
194 if (strcasecmp(str, "solid")==0) {
195 value = PLGetArrayElement(tmp, 1);
196 } else if (strcasecmp(str, "dgradient")==0
197 || strcasecmp(str, "hgradient")==0
198 || strcasecmp(str, "vgradient")==0) {
199 proplist_t c1, c2;
200 int r1, g1, b1, r2, g2, b2;
201 char buffer[32];
203 c1 = PLGetArrayElement(tmp, 1);
204 c2 = PLGetArrayElement(tmp, 2);
205 if (sscanf(PLGetString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
206 && sscanf(PLGetString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
207 sprintf(buffer, "#%2x%2x%2x", (r1+r2)/2, (g1+g2)/2,
208 (b1+b2)/2);
209 value = PLMakeString(buffer);
210 } else {
211 value = c1;
213 } else if (strcasecmp(str, "mdgradient")==0
214 || strcasecmp(str, "mhgradient")==0
215 || strcasecmp(str, "mvgradient")==0) {
217 value = PLGetArrayElement(tmp, 1);
219 } else if (strcasecmp(str, "tpixmap")==0
220 || strcasecmp(str, "cpixmap")==0
221 || strcasecmp(str, "spixmap")==0) {
223 value = PLGetArrayElement(tmp, 2);
226 if (value) {
227 PLInsertDictionaryEntry(style, PLMakeString("IconTitleBack"),
228 value);
235 BOOL
236 StringCompareHook(proplist_t pl1, proplist_t pl2)
238 char *str1, *str2;
240 str1 = PLGetString(pl1);
241 str2 = PLGetString(pl2);
243 if (strcasecmp(str1, str2)==0)
244 return YES;
245 else
246 return NO;
251 void
252 print_help()
254 printf("Usage: %s [OPTIONS] FILE\n", ProgName);
255 puts("Reads style/theme configuration from FILE and updates Window Maker.");
256 puts("");
257 puts(" --no-fonts ignore font related options");
258 puts(" --help display this help and exit");
259 puts(" --version output version information and exit");
264 int
265 main(int argc, char **argv)
267 proplist_t prop, style;
268 char *path;
269 char *file = NULL;
270 struct stat statbuf;
271 int i;
273 ProgName = argv[0];
275 if (argc<2) {
276 printf("%s: missing argument\n", ProgName);
277 printf("Try '%s --help' for more information\n", ProgName);
278 exit(1);
281 for (i = 1; i < argc; i++) {
282 if (strcmp("--no-fonts", argv[i])==0) {
283 ignoreFonts = 1;
284 } else if (strcmp("--version", argv[i])==0) {
285 puts(PROG_VERSION);
286 exit(0);
287 } else if (strcmp("--help", argv[i])==0) {
288 print_help();
289 exit(0);
290 } else {
291 if (file) {
292 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
293 printf("Try '%s --help' for more information\n", ProgName);
294 exit(1);
296 file = argv[i];
300 PLSetStringCmpHook(StringCompareHook);
302 path = defaultsPathForDomain("WindowMaker");
304 prop = PLGetProplistWithPath(path);
305 if (!prop) {
306 perror(path);
307 printf("%s:could not load WindowMaker configuration file.\n",
308 ProgName);
309 exit(1);
312 if (stat(file, &statbuf) < 0) {
313 perror(file);
314 exit(1);
317 if (S_ISDIR(statbuf.st_mode)) {
318 char buffer[4018];
319 char *prefix;
321 if (*argv[argc-1] != '/') {
322 if (!getcwd(buffer, 4000)) {
323 printf("%s: complete path for %s is too long\n", ProgName,
324 file);
325 exit(1);
327 if (strlen(buffer) + strlen(file) > 4000) {
328 printf("%s: complete path for %s is too long\n", ProgName,
329 file);
330 exit(1);
332 strcat(buffer, "/");
333 } else {
334 buffer[0] = 0;
336 strcat(buffer, file);
338 prefix = malloc(strlen(buffer)+10);
339 if (!prefix) {
340 printf("%s: out of memory\n", ProgName);
341 exit(1);
343 strcpy(prefix, buffer);
345 strcat(buffer, "/style");
347 style = PLGetProplistWithPath(buffer);
348 if (!style) {
349 perror(buffer);
350 printf("%s:could not load style file.\n", ProgName);
351 exit(1);
354 hackPaths(style, prefix);
355 free(prefix);
356 } else {
357 style = PLGetProplistWithPath(file);
358 if (!style) {
359 perror(file);
360 printf("%s:could not load style file.\n", ProgName);
361 exit(1);
365 if (!PLIsArray(style)) {
366 printf("%s: '%s' is not a style file/theme\n", ProgName, file);
367 exit(1);
370 hackStyle(style);
372 PLMergeDictionaries(prop, style);
374 PLSave(prop, YES);
376 exit(0);