From 7a2bc16d86f90a796e4c42a6c3f3908231bbe8e9 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Tue, 29 Dec 2015 11:00:01 +0100 Subject: [PATCH] Weight cases dialog: Convert from add hoc to PsppireDialogAction --- src/ui/gui/automake.mk | 4 +- src/ui/gui/data-editor.ui | 3 +- src/ui/gui/psppire-data-window.c | 2 - src/ui/gui/psppire-dialog-action-weight.c | 175 +++++++++++++++++++++ src/ui/gui/psppire-dialog-action-weight.h | 81 ++++++++++ src/ui/gui/weight-cases-dialog.c | 178 ---------------------- src/ui/gui/weight-cases-dialog.h | 27 ---- src/ui/gui/widgets.c | 2 + 8 files changed, 262 insertions(+), 210 deletions(-) create mode 100644 src/ui/gui/psppire-dialog-action-weight.c create mode 100644 src/ui/gui/psppire-dialog-action-weight.h delete mode 100644 src/ui/gui/weight-cases-dialog.c delete mode 100644 src/ui/gui/weight-cases-dialog.h diff --git a/src/ui/gui/automake.mk b/src/ui/gui/automake.mk index d9fa465b93..c2577cb845 100644 --- a/src/ui/gui/automake.mk +++ b/src/ui/gui/automake.mk @@ -259,6 +259,8 @@ src_ui_gui_psppire_SOURCES = \ src/ui/gui/psppire-dialog-action-univariate.h \ src/ui/gui/psppire-dialog-action-var-info.c \ src/ui/gui/psppire-dialog-action-var-info.h \ + src/ui/gui/psppire-dialog-action-weight.c \ + src/ui/gui/psppire-dialog-action-weight.h \ src/ui/gui/psppire-dict.c \ src/ui/gui/psppire-dict.h \ src/ui/gui/psppire-dictview.c \ @@ -317,8 +319,6 @@ src_ui_gui_psppire_SOURCES = \ src/ui/gui/var-display.h \ src/ui/gui/var-type-dialog.c \ src/ui/gui/var-type-dialog.h \ - src/ui/gui/weight-cases-dialog.c \ - src/ui/gui/weight-cases-dialog.h \ src/ui/gui/widget-io.c \ src/ui/gui/widget-io.h \ src/ui/gui/widgets.c \ diff --git a/src/ui/gui/data-editor.ui b/src/ui/gui/data-editor.ui index e5d8d7ae1f..edca09d89c 100644 --- a/src/ui/gui/data-editor.ui +++ b/src/ui/gui/data-editor.ui @@ -213,9 +213,10 @@ - + data_weight-cases _Weight Cases... + uimanager1 Weight cases by variable data-weight-cases diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c index 7ea037a911..118387f97e 100644 --- a/src/ui/gui/psppire-data-window.c +++ b/src/ui/gui/psppire-data-window.c @@ -41,7 +41,6 @@ #include "ui/gui/recode-dialog.h" #include "ui/gui/select-cases-dialog.h" #include "ui/gui/split-file-dialog.h" -#include "ui/gui/weight-cases-dialog.h" #include "ui/syntax-gen.h" #include "gl/c-strcase.h" @@ -1047,7 +1046,6 @@ psppire_data_window_finish_init (PsppireDataWindow *de, connect_action (de, "data_select-cases", G_CALLBACK (select_cases_dialog)); connect_action (de, "data_split-file", G_CALLBACK (split_file_dialog)); - connect_action (de, "data_weight-cases", G_CALLBACK (weight_cases_dialog)); connect_action (de, "utilities_comments", G_CALLBACK (comments_dialog)); connect_action (de, "transform_recode-same", G_CALLBACK (recode_same_dialog)); connect_action (de, "transform_recode-different", G_CALLBACK (recode_different_dialog)); diff --git a/src/ui/gui/psppire-dialog-action-weight.c b/src/ui/gui/psppire-dialog-action-weight.c new file mode 100644 index 0000000000..ab06987b1b --- /dev/null +++ b/src/ui/gui/psppire-dialog-action-weight.c @@ -0,0 +1,175 @@ +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2015 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-weight.h" +#include "psppire-selector.h" +#include "psppire-var-view.h" +#include "dict-display.h" + +#include "psppire-dialog.h" +#include "builder-wrapper.h" + +#include +#define _(msgid) gettext (msgid) +#define N_(msgid) msgid + +static void psppire_dialog_action_weight_init (PsppireDialogActionWeight *act); +static void psppire_dialog_action_weight_class_init (PsppireDialogActionWeightClass *class); + +G_DEFINE_TYPE (PsppireDialogActionWeight, psppire_dialog_action_weight, PSPPIRE_TYPE_DIALOG_ACTION); + + +static char * +generate_syntax (PsppireDialogAction *pda) +{ + gchar *syntax = NULL; + PsppireDialogActionWeight *wcd = PSPPIRE_DIALOG_ACTION_WEIGHT (pda); + + const gchar *text = gtk_entry_get_text (GTK_ENTRY (wcd->entry)); + + const struct variable *var = psppire_dict_lookup_var (pda->dict, text); + + if ( var == NULL) + syntax = g_strdup ("WEIGHT OFF.\n"); + else + syntax = g_strdup_printf ("WEIGHT BY %s.\n", + var_get_name (var)); + + return syntax; +} + + +static gboolean +dialog_state_valid (gpointer data) +{ + return TRUE; +} + +static void +refresh (PsppireDialogAction *pda) +{ + PsppireDialogActionWeight *wcd = PSPPIRE_DIALOG_ACTION_WEIGHT (pda); + + const struct variable *var = dict_get_weight (pda->dict->dict); + + if ( ! var ) + { + gtk_entry_set_text (GTK_ENTRY (wcd->entry), ""); + gtk_label_set_text (GTK_LABEL (wcd->status), _("Do not weight cases")); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->off), TRUE); + } + else + { + gchar *text = + g_strdup_printf (_("Weight cases by %s"), var_get_name (var)); + + gtk_entry_set_text (GTK_ENTRY (wcd->entry), var_get_name (var)); + gtk_label_set_text (GTK_LABEL (wcd->status), text); + + g_free (text); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->on), TRUE); + } + + g_signal_emit_by_name (wcd->entry, "activate"); +} + +static void +on_select (PsppireSelector *sel, gpointer data) +{ + PsppireDialogActionWeight *wcd = data; + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->on), TRUE); + gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), TRUE); +} + +static void +on_deselect (PsppireSelector *sel, gpointer data) +{ + PsppireDialogActionWeight *wcd = data; + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->off), TRUE); + gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), FALSE); +} + +static void +on_toggle (GtkToggleButton *off, gpointer data) +{ + PsppireDialogActionWeight *wcd = data; + + if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wcd->off))) + { + gtk_entry_set_text (GTK_ENTRY (wcd->entry), ""); + } +} + + +static void +psppire_dialog_action_weight_activate (PsppireDialogAction *pda) +{ + PsppireDialogActionWeight *act = PSPPIRE_DIALOG_ACTION_WEIGHT (pda); + + GHashTable *thing = psppire_dialog_action_get_hash_table (pda); + GtkBuilder *xml = g_hash_table_lookup (thing, pda); + if (!xml) + { + xml = builder_new ("weight.ui"); + g_hash_table_insert (thing, pda, xml); + + pda->dialog = get_widget_assert (xml, "weight-cases-dialog"); + pda->source = get_widget_assert (xml, "weight-cases-treeview"); + + act->entry = get_widget_assert (xml, "weight-cases-entry"); + act->off = get_widget_assert (xml,"weight-cases-radiobutton1"); + act->on = get_widget_assert (xml, "radiobutton2"); + act->status = get_widget_assert (xml, "weight-status-label"); + GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector"); + + g_signal_connect (selector, "selected", G_CALLBACK (on_select), act); + g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect), act); + g_signal_connect (act->off, "toggled", G_CALLBACK (on_toggle), act); + + g_object_set (pda->source, + "selection-mode", GTK_SELECTION_SINGLE, + "predicate", var_is_numeric, + NULL); + + psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector), + is_currently_in_entry); + } + + psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid); + psppire_dialog_action_set_refresh (pda, refresh); + + if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_weight_parent_class)->activate) + PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_weight_parent_class)->activate (pda); +} + +static void +psppire_dialog_action_weight_class_init (PsppireDialogActionWeightClass *class) +{ + psppire_dialog_action_set_activation (class, psppire_dialog_action_weight_activate); + PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax; +} + + +static void +psppire_dialog_action_weight_init (PsppireDialogActionWeight *act) +{ +} + diff --git a/src/ui/gui/psppire-dialog-action-weight.h b/src/ui/gui/psppire-dialog-action-weight.h new file mode 100644 index 0000000000..2487b0a188 --- /dev/null +++ b/src/ui/gui/psppire-dialog-action-weight.h @@ -0,0 +1,81 @@ +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2015 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_WEIGHT_H__ +#define __PSPPIRE_DIALOG_ACTION_WEIGHT_H__ + +G_BEGIN_DECLS + + +#define PSPPIRE_TYPE_DIALOG_ACTION_WEIGHT (psppire_dialog_action_weight_get_type ()) + +#define PSPPIRE_DIALOG_ACTION_WEIGHT(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + PSPPIRE_TYPE_DIALOG_ACTION_WEIGHT, PsppireDialogActionWeight)) + +#define PSPPIRE_DIALOG_ACTION_WEIGHT_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), \ + PSPPIRE_TYPE_DIALOG_ACTION_WEIGHT, \ + PsppireDialogActionWeightClass)) + + +#define PSPPIRE_IS_DIALOG_ACTION_WEIGHT(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PSPPIRE_TYPE_DIALOG_ACTION_WEIGHT)) + +#define PSPPIRE_IS_DIALOG_ACTION_WEIGHT_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), PSPPIRE_TYPE_DIALOG_ACTION_WEIGHT)) + + +#define PSPPIRE_DIALOG_ACTION_WEIGHT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + PSPPIRE_TYPE_DIALOG_ACTION_WEIGHT, \ + PsppireDialogActionWeightClass)) + +typedef struct _PsppireDialogActionWeight PsppireDialogActionWeight; +typedef struct _PsppireDialogActionWeightClass PsppireDialogActionWeightClass; + + +struct _PsppireDialogActionWeight +{ + PsppireDialogAction parent; + + /*< private >*/ + + GtkWidget *entry; + GtkWidget *variables; + + GtkWidget *status; + GtkWidget *off; + GtkWidget *on; +}; + + +struct _PsppireDialogActionWeightClass +{ + PsppireDialogActionClass parent_class; +}; + + +GType psppire_dialog_action_weight_get_type (void) ; + +G_END_DECLS + +#endif /* __PSPPIRE_DIALOG_ACTION_WEIGHT_H__ */ diff --git a/src/ui/gui/weight-cases-dialog.c b/src/ui/gui/weight-cases-dialog.c deleted file mode 100644 index af2740abaf..0000000000 --- a/src/ui/gui/weight-cases-dialog.c +++ /dev/null @@ -1,178 +0,0 @@ -/* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2007, 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 "weight-cases-dialog.h" -#include "psppire-selector.h" -#include "psppire-dialog.h" -#include "executor.h" -#include "psppire-data-window.h" -#include "dict-display.h" -#include "builder-wrapper.h" -#include "helper.h" - -#include - -#include -#define _(msgid) gettext (msgid) -#define N_(msgid) msgid - - -struct weight_cases_dialog -{ - PsppireDict *dict; - GtkEntry *entry; - GtkLabel *status; - GtkToggleButton *off; - GtkToggleButton *on; -}; - -static void -on_select (PsppireSelector *sel, gpointer data) -{ - struct weight_cases_dialog *wcd = data; - - gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->on), TRUE); - gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), TRUE); -} - -static void -on_deselect (PsppireSelector *sel, gpointer data) -{ - struct weight_cases_dialog *wcd = data; - - gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->off), TRUE); - gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), FALSE); -} - - -static void -on_toggle (GtkToggleButton *button, gpointer data) -{ - GtkEntry *entry = data; - if ( gtk_toggle_button_get_active (button)) - gtk_entry_set_text (entry, ""); -} - -static void -refresh (PsppireDialog *dialog, const struct weight_cases_dialog *wcd) -{ - const struct variable *var = dict_get_weight (wcd->dict->dict); - - if ( ! var ) - { - gtk_entry_set_text (wcd->entry, ""); - gtk_label_set_text (wcd->status, _("Do not weight cases")); - gtk_toggle_button_set_active (wcd->off, TRUE); - } - else - { - gchar *text = - g_strdup_printf (_("Weight cases by %s"), var_get_name (var)); - - gtk_entry_set_text (wcd->entry, var_get_name (var)); - gtk_label_set_text (wcd->status, text); - - g_free (text); - gtk_toggle_button_set_active (wcd->on, TRUE); - } - - g_signal_emit_by_name (wcd->entry, "activate"); -} - - -static gchar * generate_syntax (const struct weight_cases_dialog *wcd); - - -/* Pops up the Weight Cases dialog box */ -void -weight_cases_dialog (PsppireDataWindow *de) -{ - gint response; - struct weight_cases_dialog wcd; - - GtkBuilder *xml = builder_new ("weight.ui"); - - GtkWidget *dialog = get_widget_assert (xml, "weight-cases-dialog"); - GtkWidget *source = get_widget_assert (xml, "weight-cases-treeview"); - GtkWidget *entry = get_widget_assert (xml, "weight-cases-entry"); - GtkWidget *radiobutton1 = get_widget_assert (xml, - "weight-cases-radiobutton1"); - GtkWidget *radiobutton2 = get_widget_assert (xml, "radiobutton2"); - GtkWidget *status = get_widget_assert (xml, "weight-status-label"); - - GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector"); - - g_object_get (de->data_editor, "dictionary", &wcd.dict, NULL); - - gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de)); - - g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry); - - g_signal_connect (selector, "selected", G_CALLBACK (on_select), &wcd); - g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect), &wcd); - - g_object_set (source, "model", wcd.dict, - "selection-mode", GTK_SELECTION_SINGLE, - "predicate", var_is_numeric, - NULL); - - psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector), - is_currently_in_entry); - - wcd.entry = GTK_ENTRY (entry); - wcd.status = GTK_LABEL (status); - wcd.off = GTK_TOGGLE_BUTTON (radiobutton1); - wcd.on = GTK_TOGGLE_BUTTON (radiobutton2); - - g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &wcd); - - response = psppire_dialog_run (PSPPIRE_DIALOG (dialog)); - - g_object_unref (xml); - - switch (response) - { - case GTK_RESPONSE_OK: - g_free (execute_syntax_string (de, generate_syntax (&wcd))); - break; - case PSPPIRE_RESPONSE_PASTE: - g_free (paste_syntax_to_window (generate_syntax (&wcd))); - break; - default: - break; - } -} - - -static gchar * -generate_syntax (const struct weight_cases_dialog *wcd) -{ - gchar *syntax; - - const gchar *text = gtk_entry_get_text (wcd->entry); - - struct variable *var = psppire_dict_lookup_var (wcd->dict, text); - - if ( var == NULL) - syntax = g_strdup ("WEIGHT OFF."); - else - syntax = g_strdup_printf ("WEIGHT BY %s.\n", - var_get_name (var)); - - return syntax; -} diff --git a/src/ui/gui/weight-cases-dialog.h b/src/ui/gui/weight-cases-dialog.h deleted file mode 100644 index e7a3d0b906..0000000000 --- a/src/ui/gui/weight-cases-dialog.h +++ /dev/null @@ -1,27 +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 WEIGHT_CASES_DIALOG_H -#define WEIGHT_CASES_DIALOG_H - -#include "psppire-data-window.h" - -/* Pops up the Weight Cases dialog box */ -void weight_cases_dialog (PsppireDataWindow * data); - - -#endif diff --git a/src/ui/gui/widgets.c b/src/ui/gui/widgets.c index 1e1a8e9027..7161a0d113 100644 --- a/src/ui/gui/widgets.c +++ b/src/ui/gui/widgets.c @@ -51,6 +51,7 @@ #include "psppire-dialog-action-two-sample.h" #include "psppire-dialog-action-univariate.h" #include "psppire-dialog-action-var-info.h" +#include "psppire-dialog-action-weight.h" #include "psppire-value-entry.h" static volatile GType kludge; @@ -108,6 +109,7 @@ preregister_widgets (void) psppire_dialog_action_tt1s_get_type (); psppire_dialog_action_two_sample_get_type (); psppire_dialog_action_univariate_get_type (); + psppire_dialog_action_weight_get_type (); /* This seems to be necessary on Cygwin. It ought not to be necessary. Having it here can't do any harm. */ -- 2.30.2