Added the One Sample T Test dialog
[pspp-builds.git] / src / ui / gui / t-test-one-sample.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-one-sample.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
33 #include "t-test-options.h"
34 #include <language/syntax-string-source.h>
35 #include "syntax-editor.h"
36
37 #include <gettext.h>
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41
42 struct tt_one_sample_dialog
43 {
44   PsppireDict *dict;
45   GtkWidget *vars_treeview;
46   GtkWidget *test_value_entry;
47   struct tt_options_dialog *opt;
48 };
49
50
51 static gchar *
52 generate_syntax (const struct tt_one_sample_dialog *d)
53 {
54   gchar *text;
55
56   GString *str = g_string_new ("T-TEST ");
57
58   g_string_append_printf (str, "/TESTVAL=%s",
59                           gtk_entry_get_text (GTK_ENTRY (d->test_value_entry)));
60
61   g_string_append (str, "\n\t/VARIABLES=");
62
63   append_variable_names (str, d->dict, GTK_TREE_VIEW (d->vars_treeview));
64
65   tt_options_dialog_append_syntax (d->opt, str);
66
67   g_string_append (str, ".\n");
68
69   text = str->str;
70
71   g_string_free (str, FALSE);
72
73   return text;
74 }
75
76
77
78 static void
79 refresh (const struct tt_one_sample_dialog *d)
80 {
81   GtkTreeModel *model =
82     gtk_tree_view_get_model (GTK_TREE_VIEW (d->vars_treeview));
83
84   gtk_entry_set_text (GTK_ENTRY (d->test_value_entry), "");
85
86   gtk_list_store_clear (GTK_LIST_STORE (model));
87 }
88
89
90
91 static gboolean
92 dialog_state_valid (gpointer data)
93 {
94   gchar *s = NULL;
95   const gchar *text;
96   struct tt_one_sample_dialog *tt_d = data;
97
98   GtkTreeModel *vars =
99     gtk_tree_view_get_model (GTK_TREE_VIEW (tt_d->vars_treeview));
100
101   GtkTreeIter notused;
102
103   text = gtk_entry_get_text (GTK_ENTRY (tt_d->test_value_entry));
104
105   if ( 0 == strcmp ("", text))
106     return FALSE;
107
108   /* Check to see if the entry is numeric */
109   g_strtod (text, &s);
110
111   if (s - text != strlen (text))
112     return FALSE;
113
114
115   if ( 0 == gtk_tree_model_get_iter_first (vars, &notused))
116     return FALSE;
117
118   return TRUE;
119 }
120
121
122 /* Pops up the dialog box */
123 void
124 t_test_one_sample_dialog (GObject *o, gpointer data)
125 {
126   struct tt_one_sample_dialog tt_d;
127   gint response;
128   struct data_editor *de = data;
129
130   PsppireVarStore *vs;
131
132   GladeXML *xml = XML_NEW ("t-test.glade");
133
134   GtkSheet *var_sheet =
135     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
136
137   GtkWidget *dict_view =
138     get_widget_assert (xml, "one-sample-t-test-treeview2");
139
140   GtkWidget *options_button =
141     get_widget_assert (xml, "button1");
142
143   GtkWidget *selector = get_widget_assert (xml, "psppire-selector1");
144
145   GtkWidget *dialog = get_widget_assert (xml, "t-test-one-sample-dialog");
146
147   vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
148
149   tt_d.dict = vs->dict;
150   tt_d.vars_treeview = get_widget_assert (xml, "one-sample-t-test-treeview1");
151   tt_d.test_value_entry = get_widget_assert (xml, "test-value-entry");
152   tt_d.opt = tt_options_dialog_create (xml, de->parent.window);
153
154   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
155
156   attach_dictionary_to_treeview (GTK_TREE_VIEW (dict_view),
157                                  vs->dict,
158                                  GTK_SELECTION_MULTIPLE,
159                                  var_is_numeric);
160
161   set_dest_model (GTK_TREE_VIEW (tt_d.vars_treeview), vs->dict);
162
163
164   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
165                                  dict_view, tt_d.vars_treeview,
166                                  insert_source_row_into_tree_view,
167                                  NULL);
168
169
170   g_signal_connect_swapped (dialog, "refresh",
171                             G_CALLBACK (refresh),  &tt_d);
172
173
174   g_signal_connect_swapped (options_button, "clicked",
175                             G_CALLBACK (tt_options_dialog_run), tt_d.opt);
176
177   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
178                                       dialog_state_valid, &tt_d);
179
180   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
181
182   switch (response)
183     {
184     case GTK_RESPONSE_OK:
185       {
186         gchar *syntax = generate_syntax (&tt_d);
187         struct getl_interface *sss = create_syntax_string_source (syntax);
188         execute_syntax (sss);
189
190         g_free (syntax);
191       }
192       break;
193     case PSPPIRE_RESPONSE_PASTE:
194       {
195         gchar *syntax = generate_syntax (&tt_d);
196
197         struct syntax_editor *se =
198           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
199
200         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
201
202         g_free (syntax);
203       }
204       break;
205     default:
206       break;
207     }
208
209
210   g_object_unref (xml);
211   tt_options_dialog_destroy (tt_d.opt);
212 }
213
214