Rename psppire_dialog_action_get_pointer -> psppire_dialog_action_get_hash_table
[pspp] / src / ui / gui / psppire-dialog-action-crosstabs.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012  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
18 #include <config.h>
19
20 #include "psppire-dialog-action-crosstabs.h"
21 #include "psppire-value-entry.h"
22
23 #include "dialog-common.h"
24 #include "helper.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
27
28 #include "psppire-dialog.h"
29 #include "builder-wrapper.h"
30 #include "psppire-checkbox-treeview.h"
31 #include "psppire-dict.h"
32 #include "libpspp/str.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 static void
40 psppire_dialog_action_crosstabs_class_init (PsppireDialogActionCrosstabsClass *class);
41
42 G_DEFINE_TYPE (PsppireDialogActionCrosstabs, psppire_dialog_action_crosstabs, PSPPIRE_TYPE_DIALOG_ACTION);
43
44 static gboolean
45 dialog_state_valid (gpointer data)
46 {
47   PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (data);
48
49   GtkTreeModel *row_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_rows));
50   GtkTreeModel *col_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_cols));
51
52   GtkTreeIter notused;
53
54   return (gtk_tree_model_get_iter_first (row_vars, &notused) 
55     && gtk_tree_model_get_iter_first (col_vars, &notused));
56 }
57
58 static void
59 refresh (PsppireDialogAction *rd_)
60 {
61   PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (rd_);
62
63   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_rows));
64   gtk_list_store_clear (GTK_LIST_STORE (liststore));
65   
66   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_cols));
67   gtk_list_store_clear (GTK_LIST_STORE (liststore));
68 }
69
70 #define CROSSTABS_STATS                         \
71   CS (CHISQ, N_("Chisq"))                       \
72   CS (PHI, N_("Phi"))                           \
73   CS (CC, N_("CC"))                             \
74   CS (LAMBDA, N_("Lambda"))                     \
75   CS (UC, N_("UC"))                             \
76   CS (BTAU, N_("BTau"))                         \
77   CS (CTAU, N_("CTau"))                         \
78   CS (RISK, N_("Risk"))                         \
79   CS (GAMMA, N_("Gamma"))                       \
80   CS (D, N_("D"))                               \
81   CS (KAPPA, N_("Kappa"))                       \
82   CS (ETA, N_("Eta"))                           \
83   CS (CORR, N_("Corr"))                         \
84   CS (STATS_NONE, N_("None"))
85
86
87 #define CROSSTABS_CELLS                         \
88   CS (COUNT, N_("Count"))                       \
89   CS (ROW, N_("Row"))                           \
90   CS (COLUMN, N_("Column"))                     \
91   CS (TOTAL, N_("Total"))                       \
92   CS (EXPECTED, N_("Expected"))                 \
93   CS (RESIDUAL, N_("Residual"))                 \
94   CS (SRESIDUAL, N_("Std. Residual"))           \
95   CS (ASRESIDUAL, N_("Adjusted Std. Residual")) \
96   CS (CELLS_NONE, N_("None"))
97
98 enum
99   {
100 #define CS(NAME, LABEL) CS_##NAME,
101     CROSSTABS_STATS
102 #undef CS
103     N_CROSSTABS_STATS
104   };
105
106 enum
107   {
108 #define CS(NAME, LABEL) CS_##NAME,
109     CROSSTABS_CELLS
110 #undef CS
111     N_CROSSTABS_CELLS
112   };
113
114 enum
115   {
116 #define CS(NAME, LABEL) B_CS_##NAME = 1u << CS_##NAME,
117     CROSSTABS_STATS
118     CROSSTABS_CELLS
119 #undef CS
120     B_CS_STATS_ALL = (1u << N_CROSSTABS_STATS) - 1,
121     B_CS_CELLS_ALL = (1u << N_CROSSTABS_CELLS) - 1,
122     B_CS_STATS_DEFAULT = B_CS_CHISQ,
123     B_CS_CELL_DEFAULT = B_CS_COUNT | B_CS_ROW | B_CS_COLUMN | B_CS_TOTAL,
124     B_CS_NONE
125   };
126
127 static const struct checkbox_entry_item stats[] =
128   {
129 #define CS(NAME, LABEL) {#NAME, LABEL},
130     CROSSTABS_STATS \
131     CS(NONE, N_("None"))
132 #undef CS
133   };
134
135 static const struct checkbox_entry_item cells[] =
136   {
137 #define CS(NAME, LABEL) {#NAME, LABEL},
138     CROSSTABS_CELLS \
139     CS(NONE, N_("None"))
140 #undef CS
141   };
142
143 static void
144 on_format_clicked (PsppireDialogActionCrosstabs *cd)
145 {
146   int ret;
147
148   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cd->avalue_button), cd->format_options_avalue);
149   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cd->table_button), cd->format_options_table);
150   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cd->pivot_button), cd->format_options_pivot);
151
152   ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->format_dialog));
153
154   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
155     {
156       cd->format_options_avalue =
157         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->avalue_button));
158
159       cd->format_options_table =
160         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->table_button));
161
162       cd->format_options_pivot = 
163         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->pivot_button));
164     }
165 }
166
167 static void
168 on_cell_clicked (PsppireDialogActionCrosstabs *cd)
169 {
170   GtkListStore *liststore = clone_list_store (GTK_LIST_STORE (cd->cell));
171
172   gint ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->cell_dialog));
173
174   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
175     {
176       g_object_unref (liststore);
177     }
178   else
179     {
180       gtk_tree_view_set_model (GTK_TREE_VIEW (cd->cell_view) , GTK_TREE_MODEL (liststore));
181       cd->cell = GTK_TREE_MODEL (liststore);
182     }
183 }
184
185
186 static void
187 on_statistics_clicked (PsppireDialogActionCrosstabs *cd)
188 {
189   GtkListStore *liststore = clone_list_store (GTK_LIST_STORE (cd->stat));
190
191   gint ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->stat_dialog));
192
193   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
194     {
195       g_object_unref (liststore);
196     }
197   else
198     {
199       gtk_tree_view_set_model (GTK_TREE_VIEW (cd->stat_view) , GTK_TREE_MODEL (liststore));
200       cd->stat = GTK_TREE_MODEL (liststore);
201     }
202 }
203
204
205 static void
206 psppire_dialog_action_crosstabs_activate (GtkAction *a)
207 {
208   PsppireDialogActionCrosstabs *act = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
209   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
210
211   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
212   GtkBuilder *xml = g_hash_table_lookup (thing, a);
213   if (!xml)
214     {
215       xml = builder_new ("crosstabs.ui");
216       g_hash_table_insert (thing, a, xml);
217
218       pda->dialog = get_widget_assert   (xml, "crosstabs-dialog");
219       pda->source = get_widget_assert   (xml, "dict-treeview");
220
221       act->dest_rows =   get_widget_assert   (xml, "rows");
222       act->dest_cols =   get_widget_assert   (xml, "cols");
223       act->format_button = get_widget_assert (xml, "format-button");
224       act->stat_button = get_widget_assert (xml, "stats-button");
225       act->cell_button = get_widget_assert (xml, "cell-button");
226       act->stat_view =   get_widget_assert (xml, "stats-view");
227       act->cell_view =   get_widget_assert (xml, "cell-view");
228       act->cell_dialog = get_widget_assert (xml, "cell-dialog");
229       act->stat_dialog = get_widget_assert (xml, "stat-dialog");
230       act->format_dialog = get_widget_assert (xml, "format-dialog");
231
232       act->avalue_button = get_widget_assert (xml, "ascending");
233       act->table_button = get_widget_assert (xml, "print-tables");
234       act->pivot_button = get_widget_assert (xml, "pivot");
235
236       act->format_options_avalue = TRUE;
237       act->format_options_table = TRUE;
238       act->format_options_pivot = TRUE;
239
240       psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->cell_view),
241                                           B_CS_CELL_DEFAULT,
242                                           N_CROSSTABS_CELLS,
243                                           cells);
244
245       act->cell = gtk_tree_view_get_model (GTK_TREE_VIEW (act->cell_view));
246
247       psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
248                                           B_CS_STATS_DEFAULT,
249                                           N_CROSSTABS_STATS,
250                                           stats);
251
252       act->stat = gtk_tree_view_get_model (GTK_TREE_VIEW (act->stat_view));
253
254       psppire_dialog_action_set_refresh (pda, refresh);
255
256       psppire_dialog_action_set_valid_predicate (pda,
257                                                  dialog_state_valid);
258
259       g_signal_connect_swapped (act->cell_button, "clicked",
260                                 G_CALLBACK (on_cell_clicked), act);
261
262       g_signal_connect_swapped (act->stat_button, "clicked",
263                                 G_CALLBACK (on_statistics_clicked), act);
264
265       g_signal_connect_swapped (act->format_button, "clicked",
266                                 G_CALLBACK (on_format_clicked), act);
267
268     }
269   
270   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_crosstabs_parent_class)->activate)
271     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_crosstabs_parent_class)->activate (pda);
272 }
273
274
275
276 static char *
277 generate_syntax (PsppireDialogAction *a)
278 {
279   PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
280   gchar *text = NULL;
281   int i, n;
282   guint selected;
283   GString *string = g_string_new ("CROSSTABS ");
284   gboolean ok;
285   GtkTreeIter iter;
286
287   g_string_append (string, "\n\t/TABLES=");
288   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->dest_rows), 0, string);
289   g_string_append (string, "\tBY\t");
290   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->dest_cols), 0, string);
291
292
293   g_string_append (string, "\n\t/FORMAT=");
294
295   if (cd->format_options_avalue)
296     g_string_append (string, "AVALUE");
297   else 
298     g_string_append (string, "DVALUE");
299   g_string_append (string, " ");
300
301   if (cd->format_options_table)
302     g_string_append (string, "TABLES");
303   else
304     g_string_append (string, "NOTABLES");
305   g_string_append (string, " ");
306
307   if (cd->format_options_pivot)
308     g_string_append (string, "PIVOT");
309   else 
310     g_string_append (string, "NOPIVOT");
311
312
313   selected = 0;
314   for (i = 0, ok = gtk_tree_model_get_iter_first (cd->stat, &iter); ok; 
315        i++, ok = gtk_tree_model_iter_next (cd->stat, &iter))
316     {
317       gboolean toggled;
318       gtk_tree_model_get (cd->stat, &iter,
319                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
320       if (toggled) 
321         selected |= 1u << i; 
322       else 
323         selected &= ~(1u << i);
324     }
325
326   if (!(selected & (1u << CS_STATS_NONE)))
327     {
328       if (selected)
329         {
330           g_string_append (string, "\n\t/STATISTICS=");
331           n = 0;
332           for (i = 0; i < N_CROSSTABS_STATS; i++)
333             if (selected & (1u << i))
334               {
335                 if (n++)
336                   g_string_append (string, " ");
337                 g_string_append (string, stats[i].name);
338               }
339         }
340     }
341
342   selected = 0;
343   for (i = 0, ok = gtk_tree_model_get_iter_first (cd->cell, &iter); ok; 
344        i++, ok = gtk_tree_model_iter_next (cd->cell, &iter))
345     {
346       gboolean toggled;
347       gtk_tree_model_get (cd->cell, &iter,
348                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
349       if (toggled) 
350         selected |= 1u << i; 
351       else 
352         selected &= ~(1u << i);
353     }
354
355
356
357   g_string_append (string, "\n\t/CELLS=");
358   if (selected & (1u << CS_CELLS_NONE))
359     g_string_append (string, "NONE");
360   else
361     {
362       n = 0;
363       for (i = 0; i < N_CROSSTABS_CELLS; i++)
364         if (selected & (1u << i))
365           {
366             if (n++)
367               g_string_append (string, " ");
368             g_string_append (string, cells[i].name);
369           }
370     }
371
372   g_string_append (string, ".\n");
373
374   text = string->str;
375
376   g_string_free (string, FALSE);
377
378   return text;
379 }
380
381 static void
382 psppire_dialog_action_crosstabs_class_init (PsppireDialogActionCrosstabsClass *class)
383 {
384   psppire_dialog_action_set_activation (class, psppire_dialog_action_crosstabs_activate);
385   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
386 }
387
388
389 static void
390 psppire_dialog_action_crosstabs_init (PsppireDialogActionCrosstabs *act)
391 {
392 }
393