1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011, 2012 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 #include "psppire-empty-list-store.h"
21 static void psppire_empty_list_store_class_init (PsppireEmptyListStoreClass *);
22 static void psppire_empty_list_store_init (PsppireEmptyListStore *);
24 /* GtkTreeModel interface. */
25 static void gtk_tree_model_interface_init (GtkTreeModelIface *iface);
26 static GtkTreeModelFlags empty_list_store_get_flags (GtkTreeModel *tree_model);
27 static gint empty_list_store_get_n_columns (GtkTreeModel *tree_model);
28 static GType empty_list_store_get_column_type (GtkTreeModel *tree_model,
30 static gboolean empty_list_store_get_iter (GtkTreeModel *tree_model,
33 static GtkTreePath * empty_list_store_get_path (GtkTreeModel *tree_model,
35 static void empty_list_store_get_value (GtkTreeModel *tree_model,
39 static gboolean empty_list_store_iter_next (GtkTreeModel *tree_model,
41 static gboolean empty_list_store_iter_children (GtkTreeModel *tree_model,
44 static gboolean empty_list_store_iter_has_child (GtkTreeModel *tree_model,
46 static gint empty_list_store_iter_n_children (GtkTreeModel *tree_model,
48 static gboolean empty_list_store_iter_nth_child (GtkTreeModel *tree_model,
52 static gboolean empty_list_store_iter_parent (GtkTreeModel *tree_model,
57 psppire_empty_list_store_get_type (void)
59 static GType type = 0;
62 static const GTypeInfo psppire_empty_list_store_info =
64 sizeof(PsppireEmptyListStoreClass),
66 NULL, /* base finalize */
67 (GClassInitFunc) psppire_empty_list_store_class_init,
68 NULL, /* class finalize */
69 NULL, /* class data */
70 sizeof(PsppireEmptyListStore),
71 0, /* n_preallocs, ignored since 2.10 */
72 (GInstanceInitFunc) psppire_empty_list_store_init,
75 static const GInterfaceInfo gtk_tree_model_info =
77 (GInterfaceInitFunc) gtk_tree_model_interface_init,
78 (GInterfaceFinalizeFunc) NULL,
81 type = g_type_register_static (G_TYPE_OBJECT,
82 "PsppireEmptyListStore",
83 &psppire_empty_list_store_info, 0);
84 g_type_add_interface_static (type, GTK_TYPE_TREE_MODEL,
85 >k_tree_model_info);
97 psppire_empty_list_store_set_property (GObject *object,
102 PsppireEmptyListStore *obj = PSPPIRE_EMPTY_LIST_STORE (object);
107 obj->n_rows = g_value_get_int (value);
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 psppire_empty_list_store_get_property (GObject *object,
122 PsppireEmptyListStore *obj = PSPPIRE_EMPTY_LIST_STORE (object);
127 g_value_set_int (value, obj->n_rows);
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137 psppire_empty_list_store_class_init (PsppireEmptyListStoreClass *class)
139 GObjectClass *gobject_class;
140 gobject_class = G_OBJECT_CLASS (class);
142 gobject_class->set_property = psppire_empty_list_store_set_property;
143 gobject_class->get_property = psppire_empty_list_store_get_property;
145 g_object_class_install_property (gobject_class,
147 g_param_spec_int ("n-rows",
149 ("Number of rows in GtkTreeModel"),
157 psppire_empty_list_store_init (PsppireEmptyListStore *obj)
162 PsppireEmptyListStore *
163 psppire_empty_list_store_new (gint n_rows)
165 return PSPPIRE_EMPTY_LIST_STORE (g_object_new (PSPPIRE_TYPE_EMPTY_LIST_STORE,
171 psppire_empty_list_store_get_n_rows (const PsppireEmptyListStore *obj)
177 psppire_empty_list_store_set_n_rows (PsppireEmptyListStore *obj,
180 obj->n_rows = n_rows;
184 psppire_empty_list_store_row_inserted (PsppireEmptyListStore *obj,
187 GtkTreeModel *tree_model = GTK_TREE_MODEL (obj);
191 path = gtk_tree_path_new_from_indices (row, -1);
192 gtk_tree_model_get_iter (tree_model, &iter, path);
193 gtk_tree_model_row_inserted (tree_model, path, &iter);
194 gtk_tree_path_free (path);
198 psppire_empty_list_store_row_deleted (PsppireEmptyListStore *obj,
201 GtkTreeModel *tree_model = GTK_TREE_MODEL (obj);
204 path = gtk_tree_path_new_from_indices (row, -1);
205 gtk_tree_model_row_deleted (tree_model, path);
206 gtk_tree_path_free (path);
209 /* GtkTreeModel interface. */
211 /* Random number used in 'stamp' member of GtkTreeIter. */
212 #define TREE_MODEL_STAMP 0x10c44c13
215 empty_list_store_init_iter (GtkTreeModel *model, gint idx, GtkTreeIter *iter)
217 const PsppireEmptyListStore *store = PSPPIRE_EMPTY_LIST_STORE (model);
219 if (idx < 0 || idx >= store->n_rows)
222 iter->user_data = GINT_TO_POINTER (-1);
227 iter->stamp = TREE_MODEL_STAMP;
228 iter->user_data = GINT_TO_POINTER (idx);
234 gtk_tree_model_interface_init (GtkTreeModelIface *iface)
236 g_return_if_fail (iface != NULL);
238 iface->get_flags = empty_list_store_get_flags;
239 iface->get_n_columns = empty_list_store_get_n_columns;
240 iface->get_column_type = empty_list_store_get_column_type;
241 iface->get_iter = empty_list_store_get_iter;
242 iface->get_path = empty_list_store_get_path;
243 iface->get_value = empty_list_store_get_value;
244 iface->iter_next = empty_list_store_iter_next;
245 iface->iter_children = empty_list_store_iter_children;
246 iface->iter_has_child = empty_list_store_iter_has_child;
247 iface->iter_n_children = empty_list_store_iter_n_children;
248 iface->iter_nth_child = empty_list_store_iter_nth_child;
249 iface->iter_parent = empty_list_store_iter_parent;
252 static GtkTreeModelFlags
253 empty_list_store_get_flags (GtkTreeModel *tree_model G_GNUC_UNUSED)
255 return GTK_TREE_MODEL_LIST_ONLY;
259 empty_list_store_get_n_columns (GtkTreeModel *tree_model G_GNUC_UNUSED)
265 empty_list_store_get_column_type (GtkTreeModel *tree_model,
268 g_return_val_if_reached (G_TYPE_NONE);
272 empty_list_store_get_iter (GtkTreeModel *tree_model,
276 gint *indices, depth;
278 g_return_val_if_fail (path, FALSE);
280 indices = gtk_tree_path_get_indices (path);
281 depth = gtk_tree_path_get_depth (path);
283 g_return_val_if_fail (depth == 1, FALSE);
285 return empty_list_store_init_iter (tree_model, indices[0], iter);
289 empty_list_store_get_path (GtkTreeModel *tree_model,
294 g_return_val_if_fail (iter->stamp == TREE_MODEL_STAMP, FALSE);
296 path = gtk_tree_path_new ();
297 gtk_tree_path_append_index (path, GPOINTER_TO_INT (iter->user_data));
303 empty_list_store_get_value (GtkTreeModel *tree_model,
308 g_return_if_reached ();
312 empty_list_store_iter_next (GtkTreeModel *tree_model,
317 g_return_val_if_fail (iter->stamp == TREE_MODEL_STAMP, FALSE);
319 idx = GPOINTER_TO_INT (iter->user_data);
320 return empty_list_store_init_iter (tree_model, idx + (idx >= 0), iter);
324 empty_list_store_iter_children (GtkTreeModel *tree_model,
332 empty_list_store_iter_has_child (GtkTreeModel *tree_model,
339 empty_list_store_iter_n_children (GtkTreeModel *tree_model,
342 return iter == NULL ? PSPPIRE_EMPTY_LIST_STORE (tree_model)->n_rows : 0;
346 empty_list_store_iter_nth_child (GtkTreeModel *tree_model,
351 g_return_val_if_fail (parent == NULL, FALSE);
353 return empty_list_store_init_iter (tree_model, n, iter);
357 empty_list_store_iter_parent (GtkTreeModel *tree_model,
365 empty_list_store_iter_to_row (const GtkTreeIter *iter)
367 g_return_val_if_fail (iter->stamp == TREE_MODEL_STAMP, 0);
368 return GPOINTER_TO_INT (iter->user_data);