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