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