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 "weight-cases-dialog.h"
20 #include "psppire-selector.h"
21 #include "psppire-dialog.h"
23 #include "data-editor.h"
24 #include "dict-display.h"
25 #include <language/syntax-string-source.h>
26 #include "syntax-editor.h"
29 #include <glade/glade.h>
32 #define _(msgid) gettext (msgid)
33 #define N_(msgid) msgid
36 /* FIXME: These shouldn't be here */
37 #include <gtksheet/gtksheet.h>
38 #include "psppire-var-store.h"
41 on_select (PsppireSelector *sel, gpointer data)
43 GtkRadioButton *radiobutton2 = data;
45 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton2), TRUE);
49 on_deselect (PsppireSelector *sel, gpointer data)
51 GtkRadioButton *radiobutton1 = data;
53 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton1), TRUE);
58 on_toggle (GtkToggleButton *button, gpointer data)
60 GtkEntry *entry = data;
61 if ( gtk_toggle_button_get_active (button))
62 gtk_entry_set_text (entry, "");
65 struct weight_cases_dialog
75 refresh (PsppireDialog *dialog, const struct weight_cases_dialog *wcd)
77 const struct variable *var = dict_get_weight (wcd->dict->dict);
81 gtk_entry_set_text (wcd->entry, "");
82 gtk_label_set_text (wcd->status, _("Do not weight cases"));
83 gtk_toggle_button_set_active (wcd->off, TRUE);
88 g_strdup_printf (_("Weight cases by %s"), var_get_name (var));
90 gtk_entry_set_text (wcd->entry, var_get_name (var));
91 gtk_label_set_text (wcd->status, text);
94 gtk_toggle_button_set_active (wcd->on, TRUE);
97 g_signal_emit_by_name (wcd->entry, "activate");
101 static gchar * generate_syntax (const struct weight_cases_dialog *wcd);
104 /* Pops up the Weight Cases dialog box */
106 weight_cases_dialog (GObject *o, gpointer data)
109 struct data_editor *de = data;
110 struct weight_cases_dialog wcd;
112 GladeXML *xml = XML_NEW ("psppire.glade");
114 GtkWidget *dialog = get_widget_assert (xml, "weight-cases-dialog");
115 GtkWidget *source = get_widget_assert (xml, "weight-cases-treeview");
116 GtkWidget *entry = get_widget_assert (xml, "weight-cases-entry");
117 GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
118 GtkWidget *radiobutton1 = get_widget_assert (xml,
119 "weight-cases-radiobutton1");
120 GtkWidget *radiobutton2 = get_widget_assert (xml, "radiobutton2");
121 GtkWidget *status = get_widget_assert (xml, "weight-status-label");
123 GtkSheet *var_sheet =
124 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
126 PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
128 gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
130 g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
131 g_signal_connect (selector, "selected", G_CALLBACK (on_select),
134 g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect),
137 attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
139 GTK_SELECTION_SINGLE,
143 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
146 insert_source_row_into_entry,
147 is_currently_in_entry
152 wcd.entry = GTK_ENTRY (entry);
153 wcd.status = GTK_LABEL (status);
154 wcd.off = GTK_TOGGLE_BUTTON (radiobutton1);
155 wcd.on = GTK_TOGGLE_BUTTON (radiobutton2);
157 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &wcd);
159 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
161 g_object_unref (xml);
165 case GTK_RESPONSE_OK:
167 gchar *syntax = generate_syntax (&wcd);
168 struct getl_interface *sss = create_syntax_string_source (syntax);
169 execute_syntax (sss);
174 case PSPPIRE_RESPONSE_PASTE:
176 gchar *syntax = generate_syntax (&wcd);
178 struct syntax_editor *se =
179 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
181 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
193 generate_syntax (const struct weight_cases_dialog *wcd)
197 const gchar *text = gtk_entry_get_text (wcd->entry);
199 struct variable *var = psppire_dict_lookup_var (wcd->dict, text);
202 syntax = g_strdup ("WEIGHT OFF.");
204 syntax = g_strdup_printf ("WEIGHT BY %s.\n",