Added some missing calls to gtk_window_set_transient_for
[pspp-builds.git] / src / ui / gui / t-test-independent-samples-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007  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
19 #include <glade/glade.h>
20 #include <gtk/gtk.h>
21 #include "t-test-independent-samples-dialog.h"
22 #include "psppire-dict.h"
23 #include "psppire-var-store.h"
24 #include "helper.h"
25 #include <gtksheet/gtksheet.h>
26 #include "data-editor.h"
27 #include "psppire-dialog.h"
28 #include "dialog-common.h"
29 #include "dict-display.h"
30 #include "widget-io.h"
31
32 #include <language/syntax-string-source.h>
33 #include "syntax-editor.h"
34
35 #include <gettext.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
38
39
40 struct tt_indep_samples_dialog
41 {
42   GladeXML *xml;  /* The xml that generated the widgets */
43   GtkWidget *dialog;
44   PsppireDict *dict;
45   gboolean groups_defined;
46   gboolean non_default_options;
47   gdouble confidence_interval;
48 };
49
50
51 static gchar *
52 generate_syntax (const struct tt_indep_samples_dialog *d)
53 {
54   gchar *text;
55   GtkWidget *entry =
56     get_widget_assert (d->xml, "indep-samples-t-test-entry");
57
58   GtkWidget *tv =
59     get_widget_assert (d->xml, "indep-samples-t-test-treeview2");
60
61   GString *str = g_string_new ("T-TEST /VARIABLES=");
62
63   append_variable_names (str, d->dict, GTK_TREE_VIEW (tv));
64
65   g_string_append (str, "\n\t/GROUPS=");
66
67   g_string_append (str, gtk_entry_get_text (GTK_ENTRY (entry)));
68
69   if ( d->groups_defined )
70     {
71       GtkWidget *entry1 = get_widget_assert (d->xml, "group1-entry");
72       GtkWidget *entry2 = get_widget_assert (d->xml, "group2-entry");
73
74       g_string_append (str, "(");
75       g_string_append (str, gtk_entry_get_text (GTK_ENTRY (entry1)));
76       g_string_append (str, ",");
77       g_string_append (str, gtk_entry_get_text (GTK_ENTRY (entry2)));
78       g_string_append (str, ")");
79     }
80
81   if ( d->non_default_options )
82     {
83       GtkToggleButton *analysis =
84         GTK_TOGGLE_BUTTON (get_widget_assert (d->xml, "radiobutton1"));
85
86       g_string_append (str, "\n\t");
87       g_string_append_printf (str, "/CRITERIA=CIN(%g)",
88                               d->confidence_interval/100.0);
89
90
91       g_string_append (str, "\n\t");
92       g_string_append_printf (str, "/MISSING=%s",
93                        gtk_toggle_button_get_active (analysis) ?
94                        "ANALYSIS" : "LISTWISE");
95     }
96
97   g_string_append (str, ".\n");
98
99   text = str->str;
100
101   g_string_free (str, FALSE);
102
103   return text;
104 }
105
106
107
108 static void
109 refresh (GladeXML *xml)
110 {
111   GtkWidget *entry =
112     get_widget_assert (xml, "indep-samples-t-test-entry");
113
114   GtkWidget *tv =
115     get_widget_assert (xml, "indep-samples-t-test-treeview2");
116
117   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (tv));
118
119   gtk_entry_set_text (GTK_ENTRY (entry), "");
120
121   gtk_list_store_clear (GTK_LIST_STORE (model));
122 }
123
124
125 static gboolean
126 define_groups_state_valid (gpointer data)
127 {
128   struct tt_indep_samples_dialog *d = data;
129
130   GtkWidget *entry1 = get_widget_assert (d->xml, "group1-entry");
131   GtkWidget *entry2 = get_widget_assert (d->xml, "group2-entry");
132
133   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry1))))
134     return FALSE;
135
136   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry2))))
137     return FALSE;
138
139   return TRUE;
140 }
141
142 static void
143 run_define_groups (struct tt_indep_samples_dialog *ttd)
144 {
145   gint response;
146   GtkWidget *dialog =
147     get_widget_assert (ttd->xml, "define-groups-dialog");
148
149
150   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
151                                       define_groups_state_valid, ttd);
152
153   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (ttd->dialog));
154
155   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
156
157   ttd->groups_defined = (response == PSPPIRE_RESPONSE_CONTINUE);
158 }
159
160
161 static void
162 run_options (struct tt_indep_samples_dialog *ttd)
163 {
164   gint response;
165   GtkWidget *dialog =
166     get_widget_assert (ttd->xml, "options-dialog");
167
168   GtkWidget *box =
169     get_widget_assert (ttd->xml, "vbox1");
170
171   GtkSpinButton *conf_percent = NULL;
172
173   GtkWidget *confidence =
174     widget_scanf (_("Confidence Interval: %2d %%"),
175                   &conf_percent);
176
177   gtk_spin_button_set_value (conf_percent, ttd->confidence_interval);
178
179   gtk_widget_show (confidence);
180
181   gtk_box_pack_start_defaults (GTK_BOX (box), confidence);
182
183   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (ttd->dialog));
184
185   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
186
187   if ( response == PSPPIRE_RESPONSE_CONTINUE)
188     {
189       ttd->non_default_options = TRUE;
190       ttd->confidence_interval = gtk_spin_button_get_value (conf_percent);
191     }
192
193   gtk_container_remove (GTK_CONTAINER (box), confidence);
194 }
195
196
197
198
199 static gboolean
200 dialog_state_valid (gpointer data)
201 {
202   struct tt_indep_samples_dialog *tt_d = data;
203
204   GtkWidget *entry =
205     get_widget_assert (tt_d->xml, "indep-samples-t-test-entry");
206
207   GtkWidget *tv_vars =
208     get_widget_assert (tt_d->xml, "indep-samples-t-test-treeview2");
209
210   GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (tv_vars));
211
212   GtkTreeIter notused;
213
214   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))))
215     return FALSE;
216
217
218   if ( 0 == gtk_tree_model_get_iter_first (vars, &notused))
219     return FALSE;
220
221   return TRUE;
222 }
223
224
225 /* Pops up the dialog box */
226 void
227 t_test_independent_samples_dialog (GObject *o, gpointer data)
228 {
229   struct tt_indep_samples_dialog tt_d;
230   gint response;
231   struct data_editor *de = data;
232
233   PsppireVarStore *vs;
234
235   GladeXML *xml = XML_NEW ("t-test.glade");
236
237   tt_d.dialog = get_widget_assert (xml,
238                                          "t-test-independent-samples-dialog");
239
240   GtkSheet *var_sheet =
241     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
242
243   GtkWidget *dict_view =
244     get_widget_assert (xml, "indep-samples-t-test-treeview1");
245
246   GtkWidget *test_variables_treeview =
247     get_widget_assert (xml, "indep-samples-t-test-treeview2");
248
249   GtkWidget *selector2 =
250     get_widget_assert (xml, "indep-samples-t-test-selector2");
251
252   GtkWidget *selector1 =
253     get_widget_assert (xml, "indep-samples-t-test-selector1");
254
255
256   GtkWidget *entry =
257     get_widget_assert (xml, "indep-samples-t-test-entry");
258
259   GtkWidget *define_groups_button =
260     get_widget_assert (xml, "define-groups-button");
261
262   GtkWidget *options_button =
263     get_widget_assert (xml, "options-button");
264
265   vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
266
267   tt_d.xml = xml;
268   tt_d.dict = vs->dict;
269   tt_d.groups_defined = FALSE;
270   tt_d.non_default_options = FALSE;
271   tt_d.confidence_interval = 95.0;
272
273   gtk_window_set_transient_for (GTK_WINDOW (tt_d.dialog), de->parent.window);
274
275   attach_dictionary_to_treeview (GTK_TREE_VIEW (dict_view),
276                                  vs->dict,
277                                  GTK_SELECTION_MULTIPLE, NULL);
278
279   set_dest_model (GTK_TREE_VIEW (test_variables_treeview), vs->dict);
280
281
282   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
283                                  dict_view, test_variables_treeview,
284                                  insert_source_row_into_tree_view,
285                                  NULL);
286
287
288   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
289                                  dict_view, entry,
290                                  insert_source_row_into_entry,
291                                  is_currently_in_entry);
292
293   g_signal_connect_swapped (define_groups_button, "clicked",
294                             G_CALLBACK (run_define_groups), &tt_d);
295
296
297   g_signal_connect_swapped (options_button, "clicked",
298                             G_CALLBACK (run_options), &tt_d);
299
300
301   g_signal_connect_swapped (tt_d.dialog, "refresh", G_CALLBACK (refresh),  xml);
302
303
304   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (tt_d.dialog),
305                                       dialog_state_valid, &tt_d);
306
307   response = psppire_dialog_run (PSPPIRE_DIALOG (tt_d.dialog));
308
309   switch (response)
310     {
311     case GTK_RESPONSE_OK:
312       {
313         gchar *syntax = generate_syntax (&tt_d);
314         struct getl_interface *sss = create_syntax_string_source (syntax);
315         execute_syntax (sss);
316
317         g_free (syntax);
318       }
319       break;
320     case PSPPIRE_RESPONSE_PASTE:
321       {
322         gchar *syntax = generate_syntax (&tt_d);
323
324         struct syntax_editor *se =
325           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
326
327         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
328
329         g_free (syntax);
330       }
331       break;
332     default:
333       break;
334     }
335
336
337   g_object_unref (xml);
338 }
339
340