1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 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 "psppire-dialog-action-crosstabs.h"
21 #include "psppire-value-entry.h"
23 #include "dialog-common.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
28 #include "psppire-dialog.h"
29 #include "builder-wrapper.h"
30 #include "psppire-checkbox-treeview.h"
31 #include "psppire-dict.h"
32 #include "libpspp/str.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
40 psppire_dialog_action_crosstabs_class_init (PsppireDialogActionCrosstabsClass *class);
42 G_DEFINE_TYPE (PsppireDialogActionCrosstabs, psppire_dialog_action_crosstabs, PSPPIRE_TYPE_DIALOG_ACTION);
45 dialog_state_valid (gpointer data)
47 PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (data);
49 GtkTreeModel *row_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_rows));
50 GtkTreeModel *col_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_cols));
54 return (gtk_tree_model_get_iter_first (row_vars, ¬used)
55 && gtk_tree_model_get_iter_first (col_vars, ¬used));
59 refresh (PsppireDialogAction *rd_)
61 PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (rd_);
63 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_rows));
64 gtk_list_store_clear (GTK_LIST_STORE (liststore));
66 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_cols));
67 gtk_list_store_clear (GTK_LIST_STORE (liststore));
70 #define CROSSTABS_STATS \
71 CS (CHISQ, N_("Chisq")) \
74 CS (LAMBDA, N_("Lambda")) \
76 CS (BTAU, N_("BTau")) \
77 CS (CTAU, N_("CTau")) \
78 CS (RISK, N_("Risk")) \
79 CS (GAMMA, N_("Gamma")) \
81 CS (KAPPA, N_("Kappa")) \
83 CS (CORR, N_("Corr")) \
84 CS (STATS_NONE, N_("None"))
87 #define CROSSTABS_CELLS \
88 CS (COUNT, N_("Count")) \
90 CS (COLUMN, N_("Column")) \
91 CS (TOTAL, N_("Total")) \
92 CS (EXPECTED, N_("Expected")) \
93 CS (RESIDUAL, N_("Residual")) \
94 CS (SRESIDUAL, N_("Std. Residual")) \
95 CS (ASRESIDUAL, N_("Adjusted Std. Residual")) \
96 CS (CELLS_NONE, N_("None"))
100 #define CS(NAME, LABEL) CS_##NAME,
108 #define CS(NAME, LABEL) CS_##NAME,
116 #define CS(NAME, LABEL) B_CS_##NAME = 1u << CS_##NAME,
120 B_CS_STATS_ALL = (1u << N_CROSSTABS_STATS) - 1,
121 B_CS_CELLS_ALL = (1u << N_CROSSTABS_CELLS) - 1,
122 B_CS_STATS_DEFAULT = B_CS_CHISQ,
123 B_CS_CELL_DEFAULT = B_CS_COUNT | B_CS_ROW | B_CS_COLUMN | B_CS_TOTAL,
127 static const struct checkbox_entry_item stats[] =
129 #define CS(NAME, LABEL) {#NAME, LABEL},
135 static const struct checkbox_entry_item cells[] =
137 #define CS(NAME, LABEL) {#NAME, LABEL},
144 on_format_clicked (PsppireDialogActionCrosstabs *cd)
148 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cd->avalue_button), cd->format_options_avalue);
149 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cd->table_button), cd->format_options_table);
150 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cd->pivot_button), cd->format_options_pivot);
152 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->format_dialog));
154 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
156 cd->format_options_avalue =
157 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->avalue_button));
159 cd->format_options_table =
160 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->table_button));
162 cd->format_options_pivot =
163 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->pivot_button));
168 on_cell_clicked (PsppireDialogActionCrosstabs *cd)
170 GtkListStore *liststore = clone_list_store (GTK_LIST_STORE (cd->cell));
172 gint ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->cell_dialog));
174 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
176 g_object_unref (liststore);
180 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->cell_view) , GTK_TREE_MODEL (liststore));
181 cd->cell = GTK_TREE_MODEL (liststore);
187 on_statistics_clicked (PsppireDialogActionCrosstabs *cd)
189 GtkListStore *liststore = clone_list_store (GTK_LIST_STORE (cd->stat));
191 gint ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->stat_dialog));
193 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
195 g_object_unref (liststore);
199 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->stat_view) , GTK_TREE_MODEL (liststore));
200 cd->stat = GTK_TREE_MODEL (liststore);
206 psppire_dialog_action_crosstabs_activate (GtkAction *a)
208 PsppireDialogActionCrosstabs *act = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
209 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
211 GHashTable *thing = psppire_dialog_action_get_pointer (pda);
212 GtkBuilder *xml = g_hash_table_lookup (thing, a);
215 xml = builder_new ("crosstabs.ui");
216 g_hash_table_insert (thing, a, xml);
219 pda->dialog = get_widget_assert (xml, "crosstabs-dialog");
220 pda->source = get_widget_assert (xml, "dict-treeview");
222 act->dest_rows = get_widget_assert (xml, "rows");
223 act->dest_cols = get_widget_assert (xml, "cols");
224 act->format_button = get_widget_assert (xml, "format-button");
225 act->stat_button = get_widget_assert (xml, "stats-button");
226 act->cell_button = get_widget_assert (xml, "cell-button");
227 act->stat_view = get_widget_assert (xml, "stats-view");
228 act->cell_view = get_widget_assert (xml, "cell-view");
229 act->cell_dialog = get_widget_assert (xml, "cell-dialog");
230 act->stat_dialog = get_widget_assert (xml, "stat-dialog");
231 act->format_dialog = get_widget_assert (xml, "format-dialog");
233 act->avalue_button = get_widget_assert (xml, "ascending");
234 act->table_button = get_widget_assert (xml, "print-tables");
235 act->pivot_button = get_widget_assert (xml, "pivot");
237 act->format_options_avalue = TRUE;
238 act->format_options_table = TRUE;
239 act->format_options_pivot = TRUE;
241 psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->cell_view),
246 act->cell = gtk_tree_view_get_model (GTK_TREE_VIEW (act->cell_view));
248 psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
253 act->stat = gtk_tree_view_get_model (GTK_TREE_VIEW (act->stat_view));
255 psppire_dialog_action_set_refresh (pda, refresh);
257 psppire_dialog_action_set_valid_predicate (pda,
260 g_signal_connect_swapped (act->cell_button, "clicked",
261 G_CALLBACK (on_cell_clicked), act);
263 g_signal_connect_swapped (act->stat_button, "clicked",
264 G_CALLBACK (on_statistics_clicked), act);
266 g_signal_connect_swapped (act->format_button, "clicked",
267 G_CALLBACK (on_format_clicked), act);
270 if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_crosstabs_parent_class)->activate)
271 PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_crosstabs_parent_class)->activate (pda);
277 generate_syntax (PsppireDialogAction *a)
279 PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
283 GString *string = g_string_new ("CROSSTABS ");
287 g_string_append (string, "\n\t/TABLES=");
288 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->dest_rows), 0, string);
289 g_string_append (string, "\tBY\t");
290 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->dest_cols), 0, string);
293 g_string_append (string, "\n\t/FORMAT=");
295 if (cd->format_options_avalue)
296 g_string_append (string, "AVALUE");
298 g_string_append (string, "DVALUE");
299 g_string_append (string, " ");
301 if (cd->format_options_table)
302 g_string_append (string, "TABLES");
304 g_string_append (string, "NOTABLES");
305 g_string_append (string, " ");
307 if (cd->format_options_pivot)
308 g_string_append (string, "PIVOT");
310 g_string_append (string, "NOPIVOT");
314 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->stat, &iter); ok;
315 i++, ok = gtk_tree_model_iter_next (cd->stat, &iter))
318 gtk_tree_model_get (cd->stat, &iter,
319 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
323 selected &= ~(1u << i);
326 if (!(selected & (1u << CS_STATS_NONE)))
330 g_string_append (string, "\n\t/STATISTICS=");
332 for (i = 0; i < N_CROSSTABS_STATS; i++)
333 if (selected & (1u << i))
336 g_string_append (string, " ");
337 g_string_append (string, stats[i].name);
343 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->cell, &iter); ok;
344 i++, ok = gtk_tree_model_iter_next (cd->cell, &iter))
347 gtk_tree_model_get (cd->cell, &iter,
348 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
352 selected &= ~(1u << i);
357 g_string_append (string, "\n\t/CELLS=");
358 if (selected & (1u << CS_CELLS_NONE))
359 g_string_append (string, "NONE");
363 for (i = 0; i < N_CROSSTABS_CELLS; i++)
364 if (selected & (1u << i))
367 g_string_append (string, " ");
368 g_string_append (string, cells[i].name);
372 g_string_append (string, ".\n");
376 g_string_free (string, FALSE);
382 psppire_dialog_action_crosstabs_class_init (PsppireDialogActionCrosstabsClass *class)
384 psppire_dialog_action_set_activation (class, psppire_dialog_action_crosstabs_activate);
385 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
390 psppire_dialog_action_crosstabs_init (PsppireDialogActionCrosstabs *act)