1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "ui/gui/psppire-encoding-selector.h"
24 #include "libpspp/cast.h"
25 #include "libpspp/compiler.h"
26 #include "libpspp/i18n.h"
28 #include "gl/c-strcase.h"
29 #include "gl/localcharset.h"
30 #include "gl/xalloc.h"
31 #include "gl/xvasprintf.h"
34 #define _(msgid) gettext (msgid)
35 #define N_(msgid) msgid
43 static void SENTINEL (0)
44 add_encodings (GtkTreeStore *store, const char *category, ...)
46 const char *encodings[16];
50 /* Count encoding arguments. */
51 va_start (args, category);
53 while ((encodings[n] = va_arg (args, const char *)) != NULL)
55 const char *encoding = encodings[n];
56 if (!strcmp (encoding, "Auto") || is_encoding_supported (encoding))
59 assert (n < sizeof encodings / sizeof *encodings);
65 va_start (args, category);
70 if (strcmp (encodings[0], "Auto"))
71 description = xasprintf ("%s (%s)", category, encodings[0]);
73 description = xstrdup (category);
75 gtk_tree_store_insert_with_values (
76 store, NULL, NULL, G_MAXINT,
77 COL_DESCRIPTION, description,
78 COL_ENCODING, encodings[0],
88 gtk_tree_store_insert_with_values (
89 store, &head, NULL, G_MAXINT,
90 COL_DESCRIPTION, category,
93 for (i = 0; i < n; i++)
94 gtk_tree_store_insert_with_values (
95 store, NULL, &head, G_MAXINT,
96 COL_DESCRIPTION, encodings[i],
97 COL_ENCODING, encodings[i],
104 set_sensitive (GtkCellLayout *cell_layout,
105 GtkCellRenderer *cell,
106 GtkTreeModel *tree_model,
112 sensitive = !gtk_tree_model_iter_has_child (tree_model, iter);
113 g_object_set (cell, "sensitive", sensitive, NULL);
116 struct find_default_encoding_aux
118 const char *default_encoding;
123 find_default_encoding (GtkTreeModel *model,
128 struct find_default_encoding_aux *aux = aux_;
132 gtk_tree_model_get (model, iter, COL_ENCODING, &encoding, -1);
133 found = encoding != NULL && !c_strcasecmp (encoding, aux->default_encoding);
141 psppire_encoding_selector_new (const char *default_encoding,
144 struct find_default_encoding_aux aux;
145 GtkCellRenderer *renderer;
147 GtkWidget *combo_box;
150 store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
153 add_encodings (store, _("Automatically Detect"), "Auto", NULL_SENTINEL);
154 add_encodings (store, _("Locale Encoding"), locale_charset (),
156 add_encodings (store, "Unicode", "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE",
157 "UTF-32", "UTF-32BE", "UTF-32LE", NULL_SENTINEL);
158 add_encodings (store, _("Arabic"), "IBM864", "ISO-8859-6", "Windows-1256",
160 add_encodings (store, _("Armenian"), "ARMSCII-8", NULL_SENTINEL);
161 add_encodings (store, _("Baltic"), "ISO-8859-13", "ISO-8859-4",
162 "Windows-1257", NULL_SENTINEL);
163 add_encodings (store, _("Celtic"), "ISO-8859-14", NULL_SENTINEL);
164 add_encodings (store, _("Central European"), "IBM852", "ISO-8859-2",
165 "Mac-CentralEurope", "Windows-1250", NULL_SENTINEL);
166 add_encodings (store, _("Chinese Simplified"), "GB18030", "GB2312", "GBK",
167 "HZ-GB-2312", "ISO-2022-CN", NULL_SENTINEL);
168 add_encodings (store, _("Chinese Traditional"), "Big5", "Big5-HKSCS",
169 "EUC-TW", NULL_SENTINEL);
170 add_encodings (store, _("Croatian"), "MacCroatian", NULL_SENTINEL);
171 add_encodings (store, _("Cyrillic"), "IBM855", "ISO-8859-5", "ISO-IR-111",
172 "KOI8-R", "MacCyrillic", NULL_SENTINEL);
173 add_encodings (store, _("Cyrillic/Russian"), "IBM866", NULL_SENTINEL);
174 add_encodings (store, _("Cyrillic/Ukrainian"), "KOI8-U", "MacUkrainian",
176 add_encodings (store, _("Georgian"), "GEOSTD8", NULL_SENTINEL);
177 add_encodings (store, _("Greek"), "ISO-8859-7", "MacGreek", NULL_SENTINEL);
178 add_encodings (store, _("Gujarati"), "MacGujarati", NULL_SENTINEL);
179 add_encodings (store, _("Gurmukhi"), "MacGurmukhi", NULL_SENTINEL);
180 add_encodings (store, _("Hebrew"), "IBM862", "ISO-8859-8-I", "Windows-1255",
182 add_encodings (store, _("Hebrew Visual"), "ISO-8859-8", NULL_SENTINEL);
183 add_encodings (store, _("Hindi"), "MacDevangari", NULL_SENTINEL);
184 add_encodings (store, _("Icelandic"), "MacIcelandic", NULL_SENTINEL);
185 add_encodings (store, _("Japanese"), "EUC-JP", "ISO-2022-JP", "Shift_JIS",
187 add_encodings (store, _("Korean"), "EUC-KR", "ISO-2022-KR", "JOHAB", "UHC",
189 add_encodings (store, _("Nordic"), "ISO-8859-10", NULL_SENTINEL);
190 add_encodings (store, _("Romanian"), "ISO-8859-16", "MacRomanian",
192 add_encodings (store, _("South European"), "ISO-8859-3", NULL_SENTINEL);
193 add_encodings (store, _("Thai"), "ISO-8859-11", "TIS-620", "Windows-874",
195 add_encodings (store, _("Turkish"), "IBM857", "ISO-8859-9", "Windows-1254",
197 add_encodings (store, _("Vietnamese"), "TVCN", "VISCII", "VPS",
198 "Windows-1258", NULL_SENTINEL);
199 add_encodings (store, _("Western European"), "ISO-8859-1", "ISO-8859-15",
200 "Windows-1252", "IBM850", "MacRoman", NULL_SENTINEL);
202 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
204 aux.default_encoding = default_encoding ? default_encoding : "Auto";
205 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &aux.iter);
206 gtk_tree_model_foreach (GTK_TREE_MODEL (store), find_default_encoding, &aux);
207 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &aux.iter);
209 g_object_unref (store);
211 renderer = gtk_cell_renderer_text_new ();
212 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
213 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
214 "text", COL_DESCRIPTION,
216 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box),
217 renderer, set_sensitive,
220 hbox = gtk_hbox_new (FALSE, 0);
221 gtk_box_pack_start (GTK_BOX (hbox),
222 gtk_label_new (_("Character Encoding: ")),
224 gtk_box_pack_start (GTK_BOX (hbox), combo_box, FALSE, TRUE, 0);
225 gtk_widget_show_all (hbox);
231 psppire_encoding_selector_get_encoding (GtkWidget *selector)
233 gchar *encoding = NULL;
236 list = gtk_container_get_children (GTK_CONTAINER (selector));
237 for (pos = list; pos; pos = pos->next)
239 GtkWidget *widget = pos->data;
240 if (GTK_IS_COMBO_BOX (widget))
245 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter))
248 model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
249 gtk_tree_model_get (model, &iter, COL_ENCODING, &encoding, -1);