Mercurial > pidgin.yaz
annotate finch/libgnt/gntcolors.c @ 21691:3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
author | Richard Nelson <wabz@pidgin.im> |
---|---|
date | Fri, 30 Nov 2007 03:17:07 +0000 |
parents | 9187d331aebe |
children | 2de3a2de5f13 |
rev | line source |
---|---|
17718
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
1 /** |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
3 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
6 * source distribution. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
7 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
11 * (at your option) any later version. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
12 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
16 * GNU General Public License for more details. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
17 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
19680
44b4e8bd759b
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
19381
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
17718
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
21 */ |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15818
diff
changeset
|
22 |
15818 | 23 #include "config.h" |
24 | |
25 #include <ncurses.h> | |
26 | |
27 #include "gntcolors.h" | |
28 #include "gntstyle.h" | |
29 | |
30 #include <glib.h> | |
31 | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 | |
21250
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
35 static gboolean hascolors; |
21691
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
36 static int custom_type = GNT_COLORS; |
15818 | 37 static struct |
38 { | |
39 short r, g, b; | |
40 } colors[GNT_TOTAL_COLORS]; | |
41 | |
42 static void | |
43 backup_colors() | |
44 { | |
45 short i; | |
46 for (i = 0; i < GNT_TOTAL_COLORS; i++) | |
47 { | |
48 color_content(i, &colors[i].r, | |
49 &colors[i].g, &colors[i].b); | |
50 } | |
51 } | |
52 | |
53 static gboolean | |
54 can_use_custom_color() | |
55 { | |
56 return (gnt_style_get_bool(GNT_STYLE_COLOR, FALSE) && can_change_color()); | |
57 } | |
58 | |
59 static void | |
60 restore_colors() | |
61 { | |
62 short i; | |
63 for (i = 0; i < GNT_TOTAL_COLORS; i++) | |
64 { | |
65 init_color(i, colors[i].r, | |
66 colors[i].g, colors[i].b); | |
67 } | |
68 } | |
69 | |
70 void gnt_init_colors() | |
71 { | |
72 static gboolean init = FALSE; | |
73 int defaults; | |
74 | |
75 if (init) | |
76 return; | |
77 init = TRUE; | |
78 | |
79 start_color(); | |
21250
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
80 if (!(hascolors = has_colors())) |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
81 return; |
15818 | 82 defaults = use_default_colors(); |
83 | |
84 if (can_use_custom_color()) | |
85 { | |
86 backup_colors(); | |
87 | |
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); | |
96 | |
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); | |
101 | |
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); | |
104 | |
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); | |
109 } | |
110 else | |
111 { | |
112 int bg; | |
113 | |
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; | |
120 } | |
121 init_pair(GNT_COLOR_DISABLED, COLOR_YELLOW, bg); | |
122 init_pair(GNT_COLOR_URGENT, COLOR_GREEN, bg); | |
123 | |
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); | |
130 } | |
131 } | |
132 | |
133 void | |
134 gnt_uninit_colors() | |
135 { | |
136 if (can_use_custom_color()) | |
137 restore_colors(); | |
138 } | |
139 | |
19381
8d9913d9fe5a
Get rid of compile warnings on older glib.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
17718
diff
changeset
|
140 #if GLIB_CHECK_VERSION(2,6,0) |
21691
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
141 int |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
142 gnt_colors_get_color(char *key) |
15818 | 143 { |
144 int color; | |
145 gboolean custom = can_use_custom_color(); | |
146 | |
147 key = g_strstrip(key); | |
148 | |
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 | |
168 color = -1; | |
169 return color; | |
170 } | |
171 | |
172 void gnt_colors_parse(GKeyFile *kfile) | |
173 { | |
174 GError *error = NULL; | |
175 gsize nkeys; | |
176 char **keys = g_key_file_get_keys(kfile, "colors", &nkeys, &error); | |
177 | |
178 if (error) | |
179 { | |
180 g_printerr("GntColors: %s\n", error->message); | |
181 g_error_free(error); | |
182 error = NULL; | |
183 } | |
184 else if (nkeys) | |
185 { | |
186 gnt_init_colors(); | |
187 while (nkeys--) | |
188 { | |
189 gsize len; | |
190 gchar *key = keys[nkeys]; | |
191 char **list = g_key_file_get_string_list(kfile, "colors", key, &len, NULL); | |
192 if (len == 3) | |
193 { | |
194 int r = atoi(list[0]); | |
195 int g = atoi(list[1]); | |
196 int b = atoi(list[2]); | |
197 int color = -1; | |
198 | |
199 key = g_ascii_strdown(key, -1); | |
21691
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
200 color = gnt_colors_get_color(key); |
15818 | 201 g_free(key); |
202 if (color == -1) | |
203 continue; | |
204 | |
205 init_color(color, r, g, b); | |
206 } | |
207 g_strfreev(list); | |
208 } | |
209 | |
210 g_strfreev(keys); | |
211 } | |
212 | |
213 gnt_color_pairs_parse(kfile); | |
214 } | |
215 | |
216 void gnt_color_pairs_parse(GKeyFile *kfile) | |
217 { | |
218 GError *error = NULL; | |
219 gsize nkeys; | |
220 char **keys = g_key_file_get_keys(kfile, "colorpairs", &nkeys, &error); | |
221 | |
222 if (error) | |
223 { | |
224 g_printerr("GntColors: %s\n", error->message); | |
225 g_error_free(error); | |
226 return; | |
227 } | |
228 else if (nkeys) | |
229 gnt_init_colors(); | |
230 | |
231 while (nkeys--) | |
232 { | |
233 gsize len; | |
234 gchar *key = keys[nkeys]; | |
235 char **list = g_key_file_get_string_list(kfile, "colorpairs", key, &len, NULL); | |
236 if (len == 2) | |
237 { | |
238 GntColorType type = 0; | |
239 gchar *fgc = g_ascii_strdown(list[0], -1); | |
240 gchar *bgc = g_ascii_strdown(list[1], -1); | |
21691
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
241 int fg = gnt_colors_get_color(fgc); |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
242 int bg = gnt_colors_get_color(bgc); |
15818 | 243 g_free(fgc); |
244 g_free(bgc); | |
245 if (fg == -1 || bg == -1) | |
246 continue; | |
247 | |
248 key = g_ascii_strdown(key, -1); | |
249 | |
250 if (strcmp(key, "normal") == 0) | |
251 type = GNT_COLOR_NORMAL; | |
252 else if (strcmp(key, "highlight") == 0) | |
253 type = GNT_COLOR_HIGHLIGHT; | |
254 else if (strcmp(key, "highlightd") == 0) | |
255 type = GNT_COLOR_HIGHLIGHT_D; | |
256 else if (strcmp(key, "shadow") == 0) | |
257 type = GNT_COLOR_SHADOW; | |
258 else if (strcmp(key, "title") == 0) | |
259 type = GNT_COLOR_TITLE; | |
260 else if (strcmp(key, "titled") == 0) | |
261 type = GNT_COLOR_TITLE_D; | |
262 else if (strcmp(key, "text") == 0) | |
263 type = GNT_COLOR_TEXT_NORMAL; | |
264 else if (strcmp(key, "disabled") == 0) | |
265 type = GNT_COLOR_DISABLED; | |
266 else if (strcmp(key, "urgent") == 0) | |
267 type = GNT_COLOR_URGENT; | |
268 else { | |
269 g_free(key); | |
270 continue; | |
271 } | |
272 g_free(key); | |
273 | |
274 init_pair(type, fg, bg); | |
275 } | |
276 g_strfreev(list); | |
277 } | |
278 | |
279 g_strfreev(keys); | |
280 } | |
281 | |
282 #endif /* GKeyFile */ | |
21250
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
283 |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
284 int gnt_color_pair(int pair) |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
285 { |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
286 return (hascolors ? COLOR_PAIR(pair) : |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
287 ((pair == GNT_COLOR_NORMAL || pair == GNT_COLOR_HIGHLIGHT_D || |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
288 pair == GNT_COLOR_TITLE_D || pair == GNT_COLOR_DISABLED) ? 0 : A_STANDOUT)); |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
289 } |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
290 |
21691
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
291 int gnt_color_add_pair(int fg, int bg) |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
292 { |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
293 init_pair(custom_type, fg, bg); |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
294 return custom_type++; |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
295 } |