Weight cases dialog: set the sensitivity of radiobuttons.
[pspp-builds.git] / src / ui / gui / weight-cases-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "weight-cases-dialog.h"
20 #include "psppire-selector.h"
21 #include "psppire-dialog.h"
22 #include "executor.h"
23 #include "psppire-data-window.h"
24 #include "dict-display.h"
25 #include <language/syntax-string-source.h>
26 #include "helper.h"
27
28 #include <gtk/gtk.h>
29
30 #include <gettext.h>
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
33
34
35 #include "psppire-var-store.h"
36
37 struct weight_cases_dialog
38 {
39   PsppireDict *dict;
40   GtkEntry *entry;
41   GtkLabel *status;
42   GtkToggleButton *off;
43   GtkToggleButton *on;
44 };
45
46 static void
47 on_select (PsppireSelector *sel, gpointer data)
48 {
49   struct weight_cases_dialog *wcd = data;
50
51   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->on), TRUE);
52   gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), TRUE);
53 }
54
55 static void
56 on_deselect (PsppireSelector *sel, gpointer data)
57 {
58   struct weight_cases_dialog *wcd = data;
59
60   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->off), TRUE);
61   gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), FALSE);
62 }
63
64
65 static void
66 on_toggle (GtkToggleButton *button, gpointer data)
67 {
68   GtkEntry *entry = data;
69   if ( gtk_toggle_button_get_active (button))
70     gtk_entry_set_text (entry, "");
71 }
72
73 static void
74 refresh (PsppireDialog *dialog, const struct weight_cases_dialog *wcd)
75 {
76   const struct variable *var = dict_get_weight (wcd->dict->dict);
77
78   if ( ! var )
79     {
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);
83     }
84   else
85     {
86       gchar *text =
87         g_strdup_printf (_("Weight cases by %s"), var_get_name (var));
88
89       gtk_entry_set_text (wcd->entry, var_get_name (var));
90       gtk_label_set_text (wcd->status, text);
91
92       g_free (text);
93       gtk_toggle_button_set_active (wcd->on, TRUE);
94     }
95
96   g_signal_emit_by_name (wcd->entry, "activate");
97 }
98
99
100 static gchar * generate_syntax (const struct weight_cases_dialog *wcd);
101
102
103 /* Pops up the Weight Cases dialog box */
104 void
105 weight_cases_dialog (GObject *o, gpointer data)
106 {
107   gint response;
108   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
109   struct weight_cases_dialog wcd;
110
111   GtkBuilder *xml = builder_new ("psppire.ui");
112
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");
120
121   GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
122
123   PsppireVarStore *vs = NULL;
124
125   g_object_get (de->data_editor, "var-store", &vs,  NULL);
126   g_object_get (vs, "dictionary", &wcd.dict, NULL);
127
128   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
129
130   g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
131
132   g_signal_connect (selector, "selected", G_CALLBACK (on_select), &wcd);
133   g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect), &wcd);
134   
135   g_object_set (source, "model", wcd.dict,
136                                  "selection-mode", GTK_SELECTION_SINGLE,
137                                  "predicate", var_is_numeric,
138                                  NULL);
139
140   psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector),
141                                     is_currently_in_entry);
142
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);
147
148   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &wcd);
149
150   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
151
152   g_object_unref (xml);
153
154   switch (response)
155     {
156     case GTK_RESPONSE_OK:
157       {
158         gchar *syntax = generate_syntax (&wcd);
159         struct getl_interface *sss = create_syntax_string_source (syntax);
160         execute_syntax (sss);
161
162         g_free (syntax);
163       }
164       break;
165     case PSPPIRE_RESPONSE_PASTE:
166       {
167         gchar *syntax = generate_syntax (&wcd);
168         paste_syntax_in_new_window (syntax);
169         g_free (syntax);
170       }
171       break;
172     default:
173       break;
174     }
175 }
176
177
178 static gchar *
179 generate_syntax (const struct weight_cases_dialog *wcd)
180 {
181   gchar *syntax;
182
183   const gchar *text  = gtk_entry_get_text (wcd->entry);
184
185   struct variable *var = psppire_dict_lookup_var (wcd->dict, text);
186
187   if ( var == NULL)
188     syntax = g_strdup ("WEIGHT OFF.");
189   else
190     syntax = g_strdup_printf ("WEIGHT BY %s.\n",
191                               var_get_name (var));
192
193   return syntax;
194 }