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