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"
35 #include "wx/gtk/dc.h"
36 #include "wx/gtk/dcclient.h"
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 class wxDataViewCtrlInternal
;
43 wxDataViewCtrlInternal
*g_internal
= NULL
;
45 class wxGtkTreeModelNode
;
48 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
51 //-----------------------------------------------------------------------------
52 // wxDataViewCtrlInternal
53 //-----------------------------------------------------------------------------
55 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
56 WX_DEFINE_LIST(ItemList
)
58 class wxDataViewCtrlInternal
61 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
, GtkWxTreeModel
*owner
);
62 ~wxDataViewCtrlInternal();
64 GtkTreeModelFlags
get_flags();
65 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
66 GtkTreePath
*get_path( GtkTreeIter
*iter
);
67 gboolean
iter_next( GtkTreeIter
*iter
);
68 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
69 gboolean
iter_has_child( GtkTreeIter
*iter
);
70 gint
iter_n_children( GtkTreeIter
*iter
);
71 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
72 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
74 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
75 wxDataViewCtrl
* GetOwner() { return m_owner
; }
76 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
78 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
79 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
80 bool ItemChanged( const wxDataViewItem
&item
);
81 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
85 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
86 GtkSortType
GetSortOrder() { return m_sort_order
; }
88 void SetSortColumn( int column
) { m_sort_column
= column
; }
89 int GetSortColumn() { return m_sort_column
; }
91 void SetDataViewSortColumn( wxDataViewColumn
*column
) { m_dataview_sort_column
= column
; }
92 wxDataViewColumn
*GetDataViewSortColumn() { return m_dataview_sort_column
; }
94 bool IsSorted() { return (m_sort_column
>= 0); }
99 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
100 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
101 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
102 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
103 void BuildBranch( wxGtkTreeModelNode
*branch
);
106 wxGtkTreeModelNode
*m_root
;
107 wxDataViewModel
*m_wx_model
;
108 GtkWxTreeModel
*m_gtk_model
;
109 wxDataViewCtrl
*m_owner
;
110 GtkSortType m_sort_order
;
111 wxDataViewColumn
*m_dataview_sort_column
;
116 //-----------------------------------------------------------------------------
117 // wxGtkTreeModelNode
118 //-----------------------------------------------------------------------------
120 int LINKAGEMODE
wxGtkTreeModelChildCmp( void** id1
, void** id2
)
122 int ret
= g_internal
->GetDataViewModel()->Compare( *id1
, *id2
,
123 g_internal
->GetSortColumn(), (g_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
128 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
129 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren
);
131 class wxGtkTreeModelNode
134 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
135 wxDataViewCtrlInternal
*internal
)
139 m_internal
= internal
;
142 ~wxGtkTreeModelNode()
144 size_t count
= m_children
.GetCount();
146 for (i
= 0; i
< count
; i
++)
148 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
153 unsigned int AddNode( wxGtkTreeModelNode
* child
)
155 m_nodes
.Add( child
);
157 void *id
= child
->GetItem().GetID();
159 m_children
.Add( id
);
161 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
163 g_internal
= m_internal
;
164 m_children
.Sort( &wxGtkTreeModelChildCmp
);
165 return m_children
.Index( id
);
168 return m_children
.GetCount()-1;
171 unsigned int AddLeave( void* id
)
173 m_children
.Add( id
);
175 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
177 g_internal
= m_internal
;
178 m_children
.Sort( &wxGtkTreeModelChildCmp
);
179 return m_children
.Index( id
);
182 return m_children
.GetCount()-1;
185 void DeleteChild( void* id
)
187 m_children
.Remove( id
);
189 unsigned int count
= m_nodes
.GetCount();
191 for (pos
= 0; pos
< count
; pos
++)
193 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
194 if (node
->GetItem().GetID() == id
)
196 m_nodes
.RemoveAt( pos
);
204 wxGtkTreeModelNode
* GetParent()
206 wxGtkTreeModelNodes
&GetNodes()
208 wxGtkTreeModelChildren
&GetChildren()
209 { return m_children
; }
211 unsigned int GetChildCount() { return m_children
.GetCount(); }
212 unsigned int GetNodesCount() { return m_nodes
.GetCount(); }
214 wxDataViewItem
&GetItem() { return m_item
; }
215 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
220 wxGtkTreeModelNode
*m_parent
;
221 wxGtkTreeModelNodes m_nodes
;
222 wxGtkTreeModelChildren m_children
;
223 wxDataViewItem m_item
;
224 wxDataViewCtrlInternal
*m_internal
;
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
232 extern bool g_blockEventsOnDrag
;
234 //-----------------------------------------------------------------------------
235 // define new GTK+ class wxGtkTreeModel
236 //-----------------------------------------------------------------------------
240 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
241 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
242 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
243 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
244 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
245 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
247 GType
gtk_wx_tree_model_get_type (void);
249 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
251 struct _GtkWxTreeModel
257 wxDataViewCtrlInternal
*internal
;
260 struct _GtkWxTreeModelClass
262 GObjectClass list_parent_class
;
265 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
266 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
267 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
268 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
269 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
270 static void wxgtk_tree_model_finalize (GObject
*object
);
271 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
272 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
273 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
275 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
278 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
280 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
284 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
286 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
288 GtkTreeIter
*parent
);
289 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
291 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
293 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
297 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
302 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
303 gint
*sort_column_id
,
305 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
308 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
310 GtkTreeIterCompareFunc func
,
312 GtkDestroyNotify destroy
);
313 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
314 GtkTreeIterCompareFunc func
,
316 GtkDestroyNotify destroy
);
317 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
321 static GObjectClass
*list_parent_class
= NULL
;
324 gtk_wx_tree_model_get_type (void)
326 static GType tree_model_type
= 0;
328 if (!tree_model_type
)
330 const GTypeInfo tree_model_info
=
332 sizeof (GtkWxTreeModelClass
),
333 NULL
, /* base_init */
334 NULL
, /* base_finalize */
335 (GClassInitFunc
) wxgtk_tree_model_class_init
,
336 NULL
, /* class_finalize */
337 NULL
, /* class_data */
338 sizeof (GtkWxTreeModel
),
340 (GInstanceInitFunc
) wxgtk_tree_model_init
,
343 static const GInterfaceInfo tree_model_iface_info
=
345 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
350 static const GInterfaceInfo sortable_iface_info
=
352 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
357 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
358 &tree_model_info
, (GTypeFlags
)0 );
360 g_type_add_interface_static (tree_model_type
,
362 &tree_model_iface_info
);
363 g_type_add_interface_static (tree_model_type
,
364 GTK_TYPE_TREE_SORTABLE
,
365 &sortable_iface_info
);
368 return tree_model_type
;
371 static GtkWxTreeModel
*
372 wxgtk_tree_model_new(void)
374 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
379 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
381 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
382 GObjectClass
*object_class
= (GObjectClass
*) klass
;
383 object_class
->finalize
= wxgtk_tree_model_finalize
;
387 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
389 iface
->get_flags
= wxgtk_tree_model_get_flags
;
390 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
391 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
392 iface
->get_iter
= wxgtk_tree_model_get_iter
;
393 iface
->get_path
= wxgtk_tree_model_get_path
;
394 iface
->get_value
= wxgtk_tree_model_get_value
;
395 iface
->iter_next
= wxgtk_tree_model_iter_next
;
396 iface
->iter_children
= wxgtk_tree_model_iter_children
;
397 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
398 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
399 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
400 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
404 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
406 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
407 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
408 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
409 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
410 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
414 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
416 tree_model
->internal
= NULL
;
417 tree_model
->stamp
= g_random_int();
421 wxgtk_tree_model_finalize (GObject
*object
)
424 (* list_parent_class
->finalize
) (object
);
429 //-----------------------------------------------------------------------------
430 // implement callbacks from wxGtkTreeModel class by letting
431 // them call the methods of wxWidgets' wxDataViewModel
432 //-----------------------------------------------------------------------------
434 static GtkTreeModelFlags
435 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
437 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
438 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), (GtkTreeModelFlags
)0 );
440 return wxtree_model
->internal
->get_flags();
444 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
446 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
447 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
449 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
453 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
456 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
457 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
459 GType gtype
= G_TYPE_INVALID
;
461 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
463 if (wxtype
== wxT("string"))
464 gtype
= G_TYPE_STRING
;
467 gtype
= G_TYPE_STRING
;
468 // wxFAIL_MSG( _T("non-string columns not supported yet") );
475 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
479 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
480 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
481 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
483 return wxtree_model
->internal
->get_iter( iter
, path
);
487 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
490 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
491 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
492 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
494 return wxtree_model
->internal
->get_path( iter
);
498 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
503 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
504 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
506 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
507 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
508 if (mtype
== wxT("string"))
511 g_value_init( value
, G_TYPE_STRING
);
512 wxDataViewItem
item( (void*) iter
->user_data
);
513 model
->GetValue( variant
, item
, (unsigned int) column
);
515 g_value_set_string( value
, variant
.GetString().utf8_str() );
519 wxFAIL_MSG( _T("non-string columns not supported yet") );
524 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
527 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
528 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
529 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
531 return wxtree_model
->internal
->iter_next( iter
);
535 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
539 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
540 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
541 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
543 return wxtree_model
->internal
->iter_children( iter
, parent
);
547 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
550 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
551 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
552 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
554 return wxtree_model
->internal
->iter_has_child( iter
);
558 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
561 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
562 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
563 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, 0);
565 return wxtree_model
->internal
->iter_n_children( iter
);
569 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
574 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
575 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
577 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
581 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
585 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
586 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
587 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
589 return wxtree_model
->internal
->iter_parent( iter
, child
);
593 gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
594 gint
*sort_column_id
,
597 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
599 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
601 if (!tree_model
->internal
->IsSorted())
604 *sort_column_id
= -1;
611 *sort_column_id
= tree_model
->internal
->GetSortColumn();
614 *order
= tree_model
->internal
->GetSortOrder();
619 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
621 void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
625 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
626 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
628 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
630 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
631 (order
!= tree_model
->internal
->GetSortOrder()))
633 tree_model
->internal
->SetSortColumn( sort_column_id
);
634 tree_model
->internal
->SetSortOrder( order
);
636 gtk_tree_sortable_sort_column_changed (sortable
);
638 tree_model
->internal
->GetDataViewModel()->Resort();
641 if (gs_lastLeftClickHeader
)
643 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
644 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
645 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
646 event
.SetModel( dv
->GetModel() );
647 dv
->HandleWindowEvent( event
);
650 gs_lastLeftClickHeader
= NULL
;
653 void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
655 GtkTreeIterCompareFunc func
,
657 GtkDestroyNotify destroy
)
659 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
660 g_return_if_fail (func
!= NULL
);
663 void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
664 GtkTreeIterCompareFunc func
,
666 GtkDestroyNotify destroy
)
668 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
669 g_return_if_fail (func
!= NULL
);
671 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
674 gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
679 //-----------------------------------------------------------------------------
680 // define new GTK+ class wxGtkRendererRenderer
681 //-----------------------------------------------------------------------------
685 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
686 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
687 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
688 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
689 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
690 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
692 GType
gtk_wx_cell_renderer_get_type (void);
694 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
695 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
697 struct _GtkWxCellRenderer
699 GtkCellRenderer parent
;
702 wxDataViewCustomRenderer
*cell
;
706 struct _GtkWxCellRendererClass
708 GtkCellRendererClass cell_parent_class
;
712 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
713 static void gtk_wx_cell_renderer_init (
714 GtkWxCellRenderer
*cell
);
715 static void gtk_wx_cell_renderer_class_init(
716 GtkWxCellRendererClass
*klass
);
717 static void gtk_wx_cell_renderer_finalize (
719 static void gtk_wx_cell_renderer_get_size (
720 GtkCellRenderer
*cell
,
722 GdkRectangle
*rectangle
,
727 static void gtk_wx_cell_renderer_render (
728 GtkCellRenderer
*cell
,
731 GdkRectangle
*background_area
,
732 GdkRectangle
*cell_area
,
733 GdkRectangle
*expose_area
,
734 GtkCellRendererState flags
);
735 static gboolean
gtk_wx_cell_renderer_activate(
736 GtkCellRenderer
*cell
,
740 GdkRectangle
*background_area
,
741 GdkRectangle
*cell_area
,
742 GtkCellRendererState flags
);
743 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
744 GtkCellRenderer
*cell
,
748 GdkRectangle
*background_area
,
749 GdkRectangle
*cell_area
,
750 GtkCellRendererState flags
);
753 static GObjectClass
*cell_parent_class
= NULL
;
758 gtk_wx_cell_renderer_get_type (void)
760 static GType cell_wx_type
= 0;
764 const GTypeInfo cell_wx_info
=
766 sizeof (GtkWxCellRendererClass
),
767 NULL
, /* base_init */
768 NULL
, /* base_finalize */
769 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
770 NULL
, /* class_finalize */
771 NULL
, /* class_data */
772 sizeof (GtkWxCellRenderer
),
774 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
777 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
778 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
785 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
788 cell
->last_click
= 0;
792 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
794 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
795 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
797 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
799 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
801 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
802 cell_class
->render
= gtk_wx_cell_renderer_render
;
803 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
804 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
808 gtk_wx_cell_renderer_finalize (GObject
*object
)
811 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
815 gtk_wx_cell_renderer_new (void)
817 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
822 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
823 GtkCellRenderer
*renderer
,
827 GdkRectangle
*background_area
,
828 GdkRectangle
*cell_area
,
829 GtkCellRendererState flags
)
831 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
832 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
833 if (!cell
->HasEditorCtrl())
837 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
843 rect
.x
+= cell_area
->x
;
844 rect
.y
+= cell_area
->y
;
845 // rect.width -= renderer->xpad * 2;
846 // rect.height -= renderer->ypad * 2;
848 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
849 wxRect
renderrect( cell_area
->x
, cell_area
->y
, cell_area
->width
, cell_area
->height
);
851 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
853 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
854 wxDataViewItem
item( (void*) iter
.user_data
);
855 gtk_tree_path_free( treepath
);
857 cell
->StartEditing( item
, renderrect
);
863 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
865 GdkRectangle
*cell_area
,
871 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
872 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
874 wxSize size
= cell
->GetSize();
876 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
877 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
884 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
888 *x_offset
= (gint
)((renderer
->xalign
*
889 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
890 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
894 *y_offset
= (gint
)((renderer
->yalign
*
895 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
896 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
904 *height
= calc_height
;
908 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
911 GdkRectangle
*background_area
,
912 GdkRectangle
*cell_area
,
913 GdkRectangle
*expose_area
,
914 GtkCellRendererState flags
)
917 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
918 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
920 cell
->window
= window
;
921 cell
->widget
= widget
;
922 cell
->background_area
= (void*) background_area
;
923 cell
->cell_area
= (void*) cell_area
;
924 cell
->expose_area
= (void*) expose_area
;
925 cell
->flags
= (int) flags
;
928 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
934 rect
.x
+= cell_area
->x
;
935 rect
.y
+= cell_area
->y
;
936 rect
.width
-= renderer
->xpad
* 2;
937 rect
.height
-= renderer
->ypad
* 2;
940 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
942 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
943 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
944 wxWindowDCImpl
*impl
= (wxWindowDCImpl
*) dc
->GetImpl();
945 if (impl
->m_gdkwindow
== NULL
)
947 impl
->m_gdkwindow
= window
;
952 if (flags
& GTK_CELL_RENDERER_SELECTED
)
953 state
|= wxDATAVIEW_CELL_SELECTED
;
954 if (flags
& GTK_CELL_RENDERER_PRELIT
)
955 state
|= wxDATAVIEW_CELL_PRELIT
;
956 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
957 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
958 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
959 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
960 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
961 state
|= wxDATAVIEW_CELL_FOCUSED
;
962 cell
->Render( renderrect
, dc
, state
);
967 gtk_wx_cell_renderer_activate(
968 GtkCellRenderer
*renderer
,
972 GdkRectangle
*background_area
,
973 GdkRectangle
*cell_area
,
974 GtkCellRendererState flags
)
976 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
977 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
980 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
986 rect
.x
+= cell_area
->x
;
987 rect
.y
+= cell_area
->y
;
988 rect
.width
-= renderer
->xpad
* 2;
989 rect
.height
-= renderer
->ypad
* 2;
991 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
993 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
995 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
998 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
999 wxDataViewItem
item( iter
.user_data
);
1000 gtk_tree_path_free( treepath
);
1002 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1008 // activated by <ENTER>
1009 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1014 else if (event
->type
== GDK_BUTTON_PRESS
)
1016 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
1017 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
1018 ((int) button_event
->y
) - renderrect
.y
);
1021 if (button_event
->button
== 1)
1023 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
1025 // TODO: query system double-click time
1026 if (button_event
->time
- wxrenderer
->last_click
< 400)
1027 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1030 if (button_event
->button
== 3)
1032 if (cell
->RightClick( pt
, renderrect
, model
, item
, model_col
))
1036 wxrenderer
->last_click
= button_event
->time
;
1044 // ---------------------------------------------------------
1045 // wxGtkDataViewModelNotifier
1046 // ---------------------------------------------------------
1048 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1051 wxGtkDataViewModelNotifier( GtkWxTreeModel
*wxgtk_model
,
1052 wxDataViewModel
*wx_model
,
1053 wxDataViewCtrl
*ctrl
);
1054 ~wxGtkDataViewModelNotifier();
1056 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1057 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1058 virtual bool ItemChanged( const wxDataViewItem
&item
);
1059 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
1060 virtual bool Cleared();
1061 virtual void Resort();
1063 GtkWxTreeModel
*m_wxgtk_model
;
1064 wxDataViewModel
*m_wx_model
;
1065 wxDataViewCtrl
*m_owner
;
1068 // ---------------------------------------------------------
1069 // wxGtkDataViewListModelNotifier
1070 // ---------------------------------------------------------
1072 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1073 GtkWxTreeModel
* wxgtk_model
, wxDataViewModel
*wx_model
,
1074 wxDataViewCtrl
*ctrl
)
1076 m_wxgtk_model
= wxgtk_model
;
1077 m_wx_model
= wx_model
;
1081 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1084 m_wxgtk_model
= NULL
;
1087 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1089 m_owner
->GtkGetInternal()->ItemAdded( parent
, item
);
1092 iter
.stamp
= m_wxgtk_model
->stamp
;
1093 iter
.user_data
= (gpointer
) item
.GetID();
1095 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1096 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1097 gtk_tree_model_row_inserted(
1098 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1099 gtk_tree_path_free (path
);
1104 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1107 iter
.stamp
= m_wxgtk_model
->stamp
;
1108 iter
.user_data
= (gpointer
) item
.GetID();
1110 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1111 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1112 gtk_tree_model_row_deleted(
1113 GTK_TREE_MODEL(m_wxgtk_model
), path
);
1114 gtk_tree_path_free (path
);
1116 m_owner
->GtkGetInternal()->ItemDeleted( parent
, item
);
1121 void wxGtkDataViewModelNotifier::Resort()
1123 m_owner
->GtkGetInternal()->Resort();
1126 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1129 iter
.stamp
= m_wxgtk_model
->stamp
;
1130 iter
.user_data
= (gpointer
) item
.GetID();
1132 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1133 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1134 gtk_tree_model_row_changed(
1135 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1136 gtk_tree_path_free (path
);
1138 m_owner
->GtkGetInternal()->ItemChanged( item
);
1143 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_col
)
1145 // This adds GTK+'s missing MVC logic for ValueChanged
1147 for (index
= 0; index
< m_owner
->GetColumnCount(); index
++)
1149 wxDataViewColumn
*column
= m_owner
->GetColumn( index
);
1150 if (column
->GetModelColumn() == model_col
)
1152 GtkTreeView
*widget
= GTK_TREE_VIEW(m_owner
->m_treeview
);
1153 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1157 iter
.stamp
= m_wxgtk_model
->stamp
;
1158 iter
.user_data
= (gpointer
) item
.GetID();
1159 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1160 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1161 GdkRectangle cell_area
;
1162 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1163 gtk_tree_path_free( path
);
1165 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1166 double d
= gtk_adjustment_get_value( hadjust
);
1167 int xdiff
= (int) d
;
1169 int ydiff
= gcolumn
->button
->allocation
.height
;
1171 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1172 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1174 m_owner
->GtkGetInternal()->ValueChanged( item
, model_col
);
1183 bool wxGtkDataViewModelNotifier::Cleared()
1185 // TODO: delete everything
1187 m_owner
->GtkGetInternal()->Cleared();
1192 // ---------------------------------------------------------
1193 // wxDataViewRenderer
1194 // ---------------------------------------------------------
1196 static gpointer s_user_data
= NULL
;
1199 wxgtk_cell_editable_editing_done( GtkCellEditable
*editable
,
1200 wxDataViewRenderer
*wxrenderer
)
1202 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1203 wxDataViewCtrl
*dv
= column
->GetOwner();
1204 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1205 event
.SetDataViewColumn( column
);
1206 event
.SetModel( dv
->GetModel() );
1207 wxDataViewItem
item( s_user_data
);
1208 event
.SetItem( item
);
1209 dv
->HandleWindowEvent( event
);
1213 wxgtk_renderer_editing_started( GtkCellRenderer
*cell
, GtkCellEditable
*editable
,
1214 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1216 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1217 wxDataViewCtrl
*dv
= column
->GetOwner();
1218 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1219 event
.SetDataViewColumn( column
);
1220 event
.SetModel( dv
->GetModel() );
1221 GtkTreePath
*tree_path
= gtk_tree_path_new_from_string( path
);
1223 dv
->GtkGetInternal()->get_iter( &iter
, tree_path
);
1224 gtk_tree_path_free( tree_path
);
1225 wxDataViewItem
item( iter
.user_data
);
1226 event
.SetItem( item
);
1227 dv
->HandleWindowEvent( event
);
1229 if (GTK_IS_CELL_EDITABLE(editable
))
1231 s_user_data
= iter
.user_data
;
1233 g_signal_connect (GTK_CELL_EDITABLE (editable
), "editing_done",
1234 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1235 (gpointer
) wxrenderer
);
1241 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1243 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1245 wxDataViewRendererBase( varianttype
, mode
, align
)
1249 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1250 // after the m_renderer pointer has been initialized
1253 void wxDataViewRenderer::GtkInitHandlers()
1255 if (!gtk_check_version(2,6,0))
1257 g_signal_connect (GTK_CELL_RENDERER(m_renderer
), "editing_started",
1258 G_CALLBACK (wxgtk_renderer_editing_started
),
1263 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1265 GtkCellRendererMode gtkMode
;
1268 case wxDATAVIEW_CELL_INERT
:
1269 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1271 case wxDATAVIEW_CELL_ACTIVATABLE
:
1272 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1274 case wxDATAVIEW_CELL_EDITABLE
:
1275 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1279 // This value is most often ignored in GtkTreeView
1280 GValue gvalue
= { 0, };
1281 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1282 g_value_set_enum( &gvalue
, gtkMode
);
1283 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1284 g_value_unset( &gvalue
);
1287 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1289 wxDataViewCellMode ret
;
1292 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1294 switch (g_value_get_enum(&gvalue
))
1296 case GTK_CELL_RENDERER_MODE_INERT
:
1297 ret
= wxDATAVIEW_CELL_INERT
;
1299 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1300 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1302 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1303 ret
= wxDATAVIEW_CELL_EDITABLE
;
1307 g_value_unset( &gvalue
);
1312 void wxDataViewRenderer::SetAlignment( int align
)
1314 // horizontal alignment:
1316 gfloat xalign
= 0.0;
1317 if (align
& wxALIGN_RIGHT
)
1319 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1322 GValue gvalue
= { 0, };
1323 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1324 g_value_set_float( &gvalue
, xalign
);
1325 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1326 g_value_unset( &gvalue
);
1328 // vertical alignment:
1330 gfloat yalign
= 0.0;
1331 if (align
& wxALIGN_BOTTOM
)
1333 else if (align
& wxALIGN_CENTER_VERTICAL
)
1336 GValue gvalue2
= { 0, };
1337 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1338 g_value_set_float( &gvalue2
, yalign
);
1339 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1340 g_value_unset( &gvalue2
);
1343 int wxDataViewRenderer::GetAlignment() const
1348 // horizontal alignment:
1350 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
1351 float xalign
= g_value_get_float( &gvalue
);
1353 ret
|= wxALIGN_LEFT
;
1354 else if (xalign
== 0.5)
1355 ret
|= wxALIGN_CENTER_HORIZONTAL
;
1357 ret
|= wxALIGN_RIGHT
;
1358 g_value_unset( &gvalue
);
1361 // vertical alignment:
1363 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
1364 float yalign
= g_value_get_float( &gvalue
);
1367 else if (yalign
== 0.5)
1368 ret
|= wxALIGN_CENTER_VERTICAL
;
1370 ret
|= wxALIGN_BOTTOM
;
1371 g_value_unset( &gvalue
);
1378 // ---------------------------------------------------------
1379 // wxDataViewTextRenderer
1380 // ---------------------------------------------------------
1383 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1384 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1387 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1388 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1390 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1392 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1393 wxVariant value
= tmp
;
1394 if (!cell
->Validate( value
))
1397 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1399 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1401 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, path
);
1402 wxDataViewItem
item( (void*) iter
.user_data
);;
1403 gtk_tree_path_free( path
);
1405 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1407 model
->SetValue( value
, item
, model_col
);
1408 model
->ValueChanged( item
, model_col
);
1411 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1413 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1415 wxDataViewRenderer( varianttype
, mode
, align
)
1417 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_text_new();
1419 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1421 GValue gvalue
= { 0, };
1422 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1423 g_value_set_boolean( &gvalue
, true );
1424 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1425 g_value_unset( &gvalue
);
1427 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1433 SetAlignment(align
);
1436 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1438 wxString tmp
= value
;
1440 GValue gvalue
= { 0, };
1441 g_value_init( &gvalue
, G_TYPE_STRING
);
1442 g_value_set_string( &gvalue
, wxGTK_CONV( tmp
) );
1443 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1444 g_value_unset( &gvalue
);
1449 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1451 GValue gvalue
= { 0, };
1452 g_value_init( &gvalue
, G_TYPE_STRING
);
1453 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1454 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &gvalue
) );
1455 g_value_unset( &gvalue
);
1462 void wxDataViewTextRenderer::SetAlignment( int align
)
1464 wxDataViewRenderer::SetAlignment(align
);
1466 if (gtk_check_version(2,10,0))
1469 // horizontal alignment:
1470 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1471 if (align
& wxALIGN_RIGHT
)
1472 pangoAlign
= PANGO_ALIGN_RIGHT
;
1473 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1474 pangoAlign
= PANGO_ALIGN_CENTER
;
1476 GValue gvalue
= { 0, };
1477 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1478 g_value_set_enum( &gvalue
, pangoAlign
);
1479 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1480 g_value_unset( &gvalue
);
1483 // ---------------------------------------------------------
1484 // wxDataViewTextRendererAttr
1485 // ---------------------------------------------------------
1487 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
,wxDataViewTextRenderer
)
1489 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
1490 wxDataViewCellMode mode
, int align
) :
1491 wxDataViewTextRenderer( varianttype
, mode
, align
)
1495 // ---------------------------------------------------------
1496 // wxDataViewBitmapRenderer
1497 // ---------------------------------------------------------
1499 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1501 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1503 wxDataViewRenderer( varianttype
, mode
, align
)
1505 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_pixbuf_new();
1508 SetAlignment(align
);
1511 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1513 if (value
.GetType() == wxT("wxBitmap"))
1518 // This may create a Pixbuf representation in the
1519 // wxBitmap object (and it will stay there)
1520 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1522 GValue gvalue
= { 0, };
1523 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1524 g_value_set_object( &gvalue
, pixbuf
);
1525 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1526 g_value_unset( &gvalue
);
1531 if (value
.GetType() == wxT("wxIcon"))
1536 // This may create a Pixbuf representation in the
1537 // wxBitmap object (and it will stay there)
1538 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1540 GValue gvalue
= { 0, };
1541 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1542 g_value_set_object( &gvalue
, pixbuf
);
1543 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1544 g_value_unset( &gvalue
);
1552 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1557 // ---------------------------------------------------------
1558 // wxDataViewToggleRenderer
1559 // ---------------------------------------------------------
1562 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1563 gchar
*path
, gpointer user_data
);
1566 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1567 gchar
*path
, gpointer user_data
)
1569 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1572 GValue gvalue
= { 0, };
1573 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1574 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1575 bool tmp
= g_value_get_boolean( &gvalue
);
1576 g_value_unset( &gvalue
);
1580 wxVariant value
= tmp
;
1581 if (!cell
->Validate( value
))
1584 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1586 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1588 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, gtk_path
);
1589 wxDataViewItem
item( (void*) iter
.user_data
);;
1590 gtk_tree_path_free( gtk_path
);
1592 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1594 model
->SetValue( value
, item
, model_col
);
1595 model
->ValueChanged( item
, model_col
);
1598 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1600 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1601 wxDataViewCellMode mode
, int align
) :
1602 wxDataViewRenderer( varianttype
, mode
, align
)
1604 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
1606 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1608 g_signal_connect_after( m_renderer
, "toggled",
1609 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1613 GValue gvalue
= { 0, };
1614 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1615 g_value_set_boolean( &gvalue
, false );
1616 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1617 g_value_unset( &gvalue
);
1621 SetAlignment(align
);
1624 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1628 GValue gvalue
= { 0, };
1629 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1630 g_value_set_boolean( &gvalue
, tmp
);
1631 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1632 g_value_unset( &gvalue
);
1637 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1639 GValue gvalue
= { 0, };
1640 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1641 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1642 bool tmp
= g_value_get_boolean( &gvalue
);
1643 g_value_unset( &gvalue
);
1650 // ---------------------------------------------------------
1651 // wxDataViewCustomRenderer
1652 // ---------------------------------------------------------
1654 class wxDataViewCtrlDCImpl
: public wxWindowDCImpl
1657 wxDataViewCtrlDCImpl( wxDC
*owner
, wxDataViewCtrl
*window
) :
1658 wxWindowDCImpl( owner
)
1660 GtkWidget
*widget
= window
->m_treeview
;
1666 m_context
= window
->GtkGetPangoDefaultContext();
1667 m_layout
= pango_layout_new( m_context
);
1668 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1670 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1672 // Set m_gdkwindow later
1677 class wxDataViewCtrlDC
: public wxWindowDC
1680 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1681 { m_pimpl
= new wxDataViewCtrlDCImpl( this, window
); }
1685 // ---------------------------------------------------------
1686 // wxDataViewCustomRenderer
1687 // ---------------------------------------------------------
1689 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1691 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1692 wxDataViewCellMode mode
, int align
,
1694 wxDataViewRenderer( varianttype
, mode
, align
)
1697 m_text_renderer
= NULL
;
1705 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
1708 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
1709 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
1710 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
1711 view
->GetForegroundColour();
1712 dc
->SetTextForeground(col
);
1713 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
1715 if (!m_text_renderer
)
1716 m_text_renderer
= gtk_cell_renderer_text_new();
1718 GValue gvalue
= { 0, };
1719 g_value_init( &gvalue
, G_TYPE_STRING
);
1720 g_value_set_string( &gvalue
, wxGTK_CONV( text
) );
1721 g_object_set_property( G_OBJECT(m_text_renderer
), "text", &gvalue
);
1722 g_value_unset( &gvalue
);
1724 ((GdkRectangle
*) cell_area
)->x
+= xoffset
;
1725 ((GdkRectangle
*) cell_area
)->width
-= xoffset
;
1727 gtk_cell_renderer_render( m_text_renderer
,
1730 (GdkRectangle
*) background_area
,
1731 (GdkRectangle
*) cell_area
,
1732 (GdkRectangle
*) expose_area
,
1733 (GtkCellRendererState
) flags
);
1735 ((GdkRectangle
*) cell_area
)->x
-= xoffset
;
1736 ((GdkRectangle
*) cell_area
)->width
+= xoffset
;
1740 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1742 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1743 renderer
->cell
= this;
1745 m_renderer
= (GtkCellRenderer
*) renderer
;
1748 SetAlignment(align
);
1755 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1760 if (m_text_renderer
)
1761 gtk_object_sink( GTK_OBJECT(m_text_renderer
) );
1764 wxDC
*wxDataViewCustomRenderer::GetDC()
1768 if (GetOwner() == NULL
)
1770 if (GetOwner()->GetOwner() == NULL
)
1772 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1778 // ---------------------------------------------------------
1779 // wxDataViewProgressRenderer
1780 // ---------------------------------------------------------
1782 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1784 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1785 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1786 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1792 if (!gtk_check_version(2,6,0))
1794 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
1796 GValue gvalue
= { 0, };
1797 g_value_init( &gvalue
, G_TYPE_STRING
);
1799 g_value_set_string( &gvalue
, wxGTK_CONV(m_label
) );
1800 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1801 g_value_unset( &gvalue
);
1804 SetAlignment(align
);
1809 // Use custom cell code
1810 wxDataViewCustomRenderer::Init(mode
, align
);
1814 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1818 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1821 if (!gtk_check_version(2,6,0))
1823 gint tmp
= (long) value
;
1824 GValue gvalue
= { 0, };
1825 g_value_init( &gvalue
, G_TYPE_INT
);
1826 g_value_set_int( &gvalue
, tmp
);
1827 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1828 g_value_unset( &gvalue
);
1833 m_value
= (long) value
;
1835 if (m_value
< 0) m_value
= 0;
1836 if (m_value
> 100) m_value
= 100;
1842 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1847 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1849 double pct
= (double)m_value
/ 100.0;
1851 bar
.width
= (int)(cell
.width
* pct
);
1852 dc
->SetPen( *wxTRANSPARENT_PEN
);
1853 dc
->SetBrush( *wxBLUE_BRUSH
);
1854 dc
->DrawRectangle( bar
);
1856 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1857 dc
->SetPen( *wxBLACK_PEN
);
1858 dc
->DrawRectangle( cell
);
1863 wxSize
wxDataViewProgressRenderer::GetSize() const
1865 return wxSize(40,12);
1868 // ---------------------------------------------------------
1869 // wxDataViewDateRenderer
1870 // ---------------------------------------------------------
1872 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1875 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1876 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
1877 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1882 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1883 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1884 sizer
->Add( m_cal
, 1, wxGROW
);
1889 virtual void OnDismiss()
1893 void OnCalendar( wxCalendarEvent
&event
);
1895 wxCalendarCtrl
*m_cal
;
1896 wxDataViewModel
*m_model
;
1897 wxDataViewItem m_item
;
1901 DECLARE_EVENT_TABLE()
1904 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1905 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1908 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1910 wxDateTime date
= event
.GetDate();
1911 wxVariant value
= date
;
1912 m_model
->SetValue( value
, m_item
, m_col
);
1913 m_model
->ValueChanged( m_item
, m_col
);
1917 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1919 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1920 wxDataViewCellMode mode
, int align
) :
1921 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1924 SetAlignment(align
);
1927 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1929 m_date
= value
.GetDateTime();
1934 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1939 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1941 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1942 wxString tmp
= m_date
.FormatDate();
1943 RenderText( tmp
, 0, cell
, dc
, state
);
1947 wxSize
wxDataViewDateRenderer::GetSize() const
1949 wxString tmp
= m_date
.FormatDate();
1951 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1952 return wxSize(x
,y
+d
);
1955 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewModel
*model
,
1956 const wxDataViewItem
&item
, unsigned int col
)
1959 model
->GetValue( variant
, item
, col
);
1960 wxDateTime value
= variant
.GetDateTime();
1962 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1963 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1964 wxPoint pos
= wxGetMousePosition();
1967 popup
->Popup( popup
->m_cal
);
1973 // ---------------------------------------------------------
1974 // wxDataViewIconTextRenderer
1975 // ---------------------------------------------------------
1977 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1979 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1980 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1981 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1984 SetAlignment(align
);
1987 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1991 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1997 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
2002 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
2004 const wxIcon
&icon
= m_value
.GetIcon();
2008 int yoffset
= wxMax( 0, (cell
.height
- icon
.GetHeight()) / 2 );
2009 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ yoffset
);
2010 offset
= icon
.GetWidth() + 4;
2013 RenderText( m_value
.GetText(), offset
, cell
, dc
, state
);
2018 wxSize
wxDataViewIconTextRenderer::GetSize() const
2021 if (m_value
.GetIcon().IsOk())
2022 size
.x
= 4 + m_value
.GetIcon().GetWidth();
2024 GetView()->GetTextExtent( m_value
.GetText(), &x
, &y
, &d
);
2030 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
2035 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
2040 // ---------------------------------------------------------
2042 // ---------------------------------------------------------
2046 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
2047 GdkEventButton
*gdk_event
,
2048 wxDataViewColumn
*column
)
2050 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
2053 if (gdk_event
->button
== 1)
2055 gs_lastLeftClickHeader
= column
;
2057 wxDataViewCtrl
*dv
= column
->GetOwner();
2058 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2059 event
.SetDataViewColumn( column
);
2060 event
.SetModel( dv
->GetModel() );
2061 if (dv
->HandleWindowEvent( event
))
2065 if (gdk_event
->button
== 3)
2067 wxDataViewCtrl
*dv
= column
->GetOwner();
2068 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2069 event
.SetDataViewColumn( column
);
2070 event
.SetModel( dv
->GetModel() );
2071 if (dv
->HandleWindowEvent( event
))
2079 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
2080 GtkCellRenderer
*cell
,
2081 GtkTreeModel
*model
,
2087 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
2088 GtkCellRenderer
*renderer
,
2089 GtkTreeModel
*model
,
2093 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2094 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2096 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2098 wxDataViewItem
item( (void*) iter
->user_data
);
2100 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2102 if (!wx_model
->IsIndexListModel())
2105 if (wx_model
->IsContainer( item
))
2107 if (wx_model
->HasContainerColumns( item
) || (cell
->GetOwner()->GetModelColumn() == 0))
2109 GValue gvalue
= { 0, };
2110 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2111 g_value_set_boolean( &gvalue
, TRUE
);
2112 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2113 g_value_unset( &gvalue
);
2117 GValue gvalue
= { 0, };
2118 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2119 g_value_set_boolean( &gvalue
, FALSE
);
2120 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2121 g_value_unset( &gvalue
);
2128 GValue gvalue
= { 0, };
2129 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2130 g_value_set_boolean( &gvalue
, TRUE
);
2131 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2132 g_value_unset( &gvalue
);
2138 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2140 if (value
.GetType() != cell
->GetVariantType())
2141 wxLogError( wxT("Wrong type, required: %s but: %s"),
2142 value
.GetType().c_str(),
2143 cell
->GetVariantType().c_str() );
2145 cell
->SetValue( value
);
2147 if (cell
->GtkHasAttributes())
2149 wxDataViewItemAttr attr
;
2150 bool colour_set
= false;
2151 bool style_set
= false;
2152 bool weight_set
= false;
2154 if (wx_model
->GetAttr( item
, cell
->GetOwner()->GetModelColumn(), attr
))
2156 // this must be a GtkCellRendererText
2157 wxColour colour
= attr
.GetColour();
2160 const GdkColor
* const gcol
= colour
.GetColor();
2162 GValue gvalue
= { 0, };
2163 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2164 g_value_set_boxed( &gvalue
, gcol
);
2165 g_object_set_property( G_OBJECT(renderer
), "foreground_gdk", &gvalue
);
2166 g_value_unset( &gvalue
);
2171 if (attr
.GetItalic())
2173 GValue gvalue
= { 0, };
2174 g_value_init( &gvalue
, PANGO_TYPE_STYLE
);
2175 g_value_set_enum( &gvalue
, PANGO_STYLE_ITALIC
);
2176 g_object_set_property( G_OBJECT(renderer
), "style", &gvalue
);
2177 g_value_unset( &gvalue
);
2184 GValue gvalue
= { 0, };
2185 g_value_init( &gvalue
, PANGO_TYPE_WEIGHT
);
2186 g_value_set_enum( &gvalue
, PANGO_WEIGHT_BOLD
);
2187 g_object_set_property( G_OBJECT(renderer
), "weight", &gvalue
);
2188 g_value_unset( &gvalue
);
2196 GValue gvalue
= { 0, };
2197 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2198 g_value_set_boolean( &gvalue
, FALSE
);
2199 g_object_set_property( G_OBJECT(renderer
), "style-set", &gvalue
);
2200 g_value_unset( &gvalue
);
2205 GValue gvalue
= { 0, };
2206 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2207 g_value_set_boolean( &gvalue
, FALSE
);
2208 g_object_set_property( G_OBJECT(renderer
), "weight-set", &gvalue
);
2209 g_value_unset( &gvalue
);
2214 GValue gvalue
= { 0, };
2215 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2216 g_value_set_boolean( &gvalue
, FALSE
);
2217 g_object_set_property( G_OBJECT(renderer
), "foreground-set", &gvalue
);
2218 g_value_unset( &gvalue
);
2223 if (attr
.HasBackgroundColour())
2225 wxColour colour
= attr
.GetBackgroundColour();
2226 const GdkColor
* const gcol
= colour
.GetColor();
2228 GValue gvalue
= { 0, };
2229 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2230 g_value_set_boxed( &gvalue
, gcol
);
2231 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2232 g_value_unset( &gvalue
);
2236 GValue gvalue
= { 0, };
2237 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2238 g_value_set_boolean( &gvalue
, FALSE
);
2239 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2240 g_value_unset( &gvalue
);
2246 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
2248 #include <wx/listimpl.cpp>
2249 WX_DEFINE_LIST(wxDataViewColumnList
)
2251 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
2252 unsigned int model_column
, int width
,
2253 wxAlignment align
, int flags
) :
2254 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
2256 Init( align
, flags
, width
);
2258 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
2262 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
2263 unsigned int model_column
, int width
,
2264 wxAlignment align
, int flags
) :
2265 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
2267 Init( align
, flags
, width
);
2269 SetBitmap( bitmap
);
2272 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
2274 m_isConnected
= false;
2276 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
2277 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
2278 m_column
= (GtkWidget
*) column
;
2281 SetAlignment( align
);
2283 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
2284 // as GTK+ is smart and unless explicitely told, will set the minimal
2285 // width to the title's lenght, which is a better default
2287 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
2288 // that we use for the wxDataViewCtrl
2289 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
2290 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
2292 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
2294 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
2295 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
2298 wxDataViewColumn::~wxDataViewColumn()
2302 void wxDataViewColumn::OnInternalIdle()
2307 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
2309 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2312 g_signal_connect(column
->button
, "button_press_event",
2313 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
2315 m_isConnected
= true;
2320 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
2322 wxDataViewColumnBase::SetOwner( owner
);
2324 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2326 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
2329 void wxDataViewColumn::SetTitle( const wxString
&title
)
2331 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2335 // disconnect before column->button gets recreated
2336 g_signal_handlers_disconnect_by_func( column
->button
,
2337 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
2339 m_isConnected
= false;
2342 // FIXME: can it really happen that we don't have the owner here??
2343 wxDataViewCtrl
*ctrl
= GetOwner();
2344 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
2345 : wxGTK_CONV_SYS(title
) );
2347 gtk_tree_view_column_set_widget( column
, NULL
);
2350 wxString
wxDataViewColumn::GetTitle() const
2352 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
2353 return wxConvFileName
->cMB2WX(str
);
2356 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
2358 wxDataViewColumnBase::SetBitmap( bitmap
);
2360 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2363 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
2365 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
2366 if (bitmap
.GetMask())
2367 mask
= bitmap
.GetMask()->GetBitmap();
2369 if (bitmap
.HasPixbuf())
2371 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
2372 bitmap
.GetPixbuf());
2376 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
2377 bitmap
.GetPixmap(), mask
);
2379 gtk_widget_show( GTK_WIDGET(gtk_image
) );
2381 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
2385 gtk_tree_view_column_set_widget( column
, NULL
);
2389 void wxDataViewColumn::SetHidden( bool hidden
)
2391 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
2394 void wxDataViewColumn::SetResizeable( bool resizeable
)
2396 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
2399 void wxDataViewColumn::SetAlignment( wxAlignment align
)
2401 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2403 gfloat xalign
= 0.0;
2404 if (align
== wxALIGN_RIGHT
)
2406 if (align
== wxALIGN_CENTER_HORIZONTAL
||
2407 align
== wxALIGN_CENTER
)
2410 gtk_tree_view_column_set_alignment( column
, xalign
);
2413 wxAlignment
wxDataViewColumn::GetAlignment() const
2415 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
2418 return wxALIGN_RIGHT
;
2420 return wxALIGN_CENTER_HORIZONTAL
;
2422 return wxALIGN_LEFT
;
2425 void wxDataViewColumn::SetSortable( bool sortable
)
2427 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2431 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
2435 gtk_tree_view_column_set_sort_column_id( column
, -1 );
2436 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
2440 bool wxDataViewColumn::IsSortable() const
2442 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2443 return (gtk_tree_view_column_get_sort_column_id( column
) != -1);
2446 bool wxDataViewColumn::IsResizeable() const
2448 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2449 return gtk_tree_view_column_get_resizable( column
);
2452 bool wxDataViewColumn::IsHidden() const
2454 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2455 return !gtk_tree_view_column_get_visible( column
);
2458 void wxDataViewColumn::SetSortOrder( bool ascending
)
2460 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2463 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
2465 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
2467 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
2470 bool wxDataViewColumn::IsSortOrderAscending() const
2472 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2474 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
2477 void wxDataViewColumn::SetMinWidth( int width
)
2479 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2482 int wxDataViewColumn::GetMinWidth() const
2484 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2487 int wxDataViewColumn::GetWidth() const
2489 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2492 void wxDataViewColumn::SetWidth( int width
)
2494 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2498 //-----------------------------------------------------------------------------
2499 // wxGtkTreeModelNode
2500 //-----------------------------------------------------------------------------
2502 void wxGtkTreeModelNode::Resort()
2504 size_t child_count
= GetChildCount();
2505 if (child_count
== 0)
2508 size_t node_count
= GetNodesCount();
2510 if (child_count
== 1)
2512 if (node_count
== 1)
2514 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
2520 wxGtkTreeModelChildren temp
;
2521 WX_APPEND_ARRAY( temp
, m_children
);
2523 g_internal
= m_internal
;
2524 m_children
.Sort( &wxGtkTreeModelChildCmp
);
2526 gint
*new_order
= new gint
[child_count
];
2529 for (pos
= 0; pos
< child_count
; pos
++)
2531 void *id
= m_children
.Item( pos
);
2532 int old_pos
= temp
.Index( id
);
2533 new_order
[pos
] = old_pos
;
2536 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
2539 iter
.user_data
= GetItem().GetID();
2540 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
2542 GtkTreePath
*path
= m_internal
->get_path( &iter
);
2544 gtk_tree_model_rows_reordered( gtk_tree_model
, path
, &iter
, new_order
);
2546 gtk_tree_path_free (path
);
2548 delete [] new_order
;
2550 for (pos
= 0; pos
< node_count
; pos
++)
2552 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
2557 //-----------------------------------------------------------------------------
2558 // wxDataViewCtrlInternal
2559 //-----------------------------------------------------------------------------
2561 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
,
2562 wxDataViewModel
*wx_model
, GtkWxTreeModel
*gtk_model
)
2565 m_wx_model
= wx_model
;
2566 m_gtk_model
= gtk_model
;
2568 m_sort_order
= GTK_SORT_ASCENDING
;
2570 m_dataview_sort_column
= NULL
;
2572 if (!m_wx_model
->IsIndexListModel())
2576 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2578 g_object_unref( m_gtk_model
);
2581 void wxDataViewCtrlInternal::InitTree()
2583 wxDataViewItem item
;
2584 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
2586 BuildBranch( m_root
);
2589 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
2591 if (node
->GetChildCount() == 0)
2593 wxDataViewItemArray children
;
2594 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
2596 for (pos
= 0; pos
< count
; pos
++)
2598 wxDataViewItem child
= children
[pos
];
2600 if (m_wx_model
->IsContainer( child
))
2601 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
2603 node
->AddLeave( child
.GetID() );
2605 // Don't send any events here
2610 void wxDataViewCtrlInternal::Resort()
2612 if (!m_wx_model
->IsIndexListModel())
2616 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2618 if (!m_wx_model
->IsIndexListModel())
2620 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2621 if (m_wx_model
->IsContainer( item
))
2622 parent_node
->AddNode( new wxGtkTreeModelNode( parent_node
, item
, this ) );
2624 parent_node
->AddLeave( item
.GetID() );
2630 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2632 if (!m_wx_model
->IsIndexListModel())
2634 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2635 parent_node
->DeleteChild( item
.GetID() );
2641 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
2643 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
2644 event
.SetEventObject( m_owner
);
2645 event
.SetModel( m_owner
->GetModel() );
2646 event
.SetItem( item
);
2647 m_owner
->HandleWindowEvent( event
);
2652 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
2654 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
2655 event
.SetEventObject( m_owner
);
2656 event
.SetModel( m_owner
->GetModel() );
2657 event
.SetColumn( col
);
2658 event
.SetDataViewColumn( GetOwner()->GetColumn(col
) );
2659 event
.SetItem( item
);
2660 m_owner
->HandleWindowEvent( event
);
2665 bool wxDataViewCtrlInternal::Cleared()
2670 GtkTreeModelFlags
wxDataViewCtrlInternal::get_flags()
2672 if (m_wx_model
->IsIndexListModel())
2673 return GTK_TREE_MODEL_LIST_ONLY
;
2675 return GTK_TREE_MODEL_ITERS_PERSIST
;
2678 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
2680 if (m_wx_model
->IsIndexListModel())
2682 wxDataViewIndexListModel
*wx_model
= (wxDataViewIndexListModel
*) m_wx_model
;
2684 unsigned int i
= (unsigned int)gtk_tree_path_get_indices (path
)[0];
2686 if (i
>= wx_model
->GetLastIndex() + 1)
2689 iter
->stamp
= m_gtk_model
->stamp
;
2690 // user_data is just the index
2691 iter
->user_data
= (gpointer
) i
;
2697 int depth
= gtk_tree_path_get_depth( path
);
2699 wxGtkTreeModelNode
*node
= m_root
;
2702 for (i
= 0; i
< depth
; i
++)
2704 BuildBranch( node
);
2706 gint pos
= gtk_tree_path_get_indices (path
)[i
];
2707 if (pos
< 0) return FALSE
;
2708 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
2710 void* id
= node
->GetChildren().Item( (size_t) pos
);
2714 iter
->stamp
= m_gtk_model
->stamp
;
2715 iter
->user_data
= id
;
2719 size_t count
= node
->GetNodes().GetCount();
2721 for (pos2
= 0; pos2
< count
; pos2
++)
2723 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
2724 if (child_node
->GetItem().GetID() == id
)
2736 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
2738 GtkTreePath
*retval
= gtk_tree_path_new ();
2740 if (m_wx_model
->IsIndexListModel())
2742 // user_data is just the index
2743 int i
= (wxUIntPtr
) iter
->user_data
;
2744 gtk_tree_path_append_index (retval
, i
);
2748 void *id
= iter
->user_data
;
2750 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
2753 int pos
= node
->GetChildren().Index( id
);
2755 gtk_tree_path_prepend_index( retval
, pos
);
2757 id
= node
->GetItem().GetID();
2758 node
= node
->GetParent();
2765 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
2767 if (m_wx_model
->IsIndexListModel())
2769 wxDataViewIndexListModel
*wx_model
= (wxDataViewIndexListModel
*) m_wx_model
;
2771 int n
= (wxUIntPtr
) iter
->user_data
;
2776 if (n
>= (int) wx_model
->GetLastIndex())
2779 iter
->user_data
= (gpointer
) ++n
;
2783 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
2784 if( parent
== NULL
)
2787 int pos
= parent
->GetChildren().Index( iter
->user_data
);
2789 if (pos
== (int) parent
->GetChildCount()-1)
2792 iter
->stamp
= m_gtk_model
->stamp
;
2793 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
2799 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
2801 if (m_wx_model
->IsIndexListModel())
2803 // this is a list, nodes have no children
2807 iter
->stamp
= m_gtk_model
->stamp
;
2808 iter
->user_data
= (gpointer
) -1;
2814 wxDataViewItem
item( (void*) parent
->user_data
);
2816 if (!m_wx_model
->IsContainer( item
))
2819 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2820 BuildBranch( parent_node
);
2822 if (parent_node
->GetChildCount() == 0)
2825 iter
->stamp
= m_gtk_model
->stamp
;
2826 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
2832 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
2834 if (m_wx_model
->IsIndexListModel())
2836 // this is a list, nodes have no children
2841 wxDataViewItem
item( (void*) iter
->user_data
);
2843 bool is_container
= m_wx_model
->IsContainer( item
);
2848 wxGtkTreeModelNode
*node
= FindNode( iter
);
2849 BuildBranch( node
);
2851 return (node
->GetChildCount() > 0);
2855 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
2857 if (m_wx_model
->IsIndexListModel())
2859 wxDataViewIndexListModel
*wx_model
= (wxDataViewIndexListModel
*) m_wx_model
;
2862 return (gint
) wx_model
->GetLastIndex() + 1;
2868 wxDataViewItem
item( (void*) iter
->user_data
);
2870 if (!m_wx_model
->IsContainer( item
))
2873 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
2874 BuildBranch( parent_node
);
2876 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2878 return parent_node
->GetChildCount();
2882 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
2884 if (m_wx_model
->IsIndexListModel())
2886 wxDataViewIndexListModel
*wx_model
= (wxDataViewIndexListModel
*) m_wx_model
;
2894 if (n
>= (gint
) wx_model
->GetLastIndex() + 1)
2897 iter
->stamp
= m_gtk_model
->stamp
;
2898 iter
->user_data
= (gpointer
) n
;
2905 if (parent
) id
= (void*) parent
->user_data
;
2906 wxDataViewItem
item( id
);
2908 if (!m_wx_model
->IsContainer( item
))
2911 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2912 BuildBranch( parent_node
);
2914 // wxPrintf( "iter_nth_child %d\n", n );
2916 iter
->stamp
= m_gtk_model
->stamp
;
2917 iter
->user_data
= parent_node
->GetChildren().Item( n
);
2923 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
2925 if (m_wx_model
->IsIndexListModel())
2931 wxGtkTreeModelNode
*node
= FindParentNode( child
);
2935 iter
->stamp
= m_gtk_model
->stamp
;
2936 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2942 static wxGtkTreeModelNode
*
2943 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
2949 list
.DeleteContents( true );
2950 wxDataViewItem
it( item
);
2954 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2955 list
.Insert( pItem
);
2956 it
= model
->GetParent( it
);
2959 wxGtkTreeModelNode
* node
= treeNode
;
2960 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2962 if( node
&& node
->GetNodes().GetCount() != 0 )
2964 int len
= node
->GetNodes().GetCount();
2965 wxGtkTreeModelNodes nodes
= node
->GetNodes();
2967 for( ; j
< len
; j
++)
2969 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2988 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
2993 wxDataViewItem
item( (void*) iter
->user_data
);
2997 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
3001 wxLogDebug( "Not found %p", iter
->user_data
);
3009 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
3014 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
3018 wxLogDebug( "Not found %p", item
.GetID() );
3026 static wxGtkTreeModelNode
*
3027 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
3033 list
.DeleteContents( true );
3037 wxDataViewItem
it( model
->GetParent( item
) );
3040 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3041 list
.Insert( pItem
);
3042 it
= model
->GetParent( it
);
3045 wxGtkTreeModelNode
* node
= treeNode
;
3046 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
3048 if( node
&& node
->GetNodes().GetCount() != 0 )
3050 int len
= node
->GetNodes().GetCount();
3051 wxGtkTreeModelNodes nodes
= node
->GetNodes();
3053 for( ; j
< len
; j
++)
3055 if( nodes
[j
]->GetItem() == *(n
->GetData()))
3070 //Examine whether the node is item's parent node
3071 int len
= node
->GetChildCount();
3072 for( int i
= 0; i
< len
; i
++ )
3074 if( node
->GetChildren().Item( i
) == item
.GetID() )
3080 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
3085 wxDataViewItem
item( (void*) iter
->user_data
);
3089 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
3092 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
3097 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
3100 //-----------------------------------------------------------------------------
3101 // wxDataViewCtrl signal callbacks
3102 //-----------------------------------------------------------------------------
3105 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
3107 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
3110 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, dv
->GetId() );
3111 event
.SetItem( dv
->GetSelection() );
3112 event
.SetModel( dv
->GetModel() );
3113 dv
->HandleWindowEvent( event
);
3117 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
3118 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
3120 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
3123 dv
->GtkGetInternal()->get_iter( &iter
, path
);
3124 wxDataViewItem
item( (void*) iter
.user_data
);;
3125 event
.SetItem( item
);
3126 event
.SetModel( dv
->GetModel() );
3127 dv
->HandleWindowEvent( event
);
3131 wxdataview_test_expand_row_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
3132 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
3134 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, dv
->GetId() );
3136 wxDataViewItem
item( (void*) iter
->user_data
);;
3137 event
.SetItem( item
);
3138 event
.SetModel( dv
->GetModel() );
3139 dv
->HandleWindowEvent( event
);
3141 return !event
.IsAllowed();
3145 wxdataview_row_expanded_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
3146 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
3148 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
, dv
->GetId() );
3150 wxDataViewItem
item( (void*) iter
->user_data
);;
3151 event
.SetItem( item
);
3152 event
.SetModel( dv
->GetModel() );
3153 dv
->HandleWindowEvent( event
);
3157 wxdataview_test_collapse_row_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
3158 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
3160 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
, dv
->GetId() );
3162 wxDataViewItem
item( (void*) iter
->user_data
);;
3163 event
.SetItem( item
);
3164 event
.SetModel( dv
->GetModel() );
3165 dv
->HandleWindowEvent( event
);
3167 return !event
.IsAllowed();
3171 wxdataview_row_collapsed_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
3172 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
3174 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
, dv
->GetId() );
3176 wxDataViewItem
item( (void*) iter
->user_data
);;
3177 event
.SetItem( item
);
3178 event
.SetModel( dv
->GetModel() );
3179 dv
->HandleWindowEvent( event
);
3182 //-----------------------------------------------------------------------------
3184 //-----------------------------------------------------------------------------
3186 //-----------------------------------------------------------------------------
3187 // InsertChild for wxDataViewCtrl
3188 //-----------------------------------------------------------------------------
3190 static void wxInsertChildInDataViewCtrl( wxWindowGTK
* parent
, wxWindowGTK
* child
)
3192 wxDataViewCtrl
* dvc
= (wxDataViewCtrl
*) parent
;
3193 GtkWidget
*treeview
= dvc
->GtkGetTreeView();
3195 // Insert widget in GtkTreeView
3196 if (GTK_WIDGET_REALIZED(treeview
))
3197 gtk_widget_set_parent_window( child
->m_widget
,
3198 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
3199 gtk_widget_set_parent( child
->m_widget
, treeview
);
3203 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
3204 GtkAllocation
*alloc
,
3205 wxDataViewCtrl
*win
)
3207 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
3210 wxWindow
*child
= node
->GetData();
3213 gtk_widget_size_request( child
->m_widget
, &req
);
3215 GtkAllocation alloc
;
3216 alloc
.x
= child
->m_x
;
3217 alloc
.y
= child
->m_y
;
3218 alloc
.width
= child
->m_width
;
3219 alloc
.height
= child
->m_height
;
3220 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
3222 node
= node
->GetNext();
3227 //-----------------------------------------------------------------------------
3228 // "motion_notify_event"
3229 //-----------------------------------------------------------------------------
3232 gtk_dataview_motion_notify_callback( GtkWidget
*widget
,
3233 GdkEventMotion
*gdk_event
,
3234 wxDataViewCtrl
*dv
)
3236 if (gdk_event
->is_hint
)
3240 GdkModifierType state
;
3241 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
3246 GtkTreePath
*path
= NULL
;
3247 GtkTreeViewColumn
*column
= NULL
;
3250 if (gtk_tree_view_get_path_at_pos(
3251 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
3252 (int) gdk_event
->x
, (int) gdk_event
->y
,
3261 dv
->GtkGetInternal()->get_iter( &iter
, path
);
3263 // wxPrintf( "mouse %d %d\n", (int) gdk_event->x, (int) gdk_event->y );
3265 gtk_tree_path_free( path
);
3274 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3276 wxDataViewCtrl::~wxDataViewCtrl()
3279 GetModel()->RemoveNotifier( m_notifier
);
3281 // remove the model from the GtkTreeView before it gets destroyed by the
3282 // wxDataViewCtrlBase's dtor
3283 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
3288 void wxDataViewCtrl::Init()
3293 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3294 const wxPoint
& pos
, const wxSize
& size
,
3295 long style
, const wxValidator
& validator
)
3299 if (!PreCreation( parent
, pos
, size
) ||
3300 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
3302 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
3306 m_insertCallback
= wxInsertChildInDataViewCtrl
;
3308 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
3310 GtkScrolledWindowSetBorder(m_widget
, style
);
3312 m_treeview
= gtk_tree_view_new();
3313 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
3315 g_signal_connect (m_treeview
, "size_allocate",
3316 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
3319 if (!gtk_check_version(2,6,0))
3320 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
3323 if (style
& wxDV_MULTIPLE
)
3325 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3326 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
3329 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
3332 if (!gtk_check_version(2,10,0))
3334 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
3336 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
3337 (style
& wxDV_VERT_RULES
) != 0)
3338 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
3339 else if (style
& wxDV_VERT_RULES
)
3340 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
3341 else if (style
& wxDV_HORIZ_RULES
)
3342 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
3344 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
3349 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
3352 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
3353 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
3354 gtk_widget_show (m_treeview
);
3356 m_parent
->DoAddChild( this );
3360 GtkEnableSelectionEvents();
3362 g_signal_connect_after (m_treeview
, "row-activated",
3363 G_CALLBACK (wxdataview_row_activated_callback
), this);
3365 g_signal_connect (m_treeview
, "test-collapse-row",
3366 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
3368 g_signal_connect_after (m_treeview
, "row-collapsed",
3369 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
3371 g_signal_connect (m_treeview
, "test-expand-row",
3372 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
3374 g_signal_connect_after (m_treeview
, "row-expanded",
3375 G_CALLBACK (wxdataview_row_expanded_callback
), this);
3377 g_signal_connect (m_treeview
, "motion_notify_event",
3378 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
3383 void wxDataViewCtrl::OnInternalIdle()
3385 wxWindow::OnInternalIdle();
3387 unsigned int cols
= GetColumnCount();
3389 for (i
= 0; i
< cols
; i
++)
3391 wxDataViewColumn
*col
= GetColumn( i
);
3392 col
->OnInternalIdle();
3396 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3398 if (!wxDataViewCtrlBase::AssociateModel( model
))
3401 GtkWxTreeModel
*gtk_model
= wxgtk_tree_model_new();
3402 m_internal
= new wxDataViewCtrlInternal( this, model
, gtk_model
);
3403 gtk_model
->internal
= m_internal
;
3405 m_notifier
= new wxGtkDataViewModelNotifier( gtk_model
, model
, this );
3407 model
->AddNotifier( m_notifier
);
3409 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_model
) );
3411 // unref in wxDataViewCtrlInternal
3412 // g_object_unref( gtk_model );
3417 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3419 if (!wxDataViewCtrlBase::AppendColumn(col
))
3422 m_cols
.Append( col
);
3424 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
3425 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
3430 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3432 if (!wxDataViewCtrlBase::PrependColumn(col
))
3435 m_cols
.Insert( col
);
3437 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
3438 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
3443 unsigned int wxDataViewCtrl::GetColumnCount() const
3445 return m_cols
.GetCount();
3448 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3450 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
3454 wxDataViewColumnList::const_iterator iter
;
3455 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); iter
++)
3457 wxDataViewColumn
*col
= *iter
;
3458 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
3467 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3469 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
3470 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
3472 m_cols
.remove( column
);
3479 bool wxDataViewCtrl::ClearColumns()
3481 wxDataViewColumnList::iterator iter
;
3482 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); iter
++)
3484 wxDataViewColumn
*col
= *iter
;
3485 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
3486 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
3494 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3496 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetConstGtkHandle());
3498 GList
*list
= gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview
) );
3500 gint pos
= g_list_index( list
, (gconstpointer
) gtk_column
);
3502 g_list_free( list
);
3507 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3509 return m_internal
->GetDataViewSortColumn();
3512 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
3515 iter
.user_data
= item
.GetID();
3516 GtkTreePath
*path
= m_internal
->get_path( &iter
);
3517 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
3518 gtk_tree_path_free( path
);
3521 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
3524 iter
.user_data
= item
.GetID();
3525 GtkTreePath
*path
= m_internal
->get_path( &iter
);
3526 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
3527 gtk_tree_path_free( path
);
3530 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3532 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3534 if (m_windowStyle
& wxDV_MULTIPLE
)
3536 // Report the first one
3537 GtkTreeModel
*model
;
3538 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
3542 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
3544 m_internal
->get_iter( &iter
, path
);
3547 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
3548 g_list_free( list
);
3550 return wxDataViewItem( (void*) iter
.user_data
);
3556 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
3558 wxDataViewItem
item( (void*) iter
.user_data
);
3563 return wxDataViewItem(0);
3566 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3570 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3571 if (HasFlag(wxDV_MULTIPLE
))
3573 GtkTreeModel
*model
;
3574 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
3579 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
3582 m_internal
->get_iter( &iter
, path
);
3584 sel
.Add( wxDataViewItem( (void*) iter
.user_data
) );
3586 list
= g_list_next( list
);
3591 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
3592 g_list_free( list
);
3598 GtkTreeModel
*model
;
3600 gboolean has_selection
= gtk_tree_selection_get_selected( selection
, &model
, &iter
);
3603 sel
.Add( wxDataViewItem( (void*) iter
.user_data
) );
3611 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3613 GtkDisableSelectionEvents();
3615 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3617 gtk_tree_selection_unselect_all( selection
);
3620 for (i
= 0; i
< sel
.GetCount(); i
++)
3623 iter
.user_data
= (gpointer
) sel
[i
].GetID();
3624 gtk_tree_selection_select_iter( selection
, &iter
);
3627 GtkEnableSelectionEvents();
3630 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3632 GtkDisableSelectionEvents();
3634 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3637 iter
.user_data
= (gpointer
) item
.GetID();
3638 gtk_tree_selection_select_iter( selection
, &iter
);
3640 GtkEnableSelectionEvents();
3643 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3645 GtkDisableSelectionEvents();
3647 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3650 iter
.user_data
= (gpointer
) item
.GetID();
3651 gtk_tree_selection_unselect_iter( selection
, &iter
);
3653 GtkEnableSelectionEvents();
3656 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3658 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3661 iter
.user_data
= (gpointer
) item
.GetID();
3663 return gtk_tree_selection_iter_is_selected( selection
, &iter
);
3666 void wxDataViewCtrl::SelectAll()
3668 GtkDisableSelectionEvents();
3670 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3672 gtk_tree_selection_select_all( selection
);
3674 GtkEnableSelectionEvents();
3677 void wxDataViewCtrl::UnselectAll()
3679 GtkDisableSelectionEvents();
3681 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3683 gtk_tree_selection_unselect_all( selection
);
3685 GtkEnableSelectionEvents();
3688 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
3691 iter
.user_data
= (gpointer
) item
.GetID();
3692 GtkTreePath
*path
= m_internal
->get_path( &iter
);
3693 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
3694 gtk_tree_path_free( path
);
3697 void wxDataViewCtrl::HitTest( const wxPoint
&point
,
3698 wxDataViewItem
&item
, wxDataViewColumn
*&column
) const
3700 item
= wxDataViewItem(0);
3704 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
&item
,
3705 const wxDataViewColumn
*column
) const
3710 void wxDataViewCtrl::DoSetExpanderColumn()
3712 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
3713 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
3716 void wxDataViewCtrl::DoSetIndent()
3720 void wxDataViewCtrl::GtkDisableSelectionEvents()
3722 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3723 g_signal_handlers_disconnect_by_func( selection
,
3724 (gpointer
) (wxdataview_selection_changed_callback
), this);
3727 void wxDataViewCtrl::GtkEnableSelectionEvents()
3729 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3730 g_signal_connect_after (selection
, "changed",
3731 G_CALLBACK (wxdataview_selection_changed_callback
), this);
3736 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3738 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
3743 // !wxUSE_GENERICDATAVIEWCTRL
3746 // wxUSE_DATAVIEWCTRL