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/stockitem.h"
26 #include "wx/calctrl.h"
27 #include "wx/popupwin.h"
30 #include "wx/listimpl.cpp"
31 #include "wx/settings.h"
33 #include "wx/gtk/private.h"
34 #include "wx/gtk/win_gtk.h"
36 #include <gobject/gvaluecollector.h>
37 #include <gtk/gtktreemodel.h>
38 #include <gtk/gtktreesortable.h>
39 #include <gtk/gtktreednd.h>
41 #include <gdk/gdkkeysyms.h>
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 class wxDataViewCtrlInternal
;
48 wxDataViewCtrlInternal
*g_internal
= NULL
;
50 class wxGtkTreeModelNode
;
53 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
56 //-----------------------------------------------------------------------------
57 // wxDataViewCtrlInternal
58 //-----------------------------------------------------------------------------
60 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
61 WX_DEFINE_LIST(ItemList
);
63 class wxDataViewCtrlInternal
66 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
, GtkWxTreeModel
*owner
);
67 ~wxDataViewCtrlInternal();
69 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
70 GtkTreePath
*get_path( GtkTreeIter
*iter
);
71 gboolean
iter_next( GtkTreeIter
*iter
);
72 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
73 gboolean
iter_has_child( GtkTreeIter
*iter
);
74 gint
iter_n_children( GtkTreeIter
*iter
);
75 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
76 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
78 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
79 wxDataViewCtrl
* GetOwner() { return m_owner
; }
80 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
82 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
83 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
84 bool ItemChanged( const wxDataViewItem
&item
);
85 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
89 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
90 GtkSortType
GetSortOrder() { return m_sort_order
; }
92 void SetSortColumn( int column
) { m_sort_column
= column
; }
93 int GetSortColumn() { return m_sort_column
; }
95 void SetDataViewSortColumn( wxDataViewColumn
*column
) { m_dataview_sort_column
= column
; }
96 wxDataViewColumn
*GetDataViewSortColumn() { return m_dataview_sort_column
; }
98 bool IsSorted() { return (m_sort_column
>= 0); }
103 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
104 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
105 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
106 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
107 void BuildBranch( wxGtkTreeModelNode
*branch
);
110 wxGtkTreeModelNode
*m_root
;
111 wxDataViewModel
*m_wx_model
;
112 GtkWxTreeModel
*m_gtk_model
;
113 wxDataViewCtrl
*m_owner
;
114 GtkSortType m_sort_order
;
115 wxDataViewColumn
*m_dataview_sort_column
;
120 //-----------------------------------------------------------------------------
121 // wxGtkTreeModelNode
122 //-----------------------------------------------------------------------------
124 int LINKAGEMODE
wxGtkTreeModelChildCmp( void** id1
, void** id2
)
126 int ret
= g_internal
->GetDataViewModel()->Compare( *id1
, *id2
,
127 g_internal
->GetSortColumn(), (g_internal
->GetSortOrder() == GTK_SORT_ASCENDING
) );
132 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
133 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren
);
135 class wxGtkTreeModelNode
138 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
139 wxDataViewCtrlInternal
*internal
)
143 m_internal
= internal
;
146 ~wxGtkTreeModelNode()
148 size_t count
= m_children
.GetCount();
150 for (i
= 0; i
< count
; i
++)
152 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
157 unsigned int AddNode( wxGtkTreeModelNode
* child
)
159 m_nodes
.Add( child
);
161 void *id
= child
->GetItem().GetID();
163 m_children
.Add( id
);
165 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
167 g_internal
= m_internal
;
168 m_children
.Sort( &wxGtkTreeModelChildCmp
);
169 return m_children
.Index( id
);
172 return m_children
.GetCount()-1;
175 unsigned int AddLeave( void* id
)
177 m_children
.Add( id
);
179 if (m_internal
->IsSorted() || m_internal
->GetDataViewModel()->HasDefaultCompare())
181 g_internal
= m_internal
;
182 m_children
.Sort( &wxGtkTreeModelChildCmp
);
183 return m_children
.Index( id
);
186 return m_children
.GetCount()-1;
189 void DeleteChild( void* id
)
191 m_children
.Remove( id
);
193 unsigned int count
= m_nodes
.GetCount();
195 for (pos
= 0; pos
< count
; pos
++)
197 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
198 if (node
->GetItem().GetID() == id
)
200 m_nodes
.RemoveAt( pos
);
208 wxGtkTreeModelNode
* GetParent()
210 wxGtkTreeModelNodes
&GetNodes()
212 wxGtkTreeModelChildren
&GetChildren()
213 { return m_children
; }
215 unsigned int GetChildCount() { return m_children
.GetCount(); }
216 unsigned int GetNodesCount() { return m_nodes
.GetCount(); }
218 wxDataViewItem
&GetItem() { return m_item
; }
219 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
224 wxGtkTreeModelNode
*m_parent
;
225 wxGtkTreeModelNodes m_nodes
;
226 wxGtkTreeModelChildren m_children
;
227 wxDataViewItem m_item
;
228 wxDataViewCtrlInternal
*m_internal
;
232 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
236 extern bool g_blockEventsOnDrag
;
238 //-----------------------------------------------------------------------------
239 // define new GTK+ class wxGtkTreeModel
240 //-----------------------------------------------------------------------------
244 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
245 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
246 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
247 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
248 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
249 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
251 GType
gtk_wx_tree_model_get_type (void);
253 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
255 struct _GtkWxTreeModel
261 wxDataViewCtrlInternal
*internal
;
264 struct _GtkWxTreeModelClass
266 GObjectClass list_parent_class
;
269 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
270 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
271 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
272 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
273 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
274 static void wxgtk_tree_model_finalize (GObject
*object
);
275 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
276 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
277 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
279 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
282 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
284 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
288 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
290 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
292 GtkTreeIter
*parent
);
293 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
295 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
297 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
301 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
306 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
307 gint
*sort_column_id
,
309 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
312 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
314 GtkTreeIterCompareFunc func
,
316 GtkDestroyNotify destroy
);
317 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
318 GtkTreeIterCompareFunc func
,
320 GtkDestroyNotify destroy
);
321 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
325 static GObjectClass
*list_parent_class
= NULL
;
328 gtk_wx_tree_model_get_type (void)
330 static GType tree_model_type
= 0;
332 if (!tree_model_type
)
334 const GTypeInfo tree_model_info
=
336 sizeof (GtkWxTreeModelClass
),
337 NULL
, /* base_init */
338 NULL
, /* base_finalize */
339 (GClassInitFunc
) wxgtk_tree_model_class_init
,
340 NULL
, /* class_finalize */
341 NULL
, /* class_data */
342 sizeof (GtkWxTreeModel
),
344 (GInstanceInitFunc
) wxgtk_tree_model_init
,
347 static const GInterfaceInfo tree_model_iface_info
=
349 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
354 static const GInterfaceInfo sortable_iface_info
=
356 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
361 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
362 &tree_model_info
, (GTypeFlags
)0 );
364 g_type_add_interface_static (tree_model_type
,
366 &tree_model_iface_info
);
367 g_type_add_interface_static (tree_model_type
,
368 GTK_TYPE_TREE_SORTABLE
,
369 &sortable_iface_info
);
372 return tree_model_type
;
375 static GtkWxTreeModel
*
376 wxgtk_tree_model_new(void)
378 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
383 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
385 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
386 GObjectClass
*object_class
= (GObjectClass
*) klass
;
387 object_class
->finalize
= wxgtk_tree_model_finalize
;
391 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
393 iface
->get_flags
= wxgtk_tree_model_get_flags
;
394 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
395 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
396 iface
->get_iter
= wxgtk_tree_model_get_iter
;
397 iface
->get_path
= wxgtk_tree_model_get_path
;
398 iface
->get_value
= wxgtk_tree_model_get_value
;
399 iface
->iter_next
= wxgtk_tree_model_iter_next
;
400 iface
->iter_children
= wxgtk_tree_model_iter_children
;
401 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
402 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
403 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
404 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
408 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
410 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
411 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
412 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
413 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
414 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
418 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
420 tree_model
->internal
= NULL
;
421 tree_model
->stamp
= g_random_int();
425 wxgtk_tree_model_finalize (GObject
*object
)
428 (* list_parent_class
->finalize
) (object
);
433 //-----------------------------------------------------------------------------
434 // implement callbacks from wxGtkTreeModel class by letting
435 // them call the methods of wxWidgets' wxDataViewModel
436 //-----------------------------------------------------------------------------
438 static GtkTreeModelFlags
439 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
441 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model
), (GtkTreeModelFlags
)0 );
443 return GTK_TREE_MODEL_ITERS_PERSIST
;
447 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
449 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
450 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
452 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
456 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
459 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
460 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
462 GType gtype
= G_TYPE_INVALID
;
464 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
466 if (wxtype
== wxT("string"))
467 gtype
= G_TYPE_STRING
;
470 gtype
= G_TYPE_STRING
;
471 // wxFAIL_MSG( _T("non-string columns not supported yet") );
478 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
482 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
483 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
484 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
486 return wxtree_model
->internal
->get_iter( iter
, path
);
490 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
493 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
494 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
495 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
497 return wxtree_model
->internal
->get_path( iter
);
501 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
506 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
507 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
509 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
510 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
511 if (mtype
== wxT("string"))
514 g_value_init( value
, G_TYPE_STRING
);
515 wxDataViewItem
item( (void*) iter
->user_data
);
516 model
->GetValue( variant
, item
, (unsigned int) column
);
518 g_value_set_string( value
, variant
.GetString().utf8_str() );
522 wxFAIL_MSG( _T("non-string columns not supported yet") );
527 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
530 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
531 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
532 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
534 return wxtree_model
->internal
->iter_next( iter
);
538 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
542 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
543 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
544 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
546 return wxtree_model
->internal
->iter_children( iter
, parent
);
550 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
553 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
554 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
555 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
557 return wxtree_model
->internal
->iter_has_child( iter
);
561 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
564 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
565 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
566 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, 0);
568 return wxtree_model
->internal
->iter_n_children( iter
);
572 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
577 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
578 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
580 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
584 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
588 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
589 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
590 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
592 return wxtree_model
->internal
->iter_parent( iter
, child
);
596 gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
597 gint
*sort_column_id
,
600 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
602 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
604 if (!tree_model
->internal
->IsSorted())
607 *sort_column_id
= -1;
614 *sort_column_id
= tree_model
->internal
->GetSortColumn();
617 *order
= tree_model
->internal
->GetSortOrder();
622 wxDataViewColumn
*gs_lastLeftClickHeader
= NULL
;
624 void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
628 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
629 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
631 tree_model
->internal
->SetDataViewSortColumn( gs_lastLeftClickHeader
);
633 if ((sort_column_id
!= (gint
) tree_model
->internal
->GetSortColumn()) ||
634 (order
!= tree_model
->internal
->GetSortOrder()))
636 tree_model
->internal
->SetSortColumn( sort_column_id
);
637 tree_model
->internal
->SetSortOrder( order
);
639 gtk_tree_sortable_sort_column_changed (sortable
);
641 tree_model
->internal
->GetDataViewModel()->Resort();
644 if (gs_lastLeftClickHeader
)
646 wxDataViewCtrl
*dv
= tree_model
->internal
->GetOwner();
647 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, dv
->GetId() );
648 event
.SetDataViewColumn( gs_lastLeftClickHeader
);
649 event
.SetModel( dv
->GetModel() );
650 dv
->GetEventHandler()->ProcessEvent( event
);
653 gs_lastLeftClickHeader
= NULL
;
656 void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
658 GtkTreeIterCompareFunc func
,
660 GtkDestroyNotify destroy
)
662 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
663 g_return_if_fail (func
!= NULL
);
666 void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
667 GtkTreeIterCompareFunc func
,
669 GtkDestroyNotify destroy
)
671 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
672 g_return_if_fail (func
!= NULL
);
674 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
677 gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
682 //-----------------------------------------------------------------------------
683 // define new GTK+ class wxGtkRendererRenderer
684 //-----------------------------------------------------------------------------
688 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
689 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
690 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
691 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
692 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
693 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
695 GType
gtk_wx_cell_renderer_get_type (void);
697 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
698 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
700 struct _GtkWxCellRenderer
702 GtkCellRenderer parent
;
705 wxDataViewCustomRenderer
*cell
;
709 struct _GtkWxCellRendererClass
711 GtkCellRendererClass cell_parent_class
;
715 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
716 static void gtk_wx_cell_renderer_init (
717 GtkWxCellRenderer
*cell
);
718 static void gtk_wx_cell_renderer_class_init(
719 GtkWxCellRendererClass
*klass
);
720 static void gtk_wx_cell_renderer_finalize (
722 static void gtk_wx_cell_renderer_get_size (
723 GtkCellRenderer
*cell
,
725 GdkRectangle
*rectangle
,
730 static void gtk_wx_cell_renderer_render (
731 GtkCellRenderer
*cell
,
734 GdkRectangle
*background_area
,
735 GdkRectangle
*cell_area
,
736 GdkRectangle
*expose_area
,
737 GtkCellRendererState flags
);
738 static gboolean
gtk_wx_cell_renderer_activate(
739 GtkCellRenderer
*cell
,
743 GdkRectangle
*background_area
,
744 GdkRectangle
*cell_area
,
745 GtkCellRendererState flags
);
746 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
747 GtkCellRenderer
*cell
,
751 GdkRectangle
*background_area
,
752 GdkRectangle
*cell_area
,
753 GtkCellRendererState flags
);
756 static GObjectClass
*cell_parent_class
= NULL
;
761 gtk_wx_cell_renderer_get_type (void)
763 static GType cell_wx_type
= 0;
767 const GTypeInfo cell_wx_info
=
769 sizeof (GtkWxCellRendererClass
),
770 NULL
, /* base_init */
771 NULL
, /* base_finalize */
772 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
773 NULL
, /* class_finalize */
774 NULL
, /* class_data */
775 sizeof (GtkWxCellRenderer
),
777 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
780 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
781 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
788 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
791 cell
->last_click
= 0;
795 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
797 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
798 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
800 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
802 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
804 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
805 cell_class
->render
= gtk_wx_cell_renderer_render
;
806 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
807 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
811 gtk_wx_cell_renderer_finalize (GObject
*object
)
814 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
818 gtk_wx_cell_renderer_new (void)
820 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
825 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
826 GtkCellRenderer
*renderer
,
830 GdkRectangle
*background_area
,
831 GdkRectangle
*cell_area
,
832 GtkCellRendererState flags
)
834 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
835 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
836 if (!cell
->HasEditorCtrl())
840 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
846 rect
.x
+= cell_area
->x
;
847 rect
.y
+= cell_area
->y
;
848 // rect.width -= renderer->xpad * 2;
849 // rect.height -= renderer->ypad * 2;
851 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
852 wxRect
renderrect( cell_area
->x
, cell_area
->y
, cell_area
->width
, cell_area
->height
);
854 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
856 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
857 wxDataViewItem
item( (void*) iter
.user_data
);
858 gtk_tree_path_free( treepath
);
860 cell
->StartEditing( item
, renderrect
);
866 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
868 GdkRectangle
*cell_area
,
874 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
875 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
877 wxSize size
= cell
->GetSize();
879 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
880 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
887 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
891 *x_offset
= (gint
)((renderer
->xalign
*
892 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
893 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
897 *y_offset
= (gint
)((renderer
->yalign
*
898 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
899 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
907 *height
= calc_height
;
911 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
914 GdkRectangle
*background_area
,
915 GdkRectangle
*cell_area
,
916 GdkRectangle
*expose_area
,
917 GtkCellRendererState flags
)
920 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
921 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
924 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
930 rect
.x
+= cell_area
->x
;
931 rect
.y
+= cell_area
->y
;
932 rect
.width
-= renderer
->xpad
* 2;
933 rect
.height
-= renderer
->ypad
* 2;
936 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
938 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
939 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
940 if (dc
->m_window
== NULL
)
942 dc
->m_window
= window
;
947 if (flags
& GTK_CELL_RENDERER_SELECTED
)
948 state
|= wxDATAVIEW_CELL_SELECTED
;
949 if (flags
& GTK_CELL_RENDERER_PRELIT
)
950 state
|= wxDATAVIEW_CELL_PRELIT
;
951 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
952 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
953 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
954 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
955 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
956 state
|= wxDATAVIEW_CELL_FOCUSED
;
957 cell
->Render( renderrect
, dc
, state
);
962 gtk_wx_cell_renderer_activate(
963 GtkCellRenderer
*renderer
,
967 GdkRectangle
*background_area
,
968 GdkRectangle
*cell_area
,
969 GtkCellRendererState flags
)
971 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
972 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
975 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
981 rect
.x
+= cell_area
->x
;
982 rect
.y
+= cell_area
->y
;
983 rect
.width
-= renderer
->xpad
* 2;
984 rect
.height
-= renderer
->ypad
* 2;
986 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
988 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
990 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
993 cell
->GetOwner()->GetOwner()->GetInternal()->get_iter( &iter
, treepath
);
994 wxDataViewItem
item( iter
.user_data
);
995 gtk_tree_path_free( treepath
);
997 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1003 // activated by <ENTER>
1004 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1009 else if (event
->type
== GDK_BUTTON_PRESS
)
1011 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
1012 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
1013 ((int) button_event
->y
) - renderrect
.y
);
1016 if (button_event
->button
== 1)
1018 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
1020 // TODO: query system double-click time
1021 if (button_event
->time
- wxrenderer
->last_click
< 400)
1022 if (cell
->Activate( renderrect
, model
, item
, model_col
))
1025 if (button_event
->button
== 3)
1027 if (cell
->RightClick( pt
, renderrect
, model
, item
, model_col
))
1031 wxrenderer
->last_click
= button_event
->time
;
1039 // ---------------------------------------------------------
1040 // wxGtkDataViewModelNotifier
1041 // ---------------------------------------------------------
1043 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1046 wxGtkDataViewModelNotifier( GtkWxTreeModel
*wxgtk_model
,
1047 wxDataViewModel
*wx_model
,
1048 wxDataViewCtrl
*ctrl
);
1049 ~wxGtkDataViewModelNotifier();
1051 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1052 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1053 virtual bool ItemChanged( const wxDataViewItem
&item
);
1054 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
1055 virtual bool Cleared();
1056 virtual void Resort();
1058 GtkWxTreeModel
*m_wxgtk_model
;
1059 wxDataViewModel
*m_wx_model
;
1060 wxDataViewCtrl
*m_owner
;
1063 // ---------------------------------------------------------
1064 // wxGtkDataViewListModelNotifier
1065 // ---------------------------------------------------------
1067 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1068 GtkWxTreeModel
* wxgtk_model
, wxDataViewModel
*wx_model
,
1069 wxDataViewCtrl
*ctrl
)
1071 m_wxgtk_model
= wxgtk_model
;
1072 m_wx_model
= wx_model
;
1076 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1079 m_wxgtk_model
= NULL
;
1082 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1084 m_owner
->GtkGetInternal()->ItemAdded( parent
, item
);
1087 iter
.stamp
= m_wxgtk_model
->stamp
;
1088 iter
.user_data
= (gpointer
) item
.GetID();
1090 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1091 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1092 gtk_tree_model_row_inserted(
1093 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1094 gtk_tree_path_free (path
);
1099 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1102 iter
.stamp
= m_wxgtk_model
->stamp
;
1103 iter
.user_data
= (gpointer
) item
.GetID();
1105 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1106 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1107 gtk_tree_model_row_deleted(
1108 GTK_TREE_MODEL(m_wxgtk_model
), path
);
1109 gtk_tree_path_free (path
);
1111 m_owner
->GtkGetInternal()->ItemDeleted( parent
, item
);
1116 void wxGtkDataViewModelNotifier::Resort()
1118 m_owner
->GtkGetInternal()->Resort();
1121 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1124 iter
.stamp
= m_wxgtk_model
->stamp
;
1125 iter
.user_data
= (gpointer
) item
.GetID();
1127 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1128 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1129 gtk_tree_model_row_changed(
1130 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1131 gtk_tree_path_free (path
);
1133 m_owner
->GtkGetInternal()->ItemChanged( item
);
1138 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_col
)
1140 // This adds GTK+'s missing MVC logic for ValueChanged
1142 for (index
= 0; index
< m_owner
->GetColumnCount(); index
++)
1144 wxDataViewColumn
*column
= m_owner
->GetColumn( index
);
1145 if (column
->GetModelColumn() == model_col
)
1147 GtkTreeView
*widget
= GTK_TREE_VIEW(m_owner
->m_treeview
);
1148 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1152 iter
.stamp
= m_wxgtk_model
->stamp
;
1153 iter
.user_data
= (gpointer
) item
.GetID();
1154 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1155 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1156 GdkRectangle cell_area
;
1157 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1158 gtk_tree_path_free( path
);
1160 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1161 double d
= gtk_adjustment_get_value( hadjust
);
1162 int xdiff
= (int) d
;
1164 int ydiff
= gcolumn
->button
->allocation
.height
;
1166 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1167 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1169 m_owner
->GtkGetInternal()->ValueChanged( item
, model_col
);
1178 bool wxGtkDataViewModelNotifier::Cleared()
1180 // TODO: delete everything
1182 m_owner
->GtkGetInternal()->Cleared();
1187 // ---------------------------------------------------------
1188 // wxDataViewRenderer
1189 // ---------------------------------------------------------
1191 static gpointer s_user_data
= NULL
;
1194 wxgtk_cell_editable_editing_done( GtkCellEditable
*editable
,
1195 wxDataViewRenderer
*wxrenderer
)
1197 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1198 wxDataViewCtrl
*dv
= column
->GetOwner();
1199 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv
->GetId() );
1200 event
.SetDataViewColumn( column
);
1201 event
.SetModel( dv
->GetModel() );
1202 wxDataViewItem
item( s_user_data
);
1203 event
.SetItem( item
);
1204 dv
->GetEventHandler()->ProcessEvent( event
);
1208 wxgtk_renderer_editing_started( GtkCellRenderer
*cell
, GtkCellEditable
*editable
,
1209 gchar
*path
, wxDataViewRenderer
*wxrenderer
)
1211 wxDataViewColumn
*column
= wxrenderer
->GetOwner();
1212 wxDataViewCtrl
*dv
= column
->GetOwner();
1213 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv
->GetId() );
1214 event
.SetDataViewColumn( column
);
1215 event
.SetModel( dv
->GetModel() );
1216 GtkTreePath
*tree_path
= gtk_tree_path_new_from_string( path
);
1218 dv
->GtkGetInternal()->get_iter( &iter
, tree_path
);
1219 gtk_tree_path_free( tree_path
);
1220 wxDataViewItem
item( iter
.user_data
);
1221 event
.SetItem( item
);
1222 dv
->GetEventHandler()->ProcessEvent( event
);
1224 if (GTK_IS_CELL_EDITABLE(editable
))
1226 s_user_data
= iter
.user_data
;
1228 g_signal_connect (GTK_CELL_EDITABLE (editable
), "editing_done",
1229 G_CALLBACK (wxgtk_cell_editable_editing_done
),
1230 (gpointer
) wxrenderer
);
1236 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1238 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1240 wxDataViewRendererBase( varianttype
, mode
, align
)
1244 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1245 // after the m_renderer pointer has been initialized
1248 void wxDataViewRenderer::GtkInitHandlers()
1250 if (!gtk_check_version(2,6,0))
1252 g_signal_connect (GTK_CELL_RENDERER(m_renderer
), "editing_started",
1253 G_CALLBACK (wxgtk_renderer_editing_started
),
1258 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1260 GtkCellRendererMode gtkMode
;
1263 case wxDATAVIEW_CELL_INERT
:
1264 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1266 case wxDATAVIEW_CELL_ACTIVATABLE
:
1267 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1269 case wxDATAVIEW_CELL_EDITABLE
:
1270 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1274 // This value is most often ignored in GtkTreeView
1275 GValue gvalue
= { 0, };
1276 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1277 g_value_set_enum( &gvalue
, gtkMode
);
1278 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1279 g_value_unset( &gvalue
);
1282 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1284 wxDataViewCellMode ret
;
1287 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1289 switch (g_value_get_enum(&gvalue
))
1291 case GTK_CELL_RENDERER_MODE_INERT
:
1292 ret
= wxDATAVIEW_CELL_INERT
;
1294 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1295 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1297 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1298 ret
= wxDATAVIEW_CELL_EDITABLE
;
1302 g_value_unset( &gvalue
);
1307 void wxDataViewRenderer::SetAlignment( int align
)
1309 // horizontal alignment:
1311 gfloat xalign
= 0.0;
1312 if (align
& wxALIGN_RIGHT
)
1314 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1317 GValue gvalue
= { 0, };
1318 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1319 g_value_set_float( &gvalue
, xalign
);
1320 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1321 g_value_unset( &gvalue
);
1323 // vertical alignment:
1325 gfloat yalign
= 0.0;
1326 if (align
& wxALIGN_BOTTOM
)
1328 else if (align
& wxALIGN_CENTER_VERTICAL
)
1331 GValue gvalue2
= { 0, };
1332 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1333 g_value_set_float( &gvalue2
, yalign
);
1334 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1335 g_value_unset( &gvalue2
);
1338 int wxDataViewRenderer::GetAlignment() const
1343 // horizontal alignment:
1345 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
1346 float xalign
= g_value_get_float( &gvalue
);
1348 ret
|= wxALIGN_LEFT
;
1349 else if (xalign
== 0.5)
1350 ret
|= wxALIGN_CENTER_HORIZONTAL
;
1352 ret
|= wxALIGN_RIGHT
;
1353 g_value_unset( &gvalue
);
1356 // vertical alignment:
1358 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
1359 float yalign
= g_value_get_float( &gvalue
);
1362 else if (yalign
== 0.5)
1363 ret
|= wxALIGN_CENTER_VERTICAL
;
1365 ret
|= wxALIGN_BOTTOM
;
1366 g_value_unset( &gvalue
);
1373 // ---------------------------------------------------------
1374 // wxDataViewTextRenderer
1375 // ---------------------------------------------------------
1378 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1379 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1382 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1383 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1385 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1387 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1388 wxVariant value
= tmp
;
1389 if (!cell
->Validate( value
))
1392 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1394 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1396 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, path
);
1397 wxDataViewItem
item( (void*) iter
.user_data
);;
1398 gtk_tree_path_free( path
);
1400 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1402 model
->SetValue( value
, item
, model_col
);
1403 model
->ValueChanged( item
, model_col
);
1406 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1408 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1410 wxDataViewRenderer( varianttype
, mode
, align
)
1412 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_text_new();
1414 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1416 GValue gvalue
= { 0, };
1417 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1418 g_value_set_boolean( &gvalue
, true );
1419 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1420 g_value_unset( &gvalue
);
1422 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1428 SetAlignment(align
);
1431 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1433 wxString tmp
= value
;
1435 GValue gvalue
= { 0, };
1436 g_value_init( &gvalue
, G_TYPE_STRING
);
1437 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( tmp
, GetOwner()->GetOwner()->GetFont() ) );
1438 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1439 g_value_unset( &gvalue
);
1444 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1446 GValue gvalue
= { 0, };
1447 g_value_init( &gvalue
, G_TYPE_STRING
);
1448 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1449 wxString tmp
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
),
1450 wx_const_cast(wxDataViewTextRenderer
*, this)->GetOwner()->GetOwner()->GetFont() );
1451 g_value_unset( &gvalue
);
1458 void wxDataViewTextRenderer::SetAlignment( int align
)
1460 wxDataViewRenderer::SetAlignment(align
);
1462 if (gtk_check_version(2,10,0))
1465 // horizontal alignment:
1466 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1467 if (align
& wxALIGN_RIGHT
)
1468 pangoAlign
= PANGO_ALIGN_RIGHT
;
1469 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1470 pangoAlign
= PANGO_ALIGN_CENTER
;
1472 GValue gvalue
= { 0, };
1473 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1474 g_value_set_enum( &gvalue
, pangoAlign
);
1475 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1476 g_value_unset( &gvalue
);
1479 // ---------------------------------------------------------
1480 // wxDataViewBitmapRenderer
1481 // ---------------------------------------------------------
1483 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1485 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1487 wxDataViewRenderer( varianttype
, mode
, align
)
1489 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_pixbuf_new();
1492 SetAlignment(align
);
1495 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1497 if (value
.GetType() == wxT("wxBitmap"))
1502 // This may create a Pixbuf representation in the
1503 // wxBitmap object (and it will stay there)
1504 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1506 GValue gvalue
= { 0, };
1507 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1508 g_value_set_object( &gvalue
, pixbuf
);
1509 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1510 g_value_unset( &gvalue
);
1515 if (value
.GetType() == wxT("wxIcon"))
1520 // This may create a Pixbuf representation in the
1521 // wxBitmap object (and it will stay there)
1522 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1524 GValue gvalue
= { 0, };
1525 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1526 g_value_set_object( &gvalue
, pixbuf
);
1527 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1528 g_value_unset( &gvalue
);
1536 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1541 // ---------------------------------------------------------
1542 // wxDataViewToggleRenderer
1543 // ---------------------------------------------------------
1546 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1547 gchar
*path
, gpointer user_data
);
1550 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1551 gchar
*path
, gpointer user_data
)
1553 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1556 GValue gvalue
= { 0, };
1557 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1558 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1559 bool tmp
= g_value_get_boolean( &gvalue
);
1560 g_value_unset( &gvalue
);
1564 wxVariant value
= tmp
;
1565 if (!cell
->Validate( value
))
1568 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1570 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1572 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, gtk_path
);
1573 wxDataViewItem
item( (void*) iter
.user_data
);;
1574 gtk_tree_path_free( gtk_path
);
1576 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1578 model
->SetValue( value
, item
, model_col
);
1579 model
->ValueChanged( item
, model_col
);
1582 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1584 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1585 wxDataViewCellMode mode
, int align
) :
1586 wxDataViewRenderer( varianttype
, mode
, align
)
1588 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
1590 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1592 g_signal_connect_after( m_renderer
, "toggled",
1593 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1597 GValue gvalue
= { 0, };
1598 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1599 g_value_set_boolean( &gvalue
, false );
1600 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1601 g_value_unset( &gvalue
);
1605 SetAlignment(align
);
1608 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1612 GValue gvalue
= { 0, };
1613 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1614 g_value_set_boolean( &gvalue
, tmp
);
1615 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1616 g_value_unset( &gvalue
);
1621 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1623 GValue gvalue
= { 0, };
1624 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1625 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1626 bool tmp
= g_value_get_boolean( &gvalue
);
1627 g_value_unset( &gvalue
);
1634 // ---------------------------------------------------------
1635 // wxDataViewCustomRenderer
1636 // ---------------------------------------------------------
1638 class wxDataViewCtrlDC
: public wxWindowDC
1641 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1643 GtkWidget
*widget
= window
->m_treeview
;
1647 m_context
= window
->GtkGetPangoDefaultContext();
1648 m_layout
= pango_layout_new( m_context
);
1649 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1651 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1653 // Set m_window later
1655 // m_owner = window;
1659 // ---------------------------------------------------------
1660 // wxDataViewCustomRenderer
1661 // ---------------------------------------------------------
1663 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1665 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1666 wxDataViewCellMode mode
, int align
,
1668 wxDataViewRenderer( varianttype
, mode
, align
)
1678 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
1680 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
1681 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
1682 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
1683 view
->GetForegroundColour();
1684 dc
->SetTextForeground(col
);
1685 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
1688 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1690 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1691 renderer
->cell
= this;
1693 m_renderer
= (GtkCellRenderer
*) renderer
;
1696 SetAlignment(align
);
1703 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1709 wxDC
*wxDataViewCustomRenderer::GetDC()
1713 if (GetOwner() == NULL
)
1715 if (GetOwner()->GetOwner() == NULL
)
1717 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1723 // ---------------------------------------------------------
1724 // wxDataViewProgressRenderer
1725 // ---------------------------------------------------------
1727 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1729 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1730 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1731 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1737 if (!gtk_check_version(2,6,0))
1739 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
1741 GValue gvalue
= { 0, };
1742 g_value_init( &gvalue
, G_TYPE_STRING
);
1744 // FIXME: font encoding support
1745 g_value_set_string( &gvalue
, wxGTK_CONV_SYS(m_label
) );
1746 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1747 g_value_unset( &gvalue
);
1750 SetAlignment(align
);
1755 // Use custom cell code
1756 wxDataViewCustomRenderer::Init(mode
, align
);
1760 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1764 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1767 if (!gtk_check_version(2,6,0))
1769 gint tmp
= (long) value
;
1770 GValue gvalue
= { 0, };
1771 g_value_init( &gvalue
, G_TYPE_INT
);
1772 g_value_set_int( &gvalue
, tmp
);
1773 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1774 g_value_unset( &gvalue
);
1779 m_value
= (long) value
;
1781 if (m_value
< 0) m_value
= 0;
1782 if (m_value
> 100) m_value
= 100;
1788 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1793 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1795 double pct
= (double)m_value
/ 100.0;
1797 bar
.width
= (int)(cell
.width
* pct
);
1798 dc
->SetPen( *wxTRANSPARENT_PEN
);
1799 dc
->SetBrush( *wxBLUE_BRUSH
);
1800 dc
->DrawRectangle( bar
);
1802 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1803 dc
->SetPen( *wxBLACK_PEN
);
1804 dc
->DrawRectangle( cell
);
1809 wxSize
wxDataViewProgressRenderer::GetSize() const
1811 return wxSize(40,12);
1814 // ---------------------------------------------------------
1815 // wxDataViewDateRenderer
1816 // ---------------------------------------------------------
1818 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1821 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1822 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
1823 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1828 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1829 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1830 sizer
->Add( m_cal
, 1, wxGROW
);
1835 virtual void OnDismiss()
1839 void OnCalendar( wxCalendarEvent
&event
);
1841 wxCalendarCtrl
*m_cal
;
1842 wxDataViewModel
*m_model
;
1843 wxDataViewItem m_item
;
1847 DECLARE_EVENT_TABLE()
1850 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1851 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1854 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1856 wxDateTime date
= event
.GetDate();
1857 wxVariant value
= date
;
1858 m_model
->SetValue( value
, m_item
, m_col
);
1859 m_model
->ValueChanged( m_item
, m_col
);
1863 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1865 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1866 wxDataViewCellMode mode
, int align
) :
1867 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1870 SetAlignment(align
);
1873 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1875 m_date
= value
.GetDateTime();
1880 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1885 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1887 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1888 wxString tmp
= m_date
.FormatDate();
1889 RenderText( tmp
, 0, cell
, dc
, state
);
1893 wxSize
wxDataViewDateRenderer::GetSize() const
1895 wxString tmp
= m_date
.FormatDate();
1897 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1898 return wxSize(x
,y
+d
);
1901 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewModel
*model
,
1902 const wxDataViewItem
&item
, unsigned int col
)
1905 model
->GetValue( variant
, item
, col
);
1906 wxDateTime value
= variant
.GetDateTime();
1908 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1909 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1910 wxPoint pos
= wxGetMousePosition();
1913 popup
->Popup( popup
->m_cal
);
1919 // ---------------------------------------------------------
1920 // wxDataViewIconTextRenderer
1921 // ---------------------------------------------------------
1923 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1925 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1926 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1927 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1930 SetAlignment(align
);
1933 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1937 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1943 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
1948 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1950 const wxIcon
&icon
= m_value
.GetIcon();
1954 int yoffset
= wxMax( 0, (cell
.height
- icon
.GetHeight()) / 2 );
1955 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ yoffset
);
1956 offset
= icon
.GetWidth() + 4;
1959 RenderText( m_value
.GetText(), offset
, cell
, dc
, state
);
1964 wxSize
wxDataViewIconTextRenderer::GetSize() const
1967 if (m_value
.GetIcon().IsOk())
1968 size
.x
= 4 + m_value
.GetIcon().GetWidth();
1970 GetView()->GetTextExtent( m_value
.GetText(), &x
, &y
, &d
);
1976 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1981 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1986 // ---------------------------------------------------------
1988 // ---------------------------------------------------------
1992 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
1993 GdkEventButton
*gdk_event
,
1994 wxDataViewColumn
*column
)
1996 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
1999 if (gdk_event
->button
== 1)
2001 gs_lastLeftClickHeader
= column
;
2003 wxDataViewCtrl
*dv
= column
->GetOwner();
2004 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
2005 event
.SetDataViewColumn( column
);
2006 event
.SetModel( dv
->GetModel() );
2007 if (dv
->GetEventHandler()->ProcessEvent( event
))
2011 if (gdk_event
->button
== 3)
2013 wxDataViewCtrl
*dv
= column
->GetOwner();
2014 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, dv
->GetId() );
2015 event
.SetDataViewColumn( column
);
2016 event
.SetModel( dv
->GetModel() );
2017 if (dv
->GetEventHandler()->ProcessEvent( event
))
2025 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
2026 GtkCellRenderer
*cell
,
2027 GtkTreeModel
*model
,
2033 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
2034 GtkCellRenderer
*renderer
,
2035 GtkTreeModel
*model
,
2039 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
2040 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
2042 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
2044 wxDataViewItem
item( (void*) iter
->user_data
);
2046 wxDataViewModel
*wx_model
= tree_model
->internal
->GetDataViewModel();
2048 if (wx_model
->IsContainer( item
))
2050 if (wx_model
->HasContainerColumns( item
) || (cell
->GetOwner()->GetModelColumn() == 0))
2052 GValue gvalue
= { 0, };
2053 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2054 g_value_set_boolean( &gvalue
, TRUE
);
2055 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2056 g_value_unset( &gvalue
);
2060 GValue gvalue
= { 0, };
2061 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2062 g_value_set_boolean( &gvalue
, FALSE
);
2063 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2064 g_value_unset( &gvalue
);
2071 GValue gvalue
= { 0, };
2072 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2073 g_value_set_boolean( &gvalue
, TRUE
);
2074 g_object_set_property( G_OBJECT(renderer
), "visible", &gvalue
);
2075 g_value_unset( &gvalue
);
2079 wx_model
->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
2081 if (value
.GetType() != cell
->GetVariantType())
2082 wxLogError( wxT("Wrong type, required: %s but: %s"),
2083 value
.GetType().c_str(),
2084 cell
->GetVariantType().c_str() );
2086 cell
->SetValue( value
);
2089 wxListItemAttr attr
;
2090 wx_model
->GetAttr( item
, attr
, cell
->GetOwner()->GetModelColumn() );
2092 if (attr
.HasBackgroundColour())
2094 wxColour colour
= attr
.GetBackgroundColour();
2095 const GdkColor
* const gcol
= colour
.GetColor();
2097 GValue gvalue
= { 0, };
2098 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
2099 g_value_set_boxed( &gvalue
, gcol
);
2100 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
2101 g_value_unset( &gvalue
);
2105 GValue gvalue
= { 0, };
2106 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
2107 g_value_set_boolean( &gvalue
, FALSE
);
2108 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
2109 g_value_unset( &gvalue
);
2115 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
2117 #include <wx/listimpl.cpp>
2118 WX_DEFINE_LIST(wxDataViewColumnList
);
2120 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
2121 unsigned int model_column
, int width
,
2122 wxAlignment align
, int flags
) :
2123 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
2125 Init( align
, flags
, width
);
2127 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
2131 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
2132 unsigned int model_column
, int width
,
2133 wxAlignment align
, int flags
) :
2134 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
2136 Init( align
, flags
, width
);
2138 SetBitmap( bitmap
);
2141 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
2143 m_isConnected
= false;
2145 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
2146 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
2147 m_column
= (GtkWidget
*) column
;
2150 SetAlignment( align
);
2152 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
2153 // as GTK+ is smart and unless explicitely told, will set the minimal
2154 // width to the title's lenght, which is a better default
2156 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
2157 // that we use for the wxDataViewCtrl
2158 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
2159 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
2161 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
2163 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
2164 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
2167 wxDataViewColumn::~wxDataViewColumn()
2171 void wxDataViewColumn::OnInternalIdle()
2176 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
2178 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2181 g_signal_connect(column
->button
, "button_press_event",
2182 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
2184 m_isConnected
= true;
2189 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
2191 wxDataViewColumnBase::SetOwner( owner
);
2193 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2195 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
2198 void wxDataViewColumn::SetTitle( const wxString
&title
)
2200 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2204 // disconnect before column->button gets recreated
2205 g_signal_handlers_disconnect_by_func( column
->button
,
2206 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
2208 m_isConnected
= false;
2211 // FIXME: can it really happen that we don't have the owner here??
2212 wxDataViewCtrl
*ctrl
= GetOwner();
2213 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
2214 : wxGTK_CONV_SYS(title
) );
2216 gtk_tree_view_column_set_widget( column
, NULL
);
2219 wxString
wxDataViewColumn::GetTitle() const
2221 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
2222 return wxConvFileName
->cMB2WX(str
);
2225 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
2227 wxDataViewColumnBase::SetBitmap( bitmap
);
2229 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2232 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
2234 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
2235 if (bitmap
.GetMask())
2236 mask
= bitmap
.GetMask()->GetBitmap();
2238 if (bitmap
.HasPixbuf())
2240 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
2241 bitmap
.GetPixbuf());
2245 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
2246 bitmap
.GetPixmap(), mask
);
2248 gtk_widget_show( GTK_WIDGET(gtk_image
) );
2250 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
2254 gtk_tree_view_column_set_widget( column
, NULL
);
2258 void wxDataViewColumn::SetHidden( bool hidden
)
2260 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
2263 void wxDataViewColumn::SetResizeable( bool resizeable
)
2265 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
2268 void wxDataViewColumn::SetAlignment( wxAlignment align
)
2270 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2272 gfloat xalign
= 0.0;
2273 if (align
== wxALIGN_RIGHT
)
2275 if (align
== wxALIGN_CENTER_HORIZONTAL
||
2276 align
== wxALIGN_CENTER
)
2279 gtk_tree_view_column_set_alignment( column
, xalign
);
2282 wxAlignment
wxDataViewColumn::GetAlignment() const
2284 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
2287 return wxALIGN_RIGHT
;
2289 return wxALIGN_CENTER_HORIZONTAL
;
2291 return wxALIGN_LEFT
;
2294 void wxDataViewColumn::SetSortable( bool sortable
)
2296 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2300 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
2304 gtk_tree_view_column_set_sort_column_id( column
, -1 );
2305 gtk_tree_view_column_set_sort_indicator( column
, FALSE
);
2309 bool wxDataViewColumn::IsSortable() const
2311 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2312 return (gtk_tree_view_column_get_sort_column_id( column
) != -1);
2315 bool wxDataViewColumn::IsResizeable() const
2317 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2318 return gtk_tree_view_column_get_resizable( column
);
2321 bool wxDataViewColumn::IsHidden() const
2323 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2324 return !gtk_tree_view_column_get_visible( column
);
2327 void wxDataViewColumn::SetSortOrder( bool ascending
)
2329 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2332 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
2334 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
2336 gtk_tree_view_column_set_sort_indicator( column
, TRUE
);
2339 bool wxDataViewColumn::IsSortOrderAscending() const
2341 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2343 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
2346 void wxDataViewColumn::SetMinWidth( int width
)
2348 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2351 int wxDataViewColumn::GetMinWidth() const
2353 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2356 int wxDataViewColumn::GetWidth() const
2358 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2361 void wxDataViewColumn::SetWidth( int width
)
2363 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2367 //-----------------------------------------------------------------------------
2368 // wxGtkTreeModelNode
2369 //-----------------------------------------------------------------------------
2371 void wxGtkTreeModelNode::Resort()
2373 size_t child_count
= GetChildCount();
2374 if (child_count
== 0)
2377 size_t node_count
= GetNodesCount();
2379 if (child_count
== 1)
2381 if (node_count
== 1)
2383 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
2389 wxGtkTreeModelChildren temp
;
2390 WX_APPEND_ARRAY( temp
, m_children
);
2392 g_internal
= m_internal
;
2393 m_children
.Sort( &wxGtkTreeModelChildCmp
);
2395 gint
*new_order
= new gint
[child_count
];
2398 for (pos
= 0; pos
< child_count
; pos
++)
2400 void *id
= m_children
.Item( pos
);
2401 int old_pos
= temp
.Index( id
);
2402 new_order
[pos
] = old_pos
;
2405 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
2408 iter
.user_data
= GetItem().GetID();
2409 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
2411 GtkTreePath
*path
= m_internal
->get_path( &iter
);
2413 gtk_tree_model_rows_reordered( gtk_tree_model
, path
, &iter
, new_order
);
2415 gtk_tree_path_free (path
);
2417 delete [] new_order
;
2419 for (pos
= 0; pos
< node_count
; pos
++)
2421 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
2426 //-----------------------------------------------------------------------------
2427 // wxDataViewCtrlInternal
2428 //-----------------------------------------------------------------------------
2430 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
,
2431 wxDataViewModel
*wx_model
, GtkWxTreeModel
*gtk_model
)
2434 m_wx_model
= wx_model
;
2435 m_gtk_model
= gtk_model
;
2437 m_sort_order
= GTK_SORT_ASCENDING
;
2439 m_dataview_sort_column
= NULL
;
2443 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2445 g_object_unref( m_gtk_model
);
2448 void wxDataViewCtrlInternal::InitTree()
2450 wxDataViewItem item
;
2451 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
2453 BuildBranch( m_root
);
2456 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
2458 if (node
->GetChildCount() == 0)
2460 wxDataViewItemArray children
;
2461 unsigned int count
= m_wx_model
->GetChildren( node
->GetItem(), children
);
2463 for (pos
= 0; pos
< count
; pos
++)
2465 wxDataViewItem child
= children
[pos
];
2467 if (m_wx_model
->IsContainer( child
))
2468 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
2470 node
->AddLeave( child
.GetID() );
2472 // Don't send any events here
2477 void wxDataViewCtrlInternal::Resort()
2482 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2484 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2485 if (m_wx_model
->IsContainer( item
))
2486 parent_node
->AddNode( new wxGtkTreeModelNode( parent_node
, item
, this ) );
2488 parent_node
->AddLeave( item
.GetID() );
2493 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2495 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2496 parent_node
->DeleteChild( item
.GetID() );
2501 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
2503 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
2504 event
.SetEventObject( m_owner
);
2505 event
.SetModel( m_owner
->GetModel() );
2506 event
.SetItem( item
);
2507 m_owner
->GetEventHandler()->ProcessEvent( event
);
2512 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
2514 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, m_owner
->GetId() );
2515 event
.SetEventObject( m_owner
);
2516 event
.SetModel( m_owner
->GetModel() );
2517 event
.SetColumn( col
);
2518 event
.SetDataViewColumn( GetOwner()->GetColumn(col
) );
2519 event
.SetItem( item
);
2520 m_owner
->GetEventHandler()->ProcessEvent( event
);
2525 bool wxDataViewCtrlInternal::Cleared()
2530 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
2532 int depth
= gtk_tree_path_get_depth( path
);
2534 wxGtkTreeModelNode
*node
= m_root
;
2537 for (i
= 0; i
< depth
; i
++)
2539 BuildBranch( node
);
2541 gint pos
= gtk_tree_path_get_indices (path
)[i
];
2542 if (pos
< 0) return FALSE
;
2543 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
2545 void* id
= node
->GetChildren().Item( (size_t) pos
);
2549 iter
->stamp
= m_gtk_model
->stamp
;
2550 iter
->user_data
= id
;
2554 size_t count
= node
->GetNodes().GetCount();
2556 for (pos2
= 0; pos2
< count
; pos2
++)
2558 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
2559 if (child_node
->GetItem().GetID() == id
)
2570 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
2572 GtkTreePath
*retval
= gtk_tree_path_new ();
2573 void *id
= iter
->user_data
;
2575 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
2578 int pos
= node
->GetChildren().Index( id
);
2580 gtk_tree_path_prepend_index( retval
, pos
);
2582 id
= node
->GetItem().GetID();
2583 node
= node
->GetParent();
2589 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
2591 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
2592 if( parent
== NULL
)
2595 int pos
= parent
->GetChildren().Index( iter
->user_data
);
2597 if (pos
== (int) parent
->GetChildCount()-1)
2600 iter
->stamp
= m_gtk_model
->stamp
;
2601 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
2606 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
2608 wxDataViewItem
item( (void*) parent
->user_data
);
2610 if (!m_wx_model
->IsContainer( item
))
2613 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2614 BuildBranch( parent_node
);
2616 if (parent_node
->GetChildCount() == 0)
2619 iter
->stamp
= m_gtk_model
->stamp
;
2620 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
2625 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
2627 wxDataViewItem
item( (void*) iter
->user_data
);
2629 bool is_container
= m_wx_model
->IsContainer( item
);
2634 wxGtkTreeModelNode
*node
= FindNode( iter
);
2635 BuildBranch( node
);
2637 return (node
->GetChildCount() > 0);
2640 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
2642 wxDataViewItem
item( (void*) iter
->user_data
);
2644 if (!m_wx_model
->IsContainer( item
))
2647 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
2648 BuildBranch( parent_node
);
2650 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2652 return parent_node
->GetChildCount();
2655 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
2658 if (parent
) id
= (void*) parent
->user_data
;
2659 wxDataViewItem
item( id
);
2661 if (!m_wx_model
->IsContainer( item
))
2664 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2665 BuildBranch( parent_node
);
2667 // wxPrintf( "iter_nth_child %d\n", n );
2669 iter
->stamp
= m_gtk_model
->stamp
;
2670 iter
->user_data
= parent_node
->GetChildren().Item( n
);
2675 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
2677 wxGtkTreeModelNode
*node
= FindParentNode( child
);
2681 iter
->stamp
= m_gtk_model
->stamp
;
2682 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2687 static wxGtkTreeModelNode
*
2688 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
2694 list
.DeleteContents( true );
2695 wxDataViewItem
it( item
);
2699 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2700 list
.Insert( pItem
);
2701 it
= model
->GetParent( it
);
2704 wxGtkTreeModelNode
* node
= treeNode
;
2705 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2707 if( node
&& node
->GetNodes().GetCount() != 0 )
2709 int len
= node
->GetNodes().GetCount();
2710 wxGtkTreeModelNodes nodes
= node
->GetNodes();
2712 for( ; j
< len
; j
++)
2714 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2733 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
2738 wxDataViewItem
item( (void*) iter
->user_data
);
2742 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
2746 wxLogDebug( "Not found %p", iter
->user_data
);
2754 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
2759 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
2763 wxLogDebug( "Not found %p", item
.GetID() );
2771 static wxGtkTreeModelNode
*
2772 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
2778 list
.DeleteContents( true );
2782 wxDataViewItem
it( model
->GetParent( item
) );
2785 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2786 list
.Insert( pItem
);
2787 it
= model
->GetParent( it
);
2790 wxGtkTreeModelNode
* node
= treeNode
;
2791 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2793 if( node
&& node
->GetNodes().GetCount() != 0 )
2795 int len
= node
->GetNodes().GetCount();
2796 wxGtkTreeModelNodes nodes
= node
->GetNodes();
2798 for( ; j
< len
; j
++)
2800 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2815 //Examine whether the node is item's parent node
2816 int len
= node
->GetChildCount();
2817 for( int i
= 0; i
< len
; i
++ )
2819 if( node
->GetChildren().Item( i
) == item
.GetID() )
2825 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
2830 wxDataViewItem
item( (void*) iter
->user_data
);
2834 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
2837 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
2842 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
2845 //-----------------------------------------------------------------------------
2846 // wxDataViewCtrl signal callbacks
2847 //-----------------------------------------------------------------------------
2850 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
2852 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
2855 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, dv
->GetId() );
2856 event
.SetItem( dv
->GetSelection() );
2857 event
.SetModel( dv
->GetModel() );
2858 dv
->GetEventHandler()->ProcessEvent( event
);
2862 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
2863 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
2865 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
2868 dv
->GtkGetInternal()->get_iter( &iter
, path
);
2869 wxDataViewItem
item( (void*) iter
.user_data
);;
2870 event
.SetItem( item
);
2871 event
.SetModel( dv
->GetModel() );
2872 dv
->GetEventHandler()->ProcessEvent( event
);
2876 wxdataview_test_expand_row_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
2877 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
2879 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, dv
->GetId() );
2881 wxDataViewItem
item( (void*) iter
->user_data
);;
2882 event
.SetItem( item
);
2883 event
.SetModel( dv
->GetModel() );
2884 dv
->GetEventHandler()->ProcessEvent( event
);
2886 return !event
.IsAllowed();
2890 wxdataview_row_expanded_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
2891 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
2893 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
, dv
->GetId() );
2895 wxDataViewItem
item( (void*) iter
->user_data
);;
2896 event
.SetItem( item
);
2897 event
.SetModel( dv
->GetModel() );
2898 dv
->GetEventHandler()->ProcessEvent( event
);
2902 wxdataview_test_collapse_row_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
2903 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
2905 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
, dv
->GetId() );
2907 wxDataViewItem
item( (void*) iter
->user_data
);;
2908 event
.SetItem( item
);
2909 event
.SetModel( dv
->GetModel() );
2910 dv
->GetEventHandler()->ProcessEvent( event
);
2912 return !event
.IsAllowed();
2916 wxdataview_row_collapsed_callback( GtkTreeView
* treeview
, GtkTreeIter
* iter
,
2917 GtkTreePath
*path
, wxDataViewCtrl
*dv
)
2919 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
, dv
->GetId() );
2921 wxDataViewItem
item( (void*) iter
->user_data
);;
2922 event
.SetItem( item
);
2923 event
.SetModel( dv
->GetModel() );
2924 dv
->GetEventHandler()->ProcessEvent( event
);
2927 //-----------------------------------------------------------------------------
2929 //-----------------------------------------------------------------------------
2931 //-----------------------------------------------------------------------------
2932 // InsertChild for wxDataViewCtrl
2933 //-----------------------------------------------------------------------------
2935 static void wxInsertChildInDataViewCtrl( wxWindowGTK
* parent
, wxWindowGTK
* child
)
2937 wxDataViewCtrl
* dvc
= (wxDataViewCtrl
*) parent
;
2938 GtkWidget
*treeview
= dvc
->GtkGetTreeView();
2940 // Insert widget in GtkTreeView
2941 if (GTK_WIDGET_REALIZED(treeview
))
2942 gtk_widget_set_parent_window( child
->m_widget
,
2943 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
2944 gtk_widget_set_parent( child
->m_widget
, treeview
);
2948 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
2949 GtkAllocation
*alloc
,
2950 wxDataViewCtrl
*win
)
2952 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
2955 wxWindow
*child
= node
->GetData();
2958 gtk_widget_size_request( child
->m_widget
, &req
);
2960 GtkAllocation alloc
;
2961 alloc
.x
= child
->m_x
;
2962 alloc
.y
= child
->m_y
;
2963 alloc
.width
= child
->m_width
;
2964 alloc
.height
= child
->m_height
;
2965 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
2967 node
= node
->GetNext();
2972 //-----------------------------------------------------------------------------
2973 // "motion_notify_event"
2974 //-----------------------------------------------------------------------------
2977 gtk_dataview_motion_notify_callback( GtkWidget
*widget
,
2978 GdkEventMotion
*gdk_event
,
2979 wxDataViewCtrl
*dv
)
2981 if (gdk_event
->is_hint
)
2985 GdkModifierType state
;
2986 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
2991 GtkTreePath
*path
= NULL
;
2992 GtkTreeViewColumn
*column
= NULL
;
2995 if (gtk_tree_view_get_path_at_pos(
2996 GTK_TREE_VIEW(dv
->GtkGetTreeView()),
2997 (int) gdk_event
->x
, (int) gdk_event
->y
,
3006 dv
->GtkGetInternal()->get_iter( &iter
, path
);
3008 // wxPrintf( "mouse %d %d\n", (int) gdk_event->x, (int) gdk_event->y );
3010 gtk_tree_path_free( path
);
3019 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3021 wxDataViewCtrl::~wxDataViewCtrl()
3024 GetModel()->RemoveNotifier( m_notifier
);
3026 // remove the model from the GtkTreeView before it gets destroyed by the
3027 // wxDataViewCtrlBase's dtor
3028 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
3033 void wxDataViewCtrl::Init()
3038 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3039 const wxPoint
& pos
, const wxSize
& size
,
3040 long style
, const wxValidator
& validator
)
3044 if (!PreCreation( parent
, pos
, size
) ||
3045 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
3047 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
3051 m_insertCallback
= wxInsertChildInDataViewCtrl
;
3053 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
3055 GtkScrolledWindowSetBorder(m_widget
, style
);
3057 m_treeview
= gtk_tree_view_new();
3058 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
3060 g_signal_connect (m_treeview
, "size_allocate",
3061 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
3064 if (!gtk_check_version(2,6,0))
3065 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
3068 if (style
& wxDV_MULTIPLE
)
3070 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3071 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
3074 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
3077 if (!gtk_check_version(2,10,0))
3079 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
3081 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
3082 (style
& wxDV_VERT_RULES
) != 0)
3083 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
3084 else if (style
& wxDV_VERT_RULES
)
3085 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
3086 else if (style
& wxDV_HORIZ_RULES
)
3087 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
3089 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
3094 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
3097 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
3098 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
3099 gtk_widget_show (m_treeview
);
3101 m_parent
->DoAddChild( this );
3105 GtkEnableSelectionEvents();
3107 g_signal_connect_after (m_treeview
, "row-activated",
3108 G_CALLBACK (wxdataview_row_activated_callback
), this);
3110 g_signal_connect (m_treeview
, "test-collapse-row",
3111 G_CALLBACK (wxdataview_test_collapse_row_callback
), this);
3113 g_signal_connect_after (m_treeview
, "row-collapsed",
3114 G_CALLBACK (wxdataview_row_collapsed_callback
), this);
3116 g_signal_connect (m_treeview
, "test-expand-row",
3117 G_CALLBACK (wxdataview_test_expand_row_callback
), this);
3119 g_signal_connect_after (m_treeview
, "row-expanded",
3120 G_CALLBACK (wxdataview_row_expanded_callback
), this);
3122 g_signal_connect (m_treeview
, "motion_notify_event",
3123 G_CALLBACK (gtk_dataview_motion_notify_callback
), this);
3128 void wxDataViewCtrl::OnInternalIdle()
3130 wxWindow::OnInternalIdle();
3132 unsigned int cols
= GetColumnCount();
3134 for (i
= 0; i
< cols
; i
++)
3136 wxDataViewColumn
*col
= GetColumn( i
);
3137 col
->OnInternalIdle();
3141 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3143 if (!wxDataViewCtrlBase::AssociateModel( model
))
3146 GtkWxTreeModel
*gtk_model
= wxgtk_tree_model_new();
3147 m_internal
= new wxDataViewCtrlInternal( this, model
, gtk_model
);
3148 gtk_model
->internal
= m_internal
;
3150 m_notifier
= new wxGtkDataViewModelNotifier( gtk_model
, model
, this );
3152 model
->AddNotifier( m_notifier
);
3154 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_model
) );
3156 // unref in wxDataViewCtrlInternal
3157 // g_object_unref( gtk_model );
3162 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3164 if (!wxDataViewCtrlBase::AppendColumn(col
))
3167 m_cols
.Append( col
);
3169 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
),
3170 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
3175 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3177 if (!wxDataViewCtrlBase::PrependColumn(col
))
3180 m_cols
.Insert( col
);
3182 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview
),
3183 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()), 0 );
3188 unsigned int wxDataViewCtrl::GetColumnCount() const
3190 return m_cols
.GetCount();
3193 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3195 GtkTreeViewColumn
*gtk_col
= gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview
), pos
);
3199 wxDataViewColumnList::const_iterator iter
;
3200 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); iter
++)
3202 wxDataViewColumn
*col
= *iter
;
3203 if (GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) == gtk_col
)
3212 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3214 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
3215 GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle()) );
3217 m_cols
.remove( column
);
3224 bool wxDataViewCtrl::ClearColumns()
3226 wxDataViewColumnList::iterator iter
;
3227 for (iter
= m_cols
.begin(); iter
!= m_cols
.end(); iter
++)
3229 wxDataViewColumn
*col
= *iter
;
3230 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview
),
3231 GTK_TREE_VIEW_COLUMN(col
->GetGtkHandle()) );
3239 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3241 GtkTreeViewColumn
*gtk_column
= GTK_TREE_VIEW_COLUMN(column
->GetConstGtkHandle());
3243 GList
*list
= gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview
) );
3245 gint pos
= g_list_index( list
, (gconstpointer
) gtk_column
);
3247 g_list_free( list
);
3252 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3254 return m_internal
->GetDataViewSortColumn();
3257 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
3260 iter
.user_data
= item
.GetID();
3261 GtkTreePath
*path
= m_internal
->get_path( &iter
);
3262 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview
), path
, false );
3263 gtk_tree_path_free( path
);
3266 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
3269 iter
.user_data
= item
.GetID();
3270 GtkTreePath
*path
= m_internal
->get_path( &iter
);
3271 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview
), path
);
3272 gtk_tree_path_free( path
);
3275 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3277 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3279 if (m_windowStyle
& wxDV_MULTIPLE
)
3281 // Report the first one
3282 GtkTreeModel
*model
;
3283 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
3287 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
3289 m_internal
->get_iter( &iter
, path
);
3292 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
3293 g_list_free( list
);
3295 return wxDataViewItem( (void*) iter
.user_data
);
3301 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
3303 wxDataViewItem
item( (void*) iter
.user_data
);
3308 return wxDataViewItem(0);
3311 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3315 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3316 if (HasFlag(wxDV_MULTIPLE
))
3318 GtkTreeModel
*model
;
3319 GList
*list
= gtk_tree_selection_get_selected_rows( selection
, &model
);
3324 GtkTreePath
*path
= (GtkTreePath
*) list
->data
;
3327 m_internal
->get_iter( &iter
, path
);
3329 sel
.Add( wxDataViewItem( (void*) iter
.user_data
) );
3331 list
= g_list_next( list
);
3336 g_list_foreach( list
, (GFunc
) gtk_tree_path_free
, NULL
);
3337 g_list_free( list
);
3343 GtkTreeModel
*model
;
3345 gboolean has_selection
= gtk_tree_selection_get_selected( selection
, &model
, &iter
);
3348 sel
.Add( wxDataViewItem( (void*) iter
.user_data
) );
3356 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3358 GtkDisableSelectionEvents();
3360 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3362 gtk_tree_selection_unselect_all( selection
);
3365 for (i
= 0; i
< sel
.GetCount(); i
++)
3368 iter
.user_data
= (gpointer
) sel
[i
].GetID();
3369 gtk_tree_selection_select_iter( selection
, &iter
);
3372 GtkEnableSelectionEvents();
3375 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3377 GtkDisableSelectionEvents();
3379 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3382 iter
.user_data
= (gpointer
) item
.GetID();
3383 gtk_tree_selection_select_iter( selection
, &iter
);
3385 GtkEnableSelectionEvents();
3388 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3390 GtkDisableSelectionEvents();
3392 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3395 iter
.user_data
= (gpointer
) item
.GetID();
3396 gtk_tree_selection_unselect_iter( selection
, &iter
);
3398 GtkEnableSelectionEvents();
3401 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3403 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3406 iter
.user_data
= (gpointer
) item
.GetID();
3408 return gtk_tree_selection_iter_is_selected( selection
, &iter
);
3411 void wxDataViewCtrl::SelectAll()
3413 GtkDisableSelectionEvents();
3415 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3417 gtk_tree_selection_select_all( selection
);
3419 GtkEnableSelectionEvents();
3422 void wxDataViewCtrl::UnselectAll()
3424 GtkDisableSelectionEvents();
3426 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3428 gtk_tree_selection_unselect_all( selection
);
3430 GtkEnableSelectionEvents();
3433 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
3436 iter
.user_data
= (gpointer
) item
.GetID();
3437 GtkTreePath
*path
= m_internal
->get_path( &iter
);
3438 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview
), path
, NULL
, false, 0.0, 0.0 );
3439 gtk_tree_path_free( path
);
3442 void wxDataViewCtrl::HitTest( const wxPoint
&point
,
3443 wxDataViewItem
&item
, wxDataViewColumn
*&column
) const
3445 item
= wxDataViewItem(0);
3449 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
&item
,
3450 const wxDataViewColumn
*column
) const
3455 void wxDataViewCtrl::DoSetExpanderColumn()
3457 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview
),
3458 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
3461 void wxDataViewCtrl::DoSetIndent()
3465 void wxDataViewCtrl::GtkDisableSelectionEvents()
3467 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3468 g_signal_handlers_disconnect_by_func( selection
,
3469 (gpointer
) (wxdataview_selection_changed_callback
), this);
3472 void wxDataViewCtrl::GtkEnableSelectionEvents()
3474 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
3475 g_signal_connect_after (selection
, "changed",
3476 G_CALLBACK (wxdataview_selection_changed_callback
), this);
3481 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3483 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
3488 // !wxUSE_GENERICDATAVIEWCTRL
3491 // wxUSE_DATAVIEWCTRL