~ubuntu-branches/ubuntu/trusty/weechat/trusty-proposed

« back to all changes in this revision

Viewing changes to src/gui/gui-color.c

  • Committer: Bazaar Package Importer
  • Author(s): Emmanuel Bouthenot
  • Date: 2009-09-15 20:58:07 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090915205807-nd2nx3kof2aldqbt
Tags: 0.3.0-1
* New (final) upstream release.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2003-2009 by FlashCode <flashcode@flashtux.org>
 
3
 * See README for License detail, AUTHORS for developers list.
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
/* gui-color.c: color functions, used by all GUI */
 
20
 
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include "config.h"
 
24
#endif
 
25
 
 
26
#include <stdlib.h>
 
27
#include <stdio.h>
 
28
#include <stdarg.h>
 
29
#include <unistd.h>
 
30
#include <string.h>
 
31
#include <signal.h>
 
32
#include <time.h>
 
33
#include <ctype.h>
 
34
 
 
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"
 
40
 
 
41
 
 
42
struct t_gui_color *gui_color[GUI_COLOR_NUM_COLORS]; /* GUI colors          */
 
43
 
 
44
 
 
45
/*
 
46
 * gui_color_search_config_int: search a color with configuration option name
 
47
 *                              return color found (number >= 0), -1 if not found
 
48
 */
 
49
 
 
50
int
 
51
gui_color_search_config_int (const char *color_name)
 
52
{
 
53
    struct t_config_section *ptr_section;
 
54
    struct t_config_option *ptr_option;
 
55
    
 
56
    if (color_name)
 
57
    {
 
58
        ptr_section = config_file_search_section (weechat_config_file,
 
59
                                                  "color");
 
60
        if (ptr_section)
 
61
        {
 
62
            for (ptr_option = ptr_section->options; ptr_option;
 
63
                 ptr_option = ptr_option->next_option)
 
64
            {
 
65
                if (string_strcasecmp (ptr_option->name, color_name) == 0)
 
66
                    return ptr_option->min;
 
67
            }
 
68
        }
 
69
    }
 
70
    
 
71
    /* color not found */
 
72
    return -1;
 
73
}
 
74
 
 
75
/*
 
76
 * gui_color_get_custom: get a custom color with a name (GUI dependent)
 
77
 */
 
78
 
 
79
const char *
 
80
gui_color_get_custom (const char *color_name)
 
