acac2b67494849a5ff42cb342c25a7627821fb78
[pspp-builds.git] / src / ui / gui / crosstabs-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2010, 2011  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "checkbox-treeview.h"
20 #include "crosstabs-dialog.h"
21 #include "psppire-var-view.h"
22
23 #include <gtk/gtk.h>
24 #include <stdlib.h>
25
26 #include <ui/gui/psppire-data-window.h>
27 #include <ui/gui/dialog-common.h>
28 #include <ui/gui/dict-display.h>
29 #include "executor.h"
30 #include <ui/gui/psppire-dialog.h>
31 #include <ui/gui/psppire-var-store.h>
32 #include <ui/gui/helper.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 #define CROSSTABS_STATS                       \
40   CS (CHISQ, N_("Chisq"))                         \
41   CS (PHI, N_("Phi"))         \
42   CS (CC, N_("CC"))                   \
43   CS (LAMBDA, N_("Lambda"))                   \
44   CS (UC, N_("UC")) \
45   CS (BTAU, N_("BTau"))                 \
46   CS (CTAU, N_("CTau"))                 \
47   CS (RISK, N_("Risk"))  \
48   CS (GAMMA, N_("Gamma"))                       \
49   CS (D, N_("D"))                         \
50   CS (KAPPA, N_("Kappa"))                 \
51   CS (ETA, N_("Eta"))  \
52   CS (CORR, N_("Corr")) \
53   CS (STATS_NONE, N_("None"))
54
55 #define CROSSTABS_CELLS \
56   CS (COUNT, N_("Count"))   \
57   CS (ROW, N_("Row"))   \
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"))
65
66 enum
67   {
68 #define CS(NAME, LABEL) CS_##NAME,
69     CROSSTABS_STATS
70 #undef CS
71     N_CROSSTABS_STATS
72   };
73
74 enum
75   {
76 #define CS(NAME, LABEL) CS_##NAME,
77     CROSSTABS_CELLS
78 #undef CS
79     N_CROSSTABS_CELLS
80   };
81
82 enum
83   {
84 #define CS(NAME, LABEL) B_CS_##NAME = 1u << CS_##NAME,
85     CROSSTABS_STATS
86     CROSSTABS_CELLS
87 #undef CS
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,
92     B_CS_NONE
93   };
94
95 static const struct checkbox_entry_item stats[] =
96   {
97 #define CS(NAME, LABEL) {#NAME, LABEL},
98     CROSSTABS_STATS \
99     CS(NONE, N_("None"))
100 #undef CS
101   };
102
103 static const struct checkbox_entry_item cells[] =
104   {
105 #define CS(NAME, LABEL) {#NAME, LABEL},
106     CROSSTABS_CELLS \
107     CS(NONE, N_("None"))
108 #undef CS
109   };
110
111 struct format_options
112 {
113   gboolean avalue;
114   gboolean pivot;
115   gboolean table;
116 };
117
118 struct crosstabs_dialog
119 {
120   GtkTreeView *row_vars;
121   GtkTreeView *col_vars;
122   PsppireDict *dict;
123
124   GtkToggleButton *table_button;
125   GtkToggleButton *pivot_button;
126
127   GtkWidget *format_dialog;
128   GtkWidget *cell_dialog;
129   GtkWidget *stat_dialog;
130
131   GtkToggleButton  *avalue;
132   GtkTreeModel *stat;
133   GtkTreeModel *cell;
134
135   GtkWidget *stat_view;
136   GtkWidget *cell_view;
137   struct format_options current_opts;
138 };
139
140 static void
141 refresh (PsppireDialog *dialog, struct crosstabs_dialog *cd)
142 {
143   GtkTreeModel *liststore = gtk_tree_view_get_model (cd->row_vars);
144   gtk_list_store_clear (GTK_LIST_STORE (liststore));
145   
146   liststore = gtk_tree_view_get_model (cd->col_vars);
147   gtk_list_store_clear (GTK_LIST_STORE (liststore));
148 }
149 static void
150 on_format_clicked (struct crosstabs_dialog *cd)
151 {
152   int ret;
153
154   if (cd->current_opts.avalue)
155     {
156       gtk_toggle_button_set_active (cd->avalue, TRUE);
157     }
158   if (cd->current_opts.table)
159     {
160       gtk_toggle_button_set_active (cd->table_button, TRUE);
161     }
162   if (cd->current_opts.pivot)
163     {
164       gtk_toggle_button_set_active (cd->pivot_button, TRUE);
165     }
166
167   ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->format_dialog));
168
169   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
170     {
171       cd->current_opts.avalue = (gtk_toggle_button_get_active (cd->avalue) == TRUE ) 
172         ? TRUE : FALSE;
173       cd->current_opts.table = (gtk_toggle_button_get_active (cd->table_button) == TRUE)
174         ? TRUE : FALSE;
175       cd->current_opts.pivot = (gtk_toggle_button_get_active (cd->pivot_button) == TRUE)
176         ? TRUE : FALSE;
177     }
178 }
179
180 static void
181 on_statistics_clicked (struct crosstabs_dialog *cd)
182 {
183   GtkListStore *liststore;
184   int ret;
185
186   liststore = clone_list_store (GTK_LIST_STORE (cd->stat));
187
188   ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->stat_dialog));
189
190   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
191     {
192       g_object_unref (liststore);
193     }
194   else
195     {
196       g_object_unref (cd->stat);
197       gtk_tree_view_set_model (GTK_TREE_VIEW (cd->stat_view) , GTK_TREE_MODEL (liststore));
198       cd->stat = GTK_TREE_MODEL (liststore);
199     }
200 }
201 static void
202 on_cell_clicked (struct crosstabs_dialog *cd)
203 {
204   GtkListStore *liststore;
205   int ret;
206
207   liststore = clone_list_store (GTK_LIST_STORE (cd->cell));
208
209   ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->cell_dialog));
210
211   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
212     {
213       g_object_unref (liststore);
214     }
215   else
216     {
217       g_object_unref (cd->cell);
218       gtk_tree_view_set_model (GTK_TREE_VIEW (cd->cell_view) , GTK_TREE_MODEL (liststore));
219       cd->cell = GTK_TREE_MODEL (liststore);
220     }
221 }
222
223 static char *
224 generate_syntax (const struct crosstabs_dialog *cd)
225 {
226   gint i;
227   int n;
228   guint selected;
229   GtkTreeIter iter;
230   gboolean ok;
231
232   gchar *text;
233   GString *string = g_string_new ("CROSSTABS");
234
235   g_string_append (string, "\n\t/TABLES=");
236   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->row_vars), 0, string);
237   g_string_append (string, "\tBY\t");
238   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->col_vars), 0, string);
239
240   g_string_append (string, "\n\t/FORMAT=");
241
242   if (cd->current_opts.avalue)
243     {
244       g_string_append (string, "AVALUE");
245     }
246   else 
247     {
248       g_string_append (string, "DVALUE");
249     }
250   g_string_append (string, " ");
251   if (cd->current_opts.table)
252     g_string_append (string, "TABLES");
253   else
254     g_string_append (string, "NOTABLES");
255   g_string_append (string, " ");
256
257   if (cd->current_opts.pivot)
258     g_string_append (string, "PIVOT");
259   else 
260     g_string_append (string, "NOPIVOT");
261
262   selected = 0;
263   for (i = 0, ok = gtk_tree_model_get_iter_first (cd->stat, &iter); ok; 
264        i++, ok = gtk_tree_model_iter_next (cd->stat, &iter))
265     {
266       gboolean toggled;
267       gtk_tree_model_get (cd->stat, &iter,
268                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
269       if (toggled) 
270         selected |= 1u << i; 
271       else 
272         selected &= ~(1u << i);
273     }
274
275   if (!(selected & (1u << CS_STATS_NONE)))
276     {
277       if (selected)
278         {
279           g_string_append (string, "\n\t/STATISTICS=");
280           n = 0;
281           for (i = 0; i < N_CROSSTABS_STATS; i++)
282             if (selected & (1u << i))
283               {
284                 if (n++)
285                   g_string_append (string, " ");
286                 g_string_append (string, stats[i].name);
287               }
288         }
289     }
290
291   selected = 0;
292   for (i = 0, ok = gtk_tree_model_get_iter_first (cd->cell, &iter); ok; 
293        i++, ok = gtk_tree_model_iter_next (cd->cell, &iter))
294     {
295       gboolean toggled;
296       gtk_tree_model_get (cd->cell, &iter,
297                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
298       if (toggled) 
299         selected |= 1u << i; 
300       else 
301         selected &= ~(1u << i);
302     }
303
304   g_string_append (string, "\n\t/CELLS=");
305   if (selected & (1u << CS_CELLS_NONE))
306     g_string_append (string, "NONE");
307   else
308     {
309       n = 0;
310       for (i = 0; i < N_CROSSTABS_CELLS; i++)
311         if (selected & (1u << i))
312           {
313             if (n++)
314               g_string_append (string, " ");
315             g_string_append (string, cells[i].name);
316           }
317     }
318   
319   g_string_append (string, ".\n");
320
321   text = string->str;
322
323   g_string_free (string, FALSE);
324
325   return text;
326 }
327
328 /* Dialog is valid iff at least one row and one column variable has
329    been selected. */
330 static gboolean
331 dialog_state_valid (gpointer data)
332 {
333   struct crosstabs_dialog *cd = data;
334
335   GtkTreeModel *row_vars = gtk_tree_view_get_model (cd->row_vars);
336   GtkTreeModel *col_vars = gtk_tree_view_get_model (cd->col_vars);
337
338   GtkTreeIter notused;
339
340   return (gtk_tree_model_get_iter_first (row_vars, &notused) 
341     && gtk_tree_model_get_iter_first (col_vars, &notused));
342 }
343
344 /* Pops up the Crosstabs dialog box */
345 void
346 crosstabs_dialog (PsppireDataWindow *de)
347 {
348   gint response;
349   struct crosstabs_dialog cd;
350
351   GtkBuilder *xml = builder_new ("crosstabs.ui");
352   PsppireVarStore *vs = NULL;
353   PsppireDict *dict = NULL;
354
355
356   GtkWidget *dialog = get_widget_assert   (xml, "crosstabs-dialog");
357   GtkWidget *source = get_widget_assert   (xml, "dict-treeview");
358   GtkWidget *dest_rows =   get_widget_assert   (xml, "rows");
359   GtkWidget *dest_cols =   get_widget_assert   (xml, "cols");
360   GtkWidget *format_button = get_widget_assert (xml, "format-button");
361   GtkWidget *stat_button = get_widget_assert (xml, "stats-button");
362   GtkWidget *cell_button = get_widget_assert (xml, "cell-button");
363
364
365   cd.stat_view = get_widget_assert (xml, "stats-view");
366   cd.cell_view = get_widget_assert (xml, "cell-view");
367
368   g_object_get (de->data_editor, "var-store", &vs, NULL);
369
370   put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.stat_view),
371                                   B_CS_STATS_DEFAULT,
372                                   N_CROSSTABS_STATS,
373                                   stats
374                                   );
375   put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.cell_view),
376                                   B_CS_CELL_DEFAULT,
377                                   N_CROSSTABS_CELLS,
378                                   cells
379                                   );
380
381   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
382
383   g_object_get (vs, "dictionary", &dict, NULL);
384   g_object_set (source, "model", dict, NULL);
385
386   cd.row_vars = GTK_TREE_VIEW (dest_rows);
387   cd.col_vars = GTK_TREE_VIEW (dest_cols);
388   g_object_get (vs, "dictionary", &cd.dict, NULL);
389   cd.format_dialog = get_widget_assert (xml, "format-dialog");
390   cd.table_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "print-tables"));
391   cd.pivot_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pivot"));
392   cd.stat_dialog = get_widget_assert (xml, "stat-dialog");
393   cd.cell_dialog = get_widget_assert (xml, "cell-dialog");
394
395   cd.stat = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.stat_view));
396   cd.cell = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.cell_view));
397   cd.avalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "ascending"));
398   cd.current_opts.avalue = TRUE;
399   cd.current_opts.table = TRUE;
400   cd.current_opts.pivot = TRUE;
401
402   gtk_window_set_transient_for (GTK_WINDOW (cd.format_dialog), GTK_WINDOW (de));
403   gtk_window_set_transient_for (GTK_WINDOW (cd.cell_dialog), GTK_WINDOW (de));
404   gtk_window_set_transient_for (GTK_WINDOW (cd.stat_dialog), GTK_WINDOW (de));
405
406   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &cd);
407
408   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
409                                       dialog_state_valid, &cd);
410
411   g_signal_connect_swapped (format_button, "clicked",
412                             G_CALLBACK (on_format_clicked),  &cd);
413   g_signal_connect_swapped (stat_button, "clicked",
414                             G_CALLBACK (on_statistics_clicked),  &cd);
415   g_signal_connect_swapped (cell_button, "clicked",
416                             G_CALLBACK (on_cell_clicked),  &cd);
417
418   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
419
420
421   switch (response)
422     {
423     case GTK_RESPONSE_OK:
424       g_free (execute_syntax_string (de, generate_syntax (&cd)));
425       break;
426     case PSPPIRE_RESPONSE_PASTE:
427       g_free (paste_syntax_to_window (generate_syntax (&cd)));
428       break;
429     default:
430       break;
431     }
432
433   g_object_unref (xml);
434 }