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