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