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