]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dataview.cpp
Fix button order in wxGTK wxMessageDialog and wxStdDialogButtonSizer.
[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
RR
233 bool ItemChanged( const wxDataViewItem &item );
234 bool ValueChanged( const wxDataViewItem &item, unsigned int col );
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
RR
1492 virtual bool ItemChanged( const wxDataViewItem &item );
1493 virtual bool ValueChanged( const wxDataViewItem &item, unsigned int col );
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
e0062c04 1594bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem &item, unsigned int model_col )
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 );
63415a42 1604 if (column->GetModelColumn() == model_col)
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
673810ee 1627 m_internal->ValueChanged( item, model_col );
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;
9861f022 1734
b74399b9
VZ
1735 // we haven't changed them yet
1736 m_usingDefaultAttrs = true;
1737
9861f022
RR
1738 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1739 // after the m_renderer pointer has been initialized
1740}
1741
205bdf20
VZ
1742void wxDataViewRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
1743{
1744 gtk_tree_view_column_pack_end( column, m_renderer, TRUE /* expand */);
1745}
1746
a912e81f
RR
1747void wxDataViewRenderer::GtkInitHandlers()
1748{
1749 if (!gtk_check_version(2,6,0))
1750 {
1751 g_signal_connect (GTK_CELL_RENDERER(m_renderer), "editing_started",
03647350
VZ
1752 G_CALLBACK (wxgtk_renderer_editing_started),
1753 this);
a912e81f
RR
1754 }
1755}
1756
9861f022
RR
1757void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
1758{
1759 GtkCellRendererMode gtkMode;
1760 switch (mode)
1761 {
25bc5c55
VZ
1762 case wxDATAVIEW_CELL_INERT:
1763 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
1764 break;
1765
1766 case wxDATAVIEW_CELL_ACTIVATABLE:
1767 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
1768 break;
1769
1770 case wxDATAVIEW_CELL_EDITABLE:
1771 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
1772 break;
1773
1774 default:
1775 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1776 return;
9861f022
RR
1777 }
1778
b9db5f30 1779 // This value is most often ignored in GtkTreeView
9861f022
RR
1780 GValue gvalue = { 0, };
1781 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1782 g_value_set_enum( &gvalue, gtkMode );
1783 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1784 g_value_unset( &gvalue );
1785}
1786
1787wxDataViewCellMode wxDataViewRenderer::GetMode() const
1788{
1789 wxDataViewCellMode ret;
1790
1791 GValue gvalue;
1792 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1793
1794 switch (g_value_get_enum(&gvalue))
1795 {
25bc5c55
VZ
1796 default:
1797 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1798 // fall through (we have to return something)
1799
1800 case GTK_CELL_RENDERER_MODE_INERT:
1801 ret = wxDATAVIEW_CELL_INERT;
1802 break;
1803
1804 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1805 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1806 break;
1807
1808 case GTK_CELL_RENDERER_MODE_EDITABLE:
1809 ret = wxDATAVIEW_CELL_EDITABLE;
1810 break;
9861f022
RR
1811 }
1812
1813 g_value_unset( &gvalue );
1814
1815 return ret;
1816}
1817
f2b7492a 1818void wxDataViewRenderer::GtkUpdateAlignment()
9861f022 1819{
f2b7492a 1820 int align = m_alignment;
bc0289bf 1821
f2b7492a
RR
1822 // query alignment from column ?
1823 if (align == -1)
1824 {
1825 // None there yet
1826 if (GetOwner() == NULL)
1827 return;
bc0289bf 1828
f2b7492a
RR
1829 align = GetOwner()->GetAlignment();
1830 align |= wxALIGN_CENTRE_VERTICAL;
1831 }
bc0289bf 1832
9861f022
RR
1833 // horizontal alignment:
1834
1835 gfloat xalign = 0.0;
1836 if (align & wxALIGN_RIGHT)
1837 xalign = 1.0;
1838 else if (align & wxALIGN_CENTER_HORIZONTAL)
1839 xalign = 0.5;
1840
1841 GValue gvalue = { 0, };
1842 g_value_init( &gvalue, G_TYPE_FLOAT );
1843 g_value_set_float( &gvalue, xalign );
1844 g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue );
1845 g_value_unset( &gvalue );
1846
1847 // vertical alignment:
1848
1849 gfloat yalign = 0.0;
1850 if (align & wxALIGN_BOTTOM)
1851 yalign = 1.0;
1852 else if (align & wxALIGN_CENTER_VERTICAL)
1853 yalign = 0.5;
1854
1855 GValue gvalue2 = { 0, };
1856 g_value_init( &gvalue2, G_TYPE_FLOAT );
1857 g_value_set_float( &gvalue2, yalign );
1858 g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 );
1859 g_value_unset( &gvalue2 );
6842a71a
RR
1860}
1861
f2b7492a 1862void wxDataViewRenderer::SetAlignment( int align )
9861f022 1863{
f2b7492a
RR
1864 m_alignment = align;
1865 GtkUpdateAlignment();
9861f022
RR
1866}
1867
f2b7492a
RR
1868int wxDataViewRenderer::GetAlignment() const
1869{
1870 return m_alignment;
1871}
9861f022 1872
c937bcac
VZ
1873void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode)
1874{
995d5aa2 1875#ifdef __WXGTK26__
c937bcac
VZ
1876 if ( gtk_check_version(2, 6, 0) != NULL )
1877 return;
1878
17cbc244
VZ
1879 GtkCellRendererText * const rend = GtkGetTextRenderer();
1880 if ( !rend )
1881 return;
1882
c937bcac
VZ
1883 // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we
1884 // can just cast between them
1885 GValue gvalue = { 0, };
1886 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
1887 g_value_set_enum( &gvalue, static_cast<PangoEllipsizeMode>(mode) );
17cbc244 1888 g_object_set_property( G_OBJECT(rend), "ellipsize", &gvalue );
c937bcac 1889 g_value_unset( &gvalue );
995d5aa2
VZ
1890#else // GTK < 2.6
1891 wxUnusedVar(mode);
1892#endif // GTK 2.6/before
c937bcac
VZ
1893}
1894
1895wxEllipsizeMode wxDataViewRenderer::GetEllipsizeMode() const
1896{
995d5aa2 1897#ifdef __WXGTK26__
c937bcac
VZ
1898 if ( gtk_check_version(2, 6, 0) != NULL )
1899 return wxELLIPSIZE_NONE;
1900
17cbc244
VZ
1901 GtkCellRendererText * const rend = GtkGetTextRenderer();
1902 if ( !rend )
1903 return wxELLIPSIZE_NONE;
1904
c937bcac
VZ
1905 GValue gvalue = { 0, };
1906 g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE );
17cbc244 1907 g_object_get_property( G_OBJECT(rend), "ellipsize", &gvalue );
c937bcac
VZ
1908 wxEllipsizeMode
1909 mode = static_cast<wxEllipsizeMode>(g_value_get_enum( &gvalue ));
1910 g_value_unset( &gvalue );
1911
1912 return mode;
995d5aa2
VZ
1913#else // GTK < 2.6
1914 return wxELLIPSIZE_NONE;
1915#endif // GTK 2.6/before
c937bcac
VZ
1916}
1917
205bdf20
VZ
1918void
1919wxDataViewRenderer::GtkOnTextEdited(const gchar *itempath, const wxString& str)
1920{
1921 wxVariant value(str);
1922 if (!Validate( value ))
1923 return;
1924
0c2a7270
VZ
1925 wxDataViewItem
1926 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
205bdf20
VZ
1927
1928 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
1929}
1930
1931void
1932wxDataViewRenderer::GtkOnCellChanged(const wxVariant& value,
1933 const wxDataViewItem& item,
1934 unsigned col)
1935{
1936 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
795dac4c 1937 model->ChangeValue( value, item, col );
205bdf20
VZ
1938}
1939
93763ad5 1940// ---------------------------------------------------------
baa9ebc4 1941// wxDataViewTextRenderer
93763ad5 1942// ---------------------------------------------------------
6842a71a 1943
205bdf20
VZ
1944extern "C"
1945{
a7f61f76 1946
c5c5395b 1947static void wxGtkTextRendererEditedCallback( GtkCellRendererText *WXUNUSED(renderer),
a7f61f76
RR
1948 gchar *arg1, gchar *arg2, gpointer user_data )
1949{
7448d67c 1950 wxDataViewRenderer *cell = (wxDataViewRenderer*) user_data;
93763ad5 1951
205bdf20
VZ
1952 cell->GtkOnTextEdited(arg1, wxGTK_CONV_BACK_FONT(
1953 arg2, cell->GetOwner()->GetOwner()->GetFont()));
1954}
93763ad5 1955
a7f61f76
RR
1956}
1957
c80cde00
VZ
1958namespace
1959{
1960
1961// helper function used by wxDataViewTextRenderer and
1962// wxDataViewCustomRenderer::RenderText(): it applies the attributes to the
1963// given text renderer and returns true if anything was done
1964bool GtkApplyAttr(GtkCellRendererText *renderer, const wxDataViewItemAttr& attr)
1965{
1966 bool usingDefaultAttrs = true;
1967 if (attr.HasColour())
1968 {
1969 const GdkColor * const gcol = attr.GetColour().GetColor();
1970
1971 GValue gvalue = { 0, };
1972 g_value_init( &gvalue, GDK_TYPE_COLOR );
1973 g_value_set_boxed( &gvalue, gcol );
1974 g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
1975 g_value_unset( &gvalue );
1976
1977 usingDefaultAttrs = false;
1978 }
1979 else
1980 {
1981 GValue gvalue = { 0, };
1982 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1983 g_value_set_boolean( &gvalue, FALSE );
1984 g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
1985 g_value_unset( &gvalue );
1986 }
1987
1988 if (attr.GetItalic())
1989 {
1990 GValue gvalue = { 0, };
1991 g_value_init( &gvalue, PANGO_TYPE_STYLE );
1992 g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
1993 g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
1994 g_value_unset( &gvalue );
1995
1996 usingDefaultAttrs = false;
1997 }
1998 else
1999 {
2000 GValue gvalue = { 0, };
2001 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2002 g_value_set_boolean( &gvalue, FALSE );
2003 g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
2004 g_value_unset( &gvalue );
2005 }
2006
2007
2008 if (attr.GetBold())
2009 {
2010 GValue gvalue = { 0, };
2011 g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
2012 g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
2013 g_object_set_property( G_OBJECT(renderer), "weight", &gvalue );
2014 g_value_unset( &gvalue );
2015
2016 usingDefaultAttrs = false;
2017 }
2018 else
2019 {
2020 GValue gvalue = { 0, };
2021 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2022 g_value_set_boolean( &gvalue, FALSE );
2023 g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue );
2024 g_value_unset( &gvalue );
2025 }
2026
2027#if 0
2028 if (attr.HasBackgroundColour())
2029 {
2030 wxColour colour = attr.GetBackgroundColour();
2031 const GdkColor * const gcol = colour.GetColor();
2032
2033 GValue gvalue = { 0, };
2034 g_value_init( &gvalue, GDK_TYPE_COLOR );
2035 g_value_set_boxed( &gvalue, gcol );
2036 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
2037 g_value_unset( &gvalue );
2038 }
2039 else
2040 {
2041 GValue gvalue = { 0, };
2042 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2043 g_value_set_boolean( &gvalue, FALSE );
2044 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
2045 g_value_unset( &gvalue );
2046 }
2047#endif
2048
2049 return !usingDefaultAttrs;
2050}
2051
2052} // anonymous namespace
2053
baa9ebc4 2054IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
6842a71a 2055
9861f022
RR
2056wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2057 int align ) :
2058 wxDataViewRenderer( varianttype, mode, align )
6842a71a 2059{
ecc32226
RR
2060 GtkWxCellRendererText *text_renderer = gtk_wx_cell_renderer_text_new();
2061 text_renderer->wx_renderer = this;
2062 m_renderer = (GtkCellRenderer*) text_renderer;
93763ad5 2063
9861f022 2064 if (mode & wxDATAVIEW_CELL_EDITABLE)
a7f61f76
RR
2065 {
2066 GValue gvalue = { 0, };
2067 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2068 g_value_set_boolean( &gvalue, true );
2069 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
2070 g_value_unset( &gvalue );
93763ad5 2071
a7f61f76 2072 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
b9db5f30 2073
a912e81f 2074 GtkInitHandlers();
a7f61f76 2075 }
9861f022
RR
2076
2077 SetMode(mode);
2078 SetAlignment(align);
6842a71a 2079}
790b137e 2080
205bdf20 2081bool wxDataViewTextRenderer::SetTextValue(const wxString& str)
7b4fde82 2082{
7b4fde82
RR
2083 GValue gvalue = { 0, };
2084 g_value_init( &gvalue, G_TYPE_STRING );
205bdf20 2085 g_value_set_string( &gvalue, wxGTK_CONV_FONT( str, GetOwner()->GetOwner()->GetFont() ) );
7b4fde82
RR
2086 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2087 g_value_unset( &gvalue );
93763ad5 2088
7b4fde82
RR
2089 return true;
2090}
2091
205bdf20 2092bool wxDataViewTextRenderer::GetTextValue(wxString& str) const
a7f61f76
RR
2093{
2094 GValue gvalue = { 0, };
2095 g_value_init( &gvalue, G_TYPE_STRING );
2096 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
205bdf20 2097 str = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), const_cast<wxDataViewTextRenderer*>(this)->GetOwner()->GetOwner()->GetFont() );
a7f61f76 2098 g_value_unset( &gvalue );
93763ad5 2099
a7f61f76
RR
2100 return true;
2101}
2102
9861f022
RR
2103void wxDataViewTextRenderer::SetAlignment( int align )
2104{
2105 wxDataViewRenderer::SetAlignment(align);
2106
01705e98
RR
2107 if (gtk_check_version(2,10,0))
2108 return;
9861f022 2109
01705e98 2110 // horizontal alignment:
9861f022
RR
2111 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2112 if (align & wxALIGN_RIGHT)
2113 pangoAlign = PANGO_ALIGN_RIGHT;
2114 else if (align & wxALIGN_CENTER_HORIZONTAL)
2115 pangoAlign = PANGO_ALIGN_CENTER;
2116
2117 GValue gvalue = { 0, };
2118 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2119 g_value_set_enum( &gvalue, pangoAlign );
2120 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2121 g_value_unset( &gvalue );
2122}
2123
c80cde00
VZ
2124bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr& attr)
2125{
17cbc244
VZ
2126 return GtkApplyAttr(GtkGetTextRenderer(), attr);
2127}
2128
2129GtkCellRendererText *wxDataViewTextRenderer::GtkGetTextRenderer() const
2130{
2131 return GTK_CELL_RENDERER_TEXT(m_renderer);
c80cde00
VZ
2132}
2133
f4322df6 2134// ---------------------------------------------------------
baa9ebc4 2135// wxDataViewBitmapRenderer
f4322df6 2136// ---------------------------------------------------------
cbc9145c 2137
205bdf20
VZ
2138namespace
2139{
2140
2141// set "pixbuf" property on the given renderer
2142void SetPixbufProp(GtkCellRenderer *renderer, GdkPixbuf *pixbuf)
2143{
2144 GValue gvalue = { 0, };
2145 g_value_init( &gvalue, G_TYPE_OBJECT );
2146 g_value_set_object( &gvalue, pixbuf );
2147 g_object_set_property( G_OBJECT(renderer), "pixbuf", &gvalue );
2148 g_value_unset( &gvalue );
2149}
2150
2151} // anonymous namespace
2152
baa9ebc4 2153IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
cbc9145c 2154
9861f022
RR
2155wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
2156 int align ) :
2157 wxDataViewRenderer( varianttype, mode, align )
cbc9145c 2158{
205bdf20 2159 m_renderer = gtk_cell_renderer_pixbuf_new();
9861f022
RR
2160
2161 SetMode(mode);
2162 SetAlignment(align);
cbc9145c
RR
2163}
2164
baa9ebc4 2165bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
cbc9145c
RR
2166{
2167 if (value.GetType() == wxT("wxBitmap"))
2168 {
2586d4a1
RR
2169 wxBitmap bitmap;
2170 bitmap << value;
f4322df6 2171
205bdf20
VZ
2172 // GetPixbuf() may create a Pixbuf representation in the wxBitmap
2173 // object (and it will stay there and remain owned by wxBitmap)
2174 SetPixbufProp(m_renderer, bitmap.GetPixbuf());
2586d4a1 2175 }
205bdf20 2176 else if (value.GetType() == wxT("wxIcon"))
2586d4a1 2177 {
205bdf20
VZ
2178 wxIcon icon;
2179 icon << value;
f4322df6 2180
205bdf20
VZ
2181 SetPixbufProp(m_renderer, icon.GetPixbuf());
2182 }
2183 else
2184 {
2185 return false;
cbc9145c 2186 }
f4322df6 2187
205bdf20 2188 return true;
cbc9145c
RR
2189}
2190
c5c5395b 2191bool wxDataViewBitmapRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
cbc9145c
RR
2192{
2193 return false;
2194}
f4322df6 2195
93763ad5 2196// ---------------------------------------------------------
baa9ebc4 2197// wxDataViewToggleRenderer
93763ad5 2198// ---------------------------------------------------------
fa28826d 2199
605c2c4a 2200extern "C" {
93763ad5 2201static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
2202 gchar *path, gpointer user_data );
2203}
2204
93763ad5 2205static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
2206 gchar *path, gpointer user_data )
2207{
baa9ebc4 2208 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
605c2c4a 2209
93763ad5 2210 // get old value
605c2c4a
RR
2211 GValue gvalue = { 0, };
2212 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2213 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
93763ad5 2214 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a
RR
2215 g_value_unset( &gvalue );
2216 // invert it
2217 tmp = !tmp;
93763ad5 2218
605c2c4a
RR
2219 wxVariant value = tmp;
2220 if (!cell->Validate( value ))
2221 return;
93763ad5 2222
0c2a7270
VZ
2223 wxDataViewCtrl * const ctrl = cell->GetOwner()->GetOwner();
2224 wxDataViewModel *model = ctrl->GetModel();
93763ad5 2225
0c2a7270 2226 wxDataViewItem item(ctrl->GTKPathToItem(wxGtkTreePath(path)));
93763ad5 2227
0a71f9e9 2228 unsigned int model_col = cell->GetOwner()->GetModelColumn();
93763ad5 2229
795dac4c 2230 model->ChangeValue( value, item, model_col );
605c2c4a
RR
2231}
2232
baa9ebc4 2233IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
605c2c4a 2234
baa9ebc4 2235wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
9861f022
RR
2236 wxDataViewCellMode mode, int align ) :
2237 wxDataViewRenderer( varianttype, mode, align )
605c2c4a 2238{
ed38aa55 2239 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
93763ad5 2240
9861f022 2241 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
605c2c4a 2242 {
9861f022
RR
2243 g_signal_connect_after( m_renderer, "toggled",
2244 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
553f7d8f
RR
2245 }
2246 else
2247 {
605c2c4a
RR
2248 GValue gvalue = { 0, };
2249 g_value_init( &gvalue, G_TYPE_BOOLEAN );
553f7d8f 2250 g_value_set_boolean( &gvalue, false );
605c2c4a
RR
2251 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
2252 g_value_unset( &gvalue );
605c2c4a 2253 }
9861f022
RR
2254
2255 SetMode(mode);
2256 SetAlignment(align);
605c2c4a
RR
2257}
2258
baa9ebc4 2259bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
605c2c4a
RR
2260{
2261 bool tmp = value;
93763ad5 2262
605c2c4a
RR
2263 GValue gvalue = { 0, };
2264 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2265 g_value_set_boolean( &gvalue, tmp );
2266 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
2267 g_value_unset( &gvalue );
93763ad5 2268
605c2c4a
RR
2269 return true;
2270}
2271
9861f022 2272bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
605c2c4a
RR
2273{
2274 GValue gvalue = { 0, };
2275 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2276 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
93763ad5 2277 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a 2278 g_value_unset( &gvalue );
93763ad5 2279
605c2c4a
RR
2280 value = tmp;
2281
2282 return true;
2283}
93763ad5
WS
2284
2285// ---------------------------------------------------------
baa9ebc4 2286// wxDataViewCustomRenderer
93763ad5 2287// ---------------------------------------------------------
e152afc3 2288
888dde65 2289class wxDataViewCtrlDCImpl: public wxWindowDCImpl
e152afc3
RR
2290{
2291public:
888dde65
RR
2292 wxDataViewCtrlDCImpl( wxDC *owner, wxDataViewCtrl *window ) :
2293 wxWindowDCImpl( owner )
2294 {
1a367564 2295 GtkWidget *widget = window->m_treeview;
e152afc3 2296 // Set later
888dde65 2297 m_gdkwindow = NULL;
7857346a 2298
888dde65 2299 m_window = window;
4d496ecb 2300
496e7ec6 2301 m_context = window->GTKGetPangoDefaultContext();
e152afc3
RR
2302 m_layout = pango_layout_new( m_context );
2303 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
2304
2305 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
888dde65
RR
2306
2307 // Set m_gdkwindow later
4d496ecb 2308 // SetUpDC();
e152afc3
RR
2309 }
2310};
2311
888dde65
RR
2312class wxDataViewCtrlDC: public wxWindowDC
2313{
2314public:
c5c5395b
RR
2315 wxDataViewCtrlDC( wxDataViewCtrl *window ) :
2316 wxWindowDC( new wxDataViewCtrlDCImpl( this, window ) )
7857346a 2317 { }
888dde65 2318};
7857346a 2319
888dde65 2320
93763ad5 2321// ---------------------------------------------------------
baa9ebc4 2322// wxDataViewCustomRenderer
93763ad5 2323// ---------------------------------------------------------
e152afc3 2324
baa9ebc4 2325IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
e152afc3 2326
baa9ebc4 2327wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
6eec70b9
VZ
2328 wxDataViewCellMode mode,
2329 int align,
2330 bool no_init )
2331 : wxDataViewCustomRendererBase( varianttype, mode, align )
e152afc3
RR
2332{
2333 m_dc = NULL;
f69c03de 2334 m_text_renderer = NULL;
93763ad5 2335
ad63bf41
RR
2336 if (no_init)
2337 m_renderer = NULL;
2338 else
9861f022 2339 Init(mode, align);
ad63bf41
RR
2340}
2341
17cbc244
VZ
2342GtkCellRendererText *wxDataViewCustomRenderer::GtkGetTextRenderer() const
2343{
2344 if ( !m_text_renderer )
2345 {
2346 // we create it on demand so need to do it even from a const function
2347 const_cast<wxDataViewCustomRenderer *>(this)->
2348 m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
2349 }
2350
2351 return m_text_renderer;
2352}
2353
2a454ffd
VZ
2354void wxDataViewCustomRenderer::RenderText( const wxString &text,
2355 int xoffset,
2356 wxRect cell,
2357 wxDC *WXUNUSED(dc),
2358 int WXUNUSED(state) )
52e750fc 2359{
17cbc244
VZ
2360
2361 GtkCellRendererText * const textRenderer = GtkGetTextRenderer();
b9db5f30 2362
f69c03de
RR
2363 GValue gvalue = { 0, };
2364 g_value_init( &gvalue, G_TYPE_STRING );
1bf629bd 2365 g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) );
17cbc244 2366 g_object_set_property( G_OBJECT(textRenderer), "text", &gvalue );
f69c03de
RR
2367 g_value_unset( &gvalue );
2368
17cbc244 2369 GtkApplyAttr(textRenderer, GetAttr());
c80cde00 2370
2a454ffd
VZ
2371 GdkRectangle cell_area;
2372 wxRectToGDKRect(cell, cell_area);
2373 cell_area.x += xoffset;
2374 cell_area.width -= xoffset;
b9db5f30 2375
17cbc244 2376 gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer),
2a454ffd
VZ
2377 m_renderParams.window,
2378 m_renderParams.widget,
2379 m_renderParams.background_area,
2380 &cell_area,
2381 m_renderParams.expose_area,
2382 (GtkCellRendererState) m_renderParams.flags );
52e750fc
RR
2383}
2384
9861f022 2385bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
ad63bf41 2386{
e152afc3
RR
2387 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
2388 renderer->cell = this;
93763ad5 2389
ed38aa55 2390 m_renderer = (GtkCellRenderer*) renderer;
93763ad5 2391
9861f022
RR
2392 SetMode(mode);
2393 SetAlignment(align);
93763ad5 2394
a912e81f 2395 GtkInitHandlers();
b9db5f30 2396
ad63bf41 2397 return true;
e152afc3
RR
2398}
2399
baa9ebc4 2400wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
e152afc3
RR
2401{
2402 if (m_dc)
2403 delete m_dc;
f69c03de
RR
2404
2405 if (m_text_renderer)
352cd79f 2406 gtk_object_sink( GTK_OBJECT(m_text_renderer) );
e152afc3
RR
2407}
2408
baa9ebc4 2409wxDC *wxDataViewCustomRenderer::GetDC()
e152afc3
RR
2410{
2411 if (m_dc == NULL)
4d496ecb
RR
2412 {
2413 if (GetOwner() == NULL)
2414 return NULL;
2415 if (GetOwner()->GetOwner() == NULL)
2416 return NULL;
e152afc3 2417 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
4d496ecb 2418 }
93763ad5 2419
e152afc3
RR
2420 return m_dc;
2421}
93763ad5
WS
2422
2423// ---------------------------------------------------------
baa9ebc4 2424// wxDataViewProgressRenderer
93763ad5 2425// ---------------------------------------------------------
ad63bf41 2426
baa9ebc4 2427IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
ad63bf41 2428
baa9ebc4 2429wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
9861f022
RR
2430 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
2431 wxDataViewCustomRenderer( varianttype, mode, align, true )
ad63bf41
RR
2432{
2433 m_label = label;
2434 m_value = 0;
93763ad5 2435
ad63bf41
RR
2436#ifdef __WXGTK26__
2437 if (!gtk_check_version(2,6,0))
2438 {
ed38aa55 2439 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
93763ad5 2440
ad63bf41
RR
2441 GValue gvalue = { 0, };
2442 g_value_init( &gvalue, G_TYPE_STRING );
09dfa6a0 2443
1bf629bd 2444 g_value_set_string( &gvalue, wxGTK_CONV_FONT( m_label, GetOwner()->GetOwner()->GetFont() ) );
ad63bf41
RR
2445 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2446 g_value_unset( &gvalue );
9861f022
RR
2447
2448 SetMode(mode);
2449 SetAlignment(align);
ad63bf41
RR
2450 }
2451 else
2452#endif
2453 {
2454 // Use custom cell code
9861f022 2455 wxDataViewCustomRenderer::Init(mode, align);
ad63bf41
RR
2456 }
2457}
2458
baa9ebc4 2459wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
ad63bf41
RR
2460{
2461}
2462
baa9ebc4 2463bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
ad63bf41
RR
2464{
2465#ifdef __WXGTK26__
2466 if (!gtk_check_version(2,6,0))
2467 {
7226118b 2468 gint tmp = (long) value;
ad63bf41
RR
2469 GValue gvalue = { 0, };
2470 g_value_init( &gvalue, G_TYPE_INT );
7226118b 2471 g_value_set_int( &gvalue, tmp );
ad63bf41
RR
2472 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
2473 g_value_unset( &gvalue );
2474 }
2475 else
2476#endif
2477 {
2478 m_value = (long) value;
93763ad5 2479
ad63bf41
RR
2480 if (m_value < 0) m_value = 0;
2481 if (m_value > 100) m_value = 100;
2482 }
93763ad5 2483
ad63bf41
RR
2484 return true;
2485}
93763ad5 2486
85136e3b 2487bool wxDataViewProgressRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
9861f022
RR
2488{
2489 return false;
2490}
2491
85136e3b 2492bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
ad63bf41
RR
2493{
2494 double pct = (double)m_value / 100.0;
2495 wxRect bar = cell;
2496 bar.width = (int)(cell.width * pct);
2497 dc->SetPen( *wxTRANSPARENT_PEN );
2498 dc->SetBrush( *wxBLUE_BRUSH );
2499 dc->DrawRectangle( bar );
2500
2501 dc->SetBrush( *wxTRANSPARENT_BRUSH );
2502 dc->SetPen( *wxBLACK_PEN );
2503 dc->DrawRectangle( cell );
93763ad5 2504
ad63bf41
RR
2505 return true;
2506}
2507
9861f022 2508wxSize wxDataViewProgressRenderer::GetSize() const
ad63bf41
RR
2509{
2510 return wxSize(40,12);
2511}
93763ad5 2512
7448d67c
RR
2513// -------------------------------------
2514// wxDataViewChoiceRenderer
2515// -------------------------------------
2516
2517wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString &choices,
2518 wxDataViewCellMode mode, int alignment ) :
2519 wxDataViewCustomRenderer( "string", mode, alignment, true )
2520{
2521 m_choices = choices;
2522
2523#ifdef __WXGTK26__
2524 if (!gtk_check_version(2,6,0))
2525 {
2526 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_combo_new();
2527
2528 GtkListStore *store = gtk_list_store_new( 1, G_TYPE_STRING );
5baee1f4
VZ
2529 for (size_t n = 0; n < m_choices.GetCount(); n++)
2530 {
2531 gtk_list_store_insert_with_values(
2532 store, NULL, n, 0,
2533 static_cast<const char *>(m_choices[n].utf8_str()), -1 );
2534 }
bc0289bf 2535
7448d67c
RR
2536 g_object_set (m_renderer,
2537 "model", store,
2538 "text-column", 0,
2539 "has-entry", FALSE,
2540 NULL);
bc0289bf 2541
7448d67c
RR
2542 bool editable = (mode & wxDATAVIEW_CELL_EDITABLE);
2543 g_object_set (m_renderer, "editable", editable, NULL);
bc0289bf 2544
7448d67c 2545 SetAlignment(alignment);
bc0289bf 2546
7448d67c
RR
2547 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
2548
2549 GtkInitHandlers();
2550 }
2551 else
2552#endif
2553 {
2554 // Use custom cell code
2555 wxDataViewCustomRenderer::Init(mode, alignment);
2556 }
2557}
2558
2559bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
2560{
2561 RenderText( m_data, 0, rect, dc, state );
2562 return true;
2563}
2564
2565wxSize wxDataViewChoiceRenderer::GetSize() const
2566{
2567 return wxSize(70,20);
2568}
2569
2570bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
2571{
bc0289bf 2572
7448d67c
RR
2573#ifdef __WXGTK26__
2574 if (!gtk_check_version(2,6,0))
2575 {
2576 GValue gvalue = { 0, };
2577 g_value_init( &gvalue, G_TYPE_STRING );
d636e043
VZ
2578 g_value_set_string(&gvalue,
2579 wxGTK_CONV_FONT(value.GetString(),
2580 GetOwner()->GetOwner()->GetFont()));
7448d67c
RR
2581 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
2582 g_value_unset( &gvalue );
2583 }
2584 else
2585#endif
2586 m_data = value.GetString();
2587
2588 return true;
2589}
2590
2591bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
2592{
2593#ifdef __WXGTK26__
2594 if (!gtk_check_version(2,6,0))
2595 {
2596 GValue gvalue = { 0, };
2597 g_value_init( &gvalue, G_TYPE_STRING );
2598 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
d636e043
VZ
2599 wxString temp = wxGTK_CONV_BACK_FONT(g_value_get_string(&gvalue),
2600 GetOwner()->GetOwner()->GetFont());
7448d67c
RR
2601 g_value_unset( &gvalue );
2602 value = temp;
c2489d8e
FM
2603
2604 //wxPrintf( "temp %s\n", temp );
2605 // TODO: remove this code
7448d67c
RR
2606 }
2607 else
2608#endif
2609 value = m_data;
2610
2611 return true;
2612}
2613
2614void wxDataViewChoiceRenderer::SetAlignment( int align )
2615{
2616 wxDataViewCustomRenderer::SetAlignment(align);
2617
2618 if (gtk_check_version(2,10,0))
2619 return;
2620
2621 // horizontal alignment:
2622 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
2623 if (align & wxALIGN_RIGHT)
2624 pangoAlign = PANGO_ALIGN_RIGHT;
2625 else if (align & wxALIGN_CENTER_HORIZONTAL)
2626 pangoAlign = PANGO_ALIGN_CENTER;
2627
2628 GValue gvalue = { 0, };
2629 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
2630 g_value_set_enum( &gvalue, pangoAlign );
2631 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
2632 g_value_unset( &gvalue );
2633}
bc0289bf 2634
65887bd0
RR
2635// ----------------------------------------------------------------------------
2636// wxDataViewChoiceByIndexRenderer
2637// ----------------------------------------------------------------------------
2638
2639wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
2640 wxDataViewCellMode mode, int alignment ) :
2641 wxDataViewChoiceRenderer( choices, mode, alignment )
2642{
2643}
ce00f59b 2644
65887bd0
RR
2645void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const gchar *itempath, const wxString& str)
2646{
2647 wxVariant value( (long) GetChoices().Index( str ) );
2648
2649 if (!Validate( value ))
2650 return;
2651
0c2a7270
VZ
2652 wxDataViewItem
2653 item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
65887bd0
RR
2654
2655 GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
2656}
2657
2658bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
2659{
2660 wxVariant string_value = GetChoice( value.GetLong() );
2661 return wxDataViewChoiceRenderer::SetValue( string_value );
2662}
ce00f59b 2663
65887bd0
RR
2664bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const
2665{
2666 wxVariant string_value;
2667 if (!wxDataViewChoiceRenderer::GetValue( string_value ))
2668 return false;
ce00f59b 2669
65887bd0
RR
2670 value = (long) GetChoices().Index( string_value.GetString() );
2671 return true;
2672}
2673
93763ad5 2674// ---------------------------------------------------------
baa9ebc4 2675// wxDataViewDateRenderer
93763ad5 2676// ---------------------------------------------------------
4d496ecb 2677
baa9ebc4 2678class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
7ea3a0de 2679{
93763ad5 2680public:
baa9ebc4 2681 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
e0062c04 2682 wxDataViewModel *model, const wxDataViewItem &item, unsigned int col ) :
7ea3a0de
RR
2683 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
2684 {
2685 m_model = model;
e0062c04 2686 m_item = item;
7ea3a0de 2687 m_col = col;
7ea3a0de
RR
2688 m_cal = new wxCalendarCtrl( this, -1, *value );
2689 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
2690 sizer->Add( m_cal, 1, wxGROW );
2691 SetSizer( sizer );
2692 sizer->Fit( this );
2693 }
93763ad5 2694
7ea3a0de
RR
2695 virtual void OnDismiss()
2696 {
2697 }
93763ad5 2698
7ea3a0de 2699 void OnCalendar( wxCalendarEvent &event );
93763ad5 2700
e0062c04
RR
2701 wxCalendarCtrl *m_cal;
2702 wxDataViewModel *m_model;
2703 wxDataViewItem m_item;
2704 unsigned int m_col;
93763ad5 2705
7ea3a0de
RR
2706private:
2707 DECLARE_EVENT_TABLE()
2708};
2709
baa9ebc4
RR
2710BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
2711 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
7ea3a0de
RR
2712END_EVENT_TABLE()
2713
baa9ebc4 2714void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
7ea3a0de 2715{
795dac4c 2716 m_model->ChangeValue( event.GetDate(), m_item, m_col );
7ea3a0de
RR
2717 DismissAndNotify();
2718}
2719
baa9ebc4 2720IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
4d496ecb 2721
baa9ebc4 2722wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
9861f022
RR
2723 wxDataViewCellMode mode, int align ) :
2724 wxDataViewCustomRenderer( varianttype, mode, align )
4d496ecb 2725{
9861f022
RR
2726 SetMode(mode);
2727 SetAlignment(align);
4d496ecb 2728}
93763ad5 2729
baa9ebc4 2730bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
4d496ecb
RR
2731{
2732 m_date = value.GetDateTime();
93763ad5 2733
4d496ecb
RR
2734 return true;
2735}
2736
85136e3b 2737bool wxDataViewDateRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
9861f022
RR
2738{
2739 return false;
2740}
2741
baa9ebc4 2742bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
4d496ecb
RR
2743{
2744 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
2745 wxString tmp = m_date.FormatDate();
52e750fc 2746 RenderText( tmp, 0, cell, dc, state );
4d496ecb
RR
2747 return true;
2748}
2749
9861f022 2750wxSize wxDataViewDateRenderer::GetSize() const
4d496ecb 2751{
4d496ecb
RR
2752 wxString tmp = m_date.FormatDate();
2753 wxCoord x,y,d;
9861f022 2754 GetView()->GetTextExtent( tmp, &x, &y, &d );
4d496ecb
RR
2755 return wxSize(x,y+d);
2756}
2757
85136e3b 2758bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model,
e0062c04 2759 const wxDataViewItem &item, unsigned int col )
4d496ecb 2760{
3f3af7e7 2761 wxVariant variant;
e0062c04 2762 model->GetValue( variant, item, col );
7ea3a0de
RR
2763 wxDateTime value = variant.GetDateTime();
2764
baa9ebc4 2765 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
e0062c04 2766 GetOwner()->GetOwner()->GetParent(), &value, model, item, col );
7ea3a0de
RR
2767 wxPoint pos = wxGetMousePosition();
2768 popup->Move( pos );
2769 popup->Layout();
2770 popup->Popup( popup->m_cal );
4d496ecb
RR
2771
2772 return true;
2773}
2774
c9c13e70 2775
b9db5f30 2776// ---------------------------------------------------------
c9c13e70 2777// wxDataViewIconTextRenderer
b9db5f30 2778// ---------------------------------------------------------
c9c13e70
RR
2779
2780IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
2781
205bdf20
VZ
2782wxDataViewIconTextRenderer::wxDataViewIconTextRenderer
2783 (
2784 const wxString &varianttype,
2785 wxDataViewCellMode mode,
2786 int align
2787 )
2788 : wxDataViewTextRenderer(varianttype, mode, align)
c9c13e70 2789{
205bdf20 2790 m_rendererIcon = gtk_cell_renderer_pixbuf_new();
c9c13e70
RR
2791}
2792
2793wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2794{
2795}
b9db5f30 2796
205bdf20 2797void wxDataViewIconTextRenderer::GtkPackIntoColumn(GtkTreeViewColumn *column)
c9c13e70 2798{
205bdf20
VZ
2799 // add the icon renderer first
2800 gtk_tree_view_column_pack_start(column, m_rendererIcon, FALSE /* !expand */);
c9c13e70 2801
205bdf20
VZ
2802 // add the text renderer too
2803 wxDataViewRenderer::GtkPackIntoColumn(column);
c9c13e70 2804}
b9db5f30 2805
205bdf20 2806bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
c9c13e70 2807{
205bdf20 2808 m_value << value;
b9db5f30 2809
205bdf20
VZ
2810 SetTextValue(m_value.GetText());
2811 SetPixbufProp(m_rendererIcon, m_value.GetIcon().GetPixbuf());
c9c13e70
RR
2812
2813 return true;
2814}
2815
205bdf20 2816bool wxDataViewIconTextRenderer::GetValue(wxVariant& value) const
c9c13e70 2817{
205bdf20
VZ
2818 wxString str;
2819 if ( !GetTextValue(str) )
2820 return false;
c9c13e70 2821
205bdf20
VZ
2822 // user doesn't have any way to edit the icon so leave it unchanged
2823 value << wxDataViewIconText(str, m_value.GetIcon());
2824
2825 return true;
c9c13e70
RR
2826}
2827
205bdf20
VZ
2828void
2829wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant& value,
2830 const wxDataViewItem& item,
2831 unsigned col)
c9c13e70 2832{
205bdf20
VZ
2833 // we receive just the text part of our value as it's the only one which
2834 // can be edited, but we need the full wxDataViewIconText value for the
2835 // model
2836 wxVariant valueIconText;
2837 valueIconText << wxDataViewIconText(value.GetString(), m_value.GetIcon());
2838 wxDataViewTextRenderer::GtkOnCellChanged(valueIconText, item, col);
c9c13e70
RR
2839}
2840
93763ad5 2841// ---------------------------------------------------------
605c2c4a 2842// wxDataViewColumn
93763ad5 2843// ---------------------------------------------------------
7b4fde82 2844
31fb32e1
RR
2845
2846static gboolean
ad386793 2847gtk_dataview_header_button_press_callback( GtkWidget *WXUNUSED(widget),
31fb32e1
RR
2848 GdkEventButton *gdk_event,
2849 wxDataViewColumn *column )
2850{
2851 if (gdk_event->type != GDK_BUTTON_PRESS)
94b1f7bc 2852 return FALSE;
f4322df6 2853
31fb32e1
RR
2854 if (gdk_event->button == 1)
2855 {
d32332aa 2856 gs_lastLeftClickHeader = column;
7857346a 2857
31fb32e1
RR
2858 wxDataViewCtrl *dv = column->GetOwner();
2859 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
2860 event.SetDataViewColumn( column );
2861 event.SetModel( dv->GetModel() );
937013e0 2862 if (dv->HandleWindowEvent( event ))
a84c5b6f 2863 return FALSE;
31fb32e1 2864 }
f4322df6 2865
dadc879e
RR
2866 if (gdk_event->button == 3)
2867 {
2868 wxDataViewCtrl *dv = column->GetOwner();
2869 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, dv->GetId() );
2870 event.SetDataViewColumn( column );
2871 event.SetModel( dv->GetModel() );
937013e0 2872 if (dv->HandleWindowEvent( event ))
dadc879e
RR
2873 return FALSE;
2874 }
2875
94b1f7bc 2876 return FALSE;
31fb32e1
RR
2877}
2878
47583ac1
VZ
2879extern "C"
2880{
7b4fde82 2881
ad386793 2882static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *WXUNUSED(column),
7b4fde82
RR
2883 GtkCellRenderer *renderer,
2884 GtkTreeModel *model,
2885 GtkTreeIter *iter,
2886 gpointer data )
2887{
e0062c04
RR
2888 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
2889 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
93763ad5 2890
baa9ebc4 2891 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
a7f61f76 2892
9d52aad3 2893 wxDataViewItem item( (void*) iter->user_data );
93763ad5 2894
a826202e
RR
2895 wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel();
2896
e39de702 2897 if (!wx_model->IsVirtualListModel())
2056dede 2898 {
47583ac1
VZ
2899 gboolean visible;
2900 if (wx_model->IsContainer( item ))
a826202e 2901 {
47583ac1
VZ
2902 visible = wx_model->HasContainerColumns( item ) ||
2903 (cell->GetOwner()->GetModelColumn() == 0);
a826202e
RR
2904 }
2905 else
2906 {
47583ac1 2907 visible = true;
a826202e 2908 }
47583ac1 2909
a826202e
RR
2910 GValue gvalue = { 0, };
2911 g_value_init( &gvalue, G_TYPE_BOOLEAN );
47583ac1 2912 g_value_set_boolean( &gvalue, visible );
a826202e
RR
2913 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2914 g_value_unset( &gvalue );
b9db5f30 2915
47583ac1
VZ
2916 if ( !visible )
2917 return;
2056dede 2918 }
a826202e 2919
3f3af7e7 2920 wxVariant value;
a826202e 2921 wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
7b4fde82
RR
2922
2923 if (value.GetType() != cell->GetVariantType())
af588446 2924 {
f4322df6
VZ
2925 wxLogError( wxT("Wrong type, required: %s but: %s"),
2926 value.GetType().c_str(),
cbc9145c 2927 cell->GetVariantType().c_str() );
af588446 2928 }
93763ad5 2929
7b4fde82 2930 cell->SetValue( value );
e0743e63 2931
b74399b9 2932
c80cde00
VZ
2933 // deal with attributes: if the renderer doesn't support them at all, we
2934 // don't even need to query the model for them
c2a738e3
VZ
2935 if ( !cell->GtkSupportsAttrs() )
2936 return;
2937
c80cde00 2938 // it can support attributes so check if this item has any
2d0d7813 2939 wxDataViewItemAttr attr;
c80cde00
VZ
2940 if ( wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr )
2941 || !cell->GtkIsUsingDefaultAttrs() )
b74399b9 2942 {
c80cde00
VZ
2943 bool usingDefaultAttrs = !cell->GtkSetAttr(attr);
2944 cell->GtkSetUsingDefaultAttrs(usingDefaultAttrs);
b74399b9 2945 }
c80cde00
VZ
2946 // else: no custom attributes specified and we're already using the default
2947 // ones -- nothing to do
7b4fde82
RR
2948}
2949
47583ac1
VZ
2950} // extern "C"
2951
91a6c655 2952#include <wx/listimpl.cpp>
a76c2f37 2953WX_DEFINE_LIST(wxDataViewColumnList)
91a6c655 2954
f4322df6
VZ
2955wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
2956 unsigned int model_column, int width,
835d0d55
VZ
2957 wxAlignment align, int flags )
2958 : wxDataViewColumnBase( cell, model_column )
fa28826d 2959{
9861f022 2960 Init( align, flags, width );
31fb32e1 2961
31fb32e1 2962 SetTitle( title );
fa28826d
RR
2963}
2964
f4322df6
VZ
2965wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
2966 unsigned int model_column, int width,
835d0d55
VZ
2967 wxAlignment align, int flags )
2968 : wxDataViewColumnBase( bitmap, cell, model_column )
9861f022
RR
2969{
2970 Init( align, flags, width );
2971
2972 SetBitmap( bitmap );
2973}
2974
2975void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
07a84e7b 2976{
31fb32e1 2977 m_isConnected = false;
07a84e7b
RR
2978
2979 GtkTreeViewColumn *column = gtk_tree_view_column_new();
9861f022 2980 m_column = (GtkWidget*) column;
07a84e7b 2981
9861f022
RR
2982 SetFlags( flags );
2983 SetAlignment( align );
07a84e7b 2984
ad386793 2985 SetWidth( width );
07a84e7b 2986
419a3607
RR
2987 // Create container for icon and label
2988 GtkWidget *box = gtk_hbox_new( FALSE, 1 );
2989 gtk_widget_show( box );
2990 // gtk_container_set_border_width((GtkContainer*)box, 2);
2991 m_image = gtk_image_new();
2992 gtk_box_pack_start(GTK_BOX(box), m_image, FALSE, FALSE, 1);
2993 m_label = gtk_label_new("");
2994 gtk_box_pack_end( GTK_BOX(box), GTK_WIDGET(m_label), FALSE, FALSE, 1 );
2995 gtk_tree_view_column_set_widget( column, box );
bc0289bf 2996
205bdf20
VZ
2997 wxDataViewRenderer * const colRenderer = GetRenderer();
2998 GtkCellRenderer * const cellRenderer = colRenderer->GetGtkHandle();
2999
3000 colRenderer->GtkPackIntoColumn(column);
07a84e7b 3001
205bdf20
VZ
3002 gtk_tree_view_column_set_cell_data_func( column, cellRenderer,
3003 wxGtkTreeCellDataFunc, (gpointer) colRenderer, NULL );
07a84e7b
RR
3004}
3005
31fb32e1
RR
3006void wxDataViewColumn::OnInternalIdle()
3007{
3008 if (m_isConnected)
3009 return;
ce00f59b 3010
31fb32e1
RR
3011 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
3012 {
9861f022 3013 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
31fb32e1
RR
3014 if (column->button)
3015 {
3016 g_signal_connect(column->button, "button_press_event",
3017 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
f4322df6 3018
5d9e1605 3019 // otherwise the event will be blocked by GTK+
ce00f59b
VZ
3020 gtk_tree_view_column_set_clickable( column, TRUE );
3021
31fb32e1
RR
3022 m_isConnected = true;
3023 }
3024 }
3025}
3026
b94db696
RR
3027void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
3028{
3029 wxDataViewColumnBase::SetOwner( owner );
f4322df6 3030
9861f022 3031 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 3032
b94db696 3033 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
b94db696
RR
3034}
3035
fa28826d
RR
3036void wxDataViewColumn::SetTitle( const wxString &title )
3037{
09dfa6a0 3038 wxDataViewCtrl *ctrl = GetOwner();
419a3607 3039 gtk_label_set_text( GTK_LABEL(m_label), ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
09dfa6a0 3040 : wxGTK_CONV_SYS(title) );
419a3607
RR
3041 if (title.empty())
3042 gtk_widget_hide( m_label );
3043 else
3044 gtk_widget_show( m_label );
07a84e7b
RR
3045}
3046
9861f022
RR
3047wxString wxDataViewColumn::GetTitle() const
3048{
7f6cbcea
VZ
3049 return wxGTK_CONV_BACK_FONT(
3050 gtk_label_get_text( GTK_LABEL(m_label) ),
3051 GetOwner()->GetFont()
3052 );
9861f022
RR
3053}
3054
07a84e7b
RR
3055void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
3056{
3057 wxDataViewColumnBase::SetBitmap( bitmap );
3058
07a84e7b
RR
3059 if (bitmap.Ok())
3060 {
419a3607 3061 GtkImage *gtk_image = GTK_IMAGE(m_image);
f4322df6 3062
d3b9f782 3063 GdkBitmap *mask = NULL;
07a84e7b
RR
3064 if (bitmap.GetMask())
3065 mask = bitmap.GetMask()->GetBitmap();
3066
3067 if (bitmap.HasPixbuf())
3068 {
3069 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
3070 bitmap.GetPixbuf());
3071 }
3072 else
3073 {
3074 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
3075 bitmap.GetPixmap(), mask);
3076 }
419a3607 3077 gtk_widget_show( m_image );
07a84e7b
RR
3078 }
3079 else
3080 {
419a3607 3081 gtk_widget_hide( m_image );
07a84e7b 3082 }
fa28826d
RR
3083}
3084
9861f022
RR
3085void wxDataViewColumn::SetHidden( bool hidden )
3086{
3087 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
3088}
3089
3090void wxDataViewColumn::SetResizeable( bool resizeable )
3091{
3092 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
3093}
3094
47cef10f
RR
3095void wxDataViewColumn::SetAlignment( wxAlignment align )
3096{
9861f022 3097 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 3098
47cef10f
RR
3099 gfloat xalign = 0.0;
3100 if (align == wxALIGN_RIGHT)
3101 xalign = 1.0;
9861f022
RR
3102 if (align == wxALIGN_CENTER_HORIZONTAL ||
3103 align == wxALIGN_CENTER)
47cef10f 3104 xalign = 0.5;
f4322df6 3105
9861f022 3106 gtk_tree_view_column_set_alignment( column, xalign );
bc0289bf 3107
f2b7492a
RR
3108 if (m_renderer && m_renderer->GetAlignment() == -1)
3109 m_renderer->GtkUpdateAlignment();
9861f022
RR
3110}
3111
3112wxAlignment wxDataViewColumn::GetAlignment() const
3113{
3114 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
3115
3116 if (xalign == 1.0)
3117 return wxALIGN_RIGHT;
3118 if (xalign == 0.5)
3119 return wxALIGN_CENTER_HORIZONTAL;
f4322df6 3120
9861f022 3121 return wxALIGN_LEFT;
47cef10f
RR
3122}
3123
31fb32e1
RR
3124void wxDataViewColumn::SetSortable( bool sortable )
3125{
9861f022 3126 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
b9db5f30 3127
140a119a
VZ
3128 if ( sortable )
3129 {
3130 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
3131 }
3132 else
3133 {
3134 gtk_tree_view_column_set_sort_column_id( column, -1 );
3135 gtk_tree_view_column_set_sort_indicator( column, FALSE );
3136 gtk_tree_view_column_set_clickable( column, FALSE );
3137 }
e2bfe673
VZ
3138}
3139
3140bool wxDataViewColumn::IsSortable() const
3141{
3142 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3143 return gtk_tree_view_column_get_clickable( column );
3144}
3145
140a119a 3146void wxDataViewColumn::SetAsSortKey( bool WXUNUSED(sort) )
e2bfe673 3147{
dcb6cbec 3148 // it might not make sense to have this function in wxHeaderColumn at
140a119a
VZ
3149 // all in fact, changing of the sort order should only be done using the
3150 // associated control API
3151 wxFAIL_MSG( "not implemented" );
31fb32e1
RR
3152}
3153
e2bfe673 3154bool wxDataViewColumn::IsSortKey() const
31fb32e1 3155{
9861f022 3156 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
140a119a 3157 return gtk_tree_view_column_get_sort_indicator( column );
31fb32e1
RR
3158}
3159
9861f022
RR
3160bool wxDataViewColumn::IsResizeable() const
3161{
3162 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3163 return gtk_tree_view_column_get_resizable( column );
3164}
3165
3166bool wxDataViewColumn::IsHidden() const
3167{
3168 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
3169 return !gtk_tree_view_column_get_visible( column );
3170}
3171
47cef10f
RR
3172void wxDataViewColumn::SetSortOrder( bool ascending )
3173{
9861f022 3174 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 3175
47cef10f
RR
3176 if (ascending)
3177 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
3178 else
3179 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
0bd26819
RR
3180
3181 gtk_tree_view_column_set_sort_indicator( column, TRUE );
47cef10f
RR
3182}
3183
87f0efe2 3184bool wxDataViewColumn::IsSortOrderAscending() const
31fb32e1 3185{
9861f022 3186 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 3187
31fb32e1
RR
3188 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
3189}
3190
9861f022 3191void wxDataViewColumn::SetMinWidth( int width )
533544f2 3192{
9861f022 3193 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
533544f2
RR
3194}
3195
9861f022
RR
3196int wxDataViewColumn::GetMinWidth() const
3197{
3198 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
3199}
3200
3201int wxDataViewColumn::GetWidth() const
533544f2 3202{
9861f022 3203 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
533544f2
RR
3204}
3205
9861f022 3206void wxDataViewColumn::SetWidth( int width )
533544f2 3207{
ad386793
RR
3208 if (width < 0)
3209 {
973dc907
RR
3210#if 1
3211 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
3212
3213 // TODO find a better calculation
97e89e29 3214 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), wxDVC_DEFAULT_WIDTH );
973dc907
RR
3215#else
3216 // this is unpractical for large numbers of items and disables
3217 // user resizing, which is totally unexpected
ad386793 3218 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
973dc907 3219#endif
ad386793
RR
3220 }
3221 else
3222 {
3223 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
7857346a 3224
ad386793
RR
3225 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
3226 }
533544f2
RR
3227}
3228
99c75ebc
RR
3229void wxDataViewColumn::SetReorderable( bool reorderable )
3230{
3231 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
3232}
3233
3234bool wxDataViewColumn::IsReorderable() const
3235{
3236 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) );
3237}
9861f022 3238
4508fcd2
RR
3239//-----------------------------------------------------------------------------
3240// wxGtkTreeModelNode
3241//-----------------------------------------------------------------------------
3242
5b276ac1 3243#if 0
1f226ad8
RR
3244class wxGtkTreeModelChildWithPos
3245{
3246public:
3247 unsigned int pos;
3248 void *id;
3249};
3250
3251static
3252int wxGtkTreeModelChildWithPosCmp( const void* data1, const void* data2, const void* user_data )
3253{
3254 const wxGtkTreeModelChildWithPos* child1 = (const wxGtkTreeModelChildWithPos*) data1;
3255 const wxGtkTreeModelChildWithPos* child2 = (const wxGtkTreeModelChildWithPos*) data2;
3256 const wxDataViewCtrlInternal *internal = (const wxDataViewCtrlInternal *) user_data;
3257 int ret = internal->GetDataViewModel()->Compare( child1->id, child2->id,
5b276ac1 3258 internal->GetSortColumn(), (internal->GetSortOrder() == GTK_SORT_DESCENDING) );
1f226ad8
RR
3259
3260 return ret;
3261}
5b276ac1
RR
3262#else
3263static
3264int LINKAGEMODE wxGtkTreeModelChildPtrCmp( void*** data1, void*** data2 )
3265{
3266 return gs_internal->GetDataViewModel()->Compare( **data1, **data2,
3267 gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
3268}
3269
3270WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr );
3271#endif
1f226ad8 3272
4508fcd2
RR
3273void wxGtkTreeModelNode::Resort()
3274{
af110130
RR
3275 size_t child_count = GetChildCount();
3276 if (child_count == 0)
0be79c8a
RR
3277 return;
3278
af110130
RR
3279 size_t node_count = GetNodesCount();
3280
3281 if (child_count == 1)
0be79c8a 3282 {
af110130
RR
3283 if (node_count == 1)
3284 {
3285 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
3286 node->Resort();
3287 }
0be79c8a
RR
3288 return;
3289 }
3290
5b276ac1
RR
3291 gint *new_order = new gint[child_count];
3292
1f226ad8 3293#if 1
5b276ac1
RR
3294 // m_children has the original *void
3295 // ptrs points to these
3296 wxGtkTreeModelChildrenPtr ptrs;
3297 size_t i;
3298 for (i = 0; i < child_count; i++)
3299 ptrs.Add( &(m_children[i]) );
3300 // Sort the ptrs
3301 gs_internal = m_internal;
3302 ptrs.Sort( &wxGtkTreeModelChildPtrCmp );
03647350 3303
5b276ac1
RR
3304 wxGtkTreeModelChildren temp;
3305 void** base_ptr = &(m_children[0]);
03647350 3306 // Transfer positions to new_order array and
5b276ac1
RR
3307 // IDs to temp
3308 for (i = 0; i < child_count; i++)
3309 {
3310 new_order[i] = ptrs[i] - base_ptr;
3311 temp.Add( *ptrs[i] );
3312 }
3313
3314 // Transfer IDs back to m_children
3315 m_children.Clear();
3316 WX_APPEND_ARRAY( temp, m_children );
3317#endif
3318#if 0
3319 // Too slow
3320
1f226ad8
RR
3321 // Build up array with IDs and original positions
3322 wxGtkTreeModelChildWithPos* temp = new wxGtkTreeModelChildWithPos[child_count];
3323 size_t i;
3324 for (i = 0; i < child_count; i++)
3325 {
3326 temp[i].pos = i;
3327 temp[i].id = m_children[i];
3328 }
3329 // Sort array keeping original positions
3330 wxQsort( temp, child_count, sizeof(wxGtkTreeModelChildWithPos),
3331 &wxGtkTreeModelChildWithPosCmp, m_internal );
e4a8d29c
RR
3332 // Transfer positions to new_order array and
3333 // IDs to m_children
3334 m_children.Clear();
1f226ad8 3335 for (i = 0; i < child_count; i++)
e4a8d29c 3336 {
1f226ad8 3337 new_order[i] = temp[i].pos;
1f226ad8 3338 m_children.Add( temp[i].id );
e4a8d29c 3339 }
1f226ad8
RR
3340 // Delete array
3341 delete [] temp;
5b276ac1
RR
3342#endif
3343
3344#if 0
3345 // Too slow
03647350 3346
40196b1e
RR
3347 wxGtkTreeModelChildren temp;
3348 WX_APPEND_ARRAY( temp, m_children );
b9db5f30 3349
8c2654ce 3350 gs_internal = m_internal;
40196b1e 3351 m_children.Sort( &wxGtkTreeModelChildCmp );
03647350 3352
40196b1e 3353 unsigned int pos;
af110130 3354 for (pos = 0; pos < child_count; pos++)
4508fcd2 3355 {
40196b1e
RR
3356 void *id = m_children.Item( pos );
3357 int old_pos = temp.Index( id );
3358 new_order[pos] = old_pos;
4508fcd2 3359 }
1f226ad8 3360#endif
b9db5f30 3361
0be79c8a
RR
3362 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
3363
3364 GtkTreeIter iter;
40196b1e 3365 iter.user_data = GetItem().GetID();
0be79c8a 3366 iter.stamp = m_internal->GetGtkModel()->stamp;
b9db5f30 3367
0c2a7270
VZ
3368 gtk_tree_model_rows_reordered( gtk_tree_model,
3369 wxGtkTreePath(m_internal->get_path(&iter)), &iter, new_order );
b9db5f30 3370
0be79c8a 3371 delete [] new_order;
03647350 3372
1f226ad8 3373 unsigned int pos;
af110130 3374 for (pos = 0; pos < node_count; pos++)
4508fcd2 3375 {
af110130 3376 wxGtkTreeModelNode *node = m_nodes.Item( pos );
4508fcd2
RR
3377 node->Resort();
3378 }
3379}
3380
55fbde12
RR
3381//-----------------------------------------------------------------------------
3382// wxDataViewCtrlInternal
3383//-----------------------------------------------------------------------------
3384
673810ee 3385wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model )
b9db5f30 3386{
55fbde12 3387 m_owner = owner;
b9db5f30 3388 m_wx_model = wx_model;
ce00f59b 3389
673810ee 3390 m_gtk_model = NULL;
b9db5f30 3391 m_root = NULL;
b8b7b087 3392 m_sort_order = GTK_SORT_ASCENDING;
40196b1e 3393 m_sort_column = -1;
d32332aa 3394 m_dataview_sort_column = NULL;
8c2654ce 3395
591cc82d
RR
3396 m_dragDataObject = NULL;
3397 m_dropDataObject = NULL;
b9db5f30 3398
d8090b5e 3399 m_dirty = false;
ce00f59b 3400
673810ee
RR
3401 m_gtk_model = wxgtk_tree_model_new();
3402 m_gtk_model->internal = this;
3403
3404 m_notifier = new wxGtkDataViewModelNotifier( wx_model, this );
3405
3406 wx_model->AddNotifier( m_notifier );
3407
3408 // g_object_unref( gtk_model ); ???
3409
e39de702 3410 if (!m_wx_model->IsVirtualListModel())
2056dede 3411 InitTree();
ce00f59b 3412
673810ee 3413 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model) );
55fbde12 3414}
b9db5f30 3415
55fbde12
RR
3416wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
3417{
673810ee
RR
3418 m_wx_model->RemoveNotifier( m_notifier );
3419
ce00f59b 3420 // remove the model from the GtkTreeView before it gets destroyed
673810ee
RR
3421 gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner->GtkGetTreeView() ), NULL );
3422
55fbde12 3423 g_object_unref( m_gtk_model );
8c2654ce 3424
591cc82d
RR
3425 delete m_dragDataObject;
3426 delete m_dropDataObject;
55fbde12 3427}
b9db5f30 3428
d8090b5e
RR
3429void wxDataViewCtrlInternal::ScheduleRefresh()
3430{
3431 m_dirty = true;
3432}
3433
3434void wxDataViewCtrlInternal::OnInternalIdle()
3435{
3436 if (m_dirty)
3437 {
3438 GtkWidget *widget = m_owner->GtkGetTreeView();
3439 gtk_widget_queue_draw( widget );
3440 m_dirty = false;
3441 }
3442}
3443
ef427989
RR
3444void wxDataViewCtrlInternal::InitTree()
3445{
3446 wxDataViewItem item;
0be79c8a 3447 m_root = new wxGtkTreeModelNode( NULL, item, this );
ef427989
RR
3448
3449 BuildBranch( m_root );
3450}
3451
55fbde12
RR
3452void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
3453{
3454 if (node->GetChildCount() == 0)
3455 {
74fe973b
RR
3456 wxDataViewItemArray children;
3457 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
ce00f59b 3458
74fe973b
RR
3459 unsigned int pos;
3460 for (pos = 0; pos < count; pos++)
55fbde12 3461 {
74fe973b 3462 wxDataViewItem child = children[pos];
b9db5f30 3463
af110130
RR
3464 if (m_wx_model->IsContainer( child ))
3465 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
3466 else
3467 node->AddLeave( child.GetID() );
b9db5f30 3468
effd54b0 3469 // Don't send any events here
55fbde12
RR
3470 }
3471 }
3472}
3473
f6f0ef85
RR
3474// GTK+ dnd iface
3475
15cac64f
RR
3476bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format )
3477{
3478 wxGtkString atom_str( gdk_atom_name( format ) );
3479 m_dragSourceTargetEntryTarget = wxCharBuffer( atom_str );
8c2654ce 3480
15cac64f
RR
3481 m_dragSourceTargetEntry.target = m_dragSourceTargetEntryTarget.data();
3482 m_dragSourceTargetEntry.flags = 0;
3483 m_dragSourceTargetEntry.info = static_cast<guint>(-1);
8c2654ce 3484
15cac64f
RR
3485 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3486 GDK_BUTTON1_MASK, &m_dragSourceTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
8c2654ce 3487
15cac64f
RR
3488 return true;
3489}
3490
e4de825e
RR
3491bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat &format )
3492{
3493 wxGtkString atom_str( gdk_atom_name( format ) );
3494 m_dropTargetTargetEntryTarget = wxCharBuffer( atom_str );
8c2654ce 3495
b4c40918 3496 m_dropTargetTargetEntry.target = m_dropTargetTargetEntryTarget.data();
e4de825e
RR
3497 m_dropTargetTargetEntry.flags = 0;
3498 m_dropTargetTargetEntry.info = static_cast<guint>(-1);
8c2654ce 3499
e4de825e
RR
3500 gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
3501 &m_dropTargetTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
8c2654ce 3502
e4de825e
RR
3503 return true;
3504}
3505
7857346a 3506gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
3507 GtkTreePath *path )
3508{
591cc82d 3509 delete m_dragDataObject;
8c2654ce 3510
17d98558
VZ
3511 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3512 if ( !item )
3513 return FALSE;
f6f0ef85 3514
591cc82d 3515 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
e4de825e 3516 event.SetEventObject( m_owner );
15cac64f
RR
3517 event.SetItem( item );
3518 event.SetModel( m_wx_model );
591cc82d
RR
3519 if (!m_owner->HandleWindowEvent( event ))
3520 return FALSE;
8c2654ce 3521
591cc82d
RR
3522 if (!event.IsAllowed())
3523 return FALSE;
8c2654ce 3524
591cc82d
RR
3525 wxDataObject *obj = event.GetDataObject();
3526 if (!obj)
3527 return FALSE;
8c2654ce 3528
591cc82d 3529 m_dragDataObject = obj;
8c2654ce 3530
591cc82d 3531 return TRUE;
f6f0ef85
RR
3532}
3533
7857346a
VZ
3534gboolean
3535wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
3536 GtkTreePath *WXUNUSED(path))
f6f0ef85
RR
3537{
3538 return FALSE;
3539}
3540
7857346a 3541gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
3542 GtkTreePath *path, GtkSelectionData *selection_data )
3543{
17d98558
VZ
3544 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3545 if ( !item )
3546 return FALSE;
7857346a 3547
591cc82d
RR
3548 if (!m_dragDataObject->IsSupported( selection_data->target ))
3549 return FALSE;
7857346a 3550
591cc82d
RR
3551 size_t size = m_dragDataObject->GetDataSize( selection_data->target );
3552 if (size == 0)
3553 return FALSE;
8c2654ce 3554
591cc82d 3555 void *buf = malloc( size );
8c2654ce 3556
15cac64f 3557 gboolean res = FALSE;
591cc82d 3558 if (m_dragDataObject->GetDataHere( selection_data->target, buf ))
15cac64f 3559 {
15cac64f 3560 res = TRUE;
8c2654ce 3561
591cc82d
RR
3562 gtk_selection_data_set( selection_data, selection_data->target,
3563 8, (const guchar*) buf, size );
15cac64f 3564 }
f6f0ef85 3565
591cc82d 3566 free( buf );
7857346a 3567
15cac64f 3568 return res;
f6f0ef85
RR
3569}
3570
7857346a
VZ
3571gboolean
3572wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
e4de825e
RR
3573 GtkTreePath *path,
3574 GtkSelectionData *selection_data)
f6f0ef85 3575{
17d98558
VZ
3576 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3577 if ( !item )
3578 return FALSE;
8c2654ce 3579
e4de825e
RR
3580 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() );
3581 event.SetEventObject( m_owner );
3582 event.SetItem( item );
3583 event.SetModel( m_wx_model );
3584 event.SetDataFormat( selection_data->target );
3585 event.SetDataSize( selection_data->length );
3586 event.SetDataBuffer( selection_data->data );
3587 if (!m_owner->HandleWindowEvent( event ))
3588 return FALSE;
8c2654ce 3589
e4de825e
RR
3590 if (!event.IsAllowed())
3591 return FALSE;
3592
3593 return TRUE;
f6f0ef85
RR
3594}
3595
7857346a
VZ
3596gboolean
3597wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
e4de825e
RR
3598 GtkTreePath *path,
3599 GtkSelectionData *selection_data)
f6f0ef85 3600{
17d98558
VZ
3601 wxDataViewItem item(GetOwner()->GTKPathToItem(path));
3602 if ( !item )
3603 return FALSE;
8c2654ce 3604
e4de825e
RR
3605 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
3606 event.SetEventObject( m_owner );
3607 event.SetItem( item );
3608 event.SetModel( m_wx_model );
3609 event.SetDataFormat( selection_data->target );
3610 if (!m_owner->HandleWindowEvent( event ))
3611 return FALSE;
8c2654ce 3612
e4de825e
RR
3613 if (!event.IsAllowed())
3614 return FALSE;
8c2654ce 3615
e4de825e 3616 return TRUE;
f6f0ef85
RR
3617}
3618
3619// notifications from wxDataViewModel
3620
33ba5a05
RR
3621bool wxDataViewCtrlInternal::Cleared()
3622{
3623 if (m_root)
3624 {
3625 delete m_root;
c225708f 3626 m_root = NULL;
7857346a 3627 }
ce00f59b 3628
c225708f 3629 InitTree();
ce00f59b 3630
d8090b5e 3631 ScheduleRefresh();
ce00f59b 3632
33ba5a05
RR
3633 return true;
3634}
3635
4508fcd2
RR
3636void wxDataViewCtrlInternal::Resort()
3637{
e39de702 3638 if (!m_wx_model->IsVirtualListModel())
2056dede 3639 m_root->Resort();
ce00f59b 3640
d8090b5e 3641 ScheduleRefresh();
4508fcd2
RR
3642}
3643
55fbde12
RR
3644bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
3645{
e39de702 3646 if (!m_wx_model->IsVirtualListModel())
2056dede
RR
3647 {
3648 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3649 wxASSERT_MSG(parent_node,
3650 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3651
2056dede
RR
3652 if (m_wx_model->IsContainer( item ))
3653 parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) );
3654 else
3655 parent_node->AddLeave( item.GetID() );
3656 }
b9db5f30 3657
d8090b5e 3658 ScheduleRefresh();
ce00f59b 3659
55fbde12
RR
3660 return true;
3661}
3662
469d3e9b 3663bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
55fbde12 3664{
e39de702 3665 if (!m_wx_model->IsVirtualListModel())
2056dede
RR
3666 {
3667 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3668 wxASSERT_MSG(parent_node,
3669 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3670
2056dede
RR
3671 parent_node->DeleteChild( item.GetID() );
3672 }
b9db5f30 3673
d8090b5e 3674 ScheduleRefresh();
ce00f59b 3675
d8331a01
RR
3676 return true;
3677}
3678
3679bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
3680{
6608fdab 3681 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
3682 event.SetEventObject( m_owner );
3683 event.SetModel( m_owner->GetModel() );
3684 event.SetItem( item );
937013e0 3685 m_owner->HandleWindowEvent( event );
b9db5f30 3686
d8331a01
RR
3687 return true;
3688}
3689
3690bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int col )
3691{
6608fdab 3692 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
3693 event.SetEventObject( m_owner );
3694 event.SetModel( m_owner->GetModel() );
3695 event.SetColumn( col );
6608fdab 3696 event.SetDataViewColumn( GetOwner()->GetColumn(col) );
d8331a01 3697 event.SetItem( item );
937013e0 3698 m_owner->HandleWindowEvent( event );
b9db5f30 3699
d8331a01
RR
3700 return true;
3701}
3702
f6f0ef85
RR
3703// GTK+ model iface
3704
2056dede
RR
3705GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
3706{
ce5abbf9
VS
3707 int flags = 0;
3708
3709 if ( m_wx_model->IsListModel() )
3710 flags |= GTK_TREE_MODEL_LIST_ONLY;
3711
3712 if ( !m_wx_model->IsVirtualListModel() )
3713 flags |= GTK_TREE_MODEL_ITERS_PERSIST;
3714
3715 return GtkTreeModelFlags(flags);
2056dede
RR
3716}
3717
55fbde12
RR
3718gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
3719{
9330d5af 3720
e39de702 3721 if (m_wx_model->IsVirtualListModel())
2056dede 3722 {
9330d5af 3723 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
03647350 3724
2056dede 3725 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
03647350 3726
9330d5af 3727 if (i >= wx_model->GetCount())
2056dede 3728 return FALSE;
55fbde12 3729
2056dede 3730 iter->stamp = m_gtk_model->stamp;
0ae51f62
RR
3731 // user_data is just the index +1
3732 iter->user_data = (gpointer) (i+1);
55fbde12 3733
2056dede
RR
3734 return TRUE;
3735 }
3736 else
55fbde12 3737 {
2056dede
RR
3738 int depth = gtk_tree_path_get_depth( path );
3739
3740 wxGtkTreeModelNode *node = m_root;
3741
3742 int i;
b9db5f30 3743 for (i = 0; i < depth; i++)
2056dede
RR
3744 {
3745 BuildBranch( node );
b9db5f30 3746
2056dede
RR
3747 gint pos = gtk_tree_path_get_indices (path)[i];
3748 if (pos < 0) return FALSE;
3749 if ((size_t)pos >= node->GetChildCount()) return FALSE;
b9db5f30 3750
2056dede 3751 void* id = node->GetChildren().Item( (size_t) pos );
b9db5f30 3752
2056dede 3753 if (i == depth-1)
af110130 3754 {
2056dede
RR
3755 iter->stamp = m_gtk_model->stamp;
3756 iter->user_data = id;
3757 return TRUE;
af110130 3758 }
2056dede
RR
3759
3760 size_t count = node->GetNodes().GetCount();
3761 size_t pos2;
3762 for (pos2 = 0; pos2 < count; pos2++)
3763 {
3764 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
3765 if (child_node->GetItem().GetID() == id)
3766 {
3767 node = child_node;
3768 break;
3769 }
b9db5f30 3770 }
2056dede 3771 }
af110130 3772 }
55fbde12 3773
af110130 3774 return FALSE;
55fbde12
RR
3775}
3776
3777GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
3778{
8650c4ba
RR
3779 // When this is called from ItemDeleted(), the item is already
3780 // deleted in the model.
3781
55fbde12 3782 GtkTreePath *retval = gtk_tree_path_new ();
b9db5f30 3783
e39de702 3784 if (m_wx_model->IsVirtualListModel())
2056dede 3785 {
559c42a5
RR
3786 // iter is root, add nothing
3787 if (!iter->user_data)
3788 return retval;
74035a19 3789
0ae51f62
RR
3790 // user_data is just the index +1
3791 int i = ( (wxUIntPtr) iter->user_data ) -1;
2056dede
RR
3792 gtk_tree_path_append_index (retval, i);
3793 }
3794 else
55fbde12 3795 {
b9db5f30
VS
3796 void *id = iter->user_data;
3797
2056dede
RR
3798 wxGtkTreeModelNode *node = FindParentNode( iter );
3799 while (node)
3800 {
3801 int pos = node->GetChildren().Index( id );
ce00f59b 3802
2056dede 3803 gtk_tree_path_prepend_index( retval, pos );
b9db5f30 3804
2056dede
RR
3805 id = node->GetItem().GetID();
3806 node = node->GetParent();
3807 }
55fbde12 3808 }
b9db5f30 3809
55fbde12
RR
3810 return retval;
3811}
3812
3813gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
3814{
e39de702 3815 if (m_wx_model->IsVirtualListModel())
2056dede 3816 {
9330d5af 3817 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
b9db5f30 3818
0ae51f62
RR
3819 // user_data is just the index +1
3820 int n = ( (wxUIntPtr) iter->user_data ) -1;
69892729 3821
2056dede 3822 if (n == -1)
8a568372
RR
3823 {
3824 iter->user_data = NULL;
2056dede 3825 return FALSE;
8a568372 3826 }
40196b1e 3827
8a568372
RR
3828 if (n >= (int) wx_model->GetCount()-1)
3829 {
3830 iter->user_data = NULL;
2056dede 3831 return FALSE;
8a568372 3832 }
03647350 3833
0ae51f62
RR
3834 // user_data is just the index +1 (+2 because we need the next)
3835 iter->user_data = (gpointer) (n+2);
2056dede
RR
3836 }
3837 else
3838 {
3839 wxGtkTreeModelNode *parent = FindParentNode( iter );
3840 if( parent == NULL )
8a568372
RR
3841 {
3842 iter->user_data = NULL;
2056dede 3843 return FALSE;
8a568372 3844 }
2056dede
RR
3845
3846 int pos = parent->GetChildren().Index( iter->user_data );
3847
3848 if (pos == (int) parent->GetChildCount()-1)
8a568372
RR
3849 {
3850 iter->user_data = NULL;
2056dede 3851 return FALSE;
8a568372 3852 }
b9db5f30 3853
2056dede
RR
3854 iter->user_data = parent->GetChildren().Item( pos+1 );
3855 }
55fbde12
RR
3856
3857 return TRUE;
3858}
3859
3860gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
3861{
e39de702 3862 if (m_wx_model->IsVirtualListModel())
2056dede
RR
3863 {
3864 // this is a list, nodes have no children
3865 if (parent)
3866 return FALSE;
3867
3868 iter->stamp = m_gtk_model->stamp;
9330d5af 3869 iter->user_data = (gpointer) 1;
2056dede
RR
3870
3871 return TRUE;
3872 }
3873 else
3874 {
8a568372
RR
3875 if (iter == NULL)
3876 {
3877 if (m_root->GetChildCount() == 0) return FALSE;
3878 iter->stamp = m_gtk_model->stamp;
3879 iter->user_data = (gpointer) m_root->GetChildren().Item( 0 );
3880 return TRUE;
3881 }
03647350 3882
74035a19 3883
3556d648
RR
3884 wxDataViewItem item;
3885 if (parent)
3886 item = wxDataViewItem( (void*) parent->user_data );
b9db5f30 3887
2056dede
RR
3888 if (!m_wx_model->IsContainer( item ))
3889 return FALSE;
b9db5f30 3890
2056dede 3891 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
3892 wxASSERT_MSG(parent_node,
3893 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3894
2056dede 3895 BuildBranch( parent_node );
b9db5f30 3896
2056dede
RR
3897 if (parent_node->GetChildCount() == 0)
3898 return FALSE;
b9db5f30 3899
2056dede
RR
3900 iter->stamp = m_gtk_model->stamp;
3901 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
3902 }
b9db5f30 3903
55fbde12
RR
3904 return TRUE;
3905}
3906
3907gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
3908{
e39de702 3909 if (m_wx_model->IsVirtualListModel())
2056dede 3910 {
9330d5af
RR
3911 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
3912
3913 if (iter == NULL)
8a568372 3914 return (wx_model->GetCount() > 0);
03647350 3915
2056dede
RR
3916 // this is a list, nodes have no children
3917 return FALSE;
3918 }
3919 else
3920 {
8a568372
RR
3921 if (iter == NULL)
3922 return (m_root->GetChildCount() > 0);
03647350 3923
2056dede 3924 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3925
2056dede 3926 bool is_container = m_wx_model->IsContainer( item );
b9db5f30 3927
2056dede
RR
3928 if (!is_container)
3929 return FALSE;
b9db5f30 3930
2056dede 3931 wxGtkTreeModelNode *node = FindNode( iter );
c2489d8e
FM
3932 wxASSERT_MSG(node,
3933 "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel");
3934
2056dede 3935 BuildBranch( node );
b9db5f30 3936
2056dede
RR
3937 return (node->GetChildCount() > 0);
3938 }
55fbde12
RR
3939}
3940
3941gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
3942{
e39de702 3943 if (m_wx_model->IsVirtualListModel())
2056dede 3944 {
9330d5af 3945 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
b9db5f30 3946
2056dede 3947 if (iter == NULL)
9330d5af 3948 return (gint) wx_model->GetCount();
2056dede 3949
55fbde12 3950 return 0;
2056dede
RR
3951 }
3952 else
3953 {
11f20f99
RR
3954 if (iter == NULL)
3955 return m_root->GetChildCount();
c2489d8e 3956
2056dede 3957 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3958
2056dede
RR
3959 if (!m_wx_model->IsContainer( item ))
3960 return 0;
b9db5f30 3961
2056dede 3962 wxGtkTreeModelNode *parent_node = FindNode( iter );
c2489d8e
FM
3963 wxASSERT_MSG(parent_node,
3964 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
3965
2056dede 3966 BuildBranch( parent_node );
ef427989 3967
2056dede
RR
3968 return parent_node->GetChildCount();
3969 }
55fbde12
RR
3970}
3971
3972gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
3973{
e39de702 3974 if (m_wx_model->IsVirtualListModel())
2056dede 3975 {
9330d5af 3976 wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
b9db5f30 3977
2056dede
RR
3978 if (parent)
3979 return FALSE;
3980
3981 if (n < 0)
3982 return FALSE;
3983
9330d5af 3984 if (n >= (gint) wx_model->GetCount())
2056dede
RR
3985 return FALSE;
3986
3987 iter->stamp = m_gtk_model->stamp;
0ae51f62 3988 // user_data is just the index +1
9330d5af 3989 iter->user_data = (gpointer) (n+1);
2056dede
RR
3990
3991 return TRUE;
3992 }
3993 else
3994 {
3995 void* id = NULL;
3996 if (parent) id = (void*) parent->user_data;
3997 wxDataViewItem item( id );
b9db5f30 3998
2056dede
RR
3999 if (!m_wx_model->IsContainer( item ))
4000 return FALSE;
b9db5f30 4001
2056dede 4002 wxGtkTreeModelNode *parent_node = FindNode( parent );
c2489d8e
FM
4003 wxASSERT_MSG(parent_node,
4004 "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
4005
2056dede 4006 BuildBranch( parent_node );
55fbde12 4007
2056dede
RR
4008 iter->stamp = m_gtk_model->stamp;
4009 iter->user_data = parent_node->GetChildren().Item( n );
55fbde12 4010
2056dede
RR
4011 return TRUE;
4012 }
55fbde12
RR
4013}
4014
4015gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
4016{
e39de702 4017 if (m_wx_model->IsVirtualListModel())
2056dede 4018 {
55fbde12 4019 return FALSE;
2056dede
RR
4020 }
4021 else
4022 {
4023 wxGtkTreeModelNode *node = FindParentNode( child );
4024 if (!node)
4025 return FALSE;
b9db5f30 4026
2056dede
RR
4027 iter->stamp = m_gtk_model->stamp;
4028 iter->user_data = (gpointer) node->GetItem().GetID();
55fbde12 4029
2056dede
RR
4030 return TRUE;
4031 }
55fbde12 4032}
b9db5f30 4033
74035a19 4034// item can be deleted already in the model
8650c4ba
RR
4035int wxDataViewCtrlInternal::GetIndexOf( const wxDataViewItem &parent, const wxDataViewItem &item )
4036{
559c42a5
RR
4037 if (m_wx_model->IsVirtualListModel())
4038 {
74035a19 4039 return wxPtrToUInt(item.GetID()) - 1;
559c42a5
RR
4040 }
4041 else
8650c4ba 4042 {
559c42a5
RR
4043 wxGtkTreeModelNode *parent_node = FindNode( parent );
4044 wxGtkTreeModelChildren &children = parent_node->GetChildren();
4045 size_t j;
4046 for (j = 0; j < children.GetCount(); j++)
4047 {
4048 if (children[j] == item.GetID())
4049 return j;
4050 }
8650c4ba
RR
4051 }
4052 return -1;
4053}
4054
4055
55fbde12 4056static wxGtkTreeModelNode*
69892729 4057wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
55fbde12 4058{
69892729
RR
4059 if( model == NULL )
4060 return NULL;
55fbde12 4061
69892729
RR
4062 ItemList list;
4063 list.DeleteContents( true );
4064 wxDataViewItem it( item );
b9db5f30 4065
69892729 4066 while( it.IsOk() )
55fbde12 4067 {
69892729
RR
4068 wxDataViewItem * pItem = new wxDataViewItem( it );
4069 list.Insert( pItem );
4070 it = model->GetParent( it );
4071 }
4072
4073 wxGtkTreeModelNode * node = treeNode;
966cb94d 4074 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
69892729
RR
4075 {
4076 if( node && node->GetNodes().GetCount() != 0 )
ef427989 4077 {
69892729 4078 int len = node->GetNodes().GetCount();
8650c4ba 4079 wxGtkTreeModelNodes &nodes = node->GetNodes();
69892729
RR
4080 int j = 0;
4081 for( ; j < len; j ++)
4082 {
4083 if( nodes[j]->GetItem() == *(n->GetData()))
4084 {
4085 node = nodes[j];
4086 break;
b9db5f30 4087 }
69892729
RR
4088 }
4089
4090 if( j == len )
4091 {
4092 return NULL;
4093 }
ef427989 4094 }
69892729
RR
4095 else
4096 return NULL;
55fbde12 4097 }
69892729
RR
4098 return node;
4099
55fbde12
RR
4100}
4101
4102wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
4103{
4104 if (!iter)
4105 return m_root;
4106
4107 wxDataViewItem item( (void*) iter->user_data );
0be79c8a
RR
4108 if (!item.IsOk())
4109 return m_root;
b9db5f30 4110
69892729 4111 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
b9db5f30 4112
c2489d8e 4113/*
55fbde12
RR
4114 if (!result)
4115 {
26f119eb 4116 wxLogDebug( "Not found %p", iter->user_data );
55fbde12
RR
4117 char *crash = NULL;
4118 *crash = 0;
4119 }
c2489d8e
FM
4120 // TODO: remove this code
4121*/
b9db5f30 4122
55fbde12
RR
4123 return result;
4124}
4125
4126wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
4127{
0be79c8a
RR
4128 if (!item.IsOk())
4129 return m_root;
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", item.GetID() );
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
af110130 4146static wxGtkTreeModelNode*
69892729 4147wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
af110130 4148{
69892729
RR
4149 if( model == NULL )
4150 return NULL;
4151
4152 ItemList list;
4153 list.DeleteContents( true );
4154 if( !item.IsOk() )
4155 return NULL;
4156
4157 wxDataViewItem it( model->GetParent( item ) );
4158 while( it.IsOk() )
af110130 4159 {
69892729
RR
4160 wxDataViewItem * pItem = new wxDataViewItem( it );
4161 list.Insert( pItem );
4162 it = model->GetParent( it );
af110130
RR
4163 }
4164
69892729 4165 wxGtkTreeModelNode * node = treeNode;
966cb94d 4166 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
af110130 4167 {
69892729 4168 if( node && node->GetNodes().GetCount() != 0 )
af110130 4169 {
69892729
RR
4170 int len = node->GetNodes().GetCount();
4171 wxGtkTreeModelNodes nodes = node->GetNodes();
4172 int j = 0;
4173 for( ; j < len; j ++)
4174 {
4175 if( nodes[j]->GetItem() == *(n->GetData()))
4176 {
4177 node = nodes[j];
4178 break;
b9db5f30 4179 }
69892729
RR
4180 }
4181
4182 if( j == len )
4183 {
4184 return NULL;
4185 }
af110130 4186 }
69892729
RR
4187 else
4188 return NULL;
4189 }
4190 //Examine whether the node is item's parent node
4191 int len = node->GetChildCount();
4192 for( int i = 0; i < len ; i ++ )
4193 {
4194 if( node->GetChildren().Item( i ) == item.GetID() )
4195 return node;
af110130 4196 }
af110130
RR
4197 return NULL;
4198}
4199
4200wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
4201{
4202 if (!iter)
4203 return NULL;
b9db5f30 4204
af110130
RR
4205 wxDataViewItem item( (void*) iter->user_data );
4206 if (!item.IsOk())
4207 return NULL;
4208
69892729 4209 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
4210}
4211
4212wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
4213{
4214 if (!item.IsOk())
4215 return NULL;
4216
69892729 4217 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
4218}
4219
eb7f97f8
RR
4220//-----------------------------------------------------------------------------
4221// wxDataViewCtrl signal callbacks
4222//-----------------------------------------------------------------------------
4223
4224static void
ad386793 4225wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv )
eb7f97f8 4226{
b94db696
RR
4227 if (!GTK_WIDGET_REALIZED(dv->m_widget))
4228 return;
f4322df6 4229
d86c1870 4230 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() );
6848478c 4231 event.SetItem( dv->GetSelection() );
eb7f97f8 4232 event.SetModel( dv->GetModel() );
937013e0 4233 dv->HandleWindowEvent( event );
eb7f97f8
RR
4234}
4235
f828871d 4236static void
ad386793
RR
4237wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path,
4238 GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv )
f828871d 4239{
e0062c04 4240 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
55fbde12 4241
17d98558 4242 wxDataViewItem item(dv->GTKPathToItem(path));
55fbde12 4243 event.SetItem( item );
f828871d 4244 event.SetModel( dv->GetModel() );
937013e0 4245 dv->HandleWindowEvent( event );
f828871d
RR
4246}
4247
718fd180 4248static gboolean
ad386793
RR
4249wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4250 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
4251{
4252 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
4253
4254 wxDataViewItem item( (void*) iter->user_data );;
4255 event.SetItem( item );
4256 event.SetModel( dv->GetModel() );
937013e0 4257 dv->HandleWindowEvent( event );
b9db5f30 4258
718fd180
RR
4259 return !event.IsAllowed();
4260}
4261
4262static void
ad386793
RR
4263wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4264 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
4265{
4266 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
4267
4268 wxDataViewItem item( (void*) iter->user_data );;
4269 event.SetItem( item );
4270 event.SetModel( dv->GetModel() );
937013e0 4271 dv->HandleWindowEvent( event );
718fd180
RR
4272}
4273
4274static gboolean
ad386793
RR
4275wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4276 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
4277{
4278 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
4279
4280 wxDataViewItem item( (void*) iter->user_data );;
4281 event.SetItem( item );
4282 event.SetModel( dv->GetModel() );
937013e0 4283 dv->HandleWindowEvent( event );
b9db5f30 4284
718fd180
RR
4285 return !event.IsAllowed();
4286}
4287
4288static void
ad386793
RR
4289wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
4290 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
4291{
4292 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
4293
4294 wxDataViewItem item( (void*) iter->user_data );;
4295 event.SetItem( item );
4296 event.SetModel( dv->GetModel() );
937013e0 4297 dv->HandleWindowEvent( event );
718fd180
RR
4298}
4299
790b137e 4300//-----------------------------------------------------------------------------
718fd180 4301 // wxDataViewCtrl
790b137e
RR
4302//-----------------------------------------------------------------------------
4303
48200154 4304void wxDataViewCtrl::AddChildGTK(wxWindowGTK* child)
1e510b1e 4305{
48200154 4306 GtkWidget* treeview = GtkGetTreeView();
1e510b1e
RR
4307
4308 // Insert widget in GtkTreeView
4309 if (GTK_WIDGET_REALIZED(treeview))
e8375af8 4310 gtk_widget_set_parent_window( child->m_widget,
1e510b1e
RR
4311 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
4312 gtk_widget_set_parent( child->m_widget, treeview );
4313}
4314
4315static
4316void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
ad386793 4317 GtkAllocation *WXUNUSED(gtk_alloc),
1e510b1e
RR
4318 wxDataViewCtrl *win )
4319{
966cb94d 4320 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
1e510b1e
RR
4321 while (node)
4322 {
4323 wxWindow *child = node->GetData();
e8375af8 4324
1e510b1e
RR
4325 GtkRequisition req;
4326 gtk_widget_size_request( child->m_widget, &req );
e8375af8 4327
1e510b1e
RR
4328 GtkAllocation alloc;
4329 alloc.x = child->m_x;
4330 alloc.y = child->m_y;
4331 alloc.width = child->m_width;
4332 alloc.height = child->m_height;
4333 gtk_widget_size_allocate( child->m_widget, &alloc );
e8375af8 4334
1e510b1e
RR
4335 node = node->GetNext();
4336 }
4337}
4338
4339
6608fdab
RR
4340//-----------------------------------------------------------------------------
4341// "motion_notify_event"
4342//-----------------------------------------------------------------------------
4343
4344static gboolean
ad386793 4345gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget),
74dea0de
RR
4346 GdkEventMotion *gdk_event,
4347 wxDataViewCtrl *dv )
6608fdab
RR
4348{
4349 if (gdk_event->is_hint)
4350 {
4351 int x = 0;
4352 int y = 0;
4353 GdkModifierType state;
4354 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
4355 gdk_event->x = x;
4356 gdk_event->y = y;
4357 }
4358
0c2a7270 4359 wxGtkTreePath path;
6608fdab
RR
4360 GtkTreeViewColumn *column = NULL;
4361 gint cell_x = 0;
4362 gint cell_y = 0;
b9db5f30 4363 if (gtk_tree_view_get_path_at_pos(
6608fdab
RR
4364 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4365 (int) gdk_event->x, (int) gdk_event->y,
0c2a7270 4366 path.ByRef(),
6608fdab
RR
4367 &column,
4368 &cell_x,
4369 &cell_y))
4370 {
4371 if (path)
4372 {
4373 GtkTreeIter iter;
4374 dv->GtkGetInternal()->get_iter( &iter, path );
6608fdab
RR
4375 }
4376 }
4377
4378
4379 return FALSE;
4380}
4381
74dea0de
RR
4382//-----------------------------------------------------------------------------
4383// "button_press_event"
4384//-----------------------------------------------------------------------------
4385
4386static gboolean
4387gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget),
4388 GdkEventButton *gdk_event,
4389 wxDataViewCtrl *dv )
4390{
4391 if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS))
4392 {
0c2a7270 4393 wxGtkTreePath path;
74dea0de
RR
4394 GtkTreeViewColumn *column = NULL;
4395 gint cell_x = 0;
4396 gint cell_y = 0;
4397 if (gtk_tree_view_get_path_at_pos(
4398 GTK_TREE_VIEW(dv->GtkGetTreeView()),
4399 (int) gdk_event->x, (int) gdk_event->y,
0c2a7270 4400 path.ByRef(),
74dea0de
RR
4401 &column,
4402 &cell_x,
4403 &cell_y))
4404 {
4405 if (path)
4406 {
74dea0de 4407 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() );
17d98558 4408 event.SetItem(dv->GTKPathToItem(path));
74dea0de 4409 event.SetModel( dv->GetModel() );
0c2a7270 4410 return dv->HandleWindowEvent( event );
74dea0de
RR
4411 }
4412 }
4413 }
4414
4415 return FALSE;
4416}
1e510b1e 4417
239eaa41
RR
4418IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4419
4420wxDataViewCtrl::~wxDataViewCtrl()
4421{
ef11ea71
RR
4422 m_cols.Clear();
4423
55fbde12 4424 delete m_internal;
239eaa41
RR
4425}
4426
4427void wxDataViewCtrl::Init()
4428{
33ba5a05 4429 m_internal = NULL;
bc0289bf 4430
ef11ea71 4431 m_cols.DeleteContents( true );
239eaa41
RR
4432}
4433
62e9285a
VZ
4434bool wxDataViewCtrl::Create(wxWindow *parent,
4435 wxWindowID id,
4436 const wxPoint& pos,
4437 const wxSize& size,
4438 long style,
4439 const wxValidator& validator,
4440 const wxString& name)
239eaa41 4441{
239eaa41 4442 if (!PreCreation( parent, pos, size ) ||
62e9285a 4443 !CreateBase( parent, id, pos, size, style, validator, name ))
239eaa41
RR
4444 {
4445 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
93763ad5 4446 return false;
239eaa41 4447 }
1a367564
RR
4448
4449 m_widget = gtk_scrolled_window_new (NULL, NULL);
9ff9d30c 4450 g_object_ref(m_widget);
6493aaca 4451
496e7ec6 4452 GTKScrolledWindowSetBorder(m_widget, style);
1a367564
RR
4453
4454 m_treeview = gtk_tree_view_new();
4455 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
e8375af8 4456
1e510b1e
RR
4457 g_signal_connect (m_treeview, "size_allocate",
4458 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
ed4b0fdc 4459
b94db696
RR
4460#ifdef __WXGTK26__
4461 if (!gtk_check_version(2,6,0))
344ed1f3
RR
4462 {
4463 bool fixed = (style & wxDV_VARIABLE_LINE_HEIGHT) == 0;
4464 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4465 }
b94db696
RR
4466#endif
4467
daebb44c
RR
4468 if (style & wxDV_MULTIPLE)
4469 {
4470 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4471 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
4472 }
93763ad5 4473
9861f022
RR
4474 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
4475
4476#ifdef __WXGTK210__
4477 if (!gtk_check_version(2,10,0))
4478 {
4479 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
f4322df6
VZ
4480
4481 if ((style & wxDV_HORIZ_RULES) != 0 &&
9861f022
RR
4482 (style & wxDV_VERT_RULES) != 0)
4483 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
4484 else if (style & wxDV_VERT_RULES)
4485 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
4486 else if (style & wxDV_HORIZ_RULES)
4487 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
4488
1a07a730
RR
4489 if (grid != GTK_TREE_VIEW_GRID_LINES_NONE)
4490 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
9861f022 4491 }
9861f022 4492#endif
1a07a730
RR
4493
4494 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 );
9861f022 4495
1a367564 4496 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
16eab2e1 4497 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1a367564 4498 gtk_widget_show (m_treeview);
93763ad5 4499
239eaa41
RR
4500 m_parent->DoAddChild( this );
4501
b94db696
RR
4502 PostCreation(size);
4503
b086d55f
RR
4504 GtkEnableSelectionEvents();
4505
718fd180 4506 g_signal_connect_after (m_treeview, "row-activated",
f828871d 4507 G_CALLBACK (wxdataview_row_activated_callback), this);
eb7f97f8 4508
718fd180
RR
4509 g_signal_connect (m_treeview, "test-collapse-row",
4510 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
4511
4512 g_signal_connect_after (m_treeview, "row-collapsed",
4513 G_CALLBACK (wxdataview_row_collapsed_callback), this);
4514
4515 g_signal_connect (m_treeview, "test-expand-row",
4516 G_CALLBACK (wxdataview_test_expand_row_callback), this);
b9db5f30 4517
718fd180
RR
4518 g_signal_connect_after (m_treeview, "row-expanded",
4519 G_CALLBACK (wxdataview_row_expanded_callback), this);
4520
6608fdab
RR
4521 g_signal_connect (m_treeview, "motion_notify_event",
4522 G_CALLBACK (gtk_dataview_motion_notify_callback), this);
4523
74dea0de
RR
4524 g_signal_connect (m_treeview, "button_press_event",
4525 G_CALLBACK (gtk_dataview_button_press_callback), this);
7857346a 4526
239eaa41
RR
4527 return true;
4528}
4529
17d98558
VZ
4530wxDataViewItem wxDataViewCtrl::GTKPathToItem(GtkTreePath *path) const
4531{
4532 GtkTreeIter iter;
4533 return wxDataViewItem(path && m_internal->get_iter(&iter, path)
4534 ? iter.user_data
4535 : NULL);
4536}
4537
31fb32e1
RR
4538void wxDataViewCtrl::OnInternalIdle()
4539{
4540 wxWindow::OnInternalIdle();
ce00f59b 4541
d8090b5e 4542 m_internal->OnInternalIdle();
f4322df6 4543
9861f022 4544 unsigned int cols = GetColumnCount();
31fb32e1
RR
4545 unsigned int i;
4546 for (i = 0; i < cols; i++)
4547 {
4548 wxDataViewColumn *col = GetColumn( i );
4549 col->OnInternalIdle();
4550 }
ce00f59b 4551
3f53dd3a
RR
4552 if (m_ensureVisibleDefered.IsOk())
4553 {
4554 ExpandAncestors(m_ensureVisibleDefered);
4555 GtkTreeIter iter;
4556 iter.user_data = (gpointer) m_ensureVisibleDefered.GetID();
4557 wxGtkTreePath path(m_internal->get_path( &iter ));
4558 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4559 m_ensureVisibleDefered = wxDataViewItem(0);
4560 }
31fb32e1
RR
4561}
4562
e0062c04 4563bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
239eaa41 4564{
5276b0a5 4565 wxDELETE(m_internal);
7857346a 4566
239eaa41
RR
4567 if (!wxDataViewCtrlBase::AssociateModel( model ))
4568 return false;
4569
344ed1f3
RR
4570#ifdef __WXGTK26__
4571 if (!gtk_check_version(2,6,0))
4572 {
4573 bool fixed = (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) == 0) || (model->IsVirtualListModel()));
4574 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed );
4575 }
4576#endif
4577
673810ee 4578 m_internal = new wxDataViewCtrlInternal( this, model );
93763ad5 4579
239eaa41
RR
4580 return true;
4581}
790b137e 4582
15cac64f
RR
4583bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4584{
4585 return m_internal->EnableDragSource( format );
4586}
4587
e4de825e
RR
4588bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4589{
4590 return m_internal->EnableDropTarget( format );
4591}
4592
fa28826d
RR
4593bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4594{
4595 if (!wxDataViewCtrlBase::AppendColumn(col))
4596 return false;
93763ad5 4597
91a6c655
RR
4598 m_cols.Append( col );
4599
ad386793
RR
4600#ifdef __WXGTK26__
4601 if (!gtk_check_version(2,6,0))
4602 {
4603 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4604 GTK_TREE_VIEW_COLUMN_FIXED)
4605 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4606 }
4607#endif
4608
b9db5f30 4609 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
4610 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4611
4612 return true;
4613}
4614
736fe67c
RR
4615bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4616{
4617 if (!wxDataViewCtrlBase::PrependColumn(col))
4618 return false;
4619
4620 m_cols.Insert( col );
4621
ad386793
RR
4622#ifdef __WXGTK26__
4623 if (!gtk_check_version(2,6,0))
4624 {
4625 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4626 GTK_TREE_VIEW_COLUMN_FIXED)
4627 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4628 }
4629#endif
4630
b9db5f30 4631 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
736fe67c
RR
4632 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 );
4633
4634 return true;
4635}
4636
19723525
RR
4637bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4638{
4639 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4640 return false;
4641
4642 m_cols.Insert( pos, col );
4643
4644#ifdef __WXGTK26__
4645 if (!gtk_check_version(2,6,0))
4646 {
4647 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
4648 GTK_TREE_VIEW_COLUMN_FIXED)
4649 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
4650 }
4651#endif
4652
4653 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
4654 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), pos );
4655
4656 return true;
4657}
4658
91a6c655
RR
4659unsigned int wxDataViewCtrl::GetColumnCount() const
4660{
4661 return m_cols.GetCount();
4662}
4663
4664wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
4665{
4666 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
4667 if (!gtk_col)
4668 return NULL;
b9db5f30 4669
91a6c655 4670 wxDataViewColumnList::const_iterator iter;
ef11ea71 4671 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
91a6c655
RR
4672 {
4673 wxDataViewColumn *col = *iter;
4674 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
4675 {
4676 return col;
4677 }
4678 }
b9db5f30 4679
91a6c655
RR
4680 return NULL;
4681}
4682
4683bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4684{
b9db5f30 4685 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
4686 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
4687
ef11ea71 4688 m_cols.DeleteObject( column );
fa28826d
RR
4689
4690 return true;
4691}
4692
91a6c655
RR
4693bool wxDataViewCtrl::ClearColumns()
4694{
4695 wxDataViewColumnList::iterator iter;
ef11ea71 4696 for (iter = m_cols.begin(); iter != m_cols.end(); ++iter)
91a6c655
RR
4697 {
4698 wxDataViewColumn *col = *iter;
b9db5f30 4699 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
4700 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
4701 }
b9db5f30 4702
ef11ea71 4703 m_cols.Clear();
b9db5f30 4704
91a6c655
RR
4705 return true;
4706}
4707
453091c2
RR
4708int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4709{
aed836f3
RR
4710 GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetConstGtkHandle());
4711
4712 GList *list = gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4713
aed836f3 4714 gint pos = g_list_index( list, (gconstpointer) gtk_column );
b9db5f30 4715
aed836f3
RR
4716 g_list_free( list );
4717
4718 return pos;
453091c2
RR
4719}
4720
21f47fb9
RR
4721wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4722{
d32332aa 4723 return m_internal->GetDataViewSortColumn();
21f47fb9
RR
4724}
4725
f71d3ba4
RR
4726void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4727{
4728 GtkTreeIter iter;
4729 iter.user_data = item.GetID();
0c2a7270 4730 wxGtkTreePath path(m_internal->get_path( &iter ));
f71d3ba4 4731 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
f71d3ba4
RR
4732}
4733
4734void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4735{
4736 GtkTreeIter iter;
4737 iter.user_data = item.GetID();
0c2a7270 4738 wxGtkTreePath path(m_internal->get_path( &iter ));
f71d3ba4 4739 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
739a8399
RR
4740}
4741
4742bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4743{
4744 GtkTreeIter iter;
4745 iter.user_data = item.GetID();
0c2a7270
VZ
4746 wxGtkTreePath path(m_internal->get_path( &iter ));
4747 return gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview), path );
f71d3ba4
RR
4748}
4749
80ce465c
VZ
4750wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4751{
4752 // The tree doesn't have any current item if it hadn't been created yet but
4753 // it's arguably not an error to call this function in this case so just
4754 // return an invalid item without asserting.
4755 if ( !m_treeview )
4756 return wxDataViewItem();
4757
4758 wxGtkTreePath path;
4759 gtk_tree_view_get_cursor(GTK_TREE_VIEW(m_treeview), path.ByRef(), NULL);
4760
4761 return GTKPathToItem(path);
4762}
4763
4764void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4765{
4766 wxCHECK_RET( m_treeview,
4767 "Current item can't be set before creating the control." );
4768
4769 // We need to make sure the model knows about this item or the path would
4770 // be invalid and gtk_tree_view_set_cursor() would silently do nothing.
4771 ExpandAncestors(item);
4772
4773 // We also need to preserve the existing selection from changing.
4774 // Unfortunately the only way to do it seems to use our own selection
4775 // function and forbid any selection changes during set cursor call.
4776 wxGtkTreeSelectionLock
4777 lock(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_treeview)));
4778
4779 // Do move the cursor now.
4780 GtkTreeIter iter;
4781 iter.user_data = item.GetID();
4782 wxGtkTreePath path(m_internal->get_path( &iter ));
4783
4784 gtk_tree_view_set_cursor(GTK_TREE_VIEW(m_treeview), path, NULL, FALSE);
4785}
4786
fbda518c 4787wxDataViewItem wxDataViewCtrl::GetSelection() const
1e08ad10
RR
4788{
4789 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4790
1e08ad10
RR
4791 if (m_windowStyle & wxDV_MULTIPLE)
4792 {
6848478c
RR
4793 // Report the first one
4794 GtkTreeModel *model;
4795 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
b9db5f30 4796
6848478c
RR
4797 if (list)
4798 {
4799 GtkTreePath *path = (GtkTreePath*) list->data;
17d98558 4800 wxDataViewItem item(GTKPathToItem(path));
b9db5f30 4801
6848478c
RR
4802 // delete list
4803 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4804 g_list_free( list );
b9db5f30 4805
17d98558 4806 return item;
6848478c 4807 }
1e08ad10
RR
4808 }
4809 else
4810 {
4811 GtkTreeIter iter;
4812 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
4813 {
17d98558 4814 wxDataViewItem item( iter.user_data );
1e08ad10
RR
4815 return item;
4816 }
4817 }
b9db5f30 4818
1e08ad10
RR
4819 return wxDataViewItem(0);
4820}
4821
718fd180
RR
4822int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4823{
6848478c
RR
4824 sel.Clear();
4825
4826 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4827 if (HasFlag(wxDV_MULTIPLE))
4828 {
4829 GtkTreeModel *model;
4830 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
4831
4832 int count = 0;
4833 while (list)
4834 {
4835 GtkTreePath *path = (GtkTreePath*) list->data;
b9db5f30 4836
17d98558 4837 sel.Add(GTKPathToItem(path));
6848478c
RR
4838
4839 list = g_list_next( list );
4840 count++;
4841 }
4842
4843 // delete list
4844 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
4845 g_list_free( list );
4846
4847 return count;
4848 }
4849 else
4850 {
4851 GtkTreeModel *model;
4852 GtkTreeIter iter;
4853 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
4854 if (has_selection)
4855 {
4856 sel.Add( wxDataViewItem( (void*) iter.user_data) );
4857 return 1;
4858 }
4859 }
4860
718fd180
RR
4861 return 0;
4862}
4863
4864void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4865{
6848478c 4866 GtkDisableSelectionEvents();
b9db5f30 4867
6848478c
RR
4868 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4869
4870 gtk_tree_selection_unselect_all( selection );
b9db5f30 4871
4219d8b0
RR
4872 wxDataViewItem last_parent;
4873
6848478c
RR
4874 size_t i;
4875 for (i = 0; i < sel.GetCount(); i++)
4876 {
4219d8b0
RR
4877 wxDataViewItem item = sel[i];
4878 wxDataViewItem parent = GetModel()->GetParent( item );
4879 if (parent)
4880 {
4881 if (parent != last_parent)
4882 ExpandAncestors(item);
4883 }
4884 last_parent = parent;
4885
6848478c 4886 GtkTreeIter iter;
0e50223f 4887 iter.stamp = m_internal->GetGtkModel()->stamp;
4219d8b0 4888 iter.user_data = (gpointer) item.GetID();
6848478c
RR
4889 gtk_tree_selection_select_iter( selection, &iter );
4890 }
b9db5f30 4891
6848478c 4892 GtkEnableSelectionEvents();
718fd180
RR
4893}
4894
4895void wxDataViewCtrl::Select( const wxDataViewItem & item )
4896{
4219d8b0
RR
4897 ExpandAncestors(item);
4898
6848478c
RR
4899 GtkDisableSelectionEvents();
4900
4901 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4902
4903 GtkTreeIter iter;
0e50223f 4904 iter.stamp = m_internal->GetGtkModel()->stamp;
6848478c
RR
4905 iter.user_data = (gpointer) item.GetID();
4906 gtk_tree_selection_select_iter( selection, &iter );
4907
4908 GtkEnableSelectionEvents();
718fd180
RR
4909}
4910
4911void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
4912{
6848478c
RR
4913 GtkDisableSelectionEvents();
4914
4915 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4916
4917 GtkTreeIter iter;
0e50223f 4918 iter.stamp = m_internal->GetGtkModel()->stamp;
6848478c
RR
4919 iter.user_data = (gpointer) item.GetID();
4920 gtk_tree_selection_unselect_iter( selection, &iter );
4921
4922 GtkEnableSelectionEvents();
718fd180
RR
4923}
4924
4925bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
4926{
6848478c
RR
4927 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
4928
4929 GtkTreeIter iter;
0e50223f 4930 iter.stamp = m_internal->GetGtkModel()->stamp;
6848478c 4931 iter.user_data = (gpointer) item.GetID();
b9db5f30 4932
6848478c 4933 return gtk_tree_selection_iter_is_selected( selection, &iter );
718fd180
RR
4934}
4935
4936void wxDataViewCtrl::SelectAll()
4937{
6848478c 4938 GtkDisableSelectionEvents();
b9db5f30 4939
6848478c 4940 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4941
6848478c 4942 gtk_tree_selection_select_all( selection );
b9db5f30 4943
6848478c 4944 GtkEnableSelectionEvents();
718fd180
RR
4945}
4946
4947void wxDataViewCtrl::UnselectAll()
4948{
6848478c 4949 GtkDisableSelectionEvents();
b9db5f30 4950
6848478c 4951 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4952
6848478c 4953 gtk_tree_selection_unselect_all( selection );
b9db5f30 4954
6848478c 4955 GtkEnableSelectionEvents();
718fd180
RR
4956}
4957
7857346a 4958void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,
4219d8b0 4959 const wxDataViewColumn *WXUNUSED(column))
718fd180 4960{
3f53dd3a 4961 m_ensureVisibleDefered = item;
4219d8b0 4962 ExpandAncestors(item);
a881f34e 4963
6154212e
RR
4964 GtkTreeIter iter;
4965 iter.user_data = (gpointer) item.GetID();
0c2a7270 4966 wxGtkTreePath path(m_internal->get_path( &iter ));
6154212e 4967 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
718fd180
RR
4968}
4969
7857346a
VZ
4970void wxDataViewCtrl::HitTest(const wxPoint& WXUNUSED(point),
4971 wxDataViewItem& item,
4972 wxDataViewColumn *& column) const
66e09788
RR
4973{
4974 item = wxDataViewItem(0);
a87b466d 4975 column = NULL;
66e09788
RR
4976}
4977
7857346a
VZ
4978wxRect
4979wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item),
4980 const wxDataViewColumn *WXUNUSED(column)) const
66e09788
RR
4981{
4982 return wxRect();
4983}
4984
3b6280be
RR
4985void wxDataViewCtrl::DoSetExpanderColumn()
4986{
b9db5f30 4987 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
1b27b2bd 4988 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
3b6280be
RR
4989}
4990
4991void wxDataViewCtrl::DoSetIndent()
4992{
4993}
4994
b086d55f
RR
4995void wxDataViewCtrl::GtkDisableSelectionEvents()
4996{
4997 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
4998 g_signal_handlers_disconnect_by_func( selection,
4999 (gpointer) (wxdataview_selection_changed_callback), this);
b086d55f
RR
5000}
5001
5002void wxDataViewCtrl::GtkEnableSelectionEvents()
5003{
5004 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
5005 g_signal_connect_after (selection, "changed",
5006 G_CALLBACK (wxdataview_selection_changed_callback), this);
b086d55f
RR
5007}
5008
ddb44248
VZ
5009// ----------------------------------------------------------------------------
5010// visual attributes stuff
5011// ----------------------------------------------------------------------------
5012
b94db696
RR
5013// static
5014wxVisualAttributes
5015wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
5016{
5017 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
5018}
5019
ddb44248
VZ
5020void wxDataViewCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
5021{
5022 wxDataViewCtrlBase::DoApplyWidgetStyle(style);
5023 gtk_widget_modify_style(m_treeview, style);
5024}
6ff7eee7 5025
ddb44248 5026#endif // !wxUSE_GENERICDATAVIEWCTRL
790b137e 5027
ddb44248 5028#endif // wxUSE_DATAVIEWCTRL