Commit | Line | Data |
---|---|---|
d4ef4132 RK |
1 | /* |
2 | * This file is part of Disobedience | |
5aff007d | 3 | * Copyright (C) 2007, 2008 Richard Kettlewell |
d4ef4132 RK |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
f486ea18 RK |
20 | /** @file disobedience/settings.c |
21 | * @brief Disobedience settings | |
d4ef4132 RK |
22 | * |
23 | * Originally I attempted to use a built-in rc file to configure | |
24 | * Disobedience's colors. This is quite convenient but fails in the | |
25 | * face of themes, as the theme settings override the application | |
26 | * ones. | |
27 | * | |
28 | * This file therefore collects all the colors of the Disobedience UI | |
29 | * and (in time) will have a configuration dialog too. | |
30 | */ | |
31 | ||
32 | #include "disobedience.h" | |
33 | #include "inputline.h" | |
d308abea | 34 | #include "split.h" |
d4ef4132 RK |
35 | #include <sys/stat.h> |
36 | ||
f486ea18 RK |
37 | /** @brief HTML displayer */ |
38 | const char *browser = BROWSER; | |
39 | ||
b4f8eba1 RK |
40 | /** @brief Default style for layouts */ |
41 | GtkStyle *layout_style; | |
42 | ||
43 | /** @brief Title-row style for layouts */ | |
44 | GtkStyle *title_style; | |
45 | ||
46 | /** @brief Even-row style for layouts */ | |
47 | GtkStyle *even_style; | |
48 | ||
49 | /** @brief Odd-row style for layouts */ | |
50 | GtkStyle *odd_style; | |
51 | ||
52 | /** @brief Active-row style for layouts */ | |
53 | GtkStyle *active_style; | |
54 | ||
55 | /** @brief Style for tools */ | |
56 | GtkStyle *tool_style; | |
57 | ||
58 | /** @brief Style for search results */ | |
59 | GtkStyle *search_style; | |
60 | ||
61 | /** @brief Style for drag targets */ | |
62 | GtkStyle *drag_style; | |
63 | ||
64 | /** @brief Table of styles */ | |
65 | static const struct { | |
66 | const char *name; | |
67 | GtkStyle **style; | |
68 | } styles[] = { | |
69 | { "layout", &layout_style }, | |
70 | { "title", &title_style }, | |
71 | { "even", &even_style }, | |
72 | { "odd", &odd_style }, | |
73 | { "active", &active_style }, | |
74 | { "tool", &tool_style }, | |
75 | { "search", &search_style }, | |
76 | { "drag", &drag_style }, | |
77 | }; | |
78 | ||
79 | #define NSTYLES (sizeof styles / sizeof *styles) | |
80 | ||
81 | /** @brief Table of state types */ | |
82 | static const char *const states[] = { | |
83 | "normal", | |
84 | "active", | |
85 | "prelight", | |
86 | "selected", | |
87 | "insensitive" | |
88 | }; | |
89 | ||
90 | #define NSTATES (sizeof states / sizeof *states) | |
91 | ||
92 | /** @brief Table of colors */ | |
93 | static const struct { | |
94 | const char *name; | |
95 | size_t offset; | |
96 | } colors[] = { | |
97 | { "fg", offsetof(GtkStyle, fg) }, | |
98 | { "bg", offsetof(GtkStyle, bg) }, | |
99 | }; | |
100 | ||
101 | #define NCOLORS (sizeof colors / sizeof *colors) | |
102 | ||
103 | /** @brief Initialize styles */ | |
104 | void init_styles(void) { | |
105 | layout_style = gtk_style_new(); | |
106 | title_style = gtk_style_new(); | |
107 | even_style = gtk_style_new(); | |
108 | odd_style = gtk_style_new(); | |
109 | active_style = gtk_style_new(); | |
110 | search_style = gtk_style_new(); | |
111 | tool_style = gtk_style_new(); | |
112 | drag_style = gtk_style_new(); | |
113 | ||
114 | /* Style defaults */ | |
115 | ||
116 | /* Layouts are basically black on white */ | |
117 | layout_style->bg[GTK_STATE_NORMAL] = layout_style->white; | |
118 | layout_style->fg[GTK_STATE_NORMAL] = layout_style->black; | |
119 | ||
120 | /* Title row is inverted */ | |
121 | title_style->bg[GTK_STATE_NORMAL] = layout_style->fg[GTK_STATE_NORMAL]; | |
122 | title_style->fg[GTK_STATE_NORMAL] = layout_style->bg[GTK_STATE_NORMAL]; | |
123 | ||
124 | /* Active row is pastel green */ | |
125 | active_style->bg[GTK_STATE_NORMAL].red = 0xE000; | |
126 | active_style->bg[GTK_STATE_NORMAL].green = 0xFFFF; | |
127 | active_style->bg[GTK_STATE_NORMAL].blue = 0xE000; | |
128 | active_style->fg[GTK_STATE_NORMAL] = layout_style->fg[GTK_STATE_NORMAL]; | |
129 | ||
130 | /* Even rows are pastel red */ | |
131 | even_style->bg[GTK_STATE_NORMAL].red = 0xFFFF; | |
132 | even_style->bg[GTK_STATE_NORMAL].green = 0xEC00; | |
133 | even_style->bg[GTK_STATE_NORMAL].blue = 0xEC00; | |
134 | even_style->fg[GTK_STATE_NORMAL] = layout_style->fg[GTK_STATE_NORMAL]; | |
135 | ||
136 | /* Odd rows match the underlying layout */ | |
137 | odd_style->bg[GTK_STATE_NORMAL] = layout_style->bg[GTK_STATE_NORMAL]; | |
138 | odd_style->fg[GTK_STATE_NORMAL] = layout_style->fg[GTK_STATE_NORMAL]; | |
139 | ||
140 | /* Search results have a yellow background */ | |
141 | search_style->fg[GTK_STATE_NORMAL] = layout_style->fg[GTK_STATE_NORMAL]; | |
142 | search_style->bg[GTK_STATE_NORMAL].red = 0xFFFF; | |
143 | search_style->bg[GTK_STATE_NORMAL].green = 0xFFFF; | |
144 | search_style->bg[GTK_STATE_NORMAL].blue = 0x0000; | |
145 | ||
146 | /* Drag targets are grey */ | |
147 | drag_style->bg[GTK_STATE_NORMAL].red = 0x6666; | |
148 | drag_style->bg[GTK_STATE_NORMAL].green = 0x6666; | |
149 | drag_style->bg[GTK_STATE_NORMAL].blue = 0x6666; | |
150 | ||
151 | /* Tools we leave at GTK+ defaults */ | |
152 | } | |
153 | ||
f486ea18 | 154 | void save_settings(void) { |
d4ef4132 RK |
155 | char *dir, *path, *tmp; |
156 | FILE *fp = 0; | |
b4f8eba1 | 157 | size_t n, m, c; |
d4ef4132 RK |
158 | |
159 | byte_xasprintf(&dir, "%s/.disorder", getenv("HOME")); | |
160 | byte_xasprintf(&path, "%s/disobedience", dir); | |
161 | byte_xasprintf(&tmp, "%s.tmp", path); | |
162 | mkdir(dir, 02700); /* make sure directory exists */ | |
163 | if(!(fp = fopen(tmp, "w"))) { | |
164 | fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s", | |
165 | tmp, strerror(errno)); | |
166 | goto done; | |
167 | } | |
b4f8eba1 RK |
168 | if(fprintf(fp, |
169 | "# automatically generated!\n\n") < 0) | |
170 | goto write_error; | |
171 | for(n = 0; n < NSTYLES; ++n) | |
172 | for(c = 0; c < NCOLORS; ++c) | |
173 | for(m = 0; m < NSTATES; ++m) { | |
174 | const GdkColor *color = (GdkColor *)((char *)styles[n].style + colors[c].offset) + m; | |
175 | if(fprintf(fp, "color %8s %12s %s 0x%04x 0x%04x 0x%04x\n", | |
176 | styles[n].name, states[m], colors[c].name, | |
177 | color->red, | |
178 | color->green, | |
179 | color->blue) < 0) | |
180 | goto write_error; | |
181 | } | |
182 | if(fprintf(fp, "browser %s\n", browser) < 0) | |
183 | goto write_error; | |
d4ef4132 | 184 | if(fclose(fp) < 0) { |
b4f8eba1 RK |
185 | fp = 0; |
186 | write_error: | |
d4ef4132 RK |
187 | fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s", |
188 | tmp, strerror(errno)); | |
d4ef4132 RK |
189 | goto done; |
190 | } | |
191 | fp = 0; | |
192 | if(rename(tmp, path) < 0) | |
193 | fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s to %s: %s", | |
194 | tmp, path, strerror(errno)); | |
195 | done: | |
196 | if(fp) | |
197 | fclose(fp); | |
198 | } | |
199 | ||
200 | static inline unsigned clamp(unsigned n) { | |
201 | return n > 0xFFFF ? 0xFFFF : n; | |
202 | } | |
203 | ||
f486ea18 | 204 | void load_settings(void) { |
d4ef4132 RK |
205 | char *path, *line; |
206 | FILE *fp; | |
d4ef4132 RK |
207 | char **vec; |
208 | int nvec; | |
b4f8eba1 | 209 | size_t n, m, c; |
d4ef4132 RK |
210 | |
211 | byte_xasprintf(&path, "%s/.disorder/disobedience", getenv("HOME")); | |
212 | if(!(fp = fopen(path, "r"))) { | |
213 | if(errno != ENOENT) | |
214 | fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s", | |
215 | path, strerror(errno)); | |
f3258332 RK |
216 | } else { |
217 | while(!inputline(path, fp, &line, '\n')) { | |
218 | if(!(vec = split(line, &nvec, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)) | |
219 | || !nvec) | |
d4ef4132 | 220 | continue; |
f3258332 | 221 | if(!strcmp(vec[0], "color")) { |
b4f8eba1 RK |
222 | GdkColor *color; |
223 | if(nvec != 7) { | |
f3258332 RK |
224 | error(0, "%s: malformed '%s' command", path, vec[0]); |
225 | continue; | |
226 | } | |
b4f8eba1 RK |
227 | for(n = 0; n < NSTYLES && strcmp(styles[n].name, vec[1]); ++n) |
228 | ; | |
229 | if(n >= NSTYLES) { | |
230 | error(0, "%s: unknown style '%s'", path, vec[1]); | |
231 | continue; | |
232 | } | |
233 | for(m = 0; m < NSTATES && strcmp(states[m], vec[2]); ++m) | |
234 | ; | |
235 | if(m >= NSTATES) { | |
236 | error(0, "%s: unknown state '%s'", path, vec[2]); | |
237 | continue; | |
238 | } | |
239 | for(c = 0; c < NCOLORS && strcmp(colors[c].name, vec[3]); ++c) | |
240 | ; | |
241 | if(c >= NCOLORS) { | |
242 | error(0, "%s: unknown color '%s'", path, vec[3]); | |
243 | continue; | |
244 | } | |
245 | color = (GdkColor *)((char *)styles[n].style + colors[c].offset) + m; | |
246 | color->red = strtoul(vec[4], 0, 0); | |
247 | color->green = strtoul(vec[5], 0, 0); | |
248 | color->blue = strtoul(vec[6], 0, 0); | |
249 | } else if(!strcmp(vec[0], "browser")) { | |
250 | if(nvec != 2) { | |
251 | error(0, "%s: malformed '%s' command", path, vec[0]); | |
252 | continue; | |
253 | } | |
254 | browser = vec[1]; | |
f3258332 RK |
255 | } else |
256 | /* mention errors but otherwise ignore them */ | |
257 | error(0, "%s: unknown command '%s'", path, vec[0]); | |
258 | } | |
259 | if(ferror(fp)) { | |
260 | fpopup_msg(GTK_MESSAGE_ERROR, "error reading %s: %s", | |
261 | path, strerror(errno)); | |
262 | fclose(fp); | |
263 | } | |
d4ef4132 | 264 | } |
d4ef4132 RK |
265 | } |
266 | ||
87eb933d RK |
267 | /** @brief Recursively set tool widget colors |
268 | * | |
269 | * This is currently unused; the idea was to allow for configurability without | |
270 | * allowing GTK+ to override our use of color, but things seem generally better | |
271 | * without this particular call. | |
272 | */ | |
273 | void set_tool_colors(GtkWidget attribute((unused)) *w) { | |
d4ef4132 RK |
274 | } |
275 | ||
46fb1b05 RK |
276 | /** @brief Pop up a settings editor widget */ |
277 | void popup_settings(void) { | |
278 | static GtkWidget *settings_window; | |
279 | GtkWidget *table; | |
280 | unsigned row, col; | |
281 | ||
282 | if(settings_window) { | |
283 | gtk_window_present(GTK_WINDOW(settings_window)); | |
284 | return; | |
285 | } | |
286 | /* Create the window */ | |
287 | settings_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
288 | gtk_widget_set_style(settings_window, tool_style); | |
289 | gtk_window_set_title(GTK_WINDOW(settings_window), "Disobedience Settings"); | |
290 | /* Clear the pointer to the window widget when it is closed */ | |
291 | g_signal_connect(settings_window, "destroy", | |
292 | G_CALLBACK(gtk_widget_destroyed), &settings_window); | |
293 | ||
294 | /* The color settings live in a big table */ | |
295 | table = gtk_table_new(2 * NSTYLES + 1/*rows */, NSTATES + 1/*cols*/, | |
296 | TRUE/*homogeneous*/); | |
297 | /* Titles */ | |
298 | for(row = 0; row < 2 * NSTYLES; ++row) { | |
299 | char *legend; | |
300 | ||
301 | byte_xasprintf(&legend, "%s %s", styles[row / 2].name, | |
302 | row % 2 ? "background" : "foreground"); | |
303 | gtk_table_attach(GTK_TABLE(table), | |
304 | gtk_label_new(legend), | |
305 | 0, 1, | |
306 | row + 1, row + 2, | |
307 | GTK_FILL, GTK_FILL, | |
308 | 1, 1); | |
309 | } | |
310 | for(col = 0; col < NSTATES; ++col) { | |
311 | gtk_table_attach(GTK_TABLE(table), | |
312 | gtk_label_new(states[col]), | |
313 | col + 1, col + 2, | |
314 | 0, 1, | |
315 | GTK_FILL, GTK_FILL, | |
316 | 1, 1); | |
317 | } | |
318 | /* The actual colors */ | |
319 | for(row = 0; row < 2 * NSTYLES; ++row) { | |
320 | for(col = 0; col < NSTATES; ++col) { | |
321 | GdkColor *const c = &(row % 2 | |
322 | ? (**styles[row / 2].style).bg | |
323 | : (**styles[row / 2].style).fg)[col]; | |
324 | gtk_table_attach(GTK_TABLE(table), | |
325 | gtk_color_button_new_with_color(c), | |
326 | col + 1, col + 2, | |
327 | row + 1, row + 2, | |
328 | GTK_FILL, GTK_FILL, | |
329 | 1, 1); | |
330 | } | |
331 | } | |
e18c4734 | 332 | gtk_container_add(GTK_CONTAINER(settings_window), frame_widget(table, NULL)); |
46fb1b05 RK |
333 | gtk_widget_show_all(settings_window); |
334 | /* TODO: save settings | |
335 | TODO: web browser | |
336 | TODO: impose settings when they are set | |
337 | */ | |
338 | } | |
339 | ||
d4ef4132 RK |
340 | /* |
341 | Local Variables: | |
342 | c-basic-offset:2 | |
343 | comment-column:40 | |
344 | fill-column:79 | |
345 | indent-tabs-mode:nil | |
346 | End: | |
347 | */ |