Merge branch '2087_standalone_mcedit_crash'
[kaloumi3.git] / src / selcodepage.c
blob69f75b3d34189fc07a7084d18360a99a710d2644
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 "lib/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 default_source_codepage = -1;
44 int display_codepage = -1;
45 char* autodetect_codeset = NULL;
46 gboolean is_autodetect_codeset_enabled = FALSE;
48 static unsigned char
49 get_hotkey (int n)
51 return (n <= 9) ? '0' + n : 'a' + n - 10;
54 /* Return value:
55 * -2 (SELECT_CHARSET_CANCEL) : Cancel
56 * -1 (SELECT_CHARSET_OTHER_8BIT) : "Other 8 bit" if seldisplay == TRUE
57 * -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" if seldisplay == FALSE
58 * >= 0 : charset number
60 int
61 select_charset (int center_y, int center_x, int current_charset, gboolean seldisplay)
63 int i;
64 char buffer[255];
66 /* Create listbox */
67 Listbox *listbox = create_listbox_window_centered (center_y, center_x,
68 n_codepages + 1, ENTRY_LEN + 2,
69 _("Choose codepage"),
70 "[Codepages Translation]");
72 if (!seldisplay)
73 LISTBOX_APPEND_TEXT (listbox, '-', _("- < No translation >"),
74 NULL);
76 /* insert all the items found */
77 for (i = 0; i < n_codepages; i++) {
78 char *name = codepages[i].name;
79 g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i),
80 name);
81 LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL);
83 if (seldisplay) {
84 g_snprintf (buffer, sizeof (buffer), "%c %s",
85 get_hotkey (n_codepages), _("Other 8 bit"));
86 LISTBOX_APPEND_TEXT (listbox, get_hotkey (n_codepages), buffer,
87 NULL);
90 /* Select the default entry */
91 i = (seldisplay)
92 ? ((current_charset < 0) ? n_codepages : current_charset)
93 : (current_charset + 1);
95 listbox_select_entry (listbox->list, i);
97 i = run_listbox (listbox);
99 if (i < 0) {
100 /* Cancel dialog */
101 return SELECT_CHARSET_CANCEL;
102 } else {
103 /* some charset has been selected */
104 if (seldisplay) {
105 /* charset list is finished with "Other 8 bit" item */
106 return (i >= n_codepages) ? SELECT_CHARSET_OTHER_8BIT : i;
107 } else {
108 /* charset list is began with "- < No translation >" item */
109 return (i - 1);
114 /* Set codepage */
115 gboolean
116 do_set_codepage (int codepage)
118 const char *errmsg = NULL;
120 source_codepage = codepage;
121 errmsg = init_translation_table (codepage == SELECT_CHARSET_NO_TRANSLATE ?
122 display_codepage : source_codepage,
123 display_codepage);
124 if (errmsg != NULL)
125 message (D_ERROR, MSG_ERROR, "%s", errmsg);
127 return (errmsg == NULL);
130 /* Show menu selecting codepage */
132 gboolean
133 do_select_codepage (void)
135 int r;
137 r = select_charset (-1, -1, default_source_codepage, FALSE);
138 if (r == SELECT_CHARSET_CANCEL)
139 return FALSE;
141 default_source_codepage = r;
142 return do_set_codepage (default_source_codepage);
145 #endif /* HAVE_CHARSET */