1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2005 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>
31 struct val_labs_dialog
35 /* The variable to be updated */
38 /* Local copy of labels */
39 struct val_labs *labs;
42 GtkWidget *add_button;
43 GtkWidget *remove_button;
44 GtkWidget *change_button;
47 GtkWidget *value_entry;
48 GtkWidget *label_entry;
50 /* Signal handler ids */
51 gint change_handler_id;
52 gint value_handler_id;
58 /* This callback occurs when the text in the label entry box
61 on_label_entry_change (GtkEntry *entry, gpointer data)
65 struct val_labs_dialog *dialog = data;
66 g_assert (dialog->labs);
68 text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
70 text_to_value (text, &v,
71 *var_get_write_format (dialog->pv));
74 if ( val_labs_find (dialog->labs, v) )
76 gtk_widget_set_sensitive (dialog->change_button, TRUE);
77 gtk_widget_set_sensitive (dialog->add_button, FALSE);
81 gtk_widget_set_sensitive (dialog->change_button, FALSE);
82 gtk_widget_set_sensitive (dialog->add_button, TRUE);
87 /* Set the TREEVIEW list cursor to the item which has the value VAL */
89 select_treeview_from_value (GtkTreeView *treeview, union value *val)
94 We do this with a linear search through the model --- hardly
95 efficient, but the list is short ... */
98 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
101 for (success = gtk_tree_model_get_iter_first (model, &iter);
103 success = gtk_tree_model_iter_next (model, &iter))
108 gtk_tree_model_get_value (model, &iter, 1, &gvalue);
110 v.f = g_value_get_double (&gvalue);
112 if ( 0 == memcmp (&v, val, sizeof (union value)))
118 path = gtk_tree_model_get_path (model, &iter);
121 gtk_tree_view_set_cursor (treeview, path, 0, 0);
122 gtk_tree_path_free (path);
128 /* This callback occurs when the text in the value entry box is
131 on_value_entry_change (GtkEntry *entry, gpointer data)
135 struct val_labs_dialog *dialog = data;
137 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
140 text_to_value (text, &v,
141 *var_get_write_format (dialog->pv));
144 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
145 dialog->change_handler_id);
147 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),"");
150 if ( (s = val_labs_find (dialog->labs, v)) )
152 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), s);
153 gtk_widget_set_sensitive (dialog->add_button, FALSE);
154 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
155 select_treeview_from_value (GTK_TREE_VIEW (dialog->treeview), &v);
159 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
160 gtk_widget_set_sensitive (dialog->add_button, TRUE);
163 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
164 dialog->change_handler_id);
168 /* Callback for when the Value Labels dialog is closed using
171 val_labs_ok (GtkWidget *w, gpointer data)
173 struct val_labs_dialog *dialog = data;
175 var_set_value_labels (dialog->pv, dialog->labs);
177 val_labs_destroy (dialog->labs);
181 gtk_widget_hide (dialog->window);
186 /* Callback for when the Value Labels dialog is closed using
189 val_labs_cancel (struct val_labs_dialog *dialog)
191 val_labs_destroy (dialog->labs);
195 gtk_widget_hide (dialog->window);
199 /* Callback for when the Value Labels dialog is closed using
202 on_cancel (GtkWidget *w, gpointer data)
204 struct val_labs_dialog *dialog = data;
206 val_labs_cancel (dialog);
212 /* Callback for when the Value Labels dialog is closed using
213 the window delete button.*/
215 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
217 struct val_labs_dialog *dialog = data;
219 val_labs_cancel (dialog);
225 /* Return the value-label pair currently selected in the dialog box */
226 static struct val_lab *
227 get_selected_tuple (struct val_labs_dialog *dialog)
229 GtkTreeView *treeview = GTK_TREE_VIEW (dialog->treeview);
230 static struct val_lab vl;
233 GValue the_value = {0};
235 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
237 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
239 gtk_tree_selection_get_selected (sel, &model, &iter);
241 gtk_tree_model_get_value (model, &iter, 1, &the_value);
243 vl.value.f = g_value_get_double (&the_value);
244 g_value_unset (&the_value);
246 vl.label = val_labs_find (dialog->labs, vl.value);
252 static void repopulate_dialog (struct val_labs_dialog *dialog);
254 /* Callback which occurs when the "Change" button is clicked */
256 on_change (GtkWidget *w, gpointer data)
258 struct val_labs_dialog *dialog = data;
260 const gchar *val_text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
264 text_to_value (val_text, &v,
265 *var_get_write_format (dialog->pv));
267 val_labs_replace (dialog->labs, v,
268 gtk_entry_get_text (GTK_ENTRY (dialog->label_entry)));
270 gtk_widget_set_sensitive (dialog->change_button, FALSE);
272 repopulate_dialog (dialog);
277 /* Callback which occurs when the "Add" button is clicked */
279 on_add (GtkWidget *w, gpointer data)
281 struct val_labs_dialog *dialog = data;
285 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
287 text_to_value (text, &v,
288 *var_get_write_format (dialog->pv));
291 if ( ! val_labs_add (dialog->labs, v,
293 ( GTK_ENTRY (dialog->label_entry)) ) )
296 gtk_widget_set_sensitive (dialog->add_button, FALSE);
298 repopulate_dialog (dialog);
303 /* Callback which occurs when the "Remove" button is clicked */
305 on_remove (GtkWidget *w, gpointer data)
307 struct val_labs_dialog *dialog = data;
309 struct val_lab *vl = get_selected_tuple (dialog);
311 val_labs_remove (dialog->labs, vl->value);
313 repopulate_dialog (dialog);
315 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
322 /* Callback which occurs when a line item is selected in the list of
323 value--label pairs.*/
325 on_select_row (GtkTreeView *treeview,
329 struct val_labs_dialog *dialog = data;
331 struct val_lab * vl = get_selected_tuple (dialog);
333 gchar *const text = value_to_text (vl->value,
334 *var_get_write_format (dialog->pv));
336 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
337 dialog->value_handler_id);
339 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), text);
341 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
342 dialog->value_handler_id);
345 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
346 dialog->change_handler_id);
348 labeltext = pspp_locale_to_utf8 (vl->label, -1, 0);
349 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
353 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
354 dialog->change_handler_id);
356 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
357 gtk_widget_set_sensitive (dialog->change_button, FALSE);
361 /* Create a new dialog box
362 (there should normally be only one)*/
363 struct val_labs_dialog *
364 val_labs_dialog_create (GladeXML *xml)
366 GtkTreeViewColumn *column;
368 GtkCellRenderer *renderer ;
370 struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
374 dialog->window = get_widget_assert (xml,"val_labs_dialog");
375 dialog->value_entry = get_widget_assert (xml,"value_entry");
376 dialog->label_entry = get_widget_assert (xml,"label_entry");
378 gtk_window_set_transient_for
379 (GTK_WINDOW (dialog->window),
380 GTK_WINDOW (get_widget_assert (xml, "data_editor")));
382 dialog->add_button = get_widget_assert (xml, "val_labs_add");
383 dialog->remove_button = get_widget_assert (xml, "val_labs_remove");
384 dialog->change_button = get_widget_assert (xml, "val_labs_change");
386 dialog->treeview = get_widget_assert (xml,"treeview1");
388 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->treeview), FALSE);
390 renderer = gtk_cell_renderer_text_new ();
392 column = gtk_tree_view_column_new_with_attributes ("Title",
398 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
400 g_signal_connect (get_widget_assert (xml, "val_labs_cancel"),
402 GTK_SIGNAL_FUNC (on_cancel), dialog);
404 g_signal_connect (dialog->window, "delete-event",
405 GTK_SIGNAL_FUNC (on_delete), dialog);
407 g_signal_connect (get_widget_assert (xml, "val_labs_ok"),
409 GTK_SIGNAL_FUNC (val_labs_ok), dialog);
411 dialog->change_handler_id =
412 g_signal_connect (dialog->label_entry,
414 GTK_SIGNAL_FUNC (on_label_entry_change), dialog);
416 dialog->value_handler_id =
417 g_signal_connect (dialog->value_entry,
419 GTK_SIGNAL_FUNC (on_value_entry_change), dialog);
421 g_signal_connect (dialog->change_button,
423 GTK_SIGNAL_FUNC (on_change), dialog);
426 g_signal_connect (dialog->treeview, "cursor-changed",
427 GTK_SIGNAL_FUNC (on_select_row), dialog);
429 g_signal_connect (dialog->remove_button, "clicked",
430 GTK_SIGNAL_FUNC (on_remove), dialog);
432 g_signal_connect (dialog->add_button, "clicked",
433 GTK_SIGNAL_FUNC (on_add), dialog);
442 val_labs_dialog_set_target_variable (struct val_labs_dialog *dialog,
443 struct variable *var)
450 /* Populate the components of the dialog box, from the 'labs' member
453 repopulate_dialog (struct val_labs_dialog *dialog)
455 struct val_labs_iterator *vli = 0;
460 GtkListStore *list_store = gtk_list_store_new (2,
464 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
465 dialog->change_handler_id);
466 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
467 dialog->value_handler_id);
469 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
470 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
472 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
473 dialog->value_handler_id);
474 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
475 dialog->change_handler_id);
478 for (vl = val_labs_first_sorted (dialog->labs, &vli);
480 vl = val_labs_next (dialog->labs, &vli))
484 value_to_text (vl->value,
485 *var_get_write_format (dialog->pv));
488 pspp_locale_to_utf8 (vl->label, -1, 0);
490 gchar *const text = g_strdup_printf ("%s = \"%s\"",
494 gtk_list_store_append (list_store, &iter);
495 gtk_list_store_set (list_store, &iter,
505 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
506 GTK_TREE_MODEL (list_store));
508 g_object_unref (list_store);
512 /* Initialise and display the dialog box */
514 val_labs_dialog_show (struct val_labs_dialog *dialog)
516 const struct val_labs *value_labels;
518 g_assert (!dialog->labs);
520 value_labels = var_get_value_labels (dialog->pv);
523 dialog->labs = val_labs_clone ( value_labels );
525 dialog->labs = val_labs_create ( var_get_width (dialog->pv));
527 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
528 gtk_widget_set_sensitive (dialog->change_button, FALSE);
529 gtk_widget_set_sensitive (dialog->add_button, FALSE);
531 repopulate_dialog (dialog);
532 gtk_widget_show (dialog->window);