1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011, 2012 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"
32 #include "builder-wrapper.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
56 PsppireDataWindow *de ;
62 static char * generate_syntax (const struct runs *rd);
64 /* Makes widget W's sensitivity follow the active state of TOGGLE */
66 sensitive_if_active (GtkToggleButton *toggle, GtkWidget *w)
68 gboolean active = gtk_toggle_button_get_active (toggle);
70 gtk_widget_set_sensitive (w, active);
74 refresh (struct runs *fd)
77 GtkTreeModel *liststore =
78 gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
79 gtk_list_store_clear (GTK_LIST_STORE (liststore));
81 gtk_entry_set_text (GTK_ENTRY (fd->entry), "");
83 for (i = 0; i < 4; ++i)
84 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->cb[i]), FALSE);
89 dialog_state_valid (gpointer data)
92 struct runs *fd = data;
94 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
96 if (gtk_tree_model_iter_n_children (liststore, NULL) < 1)
99 for (i = 0; i < 4; ++i)
101 if ( TRUE == gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[i])))
108 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[CB_CUSTOM])))
110 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (fd->entry))))
118 /* Pops up the Runs dialog box */
120 runs_dialog (PsppireDataWindow *dw)
130 fd.xml = builder_new ("runs.ui");
132 dialog = get_widget_assert (fd.xml, "runs-dialog");
133 source = get_widget_assert (fd.xml, "dict-view");
135 fd.entry = get_widget_assert (fd.xml, "entry1");
136 fd.cb[CB_MEDIAN] = get_widget_assert (fd.xml, "checkbutton1");
137 fd.cb[CB_MEAN] = get_widget_assert (fd.xml, "checkbutton2");
138 fd.cb[CB_MODE] = get_widget_assert (fd.xml, "checkbutton4");
139 fd.cb[CB_CUSTOM] = get_widget_assert (fd.xml, "checkbutton3");
143 g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh), &fd);
146 fd.variables = get_widget_assert (fd.xml, "psppire-var-view1");
148 g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
150 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
152 g_object_get (vs, "dictionary", &fd.dict, NULL);
153 g_object_set (source, "model", fd.dict,
154 "predicate", var_is_numeric,
157 g_signal_connect (fd.cb[CB_CUSTOM], "toggled",
158 G_CALLBACK (sensitive_if_active), fd.entry);
160 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
161 dialog_state_valid, &fd);
163 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
167 case GTK_RESPONSE_OK:
168 g_free (execute_syntax_string (dw, generate_syntax (&fd)));
170 case PSPPIRE_RESPONSE_PASTE:
171 g_free (paste_syntax_to_window (generate_syntax (&fd)));
177 g_object_unref (fd.xml);
183 append_fragment (GString *string, const gchar *cut, PsppireVarView *vv)
185 g_string_append (string, "\n\t/RUNS");
187 g_string_append (string, " ( ");
188 g_string_append (string, cut);
189 g_string_append (string, " ) = ");
191 psppire_var_view_append_names (vv, 0, string);
196 generate_syntax (const struct runs *rd)
200 GString *string = g_string_new ("NPAR TEST");
202 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MEAN])))
203 append_fragment (string, "MEAN", PSPPIRE_VAR_VIEW (rd->variables));
205 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MEDIAN])))
206 append_fragment (string, "MEDIAN", PSPPIRE_VAR_VIEW (rd->variables));
208 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MODE])))
209 append_fragment (string, "MODE", PSPPIRE_VAR_VIEW (rd->variables));
211 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_CUSTOM])))
213 const char *text = gtk_entry_get_text (GTK_ENTRY (rd->entry));
214 append_fragment (string, text, PSPPIRE_VAR_VIEW (rd->variables));
217 g_string_append (string, ".\n");
221 g_string_free (string, FALSE);