1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 Free Software Foundation, Inc.
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.
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.
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/>. */
19 This widget is a GtkBox which looks roughly like:
21 +-----------------------------+
22 |+------------+ +----------+ |
32 |+------------+ +----------+ |
33 +-----------------------------+
40 #include "psppire-acr.h"
42 static void psppire_acr_init (PsppireAcr *);
45 psppire_acr_get_type (void)
47 static GType acr_type = 0;
51 static const GTypeInfo acr_info =
53 sizeof (PsppireAcrClass),
55 NULL, /* base_finalize */
56 NULL, /* class_init */
57 NULL, /* class_finalize */
58 NULL, /* class_data */
61 (GInstanceInitFunc) psppire_acr_init,
64 acr_type = g_type_register_static (GTK_TYPE_HBOX, "PsppireAcr",
72 static gboolean row_is_selected (const PsppireAcr *acr);
76 value_from_entry (gint col, GValue *val, gpointer data)
78 GtkEntry *entry = data;
79 const gchar *text = gtk_entry_get_text (entry);
80 gdouble x = g_strtod (text, 0);
82 g_value_init (val, G_TYPE_DOUBLE);
83 g_value_set_double (val, x);
89 /* Returns true, if there's text in the entry */
91 entry_not_empty (gpointer data)
93 GtkEntry *entry = data;
95 const char *text = gtk_entry_get_text (entry);
97 return !g_str_equal (text, "");
102 clear_entry (gpointer data)
104 GtkEntry *entry = data;
105 gtk_entry_set_text (entry, "");
110 on_entry_change (GtkEntry *entry, PsppireAcr *acr)
112 gtk_widget_set_sensitive (acr->add_button, acr->enabled (entry));
114 gtk_widget_set_sensitive (acr->change_button, acr->enabled (entry)
115 && row_is_selected (acr));
119 psppire_acr_set_entry (PsppireAcr *acr, GtkEntry *entry)
121 acr->get_value = value_from_entry;
122 acr->get_value_data = entry;
123 acr->enabled = entry_not_empty;
124 acr->enabled_data = entry;
125 acr->update = clear_entry;
126 acr->update_data = entry;
128 g_signal_connect (entry, "changed", G_CALLBACK (on_entry_change), acr);
132 /* Callback for when the Add button is clicked.
133 It appends an item to the list. */
135 on_add_button_clicked (PsppireAcr *acr)
139 gtk_list_store_append (acr->list_store, &iter);
142 i < gtk_tree_model_get_n_columns (GTK_TREE_MODEL (acr->list_store));
146 if ( ! acr->get_value (i, &value, acr->get_value_data) )
149 gtk_list_store_set_value (acr->list_store, &iter,
151 g_value_unset (&value);
154 if (acr->update) acr->update (acr->update_data);
158 /* Callback for when the Changed button is clicked.
159 It replaces the currently selected entry. */
161 on_change_button_clicked (PsppireAcr *acr)
164 GtkTreeModel *model = GTK_TREE_MODEL (acr->list_store);
167 gtk_tree_selection_get_selected_rows (acr->selection,
170 GtkTreePath *path = l->data;
174 gtk_tree_model_get_iter (model, &iter, path);
177 i < gtk_tree_model_get_n_columns (GTK_TREE_MODEL (acr->list_store));
181 if ( ! acr->get_value (i, &value, acr->get_value_data) )
184 gtk_list_store_set_value (acr->list_store, &iter,
186 g_value_unset (&value);
189 g_list_foreach (l, (GFunc) gtk_tree_path_free, NULL);
192 if ( acr->update) acr->update (acr->update_data);
196 /* Callback for when the remove button is clicked.
197 It deletes the currently selected entry. */
199 on_remove_button_clicked (PsppireAcr *acr)
201 GtkTreeModel *model = GTK_TREE_MODEL (acr->list_store);
204 gtk_tree_selection_get_selected_rows (acr->selection,
207 GtkTreePath *path = l->data;
211 gtk_tree_model_get_iter (model, &iter, path);
213 gtk_list_store_remove (acr->list_store, &iter);
215 g_list_foreach (l, (GFunc) gtk_tree_path_free, NULL);
219 /* Returns true if there is a row currently selected.
222 row_is_selected (const PsppireAcr *acr)
225 GtkTreeModel *model = GTK_TREE_MODEL (acr->list_store);
226 GList *l = gtk_tree_selection_get_selected_rows (acr->selection,
229 result = (l != NULL);
231 g_list_foreach (l, (GFunc) gtk_tree_path_free, NULL);
238 /* Callback which occurs when an item in the treeview
241 on_select (GtkTreeSelection *selection, gpointer data)
243 PsppireAcr *acr = data;
245 gtk_widget_set_sensitive (acr->remove_button, row_is_selected (acr));
247 gtk_widget_set_sensitive (acr->change_button,
248 row_is_selected (acr)
254 psppire_acr_set_enabled (PsppireAcr *acr, gboolean status)
257 gtk_widget_set_sensitive (acr->add_button, status);
259 gtk_widget_set_sensitive (acr->change_button, status
260 && row_is_selected (acr));
265 psppire_acr_init (PsppireAcr *acr)
267 GtkWidget *bb = gtk_vbutton_box_new ();
269 GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
271 acr->tv = GTK_TREE_VIEW (gtk_tree_view_new ());
273 acr->add_button = gtk_button_new_from_stock (GTK_STOCK_ADD);
274 acr->change_button = gtk_button_new_from_stock (GTK_STOCK_EDIT);
275 acr->remove_button = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
277 acr->get_value = NULL;
278 acr->get_value_data = NULL;
282 gtk_widget_set_sensitive (acr->change_button, FALSE);
283 gtk_widget_set_sensitive (acr->remove_button, FALSE);
284 gtk_widget_set_sensitive (acr->add_button, FALSE);
286 gtk_box_pack_start_defaults (GTK_BOX (bb), acr->add_button);
287 gtk_box_pack_start_defaults (GTK_BOX (bb), acr->change_button);
288 gtk_box_pack_start_defaults (GTK_BOX (bb), acr->remove_button);
290 gtk_box_pack_start (GTK_BOX (acr), bb, FALSE, TRUE, 5);
293 "hscrollbar-policy", GTK_POLICY_NEVER,
294 "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
295 "shadow-type", GTK_SHADOW_ETCHED_IN,
298 gtk_container_add (GTK_CONTAINER (sw), GTK_WIDGET (acr->tv));
300 gtk_box_pack_start (GTK_BOX (acr), sw, TRUE, TRUE, 5);
303 g_signal_connect_swapped (acr->add_button, "clicked",
304 G_CALLBACK (on_add_button_clicked), acr);
305 g_signal_connect_swapped (acr->change_button, "clicked",
306 G_CALLBACK (on_change_button_clicked), acr);
307 g_signal_connect_swapped (acr->remove_button, "clicked",
308 G_CALLBACK (on_remove_button_clicked), acr);
310 gtk_widget_show_all (bb);
313 g_object_set (acr->tv, "headers-visible", FALSE, NULL);
315 acr->list_store = NULL;
317 psppire_acr_set_model (acr, acr->list_store);
319 acr->selection = gtk_tree_view_get_selection (acr->tv);
321 g_signal_connect (acr->selection, "changed", G_CALLBACK (on_select), acr);
323 gtk_widget_set_sensitive (GTK_WIDGET (acr), FALSE);
325 gtk_widget_show_all (sw);
328 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
329 GtkTreeViewColumn *column =
330 gtk_tree_view_column_new_with_attributes ("value",
335 gtk_tree_view_append_column (acr->tv, column);
342 psppire_acr_new (void)
344 return GTK_WIDGET (g_object_new (psppire_acr_get_type (), NULL));
349 /* Set the widget's treemodel */
351 psppire_acr_set_model (PsppireAcr *acr, GtkListStore *liststore)
353 acr->list_store = liststore;
355 gtk_tree_view_set_model (GTK_TREE_VIEW (acr->tv),
356 GTK_TREE_MODEL (liststore));
358 gtk_widget_set_sensitive (GTK_WIDGET (acr), liststore != NULL);
363 psppire_acr_set_enable_func (PsppireAcr *acr, EnabledFunc func, gpointer p)
366 acr->enabled_data = p;
370 psppire_acr_set_get_value_func (PsppireAcr *acr,
371 GetValueFunc getvalue, gpointer data)
373 acr->get_value_data = data;
374 acr->get_value = getvalue;