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