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