gui: Recode syntax files on load and save.
[pspp] / src / ui / gui / psppire-encoding-selector.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "ui/gui/psppire-encoding-selector.h"
20
21 #include <assert.h>
22 #include <stdlib.h>
23
24 #include "libpspp/cast.h"
25 #include "libpspp/compiler.h"
26 #include "libpspp/i18n.h"
27
28 #include "gl/c-strcase.h"
29 #include "gl/localcharset.h"
30 #include "gl/xalloc.h"
31 #include "gl/xvasprintf.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35 #define N_(msgid) msgid
36
37 enum
38   {
39     COL_DESCRIPTION,
40     COL_ENCODING
41   };
42
43 static void SENTINEL (0)
44 add_encodings (GtkTreeStore *store, const char *category, ...)
45 {
46   const char *encodings[16];
47   va_list args;
48   int n;
49
50   /* Count encoding arguments. */
51   va_start (args, category);
52   n = 0;
53   while ((encodings[n] = va_arg (args, const char *)) != NULL)
54     {
55       const char *encoding = encodings[n];
56       if (!strcmp (encoding, "Auto") || is_encoding_supported (encoding))
57         n++;
58     }
59   assert (n < sizeof encodings / sizeof *encodings);
60   va_end (args);
61
62   if (n == 0)
63     return;
64
65   va_start (args, category);
66   if (n == 1)
67     {
68       char *description;
69
70       if (strcmp (encodings[0], "Auto"))
71         description = xasprintf ("%s (%s)", category, encodings[0]);
72       else
73         description = xstrdup (category);
74
75       gtk_tree_store_insert_with_values (
76         store, NULL, NULL, G_MAXINT,
77         COL_DESCRIPTION, description,
78         COL_ENCODING, encodings[0],
79         -1);
80
81       free (description);
82     }
83   else
84     {
85       GtkTreeIter head;
86       int i;
87
88       gtk_tree_store_insert_with_values (
89         store, &head, NULL, G_MAXINT,
90         COL_DESCRIPTION, category,
91         -1);
92
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],
98           -1);
99     }
100   va_end (args);
101 }
102
103 static void
104 set_sensitive (GtkCellLayout   *cell_layout,
105                GtkCellRenderer *cell,
106                GtkTreeModel    *tree_model,
107                GtkTreeIter     *iter,
108                gpointer         data)
109 {
110   gboolean sensitive;
111
112   sensitive = !gtk_tree_model_iter_has_child (tree_model, iter);
113   g_object_set (cell, "sensitive", sensitive, NULL);
114 }
115
116 struct find_default_encoding_aux
117   {
118     const char *default_encoding;
119     GtkTreeIter iter;
120   };
121
122 static gboolean
123 find_default_encoding (GtkTreeModel *model,
124                        GtkTreePath *path,
125                        GtkTreeIter *iter,
126                        gpointer aux_)
127 {
128   struct find_default_encoding_aux *aux = aux_;
129   gchar *encoding;
130   gboolean found;
131
132   gtk_tree_model_get (model, iter, COL_ENCODING, &encoding, -1);
133   found = encoding != NULL && !c_strcasecmp (encoding, aux->default_encoding);
134   if (found)
135     aux->iter = *iter;
136   g_free (encoding);
137   return found;
138 }
139
140 GtkWidget *
141 psppire_encoding_selector_new (const char *default_encoding,
142                                gboolean allow_auto)
143 {
144   struct find_default_encoding_aux aux;
145   GtkCellRenderer *renderer;
146   GtkWidget *hbox;
147   GtkWidget *combo_box;
148   GtkTreeStore *store;
149
150   store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
151
152   if (allow_auto)
153     add_encodings (store, _("Automatically Detect"), "Auto", NULL_SENTINEL);
154   add_encodings (store, _("Locale Encoding"), locale_charset (),
155                  NULL_SENTINEL);
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",
159                  NULL_SENTINEL);
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",
175                  NULL_SENTINEL);
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",
181                  NULL_SENTINEL);
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",
186                  NULL_SENTINEL);
187   add_encodings (store, _("Korean"), "EUC-KR", "ISO-2022-KR", "JOHAB", "UHC",
188                  NULL_SENTINEL);
189   add_encodings (store, _("Nordic"), "ISO-8859-10", NULL_SENTINEL);
190   add_encodings (store, _("Romanian"), "ISO-8859-16", "MacRomanian",
191                  NULL_SENTINEL);
192   add_encodings (store, _("South European"), "ISO-8859-3", NULL_SENTINEL);
193   add_encodings (store, _("Thai"), "ISO-8859-11", "TIS-620", "Windows-874",
194                  NULL_SENTINEL);
195   add_encodings (store, _("Turkish"), "IBM857", "ISO-8859-9", "Windows-1254",
196                  NULL_SENTINEL);
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);
201
202   combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
203
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);
208
209   g_object_unref (store);
210
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,
215                                   NULL);
216   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box),
217                                       renderer, set_sensitive,
218                                       NULL, NULL);
219
220   hbox = gtk_hbox_new (FALSE, 0);
221   gtk_box_pack_start (GTK_BOX (hbox),
222                       gtk_label_new (_("Character Encoding: ")),
223                       FALSE, FALSE, 0);
224   gtk_box_pack_start (GTK_BOX (hbox), combo_box, FALSE, TRUE, 0);
225   gtk_widget_show_all (hbox);
226
227   return hbox;
228 }
229
230 gchar *
231 psppire_encoding_selector_get_encoding (GtkWidget *selector)
232 {
233   gchar *encoding = NULL;
234   GList *list, *pos;
235
236   list = gtk_container_get_children (GTK_CONTAINER (selector));
237   for (pos = list; pos; pos = pos->next)
238     {
239       GtkWidget *widget = pos->data;
240       if (GTK_IS_COMBO_BOX (widget))
241         {
242           GtkTreeModel *model;
243           GtkTreeIter iter;
244
245           if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter))
246             break;
247
248           model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
249           gtk_tree_model_get (model, &iter, COL_ENCODING, &encoding, -1);
250           break;
251         }
252     }
253   g_list_free (list);
254   return encoding;
255 }