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