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