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"
25 #include "wx/settings.h"
29 #include "wx/stockitem.h"
30 #include "wx/calctrl.h"
31 #include "wx/popupwin.h"
32 #include "wx/listimpl.cpp"
34 #include "wx/gtk/private.h"
36 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 class wxDataViewCtrlInternal
;
41 wxDataViewCtrlInternal
*g_internal
= NULL
;
43 class wxGtkTreeModelNode
;
46 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
49 //-----------------------------------------------------------------------------
50 // wxDataViewCtrlInternal
51 //-----------------------------------------------------------------------------
53 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
54 WX_DEFINE_LIST(ItemList
)
56 class wxDataViewCtrlInternal
59 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
, GtkWxTreeModel
*owner
);
60 ~wxDataViewCtrlInternal();
62 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
63 GtkTreePath
*get_path( GtkTreeIter
*iter
);
64 gboolean
iter_next( GtkTreeIter
*iter
);
65 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
66 gboolean
iter_has_child( GtkTreeIter
*iter
);
67 gint
iter_n_children( GtkTreeIter
*iter
);
68 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
69 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
71 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
72 wxDataViewCtrl
* GetOwner() { return m_owner
; }
73 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
75 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
76 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
77 bool ItemChanged( const wxDataViewItem
&item
);
78 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
82 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
83 GtkSortType
GetSortOrder() { return m_sort_order
; }
85 void SetSortColumn( int column
) { m_sort_column
= column
; }
86 int GetSortColumn() { return m_sort_column
; }
88 void SetDataViewSortColumn( wxDataViewColumn
*column
) { m_dataview_sort_column
= column
; }
89 wxDataViewColumn
*GetDataViewSortColumn() { return m_dataview_sort_column
; }
91 bool IsSorted() { return (m_sort_column
>= 0); }
96 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
97 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
98 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
99 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
100 void BuildBranch( wxGtkTreeModelNode
*branch
);
103 wxGtkTreeModelNode
*m_root
;
104 wxDataViewModel
*m_wx_model
;
105 GtkWxTreeModel
*m_gtk_model
;
106 wxDataViewCtrl
*m_owner
;
107 GtkSortType m_sort_order
;
108 wxDataViewColumn
*m_dataview_sort_column
;
113 //-----------------------------------------------------------------------------
114 // wxGtkTreeModelNode
115 //-----------------------------------------------------------------------------
117 int LINKAGEMODE
wxGtkTreeModelChildCmp( void** id1
, void** id2
)
119 int ret
= g_internal
->GetDataViewModel()->Compare( *id1
, *id2
,
120 g_internal
->GetSortColumn(), (g_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
125 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
126 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren
);
128 class wxGtkTreeModelNode
131 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
132 wxDataViewCtrlInternal
*internal
)
136 m_internal
= internal
;
139 ~wxGtkTreeModelNode()
141 size_t count
= m_children
.GetCount();
143 for (i
= 0; i
< count
; i
++)
145 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
150 unsigned int AddNode( wxGtkTreeModelNode
* child
)
152 m_nodes
.Add( child
);
154 void *id
= child
->GetItem().GetID();
156 m_children
.Add( id
);
158 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
160 g_internal
= m_internal
;
161 m_children
.Sort( &wxGtkTreeModelChildCmp
);
162 return m_children
.Index( id
);
165 return m_children
.GetCount()-1;
168 unsigned int AddLeave( void* id
)
170 m_children
.Add( id
);
172 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
174 g_internal
= m_internal
;
175 m_children
.Sort( &wxGtkTreeModelChildCmp
);
176 return m_children
.Index( id
);
179 return m_children
.GetCount()-1;
182 void DeleteChild( void* id
)
184 m_children
.Remove( id
);
186 unsigned int count
= m_nodes
.GetCount();
188 for (pos
= 0; pos
< count
; pos
++)
190 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
191 if (node
->GetItem().GetID() == id
)
193 m_nodes
.RemoveAt( pos
);
201 wxGtkTreeModelNode
* GetParent()
203 wxGtkTreeModelNodes
&GetNodes()
205 wxGtkTreeModelChildren
&GetChildren()
206 { return m_children
; }
208 unsigned int GetChildCount() { return m_children
.GetCount(); }
209 unsigned int GetNodesCount() { return m_nodes
.GetCount(); }
211 wxDataViewItem
&GetItem() { return m_item
; }
212 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
217 wxGtkTreeModelNode
*m_parent
;
218 wxGtkTreeModelNodes m_nodes
;
219 wxGtkTreeModelChildren m_children
;
220 wxDataViewItem m_item
;
221 wxDataViewCtrlInternal
*m_internal
;
225 //-----------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------
229 extern bool g_blockEventsOnDrag
;
231 //-----------------------------------------------------------------------------
232 // define new GTK+ class wxGtkTreeModel
233 //-----------------------------------------------------------------------------
237 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
238 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
239 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
240 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
241 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
242 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
244 GType
gtk_wx_tree_model_get_type (void);
246 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
248 struct _GtkWxTreeModel
254 wxDataViewCtrlInternal
*internal
;
257 struct _GtkWxTreeModelClass
259 GObjectClass list_parent_class
;
262 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
263 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
264 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
265 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
266 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
267 static void wxgtk_tree_model_finalize (GObject
*object
);
268 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
269 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
270 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
272 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
275 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
277 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
281 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
283 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
285 GtkTreeIter
*parent
);
286 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
288 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
290 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
294 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
299 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
300 gint
*sort_column_id
,
302 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
305 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
307 GtkTreeIterCompareFunc func
,
309 GtkDestroyNotify destroy
);
310 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
311 GtkTreeIterCompareFunc func
,
313 GtkDestroyNotify destroy
);
314 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
318 static GObjectClass
*list_parent_class
= NULL
;
321 gtk_wx_tree_model_get_type (void)
323 static GType tree_model_type
= 0;
325 if (!tree_model_type
)
327 const GTypeInfo tree_model_info
=
329 sizeof (GtkWxTreeModelClass
),
330 NULL
, /* base_init */
331 NULL
, /* base_finalize */
332 (GClassInitFunc
) wxgtk_tree_model_class_init
,
333 NULL
, /* class_finalize */
334 NULL
, /* class_data */
335 sizeof (GtkWxTreeModel
),
337 (GInstanceInitFunc
) wxgtk_tree_model_init
,
340 static const GInterfaceInfo tree_model_iface_info
=
342 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
347 static const GInterfaceInfo sortable_iface_info
=
349 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
354 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
355 &tree_model_info
, (GTypeFlags
)0 );
357 g_type_add_interface_static (tree_model_type
,
359 &tree_model_iface_info
);
360 g_type_add_interface_static (tree_model_type
,
361 GTK_TYPE_TREE_SORTABLE
,
362 &sortable_iface_info
);
365 return tree_model_type
;
368 static GtkWxTreeModel
*
369 wxgtk_tree_model_new(void)
371 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
376 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
378 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
379 GObjectClass
*object_class
= (GObjectClass
*) klass
;
380 object_class
->finalize
= wxgtk_tree_model_finalize
;
384 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
386 iface
->get_flags
= wxgtk_tree_model_get_flags
;
387 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
388 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
389 iface
->get_iter
= wxgtk_tree_model_get_iter
;
390 iface
->get_path
= wxgtk_tree_model_get_path
;
391 iface
->get_value
= wxgtk_tree_model_get_value
;
392 iface
->iter_next
= wxgtk_tree_model_iter_next
;
393 iface
->iter_children
= wxgtk_tree_model_iter_children
;
394 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
395 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
396 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
397 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
401 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
403 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
404 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
405 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
406 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
407 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
411 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
413 tree_model
->internal
= NULL
;
414 tree_model
->stamp
= g_random_int();
418 wxgtk_tree_model_finalize (GObject
*object
)
421 (* list_parent_class
->finalize
) (object
);
426 //-----------------------------------------------------------------------------
427 // implement callbacks from wxGtkTreeModel class by letting
428 // them call the methods of wxWidgets' wxDataViewModel
429 //-----------------------------------------------------------------------------
431 static GtkTreeModelFlags
432 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
434 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model
), (GtkTreeModelFlags
)0 );
436 return GTK_TREE_MODEL_ITERS_PERSIST
;
440 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
442 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
443 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
445 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
449 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
452 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
453 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
455 GType gtype
= G_TYPE_INVALID
;
457 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
459 if (wxtype
== wxT("string"))
460 gtype
= G_TYPE_STRING
;
463 gtype
= G_TYPE_STRING
;
464 // wxFAIL_MSG( _T("non-string columns not supported yet") );
471 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
475 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
476 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
477 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
479 return wxtree_model
->internal
->get_iter( iter
, path
);
483 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
486 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
487 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
488 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
490 return wxtree_model
->internal
->get_path( iter
);
494 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
499 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
500 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
502 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
503 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
504 if (mtype
== wxT("string"))
507 g_value_init( value
, G_TYPE_STRING
);
508 wxDataViewItem
item( (void*) iter
->user_data
);
509 model
->GetValue( variant
, item
, (unsigned int) column
);
511 g_value_set_string( value
, variant
.GetString().utf8_str() );
515 wxFAIL_MSG( _T("non-string columns not supported yet") );
520 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
523 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
524 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
525 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
527 return wxtree_model
->internal
->iter_next( iter
);
531 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
535 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
536 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
537 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
539 return wxtree_model
->internal
->iter_children( iter
, parent
);
543 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
546 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
547 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
548 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
550 return wxtree_model
->internal
->iter_has_child( iter
);
554 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
557 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
558 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
559 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, 0);
561 return wxtree_model
->internal
->iter_n_children( iter
);
565 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
570 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
571 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
573 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
577 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
581 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
582 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
583 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
585 return wxtree_model
->internal
->iter_parent( iter
, child
);
589 gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
590 gint
*sort_column_id
,
593 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
595 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
597 if (!tree_model
->internal
->IsSorted())
600 *sort_column_id
= -1;
607 *sort_column_id
= tree_model
->internal
->GetSortColumn();
610 *order
= tree_model
->internal
->GetSortOrder();
615 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
617 void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
621 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
622 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
624 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
626 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
627 (order
!= tree_model
->internal
->GetSortOrder()))
629 tree_model
->internal
->SetSortColumn( sort_column_id
);
630 tree_model
->internal
->SetSortOrder( order
);
632 gtk_tree_sortable_sort_column_changed (sortable
);
634 tree_model
->internal
->GetDataViewModel()->Resort();
637 if (gs_lastLeftClickHeader
)
639 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
640 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
641 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
642 event
.SetModel( dv
->GetModel() );
643 dv
->GetEventHandler()->ProcessEvent( event
);
646 gs_lastLeftClickHeader
= NULL
;
649 void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
651 GtkTreeIterCompareFunc func
,
653 GtkDestroyNotify destroy
)
655 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
656 g_return_if_fail (func
!= NULL
);
659 void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
660 GtkTreeIterCompareFunc func
,
662 GtkDestroyNotify destroy
)
664 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
665 g_return_if_fail (func
!= NULL
);
667 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
670 gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
675 //-----------------------------------------------------------------------------
676 // define new GTK+ class wxGtkRendererRenderer
677 //-----------------------------------------------------------------------------
681 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
682 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
683 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
684 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
685 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
686 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
688 GType
gtk_wx_cell_renderer_get_type (void);
690 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
691 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
693 struct _GtkWxCellRenderer
695 GtkCellRenderer parent
;
698 wxDataViewCustomRenderer
*cell
;
702 struct _GtkWxCellRendererClass
704 GtkCellRendererClass cell_parent_class
;
708 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
709 static void gtk_wx_cell_renderer_init (
710 GtkWxCellRenderer
*cell
);
711 static void gtk_wx_cell_renderer_class_init(
712 GtkWxCellRendererClass
*klass
);
713 static void gtk_wx_cell_renderer_finalize (
715 static void gtk_wx_cell_renderer_get_size (
716 GtkCellRenderer
*cell
,
718 GdkRectangle
*rectangle
,
723 static void gtk_wx_cell_renderer_render (
724 GtkCellRenderer
*cell
,
727 GdkRectangle
*background_area
,
728 GdkRectangle
*cell_area
,
729 GdkRectangle
*expose_area
,
730 GtkCellRendererState flags
);
731 static gboolean
gtk_wx_cell_renderer_activate(
732 GtkCellRenderer
*cell
,
736 GdkRectangle
*background_area
,
737 GdkRectangle
*cell_area
,
738 GtkCellRendererState flags
);
739 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
740 GtkCellRenderer
*cell
,
744 GdkRectangle
*background_area
,
745 GdkRectangle
*cell_area
,
746 GtkCellRendererState flags
);
749 static GObjectClass
*cell_parent_class
= NULL
;
754 gtk_wx_cell_renderer_get_type (void)
756 static GType cell_wx_type
= 0;
760 const GTypeInfo cell_wx_info
=
762 sizeof (GtkWxCellRendererClass
),
763 NULL
, /* base_init */
764 NULL
, /* base_finalize */
765 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
766 NULL
, /* class_finalize */
767 NULL
, /* class_data */
768 sizeof (GtkWxCellRenderer
),
770 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
773 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
774 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
781 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
784 cell
->last_click
= 0;
788 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
790 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
791 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
793 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
795 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
797 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
798 cell_class
->render
= gtk_wx_cell_renderer_render
;
799 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
800 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
804 gtk_wx_cell_renderer_finalize (GObject
*object
)
807 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
811 gtk_wx_cell_renderer_new (void)
813 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
818 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
819 GtkCellRenderer
*renderer
,
823 GdkRectangle
*background_area
,
824 GdkRectangle
*cell_area
,
825 GtkCellRendererState flags
)
827 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
828 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
829 if (!cell
->HasEditorCtrl())
833 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
839 rect
.x
+= cell_area
->x
;
840 rect
.y
+= cell_area
->y
;
841 // rect.width -= renderer->xpad * 2;
842 // rect.height -= renderer->ypad * 2;
844 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
845 wxRect
renderrect( cell_area
->x
, cell_area
->y
, cell_area
->width
, cell_area
->height
);
847 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
849 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
850 wxDataViewItem
item( (void*) iter
.user_data
);
851 gtk_tree_path_free( treepath
);
853 cell
->StartEditing( item
, renderrect
);
859 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
861 GdkRectangle
*cell_area
,
867 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
868 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
870 wxSize size
= cell
->GetSize();
872 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
873 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
880 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
884 *x_offset
= (gint
)((renderer
->xalign
*
885 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
886 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
890 *y_offset
= (gint
)((renderer
->yalign
*
891 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
892 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
900 *height
= calc_height
;
904 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
907 GdkRectangle
*background_area
,
908 GdkRectangle
*cell_area
,
909 GdkRectangle
*expose_area
,
910 GtkCellRendererState flags
)
913 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
914 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
916 cell
->window
= window
;
917 cell
->widget
= widget
;
918 cell
->background_area
= (void*) background_area
;
919 cell
->cell_area
= (void*) cell_area
;
920 cell
->expose_area
= (void*) expose_area
;
921 cell
->flags
= (int) flags
;
924 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
930 rect
.x
+= cell_area
->x
;
931 rect
.y
+= cell_area
->y
;
932 rect
.width
-= renderer
->xpad
* 2;
933 rect
.height
-= renderer
->ypad
* 2;
936 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
938 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
939 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
941 wxGTKWindowImplDC
*impldc
= (wxGTKWindowImplDC
*) dc
->GetImpl();
942 if (impldc
->m_window
== NULL
)
944 impldc
->m_window
= window
;
948 if (dc
->m_window
== NULL
)
950 dc
->m_window
= window
;
956 if (flags
& GTK_CELL_RENDERER_SELECTED
)
957 state
|= wxDATAVIEW_CELL_SELECTED
;
958 if (flags
& GTK_CELL_RENDERER_PRELIT
)
959 state
|= wxDATAVIEW_CELL_PRELIT
;
960 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
961 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
962 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
963 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
964 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
965 state
|= wxDATAVIEW_CELL_FOCUSED
;
966 cell
->Render( renderrect
, dc
, state
);
971 gtk_wx_cell_renderer_activate(
972 GtkCellRenderer
*renderer
,
976 GdkRectangle
*background_area
,
977 GdkRectangle
*cell_area
,
978 GtkCellRendererState flags
)
980 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
981 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
984 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
990 rect
.x
+= cell_area
->x
;
991 rect
.y
+= cell_area
->y
;
992 rect
.width
-= renderer
->xpad
* 2;
993 rect
.height
-= renderer
->ypad
* 2;
995 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
997 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
999 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
1002 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
1003 wxDataViewItem
item( iter
.user_data
);
1004 gtk_tree_path_free( treepath
);
1006 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1012 // activated by <ENTER>
1013 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1018 else if (event
->type
== GDK_BUTTON_PRESS
)
1020 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
1021 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
1022 ((int) button_event
->y
) - renderrect
.y
);
1025 if (button_event
->button
== 1)
1027 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
1029 // TODO: query system double-click time
1030 if (button_event
->time
- wxrenderer
->last_click
< 400)
1031 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1034 if (button_event
->button
== 3)
1036 if (cell
->RightClick( pt
, renderrect
, model
, item
, model_col
))
1040 wxrenderer
->last_click
= button_event
->time
;
1048 // ---------------------------------------------------------
1049 // wxGtkDataViewModelNotifier
1050 // ---------------------------------------------------------
1052 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1055 wxGtkDataViewModelNotifier( GtkWxTreeModel
*wxgtk_model
,
1056 wxDataViewModel
*wx_model
,
1057 wxDataViewCtrl
*ctrl
);
1058 ~wxGtkDataViewModelNotifier();
1060 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1061 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1062 virtual bool ItemChanged( const wxDataViewItem
&item
);
1063 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
1064 virtual bool Cleared();
1065 virtual void Resort();
1067 GtkWxTreeModel
*m_wxgtk_model
;
1068 wxDataViewModel
*m_wx_model
;
1069 wxDataViewCtrl
*m_owner
;
1072 // ---------------------------------------------------------
1073 // wxGtkDataViewListModelNotifier
1074 // ---------------------------------------------------------
1076 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1077 GtkWxTreeModel
* wxgtk_model
, wxDataViewModel
*wx_model
,
1078 wxDataViewCtrl
*ctrl
)
1080 m_wxgtk_model
= wxgtk_model
;
1081 m_wx_model
= wx_model
;
1085 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1088 m_wxgtk_model
= NULL
;
1091 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1093 m_owner
->GtkGetInternal()->ItemAdded( parent
, item
);
1096 iter
.stamp
= m_wxgtk_model
->stamp
;
1097 iter
.user_data
= (gpointer
) item
.GetID();
1099 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1100 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1101 gtk_tree_model_row_inserted(
1102 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1103 gtk_tree_path_free (path
);
1108 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1111 iter
.stamp
= m_wxgtk_model
->stamp
;
1112 iter
.user_data
= (gpointer
) item
.GetID();
1114 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1115 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1116 gtk_tree_model_row_deleted(
1117 GTK_TREE_MODEL(m_wxgtk_model
), path
);
1118 gtk_tree_path_free (path
);
1120 m_owner
->GtkGetInternal()->ItemDeleted( parent
, item
);
1125 void wxGtkDataViewModelNotifier::Resort()
1127 m_owner
->GtkGetInternal()->Resort();
1130 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1133 iter
.stamp
= m_wxgtk_model
->stamp
;
1134 iter
.user_data
= (gpointer
) item
.GetID();
1136 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1137 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1138 gtk_tree_model_row_changed(
1139 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1140 gtk_tree_path_free (path
);
1142 m_owner
->GtkGetInternal()->ItemChanged( item
);
1147 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_col
)
1149 // This adds GTK+'s missing MVC logic for ValueChanged
1151 for (index
= 0; index
< m_owner
->GetColumnCount(); index
++)
1153 wxDataViewColumn
*column
= m_owner
->GetColumn( index
);
1154 if (column
->GetModelColumn() == model_col
)
1156 GtkTreeView
*widget
= GTK_TREE_VIEW(m_owner
->m_treeview
);
1157 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1161 iter
.stamp
= m_wxgtk_model
->stamp
;
1162 iter
.user_data
= (gpointer
) item
.GetID();
1163 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1164 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1165 GdkRectangle cell_area
;
1166 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1167 gtk_tree_path_free( path
);
1169 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1170 double d
= gtk_adjustment_get_value( hadjust
);
1171 int xdiff
= (int) d
;
1173 int ydiff
= gcolumn
->button
->allocation
.height
;
1175 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1176 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1178 m_owner
->GtkGetInternal()->ValueChanged( item
, model_col
);
1187 bool wxGtkDataViewModelNotifier::Cleared()
1189 // TODO: delete everything
1191 m_owner
->GtkGetInternal()->Cleared();
1196 // ---------------------------------------------------------
1197 // wxDataViewRenderer
1198 // ---------------------------------------------------------
1200 static gpointer s_user_data
= NULL
;
1203 wxgtk_cell_editable_editing_done( GtkCellEditable
*editable
,
1204 wxDataViewRenderer
*wxrenderer
)
1206 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1207 wxDataViewCtrl
*dv
= column
->GetOwner();
1208 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1209 event
.SetDataViewColumn( column
);
1210 event
.SetModel( dv
->GetModel() );
1211 wxDataViewItem
item( s_user_data
);
1212 event
.SetItem( item
);
1213 dv
->GetEventHandler()->ProcessEvent( event
);
1217 wxgtk_renderer_editing_started( GtkCellRenderer
*cell
, GtkCellEditable
*editable
,
1218 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1220 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1221 wxDataViewCtrl
*dv
= column
->GetOwner();
1222 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1223 event
.SetDataViewColumn( column
);
1224 event
.SetModel( dv
->GetModel() );
1225 GtkTreePath
*tree_path
= gtk_tree_path_new_from_string( path
);
1227 dv
->GtkGetInternal()->get_iter( &iter
, tree_path
);
1228 gtk_tree_path_free( tree_path
);
1229 wxDataViewItem
item( iter
.user_data
);
1230 event
.SetItem( item
);
1231 dv
->GetEventHandler()->ProcessEvent( event
);
1233 if (GTK_IS_CELL_EDITABLE(editable
))
1235 s_user_data
= iter
.user_data
;
1237 g_signal_connect (GTK_CELL_EDITABLE (editable
), "editing_done",
1238 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1239 (gpointer
) wxrenderer
);
1245 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1247 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1249 wxDataViewRendererBase( varianttype
, mode
, align
)
1253 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1254 // after the m_renderer pointer has been initialized
1257 void wxDataViewRenderer::GtkInitHandlers()
1259 if (!gtk_check_version(2,6,0))
1261 g_signal_connect (GTK_CELL_RENDERER(m_renderer
), "editing_started",
1262 G_CALLBACK (wxgtk_renderer_editing_started
),
1267 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1269 GtkCellRendererMode gtkMode
;
1272 case wxDATAVIEW_CELL_INERT
:
1273 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1275 case wxDATAVIEW_CELL_ACTIVATABLE
:
1276 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1278 case wxDATAVIEW_CELL_EDITABLE
:
1279 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1283 // This value is most often ignored in GtkTreeView
1284 GValue gvalue
= { 0, };
1285 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1286 g_value_set_enum( &gvalue
, gtkMode
);
1287 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1288 g_value_unset( &gvalue
);
1291 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1293 wxDataViewCellMode ret
;
1296 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1298 switch (g_value_get_enum(&gvalue
))
1300 case GTK_CELL_RENDERER_MODE_INERT
:
1301 ret
= wxDATAVIEW_CELL_INERT
;
1303 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1304 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1306 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1307 ret
= wxDATAVIEW_CELL_EDITABLE
;
1311 g_value_unset( &gvalue
);
1316 void wxDataViewRenderer::SetAlignment( int align
)
1318 // horizontal alignment:
1320 gfloat xalign
= 0.0;
1321 if (align
& wxALIGN_RIGHT
)
1323 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1326 GValue gvalue
= { 0, };
1327 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1328 g_value_set_float( &gvalue
, xalign
);
1329 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1330 g_value_unset( &gvalue
);
1332 // vertical alignment:
1334 gfloat yalign
= 0.0;
1335 if (align
& wxALIGN_BOTTOM
)
1337 else if (align
& wxALIGN_CENTER_VERTICAL
)
1340 GValue gvalue2
= { 0, };
1341 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1342 g_value_set_float( &gvalue2
, yalign
);
1343 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1344 g_value_unset( &gvalue2
);
1347 int wxDataViewRenderer::GetAlignment() const
1352 // horizontal alignment:
1354 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
1355 float xalign
= g_value_get_float( &gvalue
);
1357 ret
|= wxALIGN_LEFT
;
1358 else if (xalign
== 0.5)
1359 ret
|= wxALIGN_CENTER_HORIZONTAL
;
1361 ret
|= wxALIGN_RIGHT
;
1362 g_value_unset( &gvalue
);
1365 // vertical alignment:
1367 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
1368 float yalign
= g_value_get_float( &gvalue
);
1371 else if (yalign
== 0.5)
1372 ret
|= wxALIGN_CENTER_VERTICAL
;
1374 ret
|= wxALIGN_BOTTOM
;
1375 g_value_unset( &gvalue
);
1382 // ---------------------------------------------------------
1383 // wxDataViewTextRenderer
1384 // ---------------------------------------------------------
1387 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1388 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1391 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1392 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1394 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1396 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1397 wxVariant value
= tmp
;
1398 if (!cell
->Validate( value
))
1401 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1403 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1405 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, path
);
1406 wxDataViewItem
item( (void*) iter
.user_data
);;
1407 gtk_tree_path_free( path
);
1409 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1411 model
->SetValue( value
, item
, model_col
);
1412 model
->ValueChanged( item
, model_col
);
1415 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1417 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1419 wxDataViewRenderer( varianttype
, mode
, align
)
1421 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_text_new();
1423 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1425 GValue gvalue
= { 0, };
1426 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1427 g_value_set_boolean( &gvalue
, true );
1428 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1429 g_value_unset( &gvalue
);
1431 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1437 SetAlignment(align
);
1440 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1442 wxString tmp
= value
;
1444 GValue gvalue
= { 0, };
1445 g_value_init( &gvalue
, G_TYPE_STRING
);
1446 g_value_set_string( &gvalue
, wxGTK_CONV( tmp
) );
1447 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1448 g_value_unset( &gvalue
);
1453 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1455 GValue gvalue
= { 0, };
1456 g_value_init( &gvalue
, G_TYPE_STRING
);
1457 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1458 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &gvalue
) );
1459 g_value_unset( &gvalue
);
1466 void wxDataViewTextRenderer::SetAlignment( int align
)
1468 wxDataViewRenderer::SetAlignment(align
);
1470 if (gtk_check_version(2,10,0))
1473 // horizontal alignment:
1474 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1475 if (align
& wxALIGN_RIGHT
)
1476 pangoAlign
= PANGO_ALIGN_RIGHT
;
1477 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1478 pangoAlign
= PANGO_ALIGN_CENTER
;
1480 GValue gvalue
= { 0, };
1481 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1482 g_value_set_enum( &gvalue
, pangoAlign
);
1483 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1484 g_value_unset( &gvalue
);
1487 // ---------------------------------------------------------
1488 // wxDataViewTextRendererAttr
1489 // ---------------------------------------------------------
1491 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
,wxDataViewTextRenderer
)
1493 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
1494 wxDataViewCellMode mode
, int align
) :
1495 wxDataViewTextRenderer( varianttype
, mode
, align
)
1499 // ---------------------------------------------------------
1500 // wxDataViewBitmapRenderer
1501 // ---------------------------------------------------------
1503 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1505 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1507 wxDataViewRenderer( varianttype
, mode
, align
)
1509 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_pixbuf_new();
1512 SetAlignment(align
);
1515 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1517 if (value
.GetType() == wxT("wxBitmap"))
1522 // This may create a Pixbuf representation in the
1523 // wxBitmap object (and it will stay there)
1524 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1526 GValue gvalue
= { 0, };
1527 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1528 g_value_set_object( &gvalue
, pixbuf
);
1529 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1530 g_value_unset( &gvalue
);
1535 if (value
.GetType() == wxT("wxIcon"))
1540 // This may create a Pixbuf representation in the
1541 // wxBitmap object (and it will stay there)
1542 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1544 GValue gvalue
= { 0, };
1545 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1546 g_value_set_object( &gvalue
, pixbuf
);
1547 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1548 g_value_unset( &gvalue
);
1556 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1561 // ---------------------------------------------------------
1562 // wxDataViewToggleRenderer
1563 // ---------------------------------------------------------
1566 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1567 gchar
*path
, gpointer user_data
);
1570 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1571 gchar
*path
, gpointer user_data
)
1573 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1576 GValue gvalue
= { 0, };
1577 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1578 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1579 bool tmp
= g_value_get_boolean( &gvalue
);
1580 g_value_unset( &gvalue
);
1584 wxVariant value
= tmp
;
1585 if (!cell
->Validate( value
))
1588 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1590 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1592 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, gtk_path
);
1593 wxDataViewItem
item( (void*) iter
.user_data
);;
1594 gtk_tree_path_free( gtk_path
);
1596 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1598 model
->SetValue( value
, item
, model_col
);
1599 model
->ValueChanged( item
, model_col
);
1602 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1604 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1605 wxDataViewCellMode mode
, int align
) :
1606 wxDataViewRenderer( varianttype
, mode
, align
)
1608 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
1610 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1612 g_signal_connect_after( m_renderer
, "toggled",
1613 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1617 GValue gvalue
= { 0, };
1618 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1619 g_value_set_boolean( &gvalue
, false );
1620 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1621 g_value_unset( &gvalue
);
1625 SetAlignment(align
);
1628 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1632 GValue gvalue
= { 0, };
1633 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1634 g_value_set_boolean( &gvalue
, tmp
);
1635 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1636 g_value_unset( &gvalue
);
1641 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1643 GValue gvalue
= { 0, };
1644 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1645 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1646 bool tmp
= g_value_get_boolean( &gvalue
);
1647 g_value_unset( &gvalue
);
1654 // ---------------------------------------------------------
1655 // wxDataViewCustomRenderer
1656 // ---------------------------------------------------------
1658 class wxDataViewCtrlDC
: public wxWindowDC
1661 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1664 wxGTKWindowImplDC
*impl
= (wxGTKWindowImplDC
*) GetImpl();
1666 GtkWidget
*widget
= window
->m_treeview
;
1668 impl
->m_window
= NULL
;
1670 impl
->m_context
= window
->GtkGetPangoDefaultContext();
1671 impl
->m_layout
= pango_layout_new( impl
->m_context
);
1672 impl
->m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1674 impl
->m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1677 GtkWidget
*widget
= window
->m_treeview
;
1681 m_context
= window
->GtkGetPangoDefaultContext();
1682 m_layout
= pango_layout_new( m_context
);
1683 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1685 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1687 // Set m_window later
1689 // m_owner = window;
1693 // ---------------------------------------------------------
1694 // wxDataViewCustomRenderer
1695 // ---------------------------------------------------------
1697 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1699 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1700 wxDataViewCellMode mode
, int align
,
1702 wxDataViewRenderer( varianttype
, mode
, align
)
1705 m_text_renderer
= NULL
;
1713 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
1716 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
1717 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
1718 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
1719 view
->GetForegroundColour();
1720 dc
->SetTextForeground(col
);
1721 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
1723 if (!m_text_renderer
)
1724 m_text_renderer
= gtk_cell_renderer_text_new();
1726 GValue gvalue
= { 0, };
1727 g_value_init( &gvalue
, G_TYPE_STRING
);
1728 g_value_set_string( &gvalue
, wxGTK_CONV( text
) );
1729 g_object_set_property( G_OBJECT(m_text_renderer
), "text", &gvalue
);
1730 g_value_unset( &gvalue
);
1732 ((GdkRectangle
*) cell_area
)->x
+= xoffset
;
1733 ((GdkRectangle
*) cell_area
)->width
-= xoffset
;
1735 gtk_cell_renderer_render( m_text_renderer
,
1738 (GdkRectangle
*) background_area
,
1739 (GdkRectangle
*) cell_area
,
1740 (GdkRectangle
*) expose_area
,
1741 (GtkCellRendererState
) flags
);
1743 ((GdkRectangle
*) cell_area
)->x
-= xoffset
;
1744 ((GdkRectangle
*) cell_area
)->width
+= xoffset
;
1748 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1750 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1751 renderer
->cell
= this;
1753 m_renderer
= (GtkCellRenderer
*) renderer
;
1756 SetAlignment(align
);
1763 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1768 if (m_text_renderer
)
1769 gtk_object_sink( GTK_OBJECT(m_text_renderer
) );
1772 wxDC
*wxDataViewCustomRenderer::GetDC()
1776 if (GetOwner() == NULL
)
1778 if (GetOwner()->GetOwner() == NULL
)
1780 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1786 // ---------------------------------------------------------
1787 // wxDataViewProgressRenderer
1788 // ---------------------------------------------------------
1790 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1792 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1793 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1794 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1800 if (!gtk_check_version(2,6,0))
1802 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
1804 GValue gvalue
= { 0, };
1805 g_value_init( &gvalue
, G_TYPE_STRING
);
1807 g_value_set_string( &gvalue
, wxGTK_CONV(m_label
) );
1808 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1809 g_value_unset( &gvalue
);
1812 SetAlignment(align
);
1817 // Use custom cell code
1818 wxDataViewCustomRenderer::Init(mode
, align
);
1822 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1826 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1829 if (!gtk_check_version(2,6,0))
1831 gint tmp
= (long) value
;
1832 GValue gvalue
= { 0, };
1833 g_value_init( &gvalue
, G_TYPE_INT
);
1834 g_value_set_int( &gvalue
, tmp
);
1835 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1836 g_value_unset( &gvalue
);
1841 m_value
= (long) value
;
1843 if (m_value
< 0) m_value
= 0;
1844 if (m_value
> 100) m_value
= 100;
1850 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1855 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1857 double pct
= (double)m_value
/ 100.0;
1859 bar
.width
= (int)(cell
.width
* pct
);
1860 dc
->SetPen( *wxTRANSPARENT_PEN
);
1861 dc
->SetBrush( *wxBLUE_BRUSH
);
1862 dc
->DrawRectangle( bar
);
1864 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1865 dc
->SetPen( *wxBLACK_PEN
);
1866 dc
->DrawRectangle( cell
);
1871 wxSize
wxDataViewProgressRenderer::GetSize() const
1873 return wxSize(40,12);
1876 // ---------------------------------------------------------
1877 // wxDataViewDateRenderer
1878 // ---------------------------------------------------------
1880 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1883 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1884 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
1885 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1890 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1891 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1892 sizer
->Add( m_cal
, 1, wxGROW
);
1897 virtual void OnDismiss()
1901 void OnCalendar( wxCalendarEvent
&event
);
1903 wxCalendarCtrl
*m_cal
;
1904 wxDataViewModel
*m_model
;
1905 wxDataViewItem m_item
;
1909 DECLARE_EVENT_TABLE()
1912 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1913 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1916 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1918 wxDateTime date
= event
.GetDate();
1919 wxVariant value
= date
;
1920 m_model
->SetValue( value
, m_item
, m_col
);
1921 m_model
->ValueChanged( m_item
, m_col
);
1925 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1927 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1928 wxDataViewCellMode mode
, int align
) :
1929 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1932 SetAlignment(align
);
1935 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1937 m_date
= value
.GetDateTime();
1942 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1947 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1949 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1950 wxString tmp
= m_date
.FormatDate();
1951 RenderText( tmp
, 0, cell
, dc
, state
);
1955 wxSize
wxDataViewDateRenderer::GetSize() const
1957 wxString tmp
= m_date
.FormatDate();
1959 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1960 return wxSize(x
,y
+d
);
1963 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewModel
*model
,
1964 const wxDataViewItem
&item
, unsigned int col
)
1967 model
->GetValue( variant
, item
, col
);
1968 wxDateTime value
= variant
.GetDateTime();
1970 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1971 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1972 wxPoint pos
= wxGetMousePosition();
1975 popup
->Popup( popup
->m_cal
);
1981 // ---------------------------------------------------------
1982 // wxDataViewIconTextRenderer
1983 // ---------------------------------------------------------
1985 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1987 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1988 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1989 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1992 SetAlignment(align
);
1995 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1999 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
2005 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
2010 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
2012 const wxIcon
&icon
= m_value
.GetIcon();
2016 int yoffset
= wxMax( 0, (cell
.height
- icon
.GetHeight()) / 2 );
2017 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ yoffset
);
2018 offset
= icon
.GetWidth() + 4;
2021 RenderText( m_value
.GetText(), offset
, cell
, dc
, state
);
2026 wxSize
wxDataViewIconTextRenderer::GetSize() const
2029 if (m_value
.GetIcon().IsOk())
2030 size
.x
= 4 + m_value
.GetIcon().GetWidth();
2032 GetView()->GetTextExtent( m_value
.GetText(), &x
, &y
, &d
);
2038 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
2043 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
2048 // ---------------------------------------------------------
2050 // ---------------------------------------------------------
2054 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
2055 GdkEventButton
*gdk_event
,
2056 wxDataViewColumn
*column
)
2058 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
2061 if (gdk_event
->button
== 1)
2063 gs_lastLeftClickHeader
= column
;
2065 wxDataViewCtrl
*dv
= column
->GetOwner();
2066 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2067 event
.SetDataViewColumn( column
);
2068 event
.SetModel( dv
->GetModel() );
2069 if (dv
->GetEventHandler()->ProcessEvent( event
))
2073 if (gdk_event
->button
== 3)
2075 wxDataViewCtrl
*dv
= column
->GetOwner();
2076 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2077 event
.SetDataViewColumn( column
);
2078 event
.SetModel( dv
->GetModel() );
2079 if (dv
->GetEventHandler()->ProcessEvent( event
))
2087 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
2088 GtkCellRenderer
*cell
,
2089 GtkTreeModel
*model
,
2095 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
2096 GtkCellRenderer
*renderer
,
2097 GtkTreeModel
*model
,
2101 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2102 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2104 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2106 wxDataViewItem
item( (void*) iter
->user_data
);
2108 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2110 if (wx_model
->IsContainer( item
))
2112 if (wx_model
->HasContainerColumns( item
) || (cell
->GetOwner()->GetModelColumn() == 0))
2114 GValue gvalue
= { 0, };
2115 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2116 g_value_set_boolean( &gvalue
, TRUE
);
2117 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2118 g_value_unset( &gvalue
);
2122 GValue gvalue
= { 0, };
2123 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2124 g_value_set_boolean( &gvalue
, FALSE
);
2125 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2126 g_value_unset( &gvalue
);
2133 GValue gvalue
= { 0, };
2134 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2135 g_value_set_boolean( &gvalue
, TRUE
);
2136 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2137 g_value_unset( &gvalue
);
2141 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2143 if (value
.GetType() != cell
->GetVariantType())
2144 wxLogError( wxT("Wrong type, required: %s but: %s"),
2145 value
.GetType().c_str(),
2146 cell
->GetVariantType().c_str() );
2148 cell
->SetValue( value
);
2150 if (cell
->GtkHasAttributes())
2152 wxDataViewItemAttr attr
;
2153 bool colour_set
= false;
2154 bool style_set
= false;
2155 bool weight_set
= false;
2157 if (wx_model
->GetAttr( item
, cell
->GetOwner()->GetModelColumn(), attr
))
2159 // this must be a GtkCellRendererText
2160 wxColour colour
= attr
.GetColour();
2163 const GdkColor
* const gcol
= colour
.GetColor();
2165 GValue gvalue
= { 0, };
2166 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2167 g_value_set_boxed( &gvalue
, gcol
);
2168 g_object_set_property( G_OBJECT(renderer
), "foreground_gdk", &gvalue
);
2169 g_value_unset( &gvalue
);
2174 if (attr
.GetItalic())
2176 GValue gvalue
= { 0, };
2177 g_value_init( &gvalue
, PANGO_TYPE_STYLE
);
2178 g_value_set_enum( &gvalue
, PANGO_STYLE_ITALIC
);
2179 g_object_set_property( G_OBJECT(renderer
), "style", &gvalue
);
2180 g_value_unset( &gvalue
);
2187 GValue gvalue
= { 0, };
2188 g_value_init( &gvalue
, PANGO_TYPE_WEIGHT
);
2189 g_value_set_enum( &gvalue
, PANGO_WEIGHT_BOLD
);
2190 g_object_set_property( G_OBJECT(renderer
), "weight", &gvalue
);
2191 g_value_unset( &gvalue
);
2199 GValue gvalue
= { 0, };
2200 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2201 g_value_set_boolean( &gvalue
, FALSE
);
2202 g_object_set_property( G_OBJECT(renderer
), "style-set", &gvalue
);
2203 g_value_unset( &gvalue
);
2208 GValue gvalue
= { 0, };
2209 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2210 g_value_set_boolean( &gvalue
, FALSE
);
2211 g_object_set_property( G_OBJECT(renderer
), "weight-set", &gvalue
);
2212 g_value_unset( &gvalue
);
2217 GValue gvalue
= { 0, };
2218 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2219 g_value_set_boolean( &gvalue
, FALSE
);
2220 g_object_set_property( G_OBJECT(renderer
), "foreground-set", &gvalue
);
2221 g_value_unset( &gvalue
);
2226 if (attr
.HasBackgroundColour())
2228 wxColour colour
= attr
.GetBackgroundColour();
2229 const GdkColor
* const gcol
= colour
.GetColor();
2231 GValue gvalue
= { 0, };
2232 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2233 g_value_set_boxed( &gvalue
, gcol
);
2234 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2235 g_value_unset( &gvalue
);
2239 GValue gvalue
= { 0, };
2240 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2241 g_value_set_boolean( &gvalue
, FALSE
);
2242 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2243 g_value_unset( &gvalue
);
2249 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
2251 #include <wx/listimpl.cpp>
2252 WX_DEFINE_LIST(wxDataViewColumnList
)
2254 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
2255 unsigned int model_column
, int width
,
2256 wxAlignment align
, int flags
) :
2257 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
2259 Init( align
, flags
, width
);
2261 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
2265 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
2266 unsigned int model_column
, int width
,
2267 wxAlignment align
, int flags
) :
2268 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
2270 Init( align
, flags
, width
);
2272 SetBitmap( bitmap
);
2275 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
2277 m_isConnected
= false;
2279 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
2280 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
2281 m_column
= (GtkWidget
*) column
;
2284 SetAlignment( align
);
2286 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
2287 // as GTK+ is smart and unless explicitely told, will set the minimal
2288 // width to the title's lenght, which is a better default
2290 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
2291 // that we use for the wxDataViewCtrl
2292 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
2293 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
2295 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
2297 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
2298 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
2301 wxDataViewColumn::~wxDataViewColumn()
2305 void wxDataViewColumn::OnInternalIdle()
2310 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
2312 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2315 g_signal_connect(column
->button
, "button_press_event",
2316 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
2318 m_isConnected
= true;
2323 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
2325 wxDataViewColumnBase::SetOwner( owner
);
2327 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2329 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
2332 void wxDataViewColumn::SetTitle( const wxString
&title
)
2334 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2338 // disconnect before column->button gets recreated
2339 g_signal_handlers_disconnect_by_func( column
->button
,
2340 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
2342 m_isConnected
= false;
2345 // FIXME: can it really happen that we don't have the owner here??
2346 wxDataViewCtrl
*ctrl
= GetOwner();
2347 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
2348 : wxGTK_CONV_SYS(title
) );
2350 gtk_tree_view_column_set_widget( column
, NULL
);
2353 wxString
wxDataViewColumn::GetTitle() const
2355 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
2356 return wxConvFileName
->cMB2WX(str
);
2359 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
2361 wxDataViewColumnBase::SetBitmap( bitmap
);
2363 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2366 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
2368 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
2369 if (bitmap
.GetMask())
2370 mask
= bitmap
.GetMask()->GetBitmap();
2372 if (bitmap
.HasPixbuf())
2374 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
2375 bitmap
.GetPixbuf());
2379 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
2380 bitmap
.GetPixmap(), mask
);
2382 gtk_widget_show( GTK_WIDGET(gtk_image
) );
2384 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
2388 gtk_tree_view_column_set_widget( column
, NULL
);
2392 void wxDataViewColumn::SetHidden( bool hidden
)
2394 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
2397 void wxDataViewColumn::SetResizeable( bool resizeable
)
2399 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
2402 void wxDataViewColumn::SetAlignment( wxAlignment align
)
2404 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2406 gfloat xalign
= 0.0;
2407 if (align
== wxALIGN_RIGHT
)
2409 if (align
== wxALIGN_CENTER_HORIZONTAL
||
2410 align
== wxALIGN_CENTER
)
2413 gtk_tree_view_column_set_alignment( column
, xalign
);
2416 wxAlignment
wxDataViewColumn::GetAlignment() const
2418 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
2421 return wxALIGN_RIGHT
;
2423 return wxALIGN_CENTER_HORIZONTAL
;
2425 return wxALIGN_LEFT
;
2428 void wxDataViewColumn::SetSortable( bool sortable
)
2430 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2434 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
2438 gtk_tree_view_column_set_sort_column_id( column
, -1 );
2439 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
2443 bool wxDataViewColumn::IsSortable() const
2445 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2446 return (gtk_tree_view_column_get_sort_column_id( column
) != -1);
2449 bool wxDataViewColumn::IsResizeable() const
2451 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2452 return gtk_tree_view_column_get_resizable( column
);
2455 bool wxDataViewColumn::IsHidden() const
2457 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2458 return !gtk_tree_view_column_get_visible( column
);
2461 void wxDataViewColumn::SetSortOrder( bool ascending
)
2463 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2466 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
2468 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
2470 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
2473 bool wxDataViewColumn::IsSortOrderAscending() const
2475 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2477 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
2480 void wxDataViewColumn::SetMinWidth( int width
)
2482 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2485 int wxDataViewColumn::GetMinWidth() const
2487 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2490 int wxDataViewColumn::GetWidth() const
2492 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2495 void wxDataViewColumn::SetWidth( int width
)
2497 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2501 //-----------------------------------------------------------------------------
2502 // wxGtkTreeModelNode
2503 //-----------------------------------------------------------------------------
2505 void wxGtkTreeModelNode::Resort()
2507 size_t child_count
= GetChildCount();
2508 if (child_count
== 0)
2511 size_t node_count
= GetNodesCount();
2513 if (child_count
== 1)
2515 if (node_count
== 1)
2517 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
2523 wxGtkTreeModelChildren temp
;
2524 WX_APPEND_ARRAY( temp
, m_children
);
2526 g_internal
= m_internal
;
2527 m_children
.Sort( &wxGtkTreeModelChildCmp
);
2529 gint
*new_order
= new gint
[child_count
];
2532 for (pos
= 0; pos
< child_count
; pos
++)
2534 void *id
= m_children
.Item( pos
);
2535 int old_pos
= temp
.Index( id
);
2536 new_order
[pos
] = old_pos
;
2539 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
2542 iter
.user_data
= GetItem().GetID();
2543 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
2545 GtkTreePath
*path
= m_internal
->get_path( &iter
);
2547 gtk_tree_model_rows_reordered( gtk_tree_model
, path
, &iter
, new_order
);
2549 gtk_tree_path_free (path
);
2551 delete [] new_order
;
2553 for (pos
= 0; pos
< node_count
; pos
++)
2555 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
2560 //-----------------------------------------------------------------------------
2561 // wxDataViewCtrlInternal
2562 //-----------------------------------------------------------------------------
2564 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
,
2565 wxDataViewModel
*wx_model
, GtkWxTreeModel
*gtk_model
)
2568 m_wx_model
= wx_model
;
2569 m_gtk_model
= gtk_model
;
2571 m_sort_order
= GTK_SORT_ASCENDING
;
2573 m_dataview_sort_column
= NULL
;
2577 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2579 g_object_unref( m_gtk_model
);
2582 void wxDataViewCtrlInternal::InitTree()
2584 wxDataViewItem item
;
2585 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
2587 BuildBranch( m_root
);
2590 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
2592 if (node
->GetChildCount() == 0)
2594 wxDataViewItemArray children
;
2595 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
2597 for (pos
= 0; pos
< count
; pos
++)
2599 wxDataViewItem child
= children
[pos
];
2601 if (m_wx_model
->IsContainer( child
))
2602 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
2604 node
->AddLeave( child
.GetID() );
2606 // Don't send any events here
2611 void wxDataViewCtrlInternal::Resort()
2616 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2618 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2619 if (m_wx_model
->IsContainer( item
))
2620 parent_node
->AddNode( new wxGtkTreeModelNode( parent_node
, item
, this ) );
2622 parent_node
->AddLeave( item
.GetID() );
2627 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2629 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2630 parent_node
->DeleteChild( item
.GetID() );
2635 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
2637 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
2638 event
.SetEventObject( m_owner
);
2639 event
.SetModel( m_owner
->GetModel() );
2640 event
.SetItem( item
);
2641 m_owner
->GetEventHandler()->ProcessEvent( event
);
2646 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
2648 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
2649 event
.SetEventObject( m_owner
);
2650 event
.SetModel( m_owner
->GetModel() );
2651 event
.SetColumn( col
);
2652 event
.SetDataViewColumn( GetOwner()->GetColumn(col
) );
2653 event
.SetItem( item
);
2654 m_owner
->GetEventHandler()->ProcessEvent( event
);
2659 bool wxDataViewCtrlInternal::Cleared()
2664 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
2666 int depth
= gtk_tree_path_get_depth( path
);
2668 wxGtkTreeModelNode
*node
= m_root
;
2671 for (i
= 0; i
< depth
; i
++)
2673 BuildBranch( node
);
2675 gint pos
= gtk_tree_path_get_indices (path
)[i
];
2676 if (pos
< 0) return FALSE
;
2677 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
2679 void* id
= node
->GetChildren().Item( (size_t) pos
);
2683 iter
->stamp
= m_gtk_model
->stamp
;
2684 iter
->user_data
= id
;
2688 size_t count
= node
->GetNodes().GetCount();
2690 for (pos2
= 0; pos2
< count
; pos2
++)
2692 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
2693 if (child_node
->GetItem().GetID() == id
)
2704 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
2706 GtkTreePath
*retval
= gtk_tree_path_new ();
2707 void *id
= iter
->user_data
;
2709 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
2712 int pos
= node
->GetChildren().Index( id
);
2714 gtk_tree_path_prepend_index( retval
, pos
);
2716 id
= node
->GetItem().GetID();
2717 node
= node
->GetParent();
2723 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
2725 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
2726 if( parent
== NULL
)
2729 int pos
= parent
->GetChildren().Index( iter
->user_data
);
2731 if (pos
== (int) parent
->GetChildCount()-1)
2734 iter
->stamp
= m_gtk_model
->stamp
;
2735 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
2740 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
2742 wxDataViewItem
item( (void*) parent
->user_data
);
2744 if (!m_wx_model
->IsContainer( item
))
2747 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2748 BuildBranch( parent_node
);
2750 if (parent_node
->GetChildCount() == 0)
2753 iter
->stamp
= m_gtk_model
->stamp
;
2754 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
2759 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
2761 wxDataViewItem
item( (void*) iter
->user_data
);
2763 bool is_container
= m_wx_model
->IsContainer( item
);
2768 wxGtkTreeModelNode
*node
= FindNode( iter
);
2769 BuildBranch( node
);
2771 return (node
->GetChildCount() > 0);
2774 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
2776 wxDataViewItem
item( (void*) iter
->user_data
);
2778 if (!m_wx_model
->IsContainer( item
))
2781 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
2782 BuildBranch( parent_node
);
2784 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2786 return parent_node
->GetChildCount();
2789 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
2792 if (parent
) id
= (void*) parent
->user_data
;
2793 wxDataViewItem
item( id
);
2795 if (!m_wx_model
->IsContainer( item
))
2798 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2799 BuildBranch( parent_node
);
2801 // wxPrintf( "iter_nth_child %d\n", n );
2803 iter
->stamp
= m_gtk_model
->stamp
;
2804 iter
->user_data
= parent_node
->GetChildren().Item( n
);
2809 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
2811 wxGtkTreeModelNode
*node
= FindParentNode( child
);
2815 iter
->stamp
= m_gtk_model
->stamp
;
2816 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2821 static wxGtkTreeModelNode
*
2822 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
2828 list
.DeleteContents( true );
2829 wxDataViewItem
it( item
);
2833 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2834 list
.Insert( pItem
);
2835 it
= model
->GetParent( it
);
2838 wxGtkTreeModelNode
* node
= treeNode
;
2839 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2841 if( node
&& node
->GetNodes().GetCount() != 0 )
2843 int len
= node
->GetNodes().GetCount();
2844 wxGtkTreeModelNodes nodes
= node
->GetNodes();
2846 for( ; j
< len
; j
++)
2848 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2867 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
2872 wxDataViewItem
item( (void*) iter
->user_data
);
2876 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
2880 wxLogDebug( "Not found %p", iter
->user_data
);
2888 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
2893 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
2897 wxLogDebug( "Not found %p", item
.GetID() );
2905 static wxGtkTreeModelNode
*
2906 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
2912 list
.DeleteContents( true );
2916 wxDataViewItem
it( model
->GetParent( item
) );
2919 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2920 list
.Insert( pItem
);
2921 it
= model
->GetParent( it
);
2924 wxGtkTreeModelNode
* node
= treeNode
;
2925 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2927 if( node
&& node
->GetNodes().GetCount() != 0 )
2929 int len
= node
->GetNodes().GetCount();
2930 wxGtkTreeModelNodes nodes
= node
->GetNodes();
2932 for( ; j
< len
; j
++)
2934 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2949 //Examine whether the node is item's parent node
2950 int len
= node
->GetChildCount();
2951 for( int i
= 0; i
< len
; i
++ )
2953 if( node
->GetChildren().Item( i
) == item
.GetID() )
2959 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
2964 wxDataViewItem
item( (void*) iter
->user_data
);
2968 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
2971 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
2976 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
2979 //-----------------------------------------------------------------------------
2980 // wxDataViewCtrl signal callbacks
2981 //-----------------------------------------------------------------------------
2984 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
2986 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
2989 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, dv
->GetId() );
2990 event
.SetItem( dv
->GetSelection() );
2991 event
.SetModel( dv
->GetModel() );
2992 dv
->GetEventHandler()->ProcessEvent( event
);
2996 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
2997 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
2999 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
3002 dv
->GtkGetInternal()->get_iter( &iter
, path
);
3003 wxDataViewItem
item( (void*) iter
.user_data
);;
3004 event
.SetItem( item
);
3005 event
.SetModel( dv
->GetModel() );
3006 dv
->GetEventHandler()->ProcessEvent( event
);
3010 wxdataview_test_expand_row_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
3011 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
3013 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, dv
->GetId() );
3015 wxDataViewItem
item( (void*) iter
->user_data
);;
3016 event
.SetItem( item
);
3017 event
.SetModel( dv
->GetModel() );
3018 dv
->GetEventHandler()->ProcessEvent( event
);
3020 return !event
.IsAllowed();
3024 wxdataview_row_expanded_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
3025 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
3027 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
, dv
->GetId() );
3029 wxDataViewItem
item( (void*) iter
->user_data
);;
3030 event
.SetItem( item
);
3031 event
.SetModel( dv
->GetModel() );
3032 dv
->GetEventHandler()->ProcessEvent( event
);
3036 wxdataview_test_collapse_row_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
3037 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
3039 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
, dv
->GetId() );
3041 wxDataViewItem
item( (void*) iter
->user_data
);;
3042 event
.SetItem( item
);
3043 event
.SetModel( dv
->GetModel() );
3044 dv
->GetEventHandler()->ProcessEvent( event
);
3046 return !event
.IsAllowed();
3050 wxdataview_row_collapsed_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
3051 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
3053 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
, dv
->GetId() );
3055 wxDataViewItem
item( (void*) iter
->user_data
);;
3056 event
.SetItem( item
);
3057 event
.SetModel( dv
->GetModel() );
3058 dv
->GetEventHandler()->ProcessEvent( event
);
3061 //-----------------------------------------------------------------------------
3063 //-----------------------------------------------------------------------------
3065 //-----------------------------------------------------------------------------
3066 // InsertChild for wxDataViewCtrl
3067 //-----------------------------------------------------------------------------
3069 static void wxInsertChildInDataViewCtrl( wxWindowGTK
* parent
, wxWindowGTK
* child
)
3071 wxDataViewCtrl
* dvc
= (wxDataViewCtrl
*) parent
;
3072 GtkWidget
*treeview
= dvc
->GtkGetTreeView();
3074 // Insert widget in GtkTreeView
3075 if (GTK_WIDGET_REALIZED(treeview
))
3076 gtk_widget_set_parent_window( child
->m_widget
,
3077 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
3078 gtk_widget_set_parent( child
->m_widget
, treeview
);
3082 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
3083 GtkAllocation
*alloc
,
3084 wxDataViewCtrl
*win
)
3086 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
3089 wxWindow
*child
= node
->GetData();
3092 gtk_widget_size_request( child
->m_widget
, &req
);
3094 GtkAllocation alloc
;
3095 alloc
.x
= child
->m_x
;
3096 alloc
.y
= child
->m_y
;
3097 alloc
.width
= child
->m_width
;
3098 alloc
.height
= child
->m_height
;
3099 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
3101 node
= node
->GetNext();
3106 //-----------------------------------------------------------------------------
3107 // "motion_notify_event"
3108 //-----------------------------------------------------------------------------
3111 gtk_dataview_motion_notify_callback( GtkWidget
*widget
,
3112 GdkEventMotion
*gdk_event
,
3113 wxDataViewCtrl
*dv
)
3115 if (gdk_event
->is_hint
)
3119 GdkModifierType state
;
3120 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
3125 GtkTreePath
*path
= NULL
;
3126 GtkTreeViewColumn
*column
= NULL
;
3129 if (gtk_tree_view_get_path_at_pos(
3130 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
3131 (int) gdk_event
->x
, (int) gdk_event
->y
,
3140 dv
->GtkGetInternal()->get_iter( &iter
, path
);
3142 // wxPrintf( "mouse %d %d\n", (int) gdk_event->x, (int) gdk_event->y );
3144 gtk_tree_path_free( path
);
3153 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3155 wxDataViewCtrl::~wxDataViewCtrl()
3158 GetModel()->RemoveNotifier( m_notifier
);
3160 // remove the model from the GtkTreeView before it gets destroyed by the
3161 // wxDataViewCtrlBase's dtor
3162 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
3167 void wxDataViewCtrl::Init()
3172 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3173 const wxPoint
& pos
, const wxSize
& size
,
3174 long style
, const wxValidator
& validator
)
3178 if (!PreCreation( parent
, pos
, size
) ||
3179 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
3181 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
3185 m_insertCallback
= wxInsertChildInDataViewCtrl
;
3187 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
3189 GtkScrolledWindowSetBorder(m_widget
, style
);
3191 m_treeview
= gtk_tree_view_new();
3192 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
3194 g_signal_connect (m_treeview
, "size_allocate",
3195 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
3198 if (!gtk_check_version(2,6,0))
3199 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
3202 if (style
& wxDV_MULTIPLE
)
3204 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3205 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
3208 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
3211 if (!gtk_check_version(2,10,0))
3213 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
3215 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
3216 (style
& wxDV_VERT_RULES
) != 0)
3217 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
3218 else if (style
& wxDV_VERT_RULES
)
3219 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
3220 else if (style
& wxDV_HORIZ_RULES
)
3221 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
3223 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
3228 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
3231 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
3232 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
3233 gtk_widget_show (m_treeview
);
3235 m_parent
->DoAddChild( this );
3239 GtkEnableSelectionEvents();
3241 g_signal_connect_after (m_treeview
, "row-activated",
3242 G_CALLBACK (wxdataview_row_activated_callback
), this);
3244 g_signal_connect (m_treeview
, "test-collapse-row",
3245 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
3247 g_signal_connect_after (m_treeview
, "row-collapsed",
3248 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
3250 g_signal_connect (m_treeview
, "test-expand-row",
3251 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
3253 g_signal_connect_after (m_treeview
, "row-expanded",
3254 G_CALLBACK (wxdataview_row_expanded_callback
), this);
3256 g_signal_connect (m_treeview
, "motion_notify_event",
3257 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
3262 void wxDataViewCtrl::OnInternalIdle()
3264 wxWindow::OnInternalIdle();
3266 unsigned int cols
= GetColumnCount();
3268 for (i
= 0; i
< cols
; i
++)
3270 wxDataViewColumn
*col
= GetColumn( i
);
3271 col
->OnInternalIdle();
3275 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3277 if (!wxDataViewCtrlBase::AssociateModel( model
))
3280 GtkWxTreeModel
*gtk_model
= wxgtk_tree_model_new();
3281 m_internal
= new wxDataViewCtrlInternal( this, model
, gtk_model
);
3282 gtk_model
->internal
= m_internal
;
3284 m_notifier
= new wxGtkDataViewModelNotifier( gtk_model
, model
, this );
3286 model
->AddNotifier( m_notifier
);
3288 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_model
) );
3290 // unref in wxDataViewCtrlInternal
3291 // g_object_unref( gtk_model );
3296 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3298 if (!wxDataViewCtrlBase::AppendColumn(col
))
3301 m_cols
.Append( col
);
3303 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
3304 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
3309 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3311 if (!wxDataViewCtrlBase::PrependColumn(col
))
3314 m_cols
.Insert( col
);
3316 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
3317 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
3322 unsigned int wxDataViewCtrl::GetColumnCount() const
3324 return m_cols
.GetCount();
3327 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3329 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
3333 wxDataViewColumnList::const_iterator iter
;
3334 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); iter
++)
3336 wxDataViewColumn
*col
= *iter
;
3337 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
3346 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3348 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
3349 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
3351 m_cols
.remove( column
);
3358 bool wxDataViewCtrl::ClearColumns()
3360 wxDataViewColumnList::iterator iter
;
3361 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); iter
++)
3363 wxDataViewColumn
*col
= *iter
;
3364 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
3365 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
3373 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3375 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetConstGtkHandle());
3377 GList
*list
= gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview
) );
3379 gint pos
= g_list_index( list
, (gconstpointer
) gtk_column
);
3381 g_list_free( list
);
3386 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3388 return m_internal
->GetDataViewSortColumn();
3391 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
3394 iter
.user_data
= item
.GetID();
3395 GtkTreePath
*path
= m_internal
->get_path( &iter
);
3396 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
3397 gtk_tree_path_free( path
);
3400 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
3403 iter
.user_data
= item
.GetID();
3404 GtkTreePath
*path
= m_internal
->get_path( &iter
);
3405 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
3406 gtk_tree_path_free( path
);
3409 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3411 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3413 if (m_windowStyle
& wxDV_MULTIPLE
)
3415 // Report the first one
3416 GtkTreeModel
*model
;
3417 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
3421 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
3423 m_internal
->get_iter( &iter
, path
);
3426 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
3427 g_list_free( list
);
3429 return wxDataViewItem( (void*) iter
.user_data
);
3435 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
3437 wxDataViewItem
item( (void*) iter
.user_data
);
3442 return wxDataViewItem(0);
3445 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3449 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3450 if (HasFlag(wxDV_MULTIPLE
))
3452 GtkTreeModel
*model
;
3453 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
3458 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
3461 m_internal
->get_iter( &iter
, path
);
3463 sel
.Add( wxDataViewItem( (void*) iter
.user_data
) );
3465 list
= g_list_next( list
);
3470 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
3471 g_list_free( list
);
3477 GtkTreeModel
*model
;
3479 gboolean has_selection
= gtk_tree_selection_get_selected( selection
, &model
, &iter
);
3482 sel
.Add( wxDataViewItem( (void*) iter
.user_data
) );
3490 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3492 GtkDisableSelectionEvents();
3494 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3496 gtk_tree_selection_unselect_all( selection
);
3499 for (i
= 0; i
< sel
.GetCount(); i
++)
3502 iter
.user_data
= (gpointer
) sel
[i
].GetID();
3503 gtk_tree_selection_select_iter( selection
, &iter
);
3506 GtkEnableSelectionEvents();
3509 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3511 GtkDisableSelectionEvents();
3513 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3516 iter
.user_data
= (gpointer
) item
.GetID();
3517 gtk_tree_selection_select_iter( selection
, &iter
);
3519 GtkEnableSelectionEvents();
3522 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3524 GtkDisableSelectionEvents();
3526 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3529 iter
.user_data
= (gpointer
) item
.GetID();
3530 gtk_tree_selection_unselect_iter( selection
, &iter
);
3532 GtkEnableSelectionEvents();
3535 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3537 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3540 iter
.user_data
= (gpointer
) item
.GetID();
3542 return gtk_tree_selection_iter_is_selected( selection
, &iter
);
3545 void wxDataViewCtrl::SelectAll()
3547 GtkDisableSelectionEvents();
3549 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3551 gtk_tree_selection_select_all( selection
);
3553 GtkEnableSelectionEvents();
3556 void wxDataViewCtrl::UnselectAll()
3558 GtkDisableSelectionEvents();
3560 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3562 gtk_tree_selection_unselect_all( selection
);
3564 GtkEnableSelectionEvents();
3567 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
3570 iter
.user_data
= (gpointer
) item
.GetID();
3571 GtkTreePath
*path
= m_internal
->get_path( &iter
);
3572 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
3573 gtk_tree_path_free( path
);
3576 void wxDataViewCtrl::HitTest( const wxPoint
&point
,
3577 wxDataViewItem
&item
, wxDataViewColumn
*&column
) const
3579 item
= wxDataViewItem(0);
3583 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
&item
,
3584 const wxDataViewColumn
*column
) const
3589 void wxDataViewCtrl::DoSetExpanderColumn()
3591 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
3592 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
3595 void wxDataViewCtrl::DoSetIndent()
3599 void wxDataViewCtrl::GtkDisableSelectionEvents()
3601 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3602 g_signal_handlers_disconnect_by_func( selection
,
3603 (gpointer
) (wxdataview_selection_changed_callback
), this);
3606 void wxDataViewCtrl::GtkEnableSelectionEvents()
3608 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3609 g_signal_connect_after (selection
, "changed",
3610 G_CALLBACK (wxdataview_selection_changed_callback
), this);
3615 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3617 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
3622 // !wxUSE_GENERICDATAVIEWCTRL
3625 // wxUSE_DATAVIEWCTRL