Implemented the paired samples t test dialog. Closes patch #6378
[pspp-builds.git] / src / ui / gui / frequencies-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007  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 "frequencies-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 FREQUENCY_STATS                       \
41   FS (MEAN, N_("Mean"))                         \
42   FS (STDDEV, N_("Standard deviation"))         \
43   FS (MINIMUM, N_("Minimum"))                   \
44   FS (MAXIMUM, N_("Maximum"))                   \
45   FS (SEMEAN, N_("Standard error of the mean")) \
46   FS (VARIANCE, N_("Variance"))                 \
47   FS (SKEWNESS, N_("Skewness"))                 \
48   FS (SESKEW, N_("Standard error of the skewness"))  \
49   FS (RANGE, N_("Range"))                       \
50   FS (MODE, N_("Mode"))                         \
51   FS (KURTOSIS, N_("Kurtosis"))                 \
52   FS (SEKURT, N_("Standard error of the kurtosis"))  \
53   FS (MEDIAN, N_("Median"))      \
54   FS (SUM, N_("Sum"))
55
56 enum
57   {
58 #define FS(NAME, LABEL) FS_##NAME,
59     FREQUENCY_STATS
60 #undef FS
61     N_FREQUENCY_STATS
62   };
63
64 enum
65   {
66 #define FS(NAME, LABEL) B_FS_##NAME = 1u << FS_##NAME,
67     FREQUENCY_STATS
68 #undef FS
69     B_FS_ALL = (1u << N_FREQUENCY_STATS) - 1,
70     B_FS_DEFAULT = B_FS_MEAN | B_FS_STDDEV | B_FS_MINIMUM | B_FS_MAXIMUM
71   };
72
73
74 static const struct checkbox_entry_item stats[] =
75   {
76 #define FS(NAME, LABEL) {#NAME, LABEL},
77     FREQUENCY_STATS
78 #undef FS
79   };
80
81
82
83 enum frq_order
84   {
85     FRQ_AVALUE,
86     FRQ_DVALUE,
87     FRQ_ACOUNT,
88     FRQ_DCOUNT
89   };
90
91 struct format_options
92 {
93   enum frq_order order;
94   gboolean use_limits;
95   int limit;
96 };
97
98 struct frequencies_dialog
99 {
100   GtkTreeView *stat_vars;
101   PsppireDict *dict;
102
103   GtkWidget *table_button;
104
105   GtkWidget *format_dialog;
106   GtkWidget *maximum_cats;
107   GtkWidget *limit_toggle_button;
108   GtkSpinButton *limit_spinbutton;
109
110   GtkToggleButton  *avalue;
111   GtkToggleButton  *dvalue;
112   GtkToggleButton  *afreq;
113   GtkToggleButton  *dfreq;
114
115   struct format_options current_opts;
116
117   GtkTreeModel *stats;
118 };
119
120 static void
121 refresh (PsppireDialog *dialog, struct frequencies_dialog *fd)
122 {
123   GtkTreeIter iter;
124   size_t i;
125   bool ok;
126
127   GtkTreeModel *liststore = gtk_tree_view_get_model (fd->stat_vars);
128   gtk_list_store_clear (GTK_LIST_STORE (liststore));
129
130   for (i = 0, ok = gtk_tree_model_get_iter_first (fd->stats, &iter); ok;
131        i++, ok = gtk_tree_model_iter_next (fd->stats, &iter))
132     gtk_list_store_set (GTK_LIST_STORE (fd->stats), &iter,
133                         CHECKBOX_COLUMN_SELECTED,
134                         (B_FS_DEFAULT & (1u << i)) ? true : false, -1);
135
136   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->table_button), TRUE);
137 }
138
139 static char *
140 generate_syntax (const struct frequencies_dialog *fd)
141 {
142   GtkTreeIter iter;
143   gboolean ok;
144   gint i;
145   guint selected = 0;
146
147   gchar *text;
148   GString *string = g_string_new ("FREQUENCIES");
149
150   g_string_append (string, "\n\t/VARIABLES=");
151   append_variable_names (string, fd->dict, GTK_TREE_VIEW (fd->stat_vars), 0);
152
153   g_string_append (string, "\n\t/FORMAT=");
154
155   switch (fd->current_opts.order)
156     {
157     case FRQ_AVALUE:
158       g_string_append (string, "AVALUE");
159       break;
160     case FRQ_DVALUE:
161       g_string_append (string, "DVALUE");
162       break;
163     case FRQ_ACOUNT:
164       g_string_append (string, "AFREQ");
165       break;
166     case FRQ_DCOUNT:
167       g_string_append (string, "DFREQ");
168       break;
169     default:
170       g_assert_not_reached();
171     }
172
173   g_string_append (string, " ");
174
175   if ( fd->current_opts.use_limits )
176     {
177       g_string_append_printf (string, "LIMIT (%d)", fd->current_opts.limit);
178     }
179   else
180     {
181       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->table_button)))
182         g_string_append (string, "TABLE");
183       else
184         g_string_append (string, "NOTABLE");
185     }
186
187
188   for (i = 0, ok = gtk_tree_model_get_iter_first (fd->stats, &iter); ok;
189        i++, ok = gtk_tree_model_iter_next (fd->stats, &iter))
190     {
191       gboolean toggled;
192       gtk_tree_model_get (fd->stats, &iter,
193                           CHECKBOX_COLUMN_SELECTED, &toggled, -1);
194       if (toggled)
195         selected |= 1u << i;
196     }
197
198   if (selected != B_FS_DEFAULT)
199     {
200       g_string_append (string, "\n\t/STATISTICS=");
201       if (selected == B_FS_ALL)
202         g_string_append (string, "ALL");
203       else if (selected == 0)
204         g_string_append (string, "NONE");
205       else
206         {
207           int n = 0;
208           if ((selected & B_FS_DEFAULT) == B_FS_DEFAULT)
209             {
210               g_string_append (string, "DEFAULT");
211               selected &= ~B_FS_DEFAULT;
212               n++;
213             }
214           for (i = 0; i < N_FREQUENCY_STATS; i++)
215             if (selected & (1u << i))
216               {
217                 if (n++)
218                   g_string_append (string, " ");
219                 g_string_append (string, stats[i].name);
220               }
221         }
222     }
223
224   g_string_append (string, ".\n");
225
226   text = string->str;
227
228   g_string_free (string, FALSE);
229
230   return text;
231 }
232
233 /* Dialog is valid iff at least one variable has been selected */
234 static gboolean
235 dialog_state_valid (gpointer data)
236 {
237   struct frequencies_dialog *fd = data;
238
239   GtkTreeModel *vars = gtk_tree_view_get_model (fd->stat_vars);
240
241   GtkTreeIter notused;
242
243   return gtk_tree_model_get_iter_first (vars, &notused);
244 }
245
246
247 static void
248 on_format_clicked (struct frequencies_dialog *fd)
249 {
250   int ret;
251   g_signal_emit_by_name (fd->limit_toggle_button, "toggled");
252
253   switch (fd->current_opts.order)
254     {
255     case FRQ_AVALUE:
256       gtk_toggle_button_set_active (fd->avalue, TRUE);
257       break;
258     case FRQ_DVALUE:
259       gtk_toggle_button_set_active (fd->dvalue, TRUE);
260       break;
261     case FRQ_ACOUNT:
262       gtk_toggle_button_set_active (fd->dfreq, TRUE);
263       break;
264     case FRQ_DCOUNT:
265       gtk_toggle_button_set_active (fd->dfreq, TRUE);
266       break;
267     };
268
269   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->limit_toggle_button),
270                                 fd->current_opts.use_limits);
271
272   gtk_spin_button_set_value (fd->limit_spinbutton,
273                              fd->current_opts.limit);
274
275   ret = psppire_dialog_run (PSPPIRE_DIALOG (fd->format_dialog));
276
277   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
278     {
279       if (gtk_toggle_button_get_active (fd->avalue))
280         fd->current_opts.order = FRQ_AVALUE;
281       else if (gtk_toggle_button_get_active (fd->dvalue))
282         fd->current_opts.order = FRQ_DVALUE;
283       else if (gtk_toggle_button_get_active (fd->afreq))
284         fd->current_opts.order = FRQ_ACOUNT;
285       else if (gtk_toggle_button_get_active (fd->dfreq))
286         fd->current_opts.order = FRQ_DCOUNT;
287
288       fd->current_opts.use_limits = gtk_toggle_button_get_active
289         (GTK_TOGGLE_BUTTON (fd->limit_toggle_button));
290
291       fd->current_opts.limit =
292         gtk_spin_button_get_value (fd->limit_spinbutton);
293     }
294 }
295
296
297 /* Makes widget W's sensitivity follow the active state of TOGGLE */
298 static void
299 sensitive_if_active (GtkToggleButton *toggle, GtkWidget *w)
300 {
301   gboolean active = gtk_toggle_button_get_active (toggle);
302
303   gtk_widget_set_sensitive (w, active);
304 }
305
306 /* Pops up the Frequencies dialog box */
307 void
308 frequencies_dialog (GObject *o, gpointer data)
309 {
310   gint response;
311   struct data_editor *de = data;
312
313   struct frequencies_dialog fd;
314
315   GladeXML *xml = XML_NEW ("frequencies.glade");
316
317   GtkWidget *dialog = get_widget_assert   (xml, "frequencies-dialog");
318   GtkWidget *source = get_widget_assert   (xml, "dict-treeview");
319   GtkWidget *dest =   get_widget_assert   (xml, "var-treeview");
320   GtkWidget *selector = get_widget_assert (xml, "selector1");
321   GtkWidget *format_button = get_widget_assert (xml, "button1");
322   GtkWidget *stats_treeview = get_widget_assert (xml, "stats-treeview");
323
324   GtkSheet *var_sheet =
325     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
326
327   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
328
329   put_checkbox_items_in_treeview (GTK_TREE_VIEW(stats_treeview),
330                                   B_FS_DEFAULT,
331                                   N_FREQUENCY_STATS,
332                                   stats
333                                   );
334
335
336
337
338   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
339
340   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
341                                  vs->dict,
342                                  GTK_SELECTION_MULTIPLE, NULL);
343
344   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
345
346
347   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
348                                  source,
349                                  dest,
350                                  insert_source_row_into_tree_view,
351                                  NULL,
352                                  NULL);
353
354
355   fd.stat_vars = GTK_TREE_VIEW (dest);
356   fd.dict = vs->dict;
357   fd.table_button = get_widget_assert (xml, "checkbutton1");
358   fd.format_dialog = get_widget_assert (xml, "format-dialog");
359   fd.maximum_cats = get_widget_assert (xml, "hbox5");
360   fd.limit_toggle_button = get_widget_assert (xml, "checkbutton2");
361   fd.limit_spinbutton =
362     GTK_SPIN_BUTTON (get_widget_assert (xml, "spinbutton1"));
363
364   fd.stats = gtk_tree_view_get_model (GTK_TREE_VIEW (stats_treeview));
365
366   fd.avalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
367   fd.dvalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton2"));
368   fd.afreq  = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton3"));
369   fd.dfreq  = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton4"));
370
371   fd.current_opts.order = FRQ_AVALUE;
372   fd.current_opts.use_limits = FALSE;
373   fd.current_opts.limit = 50;
374
375
376   gtk_window_set_transient_for (GTK_WINDOW (fd.format_dialog), de->parent.window);
377
378
379   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &fd);
380
381   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
382                                       dialog_state_valid, &fd);
383
384
385   g_signal_connect_swapped (format_button, "clicked",
386                             G_CALLBACK (on_format_clicked),  &fd);
387
388   g_signal_connect (fd.limit_toggle_button, "toggled",
389                     G_CALLBACK (sensitive_if_active), fd.maximum_cats);
390
391
392   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
393
394
395   switch (response)
396     {
397     case GTK_RESPONSE_OK:
398       {
399         gchar *syntax = generate_syntax (&fd);
400         struct getl_interface *sss = create_syntax_string_source (syntax);
401         execute_syntax (sss);
402
403         g_free (syntax);
404       }
405       break;
406     case PSPPIRE_RESPONSE_PASTE:
407       {
408         gchar *syntax = generate_syntax (&fd);
409
410         struct syntax_editor *se =
411           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
412
413         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
414
415         g_free (syntax);
416       }
417       break;
418     default:
419       break;
420     }
421
422   g_object_unref (xml);
423 }