Merge branch '1396_with_search_engine'
[midnight-commander.git] / src / selcodepage.c
bloba3be1a1d45a1e23e24f69fafafd6244ea8545ed8
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 /** \file selcodepage.c
21 * \brief Source: user %interface for charset %selection
24 #include <config.h>
26 #ifdef HAVE_CHARSET
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "global.h"
32 #include "dialog.h"
33 #include "widget.h"
34 #include "wtools.h"
35 #include "charsets.h"
36 #include "selcodepage.h"
37 #include "main.h"
39 #define ENTRY_LEN 30
41 /* Numbers of (file I/O) and (input/display) codepages. -1 if not selected */
42 int source_codepage = -1;
43 int display_codepage = -1;
45 static unsigned char
46 get_hotkey (int n)
48 return (n <= 9) ? '0' + n : 'a' + n - 10;
51 /* Return value:
52 * -2 (SELECT_CHARSET_CANCEL) : Cancel
53 * -1 (SELECT_CHARSET_OTHER_8BIT) : "Other 8 bit" if seldisplay == TRUE
54 * -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" if seldisplay == FALSE
55 * >= 0 : charset number
57 int
58 select_charset (int center_y, int center_x, int current_charset, gboolean seldisplay)
60 int i;
61 char buffer[255];
63 /* Create listbox */
64 Listbox *listbox = create_listbox_window_centered (center_y, center_x,
65 n_codepages + 1, ENTRY_LEN + 2,
66 _("Choose codepage"),
67 "[Codepages Translation]");
69 if (!seldisplay)
70 LISTBOX_APPEND_TEXT (listbox, '-', _("- < No translation >"),
71 NULL);
73 /* insert all the items found */
74 for (i = 0; i < n_codepages; i++) {
75 char *name = codepages[i].name;
76 g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i),
77 name);
78 LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL);
80 if (seldisplay) {
81 g_snprintf (buffer, sizeof (buffer), "%c %s",
82 get_hotkey (n_codepages), _("Other 8 bit"));
83 LISTBOX_APPEND_TEXT (listbox, get_hotkey (n_codepages), buffer,
84 NULL);
87 /* Select the default entry */
88 i = (seldisplay)
89 ? ((current_charset < 0) ? n_codepages : current_charset)
90 : (current_charset + 1);
92 listbox_select_by_number (listbox->list, i);
94 i = run_listbox (listbox);
96 if (i < 0) {
97 /* Cancel dialog */
98 return SELECT_CHARSET_CANCEL;
99 } else {
100 /* some charset has been selected */
101 if (seldisplay) {
102 /* charset list is finished with "Other 8 bit" item */
103 return (i >= n_codepages) ? SELECT_CHARSET_OTHER_8BIT : i;
104 } else {
105 /* charset list is began with "- < No translation >" item */
106 return (i - 1);
111 gboolean
112 do_select_codepage (void)
114 const char *errmsg = NULL;
115 int r;
117 r = select_charset (-1, -1, source_codepage, FALSE);
118 if (r == SELECT_CHARSET_CANCEL)
119 return FALSE;
121 source_codepage = r;
123 errmsg = init_translation_table (r == SELECT_CHARSET_NO_TRANSLATE ?
124 display_codepage : source_codepage,
125 display_codepage);
126 if (errmsg != NULL)
127 message (D_ERROR, MSG_ERROR, "%s", errmsg);
129 return (errmsg == NULL);
132 #endif /* HAVE_CHARSET */