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