Merge branch '3578_mcext_fixes'
[midnight-commander.git] / lib / skin / colors-old.c
blobb34ac14d951a4dee0235a8ed812ede7ffa01e98e
1 /*
2 Skins engine.
3 Work with colors - backward compatibility
5 Copyright (C) 2009-2016
6 Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009
10 Egmont Koblinger <egmont@gmail.com>, 2010
11 Andrew Borodin <aborodin@vmail.ru>, 2012
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <config.h>
30 #include <stdlib.h>
31 #include <string.h> /* strcmp() */
32 #include <sys/types.h> /* size_t */
34 #include "internal.h"
36 #include "lib/tty/color.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 typedef struct mc_skin_colors_old_struct
46 const char *old_color;
47 const char *group;
48 const char *key;
49 } mc_skin_colors_old_t;
51 /*** file scope variables ************************************************************************/
53 /* keep this table alphabetically sorted */
54 static const mc_skin_colors_old_t old_colors[] = {
55 {"bbarbutton", "buttonbar", "button"},
56 {"bbarhotkey", "buttonbar", "hotkey"},
57 {"commandlinemark", "core", "commandlinemark"},
58 {"dfocus", "dialog", "dfocus"},
59 {"dhotfocus", "dialog", "dhotfocus"},
60 {"dhotnormal", "dialog", "dhotnormal"},
61 {"disabled", "core", "disabled"},
62 {"dnormal", "dialog", "_default_"},
63 {"editbg", "editor", "editbg"},
64 {"editbold", "editor", "editbold"},
65 {"editframe", "editor", "editframe"},
66 {"editframeactive", "editor", "editframeactive"},
67 {"editframedrag", "editor", "editframedrag"},
68 {"editlinestate", "editor", "editlinestate"},
69 {"editmarked", "editor", "editmarked"},
70 {"editnormal", "editor", "_default_"},
71 {"editwhitespace", "editor", "editwhitespace"},
72 {"errdhotfocus", "error", "errdhotfocus"},
73 {"errdhotnormal", "error", "errdhotnormal"},
74 {"errors", "error", "_default_"},
75 {"gauge", "core", "gauge"},
76 {"header", "core", "header"},
77 {"helpbold", "help", "helpbold"},
78 {"helpitalic", "help", "helpitalic"},
79 {"helplink", "help", "helplink"},
80 {"helpnormal", "help", "_default_"},
81 {"helpslink", "help", "helpslink"},
82 {"input", "core", "input"},
83 {"inputmark", "core", "inputmark"},
84 {"inputunchanged", "core", "inputunchanged"},
85 {"marked", "core", "marked"},
86 {"markselect", "core", "markselect"},
87 {"menuhot", "menu", "menuhot"},
88 {"menuhotsel", "menu", "menuhotsel"},
89 {"menuinactive", "menu", "menuinactive"},
90 {"menunormal", "menu", "_default_"},
91 {"menusel", "menu", "menusel"},
92 {"normal", "core", "_default_"},
93 {"pmenunormal", "popupmenu", "_default_"},
94 {"pmenusel", "popupmenu", "menusel"},
95 {"pmenutitle", "popupmenu", "menutitle"},
96 {"reverse", "core", "reverse"},
97 {"selected", "core", "selected"},
98 {"statusbar", "statusbar", "_default_"},
99 {"viewbold", "viewer", "viewbold"},
100 {"viewnormal", "viewer", "_default_"},
101 {"viewselected", "viewer", "viewselected"},
102 {"viewunderline", "viewer", "viewunderline"}
105 static const size_t num_old_colors = G_N_ELEMENTS (old_colors);
107 /*** file scope functions ************************************************************************/
109 static int
110 old_color_comparator (const void *p1, const void *p2)
112 const mc_skin_colors_old_t *m1 = (const mc_skin_colors_old_t *) p1;
113 const mc_skin_colors_old_t *m2 = (const mc_skin_colors_old_t *) p2;
115 return strcmp (m1->old_color, m2->old_color);
118 /* --------------------------------------------------------------------------------------------- */
120 static gboolean
121 mc_skin_colors_old_transform (const char *old_color, const char **group, const char **key)
123 const mc_skin_colors_old_t oc = { old_color, NULL, NULL };
124 mc_skin_colors_old_t *res;
126 if (old_color == NULL)
127 return FALSE;
129 res = (mc_skin_colors_old_t *) bsearch (&oc, old_colors, num_old_colors,
130 sizeof (old_colors[0]), old_color_comparator);
132 if (res == NULL)
133 return FALSE;
135 if (group != NULL)
136 *group = res->group;
137 if (key != NULL)
138 *key = res->key;
139 return TRUE;
142 /* --------------------------------------------------------------------------------------------- */
144 static void
145 mc_skin_colors_old_configure_one (mc_skin_t * mc_skin, const char *the_color_string)
147 gchar **colors, **orig_colors;
149 if (the_color_string == NULL)
150 return;
152 orig_colors = g_strsplit (the_color_string, ":", -1);
153 if (orig_colors == NULL)
154 return;
156 for (colors = orig_colors; *colors != NULL; colors++)
158 gchar **key_val;
159 const gchar *skin_group, *skin_key;
161 key_val = g_strsplit_set (*colors, "=,", 4);
163 if (key_val == NULL)
164 continue;
166 if (key_val[1] != NULL && mc_skin_colors_old_transform (key_val[0], &skin_group, &skin_key))
168 gchar *skin_val;
170 if (key_val[2] == NULL)
171 skin_val = g_strdup_printf ("%s;", key_val[1]);
172 else if (key_val[3] == NULL)
173 skin_val = g_strdup_printf ("%s;%s", key_val[1], key_val[2]);
174 else
175 skin_val = g_strdup_printf ("%s;%s;%s", key_val[1], key_val[2], key_val[3]);
177 mc_config_set_string (mc_skin->config, skin_group, skin_key, skin_val);
178 g_free (skin_val);
181 g_strfreev (key_val);
183 g_strfreev (orig_colors);
186 /* --------------------------------------------------------------------------------------------- */
187 /*** public functions ****************************************************************************/
188 /* --------------------------------------------------------------------------------------------- */
190 void
191 mc_skin_colors_old_configure (mc_skin_t * mc_skin)
193 mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.setup_color_string);
194 mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.term_color_string);
195 mc_skin_colors_old_configure_one (mc_skin, getenv ("MC_COLOR_TABLE"));
196 mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.command_line_colors);
199 /* --------------------------------------------------------------------------------------------- */