]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
Apply the background fix only if the toolbar has controls
[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 GValue gvalue = { 0, };
1260 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1261 g_value_set_enum( &gvalue, gtkMode );
1262 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1263 g_value_unset( &gvalue );
1264 }
1265
1266 wxDataViewCellMode wxDataViewRenderer::GetMode() const
1267 {
1268 wxDataViewCellMode ret;
1269
1270 GValue gvalue;
1271 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1272
1273 switch (g_value_get_enum(&gvalue))
1274 {
1275 case GTK_CELL_RENDERER_MODE_INERT:
1276 ret = wxDATAVIEW_CELL_INERT;
1277 break;
1278 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1279 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1280 break;
1281 case GTK_CELL_RENDERER_MODE_EDITABLE:
1282 ret = wxDATAVIEW_CELL_EDITABLE;
1283 break;
1284 }
1285
1286 g_value_unset( &gvalue );
1287
1288 return ret;
1289 }
1290
1291 void wxDataViewRenderer::SetAlignment( int align )
1292 {
1293 // horizontal alignment:
1294
1295 gfloat xalign = 0.0;
1296 if (align & wxALIGN_RIGHT)
1297 xalign = 1.0;
1298 else if (align & wxALIGN_CENTER_HORIZONTAL)
1299 xalign = 0.5;
1300
1301 GValue gvalue = { 0, };
1302 g_value_init( &gvalue, G_TYPE_FLOAT );
1303 g_value_set_float( &gvalue, xalign );
1304 g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue );
1305 g_value_unset( &gvalue );
1306
1307 // vertical alignment:
1308
1309 gfloat yalign = 0.0;
1310 if (align & wxALIGN_BOTTOM)
1311 yalign = 1.0;
1312 else if (align & wxALIGN_CENTER_VERTICAL)
1313 yalign = 0.5;
1314
1315 GValue gvalue2 = { 0, };
1316 g_value_init( &gvalue2, G_TYPE_FLOAT );
1317 g_value_set_float( &gvalue2, yalign );
1318 g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 );
1319 g_value_unset( &gvalue2 );
1320 }
1321
1322 int wxDataViewRenderer::GetAlignment() const
1323 {
1324 int ret = 0;
1325 GValue gvalue;
1326
1327 // horizontal alignment:
1328
1329 g_object_get( G_OBJECT(m_renderer), "xalign", &gvalue, NULL );
1330 float xalign = g_value_get_float( &gvalue );
1331 if (xalign < 0.5)
1332 ret |= wxALIGN_LEFT;
1333 else if (xalign == 0.5)
1334 ret |= wxALIGN_CENTER_HORIZONTAL;
1335 else
1336 ret |= wxALIGN_RIGHT;
1337 g_value_unset( &gvalue );
1338
1339
1340 // vertical alignment:
1341
1342 g_object_get( G_OBJECT(m_renderer), "yalign", &gvalue, NULL );
1343 float yalign = g_value_get_float( &gvalue );
1344 if (yalign < 0.5)
1345 ret |= wxALIGN_TOP;
1346 else if (yalign == 0.5)
1347 ret |= wxALIGN_CENTER_VERTICAL;
1348 else
1349 ret |= wxALIGN_BOTTOM;
1350 g_value_unset( &gvalue );
1351
1352 return ret;
1353 }
1354
1355
1356
1357 // ---------------------------------------------------------
1358 // wxDataViewTextRenderer
1359 // ---------------------------------------------------------
1360
1361 extern "C" {
1362 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
1363 gchar *arg1, gchar *arg2, gpointer user_data );
1364 }
1365
1366 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
1367 gchar *arg1, gchar *arg2, gpointer user_data )
1368 {
1369 wxDataViewTextRenderer *cell = (wxDataViewTextRenderer*) user_data;
1370
1371 wxString tmp = wxGTK_CONV_BACK_FONT(arg2, cell->GetOwner()->GetOwner()->GetFont());
1372 wxVariant value = tmp;
1373 if (!cell->Validate( value ))
1374 return;
1375
1376 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
1377
1378 GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
1379 GtkTreeIter iter;
1380 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, path );
1381 wxDataViewItem item( (void*) iter.user_data );;
1382 gtk_tree_path_free( path );
1383
1384 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1385
1386 model->SetValue( value, item, model_col );
1387 model->ValueChanged( item, model_col );
1388 }
1389
1390 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
1391
1392 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1393 int align ) :
1394 wxDataViewRenderer( varianttype, mode, align )
1395 {
1396 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_text_new();
1397
1398 if (mode & wxDATAVIEW_CELL_EDITABLE)
1399 {
1400 GValue gvalue = { 0, };
1401 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1402 g_value_set_boolean( &gvalue, true );
1403 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
1404 g_value_unset( &gvalue );
1405
1406 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
1407
1408 GtkInitHandlers();
1409 }
1410
1411 SetMode(mode);
1412 SetAlignment(align);
1413 }
1414
1415 bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
1416 {
1417 wxString tmp = value;
1418
1419 GValue gvalue = { 0, };
1420 g_value_init( &gvalue, G_TYPE_STRING );
1421 g_value_set_string( &gvalue, wxGTK_CONV_FONT( tmp, GetOwner()->GetOwner()->GetFont() ) );
1422 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1423 g_value_unset( &gvalue );
1424
1425 return true;
1426 }
1427
1428 bool wxDataViewTextRenderer::GetValue( wxVariant &value ) const
1429 {
1430 GValue gvalue = { 0, };
1431 g_value_init( &gvalue, G_TYPE_STRING );
1432 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
1433 wxString tmp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ),
1434 wx_const_cast(wxDataViewTextRenderer*, this)->GetOwner()->GetOwner()->GetFont() );
1435 g_value_unset( &gvalue );
1436
1437 value = tmp;
1438
1439 return true;
1440 }
1441
1442 void wxDataViewTextRenderer::SetAlignment( int align )
1443 {
1444 wxDataViewRenderer::SetAlignment(align);
1445
1446 if (gtk_check_version(2,10,0))
1447 return;
1448
1449 // horizontal alignment:
1450 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
1451 if (align & wxALIGN_RIGHT)
1452 pangoAlign = PANGO_ALIGN_RIGHT;
1453 else if (align & wxALIGN_CENTER_HORIZONTAL)
1454 pangoAlign = PANGO_ALIGN_CENTER;
1455
1456 GValue gvalue = { 0, };
1457 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1458 g_value_set_enum( &gvalue, pangoAlign );
1459 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
1460 g_value_unset( &gvalue );
1461 }
1462
1463 // ---------------------------------------------------------
1464 // wxDataViewBitmapRenderer
1465 // ---------------------------------------------------------
1466
1467 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
1468
1469 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1470 int align ) :
1471 wxDataViewRenderer( varianttype, mode, align )
1472 {
1473 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_pixbuf_new();
1474
1475 SetMode(mode);
1476 SetAlignment(align);
1477 }
1478
1479 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
1480 {
1481 if (value.GetType() == wxT("wxBitmap"))
1482 {
1483 wxBitmap bitmap;
1484 bitmap << value;
1485
1486 // This may create a Pixbuf representation in the
1487 // wxBitmap object (and it will stay there)
1488 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
1489
1490 GValue gvalue = { 0, };
1491 g_value_init( &gvalue, G_TYPE_OBJECT );
1492 g_value_set_object( &gvalue, pixbuf );
1493 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1494 g_value_unset( &gvalue );
1495
1496 return true;
1497 }
1498
1499 if (value.GetType() == wxT("wxIcon"))
1500 {
1501 wxIcon bitmap;
1502 bitmap << value;
1503
1504 // This may create a Pixbuf representation in the
1505 // wxBitmap object (and it will stay there)
1506 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
1507
1508 GValue gvalue = { 0, };
1509 g_value_init( &gvalue, G_TYPE_OBJECT );
1510 g_value_set_object( &gvalue, pixbuf );
1511 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1512 g_value_unset( &gvalue );
1513
1514 return true;
1515 }
1516
1517 return false;
1518 }
1519
1520 bool wxDataViewBitmapRenderer::GetValue( wxVariant &value ) const
1521 {
1522 return false;
1523 }
1524
1525 // ---------------------------------------------------------
1526 // wxDataViewToggleRenderer
1527 // ---------------------------------------------------------
1528
1529 extern "C" {
1530 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
1531 gchar *path, gpointer user_data );
1532 }
1533
1534 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
1535 gchar *path, gpointer user_data )
1536 {
1537 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
1538
1539 // get old value
1540 GValue gvalue = { 0, };
1541 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1542 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
1543 bool tmp = g_value_get_boolean( &gvalue );
1544 g_value_unset( &gvalue );
1545 // invert it
1546 tmp = !tmp;
1547
1548 wxVariant value = tmp;
1549 if (!cell->Validate( value ))
1550 return;
1551
1552 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
1553
1554 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
1555 GtkTreeIter iter;
1556 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, gtk_path );
1557 wxDataViewItem item( (void*) iter.user_data );;
1558 gtk_tree_path_free( gtk_path );
1559
1560 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1561
1562 model->SetValue( value, item, model_col );
1563 model->ValueChanged( item, model_col );
1564 }
1565
1566 IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
1567
1568 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
1569 wxDataViewCellMode mode, int align ) :
1570 wxDataViewRenderer( varianttype, mode, align )
1571 {
1572 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
1573
1574 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
1575 {
1576 g_signal_connect_after( m_renderer, "toggled",
1577 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
1578 }
1579 else
1580 {
1581 GValue gvalue = { 0, };
1582 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1583 g_value_set_boolean( &gvalue, false );
1584 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
1585 g_value_unset( &gvalue );
1586 }
1587
1588 SetMode(mode);
1589 SetAlignment(align);
1590 }
1591
1592 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
1593 {
1594 bool tmp = value;
1595
1596 GValue gvalue = { 0, };
1597 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1598 g_value_set_boolean( &gvalue, tmp );
1599 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
1600 g_value_unset( &gvalue );
1601
1602 return true;
1603 }
1604
1605 bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
1606 {
1607 GValue gvalue = { 0, };
1608 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1609 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
1610 bool tmp = g_value_get_boolean( &gvalue );
1611 g_value_unset( &gvalue );
1612
1613 value = tmp;
1614
1615 return true;
1616 }
1617
1618 // ---------------------------------------------------------
1619 // wxDataViewCustomRenderer
1620 // ---------------------------------------------------------
1621
1622 class wxDataViewCtrlDC: public wxWindowDC
1623 {
1624 public:
1625 wxDataViewCtrlDC( wxDataViewCtrl *window )
1626 {
1627 GtkWidget *widget = window->m_treeview;
1628 // Set later
1629 m_window = NULL;
1630
1631 m_context = window->GtkGetPangoDefaultContext();
1632 m_layout = pango_layout_new( m_context );
1633 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
1634
1635 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
1636
1637 // Set m_window later
1638 // SetUpDC();
1639 // m_owner = window;
1640 }
1641 };
1642
1643 // ---------------------------------------------------------
1644 // wxDataViewCustomRenderer
1645 // ---------------------------------------------------------
1646
1647 IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
1648
1649 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
1650 wxDataViewCellMode mode, int align,
1651 bool no_init ) :
1652 wxDataViewRenderer( varianttype, mode, align )
1653 {
1654 m_dc = NULL;
1655
1656 if (no_init)
1657 m_renderer = NULL;
1658 else
1659 Init(mode, align);
1660 }
1661
1662 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
1663 {
1664 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
1665 renderer->cell = this;
1666
1667 m_renderer = (GtkCellRenderer*) renderer;
1668
1669 SetMode(mode);
1670 SetAlignment(align);
1671
1672 GtkInitHandlers();
1673
1674 return true;
1675 }
1676
1677 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1678 {
1679 if (m_dc)
1680 delete m_dc;
1681 }
1682
1683 wxDC *wxDataViewCustomRenderer::GetDC()
1684 {
1685 if (m_dc == NULL)
1686 {
1687 if (GetOwner() == NULL)
1688 return NULL;
1689 if (GetOwner()->GetOwner() == NULL)
1690 return NULL;
1691 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1692 }
1693
1694 return m_dc;
1695 }
1696
1697 // ---------------------------------------------------------
1698 // wxDataViewProgressRenderer
1699 // ---------------------------------------------------------
1700
1701 IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
1702
1703 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
1704 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
1705 wxDataViewCustomRenderer( varianttype, mode, align, true )
1706 {
1707 m_label = label;
1708 m_value = 0;
1709
1710 #ifdef __WXGTK26__
1711 if (!gtk_check_version(2,6,0))
1712 {
1713 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
1714
1715 GValue gvalue = { 0, };
1716 g_value_init( &gvalue, G_TYPE_STRING );
1717
1718 // FIXME: font encoding support
1719 g_value_set_string( &gvalue, wxGTK_CONV_SYS(m_label) );
1720 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1721 g_value_unset( &gvalue );
1722
1723 SetMode(mode);
1724 SetAlignment(align);
1725 }
1726 else
1727 #endif
1728 {
1729 // Use custom cell code
1730 wxDataViewCustomRenderer::Init(mode, align);
1731 }
1732 }
1733
1734 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1735 {
1736 }
1737
1738 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
1739 {
1740 #ifdef __WXGTK26__
1741 if (!gtk_check_version(2,6,0))
1742 {
1743 gint tmp = (long) value;
1744 GValue gvalue = { 0, };
1745 g_value_init( &gvalue, G_TYPE_INT );
1746 g_value_set_int( &gvalue, tmp );
1747 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
1748 g_value_unset( &gvalue );
1749 }
1750 else
1751 #endif
1752 {
1753 m_value = (long) value;
1754
1755 if (m_value < 0) m_value = 0;
1756 if (m_value > 100) m_value = 100;
1757 }
1758
1759 return true;
1760 }
1761
1762 bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const
1763 {
1764 return false;
1765 }
1766
1767 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int state )
1768 {
1769 double pct = (double)m_value / 100.0;
1770 wxRect bar = cell;
1771 bar.width = (int)(cell.width * pct);
1772 dc->SetPen( *wxTRANSPARENT_PEN );
1773 dc->SetBrush( *wxBLUE_BRUSH );
1774 dc->DrawRectangle( bar );
1775
1776 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1777 dc->SetPen( *wxBLACK_PEN );
1778 dc->DrawRectangle( cell );
1779
1780 return true;
1781 }
1782
1783 wxSize wxDataViewProgressRenderer::GetSize() const
1784 {
1785 return wxSize(40,12);
1786 }
1787
1788 // ---------------------------------------------------------
1789 // wxDataViewDateRenderer
1790 // ---------------------------------------------------------
1791
1792 class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
1793 {
1794 public:
1795 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
1796 wxDataViewModel *model, const wxDataViewItem &item, unsigned int col ) :
1797 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
1798 {
1799 m_model = model;
1800 m_item = item;
1801 m_col = col;
1802 m_cal = new wxCalendarCtrl( this, -1, *value );
1803 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
1804 sizer->Add( m_cal, 1, wxGROW );
1805 SetSizer( sizer );
1806 sizer->Fit( this );
1807 }
1808
1809 virtual void OnDismiss()
1810 {
1811 }
1812
1813 void OnCalendar( wxCalendarEvent &event );
1814
1815 wxCalendarCtrl *m_cal;
1816 wxDataViewModel *m_model;
1817 wxDataViewItem m_item;
1818 unsigned int m_col;
1819
1820 private:
1821 DECLARE_EVENT_TABLE()
1822 };
1823
1824 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
1825 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
1826 END_EVENT_TABLE()
1827
1828 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
1829 {
1830 wxDateTime date = event.GetDate();
1831 wxVariant value = date;
1832 m_model->SetValue( value, m_item, m_col );
1833 m_model->ValueChanged( m_item, m_col );
1834 DismissAndNotify();
1835 }
1836
1837 IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
1838
1839 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
1840 wxDataViewCellMode mode, int align ) :
1841 wxDataViewCustomRenderer( varianttype, mode, align )
1842 {
1843 SetMode(mode);
1844 SetAlignment(align);
1845 }
1846
1847 bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
1848 {
1849 m_date = value.GetDateTime();
1850
1851 return true;
1852 }
1853
1854 bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const
1855 {
1856 return false;
1857 }
1858
1859 bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
1860 {
1861 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
1862 wxString tmp = m_date.FormatDate();
1863 dc->DrawText( tmp, cell.x, cell.y );
1864
1865 return true;
1866 }
1867
1868 wxSize wxDataViewDateRenderer::GetSize() const
1869 {
1870 wxString tmp = m_date.FormatDate();
1871 wxCoord x,y,d;
1872 GetView()->GetTextExtent( tmp, &x, &y, &d );
1873 return wxSize(x,y+d);
1874 }
1875
1876 bool wxDataViewDateRenderer::Activate( wxRect cell, wxDataViewModel *model,
1877 const wxDataViewItem &item, unsigned int col )
1878 {
1879 wxVariant variant;
1880 model->GetValue( variant, item, col );
1881 wxDateTime value = variant.GetDateTime();
1882
1883 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
1884 GetOwner()->GetOwner()->GetParent(), &value, model, item, col );
1885 wxPoint pos = wxGetMousePosition();
1886 popup->Move( pos );
1887 popup->Layout();
1888 popup->Popup( popup->m_cal );
1889
1890 return true;
1891 }
1892
1893
1894 // ---------------------------------------------------------
1895 // wxDataViewIconTextRenderer
1896 // ---------------------------------------------------------
1897
1898 IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
1899
1900 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1901 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
1902 wxDataViewCustomRenderer( varianttype, mode, align )
1903 {
1904 SetMode(mode);
1905 SetAlignment(align);
1906 }
1907
1908 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1909 {
1910 }
1911
1912 bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
1913 {
1914 m_value << value;
1915 return true;
1916 }
1917
1918 bool wxDataViewIconTextRenderer::GetValue( wxVariant &value ) const
1919 {
1920 return false;
1921 }
1922
1923 bool wxDataViewIconTextRenderer::Render( wxRect cell, wxDC *dc, int state )
1924 {
1925 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
1926
1927 const wxIcon &icon = m_value.GetIcon();
1928 if (icon.IsOk())
1929 {
1930 dc->DrawIcon( icon, cell.x, cell.y ); // TODO centre
1931 cell.x += icon.GetWidth()+4;
1932 }
1933
1934 dc->DrawText( m_value.GetText(), cell.x, cell.y );
1935
1936 return true;
1937 }
1938
1939 wxSize wxDataViewIconTextRenderer::GetSize() const
1940 {
1941 return wxSize(80,16); // TODO
1942 }
1943
1944 wxControl* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1945 {
1946 return NULL;
1947 }
1948
1949 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
1950 {
1951 return false;
1952 }
1953
1954 // ---------------------------------------------------------
1955 // wxDataViewColumn
1956 // ---------------------------------------------------------
1957
1958
1959 static gboolean
1960 gtk_dataview_header_button_press_callback( GtkWidget *widget,
1961 GdkEventButton *gdk_event,
1962 wxDataViewColumn *column )
1963 {
1964 if (gdk_event->type != GDK_BUTTON_PRESS)
1965 return FALSE;
1966
1967 if (gdk_event->button == 1)
1968 {
1969 wxDataViewCtrl *dv = column->GetOwner();
1970 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
1971 event.SetDataViewColumn( column );
1972 event.SetModel( dv->GetModel() );
1973 if (dv->GetEventHandler()->ProcessEvent( event ))
1974 return FALSE;
1975 }
1976
1977 return FALSE;
1978 }
1979
1980 extern "C" {
1981 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1982 GtkCellRenderer *cell,
1983 GtkTreeModel *model,
1984 GtkTreeIter *iter,
1985 gpointer data );
1986 }
1987
1988
1989 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1990 GtkCellRenderer *renderer,
1991 GtkTreeModel *model,
1992 GtkTreeIter *iter,
1993 gpointer data )
1994 {
1995 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
1996 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
1997
1998 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
1999
2000 wxDataViewItem item( (void*) iter->user_data );
2001
2002 wxVariant value;
2003 tree_model->internal->GetDataViewModel()->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
2004
2005 if (value.GetType() != cell->GetVariantType())
2006 wxLogError( wxT("Wrong type, required: %s but: %s"),
2007 value.GetType().c_str(),
2008 cell->GetVariantType().c_str() );
2009
2010 cell->SetValue( value );
2011
2012 #if 0
2013 wxListItemAttr attr;
2014 tree_model->model->GetAttr( attr, cell->GetOwner()->GetModelColumn(), model_row );
2015
2016 if (attr.HasBackgroundColour())
2017 {
2018 wxColour colour = attr.GetBackgroundColour();
2019 const GdkColor * const gcol = colour.GetColor();
2020
2021 GValue gvalue = { 0, };
2022 g_value_init( &gvalue, GDK_TYPE_COLOR );
2023 g_value_set_boxed( &gvalue, gcol );
2024 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
2025 g_value_unset( &gvalue );
2026 }
2027 else
2028 {
2029 GValue gvalue = { 0, };
2030 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2031 g_value_set_boolean( &gvalue, FALSE );
2032 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
2033 g_value_unset( &gvalue );
2034 }
2035 #endif
2036
2037 }
2038
2039 IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
2040
2041 #include <wx/listimpl.cpp>
2042 WX_DEFINE_LIST(wxDataViewColumnList);
2043
2044 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
2045 unsigned int model_column, int width,
2046 wxAlignment align, int flags ) :
2047 wxDataViewColumnBase( title, cell, model_column, width, align, flags )
2048 {
2049 Init( align, flags, width );
2050
2051 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column), TRUE );
2052 SetTitle( title );
2053 }
2054
2055 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
2056 unsigned int model_column, int width,
2057 wxAlignment align, int flags ) :
2058 wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags )
2059 {
2060 Init( align, flags, width );
2061
2062 SetBitmap( bitmap );
2063 }
2064
2065 void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
2066 {
2067 m_isConnected = false;
2068
2069 GtkCellRenderer *renderer = (GtkCellRenderer *) GetRenderer()->GetGtkHandle();
2070 GtkTreeViewColumn *column = gtk_tree_view_column_new();
2071 m_column = (GtkWidget*) column;
2072
2073 SetFlags( flags );
2074 SetAlignment( align );
2075
2076 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
2077 // as GTK+ is smart and unless explicitely told, will set the minimal
2078 // width to the title's lenght, which is a better default
2079
2080 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
2081 // that we use for the wxDataViewCtrl
2082 gtk_tree_view_column_set_fixed_width( column, width < 0 ? wxDVC_DEFAULT_WIDTH : width );
2083 gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED );
2084
2085 gtk_tree_view_column_pack_end( column, renderer, TRUE );
2086
2087 gtk_tree_view_column_set_cell_data_func( column, renderer,
2088 wxGtkTreeCellDataFunc, (gpointer) GetRenderer(), NULL );
2089 }
2090
2091 wxDataViewColumn::~wxDataViewColumn()
2092 {
2093 }
2094
2095 void wxDataViewColumn::OnInternalIdle()
2096 {
2097 if (m_isConnected)
2098 return;
2099
2100 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
2101 {
2102 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2103 if (column->button)
2104 {
2105 g_signal_connect(column->button, "button_press_event",
2106 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
2107
2108 m_isConnected = true;
2109 }
2110 }
2111 }
2112
2113 void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
2114 {
2115 wxDataViewColumnBase::SetOwner( owner );
2116
2117 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2118
2119 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
2120 }
2121
2122 void wxDataViewColumn::SetTitle( const wxString &title )
2123 {
2124 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2125
2126 if (m_isConnected)
2127 {
2128 // disconnect before column->button gets recreated
2129 g_signal_handlers_disconnect_by_func( column->button,
2130 (GtkWidget*) gtk_dataview_header_button_press_callback, this);
2131
2132 m_isConnected = false;
2133 }
2134
2135 // FIXME: can it really happen that we don't have the owner here??
2136 wxDataViewCtrl *ctrl = GetOwner();
2137 gtk_tree_view_column_set_title( column, ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
2138 : wxGTK_CONV_SYS(title) );
2139
2140 gtk_tree_view_column_set_widget( column, NULL );
2141 }
2142
2143 wxString wxDataViewColumn::GetTitle() const
2144 {
2145 const gchar *str = gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column) );
2146 return wxConvFileName->cMB2WX(str);
2147 }
2148
2149 void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
2150 {
2151 wxDataViewColumnBase::SetBitmap( bitmap );
2152
2153 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2154 if (bitmap.Ok())
2155 {
2156 GtkImage *gtk_image = GTK_IMAGE( gtk_image_new() );
2157
2158 GdkBitmap *mask = (GdkBitmap *) NULL;
2159 if (bitmap.GetMask())
2160 mask = bitmap.GetMask()->GetBitmap();
2161
2162 if (bitmap.HasPixbuf())
2163 {
2164 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
2165 bitmap.GetPixbuf());
2166 }
2167 else
2168 {
2169 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
2170 bitmap.GetPixmap(), mask);
2171 }
2172 gtk_widget_show( GTK_WIDGET(gtk_image) );
2173
2174 gtk_tree_view_column_set_widget( column, GTK_WIDGET(gtk_image) );
2175 }
2176 else
2177 {
2178 gtk_tree_view_column_set_widget( column, NULL );
2179 }
2180 }
2181
2182 void wxDataViewColumn::SetHidden( bool hidden )
2183 {
2184 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
2185 }
2186
2187 void wxDataViewColumn::SetResizeable( bool resizeable )
2188 {
2189 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
2190 }
2191
2192 void wxDataViewColumn::SetAlignment( wxAlignment align )
2193 {
2194 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2195
2196 gfloat xalign = 0.0;
2197 if (align == wxALIGN_RIGHT)
2198 xalign = 1.0;
2199 if (align == wxALIGN_CENTER_HORIZONTAL ||
2200 align == wxALIGN_CENTER)
2201 xalign = 0.5;
2202
2203 gtk_tree_view_column_set_alignment( column, xalign );
2204 }
2205
2206 wxAlignment wxDataViewColumn::GetAlignment() const
2207 {
2208 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
2209
2210 if (xalign == 1.0)
2211 return wxALIGN_RIGHT;
2212 if (xalign == 0.5)
2213 return wxALIGN_CENTER_HORIZONTAL;
2214
2215 return wxALIGN_LEFT;
2216 }
2217
2218 void wxDataViewColumn::SetSortable( bool sortable )
2219 {
2220 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2221
2222 if (sortable)
2223 {
2224 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
2225 }
2226 else
2227 {
2228 gtk_tree_view_column_set_sort_column_id( column, -1 );
2229 gtk_tree_view_column_set_sort_indicator( column, FALSE );
2230 }
2231 }
2232
2233 bool wxDataViewColumn::IsSortable() const
2234 {
2235 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2236 return (gtk_tree_view_column_get_sort_column_id( column ) != -1);
2237 }
2238
2239 bool wxDataViewColumn::IsResizeable() const
2240 {
2241 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2242 return gtk_tree_view_column_get_resizable( column );
2243 }
2244
2245 bool wxDataViewColumn::IsHidden() const
2246 {
2247 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2248 return !gtk_tree_view_column_get_visible( column );
2249 }
2250
2251 void wxDataViewColumn::SetSortOrder( bool ascending )
2252 {
2253 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2254
2255 if (ascending)
2256 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
2257 else
2258 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
2259
2260 gtk_tree_view_column_set_sort_indicator( column, TRUE );
2261 }
2262
2263 bool wxDataViewColumn::IsSortOrderAscending() const
2264 {
2265 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2266
2267 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
2268 }
2269
2270 void wxDataViewColumn::SetMinWidth( int width )
2271 {
2272 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
2273 }
2274
2275 int wxDataViewColumn::GetMinWidth() const
2276 {
2277 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
2278 }
2279
2280 int wxDataViewColumn::GetWidth() const
2281 {
2282 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
2283 }
2284
2285 void wxDataViewColumn::SetWidth( int width )
2286 {
2287 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
2288 }
2289
2290
2291 //-----------------------------------------------------------------------------
2292 // wxGtkTreeModelNode
2293 //-----------------------------------------------------------------------------
2294
2295 void wxGtkTreeModelNode::Resort()
2296 {
2297 size_t child_count = GetChildCount();
2298 if (child_count == 0)
2299 return;
2300
2301 size_t node_count = GetNodesCount();
2302
2303 if (child_count == 1)
2304 {
2305 if (node_count == 1)
2306 {
2307 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
2308 node->Resort();
2309 }
2310 return;
2311 }
2312
2313 wxGtkTreeModelChildren temp;
2314 WX_APPEND_ARRAY( temp, m_children );
2315
2316 g_internal = m_internal;
2317 m_children.Sort( &wxGtkTreeModelChildCmp );
2318
2319 gint *new_order = new gint[child_count];
2320
2321 unsigned int pos;
2322 for (pos = 0; pos < child_count; pos++)
2323 {
2324 void *id = m_children.Item( pos );
2325 int old_pos = temp.Index( id );
2326 new_order[pos] = old_pos;
2327 }
2328
2329 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
2330
2331 GtkTreeIter iter;
2332 iter.user_data = GetItem().GetID();
2333 iter.stamp = m_internal->GetGtkModel()->stamp;
2334
2335 GtkTreePath *path = m_internal->get_path( &iter );
2336
2337 gtk_tree_model_rows_reordered( gtk_tree_model, path, &iter, new_order );
2338
2339 gtk_tree_path_free (path);
2340
2341 delete [] new_order;
2342
2343 for (pos = 0; pos < node_count; pos++)
2344 {
2345 wxGtkTreeModelNode *node = m_nodes.Item( pos );
2346 node->Resort();
2347 }
2348 }
2349
2350 //-----------------------------------------------------------------------------
2351 // wxDataViewCtrlInternal
2352 //-----------------------------------------------------------------------------
2353
2354 wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner,
2355 wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model )
2356 {
2357 m_owner = owner;
2358 m_wx_model = wx_model;
2359 m_gtk_model = gtk_model;
2360 m_root = NULL;
2361 m_sort_order = GTK_SORT_ASCENDING;
2362 m_sort_column = -1;
2363 InitTree();
2364 }
2365
2366 wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2367 {
2368 g_object_unref( m_gtk_model );
2369 }
2370
2371 void wxDataViewCtrlInternal::InitTree()
2372 {
2373 wxDataViewItem item;
2374 m_root = new wxGtkTreeModelNode( NULL, item, this );
2375
2376 BuildBranch( m_root );
2377 }
2378
2379 void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
2380 {
2381 if (node->GetChildCount() == 0)
2382 {
2383 wxDataViewItemArray children;
2384 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
2385 unsigned int pos;
2386 for (pos = 0; pos < count; pos++)
2387 {
2388 wxDataViewItem child = children[pos];
2389
2390 if (m_wx_model->IsContainer( child ))
2391 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
2392 else
2393 node->AddLeave( child.GetID() );
2394
2395 // Don't send any events here
2396 }
2397 }
2398 }
2399
2400 void wxDataViewCtrlInternal::Resort()
2401 {
2402 m_root->Resort();
2403 }
2404
2405 bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
2406 {
2407 wxGtkTreeModelNode *parent_node = FindNode( parent );
2408 if (m_wx_model->IsContainer( item ))
2409 parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) );
2410 else
2411 parent_node->AddLeave( item.GetID() );
2412
2413 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED, m_owner->GetId() );
2414 event.SetEventObject( m_owner );
2415 event.SetModel( m_owner->GetModel() );
2416 event.SetItem( item );
2417 m_owner->GetEventHandler()->ProcessEvent( event );
2418
2419 return true;
2420 }
2421
2422 bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
2423 {
2424 wxGtkTreeModelNode *parent_node = FindNode( parent );
2425 parent_node->DeleteChild( item.GetID() );
2426
2427 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED, m_owner->GetId() );
2428 event.SetEventObject( m_owner );
2429 event.SetModel( m_owner->GetModel() );
2430 event.SetItem( item );
2431 m_owner->GetEventHandler()->ProcessEvent( event );
2432
2433 return true;
2434 }
2435
2436 bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
2437 {
2438 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED, m_owner->GetId() );
2439 event.SetEventObject( m_owner );
2440 event.SetModel( m_owner->GetModel() );
2441 event.SetItem( item );
2442 m_owner->GetEventHandler()->ProcessEvent( event );
2443
2444 return true;
2445 }
2446
2447 bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int col )
2448 {
2449 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED, m_owner->GetId() );
2450 event.SetEventObject( m_owner );
2451 event.SetModel( m_owner->GetModel() );
2452 event.SetColumn( col );
2453 event.SetItem( item );
2454 m_owner->GetEventHandler()->ProcessEvent( event );
2455
2456 return true;
2457 }
2458
2459 bool wxDataViewCtrlInternal::Cleared()
2460 {
2461 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED, m_owner->GetId() );
2462 event.SetEventObject( m_owner );
2463 event.SetModel( m_owner->GetModel() );
2464 m_owner->GetEventHandler()->ProcessEvent( event );
2465
2466 return true;
2467 }
2468
2469 gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
2470 {
2471 int depth = gtk_tree_path_get_depth( path );
2472
2473 wxGtkTreeModelNode *node = m_root;
2474
2475 int i;
2476 for (i = 0; i < depth; i++)
2477 {
2478 BuildBranch( node );
2479
2480 gint pos = gtk_tree_path_get_indices (path)[i];
2481 if (pos < 0) return FALSE;
2482 if ((size_t)pos >= node->GetChildCount()) return FALSE;
2483
2484 void* id = node->GetChildren().Item( (size_t) pos );
2485
2486 if (i == depth-1)
2487 {
2488 iter->stamp = m_gtk_model->stamp;
2489 iter->user_data = id;
2490 return TRUE;
2491 }
2492
2493 size_t count = node->GetNodes().GetCount();
2494 size_t pos2;
2495 for (pos2 = 0; pos2 < count; pos2++)
2496 {
2497 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
2498 if (child_node->GetItem().GetID() == id)
2499 {
2500 node = child_node;
2501 break;
2502 }
2503 }
2504 }
2505
2506 return FALSE;
2507 }
2508
2509 GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
2510 {
2511 GtkTreePath *retval = gtk_tree_path_new ();
2512 void *id = iter->user_data;
2513
2514 wxGtkTreeModelNode *node = FindParentNode( iter );
2515 while (node)
2516 {
2517 int pos = node->GetChildren().Index( id );
2518
2519 gtk_tree_path_prepend_index( retval, pos );
2520
2521 id = node->GetItem().GetID();
2522 node = node->GetParent();
2523 }
2524
2525 return retval;
2526 }
2527
2528 gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
2529 {
2530 wxGtkTreeModelNode *parent = FindParentNode( iter );
2531 if( parent == NULL )
2532 return FALSE;
2533
2534 int pos = parent->GetChildren().Index( iter->user_data );
2535
2536 if (pos == (int) parent->GetChildCount()-1)
2537 return FALSE;
2538
2539 iter->stamp = m_gtk_model->stamp;
2540 iter->user_data = parent->GetChildren().Item( pos+1 );
2541
2542 return TRUE;
2543 }
2544
2545 gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
2546 {
2547 wxDataViewItem item( (void*) parent->user_data );
2548
2549 if (!m_wx_model->IsContainer( item ))
2550 return FALSE;
2551
2552 wxGtkTreeModelNode *parent_node = FindNode( parent );
2553 BuildBranch( parent_node );
2554
2555 if (parent_node->GetChildCount() == 0)
2556 return FALSE;
2557
2558 iter->stamp = m_gtk_model->stamp;
2559 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
2560
2561 return TRUE;
2562 }
2563
2564 gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
2565 {
2566 wxDataViewItem item( (void*) iter->user_data );
2567 bool is_container = m_wx_model->IsContainer( item );
2568
2569 if (!is_container)
2570 return FALSE;
2571
2572 wxGtkTreeModelNode *node = FindNode( iter );
2573 BuildBranch( node );
2574
2575 return (node->GetChildCount() > 0);
2576 }
2577
2578 gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
2579 {
2580 wxDataViewItem item( (void*) iter->user_data );
2581
2582 if (!m_wx_model->IsContainer( item ))
2583 return 0;
2584
2585 wxGtkTreeModelNode *parent_node = FindNode( iter );
2586 BuildBranch( parent_node );
2587
2588 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
2589
2590 return parent_node->GetChildCount();
2591 }
2592
2593 gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
2594 {
2595 void* id = NULL;
2596 if (parent) id = (void*) parent->user_data;
2597 wxDataViewItem item( id );
2598
2599 if (!m_wx_model->IsContainer( item ))
2600 return FALSE;
2601
2602 wxGtkTreeModelNode *parent_node = FindNode( parent );
2603 BuildBranch( parent_node );
2604
2605 // wxPrintf( "iter_nth_child %d\n", n );
2606
2607 iter->stamp = m_gtk_model->stamp;
2608 iter->user_data = parent_node->GetChildren().Item( n );
2609
2610 return TRUE;
2611 }
2612
2613 gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
2614 {
2615 wxGtkTreeModelNode *node = FindParentNode( child );
2616 if (!node)
2617 return FALSE;
2618
2619 iter->stamp = m_gtk_model->stamp;
2620 iter->user_data = (gpointer) node->GetItem().GetID();
2621
2622 return TRUE;
2623 }
2624
2625 static wxGtkTreeModelNode*
2626 wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
2627 {
2628 if( model == NULL )
2629 return NULL;
2630
2631 ItemList list;
2632 list.DeleteContents( true );
2633 wxDataViewItem it( item );
2634 while( it.IsOk() )
2635 {
2636 wxDataViewItem * pItem = new wxDataViewItem( it );
2637 list.Insert( pItem );
2638 it = model->GetParent( it );
2639 }
2640
2641 wxGtkTreeModelNode * node = treeNode;
2642 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
2643 {
2644 if( node && node->GetNodes().GetCount() != 0 )
2645 {
2646 int len = node->GetNodes().GetCount();
2647 wxGtkTreeModelNodes nodes = node->GetNodes();
2648 int j = 0;
2649 for( ; j < len; j ++)
2650 {
2651 if( nodes[j]->GetItem() == *(n->GetData()))
2652 {
2653 node = nodes[j];
2654 break;
2655 }
2656 }
2657
2658 if( j == len )
2659 {
2660 return NULL;
2661 }
2662 }
2663 else
2664 return NULL;
2665 }
2666 return node;
2667
2668 }
2669
2670 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
2671 {
2672 if (!iter)
2673 return m_root;
2674
2675 wxDataViewItem item( (void*) iter->user_data );
2676 if (!item.IsOk())
2677 return m_root;
2678
2679 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
2680
2681 if (!result)
2682 {
2683 wxPrintf( "Not found %d\n", (int) iter->user_data );
2684 char *crash = NULL;
2685 *crash = 0;
2686 }
2687
2688 return result;
2689 }
2690
2691 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
2692 {
2693 if (!item.IsOk())
2694 return m_root;
2695
2696 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
2697
2698 if (!result)
2699 {
2700 wxPrintf( "Not found %d\n", (int) item.GetID() );
2701 char *crash = NULL;
2702 *crash = 0;
2703 }
2704
2705 return result;
2706 }
2707
2708 static wxGtkTreeModelNode*
2709 wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
2710 {
2711 if( model == NULL )
2712 return NULL;
2713
2714 ItemList list;
2715 list.DeleteContents( true );
2716 if( !item.IsOk() )
2717 return NULL;
2718
2719 wxDataViewItem it( model->GetParent( item ) );
2720 while( it.IsOk() )
2721 {
2722 wxDataViewItem * pItem = new wxDataViewItem( it );
2723 list.Insert( pItem );
2724 it = model->GetParent( it );
2725 }
2726
2727 wxGtkTreeModelNode * node = treeNode;
2728 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
2729 {
2730 if( node && node->GetNodes().GetCount() != 0 )
2731 {
2732 int len = node->GetNodes().GetCount();
2733 wxGtkTreeModelNodes nodes = node->GetNodes();
2734 int j = 0;
2735 for( ; j < len; j ++)
2736 {
2737 if( nodes[j]->GetItem() == *(n->GetData()))
2738 {
2739 node = nodes[j];
2740 break;
2741 }
2742 }
2743
2744 if( j == len )
2745 {
2746 return NULL;
2747 }
2748 }
2749 else
2750 return NULL;
2751 }
2752 //Examine whether the node is item's parent node
2753 int len = node->GetChildCount();
2754 for( int i = 0; i < len ; i ++ )
2755 {
2756 if( node->GetChildren().Item( i ) == item.GetID() )
2757 return node;
2758 }
2759 return NULL;
2760 }
2761
2762 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
2763 {
2764 if (!iter)
2765 return NULL;
2766
2767 wxDataViewItem item( (void*) iter->user_data );
2768 if (!item.IsOk())
2769 return NULL;
2770
2771 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
2772 }
2773
2774 wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
2775 {
2776 if (!item.IsOk())
2777 return NULL;
2778
2779 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
2780 }
2781
2782 //-----------------------------------------------------------------------------
2783 // wxDataViewCtrl signal callbacks
2784 //-----------------------------------------------------------------------------
2785
2786 static void
2787 wxdataview_selection_changed_callback( GtkTreeSelection* selection, wxDataViewCtrl *dv )
2788 {
2789 if (!GTK_WIDGET_REALIZED(dv->m_widget))
2790 return;
2791
2792 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED, dv->GetId() );
2793 event.SetItem( dv->GetSelection() );
2794 event.SetModel( dv->GetModel() );
2795 dv->GetEventHandler()->ProcessEvent( event );
2796 }
2797
2798 static void
2799 wxdataview_row_activated_callback( GtkTreeView* treeview, GtkTreePath *path,
2800 GtkTreeViewColumn *column, wxDataViewCtrl *dv )
2801 {
2802 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
2803
2804 GtkTreeIter iter;
2805 dv->GtkGetInternal()->get_iter( &iter, path );
2806 wxDataViewItem item( (void*) iter.user_data );;
2807 event.SetItem( item );
2808 event.SetModel( dv->GetModel() );
2809 dv->GetEventHandler()->ProcessEvent( event );
2810 }
2811
2812 static gboolean
2813 wxdataview_test_expand_row_callback( GtkTreeView* treeview, GtkTreeIter* iter,
2814 GtkTreePath *path, wxDataViewCtrl *dv )
2815 {
2816 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
2817
2818 wxDataViewItem item( (void*) iter->user_data );;
2819 event.SetItem( item );
2820 event.SetModel( dv->GetModel() );
2821 dv->GetEventHandler()->ProcessEvent( event );
2822
2823 return !event.IsAllowed();
2824 }
2825
2826 static void
2827 wxdataview_row_expanded_callback( GtkTreeView* treeview, GtkTreeIter* iter,
2828 GtkTreePath *path, wxDataViewCtrl *dv )
2829 {
2830 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
2831
2832 wxDataViewItem item( (void*) iter->user_data );;
2833 event.SetItem( item );
2834 event.SetModel( dv->GetModel() );
2835 dv->GetEventHandler()->ProcessEvent( event );
2836 }
2837
2838 static gboolean
2839 wxdataview_test_collapse_row_callback( GtkTreeView* treeview, GtkTreeIter* iter,
2840 GtkTreePath *path, wxDataViewCtrl *dv )
2841 {
2842 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
2843
2844 wxDataViewItem item( (void*) iter->user_data );;
2845 event.SetItem( item );
2846 event.SetModel( dv->GetModel() );
2847 dv->GetEventHandler()->ProcessEvent( event );
2848
2849 return !event.IsAllowed();
2850 }
2851
2852 static void
2853 wxdataview_row_collapsed_callback( GtkTreeView* treeview, GtkTreeIter* iter,
2854 GtkTreePath *path, wxDataViewCtrl *dv )
2855 {
2856 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
2857
2858 wxDataViewItem item( (void*) iter->user_data );;
2859 event.SetItem( item );
2860 event.SetModel( dv->GetModel() );
2861 dv->GetEventHandler()->ProcessEvent( event );
2862 }
2863
2864 //-----------------------------------------------------------------------------
2865 // wxDataViewCtrl
2866 //-----------------------------------------------------------------------------
2867
2868 //-----------------------------------------------------------------------------
2869 // InsertChild for wxDataViewCtrl
2870 //-----------------------------------------------------------------------------
2871
2872 static void wxInsertChildInDataViewCtrl( wxWindowGTK* parent, wxWindowGTK* child )
2873 {
2874 wxDataViewCtrl * dvc = (wxDataViewCtrl*) parent;
2875 GtkWidget *treeview = dvc->GtkGetTreeView();
2876
2877 // Insert widget in GtkTreeView
2878 if (GTK_WIDGET_REALIZED(treeview))
2879 gtk_widget_set_parent_window( child->m_widget,
2880 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
2881 gtk_widget_set_parent( child->m_widget, treeview );
2882 }
2883
2884 static
2885 void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
2886 GtkAllocation *alloc,
2887 wxDataViewCtrl *win )
2888 {
2889 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
2890 while (node)
2891 {
2892 wxWindow *child = node->GetData();
2893
2894 GtkRequisition req;
2895 gtk_widget_size_request( child->m_widget, &req );
2896
2897 GtkAllocation alloc;
2898 alloc.x = child->m_x;
2899 alloc.y = child->m_y;
2900 alloc.width = child->m_width;
2901 alloc.height = child->m_height;
2902 gtk_widget_size_allocate( child->m_widget, &alloc );
2903
2904 node = node->GetNext();
2905 }
2906 }
2907
2908
2909
2910 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
2911
2912 wxDataViewCtrl::~wxDataViewCtrl()
2913 {
2914 if (m_notifier)
2915 GetModel()->RemoveNotifier( m_notifier );
2916
2917 // remove the model from the GtkTreeView before it gets destroyed by the
2918 // wxDataViewCtrlBase's dtor
2919 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL );
2920
2921 delete m_internal;
2922 }
2923
2924 void wxDataViewCtrl::Init()
2925 {
2926 m_notifier = NULL;
2927 }
2928
2929 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
2930 const wxPoint& pos, const wxSize& size,
2931 long style, const wxValidator& validator )
2932 {
2933 Init();
2934
2935 if (!PreCreation( parent, pos, size ) ||
2936 !CreateBase( parent, id, pos, size, style, validator ))
2937 {
2938 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
2939 return false;
2940 }
2941
2942 m_insertCallback = wxInsertChildInDataViewCtrl;
2943
2944 m_widget = gtk_scrolled_window_new (NULL, NULL);
2945
2946 GtkScrolledWindowSetBorder(m_widget, style);
2947
2948 m_treeview = gtk_tree_view_new();
2949 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
2950
2951 g_signal_connect (m_treeview, "size_allocate",
2952 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
2953
2954 #ifdef __WXGTK26__
2955 if (!gtk_check_version(2,6,0))
2956 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), TRUE );
2957 #endif
2958
2959 if (style & wxDV_MULTIPLE)
2960 {
2961 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2962 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2963 }
2964
2965 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
2966
2967 #ifdef __WXGTK210__
2968 if (!gtk_check_version(2,10,0))
2969 {
2970 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
2971
2972 if ((style & wxDV_HORIZ_RULES) != 0 &&
2973 (style & wxDV_VERT_RULES) != 0)
2974 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
2975 else if (style & wxDV_VERT_RULES)
2976 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
2977 else if (style & wxDV_HORIZ_RULES)
2978 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
2979
2980 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
2981 }
2982 else
2983 #endif
2984 {
2985 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_HORIZ_RULES) != 0 );
2986 }
2987
2988 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
2989 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
2990 gtk_widget_show (m_treeview);
2991
2992 m_parent->DoAddChild( this );
2993
2994 PostCreation(size);
2995
2996 GtkEnableSelectionEvents();
2997
2998 g_signal_connect_after (m_treeview, "row-activated",
2999 G_CALLBACK (wxdataview_row_activated_callback), this);
3000
3001 g_signal_connect (m_treeview, "test-collapse-row",
3002 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
3003
3004 g_signal_connect_after (m_treeview, "row-collapsed",
3005 G_CALLBACK (wxdataview_row_collapsed_callback), this);
3006
3007 g_signal_connect (m_treeview, "test-expand-row",
3008 G_CALLBACK (wxdataview_test_expand_row_callback), this);
3009
3010 g_signal_connect_after (m_treeview, "row-expanded",
3011 G_CALLBACK (wxdataview_row_expanded_callback), this);
3012
3013 return true;
3014 }
3015
3016 void wxDataViewCtrl::OnInternalIdle()
3017 {
3018 wxWindow::OnInternalIdle();
3019
3020 unsigned int cols = GetColumnCount();
3021 unsigned int i;
3022 for (i = 0; i < cols; i++)
3023 {
3024 wxDataViewColumn *col = GetColumn( i );
3025 col->OnInternalIdle();
3026 }
3027 }
3028
3029 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
3030 {
3031 if (!wxDataViewCtrlBase::AssociateModel( model ))
3032 return false;
3033
3034 GtkWxTreeModel *gtk_model = wxgtk_tree_model_new();
3035 m_internal = new wxDataViewCtrlInternal( this, model, gtk_model );
3036 gtk_model->internal = m_internal;
3037
3038 m_notifier = new wxGtkDataViewModelNotifier( gtk_model, model, this );
3039
3040 model->AddNotifier( m_notifier );
3041
3042 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_model) );
3043
3044 // unref in wxDataViewCtrlInternal
3045 // g_object_unref( gtk_model );
3046
3047 return true;
3048 }
3049
3050 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
3051 {
3052 if (!wxDataViewCtrlBase::AppendColumn(col))
3053 return false;
3054
3055 m_cols.Append( col );
3056
3057 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
3058 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
3059
3060 return true;
3061 }
3062
3063 unsigned int wxDataViewCtrl::GetColumnCount() const
3064 {
3065 return m_cols.GetCount();
3066 }
3067
3068 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
3069 {
3070 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
3071 if (!gtk_col)
3072 return NULL;
3073
3074 wxDataViewColumnList::const_iterator iter;
3075 for (iter = m_cols.begin(); iter != m_cols.end(); iter++)
3076 {
3077 wxDataViewColumn *col = *iter;
3078 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
3079 {
3080 return col;
3081 }
3082 }
3083
3084 return NULL;
3085 }
3086
3087 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
3088 {
3089 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
3090 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
3091
3092 m_cols.remove( column );
3093
3094 delete column;
3095
3096 return true;
3097 }
3098
3099 bool wxDataViewCtrl::ClearColumns()
3100 {
3101 wxDataViewColumnList::iterator iter;
3102 for (iter = m_cols.begin(); iter != m_cols.end(); iter++)
3103 {
3104 wxDataViewColumn *col = *iter;
3105 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
3106 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
3107 }
3108
3109 m_cols.clear();
3110
3111 return true;
3112 }
3113
3114 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
3115 {
3116 GtkTreeIter iter;
3117 iter.user_data = item.GetID();
3118 GtkTreePath *path = m_internal->get_path( &iter );
3119 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
3120 gtk_tree_path_free( path );
3121 }
3122
3123 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
3124 {
3125 GtkTreeIter iter;
3126 iter.user_data = item.GetID();
3127 GtkTreePath *path = m_internal->get_path( &iter );
3128 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
3129 gtk_tree_path_free( path );
3130 }
3131
3132 wxDataViewItem wxDataViewCtrl::GetSelection() const
3133 {
3134 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3135
3136 if (m_windowStyle & wxDV_MULTIPLE)
3137 {
3138 // Report the first one
3139 GtkTreeModel *model;
3140 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
3141
3142 if (list)
3143 {
3144 GtkTreePath *path = (GtkTreePath*) list->data;
3145 GtkTreeIter iter;
3146 m_internal->get_iter( &iter, path );
3147
3148 // delete list
3149 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
3150 g_list_free( list );
3151
3152 return wxDataViewItem( (void*) iter.user_data );
3153 }
3154 }
3155 else
3156 {
3157 GtkTreeIter iter;
3158 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
3159 {
3160 wxDataViewItem item( (void*) iter.user_data );
3161 return item;
3162 }
3163 }
3164
3165 return wxDataViewItem(0);
3166 }
3167
3168 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
3169 {
3170 sel.Clear();
3171
3172 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3173 if (HasFlag(wxDV_MULTIPLE))
3174 {
3175 GtkTreeModel *model;
3176 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
3177
3178 int count = 0;
3179 while (list)
3180 {
3181 GtkTreePath *path = (GtkTreePath*) list->data;
3182
3183 GtkTreeIter iter;
3184 m_internal->get_iter( &iter, path );
3185
3186 sel.Add( wxDataViewItem( (void*) iter.user_data ) );
3187
3188 list = g_list_next( list );
3189 count++;
3190 }
3191
3192 // delete list
3193 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
3194 g_list_free( list );
3195
3196 return count;
3197 }
3198 else
3199 {
3200 GtkTreeModel *model;
3201 GtkTreeIter iter;
3202 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
3203 if (has_selection)
3204 {
3205 sel.Add( wxDataViewItem( (void*) iter.user_data) );
3206 return 1;
3207 }
3208 }
3209
3210 return 0;
3211 }
3212
3213 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
3214 {
3215 GtkDisableSelectionEvents();
3216
3217 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3218
3219 gtk_tree_selection_unselect_all( selection );
3220
3221 size_t i;
3222 for (i = 0; i < sel.GetCount(); i++)
3223 {
3224 GtkTreeIter iter;
3225 iter.user_data = (gpointer) sel[i].GetID();
3226 gtk_tree_selection_select_iter( selection, &iter );
3227 }
3228
3229 GtkEnableSelectionEvents();
3230 }
3231
3232 void wxDataViewCtrl::Select( const wxDataViewItem & item )
3233 {
3234 GtkDisableSelectionEvents();
3235
3236 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3237
3238 GtkTreeIter iter;
3239 iter.user_data = (gpointer) item.GetID();
3240 gtk_tree_selection_select_iter( selection, &iter );
3241
3242 GtkEnableSelectionEvents();
3243 }
3244
3245 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
3246 {
3247 GtkDisableSelectionEvents();
3248
3249 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3250
3251 GtkTreeIter iter;
3252 iter.user_data = (gpointer) item.GetID();
3253 gtk_tree_selection_unselect_iter( selection, &iter );
3254
3255 GtkEnableSelectionEvents();
3256 }
3257
3258 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
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
3265 return gtk_tree_selection_iter_is_selected( selection, &iter );
3266 }
3267
3268 void wxDataViewCtrl::SelectAll()
3269 {
3270 GtkDisableSelectionEvents();
3271
3272 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3273
3274 gtk_tree_selection_select_all( selection );
3275
3276 GtkEnableSelectionEvents();
3277 }
3278
3279 void wxDataViewCtrl::UnselectAll()
3280 {
3281 GtkDisableSelectionEvents();
3282
3283 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3284
3285 gtk_tree_selection_unselect_all( selection );
3286
3287 GtkEnableSelectionEvents();
3288 }
3289
3290 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn *column )
3291 {
3292 GtkTreeIter iter;
3293 iter.user_data = (gpointer) item.GetID();
3294 GtkTreePath *path = m_internal->get_path( &iter );
3295 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
3296 gtk_tree_path_free( path );
3297 }
3298
3299 void wxDataViewCtrl::HitTest( const wxPoint &point,
3300 wxDataViewItem &item, wxDataViewColumn *&column ) const
3301 {
3302 item = wxDataViewItem(0);
3303 column = NULL;
3304 }
3305
3306 wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem &item,
3307 const wxDataViewColumn *column ) const
3308 {
3309 return wxRect();
3310 }
3311
3312 void wxDataViewCtrl::DoSetExpanderColumn()
3313 {
3314 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
3315 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
3316 }
3317
3318 void wxDataViewCtrl::DoSetIndent()
3319 {
3320 }
3321
3322 void wxDataViewCtrl::GtkDisableSelectionEvents()
3323 {
3324 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3325 g_signal_handlers_disconnect_by_func( selection,
3326 (gpointer) (wxdataview_selection_changed_callback), this);
3327 }
3328
3329 void wxDataViewCtrl::GtkEnableSelectionEvents()
3330 {
3331 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3332 g_signal_connect_after (selection, "changed",
3333 G_CALLBACK (wxdataview_selection_changed_callback), this);
3334 }
3335
3336 // static
3337 wxVisualAttributes
3338 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
3339 {
3340 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
3341 }
3342
3343
3344 #endif
3345 // !wxUSE_GENERICDATAVIEWCTRL
3346
3347 #endif
3348 // wxUSE_DATAVIEWCTRL