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