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