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