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