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