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
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)
341 struct val_labs_dialog *dialog = data;
348 PsppireVarStore *var_store =
349 PSPPIRE_VAR_STORE (psppire_sheet_get_model (dialog->vs));
351 get_selected_tuple (dialog, &value, &label);
352 text = value_to_text (value, *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 labeltext = recode_string (UTF8, psppire_dict_encoding (var_store->dict),
370 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
374 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
375 dialog->change_handler_id);
377 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
378 gtk_widget_set_sensitive (dialog->change_button, FALSE);
382 /* Create a new dialog box
383 (there should normally be only one)*/
384 struct val_labs_dialog *
385 val_labs_dialog_create (GtkWindow *toplevel, PsppireSheet *sheet)
387 GtkTreeViewColumn *column;
389 GtkCellRenderer *renderer ;
391 GtkBuilder *xml = builder_new ("var-sheet-dialogs.ui");
393 struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
395 dialog->window = get_widget_assert (xml,"val_labs_dialog");
396 dialog->value_entry = get_widget_assert (xml,"value_entry");
397 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 GTK_SIGNAL_FUNC (on_cancel), dialog);
425 g_signal_connect (dialog->window, "delete-event",
426 GTK_SIGNAL_FUNC (on_delete), dialog);
428 g_signal_connect (get_widget_assert (xml, "val_labs_ok"),
430 GTK_SIGNAL_FUNC (val_labs_ok), dialog);
432 dialog->change_handler_id =
433 g_signal_connect (dialog->label_entry,
435 GTK_SIGNAL_FUNC (on_label_entry_change), dialog);
437 dialog->value_handler_id =
438 g_signal_connect (dialog->value_entry,
440 GTK_SIGNAL_FUNC (on_value_entry_change), dialog);
442 g_signal_connect (dialog->change_button,
444 GTK_SIGNAL_FUNC (on_change), dialog);
447 g_signal_connect (dialog->treeview, "cursor-changed",
448 GTK_SIGNAL_FUNC (on_select_row), dialog);
450 g_signal_connect (dialog->remove_button, "clicked",
451 GTK_SIGNAL_FUNC (on_remove), dialog);
453 g_signal_connect (dialog->add_button, "clicked",
454 GTK_SIGNAL_FUNC (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 PsppireVarStore *var_store =
485 PSPPIRE_VAR_STORE (psppire_sheet_get_model (dialog->vs));
487 GtkListStore *list_store = gtk_list_store_new (2,
491 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
492 dialog->change_handler_id);
493 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
494 dialog->value_handler_id);
496 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
497 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
499 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
500 dialog->value_handler_id);
501 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
502 dialog->change_handler_id);
504 labels = val_labs_sorted (dialog->labs);
505 n_labels = val_labs_count (dialog->labs);
506 for (i = 0; i < n_labels; i++)
508 const struct val_lab *vl = labels[i];
511 value_to_text (vl->value,
512 *var_get_write_format (dialog->pv));
516 psppire_dict_encoding (var_store->dict),
517 val_lab_get_label (vl), -1);
519 gchar *const text = g_strdup_printf ("%s = \"%s\"",
522 gtk_list_store_append (list_store, &iter);
523 gtk_list_store_set (list_store, &iter,
534 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
535 GTK_TREE_MODEL (list_store));
537 g_object_unref (list_store);
541 /* Initialise and display the dialog box */
543 val_labs_dialog_show (struct val_labs_dialog *dialog)
545 const struct val_labs *value_labels;
547 g_assert (!dialog->labs);
549 value_labels = var_get_value_labels (dialog->pv);
552 dialog->labs = val_labs_clone ( value_labels );
554 dialog->labs = val_labs_create ( var_get_width (dialog->pv));
556 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
557 gtk_widget_set_sensitive (dialog->change_button, FALSE);
558 gtk_widget_set_sensitive (dialog->add_button, FALSE);
560 gtk_widget_grab_focus (dialog->value_entry);
562 repopulate_dialog (dialog);
563 gtk_widget_show (dialog->window);