ROC dialog: Disable reference button when curve is not drawn
[pspp-builds.git] / src / ui / gui / roc-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009  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 #include <config.h>
18
19 #include "dialog-common.h"
20 #include <language/syntax-string-source.h>
21 #include <ui/syntax-gen.h>
22 #include <libpspp/str.h>
23
24 #include "roc-dialog.h"
25 #include "psppire-selector.h"
26 #include "psppire-dictview.h"
27 #include "psppire-dialog.h"
28
29 #include "psppire-data-window.h"
30 #include "psppire-var-view.h"
31
32 #include "executor.h"
33 #include "helper.h"
34
35 #include <gtk/gtk.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41
42 struct roc
43 {
44   PsppireDict *dict;
45
46   GtkWidget *test_variables;
47   GtkWidget *state_variable;
48   GtkWidget *state_value;
49
50   GtkWidget *curve;
51   GtkWidget *reference;
52   GtkWidget *standard_error;
53   GtkWidget *coordinates;
54 };
55
56
57 static char * generate_syntax (const struct roc *rd);
58
59
60 static void
61 refresh (struct roc *rd)
62 {
63   GtkTreeModel *liststore =
64     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->test_variables));
65   gtk_list_store_clear (GTK_LIST_STORE (liststore));
66
67   gtk_entry_set_text (GTK_ENTRY (rd->state_variable), "");
68   gtk_entry_set_text (GTK_ENTRY (rd->state_value), "");
69
70   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->curve),          TRUE);
71   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->reference),      FALSE);
72   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->standard_error), FALSE);
73   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->coordinates),    FALSE);
74 }
75
76
77 static gboolean
78 dialog_state_valid (gpointer data)
79 {
80   struct roc *rd = data;
81   const gchar *text;
82
83   GtkTreeModel *liststore =
84     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->test_variables));
85
86   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 1)
87     return FALSE;
88
89   
90   text = gtk_entry_get_text (GTK_ENTRY (rd->state_variable));
91   if ( 0 == strcmp ("", text))
92     return FALSE;
93
94
95   text = gtk_entry_get_text (GTK_ENTRY (rd->state_value));
96   if ( 0 == strcmp ("", text))
97     return FALSE;
98
99
100   return TRUE;
101 }
102
103 static void
104 on_curve_button_toggle  (GtkCheckButton *curve, struct roc *rd)
105 {
106   if ( !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (curve)))
107     {
108       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->reference)))
109         g_object_set (rd->reference, "inconsistent", TRUE, NULL);
110       g_object_set (rd->reference, "sensitive", FALSE, NULL);
111     }
112   else 
113     {
114       g_object_set (rd->reference, "inconsistent", FALSE, NULL);
115       g_object_set (rd->reference, "sensitive", TRUE, NULL);
116     }
117 }
118
119
120 /* Pops up the Roc dialog box */
121 void
122 roc_dialog (GObject *o, gpointer data)
123 {
124   struct roc rd;
125   gint response;
126
127   GtkBuilder *xml = builder_new ("roc.ui");
128   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
129   PsppireVarStore *vs;
130
131   GtkWidget *dialog = get_widget_assert   (xml, "roc-dialog");
132   GtkWidget *source = get_widget_assert   (xml, "dict-view");
133
134   rd.test_variables    = get_widget_assert   (xml, "psppire-var-view1");
135   rd.state_variable    = get_widget_assert   (xml, "entry1");
136   rd.state_value       = get_widget_assert   (xml, "entry2");
137
138   rd.curve          = get_widget_assert   (xml, "curve");
139   rd.reference      = get_widget_assert   (xml, "reference-line");
140   rd.standard_error = get_widget_assert   (xml, "standard-error");
141   rd.coordinates    = get_widget_assert   (xml, "co-ordinates");
142
143
144   g_object_get (de->data_editor, "var-store", &vs, NULL);
145
146   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
147
148   g_object_get (vs, "dictionary", &rd.dict, NULL);
149   g_object_set (source, "model", rd.dict, NULL);
150
151   g_signal_connect (rd.curve, "toggled", G_CALLBACK (on_curve_button_toggle), &rd);
152
153   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &rd);
154
155   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
156                                       dialog_state_valid, &rd);
157
158   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
159
160   switch (response)
161     {
162     case GTK_RESPONSE_OK:
163       {
164         gchar *syntax = generate_syntax (&rd);
165
166         struct getl_interface *sss = create_syntax_string_source (syntax);
167         execute_syntax (sss);
168
169         g_free (syntax);
170       }
171       break;
172     case PSPPIRE_RESPONSE_PASTE:
173       {
174         gchar *syntax = generate_syntax (&rd);
175         paste_syntax_in_new_window (syntax);
176
177         g_free (syntax);
178       }
179       break;
180     default:
181       break;
182     }
183
184   g_object_unref (xml);
185 }
186
187
188 \f
189
190 static char *
191 generate_syntax (const struct roc *rd)
192 {
193   gchar *text;
194   const gchar *var_name = gtk_entry_get_text (GTK_ENTRY (rd->state_variable));
195   GString *string = g_string_new ("ROC");
196
197   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->test_variables), 0, string);
198
199   g_string_append (string, " BY ");
200
201   g_string_append (string, var_name);
202
203   g_string_append (string, " (");
204   {
205     const gchar *value = gtk_entry_get_text (GTK_ENTRY (rd->state_value));
206
207     const struct variable *var = psppire_dict_lookup_var (rd->dict, var_name);
208
209     g_return_val_if_fail (var, NULL);
210
211     if ( var_is_alpha (var))
212       {
213         struct string xx;
214         ds_init_empty (&xx);
215         syntax_gen_string (&xx, ss_cstr (value));
216         g_string_append (string, ds_cstr (&xx));
217         ds_destroy (&xx);
218       }
219     else
220       g_string_append (string, value);
221   }
222   g_string_append (string, ")");
223
224
225   /* The /PLOT subcommand */
226   g_string_append (string, "\n\t/PLOT ");
227   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->curve)))
228     {
229       g_string_append (string, "CURVE");
230       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->reference)))
231         g_string_append (string, " (REFERENCE)");
232     }
233   else
234     g_string_append (string, "NONE");
235
236
237   /* The /PRINT subcommand */
238   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->standard_error)) ||
239        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->coordinates)) )
240     {
241       g_string_append (string, "\n\t/PRINT");
242
243       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->standard_error)))
244         g_string_append (string, " SE");
245
246       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->coordinates)))
247         g_string_append (string, " COORDINATES");
248     }
249
250   g_string_append (string, ".\n");
251
252   text = string->str;
253
254   g_string_free (string, FALSE);
255
256   return text;
257 }