Merge branch '2121_dir_symlink'
[kaloumi3.git] / lib / skin / colors.c
blob22f6c1ae04d8f2571bf0ad76c32fe653d15318a4
1 /*
2 Skins engine.
3 Work with colors
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 #include <config.h>
29 #include <string.h>
31 #include "internal.h"
32 #include "lib/tty/color.h"
34 #include "src/args.h"
36 /*** global variables ****************************************************************************/
38 int mc_skin_color__cache[MC_SKIN_COLOR_CACHE_COUNT];
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 /*** file scope functions ************************************************************************/
48 static mc_skin_color_t *
49 mc_skin_color_get_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
51 gchar kname[BUF_TINY];
52 mc_skin_color_t *mc_skin_color;
54 if (group == NULL || key == NULL)
55 return NULL;
57 if (mc_skin == NULL)
58 mc_skin = &mc_skin__default;
60 g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
61 mc_skin_color = (mc_skin_color_t *) g_hash_table_lookup (mc_skin->colors, (gpointer) kname);
63 return mc_skin_color;
66 /* --------------------------------------------------------------------------------------------- */
68 #if 0
69 static void
70 mc_skin_color_remove_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
72 gchar kname[BUF_TINY];
73 if (group == NULL || key == NULL)
74 return;
76 if (mc_skin == NULL)
77 mc_skin = &mc_skin__default;
79 g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
80 g_hash_table_remove (mc_skin->colors, (gpointer) kname);
82 #endif
83 /* --------------------------------------------------------------------------------------------- */
85 static void
86 mc_skin_color_add_to_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key,
87 mc_skin_color_t * mc_skin_color)
89 gchar *kname;
91 kname = g_strdup_printf ("%s.%s", group, key);
92 if (kname != NULL) {
93 if (g_hash_table_lookup (mc_skin->colors, (gpointer) kname) != NULL)
94 g_hash_table_remove (mc_skin->colors, (gpointer) kname);
96 g_hash_table_insert (mc_skin->colors, (gpointer) kname, (gpointer) mc_skin_color);
100 /* --------------------------------------------------------------------------------------------- */
102 static mc_skin_color_t *
103 mc_skin_color_get_with_defaults (const gchar * group, const gchar * name)
105 mc_skin_color_t *mc_skin_color;
107 mc_skin_color = mc_skin_color_get_from_hash (NULL, group, name);
108 if (mc_skin_color != NULL)
109 return mc_skin_color;
111 mc_skin_color = mc_skin_color_get_from_hash (NULL, group, "_default_");
112 if (mc_skin_color != NULL)
113 return mc_skin_color;
115 mc_skin_color = mc_skin_color_get_from_hash (NULL, "core", "_default_");
116 return mc_skin_color;
119 /* --------------------------------------------------------------------------------------------- */
121 static mc_skin_color_t *
122 mc_skin_color_get_from_ini_file (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
124 gsize items_count;
125 gchar **values;
126 mc_skin_color_t *mc_skin_color, *tmp;
128 values = mc_config_get_string_list (mc_skin->config, group, key, &items_count);
130 if (values == NULL || *values == NULL) {
131 if (values != NULL)
132 g_strfreev (values);
133 return NULL;
135 mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
136 if (mc_skin_color == NULL) {
137 g_strfreev (values);
138 return NULL;
141 switch (items_count) {
142 case 0:
143 tmp = mc_skin_color_get_with_defaults (group, "_default_");
144 if (tmp) {
145 mc_skin_color->fgcolor = g_strdup (tmp->fgcolor);
146 mc_skin_color->bgcolor = g_strdup (tmp->bgcolor);
147 } else {
148 g_strfreev (values);
149 g_free (mc_skin_color);
150 return NULL;
152 break;
153 case 1:
154 mc_skin_color->fgcolor = (values[0]) ? g_strstrip (g_strdup (values[0])) : NULL;
155 tmp = mc_skin_color_get_with_defaults (group, "_default_");
156 mc_skin_color->bgcolor = (tmp != NULL) ? g_strdup (tmp->bgcolor) : NULL;
157 break;
158 case 2:
159 mc_skin_color->fgcolor = (values[0]) ? g_strstrip (g_strdup (values[0])) : NULL;
160 mc_skin_color->bgcolor = (values[1]) ? g_strstrip (g_strdup (values[1])) : NULL;
161 break;
163 g_strfreev (values);
165 mc_skin_color->pair_index =
166 tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
168 return mc_skin_color;
171 /* --------------------------------------------------------------------------------------------- */
173 static void
174 mc_skin_color_set_default_for_terminal (mc_skin_t * mc_skin)
176 mc_skin_color_t *mc_skin_color;
177 mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
178 if (mc_skin_color != NULL) {
179 mc_skin_color->fgcolor = g_strdup ("default");
180 mc_skin_color->bgcolor = g_strdup ("default");
181 mc_skin_color->pair_index =
182 tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
183 mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
187 /* --------------------------------------------------------------------------------------------- */
188 static void
189 mc_skin_color_cache_init (void)
191 DEFAULT_COLOR = mc_skin_color_get ("skin", "terminal_default_color");
192 NORMAL_COLOR = mc_skin_color_get ("core", "_default_");
193 MARKED_COLOR = mc_skin_color_get ("core", "marked");
194 SELECTED_COLOR = mc_skin_color_get ("core", "selected");
195 MARKED_SELECTED_COLOR = mc_skin_color_get ("core", "markselect");
196 REVERSE_COLOR = mc_skin_color_get ("core", "reverse");
198 COLOR_NORMAL = mc_skin_color_get ("dialog", "_default_");
199 COLOR_FOCUS = mc_skin_color_get ("dialog", "dfocus");
200 COLOR_HOT_NORMAL = mc_skin_color_get ("dialog", "dhotnormal");
201 COLOR_HOT_FOCUS = mc_skin_color_get ("dialog", "dhotfocus");
203 ERROR_COLOR = mc_skin_color_get ("error", "_default_");
204 ERROR_HOT_NORMAL = mc_skin_color_get ("error", "errdhotnormal");
205 ERROR_HOT_FOCUS = mc_skin_color_get ("error", "errdhotfocus");
207 MENU_ENTRY_COLOR = mc_skin_color_get ("menu", "_default_");
208 MENU_SELECTED_COLOR = mc_skin_color_get ("menu", "menusel");
209 MENU_HOT_COLOR = mc_skin_color_get ("menu", "menuhot");
210 MENU_HOTSEL_COLOR = mc_skin_color_get ("menu", "menuhotsel");
211 MENU_INACTIVE_COLOR = mc_skin_color_get ("menu", "menuinactive");
213 GAUGE_COLOR = mc_skin_color_get ("core", "gauge");
214 INPUT_COLOR = mc_skin_color_get ("core", "input");
216 HELP_NORMAL_COLOR = mc_skin_color_get ("help", "_default_");
217 HELP_ITALIC_COLOR = mc_skin_color_get ("help", "helpitalic");
218 HELP_BOLD_COLOR = mc_skin_color_get ("help", "helpbold");
219 HELP_LINK_COLOR = mc_skin_color_get ("help", "helplink");
220 HELP_SLINK_COLOR = mc_skin_color_get ("help", "helpslink");
222 VIEW_UNDERLINED_COLOR = mc_skin_color_get ("viewer", "viewunderline");
224 EDITOR_NORMAL_COLOR = mc_skin_color_get ("editor", "_default_");
225 EDITOR_BOLD_COLOR = mc_skin_color_get ("editor", "editbold");
226 EDITOR_MARKED_COLOR = mc_skin_color_get ("editor", "editmarked");
227 EDITOR_WHITESPACE_COLOR = mc_skin_color_get ("editor", "editwhitespace");
228 EDITOR_RIGHT_MARGIN_COLOR = mc_skin_color_get ("editor", "editrightmargin");
229 LINE_STATE_COLOR = mc_skin_color_get ("editor", "linestate");
231 BOOK_MARK_COLOR = mc_skin_color_get ("editor", "bookmark");
232 BOOK_MARK_FOUND_COLOR = mc_skin_color_get ("editor", "bookmarkfound");
233 BUTTONBAR_HOTKEY_COLOR = mc_skin_color_get ("buttonbar", "hotkey");
234 BUTTONBAR_BUTTON_COLOR = mc_skin_color_get ("buttonbar", "button");
237 /* --------------------------------------------------------------------------------------------- */
239 static gboolean
240 mc_skin_color_check_inisection (const gchar * group)
242 return !((strcasecmp ("skin", group) == 0)
243 || (strcasecmp ("lines", group) == 0)
244 || (strncasecmp ("widget-", group, 7) == 0));
247 /* --------------------------------------------------------------------------------------------- */
249 static void
250 mc_skin_color_check_bw_mode (mc_skin_t * mc_skin)
252 gchar **groups, **orig_groups;
254 if ( tty_use_colors () && !mc_args__disable_colors)
255 return;
257 orig_groups = groups = mc_config_get_groups (mc_skin->config, NULL);
259 if (groups == NULL)
260 return;
262 for (; *groups != NULL; groups++) {
263 if (mc_skin_color_check_inisection (*groups))
264 mc_config_del_group (mc_skin->config, *groups);
266 g_strfreev (orig_groups);
267 mc_skin_hardcoded_blackwhite_colors (mc_skin);
270 /* --------------------------------------------------------------------------------------------- */
271 /*** public functions ****************************************************************************/
272 /* --------------------------------------------------------------------------------------------- */
274 gboolean
275 mc_skin_color_parse_ini_file (mc_skin_t * mc_skin)
277 gsize items_count;
278 gchar **groups, **orig_groups;
279 gchar **keys, **orig_keys;
280 mc_skin_color_t *mc_skin_color;
282 mc_skin_color_check_bw_mode (mc_skin);
284 orig_groups = groups = mc_config_get_groups (mc_skin->config, &items_count);
286 if (groups == NULL)
287 return FALSE;
289 if (*groups == NULL) {
290 g_strfreev (groups);
291 return FALSE;
294 /* as first, need to set up default colors */
295 mc_skin_color_set_default_for_terminal (mc_skin);
296 mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
297 if (mc_skin_color == NULL)
298 return FALSE;
300 tty_color_set_defaults (mc_skin_color->fgcolor, mc_skin_color->bgcolor);
301 mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
303 for (; *groups != NULL; groups++) {
304 if (!mc_skin_color_check_inisection (*groups))
305 continue;
307 orig_keys = keys = mc_config_get_keys (mc_skin->config, *groups, &items_count);
308 if (keys == NULL)
309 continue;
311 if(*keys == NULL) {
312 g_strfreev (keys);
313 continue;
316 for (; *keys; keys++) {
317 mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
318 if (mc_skin_color != NULL)
319 mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
321 g_strfreev (orig_keys);
323 g_strfreev (orig_groups);
325 mc_skin_color_cache_init ();
326 return TRUE;
329 /* --------------------------------------------------------------------------------------------- */
332 mc_skin_color_get (const gchar * group, const gchar * name)
334 mc_skin_color_t *mc_skin_color;
336 mc_skin_color = mc_skin_color_get_with_defaults (group, name);
338 return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
341 /* --------------------------------------------------------------------------------------------- */