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/>.
20 #include <glade/glade.h>
22 #include "oneway-anova-dialog.h"
23 #include "psppire-dict.h"
24 #include "psppire-var-store.h"
26 #include "data-editor.h"
27 #include "psppire-dialog.h"
28 #include "dialog-common.h"
29 #include "dict-display.h"
30 #include "psppire-acr.h"
33 #include <language/syntax-string-source.h>
34 #include "syntax-editor.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
42 struct contrasts_subdialog;
43 struct oneway_anova_dialog;
46 static void run_contrasts_dialog (struct oneway_anova_dialog *);
47 static void push_new_store (GArray *, struct contrasts_subdialog *);
49 static void next (GtkWidget *, gpointer);
50 static void prev (GtkWidget *, gpointer);
53 struct contrasts_subdialog
55 /* Contrasts Dialog widgets */
56 GtkWidget *contrasts_dialog;
57 GtkWidget *stack_label;
61 /* Gets copied into contrasts when "Continue"
63 GArray *temp_contrasts;
65 /* Index into the temp_contrasts */
75 struct oneway_anova_dialog
79 GtkWidget *factor_entry;
80 GtkWidget *vars_treeview;
82 GArray *contrasts_array;
84 GtkToggleButton *descriptives;
85 GtkToggleButton *homogeneity;
87 struct contrasts_subdialog contrasts;
91 dialog_state_valid (gpointer data)
93 struct oneway_anova_dialog *ow = data;
96 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
100 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
103 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
109 static gchar * generate_syntax (const struct oneway_anova_dialog *);
113 refresh (struct oneway_anova_dialog *ow)
115 GtkTreeModel *model =
116 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
118 gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
120 gtk_list_store_clear (GTK_LIST_STORE (model));
124 /* Pops up the dialog box */
126 oneway_anova_dialog (GObject *o, gpointer data)
129 struct data_editor *de = data;
131 PsppireVarStore *vs = NULL;
133 GladeXML *xml = XML_NEW ("oneway.glade");
135 struct oneway_anova_dialog ow;
137 GtkWidget *dict_view =
138 get_widget_assert (xml, "oneway-anova-treeview1");
140 GtkWidget *selector2 =
141 get_widget_assert (xml, "oneway-anova-selector2");
143 GtkWidget *selector1 =
144 get_widget_assert (xml, "oneway-anova-selector1");
146 GtkWidget *contrasts_button =
147 get_widget_assert (xml, "contrasts-button");
150 g_signal_connect_swapped (contrasts_button, "clicked",
151 G_CALLBACK (run_contrasts_dialog), &ow);
154 ow.factor_entry = get_widget_assert (xml, "oneway-anova-entry");
156 get_widget_assert (xml, "oneway-anova-treeview2");
159 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "checkbutton1"));
162 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "checkbutton2"));
164 g_object_get (de->data_editor, "var-store", &vs, NULL);
169 GTK_WINDOW (get_widget_assert (xml, "oneway-anova-dialog"));
171 gtk_window_set_transient_for (ow.dialog, de->parent.window);
173 attach_dictionary_to_treeview (GTK_TREE_VIEW (dict_view),
175 GTK_SELECTION_MULTIPLE, NULL);
177 set_dest_model (GTK_TREE_VIEW (ow.vars_treeview), vs->dict);
180 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
181 dict_view, ow.vars_treeview,
182 insert_source_row_into_tree_view,
187 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
188 dict_view, ow.factor_entry,
189 insert_source_row_into_entry,
190 is_currently_in_entry,
195 g_signal_connect_swapped (ow.dialog, "refresh", G_CALLBACK (refresh), &ow);
199 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (ow.dialog),
200 dialog_state_valid, &ow);
204 struct contrasts_subdialog *cd = &ow.contrasts;
205 GtkEntry *entry = GTK_ENTRY (get_widget_assert (xml, "entry1"));
207 cd->acr = PSPPIRE_ACR (get_widget_assert (xml, "psppire-acr1"));
208 cd->contrasts_dialog = get_widget_assert (xml, "contrasts-dialog");
210 cd->next = get_widget_assert (xml, "next-button");
211 cd->prev = get_widget_assert (xml, "prev-button");
212 cd->ctotal = get_widget_assert (xml, "entry2");
214 cd->stack_label = get_widget_assert (xml, "contrast-stack-label");
217 ow.contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
219 g_signal_connect (cd->next, "clicked", G_CALLBACK (next), cd);
220 g_signal_connect (cd->prev, "clicked", G_CALLBACK (prev), cd);
222 psppire_acr_set_entry (cd->acr, entry);
224 gtk_window_set_transient_for (GTK_WINDOW (cd->contrasts_dialog),
228 response = psppire_dialog_run (PSPPIRE_DIALOG (ow.dialog));
232 case GTK_RESPONSE_OK:
234 gchar *syntax = generate_syntax (&ow);
235 struct getl_interface *sss = create_syntax_string_source (syntax);
236 execute_syntax (sss);
241 case PSPPIRE_RESPONSE_PASTE:
243 gchar *syntax = generate_syntax (&ow);
245 struct syntax_editor *se =
246 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
248 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
257 g_array_free (ow.contrasts_array, FALSE);
259 g_object_unref (xml);
263 static gchar * generate_syntax (const struct oneway_anova_dialog *ow)
267 gboolean descriptives = gtk_toggle_button_get_active (ow->descriptives);
268 gboolean homogeneity = gtk_toggle_button_get_active (ow->homogeneity);
270 GString *str = g_string_new ("ONEWAY /VARIABLES=");
272 append_variable_names (str, ow->dict, GTK_TREE_VIEW (ow->vars_treeview), 0);
274 g_string_append (str, " BY ");
276 g_string_append (str, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
278 if (descriptives || homogeneity )
280 g_string_append (str, "\n\t/STATISTICS=");
282 g_string_append (str, "DESCRIPTIVES ");
284 g_string_append (str, "HOMOGENEITY ");
287 for (i = 0 ; i < ow->contrasts_array->len ; ++i )
289 GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
293 g_string_append (str, "\n\t/CONTRAST=");
295 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
298 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
302 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
304 g_string_append_printf (str, " %g", v);
308 g_string_append (str, ".\n");
311 g_string_free (str, FALSE);
318 /* Contrasts stuff */
323 /* Callback for when the list store currently associated with the
324 treeview has changed. It sets the widgets of the subdialog
325 to reflect the store's new state.
328 list_store_changed (struct contrasts_subdialog *csd)
333 GtkTreeModel *ls = NULL;
335 g_strdup_printf (_("Contrast %d of %d"),
336 csd->c, csd->temp_contrasts->len);
338 gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
342 gtk_widget_set_sensitive (csd->prev, csd->c > 1);
345 ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
347 psppire_acr_set_model (csd->acr, GTK_LIST_STORE (ls));
349 /* Sensitive iff the liststore has two items or more */
350 gtk_widget_set_sensitive (csd->next,
351 gtk_tree_model_iter_nth_child
352 (ls, &iter, NULL, 1));
354 for (ok = gtk_tree_model_get_iter_first (ls, &iter);
356 ok = gtk_tree_model_iter_next (ls, &iter)
360 gtk_tree_model_get (ls, &iter, 0, &v, -1);
364 text = g_strdup_printf ("%g", total);
366 gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
373 /* Copy the contrasts array into the local array */
375 clone_contrasts_array (GArray *src_array)
380 g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
383 for (i = 0 ; i < src_array->len ; ++i )
386 GtkTreeIter src_iter;
387 GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
390 /* Refuse to copy empty stores */
391 if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
395 dest = clone_list_store (src);
397 g_array_append_val (dest_array, dest);
405 run_contrasts_dialog (struct oneway_anova_dialog *ow)
407 struct contrasts_subdialog *csd = &ow->contrasts;
410 csd->temp_contrasts = clone_contrasts_array (ow->contrasts_array);
414 push_new_store (csd->temp_contrasts, csd);
416 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
418 if ( response == PSPPIRE_RESPONSE_CONTINUE )
420 ow->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
423 /* Destroy the temp contrasts here */
429 push_new_store (GArray *contrast_stack, struct contrasts_subdialog *csd)
431 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
433 g_array_append_val (contrast_stack, ls);
435 g_signal_connect_swapped (ls, "row-deleted",
436 G_CALLBACK (list_store_changed), csd);
438 g_signal_connect_swapped (ls, "row-changed",
439 G_CALLBACK (list_store_changed), csd);
441 list_store_changed (csd);
445 next (GtkWidget *widget, gpointer data)
447 struct contrasts_subdialog *csd = data;
449 if (csd->c >= csd->temp_contrasts->len)
450 push_new_store (csd->temp_contrasts, csd);
454 list_store_changed (csd);
459 prev (GtkWidget *widget, gpointer data)
461 struct contrasts_subdialog *csd = data;
466 list_store_changed (csd);