1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011 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"
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"
31 #include <ui/syntax-gen.h>
40 GtkListStore *value_list;
45 GtkWidget *variable_treeview;
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
54 on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
57 struct old_value *ov = NULL;
58 GtkTreeModel *model = NULL;
59 struct cnt_dialog *cnt = data;
60 GValue ov_value = {0};
62 if ( ! gtk_tree_selection_get_selected (selection, &model, &iter) )
65 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
68 ov = g_value_get_boxed (&ov_value);
69 psppire_val_chooser_set_status (PSPPIRE_VAL_CHOOSER (cnt->chooser), ov);
74 static char * generate_syntax (const struct cnt_dialog *cnt);
76 static void values_dialog (struct cnt_dialog *cd);
79 refresh (PsppireDialog *dialog, struct cnt_dialog *cnt)
82 gtk_tree_view_get_model (GTK_TREE_VIEW (cnt->variable_treeview));
84 gtk_list_store_clear (GTK_LIST_STORE (vars));
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));
92 dialog_state_valid (gpointer data)
95 struct cnt_dialog *cnt = data;
97 if (! cnt->value_list)
100 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cnt->value_list), &iter) )
103 if (!gtk_tree_model_get_iter_first (gtk_tree_view_get_model (GTK_TREE_VIEW (cnt->variable_treeview)), &iter))
106 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (cnt->target))))
112 void count_dialog (PsppireDataWindow *de)
115 PsppireVarStore *vs = NULL;
116 struct cnt_dialog cnt;
118 GtkBuilder *builder = builder_new ("count.ui");
120 GtkWidget *selector = get_widget_assert (builder, "count-selector1");
122 GtkWidget *dict_view = get_widget_assert (builder, "dict-view");
123 GtkWidget *button = get_widget_assert (builder, "button1");
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");
129 g_signal_connect_swapped (button, "clicked", G_CALLBACK (values_dialog), &cnt);
131 cnt.value_list = gtk_list_store_new (1,old_value_get_type ());
133 cnt.dialog = get_widget_assert (builder, "count-dialog");
135 g_signal_connect (cnt.dialog, "refresh", G_CALLBACK (refresh), &cnt);
138 g_object_get (de->data_editor, "var-store", &vs, NULL);
140 g_object_get (vs, "dictionary", &cnt.dict, NULL);
142 gtk_window_set_transient_for (GTK_WINDOW (cnt.dialog), GTK_WINDOW (de));
144 g_object_set (dict_view, "model", cnt.dict, NULL);
146 psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), numeric_only);
148 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (cnt.dialog),
149 dialog_state_valid, &cnt);
151 response = psppire_dialog_run (PSPPIRE_DIALOG (cnt.dialog));
155 case GTK_RESPONSE_OK:
156 g_free (execute_syntax_string (de, generate_syntax (&cnt)));
158 case PSPPIRE_RESPONSE_PASTE:
159 g_free (paste_syntax_to_window (generate_syntax (&cnt)));
166 g_object_unref (builder);
169 /* A function to set a value in a column in the ACR */
171 set_value (gint col, GValue *val, gpointer data)
173 struct cnt_dialog *cnt = data;
174 PsppireValChooser *vc = PSPPIRE_VAL_CHOOSER (cnt->chooser);
179 psppire_val_chooser_get_status (vc, &ov);
181 g_value_init (val, old_value_get_type ());
182 g_value_set_boxed (val, &ov);
189 values_dialog (struct cnt_dialog *cd)
192 GtkListStore *local_store = clone_list_store (cd->value_list);
193 GtkBuilder *builder = builder_new ("count.ui");
195 GtkWidget *dialog = get_widget_assert (builder, "values-dialog");
197 GtkWidget *acr = get_widget_assert (builder, "acr");
198 cd->chooser = get_widget_assert (builder, "value-chooser");
200 psppire_acr_set_enabled (PSPPIRE_ACR (acr), TRUE);
202 psppire_acr_set_model (PSPPIRE_ACR (acr), local_store);
203 psppire_acr_set_get_value_func (PSPPIRE_ACR (acr), set_value, cd);
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);
211 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
213 if ( response == PSPPIRE_RESPONSE_CONTINUE )
215 g_object_unref (cd->value_list);
216 cd->value_list = local_store;
220 g_object_unref (local_store);
223 psppire_dialog_notify_change (PSPPIRE_DIALOG (cd->dialog));
225 g_object_unref (builder);
231 generate_syntax (const struct cnt_dialog *cnt)
234 const gchar *s = NULL;
237 GString *str = g_string_sized_new (100);
239 g_string_append (str, "\nCOUNT ");
241 g_string_append (str, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
243 g_string_append (str, " =");
245 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cnt->variable_treeview), 0, str);
247 g_string_append (str, "(");
248 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cnt->value_list),
251 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (cnt->value_list), &iter))
253 GValue a_value = {0};
254 struct old_value *ov;
256 gtk_tree_model_get_value (GTK_TREE_MODEL (cnt->value_list), &iter,
259 ov = g_value_get_boxed (&a_value);
261 g_string_append (str, " ");
262 old_value_append_syntax (str, ov);
264 g_string_append (str, ").");
267 s = gtk_entry_get_text (GTK_ENTRY (cnt->label));
268 if (0 != strcmp (s, ""))
272 g_string_append (str, "\nVARIABLE LABELS ");
274 g_string_append (str, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
276 g_string_append (str, " ");
278 syntax_gen_string (&ds, ss_cstr (s));
280 g_string_append (str, ds_cstr (&ds));
282 g_string_append (str, ".");
286 g_string_append (str, "\nEXECUTE.\n");
290 g_string_free (str, FALSE);