Added custom psppire-selector widget.
[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 = glade_xml_new (PKGDATADIR "/psppire.glade",
79                                  "weight-cases-dialog", NULL);
80
81   GtkWidget *dialog = get_widget_assert (xml, "weight-cases-dialog");
82   GtkWidget *source = get_widget_assert (xml, "weight-cases-treeview");
83   GtkWidget *entry = get_widget_assert (xml, "weight-cases-entry");
84   GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
85   GtkWidget *radiobutton1 = get_widget_assert (xml,
86                                                "weight-cases-radiobutton1");
87   GtkWidget *radiobutton2 = get_widget_assert (xml, "radiobutton2");
88
89   GtkSheet *var_sheet =
90     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
91
92   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
93
94   dict = vs->dict;
95
96   g_signal_connect (radiobutton1, "toggled", G_CALLBACK (on_toggle), entry);
97   g_signal_connect (selector, "selected", G_CALLBACK (on_select),
98                     radiobutton2);
99
100   g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect),
101                     radiobutton1);
102
103   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
104                                  dict,
105                                  GTK_SELECTION_SINGLE,
106                                  var_is_numeric
107                                  );
108
109   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
110                                  source,
111                                  entry,
112                                  insert_source_row_into_entry,
113                                  is_currently_in_entry
114                                  );
115
116   var = dict_get_weight (dict->dict);
117   if ( ! var )
118     gtk_entry_set_text (GTK_ENTRY (entry), "");
119   else
120     gtk_entry_set_text (GTK_ENTRY (entry), var_get_name (var));
121
122   g_signal_emit_by_name (entry, "activate");
123
124   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
125
126   g_object_unref (xml);
127
128   switch (response)
129     {
130     case GTK_RESPONSE_OK:
131       {
132         gchar *syntax = generate_syntax (dict, GTK_ENTRY (entry));
133         struct getl_interface *sss = create_syntax_string_source (syntax);
134         execute_syntax (sss);
135
136         g_free (syntax);
137       }
138       break;
139     case PSPPIRE_RESPONSE_PASTE:
140       {
141         gchar *syntax = generate_syntax (dict, GTK_ENTRY (entry));
142
143         struct syntax_editor *se =
144           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
145
146         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
147
148         g_free (syntax);
149       }
150       break;
151     default:
152       break;
153     }
154 }
155
156
157 static gchar *
158 generate_syntax (PsppireDict *dict, GtkEntry *entry)
159 {
160   gchar *syntax;
161
162   const gchar *text  = gtk_entry_get_text (entry);
163
164   struct variable *var = psppire_dict_lookup_var (dict, text);
165
166   if ( var == NULL)
167     syntax = g_strdup ("WEIGHT OFF.");
168   else
169     syntax = g_strdup_printf ("WEIGHT BY %s.\n",
170                               var_get_name (var));
171
172   return syntax;
173 }