2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2005 Free Software Foundation
4 Written by John Darrington
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* This module describes the behaviour of the Value Labels dialog box,
23 used for input of the value labels in the variable sheet */
28 #include "val-labs-dialog.h"
29 #include "value-labels.h"
30 #include "psppire-variable.h"
32 /* This callback occurs when the text in the label entry box
35 on_label_entry_change(GtkEntry *entry, gpointer data)
37 struct val_labs_dialog *dialog = data;
38 g_assert(dialog->labs);
41 const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
43 text_to_value(text, &v,
44 *psppire_variable_get_write_spec(dialog->pv));
47 if ( val_labs_find (dialog->labs, v) )
49 gtk_widget_set_sensitive(dialog->change_button, TRUE);
50 gtk_widget_set_sensitive(dialog->add_button, FALSE);
54 gtk_widget_set_sensitive(dialog->change_button, FALSE);
55 gtk_widget_set_sensitive(dialog->add_button, TRUE);
60 /* Set the TREEVIEW list cursor to the item which has the value VAL */
62 select_treeview_from_value(GtkTreeView *treeview, union value *val)
65 We do this with a linear search through the model --- hardly
66 efficient, but the list is short ... */
69 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
72 for (success = gtk_tree_model_get_iter_first(model, &iter);
74 success = gtk_tree_model_iter_next(model, &iter))
78 gtk_tree_model_get_value(model, &iter, 1, &gvalue);
81 v.f = g_value_get_double(&gvalue);
83 if ( 0 == memcmp(&v, val, sizeof (union value)))
89 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
92 gtk_tree_view_set_cursor(treeview, path, 0, 0);
93 gtk_tree_path_free(path);
99 /* This callback occurs when the text in the value entry box is
102 on_value_entry_change(GtkEntry *entry, gpointer data)
104 struct val_labs_dialog *dialog = data;
106 const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
109 text_to_value(text, &v,
110 *psppire_variable_get_write_spec(dialog->pv));
113 g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
114 dialog->change_handler_id);
116 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),"");
120 if ( (s = val_labs_find (dialog->labs, v)) )
122 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), s);
123 gtk_widget_set_sensitive(dialog->add_button, FALSE);
124 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
125 select_treeview_from_value(GTK_TREE_VIEW(dialog->treeview), &v);
129 gtk_widget_set_sensitive(dialog->remove_button, FALSE);
130 gtk_widget_set_sensitive(dialog->add_button, TRUE);
133 g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
134 dialog->change_handler_id);
138 /* Callback for when the Value Labels dialog is closed using
141 val_labs_ok(GtkWidget *w, gpointer data)
143 struct val_labs_dialog *dialog = data;
145 psppire_variable_set_value_labels(dialog->pv, dialog->labs);
147 val_labs_destroy (dialog->labs);
153 /* Callback for when the Value Labels dialog is closed using
156 val_labs_cancel(GtkWidget *w, gpointer data)
158 struct val_labs_dialog *dialog = data;
160 val_labs_destroy (dialog->labs);
167 /* Return the value-label pair currently selected in the dialog box */
168 static struct val_lab *
169 get_selected_tuple(struct val_labs_dialog *dialog)
171 GtkTreeView *treeview = GTK_TREE_VIEW(dialog->treeview);
172 static struct val_lab vl;
175 GValue the_value = {0};
177 GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview);
179 GtkTreeModel * model = gtk_tree_view_get_model(treeview);
181 gtk_tree_selection_get_selected (sel, &model, &iter);
183 gtk_tree_model_get_value(model, &iter, 1, &the_value);
185 vl.value.f = g_value_get_double(&the_value);
186 g_value_unset(&the_value);
188 vl.label = val_labs_find (dialog->labs, vl.value);
194 static void repopulate_dialog(struct val_labs_dialog *dialog);
196 /* Callback which occurs when the "Change" button is clicked */
198 on_change(GtkWidget *w, gpointer data)
200 struct val_labs_dialog *dialog = data;
202 const gchar *val_text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
206 text_to_value(val_text, &v,
207 *psppire_variable_get_write_spec(dialog->pv));
209 val_labs_replace (dialog->labs, v,
210 gtk_entry_get_text(GTK_ENTRY(dialog->label_entry)));
212 gtk_widget_set_sensitive(dialog->change_button, FALSE);
214 repopulate_dialog(dialog);
219 /* Callback which occurs when the "Add" button is clicked */
221 on_add(GtkWidget *w, gpointer data)
223 struct val_labs_dialog *dialog = data;
227 const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
229 text_to_value(text, &v,
230 *psppire_variable_get_write_spec(dialog->pv));
233 if ( ! val_labs_add (dialog->labs, v,
234 gtk_entry_get_text(GTK_ENTRY(dialog->label_entry)) ) )
238 gtk_widget_set_sensitive(dialog->add_button, FALSE);
240 repopulate_dialog(dialog);
245 /* Callback which occurs when the "Remove" button is clicked */
247 on_remove(GtkWidget *w, gpointer data)
249 struct val_labs_dialog *dialog = data;
251 struct val_lab *vl = get_selected_tuple(dialog);
253 val_labs_remove (dialog->labs, vl->value);
255 repopulate_dialog(dialog);
257 gtk_widget_set_sensitive(dialog->remove_button, FALSE);
264 /* Callback which occurs when a line item is selected in the list of
265 value--label pairs.*/
267 on_select_row (GtkTreeView *treeview,
270 struct val_labs_dialog *dialog = data;
272 struct val_lab * vl = get_selected_tuple(dialog);
274 gchar *const text = value_to_text(vl->value,
275 *psppire_variable_get_write_spec(dialog->pv));
277 g_signal_handler_block(GTK_ENTRY(dialog->value_entry),
278 dialog->value_handler_id);
280 gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), text);
282 g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry),
283 dialog->value_handler_id);
286 g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
287 dialog->change_handler_id);
289 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),
292 g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
293 dialog->change_handler_id);
295 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
296 gtk_widget_set_sensitive(dialog->change_button, FALSE);
300 /* Create a new dialog box
301 (there should normally be only one)*/
302 struct val_labs_dialog *
303 val_labs_dialog_create(GladeXML *xml)
305 struct val_labs_dialog *dialog = g_malloc(sizeof(*dialog));
307 dialog->window = get_widget_assert(xml,"val_labs_dialog");
308 dialog->value_entry = get_widget_assert(xml,"value_entry");
309 dialog->label_entry = get_widget_assert(xml,"label_entry");
311 gtk_window_set_transient_for
312 (GTK_WINDOW(dialog->window),
313 GTK_WINDOW(get_widget_assert(xml, "data_editor")));
315 dialog->ok = get_widget_assert(xml, "val_labs_ok");
316 dialog->add_button = get_widget_assert(xml, "val_labs_add");
317 dialog->remove_button = get_widget_assert(xml, "val_labs_remove");
318 dialog->change_button = get_widget_assert(xml, "val_labs_change");
320 dialog->treeview = get_widget_assert(xml,"treeview1");
322 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dialog->treeview), FALSE);
324 GtkTreeViewColumn *column;
326 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
328 column = gtk_tree_view_column_new_with_attributes ("Title",
334 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
336 g_signal_connect(GTK_OBJECT(get_widget_assert(xml, "val_labs_cancel")),
338 GTK_SIGNAL_FUNC(val_labs_cancel), dialog);
340 dialog->change_handler_id =
341 g_signal_connect(GTK_OBJECT(dialog->label_entry),
343 GTK_SIGNAL_FUNC(on_label_entry_change), dialog);
345 dialog->value_handler_id =
346 g_signal_connect(GTK_OBJECT(dialog->value_entry),
348 GTK_SIGNAL_FUNC(on_value_entry_change), dialog);
350 g_signal_connect(GTK_OBJECT(dialog->change_button),
352 GTK_SIGNAL_FUNC(on_change), dialog);
355 g_signal_connect(GTK_OBJECT(get_widget_assert(xml, "val_labs_ok")),
357 GTK_SIGNAL_FUNC(val_labs_ok), dialog);
360 g_signal_connect(GTK_OBJECT(dialog->treeview), "cursor-changed",
361 GTK_SIGNAL_FUNC(on_select_row), dialog);
364 g_signal_connect(GTK_OBJECT(dialog->remove_button), "clicked",
365 GTK_SIGNAL_FUNC(on_remove), dialog);
368 g_signal_connect(GTK_OBJECT(dialog->add_button), "clicked",
369 GTK_SIGNAL_FUNC(on_add), dialog);
377 /* Populate the components of the dialog box, from the 'labs' member
380 repopulate_dialog(struct val_labs_dialog *dialog)
382 struct val_labs_iterator *vli = 0;
387 GtkListStore *list_store = gtk_list_store_new (2,
391 g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
392 dialog->change_handler_id);
393 g_signal_handler_block(GTK_ENTRY(dialog->value_entry),
394 dialog->value_handler_id);
396 gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), "");
397 gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), "");
399 g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry),
400 dialog->value_handler_id);
401 g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
402 dialog->change_handler_id);
405 for(vl = val_labs_first_sorted (dialog->labs, &vli);
407 vl = val_labs_next(dialog->labs, &vli))
410 value_to_text(vl->value,
411 *psppire_variable_get_write_spec(dialog->pv));
415 gchar *const text = g_strdup_printf("%s = \"%s\"",
418 gtk_list_store_append (list_store, &iter);
419 gtk_list_store_set (list_store, &iter,
428 gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->treeview),
429 GTK_TREE_MODEL(list_store));
431 g_object_unref(list_store);
435 /* Initialise and display the dialog box */
437 val_labs_dialog_show(struct val_labs_dialog *dialog)
439 g_assert(!dialog->labs);
440 dialog->labs = val_labs_copy(
441 psppire_variable_get_value_labels(dialog->pv)
445 gtk_widget_set_sensitive(dialog->remove_button, FALSE);
446 gtk_widget_set_sensitive(dialog->change_button, FALSE);
447 gtk_widget_set_sensitive(dialog->add_button, FALSE);
449 repopulate_dialog(dialog);
450 gtk_widget_show(dialog->window);