]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
Correct bug in the wxSpinCtrlGeneric sub-controls resizing.
[wxWidgets.git] / src / gtk / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataview.cpp
3 // Purpose: wxDataViewCtrl GTK+2 implementation
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_DATAVIEWCTRL
14
15 #include "wx/dataview.h"
16
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
18
19 #ifndef WX_PRECOMP
20 #include "wx/log.h"
21 #include "wx/dcclient.h"
22 #include "wx/sizer.h"
23 #include "wx/settings.h"
24 #include "wx/crt.h"
25 #endif
26
27 #include "wx/stockitem.h"
28 #include "wx/calctrl.h"
29 #include "wx/popupwin.h"
30 #include "wx/listimpl.cpp"
31
32 #include "wx/gtk/private.h"
33 #include "wx/gtk/dc.h"
34 #include "wx/gtk/dcclient.h"
35
36 #include "wx/gtk/private/gdkconv.h"
37 using namespace wxGTKImpl;
38
39 class wxGtkDataViewModelNotifier;
40
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43
44 static wxDataViewCtrlInternal *gs_internal = NULL;
45
46 class wxGtkTreeModelNode;
47
48 extern "C" {
49 typedef struct _GtkWxTreeModel GtkWxTreeModel;
50 }
51
52 // ----------------------------------------------------------------------------
53 // wxGtkTreePath: self-destroying GtkTreePath
54 // ----------------------------------------------------------------------------
55
56 // Usually this object is initialized with the associated GtkTreePath
57 // immediately when it's constructed but it can also be changed later either by
58 // using Assign() or by getting the pointer to the internally stored pointer
59 // value using ByRef(). The latter should be avoided but is very convenient
60 // when using GTK functions with GtkTreePath output parameters.
61 class wxGtkTreePath
62 {
63 public:
64 // Ctor takes ownership of the given path and will free it if non-NULL.
65 wxGtkTreePath(GtkTreePath *path = NULL) : m_path(path) { }
66
67 // Creates a tree path for the given string path.
68 wxGtkTreePath(const gchar *strpath)
69 : m_path(gtk_tree_path_new_from_string(strpath))
70 {
71 }
72
73 // Set the stored pointer if not done by ctor.
74 void Assign(GtkTreePath *path)
75 {
76 wxASSERT_MSG( !m_path, "shouldn't be already initialized" );
77
78 m_path = path;
79 }
80
81 // Return the pointer to the internally stored pointer. This should only be
82 // used to initialize the object by passing it to some GTK function.
83 GtkTreePath **ByRef()
84 {
85 wxASSERT_MSG( !m_path, "shouldn't be already initialized" );
86
87 return &m_path;
88 }
89
90
91 operator GtkTreePath *() const { return m_path; }
92
93 ~wxGtkTreePath() { if ( m_path ) gtk_tree_path_free(m_path); }
94
95 private:
96 GtkTreePath *m_path;
97
98 wxDECLARE_NO_COPY_CLASS(wxGtkTreePath);
99 };
100
101 // ----------------------------------------------------------------------------
102 // wxGtkTreeSelectionLock: prevent selection from changing during the
103 // lifetime of this object
104 // ----------------------------------------------------------------------------
105
106 // Implementation note: it could be expected that setting the selection
107 // function in this class ctor and resetting it back to the old value in its
108 // dtor would work. However currently gtk_tree_selection_get_select_function()
109 // can't be passed NULL (see https://bugzilla.gnome.org/show_bug.cgi?id=626276)
110 // so we can't do this. Instead, we always use the selection function (which
111 // imposes extra overhead, albeit minimal one, on all selection operations) and
112 // just set/reset the flag telling it whether it should allow or forbid the
113 // selection.
114 //
115 // Also notice that currently only a single object of this class may exist at
116 // any given moment. It's just simpler like this and we don't need anything
117 // more for now.
118
119 extern "C"
120 gboolean wxdataview_selection_func(GtkTreeSelection * WXUNUSED(selection),
121 GtkTreeModel * WXUNUSED(model),
122 GtkTreePath * WXUNUSED(path),
123 gboolean WXUNUSED(path_currently_selected),
124 gpointer data)
125 {
126 return data == NULL;
127 }
128
129 class wxGtkTreeSelectionLock
130 {
131 public:
132 wxGtkTreeSelectionLock(GtkTreeSelection *selection)
133 : m_selection(selection)
134 {
135 wxASSERT_MSG( !ms_instance, "this class is not reentrant currently" );
136
137 ms_instance = this;
138
139 CheckCurrentSelectionFunc(NULL);
140
141 // Pass some non-NULL pointer as "data" for the callback, it doesn't
142 // matter what it is as long as it's non-NULL.
143 gtk_tree_selection_set_select_function(selection,
144 wxdataview_selection_func,
145 this,
146 NULL);
147 }
148
149 ~wxGtkTreeSelectionLock()
150 {
151 CheckCurrentSelectionFunc(wxdataview_selection_func);
152
153 gtk_tree_selection_set_select_function(m_selection,
154 wxdataview_selection_func,
155 NULL,
156 NULL);
157
158 ms_instance = NULL;
159 }
160
161 private:
162 void CheckCurrentSelectionFunc(GtkTreeSelectionFunc func)
163 {
164 // We can only use gtk_tree_selection_get_select_function() with 2.14+
165 // so check for its availability both during compile- and run-time.
166 #if GTK_CHECK_VERSION(2, 14, 0)
167 if ( gtk_check_version(2, 14, 0) != NULL )
168 return;
169
170 // If this assert is triggered, it means the code elsewhere has called
171 // gtk_tree_selection_set_select_function() but currently doing this
172 // breaks this class so the code here needs to be changed.
173 wxASSERT_MSG
174 (
175 gtk_tree_selection_get_select_function(m_selection) == func,
176 "selection function has changed unexpectedly, review this code!"
177 );
178 #endif // GTK+ 2.14+
179
180 wxUnusedVar(func);
181 }
182
183 static wxGtkTreeSelectionLock *ms_instance;
184
185 GtkTreeSelection * const m_selection;
186
187 wxDECLARE_NO_COPY_CLASS(wxGtkTreeSelectionLock);
188 };
189
190 wxGtkTreeSelectionLock *wxGtkTreeSelectionLock::ms_instance = NULL;
191
192 //-----------------------------------------------------------------------------
193 // wxDataViewCtrlInternal
194 //-----------------------------------------------------------------------------
195
196 WX_DECLARE_LIST(wxDataViewItem, ItemList);
197 WX_DEFINE_LIST(ItemList)
198
199 class wxDataViewCtrlInternal
200 {
201 public:
202 wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model );
203 ~wxDataViewCtrlInternal();
204
205 // model iface
206 GtkTreeModelFlags get_flags();
207 gboolean get_iter( GtkTreeIter *iter, GtkTreePath *path );
208 GtkTreePath *get_path( GtkTreeIter *iter);
209 gboolean iter_next( GtkTreeIter *iter );
210 gboolean iter_children( GtkTreeIter *iter, GtkTreeIter *parent);
211 gboolean iter_has_child( GtkTreeIter *iter );
212 gint iter_n_children( GtkTreeIter *iter );
213 gboolean iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n );
214 gboolean iter_parent( GtkTreeIter *iter, GtkTreeIter *child );
215
216 // dnd iface
217
218 bool EnableDragSource( const wxDataFormat &format );
219 bool EnableDropTarget( const wxDataFormat &format );
220
221 gboolean row_draggable( GtkTreeDragSource *drag_source, GtkTreePath *path );
222 gboolean drag_data_delete( GtkTreeDragSource *drag_source, GtkTreePath* path );
223 gboolean drag_data_get( GtkTreeDragSource *drag_source, GtkTreePath *path,
224 GtkSelectionData *selection_data );
225 gboolean drag_data_received( GtkTreeDragDest *drag_dest, GtkTreePath *dest,
226 GtkSelectionData *selection_data );
227 gboolean row_drop_possible( GtkTreeDragDest *drag_dest, GtkTreePath *dest_path,
228 GtkSelectionData *selection_data );
229
230 // notifactions from wxDataViewModel
231 bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
232 bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
233 bool ItemChanged( const wxDataViewItem &item );
234 bool ValueChanged( const wxDataViewItem &item, unsigned int model_column );
235 bool Cleared();
236 bool BeforeReset();
237 bool AfterReset();
238 void Resort();
239
240 // sorting interface
241 void SetSortOrder( GtkSortType sort_order ) { m_sort_order = sort_order; }
242 GtkSortType GetSortOrder() const { return m_sort_order; }
243
244 void SetSortColumn( int column ) { m_sort_column = column; }
245 int GetSortColumn() const { return m_sort_column; }
246
247 void SetDataViewSortColumn( wxDataViewColumn *column ) { m_dataview_sort_column = column; }
248 wxDataViewColumn *GetDataViewSortColumn() { return m_dataview_sort_column; }
249
250 bool IsSorted() { return (m_sort_column >= 0); }
251
252 // accessors
253 wxDataViewModel* GetDataViewModel() { return m_wx_model; }
254 const wxDataViewModel* GetDataViewModel() const { return m_wx_model; }
255 wxDataViewCtrl* GetOwner() { return m_owner; }
256 GtkWxTreeModel* GetGtkModel() { return m_gtk_model; }
257
258 // item can be deleted already in the model
259 int GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item );
260
261 virtual void OnInternalIdle();
262
263 protected:
264 void InitTree();
265 void ScheduleRefresh();
266
267 wxGtkTreeModelNode *FindNode( const wxDataViewItem &item );
268 wxGtkTreeModelNode *FindNode( GtkTreeIter *iter );
269 wxGtkTreeModelNode *FindParentNode( const wxDataViewItem &item );
270 wxGtkTreeModelNode *FindParentNode( GtkTreeIter *iter );
271 void BuildBranch( wxGtkTreeModelNode *branch );
272
273 private:
274 wxGtkTreeModelNode *m_root;
275 wxDataViewModel *m_wx_model;
276 GtkWxTreeModel *m_gtk_model;
277 wxDataViewCtrl *m_owner;
278 GtkSortType m_sort_order;
279 wxDataViewColumn *m_dataview_sort_column;
280 int m_sort_column;
281
282 GtkTargetEntry m_dragSourceTargetEntry;
283 wxCharBuffer m_dragSourceTargetEntryTarget;
284 wxDataObject *m_dragDataObject;
285
286 GtkTargetEntry m_dropTargetTargetEntry;
287 wxCharBuffer m_dropTargetTargetEntryTarget;
288 wxDataObject *m_dropDataObject;
289
290 wxGtkDataViewModelNotifier *m_notifier;
291
292 bool m_dirty;
293 };
294
295
296 //-----------------------------------------------------------------------------
297 // wxGtkTreeModelNode
298 //-----------------------------------------------------------------------------
299
300 static
301 int LINKAGEMODE wxGtkTreeModelChildCmp( void** id1, void** id2 )
302 {
303 int ret = gs_internal->GetDataViewModel()->Compare( *id1, *id2,
304 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
305
306 return ret;
307 }
308
309 WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode*, wxGtkTreeModelNodes );
310 WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren );
311
312 class wxGtkTreeModelNode
313 {
314 public:
315 wxGtkTreeModelNode( wxGtkTreeModelNode* parent, const wxDataViewItem &item,
316 wxDataViewCtrlInternal *internal )
317 {
318 m_parent = parent;
319 m_item = item;
320 m_internal = internal;
321 }
322
323 ~wxGtkTreeModelNode()
324 {
325 size_t count = m_nodes.GetCount();
326 size_t i;
327 for (i = 0; i < count; i++)
328 {
329 wxGtkTreeModelNode *child = m_nodes.Item( i );
330 delete child;
331 }
332 }
333
334 unsigned int AddNode( wxGtkTreeModelNode* child )
335 {
336 m_nodes.Add( child );
337
338 void *id = child->GetItem().GetID();
339
340 m_children.Add( id );
341
342 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
343 {
344 gs_internal = m_internal;
345 m_children.Sort( &wxGtkTreeModelChildCmp );
346 return m_children.Index( id );
347 }
348
349 return m_children.GetCount()-1;
350 }
351
352 unsigned int AddLeave( void* id )
353 {
354 m_children.Add( id );
355
356 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
357 {
358 gs_internal = m_internal;
359 m_children.Sort( &wxGtkTreeModelChildCmp );
360 return m_children.Index( id );
361 }
362
363 return m_children.GetCount()-1;
364 }
365
366 void DeleteChild( void* id )
367 {
368 m_children.Remove( id );
369
370 unsigned int count = m_nodes.GetCount();
371 unsigned int pos;
372 for (pos = 0; pos < count; pos++)
373 {
374 wxGtkTreeModelNode *node = m_nodes.Item( pos );
375 if (node->GetItem().GetID() == id)
376 {
377 m_nodes.RemoveAt( pos );
378 delete node;
379 break;
380 }
381 }
382 }
383
384 wxGtkTreeModelNode* GetParent()
385 { return m_parent; }
386 wxGtkTreeModelNodes &GetNodes()
387 { return m_nodes; }
388 wxGtkTreeModelChildren &GetChildren()
389 { return m_children; }
390
391 unsigned int GetChildCount() const { return m_children.GetCount(); }
392 unsigned int GetNodesCount() const { return m_nodes.GetCount(); }
393
394 wxDataViewItem &GetItem() { return m_item; }
395 wxDataViewCtrlInternal *GetInternal() { return m_internal; }
396
397 void Resort();
398
399 private:
400 wxGtkTreeModelNode *m_parent;
401 wxGtkTreeModelNodes m_nodes;
402 wxGtkTreeModelChildren m_children;
403 wxDataViewItem m_item;
404 wxDataViewCtrlInternal *m_internal;
405 };
406
407
408 //-----------------------------------------------------------------------------
409 // data
410 //-----------------------------------------------------------------------------
411
412 extern bool g_blockEventsOnDrag;
413
414 //-----------------------------------------------------------------------------
415 // define new GTK+ class wxGtkTreeModel
416 //-----------------------------------------------------------------------------
417
418 extern "C" {
419
420 #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
421 #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
422 #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
423 #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
424 #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
425 #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
426
427 GType gtk_wx_tree_model_get_type (void);
428
429 typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass;
430
431 struct _GtkWxTreeModel
432 {
433 GObject parent;
434
435 /*< private >*/
436 gint stamp;
437 wxDataViewCtrlInternal *internal;
438 };
439
440 struct _GtkWxTreeModelClass
441 {
442 GObjectClass list_parent_class;
443 };
444
445 static GtkWxTreeModel *wxgtk_tree_model_new (void);
446 static void wxgtk_tree_model_init (GtkWxTreeModel *tree_model);
447 static void wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass);
448
449 static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface);
450 static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface);
451 static void wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface);
452 static void wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface);
453
454 static void wxgtk_tree_model_finalize (GObject *object);
455 static GtkTreeModelFlags wxgtk_tree_model_get_flags (GtkTreeModel *tree_model);
456 static gint wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model);
457 static GType wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
458 gint index);
459 static gboolean wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
460 GtkTreeIter *iter,
461 GtkTreePath *path);
462 static GtkTreePath *wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
463 GtkTreeIter *iter);
464 static void wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
465 GtkTreeIter *iter,
466 gint column,
467 GValue *value);
468 static gboolean wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
469 GtkTreeIter *iter);
470 static gboolean wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
471 GtkTreeIter *iter,
472 GtkTreeIter *parent);
473 static gboolean wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
474 GtkTreeIter *iter);
475 static gint wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
476 GtkTreeIter *iter);
477 static gboolean wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
478 GtkTreeIter *iter,
479 GtkTreeIter *parent,
480 gint n);
481 static gboolean wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
482 GtkTreeIter *iter,
483 GtkTreeIter *child);
484
485 /* sortable */
486 static gboolean wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
487 gint *sort_column_id,
488 GtkSortType *order);
489 static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
490 gint sort_column_id,
491 GtkSortType order);
492 static void wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
493 gint sort_column_id,
494 GtkTreeIterCompareFunc func,
495 gpointer data,
496 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 model_column );
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_column )
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_column)
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_column );
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 m_mode = mode;
1735
1736 // we haven't changed them yet
1737 m_usingDefaultAttrs = true;
1738
1739 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1740 // after the m_renderer pointer has been initialized
1741 }
1742
1743 void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
1744 {
1745 gtk_tree_view_column_pack_end( column, m_renderer, TRUE /* expand */);
1746 }
1747
1748 void wxDataViewRenderer::GtkInitHandlers()
1749 {
1750 if (!gtk_check_version(2,6,0))
1751 {
1752 g_signal_connect (GTK_CELL_RENDERER(m_renderer), "editing_started",
1753 G_CALLBACK (wxgtk_renderer_editing_started),
1754 this);
1755 }
1756 }
1757
1758 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
1759 {
1760 GtkCellRendererMode gtkMode;
1761 switch (mode)
1762 {
1763 case wxDATAVIEW_CELL_INERT:
1764 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
1765 break;
1766
1767 case wxDATAVIEW_CELL_ACTIVATABLE:
1768 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
1769 break;
1770
1771 case wxDATAVIEW_CELL_EDITABLE:
1772 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
1773 break;
1774
1775 default:
1776 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1777 return;
1778 }
1779
1780 // This value is most often ignored in GtkTreeView
1781 GValue gvalue = { 0, };
1782 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1783 g_value_set_enum( &gvalue, gtkMode );
1784 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1785 g_value_unset( &gvalue );
1786 }
1787
1788 wxDataViewCellMode wxDataViewRenderer::GetMode() const
1789 {
1790 wxDataViewCellMode ret;
1791
1792 GValue gvalue;
1793 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1794
1795 switch (g_value_get_enum(&gvalue))
1796 {
1797 default:
1798 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1799 // fall through (we have to return something)
1800
1801 case GTK_CELL_RENDERER_MODE_INERT:
1802 ret = wxDATAVIEW_CELL_INERT;
1803 break;
1804
1805 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1806 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1807 break;
1808
1809 case GTK_CELL_RENDERER_MODE_EDITABLE:
1810 ret = wxDATAVIEW_CELL_EDITABLE;
1811 break;
1812 }
1813
1814 g_value_unset( &gvalue );
1815
1816 return ret;
1817 }
1818
1819 void wxDataViewRenderer::GtkUpdateAlignment()
1820 {
1821 int align = m_alignment;
1822
1823 // query alignment from column ?
1824 if (align == -1)
1825 {
1826 // None there yet
1827 if (GetOwner() == NULL)
1828 return;
1829
1830 align = GetOwner()->GetAlignment();
1831 align |= wxALIGN_CENTRE_VERTICAL;
1832 }
1833
1834 // horizontal alignment:
1835
1836 gfloat xalign = 0.0;
1837 if (align & wxALIGN_RIGHT)
1838 xalign = 1.0;
1839 else if (align & wxALIGN_CENTER_HORIZONTAL)
1840 xalign = 0.5;
1841
1842 GValue gvalue = { 0, };
1843 g_value_init( &gvalue, G_TYPE_FLOAT );
1844 g_value_set_float( &gvalue, xalign );
1845 g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue );
1846 g_value_unset( &gvalue );
1847
1848 // vertical alignment:
1849
1850 gfloat yalign = 0.0;
1851 if (align & wxALIGN_BOTTOM)
1852 yalign = 1.0;
1853 else if (align & wxALIGN_CENTER_VERTICAL)
1854 yalign = 0.5;
1855
1856 GValue gvalue2 = { 0, };
1857 g_value_init( &gvalue2, G_TYPE_FLOAT );
1858 g_value_set_float( &gvalue2, yalign );
1859 g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 );
1860 g_value_unset( &gvalue2 );
1861 }
1862
1863 void wxDataViewRenderer::SetAlignment( int align )
1864 {
1865 m_alignment = align;
1866 GtkUpdateAlignment();
1867 }
1868
1869 int wxDataViewRenderer::GetAlignment() const
1870 {
1871 return m_alignment;
1872 }
1873
1874 void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode)
1875 {
1876 #ifdef __WXGTK26__
1877 if ( gtk_check_version(2, 6, 0) != NULL )
1878 return;
1879
1880 GtkCellRendererText * const rend = GtkGetTextRenderer();
1881 if ( !rend )
1882 return;
1883
1884 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1885 // can just cast between them
1886 GValue gvalue = { 0, };
1887 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1888 g_value_set_enum( &gvalue, static_cast<PangoEllipsizeMode>(mode) );
1889 g_object_set_property( G_OBJECT(rend), "ellipsize", &gvalue );
1890 g_value_unset( &gvalue );
1891 #else // GTK < 2.6
1892 wxUnusedVar(mode);
1893 #endif // GTK 2.6/before
1894 }
1895
1896 wxEllipsizeMode wxDataViewRenderer::GetEllipsizeMode() const
1897 {
1898 #ifdef __WXGTK26__
1899 if ( gtk_check_version(2, 6, 0) != NULL )
1900 return wxELLIPSIZE_NONE;
1901
1902 GtkCellRendererText * const rend = GtkGetTextRenderer();
1903 if ( !rend )
1904 return wxELLIPSIZE_NONE;
1905
1906 GValue gvalue = { 0, };
1907 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1908 g_object_get_property( G_OBJECT(rend), "ellipsize", &gvalue );
1909 wxEllipsizeMode
1910 mode = static_cast<wxEllipsizeMode>(g_value_get_enum( &gvalue ));
1911 g_value_unset( &gvalue );
1912
1913 return mode;
1914 #else // GTK < 2.6
1915 return wxELLIPSIZE_NONE;
1916 #endif // GTK 2.6/before
1917 }
1918
1919 void
1920 wxDataViewRenderer::GtkOnTextEdited(const gchar *itempath, const wxString& str)
1921 {
1922 wxVariant value(str);
1923 if (!Validate( value ))
1924 return;
1925
1926 wxDataViewItem
1927 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
1928
1929 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
1930 }
1931
1932 void
1933 wxDataViewRenderer::GtkOnCellChanged(const wxVariant& value,
1934 const wxDataViewItem& item,
1935 unsigned col)
1936 {
1937 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
1938 model->ChangeValue( value, item, col );
1939 }
1940
1941 // ---------------------------------------------------------
1942 // wxDataViewTextRenderer
1943 // ---------------------------------------------------------
1944
1945 extern "C"
1946 {
1947
1948 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *WXUNUSED(renderer),
1949 gchar *arg1, gchar *arg2, gpointer user_data )
1950 {
1951 wxDataViewRenderer *cell = (wxDataViewRenderer*) user_data;
1952
1953 cell->GtkOnTextEdited(arg1, wxGTK_CONV_BACK_FONT(
1954 arg2, cell->GetOwner()->GetOwner()->GetFont()));
1955 }
1956
1957 }
1958
1959 namespace
1960 {
1961
1962 // helper function used by wxDataViewTextRenderer and
1963 // wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
1964 // given text renderer and returns true if anything was done
1965 bool GtkApplyAttr(GtkCellRendererText *renderer, const wxDataViewItemAttr& attr)
1966 {
1967 bool usingDefaultAttrs = true;
1968 if (attr.HasColour())
1969 {
1970 const GdkColor * const gcol = attr.GetColour().GetColor();
1971
1972 GValue gvalue = { 0, };
1973 g_value_init( &gvalue, GDK_TYPE_COLOR );
1974 g_value_set_boxed( &gvalue, gcol );
1975 g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
1976 g_value_unset( &gvalue );
1977
1978 usingDefaultAttrs = false;
1979 }
1980 else
1981 {
1982 GValue gvalue = { 0, };
1983 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1984 g_value_set_boolean( &gvalue, FALSE );
1985 g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
1986 g_value_unset( &gvalue );
1987 }
1988
1989 if (attr.GetItalic())
1990 {
1991 GValue gvalue = { 0, };
1992 g_value_init( &gvalue, PANGO_TYPE_STYLE );
1993 g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
1994 g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
1995 g_value_unset( &gvalue );
1996
1997 usingDefaultAttrs = false;
1998 }
1999 else
2000 {
2001 GValue gvalue = { 0, };
2002 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2003 g_value_set_boolean( &gvalue, FALSE );
2004 g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
2005 g_value_unset( &gvalue );
2006 }
2007
2008
2009 if (attr.GetBold())
2010 {
2011 GValue gvalue = { 0, };
2012 g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
2013 g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
2014 g_object_set_property( G_OBJECT(renderer), "weight", &gvalue );
2015 g_value_unset( &gvalue );
2016
2017 usingDefaultAttrs = false;
2018 }
2019 else
2020 {
2021 GValue gvalue = { 0, };
2022 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2023 g_value_set_boolean( &gvalue, FALSE );
2024 g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue );
2025 g_value_unset( &gvalue );
2026 }
2027
2028 #if 0
2029 if (attr.HasBackgroundColour())
2030 {
2031 wxColour colour = attr.GetBackgroundColour();
2032 const GdkColor * const gcol = colour.GetColor();
2033
2034 GValue gvalue = { 0, };
2035 g_value_init( &gvalue, GDK_TYPE_COLOR );
2036 g_value_set_boxed( &gvalue, gcol );
2037 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
2038 g_value_unset( &gvalue );
2039 }
2040 else
2041 {
2042 GValue gvalue = { 0, };
2043 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2044 g_value_set_boolean( &gvalue, FALSE );
2045 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
2046 g_value_unset( &gvalue );
2047 }
2048 #endif
2049
2050 return !usingDefaultAttrs;
2051 }
2052
2053 } // anonymous namespace
2054
2055 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
2056
2057 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2058 int align ) :
2059 wxDataViewRenderer( varianttype, mode, align )
2060 {
2061 GtkWxCellRendererText *text_renderer = gtk_wx_cell_renderer_text_new();
2062 text_renderer->wx_renderer = this;
2063 m_renderer = (GtkCellRenderer*) text_renderer;
2064
2065 if (mode & wxDATAVIEW_CELL_EDITABLE)
2066 {
2067 GValue gvalue = { 0, };
2068 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2069 g_value_set_boolean( &gvalue, true );
2070 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
2071 g_value_unset( &gvalue );
2072
2073 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2074
2075 GtkInitHandlers();
2076 }
2077
2078 SetMode(mode);
2079 SetAlignment(align);
2080 }
2081
2082 bool wxDataViewTextRenderer::SetTextValue(const wxString& str)
2083 {
2084 GValue gvalue = { 0, };
2085 g_value_init( &gvalue, G_TYPE_STRING );
2086 g_value_set_string( &gvalue, wxGTK_CONV_FONT( str, GetOwner()->GetOwner()->GetFont() ) );
2087 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2088 g_value_unset( &gvalue );
2089
2090 return true;
2091 }
2092
2093 bool wxDataViewTextRenderer::GetTextValue(wxString& str) const
2094 {
2095 GValue gvalue = { 0, };
2096 g_value_init( &gvalue, G_TYPE_STRING );
2097 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
2098 str = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), const_cast<wxDataViewTextRenderer*>(this)->GetOwner()->GetOwner()->GetFont() );
2099 g_value_unset( &gvalue );
2100
2101 return true;
2102 }
2103
2104 void wxDataViewTextRenderer::SetAlignment( int align )
2105 {
2106 wxDataViewRenderer::SetAlignment(align);
2107
2108 if (gtk_check_version(2,10,0))
2109 return;
2110
2111 // horizontal alignment:
2112 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2113 if (align & wxALIGN_RIGHT)
2114 pangoAlign = PANGO_ALIGN_RIGHT;
2115 else if (align & wxALIGN_CENTER_HORIZONTAL)
2116 pangoAlign = PANGO_ALIGN_CENTER;
2117
2118 GValue gvalue = { 0, };
2119 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2120 g_value_set_enum( &gvalue, pangoAlign );
2121 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2122 g_value_unset( &gvalue );
2123 }
2124
2125 bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr& attr)
2126 {
2127 return GtkApplyAttr(GtkGetTextRenderer(), attr);
2128 }
2129
2130 GtkCellRendererText *wxDataViewTextRenderer::GtkGetTextRenderer() const
2131 {
2132 return GTK_CELL_RENDERER_TEXT(m_renderer);
2133 }
2134
2135 // ---------------------------------------------------------
2136 // wxDataViewBitmapRenderer
2137 // ---------------------------------------------------------
2138
2139 namespace
2140 {
2141
2142 // set "pixbuf" property on the given renderer
2143 void SetPixbufProp(GtkCellRenderer *renderer, GdkPixbuf *pixbuf)
2144 {
2145 GValue gvalue = { 0, };
2146 g_value_init( &gvalue, G_TYPE_OBJECT );
2147 g_value_set_object( &gvalue, pixbuf );
2148 g_object_set_property( G_OBJECT(renderer), "pixbuf", &gvalue );
2149 g_value_unset( &gvalue );
2150 }
2151
2152 } // anonymous namespace
2153
2154 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
2155
2156 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2157 int align ) :
2158 wxDataViewRenderer( varianttype, mode, align )
2159 {
2160 m_renderer = gtk_cell_renderer_pixbuf_new();
2161
2162 SetMode(mode);
2163 SetAlignment(align);
2164 }
2165
2166 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
2167 {
2168 if (value.GetType() == wxT("wxBitmap"))
2169 {
2170 wxBitmap bitmap;
2171 bitmap << value;
2172
2173 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2174 // object (and it will stay there and remain owned by wxBitmap)
2175 SetPixbufProp(m_renderer, bitmap.GetPixbuf());
2176 }
2177 else if (value.GetType() == wxT("wxIcon"))
2178 {
2179 wxIcon icon;
2180 icon << value;
2181
2182 SetPixbufProp(m_renderer, icon.GetPixbuf());
2183 }
2184 else
2185 {
2186 return false;
2187 }
2188
2189 return true;
2190 }
2191
2192 bool wxDataViewBitmapRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2193 {
2194 return false;
2195 }
2196
2197 // ---------------------------------------------------------
2198 // wxDataViewToggleRenderer
2199 // ---------------------------------------------------------
2200
2201 extern "C" {
2202 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
2203 gchar *path, gpointer user_data );
2204 }
2205
2206 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
2207 gchar *path, gpointer user_data )
2208 {
2209 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
2210
2211 // get old value
2212 GValue gvalue = { 0, };
2213 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2214 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
2215 bool tmp = g_value_get_boolean( &gvalue );
2216 g_value_unset( &gvalue );
2217 // invert it
2218 tmp = !tmp;
2219
2220 wxVariant value = tmp;
2221 if (!cell->Validate( value ))
2222 return;
2223
2224 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
2225 wxDataViewModel *model = ctrl->GetModel();
2226
2227 wxDataViewItem item(ctrl->GTKPathToItem(wxGtkTreePath(path)));
2228
2229 unsigned int model_col = cell->GetOwner()->GetModelColumn();
2230
2231 model->ChangeValue( value, item, model_col );
2232 }
2233
2234 IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
2235
2236 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
2237 wxDataViewCellMode mode, int align ) :
2238 wxDataViewRenderer( varianttype, mode, align )
2239 {
2240 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
2241
2242 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
2243 {
2244 g_signal_connect_after( m_renderer, "toggled",
2245 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
2246 }
2247 else
2248 {
2249 GValue gvalue = { 0, };
2250 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2251 g_value_set_boolean( &gvalue, false );
2252 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
2253 g_value_unset( &gvalue );
2254 }
2255
2256 SetMode(mode);
2257 SetAlignment(align);
2258 }
2259
2260 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
2261 {
2262 bool tmp = value;
2263
2264 GValue gvalue = { 0, };
2265 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2266 g_value_set_boolean( &gvalue, tmp );
2267 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
2268 g_value_unset( &gvalue );
2269
2270 return true;
2271 }
2272
2273 bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
2274 {
2275 GValue gvalue = { 0, };
2276 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2277 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
2278 bool tmp = g_value_get_boolean( &gvalue );
2279 g_value_unset( &gvalue );
2280
2281 value = tmp;
2282
2283 return true;
2284 }
2285
2286 // ---------------------------------------------------------
2287 // wxDataViewCustomRenderer
2288 // ---------------------------------------------------------
2289
2290 class wxDataViewCtrlDCImpl: public wxWindowDCImpl
2291 {
2292 public:
2293 wxDataViewCtrlDCImpl( wxDC *owner, wxDataViewCtrl *window ) :
2294 wxWindowDCImpl( owner )
2295 {
2296 GtkWidget *widget = window->m_treeview;
2297 // Set later
2298 m_gdkwindow = NULL;
2299
2300 m_window = window;
2301
2302 m_context = window->GTKGetPangoDefaultContext();
2303 m_layout = pango_layout_new( m_context );
2304 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
2305
2306 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
2307
2308 // Set m_gdkwindow later
2309 // SetUpDC();
2310 }
2311 };
2312
2313 class wxDataViewCtrlDC: public wxWindowDC
2314 {
2315 public:
2316 wxDataViewCtrlDC( wxDataViewCtrl *window ) :
2317 wxWindowDC( new wxDataViewCtrlDCImpl( this, window ) )
2318 { }
2319 };
2320
2321
2322 // ---------------------------------------------------------
2323 // wxDataViewCustomRenderer
2324 // ---------------------------------------------------------
2325
2326 IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
2327
2328 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
2329 wxDataViewCellMode mode,
2330 int align,
2331 bool no_init )
2332 : wxDataViewCustomRendererBase( varianttype, mode, align )
2333 {
2334 m_dc = NULL;
2335 m_text_renderer = NULL;
2336
2337 if (no_init)
2338 m_renderer = NULL;
2339 else
2340 Init(mode, align);
2341 }
2342
2343 GtkCellRendererText *wxDataViewCustomRenderer::GtkGetTextRenderer() const
2344 {
2345 if ( !m_text_renderer )
2346 {
2347 // we create it on demand so need to do it even from a const function
2348 const_cast<wxDataViewCustomRenderer *>(this)->
2349 m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2350 }
2351
2352 return m_text_renderer;
2353 }
2354
2355 void wxDataViewCustomRenderer::RenderText( const wxString &text,
2356 int xoffset,
2357 wxRect cell,
2358 wxDC *WXUNUSED(dc),
2359 int WXUNUSED(state) )
2360 {
2361
2362 GtkCellRendererText * const textRenderer = GtkGetTextRenderer();
2363
2364 GValue gvalue = { 0, };
2365 g_value_init( &gvalue, G_TYPE_STRING );
2366 g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) );
2367 g_object_set_property( G_OBJECT(textRenderer), "text", &gvalue );
2368 g_value_unset( &gvalue );
2369
2370 GtkApplyAttr(textRenderer, GetAttr());
2371
2372 GdkRectangle cell_area;
2373 wxRectToGDKRect(cell, cell_area);
2374 cell_area.x += xoffset;
2375 cell_area.width -= xoffset;
2376
2377 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer),
2378 m_renderParams.window,
2379 m_renderParams.widget,
2380 m_renderParams.background_area,
2381 &cell_area,
2382 m_renderParams.expose_area,
2383 (GtkCellRendererState) m_renderParams.flags );
2384 }
2385
2386 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
2387 {
2388 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
2389 renderer->cell = this;
2390
2391 m_renderer = (GtkCellRenderer*) renderer;
2392
2393 SetMode(mode);
2394 SetAlignment(align);
2395
2396 GtkInitHandlers();
2397
2398 return true;
2399 }
2400
2401 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
2402 {
2403 if (m_dc)
2404 delete m_dc;
2405
2406 if (m_text_renderer)
2407 gtk_object_sink( GTK_OBJECT(m_text_renderer) );
2408 }
2409
2410 wxDC *wxDataViewCustomRenderer::GetDC()
2411 {
2412 if (m_dc == NULL)
2413 {
2414 if (GetOwner() == NULL)
2415 return NULL;
2416 if (GetOwner()->GetOwner() == NULL)
2417 return NULL;
2418 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
2419 }
2420
2421 return m_dc;
2422 }
2423
2424 // ---------------------------------------------------------
2425 // wxDataViewProgressRenderer
2426 // ---------------------------------------------------------
2427
2428 IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
2429
2430 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
2431 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
2432 wxDataViewCustomRenderer( varianttype, mode, align, true )
2433 {
2434 m_label = label;
2435 m_value = 0;
2436
2437 #ifdef __WXGTK26__
2438 if (!gtk_check_version(2,6,0))
2439 {
2440 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
2441
2442 GValue gvalue = { 0, };
2443 g_value_init( &gvalue, G_TYPE_STRING );
2444
2445 g_value_set_string( &gvalue, wxGTK_CONV_FONT( m_label, GetOwner()->GetOwner()->GetFont() ) );
2446 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2447 g_value_unset( &gvalue );
2448
2449 SetMode(mode);
2450 SetAlignment(align);
2451 }
2452 else
2453 #endif
2454 {
2455 // Use custom cell code
2456 wxDataViewCustomRenderer::Init(mode, align);
2457 }
2458 }
2459
2460 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
2461 {
2462 }
2463
2464 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
2465 {
2466 #ifdef __WXGTK26__
2467 if (!gtk_check_version(2,6,0))
2468 {
2469 gint tmp = (long) value;
2470 GValue gvalue = { 0, };
2471 g_value_init( &gvalue, G_TYPE_INT );
2472 g_value_set_int( &gvalue, tmp );
2473 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
2474 g_value_unset( &gvalue );
2475 }
2476 else
2477 #endif
2478 {
2479 m_value = (long) value;
2480
2481 if (m_value < 0) m_value = 0;
2482 if (m_value > 100) m_value = 100;
2483 }
2484
2485 return true;
2486 }
2487
2488 bool wxDataViewProgressRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2489 {
2490 return false;
2491 }
2492
2493 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
2494 {
2495 double pct = (double)m_value / 100.0;
2496 wxRect bar = cell;
2497 bar.width = (int)(cell.width * pct);
2498 dc->SetPen( *wxTRANSPARENT_PEN );
2499 dc->SetBrush( *wxBLUE_BRUSH );
2500 dc->DrawRectangle( bar );
2501
2502 dc->SetBrush( *wxTRANSPARENT_BRUSH );
2503 dc->SetPen( *wxBLACK_PEN );
2504 dc->DrawRectangle( cell );
2505
2506 return true;
2507 }
2508
2509 wxSize wxDataViewProgressRenderer::GetSize() const
2510 {
2511 return wxSize(40,12);
2512 }
2513
2514 // -------------------------------------
2515 // wxDataViewChoiceRenderer
2516 // -------------------------------------
2517
2518 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString &choices,
2519 wxDataViewCellMode mode, int alignment ) :
2520 wxDataViewCustomRenderer( "string", mode, alignment, true )
2521 {
2522 m_choices = choices;
2523
2524 #ifdef __WXGTK26__
2525 if (!gtk_check_version(2,6,0))
2526 {
2527 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_combo_new();
2528
2529 GtkListStore *store = gtk_list_store_new( 1, G_TYPE_STRING );
2530 for (size_t n = 0; n < m_choices.GetCount(); n++)
2531 {
2532 gtk_list_store_insert_with_values(
2533 store, NULL, n, 0,
2534 static_cast<const char *>(m_choices[n].utf8_str()), -1 );
2535 }
2536
2537 g_object_set (m_renderer,
2538 "model", store,
2539 "text-column", 0,
2540 "has-entry", FALSE,
2541 NULL);
2542
2543 bool editable = (mode & wxDATAVIEW_CELL_EDITABLE);
2544 g_object_set (m_renderer, "editable", editable, NULL);
2545
2546 SetAlignment(alignment);
2547
2548 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2549
2550 GtkInitHandlers();
2551 }
2552 else
2553 #endif
2554 {
2555 // Use custom cell code
2556 wxDataViewCustomRenderer::Init(mode, alignment);
2557 }
2558 }
2559
2560 bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
2561 {
2562 RenderText( m_data, 0, rect, dc, state );
2563 return true;
2564 }
2565
2566 wxSize wxDataViewChoiceRenderer::GetSize() const
2567 {
2568 return wxSize(70,20);
2569 }
2570
2571 bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
2572 {
2573
2574 #ifdef __WXGTK26__
2575 if (!gtk_check_version(2,6,0))
2576 {
2577 GValue gvalue = { 0, };
2578 g_value_init( &gvalue, G_TYPE_STRING );
2579 g_value_set_string(&gvalue,
2580 wxGTK_CONV_FONT(value.GetString(),
2581 GetOwner()->GetOwner()->GetFont()));
2582 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2583 g_value_unset( &gvalue );
2584 }
2585 else
2586 #endif
2587 m_data = value.GetString();
2588
2589 return true;
2590 }
2591
2592 bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
2593 {
2594 #ifdef __WXGTK26__
2595 if (!gtk_check_version(2,6,0))
2596 {
2597 GValue gvalue = { 0, };
2598 g_value_init( &gvalue, G_TYPE_STRING );
2599 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
2600 wxString temp = wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue),
2601 GetOwner()->GetOwner()->GetFont());
2602 g_value_unset( &gvalue );
2603 value = temp;
2604
2605 //wxPrintf( "temp %s\n", temp );
2606 // TODO: remove this code
2607 }
2608 else
2609 #endif
2610 value = m_data;
2611
2612 return true;
2613 }
2614
2615 void wxDataViewChoiceRenderer::SetAlignment( int align )
2616 {
2617 wxDataViewCustomRenderer::SetAlignment(align);
2618
2619 if (gtk_check_version(2,10,0))
2620 return;
2621
2622 // horizontal alignment:
2623 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2624 if (align & wxALIGN_RIGHT)
2625 pangoAlign = PANGO_ALIGN_RIGHT;
2626 else if (align & wxALIGN_CENTER_HORIZONTAL)
2627 pangoAlign = PANGO_ALIGN_CENTER;
2628
2629 GValue gvalue = { 0, };
2630 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2631 g_value_set_enum( &gvalue, pangoAlign );
2632 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2633 g_value_unset( &gvalue );
2634 }
2635
2636 // ----------------------------------------------------------------------------
2637 // wxDataViewChoiceByIndexRenderer
2638 // ----------------------------------------------------------------------------
2639
2640 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
2641 wxDataViewCellMode mode, int alignment ) :
2642 wxDataViewChoiceRenderer( choices, mode, alignment )
2643 {
2644 }
2645
2646 void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const gchar *itempath, const wxString& str)
2647 {
2648 wxVariant value( (long) GetChoices().Index( str ) );
2649
2650 if (!Validate( value ))
2651 return;
2652
2653 wxDataViewItem
2654 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
2655
2656 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
2657 }
2658
2659 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
2660 {
2661 wxVariant string_value = GetChoice( value.GetLong() );
2662 return wxDataViewChoiceRenderer::SetValue( string_value );
2663 }
2664
2665 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const
2666 {
2667 wxVariant string_value;
2668 if (!wxDataViewChoiceRenderer::GetValue( string_value ))
2669 return false;
2670
2671 value = (long) GetChoices().Index( string_value.GetString() );
2672 return true;
2673 }
2674
2675 // ---------------------------------------------------------
2676 // wxDataViewDateRenderer
2677 // ---------------------------------------------------------
2678
2679 class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
2680 {
2681 public:
2682 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
2683 wxDataViewModel *model, const wxDataViewItem &item, unsigned int col ) :
2684 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
2685 {
2686 m_model = model;
2687 m_item = item;
2688 m_col = col;
2689 m_cal = new wxCalendarCtrl( this, -1, *value );
2690 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
2691 sizer->Add( m_cal, 1, wxGROW );
2692 SetSizer( sizer );
2693 sizer->Fit( this );
2694 }
2695
2696 virtual void OnDismiss()
2697 {
2698 }
2699
2700 void OnCalendar( wxCalendarEvent &event );
2701
2702 wxCalendarCtrl *m_cal;
2703 wxDataViewModel *m_model;
2704 wxDataViewItem m_item;
2705 unsigned int m_col;
2706
2707 private:
2708 DECLARE_EVENT_TABLE()
2709 };
2710
2711 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
2712 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
2713 END_EVENT_TABLE()
2714
2715 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
2716 {
2717 m_model->ChangeValue( event.GetDate(), m_item, m_col );
2718 DismissAndNotify();
2719 }
2720
2721 IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
2722
2723 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
2724 wxDataViewCellMode mode, int align ) :
2725 wxDataViewCustomRenderer( varianttype, mode, align )
2726 {
2727 SetMode(mode);
2728 SetAlignment(align);
2729 }
2730
2731 bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
2732 {
2733 m_date = value.GetDateTime();
2734
2735 return true;
2736 }
2737
2738 bool wxDataViewDateRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
2739 {
2740 return false;
2741 }
2742
2743 bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
2744 {
2745 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
2746 wxString tmp = m_date.FormatDate();
2747 RenderText( tmp, 0, cell, dc, state );
2748 return true;
2749 }
2750
2751 wxSize wxDataViewDateRenderer::GetSize() const
2752 {
2753 wxString tmp = m_date.FormatDate();
2754 wxCoord x,y,d;
2755 GetView()->GetTextExtent( tmp, &x, &y, &d );
2756 return wxSize(x,y+d);
2757 }
2758
2759 bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model,
2760 const wxDataViewItem &item, unsigned int col )
2761 {
2762 wxVariant variant;
2763 model->GetValue( variant, item, col );
2764 wxDateTime value = variant.GetDateTime();
2765
2766 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
2767 GetOwner()->GetOwner()->GetParent(), &value, model, item, col );
2768 wxPoint pos = wxGetMousePosition();
2769 popup->Move( pos );
2770 popup->Layout();
2771 popup->Popup( popup->m_cal );
2772
2773 return true;
2774 }
2775
2776
2777 // ---------------------------------------------------------
2778 // wxDataViewIconTextRenderer
2779 // ---------------------------------------------------------
2780
2781 IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
2782
2783 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2784 (
2785 const wxString &varianttype,
2786 wxDataViewCellMode mode,
2787 int align
2788 )
2789 : wxDataViewTextRenderer(varianttype, mode, align)
2790 {
2791 m_rendererIcon = gtk_cell_renderer_pixbuf_new();
2792 }
2793
2794 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2795 {
2796 }
2797
2798 void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
2799 {
2800 // add the icon renderer first
2801 gtk_tree_view_column_pack_start(column, m_rendererIcon, FALSE /* !expand */);
2802
2803 // add the text renderer too
2804 wxDataViewRenderer::GtkPackIntoColumn(column);
2805 }
2806
2807 bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
2808 {
2809 m_value << value;
2810
2811 SetTextValue(m_value.GetText());
2812 SetPixbufProp(m_rendererIcon, m_value.GetIcon().GetPixbuf());
2813
2814 return true;
2815 }
2816
2817 bool wxDataViewIconTextRenderer::GetValue(wxVariant& value) const
2818 {
2819 wxString str;
2820 if ( !GetTextValue(str) )
2821 return false;
2822
2823 // user doesn't have any way to edit the icon so leave it unchanged
2824 value << wxDataViewIconText(str, m_value.GetIcon());
2825
2826 return true;
2827 }
2828
2829 void
2830 wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant& value,
2831 const wxDataViewItem& item,
2832 unsigned col)
2833 {
2834 // we receive just the text part of our value as it's the only one which
2835 // can be edited, but we need the full wxDataViewIconText value for the
2836 // model
2837 wxVariant valueIconText;
2838 valueIconText << wxDataViewIconText(value.GetString(), m_value.GetIcon());
2839 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText, item, col);
2840 }
2841
2842 // ---------------------------------------------------------
2843 // wxDataViewColumn
2844 // ---------------------------------------------------------
2845
2846
2847 static gboolean
2848 gtk_dataview_header_button_press_callback( GtkWidget *WXUNUSED(widget),
2849 GdkEventButton *gdk_event,
2850 wxDataViewColumn *column )
2851 {
2852 if (gdk_event->type != GDK_BUTTON_PRESS)
2853 return FALSE;
2854
2855 if (gdk_event->button == 1)
2856 {
2857 gs_lastLeftClickHeader = column;
2858
2859 wxDataViewCtrl *dv = column->GetOwner();
2860 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
2861 event.SetDataViewColumn( column );
2862 event.SetModel( dv->GetModel() );
2863 if (dv->HandleWindowEvent( event ))
2864 return FALSE;
2865 }
2866
2867 if (gdk_event->button == 3)
2868 {
2869 wxDataViewCtrl *dv = column->GetOwner();
2870 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, dv->GetId() );
2871 event.SetDataViewColumn( column );
2872 event.SetModel( dv->GetModel() );
2873 if (dv->HandleWindowEvent( event ))
2874 return FALSE;
2875 }
2876
2877 return FALSE;
2878 }
2879
2880 extern "C"
2881 {
2882
2883 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *WXUNUSED(column),
2884 GtkCellRenderer *renderer,
2885 GtkTreeModel *model,
2886 GtkTreeIter *iter,
2887 gpointer data )
2888 {
2889 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
2890 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
2891
2892 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
2893
2894 wxDataViewItem item( (void*) iter->user_data );
2895
2896 wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel();
2897
2898 if (!wx_model->IsVirtualListModel())
2899 {
2900 gboolean visible;
2901 if (wx_model->IsContainer( item ))
2902 {
2903 visible = wx_model->HasContainerColumns( item ) ||
2904 (cell->GetOwner()->GetModelColumn() == 0);
2905 }
2906 else
2907 {
2908 visible = true;
2909 }
2910
2911 GValue gvalue = { 0, };
2912 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2913 g_value_set_boolean( &gvalue, visible );
2914 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2915 g_value_unset( &gvalue );
2916
2917 if ( !visible )
2918 return;
2919 }
2920
2921 wxVariant value;
2922 wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
2923
2924 if (value.GetType() != cell->GetVariantType())
2925 {
2926 wxLogError( wxT("Wrong type, required: %s but: %s"),
2927 value.GetType().c_str(),
2928 cell->GetVariantType().c_str() );
2929 }
2930
2931 cell->SetValue( value );
2932
2933 // deal with disabled items
2934 bool enabled = wx_model->IsEnabled( item, cell->GetOwner()->GetModelColumn() );
2935
2936 // a) this sets the appearance to disabled grey
2937 GValue gvalue = { 0, };
2938 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2939 g_value_set_boolean( &gvalue, enabled );
2940 g_object_set_property( G_OBJECT(renderer), "sensitive", &gvalue );
2941 g_value_unset( &gvalue );
2942
2943 // b) this actually disables the control/renderer
2944 if (enabled)
2945 cell->SetMode( cell->GtkGetMode() );
2946 else
2947 cell->SetMode( wxDATAVIEW_CELL_INERT );
2948
2949
2950 // deal with attributes: if the renderer doesn't support them at all, we
2951 // don't even need to query the model for them
2952 if ( !cell->GtkSupportsAttrs() )
2953 return;
2954
2955 // it can support attributes so check if this item has any
2956 wxDataViewItemAttr attr;
2957 if ( wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr )
2958 || !cell->GtkIsUsingDefaultAttrs() )
2959 {
2960 bool usingDefaultAttrs = !cell->GtkSetAttr(attr);
2961 cell->GtkSetUsingDefaultAttrs(usingDefaultAttrs);
2962 }
2963 // else: no custom attributes specified and we're already using the default
2964 // ones -- nothing to do
2965
2966 }
2967
2968 } // extern "C"
2969
2970 #include <wx/listimpl.cpp>
2971 WX_DEFINE_LIST(wxDataViewColumnList)
2972
2973 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
2974 unsigned int model_column, int width,
2975 wxAlignment align, int flags )
2976 : wxDataViewColumnBase( cell, model_column )
2977 {
2978 Init( align, flags, width );
2979
2980 SetTitle( title );
2981 }
2982
2983 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
2984 unsigned int model_column, int width,
2985 wxAlignment align, int flags )
2986 : wxDataViewColumnBase( bitmap, cell, model_column )
2987 {
2988 Init( align, flags, width );
2989
2990 SetBitmap( bitmap );
2991 }
2992
2993 void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
2994 {
2995 m_isConnected = false;
2996
2997 GtkTreeViewColumn *column = gtk_tree_view_column_new();
2998 m_column = (GtkWidget*) column;
2999
3000 SetFlags( flags );
3001 SetAlignment( align );
3002
3003 SetWidth( width );
3004
3005 // Create container for icon and label
3006 GtkWidget *box = gtk_hbox_new( FALSE, 1 );
3007 gtk_widget_show( box );
3008 // gtk_container_set_border_width((GtkContainer*)box, 2);
3009 m_image = gtk_image_new();
3010 gtk_box_pack_start(GTK_BOX(box), m_image, FALSE, FALSE, 1);
3011 m_label = gtk_label_new("");
3012 gtk_box_pack_end( GTK_BOX(box), GTK_WIDGET(m_label), FALSE, FALSE, 1 );
3013 gtk_tree_view_column_set_widget( column, box );
3014
3015 wxDataViewRenderer * const colRenderer = GetRenderer();
3016 GtkCellRenderer * const cellRenderer = colRenderer->GetGtkHandle();
3017
3018 colRenderer->GtkPackIntoColumn(column);
3019
3020 gtk_tree_view_column_set_cell_data_func( column, cellRenderer,
3021 wxGtkTreeCellDataFunc, (gpointer) colRenderer, NULL );
3022 }
3023
3024 void wxDataViewColumn::OnInternalIdle()
3025 {
3026 if (m_isConnected)
3027 return;
3028
3029 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
3030 {
3031 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3032 if (column->button)
3033 {
3034 g_signal_connect(column->button, "button_press_event",
3035 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
3036
3037 // otherwise the event will be blocked by GTK+
3038 gtk_tree_view_column_set_clickable( column, TRUE );
3039
3040 m_isConnected = true;
3041 }
3042 }
3043 }
3044
3045 void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
3046 {
3047 wxDataViewColumnBase::SetOwner( owner );
3048
3049 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3050
3051 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
3052 }
3053
3054 void wxDataViewColumn::SetTitle( const wxString &title )
3055 {
3056 wxDataViewCtrl *ctrl = GetOwner();
3057 gtk_label_set_text( GTK_LABEL(m_label), ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
3058 : wxGTK_CONV_SYS(title) );
3059 if (title.empty())
3060 gtk_widget_hide( m_label );
3061 else
3062 gtk_widget_show( m_label );
3063 }
3064
3065 wxString wxDataViewColumn::GetTitle() const
3066 {
3067 return wxGTK_CONV_BACK_FONT(
3068 gtk_label_get_text( GTK_LABEL(m_label) ),
3069 GetOwner()->GetFont()
3070 );
3071 }
3072
3073 void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
3074 {
3075 wxDataViewColumnBase::SetBitmap( bitmap );
3076
3077 if (bitmap.Ok())
3078 {
3079 GtkImage *gtk_image = GTK_IMAGE(m_image);
3080
3081 GdkBitmap *mask = NULL;
3082 if (bitmap.GetMask())
3083 mask = bitmap.GetMask()->GetBitmap();
3084
3085 if (bitmap.HasPixbuf())
3086 {
3087 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
3088 bitmap.GetPixbuf());
3089 }
3090 else
3091 {
3092 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
3093 bitmap.GetPixmap(), mask);
3094 }
3095 gtk_widget_show( m_image );
3096 }
3097 else
3098 {
3099 gtk_widget_hide( m_image );
3100 }
3101 }
3102
3103 void wxDataViewColumn::SetHidden( bool hidden )
3104 {
3105 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
3106 }
3107
3108 void wxDataViewColumn::SetResizeable( bool resizeable )
3109 {
3110 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
3111 }
3112
3113 void wxDataViewColumn::SetAlignment( wxAlignment align )
3114 {
3115 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3116
3117 gfloat xalign = 0.0;
3118 if (align == wxALIGN_RIGHT)
3119 xalign = 1.0;
3120 if (align == wxALIGN_CENTER_HORIZONTAL ||
3121 align == wxALIGN_CENTER)
3122 xalign = 0.5;
3123
3124 gtk_tree_view_column_set_alignment( column, xalign );
3125
3126 if (m_renderer && m_renderer->GetAlignment() == -1)
3127 m_renderer->GtkUpdateAlignment();
3128 }
3129
3130 wxAlignment wxDataViewColumn::GetAlignment() const
3131 {
3132 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
3133
3134 if (xalign == 1.0)
3135 return wxALIGN_RIGHT;
3136 if (xalign == 0.5)
3137 return wxALIGN_CENTER_HORIZONTAL;
3138
3139 return wxALIGN_LEFT;
3140 }
3141
3142 void wxDataViewColumn::SetSortable( bool sortable )
3143 {
3144 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3145
3146 if ( sortable )
3147 {
3148 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
3149 }
3150 else
3151 {
3152 gtk_tree_view_column_set_sort_column_id( column, -1 );
3153 gtk_tree_view_column_set_sort_indicator( column, FALSE );
3154 gtk_tree_view_column_set_clickable( column, FALSE );
3155 }
3156 }
3157
3158 bool wxDataViewColumn::IsSortable() const
3159 {
3160 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3161 return gtk_tree_view_column_get_clickable( column );
3162 }
3163
3164 void wxDataViewColumn::SetAsSortKey( bool WXUNUSED(sort) )
3165 {
3166 // it might not make sense to have this function in wxHeaderColumn at
3167 // all in fact, changing of the sort order should only be done using the
3168 // associated control API
3169 wxFAIL_MSG( "not implemented" );
3170 }
3171
3172 bool wxDataViewColumn::IsSortKey() const
3173 {
3174 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3175 return gtk_tree_view_column_get_sort_indicator( column );
3176 }
3177
3178 bool wxDataViewColumn::IsResizeable() const
3179 {
3180 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3181 return gtk_tree_view_column_get_resizable( column );
3182 }
3183
3184 bool wxDataViewColumn::IsHidden() const
3185 {
3186 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3187 return !gtk_tree_view_column_get_visible( column );
3188 }
3189
3190 void wxDataViewColumn::SetSortOrder( bool ascending )
3191 {
3192 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3193
3194 if (ascending)
3195 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
3196 else
3197 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
3198
3199 gtk_tree_view_column_set_sort_indicator( column, TRUE );
3200 }
3201
3202 bool wxDataViewColumn::IsSortOrderAscending() const
3203 {
3204 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3205
3206 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
3207 }
3208
3209 void wxDataViewColumn::SetMinWidth( int width )
3210 {
3211 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3212 }
3213
3214 int wxDataViewColumn::GetMinWidth() const
3215 {
3216 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
3217 }
3218
3219 int wxDataViewColumn::GetWidth() const
3220 {
3221 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
3222 }
3223
3224 void wxDataViewColumn::SetWidth( int width )
3225 {
3226 if ( width == wxCOL_WIDTH_AUTOSIZE )
3227 {
3228 // NB: this disables user resizing
3229 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
3230 }
3231 else
3232 {
3233 if ( width == wxCOL_WIDTH_DEFAULT )
3234 {
3235 // TODO find a better calculation
3236 width = wxDVC_DEFAULT_WIDTH;
3237 }
3238
3239 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
3240 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3241 }
3242 }
3243
3244 void wxDataViewColumn::SetReorderable( bool reorderable )
3245 {
3246 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
3247 }
3248
3249 bool wxDataViewColumn::IsReorderable() const
3250 {
3251 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) );
3252 }
3253
3254 //-----------------------------------------------------------------------------
3255 // wxGtkTreeModelNode
3256 //-----------------------------------------------------------------------------
3257
3258 #if 0
3259 class wxGtkTreeModelChildWithPos
3260 {
3261 public:
3262 unsigned int pos;
3263 void *id;
3264 };
3265
3266 static
3267 int wxGtkTreeModelChildWithPosCmp( const void* data1, const void* data2, const void* user_data )
3268 {
3269 const wxGtkTreeModelChildWithPos* child1 = (const wxGtkTreeModelChildWithPos*) data1;
3270 const wxGtkTreeModelChildWithPos* child2 = (const wxGtkTreeModelChildWithPos*) data2;
3271 const wxDataViewCtrlInternal *internal = (const wxDataViewCtrlInternal *) user_data;
3272 int ret = internal->GetDataViewModel()->Compare( child1->id, child2->id,
3273 internal->GetSortColumn(), (internal->GetSortOrder() == GTK_SORT_DESCENDING) );
3274
3275 return ret;
3276 }
3277 #else
3278 static
3279 int LINKAGEMODE wxGtkTreeModelChildPtrCmp( void*** data1, void*** data2 )
3280 {
3281 return gs_internal->GetDataViewModel()->Compare( **data1, **data2,
3282 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
3283 }
3284
3285 WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr );
3286 #endif
3287
3288 void wxGtkTreeModelNode::Resort()
3289 {
3290 size_t child_count = GetChildCount();
3291 if (child_count == 0)
3292 return;
3293
3294 size_t node_count = GetNodesCount();
3295
3296 if (child_count == 1)
3297 {
3298 if (node_count == 1)
3299 {
3300 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
3301 node->Resort();
3302 }
3303 return;
3304 }
3305
3306 gint *new_order = new gint[child_count];
3307
3308 #if 1
3309 // m_children has the original *void
3310 // ptrs points to these
3311 wxGtkTreeModelChildrenPtr ptrs;
3312 size_t i;
3313 for (i = 0; i < child_count; i++)
3314 ptrs.Add( &(m_children[i]) );
3315 // Sort the ptrs
3316 gs_internal = m_internal;
3317 ptrs.Sort( &wxGtkTreeModelChildPtrCmp );
3318
3319 wxGtkTreeModelChildren temp;
3320 void** base_ptr = &(m_children[0]);
3321 // Transfer positions to new_order array and
3322 // IDs to temp
3323 for (i = 0; i < child_count; i++)
3324 {
3325 new_order[i] = ptrs[i] - base_ptr;
3326 temp.Add( *ptrs[i] );
3327 }
3328
3329 // Transfer IDs back to m_children
3330 m_children.Clear();
3331 WX_APPEND_ARRAY( temp, m_children );
3332 #endif
3333 #if 0
3334 // Too slow
3335
3336 // Build up array with IDs and original positions
3337 wxGtkTreeModelChildWithPos* temp = new wxGtkTreeModelChildWithPos[child_count];
3338 size_t i;
3339 for (i = 0; i < child_count; i++)
3340 {
3341 temp[i].pos = i;
3342 temp[i].id = m_children[i];
3343 }
3344 // Sort array keeping original positions
3345 wxQsort( temp, child_count, sizeof(wxGtkTreeModelChildWithPos),
3346 &wxGtkTreeModelChildWithPosCmp, m_internal );
3347 // Transfer positions to new_order array and
3348 // IDs to m_children
3349 m_children.Clear();
3350 for (i = 0; i < child_count; i++)
3351 {
3352 new_order[i] = temp[i].pos;
3353 m_children.Add( temp[i].id );
3354 }
3355 // Delete array
3356 delete [] temp;
3357 #endif
3358
3359 #if 0
3360 // Too slow
3361
3362 wxGtkTreeModelChildren temp;
3363 WX_APPEND_ARRAY( temp, m_children );
3364
3365 gs_internal = m_internal;
3366 m_children.Sort( &wxGtkTreeModelChildCmp );
3367
3368 unsigned int pos;
3369 for (pos = 0; pos < child_count; pos++)
3370 {
3371 void *id = m_children.Item( pos );
3372 int old_pos = temp.Index( id );
3373 new_order[pos] = old_pos;
3374 }
3375 #endif
3376
3377 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
3378
3379 GtkTreeIter iter;
3380 iter.user_data = GetItem().GetID();
3381 iter.stamp = m_internal->GetGtkModel()->stamp;
3382
3383 gtk_tree_model_rows_reordered( gtk_tree_model,
3384 wxGtkTreePath(m_internal->get_path(&iter)), &iter, new_order );
3385
3386 delete [] new_order;
3387
3388 unsigned int pos;
3389 for (pos = 0; pos < node_count; pos++)
3390 {
3391 wxGtkTreeModelNode *node = m_nodes.Item( pos );
3392 node->Resort();
3393 }
3394 }
3395
3396 //-----------------------------------------------------------------------------
3397 // wxDataViewCtrlInternal
3398 //-----------------------------------------------------------------------------
3399
3400 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model )
3401 {
3402 m_owner = owner;
3403 m_wx_model = wx_model;
3404
3405 m_gtk_model = NULL;
3406 m_root = NULL;
3407 m_sort_order = GTK_SORT_ASCENDING;
3408 m_sort_column = -1;
3409 m_dataview_sort_column = NULL;
3410
3411 m_dragDataObject = NULL;
3412 m_dropDataObject = NULL;
3413
3414 m_dirty = false;
3415
3416 m_gtk_model = wxgtk_tree_model_new();
3417 m_gtk_model->internal = this;
3418
3419 m_notifier = new wxGtkDataViewModelNotifier( wx_model, this );
3420
3421 wx_model->AddNotifier( m_notifier );
3422
3423 // g_object_unref( gtk_model ); ???
3424
3425 if (!m_wx_model->IsVirtualListModel())
3426 InitTree();
3427
3428 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model) );
3429 }
3430
3431 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3432 {
3433 m_wx_model->RemoveNotifier( m_notifier );
3434
3435 // remove the model from the GtkTreeView before it gets destroyed
3436 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner->GtkGetTreeView() ), NULL );
3437
3438 g_object_unref( m_gtk_model );
3439
3440 delete m_dragDataObject;
3441 delete m_dropDataObject;
3442 }
3443
3444 void wxDataViewCtrlInternal::ScheduleRefresh()
3445 {
3446 m_dirty = true;
3447 }
3448
3449 void wxDataViewCtrlInternal::OnInternalIdle()
3450 {
3451 if (m_dirty)
3452 {
3453 GtkWidget *widget = m_owner->GtkGetTreeView();
3454 gtk_widget_queue_draw( widget );
3455 m_dirty = false;
3456 }
3457 }
3458
3459 void wxDataViewCtrlInternal::InitTree()
3460 {
3461 wxDataViewItem item;
3462 m_root = new wxGtkTreeModelNode( NULL, item, this );
3463
3464 BuildBranch( m_root );
3465 }
3466
3467 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
3468 {
3469 if (node->GetChildCount() == 0)
3470 {
3471 wxDataViewItemArray children;
3472 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
3473
3474 unsigned int pos;
3475 for (pos = 0; pos < count; pos++)
3476 {
3477 wxDataViewItem child = children[pos];
3478
3479 if (m_wx_model->IsContainer( child ))
3480 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
3481 else
3482 node->AddLeave( child.GetID() );
3483
3484 // Don't send any events here
3485 }
3486 }
3487 }
3488
3489 // GTK+ dnd iface
3490
3491 bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format )
3492 {
3493 wxGtkString atom_str( gdk_atom_name( format ) );
3494 m_dragSourceTargetEntryTarget = wxCharBuffer( atom_str );
3495
3496 m_dragSourceTargetEntry.target = m_dragSourceTargetEntryTarget.data();
3497 m_dragSourceTargetEntry.flags = 0;
3498 m_dragSourceTargetEntry.info = static_cast<guint>(-1);
3499
3500 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3501 GDK_BUTTON1_MASK, &m_dragSourceTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
3502
3503 return true;
3504 }
3505
3506 bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat &format )
3507 {
3508 wxGtkString atom_str( gdk_atom_name( format ) );
3509 m_dropTargetTargetEntryTarget = wxCharBuffer( atom_str );
3510
3511 m_dropTargetTargetEntry.target = m_dropTargetTargetEntryTarget.data();
3512 m_dropTargetTargetEntry.flags = 0;
3513 m_dropTargetTargetEntry.info = static_cast<guint>(-1);
3514
3515 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3516 &m_dropTargetTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
3517
3518 return true;
3519 }
3520
3521 gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
3522 GtkTreePath *path )
3523 {
3524 delete m_dragDataObject;
3525
3526 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3527 if ( !item )
3528 return FALSE;
3529
3530 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
3531 event.SetEventObject( m_owner );
3532 event.SetItem( item );
3533 event.SetModel( m_wx_model );
3534 gint x, y;
3535 gtk_widget_get_pointer(m_owner->GtkGetTreeView(), &x, &y);
3536 event.SetPosition(x, y);
3537 if (!m_owner->HandleWindowEvent( event ))
3538 return FALSE;
3539
3540 if (!event.IsAllowed())
3541 return FALSE;
3542
3543 wxDataObject *obj = event.GetDataObject();
3544 if (!obj)
3545 return FALSE;
3546
3547 m_dragDataObject = obj;
3548
3549 return TRUE;
3550 }
3551
3552 gboolean
3553 wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
3554 GtkTreePath *WXUNUSED(path))
3555 {
3556 return FALSE;
3557 }
3558
3559 gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
3560 GtkTreePath *path, GtkSelectionData *selection_data )
3561 {
3562 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3563 if ( !item )
3564 return FALSE;
3565
3566 if (!m_dragDataObject->IsSupported( selection_data->target ))
3567 return FALSE;
3568
3569 size_t size = m_dragDataObject->GetDataSize( selection_data->target );
3570 if (size == 0)
3571 return FALSE;
3572
3573 void *buf = malloc( size );
3574
3575 gboolean res = FALSE;
3576 if (m_dragDataObject->GetDataHere( selection_data->target, buf ))
3577 {
3578 res = TRUE;
3579
3580 gtk_selection_data_set( selection_data, selection_data->target,
3581 8, (const guchar*) buf, size );
3582 }
3583
3584 free( buf );
3585
3586 return res;
3587 }
3588
3589 gboolean
3590 wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
3591 GtkTreePath *path,
3592 GtkSelectionData *selection_data)
3593 {
3594 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3595 if ( !item )
3596 return FALSE;
3597
3598 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() );
3599 event.SetEventObject( m_owner );
3600 event.SetItem( item );
3601 event.SetModel( m_wx_model );
3602 event.SetDataFormat( selection_data->target );
3603 event.SetDataSize( selection_data->length );
3604 event.SetDataBuffer( selection_data->data );
3605 if (!m_owner->HandleWindowEvent( event ))
3606 return FALSE;
3607
3608 if (!event.IsAllowed())
3609 return FALSE;
3610
3611 return TRUE;
3612 }
3613
3614 gboolean
3615 wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
3616 GtkTreePath *path,
3617 GtkSelectionData *selection_data)
3618 {
3619 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3620 if ( !item )
3621 return FALSE;
3622
3623 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
3624 event.SetEventObject( m_owner );
3625 event.SetItem( item );
3626 event.SetModel( m_wx_model );
3627 event.SetDataFormat( selection_data->target );
3628 if (!m_owner->HandleWindowEvent( event ))
3629 return FALSE;
3630
3631 if (!event.IsAllowed())
3632 return FALSE;
3633
3634 return TRUE;
3635 }
3636
3637 // notifications from wxDataViewModel
3638
3639 bool wxDataViewCtrlInternal::Cleared()
3640 {
3641 if (m_root)
3642 {
3643 delete m_root;
3644 m_root = NULL;
3645 }
3646
3647 InitTree();
3648
3649 ScheduleRefresh();
3650
3651 return true;
3652 }
3653
3654 void wxDataViewCtrlInternal::Resort()
3655 {
3656 if (!m_wx_model->IsVirtualListModel())
3657 m_root->Resort();
3658
3659 ScheduleRefresh();
3660 }
3661
3662 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
3663 {
3664 if (!m_wx_model->IsVirtualListModel())
3665 {
3666 wxGtkTreeModelNode *parent_node = FindNode( parent );
3667 wxASSERT_MSG(parent_node,
3668 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3669
3670 if (m_wx_model->IsContainer( item ))
3671 parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) );
3672 else
3673 parent_node->AddLeave( item.GetID() );
3674 }
3675
3676 ScheduleRefresh();
3677
3678 return true;
3679 }
3680
3681 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
3682 {
3683 if (!m_wx_model->IsVirtualListModel())
3684 {
3685 wxGtkTreeModelNode *parent_node = FindNode( parent );
3686 wxASSERT_MSG(parent_node,
3687 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3688
3689 parent_node->DeleteChild( item.GetID() );
3690 }
3691
3692 ScheduleRefresh();
3693
3694 return true;
3695 }
3696
3697 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
3698 {
3699 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
3700 event.SetEventObject( m_owner );
3701 event.SetModel( m_owner->GetModel() );
3702 event.SetItem( item );
3703 m_owner->HandleWindowEvent( event );
3704
3705 return true;
3706 }
3707
3708 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int view_column )
3709 {
3710 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
3711 event.SetEventObject( m_owner );
3712 event.SetModel( m_owner->GetModel() );
3713 event.SetColumn( view_column );
3714 event.SetDataViewColumn( GetOwner()->GetColumn(view_column) );
3715 event.SetItem( item );
3716 m_owner->HandleWindowEvent( event );
3717
3718 return true;
3719 }
3720
3721 // GTK+ model iface
3722
3723 GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
3724 {
3725 int flags = 0;
3726
3727 if ( m_wx_model->IsListModel() )
3728 flags |= GTK_TREE_MODEL_LIST_ONLY;
3729
3730 if ( !m_wx_model->IsVirtualListModel() )
3731 flags |= GTK_TREE_MODEL_ITERS_PERSIST;
3732
3733 return GtkTreeModelFlags(flags);
3734 }
3735
3736 gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
3737 {
3738
3739 if (m_wx_model->IsVirtualListModel())
3740 {
3741 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3742
3743 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
3744
3745 if (i >= wx_model->GetCount())
3746 return FALSE;
3747
3748 iter->stamp = m_gtk_model->stamp;
3749 // user_data is just the index +1
3750 iter->user_data = (gpointer) (i+1);
3751
3752 return TRUE;
3753 }
3754 else
3755 {
3756 int depth = gtk_tree_path_get_depth( path );
3757
3758 wxGtkTreeModelNode *node = m_root;
3759
3760 int i;
3761 for (i = 0; i < depth; i++)
3762 {
3763 BuildBranch( node );
3764
3765 gint pos = gtk_tree_path_get_indices (path)[i];
3766 if (pos < 0) return FALSE;
3767 if ((size_t)pos >= node->GetChildCount()) return FALSE;
3768
3769 void* id = node->GetChildren().Item( (size_t) pos );
3770
3771 if (i == depth-1)
3772 {
3773 iter->stamp = m_gtk_model->stamp;
3774 iter->user_data = id;
3775 return TRUE;
3776 }
3777
3778 size_t count = node->GetNodes().GetCount();
3779 size_t pos2;
3780 for (pos2 = 0; pos2 < count; pos2++)
3781 {
3782 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
3783 if (child_node->GetItem().GetID() == id)
3784 {
3785 node = child_node;
3786 break;
3787 }
3788 }
3789 }
3790 }
3791
3792 return FALSE;
3793 }
3794
3795 GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
3796 {
3797 // When this is called from ItemDeleted(), the item is already
3798 // deleted in the model.
3799
3800 GtkTreePath *retval = gtk_tree_path_new ();
3801
3802 if (m_wx_model->IsVirtualListModel())
3803 {
3804 // iter is root, add nothing
3805 if (!iter->user_data)
3806 return retval;
3807
3808 // user_data is just the index +1
3809 int i = ( (wxUIntPtr) iter->user_data ) -1;
3810 gtk_tree_path_append_index (retval, i);
3811 }
3812 else
3813 {
3814 void *id = iter->user_data;
3815
3816 wxGtkTreeModelNode *node = FindParentNode( iter );
3817 while (node)
3818 {
3819 int pos = node->GetChildren().Index( id );
3820
3821 gtk_tree_path_prepend_index( retval, pos );
3822
3823 id = node->GetItem().GetID();
3824 node = node->GetParent();
3825 }
3826 }
3827
3828 return retval;
3829 }
3830
3831 gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
3832 {
3833 if (m_wx_model->IsVirtualListModel())
3834 {
3835 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3836
3837 // user_data is just the index +1
3838 int n = ( (wxUIntPtr) iter->user_data ) -1;
3839
3840 if (n == -1)
3841 {
3842 iter->user_data = NULL;
3843 return FALSE;
3844 }
3845
3846 if (n >= (int) wx_model->GetCount()-1)
3847 {
3848 iter->user_data = NULL;
3849 return FALSE;
3850 }
3851
3852 // user_data is just the index +1 (+2 because we need the next)
3853 iter->user_data = (gpointer) (n+2);
3854 }
3855 else
3856 {
3857 wxGtkTreeModelNode *parent = FindParentNode( iter );
3858 if( parent == NULL )
3859 {
3860 iter->user_data = NULL;
3861 return FALSE;
3862 }
3863
3864 int pos = parent->GetChildren().Index( iter->user_data );
3865
3866 if (pos == (int) parent->GetChildCount()-1)
3867 {
3868 iter->user_data = NULL;
3869 return FALSE;
3870 }
3871
3872 iter->user_data = parent->GetChildren().Item( pos+1 );
3873 }
3874
3875 return TRUE;
3876 }
3877
3878 gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
3879 {
3880 if (m_wx_model->IsVirtualListModel())
3881 {
3882 // this is a list, nodes have no children
3883 if (parent)
3884 return FALSE;
3885
3886 iter->stamp = m_gtk_model->stamp;
3887 iter->user_data = (gpointer) 1;
3888
3889 return TRUE;
3890 }
3891 else
3892 {
3893 if (iter == NULL)
3894 {
3895 if (m_root->GetChildCount() == 0) return FALSE;
3896 iter->stamp = m_gtk_model->stamp;
3897 iter->user_data = (gpointer) m_root->GetChildren().Item( 0 );
3898 return TRUE;
3899 }
3900
3901
3902 wxDataViewItem item;
3903 if (parent)
3904 item = wxDataViewItem( (void*) parent->user_data );
3905
3906 if (!m_wx_model->IsContainer( item ))
3907 return FALSE;
3908
3909 wxGtkTreeModelNode *parent_node = FindNode( parent );
3910 wxASSERT_MSG(parent_node,
3911 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3912
3913 BuildBranch( parent_node );
3914
3915 if (parent_node->GetChildCount() == 0)
3916 return FALSE;
3917
3918 iter->stamp = m_gtk_model->stamp;
3919 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
3920 }
3921
3922 return TRUE;
3923 }
3924
3925 gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
3926 {
3927 if (m_wx_model->IsVirtualListModel())
3928 {
3929 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3930
3931 if (iter == NULL)
3932 return (wx_model->GetCount() > 0);
3933
3934 // this is a list, nodes have no children
3935 return FALSE;
3936 }
3937 else
3938 {
3939 if (iter == NULL)
3940 return (m_root->GetChildCount() > 0);
3941
3942 wxDataViewItem item( (void*) iter->user_data );
3943
3944 bool is_container = m_wx_model->IsContainer( item );
3945
3946 if (!is_container)
3947 return FALSE;
3948
3949 wxGtkTreeModelNode *node = FindNode( iter );
3950 wxASSERT_MSG(node,
3951 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3952
3953 BuildBranch( node );
3954
3955 return (node->GetChildCount() > 0);
3956 }
3957 }
3958
3959 gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
3960 {
3961 if (m_wx_model->IsVirtualListModel())
3962 {
3963 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3964
3965 if (iter == NULL)
3966 return (gint) wx_model->GetCount();
3967
3968 return 0;
3969 }
3970 else
3971 {
3972 if (iter == NULL)
3973 return m_root->GetChildCount();
3974
3975 wxDataViewItem item( (void*) iter->user_data );
3976
3977 if (!m_wx_model->IsContainer( item ))
3978 return 0;
3979
3980 wxGtkTreeModelNode *parent_node = FindNode( iter );
3981 wxASSERT_MSG(parent_node,
3982 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3983
3984 BuildBranch( parent_node );
3985
3986 return parent_node->GetChildCount();
3987 }
3988 }
3989
3990 gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
3991 {
3992 if (m_wx_model->IsVirtualListModel())
3993 {
3994 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3995
3996 if (parent)
3997 return FALSE;
3998
3999 if (n < 0)
4000 return FALSE;
4001
4002 if (n >= (gint) wx_model->GetCount())
4003 return FALSE;
4004
4005 iter->stamp = m_gtk_model->stamp;
4006 // user_data is just the index +1
4007 iter->user_data = (gpointer) (n+1);
4008
4009 return TRUE;
4010 }
4011 else
4012 {
4013 void* id = NULL;
4014 if (parent) id = (void*) parent->user_data;
4015 wxDataViewItem item( id );
4016
4017 if (!m_wx_model->IsContainer( item ))
4018 return FALSE;
4019
4020 wxGtkTreeModelNode *parent_node = FindNode( parent );
4021 wxASSERT_MSG(parent_node,
4022 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4023
4024 BuildBranch( parent_node );
4025
4026 iter->stamp = m_gtk_model->stamp;
4027 iter->user_data = parent_node->GetChildren().Item( n );
4028
4029 return TRUE;
4030 }
4031 }
4032
4033 gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
4034 {
4035 if (m_wx_model->IsVirtualListModel())
4036 {
4037 return FALSE;
4038 }
4039 else
4040 {
4041 wxGtkTreeModelNode *node = FindParentNode( child );
4042 if (!node)
4043 return FALSE;
4044
4045 iter->stamp = m_gtk_model->stamp;
4046 iter->user_data = (gpointer) node->GetItem().GetID();
4047
4048 return TRUE;
4049 }
4050 }
4051
4052 // item can be deleted already in the model
4053 int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item )
4054 {
4055 if (m_wx_model->IsVirtualListModel())
4056 {
4057 return wxPtrToUInt(item.GetID()) - 1;
4058 }
4059 else
4060 {
4061 wxGtkTreeModelNode *parent_node = FindNode( parent );
4062 wxGtkTreeModelChildren &children = parent_node->GetChildren();
4063 size_t j;
4064 for (j = 0; j < children.GetCount(); j++)
4065 {
4066 if (children[j] == item.GetID())
4067 return j;
4068 }
4069 }
4070 return -1;
4071 }
4072
4073
4074 static wxGtkTreeModelNode*
4075 wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
4076 {
4077 if( model == NULL )
4078 return NULL;
4079
4080 ItemList list;
4081 list.DeleteContents( true );
4082 wxDataViewItem it( item );
4083
4084 while( it.IsOk() )
4085 {
4086 wxDataViewItem * pItem = new wxDataViewItem( it );
4087 list.Insert( pItem );
4088 it = model->GetParent( it );
4089 }
4090
4091 wxGtkTreeModelNode * node = treeNode;
4092 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
4093 {
4094 if( node && node->GetNodes().GetCount() != 0 )
4095 {
4096 int len = node->GetNodes().GetCount();
4097 wxGtkTreeModelNodes &nodes = node->GetNodes();
4098 int j = 0;
4099 for( ; j < len; j ++)
4100 {
4101 if( nodes[j]->GetItem() == *(n->GetData()))
4102 {
4103 node = nodes[j];
4104 break;
4105 }
4106 }
4107
4108 if( j == len )
4109 {
4110 return NULL;
4111 }
4112 }
4113 else
4114 return NULL;
4115 }
4116 return node;
4117
4118 }
4119
4120 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
4121 {
4122 if (!iter)
4123 return m_root;
4124
4125 wxDataViewItem item( (void*) iter->user_data );
4126 if (!item.IsOk())
4127 return m_root;
4128
4129 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
4130
4131 /*
4132 if (!result)
4133 {
4134 wxLogDebug( "Not found %p", iter->user_data );
4135 char *crash = NULL;
4136 *crash = 0;
4137 }
4138 // TODO: remove this code
4139 */
4140
4141 return result;
4142 }
4143
4144 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
4145 {
4146 if (!item.IsOk())
4147 return m_root;
4148
4149 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
4150
4151 /*
4152 if (!result)
4153 {
4154 wxLogDebug( "Not found %p", item.GetID() );
4155 char *crash = NULL;
4156 *crash = 0;
4157 }
4158 // TODO: remove this code
4159 */
4160
4161 return result;
4162 }
4163
4164 static wxGtkTreeModelNode*
4165 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
4166 {
4167 if( model == NULL )
4168 return NULL;
4169
4170 ItemList list;
4171 list.DeleteContents( true );
4172 if( !item.IsOk() )
4173 return NULL;
4174
4175 wxDataViewItem it( model->GetParent( item ) );
4176 while( it.IsOk() )
4177 {
4178 wxDataViewItem * pItem = new wxDataViewItem( it );
4179 list.Insert( pItem );
4180 it = model->GetParent( it );
4181 }
4182
4183 wxGtkTreeModelNode * node = treeNode;
4184 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
4185 {
4186 if( node && node->GetNodes().GetCount() != 0 )
4187 {
4188 int len = node->GetNodes().GetCount();
4189 wxGtkTreeModelNodes nodes = node->GetNodes();
4190 int j = 0;
4191 for( ; j < len; j ++)
4192 {
4193 if( nodes[j]->GetItem() == *(n->GetData()))
4194 {
4195 node = nodes[j];
4196 break;
4197 }
4198 }
4199
4200 if( j == len )
4201 {
4202 return NULL;
4203 }
4204 }
4205 else
4206 return NULL;
4207 }
4208 //Examine whether the node is item's parent node
4209 int len = node->GetChildCount();
4210 for( int i = 0; i < len ; i ++ )
4211 {
4212 if( node->GetChildren().Item( i ) == item.GetID() )
4213 return node;
4214 }
4215 return NULL;
4216 }
4217
4218 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
4219 {
4220 if (!iter)
4221 return NULL;
4222
4223 wxDataViewItem item( (void*) iter->user_data );
4224 if (!item.IsOk())
4225 return NULL;
4226
4227 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
4228 }
4229
4230 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
4231 {
4232 if (!item.IsOk())
4233 return NULL;
4234
4235 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
4236 }
4237
4238 //-----------------------------------------------------------------------------
4239 // wxDataViewCtrl signal callbacks
4240 //-----------------------------------------------------------------------------
4241
4242 static void
4243 wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv )
4244 {
4245 if (!GTK_WIDGET_REALIZED(dv->m_widget))
4246 return;
4247
4248 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() );
4249 event.SetItem( dv->GetSelection() );
4250 event.SetModel( dv->GetModel() );
4251 dv->HandleWindowEvent( event );
4252 }
4253
4254 static void
4255 wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path,
4256 GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv )
4257 {
4258 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
4259
4260 wxDataViewItem item(dv->GTKPathToItem(path));
4261 event.SetItem( item );
4262 event.SetModel( dv->GetModel() );
4263 dv->HandleWindowEvent( event );
4264 }
4265
4266 static gboolean
4267 wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4268 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4269 {
4270 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
4271
4272 wxDataViewItem item( (void*) iter->user_data );;
4273 event.SetItem( item );
4274 event.SetModel( dv->GetModel() );
4275 dv->HandleWindowEvent( event );
4276
4277 return !event.IsAllowed();
4278 }
4279
4280 static void
4281 wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4282 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4283 {
4284 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
4285
4286 wxDataViewItem item( (void*) iter->user_data );;
4287 event.SetItem( item );
4288 event.SetModel( dv->GetModel() );
4289 dv->HandleWindowEvent( event );
4290 }
4291
4292 static gboolean
4293 wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4294 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4295 {
4296 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
4297
4298 wxDataViewItem item( (void*) iter->user_data );;
4299 event.SetItem( item );
4300 event.SetModel( dv->GetModel() );
4301 dv->HandleWindowEvent( event );
4302
4303 return !event.IsAllowed();
4304 }
4305
4306 static void
4307 wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4308 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
4309 {
4310 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
4311
4312 wxDataViewItem item( (void*) iter->user_data );;
4313 event.SetItem( item );
4314 event.SetModel( dv->GetModel() );
4315 dv->HandleWindowEvent( event );
4316 }
4317
4318 //-----------------------------------------------------------------------------
4319 // wxDataViewCtrl
4320 //-----------------------------------------------------------------------------
4321
4322 void wxDataViewCtrl::AddChildGTK(wxWindowGTK* child)
4323 {
4324 GtkWidget* treeview = GtkGetTreeView();
4325
4326 // Insert widget in GtkTreeView
4327 if (GTK_WIDGET_REALIZED(treeview))
4328 gtk_widget_set_parent_window( child->m_widget,
4329 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
4330 gtk_widget_set_parent( child->m_widget, treeview );
4331 }
4332
4333 static
4334 void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
4335 GtkAllocation *WXUNUSED(gtk_alloc),
4336 wxDataViewCtrl *win )
4337 {
4338 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
4339 while (node)
4340 {
4341 wxWindow *child = node->GetData();
4342
4343 GtkRequisition req;
4344 gtk_widget_size_request( child->m_widget, &req );
4345
4346 GtkAllocation alloc;
4347 alloc.x = child->m_x;
4348 alloc.y = child->m_y;
4349 alloc.width = child->m_width;
4350 alloc.height = child->m_height;
4351 gtk_widget_size_allocate( child->m_widget, &alloc );
4352
4353 node = node->GetNext();
4354 }
4355 }
4356
4357
4358 //-----------------------------------------------------------------------------
4359 // "motion_notify_event"
4360 //-----------------------------------------------------------------------------
4361
4362 static gboolean
4363 gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget),
4364 GdkEventMotion *gdk_event,
4365 wxDataViewCtrl *dv )
4366 {
4367 if (gdk_event->is_hint)
4368 {
4369 int x = 0;
4370 int y = 0;
4371 GdkModifierType state;
4372 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
4373 gdk_event->x = x;
4374 gdk_event->y = y;
4375 }
4376
4377 wxGtkTreePath path;
4378 GtkTreeViewColumn *column = NULL;
4379 gint cell_x = 0;
4380 gint cell_y = 0;
4381 if (gtk_tree_view_get_path_at_pos(
4382 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4383 (int) gdk_event->x, (int) gdk_event->y,
4384 path.ByRef(),
4385 &column,
4386 &cell_x,
4387 &cell_y))
4388 {
4389 if (path)
4390 {
4391 GtkTreeIter iter;
4392 dv->GtkGetInternal()->get_iter( &iter, path );
4393 }
4394 }
4395
4396
4397 return FALSE;
4398 }
4399
4400 //-----------------------------------------------------------------------------
4401 // "button_press_event"
4402 //-----------------------------------------------------------------------------
4403
4404 static gboolean
4405 gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget),
4406 GdkEventButton *gdk_event,
4407 wxDataViewCtrl *dv )
4408 {
4409 if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS))
4410 {
4411 wxGtkTreePath path;
4412 GtkTreeViewColumn *column = NULL;
4413 gint cell_x = 0;
4414 gint cell_y = 0;
4415 if (gtk_tree_view_get_path_at_pos(
4416 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4417 (int) gdk_event->x, (int) gdk_event->y,
4418 path.ByRef(),
4419 &column,
4420 &cell_x,
4421 &cell_y))
4422 {
4423 if (path)
4424 {
4425 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() );
4426 event.SetItem(dv->GTKPathToItem(path));
4427 event.SetModel( dv->GetModel() );
4428 return dv->HandleWindowEvent( event );
4429 }
4430 }
4431 }
4432
4433 return FALSE;
4434 }
4435
4436 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4437
4438 wxDataViewCtrl::~wxDataViewCtrl()
4439 {
4440 m_cols.Clear();
4441
4442 delete m_internal;
4443 }
4444
4445 void wxDataViewCtrl::Init()
4446 {
4447 m_internal = NULL;
4448
4449 m_cols.DeleteContents( true );
4450 }
4451
4452 bool wxDataViewCtrl::Create(wxWindow *parent,
4453 wxWindowID id,
4454 const wxPoint& pos,
4455 const wxSize& size,
4456 long style,
4457 const wxValidator& validator,
4458 const wxString& name)
4459 {
4460 if (!PreCreation( parent, pos, size ) ||
4461 !CreateBase( parent, id, pos, size, style, validator, name ))
4462 {
4463 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
4464 return false;
4465 }
4466
4467 m_widget = gtk_scrolled_window_new (NULL, NULL);
4468 g_object_ref(m_widget);
4469
4470 GTKScrolledWindowSetBorder(m_widget, style);
4471
4472 m_treeview = gtk_tree_view_new();
4473 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
4474
4475 g_signal_connect (m_treeview, "size_allocate",
4476 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
4477
4478 #ifdef __WXGTK26__
4479 if (!gtk_check_version(2,6,0))
4480 {
4481 bool fixed = (style & wxDV_VARIABLE_LINE_HEIGHT) == 0;
4482 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4483 }
4484 #endif
4485
4486 if (style & wxDV_MULTIPLE)
4487 {
4488 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4489 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
4490 }
4491
4492 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
4493
4494 #ifdef __WXGTK210__
4495 if (!gtk_check_version(2,10,0))
4496 {
4497 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
4498
4499 if ((style & wxDV_HORIZ_RULES) != 0 &&
4500 (style & wxDV_VERT_RULES) != 0)
4501 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
4502 else if (style & wxDV_VERT_RULES)
4503 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
4504 else if (style & wxDV_HORIZ_RULES)
4505 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
4506
4507 if (grid != GTK_TREE_VIEW_GRID_LINES_NONE)
4508 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
4509 }
4510 #endif
4511
4512 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 );
4513
4514 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
4515 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4516 gtk_widget_show (m_treeview);
4517
4518 m_parent->DoAddChild( this );
4519
4520 PostCreation(size);
4521
4522 GtkEnableSelectionEvents();
4523
4524 g_signal_connect_after (m_treeview, "row-activated",
4525 G_CALLBACK (wxdataview_row_activated_callback), this);
4526
4527 g_signal_connect (m_treeview, "test-collapse-row",
4528 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
4529
4530 g_signal_connect_after (m_treeview, "row-collapsed",
4531 G_CALLBACK (wxdataview_row_collapsed_callback), this);
4532
4533 g_signal_connect (m_treeview, "test-expand-row",
4534 G_CALLBACK (wxdataview_test_expand_row_callback), this);
4535
4536 g_signal_connect_after (m_treeview, "row-expanded",
4537 G_CALLBACK (wxdataview_row_expanded_callback), this);
4538
4539 g_signal_connect (m_treeview, "motion_notify_event",
4540 G_CALLBACK (gtk_dataview_motion_notify_callback), this);
4541
4542 g_signal_connect (m_treeview, "button_press_event",
4543 G_CALLBACK (gtk_dataview_button_press_callback), this);
4544
4545 return true;
4546 }
4547
4548 wxDataViewItem wxDataViewCtrl::GTKPathToItem(GtkTreePath *path) const
4549 {
4550 GtkTreeIter iter;
4551 return wxDataViewItem(path && m_internal->get_iter(&iter, path)
4552 ? iter.user_data
4553 : NULL);
4554 }
4555
4556 void wxDataViewCtrl::OnInternalIdle()
4557 {
4558 wxWindow::OnInternalIdle();
4559
4560 m_internal->OnInternalIdle();
4561
4562 unsigned int cols = GetColumnCount();
4563 unsigned int i;
4564 for (i = 0; i < cols; i++)
4565 {
4566 wxDataViewColumn *col = GetColumn( i );
4567 col->OnInternalIdle();
4568 }
4569
4570 if (m_ensureVisibleDefered.IsOk())
4571 {
4572 ExpandAncestors(m_ensureVisibleDefered);
4573 GtkTreeIter iter;
4574 iter.user_data = (gpointer) m_ensureVisibleDefered.GetID();
4575 wxGtkTreePath path(m_internal->get_path( &iter ));
4576 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4577 m_ensureVisibleDefered = wxDataViewItem(0);
4578 }
4579 }
4580
4581 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4582 {
4583 wxDELETE(m_internal);
4584
4585 if (!wxDataViewCtrlBase::AssociateModel( model ))
4586 return false;
4587
4588 #ifdef __WXGTK26__
4589 if (!gtk_check_version(2,6,0))
4590 {
4591 bool fixed = (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) == 0) || (model->IsVirtualListModel()));
4592 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4593 }
4594 #endif
4595
4596 m_internal = new wxDataViewCtrlInternal( this, model );
4597
4598 return true;
4599 }
4600
4601 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4602 {
4603 return m_internal->EnableDragSource( format );
4604 }
4605
4606 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4607 {
4608 return m_internal->EnableDropTarget( format );
4609 }
4610
4611 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4612 {
4613 if (!wxDataViewCtrlBase::AppendColumn(col))
4614 return false;
4615
4616 m_cols.Append( col );
4617
4618 #ifdef __WXGTK26__
4619 if (!gtk_check_version(2,6,0))
4620 {
4621 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4622 GTK_TREE_VIEW_COLUMN_FIXED)
4623 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4624 }
4625 #endif
4626
4627 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
4628 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4629
4630 return true;
4631 }
4632
4633 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4634 {
4635 if (!wxDataViewCtrlBase::PrependColumn(col))
4636 return false;
4637
4638 m_cols.Insert( col );
4639
4640 #ifdef __WXGTK26__
4641 if (!gtk_check_version(2,6,0))
4642 {
4643 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4644 GTK_TREE_VIEW_COLUMN_FIXED)
4645 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4646 }
4647 #endif
4648
4649 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4650 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 );
4651
4652 return true;
4653 }
4654
4655 bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4656 {
4657 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4658 return false;
4659
4660 m_cols.Insert( pos, col );
4661
4662 #ifdef __WXGTK26__
4663 if (!gtk_check_version(2,6,0))
4664 {
4665 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4666 GTK_TREE_VIEW_COLUMN_FIXED)
4667 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4668 }
4669 #endif
4670
4671 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4672 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), pos );
4673
4674 return true;
4675 }
4676
4677 unsigned int wxDataViewCtrl::GetColumnCount() const
4678 {
4679 return m_cols.GetCount();
4680 }
4681
4682 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
4683 {
4684 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
4685 if (!gtk_col)
4686 return NULL;
4687
4688 wxDataViewColumnList::const_iterator iter;
4689 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
4690 {
4691 wxDataViewColumn *col = *iter;
4692 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
4693 {
4694 return col;
4695 }
4696 }
4697
4698 return NULL;
4699 }
4700
4701 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4702 {
4703 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
4704 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
4705
4706 m_cols.DeleteObject( column );
4707
4708 return true;
4709 }
4710
4711 bool wxDataViewCtrl::ClearColumns()
4712 {
4713 wxDataViewColumnList::iterator iter;
4714 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
4715 {
4716 wxDataViewColumn *col = *iter;
4717 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
4718 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4719 }
4720
4721 m_cols.Clear();
4722
4723 return true;
4724 }
4725
4726 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4727 {
4728 GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetConstGtkHandle());
4729
4730 GList *list = gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview) );
4731
4732 gint pos = g_list_index( list, (gconstpointer) gtk_column );
4733
4734 g_list_free( list );
4735
4736 return pos;
4737 }
4738
4739 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4740 {
4741 return m_internal->GetDataViewSortColumn();
4742 }
4743
4744 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4745 {
4746 GtkTreeIter iter;
4747 iter.user_data = item.GetID();
4748 wxGtkTreePath path(m_internal->get_path( &iter ));
4749 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
4750 }
4751
4752 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4753 {
4754 GtkTreeIter iter;
4755 iter.user_data = item.GetID();
4756 wxGtkTreePath path(m_internal->get_path( &iter ));
4757 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
4758 }
4759
4760 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4761 {
4762 GtkTreeIter iter;
4763 iter.user_data = item.GetID();
4764 wxGtkTreePath path(m_internal->get_path( &iter ));
4765 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview), path );
4766 }
4767
4768 wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4769 {
4770 // The tree doesn't have any current item if it hadn't been created yet but
4771 // it's arguably not an error to call this function in this case so just
4772 // return an invalid item without asserting.
4773 if ( !m_treeview )
4774 return wxDataViewItem();
4775
4776 wxGtkTreePath path;
4777 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview), path.ByRef(), NULL);
4778
4779 return GTKPathToItem(path);
4780 }
4781
4782 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4783 {
4784 wxCHECK_RET( m_treeview,
4785 "Current item can't be set before creating the control." );
4786
4787 // We need to make sure the model knows about this item or the path would
4788 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4789 ExpandAncestors(item);
4790
4791 // We also need to preserve the existing selection from changing.
4792 // Unfortunately the only way to do it seems to use our own selection
4793 // function and forbid any selection changes during set cursor call.
4794 wxGtkTreeSelectionLock
4795 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview)));
4796
4797 // Do move the cursor now.
4798 GtkTreeIter iter;
4799 iter.user_data = item.GetID();
4800 wxGtkTreePath path(m_internal->get_path( &iter ));
4801
4802 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview), path, NULL, FALSE);
4803 }
4804
4805 wxDataViewItem wxDataViewCtrl::GetSelection() const
4806 {
4807 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4808
4809 if (m_windowStyle & wxDV_MULTIPLE)
4810 {
4811 // Report the first one
4812 GtkTreeModel *model;
4813 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
4814
4815 if (list)
4816 {
4817 GtkTreePath *path = (GtkTreePath*) list->data;
4818 wxDataViewItem item(GTKPathToItem(path));
4819
4820 // delete list
4821 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4822 g_list_free( list );
4823
4824 return item;
4825 }
4826 }
4827 else
4828 {
4829 GtkTreeIter iter;
4830 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
4831 {
4832 wxDataViewItem item( iter.user_data );
4833 return item;
4834 }
4835 }
4836
4837 return wxDataViewItem(0);
4838 }
4839
4840 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4841 {
4842 sel.Clear();
4843
4844 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4845 if (HasFlag(wxDV_MULTIPLE))
4846 {
4847 GtkTreeModel *model;
4848 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
4849
4850 int count = 0;
4851 while (list)
4852 {
4853 GtkTreePath *path = (GtkTreePath*) list->data;
4854
4855 sel.Add(GTKPathToItem(path));
4856
4857 list = g_list_next( list );
4858 count++;
4859 }
4860
4861 // delete list
4862 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4863 g_list_free( list );
4864
4865 return count;
4866 }
4867 else
4868 {
4869 GtkTreeModel *model;
4870 GtkTreeIter iter;
4871 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
4872 if (has_selection)
4873 {
4874 sel.Add( wxDataViewItem( (void*) iter.user_data) );
4875 return 1;
4876 }
4877 }
4878
4879 return 0;
4880 }
4881
4882 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4883 {
4884 GtkDisableSelectionEvents();
4885
4886 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4887
4888 gtk_tree_selection_unselect_all( selection );
4889
4890 wxDataViewItem last_parent;
4891
4892 size_t i;
4893 for (i = 0; i < sel.GetCount(); i++)
4894 {
4895 wxDataViewItem item = sel[i];
4896 wxDataViewItem parent = GetModel()->GetParent( item );
4897 if (parent)
4898 {
4899 if (parent != last_parent)
4900 ExpandAncestors(item);
4901 }
4902 last_parent = parent;
4903
4904 GtkTreeIter iter;
4905 iter.stamp = m_internal->GetGtkModel()->stamp;
4906 iter.user_data = (gpointer) item.GetID();
4907 gtk_tree_selection_select_iter( selection, &iter );
4908 }
4909
4910 GtkEnableSelectionEvents();
4911 }
4912
4913 void wxDataViewCtrl::Select( const wxDataViewItem & item )
4914 {
4915 ExpandAncestors(item);
4916
4917 GtkDisableSelectionEvents();
4918
4919 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4920
4921 GtkTreeIter iter;
4922 iter.stamp = m_internal->GetGtkModel()->stamp;
4923 iter.user_data = (gpointer) item.GetID();
4924 gtk_tree_selection_select_iter( selection, &iter );
4925
4926 GtkEnableSelectionEvents();
4927 }
4928
4929 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
4930 {
4931 GtkDisableSelectionEvents();
4932
4933 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4934
4935 GtkTreeIter iter;
4936 iter.stamp = m_internal->GetGtkModel()->stamp;
4937 iter.user_data = (gpointer) item.GetID();
4938 gtk_tree_selection_unselect_iter( selection, &iter );
4939
4940 GtkEnableSelectionEvents();
4941 }
4942
4943 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
4944 {
4945 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4946
4947 GtkTreeIter iter;
4948 iter.stamp = m_internal->GetGtkModel()->stamp;
4949 iter.user_data = (gpointer) item.GetID();
4950
4951 return gtk_tree_selection_iter_is_selected( selection, &iter );
4952 }
4953
4954 void wxDataViewCtrl::SelectAll()
4955 {
4956 GtkDisableSelectionEvents();
4957
4958 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4959
4960 gtk_tree_selection_select_all( selection );
4961
4962 GtkEnableSelectionEvents();
4963 }
4964
4965 void wxDataViewCtrl::UnselectAll()
4966 {
4967 GtkDisableSelectionEvents();
4968
4969 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4970
4971 gtk_tree_selection_unselect_all( selection );
4972
4973 GtkEnableSelectionEvents();
4974 }
4975
4976 void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,
4977 const wxDataViewColumn *WXUNUSED(column))
4978 {
4979 m_ensureVisibleDefered = item;
4980 ExpandAncestors(item);
4981
4982 GtkTreeIter iter;
4983 iter.user_data = (gpointer) item.GetID();
4984 wxGtkTreePath path(m_internal->get_path( &iter ));
4985 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4986 }
4987
4988 void wxDataViewCtrl::HitTest(const wxPoint& point,
4989 wxDataViewItem& item,
4990 wxDataViewColumn *& column) const
4991 {
4992 // gtk_tree_view_get_dest_row_at_pos() is the right one. But it does not tell the column.
4993 // gtk_tree_view_get_path_at_pos() is the wrong function. It doesn't mind the header but returns column.
4994 // See http://mail.gnome.org/archives/gtkmm-list/2005-January/msg00080.html
4995 // So we have to use both of them.
4996 // Friedrich Haase 2010-9-20
4997 wxGtkTreePath path, pathScratch;
4998 GtkTreeViewColumn* GtkColumn = NULL;
4999 GtkTreeViewDropPosition pos = GTK_TREE_VIEW_DROP_INTO_OR_AFTER;
5000 gint cell_x = 0;
5001 gint cell_y = 0;
5002
5003 // cannot directly call GtkGetTreeView(), HitTest is const and so is this pointer
5004 wxDataViewCtrl* ctrl = (wxDataViewCtrl*)this; // ugly workaround, ctrl is NOT const
5005 GtkTreeView* treeView = GTK_TREE_VIEW(ctrl->GtkGetTreeView());
5006
5007 // is there possibly a better suited function to get the column?
5008 gtk_tree_view_get_path_at_pos( // and this is the wrong call but it delivers the column
5009 treeView,
5010 (int) point.x, (int) point.y,
5011 pathScratch.ByRef(),
5012 &GtkColumn, // here we get the GtkColumn
5013 &cell_x,
5014 &cell_y );
5015
5016 if ( GtkColumn != NULL )
5017 {
5018 // we got GTK column
5019 // the right call now which takes the header into account
5020 gtk_tree_view_get_dest_row_at_pos( treeView, (int) point.x, (int) point.y, path.ByRef(), &pos);
5021
5022 if (path)
5023 item = wxDataViewItem(GTKPathToItem(path));
5024 // else we got a GTK column but the position is not over an item, e.g. below last item
5025 for ( unsigned int i=0, cols=GetColumnCount(); i<cols; ++i ) // search the wx column
5026 {
5027 wxDataViewColumn* col = GetColumn(i);
5028 if ( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == GtkColumn )
5029 {
5030 column = col; // here we get the wx column
5031 break;
5032 }
5033 }
5034 }
5035 // else no column and thus no item, both null
5036 }
5037
5038 wxRect
5039 wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item),
5040 const wxDataViewColumn *WXUNUSED(column)) const
5041 {
5042 return wxRect();
5043 }
5044
5045 void wxDataViewCtrl::DoSetExpanderColumn()
5046 {
5047 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
5048 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
5049 }
5050
5051 void wxDataViewCtrl::DoSetIndent()
5052 {
5053 }
5054
5055 void wxDataViewCtrl::GtkDisableSelectionEvents()
5056 {
5057 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5058 g_signal_handlers_disconnect_by_func( selection,
5059 (gpointer) (wxdataview_selection_changed_callback), this);
5060 }
5061
5062 void wxDataViewCtrl::GtkEnableSelectionEvents()
5063 {
5064 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
5065 g_signal_connect_after (selection, "changed",
5066 G_CALLBACK (wxdataview_selection_changed_callback), this);
5067 }
5068
5069 // ----------------------------------------------------------------------------
5070 // visual attributes stuff
5071 // ----------------------------------------------------------------------------
5072
5073 // static
5074 wxVisualAttributes
5075 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
5076 {
5077 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
5078 }
5079
5080 void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
5081 {
5082 wxDataViewCtrlBase::DoApplyWidgetStyle(style);
5083 gtk_widget_modify_style(m_treeview, style);
5084 }
5085
5086 #endif // !wxUSE_GENERICDATAVIEWCTRL
5087
5088 #endif // wxUSE_DATAVIEWCTRL