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 using namespace wxGTKImpl
;
39 class wxGtkDataViewModelNotifier
;
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static wxDataViewCtrlInternal
*gs_internal
= NULL
;
46 class wxGtkTreeModelNode
;
49 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
52 // ----------------------------------------------------------------------------
53 // wxGtkTreePath: self-destroying GtkTreePath
54 // ----------------------------------------------------------------------------
56 // Usually this object is initialized with the associated GtkTreePath
57 // immediately when it's constructed but it can also be changed later either by
58 // using Assign() or by getting the pointer to the internally stored pointer
59 // value using ByRef(). The latter should be avoided but is very convenient
60 // when using GTK functions with GtkTreePath output parameters.
64 // Ctor takes ownership of the given path and will free it if non-NULL.
65 wxGtkTreePath(GtkTreePath
*path
= NULL
) : m_path(path
) { }
67 // Creates a tree path for the given string path.
68 wxGtkTreePath(const gchar
*strpath
)
69 : m_path(gtk_tree_path_new_from_string(strpath
))
73 // Set the stored pointer if not done by ctor.
74 void Assign(GtkTreePath
*path
)
76 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
81 // Return the pointer to the internally stored pointer. This should only be
82 // used to initialize the object by passing it to some GTK function.
85 wxASSERT_MSG( !m_path
, "shouldn't be already initialized" );
91 operator GtkTreePath
*() const { return m_path
; }
93 ~wxGtkTreePath() { if ( m_path
) gtk_tree_path_free(m_path
); }
98 wxDECLARE_NO_COPY_CLASS(wxGtkTreePath
);
101 // ----------------------------------------------------------------------------
102 // wxGtkTreePathList: self-destroying list of GtkTreePath objects.
103 // ----------------------------------------------------------------------------
105 class wxGtkTreePathList
: public wxGtkList
108 // Ctor takes ownership of the list.
109 explicit wxGtkTreePathList(GList
* list
)
116 // Delete the list contents, wxGtkList will delete the list itself.
117 g_list_foreach(m_list
, (GFunc
)gtk_tree_path_free
, NULL
);
121 // ----------------------------------------------------------------------------
122 // wxGtkTreeSelectionLock: prevent selection from changing during the
123 // lifetime of this object
124 // ----------------------------------------------------------------------------
126 // Implementation note: it could be expected that setting the selection
127 // function in this class ctor and resetting it back to the old value in its
128 // dtor would work. However currently gtk_tree_selection_get_select_function()
129 // can't be passed NULL (see https://bugzilla.gnome.org/show_bug.cgi?id=626276)
130 // so we can't do this. Instead, we always use the selection function (which
131 // imposes extra overhead, albeit minimal one, on all selection operations) and
132 // just set/reset the flag telling it whether it should allow or forbid the
135 // Also notice that currently only a single object of this class may exist at
136 // any given moment. It's just simpler like this and we don't need anything
140 gboolean
wxdataview_selection_func(GtkTreeSelection
* WXUNUSED(selection
),
141 GtkTreeModel
* WXUNUSED(model
),
142 GtkTreePath
* WXUNUSED(path
),
143 gboolean
WXUNUSED(path_currently_selected
),
149 class wxGtkTreeSelectionLock
152 wxGtkTreeSelectionLock(GtkTreeSelection
*selection
)
153 : m_selection(selection
)
155 wxASSERT_MSG( !ms_instance
, "this class is not reentrant currently" );
159 CheckCurrentSelectionFunc(NULL
);
161 // Pass some non-NULL pointer as "data" for the callback, it doesn't
162 // matter what it is as long as it's non-NULL.
163 gtk_tree_selection_set_select_function(selection
,
164 wxdataview_selection_func
,
169 ~wxGtkTreeSelectionLock()
171 CheckCurrentSelectionFunc(wxdataview_selection_func
);
173 gtk_tree_selection_set_select_function(m_selection
,
174 wxdataview_selection_func
,
182 void CheckCurrentSelectionFunc(GtkTreeSelectionFunc func
)
184 // We can only use gtk_tree_selection_get_select_function() with 2.14+
185 // so check for its availability both during compile- and run-time.
186 #if GTK_CHECK_VERSION(2, 14, 0)
187 if ( gtk_check_version(2, 14, 0) != NULL
)
190 // If this assert is triggered, it means the code elsewhere has called
191 // gtk_tree_selection_set_select_function() but currently doing this
192 // breaks this class so the code here needs to be changed.
195 gtk_tree_selection_get_select_function(m_selection
) == func
,
196 "selection function has changed unexpectedly, review this code!"
203 static wxGtkTreeSelectionLock
*ms_instance
;
205 GtkTreeSelection
* const m_selection
;
207 wxDECLARE_NO_COPY_CLASS(wxGtkTreeSelectionLock
);
210 wxGtkTreeSelectionLock
*wxGtkTreeSelectionLock::ms_instance
= NULL
;
212 //-----------------------------------------------------------------------------
213 // wxDataViewCtrlInternal
214 //-----------------------------------------------------------------------------
216 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
217 WX_DEFINE_LIST(ItemList
)
219 class wxDataViewCtrlInternal
222 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
);
223 ~wxDataViewCtrlInternal();
226 GtkTreeModelFlags
get_flags();
227 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
228 GtkTreePath
*get_path( GtkTreeIter
*iter
);
229 gboolean
iter_next( GtkTreeIter
*iter
);
230 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
231 gboolean
iter_has_child( GtkTreeIter
*iter
);
232 gint
iter_n_children( GtkTreeIter
*iter
);
233 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
234 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
238 bool EnableDragSource( const wxDataFormat
&format
);
239 bool EnableDropTarget( const wxDataFormat
&format
);
241 gboolean
row_draggable( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
);
242 gboolean
drag_data_delete( GtkTreeDragSource
*drag_source
, GtkTreePath
* path
);
243 gboolean
drag_data_get( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
,
244 GtkSelectionData
*selection_data
);
245 gboolean
drag_data_received( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest
,
246 GtkSelectionData
*selection_data
);
247 gboolean
row_drop_possible( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest_path
,
248 GtkSelectionData
*selection_data
);
250 // notifactions from wxDataViewModel
251 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
252 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
253 bool ItemChanged( const wxDataViewItem
&item
);
254 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
261 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
262 GtkSortType
GetSortOrder() const { return m_sort_order
; }
264 void SetSortColumn( int column
) { m_sort_column
= column
; }
265 int GetSortColumn() const { return m_sort_column
; }
267 void SetDataViewSortColumn( wxDataViewColumn
*column
) { m_dataview_sort_column
= column
; }
268 wxDataViewColumn
*GetDataViewSortColumn() { return m_dataview_sort_column
; }
270 bool IsSorted() { return (m_sort_column
>= 0); }
273 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
274 const wxDataViewModel
* GetDataViewModel() const { return m_wx_model
; }
275 wxDataViewCtrl
* GetOwner() { return m_owner
; }
276 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
278 // item can be deleted already in the model
279 int GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
281 virtual void OnInternalIdle();
285 void ScheduleRefresh();
287 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
288 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
289 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
290 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
291 void BuildBranch( wxGtkTreeModelNode
*branch
);
294 wxGtkTreeModelNode
*m_root
;
295 wxDataViewModel
*m_wx_model
;
296 GtkWxTreeModel
*m_gtk_model
;
297 wxDataViewCtrl
*m_owner
;
298 GtkSortType m_sort_order
;
299 wxDataViewColumn
*m_dataview_sort_column
;
302 GtkTargetEntry m_dragSourceTargetEntry
;
303 wxCharBuffer m_dragSourceTargetEntryTarget
;
304 wxDataObject
*m_dragDataObject
;
306 GtkTargetEntry m_dropTargetTargetEntry
;
307 wxCharBuffer m_dropTargetTargetEntryTarget
;
308 wxDataObject
*m_dropDataObject
;
310 wxGtkDataViewModelNotifier
*m_notifier
;
316 //-----------------------------------------------------------------------------
317 // wxGtkTreeModelNode
318 //-----------------------------------------------------------------------------
321 int LINKAGEMODE
wxGtkTreeModelChildCmp( void** id1
, void** id2
)
323 int ret
= gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(*id1
), wxDataViewItem(*id2
),
324 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
329 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
330 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren
);
332 class wxGtkTreeModelNode
335 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
336 wxDataViewCtrlInternal
*internal
)
340 m_internal
= internal
;
343 ~wxGtkTreeModelNode()
345 size_t count
= m_nodes
.GetCount();
347 for (i
= 0; i
< count
; i
++)
349 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
354 void AddNode( wxGtkTreeModelNode
* child
)
356 m_nodes
.Add( child
);
358 void *id
= child
->GetItem().GetID();
360 m_children
.Add( id
);
362 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
364 gs_internal
= m_internal
;
365 m_children
.Sort( &wxGtkTreeModelChildCmp
);
369 void InsertNode( wxGtkTreeModelNode
* child
, unsigned pos
)
371 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
377 void *id
= child
->GetItem().GetID();
379 // Insert into m_nodes so that the order of nodes in m_nodes is the
380 // same as the order of their corresponding IDs in m_children:
381 const unsigned int count
= m_nodes
.GetCount();
382 bool inserted
= false;
383 for (unsigned i
= 0; i
< count
; i
++)
385 wxGtkTreeModelNode
*node
= m_nodes
[i
];
386 int posInChildren
= m_children
.Index(node
->GetItem().GetID());
387 if ( (unsigned)posInChildren
>= pos
)
389 m_nodes
.Insert(child
, i
);
397 m_children
.Insert( id
, pos
);
400 void AddLeaf( void* id
)
402 InsertLeaf(id
, m_children
.size());
405 void InsertLeaf( void* id
, unsigned pos
)
407 m_children
.Insert( id
, pos
);
409 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
411 gs_internal
= m_internal
;
412 m_children
.Sort( &wxGtkTreeModelChildCmp
);
416 void DeleteChild( void* id
)
418 m_children
.Remove( id
);
420 unsigned int count
= m_nodes
.GetCount();
422 for (pos
= 0; pos
< count
; pos
++)
424 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
425 if (node
->GetItem().GetID() == id
)
427 m_nodes
.RemoveAt( pos
);
434 wxGtkTreeModelNode
* GetParent()
436 wxGtkTreeModelNodes
&GetNodes()
438 wxGtkTreeModelChildren
&GetChildren()
439 { return m_children
; }
441 unsigned int GetChildCount() const { return m_children
.GetCount(); }
442 unsigned int GetNodesCount() const { return m_nodes
.GetCount(); }
444 wxDataViewItem
&GetItem() { return m_item
; }
445 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
450 wxGtkTreeModelNode
*m_parent
;
451 wxGtkTreeModelNodes m_nodes
;
452 wxGtkTreeModelChildren m_children
;
453 wxDataViewItem m_item
;
454 wxDataViewCtrlInternal
*m_internal
;
458 //-----------------------------------------------------------------------------
460 //-----------------------------------------------------------------------------
462 extern bool g_blockEventsOnDrag
;
464 //-----------------------------------------------------------------------------
465 // define new GTK+ class wxGtkTreeModel
466 //-----------------------------------------------------------------------------
470 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
471 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
472 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
473 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
474 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
475 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
477 GType
gtk_wx_tree_model_get_type (void);
479 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
481 struct _GtkWxTreeModel
487 wxDataViewCtrlInternal
*internal
;
490 struct _GtkWxTreeModelClass
492 GObjectClass list_parent_class
;
495 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
496 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
497 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
499 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
500 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
501 static void wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface
*iface
);
502 static void wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface
*iface
);
504 static void wxgtk_tree_model_finalize (GObject
*object
);
505 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
506 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
507 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
509 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
512 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
514 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
518 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
520 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
522 GtkTreeIter
*parent
);
523 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
525 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
527 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
531 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
536 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
537 gint
*sort_column_id
,
539 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
542 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
544 GtkTreeIterCompareFunc func
,
546 GDestroyNotify destroy
);
547 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
548 GtkTreeIterCompareFunc func
,
550 GDestroyNotify destroy
);
551 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
554 static gboolean
wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
556 static gboolean
wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
558 static gboolean
wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
560 GtkSelectionData
*selection_data
);
561 static gboolean
wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
563 GtkSelectionData
*selection_data
);
564 static gboolean
wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
565 GtkTreePath
*dest_path
,
566 GtkSelectionData
*selection_data
);
569 static GObjectClass
*list_parent_class
= NULL
;
572 gtk_wx_tree_model_get_type (void)
574 static GType tree_model_type
= 0;
576 if (!tree_model_type
)
578 const GTypeInfo tree_model_info
=
580 sizeof (GtkWxTreeModelClass
),
581 NULL
, /* base_init */
582 NULL
, /* base_finalize */
583 (GClassInitFunc
) wxgtk_tree_model_class_init
,
584 NULL
, /* class_finalize */
585 NULL
, /* class_data */
586 sizeof (GtkWxTreeModel
),
588 (GInstanceInitFunc
) wxgtk_tree_model_init
,
591 static const GInterfaceInfo tree_model_iface_info
=
593 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
598 static const GInterfaceInfo sortable_iface_info
=
600 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
605 static const GInterfaceInfo drag_source_iface_info
=
607 (GInterfaceInitFunc
) wxgtk_tree_model_drag_source_init
,
612 static const GInterfaceInfo drag_dest_iface_info
=
614 (GInterfaceInitFunc
) wxgtk_tree_model_drag_dest_init
,
619 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
620 &tree_model_info
, (GTypeFlags
)0 );
622 g_type_add_interface_static (tree_model_type
,
624 &tree_model_iface_info
);
625 g_type_add_interface_static (tree_model_type
,
626 GTK_TYPE_TREE_SORTABLE
,
627 &sortable_iface_info
);
628 g_type_add_interface_static (tree_model_type
,
629 GTK_TYPE_TREE_DRAG_DEST
,
630 &drag_dest_iface_info
);
631 g_type_add_interface_static (tree_model_type
,
632 GTK_TYPE_TREE_DRAG_SOURCE
,
633 &drag_source_iface_info
);
636 return tree_model_type
;
639 static GtkWxTreeModel
*
640 wxgtk_tree_model_new(void)
642 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
647 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
649 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
650 GObjectClass
*object_class
= (GObjectClass
*) klass
;
651 object_class
->finalize
= wxgtk_tree_model_finalize
;
655 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
657 iface
->get_flags
= wxgtk_tree_model_get_flags
;
658 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
659 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
660 iface
->get_iter
= wxgtk_tree_model_get_iter
;
661 iface
->get_path
= wxgtk_tree_model_get_path
;
662 iface
->get_value
= wxgtk_tree_model_get_value
;
663 iface
->iter_next
= wxgtk_tree_model_iter_next
;
664 iface
->iter_children
= wxgtk_tree_model_iter_children
;
665 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
666 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
667 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
668 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
672 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
674 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
675 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
676 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
677 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
678 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
682 wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface
*iface
)
684 iface
->row_draggable
= wxgtk_tree_model_row_draggable
;
685 iface
->drag_data_delete
= wxgtk_tree_model_drag_data_delete
;
686 iface
->drag_data_get
= wxgtk_tree_model_drag_data_get
;
690 wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface
*iface
)
692 iface
->drag_data_received
= wxgtk_tree_model_drag_data_received
;
693 iface
->row_drop_possible
= wxgtk_tree_model_row_drop_possible
;
697 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
699 tree_model
->internal
= NULL
;
700 tree_model
->stamp
= g_random_int();
704 wxgtk_tree_model_finalize (GObject
*object
)
707 (* list_parent_class
->finalize
) (object
);
712 //-----------------------------------------------------------------------------
713 // implement callbacks from wxGtkTreeModel class by letting
714 // them call the methods of wxWidgets' wxDataViewModel
715 //-----------------------------------------------------------------------------
717 static GtkTreeModelFlags
718 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
720 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
721 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), (GtkTreeModelFlags
)0 );
723 return wxtree_model
->internal
->get_flags();
727 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
729 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
730 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
732 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
736 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
739 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
740 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
742 GType gtype
= G_TYPE_INVALID
;
744 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
746 if (wxtype
== wxT("string"))
747 gtype
= G_TYPE_STRING
;
750 gtype
= G_TYPE_POINTER
;
751 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
758 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
762 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
763 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
764 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
766 return wxtree_model
->internal
->get_iter( iter
, path
);
770 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
773 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
774 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
775 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
777 return wxtree_model
->internal
->get_path( iter
);
781 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
786 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
787 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
789 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
790 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
791 if (mtype
== wxT("string"))
794 g_value_init( value
, G_TYPE_STRING
);
795 wxDataViewItem
item( (void*) iter
->user_data
);
796 model
->GetValue( variant
, item
, (unsigned int) column
);
798 g_value_set_string( value
, variant
.GetString().utf8_str() );
802 wxFAIL_MSG( wxT("non-string columns not supported yet") );
807 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
810 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
812 // This happens when clearing the view by calling .._set_model( NULL );
813 if (iter
->stamp
== 0) return FALSE
;
815 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
816 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
818 return wxtree_model
->internal
->iter_next( iter
);
822 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
826 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
827 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
830 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
833 return wxtree_model
->internal
->iter_children( iter
, parent
);
837 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
840 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
841 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
842 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
844 return wxtree_model
->internal
->iter_has_child( iter
);
848 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
851 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
852 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
853 g_return_val_if_fail ( !iter
|| wxtree_model
->stamp
== iter
->stamp
, 0);
855 return wxtree_model
->internal
->iter_n_children( iter
);
859 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
864 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
865 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
867 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
871 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
875 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
876 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
877 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
879 return wxtree_model
->internal
->iter_parent( iter
, child
);
882 /* drag'n'drop iface */
884 wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
887 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
888 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
890 return wxtree_model
->internal
->row_draggable( drag_source
, path
);
894 wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
897 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
898 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
900 return wxtree_model
->internal
->drag_data_delete( drag_source
, path
);
904 wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
906 GtkSelectionData
*selection_data
)
908 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
909 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
912 wxPrintf( "drag_get_data\n");
914 wxGtkString
atom_selection(gdk_atom_name(selection_data
->selection
));
915 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection
) );
917 wxGtkString
atom_target(gdk_atom_name(selection_data
->target
));
918 wxPrintf( "target %s\n", wxString::FromAscii(atom_target
) );
920 wxGtkString
atom_type(gdk_atom_name(selection_data
->type
));
921 wxPrintf( "type %s\n", wxString::FromAscii(atom_type
) );
923 wxPrintf( "format %d\n", selection_data
->format
);
926 return wxtree_model
->internal
->drag_data_get( drag_source
, path
, selection_data
);
930 wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
932 GtkSelectionData
*selection_data
)
934 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
935 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
937 return wxtree_model
->internal
->drag_data_received( drag_dest
, dest
, selection_data
);
941 wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
942 GtkTreePath
*dest_path
,
943 GtkSelectionData
*selection_data
)
945 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
946 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
948 return wxtree_model
->internal
->row_drop_possible( drag_dest
, dest_path
, selection_data
);
953 wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
954 gint
*sort_column_id
,
957 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) sortable
;
959 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
961 if (!wxtree_model
->internal
->IsSorted())
964 *sort_column_id
= -1;
971 *sort_column_id
= wxtree_model
->internal
->GetSortColumn();
974 *order
= wxtree_model
->internal
->GetSortOrder();
979 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
982 wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
986 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
987 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
989 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
991 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
992 (order
!= tree_model
->internal
->GetSortOrder()))
994 tree_model
->internal
->SetSortColumn( sort_column_id
);
995 tree_model
->internal
->SetSortOrder( order
);
997 gtk_tree_sortable_sort_column_changed (sortable
);
999 tree_model
->internal
->GetDataViewModel()->Resort();
1002 if (gs_lastLeftClickHeader
)
1004 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
1005 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
1006 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
1007 event
.SetModel( dv
->GetModel() );
1008 dv
->HandleWindowEvent( event
);
1011 gs_lastLeftClickHeader
= NULL
;
1015 wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
1016 gint
WXUNUSED(sort_column_id
),
1017 GtkTreeIterCompareFunc func
,
1018 gpointer
WXUNUSED(data
),
1019 GDestroyNotify
WXUNUSED(destroy
))
1021 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1022 g_return_if_fail (func
!= NULL
);
1026 wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
1027 GtkTreeIterCompareFunc func
,
1028 gpointer
WXUNUSED(data
),
1029 GDestroyNotify
WXUNUSED(destroy
))
1031 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
1032 g_return_if_fail (func
!= NULL
);
1034 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
1035 // TODO: remove this code
1039 wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
1041 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
1046 //-----------------------------------------------------------------------------
1047 // define new GTK+ class GtkWxRendererText
1048 //-----------------------------------------------------------------------------
1052 #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
1053 #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
1054 #define GTK_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1055 #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1056 #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1057 #define GTK_WX_CELL_RENDERER_TEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1059 GType
gtk_wx_cell_renderer_text_get_type (void);
1061 typedef struct _GtkWxCellRendererText GtkWxCellRendererText
;
1062 typedef struct _GtkWxCellRendererTextClass GtkWxCellRendererTextClass
;
1064 struct _GtkWxCellRendererText
1066 GtkCellRendererText parent
;
1068 wxDataViewRenderer
*wx_renderer
;
1071 struct _GtkWxCellRendererTextClass
1073 GtkCellRendererTextClass cell_parent_class
;
1077 static GtkWxCellRendererText
*gtk_wx_cell_renderer_text_new (void);
1078 static void gtk_wx_cell_renderer_text_init (
1079 GtkWxCellRendererText
*cell
);
1080 static void gtk_wx_cell_renderer_text_class_init(
1081 GtkWxCellRendererTextClass
*klass
);
1082 static void gtk_wx_cell_renderer_text_finalize (
1084 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1085 GtkCellRenderer
*cell
,
1089 GdkRectangle
*background_area
,
1090 GdkRectangle
*cell_area
,
1091 GtkCellRendererState flags
);
1094 static GObjectClass
*text_cell_parent_class
= NULL
;
1099 gtk_wx_cell_renderer_text_get_type (void)
1101 static GType cell_wx_type
= 0;
1105 const GTypeInfo cell_wx_info
=
1107 sizeof (GtkWxCellRendererTextClass
),
1108 NULL
, /* base_init */
1109 NULL
, /* base_finalize */
1110 (GClassInitFunc
) gtk_wx_cell_renderer_text_class_init
,
1111 NULL
, /* class_finalize */
1112 NULL
, /* class_data */
1113 sizeof (GtkWxCellRendererText
),
1114 0, /* n_preallocs */
1115 (GInstanceInitFunc
) gtk_wx_cell_renderer_text_init
,
1118 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT
,
1119 "GtkWxCellRendererText", &cell_wx_info
, (GTypeFlags
)0 );
1122 return cell_wx_type
;
1126 gtk_wx_cell_renderer_text_init (GtkWxCellRendererText
*cell
)
1128 cell
->wx_renderer
= NULL
;
1132 gtk_wx_cell_renderer_text_class_init (GtkWxCellRendererTextClass
*klass
)
1134 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
1135 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1137 text_cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1139 object_class
->finalize
= gtk_wx_cell_renderer_text_finalize
;
1141 cell_class
->start_editing
= gtk_wx_cell_renderer_text_start_editing
;
1145 gtk_wx_cell_renderer_text_finalize (GObject
*object
)
1148 (* G_OBJECT_CLASS (text_cell_parent_class
)->finalize
) (object
);
1151 GtkWxCellRendererText
*
1152 gtk_wx_cell_renderer_text_new (void)
1154 return (GtkWxCellRendererText
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT
, NULL
);
1157 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1158 GtkCellRenderer
*gtk_renderer
,
1159 GdkEvent
*gdk_event
,
1162 GdkRectangle
*background_area
,
1163 GdkRectangle
*cell_area
,
1164 GtkCellRendererState flags
)
1166 GtkWxCellRendererText
*wxgtk_renderer
= (GtkWxCellRendererText
*) gtk_renderer
;
1167 wxDataViewRenderer
*wx_renderer
= wxgtk_renderer
->wx_renderer
;
1168 wxDataViewColumn
*column
= wx_renderer
->GetOwner();
1171 item(column
->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1173 wxDataViewCtrl
*dv
= column
->GetOwner();
1174 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING
, dv
->GetId() );
1175 event
.SetDataViewColumn( column
);
1176 event
.SetModel( dv
->GetModel() );
1177 event
.SetColumn( column
->GetModelColumn() );
1178 event
.SetItem( item
);
1179 dv
->HandleWindowEvent( event
);
1181 if (event
.IsAllowed())
1182 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class
)->
1183 start_editing( gtk_renderer
, gdk_event
, widget
, path
, background_area
, cell_area
, flags
);
1188 //-----------------------------------------------------------------------------
1189 // define new GTK+ class GtkWxCellRenderer
1190 //-----------------------------------------------------------------------------
1194 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
1195 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
1196 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1197 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
1198 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
1199 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1201 GType
gtk_wx_cell_renderer_get_type (void);
1203 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
1204 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
1206 struct _GtkWxCellRenderer
1208 GtkCellRenderer parent
;
1211 wxDataViewCustomRenderer
*cell
;
1215 struct _GtkWxCellRendererClass
1217 GtkCellRendererClass cell_parent_class
;
1221 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
1222 static void gtk_wx_cell_renderer_init (
1223 GtkWxCellRenderer
*cell
);
1224 static void gtk_wx_cell_renderer_class_init(
1225 GtkWxCellRendererClass
*klass
);
1226 static void gtk_wx_cell_renderer_finalize (
1228 static void gtk_wx_cell_renderer_get_size (
1229 GtkCellRenderer
*cell
,
1231 GdkRectangle
*rectangle
,
1236 static void gtk_wx_cell_renderer_render (
1237 GtkCellRenderer
*cell
,
1240 GdkRectangle
*background_area
,
1241 GdkRectangle
*cell_area
,
1242 GdkRectangle
*expose_area
,
1243 GtkCellRendererState flags
);
1244 static gboolean
gtk_wx_cell_renderer_activate(
1245 GtkCellRenderer
*cell
,
1249 GdkRectangle
*background_area
,
1250 GdkRectangle
*cell_area
,
1251 GtkCellRendererState flags
);
1252 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1253 GtkCellRenderer
*cell
,
1257 GdkRectangle
*background_area
,
1258 GdkRectangle
*cell_area
,
1259 GtkCellRendererState flags
);
1262 static GObjectClass
*cell_parent_class
= NULL
;
1267 gtk_wx_cell_renderer_get_type (void)
1269 static GType cell_wx_type
= 0;
1273 const GTypeInfo cell_wx_info
=
1275 sizeof (GtkWxCellRendererClass
),
1276 NULL
, /* base_init */
1277 NULL
, /* base_finalize */
1278 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
1279 NULL
, /* class_finalize */
1280 NULL
, /* class_data */
1281 sizeof (GtkWxCellRenderer
),
1282 0, /* n_preallocs */
1283 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
1286 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
1287 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
1290 return cell_wx_type
;
1294 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
1297 cell
->last_click
= 0;
1301 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
1303 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
1304 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1306 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1308 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
1310 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
1311 cell_class
->render
= gtk_wx_cell_renderer_render
;
1312 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
1313 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
1317 gtk_wx_cell_renderer_finalize (GObject
*object
)
1320 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
1324 gtk_wx_cell_renderer_new (void)
1326 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
1329 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1330 GtkCellRenderer
*renderer
,
1331 GdkEvent
*WXUNUSED(event
),
1334 GdkRectangle
*WXUNUSED(background_area
),
1335 GdkRectangle
*cell_area
,
1336 GtkCellRendererState
WXUNUSED(flags
) )
1338 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1339 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1341 // Renderer doesn't support in-place editing
1342 if (!cell
->HasEditorCtrl())
1345 // An in-place editing control is still around
1346 if (cell
->GetEditorCtrl())
1350 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1356 rect
.x
+= cell_area
->x
;
1357 rect
.y
+= cell_area
->y
;
1358 // rect.width -= renderer->xpad * 2;
1359 // rect.height -= renderer->ypad * 2;
1361 // wxRect renderrect(wxRectFromGDKRect(&rect));
1362 wxRect
renderrect(wxRectFromGDKRect(cell_area
));
1365 item(cell
->GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1367 cell
->StartEditing( item
, renderrect
);
1373 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
1374 GtkWidget
*WXUNUSED(widget
),
1375 GdkRectangle
*cell_area
,
1381 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1382 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1384 wxSize size
= cell
->GetSize();
1386 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1388 // Uniform row height, if specified, overrides the value returned by the
1390 if ( !ctrl
->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
1392 const int uniformHeight
= ctrl
->GTKGetUniformRowHeight();
1393 if ( uniformHeight
> 0 )
1394 size
.y
= uniformHeight
;
1398 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1399 int calc_width
= xpad
* 2 + size
.x
;
1400 int calc_height
= ypad
* 2 + size
.y
;
1407 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
1409 float xalign
, yalign
;
1410 gtk_cell_renderer_get_alignment(renderer
, &xalign
, &yalign
);
1413 *x_offset
= int(xalign
* (cell_area
->width
- calc_width
- 2 * xpad
));
1414 *x_offset
= MAX(*x_offset
, 0) + xpad
;
1418 *y_offset
= int(yalign
* (cell_area
->height
- calc_height
- 2 * ypad
));
1419 *y_offset
= MAX(*y_offset
, 0) + ypad
;
1424 *width
= calc_width
;
1427 *height
= calc_height
;
1431 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
1434 GdkRectangle
*background_area
,
1435 GdkRectangle
*cell_area
,
1436 GdkRectangle
*expose_area
,
1437 GtkCellRendererState flags
)
1440 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1441 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1443 cell
->GTKStashRenderParams(window
, widget
,
1444 background_area
, expose_area
, flags
);
1446 wxRect
rect(wxRectFromGDKRect(cell_area
));
1448 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1449 rect
= rect
.Deflate(xpad
, ypad
);
1451 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
1452 wxWindowDCImpl
*impl
= (wxWindowDCImpl
*) dc
->GetImpl();
1454 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1455 // window such as a DnD drop window.
1456 if (window
!= impl
->m_gdkwindow
)
1459 impl
->m_gdkwindow
= window
;
1464 if (flags
& GTK_CELL_RENDERER_SELECTED
)
1465 state
|= wxDATAVIEW_CELL_SELECTED
;
1466 if (flags
& GTK_CELL_RENDERER_PRELIT
)
1467 state
|= wxDATAVIEW_CELL_PRELIT
;
1468 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1469 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1470 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1471 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1472 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
1473 state
|= wxDATAVIEW_CELL_FOCUSED
;
1474 cell
->WXCallRender( rect
, dc
, state
);
1478 gtk_wx_cell_renderer_activate(
1479 GtkCellRenderer
*renderer
,
1483 GdkRectangle
*WXUNUSED(background_area
),
1484 GdkRectangle
*cell_area
,
1485 GtkCellRendererState
WXUNUSED(flags
) )
1487 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1488 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1491 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1497 rect
.x
+= cell_area
->x
;
1498 rect
.y
+= cell_area
->y
;
1500 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1501 rect
.width
-= xpad
* 2;
1502 rect
.height
-= ypad
* 2;
1504 wxRect
renderrect(wxRectFromGDKRect(&rect
));
1506 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1507 wxDataViewModel
*model
= ctrl
->GetModel();
1509 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
1511 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1517 // activated by <ENTER>
1518 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1523 else if (event
->type
== GDK_BUTTON_PRESS
)
1525 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
1526 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
1527 ((int) button_event
->y
) - renderrect
.y
);
1530 if (button_event
->button
== 1)
1532 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
1534 // TODO: query system double-click time
1535 if (button_event
->time
- wxrenderer
->last_click
< 400)
1536 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1539 wxrenderer
->last_click
= button_event
->time
;
1547 // ---------------------------------------------------------
1548 // wxGtkDataViewModelNotifier
1549 // ---------------------------------------------------------
1551 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1554 wxGtkDataViewModelNotifier( wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
);
1555 ~wxGtkDataViewModelNotifier();
1557 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1558 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1559 virtual bool ItemChanged( const wxDataViewItem
&item
);
1560 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
1561 virtual bool Cleared();
1562 virtual void Resort();
1563 virtual bool BeforeReset();
1564 virtual bool AfterReset();
1566 void UpdateLastCount();
1569 wxDataViewModel
*m_wx_model
;
1570 wxDataViewCtrlInternal
*m_internal
;
1573 // ---------------------------------------------------------
1574 // wxGtkDataViewListModelNotifier
1575 // ---------------------------------------------------------
1577 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1578 wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
)
1580 m_wx_model
= wx_model
;
1581 m_internal
= internal
;
1584 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1590 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1592 m_internal
->ItemAdded( parent
, item
);
1593 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1596 iter
.stamp
= wxgtk_model
->stamp
;
1597 iter
.user_data
= item
.GetID();
1599 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1600 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1601 gtk_tree_model_row_inserted(
1602 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1607 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1609 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1611 // using _get_path for a deleted item cannot be
1614 iter
.stamp
= wxgtk_model
->stamp
;
1615 iter
.user_data
= (gpointer
) item
.GetID();
1616 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1617 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1619 // so get the path from the parent
1620 GtkTreeIter parentIter
;
1621 parentIter
.stamp
= wxgtk_model
->stamp
;
1622 parentIter
.user_data
= (gpointer
) parent
.GetID();
1623 wxGtkTreePath
parentPath(wxgtk_tree_model_get_path(
1624 GTK_TREE_MODEL(wxgtk_model
), &parentIter
));
1626 // and add the final index ourselves
1627 wxGtkTreePath
path(gtk_tree_path_copy(parentPath
));
1628 int index
= m_internal
->GetIndexOf( parent
, item
);
1629 gtk_tree_path_append_index( path
, index
);
1632 gtk_tree_model_row_deleted(
1633 GTK_TREE_MODEL(wxgtk_model
), path
);
1635 m_internal
->ItemDeleted( parent
, item
);
1637 // Did we remove the last child, causing 'parent' to become a leaf?
1638 if ( !m_wx_model
->IsContainer(parent
) )
1640 gtk_tree_model_row_has_child_toggled
1642 GTK_TREE_MODEL(wxgtk_model
),
1651 void wxGtkDataViewModelNotifier::Resort()
1653 m_internal
->Resort();
1656 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1658 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1661 iter
.stamp
= wxgtk_model
->stamp
;
1662 iter
.user_data
= (gpointer
) item
.GetID();
1664 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1665 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1666 gtk_tree_model_row_changed(
1667 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1669 m_internal
->ItemChanged( item
);
1674 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
)
1676 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1677 wxDataViewCtrl
*ctrl
= m_internal
->GetOwner();
1679 // This adds GTK+'s missing MVC logic for ValueChanged
1681 for (index
= 0; index
< ctrl
->GetColumnCount(); index
++)
1683 wxDataViewColumn
*column
= ctrl
->GetColumn( index
);
1684 if (column
->GetModelColumn() == model_column
)
1686 GtkTreeView
*widget
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
1687 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1689 // Don't attempt to refresh not yet realized tree, it is useless
1690 // and results in GTK errors.
1691 if ( gtk_widget_get_realized(ctrl
->GtkGetTreeView()) )
1695 iter
.stamp
= wxgtk_model
->stamp
;
1696 iter
.user_data
= (gpointer
) item
.GetID();
1697 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1698 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1699 GdkRectangle cell_area
;
1700 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1702 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1703 double d
= gtk_adjustment_get_value( hadjust
);
1704 int xdiff
= (int) d
;
1706 int ydiff
= gcolumn
->button
->allocation
.height
;
1708 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1709 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1712 m_internal
->ValueChanged( item
, model_column
);
1721 bool wxGtkDataViewModelNotifier::BeforeReset()
1723 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1724 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), NULL
);
1729 bool wxGtkDataViewModelNotifier::AfterReset()
1731 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1732 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1734 m_internal
->Cleared();
1736 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), GTK_TREE_MODEL(wxgtk_model
) );
1741 bool wxGtkDataViewModelNotifier::Cleared()
1743 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1745 // There is no call to tell the model that everything
1746 // has been deleted so call row_deleted() for every
1749 int count
= m_internal
->iter_n_children( NULL
); // number of children of root
1751 GtkTreePath
*path
= gtk_tree_path_new_first(); // points to root
1754 for (i
= 0; i
< count
; i
++)
1755 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model
), path
);
1757 gtk_tree_path_free( path
);
1759 m_internal
->Cleared();
1764 // ---------------------------------------------------------
1765 // wxDataViewRenderer
1766 // ---------------------------------------------------------
1768 static gpointer s_user_data
= NULL
;
1771 wxgtk_cell_editable_editing_done( GtkCellEditable
*WXUNUSED(editable
),
1772 wxDataViewRenderer
*wxrenderer
)
1774 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1775 wxDataViewCtrl
*dv
= column
->GetOwner();
1776 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1777 event
.SetDataViewColumn( column
);
1778 event
.SetModel( dv
->GetModel() );
1779 wxDataViewItem
item( s_user_data
);
1780 event
.SetItem( item
);
1781 dv
->HandleWindowEvent( event
);
1785 wxgtk_renderer_editing_started( GtkCellRenderer
*WXUNUSED(cell
), GtkCellEditable
*editable
,
1786 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1791 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1792 wxDataViewCtrl
*dv
= column
->GetOwner();
1793 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1794 event
.SetDataViewColumn( column
);
1795 event
.SetModel( dv
->GetModel() );
1796 wxDataViewItem
item(dv
->GTKPathToItem(wxGtkTreePath(path
)));
1797 event
.SetItem( item
);
1798 dv
->HandleWindowEvent( event
);
1800 if (GTK_IS_CELL_EDITABLE(editable
))
1802 s_user_data
= item
.GetID();
1804 g_signal_connect (GTK_CELL_EDITABLE (editable
), "editing_done",
1805 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1806 (gpointer
) wxrenderer
);
1812 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1814 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1816 wxDataViewRendererBase( varianttype
, mode
, align
)
1821 // we haven't changed them yet
1822 m_usingDefaultAttrs
= true;
1824 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1825 // after the m_renderer pointer has been initialized
1828 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
1830 gtk_tree_view_column_pack_end( column
, m_renderer
, TRUE
/* expand */);
1833 void wxDataViewRenderer::GtkInitHandlers()
1835 if (!gtk_check_version(2,6,0))
1837 g_signal_connect (GTK_CELL_RENDERER(m_renderer
), "editing_started",
1838 G_CALLBACK (wxgtk_renderer_editing_started
),
1843 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1845 GtkCellRendererMode gtkMode
;
1848 case wxDATAVIEW_CELL_INERT
:
1849 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1852 case wxDATAVIEW_CELL_ACTIVATABLE
:
1853 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1856 case wxDATAVIEW_CELL_EDITABLE
:
1857 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1861 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1867 // This value is most often ignored in GtkTreeView
1868 GValue gvalue
= { 0, };
1869 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1870 g_value_set_enum( &gvalue
, gtkMode
);
1871 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1872 g_value_unset( &gvalue
);
1875 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1877 wxDataViewCellMode ret
;
1880 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1882 switch (g_value_get_enum(&gvalue
))
1885 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1886 // fall through (we have to return something)
1888 case GTK_CELL_RENDERER_MODE_INERT
:
1889 ret
= wxDATAVIEW_CELL_INERT
;
1892 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1893 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1896 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1897 ret
= wxDATAVIEW_CELL_EDITABLE
;
1901 g_value_unset( &gvalue
);
1906 void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer
*renderer
)
1908 int align
= m_alignment
;
1910 // query alignment from column ?
1914 if (GetOwner() == NULL
)
1917 align
= GetOwner()->GetAlignment();
1918 align
|= wxALIGN_CENTRE_VERTICAL
;
1921 // horizontal alignment:
1923 gfloat xalign
= 0.0;
1924 if (align
& wxALIGN_RIGHT
)
1926 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1929 GValue gvalue
= { 0, };
1930 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1931 g_value_set_float( &gvalue
, xalign
);
1932 g_object_set_property( G_OBJECT(renderer
), "xalign", &gvalue
);
1933 g_value_unset( &gvalue
);
1935 // vertical alignment:
1937 gfloat yalign
= 0.0;
1938 if (align
& wxALIGN_BOTTOM
)
1940 else if (align
& wxALIGN_CENTER_VERTICAL
)
1943 GValue gvalue2
= { 0, };
1944 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1945 g_value_set_float( &gvalue2
, yalign
);
1946 g_object_set_property( G_OBJECT(renderer
), "yalign", &gvalue2
);
1947 g_value_unset( &gvalue2
);
1950 void wxDataViewRenderer::SetAlignment( int align
)
1952 m_alignment
= align
;
1953 GtkUpdateAlignment();
1956 int wxDataViewRenderer::GetAlignment() const
1961 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode
)
1964 if ( gtk_check_version(2, 6, 0) != NULL
)
1967 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1971 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1972 // can just cast between them
1973 GValue gvalue
= { 0, };
1974 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1975 g_value_set_enum( &gvalue
, static_cast<PangoEllipsizeMode
>(mode
) );
1976 g_object_set_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1977 g_value_unset( &gvalue
);
1980 #endif // GTK 2.6/before
1983 wxEllipsizeMode
wxDataViewRenderer::GetEllipsizeMode() const
1986 if ( gtk_check_version(2, 6, 0) != NULL
)
1987 return wxELLIPSIZE_NONE
;
1989 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1991 return wxELLIPSIZE_NONE
;
1993 GValue gvalue
= { 0, };
1994 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1995 g_object_get_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1997 mode
= static_cast<wxEllipsizeMode
>(g_value_get_enum( &gvalue
));
1998 g_value_unset( &gvalue
);
2002 return wxELLIPSIZE_NONE
;
2003 #endif // GTK 2.6/before
2007 wxDataViewRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
2009 wxVariant
value(str
);
2010 if (!Validate( value
))
2014 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2016 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2020 wxDataViewRenderer::GtkOnCellChanged(const wxVariant
& value
,
2021 const wxDataViewItem
& item
,
2024 wxDataViewModel
*model
= GetOwner()->GetOwner()->GetModel();
2025 model
->ChangeValue( value
, item
, col
);
2028 // ---------------------------------------------------------
2029 // wxDataViewTextRenderer
2030 // ---------------------------------------------------------
2035 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*WXUNUSED(renderer
),
2036 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
2038 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) user_data
;
2040 cell
->GtkOnTextEdited(arg1
, wxGTK_CONV_BACK_FONT(
2041 arg2
, cell
->GetOwner()->GetOwner()->GetFont()));
2049 // helper function used by wxDataViewTextRenderer and
2050 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
2051 // given text renderer and returns true if anything was done
2052 bool GtkApplyAttr(GtkCellRendererText
*renderer
, const wxDataViewItemAttr
& attr
)
2054 bool usingDefaultAttrs
= true;
2055 if (attr
.HasColour())
2057 const GdkColor
* const gcol
= attr
.GetColour().GetColor();
2059 GValue gvalue
= { 0, };
2060 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2061 g_value_set_boxed( &gvalue
, gcol
);
2062 g_object_set_property( G_OBJECT(renderer
), "foreground_gdk", &gvalue
);
2063 g_value_unset( &gvalue
);
2065 usingDefaultAttrs
= false;
2069 GValue gvalue
= { 0, };
2070 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2071 g_value_set_boolean( &gvalue
, FALSE
);
2072 g_object_set_property( G_OBJECT(renderer
), "foreground-set", &gvalue
);
2073 g_value_unset( &gvalue
);
2076 if (attr
.GetItalic())
2078 GValue gvalue
= { 0, };
2079 g_value_init( &gvalue
, PANGO_TYPE_STYLE
);
2080 g_value_set_enum( &gvalue
, PANGO_STYLE_ITALIC
);
2081 g_object_set_property( G_OBJECT(renderer
), "style", &gvalue
);
2082 g_value_unset( &gvalue
);
2084 usingDefaultAttrs
= false;
2088 GValue gvalue
= { 0, };
2089 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2090 g_value_set_boolean( &gvalue
, FALSE
);
2091 g_object_set_property( G_OBJECT(renderer
), "style-set", &gvalue
);
2092 g_value_unset( &gvalue
);
2098 GValue gvalue
= { 0, };
2099 g_value_init( &gvalue
, PANGO_TYPE_WEIGHT
);
2100 g_value_set_enum( &gvalue
, PANGO_WEIGHT_BOLD
);
2101 g_object_set_property( G_OBJECT(renderer
), "weight", &gvalue
);
2102 g_value_unset( &gvalue
);
2104 usingDefaultAttrs
= false;
2108 GValue gvalue
= { 0, };
2109 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2110 g_value_set_boolean( &gvalue
, FALSE
);
2111 g_object_set_property( G_OBJECT(renderer
), "weight-set", &gvalue
);
2112 g_value_unset( &gvalue
);
2116 if (attr
.HasBackgroundColour())
2118 wxColour colour
= attr
.GetBackgroundColour();
2119 const GdkColor
* const gcol
= colour
.GetColor();
2121 GValue gvalue
= { 0, };
2122 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2123 g_value_set_boxed( &gvalue
, gcol
);
2124 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2125 g_value_unset( &gvalue
);
2129 GValue gvalue
= { 0, };
2130 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2131 g_value_set_boolean( &gvalue
, FALSE
);
2132 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2133 g_value_unset( &gvalue
);
2137 return !usingDefaultAttrs
;
2140 } // anonymous namespace
2142 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
2144 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2146 wxDataViewRenderer( varianttype
, mode
, align
)
2148 GtkWxCellRendererText
*text_renderer
= gtk_wx_cell_renderer_text_new();
2149 text_renderer
->wx_renderer
= this;
2150 m_renderer
= (GtkCellRenderer
*) text_renderer
;
2152 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
2154 GValue gvalue
= { 0, };
2155 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2156 g_value_set_boolean( &gvalue
, true );
2157 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
2158 g_value_unset( &gvalue
);
2160 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2166 SetAlignment(align
);
2169 bool wxDataViewTextRenderer::SetTextValue(const wxString
& str
)
2171 GValue gvalue
= { 0, };
2172 g_value_init( &gvalue
, G_TYPE_STRING
);
2173 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( str
, GetOwner()->GetOwner()->GetFont() ) );
2174 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2175 g_value_unset( &gvalue
);
2180 bool wxDataViewTextRenderer::GetTextValue(wxString
& str
) const
2182 GValue gvalue
= { 0, };
2183 g_value_init( &gvalue
, G_TYPE_STRING
);
2184 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2185 str
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
), const_cast<wxDataViewTextRenderer
*>(this)->GetOwner()->GetOwner()->GetFont() );
2186 g_value_unset( &gvalue
);
2191 void wxDataViewTextRenderer::SetAlignment( int align
)
2193 wxDataViewRenderer::SetAlignment(align
);
2195 if (gtk_check_version(2,10,0))
2198 // horizontal alignment:
2199 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2200 if (align
& wxALIGN_RIGHT
)
2201 pangoAlign
= PANGO_ALIGN_RIGHT
;
2202 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2203 pangoAlign
= PANGO_ALIGN_CENTER
;
2205 GValue gvalue
= { 0, };
2206 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2207 g_value_set_enum( &gvalue
, pangoAlign
);
2208 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2209 g_value_unset( &gvalue
);
2212 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr
& attr
)
2214 return GtkApplyAttr(GtkGetTextRenderer(), attr
);
2217 GtkCellRendererText
*wxDataViewTextRenderer::GtkGetTextRenderer() const
2219 return GTK_CELL_RENDERER_TEXT(m_renderer
);
2222 // ---------------------------------------------------------
2223 // wxDataViewBitmapRenderer
2224 // ---------------------------------------------------------
2229 // set "pixbuf" property on the given renderer
2230 void SetPixbufProp(GtkCellRenderer
*renderer
, GdkPixbuf
*pixbuf
)
2232 GValue gvalue
= { 0, };
2233 g_value_init( &gvalue
, G_TYPE_OBJECT
);
2234 g_value_set_object( &gvalue
, pixbuf
);
2235 g_object_set_property( G_OBJECT(renderer
), "pixbuf", &gvalue
);
2236 g_value_unset( &gvalue
);
2239 } // anonymous namespace
2241 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
2243 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2245 wxDataViewRenderer( varianttype
, mode
, align
)
2247 m_renderer
= gtk_cell_renderer_pixbuf_new();
2250 SetAlignment(align
);
2253 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
2255 if (value
.GetType() == wxT("wxBitmap"))
2260 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2261 // object (and it will stay there and remain owned by wxBitmap)
2262 SetPixbufProp(m_renderer
, bitmap
.GetPixbuf());
2264 else if (value
.GetType() == wxT("wxIcon"))
2269 SetPixbufProp(m_renderer
, icon
.GetPixbuf());
2279 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2284 // ---------------------------------------------------------
2285 // wxDataViewToggleRenderer
2286 // ---------------------------------------------------------
2289 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2290 gchar
*path
, gpointer user_data
);
2293 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2294 gchar
*path
, gpointer user_data
)
2296 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
2299 GValue gvalue
= { 0, };
2300 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2301 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
2302 bool tmp
= g_value_get_boolean( &gvalue
);
2303 g_value_unset( &gvalue
);
2307 wxVariant value
= tmp
;
2308 if (!cell
->Validate( value
))
2311 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
2312 wxDataViewModel
*model
= ctrl
->GetModel();
2314 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
2316 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
2318 model
->ChangeValue( value
, item
, model_col
);
2321 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
2323 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
2324 wxDataViewCellMode mode
, int align
) :
2325 wxDataViewRenderer( varianttype
, mode
, align
)
2327 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
2329 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
2331 g_signal_connect_after( m_renderer
, "toggled",
2332 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
2336 GValue gvalue
= { 0, };
2337 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2338 g_value_set_boolean( &gvalue
, false );
2339 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
2340 g_value_unset( &gvalue
);
2344 SetAlignment(align
);
2347 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
2351 GValue gvalue
= { 0, };
2352 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2353 g_value_set_boolean( &gvalue
, tmp
);
2354 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2355 g_value_unset( &gvalue
);
2360 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
2362 GValue gvalue
= { 0, };
2363 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2364 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2365 bool tmp
= g_value_get_boolean( &gvalue
);
2366 g_value_unset( &gvalue
);
2373 // ---------------------------------------------------------
2374 // wxDataViewCustomRenderer
2375 // ---------------------------------------------------------
2377 class wxDataViewCtrlDCImpl
: public wxWindowDCImpl
2380 wxDataViewCtrlDCImpl( wxDC
*owner
, wxDataViewCtrl
*window
) :
2381 wxWindowDCImpl( owner
)
2383 GtkWidget
*widget
= window
->m_treeview
;
2389 m_context
= window
->GTKGetPangoDefaultContext();
2390 m_layout
= pango_layout_new( m_context
);
2391 m_fontdesc
= pango_font_description_copy(gtk_widget_get_style(widget
)->font_desc
);
2393 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
2395 // Set m_gdkwindow later
2400 class wxDataViewCtrlDC
: public wxWindowDC
2403 wxDataViewCtrlDC( wxDataViewCtrl
*window
) :
2404 wxWindowDC( new wxDataViewCtrlDCImpl( this, window
) )
2409 // ---------------------------------------------------------
2410 // wxDataViewCustomRenderer
2411 // ---------------------------------------------------------
2413 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
2415 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
2416 wxDataViewCellMode mode
,
2419 : wxDataViewCustomRendererBase( varianttype
, mode
, align
)
2422 m_text_renderer
= NULL
;
2430 void wxDataViewCustomRenderer::GtkInitTextRenderer()
2432 m_text_renderer
= GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2433 g_object_ref_sink(m_text_renderer
);
2435 GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer
));
2438 GtkCellRendererText
*wxDataViewCustomRenderer::GtkGetTextRenderer() const
2440 if ( !m_text_renderer
)
2442 // we create it on demand so need to do it even from a const function
2443 const_cast<wxDataViewCustomRenderer
*>(this)->GtkInitTextRenderer();
2446 return m_text_renderer
;
2449 void wxDataViewCustomRenderer::RenderText( const wxString
&text
,
2453 int WXUNUSED(state
) )
2456 GtkCellRendererText
* const textRenderer
= GtkGetTextRenderer();
2458 GValue gvalue
= { 0, };
2459 g_value_init( &gvalue
, G_TYPE_STRING
);
2460 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( text
, GetOwner()->GetOwner()->GetFont() ) );
2461 g_object_set_property( G_OBJECT(textRenderer
), "text", &gvalue
);
2462 g_value_unset( &gvalue
);
2464 GtkApplyAttr(textRenderer
, GetAttr());
2466 GdkRectangle cell_area
;
2467 wxRectToGDKRect(cell
, cell_area
);
2468 cell_area
.x
+= xoffset
;
2469 cell_area
.width
-= xoffset
;
2471 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer
),
2472 m_renderParams
.window
,
2473 m_renderParams
.widget
,
2474 m_renderParams
.background_area
,
2476 m_renderParams
.expose_area
,
2477 (GtkCellRendererState
) m_renderParams
.flags
);
2480 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
2482 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
2483 renderer
->cell
= this;
2485 m_renderer
= (GtkCellRenderer
*) renderer
;
2488 SetAlignment(align
);
2495 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2500 if (m_text_renderer
)
2501 g_object_unref(m_text_renderer
);
2504 wxDC
*wxDataViewCustomRenderer::GetDC()
2508 if (GetOwner() == NULL
)
2510 if (GetOwner()->GetOwner() == NULL
)
2512 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2518 // ---------------------------------------------------------
2519 // wxDataViewProgressRenderer
2520 // ---------------------------------------------------------
2522 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
2524 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
2525 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
2526 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
2532 if (!gtk_check_version(2,6,0))
2534 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
2537 SetAlignment(align
);
2540 // We can't initialize the renderer just yet because we don't have the
2541 // pointer to the column that uses this renderer yet and so attempt to
2542 // dereference GetOwner() to get the font that is used as a source of
2543 // encoding in multibyte-to-Unicode conversion in GTKSetLabel() in
2544 // non-Unicode builds would crash. So simply remember to do it later.
2545 if ( !m_label
.empty() )
2546 m_needsToSetLabel
= true;
2548 #endif // !wxUSE_UNICODE
2554 // Use custom cell code
2555 wxDataViewCustomRenderer::Init(mode
, align
);
2559 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2563 void wxDataViewProgressRenderer::GTKSetLabel()
2565 GValue gvalue
= { 0, };
2566 g_value_init( &gvalue
, G_TYPE_STRING
);
2568 // Take care to not use GetOwner() here if the label is empty, we can be
2569 // called from ctor when GetOwner() is still NULL in this case.
2570 g_value_set_string( &gvalue
,
2571 m_label
.empty() ? ""
2572 : wxGTK_CONV_FONT(m_label
,
2573 GetOwner()->GetOwner()->GetFont())
2575 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2576 g_value_unset( &gvalue
);
2579 m_needsToSetLabel
= false;
2580 #endif // !wxUSE_UNICODE
2583 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
2586 if (!gtk_check_version(2,6,0))
2589 if ( m_needsToSetLabel
)
2591 #endif // !wxUSE_UNICODE
2593 gint tmp
= (long) value
;
2594 GValue gvalue
= { 0, };
2595 g_value_init( &gvalue
, G_TYPE_INT
);
2596 g_value_set_int( &gvalue
, tmp
);
2597 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
2598 g_value_unset( &gvalue
);
2603 m_value
= (long) value
;
2605 if (m_value
< 0) m_value
= 0;
2606 if (m_value
> 100) m_value
= 100;
2612 bool wxDataViewProgressRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2617 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
2619 double pct
= (double)m_value
/ 100.0;
2621 bar
.width
= (int)(cell
.width
* pct
);
2622 dc
->SetPen( *wxTRANSPARENT_PEN
);
2623 dc
->SetBrush( *wxBLUE_BRUSH
);
2624 dc
->DrawRectangle( bar
);
2626 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
2627 dc
->SetPen( *wxBLACK_PEN
);
2628 dc
->DrawRectangle( cell
);
2633 wxSize
wxDataViewProgressRenderer::GetSize() const
2635 return wxSize(40,12);
2638 // -------------------------------------
2639 // wxDataViewChoiceRenderer
2640 // -------------------------------------
2642 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString
&choices
,
2643 wxDataViewCellMode mode
, int alignment
) :
2644 wxDataViewCustomRenderer( "string", mode
, alignment
, true )
2646 m_choices
= choices
;
2649 if (!gtk_check_version(2,6,0))
2651 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_combo_new();
2653 GtkListStore
*store
= gtk_list_store_new( 1, G_TYPE_STRING
);
2654 for (size_t n
= 0; n
< m_choices
.GetCount(); n
++)
2656 gtk_list_store_insert_with_values(
2658 static_cast<const char *>(m_choices
[n
].utf8_str()), -1 );
2661 g_object_set (m_renderer
,
2667 bool editable
= (mode
& wxDATAVIEW_CELL_EDITABLE
);
2668 g_object_set (m_renderer
, "editable", editable
, NULL
);
2670 SetAlignment(alignment
);
2672 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2679 // Use custom cell code
2680 wxDataViewCustomRenderer::Init(mode
, alignment
);
2684 bool wxDataViewChoiceRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
2686 RenderText( m_data
, 0, rect
, dc
, state
);
2690 wxSize
wxDataViewChoiceRenderer::GetSize() const
2692 return wxSize(70,20);
2695 bool wxDataViewChoiceRenderer::SetValue( const wxVariant
&value
)
2699 if (!gtk_check_version(2,6,0))
2701 GValue gvalue
= { 0, };
2702 g_value_init( &gvalue
, G_TYPE_STRING
);
2703 g_value_set_string(&gvalue
,
2704 wxGTK_CONV_FONT(value
.GetString(),
2705 GetOwner()->GetOwner()->GetFont()));
2706 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2707 g_value_unset( &gvalue
);
2711 m_data
= value
.GetString();
2716 bool wxDataViewChoiceRenderer::GetValue( wxVariant
&value
) const
2719 if (!gtk_check_version(2,6,0))
2721 GValue gvalue
= { 0, };
2722 g_value_init( &gvalue
, G_TYPE_STRING
);
2723 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2724 wxString temp
= wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue
),
2725 GetOwner()->GetOwner()->GetFont());
2726 g_value_unset( &gvalue
);
2729 //wxPrintf( "temp %s\n", temp );
2730 // TODO: remove this code
2739 void wxDataViewChoiceRenderer::SetAlignment( int align
)
2741 wxDataViewCustomRenderer::SetAlignment(align
);
2743 if (gtk_check_version(2,10,0))
2746 // horizontal alignment:
2747 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2748 if (align
& wxALIGN_RIGHT
)
2749 pangoAlign
= PANGO_ALIGN_RIGHT
;
2750 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2751 pangoAlign
= PANGO_ALIGN_CENTER
;
2753 GValue gvalue
= { 0, };
2754 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2755 g_value_set_enum( &gvalue
, pangoAlign
);
2756 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2757 g_value_unset( &gvalue
);
2760 // ----------------------------------------------------------------------------
2761 // wxDataViewChoiceByIndexRenderer
2762 // ----------------------------------------------------------------------------
2764 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
2765 wxDataViewCellMode mode
, int alignment
) :
2766 wxDataViewChoiceRenderer( choices
, mode
, alignment
)
2770 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const char *itempath
, const wxString
& str
)
2772 wxVariant
value( (long) GetChoices().Index( str
) );
2774 if (!Validate( value
))
2778 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2780 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2783 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant
&value
)
2785 wxVariant string_value
= GetChoice( value
.GetLong() );
2786 return wxDataViewChoiceRenderer::SetValue( string_value
);
2789 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant
&value
) const
2791 wxVariant string_value
;
2792 if (!wxDataViewChoiceRenderer::GetValue( string_value
))
2795 value
= (long) GetChoices().Index( string_value
.GetString() );
2799 // ---------------------------------------------------------
2800 // wxDataViewIconTextRenderer
2801 // ---------------------------------------------------------
2803 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
2805 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2807 const wxString
&varianttype
,
2808 wxDataViewCellMode mode
,
2811 : wxDataViewTextRenderer(varianttype
, mode
, align
)
2813 m_rendererIcon
= gtk_cell_renderer_pixbuf_new();
2816 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2820 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
2822 // add the icon renderer first
2823 gtk_tree_view_column_pack_start(column
, m_rendererIcon
, FALSE
/* !expand */);
2825 // add the text renderer too
2826 wxDataViewRenderer::GtkPackIntoColumn(column
);
2829 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
2833 SetTextValue(m_value
.GetText());
2834 SetPixbufProp(m_rendererIcon
, m_value
.GetIcon().GetPixbuf());
2839 bool wxDataViewIconTextRenderer::GetValue(wxVariant
& value
) const
2842 if ( !GetTextValue(str
) )
2845 // user doesn't have any way to edit the icon so leave it unchanged
2846 value
<< wxDataViewIconText(str
, m_value
.GetIcon());
2852 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant
& value
,
2853 const wxDataViewItem
& item
,
2856 // we receive just the text part of our value as it's the only one which
2857 // can be edited, but we need the full wxDataViewIconText value for the
2859 wxVariant valueIconText
;
2860 valueIconText
<< wxDataViewIconText(value
.GetString(), m_value
.GetIcon());
2861 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText
, item
, col
);
2864 // ---------------------------------------------------------
2866 // ---------------------------------------------------------
2870 gtk_dataview_header_button_press_callback( GtkWidget
*WXUNUSED(widget
),
2871 GdkEventButton
*gdk_event
,
2872 wxDataViewColumn
*column
)
2874 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
2877 if (gdk_event
->button
== 1)
2879 gs_lastLeftClickHeader
= column
;
2881 wxDataViewCtrl
*dv
= column
->GetOwner();
2882 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2883 event
.SetDataViewColumn( column
);
2884 event
.SetModel( dv
->GetModel() );
2885 if (dv
->HandleWindowEvent( event
))
2889 if (gdk_event
->button
== 3)
2891 wxDataViewCtrl
*dv
= column
->GetOwner();
2892 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2893 event
.SetDataViewColumn( column
);
2894 event
.SetModel( dv
->GetModel() );
2895 if (dv
->HandleWindowEvent( event
))
2905 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*WXUNUSED(column
),
2906 GtkCellRenderer
*renderer
,
2907 GtkTreeModel
*model
,
2911 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2912 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2914 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2916 wxDataViewItem
item( (void*) iter
->user_data
);
2918 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2920 if (!wx_model
->IsVirtualListModel())
2923 if (wx_model
->IsContainer( item
))
2925 visible
= wx_model
->HasContainerColumns( item
) ||
2926 (cell
->GetOwner()->GetModelColumn() == 0);
2933 GValue gvalue
= { 0, };
2934 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2935 g_value_set_boolean( &gvalue
, visible
);
2936 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2937 g_value_unset( &gvalue
);
2944 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2946 if (value
.GetType() != cell
->GetVariantType())
2948 wxLogError( wxT("Wrong type, required: %s but: %s"),
2949 value
.GetType().c_str(),
2950 cell
->GetVariantType().c_str() );
2953 cell
->SetValue( value
);
2955 // deal with disabled items
2956 bool enabled
= wx_model
->IsEnabled( item
, cell
->GetOwner()->GetModelColumn() );
2958 // a) this sets the appearance to disabled grey
2959 GValue gvalue
= { 0, };
2960 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2961 g_value_set_boolean( &gvalue
, enabled
);
2962 g_object_set_property( G_OBJECT(renderer
), "sensitive", &gvalue
);
2963 g_value_unset( &gvalue
);
2965 // b) this actually disables the control/renderer
2967 cell
->SetMode( cell
->GtkGetMode() );
2969 cell
->SetMode( wxDATAVIEW_CELL_INERT
);
2972 // deal with attributes: if the renderer doesn't support them at all, we
2973 // don't even need to query the model for them
2974 if ( !cell
->GtkSupportsAttrs() )
2977 // it can support attributes so check if this item has any
2978 wxDataViewItemAttr attr
;
2979 if ( wx_model
->GetAttr( item
, cell
->GetOwner()->GetModelColumn(), attr
)
2980 || !cell
->GtkIsUsingDefaultAttrs() )
2982 bool usingDefaultAttrs
= !cell
->GtkSetAttr(attr
);
2983 cell
->GtkSetUsingDefaultAttrs(usingDefaultAttrs
);
2985 // else: no custom attributes specified and we're already using the default
2986 // ones -- nothing to do
2992 #include <wx/listimpl.cpp>
2993 WX_DEFINE_LIST(wxDataViewColumnList
)
2995 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
2996 unsigned int model_column
, int width
,
2997 wxAlignment align
, int flags
)
2998 : wxDataViewColumnBase( cell
, model_column
)
3000 Init( align
, flags
, width
);
3005 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
3006 unsigned int model_column
, int width
,
3007 wxAlignment align
, int flags
)
3008 : wxDataViewColumnBase( bitmap
, cell
, model_column
)
3010 Init( align
, flags
, width
);
3012 SetBitmap( bitmap
);
3015 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
3017 m_isConnected
= false;
3019 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
3020 m_column
= (GtkWidget
*) column
;
3023 SetAlignment( align
);
3027 // Create container for icon and label
3028 GtkWidget
*box
= gtk_hbox_new( FALSE
, 1 );
3029 gtk_widget_show( box
);
3030 // gtk_container_set_border_width((GtkContainer*)box, 2);
3031 m_image
= gtk_image_new();
3032 gtk_box_pack_start(GTK_BOX(box
), m_image
, FALSE
, FALSE
, 1);
3033 m_label
= gtk_label_new("");
3034 gtk_box_pack_end( GTK_BOX(box
), GTK_WIDGET(m_label
), FALSE
, FALSE
, 1 );
3035 gtk_tree_view_column_set_widget( column
, box
);
3037 wxDataViewRenderer
* const colRenderer
= GetRenderer();
3038 GtkCellRenderer
* const cellRenderer
= colRenderer
->GetGtkHandle();
3040 colRenderer
->GtkPackIntoColumn(column
);
3042 gtk_tree_view_column_set_cell_data_func( column
, cellRenderer
,
3043 wxGtkTreeCellDataFunc
, (gpointer
) colRenderer
, NULL
);
3046 void wxDataViewColumn::OnInternalIdle()
3051 if (gtk_widget_get_realized(GetOwner()->m_treeview
))
3053 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3056 g_signal_connect(column
->button
, "button_press_event",
3057 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
3059 // otherwise the event will be blocked by GTK+
3060 gtk_tree_view_column_set_clickable( column
, TRUE
);
3062 m_isConnected
= true;
3067 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
3069 wxDataViewColumnBase::SetOwner( owner
);
3071 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3073 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3076 void wxDataViewColumn::SetTitle( const wxString
&title
)
3078 wxDataViewCtrl
*ctrl
= GetOwner();
3079 gtk_label_set_text( GTK_LABEL(m_label
), ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
3080 : wxGTK_CONV_SYS(title
) );
3082 gtk_widget_hide( m_label
);
3084 gtk_widget_show( m_label
);
3087 wxString
wxDataViewColumn::GetTitle() const
3089 return wxGTK_CONV_BACK_FONT(
3090 gtk_label_get_text( GTK_LABEL(m_label
) ),
3091 GetOwner()->GetFont()
3095 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
3097 wxDataViewColumnBase::SetBitmap( bitmap
);
3101 GtkImage
*gtk_image
= GTK_IMAGE(m_image
);
3103 GdkBitmap
*mask
= NULL
;
3104 if (bitmap
.GetMask())
3105 mask
= bitmap
.GetMask()->GetBitmap();
3107 if (bitmap
.HasPixbuf())
3109 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
3110 bitmap
.GetPixbuf());
3114 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
3115 bitmap
.GetPixmap(), mask
);
3117 gtk_widget_show( m_image
);
3121 gtk_widget_hide( m_image
);
3125 void wxDataViewColumn::SetHidden( bool hidden
)
3127 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
3130 void wxDataViewColumn::SetResizeable( bool resizable
)
3132 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizable
);
3135 void wxDataViewColumn::SetAlignment( wxAlignment align
)
3137 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3139 gfloat xalign
= 0.0;
3140 if (align
== wxALIGN_RIGHT
)
3142 if (align
== wxALIGN_CENTER_HORIZONTAL
||
3143 align
== wxALIGN_CENTER
)
3146 gtk_tree_view_column_set_alignment( column
, xalign
);
3148 if (m_renderer
&& m_renderer
->GetAlignment() == -1)
3149 m_renderer
->GtkUpdateAlignment();
3152 wxAlignment
wxDataViewColumn::GetAlignment() const
3154 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
3157 return wxALIGN_RIGHT
;
3159 return wxALIGN_CENTER_HORIZONTAL
;
3161 return wxALIGN_LEFT
;
3164 void wxDataViewColumn::SetSortable( bool sortable
)
3166 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3170 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
3174 gtk_tree_view_column_set_sort_column_id( column
, -1 );
3175 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
3176 gtk_tree_view_column_set_clickable( column
, FALSE
);
3180 bool wxDataViewColumn::IsSortable() const
3182 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3183 return gtk_tree_view_column_get_clickable( column
);
3186 bool wxDataViewColumn::IsSortKey() const
3188 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3189 return gtk_tree_view_column_get_sort_indicator( column
);
3192 bool wxDataViewColumn::IsResizeable() const
3194 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3195 return gtk_tree_view_column_get_resizable( column
);
3198 bool wxDataViewColumn::IsHidden() const
3200 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3201 return !gtk_tree_view_column_get_visible( column
);
3204 void wxDataViewColumn::SetSortOrder( bool ascending
)
3206 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3209 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
3211 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
3213 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
3216 bool wxDataViewColumn::IsSortOrderAscending() const
3218 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3220 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
3223 void wxDataViewColumn::SetMinWidth( int width
)
3225 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3228 int wxDataViewColumn::GetMinWidth() const
3230 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3233 int wxDataViewColumn::GetWidth() const
3235 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3238 void wxDataViewColumn::SetWidth( int width
)
3240 if ( width
== wxCOL_WIDTH_AUTOSIZE
)
3242 // NB: this disables user resizing
3243 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
3247 if ( width
== wxCOL_WIDTH_DEFAULT
)
3249 // TODO find a better calculation
3250 width
= wxDVC_DEFAULT_WIDTH
;
3253 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_FIXED
);
3254 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3258 void wxDataViewColumn::SetReorderable( bool reorderable
)
3260 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column
), reorderable
);
3263 bool wxDataViewColumn::IsReorderable() const
3265 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column
) );
3268 //-----------------------------------------------------------------------------
3269 // wxGtkTreeModelNode
3270 //-----------------------------------------------------------------------------
3273 class wxGtkTreeModelChildWithPos
3281 int wxGtkTreeModelChildWithPosCmp( const void* data1
, const void* data2
, const void* user_data
)
3283 const wxGtkTreeModelChildWithPos
* child1
= (const wxGtkTreeModelChildWithPos
*) data1
;
3284 const wxGtkTreeModelChildWithPos
* child2
= (const wxGtkTreeModelChildWithPos
*) data2
;
3285 const wxDataViewCtrlInternal
*internal
= (const wxDataViewCtrlInternal
*) user_data
;
3286 int ret
= internal
->GetDataViewModel()->Compare( child1
->id
, child2
->id
,
3287 internal
->GetSortColumn(), (internal
->GetSortOrder() == GTK_SORT_DESCENDING
) );
3293 int LINKAGEMODE
wxGtkTreeModelChildPtrCmp( void*** data1
, void*** data2
)
3295 return gs_internal
->GetDataViewModel()->Compare( wxDataViewItem(**data1
), wxDataViewItem(**data2
),
3296 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
3299 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr
);
3302 void wxGtkTreeModelNode::Resort()
3304 size_t child_count
= GetChildCount();
3305 if (child_count
== 0)
3308 size_t node_count
= GetNodesCount();
3310 if (child_count
== 1)
3312 if (node_count
== 1)
3314 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
3320 gint
*new_order
= new gint
[child_count
];
3323 // m_children has the original *void
3324 // ptrs points to these
3325 wxGtkTreeModelChildrenPtr ptrs
;
3327 for (i
= 0; i
< child_count
; i
++)
3328 ptrs
.Add( &(m_children
[i
]) );
3330 gs_internal
= m_internal
;
3331 ptrs
.Sort( &wxGtkTreeModelChildPtrCmp
);
3333 wxGtkTreeModelChildren temp
;
3334 void** base_ptr
= &(m_children
[0]);
3335 // Transfer positions to new_order array and
3337 for (i
= 0; i
< child_count
; i
++)
3339 new_order
[i
] = ptrs
[i
] - base_ptr
;
3340 temp
.Add( *ptrs
[i
] );
3343 // Transfer IDs back to m_children
3345 WX_APPEND_ARRAY( temp
, m_children
);
3350 // Build up array with IDs and original positions
3351 wxGtkTreeModelChildWithPos
* temp
= new wxGtkTreeModelChildWithPos
[child_count
];
3353 for (i
= 0; i
< child_count
; i
++)
3356 temp
[i
].id
= m_children
[i
];
3358 // Sort array keeping original positions
3359 wxQsort( temp
, child_count
, sizeof(wxGtkTreeModelChildWithPos
),
3360 &wxGtkTreeModelChildWithPosCmp
, m_internal
);
3361 // Transfer positions to new_order array and
3362 // IDs to m_children
3364 for (i
= 0; i
< child_count
; i
++)
3366 new_order
[i
] = temp
[i
].pos
;
3367 m_children
.Add( temp
[i
].id
);
3376 wxGtkTreeModelChildren temp
;
3377 WX_APPEND_ARRAY( temp
, m_children
);
3379 gs_internal
= m_internal
;
3380 m_children
.Sort( &wxGtkTreeModelChildCmp
);
3383 for (pos
= 0; pos
< child_count
; pos
++)
3385 void *id
= m_children
.Item( pos
);
3386 int old_pos
= temp
.Index( id
);
3387 new_order
[pos
] = old_pos
;
3391 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
3394 iter
.user_data
= GetItem().GetID();
3395 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
3397 gtk_tree_model_rows_reordered( gtk_tree_model
,
3398 wxGtkTreePath(m_internal
->get_path(&iter
)), &iter
, new_order
);
3400 delete [] new_order
;
3403 for (pos
= 0; pos
< node_count
; pos
++)
3405 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
3410 //-----------------------------------------------------------------------------
3411 // wxDataViewCtrlInternal
3412 //-----------------------------------------------------------------------------
3414 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
)
3417 m_wx_model
= wx_model
;
3421 m_sort_order
= GTK_SORT_ASCENDING
;
3423 m_dataview_sort_column
= NULL
;
3425 m_dragDataObject
= NULL
;
3426 m_dropDataObject
= NULL
;
3430 m_gtk_model
= wxgtk_tree_model_new();
3431 m_gtk_model
->internal
= this;
3433 m_notifier
= new wxGtkDataViewModelNotifier( wx_model
, this );
3435 wx_model
->AddNotifier( m_notifier
);
3437 // g_object_unref( gtk_model ); ???
3439 if (!m_wx_model
->IsVirtualListModel())
3442 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner
->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model
) );
3445 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3447 m_wx_model
->RemoveNotifier( m_notifier
);
3449 // remove the model from the GtkTreeView before it gets destroyed
3450 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner
->GtkGetTreeView() ), NULL
);
3452 g_object_unref( m_gtk_model
);
3454 delete m_dragDataObject
;
3455 delete m_dropDataObject
;
3458 void wxDataViewCtrlInternal::ScheduleRefresh()
3463 void wxDataViewCtrlInternal::OnInternalIdle()
3467 GtkWidget
*widget
= m_owner
->GtkGetTreeView();
3468 gtk_widget_queue_draw( widget
);
3473 void wxDataViewCtrlInternal::InitTree()
3475 wxDataViewItem item
;
3476 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
3478 BuildBranch( m_root
);
3481 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
3483 if (node
->GetChildCount() == 0)
3485 wxDataViewItemArray children
;
3486 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
3489 for (pos
= 0; pos
< count
; pos
++)
3491 wxDataViewItem child
= children
[pos
];
3493 if (m_wx_model
->IsContainer( child
))
3494 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
3496 node
->AddLeaf( child
.GetID() );
3498 // Don't send any events here
3505 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat
&format
)
3507 wxGtkString
atom_str( gdk_atom_name( format
) );
3508 m_dragSourceTargetEntryTarget
= wxCharBuffer( atom_str
);
3510 m_dragSourceTargetEntry
.target
= m_dragSourceTargetEntryTarget
.data();
3511 m_dragSourceTargetEntry
.flags
= 0;
3512 m_dragSourceTargetEntry
.info
= static_cast<guint
>(-1);
3514 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3515 GDK_BUTTON1_MASK
, &m_dragSourceTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3520 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat
&format
)
3522 wxGtkString
atom_str( gdk_atom_name( format
) );
3523 m_dropTargetTargetEntryTarget
= wxCharBuffer( atom_str
);
3525 m_dropTargetTargetEntry
.target
= m_dropTargetTargetEntryTarget
.data();
3526 m_dropTargetTargetEntry
.flags
= 0;
3527 m_dropTargetTargetEntry
.info
= static_cast<guint
>(-1);
3529 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3530 &m_dropTargetTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3535 gboolean
wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource
*WXUNUSED(drag_source
),
3538 delete m_dragDataObject
;
3539 m_dragDataObject
= NULL
;
3541 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3545 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3546 event
.SetEventObject( m_owner
);
3547 event
.SetItem( item
);
3548 event
.SetModel( m_wx_model
);
3550 gtk_widget_get_pointer(m_owner
->GtkGetTreeView(), &x
, &y
);
3551 event
.SetPosition(x
, y
);
3552 if (!m_owner
->HandleWindowEvent( event
))
3555 if (!event
.IsAllowed())
3558 wxDataObject
*obj
= event
.GetDataObject();
3562 m_dragDataObject
= obj
;
3568 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource
*WXUNUSED(drag_source
),
3569 GtkTreePath
*WXUNUSED(path
))
3574 gboolean
wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource
*WXUNUSED(drag_source
),
3575 GtkTreePath
*path
, GtkSelectionData
*selection_data
)
3577 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3581 GdkAtom target
= gtk_selection_data_get_target(selection_data
);
3582 if (!m_dragDataObject
->IsSupported(target
))
3585 size_t size
= m_dragDataObject
->GetDataSize(target
);
3589 void *buf
= malloc( size
);
3591 gboolean res
= FALSE
;
3592 if (m_dragDataObject
->GetDataHere(target
, buf
))
3596 gtk_selection_data_set(selection_data
, target
,
3597 8, (const guchar
*) buf
, size
);
3606 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3608 GtkSelectionData
*selection_data
)
3610 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3614 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
3615 event
.SetEventObject( m_owner
);
3616 event
.SetItem( item
);
3617 event
.SetModel( m_wx_model
);
3618 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3619 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3620 event
.SetDataBuffer(const_cast<guchar
*>(gtk_selection_data_get_data(selection_data
)));
3621 if (!m_owner
->HandleWindowEvent( event
))
3624 if (!event
.IsAllowed())
3631 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3633 GtkSelectionData
*selection_data
)
3635 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3639 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
3640 event
.SetEventObject( m_owner
);
3641 event
.SetItem( item
);
3642 event
.SetModel( m_wx_model
);
3643 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3644 if (!m_owner
->HandleWindowEvent( event
))
3647 if (!event
.IsAllowed())
3653 // notifications from wxDataViewModel
3655 bool wxDataViewCtrlInternal::Cleared()
3670 void wxDataViewCtrlInternal::Resort()
3672 if (!m_wx_model
->IsVirtualListModel())
3678 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3680 if (!m_wx_model
->IsVirtualListModel())
3682 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3683 wxCHECK_MSG(parent_node
, false,
3684 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3686 wxDataViewItemArray siblings
;
3687 m_wx_model
->GetChildren(parent
, siblings
);
3688 int itemPos
= siblings
.Index(item
, /*fromEnd=*/true);
3689 wxCHECK_MSG( itemPos
!= wxNOT_FOUND
, false, "adding non-existent item?" );
3691 if (m_wx_model
->IsContainer( item
))
3692 parent_node
->InsertNode( new wxGtkTreeModelNode( parent_node
, item
, this ), itemPos
);
3694 parent_node
->InsertLeaf( item
.GetID(), itemPos
);
3702 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3704 if (!m_wx_model
->IsVirtualListModel())
3706 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3707 wxASSERT_MSG(parent_node
,
3708 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3710 parent_node
->DeleteChild( item
.GetID() );
3718 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
3720 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3721 event
.SetEventObject( m_owner
);
3722 event
.SetModel( m_owner
->GetModel() );
3723 event
.SetItem( item
);
3724 m_owner
->HandleWindowEvent( event
);
3729 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int view_column
)
3731 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
3732 event
.SetEventObject( m_owner
);
3733 event
.SetModel( m_owner
->GetModel() );
3734 event
.SetColumn( view_column
);
3735 event
.SetDataViewColumn( GetOwner()->GetColumn(view_column
) );
3736 event
.SetItem( item
);
3737 m_owner
->HandleWindowEvent( event
);
3744 GtkTreeModelFlags
wxDataViewCtrlInternal::get_flags()
3748 if ( m_wx_model
->IsListModel() )
3749 flags
|= GTK_TREE_MODEL_LIST_ONLY
;
3751 if ( !m_wx_model
->IsVirtualListModel() )
3752 flags
|= GTK_TREE_MODEL_ITERS_PERSIST
;
3754 return GtkTreeModelFlags(flags
);
3757 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
3760 if (m_wx_model
->IsVirtualListModel())
3762 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3764 unsigned int i
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
3766 if (i
>= wx_model
->GetCount())
3769 iter
->stamp
= m_gtk_model
->stamp
;
3770 // user_data is just the index +1
3771 iter
->user_data
= (gpointer
) (i
+1);
3777 int depth
= gtk_tree_path_get_depth( path
);
3779 wxGtkTreeModelNode
*node
= m_root
;
3782 for (i
= 0; i
< depth
; i
++)
3784 BuildBranch( node
);
3786 gint pos
= gtk_tree_path_get_indices (path
)[i
];
3787 if (pos
< 0) return FALSE
;
3788 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
3790 void* id
= node
->GetChildren().Item( (size_t) pos
);
3794 iter
->stamp
= m_gtk_model
->stamp
;
3795 iter
->user_data
= id
;
3799 size_t count
= node
->GetNodes().GetCount();
3801 for (pos2
= 0; pos2
< count
; pos2
++)
3803 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
3804 if (child_node
->GetItem().GetID() == id
)
3816 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
3818 // When this is called from ItemDeleted(), the item is already
3819 // deleted in the model.
3821 GtkTreePath
*retval
= gtk_tree_path_new ();
3823 if (m_wx_model
->IsVirtualListModel())
3825 // iter is root, add nothing
3826 if (!iter
->user_data
)
3829 // user_data is just the index +1
3830 int i
= ( (wxUIntPtr
) iter
->user_data
) -1;
3831 gtk_tree_path_append_index (retval
, i
);
3835 void *id
= iter
->user_data
;
3837 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
3840 int pos
= node
->GetChildren().Index( id
);
3842 gtk_tree_path_prepend_index( retval
, pos
);
3844 id
= node
->GetItem().GetID();
3845 node
= node
->GetParent();
3852 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
3854 if (m_wx_model
->IsVirtualListModel())
3856 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3858 // user_data is just the index +1
3859 int n
= ( (wxUIntPtr
) iter
->user_data
) -1;
3863 iter
->user_data
= NULL
;
3867 if (n
>= (int) wx_model
->GetCount()-1)
3869 iter
->user_data
= NULL
;
3873 // user_data is just the index +1 (+2 because we need the next)
3874 iter
->user_data
= (gpointer
) (n
+2);
3878 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
3879 if( parent
== NULL
)
3881 iter
->user_data
= NULL
;
3885 int pos
= parent
->GetChildren().Index( iter
->user_data
);
3887 if (pos
== (int) parent
->GetChildCount()-1)
3889 iter
->user_data
= NULL
;
3893 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
3899 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
3901 if (m_wx_model
->IsVirtualListModel())
3903 // this is a list, nodes have no children
3907 iter
->stamp
= m_gtk_model
->stamp
;
3908 iter
->user_data
= (gpointer
) 1;
3916 if (m_root
->GetChildCount() == 0) return FALSE
;
3917 iter
->stamp
= m_gtk_model
->stamp
;
3918 iter
->user_data
= (gpointer
) m_root
->GetChildren().Item( 0 );
3923 wxDataViewItem item
;
3925 item
= wxDataViewItem( (void*) parent
->user_data
);
3927 if (!m_wx_model
->IsContainer( item
))
3930 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3931 wxASSERT_MSG(parent_node
,
3932 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3934 BuildBranch( parent_node
);
3936 if (parent_node
->GetChildCount() == 0)
3939 iter
->stamp
= m_gtk_model
->stamp
;
3940 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
3946 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
3948 if (m_wx_model
->IsVirtualListModel())
3950 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3953 return (wx_model
->GetCount() > 0);
3955 // this is a list, nodes have no children
3961 return (m_root
->GetChildCount() > 0);
3963 wxDataViewItem
item( (void*) iter
->user_data
);
3965 bool is_container
= m_wx_model
->IsContainer( item
);
3970 wxGtkTreeModelNode
*node
= FindNode( iter
);
3972 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3974 BuildBranch( node
);
3976 return (node
->GetChildCount() > 0);
3980 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
3982 if (m_wx_model
->IsVirtualListModel())
3984 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
3987 return (gint
) wx_model
->GetCount();
3994 return m_root
->GetChildCount();
3996 wxDataViewItem
item( (void*) iter
->user_data
);
3998 if (!m_wx_model
->IsContainer( item
))
4001 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
4002 wxASSERT_MSG(parent_node
,
4003 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4005 BuildBranch( parent_node
);
4007 return parent_node
->GetChildCount();
4011 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
4013 if (m_wx_model
->IsVirtualListModel())
4015 wxDataViewVirtualListModel
*wx_model
= (wxDataViewVirtualListModel
*) m_wx_model
;
4023 if (n
>= (gint
) wx_model
->GetCount())
4026 iter
->stamp
= m_gtk_model
->stamp
;
4027 // user_data is just the index +1
4028 iter
->user_data
= (gpointer
) (n
+1);
4035 if (parent
) id
= (void*) parent
->user_data
;
4036 wxDataViewItem
item( id
);
4038 if (!m_wx_model
->IsContainer( item
))
4041 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4042 wxASSERT_MSG(parent_node
,
4043 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4045 BuildBranch( parent_node
);
4047 iter
->stamp
= m_gtk_model
->stamp
;
4048 iter
->user_data
= parent_node
->GetChildren().Item( n
);
4054 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
4056 if (m_wx_model
->IsVirtualListModel())
4062 wxGtkTreeModelNode
*node
= FindParentNode( child
);
4066 iter
->stamp
= m_gtk_model
->stamp
;
4067 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
4073 // item can be deleted already in the model
4074 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
4076 if (m_wx_model
->IsVirtualListModel())
4078 return wxPtrToUInt(item
.GetID()) - 1;
4082 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
4083 wxGtkTreeModelChildren
&children
= parent_node
->GetChildren();
4085 for (j
= 0; j
< children
.GetCount(); j
++)
4087 if (children
[j
] == item
.GetID())
4095 static wxGtkTreeModelNode
*
4096 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4102 list
.DeleteContents( true );
4103 wxDataViewItem
it( item
);
4107 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4108 list
.Insert( pItem
);
4109 it
= model
->GetParent( it
);
4112 wxGtkTreeModelNode
* node
= treeNode
;
4113 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4115 if( node
&& node
->GetNodes().GetCount() != 0 )
4117 int len
= node
->GetNodes().GetCount();
4118 wxGtkTreeModelNodes
&nodes
= node
->GetNodes();
4120 for( ; j
< len
; j
++)
4122 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4141 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
4146 wxDataViewItem
item( (void*) iter
->user_data
);
4150 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4155 wxLogDebug( "Not found %p", iter->user_data );
4159 // TODO: remove this code
4165 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
4170 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
4175 wxLogDebug( "Not found %p", item.GetID() );
4179 // TODO: remove this code
4185 static wxGtkTreeModelNode
*
4186 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
4192 list
.DeleteContents( true );
4196 wxDataViewItem
it( model
->GetParent( item
) );
4199 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
4200 list
.Insert( pItem
);
4201 it
= model
->GetParent( it
);
4204 wxGtkTreeModelNode
* node
= treeNode
;
4205 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
4207 if( node
&& node
->GetNodes().GetCount() != 0 )
4209 int len
= node
->GetNodes().GetCount();
4210 wxGtkTreeModelNodes nodes
= node
->GetNodes();
4212 for( ; j
< len
; j
++)
4214 if( nodes
[j
]->GetItem() == *(n
->GetData()))
4229 //Examine whether the node is item's parent node
4230 int len
= node
->GetChildCount();
4231 for( int i
= 0; i
< len
; i
++ )
4233 if( node
->GetChildren().Item( i
) == item
.GetID() )
4239 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
4244 wxDataViewItem
item( (void*) iter
->user_data
);
4248 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4251 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
4256 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
4259 //-----------------------------------------------------------------------------
4260 // wxDataViewCtrl signal callbacks
4261 //-----------------------------------------------------------------------------
4264 wxdataview_selection_changed_callback( GtkTreeSelection
* WXUNUSED(selection
), wxDataViewCtrl
*dv
)
4266 if (!gtk_widget_get_realized(dv
->m_widget
))
4269 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, dv
->GetId() );
4270 event
.SetItem( dv
->GetSelection() );
4271 event
.SetModel( dv
->GetModel() );
4272 dv
->HandleWindowEvent( event
);
4276 wxdataview_row_activated_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreePath
*path
,
4277 GtkTreeViewColumn
*WXUNUSED(column
), wxDataViewCtrl
*dv
)
4279 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
4281 wxDataViewItem
item(dv
->GTKPathToItem(path
));
4282 event
.SetItem( item
);
4283 event
.SetModel( dv
->GetModel() );
4284 dv
->HandleWindowEvent( event
);
4288 wxdataview_test_expand_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4289 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4291 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, dv
->GetId() );
4293 wxDataViewItem
item( (void*) iter
->user_data
);;
4294 event
.SetItem( item
);
4295 event
.SetModel( dv
->GetModel() );
4296 dv
->HandleWindowEvent( event
);
4298 return !event
.IsAllowed();
4302 wxdataview_row_expanded_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4303 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4305 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
, dv
->GetId() );
4307 wxDataViewItem
item( (void*) iter
->user_data
);;
4308 event
.SetItem( item
);
4309 event
.SetModel( dv
->GetModel() );
4310 dv
->HandleWindowEvent( event
);
4314 wxdataview_test_collapse_row_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4315 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4317 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
, dv
->GetId() );
4319 wxDataViewItem
item( (void*) iter
->user_data
);;
4320 event
.SetItem( item
);
4321 event
.SetModel( dv
->GetModel() );
4322 dv
->HandleWindowEvent( event
);
4324 return !event
.IsAllowed();
4328 wxdataview_row_collapsed_callback( GtkTreeView
* WXUNUSED(treeview
), GtkTreeIter
* iter
,
4329 GtkTreePath
*WXUNUSED(path
), wxDataViewCtrl
*dv
)
4331 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
, dv
->GetId() );
4333 wxDataViewItem
item( (void*) iter
->user_data
);;
4334 event
.SetItem( item
);
4335 event
.SetModel( dv
->GetModel() );
4336 dv
->HandleWindowEvent( event
);
4339 //-----------------------------------------------------------------------------
4341 //-----------------------------------------------------------------------------
4343 void wxDataViewCtrl::AddChildGTK(wxWindowGTK
* child
)
4345 GtkWidget
* treeview
= GtkGetTreeView();
4347 // Insert widget in GtkTreeView
4348 if (gtk_widget_get_realized(treeview
))
4349 gtk_widget_set_parent_window( child
->m_widget
,
4350 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
4351 gtk_widget_set_parent( child
->m_widget
, treeview
);
4355 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
4356 GtkAllocation
*WXUNUSED(gtk_alloc
),
4357 wxDataViewCtrl
*win
)
4359 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
4362 wxWindow
*child
= node
->GetData();
4365 gtk_widget_size_request( child
->m_widget
, &req
);
4367 GtkAllocation alloc
;
4368 alloc
.x
= child
->m_x
;
4369 alloc
.y
= child
->m_y
;
4370 alloc
.width
= child
->m_width
;
4371 alloc
.height
= child
->m_height
;
4372 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
4374 node
= node
->GetNext();
4379 //-----------------------------------------------------------------------------
4380 // "motion_notify_event"
4381 //-----------------------------------------------------------------------------
4384 gtk_dataview_motion_notify_callback( GtkWidget
*WXUNUSED(widget
),
4385 GdkEventMotion
*gdk_event
,
4386 wxDataViewCtrl
*dv
)
4388 if (gdk_event
->is_hint
)
4392 GdkModifierType state
;
4393 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
4399 GtkTreeViewColumn
*column
= NULL
;
4402 if (gtk_tree_view_get_path_at_pos(
4403 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4404 (int) gdk_event
->x
, (int) gdk_event
->y
,
4413 dv
->GtkGetInternal()->get_iter( &iter
, path
);
4421 //-----------------------------------------------------------------------------
4422 // "button_press_event"
4423 //-----------------------------------------------------------------------------
4426 gtk_dataview_button_press_callback( GtkWidget
*WXUNUSED(widget
),
4427 GdkEventButton
*gdk_event
,
4428 wxDataViewCtrl
*dv
)
4430 if ((gdk_event
->button
== 3) && (gdk_event
->type
== GDK_BUTTON_PRESS
))
4433 GtkTreeViewColumn
*column
= NULL
;
4436 gtk_tree_view_get_path_at_pos
4438 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4439 (int) gdk_event
->x
, (int) gdk_event
->y
,
4446 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, dv
->GetId() );
4448 event
.SetItem(dv
->GTKPathToItem(path
));
4449 event
.SetModel( dv
->GetModel() );
4450 return dv
->HandleWindowEvent( event
);
4456 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4458 wxDataViewCtrl::~wxDataViewCtrl()
4460 // Stop editing before destroying the control to remove any event handlers
4461 // which are added when editing started: if we didn't do this, the base
4462 // class dtor would assert as it checks for any leftover handlers.
4465 GtkTreeViewColumn
*col
;
4466 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4468 wxDataViewColumn
* const wxcol
= FromGTKColumn(col
);
4471 // This won't do anything if we're not editing it
4472 wxcol
->GetRenderer()->CancelEditing();
4481 void wxDataViewCtrl::Init()
4485 m_cols
.DeleteContents( true );
4487 m_uniformRowHeight
= -1;
4490 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4495 const wxValidator
& validator
,
4496 const wxString
& name
)
4498 if (!PreCreation( parent
, pos
, size
) ||
4499 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
4501 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4505 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
4506 g_object_ref(m_widget
);
4508 GTKScrolledWindowSetBorder(m_widget
, style
);
4510 m_treeview
= gtk_tree_view_new();
4511 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
4513 m_focusWidget
= GTK_WIDGET(m_treeview
);
4515 g_signal_connect (m_treeview
, "size_allocate",
4516 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
4519 if (!gtk_check_version(2,6,0))
4521 bool fixed
= (style
& wxDV_VARIABLE_LINE_HEIGHT
) == 0;
4522 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4526 if (style
& wxDV_MULTIPLE
)
4528 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4529 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
4532 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
4535 if (!gtk_check_version(2,10,0))
4537 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
4539 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
4540 (style
& wxDV_VERT_RULES
) != 0)
4541 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
4542 else if (style
& wxDV_VERT_RULES
)
4543 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
4544 else if (style
& wxDV_HORIZ_RULES
)
4545 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
4547 if (grid
!= GTK_TREE_VIEW_GRID_LINES_NONE
)
4548 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
4552 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_ROW_LINES
) != 0 );
4554 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
4555 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
4556 gtk_widget_show (m_treeview
);
4558 m_parent
->DoAddChild( this );
4562 GtkEnableSelectionEvents();
4564 g_signal_connect_after (m_treeview
, "row-activated",
4565 G_CALLBACK (wxdataview_row_activated_callback
), this);
4567 g_signal_connect (m_treeview
, "test-collapse-row",
4568 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
4570 g_signal_connect_after (m_treeview
, "row-collapsed",
4571 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
4573 g_signal_connect (m_treeview
, "test-expand-row",
4574 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
4576 g_signal_connect_after (m_treeview
, "row-expanded",
4577 G_CALLBACK (wxdataview_row_expanded_callback
), this);
4579 g_signal_connect (m_treeview
, "motion_notify_event",
4580 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
4582 g_signal_connect (m_treeview
, "button_press_event",
4583 G_CALLBACK (gtk_dataview_button_press_callback
), this);
4588 wxDataViewItem
wxDataViewCtrl::GTKPathToItem(GtkTreePath
*path
) const
4591 return wxDataViewItem(path
&& m_internal
->get_iter(&iter
, path
)
4596 void wxDataViewCtrl::OnInternalIdle()
4598 wxWindow::OnInternalIdle();
4600 m_internal
->OnInternalIdle();
4602 unsigned int cols
= GetColumnCount();
4604 for (i
= 0; i
< cols
; i
++)
4606 wxDataViewColumn
*col
= GetColumn( i
);
4607 col
->OnInternalIdle();
4610 if (m_ensureVisibleDefered
.IsOk())
4612 ExpandAncestors(m_ensureVisibleDefered
);
4614 iter
.user_data
= (gpointer
) m_ensureVisibleDefered
.GetID();
4615 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4616 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
4617 m_ensureVisibleDefered
= wxDataViewItem(0);
4621 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4623 wxDELETE(m_internal
);
4625 if (!wxDataViewCtrlBase::AssociateModel( model
))
4629 if (!gtk_check_version(2,6,0))
4631 bool fixed
= (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
) == 0) || (model
->IsVirtualListModel()));
4632 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4636 m_internal
= new wxDataViewCtrlInternal( this, model
);
4641 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4643 return m_internal
->EnableDragSource( format
);
4646 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4648 return m_internal
->EnableDropTarget( format
);
4651 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4653 if (!wxDataViewCtrlBase::AppendColumn(col
))
4656 m_cols
.Append( col
);
4659 if (!gtk_check_version(2,6,0))
4661 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4662 GTK_TREE_VIEW_COLUMN_FIXED
)
4663 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4667 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
4668 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4673 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4675 if (!wxDataViewCtrlBase::PrependColumn(col
))
4678 m_cols
.Insert( col
);
4681 if (!gtk_check_version(2,6,0))
4683 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4684 GTK_TREE_VIEW_COLUMN_FIXED
)
4685 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4689 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4690 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
4695 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4697 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4700 m_cols
.Insert( pos
, col
);
4703 if (!gtk_check_version(2,6,0))
4705 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4706 GTK_TREE_VIEW_COLUMN_FIXED
)
4707 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4711 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4712 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), pos
);
4717 unsigned int wxDataViewCtrl::GetColumnCount() const
4719 return m_cols
.GetCount();
4722 wxDataViewColumn
* wxDataViewCtrl::FromGTKColumn(GtkTreeViewColumn
*gtk_col
) const
4727 wxDataViewColumnList::const_iterator iter
;
4728 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4730 wxDataViewColumn
*col
= *iter
;
4731 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
4737 wxFAIL_MSG( "No matching column?" );
4742 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
4744 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
4746 return FromGTKColumn(gtk_col
);
4749 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4751 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4752 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
4754 m_cols
.DeleteObject( column
);
4759 bool wxDataViewCtrl::ClearColumns()
4761 wxDataViewColumnList::iterator iter
;
4762 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4764 wxDataViewColumn
*col
= *iter
;
4765 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4766 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4774 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4776 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4778 wxGtkList
list(gtk_tree_view_get_columns(GTK_TREE_VIEW(m_treeview
)));
4780 return g_list_index( list
, (gconstpointer
) gtk_column
);
4783 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4785 return m_internal
->GetDataViewSortColumn();
4788 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4791 iter
.user_data
= item
.GetID();
4792 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4793 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
4796 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4799 iter
.user_data
= item
.GetID();
4800 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4801 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
4804 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4807 iter
.user_data
= item
.GetID();
4808 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4809 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview
), path
);
4812 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4814 // The tree doesn't have any current item if it hadn't been created yet but
4815 // it's arguably not an error to call this function in this case so just
4816 // return an invalid item without asserting.
4818 return wxDataViewItem();
4821 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), path
.ByRef(), NULL
);
4823 return GTKPathToItem(path
);
4826 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4828 wxCHECK_RET( m_treeview
,
4829 "Current item can't be set before creating the control." );
4831 // We need to make sure the model knows about this item or the path would
4832 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4833 ExpandAncestors(item
);
4835 // We also need to preserve the existing selection from changing.
4836 // Unfortunately the only way to do it seems to use our own selection
4837 // function and forbid any selection changes during set cursor call.
4838 wxGtkTreeSelectionLock
4839 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4841 // Do move the cursor now.
4843 iter
.user_data
= item
.GetID();
4844 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4846 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, NULL
, FALSE
);
4849 void wxDataViewCtrl::StartEditor(const wxDataViewItem
& item
, unsigned int column
)
4851 wxCHECK_RET( m_treeview
,
4852 "Current item can't be set before creating the control." );
4854 // We need to make sure the model knows about this item or the path would
4855 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4856 ExpandAncestors(item
);
4858 wxDataViewColumn
*dvcolumn
= GetColumn(column
);
4859 wxASSERT_MSG(dvcolumn
, "Could not retrieve column");
4860 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(dvcolumn
->GetGtkHandle());
4862 // We also need to preserve the existing selection from changing.
4863 // Unfortunately the only way to do it seems to use our own selection
4864 // function and forbid any selection changes during set cursor call.
4865 wxGtkTreeSelectionLock
4866 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4868 // Do move the cursor now.
4870 iter
.user_data
= item
.GetID();
4871 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4873 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, gcolumn
, TRUE
);
4876 int wxDataViewCtrl::GetSelectedItemsCount() const
4878 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4880 return gtk_tree_selection_count_selected_rows(selection
);
4883 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4887 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4888 if (HasFlag(wxDV_MULTIPLE
))
4890 GtkTreeModel
*model
;
4891 wxGtkTreePathList
list(gtk_tree_selection_get_selected_rows(selection
, &model
));
4893 for ( GList
* current
= list
; current
; current
= g_list_next(current
) )
4895 GtkTreePath
*path
= (GtkTreePath
*) current
->data
;
4897 sel
.Add(GTKPathToItem(path
));
4903 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
4905 sel
.Add( wxDataViewItem(iter
.user_data
) );
4912 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4914 GtkDisableSelectionEvents();
4916 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4918 gtk_tree_selection_unselect_all( selection
);
4920 wxDataViewItem last_parent
;
4923 for (i
= 0; i
< sel
.GetCount(); i
++)
4925 wxDataViewItem item
= sel
[i
];
4926 wxDataViewItem parent
= GetModel()->GetParent( item
);
4929 if (parent
!= last_parent
)
4930 ExpandAncestors(item
);
4932 last_parent
= parent
;
4935 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4936 iter
.user_data
= (gpointer
) item
.GetID();
4937 gtk_tree_selection_select_iter( selection
, &iter
);
4940 GtkEnableSelectionEvents();
4943 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4945 ExpandAncestors(item
);
4947 GtkDisableSelectionEvents();
4949 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4952 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4953 iter
.user_data
= (gpointer
) item
.GetID();
4954 gtk_tree_selection_select_iter( selection
, &iter
);
4956 GtkEnableSelectionEvents();
4959 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4961 GtkDisableSelectionEvents();
4963 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4966 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4967 iter
.user_data
= (gpointer
) item
.GetID();
4968 gtk_tree_selection_unselect_iter( selection
, &iter
);
4970 GtkEnableSelectionEvents();
4973 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
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();
4981 return gtk_tree_selection_iter_is_selected( selection
, &iter
);
4984 void wxDataViewCtrl::SelectAll()
4986 GtkDisableSelectionEvents();
4988 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4990 gtk_tree_selection_select_all( selection
);
4992 GtkEnableSelectionEvents();
4995 void wxDataViewCtrl::UnselectAll()
4997 GtkDisableSelectionEvents();
4999 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5001 gtk_tree_selection_unselect_all( selection
);
5003 GtkEnableSelectionEvents();
5006 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem
& item
,
5007 const wxDataViewColumn
*WXUNUSED(column
))
5009 m_ensureVisibleDefered
= item
;
5010 ExpandAncestors(item
);
5013 iter
.user_data
= (gpointer
) item
.GetID();
5014 wxGtkTreePath
path(m_internal
->get_path( &iter
));
5015 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
5018 void wxDataViewCtrl::HitTest(const wxPoint
& point
,
5019 wxDataViewItem
& item
,
5020 wxDataViewColumn
*& column
) const
5022 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
5023 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
5024 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
5025 // So we have to use both of them.
5026 // Friedrich Haase 2010-9-20
5027 wxGtkTreePath path
, pathScratch
;
5028 GtkTreeViewColumn
* GtkColumn
= NULL
;
5029 GtkTreeViewDropPosition pos
= GTK_TREE_VIEW_DROP_INTO_OR_AFTER
;
5033 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5034 wxDataViewCtrl
* ctrl
= (wxDataViewCtrl
*)this; // ugly workaround, ctrl is NOT const
5035 GtkTreeView
* treeView
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
5037 // is there possibly a better suited function to get the column?
5038 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5040 (int) point
.x
, (int) point
.y
,
5041 pathScratch
.ByRef(),
5042 &GtkColumn
, // here we get the GtkColumn
5046 if ( GtkColumn
!= NULL
)
5048 // we got GTK column
5049 // the right call now which takes the header into account
5050 gtk_tree_view_get_dest_row_at_pos( treeView
, (int) point
.x
, (int) point
.y
, path
.ByRef(), &pos
);
5053 item
= wxDataViewItem(GTKPathToItem(path
));
5054 // else we got a GTK column but the position is not over an item, e.g. below last item
5055 for ( unsigned int i
=0, cols
=GetColumnCount(); i
<cols
; ++i
) // search the wx column
5057 wxDataViewColumn
* col
= GetColumn(i
);
5058 if ( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == GtkColumn
)
5060 column
= col
; // here we get the wx column
5065 // else no column and thus no item, both null
5069 wxDataViewCtrl::GetItemRect(const wxDataViewItem
& WXUNUSED(item
),
5070 const wxDataViewColumn
*WXUNUSED(column
)) const
5075 bool wxDataViewCtrl::SetRowHeight(int rowHeight
)
5077 m_uniformRowHeight
= rowHeight
;
5081 void wxDataViewCtrl::DoSetExpanderColumn()
5083 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
5084 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5087 void wxDataViewCtrl::DoSetIndent()
5091 void wxDataViewCtrl::GtkDisableSelectionEvents()
5093 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5094 g_signal_handlers_disconnect_by_func( selection
,
5095 (gpointer
) (wxdataview_selection_changed_callback
), this);
5098 void wxDataViewCtrl::GtkEnableSelectionEvents()
5100 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5101 g_signal_connect_after (selection
, "changed",
5102 G_CALLBACK (wxdataview_selection_changed_callback
), this);
5105 // ----------------------------------------------------------------------------
5106 // visual attributes stuff
5107 // ----------------------------------------------------------------------------
5111 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
5113 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
5116 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
5118 wxDataViewCtrlBase::DoApplyWidgetStyle(style
);
5119 gtk_widget_modify_style(m_treeview
, style
);
5122 #endif // !wxUSE_GENERICDATAVIEWCTRL
5124 #endif // wxUSE_DATAVIEWCTRL