colors-old.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Skins engine.
  3. Work with colors - backward compatibility
  4. Copyright (C) 2009-2017
  5. 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. {"viewnormal", "viewer", "_default_"},
  86. {"viewselected", "viewer", "viewselected"},
  87. {"viewunderline", "viewer", "viewunderline"}
  88. };
  89. static const size_t num_old_colors = G_N_ELEMENTS (old_colors);
  90. /*** file scope functions ************************************************************************/
  91. static int
  92. old_color_comparator (const void *p1, const void *p2)
  93. {
  94. const mc_skin_colors_old_t *m1 = (const mc_skin_colors_old_t *) p1;
  95. const mc_skin_colors_old_t *m2 = (const mc_skin_colors_old_t *) p2;
  96. return strcmp (m1->old_color, m2->old_color);
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. static gboolean
  100. mc_skin_colors_old_transform (const char *old_color, const char **group, const char **key)
  101. {
  102. const mc_skin_colors_old_t oc = { old_color, NULL, NULL };
  103. mc_skin_colors_old_t *res;
  104. if (old_color == NULL)
  105. return FALSE;
  106. res = (mc_skin_colors_old_t *) bsearch (&oc, old_colors, num_old_colors,
  107. sizeof (old_colors[0]), old_color_comparator);
  108. if (res == NULL)
  109. return FALSE;
  110. if (group != NULL)
  111. *group = res->group;
  112. if (key != NULL)
  113. *key = res->key;
  114. return TRUE;
  115. }
  116. /* --------------------------------------------------------------------------------------------- */
  117. static void
  118. mc_skin_colors_old_configure_one (mc_skin_t * mc_skin, const char *the_color_string)
  119. {
  120. gchar **colors, **orig_colors;
  121. if (the_color_string == NULL)
  122. return;
  123. orig_colors = g_strsplit (the_color_string, ":", -1);
  124. if (orig_colors == NULL)
  125. return;
  126. for (colors = orig_colors; *colors != NULL; colors++)
  127. {
  128. gchar **key_val;
  129. const gchar *skin_group, *skin_key;
  130. key_val = g_strsplit_set (*colors, "=,", 4);
  131. if (key_val == NULL)
  132. continue;
  133. if (key_val[1] != NULL && mc_skin_colors_old_transform (key_val[0], &skin_group, &skin_key))
  134. {
  135. gchar *skin_val;
  136. if (key_val[2] == NULL)
  137. skin_val = g_strdup_printf ("%s;", key_val[1]);
  138. else if (key_val[3] == NULL)
  139. skin_val = g_strdup_printf ("%s;%s", key_val[1], key_val[2]);
  140. else
  141. skin_val = g_strdup_printf ("%s;%s;%s", key_val[1], key_val[2], key_val[3]);
  142. mc_config_set_string (mc_skin->config, skin_group, skin_key, skin_val);
  143. g_free (skin_val);
  144. }
  145. g_strfreev (key_val);
  146. }
  147. g_strfreev (orig_colors);
  148. }
  149. /* --------------------------------------------------------------------------------------------- */
  150. /*** public functions ****************************************************************************/
  151. /* --------------------------------------------------------------------------------------------- */
  152. void
  153. mc_skin_colors_old_configure (mc_skin_t * mc_skin)
  154. {
  155. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.setup_color_string);
  156. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.term_color_string);
  157. mc_skin_colors_old_configure_one (mc_skin, getenv ("MC_COLOR_TABLE"));
  158. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.command_line_colors);
  159. }
  160. /* --------------------------------------------------------------------------------------------- */