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