Applied patch #6428
[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), 0);
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 = NULL;
131
132   GladeXML *xml = XML_NEW ("t-test.glade");
133
134   GtkWidget *dict_view =
135     get_widget_assert (xml, "one-sample-t-test-treeview2");
136
137   GtkWidget *options_button =
138     get_widget_assert (xml, "button1");
139
140   GtkWidget *selector = get_widget_assert (xml, "psppire-selector1");
141
142   GtkWidget *dialog = get_widget_assert (xml, "t-test-one-sample-dialog");
143
144   g_object_get (de->data_editor, "var-store", &vs, NULL);
145
146   tt_d.dict = vs->dict;
147   tt_d.vars_treeview = get_widget_assert (xml, "one-sample-t-test-treeview1");
148   tt_d.test_value_entry = get_widget_assert (xml, "test-value-entry");
149   tt_d.opt = tt_options_dialog_create (xml, de->parent.window);
150
151   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
152
153   attach_dictionary_to_treeview (GTK_TREE_VIEW (dict_view),
154                                  vs->dict,
155                                  GTK_SELECTION_MULTIPLE,
156                                  var_is_numeric);
157
158   set_dest_model (GTK_TREE_VIEW (tt_d.vars_treeview), vs->dict);
159
160
161   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
162                                  dict_view, tt_d.vars_treeview,
163                                  insert_source_row_into_tree_view,
164                                  NULL,
165                                  NULL);
166
167
168   g_signal_connect_swapped (dialog, "refresh",
169                             G_CALLBACK (refresh),  &tt_d);
170
171
172   g_signal_connect_swapped (options_button, "clicked",
173                             G_CALLBACK (tt_options_dialog_run), tt_d.opt);
174
175   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
176                                       dialog_state_valid, &tt_d);
177
178   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
179
180   switch (response)
181     {
182     case GTK_RESPONSE_OK:
183       {
184         gchar *syntax = generate_syntax (&tt_d);
185         struct getl_interface *sss = create_syntax_string_source (syntax);
186         execute_syntax (sss);
187
188         g_free (syntax);
189       }
190       break;
191     case PSPPIRE_RESPONSE_PASTE:
192       {
193         gchar *syntax = generate_syntax (&tt_d);
194
195         struct syntax_editor *se =
196           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
197
198         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
199
200         g_free (syntax);
201       }
202       break;
203     default:
204       break;
205     }
206
207
208   g_object_unref (xml);
209   tt_options_dialog_destroy (tt_d.opt);
210 }
211
212