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