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