Add "dictionary" property to PsppireVarStore and use it.
[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 static void
38 on_select (PsppireSelector *sel, gpointer data)
39 {
40   GtkRadioButton *radiobutton2 = data;
41
42   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton2), TRUE);
43 }
44
45 static void
46 on_deselect (PsppireSelector *sel, gpointer data)
47 {
48   GtkRadioButton *radiobutton1 = data;
49
50   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton1), TRUE);
51 }
52
53
54 static void
55 on_toggle (GtkToggleButton *button, gpointer data)
56 {
57   GtkEntry *entry = data;
58   if ( gtk_toggle_button_get_active (button))
59     gtk_entry_set_text (entry, "");
60 }
61
62 struct weight_cases_dialog
63 {
64   PsppireDict *dict;
65   GtkEntry *entry;
66   GtkLabel *status;
67   GtkToggleButton *off;
68   GtkToggleButton *on;
69 };
70
71 static void
72 refresh (PsppireDialog *dialog, const struct weight_cases_dialog *wcd)
73 {
74   const struct variable *var = dict_get_weight (wcd->dict->dict);
75
76   if ( ! var )
77     {
78       gtk_entry_set_text (wcd->entry, "");
79       gtk_label_set_text (wcd->status, _("Do not weight cases"));
80       gtk_toggle_button_set_active (wcd->off, TRUE);
81     }
82   else
83     {
84       gchar *text =
85         g_strdup_printf (_("Weight cases by %s"), var_get_name (var));
86
87       gtk_entry_set_text (wcd->entry, var_get_name (var));
88       gtk_label_set_text (wcd->status, text);
89
90       g_free (text);
91       gtk_toggle_button_set_active (wcd->on, TRUE);
92     }
93
94   g_signal_emit_by_name (wcd->entry, "activate");
95 }
96
97
98 static gchar * generate_syntax (const struct weight_cases_dialog *wcd);
99
100
101 /* Pops up the Weight Cases dialog box */
102 void
103 weight_cases_dialog (GObject *o, gpointer data)
104 {
105   gint response;
106   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
107   struct weight_cases_dialog wcd;
108
109   GtkBuilder *xml = builder_new ("psppire.ui");
110
111   GtkWidget *dialog = get_widget_assert (xml, "weight-cases-dialog");
112   GtkWidget *source = get_widget_assert (xml, "weight-cases-treeview");
113   GtkWidget *entry = get_widget_assert (xml, "weight-cases-entry");
114   GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
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");
119
120   PsppireVarStore *vs = NULL;
121
122   g_object_get (de->data_editor, "var-store", &vs,  NULL);
123   g_object_get (vs, "dictionary", &wcd.dict, NULL);
124
125   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
126
127   g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
128   g_signal_connect (selector, "selected", G_CALLBACK (on_select),
129                     radiobutton2);
130
131   g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect),
132                     radiobutton1);
133
134   
135   g_object_set (source, "dictionary", wcd.dict,
136                                  "selection-mode", GTK_SELECTION_SINGLE,
137                                  "predicate", var_is_numeric,
138                                  NULL);
139
140   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
141                                  source,
142                                  entry,
143                                  insert_source_row_into_entry,
144                                  is_currently_in_entry,
145                                  NULL
146                                  );
147
148
149
150   wcd.entry = GTK_ENTRY (entry);
151   wcd.status = GTK_LABEL (status);
152   wcd.off = GTK_TOGGLE_BUTTON (radiobutton1);
153   wcd.on = GTK_TOGGLE_BUTTON (radiobutton2);
154
155   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &wcd);
156
157   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
158
159   g_object_unref (xml);
160
161   switch (response)
162     {
163     case GTK_RESPONSE_OK:
164       {
165         gchar *syntax = generate_syntax (&wcd);
166         struct getl_interface *sss = create_syntax_string_source (syntax);
167         execute_syntax (sss);
168
169         g_free (syntax);
170       }
171       break;
172     case PSPPIRE_RESPONSE_PASTE:
173       {
174         gchar *syntax = generate_syntax (&wcd);
175         paste_syntax_in_new_window (syntax);
176         g_free (syntax);
177       }
178       break;
179     default:
180       break;
181     }
182 }
183
184
185 static gchar *
186 generate_syntax (const struct weight_cases_dialog *wcd)
187 {
188   gchar *syntax;
189
190   const gchar *text  = gtk_entry_get_text (wcd->entry);
191
192   struct variable *var = psppire_dict_lookup_var (wcd->dict, text);
193
194   if ( var == NULL)
195     syntax = g_strdup ("WEIGHT OFF.");
196   else
197     syntax = g_strdup_printf ("WEIGHT BY %s.\n",
198                               var_get_name (var));
199
200   return syntax;
201 }