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