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"
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>
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
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")) \
57 #define FS(NAME, LABEL) FS_##NAME,
65 #define FS(NAME, LABEL) B_FS_##NAME = 1u << FS_##NAME,
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
73 static const struct checkbox_entry_item stats[] =
75 #define FS(NAME, LABEL) {#NAME, LABEL},
97 struct frequencies_dialog
99 GtkTreeView *stat_vars;
102 GtkWidget *table_button;
104 GtkWidget *format_dialog;
105 GtkWidget *maximum_cats;
106 GtkWidget *limit_toggle_button;
107 GtkSpinButton *limit_spinbutton;
109 GtkToggleButton *avalue;
110 GtkToggleButton *dvalue;
111 GtkToggleButton *afreq;
112 GtkToggleButton *dfreq;
114 struct format_options current_opts;
120 refresh (PsppireDialog *dialog, struct frequencies_dialog *fd)
126 GtkTreeModel *liststore = gtk_tree_view_get_model (fd->stat_vars);
127 gtk_list_store_clear (GTK_LIST_STORE (liststore));
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);
135 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->table_button), TRUE);
139 generate_syntax (const struct frequencies_dialog *fd)
147 GString *string = g_string_new ("FREQUENCIES");
149 g_string_append (string, "\n\t/VARIABLES=");
150 append_variable_names (string, fd->dict, GTK_TREE_VIEW (fd->stat_vars), 0);
152 g_string_append (string, "\n\t/FORMAT=");
154 switch (fd->current_opts.order)
157 g_string_append (string, "AVALUE");
160 g_string_append (string, "DVALUE");
163 g_string_append (string, "AFREQ");
166 g_string_append (string, "DFREQ");
169 g_assert_not_reached();
172 g_string_append (string, " ");
174 if ( fd->current_opts.use_limits )
176 g_string_append_printf (string, "LIMIT (%d)", fd->current_opts.limit);
180 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->table_button)))
181 g_string_append (string, "TABLE");
183 g_string_append (string, "NOTABLE");
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))
191 gtk_tree_model_get (fd->stats, &iter,
192 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
197 if (selected != B_FS_DEFAULT)
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");
207 if ((selected & B_FS_DEFAULT) == B_FS_DEFAULT)
209 g_string_append (string, "DEFAULT");
210 selected &= ~B_FS_DEFAULT;
213 for (i = 0; i < N_FREQUENCY_STATS; i++)
214 if (selected & (1u << i))
217 g_string_append (string, " ");
218 g_string_append (string, stats[i].name);
223 g_string_append (string, ".\n");
227 g_string_free (string, FALSE);
232 /* Dialog is valid iff at least one variable has been selected */
234 dialog_state_valid (gpointer data)
236 struct frequencies_dialog *fd = data;
238 GtkTreeModel *vars = gtk_tree_view_get_model (fd->stat_vars);
242 return gtk_tree_model_get_iter_first (vars, ¬used);
247 on_format_clicked (struct frequencies_dialog *fd)
250 g_signal_emit_by_name (fd->limit_toggle_button, "toggled");
252 switch (fd->current_opts.order)
255 gtk_toggle_button_set_active (fd->avalue, TRUE);
258 gtk_toggle_button_set_active (fd->dvalue, TRUE);
261 gtk_toggle_button_set_active (fd->dfreq, TRUE);
264 gtk_toggle_button_set_active (fd->dfreq, TRUE);
268 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->limit_toggle_button),
269 fd->current_opts.use_limits);
271 gtk_spin_button_set_value (fd->limit_spinbutton,
272 fd->current_opts.limit);
274 ret = psppire_dialog_run (PSPPIRE_DIALOG (fd->format_dialog));
276 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
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;
287 fd->current_opts.use_limits = gtk_toggle_button_get_active
288 (GTK_TOGGLE_BUTTON (fd->limit_toggle_button));
290 fd->current_opts.limit =
291 gtk_spin_button_get_value (fd->limit_spinbutton);
296 /* Makes widget W's sensitivity follow the active state of TOGGLE */
298 sensitive_if_active (GtkToggleButton *toggle, GtkWidget *w)
300 gboolean active = gtk_toggle_button_get_active (toggle);
302 gtk_widget_set_sensitive (w, active);
305 /* Pops up the Frequencies dialog box */
307 frequencies_dialog (GObject *o, gpointer data)
310 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
312 struct frequencies_dialog fd;
314 GtkBuilder *xml = builder_new ("frequencies.ui");
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");
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_set (source, "dictionary", vs->dict, NULL);
338 set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
341 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
344 insert_source_row_into_tree_view,
349 fd.stat_vars = GTK_TREE_VIEW (dest);
351 fd.table_button = get_widget_assert (xml, "checkbutton1");
352 fd.format_dialog = get_widget_assert (xml, "format-dialog");
353 fd.maximum_cats = get_widget_assert (xml, "hbox5");
354 fd.limit_toggle_button = get_widget_assert (xml, "checkbutton2");
355 fd.limit_spinbutton =
356 GTK_SPIN_BUTTON (get_widget_assert (xml, "spinbutton1"));
358 fd.stats = gtk_tree_view_get_model (GTK_TREE_VIEW (stats_treeview));
360 fd.avalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
361 fd.dvalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton2"));
362 fd.afreq = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton3"));
363 fd.dfreq = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton4"));
365 fd.current_opts.order = FRQ_AVALUE;
366 fd.current_opts.use_limits = FALSE;
367 fd.current_opts.limit = 50;
370 gtk_window_set_transient_for (GTK_WINDOW (fd.format_dialog), GTK_WINDOW (de));
373 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &fd);
375 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
376 dialog_state_valid, &fd);
379 g_signal_connect_swapped (format_button, "clicked",
380 G_CALLBACK (on_format_clicked), &fd);
382 g_signal_connect (fd.limit_toggle_button, "toggled",
383 G_CALLBACK (sensitive_if_active), fd.maximum_cats);
386 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
391 case GTK_RESPONSE_OK:
393 gchar *syntax = generate_syntax (&fd);
395 struct getl_interface *sss = create_syntax_string_source (syntax);
396 execute_syntax (sss);
401 case PSPPIRE_RESPONSE_PASTE:
403 gchar *syntax = generate_syntax (&fd);
404 paste_syntax_in_new_window (syntax);
412 g_object_unref (xml);