colors-old.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Skins engine.
  3. Work with colors - backward compability
  4. Copyright (C) 2009 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software; you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be
  13. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. MA 02110-1301, USA.
  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. #include "src/setup.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. gchar **key_val;
  117. const gchar *skin_group, *skin_key;
  118. gchar *skin_val;
  119. if (the_color_string == NULL)
  120. return;
  121. orig_colors = colors = g_strsplit (the_color_string, ":", -1);
  122. if (colors == NULL)
  123. return;
  124. for (; *colors; colors++)
  125. {
  126. key_val = g_strsplit_set (*colors, "=,", 3);
  127. if (!key_val)
  128. continue;
  129. if (key_val[1] == NULL
  130. || !mc_skin_colors_old_transform (key_val[0], &skin_group, &skin_key))
  131. {
  132. g_strfreev (key_val);
  133. continue;
  134. }
  135. if (key_val[2] != NULL)
  136. skin_val = g_strdup_printf ("%s;%s", key_val[1], key_val[2]);
  137. else
  138. skin_val = g_strdup_printf ("%s;", key_val[1]);
  139. mc_config_set_string (mc_skin->config, skin_group, skin_key, skin_val);
  140. g_free (skin_val);
  141. g_strfreev (key_val);
  142. }
  143. g_strfreev (orig_colors);
  144. }
  145. /* --------------------------------------------------------------------------------------------- */
  146. /*** public functions ****************************************************************************/
  147. /* --------------------------------------------------------------------------------------------- */
  148. void
  149. mc_skin_colors_old_configure (mc_skin_t * mc_skin)
  150. {
  151. mc_skin_colors_old_configure_one (mc_skin, setup_color_string);
  152. mc_skin_colors_old_configure_one (mc_skin, term_color_string);
  153. mc_skin_colors_old_configure_one (mc_skin, getenv ("MC_COLOR_TABLE"));
  154. mc_skin_colors_old_configure_one (mc_skin, command_line_colors);
  155. }
  156. /* --------------------------------------------------------------------------------------------- */