31907a15fa21cc5768f7f77d0aa90c26766f8463
[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 /* FIXME: These shouldn't be here */
35 #include <gtksheet/gtksheet.h>
36 #include "psppire-var-store.h"
37
38 static void
39 on_select (PsppireSelector *sel, gpointer data)
40 {
41   GtkRadioButton *radiobutton2 = data;
42
43   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton2), TRUE);
44 }
45
46 static void
47 on_deselect (PsppireSelector *sel, gpointer data)
48 {
49   GtkRadioButton *radiobutton1 = data;
50
51   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton1), TRUE);
52 }
53
54
55 static void
56 on_toggle (GtkToggleButton *button, gpointer data)
57 {
58   GtkEntry *entry = data;
59   if ( gtk_toggle_button_get_active (button))
60     gtk_entry_set_text (entry, "");
61 }
62
63
64
65
66 static gchar * generate_syntax (PsppireDict *, GtkEntry *);
67
68
69 /* Pops up the Weight Cases dialog box */
70 void
71 weight_cases_dialog (GObject *o, gpointer data)
72 {
73   gint response;
74   struct data_editor *de = data;
75   PsppireDict *dict;
76   struct variable *var;
77
78   GladeXML *xml = XML_NEW ("psppire.glade");
79
80   GtkWidget *dialog = get_widget_assert (xml, "weight-cases-dialog");
81   GtkWidget *source = get_widget_assert (xml, "weight-cases-treeview");
82   GtkWidget *entry = get_widget_assert (xml, "weight-cases-entry");
83   GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
84   GtkWidget *radiobutton1 = get_widget_assert (xml,
85                                                "weight-cases-radiobutton1");
86   GtkWidget *radiobutton2 = get_widget_assert (xml, "radiobutton2");
87
88   GtkSheet *var_sheet =
89     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
90
91   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
92
93   dict = vs->dict;
94
95   g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
96   g_signal_connect (selector, "selected", G_CALLBACK (on_select),
97                     radiobutton2);
98
99   g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect),
100                     radiobutton1);
101
102   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
103                                  dict,
104                                  GTK_SELECTION_SINGLE,
105                                  var_is_numeric
106                                  );
107
108   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
109                                  source,
110                                  entry,
111                                  insert_source_row_into_entry,
112                                  is_currently_in_entry
113                                  );
114
115   var = dict_get_weight (dict->dict);
116   if ( ! var )
117     gtk_entry_set_text (GTK_ENTRY (entry), "");
118   else
119     gtk_entry_set_text (GTK_ENTRY (entry), var_get_name (var));
120
121   g_signal_emit_by_name (entry, "activate");
122
123   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
124
125   g_object_unref (xml);
126
127   switch (response)
128     {
129     case GTK_RESPONSE_OK:
130       {
131         gchar *syntax = generate_syntax (dict, GTK_ENTRY (entry));
132         struct getl_interface *sss = create_syntax_string_source (syntax);
133         execute_syntax (sss);
134
135         g_free (syntax);
136       }
137       break;
138     case PSPPIRE_RESPONSE_PASTE:
139       {
140         gchar *syntax = generate_syntax (dict, GTK_ENTRY (entry));
141
142         struct syntax_editor *se =
143           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
144
145         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
146
147         g_free (syntax);
148       }
149       break;
150     default:
151       break;
152     }
153 }
154
155
156 static gchar *
157 generate_syntax (PsppireDict *dict, GtkEntry *entry)
158 {
159   gchar *syntax;
160
161   const gchar *text  = gtk_entry_get_text (entry);
162
163   struct variable *var = psppire_dict_lookup_var (dict, text);
164
165   if ( var == NULL)
166     syntax = g_strdup ("WEIGHT OFF.");
167   else
168     syntax = g_strdup_printf ("WEIGHT BY %s.\n",
169                               var_get_name (var));
170
171   return syntax;
172 }