Sync with gnulib 6ed53f13bc39d9a0252549e98a2a59441fb2351f.
[midnight-commander.git] / src / selcodepage.c
blob59d970b65de4687af792da861503de3971fd9d90
1 /*
2 User interface for charset selection.
4 Copyright (C) 2001 Walery Studennikov <despair@sama.ru>
6 Copyright (C) 2011-2016
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 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
59 static unsigned char
60 get_hotkey (int n)
62 return (n <= 9) ? '0' + n : 'a' + n - 10;
65 /* --------------------------------------------------------------------------------------------- */
66 /*** public functions ****************************************************************************/
67 /* --------------------------------------------------------------------------------------------- */
69 /* Return value:
70 * -2 (SELECT_CHARSET_CANCEL) : Cancel
71 * -1 (SELECT_CHARSET_OTHER_8BIT) : "Other 8 bit" if seldisplay == TRUE
72 * -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" if seldisplay == FALSE
73 * >= 0 : charset number
75 int
76 select_charset (int center_y, int center_x, int current_charset, gboolean seldisplay)
78 size_t i;
79 int listbox_result;
80 char buffer[255];
82 /* Create listbox */
83 Listbox *listbox = create_listbox_window_centered (center_y, center_x,
84 codepages->len + 1, ENTRY_LEN + 2,
85 _("Choose codepage"),
86 "[Codepages Translation]");
88 if (!seldisplay)
89 LISTBOX_APPEND_TEXT (listbox, '-', _("- < No translation >"), NULL, FALSE);
91 /* insert all the items found */
92 for (i = 0; i < codepages->len; i++)
94 const char *name = ((codepage_desc *) g_ptr_array_index (codepages, i))->name;
95 g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i), name);
96 LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL, FALSE);
98 if (seldisplay)
100 unsigned char hotkey = get_hotkey (codepages->len);
101 g_snprintf (buffer, sizeof (buffer), "%c %s", hotkey, _("Other 8 bit"));
102 LISTBOX_APPEND_TEXT (listbox, hotkey, buffer, NULL, FALSE);
105 /* Select the default entry */
106 i = (seldisplay)
107 ? ((current_charset < 0) ? codepages->len : (size_t) current_charset)
108 : ((size_t) current_charset + 1);
110 listbox_select_entry (listbox->list, i);
112 listbox_result = run_listbox (listbox);
114 if (listbox_result < 0)
116 /* Cancel dialog */
117 return SELECT_CHARSET_CANCEL;
119 else
121 /* some charset has been selected */
122 if (seldisplay)
124 /* charset list is finished with "Other 8 bit" item */
125 return (listbox_result >= (int) codepages->len)
126 ? SELECT_CHARSET_OTHER_8BIT : listbox_result;
128 else
130 /* charset list is began with "- < No translation >" item */
131 return (listbox_result - 1);
137 /* --------------------------------------------------------------------------------------------- */
138 /** Set codepage */
139 gboolean
140 do_set_codepage (int codepage)
142 char *errmsg;
143 gboolean ret;
145 mc_global.source_codepage = codepage;
146 errmsg = init_translation_table (codepage == SELECT_CHARSET_NO_TRANSLATE ?
147 mc_global.display_codepage : mc_global.source_codepage,
148 mc_global.display_codepage);
149 ret = errmsg == NULL;
151 if (!ret)
153 message (D_ERROR, MSG_ERROR, "%s", errmsg);
154 g_free (errmsg);
157 return ret;
160 /* --------------------------------------------------------------------------------------------- */
161 /** Show menu selecting codepage */
163 gboolean
164 do_select_codepage (void)
166 int r;
168 r = select_charset (-1, -1, default_source_codepage, FALSE);
169 if (r == SELECT_CHARSET_CANCEL)
170 return FALSE;
172 default_source_codepage = r;
173 return do_set_codepage (default_source_codepage);
176 /* --------------------------------------------------------------------------------------------- */