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