colors-old.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /*** 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. {"editbold", "editor", "editbold"},
  49. {"editlinestate", "editor", "editlinestate"},
  50. {"editmarked", "editor", "editmarked"},
  51. {"editnormal", "editor", "_default_"},
  52. {"editwhitespace", "editor", "editwhitespace"},
  53. {"errdhotfocus", "error", "errdhotfocus"},
  54. {"errdhotnormal", "error", "errdhotnormal"},
  55. {"errors", "error", "_default_"},
  56. {"gauge", "core", "gauge"},
  57. {"header", "core", "header"},
  58. {"helpbold", "help", "helpbold"},
  59. {"helpitalic", "help", "helpitalic"},
  60. {"helplink", "help", "helplink"},
  61. {"helpnormal", "help", "_default_"},
  62. {"helpslink", "help", "helpslink"},
  63. {"input", "core", "input"},
  64. {"inputmark", "core", "inputmark"},
  65. {"inputunchanged", "core", "inputunchanged"},
  66. {"marked", "core", "marked"},
  67. {"markselect", "core", "markselect"},
  68. {"menuhot", "menu", "menuhot"},
  69. {"menuhotsel", "menu", "menuhotsel"},
  70. {"menuinactive", "menu", "menuinactive"},
  71. {"menunormal", "menu", "_default_"},
  72. {"menusel", "menu", "menusel"},
  73. {"normal", "core", "_default_"},
  74. {"pmenunormal", "popupmenu", "_default_"},
  75. {"pmenusel", "popupmenu", "menusel"},
  76. {"pmenutitle", "popupmenu", "menutitle"},
  77. {"reverse", "core", "reverse"},
  78. {"selected", "core", "selected"},
  79. {"statusbar", "statusbar", "_default_"},
  80. {"viewbold", "viewer", "viewbold"},
  81. {"viewselected", "viewer", "viewselected"},
  82. {"viewunderline", "viewer", "viewunderline"}
  83. };
  84. static const size_t num_old_colors = G_N_ELEMENTS (old_colors);
  85. /*** file scope functions ************************************************************************/
  86. static int
  87. old_color_comparator (const void *p1, const void *p2)
  88. {
  89. const mc_skin_colors_old_t *m1 = (const mc_skin_colors_old_t *) p1;
  90. const mc_skin_colors_old_t *m2 = (const mc_skin_colors_old_t *) p2;
  91. return strcmp (m1->old_color, m2->old_color);
  92. }
  93. /* --------------------------------------------------------------------------------------------- */
  94. static gboolean
  95. mc_skin_colors_old_transform (const char *old_color, const char **group, const char **key)
  96. {
  97. const mc_skin_colors_old_t oc = { old_color, NULL, NULL };
  98. mc_skin_colors_old_t *res;
  99. if (old_color == NULL)
  100. return FALSE;
  101. res = (mc_skin_colors_old_t *) bsearch (&oc, old_colors, num_old_colors,
  102. sizeof (old_colors[0]), old_color_comparator);
  103. if (res == NULL)
  104. return FALSE;
  105. if (group != NULL)
  106. *group = res->group;
  107. if (key != NULL)
  108. *key = res->key;
  109. return TRUE;
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. static void
  113. mc_skin_colors_old_configure_one (mc_skin_t * mc_skin, const char *the_color_string)
  114. {
  115. gchar **colors, **orig_colors;
  116. if (the_color_string == NULL)
  117. return;
  118. orig_colors = colors = g_strsplit (the_color_string, ":", -1);
  119. if (colors == NULL)
  120. return;
  121. for (; *colors != NULL; colors++)
  122. {
  123. gchar **key_val;
  124. const gchar *skin_group, *skin_key;
  125. key_val = g_strsplit_set (*colors, "=,", 4);
  126. if (key_val == NULL)
  127. continue;
  128. if (key_val[1] != NULL && mc_skin_colors_old_transform (key_val[0], &skin_group, &skin_key))
  129. {
  130. gchar *skin_val;
  131. if (key_val[2] == NULL)
  132. skin_val = g_strdup_printf ("%s;", key_val[1]);
  133. else if (key_val[3] == NULL)
  134. skin_val = g_strdup_printf ("%s;%s", key_val[1], key_val[2]);
  135. else
  136. skin_val = g_strdup_printf ("%s;%s;%s", key_val[1], key_val[2], key_val[3]);
  137. mc_config_set_string (mc_skin->config, skin_group, skin_key, skin_val);
  138. g_free (skin_val);
  139. }
  140. g_strfreev (key_val);
  141. }
  142. g_strfreev (orig_colors);
  143. }
  144. /* --------------------------------------------------------------------------------------------- */
  145. /*** public functions ****************************************************************************/
  146. /* --------------------------------------------------------------------------------------------- */
  147. void
  148. mc_skin_colors_old_configure (mc_skin_t * mc_skin)
  149. {
  150. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.setup_color_string);
  151. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.term_color_string);
  152. mc_skin_colors_old_configure_one (mc_skin, getenv ("MC_COLOR_TABLE"));
  153. mc_skin_colors_old_configure_one (mc_skin, mc_global.tty.command_line_colors);
  154. }
  155. /* --------------------------------------------------------------------------------------------- */