1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataview.cpp
3 // Purpose: wxDataViewCtrl GTK+2 implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if wxUSE_DATAVIEWCTRL
15 #include "wx/dataview.h"
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
21 #include "wx/dcclient.h"
23 #include "wx/settings.h"
27 #include "wx/stockitem.h"
28 #include "wx/popupwin.h"
29 #include "wx/listimpl.cpp"
31 #include "wx/gtk/private.h"
32 #include "wx/gtk/dc.h"
33 #include "wx/gtk/dcclient.h"
35 #include "wx/gtk/private/gdkconv.h"
36 #include "wx/gtk/private/list.h"
37 #include "wx/gtk/private/event.h"
38 using namespace wxGTKImpl
;
40 class wxGtkDataViewModelNotifier
;
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static wxDataViewCtrlInternal
*gs_internal
= NULL
;
47 class wxGtkTreeModelNode
;
50 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
53 // ----------------------------------------------------------------------------
54 // wxGtkTreePath: self-destroying GtkTreePath
55 // ----------------------------------------------------------------------------
57 // Usually this object is initialized with the associated GtkTreePath
58 // immediately when it's constructed but it can also be changed later either by
59 // using Assign() or by getting the pointer to the internally stored pointer
60 // value using ByRef(). The latter should be avoided but is very convenient
61 // when using GTK functions with GtkTreePath output parameters.
65 // Ctor takes ownership of the given path and will free it if non-NULL.
66 wxGtkTreePath(GtkTreePath
*path
= NULL
) : m_path(path
) { }
68 // Creates a tree path for the given string path.
69 wxGtkTreePath(const gchar
*strpath
)
70 : m_path(gtk_tree_path_new_from_string(strpath
))
74 // Set the stored pointer if not done by ctor.
75 void Assign(GtkTreePath
*path
)
77 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
82 // Return the pointer to the internally stored pointer. This should only be
83 // used to initialize the object by passing it to some GTK function.
86 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
92 operator GtkTreePath
*() const { return m_path
; }
94 ~wxGtkTreePath() { if ( m_path
) gtk_tree_path_free(m_path
); }
99 wxDECLARE_NO_COPY_CLASS(wxGtkTreePath
);
102 // ----------------------------------------------------------------------------
103 // wxGtkTreePathList: self-destroying list of GtkTreePath objects.
104 // ----------------------------------------------------------------------------
106 class wxGtkTreePathList
: public wxGtkList
109 // Ctor takes ownership of the list.
110 explicit wxGtkTreePathList(GList
* list
)
117 // Delete the list contents, wxGtkList will delete the list itself.
118 g_list_foreach(m_list
, (GFunc
)gtk_tree_path_free
, NULL
);
122 // ----------------------------------------------------------------------------
123 // wxGtkTreeSelectionLock: prevent selection from changing during the
124 // lifetime of this object
125 // ----------------------------------------------------------------------------
127 // Implementation note: it could be expected that setting the selection
128 // function in this class ctor and resetting it back to the old value in its
129 // dtor would work. However currently gtk_tree_selection_get_select_function()
130 // can't be passed NULL (see https://bugzilla.gnome.org/show_bug.cgi?id=626276)
131 // so we can't do this. Instead, we always use the selection function (which
132 // imposes extra overhead, albeit minimal one, on all selection operations) and
133 // just set/reset the flag telling it whether it should allow or forbid the
136 // Also notice that currently only a single object of this class may exist at
137 // any given moment. It's just simpler like this and we don't need anything
141 gboolean
wxdataview_selection_func(GtkTreeSelection
* WXUNUSED(selection
),
142 GtkTreeModel
* WXUNUSED(model
),
143 GtkTreePath
* WXUNUSED(path
),
144 gboolean
WXUNUSED(path_currently_selected
),
150 class wxGtkTreeSelectionLock
153 wxGtkTreeSelectionLock(GtkTreeSelection
*selection
)
154 : m_selection(selection
)
156 wxASSERT_MSG( !ms_instance
, "this class is not reentrant currently" );
160 CheckCurrentSelectionFunc(NULL
);
162 // Pass some non-NULL pointer as "data" for the callback, it doesn't
163 // matter what it is as long as it's non-NULL.
164 gtk_tree_selection_set_select_function(selection
,
165 wxdataview_selection_func
,
170 ~wxGtkTreeSelectionLock()
172 CheckCurrentSelectionFunc(wxdataview_selection_func
);
174 gtk_tree_selection_set_select_function(m_selection
,
175 wxdataview_selection_func
,
183 void CheckCurrentSelectionFunc(GtkTreeSelectionFunc func
)
185 // We can only use gtk_tree_selection_get_select_function() with 2.14+
186 // so check for its availability both during compile- and run-time.
187 #if GTK_CHECK_VERSION(2, 14, 0)
188 if ( gtk_check_version(2, 14, 0) != NULL
)
191 // If this assert is triggered, it means the code elsewhere has called
192 // gtk_tree_selection_set_select_function() but currently doing this
193 // breaks this class so the code here needs to be changed.
196 gtk_tree_selection_get_select_function(m_selection
) == func
,
197 "selection function has changed unexpectedly, review this code!"
204 static wxGtkTreeSelectionLock
*ms_instance
;
206 GtkTreeSelection
* const m_selection
;
208 wxDECLARE_NO_COPY_CLASS(wxGtkTreeSelectionLock
);
211 wxGtkTreeSelectionLock
*wxGtkTreeSelectionLock::ms_instance
= NULL
;
213 //-----------------------------------------------------------------------------
214 // wxDataViewCtrlInternal
215 //-----------------------------------------------------------------------------
217 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
218 WX_DEFINE_LIST(ItemList
)
220 class wxDataViewCtrlInternal
223 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
);
224 ~wxDataViewCtrlInternal();
227 GtkTreeModelFlags
get_flags();
228 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
229 GtkTreePath
*get_path( GtkTreeIter
*iter
);
230 gboolean
iter_next( GtkTreeIter
*iter
);
231 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
232 gboolean
iter_has_child( GtkTreeIter
*iter
);
233 gint
iter_n_children( GtkTreeIter
*iter
);
234 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
235 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
239 bool EnableDragSource( const wxDataFormat
&format
);
240 bool EnableDropTarget( const wxDataFormat
&format
);
242 gboolean
row_draggable( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
);
243 gboolean
drag_data_delete( GtkTreeDragSource
*drag_source
, GtkTreePath
* path
);
244 gboolean
drag_data_get( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
,
245 GtkSelectionData
*selection_data
);
246 gboolean
drag_data_received( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest
,
247 GtkSelectionData
*selection_data
);
248 gboolean
row_drop_possible( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest_path
,
249 GtkSelectionData
*selection_data
);
251 // notifactions from wxDataViewModel
252 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
253 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
254 bool ItemChanged( const wxDataViewItem
&item
);
255 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
262 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
263 GtkSortType
GetSortOrder() const { return m_sort_order
; }
265 void SetSortColumn( int column
) { m_sort_column
= column
; }
266 int GetSortColumn() const { return m_sort_column
; }
268 void SetDataViewSortColumn( wxDataViewColumn
*column
) { m_dataview_sort_column
= column
; }
269 wxDataViewColumn
*GetDataViewSortColumn() { return m_dataview_sort_column
; }
271 bool IsSorted() const { return m_sort_column
>= 0; }
274 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
275 const wxDataViewModel
* GetDataViewModel() const { return m_wx_model
; }
276 wxDataViewCtrl
* GetOwner() { return m_owner
; }
277 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
279 // item can be deleted already in the model
280 int GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
282 void OnInternalIdle();
286 void ScheduleRefresh();
288 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
289 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
290 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
291 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
292 void BuildBranch( wxGtkTreeModelNode
*branch
);
295 wxGtkTreeModelNode
*m_root
;
296 wxDataViewModel
*m_wx_model
;
297 GtkWxTreeModel
*m_gtk_model
;
298 wxDataViewCtrl
*m_owner
;
299 GtkSortType m_sort_order
;
300 wxDataViewColumn
*m_dataview_sort_column
;
303 GtkTargetEntry m_dragSourceTargetEntry
;
304 wxCharBuffer m_dragSourceTargetEntryTarget
;
305 wxDataObject
*m_dragDataObject
;
307 GtkTargetEntry m_dropTargetTargetEntry
;
308 wxCharBuffer m_dropTargetTargetEntryTarget
;
309 wxDataObject
*m_dropDataObject
;
311 wxGtkDataViewModelNotifier
*m_notifier
;
317 //-----------------------------------------------------------------------------
318 // wxGtkTreeModelNode
319 //-----------------------------------------------------------------------------
322 int LINKAGEMODE
wxGtkTreeModelChildCmp( void** id1
, void** id2
)
324 int ret
= gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(*id1
), wxDataViewItem(*id2
),
325 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
330 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
331 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren
);
333 class wxGtkTreeModelNode
336 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
337 wxDataViewCtrlInternal
*internal
)
341 m_internal
= internal
;
344 ~wxGtkTreeModelNode()
346 size_t count
= m_nodes
.GetCount();
348 for (i
= 0; i
< count
; i
++)
350 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
355 void AddNode( wxGtkTreeModelNode
* child
)
357 m_nodes
.Add( child
);
359 void *id
= child
->GetItem().GetID();
361 m_children
.Add( id
);
363 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
365 gs_internal
= m_internal
;
366 m_children
.Sort( &wxGtkTreeModelChildCmp
);
370 void InsertNode( wxGtkTreeModelNode
* child
, unsigned pos
)
372 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
378 void *id
= child
->GetItem().GetID();
380 // Insert into m_nodes so that the order of nodes in m_nodes is the
381 // same as the order of their corresponding IDs in m_children:
382 const unsigned int count
= m_nodes
.GetCount();
383 bool inserted
= false;
384 for (unsigned i
= 0; i
< count
; i
++)
386 wxGtkTreeModelNode
*node
= m_nodes
[i
];
387 int posInChildren
= m_children
.Index(node
->GetItem().GetID());
388 if ( (unsigned)posInChildren
>= pos
)
390 m_nodes
.Insert(child
, i
);
398 m_children
.Insert( id
, pos
);
401 void AddLeaf( void* id
)
403 InsertLeaf(id
, m_children
.size());
406 void InsertLeaf( void* id
, unsigned pos
)
408 m_children
.Insert( id
, pos
);
410 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
412 gs_internal
= m_internal
;
413 m_children
.Sort( &wxGtkTreeModelChildCmp
);
417 void DeleteChild( void* id
)
419 m_children
.Remove( id
);
421 unsigned int count
= m_nodes
.GetCount();
423 for (pos
= 0; pos
< count
; pos
++)
425 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
426 if (node
->GetItem().GetID() == id
)
428 m_nodes
.RemoveAt( pos
);
435 // returns position of child node for given item in children list or wxNOT_FOUND
436 int FindChildByItem(const wxDataViewItem
& item
) const
438 const void* itemId
= item
.GetID();
439 const wxGtkTreeModelChildren
& nodes
= m_children
;
440 const int len
= nodes
.size();
441 for ( int i
= 0; i
< len
; i
++ )
443 if ( nodes
[i
] == itemId
)
449 wxGtkTreeModelNode
* GetParent()
451 wxGtkTreeModelNodes
&GetNodes()
453 wxGtkTreeModelChildren
&GetChildren()
454 { return m_children
; }
456 unsigned int GetChildCount() const { return m_children
.GetCount(); }
457 unsigned int GetNodesCount() const { return m_nodes
.GetCount(); }
459 wxDataViewItem
&GetItem() { return m_item
; }
460 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
465 wxGtkTreeModelNode
*m_parent
;
466 wxGtkTreeModelNodes m_nodes
;
467 wxGtkTreeModelChildren m_children
;
468 wxDataViewItem m_item
;
469 wxDataViewCtrlInternal
*m_internal
;
473 //-----------------------------------------------------------------------------
475 //-----------------------------------------------------------------------------
477 extern bool g_blockEventsOnDrag
;
479 //-----------------------------------------------------------------------------
480 // define new GTK+ class wxGtkTreeModel
481 //-----------------------------------------------------------------------------
485 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
486 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
487 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
488 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
490 GType
gtk_wx_tree_model_get_type (void);
492 struct _GtkWxTreeModel
498 wxDataViewCtrlInternal
*internal
;
501 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
502 static void wxgtk_tree_model_init (GTypeInstance
* instance
, void*);
504 static void wxgtk_tree_model_tree_model_init (void* g_iface
, void*);
505 static void wxgtk_tree_model_sortable_init (void* g_iface
, void*);
506 static void wxgtk_tree_model_drag_source_init(void* g_iface
, void*);
507 static void wxgtk_tree_model_drag_dest_init (void* g_iface
, void*);
509 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
510 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
511 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
513 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
516 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
518 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
522 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
524 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
526 GtkTreeIter
*parent
);
527 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
529 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
531 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
535 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
540 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
541 gint
*sort_column_id
,
543 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
546 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
548 GtkTreeIterCompareFunc func
,
550 GDestroyNotify destroy
);
551 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
552 GtkTreeIterCompareFunc func
,
554 GDestroyNotify destroy
);
555 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
558 static gboolean
wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
560 static gboolean
wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
562 static gboolean
wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
564 GtkSelectionData
*selection_data
);
565 static gboolean
wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
567 GtkSelectionData
*selection_data
);
568 static gboolean
wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
569 GtkTreePath
*dest_path
,
570 GtkSelectionData
*selection_data
);
573 gtk_wx_tree_model_get_type (void)
575 static GType tree_model_type
= 0;
577 if (!tree_model_type
)
579 const GTypeInfo tree_model_info
=
581 sizeof (GObjectClass
),
582 NULL
, /* base_init */
583 NULL
, /* base_finalize */
585 NULL
, /* class_finalize */
586 NULL
, /* class_data */
587 sizeof (GtkWxTreeModel
),
589 wxgtk_tree_model_init
,
592 static const GInterfaceInfo tree_model_iface_info
=
594 wxgtk_tree_model_tree_model_init
,
599 static const GInterfaceInfo sortable_iface_info
=
601 wxgtk_tree_model_sortable_init
,
606 static const GInterfaceInfo drag_source_iface_info
=
608 wxgtk_tree_model_drag_source_init
,
613 static const GInterfaceInfo drag_dest_iface_info
=
615 wxgtk_tree_model_drag_dest_init
,
620 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
621 &tree_model_info
, (GTypeFlags
)0 );
623 g_type_add_interface_static (tree_model_type
,
625 &tree_model_iface_info
);
626 g_type_add_interface_static (tree_model_type
,
627 GTK_TYPE_TREE_SORTABLE
,
628 &sortable_iface_info
);
629 g_type_add_interface_static (tree_model_type
,
630 GTK_TYPE_TREE_DRAG_DEST
,
631 &drag_dest_iface_info
);
632 g_type_add_interface_static (tree_model_type
,
633 GTK_TYPE_TREE_DRAG_SOURCE
,
634 &drag_source_iface_info
);
637 return tree_model_type
;
640 static GtkWxTreeModel
*
641 wxgtk_tree_model_new(void)
643 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
648 wxgtk_tree_model_tree_model_init(void* g_iface
, void*)
650 GtkTreeModelIface
* iface
= static_cast<GtkTreeModelIface
*>(g_iface
);
651 iface
->get_flags
= wxgtk_tree_model_get_flags
;
652 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
653 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
654 iface
->get_iter
= wxgtk_tree_model_get_iter
;
655 iface
->get_path
= wxgtk_tree_model_get_path
;
656 iface
->get_value
= wxgtk_tree_model_get_value
;
657 iface
->iter_next
= wxgtk_tree_model_iter_next
;
658 iface
->iter_children
= wxgtk_tree_model_iter_children
;
659 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
660 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
661 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
662 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
666 wxgtk_tree_model_sortable_init(void* g_iface
, void*)
668 GtkTreeSortableIface
* iface
= static_cast<GtkTreeSortableIface
*>(g_iface
);
669 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
670 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
671 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
672 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
673 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
677 wxgtk_tree_model_drag_source_init(void* g_iface
, void*)
679 GtkTreeDragSourceIface
* iface
= static_cast<GtkTreeDragSourceIface
*>(g_iface
);
680 iface
->row_draggable
= wxgtk_tree_model_row_draggable
;
681 iface
->drag_data_delete
= wxgtk_tree_model_drag_data_delete
;
682 iface
->drag_data_get
= wxgtk_tree_model_drag_data_get
;
686 wxgtk_tree_model_drag_dest_init(void* g_iface
, void*)
688 GtkTreeDragDestIface
* iface
= static_cast<GtkTreeDragDestIface
*>(g_iface
);
689 iface
->drag_data_received
= wxgtk_tree_model_drag_data_received
;
690 iface
->row_drop_possible
= wxgtk_tree_model_row_drop_possible
;
694 wxgtk_tree_model_init(GTypeInstance
* instance
, void*)
696 GtkWxTreeModel
* tree_model
= GTK_WX_TREE_MODEL(instance
);
697 tree_model
->internal
= NULL
;
698 tree_model
->stamp
= g_random_int();
703 //-----------------------------------------------------------------------------
704 // implement callbacks from wxGtkTreeModel class by letting
705 // them call the methods of wxWidgets' wxDataViewModel
706 //-----------------------------------------------------------------------------
708 static GtkTreeModelFlags
709 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
711 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
712 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), (GtkTreeModelFlags
)0 );
714 return wxtree_model
->internal
->get_flags();
718 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
720 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
721 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
723 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
727 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
730 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
731 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
733 GType gtype
= G_TYPE_INVALID
;
735 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
737 if (wxtype
== wxT("string"))
738 gtype
= G_TYPE_STRING
;
741 gtype
= G_TYPE_POINTER
;
742 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
749 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
753 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
754 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
755 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
757 return wxtree_model
->internal
->get_iter( iter
, path
);
761 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
764 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
765 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
766 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
768 return wxtree_model
->internal
->get_path( iter
);
772 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
777 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
778 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
780 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
781 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
782 if (mtype
== wxT("string"))
785 g_value_init( value
, G_TYPE_STRING
);
786 wxDataViewItem
item( (void*) iter
->user_data
);
787 model
->GetValue( variant
, item
, (unsigned int) column
);
789 g_value_set_string( value
, variant
.GetString().utf8_str() );
793 wxFAIL_MSG( wxT("non-string columns not supported yet") );
798 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
801 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
803 // This happens when clearing the view by calling .._set_model( NULL );
804 if (iter
->stamp
== 0) return FALSE
;
806 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
807 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
809 return wxtree_model
->internal
->iter_next( iter
);
813 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
817 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
818 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
821 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
824 return wxtree_model
->internal
->iter_children( iter
, parent
);
828 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
831 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
832 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
833 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
835 return wxtree_model
->internal
->iter_has_child( iter
);
839 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
842 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
843 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
844 g_return_val_if_fail ( !iter
|| wxtree_model
->stamp
== iter
->stamp
, 0);
846 return wxtree_model
->internal
->iter_n_children( iter
);
850 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
855 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
856 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
858 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
862 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
866 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
867 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
868 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
870 return wxtree_model
->internal
->iter_parent( iter
, child
);
873 /* drag'n'drop iface */
875 wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
878 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
879 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
881 return wxtree_model
->internal
->row_draggable( drag_source
, path
);
885 wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
888 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
889 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
891 return wxtree_model
->internal
->drag_data_delete( drag_source
, path
);
895 wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
897 GtkSelectionData
*selection_data
)
899 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
900 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
903 wxPrintf( "drag_get_data\n");
905 wxGtkString
atom_selection(gdk_atom_name(selection_data
->selection
));
906 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection
) );
908 wxGtkString
atom_target(gdk_atom_name(selection_data
->target
));
909 wxPrintf( "target %s\n", wxString::FromAscii(atom_target
) );
911 wxGtkString
atom_type(gdk_atom_name(selection_data
->type
));
912 wxPrintf( "type %s\n", wxString::FromAscii(atom_type
) );
914 wxPrintf( "format %d\n", selection_data
->format
);
917 return wxtree_model
->internal
->drag_data_get( drag_source
, path
, selection_data
);
921 wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
923 GtkSelectionData
*selection_data
)
925 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
926 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
928 return wxtree_model
->internal
->drag_data_received( drag_dest
, dest
, selection_data
);
932 wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
933 GtkTreePath
*dest_path
,
934 GtkSelectionData
*selection_data
)
936 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
937 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
939 return wxtree_model
->internal
->row_drop_possible( drag_dest
, dest_path
, selection_data
);
944 wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
945 gint
*sort_column_id
,
948 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) sortable
;
950 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
952 if (!wxtree_model
->internal
->IsSorted())
955 *sort_column_id
= -1;
962 *sort_column_id
= wxtree_model
->internal
->GetSortColumn();
965 *order
= wxtree_model
->internal
->GetSortOrder();
970 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
973 wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
977 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
978 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
980 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
982 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
983 (order
!= tree_model
->internal
->GetSortOrder()))
985 tree_model
->internal
->SetSortColumn( sort_column_id
);
986 tree_model
->internal
->SetSortOrder( order
);
988 gtk_tree_sortable_sort_column_changed (sortable
);
990 tree_model
->internal
->GetDataViewModel()->Resort();
993 if (gs_lastLeftClickHeader
)
995 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
996 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
997 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
998 event
.SetModel( dv
->GetModel() );
999 dv
->HandleWindowEvent( event
);
1002 gs_lastLeftClickHeader
= NULL
;
1006 wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
1007 gint
WXUNUSED(sort_column_id
),
1008 GtkTreeIterCompareFunc func
,
1009 gpointer
WXUNUSED(data
),
1010 GDestroyNotify
WXUNUSED(destroy
))
1012 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1013 g_return_if_fail (func
!= NULL
);
1017 wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
1018 GtkTreeIterCompareFunc func
,
1019 gpointer
WXUNUSED(data
),
1020 GDestroyNotify
WXUNUSED(destroy
))
1022 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1023 g_return_if_fail (func
!= NULL
);
1025 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
1026 // TODO: remove this code
1030 wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
1032 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
1037 //-----------------------------------------------------------------------------
1038 // define new GTK+ class GtkWxRendererText
1039 //-----------------------------------------------------------------------------
1043 #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
1044 #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
1045 #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1046 #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1048 GType
gtk_wx_cell_renderer_text_get_type (void);
1050 typedef struct _GtkWxCellRendererText GtkWxCellRendererText
;
1052 struct _GtkWxCellRendererText
1054 GtkCellRendererText parent
;
1056 wxDataViewRenderer
*wx_renderer
;
1059 static GtkWxCellRendererText
*gtk_wx_cell_renderer_text_new (void);
1060 static void gtk_wx_cell_renderer_text_init (
1061 GTypeInstance
* instance
, void*);
1062 static void gtk_wx_cell_renderer_text_class_init(
1063 void* klass
, void*);
1064 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1065 GtkCellRenderer
*cell
,
1069 GdkRectangle
*background_area
,
1070 GdkRectangle
*cell_area
,
1071 GtkCellRendererState flags
);
1074 static GObjectClass
*text_cell_parent_class
= NULL
;
1079 gtk_wx_cell_renderer_text_get_type (void)
1081 static GType cell_wx_type
= 0;
1085 const GTypeInfo cell_wx_info
=
1087 sizeof (GtkCellRendererTextClass
),
1088 NULL
, /* base_init */
1089 NULL
, /* base_finalize */
1090 gtk_wx_cell_renderer_text_class_init
,
1091 NULL
, /* class_finalize */
1092 NULL
, /* class_data */
1093 sizeof (GtkWxCellRendererText
),
1094 0, /* n_preallocs */
1095 gtk_wx_cell_renderer_text_init
,
1098 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT
,
1099 "GtkWxCellRendererText", &cell_wx_info
, (GTypeFlags
)0 );
1102 return cell_wx_type
;
1106 gtk_wx_cell_renderer_text_init(GTypeInstance
* instance
, void*)
1108 GtkWxCellRendererText
* cell
= GTK_WX_CELL_RENDERER_TEXT(instance
);
1109 cell
->wx_renderer
= NULL
;
1113 gtk_wx_cell_renderer_text_class_init(void* klass
, void*)
1115 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1117 text_cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1119 cell_class
->start_editing
= gtk_wx_cell_renderer_text_start_editing
;
1122 GtkWxCellRendererText
*
1123 gtk_wx_cell_renderer_text_new (void)
1125 return (GtkWxCellRendererText
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT
, NULL
);
1128 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1129 GtkCellRenderer
*gtk_renderer
,
1130 GdkEvent
*gdk_event
,
1133 GdkRectangle
*background_area
,
1134 GdkRectangle
*cell_area
,
1135 GtkCellRendererState flags
)
1137 GtkWxCellRendererText
*wxgtk_renderer
= (GtkWxCellRendererText
*) gtk_renderer
;
1138 wxDataViewRenderer
*wx_renderer
= wxgtk_renderer
->wx_renderer
;
1139 wxDataViewColumn
*column
= wx_renderer
->GetOwner();
1142 item(column
->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1144 wxDataViewCtrl
*dv
= column
->GetOwner();
1145 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING
, dv
->GetId() );
1146 event
.SetDataViewColumn( column
);
1147 event
.SetModel( dv
->GetModel() );
1148 event
.SetColumn( column
->GetModelColumn() );
1149 event
.SetItem( item
);
1150 dv
->HandleWindowEvent( event
);
1152 if (event
.IsAllowed())
1153 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class
)->
1154 start_editing( gtk_renderer
, gdk_event
, widget
, path
, background_area
, cell_area
, flags
);
1159 //-----------------------------------------------------------------------------
1160 // define new GTK+ class GtkWxCellRenderer
1161 //-----------------------------------------------------------------------------
1165 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
1166 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
1167 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
1168 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
1170 GType
gtk_wx_cell_renderer_get_type (void);
1172 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
1174 struct _GtkWxCellRenderer
1176 GtkCellRenderer parent
;
1179 wxDataViewCustomRenderer
*cell
;
1182 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
1183 static void gtk_wx_cell_renderer_init (
1184 GTypeInstance
* instance
, void*);
1185 static void gtk_wx_cell_renderer_class_init(
1186 void* klass
, void*);
1187 static void gtk_wx_cell_renderer_get_size (
1188 GtkCellRenderer
*cell
,
1190 GdkRectangle
*rectangle
,
1195 static void gtk_wx_cell_renderer_render (
1196 GtkCellRenderer
*cell
,
1199 GdkRectangle
*background_area
,
1200 GdkRectangle
*cell_area
,
1201 GdkRectangle
*expose_area
,
1202 GtkCellRendererState flags
);
1203 static gboolean
gtk_wx_cell_renderer_activate(
1204 GtkCellRenderer
*cell
,
1208 GdkRectangle
*background_area
,
1209 GdkRectangle
*cell_area
,
1210 GtkCellRendererState flags
);
1211 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1212 GtkCellRenderer
*cell
,
1216 GdkRectangle
*background_area
,
1217 GdkRectangle
*cell_area
,
1218 GtkCellRendererState flags
);
1223 gtk_wx_cell_renderer_get_type (void)
1225 static GType cell_wx_type
= 0;
1229 const GTypeInfo cell_wx_info
=
1231 sizeof (GtkCellRendererClass
),
1232 NULL
, /* base_init */
1233 NULL
, /* base_finalize */
1234 gtk_wx_cell_renderer_class_init
,
1235 NULL
, /* class_finalize */
1236 NULL
, /* class_data */
1237 sizeof (GtkWxCellRenderer
),
1238 0, /* n_preallocs */
1239 gtk_wx_cell_renderer_init
,
1242 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
1243 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
1246 return cell_wx_type
;
1250 gtk_wx_cell_renderer_init(GTypeInstance
* instance
, void*)
1252 GtkWxCellRenderer
* cell
= GTK_WX_CELL_RENDERER(instance
);
1257 gtk_wx_cell_renderer_class_init(void* klass
, void*)
1259 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1261 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
1262 cell_class
->render
= gtk_wx_cell_renderer_render
;
1263 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
1264 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
1268 gtk_wx_cell_renderer_new (void)
1270 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
1273 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1274 GtkCellRenderer
*renderer
,
1275 GdkEvent
*WXUNUSED(event
),
1278 GdkRectangle
*WXUNUSED(background_area
),
1279 GdkRectangle
*cell_area
,
1280 GtkCellRendererState
WXUNUSED(flags
) )
1282 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1283 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1285 // Renderer doesn't support in-place editing
1286 if (!cell
->HasEditorCtrl())
1289 // An in-place editing control is still around
1290 if (cell
->GetEditorCtrl())
1294 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1300 rect
.x
+= cell_area
->x
;
1301 rect
.y
+= cell_area
->y
;
1302 // rect.width -= renderer->xpad * 2;
1303 // rect.height -= renderer->ypad * 2;
1305 // wxRect renderrect(wxRectFromGDKRect(&rect));
1306 wxRect
renderrect(wxRectFromGDKRect(cell_area
));
1309 item(cell
->GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1311 cell
->StartEditing( item
, renderrect
);
1317 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
1318 GtkWidget
*WXUNUSED(widget
),
1319 GdkRectangle
*cell_area
,
1325 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1326 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1328 wxSize size
= cell
->GetSize();
1330 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1332 // Uniform row height, if specified, overrides the value returned by the
1334 if ( !ctrl
->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
1336 const int uniformHeight
= ctrl
->GTKGetUniformRowHeight();
1337 if ( uniformHeight
> 0 )
1338 size
.y
= uniformHeight
;
1342 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1343 int calc_width
= xpad
* 2 + size
.x
;
1344 int calc_height
= ypad
* 2 + size
.y
;
1351 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
1353 float xalign
, yalign
;
1354 gtk_cell_renderer_get_alignment(renderer
, &xalign
, &yalign
);
1357 *x_offset
= int(xalign
* (cell_area
->width
- calc_width
- 2 * xpad
));
1358 *x_offset
= MAX(*x_offset
, 0) + xpad
;
1362 *y_offset
= int(yalign
* (cell_area
->height
- calc_height
- 2 * ypad
));
1363 *y_offset
= MAX(*y_offset
, 0) + ypad
;
1368 *width
= calc_width
;
1371 *height
= calc_height
;
1374 struct wxDataViewCustomRenderer::GTKRenderParams
1377 GdkRectangle
* expose_area
;
1379 GdkRectangle
* background_area
;
1384 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
1387 GdkRectangle
*background_area
,
1388 GdkRectangle
*cell_area
,
1389 GdkRectangle
*expose_area
,
1390 GtkCellRendererState flags
)
1393 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1394 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1396 wxDataViewCustomRenderer::GTKRenderParams renderParams
;
1397 renderParams
.window
= window
;
1398 renderParams
.expose_area
= expose_area
;
1399 renderParams
.widget
= widget
;
1400 renderParams
.background_area
= background_area
;
1401 renderParams
.flags
= flags
;
1402 cell
->GTKSetRenderParams(&renderParams
);
1404 wxRect
rect(wxRectFromGDKRect(cell_area
));
1406 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1407 rect
= rect
.Deflate(xpad
, ypad
);
1409 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
1410 wxWindowDCImpl
*impl
= (wxWindowDCImpl
*) dc
->GetImpl();
1412 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1413 // window such as a DnD drop window.
1414 if (window
!= impl
->m_gdkwindow
)
1417 impl
->m_gdkwindow
= window
;
1422 if (flags
& GTK_CELL_RENDERER_SELECTED
)
1423 state
|= wxDATAVIEW_CELL_SELECTED
;
1424 if (flags
& GTK_CELL_RENDERER_PRELIT
)
1425 state
|= wxDATAVIEW_CELL_PRELIT
;
1426 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1427 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1428 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1429 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1430 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
1431 state
|= wxDATAVIEW_CELL_FOCUSED
;
1432 cell
->WXCallRender( rect
, dc
, state
);
1434 cell
->GTKSetRenderParams(NULL
);
1438 gtk_wx_cell_renderer_activate(
1439 GtkCellRenderer
*renderer
,
1443 GdkRectangle
*WXUNUSED(background_area
),
1444 GdkRectangle
*cell_area
,
1445 GtkCellRendererState
WXUNUSED(flags
) )
1447 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1448 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1451 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1457 rect
.x
+= cell_area
->x
;
1458 rect
.y
+= cell_area
->y
;
1460 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1461 rect
.width
-= xpad
* 2;
1462 rect
.height
-= ypad
* 2;
1464 wxRect
renderrect(wxRectFromGDKRect(&rect
));
1466 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1467 wxDataViewModel
*model
= ctrl
->GetModel();
1469 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
1471 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1475 // activated by <ENTER>
1476 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, NULL
);
1478 else if ( event
->type
== GDK_BUTTON_PRESS
)
1480 GdkEventButton
*button_event
= (GdkEventButton
*)event
;
1481 if ( button_event
->button
== 1 )
1483 wxMouseEvent
mouse_event(wxEVT_LEFT_DOWN
);
1484 InitMouseEvent(ctrl
, mouse_event
, button_event
);
1486 mouse_event
.m_x
-= renderrect
.x
;
1487 mouse_event
.m_y
-= renderrect
.y
;
1489 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, &mouse_event
);
1493 wxLogDebug("unexpected event type in gtk_wx_cell_renderer_activate()");
1497 // ---------------------------------------------------------
1498 // wxGtkDataViewModelNotifier
1499 // ---------------------------------------------------------
1501 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1504 wxGtkDataViewModelNotifier( wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
);
1505 ~wxGtkDataViewModelNotifier();
1507 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1508 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1509 virtual bool ItemChanged( const wxDataViewItem
&item
);
1510 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
1511 virtual bool Cleared();
1512 virtual void Resort();
1513 virtual bool BeforeReset();
1514 virtual bool AfterReset();
1516 void UpdateLastCount();
1519 wxDataViewModel
*m_wx_model
;
1520 wxDataViewCtrlInternal
*m_internal
;
1523 // ---------------------------------------------------------
1524 // wxGtkDataViewListModelNotifier
1525 // ---------------------------------------------------------
1527 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1528 wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
)
1530 m_wx_model
= wx_model
;
1531 m_internal
= internal
;
1534 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1540 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1542 m_internal
->ItemAdded( parent
, item
);
1543 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1546 iter
.stamp
= wxgtk_model
->stamp
;
1547 iter
.user_data
= item
.GetID();
1549 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1550 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1551 gtk_tree_model_row_inserted(
1552 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1557 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1559 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1561 // using _get_path for a deleted item cannot be
1564 iter
.stamp
= wxgtk_model
->stamp
;
1565 iter
.user_data
= (gpointer
) item
.GetID();
1566 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1567 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1569 // so get the path from the parent
1570 GtkTreeIter parentIter
;
1571 parentIter
.stamp
= wxgtk_model
->stamp
;
1572 parentIter
.user_data
= (gpointer
) parent
.GetID();
1573 wxGtkTreePath
parentPath(wxgtk_tree_model_get_path(
1574 GTK_TREE_MODEL(wxgtk_model
), &parentIter
));
1576 // and add the final index ourselves
1577 wxGtkTreePath
path(gtk_tree_path_copy(parentPath
));
1578 int index
= m_internal
->GetIndexOf( parent
, item
);
1579 gtk_tree_path_append_index( path
, index
);
1582 gtk_tree_model_row_deleted(
1583 GTK_TREE_MODEL(wxgtk_model
), path
);
1585 m_internal
->ItemDeleted( parent
, item
);
1587 // Did we remove the last child, causing 'parent' to become a leaf?
1588 if ( !m_wx_model
->IsContainer(parent
) )
1590 gtk_tree_model_row_has_child_toggled
1592 GTK_TREE_MODEL(wxgtk_model
),
1601 void wxGtkDataViewModelNotifier::Resort()
1603 m_internal
->Resort();
1606 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1608 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1611 iter
.stamp
= wxgtk_model
->stamp
;
1612 iter
.user_data
= (gpointer
) item
.GetID();
1614 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1615 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1616 gtk_tree_model_row_changed(
1617 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1619 m_internal
->ItemChanged( item
);
1624 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
)
1626 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1627 wxDataViewCtrl
*ctrl
= m_internal
->GetOwner();
1629 // This adds GTK+'s missing MVC logic for ValueChanged
1631 for (index
= 0; index
< ctrl
->GetColumnCount(); index
++)
1633 wxDataViewColumn
*column
= ctrl
->GetColumn( index
);
1634 if (column
->GetModelColumn() == model_column
)
1636 GtkTreeView
*widget
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
1637 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1639 // Don't attempt to refresh not yet realized tree, it is useless
1640 // and results in GTK errors.
1641 if ( gtk_widget_get_realized(ctrl
->GtkGetTreeView()) )
1645 iter
.stamp
= wxgtk_model
->stamp
;
1646 iter
.user_data
= (gpointer
) item
.GetID();
1647 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1648 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1649 GdkRectangle cell_area
;
1650 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1652 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1653 double d
= gtk_adjustment_get_value( hadjust
);
1654 int xdiff
= (int) d
;
1656 int ydiff
= gcolumn
->button
->allocation
.height
;
1658 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1659 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1662 m_internal
->ValueChanged( item
, model_column
);
1671 bool wxGtkDataViewModelNotifier::BeforeReset()
1673 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1674 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), NULL
);
1679 bool wxGtkDataViewModelNotifier::AfterReset()
1681 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1682 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1684 m_internal
->Cleared();
1686 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), GTK_TREE_MODEL(wxgtk_model
) );
1691 bool wxGtkDataViewModelNotifier::Cleared()
1693 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1695 // There is no call to tell the model that everything
1696 // has been deleted so call row_deleted() for every
1699 int count
= m_internal
->iter_n_children( NULL
); // number of children of root
1701 GtkTreePath
*path
= gtk_tree_path_new_first(); // points to root
1704 for (i
= 0; i
< count
; i
++)
1705 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model
), path
);
1707 gtk_tree_path_free( path
);
1709 m_internal
->Cleared();
1714 // ---------------------------------------------------------
1715 // wxDataViewRenderer
1716 // ---------------------------------------------------------
1718 static gpointer s_user_data
= NULL
;
1721 wxgtk_cell_editable_editing_done( GtkCellEditable
*WXUNUSED(editable
),
1722 wxDataViewRenderer
*wxrenderer
)
1724 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1725 wxDataViewCtrl
*dv
= column
->GetOwner();
1726 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1727 event
.SetDataViewColumn( column
);
1728 event
.SetModel( dv
->GetModel() );
1729 wxDataViewItem
item( s_user_data
);
1730 event
.SetItem( item
);
1731 dv
->HandleWindowEvent( event
);
1735 wxgtk_renderer_editing_started( GtkCellRenderer
*WXUNUSED(cell
), GtkCellEditable
*editable
,
1736 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1741 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1742 wxDataViewCtrl
*dv
= column
->GetOwner();
1743 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1744 event
.SetDataViewColumn( column
);
1745 event
.SetModel( dv
->GetModel() );
1746 wxDataViewItem
item(dv
->GTKPathToItem(wxGtkTreePath(path
)));
1747 event
.SetItem( item
);
1748 dv
->HandleWindowEvent( event
);
1750 if (GTK_IS_CELL_EDITABLE(editable
))
1752 s_user_data
= item
.GetID();
1754 g_signal_connect (GTK_CELL_EDITABLE (editable
), "editing_done",
1755 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1756 (gpointer
) wxrenderer
);
1762 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1764 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1766 wxDataViewRendererBase( varianttype
, mode
, align
)
1771 // we haven't changed them yet
1772 m_usingDefaultAttrs
= true;
1774 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1775 // after the m_renderer pointer has been initialized
1778 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
1780 gtk_tree_view_column_pack_end( column
, m_renderer
, TRUE
/* expand */);
1783 void wxDataViewRenderer::GtkInitHandlers()
1785 if (!gtk_check_version(2,6,0))
1787 g_signal_connect (GTK_CELL_RENDERER(m_renderer
), "editing_started",
1788 G_CALLBACK (wxgtk_renderer_editing_started
),
1793 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1795 GtkCellRendererMode gtkMode
;
1798 case wxDATAVIEW_CELL_INERT
:
1799 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1802 case wxDATAVIEW_CELL_ACTIVATABLE
:
1803 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1806 case wxDATAVIEW_CELL_EDITABLE
:
1807 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1811 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1817 // This value is most often ignored in GtkTreeView
1818 GValue gvalue
= { 0, };
1819 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1820 g_value_set_enum( &gvalue
, gtkMode
);
1821 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1822 g_value_unset( &gvalue
);
1825 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1827 wxDataViewCellMode ret
;
1830 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1832 switch (g_value_get_enum(&gvalue
))
1835 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1836 // fall through (we have to return something)
1838 case GTK_CELL_RENDERER_MODE_INERT
:
1839 ret
= wxDATAVIEW_CELL_INERT
;
1842 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1843 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1846 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1847 ret
= wxDATAVIEW_CELL_EDITABLE
;
1851 g_value_unset( &gvalue
);
1856 void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer
*renderer
)
1858 int align
= m_alignment
;
1860 // query alignment from column ?
1864 if (GetOwner() == NULL
)
1867 align
= GetOwner()->GetAlignment();
1868 align
|= wxALIGN_CENTRE_VERTICAL
;
1871 // horizontal alignment:
1873 gfloat xalign
= 0.0;
1874 if (align
& wxALIGN_RIGHT
)
1876 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1879 GValue gvalue
= { 0, };
1880 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1881 g_value_set_float( &gvalue
, xalign
);
1882 g_object_set_property( G_OBJECT(renderer
), "xalign", &gvalue
);
1883 g_value_unset( &gvalue
);
1885 // vertical alignment:
1887 gfloat yalign
= 0.0;
1888 if (align
& wxALIGN_BOTTOM
)
1890 else if (align
& wxALIGN_CENTER_VERTICAL
)
1893 GValue gvalue2
= { 0, };
1894 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1895 g_value_set_float( &gvalue2
, yalign
);
1896 g_object_set_property( G_OBJECT(renderer
), "yalign", &gvalue2
);
1897 g_value_unset( &gvalue2
);
1900 void wxDataViewRenderer::SetAlignment( int align
)
1902 m_alignment
= align
;
1903 GtkUpdateAlignment();
1906 int wxDataViewRenderer::GetAlignment() const
1911 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode
)
1914 if ( gtk_check_version(2, 6, 0) != NULL
)
1917 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1921 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1922 // can just cast between them
1923 GValue gvalue
= { 0, };
1924 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1925 g_value_set_enum( &gvalue
, static_cast<PangoEllipsizeMode
>(mode
) );
1926 g_object_set_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1927 g_value_unset( &gvalue
);
1930 #endif // GTK 2.6/before
1933 wxEllipsizeMode
wxDataViewRenderer::GetEllipsizeMode() const
1936 if ( gtk_check_version(2, 6, 0) != NULL
)
1937 return wxELLIPSIZE_NONE
;
1939 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1941 return wxELLIPSIZE_NONE
;
1943 GValue gvalue
= { 0, };
1944 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1945 g_object_get_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1947 mode
= static_cast<wxEllipsizeMode
>(g_value_get_enum( &gvalue
));
1948 g_value_unset( &gvalue
);
1952 return wxELLIPSIZE_NONE
;
1953 #endif // GTK 2.6/before
1957 wxDataViewRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
1959 wxVariant
value(str
);
1960 if (!Validate( value
))
1964 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
1966 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
1970 wxDataViewRenderer::GtkOnCellChanged(const wxVariant
& value
,
1971 const wxDataViewItem
& item
,
1974 wxDataViewModel
*model
= GetOwner()->GetOwner()->GetModel();
1975 model
->ChangeValue( value
, item
, col
);
1978 // ---------------------------------------------------------
1979 // wxDataViewTextRenderer
1980 // ---------------------------------------------------------
1985 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*WXUNUSED(renderer
),
1986 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1988 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) user_data
;
1990 cell
->GtkOnTextEdited(arg1
, wxGTK_CONV_BACK_FONT(
1991 arg2
, cell
->GetOwner()->GetOwner()->GetFont()));
1999 // helper function used by wxDataViewTextRenderer and
2000 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
2001 // given text renderer and returns true if anything was done
2002 bool GtkApplyAttr(GtkCellRendererText
*renderer
, const wxDataViewItemAttr
& attr
)
2004 bool usingDefaultAttrs
= true;
2005 if (attr
.HasColour())
2007 const GdkColor
* const gcol
= attr
.GetColour().GetColor();
2009 GValue gvalue
= { 0, };
2010 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2011 g_value_set_boxed( &gvalue
, gcol
);
2012 g_object_set_property( G_OBJECT(renderer
), "foreground_gdk", &gvalue
);
2013 g_value_unset( &gvalue
);
2015 usingDefaultAttrs
= false;
2019 GValue gvalue
= { 0, };
2020 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2021 g_value_set_boolean( &gvalue
, FALSE
);
2022 g_object_set_property( G_OBJECT(renderer
), "foreground-set", &gvalue
);
2023 g_value_unset( &gvalue
);
2026 if (attr
.GetItalic())
2028 GValue gvalue
= { 0, };
2029 g_value_init( &gvalue
, PANGO_TYPE_STYLE
);
2030 g_value_set_enum( &gvalue
, PANGO_STYLE_ITALIC
);
2031 g_object_set_property( G_OBJECT(renderer
), "style", &gvalue
);
2032 g_value_unset( &gvalue
);
2034 usingDefaultAttrs
= false;
2038 GValue gvalue
= { 0, };
2039 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2040 g_value_set_boolean( &gvalue
, FALSE
);
2041 g_object_set_property( G_OBJECT(renderer
), "style-set", &gvalue
);
2042 g_value_unset( &gvalue
);
2048 GValue gvalue
= { 0, };
2049 g_value_init( &gvalue
, PANGO_TYPE_WEIGHT
);
2050 g_value_set_enum( &gvalue
, PANGO_WEIGHT_BOLD
);
2051 g_object_set_property( G_OBJECT(renderer
), "weight", &gvalue
);
2052 g_value_unset( &gvalue
);
2054 usingDefaultAttrs
= false;
2058 GValue gvalue
= { 0, };
2059 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2060 g_value_set_boolean( &gvalue
, FALSE
);
2061 g_object_set_property( G_OBJECT(renderer
), "weight-set", &gvalue
);
2062 g_value_unset( &gvalue
);
2066 if (attr
.HasBackgroundColour())
2068 wxColour colour
= attr
.GetBackgroundColour();
2069 const GdkColor
* const gcol
= colour
.GetColor();
2071 GValue gvalue
= { 0, };
2072 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2073 g_value_set_boxed( &gvalue
, gcol
);
2074 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2075 g_value_unset( &gvalue
);
2079 GValue gvalue
= { 0, };
2080 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2081 g_value_set_boolean( &gvalue
, FALSE
);
2082 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2083 g_value_unset( &gvalue
);
2087 return !usingDefaultAttrs
;
2090 } // anonymous namespace
2092 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
2094 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2096 wxDataViewRenderer( varianttype
, mode
, align
)
2098 GtkWxCellRendererText
*text_renderer
= gtk_wx_cell_renderer_text_new();
2099 text_renderer
->wx_renderer
= this;
2100 m_renderer
= (GtkCellRenderer
*) text_renderer
;
2102 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
2104 GValue gvalue
= { 0, };
2105 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2106 g_value_set_boolean( &gvalue
, true );
2107 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
2108 g_value_unset( &gvalue
);
2110 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2116 SetAlignment(align
);
2119 bool wxDataViewTextRenderer::SetTextValue(const wxString
& str
)
2121 GValue gvalue
= { 0, };
2122 g_value_init( &gvalue
, G_TYPE_STRING
);
2123 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( str
, GetOwner()->GetOwner()->GetFont() ) );
2124 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2125 g_value_unset( &gvalue
);
2130 bool wxDataViewTextRenderer::GetTextValue(wxString
& str
) const
2132 GValue gvalue
= { 0, };
2133 g_value_init( &gvalue
, G_TYPE_STRING
);
2134 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2135 str
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
), const_cast<wxDataViewTextRenderer
*>(this)->GetOwner()->GetOwner()->GetFont() );
2136 g_value_unset( &gvalue
);
2141 void wxDataViewTextRenderer::SetAlignment( int align
)
2143 wxDataViewRenderer::SetAlignment(align
);
2145 if (gtk_check_version(2,10,0))
2148 // horizontal alignment:
2149 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2150 if (align
& wxALIGN_RIGHT
)
2151 pangoAlign
= PANGO_ALIGN_RIGHT
;
2152 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2153 pangoAlign
= PANGO_ALIGN_CENTER
;
2155 GValue gvalue
= { 0, };
2156 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2157 g_value_set_enum( &gvalue
, pangoAlign
);
2158 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2159 g_value_unset( &gvalue
);
2162 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr
& attr
)
2164 return GtkApplyAttr(GtkGetTextRenderer(), attr
);
2167 GtkCellRendererText
*wxDataViewTextRenderer::GtkGetTextRenderer() const
2169 return GTK_CELL_RENDERER_TEXT(m_renderer
);
2172 // ---------------------------------------------------------
2173 // wxDataViewBitmapRenderer
2174 // ---------------------------------------------------------
2179 // set "pixbuf" property on the given renderer
2180 void SetPixbufProp(GtkCellRenderer
*renderer
, GdkPixbuf
*pixbuf
)
2182 GValue gvalue
= { 0, };
2183 g_value_init( &gvalue
, G_TYPE_OBJECT
);
2184 g_value_set_object( &gvalue
, pixbuf
);
2185 g_object_set_property( G_OBJECT(renderer
), "pixbuf", &gvalue
);
2186 g_value_unset( &gvalue
);
2189 } // anonymous namespace
2191 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
2193 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2195 wxDataViewRenderer( varianttype
, mode
, align
)
2197 m_renderer
= gtk_cell_renderer_pixbuf_new();
2200 SetAlignment(align
);
2203 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
2205 if (value
.GetType() == wxT("wxBitmap"))
2210 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2211 // object (and it will stay there and remain owned by wxBitmap)
2212 SetPixbufProp(m_renderer
, bitmap
.GetPixbuf());
2214 else if (value
.GetType() == wxT("wxIcon"))
2219 SetPixbufProp(m_renderer
, icon
.GetPixbuf());
2229 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2234 // ---------------------------------------------------------
2235 // wxDataViewToggleRenderer
2236 // ---------------------------------------------------------
2239 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2240 gchar
*path
, gpointer user_data
);
2243 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2244 gchar
*path
, gpointer user_data
)
2246 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
2249 GValue gvalue
= { 0, };
2250 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2251 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
2252 bool tmp
= g_value_get_boolean( &gvalue
);
2253 g_value_unset( &gvalue
);
2257 wxVariant value
= tmp
;
2258 if (!cell
->Validate( value
))
2261 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
2262 wxDataViewModel
*model
= ctrl
->GetModel();
2264 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
2266 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
2268 model
->ChangeValue( value
, item
, model_col
);
2271 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
2273 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
2274 wxDataViewCellMode mode
, int align
) :
2275 wxDataViewRenderer( varianttype
, mode
, align
)
2277 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
2279 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
2281 g_signal_connect_after( m_renderer
, "toggled",
2282 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
2286 GValue gvalue
= { 0, };
2287 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2288 g_value_set_boolean( &gvalue
, false );
2289 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
2290 g_value_unset( &gvalue
);
2294 SetAlignment(align
);
2297 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
2301 GValue gvalue
= { 0, };
2302 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2303 g_value_set_boolean( &gvalue
, tmp
);
2304 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2305 g_value_unset( &gvalue
);
2310 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
2312 GValue gvalue
= { 0, };
2313 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2314 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2315 bool tmp
= g_value_get_boolean( &gvalue
);
2316 g_value_unset( &gvalue
);
2323 // ---------------------------------------------------------
2324 // wxDataViewCustomRenderer
2325 // ---------------------------------------------------------
2327 class wxDataViewCtrlDCImpl
: public wxWindowDCImpl
2330 wxDataViewCtrlDCImpl( wxDC
*owner
, wxDataViewCtrl
*window
) :
2331 wxWindowDCImpl( owner
)
2333 GtkWidget
*widget
= window
->m_treeview
;
2339 m_context
= window
->GTKGetPangoDefaultContext();
2340 m_layout
= pango_layout_new( m_context
);
2341 m_fontdesc
= pango_font_description_copy(gtk_widget_get_style(widget
)->font_desc
);
2343 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
2345 // Set m_gdkwindow later
2350 class wxDataViewCtrlDC
: public wxWindowDC
2353 wxDataViewCtrlDC( wxDataViewCtrl
*window
) :
2354 wxWindowDC( new wxDataViewCtrlDCImpl( this, window
) )
2359 // ---------------------------------------------------------
2360 // wxDataViewCustomRenderer
2361 // ---------------------------------------------------------
2363 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
2365 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
2366 wxDataViewCellMode mode
,
2369 : wxDataViewCustomRendererBase( varianttype
, mode
, align
)
2372 m_text_renderer
= NULL
;
2373 m_renderParams
= NULL
;
2381 void wxDataViewCustomRenderer::GtkInitTextRenderer()
2383 m_text_renderer
= GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2384 g_object_ref_sink(m_text_renderer
);
2386 GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer
));
2389 GtkCellRendererText
*wxDataViewCustomRenderer::GtkGetTextRenderer() const
2391 if ( !m_text_renderer
)
2393 // we create it on demand so need to do it even from a const function
2394 const_cast<wxDataViewCustomRenderer
*>(this)->GtkInitTextRenderer();
2397 return m_text_renderer
;
2400 void wxDataViewCustomRenderer::RenderText( const wxString
&text
,
2404 int WXUNUSED(state
) )
2407 GtkCellRendererText
* const textRenderer
= GtkGetTextRenderer();
2409 GValue gvalue
= { 0, };
2410 g_value_init( &gvalue
, G_TYPE_STRING
);
2411 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( text
, GetOwner()->GetOwner()->GetFont() ) );
2412 g_object_set_property( G_OBJECT(textRenderer
), "text", &gvalue
);
2413 g_value_unset( &gvalue
);
2415 GtkApplyAttr(textRenderer
, GetAttr());
2417 GdkRectangle cell_area
;
2418 wxRectToGDKRect(cell
, cell_area
);
2419 cell_area
.x
+= xoffset
;
2420 cell_area
.width
-= xoffset
;
2422 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer
),
2423 m_renderParams
->window
,
2424 m_renderParams
->widget
,
2425 m_renderParams
->background_area
,
2427 m_renderParams
->expose_area
,
2428 GtkCellRendererState(m_renderParams
->flags
));
2431 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
2433 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
2434 renderer
->cell
= this;
2436 m_renderer
= (GtkCellRenderer
*) renderer
;
2439 SetAlignment(align
);
2446 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2451 if (m_text_renderer
)
2452 g_object_unref(m_text_renderer
);
2455 wxDC
*wxDataViewCustomRenderer::GetDC()
2459 if (GetOwner() == NULL
)
2461 if (GetOwner()->GetOwner() == NULL
)
2463 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2469 // ---------------------------------------------------------
2470 // wxDataViewProgressRenderer
2471 // ---------------------------------------------------------
2473 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
2475 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
2476 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
2477 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
2483 if (!gtk_check_version(2,6,0))
2485 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
2488 SetAlignment(align
);
2491 // We can't initialize the renderer just yet because we don't have the
2492 // pointer to the column that uses this renderer yet and so attempt to
2493 // dereference GetOwner() to get the font that is used as a source of
2494 // encoding in multibyte-to-Unicode conversion in GTKSetLabel() in
2495 // non-Unicode builds would crash. So simply remember to do it later.
2496 if ( !m_label
.empty() )
2497 m_needsToSetLabel
= true;
2499 #endif // !wxUSE_UNICODE
2505 // Use custom cell code
2506 wxDataViewCustomRenderer::Init(mode
, align
);
2510 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2514 void wxDataViewProgressRenderer::GTKSetLabel()
2516 GValue gvalue
= { 0, };
2517 g_value_init( &gvalue
, G_TYPE_STRING
);
2519 // Take care to not use GetOwner() here if the label is empty, we can be
2520 // called from ctor when GetOwner() is still NULL in this case.
2521 wxScopedCharBuffer buf
;
2522 if ( m_label
.empty() )
2523 buf
= wxScopedCharBuffer::CreateNonOwned("");
2525 buf
= wxGTK_CONV_FONT(m_label
, GetOwner()->GetOwner()->GetFont());
2527 g_value_set_string( &gvalue
, buf
);
2528 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2529 g_value_unset( &gvalue
);
2532 m_needsToSetLabel
= false;
2533 #endif // !wxUSE_UNICODE
2536 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
2539 if (!gtk_check_version(2,6,0))
2542 if ( m_needsToSetLabel
)
2544 #endif // !wxUSE_UNICODE
2546 gint tmp
= (long) value
;
2547 GValue gvalue
= { 0, };
2548 g_value_init( &gvalue
, G_TYPE_INT
);
2549 g_value_set_int( &gvalue
, tmp
);
2550 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
2551 g_value_unset( &gvalue
);
2556 m_value
= (long) value
;
2558 if (m_value
< 0) m_value
= 0;
2559 if (m_value
> 100) m_value
= 100;
2565 bool wxDataViewProgressRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2570 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
2572 double pct
= (double)m_value
/ 100.0;
2574 bar
.width
= (int)(cell
.width
* pct
);
2575 dc
->SetPen( *wxTRANSPARENT_PEN
);
2576 dc
->SetBrush( *wxBLUE_BRUSH
);
2577 dc
->DrawRectangle( bar
);
2579 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
2580 dc
->SetPen( *wxBLACK_PEN
);
2581 dc
->DrawRectangle( cell
);
2586 wxSize
wxDataViewProgressRenderer::GetSize() const
2588 return wxSize(40,12);
2591 // -------------------------------------
2592 // wxDataViewChoiceRenderer
2593 // -------------------------------------
2595 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString
&choices
,
2596 wxDataViewCellMode mode
, int alignment
) :
2597 wxDataViewCustomRenderer( "string", mode
, alignment
, true )
2599 m_choices
= choices
;
2602 if (!gtk_check_version(2,6,0))
2604 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_combo_new();
2606 GtkListStore
*store
= gtk_list_store_new( 1, G_TYPE_STRING
);
2607 for (size_t n
= 0; n
< m_choices
.GetCount(); n
++)
2609 gtk_list_store_insert_with_values(
2611 static_cast<const char *>(m_choices
[n
].utf8_str()), -1 );
2614 g_object_set (m_renderer
,
2620 bool editable
= (mode
& wxDATAVIEW_CELL_EDITABLE
);
2621 g_object_set (m_renderer
, "editable", editable
, NULL
);
2623 SetAlignment(alignment
);
2625 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2632 // Use custom cell code
2633 wxDataViewCustomRenderer::Init(mode
, alignment
);
2637 bool wxDataViewChoiceRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
2639 RenderText( m_data
, 0, rect
, dc
, state
);
2643 wxSize
wxDataViewChoiceRenderer::GetSize() const
2645 return wxSize(70,20);
2648 bool wxDataViewChoiceRenderer::SetValue( const wxVariant
&value
)
2652 if (!gtk_check_version(2,6,0))
2654 GValue gvalue
= { 0, };
2655 g_value_init( &gvalue
, G_TYPE_STRING
);
2656 g_value_set_string(&gvalue
,
2657 wxGTK_CONV_FONT(value
.GetString(),
2658 GetOwner()->GetOwner()->GetFont()));
2659 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2660 g_value_unset( &gvalue
);
2664 m_data
= value
.GetString();
2669 bool wxDataViewChoiceRenderer::GetValue( wxVariant
&value
) const
2672 if (!gtk_check_version(2,6,0))
2674 GValue gvalue
= { 0, };
2675 g_value_init( &gvalue
, G_TYPE_STRING
);
2676 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2677 wxString temp
= wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue
),
2678 GetOwner()->GetOwner()->GetFont());
2679 g_value_unset( &gvalue
);
2682 //wxPrintf( "temp %s\n", temp );
2683 // TODO: remove this code
2692 void wxDataViewChoiceRenderer::SetAlignment( int align
)
2694 wxDataViewCustomRenderer::SetAlignment(align
);
2696 if (gtk_check_version(2,10,0))
2699 // horizontal alignment:
2700 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2701 if (align
& wxALIGN_RIGHT
)
2702 pangoAlign
= PANGO_ALIGN_RIGHT
;
2703 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2704 pangoAlign
= PANGO_ALIGN_CENTER
;
2706 GValue gvalue
= { 0, };
2707 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2708 g_value_set_enum( &gvalue
, pangoAlign
);
2709 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2710 g_value_unset( &gvalue
);
2713 // ----------------------------------------------------------------------------
2714 // wxDataViewChoiceByIndexRenderer
2715 // ----------------------------------------------------------------------------
2717 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
2718 wxDataViewCellMode mode
, int alignment
) :
2719 wxDataViewChoiceRenderer( choices
, mode
, alignment
)
2723 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
2725 wxVariant
value( (long) GetChoices().Index( str
) );
2727 if (!Validate( value
))
2731 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2733 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2736 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant
&value
)
2738 wxVariant string_value
= GetChoice( value
.GetLong() );
2739 return wxDataViewChoiceRenderer::SetValue( string_value
);
2742 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant
&value
) const
2744 wxVariant string_value
;
2745 if (!wxDataViewChoiceRenderer::GetValue( string_value
))
2748 value
= (long) GetChoices().Index( string_value
.GetString() );
2752 // ---------------------------------------------------------
2753 // wxDataViewIconTextRenderer
2754 // ---------------------------------------------------------
2756 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
2758 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2760 const wxString
&varianttype
,
2761 wxDataViewCellMode mode
,
2764 : wxDataViewTextRenderer(varianttype
, mode
, align
)
2766 m_rendererIcon
= gtk_cell_renderer_pixbuf_new();
2769 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2773 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
2775 // add the icon renderer first
2776 gtk_tree_view_column_pack_start(column
, m_rendererIcon
, FALSE
/* !expand */);
2778 // add the text renderer too
2779 wxDataViewRenderer::GtkPackIntoColumn(column
);
2782 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
2786 SetTextValue(m_value
.GetText());
2787 SetPixbufProp(m_rendererIcon
, m_value
.GetIcon().GetPixbuf());
2792 bool wxDataViewIconTextRenderer::GetValue(wxVariant
& value
) const
2795 if ( !GetTextValue(str
) )
2798 // user doesn't have any way to edit the icon so leave it unchanged
2799 value
<< wxDataViewIconText(str
, m_value
.GetIcon());
2805 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant
& value
,
2806 const wxDataViewItem
& item
,
2809 // we receive just the text part of our value as it's the only one which
2810 // can be edited, but we need the full wxDataViewIconText value for the
2812 wxVariant valueIconText
;
2813 valueIconText
<< wxDataViewIconText(value
.GetString(), m_value
.GetIcon());
2814 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText
, item
, col
);
2817 // ---------------------------------------------------------
2819 // ---------------------------------------------------------
2823 gtk_dataview_header_button_press_callback( GtkWidget
*WXUNUSED(widget
),
2824 GdkEventButton
*gdk_event
,
2825 wxDataViewColumn
*column
)
2827 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
2830 if (gdk_event
->button
== 1)
2832 gs_lastLeftClickHeader
= column
;
2834 wxDataViewCtrl
*dv
= column
->GetOwner();
2835 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2836 event
.SetDataViewColumn( column
);
2837 event
.SetModel( dv
->GetModel() );
2838 if (dv
->HandleWindowEvent( event
))
2842 if (gdk_event
->button
== 3)
2844 wxDataViewCtrl
*dv
= column
->GetOwner();
2845 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2846 event
.SetDataViewColumn( column
);
2847 event
.SetModel( dv
->GetModel() );
2848 if (dv
->HandleWindowEvent( event
))
2858 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*WXUNUSED(column
),
2859 GtkCellRenderer
*renderer
,
2860 GtkTreeModel
*model
,
2864 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2865 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2867 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2869 wxDataViewItem
item( (void*) iter
->user_data
);
2871 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2873 if (!wx_model
->IsVirtualListModel())
2876 if (wx_model
->IsContainer( item
))
2878 visible
= wx_model
->HasContainerColumns( item
) ||
2879 (cell
->GetOwner()->GetModelColumn() == 0);
2886 GValue gvalue
= { 0, };
2887 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2888 g_value_set_boolean( &gvalue
, visible
);
2889 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2890 g_value_unset( &gvalue
);
2897 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2899 if (value
.GetType() != cell
->GetVariantType())
2901 wxLogError( wxT("Wrong type, required: %s but: %s"),
2902 value
.GetType().c_str(),
2903 cell
->GetVariantType().c_str() );
2906 cell
->SetValue( value
);
2908 // deal with disabled items
2909 bool enabled
= wx_model
->IsEnabled( item
, cell
->GetOwner()->GetModelColumn() );
2911 // a) this sets the appearance to disabled grey
2912 GValue gvalue
= { 0, };
2913 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2914 g_value_set_boolean( &gvalue
, enabled
);
2915 g_object_set_property( G_OBJECT(renderer
), "sensitive", &gvalue
);
2916 g_value_unset( &gvalue
);
2918 // b) this actually disables the control/renderer
2920 cell
->SetMode( cell
->GtkGetMode() );
2922 cell
->SetMode( wxDATAVIEW_CELL_INERT
);
2925 // deal with attributes: if the renderer doesn't support them at all, we
2926 // don't even need to query the model for them
2927 if ( !cell
->GtkSupportsAttrs() )
2930 // it can support attributes so check if this item has any
2931 wxDataViewItemAttr attr
;
2932 if ( wx_model
->GetAttr( item
, cell
->GetOwner()->GetModelColumn(), attr
)
2933 || !cell
->GtkIsUsingDefaultAttrs() )
2935 bool usingDefaultAttrs
= !cell
->GtkSetAttr(attr
);
2936 cell
->GtkSetUsingDefaultAttrs(usingDefaultAttrs
);
2938 // else: no custom attributes specified and we're already using the default
2939 // ones -- nothing to do
2945 #include <wx/listimpl.cpp>
2946 WX_DEFINE_LIST(wxDataViewColumnList
)
2948 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
2949 unsigned int model_column
, int width
,
2950 wxAlignment align
, int flags
)
2951 : wxDataViewColumnBase( cell
, model_column
)
2953 Init( align
, flags
, width
);
2958 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
2959 unsigned int model_column
, int width
,
2960 wxAlignment align
, int flags
)
2961 : wxDataViewColumnBase( bitmap
, cell
, model_column
)
2963 Init( align
, flags
, width
);
2965 SetBitmap( bitmap
);
2968 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
2970 m_isConnected
= false;
2972 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
2973 m_column
= (GtkWidget
*) column
;
2976 SetAlignment( align
);
2980 // Create container for icon and label
2981 GtkWidget
*box
= gtk_hbox_new( FALSE
, 1 );
2982 gtk_widget_show( box
);
2983 // gtk_container_set_border_width((GtkContainer*)box, 2);
2984 m_image
= gtk_image_new();
2985 gtk_box_pack_start(GTK_BOX(box
), m_image
, FALSE
, FALSE
, 1);
2986 m_label
= gtk_label_new("");
2987 gtk_box_pack_end( GTK_BOX(box
), GTK_WIDGET(m_label
), FALSE
, FALSE
, 1 );
2988 gtk_tree_view_column_set_widget( column
, box
);
2990 wxDataViewRenderer
* const colRenderer
= GetRenderer();
2991 GtkCellRenderer
* const cellRenderer
= colRenderer
->GetGtkHandle();
2993 colRenderer
->GtkPackIntoColumn(column
);
2995 gtk_tree_view_column_set_cell_data_func( column
, cellRenderer
,
2996 wxGtkTreeCellDataFunc
, (gpointer
) colRenderer
, NULL
);
2999 void wxDataViewColumn::OnInternalIdle()
3004 if (gtk_widget_get_realized(GetOwner()->m_treeview
))
3006 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3009 g_signal_connect(column
->button
, "button_press_event",
3010 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
3012 // otherwise the event will be blocked by GTK+
3013 gtk_tree_view_column_set_clickable( column
, TRUE
);
3015 m_isConnected
= true;
3020 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
3022 wxDataViewColumnBase::SetOwner( owner
);
3024 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3026 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3029 void wxDataViewColumn::SetTitle( const wxString
&title
)
3031 wxDataViewCtrl
*ctrl
= GetOwner();
3032 gtk_label_set_text( GTK_LABEL(m_label
), ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
3033 : wxGTK_CONV_SYS(title
) );
3035 gtk_widget_hide( m_label
);
3037 gtk_widget_show( m_label
);
3040 wxString
wxDataViewColumn::GetTitle() const
3042 return wxGTK_CONV_BACK_FONT(
3043 gtk_label_get_text( GTK_LABEL(m_label
) ),
3044 GetOwner()->GetFont()
3048 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
3050 wxDataViewColumnBase::SetBitmap( bitmap
);
3054 GtkImage
*gtk_image
= GTK_IMAGE(m_image
);
3056 GdkBitmap
*mask
= NULL
;
3057 if (bitmap
.GetMask())
3058 mask
= bitmap
.GetMask()->GetBitmap();
3060 if (bitmap
.HasPixbuf())
3062 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
3063 bitmap
.GetPixbuf());
3067 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
3068 bitmap
.GetPixmap(), mask
);
3070 gtk_widget_show( m_image
);
3074 gtk_widget_hide( m_image
);
3078 void wxDataViewColumn::SetHidden( bool hidden
)
3080 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
3083 void wxDataViewColumn::SetResizeable( bool resizable
)
3085 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizable
);
3088 void wxDataViewColumn::SetAlignment( wxAlignment align
)
3090 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3092 gfloat xalign
= 0.0;
3093 if (align
== wxALIGN_RIGHT
)
3095 if (align
== wxALIGN_CENTER_HORIZONTAL
||
3096 align
== wxALIGN_CENTER
)
3099 gtk_tree_view_column_set_alignment( column
, xalign
);
3101 if (m_renderer
&& m_renderer
->GetAlignment() == -1)
3102 m_renderer
->GtkUpdateAlignment();
3105 wxAlignment
wxDataViewColumn::GetAlignment() const
3107 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
3110 return wxALIGN_RIGHT
;
3112 return wxALIGN_CENTER_HORIZONTAL
;
3114 return wxALIGN_LEFT
;
3117 void wxDataViewColumn::SetSortable( bool sortable
)
3119 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3123 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
3127 gtk_tree_view_column_set_sort_column_id( column
, -1 );
3128 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
3129 gtk_tree_view_column_set_clickable( column
, FALSE
);
3133 bool wxDataViewColumn::IsSortable() const
3135 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3136 return gtk_tree_view_column_get_clickable( column
);
3139 bool wxDataViewColumn::IsSortKey() const
3141 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3142 return gtk_tree_view_column_get_sort_indicator( column
);
3145 bool wxDataViewColumn::IsResizeable() const
3147 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3148 return gtk_tree_view_column_get_resizable( column
);
3151 bool wxDataViewColumn::IsHidden() const
3153 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3154 return !gtk_tree_view_column_get_visible( column
);
3157 void wxDataViewColumn::SetSortOrder( bool ascending
)
3159 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3162 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
3164 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
3166 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
3169 bool wxDataViewColumn::IsSortOrderAscending() const
3171 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3173 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
3176 void wxDataViewColumn::SetMinWidth( int width
)
3178 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3181 int wxDataViewColumn::GetMinWidth() const
3183 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3186 int wxDataViewColumn::GetWidth() const
3188 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3191 void wxDataViewColumn::SetWidth( int width
)
3193 if ( width
== wxCOL_WIDTH_AUTOSIZE
)
3195 // NB: this disables user resizing
3196 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
3200 if ( width
== wxCOL_WIDTH_DEFAULT
)
3202 // TODO find a better calculation
3203 width
= wxDVC_DEFAULT_WIDTH
;
3206 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_FIXED
);
3207 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3211 void wxDataViewColumn::SetReorderable( bool reorderable
)
3213 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column
), reorderable
);
3216 bool wxDataViewColumn::IsReorderable() const
3218 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column
) );
3221 //-----------------------------------------------------------------------------
3222 // wxGtkTreeModelNode
3223 //-----------------------------------------------------------------------------
3226 class wxGtkTreeModelChildWithPos
3234 int wxGtkTreeModelChildWithPosCmp( const void* data1
, const void* data2
, const void* user_data
)
3236 const wxGtkTreeModelChildWithPos
* child1
= (const wxGtkTreeModelChildWithPos
*) data1
;
3237 const wxGtkTreeModelChildWithPos
* child2
= (const wxGtkTreeModelChildWithPos
*) data2
;
3238 const wxDataViewCtrlInternal
*internal
= (const wxDataViewCtrlInternal
*) user_data
;
3239 int ret
= internal
->GetDataViewModel()->Compare( child1
->id
, child2
->id
,
3240 internal
->GetSortColumn(), (internal
->GetSortOrder() == GTK_SORT_DESCENDING
) );
3246 int LINKAGEMODE
wxGtkTreeModelChildPtrCmp( void*** data1
, void*** data2
)
3248 return gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(**data1
), wxDataViewItem(**data2
),
3249 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
3252 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr
);
3255 void wxGtkTreeModelNode::Resort()
3257 size_t child_count
= GetChildCount();
3258 if (child_count
== 0)
3261 size_t node_count
= GetNodesCount();
3263 if (child_count
== 1)
3265 if (node_count
== 1)
3267 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
3273 gint
*new_order
= new gint
[child_count
];
3276 // m_children has the original *void
3277 // ptrs points to these
3278 wxGtkTreeModelChildrenPtr ptrs
;
3280 for (i
= 0; i
< child_count
; i
++)
3281 ptrs
.Add( &(m_children
[i
]) );
3283 gs_internal
= m_internal
;
3284 ptrs
.Sort( &wxGtkTreeModelChildPtrCmp
);
3286 wxGtkTreeModelChildren temp
;
3287 void** base_ptr
= &(m_children
[0]);
3288 // Transfer positions to new_order array and
3290 for (i
= 0; i
< child_count
; i
++)
3292 new_order
[i
] = ptrs
[i
] - base_ptr
;
3293 temp
.Add( *ptrs
[i
] );
3296 // Transfer IDs back to m_children
3298 WX_APPEND_ARRAY( temp
, m_children
);
3303 // Build up array with IDs and original positions
3304 wxGtkTreeModelChildWithPos
* temp
= new wxGtkTreeModelChildWithPos
[child_count
];
3306 for (i
= 0; i
< child_count
; i
++)
3309 temp
[i
].id
= m_children
[i
];
3311 // Sort array keeping original positions
3312 wxQsort( temp
, child_count
, sizeof(wxGtkTreeModelChildWithPos
),
3313 &wxGtkTreeModelChildWithPosCmp
, m_internal
);
3314 // Transfer positions to new_order array and
3315 // IDs to m_children
3317 for (i
= 0; i
< child_count
; i
++)
3319 new_order
[i
] = temp
[i
].pos
;
3320 m_children
.Add( temp
[i
].id
);
3329 wxGtkTreeModelChildren temp
;
3330 WX_APPEND_ARRAY( temp
, m_children
);
3332 gs_internal
= m_internal
;
3333 m_children
.Sort( &wxGtkTreeModelChildCmp
);
3336 for (pos
= 0; pos
< child_count
; pos
++)
3338 void *id
= m_children
.Item( pos
);
3339 int old_pos
= temp
.Index( id
);
3340 new_order
[pos
] = old_pos
;
3344 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
3347 iter
.user_data
= GetItem().GetID();
3348 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
3350 gtk_tree_model_rows_reordered( gtk_tree_model
,
3351 wxGtkTreePath(m_internal
->get_path(&iter
)), &iter
, new_order
);
3353 delete [] new_order
;
3356 for (pos
= 0; pos
< node_count
; pos
++)
3358 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
3363 //-----------------------------------------------------------------------------
3364 // wxDataViewCtrlInternal
3365 //-----------------------------------------------------------------------------
3367 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
)
3370 m_wx_model
= wx_model
;
3374 m_sort_order
= GTK_SORT_ASCENDING
;
3376 m_dataview_sort_column
= NULL
;
3378 m_dragDataObject
= NULL
;
3379 m_dropDataObject
= NULL
;
3383 m_gtk_model
= wxgtk_tree_model_new();
3384 m_gtk_model
->internal
= this;
3386 m_notifier
= new wxGtkDataViewModelNotifier( wx_model
, this );
3388 wx_model
->AddNotifier( m_notifier
);
3390 // g_object_unref( gtk_model ); ???
3392 if (!m_wx_model
->IsVirtualListModel())
3395 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner
->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model
) );
3398 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3400 m_wx_model
->RemoveNotifier( m_notifier
);
3402 // remove the model from the GtkTreeView before it gets destroyed
3403 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner
->GtkGetTreeView() ), NULL
);
3405 g_object_unref( m_gtk_model
);
3407 delete m_dragDataObject
;
3408 delete m_dropDataObject
;
3411 void wxDataViewCtrlInternal::ScheduleRefresh()
3416 void wxDataViewCtrlInternal::OnInternalIdle()
3420 GtkWidget
*widget
= m_owner
->GtkGetTreeView();
3421 gtk_widget_queue_draw( widget
);
3426 void wxDataViewCtrlInternal::InitTree()
3428 wxDataViewItem item
;
3429 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
3431 BuildBranch( m_root
);
3434 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
3436 if (node
->GetChildCount() == 0)
3438 wxDataViewItemArray children
;
3439 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
3442 for (pos
= 0; pos
< count
; pos
++)
3444 wxDataViewItem child
= children
[pos
];
3446 if (m_wx_model
->IsContainer( child
))
3447 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
3449 node
->AddLeaf( child
.GetID() );
3451 // Don't send any events here
3458 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat
&format
)
3460 wxGtkString
atom_str( gdk_atom_name( format
) );
3461 m_dragSourceTargetEntryTarget
= wxCharBuffer( atom_str
);
3463 m_dragSourceTargetEntry
.target
= m_dragSourceTargetEntryTarget
.data();
3464 m_dragSourceTargetEntry
.flags
= 0;
3465 m_dragSourceTargetEntry
.info
= static_cast<guint
>(-1);
3467 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3468 GDK_BUTTON1_MASK
, &m_dragSourceTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3473 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat
&format
)
3475 wxGtkString
atom_str( gdk_atom_name( format
) );
3476 m_dropTargetTargetEntryTarget
= wxCharBuffer( atom_str
);
3478 m_dropTargetTargetEntry
.target
= m_dropTargetTargetEntryTarget
.data();
3479 m_dropTargetTargetEntry
.flags
= 0;
3480 m_dropTargetTargetEntry
.info
= static_cast<guint
>(-1);
3482 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3483 &m_dropTargetTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3488 gboolean
wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource
*WXUNUSED(drag_source
),
3491 delete m_dragDataObject
;
3492 m_dragDataObject
= NULL
;
3494 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3498 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3499 event
.SetEventObject( m_owner
);
3500 event
.SetItem( item
);
3501 event
.SetModel( m_wx_model
);
3503 gtk_widget_get_pointer(m_owner
->GtkGetTreeView(), &x
, &y
);
3504 event
.SetPosition(x
, y
);
3505 if (!m_owner
->HandleWindowEvent( event
))
3508 if (!event
.IsAllowed())
3511 wxDataObject
*obj
= event
.GetDataObject();
3515 m_dragDataObject
= obj
;
3521 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource
*WXUNUSED(drag_source
),
3522 GtkTreePath
*WXUNUSED(path
))
3527 gboolean
wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource
*WXUNUSED(drag_source
),
3528 GtkTreePath
*path
, GtkSelectionData
*selection_data
)
3530 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3534 GdkAtom target
= gtk_selection_data_get_target(selection_data
);
3535 if (!m_dragDataObject
->IsSupported(target
))
3538 size_t size
= m_dragDataObject
->GetDataSize(target
);
3542 void *buf
= malloc( size
);
3544 gboolean res
= FALSE
;
3545 if (m_dragDataObject
->GetDataHere(target
, buf
))
3549 gtk_selection_data_set(selection_data
, target
,
3550 8, (const guchar
*) buf
, size
);
3559 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3561 GtkSelectionData
*selection_data
)
3563 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3567 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
3568 event
.SetEventObject( m_owner
);
3569 event
.SetItem( item
);
3570 event
.SetModel( m_wx_model
);
3571 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3572 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3573 event
.SetDataBuffer(const_cast<guchar
*>(gtk_selection_data_get_data(selection_data
)));
3574 if (!m_owner
->HandleWindowEvent( event
))
3577 if (!event
.IsAllowed())
3584 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3586 GtkSelectionData
*selection_data
)
3588 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3592 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
3593 event
.SetEventObject( m_owner
);
3594 event
.SetItem( item
);
3595 event
.SetModel( m_wx_model
);
3596 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3597 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3598 if (!m_owner
->HandleWindowEvent( event
))
3601 if (!event
.IsAllowed())
3607 // notifications from wxDataViewModel
3609 bool wxDataViewCtrlInternal::Cleared()
3624 void wxDataViewCtrlInternal::Resort()
3626 if (!m_wx_model
->IsVirtualListModel())
3632 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3634 if (!m_wx_model
->IsVirtualListModel())
3636 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3637 wxCHECK_MSG(parent_node
, false,
3638 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3640 wxDataViewItemArray modelSiblings
;
3641 m_wx_model
->GetChildren(parent
, modelSiblings
);
3642 const int modelSiblingsSize
= modelSiblings
.size();
3644 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
3645 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
3647 const wxGtkTreeModelChildren
& nodeSiblings
= parent_node
->GetChildren();
3648 const int nodeSiblingsSize
= nodeSiblings
.size();
3652 if ( posInModel
== modelSiblingsSize
- 1 )
3654 nodePos
= nodeSiblingsSize
;
3656 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
3658 // This is the simple case when our node tree already matches the
3659 // model and only this one item is missing.
3660 nodePos
= posInModel
;
3664 // It's possible that a larger discrepancy between the model and
3665 // our realization exists. This can happen e.g. when adding a bunch
3666 // of items to the model and then calling ItemsAdded() just once
3667 // afterwards. In this case, we must find the right position by
3668 // looking at sibling items.
3670 // append to the end if we won't find a better position:
3671 nodePos
= nodeSiblingsSize
;
3673 for ( int nextItemPos
= posInModel
+ 1;
3674 nextItemPos
< modelSiblingsSize
;
3677 int nextNodePos
= parent_node
->FindChildByItem(modelSiblings
[nextItemPos
]);
3678 if ( nextNodePos
!= wxNOT_FOUND
)
3680 nodePos
= nextNodePos
;
3686 if (m_wx_model
->IsContainer( item
))
3687 parent_node
->InsertNode( new wxGtkTreeModelNode( parent_node
, item
, this ), nodePos
);
3689 parent_node
->InsertLeaf( item
.GetID(), nodePos
);
3697 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3699 if (!m_wx_model
->IsVirtualListModel())
3701 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3702 wxASSERT_MSG(parent_node
,
3703 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3705 parent_node
->DeleteChild( item
.GetID() );
3713 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
3715 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3716 event
.SetEventObject( m_owner
);
3717 event
.SetModel( m_owner
->GetModel() );
3718 event
.SetItem( item
);
3719 m_owner
->HandleWindowEvent( event
);
3724 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int view_column
)
3726 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3727 event
.SetEventObject( m_owner
);
3728 event
.SetModel( m_owner
->GetModel() );
3729 event
.SetColumn( view_column
);
3730 event
.SetDataViewColumn( GetOwner()->GetColumn(view_column
) );
3731 event
.SetItem( item
);
3732 m_owner
->HandleWindowEvent( event
);
3739 GtkTreeModelFlags
wxDataViewCtrlInternal::get_flags()
3743 if ( m_wx_model
->IsListModel() )
3744 flags
|= GTK_TREE_MODEL_LIST_ONLY
;
3746 if ( !m_wx_model
->IsVirtualListModel() )
3747 flags
|= GTK_TREE_MODEL_ITERS_PERSIST
;
3749 return GtkTreeModelFlags(flags
);
3752 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
3755 if (m_wx_model
->IsVirtualListModel())
3757 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3759 unsigned int i
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
3761 if (i
>= wx_model
->GetCount())
3764 iter
->stamp
= m_gtk_model
->stamp
;
3765 // user_data is just the index +1
3766 iter
->user_data
= wxUIntToPtr(i
+1);
3772 int depth
= gtk_tree_path_get_depth( path
);
3774 wxGtkTreeModelNode
*node
= m_root
;
3777 for (i
= 0; i
< depth
; i
++)
3779 BuildBranch( node
);
3781 gint pos
= gtk_tree_path_get_indices (path
)[i
];
3782 if (pos
< 0) return FALSE
;
3783 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
3785 void* id
= node
->GetChildren().Item( (size_t) pos
);
3789 iter
->stamp
= m_gtk_model
->stamp
;
3790 iter
->user_data
= id
;
3794 size_t count
= node
->GetNodes().GetCount();
3796 for (pos2
= 0; pos2
< count
; pos2
++)
3798 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
3799 if (child_node
->GetItem().GetID() == id
)
3811 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
3813 // When this is called from ItemDeleted(), the item is already
3814 // deleted in the model.
3816 GtkTreePath
*retval
= gtk_tree_path_new ();
3818 if (m_wx_model
->IsVirtualListModel())
3820 // iter is root, add nothing
3821 if (!iter
->user_data
)
3824 // user_data is just the index +1
3825 int i
= ( (wxUIntPtr
) iter
->user_data
) -1;
3826 gtk_tree_path_append_index (retval
, i
);
3830 void *id
= iter
->user_data
;
3832 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
3835 int pos
= node
->GetChildren().Index( id
);
3837 gtk_tree_path_prepend_index( retval
, pos
);
3839 id
= node
->GetItem().GetID();
3840 node
= node
->GetParent();
3847 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
3849 if (m_wx_model
->IsVirtualListModel())
3851 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3853 // user_data is just the index +1
3854 int n
= ( (wxUIntPtr
) iter
->user_data
) -1;
3858 iter
->user_data
= NULL
;
3862 if (n
>= (int) wx_model
->GetCount()-1)
3864 iter
->user_data
= NULL
;
3868 // user_data is just the index +1 (+2 because we need the next)
3869 iter
->user_data
= wxUIntToPtr(n
+2);
3873 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
3874 if( parent
== NULL
)
3876 iter
->user_data
= NULL
;
3880 int pos
= parent
->GetChildren().Index( iter
->user_data
);
3882 if (pos
== (int) parent
->GetChildCount()-1)
3884 iter
->user_data
= NULL
;
3888 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
3894 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
3896 if (m_wx_model
->IsVirtualListModel())
3898 // this is a list, nodes have no children
3902 iter
->stamp
= m_gtk_model
->stamp
;
3903 iter
->user_data
= (gpointer
) 1;
3911 if (m_root
->GetChildCount() == 0) return FALSE
;
3912 iter
->stamp
= m_gtk_model
->stamp
;
3913 iter
->user_data
= (gpointer
) m_root
->GetChildren().Item( 0 );
3918 wxDataViewItem item
;
3920 item
= wxDataViewItem( (void*) parent
->user_data
);
3922 if (!m_wx_model
->IsContainer( item
))
3925 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3926 wxASSERT_MSG(parent_node
,
3927 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3929 BuildBranch( parent_node
);
3931 if (parent_node
->GetChildCount() == 0)
3934 iter
->stamp
= m_gtk_model
->stamp
;
3935 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
3941 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
3943 if (m_wx_model
->IsVirtualListModel())
3945 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3948 return (wx_model
->GetCount() > 0);
3950 // this is a list, nodes have no children
3956 return (m_root
->GetChildCount() > 0);
3958 wxDataViewItem
item( (void*) iter
->user_data
);
3960 bool is_container
= m_wx_model
->IsContainer( item
);
3965 wxGtkTreeModelNode
*node
= FindNode( iter
);
3967 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3969 BuildBranch( node
);
3971 return (node
->GetChildCount() > 0);
3975 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
3977 if (m_wx_model
->IsVirtualListModel())
3979 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3982 return (gint
) wx_model
->GetCount();
3989 return m_root
->GetChildCount();
3991 wxDataViewItem
item( (void*) iter
->user_data
);
3993 if (!m_wx_model
->IsContainer( item
))
3996 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
3997 wxASSERT_MSG(parent_node
,
3998 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4000 BuildBranch( parent_node
);
4002 return parent_node
->GetChildCount();
4006 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
4008 if (m_wx_model
->IsVirtualListModel())
4010 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
4018 if (n
>= (gint
) wx_model
->GetCount())
4021 iter
->stamp
= m_gtk_model
->stamp
;
4022 // user_data is just the index +1
4023 iter
->user_data
= wxUIntToPtr(n
+1);
4030 if (parent
) id
= (void*) parent
->user_data
;
4031 wxDataViewItem
item( id
);
4033 if (!m_wx_model
->IsContainer( item
))
4036 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4037 wxASSERT_MSG(parent_node
,
4038 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4040 BuildBranch( parent_node
);
4042 iter
->stamp
= m_gtk_model
->stamp
;
4043 iter
->user_data
= parent_node
->GetChildren().Item( n
);
4049 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
4051 if (m_wx_model
->IsVirtualListModel())
4057 wxGtkTreeModelNode
*node
= FindParentNode( child
);
4061 iter
->stamp
= m_gtk_model
->stamp
;
4062 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
4068 // item can be deleted already in the model
4069 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
4071 if (m_wx_model
->IsVirtualListModel())
4073 return wxPtrToUInt(item
.GetID()) - 1;
4077 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4078 wxGtkTreeModelChildren
&children
= parent_node
->GetChildren();
4080 for (j
= 0; j
< children
.GetCount(); j
++)
4082 if (children
[j
] == item
.GetID())
4090 static wxGtkTreeModelNode
*
4091 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4097 list
.DeleteContents( true );
4098 wxDataViewItem
it( item
);
4102 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4103 list
.Insert( pItem
);
4104 it
= model
->GetParent( it
);
4107 wxGtkTreeModelNode
* node
= treeNode
;
4108 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4110 if( node
&& node
->GetNodes().GetCount() != 0 )
4112 int len
= node
->GetNodes().GetCount();
4113 wxGtkTreeModelNodes
&nodes
= node
->GetNodes();
4115 for( ; j
< len
; j
++)
4117 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4136 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
4141 wxDataViewItem
item( (void*) iter
->user_data
);
4145 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4150 wxLogDebug( "Not found %p", iter->user_data );
4154 // TODO: remove this code
4160 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
4165 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4170 wxLogDebug( "Not found %p", item.GetID() );
4174 // TODO: remove this code
4180 static wxGtkTreeModelNode
*
4181 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4187 list
.DeleteContents( true );
4191 wxDataViewItem
it( model
->GetParent( item
) );
4194 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4195 list
.Insert( pItem
);
4196 it
= model
->GetParent( it
);
4199 wxGtkTreeModelNode
* node
= treeNode
;
4200 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4202 if( node
&& node
->GetNodes().GetCount() != 0 )
4204 int len
= node
->GetNodes().GetCount();
4205 wxGtkTreeModelNodes nodes
= node
->GetNodes();
4207 for( ; j
< len
; j
++)
4209 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4224 //Examine whether the node is item's parent node
4225 int len
= node
->GetChildCount();
4226 for( int i
= 0; i
< len
; i
++ )
4228 if( node
->GetChildren().Item( i
) == item
.GetID() )
4234 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
4239 wxDataViewItem
item( (void*) iter
->user_data
);
4243 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4246 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
4251 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4254 //-----------------------------------------------------------------------------
4255 // wxDataViewCtrl signal callbacks
4256 //-----------------------------------------------------------------------------
4259 wxdataview_selection_changed_callback( GtkTreeSelection
* WXUNUSED(selection
), wxDataViewCtrl
*dv
)
4261 if (!gtk_widget_get_realized(dv
->m_widget
))
4264 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, dv
->GetId() );
4265 event
.SetItem( dv
->GetSelection() );
4266 event
.SetModel( dv
->GetModel() );
4267 dv
->HandleWindowEvent( event
);
4271 wxdataview_row_activated_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreePath
*path
,
4272 GtkTreeViewColumn
*WXUNUSED(column
), wxDataViewCtrl
*dv
)
4274 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
4276 wxDataViewItem
item(dv
->GTKPathToItem(path
));
4277 event
.SetItem( item
);
4278 event
.SetModel( dv
->GetModel() );
4279 dv
->HandleWindowEvent( event
);
4283 wxdataview_test_expand_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4284 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4286 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, dv
->GetId() );
4288 wxDataViewItem
item( (void*) iter
->user_data
);;
4289 event
.SetItem( item
);
4290 event
.SetModel( dv
->GetModel() );
4291 dv
->HandleWindowEvent( event
);
4293 return !event
.IsAllowed();
4297 wxdataview_row_expanded_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4298 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4300 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
, dv
->GetId() );
4302 wxDataViewItem
item( (void*) iter
->user_data
);;
4303 event
.SetItem( item
);
4304 event
.SetModel( dv
->GetModel() );
4305 dv
->HandleWindowEvent( event
);
4309 wxdataview_test_collapse_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4310 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4312 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
, dv
->GetId() );
4314 wxDataViewItem
item( (void*) iter
->user_data
);;
4315 event
.SetItem( item
);
4316 event
.SetModel( dv
->GetModel() );
4317 dv
->HandleWindowEvent( event
);
4319 return !event
.IsAllowed();
4323 wxdataview_row_collapsed_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4324 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4326 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
, dv
->GetId() );
4328 wxDataViewItem
item( (void*) iter
->user_data
);;
4329 event
.SetItem( item
);
4330 event
.SetModel( dv
->GetModel() );
4331 dv
->HandleWindowEvent( event
);
4334 //-----------------------------------------------------------------------------
4336 //-----------------------------------------------------------------------------
4338 void wxDataViewCtrl::AddChildGTK(wxWindowGTK
* child
)
4340 GtkWidget
* treeview
= GtkGetTreeView();
4342 // Insert widget in GtkTreeView
4343 if (gtk_widget_get_realized(treeview
))
4344 gtk_widget_set_parent_window( child
->m_widget
,
4345 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
4346 gtk_widget_set_parent( child
->m_widget
, treeview
);
4350 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
4351 GtkAllocation
*WXUNUSED(gtk_alloc
),
4352 wxDataViewCtrl
*win
)
4354 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
4357 wxWindow
*child
= node
->GetData();
4360 gtk_widget_size_request( child
->m_widget
, &req
);
4362 GtkAllocation alloc
;
4363 alloc
.x
= child
->m_x
;
4364 alloc
.y
= child
->m_y
;
4365 alloc
.width
= child
->m_width
;
4366 alloc
.height
= child
->m_height
;
4367 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
4369 node
= node
->GetNext();
4374 //-----------------------------------------------------------------------------
4375 // "motion_notify_event"
4376 //-----------------------------------------------------------------------------
4379 gtk_dataview_motion_notify_callback( GtkWidget
*WXUNUSED(widget
),
4380 GdkEventMotion
*gdk_event
,
4381 wxDataViewCtrl
*dv
)
4383 if (gdk_event
->is_hint
)
4387 GdkModifierType state
;
4388 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
4394 GtkTreeViewColumn
*column
= NULL
;
4397 if (gtk_tree_view_get_path_at_pos(
4398 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4399 (int) gdk_event
->x
, (int) gdk_event
->y
,
4408 dv
->GtkGetInternal()->get_iter( &iter
, path
);
4416 //-----------------------------------------------------------------------------
4417 // "button_press_event"
4418 //-----------------------------------------------------------------------------
4421 gtk_dataview_button_press_callback( GtkWidget
*WXUNUSED(widget
),
4422 GdkEventButton
*gdk_event
,
4423 wxDataViewCtrl
*dv
)
4425 if ((gdk_event
->button
== 3) && (gdk_event
->type
== GDK_BUTTON_PRESS
))
4428 GtkTreeViewColumn
*column
= NULL
;
4431 gtk_tree_view_get_path_at_pos
4433 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4434 (int) gdk_event
->x
, (int) gdk_event
->y
,
4441 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, dv
->GetId() );
4443 event
.SetItem(dv
->GTKPathToItem(path
));
4444 event
.SetModel( dv
->GetModel() );
4445 return dv
->HandleWindowEvent( event
);
4451 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4453 wxDataViewCtrl::~wxDataViewCtrl()
4455 // Stop editing before destroying the control to remove any event handlers
4456 // which are added when editing started: if we didn't do this, the base
4457 // class dtor would assert as it checks for any leftover handlers.
4460 GtkTreeViewColumn
*col
;
4461 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4463 wxDataViewColumn
* const wxcol
= FromGTKColumn(col
);
4466 // This won't do anything if we're not editing it
4467 wxcol
->GetRenderer()->CancelEditing();
4476 void wxDataViewCtrl::Init()
4480 m_cols
.DeleteContents( true );
4482 m_uniformRowHeight
= -1;
4485 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4490 const wxValidator
& validator
,
4491 const wxString
& name
)
4493 if (!PreCreation( parent
, pos
, size
) ||
4494 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
4496 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4500 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
4501 g_object_ref(m_widget
);
4503 GTKScrolledWindowSetBorder(m_widget
, style
);
4505 m_treeview
= gtk_tree_view_new();
4506 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
4508 m_focusWidget
= GTK_WIDGET(m_treeview
);
4510 g_signal_connect (m_treeview
, "size_allocate",
4511 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
4514 if (!gtk_check_version(2,6,0))
4516 bool fixed
= (style
& wxDV_VARIABLE_LINE_HEIGHT
) == 0;
4517 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 );
4530 if (!gtk_check_version(2,10,0))
4532 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
4534 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
4535 (style
& wxDV_VERT_RULES
) != 0)
4536 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
4537 else if (style
& wxDV_VERT_RULES
)
4538 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
4539 else if (style
& wxDV_HORIZ_RULES
)
4540 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
4542 if (grid
!= GTK_TREE_VIEW_GRID_LINES_NONE
)
4543 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
4547 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_ROW_LINES
) != 0 );
4549 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
4550 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
4551 gtk_widget_show (m_treeview
);
4553 m_parent
->DoAddChild( this );
4557 GtkEnableSelectionEvents();
4559 g_signal_connect_after (m_treeview
, "row-activated",
4560 G_CALLBACK (wxdataview_row_activated_callback
), this);
4562 g_signal_connect (m_treeview
, "test-collapse-row",
4563 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
4565 g_signal_connect_after (m_treeview
, "row-collapsed",
4566 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
4568 g_signal_connect (m_treeview
, "test-expand-row",
4569 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
4571 g_signal_connect_after (m_treeview
, "row-expanded",
4572 G_CALLBACK (wxdataview_row_expanded_callback
), this);
4574 g_signal_connect (m_treeview
, "motion_notify_event",
4575 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
4577 g_signal_connect (m_treeview
, "button_press_event",
4578 G_CALLBACK (gtk_dataview_button_press_callback
), this);
4583 wxDataViewItem
wxDataViewCtrl::GTKPathToItem(GtkTreePath
*path
) const
4586 return wxDataViewItem(path
&& m_internal
->get_iter(&iter
, path
)
4591 void wxDataViewCtrl::OnInternalIdle()
4593 wxWindow::OnInternalIdle();
4595 m_internal
->OnInternalIdle();
4597 unsigned int cols
= GetColumnCount();
4599 for (i
= 0; i
< cols
; i
++)
4601 wxDataViewColumn
*col
= GetColumn( i
);
4602 col
->OnInternalIdle();
4605 if (m_ensureVisibleDefered
.IsOk())
4607 ExpandAncestors(m_ensureVisibleDefered
);
4609 iter
.user_data
= (gpointer
) m_ensureVisibleDefered
.GetID();
4610 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4611 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
4612 m_ensureVisibleDefered
= wxDataViewItem(0);
4616 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4618 wxDELETE(m_internal
);
4620 if (!wxDataViewCtrlBase::AssociateModel( model
))
4624 if (!gtk_check_version(2,6,0))
4626 bool fixed
= (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
) == 0) || (model
->IsVirtualListModel()));
4627 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4631 m_internal
= new wxDataViewCtrlInternal( this, model
);
4636 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4638 return m_internal
->EnableDragSource( format
);
4641 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4643 return m_internal
->EnableDropTarget( format
);
4646 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4648 if (!wxDataViewCtrlBase::AppendColumn(col
))
4651 m_cols
.Append( col
);
4654 if (!gtk_check_version(2,6,0))
4656 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4657 GTK_TREE_VIEW_COLUMN_FIXED
)
4658 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4662 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
4663 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4668 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4670 if (!wxDataViewCtrlBase::PrependColumn(col
))
4673 m_cols
.Insert( col
);
4676 if (!gtk_check_version(2,6,0))
4678 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4679 GTK_TREE_VIEW_COLUMN_FIXED
)
4680 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4684 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4685 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
4690 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4692 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4695 m_cols
.Insert( pos
, col
);
4698 if (!gtk_check_version(2,6,0))
4700 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4701 GTK_TREE_VIEW_COLUMN_FIXED
)
4702 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4706 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4707 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), pos
);
4712 unsigned int wxDataViewCtrl::GetColumnCount() const
4714 return m_cols
.GetCount();
4717 wxDataViewColumn
* wxDataViewCtrl::FromGTKColumn(GtkTreeViewColumn
*gtk_col
) const
4722 wxDataViewColumnList::const_iterator iter
;
4723 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4725 wxDataViewColumn
*col
= *iter
;
4726 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
4732 wxFAIL_MSG( "No matching column?" );
4737 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
4739 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
4741 return FromGTKColumn(gtk_col
);
4744 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4746 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4747 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
4749 m_cols
.DeleteObject( column
);
4754 bool wxDataViewCtrl::ClearColumns()
4756 wxDataViewColumnList::iterator iter
;
4757 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4759 wxDataViewColumn
*col
= *iter
;
4760 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4761 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4769 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4771 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4773 wxGtkList
list(gtk_tree_view_get_columns(GTK_TREE_VIEW(m_treeview
)));
4775 return g_list_index( list
, (gconstpointer
) gtk_column
);
4778 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4780 return m_internal
->GetDataViewSortColumn();
4783 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4786 iter
.user_data
= item
.GetID();
4787 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4788 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
4791 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4794 iter
.user_data
= item
.GetID();
4795 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4796 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
4799 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4802 iter
.user_data
= item
.GetID();
4803 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4804 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview
), path
);
4807 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4809 // The tree doesn't have any current item if it hadn't been created yet but
4810 // it's arguably not an error to call this function in this case so just
4811 // return an invalid item without asserting.
4813 return wxDataViewItem();
4816 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), path
.ByRef(), NULL
);
4818 return GTKPathToItem(path
);
4821 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4823 wxCHECK_RET( m_treeview
,
4824 "Current item can't be set before creating the control." );
4826 // We need to make sure the model knows about this item or the path would
4827 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4828 ExpandAncestors(item
);
4830 // We also need to preserve the existing selection from changing.
4831 // Unfortunately the only way to do it seems to use our own selection
4832 // function and forbid any selection changes during set cursor call.
4833 wxGtkTreeSelectionLock
4834 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4836 // Do move the cursor now.
4838 iter
.user_data
= item
.GetID();
4839 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4841 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, NULL
, FALSE
);
4844 wxDataViewColumn
*wxDataViewCtrl::GetCurrentColumn() const
4846 // The tree doesn't have any current item if it hadn't been created yet but
4847 // it's arguably not an error to call this function in this case so just
4848 // return NULL without asserting.
4852 GtkTreeViewColumn
*col
;
4853 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4854 return FromGTKColumn(col
);
4857 void wxDataViewCtrl::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
4859 wxCHECK_RET( m_treeview
,
4860 "Current item can't be set before creating the control." );
4861 wxCHECK_RET( item
.IsOk(), "invalid item" );
4862 wxCHECK_RET( column
, "no column provided" );
4864 // We need to make sure the model knows about this item or the path would
4865 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4866 ExpandAncestors(item
);
4868 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4870 // We also need to preserve the existing selection from changing.
4871 // Unfortunately the only way to do it seems to use our own selection
4872 // function and forbid any selection changes during set cursor call.
4873 wxGtkTreeSelectionLock
4874 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4876 // Do move the cursor now.
4878 iter
.user_data
= item
.GetID();
4879 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4881 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, gcolumn
, TRUE
);
4884 int wxDataViewCtrl::GetSelectedItemsCount() const
4886 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4888 return gtk_tree_selection_count_selected_rows(selection
);
4891 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4895 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4896 if (HasFlag(wxDV_MULTIPLE
))
4898 GtkTreeModel
*model
;
4899 wxGtkTreePathList
list(gtk_tree_selection_get_selected_rows(selection
, &model
));
4901 for ( GList
* current
= list
; current
; current
= g_list_next(current
) )
4903 GtkTreePath
*path
= (GtkTreePath
*) current
->data
;
4905 sel
.Add(GTKPathToItem(path
));
4911 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
4913 sel
.Add( wxDataViewItem(iter
.user_data
) );
4920 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4922 GtkDisableSelectionEvents();
4924 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4926 gtk_tree_selection_unselect_all( selection
);
4928 wxDataViewItem last_parent
;
4931 for (i
= 0; i
< sel
.GetCount(); i
++)
4933 wxDataViewItem item
= sel
[i
];
4934 wxDataViewItem parent
= GetModel()->GetParent( item
);
4937 if (parent
!= last_parent
)
4938 ExpandAncestors(item
);
4940 last_parent
= parent
;
4943 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4944 iter
.user_data
= (gpointer
) item
.GetID();
4945 gtk_tree_selection_select_iter( selection
, &iter
);
4948 GtkEnableSelectionEvents();
4951 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4953 ExpandAncestors(item
);
4955 GtkDisableSelectionEvents();
4957 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4960 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4961 iter
.user_data
= (gpointer
) item
.GetID();
4962 gtk_tree_selection_select_iter( selection
, &iter
);
4964 GtkEnableSelectionEvents();
4967 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4969 GtkDisableSelectionEvents();
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();
4976 gtk_tree_selection_unselect_iter( selection
, &iter
);
4978 GtkEnableSelectionEvents();
4981 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4983 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4986 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4987 iter
.user_data
= (gpointer
) item
.GetID();
4989 return gtk_tree_selection_iter_is_selected( selection
, &iter
);
4992 void wxDataViewCtrl::SelectAll()
4994 GtkDisableSelectionEvents();
4996 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4998 gtk_tree_selection_select_all( selection
);
5000 GtkEnableSelectionEvents();
5003 void wxDataViewCtrl::UnselectAll()
5005 GtkDisableSelectionEvents();
5007 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5009 gtk_tree_selection_unselect_all( selection
);
5011 GtkEnableSelectionEvents();
5014 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem
& item
,
5015 const wxDataViewColumn
*WXUNUSED(column
))
5017 m_ensureVisibleDefered
= item
;
5018 ExpandAncestors(item
);
5021 iter
.user_data
= (gpointer
) item
.GetID();
5022 wxGtkTreePath
path(m_internal
->get_path( &iter
));
5023 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
5026 void wxDataViewCtrl::HitTest(const wxPoint
& point
,
5027 wxDataViewItem
& item
,
5028 wxDataViewColumn
*& column
) const
5030 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
5031 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
5032 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
5033 // So we have to use both of them.
5034 item
= wxDataViewItem(0);
5036 wxGtkTreePath path
, pathScratch
;
5037 GtkTreeViewColumn
* GtkColumn
= NULL
;
5038 GtkTreeViewDropPosition pos
= GTK_TREE_VIEW_DROP_INTO_OR_AFTER
;
5042 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5043 wxDataViewCtrl
* self
= const_cast<wxDataViewCtrl
*>(this); // ugly workaround, self is NOT const
5044 GtkTreeView
* treeView
= GTK_TREE_VIEW(self
->GtkGetTreeView());
5046 // is there possibly a better suited function to get the column?
5047 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5049 (int) point
.x
, (int) point
.y
,
5050 pathScratch
.ByRef(),
5051 &GtkColumn
, // here we get the GtkColumn
5055 if ( GtkColumn
!= NULL
)
5057 // we got GTK column
5058 // the right call now which takes the header into account
5059 gtk_tree_view_get_dest_row_at_pos( treeView
, (int) point
.x
, (int) point
.y
, path
.ByRef(), &pos
);
5062 item
= wxDataViewItem(GTKPathToItem(path
));
5063 // else we got a GTK column but the position is not over an item, e.g. below last item
5064 for ( unsigned int i
=0, cols
=GetColumnCount(); i
<cols
; ++i
) // search the wx column
5066 wxDataViewColumn
* col
= GetColumn(i
);
5067 if ( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == GtkColumn
)
5069 column
= col
; // here we get the wx column
5074 // else no column and thus no item, both null
5078 wxDataViewCtrl::GetItemRect(const wxDataViewItem
& WXUNUSED(item
),
5079 const wxDataViewColumn
*WXUNUSED(column
)) const
5084 bool wxDataViewCtrl::SetRowHeight(int rowHeight
)
5086 m_uniformRowHeight
= rowHeight
;
5090 void wxDataViewCtrl::DoSetExpanderColumn()
5092 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
5093 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5096 void wxDataViewCtrl::DoSetIndent()
5100 void wxDataViewCtrl::GtkDisableSelectionEvents()
5102 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5103 g_signal_handlers_disconnect_by_func( selection
,
5104 (gpointer
) (wxdataview_selection_changed_callback
), this);
5107 void wxDataViewCtrl::GtkEnableSelectionEvents()
5109 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5110 g_signal_connect_after (selection
, "changed",
5111 G_CALLBACK (wxdataview_selection_changed_callback
), this);
5114 // ----------------------------------------------------------------------------
5115 // visual attributes stuff
5116 // ----------------------------------------------------------------------------
5120 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
5122 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
5125 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
5127 wxDataViewCtrlBase::DoApplyWidgetStyle(style
);
5128 gtk_widget_modify_style(m_treeview
, style
);
5131 #endif // !wxUSE_GENERICDATAVIEWCTRL
5133 #endif // wxUSE_DATAVIEWCTRL