New file: builder-wrapper.h and builder-wrapper.c
[pspp-builds.git] / src / ui / gui / weight-cases-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011, 2012  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 "builder-wrapper.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 (PsppireDataWindow *de)
106 {
107   gint response;
108   struct weight_cases_dialog wcd;
109
110   GtkBuilder *xml = builder_new ("psppire.ui");
111
112   GtkWidget *dialog = get_widget_assert (xml, "weight-cases-dialog");
113   GtkWidget *source = get_widget_assert (xml, "weight-cases-treeview");
114   GtkWidget *entry = get_widget_assert (xml, "weight-cases-entry");
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   GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
121
122   PsppireVarStore *vs = NULL;
123
124   g_object_get (de->data_editor, "var-store", &vs,  NULL);
125   g_object_get (vs, "dictionary", &wcd.dict, NULL);
126
127   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
128
129   g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
130
131   g_signal_connect (selector, "selected", G_CALLBACK (on_select), &wcd);
132   g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect), &wcd);
133   
134   g_object_set (source, "model", wcd.dict,
135                                  "selection-mode", GTK_SELECTION_SINGLE,
136                                  "predicate", var_is_numeric,
137                                  NULL);
138
139   psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector),
140                                     is_currently_in_entry);
141
142   wcd.entry = GTK_ENTRY (entry);
143   wcd.status = GTK_LABEL (status);
144   wcd.off = GTK_TOGGLE_BUTTON (radiobutton1);
145   wcd.on = GTK_TOGGLE_BUTTON (radiobutton2);
146
147   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &wcd);
148
149   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
150
151   g_object_unref (xml);
152
153   switch (response)
154     {
155     case GTK_RESPONSE_OK:
156       g_free (execute_syntax_string (de, generate_syntax (&wcd)));
157       break;
158     case PSPPIRE_RESPONSE_PASTE:
159       g_free (paste_syntax_to_window (generate_syntax (&wcd)));
160       break;
161     default:
162       break;
163     }
164 }
165
166
167 static gchar *
168 generate_syntax (const struct weight_cases_dialog *wcd)
169 {
170   gchar *syntax;
171
172   const gchar *text  = gtk_entry_get_text (wcd->entry);
173
174   struct variable *var = psppire_dict_lookup_var (wcd->dict, text);
175
176   if ( var == NULL)
177     syntax = g_strdup ("WEIGHT OFF.");
178   else
179     syntax = g_strdup_printf ("WEIGHT BY %s.\n",
180                               var_get_name (var));
181
182   return syntax;
183 }