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