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