2 * This file is part of Disobedience
3 * Copyright (C) 2007, 2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/settings.c
19 * @brief Disobedience settings
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
26 * This file therefore collects all the colors of the Disobedience UI
27 * and (in time) will have a configuration dialog too.
30 #include "disobedience.h"
31 #include "inputline.h"
35 /** @brief HTML displayer */
36 const char *browser
= BROWSER
;
38 /** @brief Default style for layouts */
39 GtkStyle
*layout_style
;
41 /** @brief Title-row style for layouts */
42 GtkStyle
*title_style
;
44 /** @brief Even-row style for layouts */
47 /** @brief Odd-row style for layouts */
50 /** @brief Active-row style for layouts */
51 GtkStyle
*active_style
;
53 /** @brief Style for tools */
56 /** @brief Style for search results */
57 GtkStyle
*search_style
;
59 /** @brief Style for drag targets */
62 /** @brief Table of 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
},
77 #define NSTYLES (sizeof styles / sizeof *styles)
79 /** @brief Table of state types */
80 static const char *const states
[] = {
88 #define NSTATES (sizeof states / sizeof *states)
90 /** @brief Table of colors */
95 { "fg", offsetof(GtkStyle
, fg
) },
96 { "bg", offsetof(GtkStyle
, bg
) },
99 #define NCOLORS (sizeof colors / sizeof *colors)
101 /** @brief Initialize styles */
102 void 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();
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
;
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
];
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
];
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
];
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
];
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;
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;
149 /* Tools we leave at GTK+ defaults */
152 void save_settings(void) {
153 char *dir
, *path
, *tmp
;
157 byte_xasprintf(&dir
, "%s/.disorder", getenv("HOME"));
158 byte_xasprintf(&path
, "%s/disobedience", dir
);
159 byte_xasprintf(&tmp
, "%s.tmp", path
);
160 mkdir(dir
, 02700); /* make sure directory exists */
161 if(!(fp
= fopen(tmp
, "w"))) {
162 fpopup_msg(GTK_MESSAGE_ERROR
, "error opening %s: %s",
163 tmp
, strerror(errno
));
167 "# automatically generated!\n\n") < 0)
169 for(n
= 0; n
< NSTYLES
; ++n
)
170 for(c
= 0; c
< NCOLORS
; ++c
)
171 for(m
= 0; m
< NSTATES
; ++m
) {
172 const GdkColor
*color
= (GdkColor
*)((char *)styles
[n
].style
+ colors
[c
].offset
) + m
;
173 if(fprintf(fp
, "color %8s %12s %s 0x%04x 0x%04x 0x%04x\n",
174 styles
[n
].name
, states
[m
], colors
[c
].name
,
180 if(fprintf(fp
, "browser %s\n", browser
) < 0)
185 fpopup_msg(GTK_MESSAGE_ERROR
, "error writing to %s: %s",
186 tmp
, strerror(errno
));
190 if(rename(tmp
, path
) < 0)
191 fpopup_msg(GTK_MESSAGE_ERROR
, "error renaming %s to %s: %s",
192 tmp
, path
, strerror(errno
));
198 static inline unsigned clamp(unsigned n
) {
199 return n
> 0xFFFF ?
0xFFFF : n
;
202 void load_settings(void) {
209 byte_xasprintf(&path
, "%s/.disorder/disobedience", getenv("HOME"));
210 if(!(fp
= fopen(path
, "r"))) {
212 fpopup_msg(GTK_MESSAGE_ERROR
, "error opening %s: %s",
213 path
, strerror(errno
));
215 while(!inputline(path
, fp
, &line
, '\n')) {
216 if(!(vec
= split(line
, &nvec
, SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0))
219 if(!strcmp(vec
[0], "color")) {
222 disorder_error(0, "%s: malformed '%s' command", path
, vec
[0]);
225 for(n
= 0; n
< NSTYLES
&& strcmp(styles
[n
].name
, vec
[1]); ++n
)
228 disorder_error(0, "%s: unknown style '%s'", path
, vec
[1]);
231 for(m
= 0; m
< NSTATES
&& strcmp(states
[m
], vec
[2]); ++m
)
234 disorder_error(0, "%s: unknown state '%s'", path
, vec
[2]);
237 for(c
= 0; c
< NCOLORS
&& strcmp(colors
[c
].name
, vec
[3]); ++c
)
240 disorder_error(0, "%s: unknown color '%s'", path
, vec
[3]);
243 color
= (GdkColor
*)((char *)styles
[n
].style
+ colors
[c
].offset
) + m
;
244 color
->red
= strtoul(vec
[4], 0, 0);
245 color
->green
= strtoul(vec
[5], 0, 0);
246 color
->blue
= strtoul(vec
[6], 0, 0);
247 } else if(!strcmp(vec
[0], "browser")) {
249 disorder_error(0, "%s: malformed '%s' command", path
, vec
[0]);
254 /* mention errors but otherwise ignore them */
255 disorder_error(0, "%s: unknown command '%s'", path
, vec
[0]);
258 fpopup_msg(GTK_MESSAGE_ERROR
, "error reading %s: %s",
259 path
, strerror(errno
));
265 /** @brief Recursively set tool widget colors
267 * This is currently unused; the idea was to allow for configurability without
268 * allowing GTK+ to override our use of color, but things seem generally better
269 * without this particular call.
271 void set_tool_colors(GtkWidget
attribute((unused
)) *w
) {
274 /** @brief Pop up a settings editor widget */
275 void popup_settings(void) {
276 static GtkWidget
*settings_window
;
280 if(settings_window
) {
281 gtk_window_present(GTK_WINDOW(settings_window
));
284 /* Create the window */
285 settings_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
286 gtk_widget_set_style(settings_window
, tool_style
);
287 gtk_window_set_title(GTK_WINDOW(settings_window
), "Disobedience Settings");
288 /* Clear the pointer to the window widget when it is closed */
289 g_signal_connect(settings_window
, "destroy",
290 G_CALLBACK(gtk_widget_destroyed
), &settings_window
);
292 /* The color settings live in a big table */
293 table
= gtk_table_new(2 * NSTYLES
+ 1/*rows */, NSTATES
+ 1/*cols*/,
294 TRUE
/*homogeneous*/);
296 for(row
= 0; row
< 2 * NSTYLES
; ++row
) {
299 byte_xasprintf(&legend
, "%s %s", styles
[row
/ 2].name
,
300 row
% 2 ?
"background" : "foreground");
301 gtk_table_attach(GTK_TABLE(table
),
302 gtk_label_new(legend
),
308 for(col
= 0; col
< NSTATES
; ++col
) {
309 gtk_table_attach(GTK_TABLE(table
),
310 gtk_label_new(states
[col
]),
316 /* The actual colors */
317 for(row
= 0; row
< 2 * NSTYLES
; ++row
) {
318 for(col
= 0; col
< NSTATES
; ++col
) {
319 GdkColor
*const c
= &(row
% 2
320 ?
(**styles
[row
/ 2].style
).bg
321 : (**styles
[row
/ 2].style
).fg
)[col
];
322 gtk_table_attach(GTK_TABLE(table
),
323 gtk_color_button_new_with_color(c
),
330 gtk_container_add(GTK_CONTAINER(settings_window
), frame_widget(table
, NULL
));
331 gtk_widget_show_all(settings_window
);
332 /* TODO: save settings
334 TODO: impose settings when they are set