colors-old.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Skins engine.
  3. Work with colors - backward compability
  4. Copyright (C) 2009, 2010, 2011, 2012
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Slava Zanko <slavazanko@gmail.com>, 2009
  8. Egmont Koblinger <egmont@gmail.com>, 2010
  9. Andrew Borodin <aborodin@vmail.ru>, 2012
  10. This file is part of the Midnight Commander.
  11. The Midnight Commander is free software: you can redistribute it
  12. and/or modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, either version 3 of the License,
  14. or (at your option) any later version.
  15. The Midnight Commander is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <stdlib.h>
  24. #include <string.h> /* strcmp() */
  25. #include <sys/types.h> /* size_t */
  26. #include "internal.h"
  27. #include "lib/tty/color.h"
  28. /*** global variables ****************************************************************************/
  29. /*** file scope macro definitions ****************************************************************/
  30. /*** file scope type declarations ****************************************************************/
  31. typedef struct mc_skin_colors_old_struct
  32. {
  33. const char *old_color;
  34. const char *group;
  35. const char *key;
  36. } mc_skin_colors_old_t;
  37. /*** file scope variables ************************************************************************/
  38. /* keep this table alphabetically sorted */
  39. static const mc_skin_colors_old_t old_colors[] = {
  40. {"bbarbutton", "buttonbar", "button"},
  41. {"bbarhotkey", "buttonbar", "hotkey"},
  42. {"commandlinemark", "core", "commandlinemark"},
  43. {"dfocus", "dialog", "dfocus"},
  44. {"dhotfocus", "dialog", "dhotfocus"},
  45. {"dhotnormal", "dialog", "dhotnormal"},
  46. {"disabled", "core", "disabled"},
  47. {"dnormal", "dialog", "_default_"},
  48. {"editbg", "editor", "editbg"},
  49. {"editbold", "editor", "editbold"},
  50. {"editframe", "editor", "editframe"},
  51. {"editframeactive", "editor", "editframeactive"},
  52. {"editframedrag", "editor", "editframedrag"},
  53. {"editlinestate", "editor", "editlinestate"},
  54. {"editmarked", "editor", "editmarked"},
  55. {"editnormal", "editor", "_default_"},
  56. {"editwhitespace", "editor", "editwhitespace"},
  57. {"errdhotfocus", "error", "errdhotfocus"},
  58. {"errdhotnormal", "error", "errdhotnormal"},
  59. {"errors", "error", "_default_"},
  60. {"gauge", "core", "gauge"},
  61. {"header", "core", "header"},
  62. {"helpbold", "help", "helpbold"},
  63. {"helpitalic", "help", "helpitalic"},
  64. {"helplink", "help", "helplink"},
  65. {"helpnormal", "help", "_default_"},
  66. {"helpslink", "help", "helpslink"},
  67. {"input", "core", "input"},
  68. {"inputmark", "core", "inputmark"},
  69. {"inputunchanged", "core", "inputunchanged"},
  70. {"marked", "core", "marked"},
  71. {"markselect", "core", "markselect"},
  72. {"menuhot", "menu", "menuhot"},
  73. {"menuhotsel", "menu", "menuhotsel"},
  74. {"menuinactive", "menu", "menuinactive"},
  75. {"menunormal", "menu", "_default_"},
  76. {"menusel", "menu", "menusel"},
  77. {"normal", "core", "_default_"},
  78. {"pmenunormal", "popupmenu", "_default_"},
  79. {"pmenusel", "popupmenu", "menusel"},
  80. {"pmenutitle", "popupmenu", "menutitle"},
  81. {"reverse", "core", "reverse"},
  82. {"selected", "core", "selected"},
  83. {"statusbar", "statusbar", "_default_"},
  84. {"viewbold", "viewer", "viewbold"},
  85. {"viewselected", "viewer", "viewselected"},
  86. {"viewunderline", "viewer", "viewunderline"}
  87. };
  88. static const size_t num_old_colors = G_N_ELEMENTS (old_colors);
  89. /*** file scope functions ************************************************************************/
  90. static int
  91. old_color_comparator (const void *p1, const void *p2)
  92. {
  93. const mc_skin_colors_old_t *m1 = (const mc_skin_colors_old_t *) p1;
  94. const mc_skin_colors_old_t *m2 = (const mc_skin_colors_old_t *) p2;
  95. return strcmp (m1->old_color, m2->old_color);
  96. }
  97. /* --------------------------------------------------------------------------------------------- */
  98. static gboolean
  99. mc_skin_colors_old_transform (const char *old_color, const char **group, const char **key)
  100. {
  101. const mc_skin_colors_old_t oc = { old_color, NULL, NULL };
  102. mc_skin_colors_old_t *res;
  103. if (old_color == NULL)
  104. return FALSE;
  105. res = (mc_skin_colors_old_t *) bsearch (&oc, old_colors, num_old_colors,
  106. sizeof (old_colors[0]), old_color_comparator);
  107. if (res == NULL)
  108. return FALSE;
  109. if (group != NULL)
  110. *group = res->group;
  111. if (key != NULL)
  112. *key = res->key;
  113. return TRUE;
  114. }
  115. /* --------------------------------------------------------------------------------------------- */
  116. static void
  117. mc_skin_colors_old_configure_one (mc_skin_t * mc_skin, const char *the_color_string)
  118. {
  119. gchar **colors, **orig_colors;
  120. if (the_color_string == NULL)
  121. return;
  122. orig_colors = colors = g_strsplit (the_color_string, ":", -1);
  123. if (colors == NULL)
  124. return;
  125. for (; *colors != NULL; colors++)
  126. {
  127. gchar **key_val;
  128. const gchar *skin_group, *skin_key;
  129. key_val = g_strsplit_set (*colors, "=,", 4);
  130. if (key_val == NULL)
  131. continue;
  132. if (key_val[1] != NULL && mc_skin_colors_old_transform (key_val[0], &skin_group, &skin_key))
  133. {
  134. gchar *skin_val;
  135. if (key_val[2] == NULL)
  136. skin_val = g_strdup_printf ("%s;", key_val[1]);
  137. else if (key_val[3] == NULL)
  138. skin_val = g_strdup_printf ("%s;%s", key_val[1], key_val[2]);
  139. else
  140. skin_val = g_strdup_printf ("%s;%s;%s", key_val[1], key_val[2], key_val[3]);
  141. mc_config_set_string (mc_skin->config, skin_group, skin_key, skin_val);
  142. g_free (skin_val);
  143. }
  144. g_strfreev (key_val);
  145. }
  146. g_strfreev (orig_colors);
  147. }
  148. /* --------------------------------------------------------------------------------------------- */
  149. /*** public functions ****************************************************************************/
  150. /* --------------------------------------------------------------------------------------------- */
  151. void
  152. mc_skin_colors_old_configure (mc_skin_t * mc_skin)
  153. {
  154. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.setup_color_string);
  155. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.term_color_string);
  156. mc_skin_colors_old_configure_one (mc_skin, getenv ("MC_COLOR_TABLE"));
  157. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.command_line_colors);
  158. }
  159. /* --------------------------------------------------------------------------------------------- */