2
* Copyright (c) 2003-2009 by FlashCode <flashcode@flashtux.org>
3
* See README for License detail, AUTHORS for developers list.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
/* gui-color.c: color functions, used by all GUI */
35
#include "../core/weechat.h"
36
#include "../core/wee-config.h"
37
#include "../core/wee-string.h"
38
#include "../core/wee-utf8.h"
39
#include "gui-color.h"
42
struct t_gui_color *gui_color[GUI_COLOR_NUM_COLORS]; /* GUI colors */
46
* gui_color_search_config_int: search a color with configuration option name
47
* return color found (number >= 0), -1 if not found
51
gui_color_search_config_int (const char *color_name)
53
struct t_config_section *ptr_section;
54
struct t_config_option *ptr_option;
58
ptr_section = config_file_search_section (weechat_config_file,
62
for (ptr_option = ptr_section->options; ptr_option;
63
ptr_option = ptr_option->next_option)
65
if (string_strcasecmp (ptr_option->name, color_name) == 0)
66
return ptr_option->min;
76
* gui_color_get_custom: get a custom color with a name (GUI dependent)
80
gui_color_get_custom (const char *color_name)
83
static char color[20][16];
84
static int index_color = 0;
85
char *pos_comma, *str_fg, *pos_bg;
87
/* attribute or other color name (GUI dependent) */
88
index_color = (index_color + 1) % 20;
89
color[index_color][0] = '\0';
91
if (!color_name || !color_name[0])
92
return color[index_color];
94
if (string_strcasecmp (color_name, "reset") == 0)
96
snprintf (color[index_color], sizeof (color[index_color]),
100
else if (string_strcasecmp (color_name, "bold") == 0)
102
snprintf (color[index_color], sizeof (color[index_color]),
104
GUI_COLOR_SET_WEECHAT_STR,
105
GUI_COLOR_ATTR_BOLD_STR);
107
else if (string_strcasecmp (color_name, "-bold") == 0)
109
snprintf (color[index_color], sizeof (color[index_color]),
111
GUI_COLOR_REMOVE_WEECHAT_STR,
112
GUI_COLOR_ATTR_BOLD_STR);
114
else if (string_strcasecmp (color_name, "reverse") == 0)
116
snprintf (color[index_color], sizeof (color[index_color]),
118
GUI_COLOR_SET_WEECHAT_STR,
119
GUI_COLOR_ATTR_REVERSE_STR);
121
else if (string_strcasecmp (color_name, "-reverse") == 0)
123
snprintf (color[index_color], sizeof (color[index_color]),
125
GUI_COLOR_REMOVE_WEECHAT_STR,
126
GUI_COLOR_ATTR_REVERSE_STR);
128
else if (string_strcasecmp (color_name, "italic") == 0)
130
snprintf (color[index_color], sizeof (color[index_color]),
132
GUI_COLOR_SET_WEECHAT_STR,
133
GUI_COLOR_ATTR_ITALIC_STR);
135
else if (string_strcasecmp (color_name, "-italic") == 0)
137
snprintf (color[index_color], sizeof (color[index_color]),
139
GUI_COLOR_REMOVE_WEECHAT_STR,
140
GUI_COLOR_ATTR_ITALIC_STR);
142
else if (string_strcasecmp (color_name, "underline") == 0)
144
snprintf (color[index_color], sizeof (color[index_color]),
146
GUI_COLOR_SET_WEECHAT_STR,
147
GUI_COLOR_ATTR_UNDERLINE_STR);
149
else if (string_strcasecmp (color_name, "-underline") == 0)
151
snprintf (color[index_color], sizeof (color[index_color]),
153
GUI_COLOR_REMOVE_WEECHAT_STR,
154
GUI_COLOR_ATTR_UNDERLINE_STR);
156
else if (string_strcasecmp (color_name, "bar_fg") == 0)
158
snprintf (color[index_color], sizeof (color[index_color]),
160
GUI_COLOR_COLOR_CHAR,
162
GUI_COLOR_BAR_FG_CHAR);
164
else if (string_strcasecmp (color_name, "bar_delim") == 0)
166
snprintf (color[index_color], sizeof (color[index_color]),
168
GUI_COLOR_COLOR_CHAR,
170
GUI_COLOR_BAR_DELIM_CHAR);
172
else if (string_strcasecmp (color_name, "bar_bg") == 0)
174
snprintf (color[index_color], sizeof (color[index_color]),
176
GUI_COLOR_COLOR_CHAR,
178
GUI_COLOR_BAR_BG_CHAR);
182
/* custom color name (GUI dependent) */
183
pos_comma = strchr (color_name, ',');
186
if (pos_comma == color_name)
189
str_fg = string_strndup (color_name, pos_comma - color_name);
190
pos_bg = pos_comma + 1;
194
str_fg = strdup (color_name);
198
if (str_fg && pos_bg)
200
fg = gui_color_search (str_fg);
201
bg = gui_color_search (pos_bg);
202
snprintf (color[index_color], sizeof (color[index_color]),
204
GUI_COLOR_COLOR_STR, fg, bg);
206
else if (str_fg && !pos_bg)
208
fg = gui_color_search (str_fg);
209
snprintf (color[index_color], sizeof (color[index_color]),
211
GUI_COLOR_COLOR_STR, fg);
213
else if (!str_fg && pos_bg)
215
bg = gui_color_search (pos_bg);
216
snprintf (color[index_color], sizeof (color[index_color]),
218
GUI_COLOR_COLOR_STR, bg);
225
return color[index_color];
229
* gui_color_decode: parses a message and remove WeeChat color codes
230
* if replacement is not NULL and not empty, it is used to
231
* replace color codes by first char of replacement (and
232
* next chars in string are NOT removed)
233
* if replacement is NULL or empty, color codes are removed,
234
* with following chars if they are related to color code
235
* After use, string returned has to be free()
239
gui_color_decode (const char *string, const char *replacement)
241
const unsigned char *ptr_string;
243
int out_length, out_pos, length;
248
out_length = (strlen ((char *)string) * 2) + 1;
249
out = malloc (out_length);
253
ptr_string = (unsigned char *)string;
255
while (ptr_string && ptr_string[0] && (out_pos < out_length - 1))
257
switch (ptr_string[0])
259
case GUI_COLOR_COLOR_CHAR:
261
if (replacement && replacement[0])
263
out[out_pos] = replacement[0];
268
switch (ptr_string[0])
270
case GUI_COLOR_FG_CHAR:
271
case GUI_COLOR_BG_CHAR:
272
if (ptr_string[1] && ptr_string[2])
275
case GUI_COLOR_FG_BG_CHAR:
276
if (ptr_string[1] && ptr_string[2] && (ptr_string[3] == ',')
277
&& ptr_string[4] && ptr_string[5])
280
case GUI_COLOR_BAR_CHAR:
282
switch (ptr_string[0])
284
case GUI_COLOR_BAR_FG_CHAR:
285
case GUI_COLOR_BAR_BG_CHAR:
286
case GUI_COLOR_BAR_DELIM_CHAR:
287
case GUI_COLOR_BAR_START_INPUT_CHAR:
288
case GUI_COLOR_BAR_MOVE_CURSOR_CHAR:
294
if (isdigit (ptr_string[0]) && isdigit (ptr_string[1]))
300
case GUI_COLOR_SET_WEECHAT_CHAR:
301
case GUI_COLOR_REMOVE_WEECHAT_CHAR:
303
if (replacement && replacement[0])
305
out[out_pos] = replacement[0];
314
case GUI_COLOR_RESET_CHAR:
316
if (replacement && replacement[0])
318
out[out_pos] = replacement[0];
323
length = utf8_char_size ((char *)ptr_string);
326
memcpy (out + out_pos, ptr_string, length);
328
ptr_string += length;
338
* gui_color_free: free a color
342
gui_color_free (struct t_gui_color *color)
347
free (color->string);