This source file includes following definitions.
- mc_skin_color_get_from_hash
- mc_skin_color_remove_from_hash
- mc_skin_color_add_to_hash
- mc_skin_color_get_with_defaults
- mc_skin_color_look_up_alias
- mc_skin_color_get_from_ini_file
- mc_skin_color_set_default_for_terminal
- mc_skin_color_cache_init
- mc_skin_color_check_inisection
- mc_skin_color_check_bw_mode
- mc_skin_color_parse_ini_file
- mc_skin_color_get
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 #include <config.h>
30 #include <string.h>
31
32 #include "internal.h"
33
34 #include "lib/tty/color.h"
35
36
37
38 int mc_skin_color__cache[MC_SKIN_COLOR_CACHE_COUNT];
39
40
41
42
43
44
45
46
47
48
49
50
51
52 static tty_color_pair_t *
53 mc_skin_color_get_from_hash (mc_skin_t *mc_skin, const gchar *group, const gchar *key)
54 {
55 gchar kname[BUF_TINY];
56 tty_color_pair_t *mc_skin_color;
57
58 if (group == NULL || key == NULL)
59 return NULL;
60
61 if (mc_skin == NULL)
62 mc_skin = &mc_skin__default;
63
64 g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
65 mc_skin_color = (tty_color_pair_t *) g_hash_table_lookup (mc_skin->colors, (gpointer) kname);
66
67 return mc_skin_color;
68 }
69
70
71
72 #if 0
73 static void
74 mc_skin_color_remove_from_hash (mc_skin_t *mc_skin, const gchar *group, const gchar *key)
75 {
76 gchar kname[BUF_TINY];
77 if (group == NULL || key == NULL)
78 return;
79
80 if (mc_skin == NULL)
81 mc_skin = &mc_skin__default;
82
83 g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
84 g_hash_table_remove (mc_skin->colors, (gpointer) kname);
85 }
86 #endif
87
88
89
90 static void
91 mc_skin_color_add_to_hash (mc_skin_t *mc_skin, const gchar *group, const gchar *key,
92 tty_color_pair_t *mc_skin_color)
93 {
94 gchar *kname;
95
96 kname = g_strdup_printf ("%s.%s", group, key);
97 if (kname != NULL)
98 {
99 if (g_hash_table_lookup (mc_skin->colors, (gpointer) kname) != NULL)
100 g_hash_table_remove (mc_skin->colors, (gpointer) kname);
101
102 g_hash_table_insert (mc_skin->colors, (gpointer) kname, (gpointer) mc_skin_color);
103 }
104 }
105
106
107
108 static tty_color_pair_t *
109 mc_skin_color_get_with_defaults (const gchar *group, const gchar *name)
110 {
111 tty_color_pair_t *mc_skin_color;
112
113 mc_skin_color = mc_skin_color_get_from_hash (NULL, group, name);
114 if (mc_skin_color != NULL)
115 return mc_skin_color;
116
117 mc_skin_color = mc_skin_color_get_from_hash (NULL, group, "_default_");
118 if (mc_skin_color != NULL)
119 return mc_skin_color;
120
121 mc_skin_color = mc_skin_color_get_from_hash (NULL, "core", "_default_");
122 return mc_skin_color;
123 }
124
125
126
127
128
129 static gchar *
130 mc_skin_color_look_up_alias (mc_skin_t *mc_skin, gchar *str)
131 {
132 gchar *orig, *str2;
133 int hop = 0;
134
135 orig = g_strdup (str);
136 str2 = g_strdup (str);
137
138 while (TRUE)
139 {
140 gchar **values;
141 gsize items_count;
142
143 values = mc_config_get_string_list (mc_skin->config, "aliases", str, &items_count);
144 if (items_count != 1)
145 {
146
147 g_strfreev (values);
148 g_free (str2);
149 g_free (orig);
150 return str;
151 }
152
153 g_free (str);
154 str = g_strdup (values[0]);
155 g_strfreev (values);
156
157
158 if (hop++ % 2 != 0)
159 {
160 values = mc_config_get_string_list (mc_skin->config, "aliases", str2, &items_count);
161 g_assert (items_count == 1);
162 g_free (str2);
163 str2 = g_strdup (values[0]);
164 g_strfreev (values);
165
166 if (strcmp (str, str2) == 0)
167 {
168
169 fprintf (stderr,
170 "Loop detected while trying to resolve alias \"%s\" in skin \"%s\"\n",
171 orig, mc_skin->name);
172 g_free (str);
173 g_free (str2);
174 return orig;
175 }
176 }
177 }
178 }
179
180
181
182 static tty_color_pair_t *
183 mc_skin_color_get_from_ini_file (mc_skin_t *mc_skin, const gchar *group, const gchar *key)
184 {
185 gsize items_count;
186 gchar **values;
187 tty_color_pair_t *mc_skin_color, *tmp;
188
189 values = mc_config_get_string_list (mc_skin->config, group, key, &items_count);
190 if (values == NULL || values[0] == NULL)
191 {
192 g_strfreev (values);
193 return NULL;
194 }
195
196 mc_skin_color = g_try_new0 (tty_color_pair_t, 1);
197 if (mc_skin_color == NULL)
198 {
199 g_strfreev (values);
200 return NULL;
201 }
202
203 tmp = mc_skin_color_get_with_defaults (group, "_default_");
204 mc_skin_color->fg = (items_count > 0 && values[0][0]) ?
205 mc_skin_color_look_up_alias (mc_skin, g_strstrip (g_strdup (values[0]))) :
206 (tmp != NULL) ? g_strdup (tmp->fg) : NULL;
207 mc_skin_color->bg = (items_count > 1 && values[1][0]) ?
208 mc_skin_color_look_up_alias (mc_skin, g_strstrip (g_strdup (values[1]))) :
209 (tmp != NULL) ? g_strdup (tmp->bg) : NULL;
210 mc_skin_color->attrs = (items_count > 2 && values[2][0]) ?
211 mc_skin_color_look_up_alias (mc_skin, g_strstrip (g_strdup (values[2]))) :
212 (tmp != NULL) ? g_strdup (tmp->attrs) : NULL;
213
214 g_strfreev (values);
215
216 mc_skin_color->pair_index = tty_try_alloc_color_pair (mc_skin_color, FALSE);
217
218 return mc_skin_color;
219 }
220
221
222
223 static void
224 mc_skin_color_set_default_for_terminal (mc_skin_t *mc_skin)
225 {
226 tty_color_pair_t *mc_skin_color;
227
228 mc_skin_color = g_try_new0 (tty_color_pair_t, 1);
229 if (mc_skin_color != NULL)
230 {
231 mc_skin_color->fg = g_strdup ("default");
232 mc_skin_color->bg = g_strdup ("default");
233 mc_skin_color->attrs = NULL;
234 mc_skin_color->pair_index = tty_try_alloc_color_pair (mc_skin_color, FALSE);
235 mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
236 }
237 }
238
239
240
241 static void
242 mc_skin_color_cache_init (void)
243 {
244 DEFAULT_COLOR = mc_skin_color_get ("skin", "terminal_default_color");
245 NORMAL_COLOR = mc_skin_color_get ("core", "_default_");
246 MARKED_COLOR = mc_skin_color_get ("core", "marked");
247 SELECTED_COLOR = mc_skin_color_get ("core", "selected");
248 MARKED_SELECTED_COLOR = mc_skin_color_get ("core", "markselect");
249 DISABLED_COLOR = mc_skin_color_get ("core", "disabled");
250 REVERSE_COLOR = mc_skin_color_get ("core", "reverse");
251 HEADER_COLOR = mc_skin_color_get ("core", "header");
252 COMMAND_MARK_COLOR = mc_skin_color_get ("core", "commandlinemark");
253 SHADOW_COLOR = mc_skin_color_get ("core", "shadow");
254
255 COLOR_NORMAL = mc_skin_color_get ("dialog", "_default_");
256 COLOR_FOCUS = mc_skin_color_get ("dialog", "dfocus");
257 COLOR_HOT_NORMAL = mc_skin_color_get ("dialog", "dhotnormal");
258 COLOR_HOT_FOCUS = mc_skin_color_get ("dialog", "dhotfocus");
259 COLOR_TITLE = mc_skin_color_get ("dialog", "dtitle");
260
261 ERROR_COLOR = mc_skin_color_get ("error", "_default_");
262 ERROR_FOCUS = mc_skin_color_get ("error", "errdfocus");
263 ERROR_HOT_NORMAL = mc_skin_color_get ("error", "errdhotnormal");
264 ERROR_HOT_FOCUS = mc_skin_color_get ("error", "errdhotfocus");
265 ERROR_TITLE = mc_skin_color_get ("error", "errdtitle");
266
267 MENU_ENTRY_COLOR = mc_skin_color_get ("menu", "_default_");
268 MENU_SELECTED_COLOR = mc_skin_color_get ("menu", "menusel");
269 MENU_HOT_COLOR = mc_skin_color_get ("menu", "menuhot");
270 MENU_HOTSEL_COLOR = mc_skin_color_get ("menu", "menuhotsel");
271 MENU_INACTIVE_COLOR = mc_skin_color_get ("menu", "menuinactive");
272
273 PMENU_ENTRY_COLOR = mc_skin_color_get ("popupmenu", "_default_");
274 PMENU_SELECTED_COLOR = mc_skin_color_get ("popupmenu", "menusel");
275 PMENU_TITLE_COLOR = mc_skin_color_get ("popupmenu", "menutitle");
276
277 BUTTONBAR_HOTKEY_COLOR = mc_skin_color_get ("buttonbar", "hotkey");
278 BUTTONBAR_BUTTON_COLOR = mc_skin_color_get ("buttonbar", "button");
279
280 STATUSBAR_COLOR = mc_skin_color_get ("statusbar", "_default_");
281
282 GAUGE_COLOR = mc_skin_color_get ("core", "gauge");
283 INPUT_COLOR = mc_skin_color_get ("core", "input");
284 INPUT_HISTORY_COLOR = mc_skin_color_get ("core", "inputhistory");
285 COMMAND_HISTORY_COLOR = mc_skin_color_get ("core", "commandhistory");
286 INPUT_MARK_COLOR = mc_skin_color_get ("core", "inputmark");
287 INPUT_UNCHANGED_COLOR = mc_skin_color_get ("core", "inputunchanged");
288
289 HELP_NORMAL_COLOR = mc_skin_color_get ("help", "_default_");
290 HELP_ITALIC_COLOR = mc_skin_color_get ("help", "helpitalic");
291 HELP_BOLD_COLOR = mc_skin_color_get ("help", "helpbold");
292 HELP_LINK_COLOR = mc_skin_color_get ("help", "helplink");
293 HELP_SLINK_COLOR = mc_skin_color_get ("help", "helpslink");
294 HELP_TITLE_COLOR = mc_skin_color_get ("help", "helptitle");
295
296 VIEW_NORMAL_COLOR = mc_skin_color_get ("viewer", "_default_");
297 VIEW_BOLD_COLOR = mc_skin_color_get ("viewer", "viewbold");
298 VIEW_UNDERLINED_COLOR = mc_skin_color_get ("viewer", "viewunderline");
299 VIEW_SELECTED_COLOR = mc_skin_color_get ("viewer", "viewselected");
300
301 EDITOR_NORMAL_COLOR = mc_skin_color_get ("editor", "_default_");
302 EDITOR_BOLD_COLOR = mc_skin_color_get ("editor", "editbold");
303 EDITOR_MARKED_COLOR = mc_skin_color_get ("editor", "editmarked");
304 EDITOR_WHITESPACE_COLOR = mc_skin_color_get ("editor", "editwhitespace");
305 EDITOR_NONPRINTABLE_COLOR = mc_skin_color_get ("editor", "editnonprintable");
306 EDITOR_RIGHT_MARGIN_COLOR = mc_skin_color_get ("editor", "editrightmargin");
307 LINE_STATE_COLOR = mc_skin_color_get ("editor", "editlinestate");
308 EDITOR_BACKGROUND = mc_skin_color_get ("editor", "editbg");
309 EDITOR_FRAME = mc_skin_color_get ("editor", "editframe");
310 EDITOR_FRAME_ACTIVE = mc_skin_color_get ("editor", "editframeactive");
311 EDITOR_FRAME_DRAG = mc_skin_color_get ("editor", "editframedrag");
312
313 BOOK_MARK_COLOR = mc_skin_color_get ("editor", "bookmark");
314 BOOK_MARK_FOUND_COLOR = mc_skin_color_get ("editor", "bookmarkfound");
315
316 DFF_ADD_COLOR = mc_skin_color_get ("diffviewer", "added");
317 DFF_CHG_COLOR = mc_skin_color_get ("diffviewer", "changedline");
318 DFF_CHH_COLOR = mc_skin_color_get ("diffviewer", "changednew");
319 DFF_CHD_COLOR = mc_skin_color_get ("diffviewer", "changed");
320 DFF_DEL_COLOR = mc_skin_color_get ("diffviewer", "removed");
321 DFF_ERROR_COLOR = mc_skin_color_get ("diffviewer", "error");
322 }
323
324
325
326 static gboolean
327 mc_skin_color_check_inisection (const gchar *group)
328 {
329 return !((strcasecmp ("skin", group) == 0) || (strcasecmp ("aliases", group) == 0)
330 || (strcasecmp ("lines", group) == 0) || (strncasecmp ("widget-", group, 7) == 0));
331 }
332
333
334
335 static void
336 mc_skin_color_check_bw_mode (mc_skin_t *mc_skin)
337 {
338 gchar **groups, **orig_groups;
339
340 if (tty_use_colors () && !mc_global.tty.disable_colors)
341 return;
342
343 orig_groups = mc_config_get_groups (mc_skin->config, NULL);
344
345 for (groups = orig_groups; *groups != NULL; groups++)
346 if (mc_skin_color_check_inisection (*groups))
347 mc_config_del_group (mc_skin->config, *groups);
348
349 g_strfreev (orig_groups);
350
351 mc_skin_hardcoded_blackwhite_colors (mc_skin);
352 }
353
354
355
356
357
358 gboolean
359 mc_skin_color_parse_ini_file (mc_skin_t *mc_skin)
360 {
361 gboolean ret = FALSE;
362 gsize items_count;
363 gchar **groups, **orig_groups;
364 tty_color_pair_t *mc_skin_color;
365
366 mc_skin_color_check_bw_mode (mc_skin);
367
368 orig_groups = mc_config_get_groups (mc_skin->config, &items_count);
369 if (*orig_groups == NULL)
370 goto ret;
371
372
373 mc_skin_color_set_default_for_terminal (mc_skin);
374 mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
375 if (mc_skin_color == NULL)
376 goto ret;
377
378 tty_color_set_defaults (mc_skin_color);
379 mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
380
381 for (groups = orig_groups; *groups != NULL; groups++)
382 {
383 gchar **keys, **orig_keys;
384
385 if (!mc_skin_color_check_inisection (*groups))
386 continue;
387
388 orig_keys = mc_config_get_keys (mc_skin->config, *groups, NULL);
389
390 for (keys = orig_keys; *keys != NULL; keys++)
391 {
392 mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
393 if (mc_skin_color != NULL)
394 mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
395 }
396 g_strfreev (orig_keys);
397 }
398
399 mc_skin_color_cache_init ();
400
401 ret = TRUE;
402
403 ret:
404 g_strfreev (orig_groups);
405 return ret;
406 }
407
408
409
410 int
411 mc_skin_color_get (const gchar *group, const gchar *name)
412 {
413 tty_color_pair_t *mc_skin_color;
414
415 mc_skin_color = mc_skin_color_get_with_defaults (group, name);
416
417 return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
418 }
419
420