]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dataview.cpp
respect wxBU_NOTEXT style in wxButton
[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
RR
542 gtype = G_TYPE_STRING;
543 // wxFAIL_MSG( _T("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 {
72a3ac9b 594 wxFAIL_MSG( _T("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())
f4322df6
VZ
2575 wxLogError( wxT("Wrong type, required: %s but: %s"),
2576 value.GetType().c_str(),
cbc9145c 2577 cell->GetVariantType().c_str() );
93763ad5 2578
7b4fde82 2579 cell->SetValue( value );
e0743e63 2580
4264606e
RR
2581 if (cell->GtkHasAttributes())
2582 {
2583 wxDataViewItemAttr attr;
2584 bool colour_set = false;
2585 bool style_set = false;
2586 bool weight_set = false;
b9db5f30 2587
4264606e
RR
2588 if (wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr ))
2589 {
2590 // this must be a GtkCellRendererText
2591 wxColour colour = attr.GetColour();
2592 if (colour.IsOk())
2593 {
2594 const GdkColor * const gcol = colour.GetColor();
2a5e6d1b 2595
4264606e
RR
2596 GValue gvalue = { 0, };
2597 g_value_init( &gvalue, GDK_TYPE_COLOR );
2598 g_value_set_boxed( &gvalue, gcol );
2599 g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
2600 g_value_unset( &gvalue );
b9db5f30 2601
4264606e
RR
2602 colour_set = true;
2603 }
b9db5f30 2604
4264606e
RR
2605 if (attr.GetItalic())
2606 {
2607 GValue gvalue = { 0, };
2608 g_value_init( &gvalue, PANGO_TYPE_STYLE );
2609 g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
2610 g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
2611 g_value_unset( &gvalue );
b9db5f30 2612
4264606e
RR
2613 style_set = true;
2614 }
b9db5f30 2615
4264606e
RR
2616 if (attr.GetBold())
2617 {
2618 GValue gvalue = { 0, };
2619 g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
2620 g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
2621 g_object_set_property( G_OBJECT(renderer), "weight", &gvalue );
2622 g_value_unset( &gvalue );
b9db5f30 2623
4264606e
RR
2624 weight_set = true;
2625 }
2626 }
b9db5f30 2627
4264606e
RR
2628 if (!style_set)
2629 {
2630 GValue gvalue = { 0, };
2631 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2632 g_value_set_boolean( &gvalue, FALSE );
2633 g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
2634 g_value_unset( &gvalue );
2635 }
b9db5f30 2636
4264606e
RR
2637 if (!weight_set)
2638 {
2639 GValue gvalue = { 0, };
2640 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2641 g_value_set_boolean( &gvalue, FALSE );
2642 g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue );
2643 g_value_unset( &gvalue );
2644 }
b9db5f30 2645
4264606e
RR
2646 if (!colour_set)
2647 {
2648 GValue gvalue = { 0, };
2649 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2650 g_value_set_boolean( &gvalue, FALSE );
2651 g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
2652 g_value_unset( &gvalue );
2653 }
2654 }
2655
2656#if 0
2a5e6d1b
RR
2657 if (attr.HasBackgroundColour())
2658 {
2659 wxColour colour = attr.GetBackgroundColour();
1cd5e95c 2660 const GdkColor * const gcol = colour.GetColor();
e0743e63 2661
2a5e6d1b
RR
2662 GValue gvalue = { 0, };
2663 g_value_init( &gvalue, GDK_TYPE_COLOR );
2664 g_value_set_boxed( &gvalue, gcol );
2665 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
2666 g_value_unset( &gvalue );
2667 }
2668 else
2669 {
2670 GValue gvalue = { 0, };
2671 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2672 g_value_set_boolean( &gvalue, FALSE );
2673 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
2674 g_value_unset( &gvalue );
2675 }
e0062c04
RR
2676#endif
2677
7b4fde82
RR
2678}
2679
91a6c655 2680#include <wx/listimpl.cpp>
a76c2f37 2681WX_DEFINE_LIST(wxDataViewColumnList)
91a6c655 2682
f4322df6
VZ
2683wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
2684 unsigned int model_column, int width,
835d0d55
VZ
2685 wxAlignment align, int flags )
2686 : wxDataViewColumnBase( cell, model_column )
fa28826d 2687{
9861f022 2688 Init( align, flags, width );
31fb32e1 2689
31fb32e1 2690 SetTitle( title );
fa28826d
RR
2691}
2692
f4322df6
VZ
2693wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
2694 unsigned int model_column, int width,
835d0d55
VZ
2695 wxAlignment align, int flags )
2696 : wxDataViewColumnBase( bitmap, cell, model_column )
9861f022
RR
2697{
2698 Init( align, flags, width );
2699
2700 SetBitmap( bitmap );
2701}
2702
2703void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
07a84e7b 2704{
31fb32e1 2705 m_isConnected = false;
07a84e7b 2706
9861f022 2707 GtkCellRenderer *renderer = (GtkCellRenderer *) GetRenderer()->GetGtkHandle();
07a84e7b 2708 GtkTreeViewColumn *column = gtk_tree_view_column_new();
9861f022 2709 m_column = (GtkWidget*) column;
07a84e7b 2710
9861f022
RR
2711 SetFlags( flags );
2712 SetAlignment( align );
07a84e7b 2713
ad386793 2714 SetWidth( width );
07a84e7b 2715
419a3607
RR
2716 // Create container for icon and label
2717 GtkWidget *box = gtk_hbox_new( FALSE, 1 );
2718 gtk_widget_show( box );
2719 // gtk_container_set_border_width((GtkContainer*)box, 2);
2720 m_image = gtk_image_new();
2721 gtk_box_pack_start(GTK_BOX(box), m_image, FALSE, FALSE, 1);
2722 m_label = gtk_label_new("");
2723 gtk_box_pack_end( GTK_BOX(box), GTK_WIDGET(m_label), FALSE, FALSE, 1 );
2724 gtk_tree_view_column_set_widget( column, box );
bc0289bf 2725
9861f022 2726 gtk_tree_view_column_pack_end( column, renderer, TRUE );
07a84e7b
RR
2727
2728 gtk_tree_view_column_set_cell_data_func( column, renderer,
9861f022 2729 wxGtkTreeCellDataFunc, (gpointer) GetRenderer(), NULL );
07a84e7b
RR
2730}
2731
31fb32e1
RR
2732void wxDataViewColumn::OnInternalIdle()
2733{
2734 if (m_isConnected)
2735 return;
f4322df6 2736
31fb32e1
RR
2737 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
2738 {
9861f022 2739 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
31fb32e1
RR
2740 if (column->button)
2741 {
2742 g_signal_connect(column->button, "button_press_event",
2743 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
f4322df6 2744
31fb32e1
RR
2745 m_isConnected = true;
2746 }
2747 }
2748}
2749
b94db696
RR
2750void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
2751{
2752 wxDataViewColumnBase::SetOwner( owner );
f4322df6 2753
9861f022 2754 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2755
b94db696 2756 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
b94db696
RR
2757}
2758
fa28826d
RR
2759void wxDataViewColumn::SetTitle( const wxString &title )
2760{
09dfa6a0 2761 wxDataViewCtrl *ctrl = GetOwner();
419a3607 2762 gtk_label_set_text( GTK_LABEL(m_label), ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
09dfa6a0 2763 : wxGTK_CONV_SYS(title) );
419a3607
RR
2764 if (title.empty())
2765 gtk_widget_hide( m_label );
2766 else
2767 gtk_widget_show( m_label );
07a84e7b
RR
2768}
2769
9861f022
RR
2770wxString wxDataViewColumn::GetTitle() const
2771{
7f6cbcea
VZ
2772 return wxGTK_CONV_BACK_FONT(
2773 gtk_label_get_text( GTK_LABEL(m_label) ),
2774 GetOwner()->GetFont()
2775 );
9861f022
RR
2776}
2777
07a84e7b
RR
2778void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
2779{
2780 wxDataViewColumnBase::SetBitmap( bitmap );
2781
07a84e7b
RR
2782 if (bitmap.Ok())
2783 {
419a3607 2784 GtkImage *gtk_image = GTK_IMAGE(m_image);
f4322df6 2785
d3b9f782 2786 GdkBitmap *mask = NULL;
07a84e7b
RR
2787 if (bitmap.GetMask())
2788 mask = bitmap.GetMask()->GetBitmap();
2789
2790 if (bitmap.HasPixbuf())
2791 {
2792 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
2793 bitmap.GetPixbuf());
2794 }
2795 else
2796 {
2797 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
2798 bitmap.GetPixmap(), mask);
2799 }
419a3607 2800 gtk_widget_show( m_image );
07a84e7b
RR
2801 }
2802 else
2803 {
419a3607 2804 gtk_widget_hide( m_image );
07a84e7b 2805 }
fa28826d
RR
2806}
2807
9861f022
RR
2808void wxDataViewColumn::SetHidden( bool hidden )
2809{
2810 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
2811}
2812
2813void wxDataViewColumn::SetResizeable( bool resizeable )
2814{
2815 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
2816}
2817
47cef10f
RR
2818void wxDataViewColumn::SetAlignment( wxAlignment align )
2819{
9861f022 2820 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2821
47cef10f
RR
2822 gfloat xalign = 0.0;
2823 if (align == wxALIGN_RIGHT)
2824 xalign = 1.0;
9861f022
RR
2825 if (align == wxALIGN_CENTER_HORIZONTAL ||
2826 align == wxALIGN_CENTER)
47cef10f 2827 xalign = 0.5;
f4322df6 2828
9861f022 2829 gtk_tree_view_column_set_alignment( column, xalign );
bc0289bf 2830
f2b7492a
RR
2831 if (m_renderer && m_renderer->GetAlignment() == -1)
2832 m_renderer->GtkUpdateAlignment();
9861f022
RR
2833}
2834
2835wxAlignment wxDataViewColumn::GetAlignment() const
2836{
2837 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
2838
2839 if (xalign == 1.0)
2840 return wxALIGN_RIGHT;
2841 if (xalign == 0.5)
2842 return wxALIGN_CENTER_HORIZONTAL;
f4322df6 2843
9861f022 2844 return wxALIGN_LEFT;
47cef10f
RR
2845}
2846
31fb32e1
RR
2847void wxDataViewColumn::SetSortable( bool sortable )
2848{
9861f022 2849 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
b9db5f30 2850
140a119a
VZ
2851 if ( sortable )
2852 {
2853 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
2854 }
2855 else
2856 {
2857 gtk_tree_view_column_set_sort_column_id( column, -1 );
2858 gtk_tree_view_column_set_sort_indicator( column, FALSE );
2859 gtk_tree_view_column_set_clickable( column, FALSE );
2860 }
e2bfe673
VZ
2861}
2862
2863bool wxDataViewColumn::IsSortable() const
2864{
2865 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2866 return gtk_tree_view_column_get_clickable( column );
2867}
2868
140a119a 2869void wxDataViewColumn::SetAsSortKey( bool WXUNUSED(sort) )
e2bfe673 2870{
dcb6cbec 2871 // it might not make sense to have this function in wxHeaderColumn at
140a119a
VZ
2872 // all in fact, changing of the sort order should only be done using the
2873 // associated control API
2874 wxFAIL_MSG( "not implemented" );
31fb32e1
RR
2875}
2876
e2bfe673 2877bool wxDataViewColumn::IsSortKey() const
31fb32e1 2878{
9861f022 2879 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
140a119a 2880 return gtk_tree_view_column_get_sort_indicator( column );
31fb32e1
RR
2881}
2882
9861f022
RR
2883bool wxDataViewColumn::IsResizeable() const
2884{
2885 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2886 return gtk_tree_view_column_get_resizable( column );
2887}
2888
2889bool wxDataViewColumn::IsHidden() const
2890{
2891 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2892 return !gtk_tree_view_column_get_visible( column );
2893}
2894
47cef10f
RR
2895void wxDataViewColumn::SetSortOrder( bool ascending )
2896{
9861f022 2897 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2898
47cef10f
RR
2899 if (ascending)
2900 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
2901 else
2902 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
0bd26819
RR
2903
2904 gtk_tree_view_column_set_sort_indicator( column, TRUE );
47cef10f
RR
2905}
2906
87f0efe2 2907bool wxDataViewColumn::IsSortOrderAscending() const
31fb32e1 2908{
9861f022 2909 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2910
31fb32e1
RR
2911 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
2912}
2913
9861f022 2914void wxDataViewColumn::SetMinWidth( int width )
533544f2 2915{
9861f022 2916 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
533544f2
RR
2917}
2918
9861f022
RR
2919int wxDataViewColumn::GetMinWidth() const
2920{
2921 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
2922}
2923
2924int wxDataViewColumn::GetWidth() const
533544f2 2925{
9861f022 2926 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
533544f2
RR
2927}
2928
9861f022 2929void wxDataViewColumn::SetWidth( int width )
533544f2 2930{
ad386793
RR
2931 if (width < 0)
2932 {
973dc907
RR
2933#if 1
2934 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
2935
2936 // TODO find a better calculation
97e89e29 2937 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), wxDVC_DEFAULT_WIDTH );
973dc907
RR
2938#else
2939 // this is unpractical for large numbers of items and disables
2940 // user resizing, which is totally unexpected
ad386793 2941 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
973dc907 2942#endif
ad386793
RR
2943 }
2944 else
2945 {
2946 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
7857346a 2947
ad386793
RR
2948 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
2949 }
533544f2
RR
2950}
2951
99c75ebc
RR
2952void wxDataViewColumn::SetReorderable( bool reorderable )
2953{
2954 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
2955}
2956
2957bool wxDataViewColumn::IsReorderable() const
2958{
2959 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) );
2960}
9861f022 2961
4508fcd2
RR
2962//-----------------------------------------------------------------------------
2963// wxGtkTreeModelNode
2964//-----------------------------------------------------------------------------
2965
5b276ac1 2966#if 0
1f226ad8
RR
2967class wxGtkTreeModelChildWithPos
2968{
2969public:
2970 unsigned int pos;
2971 void *id;
2972};
2973
2974static
2975int wxGtkTreeModelChildWithPosCmp( const void* data1, const void* data2, const void* user_data )
2976{
2977 const wxGtkTreeModelChildWithPos* child1 = (const wxGtkTreeModelChildWithPos*) data1;
2978 const wxGtkTreeModelChildWithPos* child2 = (const wxGtkTreeModelChildWithPos*) data2;
2979 const wxDataViewCtrlInternal *internal = (const wxDataViewCtrlInternal *) user_data;
2980 int ret = internal->GetDataViewModel()->Compare( child1->id, child2->id,
5b276ac1 2981 internal->GetSortColumn(), (internal->GetSortOrder() == GTK_SORT_DESCENDING) );
1f226ad8
RR
2982
2983 return ret;
2984}
5b276ac1
RR
2985#else
2986static
2987int LINKAGEMODE wxGtkTreeModelChildPtrCmp( void*** data1, void*** data2 )
2988{
2989 return gs_internal->GetDataViewModel()->Compare( **data1, **data2,
2990 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
2991}
2992
2993WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr );
2994#endif
1f226ad8 2995
4508fcd2
RR
2996void wxGtkTreeModelNode::Resort()
2997{
af110130
RR
2998 size_t child_count = GetChildCount();
2999 if (child_count == 0)
0be79c8a
RR
3000 return;
3001
af110130
RR
3002 size_t node_count = GetNodesCount();
3003
3004 if (child_count == 1)
0be79c8a 3005 {
af110130
RR
3006 if (node_count == 1)
3007 {
3008 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
3009 node->Resort();
3010 }
0be79c8a
RR
3011 return;
3012 }
3013
5b276ac1
RR
3014 gint *new_order = new gint[child_count];
3015
1f226ad8 3016#if 1
5b276ac1
RR
3017 // m_children has the original *void
3018 // ptrs points to these
3019 wxGtkTreeModelChildrenPtr ptrs;
3020 size_t i;
3021 for (i = 0; i < child_count; i++)
3022 ptrs.Add( &(m_children[i]) );
3023 // Sort the ptrs
3024 gs_internal = m_internal;
3025 ptrs.Sort( &wxGtkTreeModelChildPtrCmp );
3026
3027 wxGtkTreeModelChildren temp;
3028 void** base_ptr = &(m_children[0]);
3029 // Transfer positions to new_order array and
3030 // IDs to temp
3031 for (i = 0; i < child_count; i++)
3032 {
3033 new_order[i] = ptrs[i] - base_ptr;
3034 temp.Add( *ptrs[i] );
3035 }
3036
3037 // Transfer IDs back to m_children
3038 m_children.Clear();
3039 WX_APPEND_ARRAY( temp, m_children );
3040#endif
3041#if 0
3042 // Too slow
3043
1f226ad8
RR
3044 // Build up array with IDs and original positions
3045 wxGtkTreeModelChildWithPos* temp = new wxGtkTreeModelChildWithPos[child_count];
3046 size_t i;
3047 for (i = 0; i < child_count; i++)
3048 {
3049 temp[i].pos = i;
3050 temp[i].id = m_children[i];
3051 }
3052 // Sort array keeping original positions
3053 wxQsort( temp, child_count, sizeof(wxGtkTreeModelChildWithPos),
3054 &wxGtkTreeModelChildWithPosCmp, m_internal );
e4a8d29c
RR
3055 // Transfer positions to new_order array and
3056 // IDs to m_children
3057 m_children.Clear();
1f226ad8 3058 for (i = 0; i < child_count; i++)
e4a8d29c 3059 {
1f226ad8 3060 new_order[i] = temp[i].pos;
1f226ad8 3061 m_children.Add( temp[i].id );
e4a8d29c 3062 }
1f226ad8
RR
3063 // Delete array
3064 delete [] temp;
5b276ac1
RR
3065#endif
3066
3067#if 0
3068 // Too slow
3069
40196b1e
RR
3070 wxGtkTreeModelChildren temp;
3071 WX_APPEND_ARRAY( temp, m_children );
b9db5f30 3072
8c2654ce 3073 gs_internal = m_internal;
40196b1e 3074 m_children.Sort( &wxGtkTreeModelChildCmp );
1f226ad8 3075
40196b1e 3076 unsigned int pos;
af110130 3077 for (pos = 0; pos < child_count; pos++)
4508fcd2 3078 {
40196b1e
RR
3079 void *id = m_children.Item( pos );
3080 int old_pos = temp.Index( id );
3081 new_order[pos] = old_pos;
4508fcd2 3082 }
1f226ad8 3083#endif
b9db5f30 3084
0be79c8a
RR
3085 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
3086
3087 GtkTreeIter iter;
40196b1e 3088 iter.user_data = GetItem().GetID();
0be79c8a 3089 iter.stamp = m_internal->GetGtkModel()->stamp;
b9db5f30 3090
40196b1e 3091 GtkTreePath *path = m_internal->get_path( &iter );
b9db5f30 3092
0be79c8a 3093 gtk_tree_model_rows_reordered( gtk_tree_model, path, &iter, new_order );
b9db5f30 3094
0be79c8a 3095 gtk_tree_path_free (path);
b9db5f30 3096
0be79c8a 3097 delete [] new_order;
1f226ad8
RR
3098
3099 unsigned int pos;
af110130 3100 for (pos = 0; pos < node_count; pos++)
4508fcd2 3101 {
af110130 3102 wxGtkTreeModelNode *node = m_nodes.Item( pos );
4508fcd2
RR
3103 node->Resort();
3104 }
3105}
3106
55fbde12
RR
3107//-----------------------------------------------------------------------------
3108// wxDataViewCtrlInternal
3109//-----------------------------------------------------------------------------
3110
b9db5f30 3111wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner,
55fbde12 3112 wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model )
b9db5f30 3113{
55fbde12 3114 m_owner = owner;
b9db5f30
VS
3115 m_wx_model = wx_model;
3116 m_gtk_model = gtk_model;
3117 m_root = NULL;
b8b7b087 3118 m_sort_order = GTK_SORT_ASCENDING;
40196b1e 3119 m_sort_column = -1;
d32332aa 3120 m_dataview_sort_column = NULL;
8c2654ce 3121
591cc82d
RR
3122 m_dragDataObject = NULL;
3123 m_dropDataObject = NULL;
b9db5f30 3124
e39de702 3125 if (!m_wx_model->IsVirtualListModel())
2056dede 3126 InitTree();
55fbde12 3127}
b9db5f30 3128
55fbde12
RR
3129wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3130{
3131 g_object_unref( m_gtk_model );
8c2654ce 3132
591cc82d
RR
3133 delete m_dragDataObject;
3134 delete m_dropDataObject;
55fbde12 3135}
b9db5f30 3136
ef427989
RR
3137void wxDataViewCtrlInternal::InitTree()
3138{
3139 wxDataViewItem item;
0be79c8a 3140 m_root = new wxGtkTreeModelNode( NULL, item, this );
ef427989
RR
3141
3142 BuildBranch( m_root );
3143}
3144
55fbde12
RR
3145void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
3146{
3147 if (node->GetChildCount() == 0)
3148 {
74fe973b
RR
3149 wxDataViewItemArray children;
3150 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
3151 unsigned int pos;
3152 for (pos = 0; pos < count; pos++)
55fbde12 3153 {
74fe973b 3154 wxDataViewItem child = children[pos];
b9db5f30 3155
af110130
RR
3156 if (m_wx_model->IsContainer( child ))
3157 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
3158 else
3159 node->AddLeave( child.GetID() );
b9db5f30 3160
effd54b0 3161 // Don't send any events here
55fbde12
RR
3162 }
3163 }
3164}
3165
f6f0ef85
RR
3166// GTK+ dnd iface
3167
15cac64f
RR
3168bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format )
3169{
3170 wxGtkString atom_str( gdk_atom_name( format ) );
3171 m_dragSourceTargetEntryTarget = wxCharBuffer( atom_str );
8c2654ce 3172
15cac64f
RR
3173 m_dragSourceTargetEntry.target = m_dragSourceTargetEntryTarget.data();
3174 m_dragSourceTargetEntry.flags = 0;
3175 m_dragSourceTargetEntry.info = static_cast<guint>(-1);
8c2654ce 3176
15cac64f
RR
3177 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3178 GDK_BUTTON1_MASK, &m_dragSourceTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
8c2654ce 3179
15cac64f
RR
3180 return true;
3181}
3182
e4de825e
RR
3183bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat &format )
3184{
3185 wxGtkString atom_str( gdk_atom_name( format ) );
3186 m_dropTargetTargetEntryTarget = wxCharBuffer( atom_str );
8c2654ce 3187
b4c40918 3188 m_dropTargetTargetEntry.target = m_dropTargetTargetEntryTarget.data();
e4de825e
RR
3189 m_dropTargetTargetEntry.flags = 0;
3190 m_dropTargetTargetEntry.info = static_cast<guint>(-1);
8c2654ce 3191
e4de825e
RR
3192 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3193 &m_dropTargetTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
8c2654ce 3194
e4de825e
RR
3195 return true;
3196}
3197
7857346a 3198gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
3199 GtkTreePath *path )
3200{
591cc82d 3201 delete m_dragDataObject;
8c2654ce 3202
f6f0ef85
RR
3203 GtkTreeIter iter;
3204 if (!get_iter( &iter, path )) return FALSE;
f6f0ef85
RR
3205 wxDataViewItem item( (void*) iter.user_data );
3206
591cc82d 3207 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
e4de825e 3208 event.SetEventObject( m_owner );
15cac64f
RR
3209 event.SetItem( item );
3210 event.SetModel( m_wx_model );
591cc82d
RR
3211 if (!m_owner->HandleWindowEvent( event ))
3212 return FALSE;
8c2654ce 3213
591cc82d
RR
3214 if (!event.IsAllowed())
3215 return FALSE;
8c2654ce 3216
591cc82d
RR
3217 wxDataObject *obj = event.GetDataObject();
3218 if (!obj)
3219 return FALSE;
8c2654ce 3220
591cc82d 3221 m_dragDataObject = obj;
8c2654ce 3222
591cc82d 3223 return TRUE;
f6f0ef85
RR
3224}
3225
7857346a
VZ
3226gboolean
3227wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
3228 GtkTreePath *WXUNUSED(path))
f6f0ef85
RR
3229{
3230 return FALSE;
3231}
3232
7857346a 3233gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
3234 GtkTreePath *path, GtkSelectionData *selection_data )
3235{
3236 GtkTreeIter iter;
3237 if (!get_iter( &iter, path )) return FALSE;
f6f0ef85 3238 wxDataViewItem item( (void*) iter.user_data );
7857346a 3239
591cc82d
RR
3240 if (!m_dragDataObject->IsSupported( selection_data->target ))
3241 return FALSE;
7857346a 3242
591cc82d
RR
3243 size_t size = m_dragDataObject->GetDataSize( selection_data->target );
3244 if (size == 0)
3245 return FALSE;
8c2654ce 3246
591cc82d 3247 void *buf = malloc( size );
8c2654ce 3248
15cac64f 3249 gboolean res = FALSE;
591cc82d 3250 if (m_dragDataObject->GetDataHere( selection_data->target, buf ))
15cac64f 3251 {
15cac64f 3252 res = TRUE;
8c2654ce 3253
591cc82d
RR
3254 gtk_selection_data_set( selection_data, selection_data->target,
3255 8, (const guchar*) buf, size );
15cac64f 3256 }
f6f0ef85 3257
591cc82d 3258 free( buf );
7857346a 3259
15cac64f 3260 return res;
f6f0ef85
RR
3261}
3262
7857346a
VZ
3263gboolean
3264wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
e4de825e
RR
3265 GtkTreePath *path,
3266 GtkSelectionData *selection_data)
f6f0ef85 3267{
e4de825e
RR
3268 GtkTreeIter iter;
3269 if (!get_iter( &iter, path )) return FALSE;
3270 wxDataViewItem item( (void*) iter.user_data );
8c2654ce 3271
e4de825e
RR
3272 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() );
3273 event.SetEventObject( m_owner );
3274 event.SetItem( item );
3275 event.SetModel( m_wx_model );
3276 event.SetDataFormat( selection_data->target );
3277 event.SetDataSize( selection_data->length );
3278 event.SetDataBuffer( selection_data->data );
3279 if (!m_owner->HandleWindowEvent( event ))
3280 return FALSE;
8c2654ce 3281
e4de825e
RR
3282 if (!event.IsAllowed())
3283 return FALSE;
3284
3285 return TRUE;
f6f0ef85
RR
3286}
3287
7857346a
VZ
3288gboolean
3289wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
e4de825e
RR
3290 GtkTreePath *path,
3291 GtkSelectionData *selection_data)
f6f0ef85 3292{
e4de825e
RR
3293 GtkTreeIter iter;
3294 if (!get_iter( &iter, path )) return FALSE;
3295 wxDataViewItem item( (void*) iter.user_data );
8c2654ce 3296
e4de825e
RR
3297 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
3298 event.SetEventObject( m_owner );
3299 event.SetItem( item );
3300 event.SetModel( m_wx_model );
3301 event.SetDataFormat( selection_data->target );
3302 if (!m_owner->HandleWindowEvent( event ))
3303 return FALSE;
8c2654ce 3304
e4de825e
RR
3305 if (!event.IsAllowed())
3306 return FALSE;
8c2654ce 3307
e4de825e 3308 return TRUE;
f6f0ef85
RR
3309}
3310
3311// notifications from wxDataViewModel
3312
33ba5a05
RR
3313bool wxDataViewCtrlInternal::Cleared()
3314{
8a568372
RR
3315 GtkWidget* tree_widget = GetOwner()->GtkGetTreeView();
3316 gtk_tree_view_set_model( GTK_TREE_VIEW(tree_widget), NULL );
3317 gtk_tree_view_set_model( GTK_TREE_VIEW(tree_widget), GTK_TREE_MODEL(m_gtk_model) );
3318
33ba5a05
RR
3319 if (m_root)
3320 {
3321 delete m_root;
3322 InitTree();
7857346a 3323 }
8a568372 3324
33ba5a05
RR
3325 return true;
3326}
3327
4508fcd2
RR
3328void wxDataViewCtrlInternal::Resort()
3329{
e39de702 3330 if (!m_wx_model->IsVirtualListModel())
2056dede 3331 m_root->Resort();
4508fcd2
RR
3332}
3333
55fbde12
RR
3334bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
3335{
e39de702 3336 if (!m_wx_model->IsVirtualListModel())
2056dede
RR
3337 {
3338 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3339 wxASSERT_MSG(parent_node,
3340 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3341
2056dede
RR
3342 if (m_wx_model->IsContainer( item ))
3343 parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) );
3344 else
3345 parent_node->AddLeave( item.GetID() );
3346 }
b9db5f30 3347
55fbde12
RR
3348 return true;
3349}
3350
469d3e9b 3351bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
55fbde12 3352{
e39de702 3353 if (!m_wx_model->IsVirtualListModel())
2056dede
RR
3354 {
3355 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3356 wxASSERT_MSG(parent_node,
3357 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3358
2056dede
RR
3359 parent_node->DeleteChild( item.GetID() );
3360 }
b9db5f30 3361
d8331a01
RR
3362 return true;
3363}
3364
3365bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
3366{
6608fdab 3367 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
3368 event.SetEventObject( m_owner );
3369 event.SetModel( m_owner->GetModel() );
3370 event.SetItem( item );
937013e0 3371 m_owner->HandleWindowEvent( event );
b9db5f30 3372
d8331a01
RR
3373 return true;
3374}
3375
3376bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int col )
3377{
6608fdab 3378 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
3379 event.SetEventObject( m_owner );
3380 event.SetModel( m_owner->GetModel() );
3381 event.SetColumn( col );
6608fdab 3382 event.SetDataViewColumn( GetOwner()->GetColumn(col) );
d8331a01 3383 event.SetItem( item );
937013e0 3384 m_owner->HandleWindowEvent( event );
b9db5f30 3385
d8331a01
RR
3386 return true;
3387}
3388
f6f0ef85
RR
3389// GTK+ model iface
3390
2056dede
RR
3391GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
3392{
e39de702 3393 if (m_wx_model->IsVirtualListModel())
2056dede
RR
3394 return GTK_TREE_MODEL_LIST_ONLY;
3395 else
3396 return GTK_TREE_MODEL_ITERS_PERSIST;
3397}
3398
55fbde12
RR
3399gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
3400{
9330d5af 3401
e39de702 3402 if (m_wx_model->IsVirtualListModel())
2056dede 3403 {
9330d5af
RR
3404 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3405
2056dede 3406 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
9330d5af
RR
3407
3408 if (i >= wx_model->GetCount())
2056dede 3409 return FALSE;
55fbde12 3410
2056dede 3411 iter->stamp = m_gtk_model->stamp;
0ae51f62
RR
3412 // user_data is just the index +1
3413 iter->user_data = (gpointer) (i+1);
55fbde12 3414
2056dede
RR
3415 return TRUE;
3416 }
3417 else
55fbde12 3418 {
2056dede
RR
3419 int depth = gtk_tree_path_get_depth( path );
3420
3421 wxGtkTreeModelNode *node = m_root;
3422
3423 int i;
b9db5f30 3424 for (i = 0; i < depth; i++)
2056dede
RR
3425 {
3426 BuildBranch( node );
b9db5f30 3427
2056dede
RR
3428 gint pos = gtk_tree_path_get_indices (path)[i];
3429 if (pos < 0) return FALSE;
3430 if ((size_t)pos >= node->GetChildCount()) return FALSE;
b9db5f30 3431
2056dede 3432 void* id = node->GetChildren().Item( (size_t) pos );
b9db5f30 3433
2056dede 3434 if (i == depth-1)
af110130 3435 {
2056dede
RR
3436 iter->stamp = m_gtk_model->stamp;
3437 iter->user_data = id;
3438 return TRUE;
af110130 3439 }
2056dede
RR
3440
3441 size_t count = node->GetNodes().GetCount();
3442 size_t pos2;
3443 for (pos2 = 0; pos2 < count; pos2++)
3444 {
3445 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
3446 if (child_node->GetItem().GetID() == id)
3447 {
3448 node = child_node;
3449 break;
3450 }
b9db5f30 3451 }
2056dede 3452 }
af110130 3453 }
55fbde12 3454
af110130 3455 return FALSE;
55fbde12
RR
3456}
3457
3458GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
3459{
3460 GtkTreePath *retval = gtk_tree_path_new ();
b9db5f30 3461
e39de702 3462 if (m_wx_model->IsVirtualListModel())
2056dede 3463 {
0ae51f62
RR
3464 // user_data is just the index +1
3465 int i = ( (wxUIntPtr) iter->user_data ) -1;
2056dede
RR
3466 gtk_tree_path_append_index (retval, i);
3467 }
3468 else
55fbde12 3469 {
b9db5f30
VS
3470 void *id = iter->user_data;
3471
2056dede
RR
3472 wxGtkTreeModelNode *node = FindParentNode( iter );
3473 while (node)
3474 {
3475 int pos = node->GetChildren().Index( id );
40196b1e 3476
2056dede 3477 gtk_tree_path_prepend_index( retval, pos );
b9db5f30 3478
2056dede
RR
3479 id = node->GetItem().GetID();
3480 node = node->GetParent();
3481 }
55fbde12 3482 }
b9db5f30 3483
55fbde12
RR
3484 return retval;
3485}
3486
3487gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
3488{
e39de702 3489 if (m_wx_model->IsVirtualListModel())
2056dede 3490 {
9330d5af 3491 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
b9db5f30 3492
0ae51f62
RR
3493 // user_data is just the index +1
3494 int n = ( (wxUIntPtr) iter->user_data ) -1;
69892729 3495
2056dede 3496 if (n == -1)
8a568372
RR
3497 {
3498 iter->user_data = NULL;
2056dede 3499 return FALSE;
8a568372 3500 }
40196b1e 3501
8a568372
RR
3502 if (n >= (int) wx_model->GetCount()-1)
3503 {
3504 iter->user_data = NULL;
2056dede 3505 return FALSE;
8a568372
RR
3506 }
3507
0ae51f62
RR
3508 // user_data is just the index +1 (+2 because we need the next)
3509 iter->user_data = (gpointer) (n+2);
2056dede
RR
3510 }
3511 else
3512 {
3513 wxGtkTreeModelNode *parent = FindParentNode( iter );
3514 if( parent == NULL )
8a568372
RR
3515 {
3516 iter->user_data = NULL;
2056dede 3517 return FALSE;
8a568372 3518 }
2056dede
RR
3519
3520 int pos = parent->GetChildren().Index( iter->user_data );
3521
3522 if (pos == (int) parent->GetChildCount()-1)
8a568372
RR
3523 {
3524 iter->user_data = NULL;
2056dede 3525 return FALSE;
8a568372 3526 }
b9db5f30 3527
2056dede
RR
3528 iter->user_data = parent->GetChildren().Item( pos+1 );
3529 }
55fbde12
RR
3530
3531 return TRUE;
3532}
3533
3534gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
3535{
e39de702 3536 if (m_wx_model->IsVirtualListModel())
2056dede
RR
3537 {
3538 // this is a list, nodes have no children
3539 if (parent)
3540 return FALSE;
3541
3542 iter->stamp = m_gtk_model->stamp;
9330d5af 3543 iter->user_data = (gpointer) 1;
2056dede
RR
3544
3545 return TRUE;
3546 }
3547 else
3548 {
8a568372
RR
3549 if (iter == NULL)
3550 {
3551 if (m_root->GetChildCount() == 0) return FALSE;
3552 iter->stamp = m_gtk_model->stamp;
3553 iter->user_data = (gpointer) m_root->GetChildren().Item( 0 );
3554 return TRUE;
3555 }
3556
2056dede 3557 wxDataViewItem item( (void*) parent->user_data );
b9db5f30 3558
2056dede
RR
3559 if (!m_wx_model->IsContainer( item ))
3560 return FALSE;
b9db5f30 3561
2056dede 3562 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3563 wxASSERT_MSG(parent_node,
3564 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3565
2056dede 3566 BuildBranch( parent_node );
b9db5f30 3567
2056dede
RR
3568 if (parent_node->GetChildCount() == 0)
3569 return FALSE;
b9db5f30 3570
2056dede
RR
3571 iter->stamp = m_gtk_model->stamp;
3572 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
3573 }
b9db5f30 3574
55fbde12
RR
3575 return TRUE;
3576}
3577
3578gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
3579{
e39de702 3580 if (m_wx_model->IsVirtualListModel())
2056dede 3581 {
9330d5af
RR
3582 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3583
3584 if (iter == NULL)
8a568372 3585 return (wx_model->GetCount() > 0);
9330d5af 3586
2056dede
RR
3587 // this is a list, nodes have no children
3588 return FALSE;
3589 }
3590 else
3591 {
8a568372
RR
3592 if (iter == NULL)
3593 return (m_root->GetChildCount() > 0);
3594
2056dede 3595 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3596
2056dede 3597 bool is_container = m_wx_model->IsContainer( item );
b9db5f30 3598
2056dede
RR
3599 if (!is_container)
3600 return FALSE;
b9db5f30 3601
2056dede 3602 wxGtkTreeModelNode *node = FindNode( iter );
c2489d8e
FM
3603 wxASSERT_MSG(node,
3604 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3605
2056dede 3606 BuildBranch( node );
b9db5f30 3607
2056dede
RR
3608 return (node->GetChildCount() > 0);
3609 }
55fbde12
RR
3610}
3611
3612gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
3613{
e39de702 3614 if (m_wx_model->IsVirtualListModel())
2056dede 3615 {
9330d5af 3616 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
b9db5f30 3617
2056dede 3618 if (iter == NULL)
9330d5af 3619 return (gint) wx_model->GetCount();
2056dede 3620
55fbde12 3621 return 0;
2056dede
RR
3622 }
3623 else
3624 {
11f20f99
RR
3625 if (iter == NULL)
3626 return m_root->GetChildCount();
c2489d8e 3627
2056dede 3628 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3629
2056dede
RR
3630 if (!m_wx_model->IsContainer( item ))
3631 return 0;
b9db5f30 3632
2056dede 3633 wxGtkTreeModelNode *parent_node = FindNode( iter );
c2489d8e
FM
3634 wxASSERT_MSG(parent_node,
3635 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3636
2056dede 3637 BuildBranch( parent_node );
ef427989 3638
2056dede
RR
3639 return parent_node->GetChildCount();
3640 }
55fbde12
RR
3641}
3642
3643gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
3644{
e39de702 3645 if (m_wx_model->IsVirtualListModel())
2056dede 3646 {
9330d5af 3647 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
b9db5f30 3648
2056dede
RR
3649 if (parent)
3650 return FALSE;
3651
3652 if (n < 0)
3653 return FALSE;
3654
9330d5af 3655 if (n >= (gint) wx_model->GetCount())
2056dede
RR
3656 return FALSE;
3657
3658 iter->stamp = m_gtk_model->stamp;
0ae51f62 3659 // user_data is just the index +1
9330d5af 3660 iter->user_data = (gpointer) (n+1);
2056dede
RR
3661
3662 return TRUE;
3663 }
3664 else
3665 {
3666 void* id = NULL;
3667 if (parent) id = (void*) parent->user_data;
3668 wxDataViewItem item( id );
b9db5f30 3669
2056dede
RR
3670 if (!m_wx_model->IsContainer( item ))
3671 return FALSE;
b9db5f30 3672
2056dede 3673 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3674 wxASSERT_MSG(parent_node,
3675 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3676
2056dede 3677 BuildBranch( parent_node );
55fbde12 3678
2056dede
RR
3679 iter->stamp = m_gtk_model->stamp;
3680 iter->user_data = parent_node->GetChildren().Item( n );
55fbde12 3681
2056dede
RR
3682 return TRUE;
3683 }
55fbde12
RR
3684}
3685
3686gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
3687{
e39de702 3688 if (m_wx_model->IsVirtualListModel())
2056dede 3689 {
55fbde12 3690 return FALSE;
2056dede
RR
3691 }
3692 else
3693 {
3694 wxGtkTreeModelNode *node = FindParentNode( child );
3695 if (!node)
3696 return FALSE;
b9db5f30 3697
2056dede
RR
3698 iter->stamp = m_gtk_model->stamp;
3699 iter->user_data = (gpointer) node->GetItem().GetID();
55fbde12 3700
2056dede
RR
3701 return TRUE;
3702 }
55fbde12 3703}
b9db5f30 3704
55fbde12 3705static wxGtkTreeModelNode*
69892729 3706wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
55fbde12 3707{
69892729
RR
3708 if( model == NULL )
3709 return NULL;
55fbde12 3710
69892729
RR
3711 ItemList list;
3712 list.DeleteContents( true );
3713 wxDataViewItem it( item );
b9db5f30 3714
69892729 3715 while( it.IsOk() )
55fbde12 3716 {
69892729
RR
3717 wxDataViewItem * pItem = new wxDataViewItem( it );
3718 list.Insert( pItem );
3719 it = model->GetParent( it );
3720 }
3721
3722 wxGtkTreeModelNode * node = treeNode;
966cb94d 3723 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
69892729
RR
3724 {
3725 if( node && node->GetNodes().GetCount() != 0 )
ef427989 3726 {
69892729
RR
3727 int len = node->GetNodes().GetCount();
3728 wxGtkTreeModelNodes nodes = node->GetNodes();
3729 int j = 0;
3730 for( ; j < len; j ++)
3731 {
3732 if( nodes[j]->GetItem() == *(n->GetData()))
3733 {
3734 node = nodes[j];
3735 break;
b9db5f30 3736 }
69892729
RR
3737 }
3738
3739 if( j == len )
3740 {
3741 return NULL;
3742 }
ef427989 3743 }
69892729
RR
3744 else
3745 return NULL;
55fbde12 3746 }
69892729
RR
3747 return node;
3748
55fbde12
RR
3749}
3750
3751wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
3752{
3753 if (!iter)
3754 return m_root;
3755
3756 wxDataViewItem item( (void*) iter->user_data );
0be79c8a
RR
3757 if (!item.IsOk())
3758 return m_root;
b9db5f30 3759
69892729 3760 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
b9db5f30 3761
c2489d8e 3762/*
55fbde12
RR
3763 if (!result)
3764 {
26f119eb 3765 wxLogDebug( "Not found %p", iter->user_data );
55fbde12
RR
3766 char *crash = NULL;
3767 *crash = 0;
3768 }
c2489d8e
FM
3769 // TODO: remove this code
3770*/
b9db5f30 3771
55fbde12
RR
3772 return result;
3773}
3774
3775wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
3776{
0be79c8a
RR
3777 if (!item.IsOk())
3778 return m_root;
3779
69892729 3780 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
b9db5f30 3781
c2489d8e 3782/*
55fbde12
RR
3783 if (!result)
3784 {
26f119eb 3785 wxLogDebug( "Not found %p", item.GetID() );
55fbde12
RR
3786 char *crash = NULL;
3787 *crash = 0;
3788 }
c2489d8e
FM
3789 // TODO: remove this code
3790*/
b9db5f30 3791
55fbde12
RR
3792 return result;
3793}
3794
af110130 3795static wxGtkTreeModelNode*
69892729 3796wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
af110130 3797{
69892729
RR
3798 if( model == NULL )
3799 return NULL;
3800
3801 ItemList list;
3802 list.DeleteContents( true );
3803 if( !item.IsOk() )
3804 return NULL;
3805
3806 wxDataViewItem it( model->GetParent( item ) );
3807 while( it.IsOk() )
af110130 3808 {
69892729
RR
3809 wxDataViewItem * pItem = new wxDataViewItem( it );
3810 list.Insert( pItem );
3811 it = model->GetParent( it );
af110130
RR
3812 }
3813
69892729 3814 wxGtkTreeModelNode * node = treeNode;
966cb94d 3815 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
af110130 3816 {
69892729 3817 if( node && node->GetNodes().GetCount() != 0 )
af110130 3818 {
69892729
RR
3819 int len = node->GetNodes().GetCount();
3820 wxGtkTreeModelNodes nodes = node->GetNodes();
3821 int j = 0;
3822 for( ; j < len; j ++)
3823 {
3824 if( nodes[j]->GetItem() == *(n->GetData()))
3825 {
3826 node = nodes[j];
3827 break;
b9db5f30 3828 }
69892729
RR
3829 }
3830
3831 if( j == len )
3832 {
3833 return NULL;
3834 }
af110130 3835 }
69892729
RR
3836 else
3837 return NULL;
3838 }
3839 //Examine whether the node is item's parent node
3840 int len = node->GetChildCount();
3841 for( int i = 0; i < len ; i ++ )
3842 {
3843 if( node->GetChildren().Item( i ) == item.GetID() )
3844 return node;
af110130 3845 }
af110130
RR
3846 return NULL;
3847}
3848
3849wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
3850{
3851 if (!iter)
3852 return NULL;
b9db5f30 3853
af110130
RR
3854 wxDataViewItem item( (void*) iter->user_data );
3855 if (!item.IsOk())
3856 return NULL;
3857
69892729 3858 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
3859}
3860
3861wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
3862{
3863 if (!item.IsOk())
3864 return NULL;
3865
69892729 3866 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
3867}
3868
eb7f97f8
RR
3869//-----------------------------------------------------------------------------
3870// wxDataViewCtrl signal callbacks
3871//-----------------------------------------------------------------------------
3872
3873static void
ad386793 3874wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv )
eb7f97f8 3875{
b94db696
RR
3876 if (!GTK_WIDGET_REALIZED(dv->m_widget))
3877 return;
f4322df6 3878
d86c1870 3879 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() );
6848478c 3880 event.SetItem( dv->GetSelection() );
eb7f97f8 3881 event.SetModel( dv->GetModel() );
937013e0 3882 dv->HandleWindowEvent( event );
eb7f97f8
RR
3883}
3884
f828871d 3885static void
ad386793
RR
3886wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path,
3887 GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv )
f828871d 3888{
e0062c04 3889 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
55fbde12
RR
3890
3891 GtkTreeIter iter;
3892 dv->GtkGetInternal()->get_iter( &iter, path );
3893 wxDataViewItem item( (void*) iter.user_data );;
3894 event.SetItem( item );
f828871d 3895 event.SetModel( dv->GetModel() );
937013e0 3896 dv->HandleWindowEvent( event );
f828871d
RR
3897}
3898
718fd180 3899static gboolean
ad386793
RR
3900wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3901 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3902{
3903 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
3904
3905 wxDataViewItem item( (void*) iter->user_data );;
3906 event.SetItem( item );
3907 event.SetModel( dv->GetModel() );
937013e0 3908 dv->HandleWindowEvent( event );
b9db5f30 3909
718fd180
RR
3910 return !event.IsAllowed();
3911}
3912
3913static void
ad386793
RR
3914wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3915 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3916{
3917 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
3918
3919 wxDataViewItem item( (void*) iter->user_data );;
3920 event.SetItem( item );
3921 event.SetModel( dv->GetModel() );
937013e0 3922 dv->HandleWindowEvent( event );
718fd180
RR
3923}
3924
3925static gboolean
ad386793
RR
3926wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3927 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3928{
3929 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
3930
3931 wxDataViewItem item( (void*) iter->user_data );;
3932 event.SetItem( item );
3933 event.SetModel( dv->GetModel() );
937013e0 3934 dv->HandleWindowEvent( event );
b9db5f30 3935
718fd180
RR
3936 return !event.IsAllowed();
3937}
3938
3939static void
ad386793
RR
3940wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3941 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3942{
3943 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
3944
3945 wxDataViewItem item( (void*) iter->user_data );;
3946 event.SetItem( item );
3947 event.SetModel( dv->GetModel() );
937013e0 3948 dv->HandleWindowEvent( event );
718fd180
RR
3949}
3950
790b137e 3951//-----------------------------------------------------------------------------
718fd180 3952 // wxDataViewCtrl
790b137e
RR
3953//-----------------------------------------------------------------------------
3954
48200154 3955void wxDataViewCtrl::AddChildGTK(wxWindowGTK* child)
1e510b1e 3956{
48200154 3957 GtkWidget* treeview = GtkGetTreeView();
1e510b1e
RR
3958
3959 // Insert widget in GtkTreeView
3960 if (GTK_WIDGET_REALIZED(treeview))
e8375af8 3961 gtk_widget_set_parent_window( child->m_widget,
1e510b1e
RR
3962 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
3963 gtk_widget_set_parent( child->m_widget, treeview );
3964}
3965
3966static
3967void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
ad386793 3968 GtkAllocation *WXUNUSED(gtk_alloc),
1e510b1e
RR
3969 wxDataViewCtrl *win )
3970{
966cb94d 3971 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
1e510b1e
RR
3972 while (node)
3973 {
3974 wxWindow *child = node->GetData();
e8375af8 3975
1e510b1e
RR
3976 GtkRequisition req;
3977 gtk_widget_size_request( child->m_widget, &req );
e8375af8 3978
1e510b1e
RR
3979 GtkAllocation alloc;
3980 alloc.x = child->m_x;
3981 alloc.y = child->m_y;
3982 alloc.width = child->m_width;
3983 alloc.height = child->m_height;
3984 gtk_widget_size_allocate( child->m_widget, &alloc );
e8375af8 3985
1e510b1e
RR
3986 node = node->GetNext();
3987 }
3988}
3989
3990
6608fdab
RR
3991//-----------------------------------------------------------------------------
3992// "motion_notify_event"
3993//-----------------------------------------------------------------------------
3994
3995static gboolean
ad386793 3996gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget),
74dea0de
RR
3997 GdkEventMotion *gdk_event,
3998 wxDataViewCtrl *dv )
6608fdab
RR
3999{
4000 if (gdk_event->is_hint)
4001 {
4002 int x = 0;
4003 int y = 0;
4004 GdkModifierType state;
4005 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
4006 gdk_event->x = x;
4007 gdk_event->y = y;
4008 }
4009
4010 GtkTreePath *path = NULL;
4011 GtkTreeViewColumn *column = NULL;
4012 gint cell_x = 0;
4013 gint cell_y = 0;
b9db5f30 4014 if (gtk_tree_view_get_path_at_pos(
6608fdab
RR
4015 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4016 (int) gdk_event->x, (int) gdk_event->y,
4017 &path,
4018 &column,
4019 &cell_x,
4020 &cell_y))
4021 {
4022 if (path)
4023 {
4024 GtkTreeIter iter;
4025 dv->GtkGetInternal()->get_iter( &iter, path );
b9db5f30 4026
6608fdab
RR
4027 gtk_tree_path_free( path );
4028 }
4029 }
4030
4031
4032 return FALSE;
4033}
4034
74dea0de
RR
4035//-----------------------------------------------------------------------------
4036// "button_press_event"
4037//-----------------------------------------------------------------------------
4038
4039static gboolean
4040gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget),
4041 GdkEventButton *gdk_event,
4042 wxDataViewCtrl *dv )
4043{
4044 if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS))
4045 {
4046 GtkTreePath *path = NULL;
4047 GtkTreeViewColumn *column = NULL;
4048 gint cell_x = 0;
4049 gint cell_y = 0;
4050 if (gtk_tree_view_get_path_at_pos(
4051 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4052 (int) gdk_event->x, (int) gdk_event->y,
4053 &path,
4054 &column,
4055 &cell_x,
4056 &cell_y))
4057 {
4058 if (path)
4059 {
4060 GtkTreeIter iter;
4061 dv->GtkGetInternal()->get_iter( &iter, path );
4062
4063 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() );
4064 wxDataViewItem item( (void*) iter.user_data );;
4065 event.SetItem( item );
4066 event.SetModel( dv->GetModel() );
4067 bool ret = dv->HandleWindowEvent( event );
4068 gtk_tree_path_free( path );
4069 return ret;
4070 }
4071 }
4072 }
4073
4074 return FALSE;
4075}
1e510b1e 4076
239eaa41
RR
4077IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4078
4079wxDataViewCtrl::~wxDataViewCtrl()
4080{
8f850e28
RR
4081 if (m_notifier)
4082 GetModel()->RemoveNotifier( m_notifier );
87f0efe2 4083
ef11ea71
RR
4084 m_cols.Clear();
4085
87f0efe2
RR
4086 // remove the model from the GtkTreeView before it gets destroyed by the
4087 // wxDataViewCtrlBase's dtor
4088 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL );
b9db5f30 4089
55fbde12 4090 delete m_internal;
239eaa41
RR
4091}
4092
4093void wxDataViewCtrl::Init()
4094{
8f850e28 4095 m_notifier = NULL;
33ba5a05 4096 m_internal = NULL;
bc0289bf 4097
ef11ea71 4098 m_cols.DeleteContents( true );
239eaa41
RR
4099}
4100
4101bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
93763ad5 4102 const wxPoint& pos, const wxSize& size,
239eaa41
RR
4103 long style, const wxValidator& validator )
4104{
4105 Init();
93763ad5 4106
239eaa41
RR
4107 if (!PreCreation( parent, pos, size ) ||
4108 !CreateBase( parent, id, pos, size, style, validator ))
4109 {
4110 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
93763ad5 4111 return false;
239eaa41 4112 }
1a367564
RR
4113
4114 m_widget = gtk_scrolled_window_new (NULL, NULL);
9ff9d30c 4115 g_object_ref(m_widget);
6493aaca 4116
496e7ec6 4117 GTKScrolledWindowSetBorder(m_widget, style);
1a367564
RR
4118
4119 m_treeview = gtk_tree_view_new();
4120 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
e8375af8 4121
1e510b1e
RR
4122 g_signal_connect (m_treeview, "size_allocate",
4123 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
ed4b0fdc 4124
b94db696
RR
4125#ifdef __WXGTK26__
4126 if (!gtk_check_version(2,6,0))
344ed1f3
RR
4127 {
4128 bool fixed = (style & wxDV_VARIABLE_LINE_HEIGHT) == 0;
4129 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4130 }
b94db696
RR
4131#endif
4132
daebb44c
RR
4133 if (style & wxDV_MULTIPLE)
4134 {
4135 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4136 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
4137 }
93763ad5 4138
9861f022
RR
4139 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
4140
4141#ifdef __WXGTK210__
4142 if (!gtk_check_version(2,10,0))
4143 {
4144 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
f4322df6
VZ
4145
4146 if ((style & wxDV_HORIZ_RULES) != 0 &&
9861f022
RR
4147 (style & wxDV_VERT_RULES) != 0)
4148 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
4149 else if (style & wxDV_VERT_RULES)
4150 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
4151 else if (style & wxDV_HORIZ_RULES)
4152 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
4153
1a07a730
RR
4154 if (grid != GTK_TREE_VIEW_GRID_LINES_NONE)
4155 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
9861f022 4156 }
9861f022 4157#endif
1a07a730
RR
4158
4159 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 );
9861f022 4160
1a367564 4161 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
16eab2e1 4162 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1a367564 4163 gtk_widget_show (m_treeview);
93763ad5 4164
239eaa41
RR
4165 m_parent->DoAddChild( this );
4166
b94db696
RR
4167 PostCreation(size);
4168
b086d55f
RR
4169 GtkEnableSelectionEvents();
4170
718fd180 4171 g_signal_connect_after (m_treeview, "row-activated",
f828871d 4172 G_CALLBACK (wxdataview_row_activated_callback), this);
eb7f97f8 4173
718fd180
RR
4174 g_signal_connect (m_treeview, "test-collapse-row",
4175 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
4176
4177 g_signal_connect_after (m_treeview, "row-collapsed",
4178 G_CALLBACK (wxdataview_row_collapsed_callback), this);
4179
4180 g_signal_connect (m_treeview, "test-expand-row",
4181 G_CALLBACK (wxdataview_test_expand_row_callback), this);
b9db5f30 4182
718fd180
RR
4183 g_signal_connect_after (m_treeview, "row-expanded",
4184 G_CALLBACK (wxdataview_row_expanded_callback), this);
4185
6608fdab
RR
4186 g_signal_connect (m_treeview, "motion_notify_event",
4187 G_CALLBACK (gtk_dataview_motion_notify_callback), this);
4188
74dea0de
RR
4189 g_signal_connect (m_treeview, "button_press_event",
4190 G_CALLBACK (gtk_dataview_button_press_callback), this);
7857346a 4191
239eaa41
RR
4192 return true;
4193}
4194
31fb32e1
RR
4195void wxDataViewCtrl::OnInternalIdle()
4196{
4197 wxWindow::OnInternalIdle();
f4322df6 4198
9861f022 4199 unsigned int cols = GetColumnCount();
31fb32e1
RR
4200 unsigned int i;
4201 for (i = 0; i < cols; i++)
4202 {
4203 wxDataViewColumn *col = GetColumn( i );
4204 col->OnInternalIdle();
4205 }
4206}
4207
e0062c04 4208bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
239eaa41 4209{
33ba5a05
RR
4210 if (GetModel())
4211 {
4212 delete m_internal;
4213 m_internal = NULL;
7857346a 4214
33ba5a05
RR
4215 delete m_notifier;
4216 m_notifier = NULL;
4217 }
7857346a 4218
239eaa41
RR
4219 if (!wxDataViewCtrlBase::AssociateModel( model ))
4220 return false;
4221
344ed1f3
RR
4222#ifdef __WXGTK26__
4223 if (!gtk_check_version(2,6,0))
4224 {
4225 bool fixed = (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) == 0) || (model->IsVirtualListModel()));
4226 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4227 }
4228#endif
4229
55fbde12
RR
4230 GtkWxTreeModel *gtk_model = wxgtk_tree_model_new();
4231 m_internal = new wxDataViewCtrlInternal( this, model, gtk_model );
4232 gtk_model->internal = m_internal;
6e2e590f 4233
55fbde12 4234 m_notifier = new wxGtkDataViewModelNotifier( gtk_model, model, this );
6e2e590f 4235
93763ad5 4236 model->AddNotifier( m_notifier );
1557c77b 4237
55fbde12 4238 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_model) );
b9db5f30 4239
55fbde12
RR
4240 // unref in wxDataViewCtrlInternal
4241 // g_object_unref( gtk_model );
93763ad5 4242
239eaa41
RR
4243 return true;
4244}
790b137e 4245
15cac64f
RR
4246bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4247{
4248 return m_internal->EnableDragSource( format );
4249}
4250
e4de825e
RR
4251bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4252{
4253 return m_internal->EnableDropTarget( format );
4254}
4255
fa28826d
RR
4256bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4257{
4258 if (!wxDataViewCtrlBase::AppendColumn(col))
4259 return false;
93763ad5 4260
91a6c655
RR
4261 m_cols.Append( col );
4262
ad386793
RR
4263#ifdef __WXGTK26__
4264 if (!gtk_check_version(2,6,0))
4265 {
4266 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4267 GTK_TREE_VIEW_COLUMN_FIXED)
4268 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4269 }
4270#endif
4271
b9db5f30 4272 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
4273 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4274
4275 return true;
4276}
4277
736fe67c
RR
4278bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4279{
4280 if (!wxDataViewCtrlBase::PrependColumn(col))
4281 return false;
4282
4283 m_cols.Insert( col );
4284
ad386793
RR
4285#ifdef __WXGTK26__
4286 if (!gtk_check_version(2,6,0))
4287 {
4288 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4289 GTK_TREE_VIEW_COLUMN_FIXED)
4290 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4291 }
4292#endif
4293
b9db5f30 4294 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
736fe67c
RR
4295 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 );
4296
4297 return true;
4298}
4299
19723525
RR
4300bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4301{
4302 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4303 return false;
4304
4305 m_cols.Insert( pos, col );
4306
4307#ifdef __WXGTK26__
4308 if (!gtk_check_version(2,6,0))
4309 {
4310 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4311 GTK_TREE_VIEW_COLUMN_FIXED)
4312 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4313 }
4314#endif
4315
4316 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4317 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), pos );
4318
4319 return true;
4320}
4321
91a6c655
RR
4322unsigned int wxDataViewCtrl::GetColumnCount() const
4323{
4324 return m_cols.GetCount();
4325}
4326
4327wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
4328{
4329 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
4330 if (!gtk_col)
4331 return NULL;
b9db5f30 4332
91a6c655 4333 wxDataViewColumnList::const_iterator iter;
ef11ea71 4334 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
91a6c655
RR
4335 {
4336 wxDataViewColumn *col = *iter;
4337 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
4338 {
4339 return col;
4340 }
4341 }
b9db5f30 4342
91a6c655
RR
4343 return NULL;
4344}
4345
4346bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4347{
b9db5f30 4348 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
4349 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
4350
ef11ea71 4351 m_cols.DeleteObject( column );
fa28826d
RR
4352
4353 return true;
4354}
4355
91a6c655
RR
4356bool wxDataViewCtrl::ClearColumns()
4357{
4358 wxDataViewColumnList::iterator iter;
ef11ea71 4359 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
91a6c655
RR
4360 {
4361 wxDataViewColumn *col = *iter;
b9db5f30 4362 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
4363 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4364 }
b9db5f30 4365
ef11ea71 4366 m_cols.Clear();
b9db5f30 4367
91a6c655
RR
4368 return true;
4369}
4370
453091c2
RR
4371int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4372{
aed836f3
RR
4373 GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetConstGtkHandle());
4374
4375 GList *list = gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4376
aed836f3 4377 gint pos = g_list_index( list, (gconstpointer) gtk_column );
b9db5f30 4378
aed836f3
RR
4379 g_list_free( list );
4380
4381 return pos;
453091c2
RR
4382}
4383
21f47fb9
RR
4384wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4385{
d32332aa 4386 return m_internal->GetDataViewSortColumn();
21f47fb9
RR
4387}
4388
f71d3ba4
RR
4389void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4390{
4391 GtkTreeIter iter;
4392 iter.user_data = item.GetID();
4393 GtkTreePath *path = m_internal->get_path( &iter );
4394 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
4395 gtk_tree_path_free( path );
4396}
4397
4398void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4399{
4400 GtkTreeIter iter;
4401 iter.user_data = item.GetID();
4402 GtkTreePath *path = m_internal->get_path( &iter );
4403 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
4404 gtk_tree_path_free( path );
739a8399
RR
4405}
4406
4407bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4408{
4409 GtkTreeIter iter;
4410 iter.user_data = item.GetID();
4411 GtkTreePath *path = m_internal->get_path( &iter );
10ab6d4f 4412 bool res = gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview), path );
739a8399 4413 gtk_tree_path_free( path );
8c2654ce 4414
10ab6d4f 4415 return res;
f71d3ba4
RR
4416}
4417
fbda518c 4418wxDataViewItem wxDataViewCtrl::GetSelection() const
1e08ad10
RR
4419{
4420 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4421
1e08ad10
RR
4422 if (m_windowStyle & wxDV_MULTIPLE)
4423 {
6848478c
RR
4424 // Report the first one
4425 GtkTreeModel *model;
4426 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
b9db5f30 4427
6848478c
RR
4428 if (list)
4429 {
4430 GtkTreePath *path = (GtkTreePath*) list->data;
4431 GtkTreeIter iter;
4432 m_internal->get_iter( &iter, path );
b9db5f30 4433
6848478c
RR
4434 // delete list
4435 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4436 g_list_free( list );
b9db5f30 4437
6848478c
RR
4438 return wxDataViewItem( (void*) iter.user_data );
4439 }
1e08ad10
RR
4440 }
4441 else
4442 {
4443 GtkTreeIter iter;
4444 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
4445 {
9d52aad3 4446 wxDataViewItem item( (void*) iter.user_data );
1e08ad10
RR
4447 return item;
4448 }
4449 }
b9db5f30 4450
1e08ad10
RR
4451 return wxDataViewItem(0);
4452}
4453
718fd180
RR
4454int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4455{
6848478c
RR
4456 sel.Clear();
4457
4458 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4459 if (HasFlag(wxDV_MULTIPLE))
4460 {
4461 GtkTreeModel *model;
4462 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
4463
4464 int count = 0;
4465 while (list)
4466 {
4467 GtkTreePath *path = (GtkTreePath*) list->data;
b9db5f30 4468
6848478c
RR
4469 GtkTreeIter iter;
4470 m_internal->get_iter( &iter, path );
4471
4472 sel.Add( wxDataViewItem( (void*) iter.user_data ) );
4473
4474 list = g_list_next( list );
4475 count++;
4476 }
4477
4478 // delete list
4479 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4480 g_list_free( list );
4481
4482 return count;
4483 }
4484 else
4485 {
4486 GtkTreeModel *model;
4487 GtkTreeIter iter;
4488 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
4489 if (has_selection)
4490 {
4491 sel.Add( wxDataViewItem( (void*) iter.user_data) );
4492 return 1;
4493 }
4494 }
4495
718fd180
RR
4496 return 0;
4497}
4498
4499void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4500{
6848478c 4501 GtkDisableSelectionEvents();
b9db5f30 4502
6848478c
RR
4503 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4504
4505 gtk_tree_selection_unselect_all( selection );
b9db5f30 4506
4219d8b0
RR
4507 wxDataViewItem last_parent;
4508
6848478c
RR
4509 size_t i;
4510 for (i = 0; i < sel.GetCount(); i++)
4511 {
4219d8b0
RR
4512 wxDataViewItem item = sel[i];
4513 wxDataViewItem parent = GetModel()->GetParent( item );
4514 if (parent)
4515 {
4516 if (parent != last_parent)
4517 ExpandAncestors(item);
4518 }
4519 last_parent = parent;
4520
6848478c 4521 GtkTreeIter iter;
0e50223f 4522 iter.stamp = m_internal->GetGtkModel()->stamp;
4219d8b0 4523 iter.user_data = (gpointer) item.GetID();
6848478c
RR
4524 gtk_tree_selection_select_iter( selection, &iter );
4525 }
b9db5f30 4526
6848478c 4527 GtkEnableSelectionEvents();
718fd180
RR
4528}
4529
4530void wxDataViewCtrl::Select( const wxDataViewItem & item )
4531{
4219d8b0
RR
4532 ExpandAncestors(item);
4533
6848478c
RR
4534 GtkDisableSelectionEvents();
4535
4536 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4537
4538 GtkTreeIter iter;
0e50223f 4539 iter.stamp = m_internal->GetGtkModel()->stamp;
6848478c
RR
4540 iter.user_data = (gpointer) item.GetID();
4541 gtk_tree_selection_select_iter( selection, &iter );
4542
4543 GtkEnableSelectionEvents();
718fd180
RR
4544}
4545
4546void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
4547{
6848478c
RR
4548 GtkDisableSelectionEvents();
4549
4550 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4551
4552 GtkTreeIter iter;
0e50223f 4553 iter.stamp = m_internal->GetGtkModel()->stamp;
6848478c
RR
4554 iter.user_data = (gpointer) item.GetID();
4555 gtk_tree_selection_unselect_iter( selection, &iter );
4556
4557 GtkEnableSelectionEvents();
718fd180
RR
4558}
4559
4560bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
4561{
6848478c
RR
4562 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4563
4564 GtkTreeIter iter;
0e50223f 4565 iter.stamp = m_internal->GetGtkModel()->stamp;
6848478c 4566 iter.user_data = (gpointer) item.GetID();
b9db5f30 4567
6848478c 4568 return gtk_tree_selection_iter_is_selected( selection, &iter );
718fd180
RR
4569}
4570
4571void wxDataViewCtrl::SelectAll()
4572{
6848478c 4573 GtkDisableSelectionEvents();
b9db5f30 4574
6848478c 4575 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4576
6848478c 4577 gtk_tree_selection_select_all( selection );
b9db5f30 4578
6848478c 4579 GtkEnableSelectionEvents();
718fd180
RR
4580}
4581
4582void wxDataViewCtrl::UnselectAll()
4583{
6848478c 4584 GtkDisableSelectionEvents();
b9db5f30 4585
6848478c 4586 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4587
6848478c 4588 gtk_tree_selection_unselect_all( selection );
b9db5f30 4589
6848478c 4590 GtkEnableSelectionEvents();
718fd180
RR
4591}
4592
7857346a 4593void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,
4219d8b0 4594 const wxDataViewColumn *WXUNUSED(column))
718fd180 4595{
4219d8b0 4596 ExpandAncestors(item);
a881f34e 4597
6154212e
RR
4598 GtkTreeIter iter;
4599 iter.user_data = (gpointer) item.GetID();
4600 GtkTreePath *path = m_internal->get_path( &iter );
4601 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4602 gtk_tree_path_free( path );
718fd180
RR
4603}
4604
7857346a
VZ
4605void wxDataViewCtrl::HitTest(const wxPoint& WXUNUSED(point),
4606 wxDataViewItem& item,
4607 wxDataViewColumn *& column) const
66e09788
RR
4608{
4609 item = wxDataViewItem(0);
a87b466d 4610 column = NULL;
66e09788
RR
4611}
4612
7857346a
VZ
4613wxRect
4614wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item),
4615 const wxDataViewColumn *WXUNUSED(column)) const
66e09788
RR
4616{
4617 return wxRect();
4618}
4619
3b6280be
RR
4620void wxDataViewCtrl::DoSetExpanderColumn()
4621{
b9db5f30 4622 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
1b27b2bd 4623 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
3b6280be
RR
4624}
4625
4626void wxDataViewCtrl::DoSetIndent()
4627{
4628}
4629
b086d55f
RR
4630void wxDataViewCtrl::GtkDisableSelectionEvents()
4631{
4632 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
4633 g_signal_handlers_disconnect_by_func( selection,
4634 (gpointer) (wxdataview_selection_changed_callback), this);
b086d55f
RR
4635}
4636
4637void wxDataViewCtrl::GtkEnableSelectionEvents()
4638{
4639 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
4640 g_signal_connect_after (selection, "changed",
4641 G_CALLBACK (wxdataview_selection_changed_callback), this);
b086d55f
RR
4642}
4643
b94db696
RR
4644// static
4645wxVisualAttributes
4646wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
4647{
4648 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
4649}
4650
6ff7eee7 4651
93763ad5 4652#endif
4ed7af08 4653 // !wxUSE_GENERICDATAVIEWCTRL
790b137e 4654
93763ad5 4655#endif
4ed7af08 4656 // wxUSE_DATAVIEWCTRL