treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / ui / gui / psppire-checkbox-treeview.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2012, 2013  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
18 /*
19    This module provides a subclass of GtkTreeView, designed for dialogs
20    which need lists of annotated checkbox items.
21    The object contains the necessary model and renderers, which means that
22    the user does not have to create these herself.
23  */
24
25 #include <config.h>
26 #include <gtk/gtk.h>
27
28 #include "psppire-checkbox-treeview.h"
29
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33 #define N_(msgid) msgid
34
35 G_DEFINE_TYPE (PsppireCheckboxTreeview, psppire_checkbox_treeview, GTK_TYPE_TREE_VIEW)
36
37 /* Callback for checkbox cells in the statistics tree view.
38    Toggles the checkbox. */
39 static void
40 toggle (GtkCellRendererToggle *cell_renderer, const gchar *path_str, gpointer data)
41 {
42   GtkTreeView *tv = GTK_TREE_VIEW (data);
43   GtkTreeModel *model = gtk_tree_view_get_model (tv);
44   GtkTreeIter iter;
45   GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
46   gboolean selected;
47
48   gtk_tree_model_get_iter (model, &iter, path);
49   gtk_tree_model_get (model, &iter, CHECKBOX_COLUMN_SELECTED, &selected, -1);
50   gtk_list_store_set (GTK_LIST_STORE (model), &iter, CHECKBOX_COLUMN_SELECTED,
51                       !selected, -1);
52   gtk_tree_path_free (path);
53 }
54
55 /* Create the necessary columns and renderers and add them to the widget */
56 static void
57 treeview_checkbox_populate (GtkTreeView *treeview)
58 {
59   /* Checkbox column. */
60   GtkTreeViewColumn *col = gtk_tree_view_column_new ();
61   GtkCellRenderer *renderer = gtk_cell_renderer_toggle_new ();
62
63   gtk_tree_view_column_pack_start (col, renderer, TRUE);
64
65   gtk_tree_view_append_column (treeview, col);
66
67   gtk_tree_view_column_add_attribute  (col, renderer, "active", CHECKBOX_COLUMN_SELECTED);
68
69   g_signal_connect (renderer, "toggled", G_CALLBACK (toggle), treeview);
70
71   /* Label column. */
72   col = gtk_tree_view_column_new ();
73   gtk_tree_view_column_set_title (col, _("Statistic"));
74   renderer = gtk_cell_renderer_text_new ();
75   gtk_tree_view_column_pack_start (col, renderer, TRUE);
76
77   gtk_tree_view_column_add_attribute  (col, renderer, "text", CHECKBOX_COLUMN_LABEL);
78
79   g_object_set (renderer, "ellipsize-set", TRUE, NULL);
80   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
81   gtk_tree_view_column_set_min_width (col, 200);
82   gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
83   gtk_tree_view_column_set_resizable (col, TRUE);
84   gtk_tree_view_append_column (treeview, col);
85 }
86
87 static void
88 psppire_checkbox_treeview_init (PsppireCheckboxTreeview *cbtv)
89 {
90   cbtv->list = GTK_TREE_MODEL (gtk_list_store_new (N_CHECKBOX_COLUMNS,
91                                                    G_TYPE_STRING,
92                                                    G_TYPE_BOOLEAN,
93                                                    G_TYPE_STRING));
94
95   gtk_tree_view_set_model (GTK_TREE_VIEW (cbtv), cbtv->list);
96   g_object_unref (cbtv->list);
97
98   treeview_checkbox_populate (GTK_TREE_VIEW (cbtv));
99 }
100
101
102 /*
103   Load the object's model from the array ITEMS.
104   N_ITEMS is the size of the array.
105   DEFAULT_ITEMS is a bitwise field indicating the initial state
106   of the items.
107 */
108 void
109 psppire_checkbox_treeview_populate (PsppireCheckboxTreeview *cbtv,
110                                     guint default_items,
111                                     gint n_items,
112                                     const struct checkbox_entry_item *items)
113 {
114   size_t i;
115   for (i = 0; i < n_items; ++i)
116     {
117       GtkTreeIter iter;
118       gtk_list_store_append (GTK_LIST_STORE (cbtv->list), &iter);
119       gtk_list_store_set (GTK_LIST_STORE (cbtv->list), &iter,
120                           CHECKBOX_COLUMN_LABEL, gettext (items[i].label),
121                           CHECKBOX_COLUMN_SELECTED,  (default_items & (1u << i)) != 0,
122                           CHECKBOX_COLUMN_TOOLTIP, gettext (items[i].tooltip),
123                           -1);
124     }
125
126   gtk_tree_view_set_tooltip_column (GTK_TREE_VIEW (cbtv), CHECKBOX_COLUMN_TOOLTIP);
127 }
128
129 static void
130 psppire_checkbox_treeview_class_init (PsppireCheckboxTreeviewClass *class)
131 {
132 }