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
;
52 typedef struct _GtkWxTreeModel GtkWxTreeModel
;
55 int LINKAGEMODE
wxGtkTreeModelNodeCmp( wxGtkTreeModelNode
* node1
, wxGtkTreeModelNode
* node2
);
57 WX_DEFINE_SORTED_ARRAY( wxGtkTreeModelNode
*, wxGtkTreeModelNodes
);
59 class wxGtkTreeModelNode
62 wxGtkTreeModelNode( wxGtkTreeModelNode
* parent
, const wxDataViewItem
&item
,
63 wxDataViewCtrlInternal
*internal
)
67 m_internal
= internal
;
68 m_children
= new wxGtkTreeModelNodes( wxGtkTreeModelNodeCmp
);
73 size_t count
= m_children
->GetCount();
75 for (i
= 0; i
< count
; i
++)
77 wxGtkTreeModelNode
*child
= m_children
->Item( i
);
83 wxGtkTreeModelNode
* GetParent()
85 wxGtkTreeModelNodes
&GetChildren()
86 { return *m_children
; }
87 wxGtkTreeModelNode
* GetNthChild( unsigned int n
)
88 { return m_children
->Item( n
); }
89 unsigned int Add( wxGtkTreeModelNode
* child
)
90 { return m_children
->Add( child
); }
92 unsigned int GetChildCount() { return m_children
->GetCount(); }
94 wxDataViewItem
&GetItem() { return m_item
; }
95 wxDataViewCtrlInternal
*GetInternal() { return m_internal
; }
97 bool HasChildren() { return m_hasChildren
; }
98 void SetHasChildren( bool has
) { m_hasChildren
= has
; }
103 wxGtkTreeModelNode
*m_parent
;
104 wxGtkTreeModelNodes
*m_children
;
105 wxDataViewItem m_item
;
107 wxDataViewCtrlInternal
*m_internal
;
112 class wxDataViewCtrlInternal
115 wxDataViewCtrlInternal( wxDataViewCtrl
*owner
, wxDataViewModel
*wx_model
, GtkWxTreeModel
*owner
);
116 ~wxDataViewCtrlInternal();
118 gboolean
get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
);
119 GtkTreePath
*get_path( GtkTreeIter
*iter
);
120 GtkTreePath
*get_path_safe( GtkTreeIter
*iter
);
121 gboolean
iter_next( GtkTreeIter
*iter
);
122 gboolean
iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
);
123 gboolean
iter_has_child( GtkTreeIter
*iter
);
124 gint
iter_n_children( GtkTreeIter
*iter
);
125 gboolean
iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
);
126 gboolean
iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
);
128 wxDataViewModel
* GetDataViewModel() { return m_wx_model
; }
129 wxDataViewCtrl
* GetOwner() { return m_owner
; }
130 GtkWxTreeModel
* GetGtkModel() { return m_gtk_model
; }
132 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
133 bool ItemDeleted( const wxDataViewItem
&item
);
139 wxGtkTreeModelNode
*FindNode( const wxDataViewItem
&item
);
140 wxGtkTreeModelNode
*FindNode( GtkTreeIter
*iter
);
141 void BuildBranch( wxGtkTreeModelNode
*branch
);
144 wxGtkTreeModelNode
*m_root
;
145 wxDataViewModel
*m_wx_model
;
146 GtkWxTreeModel
*m_gtk_model
;
147 wxDataViewCtrl
*m_owner
;
151 int LINKAGEMODE
wxGtkTreeModelNodeCmp( wxGtkTreeModelNode
* node1
, wxGtkTreeModelNode
* node2
)
153 return node1
->GetInternal()->GetDataViewModel()->Compare( node1
->GetItem(), node2
->GetItem() );
156 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
160 extern bool g_blockEventsOnDrag
;
162 //-----------------------------------------------------------------------------
163 // define new GTK+ class wxGtkTreeModel
164 //-----------------------------------------------------------------------------
168 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
169 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
170 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
171 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
172 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
173 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
175 GType
gtk_wx_tree_model_get_type (void);
177 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass
;
179 struct _GtkWxTreeModel
185 wxDataViewCtrlInternal
*internal
;
188 struct _GtkWxTreeModelClass
190 GObjectClass list_parent_class
;
193 static GtkWxTreeModel
*wxgtk_tree_model_new (void);
194 static void wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
);
195 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
);
196 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
);
197 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
);
198 static void wxgtk_tree_model_finalize (GObject
*object
);
199 static GtkTreeModelFlags
wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
);
200 static gint
wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
);
201 static GType
wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
203 static gboolean
wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
206 static GtkTreePath
*wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
208 static GtkTreePath
*wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
210 static void wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
214 static gboolean
wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
216 static gboolean
wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
218 GtkTreeIter
*parent
);
219 static gboolean
wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
221 static gint
wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
223 static gboolean
wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
227 static gboolean
wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
232 static gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
233 gint
*sort_column_id
,
235 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
238 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
240 GtkTreeIterCompareFunc func
,
242 GtkDestroyNotify destroy
);
243 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
244 GtkTreeIterCompareFunc func
,
246 GtkDestroyNotify destroy
);
247 static gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
);
251 static GObjectClass
*list_parent_class
= NULL
;
254 gtk_wx_tree_model_get_type (void)
256 static GType tree_model_type
= 0;
258 if (!tree_model_type
)
260 const GTypeInfo tree_model_info
=
262 sizeof (GtkWxTreeModelClass
),
263 NULL
, /* base_init */
264 NULL
, /* base_finalize */
265 (GClassInitFunc
) wxgtk_tree_model_class_init
,
266 NULL
, /* class_finalize */
267 NULL
, /* class_data */
268 sizeof (GtkWxTreeModel
),
270 (GInstanceInitFunc
) wxgtk_tree_model_init
,
273 static const GInterfaceInfo tree_model_iface_info
=
275 (GInterfaceInitFunc
) wxgtk_tree_model_tree_model_init
,
280 static const GInterfaceInfo sortable_iface_info
=
282 (GInterfaceInitFunc
) wxgtk_tree_model_sortable_init
,
287 tree_model_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxTreeModel",
288 &tree_model_info
, (GTypeFlags
)0 );
290 g_type_add_interface_static (tree_model_type
,
292 &tree_model_iface_info
);
293 g_type_add_interface_static (tree_model_type
,
294 GTK_TYPE_TREE_SORTABLE
,
295 &sortable_iface_info
);
298 return tree_model_type
;
301 static GtkWxTreeModel
*
302 wxgtk_tree_model_new(void)
304 GtkWxTreeModel
*retval
= (GtkWxTreeModel
*) g_object_new (GTK_TYPE_WX_TREE_MODEL
, NULL
);
309 wxgtk_tree_model_class_init (GtkWxTreeModelClass
*klass
)
311 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
312 GObjectClass
*object_class
= (GObjectClass
*) klass
;
313 object_class
->finalize
= wxgtk_tree_model_finalize
;
317 wxgtk_tree_model_tree_model_init (GtkTreeModelIface
*iface
)
319 iface
->get_flags
= wxgtk_tree_model_get_flags
;
320 iface
->get_n_columns
= wxgtk_tree_model_get_n_columns
;
321 iface
->get_column_type
= wxgtk_tree_model_get_column_type
;
322 iface
->get_iter
= wxgtk_tree_model_get_iter
;
323 iface
->get_path
= wxgtk_tree_model_get_path
;
324 iface
->get_value
= wxgtk_tree_model_get_value
;
325 iface
->iter_next
= wxgtk_tree_model_iter_next
;
326 iface
->iter_children
= wxgtk_tree_model_iter_children
;
327 iface
->iter_has_child
= wxgtk_tree_model_iter_has_child
;
328 iface
->iter_n_children
= wxgtk_tree_model_iter_n_children
;
329 iface
->iter_nth_child
= wxgtk_tree_model_iter_nth_child
;
330 iface
->iter_parent
= wxgtk_tree_model_iter_parent
;
334 wxgtk_tree_model_sortable_init (GtkTreeSortableIface
*iface
)
336 iface
->get_sort_column_id
= wxgtk_tree_model_get_sort_column_id
;
337 iface
->set_sort_column_id
= wxgtk_tree_model_set_sort_column_id
;
338 iface
->set_sort_func
= wxgtk_tree_model_set_sort_func
;
339 iface
->set_default_sort_func
= wxgtk_tree_model_set_default_sort_func
;
340 iface
->has_default_sort_func
= wxgtk_tree_model_has_default_sort_func
;
344 wxgtk_tree_model_init (GtkWxTreeModel
*tree_model
)
346 tree_model
->internal
= NULL
;
347 tree_model
->stamp
= g_random_int();
351 wxgtk_tree_model_finalize (GObject
*object
)
354 (* list_parent_class
->finalize
) (object
);
359 //-----------------------------------------------------------------------------
360 // implement callbacks from wxGtkTreeModel class by letting
361 // them call the methods of wxWidgets' wxDataViewModel
362 //-----------------------------------------------------------------------------
364 static GtkTreeModelFlags
365 wxgtk_tree_model_get_flags (GtkTreeModel
*tree_model
)
367 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model
), (GtkTreeModelFlags
)0 );
369 return GTK_TREE_MODEL_ITERS_PERSIST
;
373 wxgtk_tree_model_get_n_columns (GtkTreeModel
*tree_model
)
375 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
376 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), 0);
378 return wxtree_model
->internal
->GetDataViewModel()->GetColumnCount();
382 wxgtk_tree_model_get_column_type (GtkTreeModel
*tree_model
,
385 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
386 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), G_TYPE_INVALID
);
388 GType gtype
= G_TYPE_INVALID
;
390 wxString wxtype
= wxtree_model
->internal
->GetDataViewModel()->GetColumnType( (unsigned int) index
);
392 if (wxtype
== wxT("string"))
393 gtype
= G_TYPE_STRING
;
396 wxFAIL_MSG( _T("non-string columns not supported yet") );
403 wxgtk_tree_model_get_iter (GtkTreeModel
*tree_model
,
407 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
408 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
409 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
411 return wxtree_model
->internal
->get_iter( iter
, path
);
415 wxgtk_tree_model_get_path (GtkTreeModel
*tree_model
,
418 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
419 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
420 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
422 return wxtree_model
->internal
->get_path( iter
);
426 wxgtk_tree_model_get_path_safe (GtkTreeModel
*tree_model
,
429 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
430 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), NULL
);
431 g_return_val_if_fail (iter
->stamp
== GTK_WX_TREE_MODEL (wxtree_model
)->stamp
, NULL
);
433 return wxtree_model
->internal
->get_path_safe( iter
);
437 wxgtk_tree_model_get_value (GtkTreeModel
*tree_model
,
442 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
443 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
) );
445 wxDataViewModel
*model
= wxtree_model
->internal
->GetDataViewModel();
446 wxString mtype
= model
->GetColumnType( (unsigned int) column
);
447 if (mtype
== wxT("string"))
450 g_value_init( value
, G_TYPE_STRING
);
451 wxDataViewItem
item( (void*) iter
->user_data
);
452 model
->GetValue( variant
, item
, (unsigned int) column
);
454 g_value_set_string( value
, variant
.GetString().utf8_str() );
458 wxFAIL_MSG( _T("non-string columns not supported yet") );
463 wxgtk_tree_model_iter_next (GtkTreeModel
*tree_model
,
466 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
467 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
468 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
470 return wxtree_model
->internal
->iter_next( iter
);
474 wxgtk_tree_model_iter_children (GtkTreeModel
*tree_model
,
478 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
479 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
480 g_return_val_if_fail (wxtree_model
->stamp
== parent
->stamp
, FALSE
);
482 return wxtree_model
->internal
->iter_children( iter
, parent
);
486 wxgtk_tree_model_iter_has_child (GtkTreeModel
*tree_model
,
489 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
490 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
491 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, FALSE
);
493 return wxtree_model
->internal
->iter_has_child( iter
);
497 wxgtk_tree_model_iter_n_children (GtkTreeModel
*tree_model
,
500 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
501 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
502 g_return_val_if_fail (wxtree_model
->stamp
== iter
->stamp
, 0);
504 return wxtree_model
->internal
->iter_n_children( iter
);
508 wxgtk_tree_model_iter_nth_child (GtkTreeModel
*tree_model
,
513 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
514 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
516 return wxtree_model
->internal
->iter_nth_child( iter
, parent
, n
);
520 wxgtk_tree_model_iter_parent (GtkTreeModel
*tree_model
,
524 GtkWxTreeModel
*wxtree_model
= (GtkWxTreeModel
*) tree_model
;
525 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model
), FALSE
);
526 g_return_val_if_fail (wxtree_model
->stamp
== child
->stamp
, FALSE
);
528 return wxtree_model
->internal
->iter_parent( iter
, child
);
532 gboolean
wxgtk_tree_model_get_sort_column_id (GtkTreeSortable
*sortable
,
533 gint
*sort_column_id
,
536 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
538 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable
), FALSE
);
541 *sort_column_id
= tree_model
->internal
->GetDataViewModel()->GetSortingColumn();
545 bool ascending
= tree_model
->internal
->GetDataViewModel()->GetSortOrderAscending();
547 *order
= GTK_SORT_ASCENDING
;
549 *order
= GTK_SORT_DESCENDING
;
555 void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable
*sortable
,
559 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) sortable
;
560 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
562 bool ascending
= TRUE
;
563 if (order
!= GTK_SORT_ASCENDING
)
566 if ((sort_column_id
== tree_model
->internal
->GetDataViewModel()->GetSortingColumn()) &&
567 (ascending
== tree_model
->internal
->GetDataViewModel()->GetSortOrderAscending()))
570 tree_model
->internal
->GetDataViewModel()->SetSortingColumn( sort_column_id
);
572 tree_model
->internal
->GetDataViewModel()->SetSortOrderAscending( ascending
);
574 gtk_tree_sortable_sort_column_changed (sortable
);
576 tree_model
->internal
->GetDataViewModel()->Resort();
579 void wxgtk_tree_model_set_sort_func (GtkTreeSortable
*sortable
,
581 GtkTreeIterCompareFunc func
,
583 GtkDestroyNotify destroy
)
585 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
586 g_return_if_fail (func
!= NULL
);
589 void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable
*sortable
,
590 GtkTreeIterCompareFunc func
,
592 GtkDestroyNotify destroy
)
594 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable
) );
595 g_return_if_fail (func
!= NULL
);
597 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
600 gboolean
wxgtk_tree_model_has_default_sort_func (GtkTreeSortable
*sortable
)
605 //-----------------------------------------------------------------------------
606 // define new GTK+ class wxGtkRendererRenderer
607 //-----------------------------------------------------------------------------
611 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
612 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
613 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
614 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
615 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
616 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
618 GType
gtk_wx_cell_renderer_get_type (void);
620 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
621 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
623 struct _GtkWxCellRenderer
625 GtkCellRenderer parent
;
628 wxDataViewCustomRenderer
*cell
;
632 struct _GtkWxCellRendererClass
634 GtkCellRendererClass cell_parent_class
;
638 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
639 static void gtk_wx_cell_renderer_init (
640 GtkWxCellRenderer
*cell
);
641 static void gtk_wx_cell_renderer_class_init(
642 GtkWxCellRendererClass
*klass
);
643 static void gtk_wx_cell_renderer_finalize (
645 static void gtk_wx_cell_renderer_get_size (
646 GtkCellRenderer
*cell
,
648 GdkRectangle
*rectangle
,
653 static void gtk_wx_cell_renderer_render (
654 GtkCellRenderer
*cell
,
657 GdkRectangle
*background_area
,
658 GdkRectangle
*cell_area
,
659 GdkRectangle
*expose_area
,
660 GtkCellRendererState flags
);
661 static gboolean
gtk_wx_cell_renderer_activate(
662 GtkCellRenderer
*cell
,
666 GdkRectangle
*background_area
,
667 GdkRectangle
*cell_area
,
668 GtkCellRendererState flags
);
669 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
670 GtkCellRenderer
*cell
,
674 GdkRectangle
*background_area
,
675 GdkRectangle
*cell_area
,
676 GtkCellRendererState flags
);
679 static GObjectClass
*cell_parent_class
= NULL
;
684 gtk_wx_cell_renderer_get_type (void)
686 static GType cell_wx_type
= 0;
690 const GTypeInfo cell_wx_info
=
692 sizeof (GtkWxCellRendererClass
),
693 NULL
, /* base_init */
694 NULL
, /* base_finalize */
695 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
696 NULL
, /* class_finalize */
697 NULL
, /* class_data */
698 sizeof (GtkWxCellRenderer
),
700 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
703 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
704 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
711 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
714 cell
->last_click
= 0;
718 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
720 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
721 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
723 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
725 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
727 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
728 cell_class
->render
= gtk_wx_cell_renderer_render
;
729 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
730 cell_class
->start_editing
= gtk_wx_cell_renderer_start_editing
;
734 gtk_wx_cell_renderer_finalize (GObject
*object
)
737 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
741 gtk_wx_cell_renderer_new (void)
743 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
748 static GtkCellEditable
*gtk_wx_cell_renderer_start_editing(
749 GtkCellRenderer
*renderer
,
753 GdkRectangle
*background_area
,
754 GdkRectangle
*cell_area
,
755 GtkCellRendererState flags
)
757 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
758 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
759 if (!cell
->HasEditorCtrl())
763 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
769 rect
.x
+= cell_area
->x
;
770 rect
.y
+= cell_area
->y
;
771 // rect.width -= renderer->xpad * 2;
772 // rect.height -= renderer->ypad * 2;
774 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
775 wxRect
renderrect( cell_area
->x
, cell_area
->y
, cell_area
->width
, cell_area
->height
);
777 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
779 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, treepath
);
780 wxDataViewItem
item( (void*) iter
.user_data
);
781 gtk_tree_path_free( treepath
);
783 cell
->StartEditing( item
, renderrect
);
789 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
791 GdkRectangle
*cell_area
,
797 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
798 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
800 wxSize size
= cell
->GetSize();
802 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
803 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
810 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
814 *x_offset
= (gint
)((renderer
->xalign
*
815 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
816 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
820 *y_offset
= (gint
)((renderer
->yalign
*
821 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
822 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
830 *height
= calc_height
;
834 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
837 GdkRectangle
*background_area
,
838 GdkRectangle
*cell_area
,
839 GdkRectangle
*expose_area
,
840 GtkCellRendererState flags
)
843 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
844 wxDataViewCustomRenderer
*cell
= wxrenderer
->cell
;
847 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
853 rect
.x
+= cell_area
->x
;
854 rect
.y
+= cell_area
->y
;
855 rect
.width
-= renderer
->xpad
* 2;
856 rect
.height
-= renderer
->ypad
* 2;
859 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
861 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
862 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
863 if (dc
->m_window
== NULL
)
865 dc
->m_window
= window
;
870 if (flags
& GTK_CELL_RENDERER_SELECTED
)
871 state
|= wxDATAVIEW_CELL_SELECTED
;
872 if (flags
& GTK_CELL_RENDERER_PRELIT
)
873 state
|= wxDATAVIEW_CELL_PRELIT
;
874 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
875 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
876 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
877 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
878 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
879 state
|= wxDATAVIEW_CELL_FOCUSED
;
880 cell
->Render( renderrect
, dc
, state
);
885 gtk_wx_cell_renderer_activate(
886 GtkCellRenderer
*renderer
,
890 GdkRectangle
*background_area
,
891 GdkRectangle
*cell_area
,
892 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;
909 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
911 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
913 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
916 gtk_tree_path_free( treepath
);
918 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
924 // activated by <ENTER>
925 if (cell
->Activate( renderrect
, model
, item
, model_col
))
930 else if (event
->type
== GDK_BUTTON_PRESS
)
932 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
933 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
934 ((int) button_event
->y
) - renderrect
.y
);
937 if (button_event
->button
== 1)
939 if (cell
->LeftClick( pt
, renderrect
, model
, item
, model_col
))
941 // TODO: query system double-click time
942 if (button_event
->time
- wxrenderer
->last_click
< 400)
943 if (cell
->Activate( renderrect
, model
, item
, model_col
))
946 if (button_event
->button
== 3)
948 if (cell
->RightClick( pt
, renderrect
, model
, item
, model_col
))
952 wxrenderer
->last_click
= button_event
->time
;
960 // ---------------------------------------------------------
961 // wxGtkDataViewModelNotifier
962 // ---------------------------------------------------------
964 class wxGtkDataViewModelNotifier
: public wxDataViewModelNotifier
967 wxGtkDataViewModelNotifier( GtkWxTreeModel
*wxgtk_model
,
968 wxDataViewModel
*wx_model
,
969 wxDataViewCtrl
*ctrl
);
970 ~wxGtkDataViewModelNotifier();
972 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
973 virtual bool ItemDeleted( const wxDataViewItem
&item
);
974 virtual bool ItemChanged( const wxDataViewItem
&item
);
975 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
976 virtual bool Cleared();
977 virtual void Resort();
979 GtkWxTreeModel
*m_wxgtk_model
;
980 wxDataViewModel
*m_wx_model
;
981 wxDataViewCtrl
*m_owner
;
984 // ---------------------------------------------------------
985 // wxGtkDataViewListModelNotifier
986 // ---------------------------------------------------------
988 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
989 GtkWxTreeModel
* wxgtk_model
, wxDataViewModel
*wx_model
,
990 wxDataViewCtrl
*ctrl
)
992 m_wxgtk_model
= wxgtk_model
;
993 m_wx_model
= wx_model
;
997 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1000 m_wxgtk_model
= NULL
;
1003 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
1005 m_owner
->GtkGetInternal()->ItemAdded( parent
, item
);
1008 iter
.stamp
= m_wxgtk_model
->stamp
;
1009 iter
.user_data
= (gpointer
) item
.GetID();
1011 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1012 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1013 gtk_tree_model_row_inserted(
1014 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1015 gtk_tree_path_free (path
);
1020 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem
&item
)
1023 iter
.stamp
= m_wxgtk_model
->stamp
;
1024 iter
.user_data
= (gpointer
) item
.GetID();
1026 GtkTreePath
*path
= wxgtk_tree_model_get_path_safe(
1027 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1028 gtk_tree_model_row_deleted(
1029 GTK_TREE_MODEL(m_wxgtk_model
), path
);
1030 gtk_tree_path_free (path
);
1032 m_owner
->GtkGetInternal()->ItemDeleted( item
);
1037 void wxGtkDataViewModelNotifier::Resort()
1039 m_owner
->GtkGetInternal()->Resort();
1042 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem
&item
)
1045 iter
.stamp
= m_wxgtk_model
->stamp
;
1046 iter
.user_data
= (gpointer
) item
.GetID();
1048 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1049 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1050 gtk_tree_model_row_changed(
1051 GTK_TREE_MODEL(m_wxgtk_model
), path
, &iter
);
1052 gtk_tree_path_free (path
);
1057 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem
&item
, unsigned int model_col
)
1059 // This adds GTK+'s missing MVC logic for ValueChanged
1061 for (index
= 0; index
< m_owner
->GetColumnCount(); index
++)
1063 wxDataViewColumn
*column
= m_owner
->GetColumn( index
);
1064 if (column
->GetModelColumn() == model_col
)
1066 GtkTreeView
*widget
= GTK_TREE_VIEW(m_owner
->m_treeview
);
1067 GtkTreeViewColumn
*gcolumn
= GTK_TREE_VIEW_COLUMN(column
->GetGtkHandle());
1071 iter
.stamp
= m_wxgtk_model
->stamp
;
1072 iter
.user_data
= (gpointer
) item
.GetID();
1073 GtkTreePath
*path
= wxgtk_tree_model_get_path(
1074 GTK_TREE_MODEL(m_wxgtk_model
), &iter
);
1075 GdkRectangle cell_area
;
1076 gtk_tree_view_get_cell_area( widget
, path
, gcolumn
, &cell_area
);
1077 gtk_tree_path_free( path
);
1079 GtkAdjustment
* hadjust
= gtk_tree_view_get_hadjustment( widget
);
1080 double d
= gtk_adjustment_get_value( hadjust
);
1081 int xdiff
= (int) d
;
1083 int ydiff
= gcolumn
->button
->allocation
.height
;
1085 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
1086 cell_area
.x
- xdiff
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
1093 bool wxGtkDataViewModelNotifier::Cleared()
1098 // ---------------------------------------------------------
1099 // wxDataViewRenderer
1100 // ---------------------------------------------------------
1102 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
1104 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1106 wxDataViewRendererBase( varianttype
, mode
, align
)
1110 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1111 // after the m_renderer pointer has been initialized
1114 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode
)
1116 GtkCellRendererMode gtkMode
;
1119 case wxDATAVIEW_CELL_INERT
:
1120 gtkMode
= GTK_CELL_RENDERER_MODE_INERT
;
1122 case wxDATAVIEW_CELL_ACTIVATABLE
:
1123 gtkMode
= GTK_CELL_RENDERER_MODE_ACTIVATABLE
;
1125 case wxDATAVIEW_CELL_EDITABLE
:
1126 gtkMode
= GTK_CELL_RENDERER_MODE_EDITABLE
;
1130 GValue gvalue
= { 0, };
1131 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1132 g_value_set_enum( &gvalue
, gtkMode
);
1133 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1134 g_value_unset( &gvalue
);
1137 wxDataViewCellMode
wxDataViewRenderer::GetMode() const
1139 wxDataViewCellMode ret
;
1142 g_object_get( G_OBJECT(m_renderer
), "mode", &gvalue
, NULL
);
1144 switch (g_value_get_enum(&gvalue
))
1146 case GTK_CELL_RENDERER_MODE_INERT
:
1147 ret
= wxDATAVIEW_CELL_INERT
;
1149 case GTK_CELL_RENDERER_MODE_ACTIVATABLE
:
1150 ret
= wxDATAVIEW_CELL_ACTIVATABLE
;
1152 case GTK_CELL_RENDERER_MODE_EDITABLE
:
1153 ret
= wxDATAVIEW_CELL_EDITABLE
;
1157 g_value_unset( &gvalue
);
1162 void wxDataViewRenderer::SetAlignment( int align
)
1164 // horizontal alignment:
1166 gfloat xalign
= 0.0;
1167 if (align
& wxALIGN_RIGHT
)
1169 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1172 GValue gvalue
= { 0, };
1173 g_value_init( &gvalue
, G_TYPE_FLOAT
);
1174 g_value_set_float( &gvalue
, xalign
);
1175 g_object_set_property( G_OBJECT(m_renderer
), "xalign", &gvalue
);
1176 g_value_unset( &gvalue
);
1178 // vertical alignment:
1180 gfloat yalign
= 0.0;
1181 if (align
& wxALIGN_BOTTOM
)
1183 else if (align
& wxALIGN_CENTER_VERTICAL
)
1186 GValue gvalue2
= { 0, };
1187 g_value_init( &gvalue2
, G_TYPE_FLOAT
);
1188 g_value_set_float( &gvalue2
, yalign
);
1189 g_object_set_property( G_OBJECT(m_renderer
), "yalign", &gvalue2
);
1190 g_value_unset( &gvalue2
);
1193 int wxDataViewRenderer::GetAlignment() const
1198 // horizontal alignment:
1200 g_object_get( G_OBJECT(m_renderer
), "xalign", &gvalue
, NULL
);
1201 float xalign
= g_value_get_float( &gvalue
);
1203 ret
|= wxALIGN_LEFT
;
1204 else if (xalign
== 0.5)
1205 ret
|= wxALIGN_CENTER_HORIZONTAL
;
1207 ret
|= wxALIGN_RIGHT
;
1208 g_value_unset( &gvalue
);
1211 // vertical alignment:
1213 g_object_get( G_OBJECT(m_renderer
), "yalign", &gvalue
, NULL
);
1214 float yalign
= g_value_get_float( &gvalue
);
1217 else if (yalign
== 0.5)
1218 ret
|= wxALIGN_CENTER_VERTICAL
;
1220 ret
|= wxALIGN_BOTTOM
;
1221 g_value_unset( &gvalue
);
1228 // ---------------------------------------------------------
1229 // wxDataViewTextRenderer
1230 // ---------------------------------------------------------
1233 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1234 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
1237 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
1238 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
1240 wxDataViewTextRenderer
*cell
= (wxDataViewTextRenderer
*) user_data
;
1242 wxString tmp
= wxGTK_CONV_BACK_FONT(arg2
, cell
->GetOwner()->GetOwner()->GetFont());
1243 wxVariant value
= tmp
;
1244 if (!cell
->Validate( value
))
1247 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1249 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
1251 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, path
);
1252 wxDataViewItem
item( (void*) iter
.user_data
);;
1253 gtk_tree_path_free( path
);
1255 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1257 model
->SetValue( value
, item
, model_col
);
1258 model
->ValueChanged( item
, model_col
);
1261 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
1263 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1265 wxDataViewRenderer( varianttype
, mode
, align
)
1267 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_text_new();
1269 if (mode
& wxDATAVIEW_CELL_EDITABLE
)
1271 GValue gvalue
= { 0, };
1272 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1273 g_value_set_boolean( &gvalue
, true );
1274 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
1275 g_value_unset( &gvalue
);
1277 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
1281 SetAlignment(align
);
1284 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
1286 wxString tmp
= value
;
1288 GValue gvalue
= { 0, };
1289 g_value_init( &gvalue
, G_TYPE_STRING
);
1290 g_value_set_string( &gvalue
, wxGTK_CONV_FONT( tmp
, GetOwner()->GetOwner()->GetFont() ) );
1291 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1292 g_value_unset( &gvalue
);
1297 bool wxDataViewTextRenderer::GetValue( wxVariant
&value
) const
1299 GValue gvalue
= { 0, };
1300 g_value_init( &gvalue
, G_TYPE_STRING
);
1301 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1302 wxString tmp
= wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue
),
1303 wx_const_cast(wxDataViewTextRenderer
*, this)->GetOwner()->GetOwner()->GetFont() );
1304 g_value_unset( &gvalue
);
1311 void wxDataViewTextRenderer::SetAlignment( int align
)
1313 wxDataViewRenderer::SetAlignment(align
);
1315 if (gtk_check_version(2,10,0))
1318 // horizontal alignment:
1319 PangoAlignment pangoAlign
= PANGO_ALIGN_LEFT
;
1320 if (align
& wxALIGN_RIGHT
)
1321 pangoAlign
= PANGO_ALIGN_RIGHT
;
1322 else if (align
& wxALIGN_CENTER_HORIZONTAL
)
1323 pangoAlign
= PANGO_ALIGN_CENTER
;
1325 GValue gvalue
= { 0, };
1326 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1327 g_value_set_enum( &gvalue
, pangoAlign
);
1328 g_object_set_property( G_OBJECT(m_renderer
), "alignment", &gvalue
);
1329 g_value_unset( &gvalue
);
1332 // ---------------------------------------------------------
1333 // wxDataViewBitmapRenderer
1334 // ---------------------------------------------------------
1336 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
1338 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
, wxDataViewCellMode mode
,
1340 wxDataViewRenderer( varianttype
, mode
, align
)
1342 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_pixbuf_new();
1345 SetAlignment(align
);
1348 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
1350 if (value
.GetType() == wxT("wxBitmap"))
1355 // This may create a Pixbuf representation in the
1356 // wxBitmap object (and it will stay there)
1357 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1359 GValue gvalue
= { 0, };
1360 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1361 g_value_set_object( &gvalue
, pixbuf
);
1362 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1363 g_value_unset( &gvalue
);
1368 if (value
.GetType() == wxT("wxIcon"))
1373 // This may create a Pixbuf representation in the
1374 // wxBitmap object (and it will stay there)
1375 GdkPixbuf
*pixbuf
= bitmap
.GetPixbuf();
1377 GValue gvalue
= { 0, };
1378 g_value_init( &gvalue
, G_TYPE_OBJECT
);
1379 g_value_set_object( &gvalue
, pixbuf
);
1380 g_object_set_property( G_OBJECT(m_renderer
), "pixbuf", &gvalue
);
1381 g_value_unset( &gvalue
);
1389 bool wxDataViewBitmapRenderer::GetValue( wxVariant
&value
) const
1394 // ---------------------------------------------------------
1395 // wxDataViewToggleRenderer
1396 // ---------------------------------------------------------
1399 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1400 gchar
*path
, gpointer user_data
);
1403 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
1404 gchar
*path
, gpointer user_data
)
1406 wxDataViewToggleRenderer
*cell
= (wxDataViewToggleRenderer
*) user_data
;
1409 GValue gvalue
= { 0, };
1410 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1411 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
1412 bool tmp
= g_value_get_boolean( &gvalue
);
1413 g_value_unset( &gvalue
);
1417 wxVariant value
= tmp
;
1418 if (!cell
->Validate( value
))
1421 wxDataViewModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
1423 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
1425 cell
->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter
, gtk_path
);
1426 wxDataViewItem
item( (void*) iter
.user_data
);;
1427 gtk_tree_path_free( gtk_path
);
1429 unsigned int model_col
= cell
->GetOwner()->GetModelColumn();
1431 model
->SetValue( value
, item
, model_col
);
1432 model
->ValueChanged( item
, model_col
);
1435 IMPLEMENT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1437 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1438 wxDataViewCellMode mode
, int align
) :
1439 wxDataViewRenderer( varianttype
, mode
, align
)
1441 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_toggle_new();
1443 if (mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1445 g_signal_connect_after( m_renderer
, "toggled",
1446 G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1450 GValue gvalue
= { 0, };
1451 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1452 g_value_set_boolean( &gvalue
, false );
1453 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1454 g_value_unset( &gvalue
);
1458 SetAlignment(align
);
1461 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1465 GValue gvalue
= { 0, };
1466 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1467 g_value_set_boolean( &gvalue
, tmp
);
1468 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1469 g_value_unset( &gvalue
);
1474 bool wxDataViewToggleRenderer::GetValue( wxVariant
&value
) const
1476 GValue gvalue
= { 0, };
1477 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1478 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1479 bool tmp
= g_value_get_boolean( &gvalue
);
1480 g_value_unset( &gvalue
);
1487 // ---------------------------------------------------------
1488 // wxDataViewCustomRenderer
1489 // ---------------------------------------------------------
1491 class wxDataViewCtrlDC
: public wxWindowDC
1494 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1496 GtkWidget
*widget
= window
->m_treeview
;
1500 m_context
= window
->GtkGetPangoDefaultContext();
1501 m_layout
= pango_layout_new( m_context
);
1502 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1504 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1506 // Set m_window later
1508 // m_owner = window;
1512 // ---------------------------------------------------------
1513 // wxDataViewCustomRenderer
1514 // ---------------------------------------------------------
1516 IMPLEMENT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
1518 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
1519 wxDataViewCellMode mode
, int align
,
1521 wxDataViewRenderer( varianttype
, mode
, align
)
1531 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode
, int align
)
1533 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1534 renderer
->cell
= this;
1536 m_renderer
= (GtkCellRenderer
*) renderer
;
1539 SetAlignment(align
);
1544 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1550 wxDC
*wxDataViewCustomRenderer::GetDC()
1554 if (GetOwner() == NULL
)
1556 if (GetOwner()->GetOwner() == NULL
)
1558 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1564 // ---------------------------------------------------------
1565 // wxDataViewProgressRenderer
1566 // ---------------------------------------------------------
1568 IMPLEMENT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
1570 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1571 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1572 wxDataViewCustomRenderer( varianttype
, mode
, align
, true )
1578 if (!gtk_check_version(2,6,0))
1580 m_renderer
= (GtkCellRenderer
*) gtk_cell_renderer_progress_new();
1582 GValue gvalue
= { 0, };
1583 g_value_init( &gvalue
, G_TYPE_STRING
);
1585 // FIXME: font encoding support
1586 g_value_set_string( &gvalue
, wxGTK_CONV_SYS(m_label
) );
1587 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1588 g_value_unset( &gvalue
);
1591 SetAlignment(align
);
1596 // Use custom cell code
1597 wxDataViewCustomRenderer::Init(mode
, align
);
1601 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1605 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1608 if (!gtk_check_version(2,6,0))
1610 gint tmp
= (long) value
;
1611 GValue gvalue
= { 0, };
1612 g_value_init( &gvalue
, G_TYPE_INT
);
1613 g_value_set_int( &gvalue
, tmp
);
1614 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1615 g_value_unset( &gvalue
);
1620 m_value
= (long) value
;
1622 if (m_value
< 0) m_value
= 0;
1623 if (m_value
> 100) m_value
= 100;
1629 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1634 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1636 double pct
= (double)m_value
/ 100.0;
1638 bar
.width
= (int)(cell
.width
* pct
);
1639 dc
->SetPen( *wxTRANSPARENT_PEN
);
1640 dc
->SetBrush( *wxBLUE_BRUSH
);
1641 dc
->DrawRectangle( bar
);
1643 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1644 dc
->SetPen( *wxBLACK_PEN
);
1645 dc
->DrawRectangle( cell
);
1650 wxSize
wxDataViewProgressRenderer::GetSize() const
1652 return wxSize(40,12);
1655 // ---------------------------------------------------------
1656 // wxDataViewDateRenderer
1657 // ---------------------------------------------------------
1659 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1662 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1663 wxDataViewModel
*model
, const wxDataViewItem
&item
, unsigned int col
) :
1664 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1669 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1670 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1671 sizer
->Add( m_cal
, 1, wxGROW
);
1676 virtual void OnDismiss()
1680 void OnCalendar( wxCalendarEvent
&event
);
1682 wxCalendarCtrl
*m_cal
;
1683 wxDataViewModel
*m_model
;
1684 wxDataViewItem m_item
;
1688 DECLARE_EVENT_TABLE()
1691 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1692 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar
)
1695 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1697 wxDateTime date
= event
.GetDate();
1698 wxVariant value
= date
;
1699 m_model
->SetValue( value
, m_item
, m_col
);
1700 m_model
->ValueChanged( m_item
, m_col
);
1704 IMPLEMENT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1706 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1707 wxDataViewCellMode mode
, int align
) :
1708 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1711 SetAlignment(align
);
1714 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1716 m_date
= value
.GetDateTime();
1721 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1726 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1728 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1729 wxString tmp
= m_date
.FormatDate();
1730 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1735 wxSize
wxDataViewDateRenderer::GetSize() const
1737 wxString tmp
= m_date
.FormatDate();
1739 GetView()->GetTextExtent( tmp
, &x
, &y
, &d
);
1740 return wxSize(x
,y
+d
);
1743 bool wxDataViewDateRenderer::Activate( wxRect cell
, wxDataViewModel
*model
,
1744 const wxDataViewItem
&item
, unsigned int col
)
1747 model
->GetValue( variant
, item
, col
);
1748 wxDateTime value
= variant
.GetDateTime();
1750 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1751 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1752 wxPoint pos
= wxGetMousePosition();
1755 popup
->Popup( popup
->m_cal
);
1760 // ---------------------------------------------------------
1762 // ---------------------------------------------------------
1766 gtk_dataview_header_button_press_callback( GtkWidget
*widget
,
1767 GdkEventButton
*gdk_event
,
1768 wxDataViewColumn
*column
)
1770 if (gdk_event
->type
!= GDK_BUTTON_PRESS
)
1773 if (gdk_event
->button
== 1)
1775 wxDataViewCtrl
*dv
= column
->GetOwner();
1776 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, dv
->GetId() );
1777 event
.SetDataViewColumn( column
);
1778 event
.SetModel( dv
->GetModel() );
1779 if (dv
->GetEventHandler()->ProcessEvent( event
))
1787 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1788 GtkCellRenderer
*cell
,
1789 GtkTreeModel
*model
,
1795 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1796 GtkCellRenderer
*renderer
,
1797 GtkTreeModel
*model
,
1801 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model
));
1802 GtkWxTreeModel
*tree_model
= (GtkWxTreeModel
*) model
;
1804 wxDataViewRenderer
*cell
= (wxDataViewRenderer
*) data
;
1806 wxDataViewItem
item( (void*) iter
->user_data
);
1809 tree_model
->internal
->GetDataViewModel()->GetValue( value
, item
, cell
->GetOwner()->GetModelColumn() );
1811 if (value
.GetType() != cell
->GetVariantType())
1812 wxLogError( wxT("Wrong type, required: %s but: %s"),
1813 value
.GetType().c_str(),
1814 cell
->GetVariantType().c_str() );
1816 cell
->SetValue( value
);
1819 wxListItemAttr attr
;
1820 tree_model
->model
->GetAttr( attr
, cell
->GetOwner()->GetModelColumn(), model_row
);
1822 if (attr
.HasBackgroundColour())
1824 wxColour colour
= attr
.GetBackgroundColour();
1825 const GdkColor
* const gcol
= colour
.GetColor();
1827 GValue gvalue
= { 0, };
1828 g_value_init( &gvalue
, GDK_TYPE_COLOR
);
1829 g_value_set_boxed( &gvalue
, gcol
);
1830 g_object_set_property( G_OBJECT(renderer
), "cell-background_gdk", &gvalue
);
1831 g_value_unset( &gvalue
);
1835 GValue gvalue
= { 0, };
1836 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1837 g_value_set_boolean( &gvalue
, FALSE
);
1838 g_object_set_property( G_OBJECT(renderer
), "cell-background-set", &gvalue
);
1839 g_value_unset( &gvalue
);
1845 IMPLEMENT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1847 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1848 unsigned int model_column
, int width
,
1849 wxAlignment align
, int flags
) :
1850 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1852 Init( align
, flags
, width
);
1854 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column
), TRUE
);
1858 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1859 unsigned int model_column
, int width
,
1860 wxAlignment align
, int flags
) :
1861 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1863 Init( align
, flags
, width
);
1865 SetBitmap( bitmap
);
1868 void wxDataViewColumn::Init(wxAlignment align
, int flags
, int width
)
1870 m_isConnected
= false;
1872 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) GetRenderer()->GetGtkHandle();
1873 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1874 m_column
= (GtkWidget
*) column
;
1877 SetAlignment( align
);
1879 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
1880 // as GTK+ is smart and unless explicitely told, will set the minimal
1881 // width to the title's lenght, which is a better default
1883 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
1884 // that we use for the wxDataViewCtrl
1885 gtk_tree_view_column_set_fixed_width( column
, width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1886 gtk_tree_view_column_set_sizing( column
, GTK_TREE_VIEW_COLUMN_FIXED
);
1888 gtk_tree_view_column_pack_end( column
, renderer
, TRUE
);
1890 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1891 wxGtkTreeCellDataFunc
, (gpointer
) GetRenderer(), NULL
);
1894 wxDataViewColumn::~wxDataViewColumn()
1898 void wxDataViewColumn::OnInternalIdle()
1903 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview
))
1905 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1908 g_signal_connect(column
->button
, "button_press_event",
1909 G_CALLBACK (gtk_dataview_header_button_press_callback
), this);
1911 m_isConnected
= true;
1916 void wxDataViewColumn::SetOwner( wxDataViewCtrl
*owner
)
1918 wxDataViewColumnBase::SetOwner( owner
);
1920 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1922 gtk_tree_view_column_set_title( column
, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
1925 void wxDataViewColumn::SetTitle( const wxString
&title
)
1927 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1931 // disconnect before column->button gets recreated
1932 g_signal_handlers_disconnect_by_func( column
->button
,
1933 (GtkWidget
*) gtk_dataview_header_button_press_callback
, this);
1935 m_isConnected
= false;
1938 // FIXME: can it really happen that we don't have the owner here??
1939 wxDataViewCtrl
*ctrl
= GetOwner();
1940 gtk_tree_view_column_set_title( column
, ctrl
? wxGTK_CONV_FONT(title
, ctrl
->GetFont())
1941 : wxGTK_CONV_SYS(title
) );
1943 gtk_tree_view_column_set_widget( column
, NULL
);
1946 wxString
wxDataViewColumn::GetTitle() const
1948 const gchar
*str
= gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column
) );
1949 return wxConvFileName
->cMB2WX(str
);
1952 void wxDataViewColumn::SetBitmap( const wxBitmap
&bitmap
)
1954 wxDataViewColumnBase::SetBitmap( bitmap
);
1956 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1959 GtkImage
*gtk_image
= GTK_IMAGE( gtk_image_new() );
1961 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1962 if (bitmap
.GetMask())
1963 mask
= bitmap
.GetMask()->GetBitmap();
1965 if (bitmap
.HasPixbuf())
1967 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image
),
1968 bitmap
.GetPixbuf());
1972 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image
),
1973 bitmap
.GetPixmap(), mask
);
1975 gtk_widget_show( GTK_WIDGET(gtk_image
) );
1977 gtk_tree_view_column_set_widget( column
, GTK_WIDGET(gtk_image
) );
1981 gtk_tree_view_column_set_widget( column
, NULL
);
1985 void wxDataViewColumn::SetHidden( bool hidden
)
1987 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column
), !hidden
);
1990 void wxDataViewColumn::SetResizeable( bool resizeable
)
1992 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column
), resizeable
);
1995 void wxDataViewColumn::SetAlignment( wxAlignment align
)
1997 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
1999 gfloat xalign
= 0.0;
2000 if (align
== wxALIGN_RIGHT
)
2002 if (align
== wxALIGN_CENTER_HORIZONTAL
||
2003 align
== wxALIGN_CENTER
)
2006 gtk_tree_view_column_set_alignment( column
, xalign
);
2009 wxAlignment
wxDataViewColumn::GetAlignment() const
2011 gfloat xalign
= gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column
) );
2014 return wxALIGN_RIGHT
;
2016 return wxALIGN_CENTER_HORIZONTAL
;
2018 return wxALIGN_LEFT
;
2021 void wxDataViewColumn::SetSortable( bool sortable
)
2023 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2026 gtk_tree_view_column_set_sort_column_id( column
, GetModelColumn() );
2028 gtk_tree_view_column_set_sort_column_id( column
, -1 );
2031 bool wxDataViewColumn::IsSortable() const
2033 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2034 return (gtk_tree_view_column_get_sort_column_id( column
) != -1);
2037 bool wxDataViewColumn::IsResizeable() const
2039 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2040 return gtk_tree_view_column_get_resizable( column
);
2043 bool wxDataViewColumn::IsHidden() const
2045 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2046 return !gtk_tree_view_column_get_visible( column
);
2049 void wxDataViewColumn::SetSortOrder( bool ascending
)
2051 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2054 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_ASCENDING
);
2056 gtk_tree_view_column_set_sort_order( column
, GTK_SORT_DESCENDING
);
2059 bool wxDataViewColumn::IsSortOrderAscending() const
2061 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(m_column
);
2063 return (gtk_tree_view_column_get_sort_order( column
) != GTK_SORT_DESCENDING
);
2066 void wxDataViewColumn::SetMinWidth( int width
)
2068 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2071 int wxDataViewColumn::GetMinWidth() const
2073 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2076 int wxDataViewColumn::GetWidth() const
2078 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column
) );
2081 void wxDataViewColumn::SetWidth( int width
)
2083 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column
), width
);
2087 //-----------------------------------------------------------------------------
2088 // wxGtkTreeModelNode
2089 //-----------------------------------------------------------------------------
2091 void wxGtkTreeModelNode::Resort()
2093 size_t count
= m_children
->GetCount();
2099 wxGtkTreeModelNode
*node
= m_children
->Item( 0 );
2104 wxGtkTreeModelNodes
*new_array
= new wxGtkTreeModelNodes( wxGtkTreeModelNodeCmp
);
2108 for (pos
= 0; pos
< count
; pos
++)
2109 new_array
->Add( m_children
->Item( pos
) );
2112 gint
*new_order
= new gint
[count
];
2114 for (pos
= 0; pos
< count
; pos
++)
2116 wxGtkTreeModelNode
*node
= new_array
->Item( pos
);
2118 for (old_pos
= 0; old_pos
< count
; old_pos
++)
2120 if (node
== m_children
->Item(old_pos
))
2122 new_order
[pos
] = old_pos
;
2128 // for (pos = 0; pos < count; pos++)
2129 // m_children->Clear();
2132 m_children
= new_array
;
2134 GtkTreeModel
*gtk_tree_model
= GTK_TREE_MODEL( m_internal
->GetGtkModel() );
2137 iter
.user_data
= (gpointer
) GetItem().GetID();
2138 iter
.stamp
= m_internal
->GetGtkModel()->stamp
;
2139 GtkTreePath
*path
= wxgtk_tree_model_get_path( gtk_tree_model
, &iter
);
2141 gtk_tree_model_rows_reordered( gtk_tree_model
, path
, &iter
, new_order
);
2143 gtk_tree_path_free (path
);
2145 delete [] new_order
;
2147 for (pos
= 0; pos
< count
; pos
++)
2149 wxGtkTreeModelNode
*node
= m_children
->Item( pos
);
2154 //-----------------------------------------------------------------------------
2155 // wxDataViewCtrlInternal
2156 //-----------------------------------------------------------------------------
2158 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl
*owner
,
2159 wxDataViewModel
*wx_model
, GtkWxTreeModel
*gtk_model
)
2162 m_wx_model
= wx_model
;
2163 m_gtk_model
= gtk_model
;
2168 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2170 g_object_unref( m_gtk_model
);
2173 void wxDataViewCtrlInternal::InitTree()
2175 wxDataViewItem item
;
2176 m_root
= new wxGtkTreeModelNode( NULL
, item
, this );
2178 BuildBranch( m_root
);
2181 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode
*node
)
2183 if (node
->GetChildCount() == 0)
2185 wxDataViewItem child
= m_wx_model
->GetFirstChild( node
->GetItem() );
2186 while (child
.IsOk())
2188 node
->Add( new wxGtkTreeModelNode( node
, child
, this ) );
2189 child
= m_wx_model
->GetNextSibling( child
);
2194 void wxDataViewCtrlInternal::Resort()
2199 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
2201 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2202 parent_node
->Add( new wxGtkTreeModelNode( parent_node
, item
, this ) );
2206 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem
&item
)
2208 wxGtkTreeModelNode
*node
= FindNode( item
);
2209 wxGtkTreeModelNode
*parent
= node
->GetParent();
2211 for (pos
= 0; pos
< parent
->GetChildren().GetCount(); pos
++)
2213 if (node
== parent
->GetChildren().Item( pos
))
2215 parent
->GetChildren().RemoveAt( pos
);
2223 gboolean
wxDataViewCtrlInternal::get_iter( GtkTreeIter
*iter
, GtkTreePath
*path
)
2225 int depth
= gtk_tree_path_get_depth( path
);
2227 wxGtkTreeModelNode
*node
= m_root
;
2230 for (i
= 0; i
< depth
; i
++)
2232 BuildBranch( node
);
2234 gint pos
= gtk_tree_path_get_indices (path
)[i
];
2235 if (pos
< 0) return FALSE
;
2236 if ((size_t)pos
>= node
->GetChildCount()) return FALSE
;
2238 node
= node
->GetChildren().Item( (size_t) pos
);
2241 iter
->stamp
= m_gtk_model
->stamp
;
2242 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2247 GtkTreePath
*wxDataViewCtrlInternal::get_path( GtkTreeIter
*iter
)
2249 GtkTreePath
*retval
= gtk_tree_path_new ();
2251 wxGtkTreeModelNode
*node
= FindNode( iter
);
2252 while (node
->GetParent())
2254 wxGtkTreeModelNode
*parent
= node
->GetParent();
2255 int pos
= parent
->GetChildren().Index( node
);
2257 gtk_tree_path_prepend_index( retval
, pos
);
2265 GtkTreePath
*wxDataViewCtrlInternal::get_path_safe( GtkTreeIter
*iter
)
2267 GtkTreePath
*retval
= gtk_tree_path_new ();
2269 wxGtkTreeModelNode
*node
= FindNode( iter
);
2270 while (node
->GetParent())
2272 wxGtkTreeModelNode
*parent
= node
->GetParent();
2274 for (pos
= 0; pos
< parent
->GetChildren().GetCount(); pos
++)
2276 if (node
== parent
->GetChildren().Item( pos
))
2278 gtk_tree_path_prepend_index( retval
, (int) pos
);
2289 gboolean
wxDataViewCtrlInternal::iter_next( GtkTreeIter
*iter
)
2291 wxGtkTreeModelNode
*node
= FindNode( iter
);
2292 wxGtkTreeModelNode
*parent
= node
->GetParent();
2293 unsigned int pos
= parent
->GetChildren().Index( node
);
2295 if (pos
== parent
->GetChildren().GetCount()-1)
2298 node
= parent
->GetChildren().Item( pos
+1 );
2300 iter
->stamp
= m_gtk_model
->stamp
;
2301 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2306 gboolean
wxDataViewCtrlInternal::iter_children( GtkTreeIter
*iter
, GtkTreeIter
*parent
)
2308 wxDataViewItem
item( (void*) parent
->user_data
);
2310 if (!m_wx_model
->IsContainer( item
))
2313 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2314 BuildBranch( parent_node
);
2316 if (parent_node
->GetChildren().GetCount() == 0)
2319 wxGtkTreeModelNode
*first_child_node
= parent_node
->GetChildren().Item( 0 );
2321 iter
->stamp
= m_gtk_model
->stamp
;
2322 iter
->user_data
= (gpointer
) first_child_node
->GetItem().GetID();
2327 gboolean
wxDataViewCtrlInternal::iter_has_child( GtkTreeIter
*iter
)
2329 wxDataViewItem
item( (void*) iter
->user_data
);
2330 bool res
= m_wx_model
->IsContainer( item
);
2335 wxGtkTreeModelNode
*node
= FindNode( iter
);
2336 BuildBranch( node
);
2338 return (node
->GetChildren().GetCount() > 0);
2341 gint
wxDataViewCtrlInternal::iter_n_children( GtkTreeIter
*iter
)
2343 wxDataViewItem
item( (void*) iter
->user_data
);
2345 if (!m_wx_model
->IsContainer( item
))
2348 wxGtkTreeModelNode
*parent_node
= FindNode( iter
);
2349 BuildBranch( parent_node
);
2351 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2353 return parent_node
->GetChildCount();
2356 gboolean
wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter
*iter
, GtkTreeIter
*parent
, gint n
)
2359 if (parent
) id
= (void*) parent
->user_data
;
2360 wxDataViewItem
item( id
);
2362 if (!m_wx_model
->IsContainer( item
))
2365 wxGtkTreeModelNode
*parent_node
= FindNode( parent
);
2366 BuildBranch( parent_node
);
2368 wxGtkTreeModelNode
*child_node
= parent_node
->GetChildren().Item( (size_t) n
);
2372 // wxPrintf( "iter_nth_child %d\n", n );
2374 iter
->stamp
= m_gtk_model
->stamp
;
2375 iter
->user_data
= (gpointer
) child_node
->GetItem().GetID();
2380 gboolean
wxDataViewCtrlInternal::iter_parent( GtkTreeIter
*iter
, GtkTreeIter
*child
)
2382 wxDataViewItem
item( (void*) child
->user_data
);
2384 wxGtkTreeModelNode
*node
= FindNode( child
);
2385 node
= node
->GetParent();
2389 iter
->stamp
= m_gtk_model
->stamp
;
2390 iter
->user_data
= (gpointer
) node
->GetItem().GetID();
2395 static wxGtkTreeModelNode
*
2396 wxDataViewCtrlInternal_FindNode( wxGtkTreeModelNode
*node
, const wxDataViewItem
&item
)
2398 if (!node
) return NULL
;
2400 size_t count
= node
->GetChildCount();
2402 for (i
= 0; i
< count
; i
++)
2404 wxGtkTreeModelNode
*child
= node
->GetChildren().Item( i
);
2405 if (child
->GetItem().GetID() == item
.GetID())
2407 // wxPrintf( "leave findnode at %d\n", i );
2411 wxGtkTreeModelNode
*node2
= wxDataViewCtrlInternal_FindNode( child
, item
);
2414 // wxPrintf( "branch findnode at %d\n", i );
2422 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( GtkTreeIter
*iter
)
2427 wxDataViewItem
item( (void*) iter
->user_data
);
2431 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_root
, item
);
2435 wxPrintf( "Not found %d\n", (int) iter
->user_data
);
2443 wxGtkTreeModelNode
*wxDataViewCtrlInternal::FindNode( const wxDataViewItem
&item
)
2448 wxGtkTreeModelNode
*result
= wxDataViewCtrlInternal_FindNode( m_root
, item
);
2452 wxPrintf( "Not found %d\n", (int) item
.GetID() );
2460 //-----------------------------------------------------------------------------
2461 // wxDataViewCtrl signal callbacks
2462 //-----------------------------------------------------------------------------
2465 wxdataview_selection_changed_callback( GtkTreeSelection
* selection
, wxDataViewCtrl
*dv
)
2467 if (!GTK_WIDGET_REALIZED(dv
->m_widget
))
2470 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
, dv
->GetId() );
2472 event
.SetModel( dv
->GetModel() );
2473 dv
->GetEventHandler()->ProcessEvent( event
);
2477 wxdataview_row_activated_callback( GtkTreeView
* treeview
, GtkTreePath
*path
,
2478 GtkTreeViewColumn
*column
, wxDataViewCtrl
*dv
)
2480 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, dv
->GetId() );
2483 dv
->GtkGetInternal()->get_iter( &iter
, path
);
2484 wxDataViewItem
item( (void*) iter
.user_data
);;
2485 event
.SetItem( item
);
2486 event
.SetModel( dv
->GetModel() );
2487 dv
->GetEventHandler()->ProcessEvent( event
);
2490 //-----------------------------------------------------------------------------
2492 //-----------------------------------------------------------------------------
2494 //-----------------------------------------------------------------------------
2495 // InsertChild for wxDataViewCtrl
2496 //-----------------------------------------------------------------------------
2498 static void wxInsertChildInDataViewCtrl( wxWindowGTK
* parent
, wxWindowGTK
* child
)
2500 wxDataViewCtrl
* dvc
= (wxDataViewCtrl
*) parent
;
2501 GtkWidget
*treeview
= dvc
->GtkGetTreeView();
2503 // Insert widget in GtkTreeView
2504 if (GTK_WIDGET_REALIZED(treeview
))
2505 gtk_widget_set_parent_window( child
->m_widget
,
2506 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview
) ) );
2507 gtk_widget_set_parent( child
->m_widget
, treeview
);
2511 void gtk_dataviewctrl_size_callback( GtkWidget
*WXUNUSED(widget
),
2512 GtkAllocation
*alloc
,
2513 wxDataViewCtrl
*win
)
2516 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
2519 wxWindow
*child
= node
->GetData();
2522 gtk_widget_size_request( child
->m_widget
, &req
);
2524 GtkAllocation alloc
;
2525 alloc
.x
= child
->m_x
;
2526 alloc
.y
= child
->m_y
;
2527 alloc
.width
= child
->m_width
;
2528 alloc
.height
= child
->m_height
;
2529 gtk_widget_size_allocate( child
->m_widget
, &alloc
);
2531 node
= node
->GetNext();
2537 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
2539 wxDataViewCtrl::~wxDataViewCtrl()
2542 GetModel()->RemoveNotifier( m_notifier
);
2544 // remove the model from the GtkTreeView before it gets destroyed by the
2545 // wxDataViewCtrlBase's dtor
2546 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), NULL
);
2551 void wxDataViewCtrl::Init()
2556 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
2557 const wxPoint
& pos
, const wxSize
& size
,
2558 long style
, const wxValidator
& validator
)
2562 if (!PreCreation( parent
, pos
, size
) ||
2563 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
2565 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
2569 m_insertCallback
= wxInsertChildInDataViewCtrl
;
2571 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
2573 GtkScrolledWindowSetBorder(m_widget
, style
);
2575 m_treeview
= gtk_tree_view_new();
2576 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
2578 g_signal_connect (m_treeview
, "size_allocate",
2579 G_CALLBACK (gtk_dataviewctrl_size_callback
), this);
2582 if (!gtk_check_version(2,6,0))
2583 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview
), TRUE
);
2586 if (style
& wxDV_MULTIPLE
)
2588 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2589 gtk_tree_selection_set_mode( selection
, GTK_SELECTION_MULTIPLE
);
2592 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_NO_HEADER
) == 0 );
2595 if (!gtk_check_version(2,10,0))
2597 GtkTreeViewGridLines grid
= GTK_TREE_VIEW_GRID_LINES_NONE
;
2599 if ((style
& wxDV_HORIZ_RULES
) != 0 &&
2600 (style
& wxDV_VERT_RULES
) != 0)
2601 grid
= GTK_TREE_VIEW_GRID_LINES_BOTH
;
2602 else if (style
& wxDV_VERT_RULES
)
2603 grid
= GTK_TREE_VIEW_GRID_LINES_VERTICAL
;
2604 else if (style
& wxDV_HORIZ_RULES
)
2605 grid
= GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
;
2607 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview
), grid
);
2612 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview
), (style
& wxDV_HORIZ_RULES
) != 0 );
2615 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
2616 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
2617 gtk_widget_show (m_treeview
);
2619 m_parent
->DoAddChild( this );
2623 GtkEnableSelectionEvents();
2625 g_signal_connect_after (m_treeview
, "row_activated",
2626 G_CALLBACK (wxdataview_row_activated_callback
), this);
2631 void wxDataViewCtrl::OnInternalIdle()
2633 wxWindow::OnInternalIdle();
2635 unsigned int cols
= GetColumnCount();
2637 for (i
= 0; i
< cols
; i
++)
2639 wxDataViewColumn
*col
= GetColumn( i
);
2640 col
->OnInternalIdle();
2644 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
2646 if (!wxDataViewCtrlBase::AssociateModel( model
))
2649 GtkWxTreeModel
*gtk_model
= wxgtk_tree_model_new();
2650 m_internal
= new wxDataViewCtrlInternal( this, model
, gtk_model
);
2651 gtk_model
->internal
= m_internal
;
2653 m_notifier
= new wxGtkDataViewModelNotifier( gtk_model
, model
, this );
2655 model
->AddNotifier( m_notifier
);
2657 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_model
) );
2659 // unref in wxDataViewCtrlInternal
2660 // g_object_unref( gtk_model );
2665 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
2667 if (!wxDataViewCtrlBase::AppendColumn(col
))
2670 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
2672 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
2677 wxDataViewItem
wxDataViewCtrl::GetSelection()
2679 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2681 if (m_windowStyle
& wxDV_MULTIPLE
)
2687 if (gtk_tree_selection_get_selected( selection
, NULL
, &iter
))
2689 wxDataViewItem
item( (void*) iter
.user_data
);
2694 return wxDataViewItem(0);
2697 void wxDataViewCtrl::DoSetExpanderColumn()
2701 void wxDataViewCtrl::DoSetIndent()
2705 void wxDataViewCtrl::GtkDisableSelectionEvents()
2707 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2708 g_signal_connect_after (selection
, "changed",
2709 G_CALLBACK (wxdataview_selection_changed_callback
), this);
2712 void wxDataViewCtrl::GtkEnableSelectionEvents()
2714 GtkTreeSelection
*selection
= gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview
) );
2715 g_signal_handlers_disconnect_by_func( selection
,
2716 (gpointer
) (wxdataview_selection_changed_callback
), this);
2721 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
2723 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
);
2728 // !wxUSE_GENERICDATAVIEWCTRL
2731 // wxUSE_DATAVIEWCTRL