]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
Preserve original menu label for later retrieval
[wxWidgets.git] / src / gtk / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataview.cpp
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
13 #if wxUSE_DATAVIEWCTRL
14
15 #include "wx/dataview.h"
16
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
18
19 #ifndef WX_PRECOMP
20 #include "wx/log.h"
21 #include "wx/dcclient.h"
22 #include "wx/sizer.h"
23 #endif
24
25 #include "wx/stockitem.h"
26 #include "wx/calctrl.h"
27 #include "wx/popupwin.h"
28 #include "wx/icon.h"
29 #include "wx/list.h"
30 #include "wx/listimpl.cpp"
31
32
33 #include "wx/gtk/private.h"
34 #include "wx/gtk/win_gtk.h"
35
36 #include <gobject/gvaluecollector.h>
37 #include <gtk/gtktreemodel.h>
38 #include <gtk/gtktreesortable.h>
39 #include <gtk/gtktreednd.h>
40
41 #include <gdk/gdkkeysyms.h>
42
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45
46 wxDataViewModel *g_model = NULL;
47
48 class wxGtkTreeModelNode;
49
50 extern "C" {
51 typedef struct _GtkWxTreeModel GtkWxTreeModel;
52 }
53
54 //-----------------------------------------------------------------------------
55 // wxDataViewCtrlInternal
56 //-----------------------------------------------------------------------------
57
58 WX_DECLARE_LIST(wxDataViewItem, ItemList);
59 WX_DEFINE_LIST(ItemList);
60
61 class wxDataViewCtrlInternal
62 {
63 public:
64 wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model, GtkWxTreeModel *owner );
65 ~wxDataViewCtrlInternal();
66
67 gboolean get_iter( GtkTreeIter *iter, GtkTreePath *path );
68 GtkTreePath *get_path( GtkTreeIter *iter);
69 GtkTreePath *get_path_safe( GtkTreeIter *iter);
70 gboolean iter_next( GtkTreeIter *iter );
71 gboolean iter_children( GtkTreeIter *iter, GtkTreeIter *parent);
72 gboolean iter_has_child( GtkTreeIter *iter );
73 gint iter_n_children( GtkTreeIter *iter );
74 gboolean iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n );
75 gboolean iter_parent( GtkTreeIter *iter, GtkTreeIter *child );
76
77 wxDataViewModel* GetDataViewModel() { return m_wx_model; }
78 wxDataViewCtrl* GetOwner() { return m_owner; }
79 GtkWxTreeModel* GetGtkModel() { return m_gtk_model; }
80
81 bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
82 bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
83 bool ItemChanged( const wxDataViewItem &item );
84 bool ValueChanged( const wxDataViewItem &item, unsigned int col );
85 bool Cleared();
86 void Resort();
87
88 protected:
89 void InitTree();
90 wxGtkTreeModelNode *FindNode( const wxDataViewItem &item );
91 wxGtkTreeModelNode *FindNode( GtkTreeIter *iter );
92 wxGtkTreeModelNode *FindParentNode( const wxDataViewItem &item );
93 wxGtkTreeModelNode *FindParentNode( GtkTreeIter *iter );
94 void BuildBranch( wxGtkTreeModelNode *branch );
95
96 private:
97 wxGtkTreeModelNode *m_root;
98 wxDataViewModel *m_wx_model;
99 GtkWxTreeModel *m_gtk_model;
100 wxDataViewCtrl *m_owner;
101 };
102
103
104 //-----------------------------------------------------------------------------
105 // wxGtkTreeModelNode
106 //-----------------------------------------------------------------------------
107
108 int LINKAGEMODE wxGtkTreeModelNodeCmp( void *id1, void *id2 );
109
110 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode*, wxGtkTreeModelNodes );
111 WX_DEFINE_SORTED_ARRAY( void* , wxGtkTreeModelChildren );
112
113 class wxGtkTreeModelNode
114 {
115 public:
116 wxGtkTreeModelNode( wxGtkTreeModelNode* parent, const wxDataViewItem &item,
117 wxDataViewCtrlInternal *internal )
118 {
119 m_parent = parent;
120 m_item = item;
121 m_internal = internal;
122 g_model = internal->GetDataViewModel();
123 m_children = new wxGtkTreeModelChildren( wxGtkTreeModelNodeCmp );
124 }
125
126 ~wxGtkTreeModelNode()
127 {
128 g_model = m_internal->GetDataViewModel();
129 size_t count = m_children->GetCount();
130 size_t i;
131 for (i = 0; i < count; i++)
132 {
133 wxGtkTreeModelNode *child = m_nodes.Item( i );
134 delete child;
135 }
136 delete m_children;
137 }
138
139 unsigned int AddNode( wxGtkTreeModelNode* child )
140 {
141 g_model = m_internal->GetDataViewModel();
142 m_nodes.Add( child );
143 return m_children->Add( child->GetItem().GetID() );
144 }
145
146 unsigned int AddLeave( void* id )
147 {
148 g_model = m_internal->GetDataViewModel();
149 return m_children->Add( id );
150 }
151
152 void DeleteChild( void* id )
153 {
154 g_model = m_internal->GetDataViewModel();
155 size_t pos;
156 size_t count = m_children->GetCount();
157 for (pos = 0; pos < count; pos++)
158 {
159 if (m_children->Item( pos ) == id)
160 {
161 m_children->RemoveAt( pos );
162 break;
163 }
164 }
165 count = m_nodes.GetCount();
166 for (pos = 0; pos < count; pos++)
167 {
168 wxGtkTreeModelNode *node = m_nodes.Item( pos );
169 if (node->GetItem().GetID() == id)
170 {
171 m_nodes.RemoveAt( pos );
172 delete node;
173 break;
174 }
175 }
176
177 }
178
179 wxGtkTreeModelNode* GetParent()
180 { return m_parent; }
181 wxGtkTreeModelNodes &GetNodes()
182 { return m_nodes; }
183 wxGtkTreeModelChildren &GetChildren()
184 { return *m_children; }
185
186 unsigned int GetChildCount() { return m_children->GetCount(); }
187 unsigned int GetNodesCount() { return m_nodes.GetCount(); }
188
189 wxDataViewItem &GetItem() { return m_item; }
190 wxDataViewCtrlInternal *GetInternal() { return m_internal; }
191
192 void Resort();
193
194 private:
195 wxGtkTreeModelNode *m_parent;
196 wxGtkTreeModelNodes m_nodes;
197 wxGtkTreeModelChildren *m_children;
198 wxDataViewItem m_item;
199 wxDataViewCtrlInternal *m_internal;
200 };
201
202
203 int LINKAGEMODE wxGtkTreeModelNodeCmp( void* id1, void* id2 )
204 {
205 return g_model->Compare( id1, id2 );
206 }
207
208 //-----------------------------------------------------------------------------
209 // data
210 //-----------------------------------------------------------------------------
211
212 extern bool g_blockEventsOnDrag;
213
214 //-----------------------------------------------------------------------------
215 // define new GTK+ class wxGtkTreeModel
216 //-----------------------------------------------------------------------------
217
218 extern "C" {
219
220 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
221 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
222 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
223 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
224 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
225 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
226
227 GType gtk_wx_tree_model_get_type (void);
228
229 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass;
230
231 struct _GtkWxTreeModel
232 {
233 GObject parent;
234
235 /*< private >*/
236 gint stamp;
237 wxDataViewCtrlInternal *internal;
238 };
239
240 struct _GtkWxTreeModelClass
241 {
242 GObjectClass list_parent_class;
243 };
244
245 static GtkWxTreeModel *wxgtk_tree_model_new (void);
246 static void wxgtk_tree_model_init (GtkWxTreeModel *tree_model);
247 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass);
248 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface);
249 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface);
250 static void wxgtk_tree_model_finalize (GObject *object);
251 static GtkTreeModelFlags wxgtk_tree_model_get_flags (GtkTreeModel *tree_model);
252 static gint wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model);
253 static GType wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
254 gint index);
255 static gboolean wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
256 GtkTreeIter *iter,
257 GtkTreePath *path);
258 static GtkTreePath *wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
259 GtkTreeIter *iter);
260 static GtkTreePath *wxgtk_tree_model_get_path_safe (GtkTreeModel *tree_model,
261 GtkTreeIter *iter);
262 static void wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
263 GtkTreeIter *iter,
264 gint column,
265 GValue *value);
266 static gboolean wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
267 GtkTreeIter *iter);
268 static gboolean wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
269 GtkTreeIter *iter,
270 GtkTreeIter *parent);
271 static gboolean wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
272 GtkTreeIter *iter);
273 static gint wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
274 GtkTreeIter *iter);
275 static gboolean wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
276 GtkTreeIter *iter,
277 GtkTreeIter *parent,
278 gint n);
279 static gboolean wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
280 GtkTreeIter *iter,
281 GtkTreeIter *child);
282
283 /* sortable */
284 static gboolean wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
285 gint *sort_column_id,
286 GtkSortType *order);
287 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
288 gint sort_column_id,
289 GtkSortType order);
290 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
291 gint sort_column_id,
292 GtkTreeIterCompareFunc func,
293 gpointer data,
294 GtkDestroyNotify destroy);
295 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
296 GtkTreeIterCompareFunc func,
297 gpointer data,
298 GtkDestroyNotify destroy);
299 static gboolean wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable);
300
301
302
303 static GObjectClass *list_parent_class = NULL;
304
305 GType
306 gtk_wx_tree_model_get_type (void)
307 {
308 static GType tree_model_type = 0;
309
310 if (!tree_model_type)
311 {
312 const GTypeInfo tree_model_info =
313 {
314 sizeof (GtkWxTreeModelClass),
315 NULL, /* base_init */
316 NULL, /* base_finalize */
317 (GClassInitFunc) wxgtk_tree_model_class_init,
318 NULL, /* class_finalize */
319 NULL, /* class_data */
320 sizeof (GtkWxTreeModel),
321 0,
322 (GInstanceInitFunc) wxgtk_tree_model_init,
323 };
324
325 static const GInterfaceInfo tree_model_iface_info =
326 {
327 (GInterfaceInitFunc) wxgtk_tree_model_tree_model_init,
328 NULL,
329 NULL
330 };
331
332 static const GInterfaceInfo sortable_iface_info =
333 {
334 (GInterfaceInitFunc) wxgtk_tree_model_sortable_init,
335 NULL,
336 NULL
337 };
338
339 tree_model_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxTreeModel",
340 &tree_model_info, (GTypeFlags)0 );
341
342 g_type_add_interface_static (tree_model_type,
343 GTK_TYPE_TREE_MODEL,
344 &tree_model_iface_info);
345 g_type_add_interface_static (tree_model_type,
346 GTK_TYPE_TREE_SORTABLE,
347 &sortable_iface_info);
348 }
349
350 return tree_model_type;
351 }
352
353 static GtkWxTreeModel *
354 wxgtk_tree_model_new(void)
355 {
356 GtkWxTreeModel *retval = (GtkWxTreeModel *) g_object_new (GTK_TYPE_WX_TREE_MODEL, NULL);
357 return retval;
358 }
359
360 static void
361 wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass)
362 {
363 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
364 GObjectClass *object_class = (GObjectClass*) klass;
365 object_class->finalize = wxgtk_tree_model_finalize;
366 }
367
368 static void
369 wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface)
370 {
371 iface->get_flags = wxgtk_tree_model_get_flags;
372 iface->get_n_columns = wxgtk_tree_model_get_n_columns;
373 iface->get_column_type = wxgtk_tree_model_get_column_type;
374 iface->get_iter = wxgtk_tree_model_get_iter;
375 iface->get_path = wxgtk_tree_model_get_path;
376 iface->get_value = wxgtk_tree_model_get_value;
377 iface->iter_next = wxgtk_tree_model_iter_next;
378 iface->iter_children = wxgtk_tree_model_iter_children;
379 iface->iter_has_child = wxgtk_tree_model_iter_has_child;
380 iface->iter_n_children = wxgtk_tree_model_iter_n_children;
381 iface->iter_nth_child = wxgtk_tree_model_iter_nth_child;
382 iface->iter_parent = wxgtk_tree_model_iter_parent;
383 }
384
385 static void
386 wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface)
387 {
388 iface->get_sort_column_id = wxgtk_tree_model_get_sort_column_id;
389 iface->set_sort_column_id = wxgtk_tree_model_set_sort_column_id;
390 iface->set_sort_func = wxgtk_tree_model_set_sort_func;
391 iface->set_default_sort_func = wxgtk_tree_model_set_default_sort_func;
392 iface->has_default_sort_func = wxgtk_tree_model_has_default_sort_func;
393 }
394
395 static void
396 wxgtk_tree_model_init (GtkWxTreeModel *tree_model)
397 {
398 tree_model->internal = NULL;
399 tree_model->stamp = g_random_int();
400 }
401
402 static void
403 wxgtk_tree_model_finalize (GObject *object)
404 {
405 /* must chain up */
406 (* list_parent_class->finalize) (object);
407 }
408
409 } // extern "C"
410
411 //-----------------------------------------------------------------------------
412 // implement callbacks from wxGtkTreeModel class by letting
413 // them call the methods of wxWidgets' wxDataViewModel
414 //-----------------------------------------------------------------------------
415
416 static GtkTreeModelFlags
417 wxgtk_tree_model_get_flags (GtkTreeModel *tree_model)
418 {
419 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model), (GtkTreeModelFlags)0 );
420
421 return GTK_TREE_MODEL_ITERS_PERSIST;
422 }
423
424 static gint
425 wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model)
426 {
427 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
428 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), 0);
429
430 return wxtree_model->internal->GetDataViewModel()->GetColumnCount();
431 }
432
433 static GType
434 wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
435 gint index)
436 {
437 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
438 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), G_TYPE_INVALID);
439
440 GType gtype = G_TYPE_INVALID;
441
442 wxString wxtype = wxtree_model->internal->GetDataViewModel()->GetColumnType( (unsigned int) index );
443
444 if (wxtype == wxT("string"))
445 gtype = G_TYPE_STRING;
446 else
447 {
448 wxFAIL_MSG( _T("non-string columns not supported yet") );
449 }
450
451 return gtype;
452 }
453
454 static gboolean
455 wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
456 GtkTreeIter *iter,
457 GtkTreePath *path)
458 {
459 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
460 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
461 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
462
463 return wxtree_model->internal->get_iter( iter, path );
464 }
465
466 static GtkTreePath *
467 wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
468 GtkTreeIter *iter)
469 {
470 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
471 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), NULL);
472 g_return_val_if_fail (iter->stamp == GTK_WX_TREE_MODEL (wxtree_model)->stamp, NULL);
473
474 return wxtree_model->internal->get_path( iter );
475 }
476
477 static GtkTreePath *
478 wxgtk_tree_model_get_path_safe (GtkTreeModel *tree_model,
479 GtkTreeIter *iter)
480 {
481 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
482 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), NULL);
483 g_return_val_if_fail (iter->stamp == GTK_WX_TREE_MODEL (wxtree_model)->stamp, NULL);
484
485 return wxtree_model->internal->get_path_safe( iter );
486 }
487
488 static void
489 wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
490 GtkTreeIter *iter,
491 gint column,
492 GValue *value)
493 {
494 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
495 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model) );
496
497 wxDataViewModel *model = wxtree_model->internal->GetDataViewModel();
498 wxString mtype = model->GetColumnType( (unsigned int) column );
499 if (mtype == wxT("string"))
500 {
501 wxVariant variant;
502 g_value_init( value, G_TYPE_STRING );
503 wxDataViewItem item( (void*) iter->user_data );
504 model->GetValue( variant, item, (unsigned int) column );
505
506 g_value_set_string( value, variant.GetString().utf8_str() );
507 }
508 else
509 {
510 wxFAIL_MSG( _T("non-string columns not supported yet") );
511 }
512 }
513
514 static gboolean
515 wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
516 GtkTreeIter *iter)
517 {
518 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
519 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
520 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
521
522 return wxtree_model->internal->iter_next( iter );
523 }
524
525 static gboolean
526 wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
527 GtkTreeIter *iter,
528 GtkTreeIter *parent)
529 {
530 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
531 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
532 g_return_val_if_fail (wxtree_model->stamp == parent->stamp, FALSE);
533
534 return wxtree_model->internal->iter_children( iter, parent );
535 }
536
537 static gboolean
538 wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
539 GtkTreeIter *iter)
540 {
541 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
542 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
543 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
544
545 return wxtree_model->internal->iter_has_child( iter );
546 }
547
548 static gint
549 wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
550 GtkTreeIter *iter)
551 {
552 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
553 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
554 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, 0);
555
556 return wxtree_model->internal->iter_n_children( iter );
557 }
558
559 static gboolean
560 wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
561 GtkTreeIter *iter,
562 GtkTreeIter *parent,
563 gint n)
564 {
565 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
566 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
567
568 return wxtree_model->internal->iter_nth_child( iter, parent, n );
569 }
570
571 static gboolean
572 wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
573 GtkTreeIter *iter,
574 GtkTreeIter *child)
575 {
576 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
577 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
578 g_return_val_if_fail (wxtree_model->stamp == child->stamp, FALSE);
579
580 return wxtree_model->internal->iter_parent( iter, child );
581 }
582
583 /* sortable */
584 gboolean wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
585 gint *sort_column_id,
586 GtkSortType *order)
587 {
588 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) sortable;
589
590 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE);
591
592 if (sort_column_id)
593 *sort_column_id = tree_model->internal->GetDataViewModel()->GetSortingColumn();
594
595 if (order)
596 {
597 bool ascending = tree_model->internal->GetDataViewModel()->GetSortOrderAscending();
598 if (ascending)
599 *order = GTK_SORT_ASCENDING;
600 else
601 *order = GTK_SORT_DESCENDING;
602 }
603
604 return TRUE;
605 }
606
607 void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
608 gint sort_column_id,
609 GtkSortType order)
610 {
611 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) sortable;
612 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
613
614 bool ascending = TRUE;
615 if (order != GTK_SORT_ASCENDING)
616 ascending = FALSE;
617
618 if ((sort_column_id == (gint) tree_model->internal->GetDataViewModel()->GetSortingColumn()) &&
619 (ascending == tree_model->internal->GetDataViewModel()->GetSortOrderAscending()))
620 return;
621
622 tree_model->internal->GetDataViewModel()->SetSortingColumn( sort_column_id );
623
624 tree_model->internal->GetDataViewModel()->SetSortOrderAscending( ascending );
625
626 gtk_tree_sortable_sort_column_changed (sortable);
627
628 tree_model->internal->GetDataViewModel()->Resort();
629 }
630
631 void wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
632 gint sort_column_id,
633 GtkTreeIterCompareFunc func,
634 gpointer data,
635 GtkDestroyNotify destroy)
636 {
637 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
638 g_return_if_fail (func != NULL);
639 }
640
641 void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
642 GtkTreeIterCompareFunc func,
643 gpointer data,
644 GtkDestroyNotify destroy)
645 {
646 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
647 g_return_if_fail (func != NULL);
648
649 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
650 }
651
652 gboolean wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable)
653 {
654 return FALSE;
655 }
656
657 //-----------------------------------------------------------------------------
658 // define new GTK+ class wxGtkRendererRenderer
659 //-----------------------------------------------------------------------------
660
661 extern "C" {
662
663 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
664 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
665 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
666 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
667 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
668 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
669
670 GType gtk_wx_cell_renderer_get_type (void);
671
672 typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
673 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
674
675 struct _GtkWxCellRenderer
676 {
677 GtkCellRenderer parent;
678
679 /*< private >*/
680 wxDataViewCustomRenderer *cell;
681 guint32 last_click;
682 };
683
684 struct _GtkWxCellRendererClass
685 {
686 GtkCellRendererClass cell_parent_class;
687 };
688
689
690 static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
691 static void gtk_wx_cell_renderer_init (
692 GtkWxCellRenderer *cell );
693 static void gtk_wx_cell_renderer_class_init(
694 GtkWxCellRendererClass *klass );
695 static void gtk_wx_cell_renderer_finalize (
696 GObject *object );
697 static void gtk_wx_cell_renderer_get_size (
698 GtkCellRenderer *cell,
699 GtkWidget *widget,
700 GdkRectangle *rectangle,
701 gint *x_offset,
702 gint *y_offset,
703 gint *width,
704 gint *height );
705 static void gtk_wx_cell_renderer_render (
706 GtkCellRenderer *cell,
707 GdkWindow *window,
708 GtkWidget *widget,
709 GdkRectangle *background_area,
710 GdkRectangle *cell_area,
711 GdkRectangle *expose_area,
712 GtkCellRendererState flags );
713 static gboolean gtk_wx_cell_renderer_activate(
714 GtkCellRenderer *cell,
715 GdkEvent *event,
716 GtkWidget *widget,
717 const gchar *path,
718 GdkRectangle *background_area,
719 GdkRectangle *cell_area,
720 GtkCellRendererState flags );
721 static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
722 GtkCellRenderer *cell,
723 GdkEvent *event,
724 GtkWidget *widget,
725 const gchar *path,
726 GdkRectangle *background_area,
727 GdkRectangle *cell_area,
728 GtkCellRendererState flags );
729
730
731 static GObjectClass *cell_parent_class = NULL;
732
733 } // extern "C"
734
735 GType
736 gtk_wx_cell_renderer_get_type (void)
737 {
738 static GType cell_wx_type = 0;
739
740 if (!cell_wx_type)
741 {
742 const GTypeInfo cell_wx_info =
743 {
744 sizeof (GtkWxCellRendererClass),
745 NULL, /* base_init */
746 NULL, /* base_finalize */
747 (GClassInitFunc) gtk_wx_cell_renderer_class_init,
748 NULL, /* class_finalize */
749 NULL, /* class_data */
750 sizeof (GtkWxCellRenderer),
751 0, /* n_preallocs */
752 (GInstanceInitFunc) gtk_wx_cell_renderer_init,
753 };
754
755 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER,
756 "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 );
757 }
758
759 return cell_wx_type;
760 }
761
762 static void
763 gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
764 {
765 cell->cell = NULL;
766 cell->last_click = 0;
767 }
768
769 static void
770 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
771 {
772 GObjectClass *object_class = G_OBJECT_CLASS (klass);
773 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
774
775 cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
776
777 object_class->finalize = gtk_wx_cell_renderer_finalize;
778
779 cell_class->get_size = gtk_wx_cell_renderer_get_size;
780 cell_class->render = gtk_wx_cell_renderer_render;
781 cell_class->activate = gtk_wx_cell_renderer_activate;
782 cell_class->start_editing = gtk_wx_cell_renderer_start_editing;
783 }
784
785 static void
786 gtk_wx_cell_renderer_finalize (GObject *object)
787 {
788 /* must chain up */
789 (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object);
790 }
791
792 GtkCellRenderer*
793 gtk_wx_cell_renderer_new (void)
794 {
795 return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
796 }
797
798
799
800 static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
801 GtkCellRenderer *renderer,
802 GdkEvent *event,
803 GtkWidget *widget,
804 const gchar *path,
805 GdkRectangle *background_area,
806 GdkRectangle *cell_area,
807 GtkCellRendererState flags )
808 {
809 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
810 wxDataViewCustomRenderer *cell = wxrenderer->cell;
811 if (!cell->HasEditorCtrl())
812 return NULL;
813
814 GdkRectangle rect;
815 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
816 &rect.x,
817 &rect.y,
818 &rect.width,
819 &rect.height);
820
821 rect.x += cell_area->x;
822 rect.y += cell_area->y;
823 // rect.width -= renderer->xpad * 2;
824 // rect.height -= renderer->ypad * 2;
825
826 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
827 wxRect renderrect( cell_area->x, cell_area->y, cell_area->width, cell_area->height );
828
829 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
830 GtkTreeIter iter;
831 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, treepath );
832 wxDataViewItem item( (void*) iter.user_data );
833 gtk_tree_path_free( treepath );
834
835 cell->StartEditing( item, renderrect );
836
837 return NULL;
838 }
839
840 static void
841 gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
842 GtkWidget *widget,
843 GdkRectangle *cell_area,
844 gint *x_offset,
845 gint *y_offset,
846 gint *width,
847 gint *height)
848 {
849 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
850 wxDataViewCustomRenderer *cell = wxrenderer->cell;
851
852 wxSize size = cell->GetSize();
853
854 gint calc_width = (gint) renderer->xpad * 2 + size.x;
855 gint calc_height = (gint) renderer->ypad * 2 + size.y;
856
857 if (x_offset)
858 *x_offset = 0;
859 if (y_offset)
860 *y_offset = 0;
861
862 if (cell_area && size.x > 0 && size.y > 0)
863 {
864 if (x_offset)
865 {
866 *x_offset = (gint)((renderer->xalign *
867 (cell_area->width - calc_width - 2 * renderer->xpad)));
868 *x_offset = MAX (*x_offset, 0) + renderer->xpad;
869 }
870 if (y_offset)
871 {
872 *y_offset = (gint)((renderer->yalign *
873 (cell_area->height - calc_height - 2 * renderer->ypad)));
874 *y_offset = MAX (*y_offset, 0) + renderer->ypad;
875 }
876 }
877
878 if (width)
879 *width = calc_width;
880
881 if (height)
882 *height = calc_height;
883 }
884
885 static void
886 gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
887 GdkWindow *window,
888 GtkWidget *widget,
889 GdkRectangle *background_area,
890 GdkRectangle *cell_area,
891 GdkRectangle *expose_area,
892 GtkCellRendererState flags)
893
894 {
895 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
896 wxDataViewCustomRenderer *cell = wxrenderer->cell;
897
898 GdkRectangle rect;
899 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
900 &rect.x,
901 &rect.y,
902 &rect.width,
903 &rect.height);
904
905 rect.x += cell_area->x;
906 rect.y += cell_area->y;
907 rect.width -= renderer->xpad * 2;
908 rect.height -= renderer->ypad * 2;
909
910 GdkRectangle dummy;
911 if (gdk_rectangle_intersect (expose_area, &rect, &dummy))
912 {
913 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
914 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
915 if (dc->m_window == NULL)
916 {
917 dc->m_window = window;
918 dc->SetUpDC();
919 }
920
921 int state = 0;
922 if (flags & GTK_CELL_RENDERER_SELECTED)
923 state |= wxDATAVIEW_CELL_SELECTED;
924 if (flags & GTK_CELL_RENDERER_PRELIT)
925 state |= wxDATAVIEW_CELL_PRELIT;
926 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
927 state |= wxDATAVIEW_CELL_INSENSITIVE;
928 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
929 state |= wxDATAVIEW_CELL_INSENSITIVE;
930 if (flags & GTK_CELL_RENDERER_FOCUSED)
931 state |= wxDATAVIEW_CELL_FOCUSED;
932 cell->Render( renderrect, dc, state );
933 }
934 }
935
936 static gboolean
937 gtk_wx_cell_renderer_activate(
938 GtkCellRenderer *renderer,
939 GdkEvent *event,
940 GtkWidget *widget,
941 const gchar *path,
942 GdkRectangle *background_area,
943 GdkRectangle *cell_area,
944 GtkCellRendererState flags )
945 {
946 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
947 wxDataViewCustomRenderer *cell = wxrenderer->cell;
948
949 GdkRectangle rect;
950 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
951 &rect.x,
952 &rect.y,
953 &rect.width,
954 &rect.height);
955
956 rect.x += cell_area->x;
957 rect.y += cell_area->y;
958 rect.width -= renderer->xpad * 2;
959 rect.height -= renderer->ypad * 2;
960
961 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
962
963 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
964
965 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
966 // TODO
967 wxDataViewItem item;
968 gtk_tree_path_free( treepath );
969
970 unsigned int model_col = cell->GetOwner()->GetModelColumn();
971
972 if (!event)
973 {
974 bool ret = false;
975
976 // activated by <ENTER>
977 if (cell->Activate( renderrect, model, item, model_col ))
978 ret = true;
979
980 return ret;
981 }
982 else if (event->type == GDK_BUTTON_PRESS)
983 {
984 GdkEventButton *button_event = (GdkEventButton*) event;
985 wxPoint pt( ((int) button_event->x) - renderrect.x,
986 ((int) button_event->y) - renderrect.y );
987
988 bool ret = false;
989 if (button_event->button == 1)
990 {
991 if (cell->LeftClick( pt, renderrect, model, item, model_col ))
992 ret = true;
993 // TODO: query system double-click time
994 if (button_event->time - wxrenderer->last_click < 400)
995 if (cell->Activate( renderrect, model, item, model_col ))
996 ret = true;
997 }
998 if (button_event->button == 3)
999 {
1000 if (cell->RightClick( pt, renderrect, model, item, model_col ))
1001 ret = true;
1002 }
1003
1004 wxrenderer->last_click = button_event->time;
1005
1006 return ret;
1007 }
1008
1009 return false;
1010 }
1011
1012 // ---------------------------------------------------------
1013 // wxGtkDataViewModelNotifier
1014 // ---------------------------------------------------------
1015
1016 class wxGtkDataViewModelNotifier: public wxDataViewModelNotifier
1017 {
1018 public:
1019 wxGtkDataViewModelNotifier( GtkWxTreeModel *wxgtk_model,
1020 wxDataViewModel *wx_model,
1021 wxDataViewCtrl *ctrl );
1022 ~wxGtkDataViewModelNotifier();
1023
1024 virtual bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
1025 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
1026 virtual bool ItemChanged( const wxDataViewItem &item );
1027 virtual bool ValueChanged( const wxDataViewItem &item, unsigned int col );
1028 virtual bool Cleared();
1029 virtual void Resort();
1030
1031 GtkWxTreeModel *m_wxgtk_model;
1032 wxDataViewModel *m_wx_model;
1033 wxDataViewCtrl *m_owner;
1034 };
1035
1036 // ---------------------------------------------------------
1037 // wxGtkDataViewListModelNotifier
1038 // ---------------------------------------------------------
1039
1040 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1041 GtkWxTreeModel* wxgtk_model, wxDataViewModel *wx_model,
1042 wxDataViewCtrl *ctrl )
1043 {
1044 m_wxgtk_model = wxgtk_model;
1045 m_wx_model = wx_model;
1046 m_owner = ctrl;
1047 }
1048
1049 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1050 {
1051 m_wx_model = NULL;
1052 m_wxgtk_model = NULL;
1053 }
1054
1055 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
1056 {
1057 m_owner->GtkGetInternal()->ItemAdded( parent, item );
1058
1059 GtkTreeIter iter;
1060 iter.stamp = m_wxgtk_model->stamp;
1061 iter.user_data = (gpointer) item.GetID();
1062
1063 GtkTreePath *path = wxgtk_tree_model_get_path(
1064 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1065 gtk_tree_model_row_inserted(
1066 GTK_TREE_MODEL(m_wxgtk_model), path, &iter);
1067 gtk_tree_path_free (path);
1068
1069 return true;
1070 }
1071
1072 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
1073 {
1074 GtkTreeIter iter;
1075 iter.stamp = m_wxgtk_model->stamp;
1076 iter.user_data = (gpointer) item.GetID();
1077
1078 GtkTreePath *path = wxgtk_tree_model_get_path_safe(
1079 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1080 gtk_tree_model_row_deleted(
1081 GTK_TREE_MODEL(m_wxgtk_model), path );
1082 gtk_tree_path_free (path);
1083
1084 m_owner->GtkGetInternal()->ItemDeleted( parent, item );
1085
1086 return true;
1087 }
1088
1089 void wxGtkDataViewModelNotifier::Resort()
1090 {
1091 m_owner->GtkGetInternal()->Resort();
1092 }
1093
1094 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem &item )
1095 {
1096 GtkTreeIter iter;
1097 iter.stamp = m_wxgtk_model->stamp;
1098 iter.user_data = (gpointer) item.GetID();
1099
1100 GtkTreePath *path = wxgtk_tree_model_get_path(
1101 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1102 gtk_tree_model_row_changed(
1103 GTK_TREE_MODEL(m_wxgtk_model), path, &iter );
1104 gtk_tree_path_free (path);
1105
1106 m_owner->GtkGetInternal()->ItemChanged( item );
1107
1108 return true;
1109 }
1110
1111 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem &item, unsigned int model_col )
1112 {
1113 // This adds GTK+'s missing MVC logic for ValueChanged
1114 unsigned int index;
1115 for (index = 0; index < m_owner->GetColumnCount(); index++)
1116 {
1117 wxDataViewColumn *column = m_owner->GetColumn( index );
1118 if (column->GetModelColumn() == model_col)
1119 {
1120 GtkTreeView *widget = GTK_TREE_VIEW(m_owner->m_treeview);
1121 GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
1122
1123 // Get cell area
1124 GtkTreeIter iter;
1125 iter.stamp = m_wxgtk_model->stamp;
1126 iter.user_data = (gpointer) item.GetID();
1127 GtkTreePath *path = wxgtk_tree_model_get_path(
1128 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1129 GdkRectangle cell_area;
1130 gtk_tree_view_get_cell_area( widget, path, gcolumn, &cell_area );
1131 gtk_tree_path_free( path );
1132
1133 GtkAdjustment* hadjust = gtk_tree_view_get_hadjustment( widget );
1134 double d = gtk_adjustment_get_value( hadjust );
1135 int xdiff = (int) d;
1136
1137 int ydiff = gcolumn->button->allocation.height;
1138 // Redraw
1139 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
1140 cell_area.x - xdiff, ydiff + cell_area.y, cell_area.width, cell_area.height );
1141
1142 m_owner->GtkGetInternal()->ValueChanged( item, model_col );
1143
1144 return true;
1145 }
1146 }
1147
1148 return false;
1149 }
1150
1151 bool wxGtkDataViewModelNotifier::Cleared()
1152 {
1153 // TODO: delete everything
1154
1155 m_owner->GtkGetInternal()->Cleared();
1156
1157 return false;
1158 }
1159
1160 // ---------------------------------------------------------
1161 // wxDataViewRenderer
1162 // ---------------------------------------------------------
1163
1164 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
1165
1166 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1167 int align ) :
1168 wxDataViewRendererBase( varianttype, mode, align )
1169 {
1170 m_renderer = NULL;
1171
1172 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1173 // after the m_renderer pointer has been initialized
1174 }
1175
1176 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
1177 {
1178 GtkCellRendererMode gtkMode;
1179 switch (mode)
1180 {
1181 case wxDATAVIEW_CELL_INERT:
1182 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
1183 break;
1184 case wxDATAVIEW_CELL_ACTIVATABLE:
1185 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
1186 break;
1187 case wxDATAVIEW_CELL_EDITABLE:
1188 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
1189 break;
1190 }
1191
1192 GValue gvalue = { 0, };
1193 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1194 g_value_set_enum( &gvalue, gtkMode );
1195 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1196 g_value_unset( &gvalue );
1197 }
1198
1199 wxDataViewCellMode wxDataViewRenderer::GetMode() const
1200 {
1201 wxDataViewCellMode ret;
1202
1203 GValue gvalue;
1204 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1205
1206 switch (g_value_get_enum(&gvalue))
1207 {
1208 case GTK_CELL_RENDERER_MODE_INERT:
1209 ret = wxDATAVIEW_CELL_INERT;
1210 break;
1211 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1212 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1213 break;
1214 case GTK_CELL_RENDERER_MODE_EDITABLE:
1215 ret = wxDATAVIEW_CELL_EDITABLE;
1216 break;
1217 }
1218
1219 g_value_unset( &gvalue );
1220
1221 return ret;
1222 }
1223
1224 void wxDataViewRenderer::SetAlignment( int align )
1225 {
1226 // horizontal alignment:
1227
1228 gfloat xalign = 0.0;
1229 if (align & wxALIGN_RIGHT)
1230 xalign = 1.0;
1231 else if (align & wxALIGN_CENTER_HORIZONTAL)
1232 xalign = 0.5;
1233
1234 GValue gvalue = { 0, };
1235 g_value_init( &gvalue, G_TYPE_FLOAT );
1236 g_value_set_float( &gvalue, xalign );
1237 g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue );
1238 g_value_unset( &gvalue );
1239
1240 // vertical alignment:
1241
1242 gfloat yalign = 0.0;
1243 if (align & wxALIGN_BOTTOM)
1244 yalign = 1.0;
1245 else if (align & wxALIGN_CENTER_VERTICAL)
1246 yalign = 0.5;
1247
1248 GValue gvalue2 = { 0, };
1249 g_value_init( &gvalue2, G_TYPE_FLOAT );
1250 g_value_set_float( &gvalue2, yalign );
1251 g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 );
1252 g_value_unset( &gvalue2 );
1253 }
1254
1255 int wxDataViewRenderer::GetAlignment() const
1256 {
1257 int ret = 0;
1258 GValue gvalue;
1259
1260 // horizontal alignment:
1261
1262 g_object_get( G_OBJECT(m_renderer), "xalign", &gvalue, NULL );
1263 float xalign = g_value_get_float( &gvalue );
1264 if (xalign < 0.5)
1265 ret |= wxALIGN_LEFT;
1266 else if (xalign == 0.5)
1267 ret |= wxALIGN_CENTER_HORIZONTAL;
1268 else
1269 ret |= wxALIGN_RIGHT;
1270 g_value_unset( &gvalue );
1271
1272
1273 // vertical alignment:
1274
1275 g_object_get( G_OBJECT(m_renderer), "yalign", &gvalue, NULL );
1276 float yalign = g_value_get_float( &gvalue );
1277 if (yalign < 0.5)
1278 ret |= wxALIGN_TOP;
1279 else if (yalign == 0.5)
1280 ret |= wxALIGN_CENTER_VERTICAL;
1281 else
1282 ret |= wxALIGN_BOTTOM;
1283 g_value_unset( &gvalue );
1284
1285 return ret;
1286 }
1287
1288
1289
1290 // ---------------------------------------------------------
1291 // wxDataViewTextRenderer
1292 // ---------------------------------------------------------
1293
1294 extern "C" {
1295 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
1296 gchar *arg1, gchar *arg2, gpointer user_data );
1297 }
1298
1299 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
1300 gchar *arg1, gchar *arg2, gpointer user_data )
1301 {
1302 wxDataViewTextRenderer *cell = (wxDataViewTextRenderer*) user_data;
1303
1304 wxString tmp = wxGTK_CONV_BACK_FONT(arg2, cell->GetOwner()->GetOwner()->GetFont());
1305 wxVariant value = tmp;
1306 if (!cell->Validate( value ))
1307 return;
1308
1309 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
1310
1311 GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
1312 GtkTreeIter iter;
1313 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, path );
1314 wxDataViewItem item( (void*) iter.user_data );;
1315 gtk_tree_path_free( path );
1316
1317 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1318
1319 model->SetValue( value, item, model_col );
1320 model->ValueChanged( item, model_col );
1321 }
1322
1323 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
1324
1325 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1326 int align ) :
1327 wxDataViewRenderer( varianttype, mode, align )
1328 {
1329 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_text_new();
1330
1331 if (mode & wxDATAVIEW_CELL_EDITABLE)
1332 {
1333 GValue gvalue = { 0, };
1334 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1335 g_value_set_boolean( &gvalue, true );
1336 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
1337 g_value_unset( &gvalue );
1338
1339 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
1340 }
1341
1342 SetMode(mode);
1343 SetAlignment(align);
1344 }
1345
1346 bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
1347 {
1348 wxString tmp = value;
1349
1350 GValue gvalue = { 0, };
1351 g_value_init( &gvalue, G_TYPE_STRING );
1352 g_value_set_string( &gvalue, wxGTK_CONV_FONT( tmp, GetOwner()->GetOwner()->GetFont() ) );
1353 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1354 g_value_unset( &gvalue );
1355
1356 return true;
1357 }
1358
1359 bool wxDataViewTextRenderer::GetValue( wxVariant &value ) const
1360 {
1361 GValue gvalue = { 0, };
1362 g_value_init( &gvalue, G_TYPE_STRING );
1363 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
1364 wxString tmp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ),
1365 wx_const_cast(wxDataViewTextRenderer*, this)->GetOwner()->GetOwner()->GetFont() );
1366 g_value_unset( &gvalue );
1367
1368 value = tmp;
1369
1370 return true;
1371 }
1372
1373 void wxDataViewTextRenderer::SetAlignment( int align )
1374 {
1375 wxDataViewRenderer::SetAlignment(align);
1376
1377 if (gtk_check_version(2,10,0))
1378 return;
1379
1380 // horizontal alignment:
1381 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
1382 if (align & wxALIGN_RIGHT)
1383 pangoAlign = PANGO_ALIGN_RIGHT;
1384 else if (align & wxALIGN_CENTER_HORIZONTAL)
1385 pangoAlign = PANGO_ALIGN_CENTER;
1386
1387 GValue gvalue = { 0, };
1388 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1389 g_value_set_enum( &gvalue, pangoAlign );
1390 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
1391 g_value_unset( &gvalue );
1392 }
1393
1394 // ---------------------------------------------------------
1395 // wxDataViewBitmapRenderer
1396 // ---------------------------------------------------------
1397
1398 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
1399
1400 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1401 int align ) :
1402 wxDataViewRenderer( varianttype, mode, align )
1403 {
1404 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_pixbuf_new();
1405
1406 SetMode(mode);
1407 SetAlignment(align);
1408 }
1409
1410 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
1411 {
1412 if (value.GetType() == wxT("wxBitmap"))
1413 {
1414 wxBitmap bitmap;
1415 bitmap << value;
1416
1417 // This may create a Pixbuf representation in the
1418 // wxBitmap object (and it will stay there)
1419 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
1420
1421 GValue gvalue = { 0, };
1422 g_value_init( &gvalue, G_TYPE_OBJECT );
1423 g_value_set_object( &gvalue, pixbuf );
1424 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1425 g_value_unset( &gvalue );
1426
1427 return true;
1428 }
1429
1430 if (value.GetType() == wxT("wxIcon"))
1431 {
1432 wxIcon bitmap;
1433 bitmap << value;
1434
1435 // This may create a Pixbuf representation in the
1436 // wxBitmap object (and it will stay there)
1437 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
1438
1439 GValue gvalue = { 0, };
1440 g_value_init( &gvalue, G_TYPE_OBJECT );
1441 g_value_set_object( &gvalue, pixbuf );
1442 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1443 g_value_unset( &gvalue );
1444
1445 return true;
1446 }
1447
1448 return false;
1449 }
1450
1451 bool wxDataViewBitmapRenderer::GetValue( wxVariant &value ) const
1452 {
1453 return false;
1454 }
1455
1456 // ---------------------------------------------------------
1457 // wxDataViewToggleRenderer
1458 // ---------------------------------------------------------
1459
1460 extern "C" {
1461 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
1462 gchar *path, gpointer user_data );
1463 }
1464
1465 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
1466 gchar *path, gpointer user_data )
1467 {
1468 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
1469
1470 // get old value
1471 GValue gvalue = { 0, };
1472 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1473 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
1474 bool tmp = g_value_get_boolean( &gvalue );
1475 g_value_unset( &gvalue );
1476 // invert it
1477 tmp = !tmp;
1478
1479 wxVariant value = tmp;
1480 if (!cell->Validate( value ))
1481 return;
1482
1483 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
1484
1485 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
1486 GtkTreeIter iter;
1487 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, gtk_path );
1488 wxDataViewItem item( (void*) iter.user_data );;
1489 gtk_tree_path_free( gtk_path );
1490
1491 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1492
1493 model->SetValue( value, item, model_col );
1494 model->ValueChanged( item, model_col );
1495 }
1496
1497 IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
1498
1499 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
1500 wxDataViewCellMode mode, int align ) :
1501 wxDataViewRenderer( varianttype, mode, align )
1502 {
1503 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
1504
1505 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
1506 {
1507 g_signal_connect_after( m_renderer, "toggled",
1508 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
1509 }
1510 else
1511 {
1512 GValue gvalue = { 0, };
1513 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1514 g_value_set_boolean( &gvalue, false );
1515 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
1516 g_value_unset( &gvalue );
1517 }
1518
1519 SetMode(mode);
1520 SetAlignment(align);
1521 }
1522
1523 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
1524 {
1525 bool tmp = value;
1526
1527 GValue gvalue = { 0, };
1528 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1529 g_value_set_boolean( &gvalue, tmp );
1530 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
1531 g_value_unset( &gvalue );
1532
1533 return true;
1534 }
1535
1536 bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
1537 {
1538 GValue gvalue = { 0, };
1539 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1540 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
1541 bool tmp = g_value_get_boolean( &gvalue );
1542 g_value_unset( &gvalue );
1543
1544 value = tmp;
1545
1546 return true;
1547 }
1548
1549 // ---------------------------------------------------------
1550 // wxDataViewCustomRenderer
1551 // ---------------------------------------------------------
1552
1553 class wxDataViewCtrlDC: public wxWindowDC
1554 {
1555 public:
1556 wxDataViewCtrlDC( wxDataViewCtrl *window )
1557 {
1558 GtkWidget *widget = window->m_treeview;
1559 // Set later
1560 m_window = NULL;
1561
1562 m_context = window->GtkGetPangoDefaultContext();
1563 m_layout = pango_layout_new( m_context );
1564 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
1565
1566 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
1567
1568 // Set m_window later
1569 // SetUpDC();
1570 // m_owner = window;
1571 }
1572 };
1573
1574 // ---------------------------------------------------------
1575 // wxDataViewCustomRenderer
1576 // ---------------------------------------------------------
1577
1578 IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
1579
1580 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
1581 wxDataViewCellMode mode, int align,
1582 bool no_init ) :
1583 wxDataViewRenderer( varianttype, mode, align )
1584 {
1585 m_dc = NULL;
1586
1587 if (no_init)
1588 m_renderer = NULL;
1589 else
1590 Init(mode, align);
1591 }
1592
1593 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
1594 {
1595 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
1596 renderer->cell = this;
1597
1598 m_renderer = (GtkCellRenderer*) renderer;
1599
1600 SetMode(mode);
1601 SetAlignment(align);
1602
1603 return true;
1604 }
1605
1606 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1607 {
1608 if (m_dc)
1609 delete m_dc;
1610 }
1611
1612 wxDC *wxDataViewCustomRenderer::GetDC()
1613 {
1614 if (m_dc == NULL)
1615 {
1616 if (GetOwner() == NULL)
1617 return NULL;
1618 if (GetOwner()->GetOwner() == NULL)
1619 return NULL;
1620 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1621 }
1622
1623 return m_dc;
1624 }
1625
1626 // ---------------------------------------------------------
1627 // wxDataViewProgressRenderer
1628 // ---------------------------------------------------------
1629
1630 IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
1631
1632 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
1633 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
1634 wxDataViewCustomRenderer( varianttype, mode, align, true )
1635 {
1636 m_label = label;
1637 m_value = 0;
1638
1639 #ifdef __WXGTK26__
1640 if (!gtk_check_version(2,6,0))
1641 {
1642 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
1643
1644 GValue gvalue = { 0, };
1645 g_value_init( &gvalue, G_TYPE_STRING );
1646
1647 // FIXME: font encoding support
1648 g_value_set_string( &gvalue, wxGTK_CONV_SYS(m_label) );
1649 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1650 g_value_unset( &gvalue );
1651
1652 SetMode(mode);
1653 SetAlignment(align);
1654 }
1655 else
1656 #endif
1657 {
1658 // Use custom cell code
1659 wxDataViewCustomRenderer::Init(mode, align);
1660 }
1661 }
1662
1663 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1664 {
1665 }
1666
1667 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
1668 {
1669 #ifdef __WXGTK26__
1670 if (!gtk_check_version(2,6,0))
1671 {
1672 gint tmp = (long) value;
1673 GValue gvalue = { 0, };
1674 g_value_init( &gvalue, G_TYPE_INT );
1675 g_value_set_int( &gvalue, tmp );
1676 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
1677 g_value_unset( &gvalue );
1678 }
1679 else
1680 #endif
1681 {
1682 m_value = (long) value;
1683
1684 if (m_value < 0) m_value = 0;
1685 if (m_value > 100) m_value = 100;
1686 }
1687
1688 return true;
1689 }
1690
1691 bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const
1692 {
1693 return false;
1694 }
1695
1696 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int state )
1697 {
1698 double pct = (double)m_value / 100.0;
1699 wxRect bar = cell;
1700 bar.width = (int)(cell.width * pct);
1701 dc->SetPen( *wxTRANSPARENT_PEN );
1702 dc->SetBrush( *wxBLUE_BRUSH );
1703 dc->DrawRectangle( bar );
1704
1705 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1706 dc->SetPen( *wxBLACK_PEN );
1707 dc->DrawRectangle( cell );
1708
1709 return true;
1710 }
1711
1712 wxSize wxDataViewProgressRenderer::GetSize() const
1713 {
1714 return wxSize(40,12);
1715 }
1716
1717 // ---------------------------------------------------------
1718 // wxDataViewDateRenderer
1719 // ---------------------------------------------------------
1720
1721 class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
1722 {
1723 public:
1724 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
1725 wxDataViewModel *model, const wxDataViewItem &item, unsigned int col ) :
1726 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
1727 {
1728 m_model = model;
1729 m_item = item;
1730 m_col = col;
1731 m_cal = new wxCalendarCtrl( this, -1, *value );
1732 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
1733 sizer->Add( m_cal, 1, wxGROW );
1734 SetSizer( sizer );
1735 sizer->Fit( this );
1736 }
1737
1738 virtual void OnDismiss()
1739 {
1740 }
1741
1742 void OnCalendar( wxCalendarEvent &event );
1743
1744 wxCalendarCtrl *m_cal;
1745 wxDataViewModel *m_model;
1746 wxDataViewItem m_item;
1747 unsigned int m_col;
1748
1749 private:
1750 DECLARE_EVENT_TABLE()
1751 };
1752
1753 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
1754 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
1755 END_EVENT_TABLE()
1756
1757 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
1758 {
1759 wxDateTime date = event.GetDate();
1760 wxVariant value = date;
1761 m_model->SetValue( value, m_item, m_col );
1762 m_model->ValueChanged( m_item, m_col );
1763 DismissAndNotify();
1764 }
1765
1766 IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
1767
1768 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
1769 wxDataViewCellMode mode, int align ) :
1770 wxDataViewCustomRenderer( varianttype, mode, align )
1771 {
1772 SetMode(mode);
1773 SetAlignment(align);
1774 }
1775
1776 bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
1777 {
1778 m_date = value.GetDateTime();
1779
1780 return true;
1781 }
1782
1783 bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const
1784 {
1785 return false;
1786 }
1787
1788 bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
1789 {
1790 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
1791 wxString tmp = m_date.FormatDate();
1792 dc->DrawText( tmp, cell.x, cell.y );
1793
1794 return true;
1795 }
1796
1797 wxSize wxDataViewDateRenderer::GetSize() const
1798 {
1799 wxString tmp = m_date.FormatDate();
1800 wxCoord x,y,d;
1801 GetView()->GetTextExtent( tmp, &x, &y, &d );
1802 return wxSize(x,y+d);
1803 }
1804
1805 bool wxDataViewDateRenderer::Activate( wxRect cell, wxDataViewModel *model,
1806 const wxDataViewItem &item, unsigned int col )
1807 {
1808 wxVariant variant;
1809 model->GetValue( variant, item, col );
1810 wxDateTime value = variant.GetDateTime();
1811
1812 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
1813 GetOwner()->GetOwner()->GetParent(), &value, model, item, col );
1814 wxPoint pos = wxGetMousePosition();
1815 popup->Move( pos );
1816 popup->Layout();
1817 popup->Popup( popup->m_cal );
1818
1819 return true;
1820 }
1821
1822 // ---------------------------------------------------------
1823 // wxDataViewColumn
1824 // ---------------------------------------------------------
1825
1826
1827 static gboolean
1828 gtk_dataview_header_button_press_callback( GtkWidget *widget,
1829 GdkEventButton *gdk_event,
1830 wxDataViewColumn *column )
1831 {
1832 if (gdk_event->type != GDK_BUTTON_PRESS)
1833 return FALSE;
1834
1835 if (gdk_event->button == 1)
1836 {
1837 wxDataViewCtrl *dv = column->GetOwner();
1838 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
1839 event.SetDataViewColumn( column );
1840 event.SetModel( dv->GetModel() );
1841 if (dv->GetEventHandler()->ProcessEvent( event ))
1842 return TRUE;
1843 }
1844
1845 return FALSE;
1846 }
1847
1848 extern "C" {
1849 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1850 GtkCellRenderer *cell,
1851 GtkTreeModel *model,
1852 GtkTreeIter *iter,
1853 gpointer data );
1854 }
1855
1856
1857 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1858 GtkCellRenderer *renderer,
1859 GtkTreeModel *model,
1860 GtkTreeIter *iter,
1861 gpointer data )
1862 {
1863 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
1864 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
1865
1866 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
1867
1868 wxDataViewItem item( (void*) iter->user_data );
1869
1870 wxVariant value;
1871 tree_model->internal->GetDataViewModel()->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
1872
1873 if (value.GetType() != cell->GetVariantType())
1874 wxLogError( wxT("Wrong type, required: %s but: %s"),
1875 value.GetType().c_str(),
1876 cell->GetVariantType().c_str() );
1877
1878 cell->SetValue( value );
1879
1880 #if 0
1881 wxListItemAttr attr;
1882 tree_model->model->GetAttr( attr, cell->GetOwner()->GetModelColumn(), model_row );
1883
1884 if (attr.HasBackgroundColour())
1885 {
1886 wxColour colour = attr.GetBackgroundColour();
1887 const GdkColor * const gcol = colour.GetColor();
1888
1889 GValue gvalue = { 0, };
1890 g_value_init( &gvalue, GDK_TYPE_COLOR );
1891 g_value_set_boxed( &gvalue, gcol );
1892 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
1893 g_value_unset( &gvalue );
1894 }
1895 else
1896 {
1897 GValue gvalue = { 0, };
1898 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1899 g_value_set_boolean( &gvalue, FALSE );
1900 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
1901 g_value_unset( &gvalue );
1902 }
1903 #endif
1904
1905 }
1906
1907 IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
1908
1909 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
1910 unsigned int model_column, int width,
1911 wxAlignment align, int flags ) :
1912 wxDataViewColumnBase( title, cell, model_column, width, align, flags )
1913 {
1914 Init( align, flags, width );
1915
1916 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column), TRUE );
1917 SetTitle( title );
1918 }
1919
1920 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
1921 unsigned int model_column, int width,
1922 wxAlignment align, int flags ) :
1923 wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags )
1924 {
1925 Init( align, flags, width );
1926
1927 SetBitmap( bitmap );
1928 }
1929
1930 void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
1931 {
1932 m_isConnected = false;
1933
1934 GtkCellRenderer *renderer = (GtkCellRenderer *) GetRenderer()->GetGtkHandle();
1935 GtkTreeViewColumn *column = gtk_tree_view_column_new();
1936 m_column = (GtkWidget*) column;
1937
1938 SetFlags( flags );
1939 SetAlignment( align );
1940
1941 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
1942 // as GTK+ is smart and unless explicitely told, will set the minimal
1943 // width to the title's lenght, which is a better default
1944
1945 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
1946 // that we use for the wxDataViewCtrl
1947 gtk_tree_view_column_set_fixed_width( column, width < 0 ? wxDVC_DEFAULT_WIDTH : width );
1948 gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED );
1949
1950 gtk_tree_view_column_pack_end( column, renderer, TRUE );
1951
1952 gtk_tree_view_column_set_cell_data_func( column, renderer,
1953 wxGtkTreeCellDataFunc, (gpointer) GetRenderer(), NULL );
1954 }
1955
1956 wxDataViewColumn::~wxDataViewColumn()
1957 {
1958 }
1959
1960 void wxDataViewColumn::OnInternalIdle()
1961 {
1962 if (m_isConnected)
1963 return;
1964
1965 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
1966 {
1967 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1968 if (column->button)
1969 {
1970 g_signal_connect(column->button, "button_press_event",
1971 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
1972
1973 m_isConnected = true;
1974 }
1975 }
1976 }
1977
1978 void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
1979 {
1980 wxDataViewColumnBase::SetOwner( owner );
1981
1982 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1983
1984 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
1985 }
1986
1987 void wxDataViewColumn::SetTitle( const wxString &title )
1988 {
1989 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1990
1991 if (m_isConnected)
1992 {
1993 // disconnect before column->button gets recreated
1994 g_signal_handlers_disconnect_by_func( column->button,
1995 (GtkWidget*) gtk_dataview_header_button_press_callback, this);
1996
1997 m_isConnected = false;
1998 }
1999
2000 // FIXME: can it really happen that we don't have the owner here??
2001 wxDataViewCtrl *ctrl = GetOwner();
2002 gtk_tree_view_column_set_title( column, ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
2003 : wxGTK_CONV_SYS(title) );
2004
2005 gtk_tree_view_column_set_widget( column, NULL );
2006 }
2007
2008 wxString wxDataViewColumn::GetTitle() const
2009 {
2010 const gchar *str = gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column) );
2011 return wxConvFileName->cMB2WX(str);
2012 }
2013
2014 void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
2015 {
2016 wxDataViewColumnBase::SetBitmap( bitmap );
2017
2018 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2019 if (bitmap.Ok())
2020 {
2021 GtkImage *gtk_image = GTK_IMAGE( gtk_image_new() );
2022
2023 GdkBitmap *mask = (GdkBitmap *) NULL;
2024 if (bitmap.GetMask())
2025 mask = bitmap.GetMask()->GetBitmap();
2026
2027 if (bitmap.HasPixbuf())
2028 {
2029 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
2030 bitmap.GetPixbuf());
2031 }
2032 else
2033 {
2034 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
2035 bitmap.GetPixmap(), mask);
2036 }
2037 gtk_widget_show( GTK_WIDGET(gtk_image) );
2038
2039 gtk_tree_view_column_set_widget( column, GTK_WIDGET(gtk_image) );
2040 }
2041 else
2042 {
2043 gtk_tree_view_column_set_widget( column, NULL );
2044 }
2045 }
2046
2047 void wxDataViewColumn::SetHidden( bool hidden )
2048 {
2049 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
2050 }
2051
2052 void wxDataViewColumn::SetResizeable( bool resizeable )
2053 {
2054 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
2055 }
2056
2057 void wxDataViewColumn::SetAlignment( wxAlignment align )
2058 {
2059 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2060
2061 gfloat xalign = 0.0;
2062 if (align == wxALIGN_RIGHT)
2063 xalign = 1.0;
2064 if (align == wxALIGN_CENTER_HORIZONTAL ||
2065 align == wxALIGN_CENTER)
2066 xalign = 0.5;
2067
2068 gtk_tree_view_column_set_alignment( column, xalign );
2069 }
2070
2071 wxAlignment wxDataViewColumn::GetAlignment() const
2072 {
2073 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
2074
2075 if (xalign == 1.0)
2076 return wxALIGN_RIGHT;
2077 if (xalign == 0.5)
2078 return wxALIGN_CENTER_HORIZONTAL;
2079
2080 return wxALIGN_LEFT;
2081 }
2082
2083 void wxDataViewColumn::SetSortable( bool sortable )
2084 {
2085 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2086
2087 if (sortable)
2088 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
2089 else
2090 gtk_tree_view_column_set_sort_column_id( column, -1 );
2091 }
2092
2093 bool wxDataViewColumn::IsSortable() const
2094 {
2095 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2096 return (gtk_tree_view_column_get_sort_column_id( column ) != -1);
2097 }
2098
2099 bool wxDataViewColumn::IsResizeable() const
2100 {
2101 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2102 return gtk_tree_view_column_get_resizable( column );
2103 }
2104
2105 bool wxDataViewColumn::IsHidden() const
2106 {
2107 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2108 return !gtk_tree_view_column_get_visible( column );
2109 }
2110
2111 void wxDataViewColumn::SetSortOrder( bool ascending )
2112 {
2113 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2114
2115 if (ascending)
2116 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
2117 else
2118 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
2119 }
2120
2121 bool wxDataViewColumn::IsSortOrderAscending() const
2122 {
2123 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2124
2125 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
2126 }
2127
2128 void wxDataViewColumn::SetMinWidth( int width )
2129 {
2130 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
2131 }
2132
2133 int wxDataViewColumn::GetMinWidth() const
2134 {
2135 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
2136 }
2137
2138 int wxDataViewColumn::GetWidth() const
2139 {
2140 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
2141 }
2142
2143 void wxDataViewColumn::SetWidth( int width )
2144 {
2145 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
2146 }
2147
2148
2149 //-----------------------------------------------------------------------------
2150 // wxGtkTreeModelNode
2151 //-----------------------------------------------------------------------------
2152
2153 void wxGtkTreeModelNode::Resort()
2154 {
2155 g_model = m_internal->GetDataViewModel();
2156
2157 size_t child_count = GetChildCount();
2158 if (child_count == 0)
2159 return;
2160
2161 size_t node_count = GetNodesCount();
2162
2163 if (child_count == 1)
2164 {
2165 if (node_count == 1)
2166 {
2167 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
2168 node->Resort();
2169 }
2170 return;
2171 }
2172
2173 wxGtkTreeModelChildren *new_array = new wxGtkTreeModelChildren( wxGtkTreeModelNodeCmp );
2174
2175 size_t pos;
2176 for (pos = 0; pos < child_count; pos++)
2177 new_array->Add( m_children->Item( pos ) );
2178
2179 gint *new_order = new gint[child_count];
2180
2181 for (pos = 0; pos < child_count; pos++)
2182 {
2183 void *id = new_array->Item( pos );
2184 size_t old_pos;
2185 for (old_pos = 0; old_pos < child_count; old_pos++)
2186 {
2187 if (id == m_children->Item(old_pos))
2188 {
2189 new_order[pos] = old_pos;
2190 break;
2191 }
2192 }
2193 }
2194
2195 // for (pos = 0; pos < count; pos++)
2196 // m_children->Clear();
2197 delete m_children;
2198
2199 m_children = new_array;
2200
2201 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
2202
2203 GtkTreePath *path = gtk_tree_path_new ();
2204 wxGtkTreeModelNode *parent = GetParent();
2205 void *id = GetItem().GetID();
2206
2207 while (parent)
2208 {
2209 int pos = parent->GetChildren().Index( id );
2210 gtk_tree_path_prepend_index( path, pos );
2211 id = parent->GetItem().GetID();
2212 parent = parent->GetParent();
2213 }
2214
2215 GtkTreeIter iter;
2216 iter.user_data = id;
2217 iter.stamp = m_internal->GetGtkModel()->stamp;
2218 gtk_tree_model_rows_reordered( gtk_tree_model, path, &iter, new_order );
2219
2220 gtk_tree_path_free (path);
2221
2222 delete [] new_order;
2223
2224 for (pos = 0; pos < node_count; pos++)
2225 {
2226 wxGtkTreeModelNode *node = m_nodes.Item( pos );
2227 node->Resort();
2228 }
2229 }
2230
2231 //-----------------------------------------------------------------------------
2232 // wxDataViewCtrlInternal
2233 //-----------------------------------------------------------------------------
2234
2235 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner,
2236 wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model )
2237 {
2238 m_owner = owner;
2239 m_wx_model = wx_model;
2240 m_gtk_model = gtk_model;
2241 m_root = NULL;
2242 InitTree();
2243 }
2244
2245 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2246 {
2247 g_object_unref( m_gtk_model );
2248 }
2249
2250 void wxDataViewCtrlInternal::InitTree()
2251 {
2252 wxDataViewItem item;
2253 m_root = new wxGtkTreeModelNode( NULL, item, this );
2254
2255 BuildBranch( m_root );
2256 }
2257
2258 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
2259 {
2260 if (node->GetChildCount() == 0)
2261 {
2262 wxDataViewItem child = m_wx_model->GetFirstChild( node->GetItem() );
2263 while (child.IsOk())
2264 {
2265 if (m_wx_model->IsContainer( child ))
2266 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
2267 else
2268 node->AddLeave( child.GetID() );
2269
2270 // Don't send any events here
2271
2272 child = m_wx_model->GetNextSibling( child );
2273 }
2274 }
2275 }
2276
2277 void wxDataViewCtrlInternal::Resort()
2278 {
2279 m_root->Resort();
2280 }
2281
2282 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
2283 {
2284 wxGtkTreeModelNode *parent_node = FindNode( parent );
2285 if (m_wx_model->IsContainer( item ))
2286 parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) );
2287 else
2288 parent_node->AddLeave( item.GetID() );
2289
2290 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED, m_owner->GetId() );
2291 event.SetEventObject( m_owner );
2292 event.SetModel( m_owner->GetModel() );
2293 event.SetItem( item );
2294 m_owner->GetEventHandler()->ProcessEvent( event );
2295
2296 return true;
2297 }
2298
2299 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
2300 {
2301 wxGtkTreeModelNode *parent_node = FindNode( parent );
2302 parent_node->DeleteChild( item.GetID() );
2303
2304 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED, m_owner->GetId() );
2305 event.SetEventObject( m_owner );
2306 event.SetModel( m_owner->GetModel() );
2307 event.SetItem( item );
2308 m_owner->GetEventHandler()->ProcessEvent( event );
2309
2310 return true;
2311 }
2312
2313 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
2314 {
2315 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED, m_owner->GetId() );
2316 event.SetEventObject( m_owner );
2317 event.SetModel( m_owner->GetModel() );
2318 event.SetItem( item );
2319 m_owner->GetEventHandler()->ProcessEvent( event );
2320
2321 return true;
2322 }
2323
2324 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int col )
2325 {
2326 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED, m_owner->GetId() );
2327 event.SetEventObject( m_owner );
2328 event.SetModel( m_owner->GetModel() );
2329 event.SetColumn( col );
2330 event.SetItem( item );
2331 m_owner->GetEventHandler()->ProcessEvent( event );
2332
2333 return true;
2334 }
2335
2336 bool wxDataViewCtrlInternal::Cleared()
2337 {
2338 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED, m_owner->GetId() );
2339 event.SetEventObject( m_owner );
2340 event.SetModel( m_owner->GetModel() );
2341 m_owner->GetEventHandler()->ProcessEvent( event );
2342
2343 return true;
2344 }
2345
2346 gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
2347 {
2348 g_model = m_wx_model;
2349
2350 int depth = gtk_tree_path_get_depth( path );
2351
2352 wxGtkTreeModelNode *node = m_root;
2353
2354 int i;
2355 for (i = 0; i < depth; i++)
2356 {
2357 BuildBranch( node );
2358
2359 gint pos = gtk_tree_path_get_indices (path)[i];
2360 if (pos < 0) return FALSE;
2361 if ((size_t)pos >= node->GetChildCount()) return FALSE;
2362
2363 void* id = node->GetChildren().Item( (size_t) pos );
2364
2365 if (i == depth-1)
2366 {
2367 iter->stamp = m_gtk_model->stamp;
2368 iter->user_data = id;
2369 return TRUE;
2370 }
2371
2372 size_t count = node->GetNodes().GetCount();
2373 size_t pos2;
2374 for (pos2 = 0; pos2 < count; pos2++)
2375 {
2376 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
2377 if (child_node->GetItem().GetID() == id)
2378 {
2379 node = child_node;
2380 break;
2381 }
2382 }
2383 }
2384
2385 return FALSE;
2386 }
2387
2388 GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
2389 {
2390 g_model = m_wx_model;
2391
2392 GtkTreePath *retval = gtk_tree_path_new ();
2393 void *id = iter->user_data;
2394
2395 wxGtkTreeModelNode *node = FindParentNode( iter );
2396 while (node)
2397 {
2398 int pos = node->GetChildren().Index( id );
2399 gtk_tree_path_prepend_index( retval, pos );
2400
2401 id = node->GetItem().GetID();
2402 node = node->GetParent();
2403 }
2404
2405 return retval;
2406 }
2407
2408 GtkTreePath *wxDataViewCtrlInternal::get_path_safe( GtkTreeIter *iter )
2409 {
2410 g_model = m_wx_model;
2411
2412 GtkTreePath *retval = gtk_tree_path_new ();
2413 void *id = iter->user_data;
2414
2415 wxGtkTreeModelNode *node = FindParentNode( iter );
2416 while (node)
2417 {
2418 size_t pos;
2419 for (pos = 0; pos < node->GetChildren().GetCount(); pos++)
2420 {
2421 if (id == node->GetChildren().Item( pos ))
2422 {
2423 gtk_tree_path_prepend_index( retval, (int) pos );
2424 continue;
2425 }
2426 }
2427
2428 id = node->GetItem().GetID();
2429 node = node->GetParent();
2430 }
2431
2432 return retval;
2433 }
2434
2435 gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
2436 {
2437 g_model = m_wx_model;
2438
2439 wxGtkTreeModelNode *parent = FindParentNode( iter );
2440 if( parent == NULL )
2441 return FALSE;
2442
2443 unsigned int pos = parent->GetChildren().Index( iter->user_data );
2444
2445 if (pos == parent->GetChildCount()-1)
2446 return FALSE;
2447
2448 iter->stamp = m_gtk_model->stamp;
2449 iter->user_data = parent->GetChildren().Item( pos+1 );
2450
2451 return TRUE;
2452 }
2453
2454 gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
2455 {
2456 g_model = m_wx_model;
2457
2458 wxDataViewItem item( (void*) parent->user_data );
2459
2460 if (!m_wx_model->IsContainer( item ))
2461 return FALSE;
2462
2463 wxGtkTreeModelNode *parent_node = FindNode( parent );
2464 BuildBranch( parent_node );
2465
2466 if (parent_node->GetChildCount() == 0)
2467 return FALSE;
2468
2469 iter->stamp = m_gtk_model->stamp;
2470 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
2471
2472 return TRUE;
2473 }
2474
2475 gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
2476 {
2477 g_model = m_wx_model;
2478
2479 wxDataViewItem item( (void*) iter->user_data );
2480 bool is_container = m_wx_model->IsContainer( item );
2481
2482 if (!is_container)
2483 return FALSE;
2484
2485 wxGtkTreeModelNode *node = FindNode( iter );
2486 BuildBranch( node );
2487
2488 return (node->GetChildCount() > 0);
2489 }
2490
2491 gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
2492 {
2493 g_model = m_wx_model;
2494
2495 wxDataViewItem item( (void*) iter->user_data );
2496
2497 if (!m_wx_model->IsContainer( item ))
2498 return 0;
2499
2500 wxGtkTreeModelNode *parent_node = FindNode( iter );
2501 BuildBranch( parent_node );
2502
2503 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2504
2505 return parent_node->GetChildCount();
2506 }
2507
2508 gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
2509 {
2510 g_model = m_wx_model;
2511
2512 void* id = NULL;
2513 if (parent) id = (void*) parent->user_data;
2514 wxDataViewItem item( id );
2515
2516 if (!m_wx_model->IsContainer( item ))
2517 return FALSE;
2518
2519 wxGtkTreeModelNode *parent_node = FindNode( parent );
2520 BuildBranch( parent_node );
2521
2522 // wxPrintf( "iter_nth_child %d\n", n );
2523
2524 iter->stamp = m_gtk_model->stamp;
2525 iter->user_data = parent_node->GetChildren().Item( n );
2526
2527 return TRUE;
2528 }
2529
2530 gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
2531 {
2532 g_model = m_wx_model;
2533
2534 wxGtkTreeModelNode *node = FindParentNode( child );
2535 if (!node)
2536 return FALSE;
2537
2538 iter->stamp = m_gtk_model->stamp;
2539 iter->user_data = (gpointer) node->GetItem().GetID();
2540
2541 return TRUE;
2542 }
2543
2544 static wxGtkTreeModelNode*
2545 wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
2546 {
2547 if( model == NULL )
2548 return NULL;
2549
2550 ItemList list;
2551 list.DeleteContents( true );
2552 wxDataViewItem it( item );
2553 while( it.IsOk() )
2554 {
2555 wxDataViewItem * pItem = new wxDataViewItem( it );
2556 list.Insert( pItem );
2557 it = model->GetParent( it );
2558 }
2559
2560 wxGtkTreeModelNode * node = treeNode;
2561 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
2562 {
2563 if( node && node->GetNodes().GetCount() != 0 )
2564 {
2565 int len = node->GetNodes().GetCount();
2566 wxGtkTreeModelNodes nodes = node->GetNodes();
2567 int j = 0;
2568 for( ; j < len; j ++)
2569 {
2570 if( nodes[j]->GetItem() == *(n->GetData()))
2571 {
2572 node = nodes[j];
2573 break;
2574 }
2575 }
2576
2577 if( j == len )
2578 {
2579 return NULL;
2580 }
2581 }
2582 else
2583 return NULL;
2584 }
2585 return node;
2586
2587 }
2588
2589 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
2590 {
2591 if (!iter)
2592 return m_root;
2593
2594 wxDataViewItem item( (void*) iter->user_data );
2595 if (!item.IsOk())
2596 return m_root;
2597
2598 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
2599
2600 if (!result)
2601 {
2602 wxPrintf( "Not found %d\n", (int) iter->user_data );
2603 char *crash = NULL;
2604 *crash = 0;
2605 }
2606
2607 return result;
2608 }
2609
2610 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
2611 {
2612 if (!item.IsOk())
2613 return m_root;
2614
2615 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
2616
2617 if (!result)
2618 {
2619 wxPrintf( "Not found %d\n", (int) item.GetID() );
2620 char *crash = NULL;
2621 *crash = 0;
2622 }
2623
2624 return result;
2625 }
2626
2627 static wxGtkTreeModelNode*
2628 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
2629 {
2630 if( model == NULL )
2631 return NULL;
2632
2633 ItemList list;
2634 list.DeleteContents( true );
2635 if( !item.IsOk() )
2636 return NULL;
2637
2638 wxDataViewItem it( model->GetParent( item ) );
2639 while( it.IsOk() )
2640 {
2641 wxDataViewItem * pItem = new wxDataViewItem( it );
2642 list.Insert( pItem );
2643 it = model->GetParent( it );
2644 }
2645
2646 wxGtkTreeModelNode * node = treeNode;
2647 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
2648 {
2649 if( node && node->GetNodes().GetCount() != 0 )
2650 {
2651 int len = node->GetNodes().GetCount();
2652 wxGtkTreeModelNodes nodes = node->GetNodes();
2653 int j = 0;
2654 for( ; j < len; j ++)
2655 {
2656 if( nodes[j]->GetItem() == *(n->GetData()))
2657 {
2658 node = nodes[j];
2659 break;
2660 }
2661 }
2662
2663 if( j == len )
2664 {
2665 return NULL;
2666 }
2667 }
2668 else
2669 return NULL;
2670 }
2671 //Examine whether the node is item's parent node
2672 int len = node->GetChildCount();
2673 for( int i = 0; i < len ; i ++ )
2674 {
2675 if( node->GetChildren().Item( i ) == item.GetID() )
2676 return node;
2677 }
2678 return NULL;
2679 }
2680
2681 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
2682 {
2683 if (!iter)
2684 return NULL;
2685
2686 wxDataViewItem item( (void*) iter->user_data );
2687 if (!item.IsOk())
2688 return NULL;
2689
2690 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
2691 }
2692
2693 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
2694 {
2695 if (!item.IsOk())
2696 return NULL;
2697
2698 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
2699 }
2700
2701 //-----------------------------------------------------------------------------
2702 // wxDataViewCtrl signal callbacks
2703 //-----------------------------------------------------------------------------
2704
2705 static void
2706 wxdataview_selection_changed_callback( GtkTreeSelection* selection, wxDataViewCtrl *dv )
2707 {
2708 if (!GTK_WIDGET_REALIZED(dv->m_widget))
2709 return;
2710
2711 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED, dv->GetId() );
2712 // TODO: item
2713 event.SetModel( dv->GetModel() );
2714 dv->GetEventHandler()->ProcessEvent( event );
2715 }
2716
2717 static void
2718 wxdataview_row_activated_callback( GtkTreeView* treeview, GtkTreePath *path,
2719 GtkTreeViewColumn *column, wxDataViewCtrl *dv )
2720 {
2721 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
2722
2723 GtkTreeIter iter;
2724 dv->GtkGetInternal()->get_iter( &iter, path );
2725 wxDataViewItem item( (void*) iter.user_data );;
2726 event.SetItem( item );
2727 event.SetModel( dv->GetModel() );
2728 dv->GetEventHandler()->ProcessEvent( event );
2729 }
2730
2731 //-----------------------------------------------------------------------------
2732 // wxDataViewCtrl
2733 //-----------------------------------------------------------------------------
2734
2735 //-----------------------------------------------------------------------------
2736 // InsertChild for wxDataViewCtrl
2737 //-----------------------------------------------------------------------------
2738
2739 static void wxInsertChildInDataViewCtrl( wxWindowGTK* parent, wxWindowGTK* child )
2740 {
2741 wxDataViewCtrl * dvc = (wxDataViewCtrl*) parent;
2742 GtkWidget *treeview = dvc->GtkGetTreeView();
2743
2744 // Insert widget in GtkTreeView
2745 if (GTK_WIDGET_REALIZED(treeview))
2746 gtk_widget_set_parent_window( child->m_widget,
2747 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
2748 gtk_widget_set_parent( child->m_widget, treeview );
2749 }
2750
2751 static
2752 void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
2753 GtkAllocation *alloc,
2754 wxDataViewCtrl *win )
2755 {
2756 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
2757 while (node)
2758 {
2759 wxWindow *child = node->GetData();
2760
2761 GtkRequisition req;
2762 gtk_widget_size_request( child->m_widget, &req );
2763
2764 GtkAllocation alloc;
2765 alloc.x = child->m_x;
2766 alloc.y = child->m_y;
2767 alloc.width = child->m_width;
2768 alloc.height = child->m_height;
2769 gtk_widget_size_allocate( child->m_widget, &alloc );
2770
2771 node = node->GetNext();
2772 }
2773 }
2774
2775
2776
2777 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
2778
2779 wxDataViewCtrl::~wxDataViewCtrl()
2780 {
2781 if (m_notifier)
2782 GetModel()->RemoveNotifier( m_notifier );
2783
2784 // remove the model from the GtkTreeView before it gets destroyed by the
2785 // wxDataViewCtrlBase's dtor
2786 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL );
2787
2788 delete m_internal;
2789 }
2790
2791 void wxDataViewCtrl::Init()
2792 {
2793 m_notifier = NULL;
2794 }
2795
2796 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
2797 const wxPoint& pos, const wxSize& size,
2798 long style, const wxValidator& validator )
2799 {
2800 Init();
2801
2802 if (!PreCreation( parent, pos, size ) ||
2803 !CreateBase( parent, id, pos, size, style, validator ))
2804 {
2805 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
2806 return false;
2807 }
2808
2809 m_insertCallback = wxInsertChildInDataViewCtrl;
2810
2811 m_widget = gtk_scrolled_window_new (NULL, NULL);
2812
2813 GtkScrolledWindowSetBorder(m_widget, style);
2814
2815 m_treeview = gtk_tree_view_new();
2816 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
2817
2818 g_signal_connect (m_treeview, "size_allocate",
2819 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
2820
2821 #ifdef __WXGTK26__
2822 if (!gtk_check_version(2,6,0))
2823 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), TRUE );
2824 #endif
2825
2826 if (style & wxDV_MULTIPLE)
2827 {
2828 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2829 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2830 }
2831
2832 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
2833
2834 #ifdef __WXGTK210__
2835 if (!gtk_check_version(2,10,0))
2836 {
2837 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
2838
2839 if ((style & wxDV_HORIZ_RULES) != 0 &&
2840 (style & wxDV_VERT_RULES) != 0)
2841 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
2842 else if (style & wxDV_VERT_RULES)
2843 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
2844 else if (style & wxDV_HORIZ_RULES)
2845 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
2846
2847 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
2848 }
2849 else
2850 #endif
2851 {
2852 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_HORIZ_RULES) != 0 );
2853 }
2854
2855 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
2856 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
2857 gtk_widget_show (m_treeview);
2858
2859 m_parent->DoAddChild( this );
2860
2861 PostCreation(size);
2862
2863 GtkEnableSelectionEvents();
2864
2865 g_signal_connect_after (m_treeview, "row_activated",
2866 G_CALLBACK (wxdataview_row_activated_callback), this);
2867
2868 return true;
2869 }
2870
2871 void wxDataViewCtrl::OnInternalIdle()
2872 {
2873 wxWindow::OnInternalIdle();
2874
2875 unsigned int cols = GetColumnCount();
2876 unsigned int i;
2877 for (i = 0; i < cols; i++)
2878 {
2879 wxDataViewColumn *col = GetColumn( i );
2880 col->OnInternalIdle();
2881 }
2882 }
2883
2884 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
2885 {
2886 if (!wxDataViewCtrlBase::AssociateModel( model ))
2887 return false;
2888
2889 GtkWxTreeModel *gtk_model = wxgtk_tree_model_new();
2890 m_internal = new wxDataViewCtrlInternal( this, model, gtk_model );
2891 gtk_model->internal = m_internal;
2892
2893 m_notifier = new wxGtkDataViewModelNotifier( gtk_model, model, this );
2894
2895 model->AddNotifier( m_notifier );
2896
2897 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_model) );
2898
2899 // unref in wxDataViewCtrlInternal
2900 // g_object_unref( gtk_model );
2901
2902 return true;
2903 }
2904
2905 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
2906 {
2907 if (!wxDataViewCtrlBase::AppendColumn(col))
2908 return false;
2909
2910 GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle();
2911
2912 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), column );
2913
2914 return true;
2915 }
2916
2917 wxDataViewItem wxDataViewCtrl::GetSelection()
2918 {
2919 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2920
2921 if (m_windowStyle & wxDV_MULTIPLE)
2922 {
2923 }
2924 else
2925 {
2926 GtkTreeIter iter;
2927 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
2928 {
2929 wxDataViewItem item( (void*) iter.user_data );
2930 return item;
2931 }
2932 }
2933
2934 return wxDataViewItem(0);
2935 }
2936
2937 void wxDataViewCtrl::DoSetExpanderColumn()
2938 {
2939 }
2940
2941 void wxDataViewCtrl::DoSetIndent()
2942 {
2943 }
2944
2945 void wxDataViewCtrl::GtkDisableSelectionEvents()
2946 {
2947 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2948 g_signal_connect_after (selection, "changed",
2949 G_CALLBACK (wxdataview_selection_changed_callback), this);
2950 }
2951
2952 void wxDataViewCtrl::GtkEnableSelectionEvents()
2953 {
2954 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2955 g_signal_handlers_disconnect_by_func( selection,
2956 (gpointer) (wxdataview_selection_changed_callback), this);
2957 }
2958
2959 // static
2960 wxVisualAttributes
2961 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
2962 {
2963 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
2964 }
2965
2966
2967 #endif
2968 // !wxUSE_GENERICDATAVIEWCTRL
2969
2970 #endif
2971 // wxUSE_DATAVIEWCTRL