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