1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2010, 2011 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/>. */
19 #include "checkbox-treeview.h"
20 #include "crosstabs-dialog.h"
21 #include "psppire-var-view.h"
26 #include <ui/gui/psppire-data-window.h>
27 #include <ui/gui/dialog-common.h>
28 #include <ui/gui/dict-display.h>
30 #include <ui/gui/psppire-dialog.h>
31 #include <ui/gui/psppire-var-store.h>
32 #include <ui/gui/helper.h>
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
39 #define CROSSTABS_STATS \
40 CS (CHISQ, N_("Chisq")) \
43 CS (LAMBDA, N_("Lambda")) \
45 CS (BTAU, N_("BTau")) \
46 CS (CTAU, N_("CTau")) \
47 CS (RISK, N_("Risk")) \
48 CS (GAMMA, N_("Gamma")) \
50 CS (KAPPA, N_("Kappa")) \
52 CS (CORR, N_("Corr")) \
53 CS (STATS_NONE, N_("None"))
55 #define CROSSTABS_CELLS \
56 CS (COUNT, N_("Count")) \
58 CS (COLUMN, N_("Column")) \
59 CS (TOTAL, N_("Total")) \
60 CS (EXPECTED, N_("Expected")) \
61 CS (RESIDUAL, N_("Residual")) \
62 CS (SRESIDUAL, N_("Std. Residual")) \
63 CS (ASRESIDUAL, N_("Adjusted Std. Residual")) \
64 CS (CELLS_NONE, N_("None"))
68 #define CS(NAME, LABEL) CS_##NAME,
76 #define CS(NAME, LABEL) CS_##NAME,
84 #define CS(NAME, LABEL) B_CS_##NAME = 1u << CS_##NAME,
88 B_CS_STATS_ALL = (1u << N_CROSSTABS_STATS) - 1,
89 B_CS_CELLS_ALL = (1u << N_CROSSTABS_CELLS) - 1,
90 B_CS_STATS_DEFAULT = B_CS_CHISQ,
91 B_CS_CELL_DEFAULT = B_CS_COUNT | B_CS_ROW | B_CS_COLUMN | B_CS_TOTAL,
95 static const struct checkbox_entry_item stats[] =
97 #define CS(NAME, LABEL) {#NAME, LABEL},
103 static const struct checkbox_entry_item cells[] =
105 #define CS(NAME, LABEL) {#NAME, LABEL},
111 struct format_options
118 struct crosstabs_dialog
120 GtkTreeView *row_vars;
121 GtkTreeView *col_vars;
124 GtkToggleButton *table_button;
125 GtkToggleButton *pivot_button;
127 GtkWidget *format_dialog;
128 GtkWidget *cell_dialog;
129 GtkWidget *stat_dialog;
131 GtkToggleButton *avalue;
135 GtkWidget *stat_view;
136 GtkWidget *cell_view;
137 struct format_options current_opts;
141 refresh (PsppireDialog *dialog, struct crosstabs_dialog *cd)
143 GtkTreeModel *liststore = gtk_tree_view_get_model (cd->row_vars);
144 gtk_list_store_clear (GTK_LIST_STORE (liststore));
146 liststore = gtk_tree_view_get_model (cd->col_vars);
147 gtk_list_store_clear (GTK_LIST_STORE (liststore));
150 on_format_clicked (struct crosstabs_dialog *cd)
157 if (cd->current_opts.avalue)
159 gtk_toggle_button_set_active (cd->avalue, TRUE);
161 if (cd->current_opts.table)
163 gtk_toggle_button_set_active (cd->table_button, TRUE);
165 if (cd->current_opts.pivot)
167 gtk_toggle_button_set_active (cd->pivot_button, TRUE);
170 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->format_dialog));
172 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
174 cd->current_opts.avalue = (gtk_toggle_button_get_active (cd->avalue) == TRUE )
176 cd->current_opts.table = (gtk_toggle_button_get_active (cd->table_button) == TRUE)
178 cd->current_opts.pivot = (gtk_toggle_button_get_active (cd->pivot_button) == TRUE)
184 on_statistics_clicked (struct crosstabs_dialog *cd)
186 GtkListStore *liststore;
189 liststore = clone_list_store (GTK_LIST_STORE (cd->stat));
191 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->stat_dialog));
193 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
195 g_object_unref (liststore);
199 g_object_unref (cd->stat);
200 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->stat_view) , GTK_TREE_MODEL (liststore));
201 cd->stat = GTK_TREE_MODEL (liststore);
205 on_cell_clicked (struct crosstabs_dialog *cd)
207 GtkListStore *liststore;
210 liststore = clone_list_store (GTK_LIST_STORE (cd->cell));
212 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->cell_dialog));
214 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
216 g_object_unref (liststore);
220 g_object_unref (cd->cell);
221 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->cell_view) , GTK_TREE_MODEL (liststore));
222 cd->cell = GTK_TREE_MODEL (liststore);
227 generate_syntax (const struct crosstabs_dialog *cd)
236 GString *string = g_string_new ("CROSSTABS");
238 g_string_append (string, "\n\t/TABLES=");
239 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->row_vars), 0, string);
240 g_string_append (string, "\tBY\t");
241 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->col_vars), 0, string);
243 g_string_append (string, "\n\t/FORMAT=");
245 if (cd->current_opts.avalue)
247 g_string_append (string, "AVALUE");
251 g_string_append (string, "DVALUE");
253 g_string_append (string, " ");
254 if (cd->current_opts.table)
255 g_string_append (string, "TABLES");
257 g_string_append (string, "NOTABLES");
258 g_string_append (string, " ");
260 if (cd->current_opts.pivot)
261 g_string_append (string, "PIVOT");
263 g_string_append (string, "NOPIVOT");
266 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->stat, &iter); ok;
267 i++, ok = gtk_tree_model_iter_next (cd->stat, &iter))
270 gtk_tree_model_get (cd->stat, &iter,
271 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
275 selected &= ~(1u << i);
278 if (!(selected & (1u << CS_STATS_NONE)))
282 g_string_append (string, "\n\t/STATISTICS=");
284 for (i = 0; i < N_CROSSTABS_STATS; i++)
285 if (selected & (1u << i))
288 g_string_append (string, " ");
289 g_string_append (string, stats[i].name);
295 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->cell, &iter); ok;
296 i++, ok = gtk_tree_model_iter_next (cd->cell, &iter))
299 gtk_tree_model_get (cd->cell, &iter,
300 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
304 selected &= ~(1u << i);
307 g_string_append (string, "\n\t/CELLS=");
308 if (selected & (1u << CS_CELLS_NONE))
309 g_string_append (string, "NONE");
313 for (i = 0; i < N_CROSSTABS_CELLS; i++)
314 if (selected & (1u << i))
317 g_string_append (string, " ");
318 g_string_append (string, cells[i].name);
322 g_string_append (string, ".\n");
326 g_string_free (string, FALSE);
331 /* Dialog is valid iff at least one row and one column variable has
334 dialog_state_valid (gpointer data)
336 struct crosstabs_dialog *cd = data;
338 GtkTreeModel *row_vars = gtk_tree_view_get_model (cd->row_vars);
339 GtkTreeModel *col_vars = gtk_tree_view_get_model (cd->col_vars);
343 return (gtk_tree_model_get_iter_first (row_vars, ¬used)
344 && gtk_tree_model_get_iter_first (col_vars, ¬used));
347 /* Pops up the Crosstabs dialog box */
349 crosstabs_dialog (PsppireDataWindow *de)
352 struct crosstabs_dialog cd;
354 GtkBuilder *xml = builder_new ("crosstabs.ui");
355 PsppireVarStore *vs = NULL;
356 PsppireDict *dict = NULL;
359 GtkWidget *dialog = get_widget_assert (xml, "crosstabs-dialog");
360 GtkWidget *source = get_widget_assert (xml, "dict-treeview");
361 GtkWidget *dest_rows = get_widget_assert (xml, "rows");
362 GtkWidget *dest_cols = get_widget_assert (xml, "cols");
363 GtkWidget *format_button = get_widget_assert (xml, "format-button");
364 GtkWidget *stat_button = get_widget_assert (xml, "stats-button");
365 GtkWidget *cell_button = get_widget_assert (xml, "cell-button");
368 cd.stat_view = get_widget_assert (xml, "stats-view");
369 cd.cell_view = get_widget_assert (xml, "cell-view");
371 g_object_get (de->data_editor, "var-store", &vs, NULL);
373 put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.stat_view),
378 put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.cell_view),
384 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
386 g_object_get (vs, "dictionary", &dict, NULL);
387 g_object_set (source, "model", dict, NULL);
389 cd.row_vars = GTK_TREE_VIEW (dest_rows);
390 cd.col_vars = GTK_TREE_VIEW (dest_cols);
391 g_object_get (vs, "dictionary", &cd.dict, NULL);
392 cd.format_dialog = get_widget_assert (xml, "format-dialog");
393 cd.table_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "print-tables"));
394 cd.pivot_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pivot"));
395 cd.stat_dialog = get_widget_assert (xml, "stat-dialog");
396 cd.cell_dialog = get_widget_assert (xml, "cell-dialog");
398 cd.stat = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.stat_view));
399 cd.cell = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.cell_view));
400 cd.avalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "ascending"));
401 cd.current_opts.avalue = TRUE;
402 cd.current_opts.table = TRUE;
403 cd.current_opts.pivot = TRUE;
405 gtk_window_set_transient_for (GTK_WINDOW (cd.format_dialog), GTK_WINDOW (de));
406 gtk_window_set_transient_for (GTK_WINDOW (cd.cell_dialog), GTK_WINDOW (de));
407 gtk_window_set_transient_for (GTK_WINDOW (cd.stat_dialog), GTK_WINDOW (de));
409 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &cd);
411 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
412 dialog_state_valid, &cd);
414 g_signal_connect_swapped (format_button, "clicked",
415 G_CALLBACK (on_format_clicked), &cd);
416 g_signal_connect_swapped (stat_button, "clicked",
417 G_CALLBACK (on_statistics_clicked), &cd);
418 g_signal_connect_swapped (cell_button, "clicked",
419 G_CALLBACK (on_cell_clicked), &cd);
421 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
426 case GTK_RESPONSE_OK:
427 g_free (execute_syntax_string (de, generate_syntax (&cd)));
429 case PSPPIRE_RESPONSE_PASTE:
430 g_free (paste_syntax_to_window (generate_syntax (&cd)));
436 g_object_unref (xml);