selcodepage.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. User interface for charset selection.
  3. Copyright (C) 2001 Walery Studennikov <despair@sama.ru>
  4. Copyright (C) 2011-2024
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Walery Studennikov <despair@sama.ru>, 2001
  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 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU 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, see <http://www.gnu.org/licenses/>.
  19. */
  20. /** \file selcodepage.c
  21. * \brief Source: user %interface for charset %selection
  22. */
  23. #include <config.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include "lib/global.h"
  27. #include "lib/widget.h"
  28. #include "lib/charsets.h"
  29. #include "setup.h"
  30. #include "selcodepage.h"
  31. /*** global variables ****************************************************************************/
  32. /*** file scope macro definitions ****************************************************************/
  33. #define ENTRY_LEN 30
  34. /*** file scope type declarations ****************************************************************/
  35. /*** forward declarations (file scope functions) *************************************************/
  36. /*** file scope variables ************************************************************************/
  37. /* --------------------------------------------------------------------------------------------- */
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. static unsigned char
  41. get_hotkey (int n)
  42. {
  43. return (n <= 9) ? '0' + n : 'a' + n - 10;
  44. }
  45. /* --------------------------------------------------------------------------------------------- */
  46. /*** public functions ****************************************************************************/
  47. /* --------------------------------------------------------------------------------------------- */
  48. /* Return value:
  49. * -2 (SELECT_CHARSET_CANCEL) : Cancel
  50. * -1 (SELECT_CHARSET_OTHER_8BIT) : "Other 8 bit" if seldisplay == TRUE
  51. * -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" if seldisplay == FALSE
  52. * >= 0 : charset number
  53. */
  54. int
  55. select_charset (int center_y, int center_x, int current_charset, gboolean seldisplay)
  56. {
  57. Listbox *listbox;
  58. size_t i;
  59. int listbox_result;
  60. char buffer[255];
  61. /* Create listbox */
  62. listbox =
  63. listbox_window_centered_new (center_y, center_x, codepages->len + 1, ENTRY_LEN + 2,
  64. _("Choose codepage"), "[Codepages Translation]");
  65. if (!seldisplay)
  66. LISTBOX_APPEND_TEXT (listbox, '-', _("- < No translation >"), NULL, FALSE);
  67. /* insert all the items found */
  68. for (i = 0; i < codepages->len; i++)
  69. {
  70. const char *name;
  71. name = ((codepage_desc *) g_ptr_array_index (codepages, i))->name;
  72. g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i), name);
  73. LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL, FALSE);
  74. }
  75. if (seldisplay)
  76. {
  77. unsigned char hotkey;
  78. hotkey = get_hotkey (codepages->len);
  79. g_snprintf (buffer, sizeof (buffer), "%c %s", hotkey, _("Other 8 bit"));
  80. LISTBOX_APPEND_TEXT (listbox, hotkey, buffer, NULL, FALSE);
  81. }
  82. /* Select the default entry */
  83. i = seldisplay
  84. ? ((current_charset < 0) ? codepages->len : (size_t) current_charset)
  85. : ((size_t) current_charset + 1);
  86. listbox_set_current (listbox->list, i);
  87. listbox_result = listbox_run (listbox);
  88. if (listbox_result < 0)
  89. /* Cancel dialog */
  90. return SELECT_CHARSET_CANCEL;
  91. /* some charset has been selected */
  92. if (seldisplay)
  93. /* charset list is finished with "Other 8 bit" item */
  94. return (listbox_result >=
  95. (int) codepages->len) ? SELECT_CHARSET_OTHER_8BIT : listbox_result;
  96. /* charset list is began with "- < No translation >" item */
  97. return (listbox_result - 1);
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. /** Set codepage */
  101. gboolean
  102. do_set_codepage (int codepage)
  103. {
  104. char *errmsg;
  105. gboolean ret;
  106. mc_global.source_codepage = codepage;
  107. errmsg = init_translation_table (codepage == SELECT_CHARSET_NO_TRANSLATE ?
  108. mc_global.display_codepage : mc_global.source_codepage,
  109. mc_global.display_codepage);
  110. ret = errmsg == NULL;
  111. if (!ret)
  112. {
  113. message (D_ERROR, MSG_ERROR, "%s", errmsg);
  114. g_free (errmsg);
  115. }
  116. return ret;
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. /** Show menu selecting codepage */
  120. gboolean
  121. do_select_codepage (void)
  122. {
  123. int r;
  124. r = select_charset (-1, -1, default_source_codepage, FALSE);
  125. if (r == SELECT_CHARSET_CANCEL)
  126. return FALSE;
  127. default_source_codepage = r;
  128. return do_set_codepage (default_source_codepage);
  129. }
  130. /* --------------------------------------------------------------------------------------------- */