colors-old.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Skins engine.
  3. Work with colors - backward compatibility
  4. Copyright (C) 2009-2024
  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. /*** forward declarations (file scope functions) *************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. typedef struct mc_skin_colors_old_struct
  33. {
  34. const char *old_color;
  35. const char *group;
  36. const char *key;
  37. } mc_skin_colors_old_t;
  38. /*** file scope variables ************************************************************************/
  39. /* keep this table alphabetically sorted */
  40. static const mc_skin_colors_old_t old_colors[] = {
  41. {"bbarbutton", "buttonbar", "button"},
  42. {"bbarhotkey", "buttonbar", "hotkey"},
  43. {"commandlinemark", "core", "commandlinemark"},
  44. {"dfocus", "dialog", "dfocus"},
  45. {"dhotfocus", "dialog", "dhotfocus"},
  46. {"dhotnormal", "dialog", "dhotnormal"},
  47. {"disabled", "core", "disabled"},
  48. {"dnormal", "dialog", "_default_"},
  49. {"editbg", "editor", "editbg"},
  50. {"editbold", "editor", "editbold"},
  51. {"editframe", "editor", "editframe"},
  52. {"editframeactive", "editor", "editframeactive"},
  53. {"editframedrag", "editor", "editframedrag"},
  54. {"editlinestate", "editor", "editlinestate"},
  55. {"editmarked", "editor", "editmarked"},
  56. {"editnonprintable", "editor", "editnonprintable"},
  57. {"editnormal", "editor", "_default_"},
  58. {"editwhitespace", "editor", "editwhitespace"},
  59. {"errdhotfocus", "error", "errdhotfocus"},
  60. {"errdhotnormal", "error", "errdhotnormal"},
  61. {"errors", "error", "_default_"},
  62. {"gauge", "core", "gauge"},
  63. {"header", "core", "header"},
  64. {"helpbold", "help", "helpbold"},
  65. {"helpitalic", "help", "helpitalic"},
  66. {"helplink", "help", "helplink"},
  67. {"helpnormal", "help", "_default_"},
  68. {"helpslink", "help", "helpslink"},
  69. {"input", "core", "input"},
  70. {"inputmark", "core", "inputmark"},
  71. {"inputunchanged", "core", "inputunchanged"},
  72. {"marked", "core", "marked"},
  73. {"markselect", "core", "markselect"},
  74. {"menuhot", "menu", "menuhot"},
  75. {"menuhotsel", "menu", "menuhotsel"},
  76. {"menuinactive", "menu", "menuinactive"},
  77. {"menunormal", "menu", "_default_"},
  78. {"menusel", "menu", "menusel"},
  79. {"normal", "core", "_default_"},
  80. {"pmenunormal", "popupmenu", "_default_"},
  81. {"pmenusel", "popupmenu", "menusel"},
  82. {"pmenutitle", "popupmenu", "menutitle"},
  83. {"reverse", "core", "reverse"},
  84. {"selected", "core", "selected"},
  85. {"statusbar", "statusbar", "_default_"},
  86. {"viewbold", "viewer", "viewbold"},
  87. {"viewnormal", "viewer", "_default_"},
  88. {"viewselected", "viewer", "viewselected"},
  89. {"viewunderline", "viewer", "viewunderline"}
  90. };
  91. static const size_t num_old_colors = G_N_ELEMENTS (old_colors);
  92. /* --------------------------------------------------------------------------------------------- */
  93. /*** file scope functions ************************************************************************/
  94. /* --------------------------------------------------------------------------------------------- */
  95. static int
  96. old_color_comparator (const void *p1, const void *p2)
  97. {
  98. const mc_skin_colors_old_t *m1 = (const mc_skin_colors_old_t *) p1;
  99. const mc_skin_colors_old_t *m2 = (const mc_skin_colors_old_t *) p2;
  100. return strcmp (m1->old_color, m2->old_color);
  101. }
  102. /* --------------------------------------------------------------------------------------------- */
  103. static gboolean
  104. mc_skin_colors_old_transform (const char *old_color, const char **group, const char **key)
  105. {
  106. const mc_skin_colors_old_t oc = { old_color, NULL, NULL };
  107. mc_skin_colors_old_t *res;
  108. if (old_color == NULL)
  109. return FALSE;
  110. res = (mc_skin_colors_old_t *) bsearch (&oc, old_colors, num_old_colors,
  111. sizeof (old_colors[0]), old_color_comparator);
  112. if (res == NULL)
  113. return FALSE;
  114. if (group != NULL)
  115. *group = res->group;
  116. if (key != NULL)
  117. *key = res->key;
  118. return TRUE;
  119. }
  120. /* --------------------------------------------------------------------------------------------- */
  121. static void
  122. mc_skin_colors_old_configure_one (mc_skin_t *mc_skin, const char *the_color_string)
  123. {
  124. gchar **colors, **orig_colors;
  125. if (the_color_string == NULL)
  126. return;
  127. orig_colors = g_strsplit (the_color_string, ":", -1);
  128. if (orig_colors == NULL)
  129. return;
  130. for (colors = orig_colors; *colors != NULL; colors++)
  131. {
  132. gchar **key_val;
  133. const gchar *skin_group, *skin_key;
  134. key_val = g_strsplit_set (*colors, "=,", 4);
  135. if (key_val == NULL)
  136. continue;
  137. if (key_val[1] != NULL && mc_skin_colors_old_transform (key_val[0], &skin_group, &skin_key))
  138. {
  139. gchar *skin_val;
  140. if (key_val[2] == NULL)
  141. skin_val = g_strdup_printf ("%s;", key_val[1]);
  142. else if (key_val[3] == NULL)
  143. skin_val = g_strdup_printf ("%s;%s", key_val[1], key_val[2]);
  144. else
  145. skin_val = g_strdup_printf ("%s;%s;%s", key_val[1], key_val[2], key_val[3]);
  146. mc_config_set_string (mc_skin->config, skin_group, skin_key, skin_val);
  147. g_free (skin_val);
  148. }
  149. g_strfreev (key_val);
  150. }
  151. g_strfreev (orig_colors);
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. /*** public functions ****************************************************************************/
  155. /* --------------------------------------------------------------------------------------------- */
  156. void
  157. mc_skin_colors_old_configure (mc_skin_t *mc_skin)
  158. {
  159. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.setup_color_string);
  160. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.term_color_string);
  161. mc_skin_colors_old_configure_one (mc_skin, getenv ("MC_COLOR_TABLE"));
  162. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.command_line_colors);
  163. }
  164. /* --------------------------------------------------------------------------------------------- */