1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataview.cpp
3 // Purpose: wxDataViewCtrl GTK+2 implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if wxUSE_DATAVIEWCTRL
15 #include "wx/dataview.h"
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
21 #include "wx/dcclient.h"
23 #include "wx/settings.h"
27 #include "wx/stockitem.h"
28 #include "wx/popupwin.h"
29 #include "wx/listimpl.cpp"
31 #include "wx/gtk/private.h"
32 #include "wx/gtk/dc.h"
33 #include "wx/gtk/dcclient.h"
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
;
40 class wxGtkDataViewModelNotifier
;
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static wxDataViewCtrlInternal
*gs_internal
= NULL
;
47 class wxGtkTreeModelNode
;
50 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
53 // ----------------------------------------------------------------------------
54 // wxGtkTreePath: self-destroying GtkTreePath
55 // ----------------------------------------------------------------------------
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.
65 // Ctor takes ownership of the given path and will free it if non-NULL.
66 wxGtkTreePath(GtkTreePath
*path
= NULL
) : m_path(path
) { }
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
))
74 // Set the stored pointer if not done by ctor.
75 void Assign(GtkTreePath
*path
)
77 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
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.
86 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
92 operator GtkTreePath
*() const { return m_path
; }
94 ~wxGtkTreePath() { if ( m_path
) gtk_tree_path_free(m_path
); }
99 wxDECLARE_NO_COPY_CLASS(wxGtkTreePath
);
102 // ----------------------------------------------------------------------------
103 // wxGtkTreePathList: self-destroying list of GtkTreePath objects.
104 // ----------------------------------------------------------------------------
106 class wxGtkTreePathList
: public wxGtkList
109 // Ctor takes ownership of the list.
110 explicit wxGtkTreePathList(GList
* list
)
117 // Delete the list contents, wxGtkList will delete the list itself.
118 g_list_foreach(m_list
, (GFunc
)gtk_tree_path_free
, NULL
);
122 // ----------------------------------------------------------------------------
123 // wxGtkTreeSelectionLock: prevent selection from changing during the
124 // lifetime of this object
125 // ----------------------------------------------------------------------------
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
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
141 gboolean
wxdataview_selection_func(GtkTreeSelection
* WXUNUSED(selection
),
142 GtkTreeModel
* WXUNUSED(model
),
143 GtkTreePath
* WXUNUSED(path
),
144 gboolean
WXUNUSED(path_currently_selected
),
150 class wxGtkTreeSelectionLock
153 wxGtkTreeSelectionLock(GtkTreeSelection
*selection
)
154 : m_selection(selection
)
156 wxASSERT_MSG( !ms_instance
, "this class is not reentrant currently" );
160 CheckCurrentSelectionFunc(NULL
);
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
,
170 ~wxGtkTreeSelectionLock()
172 CheckCurrentSelectionFunc(wxdataview_selection_func
);
174 gtk_tree_selection_set_select_function(m_selection
,
175 wxdataview_selection_func
,
183 void CheckCurrentSelectionFunc(GtkTreeSelectionFunc func
)
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
)
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.
196 gtk_tree_selection_get_select_function(m_selection
) == func
,
197 "selection function has changed unexpectedly, review this code!"
204 static wxGtkTreeSelectionLock
*ms_instance
;
206 GtkTreeSelection
* const m_selection
;
208 wxDECLARE_NO_COPY_CLASS(wxGtkTreeSelectionLock
);
211 wxGtkTreeSelectionLock
*wxGtkTreeSelectionLock::ms_instance
= NULL
;
213 //-----------------------------------------------------------------------------
214 // wxDataViewCtrlInternal
215 //-----------------------------------------------------------------------------
217 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
218 WX_DEFINE_LIST(ItemList
)
220 class wxDataViewCtrlInternal
223 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
);
224 ~wxDataViewCtrlInternal();
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
);
239 bool EnableDragSource( const wxDataFormat
&format
);
240 bool EnableDropTarget( const wxDataFormat
&format
);
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
);
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
);
262 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
263 GtkSortType
GetSortOrder() const { return m_sort_order
; }
265 void SetSortColumn( int column
) { m_sort_column
= column
; }
266 int GetSortColumn() const { return m_sort_column
; }
268 void SetDataViewSortColumn( wxDataViewColumn
*column
) { m_dataview_sort_column
= column
; }
269 wxDataViewColumn
*GetDataViewSortColumn() { return m_dataview_sort_column
; }
271 bool IsSorted() const { return m_sort_column
>= 0; }
273 // Should we be sorted either because we have a configured sort column or
274 // because we have a default sort order?
275 bool ShouldBeSorted() const
277 return IsSorted() || GetDataViewModel()->HasDefaultCompare();
282 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
283 const wxDataViewModel
* GetDataViewModel() const { return m_wx_model
; }
284 wxDataViewCtrl
* GetOwner() { return m_owner
; }
285 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
287 // item can be deleted already in the model
288 int GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
290 void OnInternalIdle();
294 void ScheduleRefresh();
296 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
297 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
298 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
299 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
300 void BuildBranch( wxGtkTreeModelNode
*branch
);
303 wxGtkTreeModelNode
*m_root
;
304 wxDataViewModel
*m_wx_model
;
305 GtkWxTreeModel
*m_gtk_model
;
306 wxDataViewCtrl
*m_owner
;
307 GtkSortType m_sort_order
;
308 wxDataViewColumn
*m_dataview_sort_column
;
311 GtkTargetEntry m_dragSourceTargetEntry
;
312 wxCharBuffer m_dragSourceTargetEntryTarget
;
313 wxDataObject
*m_dragDataObject
;
315 GtkTargetEntry m_dropTargetTargetEntry
;
316 wxCharBuffer m_dropTargetTargetEntryTarget
;
317 wxDataObject
*m_dropDataObject
;
319 wxGtkDataViewModelNotifier
*m_notifier
;
325 //-----------------------------------------------------------------------------
326 // wxGtkTreeModelNode
327 //-----------------------------------------------------------------------------
330 int LINKAGEMODE
wxGtkTreeModelChildCmp( void** id1
, void** id2
)
332 int ret
= gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(*id1
), wxDataViewItem(*id2
),
333 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
338 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
339 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren
);
341 class wxGtkTreeModelNode
344 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
345 wxDataViewCtrlInternal
*internal
)
349 m_internal
= internal
;
352 ~wxGtkTreeModelNode()
354 size_t count
= m_nodes
.GetCount();
356 for (i
= 0; i
< count
; i
++)
358 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
363 void AddNode( wxGtkTreeModelNode
* child
)
365 m_nodes
.Add( child
);
367 void *id
= child
->GetItem().GetID();
369 m_children
.Add( id
);
371 if (m_internal
->ShouldBeSorted())
373 gs_internal
= m_internal
;
374 m_children
.Sort( &wxGtkTreeModelChildCmp
);
378 void InsertNode( wxGtkTreeModelNode
* child
, unsigned pos
)
380 if (m_internal
->ShouldBeSorted())
386 void *id
= child
->GetItem().GetID();
388 // Insert into m_nodes so that the order of nodes in m_nodes is the
389 // same as the order of their corresponding IDs in m_children:
390 const unsigned int count
= m_nodes
.GetCount();
391 bool inserted
= false;
392 for (unsigned i
= 0; i
< count
; i
++)
394 wxGtkTreeModelNode
*node
= m_nodes
[i
];
395 int posInChildren
= m_children
.Index(node
->GetItem().GetID());
396 if ( (unsigned)posInChildren
>= pos
)
398 m_nodes
.Insert(child
, i
);
406 m_children
.Insert( id
, pos
);
409 void AddLeaf( void* id
)
411 InsertLeaf(id
, m_children
.size());
414 void InsertLeaf( void* id
, unsigned pos
)
416 m_children
.Insert( id
, pos
);
418 if (m_internal
->ShouldBeSorted())
420 gs_internal
= m_internal
;
421 m_children
.Sort( &wxGtkTreeModelChildCmp
);
425 void DeleteChild( void* id
)
427 m_children
.Remove( id
);
429 unsigned int count
= m_nodes
.GetCount();
431 for (pos
= 0; pos
< count
; pos
++)
433 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
434 if (node
->GetItem().GetID() == id
)
436 m_nodes
.RemoveAt( pos
);
443 // returns position of child node for given item in children list or wxNOT_FOUND
444 int FindChildByItem(const wxDataViewItem
& item
) const
446 const void* itemId
= item
.GetID();
447 const wxGtkTreeModelChildren
& nodes
= m_children
;
448 const int len
= nodes
.size();
449 for ( int i
= 0; i
< len
; i
++ )
451 if ( nodes
[i
] == itemId
)
457 wxGtkTreeModelNode
* GetParent()
459 wxGtkTreeModelNodes
&GetNodes()
461 wxGtkTreeModelChildren
&GetChildren()
462 { return m_children
; }
464 unsigned int GetChildCount() const { return m_children
.GetCount(); }
465 unsigned int GetNodesCount() const { return m_nodes
.GetCount(); }
467 wxDataViewItem
&GetItem() { return m_item
; }
468 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
473 wxGtkTreeModelNode
*m_parent
;
474 wxGtkTreeModelNodes m_nodes
;
475 wxGtkTreeModelChildren m_children
;
476 wxDataViewItem m_item
;
477 wxDataViewCtrlInternal
*m_internal
;
481 //-----------------------------------------------------------------------------
483 //-----------------------------------------------------------------------------
485 extern bool g_blockEventsOnDrag
;
487 //-----------------------------------------------------------------------------
488 // define new GTK+ class wxGtkTreeModel
489 //-----------------------------------------------------------------------------
493 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
494 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
495 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
496 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
498 GType
gtk_wx_tree_model_get_type (void);
500 struct _GtkWxTreeModel
506 wxDataViewCtrlInternal
*internal
;
509 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
510 static void wxgtk_tree_model_init (GTypeInstance
* instance
, void*);
512 static void wxgtk_tree_model_tree_model_init (void* g_iface
, void*);
513 static void wxgtk_tree_model_sortable_init (void* g_iface
, void*);
514 static void wxgtk_tree_model_drag_source_init(void* g_iface
, void*);
515 static void wxgtk_tree_model_drag_dest_init (void* g_iface
, void*);
517 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
518 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
519 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
521 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
524 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
526 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
530 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
532 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
534 GtkTreeIter
*parent
);
535 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
537 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
539 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
543 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
548 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
549 gint
*sort_column_id
,
551 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
554 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
556 GtkTreeIterCompareFunc func
,
558 GDestroyNotify destroy
);
559 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
560 GtkTreeIterCompareFunc func
,
562 GDestroyNotify destroy
);
563 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
566 static gboolean
wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
568 static gboolean
wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
570 static gboolean
wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
572 GtkSelectionData
*selection_data
);
573 static gboolean
wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
575 GtkSelectionData
*selection_data
);
576 static gboolean
wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
577 GtkTreePath
*dest_path
,
578 GtkSelectionData
*selection_data
);
581 gtk_wx_tree_model_get_type (void)
583 static GType tree_model_type
= 0;
585 if (!tree_model_type
)
587 const GTypeInfo tree_model_info
=
589 sizeof (GObjectClass
),
590 NULL
, /* base_init */
591 NULL
, /* base_finalize */
593 NULL
, /* class_finalize */
594 NULL
, /* class_data */
595 sizeof (GtkWxTreeModel
),
597 wxgtk_tree_model_init
,
600 static const GInterfaceInfo tree_model_iface_info
=
602 wxgtk_tree_model_tree_model_init
,
607 static const GInterfaceInfo sortable_iface_info
=
609 wxgtk_tree_model_sortable_init
,
614 static const GInterfaceInfo drag_source_iface_info
=
616 wxgtk_tree_model_drag_source_init
,
621 static const GInterfaceInfo drag_dest_iface_info
=
623 wxgtk_tree_model_drag_dest_init
,
628 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
629 &tree_model_info
, (GTypeFlags
)0 );
631 g_type_add_interface_static (tree_model_type
,
633 &tree_model_iface_info
);
634 g_type_add_interface_static (tree_model_type
,
635 GTK_TYPE_TREE_SORTABLE
,
636 &sortable_iface_info
);
637 g_type_add_interface_static (tree_model_type
,
638 GTK_TYPE_TREE_DRAG_DEST
,
639 &drag_dest_iface_info
);
640 g_type_add_interface_static (tree_model_type
,
641 GTK_TYPE_TREE_DRAG_SOURCE
,
642 &drag_source_iface_info
);
645 return tree_model_type
;
648 static GtkWxTreeModel
*
649 wxgtk_tree_model_new(void)
651 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
656 wxgtk_tree_model_tree_model_init(void* g_iface
, void*)
658 GtkTreeModelIface
* iface
= static_cast<GtkTreeModelIface
*>(g_iface
);
659 iface
->get_flags
= wxgtk_tree_model_get_flags
;
660 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
661 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
662 iface
->get_iter
= wxgtk_tree_model_get_iter
;
663 iface
->get_path
= wxgtk_tree_model_get_path
;
664 iface
->get_value
= wxgtk_tree_model_get_value
;
665 iface
->iter_next
= wxgtk_tree_model_iter_next
;
666 iface
->iter_children
= wxgtk_tree_model_iter_children
;
667 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
668 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
669 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
670 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
674 wxgtk_tree_model_sortable_init(void* g_iface
, void*)
676 GtkTreeSortableIface
* iface
= static_cast<GtkTreeSortableIface
*>(g_iface
);
677 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
678 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
679 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
680 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
681 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
685 wxgtk_tree_model_drag_source_init(void* g_iface
, void*)
687 GtkTreeDragSourceIface
* iface
= static_cast<GtkTreeDragSourceIface
*>(g_iface
);
688 iface
->row_draggable
= wxgtk_tree_model_row_draggable
;
689 iface
->drag_data_delete
= wxgtk_tree_model_drag_data_delete
;
690 iface
->drag_data_get
= wxgtk_tree_model_drag_data_get
;
694 wxgtk_tree_model_drag_dest_init(void* g_iface
, void*)
696 GtkTreeDragDestIface
* iface
= static_cast<GtkTreeDragDestIface
*>(g_iface
);
697 iface
->drag_data_received
= wxgtk_tree_model_drag_data_received
;
698 iface
->row_drop_possible
= wxgtk_tree_model_row_drop_possible
;
702 wxgtk_tree_model_init(GTypeInstance
* instance
, void*)
704 GtkWxTreeModel
* tree_model
= GTK_WX_TREE_MODEL(instance
);
705 tree_model
->internal
= NULL
;
706 tree_model
->stamp
= g_random_int();
711 //-----------------------------------------------------------------------------
712 // implement callbacks from wxGtkTreeModel class by letting
713 // them call the methods of wxWidgets' wxDataViewModel
714 //-----------------------------------------------------------------------------
716 static GtkTreeModelFlags
717 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
719 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
720 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), (GtkTreeModelFlags
)0 );
722 return wxtree_model
->internal
->get_flags();
726 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
728 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
729 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
731 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
735 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
738 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
739 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
741 GType gtype
= G_TYPE_INVALID
;
743 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
745 if (wxtype
== wxT("string"))
746 gtype
= G_TYPE_STRING
;
749 gtype
= G_TYPE_POINTER
;
750 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
757 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
761 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
762 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
763 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
765 return wxtree_model
->internal
->get_iter( iter
, path
);
769 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
772 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
773 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
774 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
776 return wxtree_model
->internal
->get_path( iter
);
780 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
785 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
786 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
788 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
789 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
790 if (mtype
== wxT("string"))
793 g_value_init( value
, G_TYPE_STRING
);
794 wxDataViewItem
item( (void*) iter
->user_data
);
795 model
->GetValue( variant
, item
, (unsigned int) column
);
797 g_value_set_string( value
, variant
.GetString().utf8_str() );
801 wxFAIL_MSG( wxT("non-string columns not supported yet") );
806 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
809 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
811 // This happens when clearing the view by calling .._set_model( NULL );
812 if (iter
->stamp
== 0) return FALSE
;
814 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
815 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
817 return wxtree_model
->internal
->iter_next( iter
);
821 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
825 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
826 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
829 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
832 return wxtree_model
->internal
->iter_children( iter
, parent
);
836 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
839 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
840 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
841 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
843 return wxtree_model
->internal
->iter_has_child( iter
);
847 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
850 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
851 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
852 g_return_val_if_fail ( !iter
|| wxtree_model
->stamp
== iter
->stamp
, 0);
854 return wxtree_model
->internal
->iter_n_children( iter
);
858 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
863 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
864 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
866 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
870 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
874 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
875 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
876 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
878 return wxtree_model
->internal
->iter_parent( iter
, child
);
881 /* drag'n'drop iface */
883 wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
886 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
887 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
889 return wxtree_model
->internal
->row_draggable( drag_source
, path
);
893 wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
896 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
897 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
899 return wxtree_model
->internal
->drag_data_delete( drag_source
, path
);
903 wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
905 GtkSelectionData
*selection_data
)
907 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
908 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
911 wxPrintf( "drag_get_data\n");
913 wxGtkString
atom_selection(gdk_atom_name(selection_data
->selection
));
914 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection
) );
916 wxGtkString
atom_target(gdk_atom_name(selection_data
->target
));
917 wxPrintf( "target %s\n", wxString::FromAscii(atom_target
) );
919 wxGtkString
atom_type(gdk_atom_name(selection_data
->type
));
920 wxPrintf( "type %s\n", wxString::FromAscii(atom_type
) );
922 wxPrintf( "format %d\n", selection_data
->format
);
925 return wxtree_model
->internal
->drag_data_get( drag_source
, path
, selection_data
);
929 wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
931 GtkSelectionData
*selection_data
)
933 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
934 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
936 return wxtree_model
->internal
->drag_data_received( drag_dest
, dest
, selection_data
);
940 wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
941 GtkTreePath
*dest_path
,
942 GtkSelectionData
*selection_data
)
944 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
945 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
947 return wxtree_model
->internal
->row_drop_possible( drag_dest
, dest_path
, selection_data
);
952 wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
953 gint
*sort_column_id
,
956 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) sortable
;
958 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
960 if (!wxtree_model
->internal
->IsSorted())
963 *sort_column_id
= -1;
970 *sort_column_id
= wxtree_model
->internal
->GetSortColumn();
973 *order
= wxtree_model
->internal
->GetSortOrder();
978 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
981 wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
985 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
986 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
988 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
990 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
991 (order
!= tree_model
->internal
->GetSortOrder()))
993 tree_model
->internal
->SetSortColumn( sort_column_id
);
994 tree_model
->internal
->SetSortOrder( order
);
996 gtk_tree_sortable_sort_column_changed (sortable
);
998 tree_model
->internal
->GetDataViewModel()->Resort();
1001 if (gs_lastLeftClickHeader
)
1003 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
1004 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
1005 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
1006 event
.SetModel( dv
->GetModel() );
1007 dv
->HandleWindowEvent( event
);
1010 gs_lastLeftClickHeader
= NULL
;
1014 wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
1015 gint
WXUNUSED(sort_column_id
),
1016 GtkTreeIterCompareFunc func
,
1017 gpointer
WXUNUSED(data
),
1018 GDestroyNotify
WXUNUSED(destroy
))
1020 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1021 g_return_if_fail (func
!= NULL
);
1025 wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
1026 GtkTreeIterCompareFunc func
,
1027 gpointer
WXUNUSED(data
),
1028 GDestroyNotify
WXUNUSED(destroy
))
1030 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1031 g_return_if_fail (func
!= NULL
);
1033 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
1034 // TODO: remove this code
1038 wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
1040 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
1045 //-----------------------------------------------------------------------------
1046 // define new GTK+ class GtkWxRendererText
1047 //-----------------------------------------------------------------------------
1051 #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
1052 #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
1053 #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1054 #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1056 GType
gtk_wx_cell_renderer_text_get_type (void);
1058 typedef struct _GtkWxCellRendererText GtkWxCellRendererText
;
1060 struct _GtkWxCellRendererText
1062 GtkCellRendererText parent
;
1064 wxDataViewRenderer
*wx_renderer
;
1067 static GtkWxCellRendererText
*gtk_wx_cell_renderer_text_new (void);
1068 static void gtk_wx_cell_renderer_text_init (
1069 GTypeInstance
* instance
, void*);
1070 static void gtk_wx_cell_renderer_text_class_init(
1071 void* klass
, void*);
1072 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1073 GtkCellRenderer
*cell
,
1077 GdkRectangle
*background_area
,
1078 GdkRectangle
*cell_area
,
1079 GtkCellRendererState flags
);
1082 static GObjectClass
*text_cell_parent_class
= NULL
;
1087 gtk_wx_cell_renderer_text_get_type (void)
1089 static GType cell_wx_type
= 0;
1093 const GTypeInfo cell_wx_info
=
1095 sizeof (GtkCellRendererTextClass
),
1096 NULL
, /* base_init */
1097 NULL
, /* base_finalize */
1098 gtk_wx_cell_renderer_text_class_init
,
1099 NULL
, /* class_finalize */
1100 NULL
, /* class_data */
1101 sizeof (GtkWxCellRendererText
),
1102 0, /* n_preallocs */
1103 gtk_wx_cell_renderer_text_init
,
1106 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT
,
1107 "GtkWxCellRendererText", &cell_wx_info
, (GTypeFlags
)0 );
1110 return cell_wx_type
;
1114 gtk_wx_cell_renderer_text_init(GTypeInstance
* instance
, void*)
1116 GtkWxCellRendererText
* cell
= GTK_WX_CELL_RENDERER_TEXT(instance
);
1117 cell
->wx_renderer
= NULL
;
1121 gtk_wx_cell_renderer_text_class_init(void* klass
, void*)
1123 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1125 text_cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1127 cell_class
->start_editing
= gtk_wx_cell_renderer_text_start_editing
;
1130 GtkWxCellRendererText
*
1131 gtk_wx_cell_renderer_text_new (void)
1133 return (GtkWxCellRendererText
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT
, NULL
);
1136 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1137 GtkCellRenderer
*gtk_renderer
,
1138 GdkEvent
*gdk_event
,
1141 GdkRectangle
*background_area
,
1142 GdkRectangle
*cell_area
,
1143 GtkCellRendererState flags
)
1145 GtkWxCellRendererText
*wxgtk_renderer
= (GtkWxCellRendererText
*) gtk_renderer
;
1146 wxDataViewRenderer
*wx_renderer
= wxgtk_renderer
->wx_renderer
;
1147 wxDataViewColumn
*column
= wx_renderer
->GetOwner();
1150 item(column
->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1152 wxDataViewCtrl
*dv
= column
->GetOwner();
1153 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING
, dv
->GetId() );
1154 event
.SetDataViewColumn( column
);
1155 event
.SetModel( dv
->GetModel() );
1156 event
.SetColumn( column
->GetModelColumn() );
1157 event
.SetItem( item
);
1158 dv
->HandleWindowEvent( event
);
1160 if (event
.IsAllowed())
1161 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class
)->
1162 start_editing( gtk_renderer
, gdk_event
, widget
, path
, background_area
, cell_area
, flags
);
1167 //-----------------------------------------------------------------------------
1168 // define new GTK+ class GtkWxCellRenderer
1169 //-----------------------------------------------------------------------------
1173 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
1174 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
1175 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
1176 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
1178 GType
gtk_wx_cell_renderer_get_type (void);
1180 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
1182 struct _GtkWxCellRenderer
1184 GtkCellRenderer parent
;
1187 wxDataViewCustomRenderer
*cell
;
1190 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
1191 static void gtk_wx_cell_renderer_init (
1192 GTypeInstance
* instance
, void*);
1193 static void gtk_wx_cell_renderer_class_init(
1194 void* klass
, void*);
1195 static void gtk_wx_cell_renderer_get_size (
1196 GtkCellRenderer
*cell
,
1198 GdkRectangle
*rectangle
,
1203 static void gtk_wx_cell_renderer_render (
1204 GtkCellRenderer
*cell
,
1207 GdkRectangle
*background_area
,
1208 GdkRectangle
*cell_area
,
1209 GdkRectangle
*expose_area
,
1210 GtkCellRendererState flags
);
1211 static gboolean
gtk_wx_cell_renderer_activate(
1212 GtkCellRenderer
*cell
,
1216 GdkRectangle
*background_area
,
1217 GdkRectangle
*cell_area
,
1218 GtkCellRendererState flags
);
1219 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1220 GtkCellRenderer
*cell
,
1224 GdkRectangle
*background_area
,
1225 GdkRectangle
*cell_area
,
1226 GtkCellRendererState flags
);
1231 gtk_wx_cell_renderer_get_type (void)
1233 static GType cell_wx_type
= 0;
1237 const GTypeInfo cell_wx_info
=
1239 sizeof (GtkCellRendererClass
),
1240 NULL
, /* base_init */
1241 NULL
, /* base_finalize */
1242 gtk_wx_cell_renderer_class_init
,
1243 NULL
, /* class_finalize */
1244 NULL
, /* class_data */
1245 sizeof (GtkWxCellRenderer
),
1246 0, /* n_preallocs */
1247 gtk_wx_cell_renderer_init
,
1250 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
1251 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
1254 return cell_wx_type
;
1258 gtk_wx_cell_renderer_init(GTypeInstance
* instance
, void*)
1260 GtkWxCellRenderer
* cell
= GTK_WX_CELL_RENDERER(instance
);
1265 gtk_wx_cell_renderer_class_init(void* klass
, void*)
1267 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1269 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
1270 cell_class
->render
= gtk_wx_cell_renderer_render
;
1271 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
1272 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
1276 gtk_wx_cell_renderer_new (void)
1278 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
1281 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1282 GtkCellRenderer
*renderer
,
1283 GdkEvent
*WXUNUSED(event
),
1286 GdkRectangle
*WXUNUSED(background_area
),
1287 GdkRectangle
*cell_area
,
1288 GtkCellRendererState
WXUNUSED(flags
) )
1290 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1291 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1293 // Renderer doesn't support in-place editing
1294 if (!cell
->HasEditorCtrl())
1297 // An in-place editing control is still around
1298 if (cell
->GetEditorCtrl())
1302 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1308 rect
.x
+= cell_area
->x
;
1309 rect
.y
+= cell_area
->y
;
1310 // rect.width -= renderer->xpad * 2;
1311 // rect.height -= renderer->ypad * 2;
1313 // wxRect renderrect(wxRectFromGDKRect(&rect));
1314 wxRect
renderrect(wxRectFromGDKRect(cell_area
));
1317 item(cell
->GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1319 cell
->StartEditing( item
, renderrect
);
1325 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
1326 GtkWidget
*WXUNUSED(widget
),
1327 GdkRectangle
*cell_area
,
1333 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1334 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1336 wxSize size
= cell
->GetSize();
1338 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1340 // Uniform row height, if specified, overrides the value returned by the
1342 if ( !ctrl
->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
1344 const int uniformHeight
= ctrl
->GTKGetUniformRowHeight();
1345 if ( uniformHeight
> 0 )
1346 size
.y
= uniformHeight
;
1350 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1351 int calc_width
= xpad
* 2 + size
.x
;
1352 int calc_height
= ypad
* 2 + size
.y
;
1359 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
1361 float xalign
, yalign
;
1362 gtk_cell_renderer_get_alignment(renderer
, &xalign
, &yalign
);
1365 *x_offset
= int(xalign
* (cell_area
->width
- calc_width
- 2 * xpad
));
1366 *x_offset
= MAX(*x_offset
, 0) + xpad
;
1370 *y_offset
= int(yalign
* (cell_area
->height
- calc_height
- 2 * ypad
));
1371 *y_offset
= MAX(*y_offset
, 0) + ypad
;
1376 *width
= calc_width
;
1379 *height
= calc_height
;
1382 struct wxDataViewCustomRenderer::GTKRenderParams
1385 GdkRectangle
* expose_area
;
1387 GdkRectangle
* background_area
;
1392 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
1395 GdkRectangle
*background_area
,
1396 GdkRectangle
*cell_area
,
1397 GdkRectangle
*expose_area
,
1398 GtkCellRendererState flags
)
1401 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1402 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1404 wxDataViewCustomRenderer::GTKRenderParams renderParams
;
1405 renderParams
.window
= window
;
1406 renderParams
.expose_area
= expose_area
;
1407 renderParams
.widget
= widget
;
1408 renderParams
.background_area
= background_area
;
1409 renderParams
.flags
= flags
;
1410 cell
->GTKSetRenderParams(&renderParams
);
1412 wxRect
rect(wxRectFromGDKRect(cell_area
));
1414 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1415 rect
= rect
.Deflate(xpad
, ypad
);
1417 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
1418 wxWindowDCImpl
*impl
= (wxWindowDCImpl
*) dc
->GetImpl();
1420 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1421 // window such as a DnD drop window.
1422 if (window
!= impl
->m_gdkwindow
)
1425 impl
->m_gdkwindow
= window
;
1430 if (flags
& GTK_CELL_RENDERER_SELECTED
)
1431 state
|= wxDATAVIEW_CELL_SELECTED
;
1432 if (flags
& GTK_CELL_RENDERER_PRELIT
)
1433 state
|= wxDATAVIEW_CELL_PRELIT
;
1434 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1435 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1436 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1437 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1438 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
1439 state
|= wxDATAVIEW_CELL_FOCUSED
;
1440 cell
->WXCallRender( rect
, dc
, state
);
1442 cell
->GTKSetRenderParams(NULL
);
1446 gtk_wx_cell_renderer_activate(
1447 GtkCellRenderer
*renderer
,
1451 GdkRectangle
*WXUNUSED(background_area
),
1452 GdkRectangle
*cell_area
,
1453 GtkCellRendererState
WXUNUSED(flags
) )
1455 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1456 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1459 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1465 rect
.x
+= cell_area
->x
;
1466 rect
.y
+= cell_area
->y
;
1468 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1469 rect
.width
-= xpad
* 2;
1470 rect
.height
-= ypad
* 2;
1472 wxRect
renderrect(wxRectFromGDKRect(&rect
));
1474 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1475 wxDataViewModel
*model
= ctrl
->GetModel();
1477 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
1479 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1483 // activated by <ENTER>
1484 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, NULL
);
1486 else if ( event
->type
== GDK_BUTTON_PRESS
)
1488 GdkEventButton
*button_event
= (GdkEventButton
*)event
;
1489 if ( button_event
->button
== 1 )
1491 wxMouseEvent
mouse_event(wxEVT_LEFT_DOWN
);
1492 InitMouseEvent(ctrl
, mouse_event
, button_event
);
1494 mouse_event
.m_x
-= renderrect
.x
;
1495 mouse_event
.m_y
-= renderrect
.y
;
1497 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, &mouse_event
);
1501 wxLogDebug("unexpected event type in gtk_wx_cell_renderer_activate()");
1505 // ---------------------------------------------------------
1506 // wxGtkDataViewModelNotifier
1507 // ---------------------------------------------------------
1509 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1512 wxGtkDataViewModelNotifier( wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
);
1513 ~wxGtkDataViewModelNotifier();
1515 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1516 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1517 virtual bool ItemChanged( const wxDataViewItem
&item
);
1518 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
1519 virtual bool Cleared();
1520 virtual void Resort();
1521 virtual bool BeforeReset();
1522 virtual bool AfterReset();
1524 void UpdateLastCount();
1527 wxDataViewModel
*m_wx_model
;
1528 wxDataViewCtrlInternal
*m_internal
;
1531 // ---------------------------------------------------------
1532 // wxGtkDataViewListModelNotifier
1533 // ---------------------------------------------------------
1535 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1536 wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
)
1538 m_wx_model
= wx_model
;
1539 m_internal
= internal
;
1542 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1548 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1550 m_internal
->ItemAdded( parent
, item
);
1551 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1554 iter
.stamp
= wxgtk_model
->stamp
;
1555 iter
.user_data
= item
.GetID();
1557 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1558 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1559 gtk_tree_model_row_inserted(
1560 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1565 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1567 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1569 // using _get_path for a deleted item cannot be
1572 iter
.stamp
= wxgtk_model
->stamp
;
1573 iter
.user_data
= (gpointer
) item
.GetID();
1574 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1575 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1577 // so get the path from the parent
1578 GtkTreeIter parentIter
;
1579 parentIter
.stamp
= wxgtk_model
->stamp
;
1580 parentIter
.user_data
= (gpointer
) parent
.GetID();
1581 wxGtkTreePath
parentPath(wxgtk_tree_model_get_path(
1582 GTK_TREE_MODEL(wxgtk_model
), &parentIter
));
1584 // and add the final index ourselves
1585 wxGtkTreePath
path(gtk_tree_path_copy(parentPath
));
1586 int index
= m_internal
->GetIndexOf( parent
, item
);
1587 gtk_tree_path_append_index( path
, index
);
1590 gtk_tree_model_row_deleted(
1591 GTK_TREE_MODEL(wxgtk_model
), path
);
1593 m_internal
->ItemDeleted( parent
, item
);
1595 // Did we remove the last child, causing 'parent' to become a leaf?
1596 if ( !m_wx_model
->IsContainer(parent
) )
1598 gtk_tree_model_row_has_child_toggled
1600 GTK_TREE_MODEL(wxgtk_model
),
1609 void wxGtkDataViewModelNotifier::Resort()
1611 m_internal
->Resort();
1614 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1616 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1619 iter
.stamp
= wxgtk_model
->stamp
;
1620 iter
.user_data
= (gpointer
) item
.GetID();
1622 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1623 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1624 gtk_tree_model_row_changed(
1625 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1627 m_internal
->ItemChanged( item
);
1632 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
)
1634 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1635 wxDataViewCtrl
*ctrl
= m_internal
->GetOwner();
1637 // This adds GTK+'s missing MVC logic for ValueChanged
1639 for (index
= 0; index
< ctrl
->GetColumnCount(); index
++)
1641 wxDataViewColumn
*column
= ctrl
->GetColumn( index
);
1642 if (column
->GetModelColumn() == model_column
)
1644 GtkTreeView
*widget
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
1645 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1647 // Don't attempt to refresh not yet realized tree, it is useless
1648 // and results in GTK errors.
1649 if ( gtk_widget_get_realized(ctrl
->GtkGetTreeView()) )
1653 iter
.stamp
= wxgtk_model
->stamp
;
1654 iter
.user_data
= (gpointer
) item
.GetID();
1655 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1656 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1657 GdkRectangle cell_area
;
1658 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1660 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1661 double d
= gtk_adjustment_get_value( hadjust
);
1662 int xdiff
= (int) d
;
1664 int ydiff
= gcolumn
->button
->allocation
.height
;
1666 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1667 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1670 m_internal
->ValueChanged( item
, model_column
);
1679 bool wxGtkDataViewModelNotifier::BeforeReset()
1681 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1682 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), NULL
);
1687 bool wxGtkDataViewModelNotifier::AfterReset()
1689 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1690 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1692 m_internal
->Cleared();
1694 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), GTK_TREE_MODEL(wxgtk_model
) );
1699 bool wxGtkDataViewModelNotifier::Cleared()
1701 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1703 // There is no call to tell the model that everything
1704 // has been deleted so call row_deleted() for every
1707 int count
= m_internal
->iter_n_children( NULL
); // number of children of root
1709 GtkTreePath
*path
= gtk_tree_path_new_first(); // points to root
1712 for (i
= 0; i
< count
; i
++)
1713 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model
), path
);
1715 gtk_tree_path_free( path
);
1717 m_internal
->Cleared();
1722 // ---------------------------------------------------------
1723 // wxDataViewRenderer
1724 // ---------------------------------------------------------
1726 static gpointer s_user_data
= NULL
;
1729 wxgtk_cell_editable_editing_done( GtkCellEditable
*WXUNUSED(editable
),
1730 wxDataViewRenderer
*wxrenderer
)
1732 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1733 wxDataViewCtrl
*dv
= column
->GetOwner();
1734 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1735 event
.SetDataViewColumn( column
);
1736 event
.SetModel( dv
->GetModel() );
1737 wxDataViewItem
item( s_user_data
);
1738 event
.SetItem( item
);
1739 dv
->HandleWindowEvent( event
);
1743 wxgtk_renderer_editing_started( GtkCellRenderer
*WXUNUSED(cell
), GtkCellEditable
*editable
,
1744 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1749 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1750 wxDataViewCtrl
*dv
= column
->GetOwner();
1751 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1752 event
.SetDataViewColumn( column
);
1753 event
.SetModel( dv
->GetModel() );
1754 wxDataViewItem
item(dv
->GTKPathToItem(wxGtkTreePath(path
)));
1755 event
.SetItem( item
);
1756 dv
->HandleWindowEvent( event
);
1758 if (GTK_IS_CELL_EDITABLE(editable
))
1760 s_user_data
= item
.GetID();
1762 g_signal_connect (GTK_CELL_EDITABLE (editable
), "editing_done",
1763 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1764 (gpointer
) wxrenderer
);
1770 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1772 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1774 wxDataViewRendererBase( varianttype
, mode
, align
)
1779 // we haven't changed them yet
1780 m_usingDefaultAttrs
= true;
1782 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1783 // after the m_renderer pointer has been initialized
1786 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
1788 gtk_tree_view_column_pack_end( column
, m_renderer
, TRUE
/* expand */);
1791 void wxDataViewRenderer::GtkInitHandlers()
1793 if (!gtk_check_version(2,6,0))
1795 g_signal_connect (GTK_CELL_RENDERER(m_renderer
), "editing_started",
1796 G_CALLBACK (wxgtk_renderer_editing_started
),
1801 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1803 GtkCellRendererMode gtkMode
;
1806 case wxDATAVIEW_CELL_INERT
:
1807 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1810 case wxDATAVIEW_CELL_ACTIVATABLE
:
1811 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1814 case wxDATAVIEW_CELL_EDITABLE
:
1815 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1819 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1825 // This value is most often ignored in GtkTreeView
1826 GValue gvalue
= { 0, };
1827 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1828 g_value_set_enum( &gvalue
, gtkMode
);
1829 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1830 g_value_unset( &gvalue
);
1833 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1835 wxDataViewCellMode ret
;
1838 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1840 switch (g_value_get_enum(&gvalue
))
1843 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1844 // fall through (we have to return something)
1846 case GTK_CELL_RENDERER_MODE_INERT
:
1847 ret
= wxDATAVIEW_CELL_INERT
;
1850 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1851 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1854 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1855 ret
= wxDATAVIEW_CELL_EDITABLE
;
1859 g_value_unset( &gvalue
);
1864 void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer
*renderer
)
1866 int align
= m_alignment
;
1868 // query alignment from column ?
1872 if (GetOwner() == NULL
)
1875 align
= GetOwner()->GetAlignment();
1876 align
|= wxALIGN_CENTRE_VERTICAL
;
1879 // horizontal alignment:
1881 gfloat xalign
= 0.0;
1882 if (align
& wxALIGN_RIGHT
)
1884 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1887 GValue gvalue
= { 0, };
1888 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1889 g_value_set_float( &gvalue
, xalign
);
1890 g_object_set_property( G_OBJECT(renderer
), "xalign", &gvalue
);
1891 g_value_unset( &gvalue
);
1893 // vertical alignment:
1895 gfloat yalign
= 0.0;
1896 if (align
& wxALIGN_BOTTOM
)
1898 else if (align
& wxALIGN_CENTER_VERTICAL
)
1901 GValue gvalue2
= { 0, };
1902 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1903 g_value_set_float( &gvalue2
, yalign
);
1904 g_object_set_property( G_OBJECT(renderer
), "yalign", &gvalue2
);
1905 g_value_unset( &gvalue2
);
1908 void wxDataViewRenderer::SetAlignment( int align
)
1910 m_alignment
= align
;
1911 GtkUpdateAlignment();
1914 int wxDataViewRenderer::GetAlignment() const
1919 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode
)
1922 if ( gtk_check_version(2, 6, 0) != NULL
)
1925 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1929 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1930 // can just cast between them
1931 GValue gvalue
= { 0, };
1932 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1933 g_value_set_enum( &gvalue
, static_cast<PangoEllipsizeMode
>(mode
) );
1934 g_object_set_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1935 g_value_unset( &gvalue
);
1938 #endif // GTK 2.6/before
1941 wxEllipsizeMode
wxDataViewRenderer::GetEllipsizeMode() const
1944 if ( gtk_check_version(2, 6, 0) != NULL
)
1945 return wxELLIPSIZE_NONE
;
1947 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1949 return wxELLIPSIZE_NONE
;
1951 GValue gvalue
= { 0, };
1952 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1953 g_object_get_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1955 mode
= static_cast<wxEllipsizeMode
>(g_value_get_enum( &gvalue
));
1956 g_value_unset( &gvalue
);
1960 return wxELLIPSIZE_NONE
;
1961 #endif // GTK 2.6/before
1965 wxDataViewRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
1967 wxVariant
value(str
);
1968 if (!Validate( value
))
1972 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
1974 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
1978 wxDataViewRenderer::GtkOnCellChanged(const wxVariant
& value
,
1979 const wxDataViewItem
& item
,
1982 wxDataViewModel
*model
= GetOwner()->GetOwner()->GetModel();
1983 model
->ChangeValue( value
, item
, col
);
1986 // ---------------------------------------------------------
1987 // wxDataViewTextRenderer
1988 // ---------------------------------------------------------
1993 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*WXUNUSED(renderer
),
1994 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1996 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) user_data
;
1998 cell
->GtkOnTextEdited(arg1
, wxGTK_CONV_BACK_FONT(
1999 arg2
, cell
->GetOwner()->GetOwner()->GetFont()));
2007 // helper function used by wxDataViewTextRenderer and
2008 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
2009 // given text renderer and returns true if anything was done
2010 bool GtkApplyAttr(GtkCellRendererText
*renderer
, const wxDataViewItemAttr
& attr
)
2012 bool usingDefaultAttrs
= true;
2013 if (attr
.HasColour())
2015 const GdkColor
* const gcol
= attr
.GetColour().GetColor();
2017 GValue gvalue
= { 0, };
2018 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2019 g_value_set_boxed( &gvalue
, gcol
);
2020 g_object_set_property( G_OBJECT(renderer
), "foreground_gdk", &gvalue
);
2021 g_value_unset( &gvalue
);
2023 usingDefaultAttrs
= false;
2027 GValue gvalue
= { 0, };
2028 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2029 g_value_set_boolean( &gvalue
, FALSE
);
2030 g_object_set_property( G_OBJECT(renderer
), "foreground-set", &gvalue
);
2031 g_value_unset( &gvalue
);
2034 if (attr
.GetItalic())
2036 GValue gvalue
= { 0, };
2037 g_value_init( &gvalue
, PANGO_TYPE_STYLE
);
2038 g_value_set_enum( &gvalue
, PANGO_STYLE_ITALIC
);
2039 g_object_set_property( G_OBJECT(renderer
), "style", &gvalue
);
2040 g_value_unset( &gvalue
);
2042 usingDefaultAttrs
= false;
2046 GValue gvalue
= { 0, };
2047 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2048 g_value_set_boolean( &gvalue
, FALSE
);
2049 g_object_set_property( G_OBJECT(renderer
), "style-set", &gvalue
);
2050 g_value_unset( &gvalue
);
2056 GValue gvalue
= { 0, };
2057 g_value_init( &gvalue
, PANGO_TYPE_WEIGHT
);
2058 g_value_set_enum( &gvalue
, PANGO_WEIGHT_BOLD
);
2059 g_object_set_property( G_OBJECT(renderer
), "weight", &gvalue
);
2060 g_value_unset( &gvalue
);
2062 usingDefaultAttrs
= false;
2066 GValue gvalue
= { 0, };
2067 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2068 g_value_set_boolean( &gvalue
, FALSE
);
2069 g_object_set_property( G_OBJECT(renderer
), "weight-set", &gvalue
);
2070 g_value_unset( &gvalue
);
2074 if (attr
.HasBackgroundColour())
2076 wxColour colour
= attr
.GetBackgroundColour();
2077 const GdkColor
* const gcol
= colour
.GetColor();
2079 GValue gvalue
= { 0, };
2080 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2081 g_value_set_boxed( &gvalue
, gcol
);
2082 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2083 g_value_unset( &gvalue
);
2087 GValue gvalue
= { 0, };
2088 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2089 g_value_set_boolean( &gvalue
, FALSE
);
2090 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2091 g_value_unset( &gvalue
);
2095 return !usingDefaultAttrs
;
2098 } // anonymous namespace
2100 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
2102 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2104 wxDataViewRenderer( varianttype
, mode
, align
)
2106 GtkWxCellRendererText
*text_renderer
= gtk_wx_cell_renderer_text_new();
2107 text_renderer
->wx_renderer
= this;
2108 m_renderer
= (GtkCellRenderer
*) text_renderer
;
2110 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
2112 GValue gvalue
= { 0, };
2113 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2114 g_value_set_boolean( &gvalue
, true );
2115 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
2116 g_value_unset( &gvalue
);
2118 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2124 SetAlignment(align
);
2127 bool wxDataViewTextRenderer::SetTextValue(const wxString
& str
)
2129 GValue gvalue
= { 0, };
2130 g_value_init( &gvalue
, G_TYPE_STRING
);
2131 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( str
, GetOwner()->GetOwner()->GetFont() ) );
2132 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2133 g_value_unset( &gvalue
);
2138 bool wxDataViewTextRenderer::GetTextValue(wxString
& str
) const
2140 GValue gvalue
= { 0, };
2141 g_value_init( &gvalue
, G_TYPE_STRING
);
2142 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2143 str
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
), const_cast<wxDataViewTextRenderer
*>(this)->GetOwner()->GetOwner()->GetFont() );
2144 g_value_unset( &gvalue
);
2149 void wxDataViewTextRenderer::SetAlignment( int align
)
2151 wxDataViewRenderer::SetAlignment(align
);
2153 if (gtk_check_version(2,10,0))
2156 // horizontal alignment:
2157 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2158 if (align
& wxALIGN_RIGHT
)
2159 pangoAlign
= PANGO_ALIGN_RIGHT
;
2160 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2161 pangoAlign
= PANGO_ALIGN_CENTER
;
2163 GValue gvalue
= { 0, };
2164 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2165 g_value_set_enum( &gvalue
, pangoAlign
);
2166 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2167 g_value_unset( &gvalue
);
2170 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr
& attr
)
2172 return GtkApplyAttr(GtkGetTextRenderer(), attr
);
2175 GtkCellRendererText
*wxDataViewTextRenderer::GtkGetTextRenderer() const
2177 return GTK_CELL_RENDERER_TEXT(m_renderer
);
2180 // ---------------------------------------------------------
2181 // wxDataViewBitmapRenderer
2182 // ---------------------------------------------------------
2187 // set "pixbuf" property on the given renderer
2188 void SetPixbufProp(GtkCellRenderer
*renderer
, GdkPixbuf
*pixbuf
)
2190 GValue gvalue
= { 0, };
2191 g_value_init( &gvalue
, G_TYPE_OBJECT
);
2192 g_value_set_object( &gvalue
, pixbuf
);
2193 g_object_set_property( G_OBJECT(renderer
), "pixbuf", &gvalue
);
2194 g_value_unset( &gvalue
);
2197 } // anonymous namespace
2199 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
2201 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2203 wxDataViewRenderer( varianttype
, mode
, align
)
2205 m_renderer
= gtk_cell_renderer_pixbuf_new();
2208 SetAlignment(align
);
2211 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
2213 if (value
.GetType() == wxT("wxBitmap"))
2218 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2219 // object (and it will stay there and remain owned by wxBitmap)
2220 SetPixbufProp(m_renderer
, bitmap
.GetPixbuf());
2222 else if (value
.GetType() == wxT("wxIcon"))
2227 SetPixbufProp(m_renderer
, icon
.GetPixbuf());
2237 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2242 // ---------------------------------------------------------
2243 // wxDataViewToggleRenderer
2244 // ---------------------------------------------------------
2247 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2248 gchar
*path
, gpointer user_data
);
2251 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2252 gchar
*path
, gpointer user_data
)
2254 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
2257 GValue gvalue
= { 0, };
2258 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2259 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
2261 wxVariant value
= !g_value_get_boolean( &gvalue
);
2262 g_value_unset( &gvalue
);
2264 if (!cell
->Validate( value
))
2267 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
2268 wxDataViewModel
*model
= ctrl
->GetModel();
2270 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
2272 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
2274 model
->ChangeValue( value
, item
, model_col
);
2277 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
2279 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
2280 wxDataViewCellMode mode
, int align
) :
2281 wxDataViewRenderer( varianttype
, mode
, align
)
2283 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
2285 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
2287 g_signal_connect_after( m_renderer
, "toggled",
2288 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
2292 GValue gvalue
= { 0, };
2293 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2294 g_value_set_boolean( &gvalue
, false );
2295 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
2296 g_value_unset( &gvalue
);
2300 SetAlignment(align
);
2303 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
2307 GValue gvalue
= { 0, };
2308 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2309 g_value_set_boolean( &gvalue
, tmp
);
2310 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2311 g_value_unset( &gvalue
);
2316 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
2318 GValue gvalue
= { 0, };
2319 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2320 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2321 value
= g_value_get_boolean( &gvalue
) != 0;
2322 g_value_unset( &gvalue
);
2327 // ---------------------------------------------------------
2328 // wxDataViewCustomRenderer
2329 // ---------------------------------------------------------
2331 class wxDataViewCtrlDCImpl
: public wxWindowDCImpl
2334 wxDataViewCtrlDCImpl( wxDC
*owner
, wxDataViewCtrl
*window
) :
2335 wxWindowDCImpl( owner
)
2337 GtkWidget
*widget
= window
->m_treeview
;
2343 m_context
= window
->GTKGetPangoDefaultContext();
2344 m_layout
= pango_layout_new( m_context
);
2345 m_fontdesc
= pango_font_description_copy(gtk_widget_get_style(widget
)->font_desc
);
2347 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
2349 // Set m_gdkwindow later
2354 class wxDataViewCtrlDC
: public wxWindowDC
2357 wxDataViewCtrlDC( wxDataViewCtrl
*window
) :
2358 wxWindowDC( new wxDataViewCtrlDCImpl( this, window
) )
2363 // ---------------------------------------------------------
2364 // wxDataViewCustomRenderer
2365 // ---------------------------------------------------------
2367 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
2369 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
2370 wxDataViewCellMode mode
,
2373 : wxDataViewCustomRendererBase( varianttype
, mode
, align
)
2376 m_text_renderer
= NULL
;
2377 m_renderParams
= NULL
;
2385 void wxDataViewCustomRenderer::GtkInitTextRenderer()
2387 m_text_renderer
= GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2388 g_object_ref_sink(m_text_renderer
);
2390 GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer
));
2393 GtkCellRendererText
*wxDataViewCustomRenderer::GtkGetTextRenderer() const
2395 if ( !m_text_renderer
)
2397 // we create it on demand so need to do it even from a const function
2398 const_cast<wxDataViewCustomRenderer
*>(this)->GtkInitTextRenderer();
2401 return m_text_renderer
;
2404 void wxDataViewCustomRenderer::RenderText( const wxString
&text
,
2408 int WXUNUSED(state
) )
2411 GtkCellRendererText
* const textRenderer
= GtkGetTextRenderer();
2413 GValue gvalue
= { 0, };
2414 g_value_init( &gvalue
, G_TYPE_STRING
);
2415 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( text
, GetOwner()->GetOwner()->GetFont() ) );
2416 g_object_set_property( G_OBJECT(textRenderer
), "text", &gvalue
);
2417 g_value_unset( &gvalue
);
2419 GtkApplyAttr(textRenderer
, GetAttr());
2421 GdkRectangle cell_area
;
2422 wxRectToGDKRect(cell
, cell_area
);
2423 cell_area
.x
+= xoffset
;
2424 cell_area
.width
-= xoffset
;
2426 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer
),
2427 m_renderParams
->window
,
2428 m_renderParams
->widget
,
2429 m_renderParams
->background_area
,
2431 m_renderParams
->expose_area
,
2432 GtkCellRendererState(m_renderParams
->flags
));
2435 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
2437 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
2438 renderer
->cell
= this;
2440 m_renderer
= (GtkCellRenderer
*) renderer
;
2443 SetAlignment(align
);
2450 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2455 if (m_text_renderer
)
2456 g_object_unref(m_text_renderer
);
2459 wxDC
*wxDataViewCustomRenderer::GetDC()
2463 if (GetOwner() == NULL
)
2465 if (GetOwner()->GetOwner() == NULL
)
2467 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2473 // ---------------------------------------------------------
2474 // wxDataViewProgressRenderer
2475 // ---------------------------------------------------------
2477 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
2479 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
2480 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
2481 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
2487 if (!gtk_check_version(2,6,0))
2489 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
2492 SetAlignment(align
);
2495 // We can't initialize the renderer just yet because we don't have the
2496 // pointer to the column that uses this renderer yet and so attempt to
2497 // dereference GetOwner() to get the font that is used as a source of
2498 // encoding in multibyte-to-Unicode conversion in GTKSetLabel() in
2499 // non-Unicode builds would crash. So simply remember to do it later.
2500 if ( !m_label
.empty() )
2501 m_needsToSetLabel
= true;
2503 #endif // !wxUSE_UNICODE
2509 // Use custom cell code
2510 wxDataViewCustomRenderer::Init(mode
, align
);
2514 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2518 void wxDataViewProgressRenderer::GTKSetLabel()
2520 GValue gvalue
= { 0, };
2521 g_value_init( &gvalue
, G_TYPE_STRING
);
2523 // Take care to not use GetOwner() here if the label is empty, we can be
2524 // called from ctor when GetOwner() is still NULL in this case.
2525 wxScopedCharBuffer buf
;
2526 if ( m_label
.empty() )
2527 buf
= wxScopedCharBuffer::CreateNonOwned("");
2529 buf
= wxGTK_CONV_FONT(m_label
, GetOwner()->GetOwner()->GetFont());
2531 g_value_set_string( &gvalue
, buf
);
2532 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2533 g_value_unset( &gvalue
);
2536 m_needsToSetLabel
= false;
2537 #endif // !wxUSE_UNICODE
2540 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
2543 if (!gtk_check_version(2,6,0))
2546 if ( m_needsToSetLabel
)
2548 #endif // !wxUSE_UNICODE
2550 gint tmp
= (long) value
;
2551 GValue gvalue
= { 0, };
2552 g_value_init( &gvalue
, G_TYPE_INT
);
2553 g_value_set_int( &gvalue
, tmp
);
2554 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
2555 g_value_unset( &gvalue
);
2560 m_value
= (long) value
;
2562 if (m_value
< 0) m_value
= 0;
2563 if (m_value
> 100) m_value
= 100;
2569 bool wxDataViewProgressRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2574 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
2576 double pct
= (double)m_value
/ 100.0;
2578 bar
.width
= (int)(cell
.width
* pct
);
2579 dc
->SetPen( *wxTRANSPARENT_PEN
);
2580 dc
->SetBrush( *wxBLUE_BRUSH
);
2581 dc
->DrawRectangle( bar
);
2583 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
2584 dc
->SetPen( *wxBLACK_PEN
);
2585 dc
->DrawRectangle( cell
);
2590 wxSize
wxDataViewProgressRenderer::GetSize() const
2592 return wxSize(40,12);
2595 // -------------------------------------
2596 // wxDataViewChoiceRenderer
2597 // -------------------------------------
2599 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString
&choices
,
2600 wxDataViewCellMode mode
, int alignment
) :
2601 wxDataViewCustomRenderer( "string", mode
, alignment
, true )
2603 m_choices
= choices
;
2606 if (!gtk_check_version(2,6,0))
2608 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_combo_new();
2610 GtkListStore
*store
= gtk_list_store_new( 1, G_TYPE_STRING
);
2611 for (size_t n
= 0; n
< m_choices
.GetCount(); n
++)
2613 gtk_list_store_insert_with_values(
2615 static_cast<const char *>(m_choices
[n
].utf8_str()), -1 );
2618 g_object_set (m_renderer
,
2624 bool editable
= (mode
& wxDATAVIEW_CELL_EDITABLE
) != 0;
2625 g_object_set (m_renderer
, "editable", editable
, NULL
);
2627 SetAlignment(alignment
);
2629 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2636 // Use custom cell code
2637 wxDataViewCustomRenderer::Init(mode
, alignment
);
2641 bool wxDataViewChoiceRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
2643 RenderText( m_data
, 0, rect
, dc
, state
);
2647 wxSize
wxDataViewChoiceRenderer::GetSize() const
2649 return wxSize(70,20);
2652 bool wxDataViewChoiceRenderer::SetValue( const wxVariant
&value
)
2656 if (!gtk_check_version(2,6,0))
2658 GValue gvalue
= { 0, };
2659 g_value_init( &gvalue
, G_TYPE_STRING
);
2660 g_value_set_string(&gvalue
,
2661 wxGTK_CONV_FONT(value
.GetString(),
2662 GetOwner()->GetOwner()->GetFont()));
2663 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2664 g_value_unset( &gvalue
);
2668 m_data
= value
.GetString();
2673 bool wxDataViewChoiceRenderer::GetValue( wxVariant
&value
) const
2676 if (!gtk_check_version(2,6,0))
2678 GValue gvalue
= { 0, };
2679 g_value_init( &gvalue
, G_TYPE_STRING
);
2680 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2681 wxString temp
= wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue
),
2682 GetOwner()->GetOwner()->GetFont());
2683 g_value_unset( &gvalue
);
2686 //wxPrintf( "temp %s\n", temp );
2687 // TODO: remove this code
2696 void wxDataViewChoiceRenderer::SetAlignment( int align
)
2698 wxDataViewCustomRenderer::SetAlignment(align
);
2700 if (gtk_check_version(2,10,0))
2703 // horizontal alignment:
2704 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2705 if (align
& wxALIGN_RIGHT
)
2706 pangoAlign
= PANGO_ALIGN_RIGHT
;
2707 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2708 pangoAlign
= PANGO_ALIGN_CENTER
;
2710 GValue gvalue
= { 0, };
2711 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2712 g_value_set_enum( &gvalue
, pangoAlign
);
2713 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2714 g_value_unset( &gvalue
);
2717 // ----------------------------------------------------------------------------
2718 // wxDataViewChoiceByIndexRenderer
2719 // ----------------------------------------------------------------------------
2721 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
2722 wxDataViewCellMode mode
, int alignment
) :
2723 wxDataViewChoiceRenderer( choices
, mode
, alignment
)
2727 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
2729 wxVariant
value( (long) GetChoices().Index( str
) );
2731 if (!Validate( value
))
2735 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2737 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2740 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant
&value
)
2742 wxVariant string_value
= GetChoice( value
.GetLong() );
2743 return wxDataViewChoiceRenderer::SetValue( string_value
);
2746 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant
&value
) const
2748 wxVariant string_value
;
2749 if (!wxDataViewChoiceRenderer::GetValue( string_value
))
2752 value
= (long) GetChoices().Index( string_value
.GetString() );
2756 // ---------------------------------------------------------
2757 // wxDataViewIconTextRenderer
2758 // ---------------------------------------------------------
2760 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
2762 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2764 const wxString
&varianttype
,
2765 wxDataViewCellMode mode
,
2768 : wxDataViewTextRenderer(varianttype
, mode
, align
)
2770 m_rendererIcon
= gtk_cell_renderer_pixbuf_new();
2773 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2777 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
2779 // add the icon renderer first
2780 gtk_tree_view_column_pack_start(column
, m_rendererIcon
, FALSE
/* !expand */);
2782 // add the text renderer too
2783 wxDataViewRenderer::GtkPackIntoColumn(column
);
2786 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
2790 SetTextValue(m_value
.GetText());
2792 const wxIcon
& icon
= m_value
.GetIcon();
2793 SetPixbufProp(m_rendererIcon
, icon
.IsOk() ? icon
.GetPixbuf() : NULL
);
2798 bool wxDataViewIconTextRenderer::GetValue(wxVariant
& value
) const
2801 if ( !GetTextValue(str
) )
2804 // user doesn't have any way to edit the icon so leave it unchanged
2805 value
<< wxDataViewIconText(str
, m_value
.GetIcon());
2811 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant
& value
,
2812 const wxDataViewItem
& item
,
2815 // we receive just the text part of our value as it's the only one which
2816 // can be edited, but we need the full wxDataViewIconText value for the
2818 wxVariant valueIconText
;
2819 valueIconText
<< wxDataViewIconText(value
.GetString(), m_value
.GetIcon());
2820 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText
, item
, col
);
2823 // ---------------------------------------------------------
2825 // ---------------------------------------------------------
2829 gtk_dataview_header_button_press_callback( GtkWidget
*WXUNUSED(widget
),
2830 GdkEventButton
*gdk_event
,
2831 wxDataViewColumn
*column
)
2833 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
2836 if (gdk_event
->button
== 1)
2838 gs_lastLeftClickHeader
= column
;
2840 wxDataViewCtrl
*dv
= column
->GetOwner();
2841 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2842 event
.SetDataViewColumn( column
);
2843 event
.SetModel( dv
->GetModel() );
2844 if (dv
->HandleWindowEvent( event
))
2848 if (gdk_event
->button
== 3)
2850 wxDataViewCtrl
*dv
= column
->GetOwner();
2851 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2852 event
.SetDataViewColumn( column
);
2853 event
.SetModel( dv
->GetModel() );
2854 if (dv
->HandleWindowEvent( event
))
2864 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*WXUNUSED(column
),
2865 GtkCellRenderer
*renderer
,
2866 GtkTreeModel
*model
,
2870 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2871 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2873 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2875 wxDataViewItem
item( (void*) iter
->user_data
);
2877 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2879 if (!wx_model
->IsVirtualListModel())
2882 if (wx_model
->IsContainer( item
))
2884 visible
= wx_model
->HasContainerColumns( item
) ||
2885 (cell
->GetOwner()->GetModelColumn() == 0);
2892 GValue gvalue
= { 0, };
2893 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2894 g_value_set_boolean( &gvalue
, visible
);
2895 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2896 g_value_unset( &gvalue
);
2903 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2905 if (value
.GetType() != cell
->GetVariantType())
2907 wxLogError( wxT("Wrong type, required: %s but: %s"),
2908 value
.GetType().c_str(),
2909 cell
->GetVariantType().c_str() );
2912 cell
->SetValue( value
);
2914 // deal with disabled items
2915 bool enabled
= wx_model
->IsEnabled( item
, cell
->GetOwner()->GetModelColumn() );
2917 // a) this sets the appearance to disabled grey
2918 GValue gvalue
= { 0, };
2919 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2920 g_value_set_boolean( &gvalue
, enabled
);
2921 g_object_set_property( G_OBJECT(renderer
), "sensitive", &gvalue
);
2922 g_value_unset( &gvalue
);
2924 // b) this actually disables the control/renderer
2926 cell
->SetMode( cell
->GtkGetMode() );
2928 cell
->SetMode( wxDATAVIEW_CELL_INERT
);
2931 // deal with attributes: if the renderer doesn't support them at all, we
2932 // don't even need to query the model for them
2933 if ( !cell
->GtkSupportsAttrs() )
2936 // it can support attributes so check if this item has any
2937 wxDataViewItemAttr attr
;
2938 if ( wx_model
->GetAttr( item
, cell
->GetOwner()->GetModelColumn(), attr
)
2939 || !cell
->GtkIsUsingDefaultAttrs() )
2941 bool usingDefaultAttrs
= !cell
->GtkSetAttr(attr
);
2942 cell
->GtkSetUsingDefaultAttrs(usingDefaultAttrs
);
2944 // else: no custom attributes specified and we're already using the default
2945 // ones -- nothing to do
2951 #include <wx/listimpl.cpp>
2952 WX_DEFINE_LIST(wxDataViewColumnList
)
2954 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
2955 unsigned int model_column
, int width
,
2956 wxAlignment align
, int flags
)
2957 : wxDataViewColumnBase( cell
, model_column
)
2959 Init( align
, flags
, width
);
2964 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
2965 unsigned int model_column
, int width
,
2966 wxAlignment align
, int flags
)
2967 : wxDataViewColumnBase( bitmap
, cell
, model_column
)
2969 Init( align
, flags
, width
);
2971 SetBitmap( bitmap
);
2974 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
2976 m_isConnected
= false;
2978 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
2979 m_column
= (GtkWidget
*) column
;
2982 SetAlignment( align
);
2986 // Create container for icon and label
2987 GtkWidget
*box
= gtk_hbox_new( FALSE
, 1 );
2988 gtk_widget_show( box
);
2989 // gtk_container_set_border_width((GtkContainer*)box, 2);
2990 m_image
= gtk_image_new();
2991 gtk_box_pack_start(GTK_BOX(box
), m_image
, FALSE
, FALSE
, 1);
2992 m_label
= gtk_label_new("");
2993 gtk_box_pack_end( GTK_BOX(box
), GTK_WIDGET(m_label
), FALSE
, FALSE
, 1 );
2994 gtk_tree_view_column_set_widget( column
, box
);
2996 wxDataViewRenderer
* const colRenderer
= GetRenderer();
2997 GtkCellRenderer
* const cellRenderer
= colRenderer
->GetGtkHandle();
2999 colRenderer
->GtkPackIntoColumn(column
);
3001 gtk_tree_view_column_set_cell_data_func( column
, cellRenderer
,
3002 wxGtkTreeCellDataFunc
, (gpointer
) colRenderer
, NULL
);
3005 void wxDataViewColumn::OnInternalIdle()
3010 if (gtk_widget_get_realized(GetOwner()->m_treeview
))
3012 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3015 g_signal_connect(column
->button
, "button_press_event",
3016 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
3018 // otherwise the event will be blocked by GTK+
3019 gtk_tree_view_column_set_clickable( column
, TRUE
);
3021 m_isConnected
= true;
3026 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
3028 wxDataViewColumnBase::SetOwner( owner
);
3030 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3032 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3035 void wxDataViewColumn::SetTitle( const wxString
&title
)
3037 wxDataViewCtrl
*ctrl
= GetOwner();
3038 gtk_label_set_text( GTK_LABEL(m_label
), ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
3039 : wxGTK_CONV_SYS(title
) );
3041 gtk_widget_hide( m_label
);
3043 gtk_widget_show( m_label
);
3046 wxString
wxDataViewColumn::GetTitle() const
3048 return wxGTK_CONV_BACK_FONT(
3049 gtk_label_get_text( GTK_LABEL(m_label
) ),
3050 GetOwner()->GetFont()
3054 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
3056 wxDataViewColumnBase::SetBitmap( bitmap
);
3060 GtkImage
*gtk_image
= GTK_IMAGE(m_image
);
3062 GdkBitmap
*mask
= NULL
;
3063 if (bitmap
.GetMask())
3064 mask
= bitmap
.GetMask()->GetBitmap();
3066 if (bitmap
.HasPixbuf())
3068 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
3069 bitmap
.GetPixbuf());
3073 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
3074 bitmap
.GetPixmap(), mask
);
3076 gtk_widget_show( m_image
);
3080 gtk_widget_hide( m_image
);
3084 void wxDataViewColumn::SetHidden( bool hidden
)
3086 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
3089 void wxDataViewColumn::SetResizeable( bool resizable
)
3091 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizable
);
3094 void wxDataViewColumn::SetAlignment( wxAlignment align
)
3096 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3098 gfloat xalign
= 0.0;
3099 if (align
== wxALIGN_RIGHT
)
3101 if (align
== wxALIGN_CENTER_HORIZONTAL
||
3102 align
== wxALIGN_CENTER
)
3105 gtk_tree_view_column_set_alignment( column
, xalign
);
3107 if (m_renderer
&& m_renderer
->GetAlignment() == -1)
3108 m_renderer
->GtkUpdateAlignment();
3111 wxAlignment
wxDataViewColumn::GetAlignment() const
3113 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
3116 return wxALIGN_RIGHT
;
3118 return wxALIGN_CENTER_HORIZONTAL
;
3120 return wxALIGN_LEFT
;
3123 void wxDataViewColumn::SetSortable( bool sortable
)
3125 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3129 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
3133 gtk_tree_view_column_set_sort_column_id( column
, -1 );
3134 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
3135 gtk_tree_view_column_set_clickable( column
, FALSE
);
3139 bool wxDataViewColumn::IsSortable() const
3141 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3142 return gtk_tree_view_column_get_clickable( column
) != 0;
3145 bool wxDataViewColumn::IsSortKey() const
3147 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3148 return gtk_tree_view_column_get_sort_indicator( column
) != 0;
3151 bool wxDataViewColumn::IsResizeable() const
3153 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3154 return gtk_tree_view_column_get_resizable( column
) != 0;
3157 bool wxDataViewColumn::IsHidden() const
3159 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3160 return !gtk_tree_view_column_get_visible( column
);
3163 void wxDataViewColumn::SetSortOrder( bool ascending
)
3165 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3168 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
3170 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
3172 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
3175 bool wxDataViewColumn::IsSortOrderAscending() const
3177 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3179 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
3182 void wxDataViewColumn::SetMinWidth( int width
)
3184 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3187 int wxDataViewColumn::GetMinWidth() const
3189 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3192 int wxDataViewColumn::GetWidth() const
3194 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3197 void wxDataViewColumn::SetWidth( int width
)
3199 if ( width
== wxCOL_WIDTH_AUTOSIZE
)
3201 // NB: this disables user resizing
3202 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
3206 if ( width
== wxCOL_WIDTH_DEFAULT
)
3208 // TODO find a better calculation
3209 width
= wxDVC_DEFAULT_WIDTH
;
3212 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_FIXED
);
3213 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3217 void wxDataViewColumn::SetReorderable( bool reorderable
)
3219 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column
), reorderable
);
3222 bool wxDataViewColumn::IsReorderable() const
3224 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column
) ) != 0;
3227 //-----------------------------------------------------------------------------
3228 // wxGtkTreeModelNode
3229 //-----------------------------------------------------------------------------
3232 class wxGtkTreeModelChildWithPos
3240 int wxGtkTreeModelChildWithPosCmp( const void* data1
, const void* data2
, const void* user_data
)
3242 const wxGtkTreeModelChildWithPos
* child1
= (const wxGtkTreeModelChildWithPos
*) data1
;
3243 const wxGtkTreeModelChildWithPos
* child2
= (const wxGtkTreeModelChildWithPos
*) data2
;
3244 const wxDataViewCtrlInternal
*internal
= (const wxDataViewCtrlInternal
*) user_data
;
3245 int ret
= internal
->GetDataViewModel()->Compare( child1
->id
, child2
->id
,
3246 internal
->GetSortColumn(), (internal
->GetSortOrder() == GTK_SORT_DESCENDING
) );
3252 int LINKAGEMODE
wxGtkTreeModelChildPtrCmp( void*** data1
, void*** data2
)
3254 return gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(**data1
), wxDataViewItem(**data2
),
3255 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
3258 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr
);
3261 void wxGtkTreeModelNode::Resort()
3263 size_t child_count
= GetChildCount();
3264 if (child_count
== 0)
3267 size_t node_count
= GetNodesCount();
3269 if (child_count
== 1)
3271 if (node_count
== 1)
3273 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
3279 gint
*new_order
= new gint
[child_count
];
3282 // m_children has the original *void
3283 // ptrs points to these
3284 wxGtkTreeModelChildrenPtr ptrs
;
3286 for (i
= 0; i
< child_count
; i
++)
3287 ptrs
.Add( &(m_children
[i
]) );
3289 gs_internal
= m_internal
;
3290 ptrs
.Sort( &wxGtkTreeModelChildPtrCmp
);
3292 wxGtkTreeModelChildren temp
;
3293 void** base_ptr
= &(m_children
[0]);
3294 // Transfer positions to new_order array and
3296 for (i
= 0; i
< child_count
; i
++)
3298 new_order
[i
] = ptrs
[i
] - base_ptr
;
3299 temp
.Add( *ptrs
[i
] );
3302 // Transfer IDs back to m_children
3304 WX_APPEND_ARRAY( temp
, m_children
);
3309 // Build up array with IDs and original positions
3310 wxGtkTreeModelChildWithPos
* temp
= new wxGtkTreeModelChildWithPos
[child_count
];
3312 for (i
= 0; i
< child_count
; i
++)
3315 temp
[i
].id
= m_children
[i
];
3317 // Sort array keeping original positions
3318 wxQsort( temp
, child_count
, sizeof(wxGtkTreeModelChildWithPos
),
3319 &wxGtkTreeModelChildWithPosCmp
, m_internal
);
3320 // Transfer positions to new_order array and
3321 // IDs to m_children
3323 for (i
= 0; i
< child_count
; i
++)
3325 new_order
[i
] = temp
[i
].pos
;
3326 m_children
.Add( temp
[i
].id
);
3335 wxGtkTreeModelChildren temp
;
3336 WX_APPEND_ARRAY( temp
, m_children
);
3338 gs_internal
= m_internal
;
3339 m_children
.Sort( &wxGtkTreeModelChildCmp
);
3342 for (pos
= 0; pos
< child_count
; pos
++)
3344 void *id
= m_children
.Item( pos
);
3345 int old_pos
= temp
.Index( id
);
3346 new_order
[pos
] = old_pos
;
3350 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
3353 iter
.user_data
= GetItem().GetID();
3354 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
3356 gtk_tree_model_rows_reordered( gtk_tree_model
,
3357 wxGtkTreePath(m_internal
->get_path(&iter
)), &iter
, new_order
);
3359 delete [] new_order
;
3362 for (pos
= 0; pos
< node_count
; pos
++)
3364 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
3369 //-----------------------------------------------------------------------------
3370 // wxDataViewCtrlInternal
3371 //-----------------------------------------------------------------------------
3373 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
)
3376 m_wx_model
= wx_model
;
3380 m_sort_order
= GTK_SORT_ASCENDING
;
3382 m_dataview_sort_column
= NULL
;
3384 m_dragDataObject
= NULL
;
3385 m_dropDataObject
= NULL
;
3389 m_gtk_model
= wxgtk_tree_model_new();
3390 m_gtk_model
->internal
= this;
3392 m_notifier
= new wxGtkDataViewModelNotifier( wx_model
, this );
3394 wx_model
->AddNotifier( m_notifier
);
3396 // g_object_unref( gtk_model ); ???
3398 if (!m_wx_model
->IsVirtualListModel())
3401 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner
->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model
) );
3404 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3406 m_wx_model
->RemoveNotifier( m_notifier
);
3408 // remove the model from the GtkTreeView before it gets destroyed
3409 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner
->GtkGetTreeView() ), NULL
);
3411 g_object_unref( m_gtk_model
);
3413 delete m_dragDataObject
;
3414 delete m_dropDataObject
;
3417 void wxDataViewCtrlInternal::ScheduleRefresh()
3422 void wxDataViewCtrlInternal::OnInternalIdle()
3426 GtkWidget
*widget
= m_owner
->GtkGetTreeView();
3427 gtk_widget_queue_draw( widget
);
3432 void wxDataViewCtrlInternal::InitTree()
3434 wxDataViewItem item
;
3435 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
3437 BuildBranch( m_root
);
3440 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
3442 if (node
->GetChildCount() == 0)
3444 wxDataViewItemArray children
;
3445 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
3448 for (pos
= 0; pos
< count
; pos
++)
3450 wxDataViewItem child
= children
[pos
];
3452 if (m_wx_model
->IsContainer( child
))
3453 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
3455 node
->AddLeaf( child
.GetID() );
3457 // Don't send any events here
3464 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat
&format
)
3466 wxGtkString
atom_str( gdk_atom_name( format
) );
3467 m_dragSourceTargetEntryTarget
= wxCharBuffer( atom_str
);
3469 m_dragSourceTargetEntry
.target
= m_dragSourceTargetEntryTarget
.data();
3470 m_dragSourceTargetEntry
.flags
= 0;
3471 m_dragSourceTargetEntry
.info
= static_cast<guint
>(-1);
3473 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3474 GDK_BUTTON1_MASK
, &m_dragSourceTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3479 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat
&format
)
3481 wxGtkString
atom_str( gdk_atom_name( format
) );
3482 m_dropTargetTargetEntryTarget
= wxCharBuffer( atom_str
);
3484 m_dropTargetTargetEntry
.target
= m_dropTargetTargetEntryTarget
.data();
3485 m_dropTargetTargetEntry
.flags
= 0;
3486 m_dropTargetTargetEntry
.info
= static_cast<guint
>(-1);
3488 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3489 &m_dropTargetTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3494 gboolean
wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource
*WXUNUSED(drag_source
),
3497 delete m_dragDataObject
;
3498 m_dragDataObject
= NULL
;
3500 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3504 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3505 event
.SetEventObject( m_owner
);
3506 event
.SetItem( item
);
3507 event
.SetModel( m_wx_model
);
3509 gtk_widget_get_pointer(m_owner
->GtkGetTreeView(), &x
, &y
);
3510 event
.SetPosition(x
, y
);
3511 if (!m_owner
->HandleWindowEvent( event
))
3514 if (!event
.IsAllowed())
3517 wxDataObject
*obj
= event
.GetDataObject();
3521 m_dragDataObject
= obj
;
3527 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource
*WXUNUSED(drag_source
),
3528 GtkTreePath
*WXUNUSED(path
))
3533 gboolean
wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource
*WXUNUSED(drag_source
),
3534 GtkTreePath
*path
, GtkSelectionData
*selection_data
)
3536 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3540 GdkAtom target
= gtk_selection_data_get_target(selection_data
);
3541 if (!m_dragDataObject
->IsSupported(target
))
3544 size_t size
= m_dragDataObject
->GetDataSize(target
);
3548 void *buf
= malloc( size
);
3550 gboolean res
= FALSE
;
3551 if (m_dragDataObject
->GetDataHere(target
, buf
))
3555 gtk_selection_data_set(selection_data
, target
,
3556 8, (const guchar
*) buf
, size
);
3565 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3567 GtkSelectionData
*selection_data
)
3569 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3573 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
3574 event
.SetEventObject( m_owner
);
3575 event
.SetItem( item
);
3576 event
.SetModel( m_wx_model
);
3577 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3578 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3579 event
.SetDataBuffer(const_cast<guchar
*>(gtk_selection_data_get_data(selection_data
)));
3580 if (!m_owner
->HandleWindowEvent( event
))
3583 if (!event
.IsAllowed())
3590 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3592 GtkSelectionData
*selection_data
)
3594 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3598 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
3599 event
.SetEventObject( m_owner
);
3600 event
.SetItem( item
);
3601 event
.SetModel( m_wx_model
);
3602 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3603 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3604 if (!m_owner
->HandleWindowEvent( event
))
3607 if (!event
.IsAllowed())
3613 // notifications from wxDataViewModel
3615 bool wxDataViewCtrlInternal::Cleared()
3630 void wxDataViewCtrlInternal::Resort()
3632 if (!m_wx_model
->IsVirtualListModel())
3638 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3640 if (!m_wx_model
->IsVirtualListModel())
3642 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3643 wxCHECK_MSG(parent_node
, false,
3644 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3646 wxDataViewItemArray modelSiblings
;
3647 m_wx_model
->GetChildren(parent
, modelSiblings
);
3648 const int modelSiblingsSize
= modelSiblings
.size();
3650 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
3651 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
3653 const wxGtkTreeModelChildren
& nodeSiblings
= parent_node
->GetChildren();
3654 const int nodeSiblingsSize
= nodeSiblings
.size();
3658 if ( posInModel
== modelSiblingsSize
- 1 )
3660 nodePos
= nodeSiblingsSize
;
3662 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
3664 // This is the simple case when our node tree already matches the
3665 // model and only this one item is missing.
3666 nodePos
= posInModel
;
3670 // It's possible that a larger discrepancy between the model and
3671 // our realization exists. This can happen e.g. when adding a bunch
3672 // of items to the model and then calling ItemsAdded() just once
3673 // afterwards. In this case, we must find the right position by
3674 // looking at sibling items.
3676 // append to the end if we won't find a better position:
3677 nodePos
= nodeSiblingsSize
;
3679 for ( int nextItemPos
= posInModel
+ 1;
3680 nextItemPos
< modelSiblingsSize
;
3683 int nextNodePos
= parent_node
->FindChildByItem(modelSiblings
[nextItemPos
]);
3684 if ( nextNodePos
!= wxNOT_FOUND
)
3686 nodePos
= nextNodePos
;
3692 if (m_wx_model
->IsContainer( item
))
3693 parent_node
->InsertNode( new wxGtkTreeModelNode( parent_node
, item
, this ), nodePos
);
3695 parent_node
->InsertLeaf( item
.GetID(), nodePos
);
3703 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3705 if (!m_wx_model
->IsVirtualListModel())
3707 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3708 wxASSERT_MSG(parent_node
,
3709 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3711 parent_node
->DeleteChild( item
.GetID() );
3719 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
3721 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3722 event
.SetEventObject( m_owner
);
3723 event
.SetModel( m_owner
->GetModel() );
3724 event
.SetItem( item
);
3725 m_owner
->HandleWindowEvent( event
);
3730 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int view_column
)
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
.SetColumn( view_column
);
3736 event
.SetDataViewColumn( GetOwner()->GetColumn(view_column
) );
3737 event
.SetItem( item
);
3738 m_owner
->HandleWindowEvent( event
);
3745 GtkTreeModelFlags
wxDataViewCtrlInternal::get_flags()
3749 if ( m_wx_model
->IsListModel() )
3750 flags
|= GTK_TREE_MODEL_LIST_ONLY
;
3752 if ( !m_wx_model
->IsVirtualListModel() )
3753 flags
|= GTK_TREE_MODEL_ITERS_PERSIST
;
3755 return GtkTreeModelFlags(flags
);
3758 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
3761 if (m_wx_model
->IsVirtualListModel())
3763 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3765 unsigned int i
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
3767 if (i
>= wx_model
->GetCount())
3770 iter
->stamp
= m_gtk_model
->stamp
;
3771 // user_data is just the index +1
3772 iter
->user_data
= wxUIntToPtr(i
+1);
3778 int depth
= gtk_tree_path_get_depth( path
);
3780 wxGtkTreeModelNode
*node
= m_root
;
3783 for (i
= 0; i
< depth
; i
++)
3785 BuildBranch( node
);
3787 gint pos
= gtk_tree_path_get_indices (path
)[i
];
3788 if (pos
< 0) return FALSE
;
3789 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
3791 void* id
= node
->GetChildren().Item( (size_t) pos
);
3795 iter
->stamp
= m_gtk_model
->stamp
;
3796 iter
->user_data
= id
;
3800 size_t count
= node
->GetNodes().GetCount();
3802 for (pos2
= 0; pos2
< count
; pos2
++)
3804 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
3805 if (child_node
->GetItem().GetID() == id
)
3817 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
3819 // When this is called from ItemDeleted(), the item is already
3820 // deleted in the model.
3822 GtkTreePath
*retval
= gtk_tree_path_new ();
3824 if (m_wx_model
->IsVirtualListModel())
3826 // iter is root, add nothing
3827 if (!iter
->user_data
)
3830 // user_data is just the index +1
3831 int i
= ( (wxUIntPtr
) iter
->user_data
) -1;
3832 gtk_tree_path_append_index (retval
, i
);
3836 void *id
= iter
->user_data
;
3838 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
3841 int pos
= node
->GetChildren().Index( id
);
3843 gtk_tree_path_prepend_index( retval
, pos
);
3845 id
= node
->GetItem().GetID();
3846 node
= node
->GetParent();
3853 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
3855 if (m_wx_model
->IsVirtualListModel())
3857 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3859 // user_data is just the index +1
3860 int n
= ( (wxUIntPtr
) iter
->user_data
) -1;
3864 iter
->user_data
= NULL
;
3868 if (n
>= (int) wx_model
->GetCount()-1)
3870 iter
->user_data
= NULL
;
3874 // user_data is just the index +1 (+2 because we need the next)
3875 iter
->user_data
= wxUIntToPtr(n
+2);
3879 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
3880 if( parent
== NULL
)
3882 iter
->user_data
= NULL
;
3886 int pos
= parent
->GetChildren().Index( iter
->user_data
);
3888 if (pos
== (int) parent
->GetChildCount()-1)
3890 iter
->user_data
= NULL
;
3894 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
3900 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
3902 if (m_wx_model
->IsVirtualListModel())
3904 // this is a list, nodes have no children
3908 iter
->stamp
= m_gtk_model
->stamp
;
3909 iter
->user_data
= (gpointer
) 1;
3917 if (m_root
->GetChildCount() == 0) return FALSE
;
3918 iter
->stamp
= m_gtk_model
->stamp
;
3919 iter
->user_data
= (gpointer
) m_root
->GetChildren().Item( 0 );
3924 wxDataViewItem item
;
3926 item
= wxDataViewItem( (void*) parent
->user_data
);
3928 if (!m_wx_model
->IsContainer( item
))
3931 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3932 wxASSERT_MSG(parent_node
,
3933 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3935 BuildBranch( parent_node
);
3937 if (parent_node
->GetChildCount() == 0)
3940 iter
->stamp
= m_gtk_model
->stamp
;
3941 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
3947 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
3949 if (m_wx_model
->IsVirtualListModel())
3951 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3954 return (wx_model
->GetCount() > 0);
3956 // this is a list, nodes have no children
3962 return (m_root
->GetChildCount() > 0);
3964 wxDataViewItem
item( (void*) iter
->user_data
);
3966 bool is_container
= m_wx_model
->IsContainer( item
);
3971 wxGtkTreeModelNode
*node
= FindNode( iter
);
3973 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3975 BuildBranch( node
);
3977 return (node
->GetChildCount() > 0);
3981 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
3983 if (m_wx_model
->IsVirtualListModel())
3985 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3988 return (gint
) wx_model
->GetCount();
3995 return m_root
->GetChildCount();
3997 wxDataViewItem
item( (void*) iter
->user_data
);
3999 if (!m_wx_model
->IsContainer( item
))
4002 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
4003 wxASSERT_MSG(parent_node
,
4004 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4006 BuildBranch( parent_node
);
4008 return parent_node
->GetChildCount();
4012 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
4014 if (m_wx_model
->IsVirtualListModel())
4016 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
4024 if (n
>= (gint
) wx_model
->GetCount())
4027 iter
->stamp
= m_gtk_model
->stamp
;
4028 // user_data is just the index +1
4029 iter
->user_data
= wxUIntToPtr(n
+1);
4036 if (parent
) id
= (void*) parent
->user_data
;
4037 wxDataViewItem
item( id
);
4039 if (!m_wx_model
->IsContainer( item
))
4042 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4043 wxASSERT_MSG(parent_node
,
4044 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4046 BuildBranch( parent_node
);
4048 iter
->stamp
= m_gtk_model
->stamp
;
4049 iter
->user_data
= parent_node
->GetChildren().Item( n
);
4055 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
4057 if (m_wx_model
->IsVirtualListModel())
4063 wxGtkTreeModelNode
*node
= FindParentNode( child
);
4067 iter
->stamp
= m_gtk_model
->stamp
;
4068 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
4074 // item can be deleted already in the model
4075 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
4077 if (m_wx_model
->IsVirtualListModel())
4079 return wxPtrToUInt(item
.GetID()) - 1;
4083 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4084 wxGtkTreeModelChildren
&children
= parent_node
->GetChildren();
4086 for (j
= 0; j
< children
.GetCount(); j
++)
4088 if (children
[j
] == item
.GetID())
4096 static wxGtkTreeModelNode
*
4097 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4103 list
.DeleteContents( true );
4104 wxDataViewItem
it( item
);
4108 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4109 list
.Insert( pItem
);
4110 it
= model
->GetParent( it
);
4113 wxGtkTreeModelNode
* node
= treeNode
;
4114 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4116 if( node
&& node
->GetNodes().GetCount() != 0 )
4118 int len
= node
->GetNodes().GetCount();
4119 wxGtkTreeModelNodes
&nodes
= node
->GetNodes();
4121 for( ; j
< len
; j
++)
4123 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4142 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
4147 wxDataViewItem
item( (void*) iter
->user_data
);
4151 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4156 wxLogDebug( "Not found %p", iter->user_data );
4160 // TODO: remove this code
4166 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
4171 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4176 wxLogDebug( "Not found %p", item.GetID() );
4180 // TODO: remove this code
4186 static wxGtkTreeModelNode
*
4187 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4193 list
.DeleteContents( true );
4197 wxDataViewItem
it( model
->GetParent( item
) );
4200 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4201 list
.Insert( pItem
);
4202 it
= model
->GetParent( it
);
4205 wxGtkTreeModelNode
* node
= treeNode
;
4206 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4208 if( node
&& node
->GetNodes().GetCount() != 0 )
4210 int len
= node
->GetNodes().GetCount();
4211 wxGtkTreeModelNodes nodes
= node
->GetNodes();
4213 for( ; j
< len
; j
++)
4215 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4230 //Examine whether the node is item's parent node
4231 int len
= node
->GetChildCount();
4232 for( int i
= 0; i
< len
; i
++ )
4234 if( node
->GetChildren().Item( i
) == item
.GetID() )
4240 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
4245 wxDataViewItem
item( (void*) iter
->user_data
);
4249 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4252 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
4257 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4260 //-----------------------------------------------------------------------------
4261 // wxDataViewCtrl signal callbacks
4262 //-----------------------------------------------------------------------------
4265 wxdataview_selection_changed_callback( GtkTreeSelection
* WXUNUSED(selection
), wxDataViewCtrl
*dv
)
4267 if (!gtk_widget_get_realized(dv
->m_widget
))
4270 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, dv
->GetId() );
4271 event
.SetItem( dv
->GetSelection() );
4272 event
.SetModel( dv
->GetModel() );
4273 dv
->HandleWindowEvent( event
);
4277 wxdataview_row_activated_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreePath
*path
,
4278 GtkTreeViewColumn
*WXUNUSED(column
), wxDataViewCtrl
*dv
)
4280 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
4282 wxDataViewItem
item(dv
->GTKPathToItem(path
));
4283 event
.SetItem( item
);
4284 event
.SetModel( dv
->GetModel() );
4285 dv
->HandleWindowEvent( event
);
4289 wxdataview_test_expand_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4290 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4292 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, dv
->GetId() );
4294 wxDataViewItem
item( (void*) iter
->user_data
);;
4295 event
.SetItem( item
);
4296 event
.SetModel( dv
->GetModel() );
4297 dv
->HandleWindowEvent( event
);
4299 return !event
.IsAllowed();
4303 wxdataview_row_expanded_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4304 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4306 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
, dv
->GetId() );
4308 wxDataViewItem
item( (void*) iter
->user_data
);;
4309 event
.SetItem( item
);
4310 event
.SetModel( dv
->GetModel() );
4311 dv
->HandleWindowEvent( event
);
4315 wxdataview_test_collapse_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4316 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4318 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
, dv
->GetId() );
4320 wxDataViewItem
item( (void*) iter
->user_data
);;
4321 event
.SetItem( item
);
4322 event
.SetModel( dv
->GetModel() );
4323 dv
->HandleWindowEvent( event
);
4325 return !event
.IsAllowed();
4329 wxdataview_row_collapsed_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4330 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4332 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
, dv
->GetId() );
4334 wxDataViewItem
item( (void*) iter
->user_data
);;
4335 event
.SetItem( item
);
4336 event
.SetModel( dv
->GetModel() );
4337 dv
->HandleWindowEvent( event
);
4340 //-----------------------------------------------------------------------------
4342 //-----------------------------------------------------------------------------
4344 void wxDataViewCtrl::AddChildGTK(wxWindowGTK
* child
)
4346 GtkWidget
* treeview
= GtkGetTreeView();
4348 // Insert widget in GtkTreeView
4349 if (gtk_widget_get_realized(treeview
))
4350 gtk_widget_set_parent_window( child
->m_widget
,
4351 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
4352 gtk_widget_set_parent( child
->m_widget
, treeview
);
4356 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
4357 GtkAllocation
*WXUNUSED(gtk_alloc
),
4358 wxDataViewCtrl
*win
)
4360 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
4363 wxWindow
*child
= node
->GetData();
4366 gtk_widget_size_request( child
->m_widget
, &req
);
4368 GtkAllocation alloc
;
4369 alloc
.x
= child
->m_x
;
4370 alloc
.y
= child
->m_y
;
4371 alloc
.width
= child
->m_width
;
4372 alloc
.height
= child
->m_height
;
4373 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
4375 node
= node
->GetNext();
4380 //-----------------------------------------------------------------------------
4381 // "motion_notify_event"
4382 //-----------------------------------------------------------------------------
4385 gtk_dataview_motion_notify_callback( GtkWidget
*WXUNUSED(widget
),
4386 GdkEventMotion
*gdk_event
,
4387 wxDataViewCtrl
*dv
)
4389 if (gdk_event
->is_hint
)
4393 GdkModifierType state
;
4394 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
4400 GtkTreeViewColumn
*column
= NULL
;
4403 if (gtk_tree_view_get_path_at_pos(
4404 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4405 (int) gdk_event
->x
, (int) gdk_event
->y
,
4414 dv
->GtkGetInternal()->get_iter( &iter
, path
);
4422 //-----------------------------------------------------------------------------
4423 // "button_press_event"
4424 //-----------------------------------------------------------------------------
4427 gtk_dataview_button_press_callback( GtkWidget
*WXUNUSED(widget
),
4428 GdkEventButton
*gdk_event
,
4429 wxDataViewCtrl
*dv
)
4431 if ((gdk_event
->button
== 3) && (gdk_event
->type
== GDK_BUTTON_PRESS
))
4434 GtkTreeViewColumn
*column
= NULL
;
4437 gtk_tree_view_get_path_at_pos
4439 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4440 (int) gdk_event
->x
, (int) gdk_event
->y
,
4447 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, dv
->GetId() );
4449 event
.SetItem(dv
->GTKPathToItem(path
));
4450 event
.SetModel( dv
->GetModel() );
4451 return dv
->HandleWindowEvent( event
);
4457 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4459 wxDataViewCtrl::~wxDataViewCtrl()
4461 // Stop editing before destroying the control to remove any event handlers
4462 // which are added when editing started: if we didn't do this, the base
4463 // class dtor would assert as it checks for any leftover handlers.
4466 GtkTreeViewColumn
*col
;
4467 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4469 wxDataViewColumn
* const wxcol
= FromGTKColumn(col
);
4472 // This won't do anything if we're not editing it
4473 wxcol
->GetRenderer()->CancelEditing();
4482 void wxDataViewCtrl::Init()
4486 m_cols
.DeleteContents( true );
4488 m_uniformRowHeight
= -1;
4491 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4496 const wxValidator
& validator
,
4497 const wxString
& name
)
4499 if (!PreCreation( parent
, pos
, size
) ||
4500 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
4502 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4506 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
4507 g_object_ref(m_widget
);
4509 GTKScrolledWindowSetBorder(m_widget
, style
);
4511 m_treeview
= gtk_tree_view_new();
4512 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
4514 m_focusWidget
= GTK_WIDGET(m_treeview
);
4516 g_signal_connect (m_treeview
, "size_allocate",
4517 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
4520 if (!gtk_check_version(2,6,0))
4522 bool fixed
= (style
& wxDV_VARIABLE_LINE_HEIGHT
) == 0;
4523 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4527 if (style
& wxDV_MULTIPLE
)
4529 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4530 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
4533 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
4536 if (!gtk_check_version(2,10,0))
4538 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
4540 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
4541 (style
& wxDV_VERT_RULES
) != 0)
4542 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
4543 else if (style
& wxDV_VERT_RULES
)
4544 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
4545 else if (style
& wxDV_HORIZ_RULES
)
4546 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
4548 if (grid
!= GTK_TREE_VIEW_GRID_LINES_NONE
)
4549 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
4553 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_ROW_LINES
) != 0 );
4555 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
4556 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
4557 gtk_widget_show (m_treeview
);
4559 m_parent
->DoAddChild( this );
4563 GtkEnableSelectionEvents();
4565 g_signal_connect_after (m_treeview
, "row-activated",
4566 G_CALLBACK (wxdataview_row_activated_callback
), this);
4568 g_signal_connect (m_treeview
, "test-collapse-row",
4569 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
4571 g_signal_connect_after (m_treeview
, "row-collapsed",
4572 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
4574 g_signal_connect (m_treeview
, "test-expand-row",
4575 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
4577 g_signal_connect_after (m_treeview
, "row-expanded",
4578 G_CALLBACK (wxdataview_row_expanded_callback
), this);
4580 g_signal_connect (m_treeview
, "motion_notify_event",
4581 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
4583 g_signal_connect (m_treeview
, "button_press_event",
4584 G_CALLBACK (gtk_dataview_button_press_callback
), this);
4589 wxDataViewItem
wxDataViewCtrl::GTKPathToItem(GtkTreePath
*path
) const
4592 return wxDataViewItem(path
&& m_internal
->get_iter(&iter
, path
)
4597 void wxDataViewCtrl::OnInternalIdle()
4599 wxWindow::OnInternalIdle();
4601 m_internal
->OnInternalIdle();
4603 unsigned int cols
= GetColumnCount();
4605 for (i
= 0; i
< cols
; i
++)
4607 wxDataViewColumn
*col
= GetColumn( i
);
4608 col
->OnInternalIdle();
4611 if (m_ensureVisibleDefered
.IsOk())
4613 ExpandAncestors(m_ensureVisibleDefered
);
4615 iter
.user_data
= (gpointer
) m_ensureVisibleDefered
.GetID();
4616 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4617 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
4618 m_ensureVisibleDefered
= wxDataViewItem(0);
4622 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4624 wxDELETE(m_internal
);
4626 if (!wxDataViewCtrlBase::AssociateModel( model
))
4630 if (!gtk_check_version(2,6,0))
4632 bool fixed
= (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
) == 0) || (model
->IsVirtualListModel()));
4633 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4637 m_internal
= new wxDataViewCtrlInternal( this, model
);
4642 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4644 return m_internal
->EnableDragSource( format
);
4647 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4649 return m_internal
->EnableDropTarget( format
);
4652 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4654 if (!wxDataViewCtrlBase::AppendColumn(col
))
4657 m_cols
.Append( col
);
4660 if (!gtk_check_version(2,6,0))
4662 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4663 GTK_TREE_VIEW_COLUMN_FIXED
)
4664 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4668 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
4669 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4674 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4676 if (!wxDataViewCtrlBase::PrependColumn(col
))
4679 m_cols
.Insert( col
);
4682 if (!gtk_check_version(2,6,0))
4684 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4685 GTK_TREE_VIEW_COLUMN_FIXED
)
4686 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4690 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4691 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
4696 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4698 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4701 m_cols
.Insert( pos
, col
);
4704 if (!gtk_check_version(2,6,0))
4706 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4707 GTK_TREE_VIEW_COLUMN_FIXED
)
4708 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4712 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4713 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), pos
);
4718 unsigned int wxDataViewCtrl::GetColumnCount() const
4720 return m_cols
.GetCount();
4723 wxDataViewColumn
* wxDataViewCtrl::FromGTKColumn(GtkTreeViewColumn
*gtk_col
) const
4728 wxDataViewColumnList::const_iterator iter
;
4729 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4731 wxDataViewColumn
*col
= *iter
;
4732 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
4738 wxFAIL_MSG( "No matching column?" );
4743 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
4745 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
4747 return FromGTKColumn(gtk_col
);
4750 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4752 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4753 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
4755 m_cols
.DeleteObject( column
);
4760 bool wxDataViewCtrl::ClearColumns()
4762 wxDataViewColumnList::iterator iter
;
4763 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4765 wxDataViewColumn
*col
= *iter
;
4766 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4767 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4775 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4777 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4779 wxGtkList
list(gtk_tree_view_get_columns(GTK_TREE_VIEW(m_treeview
)));
4781 return g_list_index( list
, (gconstpointer
) gtk_column
);
4784 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4786 return m_internal
->GetDataViewSortColumn();
4789 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4792 iter
.user_data
= item
.GetID();
4793 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4794 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
4797 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4800 iter
.user_data
= item
.GetID();
4801 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4802 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
4805 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4808 iter
.user_data
= item
.GetID();
4809 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4810 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview
), path
) != 0;
4813 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4815 // The tree doesn't have any current item if it hadn't been created yet but
4816 // it's arguably not an error to call this function in this case so just
4817 // return an invalid item without asserting.
4819 return wxDataViewItem();
4822 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), path
.ByRef(), NULL
);
4824 return GTKPathToItem(path
);
4827 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4829 wxCHECK_RET( m_treeview
,
4830 "Current item can't be set before creating the control." );
4832 // We need to make sure the model knows about this item or the path would
4833 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4834 ExpandAncestors(item
);
4836 // We also need to preserve the existing selection from changing.
4837 // Unfortunately the only way to do it seems to use our own selection
4838 // function and forbid any selection changes during set cursor call.
4839 wxGtkTreeSelectionLock
4840 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4842 // Do move the cursor now.
4844 iter
.user_data
= item
.GetID();
4845 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4847 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, NULL
, FALSE
);
4850 wxDataViewColumn
*wxDataViewCtrl::GetCurrentColumn() const
4852 // The tree doesn't have any current item if it hadn't been created yet but
4853 // it's arguably not an error to call this function in this case so just
4854 // return NULL without asserting.
4858 GtkTreeViewColumn
*col
;
4859 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4860 return FromGTKColumn(col
);
4863 void wxDataViewCtrl::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
4865 wxCHECK_RET( m_treeview
,
4866 "Current item can't be set before creating the control." );
4867 wxCHECK_RET( item
.IsOk(), "invalid item" );
4868 wxCHECK_RET( column
, "no column provided" );
4870 // We need to make sure the model knows about this item or the path would
4871 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4872 ExpandAncestors(item
);
4874 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4876 // We also need to preserve the existing selection from changing.
4877 // Unfortunately the only way to do it seems to use our own selection
4878 // function and forbid any selection changes during set cursor call.
4879 wxGtkTreeSelectionLock
4880 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4882 // Do move the cursor now.
4884 iter
.user_data
= item
.GetID();
4885 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4887 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, gcolumn
, TRUE
);
4890 int wxDataViewCtrl::GetSelectedItemsCount() const
4892 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4894 return gtk_tree_selection_count_selected_rows(selection
);
4897 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4901 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4902 if (HasFlag(wxDV_MULTIPLE
))
4904 GtkTreeModel
*model
;
4905 wxGtkTreePathList
list(gtk_tree_selection_get_selected_rows(selection
, &model
));
4907 for ( GList
* current
= list
; current
; current
= g_list_next(current
) )
4909 GtkTreePath
*path
= (GtkTreePath
*) current
->data
;
4911 sel
.Add(GTKPathToItem(path
));
4917 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
4919 sel
.Add( wxDataViewItem(iter
.user_data
) );
4926 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4928 GtkDisableSelectionEvents();
4930 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4932 gtk_tree_selection_unselect_all( selection
);
4934 wxDataViewItem last_parent
;
4937 for (i
= 0; i
< sel
.GetCount(); i
++)
4939 wxDataViewItem item
= sel
[i
];
4940 wxDataViewItem parent
= GetModel()->GetParent( item
);
4943 if (parent
!= last_parent
)
4944 ExpandAncestors(item
);
4946 last_parent
= parent
;
4949 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4950 iter
.user_data
= (gpointer
) item
.GetID();
4951 gtk_tree_selection_select_iter( selection
, &iter
);
4954 GtkEnableSelectionEvents();
4957 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4959 ExpandAncestors(item
);
4961 GtkDisableSelectionEvents();
4963 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4966 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4967 iter
.user_data
= (gpointer
) item
.GetID();
4968 gtk_tree_selection_select_iter( selection
, &iter
);
4970 GtkEnableSelectionEvents();
4973 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4975 GtkDisableSelectionEvents();
4977 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4980 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4981 iter
.user_data
= (gpointer
) item
.GetID();
4982 gtk_tree_selection_unselect_iter( selection
, &iter
);
4984 GtkEnableSelectionEvents();
4987 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4989 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4992 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4993 iter
.user_data
= (gpointer
) item
.GetID();
4995 return gtk_tree_selection_iter_is_selected( selection
, &iter
) != 0;
4998 void wxDataViewCtrl::SelectAll()
5000 GtkDisableSelectionEvents();
5002 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5004 gtk_tree_selection_select_all( selection
);
5006 GtkEnableSelectionEvents();
5009 void wxDataViewCtrl::UnselectAll()
5011 GtkDisableSelectionEvents();
5013 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5015 gtk_tree_selection_unselect_all( selection
);
5017 GtkEnableSelectionEvents();
5020 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem
& item
,
5021 const wxDataViewColumn
*WXUNUSED(column
))
5023 m_ensureVisibleDefered
= item
;
5024 ExpandAncestors(item
);
5027 iter
.user_data
= (gpointer
) item
.GetID();
5028 wxGtkTreePath
path(m_internal
->get_path( &iter
));
5029 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
5032 void wxDataViewCtrl::HitTest(const wxPoint
& point
,
5033 wxDataViewItem
& item
,
5034 wxDataViewColumn
*& column
) const
5036 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
5037 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
5038 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
5039 // So we have to use both of them.
5040 item
= wxDataViewItem(0);
5042 wxGtkTreePath path
, pathScratch
;
5043 GtkTreeViewColumn
* GtkColumn
= NULL
;
5044 GtkTreeViewDropPosition pos
= GTK_TREE_VIEW_DROP_INTO_OR_AFTER
;
5048 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5049 wxDataViewCtrl
* self
= const_cast<wxDataViewCtrl
*>(this); // ugly workaround, self is NOT const
5050 GtkTreeView
* treeView
= GTK_TREE_VIEW(self
->GtkGetTreeView());
5052 // is there possibly a better suited function to get the column?
5053 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5055 (int) point
.x
, (int) point
.y
,
5056 pathScratch
.ByRef(),
5057 &GtkColumn
, // here we get the GtkColumn
5061 if ( GtkColumn
!= NULL
)
5063 // we got GTK column
5064 // the right call now which takes the header into account
5065 gtk_tree_view_get_dest_row_at_pos( treeView
, (int) point
.x
, (int) point
.y
, path
.ByRef(), &pos
);
5068 item
= wxDataViewItem(GTKPathToItem(path
));
5069 // else we got a GTK column but the position is not over an item, e.g. below last item
5070 for ( unsigned int i
=0, cols
=GetColumnCount(); i
<cols
; ++i
) // search the wx column
5072 wxDataViewColumn
* col
= GetColumn(i
);
5073 if ( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == GtkColumn
)
5075 column
= col
; // here we get the wx column
5080 // else no column and thus no item, both null
5084 wxDataViewCtrl::GetItemRect(const wxDataViewItem
& WXUNUSED(item
),
5085 const wxDataViewColumn
*WXUNUSED(column
)) const
5090 bool wxDataViewCtrl::SetRowHeight(int rowHeight
)
5092 m_uniformRowHeight
= rowHeight
;
5096 void wxDataViewCtrl::DoSetExpanderColumn()
5098 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
5099 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5102 void wxDataViewCtrl::DoSetIndent()
5106 void wxDataViewCtrl::GtkDisableSelectionEvents()
5108 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5109 g_signal_handlers_disconnect_by_func( selection
,
5110 (gpointer
) (wxdataview_selection_changed_callback
), this);
5113 void wxDataViewCtrl::GtkEnableSelectionEvents()
5115 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5116 g_signal_connect_after (selection
, "changed",
5117 G_CALLBACK (wxdataview_selection_changed_callback
), this);
5120 // ----------------------------------------------------------------------------
5121 // visual attributes stuff
5122 // ----------------------------------------------------------------------------
5126 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
5128 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
5131 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
5133 wxDataViewCtrlBase::DoApplyWidgetStyle(style
);
5134 gtk_widget_modify_style(m_treeview
, style
);
5137 #endif // !wxUSE_GENERICDATAVIEWCTRL
5139 #endif // wxUSE_DATAVIEWCTRL