Mercurial > pidgin
annotate finch/libgnt/gntcolors.c @ 24963:33fa549f2399
Move variables to PurpleMediaStream.
author | Mike Ruprecht <maiku@soc.pidgin.im> |
---|---|
date | Sun, 16 Nov 2008 05:22:17 +0000 |
parents | ea341703b1d3 |
children | e2e57d3c0578 |
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 | |
27 #include "gntcolors.h" | |
28 #include "gntstyle.h" | |
29 | |
30 #include <glib.h> | |
31 | |
23309
ea341703b1d3
msgcolor command to change colors of messages in conversations.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
22007
diff
changeset
|
32 #include <errno.h> |
15817 | 33 #include <stdlib.h> |
34 #include <string.h> | |
35 | |
21250
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
36 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
|
37 static int custom_type = GNT_COLORS; |
15817 | 38 static struct |
39 { | |
40 short r, g, b; | |
41 } colors[GNT_TOTAL_COLORS]; | |
42 | |
43 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
|
44 backup_colors(void) |
15817 | 45 { |
46 short i; | |
47 for (i = 0; i < GNT_TOTAL_COLORS; i++) | |
48 { | |
49 color_content(i, &colors[i].r, | |
50 &colors[i].g, &colors[i].b); | |
51 } | |
52 } | |
53 | |
54 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
|
55 can_use_custom_color(void) |
15817 | 56 { |
57 return (gnt_style_get_bool(GNT_STYLE_COLOR, FALSE) && can_change_color()); | |
58 } | |
59 | |
60 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
|
61 restore_colors(void) |
15817 | 62 { |
63 short i; | |
64 for (i = 0; i < GNT_TOTAL_COLORS; i++) | |
65 { | |
66 init_color(i, colors[i].r, | |
67 colors[i].g, colors[i].b); | |
68 } | |
69 } | |
70 | |
71 void gnt_init_colors() | |
72 { | |
73 static gboolean init = FALSE; | |
74 int defaults; | |
75 | |
76 if (init) | |
77 return; | |
78 init = TRUE; | |
79 | |
80 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
|
81 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
|
82 return; |
15817 | 83 defaults = use_default_colors(); |
84 | |
85 if (can_use_custom_color()) | |
86 { | |
87 backup_colors(); | |
88 | |
89 /* Do some init_color()s */ | |
90 init_color(GNT_COLOR_BLACK, 0, 0, 0); | |
91 init_color(GNT_COLOR_RED, 1000, 0, 0); | |
92 init_color(GNT_COLOR_GREEN, 0, 1000, 0); | |
93 init_color(GNT_COLOR_BLUE, 250, 250, 700); | |
94 init_color(GNT_COLOR_WHITE, 1000, 1000, 1000); | |
95 init_color(GNT_COLOR_GRAY, 699, 699, 699); | |
96 init_color(GNT_COLOR_DARK_GRAY, 256, 256, 256); | |
97 | |
98 /* Now some init_pair()s */ | |
99 init_pair(GNT_COLOR_NORMAL, GNT_COLOR_BLACK, GNT_COLOR_WHITE); | |
100 init_pair(GNT_COLOR_HIGHLIGHT, GNT_COLOR_WHITE, GNT_COLOR_BLUE); | |
101 init_pair(GNT_COLOR_SHADOW, GNT_COLOR_BLACK, GNT_COLOR_DARK_GRAY); | |
102 | |
103 init_pair(GNT_COLOR_TITLE, GNT_COLOR_WHITE, GNT_COLOR_BLUE); | |
104 init_pair(GNT_COLOR_TITLE_D, GNT_COLOR_WHITE, GNT_COLOR_GRAY); | |
105 | |
106 init_pair(GNT_COLOR_TEXT_NORMAL, GNT_COLOR_WHITE, GNT_COLOR_BLUE); | |
107 init_pair(GNT_COLOR_HIGHLIGHT_D, GNT_COLOR_BLACK, GNT_COLOR_GRAY); | |
108 init_pair(GNT_COLOR_DISABLED, GNT_COLOR_GRAY, GNT_COLOR_WHITE); | |
109 init_pair(GNT_COLOR_URGENT, GNT_COLOR_WHITE, GNT_COLOR_RED); | |
110 } | |
111 else | |
112 { | |
113 int bg; | |
114 | |
115 if (defaults == OK) { | |
116 init_pair(GNT_COLOR_NORMAL, -1, -1); | |
117 bg = -1; | |
118 } else { | |
119 init_pair(GNT_COLOR_NORMAL, COLOR_BLACK, COLOR_WHITE); | |
120 bg = COLOR_WHITE; | |
121 } | |
122 init_pair(GNT_COLOR_DISABLED, COLOR_YELLOW, bg); | |
123 init_pair(GNT_COLOR_URGENT, COLOR_GREEN, bg); | |
124 | |
125 init_pair(GNT_COLOR_HIGHLIGHT, COLOR_WHITE, COLOR_BLUE); | |
126 init_pair(GNT_COLOR_SHADOW, COLOR_BLACK, COLOR_BLACK); | |
127 init_pair(GNT_COLOR_TITLE, COLOR_WHITE, COLOR_BLUE); | |
128 init_pair(GNT_COLOR_TITLE_D, COLOR_WHITE, COLOR_BLACK); | |
129 init_pair(GNT_COLOR_TEXT_NORMAL, COLOR_WHITE, COLOR_BLUE); | |
130 init_pair(GNT_COLOR_HIGHLIGHT_D, COLOR_CYAN, COLOR_BLACK); | |
131 } | |
132 } | |
133 | |
134 void | |
135 gnt_uninit_colors() | |
136 { | |
137 if (can_use_custom_color()) | |
138 restore_colors(); | |
139 } | |
140 | |
19381
8d9913d9fe5a
Get rid of compile warnings on older glib.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
17928
diff
changeset
|
141 #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
|
142 int |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
143 gnt_colors_get_color(char *key) |
15817 | 144 { |
145 int color; | |
146 gboolean custom = can_use_custom_color(); | |
147 | |
148 key = g_strstrip(key); | |
149 | |
150 if (strcmp(key, "black") == 0) | |
151 color = custom ? GNT_COLOR_BLACK : COLOR_BLACK; | |
152 else if (strcmp(key, "red") == 0) | |
153 color = custom ? GNT_COLOR_RED : COLOR_RED; | |
154 else if (strcmp(key, "green") == 0) | |
155 color = custom ? GNT_COLOR_GREEN : COLOR_GREEN; | |
156 else if (strcmp(key, "blue") == 0) | |
157 color = custom ? GNT_COLOR_BLUE : COLOR_BLUE; | |
158 else if (strcmp(key, "white") == 0) | |
159 color = custom ? GNT_COLOR_WHITE : COLOR_WHITE; | |
160 else if (strcmp(key, "gray") == 0) | |
161 color = custom ? GNT_COLOR_GRAY : COLOR_YELLOW; /* eh? */ | |
162 else if (strcmp(key, "darkgray") == 0) | |
163 color = custom ? GNT_COLOR_DARK_GRAY : COLOR_BLACK; | |
164 else if (strcmp(key, "magenta") == 0) | |
165 color = COLOR_MAGENTA; | |
166 else if (strcmp(key, "cyan") == 0) | |
167 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
|
168 else if (strcmp(key, "default") == 0) |
15817 | 169 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
|
170 else { |
2de3a2de5f13
Update the changelogs. Correct the man page. Allow 'default' to mean the
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
21691
diff
changeset
|
171 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
|
172 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
|
173 } |
15817 | 174 return color; |
175 } | |
176 | |
177 void gnt_colors_parse(GKeyFile *kfile) | |
178 { | |
179 GError *error = NULL; | |
180 gsize nkeys; | |
181 char **keys = g_key_file_get_keys(kfile, "colors", &nkeys, &error); | |
182 | |
183 if (error) | |
184 { | |
185 g_printerr("GntColors: %s\n", error->message); | |
186 g_error_free(error); | |
187 error = NULL; | |
188 } | |
189 else if (nkeys) | |
190 { | |
191 gnt_init_colors(); | |
192 while (nkeys--) | |
193 { | |
194 gsize len; | |
195 gchar *key = keys[nkeys]; | |
196 char **list = g_key_file_get_string_list(kfile, "colors", key, &len, NULL); | |
197 if (len == 3) | |
198 { | |
199 int r = atoi(list[0]); | |
200 int g = atoi(list[1]); | |
201 int b = atoi(list[2]); | |
202 int color = -1; | |
203 | |
204 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
|
205 color = gnt_colors_get_color(key); |
15817 | 206 g_free(key); |
207 if (color == -1) | |
208 continue; | |
209 | |
210 init_color(color, r, g, b); | |
211 } | |
212 g_strfreev(list); | |
213 } | |
214 | |
215 g_strfreev(keys); | |
216 } | |
217 | |
218 gnt_color_pairs_parse(kfile); | |
219 } | |
220 | |
221 void gnt_color_pairs_parse(GKeyFile *kfile) | |
222 { | |
223 GError *error = NULL; | |
224 gsize nkeys; | |
225 char **keys = g_key_file_get_keys(kfile, "colorpairs", &nkeys, &error); | |
226 | |
227 if (error) | |
228 { | |
229 g_printerr("GntColors: %s\n", error->message); | |
230 g_error_free(error); | |
231 return; | |
232 } | |
233 else if (nkeys) | |
234 gnt_init_colors(); | |
235 | |
236 while (nkeys--) | |
237 { | |
238 gsize len; | |
239 gchar *key = keys[nkeys]; | |
240 char **list = g_key_file_get_string_list(kfile, "colorpairs", key, &len, NULL); | |
241 if (len == 2) | |
242 { | |
243 GntColorType type = 0; | |
244 gchar *fgc = g_ascii_strdown(list[0], -1); | |
245 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
|
246 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
|
247 int bg = gnt_colors_get_color(bgc); |
15817 | 248 g_free(fgc); |
249 g_free(bgc); | |
250 if (fg == -1 || bg == -1) | |
251 continue; | |
252 | |
253 key = g_ascii_strdown(key, -1); | |
254 | |
255 if (strcmp(key, "normal") == 0) | |
256 type = GNT_COLOR_NORMAL; | |
257 else if (strcmp(key, "highlight") == 0) | |
258 type = GNT_COLOR_HIGHLIGHT; | |
259 else if (strcmp(key, "highlightd") == 0) | |
260 type = GNT_COLOR_HIGHLIGHT_D; | |
261 else if (strcmp(key, "shadow") == 0) | |
262 type = GNT_COLOR_SHADOW; | |
263 else if (strcmp(key, "title") == 0) | |
264 type = GNT_COLOR_TITLE; | |
265 else if (strcmp(key, "titled") == 0) | |
266 type = GNT_COLOR_TITLE_D; | |
267 else if (strcmp(key, "text") == 0) | |
268 type = GNT_COLOR_TEXT_NORMAL; | |
269 else if (strcmp(key, "disabled") == 0) | |
270 type = GNT_COLOR_DISABLED; | |
271 else if (strcmp(key, "urgent") == 0) | |
272 type = GNT_COLOR_URGENT; | |
273 else { | |
274 g_free(key); | |
275 continue; | |
276 } | |
277 g_free(key); | |
278 | |
279 init_pair(type, fg, bg); | |
280 } | |
281 g_strfreev(list); | |
282 } | |
283 | |
284 g_strfreev(keys); | |
285 } | |
286 | |
287 #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
|
288 |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
289 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
|
290 { |
9187d331aebe
Add gnt_color_pair, which will replace color codes with 'appropriate' text
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19681
diff
changeset
|
291 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
|
292 ((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
|
293 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
|
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 |
21691
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
296 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
|
297 { |
3ed9b027479d
Add support for colour in gnttreerows, and colourise the blist. Fixes #1490
Richard Nelson <wabz@pidgin.im>
parents:
21250
diff
changeset
|
298 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
|
299 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
|
300 } |