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;
40 /* The variable to be updated */
43 /* Local copy of labels */
44 struct val_labs *labs;
47 GtkWidget *add_button;
48 GtkWidget *remove_button;
49 GtkWidget *change_button;
52 GtkWidget *value_entry;
53 GtkWidget *label_entry;
55 /* Signal handler ids */
56 gint change_handler_id;
57 gint value_handler_id;
63 /* This callback occurs when the text in the label entry box
66 on_label_entry_change (GtkEntry *entry, gpointer data)
70 struct val_labs_dialog *dialog = data;
71 g_assert (dialog->labs);
73 text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
80 if (val_labs_find (dialog->labs, &v))
82 gtk_widget_set_sensitive (dialog->change_button, TRUE);
83 gtk_widget_set_sensitive (dialog->add_button, FALSE);
87 gtk_widget_set_sensitive (dialog->change_button, FALSE);
88 gtk_widget_set_sensitive (dialog->add_button, TRUE);
91 value_destroy (&v, var_get_width (dialog->pv));
95 /* Set the TREEVIEW list cursor to the item which has the value VAL */
97 select_treeview_from_value (GtkTreeView *treeview, union value *val)
102 We do this with a linear search through the model --- hardly
103 efficient, but the list is short ... */
106 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
109 for (success = gtk_tree_model_get_iter_first (model, &iter);
111 success = gtk_tree_model_iter_next (model, &iter))
116 gtk_tree_model_get_value (model, &iter, 1, &gvalue);
118 v.f = g_value_get_double (&gvalue);
120 if ( 0 == memcmp (&v, val, sizeof (union value)))
126 path = gtk_tree_model_get_path (model, &iter);
129 gtk_tree_view_set_cursor (treeview, path, 0, 0);
130 gtk_tree_path_free (path);
136 /* This callback occurs when the text in the value entry box is
139 on_value_entry_change (GtkEntry *entry, gpointer data)
143 struct val_labs_dialog *dialog = data;
145 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
154 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
155 dialog->change_handler_id);
157 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),"");
160 if ( (s = val_labs_find (dialog->labs, &v)) )
162 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), s);
163 gtk_widget_set_sensitive (dialog->add_button, FALSE);
164 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
165 select_treeview_from_value (GTK_TREE_VIEW (dialog->treeview), &v);
169 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
170 gtk_widget_set_sensitive (dialog->add_button, TRUE);
173 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
174 dialog->change_handler_id);
176 value_destroy (&v, var_get_width (dialog->pv));
180 /* Callback for when the Value Labels dialog is closed using
183 val_labs_ok (GtkWidget *w, gpointer data)
185 struct val_labs_dialog *dialog = data;
187 var_set_value_labels (dialog->pv, dialog->labs);
189 val_labs_destroy (dialog->labs);
193 gtk_widget_hide (dialog->window);
198 /* Callback for when the Value Labels dialog is closed using
201 val_labs_cancel (struct val_labs_dialog *dialog)
203 val_labs_destroy (dialog->labs);
207 gtk_widget_hide (dialog->window);
211 /* Callback for when the Value Labels dialog is closed using
214 on_cancel (GtkWidget *w, gpointer data)
216 struct val_labs_dialog *dialog = data;
218 val_labs_cancel (dialog);
222 /* Callback for when the Value Labels dialog is closed using
223 the window delete button.*/
225 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
227 struct val_labs_dialog *dialog = data;
229 val_labs_cancel (dialog);
235 /* Return the value-label pair currently selected in the dialog box */
237 get_selected_tuple (struct val_labs_dialog *dialog,
238 union value *valuep, const char **label)
240 GtkTreeView *treeview = GTK_TREE_VIEW (dialog->treeview);
243 GValue the_value = {0};
246 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
248 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
250 gtk_tree_selection_get_selected (sel, &model, &iter);
252 gtk_tree_model_get_value (model, &iter, 1, &the_value);
254 value.f = g_value_get_double (&the_value);
255 g_value_unset (&the_value);
260 *label = val_labs_find (dialog->labs, &value);
264 static void repopulate_dialog (struct val_labs_dialog *dialog);
266 /* Callback which occurs when the "Change" button is clicked */
268 on_change (GtkWidget *w, gpointer data)
270 struct val_labs_dialog *dialog = data;
272 const gchar *val_text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
276 text_to_value (val_text,
281 val_labs_replace (dialog->labs, &v,
282 gtk_entry_get_text (GTK_ENTRY (dialog->label_entry)));
284 gtk_widget_set_sensitive (dialog->change_button, FALSE);
286 repopulate_dialog (dialog);
287 gtk_widget_grab_focus (dialog->value_entry);
289 value_destroy (&v, var_get_width (dialog->pv));
292 /* Callback which occurs when the "Add" button is clicked */
294 on_add (GtkWidget *w, gpointer data)
296 struct val_labs_dialog *dialog = data;
300 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
307 if (val_labs_add (dialog->labs, &v,
309 ( GTK_ENTRY (dialog->label_entry)) ) )
311 gtk_widget_set_sensitive (dialog->add_button, FALSE);
313 repopulate_dialog (dialog);
314 gtk_widget_grab_focus (dialog->value_entry);
317 value_destroy (&v, var_get_width (dialog->pv));
320 /* Callback which occurs when the "Remove" button is clicked */
322 on_remove (GtkWidget *w, gpointer data)
324 struct val_labs_dialog *dialog = data;
329 get_selected_tuple (dialog, &value, NULL);
330 vl = val_labs_lookup (dialog->labs, &value);
332 val_labs_remove (dialog->labs, vl);
334 repopulate_dialog (dialog);
335 gtk_widget_grab_focus (dialog->value_entry);
337 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
342 /* Callback which occurs when a line item is selected in the list of
343 value--label pairs.*/
345 on_select_row (GtkTreeView *treeview, gpointer data)
347 struct val_labs_dialog *dialog = data;
350 const char *label = NULL;
354 get_selected_tuple (dialog, &value, &label);
355 text = value_to_text (value, dialog->dict, *var_get_write_format (dialog->pv));
357 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
358 dialog->value_handler_id);
360 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), text);
362 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
363 dialog->value_handler_id);
366 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
367 dialog->change_handler_id);
370 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
373 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
374 dialog->change_handler_id);
376 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
377 gtk_widget_set_sensitive (dialog->change_button, FALSE);
381 /* Create a new dialog box
382 (there should normally be only one)*/
383 struct val_labs_dialog *
384 val_labs_dialog_create (GtkWindow *toplevel, PsppireVarStore *var_store)
386 GtkTreeViewColumn *column;
388 GtkCellRenderer *renderer ;
390 GtkBuilder *xml = builder_new ("var-sheet-dialogs.ui");
392 struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
394 dialog->var_store = var_store;
395 g_object_get (var_store, "dictionary", &dialog->dict, NULL);
396 dialog->window = get_widget_assert (xml,"val_labs_dialog");
397 dialog->value_entry = get_widget_assert (xml,"value_entry");
398 dialog->label_entry = get_widget_assert (xml,"label_entry");
400 gtk_window_set_transient_for
401 (GTK_WINDOW (dialog->window), toplevel);
403 dialog->add_button = get_widget_assert (xml, "val_labs_add");
404 dialog->remove_button = get_widget_assert (xml, "val_labs_remove");
405 dialog->change_button = get_widget_assert (xml, "val_labs_change");
407 dialog->treeview = get_widget_assert (xml,"treeview1");
409 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->treeview), FALSE);
411 renderer = gtk_cell_renderer_text_new ();
413 column = gtk_tree_view_column_new_with_attributes ("Title",
419 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
421 g_signal_connect (get_widget_assert (xml, "val_labs_cancel"),
423 G_CALLBACK (on_cancel), dialog);
425 g_signal_connect (dialog->window, "delete-event",
426 G_CALLBACK (on_delete), dialog);
428 g_signal_connect (get_widget_assert (xml, "val_labs_ok"),
430 G_CALLBACK (val_labs_ok), dialog);
432 dialog->change_handler_id =
433 g_signal_connect (dialog->label_entry,
435 G_CALLBACK (on_label_entry_change), dialog);
437 dialog->value_handler_id =
438 g_signal_connect (dialog->value_entry,
440 G_CALLBACK (on_value_entry_change), dialog);
442 g_signal_connect (dialog->change_button,
444 G_CALLBACK (on_change), dialog);
447 g_signal_connect (dialog->treeview, "cursor-changed",
448 G_CALLBACK (on_select_row), dialog);
450 g_signal_connect (dialog->remove_button, "clicked",
451 G_CALLBACK (on_remove), dialog);
453 g_signal_connect (dialog->add_button, "clicked",
454 G_CALLBACK (on_add), dialog);
458 g_object_unref (xml);
465 val_labs_dialog_set_target_variable (struct val_labs_dialog *dialog,
466 struct variable *var)
473 /* Populate the components of the dialog box, from the 'labs' member
476 repopulate_dialog (struct val_labs_dialog *dialog)
478 const struct val_lab **labels;
484 GtkListStore *list_store = gtk_list_store_new (2,
488 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
489 dialog->change_handler_id);
490 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
491 dialog->value_handler_id);
493 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
494 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
496 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
497 dialog->value_handler_id);
498 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
499 dialog->change_handler_id);
501 labels = val_labs_sorted (dialog->labs);
502 n_labels = val_labs_count (dialog->labs);
503 for (i = 0; i < n_labels; i++)
505 const struct val_lab *vl = labels[i];
508 value_to_text (vl->value, dialog->dict,
509 *var_get_write_format (dialog->pv));
511 gchar *const text = g_strdup_printf ("%s = \"%s\"",
512 vstr, val_lab_get_label (vl));
514 gtk_list_store_append (list_store, &iter);
515 gtk_list_store_set (list_store, &iter,
525 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
526 GTK_TREE_MODEL (list_store));
528 g_object_unref (list_store);
532 /* Initialise and display the dialog box */
534 val_labs_dialog_show (struct val_labs_dialog *dialog)
536 const struct val_labs *value_labels;
538 g_assert (!dialog->labs);
540 value_labels = var_get_value_labels (dialog->pv);
543 dialog->labs = val_labs_clone ( value_labels );
545 dialog->labs = val_labs_create ( var_get_width (dialog->pv));
547 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
548 gtk_widget_set_sensitive (dialog->change_button, FALSE);
549 gtk_widget_set_sensitive (dialog->add_button, FALSE);
551 gtk_widget_grab_focus (dialog->value_entry);
553 repopulate_dialog (dialog);
554 gtk_widget_show (dialog->window);