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