From 74427ec2552637c6260c7968045620b42102f685 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sun, 22 Jan 2012 18:19:51 +0100 Subject: [PATCH 3/3] Converted variable-info-dialog into a PsppireDialogAction object. This change also adds a "response" signal to PsppirDialog which allows us to hook onto the result of running a dialog. --- src/ui/gui/automake.mk | 2 + src/ui/gui/data-editor.ui | 3 +- src/ui/gui/psppire-data-window.c | 4 - src/ui/gui/psppire-dialog-action-var-info.c | 229 +++++++++++++++++++++++++++ src/ui/gui/psppire-dialog-action-var-info.h | 78 +++++++++ src/ui/gui/psppire-dialog.c | 15 ++ src/ui/gui/variable-info-dialog.c | 223 -------------------------- src/ui/gui/variable-info-dialog.h | 24 --- src/ui/gui/widgets.c | 2 + 9 files changed, 328 insertions(+), 252 deletions(-) create mode 100644 src/ui/gui/psppire-dialog-action-var-info.c create mode 100644 src/ui/gui/psppire-dialog-action-var-info.h delete mode 100644 src/ui/gui/variable-info-dialog.c delete mode 100644 src/ui/gui/variable-info-dialog.h diff --git a/src/ui/gui/automake.mk b/src/ui/gui/automake.mk index 73dcc06..094b1d1 100644 --- a/src/ui/gui/automake.mk +++ b/src/ui/gui/automake.mk @@ -214,6 +214,8 @@ src_ui_gui_psppire_SOURCES = \ src/ui/gui/psppire-dialog-action.h \ src/ui/gui/psppire-dialog-action-descriptives.c \ src/ui/gui/psppire-dialog-action-descriptives.h \ + src/ui/gui/psppire-dialog-action-var-info.c \ + src/ui/gui/psppire-dialog-action-var-info.h \ src/ui/gui/psppire-dict.c \ src/ui/gui/psppire-dict.h \ src/ui/gui/psppire-dictview.c \ diff --git a/src/ui/gui/data-editor.ui b/src/ui/gui/data-editor.ui index 88a228d..d190a44 100644 --- a/src/ui/gui/data-editor.ui +++ b/src/ui/gui/data-editor.ui @@ -490,7 +490,8 @@ - + + uimanager1 utilities_variables _Variables... Jump to variable diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c index 8ef9307..7d58540 100644 --- a/src/ui/gui/psppire-data-window.c +++ b/src/ui/gui/psppire-data-window.c @@ -68,7 +68,6 @@ #include "ui/gui/text-data-import-dialog.h" #include "ui/gui/transpose-dialog.h" #include "ui/gui/univariate-dialog.h" -#include "ui/gui/variable-info-dialog.h" #include "ui/gui/weight-cases-dialog.h" #include "ui/syntax-gen.h" @@ -1083,9 +1082,6 @@ psppire_data_window_finish_init (PsppireDataWindow *de, connect_action (de, "data_weight-cases", G_CALLBACK (weight_cases_dialog)); - - connect_action (de, "utilities_variables", G_CALLBACK (variable_info_dialog)); - connect_action (de, "oneway-anova", G_CALLBACK (oneway_anova_dialog)); connect_action (de, "indep-t-test", G_CALLBACK (t_test_independent_samples_dialog)); diff --git a/src/ui/gui/psppire-dialog-action-var-info.c b/src/ui/gui/psppire-dialog-action-var-info.c new file mode 100644 index 0000000..ac32fae --- /dev/null +++ b/src/ui/gui/psppire-dialog-action-var-info.c @@ -0,0 +1,229 @@ +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2007, 2009, 2010, 2011, 2012 Free Software Foundation + + + 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 3 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, see . */ + + +#include + +#include "psppire-dialog-action-var-info.h" + +#include +#include +#include +#include + +#include "var-display.h" +#include "helper.h" +#include "psppire-var-view.h" +#include "psppire-dictview.h" + +#include "psppire-dialog.h" +#include "builder-wrapper.h" +#include "psppire-data-window.h" + +static void psppire_dialog_action_var_info_init (PsppireDialogActionVarInfo *act); +static void psppire_dialog_action_var_info_class_init (PsppireDialogActionVarInfoClass *class); + +G_DEFINE_TYPE (PsppireDialogActionVarInfo, psppire_dialog_action_var_info, PSPPIRE_TYPE_DIALOG_ACTION); + +#include +#define _(msgid) gettext (msgid) +#define N_(msgid) msgid + + +static const gchar none[] = N_("None"); + + +static const gchar * +label_to_string (const struct variable *var) +{ + const char *label = var_get_label (var); + + if (NULL == label) return g_strdup (none); + + return label; +} + +static gboolean +treeview_item_selected (gpointer data) +{ + GtkTreeView *tv = GTK_TREE_VIEW (data); + GtkTreeModel *model = gtk_tree_view_get_model (tv); + + gint n_rows = gtk_tree_model_iter_n_children (model, NULL); + + if ( n_rows == 0 ) + return FALSE; + + return TRUE; +} + +static gchar * +generate_syntax (PsppireDialogAction *act) + +{ + const struct variable *var = + psppire_dict_view_get_selected_variable (PSPPIRE_DICT_VIEW (act->source)); + + if ( NULL == var) + return g_strdup (""); + + return g_strdup (var_get_name (var)); +} + + + +static void +populate_text (PsppireDictView *treeview, gpointer data) +{ + gchar *text = NULL; + GString *gstring; + PsppireDict *dict; + + GtkTextBuffer *textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data)); + const struct variable *var = + psppire_dict_view_get_selected_variable (treeview); + + if ( var == NULL) + return; + + g_object_get (treeview, "model", &dict, + NULL); + + gstring = g_string_sized_new (200); + g_string_assign (gstring, var_get_name (var)); + g_string_append (gstring, "\n"); + + + g_string_append_printf (gstring, _("Label: %s\n"), label_to_string (var)); + { + const struct fmt_spec *fmt = var_get_print_format (var); + char buffer[FMT_STRING_LEN_MAX + 1]; + + fmt_to_string (fmt, buffer); + /* No conversion necessary. buffer will always be ascii */ + g_string_append_printf (gstring, _("Type: %s\n"), buffer); + } + + text = missing_values_to_string (dict, var, NULL); + g_string_append_printf (gstring, _("Missing Values: %s\n"), + text); + g_free (text); + + g_string_append_printf (gstring, _("Measurement Level: %s\n"), + measure_to_string (var_get_measure (var))); + + + /* Value Labels */ + if ( var_has_value_labels (var)) + { + const struct val_labs *vls = var_get_value_labels (var); + const struct val_lab **labels; + size_t n_labels; + size_t i; + + g_string_append (gstring, "\n"); + g_string_append (gstring, _("Value Labels:\n")); + + labels = val_labs_sorted (vls); + n_labels = val_labs_count (vls); + for (i = 0; i < n_labels; i++) + { + const struct val_lab *vl = labels[i]; + gchar *const vstr = value_to_text (vl->value, var); + + g_string_append_printf (gstring, _("%s %s\n"), + vstr, val_lab_get_escaped_label (vl)); + + g_free (vstr); + } + free (labels); + } + + gtk_text_buffer_set_text (textbuffer, gstring->str, gstring->len); + + g_string_free (gstring, TRUE); +} + + +static void +jump_to (PsppireDialog *d, gint response, gpointer data) +{ + PsppireDataWindow *dw; + PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (data); + const struct variable *var; + + if (response != PSPPIRE_RESPONSE_GOTO) + return; + + var = psppire_dict_view_get_selected_variable (PSPPIRE_DICT_VIEW (pda->source)); + + if ( NULL == var) + return; + + g_object_get (pda, "top-level", &dw, NULL); + + g_object_set (dw->data_editor, + "current-variable", var_get_dict_index (var), + NULL); +} + +static void +psppire_dialog_action_var_info_activate (GtkAction *a) +{ + PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a); + + GtkBuilder *xml = builder_new ("variable-info.ui"); + GtkWidget *textview = get_widget_assert (xml, "textview1"); + + pda->dialog = get_widget_assert (xml, "variable-info-dialog"); + pda->source = get_widget_assert (xml, "treeview2"); + + g_object_set (pda->source, + "selection-mode", GTK_SELECTION_SINGLE, + NULL); + + g_signal_connect (pda->source, "cursor-changed", G_CALLBACK (populate_text), + textview); + + + g_signal_connect (pda->dialog, "response", G_CALLBACK (jump_to), + pda); + + psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (pda->dialog), + treeview_item_selected, pda->source); + + g_object_unref (xml); + + if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_var_info_parent_class)->activate) + PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_var_info_parent_class)->activate (pda); +} + +static void +psppire_dialog_action_var_info_class_init (PsppireDialogActionVarInfoClass *class) +{ + GtkActionClass *action_class = GTK_ACTION_CLASS (class); + + action_class->activate = psppire_dialog_action_var_info_activate; + PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax; +} + + +static void +psppire_dialog_action_var_info_init (PsppireDialogActionVarInfo *act) +{ +} + diff --git a/src/ui/gui/psppire-dialog-action-var-info.h b/src/ui/gui/psppire-dialog-action-var-info.h new file mode 100644 index 0000000..90c8007 --- /dev/null +++ b/src/ui/gui/psppire-dialog-action-var-info.h @@ -0,0 +1,78 @@ +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2012 Free Software Foundation + + 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 3 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, see . */ + + +#include +#include + +#include "psppire-dialog-action.h" + +#ifndef __PSPPIRE_DIALOG_ACTION_VAR_INFO_H__ +#define __PSPPIRE_DIALOG_ACTION_VAR_INFO_H__ + +G_BEGIN_DECLS + + +#define PSPPIRE_TYPE_DIALOG_ACTION_VAR_INFO (psppire_dialog_action_var_info_get_type ()) + +#define PSPPIRE_DIALOG_ACTION_VAR_INFO(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + PSPPIRE_TYPE_DIALOG_ACTION_VAR_INFO, PsppireDialogActionVarInfo)) + +#define PSPPIRE_DIALOG_ACTION_VAR_INFO_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), \ + PSPPIRE_TYPE_DIALOG_ACTION_VAR_INFO, \ + PsppireDialogActionVarInfoClass)) + + +#define PSPPIRE_IS_DIALOG_ACTION_VAR_INFO(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PSPPIRE_TYPE_DIALOG_ACTION_VAR_INFO)) + +#define PSPPIRE_IS_DIALOG_ACTION_VAR_INFO_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), PSPPIRE_TYPE_DIALOG_ACTION_VAR_INFO)) + + +#define PSPPIRE_DIALOG_ACTION_VAR_INFO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + PSPPIRE_TYPE_DIALOG_ACTION_VAR_INFO, \ + PsppireDialogActionVarInfoClass)) + +typedef struct _PsppireDialogActionVarInfo PsppireDialogActionVarInfo; +typedef struct _PsppireDialogActionVarInfoClass PsppireDialogActionVarInfoClass; + + +struct _PsppireDialogActionVarInfo +{ + PsppireDialogAction parent; + + /*< private >*/ + gboolean dispose_has_run ; + + /* Treeview containing the selected variables */ + GtkWidget *variables; +}; + + +struct _PsppireDialogActionVarInfoClass +{ + PsppireDialogActionClass parent_class; +}; + + +GType psppire_dialog_action_var_info_get_type (void) ; + +G_END_DECLS + +#endif /* __PSPPIRE_DIALOG_ACTION_VAR_INFO_H__ */ diff --git a/src/ui/gui/psppire-dialog.c b/src/ui/gui/psppire-dialog.c index c5ac30b..c365ab4 100644 --- a/src/ui/gui/psppire-dialog.c +++ b/src/ui/gui/psppire-dialog.c @@ -31,6 +31,7 @@ static void psppire_dialog_init (PsppireDialog *); enum {DIALOG_REFRESH, + RESPONSE, VALIDITY_CHANGED, DIALOG_HELP, n_SIGNALS}; @@ -248,6 +249,18 @@ psppire_dialog_class_init (PsppireDialogClass *class) 0); + signals [RESPONSE] = + g_signal_new ("response", + G_TYPE_FROM_CLASS (class), + G_SIGNAL_RUN_FIRST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__INT, + G_TYPE_NONE, + 1, + G_TYPE_INT); + + signals [VALIDITY_CHANGED] = g_signal_new ("validity-changed", G_TYPE_FROM_CLASS (class), @@ -524,6 +537,8 @@ psppire_dialog_run (PsppireDialog *dialog) g_main_loop_unref (dialog->loop); + g_signal_emit (dialog, signals [RESPONSE], 0, dialog->response); + return dialog->response; } diff --git a/src/ui/gui/variable-info-dialog.c b/src/ui/gui/variable-info-dialog.c deleted file mode 100644 index 1cd9b1d..0000000 --- a/src/ui/gui/variable-info-dialog.c +++ /dev/null @@ -1,223 +0,0 @@ -/* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2007, 2009, 2010, 2011, 2012 Free Software Foundation - - 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 3 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, see . */ - -#include -#include - -#include -#include -#include -#include - -#include "variable-info-dialog.h" -#include "var-display.h" - -#include "psppire-data-window.h" -#include "psppire-dialog.h" -#include "psppire-dictview.h" -#include "psppire-var-store.h" -#include "builder-wrapper.h" -#include "helper.h" - - - -#include -#define _(msgid) gettext (msgid) -#define N_(msgid) msgid - - -static const gchar none[] = N_("None"); - - -static const gchar * -label_to_string (const struct variable *var) -{ - const char *label = var_get_label (var); - - if (NULL == label) return g_strdup (none); - - return label; -} - - -static void -populate_text (PsppireDictView *treeview, gpointer data) -{ - gchar *text = NULL; - GString *gstring; - PsppireDict *dict; - - GtkTextBuffer *textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data)); - const struct variable *var = - psppire_dict_view_get_selected_variable (treeview); - - if ( var == NULL) - return; - - g_object_get (treeview, "model", &dict, - NULL); - - gstring = g_string_sized_new (200); - g_string_assign (gstring, var_get_name (var)); - g_string_append (gstring, "\n"); - - - g_string_append_printf (gstring, _("Label: %s\n"), label_to_string (var)); - { - const struct fmt_spec *fmt = var_get_print_format (var); - char buffer[FMT_STRING_LEN_MAX + 1]; - - fmt_to_string (fmt, buffer); - /* No conversion necessary. buffer will always be ascii */ - g_string_append_printf (gstring, _("Type: %s\n"), buffer); - } - - text = missing_values_to_string (dict, var, NULL); - g_string_append_printf (gstring, _("Missing Values: %s\n"), - text); - g_free (text); - - g_string_append_printf (gstring, _("Measurement Level: %s\n"), - measure_to_string (var_get_measure (var))); - - - /* Value Labels */ - if ( var_has_value_labels (var)) - { - const struct val_labs *vls = var_get_value_labels (var); - const struct val_lab **labels; - size_t n_labels; - size_t i; - - g_string_append (gstring, "\n"); - g_string_append (gstring, _("Value Labels:\n")); - - labels = val_labs_sorted (vls); - n_labels = val_labs_count (vls); - for (i = 0; i < n_labels; i++) - { - const struct val_lab *vl = labels[i]; - gchar *const vstr = value_to_text (vl->value, var); - - g_string_append_printf (gstring, _("%s %s\n"), - vstr, val_lab_get_escaped_label (vl)); - - g_free (vstr); - } - free (labels); - } - - gtk_text_buffer_set_text (textbuffer, gstring->str, gstring->len); - - g_string_free (gstring, TRUE); -} - -static gboolean -treeview_item_selected (gpointer data) -{ - GtkTreeView *tv = GTK_TREE_VIEW (data); - GtkTreeModel *model = gtk_tree_view_get_model (tv); - - gint n_rows = gtk_tree_model_iter_n_children (model, NULL); - - if ( n_rows == 0 ) - return FALSE; - - return TRUE; -} - - -static gchar * generate_syntax (PsppireDictView *treeview); - - -void -variable_info_dialog (PsppireDataWindow *de) -{ - gint response ; - - GtkBuilder *xml = builder_new ("variable-info.ui"); - - GtkWidget *dialog = get_widget_assert (xml, "variable-info-dialog"); - GtkWidget *treeview = get_widget_assert (xml, "treeview2"); - GtkWidget *textview = get_widget_assert (xml, "textview1"); - - PsppireVarStore *vs = NULL; - PsppireDict *dict = NULL; - - g_object_get (de->data_editor, "var-store", &vs, NULL); - - gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de)); - - g_object_get (vs, "dictionary", &dict, NULL); - g_object_set (treeview, "model", dict, - "selection-mode", GTK_SELECTION_SINGLE, - NULL); - - g_signal_connect (treeview, "cursor-changed", G_CALLBACK (populate_text), - textview); - - - gtk_text_view_set_indent (GTK_TEXT_VIEW (textview), -5); - - psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog), - treeview_item_selected, treeview); - - response = psppire_dialog_run (PSPPIRE_DIALOG (dialog)); - - switch (response) - { - case PSPPIRE_RESPONSE_GOTO: - { - const struct variable *var = - psppire_dict_view_get_selected_variable (PSPPIRE_DICT_VIEW (treeview)); - - if ( NULL == var) - goto done; - - g_object_set (de->data_editor, - "current-variable", var_get_dict_index (var), - NULL); - } - - break; - case PSPPIRE_RESPONSE_PASTE: - { - gchar *syntax = generate_syntax (PSPPIRE_DICT_VIEW (treeview)); - paste_syntax_to_window (syntax); - - g_free (syntax); - } - break; - default: - break; - } - - done: - g_object_unref (xml); -} - -static gchar * -generate_syntax (PsppireDictView *treeview) -{ - const struct variable *var = - psppire_dict_view_get_selected_variable (treeview); - - if ( NULL == var) - return g_strdup (""); - - return g_strdup (var_get_name (var)); -} - diff --git a/src/ui/gui/variable-info-dialog.h b/src/ui/gui/variable-info-dialog.h deleted file mode 100644 index c08f6c9..0000000 --- a/src/ui/gui/variable-info-dialog.h +++ /dev/null @@ -1,24 +0,0 @@ -/* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2007 Free Software Foundation - - 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 3 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, see . */ - -#ifndef __VARIABLE_DIALOG_H -#define __VARIABLE_DIALOG_H - -#include "psppire-data-window.h" - -void variable_info_dialog (PsppireDataWindow * data); - -#endif diff --git a/src/ui/gui/widgets.c b/src/ui/gui/widgets.c index 6111bdf..cedab8c 100644 --- a/src/ui/gui/widgets.c +++ b/src/ui/gui/widgets.c @@ -14,6 +14,7 @@ #include "psppire-val-chooser.h" #include "psppire-dialog-action-descriptives.h" +#include "psppire-dialog-action-var-info.h" /* Any custom widgets which are to be used in GtkBuilder ui files @@ -33,4 +34,5 @@ preregister_widgets (void) psppire_var_view_get_type (); psppire_dialog_action_descriptives_get_type (); + psppire_dialog_action_var_info_get_type (); } -- 1.7.2.5