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