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