Binomial test dialog: Convert to new style of action objects.
[pspp] / src / ui / gui / psppire-dialog-action-binomial.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 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
18 #include <config.h>
19
20 #include "psppire-dialog-action-binomial.h"
21 #include "psppire-value-entry.h"
22
23 #include "dialog-common.h"
24 #include "helper.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
27
28 #include "psppire-dialog.h"
29 #include "builder-wrapper.h"
30 #include "checkbox-treeview.h"
31 #include "psppire-dict.h"
32 #include "libpspp/str.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 static void
40 psppire_dialog_action_binomial_class_init (PsppireDialogActionBinomialClass *class);
41
42 G_DEFINE_TYPE (PsppireDialogActionBinomial, psppire_dialog_action_binomial, PSPPIRE_TYPE_DIALOG_ACTION);
43
44
45 static gboolean
46 get_proportion (PsppireDialogActionBinomial *act, double *prop)
47 {
48     const gchar *text = gtk_entry_get_text (GTK_ENTRY (act->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   PsppireDialogActionBinomial *act = PSPPIRE_DIALOG_ACTION_BINOMIAL (data);
62   double prop;
63
64   GtkTreeModel *vars =
65     gtk_tree_view_get_model (GTK_TREE_VIEW (act->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 (act, &prop))
73     return FALSE;
74
75   if (prop < 0 || prop > 1.0)
76     return FALSE;
77
78   return TRUE;
79 }
80
81 static void
82 refresh (PsppireDialogAction *da)
83 {
84   PsppireDialogActionBinomial *act = PSPPIRE_DIALOG_ACTION_BINOMIAL (da);
85   GtkTreeModel *liststore =
86     gtk_tree_view_get_model (GTK_TREE_VIEW (act->var_view));
87
88   gtk_list_store_clear (GTK_LIST_STORE (liststore));
89
90   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (act->button1), TRUE);
91
92   gtk_entry_set_text (GTK_ENTRY (act->prop_entry), "0.5");
93
94   gtk_entry_set_text (GTK_ENTRY (act->cutpoint_entry), "");
95 }
96
97
98 static void
99 psppire_dialog_action_binomial_activate (GtkAction *a)
100 {
101   PsppireDialogActionBinomial *act = PSPPIRE_DIALOG_ACTION_BINOMIAL (a);
102   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
103
104   GtkBuilder *xml = builder_new ("binomial.ui");
105
106   pda->dialog = get_widget_assert   (xml, "binomial-dialog");
107   pda->source = get_widget_assert   (xml, "dict-view");
108
109   act->var_view   = get_widget_assert (xml, "variables-treeview");
110   act->button1   = get_widget_assert (xml, "radiobutton3");
111   act->prop_entry = get_widget_assert (xml, "proportion-entry");
112
113   act->cutpoint_entry =     get_widget_assert   (xml, "cutpoint-entry");
114   act->cutpoint_button =    get_widget_assert   (xml, "radiobutton4");
115
116   g_object_unref (xml);
117
118
119   g_signal_connect (act->cutpoint_button, "toggled", G_CALLBACK (set_sensitivity_from_toggle),
120                     act->cutpoint_entry);
121
122   psppire_dialog_action_set_refresh (pda, refresh);
123
124   psppire_dialog_action_set_valid_predicate (pda,
125                                         dialog_state_valid);
126
127   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_binomial_parent_class)->activate)
128     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_binomial_parent_class)->activate (pda);
129 }
130
131
132
133 static char *
134 generate_syntax (PsppireDialogAction *a)
135 {
136   PsppireDialogActionBinomial *scd = PSPPIRE_DIALOG_ACTION_BINOMIAL (a);
137   gchar *text = NULL;
138
139   double prop;
140   GString *string = g_string_new ("NPAR TEST\n\t/BINOMIAL");
141
142   if ( get_proportion (scd, &prop))
143     g_string_append_printf (string, "(%g)", prop);
144
145   g_string_append (string, " =");
146
147   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (scd->var_view), 0, string);
148
149   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->cutpoint_button)))
150     {
151       const gchar *cutpoint = gtk_entry_get_text (GTK_ENTRY (scd->cutpoint_entry));
152       g_string_append_printf (string, "(%s)", cutpoint);
153     }
154
155   g_string_append (string, ".\n");
156
157   text = string->str;
158
159   g_string_free (string, FALSE);
160
161   return text;
162 }
163
164 static void
165 psppire_dialog_action_binomial_class_init (PsppireDialogActionBinomialClass *class)
166 {
167   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
168
169   action_class->activate = psppire_dialog_action_binomial_activate;
170
171   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
172 }
173
174
175 static void
176 psppire_dialog_action_binomial_init (PsppireDialogActionBinomial *act)
177 {
178 }
179