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