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