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 dialog->var_store->dict,
76 *var_get_write_format (dialog->pv));
79 if (val_labs_find (dialog->labs, &v))
81 gtk_widget_set_sensitive (dialog->change_button, TRUE);
82 gtk_widget_set_sensitive (dialog->add_button, FALSE);
86 gtk_widget_set_sensitive (dialog->change_button, FALSE);
87 gtk_widget_set_sensitive (dialog->add_button, TRUE);
92 /* Set the TREEVIEW list cursor to the item which has the value VAL */
94 select_treeview_from_value (GtkTreeView *treeview, union value *val)
99 We do this with a linear search through the model --- hardly
100 efficient, but the list is short ... */
103 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
106 for (success = gtk_tree_model_get_iter_first (model, &iter);
108 success = gtk_tree_model_iter_next (model, &iter))
113 gtk_tree_model_get_value (model, &iter, 1, &gvalue);
115 v.f = g_value_get_double (&gvalue);
117 if ( 0 == memcmp (&v, val, sizeof (union value)))
123 path = gtk_tree_model_get_path (model, &iter);
126 gtk_tree_view_set_cursor (treeview, path, 0, 0);
127 gtk_tree_path_free (path);
133 /* This callback occurs when the text in the value entry box is
136 on_value_entry_change (GtkEntry *entry, gpointer data)
140 struct val_labs_dialog *dialog = data;
142 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
145 text_to_value (text, &v,
146 dialog->var_store->dict,
147 *var_get_write_format (dialog->pv));
150 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
151 dialog->change_handler_id);
153 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),"");
156 if ( (s = val_labs_find (dialog->labs, &v)) )
158 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), s);
159 gtk_widget_set_sensitive (dialog->add_button, FALSE);
160 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
161 select_treeview_from_value (GTK_TREE_VIEW (dialog->treeview), &v);
165 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
166 gtk_widget_set_sensitive (dialog->add_button, TRUE);
169 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
170 dialog->change_handler_id);
174 /* Callback for when the Value Labels dialog is closed using
177 val_labs_ok (GtkWidget *w, gpointer data)
179 struct val_labs_dialog *dialog = data;
181 var_set_value_labels (dialog->pv, dialog->labs);
183 val_labs_destroy (dialog->labs);
187 gtk_widget_hide (dialog->window);
192 /* Callback for when the Value Labels dialog is closed using
195 val_labs_cancel (struct val_labs_dialog *dialog)
197 val_labs_destroy (dialog->labs);
201 gtk_widget_hide (dialog->window);
205 /* Callback for when the Value Labels dialog is closed using
208 on_cancel (GtkWidget *w, gpointer data)
210 struct val_labs_dialog *dialog = data;
212 val_labs_cancel (dialog);
218 /* Callback for when the Value Labels dialog is closed using
219 the window delete button.*/
221 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
223 struct val_labs_dialog *dialog = data;
225 val_labs_cancel (dialog);
231 /* Return the value-label pair currently selected in the dialog box */
233 get_selected_tuple (struct val_labs_dialog *dialog,
234 union value *valuep, const char **label)
236 GtkTreeView *treeview = GTK_TREE_VIEW (dialog->treeview);
239 GValue the_value = {0};
242 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
244 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
246 gtk_tree_selection_get_selected (sel, &model, &iter);
248 gtk_tree_model_get_value (model, &iter, 1, &the_value);
250 value.f = g_value_get_double (&the_value);
251 g_value_unset (&the_value);
256 *label = val_labs_find (dialog->labs, &value);
260 static void repopulate_dialog (struct val_labs_dialog *dialog);
262 /* Callback which occurs when the "Change" button is clicked */
264 on_change (GtkWidget *w, gpointer data)
266 struct val_labs_dialog *dialog = data;
268 const gchar *val_text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
272 text_to_value (val_text, &v,
273 dialog->var_store->dict,
274 *var_get_write_format (dialog->pv));
276 val_labs_replace (dialog->labs, &v,
277 gtk_entry_get_text (GTK_ENTRY (dialog->label_entry)));
279 gtk_widget_set_sensitive (dialog->change_button, FALSE);
281 repopulate_dialog (dialog);
282 gtk_widget_grab_focus (dialog->value_entry);
287 /* Callback which occurs when the "Add" button is clicked */
289 on_add (GtkWidget *w, gpointer data)
291 struct val_labs_dialog *dialog = data;
295 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
297 text_to_value (text, &v,
298 dialog->var_store->dict,
299 *var_get_write_format (dialog->pv));
302 if ( ! val_labs_add (dialog->labs, &v,
304 ( GTK_ENTRY (dialog->label_entry)) ) )
307 gtk_widget_set_sensitive (dialog->add_button, FALSE);
309 repopulate_dialog (dialog);
310 gtk_widget_grab_focus (dialog->value_entry);
315 /* Callback which occurs when the "Remove" button is clicked */
317 on_remove (GtkWidget *w, gpointer data)
319 struct val_labs_dialog *dialog = data;
322 const struct val_lab *vl;
324 get_selected_tuple (dialog, &value, NULL);
325 vl = val_labs_lookup (dialog->labs, &value);
327 val_labs_remove (dialog->labs, vl);
329 repopulate_dialog (dialog);
330 gtk_widget_grab_focus (dialog->value_entry);
332 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
339 /* Callback which occurs when a line item is selected in the list of
340 value--label pairs.*/
342 on_select_row (GtkTreeView *treeview, gpointer data)
344 struct val_labs_dialog *dialog = data;
347 const char *label = NULL;
351 get_selected_tuple (dialog, &value, &label);
352 text = value_to_text (value, dialog->var_store->dict, *var_get_write_format (dialog->pv));
354 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
355 dialog->value_handler_id);
357 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), text);
359 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
360 dialog->value_handler_id);
363 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
364 dialog->change_handler_id);
367 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
370 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
371 dialog->change_handler_id);
373 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
374 gtk_widget_set_sensitive (dialog->change_button, FALSE);
378 /* Create a new dialog box
379 (there should normally be only one)*/
380 struct val_labs_dialog *
381 val_labs_dialog_create (GtkWindow *toplevel, PsppireVarStore *var_store)
383 GtkTreeViewColumn *column;
385 GtkCellRenderer *renderer ;
387 GtkBuilder *xml = builder_new ("var-sheet-dialogs.ui");
389 struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
391 dialog->var_store = var_store;
392 dialog->window = get_widget_assert (xml,"val_labs_dialog");
393 dialog->value_entry = get_widget_assert (xml,"value_entry");
394 dialog->label_entry = get_widget_assert (xml,"label_entry");
396 gtk_window_set_transient_for
397 (GTK_WINDOW (dialog->window), toplevel);
399 dialog->add_button = get_widget_assert (xml, "val_labs_add");
400 dialog->remove_button = get_widget_assert (xml, "val_labs_remove");
401 dialog->change_button = get_widget_assert (xml, "val_labs_change");
403 dialog->treeview = get_widget_assert (xml,"treeview1");
405 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->treeview), FALSE);
407 renderer = gtk_cell_renderer_text_new ();
409 column = gtk_tree_view_column_new_with_attributes ("Title",
415 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
417 g_signal_connect (get_widget_assert (xml, "val_labs_cancel"),
419 GTK_SIGNAL_FUNC (on_cancel), dialog);
421 g_signal_connect (dialog->window, "delete-event",
422 GTK_SIGNAL_FUNC (on_delete), dialog);
424 g_signal_connect (get_widget_assert (xml, "val_labs_ok"),
426 GTK_SIGNAL_FUNC (val_labs_ok), dialog);
428 dialog->change_handler_id =
429 g_signal_connect (dialog->label_entry,
431 GTK_SIGNAL_FUNC (on_label_entry_change), dialog);
433 dialog->value_handler_id =
434 g_signal_connect (dialog->value_entry,
436 GTK_SIGNAL_FUNC (on_value_entry_change), dialog);
438 g_signal_connect (dialog->change_button,
440 GTK_SIGNAL_FUNC (on_change), dialog);
443 g_signal_connect (dialog->treeview, "cursor-changed",
444 GTK_SIGNAL_FUNC (on_select_row), dialog);
446 g_signal_connect (dialog->remove_button, "clicked",
447 GTK_SIGNAL_FUNC (on_remove), dialog);
449 g_signal_connect (dialog->add_button, "clicked",
450 GTK_SIGNAL_FUNC (on_add), dialog);
454 g_object_unref (xml);
461 val_labs_dialog_set_target_variable (struct val_labs_dialog *dialog,
462 struct variable *var)
469 /* Populate the components of the dialog box, from the 'labs' member
472 repopulate_dialog (struct val_labs_dialog *dialog)
474 const struct val_lab **labels;
480 GtkListStore *list_store = gtk_list_store_new (2,
484 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
485 dialog->change_handler_id);
486 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
487 dialog->value_handler_id);
489 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
490 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
492 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
493 dialog->value_handler_id);
494 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
495 dialog->change_handler_id);
497 labels = val_labs_sorted (dialog->labs);
498 n_labels = val_labs_count (dialog->labs);
499 for (i = 0; i < n_labels; i++)
501 const struct val_lab *vl = labels[i];
504 value_to_text (vl->value, dialog->var_store->dict,
505 *var_get_write_format (dialog->pv));
507 gchar *const text = g_strdup_printf ("%s = \"%s\"",
508 vstr, val_lab_get_label (vl));
510 gtk_list_store_append (list_store, &iter);
511 gtk_list_store_set (list_store, &iter,
521 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
522 GTK_TREE_MODEL (list_store));
524 g_object_unref (list_store);
528 /* Initialise and display the dialog box */
530 val_labs_dialog_show (struct val_labs_dialog *dialog)
532 const struct val_labs *value_labels;
534 g_assert (!dialog->labs);
536 value_labels = var_get_value_labels (dialog->pv);
539 dialog->labs = val_labs_clone ( value_labels );
541 dialog->labs = val_labs_create ( var_get_width (dialog->pv));
543 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
544 gtk_widget_set_sensitive (dialog->change_button, FALSE);
545 gtk_widget_set_sensitive (dialog->add_button, FALSE);
547 gtk_widget_grab_focus (dialog->value_entry);
549 repopulate_dialog (dialog);
550 gtk_widget_show (dialog->window);