gui: Make syntax execution functions take a PsppireDataWindow argument.
[pspp-builds.git] / src / ui / gui / t-test-paired-samples.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2010, 2011  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <config.h>
19 #include <gtk/gtk.h>
20
21 #include "psppire-data-window.h"
22 #include "psppire-selector.h"
23 #include "psppire-var-view.h"
24
25 #include "psppire-dict.h"
26 #include "psppire-var-store.h"
27 #include "t-test-paired-samples.h"
28 #include "t-test-options.h"
29
30 #include "dialog-common.h"
31 #include "psppire-dialog.h"
32
33 #include "executor.h"
34
35 #include "helper.h"
36
37 #include "psppire-var-ptr.h"
38
39
40 #include <gettext.h>
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
43
44
45 struct tt_paired_samples_dialog
46 {
47   PsppireDict *dict;
48   GtkWidget *pairs_treeview;
49   GtkTreeModel *list_store;
50   struct tt_options_dialog *opt;
51 };
52
53 static gchar *
54 generate_syntax (const struct tt_paired_samples_dialog *d)
55 {
56   gchar *text = NULL;
57   GString *str =   g_string_new ("T-TEST \n\tPAIRS = ");
58
59   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (d->pairs_treeview), 0, str);
60
61   g_string_append (str, " WITH ");
62
63   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (d->pairs_treeview), 1, str);
64
65   g_string_append (str, " (PAIRED)");
66   g_string_append (str, "\n");
67
68   tt_options_dialog_append_syntax (d->opt, str);
69
70   g_string_append (str, ".\n");
71
72   text = str->str;
73   g_string_free (str, FALSE);
74
75   return text;
76 }
77
78 static void
79 refresh (struct tt_paired_samples_dialog *tt_d)
80 {
81   gtk_list_store_clear (GTK_LIST_STORE (tt_d->list_store));
82 }
83
84 static gboolean
85 dialog_state_valid (gpointer data)
86 {
87   struct variable *v = NULL;
88   struct tt_paired_samples_dialog *tt_d = data;
89   GtkTreeIter dest_iter;
90
91   gint n_rows = gtk_tree_model_iter_n_children  (tt_d->list_store, NULL);
92
93   if ( n_rows == 0 )
94     return FALSE;
95
96   /* Get the last row */
97   gtk_tree_model_iter_nth_child (tt_d->list_store, &dest_iter,
98                                  NULL, n_rows - 1);
99
100   /* Get the last (2nd) column */
101   gtk_tree_model_get (tt_d->list_store, &dest_iter, 1, &v, -1);
102
103
104   return (v != NULL);
105 }
106
107
108
109 static void
110 select_as_pair_member (GtkTreeIter source_iter,
111                        GtkWidget *dest,
112                        GtkTreeModel *source_model,
113                        gpointer data)
114 {
115   struct variable *v;
116   struct variable *v1;
117   gint n_rows;
118   GtkTreeIter dest_iter;
119   struct tt_paired_samples_dialog *tt_d = data;
120
121
122   gtk_tree_model_get (source_model, &source_iter,
123                       DICT_TVM_COL_VAR, &v, -1);
124
125   n_rows = gtk_tree_model_iter_n_children  (tt_d->list_store, NULL);
126
127   if ( n_rows > 0 )
128     {
129
130       gtk_tree_model_iter_nth_child (tt_d->list_store,
131                                      &dest_iter, NULL, n_rows - 1);
132
133       gtk_tree_model_get (tt_d->list_store, &dest_iter, 1, &v1, -1);
134     }
135   else
136     v1 = NULL;
137
138   if ( n_rows == 0 || v1 != NULL)
139     {
140       gtk_list_store_append (GTK_LIST_STORE (tt_d->list_store), &dest_iter);
141
142       gtk_list_store_set (GTK_LIST_STORE (tt_d->list_store), &dest_iter,
143                           0, v,
144                           1, NULL,
145                           -1);
146     }
147   else
148     {
149       gtk_list_store_set (GTK_LIST_STORE (tt_d->list_store), &dest_iter,
150                           1, v,
151                           -1);
152
153     }
154 }
155
156 /* Pops up the dialog box */
157 void
158 t_test_paired_samples_dialog (PsppireDataWindow *de)
159 {
160   struct tt_paired_samples_dialog tt_d;
161   gint response;
162
163   PsppireVarStore *vs = NULL;
164
165   GtkBuilder *xml = builder_new ("t-test.ui");
166
167   GtkWidget *dict_view =
168     get_widget_assert (xml, "paired-samples-t-test-treeview1");
169
170   GtkWidget *options_button = get_widget_assert (xml, "paired-samples-t-test-options-button");
171
172   GtkWidget *selector = get_widget_assert (xml, "psppire-selector3");
173
174   GtkWidget *dialog = get_widget_assert (xml, "t-test-paired-samples-dialog");
175
176   g_object_get (de->data_editor, "var-store", &vs, NULL);
177
178   g_object_get (vs, "dictionary", &tt_d.dict, NULL);
179   tt_d.pairs_treeview =
180    get_widget_assert (xml, "paired-samples-t-test-treeview2");
181   tt_d.opt = tt_options_dialog_create (xml, GTK_WINDOW (de));
182
183   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
184
185
186   g_object_set (dict_view, "model", tt_d.dict,
187                 "predicate",
188                 var_is_numeric, NULL);
189
190   
191   tt_d.list_store = gtk_tree_view_get_model (GTK_TREE_VIEW (tt_d.pairs_treeview));
192
193   psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
194                                     select_as_pair_member,
195                                     &tt_d);
196
197   g_signal_connect_swapped (dialog, "refresh",
198                             G_CALLBACK (refresh),  &tt_d);
199
200
201   g_signal_connect_swapped (options_button, "clicked",
202                             G_CALLBACK (tt_options_dialog_run), tt_d.opt);
203
204   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
205                                       dialog_state_valid, &tt_d);
206
207   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
208
209   switch (response)
210     {
211     case GTK_RESPONSE_OK:
212       g_free (execute_syntax_string (de, generate_syntax (&tt_d)));
213       break;
214     case PSPPIRE_RESPONSE_PASTE:
215       g_free (paste_syntax_to_window (generate_syntax (&tt_d)));
216       break;
217     default:
218       break;
219     }
220
221
222   g_object_unref (xml);
223
224   tt_options_dialog_destroy (tt_d.opt);
225 }
226
227