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