Update to 24f58c58bb8d22c0e8e6c5ce43c536c47b719bc6
[gnt.git] / gntcolors.c
blobaac69aa8e587a93f4c77ad4db7466740423e9280
1 /**
2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "config.h"
25 #include <ncurses.h>
27 #include "gntcolors.h"
28 #include "gntstyle.h"
30 #include <glib.h>
32 #include <stdlib.h>
33 #include <string.h>
35 static gboolean hascolors;
36 static int custom_type = GNT_COLORS;
37 static struct
39 short r, g, b;
40 } colors[GNT_TOTAL_COLORS];
42 static void
43 backup_colors(void)
45 short i;
46 for (i = 0; i < GNT_TOTAL_COLORS; i++)
48 color_content(i, &colors[i].r,
49 &colors[i].g, &colors[i].b);
53 static gboolean
54 can_use_custom_color(void)
56 return (gnt_style_get_bool(GNT_STYLE_COLOR, FALSE) && can_change_color());
59 static void
60 restore_colors(void)
62 short i;
63 for (i = 0; i < GNT_TOTAL_COLORS; i++)
65 init_color(i, colors[i].r,
66 colors[i].g, colors[i].b);
70 void gnt_init_colors()
72 static gboolean init = FALSE;
73 int defaults;
75 if (init)
76 return;
77 init = TRUE;
79 start_color();
80 if (!(hascolors = has_colors()))
81 return;
82 defaults = use_default_colors();
84 if (can_use_custom_color())
86 backup_colors();
88 /* Do some init_color()s */
89 init_color(GNT_COLOR_BLACK, 0, 0, 0);
90 init_color(GNT_COLOR_RED, 1000, 0, 0);
91 init_color(GNT_COLOR_GREEN, 0, 1000, 0);
92 init_color(GNT_COLOR_BLUE, 250, 250, 700);
93 init_color(GNT_COLOR_WHITE, 1000, 1000, 1000);
94 init_color(GNT_COLOR_GRAY, 699, 699, 699);
95 init_color(GNT_COLOR_DARK_GRAY, 256, 256, 256);
97 /* Now some init_pair()s */
98 init_pair(GNT_COLOR_NORMAL, GNT_COLOR_BLACK, GNT_COLOR_WHITE);
99 init_pair(GNT_COLOR_HIGHLIGHT, GNT_COLOR_WHITE, GNT_COLOR_BLUE);
100 init_pair(GNT_COLOR_SHADOW, GNT_COLOR_BLACK, GNT_COLOR_DARK_GRAY);
102 init_pair(GNT_COLOR_TITLE, GNT_COLOR_WHITE, GNT_COLOR_BLUE);
103 init_pair(GNT_COLOR_TITLE_D, GNT_COLOR_WHITE, GNT_COLOR_GRAY);
105 init_pair(GNT_COLOR_TEXT_NORMAL, GNT_COLOR_WHITE, GNT_COLOR_BLUE);
106 init_pair(GNT_COLOR_HIGHLIGHT_D, GNT_COLOR_BLACK, GNT_COLOR_GRAY);
107 init_pair(GNT_COLOR_DISABLED, GNT_COLOR_GRAY, GNT_COLOR_WHITE);
108 init_pair(GNT_COLOR_URGENT, GNT_COLOR_WHITE, GNT_COLOR_RED);
110 else
112 int bg;
114 if (defaults == OK) {
115 init_pair(GNT_COLOR_NORMAL, -1, -1);
116 bg = -1;
117 } else {
118 init_pair(GNT_COLOR_NORMAL, COLOR_BLACK, COLOR_WHITE);
119 bg = COLOR_WHITE;
121 init_pair(GNT_COLOR_DISABLED, COLOR_YELLOW, bg);
122 init_pair(GNT_COLOR_URGENT, COLOR_GREEN, bg);
124 init_pair(GNT_COLOR_HIGHLIGHT, COLOR_WHITE, COLOR_BLUE);
125 init_pair(GNT_COLOR_SHADOW, COLOR_BLACK, COLOR_BLACK);
126 init_pair(GNT_COLOR_TITLE, COLOR_WHITE, COLOR_BLUE);
127 init_pair(GNT_COLOR_TITLE_D, COLOR_WHITE, COLOR_BLACK);
128 init_pair(GNT_COLOR_TEXT_NORMAL, COLOR_WHITE, COLOR_BLUE);
129 init_pair(GNT_COLOR_HIGHLIGHT_D, COLOR_CYAN, COLOR_BLACK);
133 void
134 gnt_uninit_colors()
136 if (can_use_custom_color())
137 restore_colors();
140 #if GLIB_CHECK_VERSION(2,6,0)
142 gnt_colors_get_color(char *key)
144 int color;
145 gboolean custom = can_use_custom_color();
147 key = g_strstrip(key);
149 if (strcmp(key, "black") == 0)
150 color = custom ? GNT_COLOR_BLACK : COLOR_BLACK;
151 else if (strcmp(key, "red") == 0)
152 color = custom ? GNT_COLOR_RED : COLOR_RED;
153 else if (strcmp(key, "green") == 0)
154 color = custom ? GNT_COLOR_GREEN : COLOR_GREEN;
155 else if (strcmp(key, "blue") == 0)
156 color = custom ? GNT_COLOR_BLUE : COLOR_BLUE;
157 else if (strcmp(key, "white") == 0)
158 color = custom ? GNT_COLOR_WHITE : COLOR_WHITE;
159 else if (strcmp(key, "gray") == 0)
160 color = custom ? GNT_COLOR_GRAY : COLOR_YELLOW; /* eh? */
161 else if (strcmp(key, "darkgray") == 0)
162 color = custom ? GNT_COLOR_DARK_GRAY : COLOR_BLACK;
163 else if (strcmp(key, "magenta") == 0)
164 color = COLOR_MAGENTA;
165 else if (strcmp(key, "cyan") == 0)
166 color = COLOR_CYAN;
167 else if (strcmp(key, "default") == 0)
168 color = -1;
169 else {
170 g_warning("Invalid color name: %s\n", key);
171 color = -1;
173 return color;
176 void gnt_colors_parse(GKeyFile *kfile)
178 GError *error = NULL;
179 gsize nkeys;
180 char **keys = g_key_file_get_keys(kfile, "colors", &nkeys, &error);
182 if (error)
184 g_printerr("GntColors: %s\n", error->message);
185 g_error_free(error);
186 error = NULL;
188 else if (nkeys)
190 gnt_init_colors();
191 while (nkeys--)
193 gsize len;
194 gchar *key = keys[nkeys];
195 char **list = g_key_file_get_string_list(kfile, "colors", key, &len, NULL);
196 if (len == 3)
198 int r = atoi(list[0]);
199 int g = atoi(list[1]);
200 int b = atoi(list[2]);
201 int color = -1;
203 key = g_ascii_strdown(key, -1);
204 color = gnt_colors_get_color(key);
205 g_free(key);
206 if (color == -1)
207 continue;
209 init_color(color, r, g, b);
211 g_strfreev(list);
214 g_strfreev(keys);
217 gnt_color_pairs_parse(kfile);
220 void gnt_color_pairs_parse(GKeyFile *kfile)
222 GError *error = NULL;
223 gsize nkeys;
224 char **keys = g_key_file_get_keys(kfile, "colorpairs", &nkeys, &error);
226 if (error)
228 g_printerr("GntColors: %s\n", error->message);
229 g_error_free(error);
230 return;
232 else if (nkeys)
233 gnt_init_colors();
235 while (nkeys--)
237 gsize len;
238 gchar *key = keys[nkeys];
239 char **list = g_key_file_get_string_list(kfile, "colorpairs", key, &len, NULL);
240 if (len == 2)
242 GntColorType type = 0;
243 gchar *fgc = g_ascii_strdown(list[0], -1);
244 gchar *bgc = g_ascii_strdown(list[1], -1);
245 int fg = gnt_colors_get_color(fgc);
246 int bg = gnt_colors_get_color(bgc);
247 g_free(fgc);
248 g_free(bgc);
249 if (fg == -1 || bg == -1)
250 continue;
252 key = g_ascii_strdown(key, -1);
254 if (strcmp(key, "normal") == 0)
255 type = GNT_COLOR_NORMAL;
256 else if (strcmp(key, "highlight") == 0)
257 type = GNT_COLOR_HIGHLIGHT;
258 else if (strcmp(key, "highlightd") == 0)
259 type = GNT_COLOR_HIGHLIGHT_D;
260 else if (strcmp(key, "shadow") == 0)
261 type = GNT_COLOR_SHADOW;
262 else if (strcmp(key, "title") == 0)
263 type = GNT_COLOR_TITLE;
264 else if (strcmp(key, "titled") == 0)
265 type = GNT_COLOR_TITLE_D;
266 else if (strcmp(key, "text") == 0)
267 type = GNT_COLOR_TEXT_NORMAL;
268 else if (strcmp(key, "disabled") == 0)
269 type = GNT_COLOR_DISABLED;
270 else if (strcmp(key, "urgent") == 0)
271 type = GNT_COLOR_URGENT;
272 else {
273 g_free(key);
274 continue;
276 g_free(key);
278 init_pair(type, fg, bg);
280 g_strfreev(list);
283 g_strfreev(keys);
286 #endif /* GKeyFile */
288 int gnt_color_pair(int pair)
290 return (hascolors ? COLOR_PAIR(pair) :
291 ((pair == GNT_COLOR_NORMAL || pair == GNT_COLOR_HIGHLIGHT_D ||
292 pair == GNT_COLOR_TITLE_D || pair == GNT_COLOR_DISABLED) ? 0 : A_STANDOUT));
295 int gnt_color_add_pair(int fg, int bg)
297 init_pair(custom_type, fg, bg);
298 return custom_type++;