1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 Free Software Foundation
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.
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.
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/>. */
19 #include "checkbox-treeview.h"
20 #include "frequencies-dialog.h"
21 #include "psppire-var-view.h"
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>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
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")) \
58 #define FS(NAME, LABEL) FS_##NAME,
66 #define FS(NAME, LABEL) B_FS_##NAME = 1u << FS_##NAME,
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
74 static const struct checkbox_entry_item stats[] =
76 #define FS(NAME, LABEL) {#NAME, LABEL},
98 struct frequencies_dialog
100 GtkTreeView *stat_vars;
103 GtkWidget *table_button;
105 GtkWidget *format_dialog;
106 GtkWidget *maximum_cats;
107 GtkWidget *limit_toggle_button;
108 GtkSpinButton *limit_spinbutton;
110 GtkToggleButton *avalue;
111 GtkToggleButton *dvalue;
112 GtkToggleButton *afreq;
113 GtkToggleButton *dfreq;
115 struct format_options current_opts;
121 refresh (PsppireDialog *dialog, struct frequencies_dialog *fd)
127 GtkTreeModel *liststore = gtk_tree_view_get_model (fd->stat_vars);
128 gtk_list_store_clear (GTK_LIST_STORE (liststore));
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);
136 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->table_button), TRUE);
140 generate_syntax (const struct frequencies_dialog *fd)
148 GString *string = g_string_new ("FREQUENCIES");
150 g_string_append (string, "\n\t/VARIABLES=");
151 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (fd->stat_vars), 0, string);
153 g_string_append (string, "\n\t/FORMAT=");
155 switch (fd->current_opts.order)
158 g_string_append (string, "AVALUE");
161 g_string_append (string, "DVALUE");
164 g_string_append (string, "AFREQ");
167 g_string_append (string, "DFREQ");
170 g_assert_not_reached();
173 g_string_append (string, " ");
175 if ( fd->current_opts.use_limits )
177 g_string_append_printf (string, "LIMIT (%d)", fd->current_opts.limit);
181 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->table_button)))
182 g_string_append (string, "TABLE");
184 g_string_append (string, "NOTABLE");
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))
192 gtk_tree_model_get (fd->stats, &iter,
193 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
198 if (selected != B_FS_DEFAULT)
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");
208 if ((selected & B_FS_DEFAULT) == B_FS_DEFAULT)
210 g_string_append (string, "DEFAULT");
211 selected &= ~B_FS_DEFAULT;
214 for (i = 0; i < N_FREQUENCY_STATS; i++)
215 if (selected & (1u << i))
218 g_string_append (string, " ");
219 g_string_append (string, stats[i].name);
224 g_string_append (string, ".\n");
228 g_string_free (string, FALSE);
233 /* Dialog is valid iff at least one variable has been selected */
235 dialog_state_valid (gpointer data)
237 struct frequencies_dialog *fd = data;
239 GtkTreeModel *vars = gtk_tree_view_get_model (fd->stat_vars);
243 return gtk_tree_model_get_iter_first (vars, ¬used);
248 on_format_clicked (struct frequencies_dialog *fd)
251 g_signal_emit_by_name (fd->limit_toggle_button, "toggled");
253 switch (fd->current_opts.order)
256 gtk_toggle_button_set_active (fd->avalue, TRUE);
259 gtk_toggle_button_set_active (fd->dvalue, TRUE);
262 gtk_toggle_button_set_active (fd->dfreq, TRUE);
265 gtk_toggle_button_set_active (fd->dfreq, TRUE);
269 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->limit_toggle_button),
270 fd->current_opts.use_limits);
272 gtk_spin_button_set_value (fd->limit_spinbutton,
273 fd->current_opts.limit);
275 ret = psppire_dialog_run (PSPPIRE_DIALOG (fd->format_dialog));
277 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
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;
288 fd->current_opts.use_limits = gtk_toggle_button_get_active
289 (GTK_TOGGLE_BUTTON (fd->limit_toggle_button));
291 fd->current_opts.limit =
292 gtk_spin_button_get_value (fd->limit_spinbutton);
297 /* Makes widget W's sensitivity follow the active state of TOGGLE */
299 sensitive_if_active (GtkToggleButton *toggle, GtkWidget *w)
301 gboolean active = gtk_toggle_button_get_active (toggle);
303 gtk_widget_set_sensitive (w, active);
306 /* Pops up the Frequencies dialog box */
308 frequencies_dialog (GObject *o, gpointer data)
311 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
313 struct frequencies_dialog fd;
315 GtkBuilder *xml = builder_new ("frequencies.ui");
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");
323 PsppireVarStore *vs = NULL;
325 g_object_get (de->data_editor, "var-store", &vs, NULL);
327 put_checkbox_items_in_treeview (GTK_TREE_VIEW(stats_treeview),
334 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
336 g_object_get (vs, "dictionary", &fd.dict, NULL);
337 g_object_set (source, "model", fd.dict, NULL);
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"));
347 fd.stats = gtk_tree_view_get_model (GTK_TREE_VIEW (stats_treeview));
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"));
354 fd.current_opts.order = FRQ_AVALUE;
355 fd.current_opts.use_limits = FALSE;
356 fd.current_opts.limit = 50;
359 gtk_window_set_transient_for (GTK_WINDOW (fd.format_dialog), GTK_WINDOW (de));
362 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &fd);
364 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
365 dialog_state_valid, &fd);
368 g_signal_connect_swapped (format_button, "clicked",
369 G_CALLBACK (on_format_clicked), &fd);
371 g_signal_connect (fd.limit_toggle_button, "toggled",
372 G_CALLBACK (sensitive_if_active), fd.maximum_cats);
375 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
380 case GTK_RESPONSE_OK:
382 gchar *syntax = generate_syntax (&fd);
384 struct getl_interface *sss = create_syntax_string_source (syntax);
385 execute_syntax (sss);
390 case PSPPIRE_RESPONSE_PASTE:
392 gchar *syntax = generate_syntax (&fd);
393 paste_syntax_in_new_window (syntax);
401 g_object_unref (xml);