Update for 0.51.0
[wmaker-crm.git] / util / setstyle.c
blobf61a0beeb9563b40be3d64f1cb7e31e6febed39f
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 "WindowTitleFont",
42 NULL
47 char *ProgName;
48 int ignoreFonts = 0;
51 char*
52 defaultsPathForDomain(char *domain)
54 char path[1024];
55 char *gspath, *tmp;
57 gspath = getenv("GNUSTEP_USER_ROOT");
58 if (gspath) {
59 strcpy(path, gspath);
60 strcat(path, "/");
61 } else {
62 char *home;
64 home = getenv("HOME");
65 if (!home) {
66 printf("%s:could not get HOME environment variable!\n", ProgName);
67 exit(0);
70 strcpy(path, home);
71 strcat(path, "/GNUstep/");
73 strcat(path, DEFAULTS_DIR);
74 strcat(path, "/");
75 strcat(path, domain);
77 tmp = malloc(strlen(path)+2);
78 strcpy(tmp, path);
80 return tmp;
84 void
85 hackPaths(proplist_t style, char *prefix)
87 proplist_t keys;
88 proplist_t key;
89 proplist_t value;
90 int i;
93 keys = PLGetAllDictionaryKeys(style);
95 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
96 key = PLGetArrayElement(keys, i);
98 value = PLGetDictionaryEntry(style, key);
99 if (value && PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
100 proplist_t type;
101 char *t;
103 type = PLGetArrayElement(value, 0);
104 t = PLGetString(type);
105 if (t && (strcasecmp(t, "tpixmap")==0
106 || strcasecmp(t, "spixmap")==0
107 || strcasecmp(t, "mpixmap")==0
108 || strcasecmp(t, "cpixmap")==0
109 || strcasecmp(t, "tvgradient")==0
110 || strcasecmp(t, "thgradient")==0
111 || strcasecmp(t, "tdgradient")==0)) {
112 proplist_t file;
113 char buffer[4018];
115 file = PLGetArrayElement(value, 1);
116 sprintf(buffer, "%s/%s", prefix, PLGetString(file));
117 PLRemoveArrayElement(value, 1);
118 PLInsertArrayElement(value, PLMakeString(buffer), 1);
128 * since some of the options introduce incompatibilities, we will need
129 * to do a kluge here or the themes ppl will get real annoying.
130 * So, treat for the absence of the following options:
131 * IconTitleColor
132 * IconTitleBack
134 void
135 hackStyle(proplist_t style)
137 proplist_t keys;
138 proplist_t tmp;
139 int i;
140 int foundIconTitle = 0;
142 keys = PLGetAllDictionaryKeys(style);
144 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
145 char *str;
147 tmp = PLGetArrayElement(keys, i);
148 str = PLGetString(tmp);
149 if (str) {
150 int j, found;
152 if (ignoreFonts) {
153 for (j = 0, found = 0; FontOptions[j]!=NULL; j++) {
154 if (strcasecmp(str, FontOptions[j])==0) {
155 PLRemoveDictionaryEntry(style, tmp);
156 found = 1;
157 break;
160 if (found)
161 continue;
164 if (strcasecmp(str, "IconTitleColor")==0
165 || strcasecmp(str, "IconTitleBack")==0) {
166 foundIconTitle = 1;
171 if (!foundIconTitle) {
172 /* set the default values */
173 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleColor"));
174 if (tmp) {
175 PLInsertDictionaryEntry(style, PLMakeString("IconTitleColor"),
176 tmp);
179 tmp = PLGetDictionaryEntry(style, PLMakeString("FTitleBack"));
180 if (tmp) {
181 proplist_t type;
182 proplist_t value;
183 char *str;
185 type = PLGetArrayElement(tmp, 0);
186 if (!type)
187 return;
189 value = NULL;
191 str = PLGetString(type);
192 if (strcasecmp(str, "solid")==0) {
193 value = PLGetArrayElement(tmp, 1);
194 } else if (strcasecmp(str, "dgradient")==0
195 || strcasecmp(str, "hgradient")==0
196 || strcasecmp(str, "vgradient")==0) {
197 proplist_t c1, c2;
198 int r1, g1, b1, r2, g2, b2;
199 char buffer[32];
201 c1 = PLGetArrayElement(tmp, 1);
202 c2 = PLGetArrayElement(tmp, 2);
203 if (sscanf(PLGetString(c1), "#%2x%2x%2x", &r1, &g1, &b1)==3
204 && sscanf(PLGetString(c2), "#%2x%2x%2x", &r2, &g2, &b2)==3) {
205 sprintf(buffer, "#%2x%2x%2x", (r1+r2)/2, (g1+g2)/2,
206 (b1+b2)/2);
207 value = PLMakeString(buffer);
208 } else {
209 value = c1;
211 } else if (strcasecmp(str, "mdgradient")==0
212 || strcasecmp(str, "mhgradient")==0
213 || strcasecmp(str, "mvgradient")==0) {
215 value = PLGetArrayElement(tmp, 1);
217 } else if (strcasecmp(str, "tpixmap")==0
218 || strcasecmp(str, "cpixmap")==0
219 || strcasecmp(str, "spixmap")==0) {
221 value = PLGetArrayElement(tmp, 2);
224 if (value) {
225 PLInsertDictionaryEntry(style, PLMakeString("IconTitleBack"),
226 value);
233 BOOL
234 StringCompareHook(proplist_t pl1, proplist_t pl2)
236 char *str1, *str2;
238 str1 = PLGetString(pl1);
239 str2 = PLGetString(pl2);
241 if (strcasecmp(str1, str2)==0)
242 return YES;
243 else
244 return NO;
250 int
251 main(int argc, char **argv)
253 proplist_t prop, style;
254 char *path;
255 struct stat statbuf;
257 ProgName = argv[0];
259 if (argc<2) {
260 printf("Syntax:\n%s [-nofonts] <style file>\n", argv[0]);
261 exit(1);
264 if (argc == 3) {
265 if (strcmp(argv[1], "-nofonts")==0) {
266 ignoreFonts = 1;
267 } else {
268 printf("Syntax:\n%s <style file> [-nofonts]\n", argv[0]);
269 exit(1);
273 PLSetStringCmpHook(StringCompareHook);
275 path = defaultsPathForDomain("WindowMaker");
277 prop = PLGetProplistWithPath(path);
278 if (!prop) {
279 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
280 ProgName, path);
281 exit(1);
284 if (stat(argv[argc-1], &statbuf) < 0) {
285 perror(argv[argc-1]);
286 exit(1);
289 if (S_ISDIR(statbuf.st_mode)) {
290 char buffer[4018];
291 char *prefix;
293 if (*argv[argc-1] != '/') {
294 if (!getcwd(buffer, 4000)) {
295 printf("%s: complete path for %s is too long\n", ProgName,
296 argv[argc-1]);
297 exit(1);
299 if (strlen(buffer) + strlen(argv[argc-1]) > 4000) {
300 printf("%s: complete path for %s is too long\n", ProgName,
301 argv[argc-1]);
302 exit(1);
304 strcat(buffer, "/");
305 } else {
306 buffer[0] = 0;
308 strcat(buffer, argv[argc-1]);
310 prefix = malloc(strlen(buffer)+10);
311 if (!prefix) {
312 printf("%s: out of memory\n", ProgName);
313 exit(1);
315 strcpy(prefix, buffer);
317 strcat(buffer, "/style");
319 style = PLGetProplistWithPath(buffer);
320 if (!style) {
321 printf("%s:could not load style file \"%s\".\n", ProgName,
322 buffer);
323 exit(1);
326 hackPaths(style, prefix);
327 free(prefix);
328 } else {
329 style = PLGetProplistWithPath(argv[argc-1]);
330 if (!style) {
331 printf("%s:could not load style file \"%s\".\n", ProgName,
332 argv[argc-1]);
333 exit(1);
337 hackStyle(style);
339 PLMergeDictionaries(prop, style);
341 PLSave(prop, YES);
343 exit(0);