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 "psppire-data-window.h"
24 #include "dict-display.h"
25 #include <language/syntax-string-source.h>
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
35 #include "psppire-var-store.h"
37 struct weight_cases_dialog
47 on_select (PsppireSelector *sel, gpointer data)
49 struct weight_cases_dialog *wcd = data;
51 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->on), TRUE);
52 gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), TRUE);
56 on_deselect (PsppireSelector *sel, gpointer data)
58 struct weight_cases_dialog *wcd = data;
60 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->off), TRUE);
61 gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), FALSE);
66 on_toggle (GtkToggleButton *button, gpointer data)
68 GtkEntry *entry = data;
69 if ( gtk_toggle_button_get_active (button))
70 gtk_entry_set_text (entry, "");
74 refresh (PsppireDialog *dialog, const struct weight_cases_dialog *wcd)
76 const struct variable *var = dict_get_weight (wcd->dict->dict);
80 gtk_entry_set_text (wcd->entry, "");
81 gtk_label_set_text (wcd->status, _("Do not weight cases"));
82 gtk_toggle_button_set_active (wcd->off, TRUE);
87 g_strdup_printf (_("Weight cases by %s"), var_get_name (var));
89 gtk_entry_set_text (wcd->entry, var_get_name (var));
90 gtk_label_set_text (wcd->status, text);
93 gtk_toggle_button_set_active (wcd->on, TRUE);
96 g_signal_emit_by_name (wcd->entry, "activate");
100 static gchar * generate_syntax (const struct weight_cases_dialog *wcd);
103 /* Pops up the Weight Cases dialog box */
105 weight_cases_dialog (PsppireDataWindow *de)
108 struct weight_cases_dialog wcd;
110 GtkBuilder *xml = builder_new ("psppire.ui");
112 GtkWidget *dialog = get_widget_assert (xml, "weight-cases-dialog");
113 GtkWidget *source = get_widget_assert (xml, "weight-cases-treeview");
114 GtkWidget *entry = get_widget_assert (xml, "weight-cases-entry");
115 GtkWidget *radiobutton1 = get_widget_assert (xml,
116 "weight-cases-radiobutton1");
117 GtkWidget *radiobutton2 = get_widget_assert (xml, "radiobutton2");
118 GtkWidget *status = get_widget_assert (xml, "weight-status-label");
120 GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
122 PsppireVarStore *vs = NULL;
124 g_object_get (de->data_editor, "var-store", &vs, NULL);
125 g_object_get (vs, "dictionary", &wcd.dict, NULL);
127 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
129 g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
131 g_signal_connect (selector, "selected", G_CALLBACK (on_select), &wcd);
132 g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect), &wcd);
134 g_object_set (source, "model", wcd.dict,
135 "selection-mode", GTK_SELECTION_SINGLE,
136 "predicate", var_is_numeric,
139 psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector),
140 is_currently_in_entry);
142 wcd.entry = GTK_ENTRY (entry);
143 wcd.status = GTK_LABEL (status);
144 wcd.off = GTK_TOGGLE_BUTTON (radiobutton1);
145 wcd.on = GTK_TOGGLE_BUTTON (radiobutton2);
147 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &wcd);
149 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
151 g_object_unref (xml);
155 case GTK_RESPONSE_OK:
157 gchar *syntax = generate_syntax (&wcd);
158 struct getl_interface *sss = create_syntax_string_source (syntax);
159 execute_syntax (sss);
164 case PSPPIRE_RESPONSE_PASTE:
166 gchar *syntax = generate_syntax (&wcd);
167 paste_syntax_to_window (syntax);
178 generate_syntax (const struct weight_cases_dialog *wcd)
182 const gchar *text = gtk_entry_get_text (wcd->entry);
184 struct variable *var = psppire_dict_lookup_var (wcd->dict, text);
187 syntax = g_strdup ("WEIGHT OFF.");
189 syntax = g_strdup_printf ("WEIGHT BY %s.\n",