1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataview.cpp
3 // Purpose: wxDataViewCtrl GTK+2 implementation
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
12 #if wxUSE_DATAVIEWCTRL
14 #include "wx/dataview.h"
16 #ifndef wxUSE_GENERICDATAVIEWCTRL
20 #include "wx/dcclient.h"
22 #include "wx/settings.h"
26 #include "wx/stockitem.h"
27 #include "wx/popupwin.h"
28 #include "wx/listimpl.cpp"
30 #include "wx/gtk/dc.h"
32 #include "wx/gtk/dcclient.h"
36 #include "wx/gtk/private.h"
37 #include "wx/gtk/private/event.h"
38 #include "wx/gtk/private/gdkconv.h"
39 #include "wx/gtk/private/gtk2-compat.h"
40 #include "wx/gtk/private/list.h"
41 using namespace wxGTKImpl
;
43 class wxGtkDataViewModelNotifier
;
46 #define wxConstGdkRect const GdkRectangle
48 #define wxConstGdkRect GdkRectangle
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 static wxDataViewCtrlInternal
*gs_internal
= NULL
;
56 class wxGtkTreeModelNode
;
59 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
62 // ----------------------------------------------------------------------------
63 // wxGtkTreePath: self-destroying GtkTreePath
64 // ----------------------------------------------------------------------------
66 // Usually this object is initialized with the associated GtkTreePath
67 // immediately when it's constructed but it can also be changed later either by
68 // using Assign() or by getting the pointer to the internally stored pointer
69 // value using ByRef(). The latter should be avoided but is very convenient
70 // when using GTK functions with GtkTreePath output parameters.
74 // Ctor takes ownership of the given path and will free it if non-NULL.
75 wxGtkTreePath(GtkTreePath
*path
= NULL
) : m_path(path
) { }
77 // Creates a tree path for the given string path.
78 wxGtkTreePath(const gchar
*strpath
)
79 : m_path(gtk_tree_path_new_from_string(strpath
))
83 // Set the stored pointer if not done by ctor.
84 void Assign(GtkTreePath
*path
)
86 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
91 // Return the pointer to the internally stored pointer. This should only be
92 // used to initialize the object by passing it to some GTK function.
95 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
101 operator GtkTreePath
*() const { return m_path
; }
103 ~wxGtkTreePath() { if ( m_path
) gtk_tree_path_free(m_path
); }
108 wxDECLARE_NO_COPY_CLASS(wxGtkTreePath
);
111 // ----------------------------------------------------------------------------
112 // wxGtkTreePathList: self-destroying list of GtkTreePath objects.
113 // ----------------------------------------------------------------------------
115 class wxGtkTreePathList
: public wxGtkList
118 // Ctor takes ownership of the list.
119 explicit wxGtkTreePathList(GList
* list
)
126 // Delete the list contents, wxGtkList will delete the list itself.
127 g_list_foreach(m_list
, (GFunc
)gtk_tree_path_free
, NULL
);
131 // ----------------------------------------------------------------------------
132 // wxGtkTreeSelectionLock: prevent selection from changing during the
133 // lifetime of this object
134 // ----------------------------------------------------------------------------
136 // Implementation note: it could be expected that setting the selection
137 // function in this class ctor and resetting it back to the old value in its
138 // dtor would work. However currently gtk_tree_selection_get_select_function()
139 // can't be passed NULL (see https://bugzilla.gnome.org/show_bug.cgi?id=626276)
140 // so we can't do this. Instead, we always use the selection function (which
141 // imposes extra overhead, albeit minimal one, on all selection operations) and
142 // just set/reset the flag telling it whether it should allow or forbid the
145 // Also notice that currently only a single object of this class may exist at
146 // any given moment. It's just simpler like this and we don't need anything
151 gboolean
wxdataview_selection_func(GtkTreeSelection
* WXUNUSED(selection
),
152 GtkTreeModel
* WXUNUSED(model
),
153 GtkTreePath
* WXUNUSED(path
),
154 gboolean
WXUNUSED(path_currently_selected
),
161 class wxGtkTreeSelectionLock
164 wxGtkTreeSelectionLock(GtkTreeSelection
*selection
)
165 : m_selection(selection
)
167 wxASSERT_MSG( !ms_instance
, "this class is not reentrant currently" );
171 CheckCurrentSelectionFunc(NULL
);
173 // Pass some non-NULL pointer as "data" for the callback, it doesn't
174 // matter what it is as long as it's non-NULL.
175 gtk_tree_selection_set_select_function(selection
,
176 wxdataview_selection_func
,
181 ~wxGtkTreeSelectionLock()
183 CheckCurrentSelectionFunc(wxdataview_selection_func
);
185 gtk_tree_selection_set_select_function(m_selection
,
186 wxdataview_selection_func
,
194 void CheckCurrentSelectionFunc(GtkTreeSelectionFunc func
)
196 // We can only use gtk_tree_selection_get_select_function() with 2.14+
197 // so check for its availability both during compile- and run-time.
198 #if GTK_CHECK_VERSION(2, 14, 0)
200 if ( gtk_check_version(2, 14, 0) != NULL
)
204 // If this assert is triggered, it means the code elsewhere has called
205 // gtk_tree_selection_set_select_function() but currently doing this
206 // breaks this class so the code here needs to be changed.
209 gtk_tree_selection_get_select_function(m_selection
) == func
,
210 "selection function has changed unexpectedly, review this code!"
217 static wxGtkTreeSelectionLock
*ms_instance
;
219 GtkTreeSelection
* const m_selection
;
221 wxDECLARE_NO_COPY_CLASS(wxGtkTreeSelectionLock
);
224 wxGtkTreeSelectionLock
*wxGtkTreeSelectionLock::ms_instance
= NULL
;
226 //-----------------------------------------------------------------------------
227 // wxDataViewCtrlInternal
228 //-----------------------------------------------------------------------------
230 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
231 WX_DEFINE_LIST(ItemList
)
233 class wxDataViewCtrlInternal
236 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
);
237 ~wxDataViewCtrlInternal();
240 GtkTreeModelFlags
get_flags();
241 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
242 GtkTreePath
*get_path( GtkTreeIter
*iter
);
243 gboolean
iter_next( GtkTreeIter
*iter
);
244 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
245 gboolean
iter_has_child( GtkTreeIter
*iter
);
246 gint
iter_n_children( GtkTreeIter
*iter
);
247 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
248 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
252 bool EnableDragSource( const wxDataFormat
&format
);
253 bool EnableDropTarget( const wxDataFormat
&format
);
255 gboolean
row_draggable( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
);
256 gboolean
drag_data_delete( GtkTreeDragSource
*drag_source
, GtkTreePath
* path
);
257 gboolean
drag_data_get( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
,
258 GtkSelectionData
*selection_data
);
259 gboolean
drag_data_received( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest
,
260 GtkSelectionData
*selection_data
);
261 gboolean
row_drop_possible( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest_path
,
262 GtkSelectionData
*selection_data
);
264 // notifactions from wxDataViewModel
265 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
266 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
267 bool ItemChanged( const wxDataViewItem
&item
);
268 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
275 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
276 GtkSortType
GetSortOrder() const { return m_sort_order
; }
278 void SetSortColumn( int column
) { m_sort_column
= column
; }
279 int GetSortColumn() const { return m_sort_column
; }
281 void SetDataViewSortColumn( wxDataViewColumn
*column
) { m_dataview_sort_column
= column
; }
282 wxDataViewColumn
*GetDataViewSortColumn() { return m_dataview_sort_column
; }
284 bool IsSorted() const { return m_sort_column
>= 0; }
286 // Should we be sorted either because we have a configured sort column or
287 // because we have a default sort order?
288 bool ShouldBeSorted() const
290 return IsSorted() || GetDataViewModel()->HasDefaultCompare();
295 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
296 const wxDataViewModel
* GetDataViewModel() const { return m_wx_model
; }
297 wxDataViewCtrl
* GetOwner() { return m_owner
; }
298 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
300 // item can be deleted already in the model
301 int GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
303 void OnInternalIdle();
307 void ScheduleRefresh();
309 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
310 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
311 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
312 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
313 void BuildBranch( wxGtkTreeModelNode
*branch
);
316 wxGtkTreeModelNode
*m_root
;
317 wxDataViewModel
*m_wx_model
;
318 GtkWxTreeModel
*m_gtk_model
;
319 wxDataViewCtrl
*m_owner
;
320 GtkSortType m_sort_order
;
321 wxDataViewColumn
*m_dataview_sort_column
;
324 GtkTargetEntry m_dragSourceTargetEntry
;
325 wxCharBuffer m_dragSourceTargetEntryTarget
;
326 wxDataObject
*m_dragDataObject
;
328 GtkTargetEntry m_dropTargetTargetEntry
;
329 wxCharBuffer m_dropTargetTargetEntryTarget
;
330 wxDataObject
*m_dropDataObject
;
332 wxGtkDataViewModelNotifier
*m_notifier
;
338 //-----------------------------------------------------------------------------
339 // wxGtkTreeModelNode
340 //-----------------------------------------------------------------------------
343 int LINKAGEMODE
wxGtkTreeModelChildCmp( void** id1
, void** id2
)
345 int ret
= gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(*id1
), wxDataViewItem(*id2
),
346 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
351 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
352 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren
);
354 class wxGtkTreeModelNode
357 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
358 wxDataViewCtrlInternal
*internal
)
362 m_internal
= internal
;
365 ~wxGtkTreeModelNode()
367 size_t count
= m_nodes
.GetCount();
369 for (i
= 0; i
< count
; i
++)
371 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
376 void AddNode( wxGtkTreeModelNode
* child
)
378 m_nodes
.Add( child
);
380 void *id
= child
->GetItem().GetID();
382 m_children
.Add( id
);
384 if (m_internal
->ShouldBeSorted())
386 gs_internal
= m_internal
;
387 m_children
.Sort( &wxGtkTreeModelChildCmp
);
391 void InsertNode( wxGtkTreeModelNode
* child
, unsigned pos
)
393 if (m_internal
->ShouldBeSorted())
399 void *id
= child
->GetItem().GetID();
401 // Insert into m_nodes so that the order of nodes in m_nodes is the
402 // same as the order of their corresponding IDs in m_children:
403 const unsigned int count
= m_nodes
.GetCount();
404 bool inserted
= false;
405 for (unsigned i
= 0; i
< count
; i
++)
407 wxGtkTreeModelNode
*node
= m_nodes
[i
];
408 int posInChildren
= m_children
.Index(node
->GetItem().GetID());
409 if ( (unsigned)posInChildren
>= pos
)
411 m_nodes
.Insert(child
, i
);
419 m_children
.Insert( id
, pos
);
422 void AddLeaf( void* id
)
424 InsertLeaf(id
, m_children
.size());
427 void InsertLeaf( void* id
, unsigned pos
)
429 m_children
.Insert( id
, pos
);
431 if (m_internal
->ShouldBeSorted())
433 gs_internal
= m_internal
;
434 m_children
.Sort( &wxGtkTreeModelChildCmp
);
438 void DeleteChild( void* id
)
440 m_children
.Remove( id
);
442 unsigned int count
= m_nodes
.GetCount();
444 for (pos
= 0; pos
< count
; pos
++)
446 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
447 if (node
->GetItem().GetID() == id
)
449 m_nodes
.RemoveAt( pos
);
456 // returns position of child node for given item in children list or wxNOT_FOUND
457 int FindChildByItem(const wxDataViewItem
& item
) const
459 const void* itemId
= item
.GetID();
460 const wxGtkTreeModelChildren
& nodes
= m_children
;
461 const int len
= nodes
.size();
462 for ( int i
= 0; i
< len
; i
++ )
464 if ( nodes
[i
] == itemId
)
470 wxGtkTreeModelNode
* GetParent()
472 wxGtkTreeModelNodes
&GetNodes()
474 wxGtkTreeModelChildren
&GetChildren()
475 { return m_children
; }
477 unsigned int GetChildCount() const { return m_children
.GetCount(); }
478 unsigned int GetNodesCount() const { return m_nodes
.GetCount(); }
480 wxDataViewItem
&GetItem() { return m_item
; }
481 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
486 wxGtkTreeModelNode
*m_parent
;
487 wxGtkTreeModelNodes m_nodes
;
488 wxGtkTreeModelChildren m_children
;
489 wxDataViewItem m_item
;
490 wxDataViewCtrlInternal
*m_internal
;
494 //-----------------------------------------------------------------------------
496 //-----------------------------------------------------------------------------
498 extern bool g_blockEventsOnDrag
;
500 //-----------------------------------------------------------------------------
501 // define new GTK+ class wxGtkTreeModel
502 //-----------------------------------------------------------------------------
506 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
507 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
508 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
509 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
511 GType
gtk_wx_tree_model_get_type (void);
513 struct _GtkWxTreeModel
519 wxDataViewCtrlInternal
*internal
;
522 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
523 static void wxgtk_tree_model_init (GTypeInstance
* instance
, void*);
525 static void wxgtk_tree_model_tree_model_init (void* g_iface
, void*);
526 static void wxgtk_tree_model_sortable_init (void* g_iface
, void*);
527 static void wxgtk_tree_model_drag_source_init(void* g_iface
, void*);
528 static void wxgtk_tree_model_drag_dest_init (void* g_iface
, void*);
530 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
531 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
532 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
534 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
537 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
539 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
543 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
545 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
547 GtkTreeIter
*parent
);
548 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
550 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
552 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
556 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
561 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
562 gint
*sort_column_id
,
564 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
567 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
569 GtkTreeIterCompareFunc func
,
571 GDestroyNotify destroy
);
572 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
573 GtkTreeIterCompareFunc func
,
575 GDestroyNotify destroy
);
576 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
579 static gboolean
wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
581 static gboolean
wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
583 static gboolean
wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
585 GtkSelectionData
*selection_data
);
586 static gboolean
wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
588 GtkSelectionData
*selection_data
);
589 static gboolean
wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
590 GtkTreePath
*dest_path
,
591 GtkSelectionData
*selection_data
);
594 gtk_wx_tree_model_get_type (void)
596 static GType tree_model_type
= 0;
598 if (!tree_model_type
)
600 const GTypeInfo tree_model_info
=
602 sizeof (GObjectClass
),
603 NULL
, /* base_init */
604 NULL
, /* base_finalize */
606 NULL
, /* class_finalize */
607 NULL
, /* class_data */
608 sizeof (GtkWxTreeModel
),
610 wxgtk_tree_model_init
,
613 static const GInterfaceInfo tree_model_iface_info
=
615 wxgtk_tree_model_tree_model_init
,
620 static const GInterfaceInfo sortable_iface_info
=
622 wxgtk_tree_model_sortable_init
,
627 static const GInterfaceInfo drag_source_iface_info
=
629 wxgtk_tree_model_drag_source_init
,
634 static const GInterfaceInfo drag_dest_iface_info
=
636 wxgtk_tree_model_drag_dest_init
,
641 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
642 &tree_model_info
, (GTypeFlags
)0 );
644 g_type_add_interface_static (tree_model_type
,
646 &tree_model_iface_info
);
647 g_type_add_interface_static (tree_model_type
,
648 GTK_TYPE_TREE_SORTABLE
,
649 &sortable_iface_info
);
650 g_type_add_interface_static (tree_model_type
,
651 GTK_TYPE_TREE_DRAG_DEST
,
652 &drag_dest_iface_info
);
653 g_type_add_interface_static (tree_model_type
,
654 GTK_TYPE_TREE_DRAG_SOURCE
,
655 &drag_source_iface_info
);
658 return tree_model_type
;
661 static GtkWxTreeModel
*
662 wxgtk_tree_model_new(void)
664 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
669 wxgtk_tree_model_tree_model_init(void* g_iface
, void*)
671 GtkTreeModelIface
* iface
= static_cast<GtkTreeModelIface
*>(g_iface
);
672 iface
->get_flags
= wxgtk_tree_model_get_flags
;
673 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
674 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
675 iface
->get_iter
= wxgtk_tree_model_get_iter
;
676 iface
->get_path
= wxgtk_tree_model_get_path
;
677 iface
->get_value
= wxgtk_tree_model_get_value
;
678 iface
->iter_next
= wxgtk_tree_model_iter_next
;
679 iface
->iter_children
= wxgtk_tree_model_iter_children
;
680 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
681 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
682 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
683 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
687 wxgtk_tree_model_sortable_init(void* g_iface
, void*)
689 GtkTreeSortableIface
* iface
= static_cast<GtkTreeSortableIface
*>(g_iface
);
690 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
691 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
692 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
693 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
694 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
698 wxgtk_tree_model_drag_source_init(void* g_iface
, void*)
700 GtkTreeDragSourceIface
* iface
= static_cast<GtkTreeDragSourceIface
*>(g_iface
);
701 iface
->row_draggable
= wxgtk_tree_model_row_draggable
;
702 iface
->drag_data_delete
= wxgtk_tree_model_drag_data_delete
;
703 iface
->drag_data_get
= wxgtk_tree_model_drag_data_get
;
707 wxgtk_tree_model_drag_dest_init(void* g_iface
, void*)
709 GtkTreeDragDestIface
* iface
= static_cast<GtkTreeDragDestIface
*>(g_iface
);
710 iface
->drag_data_received
= wxgtk_tree_model_drag_data_received
;
711 iface
->row_drop_possible
= wxgtk_tree_model_row_drop_possible
;
715 wxgtk_tree_model_init(GTypeInstance
* instance
, void*)
717 GtkWxTreeModel
* tree_model
= GTK_WX_TREE_MODEL(instance
);
718 tree_model
->internal
= NULL
;
719 tree_model
->stamp
= g_random_int();
724 //-----------------------------------------------------------------------------
725 // implement callbacks from wxGtkTreeModel class by letting
726 // them call the methods of wxWidgets' wxDataViewModel
727 //-----------------------------------------------------------------------------
729 static GtkTreeModelFlags
730 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
732 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
733 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), (GtkTreeModelFlags
)0 );
735 return wxtree_model
->internal
->get_flags();
739 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
741 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
742 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
744 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
748 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
751 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
752 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
754 GType gtype
= G_TYPE_INVALID
;
756 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
758 if (wxtype
== wxT("string"))
759 gtype
= G_TYPE_STRING
;
762 gtype
= G_TYPE_POINTER
;
763 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
770 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
774 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
775 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
776 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
778 return wxtree_model
->internal
->get_iter( iter
, path
);
782 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
785 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
786 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
787 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
789 return wxtree_model
->internal
->get_path( iter
);
793 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
798 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
799 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
801 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
802 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
803 if (mtype
== wxT("string"))
806 g_value_init( value
, G_TYPE_STRING
);
807 wxDataViewItem
item( (void*) iter
->user_data
);
808 model
->GetValue( variant
, item
, (unsigned int) column
);
810 g_value_set_string( value
, variant
.GetString().utf8_str() );
814 wxFAIL_MSG( wxT("non-string columns not supported yet") );
819 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
822 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
824 // This happens when clearing the view by calling .._set_model( NULL );
825 if (iter
->stamp
== 0) return FALSE
;
827 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
828 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
830 return wxtree_model
->internal
->iter_next( iter
);
834 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
838 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
839 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
842 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
845 return wxtree_model
->internal
->iter_children( iter
, parent
);
849 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
852 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
853 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
854 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
856 return wxtree_model
->internal
->iter_has_child( iter
);
860 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
863 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
864 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
865 g_return_val_if_fail ( !iter
|| wxtree_model
->stamp
== iter
->stamp
, 0);
867 return wxtree_model
->internal
->iter_n_children( iter
);
871 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
876 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
877 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
879 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
883 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
887 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
888 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
889 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
891 return wxtree_model
->internal
->iter_parent( iter
, child
);
894 /* drag'n'drop iface */
896 wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
899 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
900 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
902 return wxtree_model
->internal
->row_draggable( drag_source
, path
);
906 wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
909 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
910 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
912 return wxtree_model
->internal
->drag_data_delete( drag_source
, path
);
916 wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
918 GtkSelectionData
*selection_data
)
920 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
921 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
924 wxPrintf( "drag_get_data\n");
926 wxGtkString
atom_selection(gdk_atom_name(selection_data
->selection
));
927 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection
) );
929 wxGtkString
atom_target(gdk_atom_name(selection_data
->target
));
930 wxPrintf( "target %s\n", wxString::FromAscii(atom_target
) );
932 wxGtkString
atom_type(gdk_atom_name(selection_data
->type
));
933 wxPrintf( "type %s\n", wxString::FromAscii(atom_type
) );
935 wxPrintf( "format %d\n", selection_data
->format
);
938 return wxtree_model
->internal
->drag_data_get( drag_source
, path
, selection_data
);
942 wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
944 GtkSelectionData
*selection_data
)
946 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
947 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
949 return wxtree_model
->internal
->drag_data_received( drag_dest
, dest
, selection_data
);
953 wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
954 GtkTreePath
*dest_path
,
955 GtkSelectionData
*selection_data
)
957 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
958 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
960 return wxtree_model
->internal
->row_drop_possible( drag_dest
, dest_path
, selection_data
);
965 wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
966 gint
*sort_column_id
,
969 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) sortable
;
971 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
973 if (!wxtree_model
->internal
->IsSorted())
976 *sort_column_id
= -1;
983 *sort_column_id
= wxtree_model
->internal
->GetSortColumn();
986 *order
= wxtree_model
->internal
->GetSortOrder();
991 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
994 wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
998 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
999 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1001 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
1003 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
1004 (order
!= tree_model
->internal
->GetSortOrder()))
1006 tree_model
->internal
->SetSortColumn( sort_column_id
);
1007 tree_model
->internal
->SetSortOrder( order
);
1009 gtk_tree_sortable_sort_column_changed (sortable
);
1011 tree_model
->internal
->GetDataViewModel()->Resort();
1014 if (gs_lastLeftClickHeader
)
1016 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
1017 wxDataViewEvent
event( wxEVT_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
1018 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
1019 event
.SetModel( dv
->GetModel() );
1020 dv
->HandleWindowEvent( event
);
1023 gs_lastLeftClickHeader
= NULL
;
1027 wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
1028 gint
WXUNUSED(sort_column_id
),
1029 GtkTreeIterCompareFunc func
,
1030 gpointer
WXUNUSED(data
),
1031 GDestroyNotify
WXUNUSED(destroy
))
1033 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1034 g_return_if_fail (func
!= NULL
);
1038 wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
1039 GtkTreeIterCompareFunc func
,
1040 gpointer
WXUNUSED(data
),
1041 GDestroyNotify
WXUNUSED(destroy
))
1043 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1044 g_return_if_fail (func
!= NULL
);
1046 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
1047 // TODO: remove this code
1051 wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
1053 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
1058 //-----------------------------------------------------------------------------
1059 // define new GTK+ class GtkWxRendererText
1060 //-----------------------------------------------------------------------------
1064 #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
1065 #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
1066 #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1067 #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1069 GType
gtk_wx_cell_renderer_text_get_type (void);
1071 typedef struct _GtkWxCellRendererText GtkWxCellRendererText
;
1073 struct _GtkWxCellRendererText
1075 GtkCellRendererText parent
;
1077 wxDataViewRenderer
*wx_renderer
;
1080 static GtkWxCellRendererText
*gtk_wx_cell_renderer_text_new (void);
1081 static void gtk_wx_cell_renderer_text_init (
1082 GTypeInstance
* instance
, void*);
1083 static void gtk_wx_cell_renderer_text_class_init(
1084 void* klass
, void*);
1085 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1086 GtkCellRenderer
*cell
,
1090 wxConstGdkRect
*background_area
,
1091 wxConstGdkRect
*cell_area
,
1092 GtkCellRendererState flags
);
1095 static GObjectClass
*text_cell_parent_class
= NULL
;
1100 gtk_wx_cell_renderer_text_get_type (void)
1102 static GType cell_wx_type
= 0;
1106 const GTypeInfo cell_wx_info
=
1108 sizeof (GtkCellRendererTextClass
),
1109 NULL
, /* base_init */
1110 NULL
, /* base_finalize */
1111 gtk_wx_cell_renderer_text_class_init
,
1112 NULL
, /* class_finalize */
1113 NULL
, /* class_data */
1114 sizeof (GtkWxCellRendererText
),
1115 0, /* n_preallocs */
1116 gtk_wx_cell_renderer_text_init
,
1119 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT
,
1120 "GtkWxCellRendererText", &cell_wx_info
, (GTypeFlags
)0 );
1123 return cell_wx_type
;
1127 gtk_wx_cell_renderer_text_init(GTypeInstance
* instance
, void*)
1129 GtkWxCellRendererText
* cell
= GTK_WX_CELL_RENDERER_TEXT(instance
);
1130 cell
->wx_renderer
= NULL
;
1134 gtk_wx_cell_renderer_text_class_init(void* klass
, void*)
1136 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1138 text_cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1140 cell_class
->start_editing
= gtk_wx_cell_renderer_text_start_editing
;
1143 GtkWxCellRendererText
*
1144 gtk_wx_cell_renderer_text_new (void)
1146 return (GtkWxCellRendererText
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT
, NULL
);
1149 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1150 GtkCellRenderer
*gtk_renderer
,
1151 GdkEvent
*gdk_event
,
1154 wxConstGdkRect
*background_area
,
1155 wxConstGdkRect
*cell_area
,
1156 GtkCellRendererState flags
)
1158 GtkWxCellRendererText
*wxgtk_renderer
= (GtkWxCellRendererText
*) gtk_renderer
;
1159 wxDataViewRenderer
*wx_renderer
= wxgtk_renderer
->wx_renderer
;
1160 wxDataViewColumn
*column
= wx_renderer
->GetOwner();
1163 item(column
->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1165 wxDataViewCtrl
*dv
= column
->GetOwner();
1166 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_START_EDITING
, dv
->GetId() );
1167 event
.SetDataViewColumn( column
);
1168 event
.SetModel( dv
->GetModel() );
1169 event
.SetColumn( column
->GetModelColumn() );
1170 event
.SetItem( item
);
1171 dv
->HandleWindowEvent( event
);
1173 if (event
.IsAllowed())
1174 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class
)->
1175 start_editing( gtk_renderer
, gdk_event
, widget
, path
, background_area
, cell_area
, flags
);
1180 //-----------------------------------------------------------------------------
1181 // define new GTK+ class GtkWxCellRenderer
1182 //-----------------------------------------------------------------------------
1186 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
1187 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
1188 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
1189 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
1191 GType
gtk_wx_cell_renderer_get_type (void);
1193 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
1195 struct _GtkWxCellRenderer
1197 GtkCellRenderer parent
;
1200 wxDataViewCustomRenderer
*cell
;
1203 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
1204 static void gtk_wx_cell_renderer_init (
1205 GTypeInstance
* instance
, void*);
1206 static void gtk_wx_cell_renderer_class_init(
1207 void* klass
, void*);
1208 static void gtk_wx_cell_renderer_get_size (
1209 GtkCellRenderer
*cell
,
1211 wxConstGdkRect
*rectangle
,
1216 static void gtk_wx_cell_renderer_render (
1217 GtkCellRenderer
*cell
,
1224 wxConstGdkRect
*background_area
,
1225 wxConstGdkRect
*cell_area
,
1227 GdkRectangle
*expose_area
,
1229 GtkCellRendererState flags
);
1230 static gboolean
gtk_wx_cell_renderer_activate(
1231 GtkCellRenderer
*cell
,
1235 wxConstGdkRect
*background_area
,
1236 wxConstGdkRect
*cell_area
,
1237 GtkCellRendererState flags
);
1238 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1239 GtkCellRenderer
*cell
,
1243 wxConstGdkRect
*background_area
,
1244 wxConstGdkRect
*cell_area
,
1245 GtkCellRendererState flags
);
1250 gtk_wx_cell_renderer_get_type (void)
1252 static GType cell_wx_type
= 0;
1256 const GTypeInfo cell_wx_info
=
1258 sizeof (GtkCellRendererClass
),
1259 NULL
, /* base_init */
1260 NULL
, /* base_finalize */
1261 gtk_wx_cell_renderer_class_init
,
1262 NULL
, /* class_finalize */
1263 NULL
, /* class_data */
1264 sizeof (GtkWxCellRenderer
),
1265 0, /* n_preallocs */
1266 gtk_wx_cell_renderer_init
,
1269 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
1270 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
1273 return cell_wx_type
;
1277 gtk_wx_cell_renderer_init(GTypeInstance
* instance
, void*)
1279 GtkWxCellRenderer
* cell
= GTK_WX_CELL_RENDERER(instance
);
1284 gtk_wx_cell_renderer_class_init(void* klass
, void*)
1286 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1288 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
1289 cell_class
->render
= gtk_wx_cell_renderer_render
;
1290 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
1291 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
1295 gtk_wx_cell_renderer_new (void)
1297 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
1300 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1301 GtkCellRenderer
*renderer
,
1302 GdkEvent
*WXUNUSED(event
),
1305 wxConstGdkRect
*WXUNUSED(background_area
),
1306 wxConstGdkRect
*cell_area
,
1307 GtkCellRendererState
WXUNUSED(flags
) )
1309 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1310 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1312 // Renderer doesn't support in-place editing
1313 if (!cell
->HasEditorCtrl())
1316 // An in-place editing control is still around
1317 if (cell
->GetEditorCtrl())
1321 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1327 rect
.x
+= cell_area
->x
;
1328 rect
.y
+= cell_area
->y
;
1329 // rect.width -= renderer->xpad * 2;
1330 // rect.height -= renderer->ypad * 2;
1332 // wxRect renderrect(wxRectFromGDKRect(&rect));
1333 wxRect
renderrect(wxRectFromGDKRect(cell_area
));
1336 item(cell
->GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1338 cell
->StartEditing( item
, renderrect
);
1344 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
1345 GtkWidget
*WXUNUSED(widget
),
1346 wxConstGdkRect
*cell_area
,
1352 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1353 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1355 wxSize size
= cell
->GetSize();
1357 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1359 // Uniform row height, if specified, overrides the value returned by the
1361 if ( !ctrl
->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
1363 const int uniformHeight
= ctrl
->GTKGetUniformRowHeight();
1364 if ( uniformHeight
> 0 )
1365 size
.y
= uniformHeight
;
1369 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1370 int calc_width
= xpad
* 2 + size
.x
;
1371 int calc_height
= ypad
* 2 + size
.y
;
1378 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
1380 float xalign
, yalign
;
1381 gtk_cell_renderer_get_alignment(renderer
, &xalign
, &yalign
);
1384 *x_offset
= int(xalign
* (cell_area
->width
- calc_width
- 2 * xpad
));
1385 *x_offset
= MAX(*x_offset
, 0) + xpad
;
1389 *y_offset
= int(yalign
* (cell_area
->height
- calc_height
- 2 * ypad
));
1390 *y_offset
= MAX(*y_offset
, 0) + ypad
;
1395 *width
= calc_width
;
1398 *height
= calc_height
;
1401 struct wxDataViewCustomRenderer::GTKRenderParams
1407 GdkRectangle
* expose_area
;
1410 wxConstGdkRect
* background_area
;
1415 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
1422 wxConstGdkRect
*background_area
,
1423 wxConstGdkRect
*cell_area
,
1425 GdkRectangle
*expose_area
,
1427 GtkCellRendererState flags
)
1430 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1431 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1433 wxDataViewCustomRenderer::GTKRenderParams renderParams
;
1435 renderParams
.cr
= cr
;
1437 renderParams
.window
= window
;
1438 renderParams
.expose_area
= expose_area
;
1440 renderParams
.widget
= widget
;
1441 renderParams
.background_area
= background_area
;
1442 renderParams
.flags
= flags
;
1443 cell
->GTKSetRenderParams(&renderParams
);
1445 wxRect
rect(wxRectFromGDKRect(cell_area
));
1447 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1448 rect
= rect
.Deflate(xpad
, ypad
);
1450 wxDC
* dc
= cell
->GetDC();
1452 wxGraphicsContext
* context
= dc
->GetGraphicsContext();
1453 void* nativeContext
= NULL
;
1455 nativeContext
= context
->GetNativeContext();
1456 if (cr
!= nativeContext
)
1458 cairo_reference(cr
);
1459 dc
->SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr
));
1462 wxWindowDCImpl
*impl
= (wxWindowDCImpl
*) dc
->GetImpl();
1464 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1465 // window such as a DnD drop window.
1466 if (window
!= impl
->m_gdkwindow
)
1469 impl
->m_gdkwindow
= window
;
1475 if (flags
& GTK_CELL_RENDERER_SELECTED
)
1476 state
|= wxDATAVIEW_CELL_SELECTED
;
1477 if (flags
& GTK_CELL_RENDERER_PRELIT
)
1478 state
|= wxDATAVIEW_CELL_PRELIT
;
1479 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1480 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1481 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1482 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1483 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
1484 state
|= wxDATAVIEW_CELL_FOCUSED
;
1485 cell
->WXCallRender( rect
, dc
, state
);
1487 cell
->GTKSetRenderParams(NULL
);
1489 dc
->SetGraphicsContext(NULL
);
1494 gtk_wx_cell_renderer_activate(
1495 GtkCellRenderer
*renderer
,
1499 wxConstGdkRect
*WXUNUSED(background_area
),
1500 wxConstGdkRect
*cell_area
,
1501 GtkCellRendererState
WXUNUSED(flags
) )
1503 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1504 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1507 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1513 rect
.x
+= cell_area
->x
;
1514 rect
.y
+= cell_area
->y
;
1516 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1517 rect
.width
-= xpad
* 2;
1518 rect
.height
-= ypad
* 2;
1520 wxRect
renderrect(wxRectFromGDKRect(&rect
));
1522 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1523 wxDataViewModel
*model
= ctrl
->GetModel();
1525 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
1527 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1531 // activated by <ENTER>
1532 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, NULL
);
1534 else if ( event
->type
== GDK_BUTTON_PRESS
)
1536 GdkEventButton
*button_event
= (GdkEventButton
*)event
;
1537 if ( button_event
->button
== 1 )
1539 wxMouseEvent
mouse_event(wxEVT_LEFT_DOWN
);
1540 InitMouseEvent(ctrl
, mouse_event
, button_event
);
1542 mouse_event
.m_x
-= renderrect
.x
;
1543 mouse_event
.m_y
-= renderrect
.y
;
1545 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, &mouse_event
);
1549 wxLogDebug("unexpected event type in gtk_wx_cell_renderer_activate()");
1553 // ---------------------------------------------------------
1554 // wxGtkDataViewModelNotifier
1555 // ---------------------------------------------------------
1557 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1560 wxGtkDataViewModelNotifier( wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
);
1561 ~wxGtkDataViewModelNotifier();
1563 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1564 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1565 virtual bool ItemChanged( const wxDataViewItem
&item
);
1566 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
1567 virtual bool Cleared();
1568 virtual void Resort();
1569 virtual bool BeforeReset();
1570 virtual bool AfterReset();
1572 void UpdateLastCount();
1575 wxDataViewModel
*m_wx_model
;
1576 wxDataViewCtrlInternal
*m_internal
;
1579 // ---------------------------------------------------------
1580 // wxGtkDataViewListModelNotifier
1581 // ---------------------------------------------------------
1583 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1584 wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
)
1586 m_wx_model
= wx_model
;
1587 m_internal
= internal
;
1590 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1596 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1598 m_internal
->ItemAdded( parent
, item
);
1599 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1602 iter
.stamp
= wxgtk_model
->stamp
;
1603 iter
.user_data
= item
.GetID();
1605 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1606 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1607 gtk_tree_model_row_inserted(
1608 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1613 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1615 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1617 // using _get_path for a deleted item cannot be
1620 iter
.stamp
= wxgtk_model
->stamp
;
1621 iter
.user_data
= (gpointer
) item
.GetID();
1622 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1623 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1625 // so get the path from the parent
1626 GtkTreeIter parentIter
;
1627 parentIter
.stamp
= wxgtk_model
->stamp
;
1628 parentIter
.user_data
= (gpointer
) parent
.GetID();
1629 wxGtkTreePath
parentPath(wxgtk_tree_model_get_path(
1630 GTK_TREE_MODEL(wxgtk_model
), &parentIter
));
1632 // and add the final index ourselves
1633 wxGtkTreePath
path(gtk_tree_path_copy(parentPath
));
1634 int index
= m_internal
->GetIndexOf( parent
, item
);
1635 gtk_tree_path_append_index( path
, index
);
1638 gtk_tree_model_row_deleted(
1639 GTK_TREE_MODEL(wxgtk_model
), path
);
1641 m_internal
->ItemDeleted( parent
, item
);
1643 // Did we remove the last child, causing 'parent' to become a leaf?
1644 if ( !m_wx_model
->IsContainer(parent
) )
1646 gtk_tree_model_row_has_child_toggled
1648 GTK_TREE_MODEL(wxgtk_model
),
1657 void wxGtkDataViewModelNotifier::Resort()
1659 m_internal
->Resort();
1662 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1664 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1667 iter
.stamp
= wxgtk_model
->stamp
;
1668 iter
.user_data
= (gpointer
) item
.GetID();
1670 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1671 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1672 gtk_tree_model_row_changed(
1673 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1675 m_internal
->ItemChanged( item
);
1680 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
)
1682 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1683 wxDataViewCtrl
*ctrl
= m_internal
->GetOwner();
1685 // This adds GTK+'s missing MVC logic for ValueChanged
1687 for (index
= 0; index
< ctrl
->GetColumnCount(); index
++)
1689 wxDataViewColumn
*column
= ctrl
->GetColumn( index
);
1690 if (column
->GetModelColumn() == model_column
)
1692 GtkTreeView
*widget
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
1693 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1695 // Don't attempt to refresh not yet realized tree, it is useless
1696 // and results in GTK errors.
1697 if ( gtk_widget_get_realized(ctrl
->GtkGetTreeView()) )
1701 iter
.stamp
= wxgtk_model
->stamp
;
1702 iter
.user_data
= (gpointer
) item
.GetID();
1703 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1704 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1705 GdkRectangle cell_area
;
1706 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1708 GtkAdjustment
* hadjust
= gtk_scrollable_get_hadjustment(GTK_SCROLLABLE(widget
));
1710 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1712 double d
= gtk_adjustment_get_value( hadjust
);
1713 int xdiff
= (int) d
;
1716 gtk_widget_get_allocation(GTK_WIDGET(gtk_tree_view_column_get_button(gcolumn
)), &a
);
1717 int ydiff
= a
.height
;
1719 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1720 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1723 m_internal
->ValueChanged( item
, model_column
);
1732 bool wxGtkDataViewModelNotifier::BeforeReset()
1734 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1735 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), NULL
);
1740 bool wxGtkDataViewModelNotifier::AfterReset()
1742 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1743 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1745 m_internal
->Cleared();
1747 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), GTK_TREE_MODEL(wxgtk_model
) );
1752 bool wxGtkDataViewModelNotifier::Cleared()
1754 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1756 // There is no call to tell the model that everything
1757 // has been deleted so call row_deleted() for every
1760 int count
= m_internal
->iter_n_children( NULL
); // number of children of root
1762 GtkTreePath
*path
= gtk_tree_path_new_first(); // points to root
1765 for (i
= 0; i
< count
; i
++)
1766 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model
), path
);
1768 gtk_tree_path_free( path
);
1770 m_internal
->Cleared();
1775 // ---------------------------------------------------------
1776 // wxDataViewRenderer
1777 // ---------------------------------------------------------
1779 static gpointer s_user_data
= NULL
;
1782 wxgtk_cell_editable_editing_done( GtkCellEditable
*WXUNUSED(editable
),
1783 wxDataViewRenderer
*wxrenderer
)
1785 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1786 wxDataViewCtrl
*dv
= column
->GetOwner();
1787 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1788 event
.SetDataViewColumn( column
);
1789 event
.SetModel( dv
->GetModel() );
1790 wxDataViewItem
item( s_user_data
);
1791 event
.SetItem( item
);
1792 dv
->HandleWindowEvent( event
);
1796 wxgtk_renderer_editing_started( GtkCellRenderer
*WXUNUSED(cell
), GtkCellEditable
*editable
,
1797 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1802 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1803 wxDataViewCtrl
*dv
= column
->GetOwner();
1804 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1805 event
.SetDataViewColumn( column
);
1806 event
.SetModel( dv
->GetModel() );
1807 wxDataViewItem
item(dv
->GTKPathToItem(wxGtkTreePath(path
)));
1808 event
.SetItem( item
);
1809 dv
->HandleWindowEvent( event
);
1811 if (GTK_IS_CELL_EDITABLE(editable
))
1813 s_user_data
= item
.GetID();
1815 g_signal_connect (editable
, "editing_done",
1816 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1817 (gpointer
) wxrenderer
);
1823 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1825 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1827 wxDataViewRendererBase( varianttype
, mode
, align
)
1832 // we haven't changed them yet
1833 m_usingDefaultAttrs
= true;
1835 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1836 // after the m_renderer pointer has been initialized
1839 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
1841 gtk_tree_view_column_pack_end( column
, m_renderer
, TRUE
/* expand */);
1844 void wxDataViewRenderer::GtkInitHandlers()
1847 g_signal_connect (m_renderer
, "editing_started",
1848 G_CALLBACK (wxgtk_renderer_editing_started
),
1853 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1855 GtkCellRendererMode gtkMode
;
1858 case wxDATAVIEW_CELL_INERT
:
1859 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1862 case wxDATAVIEW_CELL_ACTIVATABLE
:
1863 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1866 case wxDATAVIEW_CELL_EDITABLE
:
1867 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1871 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1877 // This value is most often ignored in GtkTreeView
1878 GValue gvalue
= { 0, };
1879 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1880 g_value_set_enum( &gvalue
, gtkMode
);
1881 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1882 g_value_unset( &gvalue
);
1885 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1887 wxDataViewCellMode ret
;
1890 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1892 switch (g_value_get_enum(&gvalue
))
1895 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1896 // fall through (we have to return something)
1898 case GTK_CELL_RENDERER_MODE_INERT
:
1899 ret
= wxDATAVIEW_CELL_INERT
;
1902 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1903 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1906 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1907 ret
= wxDATAVIEW_CELL_EDITABLE
;
1911 g_value_unset( &gvalue
);
1916 void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer
*renderer
)
1918 int align
= m_alignment
;
1920 // query alignment from column ?
1924 if (GetOwner() == NULL
)
1927 align
= GetOwner()->GetAlignment();
1928 align
|= wxALIGN_CENTRE_VERTICAL
;
1931 // horizontal alignment:
1933 gfloat xalign
= 0.0;
1934 if (align
& wxALIGN_RIGHT
)
1936 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1939 GValue gvalue
= { 0, };
1940 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1941 g_value_set_float( &gvalue
, xalign
);
1942 g_object_set_property( G_OBJECT(renderer
), "xalign", &gvalue
);
1943 g_value_unset( &gvalue
);
1945 // vertical alignment:
1947 gfloat yalign
= 0.0;
1948 if (align
& wxALIGN_BOTTOM
)
1950 else if (align
& wxALIGN_CENTER_VERTICAL
)
1953 GValue gvalue2
= { 0, };
1954 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1955 g_value_set_float( &gvalue2
, yalign
);
1956 g_object_set_property( G_OBJECT(renderer
), "yalign", &gvalue2
);
1957 g_value_unset( &gvalue2
);
1960 void wxDataViewRenderer::SetAlignment( int align
)
1962 m_alignment
= align
;
1963 GtkUpdateAlignment();
1966 int wxDataViewRenderer::GetAlignment() const
1971 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode
)
1973 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1977 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1978 // can just cast between them
1979 GValue gvalue
= { 0, };
1980 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1981 g_value_set_enum( &gvalue
, static_cast<PangoEllipsizeMode
>(mode
) );
1982 g_object_set_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1983 g_value_unset( &gvalue
);
1986 wxEllipsizeMode
wxDataViewRenderer::GetEllipsizeMode() const
1988 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1990 return wxELLIPSIZE_NONE
;
1992 GValue gvalue
= { 0, };
1993 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1994 g_object_get_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1996 mode
= static_cast<wxEllipsizeMode
>(g_value_get_enum( &gvalue
));
1997 g_value_unset( &gvalue
);
2003 wxDataViewRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
2005 wxVariant
value(str
);
2006 if (!Validate( value
))
2010 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2012 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2016 wxDataViewRenderer::GtkOnCellChanged(const wxVariant
& value
,
2017 const wxDataViewItem
& item
,
2020 wxDataViewModel
*model
= GetOwner()->GetOwner()->GetModel();
2021 model
->ChangeValue( value
, item
, col
);
2024 // ---------------------------------------------------------
2025 // wxDataViewTextRenderer
2026 // ---------------------------------------------------------
2031 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*WXUNUSED(renderer
),
2032 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
2034 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) user_data
;
2036 cell
->GtkOnTextEdited(arg1
, wxGTK_CONV_BACK_FONT(
2037 arg2
, cell
->GetOwner()->GetOwner()->GetFont()));
2045 // helper function used by wxDataViewTextRenderer and
2046 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
2047 // given text renderer and returns true if anything was done
2048 bool GtkApplyAttr(GtkCellRendererText
*renderer
, const wxDataViewItemAttr
& attr
)
2050 bool usingDefaultAttrs
= true;
2051 if (attr
.HasColour())
2053 const GdkColor
* const gcol
= attr
.GetColour().GetColor();
2055 GValue gvalue
= { 0, };
2056 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2057 g_value_set_boxed( &gvalue
, gcol
);
2058 g_object_set_property( G_OBJECT(renderer
), "foreground_gdk", &gvalue
);
2059 g_value_unset( &gvalue
);
2061 usingDefaultAttrs
= false;
2065 GValue gvalue
= { 0, };
2066 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2067 g_value_set_boolean( &gvalue
, FALSE
);
2068 g_object_set_property( G_OBJECT(renderer
), "foreground-set", &gvalue
);
2069 g_value_unset( &gvalue
);
2072 if (attr
.GetItalic())
2074 GValue gvalue
= { 0, };
2075 g_value_init( &gvalue
, PANGO_TYPE_STYLE
);
2076 g_value_set_enum( &gvalue
, PANGO_STYLE_ITALIC
);
2077 g_object_set_property( G_OBJECT(renderer
), "style", &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
), "style-set", &gvalue
);
2088 g_value_unset( &gvalue
);
2094 GValue gvalue
= { 0, };
2095 g_value_init( &gvalue
, PANGO_TYPE_WEIGHT
);
2096 g_value_set_enum( &gvalue
, PANGO_WEIGHT_BOLD
);
2097 g_object_set_property( G_OBJECT(renderer
), "weight", &gvalue
);
2098 g_value_unset( &gvalue
);
2100 usingDefaultAttrs
= false;
2104 GValue gvalue
= { 0, };
2105 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2106 g_value_set_boolean( &gvalue
, FALSE
);
2107 g_object_set_property( G_OBJECT(renderer
), "weight-set", &gvalue
);
2108 g_value_unset( &gvalue
);
2112 if (attr
.HasBackgroundColour())
2114 wxColour colour
= attr
.GetBackgroundColour();
2115 const GdkColor
* const gcol
= colour
.GetColor();
2117 GValue gvalue
= { 0, };
2118 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2119 g_value_set_boxed( &gvalue
, gcol
);
2120 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2121 g_value_unset( &gvalue
);
2125 GValue gvalue
= { 0, };
2126 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2127 g_value_set_boolean( &gvalue
, FALSE
);
2128 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2129 g_value_unset( &gvalue
);
2133 return !usingDefaultAttrs
;
2136 } // anonymous namespace
2138 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
2140 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2142 wxDataViewRenderer( varianttype
, mode
, align
)
2144 GtkWxCellRendererText
*text_renderer
= gtk_wx_cell_renderer_text_new();
2145 text_renderer
->wx_renderer
= this;
2146 m_renderer
= (GtkCellRenderer
*) text_renderer
;
2148 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
2150 GValue gvalue
= { 0, };
2151 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2152 g_value_set_boolean( &gvalue
, true );
2153 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
2154 g_value_unset( &gvalue
);
2156 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2162 SetAlignment(align
);
2165 bool wxDataViewTextRenderer::SetTextValue(const wxString
& str
)
2167 GValue gvalue
= { 0, };
2168 g_value_init( &gvalue
, G_TYPE_STRING
);
2169 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( str
, GetOwner()->GetOwner()->GetFont() ) );
2170 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2171 g_value_unset( &gvalue
);
2176 bool wxDataViewTextRenderer::GetTextValue(wxString
& str
) const
2178 GValue gvalue
= { 0, };
2179 g_value_init( &gvalue
, G_TYPE_STRING
);
2180 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2181 str
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
), const_cast<wxDataViewTextRenderer
*>(this)->GetOwner()->GetOwner()->GetFont() );
2182 g_value_unset( &gvalue
);
2187 void wxDataViewTextRenderer::SetAlignment( int align
)
2189 wxDataViewRenderer::SetAlignment(align
);
2192 if (gtk_check_version(2,10,0))
2196 // horizontal alignment:
2197 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2198 if (align
& wxALIGN_RIGHT
)
2199 pangoAlign
= PANGO_ALIGN_RIGHT
;
2200 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2201 pangoAlign
= PANGO_ALIGN_CENTER
;
2203 GValue gvalue
= { 0, };
2204 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2205 g_value_set_enum( &gvalue
, pangoAlign
);
2206 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2207 g_value_unset( &gvalue
);
2210 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr
& attr
)
2212 return GtkApplyAttr(GtkGetTextRenderer(), attr
);
2215 GtkCellRendererText
*wxDataViewTextRenderer::GtkGetTextRenderer() const
2217 return GTK_CELL_RENDERER_TEXT(m_renderer
);
2220 // ---------------------------------------------------------
2221 // wxDataViewBitmapRenderer
2222 // ---------------------------------------------------------
2227 // set "pixbuf" property on the given renderer
2228 void SetPixbufProp(GtkCellRenderer
*renderer
, GdkPixbuf
*pixbuf
)
2230 GValue gvalue
= { 0, };
2231 g_value_init( &gvalue
, G_TYPE_OBJECT
);
2232 g_value_set_object( &gvalue
, pixbuf
);
2233 g_object_set_property( G_OBJECT(renderer
), "pixbuf", &gvalue
);
2234 g_value_unset( &gvalue
);
2237 } // anonymous namespace
2239 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
2241 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2243 wxDataViewRenderer( varianttype
, mode
, align
)
2245 m_renderer
= gtk_cell_renderer_pixbuf_new();
2248 SetAlignment(align
);
2251 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
2253 if (value
.GetType() == wxT("wxBitmap"))
2258 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2259 // object (and it will stay there and remain owned by wxBitmap)
2260 SetPixbufProp(m_renderer
, bitmap
.GetPixbuf());
2262 else if (value
.GetType() == wxT("wxIcon"))
2267 SetPixbufProp(m_renderer
, icon
.GetPixbuf());
2277 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2282 // ---------------------------------------------------------
2283 // wxDataViewToggleRenderer
2284 // ---------------------------------------------------------
2287 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2288 gchar
*path
, gpointer user_data
);
2291 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2292 gchar
*path
, gpointer user_data
)
2294 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
2297 GValue gvalue
= { 0, };
2298 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2299 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
2301 wxVariant value
= !g_value_get_boolean( &gvalue
);
2302 g_value_unset( &gvalue
);
2304 if (!cell
->Validate( value
))
2307 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
2308 wxDataViewModel
*model
= ctrl
->GetModel();
2310 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
2312 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
2314 model
->ChangeValue( value
, item
, model_col
);
2317 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
2319 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
2320 wxDataViewCellMode mode
, int align
) :
2321 wxDataViewRenderer( varianttype
, mode
, align
)
2323 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
2325 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
2327 g_signal_connect_after( m_renderer
, "toggled",
2328 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
2332 GValue gvalue
= { 0, };
2333 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2334 g_value_set_boolean( &gvalue
, false );
2335 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
2336 g_value_unset( &gvalue
);
2340 SetAlignment(align
);
2343 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
2347 GValue gvalue
= { 0, };
2348 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2349 g_value_set_boolean( &gvalue
, tmp
);
2350 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2351 g_value_unset( &gvalue
);
2356 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
2358 GValue gvalue
= { 0, };
2359 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2360 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2361 value
= g_value_get_boolean( &gvalue
) != 0;
2362 g_value_unset( &gvalue
);
2367 // ---------------------------------------------------------
2368 // wxDataViewCustomRenderer
2369 // ---------------------------------------------------------
2372 class wxDataViewCtrlDCImpl
: public wxWindowDCImpl
2375 wxDataViewCtrlDCImpl( wxDC
*owner
, wxDataViewCtrl
*window
) :
2376 wxWindowDCImpl( owner
)
2378 GtkWidget
*widget
= window
->m_treeview
;
2384 m_context
= window
->GTKGetPangoDefaultContext();
2385 m_layout
= pango_layout_new( m_context
);
2386 m_fontdesc
= pango_font_description_copy(gtk_widget_get_style(widget
)->font_desc
);
2388 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
2390 // Set m_gdkwindow later
2395 class wxDataViewCtrlDC
: public wxWindowDC
2398 wxDataViewCtrlDC( wxDataViewCtrl
*window
) :
2399 wxWindowDC( new wxDataViewCtrlDCImpl( this, window
) )
2404 // ---------------------------------------------------------
2405 // wxDataViewCustomRenderer
2406 // ---------------------------------------------------------
2408 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
2410 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
2411 wxDataViewCellMode mode
,
2414 : wxDataViewCustomRendererBase( varianttype
, mode
, align
)
2417 m_text_renderer
= NULL
;
2418 m_renderParams
= NULL
;
2426 void wxDataViewCustomRenderer::GtkInitTextRenderer()
2428 m_text_renderer
= GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2429 g_object_ref_sink(m_text_renderer
);
2431 GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer
));
2434 GtkCellRendererText
*wxDataViewCustomRenderer::GtkGetTextRenderer() const
2436 if ( !m_text_renderer
)
2438 // we create it on demand so need to do it even from a const function
2439 const_cast<wxDataViewCustomRenderer
*>(this)->GtkInitTextRenderer();
2442 return m_text_renderer
;
2445 void wxDataViewCustomRenderer::RenderText( const wxString
&text
,
2449 int WXUNUSED(state
) )
2452 GtkCellRendererText
* const textRenderer
= GtkGetTextRenderer();
2454 GValue gvalue
= { 0, };
2455 g_value_init( &gvalue
, G_TYPE_STRING
);
2456 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( text
, GetOwner()->GetOwner()->GetFont() ) );
2457 g_object_set_property( G_OBJECT(textRenderer
), "text", &gvalue
);
2458 g_value_unset( &gvalue
);
2460 GtkApplyAttr(textRenderer
, GetAttr());
2462 GdkRectangle cell_area
;
2463 wxRectToGDKRect(cell
, cell_area
);
2464 cell_area
.x
+= xoffset
;
2465 cell_area
.width
-= xoffset
;
2467 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer
),
2471 m_renderParams
->window
,
2473 m_renderParams
->widget
,
2474 m_renderParams
->background_area
,
2477 m_renderParams
->expose_area
,
2479 GtkCellRendererState(m_renderParams
->flags
));
2482 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
2484 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
2485 renderer
->cell
= this;
2487 m_renderer
= (GtkCellRenderer
*) renderer
;
2490 SetAlignment(align
);
2497 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2502 if (m_text_renderer
)
2503 g_object_unref(m_text_renderer
);
2506 wxDC
*wxDataViewCustomRenderer::GetDC()
2511 wxASSERT(m_renderParams
);
2512 cairo_t
* cr
= m_renderParams
->cr
;
2513 wxASSERT(cr
&& cairo_status(cr
) == 0);
2514 m_dc
= new wxGTKCairoDC(cr
);
2516 if (GetOwner() == NULL
)
2518 if (GetOwner()->GetOwner() == NULL
)
2520 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2527 // ---------------------------------------------------------
2528 // wxDataViewProgressRenderer
2529 // ---------------------------------------------------------
2531 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
2533 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
2534 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
2535 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
2539 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
2542 SetAlignment(align
);
2545 // We can't initialize the renderer just yet because we don't have the
2546 // pointer to the column that uses this renderer yet and so attempt to
2547 // dereference GetOwner() to get the font that is used as a source of
2548 // encoding in multibyte-to-Unicode conversion in GTKSetLabel() in
2549 // non-Unicode builds would crash. So simply remember to do it later.
2550 if ( !m_label
.empty() )
2551 m_needsToSetLabel
= true;
2553 #endif // !wxUSE_UNICODE
2559 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2563 void wxDataViewProgressRenderer::GTKSetLabel()
2565 GValue gvalue
= { 0, };
2566 g_value_init( &gvalue
, G_TYPE_STRING
);
2568 // Take care to not use GetOwner() here if the label is empty, we can be
2569 // called from ctor when GetOwner() is still NULL in this case.
2570 wxScopedCharBuffer buf
;
2571 if ( m_label
.empty() )
2572 buf
= wxScopedCharBuffer::CreateNonOwned("");
2574 buf
= wxGTK_CONV_FONT(m_label
, GetOwner()->GetOwner()->GetFont());
2576 g_value_set_string( &gvalue
, buf
);
2577 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2578 g_value_unset( &gvalue
);
2581 m_needsToSetLabel
= false;
2582 #endif // !wxUSE_UNICODE
2585 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
2588 if ( m_needsToSetLabel
)
2590 #endif // !wxUSE_UNICODE
2592 gint tmp
= (long) value
;
2593 GValue gvalue
= { 0, };
2594 g_value_init( &gvalue
, G_TYPE_INT
);
2595 g_value_set_int( &gvalue
, tmp
);
2596 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
2597 g_value_unset( &gvalue
);
2602 bool wxDataViewProgressRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2607 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
2609 double pct
= (double)m_value
/ 100.0;
2611 bar
.width
= (int)(cell
.width
* pct
);
2612 dc
->SetPen( *wxTRANSPARENT_PEN
);
2613 dc
->SetBrush( *wxBLUE_BRUSH
);
2614 dc
->DrawRectangle( bar
);
2616 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
2617 dc
->SetPen( *wxBLACK_PEN
);
2618 dc
->DrawRectangle( cell
);
2623 wxSize
wxDataViewProgressRenderer::GetSize() const
2625 return wxSize(40,12);
2628 // -------------------------------------
2629 // wxDataViewChoiceRenderer
2630 // -------------------------------------
2632 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString
&choices
,
2633 wxDataViewCellMode mode
, int alignment
) :
2634 wxDataViewCustomRenderer( "string", mode
, alignment
, true )
2636 m_choices
= choices
;
2637 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_combo_new();
2638 GtkListStore
*store
= gtk_list_store_new( 1, G_TYPE_STRING
);
2639 for (size_t n
= 0; n
< m_choices
.GetCount(); n
++)
2641 gtk_list_store_insert_with_values(
2643 static_cast<const char *>(m_choices
[n
].utf8_str()), -1 );
2646 g_object_set (m_renderer
,
2652 bool editable
= (mode
& wxDATAVIEW_CELL_EDITABLE
) != 0;
2653 g_object_set (m_renderer
, "editable", editable
, NULL
);
2655 SetAlignment(alignment
);
2657 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2662 bool wxDataViewChoiceRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
2664 RenderText( m_data
, 0, rect
, dc
, state
);
2668 wxSize
wxDataViewChoiceRenderer::GetSize() const
2670 return wxSize(70,20);
2673 bool wxDataViewChoiceRenderer::SetValue( const wxVariant
&value
)
2675 GValue gvalue
= { 0, };
2676 g_value_init( &gvalue
, G_TYPE_STRING
);
2677 g_value_set_string(&gvalue
,
2678 wxGTK_CONV_FONT(value
.GetString(),
2679 GetOwner()->GetOwner()->GetFont()));
2680 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2681 g_value_unset( &gvalue
);
2686 bool wxDataViewChoiceRenderer::GetValue( wxVariant
&value
) const
2688 GValue gvalue
= { 0, };
2689 g_value_init( &gvalue
, G_TYPE_STRING
);
2690 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2691 wxString temp
= wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue
),
2692 GetOwner()->GetOwner()->GetFont());
2693 g_value_unset( &gvalue
);
2699 void wxDataViewChoiceRenderer::SetAlignment( int align
)
2701 wxDataViewCustomRenderer::SetAlignment(align
);
2704 if (gtk_check_version(2,10,0))
2708 // horizontal alignment:
2709 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2710 if (align
& wxALIGN_RIGHT
)
2711 pangoAlign
= PANGO_ALIGN_RIGHT
;
2712 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2713 pangoAlign
= PANGO_ALIGN_CENTER
;
2715 GValue gvalue
= { 0, };
2716 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2717 g_value_set_enum( &gvalue
, pangoAlign
);
2718 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2719 g_value_unset( &gvalue
);
2722 // ----------------------------------------------------------------------------
2723 // wxDataViewChoiceByIndexRenderer
2724 // ----------------------------------------------------------------------------
2726 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
2727 wxDataViewCellMode mode
, int alignment
) :
2728 wxDataViewChoiceRenderer( choices
, mode
, alignment
)
2732 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
2734 wxVariant
value( (long) GetChoices().Index( str
) );
2736 if (!Validate( value
))
2740 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2742 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2745 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant
&value
)
2747 wxVariant string_value
= GetChoice( value
.GetLong() );
2748 return wxDataViewChoiceRenderer::SetValue( string_value
);
2751 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant
&value
) const
2753 wxVariant string_value
;
2754 if (!wxDataViewChoiceRenderer::GetValue( string_value
))
2757 value
= (long) GetChoices().Index( string_value
.GetString() );
2761 // ---------------------------------------------------------
2762 // wxDataViewIconTextRenderer
2763 // ---------------------------------------------------------
2765 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
2767 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2769 const wxString
&varianttype
,
2770 wxDataViewCellMode mode
,
2773 : wxDataViewTextRenderer(varianttype
, mode
, align
)
2775 m_rendererIcon
= gtk_cell_renderer_pixbuf_new();
2778 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2782 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
2784 // add the icon renderer first
2785 gtk_tree_view_column_pack_start(column
, m_rendererIcon
, FALSE
/* !expand */);
2787 // add the text renderer too
2788 wxDataViewRenderer::GtkPackIntoColumn(column
);
2791 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
2795 SetTextValue(m_value
.GetText());
2797 const wxIcon
& icon
= m_value
.GetIcon();
2798 SetPixbufProp(m_rendererIcon
, icon
.IsOk() ? icon
.GetPixbuf() : NULL
);
2803 bool wxDataViewIconTextRenderer::GetValue(wxVariant
& value
) const
2806 if ( !GetTextValue(str
) )
2809 // user doesn't have any way to edit the icon so leave it unchanged
2810 value
<< wxDataViewIconText(str
, m_value
.GetIcon());
2816 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant
& value
,
2817 const wxDataViewItem
& item
,
2820 // we receive just the text part of our value as it's the only one which
2821 // can be edited, but we need the full wxDataViewIconText value for the
2823 wxVariant valueIconText
;
2824 valueIconText
<< wxDataViewIconText(value
.GetString(), m_value
.GetIcon());
2825 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText
, item
, col
);
2828 // ---------------------------------------------------------
2830 // ---------------------------------------------------------
2834 gtk_dataview_header_button_press_callback( GtkWidget
*WXUNUSED(widget
),
2835 GdkEventButton
*gdk_event
,
2836 wxDataViewColumn
*column
)
2838 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
2841 if (gdk_event
->button
== 1)
2843 gs_lastLeftClickHeader
= column
;
2845 wxDataViewCtrl
*dv
= column
->GetOwner();
2846 wxDataViewEvent
event( wxEVT_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2847 event
.SetDataViewColumn( column
);
2848 event
.SetModel( dv
->GetModel() );
2849 if (dv
->HandleWindowEvent( event
))
2853 if (gdk_event
->button
== 3)
2855 wxDataViewCtrl
*dv
= column
->GetOwner();
2856 wxDataViewEvent
event( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2857 event
.SetDataViewColumn( column
);
2858 event
.SetModel( dv
->GetModel() );
2859 if (dv
->HandleWindowEvent( event
))
2869 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*WXUNUSED(column
),
2870 GtkCellRenderer
*renderer
,
2871 GtkTreeModel
*model
,
2875 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2876 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2878 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2880 wxDataViewItem
item( (void*) iter
->user_data
);
2882 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2884 if (!wx_model
->IsVirtualListModel())
2887 if (wx_model
->IsContainer( item
))
2889 visible
= wx_model
->HasContainerColumns( item
) ||
2890 (cell
->GetOwner()->GetModelColumn() == 0);
2897 GValue gvalue
= { 0, };
2898 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2899 g_value_set_boolean( &gvalue
, visible
);
2900 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2901 g_value_unset( &gvalue
);
2908 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2910 if (value
.GetType() != cell
->GetVariantType())
2912 wxLogError( wxT("Wrong type, required: %s but: %s"),
2913 value
.GetType().c_str(),
2914 cell
->GetVariantType().c_str() );
2917 cell
->SetValue( value
);
2919 // deal with disabled items
2920 bool enabled
= wx_model
->IsEnabled( item
, cell
->GetOwner()->GetModelColumn() );
2922 // a) this sets the appearance to disabled grey
2923 GValue gvalue
= { 0, };
2924 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2925 g_value_set_boolean( &gvalue
, enabled
);
2926 g_object_set_property( G_OBJECT(renderer
), "sensitive", &gvalue
);
2927 g_value_unset( &gvalue
);
2929 // b) this actually disables the control/renderer
2931 cell
->SetMode( cell
->GtkGetMode() );
2933 cell
->SetMode( wxDATAVIEW_CELL_INERT
);
2936 // deal with attributes: if the renderer doesn't support them at all, we
2937 // don't even need to query the model for them
2938 if ( !cell
->GtkSupportsAttrs() )
2941 // it can support attributes so check if this item has any
2942 wxDataViewItemAttr attr
;
2943 if ( wx_model
->GetAttr( item
, cell
->GetOwner()->GetModelColumn(), attr
)
2944 || !cell
->GtkIsUsingDefaultAttrs() )
2946 bool usingDefaultAttrs
= !cell
->GtkSetAttr(attr
);
2947 cell
->GtkSetUsingDefaultAttrs(usingDefaultAttrs
);
2949 // else: no custom attributes specified and we're already using the default
2950 // ones -- nothing to do
2956 #include <wx/listimpl.cpp>
2957 WX_DEFINE_LIST(wxDataViewColumnList
)
2959 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
2960 unsigned int model_column
, int width
,
2961 wxAlignment align
, int flags
)
2962 : wxDataViewColumnBase( cell
, model_column
)
2964 Init( align
, flags
, width
);
2969 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
2970 unsigned int model_column
, int width
,
2971 wxAlignment align
, int flags
)
2972 : wxDataViewColumnBase( bitmap
, cell
, model_column
)
2974 Init( align
, flags
, width
);
2976 SetBitmap( bitmap
);
2979 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
2981 m_isConnected
= false;
2983 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
2984 m_column
= (GtkWidget
*) column
;
2987 SetAlignment( align
);
2991 // Create container for icon and label
2992 GtkWidget
* box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 1);
2993 gtk_widget_show( box
);
2994 // gtk_container_set_border_width((GtkContainer*)box, 2);
2995 m_image
= gtk_image_new();
2996 gtk_box_pack_start(GTK_BOX(box
), m_image
, FALSE
, FALSE
, 1);
2997 m_label
= gtk_label_new("");
2998 gtk_box_pack_end( GTK_BOX(box
), GTK_WIDGET(m_label
), FALSE
, FALSE
, 1 );
2999 gtk_tree_view_column_set_widget( column
, box
);
3001 wxDataViewRenderer
* const colRenderer
= GetRenderer();
3002 GtkCellRenderer
* const cellRenderer
= colRenderer
->GetGtkHandle();
3004 colRenderer
->GtkPackIntoColumn(column
);
3006 gtk_tree_view_column_set_cell_data_func( column
, cellRenderer
,
3007 wxGtkTreeCellDataFunc
, (gpointer
) colRenderer
, NULL
);
3010 void wxDataViewColumn::OnInternalIdle()
3015 if (gtk_widget_get_realized(GetOwner()->m_treeview
))
3017 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3018 GtkWidget
* button
= gtk_tree_view_column_get_button(column
);
3021 g_signal_connect(button
, "button_press_event",
3022 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
3024 // otherwise the event will be blocked by GTK+
3025 gtk_tree_view_column_set_clickable( column
, TRUE
);
3027 m_isConnected
= true;
3032 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
3034 wxDataViewColumnBase::SetOwner( owner
);
3036 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3038 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3041 void wxDataViewColumn::SetTitle( const wxString
&title
)
3043 wxDataViewCtrl
*ctrl
= GetOwner();
3044 gtk_label_set_text( GTK_LABEL(m_label
), ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
3045 : wxGTK_CONV_SYS(title
) );
3047 gtk_widget_hide( m_label
);
3049 gtk_widget_show( m_label
);
3052 wxString
wxDataViewColumn::GetTitle() const
3054 return wxGTK_CONV_BACK_FONT(
3055 gtk_label_get_text( GTK_LABEL(m_label
) ),
3056 GetOwner()->GetFont()
3060 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
3062 wxDataViewColumnBase::SetBitmap( bitmap
);
3066 GtkImage
*gtk_image
= GTK_IMAGE(m_image
);
3068 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
), bitmap
.GetPixbuf());
3069 gtk_widget_show( m_image
);
3073 gtk_widget_hide( m_image
);
3077 void wxDataViewColumn::SetHidden( bool hidden
)
3079 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
3082 void wxDataViewColumn::SetResizeable( bool resizable
)
3084 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizable
);
3087 void wxDataViewColumn::SetAlignment( wxAlignment align
)
3089 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3091 gfloat xalign
= 0.0;
3092 if (align
== wxALIGN_RIGHT
)
3094 if (align
== wxALIGN_CENTER_HORIZONTAL
||
3095 align
== wxALIGN_CENTER
)
3098 gtk_tree_view_column_set_alignment( column
, xalign
);
3100 if (m_renderer
&& m_renderer
->GetAlignment() == -1)
3101 m_renderer
->GtkUpdateAlignment();
3104 wxAlignment
wxDataViewColumn::GetAlignment() const
3106 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
3109 return wxALIGN_RIGHT
;
3111 return wxALIGN_CENTER_HORIZONTAL
;
3113 return wxALIGN_LEFT
;
3116 void wxDataViewColumn::SetSortable( bool sortable
)
3118 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3122 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
3126 gtk_tree_view_column_set_sort_column_id( column
, -1 );
3127 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
3128 gtk_tree_view_column_set_clickable( column
, FALSE
);
3132 bool wxDataViewColumn::IsSortable() const
3134 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3135 return gtk_tree_view_column_get_clickable( column
) != 0;
3138 bool wxDataViewColumn::IsSortKey() const
3140 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3141 return gtk_tree_view_column_get_sort_indicator( column
) != 0;
3144 bool wxDataViewColumn::IsResizeable() const
3146 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3147 return gtk_tree_view_column_get_resizable( column
) != 0;
3150 bool wxDataViewColumn::IsHidden() const
3152 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3153 return !gtk_tree_view_column_get_visible( column
);
3156 void wxDataViewColumn::SetSortOrder( bool ascending
)
3158 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3161 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
3163 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
3165 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
3168 bool wxDataViewColumn::IsSortOrderAscending() const
3170 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3172 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
3175 void wxDataViewColumn::SetMinWidth( int width
)
3177 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3180 int wxDataViewColumn::GetMinWidth() const
3182 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3185 int wxDataViewColumn::GetWidth() const
3187 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3190 void wxDataViewColumn::SetWidth( int width
)
3192 if ( width
== wxCOL_WIDTH_AUTOSIZE
)
3194 // NB: this disables user resizing
3195 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
3199 if ( width
== wxCOL_WIDTH_DEFAULT
)
3201 // TODO find a better calculation
3202 width
= wxDVC_DEFAULT_WIDTH
;
3205 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_FIXED
);
3206 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3210 void wxDataViewColumn::SetReorderable( bool reorderable
)
3212 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column
), reorderable
);
3215 bool wxDataViewColumn::IsReorderable() const
3217 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column
) ) != 0;
3220 //-----------------------------------------------------------------------------
3221 // wxGtkTreeModelNode
3222 //-----------------------------------------------------------------------------
3225 class wxGtkTreeModelChildWithPos
3233 int wxGtkTreeModelChildWithPosCmp( const void* data1
, const void* data2
, const void* user_data
)
3235 const wxGtkTreeModelChildWithPos
* child1
= (const wxGtkTreeModelChildWithPos
*) data1
;
3236 const wxGtkTreeModelChildWithPos
* child2
= (const wxGtkTreeModelChildWithPos
*) data2
;
3237 const wxDataViewCtrlInternal
*internal
= (const wxDataViewCtrlInternal
*) user_data
;
3238 int ret
= internal
->GetDataViewModel()->Compare( child1
->id
, child2
->id
,
3239 internal
->GetSortColumn(), (internal
->GetSortOrder() == GTK_SORT_DESCENDING
) );
3245 int LINKAGEMODE
wxGtkTreeModelChildPtrCmp( void*** data1
, void*** data2
)
3247 return gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(**data1
), wxDataViewItem(**data2
),
3248 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
3251 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr
);
3254 void wxGtkTreeModelNode::Resort()
3256 size_t child_count
= GetChildCount();
3257 if (child_count
== 0)
3260 size_t node_count
= GetNodesCount();
3262 if (child_count
== 1)
3264 if (node_count
== 1)
3266 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
3272 gint
*new_order
= new gint
[child_count
];
3275 // m_children has the original *void
3276 // ptrs points to these
3277 wxGtkTreeModelChildrenPtr ptrs
;
3279 for (i
= 0; i
< child_count
; i
++)
3280 ptrs
.Add( &(m_children
[i
]) );
3282 gs_internal
= m_internal
;
3283 ptrs
.Sort( &wxGtkTreeModelChildPtrCmp
);
3285 wxGtkTreeModelChildren temp
;
3286 void** base_ptr
= &(m_children
[0]);
3287 // Transfer positions to new_order array and
3289 for (i
= 0; i
< child_count
; i
++)
3291 new_order
[i
] = ptrs
[i
] - base_ptr
;
3292 temp
.Add( *ptrs
[i
] );
3295 // Transfer IDs back to m_children
3297 WX_APPEND_ARRAY( temp
, m_children
);
3302 // Build up array with IDs and original positions
3303 wxGtkTreeModelChildWithPos
* temp
= new wxGtkTreeModelChildWithPos
[child_count
];
3305 for (i
= 0; i
< child_count
; i
++)
3308 temp
[i
].id
= m_children
[i
];
3310 // Sort array keeping original positions
3311 wxQsort( temp
, child_count
, sizeof(wxGtkTreeModelChildWithPos
),
3312 &wxGtkTreeModelChildWithPosCmp
, m_internal
);
3313 // Transfer positions to new_order array and
3314 // IDs to m_children
3316 for (i
= 0; i
< child_count
; i
++)
3318 new_order
[i
] = temp
[i
].pos
;
3319 m_children
.Add( temp
[i
].id
);
3328 wxGtkTreeModelChildren temp
;
3329 WX_APPEND_ARRAY( temp
, m_children
);
3331 gs_internal
= m_internal
;
3332 m_children
.Sort( &wxGtkTreeModelChildCmp
);
3335 for (pos
= 0; pos
< child_count
; pos
++)
3337 void *id
= m_children
.Item( pos
);
3338 int old_pos
= temp
.Index( id
);
3339 new_order
[pos
] = old_pos
;
3343 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
3346 iter
.user_data
= GetItem().GetID();
3347 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
3349 gtk_tree_model_rows_reordered( gtk_tree_model
,
3350 wxGtkTreePath(m_internal
->get_path(&iter
)), &iter
, new_order
);
3352 delete [] new_order
;
3355 for (pos
= 0; pos
< node_count
; pos
++)
3357 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
3362 //-----------------------------------------------------------------------------
3363 // wxDataViewCtrlInternal
3364 //-----------------------------------------------------------------------------
3366 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
)
3369 m_wx_model
= wx_model
;
3372 m_sort_order
= GTK_SORT_ASCENDING
;
3374 m_dataview_sort_column
= NULL
;
3376 m_dragDataObject
= NULL
;
3377 m_dropDataObject
= NULL
;
3381 m_gtk_model
= wxgtk_tree_model_new();
3382 m_gtk_model
->internal
= this;
3384 m_notifier
= new wxGtkDataViewModelNotifier( wx_model
, this );
3386 wx_model
->AddNotifier( m_notifier
);
3388 // g_object_unref( gtk_model ); ???
3390 if (!m_wx_model
->IsVirtualListModel())
3393 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner
->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model
) );
3396 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3398 m_wx_model
->RemoveNotifier( m_notifier
);
3400 // remove the model from the GtkTreeView before it gets destroyed
3401 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner
->GtkGetTreeView() ), NULL
);
3403 g_object_unref( m_gtk_model
);
3405 delete m_dragDataObject
;
3406 delete m_dropDataObject
;
3409 void wxDataViewCtrlInternal::ScheduleRefresh()
3414 void wxDataViewCtrlInternal::OnInternalIdle()
3418 GtkWidget
*widget
= m_owner
->GtkGetTreeView();
3419 gtk_widget_queue_draw( widget
);
3424 void wxDataViewCtrlInternal::InitTree()
3426 wxDataViewItem item
;
3427 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
3429 BuildBranch( m_root
);
3432 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
3434 if (node
->GetChildCount() == 0)
3436 wxDataViewItemArray children
;
3437 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
3440 for (pos
= 0; pos
< count
; pos
++)
3442 wxDataViewItem child
= children
[pos
];
3444 if (m_wx_model
->IsContainer( child
))
3445 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
3447 node
->AddLeaf( child
.GetID() );
3449 // Don't send any events here
3456 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat
&format
)
3458 wxGtkString
atom_str( gdk_atom_name( format
) );
3459 m_dragSourceTargetEntryTarget
= wxCharBuffer( atom_str
);
3461 m_dragSourceTargetEntry
.target
= m_dragSourceTargetEntryTarget
.data();
3462 m_dragSourceTargetEntry
.flags
= 0;
3463 m_dragSourceTargetEntry
.info
= static_cast<guint
>(-1);
3465 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3466 GDK_BUTTON1_MASK
, &m_dragSourceTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3471 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat
&format
)
3473 wxGtkString
atom_str( gdk_atom_name( format
) );
3474 m_dropTargetTargetEntryTarget
= wxCharBuffer( atom_str
);
3476 m_dropTargetTargetEntry
.target
= m_dropTargetTargetEntryTarget
.data();
3477 m_dropTargetTargetEntry
.flags
= 0;
3478 m_dropTargetTargetEntry
.info
= static_cast<guint
>(-1);
3480 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3481 &m_dropTargetTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3486 gboolean
wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource
*WXUNUSED(drag_source
),
3489 delete m_dragDataObject
;
3490 m_dragDataObject
= NULL
;
3492 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3496 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3497 event
.SetEventObject( m_owner
);
3498 event
.SetItem( item
);
3499 event
.SetModel( m_wx_model
);
3501 gtk_widget_get_pointer(m_owner
->GtkGetTreeView(), &x
, &y
);
3502 event
.SetPosition(x
, y
);
3503 if (!m_owner
->HandleWindowEvent( event
))
3506 if (!event
.IsAllowed())
3509 wxDataObject
*obj
= event
.GetDataObject();
3513 m_dragDataObject
= obj
;
3519 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource
*WXUNUSED(drag_source
),
3520 GtkTreePath
*WXUNUSED(path
))
3525 gboolean
wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource
*WXUNUSED(drag_source
),
3526 GtkTreePath
*path
, GtkSelectionData
*selection_data
)
3528 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3532 GdkAtom target
= gtk_selection_data_get_target(selection_data
);
3533 if (!m_dragDataObject
->IsSupported(target
))
3536 size_t size
= m_dragDataObject
->GetDataSize(target
);
3540 void *buf
= malloc( size
);
3542 gboolean res
= FALSE
;
3543 if (m_dragDataObject
->GetDataHere(target
, buf
))
3547 gtk_selection_data_set(selection_data
, target
,
3548 8, (const guchar
*) buf
, size
);
3557 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3559 GtkSelectionData
*selection_data
)
3561 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3565 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
3566 event
.SetEventObject( m_owner
);
3567 event
.SetItem( item
);
3568 event
.SetModel( m_wx_model
);
3569 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3570 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3571 event
.SetDataBuffer(const_cast<guchar
*>(gtk_selection_data_get_data(selection_data
)));
3572 if (!m_owner
->HandleWindowEvent( event
))
3575 if (!event
.IsAllowed())
3582 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3584 GtkSelectionData
*selection_data
)
3586 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3590 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
3591 event
.SetEventObject( m_owner
);
3592 event
.SetItem( item
);
3593 event
.SetModel( m_wx_model
);
3594 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3595 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3596 if (!m_owner
->HandleWindowEvent( event
))
3599 if (!event
.IsAllowed())
3605 // notifications from wxDataViewModel
3607 bool wxDataViewCtrlInternal::Cleared()
3622 void wxDataViewCtrlInternal::Resort()
3624 if (!m_wx_model
->IsVirtualListModel())
3630 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3632 if (!m_wx_model
->IsVirtualListModel())
3634 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3635 wxCHECK_MSG(parent_node
, false,
3636 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3638 wxDataViewItemArray modelSiblings
;
3639 m_wx_model
->GetChildren(parent
, modelSiblings
);
3640 const int modelSiblingsSize
= modelSiblings
.size();
3642 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
3643 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
3645 const wxGtkTreeModelChildren
& nodeSiblings
= parent_node
->GetChildren();
3646 const int nodeSiblingsSize
= nodeSiblings
.size();
3650 if ( posInModel
== modelSiblingsSize
- 1 )
3652 nodePos
= nodeSiblingsSize
;
3654 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
3656 // This is the simple case when our node tree already matches the
3657 // model and only this one item is missing.
3658 nodePos
= posInModel
;
3662 // It's possible that a larger discrepancy between the model and
3663 // our realization exists. This can happen e.g. when adding a bunch
3664 // of items to the model and then calling ItemsAdded() just once
3665 // afterwards. In this case, we must find the right position by
3666 // looking at sibling items.
3668 // append to the end if we won't find a better position:
3669 nodePos
= nodeSiblingsSize
;
3671 for ( int nextItemPos
= posInModel
+ 1;
3672 nextItemPos
< modelSiblingsSize
;
3675 int nextNodePos
= parent_node
->FindChildByItem(modelSiblings
[nextItemPos
]);
3676 if ( nextNodePos
!= wxNOT_FOUND
)
3678 nodePos
= nextNodePos
;
3684 if (m_wx_model
->IsContainer( item
))
3685 parent_node
->InsertNode( new wxGtkTreeModelNode( parent_node
, item
, this ), nodePos
);
3687 parent_node
->InsertLeaf( item
.GetID(), nodePos
);
3695 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3697 if (!m_wx_model
->IsVirtualListModel())
3699 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3700 wxASSERT_MSG(parent_node
,
3701 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3703 parent_node
->DeleteChild( item
.GetID() );
3711 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
3713 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3714 event
.SetEventObject( m_owner
);
3715 event
.SetModel( m_owner
->GetModel() );
3716 event
.SetItem( item
);
3717 m_owner
->HandleWindowEvent( event
);
3722 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int view_column
)
3724 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3725 event
.SetEventObject( m_owner
);
3726 event
.SetModel( m_owner
->GetModel() );
3727 event
.SetColumn( view_column
);
3728 event
.SetDataViewColumn( GetOwner()->GetColumn(view_column
) );
3729 event
.SetItem( item
);
3730 m_owner
->HandleWindowEvent( event
);
3737 GtkTreeModelFlags
wxDataViewCtrlInternal::get_flags()
3741 if ( m_wx_model
->IsListModel() )
3742 flags
|= GTK_TREE_MODEL_LIST_ONLY
;
3744 if ( !m_wx_model
->IsVirtualListModel() )
3745 flags
|= GTK_TREE_MODEL_ITERS_PERSIST
;
3747 return GtkTreeModelFlags(flags
);
3750 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
3753 if (m_wx_model
->IsVirtualListModel())
3755 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3757 unsigned int i
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
3759 if (i
>= wx_model
->GetCount())
3762 iter
->stamp
= m_gtk_model
->stamp
;
3763 // user_data is just the index +1
3764 iter
->user_data
= wxUIntToPtr(i
+1);
3770 int depth
= gtk_tree_path_get_depth( path
);
3772 wxGtkTreeModelNode
*node
= m_root
;
3775 for (i
= 0; i
< depth
; i
++)
3777 BuildBranch( node
);
3779 gint pos
= gtk_tree_path_get_indices (path
)[i
];
3780 if (pos
< 0) return FALSE
;
3781 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
3783 void* id
= node
->GetChildren().Item( (size_t) pos
);
3787 iter
->stamp
= m_gtk_model
->stamp
;
3788 iter
->user_data
= id
;
3792 size_t count
= node
->GetNodes().GetCount();
3794 for (pos2
= 0; pos2
< count
; pos2
++)
3796 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
3797 if (child_node
->GetItem().GetID() == id
)
3809 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
3811 // When this is called from ItemDeleted(), the item is already
3812 // deleted in the model.
3814 GtkTreePath
*retval
= gtk_tree_path_new ();
3816 if (m_wx_model
->IsVirtualListModel())
3818 // iter is root, add nothing
3819 if (!iter
->user_data
)
3822 // user_data is just the index +1
3823 int i
= ( (wxUIntPtr
) iter
->user_data
) -1;
3824 gtk_tree_path_append_index (retval
, i
);
3828 void *id
= iter
->user_data
;
3830 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
3833 int pos
= node
->GetChildren().Index( id
);
3835 gtk_tree_path_prepend_index( retval
, pos
);
3837 id
= node
->GetItem().GetID();
3838 node
= node
->GetParent();
3845 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
3847 if (m_wx_model
->IsVirtualListModel())
3849 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3851 // user_data is just the index +1
3852 int n
= ( (wxUIntPtr
) iter
->user_data
) -1;
3856 iter
->user_data
= NULL
;
3860 if (n
>= (int) wx_model
->GetCount()-1)
3862 iter
->user_data
= NULL
;
3866 // user_data is just the index +1 (+2 because we need the next)
3867 iter
->user_data
= wxUIntToPtr(n
+2);
3871 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
3872 if( parent
== NULL
)
3874 iter
->user_data
= NULL
;
3878 int pos
= parent
->GetChildren().Index( iter
->user_data
);
3880 if (pos
== (int) parent
->GetChildCount()-1)
3882 iter
->user_data
= NULL
;
3886 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
3892 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
3894 if (m_wx_model
->IsVirtualListModel())
3896 // this is a list, nodes have no children
3900 iter
->stamp
= m_gtk_model
->stamp
;
3901 iter
->user_data
= (gpointer
) 1;
3909 if (m_root
->GetChildCount() == 0) return FALSE
;
3910 iter
->stamp
= m_gtk_model
->stamp
;
3911 iter
->user_data
= (gpointer
) m_root
->GetChildren().Item( 0 );
3916 wxDataViewItem item
;
3918 item
= wxDataViewItem( (void*) parent
->user_data
);
3920 if (!m_wx_model
->IsContainer( item
))
3923 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3924 wxASSERT_MSG(parent_node
,
3925 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3927 BuildBranch( parent_node
);
3929 if (parent_node
->GetChildCount() == 0)
3932 iter
->stamp
= m_gtk_model
->stamp
;
3933 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
3939 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
3941 if (m_wx_model
->IsVirtualListModel())
3943 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3946 return (wx_model
->GetCount() > 0);
3948 // this is a list, nodes have no children
3954 return (m_root
->GetChildCount() > 0);
3956 wxDataViewItem
item( (void*) iter
->user_data
);
3958 bool is_container
= m_wx_model
->IsContainer( item
);
3963 wxGtkTreeModelNode
*node
= FindNode( iter
);
3965 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3967 BuildBranch( node
);
3969 return (node
->GetChildCount() > 0);
3973 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
3975 if (m_wx_model
->IsVirtualListModel())
3977 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3980 return (gint
) wx_model
->GetCount();
3987 return m_root
->GetChildCount();
3989 wxDataViewItem
item( (void*) iter
->user_data
);
3991 if (!m_wx_model
->IsContainer( item
))
3994 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
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 return parent_node
->GetChildCount();
4004 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
4006 if (m_wx_model
->IsVirtualListModel())
4008 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
4016 if (n
>= (gint
) wx_model
->GetCount())
4019 iter
->stamp
= m_gtk_model
->stamp
;
4020 // user_data is just the index +1
4021 iter
->user_data
= wxUIntToPtr(n
+1);
4028 if (parent
) id
= (void*) parent
->user_data
;
4029 wxDataViewItem
item( id
);
4031 if (!m_wx_model
->IsContainer( item
))
4034 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4035 wxASSERT_MSG(parent_node
,
4036 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4038 BuildBranch( parent_node
);
4040 iter
->stamp
= m_gtk_model
->stamp
;
4041 iter
->user_data
= parent_node
->GetChildren().Item( n
);
4047 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
4049 if (m_wx_model
->IsVirtualListModel())
4055 wxGtkTreeModelNode
*node
= FindParentNode( child
);
4059 iter
->stamp
= m_gtk_model
->stamp
;
4060 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
4066 // item can be deleted already in the model
4067 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
4069 if (m_wx_model
->IsVirtualListModel())
4071 return wxPtrToUInt(item
.GetID()) - 1;
4075 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4076 wxGtkTreeModelChildren
&children
= parent_node
->GetChildren();
4078 for (j
= 0; j
< children
.GetCount(); j
++)
4080 if (children
[j
] == item
.GetID())
4088 static wxGtkTreeModelNode
*
4089 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4095 list
.DeleteContents( true );
4096 wxDataViewItem
it( item
);
4100 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4101 list
.Insert( pItem
);
4102 it
= model
->GetParent( it
);
4105 wxGtkTreeModelNode
* node
= treeNode
;
4106 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4108 if( node
&& node
->GetNodes().GetCount() != 0 )
4110 int len
= node
->GetNodes().GetCount();
4111 wxGtkTreeModelNodes
&nodes
= node
->GetNodes();
4113 for( ; j
< len
; j
++)
4115 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4134 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
4139 wxDataViewItem
item( (void*) iter
->user_data
);
4143 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4148 wxLogDebug( "Not found %p", iter->user_data );
4152 // TODO: remove this code
4158 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
4163 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4168 wxLogDebug( "Not found %p", item.GetID() );
4172 // TODO: remove this code
4178 static wxGtkTreeModelNode
*
4179 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4185 list
.DeleteContents( true );
4189 wxDataViewItem
it( model
->GetParent( item
) );
4192 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4193 list
.Insert( pItem
);
4194 it
= model
->GetParent( it
);
4197 wxGtkTreeModelNode
* node
= treeNode
;
4198 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4200 if( node
&& node
->GetNodes().GetCount() != 0 )
4202 int len
= node
->GetNodes().GetCount();
4203 wxGtkTreeModelNodes nodes
= node
->GetNodes();
4205 for( ; j
< len
; j
++)
4207 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4222 //Examine whether the node is item's parent node
4223 int len
= node
->GetChildCount();
4224 for( int i
= 0; i
< len
; i
++ )
4226 if( node
->GetChildren().Item( i
) == item
.GetID() )
4232 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
4237 wxDataViewItem
item( (void*) iter
->user_data
);
4241 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4244 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
4249 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4252 //-----------------------------------------------------------------------------
4253 // wxDataViewCtrl signal callbacks
4254 //-----------------------------------------------------------------------------
4257 wxdataview_selection_changed_callback( GtkTreeSelection
* WXUNUSED(selection
), wxDataViewCtrl
*dv
)
4259 if (!gtk_widget_get_realized(dv
->m_widget
))
4262 wxDataViewEvent
event( wxEVT_DATAVIEW_SELECTION_CHANGED
, dv
->GetId() );
4263 event
.SetItem( dv
->GetSelection() );
4264 event
.SetModel( dv
->GetModel() );
4265 dv
->HandleWindowEvent( event
);
4269 wxdataview_row_activated_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreePath
*path
,
4270 GtkTreeViewColumn
*WXUNUSED(column
), wxDataViewCtrl
*dv
)
4272 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
4274 wxDataViewItem
item(dv
->GTKPathToItem(path
));
4275 event
.SetItem( item
);
4276 event
.SetModel( dv
->GetModel() );
4277 dv
->HandleWindowEvent( event
);
4281 wxdataview_test_expand_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4282 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4284 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_EXPANDING
, dv
->GetId() );
4286 wxDataViewItem
item( (void*) iter
->user_data
);;
4287 event
.SetItem( item
);
4288 event
.SetModel( dv
->GetModel() );
4289 dv
->HandleWindowEvent( event
);
4291 return !event
.IsAllowed();
4295 wxdataview_row_expanded_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4296 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4298 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_EXPANDED
, dv
->GetId() );
4300 wxDataViewItem
item( (void*) iter
->user_data
);;
4301 event
.SetItem( item
);
4302 event
.SetModel( dv
->GetModel() );
4303 dv
->HandleWindowEvent( event
);
4307 wxdataview_test_collapse_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4308 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4310 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_COLLAPSING
, dv
->GetId() );
4312 wxDataViewItem
item( (void*) iter
->user_data
);;
4313 event
.SetItem( item
);
4314 event
.SetModel( dv
->GetModel() );
4315 dv
->HandleWindowEvent( event
);
4317 return !event
.IsAllowed();
4321 wxdataview_row_collapsed_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4322 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4324 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_COLLAPSED
, dv
->GetId() );
4326 wxDataViewItem
item( (void*) iter
->user_data
);;
4327 event
.SetItem( item
);
4328 event
.SetModel( dv
->GetModel() );
4329 dv
->HandleWindowEvent( event
);
4332 //-----------------------------------------------------------------------------
4334 //-----------------------------------------------------------------------------
4336 void wxDataViewCtrl::AddChildGTK(wxWindowGTK
* child
)
4338 GtkWidget
* treeview
= GtkGetTreeView();
4340 // Insert widget in GtkTreeView
4341 if (gtk_widget_get_realized(treeview
))
4342 gtk_widget_set_parent_window( child
->m_widget
,
4343 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
4344 gtk_widget_set_parent( child
->m_widget
, treeview
);
4348 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
4349 GtkAllocation
*WXUNUSED(gtk_alloc
),
4350 wxDataViewCtrl
*win
)
4352 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
4355 wxWindow
*child
= node
->GetData();
4358 gtk_widget_get_preferred_size(child
->m_widget
, NULL
, &req
);
4360 GtkAllocation alloc
;
4361 alloc
.x
= child
->m_x
;
4362 alloc
.y
= child
->m_y
;
4363 alloc
.width
= child
->m_width
;
4364 alloc
.height
= child
->m_height
;
4365 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
4367 node
= node
->GetNext();
4372 //-----------------------------------------------------------------------------
4373 // "motion_notify_event"
4374 //-----------------------------------------------------------------------------
4377 gtk_dataview_motion_notify_callback( GtkWidget
*WXUNUSED(widget
),
4378 GdkEventMotion
*gdk_event
,
4379 wxDataViewCtrl
*dv
)
4381 int x
= gdk_event
->x
;
4382 int y
= gdk_event
->y
;
4383 if (gdk_event
->is_hint
)
4386 gdk_window_get_device_position(gdk_event
->window
, gdk_event
->device
, &x
, &y
, NULL
);
4388 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, NULL
);
4393 GtkTreeViewColumn
*column
= NULL
;
4396 if (gtk_tree_view_get_path_at_pos(
4397 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4407 dv
->GtkGetInternal()->get_iter( &iter
, path
);
4415 //-----------------------------------------------------------------------------
4416 // "button_press_event"
4417 //-----------------------------------------------------------------------------
4420 gtk_dataview_button_press_callback( GtkWidget
*WXUNUSED(widget
),
4421 GdkEventButton
*gdk_event
,
4422 wxDataViewCtrl
*dv
)
4424 if ((gdk_event
->button
== 3) && (gdk_event
->type
== GDK_BUTTON_PRESS
))
4427 GtkTreeViewColumn
*column
= NULL
;
4430 gtk_tree_view_get_path_at_pos
4432 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4433 (int) gdk_event
->x
, (int) gdk_event
->y
,
4440 wxDataViewEvent
event( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU
, dv
->GetId() );
4442 event
.SetItem(dv
->GTKPathToItem(path
));
4443 event
.SetModel( dv
->GetModel() );
4444 return dv
->HandleWindowEvent( event
);
4450 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4452 wxDataViewCtrl::~wxDataViewCtrl()
4454 // Stop editing before destroying the control to remove any event handlers
4455 // which are added when editing started: if we didn't do this, the base
4456 // class dtor would assert as it checks for any leftover handlers.
4459 GtkTreeViewColumn
*col
;
4460 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4462 wxDataViewColumn
* const wxcol
= FromGTKColumn(col
);
4465 // This won't do anything if we're not editing it
4466 wxcol
->GetRenderer()->CancelEditing();
4469 GTKDisconnect(m_treeview
);
4470 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
));
4472 GTKDisconnect(selection
);
4480 void wxDataViewCtrl::Init()
4485 m_cols
.DeleteContents( true );
4487 m_uniformRowHeight
= -1;
4490 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4495 const wxValidator
& validator
,
4496 const wxString
& name
)
4498 if (!PreCreation( parent
, pos
, size
) ||
4499 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
4501 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4505 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
4506 g_object_ref(m_widget
);
4508 GTKScrolledWindowSetBorder(m_widget
, style
);
4510 m_treeview
= gtk_tree_view_new();
4511 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
4513 m_focusWidget
= GTK_WIDGET(m_treeview
);
4515 g_signal_connect (m_treeview
, "size_allocate",
4516 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
4518 bool fixed
= (style
& wxDV_VARIABLE_LINE_HEIGHT
) == 0;
4519 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4521 if (style
& wxDV_MULTIPLE
)
4523 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4524 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
4527 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
4531 if (!gtk_check_version(2,10,0))
4534 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
4536 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
4537 (style
& wxDV_VERT_RULES
) != 0)
4538 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
4539 else if (style
& wxDV_VERT_RULES
)
4540 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
4541 else if (style
& wxDV_HORIZ_RULES
)
4542 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
4544 if (grid
!= GTK_TREE_VIEW_GRID_LINES_NONE
)
4545 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
4549 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_ROW_LINES
) != 0 );
4551 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
4552 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
4553 gtk_widget_show (m_treeview
);
4555 m_parent
->DoAddChild( this );
4559 GtkEnableSelectionEvents();
4561 g_signal_connect_after (m_treeview
, "row-activated",
4562 G_CALLBACK (wxdataview_row_activated_callback
), this);
4564 g_signal_connect (m_treeview
, "test-collapse-row",
4565 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
4567 g_signal_connect_after (m_treeview
, "row-collapsed",
4568 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
4570 g_signal_connect (m_treeview
, "test-expand-row",
4571 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
4573 g_signal_connect_after (m_treeview
, "row-expanded",
4574 G_CALLBACK (wxdataview_row_expanded_callback
), this);
4576 g_signal_connect (m_treeview
, "motion_notify_event",
4577 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
4579 g_signal_connect (m_treeview
, "button_press_event",
4580 G_CALLBACK (gtk_dataview_button_press_callback
), this);
4585 wxDataViewItem
wxDataViewCtrl::GTKPathToItem(GtkTreePath
*path
) const
4588 return wxDataViewItem(path
&& m_internal
->get_iter(&iter
, path
)
4593 void wxDataViewCtrl::OnInternalIdle()
4595 wxWindow::OnInternalIdle();
4597 m_internal
->OnInternalIdle();
4599 unsigned int cols
= GetColumnCount();
4601 for (i
= 0; i
< cols
; i
++)
4603 wxDataViewColumn
*col
= GetColumn( i
);
4604 col
->OnInternalIdle();
4607 if (m_ensureVisibleDefered
.IsOk())
4609 ExpandAncestors(m_ensureVisibleDefered
);
4611 iter
.user_data
= (gpointer
) m_ensureVisibleDefered
.GetID();
4612 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4613 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
4614 m_ensureVisibleDefered
= wxDataViewItem(0);
4618 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4620 wxDELETE(m_internal
);
4622 if (!wxDataViewCtrlBase::AssociateModel( model
))
4625 bool fixed
= (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
) == 0) || (model
->IsVirtualListModel()));
4626 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4628 m_internal
= new wxDataViewCtrlInternal( this, model
);
4633 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4635 return m_internal
->EnableDragSource( format
);
4638 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4640 return m_internal
->EnableDropTarget( format
);
4643 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4645 if (!wxDataViewCtrlBase::AppendColumn(col
))
4648 m_cols
.Append( col
);
4650 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4651 GTK_TREE_VIEW_COLUMN_FIXED
)
4653 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4656 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
4657 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4662 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4664 if (!wxDataViewCtrlBase::PrependColumn(col
))
4667 m_cols
.Insert( col
);
4669 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4670 GTK_TREE_VIEW_COLUMN_FIXED
)
4672 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4675 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4676 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
4681 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4683 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4686 m_cols
.Insert( pos
, col
);
4688 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4689 GTK_TREE_VIEW_COLUMN_FIXED
)
4691 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4694 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4695 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), pos
);
4700 unsigned int wxDataViewCtrl::GetColumnCount() const
4702 return m_cols
.GetCount();
4705 wxDataViewColumn
* wxDataViewCtrl::FromGTKColumn(GtkTreeViewColumn
*gtk_col
) const
4710 wxDataViewColumnList::const_iterator iter
;
4711 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4713 wxDataViewColumn
*col
= *iter
;
4714 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
4720 wxFAIL_MSG( "No matching column?" );
4725 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
4727 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
4729 return FromGTKColumn(gtk_col
);
4732 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4734 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4735 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
4737 m_cols
.DeleteObject( column
);
4742 bool wxDataViewCtrl::ClearColumns()
4744 wxDataViewColumnList::iterator iter
;
4745 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4747 wxDataViewColumn
*col
= *iter
;
4748 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4749 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4757 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4759 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4761 wxGtkList
list(gtk_tree_view_get_columns(GTK_TREE_VIEW(m_treeview
)));
4763 return g_list_index( list
, (gconstpointer
) gtk_column
);
4766 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4768 return m_internal
->GetDataViewSortColumn();
4771 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4774 iter
.user_data
= item
.GetID();
4775 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4776 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
4779 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4782 iter
.user_data
= item
.GetID();
4783 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4784 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
4787 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4790 iter
.user_data
= item
.GetID();
4791 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4792 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview
), path
) != 0;
4795 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4797 // The tree doesn't have any current item if it hadn't been created yet but
4798 // it's arguably not an error to call this function in this case so just
4799 // return an invalid item without asserting.
4801 return wxDataViewItem();
4804 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), path
.ByRef(), NULL
);
4806 return GTKPathToItem(path
);
4809 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4811 wxCHECK_RET( m_treeview
,
4812 "Current item can't be set before creating the control." );
4814 // We need to make sure the model knows about this item or the path would
4815 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4816 ExpandAncestors(item
);
4818 // We also need to preserve the existing selection from changing.
4819 // Unfortunately the only way to do it seems to use our own selection
4820 // function and forbid any selection changes during set cursor call.
4821 wxGtkTreeSelectionLock
4822 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4824 // Do move the cursor now.
4826 iter
.user_data
= item
.GetID();
4827 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4829 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, NULL
, FALSE
);
4832 wxDataViewColumn
*wxDataViewCtrl::GetCurrentColumn() const
4834 // The tree doesn't have any current item if it hadn't been created yet but
4835 // it's arguably not an error to call this function in this case so just
4836 // return NULL without asserting.
4840 GtkTreeViewColumn
*col
;
4841 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4842 return FromGTKColumn(col
);
4845 void wxDataViewCtrl::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
4847 wxCHECK_RET( m_treeview
,
4848 "Current item can't be set before creating the control." );
4849 wxCHECK_RET( item
.IsOk(), "invalid item" );
4850 wxCHECK_RET( column
, "no column provided" );
4852 // We need to make sure the model knows about this item or the path would
4853 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4854 ExpandAncestors(item
);
4856 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4858 // We also need to preserve the existing selection from changing.
4859 // Unfortunately the only way to do it seems to use our own selection
4860 // function and forbid any selection changes during set cursor call.
4861 wxGtkTreeSelectionLock
4862 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4864 // Do move the cursor now.
4866 iter
.user_data
= item
.GetID();
4867 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4869 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, gcolumn
, TRUE
);
4872 int wxDataViewCtrl::GetSelectedItemsCount() const
4874 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4876 return gtk_tree_selection_count_selected_rows(selection
);
4879 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4883 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4884 if (HasFlag(wxDV_MULTIPLE
))
4886 GtkTreeModel
*model
;
4887 wxGtkTreePathList
list(gtk_tree_selection_get_selected_rows(selection
, &model
));
4889 for ( GList
* current
= list
; current
; current
= g_list_next(current
) )
4891 GtkTreePath
*path
= (GtkTreePath
*) current
->data
;
4893 sel
.Add(GTKPathToItem(path
));
4899 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
4901 sel
.Add( wxDataViewItem(iter
.user_data
) );
4908 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4910 GtkDisableSelectionEvents();
4912 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4914 gtk_tree_selection_unselect_all( selection
);
4916 wxDataViewItem last_parent
;
4919 for (i
= 0; i
< sel
.GetCount(); i
++)
4921 wxDataViewItem item
= sel
[i
];
4922 wxDataViewItem parent
= GetModel()->GetParent( item
);
4925 if (parent
!= last_parent
)
4926 ExpandAncestors(item
);
4928 last_parent
= parent
;
4931 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4932 iter
.user_data
= (gpointer
) item
.GetID();
4933 gtk_tree_selection_select_iter( selection
, &iter
);
4936 GtkEnableSelectionEvents();
4939 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4941 ExpandAncestors(item
);
4943 GtkDisableSelectionEvents();
4945 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4948 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4949 iter
.user_data
= (gpointer
) item
.GetID();
4950 gtk_tree_selection_select_iter( selection
, &iter
);
4952 GtkEnableSelectionEvents();
4955 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4957 GtkDisableSelectionEvents();
4959 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4962 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4963 iter
.user_data
= (gpointer
) item
.GetID();
4964 gtk_tree_selection_unselect_iter( selection
, &iter
);
4966 GtkEnableSelectionEvents();
4969 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4971 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4974 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4975 iter
.user_data
= (gpointer
) item
.GetID();
4977 return gtk_tree_selection_iter_is_selected( selection
, &iter
) != 0;
4980 void wxDataViewCtrl::SelectAll()
4982 GtkDisableSelectionEvents();
4984 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4986 gtk_tree_selection_select_all( selection
);
4988 GtkEnableSelectionEvents();
4991 void wxDataViewCtrl::UnselectAll()
4993 GtkDisableSelectionEvents();
4995 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4997 gtk_tree_selection_unselect_all( selection
);
4999 GtkEnableSelectionEvents();
5002 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem
& item
,
5003 const wxDataViewColumn
*WXUNUSED(column
))
5005 m_ensureVisibleDefered
= item
;
5006 ExpandAncestors(item
);
5009 iter
.user_data
= (gpointer
) item
.GetID();
5010 wxGtkTreePath
path(m_internal
->get_path( &iter
));
5011 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
5014 void wxDataViewCtrl::HitTest(const wxPoint
& point
,
5015 wxDataViewItem
& item
,
5016 wxDataViewColumn
*& column
) const
5018 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
5019 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
5020 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
5021 // So we have to use both of them.
5022 item
= wxDataViewItem(0);
5024 wxGtkTreePath path
, pathScratch
;
5025 GtkTreeViewColumn
* GtkColumn
= NULL
;
5026 GtkTreeViewDropPosition pos
= GTK_TREE_VIEW_DROP_INTO_OR_AFTER
;
5030 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5031 wxDataViewCtrl
* self
= const_cast<wxDataViewCtrl
*>(this); // ugly workaround, self is NOT const
5032 GtkTreeView
* treeView
= GTK_TREE_VIEW(self
->GtkGetTreeView());
5034 // is there possibly a better suited function to get the column?
5035 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5037 (int) point
.x
, (int) point
.y
,
5038 pathScratch
.ByRef(),
5039 &GtkColumn
, // here we get the GtkColumn
5043 if ( GtkColumn
!= NULL
)
5045 // we got GTK column
5046 // the right call now which takes the header into account
5047 gtk_tree_view_get_dest_row_at_pos( treeView
, (int) point
.x
, (int) point
.y
, path
.ByRef(), &pos
);
5050 item
= wxDataViewItem(GTKPathToItem(path
));
5051 // else we got a GTK column but the position is not over an item, e.g. below last item
5052 for ( unsigned int i
=0, cols
=GetColumnCount(); i
<cols
; ++i
) // search the wx column
5054 wxDataViewColumn
* col
= GetColumn(i
);
5055 if ( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == GtkColumn
)
5057 column
= col
; // here we get the wx column
5062 // else no column and thus no item, both null
5066 wxDataViewCtrl::GetItemRect(const wxDataViewItem
& WXUNUSED(item
),
5067 const wxDataViewColumn
*WXUNUSED(column
)) const
5072 bool wxDataViewCtrl::SetRowHeight(int rowHeight
)
5074 m_uniformRowHeight
= rowHeight
;
5078 void wxDataViewCtrl::DoSetExpanderColumn()
5080 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
5081 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5084 void wxDataViewCtrl::DoSetIndent()
5088 void wxDataViewCtrl::GtkDisableSelectionEvents()
5090 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5091 g_signal_handlers_disconnect_by_func( selection
,
5092 (gpointer
) (wxdataview_selection_changed_callback
), this);
5095 void wxDataViewCtrl::GtkEnableSelectionEvents()
5097 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5098 g_signal_connect_after (selection
, "changed",
5099 G_CALLBACK (wxdataview_selection_changed_callback
), this);
5102 // ----------------------------------------------------------------------------
5103 // visual attributes stuff
5104 // ----------------------------------------------------------------------------
5108 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
5110 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new());
5113 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
5115 wxDataViewCtrlBase::DoApplyWidgetStyle(style
);
5116 GTKApplyStyle(m_treeview
, style
);
5119 #endif // !wxUSE_GENERICDATAVIEWCTRL
5121 #endif // wxUSE_DATAVIEWCTRL