set explicit colors instead of using rc file
[disorder] / disobedience / appearance.c
CommitLineData
d4ef4132
RK
1/*
2 * This file is part of Disobedience
3 * Copyright (C) 2007 Richard Kettlewell
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 */
20/** @file disobedience/appearance.c
21 * @brief Visual appearance of Disobedience
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"
34#include "split.h"!
35#include <sys/stat.h>
36
37/** @brief Background colors for tools - menus, icons, etc. */
38GdkColor tool_bg = { 0, 0xDC00, 0xDA00, 0xD500 };
39
40/** @brief Background color for active tool */
41GdkColor tool_active;
42
43/** @brief Foreground colors for tools */
44GdkColor tool_fg = { 0, 0x0000, 0x0000, 0x0000 };
45
46/** @brief Foreground colors for inactive tools */
47GdkColor inactive_tool_fg = { 0, 0x8000, 0x8000, 0x8000 };
48
49/** @brief Background for inactive tools (e.g. noncurrent tabs) */
50GdkColor offtool_bg = { 0, 0xC400, 0xC200, 0xBD00 };
51
52/** @brief Background color for the various layouts */
53GdkColor layout_bg = { 0, 0xFFFF, 0xFFFF, 0xFFFF };
54
55/** @brief Title-row background color */
56GdkColor title_bg = { 0, 0x0000, 0x0000, 0x0000 };
57
58/** @brief Title-row foreground color */
59GdkColor title_fg = { 0, 0xFFFF, 0xFFFF, 0xFFFF };
60
61/** @brief Even-row background color */
62GdkColor even_bg = { 0, 0xFFFF, 0xEC00, 0xEBFF };
63
64/** @brief Odd-row background color */
65GdkColor odd_bg = { 0, 0xFFFF, 0xFFFF, 0xFFFF };
66
67/** @brief Active-row background color */
68GdkColor active_bg = { 0, 0xE000, 0xFFFF, 0xE000 };
69
70/** @brief Item foreground color */
71GdkColor item_fg = { 0, 0x0000, 0x0000, 0x0000 };
72
73/** @brief Selected background color */
74GdkColor selected_bg = { 0, 0x4B00, 0x6900, 0x8300 };
75
76/** @brief Selected foreground color */
77GdkColor selected_fg = { 0, 0xFFFF, 0xFFFF, 0xFFFF };
78
79/** @brief Search results */
80GdkColor search_bg = { 0, 0xFFFF, 0xFFFF, 0x0000 };
81
82/** @brief Drag target color */
83GdkColor drag_target = { 0, 0x6666, 0x6666, 0x6666 };
84
85struct colordesc {
86 GdkColor *color;
87 const char *name;
88 const char *description;
89};
90
91#define COLOR(name, description) { &name, #name, description }
92
93/** @brief Table of configurable colors
94 *
95 * Some of the descriptions could be improve!
96 */
97static const struct colordesc colors[] = {
98 COLOR(tool_bg, "Tool background color"),
99 COLOR(tool_fg, "Tool foreground color"),
100 COLOR(offtool_bg, "Subsiduary tool color"),
101 COLOR(layout_bg, "Layout background color"),
102 COLOR(title_bg, "Title row background color"),
103 COLOR(title_fg, "Title row foreground color"),
104 COLOR(even_bg, "Even row background color"),
105 COLOR(odd_bg, "Odd row background color"),
106 COLOR(active_bg, "Playing row background color"),
107 COLOR(item_fg, "Track foreground color"),
108 COLOR(selected_bg, "Selected item background color"),
109 COLOR(selected_fg, "Selected item foreground color"),
110 COLOR(search_bg, "Search result background color"),
111 COLOR(drag_target, "Drag target color"),
112};
113
114#define NCOLORS (sizeof colors / sizeof *colors)
115
116void save_appearance(void) {
117 char *dir, *path, *tmp;
118 FILE *fp = 0;
119 size_t n;
120
121 byte_xasprintf(&dir, "%s/.disorder", getenv("HOME"));
122 byte_xasprintf(&path, "%s/disobedience", dir);
123 byte_xasprintf(&tmp, "%s.tmp", path);
124 mkdir(dir, 02700); /* make sure directory exists */
125 if(!(fp = fopen(tmp, "w"))) {
126 fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
127 tmp, strerror(errno));
128 goto done;
129 }
130 for(n = 0; n < NCOLORS; ++n)
131 if(fprintf(fp, "color %-20s 0x%04X 0x%04X 0x%04X\n", colors[n].name,
132 colors[n].color->red,
133 colors[n].color->green,
134 colors[n].color->blue) < 0) {
135 fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
136 tmp, strerror(errno));
137 goto done;
138 }
139 if(fclose(fp) < 0) {
140 fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
141 tmp, strerror(errno));
142 fp = 0;
143 goto done;
144 }
145 fp = 0;
146 if(rename(tmp, path) < 0)
147 fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s to %s: %s",
148 tmp, path, strerror(errno));
149done:
150 if(fp)
151 fclose(fp);
152}
153
154static inline unsigned clamp(unsigned n) {
155 return n > 0xFFFF ? 0xFFFF : n;
156}
157
158void load_appearance(void) {
159 char *path, *line;
160 FILE *fp;
161 size_t n;
162 char **vec;
163 int nvec;
164
165 byte_xasprintf(&path, "%s/.disorder/disobedience", getenv("HOME"));
166 if(!(fp = fopen(path, "r"))) {
167 if(errno != ENOENT)
168 fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
169 path, strerror(errno));
170 return;
171 }
172 while(!inputline(path, fp, &line, '\n')) {
173 if(!(vec = split(line, &nvec, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0))
174 || !nvec)
175 continue;
176 if(!strcmp(vec[0], "color")) {
177 if(nvec != 5) {
178 error(0, "%s: malformed '%s' command", path, vec[0]);
179 continue;
180 }
181 for(n = 0; n < NCOLORS && strcmp(colors[n].name, vec[1]); ++n)
182 ;
183 if(n >= NCOLORS) {
184 error(0, "%s: unknown color '%s'", path, vec[1]);
185 continue;
186 }
187 colors[n].color->red = strtoul(vec[2], 0, 0);
188 colors[n].color->green = strtoul(vec[3], 0, 0);
189 colors[n].color->blue = strtoul(vec[4], 0, 0);
190 } else
191 /* mention errors but otherwise ignore them */
192 error(0, "%s: unknown command '%s'", path, vec[0]);
193 }
194 if(ferror(fp)) {
195 fpopup_msg(GTK_MESSAGE_ERROR, "error reading %s: %s",
196 path, strerror(errno));
197 fclose(fp);
198 }
199 tool_active = tool_bg;
200 tool_active.red = clamp(105 * tool_active.red / 100);
201 tool_active.green = clamp(105 * tool_active.green / 100);
202 tool_active.blue = clamp(105 * tool_active.blue / 100);
203}
204
205/** @brief Callback used by set_tool_colors() */
206static void set_tool_colors_callback(GtkWidget *w,
207 gpointer attribute((unused)) data) {
208 set_tool_colors(w);
209}
210
211/** @brief Recursively set tool widget colors */
212void set_tool_colors(GtkWidget *w) {
213 GtkWidget *child;
214
215 gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &tool_bg);
216 gtk_widget_modify_bg(w, GTK_STATE_SELECTED, &selected_bg);
217 gtk_widget_modify_bg(w, GTK_STATE_PRELIGHT, &selected_bg);
218 gtk_widget_modify_bg(w, GTK_STATE_INSENSITIVE, &tool_bg);
219 gtk_widget_modify_fg(w, GTK_STATE_NORMAL, &tool_fg);
220 gtk_widget_modify_fg(w, GTK_STATE_SELECTED, &selected_fg);
221 gtk_widget_modify_fg(w, GTK_STATE_PRELIGHT, &selected_fg);
222 gtk_widget_modify_fg(w, GTK_STATE_INSENSITIVE, &inactive_tool_fg);
223 if(GTK_IS_CONTAINER(w))
224 gtk_container_foreach(GTK_CONTAINER(w), set_tool_colors_callback, 0);
225 if(GTK_IS_MENU_ITEM(w)
226 && (child = gtk_menu_item_get_submenu(GTK_MENU_ITEM(w))))
227 set_tool_colors(child);
228}
229
230/** @brief Set the colors for a slider */
231void set_slider_colors(GtkWidget *w) {
232 if(!w)
233 return;
234 gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &tool_bg);
235 gtk_widget_modify_bg(w, GTK_STATE_ACTIVE, &tool_bg);
236 gtk_widget_modify_bg(w, GTK_STATE_SELECTED, &tool_active);
237 gtk_widget_modify_bg(w, GTK_STATE_PRELIGHT, &tool_active);
238 gtk_widget_modify_fg(w, GTK_STATE_NORMAL, &tool_fg);
239 gtk_widget_modify_fg(w, GTK_STATE_ACTIVE, &tool_fg);
240 gtk_widget_modify_fg(w, GTK_STATE_SELECTED, &tool_fg);
241 gtk_widget_modify_fg(w, GTK_STATE_PRELIGHT, &tool_fg);
242}
243
244/*
245Local Variables:
246c-basic-offset:2
247comment-column:40
248fill-column:79
249indent-tabs-mode:nil
250End:
251*/