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