psppire-dialog-action: Remove unused variable.
[pspp] / src / ui / gui / psppire-dialog-action-roc.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-roc.h"
21 #include "psppire-value-entry.h"
22
23 #include "dialog-common.h"
24 #include <ui/syntax-gen.h>
25 #include "psppire-var-view.h"
26
27 #include "psppire-dialog.h"
28 #include "builder-wrapper.h"
29
30 #include "psppire-dict.h"
31 #include "libpspp/str.h"
32
33 static void
34 psppire_dialog_action_roc_class_init (PsppireDialogActionRocClass *class);
35
36 G_DEFINE_TYPE (PsppireDialogActionRoc, psppire_dialog_action_roc, PSPPIRE_TYPE_DIALOG_ACTION);
37
38 static gboolean
39 dialog_state_valid (gpointer data)
40 {
41   int width;
42   gboolean result ;
43   union value val;
44   PsppireDialogActionRoc *rd = data;
45   const gchar *var_name;
46   const struct variable *var;
47
48   GtkTreeModel *liststore =
49     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->test_variables));
50
51   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 1)
52     return FALSE;
53
54   var_name = gtk_entry_get_text (GTK_ENTRY (rd->state_variable));
55
56   var = psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION (rd)->dict, var_name);
57
58   if ( var == NULL)
59     return FALSE;
60
61   width = var_get_width (var);
62   value_init (&val, width);
63
64   result = psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (rd->state_value), &val, width);
65   
66   if (var_is_value_missing (var, &val, MV_SYSTEM))
67       result = FALSE;
68   
69   value_destroy (&val, width);
70
71   return result;
72 }
73
74 static void
75 on_curve_button_toggle (GtkCheckButton *curve, PsppireDialogActionRoc *rd)
76 {
77   if ( !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (curve)))
78     {
79       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->reference)))
80         g_object_set (rd->reference, "inconsistent", TRUE, NULL);
81       g_object_set (rd->reference, "sensitive", FALSE, NULL);
82     }
83   else 
84     {
85       g_object_set (rd->reference, "inconsistent", FALSE, NULL);
86       g_object_set (rd->reference, "sensitive", TRUE, NULL);
87     }
88 }
89
90 static void
91 refresh (PsppireDialogAction *rd_)
92 {
93   PsppireDialogActionRoc *rd = PSPPIRE_DIALOG_ACTION_ROC (rd_);
94   GtkTreeModel *liststore =
95     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->test_variables));
96   gtk_list_store_clear (GTK_LIST_STORE (liststore));
97
98   gtk_entry_set_text (GTK_ENTRY (rd->state_variable), "");
99   psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (rd->state_value), NULL);
100
101   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->curve),          TRUE);
102   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->reference),      FALSE);
103   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->standard_error), FALSE);
104   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->coordinates),    FALSE);
105 }
106
107 static void
108 on_state_var_changed (GtkAction *a)
109 {
110   PsppireDialogActionRoc *act = PSPPIRE_DIALOG_ACTION_ROC (a);
111
112   const gchar *var_name = gtk_entry_get_text (GTK_ENTRY(act->state_variable));
113
114   const struct variable *var =
115     psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION(act)->dict, var_name);
116
117   if ( var == NULL)
118     return;
119
120   psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->state_value), var);
121 }
122
123 static void
124 psppire_dialog_action_roc_activate (GtkAction *a)
125 {
126   PsppireDialogActionRoc *act = PSPPIRE_DIALOG_ACTION_ROC (a);
127   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
128
129   GtkBuilder *xml = builder_new ("roc.ui");
130   pda->dialog = get_widget_assert   (xml, "roc-dialog");
131   pda->source = get_widget_assert   (xml, "dict-view");
132
133   act->test_variables    = get_widget_assert   (xml, "psppire-var-view1");
134   act->state_variable    = get_widget_assert   (xml, "entry1");
135   act->state_value       = get_widget_assert   (xml, "entry2");
136
137   act->curve          = get_widget_assert   (xml, "curve");
138   act->reference      = get_widget_assert   (xml, "reference-line");
139   act->standard_error = get_widget_assert   (xml, "standard-error");
140   act->coordinates    = get_widget_assert   (xml, "co-ordinates");
141
142   g_signal_connect_swapped (act->state_variable, "changed",
143                             G_CALLBACK (on_state_var_changed), act);
144
145   g_object_unref (xml);
146
147   g_signal_connect (act->curve, "toggled",
148                     G_CALLBACK (on_curve_button_toggle), act);
149
150   psppire_dialog_action_set_refresh (pda, refresh);
151
152   psppire_dialog_action_set_valid_predicate (pda,
153                                         dialog_state_valid);
154
155   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_roc_parent_class)->activate)
156     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_roc_parent_class)->activate (pda);
157 }
158
159
160
161 static char *
162 generate_syntax (PsppireDialogAction *a)
163 {
164   PsppireDialogActionRoc *rd = PSPPIRE_DIALOG_ACTION_ROC (a);
165   gchar *text;
166   const gchar *var_name = gtk_entry_get_text (GTK_ENTRY (rd->state_variable));
167   GString *string = g_string_new ("ROC");
168
169   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->test_variables), 0, string);
170
171   g_string_append (string, " BY ");
172
173   g_string_append (string, var_name);
174
175   g_string_append (string, " (");
176   {
177     const struct variable *var =
178       psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION(rd)->dict, var_name);
179
180     union value val;
181     value_init (&val, var_get_width (var));
182
183     psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (rd->state_value),
184                                    &val, var_get_width (var));
185
186     g_return_val_if_fail (var, NULL);
187
188     {
189       struct string str;
190       ds_init_empty (&str);
191       
192       syntax_gen_value (&str, &val, var_get_width (var),
193                         var_get_print_format (var));
194       
195       g_string_append (string, ds_cstr (&str));
196       ds_destroy (&str);
197     }
198     value_destroy (&val, var_get_width (var));
199   }
200
201   g_string_append (string, ")");
202
203
204   /* The /PLOT subcommand */
205   g_string_append (string, "\n\t/PLOT ");
206   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->curve)))
207     {
208       g_string_append (string, "CURVE");
209       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->reference)))
210         g_string_append (string, " (REFERENCE)");
211     }
212   else
213     g_string_append (string, "NONE");
214
215
216   /* The /PRINT subcommand */
217   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->standard_error)) ||
218        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->coordinates)) )
219     {
220       g_string_append (string, "\n\t/PRINT");
221
222       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->standard_error)))
223         g_string_append (string, " SE");
224
225       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->coordinates)))
226         g_string_append (string, " COORDINATES");
227     }
228
229   g_string_append (string, ".\n");
230
231   text = string->str;
232
233   g_string_free (string, FALSE);
234
235   return text;
236 }
237
238 static void
239 psppire_dialog_action_roc_class_init (PsppireDialogActionRocClass *class)
240 {
241   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
242
243   action_class->activate = psppire_dialog_action_roc_activate;
244
245   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
246 }
247
248
249 static void
250 psppire_dialog_action_roc_init (PsppireDialogActionRoc *act)
251 {
252 }
253