added new parameter mcview_eof into ini-file
[free-mc.git] / src / skin / colors.c
blobff38735d867f7a3aff86b1cf23df213010140bcc
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 "../src/global.h"
32 #include "../src/args.h"
33 #include "../src/tty/color.h"
34 #include "skin.h"
35 #include "internal.h"
37 /*** global variables ****************************************************************************/
39 int mc_skin_color__cache[MC_SKIN_COLOR_CACHE_COUNT];
41 /*** file scope macro definitions ****************************************************************/
43 /*** file scope type declarations ****************************************************************/
45 /*** file scope variables ************************************************************************/
47 /*** file scope functions ************************************************************************/
49 static mc_skin_color_t *
50 mc_skin_color_get_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
52 gchar key_name[BUF_TINY];
53 mc_skin_color_t *mc_skin_color;
55 if (group == NULL || key == NULL)
56 return NULL;
58 if (mc_skin == NULL)
59 mc_skin = &mc_skin__default;
61 g_snprintf (key_name, sizeof (key_name), "%s.%s", group, key);
62 mc_skin_color = (mc_skin_color_t *) g_hash_table_lookup (mc_skin->colors, (gpointer) key_name);
64 return mc_skin_color;
67 /* --------------------------------------------------------------------------------------------- */
69 #if 0
70 static void
71 mc_skin_color_remove_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
73 gchar key_name[BUF_TINY];
74 if (group == NULL || key == NULL)
75 return;
77 if (mc_skin == NULL)
78 mc_skin = &mc_skin__default;
80 g_snprintf (key_name, sizeof (key_name), "%s.%s", group, key);
81 g_hash_table_remove (mc_skin->colors, (gpointer) key_name);
83 #endif
84 /* --------------------------------------------------------------------------------------------- */
86 static void
87 mc_skin_color_add_to_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key,
88 mc_skin_color_t * mc_skin_color)
90 gchar *key_name;
92 key_name = g_strdup_printf ("%s.%s", group, key);
93 if (key_name == NULL)
94 return;
96 if (g_hash_table_lookup (mc_skin->colors, (gpointer) key_name) != NULL)
97 g_hash_table_remove (mc_skin->colors, (gpointer) key_name);
99 g_hash_table_insert (mc_skin->colors, (gpointer) key_name, (gpointer) mc_skin_color);
102 /* --------------------------------------------------------------------------------------------- */
104 static mc_skin_color_t *
105 mc_skin_color_get_with_defaults (const gchar * group, const gchar * name)
107 mc_skin_color_t *mc_skin_color;
109 mc_skin_color = mc_skin_color_get_from_hash (NULL, group, name);
110 if (mc_skin_color != NULL)
111 return mc_skin_color;
113 mc_skin_color = mc_skin_color_get_from_hash (NULL, group, "_default_");
114 if (mc_skin_color != NULL)
115 return mc_skin_color;
117 mc_skin_color = mc_skin_color_get_from_hash (NULL, "core", "_default_");
118 return mc_skin_color;
121 /* --------------------------------------------------------------------------------------------- */
123 static mc_skin_color_t *
124 mc_skin_color_get_from_ini_file (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
126 gsize items_count;
127 gchar **values;
128 mc_skin_color_t *mc_skin_color, *tmp;
130 values = mc_config_get_string_list (mc_skin->config, group, key, &items_count);
132 if (values == NULL || *values == NULL) {
133 if (values != NULL)
134 g_strfreev (values);
135 return NULL;
137 mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
138 if (mc_skin_color == NULL) {
139 g_strfreev (values);
140 return NULL;
143 switch (items_count) {
144 case 0:
145 tmp = mc_skin_color_get_with_defaults (group, "_default_");
146 if (tmp) {
147 mc_skin_color->fgcolor = g_strdup (tmp->fgcolor);
148 mc_skin_color->bgcolor = g_strdup (tmp->bgcolor);
149 } else {
150 g_strfreev (values);
151 g_free (mc_skin_color);
152 return NULL;
154 break;
155 case 1:
156 mc_skin_color->fgcolor = (values[0]) ? g_strstrip (g_strdup (values[0])) : NULL;
157 tmp = mc_skin_color_get_with_defaults (group, "_default_");
158 mc_skin_color->bgcolor = (tmp != NULL) ? g_strdup (tmp->bgcolor) : NULL;
159 break;
160 case 2:
161 mc_skin_color->fgcolor = (values[0]) ? g_strstrip (g_strdup (values[0])) : NULL;
162 mc_skin_color->bgcolor = (values[1]) ? g_strstrip (g_strdup (values[1])) : NULL;
163 break;
165 g_strfreev (values);
167 mc_skin_color->pair_index =
168 tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
170 return mc_skin_color;
173 /* --------------------------------------------------------------------------------------------- */
175 static void
176 mc_skin_color_set_default_for_terminal (mc_skin_t * mc_skin)
178 mc_skin_color_t *mc_skin_color;
179 mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
180 if (mc_skin_color != NULL) {
181 mc_skin_color->fgcolor = g_strdup ("default");
182 mc_skin_color->bgcolor = g_strdup ("default");
183 mc_skin_color->pair_index =
184 tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
185 mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
189 /* --------------------------------------------------------------------------------------------- */
190 static void
191 mc_skin_color_cache_init (void)
193 DEFAULT_COLOR = mc_skin_color_get ("skin", "terminal_default_color");
194 NORMAL_COLOR = mc_skin_color_get ("core", "_default_");
195 MARKED_COLOR = mc_skin_color_get ("core", "marked");
196 SELECTED_COLOR = mc_skin_color_get ("core", "selected");
197 MARKED_SELECTED_COLOR = mc_skin_color_get ("core", "markselect");
198 REVERSE_COLOR = mc_skin_color_get ("core", "reverse");
200 COLOR_NORMAL = mc_skin_color_get ("dialog", "_default_");
201 COLOR_FOCUS = mc_skin_color_get ("dialog", "dfocus");
202 COLOR_HOT_NORMAL = mc_skin_color_get ("dialog", "dhotnormal");
203 COLOR_HOT_FOCUS = mc_skin_color_get ("dialog", "dhotfocus");
205 ERROR_COLOR = mc_skin_color_get ("error", "_default_");
206 ERROR_HOT_NORMAL = mc_skin_color_get ("error", "errdhotnormal");
207 ERROR_HOT_FOCUS = mc_skin_color_get ("error", "errdhotfocus");
209 MENU_ENTRY_COLOR = mc_skin_color_get ("menu", "_default_");
210 MENU_SELECTED_COLOR = mc_skin_color_get ("menu", "menusel");
211 MENU_HOT_COLOR = mc_skin_color_get ("menu", "menuhot");
212 MENU_HOTSEL_COLOR = mc_skin_color_get ("menu", "menuhotsel");
214 GAUGE_COLOR = mc_skin_color_get ("core", "gauge");
215 INPUT_COLOR = mc_skin_color_get ("core", "input");
217 HELP_NORMAL_COLOR = mc_skin_color_get ("help", "_default_");
218 HELP_ITALIC_COLOR = mc_skin_color_get ("help", "helpitalic");
219 HELP_BOLD_COLOR = mc_skin_color_get ("help", "helpbold");
220 HELP_LINK_COLOR = mc_skin_color_get ("help", "helplink");
221 HELP_SLINK_COLOR = mc_skin_color_get ("help", "helpslink");
223 VIEW_UNDERLINED_COLOR = mc_skin_color_get ("viewer", "viewunderline");
225 EDITOR_NORMAL_COLOR = mc_skin_color_get ("editor", "_default_");
226 EDITOR_BOLD_COLOR = mc_skin_color_get ("editor", "editbold");
227 EDITOR_MARKED_COLOR = mc_skin_color_get ("editor", "editmarked");
228 EDITOR_WHITESPACE_COLOR = mc_skin_color_get ("editor", "editwhitespace");
229 EDITOR_RIGHT_MARGIN_COLOR = mc_skin_color_get ("editor", "editrightmargin");
230 LINE_STATE_COLOR = mc_skin_color_get ("editor", "linestate");
232 BOOK_MARK_COLOR = mc_skin_color_get ("editor", "bookmark");
233 BOOK_MARK_FOUND_COLOR = mc_skin_color_get ("editor", "bookmarkfound");
234 BUTTONBAR_HOTKEY_COLOR = mc_skin_color_get ("buttonbar", "hotkey");
235 BUTTONBAR_BUTTON_COLOR = mc_skin_color_get ("buttonbar", "button");
240 /* --------------------------------------------------------------------------------------------- */
242 static gboolean
243 mc_skin_color_check_inisection (const gchar * group)
245 return !((strcasecmp ("skin", group) == 0)
246 || (strcasecmp ("lines", group) == 0));
249 /* --------------------------------------------------------------------------------------------- */
251 static void
252 mc_skin_color_check_bw_mode (mc_skin_t * mc_skin)
254 gchar **groups, **orig_groups;
256 if (!mc_args__disable_colors)
257 return;
259 orig_groups = groups = mc_config_get_groups (mc_skin->config, NULL);
261 if (groups == NULL)
262 return;
264 for (; *groups != NULL; groups++) {
265 if (mc_skin_color_check_inisection (*groups))
266 mc_config_del_group (mc_skin->config, *groups);
268 g_strfreev (orig_groups);
269 mc_skin_hardcoded_blackwhite_colors (mc_skin);
272 /* --------------------------------------------------------------------------------------------- */
273 /*** public functions ****************************************************************************/
274 /* --------------------------------------------------------------------------------------------- */
276 gboolean
277 mc_skin_color_parse_ini_file (mc_skin_t * mc_skin)
279 gsize items_count;
280 gchar **groups, **orig_groups;
281 gchar **keys, **orig_keys;
282 mc_skin_color_t *mc_skin_color;
284 mc_skin_color_check_bw_mode (mc_skin);
286 orig_groups = groups = mc_config_get_groups (mc_skin->config, &items_count);
288 if (groups == NULL)
289 return FALSE;
291 if (*groups == NULL) {
292 g_strfreev (groups);
293 return FALSE;
296 /* as first, need to set up default colors */
297 mc_skin_color_set_default_for_terminal (mc_skin);
298 mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
299 if (mc_skin_color == NULL)
300 return FALSE;
302 tty_color_set_defaults (mc_skin_color->fgcolor, mc_skin_color->bgcolor);
303 mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
305 for (; *groups != NULL; groups++) {
306 if (!mc_skin_color_check_inisection (*groups))
307 continue;
309 orig_keys = keys = mc_config_get_keys (mc_skin->config, *groups, &items_count);
310 if (keys == NULL)
311 continue;
313 if(*keys == NULL) {
314 g_strfreev (keys);
315 continue;
318 for (; *keys; keys++) {
319 mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
320 if (mc_skin_color != NULL)
321 mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
323 g_strfreev (orig_keys);
325 g_strfreev (orig_groups);
327 mc_skin_color_cache_init ();
328 return TRUE;
331 /* --------------------------------------------------------------------------------------------- */
334 mc_skin_color_get (const gchar * group, const gchar * name)
336 mc_skin_color_t *mc_skin_color;
338 mc_skin_color = mc_skin_color_get_with_defaults (group, name);
340 return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
343 /* --------------------------------------------------------------------------------------------- */