1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2005 Free Software Foundation
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/>. */
18 /* This module describes the behaviour of the Value Labels dialog box,
19 used for input of the value labels in the variable sheet */
26 #include "val-labs-dialog.h"
27 #include <data/value-labels.h>
30 struct val_labs_dialog
34 /* The variable to be updated */
37 /* Local copy of labels */
38 struct val_labs *labs;
41 GtkWidget *add_button;
42 GtkWidget *remove_button;
43 GtkWidget *change_button;
46 GtkWidget *value_entry;
47 GtkWidget *label_entry;
49 /* Signal handler ids */
50 gint change_handler_id;
51 gint value_handler_id;
57 /* This callback occurs when the text in the label entry box
60 on_label_entry_change (GtkEntry *entry, gpointer data)
64 struct val_labs_dialog *dialog = data;
65 g_assert (dialog->labs);
67 text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
69 text_to_value (text, &v,
70 *var_get_write_format (dialog->pv));
73 if ( val_labs_find (dialog->labs, v) )
75 gtk_widget_set_sensitive (dialog->change_button, TRUE);
76 gtk_widget_set_sensitive (dialog->add_button, FALSE);
80 gtk_widget_set_sensitive (dialog->change_button, FALSE);
81 gtk_widget_set_sensitive (dialog->add_button, TRUE);
86 /* Set the TREEVIEW list cursor to the item which has the value VAL */
88 select_treeview_from_value (GtkTreeView *treeview, union value *val)
93 We do this with a linear search through the model --- hardly
94 efficient, but the list is short ... */
97 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
100 for (success = gtk_tree_model_get_iter_first (model, &iter);
102 success = gtk_tree_model_iter_next (model, &iter))
107 gtk_tree_model_get_value (model, &iter, 1, &gvalue);
109 v.f = g_value_get_double (&gvalue);
111 if ( 0 == memcmp (&v, val, sizeof (union value)))
117 path = gtk_tree_model_get_path (model, &iter);
120 gtk_tree_view_set_cursor (treeview, path, 0, 0);
121 gtk_tree_path_free (path);
127 /* This callback occurs when the text in the value entry box is
130 on_value_entry_change (GtkEntry *entry, gpointer data)
134 struct val_labs_dialog *dialog = data;
136 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
139 text_to_value (text, &v,
140 *var_get_write_format (dialog->pv));
143 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
144 dialog->change_handler_id);
146 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),"");
149 if ( (s = val_labs_find (dialog->labs, v)) )
151 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), s);
152 gtk_widget_set_sensitive (dialog->add_button, FALSE);
153 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
154 select_treeview_from_value (GTK_TREE_VIEW (dialog->treeview), &v);
158 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
159 gtk_widget_set_sensitive (dialog->add_button, TRUE);
162 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
163 dialog->change_handler_id);
167 /* Callback for when the Value Labels dialog is closed using
170 val_labs_ok (GtkWidget *w, gpointer data)
172 struct val_labs_dialog *dialog = data;
174 var_set_value_labels (dialog->pv, dialog->labs);
176 val_labs_destroy (dialog->labs);
180 gtk_widget_hide (dialog->window);
185 /* Callback for when the Value Labels dialog is closed using
188 val_labs_cancel (struct val_labs_dialog *dialog)
190 val_labs_destroy (dialog->labs);
194 gtk_widget_hide (dialog->window);
198 /* Callback for when the Value Labels dialog is closed using
201 on_cancel (GtkWidget *w, gpointer data)
203 struct val_labs_dialog *dialog = data;
205 val_labs_cancel (dialog);
211 /* Callback for when the Value Labels dialog is closed using
212 the window delete button.*/
214 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
216 struct val_labs_dialog *dialog = data;
218 val_labs_cancel (dialog);
224 /* Return the value-label pair currently selected in the dialog box */
225 static struct val_lab *
226 get_selected_tuple (struct val_labs_dialog *dialog)
228 GtkTreeView *treeview = GTK_TREE_VIEW (dialog->treeview);
229 static struct val_lab vl;
232 GValue the_value = {0};
234 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
236 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
238 gtk_tree_selection_get_selected (sel, &model, &iter);
240 gtk_tree_model_get_value (model, &iter, 1, &the_value);
242 vl.value.f = g_value_get_double (&the_value);
243 g_value_unset (&the_value);
245 vl.label = val_labs_find (dialog->labs, vl.value);
251 static void repopulate_dialog (struct val_labs_dialog *dialog);
253 /* Callback which occurs when the "Change" button is clicked */
255 on_change (GtkWidget *w, gpointer data)
257 struct val_labs_dialog *dialog = data;
259 const gchar *val_text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
263 text_to_value (val_text, &v,
264 *var_get_write_format (dialog->pv));
266 val_labs_replace (dialog->labs, v,
267 gtk_entry_get_text (GTK_ENTRY (dialog->label_entry)));
269 gtk_widget_set_sensitive (dialog->change_button, FALSE);
271 repopulate_dialog (dialog);
276 /* Callback which occurs when the "Add" button is clicked */
278 on_add (GtkWidget *w, gpointer data)
280 struct val_labs_dialog *dialog = data;
284 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
286 text_to_value (text, &v,
287 *var_get_write_format (dialog->pv));
290 if ( ! val_labs_add (dialog->labs, v,
292 ( GTK_ENTRY (dialog->label_entry)) ) )
295 gtk_widget_set_sensitive (dialog->add_button, FALSE);
297 repopulate_dialog (dialog);
302 /* Callback which occurs when the "Remove" button is clicked */
304 on_remove (GtkWidget *w, gpointer data)
306 struct val_labs_dialog *dialog = data;
308 struct val_lab *vl = get_selected_tuple (dialog);
310 val_labs_remove (dialog->labs, vl->value);
312 repopulate_dialog (dialog);
314 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
321 /* Callback which occurs when a line item is selected in the list of
322 value--label pairs.*/
324 on_select_row (GtkTreeView *treeview,
328 struct val_labs_dialog *dialog = data;
330 struct val_lab * vl = get_selected_tuple (dialog);
332 gchar *const text = value_to_text (vl->value,
333 *var_get_write_format (dialog->pv));
335 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
336 dialog->value_handler_id);
338 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), text);
340 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
341 dialog->value_handler_id);
344 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
345 dialog->change_handler_id);
347 labeltext = pspp_locale_to_utf8 (vl->label, -1, 0);
348 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
352 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
353 dialog->change_handler_id);
355 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
356 gtk_widget_set_sensitive (dialog->change_button, FALSE);
360 /* Create a new dialog box
361 (there should normally be only one)*/
362 struct val_labs_dialog *
363 val_labs_dialog_create (GladeXML *xml)
365 GtkTreeViewColumn *column;
367 GtkCellRenderer *renderer ;
369 struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
373 dialog->window = get_widget_assert (xml,"val_labs_dialog");
374 dialog->value_entry = get_widget_assert (xml,"value_entry");
375 dialog->label_entry = get_widget_assert (xml,"label_entry");
377 gtk_window_set_transient_for
378 (GTK_WINDOW (dialog->window),
379 GTK_WINDOW (get_widget_assert (xml, "data_editor")));
381 dialog->add_button = get_widget_assert (xml, "val_labs_add");
382 dialog->remove_button = get_widget_assert (xml, "val_labs_remove");
383 dialog->change_button = get_widget_assert (xml, "val_labs_change");
385 dialog->treeview = get_widget_assert (xml,"treeview1");
387 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->treeview), FALSE);
389 renderer = gtk_cell_renderer_text_new ();
391 column = gtk_tree_view_column_new_with_attributes ("Title",
397 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
399 g_signal_connect (GTK_OBJECT (get_widget_assert (xml, "val_labs_cancel")),
401 GTK_SIGNAL_FUNC (on_cancel), dialog);
403 g_signal_connect (GTK_OBJECT (dialog->window), "delete-event",
404 GTK_SIGNAL_FUNC (on_delete), dialog);
406 g_signal_connect (GTK_OBJECT (get_widget_assert (xml, "val_labs_ok")),
408 GTK_SIGNAL_FUNC (val_labs_ok), dialog);
410 dialog->change_handler_id =
411 g_signal_connect (GTK_OBJECT (dialog->label_entry),
413 GTK_SIGNAL_FUNC (on_label_entry_change), dialog);
415 dialog->value_handler_id =
416 g_signal_connect (GTK_OBJECT (dialog->value_entry),
418 GTK_SIGNAL_FUNC (on_value_entry_change), dialog);
420 g_signal_connect (GTK_OBJECT (dialog->change_button),
422 GTK_SIGNAL_FUNC (on_change), dialog);
425 g_signal_connect (GTK_OBJECT (dialog->treeview), "cursor-changed",
426 GTK_SIGNAL_FUNC (on_select_row), dialog);
428 g_signal_connect (GTK_OBJECT (dialog->remove_button), "clicked",
429 GTK_SIGNAL_FUNC (on_remove), dialog);
431 g_signal_connect (GTK_OBJECT (dialog->add_button), "clicked",
432 GTK_SIGNAL_FUNC (on_add), dialog);
441 val_labs_dialog_set_target_variable (struct val_labs_dialog *dialog,
442 struct variable *var)
449 /* Populate the components of the dialog box, from the 'labs' member
452 repopulate_dialog (struct val_labs_dialog *dialog)
454 struct val_labs_iterator *vli = 0;
459 GtkListStore *list_store = gtk_list_store_new (2,
463 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
464 dialog->change_handler_id);
465 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
466 dialog->value_handler_id);
468 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
469 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
471 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
472 dialog->value_handler_id);
473 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
474 dialog->change_handler_id);
477 for (vl = val_labs_first_sorted (dialog->labs, &vli);
479 vl = val_labs_next (dialog->labs, &vli))
483 value_to_text (vl->value,
484 *var_get_write_format (dialog->pv));
487 pspp_locale_to_utf8 (vl->label, -1, 0);
489 gchar *const text = g_strdup_printf ("%s = \"%s\"",
493 gtk_list_store_append (list_store, &iter);
494 gtk_list_store_set (list_store, &iter,
504 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
505 GTK_TREE_MODEL (list_store));
507 g_object_unref (list_store);
511 /* Initialise and display the dialog box */
513 val_labs_dialog_show (struct val_labs_dialog *dialog)
515 const struct val_labs *value_labels;
517 g_assert (!dialog->labs);
519 value_labels = var_get_value_labels (dialog->pv);
522 dialog->labs = val_labs_copy ( value_labels );
524 dialog->labs = val_labs_create ( var_get_width (dialog->pv));
526 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
527 gtk_widget_set_sensitive (dialog->change_button, FALSE);
528 gtk_widget_set_sensitive (dialog->add_button, FALSE);
530 repopulate_dialog (dialog);
531 gtk_widget_show (dialog->window);