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);
224 psppire_dialog_action_oneway_activate (PsppireDialogAction *a)
226 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
227 PsppireDialogActionOneway *act = PSPPIRE_DIALOG_ACTION_ONEWAY (a);
229 GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
230 GtkBuilder *xml = g_hash_table_lookup (thing, a);
233 xml = builder_new ("oneway.ui");
234 g_hash_table_insert (thing, a, xml);
237 GtkWidget *contrasts_button =
238 get_widget_assert (xml, "contrasts-button");
239 GtkEntry *entry = GTK_ENTRY (get_widget_assert (xml, "entry1"));
241 pda->dialog = get_widget_assert (xml, "oneway-anova-dialog");
242 pda->source = get_widget_assert (xml, "oneway-anova-treeview1");
244 act->vars_treeview = get_widget_assert (xml, "oneway-anova-treeview2");
245 act->factor_entry = get_widget_assert (xml, "oneway-anova-entry");
247 act->descriptives = get_widget_assert (xml, "checkbutton1");
248 act->homogeneity = get_widget_assert (xml, "checkbutton2");
250 act->contrasts_dialog = get_widget_assert (xml, "contrasts-dialog");
252 act->next = get_widget_assert (xml, "next-button");
253 act->prev = get_widget_assert (xml, "prev-button");
254 act->ctotal = get_widget_assert (xml, "entry2");
255 act->acr = get_widget_assert (xml, "psppire-acr1");
256 act->stack_label = get_widget_assert (xml, "contrast-stack-label");
257 act->contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
260 g_signal_connect (act->next, "clicked", G_CALLBACK (next), act);
261 g_signal_connect (act->prev, "clicked", G_CALLBACK (prev), act);
263 psppire_acr_set_entry (PSPPIRE_ACR (act->acr), entry);
265 gtk_window_set_transient_for (GTK_WINDOW (act->contrasts_dialog),
266 GTK_WINDOW (pda->toplevel));
269 g_signal_connect_swapped (contrasts_button, "clicked",
270 G_CALLBACK (run_contrasts_dialog), act);
273 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
274 psppire_dialog_action_set_refresh (pda, refresh);
279 psppire_dialog_action_oneway_class_init (PsppireDialogActionOnewayClass *class)
281 psppire_dialog_action_set_activation (class, psppire_dialog_action_oneway_activate);
282 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
287 psppire_dialog_action_oneway_init (PsppireDialogActionOneway *act)
289 act->contrasts_array = NULL;
298 run_contrasts_dialog (PsppireDialogActionOneway *csd)
302 csd->temp_contrasts = clone_contrasts_array (csd->contrasts_array);
306 push_new_store (csd->temp_contrasts, csd);
308 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
310 if ( response == PSPPIRE_RESPONSE_CONTINUE )
312 csd->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
318 push_new_store (GArray *contrast_stack, PsppireDialogActionOneway *csd)
320 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
322 g_array_append_val (contrast_stack, ls);
324 g_signal_connect_swapped (ls, "row-deleted",
325 G_CALLBACK (list_store_changed), csd);
327 g_signal_connect_swapped (ls, "row-changed",
328 G_CALLBACK (list_store_changed), csd);
330 list_store_changed (csd);
335 next (GtkWidget *widget, PsppireDialogActionOneway *csd)
337 if (csd->c >= csd->temp_contrasts->len)
338 push_new_store (csd->temp_contrasts, csd);
342 list_store_changed (csd);
347 prev (GtkWidget *widget, PsppireDialogActionOneway *csd)
352 list_store_changed (csd);