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