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