Fixed the refresh button on the dialogs.
[pspp-builds.git] / src / ui / gui / weight-cases-dialog.c
1 /*
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2007  Free Software Foundation
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18     02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "weight-cases-dialog.h"
23 #include "psppire-selector.h"
24 #include "psppire-dialog.h"
25 #include "helper.h"
26 #include "data-editor.h"
27 #include "dict-display.h"
28 #include <language/syntax-string-source.h>
29 #include "syntax-editor.h"
30
31 #include <gtk/gtk.h>
32 #include <glade/glade.h>
33
34 #include <gettext.h>
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 /* FIXME: These shouldn't be here */
40 #include <gtksheet/gtksheet.h>
41 #include "psppire-var-store.h"
42
43 static void
44 on_select (PsppireSelector *sel, gpointer data)
45 {
46   GtkRadioButton *radiobutton2 = data;
47
48   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton2), TRUE);
49 }
50
51 static void
52 on_deselect (PsppireSelector *sel, gpointer data)
53 {
54   GtkRadioButton *radiobutton1 = data;
55
56   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton1), TRUE);
57 }
58
59
60 static void
61 on_toggle (GtkToggleButton *button, gpointer data)
62 {
63   GtkEntry *entry = data;
64   if ( gtk_toggle_button_get_active (button))
65     gtk_entry_set_text (entry, "");
66 }
67
68 struct weight_cases_dialog
69 {
70   PsppireDict *dict;
71   GtkEntry *entry;
72   GtkLabel *status;
73   GtkToggleButton *off;
74   GtkToggleButton *on;
75 };
76
77 static void
78 refresh (PsppireDialog *dialog, const struct weight_cases_dialog *wcd)
79 {
80   const struct variable *var = dict_get_weight (wcd->dict->dict);
81
82   if ( ! var )
83     {
84       gtk_entry_set_text (wcd->entry, "");
85       gtk_label_set_text (wcd->status, _("Do not weight cases"));
86       gtk_toggle_button_set_active (wcd->off, TRUE);
87     }
88   else
89     {
90       gchar *text =
91         g_strdup_printf (_("Weight cases by %s"), var_get_name (var));
92
93       gtk_entry_set_text (wcd->entry, var_get_name (var));
94       gtk_label_set_text (wcd->status, text);
95
96       g_free (text);
97       gtk_toggle_button_set_active (wcd->on, TRUE);
98     }
99
100   g_signal_emit_by_name (wcd->entry, "activate");
101 }
102
103
104 static gchar * generate_syntax (const struct weight_cases_dialog *wcd);
105
106
107 /* Pops up the Weight Cases dialog box */
108 void
109 weight_cases_dialog (GObject *o, gpointer data)
110 {
111   gint response;
112   struct data_editor *de = data;
113   struct weight_cases_dialog wcd;
114
115   GladeXML *xml = XML_NEW ("psppire.glade");
116
117   GtkWidget *dialog = get_widget_assert (xml, "weight-cases-dialog");
118   GtkWidget *source = get_widget_assert (xml, "weight-cases-treeview");
119   GtkWidget *entry = get_widget_assert (xml, "weight-cases-entry");
120   GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
121   GtkWidget *radiobutton1 = get_widget_assert (xml,
122                                                "weight-cases-radiobutton1");
123   GtkWidget *radiobutton2 = get_widget_assert (xml, "radiobutton2");
124   GtkWidget *status  = get_widget_assert (xml, "weight-status-label");
125
126   GtkSheet *var_sheet =
127     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
128
129   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
130
131   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
132
133   g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
134   g_signal_connect (selector, "selected", G_CALLBACK (on_select),
135                     radiobutton2);
136
137   g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect),
138                     radiobutton1);
139
140   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
141                                  vs->dict,
142                                  GTK_SELECTION_SINGLE,
143                                  var_is_numeric
144                                  );
145
146   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
147                                  source,
148                                  entry,
149                                  insert_source_row_into_entry,
150                                  is_currently_in_entry
151                                  );
152
153
154   wcd.dict = vs->dict;
155   wcd.entry = GTK_ENTRY (entry);
156   wcd.status = GTK_LABEL (status);
157   wcd.off = GTK_TOGGLE_BUTTON (radiobutton1);
158   wcd.on = GTK_TOGGLE_BUTTON (radiobutton2);
159
160   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &wcd);
161
162   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
163
164   g_object_unref (xml);
165
166   switch (response)
167     {
168     case GTK_RESPONSE_OK:
169       {
170         gchar *syntax = generate_syntax (&wcd);
171         struct getl_interface *sss = create_syntax_string_source (syntax);
172         execute_syntax (sss);
173
174         g_free (syntax);
175       }
176       break;
177     case PSPPIRE_RESPONSE_PASTE:
178       {
179         gchar *syntax = generate_syntax (&wcd);
180
181         struct syntax_editor *se =
182           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
183
184         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
185
186         g_free (syntax);
187       }
188       break;
189     default:
190       break;
191     }
192 }
193
194
195 static gchar *
196 generate_syntax (const struct weight_cases_dialog *wcd)
197 {
198   gchar *syntax;
199
200   const gchar *text  = gtk_entry_get_text (wcd->entry);
201
202   struct variable *var = psppire_dict_lookup_var (wcd->dict, text);
203
204   if ( var == NULL)
205     syntax = g_strdup ("WEIGHT OFF.");
206   else
207     syntax = g_strdup_printf ("WEIGHT BY %s.\n",
208                               var_get_name (var));
209
210   return syntax;
211 }