]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
c6d611358e70642ac440d2933cd2f5d6591803c5
[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 #include "wx/settings.h"
24 #include "wx/crt.h"
25 #endif
26
27 #include "wx/stockitem.h"
28 #include "wx/popupwin.h"
29 #include "wx/listimpl.cpp"
30
31 #include "wx/gtk/private.h"
32 #include "wx/gtk/dc.h"
33 #include "wx/gtk/dcclient.h"
34
35 #include "wx/gtk/private/gdkconv.h"
36 #include "wx/gtk/private/list.h"
37 #include "wx/gtk/private/event.h"
38 using namespace wxGTKImpl;
39
40 class wxGtkDataViewModelNotifier;
41
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44
45 static wxDataViewCtrlInternal *gs_internal = NULL;
46
47 class wxGtkTreeModelNode;
48
49 extern "C" {
50 typedef struct _GtkWxTreeModel GtkWxTreeModel;
51 }
52
53 // ----------------------------------------------------------------------------
54 // wxGtkTreePath: self-destroying GtkTreePath
55 // ----------------------------------------------------------------------------
56
57 // Usually this object is initialized with the associated GtkTreePath
58 // immediately when it's constructed but it can also be changed later either by
59 // using Assign() or by getting the pointer to the internally stored pointer
60 // value using ByRef(). The latter should be avoided but is very convenient
61 // when using GTK functions with GtkTreePath output parameters.
62 class wxGtkTreePath
63 {
64 public:
65 // Ctor takes ownership of the given path and will free it if non-NULL.
66 wxGtkTreePath(GtkTreePath *path = NULL) : m_path(path) { }
67
68 // Creates a tree path for the given string path.
69 wxGtkTreePath(const gchar *strpath)
70 : m_path(gtk_tree_path_new_from_string(strpath))
71 {
72 }
73
74 // Set the stored pointer if not done by ctor.
75 void Assign(GtkTreePath *path)
76 {
77 wxASSERT_MSG( !m_path, "shouldn't be already initialized" );
78
79 m_path = path;
80 }
81
82 // Return the pointer to the internally stored pointer. This should only be
83 // used to initialize the object by passing it to some GTK function.
84 GtkTreePath **ByRef()
85 {
86 wxASSERT_MSG( !m_path, "shouldn't be already initialized" );
87
88 return &m_path;
89 }
90
91
92 operator GtkTreePath *() const { return m_path; }
93
94 ~wxGtkTreePath() { if ( m_path ) gtk_tree_path_free(m_path); }
95
96 private:
97 GtkTreePath *m_path;
98
99 wxDECLARE_NO_COPY_CLASS(wxGtkTreePath);
100 };
101
102 // ----------------------------------------------------------------------------
103 // wxGtkTreePathList: self-destroying list of GtkTreePath objects.
104 // ----------------------------------------------------------------------------
105
106 class wxGtkTreePathList : public wxGtkList
107 {
108 public:
109 // Ctor takes ownership of the list.
110 explicit wxGtkTreePathList(GList* list)
111 : wxGtkList(list)
112 {
113 }
114
115 ~wxGtkTreePathList()
116 {
117 // Delete the list contents, wxGtkList will delete the list itself.
118 g_list_foreach(m_list, (GFunc)gtk_tree_path_free, NULL);
119 }
120 };
121
122 // ----------------------------------------------------------------------------
123 // wxGtkTreeSelectionLock: prevent selection from changing during the
124 // lifetime of this object
125 // ----------------------------------------------------------------------------
126
127 // Implementation note: it could be expected that setting the selection
128 // function in this class ctor and resetting it back to the old value in its
129 // dtor would work. However currently gtk_tree_selection_get_select_function()
130 // can't be passed NULL (see https://bugzilla.gnome.org/show_bug.cgi?id=626276)
131 // so we can't do this. Instead, we always use the selection function (which
132 // imposes extra overhead, albeit minimal one, on all selection operations) and
133 // just set/reset the flag telling it whether it should allow or forbid the
134 // selection.
135 //
136 // Also notice that currently only a single object of this class may exist at
137 // any given moment. It's just simpler like this and we don't need anything
138 // more for now.
139
140 extern "C"
141 gboolean wxdataview_selection_func(GtkTreeSelection * WXUNUSED(selection),
142 GtkTreeModel * WXUNUSED(model),
143 GtkTreePath * WXUNUSED(path),
144 gboolean WXUNUSED(path_currently_selected),
145 gpointer data)
146 {
147 return data == NULL;
148 }
149
150 class wxGtkTreeSelectionLock
151 {
152 public:
153 wxGtkTreeSelectionLock(GtkTreeSelection *selection)
154 : m_selection(selection)
155 {
156 wxASSERT_MSG( !ms_instance, "this class is not reentrant currently" );
157
158 ms_instance = this;
159
160 CheckCurrentSelectionFunc(NULL);
161
162 // Pass some non-NULL pointer as "data" for the callback, it doesn't
163 // matter what it is as long as it's non-NULL.
164 gtk_tree_selection_set_select_function(selection,
165 wxdataview_selection_func,
166 this,
167 NULL);
168 }
169
170 ~wxGtkTreeSelectionLock()
171 {
172 CheckCurrentSelectionFunc(wxdataview_selection_func);
173
174 gtk_tree_selection_set_select_function(m_selection,
175 wxdataview_selection_func,
176 NULL,
177 NULL);
178
179 ms_instance = NULL;
180 }
181
182 private:
183 void CheckCurrentSelectionFunc(GtkTreeSelectionFunc func)
184 {
185 // We can only use gtk_tree_selection_get_select_function() with 2.14+
186 // so check for its availability both during compile- and run-time.
187 #if GTK_CHECK_VERSION(2, 14, 0)
188 if ( gtk_check_version(2, 14, 0) != NULL )
189 return;
190
191 // If this assert is triggered, it means the code elsewhere has called
192 // gtk_tree_selection_set_select_function() but currently doing this
193 // breaks this class so the code here needs to be changed.
194 wxASSERT_MSG
195 (
196 gtk_tree_selection_get_select_function(m_selection) == func,
197 "selection function has changed unexpectedly, review this code!"
198 );
199 #endif // GTK+ 2.14+
200
201 wxUnusedVar(func);
202 }
203
204 static wxGtkTreeSelectionLock *ms_instance;
205
206 GtkTreeSelection * const m_selection;
207
208 wxDECLARE_NO_COPY_CLASS(wxGtkTreeSelectionLock);
209 };
210
211 wxGtkTreeSelectionLock *wxGtkTreeSelectionLock::ms_instance = NULL;
212
213 //-----------------------------------------------------------------------------
214 // wxDataViewCtrlInternal
215 //-----------------------------------------------------------------------------
216
217 WX_DECLARE_LIST(wxDataViewItem, ItemList);
218 WX_DEFINE_LIST(ItemList)
219
220 class wxDataViewCtrlInternal
221 {
222 public:
223 wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model );
224 ~wxDataViewCtrlInternal();
225
226 // model iface
227 GtkTreeModelFlags get_flags();
228 gboolean get_iter( GtkTreeIter *iter, GtkTreePath *path );
229 GtkTreePath *get_path( GtkTreeIter *iter);
230 gboolean iter_next( GtkTreeIter *iter );
231 gboolean iter_children( GtkTreeIter *iter, GtkTreeIter *parent);
232 gboolean iter_has_child( GtkTreeIter *iter );
233 gint iter_n_children( GtkTreeIter *iter );
234 gboolean iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n );
235 gboolean iter_parent( GtkTreeIter *iter, GtkTreeIter *child );
236
237 // dnd iface
238
239 bool EnableDragSource( const wxDataFormat &format );
240 bool EnableDropTarget( const wxDataFormat &format );
241
242 gboolean row_draggable( GtkTreeDragSource *drag_source, GtkTreePath *path );
243 gboolean drag_data_delete( GtkTreeDragSource *drag_source, GtkTreePath* path );
244 gboolean drag_data_get( GtkTreeDragSource *drag_source, GtkTreePath *path,
245 GtkSelectionData *selection_data );
246 gboolean drag_data_received( GtkTreeDragDest *drag_dest, GtkTreePath *dest,
247 GtkSelectionData *selection_data );
248 gboolean row_drop_possible( GtkTreeDragDest *drag_dest, GtkTreePath *dest_path,
249 GtkSelectionData *selection_data );
250
251 // notifactions from wxDataViewModel
252 bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
253 bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
254 bool ItemChanged( const wxDataViewItem &item );
255 bool ValueChanged( const wxDataViewItem &item, unsigned int model_column );
256 bool Cleared();
257 bool BeforeReset();
258 bool AfterReset();
259 void Resort();
260
261 // sorting interface
262 void SetSortOrder( GtkSortType sort_order ) { m_sort_order = sort_order; }
263 GtkSortType GetSortOrder() const { return m_sort_order; }
264
265 void SetSortColumn( int column ) { m_sort_column = column; }
266 int GetSortColumn() const { return m_sort_column; }
267
268 void SetDataViewSortColumn( wxDataViewColumn *column ) { m_dataview_sort_column = column; }
269 wxDataViewColumn *GetDataViewSortColumn() { return m_dataview_sort_column; }
270
271 bool IsSorted() { return (m_sort_column >= 0); }
272
273 // accessors
274 wxDataViewModel* GetDataViewModel() { return m_wx_model; }
275 const wxDataViewModel* GetDataViewModel() const { return m_wx_model; }
276 wxDataViewCtrl* GetOwner() { return m_owner; }
277 GtkWxTreeModel* GetGtkModel() { return m_gtk_model; }
278
279 // item can be deleted already in the model
280 int GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item );
281
282 void OnInternalIdle();
283
284 protected:
285 void InitTree();
286 void ScheduleRefresh();
287
288 wxGtkTreeModelNode *FindNode( const wxDataViewItem &item );
289 wxGtkTreeModelNode *FindNode( GtkTreeIter *iter );
290 wxGtkTreeModelNode *FindParentNode( const wxDataViewItem &item );
291 wxGtkTreeModelNode *FindParentNode( GtkTreeIter *iter );
292 void BuildBranch( wxGtkTreeModelNode *branch );
293
294 private:
295 wxGtkTreeModelNode *m_root;
296 wxDataViewModel *m_wx_model;
297 GtkWxTreeModel *m_gtk_model;
298 wxDataViewCtrl *m_owner;
299 GtkSortType m_sort_order;
300 wxDataViewColumn *m_dataview_sort_column;
301 int m_sort_column;
302
303 GtkTargetEntry m_dragSourceTargetEntry;
304 wxCharBuffer m_dragSourceTargetEntryTarget;
305 wxDataObject *m_dragDataObject;
306
307 GtkTargetEntry m_dropTargetTargetEntry;
308 wxCharBuffer m_dropTargetTargetEntryTarget;
309 wxDataObject *m_dropDataObject;
310
311 wxGtkDataViewModelNotifier *m_notifier;
312
313 bool m_dirty;
314 };
315
316
317 //-----------------------------------------------------------------------------
318 // wxGtkTreeModelNode
319 //-----------------------------------------------------------------------------
320
321 static
322 int LINKAGEMODE wxGtkTreeModelChildCmp( void** id1, void** id2 )
323 {
324 int ret = gs_internal->GetDataViewModel()->Compare( wxDataViewItem(*id1), wxDataViewItem(*id2),
325 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
326
327 return ret;
328 }
329
330 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode*, wxGtkTreeModelNodes );
331 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren );
332
333 class wxGtkTreeModelNode
334 {
335 public:
336 wxGtkTreeModelNode( wxGtkTreeModelNode* parent, const wxDataViewItem &item,
337 wxDataViewCtrlInternal *internal )
338 {
339 m_parent = parent;
340 m_item = item;
341 m_internal = internal;
342 }
343
344 ~wxGtkTreeModelNode()
345 {
346 size_t count = m_nodes.GetCount();
347 size_t i;
348 for (i = 0; i < count; i++)
349 {
350 wxGtkTreeModelNode *child = m_nodes.Item( i );
351 delete child;
352 }
353 }
354
355 void AddNode( wxGtkTreeModelNode* child )
356 {
357 m_nodes.Add( child );
358
359 void *id = child->GetItem().GetID();
360
361 m_children.Add( id );
362
363 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
364 {
365 gs_internal = m_internal;
366 m_children.Sort( &wxGtkTreeModelChildCmp );
367 }
368 }
369
370 void InsertNode( wxGtkTreeModelNode* child, unsigned pos )
371 {
372 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
373 {
374 AddNode(child);
375 return;
376 }
377
378 void *id = child->GetItem().GetID();
379
380 // Insert into m_nodes so that the order of nodes in m_nodes is the
381 // same as the order of their corresponding IDs in m_children:
382 const unsigned int count = m_nodes.GetCount();
383 bool inserted = false;
384 for (unsigned i = 0; i < count; i++)
385 {
386 wxGtkTreeModelNode *node = m_nodes[i];
387 int posInChildren = m_children.Index(node->GetItem().GetID());
388 if ( (unsigned)posInChildren >= pos )
389 {
390 m_nodes.Insert(child, i);
391 inserted = true;
392 break;
393 }
394 }
395 if ( !inserted )
396 m_nodes.Add(child);
397
398 m_children.Insert( id, pos );
399 }
400
401 void AddLeaf( void* id )
402 {
403 InsertLeaf(id, m_children.size());
404 }
405
406 void InsertLeaf( void* id, unsigned pos )
407 {
408 m_children.Insert( id, pos );
409
410 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
411 {
412 gs_internal = m_internal;
413 m_children.Sort( &wxGtkTreeModelChildCmp );
414 }
415 }
416
417 void DeleteChild( void* id )
418 {
419 m_children.Remove( id );
420
421 unsigned int count = m_nodes.GetCount();
422 unsigned int pos;
423 for (pos = 0; pos < count; pos++)
424 {
425 wxGtkTreeModelNode *node = m_nodes.Item( pos );
426 if (node->GetItem().GetID() == id)
427 {
428 m_nodes.RemoveAt( pos );
429 delete node;
430 break;
431 }
432 }
433 }
434
435 // returns position of child node for given item in children list or wxNOT_FOUND
436 int FindChildByItem(const wxDataViewItem& item) const
437 {
438 const void* itemId = item.GetID();
439 const wxGtkTreeModelChildren& nodes = m_children;
440 const int len = nodes.size();
441 for ( int i = 0; i < len; i++ )
442 {
443 if ( nodes[i] == itemId )
444 return i;
445 }
446 return wxNOT_FOUND;
447 }
448
449 wxGtkTreeModelNode* GetParent()
450 { return m_parent; }
451 wxGtkTreeModelNodes &GetNodes()
452 { return m_nodes; }
453 wxGtkTreeModelChildren &GetChildren()
454 { return m_children; }
455
456 unsigned int GetChildCount() const { return m_children.GetCount(); }
457 unsigned int GetNodesCount() const { return m_nodes.GetCount(); }
458
459 wxDataViewItem &GetItem() { return m_item; }
460 wxDataViewCtrlInternal *GetInternal() { return m_internal; }
461
462 void Resort();
463
464 private:
465 wxGtkTreeModelNode *m_parent;
466 wxGtkTreeModelNodes m_nodes;
467 wxGtkTreeModelChildren m_children;
468 wxDataViewItem m_item;
469 wxDataViewCtrlInternal *m_internal;
470 };
471
472
473 //-----------------------------------------------------------------------------
474 // data
475 //-----------------------------------------------------------------------------
476
477 extern bool g_blockEventsOnDrag;
478
479 //-----------------------------------------------------------------------------
480 // define new GTK+ class wxGtkTreeModel
481 //-----------------------------------------------------------------------------
482
483 extern "C" {
484
485 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
486 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
487 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
488 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
489 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
490 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
491
492 GType gtk_wx_tree_model_get_type (void);
493
494 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass;
495
496 struct _GtkWxTreeModel
497 {
498 GObject parent;
499
500 /*< private >*/
501 gint stamp;
502 wxDataViewCtrlInternal *internal;
503 };
504
505 struct _GtkWxTreeModelClass
506 {
507 GObjectClass list_parent_class;
508 };
509
510 static GtkWxTreeModel *wxgtk_tree_model_new (void);
511 static void wxgtk_tree_model_init (GtkWxTreeModel *tree_model);
512 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass);
513
514 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface);
515 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface);
516 static void wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface);
517 static void wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface);
518
519 static GtkTreeModelFlags wxgtk_tree_model_get_flags (GtkTreeModel *tree_model);
520 static gint wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model);
521 static GType wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
522 gint index);
523 static gboolean wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
524 GtkTreeIter *iter,
525 GtkTreePath *path);
526 static GtkTreePath *wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
527 GtkTreeIter *iter);
528 static void wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
529 GtkTreeIter *iter,
530 gint column,
531 GValue *value);
532 static gboolean wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
533 GtkTreeIter *iter);
534 static gboolean wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
535 GtkTreeIter *iter,
536 GtkTreeIter *parent);
537 static gboolean wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
538 GtkTreeIter *iter);
539 static gint wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
540 GtkTreeIter *iter);
541 static gboolean wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
542 GtkTreeIter *iter,
543 GtkTreeIter *parent,
544 gint n);
545 static gboolean wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
546 GtkTreeIter *iter,
547 GtkTreeIter *child);
548
549 /* sortable */
550 static gboolean wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
551 gint *sort_column_id,
552 GtkSortType *order);
553 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
554 gint sort_column_id,
555 GtkSortType order);
556 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
557 gint sort_column_id,
558 GtkTreeIterCompareFunc func,
559 gpointer data,
560 GDestroyNotify destroy);
561 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
562 GtkTreeIterCompareFunc func,
563 gpointer data,
564 GDestroyNotify destroy);
565 static gboolean wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable);
566
567 /* drag'n'drop */
568 static gboolean wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source,
569 GtkTreePath *path);
570 static gboolean wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source,
571 GtkTreePath *path);
572 static gboolean wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source,
573 GtkTreePath *path,
574 GtkSelectionData *selection_data);
575 static gboolean wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest,
576 GtkTreePath *dest,
577 GtkSelectionData *selection_data);
578 static gboolean wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest,
579 GtkTreePath *dest_path,
580 GtkSelectionData *selection_data);
581
582
583 static GObjectClass *list_parent_class = NULL;
584
585 GType
586 gtk_wx_tree_model_get_type (void)
587 {
588 static GType tree_model_type = 0;
589
590 if (!tree_model_type)
591 {
592 const GTypeInfo tree_model_info =
593 {
594 sizeof (GtkWxTreeModelClass),
595 NULL, /* base_init */
596 NULL, /* base_finalize */
597 (GClassInitFunc) wxgtk_tree_model_class_init,
598 NULL, /* class_finalize */
599 NULL, /* class_data */
600 sizeof (GtkWxTreeModel),
601 0,
602 (GInstanceInitFunc) wxgtk_tree_model_init,
603 };
604
605 static const GInterfaceInfo tree_model_iface_info =
606 {
607 (GInterfaceInitFunc) wxgtk_tree_model_tree_model_init,
608 NULL,
609 NULL
610 };
611
612 static const GInterfaceInfo sortable_iface_info =
613 {
614 (GInterfaceInitFunc) wxgtk_tree_model_sortable_init,
615 NULL,
616 NULL
617 };
618
619 static const GInterfaceInfo drag_source_iface_info =
620 {
621 (GInterfaceInitFunc) wxgtk_tree_model_drag_source_init,
622 NULL,
623 NULL
624 };
625
626 static const GInterfaceInfo drag_dest_iface_info =
627 {
628 (GInterfaceInitFunc) wxgtk_tree_model_drag_dest_init,
629 NULL,
630 NULL
631 };
632
633 tree_model_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxTreeModel",
634 &tree_model_info, (GTypeFlags)0 );
635
636 g_type_add_interface_static (tree_model_type,
637 GTK_TYPE_TREE_MODEL,
638 &tree_model_iface_info);
639 g_type_add_interface_static (tree_model_type,
640 GTK_TYPE_TREE_SORTABLE,
641 &sortable_iface_info);
642 g_type_add_interface_static (tree_model_type,
643 GTK_TYPE_TREE_DRAG_DEST,
644 &drag_dest_iface_info);
645 g_type_add_interface_static (tree_model_type,
646 GTK_TYPE_TREE_DRAG_SOURCE,
647 &drag_source_iface_info);
648 }
649
650 return tree_model_type;
651 }
652
653 static GtkWxTreeModel *
654 wxgtk_tree_model_new(void)
655 {
656 GtkWxTreeModel *retval = (GtkWxTreeModel *) g_object_new (GTK_TYPE_WX_TREE_MODEL, NULL);
657 return retval;
658 }
659
660 static void
661 wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass)
662 {
663 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
664 }
665
666 static void
667 wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface)
668 {
669 iface->get_flags = wxgtk_tree_model_get_flags;
670 iface->get_n_columns = wxgtk_tree_model_get_n_columns;
671 iface->get_column_type = wxgtk_tree_model_get_column_type;
672 iface->get_iter = wxgtk_tree_model_get_iter;
673 iface->get_path = wxgtk_tree_model_get_path;
674 iface->get_value = wxgtk_tree_model_get_value;
675 iface->iter_next = wxgtk_tree_model_iter_next;
676 iface->iter_children = wxgtk_tree_model_iter_children;
677 iface->iter_has_child = wxgtk_tree_model_iter_has_child;
678 iface->iter_n_children = wxgtk_tree_model_iter_n_children;
679 iface->iter_nth_child = wxgtk_tree_model_iter_nth_child;
680 iface->iter_parent = wxgtk_tree_model_iter_parent;
681 }
682
683 static void
684 wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface)
685 {
686 iface->get_sort_column_id = wxgtk_tree_model_get_sort_column_id;
687 iface->set_sort_column_id = wxgtk_tree_model_set_sort_column_id;
688 iface->set_sort_func = wxgtk_tree_model_set_sort_func;
689 iface->set_default_sort_func = wxgtk_tree_model_set_default_sort_func;
690 iface->has_default_sort_func = wxgtk_tree_model_has_default_sort_func;
691 }
692
693 static void
694 wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface)
695 {
696 iface->row_draggable = wxgtk_tree_model_row_draggable;
697 iface->drag_data_delete = wxgtk_tree_model_drag_data_delete;
698 iface->drag_data_get = wxgtk_tree_model_drag_data_get;
699 }
700
701 static void
702 wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface)
703 {
704 iface->drag_data_received = wxgtk_tree_model_drag_data_received;
705 iface->row_drop_possible = wxgtk_tree_model_row_drop_possible;
706 }
707
708 static void
709 wxgtk_tree_model_init (GtkWxTreeModel *tree_model)
710 {
711 tree_model->internal = NULL;
712 tree_model->stamp = g_random_int();
713 }
714
715 } // extern "C"
716
717 //-----------------------------------------------------------------------------
718 // implement callbacks from wxGtkTreeModel class by letting
719 // them call the methods of wxWidgets' wxDataViewModel
720 //-----------------------------------------------------------------------------
721
722 static GtkTreeModelFlags
723 wxgtk_tree_model_get_flags (GtkTreeModel *tree_model)
724 {
725 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
726 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), (GtkTreeModelFlags)0 );
727
728 return wxtree_model->internal->get_flags();
729 }
730
731 static gint
732 wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model)
733 {
734 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
735 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), 0);
736
737 return wxtree_model->internal->GetDataViewModel()->GetColumnCount();
738 }
739
740 static GType
741 wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
742 gint index)
743 {
744 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
745 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), G_TYPE_INVALID);
746
747 GType gtype = G_TYPE_INVALID;
748
749 wxString wxtype = wxtree_model->internal->GetDataViewModel()->GetColumnType( (unsigned int) index );
750
751 if (wxtype == wxT("string"))
752 gtype = G_TYPE_STRING;
753 else
754 {
755 gtype = G_TYPE_POINTER;
756 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
757 }
758
759 return gtype;
760 }
761
762 static gboolean
763 wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
764 GtkTreeIter *iter,
765 GtkTreePath *path)
766 {
767 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
768 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
769 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
770
771 return wxtree_model->internal->get_iter( iter, path );
772 }
773
774 static GtkTreePath *
775 wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
776 GtkTreeIter *iter)
777 {
778 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
779 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), NULL);
780 g_return_val_if_fail (iter->stamp == GTK_WX_TREE_MODEL (wxtree_model)->stamp, NULL);
781
782 return wxtree_model->internal->get_path( iter );
783 }
784
785 static void
786 wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
787 GtkTreeIter *iter,
788 gint column,
789 GValue *value)
790 {
791 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
792 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model) );
793
794 wxDataViewModel *model = wxtree_model->internal->GetDataViewModel();
795 wxString mtype = model->GetColumnType( (unsigned int) column );
796 if (mtype == wxT("string"))
797 {
798 wxVariant variant;
799 g_value_init( value, G_TYPE_STRING );
800 wxDataViewItem item( (void*) iter->user_data );
801 model->GetValue( variant, item, (unsigned int) column );
802
803 g_value_set_string( value, variant.GetString().utf8_str() );
804 }
805 else
806 {
807 wxFAIL_MSG( wxT("non-string columns not supported yet") );
808 }
809 }
810
811 static gboolean
812 wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
813 GtkTreeIter *iter)
814 {
815 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
816
817 // This happens when clearing the view by calling .._set_model( NULL );
818 if (iter->stamp == 0) return FALSE;
819
820 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
821 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
822
823 return wxtree_model->internal->iter_next( iter );
824 }
825
826 static gboolean
827 wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
828 GtkTreeIter *iter,
829 GtkTreeIter *parent)
830 {
831 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
832 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
833 if (parent)
834 {
835 g_return_val_if_fail (wxtree_model->stamp == parent->stamp, FALSE);
836 }
837
838 return wxtree_model->internal->iter_children( iter, parent );
839 }
840
841 static gboolean
842 wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
843 GtkTreeIter *iter)
844 {
845 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
846 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
847 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
848
849 return wxtree_model->internal->iter_has_child( iter );
850 }
851
852 static gint
853 wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
854 GtkTreeIter *iter)
855 {
856 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
857 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), 0);
858 g_return_val_if_fail ( !iter || wxtree_model->stamp == iter->stamp, 0);
859
860 return wxtree_model->internal->iter_n_children( iter );
861 }
862
863 static gboolean
864 wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
865 GtkTreeIter *iter,
866 GtkTreeIter *parent,
867 gint n)
868 {
869 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
870 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
871
872 return wxtree_model->internal->iter_nth_child( iter, parent, n );
873 }
874
875 static gboolean
876 wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
877 GtkTreeIter *iter,
878 GtkTreeIter *child)
879 {
880 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
881 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
882 g_return_val_if_fail (wxtree_model->stamp == child->stamp, FALSE);
883
884 return wxtree_model->internal->iter_parent( iter, child );
885 }
886
887 /* drag'n'drop iface */
888 static gboolean
889 wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source,
890 GtkTreePath *path)
891 {
892 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
893 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
894
895 return wxtree_model->internal->row_draggable( drag_source, path );
896 }
897
898 static gboolean
899 wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source,
900 GtkTreePath *path)
901 {
902 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
903 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
904
905 return wxtree_model->internal->drag_data_delete( drag_source, path );
906 }
907
908 static gboolean
909 wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source,
910 GtkTreePath *path,
911 GtkSelectionData *selection_data)
912 {
913 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
914 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
915
916 #if 0
917 wxPrintf( "drag_get_data\n");
918
919 wxGtkString atom_selection(gdk_atom_name(selection_data->selection));
920 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection) );
921
922 wxGtkString atom_target(gdk_atom_name(selection_data->target));
923 wxPrintf( "target %s\n", wxString::FromAscii(atom_target) );
924
925 wxGtkString atom_type(gdk_atom_name(selection_data->type));
926 wxPrintf( "type %s\n", wxString::FromAscii(atom_type) );
927
928 wxPrintf( "format %d\n", selection_data->format );
929 #endif
930
931 return wxtree_model->internal->drag_data_get( drag_source, path, selection_data );
932 }
933
934 static gboolean
935 wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest,
936 GtkTreePath *dest,
937 GtkSelectionData *selection_data)
938 {
939 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest;
940 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
941
942 return wxtree_model->internal->drag_data_received( drag_dest, dest, selection_data );
943 }
944
945 static gboolean
946 wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest,
947 GtkTreePath *dest_path,
948 GtkSelectionData *selection_data)
949 {
950 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest;
951 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
952
953 return wxtree_model->internal->row_drop_possible( drag_dest, dest_path, selection_data );
954 }
955
956 /* sortable iface */
957 static gboolean
958 wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
959 gint *sort_column_id,
960 GtkSortType *order)
961 {
962 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) sortable;
963
964 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE);
965
966 if (!wxtree_model->internal->IsSorted())
967 {
968 if (sort_column_id)
969 *sort_column_id = -1;
970
971 return TRUE;
972 }
973
974
975 if (sort_column_id)
976 *sort_column_id = wxtree_model->internal->GetSortColumn();
977
978 if (order)
979 *order = wxtree_model->internal->GetSortOrder();
980
981 return TRUE;
982 }
983
984 wxDataViewColumn *gs_lastLeftClickHeader = NULL;
985
986 static void
987 wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
988 gint sort_column_id,
989 GtkSortType order)
990 {
991 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) sortable;
992 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
993
994 tree_model->internal->SetDataViewSortColumn( gs_lastLeftClickHeader );
995
996 if ((sort_column_id != (gint) tree_model->internal->GetSortColumn()) ||
997 (order != tree_model->internal->GetSortOrder()))
998 {
999 tree_model->internal->SetSortColumn( sort_column_id );
1000 tree_model->internal->SetSortOrder( order );
1001
1002 gtk_tree_sortable_sort_column_changed (sortable);
1003
1004 tree_model->internal->GetDataViewModel()->Resort();
1005 }
1006
1007 if (gs_lastLeftClickHeader)
1008 {
1009 wxDataViewCtrl *dv = tree_model->internal->GetOwner();
1010 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, dv->GetId() );
1011 event.SetDataViewColumn( gs_lastLeftClickHeader );
1012 event.SetModel( dv->GetModel() );
1013 dv->HandleWindowEvent( event );
1014 }
1015
1016 gs_lastLeftClickHeader = NULL;
1017 }
1018
1019 static void
1020 wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
1021 gint WXUNUSED(sort_column_id),
1022 GtkTreeIterCompareFunc func,
1023 gpointer WXUNUSED(data),
1024 GDestroyNotify WXUNUSED(destroy))
1025 {
1026 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
1027 g_return_if_fail (func != NULL);
1028 }
1029
1030 static void
1031 wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
1032 GtkTreeIterCompareFunc func,
1033 gpointer WXUNUSED(data),
1034 GDestroyNotify WXUNUSED(destroy))
1035 {
1036 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
1037 g_return_if_fail (func != NULL);
1038
1039 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
1040 // TODO: remove this code
1041 }
1042
1043 static gboolean
1044 wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable)
1045 {
1046 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE );
1047
1048 return FALSE;
1049 }
1050
1051 //-----------------------------------------------------------------------------
1052 // define new GTK+ class GtkWxRendererText
1053 //-----------------------------------------------------------------------------
1054
1055 extern "C" {
1056
1057 #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
1058 #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
1059 #define GTK_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1060 #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1061 #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1062 #define GTK_WX_CELL_RENDERER_TEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1063
1064 GType gtk_wx_cell_renderer_text_get_type (void);
1065
1066 typedef struct _GtkWxCellRendererText GtkWxCellRendererText;
1067 typedef struct _GtkWxCellRendererTextClass GtkWxCellRendererTextClass;
1068
1069 struct _GtkWxCellRendererText
1070 {
1071 GtkCellRendererText parent;
1072
1073 wxDataViewRenderer *wx_renderer;
1074 };
1075
1076 struct _GtkWxCellRendererTextClass
1077 {
1078 GtkCellRendererTextClass cell_parent_class;
1079 };
1080
1081
1082 static GtkWxCellRendererText *gtk_wx_cell_renderer_text_new (void);
1083 static void gtk_wx_cell_renderer_text_init (
1084 GtkWxCellRendererText *cell );
1085 static void gtk_wx_cell_renderer_text_class_init(
1086 GtkWxCellRendererTextClass *klass );
1087 static GtkCellEditable *gtk_wx_cell_renderer_text_start_editing(
1088 GtkCellRenderer *cell,
1089 GdkEvent *event,
1090 GtkWidget *widget,
1091 const gchar *path,
1092 GdkRectangle *background_area,
1093 GdkRectangle *cell_area,
1094 GtkCellRendererState flags );
1095
1096
1097 static GObjectClass *text_cell_parent_class = NULL;
1098
1099 } // extern "C"
1100
1101 GType
1102 gtk_wx_cell_renderer_text_get_type (void)
1103 {
1104 static GType cell_wx_type = 0;
1105
1106 if (!cell_wx_type)
1107 {
1108 const GTypeInfo cell_wx_info =
1109 {
1110 sizeof (GtkWxCellRendererTextClass),
1111 NULL, /* base_init */
1112 NULL, /* base_finalize */
1113 (GClassInitFunc) gtk_wx_cell_renderer_text_class_init,
1114 NULL, /* class_finalize */
1115 NULL, /* class_data */
1116 sizeof (GtkWxCellRendererText),
1117 0, /* n_preallocs */
1118 (GInstanceInitFunc) gtk_wx_cell_renderer_text_init,
1119 };
1120
1121 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT,
1122 "GtkWxCellRendererText", &cell_wx_info, (GTypeFlags)0 );
1123 }
1124
1125 return cell_wx_type;
1126 }
1127
1128 static void
1129 gtk_wx_cell_renderer_text_init (GtkWxCellRendererText *cell)
1130 {
1131 cell->wx_renderer = NULL;
1132 }
1133
1134 static void
1135 gtk_wx_cell_renderer_text_class_init (GtkWxCellRendererTextClass *klass)
1136 {
1137 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
1138
1139 text_cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
1140
1141 cell_class->start_editing = gtk_wx_cell_renderer_text_start_editing;
1142 }
1143
1144 GtkWxCellRendererText*
1145 gtk_wx_cell_renderer_text_new (void)
1146 {
1147 return (GtkWxCellRendererText*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT, NULL);
1148 }
1149
1150 static GtkCellEditable *gtk_wx_cell_renderer_text_start_editing(
1151 GtkCellRenderer *gtk_renderer,
1152 GdkEvent *gdk_event,
1153 GtkWidget *widget,
1154 const gchar *path,
1155 GdkRectangle *background_area,
1156 GdkRectangle *cell_area,
1157 GtkCellRendererState flags )
1158 {
1159 GtkWxCellRendererText *wxgtk_renderer = (GtkWxCellRendererText *) gtk_renderer;
1160 wxDataViewRenderer *wx_renderer = wxgtk_renderer->wx_renderer;
1161 wxDataViewColumn *column = wx_renderer->GetOwner();
1162
1163 wxDataViewItem
1164 item(column->GetOwner()->GTKPathToItem(wxGtkTreePath(path)));
1165
1166 wxDataViewCtrl *dv = column->GetOwner();
1167 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, dv->GetId() );
1168 event.SetDataViewColumn( column );
1169 event.SetModel( dv->GetModel() );
1170 event.SetColumn( column->GetModelColumn() );
1171 event.SetItem( item );
1172 dv->HandleWindowEvent( event );
1173
1174 if (event.IsAllowed())
1175 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class)->
1176 start_editing( gtk_renderer, gdk_event, widget, path, background_area, cell_area, flags );
1177 else
1178 return NULL;
1179 }
1180
1181 //-----------------------------------------------------------------------------
1182 // define new GTK+ class GtkWxCellRenderer
1183 //-----------------------------------------------------------------------------
1184
1185 extern "C" {
1186
1187 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
1188 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
1189 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1190 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
1191 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
1192 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1193
1194 GType gtk_wx_cell_renderer_get_type (void);
1195
1196 typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
1197 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
1198
1199 struct _GtkWxCellRenderer
1200 {
1201 GtkCellRenderer parent;
1202
1203 /*< private >*/
1204 wxDataViewCustomRenderer *cell;
1205 };
1206
1207 struct _GtkWxCellRendererClass
1208 {
1209 GtkCellRendererClass cell_parent_class;
1210 };
1211
1212
1213 static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
1214 static void gtk_wx_cell_renderer_init (
1215 GtkWxCellRenderer *cell );
1216 static void gtk_wx_cell_renderer_class_init(
1217 GtkWxCellRendererClass *klass );
1218 static void gtk_wx_cell_renderer_get_size (
1219 GtkCellRenderer *cell,
1220 GtkWidget *widget,
1221 GdkRectangle *rectangle,
1222 gint *x_offset,
1223 gint *y_offset,
1224 gint *width,
1225 gint *height );
1226 static void gtk_wx_cell_renderer_render (
1227 GtkCellRenderer *cell,
1228 GdkWindow *window,
1229 GtkWidget *widget,
1230 GdkRectangle *background_area,
1231 GdkRectangle *cell_area,
1232 GdkRectangle *expose_area,
1233 GtkCellRendererState flags );
1234 static gboolean gtk_wx_cell_renderer_activate(
1235 GtkCellRenderer *cell,
1236 GdkEvent *event,
1237 GtkWidget *widget,
1238 const gchar *path,
1239 GdkRectangle *background_area,
1240 GdkRectangle *cell_area,
1241 GtkCellRendererState flags );
1242 static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
1243 GtkCellRenderer *cell,
1244 GdkEvent *event,
1245 GtkWidget *widget,
1246 const gchar *path,
1247 GdkRectangle *background_area,
1248 GdkRectangle *cell_area,
1249 GtkCellRendererState flags );
1250
1251
1252 static GObjectClass *cell_parent_class = NULL;
1253
1254 } // extern "C"
1255
1256 GType
1257 gtk_wx_cell_renderer_get_type (void)
1258 {
1259 static GType cell_wx_type = 0;
1260
1261 if (!cell_wx_type)
1262 {
1263 const GTypeInfo cell_wx_info =
1264 {
1265 sizeof (GtkWxCellRendererClass),
1266 NULL, /* base_init */
1267 NULL, /* base_finalize */
1268 (GClassInitFunc) gtk_wx_cell_renderer_class_init,
1269 NULL, /* class_finalize */
1270 NULL, /* class_data */
1271 sizeof (GtkWxCellRenderer),
1272 0, /* n_preallocs */
1273 (GInstanceInitFunc) gtk_wx_cell_renderer_init,
1274 };
1275
1276 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER,
1277 "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 );
1278 }
1279
1280 return cell_wx_type;
1281 }
1282
1283 static void
1284 gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
1285 {
1286 cell->cell = NULL;
1287 }
1288
1289 static void
1290 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
1291 {
1292 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
1293
1294 cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
1295
1296 cell_class->get_size = gtk_wx_cell_renderer_get_size;
1297 cell_class->render = gtk_wx_cell_renderer_render;
1298 cell_class->activate = gtk_wx_cell_renderer_activate;
1299 cell_class->start_editing = gtk_wx_cell_renderer_start_editing;
1300 }
1301
1302 GtkCellRenderer*
1303 gtk_wx_cell_renderer_new (void)
1304 {
1305 return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
1306 }
1307
1308 static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
1309 GtkCellRenderer *renderer,
1310 GdkEvent *WXUNUSED(event),
1311 GtkWidget *widget,
1312 const gchar *path,
1313 GdkRectangle *WXUNUSED(background_area),
1314 GdkRectangle *cell_area,
1315 GtkCellRendererState WXUNUSED(flags) )
1316 {
1317 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1318 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1319
1320 // Renderer doesn't support in-place editing
1321 if (!cell->HasEditorCtrl())
1322 return NULL;
1323
1324 // An in-place editing control is still around
1325 if (cell->GetEditorCtrl())
1326 return NULL;
1327
1328 GdkRectangle rect;
1329 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
1330 &rect.x,
1331 &rect.y,
1332 &rect.width,
1333 &rect.height);
1334
1335 rect.x += cell_area->x;
1336 rect.y += cell_area->y;
1337 // rect.width -= renderer->xpad * 2;
1338 // rect.height -= renderer->ypad * 2;
1339
1340 // wxRect renderrect(wxRectFromGDKRect(&rect));
1341 wxRect renderrect(wxRectFromGDKRect(cell_area));
1342
1343 wxDataViewItem
1344 item(cell->GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(path)));
1345
1346 cell->StartEditing( item, renderrect );
1347
1348 return NULL;
1349 }
1350
1351 static void
1352 gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
1353 GtkWidget *WXUNUSED(widget),
1354 GdkRectangle *cell_area,
1355 gint *x_offset,
1356 gint *y_offset,
1357 gint *width,
1358 gint *height)
1359 {
1360 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1361 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1362
1363 wxSize size = cell->GetSize();
1364
1365 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
1366
1367 // Uniform row height, if specified, overrides the value returned by the
1368 // renderer.
1369 if ( !ctrl->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) )
1370 {
1371 const int uniformHeight = ctrl->GTKGetUniformRowHeight();
1372 if ( uniformHeight > 0 )
1373 size.y = uniformHeight;
1374 }
1375
1376 int xpad, ypad;
1377 gtk_cell_renderer_get_padding(renderer, &xpad, &ypad);
1378 int calc_width = xpad * 2 + size.x;
1379 int calc_height = ypad * 2 + size.y;
1380
1381 if (x_offset)
1382 *x_offset = 0;
1383 if (y_offset)
1384 *y_offset = 0;
1385
1386 if (cell_area && size.x > 0 && size.y > 0)
1387 {
1388 float xalign, yalign;
1389 gtk_cell_renderer_get_alignment(renderer, &xalign, &yalign);
1390 if (x_offset)
1391 {
1392 *x_offset = int(xalign * (cell_area->width - calc_width - 2 * xpad));
1393 *x_offset = MAX(*x_offset, 0) + xpad;
1394 }
1395 if (y_offset)
1396 {
1397 *y_offset = int(yalign * (cell_area->height - calc_height - 2 * ypad));
1398 *y_offset = MAX(*y_offset, 0) + ypad;
1399 }
1400 }
1401
1402 if (width)
1403 *width = calc_width;
1404
1405 if (height)
1406 *height = calc_height;
1407 }
1408
1409 static void
1410 gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
1411 GdkWindow *window,
1412 GtkWidget *widget,
1413 GdkRectangle *background_area,
1414 GdkRectangle *cell_area,
1415 GdkRectangle *expose_area,
1416 GtkCellRendererState flags)
1417
1418 {
1419 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1420 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1421
1422 cell->GTKStashRenderParams(window, widget,
1423 background_area, expose_area, flags);
1424
1425 wxRect rect(wxRectFromGDKRect(cell_area));
1426 int xpad, ypad;
1427 gtk_cell_renderer_get_padding(renderer, &xpad, &ypad);
1428 rect = rect.Deflate(xpad, ypad);
1429
1430 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
1431 wxWindowDCImpl *impl = (wxWindowDCImpl *) dc->GetImpl();
1432
1433 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1434 // window such as a DnD drop window.
1435 if (window != impl->m_gdkwindow)
1436 {
1437 impl->Destroy();
1438 impl->m_gdkwindow = window;
1439 impl->SetUpDC();
1440 }
1441
1442 int state = 0;
1443 if (flags & GTK_CELL_RENDERER_SELECTED)
1444 state |= wxDATAVIEW_CELL_SELECTED;
1445 if (flags & GTK_CELL_RENDERER_PRELIT)
1446 state |= wxDATAVIEW_CELL_PRELIT;
1447 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1448 state |= wxDATAVIEW_CELL_INSENSITIVE;
1449 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1450 state |= wxDATAVIEW_CELL_INSENSITIVE;
1451 if (flags & GTK_CELL_RENDERER_FOCUSED)
1452 state |= wxDATAVIEW_CELL_FOCUSED;
1453 cell->WXCallRender( rect, dc, state );
1454 }
1455
1456 static gboolean
1457 gtk_wx_cell_renderer_activate(
1458 GtkCellRenderer *renderer,
1459 GdkEvent *event,
1460 GtkWidget *widget,
1461 const gchar *path,
1462 GdkRectangle *WXUNUSED(background_area),
1463 GdkRectangle *cell_area,
1464 GtkCellRendererState WXUNUSED(flags) )
1465 {
1466 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1467 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1468
1469 GdkRectangle rect;
1470 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
1471 &rect.x,
1472 &rect.y,
1473 &rect.width,
1474 &rect.height);
1475
1476 rect.x += cell_area->x;
1477 rect.y += cell_area->y;
1478 int xpad, ypad;
1479 gtk_cell_renderer_get_padding(renderer, &xpad, &ypad);
1480 rect.width -= xpad * 2;
1481 rect.height -= ypad * 2;
1482
1483 wxRect renderrect(wxRectFromGDKRect(&rect));
1484
1485 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
1486 wxDataViewModel *model = ctrl->GetModel();
1487
1488 wxDataViewItem item(ctrl->GTKPathToItem(wxGtkTreePath(path)));
1489
1490 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1491
1492 if ( !event )
1493 {
1494 // activated by <ENTER>
1495 return cell->ActivateCell(renderrect, model, item, model_col, NULL);
1496 }
1497 else if ( event->type == GDK_BUTTON_PRESS )
1498 {
1499 GdkEventButton *button_event = (GdkEventButton*)event;
1500 if ( button_event->button == 1 )
1501 {
1502 wxMouseEvent mouse_event(wxEVT_LEFT_DOWN);
1503 InitMouseEvent(ctrl, mouse_event, button_event);
1504
1505 mouse_event.m_x -= renderrect.x;
1506 mouse_event.m_y -= renderrect.y;
1507
1508 return cell->ActivateCell(renderrect, model, item, model_col, &mouse_event);
1509 }
1510 }
1511
1512 wxLogDebug("unexpected event type in gtk_wx_cell_renderer_activate()");
1513 return false;
1514 }
1515
1516 // ---------------------------------------------------------
1517 // wxGtkDataViewModelNotifier
1518 // ---------------------------------------------------------
1519
1520 class wxGtkDataViewModelNotifier: public wxDataViewModelNotifier
1521 {
1522 public:
1523 wxGtkDataViewModelNotifier( wxDataViewModel *wx_model, wxDataViewCtrlInternal *internal );
1524 ~wxGtkDataViewModelNotifier();
1525
1526 virtual bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
1527 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
1528 virtual bool ItemChanged( const wxDataViewItem &item );
1529 virtual bool ValueChanged( const wxDataViewItem &item, unsigned int model_column );
1530 virtual bool Cleared();
1531 virtual void Resort();
1532 virtual bool BeforeReset();
1533 virtual bool AfterReset();
1534
1535 void UpdateLastCount();
1536
1537 private:
1538 wxDataViewModel *m_wx_model;
1539 wxDataViewCtrlInternal *m_internal;
1540 };
1541
1542 // ---------------------------------------------------------
1543 // wxGtkDataViewListModelNotifier
1544 // ---------------------------------------------------------
1545
1546 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1547 wxDataViewModel *wx_model, wxDataViewCtrlInternal *internal )
1548 {
1549 m_wx_model = wx_model;
1550 m_internal = internal;
1551 }
1552
1553 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1554 {
1555 m_wx_model = NULL;
1556 m_internal = NULL;
1557 }
1558
1559 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
1560 {
1561 m_internal->ItemAdded( parent, item );
1562 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1563
1564 GtkTreeIter iter;
1565 iter.stamp = wxgtk_model->stamp;
1566 iter.user_data = item.GetID();
1567
1568 wxGtkTreePath path(wxgtk_tree_model_get_path(
1569 GTK_TREE_MODEL(wxgtk_model), &iter ));
1570 gtk_tree_model_row_inserted(
1571 GTK_TREE_MODEL(wxgtk_model), path, &iter);
1572
1573 return true;
1574 }
1575
1576 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
1577 {
1578 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1579 #if 0
1580 // using _get_path for a deleted item cannot be
1581 // a good idea
1582 GtkTreeIter iter;
1583 iter.stamp = wxgtk_model->stamp;
1584 iter.user_data = (gpointer) item.GetID();
1585 wxGtkTreePath path(wxgtk_tree_model_get_path(
1586 GTK_TREE_MODEL(wxgtk_model), &iter ));
1587 #else
1588 // so get the path from the parent
1589 GtkTreeIter parentIter;
1590 parentIter.stamp = wxgtk_model->stamp;
1591 parentIter.user_data = (gpointer) parent.GetID();
1592 wxGtkTreePath parentPath(wxgtk_tree_model_get_path(
1593 GTK_TREE_MODEL(wxgtk_model), &parentIter ));
1594
1595 // and add the final index ourselves
1596 wxGtkTreePath path(gtk_tree_path_copy(parentPath));
1597 int index = m_internal->GetIndexOf( parent, item );
1598 gtk_tree_path_append_index( path, index );
1599 #endif
1600
1601 gtk_tree_model_row_deleted(
1602 GTK_TREE_MODEL(wxgtk_model), path );
1603
1604 m_internal->ItemDeleted( parent, item );
1605
1606 // Did we remove the last child, causing 'parent' to become a leaf?
1607 if ( !m_wx_model->IsContainer(parent) )
1608 {
1609 gtk_tree_model_row_has_child_toggled
1610 (
1611 GTK_TREE_MODEL(wxgtk_model),
1612 parentPath,
1613 &parentIter
1614 );
1615 }
1616
1617 return true;
1618 }
1619
1620 void wxGtkDataViewModelNotifier::Resort()
1621 {
1622 m_internal->Resort();
1623 }
1624
1625 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem &item )
1626 {
1627 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1628
1629 GtkTreeIter iter;
1630 iter.stamp = wxgtk_model->stamp;
1631 iter.user_data = (gpointer) item.GetID();
1632
1633 wxGtkTreePath path(wxgtk_tree_model_get_path(
1634 GTK_TREE_MODEL(wxgtk_model), &iter ));
1635 gtk_tree_model_row_changed(
1636 GTK_TREE_MODEL(wxgtk_model), path, &iter );
1637
1638 m_internal->ItemChanged( item );
1639
1640 return true;
1641 }
1642
1643 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem &item, unsigned int model_column )
1644 {
1645 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1646 wxDataViewCtrl *ctrl = m_internal->GetOwner();
1647
1648 // This adds GTK+'s missing MVC logic for ValueChanged
1649 unsigned int index;
1650 for (index = 0; index < ctrl->GetColumnCount(); index++)
1651 {
1652 wxDataViewColumn *column = ctrl->GetColumn( index );
1653 if (column->GetModelColumn() == model_column)
1654 {
1655 GtkTreeView *widget = GTK_TREE_VIEW(ctrl->GtkGetTreeView());
1656 GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
1657
1658 // Don't attempt to refresh not yet realized tree, it is useless
1659 // and results in GTK errors.
1660 if ( gtk_widget_get_realized(ctrl->GtkGetTreeView()) )
1661 {
1662 // Get cell area
1663 GtkTreeIter iter;
1664 iter.stamp = wxgtk_model->stamp;
1665 iter.user_data = (gpointer) item.GetID();
1666 wxGtkTreePath path(wxgtk_tree_model_get_path(
1667 GTK_TREE_MODEL(wxgtk_model), &iter ));
1668 GdkRectangle cell_area;
1669 gtk_tree_view_get_cell_area( widget, path, gcolumn, &cell_area );
1670
1671 GtkAdjustment* hadjust = gtk_tree_view_get_hadjustment( widget );
1672 double d = gtk_adjustment_get_value( hadjust );
1673 int xdiff = (int) d;
1674
1675 int ydiff = gcolumn->button->allocation.height;
1676 // Redraw
1677 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
1678 cell_area.x - xdiff, ydiff + cell_area.y, cell_area.width, cell_area.height );
1679 }
1680
1681 m_internal->ValueChanged( item, model_column );
1682
1683 return true;
1684 }
1685 }
1686
1687 return false;
1688 }
1689
1690 bool wxGtkDataViewModelNotifier::BeforeReset()
1691 {
1692 GtkWidget *treeview = m_internal->GetOwner()->GtkGetTreeView();
1693 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview), NULL );
1694
1695 return true;
1696 }
1697
1698 bool wxGtkDataViewModelNotifier::AfterReset()
1699 {
1700 GtkWidget *treeview = m_internal->GetOwner()->GtkGetTreeView();
1701 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1702
1703 m_internal->Cleared();
1704
1705 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(wxgtk_model) );
1706
1707 return true;
1708 }
1709
1710 bool wxGtkDataViewModelNotifier::Cleared()
1711 {
1712 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1713
1714 // There is no call to tell the model that everything
1715 // has been deleted so call row_deleted() for every
1716 // child of root...
1717
1718 int count = m_internal->iter_n_children( NULL ); // number of children of root
1719
1720 GtkTreePath *path = gtk_tree_path_new_first(); // points to root
1721
1722 int i;
1723 for (i = 0; i < count; i++)
1724 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model), path );
1725
1726 gtk_tree_path_free( path );
1727
1728 m_internal->Cleared();
1729
1730 return true;
1731 }
1732
1733 // ---------------------------------------------------------
1734 // wxDataViewRenderer
1735 // ---------------------------------------------------------
1736
1737 static gpointer s_user_data = NULL;
1738
1739 static void
1740 wxgtk_cell_editable_editing_done( GtkCellEditable *WXUNUSED(editable),
1741 wxDataViewRenderer *wxrenderer )
1742 {
1743 wxDataViewColumn *column = wxrenderer->GetOwner();
1744 wxDataViewCtrl *dv = column->GetOwner();
1745 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv->GetId() );
1746 event.SetDataViewColumn( column );
1747 event.SetModel( dv->GetModel() );
1748 wxDataViewItem item( s_user_data );
1749 event.SetItem( item );
1750 dv->HandleWindowEvent( event );
1751 }
1752
1753 static void
1754 wxgtk_renderer_editing_started( GtkCellRenderer *WXUNUSED(cell), GtkCellEditable *editable,
1755 gchar *path, wxDataViewRenderer *wxrenderer )
1756 {
1757 if (!editable)
1758 return;
1759
1760 wxDataViewColumn *column = wxrenderer->GetOwner();
1761 wxDataViewCtrl *dv = column->GetOwner();
1762 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv->GetId() );
1763 event.SetDataViewColumn( column );
1764 event.SetModel( dv->GetModel() );
1765 wxDataViewItem item(dv->GTKPathToItem(wxGtkTreePath(path)));
1766 event.SetItem( item );
1767 dv->HandleWindowEvent( event );
1768
1769 if (GTK_IS_CELL_EDITABLE(editable))
1770 {
1771 s_user_data = item.GetID();
1772
1773 g_signal_connect (GTK_CELL_EDITABLE (editable), "editing_done",
1774 G_CALLBACK (wxgtk_cell_editable_editing_done),
1775 (gpointer) wxrenderer );
1776
1777 }
1778 }
1779
1780
1781 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
1782
1783 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1784 int align ) :
1785 wxDataViewRendererBase( varianttype, mode, align )
1786 {
1787 m_renderer = NULL;
1788 m_mode = mode;
1789
1790 // we haven't changed them yet
1791 m_usingDefaultAttrs = true;
1792
1793 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1794 // after the m_renderer pointer has been initialized
1795 }
1796
1797 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
1798 {
1799 gtk_tree_view_column_pack_end( column, m_renderer, TRUE /* expand */);
1800 }
1801
1802 void wxDataViewRenderer::GtkInitHandlers()
1803 {
1804 if (!gtk_check_version(2,6,0))
1805 {
1806 g_signal_connect (GTK_CELL_RENDERER(m_renderer), "editing_started",
1807 G_CALLBACK (wxgtk_renderer_editing_started),
1808 this);
1809 }
1810 }
1811
1812 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
1813 {
1814 GtkCellRendererMode gtkMode;
1815 switch (mode)
1816 {
1817 case wxDATAVIEW_CELL_INERT:
1818 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
1819 break;
1820
1821 case wxDATAVIEW_CELL_ACTIVATABLE:
1822 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
1823 break;
1824
1825 case wxDATAVIEW_CELL_EDITABLE:
1826 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
1827 break;
1828
1829 default:
1830 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1831 return;
1832 }
1833
1834 m_mode = mode;
1835
1836 // This value is most often ignored in GtkTreeView
1837 GValue gvalue = { 0, };
1838 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1839 g_value_set_enum( &gvalue, gtkMode );
1840 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1841 g_value_unset( &gvalue );
1842 }
1843
1844 wxDataViewCellMode wxDataViewRenderer::GetMode() const
1845 {
1846 wxDataViewCellMode ret;
1847
1848 GValue gvalue;
1849 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1850
1851 switch (g_value_get_enum(&gvalue))
1852 {
1853 default:
1854 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1855 // fall through (we have to return something)
1856
1857 case GTK_CELL_RENDERER_MODE_INERT:
1858 ret = wxDATAVIEW_CELL_INERT;
1859 break;
1860
1861 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1862 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1863 break;
1864
1865 case GTK_CELL_RENDERER_MODE_EDITABLE:
1866 ret = wxDATAVIEW_CELL_EDITABLE;
1867 break;
1868 }
1869
1870 g_value_unset( &gvalue );
1871
1872 return ret;
1873 }
1874
1875 void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer *renderer)
1876 {
1877 int align = m_alignment;
1878
1879 // query alignment from column ?
1880 if (align == -1)
1881 {
1882 // None there yet
1883 if (GetOwner() == NULL)
1884 return;
1885
1886 align = GetOwner()->GetAlignment();
1887 align |= wxALIGN_CENTRE_VERTICAL;
1888 }
1889
1890 // horizontal alignment:
1891
1892 gfloat xalign = 0.0;
1893 if (align & wxALIGN_RIGHT)
1894 xalign = 1.0;
1895 else if (align & wxALIGN_CENTER_HORIZONTAL)
1896 xalign = 0.5;
1897
1898 GValue gvalue = { 0, };
1899 g_value_init( &gvalue, G_TYPE_FLOAT );
1900 g_value_set_float( &gvalue, xalign );
1901 g_object_set_property( G_OBJECT(renderer), "xalign", &gvalue );
1902 g_value_unset( &gvalue );
1903
1904 // vertical alignment:
1905
1906 gfloat yalign = 0.0;
1907 if (align & wxALIGN_BOTTOM)
1908 yalign = 1.0;
1909 else if (align & wxALIGN_CENTER_VERTICAL)
1910 yalign = 0.5;
1911
1912 GValue gvalue2 = { 0, };
1913 g_value_init( &gvalue2, G_TYPE_FLOAT );
1914 g_value_set_float( &gvalue2, yalign );
1915 g_object_set_property( G_OBJECT(renderer), "yalign", &gvalue2 );
1916 g_value_unset( &gvalue2 );
1917 }
1918
1919 void wxDataViewRenderer::SetAlignment( int align )
1920 {
1921 m_alignment = align;
1922 GtkUpdateAlignment();
1923 }
1924
1925 int wxDataViewRenderer::GetAlignment() const
1926 {
1927 return m_alignment;
1928 }
1929
1930 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode)
1931 {
1932 #ifdef __WXGTK26__
1933 if ( gtk_check_version(2, 6, 0) != NULL )
1934 return;
1935
1936 GtkCellRendererText * const rend = GtkGetTextRenderer();
1937 if ( !rend )
1938 return;
1939
1940 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1941 // can just cast between them
1942 GValue gvalue = { 0, };
1943 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1944 g_value_set_enum( &gvalue, static_cast<PangoEllipsizeMode>(mode) );
1945 g_object_set_property( G_OBJECT(rend), "ellipsize", &gvalue );
1946 g_value_unset( &gvalue );
1947 #else // GTK < 2.6
1948 wxUnusedVar(mode);
1949 #endif // GTK 2.6/before
1950 }
1951
1952 wxEllipsizeMode wxDataViewRenderer::GetEllipsizeMode() const
1953 {
1954 #ifdef __WXGTK26__
1955 if ( gtk_check_version(2, 6, 0) != NULL )
1956 return wxELLIPSIZE_NONE;
1957
1958 GtkCellRendererText * const rend = GtkGetTextRenderer();
1959 if ( !rend )
1960 return wxELLIPSIZE_NONE;
1961
1962 GValue gvalue = { 0, };
1963 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1964 g_object_get_property( G_OBJECT(rend), "ellipsize", &gvalue );
1965 wxEllipsizeMode
1966 mode = static_cast<wxEllipsizeMode>(g_value_get_enum( &gvalue ));
1967 g_value_unset( &gvalue );
1968
1969 return mode;
1970 #else // GTK < 2.6
1971 return wxELLIPSIZE_NONE;
1972 #endif // GTK 2.6/before
1973 }
1974
1975 void
1976 wxDataViewRenderer::GtkOnTextEdited(const char *itempath, const wxString& str)
1977 {
1978 wxVariant value(str);
1979 if (!Validate( value ))
1980 return;
1981
1982 wxDataViewItem
1983 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
1984
1985 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
1986 }
1987
1988 void
1989 wxDataViewRenderer::GtkOnCellChanged(const wxVariant& value,
1990 const wxDataViewItem& item,
1991 unsigned col)
1992 {
1993 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
1994 model->ChangeValue( value, item, col );
1995 }
1996
1997 // ---------------------------------------------------------
1998 // wxDataViewTextRenderer
1999 // ---------------------------------------------------------
2000
2001 extern "C"
2002 {
2003
2004 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *WXUNUSED(renderer),
2005 gchar *arg1, gchar *arg2, gpointer user_data )
2006 {
2007 wxDataViewRenderer *cell = (wxDataViewRenderer*) user_data;
2008
2009 cell->GtkOnTextEdited(arg1, wxGTK_CONV_BACK_FONT(
2010 arg2, cell->GetOwner()->GetOwner()->GetFont()));
2011 }
2012
2013 }
2014
2015 namespace
2016 {
2017
2018 // helper function used by wxDataViewTextRenderer and
2019 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
2020 // given text renderer and returns true if anything was done
2021 bool GtkApplyAttr(GtkCellRendererText *renderer, const wxDataViewItemAttr& attr)
2022 {
2023 bool usingDefaultAttrs = true;
2024 if (attr.HasColour())
2025 {
2026 const GdkColor * const gcol = attr.GetColour().GetColor();
2027
2028 GValue gvalue = { 0, };
2029 g_value_init( &gvalue, GDK_TYPE_COLOR );
2030 g_value_set_boxed( &gvalue, gcol );
2031 g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
2032 g_value_unset( &gvalue );
2033
2034 usingDefaultAttrs = false;
2035 }
2036 else
2037 {
2038 GValue gvalue = { 0, };
2039 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2040 g_value_set_boolean( &gvalue, FALSE );
2041 g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
2042 g_value_unset( &gvalue );
2043 }
2044
2045 if (attr.GetItalic())
2046 {
2047 GValue gvalue = { 0, };
2048 g_value_init( &gvalue, PANGO_TYPE_STYLE );
2049 g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
2050 g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
2051 g_value_unset( &gvalue );
2052
2053 usingDefaultAttrs = false;
2054 }
2055 else
2056 {
2057 GValue gvalue = { 0, };
2058 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2059 g_value_set_boolean( &gvalue, FALSE );
2060 g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
2061 g_value_unset( &gvalue );
2062 }
2063
2064
2065 if (attr.GetBold())
2066 {
2067 GValue gvalue = { 0, };
2068 g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
2069 g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
2070 g_object_set_property( G_OBJECT(renderer), "weight", &gvalue );
2071 g_value_unset( &gvalue );
2072
2073 usingDefaultAttrs = false;
2074 }
2075 else
2076 {
2077 GValue gvalue = { 0, };
2078 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2079 g_value_set_boolean( &gvalue, FALSE );
2080 g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue );
2081 g_value_unset( &gvalue );
2082 }
2083
2084 #if 0
2085 if (attr.HasBackgroundColour())
2086 {
2087 wxColour colour = attr.GetBackgroundColour();
2088 const GdkColor * const gcol = colour.GetColor();
2089
2090 GValue gvalue = { 0, };
2091 g_value_init( &gvalue, GDK_TYPE_COLOR );
2092 g_value_set_boxed( &gvalue, gcol );
2093 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
2094 g_value_unset( &gvalue );
2095 }
2096 else
2097 {
2098 GValue gvalue = { 0, };
2099 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2100 g_value_set_boolean( &gvalue, FALSE );
2101 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
2102 g_value_unset( &gvalue );
2103 }
2104 #endif
2105
2106 return !usingDefaultAttrs;
2107 }
2108
2109 } // anonymous namespace
2110
2111 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
2112
2113 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2114 int align ) :
2115 wxDataViewRenderer( varianttype, mode, align )
2116 {
2117 GtkWxCellRendererText *text_renderer = gtk_wx_cell_renderer_text_new();
2118 text_renderer->wx_renderer = this;
2119 m_renderer = (GtkCellRenderer*) text_renderer;
2120
2121 if (mode & wxDATAVIEW_CELL_EDITABLE)
2122 {
2123 GValue gvalue = { 0, };
2124 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2125 g_value_set_boolean( &gvalue, true );
2126 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
2127 g_value_unset( &gvalue );
2128
2129 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2130
2131 GtkInitHandlers();
2132 }
2133
2134 SetMode(mode);
2135 SetAlignment(align);
2136 }
2137
2138 bool wxDataViewTextRenderer::SetTextValue(const wxString& str)
2139 {
2140 GValue gvalue = { 0, };
2141 g_value_init( &gvalue, G_TYPE_STRING );
2142 g_value_set_string( &gvalue, wxGTK_CONV_FONT( str, GetOwner()->GetOwner()->GetFont() ) );
2143 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2144 g_value_unset( &gvalue );
2145
2146 return true;
2147 }
2148
2149 bool wxDataViewTextRenderer::GetTextValue(wxString& str) const
2150 {
2151 GValue gvalue = { 0, };
2152 g_value_init( &gvalue, G_TYPE_STRING );
2153 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
2154 str = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), const_cast<wxDataViewTextRenderer*>(this)->GetOwner()->GetOwner()->GetFont() );
2155 g_value_unset( &gvalue );
2156
2157 return true;
2158 }
2159
2160 void wxDataViewTextRenderer::SetAlignment( int align )
2161 {
2162 wxDataViewRenderer::SetAlignment(align);
2163
2164 if (gtk_check_version(2,10,0))
2165 return;
2166
2167 // horizontal alignment:
2168 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2169 if (align & wxALIGN_RIGHT)
2170 pangoAlign = PANGO_ALIGN_RIGHT;
2171 else if (align & wxALIGN_CENTER_HORIZONTAL)
2172 pangoAlign = PANGO_ALIGN_CENTER;
2173
2174 GValue gvalue = { 0, };
2175 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2176 g_value_set_enum( &gvalue, pangoAlign );
2177 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2178 g_value_unset( &gvalue );
2179 }
2180
2181 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr& attr)
2182 {
2183 return GtkApplyAttr(GtkGetTextRenderer(), attr);
2184 }
2185
2186 GtkCellRendererText *wxDataViewTextRenderer::GtkGetTextRenderer() const
2187 {
2188 return GTK_CELL_RENDERER_TEXT(m_renderer);
2189 }
2190
2191 // ---------------------------------------------------------
2192 // wxDataViewBitmapRenderer
2193 // ---------------------------------------------------------
2194
2195 namespace
2196 {
2197
2198 // set "pixbuf" property on the given renderer
2199 void SetPixbufProp(GtkCellRenderer *renderer, GdkPixbuf *pixbuf)
2200 {
2201 GValue gvalue = { 0, };
2202 g_value_init( &gvalue, G_TYPE_OBJECT );
2203 g_value_set_object( &gvalue, pixbuf );
2204 g_object_set_property( G_OBJECT(renderer), "pixbuf", &gvalue );
2205 g_value_unset( &gvalue );
2206 }
2207
2208 } // anonymous namespace
2209
2210 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
2211
2212 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2213 int align ) :
2214 wxDataViewRenderer( varianttype, mode, align )
2215 {
2216 m_renderer = gtk_cell_renderer_pixbuf_new();
2217
2218 SetMode(mode);
2219 SetAlignment(align);
2220 }
2221
2222 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
2223 {
2224 if (value.GetType() == wxT("wxBitmap"))
2225 {
2226 wxBitmap bitmap;
2227 bitmap << value;
2228
2229 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2230 // object (and it will stay there and remain owned by wxBitmap)
2231 SetPixbufProp(m_renderer, bitmap.GetPixbuf());
2232 }
2233 else if (value.GetType() == wxT("wxIcon"))
2234 {
2235 wxIcon icon;
2236 icon << value;
2237
2238 SetPixbufProp(m_renderer, icon.GetPixbuf());
2239 }
2240 else
2241 {
2242 return false;
2243 }
2244
2245 return true;
2246 }
2247
2248 bool wxDataViewBitmapRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2249 {
2250 return false;
2251 }
2252
2253 // ---------------------------------------------------------
2254 // wxDataViewToggleRenderer
2255 // ---------------------------------------------------------
2256
2257 extern "C" {
2258 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
2259 gchar *path, gpointer user_data );
2260 }
2261
2262 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
2263 gchar *path, gpointer user_data )
2264 {
2265 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
2266
2267 // get old value
2268 GValue gvalue = { 0, };
2269 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2270 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
2271 bool tmp = g_value_get_boolean( &gvalue );
2272 g_value_unset( &gvalue );
2273 // invert it
2274 tmp = !tmp;
2275
2276 wxVariant value = tmp;
2277 if (!cell->Validate( value ))
2278 return;
2279
2280 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
2281 wxDataViewModel *model = ctrl->GetModel();
2282
2283 wxDataViewItem item(ctrl->GTKPathToItem(wxGtkTreePath(path)));
2284
2285 unsigned int model_col = cell->GetOwner()->GetModelColumn();
2286
2287 model->ChangeValue( value, item, model_col );
2288 }
2289
2290 IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
2291
2292 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
2293 wxDataViewCellMode mode, int align ) :
2294 wxDataViewRenderer( varianttype, mode, align )
2295 {
2296 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
2297
2298 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
2299 {
2300 g_signal_connect_after( m_renderer, "toggled",
2301 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
2302 }
2303 else
2304 {
2305 GValue gvalue = { 0, };
2306 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2307 g_value_set_boolean( &gvalue, false );
2308 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
2309 g_value_unset( &gvalue );
2310 }
2311
2312 SetMode(mode);
2313 SetAlignment(align);
2314 }
2315
2316 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
2317 {
2318 bool tmp = value;
2319
2320 GValue gvalue = { 0, };
2321 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2322 g_value_set_boolean( &gvalue, tmp );
2323 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
2324 g_value_unset( &gvalue );
2325
2326 return true;
2327 }
2328
2329 bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
2330 {
2331 GValue gvalue = { 0, };
2332 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2333 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
2334 bool tmp = g_value_get_boolean( &gvalue );
2335 g_value_unset( &gvalue );
2336
2337 value = tmp;
2338
2339 return true;
2340 }
2341
2342 // ---------------------------------------------------------
2343 // wxDataViewCustomRenderer
2344 // ---------------------------------------------------------
2345
2346 class wxDataViewCtrlDCImpl: public wxWindowDCImpl
2347 {
2348 public:
2349 wxDataViewCtrlDCImpl( wxDC *owner, wxDataViewCtrl *window ) :
2350 wxWindowDCImpl( owner )
2351 {
2352 GtkWidget *widget = window->m_treeview;
2353 // Set later
2354 m_gdkwindow = NULL;
2355
2356 m_window = window;
2357
2358 m_context = window->GTKGetPangoDefaultContext();
2359 m_layout = pango_layout_new( m_context );
2360 m_fontdesc = pango_font_description_copy(gtk_widget_get_style(widget)->font_desc);
2361
2362 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
2363
2364 // Set m_gdkwindow later
2365 // SetUpDC();
2366 }
2367 };
2368
2369 class wxDataViewCtrlDC: public wxWindowDC
2370 {
2371 public:
2372 wxDataViewCtrlDC( wxDataViewCtrl *window ) :
2373 wxWindowDC( new wxDataViewCtrlDCImpl( this, window ) )
2374 { }
2375 };
2376
2377
2378 // ---------------------------------------------------------
2379 // wxDataViewCustomRenderer
2380 // ---------------------------------------------------------
2381
2382 IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
2383
2384 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
2385 wxDataViewCellMode mode,
2386 int align,
2387 bool no_init )
2388 : wxDataViewCustomRendererBase( varianttype, mode, align )
2389 {
2390 m_dc = NULL;
2391 m_text_renderer = NULL;
2392
2393 if (no_init)
2394 m_renderer = NULL;
2395 else
2396 Init(mode, align);
2397 }
2398
2399 void wxDataViewCustomRenderer::GtkInitTextRenderer()
2400 {
2401 m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2402 g_object_ref_sink(m_text_renderer);
2403
2404 GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer));
2405 }
2406
2407 GtkCellRendererText *wxDataViewCustomRenderer::GtkGetTextRenderer() const
2408 {
2409 if ( !m_text_renderer )
2410 {
2411 // we create it on demand so need to do it even from a const function
2412 const_cast<wxDataViewCustomRenderer *>(this)->GtkInitTextRenderer();
2413 }
2414
2415 return m_text_renderer;
2416 }
2417
2418 void wxDataViewCustomRenderer::RenderText( const wxString &text,
2419 int xoffset,
2420 wxRect cell,
2421 wxDC *WXUNUSED(dc),
2422 int WXUNUSED(state) )
2423 {
2424
2425 GtkCellRendererText * const textRenderer = GtkGetTextRenderer();
2426
2427 GValue gvalue = { 0, };
2428 g_value_init( &gvalue, G_TYPE_STRING );
2429 g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) );
2430 g_object_set_property( G_OBJECT(textRenderer), "text", &gvalue );
2431 g_value_unset( &gvalue );
2432
2433 GtkApplyAttr(textRenderer, GetAttr());
2434
2435 GdkRectangle cell_area;
2436 wxRectToGDKRect(cell, cell_area);
2437 cell_area.x += xoffset;
2438 cell_area.width -= xoffset;
2439
2440 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer),
2441 m_renderParams.window,
2442 m_renderParams.widget,
2443 m_renderParams.background_area,
2444 &cell_area,
2445 m_renderParams.expose_area,
2446 (GtkCellRendererState) m_renderParams.flags );
2447 }
2448
2449 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
2450 {
2451 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
2452 renderer->cell = this;
2453
2454 m_renderer = (GtkCellRenderer*) renderer;
2455
2456 SetMode(mode);
2457 SetAlignment(align);
2458
2459 GtkInitHandlers();
2460
2461 return true;
2462 }
2463
2464 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2465 {
2466 if (m_dc)
2467 delete m_dc;
2468
2469 if (m_text_renderer)
2470 g_object_unref(m_text_renderer);
2471 }
2472
2473 wxDC *wxDataViewCustomRenderer::GetDC()
2474 {
2475 if (m_dc == NULL)
2476 {
2477 if (GetOwner() == NULL)
2478 return NULL;
2479 if (GetOwner()->GetOwner() == NULL)
2480 return NULL;
2481 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2482 }
2483
2484 return m_dc;
2485 }
2486
2487 // ---------------------------------------------------------
2488 // wxDataViewProgressRenderer
2489 // ---------------------------------------------------------
2490
2491 IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
2492
2493 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
2494 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
2495 wxDataViewCustomRenderer( varianttype, mode, align, true )
2496 {
2497 m_label = label;
2498 m_value = 0;
2499
2500 #ifdef __WXGTK26__
2501 if (!gtk_check_version(2,6,0))
2502 {
2503 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
2504
2505 SetMode(mode);
2506 SetAlignment(align);
2507
2508 #if !wxUSE_UNICODE
2509 // We can't initialize the renderer just yet because we don't have the
2510 // pointer to the column that uses this renderer yet and so attempt to
2511 // dereference GetOwner() to get the font that is used as a source of
2512 // encoding in multibyte-to-Unicode conversion in GTKSetLabel() in
2513 // non-Unicode builds would crash. So simply remember to do it later.
2514 if ( !m_label.empty() )
2515 m_needsToSetLabel = true;
2516 else
2517 #endif // !wxUSE_UNICODE
2518 GTKSetLabel();
2519 }
2520 else
2521 #endif
2522 {
2523 // Use custom cell code
2524 wxDataViewCustomRenderer::Init(mode, align);
2525 }
2526 }
2527
2528 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2529 {
2530 }
2531
2532 void wxDataViewProgressRenderer::GTKSetLabel()
2533 {
2534 GValue gvalue = { 0, };
2535 g_value_init( &gvalue, G_TYPE_STRING );
2536
2537 // Take care to not use GetOwner() here if the label is empty, we can be
2538 // called from ctor when GetOwner() is still NULL in this case.
2539 wxScopedCharBuffer buf;
2540 if ( m_label.empty() )
2541 buf = wxScopedCharBuffer::CreateNonOwned("");
2542 else
2543 buf = wxGTK_CONV_FONT(m_label, GetOwner()->GetOwner()->GetFont());
2544
2545 g_value_set_string( &gvalue, buf);
2546 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2547 g_value_unset( &gvalue );
2548
2549 #if !wxUSE_UNICODE
2550 m_needsToSetLabel = false;
2551 #endif // !wxUSE_UNICODE
2552 }
2553
2554 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
2555 {
2556 #ifdef __WXGTK26__
2557 if (!gtk_check_version(2,6,0))
2558 {
2559 #if !wxUSE_UNICODE
2560 if ( m_needsToSetLabel )
2561 GTKSetLabel();
2562 #endif // !wxUSE_UNICODE
2563
2564 gint tmp = (long) value;
2565 GValue gvalue = { 0, };
2566 g_value_init( &gvalue, G_TYPE_INT );
2567 g_value_set_int( &gvalue, tmp );
2568 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
2569 g_value_unset( &gvalue );
2570 }
2571 else
2572 #endif
2573 {
2574 m_value = (long) value;
2575
2576 if (m_value < 0) m_value = 0;
2577 if (m_value > 100) m_value = 100;
2578 }
2579
2580 return true;
2581 }
2582
2583 bool wxDataViewProgressRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2584 {
2585 return false;
2586 }
2587
2588 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
2589 {
2590 double pct = (double)m_value / 100.0;
2591 wxRect bar = cell;
2592 bar.width = (int)(cell.width * pct);
2593 dc->SetPen( *wxTRANSPARENT_PEN );
2594 dc->SetBrush( *wxBLUE_BRUSH );
2595 dc->DrawRectangle( bar );
2596
2597 dc->SetBrush( *wxTRANSPARENT_BRUSH );
2598 dc->SetPen( *wxBLACK_PEN );
2599 dc->DrawRectangle( cell );
2600
2601 return true;
2602 }
2603
2604 wxSize wxDataViewProgressRenderer::GetSize() const
2605 {
2606 return wxSize(40,12);
2607 }
2608
2609 // -------------------------------------
2610 // wxDataViewChoiceRenderer
2611 // -------------------------------------
2612
2613 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString &choices,
2614 wxDataViewCellMode mode, int alignment ) :
2615 wxDataViewCustomRenderer( "string", mode, alignment, true )
2616 {
2617 m_choices = choices;
2618
2619 #ifdef __WXGTK26__
2620 if (!gtk_check_version(2,6,0))
2621 {
2622 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_combo_new();
2623
2624 GtkListStore *store = gtk_list_store_new( 1, G_TYPE_STRING );
2625 for (size_t n = 0; n < m_choices.GetCount(); n++)
2626 {
2627 gtk_list_store_insert_with_values(
2628 store, NULL, n, 0,
2629 static_cast<const char *>(m_choices[n].utf8_str()), -1 );
2630 }
2631
2632 g_object_set (m_renderer,
2633 "model", store,
2634 "text-column", 0,
2635 "has-entry", FALSE,
2636 NULL);
2637
2638 bool editable = (mode & wxDATAVIEW_CELL_EDITABLE);
2639 g_object_set (m_renderer, "editable", editable, NULL);
2640
2641 SetAlignment(alignment);
2642
2643 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2644
2645 GtkInitHandlers();
2646 }
2647 else
2648 #endif
2649 {
2650 // Use custom cell code
2651 wxDataViewCustomRenderer::Init(mode, alignment);
2652 }
2653 }
2654
2655 bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
2656 {
2657 RenderText( m_data, 0, rect, dc, state );
2658 return true;
2659 }
2660
2661 wxSize wxDataViewChoiceRenderer::GetSize() const
2662 {
2663 return wxSize(70,20);
2664 }
2665
2666 bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
2667 {
2668
2669 #ifdef __WXGTK26__
2670 if (!gtk_check_version(2,6,0))
2671 {
2672 GValue gvalue = { 0, };
2673 g_value_init( &gvalue, G_TYPE_STRING );
2674 g_value_set_string(&gvalue,
2675 wxGTK_CONV_FONT(value.GetString(),
2676 GetOwner()->GetOwner()->GetFont()));
2677 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2678 g_value_unset( &gvalue );
2679 }
2680 else
2681 #endif
2682 m_data = value.GetString();
2683
2684 return true;
2685 }
2686
2687 bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
2688 {
2689 #ifdef __WXGTK26__
2690 if (!gtk_check_version(2,6,0))
2691 {
2692 GValue gvalue = { 0, };
2693 g_value_init( &gvalue, G_TYPE_STRING );
2694 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
2695 wxString temp = wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue),
2696 GetOwner()->GetOwner()->GetFont());
2697 g_value_unset( &gvalue );
2698 value = temp;
2699
2700 //wxPrintf( "temp %s\n", temp );
2701 // TODO: remove this code
2702 }
2703 else
2704 #endif
2705 value = m_data;
2706
2707 return true;
2708 }
2709
2710 void wxDataViewChoiceRenderer::SetAlignment( int align )
2711 {
2712 wxDataViewCustomRenderer::SetAlignment(align);
2713
2714 if (gtk_check_version(2,10,0))
2715 return;
2716
2717 // horizontal alignment:
2718 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2719 if (align & wxALIGN_RIGHT)
2720 pangoAlign = PANGO_ALIGN_RIGHT;
2721 else if (align & wxALIGN_CENTER_HORIZONTAL)
2722 pangoAlign = PANGO_ALIGN_CENTER;
2723
2724 GValue gvalue = { 0, };
2725 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2726 g_value_set_enum( &gvalue, pangoAlign );
2727 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2728 g_value_unset( &gvalue );
2729 }
2730
2731 // ----------------------------------------------------------------------------
2732 // wxDataViewChoiceByIndexRenderer
2733 // ----------------------------------------------------------------------------
2734
2735 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
2736 wxDataViewCellMode mode, int alignment ) :
2737 wxDataViewChoiceRenderer( choices, mode, alignment )
2738 {
2739 }
2740
2741 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const char *itempath, const wxString& str)
2742 {
2743 wxVariant value( (long) GetChoices().Index( str ) );
2744
2745 if (!Validate( value ))
2746 return;
2747
2748 wxDataViewItem
2749 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
2750
2751 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
2752 }
2753
2754 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
2755 {
2756 wxVariant string_value = GetChoice( value.GetLong() );
2757 return wxDataViewChoiceRenderer::SetValue( string_value );
2758 }
2759
2760 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const
2761 {
2762 wxVariant string_value;
2763 if (!wxDataViewChoiceRenderer::GetValue( string_value ))
2764 return false;
2765
2766 value = (long) GetChoices().Index( string_value.GetString() );
2767 return true;
2768 }
2769
2770 // ---------------------------------------------------------
2771 // wxDataViewIconTextRenderer
2772 // ---------------------------------------------------------
2773
2774 IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
2775
2776 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2777 (
2778 const wxString &varianttype,
2779 wxDataViewCellMode mode,
2780 int align
2781 )
2782 : wxDataViewTextRenderer(varianttype, mode, align)
2783 {
2784 m_rendererIcon = gtk_cell_renderer_pixbuf_new();
2785 }
2786
2787 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2788 {
2789 }
2790
2791 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
2792 {
2793 // add the icon renderer first
2794 gtk_tree_view_column_pack_start(column, m_rendererIcon, FALSE /* !expand */);
2795
2796 // add the text renderer too
2797 wxDataViewRenderer::GtkPackIntoColumn(column);
2798 }
2799
2800 bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
2801 {
2802 m_value << value;
2803
2804 SetTextValue(m_value.GetText());
2805 SetPixbufProp(m_rendererIcon, m_value.GetIcon().GetPixbuf());
2806
2807 return true;
2808 }
2809
2810 bool wxDataViewIconTextRenderer::GetValue(wxVariant& value) const
2811 {
2812 wxString str;
2813 if ( !GetTextValue(str) )
2814 return false;
2815
2816 // user doesn't have any way to edit the icon so leave it unchanged
2817 value << wxDataViewIconText(str, m_value.GetIcon());
2818
2819 return true;
2820 }
2821
2822 void
2823 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant& value,
2824 const wxDataViewItem& item,
2825 unsigned col)
2826 {
2827 // we receive just the text part of our value as it's the only one which
2828 // can be edited, but we need the full wxDataViewIconText value for the
2829 // model
2830 wxVariant valueIconText;
2831 valueIconText << wxDataViewIconText(value.GetString(), m_value.GetIcon());
2832 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText, item, col);
2833 }
2834
2835 // ---------------------------------------------------------
2836 // wxDataViewColumn
2837 // ---------------------------------------------------------
2838
2839
2840 static gboolean
2841 gtk_dataview_header_button_press_callback( GtkWidget *WXUNUSED(widget),
2842 GdkEventButton *gdk_event,
2843 wxDataViewColumn *column )
2844 {
2845 if (gdk_event->type != GDK_BUTTON_PRESS)
2846 return FALSE;
2847
2848 if (gdk_event->button == 1)
2849 {
2850 gs_lastLeftClickHeader = column;
2851
2852 wxDataViewCtrl *dv = column->GetOwner();
2853 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
2854 event.SetDataViewColumn( column );
2855 event.SetModel( dv->GetModel() );
2856 if (dv->HandleWindowEvent( event ))
2857 return FALSE;
2858 }
2859
2860 if (gdk_event->button == 3)
2861 {
2862 wxDataViewCtrl *dv = column->GetOwner();
2863 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, dv->GetId() );
2864 event.SetDataViewColumn( column );
2865 event.SetModel( dv->GetModel() );
2866 if (dv->HandleWindowEvent( event ))
2867 return FALSE;
2868 }
2869
2870 return FALSE;
2871 }
2872
2873 extern "C"
2874 {
2875
2876 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *WXUNUSED(column),
2877 GtkCellRenderer *renderer,
2878 GtkTreeModel *model,
2879 GtkTreeIter *iter,
2880 gpointer data )
2881 {
2882 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
2883 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
2884
2885 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
2886
2887 wxDataViewItem item( (void*) iter->user_data );
2888
2889 wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel();
2890
2891 if (!wx_model->IsVirtualListModel())
2892 {
2893 gboolean visible;
2894 if (wx_model->IsContainer( item ))
2895 {
2896 visible = wx_model->HasContainerColumns( item ) ||
2897 (cell->GetOwner()->GetModelColumn() == 0);
2898 }
2899 else
2900 {
2901 visible = true;
2902 }
2903
2904 GValue gvalue = { 0, };
2905 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2906 g_value_set_boolean( &gvalue, visible );
2907 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2908 g_value_unset( &gvalue );
2909
2910 if ( !visible )
2911 return;
2912 }
2913
2914 wxVariant value;
2915 wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
2916
2917 if (value.GetType() != cell->GetVariantType())
2918 {
2919 wxLogError( wxT("Wrong type, required: %s but: %s"),
2920 value.GetType().c_str(),
2921 cell->GetVariantType().c_str() );
2922 }
2923
2924 cell->SetValue( value );
2925
2926 // deal with disabled items
2927 bool enabled = wx_model->IsEnabled( item, cell->GetOwner()->GetModelColumn() );
2928
2929 // a) this sets the appearance to disabled grey
2930 GValue gvalue = { 0, };
2931 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2932 g_value_set_boolean( &gvalue, enabled );
2933 g_object_set_property( G_OBJECT(renderer), "sensitive", &gvalue );
2934 g_value_unset( &gvalue );
2935
2936 // b) this actually disables the control/renderer
2937 if (enabled)
2938 cell->SetMode( cell->GtkGetMode() );
2939 else
2940 cell->SetMode( wxDATAVIEW_CELL_INERT );
2941
2942
2943 // deal with attributes: if the renderer doesn't support them at all, we
2944 // don't even need to query the model for them
2945 if ( !cell->GtkSupportsAttrs() )
2946 return;
2947
2948 // it can support attributes so check if this item has any
2949 wxDataViewItemAttr attr;
2950 if ( wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr )
2951 || !cell->GtkIsUsingDefaultAttrs() )
2952 {
2953 bool usingDefaultAttrs = !cell->GtkSetAttr(attr);
2954 cell->GtkSetUsingDefaultAttrs(usingDefaultAttrs);
2955 }
2956 // else: no custom attributes specified and we're already using the default
2957 // ones -- nothing to do
2958
2959 }
2960
2961 } // extern "C"
2962
2963 #include <wx/listimpl.cpp>
2964 WX_DEFINE_LIST(wxDataViewColumnList)
2965
2966 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
2967 unsigned int model_column, int width,
2968 wxAlignment align, int flags )
2969 : wxDataViewColumnBase( cell, model_column )
2970 {
2971 Init( align, flags, width );
2972
2973 SetTitle( title );
2974 }
2975
2976 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
2977 unsigned int model_column, int width,
2978 wxAlignment align, int flags )
2979 : wxDataViewColumnBase( bitmap, cell, model_column )
2980 {
2981 Init( align, flags, width );
2982
2983 SetBitmap( bitmap );
2984 }
2985
2986 void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
2987 {
2988 m_isConnected = false;
2989
2990 GtkTreeViewColumn *column = gtk_tree_view_column_new();
2991 m_column = (GtkWidget*) column;
2992
2993 SetFlags( flags );
2994 SetAlignment( align );
2995
2996 SetWidth( width );
2997
2998 // Create container for icon and label
2999 GtkWidget *box = gtk_hbox_new( FALSE, 1 );
3000 gtk_widget_show( box );
3001 // gtk_container_set_border_width((GtkContainer*)box, 2);
3002 m_image = gtk_image_new();
3003 gtk_box_pack_start(GTK_BOX(box), m_image, FALSE, FALSE, 1);
3004 m_label = gtk_label_new("");
3005 gtk_box_pack_end( GTK_BOX(box), GTK_WIDGET(m_label), FALSE, FALSE, 1 );
3006 gtk_tree_view_column_set_widget( column, box );
3007
3008 wxDataViewRenderer * const colRenderer = GetRenderer();
3009 GtkCellRenderer * const cellRenderer = colRenderer->GetGtkHandle();
3010
3011 colRenderer->GtkPackIntoColumn(column);
3012
3013 gtk_tree_view_column_set_cell_data_func( column, cellRenderer,
3014 wxGtkTreeCellDataFunc, (gpointer) colRenderer, NULL );
3015 }
3016
3017 void wxDataViewColumn::OnInternalIdle()
3018 {
3019 if (m_isConnected)
3020 return;
3021
3022 if (gtk_widget_get_realized(GetOwner()->m_treeview))
3023 {
3024 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3025 if (column->button)
3026 {
3027 g_signal_connect(column->button, "button_press_event",
3028 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
3029
3030 // otherwise the event will be blocked by GTK+
3031 gtk_tree_view_column_set_clickable( column, TRUE );
3032
3033 m_isConnected = true;
3034 }
3035 }
3036 }
3037
3038 void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
3039 {
3040 wxDataViewColumnBase::SetOwner( owner );
3041
3042 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3043
3044 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3045 }
3046
3047 void wxDataViewColumn::SetTitle( const wxString &title )
3048 {
3049 wxDataViewCtrl *ctrl = GetOwner();
3050 gtk_label_set_text( GTK_LABEL(m_label), ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
3051 : wxGTK_CONV_SYS(title) );
3052 if (title.empty())
3053 gtk_widget_hide( m_label );
3054 else
3055 gtk_widget_show( m_label );
3056 }
3057
3058 wxString wxDataViewColumn::GetTitle() const
3059 {
3060 return wxGTK_CONV_BACK_FONT(
3061 gtk_label_get_text( GTK_LABEL(m_label) ),
3062 GetOwner()->GetFont()
3063 );
3064 }
3065
3066 void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
3067 {
3068 wxDataViewColumnBase::SetBitmap( bitmap );
3069
3070 if (bitmap.IsOk())
3071 {
3072 GtkImage *gtk_image = GTK_IMAGE(m_image);
3073
3074 GdkBitmap *mask = NULL;
3075 if (bitmap.GetMask())
3076 mask = bitmap.GetMask()->GetBitmap();
3077
3078 if (bitmap.HasPixbuf())
3079 {
3080 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
3081 bitmap.GetPixbuf());
3082 }
3083 else
3084 {
3085 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
3086 bitmap.GetPixmap(), mask);
3087 }
3088 gtk_widget_show( m_image );
3089 }
3090 else
3091 {
3092 gtk_widget_hide( m_image );
3093 }
3094 }
3095
3096 void wxDataViewColumn::SetHidden( bool hidden )
3097 {
3098 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
3099 }
3100
3101 void wxDataViewColumn::SetResizeable( bool resizable )
3102 {
3103 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizable );
3104 }
3105
3106 void wxDataViewColumn::SetAlignment( wxAlignment align )
3107 {
3108 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3109
3110 gfloat xalign = 0.0;
3111 if (align == wxALIGN_RIGHT)
3112 xalign = 1.0;
3113 if (align == wxALIGN_CENTER_HORIZONTAL ||
3114 align == wxALIGN_CENTER)
3115 xalign = 0.5;
3116
3117 gtk_tree_view_column_set_alignment( column, xalign );
3118
3119 if (m_renderer && m_renderer->GetAlignment() == -1)
3120 m_renderer->GtkUpdateAlignment();
3121 }
3122
3123 wxAlignment wxDataViewColumn::GetAlignment() const
3124 {
3125 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
3126
3127 if (xalign == 1.0)
3128 return wxALIGN_RIGHT;
3129 if (xalign == 0.5)
3130 return wxALIGN_CENTER_HORIZONTAL;
3131
3132 return wxALIGN_LEFT;
3133 }
3134
3135 void wxDataViewColumn::SetSortable( bool sortable )
3136 {
3137 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3138
3139 if ( sortable )
3140 {
3141 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
3142 }
3143 else
3144 {
3145 gtk_tree_view_column_set_sort_column_id( column, -1 );
3146 gtk_tree_view_column_set_sort_indicator( column, FALSE );
3147 gtk_tree_view_column_set_clickable( column, FALSE );
3148 }
3149 }
3150
3151 bool wxDataViewColumn::IsSortable() const
3152 {
3153 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3154 return gtk_tree_view_column_get_clickable( column );
3155 }
3156
3157 bool wxDataViewColumn::IsSortKey() const
3158 {
3159 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3160 return gtk_tree_view_column_get_sort_indicator( column );
3161 }
3162
3163 bool wxDataViewColumn::IsResizeable() const
3164 {
3165 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3166 return gtk_tree_view_column_get_resizable( column );
3167 }
3168
3169 bool wxDataViewColumn::IsHidden() const
3170 {
3171 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3172 return !gtk_tree_view_column_get_visible( column );
3173 }
3174
3175 void wxDataViewColumn::SetSortOrder( bool ascending )
3176 {
3177 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3178
3179 if (ascending)
3180 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
3181 else
3182 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
3183
3184 gtk_tree_view_column_set_sort_indicator( column, TRUE );
3185 }
3186
3187 bool wxDataViewColumn::IsSortOrderAscending() const
3188 {
3189 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3190
3191 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
3192 }
3193
3194 void wxDataViewColumn::SetMinWidth( int width )
3195 {
3196 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3197 }
3198
3199 int wxDataViewColumn::GetMinWidth() const
3200 {
3201 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
3202 }
3203
3204 int wxDataViewColumn::GetWidth() const
3205 {
3206 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
3207 }
3208
3209 void wxDataViewColumn::SetWidth( int width )
3210 {
3211 if ( width == wxCOL_WIDTH_AUTOSIZE )
3212 {
3213 // NB: this disables user resizing
3214 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
3215 }
3216 else
3217 {
3218 if ( width == wxCOL_WIDTH_DEFAULT )
3219 {
3220 // TODO find a better calculation
3221 width = wxDVC_DEFAULT_WIDTH;
3222 }
3223
3224 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
3225 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3226 }
3227 }
3228
3229 void wxDataViewColumn::SetReorderable( bool reorderable )
3230 {
3231 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
3232 }
3233
3234 bool wxDataViewColumn::IsReorderable() const
3235 {
3236 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) );
3237 }
3238
3239 //-----------------------------------------------------------------------------
3240 // wxGtkTreeModelNode
3241 //-----------------------------------------------------------------------------
3242
3243 #if 0
3244 class wxGtkTreeModelChildWithPos
3245 {
3246 public:
3247 unsigned int pos;
3248 void *id;
3249 };
3250
3251 static
3252 int wxGtkTreeModelChildWithPosCmp( const void* data1, const void* data2, const void* user_data )
3253 {
3254 const wxGtkTreeModelChildWithPos* child1 = (const wxGtkTreeModelChildWithPos*) data1;
3255 const wxGtkTreeModelChildWithPos* child2 = (const wxGtkTreeModelChildWithPos*) data2;
3256 const wxDataViewCtrlInternal *internal = (const wxDataViewCtrlInternal *) user_data;
3257 int ret = internal->GetDataViewModel()->Compare( child1->id, child2->id,
3258 internal->GetSortColumn(), (internal->GetSortOrder() == GTK_SORT_DESCENDING) );
3259
3260 return ret;
3261 }
3262 #else
3263 static
3264 int LINKAGEMODE wxGtkTreeModelChildPtrCmp( void*** data1, void*** data2 )
3265 {
3266 return gs_internal->GetDataViewModel()->Compare( wxDataViewItem(**data1), wxDataViewItem(**data2),
3267 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
3268 }
3269
3270 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr );
3271 #endif
3272
3273 void wxGtkTreeModelNode::Resort()
3274 {
3275 size_t child_count = GetChildCount();
3276 if (child_count == 0)
3277 return;
3278
3279 size_t node_count = GetNodesCount();
3280
3281 if (child_count == 1)
3282 {
3283 if (node_count == 1)
3284 {
3285 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
3286 node->Resort();
3287 }
3288 return;
3289 }
3290
3291 gint *new_order = new gint[child_count];
3292
3293 #if 1
3294 // m_children has the original *void
3295 // ptrs points to these
3296 wxGtkTreeModelChildrenPtr ptrs;
3297 size_t i;
3298 for (i = 0; i < child_count; i++)
3299 ptrs.Add( &(m_children[i]) );
3300 // Sort the ptrs
3301 gs_internal = m_internal;
3302 ptrs.Sort( &wxGtkTreeModelChildPtrCmp );
3303
3304 wxGtkTreeModelChildren temp;
3305 void** base_ptr = &(m_children[0]);
3306 // Transfer positions to new_order array and
3307 // IDs to temp
3308 for (i = 0; i < child_count; i++)
3309 {
3310 new_order[i] = ptrs[i] - base_ptr;
3311 temp.Add( *ptrs[i] );
3312 }
3313
3314 // Transfer IDs back to m_children
3315 m_children.Clear();
3316 WX_APPEND_ARRAY( temp, m_children );
3317 #endif
3318 #if 0
3319 // Too slow
3320
3321 // Build up array with IDs and original positions
3322 wxGtkTreeModelChildWithPos* temp = new wxGtkTreeModelChildWithPos[child_count];
3323 size_t i;
3324 for (i = 0; i < child_count; i++)
3325 {
3326 temp[i].pos = i;
3327 temp[i].id = m_children[i];
3328 }
3329 // Sort array keeping original positions
3330 wxQsort( temp, child_count, sizeof(wxGtkTreeModelChildWithPos),
3331 &wxGtkTreeModelChildWithPosCmp, m_internal );
3332 // Transfer positions to new_order array and
3333 // IDs to m_children
3334 m_children.Clear();
3335 for (i = 0; i < child_count; i++)
3336 {
3337 new_order[i] = temp[i].pos;
3338 m_children.Add( temp[i].id );
3339 }
3340 // Delete array
3341 delete [] temp;
3342 #endif
3343
3344 #if 0
3345 // Too slow
3346
3347 wxGtkTreeModelChildren temp;
3348 WX_APPEND_ARRAY( temp, m_children );
3349
3350 gs_internal = m_internal;
3351 m_children.Sort( &wxGtkTreeModelChildCmp );
3352
3353 unsigned int pos;
3354 for (pos = 0; pos < child_count; pos++)
3355 {
3356 void *id = m_children.Item( pos );
3357 int old_pos = temp.Index( id );
3358 new_order[pos] = old_pos;
3359 }
3360 #endif
3361
3362 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
3363
3364 GtkTreeIter iter;
3365 iter.user_data = GetItem().GetID();
3366 iter.stamp = m_internal->GetGtkModel()->stamp;
3367
3368 gtk_tree_model_rows_reordered( gtk_tree_model,
3369 wxGtkTreePath(m_internal->get_path(&iter)), &iter, new_order );
3370
3371 delete [] new_order;
3372
3373 unsigned int pos;
3374 for (pos = 0; pos < node_count; pos++)
3375 {
3376 wxGtkTreeModelNode *node = m_nodes.Item( pos );
3377 node->Resort();
3378 }
3379 }
3380
3381 //-----------------------------------------------------------------------------
3382 // wxDataViewCtrlInternal
3383 //-----------------------------------------------------------------------------
3384
3385 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model )
3386 {
3387 m_owner = owner;
3388 m_wx_model = wx_model;
3389
3390 m_gtk_model = NULL;
3391 m_root = NULL;
3392 m_sort_order = GTK_SORT_ASCENDING;
3393 m_sort_column = -1;
3394 m_dataview_sort_column = NULL;
3395
3396 m_dragDataObject = NULL;
3397 m_dropDataObject = NULL;
3398
3399 m_dirty = false;
3400
3401 m_gtk_model = wxgtk_tree_model_new();
3402 m_gtk_model->internal = this;
3403
3404 m_notifier = new wxGtkDataViewModelNotifier( wx_model, this );
3405
3406 wx_model->AddNotifier( m_notifier );
3407
3408 // g_object_unref( gtk_model ); ???
3409
3410 if (!m_wx_model->IsVirtualListModel())
3411 InitTree();
3412
3413 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model) );
3414 }
3415
3416 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3417 {
3418 m_wx_model->RemoveNotifier( m_notifier );
3419
3420 // remove the model from the GtkTreeView before it gets destroyed
3421 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner->GtkGetTreeView() ), NULL );
3422
3423 g_object_unref( m_gtk_model );
3424
3425 delete m_dragDataObject;
3426 delete m_dropDataObject;
3427 }
3428
3429 void wxDataViewCtrlInternal::ScheduleRefresh()
3430 {
3431 m_dirty = true;
3432 }
3433
3434 void wxDataViewCtrlInternal::OnInternalIdle()
3435 {
3436 if (m_dirty)
3437 {
3438 GtkWidget *widget = m_owner->GtkGetTreeView();
3439 gtk_widget_queue_draw( widget );
3440 m_dirty = false;
3441 }
3442 }
3443
3444 void wxDataViewCtrlInternal::InitTree()
3445 {
3446 wxDataViewItem item;
3447 m_root = new wxGtkTreeModelNode( NULL, item, this );
3448
3449 BuildBranch( m_root );
3450 }
3451
3452 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
3453 {
3454 if (node->GetChildCount() == 0)
3455 {
3456 wxDataViewItemArray children;
3457 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
3458
3459 unsigned int pos;
3460 for (pos = 0; pos < count; pos++)
3461 {
3462 wxDataViewItem child = children[pos];
3463
3464 if (m_wx_model->IsContainer( child ))
3465 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
3466 else
3467 node->AddLeaf( child.GetID() );
3468
3469 // Don't send any events here
3470 }
3471 }
3472 }
3473
3474 // GTK+ dnd iface
3475
3476 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format )
3477 {
3478 wxGtkString atom_str( gdk_atom_name( format ) );
3479 m_dragSourceTargetEntryTarget = wxCharBuffer( atom_str );
3480
3481 m_dragSourceTargetEntry.target = m_dragSourceTargetEntryTarget.data();
3482 m_dragSourceTargetEntry.flags = 0;
3483 m_dragSourceTargetEntry.info = static_cast<guint>(-1);
3484
3485 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3486 GDK_BUTTON1_MASK, &m_dragSourceTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
3487
3488 return true;
3489 }
3490
3491 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat &format )
3492 {
3493 wxGtkString atom_str( gdk_atom_name( format ) );
3494 m_dropTargetTargetEntryTarget = wxCharBuffer( atom_str );
3495
3496 m_dropTargetTargetEntry.target = m_dropTargetTargetEntryTarget.data();
3497 m_dropTargetTargetEntry.flags = 0;
3498 m_dropTargetTargetEntry.info = static_cast<guint>(-1);
3499
3500 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3501 &m_dropTargetTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
3502
3503 return true;
3504 }
3505
3506 gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
3507 GtkTreePath *path )
3508 {
3509 delete m_dragDataObject;
3510 m_dragDataObject = NULL;
3511
3512 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3513 if ( !item )
3514 return FALSE;
3515
3516 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
3517 event.SetEventObject( m_owner );
3518 event.SetItem( item );
3519 event.SetModel( m_wx_model );
3520 gint x, y;
3521 gtk_widget_get_pointer(m_owner->GtkGetTreeView(), &x, &y);
3522 event.SetPosition(x, y);
3523 if (!m_owner->HandleWindowEvent( event ))
3524 return FALSE;
3525
3526 if (!event.IsAllowed())
3527 return FALSE;
3528
3529 wxDataObject *obj = event.GetDataObject();
3530 if (!obj)
3531 return FALSE;
3532
3533 m_dragDataObject = obj;
3534
3535 return TRUE;
3536 }
3537
3538 gboolean
3539 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
3540 GtkTreePath *WXUNUSED(path))
3541 {
3542 return FALSE;
3543 }
3544
3545 gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
3546 GtkTreePath *path, GtkSelectionData *selection_data )
3547 {
3548 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3549 if ( !item )
3550 return FALSE;
3551
3552 GdkAtom target = gtk_selection_data_get_target(selection_data);
3553 if (!m_dragDataObject->IsSupported(target))
3554 return FALSE;
3555
3556 size_t size = m_dragDataObject->GetDataSize(target);
3557 if (size == 0)
3558 return FALSE;
3559
3560 void *buf = malloc( size );
3561
3562 gboolean res = FALSE;
3563 if (m_dragDataObject->GetDataHere(target, buf))
3564 {
3565 res = TRUE;
3566
3567 gtk_selection_data_set(selection_data, target,
3568 8, (const guchar*) buf, size );
3569 }
3570
3571 free( buf );
3572
3573 return res;
3574 }
3575
3576 gboolean
3577 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
3578 GtkTreePath *path,
3579 GtkSelectionData *selection_data)
3580 {
3581 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3582 if ( !item )
3583 return FALSE;
3584
3585 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() );
3586 event.SetEventObject( m_owner );
3587 event.SetItem( item );
3588 event.SetModel( m_wx_model );
3589 event.SetDataFormat(gtk_selection_data_get_target(selection_data));
3590 event.SetDataSize(gtk_selection_data_get_length(selection_data));
3591 event.SetDataBuffer(const_cast<guchar*>(gtk_selection_data_get_data(selection_data)));
3592 if (!m_owner->HandleWindowEvent( event ))
3593 return FALSE;
3594
3595 if (!event.IsAllowed())
3596 return FALSE;
3597
3598 return TRUE;
3599 }
3600
3601 gboolean
3602 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
3603 GtkTreePath *path,
3604 GtkSelectionData *selection_data)
3605 {
3606 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3607 if ( !item )
3608 return FALSE;
3609
3610 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
3611 event.SetEventObject( m_owner );
3612 event.SetItem( item );
3613 event.SetModel( m_wx_model );
3614 event.SetDataFormat(gtk_selection_data_get_target(selection_data));
3615 if (!m_owner->HandleWindowEvent( event ))
3616 return FALSE;
3617
3618 if (!event.IsAllowed())
3619 return FALSE;
3620
3621 return TRUE;
3622 }
3623
3624 // notifications from wxDataViewModel
3625
3626 bool wxDataViewCtrlInternal::Cleared()
3627 {
3628 if (m_root)
3629 {
3630 delete m_root;
3631 m_root = NULL;
3632 }
3633
3634 InitTree();
3635
3636 ScheduleRefresh();
3637
3638 return true;
3639 }
3640
3641 void wxDataViewCtrlInternal::Resort()
3642 {
3643 if (!m_wx_model->IsVirtualListModel())
3644 m_root->Resort();
3645
3646 ScheduleRefresh();
3647 }
3648
3649 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
3650 {
3651 if (!m_wx_model->IsVirtualListModel())
3652 {
3653 wxGtkTreeModelNode *parent_node = FindNode( parent );
3654 wxCHECK_MSG(parent_node, false,
3655 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3656
3657 wxDataViewItemArray modelSiblings;
3658 m_wx_model->GetChildren(parent, modelSiblings);
3659 const int modelSiblingsSize = modelSiblings.size();
3660
3661 int posInModel = modelSiblings.Index(item, /*fromEnd=*/true);
3662 wxCHECK_MSG( posInModel != wxNOT_FOUND, false, "adding non-existent item?" );
3663
3664 const wxGtkTreeModelChildren& nodeSiblings = parent_node->GetChildren();
3665 const int nodeSiblingsSize = nodeSiblings.size();
3666
3667 int nodePos = 0;
3668
3669 if ( posInModel == modelSiblingsSize - 1 )
3670 {
3671 nodePos = nodeSiblingsSize;
3672 }
3673 else if ( modelSiblingsSize == nodeSiblingsSize + 1 )
3674 {
3675 // This is the simple case when our node tree already matches the
3676 // model and only this one item is missing.
3677 nodePos = posInModel;
3678 }
3679 else
3680 {
3681 // It's possible that a larger discrepancy between the model and
3682 // our realization exists. This can happen e.g. when adding a bunch
3683 // of items to the model and then calling ItemsAdded() just once
3684 // afterwards. In this case, we must find the right position by
3685 // looking at sibling items.
3686
3687 // append to the end if we won't find a better position:
3688 nodePos = nodeSiblingsSize;
3689
3690 for ( int nextItemPos = posInModel + 1;
3691 nextItemPos < modelSiblingsSize;
3692 nextItemPos++ )
3693 {
3694 int nextNodePos = parent_node->FindChildByItem(modelSiblings[nextItemPos]);
3695 if ( nextNodePos != wxNOT_FOUND )
3696 {
3697 nodePos = nextNodePos;
3698 break;
3699 }
3700 }
3701 }
3702
3703 if (m_wx_model->IsContainer( item ))
3704 parent_node->InsertNode( new wxGtkTreeModelNode( parent_node, item, this ), nodePos );
3705 else
3706 parent_node->InsertLeaf( item.GetID(), nodePos );
3707 }
3708
3709 ScheduleRefresh();
3710
3711 return true;
3712 }
3713
3714 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
3715 {
3716 if (!m_wx_model->IsVirtualListModel())
3717 {
3718 wxGtkTreeModelNode *parent_node = FindNode( parent );
3719 wxASSERT_MSG(parent_node,
3720 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3721
3722 parent_node->DeleteChild( item.GetID() );
3723 }
3724
3725 ScheduleRefresh();
3726
3727 return true;
3728 }
3729
3730 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
3731 {
3732 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
3733 event.SetEventObject( m_owner );
3734 event.SetModel( m_owner->GetModel() );
3735 event.SetItem( item );
3736 m_owner->HandleWindowEvent( event );
3737
3738 return true;
3739 }
3740
3741 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int view_column )
3742 {
3743 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
3744 event.SetEventObject( m_owner );
3745 event.SetModel( m_owner->GetModel() );
3746 event.SetColumn( view_column );
3747 event.SetDataViewColumn( GetOwner()->GetColumn(view_column) );
3748 event.SetItem( item );
3749 m_owner->HandleWindowEvent( event );
3750
3751 return true;
3752 }
3753
3754 // GTK+ model iface
3755
3756 GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
3757 {
3758 int flags = 0;
3759
3760 if ( m_wx_model->IsListModel() )
3761 flags |= GTK_TREE_MODEL_LIST_ONLY;
3762
3763 if ( !m_wx_model->IsVirtualListModel() )
3764 flags |= GTK_TREE_MODEL_ITERS_PERSIST;
3765
3766 return GtkTreeModelFlags(flags);
3767 }
3768
3769 gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
3770 {
3771
3772 if (m_wx_model->IsVirtualListModel())
3773 {
3774 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3775
3776 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
3777
3778 if (i >= wx_model->GetCount())
3779 return FALSE;
3780
3781 iter->stamp = m_gtk_model->stamp;
3782 // user_data is just the index +1
3783 iter->user_data = wxUIntToPtr(i+1);
3784
3785 return TRUE;
3786 }
3787 else
3788 {
3789 int depth = gtk_tree_path_get_depth( path );
3790
3791 wxGtkTreeModelNode *node = m_root;
3792
3793 int i;
3794 for (i = 0; i < depth; i++)
3795 {
3796 BuildBranch( node );
3797
3798 gint pos = gtk_tree_path_get_indices (path)[i];
3799 if (pos < 0) return FALSE;
3800 if ((size_t)pos >= node->GetChildCount()) return FALSE;
3801
3802 void* id = node->GetChildren().Item( (size_t) pos );
3803
3804 if (i == depth-1)
3805 {
3806 iter->stamp = m_gtk_model->stamp;
3807 iter->user_data = id;
3808 return TRUE;
3809 }
3810
3811 size_t count = node->GetNodes().GetCount();
3812 size_t pos2;
3813 for (pos2 = 0; pos2 < count; pos2++)
3814 {
3815 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
3816 if (child_node->GetItem().GetID() == id)
3817 {
3818 node = child_node;
3819 break;
3820 }
3821 }
3822 }
3823 }
3824
3825 return FALSE;
3826 }
3827
3828 GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
3829 {
3830 // When this is called from ItemDeleted(), the item is already
3831 // deleted in the model.
3832
3833 GtkTreePath *retval = gtk_tree_path_new ();
3834
3835 if (m_wx_model->IsVirtualListModel())
3836 {
3837 // iter is root, add nothing
3838 if (!iter->user_data)
3839 return retval;
3840
3841 // user_data is just the index +1
3842 int i = ( (wxUIntPtr) iter->user_data ) -1;
3843 gtk_tree_path_append_index (retval, i);
3844 }
3845 else
3846 {
3847 void *id = iter->user_data;
3848
3849 wxGtkTreeModelNode *node = FindParentNode( iter );
3850 while (node)
3851 {
3852 int pos = node->GetChildren().Index( id );
3853
3854 gtk_tree_path_prepend_index( retval, pos );
3855
3856 id = node->GetItem().GetID();
3857 node = node->GetParent();
3858 }
3859 }
3860
3861 return retval;
3862 }
3863
3864 gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
3865 {
3866 if (m_wx_model->IsVirtualListModel())
3867 {
3868 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3869
3870 // user_data is just the index +1
3871 int n = ( (wxUIntPtr) iter->user_data ) -1;
3872
3873 if (n == -1)
3874 {
3875 iter->user_data = NULL;
3876 return FALSE;
3877 }
3878
3879 if (n >= (int) wx_model->GetCount()-1)
3880 {
3881 iter->user_data = NULL;
3882 return FALSE;
3883 }
3884
3885 // user_data is just the index +1 (+2 because we need the next)
3886 iter->user_data = wxUIntToPtr(n+2);
3887 }
3888 else
3889 {
3890 wxGtkTreeModelNode *parent = FindParentNode( iter );
3891 if( parent == NULL )
3892 {
3893 iter->user_data = NULL;
3894 return FALSE;
3895 }
3896
3897 int pos = parent->GetChildren().Index( iter->user_data );
3898
3899 if (pos == (int) parent->GetChildCount()-1)
3900 {
3901 iter->user_data = NULL;
3902 return FALSE;
3903 }
3904
3905 iter->user_data = parent->GetChildren().Item( pos+1 );
3906 }
3907
3908 return TRUE;
3909 }
3910
3911 gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
3912 {
3913 if (m_wx_model->IsVirtualListModel())
3914 {
3915 // this is a list, nodes have no children
3916 if (parent)
3917 return FALSE;
3918
3919 iter->stamp = m_gtk_model->stamp;
3920 iter->user_data = (gpointer) 1;
3921
3922 return TRUE;
3923 }
3924 else
3925 {
3926 if (iter == NULL)
3927 {
3928 if (m_root->GetChildCount() == 0) return FALSE;
3929 iter->stamp = m_gtk_model->stamp;
3930 iter->user_data = (gpointer) m_root->GetChildren().Item( 0 );
3931 return TRUE;
3932 }
3933
3934
3935 wxDataViewItem item;
3936 if (parent)
3937 item = wxDataViewItem( (void*) parent->user_data );
3938
3939 if (!m_wx_model->IsContainer( item ))
3940 return FALSE;
3941
3942 wxGtkTreeModelNode *parent_node = FindNode( parent );
3943 wxASSERT_MSG(parent_node,
3944 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3945
3946 BuildBranch( parent_node );
3947
3948 if (parent_node->GetChildCount() == 0)
3949 return FALSE;
3950
3951 iter->stamp = m_gtk_model->stamp;
3952 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
3953 }
3954
3955 return TRUE;
3956 }
3957
3958 gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
3959 {
3960 if (m_wx_model->IsVirtualListModel())
3961 {
3962 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3963
3964 if (iter == NULL)
3965 return (wx_model->GetCount() > 0);
3966
3967 // this is a list, nodes have no children
3968 return FALSE;
3969 }
3970 else
3971 {
3972 if (iter == NULL)
3973 return (m_root->GetChildCount() > 0);
3974
3975 wxDataViewItem item( (void*) iter->user_data );
3976
3977 bool is_container = m_wx_model->IsContainer( item );
3978
3979 if (!is_container)
3980 return FALSE;
3981
3982 wxGtkTreeModelNode *node = FindNode( iter );
3983 wxASSERT_MSG(node,
3984 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3985
3986 BuildBranch( node );
3987
3988 return (node->GetChildCount() > 0);
3989 }
3990 }
3991
3992 gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
3993 {
3994 if (m_wx_model->IsVirtualListModel())
3995 {
3996 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3997
3998 if (iter == NULL)
3999 return (gint) wx_model->GetCount();
4000
4001 return 0;
4002 }
4003 else
4004 {
4005 if (iter == NULL)
4006 return m_root->GetChildCount();
4007
4008 wxDataViewItem item( (void*) iter->user_data );
4009
4010 if (!m_wx_model->IsContainer( item ))
4011 return 0;
4012
4013 wxGtkTreeModelNode *parent_node = FindNode( iter );
4014 wxASSERT_MSG(parent_node,
4015 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4016
4017 BuildBranch( parent_node );
4018
4019 return parent_node->GetChildCount();
4020 }
4021 }
4022
4023 gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
4024 {
4025 if (m_wx_model->IsVirtualListModel())
4026 {
4027 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
4028
4029 if (parent)
4030 return FALSE;
4031
4032 if (n < 0)
4033 return FALSE;
4034
4035 if (n >= (gint) wx_model->GetCount())
4036 return FALSE;
4037
4038 iter->stamp = m_gtk_model->stamp;
4039 // user_data is just the index +1
4040 iter->user_data = wxUIntToPtr(n+1);
4041
4042 return TRUE;
4043 }
4044 else
4045 {
4046 void* id = NULL;
4047 if (parent) id = (void*) parent->user_data;
4048 wxDataViewItem item( id );
4049
4050 if (!m_wx_model->IsContainer( item ))
4051 return FALSE;
4052
4053 wxGtkTreeModelNode *parent_node = FindNode( parent );
4054 wxASSERT_MSG(parent_node,
4055 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4056
4057 BuildBranch( parent_node );
4058
4059 iter->stamp = m_gtk_model->stamp;
4060 iter->user_data = parent_node->GetChildren().Item( n );
4061
4062 return TRUE;
4063 }
4064 }
4065
4066 gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
4067 {
4068 if (m_wx_model->IsVirtualListModel())
4069 {
4070 return FALSE;
4071 }
4072 else
4073 {
4074 wxGtkTreeModelNode *node = FindParentNode( child );
4075 if (!node)
4076 return FALSE;
4077
4078 iter->stamp = m_gtk_model->stamp;
4079 iter->user_data = (gpointer) node->GetItem().GetID();
4080
4081 return TRUE;
4082 }
4083 }
4084
4085 // item can be deleted already in the model
4086 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item )
4087 {
4088 if (m_wx_model->IsVirtualListModel())
4089 {
4090 return wxPtrToUInt(item.GetID()) - 1;
4091 }
4092 else
4093 {
4094 wxGtkTreeModelNode *parent_node = FindNode( parent );
4095 wxGtkTreeModelChildren &children = parent_node->GetChildren();
4096 size_t j;
4097 for (j = 0; j < children.GetCount(); j++)
4098 {
4099 if (children[j] == item.GetID())
4100 return j;
4101 }
4102 }
4103 return -1;
4104 }
4105
4106
4107 static wxGtkTreeModelNode*
4108 wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
4109 {
4110 if( model == NULL )
4111 return NULL;
4112
4113 ItemList list;
4114 list.DeleteContents( true );
4115 wxDataViewItem it( item );
4116
4117 while( it.IsOk() )
4118 {
4119 wxDataViewItem * pItem = new wxDataViewItem( it );
4120 list.Insert( pItem );
4121 it = model->GetParent( it );
4122 }
4123
4124 wxGtkTreeModelNode * node = treeNode;
4125 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
4126 {
4127 if( node && node->GetNodes().GetCount() != 0 )
4128 {
4129 int len = node->GetNodes().GetCount();
4130 wxGtkTreeModelNodes &nodes = node->GetNodes();
4131 int j = 0;
4132 for( ; j < len; j ++)
4133 {
4134 if( nodes[j]->GetItem() == *(n->GetData()))
4135 {
4136 node = nodes[j];
4137 break;
4138 }
4139 }
4140
4141 if( j == len )
4142 {
4143 return NULL;
4144 }
4145 }
4146 else
4147 return NULL;
4148 }
4149 return node;
4150
4151 }
4152
4153 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
4154 {
4155 if (!iter)
4156 return m_root;
4157
4158 wxDataViewItem item( (void*) iter->user_data );
4159 if (!item.IsOk())
4160 return m_root;
4161
4162 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
4163
4164 /*
4165 if (!result)
4166 {
4167 wxLogDebug( "Not found %p", iter->user_data );
4168 char *crash = NULL;
4169 *crash = 0;
4170 }
4171 // TODO: remove this code
4172 */
4173
4174 return result;
4175 }
4176
4177 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
4178 {
4179 if (!item.IsOk())
4180 return m_root;
4181
4182 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
4183
4184 /*
4185 if (!result)
4186 {
4187 wxLogDebug( "Not found %p", item.GetID() );
4188 char *crash = NULL;
4189 *crash = 0;
4190 }
4191 // TODO: remove this code
4192 */
4193
4194 return result;
4195 }
4196
4197 static wxGtkTreeModelNode*
4198 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
4199 {
4200 if( model == NULL )
4201 return NULL;
4202
4203 ItemList list;
4204 list.DeleteContents( true );
4205 if( !item.IsOk() )
4206 return NULL;
4207
4208 wxDataViewItem it( model->GetParent( item ) );
4209 while( it.IsOk() )
4210 {
4211 wxDataViewItem * pItem = new wxDataViewItem( it );
4212 list.Insert( pItem );
4213 it = model->GetParent( it );
4214 }
4215
4216 wxGtkTreeModelNode * node = treeNode;
4217 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
4218 {
4219 if( node && node->GetNodes().GetCount() != 0 )
4220 {
4221 int len = node->GetNodes().GetCount();
4222 wxGtkTreeModelNodes nodes = node->GetNodes();
4223 int j = 0;
4224 for( ; j < len; j ++)
4225 {
4226 if( nodes[j]->GetItem() == *(n->GetData()))
4227 {
4228 node = nodes[j];
4229 break;
4230 }
4231 }
4232
4233 if( j == len )
4234 {
4235 return NULL;
4236 }
4237 }
4238 else
4239 return NULL;
4240 }
4241 //Examine whether the node is item's parent node
4242 int len = node->GetChildCount();
4243 for( int i = 0; i < len ; i ++ )
4244 {
4245 if( node->GetChildren().Item( i ) == item.GetID() )
4246 return node;
4247 }
4248 return NULL;
4249 }
4250
4251 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
4252 {
4253 if (!iter)
4254 return NULL;
4255
4256 wxDataViewItem item( (void*) iter->user_data );
4257 if (!item.IsOk())
4258 return NULL;
4259
4260 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
4261 }
4262
4263 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
4264 {
4265 if (!item.IsOk())
4266 return NULL;
4267
4268 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
4269 }
4270
4271 //-----------------------------------------------------------------------------
4272 // wxDataViewCtrl signal callbacks
4273 //-----------------------------------------------------------------------------
4274
4275 static void
4276 wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv )
4277 {
4278 if (!gtk_widget_get_realized(dv->m_widget))
4279 return;
4280
4281 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() );
4282 event.SetItem( dv->GetSelection() );
4283 event.SetModel( dv->GetModel() );
4284 dv->HandleWindowEvent( event );
4285 }
4286
4287 static void
4288 wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path,
4289 GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv )
4290 {
4291 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
4292
4293 wxDataViewItem item(dv->GTKPathToItem(path));
4294 event.SetItem( item );
4295 event.SetModel( dv->GetModel() );
4296 dv->HandleWindowEvent( event );
4297 }
4298
4299 static gboolean
4300 wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4301 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4302 {
4303 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
4304
4305 wxDataViewItem item( (void*) iter->user_data );;
4306 event.SetItem( item );
4307 event.SetModel( dv->GetModel() );
4308 dv->HandleWindowEvent( event );
4309
4310 return !event.IsAllowed();
4311 }
4312
4313 static void
4314 wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4315 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4316 {
4317 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
4318
4319 wxDataViewItem item( (void*) iter->user_data );;
4320 event.SetItem( item );
4321 event.SetModel( dv->GetModel() );
4322 dv->HandleWindowEvent( event );
4323 }
4324
4325 static gboolean
4326 wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4327 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4328 {
4329 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
4330
4331 wxDataViewItem item( (void*) iter->user_data );;
4332 event.SetItem( item );
4333 event.SetModel( dv->GetModel() );
4334 dv->HandleWindowEvent( event );
4335
4336 return !event.IsAllowed();
4337 }
4338
4339 static void
4340 wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4341 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4342 {
4343 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
4344
4345 wxDataViewItem item( (void*) iter->user_data );;
4346 event.SetItem( item );
4347 event.SetModel( dv->GetModel() );
4348 dv->HandleWindowEvent( event );
4349 }
4350
4351 //-----------------------------------------------------------------------------
4352 // wxDataViewCtrl
4353 //-----------------------------------------------------------------------------
4354
4355 void wxDataViewCtrl::AddChildGTK(wxWindowGTK* child)
4356 {
4357 GtkWidget* treeview = GtkGetTreeView();
4358
4359 // Insert widget in GtkTreeView
4360 if (gtk_widget_get_realized(treeview))
4361 gtk_widget_set_parent_window( child->m_widget,
4362 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
4363 gtk_widget_set_parent( child->m_widget, treeview );
4364 }
4365
4366 static
4367 void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
4368 GtkAllocation *WXUNUSED(gtk_alloc),
4369 wxDataViewCtrl *win )
4370 {
4371 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
4372 while (node)
4373 {
4374 wxWindow *child = node->GetData();
4375
4376 GtkRequisition req;
4377 gtk_widget_size_request( child->m_widget, &req );
4378
4379 GtkAllocation alloc;
4380 alloc.x = child->m_x;
4381 alloc.y = child->m_y;
4382 alloc.width = child->m_width;
4383 alloc.height = child->m_height;
4384 gtk_widget_size_allocate( child->m_widget, &alloc );
4385
4386 node = node->GetNext();
4387 }
4388 }
4389
4390
4391 //-----------------------------------------------------------------------------
4392 // "motion_notify_event"
4393 //-----------------------------------------------------------------------------
4394
4395 static gboolean
4396 gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget),
4397 GdkEventMotion *gdk_event,
4398 wxDataViewCtrl *dv )
4399 {
4400 if (gdk_event->is_hint)
4401 {
4402 int x = 0;
4403 int y = 0;
4404 GdkModifierType state;
4405 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
4406 gdk_event->x = x;
4407 gdk_event->y = y;
4408 }
4409
4410 wxGtkTreePath path;
4411 GtkTreeViewColumn *column = NULL;
4412 gint cell_x = 0;
4413 gint cell_y = 0;
4414 if (gtk_tree_view_get_path_at_pos(
4415 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4416 (int) gdk_event->x, (int) gdk_event->y,
4417 path.ByRef(),
4418 &column,
4419 &cell_x,
4420 &cell_y))
4421 {
4422 if (path)
4423 {
4424 GtkTreeIter iter;
4425 dv->GtkGetInternal()->get_iter( &iter, path );
4426 }
4427 }
4428
4429
4430 return FALSE;
4431 }
4432
4433 //-----------------------------------------------------------------------------
4434 // "button_press_event"
4435 //-----------------------------------------------------------------------------
4436
4437 static gboolean
4438 gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget),
4439 GdkEventButton *gdk_event,
4440 wxDataViewCtrl *dv )
4441 {
4442 if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS))
4443 {
4444 wxGtkTreePath path;
4445 GtkTreeViewColumn *column = NULL;
4446 gint cell_x = 0;
4447 gint cell_y = 0;
4448 gtk_tree_view_get_path_at_pos
4449 (
4450 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4451 (int) gdk_event->x, (int) gdk_event->y,
4452 path.ByRef(),
4453 &column,
4454 &cell_x,
4455 &cell_y
4456 );
4457
4458 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() );
4459 if (path)
4460 event.SetItem(dv->GTKPathToItem(path));
4461 event.SetModel( dv->GetModel() );
4462 return dv->HandleWindowEvent( event );
4463 }
4464
4465 return FALSE;
4466 }
4467
4468 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4469
4470 wxDataViewCtrl::~wxDataViewCtrl()
4471 {
4472 // Stop editing before destroying the control to remove any event handlers
4473 // which are added when editing started: if we didn't do this, the base
4474 // class dtor would assert as it checks for any leftover handlers.
4475 if ( m_treeview )
4476 {
4477 GtkTreeViewColumn *col;
4478 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview), NULL, &col);
4479
4480 wxDataViewColumn * const wxcol = FromGTKColumn(col);
4481 if ( wxcol )
4482 {
4483 // This won't do anything if we're not editing it
4484 wxcol->GetRenderer()->CancelEditing();
4485 }
4486 }
4487
4488 m_cols.Clear();
4489
4490 delete m_internal;
4491 }
4492
4493 void wxDataViewCtrl::Init()
4494 {
4495 m_internal = NULL;
4496
4497 m_cols.DeleteContents( true );
4498
4499 m_uniformRowHeight = -1;
4500 }
4501
4502 bool wxDataViewCtrl::Create(wxWindow *parent,
4503 wxWindowID id,
4504 const wxPoint& pos,
4505 const wxSize& size,
4506 long style,
4507 const wxValidator& validator,
4508 const wxString& name)
4509 {
4510 if (!PreCreation( parent, pos, size ) ||
4511 !CreateBase( parent, id, pos, size, style, validator, name ))
4512 {
4513 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4514 return false;
4515 }
4516
4517 m_widget = gtk_scrolled_window_new (NULL, NULL);
4518 g_object_ref(m_widget);
4519
4520 GTKScrolledWindowSetBorder(m_widget, style);
4521
4522 m_treeview = gtk_tree_view_new();
4523 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
4524
4525 m_focusWidget = GTK_WIDGET(m_treeview);
4526
4527 g_signal_connect (m_treeview, "size_allocate",
4528 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
4529
4530 #ifdef __WXGTK26__
4531 if (!gtk_check_version(2,6,0))
4532 {
4533 bool fixed = (style & wxDV_VARIABLE_LINE_HEIGHT) == 0;
4534 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4535 }
4536 #endif
4537
4538 if (style & wxDV_MULTIPLE)
4539 {
4540 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4541 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
4542 }
4543
4544 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
4545
4546 #ifdef __WXGTK210__
4547 if (!gtk_check_version(2,10,0))
4548 {
4549 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
4550
4551 if ((style & wxDV_HORIZ_RULES) != 0 &&
4552 (style & wxDV_VERT_RULES) != 0)
4553 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
4554 else if (style & wxDV_VERT_RULES)
4555 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
4556 else if (style & wxDV_HORIZ_RULES)
4557 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
4558
4559 if (grid != GTK_TREE_VIEW_GRID_LINES_NONE)
4560 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
4561 }
4562 #endif
4563
4564 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 );
4565
4566 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
4567 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4568 gtk_widget_show (m_treeview);
4569
4570 m_parent->DoAddChild( this );
4571
4572 PostCreation(size);
4573
4574 GtkEnableSelectionEvents();
4575
4576 g_signal_connect_after (m_treeview, "row-activated",
4577 G_CALLBACK (wxdataview_row_activated_callback), this);
4578
4579 g_signal_connect (m_treeview, "test-collapse-row",
4580 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
4581
4582 g_signal_connect_after (m_treeview, "row-collapsed",
4583 G_CALLBACK (wxdataview_row_collapsed_callback), this);
4584
4585 g_signal_connect (m_treeview, "test-expand-row",
4586 G_CALLBACK (wxdataview_test_expand_row_callback), this);
4587
4588 g_signal_connect_after (m_treeview, "row-expanded",
4589 G_CALLBACK (wxdataview_row_expanded_callback), this);
4590
4591 g_signal_connect (m_treeview, "motion_notify_event",
4592 G_CALLBACK (gtk_dataview_motion_notify_callback), this);
4593
4594 g_signal_connect (m_treeview, "button_press_event",
4595 G_CALLBACK (gtk_dataview_button_press_callback), this);
4596
4597 return true;
4598 }
4599
4600 wxDataViewItem wxDataViewCtrl::GTKPathToItem(GtkTreePath *path) const
4601 {
4602 GtkTreeIter iter;
4603 return wxDataViewItem(path && m_internal->get_iter(&iter, path)
4604 ? iter.user_data
4605 : NULL);
4606 }
4607
4608 void wxDataViewCtrl::OnInternalIdle()
4609 {
4610 wxWindow::OnInternalIdle();
4611
4612 m_internal->OnInternalIdle();
4613
4614 unsigned int cols = GetColumnCount();
4615 unsigned int i;
4616 for (i = 0; i < cols; i++)
4617 {
4618 wxDataViewColumn *col = GetColumn( i );
4619 col->OnInternalIdle();
4620 }
4621
4622 if (m_ensureVisibleDefered.IsOk())
4623 {
4624 ExpandAncestors(m_ensureVisibleDefered);
4625 GtkTreeIter iter;
4626 iter.user_data = (gpointer) m_ensureVisibleDefered.GetID();
4627 wxGtkTreePath path(m_internal->get_path( &iter ));
4628 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4629 m_ensureVisibleDefered = wxDataViewItem(0);
4630 }
4631 }
4632
4633 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4634 {
4635 wxDELETE(m_internal);
4636
4637 if (!wxDataViewCtrlBase::AssociateModel( model ))
4638 return false;
4639
4640 #ifdef __WXGTK26__
4641 if (!gtk_check_version(2,6,0))
4642 {
4643 bool fixed = (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) == 0) || (model->IsVirtualListModel()));
4644 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4645 }
4646 #endif
4647
4648 m_internal = new wxDataViewCtrlInternal( this, model );
4649
4650 return true;
4651 }
4652
4653 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4654 {
4655 return m_internal->EnableDragSource( format );
4656 }
4657
4658 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4659 {
4660 return m_internal->EnableDropTarget( format );
4661 }
4662
4663 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4664 {
4665 if (!wxDataViewCtrlBase::AppendColumn(col))
4666 return false;
4667
4668 m_cols.Append( col );
4669
4670 #ifdef __WXGTK26__
4671 if (!gtk_check_version(2,6,0))
4672 {
4673 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4674 GTK_TREE_VIEW_COLUMN_FIXED)
4675 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4676 }
4677 #endif
4678
4679 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
4680 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4681
4682 return true;
4683 }
4684
4685 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4686 {
4687 if (!wxDataViewCtrlBase::PrependColumn(col))
4688 return false;
4689
4690 m_cols.Insert( col );
4691
4692 #ifdef __WXGTK26__
4693 if (!gtk_check_version(2,6,0))
4694 {
4695 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4696 GTK_TREE_VIEW_COLUMN_FIXED)
4697 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4698 }
4699 #endif
4700
4701 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4702 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 );
4703
4704 return true;
4705 }
4706
4707 bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4708 {
4709 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4710 return false;
4711
4712 m_cols.Insert( pos, col );
4713
4714 #ifdef __WXGTK26__
4715 if (!gtk_check_version(2,6,0))
4716 {
4717 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4718 GTK_TREE_VIEW_COLUMN_FIXED)
4719 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4720 }
4721 #endif
4722
4723 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4724 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), pos );
4725
4726 return true;
4727 }
4728
4729 unsigned int wxDataViewCtrl::GetColumnCount() const
4730 {
4731 return m_cols.GetCount();
4732 }
4733
4734 wxDataViewColumn* wxDataViewCtrl::FromGTKColumn(GtkTreeViewColumn *gtk_col) const
4735 {
4736 if ( !gtk_col )
4737 return NULL;
4738
4739 wxDataViewColumnList::const_iterator iter;
4740 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
4741 {
4742 wxDataViewColumn *col = *iter;
4743 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
4744 {
4745 return col;
4746 }
4747 }
4748
4749 wxFAIL_MSG( "No matching column?" );
4750
4751 return NULL;
4752 }
4753
4754 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
4755 {
4756 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
4757
4758 return FromGTKColumn(gtk_col);
4759 }
4760
4761 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4762 {
4763 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
4764 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
4765
4766 m_cols.DeleteObject( column );
4767
4768 return true;
4769 }
4770
4771 bool wxDataViewCtrl::ClearColumns()
4772 {
4773 wxDataViewColumnList::iterator iter;
4774 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
4775 {
4776 wxDataViewColumn *col = *iter;
4777 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
4778 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4779 }
4780
4781 m_cols.Clear();
4782
4783 return true;
4784 }
4785
4786 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4787 {
4788 GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
4789
4790 wxGtkList list(gtk_tree_view_get_columns(GTK_TREE_VIEW(m_treeview)));
4791
4792 return g_list_index( list, (gconstpointer) gtk_column );
4793 }
4794
4795 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4796 {
4797 return m_internal->GetDataViewSortColumn();
4798 }
4799
4800 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4801 {
4802 GtkTreeIter iter;
4803 iter.user_data = item.GetID();
4804 wxGtkTreePath path(m_internal->get_path( &iter ));
4805 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
4806 }
4807
4808 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4809 {
4810 GtkTreeIter iter;
4811 iter.user_data = item.GetID();
4812 wxGtkTreePath path(m_internal->get_path( &iter ));
4813 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
4814 }
4815
4816 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4817 {
4818 GtkTreeIter iter;
4819 iter.user_data = item.GetID();
4820 wxGtkTreePath path(m_internal->get_path( &iter ));
4821 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview), path );
4822 }
4823
4824 wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4825 {
4826 // The tree doesn't have any current item if it hadn't been created yet but
4827 // it's arguably not an error to call this function in this case so just
4828 // return an invalid item without asserting.
4829 if ( !m_treeview )
4830 return wxDataViewItem();
4831
4832 wxGtkTreePath path;
4833 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview), path.ByRef(), NULL);
4834
4835 return GTKPathToItem(path);
4836 }
4837
4838 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4839 {
4840 wxCHECK_RET( m_treeview,
4841 "Current item can't be set before creating the control." );
4842
4843 // We need to make sure the model knows about this item or the path would
4844 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4845 ExpandAncestors(item);
4846
4847 // We also need to preserve the existing selection from changing.
4848 // Unfortunately the only way to do it seems to use our own selection
4849 // function and forbid any selection changes during set cursor call.
4850 wxGtkTreeSelectionLock
4851 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview)));
4852
4853 // Do move the cursor now.
4854 GtkTreeIter iter;
4855 iter.user_data = item.GetID();
4856 wxGtkTreePath path(m_internal->get_path( &iter ));
4857
4858 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview), path, NULL, FALSE);
4859 }
4860
4861 void wxDataViewCtrl::StartEditor(const wxDataViewItem& item, unsigned int column)
4862 {
4863 wxCHECK_RET( m_treeview,
4864 "Current item can't be set before creating the control." );
4865
4866 // We need to make sure the model knows about this item or the path would
4867 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4868 ExpandAncestors(item);
4869
4870 wxDataViewColumn *dvcolumn = GetColumn(column);
4871 wxASSERT_MSG(dvcolumn, "Could not retrieve column");
4872 GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(dvcolumn->GetGtkHandle());
4873
4874 // We also need to preserve the existing selection from changing.
4875 // Unfortunately the only way to do it seems to use our own selection
4876 // function and forbid any selection changes during set cursor call.
4877 wxGtkTreeSelectionLock
4878 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview)));
4879
4880 // Do move the cursor now.
4881 GtkTreeIter iter;
4882 iter.user_data = item.GetID();
4883 wxGtkTreePath path(m_internal->get_path( &iter ));
4884
4885 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview), path, gcolumn, TRUE);
4886 }
4887
4888 int wxDataViewCtrl::GetSelectedItemsCount() const
4889 {
4890 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4891
4892 return gtk_tree_selection_count_selected_rows(selection);
4893 }
4894
4895 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4896 {
4897 sel.Clear();
4898
4899 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4900 if (HasFlag(wxDV_MULTIPLE))
4901 {
4902 GtkTreeModel *model;
4903 wxGtkTreePathList list(gtk_tree_selection_get_selected_rows(selection, &model));
4904
4905 for ( GList* current = list; current; current = g_list_next(current) )
4906 {
4907 GtkTreePath *path = (GtkTreePath*) current->data;
4908
4909 sel.Add(GTKPathToItem(path));
4910 }
4911 }
4912 else
4913 {
4914 GtkTreeIter iter;
4915 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
4916 {
4917 sel.Add( wxDataViewItem(iter.user_data) );
4918 }
4919 }
4920
4921 return sel.size();
4922 }
4923
4924 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4925 {
4926 GtkDisableSelectionEvents();
4927
4928 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4929
4930 gtk_tree_selection_unselect_all( selection );
4931
4932 wxDataViewItem last_parent;
4933
4934 size_t i;
4935 for (i = 0; i < sel.GetCount(); i++)
4936 {
4937 wxDataViewItem item = sel[i];
4938 wxDataViewItem parent = GetModel()->GetParent( item );
4939 if (parent)
4940 {
4941 if (parent != last_parent)
4942 ExpandAncestors(item);
4943 }
4944 last_parent = parent;
4945
4946 GtkTreeIter iter;
4947 iter.stamp = m_internal->GetGtkModel()->stamp;
4948 iter.user_data = (gpointer) item.GetID();
4949 gtk_tree_selection_select_iter( selection, &iter );
4950 }
4951
4952 GtkEnableSelectionEvents();
4953 }
4954
4955 void wxDataViewCtrl::Select( const wxDataViewItem & item )
4956 {
4957 ExpandAncestors(item);
4958
4959 GtkDisableSelectionEvents();
4960
4961 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4962
4963 GtkTreeIter iter;
4964 iter.stamp = m_internal->GetGtkModel()->stamp;
4965 iter.user_data = (gpointer) item.GetID();
4966 gtk_tree_selection_select_iter( selection, &iter );
4967
4968 GtkEnableSelectionEvents();
4969 }
4970
4971 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
4972 {
4973 GtkDisableSelectionEvents();
4974
4975 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4976
4977 GtkTreeIter iter;
4978 iter.stamp = m_internal->GetGtkModel()->stamp;
4979 iter.user_data = (gpointer) item.GetID();
4980 gtk_tree_selection_unselect_iter( selection, &iter );
4981
4982 GtkEnableSelectionEvents();
4983 }
4984
4985 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
4986 {
4987 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4988
4989 GtkTreeIter iter;
4990 iter.stamp = m_internal->GetGtkModel()->stamp;
4991 iter.user_data = (gpointer) item.GetID();
4992
4993 return gtk_tree_selection_iter_is_selected( selection, &iter );
4994 }
4995
4996 void wxDataViewCtrl::SelectAll()
4997 {
4998 GtkDisableSelectionEvents();
4999
5000 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5001
5002 gtk_tree_selection_select_all( selection );
5003
5004 GtkEnableSelectionEvents();
5005 }
5006
5007 void wxDataViewCtrl::UnselectAll()
5008 {
5009 GtkDisableSelectionEvents();
5010
5011 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5012
5013 gtk_tree_selection_unselect_all( selection );
5014
5015 GtkEnableSelectionEvents();
5016 }
5017
5018 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,
5019 const wxDataViewColumn *WXUNUSED(column))
5020 {
5021 m_ensureVisibleDefered = item;
5022 ExpandAncestors(item);
5023
5024 GtkTreeIter iter;
5025 iter.user_data = (gpointer) item.GetID();
5026 wxGtkTreePath path(m_internal->get_path( &iter ));
5027 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
5028 }
5029
5030 void wxDataViewCtrl::HitTest(const wxPoint& point,
5031 wxDataViewItem& item,
5032 wxDataViewColumn *& column) const
5033 {
5034 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
5035 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
5036 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
5037 // So we have to use both of them.
5038 // Friedrich Haase 2010-9-20
5039 wxGtkTreePath path, pathScratch;
5040 GtkTreeViewColumn* GtkColumn = NULL;
5041 GtkTreeViewDropPosition pos = GTK_TREE_VIEW_DROP_INTO_OR_AFTER;
5042 gint cell_x = 0;
5043 gint cell_y = 0;
5044
5045 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5046 wxDataViewCtrl* ctrl = (wxDataViewCtrl*)this; // ugly workaround, ctrl is NOT const
5047 GtkTreeView* treeView = GTK_TREE_VIEW(ctrl->GtkGetTreeView());
5048
5049 // is there possibly a better suited function to get the column?
5050 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5051 treeView,
5052 (int) point.x, (int) point.y,
5053 pathScratch.ByRef(),
5054 &GtkColumn, // here we get the GtkColumn
5055 &cell_x,
5056 &cell_y );
5057
5058 if ( GtkColumn != NULL )
5059 {
5060 // we got GTK column
5061 // the right call now which takes the header into account
5062 gtk_tree_view_get_dest_row_at_pos( treeView, (int) point.x, (int) point.y, path.ByRef(), &pos);
5063
5064 if (path)
5065 item = wxDataViewItem(GTKPathToItem(path));
5066 // else we got a GTK column but the position is not over an item, e.g. below last item
5067 for ( unsigned int i=0, cols=GetColumnCount(); i<cols; ++i ) // search the wx column
5068 {
5069 wxDataViewColumn* col = GetColumn(i);
5070 if ( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == GtkColumn )
5071 {
5072 column = col; // here we get the wx column
5073 break;
5074 }
5075 }
5076 }
5077 // else no column and thus no item, both null
5078 }
5079
5080 wxRect
5081 wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item),
5082 const wxDataViewColumn *WXUNUSED(column)) const
5083 {
5084 return wxRect();
5085 }
5086
5087 bool wxDataViewCtrl::SetRowHeight(int rowHeight)
5088 {
5089 m_uniformRowHeight = rowHeight;
5090 return true;
5091 }
5092
5093 void wxDataViewCtrl::DoSetExpanderColumn()
5094 {
5095 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
5096 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5097 }
5098
5099 void wxDataViewCtrl::DoSetIndent()
5100 {
5101 }
5102
5103 void wxDataViewCtrl::GtkDisableSelectionEvents()
5104 {
5105 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5106 g_signal_handlers_disconnect_by_func( selection,
5107 (gpointer) (wxdataview_selection_changed_callback), this);
5108 }
5109
5110 void wxDataViewCtrl::GtkEnableSelectionEvents()
5111 {
5112 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5113 g_signal_connect_after (selection, "changed",
5114 G_CALLBACK (wxdataview_selection_changed_callback), this);
5115 }
5116
5117 // ----------------------------------------------------------------------------
5118 // visual attributes stuff
5119 // ----------------------------------------------------------------------------
5120
5121 // static
5122 wxVisualAttributes
5123 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
5124 {
5125 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
5126 }
5127
5128 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
5129 {
5130 wxDataViewCtrlBase::DoApplyWidgetStyle(style);
5131 gtk_widget_modify_style(m_treeview, style);
5132 }
5133
5134 #endif // !wxUSE_GENERICDATAVIEWCTRL
5135
5136 #endif // wxUSE_DATAVIEWCTRL