colors-old.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Skins engine.
  3. Work with colors - backward compability
  4. Copyright (C) 2009, 2010 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009
  7. Egmont Koblinger <egmont@gmail.com>, 2010
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software; you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation; either version 2 of the
  12. License, or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be
  14. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. MA 02110-1301, USA.
  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. #include "src/setup.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  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. {"editbold", "editor", "editbold"},
  50. {"editlinestate", "editor", "editlinestate"},
  51. {"editmarked", "editor", "editmarked"},
  52. {"editnormal", "editor", "_default_"},
  53. {"editwhitespace", "editor", "editwhitespace"},
  54. {"errdhotfocus", "error", "errdhotfocus"},
  55. {"errdhotnormal", "error", "errdhotnormal"},
  56. {"errors", "error", "_default_"},
  57. {"gauge", "core", "gauge"},
  58. {"header", "core", "header"},
  59. {"helpbold", "help", "helpbold"},
  60. {"helpitalic", "help", "helpitalic"},
  61. {"helplink", "help", "helplink"},
  62. {"helpnormal", "help", "_default_"},
  63. {"helpslink", "help", "helpslink"},
  64. {"input", "core", "input"},
  65. {"inputmark", "core", "inputmark"},
  66. {"inputunchanged", "core", "inputunchanged"},
  67. {"marked", "core", "marked"},
  68. {"markselect", "core", "markselect"},
  69. {"menuhot", "menu", "menuhot"},
  70. {"menuhotsel", "menu", "menuhotsel"},
  71. {"menuinactive", "menu", "menuinactive"},
  72. {"menunormal", "menu", "_default_"},
  73. {"menusel", "menu", "menusel"},
  74. {"normal", "core", "_default_"},
  75. {"pmenunormal", "popupmenu", "_default_"},
  76. {"pmenusel", "popupmenu", "menusel"},
  77. {"pmenutitle", "popupmenu", "menutitle"},
  78. {"reverse", "core", "reverse"},
  79. {"selected", "core", "selected"},
  80. {"statusbar", "statusbar", "_default_"},
  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. gchar **key_val;
  116. const gchar *skin_group, *skin_key;
  117. gchar *skin_val;
  118. if (the_color_string == NULL)
  119. return;
  120. orig_colors = colors = g_strsplit (the_color_string, ":", -1);
  121. if (colors == NULL)
  122. return;
  123. for (; *colors; colors++)
  124. {
  125. key_val = g_strsplit_set (*colors, "=,", 4);
  126. if (!key_val)
  127. continue;
  128. if (key_val[1] == NULL
  129. || !mc_skin_colors_old_transform (key_val[0], &skin_group, &skin_key))
  130. {
  131. g_strfreev (key_val);
  132. continue;
  133. }
  134. if (key_val[3] != NULL)
  135. skin_val = g_strdup_printf ("%s;%s;%s", key_val[1], key_val[2], key_val[3]);
  136. else if (key_val[2] != NULL)
  137. skin_val = g_strdup_printf ("%s;%s", key_val[1], key_val[2]);
  138. else
  139. skin_val = g_strdup_printf ("%s;", key_val[1]);
  140. mc_config_set_string (mc_skin->config, skin_group, skin_key, skin_val);
  141. g_free (skin_val);
  142. g_strfreev (key_val);
  143. }
  144. g_strfreev (orig_colors);
  145. }
  146. /* --------------------------------------------------------------------------------------------- */
  147. /*** public functions ****************************************************************************/
  148. /* --------------------------------------------------------------------------------------------- */
  149. void
  150. mc_skin_colors_old_configure (mc_skin_t * mc_skin)
  151. {
  152. mc_skin_colors_old_configure_one (mc_skin, setup_color_string);
  153. mc_skin_colors_old_configure_one (mc_skin, term_color_string);
  154. mc_skin_colors_old_configure_one (mc_skin, getenv ("MC_COLOR_TABLE"));
  155. mc_skin_colors_old_configure_one (mc_skin, command_line_colors);
  156. }
  157. /* --------------------------------------------------------------------------------------------- */