5 Copyright (C) 2009, 2010, 2011
6 The Free Software Foundation, Inc.
9 Slava Zanko <slavazanko@gmail.com>, 2009
10 Egmont Koblinger <egmont@gmail.com>, 2010
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "lib/tty/color.h"
35 /*** global variables ****************************************************************************/
37 int mc_skin_color__cache
[MC_SKIN_COLOR_CACHE_COUNT
];
39 /*** file scope macro definitions ****************************************************************/
41 /*** file scope type declarations ****************************************************************/
43 /*** file scope variables ************************************************************************/
45 /*** file scope functions ************************************************************************/
47 static mc_skin_color_t
*
48 mc_skin_color_get_from_hash (mc_skin_t
* mc_skin
, const gchar
* group
, const gchar
* key
)
50 gchar kname
[BUF_TINY
];
51 mc_skin_color_t
*mc_skin_color
;
53 if (group
== NULL
|| key
== NULL
)
57 mc_skin
= &mc_skin__default
;
59 g_snprintf (kname
, sizeof (kname
), "%s.%s", group
, key
);
60 mc_skin_color
= (mc_skin_color_t
*) g_hash_table_lookup (mc_skin
->colors
, (gpointer
) kname
);
65 /* --------------------------------------------------------------------------------------------- */
69 mc_skin_color_remove_from_hash (mc_skin_t
* mc_skin
, const gchar
* group
, const gchar
* key
)
71 gchar kname
[BUF_TINY
];
72 if (group
== NULL
|| key
== NULL
)
76 mc_skin
= &mc_skin__default
;
78 g_snprintf (kname
, sizeof (kname
), "%s.%s", group
, key
);
79 g_hash_table_remove (mc_skin
->colors
, (gpointer
) kname
);
83 /* --------------------------------------------------------------------------------------------- */
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
)
91 kname
= g_strdup_printf ("%s.%s", group
, key
);
94 if (g_hash_table_lookup (mc_skin
->colors
, (gpointer
) kname
) != NULL
)
95 g_hash_table_remove (mc_skin
->colors
, (gpointer
) kname
);
97 g_hash_table_insert (mc_skin
->colors
, (gpointer
) kname
, (gpointer
) mc_skin_color
);
101 /* --------------------------------------------------------------------------------------------- */
103 static mc_skin_color_t
*
104 mc_skin_color_get_with_defaults (const gchar
* group
, const gchar
* name
)
106 mc_skin_color_t
*mc_skin_color
;
108 mc_skin_color
= mc_skin_color_get_from_hash (NULL
, group
, name
);
109 if (mc_skin_color
!= NULL
)
110 return mc_skin_color
;
112 mc_skin_color
= mc_skin_color_get_from_hash (NULL
, group
, "_default_");
113 if (mc_skin_color
!= NULL
)
114 return mc_skin_color
;
116 mc_skin_color
= mc_skin_color_get_from_hash (NULL
, "core", "_default_");
117 return mc_skin_color
;
120 /* --------------------------------------------------------------------------------------------- */
122 static mc_skin_color_t
*
123 mc_skin_color_get_from_ini_file (mc_skin_t
* mc_skin
, const gchar
* group
, const gchar
* key
)
127 mc_skin_color_t
*mc_skin_color
, *tmp
;
129 values
= mc_config_get_string_list (mc_skin
->config
, group
, key
, &items_count
);
130 if (values
== NULL
|| values
[0] == NULL
)
136 mc_skin_color
= g_try_new0 (mc_skin_color_t
, 1);
137 if (mc_skin_color
== NULL
)
143 tmp
= mc_skin_color_get_with_defaults (group
, "_default_");
144 mc_skin_color
->fgcolor
= (items_count
> 0 && values
[0][0]) ? g_strstrip (g_strdup (values
[0])) :
145 (tmp
!= NULL
) ? g_strdup (tmp
->fgcolor
) : NULL
;
146 mc_skin_color
->bgcolor
= (items_count
> 1 && values
[1][0]) ? g_strstrip (g_strdup (values
[1])) :
147 (tmp
!= NULL
) ? g_strdup (tmp
->bgcolor
) : NULL
;
148 mc_skin_color
->attrs
= (items_count
> 2 && values
[2][0]) ? g_strstrip (g_strdup (values
[2])) :
149 (tmp
!= NULL
) ? g_strdup (tmp
->attrs
) : NULL
;
153 mc_skin_color
->pair_index
=
154 tty_try_alloc_color_pair2 (mc_skin_color
->fgcolor
, mc_skin_color
->bgcolor
,
155 mc_skin_color
->attrs
, FALSE
);
157 return mc_skin_color
;
160 /* --------------------------------------------------------------------------------------------- */
163 mc_skin_color_set_default_for_terminal (mc_skin_t
* mc_skin
)
165 mc_skin_color_t
*mc_skin_color
;
166 mc_skin_color
= g_try_new0 (mc_skin_color_t
, 1);
167 if (mc_skin_color
!= NULL
)
169 mc_skin_color
->fgcolor
= g_strdup ("default");
170 mc_skin_color
->bgcolor
= g_strdup ("default");
171 mc_skin_color
->attrs
= NULL
;
172 mc_skin_color
->pair_index
=
173 tty_try_alloc_color_pair2 (mc_skin_color
->fgcolor
, mc_skin_color
->bgcolor
,
174 mc_skin_color
->attrs
, FALSE
);
175 mc_skin_color_add_to_hash (mc_skin
, "skin", "terminal_default_color", mc_skin_color
);
179 /* --------------------------------------------------------------------------------------------- */
181 mc_skin_color_cache_init (void)
183 DEFAULT_COLOR
= mc_skin_color_get ("skin", "terminal_default_color");
184 NORMAL_COLOR
= mc_skin_color_get ("core", "_default_");
185 MARKED_COLOR
= mc_skin_color_get ("core", "marked");
186 SELECTED_COLOR
= mc_skin_color_get ("core", "selected");
187 MARKED_SELECTED_COLOR
= mc_skin_color_get ("core", "markselect");
188 DISABLED_COLOR
= mc_skin_color_get ("core", "disabled");
189 REVERSE_COLOR
= mc_skin_color_get ("core", "reverse");
190 HEADER_COLOR
= mc_skin_color_get ("core", "header");
191 COMMAND_MARK_COLOR
= mc_skin_color_get ("core", "commandlinemark");
193 COLOR_NORMAL
= mc_skin_color_get ("dialog", "_default_");
194 COLOR_FOCUS
= mc_skin_color_get ("dialog", "dfocus");
195 COLOR_HOT_NORMAL
= mc_skin_color_get ("dialog", "dhotnormal");
196 COLOR_HOT_FOCUS
= mc_skin_color_get ("dialog", "dhotfocus");
197 COLOR_TITLE
= mc_skin_color_get ("dialog", "dtitle");
199 ERROR_COLOR
= mc_skin_color_get ("error", "_default_");
200 ERROR_FOCUS
= mc_skin_color_get ("error", "errdfocus");
201 ERROR_HOT_NORMAL
= mc_skin_color_get ("error", "errdhotnormal");
202 ERROR_HOT_FOCUS
= mc_skin_color_get ("error", "errdhotfocus");
203 ERROR_TITLE
= mc_skin_color_get ("error", "errdtitle");
205 MENU_ENTRY_COLOR
= mc_skin_color_get ("menu", "_default_");
206 MENU_SELECTED_COLOR
= mc_skin_color_get ("menu", "menusel");
207 MENU_HOT_COLOR
= mc_skin_color_get ("menu", "menuhot");
208 MENU_HOTSEL_COLOR
= mc_skin_color_get ("menu", "menuhotsel");
209 MENU_INACTIVE_COLOR
= mc_skin_color_get ("menu", "menuinactive");
211 PMENU_ENTRY_COLOR
= mc_skin_color_get ("popupmenu", "_default_");
212 PMENU_SELECTED_COLOR
= mc_skin_color_get ("popupmenu", "menusel");
213 PMENU_TITLE_COLOR
= mc_skin_color_get ("popupmenu", "menutitle");
215 BUTTONBAR_HOTKEY_COLOR
= mc_skin_color_get ("buttonbar", "hotkey");
216 BUTTONBAR_BUTTON_COLOR
= mc_skin_color_get ("buttonbar", "button");
218 STATUSBAR_COLOR
= mc_skin_color_get ("statusbar", "_default_");
220 GAUGE_COLOR
= mc_skin_color_get ("core", "gauge");
221 INPUT_COLOR
= mc_skin_color_get ("core", "input");
222 INPUT_HISTORY_COLOR
= mc_skin_color_get ("core", "inputhistory");
223 COMMAND_HISTORY_COLOR
= mc_skin_color_get ("core", "commandhistory");
224 INPUT_MARK_COLOR
= mc_skin_color_get ("core", "inputmark");
225 INPUT_UNCHANGED_COLOR
= mc_skin_color_get ("core", "inputunchanged");
227 HELP_NORMAL_COLOR
= mc_skin_color_get ("help", "_default_");
228 HELP_ITALIC_COLOR
= mc_skin_color_get ("help", "helpitalic");
229 HELP_BOLD_COLOR
= mc_skin_color_get ("help", "helpbold");
230 HELP_LINK_COLOR
= mc_skin_color_get ("help", "helplink");
231 HELP_SLINK_COLOR
= mc_skin_color_get ("help", "helpslink");
232 HELP_TITLE_COLOR
= mc_skin_color_get ("help", "helptitle");
234 VIEW_BOLD_COLOR
= mc_skin_color_get ("viewer", "viewbold");
235 VIEW_UNDERLINED_COLOR
= mc_skin_color_get ("viewer", "viewunderline");
236 VIEW_SELECTED_COLOR
= mc_skin_color_get ("viewer", "viewselected");
238 EDITOR_NORMAL_COLOR
= mc_skin_color_get ("editor", "_default_");
239 EDITOR_BOLD_COLOR
= mc_skin_color_get ("editor", "editbold");
240 EDITOR_MARKED_COLOR
= mc_skin_color_get ("editor", "editmarked");
241 EDITOR_WHITESPACE_COLOR
= mc_skin_color_get ("editor", "editwhitespace");
242 EDITOR_RIGHT_MARGIN_COLOR
= mc_skin_color_get ("editor", "editrightmargin");
243 LINE_STATE_COLOR
= mc_skin_color_get ("editor", "editlinestate");
245 BOOK_MARK_COLOR
= mc_skin_color_get ("editor", "bookmark");
246 BOOK_MARK_FOUND_COLOR
= mc_skin_color_get ("editor", "bookmarkfound");
248 DFF_ADD_COLOR
= mc_skin_color_get ("diffviewer", "added");
249 DFF_CHG_COLOR
= mc_skin_color_get ("diffviewer", "changedline");
250 DFF_CHH_COLOR
= mc_skin_color_get ("diffviewer", "changednew");
251 DFF_CHD_COLOR
= mc_skin_color_get ("diffviewer", "changed");
252 DFF_DEL_COLOR
= mc_skin_color_get ("diffviewer", "removed");
253 DFF_FOLDER_COLOR
= mc_skin_color_get ("diffviewer", "folder");
254 DFF_ERROR_COLOR
= mc_skin_color_get ("diffviewer", "error");
257 /* --------------------------------------------------------------------------------------------- */
260 mc_skin_color_check_inisection (const gchar
* group
)
262 return !((strcasecmp ("skin", group
) == 0)
263 || (strcasecmp ("lines", group
) == 0) || (strncasecmp ("widget-", group
, 7) == 0));
266 /* --------------------------------------------------------------------------------------------- */
269 mc_skin_color_check_bw_mode (mc_skin_t
* mc_skin
)
271 gchar
**groups
, **orig_groups
;
273 if (tty_use_colors () && !mc_global
.tty
.disable_colors
)
276 orig_groups
= groups
= mc_config_get_groups (mc_skin
->config
, NULL
);
281 for (; *groups
!= NULL
; groups
++)
283 if (mc_skin_color_check_inisection (*groups
))
284 mc_config_del_group (mc_skin
->config
, *groups
);
286 g_strfreev (orig_groups
);
287 mc_skin_hardcoded_blackwhite_colors (mc_skin
);
290 /* --------------------------------------------------------------------------------------------- */
291 /*** public functions ****************************************************************************/
292 /* --------------------------------------------------------------------------------------------- */
295 mc_skin_color_parse_ini_file (mc_skin_t
* mc_skin
)
298 gchar
**groups
, **orig_groups
;
299 gchar
**keys
, **orig_keys
;
300 mc_skin_color_t
*mc_skin_color
;
302 mc_skin_color_check_bw_mode (mc_skin
);
304 orig_groups
= groups
= mc_config_get_groups (mc_skin
->config
, &items_count
);
305 if (groups
== NULL
|| groups
[0] == NULL
)
311 /* as first, need to set up default colors */
312 mc_skin_color_set_default_for_terminal (mc_skin
);
313 mc_skin_color
= mc_skin_color_get_from_ini_file (mc_skin
, "core", "_default_");
314 if (mc_skin_color
== NULL
)
317 tty_color_set_defaults (mc_skin_color
->fgcolor
, mc_skin_color
->bgcolor
, mc_skin_color
->attrs
);
318 mc_skin_color_add_to_hash (mc_skin
, "core", "_default_", mc_skin_color
);
320 for (; *groups
!= NULL
; groups
++)
322 if (!mc_skin_color_check_inisection (*groups
))
325 orig_keys
= keys
= mc_config_get_keys (mc_skin
->config
, *groups
, &items_count
);
329 for (; *keys
!= NULL
; keys
++)
331 mc_skin_color
= mc_skin_color_get_from_ini_file (mc_skin
, *groups
, *keys
);
332 if (mc_skin_color
!= NULL
)
333 mc_skin_color_add_to_hash (mc_skin
, *groups
, *keys
, mc_skin_color
);
335 g_strfreev (orig_keys
);
337 g_strfreev (orig_groups
);
339 mc_skin_color_cache_init ();
343 /* --------------------------------------------------------------------------------------------- */
346 mc_skin_color_get (const gchar
* group
, const gchar
* name
)
348 mc_skin_color_t
*mc_skin_color
;
350 mc_skin_color
= mc_skin_color_get_with_defaults (group
, name
);
352 return (mc_skin_color
!= NULL
) ? mc_skin_color
->pair_index
: 0;
355 /* --------------------------------------------------------------------------------------------- */