colors.c 13 KB

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