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/>. */
19 #include "rank-dialog.h"
22 #include <gtksheet/gtksheet.h>
25 #include <language/syntax-string-source.h>
26 #include <ui/gui/data-editor.h>
27 #include <ui/gui/dialog-common.h>
28 #include <ui/gui/dict-display.h>
29 #include <ui/gui/helper.h>
30 #include <ui/gui/psppire-dialog.h>
31 #include <ui/gui/psppire-var-store.h>
32 #include <ui/gui/syntax-editor.h>
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
59 GtkWidget *group_vars;
62 GtkToggleButton *ascending_togglebutton;
63 GtkToggleButton *summary_togglebutton;
66 /* Types subdialog widgets */
68 GtkWidget *types_dialog;
69 GtkWidget *ntiles_entry;
71 GtkToggleButton *func_button[n_RANK_FUNCS];
72 GtkWidget *formula_box;
74 GtkToggleButton *blom;
75 GtkToggleButton *tukey;
76 GtkToggleButton *rankit;
79 /* Ties subdialog widgets */
81 PsppireDialog *ties_dialog;
82 GtkToggleButton *mean;
84 GtkToggleButton *high;
85 GtkToggleButton *condense;
89 refresh (PsppireDialog *dialog, struct rank_dialog *rd)
91 GtkTreeModel *liststore;
93 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->rank_vars));
94 gtk_list_store_clear (GTK_LIST_STORE (liststore));
96 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
97 gtk_list_store_clear (GTK_LIST_STORE (liststore));
99 gtk_toggle_button_set_active (rd->ascending_togglebutton, TRUE);
100 gtk_toggle_button_set_active (rd->summary_togglebutton, FALSE);
104 generate_syntax (const struct rank_dialog *rd)
108 GtkTreeModel *gs = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
112 GString *str = g_string_new ("RANK VARIABLES=");
114 append_variable_names (str, rd->dict, GTK_TREE_VIEW (rd->rank_vars), 0);
116 g_string_append_printf (str, " (%c)",
117 gtk_toggle_button_get_active (rd->ascending_togglebutton)
120 if ( gtk_tree_model_get_iter_first (gs, ¬used) )
122 g_string_append (str, "\n\tBY ");
124 append_variable_names (str, rd->dict, GTK_TREE_VIEW (rd->group_vars), 0);
127 g_string_append (str, "\n\t/PRINT = ");
128 if (gtk_toggle_button_get_active (rd->summary_togglebutton))
129 g_string_append (str, "YES");
131 g_string_append (str, "NO");
134 if (gtk_toggle_button_get_active (rd->func_button [RANK]))
135 g_string_append (str, "\n\t/RANK");
136 if (gtk_toggle_button_get_active (rd->func_button [NORMAL]))
137 g_string_append (str, "\n\t/NORMAL");
138 if (gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
139 g_string_append (str, "\n\t/PROPORTION");
140 if (gtk_toggle_button_get_active (rd->func_button [PERCENT]))
141 g_string_append (str, "\n\t/PERCENT");
142 if (gtk_toggle_button_get_active (rd->func_button [RFRACTION]))
143 g_string_append (str, "\n\t/RFRACTION");
144 if (gtk_toggle_button_get_active (rd->func_button [N]))
145 g_string_append (str, "\n\t/N");
146 if (gtk_toggle_button_get_active (rd->func_button [SAVAGE]))
147 g_string_append (str, "\n\t/SAVAGE");
148 if (gtk_toggle_button_get_active (rd->func_button [NTILES]))
150 gint n = gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->ntiles_entry));
151 g_string_append_printf (str, "\n\t/NTILES(%d)", n);
155 if (gtk_toggle_button_get_active (rd->func_button [NORMAL])
157 gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
159 g_string_append (str, "\n\t/FRACTION=");
161 if ( gtk_toggle_button_get_active (rd->blom))
162 g_string_append (str, "BLOM");
163 else if ( gtk_toggle_button_get_active (rd->tukey))
164 g_string_append (str, "TUKEY");
165 else if ( gtk_toggle_button_get_active (rd->rankit))
166 g_string_append (str, "RANKIT");
167 else if ( gtk_toggle_button_get_active (rd->vw))
168 g_string_append (str, "VW");
171 g_string_append (str, "\n\t/TIES=");
172 if ( gtk_toggle_button_get_active (rd->mean))
173 g_string_append (str, "MEAN");
174 else if ( gtk_toggle_button_get_active (rd->low))
175 g_string_append (str, "LOW");
176 else if ( gtk_toggle_button_get_active (rd->high))
177 g_string_append (str, "HIGH");
178 else if ( gtk_toggle_button_get_active (rd->condense))
179 g_string_append (str, "CONDENSE");
182 g_string_append (str, ".");
186 g_string_free (str, FALSE);
191 /* Dialog is valid iff at least one variable has been selected */
193 dialog_state_valid (gpointer data)
195 struct rank_dialog *rd = data;
197 GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->rank_vars));
201 return gtk_tree_model_get_iter_first (vars, ¬used);
204 static void on_ntiles_toggle (GtkToggleButton *, gpointer);
205 static void run_types_dialog (GtkButton *, gpointer);
206 static void run_ties_dialog (GtkButton *, gpointer );
209 set_sensitivity (struct rank_dialog *rd)
211 gboolean sens = gtk_toggle_button_get_active
212 (GTK_TOGGLE_BUTTON (rd->func_button[PROPORTION]))
214 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->func_button[NORMAL]));
216 gtk_widget_set_sensitive (rd->formula_box, sens);
219 /* Pops up the Rank dialog box */
221 rank_dialog (GObject *o, gpointer data)
224 struct data_editor *de = data;
226 struct rank_dialog rd;
228 GladeXML *xml = XML_NEW ("rank.glade");
230 GtkWidget *vars = get_widget_assert (xml, "dict-treeview");
231 GtkWidget *selector1 = get_widget_assert (xml, "psppire-selector1");
232 GtkWidget *selector2 = get_widget_assert (xml, "psppire-selector2");
235 GtkWidget *types_button = get_widget_assert (xml, "button1");
236 GtkWidget *ties_button = get_widget_assert (xml, "button2");
238 GtkSheet *var_sheet =
239 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
241 PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
245 rd.rank_vars = get_widget_assert (xml, "variables-treeview");
246 rd.group_vars = get_widget_assert (xml, "group-vars-treeview");
247 rd.dialog = get_widget_assert (xml, "rank-dialog");
248 rd.ascending_togglebutton =
249 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
251 rd.summary_togglebutton =
252 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "summary-checkbutton"));
254 rd.types_dialog = get_widget_assert (xml, "rank-types-dialog");
257 rd.ntiles_entry = get_widget_assert (xml, "ntiles-entry");
259 rd.func_button[RANK] =
260 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rank-checkbutton"));
262 rd.func_button[SAVAGE] =
263 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "savage-checkbutton"));
265 rd.func_button[RFRACTION] =
266 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rfrac-checkbutton"));
268 rd.func_button[PERCENT] =
269 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "percent-checkbutton"));
272 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "sum-checkbutton"));
274 rd.func_button[NTILES] =
275 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "ntiles-checkbutton"));
277 rd.func_button[PROPORTION] =
278 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "prop-checkbutton"));
280 rd.func_button[NORMAL] =
281 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "normal-checkbutton"));
283 rd.formula_box = get_widget_assert (xml, "formula-frame");
285 rd.blom = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "blom-button"));
286 rd.tukey = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "tukey-button"));
287 rd.rankit = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rankit-button"));
288 rd.vw = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "vw-button"));
291 rd.ties_dialog = PSPPIRE_DIALOG (get_widget_assert (xml, "ties-dialog"));
293 rd.mean = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "mean-button"));
294 rd.low = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "low-button"));
295 rd.high = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "high-button"));
296 rd.condense = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "condense-button"));
298 g_signal_connect_swapped (rd.func_button[PROPORTION], "toggled",
299 G_CALLBACK (set_sensitivity),
302 g_signal_connect_swapped (rd.func_button[NORMAL], "toggled",
303 G_CALLBACK (set_sensitivity),
306 g_signal_connect (rd.func_button[NTILES], "toggled",
307 G_CALLBACK (on_ntiles_toggle),
310 gtk_window_set_transient_for (GTK_WINDOW (rd.dialog), de->parent.window);
312 attach_dictionary_to_treeview (GTK_TREE_VIEW (vars),
314 GTK_SELECTION_MULTIPLE, NULL);
317 set_dest_model (GTK_TREE_VIEW (rd.rank_vars), vs->dict);
319 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
322 insert_source_row_into_tree_view,
326 set_dest_model (GTK_TREE_VIEW (rd.group_vars), vs->dict);
328 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
331 insert_source_row_into_tree_view,
336 g_signal_connect (types_button, "clicked",
337 G_CALLBACK (run_types_dialog), &rd);
339 g_signal_connect (ties_button, "clicked",
340 G_CALLBACK (run_ties_dialog), &rd);
342 g_signal_connect (rd.dialog, "refresh", G_CALLBACK (refresh), &rd);
344 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (rd.dialog),
345 dialog_state_valid, &rd);
347 response = psppire_dialog_run (PSPPIRE_DIALOG (rd.dialog));
352 case GTK_RESPONSE_OK:
354 gchar *syntax = generate_syntax (&rd);
355 struct getl_interface *sss = create_syntax_string_source (syntax);
356 execute_syntax (sss);
361 case PSPPIRE_RESPONSE_PASTE:
363 gchar *syntax = generate_syntax (&rd);
365 struct syntax_editor *se =
366 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
368 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
377 g_object_unref (xml);
382 types_dialog_reset (struct rank_dialog *rd)
386 for (i = 0 ; i < n_RANK_FUNCS ; ++i )
387 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->func_button [i]),
390 gtk_widget_set_sensitive (rd->ntiles_entry, FALSE);
392 gtk_widget_set_sensitive (rd->formula_box, FALSE);
398 run_types_dialog (GtkButton *b, gpointer data)
400 struct rank_dialog *rd = data;
403 gtk_window_set_transient_for (GTK_WINDOW (rd->types_dialog),
404 GTK_WINDOW (rd->dialog));
406 types_dialog_reset (rd);
408 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->types_dialog));
412 run_ties_dialog (GtkButton *b, gpointer data)
414 struct rank_dialog *rd = data;
417 gtk_window_set_transient_for (GTK_WINDOW (rd->ties_dialog),
418 GTK_WINDOW (rd->dialog));
421 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->ties_dialog));
426 on_ntiles_toggle (GtkToggleButton *toggle_button, gpointer data)
429 gboolean active = gtk_toggle_button_get_active (toggle_button);
430 gtk_widget_set_sensitive (w, active);