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 */
28 #include "val-labs-dialog.h"
29 #include <data/value-labels.h>
30 #include "psppire-variable.h"
32 /* This callback occurs when the text in the label entry box
35 on_label_entry_change(GtkEntry *entry, gpointer data)
39 struct val_labs_dialog *dialog = data;
40 g_assert(dialog->labs);
42 text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
44 text_to_value(text, &v,
45 *psppire_variable_get_write_spec(dialog->pv));
48 if ( val_labs_find (dialog->labs, v) )
50 gtk_widget_set_sensitive(dialog->change_button, TRUE);
51 gtk_widget_set_sensitive(dialog->add_button, FALSE);
55 gtk_widget_set_sensitive(dialog->change_button, FALSE);
56 gtk_widget_set_sensitive(dialog->add_button, TRUE);
61 /* Set the TREEVIEW list cursor to the item which has the value VAL */
63 select_treeview_from_value(GtkTreeView *treeview, union value *val)
68 We do this with a linear search through the model --- hardly
69 efficient, but the list is short ... */
72 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
75 for (success = gtk_tree_model_get_iter_first(model, &iter);
77 success = gtk_tree_model_iter_next(model, &iter))
82 gtk_tree_model_get_value(model, &iter, 1, &gvalue);
84 v.f = g_value_get_double(&gvalue);
86 if ( 0 == memcmp(&v, val, sizeof (union value)))
92 path = gtk_tree_model_get_path(model, &iter);
95 gtk_tree_view_set_cursor(treeview, path, 0, 0);
96 gtk_tree_path_free(path);
102 /* This callback occurs when the text in the value entry box is
105 on_value_entry_change(GtkEntry *entry, gpointer data)
109 struct val_labs_dialog *dialog = data;
111 const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
114 text_to_value(text, &v,
115 *psppire_variable_get_write_spec(dialog->pv));
118 g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
119 dialog->change_handler_id);
121 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),"");
124 if ( (s = val_labs_find (dialog->labs, v)) )
126 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), s);
127 gtk_widget_set_sensitive(dialog->add_button, FALSE);
128 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
129 select_treeview_from_value(GTK_TREE_VIEW(dialog->treeview), &v);
133 gtk_widget_set_sensitive(dialog->remove_button, FALSE);
134 gtk_widget_set_sensitive(dialog->add_button, TRUE);
137 g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
138 dialog->change_handler_id);
142 /* Callback for when the Value Labels dialog is closed using
145 val_labs_ok(GtkWidget *w, gpointer data)
147 struct val_labs_dialog *dialog = data;
149 psppire_variable_set_value_labels(dialog->pv, dialog->labs);
151 val_labs_destroy (dialog->labs);
157 /* Callback for when the Value Labels dialog is closed using
160 val_labs_cancel(GtkWidget *w, gpointer data)
162 struct val_labs_dialog *dialog = data;
164 val_labs_destroy (dialog->labs);
171 /* Return the value-label pair currently selected in the dialog box */
172 static struct val_lab *
173 get_selected_tuple(struct val_labs_dialog *dialog)
175 GtkTreeView *treeview = GTK_TREE_VIEW(dialog->treeview);
176 static struct val_lab vl;
179 GValue the_value = {0};
181 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
183 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
185 gtk_tree_selection_get_selected (sel, &model, &iter);
187 gtk_tree_model_get_value(model, &iter, 1, &the_value);
189 vl.value.f = g_value_get_double(&the_value);
190 g_value_unset(&the_value);
192 vl.label = val_labs_find (dialog->labs, vl.value);
198 static void repopulate_dialog(struct val_labs_dialog *dialog);
200 /* Callback which occurs when the "Change" button is clicked */
202 on_change(GtkWidget *w, gpointer data)
204 struct val_labs_dialog *dialog = data;
206 const gchar *val_text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
210 text_to_value(val_text, &v,
211 *psppire_variable_get_write_spec(dialog->pv));
213 val_labs_replace (dialog->labs, v,
214 gtk_entry_get_text(GTK_ENTRY(dialog->label_entry)));
216 gtk_widget_set_sensitive(dialog->change_button, FALSE);
218 repopulate_dialog(dialog);
223 /* Callback which occurs when the "Add" button is clicked */
225 on_add(GtkWidget *w, gpointer data)
227 struct val_labs_dialog *dialog = data;
231 const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
233 text_to_value(text, &v,
234 *psppire_variable_get_write_spec(dialog->pv));
237 if ( ! val_labs_add (dialog->labs, v,
238 gtk_entry_get_text(GTK_ENTRY(dialog->label_entry)) ) )
242 gtk_widget_set_sensitive(dialog->add_button, FALSE);
244 repopulate_dialog(dialog);
249 /* Callback which occurs when the "Remove" button is clicked */
251 on_remove(GtkWidget *w, gpointer data)
253 struct val_labs_dialog *dialog = data;
255 struct val_lab *vl = get_selected_tuple(dialog);
257 val_labs_remove (dialog->labs, vl->value);
259 repopulate_dialog(dialog);
261 gtk_widget_set_sensitive(dialog->remove_button, FALSE);
268 /* Callback which occurs when a line item is selected in the list of
269 value--label pairs.*/
271 on_select_row (GtkTreeView *treeview,
274 struct val_labs_dialog *dialog = data;
276 struct val_lab * vl = get_selected_tuple(dialog);
278 gchar *const text = value_to_text(vl->value,
279 *psppire_variable_get_write_spec(dialog->pv));
281 g_signal_handler_block(GTK_ENTRY(dialog->value_entry),
282 dialog->value_handler_id);
284 gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), text);
286 g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry),
287 dialog->value_handler_id);
290 g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
291 dialog->change_handler_id);
293 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),
296 g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
297 dialog->change_handler_id);
299 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
300 gtk_widget_set_sensitive(dialog->change_button, FALSE);
304 /* Create a new dialog box
305 (there should normally be only one)*/
306 struct val_labs_dialog *
307 val_labs_dialog_create(GladeXML *xml)
309 GtkTreeViewColumn *column;
311 GtkCellRenderer *renderer ;
313 struct val_labs_dialog *dialog = g_malloc(sizeof(*dialog));
315 dialog->window = get_widget_assert(xml,"val_labs_dialog");
316 dialog->value_entry = get_widget_assert(xml,"value_entry");
317 dialog->label_entry = get_widget_assert(xml,"label_entry");
319 gtk_window_set_transient_for
320 (GTK_WINDOW(dialog->window),
321 GTK_WINDOW(get_widget_assert(xml, "data_editor")));
323 dialog->ok = get_widget_assert(xml, "val_labs_ok");
324 dialog->add_button = get_widget_assert(xml, "val_labs_add");
325 dialog->remove_button = get_widget_assert(xml, "val_labs_remove");
326 dialog->change_button = get_widget_assert(xml, "val_labs_change");
328 dialog->treeview = get_widget_assert(xml,"treeview1");
330 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dialog->treeview), FALSE);
332 renderer = gtk_cell_renderer_text_new();
334 column = gtk_tree_view_column_new_with_attributes ("Title",
340 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
342 g_signal_connect(GTK_OBJECT(get_widget_assert(xml, "val_labs_cancel")),
344 GTK_SIGNAL_FUNC(val_labs_cancel), dialog);
346 dialog->change_handler_id =
347 g_signal_connect(GTK_OBJECT(dialog->label_entry),
349 GTK_SIGNAL_FUNC(on_label_entry_change), dialog);
351 dialog->value_handler_id =
352 g_signal_connect(GTK_OBJECT(dialog->value_entry),
354 GTK_SIGNAL_FUNC(on_value_entry_change), dialog);
356 g_signal_connect(GTK_OBJECT(dialog->change_button),
358 GTK_SIGNAL_FUNC(on_change), dialog);
361 g_signal_connect(GTK_OBJECT(get_widget_assert(xml, "val_labs_ok")),
363 GTK_SIGNAL_FUNC(val_labs_ok), dialog);
366 g_signal_connect(GTK_OBJECT(dialog->treeview), "cursor-changed",
367 GTK_SIGNAL_FUNC(on_select_row), dialog);
370 g_signal_connect(GTK_OBJECT(dialog->remove_button), "clicked",
371 GTK_SIGNAL_FUNC(on_remove), dialog);
374 g_signal_connect(GTK_OBJECT(dialog->add_button), "clicked",
375 GTK_SIGNAL_FUNC(on_add), dialog);
383 /* Populate the components of the dialog box, from the 'labs' member
386 repopulate_dialog(struct val_labs_dialog *dialog)
388 struct val_labs_iterator *vli = 0;
393 GtkListStore *list_store = gtk_list_store_new (2,
397 g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
398 dialog->change_handler_id);
399 g_signal_handler_block(GTK_ENTRY(dialog->value_entry),
400 dialog->value_handler_id);
402 gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), "");
403 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), "");
405 g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry),
406 dialog->value_handler_id);
407 g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
408 dialog->change_handler_id);
411 for(vl = val_labs_first_sorted (dialog->labs, &vli);
413 vl = val_labs_next(dialog->labs, &vli))
416 value_to_text(vl->value,
417 *psppire_variable_get_write_spec(dialog->pv));
421 gchar *const text = g_strdup_printf("%s = \"%s\"",
424 gtk_list_store_append (list_store, &iter);
425 gtk_list_store_set (list_store, &iter,
434 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->treeview),
435 GTK_TREE_MODEL(list_store));
437 g_object_unref(list_store);
441 /* Initialise and display the dialog box */
443 val_labs_dialog_show(struct val_labs_dialog *dialog)
445 g_assert(!dialog->labs);
446 dialog->labs = val_labs_copy(
447 psppire_variable_get_value_labels(dialog->pv)
451 gtk_widget_set_sensitive(dialog->remove_button, FALSE);
452 gtk_widget_set_sensitive(dialog->change_button, FALSE);
453 gtk_widget_set_sensitive(dialog->add_button, FALSE);
455 repopulate_dialog(dialog);
456 gtk_widget_show(dialog->window);