1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011, 2012, 2013 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 "psppire-dialog-action-count.h"
22 #include "psppire-var-view.h"
24 #include "psppire-dialog.h"
25 #include "psppire-selector.h"
26 #include "builder-wrapper.h"
27 #include "psppire-acr.h"
28 #include "dialog-common.h"
30 #include <ui/syntax-gen.h>
32 #include "psppire-val-chooser.h"
36 static void values_dialog (PsppireDialogActionCount *cd);
39 static void psppire_dialog_action_count_init (PsppireDialogActionCount *act);
40 static void psppire_dialog_action_count_class_init (PsppireDialogActionCountClass *class);
42 G_DEFINE_TYPE (PsppireDialogActionCount, psppire_dialog_action_count, PSPPIRE_TYPE_DIALOG_ACTION);
46 generate_syntax (const PsppireDialogAction *act)
48 PsppireDialogActionCount *cnt = PSPPIRE_DIALOG_ACTION_COUNT (act);
50 const gchar *s = NULL;
57 ds_put_cstr (&dds, "\nCOUNT ");
59 ds_put_cstr (&dds, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
61 ds_put_cstr (&dds, " =");
63 psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (cnt->variable_treeview), 0, &dds);
65 ds_put_cstr (&dds, "(");
66 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cnt->value_list),
69 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (cnt->value_list), &iter))
74 gtk_tree_model_get_value (GTK_TREE_MODEL (cnt->value_list), &iter,
77 ov = g_value_get_boxed (&a_value);
79 ds_put_cstr (&dds, " ");
80 old_value_append_syntax (&dds, ov);
82 ds_put_cstr (&dds, ").");
85 s = gtk_entry_get_text (GTK_ENTRY (cnt->label));
86 if (0 != strcmp (s, ""))
88 ds_put_cstr (&dds, "\nVARIABLE LABELS ");
90 ds_put_cstr (&dds, gtk_entry_get_text (GTK_ENTRY (cnt->target)));
92 ds_put_cstr (&dds, " ");
94 syntax_gen_string (&dds, ss_cstr (s));
96 ds_put_cstr (&dds, ".");
99 ds_put_cstr (&dds, "\nEXECUTE.\n");
101 text = ds_steal_cstr (&dds);
110 dialog_state_valid (gpointer data)
112 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (data);
113 PsppireDialogActionCount *cnt = PSPPIRE_DIALOG_ACTION_COUNT (pda);
117 if (! cnt->value_list)
120 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (cnt->value_list), &iter))
123 if (!gtk_tree_model_get_iter_first (gtk_tree_view_get_model (GTK_TREE_VIEW (cnt->variable_treeview)), &iter))
126 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (cnt->target))))
133 refresh (PsppireDialogAction *rd_)
135 PsppireDialogActionCount *cnt = PSPPIRE_DIALOG_ACTION_COUNT (rd_);
138 gtk_tree_view_get_model (GTK_TREE_VIEW (cnt->variable_treeview));
140 gtk_list_store_clear (GTK_LIST_STORE (vars));
142 gtk_entry_set_text (GTK_ENTRY (cnt->target), "");
143 gtk_entry_set_text (GTK_ENTRY (cnt->label), "");
144 gtk_list_store_clear (GTK_LIST_STORE (cnt->value_list));
148 psppire_dialog_action_count_activate (PsppireDialogAction *a, GVariant *param)
150 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
151 PsppireDialogActionCount *act = PSPPIRE_DIALOG_ACTION_COUNT (a);
153 GtkBuilder *xml = builder_new ("count.ui");
155 GtkWidget *selector = get_widget_assert (xml, "count-selector1");
156 GtkWidget *button = get_widget_assert (xml, "button1");
158 pda->dialog = get_widget_assert (xml, "count-dialog");
159 pda->source = get_widget_assert (xml, "dict-view");
162 act->target = get_widget_assert (xml, "entry1");
163 act->label = get_widget_assert (xml, "entry2");
164 act->variable_treeview = get_widget_assert (xml, "treeview2");
166 act->value_list = gtk_list_store_new (1, old_value_get_type ());
168 psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), numeric_only);
170 g_signal_connect_swapped (button, "clicked", G_CALLBACK (values_dialog), act);
172 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
173 psppire_dialog_action_set_refresh (pda, refresh);
179 psppire_dialog_action_count_class_init (PsppireDialogActionCountClass *class)
181 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_count_activate;
182 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
187 psppire_dialog_action_count_init (PsppireDialogActionCount *act)
196 /* Callback which gets called when a new row is selected
197 in the acr's variable treeview.
198 We use if to set the togglebuttons and entries to correspond to the
202 on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
205 struct old_value *ov = NULL;
206 GtkTreeModel *model = NULL;
207 PsppireDialogActionCount *cnt = PSPPIRE_DIALOG_ACTION_COUNT (data);
208 GValue ov_value = {0};
210 if (! gtk_tree_selection_get_selected (selection, &model, &iter))
213 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
216 ov = g_value_get_boxed (&ov_value);
217 psppire_val_chooser_set_status (PSPPIRE_VAL_CHOOSER (cnt->chooser), ov);
221 /* A function to set a value in a column in the ACR */
223 set_value (gint col, GValue *val, gpointer data)
225 PsppireDialogActionCount *cnt = PSPPIRE_DIALOG_ACTION_COUNT (data);
226 PsppireValChooser *vc = PSPPIRE_VAL_CHOOSER (cnt->chooser);
231 psppire_val_chooser_get_status (vc, &ov);
233 g_value_init (val, old_value_get_type ());
234 g_value_set_boxed (val, &ov);
241 values_dialog (PsppireDialogActionCount *cd)
243 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (cd);
245 GtkListStore *local_store = clone_list_store (cd->value_list);
246 GtkBuilder *builder = builder_new ("count.ui");
248 GtkWidget *dialog = get_widget_assert (builder, "values-dialog");
250 GtkWidget *acr = get_widget_assert (builder, "acr");
251 cd->chooser = get_widget_assert (builder, "value-chooser");
253 psppire_acr_set_enabled (PSPPIRE_ACR (acr), TRUE);
255 psppire_acr_set_model (PSPPIRE_ACR (acr), local_store);
256 psppire_acr_set_get_value_func (PSPPIRE_ACR (acr), set_value, cd);
259 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (PSPPIRE_ACR(acr)->tv));
260 g_signal_connect (sel, "changed",
261 G_CALLBACK (on_acr_selection_change), cd);
264 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
266 if (response == PSPPIRE_RESPONSE_CONTINUE)
268 g_object_unref (cd->value_list);
269 cd->value_list = local_store;
273 g_object_unref (local_store);
276 psppire_dialog_notify_change (PSPPIRE_DIALOG (pda->dialog));
278 g_object_unref (builder);