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