| |
1 #include "config.h" |
| |
2 |
| |
3 #include <ncurses.h> |
| |
4 |
| |
5 #include "gntcolors.h" |
| |
6 #include "gntstyle.h" |
| |
7 |
| |
8 #include <glib.h> |
| |
9 |
| |
10 #include <stdlib.h> |
| |
11 #include <string.h> |
| |
12 |
| |
13 static struct |
| |
14 { |
| |
15 short r, g, b; |
| |
16 } colors[GNT_TOTAL_COLORS]; |
| |
17 |
| |
18 static void |
| |
19 backup_colors() |
| |
20 { |
| |
21 short i; |
| |
22 for (i = 0; i < GNT_TOTAL_COLORS; i++) |
| |
23 { |
| |
24 color_content(i, &colors[i].r, |
| |
25 &colors[i].g, &colors[i].b); |
| |
26 } |
| |
27 } |
| |
28 |
| |
29 static gboolean |
| |
30 can_use_custom_color() |
| |
31 { |
| |
32 return (gnt_style_get_bool(GNT_STYLE_COLOR, FALSE) && can_change_color()); |
| |
33 } |
| |
34 |
| |
35 static void |
| |
36 restore_colors() |
| |
37 { |
| |
38 short i; |
| |
39 for (i = 0; i < GNT_TOTAL_COLORS; i++) |
| |
40 { |
| |
41 init_color(i, colors[i].r, |
| |
42 colors[i].g, colors[i].b); |
| |
43 } |
| |
44 } |
| |
45 |
| |
46 void gnt_init_colors() |
| |
47 { |
| |
48 static gboolean init = FALSE; |
| |
49 int defaults; |
| |
50 |
| |
51 if (init) |
| |
52 return; |
| |
53 init = TRUE; |
| |
54 |
| |
55 start_color(); |
| |
56 defaults = use_default_colors(); |
| |
57 |
| |
58 if (can_use_custom_color()) |
| |
59 { |
| |
60 backup_colors(); |
| |
61 |
| |
62 /* Do some init_color()s */ |
| |
63 init_color(GNT_COLOR_BLACK, 0, 0, 0); |
| |
64 init_color(GNT_COLOR_RED, 1000, 0, 0); |
| |
65 init_color(GNT_COLOR_GREEN, 0, 1000, 0); |
| |
66 init_color(GNT_COLOR_BLUE, 250, 250, 700); |
| |
67 init_color(GNT_COLOR_WHITE, 1000, 1000, 1000); |
| |
68 init_color(GNT_COLOR_GRAY, 699, 699, 699); |
| |
69 init_color(GNT_COLOR_DARK_GRAY, 256, 256, 256); |
| |
70 |
| |
71 /* Now some init_pair()s */ |
| |
72 init_pair(GNT_COLOR_NORMAL, GNT_COLOR_BLACK, GNT_COLOR_WHITE); |
| |
73 init_pair(GNT_COLOR_HIGHLIGHT, GNT_COLOR_WHITE, GNT_COLOR_BLUE); |
| |
74 init_pair(GNT_COLOR_SHADOW, GNT_COLOR_BLACK, GNT_COLOR_DARK_GRAY); |
| |
75 |
| |
76 init_pair(GNT_COLOR_TITLE, GNT_COLOR_WHITE, GNT_COLOR_BLUE); |
| |
77 init_pair(GNT_COLOR_TITLE_D, GNT_COLOR_WHITE, GNT_COLOR_GRAY); |
| |
78 |
| |
79 init_pair(GNT_COLOR_TEXT_NORMAL, GNT_COLOR_WHITE, GNT_COLOR_BLUE); |
| |
80 init_pair(GNT_COLOR_HIGHLIGHT_D, GNT_COLOR_BLACK, GNT_COLOR_GRAY); |
| |
81 init_pair(GNT_COLOR_DISABLED, GNT_COLOR_GRAY, GNT_COLOR_WHITE); |
| |
82 init_pair(GNT_COLOR_URGENT, GNT_COLOR_WHITE, GNT_COLOR_RED); |
| |
83 } |
| |
84 else |
| |
85 { |
| |
86 int bg; |
| |
87 |
| |
88 if (defaults == OK) { |
| |
89 init_pair(GNT_COLOR_NORMAL, -1, -1); |
| |
90 bg = -1; |
| |
91 } else { |
| |
92 init_pair(GNT_COLOR_NORMAL, COLOR_BLACK, COLOR_WHITE); |
| |
93 bg = COLOR_WHITE; |
| |
94 } |
| |
95 init_pair(GNT_COLOR_DISABLED, COLOR_YELLOW, bg); |
| |
96 init_pair(GNT_COLOR_URGENT, COLOR_GREEN, bg); |
| |
97 |
| |
98 init_pair(GNT_COLOR_HIGHLIGHT, COLOR_WHITE, COLOR_BLUE); |
| |
99 init_pair(GNT_COLOR_SHADOW, COLOR_BLACK, COLOR_BLACK); |
| |
100 init_pair(GNT_COLOR_TITLE, COLOR_WHITE, COLOR_BLUE); |
| |
101 init_pair(GNT_COLOR_TITLE_D, COLOR_WHITE, COLOR_BLACK); |
| |
102 init_pair(GNT_COLOR_TEXT_NORMAL, COLOR_WHITE, COLOR_BLUE); |
| |
103 init_pair(GNT_COLOR_HIGHLIGHT_D, COLOR_CYAN, COLOR_BLACK); |
| |
104 } |
| |
105 } |
| |
106 |
| |
107 void |
| |
108 gnt_uninit_colors() |
| |
109 { |
| |
110 if (can_use_custom_color()) |
| |
111 restore_colors(); |
| |
112 } |
| |
113 |
| |
114 static int |
| |
115 get_color(char *key) |
| |
116 { |
| |
117 int color; |
| |
118 gboolean custom = can_use_custom_color(); |
| |
119 |
| |
120 key = g_strstrip(key); |
| |
121 |
| |
122 if (strcmp(key, "black") == 0) |
| |
123 color = custom ? GNT_COLOR_BLACK : COLOR_BLACK; |
| |
124 else if (strcmp(key, "red") == 0) |
| |
125 color = custom ? GNT_COLOR_RED : COLOR_RED; |
| |
126 else if (strcmp(key, "green") == 0) |
| |
127 color = custom ? GNT_COLOR_GREEN : COLOR_GREEN; |
| |
128 else if (strcmp(key, "blue") == 0) |
| |
129 color = custom ? GNT_COLOR_BLUE : COLOR_BLUE; |
| |
130 else if (strcmp(key, "white") == 0) |
| |
131 color = custom ? GNT_COLOR_WHITE : COLOR_WHITE; |
| |
132 else if (strcmp(key, "gray") == 0) |
| |
133 color = custom ? GNT_COLOR_GRAY : COLOR_YELLOW; /* eh? */ |
| |
134 else if (strcmp(key, "darkgray") == 0) |
| |
135 color = custom ? GNT_COLOR_DARK_GRAY : COLOR_BLACK; |
| |
136 else if (strcmp(key, "magenta") == 0) |
| |
137 color = COLOR_MAGENTA; |
| |
138 else if (strcmp(key, "cyan") == 0) |
| |
139 color = COLOR_CYAN; |
| |
140 else |
| |
141 color = -1; |
| |
142 return color; |
| |
143 } |
| |
144 |
| |
145 #if GLIB_CHECK_VERSION(2,6,0) |
| |
146 void gnt_colors_parse(GKeyFile *kfile) |
| |
147 { |
| |
148 GError *error = NULL; |
| |
149 gsize nkeys; |
| |
150 char **keys = g_key_file_get_keys(kfile, "colors", &nkeys, &error); |
| |
151 |
| |
152 if (error) |
| |
153 { |
| |
154 g_printerr("GntColors: %s\n", error->message); |
| |
155 g_error_free(error); |
| |
156 error = NULL; |
| |
157 } |
| |
158 else if (nkeys) |
| |
159 { |
| |
160 gnt_init_colors(); |
| |
161 while (nkeys--) |
| |
162 { |
| |
163 gsize len; |
| |
164 gchar *key = keys[nkeys]; |
| |
165 char **list = g_key_file_get_string_list(kfile, "colors", key, &len, NULL); |
| |
166 if (len == 3) |
| |
167 { |
| |
168 int r = atoi(list[0]); |
| |
169 int g = atoi(list[1]); |
| |
170 int b = atoi(list[2]); |
| |
171 int color = -1; |
| |
172 |
| |
173 key = g_ascii_strdown(key, -1); |
| |
174 color = get_color(key); |
| |
175 g_free(key); |
| |
176 if (color == -1) |
| |
177 continue; |
| |
178 |
| |
179 init_color(color, r, g, b); |
| |
180 } |
| |
181 g_strfreev(list); |
| |
182 } |
| |
183 |
| |
184 g_strfreev(keys); |
| |
185 } |
| |
186 |
| |
187 gnt_color_pairs_parse(kfile); |
| |
188 } |
| |
189 |
| |
190 void gnt_color_pairs_parse(GKeyFile *kfile) |
| |
191 { |
| |
192 GError *error = NULL; |
| |
193 gsize nkeys; |
| |
194 char **keys = g_key_file_get_keys(kfile, "colorpairs", &nkeys, &error); |
| |
195 |
| |
196 if (error) |
| |
197 { |
| |
198 g_printerr("GntColors: %s\n", error->message); |
| |
199 g_error_free(error); |
| |
200 return; |
| |
201 } |
| |
202 else if (nkeys) |
| |
203 gnt_init_colors(); |
| |
204 |
| |
205 while (nkeys--) |
| |
206 { |
| |
207 gsize len; |
| |
208 gchar *key = keys[nkeys]; |
| |
209 char **list = g_key_file_get_string_list(kfile, "colorpairs", key, &len, NULL); |
| |
210 if (len == 2) |
| |
211 { |
| |
212 GntColorType type = 0; |
| |
213 gchar *fgc = g_ascii_strdown(list[0], -1); |
| |
214 gchar *bgc = g_ascii_strdown(list[1], -1); |
| |
215 int fg = get_color(fgc); |
| |
216 int bg = get_color(bgc); |
| |
217 g_free(fgc); |
| |
218 g_free(bgc); |
| |
219 if (fg == -1 || bg == -1) |
| |
220 continue; |
| |
221 |
| |
222 key = g_ascii_strdown(key, -1); |
| |
223 |
| |
224 if (strcmp(key, "normal") == 0) |
| |
225 type = GNT_COLOR_NORMAL; |
| |
226 else if (strcmp(key, "highlight") == 0) |
| |
227 type = GNT_COLOR_HIGHLIGHT; |
| |
228 else if (strcmp(key, "highlightd") == 0) |
| |
229 type = GNT_COLOR_HIGHLIGHT_D; |
| |
230 else if (strcmp(key, "shadow") == 0) |
| |
231 type = GNT_COLOR_SHADOW; |
| |
232 else if (strcmp(key, "title") == 0) |
| |
233 type = GNT_COLOR_TITLE; |
| |
234 else if (strcmp(key, "titled") == 0) |
| |
235 type = GNT_COLOR_TITLE_D; |
| |
236 else if (strcmp(key, "text") == 0) |
| |
237 type = GNT_COLOR_TEXT_NORMAL; |
| |
238 else if (strcmp(key, "disabled") == 0) |
| |
239 type = GNT_COLOR_DISABLED; |
| |
240 else if (strcmp(key, "urgent") == 0) |
| |
241 type = GNT_COLOR_URGENT; |
| |
242 else { |
| |
243 g_free(key); |
| |
244 continue; |
| |
245 } |
| |
246 g_free(key); |
| |
247 |
| |
248 init_pair(type, fg, bg); |
| |
249 } |
| |
250 g_strfreev(list); |
| |
251 } |
| |
252 |
| |
253 g_strfreev(keys); |
| |
254 } |
| |
255 |
| |
256 #endif /* GKeyFile */ |