pidgin  2.13.0
About: Pidgin is a chat program (multi-protocol instant messaging client) which lets you log in to accounts on multiple chat networks simultaneously (old name "gaim").
  Fossies Dox: pidgin-2.13.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

gntcolors.c
Go to the documentation of this file.
1 
23 #include "config.h"
24 
25 #include <ncurses.h>
26 
27 #include "gntinternal.h"
28 #undef GNT_LOG_DOMAIN
29 #define GNT_LOG_DOMAIN "Colors"
30 
31 #include "gntcolors.h"
32 #include "gntstyle.h"
33 
34 #include <glib.h>
35 
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 static gboolean hascolors;
41 static int custom_type = GNT_COLORS;
42 static struct
43 {
44  short r, g, b;
46 
47 static void
49 {
50  short i;
51  for (i = 0; i < GNT_TOTAL_COLORS; i++)
52  {
53  color_content(i, &colors[i].r,
54  &colors[i].g, &colors[i].b);
55  }
56 }
57 
58 static gboolean
60 {
61  return (gnt_style_get_bool(GNT_STYLE_COLOR, FALSE) && can_change_color());
62 }
63 
64 static void
66 {
67  short i;
68  for (i = 0; i < GNT_TOTAL_COLORS; i++)
69  {
70  init_color(i, colors[i].r,
71  colors[i].g, colors[i].b);
72  }
73 }
74 
76 {
77  static gboolean init = FALSE;
78  int defaults;
79 
80  if (init)
81  return;
82  init = TRUE;
83 
84  start_color();
85  if (!(hascolors = has_colors()))
86  return;
87  defaults = use_default_colors();
88 
90  {
91  backup_colors();
92 
93  /* Do some init_color()s */
94  init_color(GNT_COLOR_BLACK, 0, 0, 0);
95  init_color(GNT_COLOR_RED, 1000, 0, 0);
96  init_color(GNT_COLOR_GREEN, 0, 1000, 0);
97  init_color(GNT_COLOR_BLUE, 250, 250, 700);
98  init_color(GNT_COLOR_WHITE, 1000, 1000, 1000);
99  init_color(GNT_COLOR_GRAY, 699, 699, 699);
100  init_color(GNT_COLOR_DARK_GRAY, 256, 256, 256);
101 
102  /* Now some init_pair()s */
106 
109 
114  }
115  else
116  {
117  int bg;
118 
119  if (defaults == OK) {
120  init_pair(GNT_COLOR_NORMAL, -1, -1);
121  bg = -1;
122  } else {
123  init_pair(GNT_COLOR_NORMAL, COLOR_BLACK, COLOR_WHITE);
124  bg = COLOR_WHITE;
125  }
126  init_pair(GNT_COLOR_DISABLED, COLOR_YELLOW, bg);
127  init_pair(GNT_COLOR_URGENT, COLOR_GREEN, bg);
128 
129  init_pair(GNT_COLOR_HIGHLIGHT, COLOR_WHITE, COLOR_BLUE);
130  init_pair(GNT_COLOR_SHADOW, COLOR_BLACK, COLOR_BLACK);
131  init_pair(GNT_COLOR_TITLE, COLOR_WHITE, COLOR_BLUE);
132  init_pair(GNT_COLOR_TITLE_D, COLOR_WHITE, COLOR_BLACK);
133  init_pair(GNT_COLOR_TEXT_NORMAL, COLOR_WHITE, COLOR_BLUE);
134  init_pair(GNT_COLOR_HIGHLIGHT_D, COLOR_CYAN, COLOR_BLACK);
135  }
136 }
137 
138 void
140 {
141  if (can_use_custom_color())
142  restore_colors();
143 }
144 
145 #if GLIB_CHECK_VERSION(2,6,0)
146 int
147 gnt_colors_get_color(char *key)
148 {
149  int color;
150  gboolean custom = can_use_custom_color();
151 
152  key = g_strstrip(key);
153 
154  if (strcmp(key, "black") == 0)
155  color = custom ? GNT_COLOR_BLACK : COLOR_BLACK;
156  else if (strcmp(key, "red") == 0)
157  color = custom ? GNT_COLOR_RED : COLOR_RED;
158  else if (strcmp(key, "green") == 0)
159  color = custom ? GNT_COLOR_GREEN : COLOR_GREEN;
160  else if (strcmp(key, "blue") == 0)
161  color = custom ? GNT_COLOR_BLUE : COLOR_BLUE;
162  else if (strcmp(key, "white") == 0)
163  color = custom ? GNT_COLOR_WHITE : COLOR_WHITE;
164  else if (strcmp(key, "gray") == 0 || strcmp(key, "grey") == 0)
165  color = custom ? GNT_COLOR_GRAY : COLOR_YELLOW; /* eh? */
166  else if (strcmp(key, "darkgray") == 0 || strcmp(key, "darkgrey") == 0)
167  color = custom ? GNT_COLOR_DARK_GRAY : COLOR_BLACK;
168  else if (strcmp(key, "magenta") == 0)
169  color = COLOR_MAGENTA;
170  else if (strcmp(key, "cyan") == 0)
171  color = COLOR_CYAN;
172  else if (strcmp(key, "default") == 0)
173  color = -1;
174  else {
175  g_warning("Invalid color name: %s\n", key);
176  color = -EINVAL;
177  }
178  return color;
179 }
180 
181 void gnt_colors_parse(GKeyFile *kfile)
182 {
183  GError *error = NULL;
184  gsize nkeys;
185  char **keys = g_key_file_get_keys(kfile, "colors", &nkeys, &error);
186 
187  if (error)
188  {
189  gnt_warning("%s", error->message);
190  g_error_free(error);
191  error = NULL;
192  }
193  else if (nkeys)
194  {
195  gnt_init_colors();
196  while (nkeys--)
197  {
198  gsize len;
199  gchar *key = keys[nkeys];
200  char **list = g_key_file_get_string_list(kfile, "colors", key, &len, NULL);
201  if (len == 3)
202  {
203  int r = atoi(list[0]);
204  int g = atoi(list[1]);
205  int b = atoi(list[2]);
206  int color = -1;
207 
208  key = g_ascii_strdown(key, -1);
209  color = gnt_colors_get_color(key);
210  g_free(key);
211  if (color == -EINVAL) {
212  g_strfreev(list);
213  continue;
214  }
215 
216  init_color(color, r, g, b);
217  }
218  g_strfreev(list);
219  }
220 
221  g_strfreev(keys);
222  }
223 
224  gnt_color_pairs_parse(kfile);
225 }
226 
227 void gnt_color_pairs_parse(GKeyFile *kfile)
228 {
229  GError *error = NULL;
230  gsize nkeys;
231  char **keys = g_key_file_get_keys(kfile, "colorpairs", &nkeys, &error);
232 
233  if (error)
234  {
235  gnt_warning("%s", error->message);
236  g_error_free(error);
237  return;
238  }
239  else if (nkeys)
240  gnt_init_colors();
241 
242  while (nkeys--)
243  {
244  gsize len;
245  gchar *key = keys[nkeys];
246  char **list = g_key_file_get_string_list(kfile, "colorpairs", key, &len, NULL);
247  if (len == 2)
248  {
249  GntColorType type = 0;
250  gchar *fgc = g_ascii_strdown(list[0], -1);
251  gchar *bgc = g_ascii_strdown(list[1], -1);
252  int fg = gnt_colors_get_color(fgc);
253  int bg = gnt_colors_get_color(bgc);
254  g_free(fgc);
255  g_free(bgc);
256  if (fg == -EINVAL || bg == -EINVAL) {
257  g_strfreev(list);
258  continue;
259  }
260 
261  key = g_ascii_strdown(key, -1);
262 
263  if (strcmp(key, "normal") == 0)
265  else if (strcmp(key, "highlight") == 0)
267  else if (strcmp(key, "highlightd") == 0)
269  else if (strcmp(key, "shadow") == 0)
271  else if (strcmp(key, "title") == 0)
273  else if (strcmp(key, "titled") == 0)
275  else if (strcmp(key, "text") == 0)
277  else if (strcmp(key, "disabled") == 0)
279  else if (strcmp(key, "urgent") == 0)
281  else {
282  g_strfreev(list);
283  g_free(key);
284  continue;
285  }
286  g_free(key);
287 
288  init_pair(type, fg, bg);
289  }
290  g_strfreev(list);
291  }
292 
293  g_strfreev(keys);
294 }
295 
296 #endif /* GKeyFile */
297 
298 int gnt_color_pair(int pair)
299 {
300  return (hascolors ? COLOR_PAIR(pair) :
301  ((pair == GNT_COLOR_NORMAL || pair == GNT_COLOR_HIGHLIGHT_D ||
302  pair == GNT_COLOR_TITLE_D || pair == GNT_COLOR_DISABLED) ? 0 : (int)A_STANDOUT));
303 }
304 
305 int gnt_color_add_pair(int fg, int bg)
306 {
307  init_pair(custom_type, fg, bg);
308  return custom_type++;
309 }
GntColorType
GntColorType
Definition: gntcolors.h:35
GNT_COLOR_RED
Definition: gntcolors.h:55
GNT_COLOR_HIGHLIGHT_D
Definition: gntcolors.h:40
colors
static struct @17 colors[GNT_TOTAL_COLORS]
GNT_COLOR_TEXT_NORMAL
Definition: gntcolors.h:41
g
short g
Definition: gntcolors.c:44
GNT_COLOR_URGENT
Definition: gntcolors.h:48
custom_type
static int custom_type
Definition: gntcolors.c:41
GNT_COLOR_TITLE_D
Definition: gntcolors.h:47
keys
char * keys
Definition: gntbindable.c:45
GNT_COLOR_SHADOW
Definition: gntcolors.h:45
GNT_COLOR_TITLE
Definition: gntcolors.h:46
hascolors
static gboolean hascolors
Definition: gntcolors.c:40
gntinternal.h
gnt_init_colors
void gnt_init_colors()
Definition: gntcolors.c:75
GNT_COLOR_BLUE
Definition: gntcolors.h:57
GNT_COLOR_GRAY
Definition: gntcolors.h:59
gnt_warning
#define gnt_warning
Definition: gntinternal.h:32
NULL
#define NULL
Definition: getopt1.c:52
backup_colors
static void backup_colors(void)
Definition: gntcolors.c:48
GNT_COLOR_NORMAL
Definition: gntcolors.h:37
GNT_COLOR_BLACK
Definition: gntcolors.h:54
r
short r
Definition: gntcolors.c:44
gntcolors.h
TRUE
#define TRUE
Definition: protobuf-c.c:56
dbus-analyze-signals.type
string type
Definition: dbus-analyze-signals.py:42
FALSE
#define FALSE
Definition: protobuf-c.c:57
GNT_TOTAL_COLORS
Definition: gntcolors.h:61
GNT_COLOR_DISABLED
Definition: gntcolors.h:39
gnt_uninit_colors
void gnt_uninit_colors()
Definition: gntcolors.c:139
b
short b
Definition: gntcolors.c:44
can_use_custom_color
static gboolean can_use_custom_color(void)
Definition: gntcolors.c:59
gntstyle.h
GNT_COLOR_HIGHLIGHT
Definition: gntcolors.h:38
gnt_color_pair
int gnt_color_pair(int pair)
Definition: gntcolors.c:298
GNT_COLOR_WHITE
Definition: gntcolors.h:58
error
const char * error
Definition: auth_scram.c:51
GNT_COLOR_DARK_GRAY
Definition: gntcolors.h:60
config.h
GNT_COLORS
Definition: gntcolors.h:49
gnt_color_add_pair
int gnt_color_add_pair(int fg, int bg)
Definition: gntcolors.c:305
gnt_style_get_bool
gboolean gnt_style_get_bool(GntStyle style, gboolean def)
Definition: gntstyle.c:102
GNT_COLOR_GREEN
Definition: gntcolors.h:56
GNT_STYLE_COLOR
Definition: gntstyle.h:33
restore_colors
static void restore_colors(void)
Definition: gntcolors.c:65