Mercurial > pidgin
annotate finch/libgnt/gntcolors.c @ 18425:84d318eadc62
The input was causing some weird issues. This fixes it.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Tue, 03 Jul 2007 11:09:19 +0000 |
parents | 1cedd520cd18 |
children | 8d9913d9fe5a |
rev | line source |
---|---|
18049
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
1 /** |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
3 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
6 * source distribution. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
7 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
11 * (at your option) any later version. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
12 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
16 * GNU General Public License for more details. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
17 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
21 */ |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
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 | |
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 | |
136 static int | |
137 get_color(char *key) | |
138 { | |
139 int color; | |
140 gboolean custom = can_use_custom_color(); | |
141 | |
142 key = g_strstrip(key); | |
143 | |
144 if (strcmp(key, "black") == 0) | |
145 color = custom ? GNT_COLOR_BLACK : COLOR_BLACK; | |
146 else if (strcmp(key, "red") == 0) | |
147 color = custom ? GNT_COLOR_RED : COLOR_RED; | |
148 else if (strcmp(key, "green") == 0) | |
149 color = custom ? GNT_COLOR_GREEN : COLOR_GREEN; | |
150 else if (strcmp(key, "blue") == 0) | |
151 color = custom ? GNT_COLOR_BLUE : COLOR_BLUE; | |
152 else if (strcmp(key, "white") == 0) | |
153 color = custom ? GNT_COLOR_WHITE : COLOR_WHITE; | |
154 else if (strcmp(key, "gray") == 0) | |
155 color = custom ? GNT_COLOR_GRAY : COLOR_YELLOW; /* eh? */ | |
156 else if (strcmp(key, "darkgray") == 0) | |
157 color = custom ? GNT_COLOR_DARK_GRAY : COLOR_BLACK; | |
158 else if (strcmp(key, "magenta") == 0) | |
159 color = COLOR_MAGENTA; | |
160 else if (strcmp(key, "cyan") == 0) | |
161 color = COLOR_CYAN; | |
162 else | |
163 color = -1; | |
164 return color; | |
165 } | |
166 | |
167 #if GLIB_CHECK_VERSION(2,6,0) | |
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 */ |