39a99dbd9d8ad2ece22839c5e3ce319d85b3c050
[pspp-builds.git] / src / ui / gui / count-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
18 #include <config.h>
19
20 #include "count-dialog.h"
21
22 #include <gtk/gtk.h>
23 #include "helper.h"
24 #include "psppire-dialog.h"
25 #include "psppire-selector.h"
26 #include "psppire-val-chooser.h"
27 #include "psppire-var-view.h"
28 #include "psppire-acr.h"
29 #include "dialog-common.h"
30
31 #include <ui/syntax-gen.h>
32 #include "executor.h"
33
34 struct cnt_dialog
35 {
36   PsppireDict *dict;
37
38   GtkWidget *dialog;
39
40   GtkListStore *value_list;
41   GtkWidget *chooser;
42
43   GtkWidget *target;
44   GtkWidget *label;
45   GtkWidget *variable_treeview;
46 };
47
48 /* Callback which gets called when a new row is selected
49    in the acr's variable treeview.
50    We use if to set the togglebuttons and entries to correspond to the
51    selected row.
52 */
53 static void
54 on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
55 {
56   GtkTreeIter iter;
57   struct old_value *ov = NULL;
58   GtkTreeModel *model = NULL;
59   struct cnt_dialog *cnt = data;
60   GValue ov_value = {0};
61
62   if ( ! gtk_tree_selection_get_selected (selection, &model, &iter) )
63     return;
64
65   gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
66                             0, &ov_value);
67
68   ov = g_value_get_boxed (&ov_value);
69   psppire_val_chooser_set_status (PSPPIRE_VAL_CHOOSER (cnt->chooser), ov);
70 }
71
72
73
74 static char * generate_syntax (const struct cnt_dialog *cnt);
75
76 static void values_dialog (struct cnt_dialog *cd);
77
78 static void
79 refresh (PsppireDialog *dialog, struct cnt_dialog *cnt)
80 {
81   GtkTreeModel *vars =
82     gtk_tree_view_get_model (GTK_TREE_VIEW (cnt->variable_treeview));
83
84   gtk_list_store_clear (GTK_LIST_STORE (vars));
85
86   gtk_entry_set_text (GTK_ENTRY (cnt->target), "");
87   gtk_entry_set_text (GTK_ENTRY (cnt->label), "");
88   gtk_list_store_clear (GTK_LIST_STORE (cnt->value_list));
89 }
90
91 static gboolean
92 dialog_state_valid (gpointer data)
93 {
94   GtkTreeIter iter;
95   struct cnt_dialog *cnt = data;
96
97   if (! cnt->value_list)
98     return FALSE;
99
100   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cnt->value_list), &iter) )
101     return FALSE;
102
103   if (!gtk_tree_model_get_iter_first (gtk_tree_view_get_model (GTK_TREE_VIEW (cnt->variable_treeview)), &iter))
104     return FALSE;
105
106   if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (cnt->target))))
107     return FALSE;
108
109   return TRUE;
110 }
111
112 void count_dialog (PsppireDataWindow *de)
113 {
114   gint response;
115   PsppireVarStore *vs = NULL;
116   struct cnt_dialog cnt;
117
118   GtkBuilder *builder = builder_new ("count.ui");
119
120   GtkWidget *selector = get_widget_assert (builder, "count-selector1");
121
122   GtkWidget *dict_view = get_widget_assert (builder, "dict-view");
123   GtkWidget *button = get_widget_assert (builder, "button1");
124
125   cnt.target = get_widget_assert (builder, "entry1");
126   cnt.label = get_widget_assert (builder, "entry2");
127   cnt.variable_treeview = get_widget_assert (builder, "treeview2");
128
129   g_signal_connect_swapped (button, "clicked", G_CALLBACK (values_dialog), &cnt);
130
131   cnt.value_list = gtk_list_store_new (1,old_value_get_type ());
132
133   cnt.dialog =  get_widget_assert (builder, "count-dialog");
134
135   g_signal_connect (cnt.dialog, "refresh", G_CALLBACK (refresh),  &cnt);
136
137
138   g_object_get (de->data_editor, "var-store", &vs, NULL);
139
140   g_object_get (vs, "dictionary", &cnt.dict, NULL);
141
142   gtk_window_set_transient_for (GTK_WINDOW (cnt.dialog), GTK_WINDOW (de));
143
144   g_object_set (dict_view, "model", cnt.dict, NULL);
145
146   psppire_selector_set_allow (PSPPIRE_SELECTOR (selector),  numeric_only);
147
148   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (cnt.dialog),
149                                       dialog_state_valid, &cnt);
150
151   response = psppire_dialog_run (PSPPIRE_DIALOG (cnt.dialog));
152
153   switch (response)
154     {
155     case GTK_RESPONSE_OK:
156       g_free (execute_syntax_string (de, generate_syntax (&cnt)));
157       break;
158     case PSPPIRE_RESPONSE_PASTE:
159       g_free (paste_syntax_to_window (generate_syntax (&cnt)));
160       break;
161     default:
162       break;
163     }
164
165
166   g_object_unref (builder);
167 }
168
169 /* A function to set a value in a column in the ACR */
170 static gboolean
171 set_value (gint col, GValue  *val, gpointer data)
172 {
173   struct cnt_dialog *cnt = data;
174   PsppireValChooser *vc = PSPPIRE_VAL_CHOOSER (cnt->chooser);
175   struct old_value ov;
176         
177   g_assert (col == 0);
178
179   psppire_val_chooser_get_status (vc, &ov);
180
181   g_value_init (val, old_value_get_type ());
182   g_value_set_boxed (val, &ov);
183
184   return TRUE;
185 }
186
187
188 static void
189 values_dialog (struct cnt_dialog *cd)
190 {
191   gint response;
192   GtkListStore *local_store = clone_list_store (cd->value_list);
193   GtkBuilder *builder = builder_new ("count.ui");
194
195   GtkWidget *dialog = get_widget_assert (builder, "values-dialog");
196
197   GtkWidget *acr = get_widget_assert (builder, "acr");
198   cd->chooser = get_widget_assert (builder, "value-chooser");
199
200   psppire_acr_set_enabled (PSPPIRE_ACR (acr), TRUE);
201
202   psppire_acr_set_model (PSPPIRE_ACR (acr), local_store);
203   psppire_acr_set_get_value_func (PSPPIRE_ACR (acr), set_value, cd);
204
205   {
206     GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (PSPPIRE_ACR(acr)->tv));
207     g_signal_connect (sel, "changed",
208                     G_CALLBACK (on_acr_selection_change), cd);
209   }
210
211   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
212
213   if ( response == PSPPIRE_RESPONSE_CONTINUE )
214     {
215       g_object_unref (cd->value_list);
216       cd->value_list = local_store;
217     }
218   else
219     {
220       g_object_unref (local_store);
221     }
222
223   psppire_dialog_notify_change (PSPPIRE_DIALOG (cd->dialog));
224
225   g_object_unref (builder);
226 }
227
228
229
230 static char *
231 generate_syntax (const struct cnt_dialog *cnt)
232 {
233   gchar *text = NULL;
234   const gchar *s = NULL;
235   gboolean ok;
236   GtkTreeIter iter;
237   GString *str = g_string_sized_new (100);
238
239   g_string_append (str, "\nCOUNT ");
240
241   g_string_append (str, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
242
243   g_string_append (str, " =");
244
245   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cnt->variable_treeview), 0, str);
246
247   g_string_append (str, "(");
248   for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cnt->value_list),
249                                            &iter);
250        ok;
251        ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (cnt->value_list), &iter))
252     {
253       GValue a_value = {0};
254       struct old_value *ov;
255
256       gtk_tree_model_get_value (GTK_TREE_MODEL (cnt->value_list), &iter,
257                                 0, &a_value);
258
259       ov = g_value_get_boxed (&a_value);
260
261       g_string_append (str, " ");
262       old_value_append_syntax (str, ov);
263     }
264   g_string_append (str, ").");
265
266
267   s = gtk_entry_get_text (GTK_ENTRY (cnt->label));
268   if (0 != strcmp (s, ""))
269   {
270     struct string ds;
271     ds_init_empty (&ds);
272     g_string_append (str, "\nVARIABLE LABELS ");
273
274     g_string_append (str, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
275
276     g_string_append (str, " ");
277
278     syntax_gen_string (&ds, ss_cstr (s));
279
280     g_string_append (str, ds_cstr (&ds));
281
282     g_string_append (str, ".");
283     ds_destroy (&ds);
284   }
285
286   g_string_append (str, "\nEXECUTE.\n");
287
288   text = str->str;
289
290   g_string_free (str, FALSE);
291
292   return text;
293 }