colors.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. Skins engine.
  3. Work with colors
  4. Copyright (C) 2009, 2010 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009
  7. Egmont Koblinger <egmont@gmail.com>, 2010
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software; you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation; either version 2 of the
  12. License, or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be
  14. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. MA 02110-1301, USA.
  21. */
  22. #include <config.h>
  23. #include <string.h>
  24. #include "internal.h"
  25. #include "lib/tty/color.h"
  26. /*** global variables ****************************************************************************/
  27. int mc_skin_color__cache[MC_SKIN_COLOR_CACHE_COUNT];
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** file scope variables ************************************************************************/
  31. /*** file scope functions ************************************************************************/
  32. static mc_skin_color_t *
  33. mc_skin_color_get_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
  34. {
  35. gchar kname[BUF_TINY];
  36. mc_skin_color_t *mc_skin_color;
  37. if (group == NULL || key == NULL)
  38. return NULL;
  39. if (mc_skin == NULL)
  40. mc_skin = &mc_skin__default;
  41. g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
  42. mc_skin_color = (mc_skin_color_t *) g_hash_table_lookup (mc_skin->colors, (gpointer) kname);
  43. return mc_skin_color;
  44. }
  45. /* --------------------------------------------------------------------------------------------- */
  46. #if 0
  47. static void
  48. mc_skin_color_remove_from_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
  49. {
  50. gchar kname[BUF_TINY];
  51. if (group == NULL || key == NULL)
  52. return;
  53. if (mc_skin == NULL)
  54. mc_skin = &mc_skin__default;
  55. g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
  56. g_hash_table_remove (mc_skin->colors, (gpointer) kname);
  57. }
  58. #endif
  59. /* --------------------------------------------------------------------------------------------- */
  60. static void
  61. mc_skin_color_add_to_hash (mc_skin_t * mc_skin, const gchar * group, const gchar * key,
  62. mc_skin_color_t * mc_skin_color)
  63. {
  64. gchar *kname;
  65. kname = g_strdup_printf ("%s.%s", group, key);
  66. if (kname != NULL)
  67. {
  68. if (g_hash_table_lookup (mc_skin->colors, (gpointer) kname) != NULL)
  69. g_hash_table_remove (mc_skin->colors, (gpointer) kname);
  70. g_hash_table_insert (mc_skin->colors, (gpointer) kname, (gpointer) mc_skin_color);
  71. }
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. static mc_skin_color_t *
  75. mc_skin_color_get_with_defaults (const gchar * group, const gchar * name)
  76. {
  77. mc_skin_color_t *mc_skin_color;
  78. mc_skin_color = mc_skin_color_get_from_hash (NULL, group, name);
  79. if (mc_skin_color != NULL)
  80. return mc_skin_color;
  81. mc_skin_color = mc_skin_color_get_from_hash (NULL, group, "_default_");
  82. if (mc_skin_color != NULL)
  83. return mc_skin_color;
  84. mc_skin_color = mc_skin_color_get_from_hash (NULL, "core", "_default_");
  85. return mc_skin_color;
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. static mc_skin_color_t *
  89. mc_skin_color_get_from_ini_file (mc_skin_t * mc_skin, const gchar * group, const gchar * key)
  90. {
  91. gsize items_count;
  92. gchar **values;
  93. mc_skin_color_t *mc_skin_color, *tmp;
  94. values = mc_config_get_string_list (mc_skin->config, group, key, &items_count);
  95. if (values == NULL || values[0] == NULL)
  96. {
  97. g_strfreev (values);
  98. return NULL;
  99. }
  100. mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
  101. if (mc_skin_color == NULL)
  102. {
  103. g_strfreev (values);
  104. return NULL;
  105. }
  106. tmp = mc_skin_color_get_with_defaults (group, "_default_");
  107. mc_skin_color->fgcolor = (items_count > 0 && values[0][0]) ? g_strstrip (g_strdup (values[0])) :
  108. (tmp != NULL) ? g_strdup (tmp->fgcolor) : NULL;
  109. mc_skin_color->bgcolor = (items_count > 1 && values[1][0]) ? g_strstrip (g_strdup (values[1])) :
  110. (tmp != NULL) ? g_strdup (tmp->bgcolor) : NULL;
  111. mc_skin_color->attrs = (items_count > 2 && values[2][0]) ? g_strstrip (g_strdup (values[2])) :
  112. (tmp != NULL) ? g_strdup (tmp->attrs) : NULL;
  113. g_strfreev (values);
  114. mc_skin_color->pair_index =
  115. tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor,
  116. mc_skin_color->attrs, FALSE);
  117. return mc_skin_color;
  118. }
  119. /* --------------------------------------------------------------------------------------------- */
  120. static void
  121. mc_skin_color_set_default_for_terminal (mc_skin_t * mc_skin)
  122. {
  123. mc_skin_color_t *mc_skin_color;
  124. mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
  125. if (mc_skin_color != NULL)
  126. {
  127. mc_skin_color->fgcolor = g_strdup ("default");
  128. mc_skin_color->bgcolor = g_strdup ("default");
  129. mc_skin_color->attrs = NULL;
  130. mc_skin_color->pair_index =
  131. tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor,
  132. mc_skin_color->attrs, FALSE);
  133. mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
  134. }
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. static void
  138. mc_skin_color_cache_init (void)
  139. {
  140. DEFAULT_COLOR = mc_skin_color_get ("skin", "terminal_default_color");
  141. NORMAL_COLOR = mc_skin_color_get ("core", "_default_");
  142. MARKED_COLOR = mc_skin_color_get ("core", "marked");
  143. SELECTED_COLOR = mc_skin_color_get ("core", "selected");
  144. MARKED_SELECTED_COLOR = mc_skin_color_get ("core", "markselect");
  145. DISABLED_COLOR = mc_skin_color_get ("core", "disabled");
  146. REVERSE_COLOR = mc_skin_color_get ("core", "reverse");
  147. HEADER_COLOR = mc_skin_color_get ("core", "header");
  148. COMMAND_MARK_COLOR = mc_skin_color_get ("core", "commandlinemark");
  149. COLOR_NORMAL = mc_skin_color_get ("dialog", "_default_");
  150. COLOR_FOCUS = mc_skin_color_get ("dialog", "dfocus");
  151. COLOR_HOT_NORMAL = mc_skin_color_get ("dialog", "dhotnormal");
  152. COLOR_HOT_FOCUS = mc_skin_color_get ("dialog", "dhotfocus");
  153. COLOR_TITLE = mc_skin_color_get ("dialog", "dtitle");
  154. ERROR_COLOR = mc_skin_color_get ("error", "_default_");
  155. ERROR_FOCUS = mc_skin_color_get ("error", "errdfocus");
  156. ERROR_HOT_NORMAL = mc_skin_color_get ("error", "errdhotnormal");
  157. ERROR_HOT_FOCUS = mc_skin_color_get ("error", "errdhotfocus");
  158. ERROR_TITLE = mc_skin_color_get ("error", "errdtitle");
  159. MENU_ENTRY_COLOR = mc_skin_color_get ("menu", "_default_");
  160. MENU_SELECTED_COLOR = mc_skin_color_get ("menu", "menusel");
  161. MENU_HOT_COLOR = mc_skin_color_get ("menu", "menuhot");
  162. MENU_HOTSEL_COLOR = mc_skin_color_get ("menu", "menuhotsel");
  163. MENU_INACTIVE_COLOR = mc_skin_color_get ("menu", "menuinactive");
  164. PMENU_ENTRY_COLOR = mc_skin_color_get ("popupmenu", "_default_");
  165. PMENU_SELECTED_COLOR = mc_skin_color_get ("popupmenu", "menusel");
  166. PMENU_TITLE_COLOR = mc_skin_color_get ("popupmenu", "menutitle");
  167. BUTTONBAR_HOTKEY_COLOR = mc_skin_color_get ("buttonbar", "hotkey");
  168. BUTTONBAR_BUTTON_COLOR = mc_skin_color_get ("buttonbar", "button");
  169. STATUSBAR_COLOR = mc_skin_color_get ("statusbar", "_default_");
  170. GAUGE_COLOR = mc_skin_color_get ("core", "gauge");
  171. INPUT_COLOR = mc_skin_color_get ("core", "input");
  172. INPUT_HISTORY_COLOR = mc_skin_color_get ("core", "inputhistory");
  173. COMMAND_HISTORY_COLOR = mc_skin_color_get ("core", "commandhistory");
  174. INPUT_MARK_COLOR = mc_skin_color_get ("core", "inputmark");
  175. INPUT_UNCHANGED_COLOR = mc_skin_color_get ("core", "inputunchanged");
  176. HELP_NORMAL_COLOR = mc_skin_color_get ("help", "_default_");
  177. HELP_ITALIC_COLOR = mc_skin_color_get ("help", "helpitalic");
  178. HELP_BOLD_COLOR = mc_skin_color_get ("help", "helpbold");
  179. HELP_LINK_COLOR = mc_skin_color_get ("help", "helplink");
  180. HELP_SLINK_COLOR = mc_skin_color_get ("help", "helpslink");
  181. HELP_TITLE_COLOR = mc_skin_color_get ("help", "helptitle");
  182. VIEW_BOLD_COLOR = mc_skin_color_get ("viewer", "viewbold");
  183. VIEW_UNDERLINED_COLOR = mc_skin_color_get ("viewer", "viewunderline");
  184. VIEW_SELECTED_COLOR = mc_skin_color_get ("viewer", "viewselected");
  185. EDITOR_NORMAL_COLOR = mc_skin_color_get ("editor", "_default_");
  186. EDITOR_BOLD_COLOR = mc_skin_color_get ("editor", "editbold");
  187. EDITOR_MARKED_COLOR = mc_skin_color_get ("editor", "editmarked");
  188. EDITOR_WHITESPACE_COLOR = mc_skin_color_get ("editor", "editwhitespace");
  189. EDITOR_RIGHT_MARGIN_COLOR = mc_skin_color_get ("editor", "editrightmargin");
  190. LINE_STATE_COLOR = mc_skin_color_get ("editor", "editlinestate");
  191. BOOK_MARK_COLOR = mc_skin_color_get ("editor", "bookmark");
  192. BOOK_MARK_FOUND_COLOR = mc_skin_color_get ("editor", "bookmarkfound");
  193. DFF_ADD_COLOR = mc_skin_color_get ("diffviewer", "added");
  194. DFF_CHG_COLOR = mc_skin_color_get ("diffviewer", "changedline");
  195. DFF_CHH_COLOR = mc_skin_color_get ("diffviewer", "changednew");
  196. DFF_CHD_COLOR = mc_skin_color_get ("diffviewer", "changed");
  197. DFF_DEL_COLOR = mc_skin_color_get ("diffviewer", "removed");
  198. DFF_FOLDER_COLOR = mc_skin_color_get ("diffviewer", "folder");
  199. DFF_ERROR_COLOR = mc_skin_color_get ("diffviewer", "error");
  200. }
  201. /* --------------------------------------------------------------------------------------------- */
  202. static gboolean
  203. mc_skin_color_check_inisection (const gchar * group)
  204. {
  205. return !((strcasecmp ("skin", group) == 0)
  206. || (strcasecmp ("lines", group) == 0) || (strncasecmp ("widget-", group, 7) == 0));
  207. }
  208. /* --------------------------------------------------------------------------------------------- */
  209. static void
  210. mc_skin_color_check_bw_mode (mc_skin_t * mc_skin)
  211. {
  212. gchar **groups, **orig_groups;
  213. if (tty_use_colors () && !mc_global.tty.disable_colors)
  214. return;
  215. orig_groups = groups = mc_config_get_groups (mc_skin->config, NULL);
  216. if (groups == NULL)
  217. return;
  218. for (; *groups != NULL; groups++)
  219. {
  220. if (mc_skin_color_check_inisection (*groups))
  221. mc_config_del_group (mc_skin->config, *groups);
  222. }
  223. g_strfreev (orig_groups);
  224. mc_skin_hardcoded_blackwhite_colors (mc_skin);
  225. }
  226. /* --------------------------------------------------------------------------------------------- */
  227. /*** public functions ****************************************************************************/
  228. /* --------------------------------------------------------------------------------------------- */
  229. gboolean
  230. mc_skin_color_parse_ini_file (mc_skin_t * mc_skin)
  231. {
  232. gsize items_count;
  233. gchar **groups, **orig_groups;
  234. gchar **keys, **orig_keys;
  235. mc_skin_color_t *mc_skin_color;
  236. mc_skin_color_check_bw_mode (mc_skin);
  237. orig_groups = groups = mc_config_get_groups (mc_skin->config, &items_count);
  238. if (groups == NULL || groups[0] == NULL)
  239. {
  240. g_strfreev (groups);
  241. return FALSE;
  242. }
  243. /* as first, need to set up default colors */
  244. mc_skin_color_set_default_for_terminal (mc_skin);
  245. mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
  246. if (mc_skin_color == NULL)
  247. return FALSE;
  248. tty_color_set_defaults (mc_skin_color->fgcolor, mc_skin_color->bgcolor, mc_skin_color->attrs);
  249. mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
  250. for (; *groups != NULL; groups++)
  251. {
  252. if (!mc_skin_color_check_inisection (*groups))
  253. continue;
  254. orig_keys = keys = mc_config_get_keys (mc_skin->config, *groups, &items_count);
  255. if (keys == NULL)
  256. continue;
  257. for (; *keys != NULL; keys++)
  258. {
  259. mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
  260. if (mc_skin_color != NULL)
  261. mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
  262. }
  263. g_strfreev (orig_keys);
  264. }
  265. g_strfreev (orig_groups);
  266. mc_skin_color_cache_init ();
  267. return TRUE;
  268. }
  269. /* --------------------------------------------------------------------------------------------- */
  270. int
  271. mc_skin_color_get (const gchar * group, const gchar * name)
  272. {
  273. mc_skin_color_t *mc_skin_color;
  274. mc_skin_color = mc_skin_color_get_with_defaults (group, name);
  275. return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
  276. }
  277. /* --------------------------------------------------------------------------------------------- */