From 54bf8ec16d5a83679233eaf2f7f9e188db6fc41b Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sun, 22 Apr 2012 21:45:22 +0200 Subject: [PATCH] Fixed some issues with the PsppireValueEntry and use it in the ROC dialog. --- src/ui/gui/psppire-dialog-action-roc.c | 81 +++++++++++++++++++++++--------- src/ui/gui/psppire-value-entry.c | 52 ++++++++++++++------ src/ui/gui/psppire-value-entry.h | 8 ++- src/ui/gui/roc.ui | 4 +- src/ui/gui/widgets.c | 2 + 5 files changed, 104 insertions(+), 43 deletions(-) diff --git a/src/ui/gui/psppire-dialog-action-roc.c b/src/ui/gui/psppire-dialog-action-roc.c index fc0bb7b..babd4b3 100644 --- a/src/ui/gui/psppire-dialog-action-roc.c +++ b/src/ui/gui/psppire-dialog-action-roc.c @@ -18,6 +18,7 @@ #include #include "psppire-dialog-action-roc.h" +#include "psppire-value-entry.h" #include "dialog-common.h" #include @@ -37,8 +38,12 @@ G_DEFINE_TYPE (PsppireDialogActionRoc, psppire_dialog_action_roc, PSPPIRE_TYPE_D static gboolean dialog_state_valid (gpointer data) { + int width; + gboolean result ; + union value val; PsppireDialogActionRoc *rd = data; - const gchar *text; + const gchar *var_name; + const struct variable *var; GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->test_variables)); @@ -46,18 +51,24 @@ dialog_state_valid (gpointer data) if (gtk_tree_model_iter_n_children (liststore, NULL) < 1) return FALSE; - - text = gtk_entry_get_text (GTK_ENTRY (rd->state_variable)); - if ( 0 == strcmp ("", text)) - return FALSE; + var_name = gtk_entry_get_text (GTK_ENTRY (rd->state_variable)); + var = psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION (rd)->dict, var_name); - text = gtk_entry_get_text (GTK_ENTRY (rd->state_value)); - if ( 0 == strcmp ("", text)) + if ( var == NULL) return FALSE; + width = var_get_width (var); + value_init (&val, width); + + result = psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (rd->state_value), &val, width); + + if (var_is_value_missing (var, &val, MV_SYSTEM)) + result = FALSE; + + value_destroy (&val, width); - return TRUE; + return result; } static void @@ -85,7 +96,7 @@ refresh (PsppireDialogAction *rd_) gtk_list_store_clear (GTK_LIST_STORE (liststore)); gtk_entry_set_text (GTK_ENTRY (rd->state_variable), ""); - gtk_entry_set_text (GTK_ENTRY (rd->state_value), ""); + psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (rd->state_value), NULL); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->curve), TRUE); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->reference), FALSE); @@ -94,6 +105,23 @@ refresh (PsppireDialogAction *rd_) } static void +on_state_var_changed (GtkAction *a) +{ + PsppireDialogActionRoc *act = PSPPIRE_DIALOG_ACTION_ROC (a); + PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a); + + const gchar *var_name = gtk_entry_get_text (GTK_ENTRY(act->state_variable)); + + const struct variable *var = + psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION(act)->dict, var_name); + + if ( var == NULL) + return; + + psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->state_value), var); +} + +static void psppire_dialog_action_roc_activate (GtkAction *a) { PsppireDialogActionRoc *act = PSPPIRE_DIALOG_ACTION_ROC (a); @@ -102,7 +130,6 @@ psppire_dialog_action_roc_activate (GtkAction *a) GtkBuilder *xml = builder_new ("roc.ui"); pda->dialog = get_widget_assert (xml, "roc-dialog"); pda->source = get_widget_assert (xml, "dict-view"); - pda->source = get_widget_assert (xml, "dict-view"); act->test_variables = get_widget_assert (xml, "psppire-var-view1"); act->state_variable = get_widget_assert (xml, "entry1"); @@ -113,6 +140,9 @@ psppire_dialog_action_roc_activate (GtkAction *a) act->standard_error = get_widget_assert (xml, "standard-error"); act->coordinates = get_widget_assert (xml, "co-ordinates"); + g_signal_connect_swapped (act->state_variable, "changed", + G_CALLBACK (on_state_var_changed), act); + g_object_unref (xml); g_signal_connect (act->curve, "toggled", @@ -145,23 +175,30 @@ generate_syntax (PsppireDialogAction *a) g_string_append (string, " ("); { - const gchar *value = gtk_entry_get_text (GTK_ENTRY (rd->state_value)); + const struct variable *var = + psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION(rd)->dict, var_name); + + union value val; + value_init (&val, var_get_width (var)); - const struct variable *var = psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION(rd)->dict, var_name); + psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (rd->state_value), + &val, var_get_width (var)); g_return_val_if_fail (var, NULL); - if ( var_is_alpha (var)) - { - struct string str; - ds_init_empty (&str); - syntax_gen_string (&str, ss_cstr (value)); - g_string_append (string, ds_cstr (&str)); - ds_destroy (&str); - } - else - g_string_append (string, value); + { + struct string str; + ds_init_empty (&str); + + syntax_gen_value (&str, &val, var_get_width (var), + var_get_print_format (var)); + + g_string_append (string, ds_cstr (&str)); + ds_destroy (&str); + } + value_destroy (&val, var_get_width (var)); } + g_string_append (string, ")"); diff --git a/src/ui/gui/psppire-value-entry.c b/src/ui/gui/psppire-value-entry.c index 675df83..41a1dfa 100644 --- a/src/ui/gui/psppire-value-entry.c +++ b/src/ui/gui/psppire-value-entry.c @@ -241,10 +241,10 @@ psppire_value_entry_new (void) static void psppire_value_entry_refresh_model (PsppireValueEntry *obj) { - GtkWidget *entry = gtk_bin_get_child (GTK_BIN (obj)); + GtkTreeModel *old_model; GtkTreeModel *model; - if (val_labs_count (obj->val_labs) > 0) + if (obj->val_labs && val_labs_count (obj->val_labs) > 0) { const struct val_lab **vls = val_labs_sorted (obj->val_labs); size_t n_vls = val_labs_count (obj->val_labs); @@ -269,10 +269,22 @@ psppire_value_entry_refresh_model (PsppireValueEntry *obj) } else model = NULL; + + old_model = gtk_combo_box_get_model (GTK_COMBO_BOX (obj)); + + if (old_model != model) + { + GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj))); + gtk_entry_set_text (entry, ""); + } + + if (old_model) + g_object_unref (old_model); gtk_combo_box_set_model (GTK_COMBO_BOX (obj), model); gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (obj), COL_LABEL); - gtk_widget_set_sensitive (entry, model != NULL); + gtk_combo_box_set_button_sensitivity (GTK_COMBO_BOX (obj), model != NULL + ? GTK_SENSITIVITY_ON : GTK_SENSITIVITY_OFF); } void @@ -310,24 +322,32 @@ void psppire_value_entry_set_value_labels (PsppireValueEntry *obj, const struct val_labs *val_labs) { - if (val_labs != NULL - ? obj->val_labs == NULL || !val_labs_equal (obj->val_labs, val_labs) - : obj->val_labs != NULL) + bool changed = (obj->val_labs != val_labs); + + if (val_labs == NULL || + (obj->val_labs != NULL && !val_labs_equal (obj->val_labs, val_labs))) { - obj->cur_value = NULL; + obj->cur_value = NULL; + if (obj->val_labs) + { + val_labs_destroy (obj->val_labs); + obj->val_labs = NULL; + } + changed = true; + } - val_labs_destroy (obj->val_labs); + if (val_labs != NULL) + { + int width = val_labs_get_width (val_labs); obj->val_labs = val_labs_clone (val_labs); - if (val_labs != NULL) - { - int width = val_labs_get_width (val_labs); - if (width != fmt_var_width (&obj->format)) - obj->format = fmt_default_for_width (width); - } - + if (width != fmt_var_width (&obj->format)) + obj->format = fmt_default_for_width (width); + } + + if ( changed ) + { psppire_value_entry_refresh_model (obj); - g_object_notify (G_OBJECT (obj), "value-labels"); } } diff --git a/src/ui/gui/psppire-value-entry.h b/src/ui/gui/psppire-value-entry.h index 42821f3..d17d5ad 100644 --- a/src/ui/gui/psppire-value-entry.h +++ b/src/ui/gui/psppire-value-entry.h @@ -50,7 +50,8 @@ struct variable; typedef struct _PsppireValueEntry PsppireValueEntry; typedef struct _PsppireValueEntryClass PsppireValueEntryClass; -struct _PsppireValueEntry { +struct _PsppireValueEntry +{ GtkComboBoxEntry parent; gboolean show_value_label; @@ -62,11 +63,12 @@ struct _PsppireValueEntry { const union value *cur_value; }; -struct _PsppireValueEntryClass { +struct _PsppireValueEntryClass +{ GtkComboBoxEntryClass parent_class; }; -GType psppire_value_entry_get_type (void) G_GNUC_CONST; +GType psppire_value_entry_get_type (void); GtkWidget *psppire_value_entry_new (void); void psppire_value_entry_set_show_value_label (PsppireValueEntry *, diff --git a/src/ui/gui/roc.ui b/src/ui/gui/roc.ui index 87f0c98..6b195b2 100644 --- a/src/ui/gui/roc.ui +++ b/src/ui/gui/roc.ui @@ -1,3 +1,4 @@ + @@ -180,10 +181,9 @@ - + True True - 1 diff --git a/src/ui/gui/widgets.c b/src/ui/gui/widgets.c index ed8328a..8c64f52 100644 --- a/src/ui/gui/widgets.c +++ b/src/ui/gui/widgets.c @@ -23,6 +23,7 @@ #include "psppire-dialog-action-roc.h" #include "psppire-dialog-action-sort.h" #include "psppire-dialog-action-var-info.h" +#include "psppire-value-entry.h" /* Any custom widgets which are to be used in GtkBuilder ui files @@ -40,6 +41,7 @@ preregister_widgets (void) psppire_acr_get_type (); psppire_dict_view_get_type (); psppire_var_view_get_type (); + psppire_value_entry_get_type (); psppire_dialog_action_correlation_get_type (); psppire_dialog_action_descriptives_get_type (); -- 1.7.2.5