68b45fcb5a6a934378305a5bc30cbddd19b03c17
[pspp-builds.git] / src / ui / gui / t-test-paired-samples.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008  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 #include <language/syntax-string-source.h>
21
22 #include "psppire-data-window.h"
23 #include "psppire-selector.h"
24 #include "psppire-var-view.h"
25
26 #include "psppire-dict.h"
27 #include "psppire-var-store.h"
28 #include "t-test-paired-samples.h"
29 #include "t-test-options.h"
30
31 #include "dialog-common.h"
32 #include "psppire-dialog.h"
33
34 #include "executor.h"
35
36 #include "helper.h"
37
38 #include "psppire-var-ptr.h"
39
40
41 #include <gettext.h>
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
44
45
46 struct tt_paired_samples_dialog
47 {
48   PsppireDict *dict;
49   GtkWidget *pairs_treeview;
50   GtkTreeModel *list_store;
51   struct tt_options_dialog *opt;
52 };
53
54 static gchar *
55 generate_syntax (const struct tt_paired_samples_dialog *d)
56 {
57   gchar *text = NULL;
58   GString *str =   g_string_new ("T-TEST \n\tPAIRS = ");
59
60   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (d->pairs_treeview), 0, str);
61
62   g_string_append (str, " WITH ");
63
64   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (d->pairs_treeview), 1, str);
65
66   g_string_append (str, " (PAIRED)");
67   g_string_append (str, "\n");
68
69   tt_options_dialog_append_syntax (d->opt, str);
70
71   g_string_append (str, ".\n");
72
73   text = str->str;
74   g_string_free (str, FALSE);
75
76   return text;
77 }
78
79 static void
80 refresh (struct tt_paired_samples_dialog *tt_d)
81 {
82   gtk_list_store_clear (GTK_LIST_STORE (tt_d->list_store));
83 }
84
85 static gboolean
86 dialog_state_valid (gpointer data)
87 {
88   struct variable *v = NULL;
89   struct tt_paired_samples_dialog *tt_d = data;
90   GtkTreeIter dest_iter;
91
92   gint n_rows = gtk_tree_model_iter_n_children  (tt_d->list_store, NULL);
93
94   if ( n_rows == 0 )
95     return FALSE;
96
97   /* Get the last row */
98   gtk_tree_model_iter_nth_child (tt_d->list_store, &dest_iter,
99                                  NULL, n_rows - 1);
100
101   /* Get the last (2nd) column */
102   gtk_tree_model_get (tt_d->list_store, &dest_iter, 1, &v, -1);
103
104
105   return (v != NULL);
106 }
107
108
109
110 static void
111 select_as_pair_member (GtkTreeIter source_iter,
112                        GtkWidget *dest,
113                        GtkTreeModel *source_model,
114                        gpointer data)
115 {
116   struct variable *v;
117   struct variable *v1;
118   gint n_rows;
119   GtkTreeIter dest_iter;
120   struct tt_paired_samples_dialog *tt_d = data;
121
122
123   gtk_tree_model_get (source_model, &source_iter,
124                       DICT_TVM_COL_VAR, &v, -1);
125
126   n_rows = gtk_tree_model_iter_n_children  (tt_d->list_store, NULL);
127
128   if ( n_rows > 0 )
129     {
130
131       gtk_tree_model_iter_nth_child (tt_d->list_store,
132                                      &dest_iter, NULL, n_rows - 1);
133
134       gtk_tree_model_get (tt_d->list_store, &dest_iter, 1, &v1, -1);
135     }
136
137   if ( n_rows == 0 || v1 != NULL)
138     {
139       gtk_list_store_append (GTK_LIST_STORE (tt_d->list_store), &dest_iter);
140
141       gtk_list_store_set (GTK_LIST_STORE (tt_d->list_store), &dest_iter,
142                           0, v,
143                           1, NULL,
144                           -1);
145     }
146   else
147     {
148       gtk_list_store_set (GTK_LIST_STORE (tt_d->list_store), &dest_iter,
149                           1, v,
150                           -1);
151
152     }
153 }
154
155 #if 0
156 /* Append a new column to TV at position C, and heading TITLE */
157 static void
158 add_new_column (GtkTreeView *tv, const gchar *title, gint c)
159 {
160   GtkTreeViewColumn *col = gtk_tree_view_column_new ();
161   GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
162
163   gtk_tree_view_column_set_min_width (col, 100);
164   gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
165   gtk_tree_view_column_set_resizable (col, TRUE);
166
167
168   gtk_tree_view_column_set_title (col, title);
169
170   gtk_tree_view_column_pack_start (col, renderer, TRUE);
171
172   gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
173
174   gtk_tree_view_append_column (tv, col);
175
176   gtk_tree_view_column_add_attribute  (col, renderer, "text", c);
177 }
178
179 #endif
180
181
182 /* Pops up the dialog box */
183 void
184 t_test_paired_samples_dialog (GObject *o, gpointer data)
185 {
186   struct tt_paired_samples_dialog tt_d;
187   gint response;
188   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
189
190   PsppireVarStore *vs = NULL;
191
192   GtkBuilder *xml = builder_new ("t-test.ui");
193
194   GtkWidget *dict_view =
195     get_widget_assert (xml, "paired-samples-t-test-treeview1");
196
197   GtkWidget *options_button = get_widget_assert (xml, "paired-samples-t-test-options-button");
198
199   GtkWidget *selector = get_widget_assert (xml, "psppire-selector3");
200
201   GtkWidget *dialog = get_widget_assert (xml, "t-test-paired-samples-dialog");
202
203   g_object_get (de->data_editor, "var-store", &vs, NULL);
204
205   g_object_get (vs, "dictionary", &tt_d.dict, NULL);
206   tt_d.pairs_treeview =
207    get_widget_assert (xml, "paired-samples-t-test-treeview2");
208   tt_d.opt = tt_options_dialog_create (xml, GTK_WINDOW (de));
209
210   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
211
212
213   g_object_set (dict_view, "model", tt_d.dict,
214                 "predicate",
215                 var_is_numeric, NULL);
216
217   
218   tt_d.list_store = gtk_tree_view_get_model (GTK_TREE_VIEW (tt_d.pairs_treeview));
219
220   psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
221                                     select_as_pair_member,
222                                     &tt_d);
223
224   g_signal_connect_swapped (dialog, "refresh",
225                             G_CALLBACK (refresh),  &tt_d);
226
227
228   g_signal_connect_swapped (options_button, "clicked",
229                             G_CALLBACK (tt_options_dialog_run), tt_d.opt);
230
231   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
232                                       dialog_state_valid, &tt_d);
233
234   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
235
236   switch (response)
237     {
238     case GTK_RESPONSE_OK:
239       {
240         gchar *syntax = generate_syntax (&tt_d);
241
242         struct getl_interface *sss = create_syntax_string_source (syntax);
243         execute_syntax (sss);
244
245         g_free (syntax);
246       }
247       break;
248     case PSPPIRE_RESPONSE_PASTE:
249       {
250         gchar *syntax = generate_syntax (&tt_d);
251         paste_syntax_in_new_window (syntax);
252
253         g_free (syntax);
254       }
255       break;
256     default:
257       break;
258     }
259
260
261   g_object_unref (xml);
262
263   tt_options_dialog_destroy (tt_d.opt);
264 }
265
266