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/calctrl.h"
29 #include "wx/popupwin.h"
30 #include "wx/listimpl.cpp"
32 #include "wx/gtk/private.h"
33 #include "wx/gtk/dc.h"
34 #include "wx/gtk/dcclient.h"
36 #include "wx/gtk/private/gdkconv.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 // wxGtkTreeSelectionLock: prevent selection from changing during the
103 // lifetime of this object
104 // ----------------------------------------------------------------------------
106 // Implementation note: it could be expected that setting the selection
107 // function in this class ctor and resetting it back to the old value in its
108 // dtor would work. However currently gtk_tree_selection_get_select_function()
109 // can't be passed NULL (see https://bugzilla.gnome.org/show_bug.cgi?id=626276)
110 // so we can't do this. Instead, we always use the selection function (which
111 // imposes extra overhead, albeit minimal one, on all selection operations) and
112 // just set/reset the flag telling it whether it should allow or forbid the
115 // Also notice that currently only a single object of this class may exist at
116 // any given moment. It's just simpler like this and we don't need anything
120 gboolean
wxdataview_selection_func(GtkTreeSelection
* WXUNUSED(selection
),
121 GtkTreeModel
* WXUNUSED(model
),
122 GtkTreePath
* WXUNUSED(path
),
123 gboolean
WXUNUSED(path_currently_selected
),
129 class wxGtkTreeSelectionLock
132 wxGtkTreeSelectionLock(GtkTreeSelection
*selection
)
133 : m_selection(selection
)
135 wxASSERT_MSG( !ms_instance
, "this class is not reentrant currently" );
139 CheckCurrentSelectionFunc(NULL
);
141 // Pass some non-NULL pointer as "data" for the callback, it doesn't
142 // matter what it is as long as it's non-NULL.
143 gtk_tree_selection_set_select_function(selection
,
144 wxdataview_selection_func
,
149 ~wxGtkTreeSelectionLock()
151 CheckCurrentSelectionFunc(wxdataview_selection_func
);
153 gtk_tree_selection_set_select_function(m_selection
,
154 wxdataview_selection_func
,
162 void CheckCurrentSelectionFunc(GtkTreeSelectionFunc func
)
164 // We can only use gtk_tree_selection_get_select_function() with 2.14+
165 // so check for its availability both during compile- and run-time.
166 #if GTK_CHECK_VERSION(2, 14, 0)
167 if ( gtk_check_version(2, 14, 0) != NULL
)
170 // If this assert is triggered, it means the code elsewhere has called
171 // gtk_tree_selection_set_select_function() but currently doing this
172 // breaks this class so the code here needs to be changed.
175 gtk_tree_selection_get_select_function(m_selection
) == func
,
176 "selection function has changed unexpectedly, review this code!"
183 static wxGtkTreeSelectionLock
*ms_instance
;
185 GtkTreeSelection
* const m_selection
;
187 wxDECLARE_NO_COPY_CLASS(wxGtkTreeSelectionLock
);
190 wxGtkTreeSelectionLock
*wxGtkTreeSelectionLock::ms_instance
= NULL
;
192 //-----------------------------------------------------------------------------
193 // wxDataViewCtrlInternal
194 //-----------------------------------------------------------------------------
196 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
197 WX_DEFINE_LIST(ItemList
)
199 class wxDataViewCtrlInternal
202 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
);
203 ~wxDataViewCtrlInternal();
206 GtkTreeModelFlags
get_flags();
207 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
208 GtkTreePath
*get_path( GtkTreeIter
*iter
);
209 gboolean
iter_next( GtkTreeIter
*iter
);
210 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
211 gboolean
iter_has_child( GtkTreeIter
*iter
);
212 gint
iter_n_children( GtkTreeIter
*iter
);
213 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
214 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
218 bool EnableDragSource( const wxDataFormat
&format
);
219 bool EnableDropTarget( const wxDataFormat
&format
);
221 gboolean
row_draggable( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
);
222 gboolean
drag_data_delete( GtkTreeDragSource
*drag_source
, GtkTreePath
* path
);
223 gboolean
drag_data_get( GtkTreeDragSource
*drag_source
, GtkTreePath
*path
,
224 GtkSelectionData
*selection_data
);
225 gboolean
drag_data_received( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest
,
226 GtkSelectionData
*selection_data
);
227 gboolean
row_drop_possible( GtkTreeDragDest
*drag_dest
, GtkTreePath
*dest_path
,
228 GtkSelectionData
*selection_data
);
230 // notifactions from wxDataViewModel
231 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
232 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
233 bool ItemChanged( const wxDataViewItem
&item
);
234 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
241 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
242 GtkSortType
GetSortOrder() const { return m_sort_order
; }
244 void SetSortColumn( int column
) { m_sort_column
= column
; }
245 int GetSortColumn() const { return m_sort_column
; }
247 void SetDataViewSortColumn( wxDataViewColumn
*column
) { m_dataview_sort_column
= column
; }
248 wxDataViewColumn
*GetDataViewSortColumn() { return m_dataview_sort_column
; }
250 bool IsSorted() { return (m_sort_column
>= 0); }
253 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
254 const wxDataViewModel
* GetDataViewModel() const { return m_wx_model
; }
255 wxDataViewCtrl
* GetOwner() { return m_owner
; }
256 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
258 // item can be deleted already in the model
259 int GetIndexOf( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
261 virtual void OnInternalIdle();
265 void ScheduleRefresh();
267 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
268 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
269 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
270 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
271 void BuildBranch( wxGtkTreeModelNode
*branch
);
274 wxGtkTreeModelNode
*m_root
;
275 wxDataViewModel
*m_wx_model
;
276 GtkWxTreeModel
*m_gtk_model
;
277 wxDataViewCtrl
*m_owner
;
278 GtkSortType m_sort_order
;
279 wxDataViewColumn
*m_dataview_sort_column
;
282 GtkTargetEntry m_dragSourceTargetEntry
;
283 wxCharBuffer m_dragSourceTargetEntryTarget
;
284 wxDataObject
*m_dragDataObject
;
286 GtkTargetEntry m_dropTargetTargetEntry
;
287 wxCharBuffer m_dropTargetTargetEntryTarget
;
288 wxDataObject
*m_dropDataObject
;
290 wxGtkDataViewModelNotifier
*m_notifier
;
296 //-----------------------------------------------------------------------------
297 // wxGtkTreeModelNode
298 //-----------------------------------------------------------------------------
301 int LINKAGEMODE
wxGtkTreeModelChildCmp( void** id1
, void** id2
)
303 int ret
= gs_internal
->GetDataViewModel()->Compare( *id1
, *id2
,
304 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
309 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
310 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren
);
312 class wxGtkTreeModelNode
315 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
316 wxDataViewCtrlInternal
*internal
)
320 m_internal
= internal
;
323 ~wxGtkTreeModelNode()
325 size_t count
= m_nodes
.GetCount();
327 for (i
= 0; i
< count
; i
++)
329 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
334 unsigned int AddNode( wxGtkTreeModelNode
* child
)
336 m_nodes
.Add( child
);
338 void *id
= child
->GetItem().GetID();
340 m_children
.Add( id
);
342 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
344 gs_internal
= m_internal
;
345 m_children
.Sort( &wxGtkTreeModelChildCmp
);
346 return m_children
.Index( id
);
349 return m_children
.GetCount()-1;
352 unsigned int AddLeave( void* id
)
354 m_children
.Add( id
);
356 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
358 gs_internal
= m_internal
;
359 m_children
.Sort( &wxGtkTreeModelChildCmp
);
360 return m_children
.Index( id
);
363 return m_children
.GetCount()-1;
366 void DeleteChild( void* id
)
368 m_children
.Remove( id
);
370 unsigned int count
= m_nodes
.GetCount();
372 for (pos
= 0; pos
< count
; pos
++)
374 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
375 if (node
->GetItem().GetID() == id
)
377 m_nodes
.RemoveAt( pos
);
384 wxGtkTreeModelNode
* GetParent()
386 wxGtkTreeModelNodes
&GetNodes()
388 wxGtkTreeModelChildren
&GetChildren()
389 { return m_children
; }
391 unsigned int GetChildCount() const { return m_children
.GetCount(); }
392 unsigned int GetNodesCount() const { return m_nodes
.GetCount(); }
394 wxDataViewItem
&GetItem() { return m_item
; }
395 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
400 wxGtkTreeModelNode
*m_parent
;
401 wxGtkTreeModelNodes m_nodes
;
402 wxGtkTreeModelChildren m_children
;
403 wxDataViewItem m_item
;
404 wxDataViewCtrlInternal
*m_internal
;
408 //-----------------------------------------------------------------------------
410 //-----------------------------------------------------------------------------
412 extern bool g_blockEventsOnDrag
;
414 //-----------------------------------------------------------------------------
415 // define new GTK+ class wxGtkTreeModel
416 //-----------------------------------------------------------------------------
420 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
421 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
422 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
423 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
424 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
425 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
427 GType
gtk_wx_tree_model_get_type (void);
429 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
431 struct _GtkWxTreeModel
437 wxDataViewCtrlInternal
*internal
;
440 struct _GtkWxTreeModelClass
442 GObjectClass list_parent_class
;
445 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
446 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
447 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
449 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
450 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
451 static void wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface
*iface
);
452 static void wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface
*iface
);
454 static void wxgtk_tree_model_finalize (GObject
*object
);
455 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
456 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
457 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
459 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
462 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
464 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
468 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
470 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
472 GtkTreeIter
*parent
);
473 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
475 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
477 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
481 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
486 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
487 gint
*sort_column_id
,
489 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
492 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
494 GtkTreeIterCompareFunc func
,
496 GDestroyNotify destroy
);
497 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
498 GtkTreeIterCompareFunc func
,
500 GDestroyNotify destroy
);
501 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
504 static gboolean
wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
506 static gboolean
wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
508 static gboolean
wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
510 GtkSelectionData
*selection_data
);
511 static gboolean
wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
513 GtkSelectionData
*selection_data
);
514 static gboolean
wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
515 GtkTreePath
*dest_path
,
516 GtkSelectionData
*selection_data
);
519 static GObjectClass
*list_parent_class
= NULL
;
522 gtk_wx_tree_model_get_type (void)
524 static GType tree_model_type
= 0;
526 if (!tree_model_type
)
528 const GTypeInfo tree_model_info
=
530 sizeof (GtkWxTreeModelClass
),
531 NULL
, /* base_init */
532 NULL
, /* base_finalize */
533 (GClassInitFunc
) wxgtk_tree_model_class_init
,
534 NULL
, /* class_finalize */
535 NULL
, /* class_data */
536 sizeof (GtkWxTreeModel
),
538 (GInstanceInitFunc
) wxgtk_tree_model_init
,
541 static const GInterfaceInfo tree_model_iface_info
=
543 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
548 static const GInterfaceInfo sortable_iface_info
=
550 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
555 static const GInterfaceInfo drag_source_iface_info
=
557 (GInterfaceInitFunc
) wxgtk_tree_model_drag_source_init
,
562 static const GInterfaceInfo drag_dest_iface_info
=
564 (GInterfaceInitFunc
) wxgtk_tree_model_drag_dest_init
,
569 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
570 &tree_model_info
, (GTypeFlags
)0 );
572 g_type_add_interface_static (tree_model_type
,
574 &tree_model_iface_info
);
575 g_type_add_interface_static (tree_model_type
,
576 GTK_TYPE_TREE_SORTABLE
,
577 &sortable_iface_info
);
578 g_type_add_interface_static (tree_model_type
,
579 GTK_TYPE_TREE_DRAG_DEST
,
580 &drag_dest_iface_info
);
581 g_type_add_interface_static (tree_model_type
,
582 GTK_TYPE_TREE_DRAG_SOURCE
,
583 &drag_source_iface_info
);
586 return tree_model_type
;
589 static GtkWxTreeModel
*
590 wxgtk_tree_model_new(void)
592 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
597 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
599 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
600 GObjectClass
*object_class
= (GObjectClass
*) klass
;
601 object_class
->finalize
= wxgtk_tree_model_finalize
;
605 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
607 iface
->get_flags
= wxgtk_tree_model_get_flags
;
608 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
609 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
610 iface
->get_iter
= wxgtk_tree_model_get_iter
;
611 iface
->get_path
= wxgtk_tree_model_get_path
;
612 iface
->get_value
= wxgtk_tree_model_get_value
;
613 iface
->iter_next
= wxgtk_tree_model_iter_next
;
614 iface
->iter_children
= wxgtk_tree_model_iter_children
;
615 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
616 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
617 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
618 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
622 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
624 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
625 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
626 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
627 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
628 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
632 wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface
*iface
)
634 iface
->row_draggable
= wxgtk_tree_model_row_draggable
;
635 iface
->drag_data_delete
= wxgtk_tree_model_drag_data_delete
;
636 iface
->drag_data_get
= wxgtk_tree_model_drag_data_get
;
640 wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface
*iface
)
642 iface
->drag_data_received
= wxgtk_tree_model_drag_data_received
;
643 iface
->row_drop_possible
= wxgtk_tree_model_row_drop_possible
;
647 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
649 tree_model
->internal
= NULL
;
650 tree_model
->stamp
= g_random_int();
654 wxgtk_tree_model_finalize (GObject
*object
)
657 (* list_parent_class
->finalize
) (object
);
662 //-----------------------------------------------------------------------------
663 // implement callbacks from wxGtkTreeModel class by letting
664 // them call the methods of wxWidgets' wxDataViewModel
665 //-----------------------------------------------------------------------------
667 static GtkTreeModelFlags
668 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
670 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
671 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), (GtkTreeModelFlags
)0 );
673 return wxtree_model
->internal
->get_flags();
677 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
679 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
680 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
682 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
686 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
689 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
690 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
692 GType gtype
= G_TYPE_INVALID
;
694 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
696 if (wxtype
== wxT("string"))
697 gtype
= G_TYPE_STRING
;
700 gtype
= G_TYPE_POINTER
;
701 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
708 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
712 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
713 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
714 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
716 return wxtree_model
->internal
->get_iter( iter
, path
);
720 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
723 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
724 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
725 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
727 return wxtree_model
->internal
->get_path( iter
);
731 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
736 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
737 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
739 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
740 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
741 if (mtype
== wxT("string"))
744 g_value_init( value
, G_TYPE_STRING
);
745 wxDataViewItem
item( (void*) iter
->user_data
);
746 model
->GetValue( variant
, item
, (unsigned int) column
);
748 g_value_set_string( value
, variant
.GetString().utf8_str() );
752 wxFAIL_MSG( wxT("non-string columns not supported yet") );
757 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
760 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
762 // This happens when clearing the view by calling .._set_model( NULL );
763 if (iter
->stamp
== 0) return FALSE
;
765 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
766 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
768 return wxtree_model
->internal
->iter_next( iter
);
772 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
776 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
777 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
780 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
783 return wxtree_model
->internal
->iter_children( iter
, parent
);
787 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
790 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
791 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
792 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
794 return wxtree_model
->internal
->iter_has_child( iter
);
798 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
801 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
802 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
803 g_return_val_if_fail ( !iter
|| wxtree_model
->stamp
== iter
->stamp
, 0);
805 return wxtree_model
->internal
->iter_n_children( iter
);
809 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
814 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
815 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
817 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
821 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
825 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
826 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
827 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
829 return wxtree_model
->internal
->iter_parent( iter
, child
);
832 /* drag'n'drop iface */
834 wxgtk_tree_model_row_draggable (GtkTreeDragSource
*drag_source
,
837 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
838 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
840 return wxtree_model
->internal
->row_draggable( drag_source
, path
);
844 wxgtk_tree_model_drag_data_delete (GtkTreeDragSource
*drag_source
,
847 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
848 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
850 return wxtree_model
->internal
->drag_data_delete( drag_source
, path
);
854 wxgtk_tree_model_drag_data_get (GtkTreeDragSource
*drag_source
,
856 GtkSelectionData
*selection_data
)
858 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_source
;
859 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
862 wxPrintf( "drag_get_data\n");
864 wxGtkString
atom_selection(gdk_atom_name(selection_data
->selection
));
865 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection
) );
867 wxGtkString
atom_target(gdk_atom_name(selection_data
->target
));
868 wxPrintf( "target %s\n", wxString::FromAscii(atom_target
) );
870 wxGtkString
atom_type(gdk_atom_name(selection_data
->type
));
871 wxPrintf( "type %s\n", wxString::FromAscii(atom_type
) );
873 wxPrintf( "format %d\n", selection_data
->format
);
876 return wxtree_model
->internal
->drag_data_get( drag_source
, path
, selection_data
);
880 wxgtk_tree_model_drag_data_received (GtkTreeDragDest
*drag_dest
,
882 GtkSelectionData
*selection_data
)
884 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
885 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
887 return wxtree_model
->internal
->drag_data_received( drag_dest
, dest
, selection_data
);
891 wxgtk_tree_model_row_drop_possible (GtkTreeDragDest
*drag_dest
,
892 GtkTreePath
*dest_path
,
893 GtkSelectionData
*selection_data
)
895 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) drag_dest
;
896 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
898 return wxtree_model
->internal
->row_drop_possible( drag_dest
, dest_path
, selection_data
);
903 wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
904 gint
*sort_column_id
,
907 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) sortable
;
909 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
911 if (!wxtree_model
->internal
->IsSorted())
914 *sort_column_id
= -1;
921 *sort_column_id
= wxtree_model
->internal
->GetSortColumn();
924 *order
= wxtree_model
->internal
->GetSortOrder();
929 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
932 wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
936 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
937 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
939 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
941 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
942 (order
!= tree_model
->internal
->GetSortOrder()))
944 tree_model
->internal
->SetSortColumn( sort_column_id
);
945 tree_model
->internal
->SetSortOrder( order
);
947 gtk_tree_sortable_sort_column_changed (sortable
);
949 tree_model
->internal
->GetDataViewModel()->Resort();
952 if (gs_lastLeftClickHeader
)
954 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
955 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
956 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
957 event
.SetModel( dv
->GetModel() );
958 dv
->HandleWindowEvent( event
);
961 gs_lastLeftClickHeader
= NULL
;
965 wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
966 gint
WXUNUSED(sort_column_id
),
967 GtkTreeIterCompareFunc func
,
968 gpointer
WXUNUSED(data
),
969 GDestroyNotify
WXUNUSED(destroy
))
971 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
972 g_return_if_fail (func
!= NULL
);
976 wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
977 GtkTreeIterCompareFunc func
,
978 gpointer
WXUNUSED(data
),
979 GDestroyNotify
WXUNUSED(destroy
))
981 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
982 g_return_if_fail (func
!= NULL
);
984 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
985 // TODO: remove this code
989 wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
991 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
996 //-----------------------------------------------------------------------------
997 // define new GTK+ class GtkWxRendererText
998 //-----------------------------------------------------------------------------
1002 #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
1003 #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
1004 #define GTK_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1005 #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1006 #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1007 #define GTK_WX_CELL_RENDERER_TEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1009 GType
gtk_wx_cell_renderer_text_get_type (void);
1011 typedef struct _GtkWxCellRendererText GtkWxCellRendererText
;
1012 typedef struct _GtkWxCellRendererTextClass GtkWxCellRendererTextClass
;
1014 struct _GtkWxCellRendererText
1016 GtkCellRendererText parent
;
1018 wxDataViewRenderer
*wx_renderer
;
1021 struct _GtkWxCellRendererTextClass
1023 GtkCellRendererTextClass cell_parent_class
;
1027 static GtkWxCellRendererText
*gtk_wx_cell_renderer_text_new (void);
1028 static void gtk_wx_cell_renderer_text_init (
1029 GtkWxCellRendererText
*cell
);
1030 static void gtk_wx_cell_renderer_text_class_init(
1031 GtkWxCellRendererTextClass
*klass
);
1032 static void gtk_wx_cell_renderer_text_finalize (
1034 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1035 GtkCellRenderer
*cell
,
1039 GdkRectangle
*background_area
,
1040 GdkRectangle
*cell_area
,
1041 GtkCellRendererState flags
);
1044 static GObjectClass
*text_cell_parent_class
= NULL
;
1049 gtk_wx_cell_renderer_text_get_type (void)
1051 static GType cell_wx_type
= 0;
1055 const GTypeInfo cell_wx_info
=
1057 sizeof (GtkWxCellRendererTextClass
),
1058 NULL
, /* base_init */
1059 NULL
, /* base_finalize */
1060 (GClassInitFunc
) gtk_wx_cell_renderer_text_class_init
,
1061 NULL
, /* class_finalize */
1062 NULL
, /* class_data */
1063 sizeof (GtkWxCellRendererText
),
1064 0, /* n_preallocs */
1065 (GInstanceInitFunc
) gtk_wx_cell_renderer_text_init
,
1068 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT
,
1069 "GtkWxCellRendererText", &cell_wx_info
, (GTypeFlags
)0 );
1072 return cell_wx_type
;
1076 gtk_wx_cell_renderer_text_init (GtkWxCellRendererText
*cell
)
1078 cell
->wx_renderer
= NULL
;
1082 gtk_wx_cell_renderer_text_class_init (GtkWxCellRendererTextClass
*klass
)
1084 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
1085 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1087 text_cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1089 object_class
->finalize
= gtk_wx_cell_renderer_text_finalize
;
1091 cell_class
->start_editing
= gtk_wx_cell_renderer_text_start_editing
;
1095 gtk_wx_cell_renderer_text_finalize (GObject
*object
)
1098 (* G_OBJECT_CLASS (text_cell_parent_class
)->finalize
) (object
);
1101 GtkWxCellRendererText
*
1102 gtk_wx_cell_renderer_text_new (void)
1104 return (GtkWxCellRendererText
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT
, NULL
);
1107 static GtkCellEditable
*gtk_wx_cell_renderer_text_start_editing(
1108 GtkCellRenderer
*gtk_renderer
,
1109 GdkEvent
*gdk_event
,
1112 GdkRectangle
*background_area
,
1113 GdkRectangle
*cell_area
,
1114 GtkCellRendererState flags
)
1116 GtkWxCellRendererText
*wxgtk_renderer
= (GtkWxCellRendererText
*) gtk_renderer
;
1117 wxDataViewRenderer
*wx_renderer
= wxgtk_renderer
->wx_renderer
;
1118 wxDataViewColumn
*column
= wx_renderer
->GetOwner();
1121 item(column
->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1123 wxDataViewCtrl
*dv
= column
->GetOwner();
1124 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING
, dv
->GetId() );
1125 event
.SetDataViewColumn( column
);
1126 event
.SetModel( dv
->GetModel() );
1127 event
.SetColumn( column
->GetModelColumn() );
1128 event
.SetItem( item
);
1129 dv
->HandleWindowEvent( event
);
1131 if (event
.IsAllowed())
1132 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class
)->
1133 start_editing( gtk_renderer
, gdk_event
, widget
, path
, background_area
, cell_area
, flags
);
1138 //-----------------------------------------------------------------------------
1139 // define new GTK+ class GtkWxCellRenderer
1140 //-----------------------------------------------------------------------------
1144 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
1145 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
1146 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1147 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
1148 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
1149 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1151 GType
gtk_wx_cell_renderer_get_type (void);
1153 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
1154 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
1156 struct _GtkWxCellRenderer
1158 GtkCellRenderer parent
;
1161 wxDataViewCustomRenderer
*cell
;
1165 struct _GtkWxCellRendererClass
1167 GtkCellRendererClass cell_parent_class
;
1171 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
1172 static void gtk_wx_cell_renderer_init (
1173 GtkWxCellRenderer
*cell
);
1174 static void gtk_wx_cell_renderer_class_init(
1175 GtkWxCellRendererClass
*klass
);
1176 static void gtk_wx_cell_renderer_finalize (
1178 static void gtk_wx_cell_renderer_get_size (
1179 GtkCellRenderer
*cell
,
1181 GdkRectangle
*rectangle
,
1186 static void gtk_wx_cell_renderer_render (
1187 GtkCellRenderer
*cell
,
1190 GdkRectangle
*background_area
,
1191 GdkRectangle
*cell_area
,
1192 GdkRectangle
*expose_area
,
1193 GtkCellRendererState flags
);
1194 static gboolean
gtk_wx_cell_renderer_activate(
1195 GtkCellRenderer
*cell
,
1199 GdkRectangle
*background_area
,
1200 GdkRectangle
*cell_area
,
1201 GtkCellRendererState flags
);
1202 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1203 GtkCellRenderer
*cell
,
1207 GdkRectangle
*background_area
,
1208 GdkRectangle
*cell_area
,
1209 GtkCellRendererState flags
);
1212 static GObjectClass
*cell_parent_class
= NULL
;
1217 gtk_wx_cell_renderer_get_type (void)
1219 static GType cell_wx_type
= 0;
1223 const GTypeInfo cell_wx_info
=
1225 sizeof (GtkWxCellRendererClass
),
1226 NULL
, /* base_init */
1227 NULL
, /* base_finalize */
1228 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
1229 NULL
, /* class_finalize */
1230 NULL
, /* class_data */
1231 sizeof (GtkWxCellRenderer
),
1232 0, /* n_preallocs */
1233 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
1236 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
1237 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
1240 return cell_wx_type
;
1244 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
1247 cell
->last_click
= 0;
1251 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
1253 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
1254 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
1256 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
1258 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
1260 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
1261 cell_class
->render
= gtk_wx_cell_renderer_render
;
1262 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
1263 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
1267 gtk_wx_cell_renderer_finalize (GObject
*object
)
1270 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
1274 gtk_wx_cell_renderer_new (void)
1276 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
1279 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
1280 GtkCellRenderer
*renderer
,
1281 GdkEvent
*WXUNUSED(event
),
1284 GdkRectangle
*WXUNUSED(background_area
),
1285 GdkRectangle
*cell_area
,
1286 GtkCellRendererState
WXUNUSED(flags
) )
1288 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1289 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1291 // Renderer doesn't support in-place editing
1292 if (!cell
->HasEditorCtrl())
1295 // An in-place editing control is still around
1296 if (cell
->GetEditorCtrl())
1300 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1306 rect
.x
+= cell_area
->x
;
1307 rect
.y
+= cell_area
->y
;
1308 // rect.width -= renderer->xpad * 2;
1309 // rect.height -= renderer->ypad * 2;
1311 // wxRect renderrect(wxRectFromGDKRect(&rect));
1312 wxRect
renderrect(wxRectFromGDKRect(cell_area
));
1315 item(cell
->GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(path
)));
1317 cell
->StartEditing( item
, renderrect
);
1323 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
1324 GtkWidget
*WXUNUSED(widget
),
1325 GdkRectangle
*cell_area
,
1331 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1332 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1334 wxSize size
= cell
->GetSize();
1336 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1338 // Uniform row height, if specified, overrides the value returned by the
1340 if ( !ctrl
->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
1342 const int uniformHeight
= ctrl
->GTKGetUniformRowHeight();
1343 if ( uniformHeight
> 0 )
1344 size
.y
= uniformHeight
;
1348 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1349 int calc_width
= xpad
* 2 + size
.x
;
1350 int calc_height
= ypad
* 2 + size
.y
;
1357 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
1359 float xalign
, yalign
;
1360 gtk_cell_renderer_get_alignment(renderer
, &xalign
, &yalign
);
1363 *x_offset
= int(xalign
* (cell_area
->width
- calc_width
- 2 * xpad
));
1364 *x_offset
= MAX(*x_offset
, 0) + xpad
;
1368 *y_offset
= int(yalign
* (cell_area
->height
- calc_height
- 2 * ypad
));
1369 *y_offset
= MAX(*y_offset
, 0) + ypad
;
1374 *width
= calc_width
;
1377 *height
= calc_height
;
1381 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
1384 GdkRectangle
*background_area
,
1385 GdkRectangle
*cell_area
,
1386 GdkRectangle
*expose_area
,
1387 GtkCellRendererState flags
)
1390 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1391 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1393 cell
->GTKStashRenderParams(window
, widget
,
1394 background_area
, expose_area
, flags
);
1396 wxRect
rect(wxRectFromGDKRect(cell_area
));
1398 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1399 rect
= rect
.Deflate(xpad
, ypad
);
1401 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
1402 wxWindowDCImpl
*impl
= (wxWindowDCImpl
*) dc
->GetImpl();
1404 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1405 // window such as a DnD drop window.
1406 if (window
!= impl
->m_gdkwindow
)
1409 impl
->m_gdkwindow
= window
;
1414 if (flags
& GTK_CELL_RENDERER_SELECTED
)
1415 state
|= wxDATAVIEW_CELL_SELECTED
;
1416 if (flags
& GTK_CELL_RENDERER_PRELIT
)
1417 state
|= wxDATAVIEW_CELL_PRELIT
;
1418 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1419 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1420 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
1421 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
1422 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
1423 state
|= wxDATAVIEW_CELL_FOCUSED
;
1424 cell
->WXCallRender( rect
, dc
, state
);
1428 gtk_wx_cell_renderer_activate(
1429 GtkCellRenderer
*renderer
,
1433 GdkRectangle
*WXUNUSED(background_area
),
1434 GdkRectangle
*cell_area
,
1435 GtkCellRendererState
WXUNUSED(flags
) )
1437 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
1438 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
1441 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
1447 rect
.x
+= cell_area
->x
;
1448 rect
.y
+= cell_area
->y
;
1450 gtk_cell_renderer_get_padding(renderer
, &xpad
, &ypad
);
1451 rect
.width
-= xpad
* 2;
1452 rect
.height
-= ypad
* 2;
1454 wxRect
renderrect(wxRectFromGDKRect(&rect
));
1456 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
1457 wxDataViewModel
*model
= ctrl
->GetModel();
1459 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
1461 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1467 // activated by <ENTER>
1468 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1473 else if (event
->type
== GDK_BUTTON_PRESS
)
1475 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
1476 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
1477 ((int) button_event
->y
) - renderrect
.y
);
1480 if (button_event
->button
== 1)
1482 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
1484 // TODO: query system double-click time
1485 if (button_event
->time
- wxrenderer
->last_click
< 400)
1486 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1489 wxrenderer
->last_click
= button_event
->time
;
1497 // ---------------------------------------------------------
1498 // wxGtkDataViewModelNotifier
1499 // ---------------------------------------------------------
1501 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1504 wxGtkDataViewModelNotifier( wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
);
1505 ~wxGtkDataViewModelNotifier();
1507 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1508 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1509 virtual bool ItemChanged( const wxDataViewItem
&item
);
1510 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
1511 virtual bool Cleared();
1512 virtual void Resort();
1513 virtual bool BeforeReset();
1514 virtual bool AfterReset();
1516 void UpdateLastCount();
1519 wxDataViewModel
*m_wx_model
;
1520 wxDataViewCtrlInternal
*m_internal
;
1523 // ---------------------------------------------------------
1524 // wxGtkDataViewListModelNotifier
1525 // ---------------------------------------------------------
1527 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1528 wxDataViewModel
*wx_model
, wxDataViewCtrlInternal
*internal
)
1530 m_wx_model
= wx_model
;
1531 m_internal
= internal
;
1534 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1540 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1542 m_internal
->ItemAdded( parent
, item
);
1543 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1546 iter
.stamp
= wxgtk_model
->stamp
;
1547 iter
.user_data
= item
.GetID();
1549 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1550 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1551 gtk_tree_model_row_inserted(
1552 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1557 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1559 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1561 // using _get_path for a deleted item cannot be
1564 iter
.stamp
= wxgtk_model
->stamp
;
1565 iter
.user_data
= (gpointer
) item
.GetID();
1566 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1567 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1569 // so get the path from the parent
1571 iter
.stamp
= wxgtk_model
->stamp
;
1572 iter
.user_data
= (gpointer
) parent
.GetID();
1573 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1574 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1575 // and add the final index ourselves
1576 int index
= m_internal
->GetIndexOf( parent
, item
);
1577 gtk_tree_path_append_index( path
, index
);
1580 gtk_tree_model_row_deleted(
1581 GTK_TREE_MODEL(wxgtk_model
), path
);
1583 m_internal
->ItemDeleted( parent
, item
);
1588 void wxGtkDataViewModelNotifier::Resort()
1590 m_internal
->Resort();
1593 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1595 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1598 iter
.stamp
= wxgtk_model
->stamp
;
1599 iter
.user_data
= (gpointer
) item
.GetID();
1601 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1602 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1603 gtk_tree_model_row_changed(
1604 GTK_TREE_MODEL(wxgtk_model
), path
, &iter
);
1606 m_internal
->ItemChanged( item
);
1611 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
)
1613 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1614 wxDataViewCtrl
*ctrl
= m_internal
->GetOwner();
1616 // This adds GTK+'s missing MVC logic for ValueChanged
1618 for (index
= 0; index
< ctrl
->GetColumnCount(); index
++)
1620 wxDataViewColumn
*column
= ctrl
->GetColumn( index
);
1621 if (column
->GetModelColumn() == model_column
)
1623 GtkTreeView
*widget
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
1624 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1628 iter
.stamp
= wxgtk_model
->stamp
;
1629 iter
.user_data
= (gpointer
) item
.GetID();
1630 wxGtkTreePath
path(wxgtk_tree_model_get_path(
1631 GTK_TREE_MODEL(wxgtk_model
), &iter
));
1632 GdkRectangle cell_area
;
1633 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1635 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1636 double d
= gtk_adjustment_get_value( hadjust
);
1637 int xdiff
= (int) d
;
1639 int ydiff
= gcolumn
->button
->allocation
.height
;
1641 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1642 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1644 m_internal
->ValueChanged( item
, model_column
);
1653 bool wxGtkDataViewModelNotifier::BeforeReset()
1655 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1656 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), NULL
);
1661 bool wxGtkDataViewModelNotifier::AfterReset()
1663 GtkWidget
*treeview
= m_internal
->GetOwner()->GtkGetTreeView();
1664 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1666 m_internal
->Cleared();
1668 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview
), GTK_TREE_MODEL(wxgtk_model
) );
1673 bool wxGtkDataViewModelNotifier::Cleared()
1675 GtkWxTreeModel
*wxgtk_model
= m_internal
->GetGtkModel();
1677 // There is no call to tell the model that everything
1678 // has been deleted so call row_deleted() for every
1681 int count
= m_internal
->iter_n_children( NULL
); // number of children of root
1683 GtkTreePath
*path
= gtk_tree_path_new_first(); // points to root
1686 for (i
= 0; i
< count
; i
++)
1687 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model
), path
);
1689 gtk_tree_path_free( path
);
1691 m_internal
->Cleared();
1696 // ---------------------------------------------------------
1697 // wxDataViewRenderer
1698 // ---------------------------------------------------------
1700 static gpointer s_user_data
= NULL
;
1703 wxgtk_cell_editable_editing_done( GtkCellEditable
*WXUNUSED(editable
),
1704 wxDataViewRenderer
*wxrenderer
)
1706 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1707 wxDataViewCtrl
*dv
= column
->GetOwner();
1708 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1709 event
.SetDataViewColumn( column
);
1710 event
.SetModel( dv
->GetModel() );
1711 wxDataViewItem
item( s_user_data
);
1712 event
.SetItem( item
);
1713 dv
->HandleWindowEvent( event
);
1717 wxgtk_renderer_editing_started( GtkCellRenderer
*WXUNUSED(cell
), GtkCellEditable
*editable
,
1718 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1723 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1724 wxDataViewCtrl
*dv
= column
->GetOwner();
1725 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1726 event
.SetDataViewColumn( column
);
1727 event
.SetModel( dv
->GetModel() );
1728 wxDataViewItem
item(dv
->GTKPathToItem(wxGtkTreePath(path
)));
1729 event
.SetItem( item
);
1730 dv
->HandleWindowEvent( event
);
1732 if (GTK_IS_CELL_EDITABLE(editable
))
1734 s_user_data
= item
.GetID();
1736 g_signal_connect (GTK_CELL_EDITABLE (editable
), "editing_done",
1737 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1738 (gpointer
) wxrenderer
);
1744 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1746 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1748 wxDataViewRendererBase( varianttype
, mode
, align
)
1753 // we haven't changed them yet
1754 m_usingDefaultAttrs
= true;
1756 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1757 // after the m_renderer pointer has been initialized
1760 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
1762 gtk_tree_view_column_pack_end( column
, m_renderer
, TRUE
/* expand */);
1765 void wxDataViewRenderer::GtkInitHandlers()
1767 if (!gtk_check_version(2,6,0))
1769 g_signal_connect (GTK_CELL_RENDERER(m_renderer
), "editing_started",
1770 G_CALLBACK (wxgtk_renderer_editing_started
),
1775 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1777 GtkCellRendererMode gtkMode
;
1780 case wxDATAVIEW_CELL_INERT
:
1781 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1784 case wxDATAVIEW_CELL_ACTIVATABLE
:
1785 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1788 case wxDATAVIEW_CELL_EDITABLE
:
1789 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1793 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1799 // This value is most often ignored in GtkTreeView
1800 GValue gvalue
= { 0, };
1801 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1802 g_value_set_enum( &gvalue
, gtkMode
);
1803 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1804 g_value_unset( &gvalue
);
1807 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1809 wxDataViewCellMode ret
;
1812 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1814 switch (g_value_get_enum(&gvalue
))
1817 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1818 // fall through (we have to return something)
1820 case GTK_CELL_RENDERER_MODE_INERT
:
1821 ret
= wxDATAVIEW_CELL_INERT
;
1824 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1825 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1828 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1829 ret
= wxDATAVIEW_CELL_EDITABLE
;
1833 g_value_unset( &gvalue
);
1838 void wxDataViewRenderer::GtkUpdateAlignment()
1840 int align
= m_alignment
;
1842 // query alignment from column ?
1846 if (GetOwner() == NULL
)
1849 align
= GetOwner()->GetAlignment();
1850 align
|= wxALIGN_CENTRE_VERTICAL
;
1853 // horizontal alignment:
1855 gfloat xalign
= 0.0;
1856 if (align
& wxALIGN_RIGHT
)
1858 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1861 GValue gvalue
= { 0, };
1862 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1863 g_value_set_float( &gvalue
, xalign
);
1864 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1865 g_value_unset( &gvalue
);
1867 // vertical alignment:
1869 gfloat yalign
= 0.0;
1870 if (align
& wxALIGN_BOTTOM
)
1872 else if (align
& wxALIGN_CENTER_VERTICAL
)
1875 GValue gvalue2
= { 0, };
1876 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1877 g_value_set_float( &gvalue2
, yalign
);
1878 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1879 g_value_unset( &gvalue2
);
1882 void wxDataViewRenderer::SetAlignment( int align
)
1884 m_alignment
= align
;
1885 GtkUpdateAlignment();
1888 int wxDataViewRenderer::GetAlignment() const
1893 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode
)
1896 if ( gtk_check_version(2, 6, 0) != NULL
)
1899 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1903 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1904 // can just cast between them
1905 GValue gvalue
= { 0, };
1906 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1907 g_value_set_enum( &gvalue
, static_cast<PangoEllipsizeMode
>(mode
) );
1908 g_object_set_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1909 g_value_unset( &gvalue
);
1912 #endif // GTK 2.6/before
1915 wxEllipsizeMode
wxDataViewRenderer::GetEllipsizeMode() const
1918 if ( gtk_check_version(2, 6, 0) != NULL
)
1919 return wxELLIPSIZE_NONE
;
1921 GtkCellRendererText
* const rend
= GtkGetTextRenderer();
1923 return wxELLIPSIZE_NONE
;
1925 GValue gvalue
= { 0, };
1926 g_value_init( &gvalue
, PANGO_TYPE_ELLIPSIZE_MODE
);
1927 g_object_get_property( G_OBJECT(rend
), "ellipsize", &gvalue
);
1929 mode
= static_cast<wxEllipsizeMode
>(g_value_get_enum( &gvalue
));
1930 g_value_unset( &gvalue
);
1934 return wxELLIPSIZE_NONE
;
1935 #endif // GTK 2.6/before
1939 wxDataViewRenderer::GtkOnTextEdited(const gchar
*itempath
, const wxString
& str
)
1941 wxVariant
value(str
);
1942 if (!Validate( value
))
1946 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
1948 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
1952 wxDataViewRenderer::GtkOnCellChanged(const wxVariant
& value
,
1953 const wxDataViewItem
& item
,
1956 wxDataViewModel
*model
= GetOwner()->GetOwner()->GetModel();
1957 model
->ChangeValue( value
, item
, col
);
1960 // ---------------------------------------------------------
1961 // wxDataViewTextRenderer
1962 // ---------------------------------------------------------
1967 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*WXUNUSED(renderer
),
1968 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1970 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) user_data
;
1972 cell
->GtkOnTextEdited(arg1
, wxGTK_CONV_BACK_FONT(
1973 arg2
, cell
->GetOwner()->GetOwner()->GetFont()));
1981 // helper function used by wxDataViewTextRenderer and
1982 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
1983 // given text renderer and returns true if anything was done
1984 bool GtkApplyAttr(GtkCellRendererText
*renderer
, const wxDataViewItemAttr
& attr
)
1986 bool usingDefaultAttrs
= true;
1987 if (attr
.HasColour())
1989 const GdkColor
* const gcol
= attr
.GetColour().GetColor();
1991 GValue gvalue
= { 0, };
1992 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
1993 g_value_set_boxed( &gvalue
, gcol
);
1994 g_object_set_property( G_OBJECT(renderer
), "foreground_gdk", &gvalue
);
1995 g_value_unset( &gvalue
);
1997 usingDefaultAttrs
= false;
2001 GValue gvalue
= { 0, };
2002 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2003 g_value_set_boolean( &gvalue
, FALSE
);
2004 g_object_set_property( G_OBJECT(renderer
), "foreground-set", &gvalue
);
2005 g_value_unset( &gvalue
);
2008 if (attr
.GetItalic())
2010 GValue gvalue
= { 0, };
2011 g_value_init( &gvalue
, PANGO_TYPE_STYLE
);
2012 g_value_set_enum( &gvalue
, PANGO_STYLE_ITALIC
);
2013 g_object_set_property( G_OBJECT(renderer
), "style", &gvalue
);
2014 g_value_unset( &gvalue
);
2016 usingDefaultAttrs
= false;
2020 GValue gvalue
= { 0, };
2021 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2022 g_value_set_boolean( &gvalue
, FALSE
);
2023 g_object_set_property( G_OBJECT(renderer
), "style-set", &gvalue
);
2024 g_value_unset( &gvalue
);
2030 GValue gvalue
= { 0, };
2031 g_value_init( &gvalue
, PANGO_TYPE_WEIGHT
);
2032 g_value_set_enum( &gvalue
, PANGO_WEIGHT_BOLD
);
2033 g_object_set_property( G_OBJECT(renderer
), "weight", &gvalue
);
2034 g_value_unset( &gvalue
);
2036 usingDefaultAttrs
= false;
2040 GValue gvalue
= { 0, };
2041 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2042 g_value_set_boolean( &gvalue
, FALSE
);
2043 g_object_set_property( G_OBJECT(renderer
), "weight-set", &gvalue
);
2044 g_value_unset( &gvalue
);
2048 if (attr
.HasBackgroundColour())
2050 wxColour colour
= attr
.GetBackgroundColour();
2051 const GdkColor
* const gcol
= colour
.GetColor();
2053 GValue gvalue
= { 0, };
2054 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2055 g_value_set_boxed( &gvalue
, gcol
);
2056 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2057 g_value_unset( &gvalue
);
2061 GValue gvalue
= { 0, };
2062 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2063 g_value_set_boolean( &gvalue
, FALSE
);
2064 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2065 g_value_unset( &gvalue
);
2069 return !usingDefaultAttrs
;
2072 } // anonymous namespace
2074 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
2076 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2078 wxDataViewRenderer( varianttype
, mode
, align
)
2080 GtkWxCellRendererText
*text_renderer
= gtk_wx_cell_renderer_text_new();
2081 text_renderer
->wx_renderer
= this;
2082 m_renderer
= (GtkCellRenderer
*) text_renderer
;
2084 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
2086 GValue gvalue
= { 0, };
2087 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2088 g_value_set_boolean( &gvalue
, true );
2089 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
2090 g_value_unset( &gvalue
);
2092 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2098 SetAlignment(align
);
2101 bool wxDataViewTextRenderer::SetTextValue(const wxString
& str
)
2103 GValue gvalue
= { 0, };
2104 g_value_init( &gvalue
, G_TYPE_STRING
);
2105 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( str
, GetOwner()->GetOwner()->GetFont() ) );
2106 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2107 g_value_unset( &gvalue
);
2112 bool wxDataViewTextRenderer::GetTextValue(wxString
& str
) const
2114 GValue gvalue
= { 0, };
2115 g_value_init( &gvalue
, G_TYPE_STRING
);
2116 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2117 str
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
), const_cast<wxDataViewTextRenderer
*>(this)->GetOwner()->GetOwner()->GetFont() );
2118 g_value_unset( &gvalue
);
2123 void wxDataViewTextRenderer::SetAlignment( int align
)
2125 wxDataViewRenderer::SetAlignment(align
);
2127 if (gtk_check_version(2,10,0))
2130 // horizontal alignment:
2131 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2132 if (align
& wxALIGN_RIGHT
)
2133 pangoAlign
= PANGO_ALIGN_RIGHT
;
2134 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2135 pangoAlign
= PANGO_ALIGN_CENTER
;
2137 GValue gvalue
= { 0, };
2138 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2139 g_value_set_enum( &gvalue
, pangoAlign
);
2140 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2141 g_value_unset( &gvalue
);
2144 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr
& attr
)
2146 return GtkApplyAttr(GtkGetTextRenderer(), attr
);
2149 GtkCellRendererText
*wxDataViewTextRenderer::GtkGetTextRenderer() const
2151 return GTK_CELL_RENDERER_TEXT(m_renderer
);
2154 // ---------------------------------------------------------
2155 // wxDataViewBitmapRenderer
2156 // ---------------------------------------------------------
2161 // set "pixbuf" property on the given renderer
2162 void SetPixbufProp(GtkCellRenderer
*renderer
, GdkPixbuf
*pixbuf
)
2164 GValue gvalue
= { 0, };
2165 g_value_init( &gvalue
, G_TYPE_OBJECT
);
2166 g_value_set_object( &gvalue
, pixbuf
);
2167 g_object_set_property( G_OBJECT(renderer
), "pixbuf", &gvalue
);
2168 g_value_unset( &gvalue
);
2171 } // anonymous namespace
2173 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
2175 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
2177 wxDataViewRenderer( varianttype
, mode
, align
)
2179 m_renderer
= gtk_cell_renderer_pixbuf_new();
2182 SetAlignment(align
);
2185 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
2187 if (value
.GetType() == wxT("wxBitmap"))
2192 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2193 // object (and it will stay there and remain owned by wxBitmap)
2194 SetPixbufProp(m_renderer
, bitmap
.GetPixbuf());
2196 else if (value
.GetType() == wxT("wxIcon"))
2201 SetPixbufProp(m_renderer
, icon
.GetPixbuf());
2211 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2216 // ---------------------------------------------------------
2217 // wxDataViewToggleRenderer
2218 // ---------------------------------------------------------
2221 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2222 gchar
*path
, gpointer user_data
);
2225 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
2226 gchar
*path
, gpointer user_data
)
2228 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
2231 GValue gvalue
= { 0, };
2232 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2233 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
2234 bool tmp
= g_value_get_boolean( &gvalue
);
2235 g_value_unset( &gvalue
);
2239 wxVariant value
= tmp
;
2240 if (!cell
->Validate( value
))
2243 wxDataViewCtrl
* const ctrl
= cell
->GetOwner()->GetOwner();
2244 wxDataViewModel
*model
= ctrl
->GetModel();
2246 wxDataViewItem
item(ctrl
->GTKPathToItem(wxGtkTreePath(path
)));
2248 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
2250 model
->ChangeValue( value
, item
, model_col
);
2253 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
2255 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
2256 wxDataViewCellMode mode
, int align
) :
2257 wxDataViewRenderer( varianttype
, mode
, align
)
2259 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
2261 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
2263 g_signal_connect_after( m_renderer
, "toggled",
2264 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
2268 GValue gvalue
= { 0, };
2269 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2270 g_value_set_boolean( &gvalue
, false );
2271 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
2272 g_value_unset( &gvalue
);
2276 SetAlignment(align
);
2279 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
2283 GValue gvalue
= { 0, };
2284 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2285 g_value_set_boolean( &gvalue
, tmp
);
2286 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2287 g_value_unset( &gvalue
);
2292 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
2294 GValue gvalue
= { 0, };
2295 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2296 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
2297 bool tmp
= g_value_get_boolean( &gvalue
);
2298 g_value_unset( &gvalue
);
2305 // ---------------------------------------------------------
2306 // wxDataViewCustomRenderer
2307 // ---------------------------------------------------------
2309 class wxDataViewCtrlDCImpl
: public wxWindowDCImpl
2312 wxDataViewCtrlDCImpl( wxDC
*owner
, wxDataViewCtrl
*window
) :
2313 wxWindowDCImpl( owner
)
2315 GtkWidget
*widget
= window
->m_treeview
;
2321 m_context
= window
->GTKGetPangoDefaultContext();
2322 m_layout
= pango_layout_new( m_context
);
2323 m_fontdesc
= pango_font_description_copy(gtk_widget_get_style(widget
)->font_desc
);
2325 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
2327 // Set m_gdkwindow later
2332 class wxDataViewCtrlDC
: public wxWindowDC
2335 wxDataViewCtrlDC( wxDataViewCtrl
*window
) :
2336 wxWindowDC( new wxDataViewCtrlDCImpl( this, window
) )
2341 // ---------------------------------------------------------
2342 // wxDataViewCustomRenderer
2343 // ---------------------------------------------------------
2345 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
2347 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
2348 wxDataViewCellMode mode
,
2351 : wxDataViewCustomRendererBase( varianttype
, mode
, align
)
2354 m_text_renderer
= NULL
;
2362 GtkCellRendererText
*wxDataViewCustomRenderer::GtkGetTextRenderer() const
2364 if ( !m_text_renderer
)
2366 // we create it on demand so need to do it even from a const function
2367 const_cast<wxDataViewCustomRenderer
*>(this)->
2368 m_text_renderer
= GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2369 g_object_ref_sink(m_text_renderer
);
2372 return m_text_renderer
;
2375 void wxDataViewCustomRenderer::RenderText( const wxString
&text
,
2379 int WXUNUSED(state
) )
2382 GtkCellRendererText
* const textRenderer
= GtkGetTextRenderer();
2384 GValue gvalue
= { 0, };
2385 g_value_init( &gvalue
, G_TYPE_STRING
);
2386 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( text
, GetOwner()->GetOwner()->GetFont() ) );
2387 g_object_set_property( G_OBJECT(textRenderer
), "text", &gvalue
);
2388 g_value_unset( &gvalue
);
2390 GtkApplyAttr(textRenderer
, GetAttr());
2392 GdkRectangle cell_area
;
2393 wxRectToGDKRect(cell
, cell_area
);
2394 cell_area
.x
+= xoffset
;
2395 cell_area
.width
-= xoffset
;
2397 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer
),
2398 m_renderParams
.window
,
2399 m_renderParams
.widget
,
2400 m_renderParams
.background_area
,
2402 m_renderParams
.expose_area
,
2403 (GtkCellRendererState
) m_renderParams
.flags
);
2406 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
2408 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
2409 renderer
->cell
= this;
2411 m_renderer
= (GtkCellRenderer
*) renderer
;
2414 SetAlignment(align
);
2421 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2426 if (m_text_renderer
)
2427 g_object_unref(m_text_renderer
);
2430 wxDC
*wxDataViewCustomRenderer::GetDC()
2434 if (GetOwner() == NULL
)
2436 if (GetOwner()->GetOwner() == NULL
)
2438 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2444 // ---------------------------------------------------------
2445 // wxDataViewProgressRenderer
2446 // ---------------------------------------------------------
2448 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
2450 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
2451 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
2452 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
2458 if (!gtk_check_version(2,6,0))
2460 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
2462 GValue gvalue
= { 0, };
2463 g_value_init( &gvalue
, G_TYPE_STRING
);
2465 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( m_label
, GetOwner()->GetOwner()->GetFont() ) );
2466 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2467 g_value_unset( &gvalue
);
2470 SetAlignment(align
);
2475 // Use custom cell code
2476 wxDataViewCustomRenderer::Init(mode
, align
);
2480 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2484 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
2487 if (!gtk_check_version(2,6,0))
2489 gint tmp
= (long) value
;
2490 GValue gvalue
= { 0, };
2491 g_value_init( &gvalue
, G_TYPE_INT
);
2492 g_value_set_int( &gvalue
, tmp
);
2493 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
2494 g_value_unset( &gvalue
);
2499 m_value
= (long) value
;
2501 if (m_value
< 0) m_value
= 0;
2502 if (m_value
> 100) m_value
= 100;
2508 bool wxDataViewProgressRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2513 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
2515 double pct
= (double)m_value
/ 100.0;
2517 bar
.width
= (int)(cell
.width
* pct
);
2518 dc
->SetPen( *wxTRANSPARENT_PEN
);
2519 dc
->SetBrush( *wxBLUE_BRUSH
);
2520 dc
->DrawRectangle( bar
);
2522 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
2523 dc
->SetPen( *wxBLACK_PEN
);
2524 dc
->DrawRectangle( cell
);
2529 wxSize
wxDataViewProgressRenderer::GetSize() const
2531 return wxSize(40,12);
2534 // -------------------------------------
2535 // wxDataViewChoiceRenderer
2536 // -------------------------------------
2538 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString
&choices
,
2539 wxDataViewCellMode mode
, int alignment
) :
2540 wxDataViewCustomRenderer( "string", mode
, alignment
, true )
2542 m_choices
= choices
;
2545 if (!gtk_check_version(2,6,0))
2547 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_combo_new();
2549 GtkListStore
*store
= gtk_list_store_new( 1, G_TYPE_STRING
);
2550 for (size_t n
= 0; n
< m_choices
.GetCount(); n
++)
2552 gtk_list_store_insert_with_values(
2554 static_cast<const char *>(m_choices
[n
].utf8_str()), -1 );
2557 g_object_set (m_renderer
,
2563 bool editable
= (mode
& wxDATAVIEW_CELL_EDITABLE
);
2564 g_object_set (m_renderer
, "editable", editable
, NULL
);
2566 SetAlignment(alignment
);
2568 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
2575 // Use custom cell code
2576 wxDataViewCustomRenderer::Init(mode
, alignment
);
2580 bool wxDataViewChoiceRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
2582 RenderText( m_data
, 0, rect
, dc
, state
);
2586 wxSize
wxDataViewChoiceRenderer::GetSize() const
2588 return wxSize(70,20);
2591 bool wxDataViewChoiceRenderer::SetValue( const wxVariant
&value
)
2595 if (!gtk_check_version(2,6,0))
2597 GValue gvalue
= { 0, };
2598 g_value_init( &gvalue
, G_TYPE_STRING
);
2599 g_value_set_string(&gvalue
,
2600 wxGTK_CONV_FONT(value
.GetString(),
2601 GetOwner()->GetOwner()->GetFont()));
2602 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2603 g_value_unset( &gvalue
);
2607 m_data
= value
.GetString();
2612 bool wxDataViewChoiceRenderer::GetValue( wxVariant
&value
) const
2615 if (!gtk_check_version(2,6,0))
2617 GValue gvalue
= { 0, };
2618 g_value_init( &gvalue
, G_TYPE_STRING
);
2619 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
2620 wxString temp
= wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue
),
2621 GetOwner()->GetOwner()->GetFont());
2622 g_value_unset( &gvalue
);
2625 //wxPrintf( "temp %s\n", temp );
2626 // TODO: remove this code
2635 void wxDataViewChoiceRenderer::SetAlignment( int align
)
2637 wxDataViewCustomRenderer::SetAlignment(align
);
2639 if (gtk_check_version(2,10,0))
2642 // horizontal alignment:
2643 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
2644 if (align
& wxALIGN_RIGHT
)
2645 pangoAlign
= PANGO_ALIGN_RIGHT
;
2646 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
2647 pangoAlign
= PANGO_ALIGN_CENTER
;
2649 GValue gvalue
= { 0, };
2650 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
2651 g_value_set_enum( &gvalue
, pangoAlign
);
2652 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
2653 g_value_unset( &gvalue
);
2656 // ----------------------------------------------------------------------------
2657 // wxDataViewChoiceByIndexRenderer
2658 // ----------------------------------------------------------------------------
2660 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
2661 wxDataViewCellMode mode
, int alignment
) :
2662 wxDataViewChoiceRenderer( choices
, mode
, alignment
)
2666 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const gchar
*itempath
, const wxString
& str
)
2668 wxVariant
value( (long) GetChoices().Index( str
) );
2670 if (!Validate( value
))
2674 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath
)));
2676 GtkOnCellChanged(value
, item
, GetOwner()->GetModelColumn());
2679 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant
&value
)
2681 wxVariant string_value
= GetChoice( value
.GetLong() );
2682 return wxDataViewChoiceRenderer::SetValue( string_value
);
2685 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant
&value
) const
2687 wxVariant string_value
;
2688 if (!wxDataViewChoiceRenderer::GetValue( string_value
))
2691 value
= (long) GetChoices().Index( string_value
.GetString() );
2695 // ---------------------------------------------------------
2696 // wxDataViewDateRenderer
2697 // ---------------------------------------------------------
2699 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
2702 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
2703 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
2704 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
2709 m_cal
= new wxCalendarCtrl( this, -1, *value
);
2710 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
2711 sizer
->Add( m_cal
, 1, wxGROW
);
2716 virtual void OnDismiss()
2720 void OnCalendar( wxCalendarEvent
&event
);
2722 wxCalendarCtrl
*m_cal
;
2723 wxDataViewModel
*m_model
;
2724 wxDataViewItem m_item
;
2728 DECLARE_EVENT_TABLE()
2731 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
2732 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
2735 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
2737 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
2741 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
2743 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
2744 wxDataViewCellMode mode
, int align
) :
2745 wxDataViewCustomRenderer( varianttype
, mode
, align
)
2748 SetAlignment(align
);
2751 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
2753 m_date
= value
.GetDateTime();
2758 bool wxDataViewDateRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
2763 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
2765 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
2766 wxString tmp
= m_date
.FormatDate();
2767 RenderText( tmp
, 0, cell
, dc
, state
);
2771 wxSize
wxDataViewDateRenderer::GetSize() const
2773 wxString tmp
= m_date
.FormatDate();
2775 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
2776 return wxSize(x
,y
+d
);
2779 bool wxDataViewDateRenderer::Activate( const wxRect
& WXUNUSED(cell
), wxDataViewModel
*model
,
2780 const wxDataViewItem
&item
, unsigned int col
)
2783 model
->GetValue( variant
, item
, col
);
2784 wxDateTime value
= variant
.GetDateTime();
2786 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
2787 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
2788 wxPoint pos
= wxGetMousePosition();
2791 popup
->Popup( popup
->m_cal
);
2797 // ---------------------------------------------------------
2798 // wxDataViewIconTextRenderer
2799 // ---------------------------------------------------------
2801 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
2803 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2805 const wxString
&varianttype
,
2806 wxDataViewCellMode mode
,
2809 : wxDataViewTextRenderer(varianttype
, mode
, align
)
2811 m_rendererIcon
= gtk_cell_renderer_pixbuf_new();
2814 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2818 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn
*column
)
2820 // add the icon renderer first
2821 gtk_tree_view_column_pack_start(column
, m_rendererIcon
, FALSE
/* !expand */);
2823 // add the text renderer too
2824 wxDataViewRenderer::GtkPackIntoColumn(column
);
2827 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
2831 SetTextValue(m_value
.GetText());
2832 SetPixbufProp(m_rendererIcon
, m_value
.GetIcon().GetPixbuf());
2837 bool wxDataViewIconTextRenderer::GetValue(wxVariant
& value
) const
2840 if ( !GetTextValue(str
) )
2843 // user doesn't have any way to edit the icon so leave it unchanged
2844 value
<< wxDataViewIconText(str
, m_value
.GetIcon());
2850 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant
& value
,
2851 const wxDataViewItem
& item
,
2854 // we receive just the text part of our value as it's the only one which
2855 // can be edited, but we need the full wxDataViewIconText value for the
2857 wxVariant valueIconText
;
2858 valueIconText
<< wxDataViewIconText(value
.GetString(), m_value
.GetIcon());
2859 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText
, item
, col
);
2862 // ---------------------------------------------------------
2864 // ---------------------------------------------------------
2868 gtk_dataview_header_button_press_callback( GtkWidget
*WXUNUSED(widget
),
2869 GdkEventButton
*gdk_event
,
2870 wxDataViewColumn
*column
)
2872 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
2875 if (gdk_event
->button
== 1)
2877 gs_lastLeftClickHeader
= column
;
2879 wxDataViewCtrl
*dv
= column
->GetOwner();
2880 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2881 event
.SetDataViewColumn( column
);
2882 event
.SetModel( dv
->GetModel() );
2883 if (dv
->HandleWindowEvent( event
))
2887 if (gdk_event
->button
== 3)
2889 wxDataViewCtrl
*dv
= column
->GetOwner();
2890 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2891 event
.SetDataViewColumn( column
);
2892 event
.SetModel( dv
->GetModel() );
2893 if (dv
->HandleWindowEvent( event
))
2903 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*WXUNUSED(column
),
2904 GtkCellRenderer
*renderer
,
2905 GtkTreeModel
*model
,
2909 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2910 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2912 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2914 wxDataViewItem
item( (void*) iter
->user_data
);
2916 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2918 if (!wx_model
->IsVirtualListModel())
2921 if (wx_model
->IsContainer( item
))
2923 visible
= wx_model
->HasContainerColumns( item
) ||
2924 (cell
->GetOwner()->GetModelColumn() == 0);
2931 GValue gvalue
= { 0, };
2932 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2933 g_value_set_boolean( &gvalue
, visible
);
2934 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2935 g_value_unset( &gvalue
);
2942 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2944 if (value
.GetType() != cell
->GetVariantType())
2946 wxLogError( wxT("Wrong type, required: %s but: %s"),
2947 value
.GetType().c_str(),
2948 cell
->GetVariantType().c_str() );
2951 cell
->SetValue( value
);
2953 // deal with disabled items
2954 bool enabled
= wx_model
->IsEnabled( item
, cell
->GetOwner()->GetModelColumn() );
2956 // a) this sets the appearance to disabled grey
2957 GValue gvalue
= { 0, };
2958 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2959 g_value_set_boolean( &gvalue
, enabled
);
2960 g_object_set_property( G_OBJECT(renderer
), "sensitive", &gvalue
);
2961 g_value_unset( &gvalue
);
2963 // b) this actually disables the control/renderer
2965 cell
->SetMode( cell
->GtkGetMode() );
2967 cell
->SetMode( wxDATAVIEW_CELL_INERT
);
2970 // deal with attributes: if the renderer doesn't support them at all, we
2971 // don't even need to query the model for them
2972 if ( !cell
->GtkSupportsAttrs() )
2975 // it can support attributes so check if this item has any
2976 wxDataViewItemAttr attr
;
2977 if ( wx_model
->GetAttr( item
, cell
->GetOwner()->GetModelColumn(), attr
)
2978 || !cell
->GtkIsUsingDefaultAttrs() )
2980 bool usingDefaultAttrs
= !cell
->GtkSetAttr(attr
);
2981 cell
->GtkSetUsingDefaultAttrs(usingDefaultAttrs
);
2983 // else: no custom attributes specified and we're already using the default
2984 // ones -- nothing to do
2990 #include <wx/listimpl.cpp>
2991 WX_DEFINE_LIST(wxDataViewColumnList
)
2993 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
2994 unsigned int model_column
, int width
,
2995 wxAlignment align
, int flags
)
2996 : wxDataViewColumnBase( cell
, model_column
)
2998 Init( align
, flags
, width
);
3003 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
3004 unsigned int model_column
, int width
,
3005 wxAlignment align
, int flags
)
3006 : wxDataViewColumnBase( bitmap
, cell
, model_column
)
3008 Init( align
, flags
, width
);
3010 SetBitmap( bitmap
);
3013 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
3015 m_isConnected
= false;
3017 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
3018 m_column
= (GtkWidget
*) column
;
3021 SetAlignment( align
);
3025 // Create container for icon and label
3026 GtkWidget
*box
= gtk_hbox_new( FALSE
, 1 );
3027 gtk_widget_show( box
);
3028 // gtk_container_set_border_width((GtkContainer*)box, 2);
3029 m_image
= gtk_image_new();
3030 gtk_box_pack_start(GTK_BOX(box
), m_image
, FALSE
, FALSE
, 1);
3031 m_label
= gtk_label_new("");
3032 gtk_box_pack_end( GTK_BOX(box
), GTK_WIDGET(m_label
), FALSE
, FALSE
, 1 );
3033 gtk_tree_view_column_set_widget( column
, box
);
3035 wxDataViewRenderer
* const colRenderer
= GetRenderer();
3036 GtkCellRenderer
* const cellRenderer
= colRenderer
->GetGtkHandle();
3038 colRenderer
->GtkPackIntoColumn(column
);
3040 gtk_tree_view_column_set_cell_data_func( column
, cellRenderer
,
3041 wxGtkTreeCellDataFunc
, (gpointer
) colRenderer
, NULL
);
3044 void wxDataViewColumn::OnInternalIdle()
3049 if (gtk_widget_get_realized(GetOwner()->m_treeview
))
3051 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3054 g_signal_connect(column
->button
, "button_press_event",
3055 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
3057 // otherwise the event will be blocked by GTK+
3058 gtk_tree_view_column_set_clickable( column
, TRUE
);
3060 m_isConnected
= true;
3065 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
3067 wxDataViewColumnBase::SetOwner( owner
);
3069 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3071 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3074 void wxDataViewColumn::SetTitle( const wxString
&title
)
3076 wxDataViewCtrl
*ctrl
= GetOwner();
3077 gtk_label_set_text( GTK_LABEL(m_label
), ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
3078 : wxGTK_CONV_SYS(title
) );
3080 gtk_widget_hide( m_label
);
3082 gtk_widget_show( m_label
);
3085 wxString
wxDataViewColumn::GetTitle() const
3087 return wxGTK_CONV_BACK_FONT(
3088 gtk_label_get_text( GTK_LABEL(m_label
) ),
3089 GetOwner()->GetFont()
3093 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
3095 wxDataViewColumnBase::SetBitmap( bitmap
);
3099 GtkImage
*gtk_image
= GTK_IMAGE(m_image
);
3101 GdkBitmap
*mask
= NULL
;
3102 if (bitmap
.GetMask())
3103 mask
= bitmap
.GetMask()->GetBitmap();
3105 if (bitmap
.HasPixbuf())
3107 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
3108 bitmap
.GetPixbuf());
3112 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
3113 bitmap
.GetPixmap(), mask
);
3115 gtk_widget_show( m_image
);
3119 gtk_widget_hide( m_image
);
3123 void wxDataViewColumn::SetHidden( bool hidden
)
3125 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
3128 void wxDataViewColumn::SetResizeable( bool resizable
)
3130 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizable
);
3133 void wxDataViewColumn::SetAlignment( wxAlignment align
)
3135 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3137 gfloat xalign
= 0.0;
3138 if (align
== wxALIGN_RIGHT
)
3140 if (align
== wxALIGN_CENTER_HORIZONTAL
||
3141 align
== wxALIGN_CENTER
)
3144 gtk_tree_view_column_set_alignment( column
, xalign
);
3146 if (m_renderer
&& m_renderer
->GetAlignment() == -1)
3147 m_renderer
->GtkUpdateAlignment();
3150 wxAlignment
wxDataViewColumn::GetAlignment() const
3152 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
3155 return wxALIGN_RIGHT
;
3157 return wxALIGN_CENTER_HORIZONTAL
;
3159 return wxALIGN_LEFT
;
3162 void wxDataViewColumn::SetSortable( bool sortable
)
3164 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3168 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
3172 gtk_tree_view_column_set_sort_column_id( column
, -1 );
3173 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
3174 gtk_tree_view_column_set_clickable( column
, FALSE
);
3178 bool wxDataViewColumn::IsSortable() const
3180 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3181 return gtk_tree_view_column_get_clickable( column
);
3184 void wxDataViewColumn::SetAsSortKey( bool WXUNUSED(sort
) )
3186 // it might not make sense to have this function in wxHeaderColumn at
3187 // all in fact, changing of the sort order should only be done using the
3188 // associated control API
3189 wxFAIL_MSG( "not implemented" );
3192 bool wxDataViewColumn::IsSortKey() const
3194 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3195 return gtk_tree_view_column_get_sort_indicator( column
);
3198 bool wxDataViewColumn::IsResizeable() const
3200 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3201 return gtk_tree_view_column_get_resizable( column
);
3204 bool wxDataViewColumn::IsHidden() const
3206 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3207 return !gtk_tree_view_column_get_visible( column
);
3210 void wxDataViewColumn::SetSortOrder( bool ascending
)
3212 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3215 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
3217 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
3219 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
3222 bool wxDataViewColumn::IsSortOrderAscending() const
3224 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
3226 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
3229 void wxDataViewColumn::SetMinWidth( int width
)
3231 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3234 int wxDataViewColumn::GetMinWidth() const
3236 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3239 int wxDataViewColumn::GetWidth() const
3241 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
3244 void wxDataViewColumn::SetWidth( int width
)
3246 if ( width
== wxCOL_WIDTH_AUTOSIZE
)
3248 // NB: this disables user resizing
3249 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
3253 if ( width
== wxCOL_WIDTH_DEFAULT
)
3255 // TODO find a better calculation
3256 width
= wxDVC_DEFAULT_WIDTH
;
3259 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column
), GTK_TREE_VIEW_COLUMN_FIXED
);
3260 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
3264 void wxDataViewColumn::SetReorderable( bool reorderable
)
3266 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column
), reorderable
);
3269 bool wxDataViewColumn::IsReorderable() const
3271 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column
) );
3274 //-----------------------------------------------------------------------------
3275 // wxGtkTreeModelNode
3276 //-----------------------------------------------------------------------------
3279 class wxGtkTreeModelChildWithPos
3287 int wxGtkTreeModelChildWithPosCmp( const void* data1
, const void* data2
, const void* user_data
)
3289 const wxGtkTreeModelChildWithPos
* child1
= (const wxGtkTreeModelChildWithPos
*) data1
;
3290 const wxGtkTreeModelChildWithPos
* child2
= (const wxGtkTreeModelChildWithPos
*) data2
;
3291 const wxDataViewCtrlInternal
*internal
= (const wxDataViewCtrlInternal
*) user_data
;
3292 int ret
= internal
->GetDataViewModel()->Compare( child1
->id
, child2
->id
,
3293 internal
->GetSortColumn(), (internal
->GetSortOrder() == GTK_SORT_DESCENDING
) );
3299 int LINKAGEMODE
wxGtkTreeModelChildPtrCmp( void*** data1
, void*** data2
)
3301 return gs_internal
->GetDataViewModel()->Compare( **data1
, **data2
,
3302 gs_internal
->GetSortColumn(), (gs_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
3305 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr
);
3308 void wxGtkTreeModelNode::Resort()
3310 size_t child_count
= GetChildCount();
3311 if (child_count
== 0)
3314 size_t node_count
= GetNodesCount();
3316 if (child_count
== 1)
3318 if (node_count
== 1)
3320 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
3326 gint
*new_order
= new gint
[child_count
];
3329 // m_children has the original *void
3330 // ptrs points to these
3331 wxGtkTreeModelChildrenPtr ptrs
;
3333 for (i
= 0; i
< child_count
; i
++)
3334 ptrs
.Add( &(m_children
[i
]) );
3336 gs_internal
= m_internal
;
3337 ptrs
.Sort( &wxGtkTreeModelChildPtrCmp
);
3339 wxGtkTreeModelChildren temp
;
3340 void** base_ptr
= &(m_children
[0]);
3341 // Transfer positions to new_order array and
3343 for (i
= 0; i
< child_count
; i
++)
3345 new_order
[i
] = ptrs
[i
] - base_ptr
;
3346 temp
.Add( *ptrs
[i
] );
3349 // Transfer IDs back to m_children
3351 WX_APPEND_ARRAY( temp
, m_children
);
3356 // Build up array with IDs and original positions
3357 wxGtkTreeModelChildWithPos
* temp
= new wxGtkTreeModelChildWithPos
[child_count
];
3359 for (i
= 0; i
< child_count
; i
++)
3362 temp
[i
].id
= m_children
[i
];
3364 // Sort array keeping original positions
3365 wxQsort( temp
, child_count
, sizeof(wxGtkTreeModelChildWithPos
),
3366 &wxGtkTreeModelChildWithPosCmp
, m_internal
);
3367 // Transfer positions to new_order array and
3368 // IDs to m_children
3370 for (i
= 0; i
< child_count
; i
++)
3372 new_order
[i
] = temp
[i
].pos
;
3373 m_children
.Add( temp
[i
].id
);
3382 wxGtkTreeModelChildren temp
;
3383 WX_APPEND_ARRAY( temp
, m_children
);
3385 gs_internal
= m_internal
;
3386 m_children
.Sort( &wxGtkTreeModelChildCmp
);
3389 for (pos
= 0; pos
< child_count
; pos
++)
3391 void *id
= m_children
.Item( pos
);
3392 int old_pos
= temp
.Index( id
);
3393 new_order
[pos
] = old_pos
;
3397 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
3400 iter
.user_data
= GetItem().GetID();
3401 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
3403 gtk_tree_model_rows_reordered( gtk_tree_model
,
3404 wxGtkTreePath(m_internal
->get_path(&iter
)), &iter
, new_order
);
3406 delete [] new_order
;
3409 for (pos
= 0; pos
< node_count
; pos
++)
3411 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
3416 //-----------------------------------------------------------------------------
3417 // wxDataViewCtrlInternal
3418 //-----------------------------------------------------------------------------
3420 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
)
3423 m_wx_model
= wx_model
;
3427 m_sort_order
= GTK_SORT_ASCENDING
;
3429 m_dataview_sort_column
= NULL
;
3431 m_dragDataObject
= NULL
;
3432 m_dropDataObject
= NULL
;
3436 m_gtk_model
= wxgtk_tree_model_new();
3437 m_gtk_model
->internal
= this;
3439 m_notifier
= new wxGtkDataViewModelNotifier( wx_model
, this );
3441 wx_model
->AddNotifier( m_notifier
);
3443 // g_object_unref( gtk_model ); ???
3445 if (!m_wx_model
->IsVirtualListModel())
3448 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner
->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model
) );
3451 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3453 m_wx_model
->RemoveNotifier( m_notifier
);
3455 // remove the model from the GtkTreeView before it gets destroyed
3456 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner
->GtkGetTreeView() ), NULL
);
3458 g_object_unref( m_gtk_model
);
3460 delete m_dragDataObject
;
3461 delete m_dropDataObject
;
3464 void wxDataViewCtrlInternal::ScheduleRefresh()
3469 void wxDataViewCtrlInternal::OnInternalIdle()
3473 GtkWidget
*widget
= m_owner
->GtkGetTreeView();
3474 gtk_widget_queue_draw( widget
);
3479 void wxDataViewCtrlInternal::InitTree()
3481 wxDataViewItem item
;
3482 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
3484 BuildBranch( m_root
);
3487 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
3489 if (node
->GetChildCount() == 0)
3491 wxDataViewItemArray children
;
3492 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
3495 for (pos
= 0; pos
< count
; pos
++)
3497 wxDataViewItem child
= children
[pos
];
3499 if (m_wx_model
->IsContainer( child
))
3500 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
3502 node
->AddLeave( child
.GetID() );
3504 // Don't send any events here
3511 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat
&format
)
3513 wxGtkString
atom_str( gdk_atom_name( format
) );
3514 m_dragSourceTargetEntryTarget
= wxCharBuffer( atom_str
);
3516 m_dragSourceTargetEntry
.target
= m_dragSourceTargetEntryTarget
.data();
3517 m_dragSourceTargetEntry
.flags
= 0;
3518 m_dragSourceTargetEntry
.info
= static_cast<guint
>(-1);
3520 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3521 GDK_BUTTON1_MASK
, &m_dragSourceTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3526 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat
&format
)
3528 wxGtkString
atom_str( gdk_atom_name( format
) );
3529 m_dropTargetTargetEntryTarget
= wxCharBuffer( atom_str
);
3531 m_dropTargetTargetEntry
.target
= m_dropTargetTargetEntryTarget
.data();
3532 m_dropTargetTargetEntry
.flags
= 0;
3533 m_dropTargetTargetEntry
.info
= static_cast<guint
>(-1);
3535 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner
->GtkGetTreeView() ),
3536 &m_dropTargetTargetEntry
, 1, (GdkDragAction
) GDK_ACTION_COPY
);
3541 gboolean
wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource
*WXUNUSED(drag_source
),
3544 delete m_dragDataObject
;
3546 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3550 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3551 event
.SetEventObject( m_owner
);
3552 event
.SetItem( item
);
3553 event
.SetModel( m_wx_model
);
3555 gtk_widget_get_pointer(m_owner
->GtkGetTreeView(), &x
, &y
);
3556 event
.SetPosition(x
, y
);
3557 if (!m_owner
->HandleWindowEvent( event
))
3560 if (!event
.IsAllowed())
3563 wxDataObject
*obj
= event
.GetDataObject();
3567 m_dragDataObject
= obj
;
3573 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource
*WXUNUSED(drag_source
),
3574 GtkTreePath
*WXUNUSED(path
))
3579 gboolean
wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource
*WXUNUSED(drag_source
),
3580 GtkTreePath
*path
, GtkSelectionData
*selection_data
)
3582 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3586 GdkAtom target
= gtk_selection_data_get_target(selection_data
);
3587 if (!m_dragDataObject
->IsSupported(target
))
3590 size_t size
= m_dragDataObject
->GetDataSize(target
);
3594 void *buf
= malloc( size
);
3596 gboolean res
= FALSE
;
3597 if (m_dragDataObject
->GetDataHere(target
, buf
))
3601 gtk_selection_data_set(selection_data
, target
,
3602 8, (const guchar
*) buf
, size
);
3611 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3613 GtkSelectionData
*selection_data
)
3615 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3619 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
3620 event
.SetEventObject( m_owner
);
3621 event
.SetItem( item
);
3622 event
.SetModel( m_wx_model
);
3623 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3624 event
.SetDataSize(gtk_selection_data_get_length(selection_data
));
3625 event
.SetDataBuffer(const_cast<guchar
*>(gtk_selection_data_get_data(selection_data
)));
3626 if (!m_owner
->HandleWindowEvent( event
))
3629 if (!event
.IsAllowed())
3636 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest
*WXUNUSED(drag_dest
),
3638 GtkSelectionData
*selection_data
)
3640 wxDataViewItem
item(GetOwner()->GTKPathToItem(path
));
3644 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
3645 event
.SetEventObject( m_owner
);
3646 event
.SetItem( item
);
3647 event
.SetModel( m_wx_model
);
3648 event
.SetDataFormat(gtk_selection_data_get_target(selection_data
));
3649 if (!m_owner
->HandleWindowEvent( event
))
3652 if (!event
.IsAllowed())
3658 // notifications from wxDataViewModel
3660 bool wxDataViewCtrlInternal::Cleared()
3675 void wxDataViewCtrlInternal::Resort()
3677 if (!m_wx_model
->IsVirtualListModel())
3683 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
3685 if (!m_wx_model
->IsVirtualListModel())
3687 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
3688 wxASSERT_MSG(parent_node
,
3689 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3691 if (m_wx_model
->IsContainer( item
))
3692 parent_node
->AddNode( new wxGtkTreeModelNode( parent_node
, item
, this ) );
3694 parent_node
->AddLeave( item
.GetID() );
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 if (gtk_tree_view_get_path_at_pos(
4437 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
4438 (int) gdk_event
->x
, (int) gdk_event
->y
,
4446 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, dv
->GetId() );
4447 event
.SetItem(dv
->GTKPathToItem(path
));
4448 event
.SetModel( dv
->GetModel() );
4449 return dv
->HandleWindowEvent( event
);
4457 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4459 wxDataViewCtrl::~wxDataViewCtrl()
4461 // Stop editing before destroying the control to remove any event handlers
4462 // which are added when editing started: if we didn't do this, the base
4463 // class dtor would assert as it checks for any leftover handlers.
4466 GtkTreeViewColumn
*col
;
4467 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), NULL
, &col
);
4469 wxDataViewColumn
* const wxcol
= FromGTKColumn(col
);
4472 // This won't do anything if we're not editing it
4473 wxcol
->GetRenderer()->CancelEditing();
4482 void wxDataViewCtrl::Init()
4486 m_cols
.DeleteContents( true );
4488 m_uniformRowHeight
= -1;
4491 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4496 const wxValidator
& validator
,
4497 const wxString
& name
)
4499 if (!PreCreation( parent
, pos
, size
) ||
4500 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
4502 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4506 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
4507 g_object_ref(m_widget
);
4509 GTKScrolledWindowSetBorder(m_widget
, style
);
4511 m_treeview
= gtk_tree_view_new();
4512 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
4514 g_signal_connect (m_treeview
, "size_allocate",
4515 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
4518 if (!gtk_check_version(2,6,0))
4520 bool fixed
= (style
& wxDV_VARIABLE_LINE_HEIGHT
) == 0;
4521 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4525 if (style
& wxDV_MULTIPLE
)
4527 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4528 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
4531 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
4534 if (!gtk_check_version(2,10,0))
4536 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
4538 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
4539 (style
& wxDV_VERT_RULES
) != 0)
4540 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
4541 else if (style
& wxDV_VERT_RULES
)
4542 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
4543 else if (style
& wxDV_HORIZ_RULES
)
4544 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
4546 if (grid
!= GTK_TREE_VIEW_GRID_LINES_NONE
)
4547 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
4551 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_ROW_LINES
) != 0 );
4553 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
4554 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
4555 gtk_widget_show (m_treeview
);
4557 m_parent
->DoAddChild( this );
4561 GtkEnableSelectionEvents();
4563 g_signal_connect_after (m_treeview
, "row-activated",
4564 G_CALLBACK (wxdataview_row_activated_callback
), this);
4566 g_signal_connect (m_treeview
, "test-collapse-row",
4567 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
4569 g_signal_connect_after (m_treeview
, "row-collapsed",
4570 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
4572 g_signal_connect (m_treeview
, "test-expand-row",
4573 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
4575 g_signal_connect_after (m_treeview
, "row-expanded",
4576 G_CALLBACK (wxdataview_row_expanded_callback
), this);
4578 g_signal_connect (m_treeview
, "motion_notify_event",
4579 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
4581 g_signal_connect (m_treeview
, "button_press_event",
4582 G_CALLBACK (gtk_dataview_button_press_callback
), this);
4587 wxDataViewItem
wxDataViewCtrl::GTKPathToItem(GtkTreePath
*path
) const
4590 return wxDataViewItem(path
&& m_internal
->get_iter(&iter
, path
)
4595 void wxDataViewCtrl::OnInternalIdle()
4597 wxWindow::OnInternalIdle();
4599 m_internal
->OnInternalIdle();
4601 unsigned int cols
= GetColumnCount();
4603 for (i
= 0; i
< cols
; i
++)
4605 wxDataViewColumn
*col
= GetColumn( i
);
4606 col
->OnInternalIdle();
4609 if (m_ensureVisibleDefered
.IsOk())
4611 ExpandAncestors(m_ensureVisibleDefered
);
4613 iter
.user_data
= (gpointer
) m_ensureVisibleDefered
.GetID();
4614 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4615 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
4616 m_ensureVisibleDefered
= wxDataViewItem(0);
4620 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4622 wxDELETE(m_internal
);
4624 if (!wxDataViewCtrlBase::AssociateModel( model
))
4628 if (!gtk_check_version(2,6,0))
4630 bool fixed
= (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
) == 0) || (model
->IsVirtualListModel()));
4631 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), fixed
);
4635 m_internal
= new wxDataViewCtrlInternal( this, model
);
4640 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4642 return m_internal
->EnableDragSource( format
);
4645 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4647 return m_internal
->EnableDropTarget( format
);
4650 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4652 if (!wxDataViewCtrlBase::AppendColumn(col
))
4655 m_cols
.Append( col
);
4658 if (!gtk_check_version(2,6,0))
4660 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4661 GTK_TREE_VIEW_COLUMN_FIXED
)
4662 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4666 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
4667 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4672 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4674 if (!wxDataViewCtrlBase::PrependColumn(col
))
4677 m_cols
.Insert( col
);
4680 if (!gtk_check_version(2,6,0))
4682 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4683 GTK_TREE_VIEW_COLUMN_FIXED
)
4684 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4688 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4689 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
4694 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4696 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4699 m_cols
.Insert( pos
, col
);
4702 if (!gtk_check_version(2,6,0))
4704 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) ) !=
4705 GTK_TREE_VIEW_COLUMN_FIXED
)
4706 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), FALSE
);
4710 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
4711 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), pos
);
4716 unsigned int wxDataViewCtrl::GetColumnCount() const
4718 return m_cols
.GetCount();
4721 wxDataViewColumn
* wxDataViewCtrl::FromGTKColumn(GtkTreeViewColumn
*gtk_col
) const
4726 wxDataViewColumnList::const_iterator iter
;
4727 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4729 wxDataViewColumn
*col
= *iter
;
4730 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
4736 wxFAIL_MSG( "No matching column?" );
4741 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
4743 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
4745 return FromGTKColumn(gtk_col
);
4748 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4750 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4751 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
4753 m_cols
.DeleteObject( column
);
4758 bool wxDataViewCtrl::ClearColumns()
4760 wxDataViewColumnList::iterator iter
;
4761 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); ++iter
)
4763 wxDataViewColumn
*col
= *iter
;
4764 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
4765 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
4773 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4775 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
4777 GList
*list
= gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview
) );
4779 gint pos
= g_list_index( list
, (gconstpointer
) gtk_column
);
4781 g_list_free( list
);
4786 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4788 return m_internal
->GetDataViewSortColumn();
4791 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4794 iter
.user_data
= item
.GetID();
4795 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4796 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
4799 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4802 iter
.user_data
= item
.GetID();
4803 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4804 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
4807 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4810 iter
.user_data
= item
.GetID();
4811 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4812 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview
), path
);
4815 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4817 // The tree doesn't have any current item if it hadn't been created yet but
4818 // it's arguably not an error to call this function in this case so just
4819 // return an invalid item without asserting.
4821 return wxDataViewItem();
4824 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview
), path
.ByRef(), NULL
);
4826 return GTKPathToItem(path
);
4829 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4831 wxCHECK_RET( m_treeview
,
4832 "Current item can't be set before creating the control." );
4834 // We need to make sure the model knows about this item or the path would
4835 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4836 ExpandAncestors(item
);
4838 // We also need to preserve the existing selection from changing.
4839 // Unfortunately the only way to do it seems to use our own selection
4840 // function and forbid any selection changes during set cursor call.
4841 wxGtkTreeSelectionLock
4842 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4844 // Do move the cursor now.
4846 iter
.user_data
= item
.GetID();
4847 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4849 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, NULL
, FALSE
);
4852 void wxDataViewCtrl::StartEditor(const wxDataViewItem
& item
, unsigned int column
)
4854 wxCHECK_RET( m_treeview
,
4855 "Current item can't be set before creating the control." );
4857 // We need to make sure the model knows about this item or the path would
4858 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4859 ExpandAncestors(item
);
4861 wxDataViewColumn
*dvcolumn
= GetColumn(column
);
4862 wxASSERT_MSG(dvcolumn
, "Could not retrieve column");
4863 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(dvcolumn
->GetGtkHandle());
4865 // We also need to preserve the existing selection from changing.
4866 // Unfortunately the only way to do it seems to use our own selection
4867 // function and forbid any selection changes during set cursor call.
4868 wxGtkTreeSelectionLock
4869 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview
)));
4871 // Do move the cursor now.
4873 iter
.user_data
= item
.GetID();
4874 wxGtkTreePath
path(m_internal
->get_path( &iter
));
4876 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview
), path
, gcolumn
, TRUE
);
4879 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4881 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4883 if (m_windowStyle
& wxDV_MULTIPLE
)
4885 // Report the first one
4886 GtkTreeModel
*model
;
4887 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
4891 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
4892 wxDataViewItem
item(GTKPathToItem(path
));
4895 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
4896 g_list_free( list
);
4904 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
4906 wxDataViewItem
item( iter
.user_data
);
4911 return wxDataViewItem(0);
4914 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4918 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4919 if (HasFlag(wxDV_MULTIPLE
))
4921 GtkTreeModel
*model
;
4922 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
4927 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
4929 sel
.Add(GTKPathToItem(path
));
4931 list
= g_list_next( list
);
4936 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
4937 g_list_free( list
);
4943 GtkTreeModel
*model
;
4945 gboolean has_selection
= gtk_tree_selection_get_selected( selection
, &model
, &iter
);
4948 sel
.Add( wxDataViewItem( (void*) iter
.user_data
) );
4956 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4958 GtkDisableSelectionEvents();
4960 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4962 gtk_tree_selection_unselect_all( selection
);
4964 wxDataViewItem last_parent
;
4967 for (i
= 0; i
< sel
.GetCount(); i
++)
4969 wxDataViewItem item
= sel
[i
];
4970 wxDataViewItem parent
= GetModel()->GetParent( item
);
4973 if (parent
!= last_parent
)
4974 ExpandAncestors(item
);
4976 last_parent
= parent
;
4979 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4980 iter
.user_data
= (gpointer
) item
.GetID();
4981 gtk_tree_selection_select_iter( selection
, &iter
);
4984 GtkEnableSelectionEvents();
4987 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4989 ExpandAncestors(item
);
4991 GtkDisableSelectionEvents();
4993 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
4996 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
4997 iter
.user_data
= (gpointer
) item
.GetID();
4998 gtk_tree_selection_select_iter( selection
, &iter
);
5000 GtkEnableSelectionEvents();
5003 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
5005 GtkDisableSelectionEvents();
5007 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5010 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
5011 iter
.user_data
= (gpointer
) item
.GetID();
5012 gtk_tree_selection_unselect_iter( selection
, &iter
);
5014 GtkEnableSelectionEvents();
5017 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
5019 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5022 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
5023 iter
.user_data
= (gpointer
) item
.GetID();
5025 return gtk_tree_selection_iter_is_selected( selection
, &iter
);
5028 void wxDataViewCtrl::SelectAll()
5030 GtkDisableSelectionEvents();
5032 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5034 gtk_tree_selection_select_all( selection
);
5036 GtkEnableSelectionEvents();
5039 void wxDataViewCtrl::UnselectAll()
5041 GtkDisableSelectionEvents();
5043 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5045 gtk_tree_selection_unselect_all( selection
);
5047 GtkEnableSelectionEvents();
5050 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem
& item
,
5051 const wxDataViewColumn
*WXUNUSED(column
))
5053 m_ensureVisibleDefered
= item
;
5054 ExpandAncestors(item
);
5057 iter
.user_data
= (gpointer
) item
.GetID();
5058 wxGtkTreePath
path(m_internal
->get_path( &iter
));
5059 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
5062 void wxDataViewCtrl::HitTest(const wxPoint
& point
,
5063 wxDataViewItem
& item
,
5064 wxDataViewColumn
*& column
) const
5066 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
5067 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
5068 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
5069 // So we have to use both of them.
5070 // Friedrich Haase 2010-9-20
5071 wxGtkTreePath path
, pathScratch
;
5072 GtkTreeViewColumn
* GtkColumn
= NULL
;
5073 GtkTreeViewDropPosition pos
= GTK_TREE_VIEW_DROP_INTO_OR_AFTER
;
5077 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5078 wxDataViewCtrl
* ctrl
= (wxDataViewCtrl
*)this; // ugly workaround, ctrl is NOT const
5079 GtkTreeView
* treeView
= GTK_TREE_VIEW(ctrl
->GtkGetTreeView());
5081 // is there possibly a better suited function to get the column?
5082 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5084 (int) point
.x
, (int) point
.y
,
5085 pathScratch
.ByRef(),
5086 &GtkColumn
, // here we get the GtkColumn
5090 if ( GtkColumn
!= NULL
)
5092 // we got GTK column
5093 // the right call now which takes the header into account
5094 gtk_tree_view_get_dest_row_at_pos( treeView
, (int) point
.x
, (int) point
.y
, path
.ByRef(), &pos
);
5097 item
= wxDataViewItem(GTKPathToItem(path
));
5098 // else we got a GTK column but the position is not over an item, e.g. below last item
5099 for ( unsigned int i
=0, cols
=GetColumnCount(); i
<cols
; ++i
) // search the wx column
5101 wxDataViewColumn
* col
= GetColumn(i
);
5102 if ( GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == GtkColumn
)
5104 column
= col
; // here we get the wx column
5109 // else no column and thus no item, both null
5113 wxDataViewCtrl::GetItemRect(const wxDataViewItem
& WXUNUSED(item
),
5114 const wxDataViewColumn
*WXUNUSED(column
)) const
5119 bool wxDataViewCtrl::SetRowHeight(int rowHeight
)
5121 m_uniformRowHeight
= rowHeight
;
5125 void wxDataViewCtrl::DoSetExpanderColumn()
5127 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
5128 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5131 void wxDataViewCtrl::DoSetIndent()
5135 void wxDataViewCtrl::GtkDisableSelectionEvents()
5137 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5138 g_signal_handlers_disconnect_by_func( selection
,
5139 (gpointer
) (wxdataview_selection_changed_callback
), this);
5142 void wxDataViewCtrl::GtkEnableSelectionEvents()
5144 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
5145 g_signal_connect_after (selection
, "changed",
5146 G_CALLBACK (wxdataview_selection_changed_callback
), this);
5149 // ----------------------------------------------------------------------------
5150 // visual attributes stuff
5151 // ----------------------------------------------------------------------------
5155 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
5157 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
5160 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
5162 wxDataViewCtrlBase::DoApplyWidgetStyle(style
);
5163 gtk_widget_modify_style(m_treeview
, style
);
5166 #endif // !wxUSE_GENERICDATAVIEWCTRL
5168 #endif // wxUSE_DATAVIEWCTRL