2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2005 Free Software Foundation
4 Written by John Darrington
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* This module describes the behaviour of the Value Labels dialog box,
23 used for input of the value labels in the variable sheet */
30 #include "val-labs-dialog.h"
31 #include <data/value-labels.h>
32 #include "psppire-variable.h"
34 /* This callback occurs when the text in the label entry box
37 on_label_entry_change(GtkEntry *entry, gpointer data)
41 struct val_labs_dialog *dialog = data;
42 g_assert(dialog->labs);
44 text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
46 text_to_value(text, &v,
47 *psppire_variable_get_write_spec(dialog->pv));
50 if ( val_labs_find (dialog->labs, v) )
52 gtk_widget_set_sensitive(dialog->change_button, TRUE);
53 gtk_widget_set_sensitive(dialog->add_button, FALSE);
57 gtk_widget_set_sensitive(dialog->change_button, FALSE);
58 gtk_widget_set_sensitive(dialog->add_button, TRUE);
63 /* Set the TREEVIEW list cursor to the item which has the value VAL */
65 select_treeview_from_value(GtkTreeView *treeview, union value *val)
70 We do this with a linear search through the model --- hardly
71 efficient, but the list is short ... */
74 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
77 for (success = gtk_tree_model_get_iter_first(model, &iter);
79 success = gtk_tree_model_iter_next(model, &iter))
84 gtk_tree_model_get_value(model, &iter, 1, &gvalue);
86 v.f = g_value_get_double(&gvalue);
88 if ( 0 == memcmp(&v, val, sizeof (union value)))
94 path = gtk_tree_model_get_path(model, &iter);
97 gtk_tree_view_set_cursor(treeview, path, 0, 0);
98 gtk_tree_path_free(path);
104 /* This callback occurs when the text in the value entry box is
107 on_value_entry_change(GtkEntry *entry, gpointer data)
111 struct val_labs_dialog *dialog = data;
113 const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
116 text_to_value(text, &v,
117 *psppire_variable_get_write_spec(dialog->pv));
120 g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
121 dialog->change_handler_id);
123 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),"");
126 if ( (s = val_labs_find (dialog->labs, v)) )
128 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), s);
129 gtk_widget_set_sensitive(dialog->add_button, FALSE);
130 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
131 select_treeview_from_value(GTK_TREE_VIEW(dialog->treeview), &v);
135 gtk_widget_set_sensitive(dialog->remove_button, FALSE);
136 gtk_widget_set_sensitive(dialog->add_button, TRUE);
139 g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
140 dialog->change_handler_id);
144 /* Callback for when the Value Labels dialog is closed using
147 val_labs_ok(GtkWidget *w, gpointer data)
149 struct val_labs_dialog *dialog = data;
151 psppire_variable_set_value_labels(dialog->pv, dialog->labs);
153 val_labs_destroy (dialog->labs);
159 /* Callback for when the Value Labels dialog is closed using
162 val_labs_cancel(GtkWidget *w, gpointer data)
164 struct val_labs_dialog *dialog = data;
166 val_labs_destroy (dialog->labs);
173 /* Return the value-label pair currently selected in the dialog box */
174 static struct val_lab *
175 get_selected_tuple(struct val_labs_dialog *dialog)
177 GtkTreeView *treeview = GTK_TREE_VIEW(dialog->treeview);
178 static struct val_lab vl;
181 GValue the_value = {0};
183 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
185 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
187 gtk_tree_selection_get_selected (sel, &model, &iter);
189 gtk_tree_model_get_value(model, &iter, 1, &the_value);
191 vl.value.f = g_value_get_double(&the_value);
192 g_value_unset(&the_value);
194 vl.label = val_labs_find (dialog->labs, vl.value);
200 static void repopulate_dialog(struct val_labs_dialog *dialog);
202 /* Callback which occurs when the "Change" button is clicked */
204 on_change(GtkWidget *w, gpointer data)
206 struct val_labs_dialog *dialog = data;
208 const gchar *val_text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
212 text_to_value(val_text, &v,
213 *psppire_variable_get_write_spec(dialog->pv));
215 val_labs_replace (dialog->labs, v,
216 gtk_entry_get_text(GTK_ENTRY(dialog->label_entry)));
218 gtk_widget_set_sensitive(dialog->change_button, FALSE);
220 repopulate_dialog(dialog);
225 /* Callback which occurs when the "Add" button is clicked */
227 on_add(GtkWidget *w, gpointer data)
229 struct val_labs_dialog *dialog = data;
233 const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
235 text_to_value(text, &v,
236 *psppire_variable_get_write_spec(dialog->pv));
239 if ( ! val_labs_add (dialog->labs, v,
240 gtk_entry_get_text(GTK_ENTRY(dialog->label_entry)) ) )
244 gtk_widget_set_sensitive(dialog->add_button, FALSE);
246 repopulate_dialog(dialog);
251 /* Callback which occurs when the "Remove" button is clicked */
253 on_remove(GtkWidget *w, gpointer data)
255 struct val_labs_dialog *dialog = data;
257 struct val_lab *vl = get_selected_tuple(dialog);
259 val_labs_remove (dialog->labs, vl->value);
261 repopulate_dialog(dialog);
263 gtk_widget_set_sensitive(dialog->remove_button, FALSE);
270 /* Callback which occurs when a line item is selected in the list of
271 value--label pairs.*/
273 on_select_row (GtkTreeView *treeview,
277 struct val_labs_dialog *dialog = data;
279 struct val_lab * vl = get_selected_tuple(dialog);
281 gchar *const text = value_to_text(vl->value,
282 *psppire_variable_get_write_spec(dialog->pv));
284 g_signal_handler_block(GTK_ENTRY(dialog->value_entry),
285 dialog->value_handler_id);
287 gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), text);
289 g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry),
290 dialog->value_handler_id);
293 g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
294 dialog->change_handler_id);
296 labeltext = pspp_locale_to_utf8(vl->label, -1, 0);
297 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),
301 g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
302 dialog->change_handler_id);
304 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
305 gtk_widget_set_sensitive(dialog->change_button, FALSE);
309 /* Create a new dialog box
310 (there should normally be only one)*/
311 struct val_labs_dialog *
312 val_labs_dialog_create(GladeXML *xml)
314 GtkTreeViewColumn *column;
316 GtkCellRenderer *renderer ;
318 struct val_labs_dialog *dialog = g_malloc(sizeof(*dialog));
320 dialog->window = get_widget_assert(xml,"val_labs_dialog");
321 dialog->value_entry = get_widget_assert(xml,"value_entry");
322 dialog->label_entry = get_widget_assert(xml,"label_entry");
324 gtk_window_set_transient_for
325 (GTK_WINDOW(dialog->window),
326 GTK_WINDOW(get_widget_assert(xml, "data_editor")));
328 dialog->ok = get_widget_assert(xml, "val_labs_ok");
329 dialog->add_button = get_widget_assert(xml, "val_labs_add");
330 dialog->remove_button = get_widget_assert(xml, "val_labs_remove");
331 dialog->change_button = get_widget_assert(xml, "val_labs_change");
333 dialog->treeview = get_widget_assert(xml,"treeview1");
335 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dialog->treeview), FALSE);
337 renderer = gtk_cell_renderer_text_new();
339 column = gtk_tree_view_column_new_with_attributes ("Title",
345 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
347 g_signal_connect(GTK_OBJECT(get_widget_assert(xml, "val_labs_cancel")),
349 GTK_SIGNAL_FUNC(val_labs_cancel), dialog);
351 dialog->change_handler_id =
352 g_signal_connect(GTK_OBJECT(dialog->label_entry),
354 GTK_SIGNAL_FUNC(on_label_entry_change), dialog);
356 dialog->value_handler_id =
357 g_signal_connect(GTK_OBJECT(dialog->value_entry),
359 GTK_SIGNAL_FUNC(on_value_entry_change), dialog);
361 g_signal_connect(GTK_OBJECT(dialog->change_button),
363 GTK_SIGNAL_FUNC(on_change), dialog);
366 g_signal_connect(GTK_OBJECT(get_widget_assert(xml, "val_labs_ok")),
368 GTK_SIGNAL_FUNC(val_labs_ok), dialog);
371 g_signal_connect(GTK_OBJECT(dialog->treeview), "cursor-changed",
372 GTK_SIGNAL_FUNC(on_select_row), dialog);
375 g_signal_connect(GTK_OBJECT(dialog->remove_button), "clicked",
376 GTK_SIGNAL_FUNC(on_remove), dialog);
379 g_signal_connect(GTK_OBJECT(dialog->add_button), "clicked",
380 GTK_SIGNAL_FUNC(on_add), dialog);
388 /* Populate the components of the dialog box, from the 'labs' member
391 repopulate_dialog(struct val_labs_dialog *dialog)
393 struct val_labs_iterator *vli = 0;
398 GtkListStore *list_store = gtk_list_store_new (2,
402 g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
403 dialog->change_handler_id);
404 g_signal_handler_block(GTK_ENTRY(dialog->value_entry),
405 dialog->value_handler_id);
407 gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), "");
408 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), "");
410 g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry),
411 dialog->value_handler_id);
412 g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
413 dialog->change_handler_id);
416 for(vl = val_labs_first_sorted (dialog->labs, &vli);
418 vl = val_labs_next(dialog->labs, &vli))
422 value_to_text(vl->value,
423 *psppire_variable_get_write_spec(dialog->pv));
426 pspp_locale_to_utf8(vl->label, -1, 0);
428 gchar *const text = g_strdup_printf("%s = \"%s\"",
432 gtk_list_store_append (list_store, &iter);
433 gtk_list_store_set (list_store, &iter,
443 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->treeview),
444 GTK_TREE_MODEL(list_store));
446 g_object_unref(list_store);
450 /* Initialise and display the dialog box */
452 val_labs_dialog_show(struct val_labs_dialog *dialog)
454 g_assert(!dialog->labs);
455 dialog->labs = val_labs_copy(
456 psppire_variable_get_value_labels(dialog->pv)
460 gtk_widget_set_sensitive(dialog->remove_button, FALSE);
461 gtk_widget_set_sensitive(dialog->change_button, FALSE);
462 gtk_widget_set_sensitive(dialog->add_button, FALSE);
464 repopulate_dialog(dialog);
465 gtk_widget_show(dialog->window);