2e2193730310a12a41dc64e39eb932ee90887584
[pspp-builds.git] / src / ui / gui / runs-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 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 "runs-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 enum
42   {
43     CB_MEDIAN,
44     CB_MEAN,
45     CB_MODE,
46     CB_CUSTOM
47   };
48
49 struct runs
50 {
51   GtkBuilder *xml;
52   PsppireDict *dict;
53
54   GtkWidget *variables;
55   PsppireDataWindow *de ;
56
57   GtkWidget *entry;
58   GtkWidget *cb[4];
59 };
60
61 static char * generate_syntax (const struct runs *rd);
62
63 /* Makes widget W's sensitivity follow the active state of TOGGLE */
64 static void
65 sensitive_if_active (GtkToggleButton *toggle, GtkWidget *w)
66 {
67   gboolean active = gtk_toggle_button_get_active (toggle);
68
69   gtk_widget_set_sensitive (w, active);
70 }
71
72 static void
73 refresh (struct runs *fd)
74 {
75   int i;
76   GtkTreeModel *liststore =
77     gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
78   gtk_list_store_clear (GTK_LIST_STORE (liststore));
79
80   gtk_entry_set_text (GTK_ENTRY (fd->entry), "");
81
82   for (i = 0; i < 4; ++i)
83     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->cb[i]), FALSE);
84 }
85
86
87 static gboolean
88 dialog_state_valid (gpointer data)
89 {
90   int i;
91   struct runs *fd = data;
92
93   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
94
95   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 1)
96     return FALSE;
97
98   for (i = 0; i < 4; ++i)
99     {
100       if ( TRUE == gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[i])))
101         break;
102     }
103   if ( i >= 4)
104     return FALSE;
105
106
107   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[CB_CUSTOM])))
108     {
109       if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (fd->entry))))
110         return FALSE;
111     }
112
113   return TRUE;
114 }
115
116
117 /* Pops up the Runs dialog box */
118 void
119 runs_dialog (PsppireDataWindow *dw)
120 {
121   struct runs fd;
122   gint response;
123
124   PsppireVarStore *vs;
125
126   GtkWidget *dialog ;
127   GtkWidget *source ;
128
129   fd.xml = builder_new ("runs.ui");
130
131   dialog = get_widget_assert   (fd.xml, "runs-dialog");
132   source = get_widget_assert   (fd.xml, "dict-view");
133
134   fd.entry = get_widget_assert   (fd.xml, "entry1");
135   fd.cb[CB_MEDIAN] = get_widget_assert (fd.xml, "checkbutton1");
136   fd.cb[CB_MEAN] = get_widget_assert (fd.xml, "checkbutton2");
137   fd.cb[CB_MODE] = get_widget_assert (fd.xml, "checkbutton4");
138   fd.cb[CB_CUSTOM] = get_widget_assert (fd.xml, "checkbutton3");
139
140   fd.de = dw;
141
142   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &fd);
143
144
145   fd.variables = get_widget_assert   (fd.xml, "psppire-var-view1");
146
147   g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
148
149   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
150
151   g_object_get (vs, "dictionary", &fd.dict, NULL);
152   g_object_set (source, "model", fd.dict,
153                 "predicate", var_is_numeric,
154                 NULL);
155
156   g_signal_connect (fd.cb[CB_CUSTOM], "toggled",
157                     G_CALLBACK (sensitive_if_active), fd.entry);
158
159   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
160                                       dialog_state_valid, &fd);
161
162   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
163
164   switch (response)
165     {
166     case GTK_RESPONSE_OK:
167       g_free (execute_syntax_string (dw, generate_syntax (&fd)));
168       break;
169     case PSPPIRE_RESPONSE_PASTE:
170       g_free (paste_syntax_to_window (generate_syntax (&fd)));
171       break;
172     default:
173       break;
174     }
175
176   g_object_unref (fd.xml);
177 }
178
179
180 \f
181 static void
182 append_fragment (GString *string, const gchar *cut, PsppireVarView *vv)
183 {
184   g_string_append (string, "\n\t/RUNS");
185
186   g_string_append (string, " ( ");
187   g_string_append (string, cut);
188   g_string_append (string, " ) = ");
189
190   psppire_var_view_append_names (vv, 0, string);
191 }
192
193
194 char *
195 generate_syntax (const struct runs *rd)
196 {
197   gchar *text;
198
199   GString *string = g_string_new ("NPAR TEST");
200
201   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MEAN])))
202     append_fragment (string, "MEAN", PSPPIRE_VAR_VIEW (rd->variables));
203
204   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MEDIAN])))
205     append_fragment (string, "MEDIAN", PSPPIRE_VAR_VIEW (rd->variables));
206
207   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MODE])))
208     append_fragment (string, "MODE", PSPPIRE_VAR_VIEW (rd->variables));
209
210   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_CUSTOM])))
211     {
212       const char *text = gtk_entry_get_text (GTK_ENTRY (rd->entry));
213       append_fragment (string, text, PSPPIRE_VAR_VIEW (rd->variables));
214     }
215
216   g_string_append (string, ".\n");
217
218   text = string->str;
219
220   g_string_free (string, FALSE);
221
222   return text;
223 }