Add edit_add_window() function.
[midnight-commander.git] / src / selcodepage.c
blobd00bfac2d1b238af3d885f2a60e50e06f3b4c712
1 /*
2 User interface for charset selection.
4 Copyright (C) 2001 Walery Studennikov <despair@sama.ru>
6 Copyright (C) 2011
7 The 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 #ifdef HAVE_CHARSET
36 #include <stdio.h>
37 #include <stdlib.h>
39 #include "lib/global.h"
40 #include "lib/widget.h"
41 #include "lib/charsets.h"
43 #include "main.h"
45 #include "selcodepage.h"
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
51 #define ENTRY_LEN 30
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
61 static unsigned char
62 get_hotkey (int n)
64 return (n <= 9) ? '0' + n : 'a' + n - 10;
67 /* --------------------------------------------------------------------------------------------- */
68 /*** public functions ****************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
71 /* Return value:
72 * -2 (SELECT_CHARSET_CANCEL) : Cancel
73 * -1 (SELECT_CHARSET_OTHER_8BIT) : "Other 8 bit" if seldisplay == TRUE
74 * -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" if seldisplay == FALSE
75 * >= 0 : charset number
77 int
78 select_charset (int center_y, int center_x, int current_charset, gboolean seldisplay)
80 size_t i;
81 int listbox_result;
82 char buffer[255];
84 /* Create listbox */
85 Listbox *listbox = create_listbox_window_centered (center_y, center_x,
86 codepages->len + 1, ENTRY_LEN + 2,
87 _("Choose codepage"),
88 "[Codepages Translation]");
90 if (!seldisplay)
91 LISTBOX_APPEND_TEXT (listbox, '-', _("- < No translation >"), NULL);
93 /* insert all the items found */
94 for (i = 0; i < codepages->len; i++)
96 const char *name = ((codepage_desc *) g_ptr_array_index (codepages, i))->name;
97 g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i), name);
98 LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL);
100 if (seldisplay)
102 unsigned char hotkey = get_hotkey (codepages->len);
103 g_snprintf (buffer, sizeof (buffer), "%c %s", hotkey, _("Other 8 bit"));
104 LISTBOX_APPEND_TEXT (listbox, hotkey, buffer, NULL);
107 /* Select the default entry */
108 i = (seldisplay)
109 ? ((current_charset < 0) ? codepages->len : (size_t) current_charset)
110 : ((size_t) current_charset + 1);
112 listbox_select_entry (listbox->list, i);
114 listbox_result = run_listbox (listbox);
116 if (listbox_result < 0)
118 /* Cancel dialog */
119 return SELECT_CHARSET_CANCEL;
121 else
123 /* some charset has been selected */
124 if (seldisplay)
126 /* charset list is finished with "Other 8 bit" item */
127 return (listbox_result >= (int) codepages->len)
128 ? SELECT_CHARSET_OTHER_8BIT : listbox_result;
130 else
132 /* charset list is began with "- < No translation >" item */
133 return (listbox_result - 1);
139 /* --------------------------------------------------------------------------------------------- */
140 /** Set codepage */
141 gboolean
142 do_set_codepage (int codepage)
144 char *errmsg;
145 gboolean ret;
147 mc_global.source_codepage = codepage;
148 errmsg = init_translation_table (codepage == SELECT_CHARSET_NO_TRANSLATE ?
149 mc_global.display_codepage : mc_global.source_codepage,
150 mc_global.display_codepage);
151 ret = errmsg == NULL;
153 if (!ret)
155 message (D_ERROR, MSG_ERROR, "%s", errmsg);
156 g_free (errmsg);
159 return ret;
162 /* --------------------------------------------------------------------------------------------- */
163 /** Show menu selecting codepage */
165 gboolean
166 do_select_codepage (void)
168 int r;
170 r = select_charset (-1, -1, default_source_codepage, FALSE);
171 if (r == SELECT_CHARSET_CANCEL)
172 return FALSE;
174 default_source_codepage = r;
175 return do_set_codepage (default_source_codepage);
178 /* --------------------------------------------------------------------------------------------- */
180 #endif /* HAVE_CHARSET */