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 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 class wxGtkTreeModelNode
;
51 int LINKAGEMODE
wxGtkTreeModelNodeCmp( wxGtkTreeModelNode
* node1
, wxGtkTreeModelNode
* node2
);
53 WX_DEFINE_SORTED_ARRAY( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
55 class wxGtkTreeModelNode
58 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
59 wxDataViewModel
*model
)
64 m_children
= new wxGtkTreeModelNodes( wxGtkTreeModelNodeCmp
);
69 size_t count
= m_children
->GetCount();
71 for (i
= 0; i
< count
; i
++)
73 wxGtkTreeModelNode
*child
= m_children
->Item( i
);
79 wxGtkTreeModelNode
* GetParent()
81 wxGtkTreeModelNodes
&GetChildren()
82 { return *m_children
; }
83 wxGtkTreeModelNode
* GetNthChild( unsigned int n
)
84 { return m_children
->Item( n
); }
85 unsigned int Add( wxGtkTreeModelNode
* child
)
86 { return m_children
->Add( child
); }
88 unsigned int GetChildCount() { return m_children
->GetCount(); }
90 wxDataViewItem
&GetItem() { return m_item
; }
91 wxDataViewModel
*GetModel() { return m_model
; }
93 bool HasChildren() { return m_hasChildren
; }
94 void SetHasChildren( bool has
) { m_hasChildren
= has
; }
99 wxGtkTreeModelNode
*m_parent
;
100 wxGtkTreeModelNodes
*m_children
;
101 wxDataViewItem m_item
;
103 wxDataViewModel
*m_model
;
106 int LINKAGEMODE
wxGtkTreeModelNodeCmp( wxGtkTreeModelNode
* node1
, wxGtkTreeModelNode
* node2
)
108 return node1
->GetModel()->Compare( node1
->GetItem(), node2
->GetItem() );
113 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
116 class wxDataViewCtrlInternal
119 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
, GtkWxTreeModel
*owner
);
120 ~wxDataViewCtrlInternal();
122 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
123 GtkTreePath
*get_path( GtkTreeIter
*iter
);
124 GtkTreePath
*get_path_safe( GtkTreeIter
*iter
);
125 gboolean
iter_next( GtkTreeIter
*iter
);
126 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
127 gboolean
iter_has_child( GtkTreeIter
*iter
);
128 gint
iter_n_children( GtkTreeIter
*iter
);
129 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
130 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
132 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
133 GtkWxTreeModel
* GetOwner() { return m_gtk_model
; }
135 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
136 bool ItemDeleted( const wxDataViewItem
&item
);
142 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
143 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
144 void BuildBranch( wxGtkTreeModelNode
*branch
);
147 wxGtkTreeModelNode
*m_root
;
148 wxDataViewModel
*m_wx_model
;
149 GtkWxTreeModel
*m_gtk_model
;
150 wxDataViewCtrl
*m_owner
;
153 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
157 extern bool g_blockEventsOnDrag
;
159 //-----------------------------------------------------------------------------
160 // define new GTK+ class wxGtkTreeModel
161 //-----------------------------------------------------------------------------
165 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
166 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
167 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
168 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
169 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
170 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
172 GType
gtk_wx_tree_model_get_type (void);
174 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
176 struct _GtkWxTreeModel
182 wxDataViewCtrlInternal
*internal
;
185 struct _GtkWxTreeModelClass
187 GObjectClass list_parent_class
;
190 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
191 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
192 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
193 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
194 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
195 static void wxgtk_tree_model_finalize (GObject
*object
);
196 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
197 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
198 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
200 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
203 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
205 static GtkTreePath
*wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
207 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
211 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
213 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
215 GtkTreeIter
*parent
);
216 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
218 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
220 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
224 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
229 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
230 gint
*sort_column_id
,
232 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
235 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
237 GtkTreeIterCompareFunc func
,
239 GtkDestroyNotify destroy
);
240 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
241 GtkTreeIterCompareFunc func
,
243 GtkDestroyNotify destroy
);
244 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
248 static GObjectClass
*list_parent_class
= NULL
;
251 gtk_wx_tree_model_get_type (void)
253 static GType tree_model_type
= 0;
255 if (!tree_model_type
)
257 const GTypeInfo tree_model_info
=
259 sizeof (GtkWxTreeModelClass
),
260 NULL
, /* base_init */
261 NULL
, /* base_finalize */
262 (GClassInitFunc
) wxgtk_tree_model_class_init
,
263 NULL
, /* class_finalize */
264 NULL
, /* class_data */
265 sizeof (GtkWxTreeModel
),
267 (GInstanceInitFunc
) wxgtk_tree_model_init
,
270 static const GInterfaceInfo tree_model_iface_info
=
272 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
277 static const GInterfaceInfo sortable_iface_info
=
279 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
284 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
285 &tree_model_info
, (GTypeFlags
)0 );
287 g_type_add_interface_static (tree_model_type
,
289 &tree_model_iface_info
);
290 g_type_add_interface_static (tree_model_type
,
291 GTK_TYPE_TREE_SORTABLE
,
292 &sortable_iface_info
);
295 return tree_model_type
;
298 static GtkWxTreeModel
*
299 wxgtk_tree_model_new(void)
301 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
306 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
308 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
309 GObjectClass
*object_class
= (GObjectClass
*) klass
;
310 object_class
->finalize
= wxgtk_tree_model_finalize
;
314 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
316 iface
->get_flags
= wxgtk_tree_model_get_flags
;
317 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
318 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
319 iface
->get_iter
= wxgtk_tree_model_get_iter
;
320 iface
->get_path
= wxgtk_tree_model_get_path
;
321 iface
->get_value
= wxgtk_tree_model_get_value
;
322 iface
->iter_next
= wxgtk_tree_model_iter_next
;
323 iface
->iter_children
= wxgtk_tree_model_iter_children
;
324 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
325 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
326 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
327 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
331 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
333 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
334 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
335 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
336 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
337 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
341 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
343 tree_model
->internal
= NULL
;
344 tree_model
->stamp
= g_random_int();
348 wxgtk_tree_model_finalize (GObject
*object
)
351 (* list_parent_class
->finalize
) (object
);
356 //-----------------------------------------------------------------------------
357 // implement callbacks from wxGtkTreeModel class by letting
358 // them call the methods of wxWidgets' wxDataViewModel
359 //-----------------------------------------------------------------------------
361 static GtkTreeModelFlags
362 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
364 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model
), (GtkTreeModelFlags
)0 );
366 return GTK_TREE_MODEL_ITERS_PERSIST
;
370 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
372 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
373 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
375 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
379 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
382 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
383 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
385 GType gtype
= G_TYPE_INVALID
;
387 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
389 if (wxtype
== wxT("string"))
390 gtype
= G_TYPE_STRING
;
393 wxFAIL_MSG( _T("non-string columns not supported yet") );
400 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
404 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
405 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
406 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
408 return wxtree_model
->internal
->get_iter( iter
, path
);
412 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
415 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
416 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
417 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
419 return wxtree_model
->internal
->get_path( iter
);
423 wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
426 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
427 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
428 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
430 return wxtree_model
->internal
->get_path_safe( iter
);
434 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
439 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
440 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
442 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
443 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
444 if (mtype
== wxT("string"))
447 g_value_init( value
, G_TYPE_STRING
);
448 wxDataViewItem
item( (void*) iter
->user_data
);
449 model
->GetValue( variant
, item
, (unsigned int) column
);
451 g_value_set_string( value
, variant
.GetString().utf8_str() );
455 wxFAIL_MSG( _T("non-string columns not supported yet") );
460 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
463 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
464 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
465 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
467 return wxtree_model
->internal
->iter_next( iter
);
471 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
475 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
476 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
477 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
479 return wxtree_model
->internal
->iter_children( iter
, parent
);
483 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
486 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
487 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
488 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
490 return wxtree_model
->internal
->iter_has_child( iter
);
494 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
497 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
498 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
499 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, 0);
501 return wxtree_model
->internal
->iter_n_children( iter
);
505 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
510 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
511 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
513 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
517 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
521 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
522 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
523 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
525 return wxtree_model
->internal
->iter_parent( iter
, child
);
529 gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
530 gint
*sort_column_id
,
533 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
535 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
538 *sort_column_id
= tree_model
->internal
->GetDataViewModel()->GetSortingColumn();
542 bool ascending
= tree_model
->internal
->GetDataViewModel()->GetSortOrderAscending();
544 *order
= GTK_SORT_ASCENDING
;
546 *order
= GTK_SORT_DESCENDING
;
552 void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
556 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
557 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
559 // TODO check for equality
561 gtk_tree_sortable_sort_column_changed (sortable
);
563 tree_model
->internal
->GetDataViewModel()->SetSortingColumn( sort_column_id
);
565 bool ascending
= TRUE
;
566 if (order
!= GTK_SORT_ASCENDING
)
568 tree_model
->internal
->GetDataViewModel()->SetSortOrderAscending( ascending
);
570 tree_model
->internal
->GetDataViewModel()->Resort();
573 void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
575 GtkTreeIterCompareFunc func
,
577 GtkDestroyNotify destroy
)
579 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
580 g_return_if_fail (func
!= NULL
);
583 void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
584 GtkTreeIterCompareFunc func
,
586 GtkDestroyNotify destroy
)
588 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
589 g_return_if_fail (func
!= NULL
);
591 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
594 gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
599 //-----------------------------------------------------------------------------
600 // define new GTK+ class wxGtkRendererRenderer
601 //-----------------------------------------------------------------------------
605 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
606 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
607 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
608 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
609 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
610 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
612 GType
gtk_wx_cell_renderer_get_type (void);
614 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
615 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
617 struct _GtkWxCellRenderer
619 GtkCellRenderer parent
;
622 wxDataViewCustomRenderer
*cell
;
626 struct _GtkWxCellRendererClass
628 GtkCellRendererClass cell_parent_class
;
632 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
633 static void gtk_wx_cell_renderer_init (
634 GtkWxCellRenderer
*cell
);
635 static void gtk_wx_cell_renderer_class_init(
636 GtkWxCellRendererClass
*klass
);
637 static void gtk_wx_cell_renderer_finalize (
639 static void gtk_wx_cell_renderer_get_size (
640 GtkCellRenderer
*cell
,
642 GdkRectangle
*rectangle
,
647 static void gtk_wx_cell_renderer_render (
648 GtkCellRenderer
*cell
,
651 GdkRectangle
*background_area
,
652 GdkRectangle
*cell_area
,
653 GdkRectangle
*expose_area
,
654 GtkCellRendererState flags
);
655 static gboolean
gtk_wx_cell_renderer_activate(
656 GtkCellRenderer
*cell
,
660 GdkRectangle
*background_area
,
661 GdkRectangle
*cell_area
,
662 GtkCellRendererState flags
);
663 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
664 GtkCellRenderer
*cell
,
668 GdkRectangle
*background_area
,
669 GdkRectangle
*cell_area
,
670 GtkCellRendererState flags
);
673 static GObjectClass
*cell_parent_class
= NULL
;
678 gtk_wx_cell_renderer_get_type (void)
680 static GType cell_wx_type
= 0;
684 const GTypeInfo cell_wx_info
=
686 sizeof (GtkWxCellRendererClass
),
687 NULL
, /* base_init */
688 NULL
, /* base_finalize */
689 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
690 NULL
, /* class_finalize */
691 NULL
, /* class_data */
692 sizeof (GtkWxCellRenderer
),
694 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
697 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
698 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
705 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
708 cell
->last_click
= 0;
712 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
714 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
715 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
717 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
719 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
721 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
722 cell_class
->render
= gtk_wx_cell_renderer_render
;
723 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
724 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
728 gtk_wx_cell_renderer_finalize (GObject
*object
)
731 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
735 gtk_wx_cell_renderer_new (void)
737 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
742 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
743 GtkCellRenderer
*renderer
,
747 GdkRectangle
*background_area
,
748 GdkRectangle
*cell_area
,
749 GtkCellRendererState flags
)
751 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
752 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
753 if (!cell
->HasEditorCtrl())
757 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
763 rect
.x
+= cell_area
->x
;
764 rect
.y
+= cell_area
->y
;
765 // rect.width -= renderer->xpad * 2;
766 // rect.height -= renderer->ypad * 2;
768 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
769 wxRect
renderrect( cell_area
->x
, cell_area
->y
, cell_area
->width
, cell_area
->height
);
771 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
773 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
774 wxDataViewItem
item( (void*) iter
.user_data
);
775 gtk_tree_path_free( treepath
);
777 cell
->StartEditing( item
, renderrect
);
783 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
785 GdkRectangle
*cell_area
,
791 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
792 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
794 wxSize size
= cell
->GetSize();
796 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
797 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
804 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
808 *x_offset
= (gint
)((renderer
->xalign
*
809 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
810 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
814 *y_offset
= (gint
)((renderer
->yalign
*
815 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
816 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
824 *height
= calc_height
;
828 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
831 GdkRectangle
*background_area
,
832 GdkRectangle
*cell_area
,
833 GdkRectangle
*expose_area
,
834 GtkCellRendererState flags
)
837 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
838 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
841 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
847 rect
.x
+= cell_area
->x
;
848 rect
.y
+= cell_area
->y
;
849 rect
.width
-= renderer
->xpad
* 2;
850 rect
.height
-= renderer
->ypad
* 2;
853 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
855 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
856 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
857 if (dc
->m_window
== NULL
)
859 dc
->m_window
= window
;
864 if (flags
& GTK_CELL_RENDERER_SELECTED
)
865 state
|= wxDATAVIEW_CELL_SELECTED
;
866 if (flags
& GTK_CELL_RENDERER_PRELIT
)
867 state
|= wxDATAVIEW_CELL_PRELIT
;
868 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
869 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
870 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
871 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
872 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
873 state
|= wxDATAVIEW_CELL_FOCUSED
;
874 cell
->Render( renderrect
, dc
, state
);
879 gtk_wx_cell_renderer_activate(
880 GtkCellRenderer
*renderer
,
884 GdkRectangle
*background_area
,
885 GdkRectangle
*cell_area
,
886 GtkCellRendererState flags
)
888 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
889 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
892 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
898 rect
.x
+= cell_area
->x
;
899 rect
.y
+= cell_area
->y
;
900 rect
.width
-= renderer
->xpad
* 2;
901 rect
.height
-= renderer
->ypad
* 2;
903 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
905 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
907 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
910 gtk_tree_path_free( treepath
);
912 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
918 // activated by <ENTER>
919 if (cell
->Activate( renderrect
, model
, item
, model_col
))
924 else if (event
->type
== GDK_BUTTON_PRESS
)
926 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
927 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
928 ((int) button_event
->y
) - renderrect
.y
);
931 if (button_event
->button
== 1)
933 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
935 // TODO: query system double-click time
936 if (button_event
->time
- wxrenderer
->last_click
< 400)
937 if (cell
->Activate( renderrect
, model
, item
, model_col
))
940 if (button_event
->button
== 3)
942 if (cell
->RightClick( pt
, renderrect
, model
, item
, model_col
))
946 wxrenderer
->last_click
= button_event
->time
;
954 // ---------------------------------------------------------
955 // wxGtkDataViewModelNotifier
956 // ---------------------------------------------------------
958 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
961 wxGtkDataViewModelNotifier( GtkWxTreeModel
*wxgtk_model
,
962 wxDataViewModel
*wx_model
,
963 wxDataViewCtrl
*ctrl
);
964 ~wxGtkDataViewModelNotifier();
966 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
967 virtual bool ItemDeleted( const wxDataViewItem
&item
);
968 virtual bool ItemChanged( const wxDataViewItem
&item
);
969 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
970 virtual bool Cleared();
971 virtual void Resort();
973 GtkWxTreeModel
*m_wxgtk_model
;
974 wxDataViewModel
*m_wx_model
;
975 wxDataViewCtrl
*m_owner
;
978 // ---------------------------------------------------------
979 // wxGtkDataViewListModelNotifier
980 // ---------------------------------------------------------
982 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
983 GtkWxTreeModel
* wxgtk_model
, wxDataViewModel
*wx_model
,
984 wxDataViewCtrl
*ctrl
)
986 m_wxgtk_model
= wxgtk_model
;
987 m_wx_model
= wx_model
;
991 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
994 m_wxgtk_model
= NULL
;
997 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
999 m_owner
->GtkGetInternal()->ItemAdded( parent
, item
);
1002 iter
.stamp
= m_wxgtk_model
->stamp
;
1003 iter
.user_data
= (gpointer
) item
.GetID();
1005 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1006 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1007 gtk_tree_model_row_inserted(
1008 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1009 gtk_tree_path_free (path
);
1014 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&item
)
1017 iter
.stamp
= m_wxgtk_model
->stamp
;
1018 iter
.user_data
= (gpointer
) item
.GetID();
1020 GtkTreePath
*path
= wxgtk_tree_model_get_path_safe(
1021 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1022 gtk_tree_model_row_deleted(
1023 GTK_TREE_MODEL(m_wxgtk_model
), path
);
1024 gtk_tree_path_free (path
);
1026 m_owner
->GtkGetInternal()->ItemDeleted( item
);
1031 void wxGtkDataViewModelNotifier::Resort()
1033 m_owner
->GtkGetInternal()->Resort();
1036 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1039 iter
.stamp
= m_wxgtk_model
->stamp
;
1040 iter
.user_data
= (gpointer
) item
.GetID();
1042 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1043 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1044 gtk_tree_model_row_changed(
1045 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1046 gtk_tree_path_free (path
);
1051 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_col
)
1053 // This adds GTK+'s missing MVC logic for ValueChanged
1055 for (index
= 0; index
< m_owner
->GetColumnCount(); index
++)
1057 wxDataViewColumn
*column
= m_owner
->GetColumn( index
);
1058 if (column
->GetModelColumn() == model_col
)
1060 GtkTreeView
*widget
= GTK_TREE_VIEW(m_owner
->m_treeview
);
1061 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1065 iter
.stamp
= m_wxgtk_model
->stamp
;
1066 iter
.user_data
= (gpointer
) item
.GetID();
1067 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1068 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1069 GdkRectangle cell_area
;
1070 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1071 gtk_tree_path_free( path
);
1073 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1074 double d
= gtk_adjustment_get_value( hadjust
);
1075 int xdiff
= (int) d
;
1077 int ydiff
= gcolumn
->button
->allocation
.height
;
1079 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1080 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1087 bool wxGtkDataViewModelNotifier::Cleared()
1092 // ---------------------------------------------------------
1093 // wxDataViewRenderer
1094 // ---------------------------------------------------------
1096 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1098 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1100 wxDataViewRendererBase( varianttype
, mode
, align
)
1104 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1105 // after the m_renderer pointer has been initialized
1108 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1110 GtkCellRendererMode gtkMode
;
1113 case wxDATAVIEW_CELL_INERT
:
1114 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1116 case wxDATAVIEW_CELL_ACTIVATABLE
:
1117 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1119 case wxDATAVIEW_CELL_EDITABLE
:
1120 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1124 GValue gvalue
= { 0, };
1125 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1126 g_value_set_enum( &gvalue
, gtkMode
);
1127 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1128 g_value_unset( &gvalue
);
1131 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1133 wxDataViewCellMode ret
;
1136 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1138 switch (g_value_get_enum(&gvalue
))
1140 case GTK_CELL_RENDERER_MODE_INERT
:
1141 ret
= wxDATAVIEW_CELL_INERT
;
1143 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1144 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1146 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1147 ret
= wxDATAVIEW_CELL_EDITABLE
;
1151 g_value_unset( &gvalue
);
1156 void wxDataViewRenderer::SetAlignment( int align
)
1158 // horizontal alignment:
1160 gfloat xalign
= 0.0;
1161 if (align
& wxALIGN_RIGHT
)
1163 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1166 GValue gvalue
= { 0, };
1167 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1168 g_value_set_float( &gvalue
, xalign
);
1169 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1170 g_value_unset( &gvalue
);
1172 // vertical alignment:
1174 gfloat yalign
= 0.0;
1175 if (align
& wxALIGN_BOTTOM
)
1177 else if (align
& wxALIGN_CENTER_VERTICAL
)
1180 GValue gvalue2
= { 0, };
1181 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1182 g_value_set_float( &gvalue2
, yalign
);
1183 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1184 g_value_unset( &gvalue2
);
1187 int wxDataViewRenderer::GetAlignment() const
1192 // horizontal alignment:
1194 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
1195 float xalign
= g_value_get_float( &gvalue
);
1197 ret
|= wxALIGN_LEFT
;
1198 else if (xalign
== 0.5)
1199 ret
|= wxALIGN_CENTER_HORIZONTAL
;
1201 ret
|= wxALIGN_RIGHT
;
1202 g_value_unset( &gvalue
);
1205 // vertical alignment:
1207 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
1208 float yalign
= g_value_get_float( &gvalue
);
1211 else if (yalign
== 0.5)
1212 ret
|= wxALIGN_CENTER_VERTICAL
;
1214 ret
|= wxALIGN_BOTTOM
;
1215 g_value_unset( &gvalue
);
1222 // ---------------------------------------------------------
1223 // wxDataViewTextRenderer
1224 // ---------------------------------------------------------
1227 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1228 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1231 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1232 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1234 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1236 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1237 wxVariant value
= tmp
;
1238 if (!cell
->Validate( value
))
1241 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1243 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1245 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, path
);
1246 wxDataViewItem
item( (void*) iter
.user_data
);;
1247 gtk_tree_path_free( path
);
1249 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1251 model
->SetValue( value
, item
, model_col
);
1252 model
->ValueChanged( item
, model_col
);
1255 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1257 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1259 wxDataViewRenderer( varianttype
, mode
, align
)
1261 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_text_new();
1263 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1265 GValue gvalue
= { 0, };
1266 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1267 g_value_set_boolean( &gvalue
, true );
1268 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1269 g_value_unset( &gvalue
);
1271 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1275 SetAlignment(align
);
1278 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1280 wxString tmp
= value
;
1282 GValue gvalue
= { 0, };
1283 g_value_init( &gvalue
, G_TYPE_STRING
);
1284 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( tmp
, GetOwner()->GetOwner()->GetFont() ) );
1285 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1286 g_value_unset( &gvalue
);
1291 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1293 GValue gvalue
= { 0, };
1294 g_value_init( &gvalue
, G_TYPE_STRING
);
1295 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1296 wxString tmp
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
),
1297 wx_const_cast(wxDataViewTextRenderer
*, this)->GetOwner()->GetOwner()->GetFont() );
1298 g_value_unset( &gvalue
);
1305 void wxDataViewTextRenderer::SetAlignment( int align
)
1307 wxDataViewRenderer::SetAlignment(align
);
1309 if (gtk_check_version(2,10,0))
1312 // horizontal alignment:
1313 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1314 if (align
& wxALIGN_RIGHT
)
1315 pangoAlign
= PANGO_ALIGN_RIGHT
;
1316 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1317 pangoAlign
= PANGO_ALIGN_CENTER
;
1319 GValue gvalue
= { 0, };
1320 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1321 g_value_set_enum( &gvalue
, pangoAlign
);
1322 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1323 g_value_unset( &gvalue
);
1326 // ---------------------------------------------------------
1327 // wxDataViewBitmapRenderer
1328 // ---------------------------------------------------------
1330 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1332 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1334 wxDataViewRenderer( varianttype
, mode
, align
)
1336 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_pixbuf_new();
1339 SetAlignment(align
);
1342 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1344 if (value
.GetType() == wxT("wxBitmap"))
1349 // This may create a Pixbuf representation in the
1350 // wxBitmap object (and it will stay there)
1351 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1353 GValue gvalue
= { 0, };
1354 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1355 g_value_set_object( &gvalue
, pixbuf
);
1356 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1357 g_value_unset( &gvalue
);
1362 if (value
.GetType() == wxT("wxIcon"))
1367 // This may create a Pixbuf representation in the
1368 // wxBitmap object (and it will stay there)
1369 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1371 GValue gvalue
= { 0, };
1372 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1373 g_value_set_object( &gvalue
, pixbuf
);
1374 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1375 g_value_unset( &gvalue
);
1383 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1388 // ---------------------------------------------------------
1389 // wxDataViewToggleRenderer
1390 // ---------------------------------------------------------
1393 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1394 gchar
*path
, gpointer user_data
);
1397 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1398 gchar
*path
, gpointer user_data
)
1400 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1403 GValue gvalue
= { 0, };
1404 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1405 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1406 bool tmp
= g_value_get_boolean( &gvalue
);
1407 g_value_unset( &gvalue
);
1411 wxVariant value
= tmp
;
1412 if (!cell
->Validate( value
))
1415 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1417 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1419 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, gtk_path
);
1420 wxDataViewItem
item( (void*) iter
.user_data
);;
1421 gtk_tree_path_free( gtk_path
);
1423 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1425 model
->SetValue( value
, item
, model_col
);
1426 model
->ValueChanged( item
, model_col
);
1429 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1431 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1432 wxDataViewCellMode mode
, int align
) :
1433 wxDataViewRenderer( varianttype
, mode
, align
)
1435 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
1437 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1439 g_signal_connect_after( m_renderer
, "toggled",
1440 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1444 GValue gvalue
= { 0, };
1445 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1446 g_value_set_boolean( &gvalue
, false );
1447 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1448 g_value_unset( &gvalue
);
1452 SetAlignment(align
);
1455 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1459 GValue gvalue
= { 0, };
1460 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1461 g_value_set_boolean( &gvalue
, tmp
);
1462 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1463 g_value_unset( &gvalue
);
1468 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1470 GValue gvalue
= { 0, };
1471 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1472 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1473 bool tmp
= g_value_get_boolean( &gvalue
);
1474 g_value_unset( &gvalue
);
1481 // ---------------------------------------------------------
1482 // wxDataViewCustomRenderer
1483 // ---------------------------------------------------------
1485 class wxDataViewCtrlDC
: public wxWindowDC
1488 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1490 GtkWidget
*widget
= window
->m_treeview
;
1494 m_context
= window
->GtkGetPangoDefaultContext();
1495 m_layout
= pango_layout_new( m_context
);
1496 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1498 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1500 // Set m_window later
1502 // m_owner = window;
1506 // ---------------------------------------------------------
1507 // wxDataViewCustomRenderer
1508 // ---------------------------------------------------------
1510 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1512 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1513 wxDataViewCellMode mode
, int align
,
1515 wxDataViewRenderer( varianttype
, mode
, align
)
1525 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1527 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1528 renderer
->cell
= this;
1530 m_renderer
= (GtkCellRenderer
*) renderer
;
1533 SetAlignment(align
);
1538 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1544 wxDC
*wxDataViewCustomRenderer::GetDC()
1548 if (GetOwner() == NULL
)
1550 if (GetOwner()->GetOwner() == NULL
)
1552 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1558 // ---------------------------------------------------------
1559 // wxDataViewProgressRenderer
1560 // ---------------------------------------------------------
1562 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1564 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1565 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1566 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1572 if (!gtk_check_version(2,6,0))
1574 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
1576 GValue gvalue
= { 0, };
1577 g_value_init( &gvalue
, G_TYPE_STRING
);
1579 // FIXME: font encoding support
1580 g_value_set_string( &gvalue
, wxGTK_CONV_SYS(m_label
) );
1581 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1582 g_value_unset( &gvalue
);
1585 SetAlignment(align
);
1590 // Use custom cell code
1591 wxDataViewCustomRenderer::Init(mode
, align
);
1595 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1599 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1602 if (!gtk_check_version(2,6,0))
1604 gint tmp
= (long) value
;
1605 GValue gvalue
= { 0, };
1606 g_value_init( &gvalue
, G_TYPE_INT
);
1607 g_value_set_int( &gvalue
, tmp
);
1608 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1609 g_value_unset( &gvalue
);
1614 m_value
= (long) value
;
1616 if (m_value
< 0) m_value
= 0;
1617 if (m_value
> 100) m_value
= 100;
1623 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1628 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1630 double pct
= (double)m_value
/ 100.0;
1632 bar
.width
= (int)(cell
.width
* pct
);
1633 dc
->SetPen( *wxTRANSPARENT_PEN
);
1634 dc
->SetBrush( *wxBLUE_BRUSH
);
1635 dc
->DrawRectangle( bar
);
1637 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1638 dc
->SetPen( *wxBLACK_PEN
);
1639 dc
->DrawRectangle( cell
);
1644 wxSize
wxDataViewProgressRenderer::GetSize() const
1646 return wxSize(40,12);
1649 // ---------------------------------------------------------
1650 // wxDataViewDateRenderer
1651 // ---------------------------------------------------------
1653 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1656 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1657 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
1658 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1663 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1664 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1665 sizer
->Add( m_cal
, 1, wxGROW
);
1670 virtual void OnDismiss()
1674 void OnCalendar( wxCalendarEvent
&event
);
1676 wxCalendarCtrl
*m_cal
;
1677 wxDataViewModel
*m_model
;
1678 wxDataViewItem m_item
;
1682 DECLARE_EVENT_TABLE()
1685 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1686 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1689 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1691 wxDateTime date
= event
.GetDate();
1692 wxVariant value
= date
;
1693 m_model
->SetValue( value
, m_item
, m_col
);
1694 m_model
->ValueChanged( m_item
, m_col
);
1698 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1700 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1701 wxDataViewCellMode mode
, int align
) :
1702 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1705 SetAlignment(align
);
1708 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1710 m_date
= value
.GetDateTime();
1715 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1720 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1722 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1723 wxString tmp
= m_date
.FormatDate();
1724 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1729 wxSize
wxDataViewDateRenderer::GetSize() const
1731 wxString tmp
= m_date
.FormatDate();
1733 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1734 return wxSize(x
,y
+d
);
1737 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewModel
*model
,
1738 const wxDataViewItem
&item
, unsigned int col
)
1741 model
->GetValue( variant
, item
, col
);
1742 wxDateTime value
= variant
.GetDateTime();
1744 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1745 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1746 wxPoint pos
= wxGetMousePosition();
1749 popup
->Popup( popup
->m_cal
);
1754 // ---------------------------------------------------------
1756 // ---------------------------------------------------------
1760 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
1761 GdkEventButton
*gdk_event
,
1762 wxDataViewColumn
*column
)
1764 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
1767 if (gdk_event
->button
== 1)
1769 wxDataViewCtrl
*dv
= column
->GetOwner();
1770 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
1771 event
.SetDataViewColumn( column
);
1772 event
.SetModel( dv
->GetModel() );
1773 if (dv
->GetEventHandler()->ProcessEvent( event
))
1781 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1782 GtkCellRenderer
*cell
,
1783 GtkTreeModel
*model
,
1789 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1790 GtkCellRenderer
*renderer
,
1791 GtkTreeModel
*model
,
1795 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
1796 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
1798 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
1800 wxDataViewItem
item( (void*) iter
->user_data
);
1803 tree_model
->internal
->GetDataViewModel()->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
1805 if (value
.GetType() != cell
->GetVariantType())
1806 wxLogError( wxT("Wrong type, required: %s but: %s"),
1807 value
.GetType().c_str(),
1808 cell
->GetVariantType().c_str() );
1810 cell
->SetValue( value
);
1813 wxListItemAttr attr
;
1814 tree_model
->model
->GetAttr( attr
, cell
->GetOwner()->GetModelColumn(), model_row
);
1816 if (attr
.HasBackgroundColour())
1818 wxColour colour
= attr
.GetBackgroundColour();
1819 const GdkColor
* const gcol
= colour
.GetColor();
1821 GValue gvalue
= { 0, };
1822 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
1823 g_value_set_boxed( &gvalue
, gcol
);
1824 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
1825 g_value_unset( &gvalue
);
1829 GValue gvalue
= { 0, };
1830 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1831 g_value_set_boolean( &gvalue
, FALSE
);
1832 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
1833 g_value_unset( &gvalue
);
1839 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1841 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1842 unsigned int model_column
, int width
,
1843 wxAlignment align
, int flags
) :
1844 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1846 Init( align
, flags
, width
);
1848 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
1852 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1853 unsigned int model_column
, int width
,
1854 wxAlignment align
, int flags
) :
1855 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1857 Init( align
, flags
, width
);
1859 SetBitmap( bitmap
);
1862 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
1864 m_isConnected
= false;
1866 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
1867 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1868 m_column
= (GtkWidget
*) column
;
1871 SetAlignment( align
);
1873 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
1874 // as GTK+ is smart and unless explicitely told, will set the minimal
1875 // width to the title's lenght, which is a better default
1877 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
1878 // that we use for the wxDataViewCtrl
1879 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1880 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
1882 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
1884 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1885 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
1888 wxDataViewColumn::~wxDataViewColumn()
1892 void wxDataViewColumn::OnInternalIdle()
1897 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
1899 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1902 g_signal_connect(column
->button
, "button_press_event",
1903 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
1905 m_isConnected
= true;
1910 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
1912 wxDataViewColumnBase::SetOwner( owner
);
1914 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1916 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
1919 void wxDataViewColumn::SetTitle( const wxString
&title
)
1921 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1925 // disconnect before column->button gets recreated
1926 g_signal_handlers_disconnect_by_func( column
->button
,
1927 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
1929 m_isConnected
= false;
1932 // FIXME: can it really happen that we don't have the owner here??
1933 wxDataViewCtrl
*ctrl
= GetOwner();
1934 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
1935 : wxGTK_CONV_SYS(title
) );
1937 gtk_tree_view_column_set_widget( column
, NULL
);
1940 wxString
wxDataViewColumn::GetTitle() const
1942 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
1943 return wxConvFileName
->cMB2WX(str
);
1946 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
1948 wxDataViewColumnBase::SetBitmap( bitmap
);
1950 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1953 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
1955 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1956 if (bitmap
.GetMask())
1957 mask
= bitmap
.GetMask()->GetBitmap();
1959 if (bitmap
.HasPixbuf())
1961 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
1962 bitmap
.GetPixbuf());
1966 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
1967 bitmap
.GetPixmap(), mask
);
1969 gtk_widget_show( GTK_WIDGET(gtk_image
) );
1971 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
1975 gtk_tree_view_column_set_widget( column
, NULL
);
1979 void wxDataViewColumn::SetHidden( bool hidden
)
1981 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
1984 void wxDataViewColumn::SetResizeable( bool resizeable
)
1986 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
1989 void wxDataViewColumn::SetAlignment( wxAlignment align
)
1991 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1993 gfloat xalign
= 0.0;
1994 if (align
== wxALIGN_RIGHT
)
1996 if (align
== wxALIGN_CENTER_HORIZONTAL
||
1997 align
== wxALIGN_CENTER
)
2000 gtk_tree_view_column_set_alignment( column
, xalign
);
2003 wxAlignment
wxDataViewColumn::GetAlignment() const
2005 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
2008 return wxALIGN_RIGHT
;
2010 return wxALIGN_CENTER_HORIZONTAL
;
2012 return wxALIGN_LEFT
;
2015 void wxDataViewColumn::SetSortable( bool sortable
)
2017 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2020 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
2022 gtk_tree_view_column_set_sort_column_id( column
, -1 );
2025 bool wxDataViewColumn::IsSortable() const
2027 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2028 return (gtk_tree_view_column_get_sort_column_id( column
) != -1);
2031 bool wxDataViewColumn::IsResizeable() const
2033 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2034 return gtk_tree_view_column_get_resizable( column
);
2037 bool wxDataViewColumn::IsHidden() const
2039 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2040 return !gtk_tree_view_column_get_visible( column
);
2043 void wxDataViewColumn::SetSortOrder( bool ascending
)
2045 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2048 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
2050 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
2053 bool wxDataViewColumn::IsSortOrderAscending() const
2055 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2057 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
2060 void wxDataViewColumn::SetMinWidth( int width
)
2062 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2065 int wxDataViewColumn::GetMinWidth() const
2067 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2070 int wxDataViewColumn::GetWidth() const
2072 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2075 void wxDataViewColumn::SetWidth( int width
)
2077 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2081 //-----------------------------------------------------------------------------
2082 // wxGtkTreeModelNode
2083 //-----------------------------------------------------------------------------
2085 void wxGtkTreeModelNode::Resort()
2087 wxGtkTreeModelNodes
*new_array
= new wxGtkTreeModelNodes( wxGtkTreeModelNodeCmp
);
2090 size_t count
= m_children
->GetCount();
2092 for (pos
= 0; pos
< count
; pos
++)
2094 new_array
->Add( m_children
->Item( 0 ) );
2095 m_children
->RemoveAt( 0 );
2099 m_children
= new_array
;
2101 for (pos
= 0; pos
< count
; pos
++)
2103 wxGtkTreeModelNode
*node
= m_children
->Item( pos
);
2108 //-----------------------------------------------------------------------------
2109 // wxDataViewCtrlInternal
2110 //-----------------------------------------------------------------------------
2112 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
,
2113 wxDataViewModel
*wx_model
, GtkWxTreeModel
*gtk_model
)
2116 m_wx_model
= wx_model
;
2117 m_gtk_model
= gtk_model
;
2122 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2124 g_object_unref( m_gtk_model
);
2127 void wxDataViewCtrlInternal::InitTree()
2129 wxDataViewItem item
;
2130 m_root
= new wxGtkTreeModelNode( NULL
, item
, m_wx_model
);
2132 BuildBranch( m_root
);
2135 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
2137 if (node
->GetChildCount() == 0)
2139 wxDataViewItem child
= m_wx_model
->GetFirstChild( node
->GetItem() );
2140 while (child
.IsOk())
2142 node
->Add( new wxGtkTreeModelNode( node
, child
, node
->GetModel() ) );
2143 child
= m_wx_model
->GetNextSibling( child
);
2148 void wxDataViewCtrlInternal::Resort()
2153 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2155 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2156 parent_node
->Add( new wxGtkTreeModelNode( parent_node
, item
, parent_node
->GetModel() ) );
2160 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&item
)
2162 wxGtkTreeModelNode
*node
= FindNode( item
);
2163 wxGtkTreeModelNode
*parent
= node
->GetParent();
2165 for (pos
= 0; pos
< parent
->GetChildren().GetCount(); pos
++)
2167 if (node
== parent
->GetChildren().Item( pos
))
2169 parent
->GetChildren().RemoveAt( pos
);
2177 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
2179 int depth
= gtk_tree_path_get_depth( path
);
2181 wxGtkTreeModelNode
*node
= m_root
;
2184 for (i
= 0; i
< depth
; i
++)
2186 BuildBranch( node
);
2188 gint pos
= gtk_tree_path_get_indices (path
)[i
];
2189 if (pos
< 0) return FALSE
;
2190 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
2192 node
= node
->GetChildren().Item( (size_t) pos
);
2195 iter
->stamp
= m_gtk_model
->stamp
;
2196 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2201 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
2203 GtkTreePath
*retval
= gtk_tree_path_new ();
2205 wxGtkTreeModelNode
*node
= FindNode( iter
);
2206 while (node
->GetParent())
2208 wxGtkTreeModelNode
*parent
= node
->GetParent();
2209 int pos
= parent
->GetChildren().Index( node
);
2211 gtk_tree_path_prepend_index( retval
, pos
);
2219 GtkTreePath
*wxDataViewCtrlInternal::get_path_safe( GtkTreeIter
*iter
)
2221 GtkTreePath
*retval
= gtk_tree_path_new ();
2223 wxGtkTreeModelNode
*node
= FindNode( iter
);
2224 while (node
->GetParent())
2226 wxGtkTreeModelNode
*parent
= node
->GetParent();
2228 for (pos
= 0; pos
< parent
->GetChildren().GetCount(); pos
++)
2230 if (node
== parent
->GetChildren().Item( pos
))
2232 gtk_tree_path_prepend_index( retval
, (int) pos
);
2243 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
2245 wxGtkTreeModelNode
*node
= FindNode( iter
);
2246 wxGtkTreeModelNode
*parent
= node
->GetParent();
2247 unsigned int pos
= parent
->GetChildren().Index( node
);
2249 if (pos
== parent
->GetChildren().GetCount()-1)
2252 node
= parent
->GetChildren().Item( pos
+1 );
2254 iter
->stamp
= m_gtk_model
->stamp
;
2255 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2260 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
2262 wxDataViewItem
item( (void*) parent
->user_data
);
2264 if (!m_wx_model
->HasChildren( item
))
2267 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2268 BuildBranch( parent_node
);
2270 wxGtkTreeModelNode
*first_child_node
= parent_node
->GetChildren().Item( 0 );
2272 iter
->stamp
= m_gtk_model
->stamp
;
2273 iter
->user_data
= (gpointer
) first_child_node
->GetItem().GetID();
2278 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
2280 wxDataViewItem
item( (void*) iter
->user_data
);
2282 return m_wx_model
->HasChildren( item
);
2285 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
2287 wxDataViewItem
item( (void*) iter
->user_data
);
2289 if (!m_wx_model
->HasChildren( item
))
2292 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
2293 BuildBranch( parent_node
);
2295 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2297 return parent_node
->GetChildCount();
2300 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
2303 if (parent
) id
= (void*) parent
->user_data
;
2304 wxDataViewItem
item( id
);
2306 if (!m_wx_model
->HasChildren( item
))
2309 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2310 BuildBranch( parent_node
);
2312 wxGtkTreeModelNode
*child_node
= parent_node
->GetChildren().Item( (size_t) n
);
2316 // wxPrintf( "iter_nth_child %d\n", n );
2318 iter
->stamp
= m_gtk_model
->stamp
;
2319 iter
->user_data
= (gpointer
) child_node
->GetItem().GetID();
2324 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
2326 wxDataViewItem
item( (void*) child
->user_data
);
2328 wxGtkTreeModelNode
*node
= FindNode( child
);
2329 node
= node
->GetParent();
2333 iter
->stamp
= m_gtk_model
->stamp
;
2334 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2339 static wxGtkTreeModelNode
*
2340 wxDataViewCtrlInternal_FindNode( wxGtkTreeModelNode
*node
, const wxDataViewItem
&item
)
2342 if (!node
) return NULL
;
2344 size_t count
= node
->GetChildCount();
2346 for (i
= 0; i
< count
; i
++)
2348 wxGtkTreeModelNode
*child
= node
->GetChildren().Item( i
);
2349 if (child
->GetItem().GetID() == item
.GetID())
2351 // wxPrintf( "leave findnode at %d\n", i );
2355 wxGtkTreeModelNode
*node2
= wxDataViewCtrlInternal_FindNode( child
, item
);
2358 // wxPrintf( "branch findnode at %d\n", i );
2366 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
2371 wxDataViewItem
item( (void*) iter
->user_data
);
2373 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_root
, item
);
2377 wxPrintf( "Not found %d\n", (int) iter
->user_data
);
2385 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
2387 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_root
, item
);
2391 wxPrintf( "Not found %d\n", (int) item
.GetID() );
2399 //-----------------------------------------------------------------------------
2400 // wxDataViewCtrl signal callbacks
2401 //-----------------------------------------------------------------------------
2404 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
2406 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
2409 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
, dv
->GetId() );
2411 event
.SetModel( dv
->GetModel() );
2412 dv
->GetEventHandler()->ProcessEvent( event
);
2416 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
2417 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
2419 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
2422 dv
->GtkGetInternal()->get_iter( &iter
, path
);
2423 wxDataViewItem
item( (void*) iter
.user_data
);;
2424 event
.SetItem( item
);
2425 event
.SetModel( dv
->GetModel() );
2426 dv
->GetEventHandler()->ProcessEvent( event
);
2429 //-----------------------------------------------------------------------------
2431 //-----------------------------------------------------------------------------
2433 //-----------------------------------------------------------------------------
2434 // InsertChild for wxDataViewCtrl
2435 //-----------------------------------------------------------------------------
2437 static void wxInsertChildInDataViewCtrl( wxWindowGTK
* parent
, wxWindowGTK
* child
)
2439 wxDataViewCtrl
* dvc
= (wxDataViewCtrl
*) parent
;
2440 GtkWidget
*treeview
= dvc
->GtkGetTreeView();
2442 // Insert widget in GtkTreeView
2443 if (GTK_WIDGET_REALIZED(treeview
))
2444 gtk_widget_set_parent_window( child
->m_widget
,
2445 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
2446 gtk_widget_set_parent( child
->m_widget
, treeview
);
2450 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
2451 GtkAllocation
*alloc
,
2452 wxDataViewCtrl
*win
)
2455 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
2458 wxWindow
*child
= node
->GetData();
2461 gtk_widget_size_request( child
->m_widget
, &req
);
2463 GtkAllocation alloc
;
2464 alloc
.x
= child
->m_x
;
2465 alloc
.y
= child
->m_y
;
2466 alloc
.width
= child
->m_width
;
2467 alloc
.height
= child
->m_height
;
2468 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
2470 node
= node
->GetNext();
2476 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
2478 wxDataViewCtrl::~wxDataViewCtrl()
2481 GetModel()->RemoveNotifier( m_notifier
);
2483 // remove the model from the GtkTreeView before it gets destroyed by the
2484 // wxDataViewCtrlBase's dtor
2485 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
2490 void wxDataViewCtrl::Init()
2495 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
2496 const wxPoint
& pos
, const wxSize
& size
,
2497 long style
, const wxValidator
& validator
)
2501 if (!PreCreation( parent
, pos
, size
) ||
2502 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
2504 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
2508 m_insertCallback
= wxInsertChildInDataViewCtrl
;
2510 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
2512 GtkScrolledWindowSetBorder(m_widget
, style
);
2514 m_treeview
= gtk_tree_view_new();
2515 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
2517 g_signal_connect (m_treeview
, "size_allocate",
2518 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
2521 if (!gtk_check_version(2,6,0))
2522 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
2525 if (style
& wxDV_MULTIPLE
)
2527 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2528 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
2531 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
2534 if (!gtk_check_version(2,10,0))
2536 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
2538 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
2539 (style
& wxDV_VERT_RULES
) != 0)
2540 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
2541 else if (style
& wxDV_VERT_RULES
)
2542 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
2543 else if (style
& wxDV_HORIZ_RULES
)
2544 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
2546 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
2551 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
2554 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
2555 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2556 gtk_widget_show (m_treeview
);
2558 m_parent
->DoAddChild( this );
2562 GtkEnableSelectionEvents();
2564 g_signal_connect_after (m_treeview
, "row_activated",
2565 G_CALLBACK (wxdataview_row_activated_callback
), this);
2570 void wxDataViewCtrl::OnInternalIdle()
2572 wxWindow::OnInternalIdle();
2574 unsigned int cols
= GetColumnCount();
2576 for (i
= 0; i
< cols
; i
++)
2578 wxDataViewColumn
*col
= GetColumn( i
);
2579 col
->OnInternalIdle();
2583 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
2585 if (!wxDataViewCtrlBase::AssociateModel( model
))
2588 GtkWxTreeModel
*gtk_model
= wxgtk_tree_model_new();
2589 m_internal
= new wxDataViewCtrlInternal( this, model
, gtk_model
);
2590 gtk_model
->internal
= m_internal
;
2592 m_notifier
= new wxGtkDataViewModelNotifier( gtk_model
, model
, this );
2594 model
->AddNotifier( m_notifier
);
2596 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_model
) );
2598 // unref in wxDataViewCtrlInternal
2599 // g_object_unref( gtk_model );
2604 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
2606 if (!wxDataViewCtrlBase::AppendColumn(col
))
2609 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
2611 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
2616 wxDataViewItem
wxDataViewCtrl::GetSelection()
2618 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2620 if (m_windowStyle
& wxDV_MULTIPLE
)
2626 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
2628 wxDataViewItem
item( (void*) iter
.user_data
);
2633 return wxDataViewItem(0);
2636 void wxDataViewCtrl::DoSetExpanderColumn()
2640 void wxDataViewCtrl::DoSetIndent()
2644 void wxDataViewCtrl::GtkDisableSelectionEvents()
2646 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2647 g_signal_connect_after (selection
, "changed",
2648 G_CALLBACK (wxdataview_selection_changed_callback
), this);
2651 void wxDataViewCtrl::GtkEnableSelectionEvents()
2653 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2654 g_signal_handlers_disconnect_by_func( selection
,
2655 (gpointer
) (wxdataview_selection_changed_callback
), this);
2660 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2662 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
2667 // !wxUSE_GENERICDATAVIEWCTRL
2670 // wxUSE_DATAVIEWCTRL