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