Change some instances of GtkAction to PsppireDialogAction
[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 void
100 psppire_dialog_action_binomial_activate (PsppireDialogAction *a)
101 {
102   PsppireDialogActionBinomial *act = PSPPIRE_DIALOG_ACTION_BINOMIAL (a);
103   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
104
105   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
106   GtkBuilder *xml = g_hash_table_lookup (thing, a);
107   if (!xml)
108     {
109       xml = builder_new ("binomial.ui");
110       g_hash_table_insert (thing, a, xml);
111     }
112
113   pda->dialog = get_widget_assert   (xml, "binomial-dialog");
114   pda->source = get_widget_assert   (xml, "dict-view");
115
116   act->var_view   = get_widget_assert (xml, "variables-treeview");
117   act->button1   = get_widget_assert (xml, "radiobutton3");
118   act->prop_entry = get_widget_assert (xml, "proportion-entry");
119
120   act->cutpoint_entry =     get_widget_assert   (xml, "cutpoint-entry");
121   act->cutpoint_button =    get_widget_assert   (xml, "radiobutton4");
122
123   g_signal_connect (act->cutpoint_button, "toggled", G_CALLBACK (set_sensitivity_from_toggle),
124                     act->cutpoint_entry);
125
126   psppire_dialog_action_set_refresh (pda, refresh);
127
128   psppire_dialog_action_set_valid_predicate (pda,
129                                         dialog_state_valid);
130
131   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_binomial_parent_class)->activate)
132     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_binomial_parent_class)->activate (pda);
133 }
134
135
136
137 static char *
138 generate_syntax (PsppireDialogAction *a)
139 {
140   PsppireDialogActionBinomial *scd = PSPPIRE_DIALOG_ACTION_BINOMIAL (a);
141   gchar *text = NULL;
142
143   double prop;
144   struct string str;
145
146   ds_init_cstr (&str, "NPAR TEST\n\t/BINOMIAL");
147
148   if ( get_proportion (scd, &prop))
149     ds_put_c_format (&str, "(%.*g)", DBL_DIG + 1, prop);
150
151   ds_put_cstr (&str, " =");
152
153   psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (scd->var_view), 0, &str);
154
155   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->cutpoint_button)))
156     {
157       const gchar *cutpoint = gtk_entry_get_text (GTK_ENTRY (scd->cutpoint_entry));
158       ds_put_c_format  (&str, "(%s)", cutpoint);
159     }
160
161   ds_put_cstr (&str, ".\n");
162
163   text = ds_steal_cstr (&str);
164
165   ds_destroy (&str);
166
167   return text;
168 }
169
170 static void
171 psppire_dialog_action_binomial_class_init (PsppireDialogActionBinomialClass *class)
172 {
173   psppire_dialog_action_set_activation (class, psppire_dialog_action_binomial_activate);
174
175   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
176 }
177
178
179 static void
180 psppire_dialog_action_binomial_init (PsppireDialogActionBinomial *act)
181 {
182 }
183