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 struct cnt_dialog cnt;
119 GtkBuilder *builder = builder_new ("count.ui");
121 GtkWidget *selector = get_widget_assert (builder, "count-selector1");
123 GtkWidget *dict_view = get_widget_assert (builder, "dict-view");
124 GtkWidget *button = get_widget_assert (builder, "button1");
126 cnt.target = get_widget_assert (builder, "entry1");
127 cnt.label = get_widget_assert (builder, "entry2");
128 cnt.variable_treeview = get_widget_assert (builder, "treeview2");
130 g_signal_connect_swapped (button, "clicked", G_CALLBACK (values_dialog), &cnt);
132 cnt.value_list = gtk_list_store_new (1,old_value_get_type ());
134 cnt.dialog = get_widget_assert (builder, "count-dialog");
136 g_signal_connect (cnt.dialog, "refresh", G_CALLBACK (refresh), &cnt);
139 g_object_get (de->data_editor, "dictionary", &cnt.dict, NULL);
141 gtk_window_set_transient_for (GTK_WINDOW (cnt.dialog), GTK_WINDOW (de));
143 g_object_set (dict_view, "model", cnt.dict, NULL);
145 psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), numeric_only);
147 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (cnt.dialog),
148 dialog_state_valid, &cnt);
150 response = psppire_dialog_run (PSPPIRE_DIALOG (cnt.dialog));
154 case GTK_RESPONSE_OK:
155 g_free (execute_syntax_string (de, generate_syntax (&cnt)));
157 case PSPPIRE_RESPONSE_PASTE:
158 g_free (paste_syntax_to_window (generate_syntax (&cnt)));
165 g_object_unref (cnt.value_list);
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;
239 ds_init_empty (&dds);
241 ds_put_cstr (&dds, "\nCOUNT ");
243 ds_put_cstr (&dds, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
245 ds_put_cstr (&dds, " =");
247 psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (cnt->variable_treeview), 0, &dds);
249 ds_put_cstr (&dds, "(");
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 ds_put_cstr (&dds, " ");
264 old_value_append_syntax (&dds, ov);
266 ds_put_cstr (&dds, ").");
269 s = gtk_entry_get_text (GTK_ENTRY (cnt->label));
270 if (0 != strcmp (s, ""))
272 ds_put_cstr (&dds, "\nVARIABLE LABELS ");
274 ds_put_cstr (&dds, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
276 ds_put_cstr (&dds, " ");
278 syntax_gen_string (&dds, ss_cstr (s));
280 ds_put_cstr (&dds, ".");
283 ds_put_cstr (&dds, "\nEXECUTE.\n");
285 text = ds_steal_cstr (&dds);