]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dataview.cpp
Avoid outputting the assert message twice in non-GUI code.
[wxWidgets.git] / src / gtk / dataview.cpp
CommitLineData
790b137e 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk/dataview.cpp
790b137e
RR
3// Purpose: wxDataViewCtrl GTK+2 implementation
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
790b137e
RR
13#if wxUSE_DATAVIEWCTRL
14
15#include "wx/dataview.h"
4ed7af08
RR
16
17#ifndef wxUSE_GENERICDATAVIEWCTRL
18
e4db172a
WS
19#ifndef WX_PRECOMP
20 #include "wx/log.h"
ed4b0fdc 21 #include "wx/dcclient.h"
ed2fbeb8 22 #include "wx/sizer.h"
c3c62822
PC
23 #include "wx/settings.h"
24 #include "wx/crt.h"
e4db172a
WS
25#endif
26
790b137e 27#include "wx/stockitem.h"
7ea3a0de
RR
28#include "wx/calctrl.h"
29#include "wx/popupwin.h"
69892729 30#include "wx/listimpl.cpp"
790b137e
RR
31
32#include "wx/gtk/private.h"
888dde65
RR
33#include "wx/gtk/dc.h"
34#include "wx/gtk/dcclient.h"
4d496ecb 35
9d02e494
VZ
36#include "wx/gtk/private/gdkconv.h"
37using namespace wxGTKImpl;
38
790b137e 39//-----------------------------------------------------------------------------
790b137e
RR
40//-----------------------------------------------------------------------------
41
8c2654ce 42static wxDataViewCtrlInternal *gs_internal = NULL;
3b6280be
RR
43
44class wxGtkTreeModelNode;
ef427989 45
0be79c8a
RR
46extern "C" {
47typedef struct _GtkWxTreeModel GtkWxTreeModel;
48}
49
af110130
RR
50//-----------------------------------------------------------------------------
51// wxDataViewCtrlInternal
52//-----------------------------------------------------------------------------
53
69892729 54WX_DECLARE_LIST(wxDataViewItem, ItemList);
a76c2f37 55WX_DEFINE_LIST(ItemList)
69892729 56
8c2654ce 57class WXDLLIMPEXP_ADV wxDataViewCtrlInternal
af110130
RR
58{
59public:
7dea20f6 60 wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model );
af110130 61 ~wxDataViewCtrlInternal();
b9db5f30 62
f6f0ef85 63 // model iface
2056dede 64 GtkTreeModelFlags get_flags();
af110130
RR
65 gboolean get_iter( GtkTreeIter *iter, GtkTreePath *path );
66 GtkTreePath *get_path( GtkTreeIter *iter);
af110130
RR
67 gboolean iter_next( GtkTreeIter *iter );
68 gboolean iter_children( GtkTreeIter *iter, GtkTreeIter *parent);
69 gboolean iter_has_child( GtkTreeIter *iter );
70 gint iter_n_children( GtkTreeIter *iter );
71 gboolean iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n );
72 gboolean iter_parent( GtkTreeIter *iter, GtkTreeIter *child );
b9db5f30 73
f6f0ef85 74 // dnd iface
8c2654ce 75
15cac64f 76 bool EnableDragSource( const wxDataFormat &format );
e4de825e 77 bool EnableDropTarget( const wxDataFormat &format );
8c2654ce 78
f6f0ef85
RR
79 gboolean row_draggable( GtkTreeDragSource *drag_source, GtkTreePath *path );
80 gboolean drag_data_delete( GtkTreeDragSource *drag_source, GtkTreePath* path );
7857346a 81 gboolean drag_data_get( GtkTreeDragSource *drag_source, GtkTreePath *path,
f6f0ef85 82 GtkSelectionData *selection_data );
7857346a 83 gboolean drag_data_received( GtkTreeDragDest *drag_dest, GtkTreePath *dest,
f6f0ef85 84 GtkSelectionData *selection_data );
7857346a 85 gboolean row_drop_possible( GtkTreeDragDest *drag_dest, GtkTreePath *dest_path,
f6f0ef85
RR
86 GtkSelectionData *selection_data );
87
88 // notifactions from wxDataViewModel
af110130 89 bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
469d3e9b 90 bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
d8331a01
RR
91 bool ItemChanged( const wxDataViewItem &item );
92 bool ValueChanged( const wxDataViewItem &item, unsigned int col );
93 bool Cleared();
af110130 94 void Resort();
b9db5f30 95
f6f0ef85 96 // sorting interface
b8b7b087 97 void SetSortOrder( GtkSortType sort_order ) { m_sort_order = sort_order; }
1f226ad8 98 GtkSortType GetSortOrder() const { return m_sort_order; }
7ee7191c 99
40196b1e 100 void SetSortColumn( int column ) { m_sort_column = column; }
1f226ad8 101 int GetSortColumn() const { return m_sort_column; }
b9db5f30 102
d32332aa
RR
103 void SetDataViewSortColumn( wxDataViewColumn *column ) { m_dataview_sort_column = column; }
104 wxDataViewColumn *GetDataViewSortColumn() { return m_dataview_sort_column; }
b9db5f30 105
40196b1e 106 bool IsSorted() { return (m_sort_column >= 0); }
b9db5f30 107
f6f0ef85
RR
108 // accessors
109 wxDataViewModel* GetDataViewModel() { return m_wx_model; }
1f226ad8 110 const wxDataViewModel* GetDataViewModel() const { return m_wx_model; }
f6f0ef85
RR
111 wxDataViewCtrl* GetOwner() { return m_owner; }
112 GtkWxTreeModel* GetGtkModel() { return m_gtk_model; }
b9db5f30 113
8650c4ba
RR
114 // item can be deleted already in the model
115 int GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item );
116
af110130
RR
117protected:
118 void InitTree();
119 wxGtkTreeModelNode *FindNode( const wxDataViewItem &item );
120 wxGtkTreeModelNode *FindNode( GtkTreeIter *iter );
121 wxGtkTreeModelNode *FindParentNode( const wxDataViewItem &item );
122 wxGtkTreeModelNode *FindParentNode( GtkTreeIter *iter );
123 void BuildBranch( wxGtkTreeModelNode *branch );
b9db5f30 124
af110130
RR
125private:
126 wxGtkTreeModelNode *m_root;
127 wxDataViewModel *m_wx_model;
128 GtkWxTreeModel *m_gtk_model;
129 wxDataViewCtrl *m_owner;
b8b7b087 130 GtkSortType m_sort_order;
d32332aa 131 wxDataViewColumn *m_dataview_sort_column;
40196b1e 132 int m_sort_column;
8c2654ce 133
15cac64f
RR
134 GtkTargetEntry m_dragSourceTargetEntry;
135 wxCharBuffer m_dragSourceTargetEntryTarget;
591cc82d 136 wxDataObject *m_dragDataObject;
8c2654ce 137
e4de825e
RR
138 GtkTargetEntry m_dropTargetTargetEntry;
139 wxCharBuffer m_dropTargetTargetEntryTarget;
591cc82d 140 wxDataObject *m_dropDataObject;
af110130 141};
ef427989 142
af110130
RR
143
144//-----------------------------------------------------------------------------
145// wxGtkTreeModelNode
146//-----------------------------------------------------------------------------
147
8c2654ce 148static
40196b1e
RR
149int LINKAGEMODE wxGtkTreeModelChildCmp( void** id1, void** id2 )
150{
8c2654ce
PC
151 int ret = gs_internal->GetDataViewModel()->Compare( *id1, *id2,
152 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
b9db5f30 153
40196b1e
RR
154 return ret;
155}
af110130
RR
156
157WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode*, wxGtkTreeModelNodes );
40196b1e 158WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren );
3b6280be
RR
159
160class wxGtkTreeModelNode
161{
162public:
ef427989 163 wxGtkTreeModelNode( wxGtkTreeModelNode* parent, const wxDataViewItem &item,
c2489d8e 164 wxDataViewCtrlInternal *internal )
b9db5f30
VS
165 {
166 m_parent = parent;
d5025dc0 167 m_item = item;
0be79c8a 168 m_internal = internal;
3b6280be 169 }
b9db5f30 170
3b6280be 171 ~wxGtkTreeModelNode()
b9db5f30 172 {
023eecb7 173 size_t count = m_nodes.GetCount();
3b6280be
RR
174 size_t i;
175 for (i = 0; i < count; i++)
176 {
af110130 177 wxGtkTreeModelNode *child = m_nodes.Item( i );
3b6280be
RR
178 delete child;
179 }
180 }
181
b9db5f30
VS
182 unsigned int AddNode( wxGtkTreeModelNode* child )
183 {
af110130 184 m_nodes.Add( child );
b9db5f30 185
40196b1e 186 void *id = child->GetItem().GetID();
b9db5f30 187
40196b1e 188 m_children.Add( id );
0bd26819
RR
189
190 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
40196b1e 191 {
8c2654ce 192 gs_internal = m_internal;
40196b1e
RR
193 m_children.Sort( &wxGtkTreeModelChildCmp );
194 return m_children.Index( id );
195 }
0bd26819 196
40196b1e 197 return m_children.GetCount()-1;
af110130 198 }
b9db5f30 199
af110130
RR
200 unsigned int AddLeave( void* id )
201 {
40196b1e 202 m_children.Add( id );
0bd26819
RR
203
204 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
40196b1e 205 {
8c2654ce 206 gs_internal = m_internal;
40196b1e
RR
207 m_children.Sort( &wxGtkTreeModelChildCmp );
208 return m_children.Index( id );
209 }
0bd26819 210
40196b1e 211 return m_children.GetCount()-1;
af110130 212 }
b9db5f30 213
af110130
RR
214 void DeleteChild( void* id )
215 {
40196b1e 216 m_children.Remove( id );
b9db5f30 217
40196b1e
RR
218 unsigned int count = m_nodes.GetCount();
219 unsigned int pos;
af110130 220 for (pos = 0; pos < count; pos++)
b9db5f30 221 {
af110130
RR
222 wxGtkTreeModelNode *node = m_nodes.Item( pos );
223 if (node->GetItem().GetID() == id)
224 {
225 m_nodes.RemoveAt( pos );
226 delete node;
227 break;
228 }
229 }
af110130 230 }
b9db5f30
VS
231
232 wxGtkTreeModelNode* GetParent()
3b6280be 233 { return m_parent; }
b9db5f30 234 wxGtkTreeModelNodes &GetNodes()
af110130 235 { return m_nodes; }
b9db5f30 236 wxGtkTreeModelChildren &GetChildren()
40196b1e 237 { return m_children; }
b9db5f30 238
c2489d8e
FM
239 unsigned int GetChildCount() const { return m_children.GetCount(); }
240 unsigned int GetNodesCount() const { return m_nodes.GetCount(); }
3b6280be
RR
241
242 wxDataViewItem &GetItem() { return m_item; }
0be79c8a 243 wxDataViewCtrlInternal *GetInternal() { return m_internal; }
b9db5f30 244
4508fcd2 245 void Resort();
b9db5f30 246
3b6280be 247private:
0be79c8a 248 wxGtkTreeModelNode *m_parent;
af110130 249 wxGtkTreeModelNodes m_nodes;
40196b1e 250 wxGtkTreeModelChildren m_children;
b9db5f30 251 wxDataViewItem m_item;
0be79c8a 252 wxDataViewCtrlInternal *m_internal;
3b6280be
RR
253};
254
ef427989 255
790b137e
RR
256//-----------------------------------------------------------------------------
257// data
258//-----------------------------------------------------------------------------
259
260extern bool g_blockEventsOnDrag;
261
262//-----------------------------------------------------------------------------
e0062c04 263// define new GTK+ class wxGtkTreeModel
790b137e
RR
264//-----------------------------------------------------------------------------
265
266extern "C" {
267
e0062c04
RR
268#define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
269#define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
270#define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
271#define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
272#define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
273#define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
790b137e 274
e0062c04 275GType gtk_wx_tree_model_get_type (void);
790b137e 276
e0062c04 277typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass;
790b137e 278
e0062c04 279struct _GtkWxTreeModel
790b137e
RR
280{
281 GObject parent;
282
283 /*< private >*/
284 gint stamp;
55fbde12 285 wxDataViewCtrlInternal *internal;
790b137e
RR
286};
287
e0062c04 288struct _GtkWxTreeModelClass
790b137e 289{
e152afc3 290 GObjectClass list_parent_class;
790b137e
RR
291};
292
e0062c04 293static GtkWxTreeModel *wxgtk_tree_model_new (void);
773cca48
RR
294static void wxgtk_tree_model_init (GtkWxTreeModel *tree_model);
295static void wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass);
85136e3b
RR
296
297static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface);
298static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface);
299static void wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface);
300static void wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface);
301
e0062c04
RR
302static void wxgtk_tree_model_finalize (GObject *object);
303static GtkTreeModelFlags wxgtk_tree_model_get_flags (GtkTreeModel *tree_model);
304static gint wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model);
305static GType wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
93763ad5 306 gint index);
e0062c04 307static gboolean wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
93763ad5
WS
308 GtkTreeIter *iter,
309 GtkTreePath *path);
e0062c04 310static GtkTreePath *wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
93763ad5 311 GtkTreeIter *iter);
e0062c04 312static void wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
93763ad5
WS
313 GtkTreeIter *iter,
314 gint column,
315 GValue *value);
e0062c04 316static gboolean wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
93763ad5 317 GtkTreeIter *iter);
e0062c04 318static gboolean wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
93763ad5
WS
319 GtkTreeIter *iter,
320 GtkTreeIter *parent);
e0062c04 321static gboolean wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
93763ad5 322 GtkTreeIter *iter);
e0062c04 323static gint wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
93763ad5 324 GtkTreeIter *iter);
e0062c04 325static gboolean wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
93763ad5
WS
326 GtkTreeIter *iter,
327 GtkTreeIter *parent,
328 gint n);
e0062c04 329static gboolean wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
93763ad5
WS
330 GtkTreeIter *iter,
331 GtkTreeIter *child);
790b137e 332
773cca48
RR
333/* sortable */
334static gboolean wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
85136e3b 335 gint *sort_column_id,
c2489d8e 336 GtkSortType *order);
773cca48 337static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
85136e3b
RR
338 gint sort_column_id,
339 GtkSortType order);
773cca48 340static void wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
85136e3b
RR
341 gint sort_column_id,
342 GtkTreeIterCompareFunc func,
343 gpointer data,
344 GtkDestroyNotify destroy);
773cca48 345static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
85136e3b
RR
346 GtkTreeIterCompareFunc func,
347 gpointer data,
348 GtkDestroyNotify destroy);
773cca48
RR
349static gboolean wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable);
350
85136e3b
RR
351/* drag'n'drop */
352static gboolean wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source,
353 GtkTreePath *path);
354static gboolean wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source,
355 GtkTreePath *path);
356static gboolean wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source,
357 GtkTreePath *path,
358 GtkSelectionData *selection_data);
359static gboolean wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest,
360 GtkTreePath *dest,
361 GtkSelectionData *selection_data);
362static gboolean wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest,
363 GtkTreePath *dest_path,
c2489d8e 364 GtkSelectionData *selection_data);
773cca48
RR
365
366
e152afc3 367static GObjectClass *list_parent_class = NULL;
790b137e
RR
368
369GType
e0062c04 370gtk_wx_tree_model_get_type (void)
790b137e 371{
e0062c04 372 static GType tree_model_type = 0;
790b137e 373
e0062c04 374 if (!tree_model_type)
790b137e 375 {
e0062c04 376 const GTypeInfo tree_model_info =
93763ad5 377 {
e0062c04 378 sizeof (GtkWxTreeModelClass),
93763ad5
WS
379 NULL, /* base_init */
380 NULL, /* base_finalize */
e0062c04 381 (GClassInitFunc) wxgtk_tree_model_class_init,
93763ad5
WS
382 NULL, /* class_finalize */
383 NULL, /* class_data */
e0062c04 384 sizeof (GtkWxTreeModel),
93763ad5 385 0,
e0062c04 386 (GInstanceInitFunc) wxgtk_tree_model_init,
93763ad5 387 };
790b137e 388
773cca48
RR
389 static const GInterfaceInfo tree_model_iface_info =
390 {
391 (GInterfaceInitFunc) wxgtk_tree_model_tree_model_init,
392 NULL,
393 NULL
394 };
395
396 static const GInterfaceInfo sortable_iface_info =
397 {
398 (GInterfaceInitFunc) wxgtk_tree_model_sortable_init,
399 NULL,
400 NULL
401 };
790b137e 402
85136e3b
RR
403 static const GInterfaceInfo drag_source_iface_info =
404 {
405 (GInterfaceInitFunc) wxgtk_tree_model_drag_source_init,
406 NULL,
407 NULL
408 };
409
410 static const GInterfaceInfo drag_dest_iface_info =
411 {
412 (GInterfaceInitFunc) wxgtk_tree_model_drag_dest_init,
413 NULL,
414 NULL
415 };
416
773cca48 417 tree_model_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxTreeModel",
e0062c04 418 &tree_model_info, (GTypeFlags)0 );
790b137e 419
773cca48
RR
420 g_type_add_interface_static (tree_model_type,
421 GTK_TYPE_TREE_MODEL,
422 &tree_model_iface_info);
423 g_type_add_interface_static (tree_model_type,
424 GTK_TYPE_TREE_SORTABLE,
425 &sortable_iface_info);
85136e3b
RR
426 g_type_add_interface_static (tree_model_type,
427 GTK_TYPE_TREE_DRAG_DEST,
428 &drag_dest_iface_info);
429 g_type_add_interface_static (tree_model_type,
430 GTK_TYPE_TREE_DRAG_SOURCE,
431 &drag_source_iface_info);
790b137e
RR
432 }
433
773cca48 434 return tree_model_type;
790b137e
RR
435}
436
e0062c04
RR
437static GtkWxTreeModel *
438wxgtk_tree_model_new(void)
1557c77b 439{
e0062c04 440 GtkWxTreeModel *retval = (GtkWxTreeModel *) g_object_new (GTK_TYPE_WX_TREE_MODEL, NULL);
e152afc3 441 return retval;
1557c77b
RR
442}
443
790b137e 444static void
e0062c04 445wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass)
790b137e 446{
e152afc3
RR
447 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
448 GObjectClass *object_class = (GObjectClass*) klass;
e0062c04 449 object_class->finalize = wxgtk_tree_model_finalize;
790b137e
RR
450}
451
452static void
e0062c04
RR
453wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface)
454{
455 iface->get_flags = wxgtk_tree_model_get_flags;
456 iface->get_n_columns = wxgtk_tree_model_get_n_columns;
457 iface->get_column_type = wxgtk_tree_model_get_column_type;
458 iface->get_iter = wxgtk_tree_model_get_iter;
459 iface->get_path = wxgtk_tree_model_get_path;
460 iface->get_value = wxgtk_tree_model_get_value;
461 iface->iter_next = wxgtk_tree_model_iter_next;
462 iface->iter_children = wxgtk_tree_model_iter_children;
463 iface->iter_has_child = wxgtk_tree_model_iter_has_child;
464 iface->iter_n_children = wxgtk_tree_model_iter_n_children;
465 iface->iter_nth_child = wxgtk_tree_model_iter_nth_child;
466 iface->iter_parent = wxgtk_tree_model_iter_parent;
790b137e
RR
467}
468
773cca48
RR
469static void
470wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface)
471{
94b1f7bc
RR
472 iface->get_sort_column_id = wxgtk_tree_model_get_sort_column_id;
473 iface->set_sort_column_id = wxgtk_tree_model_set_sort_column_id;
474 iface->set_sort_func = wxgtk_tree_model_set_sort_func;
475 iface->set_default_sort_func = wxgtk_tree_model_set_default_sort_func;
476 iface->has_default_sort_func = wxgtk_tree_model_has_default_sort_func;
773cca48
RR
477}
478
7857346a 479static void
85136e3b
RR
480wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface)
481{
482 iface->row_draggable = wxgtk_tree_model_row_draggable;
483 iface->drag_data_delete = wxgtk_tree_model_drag_data_delete;
484 iface->drag_data_get = wxgtk_tree_model_drag_data_get;
485}
486
7857346a 487static void
85136e3b
RR
488wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface)
489{
490 iface->drag_data_received = wxgtk_tree_model_drag_data_received;
491 iface->row_drop_possible = wxgtk_tree_model_row_drop_possible;
492}
493
790b137e 494static void
e0062c04 495wxgtk_tree_model_init (GtkWxTreeModel *tree_model)
790b137e 496{
55fbde12 497 tree_model->internal = NULL;
e0062c04 498 tree_model->stamp = g_random_int();
790b137e
RR
499}
500
501static void
e0062c04 502wxgtk_tree_model_finalize (GObject *object)
790b137e 503{
790b137e 504 /* must chain up */
e152afc3 505 (* list_parent_class->finalize) (object);
790b137e 506}
93763ad5 507
790b137e
RR
508} // extern "C"
509
510//-----------------------------------------------------------------------------
e0062c04
RR
511// implement callbacks from wxGtkTreeModel class by letting
512// them call the methods of wxWidgets' wxDataViewModel
790b137e
RR
513//-----------------------------------------------------------------------------
514
515static GtkTreeModelFlags
e0062c04 516wxgtk_tree_model_get_flags (GtkTreeModel *tree_model)
790b137e 517{
2056dede
RR
518 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
519 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), (GtkTreeModelFlags)0 );
790b137e 520
2056dede 521 return wxtree_model->internal->get_flags();
790b137e
RR
522}
523
524static gint
e0062c04 525wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model)
790b137e 526{
e0062c04
RR
527 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
528 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), 0);
790b137e 529
55fbde12 530 return wxtree_model->internal->GetDataViewModel()->GetColumnCount();
790b137e
RR
531}
532
533static GType
e0062c04 534wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
93763ad5 535 gint index)
790b137e 536{
e0062c04
RR
537 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
538 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), G_TYPE_INVALID);
790b137e 539
1557c77b 540 GType gtype = G_TYPE_INVALID;
93763ad5 541
55fbde12 542 wxString wxtype = wxtree_model->internal->GetDataViewModel()->GetColumnType( (unsigned int) index );
93763ad5 543
1557c77b
RR
544 if (wxtype == wxT("string"))
545 gtype = G_TYPE_STRING;
72a3ac9b
VZ
546 else
547 {
200c18cc
RR
548 gtype = G_TYPE_POINTER;
549 // wxFAIL_MSG( wxT("non-string columns not supported for searching yet") );
72a3ac9b 550 }
790b137e
RR
551
552 return gtype;
553}
554
555static gboolean
e0062c04 556wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
93763ad5
WS
557 GtkTreeIter *iter,
558 GtkTreePath *path)
790b137e 559{
e0062c04
RR
560 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
561 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
790b137e
RR
562 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
563
55fbde12 564 return wxtree_model->internal->get_iter( iter, path );
790b137e
RR
565}
566
567static GtkTreePath *
e0062c04 568wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
93763ad5 569 GtkTreeIter *iter)
790b137e 570{
e0062c04
RR
571 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
572 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), NULL);
573 g_return_val_if_fail (iter->stamp == GTK_WX_TREE_MODEL (wxtree_model)->stamp, NULL);
93763ad5 574
55fbde12 575 return wxtree_model->internal->get_path( iter );
790b137e
RR
576}
577
578static void
e0062c04 579wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
93763ad5
WS
580 GtkTreeIter *iter,
581 gint column,
582 GValue *value)
790b137e 583{
e0062c04
RR
584 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
585 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model) );
239eaa41 586
55fbde12 587 wxDataViewModel *model = wxtree_model->internal->GetDataViewModel();
9861f022 588 wxString mtype = model->GetColumnType( (unsigned int) column );
1557c77b
RR
589 if (mtype == wxT("string"))
590 {
3f3af7e7 591 wxVariant variant;
1557c77b 592 g_value_init( value, G_TYPE_STRING );
9d52aad3 593 wxDataViewItem item( (void*) iter->user_data );
e0062c04 594 model->GetValue( variant, item, (unsigned int) column );
72a3ac9b 595
e0062c04 596 g_value_set_string( value, variant.GetString().utf8_str() );
1557c77b
RR
597 }
598 else
599 {
9a83f860 600 wxFAIL_MSG( wxT("non-string columns not supported yet") );
1557c77b 601 }
790b137e
RR
602}
603
604static gboolean
e0062c04 605wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
93763ad5 606 GtkTreeIter *iter)
790b137e 607{
e0062c04 608 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
7857346a 609
8a568372
RR
610 // This happens when clearing the view by calling .._set_model( NULL );
611 if (iter->stamp == 0) return FALSE;
33ba5a05 612
e0062c04 613 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
e0062c04 614 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
7857346a 615
55fbde12 616 return wxtree_model->internal->iter_next( iter );
790b137e
RR
617}
618
619static gboolean
e0062c04 620wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
93763ad5
WS
621 GtkTreeIter *iter,
622 GtkTreeIter *parent)
790b137e 623{
e0062c04
RR
624 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
625 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
e0062c04 626 g_return_val_if_fail (wxtree_model->stamp == parent->stamp, FALSE);
790b137e 627
55fbde12 628 return wxtree_model->internal->iter_children( iter, parent );
790b137e
RR
629}
630
631static gboolean
e0062c04 632wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
93763ad5 633 GtkTreeIter *iter)
790b137e 634{
e0062c04
RR
635 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
636 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
e0062c04 637 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
d5025dc0 638
55fbde12 639 return wxtree_model->internal->iter_has_child( iter );
790b137e
RR
640}
641
642static gint
e0062c04 643wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
93763ad5 644 GtkTreeIter *iter)
790b137e 645{
e0062c04 646 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
8a568372 647 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), 0);
1bfabf36 648 g_return_val_if_fail ( !iter || wxtree_model->stamp == iter->stamp, 0);
c2489d8e 649
55fbde12 650 return wxtree_model->internal->iter_n_children( iter );
790b137e
RR
651}
652
653static gboolean
e0062c04 654wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
93763ad5
WS
655 GtkTreeIter *iter,
656 GtkTreeIter *parent,
657 gint n)
790b137e 658{
e0062c04
RR
659 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
660 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
661
55fbde12 662 return wxtree_model->internal->iter_nth_child( iter, parent, n );
790b137e
RR
663}
664
665static gboolean
e0062c04 666wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
93763ad5
WS
667 GtkTreeIter *iter,
668 GtkTreeIter *child)
790b137e 669{
e0062c04
RR
670 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
671 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
e0062c04 672 g_return_val_if_fail (wxtree_model->stamp == child->stamp, FALSE);
b9db5f30 673
55fbde12 674 return wxtree_model->internal->iter_parent( iter, child );
790b137e
RR
675}
676
85136e3b 677/* drag'n'drop iface */
7857346a 678static gboolean
85136e3b
RR
679wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source,
680 GtkTreePath *path)
681{
f6f0ef85
RR
682 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
683 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 684
f6f0ef85 685 return wxtree_model->internal->row_draggable( drag_source, path );
85136e3b
RR
686}
687
7857346a 688static gboolean
85136e3b
RR
689wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source,
690 GtkTreePath *path)
691{
f6f0ef85
RR
692 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
693 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 694
f6f0ef85 695 return wxtree_model->internal->drag_data_delete( drag_source, path );
85136e3b
RR
696}
697
7857346a 698static gboolean
85136e3b
RR
699wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source,
700 GtkTreePath *path,
701 GtkSelectionData *selection_data)
702{
f6f0ef85
RR
703 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
704 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 705
f6f0ef85 706#if 0
85136e3b 707 wxPrintf( "drag_get_data\n");
7857346a 708
f6f0ef85
RR
709 wxGtkString atom_selection(gdk_atom_name(selection_data->selection));
710 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection) );
7857346a 711
f6f0ef85
RR
712 wxGtkString atom_target(gdk_atom_name(selection_data->target));
713 wxPrintf( "target %s\n", wxString::FromAscii(atom_target) );
7857346a 714
f6f0ef85
RR
715 wxGtkString atom_type(gdk_atom_name(selection_data->type));
716 wxPrintf( "type %s\n", wxString::FromAscii(atom_type) );
717
718 wxPrintf( "format %d\n", selection_data->format );
719#endif
7857346a 720
f6f0ef85 721 return wxtree_model->internal->drag_data_get( drag_source, path, selection_data );
85136e3b
RR
722}
723
7857346a 724static gboolean
85136e3b
RR
725wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest,
726 GtkTreePath *dest,
727 GtkSelectionData *selection_data)
728{
f6f0ef85
RR
729 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest;
730 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 731
f6f0ef85 732 return wxtree_model->internal->drag_data_received( drag_dest, dest, selection_data );
85136e3b
RR
733}
734
7857346a 735static gboolean
85136e3b
RR
736wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest,
737 GtkTreePath *dest_path,
c2489d8e 738 GtkSelectionData *selection_data)
85136e3b 739{
f6f0ef85
RR
740 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest;
741 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 742
f6f0ef85 743 return wxtree_model->internal->row_drop_possible( drag_dest, dest_path, selection_data );
85136e3b
RR
744}
745
746/* sortable iface */
7857346a 747static gboolean
85136e3b
RR
748wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
749 gint *sort_column_id,
750 GtkSortType *order)
773cca48 751{
f6f0ef85 752 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) sortable;
94b1f7bc
RR
753
754 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE);
755
f6f0ef85 756 if (!wxtree_model->internal->IsSorted())
40196b1e
RR
757 {
758 if (sort_column_id)
759 *sort_column_id = -1;
b9db5f30 760
40196b1e
RR
761 return TRUE;
762 }
763
764
94b1f7bc 765 if (sort_column_id)
f6f0ef85 766 *sort_column_id = wxtree_model->internal->GetSortColumn();
b9db5f30 767
94b1f7bc 768 if (order)
f6f0ef85 769 *order = wxtree_model->internal->GetSortOrder();
b9db5f30 770
94b1f7bc 771 return TRUE;
773cca48
RR
772}
773
d32332aa
RR
774wxDataViewColumn *gs_lastLeftClickHeader = NULL;
775
7857346a 776static void
85136e3b
RR
777wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
778 gint sort_column_id,
779 GtkSortType order)
773cca48 780{
94b1f7bc 781 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) sortable;
94b1f7bc
RR
782 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
783
d32332aa 784 tree_model->internal->SetDataViewSortColumn( gs_lastLeftClickHeader );
b9db5f30 785
d32332aa
RR
786 if ((sort_column_id != (gint) tree_model->internal->GetSortColumn()) ||
787 (order != tree_model->internal->GetSortOrder()))
788 {
789 tree_model->internal->SetSortColumn( sort_column_id );
790 tree_model->internal->SetSortOrder( order );
b9db5f30 791
d32332aa 792 gtk_tree_sortable_sort_column_changed (sortable);
b9db5f30 793
d32332aa
RR
794 tree_model->internal->GetDataViewModel()->Resort();
795 }
a84c5b6f 796
d32332aa
RR
797 if (gs_lastLeftClickHeader)
798 {
b9db5f30 799 wxDataViewCtrl *dv = tree_model->internal->GetOwner();
d32332aa
RR
800 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, dv->GetId() );
801 event.SetDataViewColumn( gs_lastLeftClickHeader );
802 event.SetModel( dv->GetModel() );
937013e0 803 dv->HandleWindowEvent( event );
d32332aa 804 }
b9db5f30 805
d32332aa 806 gs_lastLeftClickHeader = NULL;
773cca48
RR
807}
808
7857346a 809static void
85136e3b
RR
810wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
811 gint WXUNUSED(sort_column_id),
812 GtkTreeIterCompareFunc func,
813 gpointer WXUNUSED(data),
c2489d8e 814 GtkDestroyNotify WXUNUSED(destroy))
773cca48 815{
94b1f7bc
RR
816 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
817 g_return_if_fail (func != NULL);
773cca48
RR
818}
819
c2489d8e
FM
820static void
821wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
822 GtkTreeIterCompareFunc func,
823 gpointer WXUNUSED(data),
824 GtkDestroyNotify WXUNUSED(destroy))
773cca48 825{
94b1f7bc 826 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
94b1f7bc
RR
827 g_return_if_fail (func != NULL);
828
c2489d8e
FM
829 //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
830 // TODO: remove this code
773cca48
RR
831}
832
c2489d8e
FM
833static gboolean
834wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable)
773cca48 835{
c5c5395b 836 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE );
7857346a 837
773cca48
RR
838 return FALSE;
839}
840
e152afc3 841//-----------------------------------------------------------------------------
ecc32226
RR
842// define new GTK+ class GtkWxRendererText
843//-----------------------------------------------------------------------------
844
845extern "C" {
846
847#define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ())
848#define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText))
849#define GTK_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
850#define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT))
851#define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT))
852#define GTK_WX_CELL_RENDERER_TEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass))
853
854GType gtk_wx_cell_renderer_text_get_type (void);
855
856typedef struct _GtkWxCellRendererText GtkWxCellRendererText;
857typedef struct _GtkWxCellRendererTextClass GtkWxCellRendererTextClass;
858
859struct _GtkWxCellRendererText
860{
861 GtkCellRendererText parent;
03647350 862
ecc32226
RR
863 wxDataViewRenderer *wx_renderer;
864};
865
866struct _GtkWxCellRendererTextClass
867{
868 GtkCellRendererTextClass cell_parent_class;
869};
870
871
872static GtkWxCellRendererText *gtk_wx_cell_renderer_text_new (void);
873static void gtk_wx_cell_renderer_text_init (
874 GtkWxCellRendererText *cell );
875static void gtk_wx_cell_renderer_text_class_init(
876 GtkWxCellRendererTextClass *klass );
877static void gtk_wx_cell_renderer_text_finalize (
878 GObject *object );
879static GtkCellEditable *gtk_wx_cell_renderer_text_start_editing(
880 GtkCellRenderer *cell,
881 GdkEvent *event,
882 GtkWidget *widget,
883 const gchar *path,
884 GdkRectangle *background_area,
885 GdkRectangle *cell_area,
886 GtkCellRendererState flags );
887
888
889static GObjectClass *text_cell_parent_class = NULL;
ecc32226
RR
890
891} // extern "C"
892
893GType
894gtk_wx_cell_renderer_text_get_type (void)
895{
896 static GType cell_wx_type = 0;
897
898 if (!cell_wx_type)
899 {
900 const GTypeInfo cell_wx_info =
901 {
902 sizeof (GtkWxCellRendererTextClass),
903 NULL, /* base_init */
904 NULL, /* base_finalize */
905 (GClassInitFunc) gtk_wx_cell_renderer_text_class_init,
906 NULL, /* class_finalize */
907 NULL, /* class_data */
908 sizeof (GtkWxCellRendererText),
909 0, /* n_preallocs */
910 (GInstanceInitFunc) gtk_wx_cell_renderer_text_init,
911 };
912
913 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT,
914 "GtkWxCellRendererText", &cell_wx_info, (GTypeFlags)0 );
915 }
916
917 return cell_wx_type;
918}
919
920static void
921gtk_wx_cell_renderer_text_init (GtkWxCellRendererText *cell)
922{
923 cell->wx_renderer = NULL;
924}
925
926static void
927gtk_wx_cell_renderer_text_class_init (GtkWxCellRendererTextClass *klass)
928{
929 GObjectClass *object_class = G_OBJECT_CLASS (klass);
930 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
931
932 text_cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
933
934 object_class->finalize = gtk_wx_cell_renderer_text_finalize;
935
936 cell_class->start_editing = gtk_wx_cell_renderer_text_start_editing;
937}
938
939static void
940gtk_wx_cell_renderer_text_finalize (GObject *object)
941{
942 /* must chain up */
943 (* G_OBJECT_CLASS (text_cell_parent_class)->finalize) (object);
944}
945
946GtkWxCellRendererText*
947gtk_wx_cell_renderer_text_new (void)
948{
949 return (GtkWxCellRendererText*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT, NULL);
950}
951
952static GtkCellEditable *gtk_wx_cell_renderer_text_start_editing(
953 GtkCellRenderer *gtk_renderer,
954 GdkEvent *gdk_event,
955 GtkWidget *widget,
956 const gchar *path,
957 GdkRectangle *background_area,
958 GdkRectangle *cell_area,
959 GtkCellRendererState flags )
960{
961 GtkWxCellRendererText *wxgtk_renderer = (GtkWxCellRendererText *) gtk_renderer;
962 wxDataViewRenderer *wx_renderer = wxgtk_renderer->wx_renderer;
963
964 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
965 GtkTreeIter iter;
966 wx_renderer->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, treepath );
967 wxDataViewItem item( (void*) iter.user_data );;
968 gtk_tree_path_free( treepath );
969
970 wxDataViewColumn *column = wx_renderer->GetOwner();
971 wxDataViewCtrl *dv = column->GetOwner();
972 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, dv->GetId() );
973 event.SetDataViewColumn( column );
974 event.SetModel( dv->GetModel() );
975 event.SetColumn( wx_renderer->GetOwner()->GetModelColumn() );
976 event.SetItem( item );
977 dv->HandleWindowEvent( event );
03647350 978
ecc32226
RR
979 if (event.IsAllowed())
980 return GTK_CELL_RENDERER_CLASS(text_cell_parent_class)->
981 start_editing( gtk_renderer, gdk_event, widget, path, background_area, cell_area, flags );
982 else
983 return NULL;
984}
985
986//-----------------------------------------------------------------------------
987// define new GTK+ class GtkWxCellRenderer
e152afc3
RR
988//-----------------------------------------------------------------------------
989
990extern "C" {
991
992#define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
993#define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
994#define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
995#define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
996#define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
997#define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
998
999GType gtk_wx_cell_renderer_get_type (void);
1000
1001typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
1002typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
1003
1004struct _GtkWxCellRenderer
1005{
1006 GtkCellRenderer parent;
1007
1008 /*< private >*/
baa9ebc4 1009 wxDataViewCustomRenderer *cell;
4d496ecb 1010 guint32 last_click;
e152afc3
RR
1011};
1012
1013struct _GtkWxCellRendererClass
1014{
1015 GtkCellRendererClass cell_parent_class;
e152afc3
RR
1016};
1017
1018
1019static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
553f7d8f
RR
1020static void gtk_wx_cell_renderer_init (
1021 GtkWxCellRenderer *cell );
1022static void gtk_wx_cell_renderer_class_init(
1023 GtkWxCellRendererClass *klass );
1024static void gtk_wx_cell_renderer_finalize (
1025 GObject *object );
1026static void gtk_wx_cell_renderer_get_size (
1027 GtkCellRenderer *cell,
93763ad5
WS
1028 GtkWidget *widget,
1029 GdkRectangle *rectangle,
1030 gint *x_offset,
1031 gint *y_offset,
1032 gint *width,
1033 gint *height );
553f7d8f
RR
1034static void gtk_wx_cell_renderer_render (
1035 GtkCellRenderer *cell,
93763ad5
WS
1036 GdkWindow *window,
1037 GtkWidget *widget,
1038 GdkRectangle *background_area,
1039 GdkRectangle *cell_area,
1040 GdkRectangle *expose_area,
1041 GtkCellRendererState flags );
553f7d8f
RR
1042static gboolean gtk_wx_cell_renderer_activate(
1043 GtkCellRenderer *cell,
1044 GdkEvent *event,
1045 GtkWidget *widget,
1046 const gchar *path,
1047 GdkRectangle *background_area,
1048 GdkRectangle *cell_area,
1049 GtkCellRendererState flags );
1e510b1e
RR
1050static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
1051 GtkCellRenderer *cell,
1052 GdkEvent *event,
1053 GtkWidget *widget,
1054 const gchar *path,
1055 GdkRectangle *background_area,
1056 GdkRectangle *cell_area,
1057 GtkCellRendererState flags );
e8375af8 1058
e152afc3
RR
1059
1060static GObjectClass *cell_parent_class = NULL;
1061
1062} // extern "C"
1063
93763ad5 1064GType
e152afc3
RR
1065gtk_wx_cell_renderer_get_type (void)
1066{
553f7d8f 1067 static GType cell_wx_type = 0;
e152afc3 1068
553f7d8f 1069 if (!cell_wx_type)
e152afc3 1070 {
de4a74e2 1071 const GTypeInfo cell_wx_info =
553f7d8f
RR
1072 {
1073 sizeof (GtkWxCellRendererClass),
93763ad5
WS
1074 NULL, /* base_init */
1075 NULL, /* base_finalize */
553f7d8f 1076 (GClassInitFunc) gtk_wx_cell_renderer_class_init,
93763ad5
WS
1077 NULL, /* class_finalize */
1078 NULL, /* class_data */
553f7d8f
RR
1079 sizeof (GtkWxCellRenderer),
1080 0, /* n_preallocs */
1081 (GInstanceInitFunc) gtk_wx_cell_renderer_init,
1082 };
1083
93763ad5 1084 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER,
553f7d8f 1085 "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 );
e152afc3
RR
1086 }
1087
553f7d8f 1088 return cell_wx_type;
e152afc3
RR
1089}
1090
1091static void
1092gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
1093{
1094 cell->cell = NULL;
4d496ecb 1095 cell->last_click = 0;
e152afc3
RR
1096}
1097
1098static void
1099gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
1100{
1101 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1102 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
1103
1104 cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
1105
1106 object_class->finalize = gtk_wx_cell_renderer_finalize;
1107
1108 cell_class->get_size = gtk_wx_cell_renderer_get_size;
1109 cell_class->render = gtk_wx_cell_renderer_render;
553f7d8f 1110 cell_class->activate = gtk_wx_cell_renderer_activate;
1e510b1e 1111 cell_class->start_editing = gtk_wx_cell_renderer_start_editing;
e152afc3
RR
1112}
1113
1114static void
1115gtk_wx_cell_renderer_finalize (GObject *object)
1116{
1117 /* must chain up */
1118 (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object);
1119}
1120
1121GtkCellRenderer*
1122gtk_wx_cell_renderer_new (void)
1123{
1124 return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
1125}
1126
1e510b1e
RR
1127static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
1128 GtkCellRenderer *renderer,
c5c5395b 1129 GdkEvent *WXUNUSED(event),
1e510b1e
RR
1130 GtkWidget *widget,
1131 const gchar *path,
c5c5395b 1132 GdkRectangle *WXUNUSED(background_area),
1e510b1e 1133 GdkRectangle *cell_area,
c5c5395b 1134 GtkCellRendererState WXUNUSED(flags) )
1e510b1e
RR
1135{
1136 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
1137 wxDataViewCustomRenderer *cell = wxrenderer->cell;
bc0289bf 1138
c232dfe5 1139 // Renderer doesn't support in-place editing
1e510b1e
RR
1140 if (!cell->HasEditorCtrl())
1141 return NULL;
bc0289bf 1142
c232dfe5
RR
1143 // An in-place editing control is still around
1144 if (cell->GetEditorCtrl())
1145 return NULL;
e8375af8 1146
1e510b1e
RR
1147 GdkRectangle rect;
1148 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
1149 &rect.x,
1150 &rect.y,
1151 &rect.width,
1152 &rect.height);
1153
1154 rect.x += cell_area->x;
1155 rect.y += cell_area->y;
1156// rect.width -= renderer->xpad * 2;
1157// rect.height -= renderer->ypad * 2;
1158
9d02e494
VZ
1159// wxRect renderrect(wxRectFromGDKRect(&rect));
1160 wxRect renderrect(wxRectFromGDKRect(cell_area));
1e510b1e 1161
1e510b1e 1162 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
55fbde12
RR
1163 GtkTreeIter iter;
1164 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, treepath );
1165 wxDataViewItem item( (void*) iter.user_data );
1e510b1e
RR
1166 gtk_tree_path_free( treepath );
1167
9d52aad3 1168 cell->StartEditing( item, renderrect );
30715fa1 1169
1e510b1e
RR
1170 return NULL;
1171}
1172
e152afc3
RR
1173static void
1174gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
c5c5395b 1175 GtkWidget *WXUNUSED(widget),
93763ad5
WS
1176 GdkRectangle *cell_area,
1177 gint *x_offset,
1178 gint *y_offset,
1179 gint *width,
1180 gint *height)
e152afc3
RR
1181{
1182 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
baa9ebc4 1183 wxDataViewCustomRenderer *cell = wxrenderer->cell;
93763ad5 1184
e152afc3
RR
1185 wxSize size = cell->GetSize();
1186
1187 gint calc_width = (gint) renderer->xpad * 2 + size.x;
1188 gint calc_height = (gint) renderer->ypad * 2 + size.y;
93763ad5
WS
1189
1190 if (x_offset)
e152afc3 1191 *x_offset = 0;
93763ad5 1192 if (y_offset)
e152afc3
RR
1193 *y_offset = 0;
1194
1195 if (cell_area && size.x > 0 && size.y > 0)
1196 {
1197 if (x_offset)
93763ad5 1198 {
e152afc3
RR
1199 *x_offset = (gint)((renderer->xalign *
1200 (cell_area->width - calc_width - 2 * renderer->xpad)));
1201 *x_offset = MAX (*x_offset, 0) + renderer->xpad;
93763ad5 1202 }
e152afc3
RR
1203 if (y_offset)
1204 {
1205 *y_offset = (gint)((renderer->yalign *
1206 (cell_area->height - calc_height - 2 * renderer->ypad)));
1207 *y_offset = MAX (*y_offset, 0) + renderer->ypad;
1208 }
1209 }
1210
1211 if (width)
1212 *width = calc_width;
93763ad5 1213
e152afc3
RR
1214 if (height)
1215 *height = calc_height;
1216}
1217
1218static void
1219gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
93763ad5
WS
1220 GdkWindow *window,
1221 GtkWidget *widget,
1222 GdkRectangle *background_area,
1223 GdkRectangle *cell_area,
1224 GdkRectangle *expose_area,
1225 GtkCellRendererState flags)
e152afc3
RR
1226
1227{
1228 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
baa9ebc4 1229 wxDataViewCustomRenderer *cell = wxrenderer->cell;
93763ad5 1230
2a454ffd
VZ
1231 cell->GTKStashRenderParams(window, widget,
1232 background_area, expose_area, flags);
f69c03de 1233
a923d77f
VZ
1234 wxRect rect(wxRectFromGDKRect(cell_area));
1235 rect = rect.Deflate(renderer->xpad, renderer->ypad);
e152afc3 1236
a923d77f
VZ
1237 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
1238 wxWindowDCImpl *impl = (wxWindowDCImpl *) dc->GetImpl();
bc0289bf 1239
a923d77f
VZ
1240 // Reinitialize wxWindowDC's GDK window if drawing occurs into a different
1241 // window such as a DnD drop window.
1242 if (window != impl->m_gdkwindow)
e152afc3 1243 {
a923d77f
VZ
1244 impl->Destroy();
1245 impl->m_gdkwindow = window;
1246 impl->SetUpDC();
93763ad5 1247 }
a923d77f
VZ
1248
1249 int state = 0;
1250 if (flags & GTK_CELL_RENDERER_SELECTED)
1251 state |= wxDATAVIEW_CELL_SELECTED;
1252 if (flags & GTK_CELL_RENDERER_PRELIT)
1253 state |= wxDATAVIEW_CELL_PRELIT;
1254 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1255 state |= wxDATAVIEW_CELL_INSENSITIVE;
1256 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1257 state |= wxDATAVIEW_CELL_INSENSITIVE;
1258 if (flags & GTK_CELL_RENDERER_FOCUSED)
1259 state |= wxDATAVIEW_CELL_FOCUSED;
1260 cell->WXCallRender( rect, dc, state );
e152afc3
RR
1261}
1262
93763ad5 1263static gboolean
553f7d8f
RR
1264gtk_wx_cell_renderer_activate(
1265 GtkCellRenderer *renderer,
1266 GdkEvent *event,
1267 GtkWidget *widget,
1268 const gchar *path,
c5c5395b 1269 GdkRectangle *WXUNUSED(background_area),
553f7d8f 1270 GdkRectangle *cell_area,
c5c5395b 1271 GtkCellRendererState WXUNUSED(flags) )
553f7d8f
RR
1272{
1273 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
baa9ebc4 1274 wxDataViewCustomRenderer *cell = wxrenderer->cell;
93763ad5 1275
553f7d8f
RR
1276 GdkRectangle rect;
1277 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
93763ad5
WS
1278 &rect.x,
1279 &rect.y,
1280 &rect.width,
1281 &rect.height);
553f7d8f
RR
1282
1283 rect.x += cell_area->x;
1284 rect.y += cell_area->y;
1285 rect.width -= renderer->xpad * 2;
1286 rect.height -= renderer->ypad * 2;
93763ad5 1287
9d02e494 1288 wxRect renderrect(wxRectFromGDKRect(&rect));
93763ad5 1289
e0062c04 1290 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 1291
4d496ecb 1292 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
71c6b455
RR
1293
1294 GtkTreeIter iter;
f69c03de 1295 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, treepath );
71c6b455 1296 wxDataViewItem item( iter.user_data );
4d496ecb 1297 gtk_tree_path_free( treepath );
93763ad5 1298
0a71f9e9 1299 unsigned int model_col = cell->GetOwner()->GetModelColumn();
93763ad5 1300
456e5c21
RR
1301 if (!event)
1302 {
1303 bool ret = false;
e8375af8 1304
456e5c21 1305 // activated by <ENTER>
9d52aad3 1306 if (cell->Activate( renderrect, model, item, model_col ))
456e5c21 1307 ret = true;
e8375af8 1308
456e5c21
RR
1309 return ret;
1310 }
1311 else if (event->type == GDK_BUTTON_PRESS)
4d496ecb
RR
1312 {
1313 GdkEventButton *button_event = (GdkEventButton*) event;
93763ad5 1314 wxPoint pt( ((int) button_event->x) - renderrect.x,
4d496ecb 1315 ((int) button_event->y) - renderrect.y );
93763ad5 1316
4d496ecb
RR
1317 bool ret = false;
1318 if (button_event->button == 1)
1319 {
9d52aad3 1320 if (cell->LeftClick( pt, renderrect, model, item, model_col ))
4d496ecb 1321 ret = true;
7ea3a0de 1322 // TODO: query system double-click time
4d496ecb 1323 if (button_event->time - wxrenderer->last_click < 400)
9d52aad3 1324 if (cell->Activate( renderrect, model, item, model_col ))
4d496ecb
RR
1325 ret = true;
1326 }
4d496ecb 1327 wxrenderer->last_click = button_event->time;
93763ad5 1328
4d496ecb
RR
1329 return ret;
1330 }
93763ad5 1331
4d496ecb 1332 return false;
553f7d8f
RR
1333}
1334
93763ad5 1335// ---------------------------------------------------------
e0062c04 1336// wxGtkDataViewModelNotifier
93763ad5 1337// ---------------------------------------------------------
6e2e590f 1338
e0062c04 1339class wxGtkDataViewModelNotifier: public wxDataViewModelNotifier
6e2e590f
RR
1340{
1341public:
e0062c04
RR
1342 wxGtkDataViewModelNotifier( GtkWxTreeModel *wxgtk_model,
1343 wxDataViewModel *wx_model,
1344 wxDataViewCtrl *ctrl );
1345 ~wxGtkDataViewModelNotifier();
1346
1347 virtual bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
469d3e9b 1348 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
e0062c04
RR
1349 virtual bool ItemChanged( const wxDataViewItem &item );
1350 virtual bool ValueChanged( const wxDataViewItem &item, unsigned int col );
6e2e590f 1351 virtual bool Cleared();
4508fcd2 1352 virtual void Resort();
93763ad5 1353
33ba5a05
RR
1354 void SetGtkModel( GtkWxTreeModel *model ) { m_wxgtk_model = model; }
1355
1356private:
e0062c04
RR
1357 GtkWxTreeModel *m_wxgtk_model;
1358 wxDataViewModel *m_wx_model;
63415a42 1359 wxDataViewCtrl *m_owner;
6e2e590f
RR
1360};
1361
93763ad5 1362// ---------------------------------------------------------
6e2e590f 1363// wxGtkDataViewListModelNotifier
93763ad5 1364// ---------------------------------------------------------
6e2e590f 1365
e0062c04
RR
1366wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1367 GtkWxTreeModel* wxgtk_model, wxDataViewModel *wx_model,
63415a42 1368 wxDataViewCtrl *ctrl )
6e2e590f 1369{
e0062c04 1370 m_wxgtk_model = wxgtk_model;
6e2e590f 1371 m_wx_model = wx_model;
63415a42 1372 m_owner = ctrl;
6e2e590f 1373}
93763ad5 1374
e0062c04 1375wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
f7ed8c89
RR
1376{
1377 m_wx_model = NULL;
e0062c04 1378 m_wxgtk_model = NULL;
f7ed8c89
RR
1379}
1380
e0062c04 1381bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
6e2e590f 1382{
55fbde12 1383 m_owner->GtkGetInternal()->ItemAdded( parent, item );
1e08ad10 1384
6e2e590f 1385 GtkTreeIter iter;
e0062c04
RR
1386 iter.stamp = m_wxgtk_model->stamp;
1387 iter.user_data = (gpointer) item.GetID();
93763ad5 1388
b9db5f30 1389 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04 1390 GTK_TREE_MODEL(m_wxgtk_model), &iter );
b9db5f30 1391 gtk_tree_model_row_inserted(
e0062c04 1392 GTK_TREE_MODEL(m_wxgtk_model), path, &iter);
6e2e590f 1393 gtk_tree_path_free (path);
93763ad5 1394
6e2e590f
RR
1395 return true;
1396}
1397
469d3e9b 1398bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
6e2e590f 1399{
8650c4ba
RR
1400#if 0
1401 // using _get_path for a deleted item cannot be
1402 // a good idea
605c2c4a 1403 GtkTreeIter iter;
e0062c04
RR
1404 iter.stamp = m_wxgtk_model->stamp;
1405 iter.user_data = (gpointer) item.GetID();
b9db5f30 1406 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04 1407 GTK_TREE_MODEL(m_wxgtk_model), &iter );
8650c4ba
RR
1408#else
1409 // so get the path from the parent
1410 GtkTreeIter iter;
1411 iter.stamp = m_wxgtk_model->stamp;
1412 iter.user_data = (gpointer) parent.GetID();
1413 GtkTreePath *path = wxgtk_tree_model_get_path(
1414 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1415 // and add the final index ourselves
1416 int index = m_owner->GtkGetInternal()->GetIndexOf( parent, item );
1417 gtk_tree_path_append_index( path, index );
1418#endif
1419
e0062c04
RR
1420 gtk_tree_model_row_deleted(
1421 GTK_TREE_MODEL(m_wxgtk_model), path );
605c2c4a 1422 gtk_tree_path_free (path);
93763ad5 1423
469d3e9b 1424 m_owner->GtkGetInternal()->ItemDeleted( parent, item );
b9db5f30 1425
605c2c4a 1426 return true;
6e2e590f
RR
1427}
1428
4508fcd2
RR
1429void wxGtkDataViewModelNotifier::Resort()
1430{
1431 m_owner->GtkGetInternal()->Resort();
1432}
1433
e0062c04 1434bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem &item )
6e2e590f 1435{
4627af27 1436 GtkTreeIter iter;
e0062c04
RR
1437 iter.stamp = m_wxgtk_model->stamp;
1438 iter.user_data = (gpointer) item.GetID();
6e2e590f 1439
b9db5f30 1440 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04
RR
1441 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1442 gtk_tree_model_row_changed(
1443 GTK_TREE_MODEL(m_wxgtk_model), path, &iter );
a7f61f76
RR
1444 gtk_tree_path_free (path);
1445
d8331a01 1446 m_owner->GtkGetInternal()->ItemChanged( item );
b9db5f30 1447
a7f61f76 1448 return true;
6e2e590f
RR
1449}
1450
e0062c04 1451bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem &item, unsigned int model_col )
6e2e590f 1452{
4eccd3a1 1453 // This adds GTK+'s missing MVC logic for ValueChanged
63415a42
RR
1454 unsigned int index;
1455 for (index = 0; index < m_owner->GetColumnCount(); index++)
8f850e28 1456 {
63415a42
RR
1457 wxDataViewColumn *column = m_owner->GetColumn( index );
1458 if (column->GetModelColumn() == model_col)
8f850e28 1459 {
63415a42
RR
1460 GtkTreeView *widget = GTK_TREE_VIEW(m_owner->m_treeview);
1461 GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
8f850e28
RR
1462
1463 // Get cell area
e0062c04
RR
1464 GtkTreeIter iter;
1465 iter.stamp = m_wxgtk_model->stamp;
1466 iter.user_data = (gpointer) item.GetID();
b9db5f30 1467 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04 1468 GTK_TREE_MODEL(m_wxgtk_model), &iter );
8f850e28 1469 GdkRectangle cell_area;
63415a42 1470 gtk_tree_view_get_cell_area( widget, path, gcolumn, &cell_area );
93763ad5 1471 gtk_tree_path_free( path );
8f850e28 1472
1a64259d
RR
1473 GtkAdjustment* hadjust = gtk_tree_view_get_hadjustment( widget );
1474 double d = gtk_adjustment_get_value( hadjust );
1475 int xdiff = (int) d;
1476
63415a42 1477 int ydiff = gcolumn->button->allocation.height;
8f850e28 1478 // Redraw
93763ad5 1479 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
1a64259d 1480 cell_area.x - xdiff, ydiff + cell_area.y, cell_area.width, cell_area.height );
b9db5f30 1481
d8331a01 1482 m_owner->GtkGetInternal()->ValueChanged( item, model_col );
b9db5f30 1483
d8331a01 1484 return true;
8f850e28 1485 }
8f850e28 1486 }
93763ad5 1487
d8331a01 1488 return false;
6e2e590f
RR
1489}
1490
e0062c04 1491bool wxGtkDataViewModelNotifier::Cleared()
6e2e590f 1492{
d8331a01 1493 m_owner->GtkGetInternal()->Cleared();
03647350 1494
8a568372 1495 return true;
6e2e590f
RR
1496}
1497
93763ad5 1498// ---------------------------------------------------------
baa9ebc4 1499// wxDataViewRenderer
93763ad5 1500// ---------------------------------------------------------
6842a71a 1501
a912e81f
RR
1502static gpointer s_user_data = NULL;
1503
1504static void
c5c5395b 1505wxgtk_cell_editable_editing_done( GtkCellEditable *WXUNUSED(editable),
a912e81f
RR
1506 wxDataViewRenderer *wxrenderer )
1507{
1508 wxDataViewColumn *column = wxrenderer->GetOwner();
1509 wxDataViewCtrl *dv = column->GetOwner();
1510 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv->GetId() );
1511 event.SetDataViewColumn( column );
1512 event.SetModel( dv->GetModel() );
1513 wxDataViewItem item( s_user_data );
1514 event.SetItem( item );
937013e0 1515 dv->HandleWindowEvent( event );
a912e81f
RR
1516}
1517
b9db5f30 1518static void
c5c5395b 1519wxgtk_renderer_editing_started( GtkCellRenderer *WXUNUSED(cell), GtkCellEditable *editable,
a912e81f
RR
1520 gchar *path, wxDataViewRenderer *wxrenderer )
1521{
ecc32226
RR
1522 if (!editable)
1523 return;
1524
a912e81f
RR
1525 wxDataViewColumn *column = wxrenderer->GetOwner();
1526 wxDataViewCtrl *dv = column->GetOwner();
1527 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv->GetId() );
1528 event.SetDataViewColumn( column );
1529 event.SetModel( dv->GetModel() );
1530 GtkTreePath *tree_path = gtk_tree_path_new_from_string( path );
1531 GtkTreeIter iter;
1532 dv->GtkGetInternal()->get_iter( &iter, tree_path );
1533 gtk_tree_path_free( tree_path );
1534 wxDataViewItem item( iter.user_data );
1535 event.SetItem( item );
937013e0 1536 dv->HandleWindowEvent( event );
a912e81f
RR
1537
1538 if (GTK_IS_CELL_EDITABLE(editable))
1539 {
1540 s_user_data = iter.user_data;
b9db5f30 1541
a912e81f
RR
1542 g_signal_connect (GTK_CELL_EDITABLE (editable), "editing_done",
1543 G_CALLBACK (wxgtk_cell_editable_editing_done),
1544 (gpointer) wxrenderer );
b9db5f30 1545
a912e81f
RR
1546 }
1547}
1548
1549
baa9ebc4 1550IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
6842a71a 1551
9861f022
RR
1552wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1553 int align ) :
1554 wxDataViewRendererBase( varianttype, mode, align )
6842a71a
RR
1555{
1556 m_renderer = NULL;
9861f022 1557
b74399b9
VZ
1558 // we haven't changed them yet
1559 m_usingDefaultAttrs = true;
1560
9861f022
RR
1561 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1562 // after the m_renderer pointer has been initialized
1563}
1564
205bdf20
VZ
1565void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
1566{
1567 gtk_tree_view_column_pack_end( column, m_renderer, TRUE /* expand */);
1568}
1569
a912e81f
RR
1570void wxDataViewRenderer::GtkInitHandlers()
1571{
1572 if (!gtk_check_version(2,6,0))
1573 {
1574 g_signal_connect (GTK_CELL_RENDERER(m_renderer), "editing_started",
03647350
VZ
1575 G_CALLBACK (wxgtk_renderer_editing_started),
1576 this);
a912e81f
RR
1577 }
1578}
1579
9861f022
RR
1580void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
1581{
1582 GtkCellRendererMode gtkMode;
1583 switch (mode)
1584 {
25bc5c55
VZ
1585 case wxDATAVIEW_CELL_INERT:
1586 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
1587 break;
1588
1589 case wxDATAVIEW_CELL_ACTIVATABLE:
1590 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
1591 break;
1592
1593 case wxDATAVIEW_CELL_EDITABLE:
1594 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
1595 break;
1596
1597 default:
1598 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1599 return;
9861f022
RR
1600 }
1601
b9db5f30 1602 // This value is most often ignored in GtkTreeView
9861f022
RR
1603 GValue gvalue = { 0, };
1604 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1605 g_value_set_enum( &gvalue, gtkMode );
1606 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1607 g_value_unset( &gvalue );
1608}
1609
1610wxDataViewCellMode wxDataViewRenderer::GetMode() const
1611{
1612 wxDataViewCellMode ret;
1613
1614 GValue gvalue;
1615 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1616
1617 switch (g_value_get_enum(&gvalue))
1618 {
25bc5c55
VZ
1619 default:
1620 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1621 // fall through (we have to return something)
1622
1623 case GTK_CELL_RENDERER_MODE_INERT:
1624 ret = wxDATAVIEW_CELL_INERT;
1625 break;
1626
1627 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1628 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1629 break;
1630
1631 case GTK_CELL_RENDERER_MODE_EDITABLE:
1632 ret = wxDATAVIEW_CELL_EDITABLE;
1633 break;
9861f022
RR
1634 }
1635
1636 g_value_unset( &gvalue );
1637
1638 return ret;
1639}
1640
f2b7492a 1641void wxDataViewRenderer::GtkUpdateAlignment()
9861f022 1642{
f2b7492a 1643 int align = m_alignment;
bc0289bf 1644
f2b7492a
RR
1645 // query alignment from column ?
1646 if (align == -1)
1647 {
1648 // None there yet
1649 if (GetOwner() == NULL)
1650 return;
bc0289bf 1651
f2b7492a
RR
1652 align = GetOwner()->GetAlignment();
1653 align |= wxALIGN_CENTRE_VERTICAL;
1654 }
bc0289bf 1655
9861f022
RR
1656 // horizontal alignment:
1657
1658 gfloat xalign = 0.0;
1659 if (align & wxALIGN_RIGHT)
1660 xalign = 1.0;
1661 else if (align & wxALIGN_CENTER_HORIZONTAL)
1662 xalign = 0.5;
1663
1664 GValue gvalue = { 0, };
1665 g_value_init( &gvalue, G_TYPE_FLOAT );
1666 g_value_set_float( &gvalue, xalign );
1667 g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue );
1668 g_value_unset( &gvalue );
1669
1670 // vertical alignment:
1671
1672 gfloat yalign = 0.0;
1673 if (align & wxALIGN_BOTTOM)
1674 yalign = 1.0;
1675 else if (align & wxALIGN_CENTER_VERTICAL)
1676 yalign = 0.5;
1677
1678 GValue gvalue2 = { 0, };
1679 g_value_init( &gvalue2, G_TYPE_FLOAT );
1680 g_value_set_float( &gvalue2, yalign );
1681 g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 );
1682 g_value_unset( &gvalue2 );
6842a71a
RR
1683}
1684
f2b7492a 1685void wxDataViewRenderer::SetAlignment( int align )
9861f022 1686{
f2b7492a
RR
1687 m_alignment = align;
1688 GtkUpdateAlignment();
9861f022
RR
1689}
1690
f2b7492a
RR
1691int wxDataViewRenderer::GetAlignment() const
1692{
1693 return m_alignment;
1694}
9861f022 1695
c937bcac
VZ
1696void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode)
1697{
1698 if ( gtk_check_version(2, 6, 0) != NULL )
1699 return;
1700
17cbc244
VZ
1701 GtkCellRendererText * const rend = GtkGetTextRenderer();
1702 if ( !rend )
1703 return;
1704
c937bcac
VZ
1705 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1706 // can just cast between them
1707 GValue gvalue = { 0, };
1708 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1709 g_value_set_enum( &gvalue, static_cast<PangoEllipsizeMode>(mode) );
17cbc244 1710 g_object_set_property( G_OBJECT(rend), "ellipsize", &gvalue );
c937bcac
VZ
1711 g_value_unset( &gvalue );
1712}
1713
1714wxEllipsizeMode wxDataViewRenderer::GetEllipsizeMode() const
1715{
1716 if ( gtk_check_version(2, 6, 0) != NULL )
1717 return wxELLIPSIZE_NONE;
1718
17cbc244
VZ
1719 GtkCellRendererText * const rend = GtkGetTextRenderer();
1720 if ( !rend )
1721 return wxELLIPSIZE_NONE;
1722
c937bcac
VZ
1723 GValue gvalue = { 0, };
1724 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
17cbc244 1725 g_object_get_property( G_OBJECT(rend), "ellipsize", &gvalue );
c937bcac
VZ
1726 wxEllipsizeMode
1727 mode = static_cast<wxEllipsizeMode>(g_value_get_enum( &gvalue ));
1728 g_value_unset( &gvalue );
1729
1730 return mode;
1731}
1732
205bdf20
VZ
1733void
1734wxDataViewRenderer::GtkOnTextEdited(const gchar *itempath, const wxString& str)
1735{
1736 wxVariant value(str);
1737 if (!Validate( value ))
1738 return;
1739
1740 GtkTreePath *path = gtk_tree_path_new_from_string( itempath );
1741 GtkTreeIter iter;
1742 GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, path );
1743 wxDataViewItem item( (void*) iter.user_data );;
1744 gtk_tree_path_free( path );
1745
1746 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
1747}
1748
1749void
1750wxDataViewRenderer::GtkOnCellChanged(const wxVariant& value,
1751 const wxDataViewItem& item,
1752 unsigned col)
1753{
1754 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
795dac4c 1755 model->ChangeValue( value, item, col );
205bdf20
VZ
1756}
1757
93763ad5 1758// ---------------------------------------------------------
baa9ebc4 1759// wxDataViewTextRenderer
93763ad5 1760// ---------------------------------------------------------
6842a71a 1761
205bdf20
VZ
1762extern "C"
1763{
a7f61f76 1764
c5c5395b 1765static void wxGtkTextRendererEditedCallback( GtkCellRendererText *WXUNUSED(renderer),
a7f61f76
RR
1766 gchar *arg1, gchar *arg2, gpointer user_data )
1767{
7448d67c 1768 wxDataViewRenderer *cell = (wxDataViewRenderer*) user_data;
93763ad5 1769
205bdf20
VZ
1770 cell->GtkOnTextEdited(arg1, wxGTK_CONV_BACK_FONT(
1771 arg2, cell->GetOwner()->GetOwner()->GetFont()));
1772}
93763ad5 1773
a7f61f76
RR
1774}
1775
c80cde00
VZ
1776namespace
1777{
1778
1779// helper function used by wxDataViewTextRenderer and
1780// wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
1781// given text renderer and returns true if anything was done
1782bool GtkApplyAttr(GtkCellRendererText *renderer, const wxDataViewItemAttr& attr)
1783{
1784 bool usingDefaultAttrs = true;
1785 if (attr.HasColour())
1786 {
1787 const GdkColor * const gcol = attr.GetColour().GetColor();
1788
1789 GValue gvalue = { 0, };
1790 g_value_init( &gvalue, GDK_TYPE_COLOR );
1791 g_value_set_boxed( &gvalue, gcol );
1792 g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
1793 g_value_unset( &gvalue );
1794
1795 usingDefaultAttrs = false;
1796 }
1797 else
1798 {
1799 GValue gvalue = { 0, };
1800 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1801 g_value_set_boolean( &gvalue, FALSE );
1802 g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
1803 g_value_unset( &gvalue );
1804 }
1805
1806 if (attr.GetItalic())
1807 {
1808 GValue gvalue = { 0, };
1809 g_value_init( &gvalue, PANGO_TYPE_STYLE );
1810 g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
1811 g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
1812 g_value_unset( &gvalue );
1813
1814 usingDefaultAttrs = false;
1815 }
1816 else
1817 {
1818 GValue gvalue = { 0, };
1819 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1820 g_value_set_boolean( &gvalue, FALSE );
1821 g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
1822 g_value_unset( &gvalue );
1823 }
1824
1825
1826 if (attr.GetBold())
1827 {
1828 GValue gvalue = { 0, };
1829 g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
1830 g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
1831 g_object_set_property( G_OBJECT(renderer), "weight", &gvalue );
1832 g_value_unset( &gvalue );
1833
1834 usingDefaultAttrs = false;
1835 }
1836 else
1837 {
1838 GValue gvalue = { 0, };
1839 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1840 g_value_set_boolean( &gvalue, FALSE );
1841 g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue );
1842 g_value_unset( &gvalue );
1843 }
1844
1845#if 0
1846 if (attr.HasBackgroundColour())
1847 {
1848 wxColour colour = attr.GetBackgroundColour();
1849 const GdkColor * const gcol = colour.GetColor();
1850
1851 GValue gvalue = { 0, };
1852 g_value_init( &gvalue, GDK_TYPE_COLOR );
1853 g_value_set_boxed( &gvalue, gcol );
1854 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
1855 g_value_unset( &gvalue );
1856 }
1857 else
1858 {
1859 GValue gvalue = { 0, };
1860 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1861 g_value_set_boolean( &gvalue, FALSE );
1862 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
1863 g_value_unset( &gvalue );
1864 }
1865#endif
1866
1867 return !usingDefaultAttrs;
1868}
1869
1870} // anonymous namespace
1871
baa9ebc4 1872IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
6842a71a 1873
9861f022
RR
1874wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1875 int align ) :
1876 wxDataViewRenderer( varianttype, mode, align )
6842a71a 1877{
ecc32226
RR
1878 GtkWxCellRendererText *text_renderer = gtk_wx_cell_renderer_text_new();
1879 text_renderer->wx_renderer = this;
1880 m_renderer = (GtkCellRenderer*) text_renderer;
93763ad5 1881
9861f022 1882 if (mode & wxDATAVIEW_CELL_EDITABLE)
a7f61f76
RR
1883 {
1884 GValue gvalue = { 0, };
1885 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1886 g_value_set_boolean( &gvalue, true );
1887 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
1888 g_value_unset( &gvalue );
93763ad5 1889
a7f61f76 1890 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
b9db5f30 1891
a912e81f 1892 GtkInitHandlers();
a7f61f76 1893 }
9861f022
RR
1894
1895 SetMode(mode);
1896 SetAlignment(align);
6842a71a 1897}
790b137e 1898
205bdf20 1899bool wxDataViewTextRenderer::SetTextValue(const wxString& str)
7b4fde82 1900{
7b4fde82
RR
1901 GValue gvalue = { 0, };
1902 g_value_init( &gvalue, G_TYPE_STRING );
205bdf20 1903 g_value_set_string( &gvalue, wxGTK_CONV_FONT( str, GetOwner()->GetOwner()->GetFont() ) );
7b4fde82
RR
1904 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1905 g_value_unset( &gvalue );
93763ad5 1906
7b4fde82
RR
1907 return true;
1908}
1909
205bdf20 1910bool wxDataViewTextRenderer::GetTextValue(wxString& str) const
a7f61f76
RR
1911{
1912 GValue gvalue = { 0, };
1913 g_value_init( &gvalue, G_TYPE_STRING );
1914 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
205bdf20 1915 str = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), const_cast<wxDataViewTextRenderer*>(this)->GetOwner()->GetOwner()->GetFont() );
a7f61f76 1916 g_value_unset( &gvalue );
93763ad5 1917
a7f61f76
RR
1918 return true;
1919}
1920
9861f022
RR
1921void wxDataViewTextRenderer::SetAlignment( int align )
1922{
1923 wxDataViewRenderer::SetAlignment(align);
1924
01705e98
RR
1925 if (gtk_check_version(2,10,0))
1926 return;
9861f022 1927
01705e98 1928 // horizontal alignment:
9861f022
RR
1929 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
1930 if (align & wxALIGN_RIGHT)
1931 pangoAlign = PANGO_ALIGN_RIGHT;
1932 else if (align & wxALIGN_CENTER_HORIZONTAL)
1933 pangoAlign = PANGO_ALIGN_CENTER;
1934
1935 GValue gvalue = { 0, };
1936 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1937 g_value_set_enum( &gvalue, pangoAlign );
1938 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
1939 g_value_unset( &gvalue );
1940}
1941
c80cde00
VZ
1942bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr& attr)
1943{
17cbc244
VZ
1944 return GtkApplyAttr(GtkGetTextRenderer(), attr);
1945}
1946
1947GtkCellRendererText *wxDataViewTextRenderer::GtkGetTextRenderer() const
1948{
1949 return GTK_CELL_RENDERER_TEXT(m_renderer);
c80cde00
VZ
1950}
1951
f4322df6 1952// ---------------------------------------------------------
baa9ebc4 1953// wxDataViewBitmapRenderer
f4322df6 1954// ---------------------------------------------------------
cbc9145c 1955
205bdf20
VZ
1956namespace
1957{
1958
1959// set "pixbuf" property on the given renderer
1960void SetPixbufProp(GtkCellRenderer *renderer, GdkPixbuf *pixbuf)
1961{
1962 GValue gvalue = { 0, };
1963 g_value_init( &gvalue, G_TYPE_OBJECT );
1964 g_value_set_object( &gvalue, pixbuf );
1965 g_object_set_property( G_OBJECT(renderer), "pixbuf", &gvalue );
1966 g_value_unset( &gvalue );
1967}
1968
1969} // anonymous namespace
1970
baa9ebc4 1971IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
cbc9145c 1972
9861f022
RR
1973wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1974 int align ) :
1975 wxDataViewRenderer( varianttype, mode, align )
cbc9145c 1976{
205bdf20 1977 m_renderer = gtk_cell_renderer_pixbuf_new();
9861f022
RR
1978
1979 SetMode(mode);
1980 SetAlignment(align);
cbc9145c
RR
1981}
1982
baa9ebc4 1983bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
cbc9145c
RR
1984{
1985 if (value.GetType() == wxT("wxBitmap"))
1986 {
2586d4a1
RR
1987 wxBitmap bitmap;
1988 bitmap << value;
f4322df6 1989
205bdf20
VZ
1990 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
1991 // object (and it will stay there and remain owned by wxBitmap)
1992 SetPixbufProp(m_renderer, bitmap.GetPixbuf());
2586d4a1 1993 }
205bdf20 1994 else if (value.GetType() == wxT("wxIcon"))
2586d4a1 1995 {
205bdf20
VZ
1996 wxIcon icon;
1997 icon << value;
f4322df6 1998
205bdf20
VZ
1999 SetPixbufProp(m_renderer, icon.GetPixbuf());
2000 }
2001 else
2002 {
2003 return false;
cbc9145c 2004 }
f4322df6 2005
205bdf20 2006 return true;
cbc9145c
RR
2007}
2008
c5c5395b 2009bool wxDataViewBitmapRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
cbc9145c
RR
2010{
2011 return false;
2012}
f4322df6 2013
93763ad5 2014// ---------------------------------------------------------
baa9ebc4 2015// wxDataViewToggleRenderer
93763ad5 2016// ---------------------------------------------------------
fa28826d 2017
605c2c4a 2018extern "C" {
93763ad5 2019static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
2020 gchar *path, gpointer user_data );
2021}
2022
93763ad5 2023static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
2024 gchar *path, gpointer user_data )
2025{
baa9ebc4 2026 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
605c2c4a 2027
93763ad5 2028 // get old value
605c2c4a
RR
2029 GValue gvalue = { 0, };
2030 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2031 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
93763ad5 2032 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a
RR
2033 g_value_unset( &gvalue );
2034 // invert it
2035 tmp = !tmp;
93763ad5 2036
605c2c4a
RR
2037 wxVariant value = tmp;
2038 if (!cell->Validate( value ))
2039 return;
93763ad5 2040
e0062c04 2041 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 2042
605c2c4a 2043 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
55fbde12
RR
2044 GtkTreeIter iter;
2045 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, gtk_path );
2046 wxDataViewItem item( (void*) iter.user_data );;
605c2c4a 2047 gtk_tree_path_free( gtk_path );
93763ad5 2048
0a71f9e9 2049 unsigned int model_col = cell->GetOwner()->GetModelColumn();
93763ad5 2050
795dac4c 2051 model->ChangeValue( value, item, model_col );
605c2c4a
RR
2052}
2053
baa9ebc4 2054IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
605c2c4a 2055
baa9ebc4 2056wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
9861f022
RR
2057 wxDataViewCellMode mode, int align ) :
2058 wxDataViewRenderer( varianttype, mode, align )
605c2c4a 2059{
ed38aa55 2060 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
93763ad5 2061
9861f022 2062 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
605c2c4a 2063 {
9861f022
RR
2064 g_signal_connect_after( m_renderer, "toggled",
2065 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
553f7d8f
RR
2066 }
2067 else
2068 {
605c2c4a
RR
2069 GValue gvalue = { 0, };
2070 g_value_init( &gvalue, G_TYPE_BOOLEAN );
553f7d8f 2071 g_value_set_boolean( &gvalue, false );
605c2c4a
RR
2072 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
2073 g_value_unset( &gvalue );
605c2c4a 2074 }
9861f022
RR
2075
2076 SetMode(mode);
2077 SetAlignment(align);
605c2c4a
RR
2078}
2079
baa9ebc4 2080bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
605c2c4a
RR
2081{
2082 bool tmp = value;
93763ad5 2083
605c2c4a
RR
2084 GValue gvalue = { 0, };
2085 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2086 g_value_set_boolean( &gvalue, tmp );
2087 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
2088 g_value_unset( &gvalue );
93763ad5 2089
605c2c4a
RR
2090 return true;
2091}
2092
9861f022 2093bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
605c2c4a
RR
2094{
2095 GValue gvalue = { 0, };
2096 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2097 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
93763ad5 2098 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a 2099 g_value_unset( &gvalue );
93763ad5 2100
605c2c4a
RR
2101 value = tmp;
2102
2103 return true;
2104}
93763ad5
WS
2105
2106// ---------------------------------------------------------
baa9ebc4 2107// wxDataViewCustomRenderer
93763ad5 2108// ---------------------------------------------------------
e152afc3 2109
888dde65 2110class wxDataViewCtrlDCImpl: public wxWindowDCImpl
e152afc3
RR
2111{
2112public:
888dde65
RR
2113 wxDataViewCtrlDCImpl( wxDC *owner, wxDataViewCtrl *window ) :
2114 wxWindowDCImpl( owner )
2115 {
1a367564 2116 GtkWidget *widget = window->m_treeview;
e152afc3 2117 // Set later
888dde65 2118 m_gdkwindow = NULL;
7857346a 2119
888dde65 2120 m_window = window;
4d496ecb 2121
496e7ec6 2122 m_context = window->GTKGetPangoDefaultContext();
e152afc3
RR
2123 m_layout = pango_layout_new( m_context );
2124 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
2125
2126 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
888dde65
RR
2127
2128 // Set m_gdkwindow later
4d496ecb 2129 // SetUpDC();
e152afc3
RR
2130 }
2131};
2132
888dde65
RR
2133class wxDataViewCtrlDC: public wxWindowDC
2134{
2135public:
c5c5395b
RR
2136 wxDataViewCtrlDC( wxDataViewCtrl *window ) :
2137 wxWindowDC( new wxDataViewCtrlDCImpl( this, window ) )
7857346a 2138 { }
888dde65 2139};
7857346a 2140
888dde65 2141
93763ad5 2142// ---------------------------------------------------------
baa9ebc4 2143// wxDataViewCustomRenderer
93763ad5 2144// ---------------------------------------------------------
e152afc3 2145
baa9ebc4 2146IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
e152afc3 2147
baa9ebc4 2148wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
6eec70b9
VZ
2149 wxDataViewCellMode mode,
2150 int align,
2151 bool no_init )
2152 : wxDataViewCustomRendererBase( varianttype, mode, align )
e152afc3
RR
2153{
2154 m_dc = NULL;
f69c03de 2155 m_text_renderer = NULL;
93763ad5 2156
ad63bf41
RR
2157 if (no_init)
2158 m_renderer = NULL;
2159 else
9861f022 2160 Init(mode, align);
ad63bf41
RR
2161}
2162
17cbc244
VZ
2163GtkCellRendererText *wxDataViewCustomRenderer::GtkGetTextRenderer() const
2164{
2165 if ( !m_text_renderer )
2166 {
2167 // we create it on demand so need to do it even from a const function
2168 const_cast<wxDataViewCustomRenderer *>(this)->
2169 m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2170 }
2171
2172 return m_text_renderer;
2173}
2174
2a454ffd
VZ
2175void wxDataViewCustomRenderer::RenderText( const wxString &text,
2176 int xoffset,
2177 wxRect cell,
2178 wxDC *WXUNUSED(dc),
2179 int WXUNUSED(state) )
52e750fc 2180{
17cbc244
VZ
2181
2182 GtkCellRendererText * const textRenderer = GtkGetTextRenderer();
b9db5f30 2183
f69c03de
RR
2184 GValue gvalue = { 0, };
2185 g_value_init( &gvalue, G_TYPE_STRING );
1bf629bd 2186 g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) );
17cbc244 2187 g_object_set_property( G_OBJECT(textRenderer), "text", &gvalue );
f69c03de
RR
2188 g_value_unset( &gvalue );
2189
17cbc244 2190 GtkApplyAttr(textRenderer, GetAttr());
c80cde00 2191
2a454ffd
VZ
2192 GdkRectangle cell_area;
2193 wxRectToGDKRect(cell, cell_area);
2194 cell_area.x += xoffset;
2195 cell_area.width -= xoffset;
b9db5f30 2196
17cbc244 2197 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer),
2a454ffd
VZ
2198 m_renderParams.window,
2199 m_renderParams.widget,
2200 m_renderParams.background_area,
2201 &cell_area,
2202 m_renderParams.expose_area,
2203 (GtkCellRendererState) m_renderParams.flags );
52e750fc
RR
2204}
2205
9861f022 2206bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
ad63bf41 2207{
e152afc3
RR
2208 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
2209 renderer->cell = this;
93763ad5 2210
ed38aa55 2211 m_renderer = (GtkCellRenderer*) renderer;
93763ad5 2212
9861f022
RR
2213 SetMode(mode);
2214 SetAlignment(align);
93763ad5 2215
a912e81f 2216 GtkInitHandlers();
b9db5f30 2217
ad63bf41 2218 return true;
e152afc3
RR
2219}
2220
baa9ebc4 2221wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
e152afc3
RR
2222{
2223 if (m_dc)
2224 delete m_dc;
f69c03de
RR
2225
2226 if (m_text_renderer)
352cd79f 2227 gtk_object_sink( GTK_OBJECT(m_text_renderer) );
e152afc3
RR
2228}
2229
baa9ebc4 2230wxDC *wxDataViewCustomRenderer::GetDC()
e152afc3
RR
2231{
2232 if (m_dc == NULL)
4d496ecb
RR
2233 {
2234 if (GetOwner() == NULL)
2235 return NULL;
2236 if (GetOwner()->GetOwner() == NULL)
2237 return NULL;
e152afc3 2238 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
4d496ecb 2239 }
93763ad5 2240
e152afc3
RR
2241 return m_dc;
2242}
93763ad5
WS
2243
2244// ---------------------------------------------------------
baa9ebc4 2245// wxDataViewProgressRenderer
93763ad5 2246// ---------------------------------------------------------
ad63bf41 2247
baa9ebc4 2248IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
ad63bf41 2249
baa9ebc4 2250wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
9861f022
RR
2251 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
2252 wxDataViewCustomRenderer( varianttype, mode, align, true )
ad63bf41
RR
2253{
2254 m_label = label;
2255 m_value = 0;
93763ad5 2256
ad63bf41
RR
2257#ifdef __WXGTK26__
2258 if (!gtk_check_version(2,6,0))
2259 {
ed38aa55 2260 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
93763ad5 2261
ad63bf41
RR
2262 GValue gvalue = { 0, };
2263 g_value_init( &gvalue, G_TYPE_STRING );
09dfa6a0 2264
1bf629bd 2265 g_value_set_string( &gvalue, wxGTK_CONV_FONT( m_label, GetOwner()->GetOwner()->GetFont() ) );
ad63bf41
RR
2266 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2267 g_value_unset( &gvalue );
9861f022
RR
2268
2269 SetMode(mode);
2270 SetAlignment(align);
ad63bf41
RR
2271 }
2272 else
2273#endif
2274 {
2275 // Use custom cell code
9861f022 2276 wxDataViewCustomRenderer::Init(mode, align);
ad63bf41
RR
2277 }
2278}
2279
baa9ebc4 2280wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
ad63bf41
RR
2281{
2282}
2283
baa9ebc4 2284bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
ad63bf41
RR
2285{
2286#ifdef __WXGTK26__
2287 if (!gtk_check_version(2,6,0))
2288 {
7226118b 2289 gint tmp = (long) value;
ad63bf41
RR
2290 GValue gvalue = { 0, };
2291 g_value_init( &gvalue, G_TYPE_INT );
7226118b 2292 g_value_set_int( &gvalue, tmp );
ad63bf41
RR
2293 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
2294 g_value_unset( &gvalue );
2295 }
2296 else
2297#endif
2298 {
2299 m_value = (long) value;
93763ad5 2300
ad63bf41
RR
2301 if (m_value < 0) m_value = 0;
2302 if (m_value > 100) m_value = 100;
2303 }
93763ad5 2304
ad63bf41
RR
2305 return true;
2306}
93763ad5 2307
85136e3b 2308bool wxDataViewProgressRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
9861f022
RR
2309{
2310 return false;
2311}
2312
85136e3b 2313bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
ad63bf41
RR
2314{
2315 double pct = (double)m_value / 100.0;
2316 wxRect bar = cell;
2317 bar.width = (int)(cell.width * pct);
2318 dc->SetPen( *wxTRANSPARENT_PEN );
2319 dc->SetBrush( *wxBLUE_BRUSH );
2320 dc->DrawRectangle( bar );
2321
2322 dc->SetBrush( *wxTRANSPARENT_BRUSH );
2323 dc->SetPen( *wxBLACK_PEN );
2324 dc->DrawRectangle( cell );
93763ad5 2325
ad63bf41
RR
2326 return true;
2327}
2328
9861f022 2329wxSize wxDataViewProgressRenderer::GetSize() const
ad63bf41
RR
2330{
2331 return wxSize(40,12);
2332}
93763ad5 2333
7448d67c
RR
2334// -------------------------------------
2335// wxDataViewChoiceRenderer
2336// -------------------------------------
2337
2338wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString &choices,
2339 wxDataViewCellMode mode, int alignment ) :
2340 wxDataViewCustomRenderer( "string", mode, alignment, true )
2341{
2342 m_choices = choices;
2343
2344#ifdef __WXGTK26__
2345 if (!gtk_check_version(2,6,0))
2346 {
2347 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_combo_new();
2348
2349 GtkListStore *store = gtk_list_store_new( 1, G_TYPE_STRING );
5baee1f4
VZ
2350 for (size_t n = 0; n < m_choices.GetCount(); n++)
2351 {
2352 gtk_list_store_insert_with_values(
2353 store, NULL, n, 0,
2354 static_cast<const char *>(m_choices[n].utf8_str()), -1 );
2355 }
bc0289bf 2356
7448d67c
RR
2357 g_object_set (m_renderer,
2358 "model", store,
2359 "text-column", 0,
2360 "has-entry", FALSE,
2361 NULL);
bc0289bf 2362
7448d67c
RR
2363 bool editable = (mode & wxDATAVIEW_CELL_EDITABLE);
2364 g_object_set (m_renderer, "editable", editable, NULL);
bc0289bf 2365
7448d67c 2366 SetAlignment(alignment);
bc0289bf 2367
7448d67c
RR
2368 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2369
2370 GtkInitHandlers();
2371 }
2372 else
2373#endif
2374 {
2375 // Use custom cell code
2376 wxDataViewCustomRenderer::Init(mode, alignment);
2377 }
2378}
2379
2380bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
2381{
2382 RenderText( m_data, 0, rect, dc, state );
2383 return true;
2384}
2385
2386wxSize wxDataViewChoiceRenderer::GetSize() const
2387{
2388 return wxSize(70,20);
2389}
2390
2391bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
2392{
bc0289bf 2393
7448d67c
RR
2394#ifdef __WXGTK26__
2395 if (!gtk_check_version(2,6,0))
2396 {
2397 GValue gvalue = { 0, };
2398 g_value_init( &gvalue, G_TYPE_STRING );
2399 g_value_set_string( &gvalue, wxGTK_CONV_FONT( value.GetString(), GetOwner()->GetOwner()->GetFont() ) );
2400 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2401 g_value_unset( &gvalue );
2402 }
2403 else
2404#endif
2405 m_data = value.GetString();
2406
2407 return true;
2408}
2409
2410bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
2411{
2412#ifdef __WXGTK26__
2413 if (!gtk_check_version(2,6,0))
2414 {
2415 GValue gvalue = { 0, };
2416 g_value_init( &gvalue, G_TYPE_STRING );
2417 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
5baee1f4
VZ
2418 wxString temp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ),
2419 const_cast<wxDataViewChoiceRenderer*>(this)->GetOwner()->GetOwner()->GetFont() );
7448d67c
RR
2420 g_value_unset( &gvalue );
2421 value = temp;
c2489d8e
FM
2422
2423 //wxPrintf( "temp %s\n", temp );
2424 // TODO: remove this code
7448d67c
RR
2425 }
2426 else
2427#endif
2428 value = m_data;
2429
2430 return true;
2431}
2432
2433void wxDataViewChoiceRenderer::SetAlignment( int align )
2434{
2435 wxDataViewCustomRenderer::SetAlignment(align);
2436
2437 if (gtk_check_version(2,10,0))
2438 return;
2439
2440 // horizontal alignment:
2441 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2442 if (align & wxALIGN_RIGHT)
2443 pangoAlign = PANGO_ALIGN_RIGHT;
2444 else if (align & wxALIGN_CENTER_HORIZONTAL)
2445 pangoAlign = PANGO_ALIGN_CENTER;
2446
2447 GValue gvalue = { 0, };
2448 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2449 g_value_set_enum( &gvalue, pangoAlign );
2450 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2451 g_value_unset( &gvalue );
2452}
bc0289bf 2453
93763ad5 2454// ---------------------------------------------------------
baa9ebc4 2455// wxDataViewDateRenderer
93763ad5 2456// ---------------------------------------------------------
4d496ecb 2457
baa9ebc4 2458class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
7ea3a0de 2459{
93763ad5 2460public:
baa9ebc4 2461 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
e0062c04 2462 wxDataViewModel *model, const wxDataViewItem &item, unsigned int col ) :
7ea3a0de
RR
2463 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
2464 {
2465 m_model = model;
e0062c04 2466 m_item = item;
7ea3a0de 2467 m_col = col;
7ea3a0de
RR
2468 m_cal = new wxCalendarCtrl( this, -1, *value );
2469 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
2470 sizer->Add( m_cal, 1, wxGROW );
2471 SetSizer( sizer );
2472 sizer->Fit( this );
2473 }
93763ad5 2474
7ea3a0de
RR
2475 virtual void OnDismiss()
2476 {
2477 }
93763ad5 2478
7ea3a0de 2479 void OnCalendar( wxCalendarEvent &event );
93763ad5 2480
e0062c04
RR
2481 wxCalendarCtrl *m_cal;
2482 wxDataViewModel *m_model;
2483 wxDataViewItem m_item;
2484 unsigned int m_col;
93763ad5 2485
7ea3a0de
RR
2486private:
2487 DECLARE_EVENT_TABLE()
2488};
2489
baa9ebc4
RR
2490BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
2491 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
7ea3a0de
RR
2492END_EVENT_TABLE()
2493
baa9ebc4 2494void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
7ea3a0de 2495{
795dac4c 2496 m_model->ChangeValue( event.GetDate(), m_item, m_col );
7ea3a0de
RR
2497 DismissAndNotify();
2498}
2499
baa9ebc4 2500IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
4d496ecb 2501
baa9ebc4 2502wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
9861f022
RR
2503 wxDataViewCellMode mode, int align ) :
2504 wxDataViewCustomRenderer( varianttype, mode, align )
4d496ecb 2505{
9861f022
RR
2506 SetMode(mode);
2507 SetAlignment(align);
4d496ecb 2508}
93763ad5 2509
baa9ebc4 2510bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
4d496ecb
RR
2511{
2512 m_date = value.GetDateTime();
93763ad5 2513
4d496ecb
RR
2514 return true;
2515}
2516
85136e3b 2517bool wxDataViewDateRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
9861f022
RR
2518{
2519 return false;
2520}
2521
baa9ebc4 2522bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
4d496ecb
RR
2523{
2524 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
2525 wxString tmp = m_date.FormatDate();
52e750fc 2526 RenderText( tmp, 0, cell, dc, state );
4d496ecb
RR
2527 return true;
2528}
2529
9861f022 2530wxSize wxDataViewDateRenderer::GetSize() const
4d496ecb 2531{
4d496ecb
RR
2532 wxString tmp = m_date.FormatDate();
2533 wxCoord x,y,d;
9861f022 2534 GetView()->GetTextExtent( tmp, &x, &y, &d );
4d496ecb
RR
2535 return wxSize(x,y+d);
2536}
2537
85136e3b 2538bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model,
e0062c04 2539 const wxDataViewItem &item, unsigned int col )
4d496ecb 2540{
3f3af7e7 2541 wxVariant variant;
e0062c04 2542 model->GetValue( variant, item, col );
7ea3a0de
RR
2543 wxDateTime value = variant.GetDateTime();
2544
baa9ebc4 2545 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
e0062c04 2546 GetOwner()->GetOwner()->GetParent(), &value, model, item, col );
7ea3a0de
RR
2547 wxPoint pos = wxGetMousePosition();
2548 popup->Move( pos );
2549 popup->Layout();
2550 popup->Popup( popup->m_cal );
4d496ecb
RR
2551
2552 return true;
2553}
2554
c9c13e70 2555
b9db5f30 2556// ---------------------------------------------------------
c9c13e70 2557// wxDataViewIconTextRenderer
b9db5f30 2558// ---------------------------------------------------------
c9c13e70
RR
2559
2560IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
2561
205bdf20
VZ
2562wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2563 (
2564 const wxString &varianttype,
2565 wxDataViewCellMode mode,
2566 int align
2567 )
2568 : wxDataViewTextRenderer(varianttype, mode, align)
c9c13e70 2569{
205bdf20 2570 m_rendererIcon = gtk_cell_renderer_pixbuf_new();
c9c13e70
RR
2571}
2572
2573wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2574{
2575}
b9db5f30 2576
205bdf20 2577void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
c9c13e70 2578{
205bdf20
VZ
2579 // add the icon renderer first
2580 gtk_tree_view_column_pack_start(column, m_rendererIcon, FALSE /* !expand */);
c9c13e70 2581
205bdf20
VZ
2582 // add the text renderer too
2583 wxDataViewRenderer::GtkPackIntoColumn(column);
c9c13e70 2584}
b9db5f30 2585
205bdf20 2586bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
c9c13e70 2587{
205bdf20 2588 m_value << value;
b9db5f30 2589
205bdf20
VZ
2590 SetTextValue(m_value.GetText());
2591 SetPixbufProp(m_rendererIcon, m_value.GetIcon().GetPixbuf());
c9c13e70
RR
2592
2593 return true;
2594}
2595
205bdf20 2596bool wxDataViewIconTextRenderer::GetValue(wxVariant& value) const
c9c13e70 2597{
205bdf20
VZ
2598 wxString str;
2599 if ( !GetTextValue(str) )
2600 return false;
c9c13e70 2601
205bdf20
VZ
2602 // user doesn't have any way to edit the icon so leave it unchanged
2603 value << wxDataViewIconText(str, m_value.GetIcon());
2604
2605 return true;
c9c13e70
RR
2606}
2607
205bdf20
VZ
2608void
2609wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant& value,
2610 const wxDataViewItem& item,
2611 unsigned col)
c9c13e70 2612{
205bdf20
VZ
2613 // we receive just the text part of our value as it's the only one which
2614 // can be edited, but we need the full wxDataViewIconText value for the
2615 // model
2616 wxVariant valueIconText;
2617 valueIconText << wxDataViewIconText(value.GetString(), m_value.GetIcon());
2618 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText, item, col);
c9c13e70
RR
2619}
2620
93763ad5 2621// ---------------------------------------------------------
605c2c4a 2622// wxDataViewColumn
93763ad5 2623// ---------------------------------------------------------
7b4fde82 2624
31fb32e1
RR
2625
2626static gboolean
ad386793 2627gtk_dataview_header_button_press_callback( GtkWidget *WXUNUSED(widget),
31fb32e1
RR
2628 GdkEventButton *gdk_event,
2629 wxDataViewColumn *column )
2630{
2631 if (gdk_event->type != GDK_BUTTON_PRESS)
94b1f7bc 2632 return FALSE;
f4322df6 2633
31fb32e1
RR
2634 if (gdk_event->button == 1)
2635 {
d32332aa 2636 gs_lastLeftClickHeader = column;
7857346a 2637
31fb32e1
RR
2638 wxDataViewCtrl *dv = column->GetOwner();
2639 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
2640 event.SetDataViewColumn( column );
2641 event.SetModel( dv->GetModel() );
937013e0 2642 if (dv->HandleWindowEvent( event ))
a84c5b6f 2643 return FALSE;
31fb32e1 2644 }
f4322df6 2645
dadc879e
RR
2646 if (gdk_event->button == 3)
2647 {
2648 wxDataViewCtrl *dv = column->GetOwner();
2649 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, dv->GetId() );
2650 event.SetDataViewColumn( column );
2651 event.SetModel( dv->GetModel() );
937013e0 2652 if (dv->HandleWindowEvent( event ))
dadc879e
RR
2653 return FALSE;
2654 }
2655
94b1f7bc 2656 return FALSE;
31fb32e1
RR
2657}
2658
47583ac1
VZ
2659extern "C"
2660{
7b4fde82 2661
ad386793 2662static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *WXUNUSED(column),
7b4fde82
RR
2663 GtkCellRenderer *renderer,
2664 GtkTreeModel *model,
2665 GtkTreeIter *iter,
2666 gpointer data )
2667{
e0062c04
RR
2668 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
2669 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
93763ad5 2670
baa9ebc4 2671 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
a7f61f76 2672
9d52aad3 2673 wxDataViewItem item( (void*) iter->user_data );
93763ad5 2674
a826202e
RR
2675 wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel();
2676
e39de702 2677 if (!wx_model->IsVirtualListModel())
2056dede 2678 {
47583ac1
VZ
2679 gboolean visible;
2680 if (wx_model->IsContainer( item ))
a826202e 2681 {
47583ac1
VZ
2682 visible = wx_model->HasContainerColumns( item ) ||
2683 (cell->GetOwner()->GetModelColumn() == 0);
a826202e
RR
2684 }
2685 else
2686 {
47583ac1 2687 visible = true;
a826202e 2688 }
47583ac1 2689
a826202e
RR
2690 GValue gvalue = { 0, };
2691 g_value_init( &gvalue, G_TYPE_BOOLEAN );
47583ac1 2692 g_value_set_boolean( &gvalue, visible );
a826202e
RR
2693 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2694 g_value_unset( &gvalue );
b9db5f30 2695
47583ac1
VZ
2696 if ( !visible )
2697 return;
2056dede 2698 }
a826202e 2699
3f3af7e7 2700 wxVariant value;
a826202e 2701 wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
7b4fde82
RR
2702
2703 if (value.GetType() != cell->GetVariantType())
af588446 2704 {
f4322df6
VZ
2705 wxLogError( wxT("Wrong type, required: %s but: %s"),
2706 value.GetType().c_str(),
cbc9145c 2707 cell->GetVariantType().c_str() );
af588446 2708 }
93763ad5 2709
7b4fde82 2710 cell->SetValue( value );
e0743e63 2711
b74399b9 2712
c80cde00
VZ
2713 // deal with attributes: if the renderer doesn't support them at all, we
2714 // don't even need to query the model for them
c2a738e3
VZ
2715 if ( !cell->GtkSupportsAttrs() )
2716 return;
2717
c80cde00 2718 // it can support attributes so check if this item has any
2d0d7813 2719 wxDataViewItemAttr attr;
c80cde00
VZ
2720 if ( wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr )
2721 || !cell->GtkIsUsingDefaultAttrs() )
b74399b9 2722 {
c80cde00
VZ
2723 bool usingDefaultAttrs = !cell->GtkSetAttr(attr);
2724 cell->GtkSetUsingDefaultAttrs(usingDefaultAttrs);
b74399b9 2725 }
c80cde00
VZ
2726 // else: no custom attributes specified and we're already using the default
2727 // ones -- nothing to do
7b4fde82
RR
2728}
2729
47583ac1
VZ
2730} // extern "C"
2731
91a6c655 2732#include <wx/listimpl.cpp>
a76c2f37 2733WX_DEFINE_LIST(wxDataViewColumnList)
91a6c655 2734
f4322df6
VZ
2735wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
2736 unsigned int model_column, int width,
835d0d55
VZ
2737 wxAlignment align, int flags )
2738 : wxDataViewColumnBase( cell, model_column )
fa28826d 2739{
9861f022 2740 Init( align, flags, width );
31fb32e1 2741
31fb32e1 2742 SetTitle( title );
fa28826d
RR
2743}
2744
f4322df6
VZ
2745wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
2746 unsigned int model_column, int width,
835d0d55
VZ
2747 wxAlignment align, int flags )
2748 : wxDataViewColumnBase( bitmap, cell, model_column )
9861f022
RR
2749{
2750 Init( align, flags, width );
2751
2752 SetBitmap( bitmap );
2753}
2754
2755void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
07a84e7b 2756{
31fb32e1 2757 m_isConnected = false;
07a84e7b
RR
2758
2759 GtkTreeViewColumn *column = gtk_tree_view_column_new();
9861f022 2760 m_column = (GtkWidget*) column;
07a84e7b 2761
9861f022
RR
2762 SetFlags( flags );
2763 SetAlignment( align );
07a84e7b 2764
ad386793 2765 SetWidth( width );
07a84e7b 2766
419a3607
RR
2767 // Create container for icon and label
2768 GtkWidget *box = gtk_hbox_new( FALSE, 1 );
2769 gtk_widget_show( box );
2770 // gtk_container_set_border_width((GtkContainer*)box, 2);
2771 m_image = gtk_image_new();
2772 gtk_box_pack_start(GTK_BOX(box), m_image, FALSE, FALSE, 1);
2773 m_label = gtk_label_new("");
2774 gtk_box_pack_end( GTK_BOX(box), GTK_WIDGET(m_label), FALSE, FALSE, 1 );
2775 gtk_tree_view_column_set_widget( column, box );
bc0289bf 2776
205bdf20
VZ
2777 wxDataViewRenderer * const colRenderer = GetRenderer();
2778 GtkCellRenderer * const cellRenderer = colRenderer->GetGtkHandle();
2779
2780 colRenderer->GtkPackIntoColumn(column);
07a84e7b 2781
205bdf20
VZ
2782 gtk_tree_view_column_set_cell_data_func( column, cellRenderer,
2783 wxGtkTreeCellDataFunc, (gpointer) colRenderer, NULL );
07a84e7b
RR
2784}
2785
31fb32e1
RR
2786void wxDataViewColumn::OnInternalIdle()
2787{
2788 if (m_isConnected)
2789 return;
f4322df6 2790
31fb32e1
RR
2791 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
2792 {
9861f022 2793 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
31fb32e1
RR
2794 if (column->button)
2795 {
2796 g_signal_connect(column->button, "button_press_event",
2797 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
f4322df6 2798
31fb32e1
RR
2799 m_isConnected = true;
2800 }
2801 }
2802}
2803
b94db696
RR
2804void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
2805{
2806 wxDataViewColumnBase::SetOwner( owner );
f4322df6 2807
9861f022 2808 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2809
b94db696 2810 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
b94db696
RR
2811}
2812
fa28826d
RR
2813void wxDataViewColumn::SetTitle( const wxString &title )
2814{
09dfa6a0 2815 wxDataViewCtrl *ctrl = GetOwner();
419a3607 2816 gtk_label_set_text( GTK_LABEL(m_label), ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
09dfa6a0 2817 : wxGTK_CONV_SYS(title) );
419a3607
RR
2818 if (title.empty())
2819 gtk_widget_hide( m_label );
2820 else
2821 gtk_widget_show( m_label );
07a84e7b
RR
2822}
2823
9861f022
RR
2824wxString wxDataViewColumn::GetTitle() const
2825{
7f6cbcea
VZ
2826 return wxGTK_CONV_BACK_FONT(
2827 gtk_label_get_text( GTK_LABEL(m_label) ),
2828 GetOwner()->GetFont()
2829 );
9861f022
RR
2830}
2831
07a84e7b
RR
2832void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
2833{
2834 wxDataViewColumnBase::SetBitmap( bitmap );
2835
07a84e7b
RR
2836 if (bitmap.Ok())
2837 {
419a3607 2838 GtkImage *gtk_image = GTK_IMAGE(m_image);
f4322df6 2839
d3b9f782 2840 GdkBitmap *mask = NULL;
07a84e7b
RR
2841 if (bitmap.GetMask())
2842 mask = bitmap.GetMask()->GetBitmap();
2843
2844 if (bitmap.HasPixbuf())
2845 {
2846 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
2847 bitmap.GetPixbuf());
2848 }
2849 else
2850 {
2851 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
2852 bitmap.GetPixmap(), mask);
2853 }
419a3607 2854 gtk_widget_show( m_image );
07a84e7b
RR
2855 }
2856 else
2857 {
419a3607 2858 gtk_widget_hide( m_image );
07a84e7b 2859 }
fa28826d
RR
2860}
2861
9861f022
RR
2862void wxDataViewColumn::SetHidden( bool hidden )
2863{
2864 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
2865}
2866
2867void wxDataViewColumn::SetResizeable( bool resizeable )
2868{
2869 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
2870}
2871
47cef10f
RR
2872void wxDataViewColumn::SetAlignment( wxAlignment align )
2873{
9861f022 2874 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2875
47cef10f
RR
2876 gfloat xalign = 0.0;
2877 if (align == wxALIGN_RIGHT)
2878 xalign = 1.0;
9861f022
RR
2879 if (align == wxALIGN_CENTER_HORIZONTAL ||
2880 align == wxALIGN_CENTER)
47cef10f 2881 xalign = 0.5;
f4322df6 2882
9861f022 2883 gtk_tree_view_column_set_alignment( column, xalign );
bc0289bf 2884
f2b7492a
RR
2885 if (m_renderer && m_renderer->GetAlignment() == -1)
2886 m_renderer->GtkUpdateAlignment();
9861f022
RR
2887}
2888
2889wxAlignment wxDataViewColumn::GetAlignment() const
2890{
2891 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
2892
2893 if (xalign == 1.0)
2894 return wxALIGN_RIGHT;
2895 if (xalign == 0.5)
2896 return wxALIGN_CENTER_HORIZONTAL;
f4322df6 2897
9861f022 2898 return wxALIGN_LEFT;
47cef10f
RR
2899}
2900
31fb32e1
RR
2901void wxDataViewColumn::SetSortable( bool sortable )
2902{
9861f022 2903 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
b9db5f30 2904
140a119a
VZ
2905 if ( sortable )
2906 {
2907 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
2908 }
2909 else
2910 {
2911 gtk_tree_view_column_set_sort_column_id( column, -1 );
2912 gtk_tree_view_column_set_sort_indicator( column, FALSE );
2913 gtk_tree_view_column_set_clickable( column, FALSE );
2914 }
e2bfe673
VZ
2915}
2916
2917bool wxDataViewColumn::IsSortable() const
2918{
2919 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2920 return gtk_tree_view_column_get_clickable( column );
2921}
2922
140a119a 2923void wxDataViewColumn::SetAsSortKey( bool WXUNUSED(sort) )
e2bfe673 2924{
dcb6cbec 2925 // it might not make sense to have this function in wxHeaderColumn at
140a119a
VZ
2926 // all in fact, changing of the sort order should only be done using the
2927 // associated control API
2928 wxFAIL_MSG( "not implemented" );
31fb32e1
RR
2929}
2930
e2bfe673 2931bool wxDataViewColumn::IsSortKey() const
31fb32e1 2932{
9861f022 2933 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
140a119a 2934 return gtk_tree_view_column_get_sort_indicator( column );
31fb32e1
RR
2935}
2936
9861f022
RR
2937bool wxDataViewColumn::IsResizeable() const
2938{
2939 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2940 return gtk_tree_view_column_get_resizable( column );
2941}
2942
2943bool wxDataViewColumn::IsHidden() const
2944{
2945 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2946 return !gtk_tree_view_column_get_visible( column );
2947}
2948
47cef10f
RR
2949void wxDataViewColumn::SetSortOrder( bool ascending )
2950{
9861f022 2951 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2952
47cef10f
RR
2953 if (ascending)
2954 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
2955 else
2956 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
0bd26819
RR
2957
2958 gtk_tree_view_column_set_sort_indicator( column, TRUE );
47cef10f
RR
2959}
2960
87f0efe2 2961bool wxDataViewColumn::IsSortOrderAscending() const
31fb32e1 2962{
9861f022 2963 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2964
31fb32e1
RR
2965 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
2966}
2967
9861f022 2968void wxDataViewColumn::SetMinWidth( int width )
533544f2 2969{
9861f022 2970 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
533544f2
RR
2971}
2972
9861f022
RR
2973int wxDataViewColumn::GetMinWidth() const
2974{
2975 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
2976}
2977
2978int wxDataViewColumn::GetWidth() const
533544f2 2979{
9861f022 2980 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
533544f2
RR
2981}
2982
9861f022 2983void wxDataViewColumn::SetWidth( int width )
533544f2 2984{
ad386793
RR
2985 if (width < 0)
2986 {
973dc907
RR
2987#if 1
2988 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
2989
2990 // TODO find a better calculation
97e89e29 2991 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), wxDVC_DEFAULT_WIDTH );
973dc907
RR
2992#else
2993 // this is unpractical for large numbers of items and disables
2994 // user resizing, which is totally unexpected
ad386793 2995 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
973dc907 2996#endif
ad386793
RR
2997 }
2998 else
2999 {
3000 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
7857346a 3001
ad386793
RR
3002 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3003 }
533544f2
RR
3004}
3005
99c75ebc
RR
3006void wxDataViewColumn::SetReorderable( bool reorderable )
3007{
3008 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
3009}
3010
3011bool wxDataViewColumn::IsReorderable() const
3012{
3013 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) );
3014}
9861f022 3015
4508fcd2
RR
3016//-----------------------------------------------------------------------------
3017// wxGtkTreeModelNode
3018//-----------------------------------------------------------------------------
3019
5b276ac1 3020#if 0
1f226ad8
RR
3021class wxGtkTreeModelChildWithPos
3022{
3023public:
3024 unsigned int pos;
3025 void *id;
3026};
3027
3028static
3029int wxGtkTreeModelChildWithPosCmp( const void* data1, const void* data2, const void* user_data )
3030{
3031 const wxGtkTreeModelChildWithPos* child1 = (const wxGtkTreeModelChildWithPos*) data1;
3032 const wxGtkTreeModelChildWithPos* child2 = (const wxGtkTreeModelChildWithPos*) data2;
3033 const wxDataViewCtrlInternal *internal = (const wxDataViewCtrlInternal *) user_data;
3034 int ret = internal->GetDataViewModel()->Compare( child1->id, child2->id,
5b276ac1 3035 internal->GetSortColumn(), (internal->GetSortOrder() == GTK_SORT_DESCENDING) );
1f226ad8
RR
3036
3037 return ret;
3038}
5b276ac1
RR
3039#else
3040static
3041int LINKAGEMODE wxGtkTreeModelChildPtrCmp( void*** data1, void*** data2 )
3042{
3043 return gs_internal->GetDataViewModel()->Compare( **data1, **data2,
3044 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
3045}
3046
3047WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr );
3048#endif
1f226ad8 3049
4508fcd2
RR
3050void wxGtkTreeModelNode::Resort()
3051{
af110130
RR
3052 size_t child_count = GetChildCount();
3053 if (child_count == 0)
0be79c8a
RR
3054 return;
3055
af110130
RR
3056 size_t node_count = GetNodesCount();
3057
3058 if (child_count == 1)
0be79c8a 3059 {
af110130
RR
3060 if (node_count == 1)
3061 {
3062 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
3063 node->Resort();
3064 }
0be79c8a
RR
3065 return;
3066 }
3067
5b276ac1
RR
3068 gint *new_order = new gint[child_count];
3069
1f226ad8 3070#if 1
5b276ac1
RR
3071 // m_children has the original *void
3072 // ptrs points to these
3073 wxGtkTreeModelChildrenPtr ptrs;
3074 size_t i;
3075 for (i = 0; i < child_count; i++)
3076 ptrs.Add( &(m_children[i]) );
3077 // Sort the ptrs
3078 gs_internal = m_internal;
3079 ptrs.Sort( &wxGtkTreeModelChildPtrCmp );
03647350 3080
5b276ac1
RR
3081 wxGtkTreeModelChildren temp;
3082 void** base_ptr = &(m_children[0]);
03647350 3083 // Transfer positions to new_order array and
5b276ac1
RR
3084 // IDs to temp
3085 for (i = 0; i < child_count; i++)
3086 {
3087 new_order[i] = ptrs[i] - base_ptr;
3088 temp.Add( *ptrs[i] );
3089 }
3090
3091 // Transfer IDs back to m_children
3092 m_children.Clear();
3093 WX_APPEND_ARRAY( temp, m_children );
3094#endif
3095#if 0
3096 // Too slow
3097
1f226ad8
RR
3098 // Build up array with IDs and original positions
3099 wxGtkTreeModelChildWithPos* temp = new wxGtkTreeModelChildWithPos[child_count];
3100 size_t i;
3101 for (i = 0; i < child_count; i++)
3102 {
3103 temp[i].pos = i;
3104 temp[i].id = m_children[i];
3105 }
3106 // Sort array keeping original positions
3107 wxQsort( temp, child_count, sizeof(wxGtkTreeModelChildWithPos),
3108 &wxGtkTreeModelChildWithPosCmp, m_internal );
e4a8d29c
RR
3109 // Transfer positions to new_order array and
3110 // IDs to m_children
3111 m_children.Clear();
1f226ad8 3112 for (i = 0; i < child_count; i++)
e4a8d29c 3113 {
1f226ad8 3114 new_order[i] = temp[i].pos;
1f226ad8 3115 m_children.Add( temp[i].id );
e4a8d29c 3116 }
1f226ad8
RR
3117 // Delete array
3118 delete [] temp;
5b276ac1
RR
3119#endif
3120
3121#if 0
3122 // Too slow
03647350 3123
40196b1e
RR
3124 wxGtkTreeModelChildren temp;
3125 WX_APPEND_ARRAY( temp, m_children );
b9db5f30 3126
8c2654ce 3127 gs_internal = m_internal;
40196b1e 3128 m_children.Sort( &wxGtkTreeModelChildCmp );
03647350 3129
40196b1e 3130 unsigned int pos;
af110130 3131 for (pos = 0; pos < child_count; pos++)
4508fcd2 3132 {
40196b1e
RR
3133 void *id = m_children.Item( pos );
3134 int old_pos = temp.Index( id );
3135 new_order[pos] = old_pos;
4508fcd2 3136 }
1f226ad8 3137#endif
b9db5f30 3138
0be79c8a
RR
3139 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
3140
3141 GtkTreeIter iter;
40196b1e 3142 iter.user_data = GetItem().GetID();
0be79c8a 3143 iter.stamp = m_internal->GetGtkModel()->stamp;
b9db5f30 3144
40196b1e 3145 GtkTreePath *path = m_internal->get_path( &iter );
b9db5f30 3146
0be79c8a 3147 gtk_tree_model_rows_reordered( gtk_tree_model, path, &iter, new_order );
b9db5f30 3148
0be79c8a 3149 gtk_tree_path_free (path);
b9db5f30 3150
0be79c8a 3151 delete [] new_order;
03647350 3152
1f226ad8 3153 unsigned int pos;
af110130 3154 for (pos = 0; pos < node_count; pos++)
4508fcd2 3155 {
af110130 3156 wxGtkTreeModelNode *node = m_nodes.Item( pos );
4508fcd2
RR
3157 node->Resort();
3158 }
3159}
3160
55fbde12
RR
3161//-----------------------------------------------------------------------------
3162// wxDataViewCtrlInternal
3163//-----------------------------------------------------------------------------
3164
b9db5f30 3165wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner,
55fbde12 3166 wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model )
b9db5f30 3167{
55fbde12 3168 m_owner = owner;
b9db5f30
VS
3169 m_wx_model = wx_model;
3170 m_gtk_model = gtk_model;
3171 m_root = NULL;
b8b7b087 3172 m_sort_order = GTK_SORT_ASCENDING;
40196b1e 3173 m_sort_column = -1;
d32332aa 3174 m_dataview_sort_column = NULL;
8c2654ce 3175
591cc82d
RR
3176 m_dragDataObject = NULL;
3177 m_dropDataObject = NULL;
b9db5f30 3178
e39de702 3179 if (!m_wx_model->IsVirtualListModel())
2056dede 3180 InitTree();
55fbde12 3181}
b9db5f30 3182
55fbde12
RR
3183wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3184{
3185 g_object_unref( m_gtk_model );
8c2654ce 3186
591cc82d
RR
3187 delete m_dragDataObject;
3188 delete m_dropDataObject;
55fbde12 3189}
b9db5f30 3190
ef427989
RR
3191void wxDataViewCtrlInternal::InitTree()
3192{
3193 wxDataViewItem item;
0be79c8a 3194 m_root = new wxGtkTreeModelNode( NULL, item, this );
ef427989
RR
3195
3196 BuildBranch( m_root );
3197}
3198
55fbde12
RR
3199void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
3200{
3201 if (node->GetChildCount() == 0)
3202 {
74fe973b
RR
3203 wxDataViewItemArray children;
3204 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
3205 unsigned int pos;
3206 for (pos = 0; pos < count; pos++)
55fbde12 3207 {
74fe973b 3208 wxDataViewItem child = children[pos];
b9db5f30 3209
af110130
RR
3210 if (m_wx_model->IsContainer( child ))
3211 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
3212 else
3213 node->AddLeave( child.GetID() );
b9db5f30 3214
effd54b0 3215 // Don't send any events here
55fbde12
RR
3216 }
3217 }
3218}
3219
f6f0ef85
RR
3220// GTK+ dnd iface
3221
15cac64f
RR
3222bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format )
3223{
3224 wxGtkString atom_str( gdk_atom_name( format ) );
3225 m_dragSourceTargetEntryTarget = wxCharBuffer( atom_str );
8c2654ce 3226
15cac64f
RR
3227 m_dragSourceTargetEntry.target = m_dragSourceTargetEntryTarget.data();
3228 m_dragSourceTargetEntry.flags = 0;
3229 m_dragSourceTargetEntry.info = static_cast<guint>(-1);
8c2654ce 3230
15cac64f
RR
3231 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3232 GDK_BUTTON1_MASK, &m_dragSourceTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
8c2654ce 3233
15cac64f
RR
3234 return true;
3235}
3236
e4de825e
RR
3237bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat &format )
3238{
3239 wxGtkString atom_str( gdk_atom_name( format ) );
3240 m_dropTargetTargetEntryTarget = wxCharBuffer( atom_str );
8c2654ce 3241
b4c40918 3242 m_dropTargetTargetEntry.target = m_dropTargetTargetEntryTarget.data();
e4de825e
RR
3243 m_dropTargetTargetEntry.flags = 0;
3244 m_dropTargetTargetEntry.info = static_cast<guint>(-1);
8c2654ce 3245
e4de825e
RR
3246 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3247 &m_dropTargetTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
8c2654ce 3248
e4de825e
RR
3249 return true;
3250}
3251
7857346a 3252gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
3253 GtkTreePath *path )
3254{
591cc82d 3255 delete m_dragDataObject;
8c2654ce 3256
f6f0ef85
RR
3257 GtkTreeIter iter;
3258 if (!get_iter( &iter, path )) return FALSE;
f6f0ef85
RR
3259 wxDataViewItem item( (void*) iter.user_data );
3260
591cc82d 3261 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
e4de825e 3262 event.SetEventObject( m_owner );
15cac64f
RR
3263 event.SetItem( item );
3264 event.SetModel( m_wx_model );
591cc82d
RR
3265 if (!m_owner->HandleWindowEvent( event ))
3266 return FALSE;
8c2654ce 3267
591cc82d
RR
3268 if (!event.IsAllowed())
3269 return FALSE;
8c2654ce 3270
591cc82d
RR
3271 wxDataObject *obj = event.GetDataObject();
3272 if (!obj)
3273 return FALSE;
8c2654ce 3274
591cc82d 3275 m_dragDataObject = obj;
8c2654ce 3276
591cc82d 3277 return TRUE;
f6f0ef85
RR
3278}
3279
7857346a
VZ
3280gboolean
3281wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
3282 GtkTreePath *WXUNUSED(path))
f6f0ef85
RR
3283{
3284 return FALSE;
3285}
3286
7857346a 3287gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
3288 GtkTreePath *path, GtkSelectionData *selection_data )
3289{
3290 GtkTreeIter iter;
3291 if (!get_iter( &iter, path )) return FALSE;
f6f0ef85 3292 wxDataViewItem item( (void*) iter.user_data );
7857346a 3293
591cc82d
RR
3294 if (!m_dragDataObject->IsSupported( selection_data->target ))
3295 return FALSE;
7857346a 3296
591cc82d
RR
3297 size_t size = m_dragDataObject->GetDataSize( selection_data->target );
3298 if (size == 0)
3299 return FALSE;
8c2654ce 3300
591cc82d 3301 void *buf = malloc( size );
8c2654ce 3302
15cac64f 3303 gboolean res = FALSE;
591cc82d 3304 if (m_dragDataObject->GetDataHere( selection_data->target, buf ))
15cac64f 3305 {
15cac64f 3306 res = TRUE;
8c2654ce 3307
591cc82d
RR
3308 gtk_selection_data_set( selection_data, selection_data->target,
3309 8, (const guchar*) buf, size );
15cac64f 3310 }
f6f0ef85 3311
591cc82d 3312 free( buf );
7857346a 3313
15cac64f 3314 return res;
f6f0ef85
RR
3315}
3316
7857346a
VZ
3317gboolean
3318wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
e4de825e
RR
3319 GtkTreePath *path,
3320 GtkSelectionData *selection_data)
f6f0ef85 3321{
e4de825e
RR
3322 GtkTreeIter iter;
3323 if (!get_iter( &iter, path )) return FALSE;
3324 wxDataViewItem item( (void*) iter.user_data );
8c2654ce 3325
e4de825e
RR
3326 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() );
3327 event.SetEventObject( m_owner );
3328 event.SetItem( item );
3329 event.SetModel( m_wx_model );
3330 event.SetDataFormat( selection_data->target );
3331 event.SetDataSize( selection_data->length );
3332 event.SetDataBuffer( selection_data->data );
3333 if (!m_owner->HandleWindowEvent( event ))
3334 return FALSE;
8c2654ce 3335
e4de825e
RR
3336 if (!event.IsAllowed())
3337 return FALSE;
3338
3339 return TRUE;
f6f0ef85
RR
3340}
3341
7857346a
VZ
3342gboolean
3343wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
e4de825e
RR
3344 GtkTreePath *path,
3345 GtkSelectionData *selection_data)
f6f0ef85 3346{
e4de825e
RR
3347 GtkTreeIter iter;
3348 if (!get_iter( &iter, path )) return FALSE;
3349 wxDataViewItem item( (void*) iter.user_data );
8c2654ce 3350
e4de825e
RR
3351 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
3352 event.SetEventObject( m_owner );
3353 event.SetItem( item );
3354 event.SetModel( m_wx_model );
3355 event.SetDataFormat( selection_data->target );
3356 if (!m_owner->HandleWindowEvent( event ))
3357 return FALSE;
8c2654ce 3358
e4de825e
RR
3359 if (!event.IsAllowed())
3360 return FALSE;
8c2654ce 3361
e4de825e 3362 return TRUE;
f6f0ef85
RR
3363}
3364
3365// notifications from wxDataViewModel
3366
33ba5a05
RR
3367bool wxDataViewCtrlInternal::Cleared()
3368{
8a568372
RR
3369 GtkWidget* tree_widget = GetOwner()->GtkGetTreeView();
3370 gtk_tree_view_set_model( GTK_TREE_VIEW(tree_widget), NULL );
3371 gtk_tree_view_set_model( GTK_TREE_VIEW(tree_widget), GTK_TREE_MODEL(m_gtk_model) );
03647350 3372
33ba5a05
RR
3373 if (m_root)
3374 {
3375 delete m_root;
3376 InitTree();
7857346a 3377 }
03647350 3378
33ba5a05
RR
3379 return true;
3380}
3381
4508fcd2
RR
3382void wxDataViewCtrlInternal::Resort()
3383{
e39de702 3384 if (!m_wx_model->IsVirtualListModel())
2056dede 3385 m_root->Resort();
4508fcd2
RR
3386}
3387
55fbde12
RR
3388bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
3389{
e39de702 3390 if (!m_wx_model->IsVirtualListModel())
2056dede
RR
3391 {
3392 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3393 wxASSERT_MSG(parent_node,
3394 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3395
2056dede
RR
3396 if (m_wx_model->IsContainer( item ))
3397 parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) );
3398 else
3399 parent_node->AddLeave( item.GetID() );
3400 }
b9db5f30 3401
55fbde12
RR
3402 return true;
3403}
3404
469d3e9b 3405bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
55fbde12 3406{
e39de702 3407 if (!m_wx_model->IsVirtualListModel())
2056dede
RR
3408 {
3409 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3410 wxASSERT_MSG(parent_node,
3411 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3412
2056dede
RR
3413 parent_node->DeleteChild( item.GetID() );
3414 }
b9db5f30 3415
d8331a01
RR
3416 return true;
3417}
3418
3419bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
3420{
6608fdab 3421 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
3422 event.SetEventObject( m_owner );
3423 event.SetModel( m_owner->GetModel() );
3424 event.SetItem( item );
937013e0 3425 m_owner->HandleWindowEvent( event );
b9db5f30 3426
d8331a01
RR
3427 return true;
3428}
3429
3430bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int col )
3431{
6608fdab 3432 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
3433 event.SetEventObject( m_owner );
3434 event.SetModel( m_owner->GetModel() );
3435 event.SetColumn( col );
6608fdab 3436 event.SetDataViewColumn( GetOwner()->GetColumn(col) );
d8331a01 3437 event.SetItem( item );
937013e0 3438 m_owner->HandleWindowEvent( event );
b9db5f30 3439
d8331a01
RR
3440 return true;
3441}
3442
f6f0ef85
RR
3443// GTK+ model iface
3444
2056dede
RR
3445GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
3446{
e39de702 3447 if (m_wx_model->IsVirtualListModel())
2056dede
RR
3448 return GTK_TREE_MODEL_LIST_ONLY;
3449 else
3450 return GTK_TREE_MODEL_ITERS_PERSIST;
3451}
3452
55fbde12
RR
3453gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
3454{
9330d5af 3455
e39de702 3456 if (m_wx_model->IsVirtualListModel())
2056dede 3457 {
9330d5af 3458 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
03647350 3459
2056dede 3460 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
03647350 3461
9330d5af 3462 if (i >= wx_model->GetCount())
2056dede 3463 return FALSE;
55fbde12 3464
2056dede 3465 iter->stamp = m_gtk_model->stamp;
0ae51f62
RR
3466 // user_data is just the index +1
3467 iter->user_data = (gpointer) (i+1);
55fbde12 3468
2056dede
RR
3469 return TRUE;
3470 }
3471 else
55fbde12 3472 {
2056dede
RR
3473 int depth = gtk_tree_path_get_depth( path );
3474
3475 wxGtkTreeModelNode *node = m_root;
3476
3477 int i;
b9db5f30 3478 for (i = 0; i < depth; i++)
2056dede
RR
3479 {
3480 BuildBranch( node );
b9db5f30 3481
2056dede
RR
3482 gint pos = gtk_tree_path_get_indices (path)[i];
3483 if (pos < 0) return FALSE;
3484 if ((size_t)pos >= node->GetChildCount()) return FALSE;
b9db5f30 3485
2056dede 3486 void* id = node->GetChildren().Item( (size_t) pos );
b9db5f30 3487
2056dede 3488 if (i == depth-1)
af110130 3489 {
2056dede
RR
3490 iter->stamp = m_gtk_model->stamp;
3491 iter->user_data = id;
3492 return TRUE;
af110130 3493 }
2056dede
RR
3494
3495 size_t count = node->GetNodes().GetCount();
3496 size_t pos2;
3497 for (pos2 = 0; pos2 < count; pos2++)
3498 {
3499 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
3500 if (child_node->GetItem().GetID() == id)
3501 {
3502 node = child_node;
3503 break;
3504 }
b9db5f30 3505 }
2056dede 3506 }
af110130 3507 }
55fbde12 3508
af110130 3509 return FALSE;
55fbde12
RR
3510}
3511
3512GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
3513{
8650c4ba
RR
3514 // When this is called from ItemDeleted(), the item is already
3515 // deleted in the model.
3516
55fbde12 3517 GtkTreePath *retval = gtk_tree_path_new ();
b9db5f30 3518
e39de702 3519 if (m_wx_model->IsVirtualListModel())
2056dede 3520 {
0ae51f62
RR
3521 // user_data is just the index +1
3522 int i = ( (wxUIntPtr) iter->user_data ) -1;
2056dede
RR
3523 gtk_tree_path_append_index (retval, i);
3524 }
3525 else
55fbde12 3526 {
b9db5f30
VS
3527 void *id = iter->user_data;
3528
2056dede
RR
3529 wxGtkTreeModelNode *node = FindParentNode( iter );
3530 while (node)
3531 {
3532 int pos = node->GetChildren().Index( id );
40196b1e 3533
2056dede 3534 gtk_tree_path_prepend_index( retval, pos );
b9db5f30 3535
2056dede
RR
3536 id = node->GetItem().GetID();
3537 node = node->GetParent();
3538 }
55fbde12 3539 }
b9db5f30 3540
55fbde12
RR
3541 return retval;
3542}
3543
3544gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
3545{
e39de702 3546 if (m_wx_model->IsVirtualListModel())
2056dede 3547 {
9330d5af 3548 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
b9db5f30 3549
0ae51f62
RR
3550 // user_data is just the index +1
3551 int n = ( (wxUIntPtr) iter->user_data ) -1;
69892729 3552
2056dede 3553 if (n == -1)
8a568372
RR
3554 {
3555 iter->user_data = NULL;
2056dede 3556 return FALSE;
8a568372 3557 }
40196b1e 3558
8a568372
RR
3559 if (n >= (int) wx_model->GetCount()-1)
3560 {
3561 iter->user_data = NULL;
2056dede 3562 return FALSE;
8a568372 3563 }
03647350 3564
0ae51f62
RR
3565 // user_data is just the index +1 (+2 because we need the next)
3566 iter->user_data = (gpointer) (n+2);
2056dede
RR
3567 }
3568 else
3569 {
3570 wxGtkTreeModelNode *parent = FindParentNode( iter );
3571 if( parent == NULL )
8a568372
RR
3572 {
3573 iter->user_data = NULL;
2056dede 3574 return FALSE;
8a568372 3575 }
2056dede
RR
3576
3577 int pos = parent->GetChildren().Index( iter->user_data );
3578
3579 if (pos == (int) parent->GetChildCount()-1)
8a568372
RR
3580 {
3581 iter->user_data = NULL;
2056dede 3582 return FALSE;
8a568372 3583 }
b9db5f30 3584
2056dede
RR
3585 iter->user_data = parent->GetChildren().Item( pos+1 );
3586 }
55fbde12
RR
3587
3588 return TRUE;
3589}
3590
3591gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
3592{
e39de702 3593 if (m_wx_model->IsVirtualListModel())
2056dede
RR
3594 {
3595 // this is a list, nodes have no children
3596 if (parent)
3597 return FALSE;
3598
3599 iter->stamp = m_gtk_model->stamp;
9330d5af 3600 iter->user_data = (gpointer) 1;
2056dede
RR
3601
3602 return TRUE;
3603 }
3604 else
3605 {
8a568372
RR
3606 if (iter == NULL)
3607 {
3608 if (m_root->GetChildCount() == 0) return FALSE;
3609 iter->stamp = m_gtk_model->stamp;
3610 iter->user_data = (gpointer) m_root->GetChildren().Item( 0 );
3611 return TRUE;
3612 }
03647350 3613
2056dede 3614 wxDataViewItem item( (void*) parent->user_data );
b9db5f30 3615
2056dede
RR
3616 if (!m_wx_model->IsContainer( item ))
3617 return FALSE;
b9db5f30 3618
2056dede 3619 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3620 wxASSERT_MSG(parent_node,
3621 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3622
2056dede 3623 BuildBranch( parent_node );
b9db5f30 3624
2056dede
RR
3625 if (parent_node->GetChildCount() == 0)
3626 return FALSE;
b9db5f30 3627
2056dede
RR
3628 iter->stamp = m_gtk_model->stamp;
3629 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
3630 }
b9db5f30 3631
55fbde12
RR
3632 return TRUE;
3633}
3634
3635gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
3636{
e39de702 3637 if (m_wx_model->IsVirtualListModel())
2056dede 3638 {
9330d5af
RR
3639 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3640
3641 if (iter == NULL)
8a568372 3642 return (wx_model->GetCount() > 0);
03647350 3643
2056dede
RR
3644 // this is a list, nodes have no children
3645 return FALSE;
3646 }
3647 else
3648 {
8a568372
RR
3649 if (iter == NULL)
3650 return (m_root->GetChildCount() > 0);
03647350 3651
2056dede 3652 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3653
2056dede 3654 bool is_container = m_wx_model->IsContainer( item );
b9db5f30 3655
2056dede
RR
3656 if (!is_container)
3657 return FALSE;
b9db5f30 3658
2056dede 3659 wxGtkTreeModelNode *node = FindNode( iter );
c2489d8e
FM
3660 wxASSERT_MSG(node,
3661 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3662
2056dede 3663 BuildBranch( node );
b9db5f30 3664
2056dede
RR
3665 return (node->GetChildCount() > 0);
3666 }
55fbde12
RR
3667}
3668
3669gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
3670{
e39de702 3671 if (m_wx_model->IsVirtualListModel())
2056dede 3672 {
9330d5af 3673 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
b9db5f30 3674
2056dede 3675 if (iter == NULL)
9330d5af 3676 return (gint) wx_model->GetCount();
2056dede 3677
55fbde12 3678 return 0;
2056dede
RR
3679 }
3680 else
3681 {
11f20f99
RR
3682 if (iter == NULL)
3683 return m_root->GetChildCount();
c2489d8e 3684
2056dede 3685 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3686
2056dede
RR
3687 if (!m_wx_model->IsContainer( item ))
3688 return 0;
b9db5f30 3689
2056dede 3690 wxGtkTreeModelNode *parent_node = FindNode( iter );
c2489d8e
FM
3691 wxASSERT_MSG(parent_node,
3692 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3693
2056dede 3694 BuildBranch( parent_node );
ef427989 3695
2056dede
RR
3696 return parent_node->GetChildCount();
3697 }
55fbde12
RR
3698}
3699
3700gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
3701{
e39de702 3702 if (m_wx_model->IsVirtualListModel())
2056dede 3703 {
9330d5af 3704 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
b9db5f30 3705
2056dede
RR
3706 if (parent)
3707 return FALSE;
3708
3709 if (n < 0)
3710 return FALSE;
3711
9330d5af 3712 if (n >= (gint) wx_model->GetCount())
2056dede
RR
3713 return FALSE;
3714
3715 iter->stamp = m_gtk_model->stamp;
0ae51f62 3716 // user_data is just the index +1
9330d5af 3717 iter->user_data = (gpointer) (n+1);
2056dede
RR
3718
3719 return TRUE;
3720 }
3721 else
3722 {
3723 void* id = NULL;
3724 if (parent) id = (void*) parent->user_data;
3725 wxDataViewItem item( id );
b9db5f30 3726
2056dede
RR
3727 if (!m_wx_model->IsContainer( item ))
3728 return FALSE;
b9db5f30 3729
2056dede 3730 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3731 wxASSERT_MSG(parent_node,
3732 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3733
2056dede 3734 BuildBranch( parent_node );
55fbde12 3735
2056dede
RR
3736 iter->stamp = m_gtk_model->stamp;
3737 iter->user_data = parent_node->GetChildren().Item( n );
55fbde12 3738
2056dede
RR
3739 return TRUE;
3740 }
55fbde12
RR
3741}
3742
3743gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
3744{
e39de702 3745 if (m_wx_model->IsVirtualListModel())
2056dede 3746 {
55fbde12 3747 return FALSE;
2056dede
RR
3748 }
3749 else
3750 {
3751 wxGtkTreeModelNode *node = FindParentNode( child );
3752 if (!node)
3753 return FALSE;
b9db5f30 3754
2056dede
RR
3755 iter->stamp = m_gtk_model->stamp;
3756 iter->user_data = (gpointer) node->GetItem().GetID();
55fbde12 3757
2056dede
RR
3758 return TRUE;
3759 }
55fbde12 3760}
b9db5f30 3761
8650c4ba
RR
3762// item can be deleted already in the model
3763int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item )
3764{
3765 wxGtkTreeModelNode *parent_node = FindNode( parent );
3766 wxGtkTreeModelChildren &children = parent_node->GetChildren();
3767 size_t j;
3768 for (j = 0; j < children.GetCount(); j++)
3769 {
3770 if (children[j] == item.GetID())
3771 return j;
3772 }
3773 return -1;
3774}
3775
3776
55fbde12 3777static wxGtkTreeModelNode*
69892729 3778wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
55fbde12 3779{
69892729
RR
3780 if( model == NULL )
3781 return NULL;
55fbde12 3782
69892729
RR
3783 ItemList list;
3784 list.DeleteContents( true );
3785 wxDataViewItem it( item );
b9db5f30 3786
69892729 3787 while( it.IsOk() )
55fbde12 3788 {
69892729
RR
3789 wxDataViewItem * pItem = new wxDataViewItem( it );
3790 list.Insert( pItem );
3791 it = model->GetParent( it );
3792 }
3793
3794 wxGtkTreeModelNode * node = treeNode;
966cb94d 3795 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
69892729
RR
3796 {
3797 if( node && node->GetNodes().GetCount() != 0 )
ef427989 3798 {
69892729 3799 int len = node->GetNodes().GetCount();
8650c4ba 3800 wxGtkTreeModelNodes &nodes = node->GetNodes();
69892729
RR
3801 int j = 0;
3802 for( ; j < len; j ++)
3803 {
3804 if( nodes[j]->GetItem() == *(n->GetData()))
3805 {
3806 node = nodes[j];
3807 break;
b9db5f30 3808 }
69892729
RR
3809 }
3810
3811 if( j == len )
3812 {
3813 return NULL;
3814 }
ef427989 3815 }
69892729
RR
3816 else
3817 return NULL;
55fbde12 3818 }
69892729
RR
3819 return node;
3820
55fbde12
RR
3821}
3822
3823wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
3824{
3825 if (!iter)
3826 return m_root;
3827
3828 wxDataViewItem item( (void*) iter->user_data );
0be79c8a
RR
3829 if (!item.IsOk())
3830 return m_root;
b9db5f30 3831
69892729 3832 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
b9db5f30 3833
c2489d8e 3834/*
55fbde12
RR
3835 if (!result)
3836 {
26f119eb 3837 wxLogDebug( "Not found %p", iter->user_data );
55fbde12
RR
3838 char *crash = NULL;
3839 *crash = 0;
3840 }
c2489d8e
FM
3841 // TODO: remove this code
3842*/
b9db5f30 3843
55fbde12
RR
3844 return result;
3845}
3846
3847wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
3848{
0be79c8a
RR
3849 if (!item.IsOk())
3850 return m_root;
3851
69892729 3852 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
b9db5f30 3853
c2489d8e 3854/*
55fbde12
RR
3855 if (!result)
3856 {
26f119eb 3857 wxLogDebug( "Not found %p", item.GetID() );
55fbde12
RR
3858 char *crash = NULL;
3859 *crash = 0;
3860 }
c2489d8e
FM
3861 // TODO: remove this code
3862*/
b9db5f30 3863
55fbde12
RR
3864 return result;
3865}
3866
af110130 3867static wxGtkTreeModelNode*
69892729 3868wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
af110130 3869{
69892729
RR
3870 if( model == NULL )
3871 return NULL;
3872
3873 ItemList list;
3874 list.DeleteContents( true );
3875 if( !item.IsOk() )
3876 return NULL;
3877
3878 wxDataViewItem it( model->GetParent( item ) );
3879 while( it.IsOk() )
af110130 3880 {
69892729
RR
3881 wxDataViewItem * pItem = new wxDataViewItem( it );
3882 list.Insert( pItem );
3883 it = model->GetParent( it );
af110130
RR
3884 }
3885
69892729 3886 wxGtkTreeModelNode * node = treeNode;
966cb94d 3887 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
af110130 3888 {
69892729 3889 if( node && node->GetNodes().GetCount() != 0 )
af110130 3890 {
69892729
RR
3891 int len = node->GetNodes().GetCount();
3892 wxGtkTreeModelNodes nodes = node->GetNodes();
3893 int j = 0;
3894 for( ; j < len; j ++)
3895 {
3896 if( nodes[j]->GetItem() == *(n->GetData()))
3897 {
3898 node = nodes[j];
3899 break;
b9db5f30 3900 }
69892729
RR
3901 }
3902
3903 if( j == len )
3904 {
3905 return NULL;
3906 }
af110130 3907 }
69892729
RR
3908 else
3909 return NULL;
3910 }
3911 //Examine whether the node is item's parent node
3912 int len = node->GetChildCount();
3913 for( int i = 0; i < len ; i ++ )
3914 {
3915 if( node->GetChildren().Item( i ) == item.GetID() )
3916 return node;
af110130 3917 }
af110130
RR
3918 return NULL;
3919}
3920
3921wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
3922{
3923 if (!iter)
3924 return NULL;
b9db5f30 3925
af110130
RR
3926 wxDataViewItem item( (void*) iter->user_data );
3927 if (!item.IsOk())
3928 return NULL;
3929
69892729 3930 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
3931}
3932
3933wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
3934{
3935 if (!item.IsOk())
3936 return NULL;
3937
69892729 3938 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
3939}
3940
eb7f97f8
RR
3941//-----------------------------------------------------------------------------
3942// wxDataViewCtrl signal callbacks
3943//-----------------------------------------------------------------------------
3944
3945static void
ad386793 3946wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv )
eb7f97f8 3947{
b94db696
RR
3948 if (!GTK_WIDGET_REALIZED(dv->m_widget))
3949 return;
f4322df6 3950
d86c1870 3951 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() );
6848478c 3952 event.SetItem( dv->GetSelection() );
eb7f97f8 3953 event.SetModel( dv->GetModel() );
937013e0 3954 dv->HandleWindowEvent( event );
eb7f97f8
RR
3955}
3956
f828871d 3957static void
ad386793
RR
3958wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path,
3959 GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv )
f828871d 3960{
e0062c04 3961 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
55fbde12
RR
3962
3963 GtkTreeIter iter;
3964 dv->GtkGetInternal()->get_iter( &iter, path );
3965 wxDataViewItem item( (void*) iter.user_data );;
3966 event.SetItem( item );
f828871d 3967 event.SetModel( dv->GetModel() );
937013e0 3968 dv->HandleWindowEvent( event );
f828871d
RR
3969}
3970
718fd180 3971static gboolean
ad386793
RR
3972wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3973 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3974{
3975 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
3976
3977 wxDataViewItem item( (void*) iter->user_data );;
3978 event.SetItem( item );
3979 event.SetModel( dv->GetModel() );
937013e0 3980 dv->HandleWindowEvent( event );
b9db5f30 3981
718fd180
RR
3982 return !event.IsAllowed();
3983}
3984
3985static void
ad386793
RR
3986wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3987 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3988{
3989 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
3990
3991 wxDataViewItem item( (void*) iter->user_data );;
3992 event.SetItem( item );
3993 event.SetModel( dv->GetModel() );
937013e0 3994 dv->HandleWindowEvent( event );
718fd180
RR
3995}
3996
3997static gboolean
ad386793
RR
3998wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3999 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
4000{
4001 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
4002
4003 wxDataViewItem item( (void*) iter->user_data );;
4004 event.SetItem( item );
4005 event.SetModel( dv->GetModel() );
937013e0 4006 dv->HandleWindowEvent( event );
b9db5f30 4007
718fd180
RR
4008 return !event.IsAllowed();
4009}
4010
4011static void
ad386793
RR
4012wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4013 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
4014{
4015 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
4016
4017 wxDataViewItem item( (void*) iter->user_data );;
4018 event.SetItem( item );
4019 event.SetModel( dv->GetModel() );
937013e0 4020 dv->HandleWindowEvent( event );
718fd180
RR
4021}
4022
790b137e 4023//-----------------------------------------------------------------------------
718fd180 4024 // wxDataViewCtrl
790b137e
RR
4025//-----------------------------------------------------------------------------
4026
48200154 4027void wxDataViewCtrl::AddChildGTK(wxWindowGTK* child)
1e510b1e 4028{
48200154 4029 GtkWidget* treeview = GtkGetTreeView();
1e510b1e
RR
4030
4031 // Insert widget in GtkTreeView
4032 if (GTK_WIDGET_REALIZED(treeview))
e8375af8 4033 gtk_widget_set_parent_window( child->m_widget,
1e510b1e
RR
4034 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
4035 gtk_widget_set_parent( child->m_widget, treeview );
4036}
4037
4038static
4039void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
ad386793 4040 GtkAllocation *WXUNUSED(gtk_alloc),
1e510b1e
RR
4041 wxDataViewCtrl *win )
4042{
966cb94d 4043 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
1e510b1e
RR
4044 while (node)
4045 {
4046 wxWindow *child = node->GetData();
e8375af8 4047
1e510b1e
RR
4048 GtkRequisition req;
4049 gtk_widget_size_request( child->m_widget, &req );
e8375af8 4050
1e510b1e
RR
4051 GtkAllocation alloc;
4052 alloc.x = child->m_x;
4053 alloc.y = child->m_y;
4054 alloc.width = child->m_width;
4055 alloc.height = child->m_height;
4056 gtk_widget_size_allocate( child->m_widget, &alloc );
e8375af8 4057
1e510b1e
RR
4058 node = node->GetNext();
4059 }
4060}
4061
4062
6608fdab
RR
4063//-----------------------------------------------------------------------------
4064// "motion_notify_event"
4065//-----------------------------------------------------------------------------
4066
4067static gboolean
ad386793 4068gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget),
74dea0de
RR
4069 GdkEventMotion *gdk_event,
4070 wxDataViewCtrl *dv )
6608fdab
RR
4071{
4072 if (gdk_event->is_hint)
4073 {
4074 int x = 0;
4075 int y = 0;
4076 GdkModifierType state;
4077 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
4078 gdk_event->x = x;
4079 gdk_event->y = y;
4080 }
4081
4082 GtkTreePath *path = NULL;
4083 GtkTreeViewColumn *column = NULL;
4084 gint cell_x = 0;
4085 gint cell_y = 0;
b9db5f30 4086 if (gtk_tree_view_get_path_at_pos(
6608fdab
RR
4087 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4088 (int) gdk_event->x, (int) gdk_event->y,
4089 &path,
4090 &column,
4091 &cell_x,
4092 &cell_y))
4093 {
4094 if (path)
4095 {
4096 GtkTreeIter iter;
4097 dv->GtkGetInternal()->get_iter( &iter, path );
b9db5f30 4098
6608fdab
RR
4099 gtk_tree_path_free( path );
4100 }
4101 }
4102
4103
4104 return FALSE;
4105}
4106
74dea0de
RR
4107//-----------------------------------------------------------------------------
4108// "button_press_event"
4109//-----------------------------------------------------------------------------
4110
4111static gboolean
4112gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget),
4113 GdkEventButton *gdk_event,
4114 wxDataViewCtrl *dv )
4115{
4116 if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS))
4117 {
4118 GtkTreePath *path = NULL;
4119 GtkTreeViewColumn *column = NULL;
4120 gint cell_x = 0;
4121 gint cell_y = 0;
4122 if (gtk_tree_view_get_path_at_pos(
4123 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4124 (int) gdk_event->x, (int) gdk_event->y,
4125 &path,
4126 &column,
4127 &cell_x,
4128 &cell_y))
4129 {
4130 if (path)
4131 {
4132 GtkTreeIter iter;
4133 dv->GtkGetInternal()->get_iter( &iter, path );
4134
4135 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() );
4136 wxDataViewItem item( (void*) iter.user_data );;
4137 event.SetItem( item );
4138 event.SetModel( dv->GetModel() );
4139 bool ret = dv->HandleWindowEvent( event );
4140 gtk_tree_path_free( path );
4141 return ret;
4142 }
4143 }
4144 }
4145
4146 return FALSE;
4147}
1e510b1e 4148
239eaa41
RR
4149IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4150
4151wxDataViewCtrl::~wxDataViewCtrl()
4152{
8f850e28
RR
4153 if (m_notifier)
4154 GetModel()->RemoveNotifier( m_notifier );
87f0efe2 4155
ef11ea71
RR
4156 m_cols.Clear();
4157
87f0efe2
RR
4158 // remove the model from the GtkTreeView before it gets destroyed by the
4159 // wxDataViewCtrlBase's dtor
4160 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL );
b9db5f30 4161
55fbde12 4162 delete m_internal;
239eaa41
RR
4163}
4164
4165void wxDataViewCtrl::Init()
4166{
8f850e28 4167 m_notifier = NULL;
33ba5a05 4168 m_internal = NULL;
bc0289bf 4169
ef11ea71 4170 m_cols.DeleteContents( true );
239eaa41
RR
4171}
4172
4173bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
93763ad5 4174 const wxPoint& pos, const wxSize& size,
239eaa41
RR
4175 long style, const wxValidator& validator )
4176{
239eaa41
RR
4177 if (!PreCreation( parent, pos, size ) ||
4178 !CreateBase( parent, id, pos, size, style, validator ))
4179 {
4180 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
93763ad5 4181 return false;
239eaa41 4182 }
1a367564
RR
4183
4184 m_widget = gtk_scrolled_window_new (NULL, NULL);
9ff9d30c 4185 g_object_ref(m_widget);
6493aaca 4186
496e7ec6 4187 GTKScrolledWindowSetBorder(m_widget, style);
1a367564
RR
4188
4189 m_treeview = gtk_tree_view_new();
4190 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
e8375af8 4191
1e510b1e
RR
4192 g_signal_connect (m_treeview, "size_allocate",
4193 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
ed4b0fdc 4194
b94db696
RR
4195#ifdef __WXGTK26__
4196 if (!gtk_check_version(2,6,0))
344ed1f3
RR
4197 {
4198 bool fixed = (style & wxDV_VARIABLE_LINE_HEIGHT) == 0;
4199 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4200 }
b94db696
RR
4201#endif
4202
daebb44c
RR
4203 if (style & wxDV_MULTIPLE)
4204 {
4205 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4206 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
4207 }
93763ad5 4208
9861f022
RR
4209 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
4210
4211#ifdef __WXGTK210__
4212 if (!gtk_check_version(2,10,0))
4213 {
4214 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
f4322df6
VZ
4215
4216 if ((style & wxDV_HORIZ_RULES) != 0 &&
9861f022
RR
4217 (style & wxDV_VERT_RULES) != 0)
4218 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
4219 else if (style & wxDV_VERT_RULES)
4220 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
4221 else if (style & wxDV_HORIZ_RULES)
4222 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
4223
1a07a730
RR
4224 if (grid != GTK_TREE_VIEW_GRID_LINES_NONE)
4225 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
9861f022 4226 }
9861f022 4227#endif
1a07a730
RR
4228
4229 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 );
9861f022 4230
1a367564 4231 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
16eab2e1 4232 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1a367564 4233 gtk_widget_show (m_treeview);
93763ad5 4234
239eaa41
RR
4235 m_parent->DoAddChild( this );
4236
b94db696
RR
4237 PostCreation(size);
4238
b086d55f
RR
4239 GtkEnableSelectionEvents();
4240
718fd180 4241 g_signal_connect_after (m_treeview, "row-activated",
f828871d 4242 G_CALLBACK (wxdataview_row_activated_callback), this);
eb7f97f8 4243
718fd180
RR
4244 g_signal_connect (m_treeview, "test-collapse-row",
4245 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
4246
4247 g_signal_connect_after (m_treeview, "row-collapsed",
4248 G_CALLBACK (wxdataview_row_collapsed_callback), this);
4249
4250 g_signal_connect (m_treeview, "test-expand-row",
4251 G_CALLBACK (wxdataview_test_expand_row_callback), this);
b9db5f30 4252
718fd180
RR
4253 g_signal_connect_after (m_treeview, "row-expanded",
4254 G_CALLBACK (wxdataview_row_expanded_callback), this);
4255
6608fdab
RR
4256 g_signal_connect (m_treeview, "motion_notify_event",
4257 G_CALLBACK (gtk_dataview_motion_notify_callback), this);
4258
74dea0de
RR
4259 g_signal_connect (m_treeview, "button_press_event",
4260 G_CALLBACK (gtk_dataview_button_press_callback), this);
7857346a 4261
239eaa41
RR
4262 return true;
4263}
4264
31fb32e1
RR
4265void wxDataViewCtrl::OnInternalIdle()
4266{
4267 wxWindow::OnInternalIdle();
f4322df6 4268
9861f022 4269 unsigned int cols = GetColumnCount();
31fb32e1
RR
4270 unsigned int i;
4271 for (i = 0; i < cols; i++)
4272 {
4273 wxDataViewColumn *col = GetColumn( i );
4274 col->OnInternalIdle();
4275 }
4276}
4277
e0062c04 4278bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
239eaa41 4279{
c8b6d5a0 4280 if ( m_internal )
33ba5a05
RR
4281 {
4282 delete m_internal;
4283 m_internal = NULL;
33ba5a05 4284 }
7857346a 4285
239eaa41
RR
4286 if (!wxDataViewCtrlBase::AssociateModel( model ))
4287 return false;
4288
344ed1f3
RR
4289#ifdef __WXGTK26__
4290 if (!gtk_check_version(2,6,0))
4291 {
4292 bool fixed = (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) == 0) || (model->IsVirtualListModel()));
4293 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4294 }
4295#endif
4296
55fbde12
RR
4297 GtkWxTreeModel *gtk_model = wxgtk_tree_model_new();
4298 m_internal = new wxDataViewCtrlInternal( this, model, gtk_model );
4299 gtk_model->internal = m_internal;
6e2e590f 4300
55fbde12 4301 m_notifier = new wxGtkDataViewModelNotifier( gtk_model, model, this );
6e2e590f 4302
93763ad5 4303 model->AddNotifier( m_notifier );
1557c77b 4304
55fbde12 4305 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_model) );
b9db5f30 4306
55fbde12
RR
4307 // unref in wxDataViewCtrlInternal
4308 // g_object_unref( gtk_model );
93763ad5 4309
239eaa41
RR
4310 return true;
4311}
790b137e 4312
15cac64f
RR
4313bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4314{
4315 return m_internal->EnableDragSource( format );
4316}
4317
e4de825e
RR
4318bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4319{
4320 return m_internal->EnableDropTarget( format );
4321}
4322
fa28826d
RR
4323bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4324{
4325 if (!wxDataViewCtrlBase::AppendColumn(col))
4326 return false;
93763ad5 4327
91a6c655
RR
4328 m_cols.Append( col );
4329
ad386793
RR
4330#ifdef __WXGTK26__
4331 if (!gtk_check_version(2,6,0))
4332 {
4333 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4334 GTK_TREE_VIEW_COLUMN_FIXED)
4335 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4336 }
4337#endif
4338
b9db5f30 4339 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
4340 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4341
4342 return true;
4343}
4344
736fe67c
RR
4345bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4346{
4347 if (!wxDataViewCtrlBase::PrependColumn(col))
4348 return false;
4349
4350 m_cols.Insert( col );
4351
ad386793
RR
4352#ifdef __WXGTK26__
4353 if (!gtk_check_version(2,6,0))
4354 {
4355 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4356 GTK_TREE_VIEW_COLUMN_FIXED)
4357 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4358 }
4359#endif
4360
b9db5f30 4361 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
736fe67c
RR
4362 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 );
4363
4364 return true;
4365}
4366
19723525
RR
4367bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4368{
4369 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4370 return false;
4371
4372 m_cols.Insert( pos, col );
4373
4374#ifdef __WXGTK26__
4375 if (!gtk_check_version(2,6,0))
4376 {
4377 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4378 GTK_TREE_VIEW_COLUMN_FIXED)
4379 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4380 }
4381#endif
4382
4383 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4384 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), pos );
4385
4386 return true;
4387}
4388
91a6c655
RR
4389unsigned int wxDataViewCtrl::GetColumnCount() const
4390{
4391 return m_cols.GetCount();
4392}
4393
4394wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
4395{
4396 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
4397 if (!gtk_col)
4398 return NULL;
b9db5f30 4399
91a6c655 4400 wxDataViewColumnList::const_iterator iter;
ef11ea71 4401 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
91a6c655
RR
4402 {
4403 wxDataViewColumn *col = *iter;
4404 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
4405 {
4406 return col;
4407 }
4408 }
b9db5f30 4409
91a6c655
RR
4410 return NULL;
4411}
4412
4413bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4414{
b9db5f30 4415 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
4416 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
4417
ef11ea71 4418 m_cols.DeleteObject( column );
fa28826d
RR
4419
4420 return true;
4421}
4422
91a6c655
RR
4423bool wxDataViewCtrl::ClearColumns()
4424{
4425 wxDataViewColumnList::iterator iter;
ef11ea71 4426 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
91a6c655
RR
4427 {
4428 wxDataViewColumn *col = *iter;
b9db5f30 4429 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
4430 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4431 }
b9db5f30 4432
ef11ea71 4433 m_cols.Clear();
b9db5f30 4434
91a6c655
RR
4435 return true;
4436}
4437
453091c2
RR
4438int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4439{
aed836f3
RR
4440 GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetConstGtkHandle());
4441
4442 GList *list = gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4443
aed836f3 4444 gint pos = g_list_index( list, (gconstpointer) gtk_column );
b9db5f30 4445
aed836f3
RR
4446 g_list_free( list );
4447
4448 return pos;
453091c2
RR
4449}
4450
21f47fb9
RR
4451wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4452{
d32332aa 4453 return m_internal->GetDataViewSortColumn();
21f47fb9
RR
4454}
4455
f71d3ba4
RR
4456void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4457{
4458 GtkTreeIter iter;
4459 iter.user_data = item.GetID();
4460 GtkTreePath *path = m_internal->get_path( &iter );
4461 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
4462 gtk_tree_path_free( path );
4463}
4464
4465void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4466{
4467 GtkTreeIter iter;
4468 iter.user_data = item.GetID();
4469 GtkTreePath *path = m_internal->get_path( &iter );
4470 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
4471 gtk_tree_path_free( path );
739a8399
RR
4472}
4473
4474bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4475{
4476 GtkTreeIter iter;
4477 iter.user_data = item.GetID();
4478 GtkTreePath *path = m_internal->get_path( &iter );
10ab6d4f 4479 bool res = gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview), path );
739a8399 4480 gtk_tree_path_free( path );
8c2654ce 4481
10ab6d4f 4482 return res;
f71d3ba4
RR
4483}
4484
fbda518c 4485wxDataViewItem wxDataViewCtrl::GetSelection() const
1e08ad10
RR
4486{
4487 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4488
1e08ad10
RR
4489 if (m_windowStyle & wxDV_MULTIPLE)
4490 {
6848478c
RR
4491 // Report the first one
4492 GtkTreeModel *model;
4493 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
b9db5f30 4494
6848478c
RR
4495 if (list)
4496 {
4497 GtkTreePath *path = (GtkTreePath*) list->data;
4498 GtkTreeIter iter;
4499 m_internal->get_iter( &iter, path );
b9db5f30 4500
6848478c
RR
4501 // delete list
4502 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4503 g_list_free( list );
b9db5f30 4504
6848478c
RR
4505 return wxDataViewItem( (void*) iter.user_data );
4506 }
1e08ad10
RR
4507 }
4508 else
4509 {
4510 GtkTreeIter iter;
4511 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
4512 {
9d52aad3 4513 wxDataViewItem item( (void*) iter.user_data );
1e08ad10
RR
4514 return item;
4515 }
4516 }
b9db5f30 4517
1e08ad10
RR
4518 return wxDataViewItem(0);
4519}
4520
718fd180
RR
4521int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4522{
6848478c
RR
4523 sel.Clear();
4524
4525 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4526 if (HasFlag(wxDV_MULTIPLE))
4527 {
4528 GtkTreeModel *model;
4529 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
4530
4531 int count = 0;
4532 while (list)
4533 {
4534 GtkTreePath *path = (GtkTreePath*) list->data;
b9db5f30 4535
6848478c
RR
4536 GtkTreeIter iter;
4537 m_internal->get_iter( &iter, path );
4538
4539 sel.Add( wxDataViewItem( (void*) iter.user_data ) );
4540
4541 list = g_list_next( list );
4542 count++;
4543 }
4544
4545 // delete list
4546 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4547 g_list_free( list );
4548
4549 return count;
4550 }
4551 else
4552 {
4553 GtkTreeModel *model;
4554 GtkTreeIter iter;
4555 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
4556 if (has_selection)
4557 {
4558 sel.Add( wxDataViewItem( (void*) iter.user_data) );
4559 return 1;
4560 }
4561 }
4562
718fd180
RR
4563 return 0;
4564}
4565
4566void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4567{
6848478c 4568 GtkDisableSelectionEvents();
b9db5f30 4569
6848478c
RR
4570 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4571
4572 gtk_tree_selection_unselect_all( selection );
b9db5f30 4573
4219d8b0
RR
4574 wxDataViewItem last_parent;
4575
6848478c
RR
4576 size_t i;
4577 for (i = 0; i < sel.GetCount(); i++)
4578 {
4219d8b0
RR
4579 wxDataViewItem item = sel[i];
4580 wxDataViewItem parent = GetModel()->GetParent( item );
4581 if (parent)
4582 {
4583 if (parent != last_parent)
4584 ExpandAncestors(item);
4585 }
4586 last_parent = parent;
4587
6848478c 4588 GtkTreeIter iter;
0e50223f 4589 iter.stamp = m_internal->GetGtkModel()->stamp;
4219d8b0 4590 iter.user_data = (gpointer) item.GetID();
6848478c
RR
4591 gtk_tree_selection_select_iter( selection, &iter );
4592 }
b9db5f30 4593
6848478c 4594 GtkEnableSelectionEvents();
718fd180
RR
4595}
4596
4597void wxDataViewCtrl::Select( const wxDataViewItem & item )
4598{
4219d8b0
RR
4599 ExpandAncestors(item);
4600
6848478c
RR
4601 GtkDisableSelectionEvents();
4602
4603 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4604
4605 GtkTreeIter iter;
0e50223f 4606 iter.stamp = m_internal->GetGtkModel()->stamp;
6848478c
RR
4607 iter.user_data = (gpointer) item.GetID();
4608 gtk_tree_selection_select_iter( selection, &iter );
4609
4610 GtkEnableSelectionEvents();
718fd180
RR
4611}
4612
4613void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
4614{
6848478c
RR
4615 GtkDisableSelectionEvents();
4616
4617 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4618
4619 GtkTreeIter iter;
0e50223f 4620 iter.stamp = m_internal->GetGtkModel()->stamp;
6848478c
RR
4621 iter.user_data = (gpointer) item.GetID();
4622 gtk_tree_selection_unselect_iter( selection, &iter );
4623
4624 GtkEnableSelectionEvents();
718fd180
RR
4625}
4626
4627bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
4628{
6848478c
RR
4629 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4630
4631 GtkTreeIter iter;
0e50223f 4632 iter.stamp = m_internal->GetGtkModel()->stamp;
6848478c 4633 iter.user_data = (gpointer) item.GetID();
b9db5f30 4634
6848478c 4635 return gtk_tree_selection_iter_is_selected( selection, &iter );
718fd180
RR
4636}
4637
4638void wxDataViewCtrl::SelectAll()
4639{
6848478c 4640 GtkDisableSelectionEvents();
b9db5f30 4641
6848478c 4642 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4643
6848478c 4644 gtk_tree_selection_select_all( selection );
b9db5f30 4645
6848478c 4646 GtkEnableSelectionEvents();
718fd180
RR
4647}
4648
4649void wxDataViewCtrl::UnselectAll()
4650{
6848478c 4651 GtkDisableSelectionEvents();
b9db5f30 4652
6848478c 4653 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4654
6848478c 4655 gtk_tree_selection_unselect_all( selection );
b9db5f30 4656
6848478c 4657 GtkEnableSelectionEvents();
718fd180
RR
4658}
4659
7857346a 4660void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,
4219d8b0 4661 const wxDataViewColumn *WXUNUSED(column))
718fd180 4662{
4219d8b0 4663 ExpandAncestors(item);
a881f34e 4664
6154212e
RR
4665 GtkTreeIter iter;
4666 iter.user_data = (gpointer) item.GetID();
4667 GtkTreePath *path = m_internal->get_path( &iter );
4668 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4669 gtk_tree_path_free( path );
718fd180
RR
4670}
4671
7857346a
VZ
4672void wxDataViewCtrl::HitTest(const wxPoint& WXUNUSED(point),
4673 wxDataViewItem& item,
4674 wxDataViewColumn *& column) const
66e09788
RR
4675{
4676 item = wxDataViewItem(0);
a87b466d 4677 column = NULL;
66e09788
RR
4678}
4679
7857346a
VZ
4680wxRect
4681wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item),
4682 const wxDataViewColumn *WXUNUSED(column)) const
66e09788
RR
4683{
4684 return wxRect();
4685}
4686
3b6280be
RR
4687void wxDataViewCtrl::DoSetExpanderColumn()
4688{
b9db5f30 4689 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
1b27b2bd 4690 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
3b6280be
RR
4691}
4692
4693void wxDataViewCtrl::DoSetIndent()
4694{
4695}
4696
b086d55f
RR
4697void wxDataViewCtrl::GtkDisableSelectionEvents()
4698{
4699 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
4700 g_signal_handlers_disconnect_by_func( selection,
4701 (gpointer) (wxdataview_selection_changed_callback), this);
b086d55f
RR
4702}
4703
4704void wxDataViewCtrl::GtkEnableSelectionEvents()
4705{
4706 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
4707 g_signal_connect_after (selection, "changed",
4708 G_CALLBACK (wxdataview_selection_changed_callback), this);
b086d55f
RR
4709}
4710
ddb44248
VZ
4711// ----------------------------------------------------------------------------
4712// visual attributes stuff
4713// ----------------------------------------------------------------------------
4714
b94db696
RR
4715// static
4716wxVisualAttributes
4717wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
4718{
4719 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
4720}
4721
ddb44248
VZ
4722void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
4723{
4724 wxDataViewCtrlBase::DoApplyWidgetStyle(style);
4725 gtk_widget_modify_style(m_treeview, style);
4726}
6ff7eee7 4727
ddb44248 4728#endif // !wxUSE_GENERICDATAVIEWCTRL
790b137e 4729
ddb44248 4730#endif // wxUSE_DATAVIEWCTRL