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