Refactoring: common function set_sensitivity from toggle
[pspp] / src / ui / gui / psppire-dialog-action-logistic.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 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
18 #include <config.h>
19
20 #include "psppire-dialog-action-logistic.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_logistic_class_init (PsppireDialogActionLogisticClass *class);
41
42 G_DEFINE_TYPE (PsppireDialogActionLogistic, psppire_dialog_action_logistic, PSPPIRE_TYPE_DIALOG_ACTION);
43
44 static gboolean
45 dialog_state_valid (gpointer data)
46 {
47   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (data);
48
49   const gchar *text = gtk_entry_get_text (GTK_ENTRY (rd->dep_var));
50
51   GtkTreeModel *indep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
52
53   GtkTreeIter notused;
54
55   return 0 != strcmp ("", text) &&
56     gtk_tree_model_get_iter_first (indep_vars, &notused);
57 }
58
59 static void
60 refresh (PsppireDialogAction *rd_)
61 {
62   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (rd_);
63
64   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
65   gtk_list_store_clear (GTK_LIST_STORE (liststore));
66
67   gtk_entry_set_text (GTK_ENTRY (rd->dep_var), "");
68 }
69
70
71 static void
72 on_opts_clicked (PsppireDialogActionLogistic *act)
73 {
74   int ret;
75
76   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->conf_checkbox), act->conf);
77   gtk_spin_button_set_value (GTK_SPIN_BUTTON (act->conf_entry), act->conf_level);
78   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->const_checkbox), act->constant);
79
80   gtk_spin_button_set_value (GTK_SPIN_BUTTON (act->cut_point_entry), act->cut_point);
81   gtk_spin_button_set_value (GTK_SPIN_BUTTON (act->iterations_entry), act->max_iterations);
82
83   
84   ret = psppire_dialog_run (PSPPIRE_DIALOG (act->opts_dialog));
85
86   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
87     {
88       act->conf = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(act->conf_checkbox));
89       act->conf_level = gtk_spin_button_get_value (GTK_SPIN_BUTTON (act->conf_entry));
90       
91       act->constant = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(act->const_checkbox));
92
93       act->cut_point = gtk_spin_button_get_value (GTK_SPIN_BUTTON (act->cut_point_entry));
94       act->max_iterations = gtk_spin_button_get_value (GTK_SPIN_BUTTON (act->iterations_entry));
95     }
96 }
97
98
99 static void
100 psppire_dialog_action_logistic_activate (GtkAction *a)
101 {
102   PsppireDialogActionLogistic *act = PSPPIRE_DIALOG_ACTION_LOGISTIC (a);
103   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
104   GtkWidget *opts_button;
105
106   GtkBuilder *xml = builder_new ("logistic.ui");
107
108   pda->dialog = get_widget_assert   (xml, "logistic-dialog");
109   pda->source = get_widget_assert   (xml, "dict-view");
110   act->cut_point = 0.5;
111   act->max_iterations = 20;
112   act->constant = true;
113   act->conf = false;
114   act->conf_level = 95;
115
116   act->dep_var  = get_widget_assert   (xml, "dependent-entry");
117   act->indep_vars  = get_widget_assert   (xml, "indep-view");
118   act->opts_dialog = get_widget_assert (xml, "options-dialog");
119   act->conf_checkbox = get_widget_assert (xml, "checkbutton2");
120   act->conf_entry = get_widget_assert (xml, "spinbutton1");
121   act->const_checkbox = get_widget_assert (xml, "checkbutton1");
122
123   act->iterations_entry = get_widget_assert (xml, "spinbutton3");
124   act->cut_point_entry = get_widget_assert (xml, "spinbutton2");
125
126   opts_button = get_widget_assert (xml, "options-button");
127
128   g_signal_connect_swapped (opts_button, "clicked",
129                             G_CALLBACK (on_opts_clicked),  act);
130
131   g_signal_connect (act->conf_checkbox, "toggled",
132                     G_CALLBACK (set_sensitivity_from_toggle),  
133                     act->conf_entry);
134
135   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->conf_checkbox), TRUE);
136   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->conf_checkbox), FALSE);
137
138   g_object_unref (xml);
139
140   psppire_dialog_action_set_refresh (pda, refresh);
141
142   psppire_dialog_action_set_valid_predicate (pda,
143                                         dialog_state_valid);
144
145   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_logistic_parent_class)->activate)
146     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_logistic_parent_class)->activate (pda);
147 }
148
149
150
151 static char *
152 generate_syntax (PsppireDialogAction *a)
153 {
154   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (a);
155   gchar *text = NULL;
156
157   GString *string = g_string_new ("LOGISTIC REGRESSION ");
158
159   const gchar *dep = gtk_entry_get_text (GTK_ENTRY (rd->dep_var));
160
161   g_string_append (string, dep);
162
163   g_string_append (string, " WITH ");
164
165   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
166
167   g_string_append (string, "\n\t/CRITERIA =");
168
169   g_string_append_printf (string, " CUT(%g)", rd->cut_point);
170   g_string_append_printf (string, " ITERATE(%d)", rd->max_iterations);
171
172   if (rd->conf)
173     {
174       g_string_append_printf (string, "\n\t/PRINT = CI(%g)", rd->conf_level);
175     }
176
177   if (rd->constant) 
178     g_string_append (string, "\n\t/NOORIGIN");
179   else
180     g_string_append (string, "\n\t/ORIGIN");
181
182   g_string_append (string, ".\n");
183
184   text = string->str;
185
186   g_string_free (string, FALSE);
187
188   return text;
189 }
190
191 static void
192 psppire_dialog_action_logistic_class_init (PsppireDialogActionLogisticClass *class)
193 {
194   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
195
196   action_class->activate = psppire_dialog_action_logistic_activate;
197
198   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
199 }
200
201
202 static void
203 psppire_dialog_action_logistic_init (PsppireDialogActionLogistic *act)
204 {
205 }
206