Added two macro examples:
[zuwinko.git] / src / selcodepage.c
blobff5fd596ea4a61889afac0631bf10ea1575721b4
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 "lib/widget.h"
33 #include "lib/charsets.h"
35 #include "main.h"
37 #include "selcodepage.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 #define ENTRY_LEN 30
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 /*** file scope functions ************************************************************************/
50 /* --------------------------------------------------------------------------------------------- */
53 static unsigned char
54 get_hotkey (int n)
56 return (n <= 9) ? '0' + n : 'a' + n - 10;
59 /* --------------------------------------------------------------------------------------------- */
60 /*** public functions ****************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 /* Return value:
64 * -2 (SELECT_CHARSET_CANCEL) : Cancel
65 * -1 (SELECT_CHARSET_OTHER_8BIT) : "Other 8 bit" if seldisplay == TRUE
66 * -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" if seldisplay == FALSE
67 * >= 0 : charset number
69 int
70 select_charset (int center_y, int center_x, int current_charset, gboolean seldisplay)
72 size_t i;
73 int listbox_result;
74 char buffer[255];
76 /* Create listbox */
77 Listbox *listbox = create_listbox_window_centered (center_y, center_x,
78 codepages->len + 1, ENTRY_LEN + 2,
79 _("Choose codepage"),
80 "[Codepages Translation]");
82 if (!seldisplay)
83 LISTBOX_APPEND_TEXT (listbox, '-', _("- < No translation >"), NULL);
85 /* insert all the items found */
86 for (i = 0; i < codepages->len; i++)
88 const char *name = ((codepage_desc *) g_ptr_array_index (codepages, i))->name;
89 g_snprintf (buffer, sizeof (buffer), "%c %s", get_hotkey (i), name);
90 LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL);
92 if (seldisplay)
94 unsigned char hotkey = get_hotkey (codepages->len);
95 g_snprintf (buffer, sizeof (buffer), "%c %s", hotkey, _("Other 8 bit"));
96 LISTBOX_APPEND_TEXT (listbox, hotkey, buffer, NULL);
99 /* Select the default entry */
100 i = (seldisplay)
101 ? ((current_charset < 0) ? codepages->len : (size_t) current_charset)
102 : ((size_t) current_charset + 1);
104 listbox_select_entry (listbox->list, i);
106 listbox_result = run_listbox (listbox);
108 if (listbox_result < 0)
110 /* Cancel dialog */
111 return SELECT_CHARSET_CANCEL;
113 else
115 /* some charset has been selected */
116 if (seldisplay)
118 /* charset list is finished with "Other 8 bit" item */
119 return (listbox_result >= (int) codepages->len)
120 ? SELECT_CHARSET_OTHER_8BIT : listbox_result;
122 else
124 /* charset list is began with "- < No translation >" item */
125 return (listbox_result - 1);
131 /* --------------------------------------------------------------------------------------------- */
132 /** Set codepage */
133 gboolean
134 do_set_codepage (int codepage)
136 char *errmsg;
137 gboolean ret;
139 source_codepage = codepage;
140 errmsg = init_translation_table (codepage == SELECT_CHARSET_NO_TRANSLATE ?
141 display_codepage : source_codepage, display_codepage);
142 ret = errmsg == NULL;
144 if (!ret)
146 message (D_ERROR, MSG_ERROR, "%s", errmsg);
147 g_free (errmsg);
150 return ret;
153 /* --------------------------------------------------------------------------------------------- */
154 /** Show menu selecting codepage */
156 gboolean
157 do_select_codepage (void)
159 int r;
161 r = select_charset (-1, -1, default_source_codepage, FALSE);
162 if (r == SELECT_CHARSET_CANCEL)
163 return FALSE;
165 default_source_codepage = r;
166 return do_set_codepage (default_source_codepage);
169 /* --------------------------------------------------------------------------------------------- */
171 #endif /* HAVE_CHARSET */