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"
31 #include "wx/gtk/private.h"
32 #include "wx/gtk/win_gtk.h"
34 #include <gobject/gvaluecollector.h>
35 #include <gtk/gtktreemodel.h>
36 #include <gtk/gtktreesortable.h>
37 #include <gtk/gtktreednd.h>
39 #include <gdk/gdkkeysyms.h>
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 wxDataViewModel
*g_model
= NULL
;
46 class wxGtkTreeModelNode
;
49 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
52 //-----------------------------------------------------------------------------
53 // wxDataViewCtrlInternal
54 //-----------------------------------------------------------------------------
56 class wxDataViewCtrlInternal
59 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
, GtkWxTreeModel
*owner
);
60 ~wxDataViewCtrlInternal();
62 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
63 GtkTreePath
*get_path( GtkTreeIter
*iter
);
64 GtkTreePath
*get_path_safe( GtkTreeIter
*iter
);
65 gboolean
iter_next( GtkTreeIter
*iter
);
66 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
67 gboolean
iter_has_child( GtkTreeIter
*iter
);
68 gint
iter_n_children( GtkTreeIter
*iter
);
69 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
70 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
72 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
73 wxDataViewCtrl
* GetOwner() { return m_owner
; }
74 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
76 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
77 bool ItemDeleted( const wxDataViewItem
&item
);
83 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
84 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
85 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
86 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
87 void BuildBranch( wxGtkTreeModelNode
*branch
);
90 wxGtkTreeModelNode
*m_root
;
91 wxDataViewModel
*m_wx_model
;
92 GtkWxTreeModel
*m_gtk_model
;
93 wxDataViewCtrl
*m_owner
;
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
101 int LINKAGEMODE
wxGtkTreeModelNodeCmp( void *id1
, void *id2
);
103 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
104 WX_DEFINE_SORTED_ARRAY( void* , wxGtkTreeModelChildren
);
106 class wxGtkTreeModelNode
109 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
110 wxDataViewCtrlInternal
*internal
)
114 m_internal
= internal
;
115 g_model
= internal
->GetDataViewModel();
116 m_children
= new wxGtkTreeModelChildren( wxGtkTreeModelNodeCmp
);
119 ~wxGtkTreeModelNode()
121 size_t count
= m_children
->GetCount();
123 for (i
= 0; i
< count
; i
++)
125 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
131 unsigned int AddNode( wxGtkTreeModelNode
* child
)
133 m_nodes
.Add( child
);
134 return m_children
->Add( child
->GetItem().GetID() );
137 unsigned int AddLeave( void* id
)
139 return m_children
->Add( id
);
142 void DeleteChild( void* id
)
145 size_t count
= m_children
->GetCount();
146 for (pos
= 0; pos
< count
; pos
++)
148 if (m_children
->Item( pos
) == id
)
150 m_children
->RemoveAt( pos
);
154 count
= m_nodes
.GetCount();
155 for (pos
= 0; pos
< count
; pos
++)
157 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
158 if (node
->GetItem().GetID() == id
)
160 m_nodes
.RemoveAt( pos
);
168 wxGtkTreeModelNode
* GetParent()
170 wxGtkTreeModelNodes
&GetNodes()
172 wxGtkTreeModelChildren
&GetChildren()
173 { return *m_children
; }
175 unsigned int GetChildCount() { return m_children
->GetCount(); }
176 unsigned int GetNodesCount() { return m_nodes
.GetCount(); }
178 wxDataViewItem
&GetItem() { return m_item
; }
179 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
184 wxGtkTreeModelNode
*m_parent
;
185 wxGtkTreeModelNodes m_nodes
;
186 wxGtkTreeModelChildren
*m_children
;
187 wxDataViewItem m_item
;
188 wxDataViewCtrlInternal
*m_internal
;
192 int LINKAGEMODE
wxGtkTreeModelNodeCmp( void* id1
, void* id2
)
194 return g_model
->Compare( id1
, id2
);
197 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
201 extern bool g_blockEventsOnDrag
;
203 //-----------------------------------------------------------------------------
204 // define new GTK+ class wxGtkTreeModel
205 //-----------------------------------------------------------------------------
209 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
210 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
211 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
212 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
213 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
214 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
216 GType
gtk_wx_tree_model_get_type (void);
218 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
220 struct _GtkWxTreeModel
226 wxDataViewCtrlInternal
*internal
;
229 struct _GtkWxTreeModelClass
231 GObjectClass list_parent_class
;
234 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
235 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
236 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
237 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
238 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
239 static void wxgtk_tree_model_finalize (GObject
*object
);
240 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
241 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
242 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
244 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
247 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
249 static GtkTreePath
*wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
251 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
255 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
257 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
259 GtkTreeIter
*parent
);
260 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
262 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
264 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
268 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
273 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
274 gint
*sort_column_id
,
276 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
279 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
281 GtkTreeIterCompareFunc func
,
283 GtkDestroyNotify destroy
);
284 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
285 GtkTreeIterCompareFunc func
,
287 GtkDestroyNotify destroy
);
288 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
292 static GObjectClass
*list_parent_class
= NULL
;
295 gtk_wx_tree_model_get_type (void)
297 static GType tree_model_type
= 0;
299 if (!tree_model_type
)
301 const GTypeInfo tree_model_info
=
303 sizeof (GtkWxTreeModelClass
),
304 NULL
, /* base_init */
305 NULL
, /* base_finalize */
306 (GClassInitFunc
) wxgtk_tree_model_class_init
,
307 NULL
, /* class_finalize */
308 NULL
, /* class_data */
309 sizeof (GtkWxTreeModel
),
311 (GInstanceInitFunc
) wxgtk_tree_model_init
,
314 static const GInterfaceInfo tree_model_iface_info
=
316 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
321 static const GInterfaceInfo sortable_iface_info
=
323 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
328 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
329 &tree_model_info
, (GTypeFlags
)0 );
331 g_type_add_interface_static (tree_model_type
,
333 &tree_model_iface_info
);
334 g_type_add_interface_static (tree_model_type
,
335 GTK_TYPE_TREE_SORTABLE
,
336 &sortable_iface_info
);
339 return tree_model_type
;
342 static GtkWxTreeModel
*
343 wxgtk_tree_model_new(void)
345 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
350 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
352 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
353 GObjectClass
*object_class
= (GObjectClass
*) klass
;
354 object_class
->finalize
= wxgtk_tree_model_finalize
;
358 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
360 iface
->get_flags
= wxgtk_tree_model_get_flags
;
361 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
362 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
363 iface
->get_iter
= wxgtk_tree_model_get_iter
;
364 iface
->get_path
= wxgtk_tree_model_get_path
;
365 iface
->get_value
= wxgtk_tree_model_get_value
;
366 iface
->iter_next
= wxgtk_tree_model_iter_next
;
367 iface
->iter_children
= wxgtk_tree_model_iter_children
;
368 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
369 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
370 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
371 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
375 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
377 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
378 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
379 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
380 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
381 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
385 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
387 tree_model
->internal
= NULL
;
388 tree_model
->stamp
= g_random_int();
392 wxgtk_tree_model_finalize (GObject
*object
)
395 (* list_parent_class
->finalize
) (object
);
400 //-----------------------------------------------------------------------------
401 // implement callbacks from wxGtkTreeModel class by letting
402 // them call the methods of wxWidgets' wxDataViewModel
403 //-----------------------------------------------------------------------------
405 static GtkTreeModelFlags
406 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
408 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model
), (GtkTreeModelFlags
)0 );
410 return GTK_TREE_MODEL_ITERS_PERSIST
;
414 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
416 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
417 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
419 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
423 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
426 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
427 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
429 GType gtype
= G_TYPE_INVALID
;
431 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
433 if (wxtype
== wxT("string"))
434 gtype
= G_TYPE_STRING
;
437 wxFAIL_MSG( _T("non-string columns not supported yet") );
444 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
448 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
449 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
450 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
452 return wxtree_model
->internal
->get_iter( iter
, path
);
456 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
459 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
460 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
461 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
463 return wxtree_model
->internal
->get_path( iter
);
467 wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
470 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
471 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
472 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
474 return wxtree_model
->internal
->get_path_safe( iter
);
478 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
483 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
484 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
486 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
487 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
488 if (mtype
== wxT("string"))
491 g_value_init( value
, G_TYPE_STRING
);
492 wxDataViewItem
item( (void*) iter
->user_data
);
493 model
->GetValue( variant
, item
, (unsigned int) column
);
495 g_value_set_string( value
, variant
.GetString().utf8_str() );
499 wxFAIL_MSG( _T("non-string columns not supported yet") );
504 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
507 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
508 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
509 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
511 return wxtree_model
->internal
->iter_next( iter
);
515 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
519 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
520 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
521 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
523 return wxtree_model
->internal
->iter_children( iter
, parent
);
527 wxgtk_tree_model_iter_has_child (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_has_child( iter
);
538 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
541 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
542 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
543 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, 0);
545 return wxtree_model
->internal
->iter_n_children( iter
);
549 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
554 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
555 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
557 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
561 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
565 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
566 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
567 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
569 return wxtree_model
->internal
->iter_parent( iter
, child
);
573 gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
574 gint
*sort_column_id
,
577 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
579 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
582 *sort_column_id
= tree_model
->internal
->GetDataViewModel()->GetSortingColumn();
586 bool ascending
= tree_model
->internal
->GetDataViewModel()->GetSortOrderAscending();
588 *order
= GTK_SORT_ASCENDING
;
590 *order
= GTK_SORT_DESCENDING
;
596 void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
600 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
601 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
603 bool ascending
= TRUE
;
604 if (order
!= GTK_SORT_ASCENDING
)
607 if ((sort_column_id
== (gint
) tree_model
->internal
->GetDataViewModel()->GetSortingColumn()) &&
608 (ascending
== tree_model
->internal
->GetDataViewModel()->GetSortOrderAscending()))
611 tree_model
->internal
->GetDataViewModel()->SetSortingColumn( sort_column_id
);
613 tree_model
->internal
->GetDataViewModel()->SetSortOrderAscending( ascending
);
615 gtk_tree_sortable_sort_column_changed (sortable
);
617 tree_model
->internal
->GetDataViewModel()->Resort();
620 void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
622 GtkTreeIterCompareFunc func
,
624 GtkDestroyNotify destroy
)
626 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
627 g_return_if_fail (func
!= NULL
);
630 void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
631 GtkTreeIterCompareFunc func
,
633 GtkDestroyNotify destroy
)
635 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
636 g_return_if_fail (func
!= NULL
);
638 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
641 gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
646 //-----------------------------------------------------------------------------
647 // define new GTK+ class wxGtkRendererRenderer
648 //-----------------------------------------------------------------------------
652 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
653 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
654 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
655 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
656 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
657 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
659 GType
gtk_wx_cell_renderer_get_type (void);
661 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
662 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
664 struct _GtkWxCellRenderer
666 GtkCellRenderer parent
;
669 wxDataViewCustomRenderer
*cell
;
673 struct _GtkWxCellRendererClass
675 GtkCellRendererClass cell_parent_class
;
679 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
680 static void gtk_wx_cell_renderer_init (
681 GtkWxCellRenderer
*cell
);
682 static void gtk_wx_cell_renderer_class_init(
683 GtkWxCellRendererClass
*klass
);
684 static void gtk_wx_cell_renderer_finalize (
686 static void gtk_wx_cell_renderer_get_size (
687 GtkCellRenderer
*cell
,
689 GdkRectangle
*rectangle
,
694 static void gtk_wx_cell_renderer_render (
695 GtkCellRenderer
*cell
,
698 GdkRectangle
*background_area
,
699 GdkRectangle
*cell_area
,
700 GdkRectangle
*expose_area
,
701 GtkCellRendererState flags
);
702 static gboolean
gtk_wx_cell_renderer_activate(
703 GtkCellRenderer
*cell
,
707 GdkRectangle
*background_area
,
708 GdkRectangle
*cell_area
,
709 GtkCellRendererState flags
);
710 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
711 GtkCellRenderer
*cell
,
715 GdkRectangle
*background_area
,
716 GdkRectangle
*cell_area
,
717 GtkCellRendererState flags
);
720 static GObjectClass
*cell_parent_class
= NULL
;
725 gtk_wx_cell_renderer_get_type (void)
727 static GType cell_wx_type
= 0;
731 const GTypeInfo cell_wx_info
=
733 sizeof (GtkWxCellRendererClass
),
734 NULL
, /* base_init */
735 NULL
, /* base_finalize */
736 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
737 NULL
, /* class_finalize */
738 NULL
, /* class_data */
739 sizeof (GtkWxCellRenderer
),
741 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
744 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
745 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
752 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
755 cell
->last_click
= 0;
759 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
761 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
762 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
764 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
766 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
768 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
769 cell_class
->render
= gtk_wx_cell_renderer_render
;
770 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
771 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
775 gtk_wx_cell_renderer_finalize (GObject
*object
)
778 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
782 gtk_wx_cell_renderer_new (void)
784 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
789 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
790 GtkCellRenderer
*renderer
,
794 GdkRectangle
*background_area
,
795 GdkRectangle
*cell_area
,
796 GtkCellRendererState flags
)
798 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
799 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
800 if (!cell
->HasEditorCtrl())
804 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
810 rect
.x
+= cell_area
->x
;
811 rect
.y
+= cell_area
->y
;
812 // rect.width -= renderer->xpad * 2;
813 // rect.height -= renderer->ypad * 2;
815 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
816 wxRect
renderrect( cell_area
->x
, cell_area
->y
, cell_area
->width
, cell_area
->height
);
818 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
820 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
821 wxDataViewItem
item( (void*) iter
.user_data
);
822 gtk_tree_path_free( treepath
);
824 cell
->StartEditing( item
, renderrect
);
830 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
832 GdkRectangle
*cell_area
,
838 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
839 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
841 wxSize size
= cell
->GetSize();
843 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
844 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
851 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
855 *x_offset
= (gint
)((renderer
->xalign
*
856 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
857 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
861 *y_offset
= (gint
)((renderer
->yalign
*
862 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
863 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
871 *height
= calc_height
;
875 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
878 GdkRectangle
*background_area
,
879 GdkRectangle
*cell_area
,
880 GdkRectangle
*expose_area
,
881 GtkCellRendererState flags
)
884 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
885 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
888 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
894 rect
.x
+= cell_area
->x
;
895 rect
.y
+= cell_area
->y
;
896 rect
.width
-= renderer
->xpad
* 2;
897 rect
.height
-= renderer
->ypad
* 2;
900 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
902 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
903 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
904 if (dc
->m_window
== NULL
)
906 dc
->m_window
= window
;
911 if (flags
& GTK_CELL_RENDERER_SELECTED
)
912 state
|= wxDATAVIEW_CELL_SELECTED
;
913 if (flags
& GTK_CELL_RENDERER_PRELIT
)
914 state
|= wxDATAVIEW_CELL_PRELIT
;
915 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
916 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
917 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
918 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
919 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
920 state
|= wxDATAVIEW_CELL_FOCUSED
;
921 cell
->Render( renderrect
, dc
, state
);
926 gtk_wx_cell_renderer_activate(
927 GtkCellRenderer
*renderer
,
931 GdkRectangle
*background_area
,
932 GdkRectangle
*cell_area
,
933 GtkCellRendererState flags
)
935 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
936 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
939 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
945 rect
.x
+= cell_area
->x
;
946 rect
.y
+= cell_area
->y
;
947 rect
.width
-= renderer
->xpad
* 2;
948 rect
.height
-= renderer
->ypad
* 2;
950 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
952 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
954 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
957 gtk_tree_path_free( treepath
);
959 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
965 // activated by <ENTER>
966 if (cell
->Activate( renderrect
, model
, item
, model_col
))
971 else if (event
->type
== GDK_BUTTON_PRESS
)
973 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
974 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
975 ((int) button_event
->y
) - renderrect
.y
);
978 if (button_event
->button
== 1)
980 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
982 // TODO: query system double-click time
983 if (button_event
->time
- wxrenderer
->last_click
< 400)
984 if (cell
->Activate( renderrect
, model
, item
, model_col
))
987 if (button_event
->button
== 3)
989 if (cell
->RightClick( pt
, renderrect
, model
, item
, model_col
))
993 wxrenderer
->last_click
= button_event
->time
;
1001 // ---------------------------------------------------------
1002 // wxGtkDataViewModelNotifier
1003 // ---------------------------------------------------------
1005 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1008 wxGtkDataViewModelNotifier( GtkWxTreeModel
*wxgtk_model
,
1009 wxDataViewModel
*wx_model
,
1010 wxDataViewCtrl
*ctrl
);
1011 ~wxGtkDataViewModelNotifier();
1013 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1014 virtual bool ItemDeleted( const wxDataViewItem
&item
);
1015 virtual bool ItemChanged( const wxDataViewItem
&item
);
1016 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
1017 virtual bool Cleared();
1018 virtual void Resort();
1020 GtkWxTreeModel
*m_wxgtk_model
;
1021 wxDataViewModel
*m_wx_model
;
1022 wxDataViewCtrl
*m_owner
;
1025 // ---------------------------------------------------------
1026 // wxGtkDataViewListModelNotifier
1027 // ---------------------------------------------------------
1029 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1030 GtkWxTreeModel
* wxgtk_model
, wxDataViewModel
*wx_model
,
1031 wxDataViewCtrl
*ctrl
)
1033 m_wxgtk_model
= wxgtk_model
;
1034 m_wx_model
= wx_model
;
1038 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1041 m_wxgtk_model
= NULL
;
1044 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1046 m_owner
->GtkGetInternal()->ItemAdded( parent
, item
);
1049 iter
.stamp
= m_wxgtk_model
->stamp
;
1050 iter
.user_data
= (gpointer
) item
.GetID();
1052 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1053 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1054 gtk_tree_model_row_inserted(
1055 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1056 gtk_tree_path_free (path
);
1061 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&item
)
1064 iter
.stamp
= m_wxgtk_model
->stamp
;
1065 iter
.user_data
= (gpointer
) item
.GetID();
1067 GtkTreePath
*path
= wxgtk_tree_model_get_path_safe(
1068 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1069 gtk_tree_model_row_deleted(
1070 GTK_TREE_MODEL(m_wxgtk_model
), path
);
1071 gtk_tree_path_free (path
);
1073 m_owner
->GtkGetInternal()->ItemDeleted( item
);
1078 void wxGtkDataViewModelNotifier::Resort()
1080 m_owner
->GtkGetInternal()->Resort();
1083 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1086 iter
.stamp
= m_wxgtk_model
->stamp
;
1087 iter
.user_data
= (gpointer
) item
.GetID();
1089 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1090 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1091 gtk_tree_model_row_changed(
1092 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1093 gtk_tree_path_free (path
);
1098 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_col
)
1100 // This adds GTK+'s missing MVC logic for ValueChanged
1102 for (index
= 0; index
< m_owner
->GetColumnCount(); index
++)
1104 wxDataViewColumn
*column
= m_owner
->GetColumn( index
);
1105 if (column
->GetModelColumn() == model_col
)
1107 GtkTreeView
*widget
= GTK_TREE_VIEW(m_owner
->m_treeview
);
1108 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1112 iter
.stamp
= m_wxgtk_model
->stamp
;
1113 iter
.user_data
= (gpointer
) item
.GetID();
1114 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1115 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1116 GdkRectangle cell_area
;
1117 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1118 gtk_tree_path_free( path
);
1120 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1121 double d
= gtk_adjustment_get_value( hadjust
);
1122 int xdiff
= (int) d
;
1124 int ydiff
= gcolumn
->button
->allocation
.height
;
1126 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1127 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1134 bool wxGtkDataViewModelNotifier::Cleared()
1139 // ---------------------------------------------------------
1140 // wxDataViewRenderer
1141 // ---------------------------------------------------------
1143 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1145 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1147 wxDataViewRendererBase( varianttype
, mode
, align
)
1151 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1152 // after the m_renderer pointer has been initialized
1155 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1157 GtkCellRendererMode gtkMode
;
1160 case wxDATAVIEW_CELL_INERT
:
1161 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1163 case wxDATAVIEW_CELL_ACTIVATABLE
:
1164 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1166 case wxDATAVIEW_CELL_EDITABLE
:
1167 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1171 GValue gvalue
= { 0, };
1172 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1173 g_value_set_enum( &gvalue
, gtkMode
);
1174 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1175 g_value_unset( &gvalue
);
1178 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1180 wxDataViewCellMode ret
;
1183 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1185 switch (g_value_get_enum(&gvalue
))
1187 case GTK_CELL_RENDERER_MODE_INERT
:
1188 ret
= wxDATAVIEW_CELL_INERT
;
1190 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1191 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1193 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1194 ret
= wxDATAVIEW_CELL_EDITABLE
;
1198 g_value_unset( &gvalue
);
1203 void wxDataViewRenderer::SetAlignment( int align
)
1205 // horizontal alignment:
1207 gfloat xalign
= 0.0;
1208 if (align
& wxALIGN_RIGHT
)
1210 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1213 GValue gvalue
= { 0, };
1214 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1215 g_value_set_float( &gvalue
, xalign
);
1216 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1217 g_value_unset( &gvalue
);
1219 // vertical alignment:
1221 gfloat yalign
= 0.0;
1222 if (align
& wxALIGN_BOTTOM
)
1224 else if (align
& wxALIGN_CENTER_VERTICAL
)
1227 GValue gvalue2
= { 0, };
1228 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1229 g_value_set_float( &gvalue2
, yalign
);
1230 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1231 g_value_unset( &gvalue2
);
1234 int wxDataViewRenderer::GetAlignment() const
1239 // horizontal alignment:
1241 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
1242 float xalign
= g_value_get_float( &gvalue
);
1244 ret
|= wxALIGN_LEFT
;
1245 else if (xalign
== 0.5)
1246 ret
|= wxALIGN_CENTER_HORIZONTAL
;
1248 ret
|= wxALIGN_RIGHT
;
1249 g_value_unset( &gvalue
);
1252 // vertical alignment:
1254 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
1255 float yalign
= g_value_get_float( &gvalue
);
1258 else if (yalign
== 0.5)
1259 ret
|= wxALIGN_CENTER_VERTICAL
;
1261 ret
|= wxALIGN_BOTTOM
;
1262 g_value_unset( &gvalue
);
1269 // ---------------------------------------------------------
1270 // wxDataViewTextRenderer
1271 // ---------------------------------------------------------
1274 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1275 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1278 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1279 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1281 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1283 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1284 wxVariant value
= tmp
;
1285 if (!cell
->Validate( value
))
1288 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1290 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1292 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, path
);
1293 wxDataViewItem
item( (void*) iter
.user_data
);;
1294 gtk_tree_path_free( path
);
1296 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1298 model
->SetValue( value
, item
, model_col
);
1299 model
->ValueChanged( item
, model_col
);
1302 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1304 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1306 wxDataViewRenderer( varianttype
, mode
, align
)
1308 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_text_new();
1310 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1312 GValue gvalue
= { 0, };
1313 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1314 g_value_set_boolean( &gvalue
, true );
1315 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1316 g_value_unset( &gvalue
);
1318 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1322 SetAlignment(align
);
1325 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1327 wxString tmp
= value
;
1329 GValue gvalue
= { 0, };
1330 g_value_init( &gvalue
, G_TYPE_STRING
);
1331 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( tmp
, GetOwner()->GetOwner()->GetFont() ) );
1332 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1333 g_value_unset( &gvalue
);
1338 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1340 GValue gvalue
= { 0, };
1341 g_value_init( &gvalue
, G_TYPE_STRING
);
1342 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1343 wxString tmp
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
),
1344 wx_const_cast(wxDataViewTextRenderer
*, this)->GetOwner()->GetOwner()->GetFont() );
1345 g_value_unset( &gvalue
);
1352 void wxDataViewTextRenderer::SetAlignment( int align
)
1354 wxDataViewRenderer::SetAlignment(align
);
1356 if (gtk_check_version(2,10,0))
1359 // horizontal alignment:
1360 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1361 if (align
& wxALIGN_RIGHT
)
1362 pangoAlign
= PANGO_ALIGN_RIGHT
;
1363 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1364 pangoAlign
= PANGO_ALIGN_CENTER
;
1366 GValue gvalue
= { 0, };
1367 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1368 g_value_set_enum( &gvalue
, pangoAlign
);
1369 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1370 g_value_unset( &gvalue
);
1373 // ---------------------------------------------------------
1374 // wxDataViewBitmapRenderer
1375 // ---------------------------------------------------------
1377 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1379 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1381 wxDataViewRenderer( varianttype
, mode
, align
)
1383 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_pixbuf_new();
1386 SetAlignment(align
);
1389 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1391 if (value
.GetType() == wxT("wxBitmap"))
1396 // This may create a Pixbuf representation in the
1397 // wxBitmap object (and it will stay there)
1398 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1400 GValue gvalue
= { 0, };
1401 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1402 g_value_set_object( &gvalue
, pixbuf
);
1403 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1404 g_value_unset( &gvalue
);
1409 if (value
.GetType() == wxT("wxIcon"))
1414 // This may create a Pixbuf representation in the
1415 // wxBitmap object (and it will stay there)
1416 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1418 GValue gvalue
= { 0, };
1419 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1420 g_value_set_object( &gvalue
, pixbuf
);
1421 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1422 g_value_unset( &gvalue
);
1430 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1435 // ---------------------------------------------------------
1436 // wxDataViewToggleRenderer
1437 // ---------------------------------------------------------
1440 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1441 gchar
*path
, gpointer user_data
);
1444 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1445 gchar
*path
, gpointer user_data
)
1447 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1450 GValue gvalue
= { 0, };
1451 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1452 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1453 bool tmp
= g_value_get_boolean( &gvalue
);
1454 g_value_unset( &gvalue
);
1458 wxVariant value
= tmp
;
1459 if (!cell
->Validate( value
))
1462 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1464 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1466 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, gtk_path
);
1467 wxDataViewItem
item( (void*) iter
.user_data
);;
1468 gtk_tree_path_free( gtk_path
);
1470 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1472 model
->SetValue( value
, item
, model_col
);
1473 model
->ValueChanged( item
, model_col
);
1476 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1478 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1479 wxDataViewCellMode mode
, int align
) :
1480 wxDataViewRenderer( varianttype
, mode
, align
)
1482 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
1484 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1486 g_signal_connect_after( m_renderer
, "toggled",
1487 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1491 GValue gvalue
= { 0, };
1492 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1493 g_value_set_boolean( &gvalue
, false );
1494 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1495 g_value_unset( &gvalue
);
1499 SetAlignment(align
);
1502 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1506 GValue gvalue
= { 0, };
1507 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1508 g_value_set_boolean( &gvalue
, tmp
);
1509 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1510 g_value_unset( &gvalue
);
1515 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1517 GValue gvalue
= { 0, };
1518 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1519 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1520 bool tmp
= g_value_get_boolean( &gvalue
);
1521 g_value_unset( &gvalue
);
1528 // ---------------------------------------------------------
1529 // wxDataViewCustomRenderer
1530 // ---------------------------------------------------------
1532 class wxDataViewCtrlDC
: public wxWindowDC
1535 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1537 GtkWidget
*widget
= window
->m_treeview
;
1541 m_context
= window
->GtkGetPangoDefaultContext();
1542 m_layout
= pango_layout_new( m_context
);
1543 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1545 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1547 // Set m_window later
1549 // m_owner = window;
1553 // ---------------------------------------------------------
1554 // wxDataViewCustomRenderer
1555 // ---------------------------------------------------------
1557 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1559 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1560 wxDataViewCellMode mode
, int align
,
1562 wxDataViewRenderer( varianttype
, mode
, align
)
1572 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1574 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1575 renderer
->cell
= this;
1577 m_renderer
= (GtkCellRenderer
*) renderer
;
1580 SetAlignment(align
);
1585 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1591 wxDC
*wxDataViewCustomRenderer::GetDC()
1595 if (GetOwner() == NULL
)
1597 if (GetOwner()->GetOwner() == NULL
)
1599 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1605 // ---------------------------------------------------------
1606 // wxDataViewProgressRenderer
1607 // ---------------------------------------------------------
1609 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1611 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1612 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1613 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1619 if (!gtk_check_version(2,6,0))
1621 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
1623 GValue gvalue
= { 0, };
1624 g_value_init( &gvalue
, G_TYPE_STRING
);
1626 // FIXME: font encoding support
1627 g_value_set_string( &gvalue
, wxGTK_CONV_SYS(m_label
) );
1628 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1629 g_value_unset( &gvalue
);
1632 SetAlignment(align
);
1637 // Use custom cell code
1638 wxDataViewCustomRenderer::Init(mode
, align
);
1642 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1646 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1649 if (!gtk_check_version(2,6,0))
1651 gint tmp
= (long) value
;
1652 GValue gvalue
= { 0, };
1653 g_value_init( &gvalue
, G_TYPE_INT
);
1654 g_value_set_int( &gvalue
, tmp
);
1655 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1656 g_value_unset( &gvalue
);
1661 m_value
= (long) value
;
1663 if (m_value
< 0) m_value
= 0;
1664 if (m_value
> 100) m_value
= 100;
1670 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1675 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1677 double pct
= (double)m_value
/ 100.0;
1679 bar
.width
= (int)(cell
.width
* pct
);
1680 dc
->SetPen( *wxTRANSPARENT_PEN
);
1681 dc
->SetBrush( *wxBLUE_BRUSH
);
1682 dc
->DrawRectangle( bar
);
1684 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1685 dc
->SetPen( *wxBLACK_PEN
);
1686 dc
->DrawRectangle( cell
);
1691 wxSize
wxDataViewProgressRenderer::GetSize() const
1693 return wxSize(40,12);
1696 // ---------------------------------------------------------
1697 // wxDataViewDateRenderer
1698 // ---------------------------------------------------------
1700 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1703 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1704 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
1705 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1710 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1711 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1712 sizer
->Add( m_cal
, 1, wxGROW
);
1717 virtual void OnDismiss()
1721 void OnCalendar( wxCalendarEvent
&event
);
1723 wxCalendarCtrl
*m_cal
;
1724 wxDataViewModel
*m_model
;
1725 wxDataViewItem m_item
;
1729 DECLARE_EVENT_TABLE()
1732 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1733 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1736 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1738 wxDateTime date
= event
.GetDate();
1739 wxVariant value
= date
;
1740 m_model
->SetValue( value
, m_item
, m_col
);
1741 m_model
->ValueChanged( m_item
, m_col
);
1745 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1747 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1748 wxDataViewCellMode mode
, int align
) :
1749 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1752 SetAlignment(align
);
1755 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1757 m_date
= value
.GetDateTime();
1762 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1767 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1769 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1770 wxString tmp
= m_date
.FormatDate();
1771 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1776 wxSize
wxDataViewDateRenderer::GetSize() const
1778 wxString tmp
= m_date
.FormatDate();
1780 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1781 return wxSize(x
,y
+d
);
1784 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewModel
*model
,
1785 const wxDataViewItem
&item
, unsigned int col
)
1788 model
->GetValue( variant
, item
, col
);
1789 wxDateTime value
= variant
.GetDateTime();
1791 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1792 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1793 wxPoint pos
= wxGetMousePosition();
1796 popup
->Popup( popup
->m_cal
);
1801 // ---------------------------------------------------------
1803 // ---------------------------------------------------------
1807 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
1808 GdkEventButton
*gdk_event
,
1809 wxDataViewColumn
*column
)
1811 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
1814 if (gdk_event
->button
== 1)
1816 wxDataViewCtrl
*dv
= column
->GetOwner();
1817 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
1818 event
.SetDataViewColumn( column
);
1819 event
.SetModel( dv
->GetModel() );
1820 if (dv
->GetEventHandler()->ProcessEvent( event
))
1828 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1829 GtkCellRenderer
*cell
,
1830 GtkTreeModel
*model
,
1836 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1837 GtkCellRenderer
*renderer
,
1838 GtkTreeModel
*model
,
1842 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
1843 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
1845 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
1847 wxDataViewItem
item( (void*) iter
->user_data
);
1850 tree_model
->internal
->GetDataViewModel()->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
1852 if (value
.GetType() != cell
->GetVariantType())
1853 wxLogError( wxT("Wrong type, required: %s but: %s"),
1854 value
.GetType().c_str(),
1855 cell
->GetVariantType().c_str() );
1857 cell
->SetValue( value
);
1860 wxListItemAttr attr
;
1861 tree_model
->model
->GetAttr( attr
, cell
->GetOwner()->GetModelColumn(), model_row
);
1863 if (attr
.HasBackgroundColour())
1865 wxColour colour
= attr
.GetBackgroundColour();
1866 const GdkColor
* const gcol
= colour
.GetColor();
1868 GValue gvalue
= { 0, };
1869 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
1870 g_value_set_boxed( &gvalue
, gcol
);
1871 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
1872 g_value_unset( &gvalue
);
1876 GValue gvalue
= { 0, };
1877 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1878 g_value_set_boolean( &gvalue
, FALSE
);
1879 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
1880 g_value_unset( &gvalue
);
1886 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1888 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1889 unsigned int model_column
, int width
,
1890 wxAlignment align
, int flags
) :
1891 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1893 Init( align
, flags
, width
);
1895 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
1899 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1900 unsigned int model_column
, int width
,
1901 wxAlignment align
, int flags
) :
1902 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1904 Init( align
, flags
, width
);
1906 SetBitmap( bitmap
);
1909 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
1911 m_isConnected
= false;
1913 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
1914 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1915 m_column
= (GtkWidget
*) column
;
1918 SetAlignment( align
);
1920 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
1921 // as GTK+ is smart and unless explicitely told, will set the minimal
1922 // width to the title's lenght, which is a better default
1924 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
1925 // that we use for the wxDataViewCtrl
1926 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1927 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
1929 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
1931 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1932 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
1935 wxDataViewColumn::~wxDataViewColumn()
1939 void wxDataViewColumn::OnInternalIdle()
1944 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
1946 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1949 g_signal_connect(column
->button
, "button_press_event",
1950 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
1952 m_isConnected
= true;
1957 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
1959 wxDataViewColumnBase::SetOwner( owner
);
1961 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1963 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
1966 void wxDataViewColumn::SetTitle( const wxString
&title
)
1968 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1972 // disconnect before column->button gets recreated
1973 g_signal_handlers_disconnect_by_func( column
->button
,
1974 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
1976 m_isConnected
= false;
1979 // FIXME: can it really happen that we don't have the owner here??
1980 wxDataViewCtrl
*ctrl
= GetOwner();
1981 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
1982 : wxGTK_CONV_SYS(title
) );
1984 gtk_tree_view_column_set_widget( column
, NULL
);
1987 wxString
wxDataViewColumn::GetTitle() const
1989 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
1990 return wxConvFileName
->cMB2WX(str
);
1993 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
1995 wxDataViewColumnBase::SetBitmap( bitmap
);
1997 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2000 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
2002 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
2003 if (bitmap
.GetMask())
2004 mask
= bitmap
.GetMask()->GetBitmap();
2006 if (bitmap
.HasPixbuf())
2008 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
2009 bitmap
.GetPixbuf());
2013 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
2014 bitmap
.GetPixmap(), mask
);
2016 gtk_widget_show( GTK_WIDGET(gtk_image
) );
2018 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
2022 gtk_tree_view_column_set_widget( column
, NULL
);
2026 void wxDataViewColumn::SetHidden( bool hidden
)
2028 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
2031 void wxDataViewColumn::SetResizeable( bool resizeable
)
2033 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
2036 void wxDataViewColumn::SetAlignment( wxAlignment align
)
2038 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2040 gfloat xalign
= 0.0;
2041 if (align
== wxALIGN_RIGHT
)
2043 if (align
== wxALIGN_CENTER_HORIZONTAL
||
2044 align
== wxALIGN_CENTER
)
2047 gtk_tree_view_column_set_alignment( column
, xalign
);
2050 wxAlignment
wxDataViewColumn::GetAlignment() const
2052 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
2055 return wxALIGN_RIGHT
;
2057 return wxALIGN_CENTER_HORIZONTAL
;
2059 return wxALIGN_LEFT
;
2062 void wxDataViewColumn::SetSortable( bool sortable
)
2064 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2067 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
2069 gtk_tree_view_column_set_sort_column_id( column
, -1 );
2072 bool wxDataViewColumn::IsSortable() const
2074 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2075 return (gtk_tree_view_column_get_sort_column_id( column
) != -1);
2078 bool wxDataViewColumn::IsResizeable() const
2080 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2081 return gtk_tree_view_column_get_resizable( column
);
2084 bool wxDataViewColumn::IsHidden() const
2086 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2087 return !gtk_tree_view_column_get_visible( column
);
2090 void wxDataViewColumn::SetSortOrder( bool ascending
)
2092 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2095 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
2097 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
2100 bool wxDataViewColumn::IsSortOrderAscending() const
2102 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2104 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
2107 void wxDataViewColumn::SetMinWidth( int width
)
2109 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2112 int wxDataViewColumn::GetMinWidth() const
2114 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2117 int wxDataViewColumn::GetWidth() const
2119 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2122 void wxDataViewColumn::SetWidth( int width
)
2124 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2128 //-----------------------------------------------------------------------------
2129 // wxGtkTreeModelNode
2130 //-----------------------------------------------------------------------------
2132 void wxGtkTreeModelNode::Resort()
2134 size_t child_count
= GetChildCount();
2135 if (child_count
== 0)
2138 size_t node_count
= GetNodesCount();
2140 if (child_count
== 1)
2142 if (node_count
== 1)
2144 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
2150 wxGtkTreeModelChildren
*new_array
= new wxGtkTreeModelChildren( wxGtkTreeModelNodeCmp
);
2153 for (pos
= 0; pos
< child_count
; pos
++)
2154 new_array
->Add( m_children
->Item( pos
) );
2156 gint
*new_order
= new gint
[child_count
];
2158 for (pos
= 0; pos
< child_count
; pos
++)
2160 void *id
= new_array
->Item( pos
);
2162 for (old_pos
= 0; old_pos
< child_count
; old_pos
++)
2164 if (id
== m_children
->Item(old_pos
))
2166 new_order
[pos
] = old_pos
;
2172 // for (pos = 0; pos < count; pos++)
2173 // m_children->Clear();
2176 m_children
= new_array
;
2178 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
2180 GtkTreePath
*path
= gtk_tree_path_new ();
2181 wxGtkTreeModelNode
*parent
= GetParent();
2182 void *id
= GetItem().GetID();
2186 int pos
= parent
->GetChildren().Index( id
);
2187 gtk_tree_path_prepend_index( path
, pos
);
2188 id
= parent
->GetItem().GetID();
2189 parent
= parent
->GetParent();
2193 iter
.user_data
= id
;
2194 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
2195 gtk_tree_model_rows_reordered( gtk_tree_model
, path
, &iter
, new_order
);
2197 gtk_tree_path_free (path
);
2199 delete [] new_order
;
2201 for (pos
= 0; pos
< node_count
; pos
++)
2203 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
2208 //-----------------------------------------------------------------------------
2209 // wxDataViewCtrlInternal
2210 //-----------------------------------------------------------------------------
2212 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
,
2213 wxDataViewModel
*wx_model
, GtkWxTreeModel
*gtk_model
)
2216 m_wx_model
= wx_model
;
2217 m_gtk_model
= gtk_model
;
2222 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2224 g_object_unref( m_gtk_model
);
2227 void wxDataViewCtrlInternal::InitTree()
2229 wxDataViewItem item
;
2230 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
2232 BuildBranch( m_root
);
2235 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
2237 if (node
->GetChildCount() == 0)
2239 wxDataViewItem child
= m_wx_model
->GetFirstChild( node
->GetItem() );
2240 while (child
.IsOk())
2242 if (m_wx_model
->IsContainer( child
))
2243 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
2245 node
->AddLeave( child
.GetID() );
2246 child
= m_wx_model
->GetNextSibling( child
);
2251 void wxDataViewCtrlInternal::Resort()
2256 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2258 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2259 if (m_wx_model
->IsContainer( item
))
2260 parent_node
->AddNode( new wxGtkTreeModelNode( parent_node
, item
, this ) );
2262 parent_node
->AddLeave( item
.GetID() );
2266 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&item
)
2268 wxGtkTreeModelNode
*parent
= FindParentNode( item
);
2269 parent
->DeleteChild( item
.GetID() );
2273 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
2275 int depth
= gtk_tree_path_get_depth( path
);
2277 wxGtkTreeModelNode
*node
= m_root
;
2280 for (i
= 0; i
< depth
; i
++)
2282 BuildBranch( node
);
2284 gint pos
= gtk_tree_path_get_indices (path
)[i
];
2285 if (pos
< 0) return FALSE
;
2286 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
2288 void* id
= node
->GetChildren().Item( (size_t) pos
);
2292 iter
->stamp
= m_gtk_model
->stamp
;
2293 iter
->user_data
= id
;
2297 size_t count
= node
->GetNodes().GetCount();
2299 for (pos2
= 0; pos2
< count
; pos2
++)
2301 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
2302 if (child_node
->GetItem().GetID() == id
)
2313 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
2315 GtkTreePath
*retval
= gtk_tree_path_new ();
2316 void *id
= iter
->user_data
;
2318 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
2321 int pos
= node
->GetChildren().Index( id
);
2322 gtk_tree_path_prepend_index( retval
, pos
);
2324 id
= node
->GetItem().GetID();
2325 node
= node
->GetParent();
2331 GtkTreePath
*wxDataViewCtrlInternal::get_path_safe( GtkTreeIter
*iter
)
2333 GtkTreePath
*retval
= gtk_tree_path_new ();
2334 void *id
= iter
->user_data
;
2336 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
2340 for (pos
= 0; pos
< node
->GetChildren().GetCount(); pos
++)
2342 if (id
== node
->GetChildren().Item( pos
))
2344 gtk_tree_path_prepend_index( retval
, (int) pos
);
2349 id
= node
->GetItem().GetID();
2350 node
= node
->GetParent();
2356 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
2358 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
2359 unsigned int pos
= parent
->GetChildren().Index( iter
->user_data
);
2361 if (pos
== parent
->GetChildCount()-1)
2364 iter
->stamp
= m_gtk_model
->stamp
;
2365 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
2370 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
2372 wxDataViewItem
item( (void*) parent
->user_data
);
2374 if (!m_wx_model
->IsContainer( item
))
2377 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2378 BuildBranch( parent_node
);
2380 if (parent_node
->GetChildCount() == 0)
2383 iter
->stamp
= m_gtk_model
->stamp
;
2384 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
2389 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
2391 wxDataViewItem
item( (void*) iter
->user_data
);
2392 bool is_container
= m_wx_model
->IsContainer( item
);
2397 wxGtkTreeModelNode
*node
= FindNode( iter
);
2398 BuildBranch( node
);
2400 return (node
->GetChildCount() > 0);
2403 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
2405 wxDataViewItem
item( (void*) iter
->user_data
);
2407 if (!m_wx_model
->IsContainer( item
))
2410 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
2411 BuildBranch( parent_node
);
2413 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2415 return parent_node
->GetChildCount();
2418 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
2421 if (parent
) id
= (void*) parent
->user_data
;
2422 wxDataViewItem
item( id
);
2424 if (!m_wx_model
->IsContainer( item
))
2427 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2428 BuildBranch( parent_node
);
2430 // wxPrintf( "iter_nth_child %d\n", n );
2432 iter
->stamp
= m_gtk_model
->stamp
;
2433 iter
->user_data
= parent_node
->GetChildren().Item( n
);
2438 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
2440 wxGtkTreeModelNode
*node
= FindParentNode( child
);
2444 iter
->stamp
= m_gtk_model
->stamp
;
2445 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2450 static wxGtkTreeModelNode
*
2451 wxDataViewCtrlInternal_FindNode( wxGtkTreeModelNode
*node
, const wxDataViewItem
&item
)
2453 if (!node
) return NULL
;
2455 size_t count
= node
->GetNodesCount();
2457 for (i
= 0; i
< count
; i
++)
2459 wxGtkTreeModelNode
*child
= node
->GetNodes().Item( i
);
2460 if (child
->GetItem().GetID() == item
.GetID())
2462 // wxPrintf( "leave findnode at %d\n", i );
2466 wxGtkTreeModelNode
*node2
= wxDataViewCtrlInternal_FindNode( child
, item
);
2469 // wxPrintf( "branch findnode at %d\n", i );
2477 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
2482 wxDataViewItem
item( (void*) iter
->user_data
);
2486 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_root
, item
);
2490 wxPrintf( "Not found %d\n", (int) iter
->user_data
);
2498 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
2503 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_root
, item
);
2507 wxPrintf( "Not found %d\n", (int) item
.GetID() );
2515 static wxGtkTreeModelNode
*
2516 wxDataViewCtrlInternal_FindParentNode( wxGtkTreeModelNode
*node
, const wxDataViewItem
&item
)
2518 size_t child_count
= node
->GetChildCount();
2519 void *id
= item
.GetID();
2520 const wxGtkTreeModelChildren
&children
= node
->GetChildren();
2522 for (pos
= 0; pos
< child_count
; pos
++)
2524 if (children
.Item( pos
) == id
)
2528 size_t node_count
= node
->GetNodesCount();
2529 for (pos
= 0; pos
< node_count
; pos
++)
2531 wxGtkTreeModelNode
*child
= node
->GetNodes().Item( pos
);
2533 wxGtkTreeModelNode
*node2
= wxDataViewCtrlInternal_FindParentNode( child
, item
);
2536 // wxPrintf( "branch findnode at %d\n", i );
2544 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
2549 wxDataViewItem
item( (void*) iter
->user_data
);
2553 return wxDataViewCtrlInternal_FindParentNode( m_root
, item
);
2556 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
2561 return wxDataViewCtrlInternal_FindParentNode( m_root
, item
);
2564 //-----------------------------------------------------------------------------
2565 // wxDataViewCtrl signal callbacks
2566 //-----------------------------------------------------------------------------
2569 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
2571 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
2574 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
, dv
->GetId() );
2576 event
.SetModel( dv
->GetModel() );
2577 dv
->GetEventHandler()->ProcessEvent( event
);
2581 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
2582 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
2584 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
2587 dv
->GtkGetInternal()->get_iter( &iter
, path
);
2588 wxDataViewItem
item( (void*) iter
.user_data
);;
2589 event
.SetItem( item
);
2590 event
.SetModel( dv
->GetModel() );
2591 dv
->GetEventHandler()->ProcessEvent( event
);
2594 //-----------------------------------------------------------------------------
2596 //-----------------------------------------------------------------------------
2598 //-----------------------------------------------------------------------------
2599 // InsertChild for wxDataViewCtrl
2600 //-----------------------------------------------------------------------------
2602 static void wxInsertChildInDataViewCtrl( wxWindowGTK
* parent
, wxWindowGTK
* child
)
2604 wxDataViewCtrl
* dvc
= (wxDataViewCtrl
*) parent
;
2605 GtkWidget
*treeview
= dvc
->GtkGetTreeView();
2607 // Insert widget in GtkTreeView
2608 if (GTK_WIDGET_REALIZED(treeview
))
2609 gtk_widget_set_parent_window( child
->m_widget
,
2610 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
2611 gtk_widget_set_parent( child
->m_widget
, treeview
);
2615 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
2616 GtkAllocation
*alloc
,
2617 wxDataViewCtrl
*win
)
2620 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
2623 wxWindow
*child
= node
->GetData();
2626 gtk_widget_size_request( child
->m_widget
, &req
);
2628 GtkAllocation alloc
;
2629 alloc
.x
= child
->m_x
;
2630 alloc
.y
= child
->m_y
;
2631 alloc
.width
= child
->m_width
;
2632 alloc
.height
= child
->m_height
;
2633 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
2635 node
= node
->GetNext();
2641 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
2643 wxDataViewCtrl::~wxDataViewCtrl()
2646 GetModel()->RemoveNotifier( m_notifier
);
2648 // remove the model from the GtkTreeView before it gets destroyed by the
2649 // wxDataViewCtrlBase's dtor
2650 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
2655 void wxDataViewCtrl::Init()
2660 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
2661 const wxPoint
& pos
, const wxSize
& size
,
2662 long style
, const wxValidator
& validator
)
2666 if (!PreCreation( parent
, pos
, size
) ||
2667 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
2669 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
2673 m_insertCallback
= wxInsertChildInDataViewCtrl
;
2675 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
2677 GtkScrolledWindowSetBorder(m_widget
, style
);
2679 m_treeview
= gtk_tree_view_new();
2680 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
2682 g_signal_connect (m_treeview
, "size_allocate",
2683 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
2686 if (!gtk_check_version(2,6,0))
2687 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
2690 if (style
& wxDV_MULTIPLE
)
2692 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2693 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
2696 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
2699 if (!gtk_check_version(2,10,0))
2701 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
2703 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
2704 (style
& wxDV_VERT_RULES
) != 0)
2705 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
2706 else if (style
& wxDV_VERT_RULES
)
2707 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
2708 else if (style
& wxDV_HORIZ_RULES
)
2709 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
2711 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
2716 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
2719 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
2720 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2721 gtk_widget_show (m_treeview
);
2723 m_parent
->DoAddChild( this );
2727 GtkEnableSelectionEvents();
2729 g_signal_connect_after (m_treeview
, "row_activated",
2730 G_CALLBACK (wxdataview_row_activated_callback
), this);
2735 void wxDataViewCtrl::OnInternalIdle()
2737 wxWindow::OnInternalIdle();
2739 unsigned int cols
= GetColumnCount();
2741 for (i
= 0; i
< cols
; i
++)
2743 wxDataViewColumn
*col
= GetColumn( i
);
2744 col
->OnInternalIdle();
2748 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
2750 if (!wxDataViewCtrlBase::AssociateModel( model
))
2753 GtkWxTreeModel
*gtk_model
= wxgtk_tree_model_new();
2754 m_internal
= new wxDataViewCtrlInternal( this, model
, gtk_model
);
2755 gtk_model
->internal
= m_internal
;
2757 m_notifier
= new wxGtkDataViewModelNotifier( gtk_model
, model
, this );
2759 model
->AddNotifier( m_notifier
);
2761 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_model
) );
2763 // unref in wxDataViewCtrlInternal
2764 // g_object_unref( gtk_model );
2769 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
2771 if (!wxDataViewCtrlBase::AppendColumn(col
))
2774 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
2776 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
2781 wxDataViewItem
wxDataViewCtrl::GetSelection()
2783 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2785 if (m_windowStyle
& wxDV_MULTIPLE
)
2791 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
2793 wxDataViewItem
item( (void*) iter
.user_data
);
2798 return wxDataViewItem(0);
2801 void wxDataViewCtrl::DoSetExpanderColumn()
2805 void wxDataViewCtrl::DoSetIndent()
2809 void wxDataViewCtrl::GtkDisableSelectionEvents()
2811 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2812 g_signal_connect_after (selection
, "changed",
2813 G_CALLBACK (wxdataview_selection_changed_callback
), this);
2816 void wxDataViewCtrl::GtkEnableSelectionEvents()
2818 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2819 g_signal_handlers_disconnect_by_func( selection
,
2820 (gpointer
) (wxdataview_selection_changed_callback
), this);
2825 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2827 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
2832 // !wxUSE_GENERICDATAVIEWCTRL
2835 // wxUSE_DATAVIEWCTRL