New module psppire-var-view
[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 #include "psppire-var-view.h"
22
23 #include <gtk/gtk.h>
24 #include <stdlib.h>
25
26 #include <language/syntax-string-source.h>
27 #include <ui/gui/psppire-data-window.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 "executor.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   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (fd->stat_vars), 0, string);
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   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
312
313   struct frequencies_dialog fd;
314
315   GtkBuilder *xml = builder_new ("frequencies.ui");
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 *format_button = get_widget_assert (xml, "button1");
321   GtkWidget *stats_treeview = get_widget_assert (xml, "stats-treeview");
322
323   PsppireVarStore *vs = NULL;
324
325   g_object_get (de->data_editor, "var-store", &vs, NULL);
326
327   put_checkbox_items_in_treeview (GTK_TREE_VIEW(stats_treeview),
328                                   B_FS_DEFAULT,
329                                   N_FREQUENCY_STATS,
330                                   stats
331                                   );
332
333
334   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
335
336   g_object_get (vs, "dictionary", &fd.dict, NULL);
337   g_object_set (source, "model", fd.dict, NULL);
338
339   fd.stat_vars = GTK_TREE_VIEW (dest);
340   fd.table_button = get_widget_assert (xml, "checkbutton1");
341   fd.format_dialog = get_widget_assert (xml, "format-dialog");
342   fd.maximum_cats = get_widget_assert (xml, "hbox5");
343   fd.limit_toggle_button = get_widget_assert (xml, "checkbutton2");
344   fd.limit_spinbutton =
345     GTK_SPIN_BUTTON (get_widget_assert (xml, "spinbutton1"));
346
347   fd.stats = gtk_tree_view_get_model (GTK_TREE_VIEW (stats_treeview));
348
349   fd.avalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
350   fd.dvalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton2"));
351   fd.afreq  = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton3"));
352   fd.dfreq  = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton4"));
353
354   fd.current_opts.order = FRQ_AVALUE;
355   fd.current_opts.use_limits = FALSE;
356   fd.current_opts.limit = 50;
357
358
359   gtk_window_set_transient_for (GTK_WINDOW (fd.format_dialog), GTK_WINDOW (de));
360
361
362   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &fd);
363
364   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
365                                       dialog_state_valid, &fd);
366
367
368   g_signal_connect_swapped (format_button, "clicked",
369                             G_CALLBACK (on_format_clicked),  &fd);
370
371   g_signal_connect (fd.limit_toggle_button, "toggled",
372                     G_CALLBACK (sensitive_if_active), fd.maximum_cats);
373
374
375   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
376
377
378   switch (response)
379     {
380     case GTK_RESPONSE_OK:
381       {
382         gchar *syntax = generate_syntax (&fd);
383
384         struct getl_interface *sss = create_syntax_string_source (syntax);
385         execute_syntax (sss);
386
387         g_free (syntax);
388       }
389       break;
390     case PSPPIRE_RESPONSE_PASTE:
391       {
392         gchar *syntax = generate_syntax (&fd);
393         paste_syntax_in_new_window (syntax);
394         g_free (syntax);
395       }
396       break;
397     default:
398       break;
399     }
400
401   g_object_unref (xml);
402 }