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