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/dc.h"
33 #include "wx/gtk/dcclient.h"
37 #include "wx/gtk/private.h"
38 #include "wx/gtk/private/event.h"
39 #include "wx/gtk/private/gdkconv.h"
40 #include "wx/gtk/private/gtk2-compat.h"
41 #include "wx/gtk/private/list.h"
42 using namespace wxGTKImpl
;
44 class wxGtkDataViewModelNotifier
;
47 #define wxConstGdkRect const GdkRectangle
49 #define wxConstGdkRect GdkRectangle
52 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 static wxDataViewCtrlInternal
*gs_internal
= NULL
;
57 class wxGtkTreeModelNode
;
60 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
63 // ----------------------------------------------------------------------------
64 // wxGtkTreePath: self-destroying GtkTreePath
65 // ----------------------------------------------------------------------------
67 // Usually this object is initialized with the associated GtkTreePath
68 // immediately when it's constructed but it can also be changed later either by
69 // using Assign() or by getting the pointer to the internally stored pointer
70 // value using ByRef(). The latter should be avoided but is very convenient
71 // when using GTK functions with GtkTreePath output parameters.
75 // Ctor takes ownership of the given path and will free it if non-NULL.
76 wxGtkTreePath(GtkTreePath
*path
= NULL
) : m_path(path
) { }
78 // Creates a tree path for the given string path.
79 wxGtkTreePath(const gchar
*strpath
)
80 : m_path(gtk_tree_path_new_from_string(strpath
))
84 // Set the stored pointer if not done by ctor.
85 void Assign(GtkTreePath
*path
)
87 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
92 // Return the pointer to the internally stored pointer. This should only be
93 // used to initialize the object by passing it to some GTK function.
96 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
102 operator GtkTreePath
*() const { return m_path
; }
104 ~wxGtkTreePath() { if ( m_path
) gtk_tree_path_free(m_path
); }
109 wxDECLARE_NO_COPY_CLASS(wxGtkTreePath
);
112 // ----------------------------------------------------------------------------
113 // wxGtkTreePathList: self-destroying list of GtkTreePath objects.
114 // ----------------------------------------------------------------------------
116 class wxGtkTreePathList
: public wxGtkList
119 // Ctor takes ownership of the list.
120 explicit wxGtkTreePathList(GList
* list
)
127 // Delete the list contents, wxGtkList will delete the list itself.
128 g_list_foreach(m_list
, (GFunc
)gtk_tree_path_free
, NULL
);
132 // ----------------------------------------------------------------------------
133 // wxGtkTreeSelectionLock: prevent selection from changing during the
134 // lifetime of this object
135 // ----------------------------------------------------------------------------
137 // Implementation note: it could be expected that setting the selection
138 // function in this class ctor and resetting it back to the old value in its
139 // dtor would work. However currently gtk_tree_selection_get_select_function()
140 // can't be passed NULL (see https://bugzilla.gnome.org/show_bug.cgi?id=626276)
141 // so we can't do this. Instead, we always use the selection function (which
142 // imposes extra overhead, albeit minimal one, on all selection operations) and
143 // just set/reset the flag telling it whether it should allow or forbid the
146 // Also notice that currently only a single object of this class may exist at
147 // any given moment. It's just simpler like this and we don't need anything
152 gboolean
wxdataview_selection_func(GtkTreeSelection
* WXUNUSED(selection
),
153 GtkTreeModel
* WXUNUSED(model
),
154 GtkTreePath
* WXUNUSED(path
),
155 gboolean
WXUNUSED(path_currently_selected
),
162 class wxGtkTreeSelectionLock
165 wxGtkTreeSelectionLock(GtkTreeSelection
*selection
)
166 : m_selection(selection
)
168 wxASSERT_MSG( !ms_instance
, "this class is not reentrant currently" );
172 CheckCurrentSelectionFunc(NULL
);
174 // Pass some non-NULL pointer as "data" for the callback, it doesn't
175 // matter what it is as long as it's non-NULL.
176 gtk_tree_selection_set_select_function(selection
,
177 wxdataview_selection_func
,
182 ~wxGtkTreeSelectionLock()
184 CheckCurrentSelectionFunc(wxdataview_selection_func
);
186 gtk_tree_selection_set_select_function(m_selection
,
187 wxdataview_selection_func
,
195 void CheckCurrentSelectionFunc(GtkTreeSelectionFunc func
)
197 // We can only use gtk_tree_selection_get_select_function() with 2.14+
198 // so check for its availability both during compile- and run-time.
199 #if GTK_CHECK_VERSION(2, 14, 0)
201 if ( gtk_check_version(2, 14, 0) != NULL
)
205 // If this assert is triggered, it means the code elsewhere has called
206 // gtk_tree_selection_set_select_function() but currently doing this
207 // breaks this class so the code here needs to be changed.
210 gtk_tree_selection_get_select_function(m_selection
) == func
,
211 "selection function has changed unexpectedly, review this code!"
218 static wxGtkTreeSelectionLock
*ms_instance
;
220 GtkTreeSelection
* const m_selection
;
222 wxDECLARE_NO_COPY_CLASS(wxGtkTreeSelectionLock
);
225 wxGtkTreeSelectionLock
*wxGtkTreeSelectionLock::ms_instance
= NULL
;
227 //-----------------------------------------------------------------------------
228 // wxDataViewCtrlInternal
229 //-----------------------------------------------------------------------------
231 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
232 WX_DEFINE_LIST(ItemList
)
234 class wxDataViewCtrlInternal
237 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
);
238 ~wxDataViewCtrlInternal();
241 GtkTreeModelFlags
get_flags();
242 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
243 GtkTreePath
*get_path( GtkTreeIter
*iter
);
244 gboolean
iter_next( GtkTreeIter
*iter
);
245 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
246 gboolean
iter_has_child( GtkTreeIter
*iter
);
247 gint
iter_n_children( GtkTreeIter
*iter
);
248 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
249 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
253 bool EnableDragSource( const wxDataFormat
&format
);
254 bool EnableDropTarget( const wxDataFormat
&format
);
256 gboolean
row_draggable( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
);
257 gboolean
drag_data_delete( GtkTreeDragSource
*drag_source
, GtkTreePath
* path
);
258 gboolean
drag_data_get( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
,
259 GtkSelectionData
*selection_data
);
260 gboolean
drag_data_received( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest
,
261 GtkSelectionData
*selection_data
);
262 gboolean
row_drop_possible( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest_path
,
263 GtkSelectionData
*selection_data
);
265 // notifactions from wxDataViewModel
266 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
267 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
268 bool ItemChanged( const wxDataViewItem
&item
);
269 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
276 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
277 GtkSortType
GetSortOrder() const { return m_sort_order
; }
279 void SetSortColumn( int column
) { m_sort_column
= column
; }
280 int GetSortColumn() const { return m_sort_column
; }
282 void SetDataViewSortColumn( wxDataViewColumn
*column
) { m_dataview_sort_column
= column
; }
283 wxDataViewColumn
*GetDataViewSortColumn() { return m_dataview_sort_column
; }
285 bool IsSorted() const { return m_sort_column
>= 0; }
287 // Should we be sorted either because we have a configured sort column or
288 // because we have a default sort order?
289 bool ShouldBeSorted() const
291 return IsSorted() || GetDataViewModel()->HasDefaultCompare();
296 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
297 const wxDataViewModel
* GetDataViewModel() const { return m_wx_model
; }
298 wxDataViewCtrl
* GetOwner() { return m_owner
; }
299 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
301 // item can be deleted already in the model
302 int GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
304 void OnInternalIdle();
308 void ScheduleRefresh();
310 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
311 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
312 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
313 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
314 void BuildBranch( wxGtkTreeModelNode
*branch
);
317 wxGtkTreeModelNode
*m_root
;
318 wxDataViewModel
*m_wx_model
;
319 GtkWxTreeModel
*m_gtk_model
;
320 wxDataViewCtrl
*m_owner
;
321 GtkSortType m_sort_order
;
322 wxDataViewColumn
*m_dataview_sort_column
;
325 GtkTargetEntry m_dragSourceTargetEntry
;
326 wxCharBuffer m_dragSourceTargetEntryTarget
;
327 wxDataObject
*m_dragDataObject
;
329 GtkTargetEntry m_dropTargetTargetEntry
;
330 wxCharBuffer m_dropTargetTargetEntryTarget
;
331 wxDataObject
*m_dropDataObject
;
333 wxGtkDataViewModelNotifier
*m_notifier
;
339 //-----------------------------------------------------------------------------
340 // wxGtkTreeModelNode
341 //-----------------------------------------------------------------------------
344 int LINKAGEMODE
wxGtkTreeModelChildCmp( void** id1
, void** id2
)
346 int ret
= gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(*id1
), wxDataViewItem(*id2
),
347 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
352 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
353 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren
);
355 class wxGtkTreeModelNode
358 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
359 wxDataViewCtrlInternal
*internal
)
363 m_internal
= internal
;
366 ~wxGtkTreeModelNode()
368 size_t count
= m_nodes
.GetCount();
370 for (i
= 0; i
< count
; i
++)
372 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
377 void AddNode( wxGtkTreeModelNode
* child
)
379 m_nodes
.Add( child
);
381 void *id
= child
->GetItem().GetID();
383 m_children
.Add( id
);
385 if (m_internal
->ShouldBeSorted())
387 gs_internal
= m_internal
;
388 m_children
.Sort( &wxGtkTreeModelChildCmp
);
392 void InsertNode( wxGtkTreeModelNode
* child
, unsigned pos
)
394 if (m_internal
->ShouldBeSorted())
400 void *id
= child
->GetItem().GetID();
402 // Insert into m_nodes so that the order of nodes in m_nodes is the
403 // same as the order of their corresponding IDs in m_children:
404 const unsigned int count
= m_nodes
.GetCount();
405 bool inserted
= false;
406 for (unsigned i
= 0; i
< count
; i
++)
408 wxGtkTreeModelNode
*node
= m_nodes
[i
];
409 int posInChildren
= m_children
.Index(node
->GetItem().GetID());
410 if ( (unsigned)posInChildren
>= pos
)
412 m_nodes
.Insert(child
, i
);
420 m_children
.Insert( id
, pos
);
423 void AddLeaf( void* id
)
425 InsertLeaf(id
, m_children
.size());
428 void InsertLeaf( void* id
, unsigned pos
)
430 m_children
.Insert( id
, pos
);
432 if (m_internal
->ShouldBeSorted())
434 gs_internal
= m_internal
;
435 m_children
.Sort( &wxGtkTreeModelChildCmp
);
439 void DeleteChild( void* id
)
441 m_children
.Remove( id
);
443 unsigned int count
= m_nodes
.GetCount();
445 for (pos
= 0; pos
< count
; pos
++)
447 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
448 if (node
->GetItem().GetID() == id
)
450 m_nodes
.RemoveAt( pos
);
457 // returns position of child node for given item in children list or wxNOT_FOUND
458 int FindChildByItem(const wxDataViewItem
& item
) const
460 const void* itemId
= item
.GetID();
461 const wxGtkTreeModelChildren
& nodes
= m_children
;
462 const int len
= nodes
.size();
463 for ( int i
= 0; i
< len
; i
++ )
465 if ( nodes
[i
] == itemId
)
471 wxGtkTreeModelNode
* GetParent()
473 wxGtkTreeModelNodes
&GetNodes()
475 wxGtkTreeModelChildren
&GetChildren()
476 { return m_children
; }
478 unsigned int GetChildCount() const { return m_children
.GetCount(); }
479 unsigned int GetNodesCount() const { return m_nodes
.GetCount(); }
481 wxDataViewItem
&GetItem() { return m_item
; }
482 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
487 wxGtkTreeModelNode
*m_parent
;
488 wxGtkTreeModelNodes m_nodes
;
489 wxGtkTreeModelChildren m_children
;
490 wxDataViewItem m_item
;
491 wxDataViewCtrlInternal
*m_internal
;
495 //-----------------------------------------------------------------------------
497 //-----------------------------------------------------------------------------
499 extern bool g_blockEventsOnDrag
;
501 //-----------------------------------------------------------------------------
502 // define new GTK+ class wxGtkTreeModel
503 //-----------------------------------------------------------------------------
507 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
508 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
509 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
510 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
512 GType
gtk_wx_tree_model_get_type (void);
514 struct _GtkWxTreeModel
520 wxDataViewCtrlInternal
*internal
;
523 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
524 static void wxgtk_tree_model_init (GTypeInstance
* instance
, void*);
526 static void wxgtk_tree_model_tree_model_init (void* g_iface
, void*);
527 static void wxgtk_tree_model_sortable_init (void* g_iface
, void*);
528 static void wxgtk_tree_model_drag_source_init(void* g_iface
, void*);
529 static void wxgtk_tree_model_drag_dest_init (void* g_iface
, void*);
531 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
532 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
533 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
535 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
538 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
540 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
544 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
546 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
548 GtkTreeIter
*parent
);
549 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
551 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
553 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
557 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
562 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
563 gint
*sort_column_id
,
565 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
568 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
570 GtkTreeIterCompareFunc func
,
572 GDestroyNotify destroy
);
573 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
574 GtkTreeIterCompareFunc func
,
576 GDestroyNotify destroy
);
577 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
580 static gboolean
wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
582 static gboolean
wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
584 static gboolean
wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
586 GtkSelectionData
*selection_data
);
587 static gboolean
wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
589 GtkSelectionData
*selection_data
);
590 static gboolean
wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
591 GtkTreePath
*dest_path
,
592 GtkSelectionData
*selection_data
);
595 gtk_wx_tree_model_get_type (void)
597 static GType tree_model_type
= 0;
599 if (!tree_model_type
)
601 const GTypeInfo tree_model_info
=
603 sizeof (GObjectClass
),
604 NULL
, /* base_init */
605 NULL
, /* base_finalize */
607 NULL
, /* class_finalize */
608 NULL
, /* class_data */
609 sizeof (GtkWxTreeModel
),
611 wxgtk_tree_model_init
,
614 static const GInterfaceInfo tree_model_iface_info
=
616 wxgtk_tree_model_tree_model_init
,
621 static const GInterfaceInfo sortable_iface_info
=
623 wxgtk_tree_model_sortable_init
,
628 static const GInterfaceInfo drag_source_iface_info
=
630 wxgtk_tree_model_drag_source_init
,
635 static const GInterfaceInfo drag_dest_iface_info
=
637 wxgtk_tree_model_drag_dest_init
,
642 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
643 &tree_model_info
, (GTypeFlags
)0 );
645 g_type_add_interface_static (tree_model_type
,
647 &tree_model_iface_info
);
648 g_type_add_interface_static (tree_model_type
,
649 GTK_TYPE_TREE_SORTABLE
,
650 &sortable_iface_info
);
651 g_type_add_interface_static (tree_model_type
,
652 GTK_TYPE_TREE_DRAG_DEST
,
653 &drag_dest_iface_info
);
654 g_type_add_interface_static (tree_model_type
,
655 GTK_TYPE_TREE_DRAG_SOURCE
,
656 &drag_source_iface_info
);
659 return tree_model_type
;
662 static GtkWxTreeModel
*
663 wxgtk_tree_model_new(void)
665 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
670 wxgtk_tree_model_tree_model_init(void* g_iface
, void*)
672 GtkTreeModelIface
* iface
= static_cast<GtkTreeModelIface
*>(g_iface
);
673 iface
->get_flags
= wxgtk_tree_model_get_flags
;
674 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
675 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
676 iface
->get_iter
= wxgtk_tree_model_get_iter
;
677 iface
->get_path
= wxgtk_tree_model_get_path
;
678 iface
->get_value
= wxgtk_tree_model_get_value
;
679 iface
->iter_next
= wxgtk_tree_model_iter_next
;
680 iface
->iter_children
= wxgtk_tree_model_iter_children
;
681 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
682 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
683 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
684 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
688 wxgtk_tree_model_sortable_init(void* g_iface
, void*)
690 GtkTreeSortableIface
* iface
= static_cast<GtkTreeSortableIface
*>(g_iface
);
691 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
692 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
693 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
694 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
695 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
699 wxgtk_tree_model_drag_source_init(void* g_iface
, void*)
701 GtkTreeDragSourceIface
* iface
= static_cast<GtkTreeDragSourceIface
*>(g_iface
);
702 iface
->row_draggable
= wxgtk_tree_model_row_draggable
;
703 iface
->drag_data_delete
= wxgtk_tree_model_drag_data_delete
;
704 iface
->drag_data_get
= wxgtk_tree_model_drag_data_get
;
708 wxgtk_tree_model_drag_dest_init(void* g_iface
, void*)
710 GtkTreeDragDestIface
* iface
= static_cast<GtkTreeDragDestIface
*>(g_iface
);
711 iface
->drag_data_received
= wxgtk_tree_model_drag_data_received
;
712 iface
->row_drop_possible
= wxgtk_tree_model_row_drop_possible
;
716 wxgtk_tree_model_init(GTypeInstance
* instance
, void*)
718 GtkWxTreeModel
* tree_model
= GTK_WX_TREE_MODEL(instance
);
719 tree_model
->internal
= NULL
;
720 tree_model
->stamp
= g_random_int();
725 //-----------------------------------------------------------------------------
726 // implement callbacks from wxGtkTreeModel class by letting
727 // them call the methods of wxWidgets' wxDataViewModel
728 //-----------------------------------------------------------------------------
730 static GtkTreeModelFlags
731 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
733 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
734 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), (GtkTreeModelFlags
)0 );
736 return wxtree_model
->internal
->get_flags();
740 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
742 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
743 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
745 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
749 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
752 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
753 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
755 GType gtype
= G_TYPE_INVALID
;
757 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
759 if (wxtype
== wxT("string"))
760 gtype
= G_TYPE_STRING
;
763 gtype
= G_TYPE_POINTER
;
764 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
771 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
775 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
776 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
777 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
779 return wxtree_model
->internal
->get_iter( iter
, path
);
783 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
786 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
787 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
788 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
790 return wxtree_model
->internal
->get_path( iter
);
794 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
799 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
800 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
802 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
803 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
804 if (mtype
== wxT("string"))
807 g_value_init( value
, G_TYPE_STRING
);
808 wxDataViewItem
item( (void*) iter
->user_data
);
809 model
->GetValue( variant
, item
, (unsigned int) column
);
811 g_value_set_string( value
, variant
.GetString().utf8_str() );
815 wxFAIL_MSG( wxT("non-string columns not supported yet") );
820 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
823 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
825 // This happens when clearing the view by calling .._set_model( NULL );
826 if (iter
->stamp
== 0) return FALSE
;
828 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
829 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
831 return wxtree_model
->internal
->iter_next( iter
);
835 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
839 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
840 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
843 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
846 return wxtree_model
->internal
->iter_children( iter
, parent
);
850 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
853 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
854 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
855 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
857 return wxtree_model
->internal
->iter_has_child( iter
);
861 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
864 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
865 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
866 g_return_val_if_fail ( !iter
|| wxtree_model
->stamp
== iter
->stamp
, 0);
868 return wxtree_model
->internal
->iter_n_children( iter
);
872 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
877 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
878 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
880 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
884 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
888 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
889 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
890 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
892 return wxtree_model
->internal
->iter_parent( iter
, child
);
895 /* drag'n'drop iface */
897 wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
900 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
901 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
903 return wxtree_model
->internal
->row_draggable( drag_source
, path
);
907 wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
910 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
911 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
913 return wxtree_model
->internal
->drag_data_delete( drag_source
, path
);
917 wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
919 GtkSelectionData
*selection_data
)
921 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
922 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
925 wxPrintf( "drag_get_data\n");
927 wxGtkString
atom_selection(gdk_atom_name(selection_data
->selection
));
928 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection
) );
930 wxGtkString
atom_target(gdk_atom_name(selection_data
->target
));
931 wxPrintf( "target %s\n", wxString::FromAscii(atom_target
) );
933 wxGtkString
atom_type(gdk_atom_name(selection_data
->type
));
934 wxPrintf( "type %s\n", wxString::FromAscii(atom_type
) );
936 wxPrintf( "format %d\n", selection_data
->format
);
939 return wxtree_model
->internal
->drag_data_get( drag_source
, path
, selection_data
);
943 wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
945 GtkSelectionData
*selection_data
)
947 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
948 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
950 return wxtree_model
->internal
->drag_data_received( drag_dest
, dest
, selection_data
);
954 wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
955 GtkTreePath
*dest_path
,
956 GtkSelectionData
*selection_data
)
958 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
959 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
961 return wxtree_model
->internal
->row_drop_possible( drag_dest
, dest_path
, selection_data
);
966 wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
967 gint
*sort_column_id
,
970 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) sortable
;
972 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
974 if (!wxtree_model
->internal
->IsSorted())
977 *sort_column_id
= -1;
984 *sort_column_id
= wxtree_model
->internal
->GetSortColumn();
987 *order
= wxtree_model
->internal
->GetSortOrder();
992 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
995 wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
999 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
1000 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1002 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
1004 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
1005 (order
!= tree_model
->internal
->GetSortOrder()))
1007 tree_model
->internal
->SetSortColumn( sort_column_id
);
1008 tree_model
->internal
->SetSortOrder( order
);
1010 gtk_tree_sortable_sort_column_changed (sortable
);
1012 tree_model
->internal
->GetDataViewModel()->Resort();
1015 if (gs_lastLeftClickHeader
)
1017 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
1018 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
1019 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
1020 event
.SetModel( dv
->GetModel() );
1021 dv
->HandleWindowEvent( event
);
1024 gs_lastLeftClickHeader
= NULL
;
1028 wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
1029 gint
WXUNUSED(sort_column_id
),
1030 GtkTreeIterCompareFunc func
,
1031 gpointer
WXUNUSED(data
),
1032 GDestroyNotify
WXUNUSED(destroy
))
1034 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1035 g_return_if_fail (func
!= NULL
);
1039 wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
1040 GtkTreeIterCompareFunc func
,
1041 gpointer
WXUNUSED(data
),
1042 GDestroyNotify
WXUNUSED(destroy
))
1044 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1045 g_return_if_fail (func
!= NULL
);
1047 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
1048 // TODO: remove this code
1052 wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
1054 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
1059 //-----------------------------------------------------------------------------
1060 // define new GTK+ class GtkWxRendererText
1061 //-----------------------------------------------------------------------------
1065 #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
1066 #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
1067 #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1068 #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1070 GType
gtk_wx_cell_renderer_text_get_type (void);
1072 typedef struct _GtkWxCellRendererText GtkWxCellRendererText
;
1074 struct _GtkWxCellRendererText
1076 GtkCellRendererText parent
;
1078 wxDataViewRenderer
*wx_renderer
;
1081 static GtkWxCellRendererText
*gtk_wx_cell_renderer_text_new (void);
1082 static void gtk_wx_cell_renderer_text_init (
1083 GTypeInstance
* instance
, void*);
1084 static void gtk_wx_cell_renderer_text_class_init(
1085 void* klass
, void*);
1086 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1087 GtkCellRenderer
*cell
,
1091 wxConstGdkRect
*background_area
,
1092 wxConstGdkRect
*cell_area
,
1093 GtkCellRendererState flags
);
1096 static GObjectClass
*text_cell_parent_class
= NULL
;
1101 gtk_wx_cell_renderer_text_get_type (void)
1103 static GType cell_wx_type
= 0;
1107 const GTypeInfo cell_wx_info
=
1109 sizeof (GtkCellRendererTextClass
),
1110 NULL
, /* base_init */
1111 NULL
, /* base_finalize */
1112 gtk_wx_cell_renderer_text_class_init
,
1113 NULL
, /* class_finalize */
1114 NULL
, /* class_data */
1115 sizeof (GtkWxCellRendererText
),
1116 0, /* n_preallocs */
1117 gtk_wx_cell_renderer_text_init
,
1120 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT
,
1121 "GtkWxCellRendererText", &cell_wx_info
, (GTypeFlags
)0 );
1124 return cell_wx_type
;
1128 gtk_wx_cell_renderer_text_init(GTypeInstance
* instance
, void*)
1130 GtkWxCellRendererText
* cell
= GTK_WX_CELL_RENDERER_TEXT(instance
);
1131 cell
->wx_renderer
= NULL
;
1135 gtk_wx_cell_renderer_text_class_init(void* klass
, void*)
1137 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1139 text_cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1141 cell_class
->start_editing
= gtk_wx_cell_renderer_text_start_editing
;
1144 GtkWxCellRendererText
*
1145 gtk_wx_cell_renderer_text_new (void)
1147 return (GtkWxCellRendererText
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT
, NULL
);
1150 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1151 GtkCellRenderer
*gtk_renderer
,
1152 GdkEvent
*gdk_event
,
1155 wxConstGdkRect
*background_area
,
1156 wxConstGdkRect
*cell_area
,
1157 GtkCellRendererState flags
)
1159 GtkWxCellRendererText
*wxgtk_renderer
= (GtkWxCellRendererText
*) gtk_renderer
;
1160 wxDataViewRenderer
*wx_renderer
= wxgtk_renderer
->wx_renderer
;
1161 wxDataViewColumn
*column
= wx_renderer
->GetOwner();
1164 item(column
->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1166 wxDataViewCtrl
*dv
= column
->GetOwner();
1167 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING
, dv
->GetId() );
1168 event
.SetDataViewColumn( column
);
1169 event
.SetModel( dv
->GetModel() );
1170 event
.SetColumn( column
->GetModelColumn() );
1171 event
.SetItem( item
);
1172 dv
->HandleWindowEvent( event
);
1174 if (event
.IsAllowed())
1175 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class
)->
1176 start_editing( gtk_renderer
, gdk_event
, widget
, path
, background_area
, cell_area
, flags
);
1181 //-----------------------------------------------------------------------------
1182 // define new GTK+ class GtkWxCellRenderer
1183 //-----------------------------------------------------------------------------
1187 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
1188 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
1189 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
1190 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
1192 GType
gtk_wx_cell_renderer_get_type (void);
1194 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
1196 struct _GtkWxCellRenderer
1198 GtkCellRenderer parent
;
1201 wxDataViewCustomRenderer
*cell
;
1204 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
1205 static void gtk_wx_cell_renderer_init (
1206 GTypeInstance
* instance
, void*);
1207 static void gtk_wx_cell_renderer_class_init(
1208 void* klass
, void*);
1209 static void gtk_wx_cell_renderer_get_size (
1210 GtkCellRenderer
*cell
,
1212 wxConstGdkRect
*rectangle
,
1217 static void gtk_wx_cell_renderer_render (
1218 GtkCellRenderer
*cell
,
1225 wxConstGdkRect
*background_area
,
1226 wxConstGdkRect
*cell_area
,
1228 GdkRectangle
*expose_area
,
1230 GtkCellRendererState flags
);
1231 static gboolean
gtk_wx_cell_renderer_activate(
1232 GtkCellRenderer
*cell
,
1236 wxConstGdkRect
*background_area
,
1237 wxConstGdkRect
*cell_area
,
1238 GtkCellRendererState flags
);
1239 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1240 GtkCellRenderer
*cell
,
1244 wxConstGdkRect
*background_area
,
1245 wxConstGdkRect
*cell_area
,
1246 GtkCellRendererState flags
);
1251 gtk_wx_cell_renderer_get_type (void)
1253 static GType cell_wx_type
= 0;
1257 const GTypeInfo cell_wx_info
=
1259 sizeof (GtkCellRendererClass
),
1260 NULL
, /* base_init */
1261 NULL
, /* base_finalize */
1262 gtk_wx_cell_renderer_class_init
,
1263 NULL
, /* class_finalize */
1264 NULL
, /* class_data */
1265 sizeof (GtkWxCellRenderer
),
1266 0, /* n_preallocs */
1267 gtk_wx_cell_renderer_init
,
1270 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
1271 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
1274 return cell_wx_type
;
1278 gtk_wx_cell_renderer_init(GTypeInstance
* instance
, void*)
1280 GtkWxCellRenderer
* cell
= GTK_WX_CELL_RENDERER(instance
);
1285 gtk_wx_cell_renderer_class_init(void* klass
, void*)
1287 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1289 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
1290 cell_class
->render
= gtk_wx_cell_renderer_render
;
1291 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
1292 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
1296 gtk_wx_cell_renderer_new (void)
1298 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
1301 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1302 GtkCellRenderer
*renderer
,
1303 GdkEvent
*WXUNUSED(event
),
1306 wxConstGdkRect
*WXUNUSED(background_area
),
1307 wxConstGdkRect
*cell_area
,
1308 GtkCellRendererState
WXUNUSED(flags
) )
1310 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1311 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1313 // Renderer doesn't support in-place editing
1314 if (!cell
->HasEditorCtrl())
1317 // An in-place editing control is still around
1318 if (cell
->GetEditorCtrl())
1322 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1328 rect
.x
+= cell_area
->x
;
1329 rect
.y
+= cell_area
->y
;
1330 // rect.width -= renderer->xpad * 2;
1331 // rect.height -= renderer->ypad * 2;
1333 // wxRect renderrect(wxRectFromGDKRect(&rect));
1334 wxRect
renderrect(wxRectFromGDKRect(cell_area
));
1337 item(cell
->GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1339 cell
->StartEditing( item
, renderrect
);
1345 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
1346 GtkWidget
*WXUNUSED(widget
),
1347 wxConstGdkRect
*cell_area
,
1353 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1354 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1356 wxSize size
= cell
->GetSize();
1358 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1360 // Uniform row height, if specified, overrides the value returned by the
1362 if ( !ctrl
->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
1364 const int uniformHeight
= ctrl
->GTKGetUniformRowHeight();
1365 if ( uniformHeight
> 0 )
1366 size
.y
= uniformHeight
;
1370 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1371 int calc_width
= xpad
* 2 + size
.x
;
1372 int calc_height
= ypad
* 2 + size
.y
;
1379 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
1381 float xalign
, yalign
;
1382 gtk_cell_renderer_get_alignment(renderer
, &xalign
, &yalign
);
1385 *x_offset
= int(xalign
* (cell_area
->width
- calc_width
- 2 * xpad
));
1386 *x_offset
= MAX(*x_offset
, 0) + xpad
;
1390 *y_offset
= int(yalign
* (cell_area
->height
- calc_height
- 2 * ypad
));
1391 *y_offset
= MAX(*y_offset
, 0) + ypad
;
1396 *width
= calc_width
;
1399 *height
= calc_height
;
1402 struct wxDataViewCustomRenderer::GTKRenderParams
1408 GdkRectangle
* expose_area
;
1411 wxConstGdkRect
* background_area
;
1416 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
1423 wxConstGdkRect
*background_area
,
1424 wxConstGdkRect
*cell_area
,
1426 GdkRectangle
*expose_area
,
1428 GtkCellRendererState flags
)
1431 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1432 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1434 wxDataViewCustomRenderer::GTKRenderParams renderParams
;
1436 renderParams
.cr
= cr
;
1438 renderParams
.window
= window
;
1439 renderParams
.expose_area
= expose_area
;
1441 renderParams
.widget
= widget
;
1442 renderParams
.background_area
= background_area
;
1443 renderParams
.flags
= flags
;
1444 cell
->GTKSetRenderParams(&renderParams
);
1446 wxRect
rect(wxRectFromGDKRect(cell_area
));
1448 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1449 rect
= rect
.Deflate(xpad
, ypad
);
1451 wxDC
* dc
= cell
->GetDC();
1453 wxGraphicsContext
* context
= dc
->GetGraphicsContext();
1454 void* nativeContext
= NULL
;
1456 nativeContext
= context
->GetNativeContext();
1457 if (cr
!= nativeContext
)
1459 cairo_reference(cr
);
1460 dc
->SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr
));
1463 wxWindowDCImpl
*impl
= (wxWindowDCImpl
*) dc
->GetImpl();
1465 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1466 // window such as a DnD drop window.
1467 if (window
!= impl
->m_gdkwindow
)
1470 impl
->m_gdkwindow
= window
;
1476 if (flags
& GTK_CELL_RENDERER_SELECTED
)
1477 state
|= wxDATAVIEW_CELL_SELECTED
;
1478 if (flags
& GTK_CELL_RENDERER_PRELIT
)
1479 state
|= wxDATAVIEW_CELL_PRELIT
;
1480 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1481 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1482 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1483 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1484 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
1485 state
|= wxDATAVIEW_CELL_FOCUSED
;
1486 cell
->WXCallRender( rect
, dc
, state
);
1488 cell
->GTKSetRenderParams(NULL
);
1490 dc
->SetGraphicsContext(NULL
);
1495 gtk_wx_cell_renderer_activate(
1496 GtkCellRenderer
*renderer
,
1500 wxConstGdkRect
*WXUNUSED(background_area
),
1501 wxConstGdkRect
*cell_area
,
1502 GtkCellRendererState
WXUNUSED(flags
) )
1504 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1505 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1508 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1514 rect
.x
+= cell_area
->x
;
1515 rect
.y
+= cell_area
->y
;
1517 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1518 rect
.width
-= xpad
* 2;
1519 rect
.height
-= ypad
* 2;
1521 wxRect
renderrect(wxRectFromGDKRect(&rect
));
1523 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1524 wxDataViewModel
*model
= ctrl
->GetModel();
1526 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
1528 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1532 // activated by <ENTER>
1533 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, NULL
);
1535 else if ( event
->type
== GDK_BUTTON_PRESS
)
1537 GdkEventButton
*button_event
= (GdkEventButton
*)event
;
1538 if ( button_event
->button
== 1 )
1540 wxMouseEvent
mouse_event(wxEVT_LEFT_DOWN
);
1541 InitMouseEvent(ctrl
, mouse_event
, button_event
);
1543 mouse_event
.m_x
-= renderrect
.x
;
1544 mouse_event
.m_y
-= renderrect
.y
;
1546 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, &mouse_event
);
1550 wxLogDebug("unexpected event type in gtk_wx_cell_renderer_activate()");
1554 // ---------------------------------------------------------
1555 // wxGtkDataViewModelNotifier
1556 // ---------------------------------------------------------
1558 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1561 wxGtkDataViewModelNotifier( wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
);
1562 ~wxGtkDataViewModelNotifier();
1564 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1565 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1566 virtual bool ItemChanged( const wxDataViewItem
&item
);
1567 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
1568 virtual bool Cleared();
1569 virtual void Resort();
1570 virtual bool BeforeReset();
1571 virtual bool AfterReset();
1573 void UpdateLastCount();
1576 wxDataViewModel
*m_wx_model
;
1577 wxDataViewCtrlInternal
*m_internal
;
1580 // ---------------------------------------------------------
1581 // wxGtkDataViewListModelNotifier
1582 // ---------------------------------------------------------
1584 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1585 wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
)
1587 m_wx_model
= wx_model
;
1588 m_internal
= internal
;
1591 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1597 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1599 m_internal
->ItemAdded( parent
, item
);
1600 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1603 iter
.stamp
= wxgtk_model
->stamp
;
1604 iter
.user_data
= item
.GetID();
1606 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1607 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1608 gtk_tree_model_row_inserted(
1609 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1614 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1616 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1618 // using _get_path for a deleted item cannot be
1621 iter
.stamp
= wxgtk_model
->stamp
;
1622 iter
.user_data
= (gpointer
) item
.GetID();
1623 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1624 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1626 // so get the path from the parent
1627 GtkTreeIter parentIter
;
1628 parentIter
.stamp
= wxgtk_model
->stamp
;
1629 parentIter
.user_data
= (gpointer
) parent
.GetID();
1630 wxGtkTreePath
parentPath(wxgtk_tree_model_get_path(
1631 GTK_TREE_MODEL(wxgtk_model
), &parentIter
));
1633 // and add the final index ourselves
1634 wxGtkTreePath
path(gtk_tree_path_copy(parentPath
));
1635 int index
= m_internal
->GetIndexOf( parent
, item
);
1636 gtk_tree_path_append_index( path
, index
);
1639 gtk_tree_model_row_deleted(
1640 GTK_TREE_MODEL(wxgtk_model
), path
);
1642 m_internal
->ItemDeleted( parent
, item
);
1644 // Did we remove the last child, causing 'parent' to become a leaf?
1645 if ( !m_wx_model
->IsContainer(parent
) )
1647 gtk_tree_model_row_has_child_toggled
1649 GTK_TREE_MODEL(wxgtk_model
),
1658 void wxGtkDataViewModelNotifier::Resort()
1660 m_internal
->Resort();
1663 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1665 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1668 iter
.stamp
= wxgtk_model
->stamp
;
1669 iter
.user_data
= (gpointer
) item
.GetID();
1671 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1672 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1673 gtk_tree_model_row_changed(
1674 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1676 m_internal
->ItemChanged( item
);
1681 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
)
1683 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1684 wxDataViewCtrl
*ctrl
= m_internal
->GetOwner();
1686 // This adds GTK+'s missing MVC logic for ValueChanged
1688 for (index
= 0; index
< ctrl
->GetColumnCount(); index
++)
1690 wxDataViewColumn
*column
= ctrl
->GetColumn( index
);
1691 if (column
->GetModelColumn() == model_column
)
1693 GtkTreeView
*widget
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
1694 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1696 // Don't attempt to refresh not yet realized tree, it is useless
1697 // and results in GTK errors.
1698 if ( gtk_widget_get_realized(ctrl
->GtkGetTreeView()) )
1702 iter
.stamp
= wxgtk_model
->stamp
;
1703 iter
.user_data
= (gpointer
) item
.GetID();
1704 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1705 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1706 GdkRectangle cell_area
;
1707 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1709 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1710 double d
= gtk_adjustment_get_value( hadjust
);
1711 int xdiff
= (int) d
;
1714 gtk_widget_get_allocation(GTK_WIDGET(gtk_tree_view_column_get_button(gcolumn
)), &a
);
1715 int ydiff
= a
.height
;
1717 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1718 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1721 m_internal
->ValueChanged( item
, model_column
);
1730 bool wxGtkDataViewModelNotifier::BeforeReset()
1732 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1733 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), NULL
);
1738 bool wxGtkDataViewModelNotifier::AfterReset()
1740 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1741 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1743 m_internal
->Cleared();
1745 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), GTK_TREE_MODEL(wxgtk_model
) );
1750 bool wxGtkDataViewModelNotifier::Cleared()
1752 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1754 // There is no call to tell the model that everything
1755 // has been deleted so call row_deleted() for every
1758 int count
= m_internal
->iter_n_children( NULL
); // number of children of root
1760 GtkTreePath
*path
= gtk_tree_path_new_first(); // points to root
1763 for (i
= 0; i
< count
; i
++)
1764 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model
), path
);
1766 gtk_tree_path_free( path
);
1768 m_internal
->Cleared();
1773 // ---------------------------------------------------------
1774 // wxDataViewRenderer
1775 // ---------------------------------------------------------
1777 static gpointer s_user_data
= NULL
;
1780 wxgtk_cell_editable_editing_done( GtkCellEditable
*WXUNUSED(editable
),
1781 wxDataViewRenderer
*wxrenderer
)
1783 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1784 wxDataViewCtrl
*dv
= column
->GetOwner();
1785 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1786 event
.SetDataViewColumn( column
);
1787 event
.SetModel( dv
->GetModel() );
1788 wxDataViewItem
item( s_user_data
);
1789 event
.SetItem( item
);
1790 dv
->HandleWindowEvent( event
);
1794 wxgtk_renderer_editing_started( GtkCellRenderer
*WXUNUSED(cell
), GtkCellEditable
*editable
,
1795 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1800 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1801 wxDataViewCtrl
*dv
= column
->GetOwner();
1802 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1803 event
.SetDataViewColumn( column
);
1804 event
.SetModel( dv
->GetModel() );
1805 wxDataViewItem
item(dv
->GTKPathToItem(wxGtkTreePath(path
)));
1806 event
.SetItem( item
);
1807 dv
->HandleWindowEvent( event
);
1809 if (GTK_IS_CELL_EDITABLE(editable
))
1811 s_user_data
= item
.GetID();
1813 g_signal_connect (GTK_CELL_EDITABLE (editable
), "editing_done",
1814 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1815 (gpointer
) wxrenderer
);
1821 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1823 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1825 wxDataViewRendererBase( varianttype
, mode
, align
)
1830 // we haven't changed them yet
1831 m_usingDefaultAttrs
= true;
1833 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1834 // after the m_renderer pointer has been initialized
1837 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
1839 gtk_tree_view_column_pack_end( column
, m_renderer
, TRUE
/* expand */);
1842 void wxDataViewRenderer::GtkInitHandlers()
1845 if (!gtk_check_version(2,6,0))
1848 g_signal_connect (GTK_CELL_RENDERER(m_renderer
), "editing_started",
1849 G_CALLBACK (wxgtk_renderer_editing_started
),
1854 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1856 GtkCellRendererMode gtkMode
;
1859 case wxDATAVIEW_CELL_INERT
:
1860 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1863 case wxDATAVIEW_CELL_ACTIVATABLE
:
1864 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1867 case wxDATAVIEW_CELL_EDITABLE
:
1868 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1872 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1878 // This value is most often ignored in GtkTreeView
1879 GValue gvalue
= { 0, };
1880 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1881 g_value_set_enum( &gvalue
, gtkMode
);
1882 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1883 g_value_unset( &gvalue
);
1886 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1888 wxDataViewCellMode ret
;
1891 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1893 switch (g_value_get_enum(&gvalue
))
1896 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1897 // fall through (we have to return something)
1899 case GTK_CELL_RENDERER_MODE_INERT
:
1900 ret
= wxDATAVIEW_CELL_INERT
;
1903 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1904 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1907 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1908 ret
= wxDATAVIEW_CELL_EDITABLE
;
1912 g_value_unset( &gvalue
);
1917 void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer
*renderer
)
1919 int align
= m_alignment
;
1921 // query alignment from column ?
1925 if (GetOwner() == NULL
)
1928 align
= GetOwner()->GetAlignment();
1929 align
|= wxALIGN_CENTRE_VERTICAL
;
1932 // horizontal alignment:
1934 gfloat xalign
= 0.0;
1935 if (align
& wxALIGN_RIGHT
)
1937 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1940 GValue gvalue
= { 0, };
1941 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1942 g_value_set_float( &gvalue
, xalign
);
1943 g_object_set_property( G_OBJECT(renderer
), "xalign", &gvalue
);
1944 g_value_unset( &gvalue
);
1946 // vertical alignment:
1948 gfloat yalign
= 0.0;
1949 if (align
& wxALIGN_BOTTOM
)
1951 else if (align
& wxALIGN_CENTER_VERTICAL
)
1954 GValue gvalue2
= { 0, };
1955 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1956 g_value_set_float( &gvalue2
, yalign
);
1957 g_object_set_property( G_OBJECT(renderer
), "yalign", &gvalue2
);
1958 g_value_unset( &gvalue2
);
1961 void wxDataViewRenderer::SetAlignment( int align
)
1963 m_alignment
= align
;
1964 GtkUpdateAlignment();
1967 int wxDataViewRenderer::GetAlignment() const
1972 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode
)
1976 if ( gtk_check_version(2, 6, 0) != NULL
)
1980 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1984 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1985 // can just cast between them
1986 GValue gvalue
= { 0, };
1987 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1988 g_value_set_enum( &gvalue
, static_cast<PangoEllipsizeMode
>(mode
) );
1989 g_object_set_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1990 g_value_unset( &gvalue
);
1993 #endif // GTK 2.6/before
1996 wxEllipsizeMode
wxDataViewRenderer::GetEllipsizeMode() const
2000 if ( gtk_check_version(2, 6, 0) != NULL
)
2001 return wxELLIPSIZE_NONE
;
2004 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
2006 return wxELLIPSIZE_NONE
;
2008 GValue gvalue
= { 0, };
2009 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
2010 g_object_get_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
2012 mode
= static_cast<wxEllipsizeMode
>(g_value_get_enum( &gvalue
));
2013 g_value_unset( &gvalue
);
2017 return wxELLIPSIZE_NONE
;
2018 #endif // GTK 2.6/before
2022 wxDataViewRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
2024 wxVariant
value(str
);
2025 if (!Validate( value
))
2029 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2031 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2035 wxDataViewRenderer::GtkOnCellChanged(const wxVariant
& value
,
2036 const wxDataViewItem
& item
,
2039 wxDataViewModel
*model
= GetOwner()->GetOwner()->GetModel();
2040 model
->ChangeValue( value
, item
, col
);
2043 // ---------------------------------------------------------
2044 // wxDataViewTextRenderer
2045 // ---------------------------------------------------------
2050 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*WXUNUSED(renderer
),
2051 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
2053 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) user_data
;
2055 cell
->GtkOnTextEdited(arg1
, wxGTK_CONV_BACK_FONT(
2056 arg2
, cell
->GetOwner()->GetOwner()->GetFont()));
2064 // helper function used by wxDataViewTextRenderer and
2065 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
2066 // given text renderer and returns true if anything was done
2067 bool GtkApplyAttr(GtkCellRendererText
*renderer
, const wxDataViewItemAttr
& attr
)
2069 bool usingDefaultAttrs
= true;
2070 if (attr
.HasColour())
2072 const GdkColor
* const gcol
= attr
.GetColour().GetColor();
2074 GValue gvalue
= { 0, };
2075 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2076 g_value_set_boxed( &gvalue
, gcol
);
2077 g_object_set_property( G_OBJECT(renderer
), "foreground_gdk", &gvalue
);
2078 g_value_unset( &gvalue
);
2080 usingDefaultAttrs
= false;
2084 GValue gvalue
= { 0, };
2085 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2086 g_value_set_boolean( &gvalue
, FALSE
);
2087 g_object_set_property( G_OBJECT(renderer
), "foreground-set", &gvalue
);
2088 g_value_unset( &gvalue
);
2091 if (attr
.GetItalic())
2093 GValue gvalue
= { 0, };
2094 g_value_init( &gvalue
, PANGO_TYPE_STYLE
);
2095 g_value_set_enum( &gvalue
, PANGO_STYLE_ITALIC
);
2096 g_object_set_property( G_OBJECT(renderer
), "style", &gvalue
);
2097 g_value_unset( &gvalue
);
2099 usingDefaultAttrs
= false;
2103 GValue gvalue
= { 0, };
2104 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2105 g_value_set_boolean( &gvalue
, FALSE
);
2106 g_object_set_property( G_OBJECT(renderer
), "style-set", &gvalue
);
2107 g_value_unset( &gvalue
);
2113 GValue gvalue
= { 0, };
2114 g_value_init( &gvalue
, PANGO_TYPE_WEIGHT
);
2115 g_value_set_enum( &gvalue
, PANGO_WEIGHT_BOLD
);
2116 g_object_set_property( G_OBJECT(renderer
), "weight", &gvalue
);
2117 g_value_unset( &gvalue
);
2119 usingDefaultAttrs
= false;
2123 GValue gvalue
= { 0, };
2124 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2125 g_value_set_boolean( &gvalue
, FALSE
);
2126 g_object_set_property( G_OBJECT(renderer
), "weight-set", &gvalue
);
2127 g_value_unset( &gvalue
);
2131 if (attr
.HasBackgroundColour())
2133 wxColour colour
= attr
.GetBackgroundColour();
2134 const GdkColor
* const gcol
= colour
.GetColor();
2136 GValue gvalue
= { 0, };
2137 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2138 g_value_set_boxed( &gvalue
, gcol
);
2139 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2140 g_value_unset( &gvalue
);
2144 GValue gvalue
= { 0, };
2145 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2146 g_value_set_boolean( &gvalue
, FALSE
);
2147 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2148 g_value_unset( &gvalue
);
2152 return !usingDefaultAttrs
;
2155 } // anonymous namespace
2157 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
2159 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2161 wxDataViewRenderer( varianttype
, mode
, align
)
2163 GtkWxCellRendererText
*text_renderer
= gtk_wx_cell_renderer_text_new();
2164 text_renderer
->wx_renderer
= this;
2165 m_renderer
= (GtkCellRenderer
*) text_renderer
;
2167 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
2169 GValue gvalue
= { 0, };
2170 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2171 g_value_set_boolean( &gvalue
, true );
2172 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
2173 g_value_unset( &gvalue
);
2175 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2181 SetAlignment(align
);
2184 bool wxDataViewTextRenderer::SetTextValue(const wxString
& str
)
2186 GValue gvalue
= { 0, };
2187 g_value_init( &gvalue
, G_TYPE_STRING
);
2188 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( str
, GetOwner()->GetOwner()->GetFont() ) );
2189 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2190 g_value_unset( &gvalue
);
2195 bool wxDataViewTextRenderer::GetTextValue(wxString
& str
) const
2197 GValue gvalue
= { 0, };
2198 g_value_init( &gvalue
, G_TYPE_STRING
);
2199 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2200 str
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
), const_cast<wxDataViewTextRenderer
*>(this)->GetOwner()->GetOwner()->GetFont() );
2201 g_value_unset( &gvalue
);
2206 void wxDataViewTextRenderer::SetAlignment( int align
)
2208 wxDataViewRenderer::SetAlignment(align
);
2211 if (gtk_check_version(2,10,0))
2215 // horizontal alignment:
2216 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2217 if (align
& wxALIGN_RIGHT
)
2218 pangoAlign
= PANGO_ALIGN_RIGHT
;
2219 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2220 pangoAlign
= PANGO_ALIGN_CENTER
;
2222 GValue gvalue
= { 0, };
2223 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2224 g_value_set_enum( &gvalue
, pangoAlign
);
2225 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2226 g_value_unset( &gvalue
);
2229 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr
& attr
)
2231 return GtkApplyAttr(GtkGetTextRenderer(), attr
);
2234 GtkCellRendererText
*wxDataViewTextRenderer::GtkGetTextRenderer() const
2236 return GTK_CELL_RENDERER_TEXT(m_renderer
);
2239 // ---------------------------------------------------------
2240 // wxDataViewBitmapRenderer
2241 // ---------------------------------------------------------
2246 // set "pixbuf" property on the given renderer
2247 void SetPixbufProp(GtkCellRenderer
*renderer
, GdkPixbuf
*pixbuf
)
2249 GValue gvalue
= { 0, };
2250 g_value_init( &gvalue
, G_TYPE_OBJECT
);
2251 g_value_set_object( &gvalue
, pixbuf
);
2252 g_object_set_property( G_OBJECT(renderer
), "pixbuf", &gvalue
);
2253 g_value_unset( &gvalue
);
2256 } // anonymous namespace
2258 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
2260 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2262 wxDataViewRenderer( varianttype
, mode
, align
)
2264 m_renderer
= gtk_cell_renderer_pixbuf_new();
2267 SetAlignment(align
);
2270 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
2272 if (value
.GetType() == wxT("wxBitmap"))
2277 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2278 // object (and it will stay there and remain owned by wxBitmap)
2279 SetPixbufProp(m_renderer
, bitmap
.GetPixbuf());
2281 else if (value
.GetType() == wxT("wxIcon"))
2286 SetPixbufProp(m_renderer
, icon
.GetPixbuf());
2296 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2301 // ---------------------------------------------------------
2302 // wxDataViewToggleRenderer
2303 // ---------------------------------------------------------
2306 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2307 gchar
*path
, gpointer user_data
);
2310 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2311 gchar
*path
, gpointer user_data
)
2313 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
2316 GValue gvalue
= { 0, };
2317 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2318 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
2320 wxVariant value
= !g_value_get_boolean( &gvalue
);
2321 g_value_unset( &gvalue
);
2323 if (!cell
->Validate( value
))
2326 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
2327 wxDataViewModel
*model
= ctrl
->GetModel();
2329 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
2331 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
2333 model
->ChangeValue( value
, item
, model_col
);
2336 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
2338 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
2339 wxDataViewCellMode mode
, int align
) :
2340 wxDataViewRenderer( varianttype
, mode
, align
)
2342 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
2344 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
2346 g_signal_connect_after( m_renderer
, "toggled",
2347 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
2351 GValue gvalue
= { 0, };
2352 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2353 g_value_set_boolean( &gvalue
, false );
2354 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
2355 g_value_unset( &gvalue
);
2359 SetAlignment(align
);
2362 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
2366 GValue gvalue
= { 0, };
2367 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2368 g_value_set_boolean( &gvalue
, tmp
);
2369 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2370 g_value_unset( &gvalue
);
2375 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
2377 GValue gvalue
= { 0, };
2378 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2379 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2380 value
= g_value_get_boolean( &gvalue
) != 0;
2381 g_value_unset( &gvalue
);
2386 // ---------------------------------------------------------
2387 // wxDataViewCustomRenderer
2388 // ---------------------------------------------------------
2391 class wxDataViewCtrlDCImpl
: public wxWindowDCImpl
2394 wxDataViewCtrlDCImpl( wxDC
*owner
, wxDataViewCtrl
*window
) :
2395 wxWindowDCImpl( owner
)
2397 GtkWidget
*widget
= window
->m_treeview
;
2403 m_context
= window
->GTKGetPangoDefaultContext();
2404 m_layout
= pango_layout_new( m_context
);
2405 m_fontdesc
= pango_font_description_copy(gtk_widget_get_style(widget
)->font_desc
);
2407 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
2409 // Set m_gdkwindow later
2414 class wxDataViewCtrlDC
: public wxWindowDC
2417 wxDataViewCtrlDC( wxDataViewCtrl
*window
) :
2418 wxWindowDC( new wxDataViewCtrlDCImpl( this, window
) )
2423 // ---------------------------------------------------------
2424 // wxDataViewCustomRenderer
2425 // ---------------------------------------------------------
2427 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
2429 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
2430 wxDataViewCellMode mode
,
2433 : wxDataViewCustomRendererBase( varianttype
, mode
, align
)
2436 m_text_renderer
= NULL
;
2437 m_renderParams
= NULL
;
2445 void wxDataViewCustomRenderer::GtkInitTextRenderer()
2447 m_text_renderer
= GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2448 g_object_ref_sink(m_text_renderer
);
2450 GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer
));
2453 GtkCellRendererText
*wxDataViewCustomRenderer::GtkGetTextRenderer() const
2455 if ( !m_text_renderer
)
2457 // we create it on demand so need to do it even from a const function
2458 const_cast<wxDataViewCustomRenderer
*>(this)->GtkInitTextRenderer();
2461 return m_text_renderer
;
2464 void wxDataViewCustomRenderer::RenderText( const wxString
&text
,
2468 int WXUNUSED(state
) )
2471 GtkCellRendererText
* const textRenderer
= GtkGetTextRenderer();
2473 GValue gvalue
= { 0, };
2474 g_value_init( &gvalue
, G_TYPE_STRING
);
2475 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( text
, GetOwner()->GetOwner()->GetFont() ) );
2476 g_object_set_property( G_OBJECT(textRenderer
), "text", &gvalue
);
2477 g_value_unset( &gvalue
);
2479 GtkApplyAttr(textRenderer
, GetAttr());
2481 GdkRectangle cell_area
;
2482 wxRectToGDKRect(cell
, cell_area
);
2483 cell_area
.x
+= xoffset
;
2484 cell_area
.width
-= xoffset
;
2486 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer
),
2490 m_renderParams
->window
,
2492 m_renderParams
->widget
,
2493 m_renderParams
->background_area
,
2496 m_renderParams
->expose_area
,
2498 GtkCellRendererState(m_renderParams
->flags
));
2501 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
2503 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
2504 renderer
->cell
= this;
2506 m_renderer
= (GtkCellRenderer
*) renderer
;
2509 SetAlignment(align
);
2516 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2521 if (m_text_renderer
)
2522 g_object_unref(m_text_renderer
);
2525 wxDC
*wxDataViewCustomRenderer::GetDC()
2530 wxASSERT(m_renderParams
);
2531 cairo_t
* cr
= m_renderParams
->cr
;
2532 wxASSERT(cr
&& cairo_status(cr
) == 0);
2533 m_dc
= new wxGTKCairoDC(cr
);
2535 if (GetOwner() == NULL
)
2537 if (GetOwner()->GetOwner() == NULL
)
2539 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2546 // ---------------------------------------------------------
2547 // wxDataViewProgressRenderer
2548 // ---------------------------------------------------------
2550 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
2552 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
2553 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
2554 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
2560 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,6,0) == NULL
)
2562 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
2565 SetAlignment(align
);
2568 // We can't initialize the renderer just yet because we don't have the
2569 // pointer to the column that uses this renderer yet and so attempt to
2570 // dereference GetOwner() to get the font that is used as a source of
2571 // encoding in multibyte-to-Unicode conversion in GTKSetLabel() in
2572 // non-Unicode builds would crash. So simply remember to do it later.
2573 if ( !m_label
.empty() )
2574 m_needsToSetLabel
= true;
2576 #endif // !wxUSE_UNICODE
2582 // Use custom cell code
2583 wxDataViewCustomRenderer::Init(mode
, align
);
2587 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2591 void wxDataViewProgressRenderer::GTKSetLabel()
2593 GValue gvalue
= { 0, };
2594 g_value_init( &gvalue
, G_TYPE_STRING
);
2596 // Take care to not use GetOwner() here if the label is empty, we can be
2597 // called from ctor when GetOwner() is still NULL in this case.
2598 wxScopedCharBuffer buf
;
2599 if ( m_label
.empty() )
2600 buf
= wxScopedCharBuffer::CreateNonOwned("");
2602 buf
= wxGTK_CONV_FONT(m_label
, GetOwner()->GetOwner()->GetFont());
2604 g_value_set_string( &gvalue
, buf
);
2605 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2606 g_value_unset( &gvalue
);
2609 m_needsToSetLabel
= false;
2610 #endif // !wxUSE_UNICODE
2613 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
2616 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,6,0) == NULL
)
2619 if ( m_needsToSetLabel
)
2621 #endif // !wxUSE_UNICODE
2623 gint tmp
= (long) value
;
2624 GValue gvalue
= { 0, };
2625 g_value_init( &gvalue
, G_TYPE_INT
);
2626 g_value_set_int( &gvalue
, tmp
);
2627 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
2628 g_value_unset( &gvalue
);
2633 m_value
= (long) value
;
2635 if (m_value
< 0) m_value
= 0;
2636 if (m_value
> 100) m_value
= 100;
2642 bool wxDataViewProgressRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2647 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
2649 double pct
= (double)m_value
/ 100.0;
2651 bar
.width
= (int)(cell
.width
* pct
);
2652 dc
->SetPen( *wxTRANSPARENT_PEN
);
2653 dc
->SetBrush( *wxBLUE_BRUSH
);
2654 dc
->DrawRectangle( bar
);
2656 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
2657 dc
->SetPen( *wxBLACK_PEN
);
2658 dc
->DrawRectangle( cell
);
2663 wxSize
wxDataViewProgressRenderer::GetSize() const
2665 return wxSize(40,12);
2668 // -------------------------------------
2669 // wxDataViewChoiceRenderer
2670 // -------------------------------------
2672 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString
&choices
,
2673 wxDataViewCellMode mode
, int alignment
) :
2674 wxDataViewCustomRenderer( "string", mode
, alignment
, true )
2676 m_choices
= choices
;
2679 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,6,0) == NULL
)
2681 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_combo_new();
2683 GtkListStore
*store
= gtk_list_store_new( 1, G_TYPE_STRING
);
2684 for (size_t n
= 0; n
< m_choices
.GetCount(); n
++)
2686 gtk_list_store_insert_with_values(
2688 static_cast<const char *>(m_choices
[n
].utf8_str()), -1 );
2691 g_object_set (m_renderer
,
2697 bool editable
= (mode
& wxDATAVIEW_CELL_EDITABLE
) != 0;
2698 g_object_set (m_renderer
, "editable", editable
, NULL
);
2700 SetAlignment(alignment
);
2702 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2709 // Use custom cell code
2710 wxDataViewCustomRenderer::Init(mode
, alignment
);
2714 bool wxDataViewChoiceRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
2716 RenderText( m_data
, 0, rect
, dc
, state
);
2720 wxSize
wxDataViewChoiceRenderer::GetSize() const
2722 return wxSize(70,20);
2725 bool wxDataViewChoiceRenderer::SetValue( const wxVariant
&value
)
2729 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,6,0) == NULL
)
2731 GValue gvalue
= { 0, };
2732 g_value_init( &gvalue
, G_TYPE_STRING
);
2733 g_value_set_string(&gvalue
,
2734 wxGTK_CONV_FONT(value
.GetString(),
2735 GetOwner()->GetOwner()->GetFont()));
2736 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2737 g_value_unset( &gvalue
);
2741 m_data
= value
.GetString();
2746 bool wxDataViewChoiceRenderer::GetValue( wxVariant
&value
) const
2749 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,6,0) == NULL
)
2751 GValue gvalue
= { 0, };
2752 g_value_init( &gvalue
, G_TYPE_STRING
);
2753 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2754 wxString temp
= wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue
),
2755 GetOwner()->GetOwner()->GetFont());
2756 g_value_unset( &gvalue
);
2759 //wxPrintf( "temp %s\n", temp );
2760 // TODO: remove this code
2769 void wxDataViewChoiceRenderer::SetAlignment( int align
)
2771 wxDataViewCustomRenderer::SetAlignment(align
);
2774 if (gtk_check_version(2,10,0))
2778 // horizontal alignment:
2779 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2780 if (align
& wxALIGN_RIGHT
)
2781 pangoAlign
= PANGO_ALIGN_RIGHT
;
2782 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2783 pangoAlign
= PANGO_ALIGN_CENTER
;
2785 GValue gvalue
= { 0, };
2786 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2787 g_value_set_enum( &gvalue
, pangoAlign
);
2788 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2789 g_value_unset( &gvalue
);
2792 // ----------------------------------------------------------------------------
2793 // wxDataViewChoiceByIndexRenderer
2794 // ----------------------------------------------------------------------------
2796 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
2797 wxDataViewCellMode mode
, int alignment
) :
2798 wxDataViewChoiceRenderer( choices
, mode
, alignment
)
2802 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
2804 wxVariant
value( (long) GetChoices().Index( str
) );
2806 if (!Validate( value
))
2810 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2812 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2815 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant
&value
)
2817 wxVariant string_value
= GetChoice( value
.GetLong() );
2818 return wxDataViewChoiceRenderer::SetValue( string_value
);
2821 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant
&value
) const
2823 wxVariant string_value
;
2824 if (!wxDataViewChoiceRenderer::GetValue( string_value
))
2827 value
= (long) GetChoices().Index( string_value
.GetString() );
2831 // ---------------------------------------------------------
2832 // wxDataViewIconTextRenderer
2833 // ---------------------------------------------------------
2835 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
2837 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2839 const wxString
&varianttype
,
2840 wxDataViewCellMode mode
,
2843 : wxDataViewTextRenderer(varianttype
, mode
, align
)
2845 m_rendererIcon
= gtk_cell_renderer_pixbuf_new();
2848 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2852 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
2854 // add the icon renderer first
2855 gtk_tree_view_column_pack_start(column
, m_rendererIcon
, FALSE
/* !expand */);
2857 // add the text renderer too
2858 wxDataViewRenderer::GtkPackIntoColumn(column
);
2861 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
2865 SetTextValue(m_value
.GetText());
2867 const wxIcon
& icon
= m_value
.GetIcon();
2868 SetPixbufProp(m_rendererIcon
, icon
.IsOk() ? icon
.GetPixbuf() : NULL
);
2873 bool wxDataViewIconTextRenderer::GetValue(wxVariant
& value
) const
2876 if ( !GetTextValue(str
) )
2879 // user doesn't have any way to edit the icon so leave it unchanged
2880 value
<< wxDataViewIconText(str
, m_value
.GetIcon());
2886 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant
& value
,
2887 const wxDataViewItem
& item
,
2890 // we receive just the text part of our value as it's the only one which
2891 // can be edited, but we need the full wxDataViewIconText value for the
2893 wxVariant valueIconText
;
2894 valueIconText
<< wxDataViewIconText(value
.GetString(), m_value
.GetIcon());
2895 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText
, item
, col
);
2898 // ---------------------------------------------------------
2900 // ---------------------------------------------------------
2904 gtk_dataview_header_button_press_callback( GtkWidget
*WXUNUSED(widget
),
2905 GdkEventButton
*gdk_event
,
2906 wxDataViewColumn
*column
)
2908 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
2911 if (gdk_event
->button
== 1)
2913 gs_lastLeftClickHeader
= column
;
2915 wxDataViewCtrl
*dv
= column
->GetOwner();
2916 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2917 event
.SetDataViewColumn( column
);
2918 event
.SetModel( dv
->GetModel() );
2919 if (dv
->HandleWindowEvent( event
))
2923 if (gdk_event
->button
== 3)
2925 wxDataViewCtrl
*dv
= column
->GetOwner();
2926 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2927 event
.SetDataViewColumn( column
);
2928 event
.SetModel( dv
->GetModel() );
2929 if (dv
->HandleWindowEvent( event
))
2939 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*WXUNUSED(column
),
2940 GtkCellRenderer
*renderer
,
2941 GtkTreeModel
*model
,
2945 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2946 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2948 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2950 wxDataViewItem
item( (void*) iter
->user_data
);
2952 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2954 if (!wx_model
->IsVirtualListModel())
2957 if (wx_model
->IsContainer( item
))
2959 visible
= wx_model
->HasContainerColumns( item
) ||
2960 (cell
->GetOwner()->GetModelColumn() == 0);
2967 GValue gvalue
= { 0, };
2968 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2969 g_value_set_boolean( &gvalue
, visible
);
2970 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2971 g_value_unset( &gvalue
);
2978 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2980 if (value
.GetType() != cell
->GetVariantType())
2982 wxLogError( wxT("Wrong type, required: %s but: %s"),
2983 value
.GetType().c_str(),
2984 cell
->GetVariantType().c_str() );
2987 cell
->SetValue( value
);
2989 // deal with disabled items
2990 bool enabled
= wx_model
->IsEnabled( item
, cell
->GetOwner()->GetModelColumn() );
2992 // a) this sets the appearance to disabled grey
2993 GValue gvalue
= { 0, };
2994 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2995 g_value_set_boolean( &gvalue
, enabled
);
2996 g_object_set_property( G_OBJECT(renderer
), "sensitive", &gvalue
);
2997 g_value_unset( &gvalue
);
2999 // b) this actually disables the control/renderer
3001 cell
->SetMode( cell
->GtkGetMode() );
3003 cell
->SetMode( wxDATAVIEW_CELL_INERT
);
3006 // deal with attributes: if the renderer doesn't support them at all, we
3007 // don't even need to query the model for them
3008 if ( !cell
->GtkSupportsAttrs() )
3011 // it can support attributes so check if this item has any
3012 wxDataViewItemAttr attr
;
3013 if ( wx_model
->GetAttr( item
, cell
->GetOwner()->GetModelColumn(), attr
)
3014 || !cell
->GtkIsUsingDefaultAttrs() )
3016 bool usingDefaultAttrs
= !cell
->GtkSetAttr(attr
);
3017 cell
->GtkSetUsingDefaultAttrs(usingDefaultAttrs
);
3019 // else: no custom attributes specified and we're already using the default
3020 // ones -- nothing to do
3026 #include <wx/listimpl.cpp>
3027 WX_DEFINE_LIST(wxDataViewColumnList
)
3029 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
3030 unsigned int model_column
, int width
,
3031 wxAlignment align
, int flags
)
3032 : wxDataViewColumnBase( cell
, model_column
)
3034 Init( align
, flags
, width
);
3039 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
3040 unsigned int model_column
, int width
,
3041 wxAlignment align
, int flags
)
3042 : wxDataViewColumnBase( bitmap
, cell
, model_column
)
3044 Init( align
, flags
, width
);
3046 SetBitmap( bitmap
);
3049 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
3051 m_isConnected
= false;
3053 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
3054 m_column
= (GtkWidget
*) column
;
3057 SetAlignment( align
);
3061 // Create container for icon and label
3062 GtkWidget
*box
= gtk_hbox_new( FALSE
, 1 );
3063 gtk_widget_show( box
);
3064 // gtk_container_set_border_width((GtkContainer*)box, 2);
3065 m_image
= gtk_image_new();
3066 gtk_box_pack_start(GTK_BOX(box
), m_image
, FALSE
, FALSE
, 1);
3067 m_label
= gtk_label_new("");
3068 gtk_box_pack_end( GTK_BOX(box
), GTK_WIDGET(m_label
), FALSE
, FALSE
, 1 );
3069 gtk_tree_view_column_set_widget( column
, box
);
3071 wxDataViewRenderer
* const colRenderer
= GetRenderer();
3072 GtkCellRenderer
* const cellRenderer
= colRenderer
->GetGtkHandle();
3074 colRenderer
->GtkPackIntoColumn(column
);
3076 gtk_tree_view_column_set_cell_data_func( column
, cellRenderer
,
3077 wxGtkTreeCellDataFunc
, (gpointer
) colRenderer
, NULL
);
3080 void wxDataViewColumn::OnInternalIdle()
3085 if (gtk_widget_get_realized(GetOwner()->m_treeview
))
3087 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3088 GtkWidget
* button
= gtk_tree_view_column_get_button(column
);
3091 g_signal_connect(button
, "button_press_event",
3092 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
3094 // otherwise the event will be blocked by GTK+
3095 gtk_tree_view_column_set_clickable( column
, TRUE
);
3097 m_isConnected
= true;
3102 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
3104 wxDataViewColumnBase::SetOwner( owner
);
3106 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3108 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3111 void wxDataViewColumn::SetTitle( const wxString
&title
)
3113 wxDataViewCtrl
*ctrl
= GetOwner();
3114 gtk_label_set_text( GTK_LABEL(m_label
), ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
3115 : wxGTK_CONV_SYS(title
) );
3117 gtk_widget_hide( m_label
);
3119 gtk_widget_show( m_label
);
3122 wxString
wxDataViewColumn::GetTitle() const
3124 return wxGTK_CONV_BACK_FONT(
3125 gtk_label_get_text( GTK_LABEL(m_label
) ),
3126 GetOwner()->GetFont()
3130 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
3132 wxDataViewColumnBase::SetBitmap( bitmap
);
3136 GtkImage
*gtk_image
= GTK_IMAGE(m_image
);
3138 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
), bitmap
.GetPixbuf());
3139 gtk_widget_show( m_image
);
3143 gtk_widget_hide( m_image
);
3147 void wxDataViewColumn::SetHidden( bool hidden
)
3149 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
3152 void wxDataViewColumn::SetResizeable( bool resizable
)
3154 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizable
);
3157 void wxDataViewColumn::SetAlignment( wxAlignment align
)
3159 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3161 gfloat xalign
= 0.0;
3162 if (align
== wxALIGN_RIGHT
)
3164 if (align
== wxALIGN_CENTER_HORIZONTAL
||
3165 align
== wxALIGN_CENTER
)
3168 gtk_tree_view_column_set_alignment( column
, xalign
);
3170 if (m_renderer
&& m_renderer
->GetAlignment() == -1)
3171 m_renderer
->GtkUpdateAlignment();
3174 wxAlignment
wxDataViewColumn::GetAlignment() const
3176 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
3179 return wxALIGN_RIGHT
;
3181 return wxALIGN_CENTER_HORIZONTAL
;
3183 return wxALIGN_LEFT
;
3186 void wxDataViewColumn::SetSortable( bool sortable
)
3188 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3192 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
3196 gtk_tree_view_column_set_sort_column_id( column
, -1 );
3197 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
3198 gtk_tree_view_column_set_clickable( column
, FALSE
);
3202 bool wxDataViewColumn::IsSortable() const
3204 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3205 return gtk_tree_view_column_get_clickable( column
) != 0;
3208 bool wxDataViewColumn::IsSortKey() const
3210 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3211 return gtk_tree_view_column_get_sort_indicator( column
) != 0;
3214 bool wxDataViewColumn::IsResizeable() const
3216 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3217 return gtk_tree_view_column_get_resizable( column
) != 0;
3220 bool wxDataViewColumn::IsHidden() const
3222 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3223 return !gtk_tree_view_column_get_visible( column
);
3226 void wxDataViewColumn::SetSortOrder( bool ascending
)
3228 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3231 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
3233 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
3235 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
3238 bool wxDataViewColumn::IsSortOrderAscending() const
3240 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3242 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
3245 void wxDataViewColumn::SetMinWidth( int width
)
3247 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3250 int wxDataViewColumn::GetMinWidth() const
3252 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3255 int wxDataViewColumn::GetWidth() const
3257 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3260 void wxDataViewColumn::SetWidth( int width
)
3262 if ( width
== wxCOL_WIDTH_AUTOSIZE
)
3264 // NB: this disables user resizing
3265 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
3269 if ( width
== wxCOL_WIDTH_DEFAULT
)
3271 // TODO find a better calculation
3272 width
= wxDVC_DEFAULT_WIDTH
;
3275 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_FIXED
);
3276 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3280 void wxDataViewColumn::SetReorderable( bool reorderable
)
3282 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column
), reorderable
);
3285 bool wxDataViewColumn::IsReorderable() const
3287 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column
) ) != 0;
3290 //-----------------------------------------------------------------------------
3291 // wxGtkTreeModelNode
3292 //-----------------------------------------------------------------------------
3295 class wxGtkTreeModelChildWithPos
3303 int wxGtkTreeModelChildWithPosCmp( const void* data1
, const void* data2
, const void* user_data
)
3305 const wxGtkTreeModelChildWithPos
* child1
= (const wxGtkTreeModelChildWithPos
*) data1
;
3306 const wxGtkTreeModelChildWithPos
* child2
= (const wxGtkTreeModelChildWithPos
*) data2
;
3307 const wxDataViewCtrlInternal
*internal
= (const wxDataViewCtrlInternal
*) user_data
;
3308 int ret
= internal
->GetDataViewModel()->Compare( child1
->id
, child2
->id
,
3309 internal
->GetSortColumn(), (internal
->GetSortOrder() == GTK_SORT_DESCENDING
) );
3315 int LINKAGEMODE
wxGtkTreeModelChildPtrCmp( void*** data1
, void*** data2
)
3317 return gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(**data1
), wxDataViewItem(**data2
),
3318 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
3321 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr
);
3324 void wxGtkTreeModelNode::Resort()
3326 size_t child_count
= GetChildCount();
3327 if (child_count
== 0)
3330 size_t node_count
= GetNodesCount();
3332 if (child_count
== 1)
3334 if (node_count
== 1)
3336 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
3342 gint
*new_order
= new gint
[child_count
];
3345 // m_children has the original *void
3346 // ptrs points to these
3347 wxGtkTreeModelChildrenPtr ptrs
;
3349 for (i
= 0; i
< child_count
; i
++)
3350 ptrs
.Add( &(m_children
[i
]) );
3352 gs_internal
= m_internal
;
3353 ptrs
.Sort( &wxGtkTreeModelChildPtrCmp
);
3355 wxGtkTreeModelChildren temp
;
3356 void** base_ptr
= &(m_children
[0]);
3357 // Transfer positions to new_order array and
3359 for (i
= 0; i
< child_count
; i
++)
3361 new_order
[i
] = ptrs
[i
] - base_ptr
;
3362 temp
.Add( *ptrs
[i
] );
3365 // Transfer IDs back to m_children
3367 WX_APPEND_ARRAY( temp
, m_children
);
3372 // Build up array with IDs and original positions
3373 wxGtkTreeModelChildWithPos
* temp
= new wxGtkTreeModelChildWithPos
[child_count
];
3375 for (i
= 0; i
< child_count
; i
++)
3378 temp
[i
].id
= m_children
[i
];
3380 // Sort array keeping original positions
3381 wxQsort( temp
, child_count
, sizeof(wxGtkTreeModelChildWithPos
),
3382 &wxGtkTreeModelChildWithPosCmp
, m_internal
);
3383 // Transfer positions to new_order array and
3384 // IDs to m_children
3386 for (i
= 0; i
< child_count
; i
++)
3388 new_order
[i
] = temp
[i
].pos
;
3389 m_children
.Add( temp
[i
].id
);
3398 wxGtkTreeModelChildren temp
;
3399 WX_APPEND_ARRAY( temp
, m_children
);
3401 gs_internal
= m_internal
;
3402 m_children
.Sort( &wxGtkTreeModelChildCmp
);
3405 for (pos
= 0; pos
< child_count
; pos
++)
3407 void *id
= m_children
.Item( pos
);
3408 int old_pos
= temp
.Index( id
);
3409 new_order
[pos
] = old_pos
;
3413 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
3416 iter
.user_data
= GetItem().GetID();
3417 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
3419 gtk_tree_model_rows_reordered( gtk_tree_model
,
3420 wxGtkTreePath(m_internal
->get_path(&iter
)), &iter
, new_order
);
3422 delete [] new_order
;
3425 for (pos
= 0; pos
< node_count
; pos
++)
3427 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
3432 //-----------------------------------------------------------------------------
3433 // wxDataViewCtrlInternal
3434 //-----------------------------------------------------------------------------
3436 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
)
3439 m_wx_model
= wx_model
;
3443 m_sort_order
= GTK_SORT_ASCENDING
;
3445 m_dataview_sort_column
= NULL
;
3447 m_dragDataObject
= NULL
;
3448 m_dropDataObject
= NULL
;
3452 m_gtk_model
= wxgtk_tree_model_new();
3453 m_gtk_model
->internal
= this;
3455 m_notifier
= new wxGtkDataViewModelNotifier( wx_model
, this );
3457 wx_model
->AddNotifier( m_notifier
);
3459 // g_object_unref( gtk_model ); ???
3461 if (!m_wx_model
->IsVirtualListModel())
3464 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner
->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model
) );
3467 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3469 m_wx_model
->RemoveNotifier( m_notifier
);
3471 // remove the model from the GtkTreeView before it gets destroyed
3472 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner
->GtkGetTreeView() ), NULL
);
3474 g_object_unref( m_gtk_model
);
3476 delete m_dragDataObject
;
3477 delete m_dropDataObject
;
3480 void wxDataViewCtrlInternal::ScheduleRefresh()
3485 void wxDataViewCtrlInternal::OnInternalIdle()
3489 GtkWidget
*widget
= m_owner
->GtkGetTreeView();
3490 gtk_widget_queue_draw( widget
);
3495 void wxDataViewCtrlInternal::InitTree()
3497 wxDataViewItem item
;
3498 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
3500 BuildBranch( m_root
);
3503 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
3505 if (node
->GetChildCount() == 0)
3507 wxDataViewItemArray children
;
3508 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
3511 for (pos
= 0; pos
< count
; pos
++)
3513 wxDataViewItem child
= children
[pos
];
3515 if (m_wx_model
->IsContainer( child
))
3516 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
3518 node
->AddLeaf( child
.GetID() );
3520 // Don't send any events here
3527 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat
&format
)
3529 wxGtkString
atom_str( gdk_atom_name( format
) );
3530 m_dragSourceTargetEntryTarget
= wxCharBuffer( atom_str
);
3532 m_dragSourceTargetEntry
.target
= m_dragSourceTargetEntryTarget
.data();
3533 m_dragSourceTargetEntry
.flags
= 0;
3534 m_dragSourceTargetEntry
.info
= static_cast<guint
>(-1);
3536 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3537 GDK_BUTTON1_MASK
, &m_dragSourceTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3542 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat
&format
)
3544 wxGtkString
atom_str( gdk_atom_name( format
) );
3545 m_dropTargetTargetEntryTarget
= wxCharBuffer( atom_str
);
3547 m_dropTargetTargetEntry
.target
= m_dropTargetTargetEntryTarget
.data();
3548 m_dropTargetTargetEntry
.flags
= 0;
3549 m_dropTargetTargetEntry
.info
= static_cast<guint
>(-1);
3551 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3552 &m_dropTargetTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3557 gboolean
wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource
*WXUNUSED(drag_source
),
3560 delete m_dragDataObject
;
3561 m_dragDataObject
= NULL
;
3563 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3567 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3568 event
.SetEventObject( m_owner
);
3569 event
.SetItem( item
);
3570 event
.SetModel( m_wx_model
);
3572 gtk_widget_get_pointer(m_owner
->GtkGetTreeView(), &x
, &y
);
3573 event
.SetPosition(x
, y
);
3574 if (!m_owner
->HandleWindowEvent( event
))
3577 if (!event
.IsAllowed())
3580 wxDataObject
*obj
= event
.GetDataObject();
3584 m_dragDataObject
= obj
;
3590 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource
*WXUNUSED(drag_source
),
3591 GtkTreePath
*WXUNUSED(path
))
3596 gboolean
wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource
*WXUNUSED(drag_source
),
3597 GtkTreePath
*path
, GtkSelectionData
*selection_data
)
3599 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3603 GdkAtom target
= gtk_selection_data_get_target(selection_data
);
3604 if (!m_dragDataObject
->IsSupported(target
))
3607 size_t size
= m_dragDataObject
->GetDataSize(target
);
3611 void *buf
= malloc( size
);
3613 gboolean res
= FALSE
;
3614 if (m_dragDataObject
->GetDataHere(target
, buf
))
3618 gtk_selection_data_set(selection_data
, target
,
3619 8, (const guchar
*) buf
, size
);
3628 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3630 GtkSelectionData
*selection_data
)
3632 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3636 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
3637 event
.SetEventObject( m_owner
);
3638 event
.SetItem( item
);
3639 event
.SetModel( m_wx_model
);
3640 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3641 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3642 event
.SetDataBuffer(const_cast<guchar
*>(gtk_selection_data_get_data(selection_data
)));
3643 if (!m_owner
->HandleWindowEvent( event
))
3646 if (!event
.IsAllowed())
3653 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3655 GtkSelectionData
*selection_data
)
3657 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3661 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
3662 event
.SetEventObject( m_owner
);
3663 event
.SetItem( item
);
3664 event
.SetModel( m_wx_model
);
3665 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3666 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3667 if (!m_owner
->HandleWindowEvent( event
))
3670 if (!event
.IsAllowed())
3676 // notifications from wxDataViewModel
3678 bool wxDataViewCtrlInternal::Cleared()
3693 void wxDataViewCtrlInternal::Resort()
3695 if (!m_wx_model
->IsVirtualListModel())
3701 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3703 if (!m_wx_model
->IsVirtualListModel())
3705 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3706 wxCHECK_MSG(parent_node
, false,
3707 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3709 wxDataViewItemArray modelSiblings
;
3710 m_wx_model
->GetChildren(parent
, modelSiblings
);
3711 const int modelSiblingsSize
= modelSiblings
.size();
3713 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
3714 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
3716 const wxGtkTreeModelChildren
& nodeSiblings
= parent_node
->GetChildren();
3717 const int nodeSiblingsSize
= nodeSiblings
.size();
3721 if ( posInModel
== modelSiblingsSize
- 1 )
3723 nodePos
= nodeSiblingsSize
;
3725 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
3727 // This is the simple case when our node tree already matches the
3728 // model and only this one item is missing.
3729 nodePos
= posInModel
;
3733 // It's possible that a larger discrepancy between the model and
3734 // our realization exists. This can happen e.g. when adding a bunch
3735 // of items to the model and then calling ItemsAdded() just once
3736 // afterwards. In this case, we must find the right position by
3737 // looking at sibling items.
3739 // append to the end if we won't find a better position:
3740 nodePos
= nodeSiblingsSize
;
3742 for ( int nextItemPos
= posInModel
+ 1;
3743 nextItemPos
< modelSiblingsSize
;
3746 int nextNodePos
= parent_node
->FindChildByItem(modelSiblings
[nextItemPos
]);
3747 if ( nextNodePos
!= wxNOT_FOUND
)
3749 nodePos
= nextNodePos
;
3755 if (m_wx_model
->IsContainer( item
))
3756 parent_node
->InsertNode( new wxGtkTreeModelNode( parent_node
, item
, this ), nodePos
);
3758 parent_node
->InsertLeaf( item
.GetID(), nodePos
);
3766 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3768 if (!m_wx_model
->IsVirtualListModel())
3770 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3771 wxASSERT_MSG(parent_node
,
3772 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3774 parent_node
->DeleteChild( item
.GetID() );
3782 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
3784 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3785 event
.SetEventObject( m_owner
);
3786 event
.SetModel( m_owner
->GetModel() );
3787 event
.SetItem( item
);
3788 m_owner
->HandleWindowEvent( event
);
3793 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int view_column
)
3795 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3796 event
.SetEventObject( m_owner
);
3797 event
.SetModel( m_owner
->GetModel() );
3798 event
.SetColumn( view_column
);
3799 event
.SetDataViewColumn( GetOwner()->GetColumn(view_column
) );
3800 event
.SetItem( item
);
3801 m_owner
->HandleWindowEvent( event
);
3808 GtkTreeModelFlags
wxDataViewCtrlInternal::get_flags()
3812 if ( m_wx_model
->IsListModel() )
3813 flags
|= GTK_TREE_MODEL_LIST_ONLY
;
3815 if ( !m_wx_model
->IsVirtualListModel() )
3816 flags
|= GTK_TREE_MODEL_ITERS_PERSIST
;
3818 return GtkTreeModelFlags(flags
);
3821 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
3824 if (m_wx_model
->IsVirtualListModel())
3826 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3828 unsigned int i
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
3830 if (i
>= wx_model
->GetCount())
3833 iter
->stamp
= m_gtk_model
->stamp
;
3834 // user_data is just the index +1
3835 iter
->user_data
= wxUIntToPtr(i
+1);
3841 int depth
= gtk_tree_path_get_depth( path
);
3843 wxGtkTreeModelNode
*node
= m_root
;
3846 for (i
= 0; i
< depth
; i
++)
3848 BuildBranch( node
);
3850 gint pos
= gtk_tree_path_get_indices (path
)[i
];
3851 if (pos
< 0) return FALSE
;
3852 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
3854 void* id
= node
->GetChildren().Item( (size_t) pos
);
3858 iter
->stamp
= m_gtk_model
->stamp
;
3859 iter
->user_data
= id
;
3863 size_t count
= node
->GetNodes().GetCount();
3865 for (pos2
= 0; pos2
< count
; pos2
++)
3867 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
3868 if (child_node
->GetItem().GetID() == id
)
3880 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
3882 // When this is called from ItemDeleted(), the item is already
3883 // deleted in the model.
3885 GtkTreePath
*retval
= gtk_tree_path_new ();
3887 if (m_wx_model
->IsVirtualListModel())
3889 // iter is root, add nothing
3890 if (!iter
->user_data
)
3893 // user_data is just the index +1
3894 int i
= ( (wxUIntPtr
) iter
->user_data
) -1;
3895 gtk_tree_path_append_index (retval
, i
);
3899 void *id
= iter
->user_data
;
3901 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
3904 int pos
= node
->GetChildren().Index( id
);
3906 gtk_tree_path_prepend_index( retval
, pos
);
3908 id
= node
->GetItem().GetID();
3909 node
= node
->GetParent();
3916 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
3918 if (m_wx_model
->IsVirtualListModel())
3920 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3922 // user_data is just the index +1
3923 int n
= ( (wxUIntPtr
) iter
->user_data
) -1;
3927 iter
->user_data
= NULL
;
3931 if (n
>= (int) wx_model
->GetCount()-1)
3933 iter
->user_data
= NULL
;
3937 // user_data is just the index +1 (+2 because we need the next)
3938 iter
->user_data
= wxUIntToPtr(n
+2);
3942 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
3943 if( parent
== NULL
)
3945 iter
->user_data
= NULL
;
3949 int pos
= parent
->GetChildren().Index( iter
->user_data
);
3951 if (pos
== (int) parent
->GetChildCount()-1)
3953 iter
->user_data
= NULL
;
3957 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
3963 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
3965 if (m_wx_model
->IsVirtualListModel())
3967 // this is a list, nodes have no children
3971 iter
->stamp
= m_gtk_model
->stamp
;
3972 iter
->user_data
= (gpointer
) 1;
3980 if (m_root
->GetChildCount() == 0) return FALSE
;
3981 iter
->stamp
= m_gtk_model
->stamp
;
3982 iter
->user_data
= (gpointer
) m_root
->GetChildren().Item( 0 );
3987 wxDataViewItem item
;
3989 item
= wxDataViewItem( (void*) parent
->user_data
);
3991 if (!m_wx_model
->IsContainer( item
))
3994 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3995 wxASSERT_MSG(parent_node
,
3996 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3998 BuildBranch( parent_node
);
4000 if (parent_node
->GetChildCount() == 0)
4003 iter
->stamp
= m_gtk_model
->stamp
;
4004 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
4010 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
4012 if (m_wx_model
->IsVirtualListModel())
4014 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
4017 return (wx_model
->GetCount() > 0);
4019 // this is a list, nodes have no children
4025 return (m_root
->GetChildCount() > 0);
4027 wxDataViewItem
item( (void*) iter
->user_data
);
4029 bool is_container
= m_wx_model
->IsContainer( item
);
4034 wxGtkTreeModelNode
*node
= FindNode( iter
);
4036 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
4038 BuildBranch( node
);
4040 return (node
->GetChildCount() > 0);
4044 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
4046 if (m_wx_model
->IsVirtualListModel())
4048 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
4051 return (gint
) wx_model
->GetCount();
4058 return m_root
->GetChildCount();
4060 wxDataViewItem
item( (void*) iter
->user_data
);
4062 if (!m_wx_model
->IsContainer( item
))
4065 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
4066 wxASSERT_MSG(parent_node
,
4067 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4069 BuildBranch( parent_node
);
4071 return parent_node
->GetChildCount();
4075 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
4077 if (m_wx_model
->IsVirtualListModel())
4079 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
4087 if (n
>= (gint
) wx_model
->GetCount())
4090 iter
->stamp
= m_gtk_model
->stamp
;
4091 // user_data is just the index +1
4092 iter
->user_data
= wxUIntToPtr(n
+1);
4099 if (parent
) id
= (void*) parent
->user_data
;
4100 wxDataViewItem
item( id
);
4102 if (!m_wx_model
->IsContainer( item
))
4105 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4106 wxASSERT_MSG(parent_node
,
4107 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4109 BuildBranch( parent_node
);
4111 iter
->stamp
= m_gtk_model
->stamp
;
4112 iter
->user_data
= parent_node
->GetChildren().Item( n
);
4118 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
4120 if (m_wx_model
->IsVirtualListModel())
4126 wxGtkTreeModelNode
*node
= FindParentNode( child
);
4130 iter
->stamp
= m_gtk_model
->stamp
;
4131 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
4137 // item can be deleted already in the model
4138 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
4140 if (m_wx_model
->IsVirtualListModel())
4142 return wxPtrToUInt(item
.GetID()) - 1;
4146 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4147 wxGtkTreeModelChildren
&children
= parent_node
->GetChildren();
4149 for (j
= 0; j
< children
.GetCount(); j
++)
4151 if (children
[j
] == item
.GetID())
4159 static wxGtkTreeModelNode
*
4160 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4166 list
.DeleteContents( true );
4167 wxDataViewItem
it( item
);
4171 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4172 list
.Insert( pItem
);
4173 it
= model
->GetParent( it
);
4176 wxGtkTreeModelNode
* node
= treeNode
;
4177 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4179 if( node
&& node
->GetNodes().GetCount() != 0 )
4181 int len
= node
->GetNodes().GetCount();
4182 wxGtkTreeModelNodes
&nodes
= node
->GetNodes();
4184 for( ; j
< len
; j
++)
4186 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4205 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
4210 wxDataViewItem
item( (void*) iter
->user_data
);
4214 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4219 wxLogDebug( "Not found %p", iter->user_data );
4223 // TODO: remove this code
4229 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
4234 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4239 wxLogDebug( "Not found %p", item.GetID() );
4243 // TODO: remove this code
4249 static wxGtkTreeModelNode
*
4250 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4256 list
.DeleteContents( true );
4260 wxDataViewItem
it( model
->GetParent( item
) );
4263 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4264 list
.Insert( pItem
);
4265 it
= model
->GetParent( it
);
4268 wxGtkTreeModelNode
* node
= treeNode
;
4269 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4271 if( node
&& node
->GetNodes().GetCount() != 0 )
4273 int len
= node
->GetNodes().GetCount();
4274 wxGtkTreeModelNodes nodes
= node
->GetNodes();
4276 for( ; j
< len
; j
++)
4278 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4293 //Examine whether the node is item's parent node
4294 int len
= node
->GetChildCount();
4295 for( int i
= 0; i
< len
; i
++ )
4297 if( node
->GetChildren().Item( i
) == item
.GetID() )
4303 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
4308 wxDataViewItem
item( (void*) iter
->user_data
);
4312 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4315 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
4320 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4323 //-----------------------------------------------------------------------------
4324 // wxDataViewCtrl signal callbacks
4325 //-----------------------------------------------------------------------------
4328 wxdataview_selection_changed_callback( GtkTreeSelection
* WXUNUSED(selection
), wxDataViewCtrl
*dv
)
4330 if (!gtk_widget_get_realized(dv
->m_widget
))
4333 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, dv
->GetId() );
4334 event
.SetItem( dv
->GetSelection() );
4335 event
.SetModel( dv
->GetModel() );
4336 dv
->HandleWindowEvent( event
);
4340 wxdataview_row_activated_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreePath
*path
,
4341 GtkTreeViewColumn
*WXUNUSED(column
), wxDataViewCtrl
*dv
)
4343 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
4345 wxDataViewItem
item(dv
->GTKPathToItem(path
));
4346 event
.SetItem( item
);
4347 event
.SetModel( dv
->GetModel() );
4348 dv
->HandleWindowEvent( event
);
4352 wxdataview_test_expand_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4353 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4355 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, dv
->GetId() );
4357 wxDataViewItem
item( (void*) iter
->user_data
);;
4358 event
.SetItem( item
);
4359 event
.SetModel( dv
->GetModel() );
4360 dv
->HandleWindowEvent( event
);
4362 return !event
.IsAllowed();
4366 wxdataview_row_expanded_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4367 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4369 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
, dv
->GetId() );
4371 wxDataViewItem
item( (void*) iter
->user_data
);;
4372 event
.SetItem( item
);
4373 event
.SetModel( dv
->GetModel() );
4374 dv
->HandleWindowEvent( event
);
4378 wxdataview_test_collapse_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4379 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4381 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
, dv
->GetId() );
4383 wxDataViewItem
item( (void*) iter
->user_data
);;
4384 event
.SetItem( item
);
4385 event
.SetModel( dv
->GetModel() );
4386 dv
->HandleWindowEvent( event
);
4388 return !event
.IsAllowed();
4392 wxdataview_row_collapsed_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4393 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4395 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
, dv
->GetId() );
4397 wxDataViewItem
item( (void*) iter
->user_data
);;
4398 event
.SetItem( item
);
4399 event
.SetModel( dv
->GetModel() );
4400 dv
->HandleWindowEvent( event
);
4403 //-----------------------------------------------------------------------------
4405 //-----------------------------------------------------------------------------
4407 void wxDataViewCtrl::AddChildGTK(wxWindowGTK
* child
)
4409 GtkWidget
* treeview
= GtkGetTreeView();
4411 // Insert widget in GtkTreeView
4412 if (gtk_widget_get_realized(treeview
))
4413 gtk_widget_set_parent_window( child
->m_widget
,
4414 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
4415 gtk_widget_set_parent( child
->m_widget
, treeview
);
4419 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
4420 GtkAllocation
*WXUNUSED(gtk_alloc
),
4421 wxDataViewCtrl
*win
)
4423 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
4426 wxWindow
*child
= node
->GetData();
4429 gtk_widget_size_request( child
->m_widget
, &req
);
4431 GtkAllocation alloc
;
4432 alloc
.x
= child
->m_x
;
4433 alloc
.y
= child
->m_y
;
4434 alloc
.width
= child
->m_width
;
4435 alloc
.height
= child
->m_height
;
4436 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
4438 node
= node
->GetNext();
4443 //-----------------------------------------------------------------------------
4444 // "motion_notify_event"
4445 //-----------------------------------------------------------------------------
4448 gtk_dataview_motion_notify_callback( GtkWidget
*WXUNUSED(widget
),
4449 GdkEventMotion
*gdk_event
,
4450 wxDataViewCtrl
*dv
)
4452 if (gdk_event
->is_hint
)
4456 GdkModifierType state
;
4457 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
4463 GtkTreeViewColumn
*column
= NULL
;
4466 if (gtk_tree_view_get_path_at_pos(
4467 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4468 (int) gdk_event
->x
, (int) gdk_event
->y
,
4477 dv
->GtkGetInternal()->get_iter( &iter
, path
);
4485 //-----------------------------------------------------------------------------
4486 // "button_press_event"
4487 //-----------------------------------------------------------------------------
4490 gtk_dataview_button_press_callback( GtkWidget
*WXUNUSED(widget
),
4491 GdkEventButton
*gdk_event
,
4492 wxDataViewCtrl
*dv
)
4494 if ((gdk_event
->button
== 3) && (gdk_event
->type
== GDK_BUTTON_PRESS
))
4497 GtkTreeViewColumn
*column
= NULL
;
4500 gtk_tree_view_get_path_at_pos
4502 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4503 (int) gdk_event
->x
, (int) gdk_event
->y
,
4510 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, dv
->GetId() );
4512 event
.SetItem(dv
->GTKPathToItem(path
));
4513 event
.SetModel( dv
->GetModel() );
4514 return dv
->HandleWindowEvent( event
);
4520 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4522 wxDataViewCtrl::~wxDataViewCtrl()
4524 // Stop editing before destroying the control to remove any event handlers
4525 // which are added when editing started: if we didn't do this, the base
4526 // class dtor would assert as it checks for any leftover handlers.
4529 GtkTreeViewColumn
*col
;
4530 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4532 wxDataViewColumn
* const wxcol
= FromGTKColumn(col
);
4535 // This won't do anything if we're not editing it
4536 wxcol
->GetRenderer()->CancelEditing();
4545 void wxDataViewCtrl::Init()
4549 m_cols
.DeleteContents( true );
4551 m_uniformRowHeight
= -1;
4554 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4559 const wxValidator
& validator
,
4560 const wxString
& name
)
4562 if (!PreCreation( parent
, pos
, size
) ||
4563 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
4565 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4569 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
4570 g_object_ref(m_widget
);
4572 GTKScrolledWindowSetBorder(m_widget
, style
);
4574 m_treeview
= gtk_tree_view_new();
4575 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
4577 m_focusWidget
= GTK_WIDGET(m_treeview
);
4579 g_signal_connect (m_treeview
, "size_allocate",
4580 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
4584 if (!gtk_check_version(2,6,0))
4587 bool fixed
= (style
& wxDV_VARIABLE_LINE_HEIGHT
) == 0;
4588 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4592 if (style
& wxDV_MULTIPLE
)
4594 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4595 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
4598 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
4602 if (!gtk_check_version(2,10,0))
4605 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
4607 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
4608 (style
& wxDV_VERT_RULES
) != 0)
4609 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
4610 else if (style
& wxDV_VERT_RULES
)
4611 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
4612 else if (style
& wxDV_HORIZ_RULES
)
4613 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
4615 if (grid
!= GTK_TREE_VIEW_GRID_LINES_NONE
)
4616 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
4620 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_ROW_LINES
) != 0 );
4622 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
4623 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
4624 gtk_widget_show (m_treeview
);
4626 m_parent
->DoAddChild( this );
4630 GtkEnableSelectionEvents();
4632 g_signal_connect_after (m_treeview
, "row-activated",
4633 G_CALLBACK (wxdataview_row_activated_callback
), this);
4635 g_signal_connect (m_treeview
, "test-collapse-row",
4636 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
4638 g_signal_connect_after (m_treeview
, "row-collapsed",
4639 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
4641 g_signal_connect (m_treeview
, "test-expand-row",
4642 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
4644 g_signal_connect_after (m_treeview
, "row-expanded",
4645 G_CALLBACK (wxdataview_row_expanded_callback
), this);
4647 g_signal_connect (m_treeview
, "motion_notify_event",
4648 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
4650 g_signal_connect (m_treeview
, "button_press_event",
4651 G_CALLBACK (gtk_dataview_button_press_callback
), this);
4656 wxDataViewItem
wxDataViewCtrl::GTKPathToItem(GtkTreePath
*path
) const
4659 return wxDataViewItem(path
&& m_internal
->get_iter(&iter
, path
)
4664 void wxDataViewCtrl::OnInternalIdle()
4666 wxWindow::OnInternalIdle();
4668 m_internal
->OnInternalIdle();
4670 unsigned int cols
= GetColumnCount();
4672 for (i
= 0; i
< cols
; i
++)
4674 wxDataViewColumn
*col
= GetColumn( i
);
4675 col
->OnInternalIdle();
4678 if (m_ensureVisibleDefered
.IsOk())
4680 ExpandAncestors(m_ensureVisibleDefered
);
4682 iter
.user_data
= (gpointer
) m_ensureVisibleDefered
.GetID();
4683 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4684 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
4685 m_ensureVisibleDefered
= wxDataViewItem(0);
4689 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4691 wxDELETE(m_internal
);
4693 if (!wxDataViewCtrlBase::AssociateModel( model
))
4698 if (!gtk_check_version(2,6,0))
4701 bool fixed
= (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
) == 0) || (model
->IsVirtualListModel()));
4702 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4706 m_internal
= new wxDataViewCtrlInternal( this, model
);
4711 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4713 return m_internal
->EnableDragSource( format
);
4716 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4718 return m_internal
->EnableDropTarget( format
);
4721 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4723 if (!wxDataViewCtrlBase::AppendColumn(col
))
4726 m_cols
.Append( col
);
4730 if (!gtk_check_version(2,6,0))
4733 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4734 GTK_TREE_VIEW_COLUMN_FIXED
)
4735 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4739 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
4740 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4745 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4747 if (!wxDataViewCtrlBase::PrependColumn(col
))
4750 m_cols
.Insert( col
);
4754 if (!gtk_check_version(2,6,0))
4757 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4758 GTK_TREE_VIEW_COLUMN_FIXED
)
4759 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4763 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4764 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
4769 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4771 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4774 m_cols
.Insert( pos
, col
);
4778 if (!gtk_check_version(2,6,0))
4781 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4782 GTK_TREE_VIEW_COLUMN_FIXED
)
4783 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4787 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4788 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), pos
);
4793 unsigned int wxDataViewCtrl::GetColumnCount() const
4795 return m_cols
.GetCount();
4798 wxDataViewColumn
* wxDataViewCtrl::FromGTKColumn(GtkTreeViewColumn
*gtk_col
) const
4803 wxDataViewColumnList::const_iterator iter
;
4804 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4806 wxDataViewColumn
*col
= *iter
;
4807 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
4813 wxFAIL_MSG( "No matching column?" );
4818 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
4820 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
4822 return FromGTKColumn(gtk_col
);
4825 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4827 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4828 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
4830 m_cols
.DeleteObject( column
);
4835 bool wxDataViewCtrl::ClearColumns()
4837 wxDataViewColumnList::iterator iter
;
4838 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4840 wxDataViewColumn
*col
= *iter
;
4841 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4842 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4850 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4852 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4854 wxGtkList
list(gtk_tree_view_get_columns(GTK_TREE_VIEW(m_treeview
)));
4856 return g_list_index( list
, (gconstpointer
) gtk_column
);
4859 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4861 return m_internal
->GetDataViewSortColumn();
4864 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4867 iter
.user_data
= item
.GetID();
4868 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4869 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
4872 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4875 iter
.user_data
= item
.GetID();
4876 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4877 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
4880 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4883 iter
.user_data
= item
.GetID();
4884 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4885 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview
), path
) != 0;
4888 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4890 // The tree doesn't have any current item if it hadn't been created yet but
4891 // it's arguably not an error to call this function in this case so just
4892 // return an invalid item without asserting.
4894 return wxDataViewItem();
4897 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), path
.ByRef(), NULL
);
4899 return GTKPathToItem(path
);
4902 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4904 wxCHECK_RET( m_treeview
,
4905 "Current item can't be set before creating the control." );
4907 // We need to make sure the model knows about this item or the path would
4908 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4909 ExpandAncestors(item
);
4911 // We also need to preserve the existing selection from changing.
4912 // Unfortunately the only way to do it seems to use our own selection
4913 // function and forbid any selection changes during set cursor call.
4914 wxGtkTreeSelectionLock
4915 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4917 // Do move the cursor now.
4919 iter
.user_data
= item
.GetID();
4920 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4922 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, NULL
, FALSE
);
4925 wxDataViewColumn
*wxDataViewCtrl::GetCurrentColumn() const
4927 // The tree doesn't have any current item if it hadn't been created yet but
4928 // it's arguably not an error to call this function in this case so just
4929 // return NULL without asserting.
4933 GtkTreeViewColumn
*col
;
4934 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4935 return FromGTKColumn(col
);
4938 void wxDataViewCtrl::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
4940 wxCHECK_RET( m_treeview
,
4941 "Current item can't be set before creating the control." );
4942 wxCHECK_RET( item
.IsOk(), "invalid item" );
4943 wxCHECK_RET( column
, "no column provided" );
4945 // We need to make sure the model knows about this item or the path would
4946 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4947 ExpandAncestors(item
);
4949 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4951 // We also need to preserve the existing selection from changing.
4952 // Unfortunately the only way to do it seems to use our own selection
4953 // function and forbid any selection changes during set cursor call.
4954 wxGtkTreeSelectionLock
4955 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4957 // Do move the cursor now.
4959 iter
.user_data
= item
.GetID();
4960 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4962 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, gcolumn
, TRUE
);
4965 int wxDataViewCtrl::GetSelectedItemsCount() const
4967 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4969 return gtk_tree_selection_count_selected_rows(selection
);
4972 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4976 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4977 if (HasFlag(wxDV_MULTIPLE
))
4979 GtkTreeModel
*model
;
4980 wxGtkTreePathList
list(gtk_tree_selection_get_selected_rows(selection
, &model
));
4982 for ( GList
* current
= list
; current
; current
= g_list_next(current
) )
4984 GtkTreePath
*path
= (GtkTreePath
*) current
->data
;
4986 sel
.Add(GTKPathToItem(path
));
4992 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
4994 sel
.Add( wxDataViewItem(iter
.user_data
) );
5001 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
5003 GtkDisableSelectionEvents();
5005 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5007 gtk_tree_selection_unselect_all( selection
);
5009 wxDataViewItem last_parent
;
5012 for (i
= 0; i
< sel
.GetCount(); i
++)
5014 wxDataViewItem item
= sel
[i
];
5015 wxDataViewItem parent
= GetModel()->GetParent( item
);
5018 if (parent
!= last_parent
)
5019 ExpandAncestors(item
);
5021 last_parent
= parent
;
5024 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
5025 iter
.user_data
= (gpointer
) item
.GetID();
5026 gtk_tree_selection_select_iter( selection
, &iter
);
5029 GtkEnableSelectionEvents();
5032 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
5034 ExpandAncestors(item
);
5036 GtkDisableSelectionEvents();
5038 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5041 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
5042 iter
.user_data
= (gpointer
) item
.GetID();
5043 gtk_tree_selection_select_iter( selection
, &iter
);
5045 GtkEnableSelectionEvents();
5048 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
5050 GtkDisableSelectionEvents();
5052 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5055 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
5056 iter
.user_data
= (gpointer
) item
.GetID();
5057 gtk_tree_selection_unselect_iter( selection
, &iter
);
5059 GtkEnableSelectionEvents();
5062 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
5064 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5067 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
5068 iter
.user_data
= (gpointer
) item
.GetID();
5070 return gtk_tree_selection_iter_is_selected( selection
, &iter
) != 0;
5073 void wxDataViewCtrl::SelectAll()
5075 GtkDisableSelectionEvents();
5077 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5079 gtk_tree_selection_select_all( selection
);
5081 GtkEnableSelectionEvents();
5084 void wxDataViewCtrl::UnselectAll()
5086 GtkDisableSelectionEvents();
5088 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5090 gtk_tree_selection_unselect_all( selection
);
5092 GtkEnableSelectionEvents();
5095 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem
& item
,
5096 const wxDataViewColumn
*WXUNUSED(column
))
5098 m_ensureVisibleDefered
= item
;
5099 ExpandAncestors(item
);
5102 iter
.user_data
= (gpointer
) item
.GetID();
5103 wxGtkTreePath
path(m_internal
->get_path( &iter
));
5104 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
5107 void wxDataViewCtrl::HitTest(const wxPoint
& point
,
5108 wxDataViewItem
& item
,
5109 wxDataViewColumn
*& column
) const
5111 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
5112 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
5113 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
5114 // So we have to use both of them.
5115 item
= wxDataViewItem(0);
5117 wxGtkTreePath path
, pathScratch
;
5118 GtkTreeViewColumn
* GtkColumn
= NULL
;
5119 GtkTreeViewDropPosition pos
= GTK_TREE_VIEW_DROP_INTO_OR_AFTER
;
5123 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5124 wxDataViewCtrl
* self
= const_cast<wxDataViewCtrl
*>(this); // ugly workaround, self is NOT const
5125 GtkTreeView
* treeView
= GTK_TREE_VIEW(self
->GtkGetTreeView());
5127 // is there possibly a better suited function to get the column?
5128 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5130 (int) point
.x
, (int) point
.y
,
5131 pathScratch
.ByRef(),
5132 &GtkColumn
, // here we get the GtkColumn
5136 if ( GtkColumn
!= NULL
)
5138 // we got GTK column
5139 // the right call now which takes the header into account
5140 gtk_tree_view_get_dest_row_at_pos( treeView
, (int) point
.x
, (int) point
.y
, path
.ByRef(), &pos
);
5143 item
= wxDataViewItem(GTKPathToItem(path
));
5144 // else we got a GTK column but the position is not over an item, e.g. below last item
5145 for ( unsigned int i
=0, cols
=GetColumnCount(); i
<cols
; ++i
) // search the wx column
5147 wxDataViewColumn
* col
= GetColumn(i
);
5148 if ( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == GtkColumn
)
5150 column
= col
; // here we get the wx column
5155 // else no column and thus no item, both null
5159 wxDataViewCtrl::GetItemRect(const wxDataViewItem
& WXUNUSED(item
),
5160 const wxDataViewColumn
*WXUNUSED(column
)) const
5165 bool wxDataViewCtrl::SetRowHeight(int rowHeight
)
5167 m_uniformRowHeight
= rowHeight
;
5171 void wxDataViewCtrl::DoSetExpanderColumn()
5173 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
5174 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5177 void wxDataViewCtrl::DoSetIndent()
5181 void wxDataViewCtrl::GtkDisableSelectionEvents()
5183 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5184 g_signal_handlers_disconnect_by_func( selection
,
5185 (gpointer
) (wxdataview_selection_changed_callback
), this);
5188 void wxDataViewCtrl::GtkEnableSelectionEvents()
5190 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5191 g_signal_connect_after (selection
, "changed",
5192 G_CALLBACK (wxdataview_selection_changed_callback
), this);
5195 // ----------------------------------------------------------------------------
5196 // visual attributes stuff
5197 // ----------------------------------------------------------------------------
5201 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
5203 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
5206 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
5208 wxDataViewCtrlBase::DoApplyWidgetStyle(style
);
5209 GTKApplyStyle(m_treeview
, style
);
5212 #endif // !wxUSE_GENERICDATAVIEWCTRL
5214 #endif // wxUSE_DATAVIEWCTRL