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