2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2005 Free Software Foundation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 /* This module describes the behaviour of the Value Labels dialog box,
22 used for input of the value labels in the variable sheet */
29 #include "val-labs-dialog.h"
30 #include <data/value-labels.h>
33 struct val_labs_dialog
37 /* The variable to be updated */
40 /* Local copy of labels */
41 struct val_labs *labs;
44 GtkWidget *add_button;
45 GtkWidget *remove_button;
46 GtkWidget *change_button;
49 GtkWidget *value_entry;
50 GtkWidget *label_entry;
52 /* Signal handler ids */
53 gint change_handler_id;
54 gint value_handler_id;
60 /* This callback occurs when the text in the label entry box
63 on_label_entry_change (GtkEntry *entry, gpointer data)
67 struct val_labs_dialog *dialog = data;
68 g_assert (dialog->labs);
70 text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
72 text_to_value (text, &v,
73 *var_get_write_format (dialog->pv));
76 if ( val_labs_find (dialog->labs, v) )
78 gtk_widget_set_sensitive (dialog->change_button, TRUE);
79 gtk_widget_set_sensitive (dialog->add_button, FALSE);
83 gtk_widget_set_sensitive (dialog->change_button, FALSE);
84 gtk_widget_set_sensitive (dialog->add_button, TRUE);
89 /* Set the TREEVIEW list cursor to the item which has the value VAL */
91 select_treeview_from_value (GtkTreeView *treeview, union value *val)
96 We do this with a linear search through the model --- hardly
97 efficient, but the list is short ... */
100 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
103 for (success = gtk_tree_model_get_iter_first (model, &iter);
105 success = gtk_tree_model_iter_next (model, &iter))
110 gtk_tree_model_get_value (model, &iter, 1, &gvalue);
112 v.f = g_value_get_double (&gvalue);
114 if ( 0 == memcmp (&v, val, sizeof (union value)))
120 path = gtk_tree_model_get_path (model, &iter);
123 gtk_tree_view_set_cursor (treeview, path, 0, 0);
124 gtk_tree_path_free (path);
130 /* This callback occurs when the text in the value entry box is
133 on_value_entry_change (GtkEntry *entry, gpointer data)
137 struct val_labs_dialog *dialog = data;
139 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
142 text_to_value (text, &v,
143 *var_get_write_format (dialog->pv));
146 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
147 dialog->change_handler_id);
149 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),"");
152 if ( (s = val_labs_find (dialog->labs, v)) )
154 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), s);
155 gtk_widget_set_sensitive (dialog->add_button, FALSE);
156 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
157 select_treeview_from_value (GTK_TREE_VIEW (dialog->treeview), &v);
161 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
162 gtk_widget_set_sensitive (dialog->add_button, TRUE);
165 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
166 dialog->change_handler_id);
170 /* Callback for when the Value Labels dialog is closed using
173 val_labs_ok (GtkWidget *w, gpointer data)
175 struct val_labs_dialog *dialog = data;
177 var_set_value_labels (dialog->pv, dialog->labs);
179 val_labs_destroy (dialog->labs);
183 gtk_widget_hide (dialog->window);
188 /* Callback for when the Value Labels dialog is closed using
191 val_labs_cancel (struct val_labs_dialog *dialog)
193 val_labs_destroy (dialog->labs);
197 gtk_widget_hide (dialog->window);
201 /* Callback for when the Value Labels dialog is closed using
204 on_cancel (GtkWidget *w, gpointer data)
206 struct val_labs_dialog *dialog = data;
208 val_labs_cancel (dialog);
214 /* Callback for when the Value Labels dialog is closed using
215 the window delete button.*/
217 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
219 struct val_labs_dialog *dialog = data;
221 val_labs_cancel (dialog);
227 /* Return the value-label pair currently selected in the dialog box */
228 static struct val_lab *
229 get_selected_tuple (struct val_labs_dialog *dialog)
231 GtkTreeView *treeview = GTK_TREE_VIEW (dialog->treeview);
232 static struct val_lab vl;
235 GValue the_value = {0};
237 GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview);
239 GtkTreeModel * model = gtk_tree_view_get_model (treeview);
241 gtk_tree_selection_get_selected (sel, &model, &iter);
243 gtk_tree_model_get_value (model, &iter, 1, &the_value);
245 vl.value.f = g_value_get_double (&the_value);
246 g_value_unset (&the_value);
248 vl.label = val_labs_find (dialog->labs, vl.value);
254 static void repopulate_dialog (struct val_labs_dialog *dialog);
256 /* Callback which occurs when the "Change" button is clicked */
258 on_change (GtkWidget *w, gpointer data)
260 struct val_labs_dialog *dialog = data;
262 const gchar *val_text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
266 text_to_value (val_text, &v,
267 *var_get_write_format (dialog->pv));
269 val_labs_replace (dialog->labs, v,
270 gtk_entry_get_text (GTK_ENTRY (dialog->label_entry)));
272 gtk_widget_set_sensitive (dialog->change_button, FALSE);
274 repopulate_dialog (dialog);
279 /* Callback which occurs when the "Add" button is clicked */
281 on_add (GtkWidget *w, gpointer data)
283 struct val_labs_dialog *dialog = data;
287 const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
289 text_to_value (text, &v,
290 *var_get_write_format (dialog->pv));
293 if ( ! val_labs_add (dialog->labs, v,
295 ( GTK_ENTRY (dialog->label_entry)) ) )
298 gtk_widget_set_sensitive (dialog->add_button, FALSE);
300 repopulate_dialog (dialog);
305 /* Callback which occurs when the "Remove" button is clicked */
307 on_remove (GtkWidget *w, gpointer data)
309 struct val_labs_dialog *dialog = data;
311 struct val_lab *vl = get_selected_tuple (dialog);
313 val_labs_remove (dialog->labs, vl->value);
315 repopulate_dialog (dialog);
317 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
324 /* Callback which occurs when a line item is selected in the list of
325 value--label pairs.*/
327 on_select_row (GtkTreeView *treeview,
331 struct val_labs_dialog *dialog = data;
333 struct val_lab * vl = get_selected_tuple (dialog);
335 gchar *const text = value_to_text (vl->value,
336 *var_get_write_format (dialog->pv));
338 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
339 dialog->value_handler_id);
341 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), text);
343 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
344 dialog->value_handler_id);
347 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
348 dialog->change_handler_id);
350 labeltext = pspp_locale_to_utf8 (vl->label, -1, 0);
351 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
355 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
356 dialog->change_handler_id);
358 gtk_widget_set_sensitive (dialog->remove_button, TRUE);
359 gtk_widget_set_sensitive (dialog->change_button, FALSE);
363 /* Create a new dialog box
364 (there should normally be only one)*/
365 struct val_labs_dialog *
366 val_labs_dialog_create (GladeXML *xml)
368 GtkTreeViewColumn *column;
370 GtkCellRenderer *renderer ;
372 struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
376 dialog->window = get_widget_assert (xml,"val_labs_dialog");
377 dialog->value_entry = get_widget_assert (xml,"value_entry");
378 dialog->label_entry = get_widget_assert (xml,"label_entry");
380 gtk_window_set_transient_for
381 (GTK_WINDOW (dialog->window),
382 GTK_WINDOW (get_widget_assert (xml, "data_editor")));
384 dialog->add_button = get_widget_assert (xml, "val_labs_add");
385 dialog->remove_button = get_widget_assert (xml, "val_labs_remove");
386 dialog->change_button = get_widget_assert (xml, "val_labs_change");
388 dialog->treeview = get_widget_assert (xml,"treeview1");
390 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->treeview), FALSE);
392 renderer = gtk_cell_renderer_text_new ();
394 column = gtk_tree_view_column_new_with_attributes ("Title",
400 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
402 g_signal_connect (GTK_OBJECT (get_widget_assert (xml, "val_labs_cancel")),
404 GTK_SIGNAL_FUNC (on_cancel), dialog);
406 g_signal_connect (GTK_OBJECT (dialog->window), "delete-event",
407 GTK_SIGNAL_FUNC (on_delete), dialog);
409 g_signal_connect (GTK_OBJECT (get_widget_assert (xml, "val_labs_ok")),
411 GTK_SIGNAL_FUNC (val_labs_ok), dialog);
413 dialog->change_handler_id =
414 g_signal_connect (GTK_OBJECT (dialog->label_entry),
416 GTK_SIGNAL_FUNC (on_label_entry_change), dialog);
418 dialog->value_handler_id =
419 g_signal_connect (GTK_OBJECT (dialog->value_entry),
421 GTK_SIGNAL_FUNC (on_value_entry_change), dialog);
423 g_signal_connect (GTK_OBJECT (dialog->change_button),
425 GTK_SIGNAL_FUNC (on_change), dialog);
428 g_signal_connect (GTK_OBJECT (dialog->treeview), "cursor-changed",
429 GTK_SIGNAL_FUNC (on_select_row), dialog);
431 g_signal_connect (GTK_OBJECT (dialog->remove_button), "clicked",
432 GTK_SIGNAL_FUNC (on_remove), dialog);
434 g_signal_connect (GTK_OBJECT (dialog->add_button), "clicked",
435 GTK_SIGNAL_FUNC (on_add), dialog);
444 val_labs_dialog_set_target_variable (struct val_labs_dialog *dialog,
445 struct variable *var)
452 /* Populate the components of the dialog box, from the 'labs' member
455 repopulate_dialog (struct val_labs_dialog *dialog)
457 struct val_labs_iterator *vli = 0;
462 GtkListStore *list_store = gtk_list_store_new (2,
466 g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
467 dialog->change_handler_id);
468 g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
469 dialog->value_handler_id);
471 gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
472 gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
474 g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
475 dialog->value_handler_id);
476 g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
477 dialog->change_handler_id);
480 for (vl = val_labs_first_sorted (dialog->labs, &vli);
482 vl = val_labs_next (dialog->labs, &vli))
486 value_to_text (vl->value,
487 *var_get_write_format (dialog->pv));
490 pspp_locale_to_utf8 (vl->label, -1, 0);
492 gchar *const text = g_strdup_printf ("%s = \"%s\"",
496 gtk_list_store_append (list_store, &iter);
497 gtk_list_store_set (list_store, &iter,
507 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
508 GTK_TREE_MODEL (list_store));
510 g_object_unref (list_store);
514 /* Initialise and display the dialog box */
516 val_labs_dialog_show (struct val_labs_dialog *dialog)
518 const struct val_labs *value_labels;
520 g_assert (!dialog->labs);
522 value_labels = var_get_value_labels (dialog->pv);
525 dialog->labs = val_labs_copy ( value_labels );
527 dialog->labs = val_labs_create ( var_get_width (dialog->pv));
529 gtk_widget_set_sensitive (dialog->remove_button, FALSE);
530 gtk_widget_set_sensitive (dialog->change_button, FALSE);
531 gtk_widget_set_sensitive (dialog->add_button, FALSE);
533 repopulate_dialog (dialog);
534 gtk_widget_show (dialog->window);