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() { 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_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
488 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
489 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
490 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
492 GType
gtk_wx_tree_model_get_type (void);
494 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
496 struct _GtkWxTreeModel
502 wxDataViewCtrlInternal
*internal
;
505 struct _GtkWxTreeModelClass
507 GObjectClass list_parent_class
;
510 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
511 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
512 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
514 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
515 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
516 static void wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface
*iface
);
517 static void wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface
*iface
);
519 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
520 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
521 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
523 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
526 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
528 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
532 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
534 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
536 GtkTreeIter
*parent
);
537 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
539 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
541 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
545 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
550 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
551 gint
*sort_column_id
,
553 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
556 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
558 GtkTreeIterCompareFunc func
,
560 GDestroyNotify destroy
);
561 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
562 GtkTreeIterCompareFunc func
,
564 GDestroyNotify destroy
);
565 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
568 static gboolean
wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
570 static gboolean
wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
572 static gboolean
wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
574 GtkSelectionData
*selection_data
);
575 static gboolean
wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
577 GtkSelectionData
*selection_data
);
578 static gboolean
wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
579 GtkTreePath
*dest_path
,
580 GtkSelectionData
*selection_data
);
583 static GObjectClass
*list_parent_class
= NULL
;
586 gtk_wx_tree_model_get_type (void)
588 static GType tree_model_type
= 0;
590 if (!tree_model_type
)
592 const GTypeInfo tree_model_info
=
594 sizeof (GtkWxTreeModelClass
),
595 NULL
, /* base_init */
596 NULL
, /* base_finalize */
597 (GClassInitFunc
) wxgtk_tree_model_class_init
,
598 NULL
, /* class_finalize */
599 NULL
, /* class_data */
600 sizeof (GtkWxTreeModel
),
602 (GInstanceInitFunc
) wxgtk_tree_model_init
,
605 static const GInterfaceInfo tree_model_iface_info
=
607 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
612 static const GInterfaceInfo sortable_iface_info
=
614 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
619 static const GInterfaceInfo drag_source_iface_info
=
621 (GInterfaceInitFunc
) wxgtk_tree_model_drag_source_init
,
626 static const GInterfaceInfo drag_dest_iface_info
=
628 (GInterfaceInitFunc
) wxgtk_tree_model_drag_dest_init
,
633 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
634 &tree_model_info
, (GTypeFlags
)0 );
636 g_type_add_interface_static (tree_model_type
,
638 &tree_model_iface_info
);
639 g_type_add_interface_static (tree_model_type
,
640 GTK_TYPE_TREE_SORTABLE
,
641 &sortable_iface_info
);
642 g_type_add_interface_static (tree_model_type
,
643 GTK_TYPE_TREE_DRAG_DEST
,
644 &drag_dest_iface_info
);
645 g_type_add_interface_static (tree_model_type
,
646 GTK_TYPE_TREE_DRAG_SOURCE
,
647 &drag_source_iface_info
);
650 return tree_model_type
;
653 static GtkWxTreeModel
*
654 wxgtk_tree_model_new(void)
656 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
661 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
663 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
667 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
669 iface
->get_flags
= wxgtk_tree_model_get_flags
;
670 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
671 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
672 iface
->get_iter
= wxgtk_tree_model_get_iter
;
673 iface
->get_path
= wxgtk_tree_model_get_path
;
674 iface
->get_value
= wxgtk_tree_model_get_value
;
675 iface
->iter_next
= wxgtk_tree_model_iter_next
;
676 iface
->iter_children
= wxgtk_tree_model_iter_children
;
677 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
678 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
679 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
680 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
684 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
686 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
687 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
688 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
689 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
690 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
694 wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface
*iface
)
696 iface
->row_draggable
= wxgtk_tree_model_row_draggable
;
697 iface
->drag_data_delete
= wxgtk_tree_model_drag_data_delete
;
698 iface
->drag_data_get
= wxgtk_tree_model_drag_data_get
;
702 wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface
*iface
)
704 iface
->drag_data_received
= wxgtk_tree_model_drag_data_received
;
705 iface
->row_drop_possible
= wxgtk_tree_model_row_drop_possible
;
709 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
711 tree_model
->internal
= NULL
;
712 tree_model
->stamp
= g_random_int();
717 //-----------------------------------------------------------------------------
718 // implement callbacks from wxGtkTreeModel class by letting
719 // them call the methods of wxWidgets' wxDataViewModel
720 //-----------------------------------------------------------------------------
722 static GtkTreeModelFlags
723 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
725 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
726 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), (GtkTreeModelFlags
)0 );
728 return wxtree_model
->internal
->get_flags();
732 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
734 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
735 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
737 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
741 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
744 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
745 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
747 GType gtype
= G_TYPE_INVALID
;
749 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
751 if (wxtype
== wxT("string"))
752 gtype
= G_TYPE_STRING
;
755 gtype
= G_TYPE_POINTER
;
756 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
763 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
767 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
768 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
769 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
771 return wxtree_model
->internal
->get_iter( iter
, path
);
775 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
778 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
779 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
780 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
782 return wxtree_model
->internal
->get_path( iter
);
786 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
791 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
792 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
794 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
795 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
796 if (mtype
== wxT("string"))
799 g_value_init( value
, G_TYPE_STRING
);
800 wxDataViewItem
item( (void*) iter
->user_data
);
801 model
->GetValue( variant
, item
, (unsigned int) column
);
803 g_value_set_string( value
, variant
.GetString().utf8_str() );
807 wxFAIL_MSG( wxT("non-string columns not supported yet") );
812 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
815 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
817 // This happens when clearing the view by calling .._set_model( NULL );
818 if (iter
->stamp
== 0) return FALSE
;
820 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
821 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
823 return wxtree_model
->internal
->iter_next( iter
);
827 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
831 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
832 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
835 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
838 return wxtree_model
->internal
->iter_children( iter
, parent
);
842 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
845 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
846 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
847 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
849 return wxtree_model
->internal
->iter_has_child( iter
);
853 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
856 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
857 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
858 g_return_val_if_fail ( !iter
|| wxtree_model
->stamp
== iter
->stamp
, 0);
860 return wxtree_model
->internal
->iter_n_children( iter
);
864 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
869 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
870 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
872 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
876 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
880 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
881 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
882 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
884 return wxtree_model
->internal
->iter_parent( iter
, child
);
887 /* drag'n'drop iface */
889 wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
892 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
893 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
895 return wxtree_model
->internal
->row_draggable( drag_source
, path
);
899 wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
902 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
903 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
905 return wxtree_model
->internal
->drag_data_delete( drag_source
, path
);
909 wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
911 GtkSelectionData
*selection_data
)
913 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
914 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
917 wxPrintf( "drag_get_data\n");
919 wxGtkString
atom_selection(gdk_atom_name(selection_data
->selection
));
920 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection
) );
922 wxGtkString
atom_target(gdk_atom_name(selection_data
->target
));
923 wxPrintf( "target %s\n", wxString::FromAscii(atom_target
) );
925 wxGtkString
atom_type(gdk_atom_name(selection_data
->type
));
926 wxPrintf( "type %s\n", wxString::FromAscii(atom_type
) );
928 wxPrintf( "format %d\n", selection_data
->format
);
931 return wxtree_model
->internal
->drag_data_get( drag_source
, path
, selection_data
);
935 wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
937 GtkSelectionData
*selection_data
)
939 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
940 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
942 return wxtree_model
->internal
->drag_data_received( drag_dest
, dest
, selection_data
);
946 wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
947 GtkTreePath
*dest_path
,
948 GtkSelectionData
*selection_data
)
950 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
951 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
953 return wxtree_model
->internal
->row_drop_possible( drag_dest
, dest_path
, selection_data
);
958 wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
959 gint
*sort_column_id
,
962 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) sortable
;
964 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
966 if (!wxtree_model
->internal
->IsSorted())
969 *sort_column_id
= -1;
976 *sort_column_id
= wxtree_model
->internal
->GetSortColumn();
979 *order
= wxtree_model
->internal
->GetSortOrder();
984 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
987 wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
991 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
992 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
994 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
996 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
997 (order
!= tree_model
->internal
->GetSortOrder()))
999 tree_model
->internal
->SetSortColumn( sort_column_id
);
1000 tree_model
->internal
->SetSortOrder( order
);
1002 gtk_tree_sortable_sort_column_changed (sortable
);
1004 tree_model
->internal
->GetDataViewModel()->Resort();
1007 if (gs_lastLeftClickHeader
)
1009 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
1010 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
1011 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
1012 event
.SetModel( dv
->GetModel() );
1013 dv
->HandleWindowEvent( event
);
1016 gs_lastLeftClickHeader
= NULL
;
1020 wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
1021 gint
WXUNUSED(sort_column_id
),
1022 GtkTreeIterCompareFunc func
,
1023 gpointer
WXUNUSED(data
),
1024 GDestroyNotify
WXUNUSED(destroy
))
1026 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1027 g_return_if_fail (func
!= NULL
);
1031 wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
1032 GtkTreeIterCompareFunc func
,
1033 gpointer
WXUNUSED(data
),
1034 GDestroyNotify
WXUNUSED(destroy
))
1036 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1037 g_return_if_fail (func
!= NULL
);
1039 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
1040 // TODO: remove this code
1044 wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
1046 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
1051 //-----------------------------------------------------------------------------
1052 // define new GTK+ class GtkWxRendererText
1053 //-----------------------------------------------------------------------------
1057 #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
1058 #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
1059 #define GTK_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1060 #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1061 #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1062 #define GTK_WX_CELL_RENDERER_TEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1064 GType
gtk_wx_cell_renderer_text_get_type (void);
1066 typedef struct _GtkWxCellRendererText GtkWxCellRendererText
;
1067 typedef struct _GtkWxCellRendererTextClass GtkWxCellRendererTextClass
;
1069 struct _GtkWxCellRendererText
1071 GtkCellRendererText parent
;
1073 wxDataViewRenderer
*wx_renderer
;
1076 struct _GtkWxCellRendererTextClass
1078 GtkCellRendererTextClass cell_parent_class
;
1082 static GtkWxCellRendererText
*gtk_wx_cell_renderer_text_new (void);
1083 static void gtk_wx_cell_renderer_text_init (
1084 GtkWxCellRendererText
*cell
);
1085 static void gtk_wx_cell_renderer_text_class_init(
1086 GtkWxCellRendererTextClass
*klass
);
1087 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1088 GtkCellRenderer
*cell
,
1092 GdkRectangle
*background_area
,
1093 GdkRectangle
*cell_area
,
1094 GtkCellRendererState flags
);
1097 static GObjectClass
*text_cell_parent_class
= NULL
;
1102 gtk_wx_cell_renderer_text_get_type (void)
1104 static GType cell_wx_type
= 0;
1108 const GTypeInfo cell_wx_info
=
1110 sizeof (GtkWxCellRendererTextClass
),
1111 NULL
, /* base_init */
1112 NULL
, /* base_finalize */
1113 (GClassInitFunc
) gtk_wx_cell_renderer_text_class_init
,
1114 NULL
, /* class_finalize */
1115 NULL
, /* class_data */
1116 sizeof (GtkWxCellRendererText
),
1117 0, /* n_preallocs */
1118 (GInstanceInitFunc
) gtk_wx_cell_renderer_text_init
,
1121 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT
,
1122 "GtkWxCellRendererText", &cell_wx_info
, (GTypeFlags
)0 );
1125 return cell_wx_type
;
1129 gtk_wx_cell_renderer_text_init (GtkWxCellRendererText
*cell
)
1131 cell
->wx_renderer
= NULL
;
1135 gtk_wx_cell_renderer_text_class_init (GtkWxCellRendererTextClass
*klass
)
1137 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1139 text_cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1141 cell_class
->start_editing
= gtk_wx_cell_renderer_text_start_editing
;
1144 GtkWxCellRendererText
*
1145 gtk_wx_cell_renderer_text_new (void)
1147 return (GtkWxCellRendererText
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT
, NULL
);
1150 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1151 GtkCellRenderer
*gtk_renderer
,
1152 GdkEvent
*gdk_event
,
1155 GdkRectangle
*background_area
,
1156 GdkRectangle
*cell_area
,
1157 GtkCellRendererState flags
)
1159 GtkWxCellRendererText
*wxgtk_renderer
= (GtkWxCellRendererText
*) gtk_renderer
;
1160 wxDataViewRenderer
*wx_renderer
= wxgtk_renderer
->wx_renderer
;
1161 wxDataViewColumn
*column
= wx_renderer
->GetOwner();
1164 item(column
->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1166 wxDataViewCtrl
*dv
= column
->GetOwner();
1167 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING
, dv
->GetId() );
1168 event
.SetDataViewColumn( column
);
1169 event
.SetModel( dv
->GetModel() );
1170 event
.SetColumn( column
->GetModelColumn() );
1171 event
.SetItem( item
);
1172 dv
->HandleWindowEvent( event
);
1174 if (event
.IsAllowed())
1175 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class
)->
1176 start_editing( gtk_renderer
, gdk_event
, widget
, path
, background_area
, cell_area
, flags
);
1181 //-----------------------------------------------------------------------------
1182 // define new GTK+ class GtkWxCellRenderer
1183 //-----------------------------------------------------------------------------
1187 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
1188 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
1189 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1190 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
1191 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
1192 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1194 GType
gtk_wx_cell_renderer_get_type (void);
1196 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
1197 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
1199 struct _GtkWxCellRenderer
1201 GtkCellRenderer parent
;
1204 wxDataViewCustomRenderer
*cell
;
1207 struct _GtkWxCellRendererClass
1209 GtkCellRendererClass cell_parent_class
;
1213 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
1214 static void gtk_wx_cell_renderer_init (
1215 GtkWxCellRenderer
*cell
);
1216 static void gtk_wx_cell_renderer_class_init(
1217 GtkWxCellRendererClass
*klass
);
1218 static void gtk_wx_cell_renderer_get_size (
1219 GtkCellRenderer
*cell
,
1221 GdkRectangle
*rectangle
,
1226 static void gtk_wx_cell_renderer_render (
1227 GtkCellRenderer
*cell
,
1230 GdkRectangle
*background_area
,
1231 GdkRectangle
*cell_area
,
1232 GdkRectangle
*expose_area
,
1233 GtkCellRendererState flags
);
1234 static gboolean
gtk_wx_cell_renderer_activate(
1235 GtkCellRenderer
*cell
,
1239 GdkRectangle
*background_area
,
1240 GdkRectangle
*cell_area
,
1241 GtkCellRendererState flags
);
1242 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1243 GtkCellRenderer
*cell
,
1247 GdkRectangle
*background_area
,
1248 GdkRectangle
*cell_area
,
1249 GtkCellRendererState flags
);
1252 static GObjectClass
*cell_parent_class
= NULL
;
1257 gtk_wx_cell_renderer_get_type (void)
1259 static GType cell_wx_type
= 0;
1263 const GTypeInfo cell_wx_info
=
1265 sizeof (GtkWxCellRendererClass
),
1266 NULL
, /* base_init */
1267 NULL
, /* base_finalize */
1268 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
1269 NULL
, /* class_finalize */
1270 NULL
, /* class_data */
1271 sizeof (GtkWxCellRenderer
),
1272 0, /* n_preallocs */
1273 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
1276 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
1277 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
1280 return cell_wx_type
;
1284 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
1290 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
1292 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1294 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1296 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
1297 cell_class
->render
= gtk_wx_cell_renderer_render
;
1298 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
1299 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
1303 gtk_wx_cell_renderer_new (void)
1305 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
1308 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1309 GtkCellRenderer
*renderer
,
1310 GdkEvent
*WXUNUSED(event
),
1313 GdkRectangle
*WXUNUSED(background_area
),
1314 GdkRectangle
*cell_area
,
1315 GtkCellRendererState
WXUNUSED(flags
) )
1317 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1318 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1320 // Renderer doesn't support in-place editing
1321 if (!cell
->HasEditorCtrl())
1324 // An in-place editing control is still around
1325 if (cell
->GetEditorCtrl())
1329 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1335 rect
.x
+= cell_area
->x
;
1336 rect
.y
+= cell_area
->y
;
1337 // rect.width -= renderer->xpad * 2;
1338 // rect.height -= renderer->ypad * 2;
1340 // wxRect renderrect(wxRectFromGDKRect(&rect));
1341 wxRect
renderrect(wxRectFromGDKRect(cell_area
));
1344 item(cell
->GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1346 cell
->StartEditing( item
, renderrect
);
1352 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
1353 GtkWidget
*WXUNUSED(widget
),
1354 GdkRectangle
*cell_area
,
1360 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1361 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1363 wxSize size
= cell
->GetSize();
1365 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1367 // Uniform row height, if specified, overrides the value returned by the
1369 if ( !ctrl
->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
1371 const int uniformHeight
= ctrl
->GTKGetUniformRowHeight();
1372 if ( uniformHeight
> 0 )
1373 size
.y
= uniformHeight
;
1377 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1378 int calc_width
= xpad
* 2 + size
.x
;
1379 int calc_height
= ypad
* 2 + size
.y
;
1386 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
1388 float xalign
, yalign
;
1389 gtk_cell_renderer_get_alignment(renderer
, &xalign
, &yalign
);
1392 *x_offset
= int(xalign
* (cell_area
->width
- calc_width
- 2 * xpad
));
1393 *x_offset
= MAX(*x_offset
, 0) + xpad
;
1397 *y_offset
= int(yalign
* (cell_area
->height
- calc_height
- 2 * ypad
));
1398 *y_offset
= MAX(*y_offset
, 0) + ypad
;
1403 *width
= calc_width
;
1406 *height
= calc_height
;
1410 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
1413 GdkRectangle
*background_area
,
1414 GdkRectangle
*cell_area
,
1415 GdkRectangle
*expose_area
,
1416 GtkCellRendererState flags
)
1419 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1420 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1422 cell
->GTKStashRenderParams(window
, widget
,
1423 background_area
, expose_area
, flags
);
1425 wxRect
rect(wxRectFromGDKRect(cell_area
));
1427 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1428 rect
= rect
.Deflate(xpad
, ypad
);
1430 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
1431 wxWindowDCImpl
*impl
= (wxWindowDCImpl
*) dc
->GetImpl();
1433 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1434 // window such as a DnD drop window.
1435 if (window
!= impl
->m_gdkwindow
)
1438 impl
->m_gdkwindow
= window
;
1443 if (flags
& GTK_CELL_RENDERER_SELECTED
)
1444 state
|= wxDATAVIEW_CELL_SELECTED
;
1445 if (flags
& GTK_CELL_RENDERER_PRELIT
)
1446 state
|= wxDATAVIEW_CELL_PRELIT
;
1447 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1448 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1449 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1450 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1451 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
1452 state
|= wxDATAVIEW_CELL_FOCUSED
;
1453 cell
->WXCallRender( rect
, dc
, state
);
1457 gtk_wx_cell_renderer_activate(
1458 GtkCellRenderer
*renderer
,
1462 GdkRectangle
*WXUNUSED(background_area
),
1463 GdkRectangle
*cell_area
,
1464 GtkCellRendererState
WXUNUSED(flags
) )
1466 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1467 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1470 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1476 rect
.x
+= cell_area
->x
;
1477 rect
.y
+= cell_area
->y
;
1479 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1480 rect
.width
-= xpad
* 2;
1481 rect
.height
-= ypad
* 2;
1483 wxRect
renderrect(wxRectFromGDKRect(&rect
));
1485 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1486 wxDataViewModel
*model
= ctrl
->GetModel();
1488 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
1490 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1494 // activated by <ENTER>
1495 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, NULL
);
1497 else if ( event
->type
== GDK_BUTTON_PRESS
)
1499 GdkEventButton
*button_event
= (GdkEventButton
*)event
;
1500 if ( button_event
->button
== 1 )
1502 wxMouseEvent
mouse_event(wxEVT_LEFT_DOWN
);
1503 InitMouseEvent(ctrl
, mouse_event
, button_event
);
1505 mouse_event
.m_x
-= renderrect
.x
;
1506 mouse_event
.m_y
-= renderrect
.y
;
1508 return cell
->ActivateCell(renderrect
, model
, item
, model_col
, &mouse_event
);
1512 wxLogDebug("unexpected event type in gtk_wx_cell_renderer_activate()");
1516 // ---------------------------------------------------------
1517 // wxGtkDataViewModelNotifier
1518 // ---------------------------------------------------------
1520 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1523 wxGtkDataViewModelNotifier( wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
);
1524 ~wxGtkDataViewModelNotifier();
1526 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1527 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1528 virtual bool ItemChanged( const wxDataViewItem
&item
);
1529 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
1530 virtual bool Cleared();
1531 virtual void Resort();
1532 virtual bool BeforeReset();
1533 virtual bool AfterReset();
1535 void UpdateLastCount();
1538 wxDataViewModel
*m_wx_model
;
1539 wxDataViewCtrlInternal
*m_internal
;
1542 // ---------------------------------------------------------
1543 // wxGtkDataViewListModelNotifier
1544 // ---------------------------------------------------------
1546 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1547 wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
)
1549 m_wx_model
= wx_model
;
1550 m_internal
= internal
;
1553 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1559 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1561 m_internal
->ItemAdded( parent
, item
);
1562 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1565 iter
.stamp
= wxgtk_model
->stamp
;
1566 iter
.user_data
= item
.GetID();
1568 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1569 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1570 gtk_tree_model_row_inserted(
1571 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1576 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1578 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1580 // using _get_path for a deleted item cannot be
1583 iter
.stamp
= wxgtk_model
->stamp
;
1584 iter
.user_data
= (gpointer
) item
.GetID();
1585 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1586 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1588 // so get the path from the parent
1589 GtkTreeIter parentIter
;
1590 parentIter
.stamp
= wxgtk_model
->stamp
;
1591 parentIter
.user_data
= (gpointer
) parent
.GetID();
1592 wxGtkTreePath
parentPath(wxgtk_tree_model_get_path(
1593 GTK_TREE_MODEL(wxgtk_model
), &parentIter
));
1595 // and add the final index ourselves
1596 wxGtkTreePath
path(gtk_tree_path_copy(parentPath
));
1597 int index
= m_internal
->GetIndexOf( parent
, item
);
1598 gtk_tree_path_append_index( path
, index
);
1601 gtk_tree_model_row_deleted(
1602 GTK_TREE_MODEL(wxgtk_model
), path
);
1604 m_internal
->ItemDeleted( parent
, item
);
1606 // Did we remove the last child, causing 'parent' to become a leaf?
1607 if ( !m_wx_model
->IsContainer(parent
) )
1609 gtk_tree_model_row_has_child_toggled
1611 GTK_TREE_MODEL(wxgtk_model
),
1620 void wxGtkDataViewModelNotifier::Resort()
1622 m_internal
->Resort();
1625 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1627 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1630 iter
.stamp
= wxgtk_model
->stamp
;
1631 iter
.user_data
= (gpointer
) item
.GetID();
1633 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1634 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1635 gtk_tree_model_row_changed(
1636 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1638 m_internal
->ItemChanged( item
);
1643 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
)
1645 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1646 wxDataViewCtrl
*ctrl
= m_internal
->GetOwner();
1648 // This adds GTK+'s missing MVC logic for ValueChanged
1650 for (index
= 0; index
< ctrl
->GetColumnCount(); index
++)
1652 wxDataViewColumn
*column
= ctrl
->GetColumn( index
);
1653 if (column
->GetModelColumn() == model_column
)
1655 GtkTreeView
*widget
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
1656 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1658 // Don't attempt to refresh not yet realized tree, it is useless
1659 // and results in GTK errors.
1660 if ( gtk_widget_get_realized(ctrl
->GtkGetTreeView()) )
1664 iter
.stamp
= wxgtk_model
->stamp
;
1665 iter
.user_data
= (gpointer
) item
.GetID();
1666 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1667 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1668 GdkRectangle cell_area
;
1669 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1671 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1672 double d
= gtk_adjustment_get_value( hadjust
);
1673 int xdiff
= (int) d
;
1675 int ydiff
= gcolumn
->button
->allocation
.height
;
1677 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1678 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1681 m_internal
->ValueChanged( item
, model_column
);
1690 bool wxGtkDataViewModelNotifier::BeforeReset()
1692 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1693 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), NULL
);
1698 bool wxGtkDataViewModelNotifier::AfterReset()
1700 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1701 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1703 m_internal
->Cleared();
1705 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), GTK_TREE_MODEL(wxgtk_model
) );
1710 bool wxGtkDataViewModelNotifier::Cleared()
1712 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1714 // There is no call to tell the model that everything
1715 // has been deleted so call row_deleted() for every
1718 int count
= m_internal
->iter_n_children( NULL
); // number of children of root
1720 GtkTreePath
*path
= gtk_tree_path_new_first(); // points to root
1723 for (i
= 0; i
< count
; i
++)
1724 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model
), path
);
1726 gtk_tree_path_free( path
);
1728 m_internal
->Cleared();
1733 // ---------------------------------------------------------
1734 // wxDataViewRenderer
1735 // ---------------------------------------------------------
1737 static gpointer s_user_data
= NULL
;
1740 wxgtk_cell_editable_editing_done( GtkCellEditable
*WXUNUSED(editable
),
1741 wxDataViewRenderer
*wxrenderer
)
1743 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1744 wxDataViewCtrl
*dv
= column
->GetOwner();
1745 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1746 event
.SetDataViewColumn( column
);
1747 event
.SetModel( dv
->GetModel() );
1748 wxDataViewItem
item( s_user_data
);
1749 event
.SetItem( item
);
1750 dv
->HandleWindowEvent( event
);
1754 wxgtk_renderer_editing_started( GtkCellRenderer
*WXUNUSED(cell
), GtkCellEditable
*editable
,
1755 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1760 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1761 wxDataViewCtrl
*dv
= column
->GetOwner();
1762 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1763 event
.SetDataViewColumn( column
);
1764 event
.SetModel( dv
->GetModel() );
1765 wxDataViewItem
item(dv
->GTKPathToItem(wxGtkTreePath(path
)));
1766 event
.SetItem( item
);
1767 dv
->HandleWindowEvent( event
);
1769 if (GTK_IS_CELL_EDITABLE(editable
))
1771 s_user_data
= item
.GetID();
1773 g_signal_connect (GTK_CELL_EDITABLE (editable
), "editing_done",
1774 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1775 (gpointer
) wxrenderer
);
1781 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1783 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1785 wxDataViewRendererBase( varianttype
, mode
, align
)
1790 // we haven't changed them yet
1791 m_usingDefaultAttrs
= true;
1793 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1794 // after the m_renderer pointer has been initialized
1797 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
1799 gtk_tree_view_column_pack_end( column
, m_renderer
, TRUE
/* expand */);
1802 void wxDataViewRenderer::GtkInitHandlers()
1804 if (!gtk_check_version(2,6,0))
1806 g_signal_connect (GTK_CELL_RENDERER(m_renderer
), "editing_started",
1807 G_CALLBACK (wxgtk_renderer_editing_started
),
1812 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1814 GtkCellRendererMode gtkMode
;
1817 case wxDATAVIEW_CELL_INERT
:
1818 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1821 case wxDATAVIEW_CELL_ACTIVATABLE
:
1822 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1825 case wxDATAVIEW_CELL_EDITABLE
:
1826 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1830 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1836 // This value is most often ignored in GtkTreeView
1837 GValue gvalue
= { 0, };
1838 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1839 g_value_set_enum( &gvalue
, gtkMode
);
1840 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1841 g_value_unset( &gvalue
);
1844 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1846 wxDataViewCellMode ret
;
1849 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1851 switch (g_value_get_enum(&gvalue
))
1854 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1855 // fall through (we have to return something)
1857 case GTK_CELL_RENDERER_MODE_INERT
:
1858 ret
= wxDATAVIEW_CELL_INERT
;
1861 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1862 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1865 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1866 ret
= wxDATAVIEW_CELL_EDITABLE
;
1870 g_value_unset( &gvalue
);
1875 void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer
*renderer
)
1877 int align
= m_alignment
;
1879 // query alignment from column ?
1883 if (GetOwner() == NULL
)
1886 align
= GetOwner()->GetAlignment();
1887 align
|= wxALIGN_CENTRE_VERTICAL
;
1890 // horizontal alignment:
1892 gfloat xalign
= 0.0;
1893 if (align
& wxALIGN_RIGHT
)
1895 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1898 GValue gvalue
= { 0, };
1899 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1900 g_value_set_float( &gvalue
, xalign
);
1901 g_object_set_property( G_OBJECT(renderer
), "xalign", &gvalue
);
1902 g_value_unset( &gvalue
);
1904 // vertical alignment:
1906 gfloat yalign
= 0.0;
1907 if (align
& wxALIGN_BOTTOM
)
1909 else if (align
& wxALIGN_CENTER_VERTICAL
)
1912 GValue gvalue2
= { 0, };
1913 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1914 g_value_set_float( &gvalue2
, yalign
);
1915 g_object_set_property( G_OBJECT(renderer
), "yalign", &gvalue2
);
1916 g_value_unset( &gvalue2
);
1919 void wxDataViewRenderer::SetAlignment( int align
)
1921 m_alignment
= align
;
1922 GtkUpdateAlignment();
1925 int wxDataViewRenderer::GetAlignment() const
1930 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode
)
1933 if ( gtk_check_version(2, 6, 0) != NULL
)
1936 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1940 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1941 // can just cast between them
1942 GValue gvalue
= { 0, };
1943 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1944 g_value_set_enum( &gvalue
, static_cast<PangoEllipsizeMode
>(mode
) );
1945 g_object_set_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1946 g_value_unset( &gvalue
);
1949 #endif // GTK 2.6/before
1952 wxEllipsizeMode
wxDataViewRenderer::GetEllipsizeMode() const
1955 if ( gtk_check_version(2, 6, 0) != NULL
)
1956 return wxELLIPSIZE_NONE
;
1958 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1960 return wxELLIPSIZE_NONE
;
1962 GValue gvalue
= { 0, };
1963 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1964 g_object_get_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1966 mode
= static_cast<wxEllipsizeMode
>(g_value_get_enum( &gvalue
));
1967 g_value_unset( &gvalue
);
1971 return wxELLIPSIZE_NONE
;
1972 #endif // GTK 2.6/before
1976 wxDataViewRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
1978 wxVariant
value(str
);
1979 if (!Validate( value
))
1983 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
1985 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
1989 wxDataViewRenderer::GtkOnCellChanged(const wxVariant
& value
,
1990 const wxDataViewItem
& item
,
1993 wxDataViewModel
*model
= GetOwner()->GetOwner()->GetModel();
1994 model
->ChangeValue( value
, item
, col
);
1997 // ---------------------------------------------------------
1998 // wxDataViewTextRenderer
1999 // ---------------------------------------------------------
2004 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*WXUNUSED(renderer
),
2005 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
2007 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) user_data
;
2009 cell
->GtkOnTextEdited(arg1
, wxGTK_CONV_BACK_FONT(
2010 arg2
, cell
->GetOwner()->GetOwner()->GetFont()));
2018 // helper function used by wxDataViewTextRenderer and
2019 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
2020 // given text renderer and returns true if anything was done
2021 bool GtkApplyAttr(GtkCellRendererText
*renderer
, const wxDataViewItemAttr
& attr
)
2023 bool usingDefaultAttrs
= true;
2024 if (attr
.HasColour())
2026 const GdkColor
* const gcol
= attr
.GetColour().GetColor();
2028 GValue gvalue
= { 0, };
2029 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2030 g_value_set_boxed( &gvalue
, gcol
);
2031 g_object_set_property( G_OBJECT(renderer
), "foreground_gdk", &gvalue
);
2032 g_value_unset( &gvalue
);
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
), "foreground-set", &gvalue
);
2042 g_value_unset( &gvalue
);
2045 if (attr
.GetItalic())
2047 GValue gvalue
= { 0, };
2048 g_value_init( &gvalue
, PANGO_TYPE_STYLE
);
2049 g_value_set_enum( &gvalue
, PANGO_STYLE_ITALIC
);
2050 g_object_set_property( G_OBJECT(renderer
), "style", &gvalue
);
2051 g_value_unset( &gvalue
);
2053 usingDefaultAttrs
= false;
2057 GValue gvalue
= { 0, };
2058 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2059 g_value_set_boolean( &gvalue
, FALSE
);
2060 g_object_set_property( G_OBJECT(renderer
), "style-set", &gvalue
);
2061 g_value_unset( &gvalue
);
2067 GValue gvalue
= { 0, };
2068 g_value_init( &gvalue
, PANGO_TYPE_WEIGHT
);
2069 g_value_set_enum( &gvalue
, PANGO_WEIGHT_BOLD
);
2070 g_object_set_property( G_OBJECT(renderer
), "weight", &gvalue
);
2071 g_value_unset( &gvalue
);
2073 usingDefaultAttrs
= false;
2077 GValue gvalue
= { 0, };
2078 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2079 g_value_set_boolean( &gvalue
, FALSE
);
2080 g_object_set_property( G_OBJECT(renderer
), "weight-set", &gvalue
);
2081 g_value_unset( &gvalue
);
2085 if (attr
.HasBackgroundColour())
2087 wxColour colour
= attr
.GetBackgroundColour();
2088 const GdkColor
* const gcol
= colour
.GetColor();
2090 GValue gvalue
= { 0, };
2091 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2092 g_value_set_boxed( &gvalue
, gcol
);
2093 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2094 g_value_unset( &gvalue
);
2098 GValue gvalue
= { 0, };
2099 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2100 g_value_set_boolean( &gvalue
, FALSE
);
2101 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2102 g_value_unset( &gvalue
);
2106 return !usingDefaultAttrs
;
2109 } // anonymous namespace
2111 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
2113 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2115 wxDataViewRenderer( varianttype
, mode
, align
)
2117 GtkWxCellRendererText
*text_renderer
= gtk_wx_cell_renderer_text_new();
2118 text_renderer
->wx_renderer
= this;
2119 m_renderer
= (GtkCellRenderer
*) text_renderer
;
2121 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
2123 GValue gvalue
= { 0, };
2124 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2125 g_value_set_boolean( &gvalue
, true );
2126 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
2127 g_value_unset( &gvalue
);
2129 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2135 SetAlignment(align
);
2138 bool wxDataViewTextRenderer::SetTextValue(const wxString
& str
)
2140 GValue gvalue
= { 0, };
2141 g_value_init( &gvalue
, G_TYPE_STRING
);
2142 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( str
, GetOwner()->GetOwner()->GetFont() ) );
2143 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2144 g_value_unset( &gvalue
);
2149 bool wxDataViewTextRenderer::GetTextValue(wxString
& str
) const
2151 GValue gvalue
= { 0, };
2152 g_value_init( &gvalue
, G_TYPE_STRING
);
2153 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2154 str
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
), const_cast<wxDataViewTextRenderer
*>(this)->GetOwner()->GetOwner()->GetFont() );
2155 g_value_unset( &gvalue
);
2160 void wxDataViewTextRenderer::SetAlignment( int align
)
2162 wxDataViewRenderer::SetAlignment(align
);
2164 if (gtk_check_version(2,10,0))
2167 // horizontal alignment:
2168 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2169 if (align
& wxALIGN_RIGHT
)
2170 pangoAlign
= PANGO_ALIGN_RIGHT
;
2171 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2172 pangoAlign
= PANGO_ALIGN_CENTER
;
2174 GValue gvalue
= { 0, };
2175 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2176 g_value_set_enum( &gvalue
, pangoAlign
);
2177 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2178 g_value_unset( &gvalue
);
2181 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr
& attr
)
2183 return GtkApplyAttr(GtkGetTextRenderer(), attr
);
2186 GtkCellRendererText
*wxDataViewTextRenderer::GtkGetTextRenderer() const
2188 return GTK_CELL_RENDERER_TEXT(m_renderer
);
2191 // ---------------------------------------------------------
2192 // wxDataViewBitmapRenderer
2193 // ---------------------------------------------------------
2198 // set "pixbuf" property on the given renderer
2199 void SetPixbufProp(GtkCellRenderer
*renderer
, GdkPixbuf
*pixbuf
)
2201 GValue gvalue
= { 0, };
2202 g_value_init( &gvalue
, G_TYPE_OBJECT
);
2203 g_value_set_object( &gvalue
, pixbuf
);
2204 g_object_set_property( G_OBJECT(renderer
), "pixbuf", &gvalue
);
2205 g_value_unset( &gvalue
);
2208 } // anonymous namespace
2210 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
2212 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2214 wxDataViewRenderer( varianttype
, mode
, align
)
2216 m_renderer
= gtk_cell_renderer_pixbuf_new();
2219 SetAlignment(align
);
2222 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
2224 if (value
.GetType() == wxT("wxBitmap"))
2229 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2230 // object (and it will stay there and remain owned by wxBitmap)
2231 SetPixbufProp(m_renderer
, bitmap
.GetPixbuf());
2233 else if (value
.GetType() == wxT("wxIcon"))
2238 SetPixbufProp(m_renderer
, icon
.GetPixbuf());
2248 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2253 // ---------------------------------------------------------
2254 // wxDataViewToggleRenderer
2255 // ---------------------------------------------------------
2258 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2259 gchar
*path
, gpointer user_data
);
2262 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2263 gchar
*path
, gpointer user_data
)
2265 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
2268 GValue gvalue
= { 0, };
2269 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2270 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
2271 bool tmp
= g_value_get_boolean( &gvalue
);
2272 g_value_unset( &gvalue
);
2276 wxVariant value
= tmp
;
2277 if (!cell
->Validate( value
))
2280 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
2281 wxDataViewModel
*model
= ctrl
->GetModel();
2283 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
2285 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
2287 model
->ChangeValue( value
, item
, model_col
);
2290 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
2292 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
2293 wxDataViewCellMode mode
, int align
) :
2294 wxDataViewRenderer( varianttype
, mode
, align
)
2296 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
2298 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
2300 g_signal_connect_after( m_renderer
, "toggled",
2301 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
2305 GValue gvalue
= { 0, };
2306 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2307 g_value_set_boolean( &gvalue
, false );
2308 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
2309 g_value_unset( &gvalue
);
2313 SetAlignment(align
);
2316 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
2320 GValue gvalue
= { 0, };
2321 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2322 g_value_set_boolean( &gvalue
, tmp
);
2323 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2324 g_value_unset( &gvalue
);
2329 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
2331 GValue gvalue
= { 0, };
2332 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2333 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2334 bool tmp
= g_value_get_boolean( &gvalue
);
2335 g_value_unset( &gvalue
);
2342 // ---------------------------------------------------------
2343 // wxDataViewCustomRenderer
2344 // ---------------------------------------------------------
2346 class wxDataViewCtrlDCImpl
: public wxWindowDCImpl
2349 wxDataViewCtrlDCImpl( wxDC
*owner
, wxDataViewCtrl
*window
) :
2350 wxWindowDCImpl( owner
)
2352 GtkWidget
*widget
= window
->m_treeview
;
2358 m_context
= window
->GTKGetPangoDefaultContext();
2359 m_layout
= pango_layout_new( m_context
);
2360 m_fontdesc
= pango_font_description_copy(gtk_widget_get_style(widget
)->font_desc
);
2362 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
2364 // Set m_gdkwindow later
2369 class wxDataViewCtrlDC
: public wxWindowDC
2372 wxDataViewCtrlDC( wxDataViewCtrl
*window
) :
2373 wxWindowDC( new wxDataViewCtrlDCImpl( this, window
) )
2378 // ---------------------------------------------------------
2379 // wxDataViewCustomRenderer
2380 // ---------------------------------------------------------
2382 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
2384 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
2385 wxDataViewCellMode mode
,
2388 : wxDataViewCustomRendererBase( varianttype
, mode
, align
)
2391 m_text_renderer
= NULL
;
2399 void wxDataViewCustomRenderer::GtkInitTextRenderer()
2401 m_text_renderer
= GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2402 g_object_ref_sink(m_text_renderer
);
2404 GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer
));
2407 GtkCellRendererText
*wxDataViewCustomRenderer::GtkGetTextRenderer() const
2409 if ( !m_text_renderer
)
2411 // we create it on demand so need to do it even from a const function
2412 const_cast<wxDataViewCustomRenderer
*>(this)->GtkInitTextRenderer();
2415 return m_text_renderer
;
2418 void wxDataViewCustomRenderer::RenderText( const wxString
&text
,
2422 int WXUNUSED(state
) )
2425 GtkCellRendererText
* const textRenderer
= GtkGetTextRenderer();
2427 GValue gvalue
= { 0, };
2428 g_value_init( &gvalue
, G_TYPE_STRING
);
2429 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( text
, GetOwner()->GetOwner()->GetFont() ) );
2430 g_object_set_property( G_OBJECT(textRenderer
), "text", &gvalue
);
2431 g_value_unset( &gvalue
);
2433 GtkApplyAttr(textRenderer
, GetAttr());
2435 GdkRectangle cell_area
;
2436 wxRectToGDKRect(cell
, cell_area
);
2437 cell_area
.x
+= xoffset
;
2438 cell_area
.width
-= xoffset
;
2440 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer
),
2441 m_renderParams
.window
,
2442 m_renderParams
.widget
,
2443 m_renderParams
.background_area
,
2445 m_renderParams
.expose_area
,
2446 (GtkCellRendererState
) m_renderParams
.flags
);
2449 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
2451 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
2452 renderer
->cell
= this;
2454 m_renderer
= (GtkCellRenderer
*) renderer
;
2457 SetAlignment(align
);
2464 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2469 if (m_text_renderer
)
2470 g_object_unref(m_text_renderer
);
2473 wxDC
*wxDataViewCustomRenderer::GetDC()
2477 if (GetOwner() == NULL
)
2479 if (GetOwner()->GetOwner() == NULL
)
2481 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2487 // ---------------------------------------------------------
2488 // wxDataViewProgressRenderer
2489 // ---------------------------------------------------------
2491 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
2493 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
2494 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
2495 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
2501 if (!gtk_check_version(2,6,0))
2503 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
2506 SetAlignment(align
);
2509 // We can't initialize the renderer just yet because we don't have the
2510 // pointer to the column that uses this renderer yet and so attempt to
2511 // dereference GetOwner() to get the font that is used as a source of
2512 // encoding in multibyte-to-Unicode conversion in GTKSetLabel() in
2513 // non-Unicode builds would crash. So simply remember to do it later.
2514 if ( !m_label
.empty() )
2515 m_needsToSetLabel
= true;
2517 #endif // !wxUSE_UNICODE
2523 // Use custom cell code
2524 wxDataViewCustomRenderer::Init(mode
, align
);
2528 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2532 void wxDataViewProgressRenderer::GTKSetLabel()
2534 GValue gvalue
= { 0, };
2535 g_value_init( &gvalue
, G_TYPE_STRING
);
2537 // Take care to not use GetOwner() here if the label is empty, we can be
2538 // called from ctor when GetOwner() is still NULL in this case.
2539 wxScopedCharBuffer buf
;
2540 if ( m_label
.empty() )
2541 buf
= wxScopedCharBuffer::CreateNonOwned("");
2543 buf
= wxGTK_CONV_FONT(m_label
, GetOwner()->GetOwner()->GetFont());
2545 g_value_set_string( &gvalue
, buf
);
2546 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2547 g_value_unset( &gvalue
);
2550 m_needsToSetLabel
= false;
2551 #endif // !wxUSE_UNICODE
2554 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
2557 if (!gtk_check_version(2,6,0))
2560 if ( m_needsToSetLabel
)
2562 #endif // !wxUSE_UNICODE
2564 gint tmp
= (long) value
;
2565 GValue gvalue
= { 0, };
2566 g_value_init( &gvalue
, G_TYPE_INT
);
2567 g_value_set_int( &gvalue
, tmp
);
2568 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
2569 g_value_unset( &gvalue
);
2574 m_value
= (long) value
;
2576 if (m_value
< 0) m_value
= 0;
2577 if (m_value
> 100) m_value
= 100;
2583 bool wxDataViewProgressRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2588 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
2590 double pct
= (double)m_value
/ 100.0;
2592 bar
.width
= (int)(cell
.width
* pct
);
2593 dc
->SetPen( *wxTRANSPARENT_PEN
);
2594 dc
->SetBrush( *wxBLUE_BRUSH
);
2595 dc
->DrawRectangle( bar
);
2597 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
2598 dc
->SetPen( *wxBLACK_PEN
);
2599 dc
->DrawRectangle( cell
);
2604 wxSize
wxDataViewProgressRenderer::GetSize() const
2606 return wxSize(40,12);
2609 // -------------------------------------
2610 // wxDataViewChoiceRenderer
2611 // -------------------------------------
2613 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString
&choices
,
2614 wxDataViewCellMode mode
, int alignment
) :
2615 wxDataViewCustomRenderer( "string", mode
, alignment
, true )
2617 m_choices
= choices
;
2620 if (!gtk_check_version(2,6,0))
2622 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_combo_new();
2624 GtkListStore
*store
= gtk_list_store_new( 1, G_TYPE_STRING
);
2625 for (size_t n
= 0; n
< m_choices
.GetCount(); n
++)
2627 gtk_list_store_insert_with_values(
2629 static_cast<const char *>(m_choices
[n
].utf8_str()), -1 );
2632 g_object_set (m_renderer
,
2638 bool editable
= (mode
& wxDATAVIEW_CELL_EDITABLE
);
2639 g_object_set (m_renderer
, "editable", editable
, NULL
);
2641 SetAlignment(alignment
);
2643 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2650 // Use custom cell code
2651 wxDataViewCustomRenderer::Init(mode
, alignment
);
2655 bool wxDataViewChoiceRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
2657 RenderText( m_data
, 0, rect
, dc
, state
);
2661 wxSize
wxDataViewChoiceRenderer::GetSize() const
2663 return wxSize(70,20);
2666 bool wxDataViewChoiceRenderer::SetValue( const wxVariant
&value
)
2670 if (!gtk_check_version(2,6,0))
2672 GValue gvalue
= { 0, };
2673 g_value_init( &gvalue
, G_TYPE_STRING
);
2674 g_value_set_string(&gvalue
,
2675 wxGTK_CONV_FONT(value
.GetString(),
2676 GetOwner()->GetOwner()->GetFont()));
2677 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2678 g_value_unset( &gvalue
);
2682 m_data
= value
.GetString();
2687 bool wxDataViewChoiceRenderer::GetValue( wxVariant
&value
) const
2690 if (!gtk_check_version(2,6,0))
2692 GValue gvalue
= { 0, };
2693 g_value_init( &gvalue
, G_TYPE_STRING
);
2694 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2695 wxString temp
= wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue
),
2696 GetOwner()->GetOwner()->GetFont());
2697 g_value_unset( &gvalue
);
2700 //wxPrintf( "temp %s\n", temp );
2701 // TODO: remove this code
2710 void wxDataViewChoiceRenderer::SetAlignment( int align
)
2712 wxDataViewCustomRenderer::SetAlignment(align
);
2714 if (gtk_check_version(2,10,0))
2717 // horizontal alignment:
2718 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2719 if (align
& wxALIGN_RIGHT
)
2720 pangoAlign
= PANGO_ALIGN_RIGHT
;
2721 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2722 pangoAlign
= PANGO_ALIGN_CENTER
;
2724 GValue gvalue
= { 0, };
2725 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2726 g_value_set_enum( &gvalue
, pangoAlign
);
2727 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2728 g_value_unset( &gvalue
);
2731 // ----------------------------------------------------------------------------
2732 // wxDataViewChoiceByIndexRenderer
2733 // ----------------------------------------------------------------------------
2735 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
2736 wxDataViewCellMode mode
, int alignment
) :
2737 wxDataViewChoiceRenderer( choices
, mode
, alignment
)
2741 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
2743 wxVariant
value( (long) GetChoices().Index( str
) );
2745 if (!Validate( value
))
2749 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2751 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2754 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant
&value
)
2756 wxVariant string_value
= GetChoice( value
.GetLong() );
2757 return wxDataViewChoiceRenderer::SetValue( string_value
);
2760 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant
&value
) const
2762 wxVariant string_value
;
2763 if (!wxDataViewChoiceRenderer::GetValue( string_value
))
2766 value
= (long) GetChoices().Index( string_value
.GetString() );
2770 // ---------------------------------------------------------
2771 // wxDataViewIconTextRenderer
2772 // ---------------------------------------------------------
2774 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
2776 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2778 const wxString
&varianttype
,
2779 wxDataViewCellMode mode
,
2782 : wxDataViewTextRenderer(varianttype
, mode
, align
)
2784 m_rendererIcon
= gtk_cell_renderer_pixbuf_new();
2787 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2791 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
2793 // add the icon renderer first
2794 gtk_tree_view_column_pack_start(column
, m_rendererIcon
, FALSE
/* !expand */);
2796 // add the text renderer too
2797 wxDataViewRenderer::GtkPackIntoColumn(column
);
2800 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
2804 SetTextValue(m_value
.GetText());
2805 SetPixbufProp(m_rendererIcon
, m_value
.GetIcon().GetPixbuf());
2810 bool wxDataViewIconTextRenderer::GetValue(wxVariant
& value
) const
2813 if ( !GetTextValue(str
) )
2816 // user doesn't have any way to edit the icon so leave it unchanged
2817 value
<< wxDataViewIconText(str
, m_value
.GetIcon());
2823 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant
& value
,
2824 const wxDataViewItem
& item
,
2827 // we receive just the text part of our value as it's the only one which
2828 // can be edited, but we need the full wxDataViewIconText value for the
2830 wxVariant valueIconText
;
2831 valueIconText
<< wxDataViewIconText(value
.GetString(), m_value
.GetIcon());
2832 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText
, item
, col
);
2835 // ---------------------------------------------------------
2837 // ---------------------------------------------------------
2841 gtk_dataview_header_button_press_callback( GtkWidget
*WXUNUSED(widget
),
2842 GdkEventButton
*gdk_event
,
2843 wxDataViewColumn
*column
)
2845 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
2848 if (gdk_event
->button
== 1)
2850 gs_lastLeftClickHeader
= column
;
2852 wxDataViewCtrl
*dv
= column
->GetOwner();
2853 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2854 event
.SetDataViewColumn( column
);
2855 event
.SetModel( dv
->GetModel() );
2856 if (dv
->HandleWindowEvent( event
))
2860 if (gdk_event
->button
== 3)
2862 wxDataViewCtrl
*dv
= column
->GetOwner();
2863 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2864 event
.SetDataViewColumn( column
);
2865 event
.SetModel( dv
->GetModel() );
2866 if (dv
->HandleWindowEvent( event
))
2876 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*WXUNUSED(column
),
2877 GtkCellRenderer
*renderer
,
2878 GtkTreeModel
*model
,
2882 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2883 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2885 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2887 wxDataViewItem
item( (void*) iter
->user_data
);
2889 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2891 if (!wx_model
->IsVirtualListModel())
2894 if (wx_model
->IsContainer( item
))
2896 visible
= wx_model
->HasContainerColumns( item
) ||
2897 (cell
->GetOwner()->GetModelColumn() == 0);
2904 GValue gvalue
= { 0, };
2905 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2906 g_value_set_boolean( &gvalue
, visible
);
2907 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2908 g_value_unset( &gvalue
);
2915 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2917 if (value
.GetType() != cell
->GetVariantType())
2919 wxLogError( wxT("Wrong type, required: %s but: %s"),
2920 value
.GetType().c_str(),
2921 cell
->GetVariantType().c_str() );
2924 cell
->SetValue( value
);
2926 // deal with disabled items
2927 bool enabled
= wx_model
->IsEnabled( item
, cell
->GetOwner()->GetModelColumn() );
2929 // a) this sets the appearance to disabled grey
2930 GValue gvalue
= { 0, };
2931 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2932 g_value_set_boolean( &gvalue
, enabled
);
2933 g_object_set_property( G_OBJECT(renderer
), "sensitive", &gvalue
);
2934 g_value_unset( &gvalue
);
2936 // b) this actually disables the control/renderer
2938 cell
->SetMode( cell
->GtkGetMode() );
2940 cell
->SetMode( wxDATAVIEW_CELL_INERT
);
2943 // deal with attributes: if the renderer doesn't support them at all, we
2944 // don't even need to query the model for them
2945 if ( !cell
->GtkSupportsAttrs() )
2948 // it can support attributes so check if this item has any
2949 wxDataViewItemAttr attr
;
2950 if ( wx_model
->GetAttr( item
, cell
->GetOwner()->GetModelColumn(), attr
)
2951 || !cell
->GtkIsUsingDefaultAttrs() )
2953 bool usingDefaultAttrs
= !cell
->GtkSetAttr(attr
);
2954 cell
->GtkSetUsingDefaultAttrs(usingDefaultAttrs
);
2956 // else: no custom attributes specified and we're already using the default
2957 // ones -- nothing to do
2963 #include <wx/listimpl.cpp>
2964 WX_DEFINE_LIST(wxDataViewColumnList
)
2966 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
2967 unsigned int model_column
, int width
,
2968 wxAlignment align
, int flags
)
2969 : wxDataViewColumnBase( cell
, model_column
)
2971 Init( align
, flags
, width
);
2976 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
2977 unsigned int model_column
, int width
,
2978 wxAlignment align
, int flags
)
2979 : wxDataViewColumnBase( bitmap
, cell
, model_column
)
2981 Init( align
, flags
, width
);
2983 SetBitmap( bitmap
);
2986 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
2988 m_isConnected
= false;
2990 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
2991 m_column
= (GtkWidget
*) column
;
2994 SetAlignment( align
);
2998 // Create container for icon and label
2999 GtkWidget
*box
= gtk_hbox_new( FALSE
, 1 );
3000 gtk_widget_show( box
);
3001 // gtk_container_set_border_width((GtkContainer*)box, 2);
3002 m_image
= gtk_image_new();
3003 gtk_box_pack_start(GTK_BOX(box
), m_image
, FALSE
, FALSE
, 1);
3004 m_label
= gtk_label_new("");
3005 gtk_box_pack_end( GTK_BOX(box
), GTK_WIDGET(m_label
), FALSE
, FALSE
, 1 );
3006 gtk_tree_view_column_set_widget( column
, box
);
3008 wxDataViewRenderer
* const colRenderer
= GetRenderer();
3009 GtkCellRenderer
* const cellRenderer
= colRenderer
->GetGtkHandle();
3011 colRenderer
->GtkPackIntoColumn(column
);
3013 gtk_tree_view_column_set_cell_data_func( column
, cellRenderer
,
3014 wxGtkTreeCellDataFunc
, (gpointer
) colRenderer
, NULL
);
3017 void wxDataViewColumn::OnInternalIdle()
3022 if (gtk_widget_get_realized(GetOwner()->m_treeview
))
3024 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3027 g_signal_connect(column
->button
, "button_press_event",
3028 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
3030 // otherwise the event will be blocked by GTK+
3031 gtk_tree_view_column_set_clickable( column
, TRUE
);
3033 m_isConnected
= true;
3038 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
3040 wxDataViewColumnBase::SetOwner( owner
);
3042 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3044 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3047 void wxDataViewColumn::SetTitle( const wxString
&title
)
3049 wxDataViewCtrl
*ctrl
= GetOwner();
3050 gtk_label_set_text( GTK_LABEL(m_label
), ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
3051 : wxGTK_CONV_SYS(title
) );
3053 gtk_widget_hide( m_label
);
3055 gtk_widget_show( m_label
);
3058 wxString
wxDataViewColumn::GetTitle() const
3060 return wxGTK_CONV_BACK_FONT(
3061 gtk_label_get_text( GTK_LABEL(m_label
) ),
3062 GetOwner()->GetFont()
3066 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
3068 wxDataViewColumnBase::SetBitmap( bitmap
);
3072 GtkImage
*gtk_image
= GTK_IMAGE(m_image
);
3074 GdkBitmap
*mask
= NULL
;
3075 if (bitmap
.GetMask())
3076 mask
= bitmap
.GetMask()->GetBitmap();
3078 if (bitmap
.HasPixbuf())
3080 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
3081 bitmap
.GetPixbuf());
3085 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
3086 bitmap
.GetPixmap(), mask
);
3088 gtk_widget_show( m_image
);
3092 gtk_widget_hide( m_image
);
3096 void wxDataViewColumn::SetHidden( bool hidden
)
3098 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
3101 void wxDataViewColumn::SetResizeable( bool resizable
)
3103 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizable
);
3106 void wxDataViewColumn::SetAlignment( wxAlignment align
)
3108 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3110 gfloat xalign
= 0.0;
3111 if (align
== wxALIGN_RIGHT
)
3113 if (align
== wxALIGN_CENTER_HORIZONTAL
||
3114 align
== wxALIGN_CENTER
)
3117 gtk_tree_view_column_set_alignment( column
, xalign
);
3119 if (m_renderer
&& m_renderer
->GetAlignment() == -1)
3120 m_renderer
->GtkUpdateAlignment();
3123 wxAlignment
wxDataViewColumn::GetAlignment() const
3125 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
3128 return wxALIGN_RIGHT
;
3130 return wxALIGN_CENTER_HORIZONTAL
;
3132 return wxALIGN_LEFT
;
3135 void wxDataViewColumn::SetSortable( bool sortable
)
3137 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3141 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
3145 gtk_tree_view_column_set_sort_column_id( column
, -1 );
3146 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
3147 gtk_tree_view_column_set_clickable( column
, FALSE
);
3151 bool wxDataViewColumn::IsSortable() const
3153 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3154 return gtk_tree_view_column_get_clickable( column
);
3157 bool wxDataViewColumn::IsSortKey() const
3159 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3160 return gtk_tree_view_column_get_sort_indicator( column
);
3163 bool wxDataViewColumn::IsResizeable() const
3165 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3166 return gtk_tree_view_column_get_resizable( column
);
3169 bool wxDataViewColumn::IsHidden() const
3171 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3172 return !gtk_tree_view_column_get_visible( column
);
3175 void wxDataViewColumn::SetSortOrder( bool ascending
)
3177 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3180 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
3182 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
3184 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
3187 bool wxDataViewColumn::IsSortOrderAscending() const
3189 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3191 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
3194 void wxDataViewColumn::SetMinWidth( int width
)
3196 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3199 int wxDataViewColumn::GetMinWidth() const
3201 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3204 int wxDataViewColumn::GetWidth() const
3206 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3209 void wxDataViewColumn::SetWidth( int width
)
3211 if ( width
== wxCOL_WIDTH_AUTOSIZE
)
3213 // NB: this disables user resizing
3214 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
3218 if ( width
== wxCOL_WIDTH_DEFAULT
)
3220 // TODO find a better calculation
3221 width
= wxDVC_DEFAULT_WIDTH
;
3224 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_FIXED
);
3225 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3229 void wxDataViewColumn::SetReorderable( bool reorderable
)
3231 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column
), reorderable
);
3234 bool wxDataViewColumn::IsReorderable() const
3236 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column
) );
3239 //-----------------------------------------------------------------------------
3240 // wxGtkTreeModelNode
3241 //-----------------------------------------------------------------------------
3244 class wxGtkTreeModelChildWithPos
3252 int wxGtkTreeModelChildWithPosCmp( const void* data1
, const void* data2
, const void* user_data
)
3254 const wxGtkTreeModelChildWithPos
* child1
= (const wxGtkTreeModelChildWithPos
*) data1
;
3255 const wxGtkTreeModelChildWithPos
* child2
= (const wxGtkTreeModelChildWithPos
*) data2
;
3256 const wxDataViewCtrlInternal
*internal
= (const wxDataViewCtrlInternal
*) user_data
;
3257 int ret
= internal
->GetDataViewModel()->Compare( child1
->id
, child2
->id
,
3258 internal
->GetSortColumn(), (internal
->GetSortOrder() == GTK_SORT_DESCENDING
) );
3264 int LINKAGEMODE
wxGtkTreeModelChildPtrCmp( void*** data1
, void*** data2
)
3266 return gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(**data1
), wxDataViewItem(**data2
),
3267 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
3270 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr
);
3273 void wxGtkTreeModelNode::Resort()
3275 size_t child_count
= GetChildCount();
3276 if (child_count
== 0)
3279 size_t node_count
= GetNodesCount();
3281 if (child_count
== 1)
3283 if (node_count
== 1)
3285 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
3291 gint
*new_order
= new gint
[child_count
];
3294 // m_children has the original *void
3295 // ptrs points to these
3296 wxGtkTreeModelChildrenPtr ptrs
;
3298 for (i
= 0; i
< child_count
; i
++)
3299 ptrs
.Add( &(m_children
[i
]) );
3301 gs_internal
= m_internal
;
3302 ptrs
.Sort( &wxGtkTreeModelChildPtrCmp
);
3304 wxGtkTreeModelChildren temp
;
3305 void** base_ptr
= &(m_children
[0]);
3306 // Transfer positions to new_order array and
3308 for (i
= 0; i
< child_count
; i
++)
3310 new_order
[i
] = ptrs
[i
] - base_ptr
;
3311 temp
.Add( *ptrs
[i
] );
3314 // Transfer IDs back to m_children
3316 WX_APPEND_ARRAY( temp
, m_children
);
3321 // Build up array with IDs and original positions
3322 wxGtkTreeModelChildWithPos
* temp
= new wxGtkTreeModelChildWithPos
[child_count
];
3324 for (i
= 0; i
< child_count
; i
++)
3327 temp
[i
].id
= m_children
[i
];
3329 // Sort array keeping original positions
3330 wxQsort( temp
, child_count
, sizeof(wxGtkTreeModelChildWithPos
),
3331 &wxGtkTreeModelChildWithPosCmp
, m_internal
);
3332 // Transfer positions to new_order array and
3333 // IDs to m_children
3335 for (i
= 0; i
< child_count
; i
++)
3337 new_order
[i
] = temp
[i
].pos
;
3338 m_children
.Add( temp
[i
].id
);
3347 wxGtkTreeModelChildren temp
;
3348 WX_APPEND_ARRAY( temp
, m_children
);
3350 gs_internal
= m_internal
;
3351 m_children
.Sort( &wxGtkTreeModelChildCmp
);
3354 for (pos
= 0; pos
< child_count
; pos
++)
3356 void *id
= m_children
.Item( pos
);
3357 int old_pos
= temp
.Index( id
);
3358 new_order
[pos
] = old_pos
;
3362 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
3365 iter
.user_data
= GetItem().GetID();
3366 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
3368 gtk_tree_model_rows_reordered( gtk_tree_model
,
3369 wxGtkTreePath(m_internal
->get_path(&iter
)), &iter
, new_order
);
3371 delete [] new_order
;
3374 for (pos
= 0; pos
< node_count
; pos
++)
3376 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
3381 //-----------------------------------------------------------------------------
3382 // wxDataViewCtrlInternal
3383 //-----------------------------------------------------------------------------
3385 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
)
3388 m_wx_model
= wx_model
;
3392 m_sort_order
= GTK_SORT_ASCENDING
;
3394 m_dataview_sort_column
= NULL
;
3396 m_dragDataObject
= NULL
;
3397 m_dropDataObject
= NULL
;
3401 m_gtk_model
= wxgtk_tree_model_new();
3402 m_gtk_model
->internal
= this;
3404 m_notifier
= new wxGtkDataViewModelNotifier( wx_model
, this );
3406 wx_model
->AddNotifier( m_notifier
);
3408 // g_object_unref( gtk_model ); ???
3410 if (!m_wx_model
->IsVirtualListModel())
3413 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner
->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model
) );
3416 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3418 m_wx_model
->RemoveNotifier( m_notifier
);
3420 // remove the model from the GtkTreeView before it gets destroyed
3421 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner
->GtkGetTreeView() ), NULL
);
3423 g_object_unref( m_gtk_model
);
3425 delete m_dragDataObject
;
3426 delete m_dropDataObject
;
3429 void wxDataViewCtrlInternal::ScheduleRefresh()
3434 void wxDataViewCtrlInternal::OnInternalIdle()
3438 GtkWidget
*widget
= m_owner
->GtkGetTreeView();
3439 gtk_widget_queue_draw( widget
);
3444 void wxDataViewCtrlInternal::InitTree()
3446 wxDataViewItem item
;
3447 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
3449 BuildBranch( m_root
);
3452 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
3454 if (node
->GetChildCount() == 0)
3456 wxDataViewItemArray children
;
3457 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
3460 for (pos
= 0; pos
< count
; pos
++)
3462 wxDataViewItem child
= children
[pos
];
3464 if (m_wx_model
->IsContainer( child
))
3465 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
3467 node
->AddLeaf( child
.GetID() );
3469 // Don't send any events here
3476 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat
&format
)
3478 wxGtkString
atom_str( gdk_atom_name( format
) );
3479 m_dragSourceTargetEntryTarget
= wxCharBuffer( atom_str
);
3481 m_dragSourceTargetEntry
.target
= m_dragSourceTargetEntryTarget
.data();
3482 m_dragSourceTargetEntry
.flags
= 0;
3483 m_dragSourceTargetEntry
.info
= static_cast<guint
>(-1);
3485 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3486 GDK_BUTTON1_MASK
, &m_dragSourceTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3491 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat
&format
)
3493 wxGtkString
atom_str( gdk_atom_name( format
) );
3494 m_dropTargetTargetEntryTarget
= wxCharBuffer( atom_str
);
3496 m_dropTargetTargetEntry
.target
= m_dropTargetTargetEntryTarget
.data();
3497 m_dropTargetTargetEntry
.flags
= 0;
3498 m_dropTargetTargetEntry
.info
= static_cast<guint
>(-1);
3500 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3501 &m_dropTargetTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3506 gboolean
wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource
*WXUNUSED(drag_source
),
3509 delete m_dragDataObject
;
3510 m_dragDataObject
= NULL
;
3512 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3516 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3517 event
.SetEventObject( m_owner
);
3518 event
.SetItem( item
);
3519 event
.SetModel( m_wx_model
);
3521 gtk_widget_get_pointer(m_owner
->GtkGetTreeView(), &x
, &y
);
3522 event
.SetPosition(x
, y
);
3523 if (!m_owner
->HandleWindowEvent( event
))
3526 if (!event
.IsAllowed())
3529 wxDataObject
*obj
= event
.GetDataObject();
3533 m_dragDataObject
= obj
;
3539 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource
*WXUNUSED(drag_source
),
3540 GtkTreePath
*WXUNUSED(path
))
3545 gboolean
wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource
*WXUNUSED(drag_source
),
3546 GtkTreePath
*path
, GtkSelectionData
*selection_data
)
3548 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3552 GdkAtom target
= gtk_selection_data_get_target(selection_data
);
3553 if (!m_dragDataObject
->IsSupported(target
))
3556 size_t size
= m_dragDataObject
->GetDataSize(target
);
3560 void *buf
= malloc( size
);
3562 gboolean res
= FALSE
;
3563 if (m_dragDataObject
->GetDataHere(target
, buf
))
3567 gtk_selection_data_set(selection_data
, target
,
3568 8, (const guchar
*) buf
, size
);
3577 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3579 GtkSelectionData
*selection_data
)
3581 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3585 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
3586 event
.SetEventObject( m_owner
);
3587 event
.SetItem( item
);
3588 event
.SetModel( m_wx_model
);
3589 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3590 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3591 event
.SetDataBuffer(const_cast<guchar
*>(gtk_selection_data_get_data(selection_data
)));
3592 if (!m_owner
->HandleWindowEvent( event
))
3595 if (!event
.IsAllowed())
3602 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3604 GtkSelectionData
*selection_data
)
3606 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3610 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
3611 event
.SetEventObject( m_owner
);
3612 event
.SetItem( item
);
3613 event
.SetModel( m_wx_model
);
3614 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3615 if (!m_owner
->HandleWindowEvent( event
))
3618 if (!event
.IsAllowed())
3624 // notifications from wxDataViewModel
3626 bool wxDataViewCtrlInternal::Cleared()
3641 void wxDataViewCtrlInternal::Resort()
3643 if (!m_wx_model
->IsVirtualListModel())
3649 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3651 if (!m_wx_model
->IsVirtualListModel())
3653 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3654 wxCHECK_MSG(parent_node
, false,
3655 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3657 wxDataViewItemArray modelSiblings
;
3658 m_wx_model
->GetChildren(parent
, modelSiblings
);
3659 const int modelSiblingsSize
= modelSiblings
.size();
3661 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
3662 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
3664 const wxGtkTreeModelChildren
& nodeSiblings
= parent_node
->GetChildren();
3665 const int nodeSiblingsSize
= nodeSiblings
.size();
3669 if ( posInModel
== modelSiblingsSize
- 1 )
3671 nodePos
= nodeSiblingsSize
;
3673 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
3675 // This is the simple case when our node tree already matches the
3676 // model and only this one item is missing.
3677 nodePos
= posInModel
;
3681 // It's possible that a larger discrepancy between the model and
3682 // our realization exists. This can happen e.g. when adding a bunch
3683 // of items to the model and then calling ItemsAdded() just once
3684 // afterwards. In this case, we must find the right position by
3685 // looking at sibling items.
3687 // append to the end if we won't find a better position:
3688 nodePos
= nodeSiblingsSize
;
3690 for ( int nextItemPos
= posInModel
+ 1;
3691 nextItemPos
< modelSiblingsSize
;
3694 int nextNodePos
= parent_node
->FindChildByItem(modelSiblings
[nextItemPos
]);
3695 if ( nextNodePos
!= wxNOT_FOUND
)
3697 nodePos
= nextNodePos
;
3703 if (m_wx_model
->IsContainer( item
))
3704 parent_node
->InsertNode( new wxGtkTreeModelNode( parent_node
, item
, this ), nodePos
);
3706 parent_node
->InsertLeaf( item
.GetID(), nodePos
);
3714 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3716 if (!m_wx_model
->IsVirtualListModel())
3718 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3719 wxASSERT_MSG(parent_node
,
3720 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3722 parent_node
->DeleteChild( item
.GetID() );
3730 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
3732 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3733 event
.SetEventObject( m_owner
);
3734 event
.SetModel( m_owner
->GetModel() );
3735 event
.SetItem( item
);
3736 m_owner
->HandleWindowEvent( event
);
3741 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int view_column
)
3743 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3744 event
.SetEventObject( m_owner
);
3745 event
.SetModel( m_owner
->GetModel() );
3746 event
.SetColumn( view_column
);
3747 event
.SetDataViewColumn( GetOwner()->GetColumn(view_column
) );
3748 event
.SetItem( item
);
3749 m_owner
->HandleWindowEvent( event
);
3756 GtkTreeModelFlags
wxDataViewCtrlInternal::get_flags()
3760 if ( m_wx_model
->IsListModel() )
3761 flags
|= GTK_TREE_MODEL_LIST_ONLY
;
3763 if ( !m_wx_model
->IsVirtualListModel() )
3764 flags
|= GTK_TREE_MODEL_ITERS_PERSIST
;
3766 return GtkTreeModelFlags(flags
);
3769 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
3772 if (m_wx_model
->IsVirtualListModel())
3774 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3776 unsigned int i
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
3778 if (i
>= wx_model
->GetCount())
3781 iter
->stamp
= m_gtk_model
->stamp
;
3782 // user_data is just the index +1
3783 iter
->user_data
= wxUIntToPtr(i
+1);
3789 int depth
= gtk_tree_path_get_depth( path
);
3791 wxGtkTreeModelNode
*node
= m_root
;
3794 for (i
= 0; i
< depth
; i
++)
3796 BuildBranch( node
);
3798 gint pos
= gtk_tree_path_get_indices (path
)[i
];
3799 if (pos
< 0) return FALSE
;
3800 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
3802 void* id
= node
->GetChildren().Item( (size_t) pos
);
3806 iter
->stamp
= m_gtk_model
->stamp
;
3807 iter
->user_data
= id
;
3811 size_t count
= node
->GetNodes().GetCount();
3813 for (pos2
= 0; pos2
< count
; pos2
++)
3815 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
3816 if (child_node
->GetItem().GetID() == id
)
3828 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
3830 // When this is called from ItemDeleted(), the item is already
3831 // deleted in the model.
3833 GtkTreePath
*retval
= gtk_tree_path_new ();
3835 if (m_wx_model
->IsVirtualListModel())
3837 // iter is root, add nothing
3838 if (!iter
->user_data
)
3841 // user_data is just the index +1
3842 int i
= ( (wxUIntPtr
) iter
->user_data
) -1;
3843 gtk_tree_path_append_index (retval
, i
);
3847 void *id
= iter
->user_data
;
3849 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
3852 int pos
= node
->GetChildren().Index( id
);
3854 gtk_tree_path_prepend_index( retval
, pos
);
3856 id
= node
->GetItem().GetID();
3857 node
= node
->GetParent();
3864 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
3866 if (m_wx_model
->IsVirtualListModel())
3868 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3870 // user_data is just the index +1
3871 int n
= ( (wxUIntPtr
) iter
->user_data
) -1;
3875 iter
->user_data
= NULL
;
3879 if (n
>= (int) wx_model
->GetCount()-1)
3881 iter
->user_data
= NULL
;
3885 // user_data is just the index +1 (+2 because we need the next)
3886 iter
->user_data
= wxUIntToPtr(n
+2);
3890 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
3891 if( parent
== NULL
)
3893 iter
->user_data
= NULL
;
3897 int pos
= parent
->GetChildren().Index( iter
->user_data
);
3899 if (pos
== (int) parent
->GetChildCount()-1)
3901 iter
->user_data
= NULL
;
3905 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
3911 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
3913 if (m_wx_model
->IsVirtualListModel())
3915 // this is a list, nodes have no children
3919 iter
->stamp
= m_gtk_model
->stamp
;
3920 iter
->user_data
= (gpointer
) 1;
3928 if (m_root
->GetChildCount() == 0) return FALSE
;
3929 iter
->stamp
= m_gtk_model
->stamp
;
3930 iter
->user_data
= (gpointer
) m_root
->GetChildren().Item( 0 );
3935 wxDataViewItem item
;
3937 item
= wxDataViewItem( (void*) parent
->user_data
);
3939 if (!m_wx_model
->IsContainer( item
))
3942 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3943 wxASSERT_MSG(parent_node
,
3944 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3946 BuildBranch( parent_node
);
3948 if (parent_node
->GetChildCount() == 0)
3951 iter
->stamp
= m_gtk_model
->stamp
;
3952 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
3958 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
3960 if (m_wx_model
->IsVirtualListModel())
3962 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3965 return (wx_model
->GetCount() > 0);
3967 // this is a list, nodes have no children
3973 return (m_root
->GetChildCount() > 0);
3975 wxDataViewItem
item( (void*) iter
->user_data
);
3977 bool is_container
= m_wx_model
->IsContainer( item
);
3982 wxGtkTreeModelNode
*node
= FindNode( iter
);
3984 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3986 BuildBranch( node
);
3988 return (node
->GetChildCount() > 0);
3992 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
3994 if (m_wx_model
->IsVirtualListModel())
3996 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3999 return (gint
) wx_model
->GetCount();
4006 return m_root
->GetChildCount();
4008 wxDataViewItem
item( (void*) iter
->user_data
);
4010 if (!m_wx_model
->IsContainer( item
))
4013 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
4014 wxASSERT_MSG(parent_node
,
4015 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4017 BuildBranch( parent_node
);
4019 return parent_node
->GetChildCount();
4023 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
4025 if (m_wx_model
->IsVirtualListModel())
4027 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
4035 if (n
>= (gint
) wx_model
->GetCount())
4038 iter
->stamp
= m_gtk_model
->stamp
;
4039 // user_data is just the index +1
4040 iter
->user_data
= wxUIntToPtr(n
+1);
4047 if (parent
) id
= (void*) parent
->user_data
;
4048 wxDataViewItem
item( id
);
4050 if (!m_wx_model
->IsContainer( item
))
4053 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4054 wxASSERT_MSG(parent_node
,
4055 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4057 BuildBranch( parent_node
);
4059 iter
->stamp
= m_gtk_model
->stamp
;
4060 iter
->user_data
= parent_node
->GetChildren().Item( n
);
4066 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
4068 if (m_wx_model
->IsVirtualListModel())
4074 wxGtkTreeModelNode
*node
= FindParentNode( child
);
4078 iter
->stamp
= m_gtk_model
->stamp
;
4079 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
4085 // item can be deleted already in the model
4086 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
4088 if (m_wx_model
->IsVirtualListModel())
4090 return wxPtrToUInt(item
.GetID()) - 1;
4094 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4095 wxGtkTreeModelChildren
&children
= parent_node
->GetChildren();
4097 for (j
= 0; j
< children
.GetCount(); j
++)
4099 if (children
[j
] == item
.GetID())
4107 static wxGtkTreeModelNode
*
4108 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4114 list
.DeleteContents( true );
4115 wxDataViewItem
it( item
);
4119 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4120 list
.Insert( pItem
);
4121 it
= model
->GetParent( it
);
4124 wxGtkTreeModelNode
* node
= treeNode
;
4125 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4127 if( node
&& node
->GetNodes().GetCount() != 0 )
4129 int len
= node
->GetNodes().GetCount();
4130 wxGtkTreeModelNodes
&nodes
= node
->GetNodes();
4132 for( ; j
< len
; j
++)
4134 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4153 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
4158 wxDataViewItem
item( (void*) iter
->user_data
);
4162 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4167 wxLogDebug( "Not found %p", iter->user_data );
4171 // TODO: remove this code
4177 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
4182 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4187 wxLogDebug( "Not found %p", item.GetID() );
4191 // TODO: remove this code
4197 static wxGtkTreeModelNode
*
4198 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4204 list
.DeleteContents( true );
4208 wxDataViewItem
it( model
->GetParent( item
) );
4211 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4212 list
.Insert( pItem
);
4213 it
= model
->GetParent( it
);
4216 wxGtkTreeModelNode
* node
= treeNode
;
4217 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4219 if( node
&& node
->GetNodes().GetCount() != 0 )
4221 int len
= node
->GetNodes().GetCount();
4222 wxGtkTreeModelNodes nodes
= node
->GetNodes();
4224 for( ; j
< len
; j
++)
4226 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4241 //Examine whether the node is item's parent node
4242 int len
= node
->GetChildCount();
4243 for( int i
= 0; i
< len
; i
++ )
4245 if( node
->GetChildren().Item( i
) == item
.GetID() )
4251 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
4256 wxDataViewItem
item( (void*) iter
->user_data
);
4260 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4263 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
4268 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4271 //-----------------------------------------------------------------------------
4272 // wxDataViewCtrl signal callbacks
4273 //-----------------------------------------------------------------------------
4276 wxdataview_selection_changed_callback( GtkTreeSelection
* WXUNUSED(selection
), wxDataViewCtrl
*dv
)
4278 if (!gtk_widget_get_realized(dv
->m_widget
))
4281 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, dv
->GetId() );
4282 event
.SetItem( dv
->GetSelection() );
4283 event
.SetModel( dv
->GetModel() );
4284 dv
->HandleWindowEvent( event
);
4288 wxdataview_row_activated_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreePath
*path
,
4289 GtkTreeViewColumn
*WXUNUSED(column
), wxDataViewCtrl
*dv
)
4291 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
4293 wxDataViewItem
item(dv
->GTKPathToItem(path
));
4294 event
.SetItem( item
);
4295 event
.SetModel( dv
->GetModel() );
4296 dv
->HandleWindowEvent( event
);
4300 wxdataview_test_expand_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4301 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4303 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, dv
->GetId() );
4305 wxDataViewItem
item( (void*) iter
->user_data
);;
4306 event
.SetItem( item
);
4307 event
.SetModel( dv
->GetModel() );
4308 dv
->HandleWindowEvent( event
);
4310 return !event
.IsAllowed();
4314 wxdataview_row_expanded_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4315 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4317 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
, dv
->GetId() );
4319 wxDataViewItem
item( (void*) iter
->user_data
);;
4320 event
.SetItem( item
);
4321 event
.SetModel( dv
->GetModel() );
4322 dv
->HandleWindowEvent( event
);
4326 wxdataview_test_collapse_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4327 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4329 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
, dv
->GetId() );
4331 wxDataViewItem
item( (void*) iter
->user_data
);;
4332 event
.SetItem( item
);
4333 event
.SetModel( dv
->GetModel() );
4334 dv
->HandleWindowEvent( event
);
4336 return !event
.IsAllowed();
4340 wxdataview_row_collapsed_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4341 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4343 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
, dv
->GetId() );
4345 wxDataViewItem
item( (void*) iter
->user_data
);;
4346 event
.SetItem( item
);
4347 event
.SetModel( dv
->GetModel() );
4348 dv
->HandleWindowEvent( event
);
4351 //-----------------------------------------------------------------------------
4353 //-----------------------------------------------------------------------------
4355 void wxDataViewCtrl::AddChildGTK(wxWindowGTK
* child
)
4357 GtkWidget
* treeview
= GtkGetTreeView();
4359 // Insert widget in GtkTreeView
4360 if (gtk_widget_get_realized(treeview
))
4361 gtk_widget_set_parent_window( child
->m_widget
,
4362 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
4363 gtk_widget_set_parent( child
->m_widget
, treeview
);
4367 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
4368 GtkAllocation
*WXUNUSED(gtk_alloc
),
4369 wxDataViewCtrl
*win
)
4371 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
4374 wxWindow
*child
= node
->GetData();
4377 gtk_widget_size_request( child
->m_widget
, &req
);
4379 GtkAllocation alloc
;
4380 alloc
.x
= child
->m_x
;
4381 alloc
.y
= child
->m_y
;
4382 alloc
.width
= child
->m_width
;
4383 alloc
.height
= child
->m_height
;
4384 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
4386 node
= node
->GetNext();
4391 //-----------------------------------------------------------------------------
4392 // "motion_notify_event"
4393 //-----------------------------------------------------------------------------
4396 gtk_dataview_motion_notify_callback( GtkWidget
*WXUNUSED(widget
),
4397 GdkEventMotion
*gdk_event
,
4398 wxDataViewCtrl
*dv
)
4400 if (gdk_event
->is_hint
)
4404 GdkModifierType state
;
4405 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
4411 GtkTreeViewColumn
*column
= NULL
;
4414 if (gtk_tree_view_get_path_at_pos(
4415 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4416 (int) gdk_event
->x
, (int) gdk_event
->y
,
4425 dv
->GtkGetInternal()->get_iter( &iter
, path
);
4433 //-----------------------------------------------------------------------------
4434 // "button_press_event"
4435 //-----------------------------------------------------------------------------
4438 gtk_dataview_button_press_callback( GtkWidget
*WXUNUSED(widget
),
4439 GdkEventButton
*gdk_event
,
4440 wxDataViewCtrl
*dv
)
4442 if ((gdk_event
->button
== 3) && (gdk_event
->type
== GDK_BUTTON_PRESS
))
4445 GtkTreeViewColumn
*column
= NULL
;
4448 gtk_tree_view_get_path_at_pos
4450 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4451 (int) gdk_event
->x
, (int) gdk_event
->y
,
4458 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, dv
->GetId() );
4460 event
.SetItem(dv
->GTKPathToItem(path
));
4461 event
.SetModel( dv
->GetModel() );
4462 return dv
->HandleWindowEvent( event
);
4468 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4470 wxDataViewCtrl::~wxDataViewCtrl()
4472 // Stop editing before destroying the control to remove any event handlers
4473 // which are added when editing started: if we didn't do this, the base
4474 // class dtor would assert as it checks for any leftover handlers.
4477 GtkTreeViewColumn
*col
;
4478 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4480 wxDataViewColumn
* const wxcol
= FromGTKColumn(col
);
4483 // This won't do anything if we're not editing it
4484 wxcol
->GetRenderer()->CancelEditing();
4493 void wxDataViewCtrl::Init()
4497 m_cols
.DeleteContents( true );
4499 m_uniformRowHeight
= -1;
4502 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4507 const wxValidator
& validator
,
4508 const wxString
& name
)
4510 if (!PreCreation( parent
, pos
, size
) ||
4511 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
4513 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4517 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
4518 g_object_ref(m_widget
);
4520 GTKScrolledWindowSetBorder(m_widget
, style
);
4522 m_treeview
= gtk_tree_view_new();
4523 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
4525 m_focusWidget
= GTK_WIDGET(m_treeview
);
4527 g_signal_connect (m_treeview
, "size_allocate",
4528 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
4531 if (!gtk_check_version(2,6,0))
4533 bool fixed
= (style
& wxDV_VARIABLE_LINE_HEIGHT
) == 0;
4534 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4538 if (style
& wxDV_MULTIPLE
)
4540 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4541 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
4544 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
4547 if (!gtk_check_version(2,10,0))
4549 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
4551 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
4552 (style
& wxDV_VERT_RULES
) != 0)
4553 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
4554 else if (style
& wxDV_VERT_RULES
)
4555 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
4556 else if (style
& wxDV_HORIZ_RULES
)
4557 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
4559 if (grid
!= GTK_TREE_VIEW_GRID_LINES_NONE
)
4560 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
4564 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_ROW_LINES
) != 0 );
4566 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
4567 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
4568 gtk_widget_show (m_treeview
);
4570 m_parent
->DoAddChild( this );
4574 GtkEnableSelectionEvents();
4576 g_signal_connect_after (m_treeview
, "row-activated",
4577 G_CALLBACK (wxdataview_row_activated_callback
), this);
4579 g_signal_connect (m_treeview
, "test-collapse-row",
4580 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
4582 g_signal_connect_after (m_treeview
, "row-collapsed",
4583 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
4585 g_signal_connect (m_treeview
, "test-expand-row",
4586 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
4588 g_signal_connect_after (m_treeview
, "row-expanded",
4589 G_CALLBACK (wxdataview_row_expanded_callback
), this);
4591 g_signal_connect (m_treeview
, "motion_notify_event",
4592 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
4594 g_signal_connect (m_treeview
, "button_press_event",
4595 G_CALLBACK (gtk_dataview_button_press_callback
), this);
4600 wxDataViewItem
wxDataViewCtrl::GTKPathToItem(GtkTreePath
*path
) const
4603 return wxDataViewItem(path
&& m_internal
->get_iter(&iter
, path
)
4608 void wxDataViewCtrl::OnInternalIdle()
4610 wxWindow::OnInternalIdle();
4612 m_internal
->OnInternalIdle();
4614 unsigned int cols
= GetColumnCount();
4616 for (i
= 0; i
< cols
; i
++)
4618 wxDataViewColumn
*col
= GetColumn( i
);
4619 col
->OnInternalIdle();
4622 if (m_ensureVisibleDefered
.IsOk())
4624 ExpandAncestors(m_ensureVisibleDefered
);
4626 iter
.user_data
= (gpointer
) m_ensureVisibleDefered
.GetID();
4627 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4628 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
4629 m_ensureVisibleDefered
= wxDataViewItem(0);
4633 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4635 wxDELETE(m_internal
);
4637 if (!wxDataViewCtrlBase::AssociateModel( model
))
4641 if (!gtk_check_version(2,6,0))
4643 bool fixed
= (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
) == 0) || (model
->IsVirtualListModel()));
4644 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4648 m_internal
= new wxDataViewCtrlInternal( this, model
);
4653 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4655 return m_internal
->EnableDragSource( format
);
4658 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4660 return m_internal
->EnableDropTarget( format
);
4663 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4665 if (!wxDataViewCtrlBase::AppendColumn(col
))
4668 m_cols
.Append( col
);
4671 if (!gtk_check_version(2,6,0))
4673 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4674 GTK_TREE_VIEW_COLUMN_FIXED
)
4675 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4679 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
4680 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4685 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4687 if (!wxDataViewCtrlBase::PrependColumn(col
))
4690 m_cols
.Insert( col
);
4693 if (!gtk_check_version(2,6,0))
4695 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4696 GTK_TREE_VIEW_COLUMN_FIXED
)
4697 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4701 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4702 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
4707 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4709 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4712 m_cols
.Insert( pos
, col
);
4715 if (!gtk_check_version(2,6,0))
4717 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4718 GTK_TREE_VIEW_COLUMN_FIXED
)
4719 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4723 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4724 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), pos
);
4729 unsigned int wxDataViewCtrl::GetColumnCount() const
4731 return m_cols
.GetCount();
4734 wxDataViewColumn
* wxDataViewCtrl::FromGTKColumn(GtkTreeViewColumn
*gtk_col
) const
4739 wxDataViewColumnList::const_iterator iter
;
4740 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4742 wxDataViewColumn
*col
= *iter
;
4743 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
4749 wxFAIL_MSG( "No matching column?" );
4754 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
4756 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
4758 return FromGTKColumn(gtk_col
);
4761 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4763 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4764 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
4766 m_cols
.DeleteObject( column
);
4771 bool wxDataViewCtrl::ClearColumns()
4773 wxDataViewColumnList::iterator iter
;
4774 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4776 wxDataViewColumn
*col
= *iter
;
4777 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4778 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4786 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4788 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4790 wxGtkList
list(gtk_tree_view_get_columns(GTK_TREE_VIEW(m_treeview
)));
4792 return g_list_index( list
, (gconstpointer
) gtk_column
);
4795 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4797 return m_internal
->GetDataViewSortColumn();
4800 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4803 iter
.user_data
= item
.GetID();
4804 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4805 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
4808 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4811 iter
.user_data
= item
.GetID();
4812 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4813 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
4816 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4819 iter
.user_data
= item
.GetID();
4820 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4821 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview
), path
);
4824 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4826 // The tree doesn't have any current item if it hadn't been created yet but
4827 // it's arguably not an error to call this function in this case so just
4828 // return an invalid item without asserting.
4830 return wxDataViewItem();
4833 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), path
.ByRef(), NULL
);
4835 return GTKPathToItem(path
);
4838 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4840 wxCHECK_RET( m_treeview
,
4841 "Current item can't be set before creating the control." );
4843 // We need to make sure the model knows about this item or the path would
4844 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4845 ExpandAncestors(item
);
4847 // We also need to preserve the existing selection from changing.
4848 // Unfortunately the only way to do it seems to use our own selection
4849 // function and forbid any selection changes during set cursor call.
4850 wxGtkTreeSelectionLock
4851 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4853 // Do move the cursor now.
4855 iter
.user_data
= item
.GetID();
4856 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4858 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, NULL
, FALSE
);
4861 void wxDataViewCtrl::StartEditor(const wxDataViewItem
& item
, unsigned int column
)
4863 wxCHECK_RET( m_treeview
,
4864 "Current item can't be set before creating the control." );
4866 // We need to make sure the model knows about this item or the path would
4867 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4868 ExpandAncestors(item
);
4870 wxDataViewColumn
*dvcolumn
= GetColumn(column
);
4871 wxASSERT_MSG(dvcolumn
, "Could not retrieve column");
4872 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(dvcolumn
->GetGtkHandle());
4874 // We also need to preserve the existing selection from changing.
4875 // Unfortunately the only way to do it seems to use our own selection
4876 // function and forbid any selection changes during set cursor call.
4877 wxGtkTreeSelectionLock
4878 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4880 // Do move the cursor now.
4882 iter
.user_data
= item
.GetID();
4883 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4885 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, gcolumn
, TRUE
);
4888 int wxDataViewCtrl::GetSelectedItemsCount() const
4890 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4892 return gtk_tree_selection_count_selected_rows(selection
);
4895 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4899 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4900 if (HasFlag(wxDV_MULTIPLE
))
4902 GtkTreeModel
*model
;
4903 wxGtkTreePathList
list(gtk_tree_selection_get_selected_rows(selection
, &model
));
4905 for ( GList
* current
= list
; current
; current
= g_list_next(current
) )
4907 GtkTreePath
*path
= (GtkTreePath
*) current
->data
;
4909 sel
.Add(GTKPathToItem(path
));
4915 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
4917 sel
.Add( wxDataViewItem(iter
.user_data
) );
4924 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4926 GtkDisableSelectionEvents();
4928 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4930 gtk_tree_selection_unselect_all( selection
);
4932 wxDataViewItem last_parent
;
4935 for (i
= 0; i
< sel
.GetCount(); i
++)
4937 wxDataViewItem item
= sel
[i
];
4938 wxDataViewItem parent
= GetModel()->GetParent( item
);
4941 if (parent
!= last_parent
)
4942 ExpandAncestors(item
);
4944 last_parent
= parent
;
4947 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4948 iter
.user_data
= (gpointer
) item
.GetID();
4949 gtk_tree_selection_select_iter( selection
, &iter
);
4952 GtkEnableSelectionEvents();
4955 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4957 ExpandAncestors(item
);
4959 GtkDisableSelectionEvents();
4961 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4964 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4965 iter
.user_data
= (gpointer
) item
.GetID();
4966 gtk_tree_selection_select_iter( selection
, &iter
);
4968 GtkEnableSelectionEvents();
4971 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4973 GtkDisableSelectionEvents();
4975 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4978 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4979 iter
.user_data
= (gpointer
) item
.GetID();
4980 gtk_tree_selection_unselect_iter( selection
, &iter
);
4982 GtkEnableSelectionEvents();
4985 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4987 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4990 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4991 iter
.user_data
= (gpointer
) item
.GetID();
4993 return gtk_tree_selection_iter_is_selected( selection
, &iter
);
4996 void wxDataViewCtrl::SelectAll()
4998 GtkDisableSelectionEvents();
5000 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5002 gtk_tree_selection_select_all( selection
);
5004 GtkEnableSelectionEvents();
5007 void wxDataViewCtrl::UnselectAll()
5009 GtkDisableSelectionEvents();
5011 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5013 gtk_tree_selection_unselect_all( selection
);
5015 GtkEnableSelectionEvents();
5018 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem
& item
,
5019 const wxDataViewColumn
*WXUNUSED(column
))
5021 m_ensureVisibleDefered
= item
;
5022 ExpandAncestors(item
);
5025 iter
.user_data
= (gpointer
) item
.GetID();
5026 wxGtkTreePath
path(m_internal
->get_path( &iter
));
5027 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
5030 void wxDataViewCtrl::HitTest(const wxPoint
& point
,
5031 wxDataViewItem
& item
,
5032 wxDataViewColumn
*& column
) const
5034 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
5035 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
5036 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
5037 // So we have to use both of them.
5038 // Friedrich Haase 2010-9-20
5039 wxGtkTreePath path
, pathScratch
;
5040 GtkTreeViewColumn
* GtkColumn
= NULL
;
5041 GtkTreeViewDropPosition pos
= GTK_TREE_VIEW_DROP_INTO_OR_AFTER
;
5045 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5046 wxDataViewCtrl
* ctrl
= (wxDataViewCtrl
*)this; // ugly workaround, ctrl is NOT const
5047 GtkTreeView
* treeView
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
5049 // is there possibly a better suited function to get the column?
5050 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5052 (int) point
.x
, (int) point
.y
,
5053 pathScratch
.ByRef(),
5054 &GtkColumn
, // here we get the GtkColumn
5058 if ( GtkColumn
!= NULL
)
5060 // we got GTK column
5061 // the right call now which takes the header into account
5062 gtk_tree_view_get_dest_row_at_pos( treeView
, (int) point
.x
, (int) point
.y
, path
.ByRef(), &pos
);
5065 item
= wxDataViewItem(GTKPathToItem(path
));
5066 // else we got a GTK column but the position is not over an item, e.g. below last item
5067 for ( unsigned int i
=0, cols
=GetColumnCount(); i
<cols
; ++i
) // search the wx column
5069 wxDataViewColumn
* col
= GetColumn(i
);
5070 if ( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == GtkColumn
)
5072 column
= col
; // here we get the wx column
5077 // else no column and thus no item, both null
5081 wxDataViewCtrl::GetItemRect(const wxDataViewItem
& WXUNUSED(item
),
5082 const wxDataViewColumn
*WXUNUSED(column
)) const
5087 bool wxDataViewCtrl::SetRowHeight(int rowHeight
)
5089 m_uniformRowHeight
= rowHeight
;
5093 void wxDataViewCtrl::DoSetExpanderColumn()
5095 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
5096 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5099 void wxDataViewCtrl::DoSetIndent()
5103 void wxDataViewCtrl::GtkDisableSelectionEvents()
5105 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5106 g_signal_handlers_disconnect_by_func( selection
,
5107 (gpointer
) (wxdataview_selection_changed_callback
), this);
5110 void wxDataViewCtrl::GtkEnableSelectionEvents()
5112 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5113 g_signal_connect_after (selection
, "changed",
5114 G_CALLBACK (wxdataview_selection_changed_callback
), this);
5117 // ----------------------------------------------------------------------------
5118 // visual attributes stuff
5119 // ----------------------------------------------------------------------------
5123 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
5125 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
5128 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
5130 wxDataViewCtrlBase::DoApplyWidgetStyle(style
);
5131 gtk_widget_modify_style(m_treeview
, style
);
5134 #endif // !wxUSE_GENERICDATAVIEWCTRL
5136 #endif // wxUSE_DATAVIEWCTRL