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 (GObject *o, gpointer data)
108 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
109 struct weight_cases_dialog wcd;
111 GtkBuilder *xml = builder_new ("psppire.ui");
113 GtkWidget *dialog = get_widget_assert (xml, "weight-cases-dialog");
114 GtkWidget *source = get_widget_assert (xml, "weight-cases-treeview");
115 GtkWidget *entry = get_widget_assert (xml, "weight-cases-entry");
116 GtkWidget *radiobutton1 = get_widget_assert (xml,
117 "weight-cases-radiobutton1");
118 GtkWidget *radiobutton2 = get_widget_assert (xml, "radiobutton2");
119 GtkWidget *status = get_widget_assert (xml, "weight-status-label");
121 GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
123 PsppireVarStore *vs = NULL;
125 g_object_get (de->data_editor, "var-store", &vs, NULL);
126 g_object_get (vs, "dictionary", &wcd.dict, NULL);
128 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
130 g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
132 g_signal_connect (selector, "selected", G_CALLBACK (on_select), &wcd);
133 g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect), &wcd);
135 g_object_set (source, "model", wcd.dict,
136 "selection-mode", GTK_SELECTION_SINGLE,
137 "predicate", var_is_numeric,
140 psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector),
141 is_currently_in_entry);
143 wcd.entry = GTK_ENTRY (entry);
144 wcd.status = GTK_LABEL (status);
145 wcd.off = GTK_TOGGLE_BUTTON (radiobutton1);
146 wcd.on = GTK_TOGGLE_BUTTON (radiobutton2);
148 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &wcd);
150 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
152 g_object_unref (xml);
156 case GTK_RESPONSE_OK:
158 gchar *syntax = generate_syntax (&wcd);
159 struct getl_interface *sss = create_syntax_string_source (syntax);
160 execute_syntax (sss);
165 case PSPPIRE_RESPONSE_PASTE:
167 gchar *syntax = generate_syntax (&wcd);
168 paste_syntax_in_new_window (syntax);
179 generate_syntax (const struct weight_cases_dialog *wcd)
183 const gchar *text = gtk_entry_get_text (wcd->entry);
185 struct variable *var = psppire_dict_lookup_var (wcd->dict, text);
188 syntax = g_strdup ("WEIGHT OFF.");
190 syntax = g_strdup_printf ("WEIGHT BY %s.\n",