2009-09-04 18:22:49 +04:00
|
|
|
/*
|
|
|
|
Skins engine.
|
|
|
|
Work with colors
|
|
|
|
|
|
|
|
Copyright (C) 2009 The Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009.
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software; you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be
|
|
|
|
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "../src/global.h"
|
2009-09-10 15:14:32 +04:00
|
|
|
#include "../src/args.h"
|
2009-09-04 18:22:49 +04:00
|
|
|
#include "../src/tty/color.h"
|
|
|
|
#include "skin.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
int mc_skin_color__cache[MC_SKIN_COLOR_CACHE_COUNT];
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
|
|
|
|
static mc_skin_color_t *
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_get_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
2009-09-14 17:43:03 +04:00
|
|
|
gchar key_name[BUF_TINY];
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_t *mc_skin_color;
|
2009-09-04 18:22:49 +04:00
|
|
|
|
|
|
|
if (group == NULL || key == NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
return NULL;
|
2009-09-04 18:22:49 +04:00
|
|
|
|
|
|
|
if (mc_skin == NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin = &mc_skin__default;
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
g_snprintf (key_name, sizeof (key_name), "%s.%s", group, key);
|
2009-09-04 18:22:49 +04:00
|
|
|
mc_skin_color = (mc_skin_color_t *) g_hash_table_lookup (mc_skin->colors, (gpointer) key_name);
|
|
|
|
|
|
|
|
return mc_skin_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
#if 0
|
2009-09-04 18:22:49 +04:00
|
|
|
static void
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_remove_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
2009-09-14 17:43:03 +04:00
|
|
|
gchar key_name[BUF_TINY];
|
2009-09-04 18:22:49 +04:00
|
|
|
if (group == NULL || key == NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
return;
|
2009-09-04 18:22:49 +04:00
|
|
|
|
|
|
|
if (mc_skin == NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin = &mc_skin__default;
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
g_snprintf (key_name, sizeof (key_name), "%s.%s", group, key);
|
2009-09-04 18:22:49 +04:00
|
|
|
g_hash_table_remove (mc_skin->colors, (gpointer) key_name);
|
|
|
|
}
|
2009-09-14 17:43:03 +04:00
|
|
|
#endif
|
2009-09-04 18:22:49 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_add_to_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key,
|
|
|
|
mc_skin_color_t * mc_skin_color)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
|
|
|
gchar *key_name;
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
key_name = g_strdup_printf ("%s.%s", group, key);
|
2009-09-04 18:22:49 +04:00
|
|
|
if (key_name == NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
return;
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
if (g_hash_table_lookup (mc_skin->colors, (gpointer) key_name) != NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
g_hash_table_remove (mc_skin->colors, (gpointer) key_name);
|
2009-09-04 18:22:49 +04:00
|
|
|
|
|
|
|
g_hash_table_insert (mc_skin->colors, (gpointer) key_name, (gpointer) mc_skin_color);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static mc_skin_color_t *
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_get_with_defaults (const gchar * group, const gchar * name)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
|
|
|
mc_skin_color_t *mc_skin_color;
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color = mc_skin_color_get_from_hash (NULL, group, name);
|
2009-09-04 18:22:49 +04:00
|
|
|
if (mc_skin_color != NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
return mc_skin_color;
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color = mc_skin_color_get_from_hash (NULL, group, "_default_");
|
2009-09-04 18:22:49 +04:00
|
|
|
if (mc_skin_color != NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
return mc_skin_color;
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color = mc_skin_color_get_from_hash (NULL, "core", "_default_");
|
2009-09-04 18:22:49 +04:00
|
|
|
return mc_skin_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static mc_skin_color_t *
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_get_from_ini_file (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
|
|
|
gsize items_count;
|
|
|
|
gchar **values;
|
|
|
|
mc_skin_color_t *mc_skin_color, *tmp;
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
values = mc_config_get_string_list (mc_skin->config, group, key, &items_count);
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
if (values == NULL || *values == NULL) {
|
2009-09-14 17:43:03 +04:00
|
|
|
if (values != NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
g_strfreev (values);
|
|
|
|
return NULL;
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color = g_new0 (mc_skin_color_t, 1);
|
|
|
|
if (mc_skin_color == NULL) {
|
|
|
|
g_strfreev (values);
|
|
|
|
return NULL;
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
switch (items_count) {
|
2009-09-04 18:22:49 +04:00
|
|
|
case 0:
|
2009-09-10 16:14:18 +04:00
|
|
|
tmp = mc_skin_color_get_with_defaults (group, "_default_");
|
|
|
|
if (tmp) {
|
|
|
|
mc_skin_color->fgcolor = g_strdup (tmp->fgcolor);
|
|
|
|
mc_skin_color->bgcolor = g_strdup (tmp->bgcolor);
|
|
|
|
} else {
|
|
|
|
g_strfreev (values);
|
|
|
|
g_free (mc_skin_color);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
2009-09-04 18:22:49 +04:00
|
|
|
case 1:
|
2009-10-10 09:35:37 +04:00
|
|
|
mc_skin_color->fgcolor = (values[0]) ? g_strstrip (g_strdup (values[0])) : NULL;
|
2009-09-10 16:14:18 +04:00
|
|
|
tmp = mc_skin_color_get_with_defaults (group, "_default_");
|
2009-09-14 17:43:03 +04:00
|
|
|
mc_skin_color->bgcolor = (tmp != NULL) ? g_strdup (tmp->bgcolor) : NULL;
|
2009-09-10 16:14:18 +04:00
|
|
|
break;
|
2009-09-04 18:22:49 +04:00
|
|
|
case 2:
|
2009-10-10 09:35:37 +04:00
|
|
|
mc_skin_color->fgcolor = (values[0]) ? g_strstrip (g_strdup (values[0])) : NULL;
|
|
|
|
mc_skin_color->bgcolor = (values[1]) ? g_strstrip (g_strdup (values[1])) : NULL;
|
2009-09-10 16:14:18 +04:00
|
|
|
break;
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
|
|
|
g_strfreev (values);
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color->pair_index =
|
|
|
|
tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
|
2009-09-04 18:22:49 +04:00
|
|
|
|
|
|
|
return mc_skin_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_set_default_for_terminal (mc_skin_t * mc_skin)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
|
|
|
mc_skin_color_t *mc_skin_color;
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color = g_new0 (mc_skin_color_t, 1);
|
2009-09-04 18:22:49 +04:00
|
|
|
if (mc_skin_color == NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
return;
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color->fgcolor = g_strdup ("default");
|
|
|
|
mc_skin_color->bgcolor = g_strdup ("default");
|
|
|
|
mc_skin_color->pair_index =
|
|
|
|
tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
|
|
|
|
mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
static void
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_cache_init (void)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
2009-09-10 16:14:18 +04:00
|
|
|
DEFAULT_COLOR = mc_skin_color_get ("skin", "terminal_default_color");
|
|
|
|
NORMAL_COLOR = mc_skin_color_get ("core", "_default_");
|
|
|
|
MARKED_COLOR = mc_skin_color_get ("core", "marked");
|
|
|
|
SELECTED_COLOR = mc_skin_color_get ("core", "selected");
|
|
|
|
MARKED_SELECTED_COLOR = mc_skin_color_get ("core", "markselect");
|
2009-09-19 18:31:22 +04:00
|
|
|
REVERSE_COLOR = mc_skin_color_get ("core", "reverse");
|
2009-09-10 16:14:18 +04:00
|
|
|
|
|
|
|
COLOR_NORMAL = mc_skin_color_get ("dialog", "_default_");
|
|
|
|
COLOR_FOCUS = mc_skin_color_get ("dialog", "dfocus");
|
|
|
|
COLOR_HOT_NORMAL = mc_skin_color_get ("dialog", "dhotnormal");
|
|
|
|
COLOR_HOT_FOCUS = mc_skin_color_get ("dialog", "dhotfocus");
|
|
|
|
|
2009-09-19 18:31:22 +04:00
|
|
|
ERROR_COLOR = mc_skin_color_get ("error", "_default_");
|
|
|
|
ERROR_HOT_NORMAL = mc_skin_color_get ("error", "errdhotnormal");
|
|
|
|
ERROR_HOT_FOCUS = mc_skin_color_get ("error", "errdhotfocus");
|
|
|
|
|
|
|
|
MENU_ENTRY_COLOR = mc_skin_color_get ("menu", "_default_");
|
2009-09-10 16:14:18 +04:00
|
|
|
MENU_SELECTED_COLOR = mc_skin_color_get ("menu", "menusel");
|
|
|
|
MENU_HOT_COLOR = mc_skin_color_get ("menu", "menuhot");
|
|
|
|
MENU_HOTSEL_COLOR = mc_skin_color_get ("menu", "menuhotsel");
|
|
|
|
|
2009-09-19 18:31:22 +04:00
|
|
|
GAUGE_COLOR = mc_skin_color_get ("core", "gauge");
|
|
|
|
INPUT_COLOR = mc_skin_color_get ("core", "input");
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
HELP_NORMAL_COLOR = mc_skin_color_get ("help", "_default_");
|
|
|
|
HELP_ITALIC_COLOR = mc_skin_color_get ("help", "helpitalic");
|
|
|
|
HELP_BOLD_COLOR = mc_skin_color_get ("help", "helpbold");
|
|
|
|
HELP_LINK_COLOR = mc_skin_color_get ("help", "helplink");
|
|
|
|
HELP_SLINK_COLOR = mc_skin_color_get ("help", "helpslink");
|
|
|
|
|
2009-09-19 18:31:22 +04:00
|
|
|
VIEW_UNDERLINED_COLOR = mc_skin_color_get ("viewer", "viewunderline");
|
2009-09-10 16:14:18 +04:00
|
|
|
|
|
|
|
EDITOR_NORMAL_COLOR = mc_skin_color_get ("editor", "_default_");
|
|
|
|
EDITOR_BOLD_COLOR = mc_skin_color_get ("editor", "editbold");
|
|
|
|
EDITOR_MARKED_COLOR = mc_skin_color_get ("editor", "editmarked");
|
|
|
|
EDITOR_WHITESPACE_COLOR = mc_skin_color_get ("editor", "editwhitespace");
|
|
|
|
LINE_STATE_COLOR = mc_skin_color_get ("editor", "linestate");
|
2009-10-22 13:29:24 +04:00
|
|
|
BOOK_MARK_COLOR = mc_skin_color_get ("editor", "bookmark");
|
|
|
|
BOOK_MARK_FOUND_COLOR = mc_skin_color_get ("editor", "bookmarkfound");
|
|
|
|
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
2009-09-10 16:14:18 +04:00
|
|
|
|
2009-09-10 15:14:32 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static gboolean
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_check_inisection (const gchar * group)
|
2009-09-10 15:14:32 +04:00
|
|
|
{
|
2009-09-19 18:31:22 +04:00
|
|
|
return !((strcasecmp ("skin", group) == 0)
|
|
|
|
|| (strcasecmp ("lines", group) == 0));
|
2009-09-10 15:14:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_check_bw_mode (mc_skin_t * mc_skin)
|
2009-09-10 15:14:32 +04:00
|
|
|
{
|
|
|
|
gsize items_count;
|
|
|
|
gchar **groups, **orig_groups;
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
if (!mc_args__disable_colors)
|
|
|
|
return;
|
2009-09-10 15:14:32 +04:00
|
|
|
|
|
|
|
orig_groups = groups = mc_config_get_groups (mc_skin->config, &items_count);
|
|
|
|
|
|
|
|
if (groups == NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
return;
|
2009-09-10 15:14:32 +04:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
for (; *groups != NULL; groups++) {
|
|
|
|
if (mc_skin_color_check_inisection (*groups))
|
|
|
|
mc_config_del_group (mc_skin->config, *groups);
|
2009-09-10 15:14:32 +04:00
|
|
|
}
|
2009-09-10 16:14:18 +04:00
|
|
|
g_strfreev (orig_groups);
|
|
|
|
mc_skin_hardcoded_blackwhite_colors (mc_skin);
|
2009-09-10 15:14:32 +04:00
|
|
|
}
|
2009-09-04 18:22:49 +04:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
gboolean
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_parse_ini_file (mc_skin_t * mc_skin)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
|
|
|
gsize items_count;
|
|
|
|
gchar **groups, **orig_groups;
|
|
|
|
gchar **keys, **orig_keys;
|
|
|
|
mc_skin_color_t *mc_skin_color;
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_check_bw_mode (mc_skin);
|
2009-09-10 15:14:32 +04:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
orig_groups = groups = mc_config_get_groups (mc_skin->config, &items_count);
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
if (groups == NULL || *groups == NULL) {
|
2009-09-14 17:43:03 +04:00
|
|
|
if (groups != NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
g_strfreev (groups);
|
|
|
|
return FALSE;
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* as first, need to set up default colors */
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_set_default_for_terminal (mc_skin);
|
|
|
|
mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
|
2009-09-04 18:22:49 +04:00
|
|
|
if (mc_skin_color == NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_set_defaults (mc_skin_color->fgcolor, mc_skin_color->bgcolor);
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
for (; *groups != NULL; groups++) {
|
2009-09-10 16:14:18 +04:00
|
|
|
if (!mc_skin_color_check_inisection (*groups))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
orig_keys = keys = mc_config_get_keys (mc_skin->config, *groups, &items_count);
|
|
|
|
if (keys == NULL || *keys == NULL) {
|
2009-09-14 17:43:03 +04:00
|
|
|
if (keys != NULL)
|
2009-09-10 16:14:18 +04:00
|
|
|
g_strfreev (keys);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; *keys; keys++) {
|
|
|
|
mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
|
2009-09-14 17:43:03 +04:00
|
|
|
if (mc_skin_color != NULL)
|
|
|
|
mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
g_strfreev (orig_keys);
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
2009-09-10 16:14:18 +04:00
|
|
|
g_strfreev (orig_groups);
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_cache_init ();
|
2009-09-04 18:22:49 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color_get (const gchar * group, const gchar * name)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
|
|
|
mc_skin_color_t *mc_skin_color;
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_skin_color = mc_skin_color_get_with_defaults (group, name);
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|