81
{
 
82
    int fg, bg;
 
83
    static char color[20][16];
 
84
    static int index_color = 0;
 
85
    char *pos_comma, *str_fg, *pos_bg;
 
86
    
 
87
    /* attribute or other color name (GUI dependent) */
 
88
    index_color = (index_color + 1) % 20;
 
89
    color[index_color][0] = '\0';
 
90
    
 
91
    if (!color_name || !color_name[0])
 
92
        return color[index_color];
 
93
    
 
94
    if (string_strcasecmp (color_name, "reset") == 0)
 
95
    {
 
96
        snprintf (color[index_color], sizeof (color[index_color]),
 
97
                  "%s",
 
98
                  GUI_COLOR_RESET_STR);
 
99
    }
 
100
    else if (string_strcasecmp (color_name, "bold") == 0)
 
101
    {
 
102
        snprintf (color[index_color], sizeof (color[index_color]),
 
103
                  "%s%s",
 
104
                  GUI_COLOR_SET_WEECHAT_STR,
 
105
                  GUI_COLOR_ATTR_BOLD_STR);
 
106
    }
 
107
    else if (string_strcasecmp (color_name, "-bold") == 0)
 
108
    {
 
109
        snprintf (color[index_color], sizeof (color[index_color]),
 
110
                  "%s%s",
 
111
                  GUI_COLOR_REMOVE_WEECHAT_STR,
 
112
                  GUI_COLOR_ATTR_BOLD_STR);
 
113
    }
 
114
    else if (string_strcasecmp (color_name, "reverse") == 0)
 
115
    {
 
116
        snprintf (color[index_color], sizeof (color[index_color]),
 
117
                  "%s%s",
 
118
                  GUI_COLOR_SET_WEECHAT_STR,
 
119
                  GUI_COLOR_ATTR_REVERSE_STR);
 
120
    }
 
121
    else if (string_strcasecmp (color_name, "-reverse") == 0)
 
122
    {
 
123
        snprintf (color[index_color], sizeof (color[index_color]),
 
124
                  "%s%s",
 
125
                  GUI_COLOR_REMOVE_WEECHAT_STR,
 
126
                  GUI_COLOR_ATTR_REVERSE_STR);
 
127
    }
 
128
    else if (string_strcasecmp (color_name, "italic") == 0)
 
129
    {
 
130
        snprintf (color[index_color], sizeof (color[index_color]),
 
131
                  "%s%s",
 
132
                  GUI_COLOR_SET_WEECHAT_STR,
 
133
                  GUI_COLOR_ATTR_ITALIC_STR);
 
134
    }
 
135
    else if (string_strcasecmp (color_name, "-italic") == 0)
 
136
    {
 
137
        snprintf (color[index_color], sizeof (color[index_color]),
 
138
                  "%s%s",
 
139
                  GUI_COLOR_REMOVE_WEECHAT_STR,
 
140
                  GUI_COLOR_ATTR_ITALIC_STR);
 
141
    }
 
142
    else if (string_strcasecmp (color_name, "underline") == 0)
 
143
    {
 
144
        snprintf (color[index_color], sizeof (color[index_color]),
 
145
                  "%s%s",
 
146
                  GUI_COLOR_SET_WEECHAT_STR,
 
147
                  GUI_COLOR_ATTR_UNDERLINE_STR);
 
148
    }
 
149
    else if (string_strcasecmp (color_name, "-underline") == 0)
 
150
    {
 
151
        snprintf (color[index_color], sizeof (color[index_color]),
 
152
                  "%s%s",
 
153
                  GUI_COLOR_REMOVE_WEECHAT_STR,
 
154
                  GUI_COLOR_ATTR_UNDERLINE_STR);
 
155
    }
 
156
    else if (string_strcasecmp (color_name, "bar_fg") == 0)
 
157
    {
 
158
        snprintf (color[index_color], sizeof (color[index_color]),
 
159
                  "%c%c%c",
 
160
                  GUI_COLOR_COLOR_CHAR,
 
161
                  GUI_COLOR_BAR_CHAR,
 
162
                  GUI_COLOR_BAR_FG_CHAR);
 
163
    }
 
164
    else if (string_strcasecmp (color_name, "bar_delim") == 0)
 
165
    {
 
166
        snprintf (color[index_color], sizeof (color[index_color]),
 
167
                  "%c%c%c",
 
168
                  GUI_COLOR_COLOR_CHAR,
 
169
                  GUI_COLOR_BAR_CHAR,
 
170
                  GUI_COLOR_BAR_DELIM_CHAR);
 
171
    }
 
172
    else if (string_strcasecmp (color_name, "bar_bg") == 0)
 
173
    {
 
174
        snprintf (color[index_color], sizeof (color[index_color]),
 
175
                  "%c%c%c",
 
176
                  GUI_COLOR_COLOR_CHAR,
 
177
                  GUI_COLOR_BAR_CHAR,
 
178
                  GUI_COLOR_BAR_BG_CHAR);
 
179
    }
 
180
    else
 
181
    {
 
182
        /* custom color name (GUI dependent) */
 
183
        pos_comma = strchr (color_name, ',');
 
184
        if (pos_comma)
 
185
        {
 
186
            if (pos_comma == color_name)
 
187
                str_fg = NULL;
 
188
            else
 
189
                str_fg = string_strndup (color_name, pos_comma - color_name);
 
190
            pos_bg = pos_comma + 1;
 
191
        }
 
192
        else
 
193
        {
 
194
            str_fg = strdup (color_name);
 
195
            pos_bg = NULL;
 
196
        }
 
197
        
 
198
        if (str_fg && pos_bg)
 
199
        {
 
200
            fg = gui_color_search (str_fg);
 
201
            bg = gui_color_search (pos_bg);
 
202
            snprintf (color[index_color], sizeof (color[index_color]),
 
203
                      "%s*%02d,%02d",
 
204
                      GUI_COLOR_COLOR_STR, fg, bg);
 
205
        }
 
206
        else if (str_fg && !pos_bg)
 
207
        {
 
208
            fg = gui_color_search (str_fg);
 
209
            snprintf (color[index_color], sizeof (color[index_color]),
 
210
                      "%sF%02d",
 
211
                      GUI_COLOR_COLOR_STR, fg);
 
212
        }
 
213
        else if (!str_fg && pos_bg)
 
214
        {
 
215
            bg = gui_color_search (pos_bg);
 
216
            snprintf (color[index_color], sizeof (color[index_color]),
 
217
                      "%sB%02d",
 
218
                      GUI_COLOR_COLOR_STR, bg);
 
219
        }
 
220
        
 
221
        if (str_fg)
 
222
            free (str_fg);
 
223
    }
 
224
    
 
225
    return color[index_color];
 
226
}
 
