1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011, 2012 Free Software Foundation
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.
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.
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/>. */
20 #include "count-dialog.h"
23 #include "builder-wrapper.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"
32 #include <ui/syntax-gen.h>
42 GtkListStore *value_list;
47 GtkWidget *variable_treeview;
50 /* Callback which gets called when a new row is selected
51 in the acr's variable treeview.
52 We use if to set the togglebuttons and entries to correspond to the
56 on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
59 struct old_value *ov = NULL;
60 GtkTreeModel *model = NULL;
61 struct cnt_dialog *cnt = data;
62 GValue ov_value = {0};
64 if ( ! gtk_tree_selection_get_selected (selection, &model, &iter) )
67 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
70 ov = g_value_get_boxed (&ov_value);
71 psppire_val_chooser_set_status (PSPPIRE_VAL_CHOOSER (cnt->chooser), ov);
76 static char * generate_syntax (const struct cnt_dialog *cnt);
78 static void values_dialog (struct cnt_dialog *cd);
81 refresh (PsppireDialog *dialog, struct cnt_dialog *cnt)
84 gtk_tree_view_get_model (GTK_TREE_VIEW (cnt->variable_treeview));
86 gtk_list_store_clear (GTK_LIST_STORE (vars));
88 gtk_entry_set_text (GTK_ENTRY (cnt->target), "");
89 gtk_entry_set_text (GTK_ENTRY (cnt->label), "");
90 gtk_list_store_clear (GTK_LIST_STORE (cnt->value_list));
94 dialog_state_valid (gpointer data)
97 struct cnt_dialog *cnt = data;
99 if (! cnt->value_list)
102 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cnt->value_list), &iter) )
105 if (!gtk_tree_model_get_iter_first (gtk_tree_view_get_model (GTK_TREE_VIEW (cnt->variable_treeview)), &iter))
108 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (cnt->target))))
114 void count_dialog (PsppireDataWindow *de)
117 PsppireVarStore *vs = NULL;
118 struct cnt_dialog cnt;
120 GtkBuilder *builder = builder_new ("count.ui");
122 GtkWidget *selector = get_widget_assert (builder, "count-selector1");
124 GtkWidget *dict_view = get_widget_assert (builder, "dict-view");
125 GtkWidget *button = get_widget_assert (builder, "button1");
127 cnt.target = get_widget_assert (builder, "entry1");
128 cnt.label = get_widget_assert (builder, "entry2");
129 cnt.variable_treeview = get_widget_assert (builder, "treeview2");
131 g_signal_connect_swapped (button, "clicked", G_CALLBACK (values_dialog), &cnt);
133 cnt.value_list = gtk_list_store_new (1,old_value_get_type ());
135 cnt.dialog = get_widget_assert (builder, "count-dialog");
137 g_signal_connect (cnt.dialog, "refresh", G_CALLBACK (refresh), &cnt);
140 g_object_get (de->data_editor, "var-store", &vs, NULL);
142 g_object_get (vs, "dictionary", &cnt.dict, NULL);
144 gtk_window_set_transient_for (GTK_WINDOW (cnt.dialog), GTK_WINDOW (de));
146 g_object_set (dict_view, "model", cnt.dict, NULL);
148 psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), numeric_only);
150 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (cnt.dialog),
151 dialog_state_valid, &cnt);
153 response = psppire_dialog_run (PSPPIRE_DIALOG (cnt.dialog));
157 case GTK_RESPONSE_OK:
158 g_free (execute_syntax_string (de, generate_syntax (&cnt)));
160 case PSPPIRE_RESPONSE_PASTE:
161 g_free (paste_syntax_to_window (generate_syntax (&cnt)));
168 g_object_unref (builder);
171 /* A function to set a value in a column in the ACR */
173 set_value (gint col, GValue *val, gpointer data)
175 struct cnt_dialog *cnt = data;
176 PsppireValChooser *vc = PSPPIRE_VAL_CHOOSER (cnt->chooser);
181 psppire_val_chooser_get_status (vc, &ov);
183 g_value_init (val, old_value_get_type ());
184 g_value_set_boxed (val, &ov);
191 values_dialog (struct cnt_dialog *cd)
194 GtkListStore *local_store = clone_list_store (cd->value_list);
195 GtkBuilder *builder = builder_new ("count.ui");
197 GtkWidget *dialog = get_widget_assert (builder, "values-dialog");
199 GtkWidget *acr = get_widget_assert (builder, "acr");
200 cd->chooser = get_widget_assert (builder, "value-chooser");
202 psppire_acr_set_enabled (PSPPIRE_ACR (acr), TRUE);
204 psppire_acr_set_model (PSPPIRE_ACR (acr), local_store);
205 psppire_acr_set_get_value_func (PSPPIRE_ACR (acr), set_value, cd);
208 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (PSPPIRE_ACR(acr)->tv));
209 g_signal_connect (sel, "changed",
210 G_CALLBACK (on_acr_selection_change), cd);
213 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
215 if ( response == PSPPIRE_RESPONSE_CONTINUE )
217 g_object_unref (cd->value_list);
218 cd->value_list = local_store;
222 g_object_unref (local_store);
225 psppire_dialog_notify_change (PSPPIRE_DIALOG (cd->dialog));
227 g_object_unref (builder);
233 generate_syntax (const struct cnt_dialog *cnt)
236 const gchar *s = NULL;
239 GString *str = g_string_sized_new (100);
241 g_string_append (str, "\nCOUNT ");
243 g_string_append (str, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
245 g_string_append (str, " =");
247 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cnt->variable_treeview), 0, str);
249 g_string_append (str, "(");
250 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cnt->value_list),
253 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (cnt->value_list), &iter))
255 GValue a_value = {0};
256 struct old_value *ov;
258 gtk_tree_model_get_value (GTK_TREE_MODEL (cnt->value_list), &iter,
261 ov = g_value_get_boxed (&a_value);
263 g_string_append (str, " ");
264 old_value_append_syntax (str, ov);
266 g_string_append (str, ").");
269 s = gtk_entry_get_text (GTK_ENTRY (cnt->label));
270 if (0 != strcmp (s, ""))
274 g_string_append (str, "\nVARIABLE LABELS ");
276 g_string_append (str, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
278 g_string_append (str, " ");
280 syntax_gen_string (&ds, ss_cstr (s));
282 g_string_append (str, ds_cstr (&ds));
284 g_string_append (str, ".");
288 g_string_append (str, "\nEXECUTE.\n");
292 g_string_free (str, FALSE);