1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2012, 2013 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"
22 #include "psppire-var-view.h"
23 #include "psppire-acr.h"
25 #include "psppire-dialog.h"
26 #include "builder-wrapper.h"
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
35 static void next (GtkWidget *widget, PsppireDialogActionOneway *);
36 static void prev (GtkWidget *widget, PsppireDialogActionOneway *);
37 static void run_contrasts_dialog (PsppireDialogActionOneway *csd);
38 static void push_new_store (GArray *contrast_stack, PsppireDialogActionOneway *csd);
41 static void psppire_dialog_action_oneway_init (PsppireDialogActionOneway *act);
42 static void psppire_dialog_action_oneway_class_init (PsppireDialogActionOnewayClass *class);
44 G_DEFINE_TYPE (PsppireDialogActionOneway, psppire_dialog_action_oneway, PSPPIRE_TYPE_DIALOG_ACTION);
48 generate_syntax (PsppireDialogAction *act)
50 PsppireDialogActionOneway *ow = PSPPIRE_DIALOG_ACTION_ONEWAY (act);
54 gboolean descriptives = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->descriptives));
55 gboolean homogeneity = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->homogeneity));
58 ds_init_cstr (&dss, "ONEWAY /VARIABLES=");
60 psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (ow->vars_treeview), 0, &dss);
62 ds_put_cstr (&dss, " BY ");
64 ds_put_cstr (&dss, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
66 if (descriptives || homogeneity )
68 ds_put_cstr (&dss, "\n\t/STATISTICS=");
70 ds_put_cstr (&dss, "DESCRIPTIVES ");
72 ds_put_cstr (&dss, "HOMOGENEITY ");
75 for (i = 0 ; i < ow->contrasts_array->len ; ++i )
77 GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
81 ds_put_cstr (&dss, "\n\t/CONTRAST=");
83 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
86 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
90 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
92 ds_put_c_format (&dss, " %g", v);
96 ds_put_cstr (&dss, ".\n");
98 text = ds_steal_cstr (&dss);
106 dialog_state_valid (gpointer data)
108 PsppireDialogActionOneway *ow = PSPPIRE_DIALOG_ACTION_ONEWAY (data);
111 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
115 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
118 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
126 refresh (PsppireDialogAction *rd_)
128 PsppireDialogActionOneway *ow = PSPPIRE_DIALOG_ACTION_ONEWAY (rd_);
130 GtkTreeModel *model =
131 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
133 gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
135 gtk_list_store_clear (GTK_LIST_STORE (model));
139 /* Callback for when the list store currently associated with the
140 treeview has changed. It sets the widgets of the subdialog
141 to reflect the store's new state.
144 list_store_changed (PsppireDialogActionOneway *csd)
149 GtkTreeModel *ls = NULL;
151 g_strdup_printf (_("Contrast %d of %d"),
152 csd->c, csd->temp_contrasts->len);
154 gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
158 gtk_widget_set_sensitive (csd->prev, csd->c > 1);
161 ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
163 psppire_acr_set_model (PSPPIRE_ACR (csd->acr), GTK_LIST_STORE (ls));
165 /* Sensitive iff the liststore has two items or more */
166 gtk_widget_set_sensitive (csd->next,
167 gtk_tree_model_iter_nth_child
168 (ls, &iter, NULL, 1));
170 for (ok = gtk_tree_model_get_iter_first (ls, &iter);
172 ok = gtk_tree_model_iter_next (ls, &iter)
176 gtk_tree_model_get (ls, &iter, 0, &v, -1);
180 text = g_strdup_printf ("%g", total);
182 gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
188 /* Copy the contrasts array into the local array */
190 clone_contrasts_array (GArray *src_array)
195 g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
198 for (i = 0 ; i < src_array->len ; ++i )
201 GtkTreeIter src_iter;
202 GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
205 /* Refuse to copy empty stores */
206 if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
210 dest = clone_list_store (src);
212 g_array_append_val (dest_array, dest);
222 psppire_dialog_action_oneway_activate (GtkAction *a)
224 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
225 PsppireDialogActionOneway *act = PSPPIRE_DIALOG_ACTION_ONEWAY (a);
227 GtkBuilder *xml = builder_new ("oneway.ui");
228 GtkWidget *contrasts_button =
229 get_widget_assert (xml, "contrasts-button");
230 GtkEntry *entry = GTK_ENTRY (get_widget_assert (xml, "entry1"));
232 pda->dialog = get_widget_assert (xml, "oneway-anova-dialog");
233 pda->source = get_widget_assert (xml, "oneway-anova-treeview1");
235 act->vars_treeview = get_widget_assert (xml, "oneway-anova-treeview2");
236 act->factor_entry = get_widget_assert (xml, "oneway-anova-entry");
238 act->descriptives = get_widget_assert (xml, "checkbutton1");
239 act->homogeneity = get_widget_assert (xml, "checkbutton2");
241 act->contrasts_dialog = get_widget_assert (xml, "contrasts-dialog");
243 act->next = get_widget_assert (xml, "next-button");
244 act->prev = get_widget_assert (xml, "prev-button");
245 act->ctotal = get_widget_assert (xml, "entry2");
246 act->acr = get_widget_assert (xml, "psppire-acr1");
247 act->stack_label = get_widget_assert (xml, "contrast-stack-label");
248 act->contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
251 g_signal_connect (act->next, "clicked", G_CALLBACK (next), act);
252 g_signal_connect (act->prev, "clicked", G_CALLBACK (prev), act);
254 psppire_acr_set_entry (PSPPIRE_ACR (act->acr), entry);
256 gtk_window_set_transient_for (GTK_WINDOW (act->contrasts_dialog),
257 GTK_WINDOW (pda->toplevel));
260 g_signal_connect_swapped (contrasts_button, "clicked",
261 G_CALLBACK (run_contrasts_dialog), act);
264 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
265 psppire_dialog_action_set_refresh (pda, refresh);
267 g_object_unref (xml);
269 if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_oneway_parent_class)->activate)
270 PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_oneway_parent_class)->activate (pda);
274 psppire_dialog_action_oneway_class_init (PsppireDialogActionOnewayClass *class)
276 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
278 action_class->activate = psppire_dialog_action_oneway_activate;
279 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
284 psppire_dialog_action_oneway_init (PsppireDialogActionOneway *act)
286 act->contrasts_array = NULL;
295 run_contrasts_dialog (PsppireDialogActionOneway *csd)
299 csd->temp_contrasts = clone_contrasts_array (csd->contrasts_array);
303 push_new_store (csd->temp_contrasts, csd);
305 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
307 if ( response == PSPPIRE_RESPONSE_CONTINUE )
309 csd->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
315 push_new_store (GArray *contrast_stack, PsppireDialogActionOneway *csd)
317 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
319 g_array_append_val (contrast_stack, ls);
321 g_signal_connect_swapped (ls, "row-deleted",
322 G_CALLBACK (list_store_changed), csd);
324 g_signal_connect_swapped (ls, "row-changed",
325 G_CALLBACK (list_store_changed), csd);
327 list_store_changed (csd);
332 next (GtkWidget *widget, PsppireDialogActionOneway *csd)
334 if (csd->c >= csd->temp_contrasts->len)
335 push_new_store (csd->temp_contrasts, csd);
339 list_store_changed (csd);
344 prev (GtkWidget *widget, PsppireDialogActionOneway *csd)
349 list_store_changed (csd);