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