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