fixed relative symlink operations. Symlink now stay relative
[midnight-commander.git] / lib / skin / colors.c
blobbcb168ab7b9d0ebf4e629beb3d50970c017a7864
1 /*
2 Skins engine.
3 Work with colors
5 Copyright (C) 2009, 2010 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009
9 Egmont Koblinger <egmont@gmail.com>, 2010
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software; you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be
19 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
20 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 MA 02110-1301, USA.
29 #include <config.h>
30 #include <string.h>
32 #include "internal.h"
34 #include "lib/tty/color.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
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 *kname;
92 kname = g_strdup_printf ("%s.%s", group, key);
93 if (kname != NULL)
95 if (g_hash_table_lookup (mc_skin->colors, (gpointer) kname) != NULL)
96 g_hash_table_remove (mc_skin->colors, (gpointer) kname);
98 g_hash_table_insert (mc_skin->colors, (gpointer) kname, (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);
131 if (values == NULL || values[0] == NULL)
133 g_strfreev (values);
134 return NULL;
137 mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
138 if (mc_skin_color == NULL)
140 g_strfreev (values);
141 return NULL;
144 tmp = mc_skin_color_get_with_defaults (group, "_default_");
145 mc_skin_color->fgcolor = (items_count > 0 && values[0][0]) ? g_strstrip (g_strdup (values[0])) :
146 (tmp != NULL) ? g_strdup (tmp->fgcolor) : NULL;
147 mc_skin_color->bgcolor = (items_count > 1 && values[1][0]) ? g_strstrip (g_strdup (values[1])) :
148 (tmp != NULL) ? g_strdup (tmp->bgcolor) : NULL;
149 mc_skin_color->attrs = (items_count > 2 && values[2][0]) ? g_strstrip (g_strdup (values[2])) :
150 (tmp != NULL) ? g_strdup (tmp->attrs) : NULL;
152 g_strfreev (values);
154 mc_skin_color->pair_index =
155 tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor,
156 mc_skin_color->attrs, FALSE);
158 return mc_skin_color;
161 /* --------------------------------------------------------------------------------------------- */
163 static void
164 mc_skin_color_set_default_for_terminal (mc_skin_t * mc_skin)
166 mc_skin_color_t *mc_skin_color;
167 mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
168 if (mc_skin_color != NULL)
170 mc_skin_color->fgcolor = g_strdup ("default");
171 mc_skin_color->bgcolor = g_strdup ("default");
172 mc_skin_color->attrs = NULL;
173 mc_skin_color->pair_index =
174 tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor,
175 mc_skin_color->attrs, FALSE);
176 mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
180 /* --------------------------------------------------------------------------------------------- */
181 static void
182 mc_skin_color_cache_init (void)
184 DEFAULT_COLOR = mc_skin_color_get ("skin", "terminal_default_color");
185 NORMAL_COLOR = mc_skin_color_get ("core", "_default_");
186 MARKED_COLOR = mc_skin_color_get ("core", "marked");
187 SELECTED_COLOR = mc_skin_color_get ("core", "selected");
188 MARKED_SELECTED_COLOR = mc_skin_color_get ("core", "markselect");
189 DISABLED_COLOR = mc_skin_color_get ("core", "disabled");
190 REVERSE_COLOR = mc_skin_color_get ("core", "reverse");
191 HEADER_COLOR = mc_skin_color_get ("core", "header");
192 COMMAND_MARK_COLOR = mc_skin_color_get ("core", "commandlinemark");
194 COLOR_NORMAL = mc_skin_color_get ("dialog", "_default_");
195 COLOR_FOCUS = mc_skin_color_get ("dialog", "dfocus");
196 COLOR_HOT_NORMAL = mc_skin_color_get ("dialog", "dhotnormal");
197 COLOR_HOT_FOCUS = mc_skin_color_get ("dialog", "dhotfocus");
198 COLOR_TITLE = mc_skin_color_get ("dialog", "dtitle");
200 ERROR_COLOR = mc_skin_color_get ("error", "_default_");
201 ERROR_FOCUS = mc_skin_color_get ("error", "errdfocus");
202 ERROR_HOT_NORMAL = mc_skin_color_get ("error", "errdhotnormal");
203 ERROR_HOT_FOCUS = mc_skin_color_get ("error", "errdhotfocus");
204 ERROR_TITLE = mc_skin_color_get ("error", "errdtitle");
206 MENU_ENTRY_COLOR = mc_skin_color_get ("menu", "_default_");
207 MENU_SELECTED_COLOR = mc_skin_color_get ("menu", "menusel");
208 MENU_HOT_COLOR = mc_skin_color_get ("menu", "menuhot");
209 MENU_HOTSEL_COLOR = mc_skin_color_get ("menu", "menuhotsel");
210 MENU_INACTIVE_COLOR = mc_skin_color_get ("menu", "menuinactive");
212 PMENU_ENTRY_COLOR = mc_skin_color_get ("popupmenu", "_default_");
213 PMENU_SELECTED_COLOR = mc_skin_color_get ("popupmenu", "menusel");
214 PMENU_TITLE_COLOR = mc_skin_color_get ("popupmenu", "menutitle");
216 BUTTONBAR_HOTKEY_COLOR = mc_skin_color_get ("buttonbar", "hotkey");
217 BUTTONBAR_BUTTON_COLOR = mc_skin_color_get ("buttonbar", "button");
219 STATUSBAR_COLOR = mc_skin_color_get ("statusbar", "_default_");
221 GAUGE_COLOR = mc_skin_color_get ("core", "gauge");
222 INPUT_COLOR = mc_skin_color_get ("core", "input");
223 INPUT_HISTORY_COLOR = mc_skin_color_get ("core", "inputhistory");
224 COMMAND_HISTORY_COLOR = mc_skin_color_get ("core", "commandhistory");
225 INPUT_MARK_COLOR = mc_skin_color_get ("core", "inputmark");
226 INPUT_UNCHANGED_COLOR = mc_skin_color_get ("core", "inputunchanged");
228 HELP_NORMAL_COLOR = mc_skin_color_get ("help", "_default_");
229 HELP_ITALIC_COLOR = mc_skin_color_get ("help", "helpitalic");
230 HELP_BOLD_COLOR = mc_skin_color_get ("help", "helpbold");
231 HELP_LINK_COLOR = mc_skin_color_get ("help", "helplink");
232 HELP_SLINK_COLOR = mc_skin_color_get ("help", "helpslink");
233 HELP_TITLE_COLOR = mc_skin_color_get ("help", "helptitle");
235 VIEW_BOLD_COLOR = mc_skin_color_get ("viewer", "viewbold");
236 VIEW_UNDERLINED_COLOR = mc_skin_color_get ("viewer", "viewunderline");
237 VIEW_SELECTED_COLOR = mc_skin_color_get ("viewer", "viewselected");
239 EDITOR_NORMAL_COLOR = mc_skin_color_get ("editor", "_default_");
240 EDITOR_BOLD_COLOR = mc_skin_color_get ("editor", "editbold");
241 EDITOR_MARKED_COLOR = mc_skin_color_get ("editor", "editmarked");
242 EDITOR_WHITESPACE_COLOR = mc_skin_color_get ("editor", "editwhitespace");
243 EDITOR_RIGHT_MARGIN_COLOR = mc_skin_color_get ("editor", "editrightmargin");
244 LINE_STATE_COLOR = mc_skin_color_get ("editor", "editlinestate");
246 BOOK_MARK_COLOR = mc_skin_color_get ("editor", "bookmark");
247 BOOK_MARK_FOUND_COLOR = mc_skin_color_get ("editor", "bookmarkfound");
249 DFF_ADD_COLOR = mc_skin_color_get ("diffviewer", "added");
250 DFF_CHG_COLOR = mc_skin_color_get ("diffviewer", "changedline");
251 DFF_CHH_COLOR = mc_skin_color_get ("diffviewer", "changednew");
252 DFF_CHD_COLOR = mc_skin_color_get ("diffviewer", "changed");
253 DFF_DEL_COLOR = mc_skin_color_get ("diffviewer", "removed");
254 DFF_FOLDER_COLOR = mc_skin_color_get ("diffviewer", "folder");
255 DFF_ERROR_COLOR = mc_skin_color_get ("diffviewer", "error");
258 /* --------------------------------------------------------------------------------------------- */
260 static gboolean
261 mc_skin_color_check_inisection (const gchar * group)
263 return !((strcasecmp ("skin", group) == 0)
264 || (strcasecmp ("lines", group) == 0) || (strncasecmp ("widget-", group, 7) == 0));
267 /* --------------------------------------------------------------------------------------------- */
269 static void
270 mc_skin_color_check_bw_mode (mc_skin_t * mc_skin)
272 gchar **groups, **orig_groups;
274 if (tty_use_colors () && !mc_global.args.disable_colors)
275 return;
277 orig_groups = groups = mc_config_get_groups (mc_skin->config, NULL);
279 if (groups == NULL)
280 return;
282 for (; *groups != NULL; groups++)
284 if (mc_skin_color_check_inisection (*groups))
285 mc_config_del_group (mc_skin->config, *groups);
287 g_strfreev (orig_groups);
288 mc_skin_hardcoded_blackwhite_colors (mc_skin);
291 /* --------------------------------------------------------------------------------------------- */
292 /*** public functions ****************************************************************************/
293 /* --------------------------------------------------------------------------------------------- */
295 gboolean
296 mc_skin_color_parse_ini_file (mc_skin_t * mc_skin)
298 gsize items_count;
299 gchar **groups, **orig_groups;
300 gchar **keys, **orig_keys;
301 mc_skin_color_t *mc_skin_color;
303 mc_skin_color_check_bw_mode (mc_skin);
305 orig_groups = groups = mc_config_get_groups (mc_skin->config, &items_count);
306 if (groups == NULL || groups[0] == NULL)
308 g_strfreev (groups);
309 return FALSE;
312 /* as first, need to set up default colors */
313 mc_skin_color_set_default_for_terminal (mc_skin);
314 mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
315 if (mc_skin_color == NULL)
316 return FALSE;
318 tty_color_set_defaults (mc_skin_color->fgcolor, mc_skin_color->bgcolor, mc_skin_color->attrs);
319 mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
321 for (; *groups != NULL; groups++)
323 if (!mc_skin_color_check_inisection (*groups))
324 continue;
326 orig_keys = keys = mc_config_get_keys (mc_skin->config, *groups, &items_count);
327 if (keys == NULL)
328 continue;
330 for (; *keys != NULL; keys++)
332 mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
333 if (mc_skin_color != NULL)
334 mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
336 g_strfreev (orig_keys);
338 g_strfreev (orig_groups);
340 mc_skin_color_cache_init ();
341 return TRUE;
344 /* --------------------------------------------------------------------------------------------- */
347 mc_skin_color_get (const gchar * group, const gchar * name)
349 mc_skin_color_t *mc_skin_color;
351 mc_skin_color = mc_skin_color_get_with_defaults (group, name);
353 return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
356 /* --------------------------------------------------------------------------------------------- */