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