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 (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 (GtkAction *a)
150 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
151 PsppireDialogActionCount *act = PSPPIRE_DIALOG_ACTION_COUNT (a);
153 GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
154 GtkBuilder *xml = g_hash_table_lookup (thing, a);
157 xml = builder_new ("count.ui");
158 g_hash_table_insert (thing, a, xml);
161 GtkWidget *selector = get_widget_assert (xml, "count-selector1");
162 GtkWidget *button = get_widget_assert (xml, "button1");
164 pda->dialog = get_widget_assert (xml, "count-dialog");
165 pda->source = get_widget_assert (xml, "dict-view");
168 act->target = get_widget_assert (xml, "entry1");
169 act->label = get_widget_assert (xml, "entry2");
170 act->variable_treeview = get_widget_assert (xml, "treeview2");
172 act->value_list = gtk_list_store_new (1, old_value_get_type ());
174 psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), numeric_only);
176 g_signal_connect_swapped (button, "clicked", G_CALLBACK (values_dialog), act);
178 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
179 psppire_dialog_action_set_refresh (pda, refresh);
181 if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_count_parent_class)->activate)
182 PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_count_parent_class)->activate (pda);
186 psppire_dialog_action_count_class_init (PsppireDialogActionCountClass *class)
188 psppire_dialog_action_set_activation (class,psppire_dialog_action_count_activate);
189 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
194 psppire_dialog_action_count_init (PsppireDialogActionCount *act)
203 /* Callback which gets called when a new row is selected
204 in the acr's variable treeview.
205 We use if to set the togglebuttons and entries to correspond to the
209 on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
212 struct old_value *ov = NULL;
213 GtkTreeModel *model = NULL;
214 PsppireDialogActionCount *cnt = PSPPIRE_DIALOG_ACTION_COUNT (data);
215 GValue ov_value = {0};
217 if ( ! gtk_tree_selection_get_selected (selection, &model, &iter) )
220 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
223 ov = g_value_get_boxed (&ov_value);
224 psppire_val_chooser_set_status (PSPPIRE_VAL_CHOOSER (cnt->chooser), ov);
228 /* A function to set a value in a column in the ACR */
230 set_value (gint col, GValue *val, gpointer data)
232 PsppireDialogActionCount *cnt = PSPPIRE_DIALOG_ACTION_COUNT (data);
233 PsppireValChooser *vc = PSPPIRE_VAL_CHOOSER (cnt->chooser);
238 psppire_val_chooser_get_status (vc, &ov);
240 g_value_init (val, old_value_get_type ());
241 g_value_set_boxed (val, &ov);
248 values_dialog (PsppireDialogActionCount *cd)
250 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (cd);
252 GtkListStore *local_store = clone_list_store (cd->value_list);
253 GtkBuilder *builder = builder_new ("count.ui");
255 GtkWidget *dialog = get_widget_assert (builder, "values-dialog");
257 GtkWidget *acr = get_widget_assert (builder, "acr");
258 cd->chooser = get_widget_assert (builder, "value-chooser");
260 psppire_acr_set_enabled (PSPPIRE_ACR (acr), TRUE);
262 psppire_acr_set_model (PSPPIRE_ACR (acr), local_store);
263 psppire_acr_set_get_value_func (PSPPIRE_ACR (acr), set_value, cd);
266 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (PSPPIRE_ACR(acr)->tv));
267 g_signal_connect (sel, "changed",
268 G_CALLBACK (on_acr_selection_change), cd);
271 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
273 if ( response == PSPPIRE_RESPONSE_CONTINUE )
275 g_object_unref (cd->value_list);
276 cd->value_list = local_store;
280 g_object_unref (local_store);
283 psppire_dialog_notify_change (PSPPIRE_DIALOG (pda->dialog));
285 g_object_unref (builder);