colors.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. Skins engine.
  3. Work with colors
  4. Copyright (C) 2009 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software; you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be
  13. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. MA 02110-1301, USA.
  20. */
  21. #include <config.h>
  22. #include <string.h>
  23. #include "internal.h"
  24. #include "lib/tty/color.h"
  25. #include "src/args.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 == NULL)
  96. {
  97. if (values != NULL)
  98. g_strfreev (values);
  99. return NULL;
  100. }
  101. mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
  102. if (mc_skin_color == NULL)
  103. {
  104. g_strfreev (values);
  105. return NULL;
  106. }
  107. switch (items_count)
  108. {
  109. case 0:
  110. tmp = mc_skin_color_get_with_defaults (group, "_default_");
  111. if (tmp)
  112. {
  113. mc_skin_color->fgcolor = g_strdup (tmp->fgcolor);
  114. mc_skin_color->bgcolor = g_strdup (tmp->bgcolor);
  115. }
  116. else
  117. {
  118. g_strfreev (values);
  119. g_free (mc_skin_color);
  120. return NULL;
  121. }
  122. break;
  123. case 1:
  124. mc_skin_color->fgcolor = (values[0]) ? g_strstrip (g_strdup (values[0])) : NULL;
  125. tmp = mc_skin_color_get_with_defaults (group, "_default_");
  126. mc_skin_color->bgcolor = (tmp != NULL) ? g_strdup (tmp->bgcolor) : NULL;
  127. break;
  128. case 2:
  129. mc_skin_color->fgcolor = (values[0]) ? g_strstrip (g_strdup (values[0])) : NULL;
  130. mc_skin_color->bgcolor = (values[1]) ? g_strstrip (g_strdup (values[1])) : NULL;
  131. break;
  132. }
  133. g_strfreev (values);
  134. mc_skin_color->pair_index =
  135. tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
  136. return mc_skin_color;
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. static void
  140. mc_skin_color_set_default_for_terminal (mc_skin_t * mc_skin)
  141. {
  142. mc_skin_color_t *mc_skin_color;
  143. mc_skin_color = g_try_new0 (mc_skin_color_t, 1);
  144. if (mc_skin_color != NULL)
  145. {
  146. mc_skin_color->fgcolor = g_strdup ("default");
  147. mc_skin_color->bgcolor = g_strdup ("default");
  148. mc_skin_color->pair_index =
  149. tty_try_alloc_color_pair2 (mc_skin_color->fgcolor, mc_skin_color->bgcolor, FALSE);
  150. mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
  151. }
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. static void
  155. mc_skin_color_cache_init (void)
  156. {
  157. DEFAULT_COLOR = mc_skin_color_get ("skin", "terminal_default_color");
  158. NORMAL_COLOR = mc_skin_color_get ("core", "_default_");
  159. MARKED_COLOR = mc_skin_color_get ("core", "marked");
  160. SELECTED_COLOR = mc_skin_color_get ("core", "selected");
  161. MARKED_SELECTED_COLOR = mc_skin_color_get ("core", "markselect");
  162. REVERSE_COLOR = mc_skin_color_get ("core", "reverse");
  163. COMMAND_MARK_COLOR = mc_skin_color_get ("core", "commandlinemark");
  164. COLOR_NORMAL = mc_skin_color_get ("dialog", "_default_");
  165. COLOR_FOCUS = mc_skin_color_get ("dialog", "dfocus");
  166. COLOR_HOT_NORMAL = mc_skin_color_get ("dialog", "dhotnormal");
  167. COLOR_HOT_FOCUS = mc_skin_color_get ("dialog", "dhotfocus");
  168. ERROR_COLOR = mc_skin_color_get ("error", "_default_");
  169. ERROR_HOT_NORMAL = mc_skin_color_get ("error", "errdhotnormal");
  170. ERROR_HOT_FOCUS = mc_skin_color_get ("error", "errdhotfocus");
  171. MENU_ENTRY_COLOR = mc_skin_color_get ("menu", "_default_");
  172. MENU_SELECTED_COLOR = mc_skin_color_get ("menu", "menusel");
  173. MENU_HOT_COLOR = mc_skin_color_get ("menu", "menuhot");
  174. MENU_HOTSEL_COLOR = mc_skin_color_get ("menu", "menuhotsel");
  175. MENU_INACTIVE_COLOR = mc_skin_color_get ("menu", "menuinactive");
  176. GAUGE_COLOR = mc_skin_color_get ("core", "gauge");
  177. INPUT_COLOR = mc_skin_color_get ("core", "input");
  178. INPUT_MARK_COLOR = mc_skin_color_get ("core", "inputmark");
  179. INPUT_UNCHANGED_COLOR = mc_skin_color_get ("core", "inputunchanged");
  180. HELP_NORMAL_COLOR = mc_skin_color_get ("help", "_default_");
  181. HELP_ITALIC_COLOR = mc_skin_color_get ("help", "helpitalic");
  182. HELP_BOLD_COLOR = mc_skin_color_get ("help", "helpbold");
  183. HELP_LINK_COLOR = mc_skin_color_get ("help", "helplink");
  184. HELP_SLINK_COLOR = mc_skin_color_get ("help", "helpslink");
  185. VIEW_UNDERLINED_COLOR = mc_skin_color_get ("viewer", "viewunderline");
  186. EDITOR_NORMAL_COLOR = mc_skin_color_get ("editor", "_default_");
  187. EDITOR_BOLD_COLOR = mc_skin_color_get ("editor", "editbold");
  188. EDITOR_MARKED_COLOR = mc_skin_color_get ("editor", "editmarked");
  189. EDITOR_WHITESPACE_COLOR = mc_skin_color_get ("editor", "editwhitespace");
  190. EDITOR_RIGHT_MARGIN_COLOR = mc_skin_color_get ("editor", "editrightmargin");
  191. LINE_STATE_COLOR = mc_skin_color_get ("editor", "editlinestate");
  192. BOOK_MARK_COLOR = mc_skin_color_get ("editor", "bookmark");
  193. BOOK_MARK_FOUND_COLOR = mc_skin_color_get ("editor", "bookmarkfound");
  194. BUTTONBAR_HOTKEY_COLOR = mc_skin_color_get ("buttonbar", "hotkey");
  195. BUTTONBAR_BUTTON_COLOR = mc_skin_color_get ("buttonbar", "button");
  196. DFF_ADD_COLOR = mc_skin_color_get ("diffviewer", "added");
  197. DFF_CHG_COLOR = mc_skin_color_get ("diffviewer", "changedline");
  198. DFF_CHH_COLOR = mc_skin_color_get ("diffviewer", "changednew");
  199. DFF_CHD_COLOR = mc_skin_color_get ("diffviewer", "changed");
  200. DFF_DEL_COLOR = mc_skin_color_get ("diffviewer", "removed");
  201. DFF_FOLDER_COLOR = mc_skin_color_get ("diffviewer", "folder");
  202. DFF_ERROR_COLOR = mc_skin_color_get ("diffviewer", "error");
  203. }
  204. /* --------------------------------------------------------------------------------------------- */
  205. static gboolean
  206. mc_skin_color_check_inisection (const gchar * group)
  207. {
  208. return !((strcasecmp ("skin", group) == 0)
  209. || (strcasecmp ("lines", group) == 0) || (strncasecmp ("widget-", group, 7) == 0));
  210. }
  211. /* --------------------------------------------------------------------------------------------- */
  212. static void
  213. mc_skin_color_check_bw_mode (mc_skin_t * mc_skin)
  214. {
  215. gchar **groups, **orig_groups;
  216. if (tty_use_colors () && !mc_args__disable_colors)
  217. return;
  218. orig_groups = groups = mc_config_get_groups (mc_skin->config, NULL);
  219. if (groups == NULL)
  220. return;
  221. for (; *groups != NULL; groups++)
  222. {
  223. if (mc_skin_color_check_inisection (*groups))
  224. mc_config_del_group (mc_skin->config, *groups);
  225. }
  226. g_strfreev (orig_groups);
  227. mc_skin_hardcoded_blackwhite_colors (mc_skin);
  228. }
  229. /* --------------------------------------------------------------------------------------------- */
  230. /*** public functions ****************************************************************************/
  231. /* --------------------------------------------------------------------------------------------- */
  232. gboolean
  233. mc_skin_color_parse_ini_file (mc_skin_t * mc_skin)
  234. {
  235. gsize items_count;
  236. gchar **groups, **orig_groups;
  237. gchar **keys, **orig_keys;
  238. mc_skin_color_t *mc_skin_color;
  239. mc_skin_color_check_bw_mode (mc_skin);
  240. orig_groups = groups = mc_config_get_groups (mc_skin->config, &items_count);
  241. if (groups == NULL)
  242. return FALSE;
  243. if (*groups == NULL)
  244. {
  245. g_strfreev (groups);
  246. return FALSE;
  247. }
  248. /* as first, need to set up default colors */
  249. mc_skin_color_set_default_for_terminal (mc_skin);
  250. mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
  251. if (mc_skin_color == NULL)
  252. return FALSE;
  253. tty_color_set_defaults (mc_skin_color->fgcolor, mc_skin_color->bgcolor);
  254. mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
  255. for (; *groups != NULL; groups++)
  256. {
  257. if (!mc_skin_color_check_inisection (*groups))
  258. continue;
  259. orig_keys = keys = mc_config_get_keys (mc_skin->config, *groups, &items_count);
  260. if (keys == NULL)
  261. continue;
  262. if (*keys == NULL)
  263. {
  264. g_strfreev (keys);
  265. continue;
  266. }
  267. for (; *keys; keys++)
  268. {
  269. mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
  270. if (mc_skin_color != NULL)
  271. mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
  272. }
  273. g_strfreev (orig_keys);
  274. }
  275. g_strfreev (orig_groups);
  276. mc_skin_color_cache_init ();
  277. return TRUE;
  278. }
  279. /* --------------------------------------------------------------------------------------------- */
  280. int
  281. mc_skin_color_get (const gchar * group, const gchar * name)
  282. {
  283. mc_skin_color_t *mc_skin_color;
  284. mc_skin_color = mc_skin_color_get_with_defaults (group, name);
  285. return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
  286. }
  287. /* --------------------------------------------------------------------------------------------- */