Implemented the Binomial Test dialog box.
[pspp-builds.git] / src / ui / gui / binomial-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2010  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 #include <config.h>
18
19 #include "binomial-dialog.h"
20
21 #include <language/syntax-string-source.h>
22
23 #include "psppire-dialog.h"
24 #include "psppire-var-view.h"
25 #include "psppire-acr.h"
26 #include "dialog-common.h"
27
28 #include "helper.h"
29 #include "executor.h"
30
31
32 #include <gtk/gtk.h>
33
34 struct binomial_dialog
35 {
36   PsppireDict *dict;
37   GtkWidget *var_view;
38
39   GtkWidget *button1;
40
41   GtkWidget *prop_entry;
42
43   GtkWidget *cutpoint_button;
44   GtkWidget *cutpoint_entry;
45 };
46
47 static void
48 set_sensitivity (GtkToggleButton *button, GtkWidget *w)
49 {
50   gboolean state = gtk_toggle_button_get_active (button);
51   gtk_widget_set_sensitive (w, state);
52 }
53
54
55 static gboolean
56 get_proportion (const struct binomial_dialog *bin_d, double *prop)
57 {
58     const gchar *text = gtk_entry_get_text (GTK_ENTRY (bin_d->prop_entry));
59     gchar *endptr = NULL;
60      *prop = g_strtod (text, &endptr);
61
62     if (endptr == text)
63       return FALSE;
64
65     return TRUE; 
66 }
67
68 static gboolean
69 dialog_state_valid (gpointer data)
70 {
71   double prop;
72   struct binomial_dialog *bin_d = data;
73
74   GtkTreeModel *vars =
75     gtk_tree_view_get_model (GTK_TREE_VIEW (bin_d->var_view));
76
77   GtkTreeIter notused;
78
79   if ( !gtk_tree_model_get_iter_first (vars, &notused) )
80     return FALSE;
81
82   if ( ! get_proportion (bin_d, &prop))
83     return FALSE;
84
85   if (prop < 0 || prop > 1.0)
86     return FALSE;
87
88   return TRUE;
89 }
90
91
92 static void
93 refresh (struct binomial_dialog *bin_d)
94 {
95   GtkTreeModel *liststore =
96     gtk_tree_view_get_model (GTK_TREE_VIEW (bin_d->var_view));
97
98   gtk_list_store_clear (GTK_LIST_STORE (liststore));
99
100   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (bin_d->button1), TRUE);
101
102   gtk_entry_set_text (GTK_ENTRY (bin_d->prop_entry), "0.5");
103
104   gtk_entry_set_text (GTK_ENTRY (bin_d->cutpoint_entry), "");
105 }
106
107
108
109 static char *
110 generate_syntax (const struct binomial_dialog *scd)
111 {
112   gchar *text;
113   double prop;
114   GString *string;
115
116   string = g_string_new ("NPAR TEST\n\t/BINOMIAL");
117
118   if ( get_proportion (scd, &prop))
119     g_string_append_printf (string, "(%g)", prop);
120
121   g_string_append (string, " =");
122
123   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (scd->var_view), 0, string);
124
125   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->cutpoint_button)))
126     {
127       const gchar *cutpoint = gtk_entry_get_text (GTK_ENTRY (scd->cutpoint_entry));
128       g_string_append_printf (string, "(%s)", cutpoint);
129     }
130
131   g_string_append (string, ".\n");
132
133   text = string->str;
134
135   g_string_free (string, FALSE);
136
137   return text;
138 }
139
140
141
142 /* Pops up the Chi-Square dialog box */
143 void
144 binomial_dialog (PsppireDataWindow *dw)
145 {
146   gint response;
147
148   struct binomial_dialog bin_d;
149
150   GtkBuilder *xml = builder_new ("binomial.ui");
151   PsppireVarStore *vs;
152
153   GtkWidget *dialog = get_widget_assert   (xml, "binomial-dialog");
154
155
156
157   GtkWidget *dict_view = get_widget_assert   (xml, "dict-view");
158
159   g_object_get (dw->data_editor, "var-store", &vs, NULL);
160
161   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (dw));
162
163   bin_d.var_view  = get_widget_assert (xml, "variables-treeview");
164   bin_d.button1   = get_widget_assert (xml, "radiobutton3");
165   bin_d.prop_entry = get_widget_assert (xml, "proportion-entry");
166
167   bin_d.cutpoint_entry =     get_widget_assert   (xml, "cutpoint-entry");
168   bin_d.cutpoint_button =    get_widget_assert   (xml, "radiobutton4");
169
170   g_object_get (vs, "dictionary", &bin_d.dict, NULL);
171   g_object_set (dict_view,
172                 "model", bin_d.dict, 
173                 "predicate", var_is_numeric,
174                 NULL);
175
176   g_signal_connect (bin_d.cutpoint_button, "toggled", G_CALLBACK (set_sensitivity),
177                     bin_d.cutpoint_entry);
178
179   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &bin_d);
180
181   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
182                                       dialog_state_valid, &bin_d);
183
184   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
185
186
187   switch (response)
188     {
189     case GTK_RESPONSE_OK:
190       {
191         gchar *syntax = generate_syntax (&bin_d);
192
193         struct getl_interface *sss = create_syntax_string_source (syntax);
194         execute_syntax (sss);
195
196         g_free (syntax);
197       }
198       break;
199     case PSPPIRE_RESPONSE_PASTE:
200       {
201         gchar *syntax = generate_syntax (&bin_d);
202         paste_syntax_to_window (syntax);
203         g_free (syntax);
204       }
205       break;
206     default:
207       break;
208     }
209
210   g_object_unref (xml);
211 }