fa85ae4a02ce045c56af6cbc343b9a28f6b9b1f9
[pspp-builds.git] / src / ui / gui / weight-cases-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011  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 "helper.h"
26
27 #include <gtk/gtk.h>
28
29 #include <gettext.h>
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33
34 #include "psppire-var-store.h"
35
36 struct weight_cases_dialog
37 {
38   PsppireDict *dict;
39   GtkEntry *entry;
40   GtkLabel *status;
41   GtkToggleButton *off;
42   GtkToggleButton *on;
43 };
44
45 static void
46 on_select (PsppireSelector *sel, gpointer data)
47 {
48   struct weight_cases_dialog *wcd = data;
49
50   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->on), TRUE);
51   gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), TRUE);
52 }
53
54 static void
55 on_deselect (PsppireSelector *sel, gpointer data)
56 {
57   struct weight_cases_dialog *wcd = data;
58
59   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->off), TRUE);
60   gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), FALSE);
61 }
62
63
64 static void
65 on_toggle (GtkToggleButton *button, gpointer data)
66 {
67   GtkEntry *entry = data;
68   if ( gtk_toggle_button_get_active (button))
69     gtk_entry_set_text (entry, "");
70 }
71
72 static void
73 refresh (PsppireDialog *dialog, const struct weight_cases_dialog *wcd)
74 {
75   const struct variable *var = dict_get_weight (wcd->dict->dict);
76
77   if ( ! var )
78     {
79       gtk_entry_set_text (wcd->entry, "");
80       gtk_label_set_text (wcd->status, _("Do not weight cases"));
81       gtk_toggle_button_set_active (wcd->off, TRUE);
82     }
83   else
84     {
85       gchar *text =
86         g_strdup_printf (_("Weight cases by %s"), var_get_name (var));
87
88       gtk_entry_set_text (wcd->entry, var_get_name (var));
89       gtk_label_set_text (wcd->status, text);
90
91       g_free (text);
92       gtk_toggle_button_set_active (wcd->on, TRUE);
93     }
94
95   g_signal_emit_by_name (wcd->entry, "activate");
96 }
97
98
99 static gchar * generate_syntax (const struct weight_cases_dialog *wcd);
100
101
102 /* Pops up the Weight Cases dialog box */
103 void
104 weight_cases_dialog (PsppireDataWindow *de)
105 {
106   gint response;
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 *radiobutton1 = get_widget_assert (xml,
115                                                "weight-cases-radiobutton1");
116   GtkWidget *radiobutton2 = get_widget_assert (xml, "radiobutton2");
117   GtkWidget *status  = get_widget_assert (xml, "weight-status-label");
118
119   GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
120
121   PsppireVarStore *vs = NULL;
122
123   g_object_get (de->data_editor, "var-store", &vs,  NULL);
124   g_object_get (vs, "dictionary", &wcd.dict, NULL);
125
126   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
127
128   g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
129
130   g_signal_connect (selector, "selected", G_CALLBACK (on_select), &wcd);
131   g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect), &wcd);
132   
133   g_object_set (source, "model", wcd.dict,
134                                  "selection-mode", GTK_SELECTION_SINGLE,
135                                  "predicate", var_is_numeric,
136                                  NULL);
137
138   psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector),
139                                     is_currently_in_entry);
140
141   wcd.entry = GTK_ENTRY (entry);
142   wcd.status = GTK_LABEL (status);
143   wcd.off = GTK_TOGGLE_BUTTON (radiobutton1);
144   wcd.on = GTK_TOGGLE_BUTTON (radiobutton2);
145
146   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &wcd);
147
148   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
149
150   g_object_unref (xml);
151
152   switch (response)
153     {
154     case GTK_RESPONSE_OK:
155       g_free (execute_syntax_string (de, generate_syntax (&wcd)));
156       break;
157     case PSPPIRE_RESPONSE_PASTE:
158       g_free (paste_syntax_to_window (generate_syntax (&wcd)));
159       break;
160     default:
161       break;
162     }
163 }
164
165
166 static gchar *
167 generate_syntax (const struct weight_cases_dialog *wcd)
168 {
169   gchar *syntax;
170
171   const gchar *text  = gtk_entry_get_text (wcd->entry);
172
173   struct variable *var = psppire_dict_lookup_var (wcd->dict, text);
174
175   if ( var == NULL)
176     syntax = g_strdup ("WEIGHT OFF.");
177   else
178     syntax = g_strdup_printf ("WEIGHT BY %s.\n",
179                               var_get_name (var));
180
181   return syntax;
182 }