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