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 (PsppireDialogAction *a)
208 PsppireDialogActionCrosstabs *act = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
209 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
211 GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
212 GtkBuilder *xml = g_hash_table_lookup (thing, a);
215 xml = builder_new ("crosstabs.ui");
216 g_hash_table_insert (thing, a, xml);
218 pda->dialog = get_widget_assert (xml, "crosstabs-dialog");
219 pda->source = get_widget_assert (xml, "dict-treeview");
221 act->dest_rows = get_widget_assert (xml, "rows");
222 act->dest_cols = get_widget_assert (xml, "cols");
223 act->format_button = get_widget_assert (xml, "format-button");
224 act->stat_button = get_widget_assert (xml, "stats-button");
225 act->cell_button = get_widget_assert (xml, "cell-button");
226 act->stat_view = get_widget_assert (xml, "stats-view");
227 act->cell_view = get_widget_assert (xml, "cell-view");
228 act->cell_dialog = get_widget_assert (xml, "cell-dialog");
229 act->stat_dialog = get_widget_assert (xml, "stat-dialog");
230 act->format_dialog = get_widget_assert (xml, "format-dialog");
232 act->avalue_button = get_widget_assert (xml, "ascending");
233 act->table_button = get_widget_assert (xml, "print-tables");
234 act->pivot_button = get_widget_assert (xml, "pivot");
236 act->format_options_avalue = TRUE;
237 act->format_options_table = TRUE;
238 act->format_options_pivot = TRUE;
240 psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->cell_view),
245 act->cell = gtk_tree_view_get_model (GTK_TREE_VIEW (act->cell_view));
247 psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
252 act->stat = gtk_tree_view_get_model (GTK_TREE_VIEW (act->stat_view));
254 psppire_dialog_action_set_refresh (pda, refresh);
256 psppire_dialog_action_set_valid_predicate (pda,
259 g_signal_connect_swapped (act->cell_button, "clicked",
260 G_CALLBACK (on_cell_clicked), act);
262 g_signal_connect_swapped (act->stat_button, "clicked",
263 G_CALLBACK (on_statistics_clicked), act);
265 g_signal_connect_swapped (act->format_button, "clicked",
266 G_CALLBACK (on_format_clicked), act);
274 generate_syntax (const PsppireDialogAction *a)
276 PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
280 GString *string = g_string_new ("CROSSTABS ");
284 g_string_append (string, "\n\t/TABLES=");
285 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->dest_rows), 0, string);
286 g_string_append (string, "\tBY\t");
287 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->dest_cols), 0, string);
290 g_string_append (string, "\n\t/FORMAT=");
292 if (cd->format_options_avalue)
293 g_string_append (string, "AVALUE");
295 g_string_append (string, "DVALUE");
296 g_string_append (string, " ");
298 if (cd->format_options_table)
299 g_string_append (string, "TABLES");
301 g_string_append (string, "NOTABLES");
302 g_string_append (string, " ");
304 if (cd->format_options_pivot)
305 g_string_append (string, "PIVOT");
307 g_string_append (string, "NOPIVOT");
311 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->stat, &iter); ok;
312 i++, ok = gtk_tree_model_iter_next (cd->stat, &iter))
315 gtk_tree_model_get (cd->stat, &iter,
316 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
320 selected &= ~(1u << i);
323 if (!(selected & (1u << CS_STATS_NONE)))
327 g_string_append (string, "\n\t/STATISTICS=");
329 for (i = 0; i < N_CROSSTABS_STATS; i++)
330 if (selected & (1u << i))
333 g_string_append (string, " ");
334 g_string_append (string, stats[i].name);
340 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->cell, &iter); ok;
341 i++, ok = gtk_tree_model_iter_next (cd->cell, &iter))
344 gtk_tree_model_get (cd->cell, &iter,
345 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
349 selected &= ~(1u << i);
354 g_string_append (string, "\n\t/CELLS=");
355 if (selected & (1u << CS_CELLS_NONE))
356 g_string_append (string, "NONE");
360 for (i = 0; i < N_CROSSTABS_CELLS; i++)
361 if (selected & (1u << i))
364 g_string_append (string, " ");
365 g_string_append (string, cells[i].name);
369 g_string_append (string, ".\n");
373 g_string_free (string, FALSE);
379 psppire_dialog_action_crosstabs_class_init (PsppireDialogActionCrosstabsClass *class)
381 psppire_dialog_action_set_activation (class, psppire_dialog_action_crosstabs_activate);
382 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
387 psppire_dialog_action_crosstabs_init (PsppireDialogActionCrosstabs *act)