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>
34 #define _(msgid) gettext (msgid)
35 #define N_(msgid) msgid
37 struct val_labs_dialog
41 PsppireVarStore *var_store;
44 /* The variable to be updated */
47 /* Local copy of labels */
48 struct val_labs *labs;
51 GtkWidget *add_button;
52 GtkWidget *remove_button;
53 GtkWidget *change_button;
56 GtkWidget *value_entry;
57 GtkWidget *label_entry;
59 /* Signal handler ids */
60 gint change_handler_id;
61 gint value_handler_id;
67 /* This callback occurs when the text in the label entry box
70 on_label_entry_change (GtkEntry *entry, gpointer data)
74 struct val_labs_dialog *dialog = data;
75 g_assert (dialog->labs);
77 text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
84 if (val_labs_find (dialog->labs, &v))
86 gtk_widget_set_sensitive (dialog->change_button, TRUE);
87 gtk_widget_set_sensitive (dialog->add_button, FALSE);
91 gtk_widget_set_sensitive (dialog->change_button, FALSE);
92 gtk_widget_set_sensitive (dialog->add_button, TRUE);
95 value_destroy (&v, var_get_width (dialog->pv));
99 /* Set the TREEVIEW list cursor to the item which has the value VAL */
101 select_treeview_from_value (GtkTreeView *treeview, union value *val)
106 We do this with a linear search through the model --- hardly
107 efficient, but the list is short ... */
110 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
113 for (success = gtk_tree_model_get_iter_first (model, &iter);
115 success = gtk_tree_model_iter_next (model, &iter))
120 gtk_tree_model_get_value (model, &iter, 1, &gvalue);
122 v.f = g_value_get_double (&gvalue);
124 if ( 0 == memcmp (&v, val, sizeof (union value)))
130 path = gtk_tree_model_get_path (model, &iter);
133 gtk_tree_view_set_cursor (treeview, path, 0, 0);
134 gtk_tree_path_free (path);
140 /* This callback occurs when the text in the value entry box is
143 on_value_entry_change (GtkEntry *entry, gpointer data)
147 struct val_labs_dialog *dialog = data;
149 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
158 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
159 dialog->change_handler_id);
161 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),"");
164 if ( (s = val_labs_find (dialog->labs, &v)) )
166 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), s);
167 gtk_widget_set_sensitive (dialog->add_button, FALSE);
168 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
169 select_treeview_from_value (GTK_TREE_VIEW (dialog->treeview), &v);
173 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
174 gtk_widget_set_sensitive (dialog->add_button, TRUE);
177 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
178 dialog->change_handler_id);
180 value_destroy (&v, var_get_width (dialog->pv));
184 /* Callback for when the Value Labels dialog is closed using
187 val_labs_ok (GtkWidget *w, gpointer data)
189 struct val_labs_dialog *dialog = data;
191 var_set_value_labels (dialog->pv, dialog->labs);
193 val_labs_destroy (dialog->labs);
197 gtk_widget_hide (dialog->window);
202 /* Callback for when the Value Labels dialog is closed using
205 val_labs_cancel (struct val_labs_dialog *dialog)
207 val_labs_destroy (dialog->labs);
211 gtk_widget_hide (dialog->window);
215 /* Callback for when the Value Labels dialog is closed using
218 on_cancel (GtkWidget *w, gpointer data)
220 struct val_labs_dialog *dialog = data;
222 val_labs_cancel (dialog);
226 /* Callback for when the Value Labels dialog is closed using
227 the window delete button.*/
229 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
231 struct val_labs_dialog *dialog = data;
233 val_labs_cancel (dialog);
239 /* Return the value-label pair currently selected in the dialog box */
241 get_selected_tuple (struct val_labs_dialog *dialog,
242 union value *valuep, const char **label)
244 GtkTreeView *treeview = GTK_TREE_VIEW (dialog->treeview);
247 GValue the_value = {0};
250 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
252 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
254 gtk_tree_selection_get_selected (sel, &model, &iter);
256 gtk_tree_model_get_value (model, &iter, 1, &the_value);
258 value.f = g_value_get_double (&the_value);
259 g_value_unset (&the_value);
264 *label = val_labs_find (dialog->labs, &value);
268 static void repopulate_dialog (struct val_labs_dialog *dialog);
270 /* Callback which occurs when the "Change" button is clicked */
272 on_change (GtkWidget *w, gpointer data)
274 struct val_labs_dialog *dialog = data;
276 const gchar *val_text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
280 text_to_value (val_text,
285 val_labs_replace (dialog->labs, &v,
286 gtk_entry_get_text (GTK_ENTRY (dialog->label_entry)));
288 gtk_widget_set_sensitive (dialog->change_button, FALSE);
290 repopulate_dialog (dialog);
291 gtk_widget_grab_focus (dialog->value_entry);
293 value_destroy (&v, var_get_width (dialog->pv));
296 /* Callback which occurs when the "Add" button is clicked */
298 on_add (GtkWidget *w, gpointer data)
300 struct val_labs_dialog *dialog = data;
304 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
311 if (val_labs_add (dialog->labs, &v,
313 ( GTK_ENTRY (dialog->label_entry)) ) )
315 gtk_widget_set_sensitive (dialog->add_button, FALSE);
317 repopulate_dialog (dialog);
318 gtk_widget_grab_focus (dialog->value_entry);
321 value_destroy (&v, var_get_width (dialog->pv));
324 /* Callback which occurs when the "Remove" button is clicked */
326 on_remove (GtkWidget *w, gpointer data)
328 struct val_labs_dialog *dialog = data;
333 get_selected_tuple (dialog, &value, NULL);
334 vl = val_labs_lookup (dialog->labs, &value);
336 val_labs_remove (dialog->labs, vl);
338 repopulate_dialog (dialog);
339 gtk_widget_grab_focus (dialog->value_entry);
341 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
346 /* Callback which occurs when a line item is selected in the list of
347 value--label pairs.*/
349 on_select_row (GtkTreeView *treeview, gpointer data)
351 struct val_labs_dialog *dialog = data;
354 const char *label = NULL;
358 get_selected_tuple (dialog, &value, &label);
359 text = value_to_text (value, dialog->dict, *var_get_write_format (dialog->pv));
361 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
362 dialog->value_handler_id);
364 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), text);
366 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
367 dialog->value_handler_id);
370 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
371 dialog->change_handler_id);
374 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
377 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
378 dialog->change_handler_id);
380 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
381 gtk_widget_set_sensitive (dialog->change_button, FALSE);
385 /* Create a new dialog box
386 (there should normally be only one)*/
387 struct val_labs_dialog *
388 val_labs_dialog_create (GtkWindow *toplevel, PsppireVarStore *var_store)
390 GtkTreeViewColumn *column;
392 GtkCellRenderer *renderer ;
394 GtkBuilder *xml = builder_new ("var-sheet-dialogs.ui");
396 struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
398 dialog->var_store = var_store;
399 g_object_get (var_store, "dictionary", &dialog->dict, NULL);
400 dialog->window = get_widget_assert (xml,"val_labs_dialog");
401 dialog->value_entry = get_widget_assert (xml,"value_entry");
402 dialog->label_entry = get_widget_assert (xml,"label_entry");
404 gtk_window_set_transient_for
405 (GTK_WINDOW (dialog->window), toplevel);
407 dialog->add_button = get_widget_assert (xml, "val_labs_add");
408 dialog->remove_button = get_widget_assert (xml, "val_labs_remove");
409 dialog->change_button = get_widget_assert (xml, "val_labs_change");
411 dialog->treeview = get_widget_assert (xml,"treeview1");
413 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->treeview), FALSE);
415 renderer = gtk_cell_renderer_text_new ();
417 column = gtk_tree_view_column_new_with_attributes ("Title",
423 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
425 g_signal_connect (get_widget_assert (xml, "val_labs_cancel"),
427 G_CALLBACK (on_cancel), dialog);
429 g_signal_connect (dialog->window, "delete-event",
430 G_CALLBACK (on_delete), dialog);
432 g_signal_connect (get_widget_assert (xml, "val_labs_ok"),
434 G_CALLBACK (val_labs_ok), dialog);
436 dialog->change_handler_id =
437 g_signal_connect (dialog->label_entry,
439 G_CALLBACK (on_label_entry_change), dialog);
441 dialog->value_handler_id =
442 g_signal_connect (dialog->value_entry,
444 G_CALLBACK (on_value_entry_change), dialog);
446 g_signal_connect (dialog->change_button,
448 G_CALLBACK (on_change), dialog);
451 g_signal_connect (dialog->treeview, "cursor-changed",
452 G_CALLBACK (on_select_row), dialog);
454 g_signal_connect (dialog->remove_button, "clicked",
455 G_CALLBACK (on_remove), dialog);
457 g_signal_connect (dialog->add_button, "clicked",
458 G_CALLBACK (on_add), dialog);
462 g_object_unref (xml);
469 val_labs_dialog_set_target_variable (struct val_labs_dialog *dialog,
470 struct variable *var)
477 /* Populate the components of the dialog box, from the 'labs' member
480 repopulate_dialog (struct val_labs_dialog *dialog)
482 const struct val_lab **labels;
488 GtkListStore *list_store = gtk_list_store_new (2,
492 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
493 dialog->change_handler_id);
494 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
495 dialog->value_handler_id);
497 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
498 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
500 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
501 dialog->value_handler_id);
502 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
503 dialog->change_handler_id);
505 labels = val_labs_sorted (dialog->labs);
506 n_labels = val_labs_count (dialog->labs);
507 for (i = 0; i < n_labels; i++)
509 const struct val_lab *vl = labels[i];
512 value_to_text (vl->value, dialog->dict,
513 *var_get_write_format (dialog->pv));
515 gchar *const text = g_strdup_printf (_("%s = `%s'"),
516 vstr, val_lab_get_label (vl));
518 gtk_list_store_append (list_store, &iter);
519 gtk_list_store_set (list_store, &iter,
529 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
530 GTK_TREE_MODEL (list_store));
532 g_object_unref (list_store);
536 /* Initialise and display the dialog box */
538 val_labs_dialog_show (struct val_labs_dialog *dialog)
540 const struct val_labs *value_labels;
542 g_assert (!dialog->labs);
544 value_labels = var_get_value_labels (dialog->pv);
547 dialog->labs = val_labs_clone ( value_labels );
549 dialog->labs = val_labs_create ( var_get_width (dialog->pv));
551 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
552 gtk_widget_set_sensitive (dialog->change_button, FALSE);
553 gtk_widget_set_sensitive (dialog->add_button, FALSE);
555 gtk_widget_grab_focus (dialog->value_entry);
557 repopulate_dialog (dialog);
558 gtk_widget_show (dialog->window);