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