summaryrefslogtreecommitdiff
path: root/src/gui/gui-color.c
blob: 0cb4b526173b08a310cfdeae4d5e6535e4f57e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (c) 2003-2008 by FlashCode <flashcode@flashtux.org>
 * See README for License detail, AUTHORS for developers list.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* gui-color.c: color functions, used by all GUI */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <ctype.h>

#include "../core/weechat.h"
#include "../core/wee-config.h"
#include "../core/wee-string.h"
#include "../core/wee-utf8.h"
#include "gui-color.h"


struct t_gui_color *gui_color[GUI_COLOR_NUM_COLORS]; /* GUI colors          */


/*
 * gui_color_search_config_int: search a color with configuration option name
 *                              return color found (number >= 0), -1 if not found
 */

int
gui_color_search_config_int (char *color_name)
{
    struct t_config_section *ptr_section;
    struct t_config_option *ptr_option;
    
    if (color_name)
    {
        ptr_section = config_file_search_section (weechat_config_file,
                                                  "color");
        if (ptr_section)
        {
            for (ptr_option = ptr_section->options; ptr_option;
                 ptr_option = ptr_option->next_option)
            {
                if (string_strcasecmp (ptr_option->name, color_name) == 0)
                    return ptr_option->min;
            }
        }
    }
    
    /* color not found */
    return -1;
}

/*
 * gui_color_search_config_str: search a color configuration name with number
 *                              return color configuration name, NULL if not found
 */

char *
gui_color_search_config_str (int color_number)
{
    struct t_config_section *ptr_section;
    struct t_config_option *ptr_option;
    
    ptr_section = config_file_search_section (weechat_config_file,
                                              "color");
    if (ptr_section)
    {
        for (ptr_option = ptr_section->options; ptr_option;
             ptr_option = ptr_option->next_option)
        {
            if (ptr_option->min == color_number)
                return ptr_option->name;
        }
    }
    
    /* color not found */
    return NULL;
}

/*
 * gui_color_decode: parses a message and remove WeeChat color codes
 *                   After use, string returned has to be free()
 */

unsigned char *
gui_color_decode (unsigned char *string)
{
    unsigned char *out;
    int out_length, out_pos, length;
    
    out_length = (strlen ((char *)string) * 2) + 1;
    out = malloc (out_length);
    if (!out)
        return NULL;
    
    out_pos = 0;
    while (string && string[0] && (out_pos < out_length - 1))
    {
        switch (string[0])
        {
            case GUI_COLOR_COLOR_CHAR:
                string++;
                switch (string[0])
                {
                    case 'F':
                    case 'B':
                        if (string[1] && string[2])
                            string += 3;
                        break;
                    case '*':
                        if (string[1] && string[2] && (string[3] == ',')
                            && string[4] && string[5])
                            string += 6;
                        break;
                    default:
                        if (isdigit (string[0]) && isdigit (string[1]))
                            string += 2;
                        break;
                }
                break;
            case GUI_COLOR_SET_CHAR:
            case GUI_COLOR_REMOVE_CHAR:
                string++;
                if (string[0])
                    string++;
                break;
            case GUI_COLOR_RESET_CHAR:
                string++;
                break;
            default:
                length = utf8_char_size ((char *)string);
                if (length == 0)
                    length = 1;
                memcpy (out + out_pos, string, length);
                out_pos += length;
                string += length;
        }
    }
    out[out_pos] = '\0';
    return out;
}

/*
 * gui_color_free: free a color
 */

void
gui_color_free (struct t_gui_color *color)
{
    if (color->string)
        free (color->string);
    
    free (color);
}