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