Merge branch '4549_subshell_execl_argv0'
[midnight-commander.git] / src / selcodepage.c
bloba9fa06d607cfa2c10dbdbb71bc0f39601ab79f7c
1 /*
2 User interface for charset selection.
4 Copyright (C) 2001 Walery Studennikov <despair@sama.ru>
6 Copyright (C) 2011-2024
7 Free Software Foundation, Inc.
9 Written by:
10 Walery Studennikov <despair@sama.ru>, 2001
12 This file is part of the Midnight Commander.
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.
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.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file selcodepage.c
29 * \brief Source: user %interface for charset %selection
32 #include <config.h>
34 #include <stdio.h>
35 #include <stdlib.h>
37 #include "lib/global.h"
38 #include "lib/widget.h"
39 #include "lib/charsets.h"
41 #include "setup.h"
43 #include "selcodepage.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 #define ENTRY_LEN 30
51 /*** file scope type declarations ****************************************************************/
53 /*** forward declarations (file scope functions) *************************************************/
55 /*** file scope variables ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
61 static unsigned char
62 get_hotkey (int n)
64 return (n <= 9) ? '0' + n : 'a' + n - 10;
67 /* --------------------------------------------------------------------------------------------- */
68 /*** public functions ****************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
71 /* Return value:
72 * -2 (SELECT_CHARSET_CANCEL) : Cancel
73 * -1 (SELECT_CHARSET_OTHER_8BIT) : "Other 8 bit" if seldisplay == TRUE
74 * -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" if seldisplay == FALSE
75 * >= 0 : charset number
77 int
78 select_charset (int center_y, int center_x, int current_charset, gboolean seldisplay)
80 Listbox *listbox;
81 size_t i;
82 int listbox_result;
83 char buffer[255];
85 /* Create listbox */
86 listbox =
87 listbox_window_centered_new (center_y, center_x, codepages->len + 1, ENTRY_LEN + 2,
88 _("Choose codepage"), "[Codepages Translation]");
90 if (!seldisplay)
91 LISTBOX_APPEND_TEXT (listbox, '-', _("- < No translation >"), NULL, FALSE);
93 /* insert all the items found */
94 for (i = 0; i < codepages->len; i++)
96 const char *name;
98 name = ((codepage_desc *) g_ptr_array_index (codepages, i))->name;
99 g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i), name);
100 LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL, FALSE);
103 if (seldisplay)
105 unsigned char hotkey;
107 hotkey = get_hotkey (codepages->len);
108 g_snprintf (buffer, sizeof (buffer), "%c %s", hotkey, _("Other 8 bit"));
109 LISTBOX_APPEND_TEXT (listbox, hotkey, buffer, NULL, FALSE);
112 /* Select the default entry */
113 i = seldisplay
114 ? ((current_charset < 0) ? codepages->len : (size_t) current_charset)
115 : ((size_t) current_charset + 1);
117 listbox_set_current (listbox->list, i);
119 listbox_result = listbox_run (listbox);
121 if (listbox_result < 0)
122 /* Cancel dialog */
123 return SELECT_CHARSET_CANCEL;
125 /* some charset has been selected */
126 if (seldisplay)
127 /* charset list is finished with "Other 8 bit" item */
128 return (listbox_result >=
129 (int) codepages->len) ? SELECT_CHARSET_OTHER_8BIT : listbox_result;
131 /* charset list is began with "- < No translation >" item */
132 return (listbox_result - 1);
135 /* --------------------------------------------------------------------------------------------- */
137 /** Set codepage */
138 gboolean
139 do_set_codepage (int codepage)
141 char *errmsg;
142 gboolean ret;
144 mc_global.source_codepage = codepage;
145 errmsg = init_translation_table (codepage == SELECT_CHARSET_NO_TRANSLATE ?
146 mc_global.display_codepage : mc_global.source_codepage,
147 mc_global.display_codepage);
148 ret = errmsg == NULL;
150 if (!ret)
152 message (D_ERROR, MSG_ERROR, "%s", errmsg);
153 g_free (errmsg);
156 return ret;
159 /* --------------------------------------------------------------------------------------------- */
161 /** Show menu selecting codepage */
162 gboolean
163 do_select_codepage (void)
165 int r;
167 r = select_charset (-1, -1, default_source_codepage, FALSE);
168 if (r == SELECT_CHARSET_CANCEL)
169 return FALSE;
171 default_source_codepage = r;
172 return do_set_codepage (default_source_codepage);
175 /* --------------------------------------------------------------------------------------------- */