Use a more reliable way of setting the initial state of PsppireDialogAction objects.
[pspp] / src / ui / gui / psppire-dialog-action-binomial.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012, 2014  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 <float.h>
24
25 #include "dialog-common.h"
26 #include "helper.h"
27 #include <ui/syntax-gen.h>
28 #include "psppire-var-view.h"
29
30 #include "psppire-dialog.h"
31 #include "builder-wrapper.h"
32 #include "psppire-dict.h"
33 #include "libpspp/str.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
38
39
40 static void
41 psppire_dialog_action_binomial_class_init (PsppireDialogActionBinomialClass *class);
42
43 G_DEFINE_TYPE (PsppireDialogActionBinomial, psppire_dialog_action_binomial, PSPPIRE_TYPE_DIALOG_ACTION);
44
45
46 static gboolean
47 get_proportion (PsppireDialogActionBinomial *act, double *prop)
48 {
49     const gchar *text = gtk_entry_get_text (GTK_ENTRY (act->prop_entry));
50     gchar *endptr = NULL;
51      *prop = g_strtod (text, &endptr);
52
53     if (endptr == text)
54       return FALSE;
55
56     return TRUE;
57 }
58
59 static gboolean
60 dialog_state_valid (gpointer data)
61 {
62   PsppireDialogActionBinomial *act = PSPPIRE_DIALOG_ACTION_BINOMIAL (data);
63   double prop;
64
65   GtkTreeModel *vars =
66     gtk_tree_view_get_model (GTK_TREE_VIEW (act->var_view));
67
68   GtkTreeIter notused;
69
70   if ( !gtk_tree_model_get_iter_first (vars, &notused) )
71     return FALSE;
72
73   if ( ! get_proportion (act, &prop))
74     return FALSE;
75
76   if (prop < 0 || prop > 1.0)
77     return FALSE;
78
79   return TRUE;
80 }
81
82 static void
83 refresh (PsppireDialogAction *da)
84 {
85   PsppireDialogActionBinomial *act = PSPPIRE_DIALOG_ACTION_BINOMIAL (da);
86   GtkTreeModel *liststore =
87     gtk_tree_view_get_model (GTK_TREE_VIEW (act->var_view));
88
89   gtk_list_store_clear (GTK_LIST_STORE (liststore));
90
91   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (act->button1), TRUE);
92
93   gtk_entry_set_text (GTK_ENTRY (act->prop_entry), "0.5");
94
95   gtk_entry_set_text (GTK_ENTRY (act->cutpoint_entry), "");
96 }
97
98
99 static GtkBuilder *
100 psppire_dialog_action_binomial_activate (PsppireDialogAction *a, GVariant *param)
101 {
102   PsppireDialogActionBinomial *act = PSPPIRE_DIALOG_ACTION_BINOMIAL (a);
103   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
104
105   GtkBuilder *xml = builder_new ( "binomial.ui");
106
107   pda->dialog = get_widget_assert   (xml, "binomial-dialog");
108   pda->source = get_widget_assert   (xml, "dict-view");
109
110   act->var_view   = get_widget_assert (xml, "variables-treeview");
111   act->button1   = get_widget_assert (xml, "radiobutton3");
112   act->prop_entry = get_widget_assert (xml, "proportion-entry");
113
114   act->cutpoint_entry =     get_widget_assert   (xml, "cutpoint-entry");
115   act->cutpoint_button =    get_widget_assert   (xml, "radiobutton4");
116
117   g_signal_connect (act->cutpoint_button, "toggled", G_CALLBACK (set_sensitivity_from_toggle),
118                     act->cutpoint_entry);
119
120   psppire_dialog_action_set_refresh (pda, refresh);
121
122   psppire_dialog_action_set_valid_predicate (pda,
123                                              dialog_state_valid);
124   return xml;
125 }
126
127
128
129 static char *
130 generate_syntax (const PsppireDialogAction *a)
131 {
132   PsppireDialogActionBinomial *scd = PSPPIRE_DIALOG_ACTION_BINOMIAL (a);
133   gchar *text = NULL;
134
135   double prop;
136   struct string str;
137
138   ds_init_cstr (&str, "NPAR TEST\n\t/BINOMIAL");
139
140   if ( get_proportion (scd, &prop))
141     ds_put_c_format (&str, "(%.*g)", DBL_DIG + 1, prop);
142
143   ds_put_cstr (&str, " =");
144
145   psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (scd->var_view), 0, &str);
146
147   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->cutpoint_button)))
148     {
149       const gchar *cutpoint = gtk_entry_get_text (GTK_ENTRY (scd->cutpoint_entry));
150       ds_put_c_format  (&str, "(%s)", cutpoint);
151     }
152
153   ds_put_cstr (&str, ".\n");
154
155   text = ds_steal_cstr (&str);
156
157   ds_destroy (&str);
158
159   return text;
160 }
161
162 static void
163 psppire_dialog_action_binomial_class_init (PsppireDialogActionBinomialClass *class)
164 {
165   PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_binomial_activate;
166
167   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
168 }
169
170
171 static void
172 psppire_dialog_action_binomial_init (PsppireDialogActionBinomial *act)
173 {
174 }
175