Removed some calls to deprecated Gtk functions
[pspp-builds.git] / src / ui / gui / psppire-acr.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007 Free Software Foundation, Inc.
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
18 /*
19   This widget is a GtkBox which looks roughly like:
20
21   +-----------------------------+
22   |+------------+  +----------+ |
23   ||   Add      |  |          | |
24   |+------------+  |          | |
25   |                |          | |
26   |+------------+  |          | |
27   ||   Edit     |  |          | |
28   |+------------+  |          | |
29   |                |          | |
30   |+------------+  |          | |
31   ||  Remove    |  |          | |
32   |+------------+  +----------+ |
33   +-----------------------------+
34
35 */
36
37 #include <config.h>
38 #include <gtk/gtk.h>
39
40 #include "psppire-acr.h"
41 #include "helper.h"
42
43 static void psppire_acr_init (PsppireAcr *);
44
45 GType
46 psppire_acr_get_type (void)
47 {
48   static GType acr_type = 0;
49
50   if (!acr_type)
51     {
52       static const GTypeInfo acr_info =
53       {
54         sizeof (PsppireAcrClass),
55         NULL, /* base_init */
56         NULL, /* base_finalize */
57         NULL, /* class_init */
58         NULL, /* class_finalize */
59         NULL, /* class_data */
60         sizeof (PsppireAcr),
61         0,
62         (GInstanceInitFunc) psppire_acr_init,
63       };
64
65       acr_type = g_type_register_static (GTK_TYPE_HBOX, "PsppireAcr",
66                                         &acr_info, 0);
67     }
68
69   return acr_type;
70 }
71
72
73 static gboolean row_is_selected (const PsppireAcr *acr);
74
75
76 static gboolean
77 value_from_entry (gint col, GValue *val, gpointer data)
78 {
79   GtkEntry *entry = data;
80   const gchar *text = gtk_entry_get_text (entry);
81   gdouble x = g_strtod (text, 0);
82
83   g_value_init (val, G_TYPE_DOUBLE);
84   g_value_set_double (val, x);
85
86   return TRUE;
87 }
88
89
90 /* Returns true, if there's text in the entry */
91 static gboolean
92 entry_not_empty (gpointer data)
93 {
94   GtkEntry *entry = data;
95
96   const char *text = gtk_entry_get_text (entry);
97
98   return !g_str_equal (text, "");
99 }
100
101
102 static void
103 clear_entry (gpointer data)
104 {
105   GtkEntry *entry = data;
106   gtk_entry_set_text (entry, "");
107 }
108
109
110 static void
111 on_entry_change (GtkEntry *entry, PsppireAcr *acr)
112 {
113   gtk_widget_set_sensitive (acr->add_button, acr->enabled (entry));
114
115   gtk_widget_set_sensitive (acr->change_button, acr->enabled (entry)
116                             && row_is_selected (acr));
117 }
118
119 void
120 psppire_acr_set_entry  (PsppireAcr *acr, GtkEntry *entry)
121 {
122   acr->get_value = value_from_entry;
123   acr->get_value_data = entry;
124   acr->enabled = entry_not_empty;
125   acr->enabled_data = entry;
126   acr->update = clear_entry;
127   acr->update_data = entry;
128
129   g_signal_connect (entry, "changed", G_CALLBACK (on_entry_change), acr);
130 }
131
132
133 /* Callback for when the Add button is clicked.
134    It appends an item to the list. */
135 static void
136 on_add_button_clicked (PsppireAcr *acr)
137 {
138   gint i;
139   GtkTreeIter iter;
140   gtk_list_store_append (acr->list_store, &iter);
141
142   for (i = 0 ;
143        i < gtk_tree_model_get_n_columns (GTK_TREE_MODEL (acr->list_store));
144        ++i)
145     {
146       static GValue value;
147       if ( ! acr->get_value (i, &value, acr->get_value_data) )
148         continue;
149
150       gtk_list_store_set_value (acr->list_store, &iter,
151                                 i, &value);
152       g_value_unset (&value);
153     }
154
155   if (acr->update) acr->update (acr->update_data);
156 }
157
158
159 /* Callback for when the Changed button is clicked.
160    It replaces the currently selected entry. */
161 static void
162 on_change_button_clicked (PsppireAcr *acr)
163 {
164   gint i;
165   GtkTreeModel *model = GTK_TREE_MODEL (acr->list_store);
166
167   GList *l=
168     gtk_tree_selection_get_selected_rows (acr->selection,
169                                           &model);
170
171   GtkTreePath *path = l->data;
172
173   GtkTreeIter iter;
174
175   gtk_tree_model_get_iter (model, &iter, path);
176
177   for (i = 0 ;
178        i < gtk_tree_model_get_n_columns (GTK_TREE_MODEL (acr->list_store));
179        ++i)
180     {
181       static GValue value;
182       if ( ! acr->get_value (i, &value, acr->get_value_data) )
183         continue;
184
185       gtk_list_store_set_value (acr->list_store, &iter,
186                                 i, &value);
187       g_value_unset (&value);
188     }
189
190   g_list_foreach (l, (GFunc) gtk_tree_path_free, NULL);
191   g_list_free (l);
192
193   if ( acr->update) acr->update (acr->update_data);
194 }
195
196
197 /* Callback for when the remove button is clicked.
198    It deletes the currently selected entry. */
199 static void
200 on_remove_button_clicked (PsppireAcr *acr)
201 {
202   GtkTreeModel *model = GTK_TREE_MODEL (acr->list_store);
203
204   GList *l=
205     gtk_tree_selection_get_selected_rows (acr->selection,
206                                           &model);
207
208   GtkTreePath *path = l->data;
209
210   GtkTreeIter iter;
211
212   gtk_tree_model_get_iter (model, &iter, path);
213
214   gtk_list_store_remove (acr->list_store, &iter);
215
216   g_list_foreach (l, (GFunc) gtk_tree_path_free, NULL);
217   g_list_free (l);
218 }
219
220 /* Returns true if there is a row currently selected.
221    False otherwise. */
222 static gboolean
223 row_is_selected (const PsppireAcr *acr)
224 {
225   gboolean result;
226   GtkTreeModel *model = GTK_TREE_MODEL (acr->list_store);
227   GList *l = gtk_tree_selection_get_selected_rows (acr->selection,
228                                                    &model);
229
230   result = (l != NULL);
231
232   g_list_foreach (l, (GFunc) gtk_tree_path_free, NULL);
233   g_list_free (l);
234
235   return result;
236 }
237
238
239 /* Callback which occurs when an item in the treeview
240    is selected */
241 static void
242 on_select (GtkTreeSelection *selection, gpointer data)
243 {
244   PsppireAcr *acr = data;
245
246   gtk_widget_set_sensitive (acr->remove_button, row_is_selected (acr));
247
248   gtk_widget_set_sensitive (acr->change_button,
249                             row_is_selected (acr)
250                             );
251 }
252
253
254 void
255 psppire_acr_set_enabled (PsppireAcr *acr, gboolean status)
256 {
257
258   gtk_widget_set_sensitive (acr->add_button, status);
259
260   gtk_widget_set_sensitive (acr->change_button, status
261                             && row_is_selected (acr));
262 }
263
264
265 static void
266 psppire_acr_init (PsppireAcr *acr)
267 {
268   GtkWidget *bb  = gtk_vbutton_box_new ();
269
270   GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
271
272   acr->tv = GTK_TREE_VIEW (gtk_tree_view_new ());
273
274   acr->add_button = gtk_button_new_from_stock (GTK_STOCK_ADD);
275   acr->change_button = gtk_button_new_from_stock (GTK_STOCK_EDIT);
276   acr->remove_button = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
277
278   acr->get_value = NULL;
279   acr->get_value_data = NULL;
280   acr->enabled = NULL;
281   acr->update = NULL;
282
283   gtk_widget_set_sensitive (acr->change_button, FALSE);
284   gtk_widget_set_sensitive (acr->remove_button, FALSE);
285   gtk_widget_set_sensitive (acr->add_button, FALSE);
286
287   psppire_box_pack_start_defaults (GTK_BOX (bb), acr->add_button);
288   psppire_box_pack_start_defaults (GTK_BOX (bb), acr->change_button);
289   psppire_box_pack_start_defaults (GTK_BOX (bb), acr->remove_button);
290
291   gtk_box_pack_start (GTK_BOX (acr), bb, FALSE, TRUE, 5);
292
293   g_object_set (sw,
294                 "hscrollbar-policy", GTK_POLICY_NEVER,
295                 "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
296                 "shadow-type", GTK_SHADOW_ETCHED_IN,
297                 NULL);
298
299   gtk_container_add (GTK_CONTAINER (sw), GTK_WIDGET (acr->tv));
300
301   gtk_box_pack_start (GTK_BOX (acr), sw, TRUE, TRUE, 5);
302
303
304   g_signal_connect_swapped (acr->add_button, "clicked",
305                             G_CALLBACK (on_add_button_clicked), acr);
306   g_signal_connect_swapped (acr->change_button, "clicked",
307                             G_CALLBACK (on_change_button_clicked), acr);
308   g_signal_connect_swapped (acr->remove_button, "clicked",
309                             G_CALLBACK (on_remove_button_clicked), acr);
310
311   gtk_widget_show_all (bb);
312
313
314   g_object_set (acr->tv, "headers-visible", FALSE, NULL);
315
316   acr->list_store = NULL;
317
318   psppire_acr_set_model (acr, acr->list_store);
319
320   acr->selection = gtk_tree_view_get_selection (acr->tv);
321
322   g_signal_connect (acr->selection, "changed", G_CALLBACK (on_select), acr);
323
324   gtk_widget_set_sensitive (GTK_WIDGET (acr), FALSE);
325
326   gtk_widget_show_all (sw);
327
328   {
329     GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
330     GtkTreeViewColumn *column =
331       gtk_tree_view_column_new_with_attributes ("value",
332                                                 renderer,
333                                                 "text", 0,
334                                                 NULL);
335
336     gtk_tree_view_append_column (acr->tv, column);
337   }
338
339 }
340
341
342 GtkWidget*
343 psppire_acr_new (void)
344 {
345   return GTK_WIDGET (g_object_new (psppire_acr_get_type (), NULL));
346 }
347
348
349
350 /* Set the widget's treemodel */
351 void
352 psppire_acr_set_model (PsppireAcr *acr, GtkListStore *liststore)
353 {
354   acr->list_store = liststore;
355
356   gtk_tree_view_set_model (GTK_TREE_VIEW (acr->tv),
357                            GTK_TREE_MODEL (liststore));
358
359   gtk_widget_set_sensitive (GTK_WIDGET (acr), liststore != NULL);
360 }
361
362
363 void
364 psppire_acr_set_enable_func (PsppireAcr *acr, EnabledFunc func, gpointer p)
365 {
366   acr->enabled = func;
367   acr->enabled_data = p;
368 }
369
370 void
371 psppire_acr_set_get_value_func (PsppireAcr *acr,
372                                 GetValueFunc getvalue, gpointer data)
373 {
374   acr->get_value_data = data;
375   acr->get_value = getvalue;
376 }