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_changed (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_changed (tree_model, path, &iter);
194 gtk_tree_path_free (path);
198 psppire_empty_list_store_row_inserted (PsppireEmptyListStore *obj,
201 GtkTreeModel *tree_model = GTK_TREE_MODEL (obj);
205 path = gtk_tree_path_new_from_indices (row, -1);
206 gtk_tree_model_get_iter (tree_model, &iter, path);
207 gtk_tree_model_row_inserted (tree_model, path, &iter);
208 gtk_tree_path_free (path);
212 psppire_empty_list_store_row_deleted (PsppireEmptyListStore *obj,
215 GtkTreeModel *tree_model = GTK_TREE_MODEL (obj);
218 path = gtk_tree_path_new_from_indices (row, -1);
219 gtk_tree_model_row_deleted (tree_model, path);
220 gtk_tree_path_free (path);
223 /* GtkTreeModel interface. */
225 /* Random number used in 'stamp' member of GtkTreeIter. */
226 #define TREE_MODEL_STAMP 0x10c44c13
229 empty_list_store_init_iter (GtkTreeModel *model, gint idx, GtkTreeIter *iter)
231 const PsppireEmptyListStore *store = PSPPIRE_EMPTY_LIST_STORE (model);
233 if (idx < 0 || idx >= store->n_rows)
236 iter->user_data = GINT_TO_POINTER (-1);
241 iter->stamp = TREE_MODEL_STAMP;
242 iter->user_data = GINT_TO_POINTER (idx);
248 gtk_tree_model_interface_init (GtkTreeModelIface *iface)
250 g_return_if_fail (iface != NULL);
252 iface->get_flags = empty_list_store_get_flags;
253 iface->get_n_columns = empty_list_store_get_n_columns;
254 iface->get_column_type = empty_list_store_get_column_type;
255 iface->get_iter = empty_list_store_get_iter;
256 iface->get_path = empty_list_store_get_path;
257 iface->get_value = empty_list_store_get_value;
258 iface->iter_next = empty_list_store_iter_next;
259 iface->iter_children = empty_list_store_iter_children;
260 iface->iter_has_child = empty_list_store_iter_has_child;
261 iface->iter_n_children = empty_list_store_iter_n_children;
262 iface->iter_nth_child = empty_list_store_iter_nth_child;
263 iface->iter_parent = empty_list_store_iter_parent;
266 static GtkTreeModelFlags
267 empty_list_store_get_flags (GtkTreeModel *tree_model G_GNUC_UNUSED)
269 return GTK_TREE_MODEL_LIST_ONLY;
273 empty_list_store_get_n_columns (GtkTreeModel *tree_model G_GNUC_UNUSED)
279 empty_list_store_get_column_type (GtkTreeModel *tree_model,
282 g_return_val_if_reached (G_TYPE_NONE);
286 empty_list_store_get_iter (GtkTreeModel *tree_model,
290 gint *indices, depth;
292 g_return_val_if_fail (path, FALSE);
294 indices = gtk_tree_path_get_indices (path);
295 depth = gtk_tree_path_get_depth (path);
297 g_return_val_if_fail (depth == 1, FALSE);
299 return empty_list_store_init_iter (tree_model, indices[0], iter);
303 empty_list_store_get_path (GtkTreeModel *tree_model,
308 g_return_val_if_fail (iter->stamp == TREE_MODEL_STAMP, FALSE);
310 path = gtk_tree_path_new ();
311 gtk_tree_path_append_index (path, GPOINTER_TO_INT (iter->user_data));
317 empty_list_store_get_value (GtkTreeModel *tree_model,
322 g_return_if_reached ();
326 empty_list_store_iter_next (GtkTreeModel *tree_model,
331 g_return_val_if_fail (iter->stamp == TREE_MODEL_STAMP, FALSE);
333 idx = GPOINTER_TO_INT (iter->user_data);
334 return empty_list_store_init_iter (tree_model, idx + (idx >= 0), iter);
338 empty_list_store_iter_children (GtkTreeModel *tree_model,
346 empty_list_store_iter_has_child (GtkTreeModel *tree_model,
353 empty_list_store_iter_n_children (GtkTreeModel *tree_model,
356 return iter == NULL ? PSPPIRE_EMPTY_LIST_STORE (tree_model)->n_rows : 0;
360 empty_list_store_iter_nth_child (GtkTreeModel *tree_model,
365 g_return_val_if_fail (parent == NULL, FALSE);
367 return empty_list_store_init_iter (tree_model, n, iter);
371 empty_list_store_iter_parent (GtkTreeModel *tree_model,
379 empty_list_store_iter_to_row (const GtkTreeIter *iter)
381 g_return_val_if_fail (iter->stamp == TREE_MODEL_STAMP, 0);
382 return GPOINTER_TO_INT (iter->user_data);