2 * This file is part of Disobedience
3 * Copyright (C) 2007 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 2 of the License, or
8 * (at your option) any later version.
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.
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
20 /** @file disobedience/appearance.c
21 * @brief Visual appearance of Disobedience
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
28 * This file therefore collects all the colors of the Disobedience UI
29 * and (in time) will have a configuration dialog too.
32 #include "disobedience.h"
33 #include "inputline.h"
37 /** @brief Background colors for tools - menus, icons, etc. */
38 GdkColor tool_bg = { 0, 0xDC00, 0xDA00, 0xD500 };
40 /** @brief Background color for active tool */
43 /** @brief Foreground colors for tools */
44 GdkColor tool_fg = { 0, 0x0000, 0x0000, 0x0000 };
46 /** @brief Foreground colors for inactive tools */
47 GdkColor inactive_tool_fg = { 0, 0x8000, 0x8000, 0x8000 };
49 /** @brief Background for inactive tools (e.g. noncurrent tabs) */
50 GdkColor offtool_bg = { 0, 0xC400, 0xC200, 0xBD00 };
52 /** @brief Background color for the various layouts */
53 GdkColor layout_bg = { 0, 0xFFFF, 0xFFFF, 0xFFFF };
55 /** @brief Title-row background color */
56 GdkColor title_bg = { 0, 0x0000, 0x0000, 0x0000 };
58 /** @brief Title-row foreground color */
59 GdkColor title_fg = { 0, 0xFFFF, 0xFFFF, 0xFFFF };
61 /** @brief Even-row background color */
62 GdkColor even_bg = { 0, 0xFFFF, 0xEC00, 0xEBFF };
64 /** @brief Odd-row background color */
65 GdkColor odd_bg = { 0, 0xFFFF, 0xFFFF, 0xFFFF };
67 /** @brief Active-row background color */
68 GdkColor active_bg = { 0, 0xE000, 0xFFFF, 0xE000 };
70 /** @brief Item foreground color */
71 GdkColor item_fg = { 0, 0x0000, 0x0000, 0x0000 };
73 /** @brief Selected background color */
74 GdkColor selected_bg = { 0, 0x4B00, 0x6900, 0x8300 };
76 /** @brief Selected foreground color */
77 GdkColor selected_fg = { 0, 0xFFFF, 0xFFFF, 0xFFFF };
79 /** @brief Search results */
80 GdkColor search_bg = { 0, 0xFFFF, 0xFFFF, 0x0000 };
82 /** @brief Drag target color */
83 GdkColor drag_target = { 0, 0x6666, 0x6666, 0x6666 };
88 const char *description;
91 #define COLOR(name, description) { &name, #name, description }
93 /** @brief Table of configurable colors
95 * Some of the descriptions could be improve!
97 static 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"),
114 #define NCOLORS (sizeof colors / sizeof *colors)
116 void save_appearance(void) {
117 char *dir, *path, *tmp;
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));
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));
140 fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
141 tmp, strerror(errno));
146 if(rename(tmp, path) < 0)
147 fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s to %s: %s",
148 tmp, path, strerror(errno));
154 static inline unsigned clamp(unsigned n) {
155 return n > 0xFFFF ? 0xFFFF : n;
158 void load_appearance(void) {
165 byte_xasprintf(&path, "%s/.disorder/disobedience", getenv("HOME"));
166 if(!(fp = fopen(path, "r"))) {
168 fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
169 path, strerror(errno));
172 while(!inputline(path, fp, &line, '\n')) {
173 if(!(vec = split(line, &nvec, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0))
176 if(!strcmp(vec[0], "color")) {
178 error(0, "%s: malformed '%s' command", path, vec[0]);
181 for(n = 0; n < NCOLORS && strcmp(colors[n].name, vec[1]); ++n)
184 error(0, "%s: unknown color '%s'", path, vec[1]);
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);
191 /* mention errors but otherwise ignore them */
192 error(0, "%s: unknown command '%s'", path, vec[0]);
195 fpopup_msg(GTK_MESSAGE_ERROR, "error reading %s: %s",
196 path, strerror(errno));
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);
205 /** @brief Callback used by set_tool_colors() */
206 static void set_tool_colors_callback(GtkWidget *w,
207 gpointer attribute((unused)) data) {
211 /** @brief Recursively set tool widget colors */
212 void set_tool_colors(GtkWidget *w) {
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);
230 /** @brief Set the colors for a slider */
231 void set_slider_colors(GtkWidget *w) {
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);