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"
25 #include <language/syntax-string-source.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},
117 struct format_options
124 struct crosstabs_dialog
126 GtkTreeView *row_vars;
127 GtkTreeView *col_vars;
130 GtkToggleButton *table_button;
131 GtkToggleButton *pivot_button;
133 GtkWidget *format_dialog;
134 GtkWidget *cell_dialog;
135 GtkWidget *stat_dialog;
137 GtkToggleButton *avalue;
141 GtkWidget *stat_view;
142 GtkWidget *cell_view;
143 GtkToggleButton *label;
144 GtkToggleButton *no_label;
145 GtkToggleButton *no_val_label;
146 struct format_options current_opts;
150 refresh (PsppireDialog *dialog, struct crosstabs_dialog *cd)
152 GtkTreeModel *liststore = gtk_tree_view_get_model (cd->row_vars);
153 gtk_list_store_clear (GTK_LIST_STORE (liststore));
155 liststore = gtk_tree_view_get_model (cd->col_vars);
156 gtk_list_store_clear (GTK_LIST_STORE (liststore));
159 on_format_clicked (struct crosstabs_dialog *cd)
166 if (cd->current_opts.avalue)
168 gtk_toggle_button_set_active (cd->avalue, TRUE);
170 if (cd->current_opts.table)
172 gtk_toggle_button_set_active (cd->table_button, TRUE);
174 if (cd->current_opts.pivot)
176 gtk_toggle_button_set_active (cd->pivot_button, TRUE);
178 lab = gtk_toggle_button_get_active (cd->label);
179 no_lab = gtk_toggle_button_get_active (cd->no_label);
180 no_val_lab = gtk_toggle_button_get_active (cd->no_val_label);
184 gtk_toggle_button_set_active (cd->label, TRUE);
187 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->format_dialog));
189 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
191 cd->current_opts.avalue = (gtk_toggle_button_get_active (cd->avalue) == TRUE )
193 cd->current_opts.table = (gtk_toggle_button_get_active (cd->table_button) == TRUE)
195 cd->current_opts.pivot = (gtk_toggle_button_get_active (cd->pivot_button) == TRUE)
200 gtk_toggle_button_set_active (cd->label, lab);
201 gtk_toggle_button_set_active (cd->no_label, no_lab);
202 gtk_toggle_button_set_active (cd->no_val_label, no_val_lab);
207 on_statistics_clicked (struct crosstabs_dialog *cd)
209 GtkListStore *liststore;
212 liststore = clone_list_store (GTK_LIST_STORE (cd->stat));
214 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->stat_dialog));
216 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
218 g_object_unref (liststore);
222 g_object_unref (cd->stat);
223 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->stat_view) , GTK_TREE_MODEL (liststore));
224 cd->stat = GTK_TREE_MODEL (liststore);
228 on_cell_clicked (struct crosstabs_dialog *cd)
230 GtkListStore *liststore;
233 liststore = clone_list_store (GTK_LIST_STORE (cd->cell));
235 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->cell_dialog));
237 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
239 g_object_unref (liststore);
243 g_object_unref (cd->cell);
244 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->cell_view) , GTK_TREE_MODEL (liststore));
245 cd->cell = GTK_TREE_MODEL (liststore);
250 generate_syntax (const struct crosstabs_dialog *cd)
259 GString *string = g_string_new ("CROSSTABS");
261 g_string_append (string, "\n\t/TABLES=");
262 append_variable_names (string, cd->dict, GTK_TREE_VIEW (cd->row_vars), 0);
263 g_string_append (string, "\tBY\t");
264 append_variable_names (string, cd->dict, GTK_TREE_VIEW (cd->col_vars), 0);
266 g_string_append (string, "\n\t/FORMAT=");
268 if (cd->current_opts.avalue)
270 g_string_append (string, "AVALUE");
274 g_string_append (string, "DVALUE");
276 g_string_append (string, " ");
277 if (gtk_toggle_button_get_active (cd->label))
279 g_string_append (string, "LABELS");
281 else if (gtk_toggle_button_get_active (cd->no_label))
283 g_string_append (string, "NOLABELS");
285 else if (gtk_toggle_button_get_active (cd->no_val_label))
287 g_string_append (string, "NOVALLABS");
289 g_string_append (string, " ");
290 if (cd->current_opts.table)
291 g_string_append (string, "TABLES");
293 g_string_append (string, "NOTABLES");
294 g_string_append (string, " ");
296 if (cd->current_opts.pivot)
297 g_string_append (string, "PIVOT");
299 g_string_append (string, "NOPIVOT");
302 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->stat, &iter); ok;
303 i++, ok = gtk_tree_model_iter_next (cd->stat, &iter))
306 gtk_tree_model_get (cd->stat, &iter,
307 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
311 selected &= ~(1u << i);
314 if (!(selected & (1u << CS_STATS_NONE)))
318 g_string_append (string, "\n\t/STATISTICS=");
320 for (i = 0; i < N_CROSSTABS_STATS; i++)
321 if (selected & (1u << i))
324 g_string_append (string, " ");
325 g_string_append (string, stats[i].name);
331 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->cell, &iter); ok;
332 i++, ok = gtk_tree_model_iter_next (cd->cell, &iter))
335 gtk_tree_model_get (cd->cell, &iter,
336 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
340 selected &= ~(1u << i);
343 g_string_append (string, "\n\t/CELLS=");
344 if (selected & (1u << CS_CELLS_NONE))
345 g_string_append (string, "NONE");
349 for (i = 0; i < N_CROSSTABS_CELLS; i++)
350 if (selected & (1u << i))
353 g_string_append (string, " ");
354 g_string_append (string, cells[i].name);
358 g_string_append (string, ".\n");
362 g_string_free (string, FALSE);
367 /* Dialog is valid iff at least one row and one column variable has
370 dialog_state_valid (gpointer data)
372 struct crosstabs_dialog *cd = data;
374 GtkTreeModel *row_vars = gtk_tree_view_get_model (cd->row_vars);
375 GtkTreeModel *col_vars = gtk_tree_view_get_model (cd->col_vars);
379 return (gtk_tree_model_get_iter_first (row_vars, ¬used)
380 && gtk_tree_model_get_iter_first (col_vars, ¬used));
383 /* Pops up the Crosstabs dialog box */
385 crosstabs_dialog (GObject *o, gpointer data)
388 struct crosstabs_dialog cd;
390 GtkBuilder *xml = builder_new ("crosstabs.ui");
391 PsppireVarStore *vs = NULL;
393 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
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 *row_selector = get_widget_assert (xml, "row-selector");
401 GtkWidget *col_selector = get_widget_assert (xml, "col-selector");
402 GtkWidget *format_button = get_widget_assert (xml, "format-button");
403 GtkWidget *stat_button = get_widget_assert (xml, "stats-button");
404 GtkWidget *cell_button = get_widget_assert (xml, "cell-button");
407 cd.stat_view = get_widget_assert (xml, "stats-view");
408 cd.cell_view = get_widget_assert (xml, "cell-view");
410 g_object_get (de->data_editor, "var-store", &vs, NULL);
412 put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.stat_view),
417 put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.cell_view),
423 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
425 g_object_set (source, "dictionary", vs->dict, NULL);
427 set_dest_model (GTK_TREE_VIEW (dest_rows), vs->dict);
428 set_dest_model (GTK_TREE_VIEW (dest_cols), vs->dict);
430 psppire_selector_set_subjects (PSPPIRE_SELECTOR (row_selector),
433 insert_source_row_into_tree_view,
437 psppire_selector_set_subjects (PSPPIRE_SELECTOR (col_selector),
440 insert_source_row_into_tree_view,
444 cd.row_vars = GTK_TREE_VIEW (dest_rows);
445 cd.col_vars = GTK_TREE_VIEW (dest_cols);
447 cd.format_dialog = get_widget_assert (xml, "format-dialog");
448 cd.table_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "print-tables"));
449 cd.pivot_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pivot"));
450 cd.label = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
451 cd.no_label = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton2"));
452 cd.no_val_label = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton3"));
453 cd.stat_dialog = get_widget_assert (xml, "stat-dialog");
454 cd.cell_dialog = get_widget_assert (xml, "cell-dialog");
456 cd.stat = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.stat_view));
457 cd.cell = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.cell_view));
458 cd.avalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "ascending"));
459 cd.current_opts.avalue = TRUE;
460 cd.current_opts.table = TRUE;
461 cd.current_opts.pivot = TRUE;
463 gtk_window_set_transient_for (GTK_WINDOW (cd.format_dialog), GTK_WINDOW (de));
464 gtk_window_set_transient_for (GTK_WINDOW (cd.cell_dialog), GTK_WINDOW (de));
465 gtk_window_set_transient_for (GTK_WINDOW (cd.stat_dialog), GTK_WINDOW (de));
467 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &cd);
469 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
470 dialog_state_valid, &cd);
472 g_signal_connect_swapped (format_button, "clicked",
473 G_CALLBACK (on_format_clicked), &cd);
474 g_signal_connect_swapped (stat_button, "clicked",
475 G_CALLBACK (on_statistics_clicked), &cd);
476 g_signal_connect_swapped (cell_button, "clicked",
477 G_CALLBACK (on_cell_clicked), &cd);
479 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
484 case GTK_RESPONSE_OK:
486 gchar *syntax = generate_syntax (&cd);
488 struct getl_interface *sss = create_syntax_string_source (syntax);
489 execute_syntax (sss);
494 case PSPPIRE_RESPONSE_PASTE:
496 gchar *syntax = generate_syntax (&cd);
498 paste_syntax_in_new_window (syntax);
507 g_object_unref (xml);