1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2012, 2013, 2014 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/>. */
20 #include "psppire-dialog-action-oneway.h"
24 #include "psppire-var-view.h"
25 #include "psppire-acr.h"
27 #include "psppire-dialog.h"
28 #include "builder-wrapper.h"
33 #define _(msgid) gettext (msgid)
34 #define N_(msgid) msgid
37 static void next (GtkWidget *widget, PsppireDialogActionOneway *);
38 static void prev (GtkWidget *widget, PsppireDialogActionOneway *);
39 static void run_contrasts_dialog (PsppireDialogActionOneway *csd);
40 static void push_new_store (GArray *contrast_stack, PsppireDialogActionOneway *csd);
43 static void psppire_dialog_action_oneway_init (PsppireDialogActionOneway *act);
44 static void psppire_dialog_action_oneway_class_init (PsppireDialogActionOnewayClass *class);
46 G_DEFINE_TYPE (PsppireDialogActionOneway, psppire_dialog_action_oneway, PSPPIRE_TYPE_DIALOG_ACTION);
50 generate_syntax (const PsppireDialogAction *act)
52 PsppireDialogActionOneway *ow = PSPPIRE_DIALOG_ACTION_ONEWAY (act);
56 gboolean descriptives = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->descriptives));
57 gboolean homogeneity = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->homogeneity));
60 ds_init_cstr (&dss, "ONEWAY /VARIABLES=");
62 psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (ow->vars_treeview), 0, &dss);
64 ds_put_cstr (&dss, " BY ");
66 ds_put_cstr (&dss, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
68 if (descriptives || homogeneity )
70 ds_put_cstr (&dss, "\n\t/STATISTICS=");
72 ds_put_cstr (&dss, "DESCRIPTIVES ");
74 ds_put_cstr (&dss, "HOMOGENEITY ");
77 for (i = 0 ; i < ow->contrasts_array->len ; ++i )
79 GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
83 ds_put_cstr (&dss, "\n\t/CONTRAST=");
85 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
88 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
92 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
94 ds_put_c_format (&dss, " %.*g", DBL_DIG + 1, v);
98 ds_put_cstr (&dss, ".\n");
100 text = ds_steal_cstr (&dss);
108 dialog_state_valid (gpointer data)
110 PsppireDialogActionOneway *ow = PSPPIRE_DIALOG_ACTION_ONEWAY (data);
113 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
117 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
120 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
128 refresh (PsppireDialogAction *rd_)
130 PsppireDialogActionOneway *ow = PSPPIRE_DIALOG_ACTION_ONEWAY (rd_);
132 GtkTreeModel *model =
133 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
135 gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
137 gtk_list_store_clear (GTK_LIST_STORE (model));
141 /* Callback for when the list store currently associated with the
142 treeview has changed. It sets the widgets of the subdialog
143 to reflect the store's new state.
146 list_store_changed (PsppireDialogActionOneway *csd)
151 GtkTreeModel *ls = NULL;
153 g_strdup_printf (_("Contrast %d of %d"),
154 csd->c, csd->temp_contrasts->len);
156 gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
160 gtk_widget_set_sensitive (csd->prev, csd->c > 1);
163 ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
165 psppire_acr_set_model (PSPPIRE_ACR (csd->acr), GTK_LIST_STORE (ls));
167 /* Sensitive iff the liststore has two items or more */
168 gtk_widget_set_sensitive (csd->next,
169 gtk_tree_model_iter_nth_child
170 (ls, &iter, NULL, 1));
172 for (ok = gtk_tree_model_get_iter_first (ls, &iter);
174 ok = gtk_tree_model_iter_next (ls, &iter)
178 gtk_tree_model_get (ls, &iter, 0, &v, -1);
182 text = g_strdup_printf ("%.*g", DBL_DIG + 1, total);
184 gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
190 /* Copy the contrasts array into the local array */
192 clone_contrasts_array (GArray *src_array)
197 g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
200 for (i = 0 ; i < src_array->len ; ++i )
203 GtkTreeIter src_iter;
204 GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
207 /* Refuse to copy empty stores */
208 if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
212 dest = clone_list_store (src);
214 g_array_append_val (dest_array, dest);
222 psppire_dialog_action_oneway_activate (PsppireDialogAction *a, GVariant *param)
224 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
225 PsppireDialogActionOneway *act = PSPPIRE_DIALOG_ACTION_ONEWAY (a);
227 GtkBuilder *xml = builder_new ( "oneway.ui");
229 GtkWidget *contrasts_button =
230 get_widget_assert (xml, "contrasts-button");
231 GtkEntry *entry = GTK_ENTRY (get_widget_assert (xml, "entry1"));
233 pda->dialog = get_widget_assert (xml, "oneway-anova-dialog");
234 pda->source = get_widget_assert (xml, "oneway-anova-treeview1");
236 act->vars_treeview = get_widget_assert (xml, "oneway-anova-treeview2");
237 act->factor_entry = get_widget_assert (xml, "oneway-anova-entry");
239 act->descriptives = get_widget_assert (xml, "checkbutton1");
240 act->homogeneity = get_widget_assert (xml, "checkbutton2");
242 act->contrasts_dialog = get_widget_assert (xml, "contrasts-dialog");
244 act->next = get_widget_assert (xml, "next-button");
245 act->prev = get_widget_assert (xml, "prev-button");
246 act->ctotal = get_widget_assert (xml, "entry2");
247 act->acr = get_widget_assert (xml, "psppire-acr1");
248 act->stack_label = get_widget_assert (xml, "contrast-stack-label");
249 act->contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
252 g_signal_connect (act->next, "clicked", G_CALLBACK (next), act);
253 g_signal_connect (act->prev, "clicked", G_CALLBACK (prev), act);
255 psppire_acr_set_entry (PSPPIRE_ACR (act->acr), entry);
257 gtk_window_set_transient_for (GTK_WINDOW (act->contrasts_dialog),
258 GTK_WINDOW (pda->toplevel));
261 g_signal_connect_swapped (contrasts_button, "clicked",
262 G_CALLBACK (run_contrasts_dialog), act);
265 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
266 psppire_dialog_action_set_refresh (pda, refresh);
271 psppire_dialog_action_oneway_class_init (PsppireDialogActionOnewayClass *class)
273 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_oneway_activate;
274 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
279 psppire_dialog_action_oneway_init (PsppireDialogActionOneway *act)
281 act->contrasts_array = NULL;
290 run_contrasts_dialog (PsppireDialogActionOneway *csd)
294 csd->temp_contrasts = clone_contrasts_array (csd->contrasts_array);
298 push_new_store (csd->temp_contrasts, csd);
300 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
302 if ( response == PSPPIRE_RESPONSE_CONTINUE )
304 csd->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
310 push_new_store (GArray *contrast_stack, PsppireDialogActionOneway *csd)
312 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
314 g_array_append_val (contrast_stack, ls);
316 g_signal_connect_swapped (ls, "row-deleted",
317 G_CALLBACK (list_store_changed), csd);
319 g_signal_connect_swapped (ls, "row-changed",
320 G_CALLBACK (list_store_changed), csd);
322 list_store_changed (csd);
327 next (GtkWidget *widget, PsppireDialogActionOneway *csd)
329 if (csd->c >= csd->temp_contrasts->len)
330 push_new_store (csd->temp_contrasts, csd);
334 list_store_changed (csd);
339 prev (GtkWidget *widget, PsppireDialogActionOneway *csd)
344 list_store_changed (csd);