227
 
 
228
/*
 
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()
 
236
 */
 
237
 
 
238
char *
 
239
gui_color_decode (const char *string, const char *replacement)
 
240
{
 
241
    const unsigned char *ptr_string;
 
242
    unsigned char *out;
 
243
    int out_length, out_pos, length;
 
244
    
 
245
    if (!string)
 
246
        return NULL;
 
247
    
 
248
    out_length = (strlen ((char *)string) * 2) + 1;
 
249
    out = malloc (out_length);
 
250
    if (!out)
 
251
        return NULL;
 
252
    
 
253
    ptr_string = (unsigned char *)string;
 
254
    out_pos = 0;
 
255
    while (ptr_string && ptr_string[0] && (out_pos < out_length - 1))
 
256
    {
 
257
        switch (ptr_string[0])
 
258
        {
 
259
            case GUI_COLOR_COLOR_CHAR:
 
260
                ptr_string++;
 
261
                if (replacement && replacement[0])
 
262
                {
 
263
                    out[out_pos] = replacement[0];
 
264
                    out_pos++;
 
265
                }
 
266
                else
 
267
                {
 
268
                    switch (ptr_string[0])
 
269
                    {
 
270
                        case GUI_COLOR_FG_CHAR:
 
271
                        case GUI_COLOR_BG_CHAR:
 
272
                            if (ptr_string[1] && ptr_string[2])
 
273
                                ptr_string += 3;
 
274
                            break;
 
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])
 
278
                                ptr_string += 6;
 
279
                            break;
 
280
                        case GUI_COLOR_BAR_CHAR:
 
281
                            ptr_string++;
 
282
                            switch (ptr_string[0])
 
283
                            {
 
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:
 
289
                                    ptr_string++;
 
290
                                    break;
 
291
                            }
 
292
                            break;
 
293
                        default:
 
294
                            if (isdigit (ptr_string[0]) && isdigit (ptr_string[1]))
 
295
                                ptr_string += 2;
 
296
                            break;
 
297
                    }
 
298
                }
 
299
                break;
 
300
            case GUI_COLOR_SET_WEECHAT_CHAR:
 
301
            case GUI_COLOR_REMOVE_WEECHAT_CHAR:
 
302
                ptr_string++;
 
303
                if (replacement && replacement[0])
 
304
                {
 
305
                    out[out_pos] = replacement[0];
 
306
                    out_pos++;
 
307
                }
 
308
                else
 
309
                {
 
310
                    if (ptr_string[0])
 
311
                        ptr_string++;
 
312
                }
 
313
                break;
 
314
            case GUI_COLOR_RESET_CHAR:
 
315
                ptr_string++;
 
316
                if (replacement && replacement[0])
 
317
                {
 
318
                    out[out_pos] = replacement[0];
 
319
                    out_pos++;
 
320
                }
 
321
                break;
 
322
            default:
 
323
                length = utf8_char_size ((char *)ptr_string);
 
324
                if (length == 0)
 
325
                    length = 1;
 
326
                memcpy (out + out_pos, ptr_string, length);
 
327
                out_pos += length;
 
328
                ptr_string += length;
 
329
                break;
 
330
        }
 
331
    }
 
332
    out[out_pos] = '\0';
 
333
    
 
334
    return (char *)out;
 
335
}
 
336
 
 
337
/*
 
338
 * gui_color_free: free a color
 
339
 */
 
340
 
 
341
void
 
342
gui_color_free (struct t_gui_color *color)
 
343
{
 
344
    if (color)
 
345
    {
 
346
        if (color->string)
 
347
            free (color->string);
 
348
        
 
349
        free (color);
 
350
    }
 
351
}