]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
Disable some wxWebView tests that fail on the buildbot but not locally.
[wxWidgets.git] / src / gtk / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataview.cpp
3 // Purpose: wxDataViewCtrl GTK+2 implementation
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_DATAVIEWCTRL
14
15 #include "wx/dataview.h"
16
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
18
19 #ifndef WX_PRECOMP
20 #include "wx/log.h"
21 #include "wx/dcclient.h"
22 #include "wx/sizer.h"
23 #include "wx/settings.h"
24 #include "wx/crt.h"
25 #endif
26
27 #include "wx/stockitem.h"
28 #include "wx/popupwin.h"
29 #include "wx/listimpl.cpp"
30
31 #include "wx/gtk/private.h"
32 #include "wx/gtk/dc.h"
33 #include "wx/gtk/dcclient.h"
34
35 #include "wx/gtk/private/gdkconv.h"
36 #include "wx/gtk/private/list.h"
37 #include "wx/gtk/private/event.h"
38 using namespace wxGTKImpl;
39
40 class wxGtkDataViewModelNotifier;
41
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44
45 static wxDataViewCtrlInternal *gs_internal = NULL;
46
47 class wxGtkTreeModelNode;
48
49 extern "C" {
50 typedef struct _GtkWxTreeModel GtkWxTreeModel;
51 }
52
53 // ----------------------------------------------------------------------------
54 // wxGtkTreePath: self-destroying GtkTreePath
55 // ----------------------------------------------------------------------------
56
57 // Usually this object is initialized with the associated GtkTreePath
58 // immediately when it's constructed but it can also be changed later either by
59 // using Assign() or by getting the pointer to the internally stored pointer
60 // value using ByRef(). The latter should be avoided but is very convenient
61 // when using GTK functions with GtkTreePath output parameters.
62 class wxGtkTreePath
63 {
64 public:
65 // Ctor takes ownership of the given path and will free it if non-NULL.
66 wxGtkTreePath(GtkTreePath *path = NULL) : m_path(path) { }
67
68 // Creates a tree path for the given string path.
69 wxGtkTreePath(const gchar *strpath)
70 : m_path(gtk_tree_path_new_from_string(strpath))
71 {
72 }
73
74 // Set the stored pointer if not done by ctor.
75 void Assign(GtkTreePath *path)
76 {
77 wxASSERT_MSG( !m_path, "shouldn't be already initialized" );
78
79 m_path = path;
80 }
81
82 // Return the pointer to the internally stored pointer. This should only be
83 // used to initialize the object by passing it to some GTK function.
84 GtkTreePath **ByRef()
85 {
86 wxASSERT_MSG( !m_path, "shouldn't be already initialized" );
87
88 return &m_path;
89 }
90
91
92 operator GtkTreePath *() const { return m_path; }
93
94 ~wxGtkTreePath() { if ( m_path ) gtk_tree_path_free(m_path); }
95
96 private:
97 GtkTreePath *m_path;
98
99 wxDECLARE_NO_COPY_CLASS(wxGtkTreePath);
100 };
101
102 // ----------------------------------------------------------------------------
103 // wxGtkTreePathList: self-destroying list of GtkTreePath objects.
104 // ----------------------------------------------------------------------------
105
106 class wxGtkTreePathList : public wxGtkList
107 {
108 public:
109 // Ctor takes ownership of the list.
110 explicit wxGtkTreePathList(GList* list)
111 : wxGtkList(list)
112 {
113 }
114
115 ~wxGtkTreePathList()
116 {
117 // Delete the list contents, wxGtkList will delete the list itself.
118 g_list_foreach(m_list, (GFunc)gtk_tree_path_free, NULL);
119 }
120 };
121
122 // ----------------------------------------------------------------------------
123 // wxGtkTreeSelectionLock: prevent selection from changing during the
124 // lifetime of this object
125 // ----------------------------------------------------------------------------
126
127 // Implementation note: it could be expected that setting the selection
128 // function in this class ctor and resetting it back to the old value in its
129 // dtor would work. However currently gtk_tree_selection_get_select_function()
130 // can't be passed NULL (see https://bugzilla.gnome.org/show_bug.cgi?id=626276)
131 // so we can't do this. Instead, we always use the selection function (which
132 // imposes extra overhead, albeit minimal one, on all selection operations) and
133 // just set/reset the flag telling it whether it should allow or forbid the
134 // selection.
135 //
136 // Also notice that currently only a single object of this class may exist at
137 // any given moment. It's just simpler like this and we don't need anything
138 // more for now.
139
140 extern "C"
141 gboolean wxdataview_selection_func(GtkTreeSelection * WXUNUSED(selection),
142 GtkTreeModel * WXUNUSED(model),
143 GtkTreePath * WXUNUSED(path),
144 gboolean WXUNUSED(path_currently_selected),
145 gpointer data)
146 {
147 return data == NULL;
148 }
149
150 class wxGtkTreeSelectionLock
151 {
152 public:
153 wxGtkTreeSelectionLock(GtkTreeSelection *selection)
154 : m_selection(selection)
155 {
156 wxASSERT_MSG( !ms_instance, "this class is not reentrant currently" );
157
158 ms_instance = this;
159
160 CheckCurrentSelectionFunc(NULL);
161
162 // Pass some non-NULL pointer as "data" for the callback, it doesn't
163 // matter what it is as long as it's non-NULL.
164 gtk_tree_selection_set_select_function(selection,
165 wxdataview_selection_func,
166 this,
167 NULL);
168 }
169
170 ~wxGtkTreeSelectionLock()
171 {
172 CheckCurrentSelectionFunc(wxdataview_selection_func);
173
174 gtk_tree_selection_set_select_function(m_selection,
175 wxdataview_selection_func,
176 NULL,
177 NULL);
178
179 ms_instance = NULL;
180 }
181
182 private:
183 void CheckCurrentSelectionFunc(GtkTreeSelectionFunc func)
184 {
185 // We can only use gtk_tree_selection_get_select_function() with 2.14+
186 // so check for its availability both during compile- and run-time.
187 #if GTK_CHECK_VERSION(2, 14, 0)
188 if ( gtk_check_version(2, 14, 0) != NULL )
189 return;
190
191 // If this assert is triggered, it means the code elsewhere has called
192 // gtk_tree_selection_set_select_function() but currently doing this
193 // breaks this class so the code here needs to be changed.
194 wxASSERT_MSG
195 (
196 gtk_tree_selection_get_select_function(m_selection) == func,
197 "selection function has changed unexpectedly, review this code!"
198 );
199 #endif // GTK+ 2.14+
200
201 wxUnusedVar(func);
202 }
203
204 static wxGtkTreeSelectionLock *ms_instance;
205
206 GtkTreeSelection * const m_selection;
207
208 wxDECLARE_NO_COPY_CLASS(wxGtkTreeSelectionLock);
209 };
210
211 wxGtkTreeSelectionLock *wxGtkTreeSelectionLock::ms_instance = NULL;
212
213 //-----------------------------------------------------------------------------
214 // wxDataViewCtrlInternal
215 //-----------------------------------------------------------------------------
216
217 WX_DECLARE_LIST(wxDataViewItem, ItemList);
218 WX_DEFINE_LIST(ItemList)
219
220 class wxDataViewCtrlInternal
221 {
222 public:
223 wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model );
224 ~wxDataViewCtrlInternal();
225
226 // model iface
227 GtkTreeModelFlags get_flags();
228 gboolean get_iter( GtkTreeIter *iter, GtkTreePath *path );
229 GtkTreePath *get_path( GtkTreeIter *iter);
230 gboolean iter_next( GtkTreeIter *iter );
231 gboolean iter_children( GtkTreeIter *iter, GtkTreeIter *parent);
232 gboolean iter_has_child( GtkTreeIter *iter );
233 gint iter_n_children( GtkTreeIter *iter );
234 gboolean iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n );
235 gboolean iter_parent( GtkTreeIter *iter, GtkTreeIter *child );
236
237 // dnd iface
238
239 bool EnableDragSource( const wxDataFormat &format );
240 bool EnableDropTarget( const wxDataFormat &format );
241
242 gboolean row_draggable( GtkTreeDragSource *drag_source, GtkTreePath *path );
243 gboolean drag_data_delete( GtkTreeDragSource *drag_source, GtkTreePath* path );
244 gboolean drag_data_get( GtkTreeDragSource *drag_source, GtkTreePath *path,
245 GtkSelectionData *selection_data );
246 gboolean drag_data_received( GtkTreeDragDest *drag_dest, GtkTreePath *dest,
247 GtkSelectionData *selection_data );
248 gboolean row_drop_possible( GtkTreeDragDest *drag_dest, GtkTreePath *dest_path,
249 GtkSelectionData *selection_data );
250
251 // notifactions from wxDataViewModel
252 bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
253 bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
254 bool ItemChanged( const wxDataViewItem &item );
255 bool ValueChanged( const wxDataViewItem &item, unsigned int model_column );
256 bool Cleared();
257 bool BeforeReset();
258 bool AfterReset();
259 void Resort();
260
261 // sorting interface
262 void SetSortOrder( GtkSortType sort_order ) { m_sort_order = sort_order; }
263 GtkSortType GetSortOrder() const { return m_sort_order; }
264
265 void SetSortColumn( int column ) { m_sort_column = column; }
266 int GetSortColumn() const { return m_sort_column; }
267
268 void SetDataViewSortColumn( wxDataViewColumn *column ) { m_dataview_sort_column = column; }
269 wxDataViewColumn *GetDataViewSortColumn() { return m_dataview_sort_column; }
270
271 bool IsSorted() { return (m_sort_column >= 0); }
272
273 // accessors
274 wxDataViewModel* GetDataViewModel() { return m_wx_model; }
275 const wxDataViewModel* GetDataViewModel() const { return m_wx_model; }
276 wxDataViewCtrl* GetOwner() { return m_owner; }
277 GtkWxTreeModel* GetGtkModel() { return m_gtk_model; }
278
279 // item can be deleted already in the model
280 int GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item );
281
282 virtual void OnInternalIdle();
283
284 protected:
285 void InitTree();
286 void ScheduleRefresh();
287
288 wxGtkTreeModelNode *FindNode( const wxDataViewItem &item );
289 wxGtkTreeModelNode *FindNode( GtkTreeIter *iter );
290 wxGtkTreeModelNode *FindParentNode( const wxDataViewItem &item );
291 wxGtkTreeModelNode *FindParentNode( GtkTreeIter *iter );
292 void BuildBranch( wxGtkTreeModelNode *branch );
293
294 private:
295 wxGtkTreeModelNode *m_root;
296 wxDataViewModel *m_wx_model;
297 GtkWxTreeModel *m_gtk_model;
298 wxDataViewCtrl *m_owner;
299 GtkSortType m_sort_order;
300 wxDataViewColumn *m_dataview_sort_column;
301 int m_sort_column;
302
303 GtkTargetEntry m_dragSourceTargetEntry;
304 wxCharBuffer m_dragSourceTargetEntryTarget;
305 wxDataObject *m_dragDataObject;
306
307 GtkTargetEntry m_dropTargetTargetEntry;
308 wxCharBuffer m_dropTargetTargetEntryTarget;
309 wxDataObject *m_dropDataObject;
310
311 wxGtkDataViewModelNotifier *m_notifier;
312
313 bool m_dirty;
314 };
315
316
317 //-----------------------------------------------------------------------------
318 // wxGtkTreeModelNode
319 //-----------------------------------------------------------------------------
320
321 static
322 int LINKAGEMODE wxGtkTreeModelChildCmp( void** id1, void** id2 )
323 {
324 int ret = gs_internal->GetDataViewModel()->Compare( wxDataViewItem(*id1), wxDataViewItem(*id2),
325 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
326
327 return ret;
328 }
329
330 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode*, wxGtkTreeModelNodes );
331 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren );
332
333 class wxGtkTreeModelNode
334 {
335 public:
336 wxGtkTreeModelNode( wxGtkTreeModelNode* parent, const wxDataViewItem &item,
337 wxDataViewCtrlInternal *internal )
338 {
339 m_parent = parent;
340 m_item = item;
341 m_internal = internal;
342 }
343
344 ~wxGtkTreeModelNode()
345 {
346 size_t count = m_nodes.GetCount();
347 size_t i;
348 for (i = 0; i < count; i++)
349 {
350 wxGtkTreeModelNode *child = m_nodes.Item( i );
351 delete child;
352 }
353 }
354
355 void AddNode( wxGtkTreeModelNode* child )
356 {
357 m_nodes.Add( child );
358
359 void *id = child->GetItem().GetID();
360
361 m_children.Add( id );
362
363 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
364 {
365 gs_internal = m_internal;
366 m_children.Sort( &wxGtkTreeModelChildCmp );
367 }
368 }
369
370 void InsertNode( wxGtkTreeModelNode* child, unsigned pos )
371 {
372 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
373 {
374 AddNode(child);
375 return;
376 }
377
378 void *id = child->GetItem().GetID();
379
380 // Insert into m_nodes so that the order of nodes in m_nodes is the
381 // same as the order of their corresponding IDs in m_children:
382 const unsigned int count = m_nodes.GetCount();
383 bool inserted = false;
384 for (unsigned i = 0; i < count; i++)
385 {
386 wxGtkTreeModelNode *node = m_nodes[i];
387 int posInChildren = m_children.Index(node->GetItem().GetID());
388 if ( (unsigned)posInChildren >= pos )
389 {
390 m_nodes.Insert(child, i);
391 inserted = true;
392 break;
393 }
394 }
395 if ( !inserted )
396 m_nodes.Add(child);
397
398 m_children.Insert( id, pos );
399 }
400
401 void AddLeaf( void* id )
402 {
403 InsertLeaf(id, m_children.size());
404 }
405
406 void InsertLeaf( void* id, unsigned pos )
407 {
408 m_children.Insert( id, pos );
409
410 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
411 {
412 gs_internal = m_internal;
413 m_children.Sort( &wxGtkTreeModelChildCmp );
414 }
415 }
416
417 void DeleteChild( void* id )
418 {
419 m_children.Remove( id );
420
421 unsigned int count = m_nodes.GetCount();
422 unsigned int pos;
423 for (pos = 0; pos < count; pos++)
424 {
425 wxGtkTreeModelNode *node = m_nodes.Item( pos );
426 if (node->GetItem().GetID() == id)
427 {
428 m_nodes.RemoveAt( pos );
429 delete node;
430 break;
431 }
432 }
433 }
434
435 // returns position of child node for given item in children list or wxNOT_FOUND
436 int FindChildByItem(const wxDataViewItem& item) const
437 {
438 const void* itemId = item.GetID();
439 const wxGtkTreeModelChildren& nodes = m_children;
440 const int len = nodes.size();
441 for ( int i = 0; i < len; i++ )
442 {
443 if ( nodes[i] == itemId )
444 return i;
445 }
446 return wxNOT_FOUND;
447 }
448
449 wxGtkTreeModelNode* GetParent()
450 { return m_parent; }
451 wxGtkTreeModelNodes &GetNodes()
452 { return m_nodes; }
453 wxGtkTreeModelChildren &GetChildren()
454 { return m_children; }
455
456 unsigned int GetChildCount() const { return m_children.GetCount(); }
457 unsigned int GetNodesCount() const { return m_nodes.GetCount(); }
458
459 wxDataViewItem &GetItem() { return m_item; }
460 wxDataViewCtrlInternal *GetInternal() { return m_internal; }
461
462 void Resort();
463
464 private:
465 wxGtkTreeModelNode *m_parent;
466 wxGtkTreeModelNodes m_nodes;
467 wxGtkTreeModelChildren m_children;
468 wxDataViewItem m_item;
469 wxDataViewCtrlInternal *m_internal;
470 };
471
472
473 //-----------------------------------------------------------------------------
474 // data
475 //-----------------------------------------------------------------------------
476
477 extern bool g_blockEventsOnDrag;
478
479 //-----------------------------------------------------------------------------
480 // define new GTK+ class wxGtkTreeModel
481 //-----------------------------------------------------------------------------
482
483 extern "C" {
484
485 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
486 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
487 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
488 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
489 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
490 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
491
492 GType gtk_wx_tree_model_get_type (void);
493
494 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass;
495
496 struct _GtkWxTreeModel
497 {
498 GObject parent;
499
500 /*< private >*/
501 gint stamp;
502 wxDataViewCtrlInternal *internal;
503 };
504
505 struct _GtkWxTreeModelClass
506 {
507 GObjectClass list_parent_class;
508 };
509
510 static GtkWxTreeModel *wxgtk_tree_model_new (void);
511 static void wxgtk_tree_model_init (GtkWxTreeModel *tree_model);
512 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass);
513
514 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface);
515 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface);
516 static void wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface);
517 static void wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface);
518
519 static void wxgtk_tree_model_finalize (GObject *object);
520 static GtkTreeModelFlags wxgtk_tree_model_get_flags (GtkTreeModel *tree_model);
521 static gint wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model);
522 static GType wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
523 gint index);
524 static gboolean wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
525 GtkTreeIter *iter,
526 GtkTreePath *path);
527 static GtkTreePath *wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
528 GtkTreeIter *iter);
529 static void wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
530 GtkTreeIter *iter,
531 gint column,
532 GValue *value);
533 static gboolean wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
534 GtkTreeIter *iter);
535 static gboolean wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
536 GtkTreeIter *iter,
537 GtkTreeIter *parent);
538 static gboolean wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
539 GtkTreeIter *iter);
540 static gint wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
541 GtkTreeIter *iter);
542 static gboolean wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
543 GtkTreeIter *iter,
544 GtkTreeIter *parent,
545 gint n);
546 static gboolean wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
547 GtkTreeIter *iter,
548 GtkTreeIter *child);
549
550 /* sortable */
551 static gboolean wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
552 gint *sort_column_id,
553 GtkSortType *order);
554 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
555 gint sort_column_id,
556 GtkSortType order);
557 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
558 gint sort_column_id,
559 GtkTreeIterCompareFunc func,
560 gpointer data,
561 GDestroyNotify destroy);
562 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
563 GtkTreeIterCompareFunc func,
564 gpointer data,
565 GDestroyNotify destroy);
566 static gboolean wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable);
567
568 /* drag'n'drop */
569 static gboolean wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source,
570 GtkTreePath *path);
571 static gboolean wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source,
572 GtkTreePath *path);
573 static gboolean wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source,
574 GtkTreePath *path,
575 GtkSelectionData *selection_data);
576 static gboolean wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest,
577 GtkTreePath *dest,
578 GtkSelectionData *selection_data);
579 static gboolean wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest,
580 GtkTreePath *dest_path,
581 GtkSelectionData *selection_data);
582
583
584 static GObjectClass *list_parent_class = NULL;
585
586 GType
587 gtk_wx_tree_model_get_type (void)
588 {
589 static GType tree_model_type = 0;
590
591 if (!tree_model_type)
592 {
593 const GTypeInfo tree_model_info =
594 {
595 sizeof (GtkWxTreeModelClass),
596 NULL, /* base_init */
597 NULL, /* base_finalize */
598 (GClassInitFunc) wxgtk_tree_model_class_init,
599 NULL, /* class_finalize */
600 NULL, /* class_data */
601 sizeof (GtkWxTreeModel),
602 0,
603 (GInstanceInitFunc) wxgtk_tree_model_init,
604 };
605
606 static const GInterfaceInfo tree_model_iface_info =
607 {
608 (GInterfaceInitFunc) wxgtk_tree_model_tree_model_init,
609 NULL,
610 NULL
611 };
612
613 static const GInterfaceInfo sortable_iface_info =
614 {
615 (GInterfaceInitFunc) wxgtk_tree_model_sortable_init,
616 NULL,
617 NULL
618 };
619
620 static const GInterfaceInfo drag_source_iface_info =
621 {
622 (GInterfaceInitFunc) wxgtk_tree_model_drag_source_init,
623 NULL,
624 NULL
625 };
626
627 static const GInterfaceInfo drag_dest_iface_info =
628 {
629 (GInterfaceInitFunc) wxgtk_tree_model_drag_dest_init,
630 NULL,
631 NULL
632 };
633
634 tree_model_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxTreeModel",
635 &tree_model_info, (GTypeFlags)0 );
636
637 g_type_add_interface_static (tree_model_type,
638 GTK_TYPE_TREE_MODEL,
639 &tree_model_iface_info);
640 g_type_add_interface_static (tree_model_type,
641 GTK_TYPE_TREE_SORTABLE,
642 &sortable_iface_info);
643 g_type_add_interface_static (tree_model_type,
644 GTK_TYPE_TREE_DRAG_DEST,
645 &drag_dest_iface_info);
646 g_type_add_interface_static (tree_model_type,
647 GTK_TYPE_TREE_DRAG_SOURCE,
648 &drag_source_iface_info);
649 }
650
651 return tree_model_type;
652 }
653
654 static GtkWxTreeModel *
655 wxgtk_tree_model_new(void)
656 {
657 GtkWxTreeModel *retval = (GtkWxTreeModel *) g_object_new (GTK_TYPE_WX_TREE_MODEL, NULL);
658 return retval;
659 }
660
661 static void
662 wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass)
663 {
664 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
665 GObjectClass *object_class = (GObjectClass*) klass;
666 object_class->finalize = wxgtk_tree_model_finalize;
667 }
668
669 static void
670 wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface)
671 {
672 iface->get_flags = wxgtk_tree_model_get_flags;
673 iface->get_n_columns = wxgtk_tree_model_get_n_columns;
674 iface->get_column_type = wxgtk_tree_model_get_column_type;
675 iface->get_iter = wxgtk_tree_model_get_iter;
676 iface->get_path = wxgtk_tree_model_get_path;
677 iface->get_value = wxgtk_tree_model_get_value;
678 iface->iter_next = wxgtk_tree_model_iter_next;
679 iface->iter_children = wxgtk_tree_model_iter_children;
680 iface->iter_has_child = wxgtk_tree_model_iter_has_child;
681 iface->iter_n_children = wxgtk_tree_model_iter_n_children;
682 iface->iter_nth_child = wxgtk_tree_model_iter_nth_child;
683 iface->iter_parent = wxgtk_tree_model_iter_parent;
684 }
685
686 static void
687 wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface)
688 {
689 iface->get_sort_column_id = wxgtk_tree_model_get_sort_column_id;
690 iface->set_sort_column_id = wxgtk_tree_model_set_sort_column_id;
691 iface->set_sort_func = wxgtk_tree_model_set_sort_func;
692 iface->set_default_sort_func = wxgtk_tree_model_set_default_sort_func;
693 iface->has_default_sort_func = wxgtk_tree_model_has_default_sort_func;
694 }
695
696 static void
697 wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface)
698 {
699 iface->row_draggable = wxgtk_tree_model_row_draggable;
700 iface->drag_data_delete = wxgtk_tree_model_drag_data_delete;
701 iface->drag_data_get = wxgtk_tree_model_drag_data_get;
702 }
703
704 static void
705 wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface)
706 {
707 iface->drag_data_received = wxgtk_tree_model_drag_data_received;
708 iface->row_drop_possible = wxgtk_tree_model_row_drop_possible;
709 }
710
711 static void
712 wxgtk_tree_model_init (GtkWxTreeModel *tree_model)
713 {
714 tree_model->internal = NULL;
715 tree_model->stamp = g_random_int();
716 }
717
718 static void
719 wxgtk_tree_model_finalize (GObject *object)
720 {
721 /* must chain up */
722 (* list_parent_class->finalize) (object);
723 }
724
725 } // extern "C"
726
727 //-----------------------------------------------------------------------------
728 // implement callbacks from wxGtkTreeModel class by letting
729 // them call the methods of wxWidgets' wxDataViewModel
730 //-----------------------------------------------------------------------------
731
732 static GtkTreeModelFlags
733 wxgtk_tree_model_get_flags (GtkTreeModel *tree_model)
734 {
735 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
736 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), (GtkTreeModelFlags)0 );
737
738 return wxtree_model->internal->get_flags();
739 }
740
741 static gint
742 wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model)
743 {
744 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
745 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), 0);
746
747 return wxtree_model->internal->GetDataViewModel()->GetColumnCount();
748 }
749
750 static GType
751 wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
752 gint index)
753 {
754 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
755 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), G_TYPE_INVALID);
756
757 GType gtype = G_TYPE_INVALID;
758
759 wxString wxtype = wxtree_model->internal->GetDataViewModel()->GetColumnType( (unsigned int) index );
760
761 if (wxtype == wxT("string"))
762 gtype = G_TYPE_STRING;
763 else
764 {
765 gtype = G_TYPE_POINTER;
766 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
767 }
768
769 return gtype;
770 }
771
772 static gboolean
773 wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
774 GtkTreeIter *iter,
775 GtkTreePath *path)
776 {
777 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
778 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
779 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
780
781 return wxtree_model->internal->get_iter( iter, path );
782 }
783
784 static GtkTreePath *
785 wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
786 GtkTreeIter *iter)
787 {
788 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
789 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), NULL);
790 g_return_val_if_fail (iter->stamp == GTK_WX_TREE_MODEL (wxtree_model)->stamp, NULL);
791
792 return wxtree_model->internal->get_path( iter );
793 }
794
795 static void
796 wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
797 GtkTreeIter *iter,
798 gint column,
799 GValue *value)
800 {
801 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
802 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model) );
803
804 wxDataViewModel *model = wxtree_model->internal->GetDataViewModel();
805 wxString mtype = model->GetColumnType( (unsigned int) column );
806 if (mtype == wxT("string"))
807 {
808 wxVariant variant;
809 g_value_init( value, G_TYPE_STRING );
810 wxDataViewItem item( (void*) iter->user_data );
811 model->GetValue( variant, item, (unsigned int) column );
812
813 g_value_set_string( value, variant.GetString().utf8_str() );
814 }
815 else
816 {
817 wxFAIL_MSG( wxT("non-string columns not supported yet") );
818 }
819 }
820
821 static gboolean
822 wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
823 GtkTreeIter *iter)
824 {
825 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
826
827 // This happens when clearing the view by calling .._set_model( NULL );
828 if (iter->stamp == 0) return FALSE;
829
830 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
831 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
832
833 return wxtree_model->internal->iter_next( iter );
834 }
835
836 static gboolean
837 wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
838 GtkTreeIter *iter,
839 GtkTreeIter *parent)
840 {
841 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
842 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
843 if (parent)
844 {
845 g_return_val_if_fail (wxtree_model->stamp == parent->stamp, FALSE);
846 }
847
848 return wxtree_model->internal->iter_children( iter, parent );
849 }
850
851 static gboolean
852 wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
853 GtkTreeIter *iter)
854 {
855 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
856 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
857 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
858
859 return wxtree_model->internal->iter_has_child( iter );
860 }
861
862 static gint
863 wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
864 GtkTreeIter *iter)
865 {
866 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
867 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), 0);
868 g_return_val_if_fail ( !iter || wxtree_model->stamp == iter->stamp, 0);
869
870 return wxtree_model->internal->iter_n_children( iter );
871 }
872
873 static gboolean
874 wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
875 GtkTreeIter *iter,
876 GtkTreeIter *parent,
877 gint n)
878 {
879 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
880 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
881
882 return wxtree_model->internal->iter_nth_child( iter, parent, n );
883 }
884
885 static gboolean
886 wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
887 GtkTreeIter *iter,
888 GtkTreeIter *child)
889 {
890 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
891 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
892 g_return_val_if_fail (wxtree_model->stamp == child->stamp, FALSE);
893
894 return wxtree_model->internal->iter_parent( iter, child );
895 }
896
897 /* drag'n'drop iface */
898 static gboolean
899 wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source,
900 GtkTreePath *path)
901 {
902 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
903 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
904
905 return wxtree_model->internal->row_draggable( drag_source, path );
906 }
907
908 static gboolean
909 wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source,
910 GtkTreePath *path)
911 {
912 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
913 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
914
915 return wxtree_model->internal->drag_data_delete( drag_source, path );
916 }
917
918 static gboolean
919 wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source,
920 GtkTreePath *path,
921 GtkSelectionData *selection_data)
922 {
923 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
924 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
925
926 #if 0
927 wxPrintf( "drag_get_data\n");
928
929 wxGtkString atom_selection(gdk_atom_name(selection_data->selection));
930 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection) );
931
932 wxGtkString atom_target(gdk_atom_name(selection_data->target));
933 wxPrintf( "target %s\n", wxString::FromAscii(atom_target) );
934
935 wxGtkString atom_type(gdk_atom_name(selection_data->type));
936 wxPrintf( "type %s\n", wxString::FromAscii(atom_type) );
937
938 wxPrintf( "format %d\n", selection_data->format );
939 #endif
940
941 return wxtree_model->internal->drag_data_get( drag_source, path, selection_data );
942 }
943
944 static gboolean
945 wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest,
946 GtkTreePath *dest,
947 GtkSelectionData *selection_data)
948 {
949 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest;
950 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
951
952 return wxtree_model->internal->drag_data_received( drag_dest, dest, selection_data );
953 }
954
955 static gboolean
956 wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest,
957 GtkTreePath *dest_path,
958 GtkSelectionData *selection_data)
959 {
960 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest;
961 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
962
963 return wxtree_model->internal->row_drop_possible( drag_dest, dest_path, selection_data );
964 }
965
966 /* sortable iface */
967 static gboolean
968 wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
969 gint *sort_column_id,
970 GtkSortType *order)
971 {
972 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) sortable;
973
974 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE);
975
976 if (!wxtree_model->internal->IsSorted())
977 {
978 if (sort_column_id)
979 *sort_column_id = -1;
980
981 return TRUE;
982 }
983
984
985 if (sort_column_id)
986 *sort_column_id = wxtree_model->internal->GetSortColumn();
987
988 if (order)
989 *order = wxtree_model->internal->GetSortOrder();
990
991 return TRUE;
992 }
993
994 wxDataViewColumn *gs_lastLeftClickHeader = NULL;
995
996 static void
997 wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
998 gint sort_column_id,
999 GtkSortType order)
1000 {
1001 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) sortable;
1002 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
1003
1004 tree_model->internal->SetDataViewSortColumn( gs_lastLeftClickHeader );
1005
1006 if ((sort_column_id != (gint) tree_model->internal->GetSortColumn()) ||
1007 (order != tree_model->internal->GetSortOrder()))
1008 {
1009 tree_model->internal->SetSortColumn( sort_column_id );
1010 tree_model->internal->SetSortOrder( order );
1011
1012 gtk_tree_sortable_sort_column_changed (sortable);
1013
1014 tree_model->internal->GetDataViewModel()->Resort();
1015 }
1016
1017 if (gs_lastLeftClickHeader)
1018 {
1019 wxDataViewCtrl *dv = tree_model->internal->GetOwner();
1020 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, dv->GetId() );
1021 event.SetDataViewColumn( gs_lastLeftClickHeader );
1022 event.SetModel( dv->GetModel() );
1023 dv->HandleWindowEvent( event );
1024 }
1025
1026 gs_lastLeftClickHeader = NULL;
1027 }
1028
1029 static void
1030 wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
1031 gint WXUNUSED(sort_column_id),
1032 GtkTreeIterCompareFunc func,
1033 gpointer WXUNUSED(data),
1034 GDestroyNotify WXUNUSED(destroy))
1035 {
1036 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
1037 g_return_if_fail (func != NULL);
1038 }
1039
1040 static void
1041 wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
1042 GtkTreeIterCompareFunc func,
1043 gpointer WXUNUSED(data),
1044 GDestroyNotify WXUNUSED(destroy))
1045 {
1046 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
1047 g_return_if_fail (func != NULL);
1048
1049 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
1050 // TODO: remove this code
1051 }
1052
1053 static gboolean
1054 wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable)
1055 {
1056 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE );
1057
1058 return FALSE;
1059 }
1060
1061 //-----------------------------------------------------------------------------
1062 // define new GTK+ class GtkWxRendererText
1063 //-----------------------------------------------------------------------------
1064
1065 extern "C" {
1066
1067 #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
1068 #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
1069 #define GTK_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1070 #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1071 #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
1072 #define GTK_WX_CELL_RENDERER_TEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
1073
1074 GType gtk_wx_cell_renderer_text_get_type (void);
1075
1076 typedef struct _GtkWxCellRendererText GtkWxCellRendererText;
1077 typedef struct _GtkWxCellRendererTextClass GtkWxCellRendererTextClass;
1078
1079 struct _GtkWxCellRendererText
1080 {
1081 GtkCellRendererText parent;
1082
1083 wxDataViewRenderer *wx_renderer;
1084 };
1085
1086 struct _GtkWxCellRendererTextClass
1087 {
1088 GtkCellRendererTextClass cell_parent_class;
1089 };
1090
1091
1092 static GtkWxCellRendererText *gtk_wx_cell_renderer_text_new (void);
1093 static void gtk_wx_cell_renderer_text_init (
1094 GtkWxCellRendererText *cell );
1095 static void gtk_wx_cell_renderer_text_class_init(
1096 GtkWxCellRendererTextClass *klass );
1097 static void gtk_wx_cell_renderer_text_finalize (
1098 GObject *object );
1099 static GtkCellEditable *gtk_wx_cell_renderer_text_start_editing(
1100 GtkCellRenderer *cell,
1101 GdkEvent *event,
1102 GtkWidget *widget,
1103 const gchar *path,
1104 GdkRectangle *background_area,
1105 GdkRectangle *cell_area,
1106 GtkCellRendererState flags );
1107
1108
1109 static GObjectClass *text_cell_parent_class = NULL;
1110
1111 } // extern "C"
1112
1113 GType
1114 gtk_wx_cell_renderer_text_get_type (void)
1115 {
1116 static GType cell_wx_type = 0;
1117
1118 if (!cell_wx_type)
1119 {
1120 const GTypeInfo cell_wx_info =
1121 {
1122 sizeof (GtkWxCellRendererTextClass),
1123 NULL, /* base_init */
1124 NULL, /* base_finalize */
1125 (GClassInitFunc) gtk_wx_cell_renderer_text_class_init,
1126 NULL, /* class_finalize */
1127 NULL, /* class_data */
1128 sizeof (GtkWxCellRendererText),
1129 0, /* n_preallocs */
1130 (GInstanceInitFunc) gtk_wx_cell_renderer_text_init,
1131 };
1132
1133 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT,
1134 "GtkWxCellRendererText", &cell_wx_info, (GTypeFlags)0 );
1135 }
1136
1137 return cell_wx_type;
1138 }
1139
1140 static void
1141 gtk_wx_cell_renderer_text_init (GtkWxCellRendererText *cell)
1142 {
1143 cell->wx_renderer = NULL;
1144 }
1145
1146 static void
1147 gtk_wx_cell_renderer_text_class_init (GtkWxCellRendererTextClass *klass)
1148 {
1149 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1150 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
1151
1152 text_cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
1153
1154 object_class->finalize = gtk_wx_cell_renderer_text_finalize;
1155
1156 cell_class->start_editing = gtk_wx_cell_renderer_text_start_editing;
1157 }
1158
1159 static void
1160 gtk_wx_cell_renderer_text_finalize (GObject *object)
1161 {
1162 /* must chain up */
1163 (* G_OBJECT_CLASS (text_cell_parent_class)->finalize) (object);
1164 }
1165
1166 GtkWxCellRendererText*
1167 gtk_wx_cell_renderer_text_new (void)
1168 {
1169 return (GtkWxCellRendererText*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT, NULL);
1170 }
1171
1172 static GtkCellEditable *gtk_wx_cell_renderer_text_start_editing(
1173 GtkCellRenderer *gtk_renderer,
1174 GdkEvent *gdk_event,
1175 GtkWidget *widget,
1176 const gchar *path,
1177 GdkRectangle *background_area,
1178 GdkRectangle *cell_area,
1179 GtkCellRendererState flags )
1180 {
1181 GtkWxCellRendererText *wxgtk_renderer = (GtkWxCellRendererText *) gtk_renderer;
1182 wxDataViewRenderer *wx_renderer = wxgtk_renderer->wx_renderer;
1183 wxDataViewColumn *column = wx_renderer->GetOwner();
1184
1185 wxDataViewItem
1186 item(column->GetOwner()->GTKPathToItem(wxGtkTreePath(path)));
1187
1188 wxDataViewCtrl *dv = column->GetOwner();
1189 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, dv->GetId() );
1190 event.SetDataViewColumn( column );
1191 event.SetModel( dv->GetModel() );
1192 event.SetColumn( column->GetModelColumn() );
1193 event.SetItem( item );
1194 dv->HandleWindowEvent( event );
1195
1196 if (event.IsAllowed())
1197 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class)->
1198 start_editing( gtk_renderer, gdk_event, widget, path, background_area, cell_area, flags );
1199 else
1200 return NULL;
1201 }
1202
1203 //-----------------------------------------------------------------------------
1204 // define new GTK+ class GtkWxCellRenderer
1205 //-----------------------------------------------------------------------------
1206
1207 extern "C" {
1208
1209 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
1210 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
1211 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1212 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
1213 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
1214 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
1215
1216 GType gtk_wx_cell_renderer_get_type (void);
1217
1218 typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
1219 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
1220
1221 struct _GtkWxCellRenderer
1222 {
1223 GtkCellRenderer parent;
1224
1225 /*< private >*/
1226 wxDataViewCustomRenderer *cell;
1227 };
1228
1229 struct _GtkWxCellRendererClass
1230 {
1231 GtkCellRendererClass cell_parent_class;
1232 };
1233
1234
1235 static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
1236 static void gtk_wx_cell_renderer_init (
1237 GtkWxCellRenderer *cell );
1238 static void gtk_wx_cell_renderer_class_init(
1239 GtkWxCellRendererClass *klass );
1240 static void gtk_wx_cell_renderer_finalize (
1241 GObject *object );
1242 static void gtk_wx_cell_renderer_get_size (
1243 GtkCellRenderer *cell,
1244 GtkWidget *widget,
1245 GdkRectangle *rectangle,
1246 gint *x_offset,
1247 gint *y_offset,
1248 gint *width,
1249 gint *height );
1250 static void gtk_wx_cell_renderer_render (
1251 GtkCellRenderer *cell,
1252 GdkWindow *window,
1253 GtkWidget *widget,
1254 GdkRectangle *background_area,
1255 GdkRectangle *cell_area,
1256 GdkRectangle *expose_area,
1257 GtkCellRendererState flags );
1258 static gboolean gtk_wx_cell_renderer_activate(
1259 GtkCellRenderer *cell,
1260 GdkEvent *event,
1261 GtkWidget *widget,
1262 const gchar *path,
1263 GdkRectangle *background_area,
1264 GdkRectangle *cell_area,
1265 GtkCellRendererState flags );
1266 static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
1267 GtkCellRenderer *cell,
1268 GdkEvent *event,
1269 GtkWidget *widget,
1270 const gchar *path,
1271 GdkRectangle *background_area,
1272 GdkRectangle *cell_area,
1273 GtkCellRendererState flags );
1274
1275
1276 static GObjectClass *cell_parent_class = NULL;
1277
1278 } // extern "C"
1279
1280 GType
1281 gtk_wx_cell_renderer_get_type (void)
1282 {
1283 static GType cell_wx_type = 0;
1284
1285 if (!cell_wx_type)
1286 {
1287 const GTypeInfo cell_wx_info =
1288 {
1289 sizeof (GtkWxCellRendererClass),
1290 NULL, /* base_init */
1291 NULL, /* base_finalize */
1292 (GClassInitFunc) gtk_wx_cell_renderer_class_init,
1293 NULL, /* class_finalize */
1294 NULL, /* class_data */
1295 sizeof (GtkWxCellRenderer),
1296 0, /* n_preallocs */
1297 (GInstanceInitFunc) gtk_wx_cell_renderer_init,
1298 };
1299
1300 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER,
1301 "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 );
1302 }
1303
1304 return cell_wx_type;
1305 }
1306
1307 static void
1308 gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
1309 {
1310 cell->cell = NULL;
1311 }
1312
1313 static void
1314 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
1315 {
1316 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1317 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
1318
1319 cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
1320
1321 object_class->finalize = gtk_wx_cell_renderer_finalize;
1322
1323 cell_class->get_size = gtk_wx_cell_renderer_get_size;
1324 cell_class->render = gtk_wx_cell_renderer_render;
1325 cell_class->activate = gtk_wx_cell_renderer_activate;
1326 cell_class->start_editing = gtk_wx_cell_renderer_start_editing;
1327 }
1328
1329 static void
1330 gtk_wx_cell_renderer_finalize (GObject *object)
1331 {
1332 /* must chain up */
1333 (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object);
1334 }
1335
1336 GtkCellRenderer*
1337 gtk_wx_cell_renderer_new (void)
1338 {
1339 return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
1340 }
1341
1342 static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
1343 GtkCellRenderer *renderer,
1344 GdkEvent *WXUNUSED(event),
1345 GtkWidget *widget,
1346 const gchar *path,
1347 GdkRectangle *WXUNUSED(background_area),
1348 GdkRectangle *cell_area,
1349 GtkCellRendererState WXUNUSED(flags) )
1350 {
1351 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1352 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1353
1354 // Renderer doesn't support in-place editing
1355 if (!cell->HasEditorCtrl())
1356 return NULL;
1357
1358 // An in-place editing control is still around
1359 if (cell->GetEditorCtrl())
1360 return NULL;
1361
1362 GdkRectangle rect;
1363 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
1364 &rect.x,
1365 &rect.y,
1366 &rect.width,
1367 &rect.height);
1368
1369 rect.x += cell_area->x;
1370 rect.y += cell_area->y;
1371 // rect.width -= renderer->xpad * 2;
1372 // rect.height -= renderer->ypad * 2;
1373
1374 // wxRect renderrect(wxRectFromGDKRect(&rect));
1375 wxRect renderrect(wxRectFromGDKRect(cell_area));
1376
1377 wxDataViewItem
1378 item(cell->GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(path)));
1379
1380 cell->StartEditing( item, renderrect );
1381
1382 return NULL;
1383 }
1384
1385 static void
1386 gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
1387 GtkWidget *WXUNUSED(widget),
1388 GdkRectangle *cell_area,
1389 gint *x_offset,
1390 gint *y_offset,
1391 gint *width,
1392 gint *height)
1393 {
1394 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1395 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1396
1397 wxSize size = cell->GetSize();
1398
1399 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
1400
1401 // Uniform row height, if specified, overrides the value returned by the
1402 // renderer.
1403 if ( !ctrl->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) )
1404 {
1405 const int uniformHeight = ctrl->GTKGetUniformRowHeight();
1406 if ( uniformHeight > 0 )
1407 size.y = uniformHeight;
1408 }
1409
1410 int xpad, ypad;
1411 gtk_cell_renderer_get_padding(renderer, &xpad, &ypad);
1412 int calc_width = xpad * 2 + size.x;
1413 int calc_height = ypad * 2 + size.y;
1414
1415 if (x_offset)
1416 *x_offset = 0;
1417 if (y_offset)
1418 *y_offset = 0;
1419
1420 if (cell_area && size.x > 0 && size.y > 0)
1421 {
1422 float xalign, yalign;
1423 gtk_cell_renderer_get_alignment(renderer, &xalign, &yalign);
1424 if (x_offset)
1425 {
1426 *x_offset = int(xalign * (cell_area->width - calc_width - 2 * xpad));
1427 *x_offset = MAX(*x_offset, 0) + xpad;
1428 }
1429 if (y_offset)
1430 {
1431 *y_offset = int(yalign * (cell_area->height - calc_height - 2 * ypad));
1432 *y_offset = MAX(*y_offset, 0) + ypad;
1433 }
1434 }
1435
1436 if (width)
1437 *width = calc_width;
1438
1439 if (height)
1440 *height = calc_height;
1441 }
1442
1443 static void
1444 gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
1445 GdkWindow *window,
1446 GtkWidget *widget,
1447 GdkRectangle *background_area,
1448 GdkRectangle *cell_area,
1449 GdkRectangle *expose_area,
1450 GtkCellRendererState flags)
1451
1452 {
1453 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1454 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1455
1456 cell->GTKStashRenderParams(window, widget,
1457 background_area, expose_area, flags);
1458
1459 wxRect rect(wxRectFromGDKRect(cell_area));
1460 int xpad, ypad;
1461 gtk_cell_renderer_get_padding(renderer, &xpad, &ypad);
1462 rect = rect.Deflate(xpad, ypad);
1463
1464 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
1465 wxWindowDCImpl *impl = (wxWindowDCImpl *) dc->GetImpl();
1466
1467 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1468 // window such as a DnD drop window.
1469 if (window != impl->m_gdkwindow)
1470 {
1471 impl->Destroy();
1472 impl->m_gdkwindow = window;
1473 impl->SetUpDC();
1474 }
1475
1476 int state = 0;
1477 if (flags & GTK_CELL_RENDERER_SELECTED)
1478 state |= wxDATAVIEW_CELL_SELECTED;
1479 if (flags & GTK_CELL_RENDERER_PRELIT)
1480 state |= wxDATAVIEW_CELL_PRELIT;
1481 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1482 state |= wxDATAVIEW_CELL_INSENSITIVE;
1483 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1484 state |= wxDATAVIEW_CELL_INSENSITIVE;
1485 if (flags & GTK_CELL_RENDERER_FOCUSED)
1486 state |= wxDATAVIEW_CELL_FOCUSED;
1487 cell->WXCallRender( rect, dc, state );
1488 }
1489
1490 static gboolean
1491 gtk_wx_cell_renderer_activate(
1492 GtkCellRenderer *renderer,
1493 GdkEvent *event,
1494 GtkWidget *widget,
1495 const gchar *path,
1496 GdkRectangle *WXUNUSED(background_area),
1497 GdkRectangle *cell_area,
1498 GtkCellRendererState WXUNUSED(flags) )
1499 {
1500 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1501 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1502
1503 GdkRectangle rect;
1504 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
1505 &rect.x,
1506 &rect.y,
1507 &rect.width,
1508 &rect.height);
1509
1510 rect.x += cell_area->x;
1511 rect.y += cell_area->y;
1512 int xpad, ypad;
1513 gtk_cell_renderer_get_padding(renderer, &xpad, &ypad);
1514 rect.width -= xpad * 2;
1515 rect.height -= ypad * 2;
1516
1517 wxRect renderrect(wxRectFromGDKRect(&rect));
1518
1519 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
1520 wxDataViewModel *model = ctrl->GetModel();
1521
1522 wxDataViewItem item(ctrl->GTKPathToItem(wxGtkTreePath(path)));
1523
1524 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1525
1526 if ( !event )
1527 {
1528 // activated by <ENTER>
1529 return cell->ActivateCell(renderrect, model, item, model_col, NULL);
1530 }
1531 else if ( event->type == GDK_BUTTON_PRESS )
1532 {
1533 GdkEventButton *button_event = (GdkEventButton*)event;
1534 if ( button_event->button == 1 )
1535 {
1536 wxMouseEvent mouse_event(wxEVT_LEFT_DOWN);
1537 InitMouseEvent(ctrl, mouse_event, button_event);
1538
1539 mouse_event.m_x -= renderrect.x;
1540 mouse_event.m_y -= renderrect.y;
1541
1542 return cell->ActivateCell(renderrect, model, item, model_col, &mouse_event);
1543 }
1544 }
1545
1546 wxLogDebug("unexpected event type in gtk_wx_cell_renderer_activate()");
1547 return false;
1548 }
1549
1550 // ---------------------------------------------------------
1551 // wxGtkDataViewModelNotifier
1552 // ---------------------------------------------------------
1553
1554 class wxGtkDataViewModelNotifier: public wxDataViewModelNotifier
1555 {
1556 public:
1557 wxGtkDataViewModelNotifier( wxDataViewModel *wx_model, wxDataViewCtrlInternal *internal );
1558 ~wxGtkDataViewModelNotifier();
1559
1560 virtual bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
1561 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
1562 virtual bool ItemChanged( const wxDataViewItem &item );
1563 virtual bool ValueChanged( const wxDataViewItem &item, unsigned int model_column );
1564 virtual bool Cleared();
1565 virtual void Resort();
1566 virtual bool BeforeReset();
1567 virtual bool AfterReset();
1568
1569 void UpdateLastCount();
1570
1571 private:
1572 wxDataViewModel *m_wx_model;
1573 wxDataViewCtrlInternal *m_internal;
1574 };
1575
1576 // ---------------------------------------------------------
1577 // wxGtkDataViewListModelNotifier
1578 // ---------------------------------------------------------
1579
1580 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1581 wxDataViewModel *wx_model, wxDataViewCtrlInternal *internal )
1582 {
1583 m_wx_model = wx_model;
1584 m_internal = internal;
1585 }
1586
1587 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1588 {
1589 m_wx_model = NULL;
1590 m_internal = NULL;
1591 }
1592
1593 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
1594 {
1595 m_internal->ItemAdded( parent, item );
1596 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1597
1598 GtkTreeIter iter;
1599 iter.stamp = wxgtk_model->stamp;
1600 iter.user_data = item.GetID();
1601
1602 wxGtkTreePath path(wxgtk_tree_model_get_path(
1603 GTK_TREE_MODEL(wxgtk_model), &iter ));
1604 gtk_tree_model_row_inserted(
1605 GTK_TREE_MODEL(wxgtk_model), path, &iter);
1606
1607 return true;
1608 }
1609
1610 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
1611 {
1612 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1613 #if 0
1614 // using _get_path for a deleted item cannot be
1615 // a good idea
1616 GtkTreeIter iter;
1617 iter.stamp = wxgtk_model->stamp;
1618 iter.user_data = (gpointer) item.GetID();
1619 wxGtkTreePath path(wxgtk_tree_model_get_path(
1620 GTK_TREE_MODEL(wxgtk_model), &iter ));
1621 #else
1622 // so get the path from the parent
1623 GtkTreeIter parentIter;
1624 parentIter.stamp = wxgtk_model->stamp;
1625 parentIter.user_data = (gpointer) parent.GetID();
1626 wxGtkTreePath parentPath(wxgtk_tree_model_get_path(
1627 GTK_TREE_MODEL(wxgtk_model), &parentIter ));
1628
1629 // and add the final index ourselves
1630 wxGtkTreePath path(gtk_tree_path_copy(parentPath));
1631 int index = m_internal->GetIndexOf( parent, item );
1632 gtk_tree_path_append_index( path, index );
1633 #endif
1634
1635 gtk_tree_model_row_deleted(
1636 GTK_TREE_MODEL(wxgtk_model), path );
1637
1638 m_internal->ItemDeleted( parent, item );
1639
1640 // Did we remove the last child, causing 'parent' to become a leaf?
1641 if ( !m_wx_model->IsContainer(parent) )
1642 {
1643 gtk_tree_model_row_has_child_toggled
1644 (
1645 GTK_TREE_MODEL(wxgtk_model),
1646 parentPath,
1647 &parentIter
1648 );
1649 }
1650
1651 return true;
1652 }
1653
1654 void wxGtkDataViewModelNotifier::Resort()
1655 {
1656 m_internal->Resort();
1657 }
1658
1659 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem &item )
1660 {
1661 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1662
1663 GtkTreeIter iter;
1664 iter.stamp = wxgtk_model->stamp;
1665 iter.user_data = (gpointer) item.GetID();
1666
1667 wxGtkTreePath path(wxgtk_tree_model_get_path(
1668 GTK_TREE_MODEL(wxgtk_model), &iter ));
1669 gtk_tree_model_row_changed(
1670 GTK_TREE_MODEL(wxgtk_model), path, &iter );
1671
1672 m_internal->ItemChanged( item );
1673
1674 return true;
1675 }
1676
1677 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem &item, unsigned int model_column )
1678 {
1679 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1680 wxDataViewCtrl *ctrl = m_internal->GetOwner();
1681
1682 // This adds GTK+'s missing MVC logic for ValueChanged
1683 unsigned int index;
1684 for (index = 0; index < ctrl->GetColumnCount(); index++)
1685 {
1686 wxDataViewColumn *column = ctrl->GetColumn( index );
1687 if (column->GetModelColumn() == model_column)
1688 {
1689 GtkTreeView *widget = GTK_TREE_VIEW(ctrl->GtkGetTreeView());
1690 GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
1691
1692 // Don't attempt to refresh not yet realized tree, it is useless
1693 // and results in GTK errors.
1694 if ( gtk_widget_get_realized(ctrl->GtkGetTreeView()) )
1695 {
1696 // Get cell area
1697 GtkTreeIter iter;
1698 iter.stamp = wxgtk_model->stamp;
1699 iter.user_data = (gpointer) item.GetID();
1700 wxGtkTreePath path(wxgtk_tree_model_get_path(
1701 GTK_TREE_MODEL(wxgtk_model), &iter ));
1702 GdkRectangle cell_area;
1703 gtk_tree_view_get_cell_area( widget, path, gcolumn, &cell_area );
1704
1705 GtkAdjustment* hadjust = gtk_tree_view_get_hadjustment( widget );
1706 double d = gtk_adjustment_get_value( hadjust );
1707 int xdiff = (int) d;
1708
1709 int ydiff = gcolumn->button->allocation.height;
1710 // Redraw
1711 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
1712 cell_area.x - xdiff, ydiff + cell_area.y, cell_area.width, cell_area.height );
1713 }
1714
1715 m_internal->ValueChanged( item, model_column );
1716
1717 return true;
1718 }
1719 }
1720
1721 return false;
1722 }
1723
1724 bool wxGtkDataViewModelNotifier::BeforeReset()
1725 {
1726 GtkWidget *treeview = m_internal->GetOwner()->GtkGetTreeView();
1727 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview), NULL );
1728
1729 return true;
1730 }
1731
1732 bool wxGtkDataViewModelNotifier::AfterReset()
1733 {
1734 GtkWidget *treeview = m_internal->GetOwner()->GtkGetTreeView();
1735 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1736
1737 m_internal->Cleared();
1738
1739 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(wxgtk_model) );
1740
1741 return true;
1742 }
1743
1744 bool wxGtkDataViewModelNotifier::Cleared()
1745 {
1746 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1747
1748 // There is no call to tell the model that everything
1749 // has been deleted so call row_deleted() for every
1750 // child of root...
1751
1752 int count = m_internal->iter_n_children( NULL ); // number of children of root
1753
1754 GtkTreePath *path = gtk_tree_path_new_first(); // points to root
1755
1756 int i;
1757 for (i = 0; i < count; i++)
1758 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model), path );
1759
1760 gtk_tree_path_free( path );
1761
1762 m_internal->Cleared();
1763
1764 return true;
1765 }
1766
1767 // ---------------------------------------------------------
1768 // wxDataViewRenderer
1769 // ---------------------------------------------------------
1770
1771 static gpointer s_user_data = NULL;
1772
1773 static void
1774 wxgtk_cell_editable_editing_done( GtkCellEditable *WXUNUSED(editable),
1775 wxDataViewRenderer *wxrenderer )
1776 {
1777 wxDataViewColumn *column = wxrenderer->GetOwner();
1778 wxDataViewCtrl *dv = column->GetOwner();
1779 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv->GetId() );
1780 event.SetDataViewColumn( column );
1781 event.SetModel( dv->GetModel() );
1782 wxDataViewItem item( s_user_data );
1783 event.SetItem( item );
1784 dv->HandleWindowEvent( event );
1785 }
1786
1787 static void
1788 wxgtk_renderer_editing_started( GtkCellRenderer *WXUNUSED(cell), GtkCellEditable *editable,
1789 gchar *path, wxDataViewRenderer *wxrenderer )
1790 {
1791 if (!editable)
1792 return;
1793
1794 wxDataViewColumn *column = wxrenderer->GetOwner();
1795 wxDataViewCtrl *dv = column->GetOwner();
1796 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv->GetId() );
1797 event.SetDataViewColumn( column );
1798 event.SetModel( dv->GetModel() );
1799 wxDataViewItem item(dv->GTKPathToItem(wxGtkTreePath(path)));
1800 event.SetItem( item );
1801 dv->HandleWindowEvent( event );
1802
1803 if (GTK_IS_CELL_EDITABLE(editable))
1804 {
1805 s_user_data = item.GetID();
1806
1807 g_signal_connect (GTK_CELL_EDITABLE (editable), "editing_done",
1808 G_CALLBACK (wxgtk_cell_editable_editing_done),
1809 (gpointer) wxrenderer );
1810
1811 }
1812 }
1813
1814
1815 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
1816
1817 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1818 int align ) :
1819 wxDataViewRendererBase( varianttype, mode, align )
1820 {
1821 m_renderer = NULL;
1822 m_mode = mode;
1823
1824 // we haven't changed them yet
1825 m_usingDefaultAttrs = true;
1826
1827 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1828 // after the m_renderer pointer has been initialized
1829 }
1830
1831 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
1832 {
1833 gtk_tree_view_column_pack_end( column, m_renderer, TRUE /* expand */);
1834 }
1835
1836 void wxDataViewRenderer::GtkInitHandlers()
1837 {
1838 if (!gtk_check_version(2,6,0))
1839 {
1840 g_signal_connect (GTK_CELL_RENDERER(m_renderer), "editing_started",
1841 G_CALLBACK (wxgtk_renderer_editing_started),
1842 this);
1843 }
1844 }
1845
1846 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
1847 {
1848 GtkCellRendererMode gtkMode;
1849 switch (mode)
1850 {
1851 case wxDATAVIEW_CELL_INERT:
1852 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
1853 break;
1854
1855 case wxDATAVIEW_CELL_ACTIVATABLE:
1856 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
1857 break;
1858
1859 case wxDATAVIEW_CELL_EDITABLE:
1860 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
1861 break;
1862
1863 default:
1864 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1865 return;
1866 }
1867
1868 m_mode = mode;
1869
1870 // This value is most often ignored in GtkTreeView
1871 GValue gvalue = { 0, };
1872 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1873 g_value_set_enum( &gvalue, gtkMode );
1874 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1875 g_value_unset( &gvalue );
1876 }
1877
1878 wxDataViewCellMode wxDataViewRenderer::GetMode() const
1879 {
1880 wxDataViewCellMode ret;
1881
1882 GValue gvalue;
1883 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1884
1885 switch (g_value_get_enum(&gvalue))
1886 {
1887 default:
1888 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1889 // fall through (we have to return something)
1890
1891 case GTK_CELL_RENDERER_MODE_INERT:
1892 ret = wxDATAVIEW_CELL_INERT;
1893 break;
1894
1895 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1896 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1897 break;
1898
1899 case GTK_CELL_RENDERER_MODE_EDITABLE:
1900 ret = wxDATAVIEW_CELL_EDITABLE;
1901 break;
1902 }
1903
1904 g_value_unset( &gvalue );
1905
1906 return ret;
1907 }
1908
1909 void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer *renderer)
1910 {
1911 int align = m_alignment;
1912
1913 // query alignment from column ?
1914 if (align == -1)
1915 {
1916 // None there yet
1917 if (GetOwner() == NULL)
1918 return;
1919
1920 align = GetOwner()->GetAlignment();
1921 align |= wxALIGN_CENTRE_VERTICAL;
1922 }
1923
1924 // horizontal alignment:
1925
1926 gfloat xalign = 0.0;
1927 if (align & wxALIGN_RIGHT)
1928 xalign = 1.0;
1929 else if (align & wxALIGN_CENTER_HORIZONTAL)
1930 xalign = 0.5;
1931
1932 GValue gvalue = { 0, };
1933 g_value_init( &gvalue, G_TYPE_FLOAT );
1934 g_value_set_float( &gvalue, xalign );
1935 g_object_set_property( G_OBJECT(renderer), "xalign", &gvalue );
1936 g_value_unset( &gvalue );
1937
1938 // vertical alignment:
1939
1940 gfloat yalign = 0.0;
1941 if (align & wxALIGN_BOTTOM)
1942 yalign = 1.0;
1943 else if (align & wxALIGN_CENTER_VERTICAL)
1944 yalign = 0.5;
1945
1946 GValue gvalue2 = { 0, };
1947 g_value_init( &gvalue2, G_TYPE_FLOAT );
1948 g_value_set_float( &gvalue2, yalign );
1949 g_object_set_property( G_OBJECT(renderer), "yalign", &gvalue2 );
1950 g_value_unset( &gvalue2 );
1951 }
1952
1953 void wxDataViewRenderer::SetAlignment( int align )
1954 {
1955 m_alignment = align;
1956 GtkUpdateAlignment();
1957 }
1958
1959 int wxDataViewRenderer::GetAlignment() const
1960 {
1961 return m_alignment;
1962 }
1963
1964 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode)
1965 {
1966 #ifdef __WXGTK26__
1967 if ( gtk_check_version(2, 6, 0) != NULL )
1968 return;
1969
1970 GtkCellRendererText * const rend = GtkGetTextRenderer();
1971 if ( !rend )
1972 return;
1973
1974 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1975 // can just cast between them
1976 GValue gvalue = { 0, };
1977 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1978 g_value_set_enum( &gvalue, static_cast<PangoEllipsizeMode>(mode) );
1979 g_object_set_property( G_OBJECT(rend), "ellipsize", &gvalue );
1980 g_value_unset( &gvalue );
1981 #else // GTK < 2.6
1982 wxUnusedVar(mode);
1983 #endif // GTK 2.6/before
1984 }
1985
1986 wxEllipsizeMode wxDataViewRenderer::GetEllipsizeMode() const
1987 {
1988 #ifdef __WXGTK26__
1989 if ( gtk_check_version(2, 6, 0) != NULL )
1990 return wxELLIPSIZE_NONE;
1991
1992 GtkCellRendererText * const rend = GtkGetTextRenderer();
1993 if ( !rend )
1994 return wxELLIPSIZE_NONE;
1995
1996 GValue gvalue = { 0, };
1997 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1998 g_object_get_property( G_OBJECT(rend), "ellipsize", &gvalue );
1999 wxEllipsizeMode
2000 mode = static_cast<wxEllipsizeMode>(g_value_get_enum( &gvalue ));
2001 g_value_unset( &gvalue );
2002
2003 return mode;
2004 #else // GTK < 2.6
2005 return wxELLIPSIZE_NONE;
2006 #endif // GTK 2.6/before
2007 }
2008
2009 void
2010 wxDataViewRenderer::GtkOnTextEdited(const char *itempath, const wxString& str)
2011 {
2012 wxVariant value(str);
2013 if (!Validate( value ))
2014 return;
2015
2016 wxDataViewItem
2017 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
2018
2019 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
2020 }
2021
2022 void
2023 wxDataViewRenderer::GtkOnCellChanged(const wxVariant& value,
2024 const wxDataViewItem& item,
2025 unsigned col)
2026 {
2027 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
2028 model->ChangeValue( value, item, col );
2029 }
2030
2031 // ---------------------------------------------------------
2032 // wxDataViewTextRenderer
2033 // ---------------------------------------------------------
2034
2035 extern "C"
2036 {
2037
2038 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *WXUNUSED(renderer),
2039 gchar *arg1, gchar *arg2, gpointer user_data )
2040 {
2041 wxDataViewRenderer *cell = (wxDataViewRenderer*) user_data;
2042
2043 cell->GtkOnTextEdited(arg1, wxGTK_CONV_BACK_FONT(
2044 arg2, cell->GetOwner()->GetOwner()->GetFont()));
2045 }
2046
2047 }
2048
2049 namespace
2050 {
2051
2052 // helper function used by wxDataViewTextRenderer and
2053 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
2054 // given text renderer and returns true if anything was done
2055 bool GtkApplyAttr(GtkCellRendererText *renderer, const wxDataViewItemAttr& attr)
2056 {
2057 bool usingDefaultAttrs = true;
2058 if (attr.HasColour())
2059 {
2060 const GdkColor * const gcol = attr.GetColour().GetColor();
2061
2062 GValue gvalue = { 0, };
2063 g_value_init( &gvalue, GDK_TYPE_COLOR );
2064 g_value_set_boxed( &gvalue, gcol );
2065 g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
2066 g_value_unset( &gvalue );
2067
2068 usingDefaultAttrs = false;
2069 }
2070 else
2071 {
2072 GValue gvalue = { 0, };
2073 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2074 g_value_set_boolean( &gvalue, FALSE );
2075 g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
2076 g_value_unset( &gvalue );
2077 }
2078
2079 if (attr.GetItalic())
2080 {
2081 GValue gvalue = { 0, };
2082 g_value_init( &gvalue, PANGO_TYPE_STYLE );
2083 g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
2084 g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
2085 g_value_unset( &gvalue );
2086
2087 usingDefaultAttrs = false;
2088 }
2089 else
2090 {
2091 GValue gvalue = { 0, };
2092 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2093 g_value_set_boolean( &gvalue, FALSE );
2094 g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
2095 g_value_unset( &gvalue );
2096 }
2097
2098
2099 if (attr.GetBold())
2100 {
2101 GValue gvalue = { 0, };
2102 g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
2103 g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
2104 g_object_set_property( G_OBJECT(renderer), "weight", &gvalue );
2105 g_value_unset( &gvalue );
2106
2107 usingDefaultAttrs = false;
2108 }
2109 else
2110 {
2111 GValue gvalue = { 0, };
2112 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2113 g_value_set_boolean( &gvalue, FALSE );
2114 g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue );
2115 g_value_unset( &gvalue );
2116 }
2117
2118 #if 0
2119 if (attr.HasBackgroundColour())
2120 {
2121 wxColour colour = attr.GetBackgroundColour();
2122 const GdkColor * const gcol = colour.GetColor();
2123
2124 GValue gvalue = { 0, };
2125 g_value_init( &gvalue, GDK_TYPE_COLOR );
2126 g_value_set_boxed( &gvalue, gcol );
2127 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
2128 g_value_unset( &gvalue );
2129 }
2130 else
2131 {
2132 GValue gvalue = { 0, };
2133 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2134 g_value_set_boolean( &gvalue, FALSE );
2135 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
2136 g_value_unset( &gvalue );
2137 }
2138 #endif
2139
2140 return !usingDefaultAttrs;
2141 }
2142
2143 } // anonymous namespace
2144
2145 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
2146
2147 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2148 int align ) :
2149 wxDataViewRenderer( varianttype, mode, align )
2150 {
2151 GtkWxCellRendererText *text_renderer = gtk_wx_cell_renderer_text_new();
2152 text_renderer->wx_renderer = this;
2153 m_renderer = (GtkCellRenderer*) text_renderer;
2154
2155 if (mode & wxDATAVIEW_CELL_EDITABLE)
2156 {
2157 GValue gvalue = { 0, };
2158 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2159 g_value_set_boolean( &gvalue, true );
2160 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
2161 g_value_unset( &gvalue );
2162
2163 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2164
2165 GtkInitHandlers();
2166 }
2167
2168 SetMode(mode);
2169 SetAlignment(align);
2170 }
2171
2172 bool wxDataViewTextRenderer::SetTextValue(const wxString& str)
2173 {
2174 GValue gvalue = { 0, };
2175 g_value_init( &gvalue, G_TYPE_STRING );
2176 g_value_set_string( &gvalue, wxGTK_CONV_FONT( str, GetOwner()->GetOwner()->GetFont() ) );
2177 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2178 g_value_unset( &gvalue );
2179
2180 return true;
2181 }
2182
2183 bool wxDataViewTextRenderer::GetTextValue(wxString& str) const
2184 {
2185 GValue gvalue = { 0, };
2186 g_value_init( &gvalue, G_TYPE_STRING );
2187 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
2188 str = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), const_cast<wxDataViewTextRenderer*>(this)->GetOwner()->GetOwner()->GetFont() );
2189 g_value_unset( &gvalue );
2190
2191 return true;
2192 }
2193
2194 void wxDataViewTextRenderer::SetAlignment( int align )
2195 {
2196 wxDataViewRenderer::SetAlignment(align);
2197
2198 if (gtk_check_version(2,10,0))
2199 return;
2200
2201 // horizontal alignment:
2202 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2203 if (align & wxALIGN_RIGHT)
2204 pangoAlign = PANGO_ALIGN_RIGHT;
2205 else if (align & wxALIGN_CENTER_HORIZONTAL)
2206 pangoAlign = PANGO_ALIGN_CENTER;
2207
2208 GValue gvalue = { 0, };
2209 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2210 g_value_set_enum( &gvalue, pangoAlign );
2211 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2212 g_value_unset( &gvalue );
2213 }
2214
2215 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr& attr)
2216 {
2217 return GtkApplyAttr(GtkGetTextRenderer(), attr);
2218 }
2219
2220 GtkCellRendererText *wxDataViewTextRenderer::GtkGetTextRenderer() const
2221 {
2222 return GTK_CELL_RENDERER_TEXT(m_renderer);
2223 }
2224
2225 // ---------------------------------------------------------
2226 // wxDataViewBitmapRenderer
2227 // ---------------------------------------------------------
2228
2229 namespace
2230 {
2231
2232 // set "pixbuf" property on the given renderer
2233 void SetPixbufProp(GtkCellRenderer *renderer, GdkPixbuf *pixbuf)
2234 {
2235 GValue gvalue = { 0, };
2236 g_value_init( &gvalue, G_TYPE_OBJECT );
2237 g_value_set_object( &gvalue, pixbuf );
2238 g_object_set_property( G_OBJECT(renderer), "pixbuf", &gvalue );
2239 g_value_unset( &gvalue );
2240 }
2241
2242 } // anonymous namespace
2243
2244 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
2245
2246 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2247 int align ) :
2248 wxDataViewRenderer( varianttype, mode, align )
2249 {
2250 m_renderer = gtk_cell_renderer_pixbuf_new();
2251
2252 SetMode(mode);
2253 SetAlignment(align);
2254 }
2255
2256 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
2257 {
2258 if (value.GetType() == wxT("wxBitmap"))
2259 {
2260 wxBitmap bitmap;
2261 bitmap << value;
2262
2263 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2264 // object (and it will stay there and remain owned by wxBitmap)
2265 SetPixbufProp(m_renderer, bitmap.GetPixbuf());
2266 }
2267 else if (value.GetType() == wxT("wxIcon"))
2268 {
2269 wxIcon icon;
2270 icon << value;
2271
2272 SetPixbufProp(m_renderer, icon.GetPixbuf());
2273 }
2274 else
2275 {
2276 return false;
2277 }
2278
2279 return true;
2280 }
2281
2282 bool wxDataViewBitmapRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2283 {
2284 return false;
2285 }
2286
2287 // ---------------------------------------------------------
2288 // wxDataViewToggleRenderer
2289 // ---------------------------------------------------------
2290
2291 extern "C" {
2292 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
2293 gchar *path, gpointer user_data );
2294 }
2295
2296 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
2297 gchar *path, gpointer user_data )
2298 {
2299 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
2300
2301 // get old value
2302 GValue gvalue = { 0, };
2303 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2304 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
2305 bool tmp = g_value_get_boolean( &gvalue );
2306 g_value_unset( &gvalue );
2307 // invert it
2308 tmp = !tmp;
2309
2310 wxVariant value = tmp;
2311 if (!cell->Validate( value ))
2312 return;
2313
2314 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
2315 wxDataViewModel *model = ctrl->GetModel();
2316
2317 wxDataViewItem item(ctrl->GTKPathToItem(wxGtkTreePath(path)));
2318
2319 unsigned int model_col = cell->GetOwner()->GetModelColumn();
2320
2321 model->ChangeValue( value, item, model_col );
2322 }
2323
2324 IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
2325
2326 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
2327 wxDataViewCellMode mode, int align ) :
2328 wxDataViewRenderer( varianttype, mode, align )
2329 {
2330 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
2331
2332 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
2333 {
2334 g_signal_connect_after( m_renderer, "toggled",
2335 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
2336 }
2337 else
2338 {
2339 GValue gvalue = { 0, };
2340 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2341 g_value_set_boolean( &gvalue, false );
2342 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
2343 g_value_unset( &gvalue );
2344 }
2345
2346 SetMode(mode);
2347 SetAlignment(align);
2348 }
2349
2350 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
2351 {
2352 bool tmp = value;
2353
2354 GValue gvalue = { 0, };
2355 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2356 g_value_set_boolean( &gvalue, tmp );
2357 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
2358 g_value_unset( &gvalue );
2359
2360 return true;
2361 }
2362
2363 bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
2364 {
2365 GValue gvalue = { 0, };
2366 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2367 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
2368 bool tmp = g_value_get_boolean( &gvalue );
2369 g_value_unset( &gvalue );
2370
2371 value = tmp;
2372
2373 return true;
2374 }
2375
2376 // ---------------------------------------------------------
2377 // wxDataViewCustomRenderer
2378 // ---------------------------------------------------------
2379
2380 class wxDataViewCtrlDCImpl: public wxWindowDCImpl
2381 {
2382 public:
2383 wxDataViewCtrlDCImpl( wxDC *owner, wxDataViewCtrl *window ) :
2384 wxWindowDCImpl( owner )
2385 {
2386 GtkWidget *widget = window->m_treeview;
2387 // Set later
2388 m_gdkwindow = NULL;
2389
2390 m_window = window;
2391
2392 m_context = window->GTKGetPangoDefaultContext();
2393 m_layout = pango_layout_new( m_context );
2394 m_fontdesc = pango_font_description_copy(gtk_widget_get_style(widget)->font_desc);
2395
2396 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
2397
2398 // Set m_gdkwindow later
2399 // SetUpDC();
2400 }
2401 };
2402
2403 class wxDataViewCtrlDC: public wxWindowDC
2404 {
2405 public:
2406 wxDataViewCtrlDC( wxDataViewCtrl *window ) :
2407 wxWindowDC( new wxDataViewCtrlDCImpl( this, window ) )
2408 { }
2409 };
2410
2411
2412 // ---------------------------------------------------------
2413 // wxDataViewCustomRenderer
2414 // ---------------------------------------------------------
2415
2416 IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
2417
2418 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
2419 wxDataViewCellMode mode,
2420 int align,
2421 bool no_init )
2422 : wxDataViewCustomRendererBase( varianttype, mode, align )
2423 {
2424 m_dc = NULL;
2425 m_text_renderer = NULL;
2426
2427 if (no_init)
2428 m_renderer = NULL;
2429 else
2430 Init(mode, align);
2431 }
2432
2433 void wxDataViewCustomRenderer::GtkInitTextRenderer()
2434 {
2435 m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2436 g_object_ref_sink(m_text_renderer);
2437
2438 GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer));
2439 }
2440
2441 GtkCellRendererText *wxDataViewCustomRenderer::GtkGetTextRenderer() const
2442 {
2443 if ( !m_text_renderer )
2444 {
2445 // we create it on demand so need to do it even from a const function
2446 const_cast<wxDataViewCustomRenderer *>(this)->GtkInitTextRenderer();
2447 }
2448
2449 return m_text_renderer;
2450 }
2451
2452 void wxDataViewCustomRenderer::RenderText( const wxString &text,
2453 int xoffset,
2454 wxRect cell,
2455 wxDC *WXUNUSED(dc),
2456 int WXUNUSED(state) )
2457 {
2458
2459 GtkCellRendererText * const textRenderer = GtkGetTextRenderer();
2460
2461 GValue gvalue = { 0, };
2462 g_value_init( &gvalue, G_TYPE_STRING );
2463 g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) );
2464 g_object_set_property( G_OBJECT(textRenderer), "text", &gvalue );
2465 g_value_unset( &gvalue );
2466
2467 GtkApplyAttr(textRenderer, GetAttr());
2468
2469 GdkRectangle cell_area;
2470 wxRectToGDKRect(cell, cell_area);
2471 cell_area.x += xoffset;
2472 cell_area.width -= xoffset;
2473
2474 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer),
2475 m_renderParams.window,
2476 m_renderParams.widget,
2477 m_renderParams.background_area,
2478 &cell_area,
2479 m_renderParams.expose_area,
2480 (GtkCellRendererState) m_renderParams.flags );
2481 }
2482
2483 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
2484 {
2485 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
2486 renderer->cell = this;
2487
2488 m_renderer = (GtkCellRenderer*) renderer;
2489
2490 SetMode(mode);
2491 SetAlignment(align);
2492
2493 GtkInitHandlers();
2494
2495 return true;
2496 }
2497
2498 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2499 {
2500 if (m_dc)
2501 delete m_dc;
2502
2503 if (m_text_renderer)
2504 g_object_unref(m_text_renderer);
2505 }
2506
2507 wxDC *wxDataViewCustomRenderer::GetDC()
2508 {
2509 if (m_dc == NULL)
2510 {
2511 if (GetOwner() == NULL)
2512 return NULL;
2513 if (GetOwner()->GetOwner() == NULL)
2514 return NULL;
2515 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2516 }
2517
2518 return m_dc;
2519 }
2520
2521 // ---------------------------------------------------------
2522 // wxDataViewProgressRenderer
2523 // ---------------------------------------------------------
2524
2525 IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
2526
2527 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
2528 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
2529 wxDataViewCustomRenderer( varianttype, mode, align, true )
2530 {
2531 m_label = label;
2532 m_value = 0;
2533
2534 #ifdef __WXGTK26__
2535 if (!gtk_check_version(2,6,0))
2536 {
2537 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
2538
2539 SetMode(mode);
2540 SetAlignment(align);
2541
2542 #if !wxUSE_UNICODE
2543 // We can't initialize the renderer just yet because we don't have the
2544 // pointer to the column that uses this renderer yet and so attempt to
2545 // dereference GetOwner() to get the font that is used as a source of
2546 // encoding in multibyte-to-Unicode conversion in GTKSetLabel() in
2547 // non-Unicode builds would crash. So simply remember to do it later.
2548 if ( !m_label.empty() )
2549 m_needsToSetLabel = true;
2550 else
2551 #endif // !wxUSE_UNICODE
2552 GTKSetLabel();
2553 }
2554 else
2555 #endif
2556 {
2557 // Use custom cell code
2558 wxDataViewCustomRenderer::Init(mode, align);
2559 }
2560 }
2561
2562 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2563 {
2564 }
2565
2566 void wxDataViewProgressRenderer::GTKSetLabel()
2567 {
2568 GValue gvalue = { 0, };
2569 g_value_init( &gvalue, G_TYPE_STRING );
2570
2571 // Take care to not use GetOwner() here if the label is empty, we can be
2572 // called from ctor when GetOwner() is still NULL in this case.
2573 wxScopedCharBuffer buf;
2574 if ( m_label.empty() )
2575 buf = wxScopedCharBuffer::CreateNonOwned("");
2576 else
2577 buf = wxGTK_CONV_FONT(m_label, GetOwner()->GetOwner()->GetFont());
2578
2579 g_value_set_string( &gvalue, buf);
2580 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2581 g_value_unset( &gvalue );
2582
2583 #if !wxUSE_UNICODE
2584 m_needsToSetLabel = false;
2585 #endif // !wxUSE_UNICODE
2586 }
2587
2588 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
2589 {
2590 #ifdef __WXGTK26__
2591 if (!gtk_check_version(2,6,0))
2592 {
2593 #if !wxUSE_UNICODE
2594 if ( m_needsToSetLabel )
2595 GTKSetLabel();
2596 #endif // !wxUSE_UNICODE
2597
2598 gint tmp = (long) value;
2599 GValue gvalue = { 0, };
2600 g_value_init( &gvalue, G_TYPE_INT );
2601 g_value_set_int( &gvalue, tmp );
2602 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
2603 g_value_unset( &gvalue );
2604 }
2605 else
2606 #endif
2607 {
2608 m_value = (long) value;
2609
2610 if (m_value < 0) m_value = 0;
2611 if (m_value > 100) m_value = 100;
2612 }
2613
2614 return true;
2615 }
2616
2617 bool wxDataViewProgressRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2618 {
2619 return false;
2620 }
2621
2622 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
2623 {
2624 double pct = (double)m_value / 100.0;
2625 wxRect bar = cell;
2626 bar.width = (int)(cell.width * pct);
2627 dc->SetPen( *wxTRANSPARENT_PEN );
2628 dc->SetBrush( *wxBLUE_BRUSH );
2629 dc->DrawRectangle( bar );
2630
2631 dc->SetBrush( *wxTRANSPARENT_BRUSH );
2632 dc->SetPen( *wxBLACK_PEN );
2633 dc->DrawRectangle( cell );
2634
2635 return true;
2636 }
2637
2638 wxSize wxDataViewProgressRenderer::GetSize() const
2639 {
2640 return wxSize(40,12);
2641 }
2642
2643 // -------------------------------------
2644 // wxDataViewChoiceRenderer
2645 // -------------------------------------
2646
2647 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString &choices,
2648 wxDataViewCellMode mode, int alignment ) :
2649 wxDataViewCustomRenderer( "string", mode, alignment, true )
2650 {
2651 m_choices = choices;
2652
2653 #ifdef __WXGTK26__
2654 if (!gtk_check_version(2,6,0))
2655 {
2656 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_combo_new();
2657
2658 GtkListStore *store = gtk_list_store_new( 1, G_TYPE_STRING );
2659 for (size_t n = 0; n < m_choices.GetCount(); n++)
2660 {
2661 gtk_list_store_insert_with_values(
2662 store, NULL, n, 0,
2663 static_cast<const char *>(m_choices[n].utf8_str()), -1 );
2664 }
2665
2666 g_object_set (m_renderer,
2667 "model", store,
2668 "text-column", 0,
2669 "has-entry", FALSE,
2670 NULL);
2671
2672 bool editable = (mode & wxDATAVIEW_CELL_EDITABLE);
2673 g_object_set (m_renderer, "editable", editable, NULL);
2674
2675 SetAlignment(alignment);
2676
2677 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2678
2679 GtkInitHandlers();
2680 }
2681 else
2682 #endif
2683 {
2684 // Use custom cell code
2685 wxDataViewCustomRenderer::Init(mode, alignment);
2686 }
2687 }
2688
2689 bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
2690 {
2691 RenderText( m_data, 0, rect, dc, state );
2692 return true;
2693 }
2694
2695 wxSize wxDataViewChoiceRenderer::GetSize() const
2696 {
2697 return wxSize(70,20);
2698 }
2699
2700 bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
2701 {
2702
2703 #ifdef __WXGTK26__
2704 if (!gtk_check_version(2,6,0))
2705 {
2706 GValue gvalue = { 0, };
2707 g_value_init( &gvalue, G_TYPE_STRING );
2708 g_value_set_string(&gvalue,
2709 wxGTK_CONV_FONT(value.GetString(),
2710 GetOwner()->GetOwner()->GetFont()));
2711 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2712 g_value_unset( &gvalue );
2713 }
2714 else
2715 #endif
2716 m_data = value.GetString();
2717
2718 return true;
2719 }
2720
2721 bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
2722 {
2723 #ifdef __WXGTK26__
2724 if (!gtk_check_version(2,6,0))
2725 {
2726 GValue gvalue = { 0, };
2727 g_value_init( &gvalue, G_TYPE_STRING );
2728 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
2729 wxString temp = wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue),
2730 GetOwner()->GetOwner()->GetFont());
2731 g_value_unset( &gvalue );
2732 value = temp;
2733
2734 //wxPrintf( "temp %s\n", temp );
2735 // TODO: remove this code
2736 }
2737 else
2738 #endif
2739 value = m_data;
2740
2741 return true;
2742 }
2743
2744 void wxDataViewChoiceRenderer::SetAlignment( int align )
2745 {
2746 wxDataViewCustomRenderer::SetAlignment(align);
2747
2748 if (gtk_check_version(2,10,0))
2749 return;
2750
2751 // horizontal alignment:
2752 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2753 if (align & wxALIGN_RIGHT)
2754 pangoAlign = PANGO_ALIGN_RIGHT;
2755 else if (align & wxALIGN_CENTER_HORIZONTAL)
2756 pangoAlign = PANGO_ALIGN_CENTER;
2757
2758 GValue gvalue = { 0, };
2759 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2760 g_value_set_enum( &gvalue, pangoAlign );
2761 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2762 g_value_unset( &gvalue );
2763 }
2764
2765 // ----------------------------------------------------------------------------
2766 // wxDataViewChoiceByIndexRenderer
2767 // ----------------------------------------------------------------------------
2768
2769 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
2770 wxDataViewCellMode mode, int alignment ) :
2771 wxDataViewChoiceRenderer( choices, mode, alignment )
2772 {
2773 }
2774
2775 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const char *itempath, const wxString& str)
2776 {
2777 wxVariant value( (long) GetChoices().Index( str ) );
2778
2779 if (!Validate( value ))
2780 return;
2781
2782 wxDataViewItem
2783 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
2784
2785 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
2786 }
2787
2788 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
2789 {
2790 wxVariant string_value = GetChoice( value.GetLong() );
2791 return wxDataViewChoiceRenderer::SetValue( string_value );
2792 }
2793
2794 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const
2795 {
2796 wxVariant string_value;
2797 if (!wxDataViewChoiceRenderer::GetValue( string_value ))
2798 return false;
2799
2800 value = (long) GetChoices().Index( string_value.GetString() );
2801 return true;
2802 }
2803
2804 // ---------------------------------------------------------
2805 // wxDataViewIconTextRenderer
2806 // ---------------------------------------------------------
2807
2808 IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
2809
2810 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2811 (
2812 const wxString &varianttype,
2813 wxDataViewCellMode mode,
2814 int align
2815 )
2816 : wxDataViewTextRenderer(varianttype, mode, align)
2817 {
2818 m_rendererIcon = gtk_cell_renderer_pixbuf_new();
2819 }
2820
2821 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2822 {
2823 }
2824
2825 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
2826 {
2827 // add the icon renderer first
2828 gtk_tree_view_column_pack_start(column, m_rendererIcon, FALSE /* !expand */);
2829
2830 // add the text renderer too
2831 wxDataViewRenderer::GtkPackIntoColumn(column);
2832 }
2833
2834 bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
2835 {
2836 m_value << value;
2837
2838 SetTextValue(m_value.GetText());
2839 SetPixbufProp(m_rendererIcon, m_value.GetIcon().GetPixbuf());
2840
2841 return true;
2842 }
2843
2844 bool wxDataViewIconTextRenderer::GetValue(wxVariant& value) const
2845 {
2846 wxString str;
2847 if ( !GetTextValue(str) )
2848 return false;
2849
2850 // user doesn't have any way to edit the icon so leave it unchanged
2851 value << wxDataViewIconText(str, m_value.GetIcon());
2852
2853 return true;
2854 }
2855
2856 void
2857 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant& value,
2858 const wxDataViewItem& item,
2859 unsigned col)
2860 {
2861 // we receive just the text part of our value as it's the only one which
2862 // can be edited, but we need the full wxDataViewIconText value for the
2863 // model
2864 wxVariant valueIconText;
2865 valueIconText << wxDataViewIconText(value.GetString(), m_value.GetIcon());
2866 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText, item, col);
2867 }
2868
2869 // ---------------------------------------------------------
2870 // wxDataViewColumn
2871 // ---------------------------------------------------------
2872
2873
2874 static gboolean
2875 gtk_dataview_header_button_press_callback( GtkWidget *WXUNUSED(widget),
2876 GdkEventButton *gdk_event,
2877 wxDataViewColumn *column )
2878 {
2879 if (gdk_event->type != GDK_BUTTON_PRESS)
2880 return FALSE;
2881
2882 if (gdk_event->button == 1)
2883 {
2884 gs_lastLeftClickHeader = column;
2885
2886 wxDataViewCtrl *dv = column->GetOwner();
2887 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
2888 event.SetDataViewColumn( column );
2889 event.SetModel( dv->GetModel() );
2890 if (dv->HandleWindowEvent( event ))
2891 return FALSE;
2892 }
2893
2894 if (gdk_event->button == 3)
2895 {
2896 wxDataViewCtrl *dv = column->GetOwner();
2897 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, dv->GetId() );
2898 event.SetDataViewColumn( column );
2899 event.SetModel( dv->GetModel() );
2900 if (dv->HandleWindowEvent( event ))
2901 return FALSE;
2902 }
2903
2904 return FALSE;
2905 }
2906
2907 extern "C"
2908 {
2909
2910 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *WXUNUSED(column),
2911 GtkCellRenderer *renderer,
2912 GtkTreeModel *model,
2913 GtkTreeIter *iter,
2914 gpointer data )
2915 {
2916 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
2917 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
2918
2919 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
2920
2921 wxDataViewItem item( (void*) iter->user_data );
2922
2923 wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel();
2924
2925 if (!wx_model->IsVirtualListModel())
2926 {
2927 gboolean visible;
2928 if (wx_model->IsContainer( item ))
2929 {
2930 visible = wx_model->HasContainerColumns( item ) ||
2931 (cell->GetOwner()->GetModelColumn() == 0);
2932 }
2933 else
2934 {
2935 visible = true;
2936 }
2937
2938 GValue gvalue = { 0, };
2939 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2940 g_value_set_boolean( &gvalue, visible );
2941 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2942 g_value_unset( &gvalue );
2943
2944 if ( !visible )
2945 return;
2946 }
2947
2948 wxVariant value;
2949 wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
2950
2951 if (value.GetType() != cell->GetVariantType())
2952 {
2953 wxLogError( wxT("Wrong type, required: %s but: %s"),
2954 value.GetType().c_str(),
2955 cell->GetVariantType().c_str() );
2956 }
2957
2958 cell->SetValue( value );
2959
2960 // deal with disabled items
2961 bool enabled = wx_model->IsEnabled( item, cell->GetOwner()->GetModelColumn() );
2962
2963 // a) this sets the appearance to disabled grey
2964 GValue gvalue = { 0, };
2965 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2966 g_value_set_boolean( &gvalue, enabled );
2967 g_object_set_property( G_OBJECT(renderer), "sensitive", &gvalue );
2968 g_value_unset( &gvalue );
2969
2970 // b) this actually disables the control/renderer
2971 if (enabled)
2972 cell->SetMode( cell->GtkGetMode() );
2973 else
2974 cell->SetMode( wxDATAVIEW_CELL_INERT );
2975
2976
2977 // deal with attributes: if the renderer doesn't support them at all, we
2978 // don't even need to query the model for them
2979 if ( !cell->GtkSupportsAttrs() )
2980 return;
2981
2982 // it can support attributes so check if this item has any
2983 wxDataViewItemAttr attr;
2984 if ( wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr )
2985 || !cell->GtkIsUsingDefaultAttrs() )
2986 {
2987 bool usingDefaultAttrs = !cell->GtkSetAttr(attr);
2988 cell->GtkSetUsingDefaultAttrs(usingDefaultAttrs);
2989 }
2990 // else: no custom attributes specified and we're already using the default
2991 // ones -- nothing to do
2992
2993 }
2994
2995 } // extern "C"
2996
2997 #include <wx/listimpl.cpp>
2998 WX_DEFINE_LIST(wxDataViewColumnList)
2999
3000 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
3001 unsigned int model_column, int width,
3002 wxAlignment align, int flags )
3003 : wxDataViewColumnBase( cell, model_column )
3004 {
3005 Init( align, flags, width );
3006
3007 SetTitle( title );
3008 }
3009
3010 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
3011 unsigned int model_column, int width,
3012 wxAlignment align, int flags )
3013 : wxDataViewColumnBase( bitmap, cell, model_column )
3014 {
3015 Init( align, flags, width );
3016
3017 SetBitmap( bitmap );
3018 }
3019
3020 void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
3021 {
3022 m_isConnected = false;
3023
3024 GtkTreeViewColumn *column = gtk_tree_view_column_new();
3025 m_column = (GtkWidget*) column;
3026
3027 SetFlags( flags );
3028 SetAlignment( align );
3029
3030 SetWidth( width );
3031
3032 // Create container for icon and label
3033 GtkWidget *box = gtk_hbox_new( FALSE, 1 );
3034 gtk_widget_show( box );
3035 // gtk_container_set_border_width((GtkContainer*)box, 2);
3036 m_image = gtk_image_new();
3037 gtk_box_pack_start(GTK_BOX(box), m_image, FALSE, FALSE, 1);
3038 m_label = gtk_label_new("");
3039 gtk_box_pack_end( GTK_BOX(box), GTK_WIDGET(m_label), FALSE, FALSE, 1 );
3040 gtk_tree_view_column_set_widget( column, box );
3041
3042 wxDataViewRenderer * const colRenderer = GetRenderer();
3043 GtkCellRenderer * const cellRenderer = colRenderer->GetGtkHandle();
3044
3045 colRenderer->GtkPackIntoColumn(column);
3046
3047 gtk_tree_view_column_set_cell_data_func( column, cellRenderer,
3048 wxGtkTreeCellDataFunc, (gpointer) colRenderer, NULL );
3049 }
3050
3051 void wxDataViewColumn::OnInternalIdle()
3052 {
3053 if (m_isConnected)
3054 return;
3055
3056 if (gtk_widget_get_realized(GetOwner()->m_treeview))
3057 {
3058 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3059 if (column->button)
3060 {
3061 g_signal_connect(column->button, "button_press_event",
3062 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
3063
3064 // otherwise the event will be blocked by GTK+
3065 gtk_tree_view_column_set_clickable( column, TRUE );
3066
3067 m_isConnected = true;
3068 }
3069 }
3070 }
3071
3072 void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
3073 {
3074 wxDataViewColumnBase::SetOwner( owner );
3075
3076 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3077
3078 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3079 }
3080
3081 void wxDataViewColumn::SetTitle( const wxString &title )
3082 {
3083 wxDataViewCtrl *ctrl = GetOwner();
3084 gtk_label_set_text( GTK_LABEL(m_label), ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
3085 : wxGTK_CONV_SYS(title) );
3086 if (title.empty())
3087 gtk_widget_hide( m_label );
3088 else
3089 gtk_widget_show( m_label );
3090 }
3091
3092 wxString wxDataViewColumn::GetTitle() const
3093 {
3094 return wxGTK_CONV_BACK_FONT(
3095 gtk_label_get_text( GTK_LABEL(m_label) ),
3096 GetOwner()->GetFont()
3097 );
3098 }
3099
3100 void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
3101 {
3102 wxDataViewColumnBase::SetBitmap( bitmap );
3103
3104 if (bitmap.IsOk())
3105 {
3106 GtkImage *gtk_image = GTK_IMAGE(m_image);
3107
3108 GdkBitmap *mask = NULL;
3109 if (bitmap.GetMask())
3110 mask = bitmap.GetMask()->GetBitmap();
3111
3112 if (bitmap.HasPixbuf())
3113 {
3114 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
3115 bitmap.GetPixbuf());
3116 }
3117 else
3118 {
3119 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
3120 bitmap.GetPixmap(), mask);
3121 }
3122 gtk_widget_show( m_image );
3123 }
3124 else
3125 {
3126 gtk_widget_hide( m_image );
3127 }
3128 }
3129
3130 void wxDataViewColumn::SetHidden( bool hidden )
3131 {
3132 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
3133 }
3134
3135 void wxDataViewColumn::SetResizeable( bool resizable )
3136 {
3137 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizable );
3138 }
3139
3140 void wxDataViewColumn::SetAlignment( wxAlignment align )
3141 {
3142 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3143
3144 gfloat xalign = 0.0;
3145 if (align == wxALIGN_RIGHT)
3146 xalign = 1.0;
3147 if (align == wxALIGN_CENTER_HORIZONTAL ||
3148 align == wxALIGN_CENTER)
3149 xalign = 0.5;
3150
3151 gtk_tree_view_column_set_alignment( column, xalign );
3152
3153 if (m_renderer && m_renderer->GetAlignment() == -1)
3154 m_renderer->GtkUpdateAlignment();
3155 }
3156
3157 wxAlignment wxDataViewColumn::GetAlignment() const
3158 {
3159 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
3160
3161 if (xalign == 1.0)
3162 return wxALIGN_RIGHT;
3163 if (xalign == 0.5)
3164 return wxALIGN_CENTER_HORIZONTAL;
3165
3166 return wxALIGN_LEFT;
3167 }
3168
3169 void wxDataViewColumn::SetSortable( bool sortable )
3170 {
3171 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3172
3173 if ( sortable )
3174 {
3175 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
3176 }
3177 else
3178 {
3179 gtk_tree_view_column_set_sort_column_id( column, -1 );
3180 gtk_tree_view_column_set_sort_indicator( column, FALSE );
3181 gtk_tree_view_column_set_clickable( column, FALSE );
3182 }
3183 }
3184
3185 bool wxDataViewColumn::IsSortable() const
3186 {
3187 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3188 return gtk_tree_view_column_get_clickable( column );
3189 }
3190
3191 bool wxDataViewColumn::IsSortKey() const
3192 {
3193 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3194 return gtk_tree_view_column_get_sort_indicator( column );
3195 }
3196
3197 bool wxDataViewColumn::IsResizeable() const
3198 {
3199 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3200 return gtk_tree_view_column_get_resizable( column );
3201 }
3202
3203 bool wxDataViewColumn::IsHidden() const
3204 {
3205 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3206 return !gtk_tree_view_column_get_visible( column );
3207 }
3208
3209 void wxDataViewColumn::SetSortOrder( bool ascending )
3210 {
3211 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3212
3213 if (ascending)
3214 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
3215 else
3216 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
3217
3218 gtk_tree_view_column_set_sort_indicator( column, TRUE );
3219 }
3220
3221 bool wxDataViewColumn::IsSortOrderAscending() const
3222 {
3223 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3224
3225 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
3226 }
3227
3228 void wxDataViewColumn::SetMinWidth( int width )
3229 {
3230 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3231 }
3232
3233 int wxDataViewColumn::GetMinWidth() const
3234 {
3235 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
3236 }
3237
3238 int wxDataViewColumn::GetWidth() const
3239 {
3240 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
3241 }
3242
3243 void wxDataViewColumn::SetWidth( int width )
3244 {
3245 if ( width == wxCOL_WIDTH_AUTOSIZE )
3246 {
3247 // NB: this disables user resizing
3248 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
3249 }
3250 else
3251 {
3252 if ( width == wxCOL_WIDTH_DEFAULT )
3253 {
3254 // TODO find a better calculation
3255 width = wxDVC_DEFAULT_WIDTH;
3256 }
3257
3258 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
3259 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3260 }
3261 }
3262
3263 void wxDataViewColumn::SetReorderable( bool reorderable )
3264 {
3265 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
3266 }
3267
3268 bool wxDataViewColumn::IsReorderable() const
3269 {
3270 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) );
3271 }
3272
3273 //-----------------------------------------------------------------------------
3274 // wxGtkTreeModelNode
3275 //-----------------------------------------------------------------------------
3276
3277 #if 0
3278 class wxGtkTreeModelChildWithPos
3279 {
3280 public:
3281 unsigned int pos;
3282 void *id;
3283 };
3284
3285 static
3286 int wxGtkTreeModelChildWithPosCmp( const void* data1, const void* data2, const void* user_data )
3287 {
3288 const wxGtkTreeModelChildWithPos* child1 = (const wxGtkTreeModelChildWithPos*) data1;
3289 const wxGtkTreeModelChildWithPos* child2 = (const wxGtkTreeModelChildWithPos*) data2;
3290 const wxDataViewCtrlInternal *internal = (const wxDataViewCtrlInternal *) user_data;
3291 int ret = internal->GetDataViewModel()->Compare( child1->id, child2->id,
3292 internal->GetSortColumn(), (internal->GetSortOrder() == GTK_SORT_DESCENDING) );
3293
3294 return ret;
3295 }
3296 #else
3297 static
3298 int LINKAGEMODE wxGtkTreeModelChildPtrCmp( void*** data1, void*** data2 )
3299 {
3300 return gs_internal->GetDataViewModel()->Compare( wxDataViewItem(**data1), wxDataViewItem(**data2),
3301 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
3302 }
3303
3304 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr );
3305 #endif
3306
3307 void wxGtkTreeModelNode::Resort()
3308 {
3309 size_t child_count = GetChildCount();
3310 if (child_count == 0)
3311 return;
3312
3313 size_t node_count = GetNodesCount();
3314
3315 if (child_count == 1)
3316 {
3317 if (node_count == 1)
3318 {
3319 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
3320 node->Resort();
3321 }
3322 return;
3323 }
3324
3325 gint *new_order = new gint[child_count];
3326
3327 #if 1
3328 // m_children has the original *void
3329 // ptrs points to these
3330 wxGtkTreeModelChildrenPtr ptrs;
3331 size_t i;
3332 for (i = 0; i < child_count; i++)
3333 ptrs.Add( &(m_children[i]) );
3334 // Sort the ptrs
3335 gs_internal = m_internal;
3336 ptrs.Sort( &wxGtkTreeModelChildPtrCmp );
3337
3338 wxGtkTreeModelChildren temp;
3339 void** base_ptr = &(m_children[0]);
3340 // Transfer positions to new_order array and
3341 // IDs to temp
3342 for (i = 0; i < child_count; i++)
3343 {
3344 new_order[i] = ptrs[i] - base_ptr;
3345 temp.Add( *ptrs[i] );
3346 }
3347
3348 // Transfer IDs back to m_children
3349 m_children.Clear();
3350 WX_APPEND_ARRAY( temp, m_children );
3351 #endif
3352 #if 0
3353 // Too slow
3354
3355 // Build up array with IDs and original positions
3356 wxGtkTreeModelChildWithPos* temp = new wxGtkTreeModelChildWithPos[child_count];
3357 size_t i;
3358 for (i = 0; i < child_count; i++)
3359 {
3360 temp[i].pos = i;
3361 temp[i].id = m_children[i];
3362 }
3363 // Sort array keeping original positions
3364 wxQsort( temp, child_count, sizeof(wxGtkTreeModelChildWithPos),
3365 &wxGtkTreeModelChildWithPosCmp, m_internal );
3366 // Transfer positions to new_order array and
3367 // IDs to m_children
3368 m_children.Clear();
3369 for (i = 0; i < child_count; i++)
3370 {
3371 new_order[i] = temp[i].pos;
3372 m_children.Add( temp[i].id );
3373 }
3374 // Delete array
3375 delete [] temp;
3376 #endif
3377
3378 #if 0
3379 // Too slow
3380
3381 wxGtkTreeModelChildren temp;
3382 WX_APPEND_ARRAY( temp, m_children );
3383
3384 gs_internal = m_internal;
3385 m_children.Sort( &wxGtkTreeModelChildCmp );
3386
3387 unsigned int pos;
3388 for (pos = 0; pos < child_count; pos++)
3389 {
3390 void *id = m_children.Item( pos );
3391 int old_pos = temp.Index( id );
3392 new_order[pos] = old_pos;
3393 }
3394 #endif
3395
3396 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
3397
3398 GtkTreeIter iter;
3399 iter.user_data = GetItem().GetID();
3400 iter.stamp = m_internal->GetGtkModel()->stamp;
3401
3402 gtk_tree_model_rows_reordered( gtk_tree_model,
3403 wxGtkTreePath(m_internal->get_path(&iter)), &iter, new_order );
3404
3405 delete [] new_order;
3406
3407 unsigned int pos;
3408 for (pos = 0; pos < node_count; pos++)
3409 {
3410 wxGtkTreeModelNode *node = m_nodes.Item( pos );
3411 node->Resort();
3412 }
3413 }
3414
3415 //-----------------------------------------------------------------------------
3416 // wxDataViewCtrlInternal
3417 //-----------------------------------------------------------------------------
3418
3419 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model )
3420 {
3421 m_owner = owner;
3422 m_wx_model = wx_model;
3423
3424 m_gtk_model = NULL;
3425 m_root = NULL;
3426 m_sort_order = GTK_SORT_ASCENDING;
3427 m_sort_column = -1;
3428 m_dataview_sort_column = NULL;
3429
3430 m_dragDataObject = NULL;
3431 m_dropDataObject = NULL;
3432
3433 m_dirty = false;
3434
3435 m_gtk_model = wxgtk_tree_model_new();
3436 m_gtk_model->internal = this;
3437
3438 m_notifier = new wxGtkDataViewModelNotifier( wx_model, this );
3439
3440 wx_model->AddNotifier( m_notifier );
3441
3442 // g_object_unref( gtk_model ); ???
3443
3444 if (!m_wx_model->IsVirtualListModel())
3445 InitTree();
3446
3447 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model) );
3448 }
3449
3450 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3451 {
3452 m_wx_model->RemoveNotifier( m_notifier );
3453
3454 // remove the model from the GtkTreeView before it gets destroyed
3455 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner->GtkGetTreeView() ), NULL );
3456
3457 g_object_unref( m_gtk_model );
3458
3459 delete m_dragDataObject;
3460 delete m_dropDataObject;
3461 }
3462
3463 void wxDataViewCtrlInternal::ScheduleRefresh()
3464 {
3465 m_dirty = true;
3466 }
3467
3468 void wxDataViewCtrlInternal::OnInternalIdle()
3469 {
3470 if (m_dirty)
3471 {
3472 GtkWidget *widget = m_owner->GtkGetTreeView();
3473 gtk_widget_queue_draw( widget );
3474 m_dirty = false;
3475 }
3476 }
3477
3478 void wxDataViewCtrlInternal::InitTree()
3479 {
3480 wxDataViewItem item;
3481 m_root = new wxGtkTreeModelNode( NULL, item, this );
3482
3483 BuildBranch( m_root );
3484 }
3485
3486 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
3487 {
3488 if (node->GetChildCount() == 0)
3489 {
3490 wxDataViewItemArray children;
3491 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
3492
3493 unsigned int pos;
3494 for (pos = 0; pos < count; pos++)
3495 {
3496 wxDataViewItem child = children[pos];
3497
3498 if (m_wx_model->IsContainer( child ))
3499 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
3500 else
3501 node->AddLeaf( child.GetID() );
3502
3503 // Don't send any events here
3504 }
3505 }
3506 }
3507
3508 // GTK+ dnd iface
3509
3510 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format )
3511 {
3512 wxGtkString atom_str( gdk_atom_name( format ) );
3513 m_dragSourceTargetEntryTarget = wxCharBuffer( atom_str );
3514
3515 m_dragSourceTargetEntry.target = m_dragSourceTargetEntryTarget.data();
3516 m_dragSourceTargetEntry.flags = 0;
3517 m_dragSourceTargetEntry.info = static_cast<guint>(-1);
3518
3519 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3520 GDK_BUTTON1_MASK, &m_dragSourceTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
3521
3522 return true;
3523 }
3524
3525 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat &format )
3526 {
3527 wxGtkString atom_str( gdk_atom_name( format ) );
3528 m_dropTargetTargetEntryTarget = wxCharBuffer( atom_str );
3529
3530 m_dropTargetTargetEntry.target = m_dropTargetTargetEntryTarget.data();
3531 m_dropTargetTargetEntry.flags = 0;
3532 m_dropTargetTargetEntry.info = static_cast<guint>(-1);
3533
3534 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3535 &m_dropTargetTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
3536
3537 return true;
3538 }
3539
3540 gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
3541 GtkTreePath *path )
3542 {
3543 delete m_dragDataObject;
3544 m_dragDataObject = NULL;
3545
3546 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3547 if ( !item )
3548 return FALSE;
3549
3550 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
3551 event.SetEventObject( m_owner );
3552 event.SetItem( item );
3553 event.SetModel( m_wx_model );
3554 gint x, y;
3555 gtk_widget_get_pointer(m_owner->GtkGetTreeView(), &x, &y);
3556 event.SetPosition(x, y);
3557 if (!m_owner->HandleWindowEvent( event ))
3558 return FALSE;
3559
3560 if (!event.IsAllowed())
3561 return FALSE;
3562
3563 wxDataObject *obj = event.GetDataObject();
3564 if (!obj)
3565 return FALSE;
3566
3567 m_dragDataObject = obj;
3568
3569 return TRUE;
3570 }
3571
3572 gboolean
3573 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
3574 GtkTreePath *WXUNUSED(path))
3575 {
3576 return FALSE;
3577 }
3578
3579 gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
3580 GtkTreePath *path, GtkSelectionData *selection_data )
3581 {
3582 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3583 if ( !item )
3584 return FALSE;
3585
3586 GdkAtom target = gtk_selection_data_get_target(selection_data);
3587 if (!m_dragDataObject->IsSupported(target))
3588 return FALSE;
3589
3590 size_t size = m_dragDataObject->GetDataSize(target);
3591 if (size == 0)
3592 return FALSE;
3593
3594 void *buf = malloc( size );
3595
3596 gboolean res = FALSE;
3597 if (m_dragDataObject->GetDataHere(target, buf))
3598 {
3599 res = TRUE;
3600
3601 gtk_selection_data_set(selection_data, target,
3602 8, (const guchar*) buf, size );
3603 }
3604
3605 free( buf );
3606
3607 return res;
3608 }
3609
3610 gboolean
3611 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
3612 GtkTreePath *path,
3613 GtkSelectionData *selection_data)
3614 {
3615 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3616 if ( !item )
3617 return FALSE;
3618
3619 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() );
3620 event.SetEventObject( m_owner );
3621 event.SetItem( item );
3622 event.SetModel( m_wx_model );
3623 event.SetDataFormat(gtk_selection_data_get_target(selection_data));
3624 event.SetDataSize(gtk_selection_data_get_length(selection_data));
3625 event.SetDataBuffer(const_cast<guchar*>(gtk_selection_data_get_data(selection_data)));
3626 if (!m_owner->HandleWindowEvent( event ))
3627 return FALSE;
3628
3629 if (!event.IsAllowed())
3630 return FALSE;
3631
3632 return TRUE;
3633 }
3634
3635 gboolean
3636 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
3637 GtkTreePath *path,
3638 GtkSelectionData *selection_data)
3639 {
3640 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3641 if ( !item )
3642 return FALSE;
3643
3644 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
3645 event.SetEventObject( m_owner );
3646 event.SetItem( item );
3647 event.SetModel( m_wx_model );
3648 event.SetDataFormat(gtk_selection_data_get_target(selection_data));
3649 if (!m_owner->HandleWindowEvent( event ))
3650 return FALSE;
3651
3652 if (!event.IsAllowed())
3653 return FALSE;
3654
3655 return TRUE;
3656 }
3657
3658 // notifications from wxDataViewModel
3659
3660 bool wxDataViewCtrlInternal::Cleared()
3661 {
3662 if (m_root)
3663 {
3664 delete m_root;
3665 m_root = NULL;
3666 }
3667
3668 InitTree();
3669
3670 ScheduleRefresh();
3671
3672 return true;
3673 }
3674
3675 void wxDataViewCtrlInternal::Resort()
3676 {
3677 if (!m_wx_model->IsVirtualListModel())
3678 m_root->Resort();
3679
3680 ScheduleRefresh();
3681 }
3682
3683 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
3684 {
3685 if (!m_wx_model->IsVirtualListModel())
3686 {
3687 wxGtkTreeModelNode *parent_node = FindNode( parent );
3688 wxCHECK_MSG(parent_node, false,
3689 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3690
3691 wxDataViewItemArray modelSiblings;
3692 m_wx_model->GetChildren(parent, modelSiblings);
3693 const int modelSiblingsSize = modelSiblings.size();
3694
3695 int posInModel = modelSiblings.Index(item, /*fromEnd=*/true);
3696 wxCHECK_MSG( posInModel != wxNOT_FOUND, false, "adding non-existent item?" );
3697
3698 const wxGtkTreeModelChildren& nodeSiblings = parent_node->GetChildren();
3699 const int nodeSiblingsSize = nodeSiblings.size();
3700
3701 int nodePos = 0;
3702
3703 if ( posInModel == modelSiblingsSize - 1 )
3704 {
3705 nodePos = nodeSiblingsSize;
3706 }
3707 else if ( modelSiblingsSize == nodeSiblingsSize + 1 )
3708 {
3709 // This is the simple case when our node tree already matches the
3710 // model and only this one item is missing.
3711 nodePos = posInModel;
3712 }
3713 else
3714 {
3715 // It's possible that a larger discrepancy between the model and
3716 // our realization exists. This can happen e.g. when adding a bunch
3717 // of items to the model and then calling ItemsAdded() just once
3718 // afterwards. In this case, we must find the right position by
3719 // looking at sibling items.
3720
3721 // append to the end if we won't find a better position:
3722 nodePos = nodeSiblingsSize;
3723
3724 for ( int nextItemPos = posInModel + 1;
3725 nextItemPos < modelSiblingsSize;
3726 nextItemPos++ )
3727 {
3728 int nextNodePos = parent_node->FindChildByItem(modelSiblings[nextItemPos]);
3729 if ( nextNodePos != wxNOT_FOUND )
3730 {
3731 nodePos = nextNodePos;
3732 break;
3733 }
3734 }
3735 }
3736
3737 if (m_wx_model->IsContainer( item ))
3738 parent_node->InsertNode( new wxGtkTreeModelNode( parent_node, item, this ), nodePos );
3739 else
3740 parent_node->InsertLeaf( item.GetID(), nodePos );
3741 }
3742
3743 ScheduleRefresh();
3744
3745 return true;
3746 }
3747
3748 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
3749 {
3750 if (!m_wx_model->IsVirtualListModel())
3751 {
3752 wxGtkTreeModelNode *parent_node = FindNode( parent );
3753 wxASSERT_MSG(parent_node,
3754 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3755
3756 parent_node->DeleteChild( item.GetID() );
3757 }
3758
3759 ScheduleRefresh();
3760
3761 return true;
3762 }
3763
3764 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
3765 {
3766 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
3767 event.SetEventObject( m_owner );
3768 event.SetModel( m_owner->GetModel() );
3769 event.SetItem( item );
3770 m_owner->HandleWindowEvent( event );
3771
3772 return true;
3773 }
3774
3775 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int view_column )
3776 {
3777 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
3778 event.SetEventObject( m_owner );
3779 event.SetModel( m_owner->GetModel() );
3780 event.SetColumn( view_column );
3781 event.SetDataViewColumn( GetOwner()->GetColumn(view_column) );
3782 event.SetItem( item );
3783 m_owner->HandleWindowEvent( event );
3784
3785 return true;
3786 }
3787
3788 // GTK+ model iface
3789
3790 GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
3791 {
3792 int flags = 0;
3793
3794 if ( m_wx_model->IsListModel() )
3795 flags |= GTK_TREE_MODEL_LIST_ONLY;
3796
3797 if ( !m_wx_model->IsVirtualListModel() )
3798 flags |= GTK_TREE_MODEL_ITERS_PERSIST;
3799
3800 return GtkTreeModelFlags(flags);
3801 }
3802
3803 gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
3804 {
3805
3806 if (m_wx_model->IsVirtualListModel())
3807 {
3808 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3809
3810 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
3811
3812 if (i >= wx_model->GetCount())
3813 return FALSE;
3814
3815 iter->stamp = m_gtk_model->stamp;
3816 // user_data is just the index +1
3817 iter->user_data = (gpointer) (i+1);
3818
3819 return TRUE;
3820 }
3821 else
3822 {
3823 int depth = gtk_tree_path_get_depth( path );
3824
3825 wxGtkTreeModelNode *node = m_root;
3826
3827 int i;
3828 for (i = 0; i < depth; i++)
3829 {
3830 BuildBranch( node );
3831
3832 gint pos = gtk_tree_path_get_indices (path)[i];
3833 if (pos < 0) return FALSE;
3834 if ((size_t)pos >= node->GetChildCount()) return FALSE;
3835
3836 void* id = node->GetChildren().Item( (size_t) pos );
3837
3838 if (i == depth-1)
3839 {
3840 iter->stamp = m_gtk_model->stamp;
3841 iter->user_data = id;
3842 return TRUE;
3843 }
3844
3845 size_t count = node->GetNodes().GetCount();
3846 size_t pos2;
3847 for (pos2 = 0; pos2 < count; pos2++)
3848 {
3849 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
3850 if (child_node->GetItem().GetID() == id)
3851 {
3852 node = child_node;
3853 break;
3854 }
3855 }
3856 }
3857 }
3858
3859 return FALSE;
3860 }
3861
3862 GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
3863 {
3864 // When this is called from ItemDeleted(), the item is already
3865 // deleted in the model.
3866
3867 GtkTreePath *retval = gtk_tree_path_new ();
3868
3869 if (m_wx_model->IsVirtualListModel())
3870 {
3871 // iter is root, add nothing
3872 if (!iter->user_data)
3873 return retval;
3874
3875 // user_data is just the index +1
3876 int i = ( (wxUIntPtr) iter->user_data ) -1;
3877 gtk_tree_path_append_index (retval, i);
3878 }
3879 else
3880 {
3881 void *id = iter->user_data;
3882
3883 wxGtkTreeModelNode *node = FindParentNode( iter );
3884 while (node)
3885 {
3886 int pos = node->GetChildren().Index( id );
3887
3888 gtk_tree_path_prepend_index( retval, pos );
3889
3890 id = node->GetItem().GetID();
3891 node = node->GetParent();
3892 }
3893 }
3894
3895 return retval;
3896 }
3897
3898 gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
3899 {
3900 if (m_wx_model->IsVirtualListModel())
3901 {
3902 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3903
3904 // user_data is just the index +1
3905 int n = ( (wxUIntPtr) iter->user_data ) -1;
3906
3907 if (n == -1)
3908 {
3909 iter->user_data = NULL;
3910 return FALSE;
3911 }
3912
3913 if (n >= (int) wx_model->GetCount()-1)
3914 {
3915 iter->user_data = NULL;
3916 return FALSE;
3917 }
3918
3919 // user_data is just the index +1 (+2 because we need the next)
3920 iter->user_data = (gpointer) (n+2);
3921 }
3922 else
3923 {
3924 wxGtkTreeModelNode *parent = FindParentNode( iter );
3925 if( parent == NULL )
3926 {
3927 iter->user_data = NULL;
3928 return FALSE;
3929 }
3930
3931 int pos = parent->GetChildren().Index( iter->user_data );
3932
3933 if (pos == (int) parent->GetChildCount()-1)
3934 {
3935 iter->user_data = NULL;
3936 return FALSE;
3937 }
3938
3939 iter->user_data = parent->GetChildren().Item( pos+1 );
3940 }
3941
3942 return TRUE;
3943 }
3944
3945 gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
3946 {
3947 if (m_wx_model->IsVirtualListModel())
3948 {
3949 // this is a list, nodes have no children
3950 if (parent)
3951 return FALSE;
3952
3953 iter->stamp = m_gtk_model->stamp;
3954 iter->user_data = (gpointer) 1;
3955
3956 return TRUE;
3957 }
3958 else
3959 {
3960 if (iter == NULL)
3961 {
3962 if (m_root->GetChildCount() == 0) return FALSE;
3963 iter->stamp = m_gtk_model->stamp;
3964 iter->user_data = (gpointer) m_root->GetChildren().Item( 0 );
3965 return TRUE;
3966 }
3967
3968
3969 wxDataViewItem item;
3970 if (parent)
3971 item = wxDataViewItem( (void*) parent->user_data );
3972
3973 if (!m_wx_model->IsContainer( item ))
3974 return FALSE;
3975
3976 wxGtkTreeModelNode *parent_node = FindNode( parent );
3977 wxASSERT_MSG(parent_node,
3978 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3979
3980 BuildBranch( parent_node );
3981
3982 if (parent_node->GetChildCount() == 0)
3983 return FALSE;
3984
3985 iter->stamp = m_gtk_model->stamp;
3986 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
3987 }
3988
3989 return TRUE;
3990 }
3991
3992 gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
3993 {
3994 if (m_wx_model->IsVirtualListModel())
3995 {
3996 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3997
3998 if (iter == NULL)
3999 return (wx_model->GetCount() > 0);
4000
4001 // this is a list, nodes have no children
4002 return FALSE;
4003 }
4004 else
4005 {
4006 if (iter == NULL)
4007 return (m_root->GetChildCount() > 0);
4008
4009 wxDataViewItem item( (void*) iter->user_data );
4010
4011 bool is_container = m_wx_model->IsContainer( item );
4012
4013 if (!is_container)
4014 return FALSE;
4015
4016 wxGtkTreeModelNode *node = FindNode( iter );
4017 wxASSERT_MSG(node,
4018 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
4019
4020 BuildBranch( node );
4021
4022 return (node->GetChildCount() > 0);
4023 }
4024 }
4025
4026 gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
4027 {
4028 if (m_wx_model->IsVirtualListModel())
4029 {
4030 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
4031
4032 if (iter == NULL)
4033 return (gint) wx_model->GetCount();
4034
4035 return 0;
4036 }
4037 else
4038 {
4039 if (iter == NULL)
4040 return m_root->GetChildCount();
4041
4042 wxDataViewItem item( (void*) iter->user_data );
4043
4044 if (!m_wx_model->IsContainer( item ))
4045 return 0;
4046
4047 wxGtkTreeModelNode *parent_node = FindNode( iter );
4048 wxASSERT_MSG(parent_node,
4049 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4050
4051 BuildBranch( parent_node );
4052
4053 return parent_node->GetChildCount();
4054 }
4055 }
4056
4057 gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
4058 {
4059 if (m_wx_model->IsVirtualListModel())
4060 {
4061 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
4062
4063 if (parent)
4064 return FALSE;
4065
4066 if (n < 0)
4067 return FALSE;
4068
4069 if (n >= (gint) wx_model->GetCount())
4070 return FALSE;
4071
4072 iter->stamp = m_gtk_model->stamp;
4073 // user_data is just the index +1
4074 iter->user_data = (gpointer) (n+1);
4075
4076 return TRUE;
4077 }
4078 else
4079 {
4080 void* id = NULL;
4081 if (parent) id = (void*) parent->user_data;
4082 wxDataViewItem item( id );
4083
4084 if (!m_wx_model->IsContainer( item ))
4085 return FALSE;
4086
4087 wxGtkTreeModelNode *parent_node = FindNode( parent );
4088 wxASSERT_MSG(parent_node,
4089 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4090
4091 BuildBranch( parent_node );
4092
4093 iter->stamp = m_gtk_model->stamp;
4094 iter->user_data = parent_node->GetChildren().Item( n );
4095
4096 return TRUE;
4097 }
4098 }
4099
4100 gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
4101 {
4102 if (m_wx_model->IsVirtualListModel())
4103 {
4104 return FALSE;
4105 }
4106 else
4107 {
4108 wxGtkTreeModelNode *node = FindParentNode( child );
4109 if (!node)
4110 return FALSE;
4111
4112 iter->stamp = m_gtk_model->stamp;
4113 iter->user_data = (gpointer) node->GetItem().GetID();
4114
4115 return TRUE;
4116 }
4117 }
4118
4119 // item can be deleted already in the model
4120 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item )
4121 {
4122 if (m_wx_model->IsVirtualListModel())
4123 {
4124 return wxPtrToUInt(item.GetID()) - 1;
4125 }
4126 else
4127 {
4128 wxGtkTreeModelNode *parent_node = FindNode( parent );
4129 wxGtkTreeModelChildren &children = parent_node->GetChildren();
4130 size_t j;
4131 for (j = 0; j < children.GetCount(); j++)
4132 {
4133 if (children[j] == item.GetID())
4134 return j;
4135 }
4136 }
4137 return -1;
4138 }
4139
4140
4141 static wxGtkTreeModelNode*
4142 wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
4143 {
4144 if( model == NULL )
4145 return NULL;
4146
4147 ItemList list;
4148 list.DeleteContents( true );
4149 wxDataViewItem it( item );
4150
4151 while( it.IsOk() )
4152 {
4153 wxDataViewItem * pItem = new wxDataViewItem( it );
4154 list.Insert( pItem );
4155 it = model->GetParent( it );
4156 }
4157
4158 wxGtkTreeModelNode * node = treeNode;
4159 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
4160 {
4161 if( node && node->GetNodes().GetCount() != 0 )
4162 {
4163 int len = node->GetNodes().GetCount();
4164 wxGtkTreeModelNodes &nodes = node->GetNodes();
4165 int j = 0;
4166 for( ; j < len; j ++)
4167 {
4168 if( nodes[j]->GetItem() == *(n->GetData()))
4169 {
4170 node = nodes[j];
4171 break;
4172 }
4173 }
4174
4175 if( j == len )
4176 {
4177 return NULL;
4178 }
4179 }
4180 else
4181 return NULL;
4182 }
4183 return node;
4184
4185 }
4186
4187 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
4188 {
4189 if (!iter)
4190 return m_root;
4191
4192 wxDataViewItem item( (void*) iter->user_data );
4193 if (!item.IsOk())
4194 return m_root;
4195
4196 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
4197
4198 /*
4199 if (!result)
4200 {
4201 wxLogDebug( "Not found %p", iter->user_data );
4202 char *crash = NULL;
4203 *crash = 0;
4204 }
4205 // TODO: remove this code
4206 */
4207
4208 return result;
4209 }
4210
4211 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
4212 {
4213 if (!item.IsOk())
4214 return m_root;
4215
4216 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
4217
4218 /*
4219 if (!result)
4220 {
4221 wxLogDebug( "Not found %p", item.GetID() );
4222 char *crash = NULL;
4223 *crash = 0;
4224 }
4225 // TODO: remove this code
4226 */
4227
4228 return result;
4229 }
4230
4231 static wxGtkTreeModelNode*
4232 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
4233 {
4234 if( model == NULL )
4235 return NULL;
4236
4237 ItemList list;
4238 list.DeleteContents( true );
4239 if( !item.IsOk() )
4240 return NULL;
4241
4242 wxDataViewItem it( model->GetParent( item ) );
4243 while( it.IsOk() )
4244 {
4245 wxDataViewItem * pItem = new wxDataViewItem( it );
4246 list.Insert( pItem );
4247 it = model->GetParent( it );
4248 }
4249
4250 wxGtkTreeModelNode * node = treeNode;
4251 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
4252 {
4253 if( node && node->GetNodes().GetCount() != 0 )
4254 {
4255 int len = node->GetNodes().GetCount();
4256 wxGtkTreeModelNodes nodes = node->GetNodes();
4257 int j = 0;
4258 for( ; j < len; j ++)
4259 {
4260 if( nodes[j]->GetItem() == *(n->GetData()))
4261 {
4262 node = nodes[j];
4263 break;
4264 }
4265 }
4266
4267 if( j == len )
4268 {
4269 return NULL;
4270 }
4271 }
4272 else
4273 return NULL;
4274 }
4275 //Examine whether the node is item's parent node
4276 int len = node->GetChildCount();
4277 for( int i = 0; i < len ; i ++ )
4278 {
4279 if( node->GetChildren().Item( i ) == item.GetID() )
4280 return node;
4281 }
4282 return NULL;
4283 }
4284
4285 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
4286 {
4287 if (!iter)
4288 return NULL;
4289
4290 wxDataViewItem item( (void*) iter->user_data );
4291 if (!item.IsOk())
4292 return NULL;
4293
4294 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
4295 }
4296
4297 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
4298 {
4299 if (!item.IsOk())
4300 return NULL;
4301
4302 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
4303 }
4304
4305 //-----------------------------------------------------------------------------
4306 // wxDataViewCtrl signal callbacks
4307 //-----------------------------------------------------------------------------
4308
4309 static void
4310 wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv )
4311 {
4312 if (!gtk_widget_get_realized(dv->m_widget))
4313 return;
4314
4315 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() );
4316 event.SetItem( dv->GetSelection() );
4317 event.SetModel( dv->GetModel() );
4318 dv->HandleWindowEvent( event );
4319 }
4320
4321 static void
4322 wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path,
4323 GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv )
4324 {
4325 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
4326
4327 wxDataViewItem item(dv->GTKPathToItem(path));
4328 event.SetItem( item );
4329 event.SetModel( dv->GetModel() );
4330 dv->HandleWindowEvent( event );
4331 }
4332
4333 static gboolean
4334 wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4335 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4336 {
4337 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
4338
4339 wxDataViewItem item( (void*) iter->user_data );;
4340 event.SetItem( item );
4341 event.SetModel( dv->GetModel() );
4342 dv->HandleWindowEvent( event );
4343
4344 return !event.IsAllowed();
4345 }
4346
4347 static void
4348 wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4349 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4350 {
4351 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
4352
4353 wxDataViewItem item( (void*) iter->user_data );;
4354 event.SetItem( item );
4355 event.SetModel( dv->GetModel() );
4356 dv->HandleWindowEvent( event );
4357 }
4358
4359 static gboolean
4360 wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4361 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4362 {
4363 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
4364
4365 wxDataViewItem item( (void*) iter->user_data );;
4366 event.SetItem( item );
4367 event.SetModel( dv->GetModel() );
4368 dv->HandleWindowEvent( event );
4369
4370 return !event.IsAllowed();
4371 }
4372
4373 static void
4374 wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4375 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4376 {
4377 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
4378
4379 wxDataViewItem item( (void*) iter->user_data );;
4380 event.SetItem( item );
4381 event.SetModel( dv->GetModel() );
4382 dv->HandleWindowEvent( event );
4383 }
4384
4385 //-----------------------------------------------------------------------------
4386 // wxDataViewCtrl
4387 //-----------------------------------------------------------------------------
4388
4389 void wxDataViewCtrl::AddChildGTK(wxWindowGTK* child)
4390 {
4391 GtkWidget* treeview = GtkGetTreeView();
4392
4393 // Insert widget in GtkTreeView
4394 if (gtk_widget_get_realized(treeview))
4395 gtk_widget_set_parent_window( child->m_widget,
4396 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
4397 gtk_widget_set_parent( child->m_widget, treeview );
4398 }
4399
4400 static
4401 void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
4402 GtkAllocation *WXUNUSED(gtk_alloc),
4403 wxDataViewCtrl *win )
4404 {
4405 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
4406 while (node)
4407 {
4408 wxWindow *child = node->GetData();
4409
4410 GtkRequisition req;
4411 gtk_widget_size_request( child->m_widget, &req );
4412
4413 GtkAllocation alloc;
4414 alloc.x = child->m_x;
4415 alloc.y = child->m_y;
4416 alloc.width = child->m_width;
4417 alloc.height = child->m_height;
4418 gtk_widget_size_allocate( child->m_widget, &alloc );
4419
4420 node = node->GetNext();
4421 }
4422 }
4423
4424
4425 //-----------------------------------------------------------------------------
4426 // "motion_notify_event"
4427 //-----------------------------------------------------------------------------
4428
4429 static gboolean
4430 gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget),
4431 GdkEventMotion *gdk_event,
4432 wxDataViewCtrl *dv )
4433 {
4434 if (gdk_event->is_hint)
4435 {
4436 int x = 0;
4437 int y = 0;
4438 GdkModifierType state;
4439 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
4440 gdk_event->x = x;
4441 gdk_event->y = y;
4442 }
4443
4444 wxGtkTreePath path;
4445 GtkTreeViewColumn *column = NULL;
4446 gint cell_x = 0;
4447 gint cell_y = 0;
4448 if (gtk_tree_view_get_path_at_pos(
4449 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4450 (int) gdk_event->x, (int) gdk_event->y,
4451 path.ByRef(),
4452 &column,
4453 &cell_x,
4454 &cell_y))
4455 {
4456 if (path)
4457 {
4458 GtkTreeIter iter;
4459 dv->GtkGetInternal()->get_iter( &iter, path );
4460 }
4461 }
4462
4463
4464 return FALSE;
4465 }
4466
4467 //-----------------------------------------------------------------------------
4468 // "button_press_event"
4469 //-----------------------------------------------------------------------------
4470
4471 static gboolean
4472 gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget),
4473 GdkEventButton *gdk_event,
4474 wxDataViewCtrl *dv )
4475 {
4476 if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS))
4477 {
4478 wxGtkTreePath path;
4479 GtkTreeViewColumn *column = NULL;
4480 gint cell_x = 0;
4481 gint cell_y = 0;
4482 gtk_tree_view_get_path_at_pos
4483 (
4484 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4485 (int) gdk_event->x, (int) gdk_event->y,
4486 path.ByRef(),
4487 &column,
4488 &cell_x,
4489 &cell_y
4490 );
4491
4492 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() );
4493 if (path)
4494 event.SetItem(dv->GTKPathToItem(path));
4495 event.SetModel( dv->GetModel() );
4496 return dv->HandleWindowEvent( event );
4497 }
4498
4499 return FALSE;
4500 }
4501
4502 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4503
4504 wxDataViewCtrl::~wxDataViewCtrl()
4505 {
4506 // Stop editing before destroying the control to remove any event handlers
4507 // which are added when editing started: if we didn't do this, the base
4508 // class dtor would assert as it checks for any leftover handlers.
4509 if ( m_treeview )
4510 {
4511 GtkTreeViewColumn *col;
4512 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview), NULL, &col);
4513
4514 wxDataViewColumn * const wxcol = FromGTKColumn(col);
4515 if ( wxcol )
4516 {
4517 // This won't do anything if we're not editing it
4518 wxcol->GetRenderer()->CancelEditing();
4519 }
4520 }
4521
4522 m_cols.Clear();
4523
4524 delete m_internal;
4525 }
4526
4527 void wxDataViewCtrl::Init()
4528 {
4529 m_internal = NULL;
4530
4531 m_cols.DeleteContents( true );
4532
4533 m_uniformRowHeight = -1;
4534 }
4535
4536 bool wxDataViewCtrl::Create(wxWindow *parent,
4537 wxWindowID id,
4538 const wxPoint& pos,
4539 const wxSize& size,
4540 long style,
4541 const wxValidator& validator,
4542 const wxString& name)
4543 {
4544 if (!PreCreation( parent, pos, size ) ||
4545 !CreateBase( parent, id, pos, size, style, validator, name ))
4546 {
4547 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4548 return false;
4549 }
4550
4551 m_widget = gtk_scrolled_window_new (NULL, NULL);
4552 g_object_ref(m_widget);
4553
4554 GTKScrolledWindowSetBorder(m_widget, style);
4555
4556 m_treeview = gtk_tree_view_new();
4557 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
4558
4559 m_focusWidget = GTK_WIDGET(m_treeview);
4560
4561 g_signal_connect (m_treeview, "size_allocate",
4562 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
4563
4564 #ifdef __WXGTK26__
4565 if (!gtk_check_version(2,6,0))
4566 {
4567 bool fixed = (style & wxDV_VARIABLE_LINE_HEIGHT) == 0;
4568 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4569 }
4570 #endif
4571
4572 if (style & wxDV_MULTIPLE)
4573 {
4574 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4575 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
4576 }
4577
4578 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
4579
4580 #ifdef __WXGTK210__
4581 if (!gtk_check_version(2,10,0))
4582 {
4583 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
4584
4585 if ((style & wxDV_HORIZ_RULES) != 0 &&
4586 (style & wxDV_VERT_RULES) != 0)
4587 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
4588 else if (style & wxDV_VERT_RULES)
4589 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
4590 else if (style & wxDV_HORIZ_RULES)
4591 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
4592
4593 if (grid != GTK_TREE_VIEW_GRID_LINES_NONE)
4594 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
4595 }
4596 #endif
4597
4598 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 );
4599
4600 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
4601 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4602 gtk_widget_show (m_treeview);
4603
4604 m_parent->DoAddChild( this );
4605
4606 PostCreation(size);
4607
4608 GtkEnableSelectionEvents();
4609
4610 g_signal_connect_after (m_treeview, "row-activated",
4611 G_CALLBACK (wxdataview_row_activated_callback), this);
4612
4613 g_signal_connect (m_treeview, "test-collapse-row",
4614 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
4615
4616 g_signal_connect_after (m_treeview, "row-collapsed",
4617 G_CALLBACK (wxdataview_row_collapsed_callback), this);
4618
4619 g_signal_connect (m_treeview, "test-expand-row",
4620 G_CALLBACK (wxdataview_test_expand_row_callback), this);
4621
4622 g_signal_connect_after (m_treeview, "row-expanded",
4623 G_CALLBACK (wxdataview_row_expanded_callback), this);
4624
4625 g_signal_connect (m_treeview, "motion_notify_event",
4626 G_CALLBACK (gtk_dataview_motion_notify_callback), this);
4627
4628 g_signal_connect (m_treeview, "button_press_event",
4629 G_CALLBACK (gtk_dataview_button_press_callback), this);
4630
4631 return true;
4632 }
4633
4634 wxDataViewItem wxDataViewCtrl::GTKPathToItem(GtkTreePath *path) const
4635 {
4636 GtkTreeIter iter;
4637 return wxDataViewItem(path && m_internal->get_iter(&iter, path)
4638 ? iter.user_data
4639 : NULL);
4640 }
4641
4642 void wxDataViewCtrl::OnInternalIdle()
4643 {
4644 wxWindow::OnInternalIdle();
4645
4646 m_internal->OnInternalIdle();
4647
4648 unsigned int cols = GetColumnCount();
4649 unsigned int i;
4650 for (i = 0; i < cols; i++)
4651 {
4652 wxDataViewColumn *col = GetColumn( i );
4653 col->OnInternalIdle();
4654 }
4655
4656 if (m_ensureVisibleDefered.IsOk())
4657 {
4658 ExpandAncestors(m_ensureVisibleDefered);
4659 GtkTreeIter iter;
4660 iter.user_data = (gpointer) m_ensureVisibleDefered.GetID();
4661 wxGtkTreePath path(m_internal->get_path( &iter ));
4662 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4663 m_ensureVisibleDefered = wxDataViewItem(0);
4664 }
4665 }
4666
4667 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4668 {
4669 wxDELETE(m_internal);
4670
4671 if (!wxDataViewCtrlBase::AssociateModel( model ))
4672 return false;
4673
4674 #ifdef __WXGTK26__
4675 if (!gtk_check_version(2,6,0))
4676 {
4677 bool fixed = (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) == 0) || (model->IsVirtualListModel()));
4678 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4679 }
4680 #endif
4681
4682 m_internal = new wxDataViewCtrlInternal( this, model );
4683
4684 return true;
4685 }
4686
4687 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4688 {
4689 return m_internal->EnableDragSource( format );
4690 }
4691
4692 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4693 {
4694 return m_internal->EnableDropTarget( format );
4695 }
4696
4697 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4698 {
4699 if (!wxDataViewCtrlBase::AppendColumn(col))
4700 return false;
4701
4702 m_cols.Append( col );
4703
4704 #ifdef __WXGTK26__
4705 if (!gtk_check_version(2,6,0))
4706 {
4707 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4708 GTK_TREE_VIEW_COLUMN_FIXED)
4709 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4710 }
4711 #endif
4712
4713 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
4714 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4715
4716 return true;
4717 }
4718
4719 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4720 {
4721 if (!wxDataViewCtrlBase::PrependColumn(col))
4722 return false;
4723
4724 m_cols.Insert( col );
4725
4726 #ifdef __WXGTK26__
4727 if (!gtk_check_version(2,6,0))
4728 {
4729 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4730 GTK_TREE_VIEW_COLUMN_FIXED)
4731 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4732 }
4733 #endif
4734
4735 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4736 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 );
4737
4738 return true;
4739 }
4740
4741 bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4742 {
4743 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4744 return false;
4745
4746 m_cols.Insert( pos, col );
4747
4748 #ifdef __WXGTK26__
4749 if (!gtk_check_version(2,6,0))
4750 {
4751 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4752 GTK_TREE_VIEW_COLUMN_FIXED)
4753 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4754 }
4755 #endif
4756
4757 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4758 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), pos );
4759
4760 return true;
4761 }
4762
4763 unsigned int wxDataViewCtrl::GetColumnCount() const
4764 {
4765 return m_cols.GetCount();
4766 }
4767
4768 wxDataViewColumn* wxDataViewCtrl::FromGTKColumn(GtkTreeViewColumn *gtk_col) const
4769 {
4770 if ( !gtk_col )
4771 return NULL;
4772
4773 wxDataViewColumnList::const_iterator iter;
4774 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
4775 {
4776 wxDataViewColumn *col = *iter;
4777 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
4778 {
4779 return col;
4780 }
4781 }
4782
4783 wxFAIL_MSG( "No matching column?" );
4784
4785 return NULL;
4786 }
4787
4788 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
4789 {
4790 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
4791
4792 return FromGTKColumn(gtk_col);
4793 }
4794
4795 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4796 {
4797 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
4798 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
4799
4800 m_cols.DeleteObject( column );
4801
4802 return true;
4803 }
4804
4805 bool wxDataViewCtrl::ClearColumns()
4806 {
4807 wxDataViewColumnList::iterator iter;
4808 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
4809 {
4810 wxDataViewColumn *col = *iter;
4811 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
4812 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4813 }
4814
4815 m_cols.Clear();
4816
4817 return true;
4818 }
4819
4820 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4821 {
4822 GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
4823
4824 wxGtkList list(gtk_tree_view_get_columns(GTK_TREE_VIEW(m_treeview)));
4825
4826 return g_list_index( list, (gconstpointer) gtk_column );
4827 }
4828
4829 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4830 {
4831 return m_internal->GetDataViewSortColumn();
4832 }
4833
4834 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4835 {
4836 GtkTreeIter iter;
4837 iter.user_data = item.GetID();
4838 wxGtkTreePath path(m_internal->get_path( &iter ));
4839 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
4840 }
4841
4842 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4843 {
4844 GtkTreeIter iter;
4845 iter.user_data = item.GetID();
4846 wxGtkTreePath path(m_internal->get_path( &iter ));
4847 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
4848 }
4849
4850 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4851 {
4852 GtkTreeIter iter;
4853 iter.user_data = item.GetID();
4854 wxGtkTreePath path(m_internal->get_path( &iter ));
4855 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview), path );
4856 }
4857
4858 wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4859 {
4860 // The tree doesn't have any current item if it hadn't been created yet but
4861 // it's arguably not an error to call this function in this case so just
4862 // return an invalid item without asserting.
4863 if ( !m_treeview )
4864 return wxDataViewItem();
4865
4866 wxGtkTreePath path;
4867 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview), path.ByRef(), NULL);
4868
4869 return GTKPathToItem(path);
4870 }
4871
4872 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4873 {
4874 wxCHECK_RET( m_treeview,
4875 "Current item can't be set before creating the control." );
4876
4877 // We need to make sure the model knows about this item or the path would
4878 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4879 ExpandAncestors(item);
4880
4881 // We also need to preserve the existing selection from changing.
4882 // Unfortunately the only way to do it seems to use our own selection
4883 // function and forbid any selection changes during set cursor call.
4884 wxGtkTreeSelectionLock
4885 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview)));
4886
4887 // Do move the cursor now.
4888 GtkTreeIter iter;
4889 iter.user_data = item.GetID();
4890 wxGtkTreePath path(m_internal->get_path( &iter ));
4891
4892 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview), path, NULL, FALSE);
4893 }
4894
4895 void wxDataViewCtrl::StartEditor(const wxDataViewItem& item, unsigned int column)
4896 {
4897 wxCHECK_RET( m_treeview,
4898 "Current item can't be set before creating the control." );
4899
4900 // We need to make sure the model knows about this item or the path would
4901 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4902 ExpandAncestors(item);
4903
4904 wxDataViewColumn *dvcolumn = GetColumn(column);
4905 wxASSERT_MSG(dvcolumn, "Could not retrieve column");
4906 GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(dvcolumn->GetGtkHandle());
4907
4908 // We also need to preserve the existing selection from changing.
4909 // Unfortunately the only way to do it seems to use our own selection
4910 // function and forbid any selection changes during set cursor call.
4911 wxGtkTreeSelectionLock
4912 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview)));
4913
4914 // Do move the cursor now.
4915 GtkTreeIter iter;
4916 iter.user_data = item.GetID();
4917 wxGtkTreePath path(m_internal->get_path( &iter ));
4918
4919 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview), path, gcolumn, TRUE);
4920 }
4921
4922 int wxDataViewCtrl::GetSelectedItemsCount() const
4923 {
4924 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4925
4926 return gtk_tree_selection_count_selected_rows(selection);
4927 }
4928
4929 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4930 {
4931 sel.Clear();
4932
4933 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4934 if (HasFlag(wxDV_MULTIPLE))
4935 {
4936 GtkTreeModel *model;
4937 wxGtkTreePathList list(gtk_tree_selection_get_selected_rows(selection, &model));
4938
4939 for ( GList* current = list; current; current = g_list_next(current) )
4940 {
4941 GtkTreePath *path = (GtkTreePath*) current->data;
4942
4943 sel.Add(GTKPathToItem(path));
4944 }
4945 }
4946 else
4947 {
4948 GtkTreeIter iter;
4949 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
4950 {
4951 sel.Add( wxDataViewItem(iter.user_data) );
4952 }
4953 }
4954
4955 return sel.size();
4956 }
4957
4958 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4959 {
4960 GtkDisableSelectionEvents();
4961
4962 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4963
4964 gtk_tree_selection_unselect_all( selection );
4965
4966 wxDataViewItem last_parent;
4967
4968 size_t i;
4969 for (i = 0; i < sel.GetCount(); i++)
4970 {
4971 wxDataViewItem item = sel[i];
4972 wxDataViewItem parent = GetModel()->GetParent( item );
4973 if (parent)
4974 {
4975 if (parent != last_parent)
4976 ExpandAncestors(item);
4977 }
4978 last_parent = parent;
4979
4980 GtkTreeIter iter;
4981 iter.stamp = m_internal->GetGtkModel()->stamp;
4982 iter.user_data = (gpointer) item.GetID();
4983 gtk_tree_selection_select_iter( selection, &iter );
4984 }
4985
4986 GtkEnableSelectionEvents();
4987 }
4988
4989 void wxDataViewCtrl::Select( const wxDataViewItem & item )
4990 {
4991 ExpandAncestors(item);
4992
4993 GtkDisableSelectionEvents();
4994
4995 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4996
4997 GtkTreeIter iter;
4998 iter.stamp = m_internal->GetGtkModel()->stamp;
4999 iter.user_data = (gpointer) item.GetID();
5000 gtk_tree_selection_select_iter( selection, &iter );
5001
5002 GtkEnableSelectionEvents();
5003 }
5004
5005 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
5006 {
5007 GtkDisableSelectionEvents();
5008
5009 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5010
5011 GtkTreeIter iter;
5012 iter.stamp = m_internal->GetGtkModel()->stamp;
5013 iter.user_data = (gpointer) item.GetID();
5014 gtk_tree_selection_unselect_iter( selection, &iter );
5015
5016 GtkEnableSelectionEvents();
5017 }
5018
5019 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
5020 {
5021 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5022
5023 GtkTreeIter iter;
5024 iter.stamp = m_internal->GetGtkModel()->stamp;
5025 iter.user_data = (gpointer) item.GetID();
5026
5027 return gtk_tree_selection_iter_is_selected( selection, &iter );
5028 }
5029
5030 void wxDataViewCtrl::SelectAll()
5031 {
5032 GtkDisableSelectionEvents();
5033
5034 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5035
5036 gtk_tree_selection_select_all( selection );
5037
5038 GtkEnableSelectionEvents();
5039 }
5040
5041 void wxDataViewCtrl::UnselectAll()
5042 {
5043 GtkDisableSelectionEvents();
5044
5045 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5046
5047 gtk_tree_selection_unselect_all( selection );
5048
5049 GtkEnableSelectionEvents();
5050 }
5051
5052 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,
5053 const wxDataViewColumn *WXUNUSED(column))
5054 {
5055 m_ensureVisibleDefered = item;
5056 ExpandAncestors(item);
5057
5058 GtkTreeIter iter;
5059 iter.user_data = (gpointer) item.GetID();
5060 wxGtkTreePath path(m_internal->get_path( &iter ));
5061 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
5062 }
5063
5064 void wxDataViewCtrl::HitTest(const wxPoint& point,
5065 wxDataViewItem& item,
5066 wxDataViewColumn *& column) const
5067 {
5068 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
5069 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
5070 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
5071 // So we have to use both of them.
5072 // Friedrich Haase 2010-9-20
5073 wxGtkTreePath path, pathScratch;
5074 GtkTreeViewColumn* GtkColumn = NULL;
5075 GtkTreeViewDropPosition pos = GTK_TREE_VIEW_DROP_INTO_OR_AFTER;
5076 gint cell_x = 0;
5077 gint cell_y = 0;
5078
5079 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5080 wxDataViewCtrl* ctrl = (wxDataViewCtrl*)this; // ugly workaround, ctrl is NOT const
5081 GtkTreeView* treeView = GTK_TREE_VIEW(ctrl->GtkGetTreeView());
5082
5083 // is there possibly a better suited function to get the column?
5084 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5085 treeView,
5086 (int) point.x, (int) point.y,
5087 pathScratch.ByRef(),
5088 &GtkColumn, // here we get the GtkColumn
5089 &cell_x,
5090 &cell_y );
5091
5092 if ( GtkColumn != NULL )
5093 {
5094 // we got GTK column
5095 // the right call now which takes the header into account
5096 gtk_tree_view_get_dest_row_at_pos( treeView, (int) point.x, (int) point.y, path.ByRef(), &pos);
5097
5098 if (path)
5099 item = wxDataViewItem(GTKPathToItem(path));
5100 // else we got a GTK column but the position is not over an item, e.g. below last item
5101 for ( unsigned int i=0, cols=GetColumnCount(); i<cols; ++i ) // search the wx column
5102 {
5103 wxDataViewColumn* col = GetColumn(i);
5104 if ( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == GtkColumn )
5105 {
5106 column = col; // here we get the wx column
5107 break;
5108 }
5109 }
5110 }
5111 // else no column and thus no item, both null
5112 }
5113
5114 wxRect
5115 wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item),
5116 const wxDataViewColumn *WXUNUSED(column)) const
5117 {
5118 return wxRect();
5119 }
5120
5121 bool wxDataViewCtrl::SetRowHeight(int rowHeight)
5122 {
5123 m_uniformRowHeight = rowHeight;
5124 return true;
5125 }
5126
5127 void wxDataViewCtrl::DoSetExpanderColumn()
5128 {
5129 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
5130 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5131 }
5132
5133 void wxDataViewCtrl::DoSetIndent()
5134 {
5135 }
5136
5137 void wxDataViewCtrl::GtkDisableSelectionEvents()
5138 {
5139 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5140 g_signal_handlers_disconnect_by_func( selection,
5141 (gpointer) (wxdataview_selection_changed_callback), this);
5142 }
5143
5144 void wxDataViewCtrl::GtkEnableSelectionEvents()
5145 {
5146 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5147 g_signal_connect_after (selection, "changed",
5148 G_CALLBACK (wxdataview_selection_changed_callback), this);
5149 }
5150
5151 // ----------------------------------------------------------------------------
5152 // visual attributes stuff
5153 // ----------------------------------------------------------------------------
5154
5155 // static
5156 wxVisualAttributes
5157 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
5158 {
5159 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
5160 }
5161
5162 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
5163 {
5164 wxDataViewCtrlBase::DoApplyWidgetStyle(style);
5165 gtk_widget_modify_style(m_treeview, style);
5166 }
5167
5168 #endif // !wxUSE_GENERICDATAVIEWCTRL
5169
5170 #endif // wxUSE_DATAVIEWCTRL