Changes in SelectCodepage dialog:
[midnight-commander.git] / src / selcodepage.c
blob25b317038415a661380378cc41508b1e30b55879
1 /* User interface for charset selection.
3 Copyright (C) 2001 Walery Studennikov <despair@sama.ru>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <config.h>
22 #ifdef HAVE_CHARSET
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "global.h"
28 #include "dialog.h"
29 #include "widget.h"
30 #include "wtools.h"
31 #include "charsets.h"
32 #include "selcodepage.h"
33 #include "main.h"
35 #define ENTRY_LEN 35
37 /* Numbers of (file I/O) and (input/display) codepages. -1 if not selected */
38 int source_codepage = -1;
39 int display_codepage = -1;
41 static unsigned char
42 get_hotkey (int n)
44 return (n <= 9) ? '0' + n : 'a' + n - 10;
47 int
48 select_charset (int delta_x, int delta_y, int current_charset, int seldisplay)
50 int i, menu_lines = n_codepages + 1;
51 char buffer[255];
53 /* Create listbox */
54 Listbox *listbox = create_listbox_window_delta (delta_x, delta_y,
55 ENTRY_LEN + 2, menu_lines,
56 _(" Choose input codepage "),
57 "[Codepages Translation]");
59 if (!seldisplay)
60 LISTBOX_APPEND_TEXT (listbox, '-', _("- < No translation >"),
61 NULL);
63 /* insert all the items found */
64 for (i = 0; i < n_codepages; i++) {
65 char *name = codepages[i].name;
66 g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i),
67 name);
68 LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL);
70 if (seldisplay) {
71 g_snprintf (buffer, sizeof (buffer), "%c %s",
72 get_hotkey (n_codepages), _("Other 8 bit"));
73 LISTBOX_APPEND_TEXT (listbox, get_hotkey (n_codepages), buffer,
74 NULL);
77 /* Select the default entry */
78 i = (seldisplay)
79 ? ((current_charset < 0) ? n_codepages : current_charset)
80 : (current_charset + 1);
82 listbox_select_by_number (listbox->list, i);
84 i = run_listbox (listbox);
86 return (seldisplay) ? ((i >= n_codepages) ? -1 : i)
87 : (i - 1);
90 /* Helper functions for codepages support */
93 int
94 do_select_codepage (void)
96 const char *errmsg;
97 int r;
99 r = select_charset (0, 0, source_codepage, 0);
100 if ( r > 0 )
101 source_codepage = r;
103 errmsg = init_translation_table (source_codepage, display_codepage);
104 if (errmsg) {
105 message (D_ERROR, MSG_ERROR, "%s", errmsg);
106 return -1;
108 return 0;
111 #endif /* HAVE_CHARSET */