Mercurial > pidgin
annotate finch/libgnt/gntcolors.c @ 26559:1c9bbfcc3687
merge of '009cd1791a028ae463fba5c8a2ee1c60021efb61'
and '9420ff7a2d15050fcf1f87db358cca0e4e1d91bb'
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sun, 12 Apr 2009 21:11:00 +0000 |
parents | 075aa09253e7 |
children | f22eadbdc5d1 |
rev | line source |
---|---|
17928
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
1 /** |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
3 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
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:
15817
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:
15817
diff
changeset
|
6 * source distribution. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
7 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
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:
15817
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:
15817
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:
15817
diff
changeset
|
11 * (at your option) any later version. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
12 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
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:
15817
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
16 * GNU General Public License for more details. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
17 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
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:
15817
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
19681
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 |
17928
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
21 */ |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
15817
diff
changeset
|
22 |
15817 | 23 #include "config.h" |
24 | |
25 #include <ncurses.h> | |
26 | |
24558
e2e57d3c0578
Use glib log functions to show error messages.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23309
diff
changeset
|
27 #include "gntinternal.h" |
e2e57d3c0578
Use glib log functions to show error messages.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23309
diff
changeset
|
28 #undef GNT_LOG_DOMAIN |
e2e57d3c0578
Use glib log functions to show error messages.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23309
diff
changeset
|
29 #define GNT_LOG_DOMAIN "Colors" |
e2e57d3c0578
Use glib log functions to show error messages.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23309
diff
changeset
|
30 |
15817 | 31 #include "gntcolors.h" |
32 #include "gntstyle.h" | |
33 | |
34 #include <glib.h> | |
35 | |
23309
ea341703b1d3
msgcolor command to change colors of messages in conversations.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
22007
diff
changeset
|
36 #include <errno.h> |
15817 | 37 #include <stdlib.h> |
38 #include <string.h> | |
39 | |
21250
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
40 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
|
41 static int custom_type = GNT_COLORS; |
15817 | 42 static struct |
43 { | |
44 short r, g, b; | |
45 } colors[GNT_TOTAL_COLORS]; | |
46 | |
47 static void | |
22007
c38d72677c8a
Probe for -Wstrict-prototypes to get some more warnings. I then cleaned up
Richard Laager <rlaager@wiktel.com>
parents:
21694
diff
changeset
|
48 backup_colors(void) |
15817 | 49 { |
50 short i; | |
51 for (i = 0; i < GNT_TOTAL_COLORS; i++) | |
52 { | |
53 color_content(i, &colors[i].r, | |
54 &colors[i].g, &colors[i].b); | |
55 } | |
56 } | |
57 | |
58 static gboolean | |
22007
c38d72677c8a
Probe for -Wstrict-prototypes to get some more warnings. I then cleaned up
Richard Laager <rlaager@wiktel.com>
parents:
21694
diff
changeset
|
59 can_use_custom_color(void) |
15817 | 60 { |
61 return (gnt_style_get_bool(GNT_STYLE_COLOR, FALSE) && can_change_color()); | |
62 } | |
63 | |
64 static void | |
22007
c38d72677c8a
Probe for -Wstrict-prototypes to get some more warnings. I then cleaned up
Richard Laager <rlaager@wiktel.com>
parents:
21694
diff
changeset
|
65 restore_colors(void) |
15817 | 66 { |
67 short i; | |
68 for (i = 0; i < GNT_TOTAL_COLORS; i++) | |
69 { | |
70 init_color(i, colors[i].r, | |
71 colors[i].g, colors[i].b); | |
72 } | |
73 } | |
74 | |
75 void gnt_init_colors() | |
76 { | |
77 static gboolean init = FALSE; | |
78 int defaults; | |
79 | |
80 if (init) | |
81 return; | |
82 init = TRUE; | |
83 | |
84 start_color(); | |
21250
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
85 if (!(hascolors = has_colors())) |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
86 return; |
15817 | 87 defaults = use_default_colors(); |
88 | |
89 if (can_use_custom_color()) | |
90 { | |
91 backup_colors(); | |
92 | |
93 /* Do some init_color()s */ | |
94 init_color(GNT_COLOR_BLACK, 0, 0, 0); | |
95 init_color(GNT_COLOR_RED, 1000, 0, 0); | |
96 init_color(GNT_COLOR_GREEN, 0, 1000, 0); | |
97 init_color(GNT_COLOR_BLUE, 250, 250, 700); | |
98 init_color(GNT_COLOR_WHITE, 1000, 1000, 1000); | |
99 init_color(GNT_COLOR_GRAY, 699, 699, 699); | |
100 init_color(GNT_COLOR_DARK_GRAY, 256, 256, 256); | |
101 | |
102 /* Now some init_pair()s */ | |
103 init_pair(GNT_COLOR_NORMAL, GNT_COLOR_BLACK, GNT_COLOR_WHITE); | |
104 init_pair(GNT_COLOR_HIGHLIGHT, GNT_COLOR_WHITE, GNT_COLOR_BLUE); | |
105 init_pair(GNT_COLOR_SHADOW, GNT_COLOR_BLACK, GNT_COLOR_DARK_GRAY); | |
106 | |
107 init_pair(GNT_COLOR_TITLE, GNT_COLOR_WHITE, GNT_COLOR_BLUE); | |
108 init_pair(GNT_COLOR_TITLE_D, GNT_COLOR_WHITE, GNT_COLOR_GRAY); | |
109 | |
110 init_pair(GNT_COLOR_TEXT_NORMAL, GNT_COLOR_WHITE, GNT_COLOR_BLUE); | |
111 init_pair(GNT_COLOR_HIGHLIGHT_D, GNT_COLOR_BLACK, GNT_COLOR_GRAY); | |
112 init_pair(GNT_COLOR_DISABLED, GNT_COLOR_GRAY, GNT_COLOR_WHITE); | |
113 init_pair(GNT_COLOR_URGENT, GNT_COLOR_WHITE, GNT_COLOR_RED); | |
114 } | |
115 else | |
116 { | |
117 int bg; | |
118 | |
119 if (defaults == OK) { | |
120 init_pair(GNT_COLOR_NORMAL, -1, -1); | |
121 bg = -1; | |
122 } else { | |
123 init_pair(GNT_COLOR_NORMAL, COLOR_BLACK, COLOR_WHITE); | |
124 bg = COLOR_WHITE; | |
125 } | |
126 init_pair(GNT_COLOR_DISABLED, COLOR_YELLOW, bg); | |
127 init_pair(GNT_COLOR_URGENT, COLOR_GREEN, bg); | |
128 | |
129 init_pair(GNT_COLOR_HIGHLIGHT, COLOR_WHITE, COLOR_BLUE); | |
130 init_pair(GNT_COLOR_SHADOW, COLOR_BLACK, COLOR_BLACK); | |
131 init_pair(GNT_COLOR_TITLE, COLOR_WHITE, COLOR_BLUE); | |
132 init_pair(GNT_COLOR_TITLE_D, COLOR_WHITE, COLOR_BLACK); | |
133 init_pair(GNT_COLOR_TEXT_NORMAL, COLOR_WHITE, COLOR_BLUE); | |
134 init_pair(GNT_COLOR_HIGHLIGHT_D, COLOR_CYAN, COLOR_BLACK); | |
135 } | |
136 } | |
137 | |
138 void | |
139 gnt_uninit_colors() | |
140 { | |
141 if (can_use_custom_color()) | |
142 restore_colors(); | |
143 } | |
144 | |
19381
8d9913d9fe5a
Get rid of compile warnings on older glib.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
17928
diff
changeset
|
145 #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
|
146 int |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
147 gnt_colors_get_color(char *key) |
15817 | 148 { |
149 int color; | |
150 gboolean custom = can_use_custom_color(); | |
151 | |
152 key = g_strstrip(key); | |
153 | |
154 if (strcmp(key, "black") == 0) | |
155 color = custom ? GNT_COLOR_BLACK : COLOR_BLACK; | |
156 else if (strcmp(key, "red") == 0) | |
157 color = custom ? GNT_COLOR_RED : COLOR_RED; | |
158 else if (strcmp(key, "green") == 0) | |
159 color = custom ? GNT_COLOR_GREEN : COLOR_GREEN; | |
160 else if (strcmp(key, "blue") == 0) | |
161 color = custom ? GNT_COLOR_BLUE : COLOR_BLUE; | |
162 else if (strcmp(key, "white") == 0) | |
163 color = custom ? GNT_COLOR_WHITE : COLOR_WHITE; | |
164 else if (strcmp(key, "gray") == 0) | |
165 color = custom ? GNT_COLOR_GRAY : COLOR_YELLOW; /* eh? */ | |
166 else if (strcmp(key, "darkgray") == 0) | |
167 color = custom ? GNT_COLOR_DARK_GRAY : COLOR_BLACK; | |
168 else if (strcmp(key, "magenta") == 0) | |
169 color = COLOR_MAGENTA; | |
170 else if (strcmp(key, "cyan") == 0) | |
171 color = COLOR_CYAN; | |
21694
2de3a2de5f13
Update the changelogs. Correct the man page. Allow 'default' to mean the
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
21691
diff
changeset
|
172 else if (strcmp(key, "default") == 0) |
15817 | 173 color = -1; |
21694
2de3a2de5f13
Update the changelogs. Correct the man page. Allow 'default' to mean the
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
21691
diff
changeset
|
174 else { |
2de3a2de5f13
Update the changelogs. Correct the man page. Allow 'default' to mean the
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
21691
diff
changeset
|
175 g_warning("Invalid color name: %s\n", key); |
23309
ea341703b1d3
msgcolor command to change colors of messages in conversations.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
22007
diff
changeset
|
176 color = -EINVAL; |
21694
2de3a2de5f13
Update the changelogs. Correct the man page. Allow 'default' to mean the
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
21691
diff
changeset
|
177 } |
15817 | 178 return color; |
179 } | |
180 | |
181 void gnt_colors_parse(GKeyFile *kfile) | |
182 { | |
183 GError *error = NULL; | |
184 gsize nkeys; | |
185 char **keys = g_key_file_get_keys(kfile, "colors", &nkeys, &error); | |
186 | |
187 if (error) | |
188 { | |
24558
e2e57d3c0578
Use glib log functions to show error messages.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23309
diff
changeset
|
189 gnt_warning("%s", error->message); |
15817 | 190 g_error_free(error); |
191 error = NULL; | |
192 } | |
193 else if (nkeys) | |
194 { | |
195 gnt_init_colors(); | |
196 while (nkeys--) | |
197 { | |
198 gsize len; | |
199 gchar *key = keys[nkeys]; | |
200 char **list = g_key_file_get_string_list(kfile, "colors", key, &len, NULL); | |
201 if (len == 3) | |
202 { | |
203 int r = atoi(list[0]); | |
204 int g = atoi(list[1]); | |
205 int b = atoi(list[2]); | |
206 int color = -1; | |
207 | |
208 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
|
209 color = gnt_colors_get_color(key); |
15817 | 210 g_free(key); |
25385
075aa09253e7
Fix setting custom colorpairs containing 'default', thanks to jieryn on
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
24558
diff
changeset
|
211 if (color == -EINVAL) |
15817 | 212 continue; |
213 | |
214 init_color(color, r, g, b); | |
215 } | |
216 g_strfreev(list); | |
217 } | |
218 | |
219 g_strfreev(keys); | |
220 } | |
221 | |
222 gnt_color_pairs_parse(kfile); | |
223 } | |
224 | |
225 void gnt_color_pairs_parse(GKeyFile *kfile) | |
226 { | |
227 GError *error = NULL; | |
228 gsize nkeys; | |
229 char **keys = g_key_file_get_keys(kfile, "colorpairs", &nkeys, &error); | |
230 | |
231 if (error) | |
232 { | |
24558
e2e57d3c0578
Use glib log functions to show error messages.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23309
diff
changeset
|
233 gnt_warning("%s", error->message); |
15817 | 234 g_error_free(error); |
235 return; | |
236 } | |
237 else if (nkeys) | |
238 gnt_init_colors(); | |
239 | |
240 while (nkeys--) | |
241 { | |
242 gsize len; | |
243 gchar *key = keys[nkeys]; | |
244 char **list = g_key_file_get_string_list(kfile, "colorpairs", key, &len, NULL); | |
245 if (len == 2) | |
246 { | |
247 GntColorType type = 0; | |
248 gchar *fgc = g_ascii_strdown(list[0], -1); | |
249 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
|
250 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
|
251 int bg = gnt_colors_get_color(bgc); |
15817 | 252 g_free(fgc); |
253 g_free(bgc); | |
25385
075aa09253e7
Fix setting custom colorpairs containing 'default', thanks to jieryn on
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
24558
diff
changeset
|
254 if (fg == -EINVAL || bg == -EINVAL) |
15817 | 255 continue; |
256 | |
257 key = g_ascii_strdown(key, -1); | |
258 | |
259 if (strcmp(key, "normal") == 0) | |
260 type = GNT_COLOR_NORMAL; | |
261 else if (strcmp(key, "highlight") == 0) | |
262 type = GNT_COLOR_HIGHLIGHT; | |
263 else if (strcmp(key, "highlightd") == 0) | |
264 type = GNT_COLOR_HIGHLIGHT_D; | |
265 else if (strcmp(key, "shadow") == 0) | |
266 type = GNT_COLOR_SHADOW; | |
267 else if (strcmp(key, "title") == 0) | |
268 type = GNT_COLOR_TITLE; | |
269 else if (strcmp(key, "titled") == 0) | |
270 type = GNT_COLOR_TITLE_D; | |
271 else if (strcmp(key, "text") == 0) | |
272 type = GNT_COLOR_TEXT_NORMAL; | |
273 else if (strcmp(key, "disabled") == 0) | |
274 type = GNT_COLOR_DISABLED; | |
275 else if (strcmp(key, "urgent") == 0) | |
276 type = GNT_COLOR_URGENT; | |
277 else { | |
278 g_free(key); | |
279 continue; | |
280 } | |
281 g_free(key); | |
282 | |
283 init_pair(type, fg, bg); | |
284 } | |
285 g_strfreev(list); | |
286 } | |
287 | |
288 g_strfreev(keys); | |
289 } | |
290 | |
291 #endif /* GKeyFile */ | |
21250
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
292 |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
293 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:
19681
diff
changeset
|
294 { |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
295 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:
19681
diff
changeset
|
296 ((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:
19681
diff
changeset
|
297 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:
19681
diff
changeset
|
298 } |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
299 |
21691
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
300 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
|
301 { |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
302 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
|
303 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
|
304 } |