colors-old.c 6.6 KB

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