]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dataview.cpp
use wxCHECK instead of wxASSERT in Alloc() to avoid crashing when (trying to) use...
[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/icon.h"
24 #include "wx/list.h"
25 #include "wx/settings.h"
5e6f8927 26 #include "wx/dataobj.h"
c3c62822 27 #include "wx/crt.h"
e4db172a
WS
28#endif
29
790b137e 30#include "wx/stockitem.h"
7ea3a0de
RR
31#include "wx/calctrl.h"
32#include "wx/popupwin.h"
69892729 33#include "wx/listimpl.cpp"
790b137e
RR
34
35#include "wx/gtk/private.h"
888dde65
RR
36#include "wx/gtk/dc.h"
37#include "wx/gtk/dcclient.h"
4d496ecb 38
790b137e 39//-----------------------------------------------------------------------------
790b137e
RR
40//-----------------------------------------------------------------------------
41
b8b7b087
RR
42class wxDataViewCtrlInternal;
43
44wxDataViewCtrlInternal *g_internal = NULL;
3b6280be
RR
45
46class wxGtkTreeModelNode;
ef427989 47
0be79c8a
RR
48extern "C" {
49typedef struct _GtkWxTreeModel GtkWxTreeModel;
50}
51
af110130
RR
52//-----------------------------------------------------------------------------
53// wxDataViewCtrlInternal
54//-----------------------------------------------------------------------------
55
69892729 56WX_DECLARE_LIST(wxDataViewItem, ItemList);
a76c2f37 57WX_DEFINE_LIST(ItemList)
69892729 58
af110130
RR
59class wxDataViewCtrlInternal
60{
61public:
7dea20f6 62 wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model );
af110130 63 ~wxDataViewCtrlInternal();
b9db5f30 64
f6f0ef85 65 // model iface
2056dede 66 GtkTreeModelFlags get_flags();
af110130
RR
67 gboolean get_iter( GtkTreeIter *iter, GtkTreePath *path );
68 GtkTreePath *get_path( GtkTreeIter *iter);
af110130
RR
69 gboolean iter_next( GtkTreeIter *iter );
70 gboolean iter_children( GtkTreeIter *iter, GtkTreeIter *parent);
71 gboolean iter_has_child( GtkTreeIter *iter );
72 gint iter_n_children( GtkTreeIter *iter );
73 gboolean iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n );
74 gboolean iter_parent( GtkTreeIter *iter, GtkTreeIter *child );
b9db5f30 75
f6f0ef85
RR
76 // dnd iface
77 gboolean row_draggable( GtkTreeDragSource *drag_source, GtkTreePath *path );
78 gboolean drag_data_delete( GtkTreeDragSource *drag_source, GtkTreePath* path );
7857346a 79 gboolean drag_data_get( GtkTreeDragSource *drag_source, GtkTreePath *path,
f6f0ef85 80 GtkSelectionData *selection_data );
7857346a 81 gboolean drag_data_received( GtkTreeDragDest *drag_dest, GtkTreePath *dest,
f6f0ef85 82 GtkSelectionData *selection_data );
7857346a 83 gboolean row_drop_possible( GtkTreeDragDest *drag_dest, GtkTreePath *dest_path,
f6f0ef85
RR
84 GtkSelectionData *selection_data );
85
86 // notifactions from wxDataViewModel
af110130 87 bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
469d3e9b 88 bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
d8331a01
RR
89 bool ItemChanged( const wxDataViewItem &item );
90 bool ValueChanged( const wxDataViewItem &item, unsigned int col );
91 bool Cleared();
af110130 92 void Resort();
b9db5f30 93
f6f0ef85 94 // sorting interface
b8b7b087
RR
95 void SetSortOrder( GtkSortType sort_order ) { m_sort_order = sort_order; }
96 GtkSortType GetSortOrder() { return m_sort_order; }
7ee7191c 97
40196b1e
RR
98 void SetSortColumn( int column ) { m_sort_column = column; }
99 int GetSortColumn() { return m_sort_column; }
b9db5f30 100
d32332aa
RR
101 void SetDataViewSortColumn( wxDataViewColumn *column ) { m_dataview_sort_column = column; }
102 wxDataViewColumn *GetDataViewSortColumn() { return m_dataview_sort_column; }
b9db5f30 103
40196b1e 104 bool IsSorted() { return (m_sort_column >= 0); }
b9db5f30 105
f6f0ef85
RR
106 // accessors
107 wxDataViewModel* GetDataViewModel() { return m_wx_model; }
108 wxDataViewCtrl* GetOwner() { return m_owner; }
109 GtkWxTreeModel* GetGtkModel() { return m_gtk_model; }
b9db5f30 110
af110130
RR
111protected:
112 void InitTree();
113 wxGtkTreeModelNode *FindNode( const wxDataViewItem &item );
114 wxGtkTreeModelNode *FindNode( GtkTreeIter *iter );
115 wxGtkTreeModelNode *FindParentNode( const wxDataViewItem &item );
116 wxGtkTreeModelNode *FindParentNode( GtkTreeIter *iter );
117 void BuildBranch( wxGtkTreeModelNode *branch );
b9db5f30 118
af110130
RR
119private:
120 wxGtkTreeModelNode *m_root;
121 wxDataViewModel *m_wx_model;
122 GtkWxTreeModel *m_gtk_model;
123 wxDataViewCtrl *m_owner;
b8b7b087 124 GtkSortType m_sort_order;
d32332aa 125 wxDataViewColumn *m_dataview_sort_column;
40196b1e 126 int m_sort_column;
af110130 127};
ef427989 128
af110130
RR
129
130//-----------------------------------------------------------------------------
131// wxGtkTreeModelNode
132//-----------------------------------------------------------------------------
133
40196b1e
RR
134int LINKAGEMODE wxGtkTreeModelChildCmp( void** id1, void** id2 )
135{
b9db5f30 136 int ret = g_internal->GetDataViewModel()->Compare( *id1, *id2,
40196b1e 137 g_internal->GetSortColumn(), (g_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
b9db5f30 138
40196b1e
RR
139 return ret;
140}
af110130
RR
141
142WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode*, wxGtkTreeModelNodes );
40196b1e 143WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren );
3b6280be
RR
144
145class wxGtkTreeModelNode
146{
147public:
ef427989 148 wxGtkTreeModelNode( wxGtkTreeModelNode* parent, const wxDataViewItem &item,
0be79c8a 149 wxDataViewCtrlInternal *internal )
b9db5f30
VS
150 {
151 m_parent = parent;
d5025dc0 152 m_item = item;
0be79c8a 153 m_internal = internal;
3b6280be 154 }
b9db5f30 155
3b6280be 156 ~wxGtkTreeModelNode()
b9db5f30 157 {
40196b1e 158 size_t count = m_children.GetCount();
3b6280be
RR
159 size_t i;
160 for (i = 0; i < count; i++)
161 {
af110130 162 wxGtkTreeModelNode *child = m_nodes.Item( i );
3b6280be
RR
163 delete child;
164 }
165 }
166
b9db5f30
VS
167 unsigned int AddNode( wxGtkTreeModelNode* child )
168 {
af110130 169 m_nodes.Add( child );
b9db5f30 170
40196b1e 171 void *id = child->GetItem().GetID();
b9db5f30 172
40196b1e 173 m_children.Add( id );
0bd26819
RR
174
175 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
40196b1e
RR
176 {
177 g_internal = m_internal;
178 m_children.Sort( &wxGtkTreeModelChildCmp );
179 return m_children.Index( id );
180 }
0bd26819 181
40196b1e 182 return m_children.GetCount()-1;
af110130 183 }
b9db5f30 184
af110130
RR
185 unsigned int AddLeave( void* id )
186 {
40196b1e 187 m_children.Add( id );
0bd26819
RR
188
189 if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
40196b1e
RR
190 {
191 g_internal = m_internal;
192 m_children.Sort( &wxGtkTreeModelChildCmp );
193 return m_children.Index( id );
194 }
0bd26819 195
40196b1e 196 return m_children.GetCount()-1;
af110130 197 }
b9db5f30 198
af110130
RR
199 void DeleteChild( void* id )
200 {
40196b1e 201 m_children.Remove( id );
b9db5f30 202
40196b1e
RR
203 unsigned int count = m_nodes.GetCount();
204 unsigned int pos;
af110130 205 for (pos = 0; pos < count; pos++)
b9db5f30 206 {
af110130
RR
207 wxGtkTreeModelNode *node = m_nodes.Item( pos );
208 if (node->GetItem().GetID() == id)
209 {
210 m_nodes.RemoveAt( pos );
211 delete node;
212 break;
213 }
214 }
b9db5f30 215
af110130 216 }
b9db5f30
VS
217
218 wxGtkTreeModelNode* GetParent()
3b6280be 219 { return m_parent; }
b9db5f30 220 wxGtkTreeModelNodes &GetNodes()
af110130 221 { return m_nodes; }
b9db5f30 222 wxGtkTreeModelChildren &GetChildren()
40196b1e 223 { return m_children; }
b9db5f30 224
40196b1e 225 unsigned int GetChildCount() { return m_children.GetCount(); }
af110130 226 unsigned int GetNodesCount() { return m_nodes.GetCount(); }
3b6280be
RR
227
228 wxDataViewItem &GetItem() { return m_item; }
0be79c8a 229 wxDataViewCtrlInternal *GetInternal() { return m_internal; }
b9db5f30 230
4508fcd2 231 void Resort();
b9db5f30 232
3b6280be 233private:
0be79c8a 234 wxGtkTreeModelNode *m_parent;
af110130 235 wxGtkTreeModelNodes m_nodes;
40196b1e 236 wxGtkTreeModelChildren m_children;
b9db5f30 237 wxDataViewItem m_item;
0be79c8a 238 wxDataViewCtrlInternal *m_internal;
3b6280be
RR
239};
240
ef427989 241
790b137e
RR
242//-----------------------------------------------------------------------------
243// data
244//-----------------------------------------------------------------------------
245
246extern bool g_blockEventsOnDrag;
247
248//-----------------------------------------------------------------------------
e0062c04 249// define new GTK+ class wxGtkTreeModel
790b137e
RR
250//-----------------------------------------------------------------------------
251
252extern "C" {
253
e0062c04
RR
254#define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ())
255#define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel))
256#define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
257#define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL))
258#define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL))
259#define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass))
790b137e 260
e0062c04 261GType gtk_wx_tree_model_get_type (void);
790b137e 262
e0062c04 263typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass;
790b137e 264
e0062c04 265struct _GtkWxTreeModel
790b137e
RR
266{
267 GObject parent;
268
269 /*< private >*/
270 gint stamp;
55fbde12 271 wxDataViewCtrlInternal *internal;
790b137e
RR
272};
273
e0062c04 274struct _GtkWxTreeModelClass
790b137e 275{
e152afc3 276 GObjectClass list_parent_class;
790b137e
RR
277};
278
e0062c04 279static GtkWxTreeModel *wxgtk_tree_model_new (void);
773cca48
RR
280static void wxgtk_tree_model_init (GtkWxTreeModel *tree_model);
281static void wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass);
85136e3b
RR
282
283static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface);
284static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface);
285static void wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface);
286static void wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface);
287
e0062c04
RR
288static void wxgtk_tree_model_finalize (GObject *object);
289static GtkTreeModelFlags wxgtk_tree_model_get_flags (GtkTreeModel *tree_model);
290static gint wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model);
291static GType wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
93763ad5 292 gint index);
e0062c04 293static gboolean wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
93763ad5
WS
294 GtkTreeIter *iter,
295 GtkTreePath *path);
e0062c04 296static GtkTreePath *wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
93763ad5 297 GtkTreeIter *iter);
e0062c04 298static void wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
93763ad5
WS
299 GtkTreeIter *iter,
300 gint column,
301 GValue *value);
e0062c04 302static gboolean wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
93763ad5 303 GtkTreeIter *iter);
e0062c04 304static gboolean wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
93763ad5
WS
305 GtkTreeIter *iter,
306 GtkTreeIter *parent);
e0062c04 307static gboolean wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
93763ad5 308 GtkTreeIter *iter);
e0062c04 309static gint wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
93763ad5 310 GtkTreeIter *iter);
e0062c04 311static gboolean wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
93763ad5
WS
312 GtkTreeIter *iter,
313 GtkTreeIter *parent,
314 gint n);
e0062c04 315static gboolean wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
93763ad5
WS
316 GtkTreeIter *iter,
317 GtkTreeIter *child);
790b137e 318
773cca48
RR
319/* sortable */
320static gboolean wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
85136e3b
RR
321 gint *sort_column_id,
322 GtkSortType *order);
773cca48 323static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
85136e3b
RR
324 gint sort_column_id,
325 GtkSortType order);
773cca48 326static void wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
85136e3b
RR
327 gint sort_column_id,
328 GtkTreeIterCompareFunc func,
329 gpointer data,
330 GtkDestroyNotify destroy);
773cca48 331static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
85136e3b
RR
332 GtkTreeIterCompareFunc func,
333 gpointer data,
334 GtkDestroyNotify destroy);
773cca48
RR
335static gboolean wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable);
336
85136e3b
RR
337/* drag'n'drop */
338static gboolean wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source,
339 GtkTreePath *path);
340static gboolean wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source,
341 GtkTreePath *path);
342static gboolean wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source,
343 GtkTreePath *path,
344 GtkSelectionData *selection_data);
345static gboolean wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest,
346 GtkTreePath *dest,
347 GtkSelectionData *selection_data);
348static gboolean wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest,
349 GtkTreePath *dest_path,
350 GtkSelectionData *selection_data);
773cca48
RR
351
352
e152afc3 353static GObjectClass *list_parent_class = NULL;
790b137e
RR
354
355GType
e0062c04 356gtk_wx_tree_model_get_type (void)
790b137e 357{
e0062c04 358 static GType tree_model_type = 0;
790b137e 359
e0062c04 360 if (!tree_model_type)
790b137e 361 {
e0062c04 362 const GTypeInfo tree_model_info =
93763ad5 363 {
e0062c04 364 sizeof (GtkWxTreeModelClass),
93763ad5
WS
365 NULL, /* base_init */
366 NULL, /* base_finalize */
e0062c04 367 (GClassInitFunc) wxgtk_tree_model_class_init,
93763ad5
WS
368 NULL, /* class_finalize */
369 NULL, /* class_data */
e0062c04 370 sizeof (GtkWxTreeModel),
93763ad5 371 0,
e0062c04 372 (GInstanceInitFunc) wxgtk_tree_model_init,
93763ad5 373 };
790b137e 374
773cca48
RR
375 static const GInterfaceInfo tree_model_iface_info =
376 {
377 (GInterfaceInitFunc) wxgtk_tree_model_tree_model_init,
378 NULL,
379 NULL
380 };
381
382 static const GInterfaceInfo sortable_iface_info =
383 {
384 (GInterfaceInitFunc) wxgtk_tree_model_sortable_init,
385 NULL,
386 NULL
387 };
790b137e 388
85136e3b
RR
389 static const GInterfaceInfo drag_source_iface_info =
390 {
391 (GInterfaceInitFunc) wxgtk_tree_model_drag_source_init,
392 NULL,
393 NULL
394 };
395
396 static const GInterfaceInfo drag_dest_iface_info =
397 {
398 (GInterfaceInitFunc) wxgtk_tree_model_drag_dest_init,
399 NULL,
400 NULL
401 };
402
773cca48 403 tree_model_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxTreeModel",
e0062c04 404 &tree_model_info, (GTypeFlags)0 );
790b137e 405
773cca48
RR
406 g_type_add_interface_static (tree_model_type,
407 GTK_TYPE_TREE_MODEL,
408 &tree_model_iface_info);
409 g_type_add_interface_static (tree_model_type,
410 GTK_TYPE_TREE_SORTABLE,
411 &sortable_iface_info);
85136e3b
RR
412 g_type_add_interface_static (tree_model_type,
413 GTK_TYPE_TREE_DRAG_DEST,
414 &drag_dest_iface_info);
415 g_type_add_interface_static (tree_model_type,
416 GTK_TYPE_TREE_DRAG_SOURCE,
417 &drag_source_iface_info);
790b137e
RR
418 }
419
773cca48 420 return tree_model_type;
790b137e
RR
421}
422
e0062c04
RR
423static GtkWxTreeModel *
424wxgtk_tree_model_new(void)
1557c77b 425{
e0062c04 426 GtkWxTreeModel *retval = (GtkWxTreeModel *) g_object_new (GTK_TYPE_WX_TREE_MODEL, NULL);
e152afc3 427 return retval;
1557c77b
RR
428}
429
790b137e 430static void
e0062c04 431wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass)
790b137e 432{
e152afc3
RR
433 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
434 GObjectClass *object_class = (GObjectClass*) klass;
e0062c04 435 object_class->finalize = wxgtk_tree_model_finalize;
790b137e
RR
436}
437
438static void
e0062c04
RR
439wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface)
440{
441 iface->get_flags = wxgtk_tree_model_get_flags;
442 iface->get_n_columns = wxgtk_tree_model_get_n_columns;
443 iface->get_column_type = wxgtk_tree_model_get_column_type;
444 iface->get_iter = wxgtk_tree_model_get_iter;
445 iface->get_path = wxgtk_tree_model_get_path;
446 iface->get_value = wxgtk_tree_model_get_value;
447 iface->iter_next = wxgtk_tree_model_iter_next;
448 iface->iter_children = wxgtk_tree_model_iter_children;
449 iface->iter_has_child = wxgtk_tree_model_iter_has_child;
450 iface->iter_n_children = wxgtk_tree_model_iter_n_children;
451 iface->iter_nth_child = wxgtk_tree_model_iter_nth_child;
452 iface->iter_parent = wxgtk_tree_model_iter_parent;
790b137e
RR
453}
454
773cca48
RR
455static void
456wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface)
457{
94b1f7bc
RR
458 iface->get_sort_column_id = wxgtk_tree_model_get_sort_column_id;
459 iface->set_sort_column_id = wxgtk_tree_model_set_sort_column_id;
460 iface->set_sort_func = wxgtk_tree_model_set_sort_func;
461 iface->set_default_sort_func = wxgtk_tree_model_set_default_sort_func;
462 iface->has_default_sort_func = wxgtk_tree_model_has_default_sort_func;
773cca48
RR
463}
464
7857346a 465static void
85136e3b
RR
466wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface)
467{
468 iface->row_draggable = wxgtk_tree_model_row_draggable;
469 iface->drag_data_delete = wxgtk_tree_model_drag_data_delete;
470 iface->drag_data_get = wxgtk_tree_model_drag_data_get;
471}
472
7857346a 473static void
85136e3b
RR
474wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface)
475{
476 iface->drag_data_received = wxgtk_tree_model_drag_data_received;
477 iface->row_drop_possible = wxgtk_tree_model_row_drop_possible;
478}
479
790b137e 480static void
e0062c04 481wxgtk_tree_model_init (GtkWxTreeModel *tree_model)
790b137e 482{
55fbde12 483 tree_model->internal = NULL;
e0062c04 484 tree_model->stamp = g_random_int();
790b137e
RR
485}
486
487static void
e0062c04 488wxgtk_tree_model_finalize (GObject *object)
790b137e 489{
790b137e 490 /* must chain up */
e152afc3 491 (* list_parent_class->finalize) (object);
790b137e 492}
93763ad5 493
790b137e
RR
494} // extern "C"
495
496//-----------------------------------------------------------------------------
e0062c04
RR
497// implement callbacks from wxGtkTreeModel class by letting
498// them call the methods of wxWidgets' wxDataViewModel
790b137e
RR
499//-----------------------------------------------------------------------------
500
501static GtkTreeModelFlags
e0062c04 502wxgtk_tree_model_get_flags (GtkTreeModel *tree_model)
790b137e 503{
2056dede
RR
504 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
505 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), (GtkTreeModelFlags)0 );
790b137e 506
2056dede 507 return wxtree_model->internal->get_flags();
790b137e
RR
508}
509
510static gint
e0062c04 511wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model)
790b137e 512{
e0062c04
RR
513 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
514 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), 0);
790b137e 515
55fbde12 516 return wxtree_model->internal->GetDataViewModel()->GetColumnCount();
790b137e
RR
517}
518
519static GType
e0062c04 520wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model,
93763ad5 521 gint index)
790b137e 522{
e0062c04
RR
523 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
524 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), G_TYPE_INVALID);
790b137e 525
1557c77b 526 GType gtype = G_TYPE_INVALID;
93763ad5 527
55fbde12 528 wxString wxtype = wxtree_model->internal->GetDataViewModel()->GetColumnType( (unsigned int) index );
93763ad5 529
1557c77b
RR
530 if (wxtype == wxT("string"))
531 gtype = G_TYPE_STRING;
72a3ac9b
VZ
532 else
533 {
e94d0c1e
RR
534 gtype = G_TYPE_STRING;
535 // wxFAIL_MSG( _T("non-string columns not supported yet") );
72a3ac9b 536 }
790b137e
RR
537
538 return gtype;
539}
540
541static gboolean
e0062c04 542wxgtk_tree_model_get_iter (GtkTreeModel *tree_model,
93763ad5
WS
543 GtkTreeIter *iter,
544 GtkTreePath *path)
790b137e 545{
e0062c04
RR
546 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
547 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
790b137e
RR
548 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
549
55fbde12 550 return wxtree_model->internal->get_iter( iter, path );
790b137e
RR
551}
552
553static GtkTreePath *
e0062c04 554wxgtk_tree_model_get_path (GtkTreeModel *tree_model,
93763ad5 555 GtkTreeIter *iter)
790b137e 556{
e0062c04
RR
557 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
558 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), NULL);
559 g_return_val_if_fail (iter->stamp == GTK_WX_TREE_MODEL (wxtree_model)->stamp, NULL);
93763ad5 560
55fbde12 561 return wxtree_model->internal->get_path( iter );
790b137e
RR
562}
563
564static void
e0062c04 565wxgtk_tree_model_get_value (GtkTreeModel *tree_model,
93763ad5
WS
566 GtkTreeIter *iter,
567 gint column,
568 GValue *value)
790b137e 569{
e0062c04
RR
570 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
571 g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model) );
239eaa41 572
55fbde12 573 wxDataViewModel *model = wxtree_model->internal->GetDataViewModel();
9861f022 574 wxString mtype = model->GetColumnType( (unsigned int) column );
1557c77b
RR
575 if (mtype == wxT("string"))
576 {
3f3af7e7 577 wxVariant variant;
1557c77b 578 g_value_init( value, G_TYPE_STRING );
9d52aad3 579 wxDataViewItem item( (void*) iter->user_data );
e0062c04 580 model->GetValue( variant, item, (unsigned int) column );
72a3ac9b 581
e0062c04 582 g_value_set_string( value, variant.GetString().utf8_str() );
1557c77b
RR
583 }
584 else
585 {
72a3ac9b 586 wxFAIL_MSG( _T("non-string columns not supported yet") );
1557c77b 587 }
790b137e
RR
588}
589
590static gboolean
e0062c04 591wxgtk_tree_model_iter_next (GtkTreeModel *tree_model,
93763ad5 592 GtkTreeIter *iter)
790b137e 593{
e0062c04 594 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
7857346a 595
33ba5a05
RR
596 if (wxtree_model->stamp != iter->stamp)
597 wxPrintf( "crash\n" );
598
e0062c04 599 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
e0062c04 600 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
7857346a 601
55fbde12 602 return wxtree_model->internal->iter_next( iter );
790b137e
RR
603}
604
605static gboolean
e0062c04 606wxgtk_tree_model_iter_children (GtkTreeModel *tree_model,
93763ad5
WS
607 GtkTreeIter *iter,
608 GtkTreeIter *parent)
790b137e 609{
e0062c04
RR
610 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
611 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
e0062c04 612 g_return_val_if_fail (wxtree_model->stamp == parent->stamp, FALSE);
790b137e 613
55fbde12 614 return wxtree_model->internal->iter_children( iter, parent );
790b137e
RR
615}
616
617static gboolean
e0062c04 618wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model,
93763ad5 619 GtkTreeIter *iter)
790b137e 620{
e0062c04
RR
621 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
622 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
e0062c04 623 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE);
d5025dc0 624
55fbde12 625 return wxtree_model->internal->iter_has_child( iter );
790b137e
RR
626}
627
628static gint
e0062c04 629wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model,
93763ad5 630 GtkTreeIter *iter)
790b137e 631{
e0062c04
RR
632 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
633 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
e0062c04 634 g_return_val_if_fail (wxtree_model->stamp == iter->stamp, 0);
d5025dc0 635
55fbde12 636 return wxtree_model->internal->iter_n_children( iter );
790b137e
RR
637}
638
639static gboolean
e0062c04 640wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model,
93763ad5
WS
641 GtkTreeIter *iter,
642 GtkTreeIter *parent,
643 gint n)
790b137e 644{
e0062c04
RR
645 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
646 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
647
55fbde12 648 return wxtree_model->internal->iter_nth_child( iter, parent, n );
790b137e
RR
649}
650
651static gboolean
e0062c04 652wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model,
93763ad5
WS
653 GtkTreeIter *iter,
654 GtkTreeIter *child)
790b137e 655{
e0062c04
RR
656 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model;
657 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
e0062c04 658 g_return_val_if_fail (wxtree_model->stamp == child->stamp, FALSE);
b9db5f30 659
55fbde12 660 return wxtree_model->internal->iter_parent( iter, child );
790b137e
RR
661}
662
85136e3b 663/* drag'n'drop iface */
7857346a 664static gboolean
85136e3b
RR
665wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source,
666 GtkTreePath *path)
667{
f6f0ef85
RR
668 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
669 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 670
f6f0ef85 671 return wxtree_model->internal->row_draggable( drag_source, path );
85136e3b
RR
672}
673
7857346a 674static gboolean
85136e3b
RR
675wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source,
676 GtkTreePath *path)
677{
f6f0ef85
RR
678 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
679 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 680
f6f0ef85 681 return wxtree_model->internal->drag_data_delete( drag_source, path );
85136e3b
RR
682}
683
7857346a 684static gboolean
85136e3b
RR
685wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source,
686 GtkTreePath *path,
687 GtkSelectionData *selection_data)
688{
f6f0ef85
RR
689 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source;
690 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 691
f6f0ef85 692#if 0
85136e3b 693 wxPrintf( "drag_get_data\n");
7857346a 694
f6f0ef85
RR
695 wxGtkString atom_selection(gdk_atom_name(selection_data->selection));
696 wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection) );
7857346a 697
f6f0ef85
RR
698 wxGtkString atom_target(gdk_atom_name(selection_data->target));
699 wxPrintf( "target %s\n", wxString::FromAscii(atom_target) );
7857346a 700
f6f0ef85
RR
701 wxGtkString atom_type(gdk_atom_name(selection_data->type));
702 wxPrintf( "type %s\n", wxString::FromAscii(atom_type) );
703
704 wxPrintf( "format %d\n", selection_data->format );
705#endif
7857346a 706
f6f0ef85 707 return wxtree_model->internal->drag_data_get( drag_source, path, selection_data );
85136e3b
RR
708}
709
7857346a 710static gboolean
85136e3b
RR
711wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest,
712 GtkTreePath *dest,
713 GtkSelectionData *selection_data)
714{
f6f0ef85
RR
715 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest;
716 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 717
f6f0ef85 718 return wxtree_model->internal->drag_data_received( drag_dest, dest, selection_data );
85136e3b
RR
719}
720
7857346a 721static gboolean
85136e3b
RR
722wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest,
723 GtkTreePath *dest_path,
724 GtkSelectionData *selection_data)
725{
f6f0ef85
RR
726 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest;
727 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE);
7857346a 728
f6f0ef85 729 return wxtree_model->internal->row_drop_possible( drag_dest, dest_path, selection_data );
85136e3b
RR
730}
731
732/* sortable iface */
7857346a 733static gboolean
85136e3b
RR
734wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable,
735 gint *sort_column_id,
736 GtkSortType *order)
773cca48 737{
f6f0ef85 738 GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) sortable;
94b1f7bc
RR
739
740 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE);
741
f6f0ef85 742 if (!wxtree_model->internal->IsSorted())
40196b1e
RR
743 {
744 if (sort_column_id)
745 *sort_column_id = -1;
b9db5f30 746
40196b1e
RR
747 return TRUE;
748 }
749
750
94b1f7bc 751 if (sort_column_id)
f6f0ef85 752 *sort_column_id = wxtree_model->internal->GetSortColumn();
b9db5f30 753
94b1f7bc 754 if (order)
f6f0ef85 755 *order = wxtree_model->internal->GetSortOrder();
b9db5f30 756
94b1f7bc 757 return TRUE;
773cca48
RR
758}
759
d32332aa
RR
760wxDataViewColumn *gs_lastLeftClickHeader = NULL;
761
7857346a 762static void
85136e3b
RR
763wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable,
764 gint sort_column_id,
765 GtkSortType order)
773cca48 766{
94b1f7bc 767 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) sortable;
94b1f7bc
RR
768 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
769
d32332aa 770 tree_model->internal->SetDataViewSortColumn( gs_lastLeftClickHeader );
b9db5f30 771
d32332aa
RR
772 if ((sort_column_id != (gint) tree_model->internal->GetSortColumn()) ||
773 (order != tree_model->internal->GetSortOrder()))
774 {
775 tree_model->internal->SetSortColumn( sort_column_id );
776 tree_model->internal->SetSortOrder( order );
b9db5f30 777
d32332aa 778 gtk_tree_sortable_sort_column_changed (sortable);
b9db5f30 779
d32332aa
RR
780 tree_model->internal->GetDataViewModel()->Resort();
781 }
a84c5b6f 782
d32332aa
RR
783 if (gs_lastLeftClickHeader)
784 {
b9db5f30 785 wxDataViewCtrl *dv = tree_model->internal->GetOwner();
d32332aa
RR
786 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, dv->GetId() );
787 event.SetDataViewColumn( gs_lastLeftClickHeader );
788 event.SetModel( dv->GetModel() );
937013e0 789 dv->HandleWindowEvent( event );
d32332aa 790 }
b9db5f30 791
d32332aa 792 gs_lastLeftClickHeader = NULL;
773cca48
RR
793}
794
7857346a 795static void
85136e3b
RR
796wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable,
797 gint WXUNUSED(sort_column_id),
798 GtkTreeIterCompareFunc func,
799 gpointer WXUNUSED(data),
800 GtkDestroyNotify WXUNUSED(destroy) )
773cca48 801{
94b1f7bc
RR
802 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
803 g_return_if_fail (func != NULL);
773cca48
RR
804}
805
c5c5395b
RR
806void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable,
807 GtkTreeIterCompareFunc func,
808 gpointer WXUNUSED(data),
809 GtkDestroyNotify WXUNUSED(destroy) )
773cca48 810{
94b1f7bc 811 g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) );
94b1f7bc
RR
812 g_return_if_fail (func != NULL);
813
94b1f7bc 814 wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" );
773cca48
RR
815}
816
817gboolean wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable)
818{
c5c5395b 819 g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE );
7857346a 820
773cca48
RR
821 return FALSE;
822}
823
e152afc3 824//-----------------------------------------------------------------------------
baa9ebc4 825// define new GTK+ class wxGtkRendererRenderer
e152afc3
RR
826//-----------------------------------------------------------------------------
827
828extern "C" {
829
830#define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
831#define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
832#define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
833#define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
834#define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
835#define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
836
837GType gtk_wx_cell_renderer_get_type (void);
838
839typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
840typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
841
842struct _GtkWxCellRenderer
843{
844 GtkCellRenderer parent;
845
846 /*< private >*/
baa9ebc4 847 wxDataViewCustomRenderer *cell;
4d496ecb 848 guint32 last_click;
e152afc3
RR
849};
850
851struct _GtkWxCellRendererClass
852{
853 GtkCellRendererClass cell_parent_class;
e152afc3
RR
854};
855
856
857static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
553f7d8f
RR
858static void gtk_wx_cell_renderer_init (
859 GtkWxCellRenderer *cell );
860static void gtk_wx_cell_renderer_class_init(
861 GtkWxCellRendererClass *klass );
862static void gtk_wx_cell_renderer_finalize (
863 GObject *object );
864static void gtk_wx_cell_renderer_get_size (
865 GtkCellRenderer *cell,
93763ad5
WS
866 GtkWidget *widget,
867 GdkRectangle *rectangle,
868 gint *x_offset,
869 gint *y_offset,
870 gint *width,
871 gint *height );
553f7d8f
RR
872static void gtk_wx_cell_renderer_render (
873 GtkCellRenderer *cell,
93763ad5
WS
874 GdkWindow *window,
875 GtkWidget *widget,
876 GdkRectangle *background_area,
877 GdkRectangle *cell_area,
878 GdkRectangle *expose_area,
879 GtkCellRendererState flags );
553f7d8f
RR
880static gboolean gtk_wx_cell_renderer_activate(
881 GtkCellRenderer *cell,
882 GdkEvent *event,
883 GtkWidget *widget,
884 const gchar *path,
885 GdkRectangle *background_area,
886 GdkRectangle *cell_area,
887 GtkCellRendererState flags );
1e510b1e
RR
888static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
889 GtkCellRenderer *cell,
890 GdkEvent *event,
891 GtkWidget *widget,
892 const gchar *path,
893 GdkRectangle *background_area,
894 GdkRectangle *cell_area,
895 GtkCellRendererState flags );
e8375af8 896
e152afc3
RR
897
898static GObjectClass *cell_parent_class = NULL;
899
900} // extern "C"
901
93763ad5 902GType
e152afc3
RR
903gtk_wx_cell_renderer_get_type (void)
904{
553f7d8f 905 static GType cell_wx_type = 0;
e152afc3 906
553f7d8f 907 if (!cell_wx_type)
e152afc3 908 {
de4a74e2 909 const GTypeInfo cell_wx_info =
553f7d8f
RR
910 {
911 sizeof (GtkWxCellRendererClass),
93763ad5
WS
912 NULL, /* base_init */
913 NULL, /* base_finalize */
553f7d8f 914 (GClassInitFunc) gtk_wx_cell_renderer_class_init,
93763ad5
WS
915 NULL, /* class_finalize */
916 NULL, /* class_data */
553f7d8f
RR
917 sizeof (GtkWxCellRenderer),
918 0, /* n_preallocs */
919 (GInstanceInitFunc) gtk_wx_cell_renderer_init,
920 };
921
93763ad5 922 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER,
553f7d8f 923 "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 );
e152afc3
RR
924 }
925
553f7d8f 926 return cell_wx_type;
e152afc3
RR
927}
928
929static void
930gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
931{
932 cell->cell = NULL;
4d496ecb 933 cell->last_click = 0;
e152afc3
RR
934}
935
936static void
937gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
938{
939 GObjectClass *object_class = G_OBJECT_CLASS (klass);
940 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
941
942 cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
943
944 object_class->finalize = gtk_wx_cell_renderer_finalize;
945
946 cell_class->get_size = gtk_wx_cell_renderer_get_size;
947 cell_class->render = gtk_wx_cell_renderer_render;
553f7d8f 948 cell_class->activate = gtk_wx_cell_renderer_activate;
1e510b1e 949 cell_class->start_editing = gtk_wx_cell_renderer_start_editing;
e152afc3
RR
950}
951
952static void
953gtk_wx_cell_renderer_finalize (GObject *object)
954{
955 /* must chain up */
956 (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object);
957}
958
959GtkCellRenderer*
960gtk_wx_cell_renderer_new (void)
961{
962 return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
963}
964
1e510b1e
RR
965
966
967static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
968 GtkCellRenderer *renderer,
c5c5395b 969 GdkEvent *WXUNUSED(event),
1e510b1e
RR
970 GtkWidget *widget,
971 const gchar *path,
c5c5395b 972 GdkRectangle *WXUNUSED(background_area),
1e510b1e 973 GdkRectangle *cell_area,
c5c5395b 974 GtkCellRendererState WXUNUSED(flags) )
1e510b1e
RR
975{
976 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
977 wxDataViewCustomRenderer *cell = wxrenderer->cell;
978 if (!cell->HasEditorCtrl())
979 return NULL;
e8375af8 980
1e510b1e
RR
981 GdkRectangle rect;
982 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
983 &rect.x,
984 &rect.y,
985 &rect.width,
986 &rect.height);
987
988 rect.x += cell_area->x;
989 rect.y += cell_area->y;
990// rect.width -= renderer->xpad * 2;
991// rect.height -= renderer->ypad * 2;
992
993// wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
994 wxRect renderrect( cell_area->x, cell_area->y, cell_area->width, cell_area->height );
995
1e510b1e 996 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
55fbde12
RR
997 GtkTreeIter iter;
998 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, treepath );
999 wxDataViewItem item( (void*) iter.user_data );
1e510b1e
RR
1000 gtk_tree_path_free( treepath );
1001
9d52aad3 1002 cell->StartEditing( item, renderrect );
30715fa1 1003
1e510b1e
RR
1004 return NULL;
1005}
1006
e152afc3
RR
1007static void
1008gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
c5c5395b 1009 GtkWidget *WXUNUSED(widget),
93763ad5
WS
1010 GdkRectangle *cell_area,
1011 gint *x_offset,
1012 gint *y_offset,
1013 gint *width,
1014 gint *height)
e152afc3
RR
1015{
1016 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
baa9ebc4 1017 wxDataViewCustomRenderer *cell = wxrenderer->cell;
93763ad5 1018
e152afc3
RR
1019 wxSize size = cell->GetSize();
1020
1021 gint calc_width = (gint) renderer->xpad * 2 + size.x;
1022 gint calc_height = (gint) renderer->ypad * 2 + size.y;
93763ad5
WS
1023
1024 if (x_offset)
e152afc3 1025 *x_offset = 0;
93763ad5 1026 if (y_offset)
e152afc3
RR
1027 *y_offset = 0;
1028
1029 if (cell_area && size.x > 0 && size.y > 0)
1030 {
1031 if (x_offset)
93763ad5 1032 {
e152afc3
RR
1033 *x_offset = (gint)((renderer->xalign *
1034 (cell_area->width - calc_width - 2 * renderer->xpad)));
1035 *x_offset = MAX (*x_offset, 0) + renderer->xpad;
93763ad5 1036 }
e152afc3
RR
1037 if (y_offset)
1038 {
1039 *y_offset = (gint)((renderer->yalign *
1040 (cell_area->height - calc_height - 2 * renderer->ypad)));
1041 *y_offset = MAX (*y_offset, 0) + renderer->ypad;
1042 }
1043 }
1044
1045 if (width)
1046 *width = calc_width;
93763ad5 1047
e152afc3
RR
1048 if (height)
1049 *height = calc_height;
1050}
1051
1052static void
1053gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
93763ad5
WS
1054 GdkWindow *window,
1055 GtkWidget *widget,
1056 GdkRectangle *background_area,
1057 GdkRectangle *cell_area,
1058 GdkRectangle *expose_area,
1059 GtkCellRendererState flags)
e152afc3
RR
1060
1061{
1062 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
baa9ebc4 1063 wxDataViewCustomRenderer *cell = wxrenderer->cell;
93763ad5 1064
f69c03de
RR
1065 cell->window = window;
1066 cell->widget = widget;
1067 cell->background_area = (void*) background_area;
1068 cell->cell_area = (void*) cell_area;
1069 cell->expose_area = (void*) expose_area;
1070 cell->flags = (int) flags;
1071
e152afc3
RR
1072 GdkRectangle rect;
1073 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
93763ad5
WS
1074 &rect.x,
1075 &rect.y,
1076 &rect.width,
1077 &rect.height);
e152afc3
RR
1078
1079 rect.x += cell_area->x;
1080 rect.y += cell_area->y;
1081 rect.width -= renderer->xpad * 2;
1082 rect.height -= renderer->ypad * 2;
93763ad5 1083
e152afc3
RR
1084 GdkRectangle dummy;
1085 if (gdk_rectangle_intersect (expose_area, &rect, &dummy))
1086 {
1087 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
1088 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
888dde65
RR
1089 wxWindowDCImpl *impl = (wxWindowDCImpl *) dc->GetImpl();
1090 if (impl->m_gdkwindow == NULL)
4f37154e 1091 {
888dde65
RR
1092 impl->m_gdkwindow = window;
1093 impl->SetUpDC();
4f37154e 1094 }
93763ad5 1095
e152afc3
RR
1096 int state = 0;
1097 if (flags & GTK_CELL_RENDERER_SELECTED)
1098 state |= wxDATAVIEW_CELL_SELECTED;
1099 if (flags & GTK_CELL_RENDERER_PRELIT)
1100 state |= wxDATAVIEW_CELL_PRELIT;
1101 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1102 state |= wxDATAVIEW_CELL_INSENSITIVE;
1103 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
1104 state |= wxDATAVIEW_CELL_INSENSITIVE;
1105 if (flags & GTK_CELL_RENDERER_FOCUSED)
1106 state |= wxDATAVIEW_CELL_FOCUSED;
1107 cell->Render( renderrect, dc, state );
93763ad5 1108 }
e152afc3
RR
1109}
1110
93763ad5 1111static gboolean
553f7d8f
RR
1112gtk_wx_cell_renderer_activate(
1113 GtkCellRenderer *renderer,
1114 GdkEvent *event,
1115 GtkWidget *widget,
1116 const gchar *path,
c5c5395b 1117 GdkRectangle *WXUNUSED(background_area),
553f7d8f 1118 GdkRectangle *cell_area,
c5c5395b 1119 GtkCellRendererState WXUNUSED(flags) )
553f7d8f
RR
1120{
1121 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
baa9ebc4 1122 wxDataViewCustomRenderer *cell = wxrenderer->cell;
93763ad5 1123
553f7d8f
RR
1124 GdkRectangle rect;
1125 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
93763ad5
WS
1126 &rect.x,
1127 &rect.y,
1128 &rect.width,
1129 &rect.height);
553f7d8f
RR
1130
1131 rect.x += cell_area->x;
1132 rect.y += cell_area->y;
1133 rect.width -= renderer->xpad * 2;
1134 rect.height -= renderer->ypad * 2;
93763ad5 1135
553f7d8f 1136 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
93763ad5 1137
e0062c04 1138 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 1139
4d496ecb 1140 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
71c6b455
RR
1141
1142 GtkTreeIter iter;
f69c03de 1143 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, treepath );
71c6b455 1144 wxDataViewItem item( iter.user_data );
4d496ecb 1145 gtk_tree_path_free( treepath );
93763ad5 1146
0a71f9e9 1147 unsigned int model_col = cell->GetOwner()->GetModelColumn();
93763ad5 1148
456e5c21
RR
1149 if (!event)
1150 {
1151 bool ret = false;
e8375af8 1152
456e5c21 1153 // activated by <ENTER>
9d52aad3 1154 if (cell->Activate( renderrect, model, item, model_col ))
456e5c21 1155 ret = true;
e8375af8 1156
456e5c21
RR
1157 return ret;
1158 }
1159 else if (event->type == GDK_BUTTON_PRESS)
4d496ecb
RR
1160 {
1161 GdkEventButton *button_event = (GdkEventButton*) event;
93763ad5 1162 wxPoint pt( ((int) button_event->x) - renderrect.x,
4d496ecb 1163 ((int) button_event->y) - renderrect.y );
93763ad5 1164
4d496ecb
RR
1165 bool ret = false;
1166 if (button_event->button == 1)
1167 {
9d52aad3 1168 if (cell->LeftClick( pt, renderrect, model, item, model_col ))
4d496ecb 1169 ret = true;
7ea3a0de 1170 // TODO: query system double-click time
4d496ecb 1171 if (button_event->time - wxrenderer->last_click < 400)
9d52aad3 1172 if (cell->Activate( renderrect, model, item, model_col ))
4d496ecb
RR
1173 ret = true;
1174 }
4d496ecb 1175 wxrenderer->last_click = button_event->time;
93763ad5 1176
4d496ecb
RR
1177 return ret;
1178 }
93763ad5 1179
4d496ecb 1180 return false;
553f7d8f
RR
1181}
1182
93763ad5 1183// ---------------------------------------------------------
e0062c04 1184// wxGtkDataViewModelNotifier
93763ad5 1185// ---------------------------------------------------------
6e2e590f 1186
e0062c04 1187class wxGtkDataViewModelNotifier: public wxDataViewModelNotifier
6e2e590f
RR
1188{
1189public:
e0062c04
RR
1190 wxGtkDataViewModelNotifier( GtkWxTreeModel *wxgtk_model,
1191 wxDataViewModel *wx_model,
1192 wxDataViewCtrl *ctrl );
1193 ~wxGtkDataViewModelNotifier();
1194
1195 virtual bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
469d3e9b 1196 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
e0062c04
RR
1197 virtual bool ItemChanged( const wxDataViewItem &item );
1198 virtual bool ValueChanged( const wxDataViewItem &item, unsigned int col );
6e2e590f 1199 virtual bool Cleared();
4508fcd2 1200 virtual void Resort();
93763ad5 1201
33ba5a05
RR
1202 void SetGtkModel( GtkWxTreeModel *model ) { m_wxgtk_model = model; }
1203
1204private:
e0062c04
RR
1205 GtkWxTreeModel *m_wxgtk_model;
1206 wxDataViewModel *m_wx_model;
63415a42 1207 wxDataViewCtrl *m_owner;
6e2e590f
RR
1208};
1209
93763ad5 1210// ---------------------------------------------------------
6e2e590f 1211// wxGtkDataViewListModelNotifier
93763ad5 1212// ---------------------------------------------------------
6e2e590f 1213
e0062c04
RR
1214wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1215 GtkWxTreeModel* wxgtk_model, wxDataViewModel *wx_model,
63415a42 1216 wxDataViewCtrl *ctrl )
6e2e590f 1217{
e0062c04 1218 m_wxgtk_model = wxgtk_model;
6e2e590f 1219 m_wx_model = wx_model;
63415a42 1220 m_owner = ctrl;
6e2e590f 1221}
93763ad5 1222
e0062c04 1223wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
f7ed8c89
RR
1224{
1225 m_wx_model = NULL;
e0062c04 1226 m_wxgtk_model = NULL;
f7ed8c89
RR
1227}
1228
e0062c04 1229bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
6e2e590f 1230{
55fbde12 1231 m_owner->GtkGetInternal()->ItemAdded( parent, item );
1e08ad10 1232
6e2e590f 1233 GtkTreeIter iter;
e0062c04
RR
1234 iter.stamp = m_wxgtk_model->stamp;
1235 iter.user_data = (gpointer) item.GetID();
93763ad5 1236
b9db5f30 1237 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04 1238 GTK_TREE_MODEL(m_wxgtk_model), &iter );
b9db5f30 1239 gtk_tree_model_row_inserted(
e0062c04 1240 GTK_TREE_MODEL(m_wxgtk_model), path, &iter);
6e2e590f 1241 gtk_tree_path_free (path);
93763ad5 1242
6e2e590f
RR
1243 return true;
1244}
1245
469d3e9b 1246bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
6e2e590f 1247{
605c2c4a 1248 GtkTreeIter iter;
e0062c04
RR
1249 iter.stamp = m_wxgtk_model->stamp;
1250 iter.user_data = (gpointer) item.GetID();
93763ad5 1251
b9db5f30 1252 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04
RR
1253 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1254 gtk_tree_model_row_deleted(
1255 GTK_TREE_MODEL(m_wxgtk_model), path );
605c2c4a 1256 gtk_tree_path_free (path);
93763ad5 1257
469d3e9b 1258 m_owner->GtkGetInternal()->ItemDeleted( parent, item );
b9db5f30 1259
605c2c4a 1260 return true;
6e2e590f
RR
1261}
1262
4508fcd2
RR
1263void wxGtkDataViewModelNotifier::Resort()
1264{
1265 m_owner->GtkGetInternal()->Resort();
1266}
1267
e0062c04 1268bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem &item )
6e2e590f 1269{
4627af27 1270 GtkTreeIter iter;
e0062c04
RR
1271 iter.stamp = m_wxgtk_model->stamp;
1272 iter.user_data = (gpointer) item.GetID();
6e2e590f 1273
b9db5f30 1274 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04
RR
1275 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1276 gtk_tree_model_row_changed(
1277 GTK_TREE_MODEL(m_wxgtk_model), path, &iter );
a7f61f76
RR
1278 gtk_tree_path_free (path);
1279
d8331a01 1280 m_owner->GtkGetInternal()->ItemChanged( item );
b9db5f30 1281
a7f61f76 1282 return true;
6e2e590f
RR
1283}
1284
e0062c04 1285bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem &item, unsigned int model_col )
6e2e590f 1286{
4eccd3a1 1287 // This adds GTK+'s missing MVC logic for ValueChanged
63415a42
RR
1288 unsigned int index;
1289 for (index = 0; index < m_owner->GetColumnCount(); index++)
8f850e28 1290 {
63415a42
RR
1291 wxDataViewColumn *column = m_owner->GetColumn( index );
1292 if (column->GetModelColumn() == model_col)
8f850e28 1293 {
63415a42
RR
1294 GtkTreeView *widget = GTK_TREE_VIEW(m_owner->m_treeview);
1295 GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
8f850e28
RR
1296
1297 // Get cell area
e0062c04
RR
1298 GtkTreeIter iter;
1299 iter.stamp = m_wxgtk_model->stamp;
1300 iter.user_data = (gpointer) item.GetID();
b9db5f30 1301 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04 1302 GTK_TREE_MODEL(m_wxgtk_model), &iter );
8f850e28 1303 GdkRectangle cell_area;
63415a42 1304 gtk_tree_view_get_cell_area( widget, path, gcolumn, &cell_area );
93763ad5 1305 gtk_tree_path_free( path );
8f850e28 1306
1a64259d
RR
1307 GtkAdjustment* hadjust = gtk_tree_view_get_hadjustment( widget );
1308 double d = gtk_adjustment_get_value( hadjust );
1309 int xdiff = (int) d;
1310
63415a42 1311 int ydiff = gcolumn->button->allocation.height;
8f850e28 1312 // Redraw
93763ad5 1313 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
1a64259d 1314 cell_area.x - xdiff, ydiff + cell_area.y, cell_area.width, cell_area.height );
b9db5f30 1315
d8331a01 1316 m_owner->GtkGetInternal()->ValueChanged( item, model_col );
b9db5f30 1317
d8331a01 1318 return true;
8f850e28 1319 }
8f850e28 1320 }
93763ad5 1321
d8331a01 1322 return false;
6e2e590f
RR
1323}
1324
e0062c04 1325bool wxGtkDataViewModelNotifier::Cleared()
6e2e590f 1326{
33ba5a05 1327 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->m_treeview), NULL );
7857346a 1328
33ba5a05 1329 // this will create a new GTK model
d8331a01 1330 m_owner->GtkGetInternal()->Cleared();
7857346a 1331
33ba5a05 1332 SetGtkModel( m_owner->GtkGetInternal()->GetGtkModel() );
b9db5f30 1333
33ba5a05 1334 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->m_treeview), GTK_TREE_MODEL(m_wxgtk_model) );
7857346a 1335
6e2e590f
RR
1336 return false;
1337}
1338
93763ad5 1339// ---------------------------------------------------------
baa9ebc4 1340// wxDataViewRenderer
93763ad5 1341// ---------------------------------------------------------
6842a71a 1342
a912e81f
RR
1343static gpointer s_user_data = NULL;
1344
1345static void
c5c5395b 1346wxgtk_cell_editable_editing_done( GtkCellEditable *WXUNUSED(editable),
a912e81f
RR
1347 wxDataViewRenderer *wxrenderer )
1348{
1349 wxDataViewColumn *column = wxrenderer->GetOwner();
1350 wxDataViewCtrl *dv = column->GetOwner();
1351 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv->GetId() );
1352 event.SetDataViewColumn( column );
1353 event.SetModel( dv->GetModel() );
1354 wxDataViewItem item( s_user_data );
1355 event.SetItem( item );
937013e0 1356 dv->HandleWindowEvent( event );
a912e81f
RR
1357}
1358
b9db5f30 1359static void
c5c5395b 1360wxgtk_renderer_editing_started( GtkCellRenderer *WXUNUSED(cell), GtkCellEditable *editable,
a912e81f
RR
1361 gchar *path, wxDataViewRenderer *wxrenderer )
1362{
1363 wxDataViewColumn *column = wxrenderer->GetOwner();
1364 wxDataViewCtrl *dv = column->GetOwner();
1365 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv->GetId() );
1366 event.SetDataViewColumn( column );
1367 event.SetModel( dv->GetModel() );
1368 GtkTreePath *tree_path = gtk_tree_path_new_from_string( path );
1369 GtkTreeIter iter;
1370 dv->GtkGetInternal()->get_iter( &iter, tree_path );
1371 gtk_tree_path_free( tree_path );
1372 wxDataViewItem item( iter.user_data );
1373 event.SetItem( item );
937013e0 1374 dv->HandleWindowEvent( event );
a912e81f
RR
1375
1376 if (GTK_IS_CELL_EDITABLE(editable))
1377 {
1378 s_user_data = iter.user_data;
b9db5f30 1379
a912e81f
RR
1380 g_signal_connect (GTK_CELL_EDITABLE (editable), "editing_done",
1381 G_CALLBACK (wxgtk_cell_editable_editing_done),
1382 (gpointer) wxrenderer );
b9db5f30 1383
a912e81f
RR
1384 }
1385}
1386
1387
baa9ebc4 1388IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
6842a71a 1389
9861f022
RR
1390wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1391 int align ) :
1392 wxDataViewRendererBase( varianttype, mode, align )
6842a71a
RR
1393{
1394 m_renderer = NULL;
9861f022
RR
1395
1396 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1397 // after the m_renderer pointer has been initialized
1398}
1399
a912e81f
RR
1400void wxDataViewRenderer::GtkInitHandlers()
1401{
1402 if (!gtk_check_version(2,6,0))
1403 {
1404 g_signal_connect (GTK_CELL_RENDERER(m_renderer), "editing_started",
1405 G_CALLBACK (wxgtk_renderer_editing_started),
1406 this);
1407 }
1408}
1409
9861f022
RR
1410void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
1411{
1412 GtkCellRendererMode gtkMode;
1413 switch (mode)
1414 {
25bc5c55
VZ
1415 case wxDATAVIEW_CELL_INERT:
1416 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
1417 break;
1418
1419 case wxDATAVIEW_CELL_ACTIVATABLE:
1420 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
1421 break;
1422
1423 case wxDATAVIEW_CELL_EDITABLE:
1424 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
1425 break;
1426
1427 default:
1428 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1429 return;
9861f022
RR
1430 }
1431
b9db5f30 1432 // This value is most often ignored in GtkTreeView
9861f022
RR
1433 GValue gvalue = { 0, };
1434 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1435 g_value_set_enum( &gvalue, gtkMode );
1436 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1437 g_value_unset( &gvalue );
1438}
1439
1440wxDataViewCellMode wxDataViewRenderer::GetMode() const
1441{
1442 wxDataViewCellMode ret;
1443
1444 GValue gvalue;
1445 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1446
1447 switch (g_value_get_enum(&gvalue))
1448 {
25bc5c55
VZ
1449 default:
1450 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1451 // fall through (we have to return something)
1452
1453 case GTK_CELL_RENDERER_MODE_INERT:
1454 ret = wxDATAVIEW_CELL_INERT;
1455 break;
1456
1457 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1458 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1459 break;
1460
1461 case GTK_CELL_RENDERER_MODE_EDITABLE:
1462 ret = wxDATAVIEW_CELL_EDITABLE;
1463 break;
9861f022
RR
1464 }
1465
1466 g_value_unset( &gvalue );
1467
1468 return ret;
1469}
1470
1471void wxDataViewRenderer::SetAlignment( int align )
1472{
1473 // horizontal alignment:
1474
1475 gfloat xalign = 0.0;
1476 if (align & wxALIGN_RIGHT)
1477 xalign = 1.0;
1478 else if (align & wxALIGN_CENTER_HORIZONTAL)
1479 xalign = 0.5;
1480
1481 GValue gvalue = { 0, };
1482 g_value_init( &gvalue, G_TYPE_FLOAT );
1483 g_value_set_float( &gvalue, xalign );
1484 g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue );
1485 g_value_unset( &gvalue );
1486
1487 // vertical alignment:
1488
1489 gfloat yalign = 0.0;
1490 if (align & wxALIGN_BOTTOM)
1491 yalign = 1.0;
1492 else if (align & wxALIGN_CENTER_VERTICAL)
1493 yalign = 0.5;
1494
1495 GValue gvalue2 = { 0, };
1496 g_value_init( &gvalue2, G_TYPE_FLOAT );
1497 g_value_set_float( &gvalue2, yalign );
1498 g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 );
1499 g_value_unset( &gvalue2 );
6842a71a
RR
1500}
1501
9861f022
RR
1502int wxDataViewRenderer::GetAlignment() const
1503{
1504 int ret = 0;
1505 GValue gvalue;
1506
1507 // horizontal alignment:
1508
1509 g_object_get( G_OBJECT(m_renderer), "xalign", &gvalue, NULL );
1510 float xalign = g_value_get_float( &gvalue );
1511 if (xalign < 0.5)
1512 ret |= wxALIGN_LEFT;
1513 else if (xalign == 0.5)
1514 ret |= wxALIGN_CENTER_HORIZONTAL;
1515 else
1516 ret |= wxALIGN_RIGHT;
1517 g_value_unset( &gvalue );
1518
1519
1520 // vertical alignment:
1521
1522 g_object_get( G_OBJECT(m_renderer), "yalign", &gvalue, NULL );
1523 float yalign = g_value_get_float( &gvalue );
1524 if (yalign < 0.5)
1525 ret |= wxALIGN_TOP;
1526 else if (yalign == 0.5)
1527 ret |= wxALIGN_CENTER_VERTICAL;
1528 else
1529 ret |= wxALIGN_BOTTOM;
1530 g_value_unset( &gvalue );
1531
1532 return ret;
1533}
1534
1535
1536
93763ad5 1537// ---------------------------------------------------------
baa9ebc4 1538// wxDataViewTextRenderer
93763ad5 1539// ---------------------------------------------------------
6842a71a 1540
a7f61f76 1541extern "C" {
93763ad5 1542static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
a7f61f76
RR
1543 gchar *arg1, gchar *arg2, gpointer user_data );
1544}
1545
c5c5395b 1546static void wxGtkTextRendererEditedCallback( GtkCellRendererText *WXUNUSED(renderer),
a7f61f76
RR
1547 gchar *arg1, gchar *arg2, gpointer user_data )
1548{
baa9ebc4 1549 wxDataViewTextRenderer *cell = (wxDataViewTextRenderer*) user_data;
93763ad5 1550
1a74f561 1551 wxString tmp = wxGTK_CONV_BACK_FONT(arg2, cell->GetOwner()->GetOwner()->GetFont());
a7f61f76
RR
1552 wxVariant value = tmp;
1553 if (!cell->Validate( value ))
1554 return;
93763ad5 1555
e0062c04 1556 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 1557
a7f61f76 1558 GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
9d52aad3 1559 GtkTreeIter iter;
55fbde12
RR
1560 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, path );
1561 wxDataViewItem item( (void*) iter.user_data );;
a7f61f76 1562 gtk_tree_path_free( path );
93763ad5 1563
0a71f9e9 1564 unsigned int model_col = cell->GetOwner()->GetModelColumn();
93763ad5 1565
9d52aad3
RR
1566 model->SetValue( value, item, model_col );
1567 model->ValueChanged( item, model_col );
a7f61f76
RR
1568}
1569
baa9ebc4 1570IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
6842a71a 1571
9861f022
RR
1572wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1573 int align ) :
1574 wxDataViewRenderer( varianttype, mode, align )
6842a71a 1575{
ed38aa55 1576 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_text_new();
93763ad5 1577
9861f022 1578 if (mode & wxDATAVIEW_CELL_EDITABLE)
a7f61f76
RR
1579 {
1580 GValue gvalue = { 0, };
1581 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1582 g_value_set_boolean( &gvalue, true );
1583 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
1584 g_value_unset( &gvalue );
93763ad5 1585
a7f61f76 1586 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
b9db5f30 1587
a912e81f 1588 GtkInitHandlers();
a7f61f76 1589 }
9861f022
RR
1590
1591 SetMode(mode);
1592 SetAlignment(align);
6842a71a 1593}
790b137e 1594
baa9ebc4 1595bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
7b4fde82
RR
1596{
1597 wxString tmp = value;
93763ad5 1598
7b4fde82
RR
1599 GValue gvalue = { 0, };
1600 g_value_init( &gvalue, G_TYPE_STRING );
1bf629bd 1601 g_value_set_string( &gvalue, wxGTK_CONV_FONT( tmp, GetOwner()->GetOwner()->GetFont() ) );
7b4fde82
RR
1602 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1603 g_value_unset( &gvalue );
93763ad5 1604
7b4fde82
RR
1605 return true;
1606}
1607
9861f022 1608bool wxDataViewTextRenderer::GetValue( wxVariant &value ) const
a7f61f76
RR
1609{
1610 GValue gvalue = { 0, };
1611 g_value_init( &gvalue, G_TYPE_STRING );
1612 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
1bf629bd 1613 wxString tmp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), wx_const_cast(wxDataViewTextRenderer*, this)->GetOwner()->GetOwner()->GetFont() );
a7f61f76 1614 g_value_unset( &gvalue );
93763ad5 1615
a7f61f76
RR
1616 value = tmp;
1617
1618 return true;
1619}
1620
9861f022
RR
1621void wxDataViewTextRenderer::SetAlignment( int align )
1622{
1623 wxDataViewRenderer::SetAlignment(align);
1624
01705e98
RR
1625 if (gtk_check_version(2,10,0))
1626 return;
9861f022 1627
01705e98 1628 // horizontal alignment:
9861f022
RR
1629 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
1630 if (align & wxALIGN_RIGHT)
1631 pangoAlign = PANGO_ALIGN_RIGHT;
1632 else if (align & wxALIGN_CENTER_HORIZONTAL)
1633 pangoAlign = PANGO_ALIGN_CENTER;
1634
1635 GValue gvalue = { 0, };
1636 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1637 g_value_set_enum( &gvalue, pangoAlign );
1638 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
1639 g_value_unset( &gvalue );
1640}
1641
b9db5f30 1642// ---------------------------------------------------------
4264606e 1643// wxDataViewTextRendererAttr
b9db5f30 1644// ---------------------------------------------------------
4264606e
RR
1645
1646IMPLEMENT_CLASS(wxDataViewTextRendererAttr,wxDataViewTextRenderer)
1647
b9db5f30 1648wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString &varianttype,
4264606e
RR
1649 wxDataViewCellMode mode, int align ) :
1650 wxDataViewTextRenderer( varianttype, mode, align )
1651{
1652}
b9db5f30 1653
f4322df6 1654// ---------------------------------------------------------
baa9ebc4 1655// wxDataViewBitmapRenderer
f4322df6 1656// ---------------------------------------------------------
cbc9145c 1657
baa9ebc4 1658IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
cbc9145c 1659
9861f022
RR
1660wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1661 int align ) :
1662 wxDataViewRenderer( varianttype, mode, align )
cbc9145c 1663{
ed38aa55 1664 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_pixbuf_new();
9861f022
RR
1665
1666 SetMode(mode);
1667 SetAlignment(align);
cbc9145c
RR
1668}
1669
baa9ebc4 1670bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
cbc9145c
RR
1671{
1672 if (value.GetType() == wxT("wxBitmap"))
1673 {
2586d4a1
RR
1674 wxBitmap bitmap;
1675 bitmap << value;
f4322df6 1676
2586d4a1
RR
1677 // This may create a Pixbuf representation in the
1678 // wxBitmap object (and it will stay there)
1679 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
f4322df6 1680
2586d4a1
RR
1681 GValue gvalue = { 0, };
1682 g_value_init( &gvalue, G_TYPE_OBJECT );
1683 g_value_set_object( &gvalue, pixbuf );
1684 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1685 g_value_unset( &gvalue );
f4322df6 1686
2586d4a1
RR
1687 return true;
1688 }
f4322df6 1689
2586d4a1
RR
1690 if (value.GetType() == wxT("wxIcon"))
1691 {
1692 wxIcon bitmap;
1693 bitmap << value;
f4322df6 1694
cbc9145c
RR
1695 // This may create a Pixbuf representation in the
1696 // wxBitmap object (and it will stay there)
2586d4a1 1697 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
f4322df6 1698
cbc9145c
RR
1699 GValue gvalue = { 0, };
1700 g_value_init( &gvalue, G_TYPE_OBJECT );
1701 g_value_set_object( &gvalue, pixbuf );
1702 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1703 g_value_unset( &gvalue );
f4322df6 1704
cbc9145c
RR
1705 return true;
1706 }
f4322df6 1707
cbc9145c
RR
1708 return false;
1709}
1710
c5c5395b 1711bool wxDataViewBitmapRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
cbc9145c
RR
1712{
1713 return false;
1714}
f4322df6 1715
93763ad5 1716// ---------------------------------------------------------
baa9ebc4 1717// wxDataViewToggleRenderer
93763ad5 1718// ---------------------------------------------------------
fa28826d 1719
605c2c4a 1720extern "C" {
93763ad5 1721static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
1722 gchar *path, gpointer user_data );
1723}
1724
93763ad5 1725static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
1726 gchar *path, gpointer user_data )
1727{
baa9ebc4 1728 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
605c2c4a 1729
93763ad5 1730 // get old value
605c2c4a
RR
1731 GValue gvalue = { 0, };
1732 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1733 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
93763ad5 1734 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a
RR
1735 g_value_unset( &gvalue );
1736 // invert it
1737 tmp = !tmp;
93763ad5 1738
605c2c4a
RR
1739 wxVariant value = tmp;
1740 if (!cell->Validate( value ))
1741 return;
93763ad5 1742
e0062c04 1743 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 1744
605c2c4a 1745 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
55fbde12
RR
1746 GtkTreeIter iter;
1747 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, gtk_path );
1748 wxDataViewItem item( (void*) iter.user_data );;
605c2c4a 1749 gtk_tree_path_free( gtk_path );
93763ad5 1750
0a71f9e9 1751 unsigned int model_col = cell->GetOwner()->GetModelColumn();
93763ad5 1752
9d52aad3
RR
1753 model->SetValue( value, item, model_col );
1754 model->ValueChanged( item, model_col );
605c2c4a
RR
1755}
1756
baa9ebc4 1757IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
605c2c4a 1758
baa9ebc4 1759wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
9861f022
RR
1760 wxDataViewCellMode mode, int align ) :
1761 wxDataViewRenderer( varianttype, mode, align )
605c2c4a 1762{
ed38aa55 1763 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
93763ad5 1764
9861f022 1765 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
605c2c4a 1766 {
9861f022
RR
1767 g_signal_connect_after( m_renderer, "toggled",
1768 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
553f7d8f
RR
1769 }
1770 else
1771 {
605c2c4a
RR
1772 GValue gvalue = { 0, };
1773 g_value_init( &gvalue, G_TYPE_BOOLEAN );
553f7d8f 1774 g_value_set_boolean( &gvalue, false );
605c2c4a
RR
1775 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
1776 g_value_unset( &gvalue );
605c2c4a 1777 }
9861f022
RR
1778
1779 SetMode(mode);
1780 SetAlignment(align);
605c2c4a
RR
1781}
1782
baa9ebc4 1783bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
605c2c4a
RR
1784{
1785 bool tmp = value;
93763ad5 1786
605c2c4a
RR
1787 GValue gvalue = { 0, };
1788 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1789 g_value_set_boolean( &gvalue, tmp );
1790 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
1791 g_value_unset( &gvalue );
93763ad5 1792
605c2c4a
RR
1793 return true;
1794}
1795
9861f022 1796bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
605c2c4a
RR
1797{
1798 GValue gvalue = { 0, };
1799 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1800 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
93763ad5 1801 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a 1802 g_value_unset( &gvalue );
93763ad5 1803
605c2c4a
RR
1804 value = tmp;
1805
1806 return true;
1807}
93763ad5
WS
1808
1809// ---------------------------------------------------------
baa9ebc4 1810// wxDataViewCustomRenderer
93763ad5 1811// ---------------------------------------------------------
e152afc3 1812
888dde65 1813class wxDataViewCtrlDCImpl: public wxWindowDCImpl
e152afc3
RR
1814{
1815public:
888dde65
RR
1816 wxDataViewCtrlDCImpl( wxDC *owner, wxDataViewCtrl *window ) :
1817 wxWindowDCImpl( owner )
1818 {
1a367564 1819 GtkWidget *widget = window->m_treeview;
e152afc3 1820 // Set later
888dde65 1821 m_gdkwindow = NULL;
7857346a 1822
888dde65 1823 m_window = window;
4d496ecb 1824
e152afc3
RR
1825 m_context = window->GtkGetPangoDefaultContext();
1826 m_layout = pango_layout_new( m_context );
1827 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
1828
1829 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
888dde65
RR
1830
1831 // Set m_gdkwindow later
4d496ecb 1832 // SetUpDC();
e152afc3
RR
1833 }
1834};
1835
888dde65
RR
1836class wxDataViewCtrlDC: public wxWindowDC
1837{
1838public:
c5c5395b
RR
1839 wxDataViewCtrlDC( wxDataViewCtrl *window ) :
1840 wxWindowDC( new wxDataViewCtrlDCImpl( this, window ) )
7857346a 1841 { }
888dde65 1842};
7857346a 1843
888dde65 1844
93763ad5 1845// ---------------------------------------------------------
baa9ebc4 1846// wxDataViewCustomRenderer
93763ad5 1847// ---------------------------------------------------------
e152afc3 1848
baa9ebc4 1849IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
e152afc3 1850
baa9ebc4 1851wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
9861f022
RR
1852 wxDataViewCellMode mode, int align,
1853 bool no_init ) :
1854 wxDataViewRenderer( varianttype, mode, align )
e152afc3
RR
1855{
1856 m_dc = NULL;
f69c03de 1857 m_text_renderer = NULL;
93763ad5 1858
ad63bf41
RR
1859 if (no_init)
1860 m_renderer = NULL;
1861 else
9861f022 1862 Init(mode, align);
ad63bf41
RR
1863}
1864
7857346a 1865void wxDataViewCustomRenderer::RenderText( const wxString &text, int xoffset,
85136e3b 1866 wxRect WXUNUSED(cell), wxDC *WXUNUSED(dc), int WXUNUSED(state) )
52e750fc 1867{
f69c03de 1868#if 0
52e750fc
RR
1869 wxDataViewCtrl *view = GetOwner()->GetOwner();
1870 wxColour col = (state & wxDATAVIEW_CELL_SELECTED) ?
1871 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) :
1872 view->GetForegroundColour();
1873 dc->SetTextForeground(col);
1874 dc->DrawText( text, cell.x + xoffset, cell.y + ((cell.height - dc->GetCharHeight()) / 2));
f69c03de
RR
1875#else
1876 if (!m_text_renderer)
1877 m_text_renderer = gtk_cell_renderer_text_new();
b9db5f30 1878
f69c03de
RR
1879 GValue gvalue = { 0, };
1880 g_value_init( &gvalue, G_TYPE_STRING );
1bf629bd 1881 g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) );
f69c03de
RR
1882 g_object_set_property( G_OBJECT(m_text_renderer), "text", &gvalue );
1883 g_value_unset( &gvalue );
1884
1885 ((GdkRectangle*) cell_area)->x += xoffset;
1886 ((GdkRectangle*) cell_area)->width -= xoffset;
b9db5f30 1887
f69c03de
RR
1888 gtk_cell_renderer_render( m_text_renderer,
1889 window,
1890 widget,
1891 (GdkRectangle*) background_area,
1892 (GdkRectangle*) cell_area,
1893 (GdkRectangle*) expose_area,
1894 (GtkCellRendererState) flags );
b9db5f30 1895
f69c03de
RR
1896 ((GdkRectangle*) cell_area)->x -= xoffset;
1897 ((GdkRectangle*) cell_area)->width += xoffset;
1898#endif
52e750fc
RR
1899}
1900
9861f022 1901bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
ad63bf41 1902{
e152afc3
RR
1903 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
1904 renderer->cell = this;
93763ad5 1905
ed38aa55 1906 m_renderer = (GtkCellRenderer*) renderer;
93763ad5 1907
9861f022
RR
1908 SetMode(mode);
1909 SetAlignment(align);
93763ad5 1910
a912e81f 1911 GtkInitHandlers();
b9db5f30 1912
ad63bf41 1913 return true;
e152afc3
RR
1914}
1915
baa9ebc4 1916wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
e152afc3
RR
1917{
1918 if (m_dc)
1919 delete m_dc;
f69c03de
RR
1920
1921 if (m_text_renderer)
1922 gtk_object_sink( GTK_OBJECT(m_text_renderer) );
e152afc3
RR
1923}
1924
baa9ebc4 1925wxDC *wxDataViewCustomRenderer::GetDC()
e152afc3
RR
1926{
1927 if (m_dc == NULL)
4d496ecb
RR
1928 {
1929 if (GetOwner() == NULL)
1930 return NULL;
1931 if (GetOwner()->GetOwner() == NULL)
1932 return NULL;
e152afc3 1933 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
4d496ecb 1934 }
93763ad5 1935
e152afc3
RR
1936 return m_dc;
1937}
93763ad5
WS
1938
1939// ---------------------------------------------------------
baa9ebc4 1940// wxDataViewProgressRenderer
93763ad5 1941// ---------------------------------------------------------
ad63bf41 1942
baa9ebc4 1943IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
ad63bf41 1944
baa9ebc4 1945wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
9861f022
RR
1946 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
1947 wxDataViewCustomRenderer( varianttype, mode, align, true )
ad63bf41
RR
1948{
1949 m_label = label;
1950 m_value = 0;
93763ad5 1951
ad63bf41
RR
1952#ifdef __WXGTK26__
1953 if (!gtk_check_version(2,6,0))
1954 {
ed38aa55 1955 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
93763ad5 1956
ad63bf41
RR
1957 GValue gvalue = { 0, };
1958 g_value_init( &gvalue, G_TYPE_STRING );
09dfa6a0 1959
1bf629bd 1960 g_value_set_string( &gvalue, wxGTK_CONV_FONT( m_label, GetOwner()->GetOwner()->GetFont() ) );
ad63bf41
RR
1961 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1962 g_value_unset( &gvalue );
9861f022
RR
1963
1964 SetMode(mode);
1965 SetAlignment(align);
ad63bf41
RR
1966 }
1967 else
1968#endif
1969 {
1970 // Use custom cell code
9861f022 1971 wxDataViewCustomRenderer::Init(mode, align);
ad63bf41
RR
1972 }
1973}
1974
baa9ebc4 1975wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
ad63bf41
RR
1976{
1977}
1978
baa9ebc4 1979bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
ad63bf41
RR
1980{
1981#ifdef __WXGTK26__
1982 if (!gtk_check_version(2,6,0))
1983 {
7226118b 1984 gint tmp = (long) value;
ad63bf41
RR
1985 GValue gvalue = { 0, };
1986 g_value_init( &gvalue, G_TYPE_INT );
7226118b 1987 g_value_set_int( &gvalue, tmp );
ad63bf41
RR
1988 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
1989 g_value_unset( &gvalue );
1990 }
1991 else
1992#endif
1993 {
1994 m_value = (long) value;
93763ad5 1995
ad63bf41
RR
1996 if (m_value < 0) m_value = 0;
1997 if (m_value > 100) m_value = 100;
1998 }
93763ad5 1999
ad63bf41
RR
2000 return true;
2001}
93763ad5 2002
85136e3b 2003bool wxDataViewProgressRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
9861f022
RR
2004{
2005 return false;
2006}
2007
85136e3b 2008bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
ad63bf41
RR
2009{
2010 double pct = (double)m_value / 100.0;
2011 wxRect bar = cell;
2012 bar.width = (int)(cell.width * pct);
2013 dc->SetPen( *wxTRANSPARENT_PEN );
2014 dc->SetBrush( *wxBLUE_BRUSH );
2015 dc->DrawRectangle( bar );
2016
2017 dc->SetBrush( *wxTRANSPARENT_BRUSH );
2018 dc->SetPen( *wxBLACK_PEN );
2019 dc->DrawRectangle( cell );
93763ad5 2020
ad63bf41
RR
2021 return true;
2022}
2023
9861f022 2024wxSize wxDataViewProgressRenderer::GetSize() const
ad63bf41
RR
2025{
2026 return wxSize(40,12);
2027}
93763ad5
WS
2028
2029// ---------------------------------------------------------
baa9ebc4 2030// wxDataViewDateRenderer
93763ad5 2031// ---------------------------------------------------------
4d496ecb 2032
baa9ebc4 2033class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
7ea3a0de 2034{
93763ad5 2035public:
baa9ebc4 2036 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
e0062c04 2037 wxDataViewModel *model, const wxDataViewItem &item, unsigned int col ) :
7ea3a0de
RR
2038 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
2039 {
2040 m_model = model;
e0062c04 2041 m_item = item;
7ea3a0de 2042 m_col = col;
7ea3a0de
RR
2043 m_cal = new wxCalendarCtrl( this, -1, *value );
2044 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
2045 sizer->Add( m_cal, 1, wxGROW );
2046 SetSizer( sizer );
2047 sizer->Fit( this );
2048 }
93763ad5 2049
7ea3a0de
RR
2050 virtual void OnDismiss()
2051 {
2052 }
93763ad5 2053
7ea3a0de 2054 void OnCalendar( wxCalendarEvent &event );
93763ad5 2055
e0062c04
RR
2056 wxCalendarCtrl *m_cal;
2057 wxDataViewModel *m_model;
2058 wxDataViewItem m_item;
2059 unsigned int m_col;
93763ad5 2060
7ea3a0de
RR
2061private:
2062 DECLARE_EVENT_TABLE()
2063};
2064
baa9ebc4
RR
2065BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
2066 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
7ea3a0de
RR
2067END_EVENT_TABLE()
2068
baa9ebc4 2069void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
7ea3a0de
RR
2070{
2071 wxDateTime date = event.GetDate();
2072 wxVariant value = date;
e0062c04
RR
2073 m_model->SetValue( value, m_item, m_col );
2074 m_model->ValueChanged( m_item, m_col );
7ea3a0de
RR
2075 DismissAndNotify();
2076}
2077
baa9ebc4 2078IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
4d496ecb 2079
baa9ebc4 2080wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
9861f022
RR
2081 wxDataViewCellMode mode, int align ) :
2082 wxDataViewCustomRenderer( varianttype, mode, align )
4d496ecb 2083{
9861f022
RR
2084 SetMode(mode);
2085 SetAlignment(align);
4d496ecb 2086}
93763ad5 2087
baa9ebc4 2088bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
4d496ecb
RR
2089{
2090 m_date = value.GetDateTime();
93763ad5 2091
4d496ecb
RR
2092 return true;
2093}
2094
85136e3b 2095bool wxDataViewDateRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
9861f022
RR
2096{
2097 return false;
2098}
2099
baa9ebc4 2100bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
4d496ecb
RR
2101{
2102 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
2103 wxString tmp = m_date.FormatDate();
52e750fc 2104 RenderText( tmp, 0, cell, dc, state );
4d496ecb
RR
2105 return true;
2106}
2107
9861f022 2108wxSize wxDataViewDateRenderer::GetSize() const
4d496ecb 2109{
4d496ecb
RR
2110 wxString tmp = m_date.FormatDate();
2111 wxCoord x,y,d;
9861f022 2112 GetView()->GetTextExtent( tmp, &x, &y, &d );
4d496ecb
RR
2113 return wxSize(x,y+d);
2114}
2115
85136e3b 2116bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model,
e0062c04 2117 const wxDataViewItem &item, unsigned int col )
4d496ecb 2118{
3f3af7e7 2119 wxVariant variant;
e0062c04 2120 model->GetValue( variant, item, col );
7ea3a0de
RR
2121 wxDateTime value = variant.GetDateTime();
2122
baa9ebc4 2123 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
e0062c04 2124 GetOwner()->GetOwner()->GetParent(), &value, model, item, col );
7ea3a0de
RR
2125 wxPoint pos = wxGetMousePosition();
2126 popup->Move( pos );
2127 popup->Layout();
2128 popup->Popup( popup->m_cal );
4d496ecb
RR
2129
2130 return true;
2131}
2132
c9c13e70 2133
b9db5f30 2134// ---------------------------------------------------------
c9c13e70 2135// wxDataViewIconTextRenderer
b9db5f30 2136// ---------------------------------------------------------
c9c13e70
RR
2137
2138IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
2139
b9db5f30 2140wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
c9c13e70
RR
2141 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
2142 wxDataViewCustomRenderer( varianttype, mode, align )
2143{
2144 SetMode(mode);
2145 SetAlignment(align);
2146}
2147
2148wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2149{
2150}
b9db5f30 2151
c9c13e70
RR
2152bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
2153{
2154 m_value << value;
2155 return true;
2156}
2157
ad386793 2158bool wxDataViewIconTextRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
c9c13e70
RR
2159{
2160 return false;
2161}
b9db5f30 2162
c9c13e70
RR
2163bool wxDataViewIconTextRenderer::Render( wxRect cell, wxDC *dc, int state )
2164{
c9c13e70 2165 const wxIcon &icon = m_value.GetIcon();
52e750fc 2166 int offset = 0;
c9c13e70
RR
2167 if (icon.IsOk())
2168 {
52e750fc
RR
2169 int yoffset = wxMax( 0, (cell.height - icon.GetHeight()) / 2 );
2170 dc->DrawIcon( icon, cell.x, cell.y + yoffset );
2171 offset = icon.GetWidth() + 4;
c9c13e70 2172 }
b9db5f30 2173
52e750fc 2174 RenderText( m_value.GetText(), offset, cell, dc, state );
c9c13e70
RR
2175
2176 return true;
2177}
2178
2179wxSize wxDataViewIconTextRenderer::GetSize() const
2180{
52e750fc
RR
2181 wxSize size;
2182 if (m_value.GetIcon().IsOk())
2183 size.x = 4 + m_value.GetIcon().GetWidth();
2184 wxCoord x,y,d;
2185 GetView()->GetTextExtent( m_value.GetText(), &x, &y, &d );
2186 size.x += x;
2187 size.y = y+d;
b9db5f30 2188 return size;
c9c13e70
RR
2189}
2190
7857346a 2191wxControl* wxDataViewIconTextRenderer::CreateEditorCtrl(
ad386793 2192 wxWindow *WXUNUSED(parent), wxRect WXUNUSED(labelRect), const wxVariant &WXUNUSED(value) )
c9c13e70
RR
2193{
2194 return NULL;
2195}
2196
7857346a 2197bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl(
ad386793 2198 wxControl* WXUNUSED(editor), wxVariant &WXUNUSED(value) )
c9c13e70
RR
2199{
2200 return false;
2201}
2202
93763ad5 2203// ---------------------------------------------------------
605c2c4a 2204// wxDataViewColumn
93763ad5 2205// ---------------------------------------------------------
7b4fde82 2206
31fb32e1
RR
2207
2208static gboolean
ad386793 2209gtk_dataview_header_button_press_callback( GtkWidget *WXUNUSED(widget),
31fb32e1
RR
2210 GdkEventButton *gdk_event,
2211 wxDataViewColumn *column )
2212{
2213 if (gdk_event->type != GDK_BUTTON_PRESS)
94b1f7bc 2214 return FALSE;
f4322df6 2215
31fb32e1
RR
2216 if (gdk_event->button == 1)
2217 {
d32332aa 2218 gs_lastLeftClickHeader = column;
7857346a 2219
31fb32e1
RR
2220 wxDataViewCtrl *dv = column->GetOwner();
2221 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
2222 event.SetDataViewColumn( column );
2223 event.SetModel( dv->GetModel() );
937013e0 2224 if (dv->HandleWindowEvent( event ))
a84c5b6f 2225 return FALSE;
31fb32e1 2226 }
f4322df6 2227
dadc879e
RR
2228 if (gdk_event->button == 3)
2229 {
2230 wxDataViewCtrl *dv = column->GetOwner();
2231 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, dv->GetId() );
2232 event.SetDataViewColumn( column );
2233 event.SetModel( dv->GetModel() );
937013e0 2234 if (dv->HandleWindowEvent( event ))
dadc879e
RR
2235 return FALSE;
2236 }
2237
94b1f7bc 2238 return FALSE;
31fb32e1
RR
2239}
2240
7b4fde82
RR
2241extern "C" {
2242static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
2243 GtkCellRenderer *cell,
2244 GtkTreeModel *model,
2245 GtkTreeIter *iter,
2246 gpointer data );
2247}
2248
2249
ad386793 2250static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *WXUNUSED(column),
7b4fde82
RR
2251 GtkCellRenderer *renderer,
2252 GtkTreeModel *model,
2253 GtkTreeIter *iter,
2254 gpointer data )
2255{
e0062c04
RR
2256 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
2257 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
93763ad5 2258
baa9ebc4 2259 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
a7f61f76 2260
9d52aad3 2261 wxDataViewItem item( (void*) iter->user_data );
93763ad5 2262
a826202e
RR
2263 wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel();
2264
2056dede
RR
2265 if (!wx_model->IsIndexListModel())
2266 {
b9db5f30 2267
a826202e
RR
2268 if (wx_model->IsContainer( item ))
2269 {
2270 if (wx_model->HasContainerColumns( item ) || (cell->GetOwner()->GetModelColumn() == 0))
2271 {
2272 GValue gvalue = { 0, };
2273 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2274 g_value_set_boolean( &gvalue, TRUE );
2275 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2276 g_value_unset( &gvalue );
2277 }
2278 else
2279 {
2280 GValue gvalue = { 0, };
2281 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2282 g_value_set_boolean( &gvalue, FALSE );
2283 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2284 g_value_unset( &gvalue );
b9db5f30 2285
a826202e
RR
2286 return;
2287 }
2288 }
2289 else
2290 {
2291 GValue gvalue = { 0, };
2292 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2293 g_value_set_boolean( &gvalue, TRUE );
2294 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2295 g_value_unset( &gvalue );
2296 }
b9db5f30 2297
2056dede 2298 }
a826202e 2299
3f3af7e7 2300 wxVariant value;
a826202e 2301 wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
7b4fde82
RR
2302
2303 if (value.GetType() != cell->GetVariantType())
f4322df6
VZ
2304 wxLogError( wxT("Wrong type, required: %s but: %s"),
2305 value.GetType().c_str(),
cbc9145c 2306 cell->GetVariantType().c_str() );
93763ad5 2307
7b4fde82 2308 cell->SetValue( value );
e0743e63 2309
4264606e
RR
2310 if (cell->GtkHasAttributes())
2311 {
2312 wxDataViewItemAttr attr;
2313 bool colour_set = false;
2314 bool style_set = false;
2315 bool weight_set = false;
b9db5f30 2316
4264606e
RR
2317 if (wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr ))
2318 {
2319 // this must be a GtkCellRendererText
2320 wxColour colour = attr.GetColour();
2321 if (colour.IsOk())
2322 {
2323 const GdkColor * const gcol = colour.GetColor();
2a5e6d1b 2324
4264606e
RR
2325 GValue gvalue = { 0, };
2326 g_value_init( &gvalue, GDK_TYPE_COLOR );
2327 g_value_set_boxed( &gvalue, gcol );
2328 g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
2329 g_value_unset( &gvalue );
b9db5f30 2330
4264606e
RR
2331 colour_set = true;
2332 }
b9db5f30 2333
4264606e
RR
2334 if (attr.GetItalic())
2335 {
2336 GValue gvalue = { 0, };
2337 g_value_init( &gvalue, PANGO_TYPE_STYLE );
2338 g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
2339 g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
2340 g_value_unset( &gvalue );
b9db5f30 2341
4264606e
RR
2342 style_set = true;
2343 }
b9db5f30 2344
4264606e
RR
2345 if (attr.GetBold())
2346 {
2347 GValue gvalue = { 0, };
2348 g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
2349 g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
2350 g_object_set_property( G_OBJECT(renderer), "weight", &gvalue );
2351 g_value_unset( &gvalue );
b9db5f30 2352
4264606e
RR
2353 weight_set = true;
2354 }
2355 }
b9db5f30 2356
4264606e
RR
2357 if (!style_set)
2358 {
2359 GValue gvalue = { 0, };
2360 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2361 g_value_set_boolean( &gvalue, FALSE );
2362 g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
2363 g_value_unset( &gvalue );
2364 }
b9db5f30 2365
4264606e
RR
2366 if (!weight_set)
2367 {
2368 GValue gvalue = { 0, };
2369 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2370 g_value_set_boolean( &gvalue, FALSE );
2371 g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue );
2372 g_value_unset( &gvalue );
2373 }
b9db5f30 2374
4264606e
RR
2375 if (!colour_set)
2376 {
2377 GValue gvalue = { 0, };
2378 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2379 g_value_set_boolean( &gvalue, FALSE );
2380 g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
2381 g_value_unset( &gvalue );
2382 }
2383 }
2384
2385#if 0
2a5e6d1b
RR
2386 if (attr.HasBackgroundColour())
2387 {
2388 wxColour colour = attr.GetBackgroundColour();
1cd5e95c 2389 const GdkColor * const gcol = colour.GetColor();
e0743e63 2390
2a5e6d1b
RR
2391 GValue gvalue = { 0, };
2392 g_value_init( &gvalue, GDK_TYPE_COLOR );
2393 g_value_set_boxed( &gvalue, gcol );
2394 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
2395 g_value_unset( &gvalue );
2396 }
2397 else
2398 {
2399 GValue gvalue = { 0, };
2400 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2401 g_value_set_boolean( &gvalue, FALSE );
2402 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
2403 g_value_unset( &gvalue );
2404 }
e0062c04
RR
2405#endif
2406
7b4fde82
RR
2407}
2408
cbc9145c 2409IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
fa28826d 2410
91a6c655 2411#include <wx/listimpl.cpp>
a76c2f37 2412WX_DEFINE_LIST(wxDataViewColumnList)
91a6c655 2413
f4322df6
VZ
2414wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
2415 unsigned int model_column, int width,
87f0efe2
RR
2416 wxAlignment align, int flags ) :
2417 wxDataViewColumnBase( title, cell, model_column, width, align, flags )
fa28826d 2418{
9861f022 2419 Init( align, flags, width );
31fb32e1 2420
9861f022 2421 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column), TRUE );
31fb32e1 2422 SetTitle( title );
fa28826d
RR
2423}
2424
f4322df6
VZ
2425wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
2426 unsigned int model_column, int width,
87f0efe2
RR
2427 wxAlignment align, int flags ) :
2428 wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags )
9861f022
RR
2429{
2430 Init( align, flags, width );
2431
2432 SetBitmap( bitmap );
2433}
2434
2435void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
07a84e7b 2436{
31fb32e1 2437 m_isConnected = false;
07a84e7b 2438
9861f022 2439 GtkCellRenderer *renderer = (GtkCellRenderer *) GetRenderer()->GetGtkHandle();
07a84e7b 2440 GtkTreeViewColumn *column = gtk_tree_view_column_new();
9861f022 2441 m_column = (GtkWidget*) column;
07a84e7b 2442
9861f022
RR
2443 SetFlags( flags );
2444 SetAlignment( align );
07a84e7b 2445
ad386793 2446 SetWidth( width );
07a84e7b 2447
9861f022 2448 gtk_tree_view_column_pack_end( column, renderer, TRUE );
07a84e7b
RR
2449
2450 gtk_tree_view_column_set_cell_data_func( column, renderer,
9861f022 2451 wxGtkTreeCellDataFunc, (gpointer) GetRenderer(), NULL );
07a84e7b
RR
2452}
2453
fa28826d
RR
2454wxDataViewColumn::~wxDataViewColumn()
2455{
2456}
2457
31fb32e1
RR
2458void wxDataViewColumn::OnInternalIdle()
2459{
2460 if (m_isConnected)
2461 return;
f4322df6 2462
31fb32e1
RR
2463 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
2464 {
9861f022 2465 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
31fb32e1
RR
2466 if (column->button)
2467 {
2468 g_signal_connect(column->button, "button_press_event",
2469 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
f4322df6 2470
31fb32e1
RR
2471 m_isConnected = true;
2472 }
2473 }
2474}
2475
b94db696
RR
2476void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
2477{
2478 wxDataViewColumnBase::SetOwner( owner );
f4322df6 2479
9861f022 2480 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2481
b94db696 2482 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
b94db696
RR
2483}
2484
fa28826d
RR
2485void wxDataViewColumn::SetTitle( const wxString &title )
2486{
9861f022 2487 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2488
31fb32e1
RR
2489 if (m_isConnected)
2490 {
2491 // disconnect before column->button gets recreated
f4322df6 2492 g_signal_handlers_disconnect_by_func( column->button,
9861f022 2493 (GtkWidget*) gtk_dataview_header_button_press_callback, this);
f4322df6 2494
31fb32e1
RR
2495 m_isConnected = false;
2496 }
2497
09dfa6a0
VZ
2498 // FIXME: can it really happen that we don't have the owner here??
2499 wxDataViewCtrl *ctrl = GetOwner();
2500 gtk_tree_view_column_set_title( column, ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
2501 : wxGTK_CONV_SYS(title) );
31fb32e1 2502
07a84e7b
RR
2503 gtk_tree_view_column_set_widget( column, NULL );
2504}
2505
9861f022
RR
2506wxString wxDataViewColumn::GetTitle() const
2507{
2508 const gchar *str = gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column) );
2509 return wxConvFileName->cMB2WX(str);
2510}
2511
07a84e7b
RR
2512void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
2513{
2514 wxDataViewColumnBase::SetBitmap( bitmap );
2515
9861f022 2516 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
07a84e7b
RR
2517 if (bitmap.Ok())
2518 {
2519 GtkImage *gtk_image = GTK_IMAGE( gtk_image_new() );
f4322df6 2520
07a84e7b
RR
2521 GdkBitmap *mask = (GdkBitmap *) NULL;
2522 if (bitmap.GetMask())
2523 mask = bitmap.GetMask()->GetBitmap();
2524
2525 if (bitmap.HasPixbuf())
2526 {
2527 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
2528 bitmap.GetPixbuf());
2529 }
2530 else
2531 {
2532 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
2533 bitmap.GetPixmap(), mask);
2534 }
2535 gtk_widget_show( GTK_WIDGET(gtk_image) );
f4322df6 2536
07a84e7b
RR
2537 gtk_tree_view_column_set_widget( column, GTK_WIDGET(gtk_image) );
2538 }
2539 else
2540 {
2541 gtk_tree_view_column_set_widget( column, NULL );
2542 }
fa28826d
RR
2543}
2544
9861f022
RR
2545void wxDataViewColumn::SetHidden( bool hidden )
2546{
2547 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
2548}
2549
2550void wxDataViewColumn::SetResizeable( bool resizeable )
2551{
2552 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
2553}
2554
47cef10f
RR
2555void wxDataViewColumn::SetAlignment( wxAlignment align )
2556{
9861f022 2557 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2558
47cef10f
RR
2559 gfloat xalign = 0.0;
2560 if (align == wxALIGN_RIGHT)
2561 xalign = 1.0;
9861f022
RR
2562 if (align == wxALIGN_CENTER_HORIZONTAL ||
2563 align == wxALIGN_CENTER)
47cef10f 2564 xalign = 0.5;
f4322df6 2565
9861f022
RR
2566 gtk_tree_view_column_set_alignment( column, xalign );
2567}
2568
2569wxAlignment wxDataViewColumn::GetAlignment() const
2570{
2571 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
2572
2573 if (xalign == 1.0)
2574 return wxALIGN_RIGHT;
2575 if (xalign == 0.5)
2576 return wxALIGN_CENTER_HORIZONTAL;
f4322df6 2577
9861f022 2578 return wxALIGN_LEFT;
47cef10f
RR
2579}
2580
31fb32e1
RR
2581void wxDataViewColumn::SetSortable( bool sortable )
2582{
9861f022 2583 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
b9db5f30 2584
94b1f7bc 2585 if (sortable)
0bd26819 2586 {
94b1f7bc 2587 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
0bd26819 2588 }
94b1f7bc 2589 else
0bd26819 2590 {
94b1f7bc 2591 gtk_tree_view_column_set_sort_column_id( column, -1 );
0bd26819
RR
2592 gtk_tree_view_column_set_sort_indicator( column, FALSE );
2593 }
31fb32e1
RR
2594}
2595
87f0efe2 2596bool wxDataViewColumn::IsSortable() const
31fb32e1 2597{
9861f022 2598 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
94b1f7bc 2599 return (gtk_tree_view_column_get_sort_column_id( column ) != -1);
31fb32e1
RR
2600}
2601
9861f022
RR
2602bool wxDataViewColumn::IsResizeable() const
2603{
2604 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2605 return gtk_tree_view_column_get_resizable( column );
2606}
2607
2608bool wxDataViewColumn::IsHidden() const
2609{
2610 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2611 return !gtk_tree_view_column_get_visible( column );
2612}
2613
47cef10f
RR
2614void wxDataViewColumn::SetSortOrder( bool ascending )
2615{
9861f022 2616 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2617
47cef10f
RR
2618 if (ascending)
2619 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
2620 else
2621 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
0bd26819
RR
2622
2623 gtk_tree_view_column_set_sort_indicator( column, TRUE );
47cef10f
RR
2624}
2625
87f0efe2 2626bool wxDataViewColumn::IsSortOrderAscending() const
31fb32e1 2627{
9861f022 2628 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2629
31fb32e1
RR
2630 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
2631}
2632
9861f022 2633void wxDataViewColumn::SetMinWidth( int width )
533544f2 2634{
9861f022 2635 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
533544f2
RR
2636}
2637
9861f022
RR
2638int wxDataViewColumn::GetMinWidth() const
2639{
2640 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
2641}
2642
2643int wxDataViewColumn::GetWidth() const
533544f2 2644{
9861f022 2645 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
533544f2
RR
2646}
2647
9861f022 2648void wxDataViewColumn::SetWidth( int width )
533544f2 2649{
ad386793
RR
2650 if (width < 0)
2651 {
2652 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
2653 }
2654 else
2655 {
2656 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
7857346a 2657
ad386793
RR
2658 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
2659 }
533544f2
RR
2660}
2661
99c75ebc
RR
2662void wxDataViewColumn::SetReorderable( bool reorderable )
2663{
2664 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
2665}
2666
2667bool wxDataViewColumn::IsReorderable() const
2668{
2669 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) );
2670}
9861f022 2671
4508fcd2
RR
2672//-----------------------------------------------------------------------------
2673// wxGtkTreeModelNode
2674//-----------------------------------------------------------------------------
2675
2676void wxGtkTreeModelNode::Resort()
2677{
af110130
RR
2678 size_t child_count = GetChildCount();
2679 if (child_count == 0)
0be79c8a
RR
2680 return;
2681
af110130
RR
2682 size_t node_count = GetNodesCount();
2683
2684 if (child_count == 1)
0be79c8a 2685 {
af110130
RR
2686 if (node_count == 1)
2687 {
2688 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
2689 node->Resort();
2690 }
0be79c8a
RR
2691 return;
2692 }
2693
40196b1e
RR
2694 wxGtkTreeModelChildren temp;
2695 WX_APPEND_ARRAY( temp, m_children );
b9db5f30 2696
40196b1e
RR
2697 g_internal = m_internal;
2698 m_children.Sort( &wxGtkTreeModelChildCmp );
0be79c8a 2699
af110130 2700 gint *new_order = new gint[child_count];
b9db5f30 2701
40196b1e 2702 unsigned int pos;
af110130 2703 for (pos = 0; pos < child_count; pos++)
4508fcd2 2704 {
40196b1e
RR
2705 void *id = m_children.Item( pos );
2706 int old_pos = temp.Index( id );
2707 new_order[pos] = old_pos;
4508fcd2 2708 }
b9db5f30 2709
0be79c8a
RR
2710 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
2711
2712 GtkTreeIter iter;
40196b1e 2713 iter.user_data = GetItem().GetID();
0be79c8a 2714 iter.stamp = m_internal->GetGtkModel()->stamp;
b9db5f30 2715
40196b1e 2716 GtkTreePath *path = m_internal->get_path( &iter );
b9db5f30 2717
0be79c8a 2718 gtk_tree_model_rows_reordered( gtk_tree_model, path, &iter, new_order );
b9db5f30 2719
0be79c8a 2720 gtk_tree_path_free (path);
b9db5f30 2721
0be79c8a 2722 delete [] new_order;
b9db5f30 2723
af110130 2724 for (pos = 0; pos < node_count; pos++)
4508fcd2 2725 {
af110130 2726 wxGtkTreeModelNode *node = m_nodes.Item( pos );
4508fcd2
RR
2727 node->Resort();
2728 }
2729}
2730
55fbde12
RR
2731//-----------------------------------------------------------------------------
2732// wxDataViewCtrlInternal
2733//-----------------------------------------------------------------------------
2734
b9db5f30 2735wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner,
55fbde12 2736 wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model )
b9db5f30 2737{
55fbde12 2738 m_owner = owner;
b9db5f30
VS
2739 m_wx_model = wx_model;
2740 m_gtk_model = gtk_model;
2741 m_root = NULL;
b8b7b087 2742 m_sort_order = GTK_SORT_ASCENDING;
40196b1e 2743 m_sort_column = -1;
d32332aa 2744 m_dataview_sort_column = NULL;
b9db5f30 2745
2056dede
RR
2746 if (!m_wx_model->IsIndexListModel())
2747 InitTree();
55fbde12 2748}
b9db5f30 2749
55fbde12
RR
2750wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2751{
2752 g_object_unref( m_gtk_model );
2753}
b9db5f30 2754
ef427989
RR
2755void wxDataViewCtrlInternal::InitTree()
2756{
2757 wxDataViewItem item;
0be79c8a 2758 m_root = new wxGtkTreeModelNode( NULL, item, this );
ef427989
RR
2759
2760 BuildBranch( m_root );
2761}
2762
55fbde12
RR
2763void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
2764{
2765 if (node->GetChildCount() == 0)
2766 {
74fe973b
RR
2767 wxDataViewItemArray children;
2768 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
2769 unsigned int pos;
2770 for (pos = 0; pos < count; pos++)
55fbde12 2771 {
74fe973b 2772 wxDataViewItem child = children[pos];
b9db5f30 2773
af110130
RR
2774 if (m_wx_model->IsContainer( child ))
2775 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
2776 else
2777 node->AddLeave( child.GetID() );
b9db5f30 2778
effd54b0 2779 // Don't send any events here
55fbde12
RR
2780 }
2781 }
2782}
2783
f6f0ef85
RR
2784// GTK+ dnd iface
2785
7857346a 2786gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
2787 GtkTreePath *path )
2788{
2789 GtkTreeIter iter;
2790 if (!get_iter( &iter, path )) return FALSE;
7857346a 2791
f6f0ef85
RR
2792 wxDataViewItem item( (void*) iter.user_data );
2793
2794 return m_wx_model->IsDraggable( item );
2795}
2796
7857346a
VZ
2797gboolean
2798wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
2799 GtkTreePath *WXUNUSED(path))
f6f0ef85
RR
2800{
2801 return FALSE;
2802}
2803
7857346a 2804gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
2805 GtkTreePath *path, GtkSelectionData *selection_data )
2806{
2807 GtkTreeIter iter;
2808 if (!get_iter( &iter, path )) return FALSE;
7857346a 2809
f6f0ef85 2810 wxDataViewItem item( (void*) iter.user_data );
7857346a 2811
f6f0ef85 2812 wxDataFormat format( selection_data->target );
7857346a 2813
f6f0ef85
RR
2814 size_t size = m_wx_model->GetDragDataSize( item, format );
2815 if (size == 0) return FALSE;
7857346a 2816
f6f0ef85 2817 void *data = malloc( size );
7857346a 2818
f6f0ef85 2819 m_wx_model->GetDragData( item, format, data, size );
7857346a 2820
f6f0ef85
RR
2821 gtk_selection_data_set( selection_data, selection_data->target,
2822 8, (const guchar*) data, size );
2823
2824 free( data );
7857346a 2825
f6f0ef85
RR
2826 return TRUE;
2827}
2828
7857346a
VZ
2829gboolean
2830wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
2831 GtkTreePath *WXUNUSED(dest),
2832 GtkSelectionData *WXUNUSED(selection_data))
f6f0ef85
RR
2833{
2834 return FALSE;
2835}
2836
7857346a
VZ
2837gboolean
2838wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
2839 GtkTreePath *WXUNUSED(dest_path),
2840 GtkSelectionData *WXUNUSED(selection_data))
f6f0ef85
RR
2841{
2842 return FALSE;
2843}
2844
2845// notifications from wxDataViewModel
2846
33ba5a05
RR
2847bool wxDataViewCtrlInternal::Cleared()
2848{
2849 if (m_root)
2850 {
2851 delete m_root;
2852 InitTree();
7857346a
VZ
2853 }
2854
33ba5a05
RR
2855 // Create new GTK model
2856 g_object_unref( m_gtk_model );
2857 m_gtk_model = wxgtk_tree_model_new();
2858 m_gtk_model->internal = this;
7857346a 2859
33ba5a05
RR
2860 return true;
2861}
2862
4508fcd2
RR
2863void wxDataViewCtrlInternal::Resort()
2864{
2056dede
RR
2865 if (!m_wx_model->IsIndexListModel())
2866 m_root->Resort();
4508fcd2
RR
2867}
2868
55fbde12
RR
2869bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
2870{
2056dede
RR
2871 if (!m_wx_model->IsIndexListModel())
2872 {
2873 wxGtkTreeModelNode *parent_node = FindNode( parent );
2874 if (m_wx_model->IsContainer( item ))
2875 parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) );
2876 else
2877 parent_node->AddLeave( item.GetID() );
2878 }
b9db5f30 2879
55fbde12
RR
2880 return true;
2881}
2882
469d3e9b 2883bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
55fbde12 2884{
2056dede
RR
2885 if (!m_wx_model->IsIndexListModel())
2886 {
2887 wxGtkTreeModelNode *parent_node = FindNode( parent );
2888 parent_node->DeleteChild( item.GetID() );
2889 }
b9db5f30 2890
d8331a01
RR
2891 return true;
2892}
2893
2894bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
2895{
6608fdab 2896 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
2897 event.SetEventObject( m_owner );
2898 event.SetModel( m_owner->GetModel() );
2899 event.SetItem( item );
937013e0 2900 m_owner->HandleWindowEvent( event );
b9db5f30 2901
d8331a01
RR
2902 return true;
2903}
2904
2905bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int col )
2906{
6608fdab 2907 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
2908 event.SetEventObject( m_owner );
2909 event.SetModel( m_owner->GetModel() );
2910 event.SetColumn( col );
6608fdab 2911 event.SetDataViewColumn( GetOwner()->GetColumn(col) );
d8331a01 2912 event.SetItem( item );
937013e0 2913 m_owner->HandleWindowEvent( event );
b9db5f30 2914
d8331a01
RR
2915 return true;
2916}
2917
f6f0ef85
RR
2918// GTK+ model iface
2919
2056dede
RR
2920GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
2921{
2922 if (m_wx_model->IsIndexListModel())
2923 return GTK_TREE_MODEL_LIST_ONLY;
2924 else
2925 return GTK_TREE_MODEL_ITERS_PERSIST;
2926}
2927
55fbde12
RR
2928gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
2929{
2056dede
RR
2930 if (m_wx_model->IsIndexListModel())
2931 {
2932 wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model;
b9db5f30 2933
2056dede
RR
2934 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
2935
f52984b8 2936 if (i >= wx_model->GetLastIndex() + 1)
2056dede 2937 return FALSE;
55fbde12 2938
2056dede
RR
2939 iter->stamp = m_gtk_model->stamp;
2940 // user_data is just the index
2941 iter->user_data = (gpointer) i;
55fbde12 2942
2056dede
RR
2943 return TRUE;
2944 }
2945 else
55fbde12 2946 {
2056dede
RR
2947 int depth = gtk_tree_path_get_depth( path );
2948
2949 wxGtkTreeModelNode *node = m_root;
2950
2951 int i;
b9db5f30 2952 for (i = 0; i < depth; i++)
2056dede
RR
2953 {
2954 BuildBranch( node );
b9db5f30 2955
2056dede
RR
2956 gint pos = gtk_tree_path_get_indices (path)[i];
2957 if (pos < 0) return FALSE;
2958 if ((size_t)pos >= node->GetChildCount()) return FALSE;
b9db5f30 2959
2056dede 2960 void* id = node->GetChildren().Item( (size_t) pos );
b9db5f30 2961
2056dede 2962 if (i == depth-1)
af110130 2963 {
2056dede
RR
2964 iter->stamp = m_gtk_model->stamp;
2965 iter->user_data = id;
2966 return TRUE;
af110130 2967 }
2056dede
RR
2968
2969 size_t count = node->GetNodes().GetCount();
2970 size_t pos2;
2971 for (pos2 = 0; pos2 < count; pos2++)
2972 {
2973 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
2974 if (child_node->GetItem().GetID() == id)
2975 {
2976 node = child_node;
2977 break;
2978 }
b9db5f30 2979 }
2056dede 2980 }
af110130 2981 }
55fbde12 2982
af110130 2983 return FALSE;
55fbde12
RR
2984}
2985
2986GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
2987{
2988 GtkTreePath *retval = gtk_tree_path_new ();
b9db5f30 2989
2056dede
RR
2990 if (m_wx_model->IsIndexListModel())
2991 {
2992 // user_data is just the index
2993 int i = (wxUIntPtr) iter->user_data;
2994 gtk_tree_path_append_index (retval, i);
2995 }
2996 else
55fbde12 2997 {
b9db5f30
VS
2998 void *id = iter->user_data;
2999
2056dede
RR
3000 wxGtkTreeModelNode *node = FindParentNode( iter );
3001 while (node)
3002 {
3003 int pos = node->GetChildren().Index( id );
40196b1e 3004
2056dede 3005 gtk_tree_path_prepend_index( retval, pos );
b9db5f30 3006
2056dede
RR
3007 id = node->GetItem().GetID();
3008 node = node->GetParent();
3009 }
55fbde12 3010 }
b9db5f30 3011
55fbde12
RR
3012 return retval;
3013}
3014
3015gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
3016{
2056dede
RR
3017 if (m_wx_model->IsIndexListModel())
3018 {
3019 wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model;
b9db5f30 3020
2056dede 3021 int n = (wxUIntPtr) iter->user_data;
69892729 3022
2056dede
RR
3023 if (n == -1)
3024 return FALSE;
40196b1e 3025
f52984b8 3026 if (n >= (int) wx_model->GetLastIndex())
2056dede
RR
3027 return FALSE;
3028
3029 iter->user_data = (gpointer) ++n;
3030 }
3031 else
3032 {
3033 wxGtkTreeModelNode *parent = FindParentNode( iter );
3034 if( parent == NULL )
3035 return FALSE;
3036
3037 int pos = parent->GetChildren().Index( iter->user_data );
3038
3039 if (pos == (int) parent->GetChildCount()-1)
3040 return FALSE;
b9db5f30 3041
2056dede
RR
3042 iter->stamp = m_gtk_model->stamp;
3043 iter->user_data = parent->GetChildren().Item( pos+1 );
3044 }
55fbde12
RR
3045
3046 return TRUE;
3047}
3048
3049gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
3050{
2056dede
RR
3051 if (m_wx_model->IsIndexListModel())
3052 {
3053 // this is a list, nodes have no children
3054 if (parent)
3055 return FALSE;
3056
3057 iter->stamp = m_gtk_model->stamp;
3058 iter->user_data = (gpointer) -1;
3059
3060 return TRUE;
3061 }
3062 else
3063 {
3064 wxDataViewItem item( (void*) parent->user_data );
b9db5f30 3065
2056dede
RR
3066 if (!m_wx_model->IsContainer( item ))
3067 return FALSE;
b9db5f30 3068
2056dede
RR
3069 wxGtkTreeModelNode *parent_node = FindNode( parent );
3070 BuildBranch( parent_node );
b9db5f30 3071
2056dede
RR
3072 if (parent_node->GetChildCount() == 0)
3073 return FALSE;
b9db5f30 3074
2056dede
RR
3075 iter->stamp = m_gtk_model->stamp;
3076 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
3077 }
b9db5f30 3078
55fbde12
RR
3079 return TRUE;
3080}
3081
3082gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
3083{
2056dede
RR
3084 if (m_wx_model->IsIndexListModel())
3085 {
3086 // this is a list, nodes have no children
3087 return FALSE;
3088 }
3089 else
3090 {
3091 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3092
2056dede 3093 bool is_container = m_wx_model->IsContainer( item );
b9db5f30 3094
2056dede
RR
3095 if (!is_container)
3096 return FALSE;
b9db5f30 3097
2056dede
RR
3098 wxGtkTreeModelNode *node = FindNode( iter );
3099 BuildBranch( node );
b9db5f30 3100
2056dede
RR
3101 return (node->GetChildCount() > 0);
3102 }
55fbde12
RR
3103}
3104
3105gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
3106{
2056dede
RR
3107 if (m_wx_model->IsIndexListModel())
3108 {
3109 wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model;
b9db5f30 3110
2056dede 3111 if (iter == NULL)
f52984b8 3112 return (gint) wx_model->GetLastIndex() + 1;
2056dede 3113
55fbde12 3114 return 0;
2056dede
RR
3115 }
3116 else
3117 {
3118 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3119
2056dede
RR
3120 if (!m_wx_model->IsContainer( item ))
3121 return 0;
b9db5f30 3122
2056dede
RR
3123 wxGtkTreeModelNode *parent_node = FindNode( iter );
3124 BuildBranch( parent_node );
ef427989 3125
2056dede 3126 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
b9db5f30 3127
2056dede
RR
3128 return parent_node->GetChildCount();
3129 }
55fbde12
RR
3130}
3131
3132gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
3133{
2056dede
RR
3134 if (m_wx_model->IsIndexListModel())
3135 {
3136 wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model;
b9db5f30 3137
2056dede
RR
3138 if (parent)
3139 return FALSE;
3140
3141 if (n < 0)
3142 return FALSE;
3143
f52984b8 3144 if (n >= (gint) wx_model->GetLastIndex() + 1)
2056dede
RR
3145 return FALSE;
3146
3147 iter->stamp = m_gtk_model->stamp;
3148 iter->user_data = (gpointer) n;
3149
3150 return TRUE;
3151 }
3152 else
3153 {
3154 void* id = NULL;
3155 if (parent) id = (void*) parent->user_data;
3156 wxDataViewItem item( id );
b9db5f30 3157
2056dede
RR
3158 if (!m_wx_model->IsContainer( item ))
3159 return FALSE;
b9db5f30 3160
2056dede
RR
3161 wxGtkTreeModelNode *parent_node = FindNode( parent );
3162 BuildBranch( parent_node );
55fbde12 3163
2056dede 3164 // wxPrintf( "iter_nth_child %d\n", n );
b9db5f30 3165
2056dede
RR
3166 iter->stamp = m_gtk_model->stamp;
3167 iter->user_data = parent_node->GetChildren().Item( n );
55fbde12 3168
2056dede
RR
3169 return TRUE;
3170 }
55fbde12
RR
3171}
3172
3173gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
3174{
2056dede
RR
3175 if (m_wx_model->IsIndexListModel())
3176 {
55fbde12 3177 return FALSE;
2056dede
RR
3178 }
3179 else
3180 {
3181 wxGtkTreeModelNode *node = FindParentNode( child );
3182 if (!node)
3183 return FALSE;
b9db5f30 3184
2056dede
RR
3185 iter->stamp = m_gtk_model->stamp;
3186 iter->user_data = (gpointer) node->GetItem().GetID();
55fbde12 3187
2056dede
RR
3188 return TRUE;
3189 }
55fbde12 3190}
b9db5f30 3191
55fbde12 3192static wxGtkTreeModelNode*
69892729 3193wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
55fbde12 3194{
69892729
RR
3195 if( model == NULL )
3196 return NULL;
55fbde12 3197
69892729
RR
3198 ItemList list;
3199 list.DeleteContents( true );
3200 wxDataViewItem it( item );
b9db5f30 3201
69892729 3202 while( it.IsOk() )
55fbde12 3203 {
69892729
RR
3204 wxDataViewItem * pItem = new wxDataViewItem( it );
3205 list.Insert( pItem );
3206 it = model->GetParent( it );
3207 }
3208
3209 wxGtkTreeModelNode * node = treeNode;
966cb94d 3210 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
69892729
RR
3211 {
3212 if( node && node->GetNodes().GetCount() != 0 )
ef427989 3213 {
69892729
RR
3214 int len = node->GetNodes().GetCount();
3215 wxGtkTreeModelNodes nodes = node->GetNodes();
3216 int j = 0;
3217 for( ; j < len; j ++)
3218 {
3219 if( nodes[j]->GetItem() == *(n->GetData()))
3220 {
3221 node = nodes[j];
3222 break;
b9db5f30 3223 }
69892729
RR
3224 }
3225
3226 if( j == len )
3227 {
3228 return NULL;
3229 }
ef427989 3230 }
69892729
RR
3231 else
3232 return NULL;
55fbde12 3233 }
69892729
RR
3234 return node;
3235
55fbde12
RR
3236}
3237
3238wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
3239{
3240 if (!iter)
3241 return m_root;
3242
3243 wxDataViewItem item( (void*) iter->user_data );
0be79c8a
RR
3244 if (!item.IsOk())
3245 return m_root;
b9db5f30 3246
69892729 3247 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
b9db5f30 3248
55fbde12
RR
3249 if (!result)
3250 {
26f119eb 3251 wxLogDebug( "Not found %p", iter->user_data );
55fbde12
RR
3252 char *crash = NULL;
3253 *crash = 0;
3254 }
b9db5f30 3255
55fbde12
RR
3256 return result;
3257}
3258
3259wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
3260{
0be79c8a
RR
3261 if (!item.IsOk())
3262 return m_root;
3263
69892729 3264 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
b9db5f30 3265
55fbde12
RR
3266 if (!result)
3267 {
26f119eb 3268 wxLogDebug( "Not found %p", item.GetID() );
55fbde12
RR
3269 char *crash = NULL;
3270 *crash = 0;
3271 }
b9db5f30 3272
55fbde12
RR
3273 return result;
3274}
3275
af110130 3276static wxGtkTreeModelNode*
69892729 3277wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
af110130 3278{
69892729
RR
3279 if( model == NULL )
3280 return NULL;
3281
3282 ItemList list;
3283 list.DeleteContents( true );
3284 if( !item.IsOk() )
3285 return NULL;
3286
3287 wxDataViewItem it( model->GetParent( item ) );
3288 while( it.IsOk() )
af110130 3289 {
69892729
RR
3290 wxDataViewItem * pItem = new wxDataViewItem( it );
3291 list.Insert( pItem );
3292 it = model->GetParent( it );
af110130
RR
3293 }
3294
69892729 3295 wxGtkTreeModelNode * node = treeNode;
966cb94d 3296 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
af110130 3297 {
69892729 3298 if( node && node->GetNodes().GetCount() != 0 )
af110130 3299 {
69892729
RR
3300 int len = node->GetNodes().GetCount();
3301 wxGtkTreeModelNodes nodes = node->GetNodes();
3302 int j = 0;
3303 for( ; j < len; j ++)
3304 {
3305 if( nodes[j]->GetItem() == *(n->GetData()))
3306 {
3307 node = nodes[j];
3308 break;
b9db5f30 3309 }
69892729
RR
3310 }
3311
3312 if( j == len )
3313 {
3314 return NULL;
3315 }
af110130 3316 }
69892729
RR
3317 else
3318 return NULL;
3319 }
3320 //Examine whether the node is item's parent node
3321 int len = node->GetChildCount();
3322 for( int i = 0; i < len ; i ++ )
3323 {
3324 if( node->GetChildren().Item( i ) == item.GetID() )
3325 return node;
af110130 3326 }
af110130
RR
3327 return NULL;
3328}
3329
3330wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
3331{
3332 if (!iter)
3333 return NULL;
b9db5f30 3334
af110130
RR
3335 wxDataViewItem item( (void*) iter->user_data );
3336 if (!item.IsOk())
3337 return NULL;
3338
69892729 3339 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
3340}
3341
3342wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
3343{
3344 if (!item.IsOk())
3345 return NULL;
3346
69892729 3347 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
3348}
3349
eb7f97f8
RR
3350//-----------------------------------------------------------------------------
3351// wxDataViewCtrl signal callbacks
3352//-----------------------------------------------------------------------------
3353
3354static void
ad386793 3355wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv )
eb7f97f8 3356{
b94db696
RR
3357 if (!GTK_WIDGET_REALIZED(dv->m_widget))
3358 return;
f4322df6 3359
d86c1870 3360 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() );
6848478c 3361 event.SetItem( dv->GetSelection() );
eb7f97f8 3362 event.SetModel( dv->GetModel() );
937013e0 3363 dv->HandleWindowEvent( event );
eb7f97f8
RR
3364}
3365
f828871d 3366static void
ad386793
RR
3367wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path,
3368 GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv )
f828871d 3369{
e0062c04 3370 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
55fbde12
RR
3371
3372 GtkTreeIter iter;
3373 dv->GtkGetInternal()->get_iter( &iter, path );
3374 wxDataViewItem item( (void*) iter.user_data );;
3375 event.SetItem( item );
f828871d 3376 event.SetModel( dv->GetModel() );
937013e0 3377 dv->HandleWindowEvent( event );
f828871d
RR
3378}
3379
718fd180 3380static gboolean
ad386793
RR
3381wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3382 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3383{
3384 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
3385
3386 wxDataViewItem item( (void*) iter->user_data );;
3387 event.SetItem( item );
3388 event.SetModel( dv->GetModel() );
937013e0 3389 dv->HandleWindowEvent( event );
b9db5f30 3390
718fd180
RR
3391 return !event.IsAllowed();
3392}
3393
3394static void
ad386793
RR
3395wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3396 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3397{
3398 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
3399
3400 wxDataViewItem item( (void*) iter->user_data );;
3401 event.SetItem( item );
3402 event.SetModel( dv->GetModel() );
937013e0 3403 dv->HandleWindowEvent( event );
718fd180
RR
3404}
3405
3406static gboolean
ad386793
RR
3407wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3408 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3409{
3410 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
3411
3412 wxDataViewItem item( (void*) iter->user_data );;
3413 event.SetItem( item );
3414 event.SetModel( dv->GetModel() );
937013e0 3415 dv->HandleWindowEvent( event );
b9db5f30 3416
718fd180
RR
3417 return !event.IsAllowed();
3418}
3419
3420static void
ad386793
RR
3421wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3422 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3423{
3424 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
3425
3426 wxDataViewItem item( (void*) iter->user_data );;
3427 event.SetItem( item );
3428 event.SetModel( dv->GetModel() );
937013e0 3429 dv->HandleWindowEvent( event );
718fd180
RR
3430}
3431
790b137e 3432//-----------------------------------------------------------------------------
718fd180 3433 // wxDataViewCtrl
790b137e
RR
3434//-----------------------------------------------------------------------------
3435
1e510b1e
RR
3436//-----------------------------------------------------------------------------
3437// InsertChild for wxDataViewCtrl
3438//-----------------------------------------------------------------------------
3439
3440static void wxInsertChildInDataViewCtrl( wxWindowGTK* parent, wxWindowGTK* child )
3441{
3442 wxDataViewCtrl * dvc = (wxDataViewCtrl*) parent;
3443 GtkWidget *treeview = dvc->GtkGetTreeView();
3444
3445 // Insert widget in GtkTreeView
3446 if (GTK_WIDGET_REALIZED(treeview))
e8375af8 3447 gtk_widget_set_parent_window( child->m_widget,
1e510b1e
RR
3448 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
3449 gtk_widget_set_parent( child->m_widget, treeview );
3450}
3451
3452static
3453void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
ad386793 3454 GtkAllocation *WXUNUSED(gtk_alloc),
1e510b1e
RR
3455 wxDataViewCtrl *win )
3456{
966cb94d 3457 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
1e510b1e
RR
3458 while (node)
3459 {
3460 wxWindow *child = node->GetData();
e8375af8 3461
1e510b1e
RR
3462 GtkRequisition req;
3463 gtk_widget_size_request( child->m_widget, &req );
e8375af8 3464
1e510b1e
RR
3465 GtkAllocation alloc;
3466 alloc.x = child->m_x;
3467 alloc.y = child->m_y;
3468 alloc.width = child->m_width;
3469 alloc.height = child->m_height;
3470 gtk_widget_size_allocate( child->m_widget, &alloc );
e8375af8 3471
1e510b1e
RR
3472 node = node->GetNext();
3473 }
3474}
3475
3476
6608fdab
RR
3477//-----------------------------------------------------------------------------
3478// "motion_notify_event"
3479//-----------------------------------------------------------------------------
3480
3481static gboolean
ad386793 3482gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget),
74dea0de
RR
3483 GdkEventMotion *gdk_event,
3484 wxDataViewCtrl *dv )
6608fdab
RR
3485{
3486 if (gdk_event->is_hint)
3487 {
3488 int x = 0;
3489 int y = 0;
3490 GdkModifierType state;
3491 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
3492 gdk_event->x = x;
3493 gdk_event->y = y;
3494 }
3495
3496 GtkTreePath *path = NULL;
3497 GtkTreeViewColumn *column = NULL;
3498 gint cell_x = 0;
3499 gint cell_y = 0;
b9db5f30 3500 if (gtk_tree_view_get_path_at_pos(
6608fdab
RR
3501 GTK_TREE_VIEW(dv->GtkGetTreeView()),
3502 (int) gdk_event->x, (int) gdk_event->y,
3503 &path,
3504 &column,
3505 &cell_x,
3506 &cell_y))
3507 {
3508 if (path)
3509 {
3510 GtkTreeIter iter;
3511 dv->GtkGetInternal()->get_iter( &iter, path );
b9db5f30 3512
6608fdab 3513 // wxPrintf( "mouse %d %d\n", (int) gdk_event->x, (int) gdk_event->y );
b9db5f30 3514
6608fdab
RR
3515 gtk_tree_path_free( path );
3516 }
3517 }
3518
3519
3520 return FALSE;
3521}
3522
74dea0de
RR
3523//-----------------------------------------------------------------------------
3524// "button_press_event"
3525//-----------------------------------------------------------------------------
3526
3527static gboolean
3528gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget),
3529 GdkEventButton *gdk_event,
3530 wxDataViewCtrl *dv )
3531{
3532 if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS))
3533 {
3534 GtkTreePath *path = NULL;
3535 GtkTreeViewColumn *column = NULL;
3536 gint cell_x = 0;
3537 gint cell_y = 0;
3538 if (gtk_tree_view_get_path_at_pos(
3539 GTK_TREE_VIEW(dv->GtkGetTreeView()),
3540 (int) gdk_event->x, (int) gdk_event->y,
3541 &path,
3542 &column,
3543 &cell_x,
3544 &cell_y))
3545 {
3546 if (path)
3547 {
3548 GtkTreeIter iter;
3549 dv->GtkGetInternal()->get_iter( &iter, path );
3550
3551 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() );
3552 wxDataViewItem item( (void*) iter.user_data );;
3553 event.SetItem( item );
3554 event.SetModel( dv->GetModel() );
3555 bool ret = dv->HandleWindowEvent( event );
3556 gtk_tree_path_free( path );
3557 return ret;
3558 }
3559 }
3560 }
3561
3562 return FALSE;
3563}
1e510b1e 3564
239eaa41
RR
3565IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
3566
3567wxDataViewCtrl::~wxDataViewCtrl()
3568{
8f850e28
RR
3569 if (m_notifier)
3570 GetModel()->RemoveNotifier( m_notifier );
87f0efe2
RR
3571
3572 // remove the model from the GtkTreeView before it gets destroyed by the
3573 // wxDataViewCtrlBase's dtor
3574 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL );
b9db5f30 3575
55fbde12 3576 delete m_internal;
239eaa41
RR
3577}
3578
3579void wxDataViewCtrl::Init()
3580{
8f850e28 3581 m_notifier = NULL;
33ba5a05 3582 m_internal = NULL;
239eaa41
RR
3583}
3584
f6f0ef85
RR
3585static GtkTargetEntry gs_target;
3586
239eaa41 3587bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
93763ad5 3588 const wxPoint& pos, const wxSize& size,
239eaa41
RR
3589 long style, const wxValidator& validator )
3590{
3591 Init();
93763ad5 3592
239eaa41
RR
3593 if (!PreCreation( parent, pos, size ) ||
3594 !CreateBase( parent, id, pos, size, style, validator ))
3595 {
3596 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
93763ad5 3597 return false;
239eaa41 3598 }
1a367564 3599
1e510b1e
RR
3600 m_insertCallback = wxInsertChildInDataViewCtrl;
3601
1a367564 3602 m_widget = gtk_scrolled_window_new (NULL, NULL);
6493aaca
VZ
3603
3604 GtkScrolledWindowSetBorder(m_widget, style);
1a367564
RR
3605
3606 m_treeview = gtk_tree_view_new();
3607 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
e8375af8 3608
1e510b1e
RR
3609 g_signal_connect (m_treeview, "size_allocate",
3610 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
ed4b0fdc 3611
25bc5c55 3612 gs_target.target = const_cast<char *>("UTF8_STRING");
f6f0ef85 3613 gs_target.flags = 0;
bd7cef4a 3614 gs_target.info = static_cast<guint>(-1);
7857346a 3615 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_treeview),
f6f0ef85 3616 GDK_BUTTON1_MASK, &gs_target, 1, (GdkDragAction) GDK_ACTION_COPY );
7857346a 3617
b94db696
RR
3618#ifdef __WXGTK26__
3619 if (!gtk_check_version(2,6,0))
3620 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), TRUE );
3621#endif
3622
daebb44c
RR
3623 if (style & wxDV_MULTIPLE)
3624 {
3625 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3626 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
3627 }
93763ad5 3628
9861f022
RR
3629 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
3630
3631#ifdef __WXGTK210__
3632 if (!gtk_check_version(2,10,0))
3633 {
3634 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
f4322df6
VZ
3635
3636 if ((style & wxDV_HORIZ_RULES) != 0 &&
9861f022
RR
3637 (style & wxDV_VERT_RULES) != 0)
3638 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
3639 else if (style & wxDV_VERT_RULES)
3640 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
3641 else if (style & wxDV_HORIZ_RULES)
3642 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
3643
1a07a730
RR
3644 if (grid != GTK_TREE_VIEW_GRID_LINES_NONE)
3645 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
9861f022 3646 }
9861f022 3647#endif
1a07a730
RR
3648
3649 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 );
9861f022 3650
1a367564
RR
3651 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
3652 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
3653 gtk_widget_show (m_treeview);
93763ad5 3654
239eaa41
RR
3655 m_parent->DoAddChild( this );
3656
b94db696
RR
3657 PostCreation(size);
3658
b086d55f
RR
3659 GtkEnableSelectionEvents();
3660
718fd180 3661 g_signal_connect_after (m_treeview, "row-activated",
f828871d 3662 G_CALLBACK (wxdataview_row_activated_callback), this);
eb7f97f8 3663
718fd180
RR
3664 g_signal_connect (m_treeview, "test-collapse-row",
3665 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
3666
3667 g_signal_connect_after (m_treeview, "row-collapsed",
3668 G_CALLBACK (wxdataview_row_collapsed_callback), this);
3669
3670 g_signal_connect (m_treeview, "test-expand-row",
3671 G_CALLBACK (wxdataview_test_expand_row_callback), this);
b9db5f30 3672
718fd180
RR
3673 g_signal_connect_after (m_treeview, "row-expanded",
3674 G_CALLBACK (wxdataview_row_expanded_callback), this);
3675
6608fdab
RR
3676 g_signal_connect (m_treeview, "motion_notify_event",
3677 G_CALLBACK (gtk_dataview_motion_notify_callback), this);
3678
74dea0de
RR
3679 g_signal_connect (m_treeview, "button_press_event",
3680 G_CALLBACK (gtk_dataview_button_press_callback), this);
7857346a 3681
239eaa41
RR
3682 return true;
3683}
3684
31fb32e1
RR
3685void wxDataViewCtrl::OnInternalIdle()
3686{
3687 wxWindow::OnInternalIdle();
f4322df6 3688
9861f022 3689 unsigned int cols = GetColumnCount();
31fb32e1
RR
3690 unsigned int i;
3691 for (i = 0; i < cols; i++)
3692 {
3693 wxDataViewColumn *col = GetColumn( i );
3694 col->OnInternalIdle();
3695 }
3696}
3697
e0062c04 3698bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
239eaa41 3699{
33ba5a05
RR
3700 if (GetModel())
3701 {
3702 delete m_internal;
3703 m_internal = NULL;
7857346a 3704
33ba5a05
RR
3705 delete m_notifier;
3706 m_notifier = NULL;
3707 }
7857346a 3708
239eaa41
RR
3709 if (!wxDataViewCtrlBase::AssociateModel( model ))
3710 return false;
3711
55fbde12
RR
3712 GtkWxTreeModel *gtk_model = wxgtk_tree_model_new();
3713 m_internal = new wxDataViewCtrlInternal( this, model, gtk_model );
3714 gtk_model->internal = m_internal;
6e2e590f 3715
55fbde12 3716 m_notifier = new wxGtkDataViewModelNotifier( gtk_model, model, this );
6e2e590f 3717
93763ad5 3718 model->AddNotifier( m_notifier );
1557c77b 3719
55fbde12 3720 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_model) );
b9db5f30 3721
55fbde12
RR
3722 // unref in wxDataViewCtrlInternal
3723 // g_object_unref( gtk_model );
93763ad5 3724
239eaa41
RR
3725 return true;
3726}
790b137e 3727
fa28826d
RR
3728bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
3729{
3730 if (!wxDataViewCtrlBase::AppendColumn(col))
3731 return false;
93763ad5 3732
91a6c655
RR
3733 m_cols.Append( col );
3734
ad386793
RR
3735#ifdef __WXGTK26__
3736 if (!gtk_check_version(2,6,0))
3737 {
3738 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
3739 GTK_TREE_VIEW_COLUMN_FIXED)
3740 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
3741 }
3742#endif
3743
b9db5f30 3744 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
3745 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
3746
3747 return true;
3748}
3749
736fe67c
RR
3750bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
3751{
3752 if (!wxDataViewCtrlBase::PrependColumn(col))
3753 return false;
3754
3755 m_cols.Insert( col );
3756
ad386793
RR
3757#ifdef __WXGTK26__
3758 if (!gtk_check_version(2,6,0))
3759 {
3760 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
3761 GTK_TREE_VIEW_COLUMN_FIXED)
3762 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
3763 }
3764#endif
3765
b9db5f30 3766 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
736fe67c
RR
3767 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 );
3768
3769 return true;
3770}
3771
91a6c655
RR
3772unsigned int wxDataViewCtrl::GetColumnCount() const
3773{
3774 return m_cols.GetCount();
3775}
3776
3777wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
3778{
3779 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
3780 if (!gtk_col)
3781 return NULL;
b9db5f30 3782
91a6c655
RR
3783 wxDataViewColumnList::const_iterator iter;
3784 for (iter = m_cols.begin(); iter != m_cols.end(); iter++)
3785 {
3786 wxDataViewColumn *col = *iter;
3787 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
3788 {
3789 return col;
3790 }
3791 }
b9db5f30 3792
91a6c655
RR
3793 return NULL;
3794}
3795
3796bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
3797{
b9db5f30 3798 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
3799 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
3800
3801 m_cols.remove( column );
fa28826d 3802
91a6c655 3803 delete column;
fa28826d
RR
3804
3805 return true;
3806}
3807
91a6c655
RR
3808bool wxDataViewCtrl::ClearColumns()
3809{
3810 wxDataViewColumnList::iterator iter;
3811 for (iter = m_cols.begin(); iter != m_cols.end(); iter++)
3812 {
3813 wxDataViewColumn *col = *iter;
b9db5f30 3814 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
3815 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
3816 }
b9db5f30 3817
91a6c655 3818 m_cols.clear();
b9db5f30 3819
91a6c655
RR
3820 return true;
3821}
3822
453091c2
RR
3823int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
3824{
aed836f3
RR
3825 GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetConstGtkHandle());
3826
3827 GList *list = gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview) );
b9db5f30 3828
aed836f3 3829 gint pos = g_list_index( list, (gconstpointer) gtk_column );
b9db5f30 3830
aed836f3
RR
3831 g_list_free( list );
3832
3833 return pos;
453091c2
RR
3834}
3835
21f47fb9
RR
3836wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
3837{
d32332aa 3838 return m_internal->GetDataViewSortColumn();
21f47fb9
RR
3839}
3840
f71d3ba4
RR
3841void wxDataViewCtrl::Expand( const wxDataViewItem & item )
3842{
3843 GtkTreeIter iter;
3844 iter.user_data = item.GetID();
3845 GtkTreePath *path = m_internal->get_path( &iter );
3846 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
3847 gtk_tree_path_free( path );
3848}
3849
3850void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
3851{
3852 GtkTreeIter iter;
3853 iter.user_data = item.GetID();
3854 GtkTreePath *path = m_internal->get_path( &iter );
3855 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
3856 gtk_tree_path_free( path );
3857}
3858
fbda518c 3859wxDataViewItem wxDataViewCtrl::GetSelection() const
1e08ad10
RR
3860{
3861 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 3862
1e08ad10
RR
3863 if (m_windowStyle & wxDV_MULTIPLE)
3864 {
6848478c
RR
3865 // Report the first one
3866 GtkTreeModel *model;
3867 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
b9db5f30 3868
6848478c
RR
3869 if (list)
3870 {
3871 GtkTreePath *path = (GtkTreePath*) list->data;
3872 GtkTreeIter iter;
3873 m_internal->get_iter( &iter, path );
b9db5f30 3874
6848478c
RR
3875 // delete list
3876 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
3877 g_list_free( list );
b9db5f30 3878
6848478c
RR
3879 return wxDataViewItem( (void*) iter.user_data );
3880 }
1e08ad10
RR
3881 }
3882 else
3883 {
3884 GtkTreeIter iter;
3885 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
3886 {
9d52aad3 3887 wxDataViewItem item( (void*) iter.user_data );
1e08ad10
RR
3888 return item;
3889 }
3890 }
b9db5f30 3891
1e08ad10
RR
3892 return wxDataViewItem(0);
3893}
3894
718fd180
RR
3895int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
3896{
6848478c
RR
3897 sel.Clear();
3898
3899 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3900 if (HasFlag(wxDV_MULTIPLE))
3901 {
3902 GtkTreeModel *model;
3903 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
3904
3905 int count = 0;
3906 while (list)
3907 {
3908 GtkTreePath *path = (GtkTreePath*) list->data;
b9db5f30 3909
6848478c
RR
3910 GtkTreeIter iter;
3911 m_internal->get_iter( &iter, path );
3912
3913 sel.Add( wxDataViewItem( (void*) iter.user_data ) );
3914
3915 list = g_list_next( list );
3916 count++;
3917 }
3918
3919 // delete list
3920 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
3921 g_list_free( list );
3922
3923 return count;
3924 }
3925 else
3926 {
3927 GtkTreeModel *model;
3928 GtkTreeIter iter;
3929 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
3930 if (has_selection)
3931 {
3932 sel.Add( wxDataViewItem( (void*) iter.user_data) );
3933 return 1;
3934 }
3935 }
3936
718fd180
RR
3937 return 0;
3938}
3939
3940void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
3941{
6848478c 3942 GtkDisableSelectionEvents();
b9db5f30 3943
6848478c
RR
3944 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3945
3946 gtk_tree_selection_unselect_all( selection );
b9db5f30 3947
6848478c
RR
3948 size_t i;
3949 for (i = 0; i < sel.GetCount(); i++)
3950 {
3951 GtkTreeIter iter;
3952 iter.user_data = (gpointer) sel[i].GetID();
3953 gtk_tree_selection_select_iter( selection, &iter );
3954 }
b9db5f30 3955
6848478c 3956 GtkEnableSelectionEvents();
718fd180
RR
3957}
3958
3959void wxDataViewCtrl::Select( const wxDataViewItem & item )
3960{
6848478c
RR
3961 GtkDisableSelectionEvents();
3962
3963 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3964
3965 GtkTreeIter iter;
3966 iter.user_data = (gpointer) item.GetID();
3967 gtk_tree_selection_select_iter( selection, &iter );
3968
3969 GtkEnableSelectionEvents();
718fd180
RR
3970}
3971
3972void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
3973{
6848478c
RR
3974 GtkDisableSelectionEvents();
3975
3976 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3977
3978 GtkTreeIter iter;
3979 iter.user_data = (gpointer) item.GetID();
3980 gtk_tree_selection_unselect_iter( selection, &iter );
3981
3982 GtkEnableSelectionEvents();
718fd180
RR
3983}
3984
3985bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
3986{
6848478c
RR
3987 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3988
3989 GtkTreeIter iter;
3990 iter.user_data = (gpointer) item.GetID();
b9db5f30 3991
6848478c 3992 return gtk_tree_selection_iter_is_selected( selection, &iter );
718fd180
RR
3993}
3994
3995void wxDataViewCtrl::SelectAll()
3996{
6848478c 3997 GtkDisableSelectionEvents();
b9db5f30 3998
6848478c 3999 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4000
6848478c 4001 gtk_tree_selection_select_all( selection );
b9db5f30 4002
6848478c 4003 GtkEnableSelectionEvents();
718fd180
RR
4004}
4005
4006void wxDataViewCtrl::UnselectAll()
4007{
6848478c 4008 GtkDisableSelectionEvents();
b9db5f30 4009
6848478c 4010 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4011
6848478c 4012 gtk_tree_selection_unselect_all( selection );
b9db5f30 4013
6848478c 4014 GtkEnableSelectionEvents();
718fd180
RR
4015}
4016
7857346a
VZ
4017void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,
4018 const wxDataViewColumn *WXUNUSED(column))
718fd180 4019{
6154212e
RR
4020 GtkTreeIter iter;
4021 iter.user_data = (gpointer) item.GetID();
4022 GtkTreePath *path = m_internal->get_path( &iter );
4023 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4024 gtk_tree_path_free( path );
718fd180
RR
4025}
4026
7857346a
VZ
4027void wxDataViewCtrl::HitTest(const wxPoint& WXUNUSED(point),
4028 wxDataViewItem& item,
4029 wxDataViewColumn *& column) const
66e09788
RR
4030{
4031 item = wxDataViewItem(0);
a87b466d 4032 column = NULL;
66e09788
RR
4033}
4034
7857346a
VZ
4035wxRect
4036wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item),
4037 const wxDataViewColumn *WXUNUSED(column)) const
66e09788
RR
4038{
4039 return wxRect();
4040}
4041
3b6280be
RR
4042void wxDataViewCtrl::DoSetExpanderColumn()
4043{
b9db5f30 4044 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
1b27b2bd 4045 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
3b6280be
RR
4046}
4047
4048void wxDataViewCtrl::DoSetIndent()
4049{
4050}
4051
b086d55f
RR
4052void wxDataViewCtrl::GtkDisableSelectionEvents()
4053{
4054 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
4055 g_signal_handlers_disconnect_by_func( selection,
4056 (gpointer) (wxdataview_selection_changed_callback), this);
b086d55f
RR
4057}
4058
4059void wxDataViewCtrl::GtkEnableSelectionEvents()
4060{
4061 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
4062 g_signal_connect_after (selection, "changed",
4063 G_CALLBACK (wxdataview_selection_changed_callback), this);
b086d55f
RR
4064}
4065
b94db696
RR
4066// static
4067wxVisualAttributes
4068wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
4069{
4070 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
4071}
4072
6ff7eee7 4073
93763ad5 4074#endif
4ed7af08 4075 // !wxUSE_GENERICDATAVIEWCTRL
790b137e 4076
93763ad5 4077#endif
4ed7af08 4078 // wxUSE_DATAVIEWCTRL