123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- /*
- 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 "internal.h"
- #include "lib/tty/color.h"
- #include "src/args.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 *
- mc_skin_color_get_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
- {
- gchar kname[BUF_TINY];
- mc_skin_color_t *mc_skin_color;
- if (group == NULL || key == NULL)
- return NULL;
- if (mc_skin == NULL)
- mc_skin = &mc_skin__default;
- g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
- mc_skin_color = (mc_skin_color_t *) g_hash_table_lookup (mc_skin->colors, (gpointer) kname);
- return mc_skin_color;
- }
- /* --------------------------------------------------------------------------------------------- */
- #if 0
- static void
- mc_skin_color_remove_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
- {
- gchar kname[BUF_TINY];
- if (group == NULL || key == NULL)
- return;
- if (mc_skin == NULL)
- mc_skin = &mc_skin__default;
- g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
- g_hash_table_remove (mc_skin->colors, (gpointer) kname);
- }
- #endif
- /* --------------------------------------------------------------------------------------------- */
- static void
- mc_skin_color_add_to_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key,
- mc_skin_color_t * mc_skin_color)
- {
- gchar *kname;
- kname = g_strdup_printf ("%s.%s", group, key);
- if (kname != NULL)
- {
- if (g_hash_table_lookup (mc_skin->colors, (gpointer) kname) != NULL)
- g_hash_table_remove (mc_skin->colors, (gpointer) kname);
- g_hash_table_insert (mc_skin->colors, (gpointer) kname, (gpointer) mc_skin_color);
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static mc_skin_color_t *
- mc_skin_color_get_with_defaults (const gchar * group, const gchar * name)
- {
- mc_skin_color_t *mc_skin_color;
- mc_skin_color = mc_skin_color_get_from_hash (NULL, group, name);
- if (mc_skin_color != NULL)
- return mc_skin_color;
- mc_skin_color = mc_skin_color_get_from_hash (NULL, group, "_default_");
- if (mc_skin_color != NULL)
- return mc_skin_color;
- mc_skin_color = mc_skin_color_get_from_hash (NULL, "core", "_default_");
- return mc_skin_color;
- }
- /* --------------------------------------------------------------------------------------------- */
- static mc_skin_color_t *
- mc_skin_color_get_from_ini_file (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
- {
- gsize items_count;
- gchar **values;
- mc_skin_color_t *mc_skin_color, *tmp;
- values = mc_config_get_string_list (mc_skin->config, group, key, &items_count);
- if (values == NULL || *values == NULL)
- {
- if (values != NULL)
- g_strfreev (values);
- return NULL;
- }
- mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
- if (mc_skin_color == NULL)
- {
- g_strfreev (values);
- return NULL;
- }
- switch (items_count)
- {
- case 0:
- 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;
- case 1:
- mc_skin_color->fgcolor = (values[0]) ? g_strstrip (g_strdup (values[0])) : NULL;
- tmp = mc_skin_color_get_with_defaults (group, "_default_");
- mc_skin_color->bgcolor = (tmp != NULL) ? g_strdup (tmp->bgcolor) : NULL;
- break;
- case 2:
- 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;
- break;
- }
- g_strfreev (values);
- mc_skin_color->pair_index =
- tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
- return mc_skin_color;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- mc_skin_color_set_default_for_terminal (mc_skin_t * mc_skin)
- {
- mc_skin_color_t *mc_skin_color;
- mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
- if (mc_skin_color != NULL)
- {
- 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);
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- mc_skin_color_cache_init (void)
- {
- 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");
- REVERSE_COLOR = mc_skin_color_get ("core", "reverse");
- COMMAND_MARK_COLOR = mc_skin_color_get ("core", "commandlinemark");
- 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");
- 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_");
- 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");
- MENU_INACTIVE_COLOR = mc_skin_color_get ("menu", "menuinactive");
- GAUGE_COLOR = mc_skin_color_get ("core", "gauge");
- INPUT_COLOR = mc_skin_color_get ("core", "input");
- INPUT_MARK_COLOR = mc_skin_color_get ("core", "inputmark");
- INPUT_UNCHANGED_COLOR = mc_skin_color_get ("core", "inputunchanged");
- 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");
- VIEW_UNDERLINED_COLOR = mc_skin_color_get ("viewer", "viewunderline");
- 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");
- EDITOR_RIGHT_MARGIN_COLOR = mc_skin_color_get ("editor", "editrightmargin");
- LINE_STATE_COLOR = mc_skin_color_get ("editor", "editlinestate");
- BOOK_MARK_COLOR = mc_skin_color_get ("editor", "bookmark");
- BOOK_MARK_FOUND_COLOR = mc_skin_color_get ("editor", "bookmarkfound");
- BUTTONBAR_HOTKEY_COLOR = mc_skin_color_get ("buttonbar", "hotkey");
- BUTTONBAR_BUTTON_COLOR = mc_skin_color_get ("buttonbar", "button");
- DFF_ADD_COLOR = mc_skin_color_get ("diffviewer", "added");
- DFF_CHG_COLOR = mc_skin_color_get ("diffviewer", "changedline");
- DFF_CHH_COLOR = mc_skin_color_get ("diffviewer", "changednew");
- DFF_CHD_COLOR = mc_skin_color_get ("diffviewer", "changed");
- DFF_DEL_COLOR = mc_skin_color_get ("diffviewer", "removed");
- DFF_FOLDER_COLOR = mc_skin_color_get ("diffviewer", "folder");
- DFF_ERROR_COLOR = mc_skin_color_get ("diffviewer", "error");
- }
- /* --------------------------------------------------------------------------------------------- */
- static gboolean
- mc_skin_color_check_inisection (const gchar * group)
- {
- return !((strcasecmp ("skin", group) == 0)
- || (strcasecmp ("lines", group) == 0) || (strncasecmp ("widget-", group, 7) == 0));
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- mc_skin_color_check_bw_mode (mc_skin_t * mc_skin)
- {
- gchar **groups, **orig_groups;
- if (tty_use_colors () && !mc_args__disable_colors)
- return;
- orig_groups = groups = mc_config_get_groups (mc_skin->config, NULL);
- if (groups == NULL)
- return;
- for (; *groups != NULL; groups++)
- {
- if (mc_skin_color_check_inisection (*groups))
- mc_config_del_group (mc_skin->config, *groups);
- }
- g_strfreev (orig_groups);
- mc_skin_hardcoded_blackwhite_colors (mc_skin);
- }
- /* --------------------------------------------------------------------------------------------- */
- /*** public functions ****************************************************************************/
- /* --------------------------------------------------------------------------------------------- */
- gboolean
- mc_skin_color_parse_ini_file (mc_skin_t * mc_skin)
- {
- gsize items_count;
- gchar **groups, **orig_groups;
- gchar **keys, **orig_keys;
- mc_skin_color_t *mc_skin_color;
- mc_skin_color_check_bw_mode (mc_skin);
- orig_groups = groups = mc_config_get_groups (mc_skin->config, &items_count);
- if (groups == NULL)
- return FALSE;
- if (*groups == NULL)
- {
- g_strfreev (groups);
- return FALSE;
- }
- /* as first, need to set up default colors */
- mc_skin_color_set_default_for_terminal (mc_skin);
- mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
- if (mc_skin_color == NULL)
- return FALSE;
- tty_color_set_defaults (mc_skin_color->fgcolor, mc_skin_color->bgcolor);
- mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
- for (; *groups != NULL; groups++)
- {
- if (!mc_skin_color_check_inisection (*groups))
- continue;
- orig_keys = keys = mc_config_get_keys (mc_skin->config, *groups, &items_count);
- if (keys == NULL)
- continue;
- if (*keys == NULL)
- {
- g_strfreev (keys);
- continue;
- }
- for (; *keys; keys++)
- {
- mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
- if (mc_skin_color != NULL)
- mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
- }
- g_strfreev (orig_keys);
- }
- g_strfreev (orig_groups);
- mc_skin_color_cache_init ();
- return TRUE;
- }
- /* --------------------------------------------------------------------------------------------- */
- int
- mc_skin_color_get (const gchar * group, const gchar * name)
- {
- mc_skin_color_t *mc_skin_color;
- mc_skin_color = mc_skin_color_get_with_defaults (group, name);
- return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
- }
- /* --------------------------------------------------------------------------------------------- */
|