utilunix.c(mc_tmpdir): Check return value of getpwuid() for NULL.
[midnight-commander.git] / src / selcodepage.c
blob7b6c795382d878d297da5769cecef0d7a9cfd7a8
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <config.h>
22 #ifdef HAVE_CHARSET
23 #include <stdlib.h>
24 #include <stdio.h>
26 #include "global.h"
27 #include "dialog.h"
28 #include "widget.h"
29 #include "wtools.h"
30 #include "charsets.h"
31 #include "selcodepage.h"
32 #include "main.h"
34 #define ENTRY_LEN 35
36 /* Numbers of (file I/O) and (input/display) codepages. -1 if not selected */
37 int source_codepage = -1;
38 int display_codepage = -1;
40 static unsigned char
41 get_hotkey (int n)
43 return (n <= 9) ? '0' + n : 'a' + n - 10;
46 int
47 select_charset (int current_charset, int seldisplay)
49 int i, menu_lines = n_codepages + 1;
50 char buffer[255];
52 /* Create listbox */
53 Listbox *listbox = create_listbox_window (ENTRY_LEN + 2, menu_lines,
54 _(" Choose input codepage "),
55 "[Codepages Translation]");
57 if (!seldisplay)
58 LISTBOX_APPEND_TEXT (listbox, '-', _("- < No translation >"),
59 NULL);
61 /* insert all the items found */
62 for (i = 0; i < n_codepages; i++) {
63 char *name = codepages[i].name;
64 g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i),
65 name);
66 LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL);
68 if (seldisplay) {
69 g_snprintf (buffer, sizeof (buffer), "%c %s",
70 get_hotkey (n_codepages), _("Other 8 bit"));
71 LISTBOX_APPEND_TEXT (listbox, get_hotkey (n_codepages), buffer,
72 NULL);
75 /* Select the default entry */
76 i = (seldisplay)
77 ? ((current_charset < 0) ? n_codepages : current_charset)
78 : (current_charset + 1);
80 listbox_select_by_number (listbox->list, i);
82 i = run_listbox (listbox);
84 return (seldisplay) ? ((i >= n_codepages) ? -1 : i)
85 : (i - 1);
88 /* Helper functions for codepages support */
91 int
92 do_select_codepage (void)
94 char *errmsg;
96 if (display_codepage > 0) {
97 source_codepage = select_charset (source_codepage, 0);
98 errmsg =
99 init_translation_table (source_codepage, display_codepage);
100 if (errmsg) {
101 message (1, MSG_ERROR, "%s", errmsg);
102 return -1;
104 } else {
105 message (1, _("Warning"),
106 _("To use this feature select your codepage in\n"
107 "Setup / Display Bits dialog!\n"
108 "Do not forget to save options."));
109 return -1;
111 return 0;
114 #endif /* HAVE_CHARSET */