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