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"
23 #include "libpspp/cast.h"
24 #include "libpspp/compiler.h"
25 #include "libpspp/i18n.h"
27 #include "gl/c-strcase.h"
28 #include "gl/localcharset.h"
29 #include "gl/xalloc.h"
30 #include "gl/xvasprintf.h"
33 #define _(msgid) gettext (msgid)
34 #define N_(msgid) msgid
43 add_encodings (GtkTreeStore *store, struct encoding_category *cat)
45 if (cat->n_encodings == 1)
49 if (strcmp (cat->encodings[0], "Auto"))
50 description = xasprintf ("%s (%s)", cat->category, cat->encodings[0]);
52 description = xstrdup (cat->category);
54 gtk_tree_store_insert_with_values (
55 store, NULL, NULL, G_MAXINT,
56 COL_DESCRIPTION, description,
57 COL_ENCODING, cat->encodings[0],
67 gtk_tree_store_insert_with_values (
68 store, &head, NULL, G_MAXINT,
69 COL_DESCRIPTION, cat->category,
72 for (i = 0; i < cat->n_encodings; i++)
73 gtk_tree_store_insert_with_values (
74 store, NULL, &head, G_MAXINT,
75 COL_DESCRIPTION, cat->encodings[i],
76 COL_ENCODING, cat->encodings[i],
82 set_sensitive (GtkCellLayout *cell_layout,
83 GtkCellRenderer *cell,
84 GtkTreeModel *tree_model,
90 sensitive = !gtk_tree_model_iter_has_child (tree_model, iter);
91 g_object_set (cell, "sensitive", sensitive, NULL);
94 struct find_default_encoding_aux
96 const char *default_encoding;
101 find_default_encoding (GtkTreeModel *model,
106 struct find_default_encoding_aux *aux = aux_;
110 gtk_tree_model_get (model, iter, COL_ENCODING, &encoding, -1);
111 found = encoding != NULL && !c_strcasecmp (encoding, aux->default_encoding);
119 psppire_encoding_selector_new (const char *default_encoding,
122 struct find_default_encoding_aux aux;
123 struct encoding_category *categories;
124 struct encoding_category locale_cat;
125 const char *locale_encoding;
126 GtkCellRenderer *renderer;
128 GtkWidget *combo_box;
133 store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
137 struct encoding_category auto_cat;
138 const char *encoding = "Auto";
140 auto_cat.category = _("Automatically Detect");
141 auto_cat.encodings = &encoding;
142 auto_cat.n_encodings = 1;
143 add_encodings (store, &auto_cat);
146 locale_encoding = locale_charset ();
147 locale_cat.category = _("Locale Encoding");
148 locale_cat.encodings = &locale_encoding;
149 locale_cat.n_encodings = 1;
150 add_encodings (store, &locale_cat);
152 categories = get_encoding_categories ();
153 n_categories = get_n_encoding_categories ();
154 for (i = 0; i < n_categories; i++)
155 add_encodings (store, &categories[i]);
157 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
159 aux.default_encoding = default_encoding ? default_encoding : "Auto";
160 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &aux.iter);
161 gtk_tree_model_foreach (GTK_TREE_MODEL (store), find_default_encoding, &aux);
162 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &aux.iter);
164 g_object_unref (store);
166 renderer = gtk_cell_renderer_text_new ();
167 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
168 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
169 "text", COL_DESCRIPTION,
171 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box),
172 renderer, set_sensitive,
175 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
176 gtk_box_pack_start (GTK_BOX (hbox),
177 gtk_label_new (_("Character Encoding: ")),
179 gtk_box_pack_start (GTK_BOX (hbox), combo_box, FALSE, TRUE, 0);
180 gtk_widget_show_all (hbox);
186 Return a string describing the currently selected encoding.
187 The caller should free this string when no longer required.
190 psppire_encoding_selector_get_encoding (GtkWidget *selector)
192 gchar *encoding = NULL;
194 GList *list = gtk_container_get_children (GTK_CONTAINER (selector));
196 for (pos = list; pos; pos = pos->next)
198 GtkWidget *widget = pos->data;
199 if (GTK_IS_COMBO_BOX (widget))
204 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter))
207 model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
208 gtk_tree_model_get (model, &iter, COL_ENCODING, &encoding, -1);
215 if (0 == strcmp (encoding, "Auto"))