1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011 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 "dialog-common.h"
20 #include <ui/syntax-gen.h>
21 #include <libpspp/str.h>
23 #include "runs-dialog.h"
24 #include "psppire-selector.h"
25 #include "psppire-dictview.h"
26 #include "psppire-dialog.h"
28 #include "psppire-data-window.h"
29 #include "psppire-var-view.h"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
55 PsppireDataWindow *de ;
61 static char * generate_syntax (const struct runs *rd);
63 /* Makes widget W's sensitivity follow the active state of TOGGLE */
65 sensitive_if_active (GtkToggleButton *toggle, GtkWidget *w)
67 gboolean active = gtk_toggle_button_get_active (toggle);
69 gtk_widget_set_sensitive (w, active);
73 refresh (struct runs *fd)
76 GtkTreeModel *liststore =
77 gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
78 gtk_list_store_clear (GTK_LIST_STORE (liststore));
80 gtk_entry_set_text (GTK_ENTRY (fd->entry), "");
82 for (i = 0; i < 4; ++i)
83 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->cb[i]), FALSE);
88 dialog_state_valid (gpointer data)
91 struct runs *fd = data;
93 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
95 if (gtk_tree_model_iter_n_children (liststore, NULL) < 1)
98 for (i = 0; i < 4; ++i)
100 if ( TRUE == gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[i])))
107 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[CB_CUSTOM])))
109 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (fd->entry))))
117 /* Pops up the Runs dialog box */
119 runs_dialog (PsppireDataWindow *dw)
129 fd.xml = builder_new ("runs.ui");
131 dialog = get_widget_assert (fd.xml, "runs-dialog");
132 source = get_widget_assert (fd.xml, "dict-view");
134 fd.entry = get_widget_assert (fd.xml, "entry1");
135 fd.cb[CB_MEDIAN] = get_widget_assert (fd.xml, "checkbutton1");
136 fd.cb[CB_MEAN] = get_widget_assert (fd.xml, "checkbutton2");
137 fd.cb[CB_MODE] = get_widget_assert (fd.xml, "checkbutton4");
138 fd.cb[CB_CUSTOM] = get_widget_assert (fd.xml, "checkbutton3");
142 g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh), &fd);
145 fd.variables = get_widget_assert (fd.xml, "psppire-var-view1");
147 g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
149 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
151 g_object_get (vs, "dictionary", &fd.dict, NULL);
152 g_object_set (source, "model", fd.dict,
153 "predicate", var_is_numeric,
156 g_signal_connect (fd.cb[CB_CUSTOM], "toggled",
157 G_CALLBACK (sensitive_if_active), fd.entry);
159 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
160 dialog_state_valid, &fd);
162 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
166 case GTK_RESPONSE_OK:
167 g_free (execute_syntax_string (dw, generate_syntax (&fd)));
169 case PSPPIRE_RESPONSE_PASTE:
170 g_free (paste_syntax_to_window (generate_syntax (&fd)));
176 g_object_unref (fd.xml);
182 append_fragment (GString *string, const gchar *cut, PsppireVarView *vv)
184 g_string_append (string, "\n\t/RUNS");
186 g_string_append (string, " ( ");
187 g_string_append (string, cut);
188 g_string_append (string, " ) = ");
190 psppire_var_view_append_names (vv, 0, string);
195 generate_syntax (const struct runs *rd)
199 GString *string = g_string_new ("NPAR TEST");
201 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MEAN])))
202 append_fragment (string, "MEAN", PSPPIRE_VAR_VIEW (rd->variables));
204 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MEDIAN])))
205 append_fragment (string, "MEDIAN", PSPPIRE_VAR_VIEW (rd->variables));
207 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MODE])))
208 append_fragment (string, "MODE", PSPPIRE_VAR_VIEW (rd->variables));
210 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_CUSTOM])))
212 char *text = gtk_entry_get_text (GTK_ENTRY (rd->entry));
213 append_fragment (string, text, PSPPIRE_VAR_VIEW (rd->variables));
216 g_string_append (string, ".\n");
220 g_string_free (string, FALSE);