9 #include "ygtktreemodel.h"
11 #define YGTK_TYPE_WRAP_MODEL (ygtk_wrap_model_get_type ())
12 #define YGTK_WRAP_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
13 YGTK_TYPE_WRAP_MODEL, YGtkWrapModel))
14 #define YGTK_WRAP_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), \
15 YGTK_TYPE_WRAP_MODEL, YGtkWrapModelClass))
16 #define YGTK_IS_WRAP_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), YGTK_TYPE_WRAP_MODEL))
17 #define YGTK_IS_WRAP_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), YGTK_TYPE_WRAP_MODEL))
18 #define YGTK_WRAP_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
19 YGTK_TYPE_WRAP_MODEL, YGtkWrapModelClass))
31 GObjectClass parent_class;
35 static void ygtk_wrap_model_entry_changed (
YGtkWrapModel *model,
int row);
36 static void ygtk_wrap_model_entry_inserted (
YGtkWrapModel *model,
int row);
37 static void ygtk_wrap_model_entry_deleted (
YGtkWrapModel *model,
int row);
42 virtual void rowChanged (
int row)
43 { ygtk_wrap_model_entry_changed (model, row); }
44 virtual void rowInserted (
int row)
45 { ygtk_wrap_model_entry_inserted (model, row); }
46 virtual void rowDeleted (
int row)
47 { ygtk_wrap_model_entry_deleted (model, row); }
50 static void ygtk_wrap_model_tree_model_init (GtkTreeModelIface *iface);
52 G_DEFINE_TYPE_WITH_CODE (
YGtkWrapModel, ygtk_wrap_model, G_TYPE_OBJECT,
53 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL, ygtk_wrap_model_tree_model_init))
58 static void ygtk_wrap_model_finalize (GObject *
object)
63 delete ymodel->notify;
64 ymodel->notify = NULL;
65 G_OBJECT_CLASS (ygtk_wrap_model_parent_class)->finalize (
object);
68 static GtkTreeModelFlags ygtk_wrap_model_get_flags (GtkTreeModel *model)
69 {
return (GtkTreeModelFlags) (GTK_TREE_MODEL_ITERS_PERSIST|GTK_TREE_MODEL_LIST_ONLY); }
71 static gboolean ygtk_wrap_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
75 gint index = gtk_tree_path_get_indices (path)[0];
76 iter->user_data = GINT_TO_POINTER (index);
77 int rowsNb = ymodel->model->rowsNb();
78 if (!rowsNb && index == 0 && ymodel->model->showEmptyEntry())
80 return index < rowsNb;
83 static GtkTreePath *ygtk_wrap_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
85 int index = GPOINTER_TO_INT (iter->user_data);
86 GtkTreePath *path = gtk_tree_path_new();
87 gtk_tree_path_append_index (path, index);
91 static gboolean ygtk_wrap_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
94 int index = GPOINTER_TO_INT (iter->user_data) + 1;
95 iter->user_data = GINT_TO_POINTER (index);
96 int rowsNb = ymodel->model->rowsNb();
97 return index < rowsNb;
100 static gboolean ygtk_wrap_model_iter_parent (GtkTreeModel *, GtkTreeIter *, GtkTreeIter *)
103 static gboolean ygtk_wrap_model_iter_has_child (GtkTreeModel *, GtkTreeIter *)
106 static gint ygtk_wrap_model_iter_n_children (GtkTreeModel *model, GtkTreeIter *iter)
109 static gboolean ygtk_wrap_model_iter_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
110 GtkTreeIter *parent, gint index)
112 if (parent)
return FALSE;
114 iter->user_data = GINT_TO_POINTER (index);
115 int rowsNb = ymodel->model->rowsNb();
116 if (!rowsNb && index == 0 && ymodel->model->showEmptyEntry())
118 return index < rowsNb;
121 static gboolean ygtk_wrap_model_iter_children (
122 GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *parent)
123 {
return ygtk_wrap_model_iter_nth_child (model, iter, parent, 0); }
125 void ygtk_wrap_model_entry_changed (
YGtkWrapModel *model,
int row)
128 iter.user_data = GINT_TO_POINTER (row);
129 GtkTreePath *path = ygtk_wrap_model_get_path (GTK_TREE_MODEL (model), &iter);
130 gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
131 gtk_tree_path_free (path);
134 void ygtk_wrap_model_entry_inserted (
YGtkWrapModel *ymodel,
int row)
136 GtkTreeModel *model = GTK_TREE_MODEL (ymodel);
138 iter.user_data = GINT_TO_POINTER (row);
139 GtkTreePath *path = ygtk_wrap_model_get_path (model, &iter);
141 if (row == 0 && ymodel->model->rowsNb() == 1 && ymodel->model->showEmptyEntry())
142 gtk_tree_model_row_changed (model, path, &iter);
144 gtk_tree_model_row_inserted (model, path, &iter);
145 gtk_tree_path_free (path);
148 void ygtk_wrap_model_entry_deleted (
YGtkWrapModel *ymodel,
int row)
150 GtkTreeModel *model = GTK_TREE_MODEL (ymodel);
152 iter.user_data = GINT_TO_POINTER (row);
153 GtkTreePath *path = ygtk_wrap_model_get_path (model, &iter);
155 if (row == 0 && ymodel->model->rowsNb() == 1 && ymodel->model->showEmptyEntry())
156 gtk_tree_model_row_changed (model, path, &iter);
158 gtk_tree_model_row_deleted (model, path);
159 gtk_tree_path_free (path);
162 static gint ygtk_wrap_model_get_n_columns (GtkTreeModel *model)
165 return ymodel->model->columnsNb();
168 static GType ygtk_wrap_model_get_column_type (GtkTreeModel *model, gint column)
171 return ymodel->model->columnType (column);
174 static void ygtk_wrap_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
175 gint column, GValue *value)
177 int row = GPOINTER_TO_INT (iter->user_data);
179 g_value_init (value, ymodel->model->columnType (column));
180 if (row == 0 && ymodel->model->rowsNb() == 0)
182 ymodel->model->getValue (row, column, value);
188 ymodel->model = model;
190 model->listener = ymodel->notify;
191 return GTK_TREE_MODEL (ymodel);
194 YGtkTreeModel *ygtk_tree_model_get_model (GtkTreeModel *model)
197 return ymodel->model;
202 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
203 gobject_class->finalize = ygtk_wrap_model_finalize;
206 static void ygtk_wrap_model_tree_model_init (GtkTreeModelIface *iface)
208 iface->get_flags = ygtk_wrap_model_get_flags;
209 iface->get_n_columns = ygtk_wrap_model_get_n_columns;
210 iface->get_column_type = ygtk_wrap_model_get_column_type;
211 iface->get_iter = ygtk_wrap_model_get_iter;
212 iface->get_path = ygtk_wrap_model_get_path;
213 iface->get_value = ygtk_wrap_model_get_value;
214 iface->iter_next = ygtk_wrap_model_iter_next;
215 iface->iter_children = ygtk_wrap_model_iter_children;
216 iface->iter_has_child = ygtk_wrap_model_iter_has_child;
217 iface->iter_n_children = ygtk_wrap_model_iter_n_children;
218 iface->iter_nth_child = ygtk_wrap_model_iter_nth_child;
219 iface->iter_parent = ygtk_wrap_model_iter_parent;