1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2005, 2009 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>
28 #include <data/format.h>
29 #include "psppire-var-sheet.h"
30 #include "psppire-var-store.h"
31 #include <libpspp/i18n.h>
33 struct val_labs_dialog
37 PsppireVarStore *var_store;
39 /* The variable to be updated */
42 /* Local copy of labels */
43 struct val_labs *labs;
46 GtkWidget *add_button;
47 GtkWidget *remove_button;
48 GtkWidget *change_button;
51 GtkWidget *value_entry;
52 GtkWidget *label_entry;
54 /* Signal handler ids */
55 gint change_handler_id;
56 gint value_handler_id;
62 /* This callback occurs when the text in the label entry box
65 on_label_entry_change (GtkEntry *entry, gpointer data)
69 struct val_labs_dialog *dialog = data;
70 g_assert (dialog->labs);
72 text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
74 text_to_value (text, &v,
75 *var_get_write_format (dialog->pv));
78 if (val_labs_find (dialog->labs, &v))
80 gtk_widget_set_sensitive (dialog->change_button, TRUE);
81 gtk_widget_set_sensitive (dialog->add_button, FALSE);
85 gtk_widget_set_sensitive (dialog->change_button, FALSE);
86 gtk_widget_set_sensitive (dialog->add_button, TRUE);
91 /* Set the TREEVIEW list cursor to the item which has the value VAL */
93 select_treeview_from_value (GtkTreeView *treeview, union value *val)
98 We do this with a linear search through the model --- hardly
99 efficient, but the list is short ... */
102 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
105 for (success = gtk_tree_model_get_iter_first (model, &iter);
107 success = gtk_tree_model_iter_next (model, &iter))
112 gtk_tree_model_get_value (model, &iter, 1, &gvalue);
114 v.f = g_value_get_double (&gvalue);
116 if ( 0 == memcmp (&v, val, sizeof (union value)))
122 path = gtk_tree_model_get_path (model, &iter);
125 gtk_tree_view_set_cursor (treeview, path, 0, 0);
126 gtk_tree_path_free (path);
132 /* This callback occurs when the text in the value entry box is
135 on_value_entry_change (GtkEntry *entry, gpointer data)
139 struct val_labs_dialog *dialog = data;
141 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
144 text_to_value (text, &v,
145 *var_get_write_format (dialog->pv));
148 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
149 dialog->change_handler_id);
151 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),"");
154 if ( (s = val_labs_find (dialog->labs, &v)) )
156 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), s);
157 gtk_widget_set_sensitive (dialog->add_button, FALSE);
158 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
159 select_treeview_from_value (GTK_TREE_VIEW (dialog->treeview), &v);
163 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
164 gtk_widget_set_sensitive (dialog->add_button, TRUE);
167 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
168 dialog->change_handler_id);
172 /* Callback for when the Value Labels dialog is closed using
175 val_labs_ok (GtkWidget *w, gpointer data)
177 struct val_labs_dialog *dialog = data;
179 var_set_value_labels (dialog->pv, dialog->labs);
181 val_labs_destroy (dialog->labs);
185 gtk_widget_hide (dialog->window);
190 /* Callback for when the Value Labels dialog is closed using
193 val_labs_cancel (struct val_labs_dialog *dialog)
195 val_labs_destroy (dialog->labs);
199 gtk_widget_hide (dialog->window);
203 /* Callback for when the Value Labels dialog is closed using
206 on_cancel (GtkWidget *w, gpointer data)
208 struct val_labs_dialog *dialog = data;
210 val_labs_cancel (dialog);
216 /* Callback for when the Value Labels dialog is closed using
217 the window delete button.*/
219 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
221 struct val_labs_dialog *dialog = data;
223 val_labs_cancel (dialog);
229 /* Return the value-label pair currently selected in the dialog box */
231 get_selected_tuple (struct val_labs_dialog *dialog,
232 union value *valuep, const char **label)
234 GtkTreeView *treeview = GTK_TREE_VIEW (dialog->treeview);
237 GValue the_value = {0};
240 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
242 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
244 gtk_tree_selection_get_selected (sel, &model, &iter);
246 gtk_tree_model_get_value (model, &iter, 1, &the_value);
248 value.f = g_value_get_double (&the_value);
249 g_value_unset (&the_value);
254 *label = val_labs_find (dialog->labs, &value);
258 static void repopulate_dialog (struct val_labs_dialog *dialog);
260 /* Callback which occurs when the "Change" button is clicked */
262 on_change (GtkWidget *w, gpointer data)
264 struct val_labs_dialog *dialog = data;
266 const gchar *val_text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
270 text_to_value (val_text, &v,
271 *var_get_write_format (dialog->pv));
273 val_labs_replace (dialog->labs, &v,
274 gtk_entry_get_text (GTK_ENTRY (dialog->label_entry)));
276 gtk_widget_set_sensitive (dialog->change_button, FALSE);
278 repopulate_dialog (dialog);
279 gtk_widget_grab_focus (dialog->value_entry);
284 /* Callback which occurs when the "Add" button is clicked */
286 on_add (GtkWidget *w, gpointer data)
288 struct val_labs_dialog *dialog = data;
292 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
294 text_to_value (text, &v,
295 *var_get_write_format (dialog->pv));
298 if ( ! val_labs_add (dialog->labs, &v,
300 ( GTK_ENTRY (dialog->label_entry)) ) )
303 gtk_widget_set_sensitive (dialog->add_button, FALSE);
305 repopulate_dialog (dialog);
306 gtk_widget_grab_focus (dialog->value_entry);
311 /* Callback which occurs when the "Remove" button is clicked */
313 on_remove (GtkWidget *w, gpointer data)
315 struct val_labs_dialog *dialog = data;
318 const struct val_lab *vl;
320 get_selected_tuple (dialog, &value, NULL);
321 vl = val_labs_lookup (dialog->labs, &value);
323 val_labs_remove (dialog->labs, vl);
325 repopulate_dialog (dialog);
326 gtk_widget_grab_focus (dialog->value_entry);
328 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
335 /* Callback which occurs when a line item is selected in the list of
336 value--label pairs.*/
338 on_select_row (GtkTreeView *treeview, gpointer data)
340 struct val_labs_dialog *dialog = data;
343 const char *label = NULL;
347 get_selected_tuple (dialog, &value, &label);
348 text = value_to_text (value, dialog->var_store->dict, *var_get_write_format (dialog->pv));
350 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
351 dialog->value_handler_id);
353 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), text);
355 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
356 dialog->value_handler_id);
359 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
360 dialog->change_handler_id);
363 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
366 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
367 dialog->change_handler_id);
369 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
370 gtk_widget_set_sensitive (dialog->change_button, FALSE);
374 /* Create a new dialog box
375 (there should normally be only one)*/
376 struct val_labs_dialog *
377 val_labs_dialog_create (GtkWindow *toplevel, PsppireVarStore *var_store)
379 GtkTreeViewColumn *column;
381 GtkCellRenderer *renderer ;
383 GtkBuilder *xml = builder_new ("var-sheet-dialogs.ui");
385 struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
387 dialog->var_store = var_store;
388 dialog->window = get_widget_assert (xml,"val_labs_dialog");
389 dialog->value_entry = get_widget_assert (xml,"value_entry");
390 dialog->label_entry = get_widget_assert (xml,"label_entry");
392 gtk_window_set_transient_for
393 (GTK_WINDOW (dialog->window), toplevel);
395 dialog->add_button = get_widget_assert (xml, "val_labs_add");
396 dialog->remove_button = get_widget_assert (xml, "val_labs_remove");
397 dialog->change_button = get_widget_assert (xml, "val_labs_change");
399 dialog->treeview = get_widget_assert (xml,"treeview1");
401 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->treeview), FALSE);
403 renderer = gtk_cell_renderer_text_new ();
405 column = gtk_tree_view_column_new_with_attributes ("Title",
411 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
413 g_signal_connect (get_widget_assert (xml, "val_labs_cancel"),
415 GTK_SIGNAL_FUNC (on_cancel), dialog);
417 g_signal_connect (dialog->window, "delete-event",
418 GTK_SIGNAL_FUNC (on_delete), dialog);
420 g_signal_connect (get_widget_assert (xml, "val_labs_ok"),
422 GTK_SIGNAL_FUNC (val_labs_ok), dialog);
424 dialog->change_handler_id =
425 g_signal_connect (dialog->label_entry,
427 GTK_SIGNAL_FUNC (on_label_entry_change), dialog);
429 dialog->value_handler_id =
430 g_signal_connect (dialog->value_entry,
432 GTK_SIGNAL_FUNC (on_value_entry_change), dialog);
434 g_signal_connect (dialog->change_button,
436 GTK_SIGNAL_FUNC (on_change), dialog);
439 g_signal_connect (dialog->treeview, "cursor-changed",
440 GTK_SIGNAL_FUNC (on_select_row), dialog);
442 g_signal_connect (dialog->remove_button, "clicked",
443 GTK_SIGNAL_FUNC (on_remove), dialog);
445 g_signal_connect (dialog->add_button, "clicked",
446 GTK_SIGNAL_FUNC (on_add), dialog);
450 g_object_unref (xml);
457 val_labs_dialog_set_target_variable (struct val_labs_dialog *dialog,
458 struct variable *var)
465 /* Populate the components of the dialog box, from the 'labs' member
468 repopulate_dialog (struct val_labs_dialog *dialog)
470 const struct val_lab **labels;
476 GtkListStore *list_store = gtk_list_store_new (2,
480 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
481 dialog->change_handler_id);
482 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
483 dialog->value_handler_id);
485 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
486 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
488 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
489 dialog->value_handler_id);
490 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
491 dialog->change_handler_id);
493 labels = val_labs_sorted (dialog->labs);
494 n_labels = val_labs_count (dialog->labs);
495 for (i = 0; i < n_labels; i++)
497 const struct val_lab *vl = labels[i];
500 value_to_text (vl->value, dialog->var_store->dict,
501 *var_get_write_format (dialog->pv));
503 gchar *const text = g_strdup_printf ("%s = \"%s\"",
504 vstr, val_lab_get_label (vl));
506 gtk_list_store_append (list_store, &iter);
507 gtk_list_store_set (list_store, &iter,
517 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
518 GTK_TREE_MODEL (list_store));
520 g_object_unref (list_store);
524 /* Initialise and display the dialog box */
526 val_labs_dialog_show (struct val_labs_dialog *dialog)
528 const struct val_labs *value_labels;
530 g_assert (!dialog->labs);
532 value_labels = var_get_value_labels (dialog->pv);
535 dialog->labs = val_labs_clone ( value_labels );
537 dialog->labs = val_labs_create ( var_get_width (dialog->pv));
539 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
540 gtk_widget_set_sensitive (dialog->change_button, FALSE);
541 gtk_widget_set_sensitive (dialog->add_button, FALSE);
543 gtk_widget_grab_focus (dialog->value_entry);
545 repopulate_dialog (dialog);
546 gtk_widget_show (dialog->window);