]>
Commit | Line | Data |
---|---|---|
790b137e | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/gtk/dataview.cpp |
790b137e RR |
3 | // Purpose: wxDataViewCtrl GTK+2 implementation |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
790b137e RR |
13 | #if wxUSE_DATAVIEWCTRL |
14 | ||
15 | #include "wx/dataview.h" | |
4ed7af08 RR |
16 | |
17 | #ifndef wxUSE_GENERICDATAVIEWCTRL | |
18 | ||
e4db172a WS |
19 | #ifndef WX_PRECOMP |
20 | #include "wx/log.h" | |
ed4b0fdc | 21 | #include "wx/dcclient.h" |
ed2fbeb8 | 22 | #include "wx/sizer.h" |
e4db172a WS |
23 | #endif |
24 | ||
790b137e | 25 | #include "wx/stockitem.h" |
7ea3a0de RR |
26 | #include "wx/calctrl.h" |
27 | #include "wx/popupwin.h" | |
2586d4a1 RR |
28 | #include "wx/icon.h" |
29 | ||
790b137e RR |
30 | |
31 | #include "wx/gtk/private.h" | |
32 | #include "wx/gtk/win_gtk.h" | |
33 | ||
34 | #include <gobject/gvaluecollector.h> | |
1557c77b RR |
35 | #include <gtk/gtktreemodel.h> |
36 | #include <gtk/gtktreednd.h> | |
790b137e | 37 | |
4d496ecb RR |
38 | #include <gdk/gdkkeysyms.h> |
39 | ||
790b137e RR |
40 | //----------------------------------------------------------------------------- |
41 | // classes | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | class wxDataViewCtrl; | |
45 | ||
790b137e RR |
46 | //----------------------------------------------------------------------------- |
47 | // data | |
48 | //----------------------------------------------------------------------------- | |
49 | ||
50 | extern bool g_blockEventsOnDrag; | |
51 | ||
52 | //----------------------------------------------------------------------------- | |
53 | // define new GTK+ class wxGtkListStore | |
54 | //----------------------------------------------------------------------------- | |
55 | ||
56 | extern "C" { | |
57 | ||
58 | #define GTK_TYPE_WX_LIST_STORE (gtk_wx_list_store_get_type ()) | |
59 | #define GTK_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStore)) | |
60 | #define GTK_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass)) | |
61 | #define GTK_IS_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_LIST_STORE)) | |
62 | #define GTK_IS_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_LIST_STORE)) | |
63 | #define GTK_WX_LIST_STORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass)) | |
64 | ||
65 | GType gtk_wx_list_store_get_type (void); | |
66 | ||
67 | typedef struct _GtkWxListStore GtkWxListStore; | |
68 | typedef struct _GtkWxListStoreClass GtkWxListStoreClass; | |
69 | ||
70 | struct _GtkWxListStore | |
71 | { | |
72 | GObject parent; | |
73 | ||
74 | /*< private >*/ | |
75 | gint stamp; | |
76 | wxDataViewListModel *model; | |
77 | }; | |
78 | ||
79 | struct _GtkWxListStoreClass | |
80 | { | |
e152afc3 | 81 | GObjectClass list_parent_class; |
790b137e RR |
82 | }; |
83 | ||
e152afc3 | 84 | static GtkWxListStore *wxgtk_list_store_new (void); |
790b137e | 85 | static void wxgtk_list_store_init (GtkWxListStore *list_store); |
1557c77b | 86 | static void wxgtk_list_store_class_init (GtkWxListStoreClass *klass); |
790b137e RR |
87 | static void wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface); |
88 | static void wxgtk_list_store_finalize (GObject *object); | |
89 | static GtkTreeModelFlags wxgtk_list_store_get_flags (GtkTreeModel *tree_model); | |
90 | static gint wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model); | |
91 | static GType wxgtk_list_store_get_column_type (GtkTreeModel *tree_model, | |
93763ad5 | 92 | gint index); |
790b137e | 93 | static gboolean wxgtk_list_store_get_iter (GtkTreeModel *tree_model, |
93763ad5 WS |
94 | GtkTreeIter *iter, |
95 | GtkTreePath *path); | |
790b137e | 96 | static GtkTreePath *wxgtk_list_store_get_path (GtkTreeModel *tree_model, |
93763ad5 | 97 | GtkTreeIter *iter); |
790b137e | 98 | static void wxgtk_list_store_get_value (GtkTreeModel *tree_model, |
93763ad5 WS |
99 | GtkTreeIter *iter, |
100 | gint column, | |
101 | GValue *value); | |
790b137e | 102 | static gboolean wxgtk_list_store_iter_next (GtkTreeModel *tree_model, |
93763ad5 | 103 | GtkTreeIter *iter); |
790b137e | 104 | static gboolean wxgtk_list_store_iter_children (GtkTreeModel *tree_model, |
93763ad5 WS |
105 | GtkTreeIter *iter, |
106 | GtkTreeIter *parent); | |
790b137e | 107 | static gboolean wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model, |
93763ad5 | 108 | GtkTreeIter *iter); |
790b137e | 109 | static gint wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model, |
93763ad5 | 110 | GtkTreeIter *iter); |
790b137e | 111 | static gboolean wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model, |
93763ad5 WS |
112 | GtkTreeIter *iter, |
113 | GtkTreeIter *parent, | |
114 | gint n); | |
790b137e | 115 | static gboolean wxgtk_list_store_iter_parent (GtkTreeModel *tree_model, |
93763ad5 WS |
116 | GtkTreeIter *iter, |
117 | GtkTreeIter *child); | |
790b137e | 118 | |
e152afc3 | 119 | static GObjectClass *list_parent_class = NULL; |
790b137e RR |
120 | |
121 | GType | |
111f83c4 | 122 | gtk_wx_list_store_get_type (void) |
790b137e | 123 | { |
93763ad5 | 124 | static GType list_store_type = 0; |
790b137e | 125 | |
93763ad5 | 126 | if (!list_store_type) |
790b137e | 127 | { |
de4a74e2 | 128 | const GTypeInfo list_store_info = |
93763ad5 WS |
129 | { |
130 | sizeof (GtkWxListStoreClass), | |
131 | NULL, /* base_init */ | |
132 | NULL, /* base_finalize */ | |
133 | (GClassInitFunc) wxgtk_list_store_class_init, | |
134 | NULL, /* class_finalize */ | |
135 | NULL, /* class_data */ | |
136 | sizeof (GtkWxListStore), | |
137 | 0, | |
138 | (GInstanceInitFunc) wxgtk_list_store_init, | |
139 | }; | |
790b137e RR |
140 | |
141 | static const GInterfaceInfo tree_model_info = | |
142 | { | |
93763ad5 WS |
143 | (GInterfaceInitFunc) wxgtk_list_store_tree_model_init, |
144 | NULL, | |
145 | NULL | |
790b137e RR |
146 | }; |
147 | ||
148 | list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxListStore", | |
93763ad5 | 149 | &list_store_info, (GTypeFlags)0 ); |
790b137e RR |
150 | |
151 | g_type_add_interface_static (list_store_type, | |
93763ad5 WS |
152 | GTK_TYPE_TREE_MODEL, |
153 | &tree_model_info); | |
790b137e RR |
154 | } |
155 | ||
156 | return list_store_type; | |
157 | } | |
158 | ||
1557c77b | 159 | static GtkWxListStore * |
e152afc3 | 160 | wxgtk_list_store_new(void) |
1557c77b | 161 | { |
e152afc3 RR |
162 | GtkWxListStore *retval = (GtkWxListStore *) g_object_new (GTK_TYPE_WX_LIST_STORE, NULL); |
163 | return retval; | |
1557c77b RR |
164 | } |
165 | ||
790b137e | 166 | static void |
1557c77b | 167 | wxgtk_list_store_class_init (GtkWxListStoreClass *klass) |
790b137e | 168 | { |
e152afc3 RR |
169 | list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass); |
170 | GObjectClass *object_class = (GObjectClass*) klass; | |
790b137e RR |
171 | object_class->finalize = wxgtk_list_store_finalize; |
172 | } | |
173 | ||
174 | static void | |
175 | wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface) | |
176 | { | |
177 | iface->get_flags = wxgtk_list_store_get_flags; | |
178 | iface->get_n_columns = wxgtk_list_store_get_n_columns; | |
179 | iface->get_column_type = wxgtk_list_store_get_column_type; | |
180 | iface->get_iter = wxgtk_list_store_get_iter; | |
181 | iface->get_path = wxgtk_list_store_get_path; | |
182 | iface->get_value = wxgtk_list_store_get_value; | |
183 | iface->iter_next = wxgtk_list_store_iter_next; | |
184 | iface->iter_children = wxgtk_list_store_iter_children; | |
185 | iface->iter_has_child = wxgtk_list_store_iter_has_child; | |
186 | iface->iter_n_children = wxgtk_list_store_iter_n_children; | |
187 | iface->iter_nth_child = wxgtk_list_store_iter_nth_child; | |
188 | iface->iter_parent = wxgtk_list_store_iter_parent; | |
189 | } | |
190 | ||
191 | static void | |
192 | wxgtk_list_store_init (GtkWxListStore *list_store) | |
193 | { | |
194 | list_store->model = NULL; | |
1557c77b | 195 | list_store->stamp = g_random_int(); |
790b137e RR |
196 | } |
197 | ||
198 | static void | |
199 | wxgtk_list_store_finalize (GObject *object) | |
200 | { | |
1557c77b | 201 | /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */ |
790b137e RR |
202 | |
203 | /* we need to sort out, which class deletes what */ | |
111f83c4 | 204 | /* delete list_store->model; */ |
790b137e RR |
205 | |
206 | /* must chain up */ | |
e152afc3 | 207 | (* list_parent_class->finalize) (object); |
790b137e | 208 | } |
93763ad5 | 209 | |
790b137e RR |
210 | } // extern "C" |
211 | ||
212 | //----------------------------------------------------------------------------- | |
213 | // implement callbacks from wxGtkListStore class by letting | |
214 | // them call the methods of wxWidgets' wxDataViewListModel | |
215 | //----------------------------------------------------------------------------- | |
216 | ||
217 | static GtkTreeModelFlags | |
218 | wxgtk_list_store_get_flags (GtkTreeModel *tree_model) | |
219 | { | |
1557c77b | 220 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), (GtkTreeModelFlags)0 ); |
790b137e | 221 | |
67cbdfc1 RR |
222 | // GTK+ list store uses a linked list for storing the |
223 | // items and a pointer to a child is used as the member | |
224 | // field of a GtkTreeIter. This means that the iter is | |
225 | // valid in the GtkListStore as long as the child exists. | |
226 | // We use the index of the row and since the index of a | |
227 | // specific row will change if a row above is deleted, | |
228 | // the iter does not persist | |
229 | return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY; | |
790b137e RR |
230 | } |
231 | ||
232 | static gint | |
233 | wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model) | |
234 | { | |
235 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; | |
236 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), 0); | |
237 | ||
9861f022 | 238 | return list_store->model->GetColumnCount(); |
790b137e RR |
239 | } |
240 | ||
241 | static GType | |
242 | wxgtk_list_store_get_column_type (GtkTreeModel *tree_model, | |
93763ad5 | 243 | gint index) |
790b137e RR |
244 | { |
245 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; | |
246 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), G_TYPE_INVALID); | |
247 | ||
1557c77b | 248 | GType gtype = G_TYPE_INVALID; |
93763ad5 | 249 | |
9861f022 | 250 | wxString wxtype = list_store->model->GetColumnType( (unsigned int) index ); |
93763ad5 | 251 | |
1557c77b RR |
252 | if (wxtype == wxT("string")) |
253 | gtype = G_TYPE_STRING; | |
72a3ac9b VZ |
254 | else |
255 | { | |
256 | wxFAIL_MSG( _T("non-string columns not supported yet") ); | |
257 | } | |
790b137e RR |
258 | |
259 | return gtype; | |
260 | } | |
261 | ||
262 | static gboolean | |
263 | wxgtk_list_store_get_iter (GtkTreeModel *tree_model, | |
93763ad5 WS |
264 | GtkTreeIter *iter, |
265 | GtkTreePath *path) | |
790b137e RR |
266 | { |
267 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; | |
268 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); | |
269 | g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE); | |
270 | ||
0a71f9e9 | 271 | unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0]; |
790b137e | 272 | |
9861f022 | 273 | if (i >= list_store->model->GetRowCount()) |
239eaa41 | 274 | return FALSE; |
790b137e | 275 | |
239eaa41 RR |
276 | iter->stamp = list_store->stamp; |
277 | // user_data is just the index | |
278 | iter->user_data = (gpointer) i; | |
790b137e RR |
279 | |
280 | return TRUE; | |
281 | } | |
282 | ||
283 | static GtkTreePath * | |
284 | wxgtk_list_store_get_path (GtkTreeModel *tree_model, | |
93763ad5 | 285 | GtkTreeIter *iter) |
790b137e | 286 | { |
790b137e | 287 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), NULL); |
239eaa41 | 288 | g_return_val_if_fail (iter->stamp == GTK_WX_LIST_STORE (tree_model)->stamp, NULL); |
93763ad5 | 289 | |
239eaa41 RR |
290 | GtkTreePath *retval = gtk_tree_path_new (); |
291 | // user_data is just the index | |
7a9f5d5a | 292 | int i = (wxUIntPtr) iter->user_data; |
239eaa41 RR |
293 | gtk_tree_path_append_index (retval, i); |
294 | return retval; | |
790b137e RR |
295 | } |
296 | ||
297 | static void | |
298 | wxgtk_list_store_get_value (GtkTreeModel *tree_model, | |
93763ad5 WS |
299 | GtkTreeIter *iter, |
300 | gint column, | |
301 | GValue *value) | |
790b137e | 302 | { |
1557c77b | 303 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
790b137e | 304 | g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model) ); |
239eaa41 | 305 | |
1557c77b | 306 | wxDataViewListModel *model = list_store->model; |
9861f022 | 307 | wxString mtype = model->GetColumnType( (unsigned int) column ); |
1557c77b RR |
308 | if (mtype == wxT("string")) |
309 | { | |
3f3af7e7 | 310 | wxVariant variant; |
1557c77b | 311 | g_value_init( value, G_TYPE_STRING ); |
72a3ac9b VZ |
312 | model->GetValue( variant, |
313 | (unsigned int) column, | |
314 | (unsigned int) iter->user_data ); | |
315 | ||
316 | // FIXME: we should support different encodings here | |
317 | g_value_set_string( value, wxGTK_CONV_SYS(variant.GetString()) ); | |
1557c77b RR |
318 | } |
319 | else | |
320 | { | |
72a3ac9b | 321 | wxFAIL_MSG( _T("non-string columns not supported yet") ); |
1557c77b | 322 | } |
790b137e RR |
323 | } |
324 | ||
325 | static gboolean | |
326 | wxgtk_list_store_iter_next (GtkTreeModel *tree_model, | |
93763ad5 | 327 | GtkTreeIter *iter) |
790b137e | 328 | { |
790b137e | 329 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); |
1557c77b | 330 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
93763ad5 | 331 | |
67cbdfc1 | 332 | g_return_val_if_fail (list_store->stamp == iter->stamp, FALSE); |
790b137e | 333 | |
7a9f5d5a | 334 | int n = (wxUIntPtr) iter->user_data; |
93763ad5 | 335 | |
67cbdfc1 RR |
336 | if (n == -1) |
337 | return FALSE; | |
93763ad5 | 338 | |
9861f022 | 339 | if (n >= (int) list_store->model->GetRowCount()-1) |
67cbdfc1 | 340 | return FALSE; |
93763ad5 | 341 | |
111f83c4 | 342 | iter->user_data = (gpointer) ++n; |
790b137e | 343 | |
67cbdfc1 | 344 | return TRUE; |
790b137e RR |
345 | } |
346 | ||
347 | static gboolean | |
348 | wxgtk_list_store_iter_children (GtkTreeModel *tree_model, | |
93763ad5 WS |
349 | GtkTreeIter *iter, |
350 | GtkTreeIter *parent) | |
790b137e | 351 | { |
67cbdfc1 | 352 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); |
1557c77b | 353 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
93763ad5 | 354 | |
67cbdfc1 | 355 | // this is a list, nodes have no children |
790b137e RR |
356 | if (parent) |
357 | return FALSE; | |
358 | ||
67cbdfc1 RR |
359 | iter->stamp = list_store->stamp; |
360 | iter->user_data = (gpointer) -1; | |
93763ad5 | 361 | |
67cbdfc1 | 362 | return TRUE; |
790b137e RR |
363 | } |
364 | ||
365 | static gboolean | |
366 | wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model, | |
93763ad5 | 367 | GtkTreeIter *iter) |
790b137e RR |
368 | { |
369 | return FALSE; | |
370 | } | |
371 | ||
372 | static gint | |
373 | wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model, | |
93763ad5 | 374 | GtkTreeIter *iter) |
790b137e RR |
375 | { |
376 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), -1); | |
1557c77b | 377 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
93763ad5 | 378 | |
790b137e | 379 | if (iter == NULL) |
9861f022 | 380 | return (gint) list_store->model->GetRowCount(); |
790b137e | 381 | |
67cbdfc1 | 382 | g_return_val_if_fail (list_store->stamp == iter->stamp, -1); |
93763ad5 | 383 | |
790b137e RR |
384 | return 0; |
385 | } | |
386 | ||
387 | static gboolean | |
388 | wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model, | |
93763ad5 WS |
389 | GtkTreeIter *iter, |
390 | GtkTreeIter *parent, | |
391 | gint n) | |
790b137e | 392 | { |
67cbdfc1 | 393 | g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE); |
1557c77b | 394 | GtkWxListStore *list_store = (GtkWxListStore *) tree_model; |
93763ad5 | 395 | |
67cbdfc1 RR |
396 | if (parent) |
397 | return FALSE; | |
790b137e | 398 | |
67cbdfc1 RR |
399 | if (n < 0) |
400 | return FALSE; | |
93763ad5 | 401 | |
9861f022 | 402 | if (n >= (gint) list_store->model->GetRowCount()) |
67cbdfc1 | 403 | return FALSE; |
790b137e | 404 | |
67cbdfc1 RR |
405 | iter->stamp = list_store->stamp; |
406 | iter->user_data = (gpointer) n; | |
93763ad5 | 407 | |
67cbdfc1 | 408 | return TRUE; |
790b137e RR |
409 | } |
410 | ||
411 | static gboolean | |
412 | wxgtk_list_store_iter_parent (GtkTreeModel *tree_model, | |
93763ad5 WS |
413 | GtkTreeIter *iter, |
414 | GtkTreeIter *child) | |
790b137e RR |
415 | { |
416 | return FALSE; | |
417 | } | |
418 | ||
e152afc3 | 419 | //----------------------------------------------------------------------------- |
baa9ebc4 | 420 | // define new GTK+ class wxGtkRendererRenderer |
e152afc3 RR |
421 | //----------------------------------------------------------------------------- |
422 | ||
423 | extern "C" { | |
424 | ||
425 | #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ()) | |
426 | #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer)) | |
427 | #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass)) | |
428 | #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER)) | |
429 | #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER)) | |
430 | #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass)) | |
431 | ||
432 | GType gtk_wx_cell_renderer_get_type (void); | |
433 | ||
434 | typedef struct _GtkWxCellRenderer GtkWxCellRenderer; | |
435 | typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass; | |
436 | ||
437 | struct _GtkWxCellRenderer | |
438 | { | |
439 | GtkCellRenderer parent; | |
440 | ||
441 | /*< private >*/ | |
baa9ebc4 | 442 | wxDataViewCustomRenderer *cell; |
4d496ecb | 443 | guint32 last_click; |
e152afc3 RR |
444 | }; |
445 | ||
446 | struct _GtkWxCellRendererClass | |
447 | { | |
448 | GtkCellRendererClass cell_parent_class; | |
e152afc3 RR |
449 | }; |
450 | ||
451 | ||
452 | static GtkCellRenderer *gtk_wx_cell_renderer_new (void); | |
553f7d8f RR |
453 | static void gtk_wx_cell_renderer_init ( |
454 | GtkWxCellRenderer *cell ); | |
455 | static void gtk_wx_cell_renderer_class_init( | |
456 | GtkWxCellRendererClass *klass ); | |
457 | static void gtk_wx_cell_renderer_finalize ( | |
458 | GObject *object ); | |
459 | static void gtk_wx_cell_renderer_get_size ( | |
460 | GtkCellRenderer *cell, | |
93763ad5 WS |
461 | GtkWidget *widget, |
462 | GdkRectangle *rectangle, | |
463 | gint *x_offset, | |
464 | gint *y_offset, | |
465 | gint *width, | |
466 | gint *height ); | |
553f7d8f RR |
467 | static void gtk_wx_cell_renderer_render ( |
468 | GtkCellRenderer *cell, | |
93763ad5 WS |
469 | GdkWindow *window, |
470 | GtkWidget *widget, | |
471 | GdkRectangle *background_area, | |
472 | GdkRectangle *cell_area, | |
473 | GdkRectangle *expose_area, | |
474 | GtkCellRendererState flags ); | |
553f7d8f RR |
475 | static gboolean gtk_wx_cell_renderer_activate( |
476 | GtkCellRenderer *cell, | |
477 | GdkEvent *event, | |
478 | GtkWidget *widget, | |
479 | const gchar *path, | |
480 | GdkRectangle *background_area, | |
481 | GdkRectangle *cell_area, | |
482 | GtkCellRendererState flags ); | |
e152afc3 RR |
483 | |
484 | static GObjectClass *cell_parent_class = NULL; | |
485 | ||
486 | } // extern "C" | |
487 | ||
93763ad5 | 488 | GType |
e152afc3 RR |
489 | gtk_wx_cell_renderer_get_type (void) |
490 | { | |
553f7d8f | 491 | static GType cell_wx_type = 0; |
e152afc3 | 492 | |
553f7d8f | 493 | if (!cell_wx_type) |
e152afc3 | 494 | { |
de4a74e2 | 495 | const GTypeInfo cell_wx_info = |
553f7d8f RR |
496 | { |
497 | sizeof (GtkWxCellRendererClass), | |
93763ad5 WS |
498 | NULL, /* base_init */ |
499 | NULL, /* base_finalize */ | |
553f7d8f | 500 | (GClassInitFunc) gtk_wx_cell_renderer_class_init, |
93763ad5 WS |
501 | NULL, /* class_finalize */ |
502 | NULL, /* class_data */ | |
553f7d8f RR |
503 | sizeof (GtkWxCellRenderer), |
504 | 0, /* n_preallocs */ | |
505 | (GInstanceInitFunc) gtk_wx_cell_renderer_init, | |
506 | }; | |
507 | ||
93763ad5 | 508 | cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER, |
553f7d8f | 509 | "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 ); |
e152afc3 RR |
510 | } |
511 | ||
553f7d8f | 512 | return cell_wx_type; |
e152afc3 RR |
513 | } |
514 | ||
515 | static void | |
516 | gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell) | |
517 | { | |
518 | cell->cell = NULL; | |
4d496ecb | 519 | cell->last_click = 0; |
e152afc3 RR |
520 | } |
521 | ||
522 | static void | |
523 | gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass) | |
524 | { | |
525 | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
526 | GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass); | |
527 | ||
528 | cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass); | |
529 | ||
530 | object_class->finalize = gtk_wx_cell_renderer_finalize; | |
531 | ||
532 | cell_class->get_size = gtk_wx_cell_renderer_get_size; | |
533 | cell_class->render = gtk_wx_cell_renderer_render; | |
553f7d8f | 534 | cell_class->activate = gtk_wx_cell_renderer_activate; |
e152afc3 RR |
535 | } |
536 | ||
537 | static void | |
538 | gtk_wx_cell_renderer_finalize (GObject *object) | |
539 | { | |
540 | /* must chain up */ | |
541 | (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object); | |
542 | } | |
543 | ||
544 | GtkCellRenderer* | |
545 | gtk_wx_cell_renderer_new (void) | |
546 | { | |
547 | return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL); | |
548 | } | |
549 | ||
550 | static void | |
551 | gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer, | |
93763ad5 WS |
552 | GtkWidget *widget, |
553 | GdkRectangle *cell_area, | |
554 | gint *x_offset, | |
555 | gint *y_offset, | |
556 | gint *width, | |
557 | gint *height) | |
e152afc3 RR |
558 | { |
559 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
baa9ebc4 | 560 | wxDataViewCustomRenderer *cell = wxrenderer->cell; |
93763ad5 | 561 | |
e152afc3 RR |
562 | wxSize size = cell->GetSize(); |
563 | ||
564 | gint calc_width = (gint) renderer->xpad * 2 + size.x; | |
565 | gint calc_height = (gint) renderer->ypad * 2 + size.y; | |
93763ad5 WS |
566 | |
567 | if (x_offset) | |
e152afc3 | 568 | *x_offset = 0; |
93763ad5 | 569 | if (y_offset) |
e152afc3 RR |
570 | *y_offset = 0; |
571 | ||
572 | if (cell_area && size.x > 0 && size.y > 0) | |
573 | { | |
574 | if (x_offset) | |
93763ad5 | 575 | { |
e152afc3 RR |
576 | *x_offset = (gint)((renderer->xalign * |
577 | (cell_area->width - calc_width - 2 * renderer->xpad))); | |
578 | *x_offset = MAX (*x_offset, 0) + renderer->xpad; | |
93763ad5 | 579 | } |
e152afc3 RR |
580 | if (y_offset) |
581 | { | |
582 | *y_offset = (gint)((renderer->yalign * | |
583 | (cell_area->height - calc_height - 2 * renderer->ypad))); | |
584 | *y_offset = MAX (*y_offset, 0) + renderer->ypad; | |
585 | } | |
586 | } | |
587 | ||
588 | if (width) | |
589 | *width = calc_width; | |
93763ad5 | 590 | |
e152afc3 RR |
591 | if (height) |
592 | *height = calc_height; | |
593 | } | |
594 | ||
595 | static void | |
596 | gtk_wx_cell_renderer_render (GtkCellRenderer *renderer, | |
93763ad5 WS |
597 | GdkWindow *window, |
598 | GtkWidget *widget, | |
599 | GdkRectangle *background_area, | |
600 | GdkRectangle *cell_area, | |
601 | GdkRectangle *expose_area, | |
602 | GtkCellRendererState flags) | |
e152afc3 RR |
603 | |
604 | { | |
605 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
baa9ebc4 | 606 | wxDataViewCustomRenderer *cell = wxrenderer->cell; |
93763ad5 | 607 | |
e152afc3 RR |
608 | GdkRectangle rect; |
609 | gtk_wx_cell_renderer_get_size (renderer, widget, cell_area, | |
93763ad5 WS |
610 | &rect.x, |
611 | &rect.y, | |
612 | &rect.width, | |
613 | &rect.height); | |
e152afc3 RR |
614 | |
615 | rect.x += cell_area->x; | |
616 | rect.y += cell_area->y; | |
617 | rect.width -= renderer->xpad * 2; | |
618 | rect.height -= renderer->ypad * 2; | |
93763ad5 | 619 | |
e152afc3 RR |
620 | GdkRectangle dummy; |
621 | if (gdk_rectangle_intersect (expose_area, &rect, &dummy)) | |
622 | { | |
623 | wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); | |
624 | wxWindowDC* dc = (wxWindowDC*) cell->GetDC(); | |
4d496ecb RR |
625 | if (dc->m_window == NULL) |
626 | { | |
627 | dc->m_window = window; | |
628 | dc->SetUpDC(); | |
629 | } | |
93763ad5 | 630 | |
e152afc3 RR |
631 | int state = 0; |
632 | if (flags & GTK_CELL_RENDERER_SELECTED) | |
633 | state |= wxDATAVIEW_CELL_SELECTED; | |
634 | if (flags & GTK_CELL_RENDERER_PRELIT) | |
635 | state |= wxDATAVIEW_CELL_PRELIT; | |
636 | if (flags & GTK_CELL_RENDERER_INSENSITIVE) | |
637 | state |= wxDATAVIEW_CELL_INSENSITIVE; | |
638 | if (flags & GTK_CELL_RENDERER_INSENSITIVE) | |
639 | state |= wxDATAVIEW_CELL_INSENSITIVE; | |
640 | if (flags & GTK_CELL_RENDERER_FOCUSED) | |
641 | state |= wxDATAVIEW_CELL_FOCUSED; | |
642 | cell->Render( renderrect, dc, state ); | |
93763ad5 | 643 | } |
e152afc3 RR |
644 | } |
645 | ||
93763ad5 | 646 | static gboolean |
553f7d8f RR |
647 | gtk_wx_cell_renderer_activate( |
648 | GtkCellRenderer *renderer, | |
649 | GdkEvent *event, | |
650 | GtkWidget *widget, | |
651 | const gchar *path, | |
652 | GdkRectangle *background_area, | |
653 | GdkRectangle *cell_area, | |
654 | GtkCellRendererState flags ) | |
655 | { | |
656 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
baa9ebc4 | 657 | wxDataViewCustomRenderer *cell = wxrenderer->cell; |
93763ad5 | 658 | |
553f7d8f RR |
659 | GdkRectangle rect; |
660 | gtk_wx_cell_renderer_get_size (renderer, widget, cell_area, | |
93763ad5 WS |
661 | &rect.x, |
662 | &rect.y, | |
663 | &rect.width, | |
664 | &rect.height); | |
553f7d8f RR |
665 | |
666 | rect.x += cell_area->x; | |
667 | rect.y += cell_area->y; | |
668 | rect.width -= renderer->xpad * 2; | |
669 | rect.height -= renderer->ypad * 2; | |
93763ad5 | 670 | |
553f7d8f | 671 | wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); |
93763ad5 | 672 | |
4d496ecb | 673 | wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); |
93763ad5 | 674 | |
4d496ecb | 675 | GtkTreePath *treepath = gtk_tree_path_new_from_string( path ); |
0a71f9e9 | 676 | unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (treepath)[0]; |
4d496ecb | 677 | gtk_tree_path_free( treepath ); |
93763ad5 | 678 | |
0a71f9e9 | 679 | unsigned int model_col = cell->GetOwner()->GetModelColumn(); |
93763ad5 | 680 | |
456e5c21 RR |
681 | if (!event) |
682 | { | |
683 | bool ret = false; | |
684 | ||
685 | // activated by <ENTER> | |
686 | if (cell->Activate( renderrect, model, model_col, model_row )) | |
687 | ret = true; | |
688 | ||
689 | return ret; | |
690 | } | |
691 | else if (event->type == GDK_BUTTON_PRESS) | |
4d496ecb RR |
692 | { |
693 | GdkEventButton *button_event = (GdkEventButton*) event; | |
93763ad5 | 694 | wxPoint pt( ((int) button_event->x) - renderrect.x, |
4d496ecb | 695 | ((int) button_event->y) - renderrect.y ); |
93763ad5 | 696 | |
4d496ecb RR |
697 | bool ret = false; |
698 | if (button_event->button == 1) | |
699 | { | |
700 | if (cell->LeftClick( pt, renderrect, model, model_col, model_row )) | |
701 | ret = true; | |
7ea3a0de | 702 | // TODO: query system double-click time |
4d496ecb RR |
703 | if (button_event->time - wxrenderer->last_click < 400) |
704 | if (cell->Activate( renderrect, model, model_col, model_row )) | |
705 | ret = true; | |
706 | } | |
707 | if (button_event->button == 3) | |
708 | { | |
709 | if (cell->RightClick( pt, renderrect, model, model_col, model_row )) | |
710 | ret = true; | |
711 | } | |
93763ad5 | 712 | |
4d496ecb | 713 | wxrenderer->last_click = button_event->time; |
93763ad5 | 714 | |
4d496ecb RR |
715 | return ret; |
716 | } | |
93763ad5 | 717 | |
4d496ecb | 718 | return false; |
553f7d8f RR |
719 | } |
720 | ||
93763ad5 | 721 | // --------------------------------------------------------- |
6e2e590f | 722 | // wxGtkDataViewListModelNotifier |
93763ad5 | 723 | // --------------------------------------------------------- |
6e2e590f RR |
724 | |
725 | class wxGtkDataViewListModelNotifier: public wxDataViewListModelNotifier | |
726 | { | |
727 | public: | |
728 | wxGtkDataViewListModelNotifier( GtkWxListStore* gtk_store, wxDataViewListModel *wx_model ); | |
93763ad5 | 729 | |
6e2e590f RR |
730 | virtual bool RowAppended(); |
731 | virtual bool RowPrepended(); | |
0a71f9e9 RR |
732 | virtual bool RowInserted( unsigned int before ); |
733 | virtual bool RowDeleted( unsigned int row ); | |
734 | virtual bool RowChanged( unsigned int row ); | |
735 | virtual bool ValueChanged( unsigned int col, unsigned int row ); | |
736 | virtual bool RowsReordered( unsigned int *new_order ); | |
6e2e590f | 737 | virtual bool Cleared(); |
93763ad5 | 738 | |
87f0efe2 | 739 | virtual bool Freed() |
9861f022 RR |
740 | { |
741 | m_wx_model = NULL; | |
742 | m_gtk_store = NULL; | |
743 | return wxDataViewListModelNotifier::Freed(); | |
744 | } | |
87f0efe2 | 745 | |
6e2e590f RR |
746 | GtkWxListStore *m_gtk_store; |
747 | wxDataViewListModel *m_wx_model; | |
748 | }; | |
749 | ||
93763ad5 | 750 | // --------------------------------------------------------- |
6e2e590f | 751 | // wxGtkDataViewListModelNotifier |
93763ad5 | 752 | // --------------------------------------------------------- |
6e2e590f | 753 | |
93763ad5 | 754 | wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier( |
6e2e590f RR |
755 | GtkWxListStore* gtk_store, wxDataViewListModel *wx_model ) |
756 | { | |
757 | m_gtk_store = gtk_store; | |
758 | m_wx_model = wx_model; | |
759 | } | |
93763ad5 | 760 | |
6e2e590f RR |
761 | bool wxGtkDataViewListModelNotifier::RowAppended() |
762 | { | |
9861f022 | 763 | unsigned int pos = m_wx_model->GetRowCount()-1; |
93763ad5 | 764 | |
6e2e590f RR |
765 | GtkTreeIter iter; |
766 | iter.stamp = m_gtk_store->stamp; | |
767 | iter.user_data = (gpointer) pos; | |
93763ad5 | 768 | |
6e2e590f RR |
769 | GtkTreePath *path = gtk_tree_path_new (); |
770 | gtk_tree_path_append_index (path, (gint) pos); | |
771 | gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
772 | gtk_tree_path_free (path); | |
93763ad5 | 773 | |
6e2e590f RR |
774 | return true; |
775 | } | |
776 | ||
777 | bool wxGtkDataViewListModelNotifier::RowPrepended() | |
778 | { | |
605c2c4a RR |
779 | GtkTreeIter iter; |
780 | iter.stamp = m_gtk_store->stamp; | |
781 | iter.user_data = (gpointer) 0; | |
93763ad5 | 782 | |
605c2c4a RR |
783 | GtkTreePath *path = gtk_tree_path_new (); |
784 | gtk_tree_path_append_index (path, (gint) 0); | |
785 | gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
786 | gtk_tree_path_free (path); | |
93763ad5 | 787 | |
605c2c4a | 788 | return true; |
6e2e590f RR |
789 | } |
790 | ||
0a71f9e9 | 791 | bool wxGtkDataViewListModelNotifier::RowInserted( unsigned int before ) |
6e2e590f | 792 | { |
4627af27 RR |
793 | GtkTreeIter iter; |
794 | iter.stamp = m_gtk_store->stamp; | |
795 | iter.user_data = (gpointer) before; | |
796 | ||
797 | GtkTreePath *path = gtk_tree_path_new (); | |
798 | gtk_tree_path_append_index (path, (gint) before); | |
799 | gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
800 | gtk_tree_path_free (path); | |
801 | ||
802 | return true; | |
6e2e590f RR |
803 | } |
804 | ||
0a71f9e9 | 805 | bool wxGtkDataViewListModelNotifier::RowDeleted( unsigned int row ) |
6e2e590f | 806 | { |
4627af27 RR |
807 | GtkTreePath *path = gtk_tree_path_new (); |
808 | gtk_tree_path_append_index (path, (gint) row); | |
809 | gtk_tree_model_row_deleted (GTK_TREE_MODEL (m_gtk_store), path); | |
810 | gtk_tree_path_free (path); | |
811 | ||
812 | return true; | |
6e2e590f RR |
813 | } |
814 | ||
0a71f9e9 | 815 | bool wxGtkDataViewListModelNotifier::RowChanged( unsigned int row ) |
6e2e590f | 816 | { |
a7f61f76 RR |
817 | GtkTreeIter iter; |
818 | iter.stamp = m_gtk_store->stamp; | |
819 | iter.user_data = (gpointer) row; | |
820 | GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store), &iter); | |
821 | gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store), path, &iter); | |
822 | gtk_tree_path_free (path); | |
823 | ||
824 | return true; | |
6e2e590f RR |
825 | } |
826 | ||
0a71f9e9 | 827 | bool wxGtkDataViewListModelNotifier::ValueChanged( unsigned int model_col, unsigned int model_row ) |
6e2e590f | 828 | { |
4eccd3a1 | 829 | // This adds GTK+'s missing MVC logic for ValueChanged |
0a5af992 VZ |
830 | wxObjectList::compatibility_iterator |
831 | node = GetOwner()->m_viewingColumns.GetFirst(); | |
8f850e28 RR |
832 | while (node) |
833 | { | |
834 | wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData(); | |
835 | if (viewing_column->m_modelColumn == model_col) | |
836 | { | |
837 | GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview); | |
838 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(viewing_column->m_viewColumn->GetGtkHandle()); | |
839 | ||
840 | // Get cell area | |
841 | GtkTreePath *path = gtk_tree_path_new(); | |
842 | gtk_tree_path_append_index( path, model_row ); | |
843 | GdkRectangle cell_area; | |
844 | gtk_tree_view_get_cell_area( widget, path, column, &cell_area ); | |
93763ad5 | 845 | gtk_tree_path_free( path ); |
8f850e28 RR |
846 | |
847 | int ydiff = column->button->allocation.height; | |
848 | // Redraw | |
93763ad5 | 849 | gtk_widget_queue_draw_area( GTK_WIDGET(widget), |
8f850e28 RR |
850 | cell_area.x, ydiff + cell_area.y, cell_area.width, cell_area.height ); |
851 | } | |
852 | ||
853 | node = node->GetNext(); | |
854 | } | |
93763ad5 | 855 | |
8f850e28 | 856 | return true; |
6e2e590f RR |
857 | } |
858 | ||
0a71f9e9 | 859 | bool wxGtkDataViewListModelNotifier::RowsReordered( unsigned int *new_order ) |
4eccd3a1 | 860 | { |
0a71f9e9 | 861 | // Assume sizeof(unsigned int)= == sizeof(gint) |
4eccd3a1 RR |
862 | |
863 | GtkTreePath *path = gtk_tree_path_new (); | |
864 | gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store), path, NULL, (gint*)new_order); | |
865 | gtk_tree_path_free (path); | |
866 | ||
867 | // This adds GTK+'s missing MVC logic for RowsReordered | |
0a5af992 VZ |
868 | wxObjectList::compatibility_iterator |
869 | node = GetOwner()->m_viewingColumns.GetFirst(); | |
4eccd3a1 RR |
870 | while (node) |
871 | { | |
872 | wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData(); | |
873 | GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview); | |
874 | // Doesn't work yet... | |
93763ad5 | 875 | gtk_widget_queue_draw( GTK_WIDGET(widget) ); |
4eccd3a1 RR |
876 | |
877 | node = node->GetNext(); | |
878 | } | |
93763ad5 | 879 | |
4eccd3a1 RR |
880 | return true; |
881 | } | |
882 | ||
6e2e590f RR |
883 | bool wxGtkDataViewListModelNotifier::Cleared() |
884 | { | |
885 | return false; | |
886 | } | |
887 | ||
93763ad5 | 888 | // --------------------------------------------------------- |
baa9ebc4 | 889 | // wxDataViewRenderer |
93763ad5 | 890 | // --------------------------------------------------------- |
6842a71a | 891 | |
baa9ebc4 | 892 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase) |
6842a71a | 893 | |
9861f022 RR |
894 | wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode, |
895 | int align ) : | |
896 | wxDataViewRendererBase( varianttype, mode, align ) | |
6842a71a RR |
897 | { |
898 | m_renderer = NULL; | |
9861f022 RR |
899 | |
900 | // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor, | |
901 | // after the m_renderer pointer has been initialized | |
902 | } | |
903 | ||
904 | void wxDataViewRenderer::SetMode( wxDataViewCellMode mode ) | |
905 | { | |
906 | GtkCellRendererMode gtkMode; | |
907 | switch (mode) | |
908 | { | |
909 | case wxDATAVIEW_CELL_INERT: | |
910 | gtkMode = GTK_CELL_RENDERER_MODE_INERT; | |
911 | break; | |
912 | case wxDATAVIEW_CELL_ACTIVATABLE: | |
913 | gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE; | |
914 | break; | |
915 | case wxDATAVIEW_CELL_EDITABLE: | |
916 | gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE; | |
917 | break; | |
918 | } | |
919 | ||
920 | GValue gvalue = { 0, }; | |
921 | g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() ); | |
922 | g_value_set_enum( &gvalue, gtkMode ); | |
923 | g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue ); | |
924 | g_value_unset( &gvalue ); | |
925 | } | |
926 | ||
927 | wxDataViewCellMode wxDataViewRenderer::GetMode() const | |
928 | { | |
929 | wxDataViewCellMode ret; | |
930 | ||
931 | GValue gvalue; | |
932 | g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL); | |
933 | ||
934 | switch (g_value_get_enum(&gvalue)) | |
935 | { | |
936 | case GTK_CELL_RENDERER_MODE_INERT: | |
937 | ret = wxDATAVIEW_CELL_INERT; | |
938 | break; | |
939 | case GTK_CELL_RENDERER_MODE_ACTIVATABLE: | |
940 | ret = wxDATAVIEW_CELL_ACTIVATABLE; | |
941 | break; | |
942 | case GTK_CELL_RENDERER_MODE_EDITABLE: | |
943 | ret = wxDATAVIEW_CELL_EDITABLE; | |
944 | break; | |
945 | } | |
946 | ||
947 | g_value_unset( &gvalue ); | |
948 | ||
949 | return ret; | |
950 | } | |
951 | ||
952 | void wxDataViewRenderer::SetAlignment( int align ) | |
953 | { | |
954 | // horizontal alignment: | |
955 | ||
956 | gfloat xalign = 0.0; | |
957 | if (align & wxALIGN_RIGHT) | |
958 | xalign = 1.0; | |
959 | else if (align & wxALIGN_CENTER_HORIZONTAL) | |
960 | xalign = 0.5; | |
961 | ||
962 | GValue gvalue = { 0, }; | |
963 | g_value_init( &gvalue, G_TYPE_FLOAT ); | |
964 | g_value_set_float( &gvalue, xalign ); | |
965 | g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue ); | |
966 | g_value_unset( &gvalue ); | |
967 | ||
968 | // vertical alignment: | |
969 | ||
970 | gfloat yalign = 0.0; | |
971 | if (align & wxALIGN_BOTTOM) | |
972 | yalign = 1.0; | |
973 | else if (align & wxALIGN_CENTER_VERTICAL) | |
974 | yalign = 0.5; | |
975 | ||
976 | GValue gvalue2 = { 0, }; | |
977 | g_value_init( &gvalue2, G_TYPE_FLOAT ); | |
978 | g_value_set_float( &gvalue2, yalign ); | |
979 | g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 ); | |
980 | g_value_unset( &gvalue2 ); | |
6842a71a RR |
981 | } |
982 | ||
9861f022 RR |
983 | int wxDataViewRenderer::GetAlignment() const |
984 | { | |
985 | int ret = 0; | |
986 | GValue gvalue; | |
987 | ||
988 | // horizontal alignment: | |
989 | ||
990 | g_object_get( G_OBJECT(m_renderer), "xalign", &gvalue, NULL ); | |
991 | float xalign = g_value_get_float( &gvalue ); | |
992 | if (xalign < 0.5) | |
993 | ret |= wxALIGN_LEFT; | |
994 | else if (xalign == 0.5) | |
995 | ret |= wxALIGN_CENTER_HORIZONTAL; | |
996 | else | |
997 | ret |= wxALIGN_RIGHT; | |
998 | g_value_unset( &gvalue ); | |
999 | ||
1000 | ||
1001 | // vertical alignment: | |
1002 | ||
1003 | g_object_get( G_OBJECT(m_renderer), "yalign", &gvalue, NULL ); | |
1004 | float yalign = g_value_get_float( &gvalue ); | |
1005 | if (yalign < 0.5) | |
1006 | ret |= wxALIGN_TOP; | |
1007 | else if (yalign == 0.5) | |
1008 | ret |= wxALIGN_CENTER_VERTICAL; | |
1009 | else | |
1010 | ret |= wxALIGN_BOTTOM; | |
1011 | g_value_unset( &gvalue ); | |
1012 | ||
1013 | return ret; | |
1014 | } | |
1015 | ||
1016 | ||
1017 | ||
93763ad5 | 1018 | // --------------------------------------------------------- |
baa9ebc4 | 1019 | // wxDataViewTextRenderer |
93763ad5 | 1020 | // --------------------------------------------------------- |
6842a71a | 1021 | |
a7f61f76 | 1022 | extern "C" { |
93763ad5 | 1023 | static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer, |
a7f61f76 RR |
1024 | gchar *arg1, gchar *arg2, gpointer user_data ); |
1025 | } | |
1026 | ||
93763ad5 | 1027 | static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer, |
a7f61f76 RR |
1028 | gchar *arg1, gchar *arg2, gpointer user_data ) |
1029 | { | |
baa9ebc4 | 1030 | wxDataViewTextRenderer *cell = (wxDataViewTextRenderer*) user_data; |
93763ad5 | 1031 | |
1a74f561 | 1032 | wxString tmp = wxGTK_CONV_BACK_FONT(arg2, cell->GetOwner()->GetOwner()->GetFont()); |
a7f61f76 RR |
1033 | wxVariant value = tmp; |
1034 | if (!cell->Validate( value )) | |
1035 | return; | |
93763ad5 | 1036 | |
a7f61f76 | 1037 | wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); |
93763ad5 | 1038 | |
a7f61f76 | 1039 | GtkTreePath *path = gtk_tree_path_new_from_string( arg1 ); |
0a71f9e9 | 1040 | unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (path)[0]; |
a7f61f76 | 1041 | gtk_tree_path_free( path ); |
93763ad5 | 1042 | |
0a71f9e9 | 1043 | unsigned int model_col = cell->GetOwner()->GetModelColumn(); |
93763ad5 | 1044 | |
a7f61f76 RR |
1045 | model->SetValue( value, model_col, model_row ); |
1046 | model->ValueChanged( model_col, model_row ); | |
1047 | } | |
1048 | ||
baa9ebc4 | 1049 | IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer) |
6842a71a | 1050 | |
9861f022 RR |
1051 | wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode, |
1052 | int align ) : | |
1053 | wxDataViewRenderer( varianttype, mode, align ) | |
6842a71a | 1054 | { |
9861f022 | 1055 | m_renderer = (GtkWidget*) gtk_cell_renderer_text_new(); |
93763ad5 | 1056 | |
9861f022 | 1057 | if (mode & wxDATAVIEW_CELL_EDITABLE) |
a7f61f76 RR |
1058 | { |
1059 | GValue gvalue = { 0, }; | |
1060 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1061 | g_value_set_boolean( &gvalue, true ); | |
1062 | g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue ); | |
1063 | g_value_unset( &gvalue ); | |
93763ad5 | 1064 | |
a7f61f76 RR |
1065 | g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this ); |
1066 | } | |
9861f022 RR |
1067 | |
1068 | SetMode(mode); | |
1069 | SetAlignment(align); | |
6842a71a | 1070 | } |
790b137e | 1071 | |
baa9ebc4 | 1072 | bool wxDataViewTextRenderer::SetValue( const wxVariant &value ) |
7b4fde82 RR |
1073 | { |
1074 | wxString tmp = value; | |
93763ad5 | 1075 | |
7b4fde82 RR |
1076 | GValue gvalue = { 0, }; |
1077 | g_value_init( &gvalue, G_TYPE_STRING ); | |
b94db696 | 1078 | g_value_set_string( &gvalue, wxGTK_CONV_FONT( tmp, GetOwner()->GetOwner()->GetFont() ) ); |
7b4fde82 RR |
1079 | g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue ); |
1080 | g_value_unset( &gvalue ); | |
93763ad5 | 1081 | |
7b4fde82 RR |
1082 | return true; |
1083 | } | |
1084 | ||
9861f022 | 1085 | bool wxDataViewTextRenderer::GetValue( wxVariant &value ) const |
a7f61f76 RR |
1086 | { |
1087 | GValue gvalue = { 0, }; | |
1088 | g_value_init( &gvalue, G_TYPE_STRING ); | |
1089 | g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
1a74f561 VZ |
1090 | wxString tmp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), |
1091 | wx_const_cast(wxDataViewTextRenderer*, this)->GetOwner()->GetOwner()->GetFont() ); | |
a7f61f76 | 1092 | g_value_unset( &gvalue ); |
93763ad5 | 1093 | |
a7f61f76 RR |
1094 | value = tmp; |
1095 | ||
1096 | return true; | |
1097 | } | |
1098 | ||
9861f022 RR |
1099 | void wxDataViewTextRenderer::SetAlignment( int align ) |
1100 | { | |
1101 | wxDataViewRenderer::SetAlignment(align); | |
1102 | ||
1103 | // horizontal alignment: | |
1104 | ||
1105 | PangoAlignment pangoAlign = PANGO_ALIGN_LEFT; | |
1106 | if (align & wxALIGN_RIGHT) | |
1107 | pangoAlign = PANGO_ALIGN_RIGHT; | |
1108 | else if (align & wxALIGN_CENTER_HORIZONTAL) | |
1109 | pangoAlign = PANGO_ALIGN_CENTER; | |
1110 | ||
1111 | GValue gvalue = { 0, }; | |
1112 | g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() ); | |
1113 | g_value_set_enum( &gvalue, pangoAlign ); | |
1114 | g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue ); | |
1115 | g_value_unset( &gvalue ); | |
1116 | } | |
1117 | ||
f4322df6 | 1118 | // --------------------------------------------------------- |
baa9ebc4 | 1119 | // wxDataViewBitmapRenderer |
f4322df6 | 1120 | // --------------------------------------------------------- |
cbc9145c | 1121 | |
baa9ebc4 | 1122 | IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer) |
cbc9145c | 1123 | |
9861f022 RR |
1124 | wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode, |
1125 | int align ) : | |
1126 | wxDataViewRenderer( varianttype, mode, align ) | |
cbc9145c | 1127 | { |
9861f022 RR |
1128 | m_renderer = (GtkWidget*) gtk_cell_renderer_pixbuf_new(); |
1129 | ||
1130 | SetMode(mode); | |
1131 | SetAlignment(align); | |
cbc9145c RR |
1132 | } |
1133 | ||
baa9ebc4 | 1134 | bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value ) |
cbc9145c RR |
1135 | { |
1136 | if (value.GetType() == wxT("wxBitmap")) | |
1137 | { | |
2586d4a1 RR |
1138 | wxBitmap bitmap; |
1139 | bitmap << value; | |
f4322df6 | 1140 | |
2586d4a1 RR |
1141 | // This may create a Pixbuf representation in the |
1142 | // wxBitmap object (and it will stay there) | |
1143 | GdkPixbuf *pixbuf = bitmap.GetPixbuf(); | |
f4322df6 | 1144 | |
2586d4a1 RR |
1145 | GValue gvalue = { 0, }; |
1146 | g_value_init( &gvalue, G_TYPE_OBJECT ); | |
1147 | g_value_set_object( &gvalue, pixbuf ); | |
1148 | g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue ); | |
1149 | g_value_unset( &gvalue ); | |
f4322df6 | 1150 | |
2586d4a1 RR |
1151 | return true; |
1152 | } | |
f4322df6 | 1153 | |
2586d4a1 RR |
1154 | if (value.GetType() == wxT("wxIcon")) |
1155 | { | |
1156 | wxIcon bitmap; | |
1157 | bitmap << value; | |
f4322df6 | 1158 | |
cbc9145c RR |
1159 | // This may create a Pixbuf representation in the |
1160 | // wxBitmap object (and it will stay there) | |
2586d4a1 | 1161 | GdkPixbuf *pixbuf = bitmap.GetPixbuf(); |
f4322df6 | 1162 | |
cbc9145c RR |
1163 | GValue gvalue = { 0, }; |
1164 | g_value_init( &gvalue, G_TYPE_OBJECT ); | |
1165 | g_value_set_object( &gvalue, pixbuf ); | |
1166 | g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue ); | |
1167 | g_value_unset( &gvalue ); | |
f4322df6 | 1168 | |
cbc9145c RR |
1169 | return true; |
1170 | } | |
f4322df6 | 1171 | |
cbc9145c RR |
1172 | return false; |
1173 | } | |
1174 | ||
9861f022 | 1175 | bool wxDataViewBitmapRenderer::GetValue( wxVariant &value ) const |
cbc9145c RR |
1176 | { |
1177 | return false; | |
1178 | } | |
f4322df6 | 1179 | |
93763ad5 | 1180 | // --------------------------------------------------------- |
baa9ebc4 | 1181 | // wxDataViewToggleRenderer |
93763ad5 | 1182 | // --------------------------------------------------------- |
fa28826d | 1183 | |
605c2c4a | 1184 | extern "C" { |
93763ad5 | 1185 | static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, |
605c2c4a RR |
1186 | gchar *path, gpointer user_data ); |
1187 | } | |
1188 | ||
93763ad5 | 1189 | static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, |
605c2c4a RR |
1190 | gchar *path, gpointer user_data ) |
1191 | { | |
baa9ebc4 | 1192 | wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data; |
605c2c4a | 1193 | |
93763ad5 | 1194 | // get old value |
605c2c4a RR |
1195 | GValue gvalue = { 0, }; |
1196 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1197 | g_object_get_property( G_OBJECT(renderer), "active", &gvalue ); | |
93763ad5 | 1198 | bool tmp = g_value_get_boolean( &gvalue ); |
605c2c4a RR |
1199 | g_value_unset( &gvalue ); |
1200 | // invert it | |
1201 | tmp = !tmp; | |
93763ad5 | 1202 | |
605c2c4a RR |
1203 | wxVariant value = tmp; |
1204 | if (!cell->Validate( value )) | |
1205 | return; | |
93763ad5 | 1206 | |
605c2c4a | 1207 | wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); |
93763ad5 | 1208 | |
605c2c4a | 1209 | GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path ); |
0a71f9e9 | 1210 | unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (gtk_path)[0]; |
605c2c4a | 1211 | gtk_tree_path_free( gtk_path ); |
93763ad5 | 1212 | |
0a71f9e9 | 1213 | unsigned int model_col = cell->GetOwner()->GetModelColumn(); |
93763ad5 | 1214 | |
605c2c4a RR |
1215 | model->SetValue( value, model_col, model_row ); |
1216 | model->ValueChanged( model_col, model_row ); | |
1217 | } | |
1218 | ||
baa9ebc4 | 1219 | IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer) |
605c2c4a | 1220 | |
baa9ebc4 | 1221 | wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype, |
9861f022 RR |
1222 | wxDataViewCellMode mode, int align ) : |
1223 | wxDataViewRenderer( varianttype, mode, align ) | |
605c2c4a | 1224 | { |
9861f022 | 1225 | m_renderer = (GtkWidget*) gtk_cell_renderer_toggle_new(); |
93763ad5 | 1226 | |
9861f022 | 1227 | if (mode & wxDATAVIEW_CELL_ACTIVATABLE) |
605c2c4a | 1228 | { |
9861f022 RR |
1229 | g_signal_connect_after( m_renderer, "toggled", |
1230 | G_CALLBACK(wxGtkToggleRendererToggledCallback), this ); | |
553f7d8f RR |
1231 | } |
1232 | else | |
1233 | { | |
605c2c4a RR |
1234 | GValue gvalue = { 0, }; |
1235 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
553f7d8f | 1236 | g_value_set_boolean( &gvalue, false ); |
605c2c4a RR |
1237 | g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue ); |
1238 | g_value_unset( &gvalue ); | |
605c2c4a | 1239 | } |
9861f022 RR |
1240 | |
1241 | SetMode(mode); | |
1242 | SetAlignment(align); | |
605c2c4a RR |
1243 | } |
1244 | ||
baa9ebc4 | 1245 | bool wxDataViewToggleRenderer::SetValue( const wxVariant &value ) |
605c2c4a RR |
1246 | { |
1247 | bool tmp = value; | |
93763ad5 | 1248 | |
605c2c4a RR |
1249 | GValue gvalue = { 0, }; |
1250 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1251 | g_value_set_boolean( &gvalue, tmp ); | |
1252 | g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue ); | |
1253 | g_value_unset( &gvalue ); | |
93763ad5 | 1254 | |
605c2c4a RR |
1255 | return true; |
1256 | } | |
1257 | ||
9861f022 | 1258 | bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const |
605c2c4a RR |
1259 | { |
1260 | GValue gvalue = { 0, }; | |
1261 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1262 | g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue ); | |
93763ad5 | 1263 | bool tmp = g_value_get_boolean( &gvalue ); |
605c2c4a | 1264 | g_value_unset( &gvalue ); |
93763ad5 | 1265 | |
605c2c4a RR |
1266 | value = tmp; |
1267 | ||
1268 | return true; | |
1269 | } | |
93763ad5 WS |
1270 | |
1271 | // --------------------------------------------------------- | |
baa9ebc4 | 1272 | // wxDataViewCustomRenderer |
93763ad5 | 1273 | // --------------------------------------------------------- |
e152afc3 RR |
1274 | |
1275 | class wxDataViewCtrlDC: public wxWindowDC | |
1276 | { | |
1277 | public: | |
1278 | wxDataViewCtrlDC( wxDataViewCtrl *window ) | |
1279 | { | |
1a367564 | 1280 | GtkWidget *widget = window->m_treeview; |
e152afc3 RR |
1281 | // Set later |
1282 | m_window = NULL; | |
4d496ecb | 1283 | |
e152afc3 RR |
1284 | m_context = window->GtkGetPangoDefaultContext(); |
1285 | m_layout = pango_layout_new( m_context ); | |
1286 | m_fontdesc = pango_font_description_copy( widget->style->font_desc ); | |
1287 | ||
1288 | m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget ); | |
1289 | ||
4d496ecb RR |
1290 | // Set m_window later |
1291 | // SetUpDC(); | |
1292 | // m_owner = window; | |
e152afc3 RR |
1293 | } |
1294 | }; | |
1295 | ||
93763ad5 | 1296 | // --------------------------------------------------------- |
baa9ebc4 | 1297 | // wxDataViewCustomRenderer |
93763ad5 | 1298 | // --------------------------------------------------------- |
e152afc3 | 1299 | |
baa9ebc4 | 1300 | IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer) |
e152afc3 | 1301 | |
baa9ebc4 | 1302 | wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype, |
9861f022 RR |
1303 | wxDataViewCellMode mode, int align, |
1304 | bool no_init ) : | |
1305 | wxDataViewRenderer( varianttype, mode, align ) | |
e152afc3 RR |
1306 | { |
1307 | m_dc = NULL; | |
93763ad5 | 1308 | |
ad63bf41 RR |
1309 | if (no_init) |
1310 | m_renderer = NULL; | |
1311 | else | |
9861f022 | 1312 | Init(mode, align); |
ad63bf41 RR |
1313 | } |
1314 | ||
9861f022 | 1315 | bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align) |
ad63bf41 | 1316 | { |
e152afc3 RR |
1317 | GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new(); |
1318 | renderer->cell = this; | |
93763ad5 | 1319 | |
9861f022 | 1320 | m_renderer = (GtkWidget*) renderer; |
93763ad5 | 1321 | |
9861f022 RR |
1322 | SetMode(mode); |
1323 | SetAlignment(align); | |
93763ad5 | 1324 | |
ad63bf41 | 1325 | return true; |
e152afc3 RR |
1326 | } |
1327 | ||
baa9ebc4 | 1328 | wxDataViewCustomRenderer::~wxDataViewCustomRenderer() |
e152afc3 RR |
1329 | { |
1330 | if (m_dc) | |
1331 | delete m_dc; | |
1332 | } | |
1333 | ||
baa9ebc4 | 1334 | wxDC *wxDataViewCustomRenderer::GetDC() |
e152afc3 RR |
1335 | { |
1336 | if (m_dc == NULL) | |
4d496ecb RR |
1337 | { |
1338 | if (GetOwner() == NULL) | |
1339 | return NULL; | |
1340 | if (GetOwner()->GetOwner() == NULL) | |
1341 | return NULL; | |
e152afc3 | 1342 | m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() ); |
4d496ecb | 1343 | } |
93763ad5 | 1344 | |
e152afc3 RR |
1345 | return m_dc; |
1346 | } | |
93763ad5 WS |
1347 | |
1348 | // --------------------------------------------------------- | |
baa9ebc4 | 1349 | // wxDataViewProgressRenderer |
93763ad5 | 1350 | // --------------------------------------------------------- |
ad63bf41 | 1351 | |
baa9ebc4 | 1352 | IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer) |
ad63bf41 | 1353 | |
baa9ebc4 | 1354 | wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label, |
9861f022 RR |
1355 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : |
1356 | wxDataViewCustomRenderer( varianttype, mode, align, true ) | |
ad63bf41 RR |
1357 | { |
1358 | m_label = label; | |
1359 | m_value = 0; | |
93763ad5 | 1360 | |
ad63bf41 RR |
1361 | #ifdef __WXGTK26__ |
1362 | if (!gtk_check_version(2,6,0)) | |
1363 | { | |
9861f022 | 1364 | m_renderer = (GtkWidget*) gtk_cell_renderer_progress_new(); |
93763ad5 | 1365 | |
ad63bf41 RR |
1366 | GValue gvalue = { 0, }; |
1367 | g_value_init( &gvalue, G_TYPE_STRING ); | |
09dfa6a0 VZ |
1368 | |
1369 | // FIXME: font encoding support | |
b94db696 | 1370 | g_value_set_string( &gvalue, wxGTK_CONV_SYS(m_label) ); |
ad63bf41 RR |
1371 | g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue ); |
1372 | g_value_unset( &gvalue ); | |
9861f022 RR |
1373 | |
1374 | SetMode(mode); | |
1375 | SetAlignment(align); | |
ad63bf41 RR |
1376 | } |
1377 | else | |
1378 | #endif | |
1379 | { | |
1380 | // Use custom cell code | |
9861f022 | 1381 | wxDataViewCustomRenderer::Init(mode, align); |
ad63bf41 RR |
1382 | } |
1383 | } | |
1384 | ||
baa9ebc4 | 1385 | wxDataViewProgressRenderer::~wxDataViewProgressRenderer() |
ad63bf41 RR |
1386 | { |
1387 | } | |
1388 | ||
baa9ebc4 | 1389 | bool wxDataViewProgressRenderer::SetValue( const wxVariant &value ) |
ad63bf41 RR |
1390 | { |
1391 | #ifdef __WXGTK26__ | |
1392 | if (!gtk_check_version(2,6,0)) | |
1393 | { | |
7226118b | 1394 | gint tmp = (long) value; |
ad63bf41 RR |
1395 | GValue gvalue = { 0, }; |
1396 | g_value_init( &gvalue, G_TYPE_INT ); | |
7226118b | 1397 | g_value_set_int( &gvalue, tmp ); |
ad63bf41 RR |
1398 | g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue ); |
1399 | g_value_unset( &gvalue ); | |
1400 | } | |
1401 | else | |
1402 | #endif | |
1403 | { | |
1404 | m_value = (long) value; | |
93763ad5 | 1405 | |
ad63bf41 RR |
1406 | if (m_value < 0) m_value = 0; |
1407 | if (m_value > 100) m_value = 100; | |
1408 | } | |
93763ad5 | 1409 | |
ad63bf41 RR |
1410 | return true; |
1411 | } | |
93763ad5 | 1412 | |
9861f022 RR |
1413 | bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const |
1414 | { | |
1415 | return false; | |
1416 | } | |
1417 | ||
baa9ebc4 | 1418 | bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int state ) |
ad63bf41 RR |
1419 | { |
1420 | double pct = (double)m_value / 100.0; | |
1421 | wxRect bar = cell; | |
1422 | bar.width = (int)(cell.width * pct); | |
1423 | dc->SetPen( *wxTRANSPARENT_PEN ); | |
1424 | dc->SetBrush( *wxBLUE_BRUSH ); | |
1425 | dc->DrawRectangle( bar ); | |
1426 | ||
1427 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); | |
1428 | dc->SetPen( *wxBLACK_PEN ); | |
1429 | dc->DrawRectangle( cell ); | |
93763ad5 | 1430 | |
ad63bf41 RR |
1431 | return true; |
1432 | } | |
1433 | ||
9861f022 | 1434 | wxSize wxDataViewProgressRenderer::GetSize() const |
ad63bf41 RR |
1435 | { |
1436 | return wxSize(40,12); | |
1437 | } | |
93763ad5 WS |
1438 | |
1439 | // --------------------------------------------------------- | |
baa9ebc4 | 1440 | // wxDataViewDateRenderer |
93763ad5 | 1441 | // --------------------------------------------------------- |
4d496ecb | 1442 | |
baa9ebc4 | 1443 | class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow |
7ea3a0de | 1444 | { |
93763ad5 | 1445 | public: |
baa9ebc4 | 1446 | wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value, |
0a71f9e9 | 1447 | wxDataViewListModel *model, unsigned int col, unsigned int row ) : |
7ea3a0de RR |
1448 | wxPopupTransientWindow( parent, wxBORDER_SIMPLE ) |
1449 | { | |
1450 | m_model = model; | |
1451 | m_col = col; | |
1452 | m_row = row; | |
1453 | m_cal = new wxCalendarCtrl( this, -1, *value ); | |
1454 | wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); | |
1455 | sizer->Add( m_cal, 1, wxGROW ); | |
1456 | SetSizer( sizer ); | |
1457 | sizer->Fit( this ); | |
1458 | } | |
93763ad5 | 1459 | |
7ea3a0de RR |
1460 | virtual void OnDismiss() |
1461 | { | |
1462 | } | |
93763ad5 | 1463 | |
7ea3a0de | 1464 | void OnCalendar( wxCalendarEvent &event ); |
93763ad5 | 1465 | |
7ea3a0de | 1466 | wxCalendarCtrl *m_cal; |
93763ad5 | 1467 | wxDataViewListModel *m_model; |
0a71f9e9 RR |
1468 | unsigned int m_col; |
1469 | unsigned int m_row; | |
93763ad5 | 1470 | |
7ea3a0de RR |
1471 | private: |
1472 | DECLARE_EVENT_TABLE() | |
1473 | }; | |
1474 | ||
baa9ebc4 RR |
1475 | BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow) |
1476 | EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar ) | |
7ea3a0de RR |
1477 | END_EVENT_TABLE() |
1478 | ||
baa9ebc4 | 1479 | void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event ) |
7ea3a0de RR |
1480 | { |
1481 | wxDateTime date = event.GetDate(); | |
1482 | wxVariant value = date; | |
1483 | m_model->SetValue( value, m_col, m_row ); | |
1484 | m_model->ValueChanged( m_col, m_row ); | |
1485 | DismissAndNotify(); | |
1486 | } | |
1487 | ||
baa9ebc4 | 1488 | IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer) |
4d496ecb | 1489 | |
baa9ebc4 | 1490 | wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype, |
9861f022 RR |
1491 | wxDataViewCellMode mode, int align ) : |
1492 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
4d496ecb | 1493 | { |
9861f022 RR |
1494 | SetMode(mode); |
1495 | SetAlignment(align); | |
4d496ecb | 1496 | } |
93763ad5 | 1497 | |
baa9ebc4 | 1498 | bool wxDataViewDateRenderer::SetValue( const wxVariant &value ) |
4d496ecb RR |
1499 | { |
1500 | m_date = value.GetDateTime(); | |
93763ad5 | 1501 | |
4d496ecb RR |
1502 | return true; |
1503 | } | |
1504 | ||
9861f022 RR |
1505 | bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const |
1506 | { | |
1507 | return false; | |
1508 | } | |
1509 | ||
baa9ebc4 | 1510 | bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state ) |
4d496ecb RR |
1511 | { |
1512 | dc->SetFont( GetOwner()->GetOwner()->GetFont() ); | |
1513 | wxString tmp = m_date.FormatDate(); | |
1514 | dc->DrawText( tmp, cell.x, cell.y ); | |
1515 | ||
1516 | return true; | |
1517 | } | |
1518 | ||
9861f022 | 1519 | wxSize wxDataViewDateRenderer::GetSize() const |
4d496ecb | 1520 | { |
4d496ecb RR |
1521 | wxString tmp = m_date.FormatDate(); |
1522 | wxCoord x,y,d; | |
9861f022 | 1523 | GetView()->GetTextExtent( tmp, &x, &y, &d ); |
4d496ecb RR |
1524 | return wxSize(x,y+d); |
1525 | } | |
1526 | ||
9861f022 RR |
1527 | bool wxDataViewDateRenderer::Activate( wxRect cell, wxDataViewListModel *model, |
1528 | unsigned int col, unsigned int row ) | |
4d496ecb | 1529 | { |
3f3af7e7 RR |
1530 | wxVariant variant; |
1531 | model->GetValue( variant, col, row ); | |
7ea3a0de RR |
1532 | wxDateTime value = variant.GetDateTime(); |
1533 | ||
baa9ebc4 | 1534 | wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient( |
7ea3a0de RR |
1535 | GetOwner()->GetOwner()->GetParent(), &value, model, col, row ); |
1536 | wxPoint pos = wxGetMousePosition(); | |
1537 | popup->Move( pos ); | |
1538 | popup->Layout(); | |
1539 | popup->Popup( popup->m_cal ); | |
4d496ecb RR |
1540 | |
1541 | return true; | |
1542 | } | |
1543 | ||
93763ad5 | 1544 | // --------------------------------------------------------- |
605c2c4a | 1545 | // wxDataViewColumn |
93763ad5 | 1546 | // --------------------------------------------------------- |
7b4fde82 | 1547 | |
31fb32e1 RR |
1548 | |
1549 | static gboolean | |
1550 | gtk_dataview_header_button_press_callback( GtkWidget *widget, | |
1551 | GdkEventButton *gdk_event, | |
1552 | wxDataViewColumn *column ) | |
1553 | { | |
1554 | if (gdk_event->type != GDK_BUTTON_PRESS) | |
1555 | return TRUE; | |
f4322df6 | 1556 | |
31fb32e1 RR |
1557 | if (gdk_event->button == 1) |
1558 | { | |
1559 | wxDataViewCtrl *dv = column->GetOwner(); | |
1560 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() ); | |
1561 | event.SetDataViewColumn( column ); | |
1562 | event.SetModel( dv->GetModel() ); | |
1563 | dv->GetEventHandler()->ProcessEvent( event ); | |
1564 | } | |
f4322df6 | 1565 | |
31fb32e1 RR |
1566 | return TRUE; |
1567 | } | |
1568 | ||
7b4fde82 RR |
1569 | extern "C" { |
1570 | static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, | |
1571 | GtkCellRenderer *cell, | |
1572 | GtkTreeModel *model, | |
1573 | GtkTreeIter *iter, | |
1574 | gpointer data ); | |
1575 | } | |
1576 | ||
1577 | ||
1578 | static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, | |
1579 | GtkCellRenderer *renderer, | |
1580 | GtkTreeModel *model, | |
1581 | GtkTreeIter *iter, | |
1582 | gpointer data ) | |
1583 | { | |
1584 | g_return_if_fail (GTK_IS_WX_LIST_STORE (model)); | |
1585 | GtkWxListStore *list_store = (GtkWxListStore *) model; | |
93763ad5 | 1586 | |
baa9ebc4 | 1587 | wxDataViewRenderer *cell = (wxDataViewRenderer*) data; |
a7f61f76 | 1588 | |
0a71f9e9 | 1589 | unsigned int model_row = (unsigned int) iter->user_data; |
93763ad5 | 1590 | |
3f3af7e7 RR |
1591 | wxVariant value; |
1592 | list_store->model->GetValue( value, cell->GetOwner()->GetModelColumn(), model_row ); | |
7b4fde82 RR |
1593 | |
1594 | if (value.GetType() != cell->GetVariantType()) | |
f4322df6 VZ |
1595 | wxLogError( wxT("Wrong type, required: %s but: %s"), |
1596 | value.GetType().c_str(), | |
cbc9145c | 1597 | cell->GetVariantType().c_str() ); |
93763ad5 | 1598 | |
7b4fde82 RR |
1599 | cell->SetValue( value ); |
1600 | } | |
1601 | ||
cbc9145c | 1602 | IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase) |
fa28826d | 1603 | |
f4322df6 VZ |
1604 | wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell, |
1605 | unsigned int model_column, int width, | |
87f0efe2 RR |
1606 | wxAlignment align, int flags ) : |
1607 | wxDataViewColumnBase( title, cell, model_column, width, align, flags ) | |
fa28826d | 1608 | { |
9861f022 | 1609 | Init( align, flags, width ); |
31fb32e1 | 1610 | |
9861f022 | 1611 | gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column), TRUE ); |
31fb32e1 | 1612 | SetTitle( title ); |
fa28826d RR |
1613 | } |
1614 | ||
f4322df6 VZ |
1615 | wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell, |
1616 | unsigned int model_column, int width, | |
87f0efe2 RR |
1617 | wxAlignment align, int flags ) : |
1618 | wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags ) | |
9861f022 RR |
1619 | { |
1620 | Init( align, flags, width ); | |
1621 | ||
1622 | SetBitmap( bitmap ); | |
1623 | } | |
1624 | ||
1625 | void wxDataViewColumn::Init(wxAlignment align, int flags, int width) | |
07a84e7b | 1626 | { |
31fb32e1 | 1627 | m_isConnected = false; |
07a84e7b | 1628 | |
9861f022 | 1629 | GtkCellRenderer *renderer = (GtkCellRenderer *) GetRenderer()->GetGtkHandle(); |
07a84e7b | 1630 | GtkTreeViewColumn *column = gtk_tree_view_column_new(); |
9861f022 | 1631 | m_column = (GtkWidget*) column; |
07a84e7b | 1632 | |
9861f022 RR |
1633 | SetFlags( flags ); |
1634 | SetAlignment( align ); | |
07a84e7b | 1635 | |
9861f022 RR |
1636 | // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH); |
1637 | // as GTK+ is smart and unless explicitely told, will set the minimal | |
1638 | // width to the title's lenght, which is a better default | |
07a84e7b | 1639 | |
9861f022 RR |
1640 | // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode |
1641 | // that we use for the wxDataViewCtrl | |
1642 | gtk_tree_view_column_set_fixed_width( column, width < 0 ? wxDVC_DEFAULT_WIDTH : width ); | |
b94db696 | 1643 | gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED ); |
07a84e7b | 1644 | |
9861f022 | 1645 | gtk_tree_view_column_pack_end( column, renderer, TRUE ); |
07a84e7b RR |
1646 | |
1647 | gtk_tree_view_column_set_cell_data_func( column, renderer, | |
9861f022 | 1648 | wxGtkTreeCellDataFunc, (gpointer) GetRenderer(), NULL ); |
07a84e7b RR |
1649 | } |
1650 | ||
fa28826d RR |
1651 | wxDataViewColumn::~wxDataViewColumn() |
1652 | { | |
1653 | } | |
1654 | ||
31fb32e1 RR |
1655 | void wxDataViewColumn::OnInternalIdle() |
1656 | { | |
1657 | if (m_isConnected) | |
1658 | return; | |
f4322df6 | 1659 | |
31fb32e1 RR |
1660 | if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview)) |
1661 | { | |
9861f022 | 1662 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
31fb32e1 RR |
1663 | if (column->button) |
1664 | { | |
1665 | g_signal_connect(column->button, "button_press_event", | |
1666 | G_CALLBACK (gtk_dataview_header_button_press_callback), this); | |
f4322df6 | 1667 | |
31fb32e1 RR |
1668 | m_isConnected = true; |
1669 | } | |
1670 | } | |
1671 | } | |
1672 | ||
b94db696 RR |
1673 | void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner ) |
1674 | { | |
1675 | wxDataViewColumnBase::SetOwner( owner ); | |
f4322df6 | 1676 | |
9861f022 | 1677 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1678 | |
b94db696 | 1679 | gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) ); |
b94db696 RR |
1680 | } |
1681 | ||
fa28826d RR |
1682 | void wxDataViewColumn::SetTitle( const wxString &title ) |
1683 | { | |
9861f022 | 1684 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1685 | |
31fb32e1 RR |
1686 | if (m_isConnected) |
1687 | { | |
1688 | // disconnect before column->button gets recreated | |
f4322df6 | 1689 | g_signal_handlers_disconnect_by_func( column->button, |
9861f022 | 1690 | (GtkWidget*) gtk_dataview_header_button_press_callback, this); |
f4322df6 | 1691 | |
31fb32e1 RR |
1692 | m_isConnected = false; |
1693 | } | |
1694 | ||
09dfa6a0 VZ |
1695 | // FIXME: can it really happen that we don't have the owner here?? |
1696 | wxDataViewCtrl *ctrl = GetOwner(); | |
1697 | gtk_tree_view_column_set_title( column, ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont()) | |
1698 | : wxGTK_CONV_SYS(title) ); | |
31fb32e1 | 1699 | |
07a84e7b RR |
1700 | gtk_tree_view_column_set_widget( column, NULL ); |
1701 | } | |
1702 | ||
9861f022 RR |
1703 | wxString wxDataViewColumn::GetTitle() const |
1704 | { | |
1705 | const gchar *str = gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column) ); | |
1706 | return wxConvFileName->cMB2WX(str); | |
1707 | } | |
1708 | ||
07a84e7b RR |
1709 | void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap ) |
1710 | { | |
1711 | wxDataViewColumnBase::SetBitmap( bitmap ); | |
1712 | ||
9861f022 | 1713 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
07a84e7b RR |
1714 | if (bitmap.Ok()) |
1715 | { | |
1716 | GtkImage *gtk_image = GTK_IMAGE( gtk_image_new() ); | |
f4322df6 | 1717 | |
07a84e7b RR |
1718 | GdkBitmap *mask = (GdkBitmap *) NULL; |
1719 | if (bitmap.GetMask()) | |
1720 | mask = bitmap.GetMask()->GetBitmap(); | |
1721 | ||
1722 | if (bitmap.HasPixbuf()) | |
1723 | { | |
1724 | gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image), | |
1725 | bitmap.GetPixbuf()); | |
1726 | } | |
1727 | else | |
1728 | { | |
1729 | gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image), | |
1730 | bitmap.GetPixmap(), mask); | |
1731 | } | |
1732 | gtk_widget_show( GTK_WIDGET(gtk_image) ); | |
f4322df6 | 1733 | |
07a84e7b RR |
1734 | gtk_tree_view_column_set_widget( column, GTK_WIDGET(gtk_image) ); |
1735 | } | |
1736 | else | |
1737 | { | |
1738 | gtk_tree_view_column_set_widget( column, NULL ); | |
1739 | } | |
fa28826d RR |
1740 | } |
1741 | ||
9861f022 RR |
1742 | void wxDataViewColumn::SetHidden( bool hidden ) |
1743 | { | |
1744 | gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden ); | |
1745 | } | |
1746 | ||
1747 | void wxDataViewColumn::SetResizeable( bool resizeable ) | |
1748 | { | |
1749 | gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable ); | |
1750 | } | |
1751 | ||
47cef10f RR |
1752 | void wxDataViewColumn::SetAlignment( wxAlignment align ) |
1753 | { | |
9861f022 | 1754 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1755 | |
47cef10f RR |
1756 | gfloat xalign = 0.0; |
1757 | if (align == wxALIGN_RIGHT) | |
1758 | xalign = 1.0; | |
9861f022 RR |
1759 | if (align == wxALIGN_CENTER_HORIZONTAL || |
1760 | align == wxALIGN_CENTER) | |
47cef10f | 1761 | xalign = 0.5; |
f4322df6 | 1762 | |
9861f022 RR |
1763 | gtk_tree_view_column_set_alignment( column, xalign ); |
1764 | } | |
1765 | ||
1766 | wxAlignment wxDataViewColumn::GetAlignment() const | |
1767 | { | |
1768 | gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) ); | |
1769 | ||
1770 | if (xalign == 1.0) | |
1771 | return wxALIGN_RIGHT; | |
1772 | if (xalign == 0.5) | |
1773 | return wxALIGN_CENTER_HORIZONTAL; | |
f4322df6 | 1774 | |
9861f022 | 1775 | return wxALIGN_LEFT; |
47cef10f RR |
1776 | } |
1777 | ||
31fb32e1 RR |
1778 | void wxDataViewColumn::SetSortable( bool sortable ) |
1779 | { | |
9861f022 | 1780 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
31fb32e1 RR |
1781 | gtk_tree_view_column_set_sort_indicator( column, sortable ); |
1782 | } | |
1783 | ||
87f0efe2 | 1784 | bool wxDataViewColumn::IsSortable() const |
31fb32e1 | 1785 | { |
9861f022 | 1786 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
31fb32e1 RR |
1787 | return gtk_tree_view_column_get_sort_indicator( column ); |
1788 | } | |
1789 | ||
9861f022 RR |
1790 | bool wxDataViewColumn::IsResizeable() const |
1791 | { | |
1792 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
1793 | return gtk_tree_view_column_get_resizable( column ); | |
1794 | } | |
1795 | ||
1796 | bool wxDataViewColumn::IsHidden() const | |
1797 | { | |
1798 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
1799 | return !gtk_tree_view_column_get_visible( column ); | |
1800 | } | |
1801 | ||
47cef10f RR |
1802 | void wxDataViewColumn::SetSortOrder( bool ascending ) |
1803 | { | |
9861f022 | 1804 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1805 | |
47cef10f RR |
1806 | if (ascending) |
1807 | gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING ); | |
1808 | else | |
1809 | gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING ); | |
1810 | } | |
1811 | ||
87f0efe2 | 1812 | bool wxDataViewColumn::IsSortOrderAscending() const |
31fb32e1 | 1813 | { |
9861f022 | 1814 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); |
f4322df6 | 1815 | |
31fb32e1 RR |
1816 | return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING); |
1817 | } | |
1818 | ||
9861f022 | 1819 | void wxDataViewColumn::SetMinWidth( int width ) |
533544f2 | 1820 | { |
9861f022 | 1821 | gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width ); |
533544f2 RR |
1822 | } |
1823 | ||
9861f022 RR |
1824 | int wxDataViewColumn::GetMinWidth() const |
1825 | { | |
1826 | return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) ); | |
1827 | } | |
1828 | ||
1829 | int wxDataViewColumn::GetWidth() const | |
533544f2 | 1830 | { |
9861f022 | 1831 | return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) ); |
533544f2 RR |
1832 | } |
1833 | ||
9861f022 | 1834 | void wxDataViewColumn::SetWidth( int width ) |
533544f2 | 1835 | { |
9861f022 | 1836 | gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width ); |
533544f2 RR |
1837 | } |
1838 | ||
9861f022 | 1839 | |
eb7f97f8 RR |
1840 | //----------------------------------------------------------------------------- |
1841 | // wxDataViewCtrl signal callbacks | |
1842 | //----------------------------------------------------------------------------- | |
1843 | ||
1844 | static void | |
1845 | wxdataview_selection_changed_callback( GtkTreeSelection* selection, wxDataViewCtrl *dv ) | |
1846 | { | |
b94db696 RR |
1847 | if (!GTK_WIDGET_REALIZED(dv->m_widget)) |
1848 | return; | |
f4322df6 | 1849 | |
eb7f97f8 | 1850 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_SELECTED, dv->GetId() ); |
6ee2c58b | 1851 | event.SetRow( dv->GetSelection() ); |
eb7f97f8 RR |
1852 | event.SetModel( dv->GetModel() ); |
1853 | dv->GetEventHandler()->ProcessEvent( event ); | |
1854 | } | |
1855 | ||
f828871d | 1856 | static void |
f4322df6 | 1857 | wxdataview_row_activated_callback( GtkTreeView* treeview, GtkTreePath *path, |
f828871d RR |
1858 | GtkTreeViewColumn *column, wxDataViewCtrl *dv ) |
1859 | { | |
1860 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED, dv->GetId() ); | |
1861 | unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0]; | |
1862 | event.SetRow( row ); | |
1863 | event.SetModel( dv->GetModel() ); | |
1864 | dv->GetEventHandler()->ProcessEvent( event ); | |
1865 | } | |
1866 | ||
790b137e RR |
1867 | //----------------------------------------------------------------------------- |
1868 | // wxDataViewCtrl | |
1869 | //----------------------------------------------------------------------------- | |
1870 | ||
239eaa41 RR |
1871 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase) |
1872 | ||
1873 | wxDataViewCtrl::~wxDataViewCtrl() | |
1874 | { | |
8f850e28 RR |
1875 | if (m_notifier) |
1876 | GetModel()->RemoveNotifier( m_notifier ); | |
87f0efe2 RR |
1877 | |
1878 | // remove the model from the GtkTreeView before it gets destroyed by the | |
1879 | // wxDataViewCtrlBase's dtor | |
1880 | gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL ); | |
239eaa41 RR |
1881 | } |
1882 | ||
1883 | void wxDataViewCtrl::Init() | |
1884 | { | |
8f850e28 | 1885 | m_notifier = NULL; |
239eaa41 RR |
1886 | } |
1887 | ||
1888 | bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id, | |
93763ad5 | 1889 | const wxPoint& pos, const wxSize& size, |
239eaa41 RR |
1890 | long style, const wxValidator& validator ) |
1891 | { | |
1892 | Init(); | |
93763ad5 WS |
1893 | |
1894 | m_needParent = true; | |
239eaa41 RR |
1895 | |
1896 | if (!PreCreation( parent, pos, size ) || | |
1897 | !CreateBase( parent, id, pos, size, style, validator )) | |
1898 | { | |
1899 | wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") ); | |
93763ad5 | 1900 | return false; |
239eaa41 | 1901 | } |
1a367564 RR |
1902 | |
1903 | m_widget = gtk_scrolled_window_new (NULL, NULL); | |
6493aaca VZ |
1904 | |
1905 | GtkScrolledWindowSetBorder(m_widget, style); | |
1a367564 RR |
1906 | |
1907 | m_treeview = gtk_tree_view_new(); | |
1908 | gtk_container_add (GTK_CONTAINER (m_widget), m_treeview); | |
ed4b0fdc | 1909 | |
b94db696 RR |
1910 | #ifdef __WXGTK26__ |
1911 | if (!gtk_check_version(2,6,0)) | |
1912 | gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), TRUE ); | |
1913 | #endif | |
1914 | ||
daebb44c RR |
1915 | if (style & wxDV_MULTIPLE) |
1916 | { | |
1917 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
1918 | gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE ); | |
1919 | } | |
93763ad5 | 1920 | |
9861f022 RR |
1921 | gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 ); |
1922 | ||
1923 | #ifdef __WXGTK210__ | |
1924 | if (!gtk_check_version(2,10,0)) | |
1925 | { | |
1926 | GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE; | |
f4322df6 VZ |
1927 | |
1928 | if ((style & wxDV_HORIZ_RULES) != 0 && | |
9861f022 RR |
1929 | (style & wxDV_VERT_RULES) != 0) |
1930 | grid = GTK_TREE_VIEW_GRID_LINES_BOTH; | |
1931 | else if (style & wxDV_VERT_RULES) | |
1932 | grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL; | |
1933 | else if (style & wxDV_HORIZ_RULES) | |
1934 | grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL; | |
1935 | ||
1936 | gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid ); | |
1937 | } | |
1938 | else | |
1939 | #endif | |
1940 | gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_HORIZ_RULES) != 0 ); | |
1941 | ||
1a367564 RR |
1942 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget), |
1943 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); | |
1944 | gtk_widget_show (m_treeview); | |
93763ad5 | 1945 | |
239eaa41 RR |
1946 | m_parent->DoAddChild( this ); |
1947 | ||
b94db696 RR |
1948 | PostCreation(size); |
1949 | ||
eb7f97f8 RR |
1950 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); |
1951 | g_signal_connect_after (selection, "changed", | |
1952 | G_CALLBACK (wxdataview_selection_changed_callback), this); | |
f828871d RR |
1953 | g_signal_connect_after (m_treeview, "row_activated", |
1954 | G_CALLBACK (wxdataview_row_activated_callback), this); | |
eb7f97f8 | 1955 | |
239eaa41 RR |
1956 | return true; |
1957 | } | |
1958 | ||
31fb32e1 RR |
1959 | void wxDataViewCtrl::OnInternalIdle() |
1960 | { | |
1961 | wxWindow::OnInternalIdle(); | |
f4322df6 | 1962 | |
9861f022 | 1963 | unsigned int cols = GetColumnCount(); |
31fb32e1 RR |
1964 | unsigned int i; |
1965 | for (i = 0; i < cols; i++) | |
1966 | { | |
1967 | wxDataViewColumn *col = GetColumn( i ); | |
1968 | col->OnInternalIdle(); | |
1969 | } | |
1970 | } | |
1971 | ||
6e2e590f | 1972 | bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model ) |
239eaa41 RR |
1973 | { |
1974 | if (!wxDataViewCtrlBase::AssociateModel( model )) | |
1975 | return false; | |
1976 | ||
6e2e590f RR |
1977 | GtkWxListStore *gtk_store = wxgtk_list_store_new(); |
1978 | gtk_store->model = model; | |
1979 | ||
8f850e28 | 1980 | m_notifier = new wxGtkDataViewListModelNotifier( gtk_store, model ); |
6e2e590f | 1981 | |
93763ad5 | 1982 | model->AddNotifier( m_notifier ); |
1557c77b | 1983 | |
1a367564 RR |
1984 | gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_store) ); |
1985 | g_object_unref( gtk_store ); | |
93763ad5 | 1986 | |
239eaa41 RR |
1987 | return true; |
1988 | } | |
790b137e | 1989 | |
fa28826d RR |
1990 | bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col ) |
1991 | { | |
1992 | if (!wxDataViewCtrlBase::AppendColumn(col)) | |
1993 | return false; | |
93763ad5 | 1994 | |
fa28826d RR |
1995 | GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle(); |
1996 | ||
1a367564 | 1997 | gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), column ); |
fa28826d RR |
1998 | |
1999 | return true; | |
2000 | } | |
2001 | ||
6ff7eee7 RR |
2002 | void wxDataViewCtrl::SetSelection( int row ) |
2003 | { | |
fc211fe5 | 2004 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); |
f4322df6 | 2005 | |
fc211fe5 RR |
2006 | if (row < 0) |
2007 | { | |
2008 | gtk_tree_selection_unselect_all( selection ); | |
2009 | } | |
2010 | else | |
2011 | { | |
2012 | GtkTreePath *path = gtk_tree_path_new (); | |
2013 | gtk_tree_path_append_index( path, row ); | |
f4322df6 | 2014 | |
fc211fe5 | 2015 | gtk_tree_selection_select_path( selection, path ); |
f4322df6 | 2016 | |
fc211fe5 RR |
2017 | gtk_tree_path_free( path ); |
2018 | } | |
2019 | } | |
2020 | ||
2021 | void wxDataViewCtrl::Unselect( unsigned int row ) | |
2022 | { | |
2023 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
f4322df6 | 2024 | |
fc211fe5 RR |
2025 | GtkTreePath *path = gtk_tree_path_new (); |
2026 | gtk_tree_path_append_index( path, row ); | |
f4322df6 | 2027 | |
fc211fe5 | 2028 | gtk_tree_selection_unselect_path( selection, path ); |
f4322df6 | 2029 | |
fc211fe5 | 2030 | gtk_tree_path_free( path ); |
6ff7eee7 RR |
2031 | } |
2032 | ||
2033 | void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to ) | |
2034 | { | |
2035 | } | |
2036 | ||
2037 | void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections) | |
2038 | { | |
2039 | } | |
f4322df6 | 2040 | |
6ff7eee7 RR |
2041 | bool wxDataViewCtrl::IsSelected( unsigned int row ) const |
2042 | { | |
fc211fe5 | 2043 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); |
f4322df6 | 2044 | |
fc211fe5 RR |
2045 | GtkTreePath *path = gtk_tree_path_new (); |
2046 | gtk_tree_path_append_index( path, row ); | |
f4322df6 | 2047 | |
fc211fe5 | 2048 | gboolean ret = gtk_tree_selection_path_is_selected( selection, path ); |
f4322df6 | 2049 | |
fc211fe5 | 2050 | gtk_tree_path_free( path ); |
f4322df6 | 2051 | |
fc211fe5 | 2052 | return ret; |
6ff7eee7 RR |
2053 | } |
2054 | ||
2055 | int wxDataViewCtrl::GetSelection() const | |
2056 | { | |
6ee2c58b RR |
2057 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); |
2058 | if (HasFlag(wxDV_MULTIPLE)) | |
2059 | { | |
2060 | GtkTreeModel *model; | |
2061 | GList *list = gtk_tree_selection_get_selected_rows( selection, &model ); | |
f4322df6 | 2062 | |
6ee2c58b RR |
2063 | // do something |
2064 | if (list) | |
2065 | { | |
2066 | // list = g_list_nth( list, 0 ); should be a noop | |
2067 | GtkTreePath *path = (GtkTreePath*) list->data; | |
f4322df6 | 2068 | |
6ee2c58b | 2069 | unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0]; |
f4322df6 | 2070 | |
6ee2c58b RR |
2071 | // delete list |
2072 | g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL ); | |
2073 | g_list_free( list ); | |
f4322df6 | 2074 | |
6ee2c58b RR |
2075 | return (int) row; |
2076 | } | |
2077 | } | |
2078 | else | |
2079 | { | |
f4322df6 | 2080 | |
6ee2c58b RR |
2081 | GtkTreeModel *model; |
2082 | GtkTreeIter iter; | |
2083 | gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter ); | |
2084 | if (has_selection) | |
2085 | { | |
2086 | unsigned int row = (wxUIntPtr) iter.user_data; | |
2087 | return (int) row; | |
2088 | } | |
2089 | } | |
f4322df6 | 2090 | |
6ff7eee7 RR |
2091 | return -1; |
2092 | } | |
2093 | ||
2094 | int wxDataViewCtrl::GetSelections(wxArrayInt& aSelections) const | |
2095 | { | |
6ee2c58b RR |
2096 | aSelections.Clear(); |
2097 | ||
2098 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
2099 | if (HasFlag(wxDV_MULTIPLE)) | |
2100 | { | |
2101 | GtkTreeModel *model; | |
2102 | GList *list = gtk_tree_selection_get_selected_rows( selection, &model ); | |
f4322df6 | 2103 | |
6ee2c58b RR |
2104 | int count = 0; |
2105 | while (list) | |
2106 | { | |
f4322df6 | 2107 | |
6ee2c58b RR |
2108 | // list = g_list_nth( list, 0 ); should be a noop |
2109 | GtkTreePath *path = (GtkTreePath*) list->data; | |
f4322df6 | 2110 | |
6ee2c58b | 2111 | unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0]; |
f4322df6 | 2112 | |
6ee2c58b | 2113 | aSelections.Add( (int) row ); |
f4322df6 VZ |
2114 | |
2115 | list = g_list_next( list ); | |
6ee2c58b | 2116 | } |
f4322df6 | 2117 | |
6ee2c58b RR |
2118 | // delete list |
2119 | g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL ); | |
2120 | g_list_free( list ); | |
f4322df6 | 2121 | |
6ee2c58b RR |
2122 | return count; |
2123 | } | |
2124 | else | |
2125 | { | |
2126 | GtkTreeModel *model; | |
2127 | GtkTreeIter iter; | |
2128 | gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter ); | |
2129 | if (has_selection) | |
2130 | { | |
2131 | unsigned int row = (wxUIntPtr) iter.user_data; | |
2132 | aSelections.Add( (int) row ); | |
2133 | return 1; | |
2134 | } | |
2135 | } | |
f4322df6 | 2136 | |
6ff7eee7 RR |
2137 | return 0; |
2138 | } | |
2139 | ||
b94db696 RR |
2140 | // static |
2141 | wxVisualAttributes | |
2142 | wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
2143 | { | |
2144 | return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new); | |
2145 | } | |
2146 | ||
6ff7eee7 | 2147 | |
93763ad5 | 2148 | #endif |
4ed7af08 | 2149 | // !wxUSE_GENERICDATAVIEWCTRL |
790b137e | 2150 | |
93763ad5 | 2151 | #endif |
4ed7af08 | 2152 | // wxUSE_DATAVIEWCTRL |