]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
Add wxRenderer::GetHeaderButtonMargin().
[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 col );
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 GtkDestroyNotify destroy);
497 static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
498 GtkTreeIterCompareFunc func,
499 gpointer data,
500 GtkDestroyNotify 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 GtkDestroyNotify 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 GtkDestroyNotify 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 gint calc_width = (gint) renderer->xpad * 2 + size.x;
1337 gint calc_height = (gint) renderer->ypad * 2 + size.y;
1338
1339 if (x_offset)
1340 *x_offset = 0;
1341 if (y_offset)
1342 *y_offset = 0;
1343
1344 if (cell_area && size.x > 0 && size.y > 0)
1345 {
1346 if (x_offset)
1347 {
1348 *x_offset = (gint)((renderer->xalign *
1349 (cell_area->width - calc_width - 2 * renderer->xpad)));
1350 *x_offset = MAX (*x_offset, 0) + renderer->xpad;
1351 }
1352 if (y_offset)
1353 {
1354 *y_offset = (gint)((renderer->yalign *
1355 (cell_area->height - calc_height - 2 * renderer->ypad)));
1356 *y_offset = MAX (*y_offset, 0) + renderer->ypad;
1357 }
1358 }
1359
1360 if (width)
1361 *width = calc_width;
1362
1363 if (height)
1364 *height = calc_height;
1365 }
1366
1367 static void
1368 gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
1369 GdkWindow *window,
1370 GtkWidget *widget,
1371 GdkRectangle *background_area,
1372 GdkRectangle *cell_area,
1373 GdkRectangle *expose_area,
1374 GtkCellRendererState flags)
1375
1376 {
1377 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1378 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1379
1380 cell->GTKStashRenderParams(window, widget,
1381 background_area, expose_area, flags);
1382
1383 wxRect rect(wxRectFromGDKRect(cell_area));
1384 rect = rect.Deflate(renderer->xpad, renderer->ypad);
1385
1386 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
1387 wxWindowDCImpl *impl = (wxWindowDCImpl *) dc->GetImpl();
1388
1389 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1390 // window such as a DnD drop window.
1391 if (window != impl->m_gdkwindow)
1392 {
1393 impl->Destroy();
1394 impl->m_gdkwindow = window;
1395 impl->SetUpDC();
1396 }
1397
1398 int state = 0;
1399 if (flags & GTK_CELL_RENDERER_SELECTED)
1400 state |= wxDATAVIEW_CELL_SELECTED;
1401 if (flags & GTK_CELL_RENDERER_PRELIT)
1402 state |= wxDATAVIEW_CELL_PRELIT;
1403 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1404 state |= wxDATAVIEW_CELL_INSENSITIVE;
1405 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1406 state |= wxDATAVIEW_CELL_INSENSITIVE;
1407 if (flags & GTK_CELL_RENDERER_FOCUSED)
1408 state |= wxDATAVIEW_CELL_FOCUSED;
1409 cell->WXCallRender( rect, dc, state );
1410 }
1411
1412 static gboolean
1413 gtk_wx_cell_renderer_activate(
1414 GtkCellRenderer *renderer,
1415 GdkEvent *event,
1416 GtkWidget *widget,
1417 const gchar *path,
1418 GdkRectangle *WXUNUSED(background_area),
1419 GdkRectangle *cell_area,
1420 GtkCellRendererState WXUNUSED(flags) )
1421 {
1422 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1423 wxDataViewCustomRenderer *cell = wxrenderer->cell;
1424
1425 GdkRectangle rect;
1426 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
1427 &rect.x,
1428 &rect.y,
1429 &rect.width,
1430 &rect.height);
1431
1432 rect.x += cell_area->x;
1433 rect.y += cell_area->y;
1434 rect.width -= renderer->xpad * 2;
1435 rect.height -= renderer->ypad * 2;
1436
1437 wxRect renderrect(wxRectFromGDKRect(&rect));
1438
1439 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
1440 wxDataViewModel *model = ctrl->GetModel();
1441
1442 wxDataViewItem item(ctrl->GTKPathToItem(wxGtkTreePath(path)));
1443
1444 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1445
1446 if (!event)
1447 {
1448 bool ret = false;
1449
1450 // activated by <ENTER>
1451 if (cell->Activate( renderrect, model, item, model_col ))
1452 ret = true;
1453
1454 return ret;
1455 }
1456 else if (event->type == GDK_BUTTON_PRESS)
1457 {
1458 GdkEventButton *button_event = (GdkEventButton*) event;
1459 wxPoint pt( ((int) button_event->x) - renderrect.x,
1460 ((int) button_event->y) - renderrect.y );
1461
1462 bool ret = false;
1463 if (button_event->button == 1)
1464 {
1465 if (cell->LeftClick( pt, renderrect, model, item, model_col ))
1466 ret = true;
1467 // TODO: query system double-click time
1468 if (button_event->time - wxrenderer->last_click < 400)
1469 if (cell->Activate( renderrect, model, item, model_col ))
1470 ret = true;
1471 }
1472 wxrenderer->last_click = button_event->time;
1473
1474 return ret;
1475 }
1476
1477 return false;
1478 }
1479
1480 // ---------------------------------------------------------
1481 // wxGtkDataViewModelNotifier
1482 // ---------------------------------------------------------
1483
1484 class wxGtkDataViewModelNotifier: public wxDataViewModelNotifier
1485 {
1486 public:
1487 wxGtkDataViewModelNotifier( wxDataViewModel *wx_model, wxDataViewCtrlInternal *internal );
1488 ~wxGtkDataViewModelNotifier();
1489
1490 virtual bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
1491 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
1492 virtual bool ItemChanged( const wxDataViewItem &item );
1493 virtual bool ValueChanged( const wxDataViewItem &item, unsigned int col );
1494 virtual bool Cleared();
1495 virtual void Resort();
1496 virtual bool BeforeReset();
1497 virtual bool AfterReset();
1498
1499 void UpdateLastCount();
1500
1501 private:
1502 wxDataViewModel *m_wx_model;
1503 wxDataViewCtrlInternal *m_internal;
1504 };
1505
1506 // ---------------------------------------------------------
1507 // wxGtkDataViewListModelNotifier
1508 // ---------------------------------------------------------
1509
1510 wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1511 wxDataViewModel *wx_model, wxDataViewCtrlInternal *internal )
1512 {
1513 m_wx_model = wx_model;
1514 m_internal = internal;
1515 }
1516
1517 wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
1518 {
1519 m_wx_model = NULL;
1520 m_internal = NULL;
1521 }
1522
1523 bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
1524 {
1525 m_internal->ItemAdded( parent, item );
1526 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1527
1528 GtkTreeIter iter;
1529 iter.stamp = wxgtk_model->stamp;
1530 iter.user_data = item.GetID();
1531
1532 wxGtkTreePath path(wxgtk_tree_model_get_path(
1533 GTK_TREE_MODEL(wxgtk_model), &iter ));
1534 gtk_tree_model_row_inserted(
1535 GTK_TREE_MODEL(wxgtk_model), path, &iter);
1536
1537 return true;
1538 }
1539
1540 bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
1541 {
1542 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1543 #if 0
1544 // using _get_path for a deleted item cannot be
1545 // a good idea
1546 GtkTreeIter iter;
1547 iter.stamp = wxgtk_model->stamp;
1548 iter.user_data = (gpointer) item.GetID();
1549 wxGtkTreePath path(wxgtk_tree_model_get_path(
1550 GTK_TREE_MODEL(wxgtk_model), &iter ));
1551 #else
1552 // so get the path from the parent
1553 GtkTreeIter iter;
1554 iter.stamp = wxgtk_model->stamp;
1555 iter.user_data = (gpointer) parent.GetID();
1556 wxGtkTreePath path(wxgtk_tree_model_get_path(
1557 GTK_TREE_MODEL(wxgtk_model), &iter ));
1558 // and add the final index ourselves
1559 int index = m_internal->GetIndexOf( parent, item );
1560 gtk_tree_path_append_index( path, index );
1561 #endif
1562
1563 gtk_tree_model_row_deleted(
1564 GTK_TREE_MODEL(wxgtk_model), path );
1565
1566 m_internal->ItemDeleted( parent, item );
1567
1568 return true;
1569 }
1570
1571 void wxGtkDataViewModelNotifier::Resort()
1572 {
1573 m_internal->Resort();
1574 }
1575
1576 bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem &item )
1577 {
1578 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1579
1580 GtkTreeIter iter;
1581 iter.stamp = wxgtk_model->stamp;
1582 iter.user_data = (gpointer) item.GetID();
1583
1584 wxGtkTreePath path(wxgtk_tree_model_get_path(
1585 GTK_TREE_MODEL(wxgtk_model), &iter ));
1586 gtk_tree_model_row_changed(
1587 GTK_TREE_MODEL(wxgtk_model), path, &iter );
1588
1589 m_internal->ItemChanged( item );
1590
1591 return true;
1592 }
1593
1594 bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem &item, unsigned int model_col )
1595 {
1596 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1597 wxDataViewCtrl *ctrl = m_internal->GetOwner();
1598
1599 // This adds GTK+'s missing MVC logic for ValueChanged
1600 unsigned int index;
1601 for (index = 0; index < ctrl->GetColumnCount(); index++)
1602 {
1603 wxDataViewColumn *column = ctrl->GetColumn( index );
1604 if (column->GetModelColumn() == model_col)
1605 {
1606 GtkTreeView *widget = GTK_TREE_VIEW(ctrl->GtkGetTreeView());
1607 GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
1608
1609 // Get cell area
1610 GtkTreeIter iter;
1611 iter.stamp = wxgtk_model->stamp;
1612 iter.user_data = (gpointer) item.GetID();
1613 wxGtkTreePath path(wxgtk_tree_model_get_path(
1614 GTK_TREE_MODEL(wxgtk_model), &iter ));
1615 GdkRectangle cell_area;
1616 gtk_tree_view_get_cell_area( widget, path, gcolumn, &cell_area );
1617
1618 GtkAdjustment* hadjust = gtk_tree_view_get_hadjustment( widget );
1619 double d = gtk_adjustment_get_value( hadjust );
1620 int xdiff = (int) d;
1621
1622 int ydiff = gcolumn->button->allocation.height;
1623 // Redraw
1624 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
1625 cell_area.x - xdiff, ydiff + cell_area.y, cell_area.width, cell_area.height );
1626
1627 m_internal->ValueChanged( item, model_col );
1628
1629 return true;
1630 }
1631 }
1632
1633 return false;
1634 }
1635
1636 bool wxGtkDataViewModelNotifier::BeforeReset()
1637 {
1638 GtkWidget *treeview = m_internal->GetOwner()->GtkGetTreeView();
1639 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview), NULL );
1640
1641 return true;
1642 }
1643
1644 bool wxGtkDataViewModelNotifier::AfterReset()
1645 {
1646 GtkWidget *treeview = m_internal->GetOwner()->GtkGetTreeView();
1647 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1648
1649 m_internal->Cleared();
1650
1651 gtk_tree_view_set_model( GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(wxgtk_model) );
1652
1653 return true;
1654 }
1655
1656 bool wxGtkDataViewModelNotifier::Cleared()
1657 {
1658 GtkWxTreeModel *wxgtk_model = m_internal->GetGtkModel();
1659
1660 // There is no call to tell the model that everything
1661 // has been deleted so call row_deleted() for every
1662 // child of root...
1663
1664 int count = m_internal->iter_n_children( NULL ); // number of children of root
1665
1666 GtkTreePath *path = gtk_tree_path_new_first(); // points to root
1667
1668 int i;
1669 for (i = 0; i < count; i++)
1670 gtk_tree_model_row_deleted( GTK_TREE_MODEL(wxgtk_model), path );
1671
1672 gtk_tree_path_free( path );
1673
1674 m_internal->Cleared();
1675
1676 return true;
1677 }
1678
1679 // ---------------------------------------------------------
1680 // wxDataViewRenderer
1681 // ---------------------------------------------------------
1682
1683 static gpointer s_user_data = NULL;
1684
1685 static void
1686 wxgtk_cell_editable_editing_done( GtkCellEditable *WXUNUSED(editable),
1687 wxDataViewRenderer *wxrenderer )
1688 {
1689 wxDataViewColumn *column = wxrenderer->GetOwner();
1690 wxDataViewCtrl *dv = column->GetOwner();
1691 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv->GetId() );
1692 event.SetDataViewColumn( column );
1693 event.SetModel( dv->GetModel() );
1694 wxDataViewItem item( s_user_data );
1695 event.SetItem( item );
1696 dv->HandleWindowEvent( event );
1697 }
1698
1699 static void
1700 wxgtk_renderer_editing_started( GtkCellRenderer *WXUNUSED(cell), GtkCellEditable *editable,
1701 gchar *path, wxDataViewRenderer *wxrenderer )
1702 {
1703 if (!editable)
1704 return;
1705
1706 wxDataViewColumn *column = wxrenderer->GetOwner();
1707 wxDataViewCtrl *dv = column->GetOwner();
1708 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv->GetId() );
1709 event.SetDataViewColumn( column );
1710 event.SetModel( dv->GetModel() );
1711 wxDataViewItem item(dv->GTKPathToItem(wxGtkTreePath(path)));
1712 event.SetItem( item );
1713 dv->HandleWindowEvent( event );
1714
1715 if (GTK_IS_CELL_EDITABLE(editable))
1716 {
1717 s_user_data = item.GetID();
1718
1719 g_signal_connect (GTK_CELL_EDITABLE (editable), "editing_done",
1720 G_CALLBACK (wxgtk_cell_editable_editing_done),
1721 (gpointer) wxrenderer );
1722
1723 }
1724 }
1725
1726
1727 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
1728
1729 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1730 int align ) :
1731 wxDataViewRendererBase( varianttype, mode, align )
1732 {
1733 m_renderer = NULL;
1734
1735 // we haven't changed them yet
1736 m_usingDefaultAttrs = true;
1737
1738 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1739 // after the m_renderer pointer has been initialized
1740 }
1741
1742 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
1743 {
1744 gtk_tree_view_column_pack_end( column, m_renderer, TRUE /* expand */);
1745 }
1746
1747 void wxDataViewRenderer::GtkInitHandlers()
1748 {
1749 if (!gtk_check_version(2,6,0))
1750 {
1751 g_signal_connect (GTK_CELL_RENDERER(m_renderer), "editing_started",
1752 G_CALLBACK (wxgtk_renderer_editing_started),
1753 this);
1754 }
1755 }
1756
1757 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
1758 {
1759 GtkCellRendererMode gtkMode;
1760 switch (mode)
1761 {
1762 case wxDATAVIEW_CELL_INERT:
1763 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
1764 break;
1765
1766 case wxDATAVIEW_CELL_ACTIVATABLE:
1767 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
1768 break;
1769
1770 case wxDATAVIEW_CELL_EDITABLE:
1771 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
1772 break;
1773
1774 default:
1775 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1776 return;
1777 }
1778
1779 // This value is most often ignored in GtkTreeView
1780 GValue gvalue = { 0, };
1781 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1782 g_value_set_enum( &gvalue, gtkMode );
1783 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1784 g_value_unset( &gvalue );
1785 }
1786
1787 wxDataViewCellMode wxDataViewRenderer::GetMode() const
1788 {
1789 wxDataViewCellMode ret;
1790
1791 GValue gvalue;
1792 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1793
1794 switch (g_value_get_enum(&gvalue))
1795 {
1796 default:
1797 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1798 // fall through (we have to return something)
1799
1800 case GTK_CELL_RENDERER_MODE_INERT:
1801 ret = wxDATAVIEW_CELL_INERT;
1802 break;
1803
1804 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1805 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1806 break;
1807
1808 case GTK_CELL_RENDERER_MODE_EDITABLE:
1809 ret = wxDATAVIEW_CELL_EDITABLE;
1810 break;
1811 }
1812
1813 g_value_unset( &gvalue );
1814
1815 return ret;
1816 }
1817
1818 void wxDataViewRenderer::GtkUpdateAlignment()
1819 {
1820 int align = m_alignment;
1821
1822 // query alignment from column ?
1823 if (align == -1)
1824 {
1825 // None there yet
1826 if (GetOwner() == NULL)
1827 return;
1828
1829 align = GetOwner()->GetAlignment();
1830 align |= wxALIGN_CENTRE_VERTICAL;
1831 }
1832
1833 // horizontal alignment:
1834
1835 gfloat xalign = 0.0;
1836 if (align & wxALIGN_RIGHT)
1837 xalign = 1.0;
1838 else if (align & wxALIGN_CENTER_HORIZONTAL)
1839 xalign = 0.5;
1840
1841 GValue gvalue = { 0, };
1842 g_value_init( &gvalue, G_TYPE_FLOAT );
1843 g_value_set_float( &gvalue, xalign );
1844 g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue );
1845 g_value_unset( &gvalue );
1846
1847 // vertical alignment:
1848
1849 gfloat yalign = 0.0;
1850 if (align & wxALIGN_BOTTOM)
1851 yalign = 1.0;
1852 else if (align & wxALIGN_CENTER_VERTICAL)
1853 yalign = 0.5;
1854
1855 GValue gvalue2 = { 0, };
1856 g_value_init( &gvalue2, G_TYPE_FLOAT );
1857 g_value_set_float( &gvalue2, yalign );
1858 g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 );
1859 g_value_unset( &gvalue2 );
1860 }
1861
1862 void wxDataViewRenderer::SetAlignment( int align )
1863 {
1864 m_alignment = align;
1865 GtkUpdateAlignment();
1866 }
1867
1868 int wxDataViewRenderer::GetAlignment() const
1869 {
1870 return m_alignment;
1871 }
1872
1873 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode)
1874 {
1875 #ifdef __WXGTK26__
1876 if ( gtk_check_version(2, 6, 0) != NULL )
1877 return;
1878
1879 GtkCellRendererText * const rend = GtkGetTextRenderer();
1880 if ( !rend )
1881 return;
1882
1883 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1884 // can just cast between them
1885 GValue gvalue = { 0, };
1886 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1887 g_value_set_enum( &gvalue, static_cast<PangoEllipsizeMode>(mode) );
1888 g_object_set_property( G_OBJECT(rend), "ellipsize", &gvalue );
1889 g_value_unset( &gvalue );
1890 #else // GTK < 2.6
1891 wxUnusedVar(mode);
1892 #endif // GTK 2.6/before
1893 }
1894
1895 wxEllipsizeMode wxDataViewRenderer::GetEllipsizeMode() const
1896 {
1897 #ifdef __WXGTK26__
1898 if ( gtk_check_version(2, 6, 0) != NULL )
1899 return wxELLIPSIZE_NONE;
1900
1901 GtkCellRendererText * const rend = GtkGetTextRenderer();
1902 if ( !rend )
1903 return wxELLIPSIZE_NONE;
1904
1905 GValue gvalue = { 0, };
1906 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1907 g_object_get_property( G_OBJECT(rend), "ellipsize", &gvalue );
1908 wxEllipsizeMode
1909 mode = static_cast<wxEllipsizeMode>(g_value_get_enum( &gvalue ));
1910 g_value_unset( &gvalue );
1911
1912 return mode;
1913 #else // GTK < 2.6
1914 return wxELLIPSIZE_NONE;
1915 #endif // GTK 2.6/before
1916 }
1917
1918 void
1919 wxDataViewRenderer::GtkOnTextEdited(const gchar *itempath, const wxString& str)
1920 {
1921 wxVariant value(str);
1922 if (!Validate( value ))
1923 return;
1924
1925 wxDataViewItem
1926 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
1927
1928 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
1929 }
1930
1931 void
1932 wxDataViewRenderer::GtkOnCellChanged(const wxVariant& value,
1933 const wxDataViewItem& item,
1934 unsigned col)
1935 {
1936 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
1937 model->ChangeValue( value, item, col );
1938 }
1939
1940 // ---------------------------------------------------------
1941 // wxDataViewTextRenderer
1942 // ---------------------------------------------------------
1943
1944 extern "C"
1945 {
1946
1947 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *WXUNUSED(renderer),
1948 gchar *arg1, gchar *arg2, gpointer user_data )
1949 {
1950 wxDataViewRenderer *cell = (wxDataViewRenderer*) user_data;
1951
1952 cell->GtkOnTextEdited(arg1, wxGTK_CONV_BACK_FONT(
1953 arg2, cell->GetOwner()->GetOwner()->GetFont()));
1954 }
1955
1956 }
1957
1958 namespace
1959 {
1960
1961 // helper function used by wxDataViewTextRenderer and
1962 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
1963 // given text renderer and returns true if anything was done
1964 bool GtkApplyAttr(GtkCellRendererText *renderer, const wxDataViewItemAttr& attr)
1965 {
1966 bool usingDefaultAttrs = true;
1967 if (attr.HasColour())
1968 {
1969 const GdkColor * const gcol = attr.GetColour().GetColor();
1970
1971 GValue gvalue = { 0, };
1972 g_value_init( &gvalue, GDK_TYPE_COLOR );
1973 g_value_set_boxed( &gvalue, gcol );
1974 g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
1975 g_value_unset( &gvalue );
1976
1977 usingDefaultAttrs = false;
1978 }
1979 else
1980 {
1981 GValue gvalue = { 0, };
1982 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1983 g_value_set_boolean( &gvalue, FALSE );
1984 g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
1985 g_value_unset( &gvalue );
1986 }
1987
1988 if (attr.GetItalic())
1989 {
1990 GValue gvalue = { 0, };
1991 g_value_init( &gvalue, PANGO_TYPE_STYLE );
1992 g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
1993 g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
1994 g_value_unset( &gvalue );
1995
1996 usingDefaultAttrs = false;
1997 }
1998 else
1999 {
2000 GValue gvalue = { 0, };
2001 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2002 g_value_set_boolean( &gvalue, FALSE );
2003 g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
2004 g_value_unset( &gvalue );
2005 }
2006
2007
2008 if (attr.GetBold())
2009 {
2010 GValue gvalue = { 0, };
2011 g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
2012 g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
2013 g_object_set_property( G_OBJECT(renderer), "weight", &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), "weight-set", &gvalue );
2024 g_value_unset( &gvalue );
2025 }
2026
2027 #if 0
2028 if (attr.HasBackgroundColour())
2029 {
2030 wxColour colour = attr.GetBackgroundColour();
2031 const GdkColor * const gcol = colour.GetColor();
2032
2033 GValue gvalue = { 0, };
2034 g_value_init( &gvalue, GDK_TYPE_COLOR );
2035 g_value_set_boxed( &gvalue, gcol );
2036 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
2037 g_value_unset( &gvalue );
2038 }
2039 else
2040 {
2041 GValue gvalue = { 0, };
2042 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2043 g_value_set_boolean( &gvalue, FALSE );
2044 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
2045 g_value_unset( &gvalue );
2046 }
2047 #endif
2048
2049 return !usingDefaultAttrs;
2050 }
2051
2052 } // anonymous namespace
2053
2054 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
2055
2056 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2057 int align ) :
2058 wxDataViewRenderer( varianttype, mode, align )
2059 {
2060 GtkWxCellRendererText *text_renderer = gtk_wx_cell_renderer_text_new();
2061 text_renderer->wx_renderer = this;
2062 m_renderer = (GtkCellRenderer*) text_renderer;
2063
2064 if (mode & wxDATAVIEW_CELL_EDITABLE)
2065 {
2066 GValue gvalue = { 0, };
2067 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2068 g_value_set_boolean( &gvalue, true );
2069 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
2070 g_value_unset( &gvalue );
2071
2072 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2073
2074 GtkInitHandlers();
2075 }
2076
2077 SetMode(mode);
2078 SetAlignment(align);
2079 }
2080
2081 bool wxDataViewTextRenderer::SetTextValue(const wxString& str)
2082 {
2083 GValue gvalue = { 0, };
2084 g_value_init( &gvalue, G_TYPE_STRING );
2085 g_value_set_string( &gvalue, wxGTK_CONV_FONT( str, GetOwner()->GetOwner()->GetFont() ) );
2086 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2087 g_value_unset( &gvalue );
2088
2089 return true;
2090 }
2091
2092 bool wxDataViewTextRenderer::GetTextValue(wxString& str) const
2093 {
2094 GValue gvalue = { 0, };
2095 g_value_init( &gvalue, G_TYPE_STRING );
2096 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
2097 str = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), const_cast<wxDataViewTextRenderer*>(this)->GetOwner()->GetOwner()->GetFont() );
2098 g_value_unset( &gvalue );
2099
2100 return true;
2101 }
2102
2103 void wxDataViewTextRenderer::SetAlignment( int align )
2104 {
2105 wxDataViewRenderer::SetAlignment(align);
2106
2107 if (gtk_check_version(2,10,0))
2108 return;
2109
2110 // horizontal alignment:
2111 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2112 if (align & wxALIGN_RIGHT)
2113 pangoAlign = PANGO_ALIGN_RIGHT;
2114 else if (align & wxALIGN_CENTER_HORIZONTAL)
2115 pangoAlign = PANGO_ALIGN_CENTER;
2116
2117 GValue gvalue = { 0, };
2118 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2119 g_value_set_enum( &gvalue, pangoAlign );
2120 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2121 g_value_unset( &gvalue );
2122 }
2123
2124 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr& attr)
2125 {
2126 return GtkApplyAttr(GtkGetTextRenderer(), attr);
2127 }
2128
2129 GtkCellRendererText *wxDataViewTextRenderer::GtkGetTextRenderer() const
2130 {
2131 return GTK_CELL_RENDERER_TEXT(m_renderer);
2132 }
2133
2134 // ---------------------------------------------------------
2135 // wxDataViewBitmapRenderer
2136 // ---------------------------------------------------------
2137
2138 namespace
2139 {
2140
2141 // set "pixbuf" property on the given renderer
2142 void SetPixbufProp(GtkCellRenderer *renderer, GdkPixbuf *pixbuf)
2143 {
2144 GValue gvalue = { 0, };
2145 g_value_init( &gvalue, G_TYPE_OBJECT );
2146 g_value_set_object( &gvalue, pixbuf );
2147 g_object_set_property( G_OBJECT(renderer), "pixbuf", &gvalue );
2148 g_value_unset( &gvalue );
2149 }
2150
2151 } // anonymous namespace
2152
2153 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
2154
2155 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2156 int align ) :
2157 wxDataViewRenderer( varianttype, mode, align )
2158 {
2159 m_renderer = gtk_cell_renderer_pixbuf_new();
2160
2161 SetMode(mode);
2162 SetAlignment(align);
2163 }
2164
2165 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
2166 {
2167 if (value.GetType() == wxT("wxBitmap"))
2168 {
2169 wxBitmap bitmap;
2170 bitmap << value;
2171
2172 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2173 // object (and it will stay there and remain owned by wxBitmap)
2174 SetPixbufProp(m_renderer, bitmap.GetPixbuf());
2175 }
2176 else if (value.GetType() == wxT("wxIcon"))
2177 {
2178 wxIcon icon;
2179 icon << value;
2180
2181 SetPixbufProp(m_renderer, icon.GetPixbuf());
2182 }
2183 else
2184 {
2185 return false;
2186 }
2187
2188 return true;
2189 }
2190
2191 bool wxDataViewBitmapRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2192 {
2193 return false;
2194 }
2195
2196 // ---------------------------------------------------------
2197 // wxDataViewToggleRenderer
2198 // ---------------------------------------------------------
2199
2200 extern "C" {
2201 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
2202 gchar *path, gpointer user_data );
2203 }
2204
2205 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
2206 gchar *path, gpointer user_data )
2207 {
2208 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
2209
2210 // get old value
2211 GValue gvalue = { 0, };
2212 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2213 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
2214 bool tmp = g_value_get_boolean( &gvalue );
2215 g_value_unset( &gvalue );
2216 // invert it
2217 tmp = !tmp;
2218
2219 wxVariant value = tmp;
2220 if (!cell->Validate( value ))
2221 return;
2222
2223 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
2224 wxDataViewModel *model = ctrl->GetModel();
2225
2226 wxDataViewItem item(ctrl->GTKPathToItem(wxGtkTreePath(path)));
2227
2228 unsigned int model_col = cell->GetOwner()->GetModelColumn();
2229
2230 model->ChangeValue( value, item, model_col );
2231 }
2232
2233 IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
2234
2235 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
2236 wxDataViewCellMode mode, int align ) :
2237 wxDataViewRenderer( varianttype, mode, align )
2238 {
2239 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
2240
2241 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
2242 {
2243 g_signal_connect_after( m_renderer, "toggled",
2244 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
2245 }
2246 else
2247 {
2248 GValue gvalue = { 0, };
2249 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2250 g_value_set_boolean( &gvalue, false );
2251 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
2252 g_value_unset( &gvalue );
2253 }
2254
2255 SetMode(mode);
2256 SetAlignment(align);
2257 }
2258
2259 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
2260 {
2261 bool tmp = value;
2262
2263 GValue gvalue = { 0, };
2264 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2265 g_value_set_boolean( &gvalue, tmp );
2266 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
2267 g_value_unset( &gvalue );
2268
2269 return true;
2270 }
2271
2272 bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
2273 {
2274 GValue gvalue = { 0, };
2275 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2276 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
2277 bool tmp = g_value_get_boolean( &gvalue );
2278 g_value_unset( &gvalue );
2279
2280 value = tmp;
2281
2282 return true;
2283 }
2284
2285 // ---------------------------------------------------------
2286 // wxDataViewCustomRenderer
2287 // ---------------------------------------------------------
2288
2289 class wxDataViewCtrlDCImpl: public wxWindowDCImpl
2290 {
2291 public:
2292 wxDataViewCtrlDCImpl( wxDC *owner, wxDataViewCtrl *window ) :
2293 wxWindowDCImpl( owner )
2294 {
2295 GtkWidget *widget = window->m_treeview;
2296 // Set later
2297 m_gdkwindow = NULL;
2298
2299 m_window = window;
2300
2301 m_context = window->GTKGetPangoDefaultContext();
2302 m_layout = pango_layout_new( m_context );
2303 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
2304
2305 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
2306
2307 // Set m_gdkwindow later
2308 // SetUpDC();
2309 }
2310 };
2311
2312 class wxDataViewCtrlDC: public wxWindowDC
2313 {
2314 public:
2315 wxDataViewCtrlDC( wxDataViewCtrl *window ) :
2316 wxWindowDC( new wxDataViewCtrlDCImpl( this, window ) )
2317 { }
2318 };
2319
2320
2321 // ---------------------------------------------------------
2322 // wxDataViewCustomRenderer
2323 // ---------------------------------------------------------
2324
2325 IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
2326
2327 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
2328 wxDataViewCellMode mode,
2329 int align,
2330 bool no_init )
2331 : wxDataViewCustomRendererBase( varianttype, mode, align )
2332 {
2333 m_dc = NULL;
2334 m_text_renderer = NULL;
2335
2336 if (no_init)
2337 m_renderer = NULL;
2338 else
2339 Init(mode, align);
2340 }
2341
2342 GtkCellRendererText *wxDataViewCustomRenderer::GtkGetTextRenderer() const
2343 {
2344 if ( !m_text_renderer )
2345 {
2346 // we create it on demand so need to do it even from a const function
2347 const_cast<wxDataViewCustomRenderer *>(this)->
2348 m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2349 }
2350
2351 return m_text_renderer;
2352 }
2353
2354 void wxDataViewCustomRenderer::RenderText( const wxString &text,
2355 int xoffset,
2356 wxRect cell,
2357 wxDC *WXUNUSED(dc),
2358 int WXUNUSED(state) )
2359 {
2360
2361 GtkCellRendererText * const textRenderer = GtkGetTextRenderer();
2362
2363 GValue gvalue = { 0, };
2364 g_value_init( &gvalue, G_TYPE_STRING );
2365 g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) );
2366 g_object_set_property( G_OBJECT(textRenderer), "text", &gvalue );
2367 g_value_unset( &gvalue );
2368
2369 GtkApplyAttr(textRenderer, GetAttr());
2370
2371 GdkRectangle cell_area;
2372 wxRectToGDKRect(cell, cell_area);
2373 cell_area.x += xoffset;
2374 cell_area.width -= xoffset;
2375
2376 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer),
2377 m_renderParams.window,
2378 m_renderParams.widget,
2379 m_renderParams.background_area,
2380 &cell_area,
2381 m_renderParams.expose_area,
2382 (GtkCellRendererState) m_renderParams.flags );
2383 }
2384
2385 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
2386 {
2387 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
2388 renderer->cell = this;
2389
2390 m_renderer = (GtkCellRenderer*) renderer;
2391
2392 SetMode(mode);
2393 SetAlignment(align);
2394
2395 GtkInitHandlers();
2396
2397 return true;
2398 }
2399
2400 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2401 {
2402 if (m_dc)
2403 delete m_dc;
2404
2405 if (m_text_renderer)
2406 gtk_object_sink( GTK_OBJECT(m_text_renderer) );
2407 }
2408
2409 wxDC *wxDataViewCustomRenderer::GetDC()
2410 {
2411 if (m_dc == NULL)
2412 {
2413 if (GetOwner() == NULL)
2414 return NULL;
2415 if (GetOwner()->GetOwner() == NULL)
2416 return NULL;
2417 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2418 }
2419
2420 return m_dc;
2421 }
2422
2423 // ---------------------------------------------------------
2424 // wxDataViewProgressRenderer
2425 // ---------------------------------------------------------
2426
2427 IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
2428
2429 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
2430 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
2431 wxDataViewCustomRenderer( varianttype, mode, align, true )
2432 {
2433 m_label = label;
2434 m_value = 0;
2435
2436 #ifdef __WXGTK26__
2437 if (!gtk_check_version(2,6,0))
2438 {
2439 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
2440
2441 GValue gvalue = { 0, };
2442 g_value_init( &gvalue, G_TYPE_STRING );
2443
2444 g_value_set_string( &gvalue, wxGTK_CONV_FONT( m_label, GetOwner()->GetOwner()->GetFont() ) );
2445 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2446 g_value_unset( &gvalue );
2447
2448 SetMode(mode);
2449 SetAlignment(align);
2450 }
2451 else
2452 #endif
2453 {
2454 // Use custom cell code
2455 wxDataViewCustomRenderer::Init(mode, align);
2456 }
2457 }
2458
2459 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2460 {
2461 }
2462
2463 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
2464 {
2465 #ifdef __WXGTK26__
2466 if (!gtk_check_version(2,6,0))
2467 {
2468 gint tmp = (long) value;
2469 GValue gvalue = { 0, };
2470 g_value_init( &gvalue, G_TYPE_INT );
2471 g_value_set_int( &gvalue, tmp );
2472 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
2473 g_value_unset( &gvalue );
2474 }
2475 else
2476 #endif
2477 {
2478 m_value = (long) value;
2479
2480 if (m_value < 0) m_value = 0;
2481 if (m_value > 100) m_value = 100;
2482 }
2483
2484 return true;
2485 }
2486
2487 bool wxDataViewProgressRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2488 {
2489 return false;
2490 }
2491
2492 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
2493 {
2494 double pct = (double)m_value / 100.0;
2495 wxRect bar = cell;
2496 bar.width = (int)(cell.width * pct);
2497 dc->SetPen( *wxTRANSPARENT_PEN );
2498 dc->SetBrush( *wxBLUE_BRUSH );
2499 dc->DrawRectangle( bar );
2500
2501 dc->SetBrush( *wxTRANSPARENT_BRUSH );
2502 dc->SetPen( *wxBLACK_PEN );
2503 dc->DrawRectangle( cell );
2504
2505 return true;
2506 }
2507
2508 wxSize wxDataViewProgressRenderer::GetSize() const
2509 {
2510 return wxSize(40,12);
2511 }
2512
2513 // -------------------------------------
2514 // wxDataViewChoiceRenderer
2515 // -------------------------------------
2516
2517 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString &choices,
2518 wxDataViewCellMode mode, int alignment ) :
2519 wxDataViewCustomRenderer( "string", mode, alignment, true )
2520 {
2521 m_choices = choices;
2522
2523 #ifdef __WXGTK26__
2524 if (!gtk_check_version(2,6,0))
2525 {
2526 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_combo_new();
2527
2528 GtkListStore *store = gtk_list_store_new( 1, G_TYPE_STRING );
2529 for (size_t n = 0; n < m_choices.GetCount(); n++)
2530 {
2531 gtk_list_store_insert_with_values(
2532 store, NULL, n, 0,
2533 static_cast<const char *>(m_choices[n].utf8_str()), -1 );
2534 }
2535
2536 g_object_set (m_renderer,
2537 "model", store,
2538 "text-column", 0,
2539 "has-entry", FALSE,
2540 NULL);
2541
2542 bool editable = (mode & wxDATAVIEW_CELL_EDITABLE);
2543 g_object_set (m_renderer, "editable", editable, NULL);
2544
2545 SetAlignment(alignment);
2546
2547 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2548
2549 GtkInitHandlers();
2550 }
2551 else
2552 #endif
2553 {
2554 // Use custom cell code
2555 wxDataViewCustomRenderer::Init(mode, alignment);
2556 }
2557 }
2558
2559 bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
2560 {
2561 RenderText( m_data, 0, rect, dc, state );
2562 return true;
2563 }
2564
2565 wxSize wxDataViewChoiceRenderer::GetSize() const
2566 {
2567 return wxSize(70,20);
2568 }
2569
2570 bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
2571 {
2572
2573 #ifdef __WXGTK26__
2574 if (!gtk_check_version(2,6,0))
2575 {
2576 GValue gvalue = { 0, };
2577 g_value_init( &gvalue, G_TYPE_STRING );
2578 g_value_set_string(&gvalue,
2579 wxGTK_CONV_FONT(value.GetString(),
2580 GetOwner()->GetOwner()->GetFont()));
2581 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2582 g_value_unset( &gvalue );
2583 }
2584 else
2585 #endif
2586 m_data = value.GetString();
2587
2588 return true;
2589 }
2590
2591 bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
2592 {
2593 #ifdef __WXGTK26__
2594 if (!gtk_check_version(2,6,0))
2595 {
2596 GValue gvalue = { 0, };
2597 g_value_init( &gvalue, G_TYPE_STRING );
2598 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
2599 wxString temp = wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue),
2600 GetOwner()->GetOwner()->GetFont());
2601 g_value_unset( &gvalue );
2602 value = temp;
2603
2604 //wxPrintf( "temp %s\n", temp );
2605 // TODO: remove this code
2606 }
2607 else
2608 #endif
2609 value = m_data;
2610
2611 return true;
2612 }
2613
2614 void wxDataViewChoiceRenderer::SetAlignment( int align )
2615 {
2616 wxDataViewCustomRenderer::SetAlignment(align);
2617
2618 if (gtk_check_version(2,10,0))
2619 return;
2620
2621 // horizontal alignment:
2622 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2623 if (align & wxALIGN_RIGHT)
2624 pangoAlign = PANGO_ALIGN_RIGHT;
2625 else if (align & wxALIGN_CENTER_HORIZONTAL)
2626 pangoAlign = PANGO_ALIGN_CENTER;
2627
2628 GValue gvalue = { 0, };
2629 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2630 g_value_set_enum( &gvalue, pangoAlign );
2631 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2632 g_value_unset( &gvalue );
2633 }
2634
2635 // ----------------------------------------------------------------------------
2636 // wxDataViewChoiceByIndexRenderer
2637 // ----------------------------------------------------------------------------
2638
2639 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
2640 wxDataViewCellMode mode, int alignment ) :
2641 wxDataViewChoiceRenderer( choices, mode, alignment )
2642 {
2643 }
2644
2645 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const gchar *itempath, const wxString& str)
2646 {
2647 wxVariant value( (long) GetChoices().Index( str ) );
2648
2649 if (!Validate( value ))
2650 return;
2651
2652 wxDataViewItem
2653 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
2654
2655 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
2656 }
2657
2658 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
2659 {
2660 wxVariant string_value = GetChoice( value.GetLong() );
2661 return wxDataViewChoiceRenderer::SetValue( string_value );
2662 }
2663
2664 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const
2665 {
2666 wxVariant string_value;
2667 if (!wxDataViewChoiceRenderer::GetValue( string_value ))
2668 return false;
2669
2670 value = (long) GetChoices().Index( string_value.GetString() );
2671 return true;
2672 }
2673
2674 // ---------------------------------------------------------
2675 // wxDataViewDateRenderer
2676 // ---------------------------------------------------------
2677
2678 class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
2679 {
2680 public:
2681 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
2682 wxDataViewModel *model, const wxDataViewItem &item, unsigned int col ) :
2683 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
2684 {
2685 m_model = model;
2686 m_item = item;
2687 m_col = col;
2688 m_cal = new wxCalendarCtrl( this, -1, *value );
2689 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
2690 sizer->Add( m_cal, 1, wxGROW );
2691 SetSizer( sizer );
2692 sizer->Fit( this );
2693 }
2694
2695 virtual void OnDismiss()
2696 {
2697 }
2698
2699 void OnCalendar( wxCalendarEvent &event );
2700
2701 wxCalendarCtrl *m_cal;
2702 wxDataViewModel *m_model;
2703 wxDataViewItem m_item;
2704 unsigned int m_col;
2705
2706 private:
2707 DECLARE_EVENT_TABLE()
2708 };
2709
2710 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
2711 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
2712 END_EVENT_TABLE()
2713
2714 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
2715 {
2716 m_model->ChangeValue( event.GetDate(), m_item, m_col );
2717 DismissAndNotify();
2718 }
2719
2720 IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
2721
2722 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
2723 wxDataViewCellMode mode, int align ) :
2724 wxDataViewCustomRenderer( varianttype, mode, align )
2725 {
2726 SetMode(mode);
2727 SetAlignment(align);
2728 }
2729
2730 bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
2731 {
2732 m_date = value.GetDateTime();
2733
2734 return true;
2735 }
2736
2737 bool wxDataViewDateRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2738 {
2739 return false;
2740 }
2741
2742 bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
2743 {
2744 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
2745 wxString tmp = m_date.FormatDate();
2746 RenderText( tmp, 0, cell, dc, state );
2747 return true;
2748 }
2749
2750 wxSize wxDataViewDateRenderer::GetSize() const
2751 {
2752 wxString tmp = m_date.FormatDate();
2753 wxCoord x,y,d;
2754 GetView()->GetTextExtent( tmp, &x, &y, &d );
2755 return wxSize(x,y+d);
2756 }
2757
2758 bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model,
2759 const wxDataViewItem &item, unsigned int col )
2760 {
2761 wxVariant variant;
2762 model->GetValue( variant, item, col );
2763 wxDateTime value = variant.GetDateTime();
2764
2765 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
2766 GetOwner()->GetOwner()->GetParent(), &value, model, item, col );
2767 wxPoint pos = wxGetMousePosition();
2768 popup->Move( pos );
2769 popup->Layout();
2770 popup->Popup( popup->m_cal );
2771
2772 return true;
2773 }
2774
2775
2776 // ---------------------------------------------------------
2777 // wxDataViewIconTextRenderer
2778 // ---------------------------------------------------------
2779
2780 IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
2781
2782 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2783 (
2784 const wxString &varianttype,
2785 wxDataViewCellMode mode,
2786 int align
2787 )
2788 : wxDataViewTextRenderer(varianttype, mode, align)
2789 {
2790 m_rendererIcon = gtk_cell_renderer_pixbuf_new();
2791 }
2792
2793 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2794 {
2795 }
2796
2797 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
2798 {
2799 // add the icon renderer first
2800 gtk_tree_view_column_pack_start(column, m_rendererIcon, FALSE /* !expand */);
2801
2802 // add the text renderer too
2803 wxDataViewRenderer::GtkPackIntoColumn(column);
2804 }
2805
2806 bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
2807 {
2808 m_value << value;
2809
2810 SetTextValue(m_value.GetText());
2811 SetPixbufProp(m_rendererIcon, m_value.GetIcon().GetPixbuf());
2812
2813 return true;
2814 }
2815
2816 bool wxDataViewIconTextRenderer::GetValue(wxVariant& value) const
2817 {
2818 wxString str;
2819 if ( !GetTextValue(str) )
2820 return false;
2821
2822 // user doesn't have any way to edit the icon so leave it unchanged
2823 value << wxDataViewIconText(str, m_value.GetIcon());
2824
2825 return true;
2826 }
2827
2828 void
2829 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant& value,
2830 const wxDataViewItem& item,
2831 unsigned col)
2832 {
2833 // we receive just the text part of our value as it's the only one which
2834 // can be edited, but we need the full wxDataViewIconText value for the
2835 // model
2836 wxVariant valueIconText;
2837 valueIconText << wxDataViewIconText(value.GetString(), m_value.GetIcon());
2838 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText, item, col);
2839 }
2840
2841 // ---------------------------------------------------------
2842 // wxDataViewColumn
2843 // ---------------------------------------------------------
2844
2845
2846 static gboolean
2847 gtk_dataview_header_button_press_callback( GtkWidget *WXUNUSED(widget),
2848 GdkEventButton *gdk_event,
2849 wxDataViewColumn *column )
2850 {
2851 if (gdk_event->type != GDK_BUTTON_PRESS)
2852 return FALSE;
2853
2854 if (gdk_event->button == 1)
2855 {
2856 gs_lastLeftClickHeader = column;
2857
2858 wxDataViewCtrl *dv = column->GetOwner();
2859 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
2860 event.SetDataViewColumn( column );
2861 event.SetModel( dv->GetModel() );
2862 if (dv->HandleWindowEvent( event ))
2863 return FALSE;
2864 }
2865
2866 if (gdk_event->button == 3)
2867 {
2868 wxDataViewCtrl *dv = column->GetOwner();
2869 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, dv->GetId() );
2870 event.SetDataViewColumn( column );
2871 event.SetModel( dv->GetModel() );
2872 if (dv->HandleWindowEvent( event ))
2873 return FALSE;
2874 }
2875
2876 return FALSE;
2877 }
2878
2879 extern "C"
2880 {
2881
2882 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *WXUNUSED(column),
2883 GtkCellRenderer *renderer,
2884 GtkTreeModel *model,
2885 GtkTreeIter *iter,
2886 gpointer data )
2887 {
2888 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
2889 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
2890
2891 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
2892
2893 wxDataViewItem item( (void*) iter->user_data );
2894
2895 wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel();
2896
2897 if (!wx_model->IsVirtualListModel())
2898 {
2899 gboolean visible;
2900 if (wx_model->IsContainer( item ))
2901 {
2902 visible = wx_model->HasContainerColumns( item ) ||
2903 (cell->GetOwner()->GetModelColumn() == 0);
2904 }
2905 else
2906 {
2907 visible = true;
2908 }
2909
2910 GValue gvalue = { 0, };
2911 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2912 g_value_set_boolean( &gvalue, visible );
2913 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2914 g_value_unset( &gvalue );
2915
2916 if ( !visible )
2917 return;
2918 }
2919
2920 wxVariant value;
2921 wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
2922
2923 if (value.GetType() != cell->GetVariantType())
2924 {
2925 wxLogError( wxT("Wrong type, required: %s but: %s"),
2926 value.GetType().c_str(),
2927 cell->GetVariantType().c_str() );
2928 }
2929
2930 cell->SetValue( value );
2931
2932
2933 // deal with attributes: if the renderer doesn't support them at all, we
2934 // don't even need to query the model for them
2935 if ( !cell->GtkSupportsAttrs() )
2936 return;
2937
2938 // it can support attributes so check if this item has any
2939 wxDataViewItemAttr attr;
2940 if ( wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr )
2941 || !cell->GtkIsUsingDefaultAttrs() )
2942 {
2943 bool usingDefaultAttrs = !cell->GtkSetAttr(attr);
2944 cell->GtkSetUsingDefaultAttrs(usingDefaultAttrs);
2945 }
2946 // else: no custom attributes specified and we're already using the default
2947 // ones -- nothing to do
2948 }
2949
2950 } // extern "C"
2951
2952 #include <wx/listimpl.cpp>
2953 WX_DEFINE_LIST(wxDataViewColumnList)
2954
2955 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
2956 unsigned int model_column, int width,
2957 wxAlignment align, int flags )
2958 : wxDataViewColumnBase( cell, model_column )
2959 {
2960 Init( align, flags, width );
2961
2962 SetTitle( title );
2963 }
2964
2965 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
2966 unsigned int model_column, int width,
2967 wxAlignment align, int flags )
2968 : wxDataViewColumnBase( bitmap, cell, model_column )
2969 {
2970 Init( align, flags, width );
2971
2972 SetBitmap( bitmap );
2973 }
2974
2975 void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
2976 {
2977 m_isConnected = false;
2978
2979 GtkTreeViewColumn *column = gtk_tree_view_column_new();
2980 m_column = (GtkWidget*) column;
2981
2982 SetFlags( flags );
2983 SetAlignment( align );
2984
2985 SetWidth( width );
2986
2987 // Create container for icon and label
2988 GtkWidget *box = gtk_hbox_new( FALSE, 1 );
2989 gtk_widget_show( box );
2990 // gtk_container_set_border_width((GtkContainer*)box, 2);
2991 m_image = gtk_image_new();
2992 gtk_box_pack_start(GTK_BOX(box), m_image, FALSE, FALSE, 1);
2993 m_label = gtk_label_new("");
2994 gtk_box_pack_end( GTK_BOX(box), GTK_WIDGET(m_label), FALSE, FALSE, 1 );
2995 gtk_tree_view_column_set_widget( column, box );
2996
2997 wxDataViewRenderer * const colRenderer = GetRenderer();
2998 GtkCellRenderer * const cellRenderer = colRenderer->GetGtkHandle();
2999
3000 colRenderer->GtkPackIntoColumn(column);
3001
3002 gtk_tree_view_column_set_cell_data_func( column, cellRenderer,
3003 wxGtkTreeCellDataFunc, (gpointer) colRenderer, NULL );
3004 }
3005
3006 void wxDataViewColumn::OnInternalIdle()
3007 {
3008 if (m_isConnected)
3009 return;
3010
3011 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
3012 {
3013 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3014 if (column->button)
3015 {
3016 g_signal_connect(column->button, "button_press_event",
3017 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
3018
3019 // otherwise the event will be blocked by GTK+
3020 gtk_tree_view_column_set_clickable( column, TRUE );
3021
3022 m_isConnected = true;
3023 }
3024 }
3025 }
3026
3027 void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
3028 {
3029 wxDataViewColumnBase::SetOwner( owner );
3030
3031 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3032
3033 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3034 }
3035
3036 void wxDataViewColumn::SetTitle( const wxString &title )
3037 {
3038 wxDataViewCtrl *ctrl = GetOwner();
3039 gtk_label_set_text( GTK_LABEL(m_label), ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
3040 : wxGTK_CONV_SYS(title) );
3041 if (title.empty())
3042 gtk_widget_hide( m_label );
3043 else
3044 gtk_widget_show( m_label );
3045 }
3046
3047 wxString wxDataViewColumn::GetTitle() const
3048 {
3049 return wxGTK_CONV_BACK_FONT(
3050 gtk_label_get_text( GTK_LABEL(m_label) ),
3051 GetOwner()->GetFont()
3052 );
3053 }
3054
3055 void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
3056 {
3057 wxDataViewColumnBase::SetBitmap( bitmap );
3058
3059 if (bitmap.Ok())
3060 {
3061 GtkImage *gtk_image = GTK_IMAGE(m_image);
3062
3063 GdkBitmap *mask = NULL;
3064 if (bitmap.GetMask())
3065 mask = bitmap.GetMask()->GetBitmap();
3066
3067 if (bitmap.HasPixbuf())
3068 {
3069 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
3070 bitmap.GetPixbuf());
3071 }
3072 else
3073 {
3074 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
3075 bitmap.GetPixmap(), mask);
3076 }
3077 gtk_widget_show( m_image );
3078 }
3079 else
3080 {
3081 gtk_widget_hide( m_image );
3082 }
3083 }
3084
3085 void wxDataViewColumn::SetHidden( bool hidden )
3086 {
3087 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
3088 }
3089
3090 void wxDataViewColumn::SetResizeable( bool resizeable )
3091 {
3092 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
3093 }
3094
3095 void wxDataViewColumn::SetAlignment( wxAlignment align )
3096 {
3097 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3098
3099 gfloat xalign = 0.0;
3100 if (align == wxALIGN_RIGHT)
3101 xalign = 1.0;
3102 if (align == wxALIGN_CENTER_HORIZONTAL ||
3103 align == wxALIGN_CENTER)
3104 xalign = 0.5;
3105
3106 gtk_tree_view_column_set_alignment( column, xalign );
3107
3108 if (m_renderer && m_renderer->GetAlignment() == -1)
3109 m_renderer->GtkUpdateAlignment();
3110 }
3111
3112 wxAlignment wxDataViewColumn::GetAlignment() const
3113 {
3114 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
3115
3116 if (xalign == 1.0)
3117 return wxALIGN_RIGHT;
3118 if (xalign == 0.5)
3119 return wxALIGN_CENTER_HORIZONTAL;
3120
3121 return wxALIGN_LEFT;
3122 }
3123
3124 void wxDataViewColumn::SetSortable( bool sortable )
3125 {
3126 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3127
3128 if ( sortable )
3129 {
3130 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
3131 }
3132 else
3133 {
3134 gtk_tree_view_column_set_sort_column_id( column, -1 );
3135 gtk_tree_view_column_set_sort_indicator( column, FALSE );
3136 gtk_tree_view_column_set_clickable( column, FALSE );
3137 }
3138 }
3139
3140 bool wxDataViewColumn::IsSortable() const
3141 {
3142 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3143 return gtk_tree_view_column_get_clickable( column );
3144 }
3145
3146 void wxDataViewColumn::SetAsSortKey( bool WXUNUSED(sort) )
3147 {
3148 // it might not make sense to have this function in wxHeaderColumn at
3149 // all in fact, changing of the sort order should only be done using the
3150 // associated control API
3151 wxFAIL_MSG( "not implemented" );
3152 }
3153
3154 bool wxDataViewColumn::IsSortKey() const
3155 {
3156 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3157 return gtk_tree_view_column_get_sort_indicator( column );
3158 }
3159
3160 bool wxDataViewColumn::IsResizeable() const
3161 {
3162 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3163 return gtk_tree_view_column_get_resizable( column );
3164 }
3165
3166 bool wxDataViewColumn::IsHidden() const
3167 {
3168 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3169 return !gtk_tree_view_column_get_visible( column );
3170 }
3171
3172 void wxDataViewColumn::SetSortOrder( bool ascending )
3173 {
3174 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3175
3176 if (ascending)
3177 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
3178 else
3179 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
3180
3181 gtk_tree_view_column_set_sort_indicator( column, TRUE );
3182 }
3183
3184 bool wxDataViewColumn::IsSortOrderAscending() const
3185 {
3186 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3187
3188 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
3189 }
3190
3191 void wxDataViewColumn::SetMinWidth( int width )
3192 {
3193 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3194 }
3195
3196 int wxDataViewColumn::GetMinWidth() const
3197 {
3198 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
3199 }
3200
3201 int wxDataViewColumn::GetWidth() const
3202 {
3203 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
3204 }
3205
3206 void wxDataViewColumn::SetWidth( int width )
3207 {
3208 if (width < 0)
3209 {
3210 #if 1
3211 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
3212
3213 // TODO find a better calculation
3214 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), wxDVC_DEFAULT_WIDTH );
3215 #else
3216 // this is unpractical for large numbers of items and disables
3217 // user resizing, which is totally unexpected
3218 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
3219 #endif
3220 }
3221 else
3222 {
3223 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
3224
3225 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3226 }
3227 }
3228
3229 void wxDataViewColumn::SetReorderable( bool reorderable )
3230 {
3231 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
3232 }
3233
3234 bool wxDataViewColumn::IsReorderable() const
3235 {
3236 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) );
3237 }
3238
3239 //-----------------------------------------------------------------------------
3240 // wxGtkTreeModelNode
3241 //-----------------------------------------------------------------------------
3242
3243 #if 0
3244 class wxGtkTreeModelChildWithPos
3245 {
3246 public:
3247 unsigned int pos;
3248 void *id;
3249 };
3250
3251 static
3252 int wxGtkTreeModelChildWithPosCmp( const void* data1, const void* data2, const void* user_data )
3253 {
3254 const wxGtkTreeModelChildWithPos* child1 = (const wxGtkTreeModelChildWithPos*) data1;
3255 const wxGtkTreeModelChildWithPos* child2 = (const wxGtkTreeModelChildWithPos*) data2;
3256 const wxDataViewCtrlInternal *internal = (const wxDataViewCtrlInternal *) user_data;
3257 int ret = internal->GetDataViewModel()->Compare( child1->id, child2->id,
3258 internal->GetSortColumn(), (internal->GetSortOrder() == GTK_SORT_DESCENDING) );
3259
3260 return ret;
3261 }
3262 #else
3263 static
3264 int LINKAGEMODE wxGtkTreeModelChildPtrCmp( void*** data1, void*** data2 )
3265 {
3266 return gs_internal->GetDataViewModel()->Compare( **data1, **data2,
3267 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
3268 }
3269
3270 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr );
3271 #endif
3272
3273 void wxGtkTreeModelNode::Resort()
3274 {
3275 size_t child_count = GetChildCount();
3276 if (child_count == 0)
3277 return;
3278
3279 size_t node_count = GetNodesCount();
3280
3281 if (child_count == 1)
3282 {
3283 if (node_count == 1)
3284 {
3285 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
3286 node->Resort();
3287 }
3288 return;
3289 }
3290
3291 gint *new_order = new gint[child_count];
3292
3293 #if 1
3294 // m_children has the original *void
3295 // ptrs points to these
3296 wxGtkTreeModelChildrenPtr ptrs;
3297 size_t i;
3298 for (i = 0; i < child_count; i++)
3299 ptrs.Add( &(m_children[i]) );
3300 // Sort the ptrs
3301 gs_internal = m_internal;
3302 ptrs.Sort( &wxGtkTreeModelChildPtrCmp );
3303
3304 wxGtkTreeModelChildren temp;
3305 void** base_ptr = &(m_children[0]);
3306 // Transfer positions to new_order array and
3307 // IDs to temp
3308 for (i = 0; i < child_count; i++)
3309 {
3310 new_order[i] = ptrs[i] - base_ptr;
3311 temp.Add( *ptrs[i] );
3312 }
3313
3314 // Transfer IDs back to m_children
3315 m_children.Clear();
3316 WX_APPEND_ARRAY( temp, m_children );
3317 #endif
3318 #if 0
3319 // Too slow
3320
3321 // Build up array with IDs and original positions
3322 wxGtkTreeModelChildWithPos* temp = new wxGtkTreeModelChildWithPos[child_count];
3323 size_t i;
3324 for (i = 0; i < child_count; i++)
3325 {
3326 temp[i].pos = i;
3327 temp[i].id = m_children[i];
3328 }
3329 // Sort array keeping original positions
3330 wxQsort( temp, child_count, sizeof(wxGtkTreeModelChildWithPos),
3331 &wxGtkTreeModelChildWithPosCmp, m_internal );
3332 // Transfer positions to new_order array and
3333 // IDs to m_children
3334 m_children.Clear();
3335 for (i = 0; i < child_count; i++)
3336 {
3337 new_order[i] = temp[i].pos;
3338 m_children.Add( temp[i].id );
3339 }
3340 // Delete array
3341 delete [] temp;
3342 #endif
3343
3344 #if 0
3345 // Too slow
3346
3347 wxGtkTreeModelChildren temp;
3348 WX_APPEND_ARRAY( temp, m_children );
3349
3350 gs_internal = m_internal;
3351 m_children.Sort( &wxGtkTreeModelChildCmp );
3352
3353 unsigned int pos;
3354 for (pos = 0; pos < child_count; pos++)
3355 {
3356 void *id = m_children.Item( pos );
3357 int old_pos = temp.Index( id );
3358 new_order[pos] = old_pos;
3359 }
3360 #endif
3361
3362 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
3363
3364 GtkTreeIter iter;
3365 iter.user_data = GetItem().GetID();
3366 iter.stamp = m_internal->GetGtkModel()->stamp;
3367
3368 gtk_tree_model_rows_reordered( gtk_tree_model,
3369 wxGtkTreePath(m_internal->get_path(&iter)), &iter, new_order );
3370
3371 delete [] new_order;
3372
3373 unsigned int pos;
3374 for (pos = 0; pos < node_count; pos++)
3375 {
3376 wxGtkTreeModelNode *node = m_nodes.Item( pos );
3377 node->Resort();
3378 }
3379 }
3380
3381 //-----------------------------------------------------------------------------
3382 // wxDataViewCtrlInternal
3383 //-----------------------------------------------------------------------------
3384
3385 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model )
3386 {
3387 m_owner = owner;
3388 m_wx_model = wx_model;
3389
3390 m_gtk_model = NULL;
3391 m_root = NULL;
3392 m_sort_order = GTK_SORT_ASCENDING;
3393 m_sort_column = -1;
3394 m_dataview_sort_column = NULL;
3395
3396 m_dragDataObject = NULL;
3397 m_dropDataObject = NULL;
3398
3399 m_dirty = false;
3400
3401 m_gtk_model = wxgtk_tree_model_new();
3402 m_gtk_model->internal = this;
3403
3404 m_notifier = new wxGtkDataViewModelNotifier( wx_model, this );
3405
3406 wx_model->AddNotifier( m_notifier );
3407
3408 // g_object_unref( gtk_model ); ???
3409
3410 if (!m_wx_model->IsVirtualListModel())
3411 InitTree();
3412
3413 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model) );
3414 }
3415
3416 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3417 {
3418 m_wx_model->RemoveNotifier( m_notifier );
3419
3420 // remove the model from the GtkTreeView before it gets destroyed
3421 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner->GtkGetTreeView() ), NULL );
3422
3423 g_object_unref( m_gtk_model );
3424
3425 delete m_dragDataObject;
3426 delete m_dropDataObject;
3427 }
3428
3429 void wxDataViewCtrlInternal::ScheduleRefresh()
3430 {
3431 m_dirty = true;
3432 }
3433
3434 void wxDataViewCtrlInternal::OnInternalIdle()
3435 {
3436 if (m_dirty)
3437 {
3438 GtkWidget *widget = m_owner->GtkGetTreeView();
3439 gtk_widget_queue_draw( widget );
3440 m_dirty = false;
3441 }
3442 }
3443
3444 void wxDataViewCtrlInternal::InitTree()
3445 {
3446 wxDataViewItem item;
3447 m_root = new wxGtkTreeModelNode( NULL, item, this );
3448
3449 BuildBranch( m_root );
3450 }
3451
3452 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
3453 {
3454 if (node->GetChildCount() == 0)
3455 {
3456 wxDataViewItemArray children;
3457 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
3458
3459 unsigned int pos;
3460 for (pos = 0; pos < count; pos++)
3461 {
3462 wxDataViewItem child = children[pos];
3463
3464 if (m_wx_model->IsContainer( child ))
3465 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
3466 else
3467 node->AddLeave( child.GetID() );
3468
3469 // Don't send any events here
3470 }
3471 }
3472 }
3473
3474 // GTK+ dnd iface
3475
3476 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format )
3477 {
3478 wxGtkString atom_str( gdk_atom_name( format ) );
3479 m_dragSourceTargetEntryTarget = wxCharBuffer( atom_str );
3480
3481 m_dragSourceTargetEntry.target = m_dragSourceTargetEntryTarget.data();
3482 m_dragSourceTargetEntry.flags = 0;
3483 m_dragSourceTargetEntry.info = static_cast<guint>(-1);
3484
3485 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3486 GDK_BUTTON1_MASK, &m_dragSourceTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
3487
3488 return true;
3489 }
3490
3491 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat &format )
3492 {
3493 wxGtkString atom_str( gdk_atom_name( format ) );
3494 m_dropTargetTargetEntryTarget = wxCharBuffer( atom_str );
3495
3496 m_dropTargetTargetEntry.target = m_dropTargetTargetEntryTarget.data();
3497 m_dropTargetTargetEntry.flags = 0;
3498 m_dropTargetTargetEntry.info = static_cast<guint>(-1);
3499
3500 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3501 &m_dropTargetTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
3502
3503 return true;
3504 }
3505
3506 gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
3507 GtkTreePath *path )
3508 {
3509 delete m_dragDataObject;
3510
3511 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3512 if ( !item )
3513 return FALSE;
3514
3515 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
3516 event.SetEventObject( m_owner );
3517 event.SetItem( item );
3518 event.SetModel( m_wx_model );
3519 gint x, y;
3520 gtk_widget_get_pointer(m_owner->GtkGetTreeView(), &x, &y);
3521 event.SetPosition(x, y);
3522 if (!m_owner->HandleWindowEvent( event ))
3523 return FALSE;
3524
3525 if (!event.IsAllowed())
3526 return FALSE;
3527
3528 wxDataObject *obj = event.GetDataObject();
3529 if (!obj)
3530 return FALSE;
3531
3532 m_dragDataObject = obj;
3533
3534 return TRUE;
3535 }
3536
3537 gboolean
3538 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
3539 GtkTreePath *WXUNUSED(path))
3540 {
3541 return FALSE;
3542 }
3543
3544 gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
3545 GtkTreePath *path, GtkSelectionData *selection_data )
3546 {
3547 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3548 if ( !item )
3549 return FALSE;
3550
3551 if (!m_dragDataObject->IsSupported( selection_data->target ))
3552 return FALSE;
3553
3554 size_t size = m_dragDataObject->GetDataSize( selection_data->target );
3555 if (size == 0)
3556 return FALSE;
3557
3558 void *buf = malloc( size );
3559
3560 gboolean res = FALSE;
3561 if (m_dragDataObject->GetDataHere( selection_data->target, buf ))
3562 {
3563 res = TRUE;
3564
3565 gtk_selection_data_set( selection_data, selection_data->target,
3566 8, (const guchar*) buf, size );
3567 }
3568
3569 free( buf );
3570
3571 return res;
3572 }
3573
3574 gboolean
3575 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
3576 GtkTreePath *path,
3577 GtkSelectionData *selection_data)
3578 {
3579 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3580 if ( !item )
3581 return FALSE;
3582
3583 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() );
3584 event.SetEventObject( m_owner );
3585 event.SetItem( item );
3586 event.SetModel( m_wx_model );
3587 event.SetDataFormat( selection_data->target );
3588 event.SetDataSize( selection_data->length );
3589 event.SetDataBuffer( selection_data->data );
3590 if (!m_owner->HandleWindowEvent( event ))
3591 return FALSE;
3592
3593 if (!event.IsAllowed())
3594 return FALSE;
3595
3596 return TRUE;
3597 }
3598
3599 gboolean
3600 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
3601 GtkTreePath *path,
3602 GtkSelectionData *selection_data)
3603 {
3604 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3605 if ( !item )
3606 return FALSE;
3607
3608 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
3609 event.SetEventObject( m_owner );
3610 event.SetItem( item );
3611 event.SetModel( m_wx_model );
3612 event.SetDataFormat( selection_data->target );
3613 if (!m_owner->HandleWindowEvent( event ))
3614 return FALSE;
3615
3616 if (!event.IsAllowed())
3617 return FALSE;
3618
3619 return TRUE;
3620 }
3621
3622 // notifications from wxDataViewModel
3623
3624 bool wxDataViewCtrlInternal::Cleared()
3625 {
3626 if (m_root)
3627 {
3628 delete m_root;
3629 m_root = NULL;
3630 }
3631
3632 InitTree();
3633
3634 ScheduleRefresh();
3635
3636 return true;
3637 }
3638
3639 void wxDataViewCtrlInternal::Resort()
3640 {
3641 if (!m_wx_model->IsVirtualListModel())
3642 m_root->Resort();
3643
3644 ScheduleRefresh();
3645 }
3646
3647 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
3648 {
3649 if (!m_wx_model->IsVirtualListModel())
3650 {
3651 wxGtkTreeModelNode *parent_node = FindNode( parent );
3652 wxASSERT_MSG(parent_node,
3653 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3654
3655 if (m_wx_model->IsContainer( item ))
3656 parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) );
3657 else
3658 parent_node->AddLeave( item.GetID() );
3659 }
3660
3661 ScheduleRefresh();
3662
3663 return true;
3664 }
3665
3666 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
3667 {
3668 if (!m_wx_model->IsVirtualListModel())
3669 {
3670 wxGtkTreeModelNode *parent_node = FindNode( parent );
3671 wxASSERT_MSG(parent_node,
3672 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3673
3674 parent_node->DeleteChild( item.GetID() );
3675 }
3676
3677 ScheduleRefresh();
3678
3679 return true;
3680 }
3681
3682 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
3683 {
3684 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
3685 event.SetEventObject( m_owner );
3686 event.SetModel( m_owner->GetModel() );
3687 event.SetItem( item );
3688 m_owner->HandleWindowEvent( event );
3689
3690 return true;
3691 }
3692
3693 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int col )
3694 {
3695 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
3696 event.SetEventObject( m_owner );
3697 event.SetModel( m_owner->GetModel() );
3698 event.SetColumn( col );
3699 event.SetDataViewColumn( GetOwner()->GetColumn(col) );
3700 event.SetItem( item );
3701 m_owner->HandleWindowEvent( event );
3702
3703 return true;
3704 }
3705
3706 // GTK+ model iface
3707
3708 GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
3709 {
3710 int flags = 0;
3711
3712 if ( m_wx_model->IsListModel() )
3713 flags |= GTK_TREE_MODEL_LIST_ONLY;
3714
3715 if ( !m_wx_model->IsVirtualListModel() )
3716 flags |= GTK_TREE_MODEL_ITERS_PERSIST;
3717
3718 return GtkTreeModelFlags(flags);
3719 }
3720
3721 gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
3722 {
3723
3724 if (m_wx_model->IsVirtualListModel())
3725 {
3726 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3727
3728 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
3729
3730 if (i >= wx_model->GetCount())
3731 return FALSE;
3732
3733 iter->stamp = m_gtk_model->stamp;
3734 // user_data is just the index +1
3735 iter->user_data = (gpointer) (i+1);
3736
3737 return TRUE;
3738 }
3739 else
3740 {
3741 int depth = gtk_tree_path_get_depth( path );
3742
3743 wxGtkTreeModelNode *node = m_root;
3744
3745 int i;
3746 for (i = 0; i < depth; i++)
3747 {
3748 BuildBranch( node );
3749
3750 gint pos = gtk_tree_path_get_indices (path)[i];
3751 if (pos < 0) return FALSE;
3752 if ((size_t)pos >= node->GetChildCount()) return FALSE;
3753
3754 void* id = node->GetChildren().Item( (size_t) pos );
3755
3756 if (i == depth-1)
3757 {
3758 iter->stamp = m_gtk_model->stamp;
3759 iter->user_data = id;
3760 return TRUE;
3761 }
3762
3763 size_t count = node->GetNodes().GetCount();
3764 size_t pos2;
3765 for (pos2 = 0; pos2 < count; pos2++)
3766 {
3767 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
3768 if (child_node->GetItem().GetID() == id)
3769 {
3770 node = child_node;
3771 break;
3772 }
3773 }
3774 }
3775 }
3776
3777 return FALSE;
3778 }
3779
3780 GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
3781 {
3782 // When this is called from ItemDeleted(), the item is already
3783 // deleted in the model.
3784
3785 GtkTreePath *retval = gtk_tree_path_new ();
3786
3787 if (m_wx_model->IsVirtualListModel())
3788 {
3789 // iter is root, add nothing
3790 if (!iter->user_data)
3791 return retval;
3792
3793 // user_data is just the index +1
3794 int i = ( (wxUIntPtr) iter->user_data ) -1;
3795 gtk_tree_path_append_index (retval, i);
3796 }
3797 else
3798 {
3799 void *id = iter->user_data;
3800
3801 wxGtkTreeModelNode *node = FindParentNode( iter );
3802 while (node)
3803 {
3804 int pos = node->GetChildren().Index( id );
3805
3806 gtk_tree_path_prepend_index( retval, pos );
3807
3808 id = node->GetItem().GetID();
3809 node = node->GetParent();
3810 }
3811 }
3812
3813 return retval;
3814 }
3815
3816 gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
3817 {
3818 if (m_wx_model->IsVirtualListModel())
3819 {
3820 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3821
3822 // user_data is just the index +1
3823 int n = ( (wxUIntPtr) iter->user_data ) -1;
3824
3825 if (n == -1)
3826 {
3827 iter->user_data = NULL;
3828 return FALSE;
3829 }
3830
3831 if (n >= (int) wx_model->GetCount()-1)
3832 {
3833 iter->user_data = NULL;
3834 return FALSE;
3835 }
3836
3837 // user_data is just the index +1 (+2 because we need the next)
3838 iter->user_data = (gpointer) (n+2);
3839 }
3840 else
3841 {
3842 wxGtkTreeModelNode *parent = FindParentNode( iter );
3843 if( parent == NULL )
3844 {
3845 iter->user_data = NULL;
3846 return FALSE;
3847 }
3848
3849 int pos = parent->GetChildren().Index( iter->user_data );
3850
3851 if (pos == (int) parent->GetChildCount()-1)
3852 {
3853 iter->user_data = NULL;
3854 return FALSE;
3855 }
3856
3857 iter->user_data = parent->GetChildren().Item( pos+1 );
3858 }
3859
3860 return TRUE;
3861 }
3862
3863 gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
3864 {
3865 if (m_wx_model->IsVirtualListModel())
3866 {
3867 // this is a list, nodes have no children
3868 if (parent)
3869 return FALSE;
3870
3871 iter->stamp = m_gtk_model->stamp;
3872 iter->user_data = (gpointer) 1;
3873
3874 return TRUE;
3875 }
3876 else
3877 {
3878 if (iter == NULL)
3879 {
3880 if (m_root->GetChildCount() == 0) return FALSE;
3881 iter->stamp = m_gtk_model->stamp;
3882 iter->user_data = (gpointer) m_root->GetChildren().Item( 0 );
3883 return TRUE;
3884 }
3885
3886
3887 wxDataViewItem item;
3888 if (parent)
3889 item = wxDataViewItem( (void*) parent->user_data );
3890
3891 if (!m_wx_model->IsContainer( item ))
3892 return FALSE;
3893
3894 wxGtkTreeModelNode *parent_node = FindNode( parent );
3895 wxASSERT_MSG(parent_node,
3896 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3897
3898 BuildBranch( parent_node );
3899
3900 if (parent_node->GetChildCount() == 0)
3901 return FALSE;
3902
3903 iter->stamp = m_gtk_model->stamp;
3904 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
3905 }
3906
3907 return TRUE;
3908 }
3909
3910 gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
3911 {
3912 if (m_wx_model->IsVirtualListModel())
3913 {
3914 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3915
3916 if (iter == NULL)
3917 return (wx_model->GetCount() > 0);
3918
3919 // this is a list, nodes have no children
3920 return FALSE;
3921 }
3922 else
3923 {
3924 if (iter == NULL)
3925 return (m_root->GetChildCount() > 0);
3926
3927 wxDataViewItem item( (void*) iter->user_data );
3928
3929 bool is_container = m_wx_model->IsContainer( item );
3930
3931 if (!is_container)
3932 return FALSE;
3933
3934 wxGtkTreeModelNode *node = FindNode( iter );
3935 wxASSERT_MSG(node,
3936 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3937
3938 BuildBranch( node );
3939
3940 return (node->GetChildCount() > 0);
3941 }
3942 }
3943
3944 gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
3945 {
3946 if (m_wx_model->IsVirtualListModel())
3947 {
3948 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3949
3950 if (iter == NULL)
3951 return (gint) wx_model->GetCount();
3952
3953 return 0;
3954 }
3955 else
3956 {
3957 if (iter == NULL)
3958 return m_root->GetChildCount();
3959
3960 wxDataViewItem item( (void*) iter->user_data );
3961
3962 if (!m_wx_model->IsContainer( item ))
3963 return 0;
3964
3965 wxGtkTreeModelNode *parent_node = FindNode( iter );
3966 wxASSERT_MSG(parent_node,
3967 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3968
3969 BuildBranch( parent_node );
3970
3971 return parent_node->GetChildCount();
3972 }
3973 }
3974
3975 gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
3976 {
3977 if (m_wx_model->IsVirtualListModel())
3978 {
3979 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3980
3981 if (parent)
3982 return FALSE;
3983
3984 if (n < 0)
3985 return FALSE;
3986
3987 if (n >= (gint) wx_model->GetCount())
3988 return FALSE;
3989
3990 iter->stamp = m_gtk_model->stamp;
3991 // user_data is just the index +1
3992 iter->user_data = (gpointer) (n+1);
3993
3994 return TRUE;
3995 }
3996 else
3997 {
3998 void* id = NULL;
3999 if (parent) id = (void*) parent->user_data;
4000 wxDataViewItem item( id );
4001
4002 if (!m_wx_model->IsContainer( item ))
4003 return FALSE;
4004
4005 wxGtkTreeModelNode *parent_node = FindNode( parent );
4006 wxASSERT_MSG(parent_node,
4007 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4008
4009 BuildBranch( parent_node );
4010
4011 iter->stamp = m_gtk_model->stamp;
4012 iter->user_data = parent_node->GetChildren().Item( n );
4013
4014 return TRUE;
4015 }
4016 }
4017
4018 gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
4019 {
4020 if (m_wx_model->IsVirtualListModel())
4021 {
4022 return FALSE;
4023 }
4024 else
4025 {
4026 wxGtkTreeModelNode *node = FindParentNode( child );
4027 if (!node)
4028 return FALSE;
4029
4030 iter->stamp = m_gtk_model->stamp;
4031 iter->user_data = (gpointer) node->GetItem().GetID();
4032
4033 return TRUE;
4034 }
4035 }
4036
4037 // item can be deleted already in the model
4038 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item )
4039 {
4040 if (m_wx_model->IsVirtualListModel())
4041 {
4042 return wxPtrToUInt(item.GetID()) - 1;
4043 }
4044 else
4045 {
4046 wxGtkTreeModelNode *parent_node = FindNode( parent );
4047 wxGtkTreeModelChildren &children = parent_node->GetChildren();
4048 size_t j;
4049 for (j = 0; j < children.GetCount(); j++)
4050 {
4051 if (children[j] == item.GetID())
4052 return j;
4053 }
4054 }
4055 return -1;
4056 }
4057
4058
4059 static wxGtkTreeModelNode*
4060 wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
4061 {
4062 if( model == NULL )
4063 return NULL;
4064
4065 ItemList list;
4066 list.DeleteContents( true );
4067 wxDataViewItem it( item );
4068
4069 while( it.IsOk() )
4070 {
4071 wxDataViewItem * pItem = new wxDataViewItem( it );
4072 list.Insert( pItem );
4073 it = model->GetParent( it );
4074 }
4075
4076 wxGtkTreeModelNode * node = treeNode;
4077 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
4078 {
4079 if( node && node->GetNodes().GetCount() != 0 )
4080 {
4081 int len = node->GetNodes().GetCount();
4082 wxGtkTreeModelNodes &nodes = node->GetNodes();
4083 int j = 0;
4084 for( ; j < len; j ++)
4085 {
4086 if( nodes[j]->GetItem() == *(n->GetData()))
4087 {
4088 node = nodes[j];
4089 break;
4090 }
4091 }
4092
4093 if( j == len )
4094 {
4095 return NULL;
4096 }
4097 }
4098 else
4099 return NULL;
4100 }
4101 return node;
4102
4103 }
4104
4105 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
4106 {
4107 if (!iter)
4108 return m_root;
4109
4110 wxDataViewItem item( (void*) iter->user_data );
4111 if (!item.IsOk())
4112 return m_root;
4113
4114 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
4115
4116 /*
4117 if (!result)
4118 {
4119 wxLogDebug( "Not found %p", iter->user_data );
4120 char *crash = NULL;
4121 *crash = 0;
4122 }
4123 // TODO: remove this code
4124 */
4125
4126 return result;
4127 }
4128
4129 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
4130 {
4131 if (!item.IsOk())
4132 return m_root;
4133
4134 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
4135
4136 /*
4137 if (!result)
4138 {
4139 wxLogDebug( "Not found %p", item.GetID() );
4140 char *crash = NULL;
4141 *crash = 0;
4142 }
4143 // TODO: remove this code
4144 */
4145
4146 return result;
4147 }
4148
4149 static wxGtkTreeModelNode*
4150 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
4151 {
4152 if( model == NULL )
4153 return NULL;
4154
4155 ItemList list;
4156 list.DeleteContents( true );
4157 if( !item.IsOk() )
4158 return NULL;
4159
4160 wxDataViewItem it( model->GetParent( item ) );
4161 while( it.IsOk() )
4162 {
4163 wxDataViewItem * pItem = new wxDataViewItem( it );
4164 list.Insert( pItem );
4165 it = model->GetParent( it );
4166 }
4167
4168 wxGtkTreeModelNode * node = treeNode;
4169 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
4170 {
4171 if( node && node->GetNodes().GetCount() != 0 )
4172 {
4173 int len = node->GetNodes().GetCount();
4174 wxGtkTreeModelNodes nodes = node->GetNodes();
4175 int j = 0;
4176 for( ; j < len; j ++)
4177 {
4178 if( nodes[j]->GetItem() == *(n->GetData()))
4179 {
4180 node = nodes[j];
4181 break;
4182 }
4183 }
4184
4185 if( j == len )
4186 {
4187 return NULL;
4188 }
4189 }
4190 else
4191 return NULL;
4192 }
4193 //Examine whether the node is item's parent node
4194 int len = node->GetChildCount();
4195 for( int i = 0; i < len ; i ++ )
4196 {
4197 if( node->GetChildren().Item( i ) == item.GetID() )
4198 return node;
4199 }
4200 return NULL;
4201 }
4202
4203 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
4204 {
4205 if (!iter)
4206 return NULL;
4207
4208 wxDataViewItem item( (void*) iter->user_data );
4209 if (!item.IsOk())
4210 return NULL;
4211
4212 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
4213 }
4214
4215 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
4216 {
4217 if (!item.IsOk())
4218 return NULL;
4219
4220 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
4221 }
4222
4223 //-----------------------------------------------------------------------------
4224 // wxDataViewCtrl signal callbacks
4225 //-----------------------------------------------------------------------------
4226
4227 static void
4228 wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv )
4229 {
4230 if (!GTK_WIDGET_REALIZED(dv->m_widget))
4231 return;
4232
4233 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() );
4234 event.SetItem( dv->GetSelection() );
4235 event.SetModel( dv->GetModel() );
4236 dv->HandleWindowEvent( event );
4237 }
4238
4239 static void
4240 wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path,
4241 GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv )
4242 {
4243 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
4244
4245 wxDataViewItem item(dv->GTKPathToItem(path));
4246 event.SetItem( item );
4247 event.SetModel( dv->GetModel() );
4248 dv->HandleWindowEvent( event );
4249 }
4250
4251 static gboolean
4252 wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4253 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4254 {
4255 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
4256
4257 wxDataViewItem item( (void*) iter->user_data );;
4258 event.SetItem( item );
4259 event.SetModel( dv->GetModel() );
4260 dv->HandleWindowEvent( event );
4261
4262 return !event.IsAllowed();
4263 }
4264
4265 static void
4266 wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4267 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4268 {
4269 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
4270
4271 wxDataViewItem item( (void*) iter->user_data );;
4272 event.SetItem( item );
4273 event.SetModel( dv->GetModel() );
4274 dv->HandleWindowEvent( event );
4275 }
4276
4277 static gboolean
4278 wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4279 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4280 {
4281 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
4282
4283 wxDataViewItem item( (void*) iter->user_data );;
4284 event.SetItem( item );
4285 event.SetModel( dv->GetModel() );
4286 dv->HandleWindowEvent( event );
4287
4288 return !event.IsAllowed();
4289 }
4290
4291 static void
4292 wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4293 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4294 {
4295 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
4296
4297 wxDataViewItem item( (void*) iter->user_data );;
4298 event.SetItem( item );
4299 event.SetModel( dv->GetModel() );
4300 dv->HandleWindowEvent( event );
4301 }
4302
4303 //-----------------------------------------------------------------------------
4304 // wxDataViewCtrl
4305 //-----------------------------------------------------------------------------
4306
4307 void wxDataViewCtrl::AddChildGTK(wxWindowGTK* child)
4308 {
4309 GtkWidget* treeview = GtkGetTreeView();
4310
4311 // Insert widget in GtkTreeView
4312 if (GTK_WIDGET_REALIZED(treeview))
4313 gtk_widget_set_parent_window( child->m_widget,
4314 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
4315 gtk_widget_set_parent( child->m_widget, treeview );
4316 }
4317
4318 static
4319 void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
4320 GtkAllocation *WXUNUSED(gtk_alloc),
4321 wxDataViewCtrl *win )
4322 {
4323 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
4324 while (node)
4325 {
4326 wxWindow *child = node->GetData();
4327
4328 GtkRequisition req;
4329 gtk_widget_size_request( child->m_widget, &req );
4330
4331 GtkAllocation alloc;
4332 alloc.x = child->m_x;
4333 alloc.y = child->m_y;
4334 alloc.width = child->m_width;
4335 alloc.height = child->m_height;
4336 gtk_widget_size_allocate( child->m_widget, &alloc );
4337
4338 node = node->GetNext();
4339 }
4340 }
4341
4342
4343 //-----------------------------------------------------------------------------
4344 // "motion_notify_event"
4345 //-----------------------------------------------------------------------------
4346
4347 static gboolean
4348 gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget),
4349 GdkEventMotion *gdk_event,
4350 wxDataViewCtrl *dv )
4351 {
4352 if (gdk_event->is_hint)
4353 {
4354 int x = 0;
4355 int y = 0;
4356 GdkModifierType state;
4357 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
4358 gdk_event->x = x;
4359 gdk_event->y = y;
4360 }
4361
4362 wxGtkTreePath path;
4363 GtkTreeViewColumn *column = NULL;
4364 gint cell_x = 0;
4365 gint cell_y = 0;
4366 if (gtk_tree_view_get_path_at_pos(
4367 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4368 (int) gdk_event->x, (int) gdk_event->y,
4369 path.ByRef(),
4370 &column,
4371 &cell_x,
4372 &cell_y))
4373 {
4374 if (path)
4375 {
4376 GtkTreeIter iter;
4377 dv->GtkGetInternal()->get_iter( &iter, path );
4378 }
4379 }
4380
4381
4382 return FALSE;
4383 }
4384
4385 //-----------------------------------------------------------------------------
4386 // "button_press_event"
4387 //-----------------------------------------------------------------------------
4388
4389 static gboolean
4390 gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget),
4391 GdkEventButton *gdk_event,
4392 wxDataViewCtrl *dv )
4393 {
4394 if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS))
4395 {
4396 wxGtkTreePath path;
4397 GtkTreeViewColumn *column = NULL;
4398 gint cell_x = 0;
4399 gint cell_y = 0;
4400 if (gtk_tree_view_get_path_at_pos(
4401 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4402 (int) gdk_event->x, (int) gdk_event->y,
4403 path.ByRef(),
4404 &column,
4405 &cell_x,
4406 &cell_y))
4407 {
4408 if (path)
4409 {
4410 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() );
4411 event.SetItem(dv->GTKPathToItem(path));
4412 event.SetModel( dv->GetModel() );
4413 return dv->HandleWindowEvent( event );
4414 }
4415 }
4416 }
4417
4418 return FALSE;
4419 }
4420
4421 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4422
4423 wxDataViewCtrl::~wxDataViewCtrl()
4424 {
4425 m_cols.Clear();
4426
4427 delete m_internal;
4428 }
4429
4430 void wxDataViewCtrl::Init()
4431 {
4432 m_internal = NULL;
4433
4434 m_cols.DeleteContents( true );
4435 }
4436
4437 bool wxDataViewCtrl::Create(wxWindow *parent,
4438 wxWindowID id,
4439 const wxPoint& pos,
4440 const wxSize& size,
4441 long style,
4442 const wxValidator& validator,
4443 const wxString& name)
4444 {
4445 if (!PreCreation( parent, pos, size ) ||
4446 !CreateBase( parent, id, pos, size, style, validator, name ))
4447 {
4448 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4449 return false;
4450 }
4451
4452 m_widget = gtk_scrolled_window_new (NULL, NULL);
4453 g_object_ref(m_widget);
4454
4455 GTKScrolledWindowSetBorder(m_widget, style);
4456
4457 m_treeview = gtk_tree_view_new();
4458 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
4459
4460 g_signal_connect (m_treeview, "size_allocate",
4461 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
4462
4463 #ifdef __WXGTK26__
4464 if (!gtk_check_version(2,6,0))
4465 {
4466 bool fixed = (style & wxDV_VARIABLE_LINE_HEIGHT) == 0;
4467 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4468 }
4469 #endif
4470
4471 if (style & wxDV_MULTIPLE)
4472 {
4473 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4474 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
4475 }
4476
4477 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
4478
4479 #ifdef __WXGTK210__
4480 if (!gtk_check_version(2,10,0))
4481 {
4482 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
4483
4484 if ((style & wxDV_HORIZ_RULES) != 0 &&
4485 (style & wxDV_VERT_RULES) != 0)
4486 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
4487 else if (style & wxDV_VERT_RULES)
4488 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
4489 else if (style & wxDV_HORIZ_RULES)
4490 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
4491
4492 if (grid != GTK_TREE_VIEW_GRID_LINES_NONE)
4493 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
4494 }
4495 #endif
4496
4497 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 );
4498
4499 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
4500 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4501 gtk_widget_show (m_treeview);
4502
4503 m_parent->DoAddChild( this );
4504
4505 PostCreation(size);
4506
4507 GtkEnableSelectionEvents();
4508
4509 g_signal_connect_after (m_treeview, "row-activated",
4510 G_CALLBACK (wxdataview_row_activated_callback), this);
4511
4512 g_signal_connect (m_treeview, "test-collapse-row",
4513 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
4514
4515 g_signal_connect_after (m_treeview, "row-collapsed",
4516 G_CALLBACK (wxdataview_row_collapsed_callback), this);
4517
4518 g_signal_connect (m_treeview, "test-expand-row",
4519 G_CALLBACK (wxdataview_test_expand_row_callback), this);
4520
4521 g_signal_connect_after (m_treeview, "row-expanded",
4522 G_CALLBACK (wxdataview_row_expanded_callback), this);
4523
4524 g_signal_connect (m_treeview, "motion_notify_event",
4525 G_CALLBACK (gtk_dataview_motion_notify_callback), this);
4526
4527 g_signal_connect (m_treeview, "button_press_event",
4528 G_CALLBACK (gtk_dataview_button_press_callback), this);
4529
4530 return true;
4531 }
4532
4533 wxDataViewItem wxDataViewCtrl::GTKPathToItem(GtkTreePath *path) const
4534 {
4535 GtkTreeIter iter;
4536 return wxDataViewItem(path && m_internal->get_iter(&iter, path)
4537 ? iter.user_data
4538 : NULL);
4539 }
4540
4541 void wxDataViewCtrl::OnInternalIdle()
4542 {
4543 wxWindow::OnInternalIdle();
4544
4545 m_internal->OnInternalIdle();
4546
4547 unsigned int cols = GetColumnCount();
4548 unsigned int i;
4549 for (i = 0; i < cols; i++)
4550 {
4551 wxDataViewColumn *col = GetColumn( i );
4552 col->OnInternalIdle();
4553 }
4554
4555 if (m_ensureVisibleDefered.IsOk())
4556 {
4557 ExpandAncestors(m_ensureVisibleDefered);
4558 GtkTreeIter iter;
4559 iter.user_data = (gpointer) m_ensureVisibleDefered.GetID();
4560 wxGtkTreePath path(m_internal->get_path( &iter ));
4561 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4562 m_ensureVisibleDefered = wxDataViewItem(0);
4563 }
4564 }
4565
4566 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4567 {
4568 wxDELETE(m_internal);
4569
4570 if (!wxDataViewCtrlBase::AssociateModel( model ))
4571 return false;
4572
4573 #ifdef __WXGTK26__
4574 if (!gtk_check_version(2,6,0))
4575 {
4576 bool fixed = (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) == 0) || (model->IsVirtualListModel()));
4577 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4578 }
4579 #endif
4580
4581 m_internal = new wxDataViewCtrlInternal( this, model );
4582
4583 return true;
4584 }
4585
4586 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4587 {
4588 return m_internal->EnableDragSource( format );
4589 }
4590
4591 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4592 {
4593 return m_internal->EnableDropTarget( format );
4594 }
4595
4596 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4597 {
4598 if (!wxDataViewCtrlBase::AppendColumn(col))
4599 return false;
4600
4601 m_cols.Append( col );
4602
4603 #ifdef __WXGTK26__
4604 if (!gtk_check_version(2,6,0))
4605 {
4606 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4607 GTK_TREE_VIEW_COLUMN_FIXED)
4608 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4609 }
4610 #endif
4611
4612 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
4613 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4614
4615 return true;
4616 }
4617
4618 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4619 {
4620 if (!wxDataViewCtrlBase::PrependColumn(col))
4621 return false;
4622
4623 m_cols.Insert( col );
4624
4625 #ifdef __WXGTK26__
4626 if (!gtk_check_version(2,6,0))
4627 {
4628 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4629 GTK_TREE_VIEW_COLUMN_FIXED)
4630 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4631 }
4632 #endif
4633
4634 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4635 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 );
4636
4637 return true;
4638 }
4639
4640 bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4641 {
4642 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4643 return false;
4644
4645 m_cols.Insert( pos, col );
4646
4647 #ifdef __WXGTK26__
4648 if (!gtk_check_version(2,6,0))
4649 {
4650 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4651 GTK_TREE_VIEW_COLUMN_FIXED)
4652 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4653 }
4654 #endif
4655
4656 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4657 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), pos );
4658
4659 return true;
4660 }
4661
4662 unsigned int wxDataViewCtrl::GetColumnCount() const
4663 {
4664 return m_cols.GetCount();
4665 }
4666
4667 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
4668 {
4669 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
4670 if (!gtk_col)
4671 return NULL;
4672
4673 wxDataViewColumnList::const_iterator iter;
4674 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
4675 {
4676 wxDataViewColumn *col = *iter;
4677 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
4678 {
4679 return col;
4680 }
4681 }
4682
4683 return NULL;
4684 }
4685
4686 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4687 {
4688 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
4689 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
4690
4691 m_cols.DeleteObject( column );
4692
4693 return true;
4694 }
4695
4696 bool wxDataViewCtrl::ClearColumns()
4697 {
4698 wxDataViewColumnList::iterator iter;
4699 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
4700 {
4701 wxDataViewColumn *col = *iter;
4702 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
4703 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4704 }
4705
4706 m_cols.Clear();
4707
4708 return true;
4709 }
4710
4711 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4712 {
4713 GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetConstGtkHandle());
4714
4715 GList *list = gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview) );
4716
4717 gint pos = g_list_index( list, (gconstpointer) gtk_column );
4718
4719 g_list_free( list );
4720
4721 return pos;
4722 }
4723
4724 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4725 {
4726 return m_internal->GetDataViewSortColumn();
4727 }
4728
4729 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4730 {
4731 GtkTreeIter iter;
4732 iter.user_data = item.GetID();
4733 wxGtkTreePath path(m_internal->get_path( &iter ));
4734 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
4735 }
4736
4737 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4738 {
4739 GtkTreeIter iter;
4740 iter.user_data = item.GetID();
4741 wxGtkTreePath path(m_internal->get_path( &iter ));
4742 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
4743 }
4744
4745 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4746 {
4747 GtkTreeIter iter;
4748 iter.user_data = item.GetID();
4749 wxGtkTreePath path(m_internal->get_path( &iter ));
4750 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview), path );
4751 }
4752
4753 wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4754 {
4755 // The tree doesn't have any current item if it hadn't been created yet but
4756 // it's arguably not an error to call this function in this case so just
4757 // return an invalid item without asserting.
4758 if ( !m_treeview )
4759 return wxDataViewItem();
4760
4761 wxGtkTreePath path;
4762 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview), path.ByRef(), NULL);
4763
4764 return GTKPathToItem(path);
4765 }
4766
4767 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4768 {
4769 wxCHECK_RET( m_treeview,
4770 "Current item can't be set before creating the control." );
4771
4772 // We need to make sure the model knows about this item or the path would
4773 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4774 ExpandAncestors(item);
4775
4776 // We also need to preserve the existing selection from changing.
4777 // Unfortunately the only way to do it seems to use our own selection
4778 // function and forbid any selection changes during set cursor call.
4779 wxGtkTreeSelectionLock
4780 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview)));
4781
4782 // Do move the cursor now.
4783 GtkTreeIter iter;
4784 iter.user_data = item.GetID();
4785 wxGtkTreePath path(m_internal->get_path( &iter ));
4786
4787 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview), path, NULL, FALSE);
4788 }
4789
4790 wxDataViewItem wxDataViewCtrl::GetSelection() const
4791 {
4792 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4793
4794 if (m_windowStyle & wxDV_MULTIPLE)
4795 {
4796 // Report the first one
4797 GtkTreeModel *model;
4798 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
4799
4800 if (list)
4801 {
4802 GtkTreePath *path = (GtkTreePath*) list->data;
4803 wxDataViewItem item(GTKPathToItem(path));
4804
4805 // delete list
4806 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4807 g_list_free( list );
4808
4809 return item;
4810 }
4811 }
4812 else
4813 {
4814 GtkTreeIter iter;
4815 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
4816 {
4817 wxDataViewItem item( iter.user_data );
4818 return item;
4819 }
4820 }
4821
4822 return wxDataViewItem(0);
4823 }
4824
4825 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4826 {
4827 sel.Clear();
4828
4829 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4830 if (HasFlag(wxDV_MULTIPLE))
4831 {
4832 GtkTreeModel *model;
4833 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
4834
4835 int count = 0;
4836 while (list)
4837 {
4838 GtkTreePath *path = (GtkTreePath*) list->data;
4839
4840 sel.Add(GTKPathToItem(path));
4841
4842 list = g_list_next( list );
4843 count++;
4844 }
4845
4846 // delete list
4847 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4848 g_list_free( list );
4849
4850 return count;
4851 }
4852 else
4853 {
4854 GtkTreeModel *model;
4855 GtkTreeIter iter;
4856 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
4857 if (has_selection)
4858 {
4859 sel.Add( wxDataViewItem( (void*) iter.user_data) );
4860 return 1;
4861 }
4862 }
4863
4864 return 0;
4865 }
4866
4867 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4868 {
4869 GtkDisableSelectionEvents();
4870
4871 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4872
4873 gtk_tree_selection_unselect_all( selection );
4874
4875 wxDataViewItem last_parent;
4876
4877 size_t i;
4878 for (i = 0; i < sel.GetCount(); i++)
4879 {
4880 wxDataViewItem item = sel[i];
4881 wxDataViewItem parent = GetModel()->GetParent( item );
4882 if (parent)
4883 {
4884 if (parent != last_parent)
4885 ExpandAncestors(item);
4886 }
4887 last_parent = parent;
4888
4889 GtkTreeIter iter;
4890 iter.stamp = m_internal->GetGtkModel()->stamp;
4891 iter.user_data = (gpointer) item.GetID();
4892 gtk_tree_selection_select_iter( selection, &iter );
4893 }
4894
4895 GtkEnableSelectionEvents();
4896 }
4897
4898 void wxDataViewCtrl::Select( const wxDataViewItem & item )
4899 {
4900 ExpandAncestors(item);
4901
4902 GtkDisableSelectionEvents();
4903
4904 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4905
4906 GtkTreeIter iter;
4907 iter.stamp = m_internal->GetGtkModel()->stamp;
4908 iter.user_data = (gpointer) item.GetID();
4909 gtk_tree_selection_select_iter( selection, &iter );
4910
4911 GtkEnableSelectionEvents();
4912 }
4913
4914 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
4915 {
4916 GtkDisableSelectionEvents();
4917
4918 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4919
4920 GtkTreeIter iter;
4921 iter.stamp = m_internal->GetGtkModel()->stamp;
4922 iter.user_data = (gpointer) item.GetID();
4923 gtk_tree_selection_unselect_iter( selection, &iter );
4924
4925 GtkEnableSelectionEvents();
4926 }
4927
4928 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
4929 {
4930 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4931
4932 GtkTreeIter iter;
4933 iter.stamp = m_internal->GetGtkModel()->stamp;
4934 iter.user_data = (gpointer) item.GetID();
4935
4936 return gtk_tree_selection_iter_is_selected( selection, &iter );
4937 }
4938
4939 void wxDataViewCtrl::SelectAll()
4940 {
4941 GtkDisableSelectionEvents();
4942
4943 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4944
4945 gtk_tree_selection_select_all( selection );
4946
4947 GtkEnableSelectionEvents();
4948 }
4949
4950 void wxDataViewCtrl::UnselectAll()
4951 {
4952 GtkDisableSelectionEvents();
4953
4954 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4955
4956 gtk_tree_selection_unselect_all( selection );
4957
4958 GtkEnableSelectionEvents();
4959 }
4960
4961 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,
4962 const wxDataViewColumn *WXUNUSED(column))
4963 {
4964 m_ensureVisibleDefered = item;
4965 ExpandAncestors(item);
4966
4967 GtkTreeIter iter;
4968 iter.user_data = (gpointer) item.GetID();
4969 wxGtkTreePath path(m_internal->get_path( &iter ));
4970 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4971 }
4972
4973 void wxDataViewCtrl::HitTest(const wxPoint& point,
4974 wxDataViewItem& item,
4975 wxDataViewColumn *& column) const
4976 {
4977 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
4978 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
4979 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
4980 // So we have to use both of them.
4981 // Friedrich Haase 2010-9-20
4982 wxGtkTreePath path, pathScratch;
4983 GtkTreeViewColumn* GtkColumn = NULL;
4984 GtkTreeViewDropPosition pos = GTK_TREE_VIEW_DROP_INTO_OR_AFTER;
4985 gint cell_x = 0;
4986 gint cell_y = 0;
4987
4988 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
4989 wxDataViewCtrl* ctrl = (wxDataViewCtrl*)this; // ugly workaround, ctrl is NOT const
4990 GtkTreeView* treeView = GTK_TREE_VIEW(ctrl->GtkGetTreeView());
4991
4992 // is there possibly a better suited function to get the column?
4993 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
4994 treeView,
4995 (int) point.x, (int) point.y,
4996 pathScratch.ByRef(),
4997 &GtkColumn, // here we get the GtkColumn
4998 &cell_x,
4999 &cell_y );
5000
5001 if ( GtkColumn != NULL )
5002 {
5003 // we got GTK column
5004 // the right call now which takes the header into account
5005 gtk_tree_view_get_dest_row_at_pos( treeView, (int) point.x, (int) point.y, path.ByRef(), &pos);
5006
5007 if (path)
5008 item = wxDataViewItem(GTKPathToItem(path));
5009 // else we got a GTK column but the position is not over an item, e.g. below last item
5010 for ( unsigned int i=0, cols=GetColumnCount(); i<cols; ++i ) // search the wx column
5011 {
5012 wxDataViewColumn* col = GetColumn(i);
5013 if ( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == GtkColumn )
5014 {
5015 column = col; // here we get the wx column
5016 break;
5017 }
5018 }
5019 }
5020 // else no column and thus no item, both null
5021 }
5022
5023 wxRect
5024 wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item),
5025 const wxDataViewColumn *WXUNUSED(column)) const
5026 {
5027 return wxRect();
5028 }
5029
5030 void wxDataViewCtrl::DoSetExpanderColumn()
5031 {
5032 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
5033 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5034 }
5035
5036 void wxDataViewCtrl::DoSetIndent()
5037 {
5038 }
5039
5040 void wxDataViewCtrl::GtkDisableSelectionEvents()
5041 {
5042 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5043 g_signal_handlers_disconnect_by_func( selection,
5044 (gpointer) (wxdataview_selection_changed_callback), this);
5045 }
5046
5047 void wxDataViewCtrl::GtkEnableSelectionEvents()
5048 {
5049 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5050 g_signal_connect_after (selection, "changed",
5051 G_CALLBACK (wxdataview_selection_changed_callback), this);
5052 }
5053
5054 // ----------------------------------------------------------------------------
5055 // visual attributes stuff
5056 // ----------------------------------------------------------------------------
5057
5058 // static
5059 wxVisualAttributes
5060 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
5061 {
5062 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
5063 }
5064
5065 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
5066 {
5067 wxDataViewCtrlBase::DoApplyWidgetStyle(style);
5068 gtk_widget_modify_style(m_treeview, style);
5069 }
5070
5071 #endif // !wxUSE_GENERICDATAVIEWCTRL
5072
5073 #endif // wxUSE_DATAVIEWCTRL