1 /* 2 User interface for charset selection. 3 4 Copyright (C) 2001 Walery Studennikov <despair@sama.ru> 5 6 Copyright (C) 2011-2025 7 Free Software Foundation, Inc. 8 9 Written by: 10 Walery Studennikov <despair@sama.ru>, 2001 11 12 This file is part of the Midnight Commander. 13 14 The Midnight Commander is free software: you can redistribute it 15 and/or modify it under the terms of the GNU General Public License as 16 published by the Free Software Foundation, either version 3 of the License, 17 or (at your option) any later version. 18 19 The Midnight Commander is distributed in the hope that it will be useful, 20 but WITHOUT ANY WARRANTY; without even the implied warranty of 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 GNU General Public License for more details. 23 24 You should have received a copy of the GNU General Public License 25 along with this program. If not, see <https://www.gnu.org/licenses/>. 26 */ 27 28 /** \file selcodepage.c 29 * \brief Source: user %interface for charset %selection 30 */ 31 32 #include <config.h> 33 34 #include <stdio.h> 35 #include <stdlib.h> 36 37 #include "lib/global.h" 38 #include "lib/widget.h" 39 #include "lib/charsets.h" 40 41 #include "setup.h" 42 43 #include "selcodepage.h" 44 45 /*** global variables ****************************************************************************/ 46 47 /*** file scope macro definitions ****************************************************************/ 48 49 #define ENTRY_LEN 30 50 51 /*** file scope type declarations ****************************************************************/ 52 53 /*** forward declarations (file scope functions) *************************************************/ 54 55 /*** file scope variables ************************************************************************/ 56 57 /* --------------------------------------------------------------------------------------------- */ 58 /*** file scope functions ************************************************************************/ 59 /* --------------------------------------------------------------------------------------------- */ 60 61 static unsigned char 62 get_hotkey (int n) /**/ 63 { 64 return (n <= 9) ? '0' + n : 'a' + n - 10; 65 } 66 67 /* --------------------------------------------------------------------------------------------- */ 68 /*** public functions ****************************************************************************/ 69 /* --------------------------------------------------------------------------------------------- */ 70 71 /* Return value: 72 * -2 (SELECT_CHARSET_CANCEL) : Cancel 73 * -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" 74 * >= 0 : charset number 75 */ 76 int 77 select_charset (const int center_y, const int center_x, const int current_charset) /*
*/ 78 { 79 Listbox *listbox; 80 81 listbox = listbox_window_centered_new (center_y, center_x, codepages->len + 1, ENTRY_LEN + 2, 82 _ ("Choose codepage"), "[Codepages Translation]"); 83 84 LISTBOX_APPEND_TEXT (listbox, '-', _ ("- < No translation >"), NULL, FALSE); 85 86 for (guint i = 0; i < codepages->len; i++) 87 { 88 char buffer[BUF_SMALL]; 89 90 const char *name = ((codepage_desc *) g_ptr_array_index (codepages, i))->name; 91 g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i), name); 92 LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL, FALSE); 93 } 94 95 listbox_set_current (listbox->list, current_charset + 1); 96 97 const int listbox_result = listbox_run (listbox); 98 99 return listbox_result < 0 ? SELECT_CHARSET_CANCEL : listbox_result - 1; 100 } 101 102 /* --------------------------------------------------------------------------------------------- */ 103 104 /** Set codepage */ 105 gboolean 106 do_set_codepage (const int codepage) /*
*/ 107 { 108 char *errmsg; 109 gboolean ret; 110 111 mc_global.source_codepage = codepage; 112 errmsg = 113 init_translation_table (codepage == SELECT_CHARSET_NO_TRANSLATE ? mc_global.display_codepage 114 : mc_global.source_codepage, 115 mc_global.display_codepage); 116 ret = errmsg == NULL; 117 118 if (!ret) 119 { 120 message (D_ERROR, MSG_ERROR, "%s", errmsg); 121 g_free (errmsg); 122 } 123 124 return ret; 125 } 126 127 /* --------------------------------------------------------------------------------------------- */ 128 129 /** Show menu selecting codepage */ 130 gboolean 131 do_select_codepage (void) /*
*/ 132 { 133 const int r = select_charset (-1, -1, default_source_codepage); 134 135 if (r == SELECT_CHARSET_CANCEL) 136 return FALSE; 137 138 default_source_codepage = r; 139 return do_set_codepage (default_source_codepage); 140 } 141 142 /* --------------------------------------------------------------------------------------------- */