Added crosstabs dialog
[pspp-builds.git] / src / ui / gui / crosstabs-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008  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
22 #include <gtk/gtk.h>
23 #include <gtksheet/gtksheet.h>
24 #include <stdlib.h>
25
26 #include <language/syntax-string-source.h>
27 #include <ui/gui/data-editor.h>
28 #include <ui/gui/dialog-common.h>
29 #include <ui/gui/dict-display.h>
30 #include <ui/gui/helper.h>
31 #include <ui/gui/psppire-dialog.h>
32 #include <ui/gui/psppire-var-store.h>
33 #include <ui/gui/syntax-editor.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
38
39
40 #define CROSSTABS_STATS                       \
41   CS (CHISQ, N_("Chisq"))                         \
42   CS (PHI, N_("Phi"))         \
43   CS (CC, N_("CC"))                   \
44   CS (LAMBDA, N_("Lambda"))                   \
45   CS (UC, N_("UC")) \
46   CS (BTAU, N_("BTau"))                 \
47   CS (CTAU, N_("CTau"))                 \
48   CS (RISK, N_("Risk"))  \
49   CS (GAMMA, N_("Gamma"))                       \
50   CS (D, N_("D"))                         \
51   CS (KAPPA, N_("Kappa"))                 \
52   CS (ETA, N_("Eta"))  \
53   CS (CORR, N_("Corr")) \
54   CS (STATS_NONE, N_("None"))
55
56 #define CROSSTABS_CELLS \
57   CS (COUNT, N_("Count"))   \
58   CS (ROW, N_("Row"))   \
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"))
66
67 enum
68   {
69 #define CS(NAME, LABEL) CS_##NAME,
70     CROSSTABS_STATS
71 #undef CS
72     N_CROSSTABS_STATS
73   };
74
75 enum
76   {
77 #define CS(NAME, LABEL) CS_##NAME,
78     CROSSTABS_CELLS
79 #undef CS
80     N_CROSSTABS_CELLS
81   };
82
83 enum
84   {
85 #define CS(NAME, LABEL) B_CS_##NAME = 1u << CS_##NAME,
86     CROSSTABS_STATS
87     CROSSTABS_CELLS
88 #undef CS
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,
93     B_CS_NONE
94   };
95
96 static const struct checkbox_entry_item stats[] =
97   {
98 #define CS(NAME, LABEL) {#NAME, LABEL},
99     CROSSTABS_STATS \
100     CS(NONE, N_("None"))
101 #undef CS
102   };
103
104 static const struct checkbox_entry_item cells[] =
105   {
106 #define CS(NAME, LABEL) {#NAME, LABEL},
107     CROSSTABS_CELLS \
108     CS(NONE, N_("None"))
109 #undef CS
110   };
111
112 enum
113   {
114     LABEL,
115     NO_LABEL,
116     NO_VAL_LABEL,
117   };
118 struct format_options
119 {
120   gboolean avalue;
121   gboolean pivot;
122   gboolean table;
123 };
124
125 struct crosstabs_dialog
126 {
127   GtkTreeView *row_vars;
128   GtkTreeView *col_vars;
129   PsppireDict *dict;
130
131   GtkToggleButton *table_button;
132   GtkToggleButton *pivot_button;
133
134   GtkWidget *format_dialog;
135   GtkWidget *cell_dialog;
136   GtkWidget *stat_dialog;
137
138   GtkToggleButton  *avalue;
139   GtkTreeModel *stat;
140   GtkTreeModel *cell;
141
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;
148 };
149
150 static void
151 refresh (PsppireDialog *dialog, struct crosstabs_dialog *cd)
152 {
153   GtkTreeModel *liststore = gtk_tree_view_get_model (cd->row_vars);
154   gtk_list_store_clear (GTK_LIST_STORE (liststore));
155   
156   liststore = gtk_tree_view_get_model (cd->col_vars);
157   gtk_list_store_clear (GTK_LIST_STORE (liststore));
158 }
159 static void
160 on_format_clicked (struct crosstabs_dialog *cd)
161 {
162   int ret;
163   gboolean lab;
164   gboolean no_lab;
165   gboolean no_val_lab;
166
167   if (cd->current_opts.avalue)
168     {
169       gtk_toggle_button_set_active (cd->avalue, TRUE);
170     }
171   if (cd->current_opts.table)
172     {
173       gtk_toggle_button_set_active (cd->table_button, TRUE);
174     }
175   if (cd->current_opts.pivot)
176     {
177       gtk_toggle_button_set_active (cd->pivot_button, TRUE);
178     }
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);
182   if (!lab)
183     if (!no_lab)
184       if (!no_val_lab)
185         gtk_toggle_button_set_active (cd->label, TRUE);
186
187
188   ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->format_dialog));
189
190   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
191     {
192       cd->current_opts.avalue = (gtk_toggle_button_get_active (cd->avalue) == TRUE ) 
193         ? TRUE : FALSE;
194       cd->current_opts.table = (gtk_toggle_button_get_active (cd->table_button) == TRUE)
195         ? TRUE : FALSE;
196       cd->current_opts.pivot = (gtk_toggle_button_get_active (cd->pivot_button) == TRUE)
197         ? TRUE : FALSE;
198     }
199   else
200     {
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);
204     }
205 }
206
207 static void
208 on_statistics_clicked (struct crosstabs_dialog *cd)
209 {
210   GtkListStore *liststore;
211   int ret;
212
213   liststore = clone_list_store (GTK_LIST_STORE (cd->stat));
214
215   ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->stat_dialog));
216
217   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
218     {
219       g_object_unref (liststore);
220     }
221   else
222     {
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);
226     }
227 }
228 static void
229 on_cell_clicked (struct crosstabs_dialog *cd)
230 {
231   GtkListStore *liststore;
232   int ret;
233
234   liststore = clone_list_store (GTK_LIST_STORE (cd->cell));
235
236   ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->cell_dialog));
237
238   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
239     {
240       g_object_unref (liststore);
241     }
242   else
243     {
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);
247     }
248 }
249
250 static char *
251 generate_syntax (const struct crosstabs_dialog *cd)
252 {
253   gint i;
254   int n;
255   guint selected;
256   GtkTreeIter iter;
257   gboolean ok;
258
259   gchar *text;
260   GString *string = g_string_new ("CROSSTABS");
261
262   g_string_append (string, "\n\t/TABLES=");
263   append_variable_names (string, cd->dict, GTK_TREE_VIEW (cd->row_vars), 0);
264   g_string_append (string, "\tBY\t");
265   append_variable_names (string, cd->dict, GTK_TREE_VIEW (cd->col_vars), 0);
266
267   g_string_append (string, "\n\t/FORMAT=");
268
269   if (cd->current_opts.avalue)
270     {
271       g_string_append (string, "AVALUE");
272     }
273   else 
274     {
275       g_string_append (string, "DVALUE");
276     }
277   g_string_append (string, " ");
278   if (gtk_toggle_button_get_active (cd->label))
279     {
280       g_string_append (string, "LABELS");
281     }
282   else if (gtk_toggle_button_get_active (cd->no_label))
283     {
284       g_string_append (string, "NOLABELS");
285     }
286   else if (gtk_toggle_button_get_active (cd->no_val_label))
287     {
288       g_string_append (string, "NOVALLABS");
289     }
290   g_string_append (string, " ");
291   if (cd->current_opts.table)
292     g_string_append (string, "TABLES");
293   else
294     g_string_append (string, "NOTABLES");
295   g_string_append (string, " ");
296
297   if (cd->current_opts.pivot)
298     g_string_append (string, "PIVOT");
299   else 
300     g_string_append (string, "NOPIVOT");
301
302   selected = 0;
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))
305     {
306       gboolean toggled;
307       gtk_tree_model_get (cd->stat, &iter,
308                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
309       if (toggled) 
310         selected |= 1u << i; 
311       else 
312         selected &= ~(1u << i);
313     }
314
315   if (!(selected & (1u << CS_STATS_NONE)))
316     {
317       if (selected)
318         {
319           g_string_append (string, "\n\t/STATISTICS=");
320           n = 0;
321           for (i = 0; i < N_CROSSTABS_STATS; i++)
322             if (selected & (1u << i))
323               {
324                 if (n++)
325                   g_string_append (string, " ");
326                 g_string_append (string, stats[i].name);
327               }
328         }
329     }
330
331   selected = 0;
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))
334     {
335       gboolean toggled;
336       gtk_tree_model_get (cd->cell, &iter,
337                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
338       if (toggled) 
339         selected |= 1u << i; 
340       else 
341         selected &= ~(1u << i);
342     }
343
344   g_string_append (string, "\n\t/CELLS=");
345   if (selected & (1u << CS_CELLS_NONE))
346     g_string_append (string, "NONE");
347   else
348     {
349       n = 0;
350       for (i = 0; i < N_CROSSTABS_CELLS; i++)
351         if (selected & (1u << i))
352           {
353             if (n++)
354               g_string_append (string, " ");
355             g_string_append (string, cells[i].name);
356           }
357     }
358   
359   g_string_append (string, ".\n");
360
361   text = string->str;
362
363   g_string_free (string, FALSE);
364
365   return text;
366 }
367
368 /* Dialog is valid iff at least one row and one column variable has
369    been selected. */
370 static gboolean
371 dialog_state_valid (gpointer data)
372 {
373   struct crosstabs_dialog *cd = data;
374
375   GtkTreeModel *row_vars = gtk_tree_view_get_model (cd->row_vars);
376   GtkTreeModel *col_vars = gtk_tree_view_get_model (cd->col_vars);
377
378   GtkTreeIter notused;
379
380   return (gtk_tree_model_get_iter_first (row_vars, &notused) 
381     && gtk_tree_model_get_iter_first (col_vars, &notused));
382 }
383
384 /* Pops up the Crosstabs dialog box */
385 void
386 crosstabs_dialog (GObject *o, gpointer data)
387 {
388   gint response;
389   struct data_editor *de = data;
390
391   struct crosstabs_dialog cd;
392
393   GladeXML *xml = XML_NEW ("crosstabs.glade");
394   GtkSheet *var_sheet;
395   PsppireVarStore *vs;
396
397   GtkWidget *dialog = get_widget_assert   (xml, "crosstabs-dialog");
398   GtkWidget *source = get_widget_assert   (xml, "dict-treeview");
399   GtkWidget *dest_rows =   get_widget_assert   (xml, "rows");
400   GtkWidget *dest_cols =   get_widget_assert   (xml, "cols");
401   GtkWidget *row_selector = get_widget_assert (xml, "row-selector");
402   GtkWidget *col_selector = get_widget_assert (xml, "col-selector");
403   GtkWidget *format_button = get_widget_assert (xml, "format-button");
404   GtkWidget *stat_button = get_widget_assert (xml, "stats-button");
405   GtkWidget *cell_button = get_widget_assert (xml, "cell-button");
406
407
408   cd.stat_view = get_widget_assert (xml, "stats-view");
409   cd.cell_view = get_widget_assert (xml, "cell-view");
410
411   var_sheet = GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
412
413   vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
414
415   put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.stat_view),
416                                   B_CS_STATS_DEFAULT,
417                                   N_CROSSTABS_STATS,
418                                   stats
419                                   );
420   put_checkbox_items_in_treeview (GTK_TREE_VIEW(cd.cell_view),
421                                   B_CS_CELL_DEFAULT,
422                                   N_CROSSTABS_CELLS,
423                                   cells
424                                   );
425
426   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
427
428   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
429                                  vs->dict,
430                                  GTK_SELECTION_MULTIPLE, NULL);
431
432   set_dest_model (GTK_TREE_VIEW (dest_rows), vs->dict);
433   set_dest_model (GTK_TREE_VIEW (dest_cols), vs->dict);
434
435   psppire_selector_set_subjects (PSPPIRE_SELECTOR (row_selector),
436                                  source,
437                                  dest_rows,
438                                  insert_source_row_into_tree_view,
439                                  NULL,
440                                  NULL);
441
442   psppire_selector_set_subjects (PSPPIRE_SELECTOR (col_selector),
443                                  source,
444                                  dest_cols,
445                                  insert_source_row_into_tree_view,
446                                  NULL,
447                                  NULL);
448
449   cd.row_vars = GTK_TREE_VIEW (dest_rows);
450   cd.col_vars = GTK_TREE_VIEW (dest_cols);
451   cd.dict = vs->dict;
452   cd.format_dialog = get_widget_assert (xml, "format-dialog");
453   cd.table_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "print-tables"));
454   cd.pivot_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pivot"));
455   cd.label = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
456   cd.no_label = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton2"));
457   cd.no_val_label = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton3"));
458   cd.stat_dialog = get_widget_assert (xml, "stat-dialog");
459   cd.cell_dialog = get_widget_assert (xml, "cell-dialog");
460
461   cd.stat = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.stat_view));
462   cd.cell = gtk_tree_view_get_model (GTK_TREE_VIEW (cd.cell_view));
463   cd.avalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "ascending"));
464   cd.current_opts.avalue = TRUE;
465   cd.current_opts.table = TRUE;
466   cd.current_opts.pivot = TRUE;
467
468   gtk_window_set_transient_for (GTK_WINDOW (cd.format_dialog), de->parent.window);
469   gtk_window_set_transient_for (GTK_WINDOW (cd.cell_dialog), de->parent.window);
470   gtk_window_set_transient_for (GTK_WINDOW (cd.stat_dialog), de->parent.window);
471
472   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &cd);
473
474   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
475                                       dialog_state_valid, &cd);
476
477   g_signal_connect_swapped (format_button, "clicked",
478                             G_CALLBACK (on_format_clicked),  &cd);
479   g_signal_connect_swapped (stat_button, "clicked",
480                             G_CALLBACK (on_statistics_clicked),  &cd);
481   g_signal_connect_swapped (cell_button, "clicked",
482                             G_CALLBACK (on_cell_clicked),  &cd);
483
484   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
485
486
487   switch (response)
488     {
489     case GTK_RESPONSE_OK:
490       {
491         gchar *syntax = generate_syntax (&cd);
492         struct getl_interface *sss = create_syntax_string_source (syntax);
493         execute_syntax (sss);
494
495         g_free (syntax);
496       }
497       break;
498     case PSPPIRE_RESPONSE_PASTE:
499       {
500         gchar *syntax = generate_syntax (&cd);
501
502         struct syntax_editor *se =
503           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
504
505         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
506
507         g_free (syntax);
508       }
509       break;
510     default:
511       break;
512     }
513
514   g_object_unref (xml);
515 }