4bf486340df3348ad5ea1870c784b6f0f283766c
[pspp] / src / ui / gui / psppire-dialog-action-tt1s.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012, 2013  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 #include <config.h>
19
20 #include "psppire-dialog-action-tt1s.h"
21
22 #include "psppire-var-view.h"
23 #include "t-test-options.h"
24
25 #include "psppire-dialog.h"
26 #include "builder-wrapper.h"
27
28 static void psppire_dialog_action_tt1s_init            (PsppireDialogActionTt1s      *act);
29 static void psppire_dialog_action_tt1s_class_init      (PsppireDialogActionTt1sClass *class);
30
31 G_DEFINE_TYPE (PsppireDialogActionTt1s, psppire_dialog_action_tt1s, PSPPIRE_TYPE_DIALOG_ACTION);
32
33
34 static char *
35 generate_syntax (PsppireDialogAction *act)
36 {
37   PsppireDialogActionTt1s *d = PSPPIRE_DIALOG_ACTION_TT1S (act);
38   gchar *text;
39
40   GString *str = g_string_new ("T-TEST ");
41
42   g_string_append_printf (str, "/TESTVAL=%s",
43                           gtk_entry_get_text (GTK_ENTRY (d->test_value_entry)));
44
45   g_string_append (str, "\n\t/VARIABLES=");
46
47   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (d->vars_treeview), 0, str);
48
49   tt_options_dialog_append_syntax (d->opt, str);
50
51   g_string_append (str, ".\n");
52
53   text = str->str;
54
55   g_string_free (str, FALSE);
56
57   return text;
58 }
59
60
61 static gboolean
62 dialog_state_valid (gpointer data)
63 {
64   PsppireDialogActionTt1s *tt_d = PSPPIRE_DIALOG_ACTION_TT1S (data);
65   gchar *s = NULL;
66   const gchar *text;
67
68
69   GtkTreeModel *vars =
70     gtk_tree_view_get_model (GTK_TREE_VIEW (tt_d->vars_treeview));
71
72   GtkTreeIter notused;
73
74   text = gtk_entry_get_text (GTK_ENTRY (tt_d->test_value_entry));
75
76   if ( 0 == strcmp ("", text))
77     return FALSE;
78
79   /* Check to see if the entry is numeric */
80   g_strtod (text, &s);
81
82   if (s - text != strlen (text))
83     return FALSE;
84
85
86   if ( 0 == gtk_tree_model_get_iter_first (vars, &notused))
87     return FALSE;
88
89   return TRUE;
90 }
91
92 static void
93 refresh (PsppireDialogAction *rd_)
94 {
95   PsppireDialogActionTt1s *d = PSPPIRE_DIALOG_ACTION_TT1S (rd_);
96
97   GtkTreeModel *model =
98     gtk_tree_view_get_model (GTK_TREE_VIEW (d->vars_treeview));
99
100   gtk_entry_set_text (GTK_ENTRY (d->test_value_entry), "");
101
102   gtk_list_store_clear (GTK_LIST_STORE (model));
103 }
104
105 static void
106 psppire_dialog_action_tt1s_activate (GtkAction *a)
107 {
108   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
109   PsppireDialogActionTt1s *act = PSPPIRE_DIALOG_ACTION_TT1S (a);
110
111   GHashTable *thing = psppire_dialog_action_get_pointer (pda);
112   GtkBuilder *xml = g_hash_table_lookup (thing, a);
113   if (!xml)
114     {
115       xml = builder_new ("t-test.ui");
116       g_hash_table_insert (thing, a, xml);
117     }
118
119   GtkWidget *options_button = get_widget_assert (xml, "button1");
120
121   pda->dialog = get_widget_assert (xml, "t-test-one-sample-dialog");
122   pda->source = get_widget_assert (xml, "one-sample-t-test-treeview2");
123
124   g_object_set (pda->source, 
125                 "predicate", var_is_numeric, NULL);
126
127   act->vars_treeview = get_widget_assert (xml, "one-sample-t-test-treeview1");
128   act->test_value_entry = get_widget_assert (xml, "test-value-entry");
129
130   act->opt = tt_options_dialog_create (GTK_WINDOW (pda->toplevel));
131
132   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
133   psppire_dialog_action_set_refresh (pda, refresh);
134
135   g_signal_connect_swapped (options_button, "clicked",
136                             G_CALLBACK (tt_options_dialog_run), act->opt);
137
138   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_tt1s_parent_class)->activate)
139     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_tt1s_parent_class)->activate (pda);
140 }
141
142 static void
143 psppire_dialog_action_tt1s_finalize (GObject *a)
144 {
145   PsppireDialogActionTt1s *act = PSPPIRE_DIALOG_ACTION_TT1S (a);
146   tt_options_dialog_destroy (act->opt);
147 }
148
149 static void
150 psppire_dialog_action_tt1s_class_init (PsppireDialogActionTt1sClass *class)
151 {
152   GObjectClass *object_class = G_OBJECT_CLASS (class);
153   psppire_dialog_action_set_activation (class, psppire_dialog_action_tt1s_activate);
154
155   object_class->finalize = psppire_dialog_action_tt1s_finalize;
156   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
157 }
158
159
160 static void
161 psppire_dialog_action_tt1s_init (PsppireDialogActionTt1s *act)
162 {
163 }
164
165
166
167
168