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