Code update for Window Maker version 0.50.0
[wmaker-crm.git] / util / setstyle.c
blobdab03b3ebb4475dc43be0927211edff348af2772
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 #include <stdlib.h>
25 #include <stdio.h>
26 #include <proplist.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
30 #include <string.h>
32 #include "../src/wconfig.h"
35 char *FontOptions[] = {
36 "IconTitleFont",
37 "ClipTitleFont",
38 "DisplayFont",
39 "MenuTextFont",
40 "MenuTitleFont",
41 NULL
46 char *ProgName;
47 int ignoreFonts = 0;
50 char*
51 defaultsPathForDomain(char *domain)
53 char path[1024];
54 char *gspath, *tmp;
56 gspath = getenv("GNUSTEP_USER_ROOT");
57 if (gspath) {
58 strcpy(path, gspath);
59 strcat(path, "/");
60 } else {
61 char *home;
63 home = getenv("HOME");
64 if (!home) {
65 printf("%s:could not get HOME environment variable!\n", ProgName);
66 exit(0);
69 strcpy(path, home);
70 strcat(path, "/GNUstep/");
72 strcat(path, DEFAULTS_DIR);
73 strcat(path, "/");
74 strcat(path, domain);
76 tmp = malloc(strlen(path)+2);
77 strcpy(tmp, path);
79 return tmp;
83 void
84 hackPaths(proplist_t style, char *prefix)
86 proplist_t keys;
87 proplist_t key;
88 proplist_t value;
89 int i;
92 keys = PLGetAllDictionaryKeys(style);
94 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
95 key = PLGetArrayElement(keys, i);
97 value = PLGetDictionaryEntry(style, key);
98 if (value && PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
99 proplist_t type;
100 char *t;
102 type = PLGetArrayElement(value, 0);
103 t = PLGetString(type);
104 if (t && (strcasecmp(t, "tpixmap")==0
105 || strcasecmp(t, "spixmap")==0
106 || strcasecmp(t, "cpixmap")==0
107 || strcasecmp(t, "tvgradient")==0
108 || strcasecmp(t, "thgradient")==0
109 || strcasecmp(t, "tdgradient")==0)) {
110 proplist_t file;
111 char buffer[4018];
113 file = PLGetArrayElement(value, 1);
114 sprintf(buffer, "%s/%s", prefix, PLGetString(file));
115 PLRemoveArrayElement(value, 1);
116 PLInsertArrayElement(value, PLMakeString(buffer), 1);
126 * since some of the options introduce incompatibilities, we will need
127 * to do a kluge here or the themes ppl will get real annoying.
128 * So, treat for the absence of the following options:
129 * IconTitleColor
130 * IconTitleBack
132 void
133 hackStyle(proplist_t style)
135 proplist_t keys;
136 proplist_t tmp;
137 int i;
138 int foundIconTitle = 0;
140 keys = PLGetAllDictionaryKeys(style);
142 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
143 char *str;
145 tmp = PLGetArrayElement(keys, i);
146 str = PLGetString(tmp);
147 if (str) {
148 int j, found;
150 for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
151 if (strcasecmp(str, FontOptions[j])==0) {
152 PLRemoveDictionaryEntry(style, tmp);
153 found = 1;
154 break;
157 if (found)
158 continue;
160 if (strcasecmp(str, "IconTitleColor")==0
161 || strcasecmp(str, "IconTitleBack")==0) {
162 foundIconTitle = 1;
167 if (!foundIconTitle) {
168 /* set the default values */
169 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleColor"));
170 if (tmp) {
171 PLInsertDictionaryEntry(style, PLMakeString("IconTitleColor"),
172 tmp);
175 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleBack"));
176 if (tmp) {
177 proplist_t type;
178 proplist_t value;
179 char *str;
181 type = PLGetArrayElement(tmp, 0);
182 if (!type)
183 return;
185 value = NULL;
187 str = PLGetString(type);
188 if (strcasecmp(str, "solid")==0) {
189 value = PLGetArrayElement(tmp, 1);
190 } else if (strcasecmp(str, "dgradient")==0
191 || strcasecmp(str, "hgradient")==0
192 || strcasecmp(str, "vgradient")==0) {
193 proplist_t c1, c2;
194 int r1, g1, b1, r2, g2, b2;
195 char buffer[32];
197 c1 = PLGetArrayElement(tmp, 1);
198 c2 = PLGetArrayElement(tmp, 2);
199 if (sscanf(PLGetString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
200 && sscanf(PLGetString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
201 sprintf(buffer, "#%2x%2x%2x", (r1+r2)/2, (g1+g2)/2,
202 (b1+b2)/2);
203 value = PLMakeString(buffer);
204 } else {
205 value = c1;
207 } else if (strcasecmp(str, "mdgradient")==0
208 || strcasecmp(str, "mhgradient")==0
209 || strcasecmp(str, "mvgradient")==0) {
211 value = PLGetArrayElement(tmp, 1);
213 } else if (strcasecmp(str, "tpixmap")==0
214 || strcasecmp(str, "cpixmap")==0
215 || strcasecmp(str, "spixmap")==0) {
217 value = PLGetArrayElement(tmp, 2);
220 if (value) {
221 PLInsertDictionaryEntry(style, PLMakeString("IconTitleBack"),
222 value);
229 BOOL
230 StringCompareHook(proplist_t pl1, proplist_t pl2)
232 char *str1, *str2;
234 str1 = PLGetString(pl1);
235 str2 = PLGetString(pl2);
237 if (strcasecmp(str1, str2)==0)
238 return YES;
239 else
240 return NO;
246 int
247 main(int argc, char **argv)
249 proplist_t prop, style;
250 char *path;
251 struct stat statbuf;
253 ProgName = argv[0];
255 if (argc<2) {
256 printf("Syntax:\n%s [-nofonts] <style file>\n", argv[0]);
257 exit(1);
260 if (argc == 3) {
261 if (strcmp(argv[1], "-nofonts")==0) {
262 ignoreFonts = 1;
263 } else {
264 printf("Syntax:\n%s <style file> [-nofonts]\n", argv[0]);
265 exit(1);
269 PLSetStringCmpHook(StringCompareHook);
271 path = defaultsPathForDomain("WindowMaker");
273 prop = PLGetProplistWithPath(path);
274 if (!prop) {
275 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
276 ProgName, path);
277 exit(1);
280 if (stat(argv[argc-1], &statbuf) < 0) {
281 perror(argv[argc-1]);
282 exit(1);
285 if (S_ISDIR(statbuf.st_mode)) {
286 char buffer[4018];
287 char *prefix;
289 if (*argv[argc-1] != '/') {
290 if (!getcwd(buffer, 4000)) {
291 printf("%s: complete path for %s is too long\n", ProgName,
292 argv[argc-1]);
293 exit(1);
295 if (strlen(buffer) + strlen(argv[argc-1]) > 4000) {
296 printf("%s: complete path for %s is too long\n", ProgName,
297 argv[argc-1]);
298 exit(1);
300 strcat(buffer, "/");
301 } else {
302 buffer[0] = 0;
304 strcat(buffer, argv[argc-1]);
306 prefix = malloc(strlen(buffer)+10);
307 if (!prefix) {
308 printf("%s: out of memory\n", ProgName);
309 exit(1);
311 strcpy(prefix, buffer);
313 strcat(buffer, "/style");
315 style = PLGetProplistWithPath(buffer);
316 if (!style) {
317 printf("%s:could not load style file \"%s\".\n", ProgName,
318 buffer);
319 exit(1);
322 hackPaths(style, prefix);
323 free(prefix);
324 } else {
325 style = PLGetProplistWithPath(argv[argc-1]);
326 if (!style) {
327 printf("%s:could not load style file \"%s\".\n", ProgName,
328 argv[argc-1]);
329 exit(1);
333 hackStyle(style);
335 PLMergeDictionaries(prop, style);
337 PLSave(prop, YES);
339 exit(0);