| 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/settings.c |
| 21 | * @brief Disobedience settings |
| 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 | /** @brief HTML displayer */ |
| 83 | const char *browser = BROWSER; |
| 84 | |
| 85 | struct 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 | */ |
| 97 | static const struct colordesc colors[] = { |
| 98 | COLOR(tool_bg, "Tool background color"), |
| 99 | COLOR(tool_fg, "Tool foreground color"), |
| 100 | COLOR(layout_bg, "Layout background color"), |
| 101 | COLOR(title_bg, "Title row background color"), |
| 102 | COLOR(title_fg, "Title row foreground color"), |
| 103 | COLOR(even_bg, "Even row background color"), |
| 104 | COLOR(odd_bg, "Odd row background color"), |
| 105 | COLOR(active_bg, "Playing row background color"), |
| 106 | COLOR(item_fg, "Track foreground color"), |
| 107 | COLOR(selected_bg, "Selected item background color"), |
| 108 | COLOR(selected_fg, "Selected item foreground color"), |
| 109 | COLOR(search_bg, "Search result background color"), |
| 110 | COLOR(drag_target, "Drag target color"), |
| 111 | }; |
| 112 | |
| 113 | #define NCOLORS (sizeof colors / sizeof *colors) |
| 114 | |
| 115 | void save_settings(void) { |
| 116 | char *dir, *path, *tmp; |
| 117 | FILE *fp = 0; |
| 118 | size_t n; |
| 119 | |
| 120 | byte_xasprintf(&dir, "%s/.disorder", getenv("HOME")); |
| 121 | byte_xasprintf(&path, "%s/disobedience", dir); |
| 122 | byte_xasprintf(&tmp, "%s.tmp", path); |
| 123 | mkdir(dir, 02700); /* make sure directory exists */ |
| 124 | if(!(fp = fopen(tmp, "w"))) { |
| 125 | fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s", |
| 126 | tmp, strerror(errno)); |
| 127 | goto done; |
| 128 | } |
| 129 | for(n = 0; n < NCOLORS; ++n) |
| 130 | if(fprintf(fp, "color %-20s 0x%04X 0x%04X 0x%04X\n", colors[n].name, |
| 131 | colors[n].color->red, |
| 132 | colors[n].color->green, |
| 133 | colors[n].color->blue) < 0) { |
| 134 | fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s", |
| 135 | tmp, strerror(errno)); |
| 136 | goto done; |
| 137 | } |
| 138 | if(fclose(fp) < 0) { |
| 139 | fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s", |
| 140 | tmp, strerror(errno)); |
| 141 | fp = 0; |
| 142 | goto done; |
| 143 | } |
| 144 | fp = 0; |
| 145 | if(rename(tmp, path) < 0) |
| 146 | fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s to %s: %s", |
| 147 | tmp, path, strerror(errno)); |
| 148 | done: |
| 149 | if(fp) |
| 150 | fclose(fp); |
| 151 | } |
| 152 | |
| 153 | static inline unsigned clamp(unsigned n) { |
| 154 | return n > 0xFFFF ? 0xFFFF : n; |
| 155 | } |
| 156 | |
| 157 | void load_settings(void) { |
| 158 | char *path, *line; |
| 159 | FILE *fp; |
| 160 | size_t n; |
| 161 | char **vec; |
| 162 | int nvec; |
| 163 | |
| 164 | byte_xasprintf(&path, "%s/.disorder/disobedience", getenv("HOME")); |
| 165 | if(!(fp = fopen(path, "r"))) { |
| 166 | if(errno != ENOENT) |
| 167 | fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s", |
| 168 | path, strerror(errno)); |
| 169 | } else { |
| 170 | while(!inputline(path, fp, &line, '\n')) { |
| 171 | if(!(vec = split(line, &nvec, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)) |
| 172 | || !nvec) |
| 173 | continue; |
| 174 | if(!strcmp(vec[0], "color")) { |
| 175 | if(nvec != 5) { |
| 176 | error(0, "%s: malformed '%s' command", path, vec[0]); |
| 177 | continue; |
| 178 | } |
| 179 | for(n = 0; n < NCOLORS && strcmp(colors[n].name, vec[1]); ++n) |
| 180 | ; |
| 181 | if(n >= NCOLORS) { |
| 182 | error(0, "%s: unknown color '%s'", path, vec[1]); |
| 183 | continue; |
| 184 | } |
| 185 | colors[n].color->red = strtoul(vec[2], 0, 0); |
| 186 | colors[n].color->green = strtoul(vec[3], 0, 0); |
| 187 | colors[n].color->blue = strtoul(vec[4], 0, 0); |
| 188 | } else |
| 189 | /* mention errors but otherwise ignore them */ |
| 190 | error(0, "%s: unknown command '%s'", path, vec[0]); |
| 191 | } |
| 192 | if(ferror(fp)) { |
| 193 | fpopup_msg(GTK_MESSAGE_ERROR, "error reading %s: %s", |
| 194 | path, strerror(errno)); |
| 195 | fclose(fp); |
| 196 | } |
| 197 | } |
| 198 | tool_active = tool_bg; |
| 199 | tool_active.red = clamp(105 * tool_active.red / 100); |
| 200 | tool_active.green = clamp(105 * tool_active.green / 100); |
| 201 | tool_active.blue = clamp(105 * tool_active.blue / 100); |
| 202 | } |
| 203 | |
| 204 | /** @brief Callback used by set_tool_colors() */ |
| 205 | static void set_tool_colors_callback(GtkWidget *w, |
| 206 | gpointer attribute((unused)) data) { |
| 207 | set_tool_colors(w); |
| 208 | } |
| 209 | |
| 210 | /** @brief Recursively set tool widget colors */ |
| 211 | void set_tool_colors(GtkWidget *w) { |
| 212 | GtkWidget *child; |
| 213 | |
| 214 | gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &tool_bg); |
| 215 | gtk_widget_modify_bg(w, GTK_STATE_SELECTED, &selected_bg); |
| 216 | gtk_widget_modify_bg(w, GTK_STATE_PRELIGHT, &selected_bg); |
| 217 | gtk_widget_modify_bg(w, GTK_STATE_INSENSITIVE, &tool_bg); |
| 218 | gtk_widget_modify_fg(w, GTK_STATE_NORMAL, &tool_fg); |
| 219 | gtk_widget_modify_fg(w, GTK_STATE_SELECTED, &selected_fg); |
| 220 | gtk_widget_modify_fg(w, GTK_STATE_PRELIGHT, &selected_fg); |
| 221 | gtk_widget_modify_fg(w, GTK_STATE_INSENSITIVE, &inactive_tool_fg); |
| 222 | if(GTK_IS_CONTAINER(w)) |
| 223 | gtk_container_foreach(GTK_CONTAINER(w), set_tool_colors_callback, 0); |
| 224 | if(GTK_IS_MENU_ITEM(w) |
| 225 | && (child = gtk_menu_item_get_submenu(GTK_MENU_ITEM(w)))) |
| 226 | set_tool_colors(child); |
| 227 | } |
| 228 | |
| 229 | /** @brief Set the colors for a slider */ |
| 230 | void set_slider_colors(GtkWidget *w) { |
| 231 | if(!w) |
| 232 | return; |
| 233 | gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &tool_bg); |
| 234 | gtk_widget_modify_bg(w, GTK_STATE_ACTIVE, &tool_bg); |
| 235 | gtk_widget_modify_bg(w, GTK_STATE_SELECTED, &tool_active); |
| 236 | gtk_widget_modify_bg(w, GTK_STATE_PRELIGHT, &tool_active); |
| 237 | gtk_widget_modify_fg(w, GTK_STATE_NORMAL, &tool_fg); |
| 238 | gtk_widget_modify_fg(w, GTK_STATE_ACTIVE, &tool_fg); |
| 239 | gtk_widget_modify_fg(w, GTK_STATE_SELECTED, &tool_fg); |
| 240 | gtk_widget_modify_fg(w, GTK_STATE_PRELIGHT, &tool_fg); |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | Local Variables: |
| 245 | c-basic-offset:2 |
| 246 | comment-column:40 |
| 247 | fill-column:79 |
| 248 | indent-tabs-mode:nil |
| 249 | End: |
| 250 | */ |