1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008 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 <language/syntax-string-source.h>
27 #include <ui/gui/psppire-data-window.h>
28 #include <ui/gui/dialog-common.h>
29 #include <ui/gui/dict-display.h>
31 #include <ui/gui/psppire-dialog.h>
32 #include <ui/gui/psppire-var-store.h>
33 #include <ui/gui/helper.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
40 #define CROSSTABS_STATS \
41 CS (CHISQ, N_("Chisq")) \
44 CS (LAMBDA, N_("Lambda")) \
46 CS (BTAU, N_("BTau")) \
47 CS (CTAU, N_("CTau")) \
48 CS (RISK, N_("Risk")) \
49 CS (GAMMA, N_("Gamma")) \
51 CS (KAPPA, N_("Kappa")) \
53 CS (CORR, N_("Corr")) \
54 CS (STATS_NONE, N_("None"))
56 #define CROSSTABS_CELLS \
57 CS (COUNT, N_("Count")) \
59 CS (COLUMN, N_("Column")) \
60 CS (TOTAL, N_("Total")) \
61 CS (EXPECTED, N_("Expected")) \
62 CS (RESIDUAL, N_("Residual")) \
63 CS (SRESIDUAL, N_("Std. Residual")) \
64 CS (ASRESIDUAL, N_("Adjusted Std. Residual")) \
65 CS (CELLS_NONE, N_("None"))
69 #define CS(NAME, LABEL) CS_##NAME,
77 #define CS(NAME, LABEL) CS_##NAME,
85 #define CS(NAME, LABEL) B_CS_##NAME = 1u << CS_##NAME,
89 B_CS_STATS_ALL = (1u << N_CROSSTABS_STATS) - 1,
90 B_CS_CELLS_ALL = (1u << N_CROSSTABS_CELLS) - 1,
91 B_CS_STATS_DEFAULT = B_CS_CHISQ,
92 B_CS_CELL_DEFAULT = B_CS_COUNT | B_CS_ROW | B_CS_COLUMN | B_CS_TOTAL,
96 static const struct checkbox_entry_item stats[] =
98 #define CS(NAME, LABEL) {#NAME, LABEL},
104 static const struct checkbox_entry_item cells[] =
106 #define CS(NAME, LABEL) {#NAME, LABEL},
118 struct format_options
125 struct crosstabs_dialog
127 GtkTreeView *row_vars;
128 GtkTreeView *col_vars;
131 GtkToggleButton *table_button;
132 GtkToggleButton *pivot_button;
134 GtkWidget *format_dialog;
135 GtkWidget *cell_dialog;
136 GtkWidget *stat_dialog;
138 GtkToggleButton *avalue;
142 GtkWidget *stat_view;
143 GtkWidget *cell_view;
144 GtkToggleButton *label;
145 GtkToggleButton *no_label;
146 GtkToggleButton *no_val_label;
147 struct format_options current_opts;
151 refresh (PsppireDialog *dialog, struct crosstabs_dialog *cd)
153 GtkTreeModel *liststore = gtk_tree_view_get_model (cd->row_vars);
154 gtk_list_store_clear (GTK_LIST_STORE (liststore));
156 liststore = gtk_tree_view_get_model (cd->col_vars);
157 gtk_list_store_clear (GTK_LIST_STORE (liststore));
160 on_format_clicked (struct crosstabs_dialog *cd)
167 if (cd->current_opts.avalue)
169 gtk_toggle_button_set_active (cd->avalue, TRUE);
171 if (cd->current_opts.table)
173 gtk_toggle_button_set_active (cd->table_button, TRUE);
175 if (cd->current_opts.pivot)
177 gtk_toggle_button_set_active (cd->pivot_button, TRUE);
179 lab = gtk_toggle_button_get_active (cd->label);
180 no_lab = gtk_toggle_button_get_active (cd->no_label);
181 no_val_lab = gtk_toggle_button_get_active (cd->no_val_label);
185 gtk_toggle_button_set_active (cd->label, TRUE);
188 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->format_dialog));
190 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
192 cd->current_opts.avalue = (gtk_toggle_button_get_active (cd->avalue) == TRUE )
194 cd->current_opts.table = (gtk_toggle_button_get_active (cd->table_button) == TRUE)
196 cd->current_opts.pivot = (gtk_toggle_button_get_active (cd->pivot_button) == TRUE)
201 gtk_toggle_button_set_active (cd->label, lab);
202 gtk_toggle_button_set_active (cd->no_label, no_lab);
203 gtk_toggle_button_set_active (cd->no_val_label, no_val_lab);
208 on_statistics_clicked (struct crosstabs_dialog *cd)
210 GtkListStore *liststore;
213 liststore = clone_list_store (GTK_LIST_STORE (cd->stat));
215 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->stat_dialog));
217 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
219 g_object_unref (liststore);
223 g_object_unref (cd->stat);
224 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->stat_view) , GTK_TREE_MODEL (liststore));
225 cd->stat = GTK_TREE_MODEL (liststore);
229 on_cell_clicked (struct crosstabs_dialog *cd)
231 GtkListStore *liststore;
234 liststore = clone_list_store (GTK_LIST_STORE (cd->cell));
236 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->cell_dialog));
238 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
240 g_object_unref (liststore);
244 g_object_unref (cd->cell);
245 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->cell_view) , GTK_TREE_MODEL (liststore));
246 cd->cell = GTK_TREE_MODEL (liststore);
251 generate_syntax (const struct crosstabs_dialog *cd)
260 GString *string = g_string_new ("CROSSTABS");
262 g_string_append (string, "\n\t/TABLES=");
263 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->row_vars), 0, string);
264 g_string_append (string, "\tBY\t");
265 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->col_vars), 0, string);
267 g_string_append (string, "\n\t/FORMAT=");
269 if (cd->current_opts.avalue)
271 g_string_append (string, "AVALUE");
275 g_string_append (string, "DVALUE");
277 g_string_append (string, " ");
278 if (gtk_toggle_button_get_active (cd->label))
280 g_string_append (string, "LABELS");
282 else if (gtk_toggle_button_get_active (cd->no_label))
284 g_string_append (string, "NOLABELS");
286 else if (gtk_toggle_button_get_active (cd->no_val_label))
288 g_string_append (string, "NOVALLABS");
290 g_string_append (string, " ");
291 if (cd->current_opts.table)
292 g_string_append (string, "TABLES");
294 g_string_append (string, "NOTABLES");
295 g_string_append (string, " ");
297 if (cd->current_opts.pivot)
298 g_string_append (string, "PIVOT");
300 g_string_append (string, "NOPIVOT");
303 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->stat, &iter); ok;
304 i++, ok = gtk_tree_model_iter_next (cd->stat, &iter))
307 gtk_tree_model_get (cd->stat, &iter,
308 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
312 selected &= ~(1u << i);
315 if (!(selected & (1u << CS_STATS_NONE)))
319 g_string_append (string, "\n\t/STATISTICS=");
321 for (i = 0; i < N_CROSSTABS_STATS; i++)
322 if (selected & (1u << i))
325 g_string_append (string, " ");
326 g_string_append (string, stats[i].name);
332 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->cell, &iter); ok;
333 i++, ok = gtk_tree_model_iter_next (cd->cell, &iter))
336 gtk_tree_model_get (cd->cell, &iter,
337 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
341 selected &= ~(1u << i);
344 g_string_append (string, "\n\t/CELLS=");
345 if (selected & (1u << CS_CELLS_NONE))
346 g_string_append (string, "NONE");
350 for (i = 0; i < N_CROSSTABS_CELLS; i++)
351 if (selected & (1u << i))
354 g_string_append (string, " ");
355 g_string_append (string, cells[i].name);
359 g_string_append (string, ".\n");
363 g_string_free (string, FALSE);
368 /* Dialog is valid iff at least one row and one column variable has
371 dialog_state_valid (gpointer data)
373 struct crosstabs_dialog *cd = data;
375 GtkTreeModel *row_vars = gtk_tree_view_get_model (cd->row_vars);
376 GtkTreeModel *col_vars = gtk_tree_view_get_model (cd->col_vars);
380 return (gtk_tree_model_get_iter_first (row_vars, ¬used)
381 && gtk_tree_model_get_iter_first (col_vars, ¬used));
384 /* Pops up the Crosstabs dialog box */
386 crosstabs_dialog (PsppireDataWindow *de)
389 struct crosstabs_dialog cd;
391 GtkBuilder *xml = builder_new ("crosstabs.ui");
392 PsppireVarStore *vs = NULL;
393 PsppireDict *dict = NULL;
396 GtkWidget *dialog = get_widget_assert (xml, "crosstabs-dialog");
397 GtkWidget *source = get_widget_assert (xml, "dict-treeview");
398 GtkWidget *dest_rows = get_widget_assert (xml, "rows");
399 GtkWidget *dest_cols = get_widget_assert (xml, "cols");
400 GtkWidget *format_button = get_widget_assert (xml, "format-button");
401 GtkWidget *stat_button = get_widget_assert (xml, "stats-button");
402 GtkWidget *cell_button = get_widget_assert (xml, "cell-button");
405 cd.stat_view = get_widget_assert (xml, "stats-view");
406 cd.cell_view = get_widget_assert (xml, "cell-view");
408 g_object_get (de->data_editor, "var-store", &vs, NULL);
410 put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.stat_view),
415 put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.cell_view),
421 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
423 g_object_get (vs, "dictionary", &dict, NULL);
424 g_object_set (source, "model", dict, NULL);
426 cd.row_vars = GTK_TREE_VIEW (dest_rows);
427 cd.col_vars = GTK_TREE_VIEW (dest_cols);
428 g_object_get (vs, "dictionary", &cd.dict, NULL);
429 cd.format_dialog = get_widget_assert (xml, "format-dialog");
430 cd.table_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "print-tables"));
431 cd.pivot_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pivot"));
432 cd.label = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
433 cd.no_label = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton2"));
434 cd.no_val_label = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton3"));
435 cd.stat_dialog = get_widget_assert (xml, "stat-dialog");
436 cd.cell_dialog = get_widget_assert (xml, "cell-dialog");
438 cd.stat = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.stat_view));
439 cd.cell = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.cell_view));
440 cd.avalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "ascending"));
441 cd.current_opts.avalue = TRUE;
442 cd.current_opts.table = TRUE;
443 cd.current_opts.pivot = TRUE;
445 gtk_window_set_transient_for (GTK_WINDOW (cd.format_dialog), GTK_WINDOW (de));
446 gtk_window_set_transient_for (GTK_WINDOW (cd.cell_dialog), GTK_WINDOW (de));
447 gtk_window_set_transient_for (GTK_WINDOW (cd.stat_dialog), GTK_WINDOW (de));
449 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &cd);
451 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
452 dialog_state_valid, &cd);
454 g_signal_connect_swapped (format_button, "clicked",
455 G_CALLBACK (on_format_clicked), &cd);
456 g_signal_connect_swapped (stat_button, "clicked",
457 G_CALLBACK (on_statistics_clicked), &cd);
458 g_signal_connect_swapped (cell_button, "clicked",
459 G_CALLBACK (on_cell_clicked), &cd);
461 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
466 case GTK_RESPONSE_OK:
468 gchar *syntax = generate_syntax (&cd);
470 struct getl_interface *sss = create_syntax_string_source (syntax);
471 execute_syntax (sss);
476 case PSPPIRE_RESPONSE_PASTE:
478 gchar *syntax = generate_syntax (&cd);
480 paste_syntax_to_window (syntax);
489 g_object_unref (xml);