1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011, 2012, 2014 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
44 add_encodings (GtkTreeStore *store, struct encoding_category *cat)
46 if (cat->n_encodings == 1)
50 if (strcmp (cat->encodings[0], "Auto"))
51 description = xasprintf ("%s (%s)", cat->category, cat->encodings[0]);
53 description = xstrdup (cat->category);
55 gtk_tree_store_insert_with_values (
56 store, NULL, NULL, G_MAXINT,
57 COL_DESCRIPTION, description,
58 COL_ENCODING, cat->encodings[0],
68 gtk_tree_store_insert_with_values (
69 store, &head, NULL, G_MAXINT,
70 COL_DESCRIPTION, cat->category,
73 for (i = 0; i < cat->n_encodings; i++)
74 gtk_tree_store_insert_with_values (
75 store, NULL, &head, G_MAXINT,
76 COL_DESCRIPTION, cat->encodings[i],
77 COL_ENCODING, cat->encodings[i],
83 set_sensitive (GtkCellLayout *cell_layout,
84 GtkCellRenderer *cell,
85 GtkTreeModel *tree_model,
91 sensitive = !gtk_tree_model_iter_has_child (tree_model, iter);
92 g_object_set (cell, "sensitive", sensitive, NULL);
95 struct find_default_encoding_aux
97 const char *default_encoding;
102 find_default_encoding (GtkTreeModel *model,
107 struct find_default_encoding_aux *aux = aux_;
111 gtk_tree_model_get (model, iter, COL_ENCODING, &encoding, -1);
112 found = encoding != NULL && !c_strcasecmp (encoding, aux->default_encoding);
120 psppire_encoding_selector_new (const char *default_encoding,
123 struct find_default_encoding_aux aux;
124 struct encoding_category *categories;
125 struct encoding_category locale_cat;
126 const char *locale_encoding;
127 GtkCellRenderer *renderer;
129 GtkWidget *combo_box;
134 store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
138 struct encoding_category auto_cat;
139 const char *encoding = "Auto";
141 auto_cat.category = _("Automatically Detect");
142 auto_cat.encodings = &encoding;
143 auto_cat.n_encodings = 1;
144 add_encodings (store, &auto_cat);
147 locale_encoding = locale_charset ();
148 locale_cat.category = _("Locale Encoding");
149 locale_cat.encodings = &locale_encoding;
150 locale_cat.n_encodings = 1;
151 add_encodings (store, &locale_cat);
153 categories = get_encoding_categories ();
154 n_categories = get_n_encoding_categories ();
155 for (i = 0; i < n_categories; i++)
156 add_encodings (store, &categories[i]);
158 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
160 aux.default_encoding = default_encoding ? default_encoding : "Auto";
161 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &aux.iter);
162 gtk_tree_model_foreach (GTK_TREE_MODEL (store), find_default_encoding, &aux);
163 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &aux.iter);
165 g_object_unref (store);
167 renderer = gtk_cell_renderer_text_new ();
168 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
169 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
170 "text", COL_DESCRIPTION,
172 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box),
173 renderer, set_sensitive,
176 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
177 gtk_box_pack_start (GTK_BOX (hbox),
178 gtk_label_new (_("Character Encoding: ")),
180 gtk_box_pack_start (GTK_BOX (hbox), combo_box, FALSE, TRUE, 0);
181 gtk_widget_show_all (hbox);
187 psppire_encoding_selector_get_encoding (GtkWidget *selector)
189 gchar *encoding = NULL;
192 list = gtk_container_get_children (GTK_CONTAINER (selector));
193 for (pos = list; pos; pos = pos->next)
195 GtkWidget *widget = pos->data;
196 if (GTK_IS_COMBO_BOX (widget))
201 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter))
204 model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
205 gtk_tree_model_get (model, &iter, COL_ENCODING, &encoding, -1);
211 return encoding && !strcmp (encoding, "Auto") ? NULL : encoding;