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