Change license from GPLv2+ to GPLv3+.
[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 "helper.h"
23 #include "data-editor.h"
24 #include "dict-display.h"
25 #include <language/syntax-string-source.h>
26 #include "syntax-editor.h"
27
28 #include <gtk/gtk.h>
29 #include <glade/glade.h>
30
31 #include <gettext.h>
32 #define _(msgid) gettext (msgid)
33 #define N_(msgid) msgid
34
35
36 /* FIXME: These shouldn't be here */
37 #include <gtksheet/gtksheet.h>
38 #include "psppire-var-store.h"
39
40 static void
41 on_select (PsppireSelector *sel, gpointer data)
42 {
43   GtkRadioButton *radiobutton2 = data;
44
45   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton2), TRUE);
46 }
47
48 static void
49 on_deselect (PsppireSelector *sel, gpointer data)
50 {
51   GtkRadioButton *radiobutton1 = data;
52
53   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton1), TRUE);
54 }
55
56
57 static void
58 on_toggle (GtkToggleButton *button, gpointer data)
59 {
60   GtkEntry *entry = data;
61   if ( gtk_toggle_button_get_active (button))
62     gtk_entry_set_text (entry, "");
63 }
64
65 struct weight_cases_dialog
66 {
67   PsppireDict *dict;
68   GtkEntry *entry;
69   GtkLabel *status;
70   GtkToggleButton *off;
71   GtkToggleButton *on;
72 };
73
74 static void
75 refresh (PsppireDialog *dialog, const struct weight_cases_dialog *wcd)
76 {
77   const struct variable *var = dict_get_weight (wcd->dict->dict);
78
79   if ( ! var )
80     {
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);
84     }
85   else
86     {
87       gchar *text =
88         g_strdup_printf (_("Weight cases by %s"), var_get_name (var));
89
90       gtk_entry_set_text (wcd->entry, var_get_name (var));
91       gtk_label_set_text (wcd->status, text);
92
93       g_free (text);
94       gtk_toggle_button_set_active (wcd->on, TRUE);
95     }
96
97   g_signal_emit_by_name (wcd->entry, "activate");
98 }
99
100
101 static gchar * generate_syntax (const struct weight_cases_dialog *wcd);
102
103
104 /* Pops up the Weight Cases dialog box */
105 void
106 weight_cases_dialog (GObject *o, gpointer data)
107 {
108   gint response;
109   struct data_editor *de = data;
110   struct weight_cases_dialog wcd;
111
112   GladeXML *xml = XML_NEW ("psppire.glade");
113
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");
122
123   GtkSheet *var_sheet =
124     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
125
126   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
127
128   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
129
130   g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
131   g_signal_connect (selector, "selected", G_CALLBACK (on_select),
132                     radiobutton2);
133
134   g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect),
135                     radiobutton1);
136
137   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
138                                  vs->dict,
139                                  GTK_SELECTION_SINGLE,
140                                  var_is_numeric
141                                  );
142
143   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
144                                  source,
145                                  entry,
146                                  insert_source_row_into_entry,
147                                  is_currently_in_entry
148                                  );
149
150
151   wcd.dict = vs->dict;
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);
156
157   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &wcd);
158
159   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
160
161   g_object_unref (xml);
162
163   switch (response)
164     {
165     case GTK_RESPONSE_OK:
166       {
167         gchar *syntax = generate_syntax (&wcd);
168         struct getl_interface *sss = create_syntax_string_source (syntax);
169         execute_syntax (sss);
170
171         g_free (syntax);
172       }
173       break;
174     case PSPPIRE_RESPONSE_PASTE:
175       {
176         gchar *syntax = generate_syntax (&wcd);
177
178         struct syntax_editor *se =
179           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
180
181         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
182
183         g_free (syntax);
184       }
185       break;
186     default:
187       break;
188     }
189 }
190
191
192 static gchar *
193 generate_syntax (const struct weight_cases_dialog *wcd)
194 {
195   gchar *syntax;
196
197   const gchar *text  = gtk_entry_get_text (wcd->entry);
198
199   struct variable *var = psppire_dict_lookup_var (wcd->dict, text);
200
201   if ( var == NULL)
202     syntax = g_strdup ("WEIGHT OFF.");
203   else
204     syntax = g_strdup_printf ("WEIGHT BY %s.\n",
205                               var_get_name (var));
206
207   return syntax;
208 }