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