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