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