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
);
78 bool ItemChanged( const wxDataViewItem
&item
);
79 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
85 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
86 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
87 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
88 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
89 void BuildBranch( wxGtkTreeModelNode
*branch
);
92 wxGtkTreeModelNode
*m_root
;
93 wxDataViewModel
*m_wx_model
;
94 GtkWxTreeModel
*m_gtk_model
;
95 wxDataViewCtrl
*m_owner
;
99 //-----------------------------------------------------------------------------
100 // wxGtkTreeModelNode
101 //-----------------------------------------------------------------------------
103 int LINKAGEMODE
wxGtkTreeModelNodeCmp( void *id1
, void *id2
);
105 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
106 WX_DEFINE_SORTED_ARRAY( void* , wxGtkTreeModelChildren
);
108 class wxGtkTreeModelNode
111 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
112 wxDataViewCtrlInternal
*internal
)
116 m_internal
= internal
;
117 g_model
= internal
->GetDataViewModel();
118 m_children
= new wxGtkTreeModelChildren( wxGtkTreeModelNodeCmp
);
121 ~wxGtkTreeModelNode()
123 g_model
= m_internal
->GetDataViewModel();
124 size_t count
= m_children
->GetCount();
126 for (i
= 0; i
< count
; i
++)
128 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
134 unsigned int AddNode( wxGtkTreeModelNode
* child
)
136 g_model
= m_internal
->GetDataViewModel();
137 m_nodes
.Add( child
);
138 return m_children
->Add( child
->GetItem().GetID() );
141 unsigned int AddLeave( void* id
)
143 g_model
= m_internal
->GetDataViewModel();
144 return m_children
->Add( id
);
147 void DeleteChild( void* id
)
149 g_model
= m_internal
->GetDataViewModel();
151 size_t count
= m_children
->GetCount();
152 for (pos
= 0; pos
< count
; pos
++)
154 if (m_children
->Item( pos
) == id
)
156 m_children
->RemoveAt( pos
);
160 count
= m_nodes
.GetCount();
161 for (pos
= 0; pos
< count
; pos
++)
163 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
164 if (node
->GetItem().GetID() == id
)
166 m_nodes
.RemoveAt( pos
);
174 wxGtkTreeModelNode
* GetParent()
176 wxGtkTreeModelNodes
&GetNodes()
178 wxGtkTreeModelChildren
&GetChildren()
179 { return *m_children
; }
181 unsigned int GetChildCount() { return m_children
->GetCount(); }
182 unsigned int GetNodesCount() { return m_nodes
.GetCount(); }
184 wxDataViewItem
&GetItem() { return m_item
; }
185 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
190 wxGtkTreeModelNode
*m_parent
;
191 wxGtkTreeModelNodes m_nodes
;
192 wxGtkTreeModelChildren
*m_children
;
193 wxDataViewItem m_item
;
194 wxDataViewCtrlInternal
*m_internal
;
198 int LINKAGEMODE
wxGtkTreeModelNodeCmp( void* id1
, void* id2
)
200 return g_model
->Compare( id1
, id2
);
203 //-----------------------------------------------------------------------------
205 //-----------------------------------------------------------------------------
207 extern bool g_blockEventsOnDrag
;
209 //-----------------------------------------------------------------------------
210 // define new GTK+ class wxGtkTreeModel
211 //-----------------------------------------------------------------------------
215 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
216 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
217 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
218 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
219 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
220 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
222 GType
gtk_wx_tree_model_get_type (void);
224 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
226 struct _GtkWxTreeModel
232 wxDataViewCtrlInternal
*internal
;
235 struct _GtkWxTreeModelClass
237 GObjectClass list_parent_class
;
240 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
241 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
242 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
243 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
244 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
245 static void wxgtk_tree_model_finalize (GObject
*object
);
246 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
247 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
248 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
250 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
253 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
255 static GtkTreePath
*wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
257 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
261 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
263 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
265 GtkTreeIter
*parent
);
266 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
268 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
270 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
274 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
279 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
280 gint
*sort_column_id
,
282 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
285 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
287 GtkTreeIterCompareFunc func
,
289 GtkDestroyNotify destroy
);
290 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
291 GtkTreeIterCompareFunc func
,
293 GtkDestroyNotify destroy
);
294 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
298 static GObjectClass
*list_parent_class
= NULL
;
301 gtk_wx_tree_model_get_type (void)
303 static GType tree_model_type
= 0;
305 if (!tree_model_type
)
307 const GTypeInfo tree_model_info
=
309 sizeof (GtkWxTreeModelClass
),
310 NULL
, /* base_init */
311 NULL
, /* base_finalize */
312 (GClassInitFunc
) wxgtk_tree_model_class_init
,
313 NULL
, /* class_finalize */
314 NULL
, /* class_data */
315 sizeof (GtkWxTreeModel
),
317 (GInstanceInitFunc
) wxgtk_tree_model_init
,
320 static const GInterfaceInfo tree_model_iface_info
=
322 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
327 static const GInterfaceInfo sortable_iface_info
=
329 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
334 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
335 &tree_model_info
, (GTypeFlags
)0 );
337 g_type_add_interface_static (tree_model_type
,
339 &tree_model_iface_info
);
340 g_type_add_interface_static (tree_model_type
,
341 GTK_TYPE_TREE_SORTABLE
,
342 &sortable_iface_info
);
345 return tree_model_type
;
348 static GtkWxTreeModel
*
349 wxgtk_tree_model_new(void)
351 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
356 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
358 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
359 GObjectClass
*object_class
= (GObjectClass
*) klass
;
360 object_class
->finalize
= wxgtk_tree_model_finalize
;
364 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
366 iface
->get_flags
= wxgtk_tree_model_get_flags
;
367 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
368 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
369 iface
->get_iter
= wxgtk_tree_model_get_iter
;
370 iface
->get_path
= wxgtk_tree_model_get_path
;
371 iface
->get_value
= wxgtk_tree_model_get_value
;
372 iface
->iter_next
= wxgtk_tree_model_iter_next
;
373 iface
->iter_children
= wxgtk_tree_model_iter_children
;
374 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
375 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
376 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
377 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
381 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
383 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
384 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
385 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
386 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
387 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
391 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
393 tree_model
->internal
= NULL
;
394 tree_model
->stamp
= g_random_int();
398 wxgtk_tree_model_finalize (GObject
*object
)
401 (* list_parent_class
->finalize
) (object
);
406 //-----------------------------------------------------------------------------
407 // implement callbacks from wxGtkTreeModel class by letting
408 // them call the methods of wxWidgets' wxDataViewModel
409 //-----------------------------------------------------------------------------
411 static GtkTreeModelFlags
412 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
414 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model
), (GtkTreeModelFlags
)0 );
416 return GTK_TREE_MODEL_ITERS_PERSIST
;
420 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
422 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
423 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
425 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
429 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
432 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
433 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
435 GType gtype
= G_TYPE_INVALID
;
437 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
439 if (wxtype
== wxT("string"))
440 gtype
= G_TYPE_STRING
;
443 wxFAIL_MSG( _T("non-string columns not supported yet") );
450 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
454 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
455 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
456 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
458 return wxtree_model
->internal
->get_iter( iter
, path
);
462 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
465 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
466 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
467 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
469 return wxtree_model
->internal
->get_path( iter
);
473 wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
476 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
477 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
478 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
480 return wxtree_model
->internal
->get_path_safe( iter
);
484 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
489 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
490 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
492 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
493 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
494 if (mtype
== wxT("string"))
497 g_value_init( value
, G_TYPE_STRING
);
498 wxDataViewItem
item( (void*) iter
->user_data
);
499 model
->GetValue( variant
, item
, (unsigned int) column
);
501 g_value_set_string( value
, variant
.GetString().utf8_str() );
505 wxFAIL_MSG( _T("non-string columns not supported yet") );
510 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
513 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
514 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
515 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
517 return wxtree_model
->internal
->iter_next( iter
);
521 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
525 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
526 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
527 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
529 return wxtree_model
->internal
->iter_children( iter
, parent
);
533 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
536 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
537 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
538 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
540 return wxtree_model
->internal
->iter_has_child( iter
);
544 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
547 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
548 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
549 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, 0);
551 return wxtree_model
->internal
->iter_n_children( iter
);
555 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
560 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
561 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
563 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
567 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
571 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
572 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
573 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
575 return wxtree_model
->internal
->iter_parent( iter
, child
);
579 gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
580 gint
*sort_column_id
,
583 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
585 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
588 *sort_column_id
= tree_model
->internal
->GetDataViewModel()->GetSortingColumn();
592 bool ascending
= tree_model
->internal
->GetDataViewModel()->GetSortOrderAscending();
594 *order
= GTK_SORT_ASCENDING
;
596 *order
= GTK_SORT_DESCENDING
;
602 void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
606 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
607 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
609 bool ascending
= TRUE
;
610 if (order
!= GTK_SORT_ASCENDING
)
613 if ((sort_column_id
== (gint
) tree_model
->internal
->GetDataViewModel()->GetSortingColumn()) &&
614 (ascending
== tree_model
->internal
->GetDataViewModel()->GetSortOrderAscending()))
617 tree_model
->internal
->GetDataViewModel()->SetSortingColumn( sort_column_id
);
619 tree_model
->internal
->GetDataViewModel()->SetSortOrderAscending( ascending
);
621 gtk_tree_sortable_sort_column_changed (sortable
);
623 tree_model
->internal
->GetDataViewModel()->Resort();
626 void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
628 GtkTreeIterCompareFunc func
,
630 GtkDestroyNotify destroy
)
632 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
633 g_return_if_fail (func
!= NULL
);
636 void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
637 GtkTreeIterCompareFunc func
,
639 GtkDestroyNotify destroy
)
641 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
642 g_return_if_fail (func
!= NULL
);
644 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
647 gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
652 //-----------------------------------------------------------------------------
653 // define new GTK+ class wxGtkRendererRenderer
654 //-----------------------------------------------------------------------------
658 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
659 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
660 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
661 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
662 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
663 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
665 GType
gtk_wx_cell_renderer_get_type (void);
667 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
668 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
670 struct _GtkWxCellRenderer
672 GtkCellRenderer parent
;
675 wxDataViewCustomRenderer
*cell
;
679 struct _GtkWxCellRendererClass
681 GtkCellRendererClass cell_parent_class
;
685 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
686 static void gtk_wx_cell_renderer_init (
687 GtkWxCellRenderer
*cell
);
688 static void gtk_wx_cell_renderer_class_init(
689 GtkWxCellRendererClass
*klass
);
690 static void gtk_wx_cell_renderer_finalize (
692 static void gtk_wx_cell_renderer_get_size (
693 GtkCellRenderer
*cell
,
695 GdkRectangle
*rectangle
,
700 static void gtk_wx_cell_renderer_render (
701 GtkCellRenderer
*cell
,
704 GdkRectangle
*background_area
,
705 GdkRectangle
*cell_area
,
706 GdkRectangle
*expose_area
,
707 GtkCellRendererState flags
);
708 static gboolean
gtk_wx_cell_renderer_activate(
709 GtkCellRenderer
*cell
,
713 GdkRectangle
*background_area
,
714 GdkRectangle
*cell_area
,
715 GtkCellRendererState flags
);
716 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
717 GtkCellRenderer
*cell
,
721 GdkRectangle
*background_area
,
722 GdkRectangle
*cell_area
,
723 GtkCellRendererState flags
);
726 static GObjectClass
*cell_parent_class
= NULL
;
731 gtk_wx_cell_renderer_get_type (void)
733 static GType cell_wx_type
= 0;
737 const GTypeInfo cell_wx_info
=
739 sizeof (GtkWxCellRendererClass
),
740 NULL
, /* base_init */
741 NULL
, /* base_finalize */
742 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
743 NULL
, /* class_finalize */
744 NULL
, /* class_data */
745 sizeof (GtkWxCellRenderer
),
747 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
750 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
751 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
758 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
761 cell
->last_click
= 0;
765 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
767 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
768 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
770 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
772 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
774 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
775 cell_class
->render
= gtk_wx_cell_renderer_render
;
776 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
777 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
781 gtk_wx_cell_renderer_finalize (GObject
*object
)
784 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
788 gtk_wx_cell_renderer_new (void)
790 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
795 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
796 GtkCellRenderer
*renderer
,
800 GdkRectangle
*background_area
,
801 GdkRectangle
*cell_area
,
802 GtkCellRendererState flags
)
804 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
805 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
806 if (!cell
->HasEditorCtrl())
810 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
816 rect
.x
+= cell_area
->x
;
817 rect
.y
+= cell_area
->y
;
818 // rect.width -= renderer->xpad * 2;
819 // rect.height -= renderer->ypad * 2;
821 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
822 wxRect
renderrect( cell_area
->x
, cell_area
->y
, cell_area
->width
, cell_area
->height
);
824 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
826 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
827 wxDataViewItem
item( (void*) iter
.user_data
);
828 gtk_tree_path_free( treepath
);
830 cell
->StartEditing( item
, renderrect
);
836 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
838 GdkRectangle
*cell_area
,
844 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
845 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
847 wxSize size
= cell
->GetSize();
849 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
850 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
857 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
861 *x_offset
= (gint
)((renderer
->xalign
*
862 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
863 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
867 *y_offset
= (gint
)((renderer
->yalign
*
868 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
869 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
877 *height
= calc_height
;
881 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
884 GdkRectangle
*background_area
,
885 GdkRectangle
*cell_area
,
886 GdkRectangle
*expose_area
,
887 GtkCellRendererState flags
)
890 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
891 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
894 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
900 rect
.x
+= cell_area
->x
;
901 rect
.y
+= cell_area
->y
;
902 rect
.width
-= renderer
->xpad
* 2;
903 rect
.height
-= renderer
->ypad
* 2;
906 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
908 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
909 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
910 if (dc
->m_window
== NULL
)
912 dc
->m_window
= window
;
917 if (flags
& GTK_CELL_RENDERER_SELECTED
)
918 state
|= wxDATAVIEW_CELL_SELECTED
;
919 if (flags
& GTK_CELL_RENDERER_PRELIT
)
920 state
|= wxDATAVIEW_CELL_PRELIT
;
921 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
922 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
923 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
924 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
925 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
926 state
|= wxDATAVIEW_CELL_FOCUSED
;
927 cell
->Render( renderrect
, dc
, state
);
932 gtk_wx_cell_renderer_activate(
933 GtkCellRenderer
*renderer
,
937 GdkRectangle
*background_area
,
938 GdkRectangle
*cell_area
,
939 GtkCellRendererState flags
)
941 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
942 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
945 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
951 rect
.x
+= cell_area
->x
;
952 rect
.y
+= cell_area
->y
;
953 rect
.width
-= renderer
->xpad
* 2;
954 rect
.height
-= renderer
->ypad
* 2;
956 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
958 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
960 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
963 gtk_tree_path_free( treepath
);
965 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
971 // activated by <ENTER>
972 if (cell
->Activate( renderrect
, model
, item
, model_col
))
977 else if (event
->type
== GDK_BUTTON_PRESS
)
979 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
980 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
981 ((int) button_event
->y
) - renderrect
.y
);
984 if (button_event
->button
== 1)
986 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
988 // TODO: query system double-click time
989 if (button_event
->time
- wxrenderer
->last_click
< 400)
990 if (cell
->Activate( renderrect
, model
, item
, model_col
))
993 if (button_event
->button
== 3)
995 if (cell
->RightClick( pt
, renderrect
, model
, item
, model_col
))
999 wxrenderer
->last_click
= button_event
->time
;
1007 // ---------------------------------------------------------
1008 // wxGtkDataViewModelNotifier
1009 // ---------------------------------------------------------
1011 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1014 wxGtkDataViewModelNotifier( GtkWxTreeModel
*wxgtk_model
,
1015 wxDataViewModel
*wx_model
,
1016 wxDataViewCtrl
*ctrl
);
1017 ~wxGtkDataViewModelNotifier();
1019 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1020 virtual bool ItemDeleted( const wxDataViewItem
&item
);
1021 virtual bool ItemChanged( const wxDataViewItem
&item
);
1022 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
1023 virtual bool Cleared();
1024 virtual void Resort();
1026 GtkWxTreeModel
*m_wxgtk_model
;
1027 wxDataViewModel
*m_wx_model
;
1028 wxDataViewCtrl
*m_owner
;
1031 // ---------------------------------------------------------
1032 // wxGtkDataViewListModelNotifier
1033 // ---------------------------------------------------------
1035 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1036 GtkWxTreeModel
* wxgtk_model
, wxDataViewModel
*wx_model
,
1037 wxDataViewCtrl
*ctrl
)
1039 m_wxgtk_model
= wxgtk_model
;
1040 m_wx_model
= wx_model
;
1044 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1047 m_wxgtk_model
= NULL
;
1050 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1052 m_owner
->GtkGetInternal()->ItemAdded( parent
, item
);
1055 iter
.stamp
= m_wxgtk_model
->stamp
;
1056 iter
.user_data
= (gpointer
) item
.GetID();
1058 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1059 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1060 gtk_tree_model_row_inserted(
1061 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1062 gtk_tree_path_free (path
);
1067 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&item
)
1070 iter
.stamp
= m_wxgtk_model
->stamp
;
1071 iter
.user_data
= (gpointer
) item
.GetID();
1073 GtkTreePath
*path
= wxgtk_tree_model_get_path_safe(
1074 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1075 gtk_tree_model_row_deleted(
1076 GTK_TREE_MODEL(m_wxgtk_model
), path
);
1077 gtk_tree_path_free (path
);
1079 m_owner
->GtkGetInternal()->ItemDeleted( item
);
1084 void wxGtkDataViewModelNotifier::Resort()
1086 m_owner
->GtkGetInternal()->Resort();
1089 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1092 iter
.stamp
= m_wxgtk_model
->stamp
;
1093 iter
.user_data
= (gpointer
) item
.GetID();
1095 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1096 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1097 gtk_tree_model_row_changed(
1098 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1099 gtk_tree_path_free (path
);
1101 m_owner
->GtkGetInternal()->ItemChanged( item
);
1106 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_col
)
1108 // This adds GTK+'s missing MVC logic for ValueChanged
1110 for (index
= 0; index
< m_owner
->GetColumnCount(); index
++)
1112 wxDataViewColumn
*column
= m_owner
->GetColumn( index
);
1113 if (column
->GetModelColumn() == model_col
)
1115 GtkTreeView
*widget
= GTK_TREE_VIEW(m_owner
->m_treeview
);
1116 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1120 iter
.stamp
= m_wxgtk_model
->stamp
;
1121 iter
.user_data
= (gpointer
) item
.GetID();
1122 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1123 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1124 GdkRectangle cell_area
;
1125 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1126 gtk_tree_path_free( path
);
1128 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1129 double d
= gtk_adjustment_get_value( hadjust
);
1130 int xdiff
= (int) d
;
1132 int ydiff
= gcolumn
->button
->allocation
.height
;
1134 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1135 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1137 m_owner
->GtkGetInternal()->ValueChanged( item
, model_col
);
1146 bool wxGtkDataViewModelNotifier::Cleared()
1148 // TODO: delete everything
1150 m_owner
->GtkGetInternal()->Cleared();
1155 // ---------------------------------------------------------
1156 // wxDataViewRenderer
1157 // ---------------------------------------------------------
1159 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1161 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1163 wxDataViewRendererBase( varianttype
, mode
, align
)
1167 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1168 // after the m_renderer pointer has been initialized
1171 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1173 GtkCellRendererMode gtkMode
;
1176 case wxDATAVIEW_CELL_INERT
:
1177 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1179 case wxDATAVIEW_CELL_ACTIVATABLE
:
1180 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1182 case wxDATAVIEW_CELL_EDITABLE
:
1183 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1187 GValue gvalue
= { 0, };
1188 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1189 g_value_set_enum( &gvalue
, gtkMode
);
1190 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1191 g_value_unset( &gvalue
);
1194 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1196 wxDataViewCellMode ret
;
1199 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1201 switch (g_value_get_enum(&gvalue
))
1203 case GTK_CELL_RENDERER_MODE_INERT
:
1204 ret
= wxDATAVIEW_CELL_INERT
;
1206 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1207 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1209 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1210 ret
= wxDATAVIEW_CELL_EDITABLE
;
1214 g_value_unset( &gvalue
);
1219 void wxDataViewRenderer::SetAlignment( int align
)
1221 // horizontal alignment:
1223 gfloat xalign
= 0.0;
1224 if (align
& wxALIGN_RIGHT
)
1226 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1229 GValue gvalue
= { 0, };
1230 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1231 g_value_set_float( &gvalue
, xalign
);
1232 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1233 g_value_unset( &gvalue
);
1235 // vertical alignment:
1237 gfloat yalign
= 0.0;
1238 if (align
& wxALIGN_BOTTOM
)
1240 else if (align
& wxALIGN_CENTER_VERTICAL
)
1243 GValue gvalue2
= { 0, };
1244 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1245 g_value_set_float( &gvalue2
, yalign
);
1246 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1247 g_value_unset( &gvalue2
);
1250 int wxDataViewRenderer::GetAlignment() const
1255 // horizontal alignment:
1257 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
1258 float xalign
= g_value_get_float( &gvalue
);
1260 ret
|= wxALIGN_LEFT
;
1261 else if (xalign
== 0.5)
1262 ret
|= wxALIGN_CENTER_HORIZONTAL
;
1264 ret
|= wxALIGN_RIGHT
;
1265 g_value_unset( &gvalue
);
1268 // vertical alignment:
1270 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
1271 float yalign
= g_value_get_float( &gvalue
);
1274 else if (yalign
== 0.5)
1275 ret
|= wxALIGN_CENTER_VERTICAL
;
1277 ret
|= wxALIGN_BOTTOM
;
1278 g_value_unset( &gvalue
);
1285 // ---------------------------------------------------------
1286 // wxDataViewTextRenderer
1287 // ---------------------------------------------------------
1290 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1291 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1294 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1295 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1297 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1299 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1300 wxVariant value
= tmp
;
1301 if (!cell
->Validate( value
))
1304 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1306 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1308 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, path
);
1309 wxDataViewItem
item( (void*) iter
.user_data
);;
1310 gtk_tree_path_free( path
);
1312 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1314 model
->SetValue( value
, item
, model_col
);
1315 model
->ValueChanged( item
, model_col
);
1318 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1320 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1322 wxDataViewRenderer( varianttype
, mode
, align
)
1324 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_text_new();
1326 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1328 GValue gvalue
= { 0, };
1329 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1330 g_value_set_boolean( &gvalue
, true );
1331 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1332 g_value_unset( &gvalue
);
1334 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1338 SetAlignment(align
);
1341 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1343 wxString tmp
= value
;
1345 GValue gvalue
= { 0, };
1346 g_value_init( &gvalue
, G_TYPE_STRING
);
1347 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( tmp
, GetOwner()->GetOwner()->GetFont() ) );
1348 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1349 g_value_unset( &gvalue
);
1354 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1356 GValue gvalue
= { 0, };
1357 g_value_init( &gvalue
, G_TYPE_STRING
);
1358 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1359 wxString tmp
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
),
1360 wx_const_cast(wxDataViewTextRenderer
*, this)->GetOwner()->GetOwner()->GetFont() );
1361 g_value_unset( &gvalue
);
1368 void wxDataViewTextRenderer::SetAlignment( int align
)
1370 wxDataViewRenderer::SetAlignment(align
);
1372 if (gtk_check_version(2,10,0))
1375 // horizontal alignment:
1376 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1377 if (align
& wxALIGN_RIGHT
)
1378 pangoAlign
= PANGO_ALIGN_RIGHT
;
1379 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1380 pangoAlign
= PANGO_ALIGN_CENTER
;
1382 GValue gvalue
= { 0, };
1383 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1384 g_value_set_enum( &gvalue
, pangoAlign
);
1385 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1386 g_value_unset( &gvalue
);
1389 // ---------------------------------------------------------
1390 // wxDataViewBitmapRenderer
1391 // ---------------------------------------------------------
1393 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1395 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1397 wxDataViewRenderer( varianttype
, mode
, align
)
1399 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_pixbuf_new();
1402 SetAlignment(align
);
1405 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1407 if (value
.GetType() == wxT("wxBitmap"))
1412 // This may create a Pixbuf representation in the
1413 // wxBitmap object (and it will stay there)
1414 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1416 GValue gvalue
= { 0, };
1417 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1418 g_value_set_object( &gvalue
, pixbuf
);
1419 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1420 g_value_unset( &gvalue
);
1425 if (value
.GetType() == wxT("wxIcon"))
1430 // This may create a Pixbuf representation in the
1431 // wxBitmap object (and it will stay there)
1432 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1434 GValue gvalue
= { 0, };
1435 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1436 g_value_set_object( &gvalue
, pixbuf
);
1437 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1438 g_value_unset( &gvalue
);
1446 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1451 // ---------------------------------------------------------
1452 // wxDataViewToggleRenderer
1453 // ---------------------------------------------------------
1456 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1457 gchar
*path
, gpointer user_data
);
1460 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1461 gchar
*path
, gpointer user_data
)
1463 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1466 GValue gvalue
= { 0, };
1467 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1468 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1469 bool tmp
= g_value_get_boolean( &gvalue
);
1470 g_value_unset( &gvalue
);
1474 wxVariant value
= tmp
;
1475 if (!cell
->Validate( value
))
1478 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1480 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1482 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, gtk_path
);
1483 wxDataViewItem
item( (void*) iter
.user_data
);;
1484 gtk_tree_path_free( gtk_path
);
1486 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1488 model
->SetValue( value
, item
, model_col
);
1489 model
->ValueChanged( item
, model_col
);
1492 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1494 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1495 wxDataViewCellMode mode
, int align
) :
1496 wxDataViewRenderer( varianttype
, mode
, align
)
1498 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
1500 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1502 g_signal_connect_after( m_renderer
, "toggled",
1503 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1507 GValue gvalue
= { 0, };
1508 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1509 g_value_set_boolean( &gvalue
, false );
1510 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1511 g_value_unset( &gvalue
);
1515 SetAlignment(align
);
1518 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1522 GValue gvalue
= { 0, };
1523 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1524 g_value_set_boolean( &gvalue
, tmp
);
1525 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1526 g_value_unset( &gvalue
);
1531 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1533 GValue gvalue
= { 0, };
1534 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1535 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1536 bool tmp
= g_value_get_boolean( &gvalue
);
1537 g_value_unset( &gvalue
);
1544 // ---------------------------------------------------------
1545 // wxDataViewCustomRenderer
1546 // ---------------------------------------------------------
1548 class wxDataViewCtrlDC
: public wxWindowDC
1551 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1553 GtkWidget
*widget
= window
->m_treeview
;
1557 m_context
= window
->GtkGetPangoDefaultContext();
1558 m_layout
= pango_layout_new( m_context
);
1559 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1561 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1563 // Set m_window later
1565 // m_owner = window;
1569 // ---------------------------------------------------------
1570 // wxDataViewCustomRenderer
1571 // ---------------------------------------------------------
1573 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1575 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1576 wxDataViewCellMode mode
, int align
,
1578 wxDataViewRenderer( varianttype
, mode
, align
)
1588 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1590 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1591 renderer
->cell
= this;
1593 m_renderer
= (GtkCellRenderer
*) renderer
;
1596 SetAlignment(align
);
1601 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1607 wxDC
*wxDataViewCustomRenderer::GetDC()
1611 if (GetOwner() == NULL
)
1613 if (GetOwner()->GetOwner() == NULL
)
1615 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1621 // ---------------------------------------------------------
1622 // wxDataViewProgressRenderer
1623 // ---------------------------------------------------------
1625 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1627 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1628 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1629 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1635 if (!gtk_check_version(2,6,0))
1637 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
1639 GValue gvalue
= { 0, };
1640 g_value_init( &gvalue
, G_TYPE_STRING
);
1642 // FIXME: font encoding support
1643 g_value_set_string( &gvalue
, wxGTK_CONV_SYS(m_label
) );
1644 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1645 g_value_unset( &gvalue
);
1648 SetAlignment(align
);
1653 // Use custom cell code
1654 wxDataViewCustomRenderer::Init(mode
, align
);
1658 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1662 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1665 if (!gtk_check_version(2,6,0))
1667 gint tmp
= (long) value
;
1668 GValue gvalue
= { 0, };
1669 g_value_init( &gvalue
, G_TYPE_INT
);
1670 g_value_set_int( &gvalue
, tmp
);
1671 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1672 g_value_unset( &gvalue
);
1677 m_value
= (long) value
;
1679 if (m_value
< 0) m_value
= 0;
1680 if (m_value
> 100) m_value
= 100;
1686 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1691 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1693 double pct
= (double)m_value
/ 100.0;
1695 bar
.width
= (int)(cell
.width
* pct
);
1696 dc
->SetPen( *wxTRANSPARENT_PEN
);
1697 dc
->SetBrush( *wxBLUE_BRUSH
);
1698 dc
->DrawRectangle( bar
);
1700 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1701 dc
->SetPen( *wxBLACK_PEN
);
1702 dc
->DrawRectangle( cell
);
1707 wxSize
wxDataViewProgressRenderer::GetSize() const
1709 return wxSize(40,12);
1712 // ---------------------------------------------------------
1713 // wxDataViewDateRenderer
1714 // ---------------------------------------------------------
1716 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1719 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1720 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
1721 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1726 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1727 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1728 sizer
->Add( m_cal
, 1, wxGROW
);
1733 virtual void OnDismiss()
1737 void OnCalendar( wxCalendarEvent
&event
);
1739 wxCalendarCtrl
*m_cal
;
1740 wxDataViewModel
*m_model
;
1741 wxDataViewItem m_item
;
1745 DECLARE_EVENT_TABLE()
1748 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1749 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1752 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1754 wxDateTime date
= event
.GetDate();
1755 wxVariant value
= date
;
1756 m_model
->SetValue( value
, m_item
, m_col
);
1757 m_model
->ValueChanged( m_item
, m_col
);
1761 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1763 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1764 wxDataViewCellMode mode
, int align
) :
1765 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1768 SetAlignment(align
);
1771 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1773 m_date
= value
.GetDateTime();
1778 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1783 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1785 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1786 wxString tmp
= m_date
.FormatDate();
1787 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1792 wxSize
wxDataViewDateRenderer::GetSize() const
1794 wxString tmp
= m_date
.FormatDate();
1796 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1797 return wxSize(x
,y
+d
);
1800 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewModel
*model
,
1801 const wxDataViewItem
&item
, unsigned int col
)
1804 model
->GetValue( variant
, item
, col
);
1805 wxDateTime value
= variant
.GetDateTime();
1807 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1808 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1809 wxPoint pos
= wxGetMousePosition();
1812 popup
->Popup( popup
->m_cal
);
1817 // ---------------------------------------------------------
1819 // ---------------------------------------------------------
1823 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
1824 GdkEventButton
*gdk_event
,
1825 wxDataViewColumn
*column
)
1827 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
1830 if (gdk_event
->button
== 1)
1832 wxDataViewCtrl
*dv
= column
->GetOwner();
1833 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
1834 event
.SetDataViewColumn( column
);
1835 event
.SetModel( dv
->GetModel() );
1836 if (dv
->GetEventHandler()->ProcessEvent( event
))
1844 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1845 GtkCellRenderer
*cell
,
1846 GtkTreeModel
*model
,
1852 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1853 GtkCellRenderer
*renderer
,
1854 GtkTreeModel
*model
,
1858 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
1859 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
1861 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
1863 wxDataViewItem
item( (void*) iter
->user_data
);
1866 tree_model
->internal
->GetDataViewModel()->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
1868 if (value
.GetType() != cell
->GetVariantType())
1869 wxLogError( wxT("Wrong type, required: %s but: %s"),
1870 value
.GetType().c_str(),
1871 cell
->GetVariantType().c_str() );
1873 cell
->SetValue( value
);
1876 wxListItemAttr attr
;
1877 tree_model
->model
->GetAttr( attr
, cell
->GetOwner()->GetModelColumn(), model_row
);
1879 if (attr
.HasBackgroundColour())
1881 wxColour colour
= attr
.GetBackgroundColour();
1882 const GdkColor
* const gcol
= colour
.GetColor();
1884 GValue gvalue
= { 0, };
1885 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
1886 g_value_set_boxed( &gvalue
, gcol
);
1887 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
1888 g_value_unset( &gvalue
);
1892 GValue gvalue
= { 0, };
1893 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1894 g_value_set_boolean( &gvalue
, FALSE
);
1895 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
1896 g_value_unset( &gvalue
);
1902 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1904 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1905 unsigned int model_column
, int width
,
1906 wxAlignment align
, int flags
) :
1907 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1909 Init( align
, flags
, width
);
1911 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
1915 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1916 unsigned int model_column
, int width
,
1917 wxAlignment align
, int flags
) :
1918 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1920 Init( align
, flags
, width
);
1922 SetBitmap( bitmap
);
1925 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
1927 m_isConnected
= false;
1929 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
1930 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1931 m_column
= (GtkWidget
*) column
;
1934 SetAlignment( align
);
1936 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
1937 // as GTK+ is smart and unless explicitely told, will set the minimal
1938 // width to the title's lenght, which is a better default
1940 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
1941 // that we use for the wxDataViewCtrl
1942 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1943 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
1945 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
1947 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1948 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
1951 wxDataViewColumn::~wxDataViewColumn()
1955 void wxDataViewColumn::OnInternalIdle()
1960 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
1962 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1965 g_signal_connect(column
->button
, "button_press_event",
1966 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
1968 m_isConnected
= true;
1973 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
1975 wxDataViewColumnBase::SetOwner( owner
);
1977 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1979 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
1982 void wxDataViewColumn::SetTitle( const wxString
&title
)
1984 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1988 // disconnect before column->button gets recreated
1989 g_signal_handlers_disconnect_by_func( column
->button
,
1990 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
1992 m_isConnected
= false;
1995 // FIXME: can it really happen that we don't have the owner here??
1996 wxDataViewCtrl
*ctrl
= GetOwner();
1997 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
1998 : wxGTK_CONV_SYS(title
) );
2000 gtk_tree_view_column_set_widget( column
, NULL
);
2003 wxString
wxDataViewColumn::GetTitle() const
2005 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
2006 return wxConvFileName
->cMB2WX(str
);
2009 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
2011 wxDataViewColumnBase::SetBitmap( bitmap
);
2013 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2016 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
2018 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
2019 if (bitmap
.GetMask())
2020 mask
= bitmap
.GetMask()->GetBitmap();
2022 if (bitmap
.HasPixbuf())
2024 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
2025 bitmap
.GetPixbuf());
2029 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
2030 bitmap
.GetPixmap(), mask
);
2032 gtk_widget_show( GTK_WIDGET(gtk_image
) );
2034 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
2038 gtk_tree_view_column_set_widget( column
, NULL
);
2042 void wxDataViewColumn::SetHidden( bool hidden
)
2044 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
2047 void wxDataViewColumn::SetResizeable( bool resizeable
)
2049 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
2052 void wxDataViewColumn::SetAlignment( wxAlignment align
)
2054 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2056 gfloat xalign
= 0.0;
2057 if (align
== wxALIGN_RIGHT
)
2059 if (align
== wxALIGN_CENTER_HORIZONTAL
||
2060 align
== wxALIGN_CENTER
)
2063 gtk_tree_view_column_set_alignment( column
, xalign
);
2066 wxAlignment
wxDataViewColumn::GetAlignment() const
2068 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
2071 return wxALIGN_RIGHT
;
2073 return wxALIGN_CENTER_HORIZONTAL
;
2075 return wxALIGN_LEFT
;
2078 void wxDataViewColumn::SetSortable( bool sortable
)
2080 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2083 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
2085 gtk_tree_view_column_set_sort_column_id( column
, -1 );
2088 bool wxDataViewColumn::IsSortable() const
2090 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2091 return (gtk_tree_view_column_get_sort_column_id( column
) != -1);
2094 bool wxDataViewColumn::IsResizeable() const
2096 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2097 return gtk_tree_view_column_get_resizable( column
);
2100 bool wxDataViewColumn::IsHidden() const
2102 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2103 return !gtk_tree_view_column_get_visible( column
);
2106 void wxDataViewColumn::SetSortOrder( bool ascending
)
2108 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2111 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
2113 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
2116 bool wxDataViewColumn::IsSortOrderAscending() const
2118 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2120 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
2123 void wxDataViewColumn::SetMinWidth( int width
)
2125 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2128 int wxDataViewColumn::GetMinWidth() const
2130 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2133 int wxDataViewColumn::GetWidth() const
2135 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2138 void wxDataViewColumn::SetWidth( int width
)
2140 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2144 //-----------------------------------------------------------------------------
2145 // wxGtkTreeModelNode
2146 //-----------------------------------------------------------------------------
2148 void wxGtkTreeModelNode::Resort()
2150 g_model
= m_internal
->GetDataViewModel();
2152 size_t child_count
= GetChildCount();
2153 if (child_count
== 0)
2156 size_t node_count
= GetNodesCount();
2158 if (child_count
== 1)
2160 if (node_count
== 1)
2162 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
2168 wxGtkTreeModelChildren
*new_array
= new wxGtkTreeModelChildren( wxGtkTreeModelNodeCmp
);
2171 for (pos
= 0; pos
< child_count
; pos
++)
2172 new_array
->Add( m_children
->Item( pos
) );
2174 gint
*new_order
= new gint
[child_count
];
2176 for (pos
= 0; pos
< child_count
; pos
++)
2178 void *id
= new_array
->Item( pos
);
2180 for (old_pos
= 0; old_pos
< child_count
; old_pos
++)
2182 if (id
== m_children
->Item(old_pos
))
2184 new_order
[pos
] = old_pos
;
2190 // for (pos = 0; pos < count; pos++)
2191 // m_children->Clear();
2194 m_children
= new_array
;
2196 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
2198 GtkTreePath
*path
= gtk_tree_path_new ();
2199 wxGtkTreeModelNode
*parent
= GetParent();
2200 void *id
= GetItem().GetID();
2204 int pos
= parent
->GetChildren().Index( id
);
2205 gtk_tree_path_prepend_index( path
, pos
);
2206 id
= parent
->GetItem().GetID();
2207 parent
= parent
->GetParent();
2211 iter
.user_data
= id
;
2212 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
2213 gtk_tree_model_rows_reordered( gtk_tree_model
, path
, &iter
, new_order
);
2215 gtk_tree_path_free (path
);
2217 delete [] new_order
;
2219 for (pos
= 0; pos
< node_count
; pos
++)
2221 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
2226 //-----------------------------------------------------------------------------
2227 // wxDataViewCtrlInternal
2228 //-----------------------------------------------------------------------------
2230 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
,
2231 wxDataViewModel
*wx_model
, GtkWxTreeModel
*gtk_model
)
2234 m_wx_model
= wx_model
;
2235 m_gtk_model
= gtk_model
;
2240 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2242 g_object_unref( m_gtk_model
);
2245 void wxDataViewCtrlInternal::InitTree()
2247 wxDataViewItem item
;
2248 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
2250 BuildBranch( m_root
);
2253 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
2255 if (node
->GetChildCount() == 0)
2257 wxDataViewItem child
= m_wx_model
->GetFirstChild( node
->GetItem() );
2258 while (child
.IsOk())
2260 if (m_wx_model
->IsContainer( child
))
2261 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
2263 node
->AddLeave( child
.GetID() );
2265 // Don't send any events here
2267 child
= m_wx_model
->GetNextSibling( child
);
2272 void wxDataViewCtrlInternal::Resort()
2277 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2279 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2280 if (m_wx_model
->IsContainer( item
))
2281 parent_node
->AddNode( new wxGtkTreeModelNode( parent_node
, item
, this ) );
2283 parent_node
->AddLeave( item
.GetID() );
2285 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, m_owner
->GetId() );
2286 event
.SetEventObject( m_owner
);
2287 event
.SetModel( m_owner
->GetModel() );
2288 event
.SetItem( item
);
2289 m_owner
->GetEventHandler()->ProcessEvent( event
);
2294 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&item
)
2296 wxGtkTreeModelNode
*parent
= FindParentNode( item
);
2297 parent
->DeleteChild( item
.GetID() );
2299 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, m_owner
->GetId() );
2300 event
.SetEventObject( m_owner
);
2301 event
.SetModel( m_owner
->GetModel() );
2302 event
.SetItem( item
);
2303 m_owner
->GetEventHandler()->ProcessEvent( event
);
2308 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
2310 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
, m_owner
->GetId() );
2311 event
.SetEventObject( m_owner
);
2312 event
.SetModel( m_owner
->GetModel() );
2313 event
.SetItem( item
);
2314 m_owner
->GetEventHandler()->ProcessEvent( event
);
2319 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
2321 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, m_owner
->GetId() );
2322 event
.SetEventObject( m_owner
);
2323 event
.SetModel( m_owner
->GetModel() );
2324 event
.SetColumn( col
);
2325 event
.SetItem( item
);
2326 m_owner
->GetEventHandler()->ProcessEvent( event
);
2331 bool wxDataViewCtrlInternal::Cleared()
2333 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, m_owner
->GetId() );
2334 event
.SetEventObject( m_owner
);
2335 event
.SetModel( m_owner
->GetModel() );
2336 m_owner
->GetEventHandler()->ProcessEvent( event
);
2341 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
2343 g_model
= m_wx_model
;
2345 int depth
= gtk_tree_path_get_depth( path
);
2347 wxGtkTreeModelNode
*node
= m_root
;
2350 for (i
= 0; i
< depth
; i
++)
2352 BuildBranch( node
);
2354 gint pos
= gtk_tree_path_get_indices (path
)[i
];
2355 if (pos
< 0) return FALSE
;
2356 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
2358 void* id
= node
->GetChildren().Item( (size_t) pos
);
2362 iter
->stamp
= m_gtk_model
->stamp
;
2363 iter
->user_data
= id
;
2367 size_t count
= node
->GetNodes().GetCount();
2369 for (pos2
= 0; pos2
< count
; pos2
++)
2371 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
2372 if (child_node
->GetItem().GetID() == id
)
2383 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
2385 g_model
= m_wx_model
;
2387 GtkTreePath
*retval
= gtk_tree_path_new ();
2388 void *id
= iter
->user_data
;
2390 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
2393 int pos
= node
->GetChildren().Index( id
);
2394 gtk_tree_path_prepend_index( retval
, pos
);
2396 id
= node
->GetItem().GetID();
2397 node
= node
->GetParent();
2403 GtkTreePath
*wxDataViewCtrlInternal::get_path_safe( GtkTreeIter
*iter
)
2405 g_model
= m_wx_model
;
2407 GtkTreePath
*retval
= gtk_tree_path_new ();
2408 void *id
= iter
->user_data
;
2410 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
2414 for (pos
= 0; pos
< node
->GetChildren().GetCount(); pos
++)
2416 if (id
== node
->GetChildren().Item( pos
))
2418 gtk_tree_path_prepend_index( retval
, (int) pos
);
2423 id
= node
->GetItem().GetID();
2424 node
= node
->GetParent();
2430 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
2432 g_model
= m_wx_model
;
2434 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
2435 unsigned int pos
= parent
->GetChildren().Index( iter
->user_data
);
2437 if (pos
== parent
->GetChildCount()-1)
2440 iter
->stamp
= m_gtk_model
->stamp
;
2441 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
2446 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
2448 g_model
= m_wx_model
;
2450 wxDataViewItem
item( (void*) parent
->user_data
);
2452 if (!m_wx_model
->IsContainer( item
))
2455 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2456 BuildBranch( parent_node
);
2458 if (parent_node
->GetChildCount() == 0)
2461 iter
->stamp
= m_gtk_model
->stamp
;
2462 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
2467 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
2469 g_model
= m_wx_model
;
2471 wxDataViewItem
item( (void*) iter
->user_data
);
2472 bool is_container
= m_wx_model
->IsContainer( item
);
2477 wxGtkTreeModelNode
*node
= FindNode( iter
);
2478 BuildBranch( node
);
2480 return (node
->GetChildCount() > 0);
2483 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
2485 g_model
= m_wx_model
;
2487 wxDataViewItem
item( (void*) iter
->user_data
);
2489 if (!m_wx_model
->IsContainer( item
))
2492 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
2493 BuildBranch( parent_node
);
2495 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2497 return parent_node
->GetChildCount();
2500 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
2502 g_model
= m_wx_model
;
2505 if (parent
) id
= (void*) parent
->user_data
;
2506 wxDataViewItem
item( id
);
2508 if (!m_wx_model
->IsContainer( item
))
2511 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2512 BuildBranch( parent_node
);
2514 // wxPrintf( "iter_nth_child %d\n", n );
2516 iter
->stamp
= m_gtk_model
->stamp
;
2517 iter
->user_data
= parent_node
->GetChildren().Item( n
);
2522 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
2524 g_model
= m_wx_model
;
2526 wxGtkTreeModelNode
*node
= FindParentNode( child
);
2530 iter
->stamp
= m_gtk_model
->stamp
;
2531 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2536 static wxGtkTreeModelNode
*
2537 wxDataViewCtrlInternal_FindNode( wxGtkTreeModelNode
*node
, const wxDataViewItem
&item
)
2539 if (!node
) return NULL
;
2541 size_t count
= node
->GetNodesCount();
2543 for (i
= 0; i
< count
; i
++)
2545 wxGtkTreeModelNode
*child
= node
->GetNodes().Item( i
);
2546 if (child
->GetItem().GetID() == item
.GetID())
2548 // wxPrintf( "leave findnode at %d\n", i );
2552 wxGtkTreeModelNode
*node2
= wxDataViewCtrlInternal_FindNode( child
, item
);
2555 // wxPrintf( "branch findnode at %d\n", i );
2563 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
2568 wxDataViewItem
item( (void*) iter
->user_data
);
2572 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_root
, item
);
2576 wxPrintf( "Not found %d\n", (int) iter
->user_data
);
2584 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
2589 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_root
, item
);
2593 wxPrintf( "Not found %d\n", (int) item
.GetID() );
2601 static wxGtkTreeModelNode
*
2602 wxDataViewCtrlInternal_FindParentNode( wxGtkTreeModelNode
*node
, const wxDataViewItem
&item
)
2604 size_t child_count
= node
->GetChildCount();
2605 void *id
= item
.GetID();
2606 const wxGtkTreeModelChildren
&children
= node
->GetChildren();
2608 for (pos
= 0; pos
< child_count
; pos
++)
2610 if (children
.Item( pos
) == id
)
2614 size_t node_count
= node
->GetNodesCount();
2615 for (pos
= 0; pos
< node_count
; pos
++)
2617 wxGtkTreeModelNode
*child
= node
->GetNodes().Item( pos
);
2619 wxGtkTreeModelNode
*node2
= wxDataViewCtrlInternal_FindParentNode( child
, item
);
2622 // wxPrintf( "branch findnode at %d\n", i );
2630 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
2635 wxDataViewItem
item( (void*) iter
->user_data
);
2639 return wxDataViewCtrlInternal_FindParentNode( m_root
, item
);
2642 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
2647 return wxDataViewCtrlInternal_FindParentNode( m_root
, item
);
2650 //-----------------------------------------------------------------------------
2651 // wxDataViewCtrl signal callbacks
2652 //-----------------------------------------------------------------------------
2655 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
2657 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
2660 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
, dv
->GetId() );
2662 event
.SetModel( dv
->GetModel() );
2663 dv
->GetEventHandler()->ProcessEvent( event
);
2667 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
2668 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
2670 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
2673 dv
->GtkGetInternal()->get_iter( &iter
, path
);
2674 wxDataViewItem
item( (void*) iter
.user_data
);;
2675 event
.SetItem( item
);
2676 event
.SetModel( dv
->GetModel() );
2677 dv
->GetEventHandler()->ProcessEvent( event
);
2680 //-----------------------------------------------------------------------------
2682 //-----------------------------------------------------------------------------
2684 //-----------------------------------------------------------------------------
2685 // InsertChild for wxDataViewCtrl
2686 //-----------------------------------------------------------------------------
2688 static void wxInsertChildInDataViewCtrl( wxWindowGTK
* parent
, wxWindowGTK
* child
)
2690 wxDataViewCtrl
* dvc
= (wxDataViewCtrl
*) parent
;
2691 GtkWidget
*treeview
= dvc
->GtkGetTreeView();
2693 // Insert widget in GtkTreeView
2694 if (GTK_WIDGET_REALIZED(treeview
))
2695 gtk_widget_set_parent_window( child
->m_widget
,
2696 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
2697 gtk_widget_set_parent( child
->m_widget
, treeview
);
2701 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
2702 GtkAllocation
*alloc
,
2703 wxDataViewCtrl
*win
)
2706 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
2709 wxWindow
*child
= node
->GetData();
2712 gtk_widget_size_request( child
->m_widget
, &req
);
2714 GtkAllocation alloc
;
2715 alloc
.x
= child
->m_x
;
2716 alloc
.y
= child
->m_y
;
2717 alloc
.width
= child
->m_width
;
2718 alloc
.height
= child
->m_height
;
2719 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
2721 node
= node
->GetNext();
2727 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
2729 wxDataViewCtrl::~wxDataViewCtrl()
2732 GetModel()->RemoveNotifier( m_notifier
);
2734 // remove the model from the GtkTreeView before it gets destroyed by the
2735 // wxDataViewCtrlBase's dtor
2736 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
2741 void wxDataViewCtrl::Init()
2746 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
2747 const wxPoint
& pos
, const wxSize
& size
,
2748 long style
, const wxValidator
& validator
)
2752 if (!PreCreation( parent
, pos
, size
) ||
2753 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
2755 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
2759 m_insertCallback
= wxInsertChildInDataViewCtrl
;
2761 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
2763 GtkScrolledWindowSetBorder(m_widget
, style
);
2765 m_treeview
= gtk_tree_view_new();
2766 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
2768 g_signal_connect (m_treeview
, "size_allocate",
2769 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
2772 if (!gtk_check_version(2,6,0))
2773 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
2776 if (style
& wxDV_MULTIPLE
)
2778 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2779 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
2782 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
2785 if (!gtk_check_version(2,10,0))
2787 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
2789 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
2790 (style
& wxDV_VERT_RULES
) != 0)
2791 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
2792 else if (style
& wxDV_VERT_RULES
)
2793 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
2794 else if (style
& wxDV_HORIZ_RULES
)
2795 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
2797 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
2802 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
2805 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
2806 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2807 gtk_widget_show (m_treeview
);
2809 m_parent
->DoAddChild( this );
2813 GtkEnableSelectionEvents();
2815 g_signal_connect_after (m_treeview
, "row_activated",
2816 G_CALLBACK (wxdataview_row_activated_callback
), this);
2821 void wxDataViewCtrl::OnInternalIdle()
2823 wxWindow::OnInternalIdle();
2825 unsigned int cols
= GetColumnCount();
2827 for (i
= 0; i
< cols
; i
++)
2829 wxDataViewColumn
*col
= GetColumn( i
);
2830 col
->OnInternalIdle();
2834 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
2836 if (!wxDataViewCtrlBase::AssociateModel( model
))
2839 GtkWxTreeModel
*gtk_model
= wxgtk_tree_model_new();
2840 m_internal
= new wxDataViewCtrlInternal( this, model
, gtk_model
);
2841 gtk_model
->internal
= m_internal
;
2843 m_notifier
= new wxGtkDataViewModelNotifier( gtk_model
, model
, this );
2845 model
->AddNotifier( m_notifier
);
2847 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_model
) );
2849 // unref in wxDataViewCtrlInternal
2850 // g_object_unref( gtk_model );
2855 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
2857 if (!wxDataViewCtrlBase::AppendColumn(col
))
2860 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
2862 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
2867 wxDataViewItem
wxDataViewCtrl::GetSelection()
2869 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2871 if (m_windowStyle
& wxDV_MULTIPLE
)
2877 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
2879 wxDataViewItem
item( (void*) iter
.user_data
);
2884 return wxDataViewItem(0);
2887 void wxDataViewCtrl::DoSetExpanderColumn()
2891 void wxDataViewCtrl::DoSetIndent()
2895 void wxDataViewCtrl::GtkDisableSelectionEvents()
2897 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2898 g_signal_connect_after (selection
, "changed",
2899 G_CALLBACK (wxdataview_selection_changed_callback
), this);
2902 void wxDataViewCtrl::GtkEnableSelectionEvents()
2904 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2905 g_signal_handlers_disconnect_by_func( selection
,
2906 (gpointer
) (wxdataview_selection_changed_callback
), this);
2911 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2913 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
2918 // !wxUSE_GENERICDATAVIEWCTRL
2921 // wxUSE_DATAVIEWCTRL