Merge "master" into "psppsheet" to obtain bug fixes from "master".
[pspp] / src / ui / gui / t-test-paired-samples.c
index a239a47193b4eba5eb778355847ea2b1d5bc1490..1b1cfbc87f7ac7f29b7248596e7537bd2212252d 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008  Free Software Foundation
+   Copyright (C) 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
 
 #include <config.h>
 #include <gtk/gtk.h>
-#include <language/syntax-string-source.h>
+
+#include "t-test-options.h"
+#include "t-test-paired-samples.h"
 
 #include "psppire-data-window.h"
 #include "psppire-selector.h"
+#include "psppire-var-view.h"
 
 #include "psppire-dict.h"
-#include "psppire-var-store.h"
-#include "t-test-paired-samples.h"
-#include "t-test-options.h"
 
 #include "dialog-common.h"
 #include "psppire-dialog.h"
 
+#include "executor.h"
+
 #include "helper.h"
 
 #include "psppire-var-ptr.h"
 
+#include "paired-dialog.h"
 
 #include <gettext.h>
 #define _(msgid) gettext (msgid)
 #define N_(msgid) msgid
 
 
-struct tt_paired_samples_dialog
+static void
+refresh (void *aux)
+{
+}
+
+
+static gboolean
+valid (void *aux)
 {
-  PsppireDict *dict;
-  GtkWidget *pairs_treeview;
-  GtkTreeModel *list_store;
-  struct tt_options_dialog *opt;
-};
+  return TRUE;
+}
 
 static gchar *
-generate_syntax (const struct tt_paired_samples_dialog *d)
+generate_syntax (const struct paired_samples_dialog *d, const struct tt_options_dialog *opt)
 {
   gchar *text = NULL;
   GString *str =   g_string_new ("T-TEST \n\tPAIRS = ");
 
-  append_variable_names (str, d->dict, GTK_TREE_VIEW (d->pairs_treeview), 0);
+  psppire_var_view_append_names (PSPPIRE_VAR_VIEW (d->pairs_treeview), 0, str);
 
   g_string_append (str, " WITH ");
 
-  append_variable_names (str, d->dict, GTK_TREE_VIEW (d->pairs_treeview), 1);
+  psppire_var_view_append_names (PSPPIRE_VAR_VIEW (d->pairs_treeview), 1, str);
 
   g_string_append (str, " (PAIRED)");
   g_string_append (str, "\n");
 
-  tt_options_dialog_append_syntax (d->opt, str);
+  tt_options_dialog_append_syntax (opt, str);
 
   g_string_append (str, ".\n");
 
@@ -73,208 +80,43 @@ generate_syntax (const struct tt_paired_samples_dialog *d)
   return text;
 }
 
-static void
-refresh (struct tt_paired_samples_dialog *tt_d)
-{
-  gtk_list_store_clear (GTK_LIST_STORE (tt_d->list_store));
-}
-
-static gboolean
-dialog_state_valid (gpointer data)
-{
-  struct variable *v = NULL;
-  struct tt_paired_samples_dialog *tt_d = data;
-  GtkTreeIter dest_iter;
-
-  gint n_rows = gtk_tree_model_iter_n_children  (tt_d->list_store, NULL);
-
-  if ( n_rows == 0 )
-    return FALSE;
-
-  /* Get the last row */
-  gtk_tree_model_iter_nth_child (tt_d->list_store, &dest_iter,
-                                NULL, n_rows - 1);
-
-  /* Get the last (2nd) column */
-  gtk_tree_model_get (tt_d->list_store, &dest_iter, 1, &v, -1);
-
-
-  return (v != NULL);
-}
-
-
-
-static void
-select_as_pair_member (GtkTreeIter source_iter,
-                      GtkWidget *dest,
-                      GtkTreeModel *source_model,
-                      gpointer data)
-{
-  struct variable *v;
-  struct variable *v1;
-  gint n_rows;
-  GtkTreeIter dest_iter;
-  struct tt_paired_samples_dialog *tt_d = data;
-
-
-  gtk_tree_model_get (source_model, &source_iter,
-                     DICT_TVM_COL_VAR, &v, -1);
-
-  n_rows = gtk_tree_model_iter_n_children  (tt_d->list_store, NULL);
-
-  if ( n_rows > 0 )
-    {
-
-      gtk_tree_model_iter_nth_child (tt_d->list_store,
-                                    &dest_iter, NULL, n_rows - 1);
-
-      gtk_tree_model_get (tt_d->list_store, &dest_iter, 1, &v1, -1);
-    }
-
-  if ( n_rows == 0 || v1 != NULL)
-    {
-      gtk_list_store_append (GTK_LIST_STORE (tt_d->list_store), &dest_iter);
-
-      gtk_list_store_set (GTK_LIST_STORE (tt_d->list_store), &dest_iter,
-                         0, v,
-                         1, NULL,
-                         -1);
-    }
-  else
-    {
-      gtk_list_store_set (GTK_LIST_STORE (tt_d->list_store), &dest_iter,
-                         1, v,
-                         -1);
-
-    }
-}
-
-
-/* Append a new column to TV at position C, and heading TITLE */
-static void
-add_new_column (GtkTreeView *tv, const gchar *title, gint c)
-{
-  GtkTreeViewColumn *col = gtk_tree_view_column_new ();
-  GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
-
-  gtk_tree_view_column_set_min_width (col, 100);
-  gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
-  gtk_tree_view_column_set_resizable (col, TRUE);
-
-
-  gtk_tree_view_column_set_title (col, title);
-
-  gtk_tree_view_column_pack_start (col, renderer, TRUE);
-
-  gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
-
-  gtk_tree_view_append_column (tv, col);
-
-  gtk_tree_view_column_add_attribute  (col, renderer, "text", c);
-}
-
-
 /* Pops up the dialog box */
 void
