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