Fix compiler warning
[pspp] / 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 gboolean
46 get_proportion (const struct binomial_dialog *bin_d, double *prop)
47 {
48     const gchar *text = gtk_entry_get_text (GTK_ENTRY (bin_d->prop_entry));
49     gchar *endptr = NULL;
50      *prop = g_strtod (text, &endptr);
51
52     if (endptr == text)
53       return FALSE;
54
55     return TRUE; 
56 }
57
58 static gboolean
59 dialog_state_valid (gpointer data)
60 {
61   double prop;
62   struct binomial_dialog *bin_d = data;
63
64   GtkTreeModel *vars =
65     gtk_tree_view_get_model (GTK_TREE_VIEW (bin_d->var_view));
66
67   GtkTreeIter notused;
68
69   if ( !gtk_tree_model_get_iter_first (vars, &notused) )
70     return FALSE;
71
72   if ( ! get_proportion (bin_d, &prop))
73     return FALSE;
74
75   if (prop < 0 || prop > 1.0)
76     return FALSE;
77
78   return TRUE;
79 }
80
81
82 static void
83 refresh (struct binomial_dialog *bin_d)
84 {
85   GtkTreeModel *liststore =
86     gtk_tree_view_get_model (GTK_TREE_VIEW (bin_d->var_view));
87
88   gtk_list_store_clear (GTK_LIST_STORE (liststore));
89
90   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (bin_d->button1), TRUE);
91
92   gtk_entry_set_text (GTK_ENTRY (bin_d->prop_entry), "0.5");
93
94   gtk_entry_set_text (GTK_ENTRY (bin_d->cutpoint_entry), "");
95 }
96
97
98
99 static char *
100 generate_syntax (const struct binomial_dialog *scd)
101 {
102   gchar *text;
103   double prop;
104   GString *string;
105
106   string = g_string_new ("NPAR TEST\n\t/BINOMIAL");
107
108   if ( get_proportion (scd, &prop))
109     g_string_append_printf (string, "(%g)", prop);
110
111   g_string_append (string, " =");
112
113   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (scd->var_view), 0, string);
114
115   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->cutpoint_button)))
116     {
117       const gchar *cutpoint = gtk_entry_get_text (GTK_ENTRY (scd->cutpoint_entry));
118       g_string_append_printf (string, "(%s)", cutpoint);
119     }
120
121   g_string_append (string, ".\n");
122
123   text = string->str;
124
125   g_string_free (string, FALSE);
126
127   return text;
128 }
129
130
131
132 /* Pops up the Chi-Square dialog box */
133 void
134 binomial_dialog (PsppireDataWindow *dw)
135 {
136   gint response;
137
138   struct binomial_dialog bin_d;
139
140   GtkBuilder *xml = builder_new ("binomial.ui");
141   PsppireVarStore *vs;
142
143   GtkWidget *dialog = get_widget_assert   (xml, "binomial-dialog");
144
145
146
147   GtkWidget *dict_view = get_widget_assert   (xml, "dict-view");
148
149   g_object_get (dw->data_editor, "var-store", &vs, NULL);
150
151   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (dw));
152
153   bin_d.var_view  = get_widget_assert (xml, "variables-treeview");
154   bin_d.button1   = get_widget_assert (xml, "radiobutton3");
155   bin_d.prop_entry = get_widget_assert (xml, "proportion-entry");
156
157   bin_d.cutpoint_entry =     get_widget_assert   (xml, "cutpoint-entry");
158   bin_d.cutpoint_button =    get_widget_assert   (xml, "radiobutton4");
159
160   g_object_get (vs, "dictionary", &bin_d.dict, NULL);
161   g_object_set (dict_view,
162                 "model", bin_d.dict, 
163                 "predicate", var_is_numeric,
164                 NULL);
165
166   g_signal_connect (bin_d.cutpoint_button, "toggled", G_CALLBACK (set_sensitivity_from_toggle),
167                     bin_d.cutpoint_entry);
168
169   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &bin_d);
170
171   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
172                                       dialog_state_valid, &bin_d);
173
174   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
175
176
177   switch (response)
178     {
179     case GTK_RESPONSE_OK:
180       g_free (execute_syntax_string (dw, generate_syntax (&bin_d)));
181       break;
182     case PSPPIRE_RESPONSE_PASTE:
183       g_free (paste_syntax_to_window (generate_syntax (&bin_d)));
184       break;
185     default:
186       break;
187     }
188
189   g_object_unref (xml);
190 }