-t_test_paired_samples_dialog (GObject *o, gpointer data)
+t_test_paired_samples_dialog (PsppireDataWindow *de)
 {
-  struct tt_paired_samples_dialog tt_d;
   gint response;
-  PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
-
-  PsppireVarStore *vs = NULL;
-
-  GtkBuilder *xml = builder_new ("t-test.ui");
-
-  GtkWidget *dict_view =
-    get_widget_assert (xml, "paired-samples-t-test-treeview1");
-
-  GtkWidget *options_button = get_widget_assert (xml, "paired-samples-t-test-options-button");
-
-  GtkWidget *selector = get_widget_assert (xml, "psppire-selector3");
+  struct paired_samples_dialog *tt_d = two_sample_dialog_create (de);
+  struct tt_options_dialog *opts = tt_options_dialog_create (GTK_WINDOW (de));
 
-  GtkWidget *dialog = get_widget_assert (xml, "t-test-paired-samples-dialog");
+  GtkWidget *bb = gtk_hbutton_box_new ();
+  GtkWidget *opt = gtk_button_new_with_mnemonic (_("_Options"));
+  gtk_box_pack_start (GTK_BOX (bb), opt, TRUE, TRUE, 5);
 
-  g_object_get (de->data_editor, "var-store", &vs, NULL);
+  gtk_widget_show_all (bb);
+  two_sample_dialog_add_widget (tt_d, bb);
+  
+  g_signal_connect_swapped (opt, "clicked", G_CALLBACK (tt_options_dialog_run), opts);
 
-  tt_d.dict = vs->dict;
-  tt_d.pairs_treeview =
-   get_widget_assert (xml, "paired-samples-t-test-treeview2");
-  tt_d.opt = tt_options_dialog_create (xml, GTK_WINDOW (de));
+  tt_d->refresh = refresh;
+  tt_d->valid = valid;
+  tt_d->aux = opts;
 
-  gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
+  gtk_window_set_title (GTK_WINDOW (tt_d->dialog), _("Paired Samples T Test"));
 
-
-  g_object_set (dict_view, "dictionary",
-                                vs->dict,
-                                "predicate",
-                                var_is_numeric, NULL);
-
-  {
-    tt_d.list_store =
-      GTK_TREE_MODEL (
-                     gtk_list_store_new (2,
-                                         PSPPIRE_VAR_PTR_TYPE,
-                                         PSPPIRE_VAR_PTR_TYPE));
-
-
-    gtk_tree_view_set_model (GTK_TREE_VIEW (tt_d.pairs_treeview),
-                            GTK_TREE_MODEL (tt_d.list_store));
-
-
-    add_new_column (GTK_TREE_VIEW (tt_d.pairs_treeview), _("Var 1"), 0);
-    add_new_column (GTK_TREE_VIEW (tt_d.pairs_treeview), _("Var 2"), 1);
-  }
-
-
-  psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
-                                dict_view,
-                                tt_d.pairs_treeview,
-                                select_as_pair_member,
-                                NULL,
-                                &tt_d);
-
-
-  g_signal_connect_swapped (dialog, "refresh",
-                           G_CALLBACK (refresh),  &tt_d);
-
-
-  g_signal_connect_swapped (options_button, "clicked",
-                           G_CALLBACK (tt_options_dialog_run), tt_d.opt);
-
-  psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
-                                     dialog_state_valid, &tt_d);
-
-  response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
+  response = psppire_dialog_run (PSPPIRE_DIALOG (tt_d->dialog));
 
   switch (response)
     {
     case GTK_RESPONSE_OK:
-      {
-       gchar *syntax = generate_syntax (&tt_d);
-
-       struct getl_interface *sss = create_syntax_string_source (syntax);
-       execute_syntax (sss);
-
-       g_free (syntax);
-      }
+      g_free (execute_syntax_string (de, generate_syntax (tt_d, opts)));
       break;
     case PSPPIRE_RESPONSE_PASTE:
-      {
-       gchar *syntax = generate_syntax (&tt_d);
-        paste_syntax_in_new_window (syntax);
-
-       g_free (syntax);
-      }
+      g_free (paste_syntax_to_window (generate_syntax (tt_d, opts)));
       break;
     default:
       break;
     }
 
-
-  g_object_unref (xml);
-
-  tt_options_dialog_destroy (tt_d.opt);
+  two_sample_dialog_destroy (tt_d);
+  tt_options_dialog_destroy (opts);
 }
-
-