Weight cases dialog: Convert from add hoc to PsppireDialogAction
authorJohn Darrington <john@darrington.wattle.id.au>
Tue, 29 Dec 2015 10:00:01 +0000 (11:00 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Tue, 29 Dec 2015 10:00:01 +0000 (11:00 +0100)
src/ui/gui/automake.mk
src/ui/gui/data-editor.ui
src/ui/gui/psppire-data-window.c
src/ui/gui/psppire-dialog-action-weight.c [new file with mode: 0644]
src/ui/gui/psppire-dialog-action-weight.h [new file with mode: 0644]
src/ui/gui/weight-cases-dialog.c [deleted file]
src/ui/gui/weight-cases-dialog.h [deleted file]
src/ui/gui/widgets.c

index d9fa465b937d2c3cbcf0fa151a84342f99050e34..c2577cb8459f618854ab923a1a4f95d42a883f9a 100644 (file)
@@ -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 \
index e5d8d7ae1fcde0d29c3cabaf4da36e59da87b051..edca09d89cd1575bbde3a0af32a9b926f812bbea 100644 (file)
           </object>
         </child>
         <child>
-          <object class="GtkAction" id="data_weight-cases">
+          <object class="PsppireDialogActionWeight" id="data_weight-cases">
             <property name="name">data_weight-cases</property>
             <property name="label" translatable="yes">_Weight Cases...</property>
+           <property name="manager">uimanager1</property>
             <property name="tooltip" translatable="yes">Weight cases by variable</property>
             <property name="stock-id">data-weight-cases</property>
           </object>
index 7ea037a9117d75b5423263d46dd91228e8b5cfc6..118387f97e32ad2779463e06e8179457121623b8 100644 (file)
@@ -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 (file)
index 0000000..ab06987
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>. */
+
+
+#include <config.h>
+
+#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 <gettext.h>
+#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 (file)
index 0000000..2487b0a
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>. */
+
+
+#include <glib-object.h>
+#include <glib.h>
+
+#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 (file)
index af2740a..0000000
+++ /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 <http://www.gnu.org/licenses/>. */
-
-#include <config.h>
-
-#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 <gtk/gtk.h>
-
-#include <gettext.h>
-#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 (file)
index e7a3d0b..0000000
+++ /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 <http://www.gnu.org/licenses/>. */
-
-
-#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
index 1e1a8e902731aedccdb592d4a688af3c9c6e15b7..7161a0d1134bb9014f8876a6a38a6f77dfb4e10c 100644 (file)
@@ -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. */