/* PSPPIRE --- A Graphical User Interface for PSPP Copyright (C) 2005 Free Software Foundation Written by John Darrington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* This module describes the behaviour of the Value Labels dialog box, used for input of the value labels in the variable sheet */ /* FIXME: Much of this code assumes that values are Numeric values. Behaviour needs to be tested (and fixed) for Currency, Shortstring and Date values */ #include "val_labs_dialog.h" #include "value-labels.h" /* This callback occurs when the text in the label entry box is changed */ static void on_label_entry_change(GtkEntry *entry, gpointer data) { struct val_labs_dialog *dialog = data; union value v; const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry)); v.f = strtod(text, 0); if ( val_labs_find (dialog->labs, v) ) gtk_widget_set_sensitive(dialog->change_button, TRUE); else gtk_widget_set_sensitive(dialog->change_button, FALSE); } /* This callback occurs when the text in the value entry box is changed */ static void on_value_entry_change(GtkEntry *entry, gpointer data) { struct val_labs_dialog *dialog = data; const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry)); union value v; v.f = strtod(text, 0); g_signal_handler_block(GTK_ENTRY(dialog->label_entry), dialog->change_handler_id); gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),""); char *s; if ( s = val_labs_find (dialog->labs, v) ) { gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), s); gtk_widget_set_sensitive(dialog->add_button, FALSE); gtk_widget_set_sensitive(dialog->remove_button, TRUE); } else { gtk_widget_set_sensitive(dialog->remove_button, FALSE); gtk_widget_set_sensitive(dialog->add_button, TRUE); } g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry), dialog->change_handler_id); } /* Callback for when the Value Labels dialog is closed using the OK button.*/ static gint val_labs_ok(GtkWidget *w, gpointer data) { struct val_labs_dialog *dialog = data; val_labs_destroy(*dialog->target); *dialog->target = val_labs_copy(dialog->labs); val_labs_destroy (dialog->labs); dialog->labs = 0; return FALSE; } /* Callback for when the Value Labels dialog is closed using the Cancel button.*/ static gint val_labs_cancel(GtkWidget *w, gpointer data) { struct val_labs_dialog *dialog = data; val_labs_destroy (dialog->labs); dialog->labs = 0; return FALSE; } /* Return the value-label pair currently selected in the dialog box */ static struct val_lab * get_selected_tuple(struct val_labs_dialog *dialog) { GtkTreeView *treeview = GTK_TREE_VIEW(dialog->treeview); static struct val_lab vl; GtkTreeIter iter ; GValue the_value = {0}; GtkTreeSelection* sel = gtk_tree_view_get_selection(treeview); GtkTreeModel * model = gtk_tree_view_get_model(treeview); gtk_tree_selection_get_selected (sel, &model, &iter); gtk_tree_model_get_value(model, &iter, 1, &the_value); vl.value.f = g_value_get_double(&the_value); g_value_unset(&the_value); vl.label = val_labs_find (dialog->labs, vl.value); return &vl; } static void repopulate_dialog(struct val_labs_dialog *dialog); /* Callback which occurs when the "Change" button is clicked */ static gint on_change(GtkWidget *w, gpointer data) { struct val_labs_dialog *dialog = data; struct val_lab *vl = get_selected_tuple(dialog); val_labs_replace (dialog->labs, vl->value, gtk_entry_get_text(GTK_ENTRY(dialog->label_entry))); gtk_widget_set_sensitive(dialog->change_button, FALSE); repopulate_dialog(dialog); return FALSE; } /* Callback which occurs when the "Add" button is clicked */ static gint on_add(GtkWidget *w, gpointer data) { struct val_labs_dialog *dialog = data; union value v; v.f = strtod(gtk_entry_get_text(GTK_ENTRY(dialog->value_entry)),0); if ( ! val_labs_add (dialog->labs, v, gtk_entry_get_text(GTK_ENTRY(dialog->label_entry)) ) ) return FALSE; gtk_widget_set_sensitive(dialog->add_button, FALSE); repopulate_dialog(dialog); return FALSE; } /* Callback which occurs when the "Remove" button is clicked */ static gint on_remove(GtkWidget *w, gpointer data) { struct val_labs_dialog *dialog = data; struct val_lab *vl = get_selected_tuple(dialog); val_labs_remove (dialog->labs, vl->value); repopulate_dialog(dialog); gtk_widget_set_sensitive(dialog->remove_button, FALSE); return FALSE; } /* Callback which occurs when a line item is selected in the list of value--label pairs.*/ static void on_select_row (GtkTreeView *treeview, gpointer data) { struct val_labs_dialog *dialog = data; struct val_lab * vl = get_selected_tuple(dialog); /* FIXME: properly deal with a) short string variables; b) format width */ gchar * text = g_strdup_printf("%g", vl->value.f); g_signal_handler_block(GTK_ENTRY(dialog->value_entry), dialog->value_handler_id); gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), text); g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry), dialog->value_handler_id); g_free(text); g_signal_handler_block(GTK_ENTRY(dialog->label_entry), dialog->change_handler_id); gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), vl->label); g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry), dialog->change_handler_id); gtk_widget_set_sensitive(dialog->remove_button, TRUE); gtk_widget_set_sensitive(dialog->change_button, FALSE); } /* Create a new dialog box (there should normally be only one)*/ struct val_labs_dialog * val_labs_dialog_create(GladeXML *xml) { struct val_labs_dialog *dialog = g_malloc(sizeof(*dialog)); dialog->window = glade_xml_get_widget(xml,"val_labs_dialog"); dialog->value_entry = glade_xml_get_widget(xml,"value_entry"); dialog->label_entry = glade_xml_get_widget(xml,"label_entry"); gtk_window_set_transient_for (GTK_WINDOW(dialog->window), GTK_WINDOW(glade_xml_get_widget(xml, "window1"))); dialog->ok = glade_xml_get_widget(xml, "val_labs_ok"); dialog->add_button = glade_xml_get_widget(xml, "val_labs_add"); dialog->remove_button = glade_xml_get_widget(xml, "val_labs_remove"); dialog->change_button = glade_xml_get_widget(xml, "val_labs_change"); dialog->treeview = glade_xml_get_widget(xml,"treeview1"); gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dialog->treeview), FALSE); GtkTreeViewColumn *column; GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); column = gtk_tree_view_column_new_with_attributes ("Title", renderer, "text", 0, NULL); gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column); g_signal_connect(GTK_OBJECT(glade_xml_get_widget(xml, "val_labs_cancel")), "clicked", GTK_SIGNAL_FUNC(val_labs_cancel), dialog); dialog->change_handler_id = g_signal_connect(GTK_OBJECT(dialog->label_entry), "changed", GTK_SIGNAL_FUNC(on_label_entry_change), dialog); dialog->value_handler_id = g_signal_connect(GTK_OBJECT(dialog->value_entry), "changed", GTK_SIGNAL_FUNC(on_value_entry_change), dialog); g_signal_connect(GTK_OBJECT(dialog->change_button), "clicked", GTK_SIGNAL_FUNC(on_change), dialog); g_signal_connect(GTK_OBJECT(glade_xml_get_widget(xml, "val_labs_ok")), "clicked", GTK_SIGNAL_FUNC(val_labs_ok), dialog); g_signal_connect(GTK_OBJECT(dialog->treeview), "cursor-changed", GTK_SIGNAL_FUNC(on_select_row), dialog); g_signal_connect(GTK_OBJECT(dialog->remove_button), "clicked", GTK_SIGNAL_FUNC(on_remove), dialog); g_signal_connect(GTK_OBJECT(dialog->add_button), "clicked", GTK_SIGNAL_FUNC(on_add), dialog); dialog->labs = 0; return dialog; } /* Populate the components of the dialog box, from the 'labs' member variable */ static void repopulate_dialog(struct val_labs_dialog *dialog) { struct val_labs_iterator *vli = 0; struct val_lab *vl; GtkTreePath *path; GtkTreeIter iter; GtkListStore *list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_DOUBLE); g_signal_handler_block(GTK_ENTRY(dialog->label_entry), dialog->change_handler_id); g_signal_handler_block(GTK_ENTRY(dialog->value_entry), dialog->value_handler_id); gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), ""); gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), ""); g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry), dialog->value_handler_id); g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry), dialog->change_handler_id); for(vl = val_labs_first_sorted (dialog->labs, &vli); vl; vl = val_labs_next(dialog->labs, &vli)) { gchar *text = g_strdup_printf("%g = \"%s\"",vl->value.f, vl->label); gtk_list_store_append (list_store, &iter); gtk_list_store_set (list_store, &iter, 0, text, 1, vl->value.f, -1); g_free(text); } gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->treeview), GTK_TREE_MODEL(list_store)); g_object_unref(list_store); } /* Initialise and display the dialog box */ void val_labs_dialog_show(struct val_labs_dialog *dialog) { g_assert(!dialog->labs); dialog->labs = val_labs_copy(*dialog->target); gtk_widget_set_sensitive(dialog->remove_button, FALSE); gtk_widget_set_sensitive(dialog->change_button, FALSE); gtk_widget_set_sensitive(dialog->add_button, FALSE); repopulate_dialog(dialog); gtk_widget_show(dialog->window); }