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"
23 #include <gtksheet/gtksheet.h>
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>
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 append_variable_names (string, fd->dict, GTK_TREE_VIEW (fd->stat_vars));
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 struct data_editor *de = data;
313 struct frequencies_dialog fd;
315 GladeXML *xml = XML_NEW ("frequencies.glade");
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");
324 GtkSheet *var_sheet =
325 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
327 PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
329 put_checkbox_items_in_treeview (GTK_TREE_VIEW(stats_treeview),
338 gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
340 attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
342 GTK_SELECTION_MULTIPLE, NULL);
344 set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
347 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
350 insert_source_row_into_tree_view,
354 fd.stat_vars = GTK_TREE_VIEW (dest);
356 fd.table_button = get_widget_assert (xml, "checkbutton1");
357 fd.format_dialog = get_widget_assert (xml, "format-dialog");
358 fd.maximum_cats = get_widget_assert (xml, "hbox5");
359 fd.limit_toggle_button = get_widget_assert (xml, "checkbutton2");
360 fd.limit_spinbutton =
361 GTK_SPIN_BUTTON (get_widget_assert (xml, "spinbutton1"));
363 fd.stats = gtk_tree_view_get_model (GTK_TREE_VIEW (stats_treeview));
365 fd.avalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
366 fd.dvalue = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton2"));
367 fd.afreq = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton3"));
368 fd.dfreq = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton4"));
370 fd.current_opts.order = FRQ_AVALUE;
371 fd.current_opts.use_limits = FALSE;
372 fd.current_opts.limit = 50;
374 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &fd);
376 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
377 dialog_state_valid, &fd);
380 g_signal_connect_swapped (format_button, "clicked",
381 G_CALLBACK (on_format_clicked), &fd);
383 g_signal_connect (fd.limit_toggle_button, "toggled",
384 G_CALLBACK (sensitive_if_active), fd.maximum_cats);
387 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
392 case GTK_RESPONSE_OK:
394 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);
405 struct syntax_editor *se =
406 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
408 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
417 g_object_unref (xml);