1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataview.cpp
3 // Purpose: wxDataViewCtrl GTK+2 implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if wxUSE_DATAVIEWCTRL
15 #include "wx/dataview.h"
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
21 #include "wx/dcclient.h"
25 #include "wx/stockitem.h"
26 #include "wx/calctrl.h"
27 #include "wx/popupwin.h"
30 #include "wx/listimpl.cpp"
33 #include "wx/gtk/private.h"
34 #include "wx/gtk/win_gtk.h"
36 #include <gobject/gvaluecollector.h>
37 #include <gtk/gtktreemodel.h>
38 #include <gtk/gtktreesortable.h>
39 #include <gtk/gtktreednd.h>
41 #include <gdk/gdkkeysyms.h>
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 class wxDataViewCtrlInternal
;
48 wxDataViewCtrlInternal
*g_internal
= NULL
;
50 class wxGtkTreeModelNode
;
53 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
56 //-----------------------------------------------------------------------------
57 // wxDataViewCtrlInternal
58 //-----------------------------------------------------------------------------
60 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
61 WX_DEFINE_LIST(ItemList
);
63 class wxDataViewCtrlInternal
66 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
, GtkWxTreeModel
*owner
);
67 ~wxDataViewCtrlInternal();
69 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
70 GtkTreePath
*get_path( GtkTreeIter
*iter
);
71 GtkTreePath
*get_path_safe( GtkTreeIter
*iter
);
72 gboolean
iter_next( GtkTreeIter
*iter
);
73 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
74 gboolean
iter_has_child( GtkTreeIter
*iter
);
75 gint
iter_n_children( GtkTreeIter
*iter
);
76 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
77 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
79 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
80 wxDataViewCtrl
* GetOwner() { return m_owner
; }
81 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
83 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
84 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
85 bool ItemChanged( const wxDataViewItem
&item
);
86 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
90 void SetSortOrder( GtkSortType sort_order
) { m_sort_order
= sort_order
; }
91 GtkSortType
GetSortOrder() { return m_sort_order
; }
95 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
96 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
97 wxGtkTreeModelNode
*FindParentNode( const wxDataViewItem
&item
);
98 wxGtkTreeModelNode
*FindParentNode( GtkTreeIter
*iter
);
99 void BuildBranch( wxGtkTreeModelNode
*branch
);
102 wxGtkTreeModelNode
*m_root
;
103 wxDataViewModel
*m_wx_model
;
104 GtkWxTreeModel
*m_gtk_model
;
105 wxDataViewCtrl
*m_owner
;
106 GtkSortType m_sort_order
;
110 //-----------------------------------------------------------------------------
111 // wxGtkTreeModelNode
112 //-----------------------------------------------------------------------------
114 int LINKAGEMODE
wxGtkTreeModelNodeCmp( void *id1
, void *id2
);
116 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
117 WX_DEFINE_SORTED_ARRAY( void* , wxGtkTreeModelChildren
);
119 class wxGtkTreeModelNode
122 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
123 wxDataViewCtrlInternal
*internal
)
127 m_internal
= internal
;
128 g_internal
= internal
;
129 m_children
= new wxGtkTreeModelChildren( wxGtkTreeModelNodeCmp
);
132 ~wxGtkTreeModelNode()
134 g_internal
= m_internal
;
135 size_t count
= m_children
->GetCount();
137 for (i
= 0; i
< count
; i
++)
139 wxGtkTreeModelNode
*child
= m_nodes
.Item( i
);
145 unsigned int AddNode( wxGtkTreeModelNode
* child
)
147 g_internal
= m_internal
;
148 m_nodes
.Add( child
);
149 return m_children
->Add( child
->GetItem().GetID() );
152 unsigned int AddLeave( void* id
)
154 g_internal
= m_internal
;
155 return m_children
->Add( id
);
158 void DeleteChild( void* id
)
160 g_internal
= m_internal
;
162 size_t count
= m_children
->GetCount();
163 for (pos
= 0; pos
< count
; pos
++)
165 if (m_children
->Item( pos
) == id
)
167 m_children
->RemoveAt( pos
);
171 count
= m_nodes
.GetCount();
172 for (pos
= 0; pos
< count
; pos
++)
174 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
175 if (node
->GetItem().GetID() == id
)
177 m_nodes
.RemoveAt( pos
);
185 wxGtkTreeModelNode
* GetParent()
187 wxGtkTreeModelNodes
&GetNodes()
189 wxGtkTreeModelChildren
&GetChildren()
190 { return *m_children
; }
192 unsigned int GetChildCount() { return m_children
->GetCount(); }
193 unsigned int GetNodesCount() { return m_nodes
.GetCount(); }
195 wxDataViewItem
&GetItem() { return m_item
; }
196 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
201 wxGtkTreeModelNode
*m_parent
;
202 wxGtkTreeModelNodes m_nodes
;
203 wxGtkTreeModelChildren
*m_children
;
204 wxDataViewItem m_item
;
205 wxDataViewCtrlInternal
*m_internal
;
209 int LINKAGEMODE
wxGtkTreeModelNodeCmp( void* id1
, void* id2
)
211 int ret
= g_internal
->GetDataViewModel()->Compare( id1
, id2
);
212 if (g_internal
->GetSortOrder() == GTK_SORT_DESCENDING
)
217 //-----------------------------------------------------------------------------
219 //-----------------------------------------------------------------------------
221 extern bool g_blockEventsOnDrag
;
223 //-----------------------------------------------------------------------------
224 // define new GTK+ class wxGtkTreeModel
225 //-----------------------------------------------------------------------------
229 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
230 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
231 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
232 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
233 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
234 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
236 GType
gtk_wx_tree_model_get_type (void);
238 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
240 struct _GtkWxTreeModel
246 wxDataViewCtrlInternal
*internal
;
249 struct _GtkWxTreeModelClass
251 GObjectClass list_parent_class
;
254 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
255 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
256 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
257 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
258 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
259 static void wxgtk_tree_model_finalize (GObject
*object
);
260 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
261 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
262 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
264 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
267 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
269 static GtkTreePath
*wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
271 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
275 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
277 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
279 GtkTreeIter
*parent
);
280 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
282 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
284 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
288 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
293 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
294 gint
*sort_column_id
,
296 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
299 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
301 GtkTreeIterCompareFunc func
,
303 GtkDestroyNotify destroy
);
304 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
305 GtkTreeIterCompareFunc func
,
307 GtkDestroyNotify destroy
);
308 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
312 static GObjectClass
*list_parent_class
= NULL
;
315 gtk_wx_tree_model_get_type (void)
317 static GType tree_model_type
= 0;
319 if (!tree_model_type
)
321 const GTypeInfo tree_model_info
=
323 sizeof (GtkWxTreeModelClass
),
324 NULL
, /* base_init */
325 NULL
, /* base_finalize */
326 (GClassInitFunc
) wxgtk_tree_model_class_init
,
327 NULL
, /* class_finalize */
328 NULL
, /* class_data */
329 sizeof (GtkWxTreeModel
),
331 (GInstanceInitFunc
) wxgtk_tree_model_init
,
334 static const GInterfaceInfo tree_model_iface_info
=
336 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
341 static const GInterfaceInfo sortable_iface_info
=
343 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
348 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
349 &tree_model_info
, (GTypeFlags
)0 );
351 g_type_add_interface_static (tree_model_type
,
353 &tree_model_iface_info
);
354 g_type_add_interface_static (tree_model_type
,
355 GTK_TYPE_TREE_SORTABLE
,
356 &sortable_iface_info
);
359 return tree_model_type
;
362 static GtkWxTreeModel
*
363 wxgtk_tree_model_new(void)
365 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
370 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
372 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
373 GObjectClass
*object_class
= (GObjectClass
*) klass
;
374 object_class
->finalize
= wxgtk_tree_model_finalize
;
378 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
380 iface
->get_flags
= wxgtk_tree_model_get_flags
;
381 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
382 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
383 iface
->get_iter
= wxgtk_tree_model_get_iter
;
384 iface
->get_path
= wxgtk_tree_model_get_path
;
385 iface
->get_value
= wxgtk_tree_model_get_value
;
386 iface
->iter_next
= wxgtk_tree_model_iter_next
;
387 iface
->iter_children
= wxgtk_tree_model_iter_children
;
388 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
389 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
390 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
391 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
395 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
397 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
398 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
399 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
400 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
401 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
405 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
407 tree_model
->internal
= NULL
;
408 tree_model
->stamp
= g_random_int();
412 wxgtk_tree_model_finalize (GObject
*object
)
415 (* list_parent_class
->finalize
) (object
);
420 //-----------------------------------------------------------------------------
421 // implement callbacks from wxGtkTreeModel class by letting
422 // them call the methods of wxWidgets' wxDataViewModel
423 //-----------------------------------------------------------------------------
425 static GtkTreeModelFlags
426 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
428 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model
), (GtkTreeModelFlags
)0 );
430 return GTK_TREE_MODEL_ITERS_PERSIST
;
434 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
436 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
437 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
439 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
443 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
446 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
447 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
449 GType gtype
= G_TYPE_INVALID
;
451 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
453 if (wxtype
== wxT("string"))
454 gtype
= G_TYPE_STRING
;
457 wxFAIL_MSG( _T("non-string columns not supported yet") );
464 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
468 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
469 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
470 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
472 return wxtree_model
->internal
->get_iter( iter
, path
);
476 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
479 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
480 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
481 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
483 return wxtree_model
->internal
->get_path( iter
);
487 wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
490 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
491 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
492 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
494 return wxtree_model
->internal
->get_path_safe( iter
);
498 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
503 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
504 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
506 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
507 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
508 if (mtype
== wxT("string"))
511 g_value_init( value
, G_TYPE_STRING
);
512 wxDataViewItem
item( (void*) iter
->user_data
);
513 model
->GetValue( variant
, item
, (unsigned int) column
);
515 g_value_set_string( value
, variant
.GetString().utf8_str() );
519 wxFAIL_MSG( _T("non-string columns not supported yet") );
524 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
527 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
528 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
529 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
531 return wxtree_model
->internal
->iter_next( iter
);
535 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
539 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
540 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
541 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
543 return wxtree_model
->internal
->iter_children( iter
, parent
);
547 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
550 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
551 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
552 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
554 return wxtree_model
->internal
->iter_has_child( iter
);
558 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
561 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
562 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
563 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, 0);
565 return wxtree_model
->internal
->iter_n_children( iter
);
569 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
574 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
575 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
577 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
581 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
585 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
586 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
587 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
589 return wxtree_model
->internal
->iter_parent( iter
, child
);
593 gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
594 gint
*sort_column_id
,
597 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
599 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
602 *sort_column_id
= tree_model
->internal
->GetDataViewModel()->GetSortingColumn();
605 *order
= tree_model
->internal
->GetSortOrder();
610 void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
614 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
615 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
617 if ((sort_column_id
== (gint
) tree_model
->internal
->GetDataViewModel()->GetSortingColumn()) &&
618 (order
== tree_model
->internal
->GetSortOrder()))
621 tree_model
->internal
->GetDataViewModel()->SetSortingColumn( sort_column_id
);
623 tree_model
->internal
->SetSortOrder( order
);
625 gtk_tree_sortable_sort_column_changed (sortable
);
627 tree_model
->internal
->GetDataViewModel()->Resort();
630 void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
632 GtkTreeIterCompareFunc func
,
634 GtkDestroyNotify destroy
)
636 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
637 g_return_if_fail (func
!= NULL
);
640 void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
641 GtkTreeIterCompareFunc func
,
643 GtkDestroyNotify destroy
)
645 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
646 g_return_if_fail (func
!= NULL
);
648 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
651 gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
656 //-----------------------------------------------------------------------------
657 // define new GTK+ class wxGtkRendererRenderer
658 //-----------------------------------------------------------------------------
662 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
663 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
664 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
665 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
666 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
667 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
669 GType
gtk_wx_cell_renderer_get_type (void);
671 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
672 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
674 struct _GtkWxCellRenderer
676 GtkCellRenderer parent
;
679 wxDataViewCustomRenderer
*cell
;
683 struct _GtkWxCellRendererClass
685 GtkCellRendererClass cell_parent_class
;
689 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
690 static void gtk_wx_cell_renderer_init (
691 GtkWxCellRenderer
*cell
);
692 static void gtk_wx_cell_renderer_class_init(
693 GtkWxCellRendererClass
*klass
);
694 static void gtk_wx_cell_renderer_finalize (
696 static void gtk_wx_cell_renderer_get_size (
697 GtkCellRenderer
*cell
,
699 GdkRectangle
*rectangle
,
704 static void gtk_wx_cell_renderer_render (
705 GtkCellRenderer
*cell
,
708 GdkRectangle
*background_area
,
709 GdkRectangle
*cell_area
,
710 GdkRectangle
*expose_area
,
711 GtkCellRendererState flags
);
712 static gboolean
gtk_wx_cell_renderer_activate(
713 GtkCellRenderer
*cell
,
717 GdkRectangle
*background_area
,
718 GdkRectangle
*cell_area
,
719 GtkCellRendererState flags
);
720 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
721 GtkCellRenderer
*cell
,
725 GdkRectangle
*background_area
,
726 GdkRectangle
*cell_area
,
727 GtkCellRendererState flags
);
730 static GObjectClass
*cell_parent_class
= NULL
;
735 gtk_wx_cell_renderer_get_type (void)
737 static GType cell_wx_type
= 0;
741 const GTypeInfo cell_wx_info
=
743 sizeof (GtkWxCellRendererClass
),
744 NULL
, /* base_init */
745 NULL
, /* base_finalize */
746 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
747 NULL
, /* class_finalize */
748 NULL
, /* class_data */
749 sizeof (GtkWxCellRenderer
),
751 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
754 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
755 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
762 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
765 cell
->last_click
= 0;
769 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
771 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
772 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
774 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
776 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
778 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
779 cell_class
->render
= gtk_wx_cell_renderer_render
;
780 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
781 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
785 gtk_wx_cell_renderer_finalize (GObject
*object
)
788 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
792 gtk_wx_cell_renderer_new (void)
794 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
799 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
800 GtkCellRenderer
*renderer
,
804 GdkRectangle
*background_area
,
805 GdkRectangle
*cell_area
,
806 GtkCellRendererState flags
)
808 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
809 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
810 if (!cell
->HasEditorCtrl())
814 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
820 rect
.x
+= cell_area
->x
;
821 rect
.y
+= cell_area
->y
;
822 // rect.width -= renderer->xpad * 2;
823 // rect.height -= renderer->ypad * 2;
825 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
826 wxRect
renderrect( cell_area
->x
, cell_area
->y
, cell_area
->width
, cell_area
->height
);
828 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
830 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
831 wxDataViewItem
item( (void*) iter
.user_data
);
832 gtk_tree_path_free( treepath
);
834 cell
->StartEditing( item
, renderrect
);
840 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
842 GdkRectangle
*cell_area
,
848 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
849 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
851 wxSize size
= cell
->GetSize();
853 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
854 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
861 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
865 *x_offset
= (gint
)((renderer
->xalign
*
866 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
867 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
871 *y_offset
= (gint
)((renderer
->yalign
*
872 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
873 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
881 *height
= calc_height
;
885 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
888 GdkRectangle
*background_area
,
889 GdkRectangle
*cell_area
,
890 GdkRectangle
*expose_area
,
891 GtkCellRendererState flags
)
894 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
895 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
898 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
904 rect
.x
+= cell_area
->x
;
905 rect
.y
+= cell_area
->y
;
906 rect
.width
-= renderer
->xpad
* 2;
907 rect
.height
-= renderer
->ypad
* 2;
910 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
912 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
913 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
914 if (dc
->m_window
== NULL
)
916 dc
->m_window
= window
;
921 if (flags
& GTK_CELL_RENDERER_SELECTED
)
922 state
|= wxDATAVIEW_CELL_SELECTED
;
923 if (flags
& GTK_CELL_RENDERER_PRELIT
)
924 state
|= wxDATAVIEW_CELL_PRELIT
;
925 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
926 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
927 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
928 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
929 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
930 state
|= wxDATAVIEW_CELL_FOCUSED
;
931 cell
->Render( renderrect
, dc
, state
);
936 gtk_wx_cell_renderer_activate(
937 GtkCellRenderer
*renderer
,
941 GdkRectangle
*background_area
,
942 GdkRectangle
*cell_area
,
943 GtkCellRendererState flags
)
945 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
946 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
949 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
955 rect
.x
+= cell_area
->x
;
956 rect
.y
+= cell_area
->y
;
957 rect
.width
-= renderer
->xpad
* 2;
958 rect
.height
-= renderer
->ypad
* 2;
960 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
962 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
964 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
967 gtk_tree_path_free( treepath
);
969 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
975 // activated by <ENTER>
976 if (cell
->Activate( renderrect
, model
, item
, model_col
))
981 else if (event
->type
== GDK_BUTTON_PRESS
)
983 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
984 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
985 ((int) button_event
->y
) - renderrect
.y
);
988 if (button_event
->button
== 1)
990 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
992 // TODO: query system double-click time
993 if (button_event
->time
- wxrenderer
->last_click
< 400)
994 if (cell
->Activate( renderrect
, model
, item
, model_col
))
997 if (button_event
->button
== 3)
999 if (cell
->RightClick( pt
, renderrect
, model
, item
, model_col
))
1003 wxrenderer
->last_click
= button_event
->time
;
1011 // ---------------------------------------------------------
1012 // wxGtkDataViewModelNotifier
1013 // ---------------------------------------------------------
1015 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
1018 wxGtkDataViewModelNotifier( GtkWxTreeModel
*wxgtk_model
,
1019 wxDataViewModel
*wx_model
,
1020 wxDataViewCtrl
*ctrl
);
1021 ~wxGtkDataViewModelNotifier();
1023 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1024 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
1025 virtual bool ItemChanged( const wxDataViewItem
&item
);
1026 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
1027 virtual bool Cleared();
1028 virtual void Resort();
1030 GtkWxTreeModel
*m_wxgtk_model
;
1031 wxDataViewModel
*m_wx_model
;
1032 wxDataViewCtrl
*m_owner
;
1035 // ---------------------------------------------------------
1036 // wxGtkDataViewListModelNotifier
1037 // ---------------------------------------------------------
1039 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1040 GtkWxTreeModel
* wxgtk_model
, wxDataViewModel
*wx_model
,
1041 wxDataViewCtrl
*ctrl
)
1043 m_wxgtk_model
= wxgtk_model
;
1044 m_wx_model
= wx_model
;
1048 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1051 m_wxgtk_model
= NULL
;
1054 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1056 m_owner
->GtkGetInternal()->ItemAdded( parent
, item
);
1059 iter
.stamp
= m_wxgtk_model
->stamp
;
1060 iter
.user_data
= (gpointer
) item
.GetID();
1062 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1063 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1064 gtk_tree_model_row_inserted(
1065 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1066 gtk_tree_path_free (path
);
1071 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1074 iter
.stamp
= m_wxgtk_model
->stamp
;
1075 iter
.user_data
= (gpointer
) item
.GetID();
1077 GtkTreePath
*path
= wxgtk_tree_model_get_path_safe(
1078 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1079 gtk_tree_model_row_deleted(
1080 GTK_TREE_MODEL(m_wxgtk_model
), path
);
1081 gtk_tree_path_free (path
);
1083 m_owner
->GtkGetInternal()->ItemDeleted( parent
, item
);
1088 void wxGtkDataViewModelNotifier::Resort()
1090 m_owner
->GtkGetInternal()->Resort();
1093 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1096 iter
.stamp
= m_wxgtk_model
->stamp
;
1097 iter
.user_data
= (gpointer
) item
.GetID();
1099 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1100 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1101 gtk_tree_model_row_changed(
1102 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1103 gtk_tree_path_free (path
);
1105 m_owner
->GtkGetInternal()->ItemChanged( item
);
1110 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_col
)
1112 // This adds GTK+'s missing MVC logic for ValueChanged
1114 for (index
= 0; index
< m_owner
->GetColumnCount(); index
++)
1116 wxDataViewColumn
*column
= m_owner
->GetColumn( index
);
1117 if (column
->GetModelColumn() == model_col
)
1119 GtkTreeView
*widget
= GTK_TREE_VIEW(m_owner
->m_treeview
);
1120 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1124 iter
.stamp
= m_wxgtk_model
->stamp
;
1125 iter
.user_data
= (gpointer
) item
.GetID();
1126 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1127 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1128 GdkRectangle cell_area
;
1129 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1130 gtk_tree_path_free( path
);
1132 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1133 double d
= gtk_adjustment_get_value( hadjust
);
1134 int xdiff
= (int) d
;
1136 int ydiff
= gcolumn
->button
->allocation
.height
;
1138 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1139 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1141 m_owner
->GtkGetInternal()->ValueChanged( item
, model_col
);
1150 bool wxGtkDataViewModelNotifier::Cleared()
1152 // TODO: delete everything
1154 m_owner
->GtkGetInternal()->Cleared();
1159 // ---------------------------------------------------------
1160 // wxDataViewRenderer
1161 // ---------------------------------------------------------
1163 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1165 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1167 wxDataViewRendererBase( varianttype
, mode
, align
)
1171 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1172 // after the m_renderer pointer has been initialized
1175 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1177 GtkCellRendererMode gtkMode
;
1180 case wxDATAVIEW_CELL_INERT
:
1181 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1183 case wxDATAVIEW_CELL_ACTIVATABLE
:
1184 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1186 case wxDATAVIEW_CELL_EDITABLE
:
1187 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1191 GValue gvalue
= { 0, };
1192 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1193 g_value_set_enum( &gvalue
, gtkMode
);
1194 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1195 g_value_unset( &gvalue
);
1198 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1200 wxDataViewCellMode ret
;
1203 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1205 switch (g_value_get_enum(&gvalue
))
1207 case GTK_CELL_RENDERER_MODE_INERT
:
1208 ret
= wxDATAVIEW_CELL_INERT
;
1210 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1211 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1213 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1214 ret
= wxDATAVIEW_CELL_EDITABLE
;
1218 g_value_unset( &gvalue
);
1223 void wxDataViewRenderer::SetAlignment( int align
)
1225 // horizontal alignment:
1227 gfloat xalign
= 0.0;
1228 if (align
& wxALIGN_RIGHT
)
1230 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1233 GValue gvalue
= { 0, };
1234 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1235 g_value_set_float( &gvalue
, xalign
);
1236 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1237 g_value_unset( &gvalue
);
1239 // vertical alignment:
1241 gfloat yalign
= 0.0;
1242 if (align
& wxALIGN_BOTTOM
)
1244 else if (align
& wxALIGN_CENTER_VERTICAL
)
1247 GValue gvalue2
= { 0, };
1248 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1249 g_value_set_float( &gvalue2
, yalign
);
1250 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1251 g_value_unset( &gvalue2
);
1254 int wxDataViewRenderer::GetAlignment() const
1259 // horizontal alignment:
1261 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
1262 float xalign
= g_value_get_float( &gvalue
);
1264 ret
|= wxALIGN_LEFT
;
1265 else if (xalign
== 0.5)
1266 ret
|= wxALIGN_CENTER_HORIZONTAL
;
1268 ret
|= wxALIGN_RIGHT
;
1269 g_value_unset( &gvalue
);
1272 // vertical alignment:
1274 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
1275 float yalign
= g_value_get_float( &gvalue
);
1278 else if (yalign
== 0.5)
1279 ret
|= wxALIGN_CENTER_VERTICAL
;
1281 ret
|= wxALIGN_BOTTOM
;
1282 g_value_unset( &gvalue
);
1289 // ---------------------------------------------------------
1290 // wxDataViewTextRenderer
1291 // ---------------------------------------------------------
1294 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1295 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1298 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1299 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1301 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1303 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1304 wxVariant value
= tmp
;
1305 if (!cell
->Validate( value
))
1308 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1310 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1312 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, path
);
1313 wxDataViewItem
item( (void*) iter
.user_data
);;
1314 gtk_tree_path_free( path
);
1316 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1318 model
->SetValue( value
, item
, model_col
);
1319 model
->ValueChanged( item
, model_col
);
1322 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1324 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1326 wxDataViewRenderer( varianttype
, mode
, align
)
1328 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_text_new();
1330 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1332 GValue gvalue
= { 0, };
1333 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1334 g_value_set_boolean( &gvalue
, true );
1335 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1336 g_value_unset( &gvalue
);
1338 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1342 SetAlignment(align
);
1345 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1347 wxString tmp
= value
;
1349 GValue gvalue
= { 0, };
1350 g_value_init( &gvalue
, G_TYPE_STRING
);
1351 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( tmp
, GetOwner()->GetOwner()->GetFont() ) );
1352 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1353 g_value_unset( &gvalue
);
1358 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1360 GValue gvalue
= { 0, };
1361 g_value_init( &gvalue
, G_TYPE_STRING
);
1362 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1363 wxString tmp
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
),
1364 wx_const_cast(wxDataViewTextRenderer
*, this)->GetOwner()->GetOwner()->GetFont() );
1365 g_value_unset( &gvalue
);
1372 void wxDataViewTextRenderer::SetAlignment( int align
)
1374 wxDataViewRenderer::SetAlignment(align
);
1376 if (gtk_check_version(2,10,0))
1379 // horizontal alignment:
1380 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1381 if (align
& wxALIGN_RIGHT
)
1382 pangoAlign
= PANGO_ALIGN_RIGHT
;
1383 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1384 pangoAlign
= PANGO_ALIGN_CENTER
;
1386 GValue gvalue
= { 0, };
1387 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1388 g_value_set_enum( &gvalue
, pangoAlign
);
1389 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1390 g_value_unset( &gvalue
);
1393 // ---------------------------------------------------------
1394 // wxDataViewBitmapRenderer
1395 // ---------------------------------------------------------
1397 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1399 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1401 wxDataViewRenderer( varianttype
, mode
, align
)
1403 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_pixbuf_new();
1406 SetAlignment(align
);
1409 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1411 if (value
.GetType() == wxT("wxBitmap"))
1416 // This may create a Pixbuf representation in the
1417 // wxBitmap object (and it will stay there)
1418 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1420 GValue gvalue
= { 0, };
1421 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1422 g_value_set_object( &gvalue
, pixbuf
);
1423 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1424 g_value_unset( &gvalue
);
1429 if (value
.GetType() == wxT("wxIcon"))
1434 // This may create a Pixbuf representation in the
1435 // wxBitmap object (and it will stay there)
1436 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1438 GValue gvalue
= { 0, };
1439 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1440 g_value_set_object( &gvalue
, pixbuf
);
1441 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1442 g_value_unset( &gvalue
);
1450 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1455 // ---------------------------------------------------------
1456 // wxDataViewToggleRenderer
1457 // ---------------------------------------------------------
1460 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1461 gchar
*path
, gpointer user_data
);
1464 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1465 gchar
*path
, gpointer user_data
)
1467 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1470 GValue gvalue
= { 0, };
1471 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1472 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1473 bool tmp
= g_value_get_boolean( &gvalue
);
1474 g_value_unset( &gvalue
);
1478 wxVariant value
= tmp
;
1479 if (!cell
->Validate( value
))
1482 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1484 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1486 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, gtk_path
);
1487 wxDataViewItem
item( (void*) iter
.user_data
);;
1488 gtk_tree_path_free( gtk_path
);
1490 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1492 model
->SetValue( value
, item
, model_col
);
1493 model
->ValueChanged( item
, model_col
);
1496 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1498 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1499 wxDataViewCellMode mode
, int align
) :
1500 wxDataViewRenderer( varianttype
, mode
, align
)
1502 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
1504 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1506 g_signal_connect_after( m_renderer
, "toggled",
1507 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1511 GValue gvalue
= { 0, };
1512 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1513 g_value_set_boolean( &gvalue
, false );
1514 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1515 g_value_unset( &gvalue
);
1519 SetAlignment(align
);
1522 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1526 GValue gvalue
= { 0, };
1527 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1528 g_value_set_boolean( &gvalue
, tmp
);
1529 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1530 g_value_unset( &gvalue
);
1535 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1537 GValue gvalue
= { 0, };
1538 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1539 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1540 bool tmp
= g_value_get_boolean( &gvalue
);
1541 g_value_unset( &gvalue
);
1548 // ---------------------------------------------------------
1549 // wxDataViewCustomRenderer
1550 // ---------------------------------------------------------
1552 class wxDataViewCtrlDC
: public wxWindowDC
1555 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1557 GtkWidget
*widget
= window
->m_treeview
;
1561 m_context
= window
->GtkGetPangoDefaultContext();
1562 m_layout
= pango_layout_new( m_context
);
1563 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1565 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1567 // Set m_window later
1569 // m_owner = window;
1573 // ---------------------------------------------------------
1574 // wxDataViewCustomRenderer
1575 // ---------------------------------------------------------
1577 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1579 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1580 wxDataViewCellMode mode
, int align
,
1582 wxDataViewRenderer( varianttype
, mode
, align
)
1592 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1594 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1595 renderer
->cell
= this;
1597 m_renderer
= (GtkCellRenderer
*) renderer
;
1600 SetAlignment(align
);
1605 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1611 wxDC
*wxDataViewCustomRenderer::GetDC()
1615 if (GetOwner() == NULL
)
1617 if (GetOwner()->GetOwner() == NULL
)
1619 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1625 // ---------------------------------------------------------
1626 // wxDataViewProgressRenderer
1627 // ---------------------------------------------------------
1629 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1631 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1632 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1633 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1639 if (!gtk_check_version(2,6,0))
1641 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
1643 GValue gvalue
= { 0, };
1644 g_value_init( &gvalue
, G_TYPE_STRING
);
1646 // FIXME: font encoding support
1647 g_value_set_string( &gvalue
, wxGTK_CONV_SYS(m_label
) );
1648 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1649 g_value_unset( &gvalue
);
1652 SetAlignment(align
);
1657 // Use custom cell code
1658 wxDataViewCustomRenderer::Init(mode
, align
);
1662 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1666 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1669 if (!gtk_check_version(2,6,0))
1671 gint tmp
= (long) value
;
1672 GValue gvalue
= { 0, };
1673 g_value_init( &gvalue
, G_TYPE_INT
);
1674 g_value_set_int( &gvalue
, tmp
);
1675 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1676 g_value_unset( &gvalue
);
1681 m_value
= (long) value
;
1683 if (m_value
< 0) m_value
= 0;
1684 if (m_value
> 100) m_value
= 100;
1690 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1695 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1697 double pct
= (double)m_value
/ 100.0;
1699 bar
.width
= (int)(cell
.width
* pct
);
1700 dc
->SetPen( *wxTRANSPARENT_PEN
);
1701 dc
->SetBrush( *wxBLUE_BRUSH
);
1702 dc
->DrawRectangle( bar
);
1704 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1705 dc
->SetPen( *wxBLACK_PEN
);
1706 dc
->DrawRectangle( cell
);
1711 wxSize
wxDataViewProgressRenderer::GetSize() const
1713 return wxSize(40,12);
1716 // ---------------------------------------------------------
1717 // wxDataViewDateRenderer
1718 // ---------------------------------------------------------
1720 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1723 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1724 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
1725 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1730 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1731 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1732 sizer
->Add( m_cal
, 1, wxGROW
);
1737 virtual void OnDismiss()
1741 void OnCalendar( wxCalendarEvent
&event
);
1743 wxCalendarCtrl
*m_cal
;
1744 wxDataViewModel
*m_model
;
1745 wxDataViewItem m_item
;
1749 DECLARE_EVENT_TABLE()
1752 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1753 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1756 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1758 wxDateTime date
= event
.GetDate();
1759 wxVariant value
= date
;
1760 m_model
->SetValue( value
, m_item
, m_col
);
1761 m_model
->ValueChanged( m_item
, m_col
);
1765 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1767 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1768 wxDataViewCellMode mode
, int align
) :
1769 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1772 SetAlignment(align
);
1775 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1777 m_date
= value
.GetDateTime();
1782 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1787 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1789 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1790 wxString tmp
= m_date
.FormatDate();
1791 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1796 wxSize
wxDataViewDateRenderer::GetSize() const
1798 wxString tmp
= m_date
.FormatDate();
1800 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1801 return wxSize(x
,y
+d
);
1804 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewModel
*model
,
1805 const wxDataViewItem
&item
, unsigned int col
)
1808 model
->GetValue( variant
, item
, col
);
1809 wxDateTime value
= variant
.GetDateTime();
1811 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1812 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1813 wxPoint pos
= wxGetMousePosition();
1816 popup
->Popup( popup
->m_cal
);
1821 // ---------------------------------------------------------
1823 // ---------------------------------------------------------
1827 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
1828 GdkEventButton
*gdk_event
,
1829 wxDataViewColumn
*column
)
1831 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
1834 if (gdk_event
->button
== 1)
1836 wxDataViewCtrl
*dv
= column
->GetOwner();
1837 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
1838 event
.SetDataViewColumn( column
);
1839 event
.SetModel( dv
->GetModel() );
1840 if (dv
->GetEventHandler()->ProcessEvent( event
))
1848 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1849 GtkCellRenderer
*cell
,
1850 GtkTreeModel
*model
,
1856 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1857 GtkCellRenderer
*renderer
,
1858 GtkTreeModel
*model
,
1862 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
1863 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
1865 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
1867 wxDataViewItem
item( (void*) iter
->user_data
);
1870 tree_model
->internal
->GetDataViewModel()->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
1872 if (value
.GetType() != cell
->GetVariantType())
1873 wxLogError( wxT("Wrong type, required: %s but: %s"),
1874 value
.GetType().c_str(),
1875 cell
->GetVariantType().c_str() );
1877 cell
->SetValue( value
);
1880 wxListItemAttr attr
;
1881 tree_model
->model
->GetAttr( attr
, cell
->GetOwner()->GetModelColumn(), model_row
);
1883 if (attr
.HasBackgroundColour())
1885 wxColour colour
= attr
.GetBackgroundColour();
1886 const GdkColor
* const gcol
= colour
.GetColor();
1888 GValue gvalue
= { 0, };
1889 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
1890 g_value_set_boxed( &gvalue
, gcol
);
1891 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
1892 g_value_unset( &gvalue
);
1896 GValue gvalue
= { 0, };
1897 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1898 g_value_set_boolean( &gvalue
, FALSE
);
1899 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
1900 g_value_unset( &gvalue
);
1906 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1908 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1909 unsigned int model_column
, int width
,
1910 wxAlignment align
, int flags
) :
1911 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1913 Init( align
, flags
, width
);
1915 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
1919 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1920 unsigned int model_column
, int width
,
1921 wxAlignment align
, int flags
) :
1922 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1924 Init( align
, flags
, width
);
1926 SetBitmap( bitmap
);
1929 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
1931 m_isConnected
= false;
1933 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
1934 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1935 m_column
= (GtkWidget
*) column
;
1938 SetAlignment( align
);
1940 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
1941 // as GTK+ is smart and unless explicitely told, will set the minimal
1942 // width to the title's lenght, which is a better default
1944 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
1945 // that we use for the wxDataViewCtrl
1946 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1947 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
1949 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
1951 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1952 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
1955 wxDataViewColumn::~wxDataViewColumn()
1959 void wxDataViewColumn::OnInternalIdle()
1964 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
1966 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1969 g_signal_connect(column
->button
, "button_press_event",
1970 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
1972 m_isConnected
= true;
1977 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
1979 wxDataViewColumnBase::SetOwner( owner
);
1981 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1983 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
1986 void wxDataViewColumn::SetTitle( const wxString
&title
)
1988 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1992 // disconnect before column->button gets recreated
1993 g_signal_handlers_disconnect_by_func( column
->button
,
1994 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
1996 m_isConnected
= false;
1999 // FIXME: can it really happen that we don't have the owner here??
2000 wxDataViewCtrl
*ctrl
= GetOwner();
2001 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
2002 : wxGTK_CONV_SYS(title
) );
2004 gtk_tree_view_column_set_widget( column
, NULL
);
2007 wxString
wxDataViewColumn::GetTitle() const
2009 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
2010 return wxConvFileName
->cMB2WX(str
);
2013 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
2015 wxDataViewColumnBase::SetBitmap( bitmap
);
2017 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2020 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
2022 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
2023 if (bitmap
.GetMask())
2024 mask
= bitmap
.GetMask()->GetBitmap();
2026 if (bitmap
.HasPixbuf())
2028 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
2029 bitmap
.GetPixbuf());
2033 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
2034 bitmap
.GetPixmap(), mask
);
2036 gtk_widget_show( GTK_WIDGET(gtk_image
) );
2038 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
2042 gtk_tree_view_column_set_widget( column
, NULL
);
2046 void wxDataViewColumn::SetHidden( bool hidden
)
2048 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
2051 void wxDataViewColumn::SetResizeable( bool resizeable
)
2053 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
2056 void wxDataViewColumn::SetAlignment( wxAlignment align
)
2058 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2060 gfloat xalign
= 0.0;
2061 if (align
== wxALIGN_RIGHT
)
2063 if (align
== wxALIGN_CENTER_HORIZONTAL
||
2064 align
== wxALIGN_CENTER
)
2067 gtk_tree_view_column_set_alignment( column
, xalign
);
2070 wxAlignment
wxDataViewColumn::GetAlignment() const
2072 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
2075 return wxALIGN_RIGHT
;
2077 return wxALIGN_CENTER_HORIZONTAL
;
2079 return wxALIGN_LEFT
;
2082 void wxDataViewColumn::SetSortable( bool sortable
)
2084 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2087 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
2089 gtk_tree_view_column_set_sort_column_id( column
, -1 );
2092 bool wxDataViewColumn::IsSortable() const
2094 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2095 return (gtk_tree_view_column_get_sort_column_id( column
) != -1);
2098 bool wxDataViewColumn::IsResizeable() const
2100 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2101 return gtk_tree_view_column_get_resizable( column
);
2104 bool wxDataViewColumn::IsHidden() const
2106 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2107 return !gtk_tree_view_column_get_visible( column
);
2110 void wxDataViewColumn::SetSortOrder( bool ascending
)
2112 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2115 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
2117 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
2120 bool wxDataViewColumn::IsSortOrderAscending() const
2122 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2124 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
2127 void wxDataViewColumn::SetMinWidth( int width
)
2129 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2132 int wxDataViewColumn::GetMinWidth() const
2134 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2137 int wxDataViewColumn::GetWidth() const
2139 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2142 void wxDataViewColumn::SetWidth( int width
)
2144 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2148 //-----------------------------------------------------------------------------
2149 // wxGtkTreeModelNode
2150 //-----------------------------------------------------------------------------
2152 void wxGtkTreeModelNode::Resort()
2154 g_internal
= m_internal
;
2156 size_t child_count
= GetChildCount();
2157 if (child_count
== 0)
2160 size_t node_count
= GetNodesCount();
2162 if (child_count
== 1)
2164 if (node_count
== 1)
2166 wxGtkTreeModelNode
*node
= m_nodes
.Item( 0 );
2172 wxGtkTreeModelChildren
*new_array
= new wxGtkTreeModelChildren( wxGtkTreeModelNodeCmp
);
2175 for (pos
= 0; pos
< child_count
; pos
++)
2176 new_array
->Add( m_children
->Item( pos
) );
2178 gint
*new_order
= new gint
[child_count
];
2180 for (pos
= 0; pos
< child_count
; pos
++)
2182 void *id
= new_array
->Item( pos
);
2184 for (old_pos
= 0; old_pos
< child_count
; old_pos
++)
2186 if (id
== m_children
->Item(old_pos
))
2188 new_order
[pos
] = old_pos
;
2194 // for (pos = 0; pos < count; pos++)
2195 // m_children->Clear();
2198 m_children
= new_array
;
2200 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
2202 GtkTreePath
*path
= gtk_tree_path_new ();
2203 wxGtkTreeModelNode
*parent
= GetParent();
2204 void *id
= GetItem().GetID();
2208 int pos
= parent
->GetChildren().Index( id
);
2209 gtk_tree_path_prepend_index( path
, pos
);
2210 id
= parent
->GetItem().GetID();
2211 parent
= parent
->GetParent();
2215 iter
.user_data
= id
;
2216 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
2217 gtk_tree_model_rows_reordered( gtk_tree_model
, path
, &iter
, new_order
);
2219 gtk_tree_path_free (path
);
2221 delete [] new_order
;
2223 for (pos
= 0; pos
< node_count
; pos
++)
2225 wxGtkTreeModelNode
*node
= m_nodes
.Item( pos
);
2230 //-----------------------------------------------------------------------------
2231 // wxDataViewCtrlInternal
2232 //-----------------------------------------------------------------------------
2234 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
,
2235 wxDataViewModel
*wx_model
, GtkWxTreeModel
*gtk_model
)
2238 m_wx_model
= wx_model
;
2239 m_gtk_model
= gtk_model
;
2241 m_sort_order
= GTK_SORT_ASCENDING
;
2245 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2247 g_object_unref( m_gtk_model
);
2250 void wxDataViewCtrlInternal::InitTree()
2252 wxDataViewItem item
;
2253 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
2255 BuildBranch( m_root
);
2258 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
2260 if (node
->GetChildCount() == 0)
2262 wxDataViewItem child
= m_wx_model
->GetFirstChild( node
->GetItem() );
2263 while (child
.IsOk())
2265 if (m_wx_model
->IsContainer( child
))
2266 node
->AddNode( new wxGtkTreeModelNode( node
, child
, this ) );
2268 node
->AddLeave( child
.GetID() );
2270 // Don't send any events here
2272 child
= m_wx_model
->GetNextSibling( child
);
2277 void wxDataViewCtrlInternal::Resort()
2282 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2284 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2285 if (m_wx_model
->IsContainer( item
))
2286 parent_node
->AddNode( new wxGtkTreeModelNode( parent_node
, item
, this ) );
2288 parent_node
->AddLeave( item
.GetID() );
2290 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, m_owner
->GetId() );
2291 event
.SetEventObject( m_owner
);
2292 event
.SetModel( m_owner
->GetModel() );
2293 event
.SetItem( item
);
2294 m_owner
->GetEventHandler()->ProcessEvent( event
);
2299 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2301 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2302 parent_node
->DeleteChild( item
.GetID() );
2304 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, m_owner
->GetId() );
2305 event
.SetEventObject( m_owner
);
2306 event
.SetModel( m_owner
->GetModel() );
2307 event
.SetItem( item
);
2308 m_owner
->GetEventHandler()->ProcessEvent( event
);
2313 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem
&item
)
2315 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
, m_owner
->GetId() );
2316 event
.SetEventObject( m_owner
);
2317 event
.SetModel( m_owner
->GetModel() );
2318 event
.SetItem( item
);
2319 m_owner
->GetEventHandler()->ProcessEvent( event
);
2324 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
2326 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, m_owner
->GetId() );
2327 event
.SetEventObject( m_owner
);
2328 event
.SetModel( m_owner
->GetModel() );
2329 event
.SetColumn( col
);
2330 event
.SetItem( item
);
2331 m_owner
->GetEventHandler()->ProcessEvent( event
);
2336 bool wxDataViewCtrlInternal::Cleared()
2338 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, m_owner
->GetId() );
2339 event
.SetEventObject( m_owner
);
2340 event
.SetModel( m_owner
->GetModel() );
2341 m_owner
->GetEventHandler()->ProcessEvent( event
);
2346 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
2350 int depth
= gtk_tree_path_get_depth( path
);
2352 wxGtkTreeModelNode
*node
= m_root
;
2355 for (i
= 0; i
< depth
; i
++)
2357 BuildBranch( node
);
2359 gint pos
= gtk_tree_path_get_indices (path
)[i
];
2360 if (pos
< 0) return FALSE
;
2361 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
2363 void* id
= node
->GetChildren().Item( (size_t) pos
);
2367 iter
->stamp
= m_gtk_model
->stamp
;
2368 iter
->user_data
= id
;
2372 size_t count
= node
->GetNodes().GetCount();
2374 for (pos2
= 0; pos2
< count
; pos2
++)
2376 wxGtkTreeModelNode
*child_node
= node
->GetNodes().Item( pos2
);
2377 if (child_node
->GetItem().GetID() == id
)
2388 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
2392 GtkTreePath
*retval
= gtk_tree_path_new ();
2393 void *id
= iter
->user_data
;
2395 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
2398 int pos
= node
->GetChildren().Index( id
);
2399 gtk_tree_path_prepend_index( retval
, pos
);
2401 id
= node
->GetItem().GetID();
2402 node
= node
->GetParent();
2408 GtkTreePath
*wxDataViewCtrlInternal::get_path_safe( GtkTreeIter
*iter
)
2412 GtkTreePath
*retval
= gtk_tree_path_new ();
2413 void *id
= iter
->user_data
;
2415 wxGtkTreeModelNode
*node
= FindParentNode( iter
);
2419 for (pos
= 0; pos
< node
->GetChildren().GetCount(); pos
++)
2421 if (id
== node
->GetChildren().Item( pos
))
2423 gtk_tree_path_prepend_index( retval
, (int) pos
);
2428 id
= node
->GetItem().GetID();
2429 node
= node
->GetParent();
2435 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
2439 wxGtkTreeModelNode
*parent
= FindParentNode( iter
);
2440 if( parent
== NULL
)
2443 unsigned int pos
= parent
->GetChildren().Index( iter
->user_data
);
2445 if (pos
== parent
->GetChildCount()-1)
2448 iter
->stamp
= m_gtk_model
->stamp
;
2449 iter
->user_data
= parent
->GetChildren().Item( pos
+1 );
2454 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
2458 wxDataViewItem
item( (void*) parent
->user_data
);
2460 if (!m_wx_model
->IsContainer( item
))
2463 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2464 BuildBranch( parent_node
);
2466 if (parent_node
->GetChildCount() == 0)
2469 iter
->stamp
= m_gtk_model
->stamp
;
2470 iter
->user_data
= (gpointer
) parent_node
->GetChildren().Item( 0 );
2475 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
2479 wxDataViewItem
item( (void*) iter
->user_data
);
2480 bool is_container
= m_wx_model
->IsContainer( item
);
2485 wxGtkTreeModelNode
*node
= FindNode( iter
);
2486 BuildBranch( node
);
2488 return (node
->GetChildCount() > 0);
2491 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
2495 wxDataViewItem
item( (void*) iter
->user_data
);
2497 if (!m_wx_model
->IsContainer( item
))
2500 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
2501 BuildBranch( parent_node
);
2503 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2505 return parent_node
->GetChildCount();
2508 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
2513 if (parent
) id
= (void*) parent
->user_data
;
2514 wxDataViewItem
item( id
);
2516 if (!m_wx_model
->IsContainer( item
))
2519 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2520 BuildBranch( parent_node
);
2522 // wxPrintf( "iter_nth_child %d\n", n );
2524 iter
->stamp
= m_gtk_model
->stamp
;
2525 iter
->user_data
= parent_node
->GetChildren().Item( n
);
2530 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
2534 wxGtkTreeModelNode
*node
= FindParentNode( child
);
2538 iter
->stamp
= m_gtk_model
->stamp
;
2539 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2544 static wxGtkTreeModelNode
*
2545 wxDataViewCtrlInternal_FindNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
2551 list
.DeleteContents( true );
2552 wxDataViewItem
it( item
);
2555 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2556 list
.Insert( pItem
);
2557 it
= model
->GetParent( it
);
2560 wxGtkTreeModelNode
* node
= treeNode
;
2561 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2563 if( node
&& node
->GetNodes().GetCount() != 0 )
2565 int len
= node
->GetNodes().GetCount();
2566 wxGtkTreeModelNodes nodes
= node
->GetNodes();
2568 for( ; j
< len
; j
++)
2570 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2589 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
2594 wxDataViewItem
item( (void*) iter
->user_data
);
2598 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
2602 wxPrintf( "Not found %d\n", (int) iter
->user_data
);
2610 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
2615 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_wx_model
, m_root
, item
);
2619 wxPrintf( "Not found %d\n", (int) item
.GetID() );
2627 static wxGtkTreeModelNode
*
2628 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel
* model
, wxGtkTreeModelNode
*treeNode
, const wxDataViewItem
&item
)
2634 list
.DeleteContents( true );
2638 wxDataViewItem
it( model
->GetParent( item
) );
2641 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2642 list
.Insert( pItem
);
2643 it
= model
->GetParent( it
);
2646 wxGtkTreeModelNode
* node
= treeNode
;
2647 for( ItemList::compatibility_iterator n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2649 if( node
&& node
->GetNodes().GetCount() != 0 )
2651 int len
= node
->GetNodes().GetCount();
2652 wxGtkTreeModelNodes nodes
= node
->GetNodes();
2654 for( ; j
< len
; j
++)
2656 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2671 //Examine whether the node is item's parent node
2672 int len
= node
->GetChildCount();
2673 for( int i
= 0; i
< len
; i
++ )
2675 if( node
->GetChildren().Item( i
) == item
.GetID() )
2681 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( GtkTreeIter
*iter
)
2686 wxDataViewItem
item( (void*) iter
->user_data
);
2690 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
2693 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem
&item
)
2698 return wxDataViewCtrlInternal_FindParentNode( m_wx_model
, m_root
, item
);
2701 //-----------------------------------------------------------------------------
2702 // wxDataViewCtrl signal callbacks
2703 //-----------------------------------------------------------------------------
2706 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
2708 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
2711 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
, dv
->GetId() );
2713 event
.SetModel( dv
->GetModel() );
2714 dv
->GetEventHandler()->ProcessEvent( event
);
2718 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
2719 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
2721 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
2724 dv
->GtkGetInternal()->get_iter( &iter
, path
);
2725 wxDataViewItem
item( (void*) iter
.user_data
);;
2726 event
.SetItem( item
);
2727 event
.SetModel( dv
->GetModel() );
2728 dv
->GetEventHandler()->ProcessEvent( event
);
2731 //-----------------------------------------------------------------------------
2733 //-----------------------------------------------------------------------------
2735 //-----------------------------------------------------------------------------
2736 // InsertChild for wxDataViewCtrl
2737 //-----------------------------------------------------------------------------
2739 static void wxInsertChildInDataViewCtrl( wxWindowGTK
* parent
, wxWindowGTK
* child
)
2741 wxDataViewCtrl
* dvc
= (wxDataViewCtrl
*) parent
;
2742 GtkWidget
*treeview
= dvc
->GtkGetTreeView();
2744 // Insert widget in GtkTreeView
2745 if (GTK_WIDGET_REALIZED(treeview
))
2746 gtk_widget_set_parent_window( child
->m_widget
,
2747 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
2748 gtk_widget_set_parent( child
->m_widget
, treeview
);
2752 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
2753 GtkAllocation
*alloc
,
2754 wxDataViewCtrl
*win
)
2756 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
2759 wxWindow
*child
= node
->GetData();
2762 gtk_widget_size_request( child
->m_widget
, &req
);
2764 GtkAllocation alloc
;
2765 alloc
.x
= child
->m_x
;
2766 alloc
.y
= child
->m_y
;
2767 alloc
.width
= child
->m_width
;
2768 alloc
.height
= child
->m_height
;
2769 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
2771 node
= node
->GetNext();
2777 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
2779 wxDataViewCtrl::~wxDataViewCtrl()
2782 GetModel()->RemoveNotifier( m_notifier
);
2784 // remove the model from the GtkTreeView before it gets destroyed by the
2785 // wxDataViewCtrlBase's dtor
2786 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
2791 void wxDataViewCtrl::Init()
2796 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
2797 const wxPoint
& pos
, const wxSize
& size
,
2798 long style
, const wxValidator
& validator
)
2802 if (!PreCreation( parent
, pos
, size
) ||
2803 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
2805 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
2809 m_insertCallback
= wxInsertChildInDataViewCtrl
;
2811 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
2813 GtkScrolledWindowSetBorder(m_widget
, style
);
2815 m_treeview
= gtk_tree_view_new();
2816 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
2818 g_signal_connect (m_treeview
, "size_allocate",
2819 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
2822 if (!gtk_check_version(2,6,0))
2823 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
2826 if (style
& wxDV_MULTIPLE
)
2828 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2829 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
2832 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
2835 if (!gtk_check_version(2,10,0))
2837 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
2839 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
2840 (style
& wxDV_VERT_RULES
) != 0)
2841 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
2842 else if (style
& wxDV_VERT_RULES
)
2843 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
2844 else if (style
& wxDV_HORIZ_RULES
)
2845 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
2847 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
2852 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
2855 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
2856 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2857 gtk_widget_show (m_treeview
);
2859 m_parent
->DoAddChild( this );
2863 GtkEnableSelectionEvents();
2865 g_signal_connect_after (m_treeview
, "row_activated",
2866 G_CALLBACK (wxdataview_row_activated_callback
), this);
2871 void wxDataViewCtrl::OnInternalIdle()
2873 wxWindow::OnInternalIdle();
2875 unsigned int cols
= GetColumnCount();
2877 for (i
= 0; i
< cols
; i
++)
2879 wxDataViewColumn
*col
= GetColumn( i
);
2880 col
->OnInternalIdle();
2884 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
2886 if (!wxDataViewCtrlBase::AssociateModel( model
))
2889 GtkWxTreeModel
*gtk_model
= wxgtk_tree_model_new();
2890 m_internal
= new wxDataViewCtrlInternal( this, model
, gtk_model
);
2891 gtk_model
->internal
= m_internal
;
2893 m_notifier
= new wxGtkDataViewModelNotifier( gtk_model
, model
, this );
2895 model
->AddNotifier( m_notifier
);
2897 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_model
) );
2899 // unref in wxDataViewCtrlInternal
2900 // g_object_unref( gtk_model );
2905 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
2907 if (!wxDataViewCtrlBase::AppendColumn(col
))
2910 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
2912 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
2917 wxDataViewItem
wxDataViewCtrl::GetSelection()
2919 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2921 if (m_windowStyle
& wxDV_MULTIPLE
)
2927 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
2929 wxDataViewItem
item( (void*) iter
.user_data
);
2934 return wxDataViewItem(0);
2937 void wxDataViewCtrl::DoSetExpanderColumn()
2941 void wxDataViewCtrl::DoSetIndent()
2945 void wxDataViewCtrl::GtkDisableSelectionEvents()
2947 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2948 g_signal_connect_after (selection
, "changed",
2949 G_CALLBACK (wxdataview_selection_changed_callback
), this);
2952 void wxDataViewCtrl::GtkEnableSelectionEvents()
2954 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2955 g_signal_handlers_disconnect_by_func( selection
,
2956 (gpointer
) (wxdataview_selection_changed_callback
), this);
2961 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2963 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
2968 // !wxUSE_GENERICDATAVIEWCTRL
2971 // wxUSE_DATAVIEWCTRL