]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dataview.cpp
Use SetCanFocus
[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 }
1175 if (button_event->button == 3)
1176 {
9d52aad3 1177 if (cell->RightClick( pt, renderrect, model, item, model_col ))
4d496ecb
RR
1178 ret = true;
1179 }
93763ad5 1180
4d496ecb 1181 wxrenderer->last_click = button_event->time;
93763ad5 1182
4d496ecb
RR
1183 return ret;
1184 }
93763ad5 1185
4d496ecb 1186 return false;
553f7d8f
RR
1187}
1188
93763ad5 1189// ---------------------------------------------------------
e0062c04 1190// wxGtkDataViewModelNotifier
93763ad5 1191// ---------------------------------------------------------
6e2e590f 1192
e0062c04 1193class wxGtkDataViewModelNotifier: public wxDataViewModelNotifier
6e2e590f
RR
1194{
1195public:
e0062c04
RR
1196 wxGtkDataViewModelNotifier( GtkWxTreeModel *wxgtk_model,
1197 wxDataViewModel *wx_model,
1198 wxDataViewCtrl *ctrl );
1199 ~wxGtkDataViewModelNotifier();
1200
1201 virtual bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
469d3e9b 1202 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
e0062c04
RR
1203 virtual bool ItemChanged( const wxDataViewItem &item );
1204 virtual bool ValueChanged( const wxDataViewItem &item, unsigned int col );
6e2e590f 1205 virtual bool Cleared();
4508fcd2 1206 virtual void Resort();
93763ad5 1207
33ba5a05
RR
1208 void SetGtkModel( GtkWxTreeModel *model ) { m_wxgtk_model = model; }
1209
1210private:
e0062c04
RR
1211 GtkWxTreeModel *m_wxgtk_model;
1212 wxDataViewModel *m_wx_model;
63415a42 1213 wxDataViewCtrl *m_owner;
6e2e590f
RR
1214};
1215
93763ad5 1216// ---------------------------------------------------------
6e2e590f 1217// wxGtkDataViewListModelNotifier
93763ad5 1218// ---------------------------------------------------------
6e2e590f 1219
e0062c04
RR
1220wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier(
1221 GtkWxTreeModel* wxgtk_model, wxDataViewModel *wx_model,
63415a42 1222 wxDataViewCtrl *ctrl )
6e2e590f 1223{
e0062c04 1224 m_wxgtk_model = wxgtk_model;
6e2e590f 1225 m_wx_model = wx_model;
63415a42 1226 m_owner = ctrl;
6e2e590f 1227}
93763ad5 1228
e0062c04 1229wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier()
f7ed8c89
RR
1230{
1231 m_wx_model = NULL;
e0062c04 1232 m_wxgtk_model = NULL;
f7ed8c89
RR
1233}
1234
e0062c04 1235bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
6e2e590f 1236{
55fbde12 1237 m_owner->GtkGetInternal()->ItemAdded( parent, item );
1e08ad10 1238
6e2e590f 1239 GtkTreeIter iter;
e0062c04
RR
1240 iter.stamp = m_wxgtk_model->stamp;
1241 iter.user_data = (gpointer) item.GetID();
93763ad5 1242
b9db5f30 1243 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04 1244 GTK_TREE_MODEL(m_wxgtk_model), &iter );
b9db5f30 1245 gtk_tree_model_row_inserted(
e0062c04 1246 GTK_TREE_MODEL(m_wxgtk_model), path, &iter);
6e2e590f 1247 gtk_tree_path_free (path);
93763ad5 1248
6e2e590f
RR
1249 return true;
1250}
1251
469d3e9b 1252bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
6e2e590f 1253{
605c2c4a 1254 GtkTreeIter iter;
e0062c04
RR
1255 iter.stamp = m_wxgtk_model->stamp;
1256 iter.user_data = (gpointer) item.GetID();
93763ad5 1257
b9db5f30 1258 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04
RR
1259 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1260 gtk_tree_model_row_deleted(
1261 GTK_TREE_MODEL(m_wxgtk_model), path );
605c2c4a 1262 gtk_tree_path_free (path);
93763ad5 1263
469d3e9b 1264 m_owner->GtkGetInternal()->ItemDeleted( parent, item );
b9db5f30 1265
605c2c4a 1266 return true;
6e2e590f
RR
1267}
1268
4508fcd2
RR
1269void wxGtkDataViewModelNotifier::Resort()
1270{
1271 m_owner->GtkGetInternal()->Resort();
1272}
1273
e0062c04 1274bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem &item )
6e2e590f 1275{
4627af27 1276 GtkTreeIter iter;
e0062c04
RR
1277 iter.stamp = m_wxgtk_model->stamp;
1278 iter.user_data = (gpointer) item.GetID();
6e2e590f 1279
b9db5f30 1280 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04
RR
1281 GTK_TREE_MODEL(m_wxgtk_model), &iter );
1282 gtk_tree_model_row_changed(
1283 GTK_TREE_MODEL(m_wxgtk_model), path, &iter );
a7f61f76
RR
1284 gtk_tree_path_free (path);
1285
d8331a01 1286 m_owner->GtkGetInternal()->ItemChanged( item );
b9db5f30 1287
a7f61f76 1288 return true;
6e2e590f
RR
1289}
1290
e0062c04 1291bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem &item, unsigned int model_col )
6e2e590f 1292{
4eccd3a1 1293 // This adds GTK+'s missing MVC logic for ValueChanged
63415a42
RR
1294 unsigned int index;
1295 for (index = 0; index < m_owner->GetColumnCount(); index++)
8f850e28 1296 {
63415a42
RR
1297 wxDataViewColumn *column = m_owner->GetColumn( index );
1298 if (column->GetModelColumn() == model_col)
8f850e28 1299 {
63415a42
RR
1300 GtkTreeView *widget = GTK_TREE_VIEW(m_owner->m_treeview);
1301 GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle());
8f850e28
RR
1302
1303 // Get cell area
e0062c04
RR
1304 GtkTreeIter iter;
1305 iter.stamp = m_wxgtk_model->stamp;
1306 iter.user_data = (gpointer) item.GetID();
b9db5f30 1307 GtkTreePath *path = wxgtk_tree_model_get_path(
e0062c04 1308 GTK_TREE_MODEL(m_wxgtk_model), &iter );
8f850e28 1309 GdkRectangle cell_area;
63415a42 1310 gtk_tree_view_get_cell_area( widget, path, gcolumn, &cell_area );
93763ad5 1311 gtk_tree_path_free( path );
8f850e28 1312
1a64259d
RR
1313 GtkAdjustment* hadjust = gtk_tree_view_get_hadjustment( widget );
1314 double d = gtk_adjustment_get_value( hadjust );
1315 int xdiff = (int) d;
1316
63415a42 1317 int ydiff = gcolumn->button->allocation.height;
8f850e28 1318 // Redraw
93763ad5 1319 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
1a64259d 1320 cell_area.x - xdiff, ydiff + cell_area.y, cell_area.width, cell_area.height );
b9db5f30 1321
d8331a01 1322 m_owner->GtkGetInternal()->ValueChanged( item, model_col );
b9db5f30 1323
d8331a01 1324 return true;
8f850e28 1325 }
8f850e28 1326 }
93763ad5 1327
d8331a01 1328 return false;
6e2e590f
RR
1329}
1330
e0062c04 1331bool wxGtkDataViewModelNotifier::Cleared()
6e2e590f 1332{
33ba5a05 1333 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->m_treeview), NULL );
7857346a 1334
33ba5a05 1335 // this will create a new GTK model
d8331a01 1336 m_owner->GtkGetInternal()->Cleared();
7857346a 1337
33ba5a05 1338 SetGtkModel( m_owner->GtkGetInternal()->GetGtkModel() );
b9db5f30 1339
33ba5a05 1340 gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->m_treeview), GTK_TREE_MODEL(m_wxgtk_model) );
7857346a 1341
6e2e590f
RR
1342 return false;
1343}
1344
93763ad5 1345// ---------------------------------------------------------
baa9ebc4 1346// wxDataViewRenderer
93763ad5 1347// ---------------------------------------------------------
6842a71a 1348
a912e81f
RR
1349static gpointer s_user_data = NULL;
1350
1351static void
c5c5395b 1352wxgtk_cell_editable_editing_done( GtkCellEditable *WXUNUSED(editable),
a912e81f
RR
1353 wxDataViewRenderer *wxrenderer )
1354{
1355 wxDataViewColumn *column = wxrenderer->GetOwner();
1356 wxDataViewCtrl *dv = column->GetOwner();
1357 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv->GetId() );
1358 event.SetDataViewColumn( column );
1359 event.SetModel( dv->GetModel() );
1360 wxDataViewItem item( s_user_data );
1361 event.SetItem( item );
937013e0 1362 dv->HandleWindowEvent( event );
a912e81f
RR
1363}
1364
b9db5f30 1365static void
c5c5395b 1366wxgtk_renderer_editing_started( GtkCellRenderer *WXUNUSED(cell), GtkCellEditable *editable,
a912e81f
RR
1367 gchar *path, wxDataViewRenderer *wxrenderer )
1368{
1369 wxDataViewColumn *column = wxrenderer->GetOwner();
1370 wxDataViewCtrl *dv = column->GetOwner();
1371 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv->GetId() );
1372 event.SetDataViewColumn( column );
1373 event.SetModel( dv->GetModel() );
1374 GtkTreePath *tree_path = gtk_tree_path_new_from_string( path );
1375 GtkTreeIter iter;
1376 dv->GtkGetInternal()->get_iter( &iter, tree_path );
1377 gtk_tree_path_free( tree_path );
1378 wxDataViewItem item( iter.user_data );
1379 event.SetItem( item );
937013e0 1380 dv->HandleWindowEvent( event );
a912e81f
RR
1381
1382 if (GTK_IS_CELL_EDITABLE(editable))
1383 {
1384 s_user_data = iter.user_data;
b9db5f30 1385
a912e81f
RR
1386 g_signal_connect (GTK_CELL_EDITABLE (editable), "editing_done",
1387 G_CALLBACK (wxgtk_cell_editable_editing_done),
1388 (gpointer) wxrenderer );
b9db5f30 1389
a912e81f
RR
1390 }
1391}
1392
1393
baa9ebc4 1394IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
6842a71a 1395
9861f022
RR
1396wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1397 int align ) :
1398 wxDataViewRendererBase( varianttype, mode, align )
6842a71a
RR
1399{
1400 m_renderer = NULL;
9861f022
RR
1401
1402 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
1403 // after the m_renderer pointer has been initialized
1404}
1405
a912e81f
RR
1406void wxDataViewRenderer::GtkInitHandlers()
1407{
1408 if (!gtk_check_version(2,6,0))
1409 {
1410 g_signal_connect (GTK_CELL_RENDERER(m_renderer), "editing_started",
1411 G_CALLBACK (wxgtk_renderer_editing_started),
1412 this);
1413 }
1414}
1415
9861f022
RR
1416void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
1417{
1418 GtkCellRendererMode gtkMode;
1419 switch (mode)
1420 {
25bc5c55
VZ
1421 case wxDATAVIEW_CELL_INERT:
1422 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
1423 break;
1424
1425 case wxDATAVIEW_CELL_ACTIVATABLE:
1426 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
1427 break;
1428
1429 case wxDATAVIEW_CELL_EDITABLE:
1430 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
1431 break;
1432
1433 default:
1434 wxFAIL_MSG( "unknown wxDataViewCellMode value" );
1435 return;
9861f022
RR
1436 }
1437
b9db5f30 1438 // This value is most often ignored in GtkTreeView
9861f022
RR
1439 GValue gvalue = { 0, };
1440 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1441 g_value_set_enum( &gvalue, gtkMode );
1442 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1443 g_value_unset( &gvalue );
1444}
1445
1446wxDataViewCellMode wxDataViewRenderer::GetMode() const
1447{
1448 wxDataViewCellMode ret;
1449
1450 GValue gvalue;
1451 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
1452
1453 switch (g_value_get_enum(&gvalue))
1454 {
25bc5c55
VZ
1455 default:
1456 wxFAIL_MSG( "unknown GtkCellRendererMode value" );
1457 // fall through (we have to return something)
1458
1459 case GTK_CELL_RENDERER_MODE_INERT:
1460 ret = wxDATAVIEW_CELL_INERT;
1461 break;
1462
1463 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
1464 ret = wxDATAVIEW_CELL_ACTIVATABLE;
1465 break;
1466
1467 case GTK_CELL_RENDERER_MODE_EDITABLE:
1468 ret = wxDATAVIEW_CELL_EDITABLE;
1469 break;
9861f022
RR
1470 }
1471
1472 g_value_unset( &gvalue );
1473
1474 return ret;
1475}
1476
1477void wxDataViewRenderer::SetAlignment( int align )
1478{
1479 // horizontal alignment:
1480
1481 gfloat xalign = 0.0;
1482 if (align & wxALIGN_RIGHT)
1483 xalign = 1.0;
1484 else if (align & wxALIGN_CENTER_HORIZONTAL)
1485 xalign = 0.5;
1486
1487 GValue gvalue = { 0, };
1488 g_value_init( &gvalue, G_TYPE_FLOAT );
1489 g_value_set_float( &gvalue, xalign );
1490 g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue );
1491 g_value_unset( &gvalue );
1492
1493 // vertical alignment:
1494
1495 gfloat yalign = 0.0;
1496 if (align & wxALIGN_BOTTOM)
1497 yalign = 1.0;
1498 else if (align & wxALIGN_CENTER_VERTICAL)
1499 yalign = 0.5;
1500
1501 GValue gvalue2 = { 0, };
1502 g_value_init( &gvalue2, G_TYPE_FLOAT );
1503 g_value_set_float( &gvalue2, yalign );
1504 g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 );
1505 g_value_unset( &gvalue2 );
6842a71a
RR
1506}
1507
9861f022
RR
1508int wxDataViewRenderer::GetAlignment() const
1509{
1510 int ret = 0;
1511 GValue gvalue;
1512
1513 // horizontal alignment:
1514
1515 g_object_get( G_OBJECT(m_renderer), "xalign", &gvalue, NULL );
1516 float xalign = g_value_get_float( &gvalue );
1517 if (xalign < 0.5)
1518 ret |= wxALIGN_LEFT;
1519 else if (xalign == 0.5)
1520 ret |= wxALIGN_CENTER_HORIZONTAL;
1521 else
1522 ret |= wxALIGN_RIGHT;
1523 g_value_unset( &gvalue );
1524
1525
1526 // vertical alignment:
1527
1528 g_object_get( G_OBJECT(m_renderer), "yalign", &gvalue, NULL );
1529 float yalign = g_value_get_float( &gvalue );
1530 if (yalign < 0.5)
1531 ret |= wxALIGN_TOP;
1532 else if (yalign == 0.5)
1533 ret |= wxALIGN_CENTER_VERTICAL;
1534 else
1535 ret |= wxALIGN_BOTTOM;
1536 g_value_unset( &gvalue );
1537
1538 return ret;
1539}
1540
1541
1542
93763ad5 1543// ---------------------------------------------------------
baa9ebc4 1544// wxDataViewTextRenderer
93763ad5 1545// ---------------------------------------------------------
6842a71a 1546
a7f61f76 1547extern "C" {
93763ad5 1548static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
a7f61f76
RR
1549 gchar *arg1, gchar *arg2, gpointer user_data );
1550}
1551
c5c5395b 1552static void wxGtkTextRendererEditedCallback( GtkCellRendererText *WXUNUSED(renderer),
a7f61f76
RR
1553 gchar *arg1, gchar *arg2, gpointer user_data )
1554{
baa9ebc4 1555 wxDataViewTextRenderer *cell = (wxDataViewTextRenderer*) user_data;
93763ad5 1556
1a74f561 1557 wxString tmp = wxGTK_CONV_BACK_FONT(arg2, cell->GetOwner()->GetOwner()->GetFont());
a7f61f76
RR
1558 wxVariant value = tmp;
1559 if (!cell->Validate( value ))
1560 return;
93763ad5 1561
e0062c04 1562 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 1563
a7f61f76 1564 GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
9d52aad3 1565 GtkTreeIter iter;
55fbde12
RR
1566 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, path );
1567 wxDataViewItem item( (void*) iter.user_data );;
a7f61f76 1568 gtk_tree_path_free( path );
93763ad5 1569
0a71f9e9 1570 unsigned int model_col = cell->GetOwner()->GetModelColumn();
93763ad5 1571
9d52aad3
RR
1572 model->SetValue( value, item, model_col );
1573 model->ValueChanged( item, model_col );
a7f61f76
RR
1574}
1575
baa9ebc4 1576IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
6842a71a 1577
9861f022
RR
1578wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1579 int align ) :
1580 wxDataViewRenderer( varianttype, mode, align )
6842a71a 1581{
ed38aa55 1582 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_text_new();
93763ad5 1583
9861f022 1584 if (mode & wxDATAVIEW_CELL_EDITABLE)
a7f61f76
RR
1585 {
1586 GValue gvalue = { 0, };
1587 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1588 g_value_set_boolean( &gvalue, true );
1589 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
1590 g_value_unset( &gvalue );
93763ad5 1591
a7f61f76 1592 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
b9db5f30 1593
a912e81f 1594 GtkInitHandlers();
a7f61f76 1595 }
9861f022
RR
1596
1597 SetMode(mode);
1598 SetAlignment(align);
6842a71a 1599}
790b137e 1600
baa9ebc4 1601bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
7b4fde82
RR
1602{
1603 wxString tmp = value;
93763ad5 1604
7b4fde82
RR
1605 GValue gvalue = { 0, };
1606 g_value_init( &gvalue, G_TYPE_STRING );
1bf629bd 1607 g_value_set_string( &gvalue, wxGTK_CONV_FONT( tmp, GetOwner()->GetOwner()->GetFont() ) );
7b4fde82
RR
1608 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1609 g_value_unset( &gvalue );
93763ad5 1610
7b4fde82
RR
1611 return true;
1612}
1613
9861f022 1614bool wxDataViewTextRenderer::GetValue( wxVariant &value ) const
a7f61f76
RR
1615{
1616 GValue gvalue = { 0, };
1617 g_value_init( &gvalue, G_TYPE_STRING );
1618 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
1bf629bd 1619 wxString tmp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), wx_const_cast(wxDataViewTextRenderer*, this)->GetOwner()->GetOwner()->GetFont() );
a7f61f76 1620 g_value_unset( &gvalue );
93763ad5 1621
a7f61f76
RR
1622 value = tmp;
1623
1624 return true;
1625}
1626
9861f022
RR
1627void wxDataViewTextRenderer::SetAlignment( int align )
1628{
1629 wxDataViewRenderer::SetAlignment(align);
1630
01705e98
RR
1631 if (gtk_check_version(2,10,0))
1632 return;
9861f022 1633
01705e98 1634 // horizontal alignment:
9861f022
RR
1635 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
1636 if (align & wxALIGN_RIGHT)
1637 pangoAlign = PANGO_ALIGN_RIGHT;
1638 else if (align & wxALIGN_CENTER_HORIZONTAL)
1639 pangoAlign = PANGO_ALIGN_CENTER;
1640
1641 GValue gvalue = { 0, };
1642 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1643 g_value_set_enum( &gvalue, pangoAlign );
1644 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
1645 g_value_unset( &gvalue );
1646}
1647
b9db5f30 1648// ---------------------------------------------------------
4264606e 1649// wxDataViewTextRendererAttr
b9db5f30 1650// ---------------------------------------------------------
4264606e
RR
1651
1652IMPLEMENT_CLASS(wxDataViewTextRendererAttr,wxDataViewTextRenderer)
1653
b9db5f30 1654wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString &varianttype,
4264606e
RR
1655 wxDataViewCellMode mode, int align ) :
1656 wxDataViewTextRenderer( varianttype, mode, align )
1657{
1658}
b9db5f30 1659
f4322df6 1660// ---------------------------------------------------------
baa9ebc4 1661// wxDataViewBitmapRenderer
f4322df6 1662// ---------------------------------------------------------
cbc9145c 1663
baa9ebc4 1664IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
cbc9145c 1665
9861f022
RR
1666wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1667 int align ) :
1668 wxDataViewRenderer( varianttype, mode, align )
cbc9145c 1669{
ed38aa55 1670 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_pixbuf_new();
9861f022
RR
1671
1672 SetMode(mode);
1673 SetAlignment(align);
cbc9145c
RR
1674}
1675
baa9ebc4 1676bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
cbc9145c
RR
1677{
1678 if (value.GetType() == wxT("wxBitmap"))
1679 {
2586d4a1
RR
1680 wxBitmap bitmap;
1681 bitmap << value;
f4322df6 1682
2586d4a1
RR
1683 // This may create a Pixbuf representation in the
1684 // wxBitmap object (and it will stay there)
1685 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
f4322df6 1686
2586d4a1
RR
1687 GValue gvalue = { 0, };
1688 g_value_init( &gvalue, G_TYPE_OBJECT );
1689 g_value_set_object( &gvalue, pixbuf );
1690 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1691 g_value_unset( &gvalue );
f4322df6 1692
2586d4a1
RR
1693 return true;
1694 }
f4322df6 1695
2586d4a1
RR
1696 if (value.GetType() == wxT("wxIcon"))
1697 {
1698 wxIcon bitmap;
1699 bitmap << value;
f4322df6 1700
cbc9145c
RR
1701 // This may create a Pixbuf representation in the
1702 // wxBitmap object (and it will stay there)
2586d4a1 1703 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
f4322df6 1704
cbc9145c
RR
1705 GValue gvalue = { 0, };
1706 g_value_init( &gvalue, G_TYPE_OBJECT );
1707 g_value_set_object( &gvalue, pixbuf );
1708 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1709 g_value_unset( &gvalue );
f4322df6 1710
cbc9145c
RR
1711 return true;
1712 }
f4322df6 1713
cbc9145c
RR
1714 return false;
1715}
1716
c5c5395b 1717bool wxDataViewBitmapRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
cbc9145c
RR
1718{
1719 return false;
1720}
f4322df6 1721
93763ad5 1722// ---------------------------------------------------------
baa9ebc4 1723// wxDataViewToggleRenderer
93763ad5 1724// ---------------------------------------------------------
fa28826d 1725
605c2c4a 1726extern "C" {
93763ad5 1727static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
1728 gchar *path, gpointer user_data );
1729}
1730
93763ad5 1731static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
1732 gchar *path, gpointer user_data )
1733{
baa9ebc4 1734 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
605c2c4a 1735
93763ad5 1736 // get old value
605c2c4a
RR
1737 GValue gvalue = { 0, };
1738 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1739 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
93763ad5 1740 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a
RR
1741 g_value_unset( &gvalue );
1742 // invert it
1743 tmp = !tmp;
93763ad5 1744
605c2c4a
RR
1745 wxVariant value = tmp;
1746 if (!cell->Validate( value ))
1747 return;
93763ad5 1748
e0062c04 1749 wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 1750
605c2c4a 1751 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
55fbde12
RR
1752 GtkTreeIter iter;
1753 cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, gtk_path );
1754 wxDataViewItem item( (void*) iter.user_data );;
605c2c4a 1755 gtk_tree_path_free( gtk_path );
93763ad5 1756
0a71f9e9 1757 unsigned int model_col = cell->GetOwner()->GetModelColumn();
93763ad5 1758
9d52aad3
RR
1759 model->SetValue( value, item, model_col );
1760 model->ValueChanged( item, model_col );
605c2c4a
RR
1761}
1762
baa9ebc4 1763IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
605c2c4a 1764
baa9ebc4 1765wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
9861f022
RR
1766 wxDataViewCellMode mode, int align ) :
1767 wxDataViewRenderer( varianttype, mode, align )
605c2c4a 1768{
ed38aa55 1769 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new();
93763ad5 1770
9861f022 1771 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
605c2c4a 1772 {
9861f022
RR
1773 g_signal_connect_after( m_renderer, "toggled",
1774 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
553f7d8f
RR
1775 }
1776 else
1777 {
605c2c4a
RR
1778 GValue gvalue = { 0, };
1779 g_value_init( &gvalue, G_TYPE_BOOLEAN );
553f7d8f 1780 g_value_set_boolean( &gvalue, false );
605c2c4a
RR
1781 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
1782 g_value_unset( &gvalue );
605c2c4a 1783 }
9861f022
RR
1784
1785 SetMode(mode);
1786 SetAlignment(align);
605c2c4a
RR
1787}
1788
baa9ebc4 1789bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
605c2c4a
RR
1790{
1791 bool tmp = value;
93763ad5 1792
605c2c4a
RR
1793 GValue gvalue = { 0, };
1794 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1795 g_value_set_boolean( &gvalue, tmp );
1796 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
1797 g_value_unset( &gvalue );
93763ad5 1798
605c2c4a
RR
1799 return true;
1800}
1801
9861f022 1802bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
605c2c4a
RR
1803{
1804 GValue gvalue = { 0, };
1805 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1806 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
93763ad5 1807 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a 1808 g_value_unset( &gvalue );
93763ad5 1809
605c2c4a
RR
1810 value = tmp;
1811
1812 return true;
1813}
93763ad5
WS
1814
1815// ---------------------------------------------------------
baa9ebc4 1816// wxDataViewCustomRenderer
93763ad5 1817// ---------------------------------------------------------
e152afc3 1818
888dde65 1819class wxDataViewCtrlDCImpl: public wxWindowDCImpl
e152afc3
RR
1820{
1821public:
888dde65
RR
1822 wxDataViewCtrlDCImpl( wxDC *owner, wxDataViewCtrl *window ) :
1823 wxWindowDCImpl( owner )
1824 {
1a367564 1825 GtkWidget *widget = window->m_treeview;
e152afc3 1826 // Set later
888dde65 1827 m_gdkwindow = NULL;
7857346a 1828
888dde65 1829 m_window = window;
4d496ecb 1830
e152afc3
RR
1831 m_context = window->GtkGetPangoDefaultContext();
1832 m_layout = pango_layout_new( m_context );
1833 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
1834
1835 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
888dde65
RR
1836
1837 // Set m_gdkwindow later
4d496ecb 1838 // SetUpDC();
e152afc3
RR
1839 }
1840};
1841
888dde65
RR
1842class wxDataViewCtrlDC: public wxWindowDC
1843{
1844public:
c5c5395b
RR
1845 wxDataViewCtrlDC( wxDataViewCtrl *window ) :
1846 wxWindowDC( new wxDataViewCtrlDCImpl( this, window ) )
7857346a 1847 { }
888dde65 1848};
7857346a 1849
888dde65 1850
93763ad5 1851// ---------------------------------------------------------
baa9ebc4 1852// wxDataViewCustomRenderer
93763ad5 1853// ---------------------------------------------------------
e152afc3 1854
baa9ebc4 1855IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
e152afc3 1856
baa9ebc4 1857wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
9861f022
RR
1858 wxDataViewCellMode mode, int align,
1859 bool no_init ) :
1860 wxDataViewRenderer( varianttype, mode, align )
e152afc3
RR
1861{
1862 m_dc = NULL;
f69c03de 1863 m_text_renderer = NULL;
93763ad5 1864
ad63bf41
RR
1865 if (no_init)
1866 m_renderer = NULL;
1867 else
9861f022 1868 Init(mode, align);
ad63bf41
RR
1869}
1870
7857346a 1871void wxDataViewCustomRenderer::RenderText( const wxString &text, int xoffset,
85136e3b 1872 wxRect WXUNUSED(cell), wxDC *WXUNUSED(dc), int WXUNUSED(state) )
52e750fc 1873{
f69c03de 1874#if 0
52e750fc
RR
1875 wxDataViewCtrl *view = GetOwner()->GetOwner();
1876 wxColour col = (state & wxDATAVIEW_CELL_SELECTED) ?
1877 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) :
1878 view->GetForegroundColour();
1879 dc->SetTextForeground(col);
1880 dc->DrawText( text, cell.x + xoffset, cell.y + ((cell.height - dc->GetCharHeight()) / 2));
f69c03de
RR
1881#else
1882 if (!m_text_renderer)
1883 m_text_renderer = gtk_cell_renderer_text_new();
b9db5f30 1884
f69c03de
RR
1885 GValue gvalue = { 0, };
1886 g_value_init( &gvalue, G_TYPE_STRING );
1bf629bd 1887 g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) );
f69c03de
RR
1888 g_object_set_property( G_OBJECT(m_text_renderer), "text", &gvalue );
1889 g_value_unset( &gvalue );
1890
1891 ((GdkRectangle*) cell_area)->x += xoffset;
1892 ((GdkRectangle*) cell_area)->width -= xoffset;
b9db5f30 1893
f69c03de
RR
1894 gtk_cell_renderer_render( m_text_renderer,
1895 window,
1896 widget,
1897 (GdkRectangle*) background_area,
1898 (GdkRectangle*) cell_area,
1899 (GdkRectangle*) expose_area,
1900 (GtkCellRendererState) flags );
b9db5f30 1901
f69c03de
RR
1902 ((GdkRectangle*) cell_area)->x -= xoffset;
1903 ((GdkRectangle*) cell_area)->width += xoffset;
1904#endif
52e750fc
RR
1905}
1906
9861f022 1907bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
ad63bf41 1908{
e152afc3
RR
1909 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
1910 renderer->cell = this;
93763ad5 1911
ed38aa55 1912 m_renderer = (GtkCellRenderer*) renderer;
93763ad5 1913
9861f022
RR
1914 SetMode(mode);
1915 SetAlignment(align);
93763ad5 1916
a912e81f 1917 GtkInitHandlers();
b9db5f30 1918
ad63bf41 1919 return true;
e152afc3
RR
1920}
1921
baa9ebc4 1922wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
e152afc3
RR
1923{
1924 if (m_dc)
1925 delete m_dc;
f69c03de
RR
1926
1927 if (m_text_renderer)
1928 gtk_object_sink( GTK_OBJECT(m_text_renderer) );
e152afc3
RR
1929}
1930
baa9ebc4 1931wxDC *wxDataViewCustomRenderer::GetDC()
e152afc3
RR
1932{
1933 if (m_dc == NULL)
4d496ecb
RR
1934 {
1935 if (GetOwner() == NULL)
1936 return NULL;
1937 if (GetOwner()->GetOwner() == NULL)
1938 return NULL;
e152afc3 1939 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
4d496ecb 1940 }
93763ad5 1941
e152afc3
RR
1942 return m_dc;
1943}
93763ad5
WS
1944
1945// ---------------------------------------------------------
baa9ebc4 1946// wxDataViewProgressRenderer
93763ad5 1947// ---------------------------------------------------------
ad63bf41 1948
baa9ebc4 1949IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
ad63bf41 1950
baa9ebc4 1951wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
9861f022
RR
1952 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
1953 wxDataViewCustomRenderer( varianttype, mode, align, true )
ad63bf41
RR
1954{
1955 m_label = label;
1956 m_value = 0;
93763ad5 1957
ad63bf41
RR
1958#ifdef __WXGTK26__
1959 if (!gtk_check_version(2,6,0))
1960 {
ed38aa55 1961 m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new();
93763ad5 1962
ad63bf41
RR
1963 GValue gvalue = { 0, };
1964 g_value_init( &gvalue, G_TYPE_STRING );
09dfa6a0 1965
1bf629bd 1966 g_value_set_string( &gvalue, wxGTK_CONV_FONT( m_label, GetOwner()->GetOwner()->GetFont() ) );
ad63bf41
RR
1967 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1968 g_value_unset( &gvalue );
9861f022
RR
1969
1970 SetMode(mode);
1971 SetAlignment(align);
ad63bf41
RR
1972 }
1973 else
1974#endif
1975 {
1976 // Use custom cell code
9861f022 1977 wxDataViewCustomRenderer::Init(mode, align);
ad63bf41
RR
1978 }
1979}
1980
baa9ebc4 1981wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
ad63bf41
RR
1982{
1983}
1984
baa9ebc4 1985bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
ad63bf41
RR
1986{
1987#ifdef __WXGTK26__
1988 if (!gtk_check_version(2,6,0))
1989 {
7226118b 1990 gint tmp = (long) value;
ad63bf41
RR
1991 GValue gvalue = { 0, };
1992 g_value_init( &gvalue, G_TYPE_INT );
7226118b 1993 g_value_set_int( &gvalue, tmp );
ad63bf41
RR
1994 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
1995 g_value_unset( &gvalue );
1996 }
1997 else
1998#endif
1999 {
2000 m_value = (long) value;
93763ad5 2001
ad63bf41
RR
2002 if (m_value < 0) m_value = 0;
2003 if (m_value > 100) m_value = 100;
2004 }
93763ad5 2005
ad63bf41
RR
2006 return true;
2007}
93763ad5 2008
85136e3b 2009bool wxDataViewProgressRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
9861f022
RR
2010{
2011 return false;
2012}
2013
85136e3b 2014bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
ad63bf41
RR
2015{
2016 double pct = (double)m_value / 100.0;
2017 wxRect bar = cell;
2018 bar.width = (int)(cell.width * pct);
2019 dc->SetPen( *wxTRANSPARENT_PEN );
2020 dc->SetBrush( *wxBLUE_BRUSH );
2021 dc->DrawRectangle( bar );
2022
2023 dc->SetBrush( *wxTRANSPARENT_BRUSH );
2024 dc->SetPen( *wxBLACK_PEN );
2025 dc->DrawRectangle( cell );
93763ad5 2026
ad63bf41
RR
2027 return true;
2028}
2029
9861f022 2030wxSize wxDataViewProgressRenderer::GetSize() const
ad63bf41
RR
2031{
2032 return wxSize(40,12);
2033}
93763ad5
WS
2034
2035// ---------------------------------------------------------
baa9ebc4 2036// wxDataViewDateRenderer
93763ad5 2037// ---------------------------------------------------------
4d496ecb 2038
baa9ebc4 2039class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
7ea3a0de 2040{
93763ad5 2041public:
baa9ebc4 2042 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
e0062c04 2043 wxDataViewModel *model, const wxDataViewItem &item, unsigned int col ) :
7ea3a0de
RR
2044 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
2045 {
2046 m_model = model;
e0062c04 2047 m_item = item;
7ea3a0de 2048 m_col = col;
7ea3a0de
RR
2049 m_cal = new wxCalendarCtrl( this, -1, *value );
2050 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
2051 sizer->Add( m_cal, 1, wxGROW );
2052 SetSizer( sizer );
2053 sizer->Fit( this );
2054 }
93763ad5 2055
7ea3a0de
RR
2056 virtual void OnDismiss()
2057 {
2058 }
93763ad5 2059
7ea3a0de 2060 void OnCalendar( wxCalendarEvent &event );
93763ad5 2061
e0062c04
RR
2062 wxCalendarCtrl *m_cal;
2063 wxDataViewModel *m_model;
2064 wxDataViewItem m_item;
2065 unsigned int m_col;
93763ad5 2066
7ea3a0de
RR
2067private:
2068 DECLARE_EVENT_TABLE()
2069};
2070
baa9ebc4
RR
2071BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
2072 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
7ea3a0de
RR
2073END_EVENT_TABLE()
2074
baa9ebc4 2075void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
7ea3a0de
RR
2076{
2077 wxDateTime date = event.GetDate();
2078 wxVariant value = date;
e0062c04
RR
2079 m_model->SetValue( value, m_item, m_col );
2080 m_model->ValueChanged( m_item, m_col );
7ea3a0de
RR
2081 DismissAndNotify();
2082}
2083
baa9ebc4 2084IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
4d496ecb 2085
baa9ebc4 2086wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
9861f022
RR
2087 wxDataViewCellMode mode, int align ) :
2088 wxDataViewCustomRenderer( varianttype, mode, align )
4d496ecb 2089{
9861f022
RR
2090 SetMode(mode);
2091 SetAlignment(align);
4d496ecb 2092}
93763ad5 2093
baa9ebc4 2094bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
4d496ecb
RR
2095{
2096 m_date = value.GetDateTime();
93763ad5 2097
4d496ecb
RR
2098 return true;
2099}
2100
85136e3b 2101bool wxDataViewDateRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
9861f022
RR
2102{
2103 return false;
2104}
2105
baa9ebc4 2106bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
4d496ecb
RR
2107{
2108 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
2109 wxString tmp = m_date.FormatDate();
52e750fc 2110 RenderText( tmp, 0, cell, dc, state );
4d496ecb
RR
2111 return true;
2112}
2113
9861f022 2114wxSize wxDataViewDateRenderer::GetSize() const
4d496ecb 2115{
4d496ecb
RR
2116 wxString tmp = m_date.FormatDate();
2117 wxCoord x,y,d;
9861f022 2118 GetView()->GetTextExtent( tmp, &x, &y, &d );
4d496ecb
RR
2119 return wxSize(x,y+d);
2120}
2121
85136e3b 2122bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model,
e0062c04 2123 const wxDataViewItem &item, unsigned int col )
4d496ecb 2124{
3f3af7e7 2125 wxVariant variant;
e0062c04 2126 model->GetValue( variant, item, col );
7ea3a0de
RR
2127 wxDateTime value = variant.GetDateTime();
2128
baa9ebc4 2129 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
e0062c04 2130 GetOwner()->GetOwner()->GetParent(), &value, model, item, col );
7ea3a0de
RR
2131 wxPoint pos = wxGetMousePosition();
2132 popup->Move( pos );
2133 popup->Layout();
2134 popup->Popup( popup->m_cal );
4d496ecb
RR
2135
2136 return true;
2137}
2138
c9c13e70 2139
b9db5f30 2140// ---------------------------------------------------------
c9c13e70 2141// wxDataViewIconTextRenderer
b9db5f30 2142// ---------------------------------------------------------
c9c13e70
RR
2143
2144IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
2145
b9db5f30 2146wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
c9c13e70
RR
2147 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
2148 wxDataViewCustomRenderer( varianttype, mode, align )
2149{
2150 SetMode(mode);
2151 SetAlignment(align);
2152}
2153
2154wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
2155{
2156}
b9db5f30 2157
c9c13e70
RR
2158bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
2159{
2160 m_value << value;
2161 return true;
2162}
2163
ad386793 2164bool wxDataViewIconTextRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
c9c13e70
RR
2165{
2166 return false;
2167}
b9db5f30 2168
c9c13e70
RR
2169bool wxDataViewIconTextRenderer::Render( wxRect cell, wxDC *dc, int state )
2170{
c9c13e70 2171 const wxIcon &icon = m_value.GetIcon();
52e750fc 2172 int offset = 0;
c9c13e70
RR
2173 if (icon.IsOk())
2174 {
52e750fc
RR
2175 int yoffset = wxMax( 0, (cell.height - icon.GetHeight()) / 2 );
2176 dc->DrawIcon( icon, cell.x, cell.y + yoffset );
2177 offset = icon.GetWidth() + 4;
c9c13e70 2178 }
b9db5f30 2179
52e750fc 2180 RenderText( m_value.GetText(), offset, cell, dc, state );
c9c13e70
RR
2181
2182 return true;
2183}
2184
2185wxSize wxDataViewIconTextRenderer::GetSize() const
2186{
52e750fc
RR
2187 wxSize size;
2188 if (m_value.GetIcon().IsOk())
2189 size.x = 4 + m_value.GetIcon().GetWidth();
2190 wxCoord x,y,d;
2191 GetView()->GetTextExtent( m_value.GetText(), &x, &y, &d );
2192 size.x += x;
2193 size.y = y+d;
b9db5f30 2194 return size;
c9c13e70
RR
2195}
2196
7857346a 2197wxControl* wxDataViewIconTextRenderer::CreateEditorCtrl(
ad386793 2198 wxWindow *WXUNUSED(parent), wxRect WXUNUSED(labelRect), const wxVariant &WXUNUSED(value) )
c9c13e70
RR
2199{
2200 return NULL;
2201}
2202
7857346a 2203bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl(
ad386793 2204 wxControl* WXUNUSED(editor), wxVariant &WXUNUSED(value) )
c9c13e70
RR
2205{
2206 return false;
2207}
2208
93763ad5 2209// ---------------------------------------------------------
605c2c4a 2210// wxDataViewColumn
93763ad5 2211// ---------------------------------------------------------
7b4fde82 2212
31fb32e1
RR
2213
2214static gboolean
ad386793 2215gtk_dataview_header_button_press_callback( GtkWidget *WXUNUSED(widget),
31fb32e1
RR
2216 GdkEventButton *gdk_event,
2217 wxDataViewColumn *column )
2218{
2219 if (gdk_event->type != GDK_BUTTON_PRESS)
94b1f7bc 2220 return FALSE;
f4322df6 2221
31fb32e1
RR
2222 if (gdk_event->button == 1)
2223 {
d32332aa 2224 gs_lastLeftClickHeader = column;
7857346a 2225
31fb32e1
RR
2226 wxDataViewCtrl *dv = column->GetOwner();
2227 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
2228 event.SetDataViewColumn( column );
2229 event.SetModel( dv->GetModel() );
937013e0 2230 if (dv->HandleWindowEvent( event ))
a84c5b6f 2231 return FALSE;
31fb32e1 2232 }
f4322df6 2233
dadc879e
RR
2234 if (gdk_event->button == 3)
2235 {
2236 wxDataViewCtrl *dv = column->GetOwner();
2237 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, dv->GetId() );
2238 event.SetDataViewColumn( column );
2239 event.SetModel( dv->GetModel() );
937013e0 2240 if (dv->HandleWindowEvent( event ))
dadc879e
RR
2241 return FALSE;
2242 }
2243
94b1f7bc 2244 return FALSE;
31fb32e1
RR
2245}
2246
7b4fde82
RR
2247extern "C" {
2248static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
2249 GtkCellRenderer *cell,
2250 GtkTreeModel *model,
2251 GtkTreeIter *iter,
2252 gpointer data );
2253}
2254
2255
ad386793 2256static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *WXUNUSED(column),
7b4fde82
RR
2257 GtkCellRenderer *renderer,
2258 GtkTreeModel *model,
2259 GtkTreeIter *iter,
2260 gpointer data )
2261{
e0062c04
RR
2262 g_return_if_fail (GTK_IS_WX_TREE_MODEL (model));
2263 GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model;
93763ad5 2264
baa9ebc4 2265 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
a7f61f76 2266
9d52aad3 2267 wxDataViewItem item( (void*) iter->user_data );
93763ad5 2268
a826202e
RR
2269 wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel();
2270
2056dede
RR
2271 if (!wx_model->IsIndexListModel())
2272 {
b9db5f30 2273
a826202e
RR
2274 if (wx_model->IsContainer( item ))
2275 {
2276 if (wx_model->HasContainerColumns( item ) || (cell->GetOwner()->GetModelColumn() == 0))
2277 {
2278 GValue gvalue = { 0, };
2279 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2280 g_value_set_boolean( &gvalue, TRUE );
2281 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2282 g_value_unset( &gvalue );
2283 }
2284 else
2285 {
2286 GValue gvalue = { 0, };
2287 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2288 g_value_set_boolean( &gvalue, FALSE );
2289 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2290 g_value_unset( &gvalue );
b9db5f30 2291
a826202e
RR
2292 return;
2293 }
2294 }
2295 else
2296 {
2297 GValue gvalue = { 0, };
2298 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2299 g_value_set_boolean( &gvalue, TRUE );
2300 g_object_set_property( G_OBJECT(renderer), "visible", &gvalue );
2301 g_value_unset( &gvalue );
2302 }
b9db5f30 2303
2056dede 2304 }
a826202e 2305
3f3af7e7 2306 wxVariant value;
a826202e 2307 wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() );
7b4fde82
RR
2308
2309 if (value.GetType() != cell->GetVariantType())
f4322df6
VZ
2310 wxLogError( wxT("Wrong type, required: %s but: %s"),
2311 value.GetType().c_str(),
cbc9145c 2312 cell->GetVariantType().c_str() );
93763ad5 2313
7b4fde82 2314 cell->SetValue( value );
e0743e63 2315
4264606e
RR
2316 if (cell->GtkHasAttributes())
2317 {
2318 wxDataViewItemAttr attr;
2319 bool colour_set = false;
2320 bool style_set = false;
2321 bool weight_set = false;
b9db5f30 2322
4264606e
RR
2323 if (wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr ))
2324 {
2325 // this must be a GtkCellRendererText
2326 wxColour colour = attr.GetColour();
2327 if (colour.IsOk())
2328 {
2329 const GdkColor * const gcol = colour.GetColor();
2a5e6d1b 2330
4264606e
RR
2331 GValue gvalue = { 0, };
2332 g_value_init( &gvalue, GDK_TYPE_COLOR );
2333 g_value_set_boxed( &gvalue, gcol );
2334 g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
2335 g_value_unset( &gvalue );
b9db5f30 2336
4264606e
RR
2337 colour_set = true;
2338 }
b9db5f30 2339
4264606e
RR
2340 if (attr.GetItalic())
2341 {
2342 GValue gvalue = { 0, };
2343 g_value_init( &gvalue, PANGO_TYPE_STYLE );
2344 g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
2345 g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
2346 g_value_unset( &gvalue );
b9db5f30 2347
4264606e
RR
2348 style_set = true;
2349 }
b9db5f30 2350
4264606e
RR
2351 if (attr.GetBold())
2352 {
2353 GValue gvalue = { 0, };
2354 g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
2355 g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
2356 g_object_set_property( G_OBJECT(renderer), "weight", &gvalue );
2357 g_value_unset( &gvalue );
b9db5f30 2358
4264606e
RR
2359 weight_set = true;
2360 }
2361 }
b9db5f30 2362
4264606e
RR
2363 if (!style_set)
2364 {
2365 GValue gvalue = { 0, };
2366 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2367 g_value_set_boolean( &gvalue, FALSE );
2368 g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
2369 g_value_unset( &gvalue );
2370 }
b9db5f30 2371
4264606e
RR
2372 if (!weight_set)
2373 {
2374 GValue gvalue = { 0, };
2375 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2376 g_value_set_boolean( &gvalue, FALSE );
2377 g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue );
2378 g_value_unset( &gvalue );
2379 }
b9db5f30 2380
4264606e
RR
2381 if (!colour_set)
2382 {
2383 GValue gvalue = { 0, };
2384 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2385 g_value_set_boolean( &gvalue, FALSE );
2386 g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
2387 g_value_unset( &gvalue );
2388 }
2389 }
2390
2391#if 0
2a5e6d1b
RR
2392 if (attr.HasBackgroundColour())
2393 {
2394 wxColour colour = attr.GetBackgroundColour();
1cd5e95c 2395 const GdkColor * const gcol = colour.GetColor();
e0743e63 2396
2a5e6d1b
RR
2397 GValue gvalue = { 0, };
2398 g_value_init( &gvalue, GDK_TYPE_COLOR );
2399 g_value_set_boxed( &gvalue, gcol );
2400 g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue );
2401 g_value_unset( &gvalue );
2402 }
2403 else
2404 {
2405 GValue gvalue = { 0, };
2406 g_value_init( &gvalue, G_TYPE_BOOLEAN );
2407 g_value_set_boolean( &gvalue, FALSE );
2408 g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue );
2409 g_value_unset( &gvalue );
2410 }
e0062c04
RR
2411#endif
2412
7b4fde82
RR
2413}
2414
cbc9145c 2415IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
fa28826d 2416
91a6c655 2417#include <wx/listimpl.cpp>
a76c2f37 2418WX_DEFINE_LIST(wxDataViewColumnList)
91a6c655 2419
f4322df6
VZ
2420wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
2421 unsigned int model_column, int width,
87f0efe2
RR
2422 wxAlignment align, int flags ) :
2423 wxDataViewColumnBase( title, cell, model_column, width, align, flags )
fa28826d 2424{
9861f022 2425 Init( align, flags, width );
31fb32e1 2426
9861f022 2427 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column), TRUE );
31fb32e1 2428 SetTitle( title );
fa28826d
RR
2429}
2430
f4322df6
VZ
2431wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
2432 unsigned int model_column, int width,
87f0efe2
RR
2433 wxAlignment align, int flags ) :
2434 wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags )
9861f022
RR
2435{
2436 Init( align, flags, width );
2437
2438 SetBitmap( bitmap );
2439}
2440
2441void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
07a84e7b 2442{
31fb32e1 2443 m_isConnected = false;
07a84e7b 2444
9861f022 2445 GtkCellRenderer *renderer = (GtkCellRenderer *) GetRenderer()->GetGtkHandle();
07a84e7b 2446 GtkTreeViewColumn *column = gtk_tree_view_column_new();
9861f022 2447 m_column = (GtkWidget*) column;
07a84e7b 2448
9861f022
RR
2449 SetFlags( flags );
2450 SetAlignment( align );
07a84e7b 2451
ad386793 2452 SetWidth( width );
07a84e7b 2453
9861f022 2454 gtk_tree_view_column_pack_end( column, renderer, TRUE );
07a84e7b
RR
2455
2456 gtk_tree_view_column_set_cell_data_func( column, renderer,
9861f022 2457 wxGtkTreeCellDataFunc, (gpointer) GetRenderer(), NULL );
07a84e7b
RR
2458}
2459
fa28826d
RR
2460wxDataViewColumn::~wxDataViewColumn()
2461{
2462}
2463
31fb32e1
RR
2464void wxDataViewColumn::OnInternalIdle()
2465{
2466 if (m_isConnected)
2467 return;
f4322df6 2468
31fb32e1
RR
2469 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
2470 {
9861f022 2471 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
31fb32e1
RR
2472 if (column->button)
2473 {
2474 g_signal_connect(column->button, "button_press_event",
2475 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
f4322df6 2476
31fb32e1
RR
2477 m_isConnected = true;
2478 }
2479 }
2480}
2481
b94db696
RR
2482void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
2483{
2484 wxDataViewColumnBase::SetOwner( owner );
f4322df6 2485
9861f022 2486 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2487
b94db696 2488 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
b94db696
RR
2489}
2490
fa28826d
RR
2491void wxDataViewColumn::SetTitle( const wxString &title )
2492{
9861f022 2493 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2494
31fb32e1
RR
2495 if (m_isConnected)
2496 {
2497 // disconnect before column->button gets recreated
f4322df6 2498 g_signal_handlers_disconnect_by_func( column->button,
9861f022 2499 (GtkWidget*) gtk_dataview_header_button_press_callback, this);
f4322df6 2500
31fb32e1
RR
2501 m_isConnected = false;
2502 }
2503
09dfa6a0
VZ
2504 // FIXME: can it really happen that we don't have the owner here??
2505 wxDataViewCtrl *ctrl = GetOwner();
2506 gtk_tree_view_column_set_title( column, ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
2507 : wxGTK_CONV_SYS(title) );
31fb32e1 2508
07a84e7b
RR
2509 gtk_tree_view_column_set_widget( column, NULL );
2510}
2511
9861f022
RR
2512wxString wxDataViewColumn::GetTitle() const
2513{
2514 const gchar *str = gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column) );
2515 return wxConvFileName->cMB2WX(str);
2516}
2517
07a84e7b
RR
2518void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
2519{
2520 wxDataViewColumnBase::SetBitmap( bitmap );
2521
9861f022 2522 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
07a84e7b
RR
2523 if (bitmap.Ok())
2524 {
2525 GtkImage *gtk_image = GTK_IMAGE( gtk_image_new() );
f4322df6 2526
07a84e7b
RR
2527 GdkBitmap *mask = (GdkBitmap *) NULL;
2528 if (bitmap.GetMask())
2529 mask = bitmap.GetMask()->GetBitmap();
2530
2531 if (bitmap.HasPixbuf())
2532 {
2533 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
2534 bitmap.GetPixbuf());
2535 }
2536 else
2537 {
2538 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
2539 bitmap.GetPixmap(), mask);
2540 }
2541 gtk_widget_show( GTK_WIDGET(gtk_image) );
f4322df6 2542
07a84e7b
RR
2543 gtk_tree_view_column_set_widget( column, GTK_WIDGET(gtk_image) );
2544 }
2545 else
2546 {
2547 gtk_tree_view_column_set_widget( column, NULL );
2548 }
fa28826d
RR
2549}
2550
9861f022
RR
2551void wxDataViewColumn::SetHidden( bool hidden )
2552{
2553 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
2554}
2555
2556void wxDataViewColumn::SetResizeable( bool resizeable )
2557{
2558 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
2559}
2560
47cef10f
RR
2561void wxDataViewColumn::SetAlignment( wxAlignment align )
2562{
9861f022 2563 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2564
47cef10f
RR
2565 gfloat xalign = 0.0;
2566 if (align == wxALIGN_RIGHT)
2567 xalign = 1.0;
9861f022
RR
2568 if (align == wxALIGN_CENTER_HORIZONTAL ||
2569 align == wxALIGN_CENTER)
47cef10f 2570 xalign = 0.5;
f4322df6 2571
9861f022
RR
2572 gtk_tree_view_column_set_alignment( column, xalign );
2573}
2574
2575wxAlignment wxDataViewColumn::GetAlignment() const
2576{
2577 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
2578
2579 if (xalign == 1.0)
2580 return wxALIGN_RIGHT;
2581 if (xalign == 0.5)
2582 return wxALIGN_CENTER_HORIZONTAL;
f4322df6 2583
9861f022 2584 return wxALIGN_LEFT;
47cef10f
RR
2585}
2586
31fb32e1
RR
2587void wxDataViewColumn::SetSortable( bool sortable )
2588{
9861f022 2589 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
b9db5f30 2590
94b1f7bc 2591 if (sortable)
0bd26819 2592 {
94b1f7bc 2593 gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
0bd26819 2594 }
94b1f7bc 2595 else
0bd26819 2596 {
94b1f7bc 2597 gtk_tree_view_column_set_sort_column_id( column, -1 );
0bd26819
RR
2598 gtk_tree_view_column_set_sort_indicator( column, FALSE );
2599 }
31fb32e1
RR
2600}
2601
87f0efe2 2602bool wxDataViewColumn::IsSortable() const
31fb32e1 2603{
9861f022 2604 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
94b1f7bc 2605 return (gtk_tree_view_column_get_sort_column_id( column ) != -1);
31fb32e1
RR
2606}
2607
9861f022
RR
2608bool wxDataViewColumn::IsResizeable() const
2609{
2610 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2611 return gtk_tree_view_column_get_resizable( column );
2612}
2613
2614bool wxDataViewColumn::IsHidden() const
2615{
2616 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
2617 return !gtk_tree_view_column_get_visible( column );
2618}
2619
47cef10f
RR
2620void wxDataViewColumn::SetSortOrder( bool ascending )
2621{
9861f022 2622 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2623
47cef10f
RR
2624 if (ascending)
2625 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
2626 else
2627 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
0bd26819
RR
2628
2629 gtk_tree_view_column_set_sort_indicator( column, TRUE );
47cef10f
RR
2630}
2631
87f0efe2 2632bool wxDataViewColumn::IsSortOrderAscending() const
31fb32e1 2633{
9861f022 2634 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
f4322df6 2635
31fb32e1
RR
2636 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
2637}
2638
9861f022 2639void wxDataViewColumn::SetMinWidth( int width )
533544f2 2640{
9861f022 2641 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
533544f2
RR
2642}
2643
9861f022
RR
2644int wxDataViewColumn::GetMinWidth() const
2645{
2646 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
2647}
2648
2649int wxDataViewColumn::GetWidth() const
533544f2 2650{
9861f022 2651 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
533544f2
RR
2652}
2653
9861f022 2654void wxDataViewColumn::SetWidth( int width )
533544f2 2655{
ad386793
RR
2656 if (width < 0)
2657 {
2658 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
2659 }
2660 else
2661 {
2662 gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
7857346a 2663
ad386793
RR
2664 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
2665 }
533544f2
RR
2666}
2667
99c75ebc
RR
2668void wxDataViewColumn::SetReorderable( bool reorderable )
2669{
2670 gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
2671}
2672
2673bool wxDataViewColumn::IsReorderable() const
2674{
2675 return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) );
2676}
9861f022 2677
4508fcd2
RR
2678//-----------------------------------------------------------------------------
2679// wxGtkTreeModelNode
2680//-----------------------------------------------------------------------------
2681
2682void wxGtkTreeModelNode::Resort()
2683{
af110130
RR
2684 size_t child_count = GetChildCount();
2685 if (child_count == 0)
0be79c8a
RR
2686 return;
2687
af110130
RR
2688 size_t node_count = GetNodesCount();
2689
2690 if (child_count == 1)
0be79c8a 2691 {
af110130
RR
2692 if (node_count == 1)
2693 {
2694 wxGtkTreeModelNode *node = m_nodes.Item( 0 );
2695 node->Resort();
2696 }
0be79c8a
RR
2697 return;
2698 }
2699
40196b1e
RR
2700 wxGtkTreeModelChildren temp;
2701 WX_APPEND_ARRAY( temp, m_children );
b9db5f30 2702
40196b1e
RR
2703 g_internal = m_internal;
2704 m_children.Sort( &wxGtkTreeModelChildCmp );
0be79c8a 2705
af110130 2706 gint *new_order = new gint[child_count];
b9db5f30 2707
40196b1e 2708 unsigned int pos;
af110130 2709 for (pos = 0; pos < child_count; pos++)
4508fcd2 2710 {
40196b1e
RR
2711 void *id = m_children.Item( pos );
2712 int old_pos = temp.Index( id );
2713 new_order[pos] = old_pos;
4508fcd2 2714 }
b9db5f30 2715
0be79c8a
RR
2716 GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
2717
2718 GtkTreeIter iter;
40196b1e 2719 iter.user_data = GetItem().GetID();
0be79c8a 2720 iter.stamp = m_internal->GetGtkModel()->stamp;
b9db5f30 2721
40196b1e 2722 GtkTreePath *path = m_internal->get_path( &iter );
b9db5f30 2723
0be79c8a 2724 gtk_tree_model_rows_reordered( gtk_tree_model, path, &iter, new_order );
b9db5f30 2725
0be79c8a 2726 gtk_tree_path_free (path);
b9db5f30 2727
0be79c8a 2728 delete [] new_order;
b9db5f30 2729
af110130 2730 for (pos = 0; pos < node_count; pos++)
4508fcd2 2731 {
af110130 2732 wxGtkTreeModelNode *node = m_nodes.Item( pos );
4508fcd2
RR
2733 node->Resort();
2734 }
2735}
2736
55fbde12
RR
2737//-----------------------------------------------------------------------------
2738// wxDataViewCtrlInternal
2739//-----------------------------------------------------------------------------
2740
b9db5f30 2741wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner,
55fbde12 2742 wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model )
b9db5f30 2743{
55fbde12 2744 m_owner = owner;
b9db5f30
VS
2745 m_wx_model = wx_model;
2746 m_gtk_model = gtk_model;
2747 m_root = NULL;
b8b7b087 2748 m_sort_order = GTK_SORT_ASCENDING;
40196b1e 2749 m_sort_column = -1;
d32332aa 2750 m_dataview_sort_column = NULL;
b9db5f30 2751
2056dede
RR
2752 if (!m_wx_model->IsIndexListModel())
2753 InitTree();
55fbde12 2754}
b9db5f30 2755
55fbde12
RR
2756wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
2757{
2758 g_object_unref( m_gtk_model );
2759}
b9db5f30 2760
ef427989
RR
2761void wxDataViewCtrlInternal::InitTree()
2762{
2763 wxDataViewItem item;
0be79c8a 2764 m_root = new wxGtkTreeModelNode( NULL, item, this );
ef427989
RR
2765
2766 BuildBranch( m_root );
2767}
2768
55fbde12
RR
2769void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
2770{
2771 if (node->GetChildCount() == 0)
2772 {
74fe973b
RR
2773 wxDataViewItemArray children;
2774 unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
2775 unsigned int pos;
2776 for (pos = 0; pos < count; pos++)
55fbde12 2777 {
74fe973b 2778 wxDataViewItem child = children[pos];
b9db5f30 2779
af110130
RR
2780 if (m_wx_model->IsContainer( child ))
2781 node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
2782 else
2783 node->AddLeave( child.GetID() );
b9db5f30 2784
effd54b0 2785 // Don't send any events here
55fbde12
RR
2786 }
2787 }
2788}
2789
f6f0ef85
RR
2790// GTK+ dnd iface
2791
7857346a 2792gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
2793 GtkTreePath *path )
2794{
2795 GtkTreeIter iter;
2796 if (!get_iter( &iter, path )) return FALSE;
7857346a 2797
f6f0ef85
RR
2798 wxDataViewItem item( (void*) iter.user_data );
2799
2800 return m_wx_model->IsDraggable( item );
2801}
2802
7857346a
VZ
2803gboolean
2804wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
2805 GtkTreePath *WXUNUSED(path))
f6f0ef85
RR
2806{
2807 return FALSE;
2808}
2809
7857346a 2810gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
f6f0ef85
RR
2811 GtkTreePath *path, GtkSelectionData *selection_data )
2812{
2813 GtkTreeIter iter;
2814 if (!get_iter( &iter, path )) return FALSE;
7857346a 2815
f6f0ef85 2816 wxDataViewItem item( (void*) iter.user_data );
7857346a 2817
f6f0ef85 2818 wxDataFormat format( selection_data->target );
7857346a 2819
f6f0ef85
RR
2820 size_t size = m_wx_model->GetDragDataSize( item, format );
2821 if (size == 0) return FALSE;
7857346a 2822
f6f0ef85 2823 void *data = malloc( size );
7857346a 2824
f6f0ef85 2825 m_wx_model->GetDragData( item, format, data, size );
7857346a 2826
f6f0ef85
RR
2827 gtk_selection_data_set( selection_data, selection_data->target,
2828 8, (const guchar*) data, size );
2829
2830 free( data );
7857346a 2831
f6f0ef85
RR
2832 return TRUE;
2833}
2834
7857346a
VZ
2835gboolean
2836wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
2837 GtkTreePath *WXUNUSED(dest),
2838 GtkSelectionData *WXUNUSED(selection_data))
f6f0ef85
RR
2839{
2840 return FALSE;
2841}
2842
7857346a
VZ
2843gboolean
2844wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
2845 GtkTreePath *WXUNUSED(dest_path),
2846 GtkSelectionData *WXUNUSED(selection_data))
f6f0ef85
RR
2847{
2848 return FALSE;
2849}
2850
2851// notifications from wxDataViewModel
2852
33ba5a05
RR
2853bool wxDataViewCtrlInternal::Cleared()
2854{
2855 if (m_root)
2856 {
2857 delete m_root;
2858 InitTree();
7857346a
VZ
2859 }
2860
33ba5a05
RR
2861 // Create new GTK model
2862 g_object_unref( m_gtk_model );
2863 m_gtk_model = wxgtk_tree_model_new();
2864 m_gtk_model->internal = this;
7857346a 2865
33ba5a05
RR
2866 return true;
2867}
2868
4508fcd2
RR
2869void wxDataViewCtrlInternal::Resort()
2870{
2056dede
RR
2871 if (!m_wx_model->IsIndexListModel())
2872 m_root->Resort();
4508fcd2
RR
2873}
2874
55fbde12
RR
2875bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
2876{
2056dede
RR
2877 if (!m_wx_model->IsIndexListModel())
2878 {
2879 wxGtkTreeModelNode *parent_node = FindNode( parent );
2880 if (m_wx_model->IsContainer( item ))
2881 parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) );
2882 else
2883 parent_node->AddLeave( item.GetID() );
2884 }
b9db5f30 2885
55fbde12
RR
2886 return true;
2887}
2888
469d3e9b 2889bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
55fbde12 2890{
2056dede
RR
2891 if (!m_wx_model->IsIndexListModel())
2892 {
2893 wxGtkTreeModelNode *parent_node = FindNode( parent );
2894 parent_node->DeleteChild( item.GetID() );
2895 }
b9db5f30 2896
d8331a01
RR
2897 return true;
2898}
2899
2900bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
2901{
6608fdab 2902 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
2903 event.SetEventObject( m_owner );
2904 event.SetModel( m_owner->GetModel() );
2905 event.SetItem( item );
937013e0 2906 m_owner->HandleWindowEvent( event );
b9db5f30 2907
d8331a01
RR
2908 return true;
2909}
2910
2911bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int col )
2912{
6608fdab 2913 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
d8331a01
RR
2914 event.SetEventObject( m_owner );
2915 event.SetModel( m_owner->GetModel() );
2916 event.SetColumn( col );
6608fdab 2917 event.SetDataViewColumn( GetOwner()->GetColumn(col) );
d8331a01 2918 event.SetItem( item );
937013e0 2919 m_owner->HandleWindowEvent( event );
b9db5f30 2920
d8331a01
RR
2921 return true;
2922}
2923
f6f0ef85
RR
2924// GTK+ model iface
2925
2056dede
RR
2926GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
2927{
2928 if (m_wx_model->IsIndexListModel())
2929 return GTK_TREE_MODEL_LIST_ONLY;
2930 else
2931 return GTK_TREE_MODEL_ITERS_PERSIST;
2932}
2933
55fbde12
RR
2934gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
2935{
2056dede
RR
2936 if (m_wx_model->IsIndexListModel())
2937 {
2938 wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model;
b9db5f30 2939
2056dede
RR
2940 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
2941
f52984b8 2942 if (i >= wx_model->GetLastIndex() + 1)
2056dede 2943 return FALSE;
55fbde12 2944
2056dede
RR
2945 iter->stamp = m_gtk_model->stamp;
2946 // user_data is just the index
2947 iter->user_data = (gpointer) i;
55fbde12 2948
2056dede
RR
2949 return TRUE;
2950 }
2951 else
55fbde12 2952 {
2056dede
RR
2953 int depth = gtk_tree_path_get_depth( path );
2954
2955 wxGtkTreeModelNode *node = m_root;
2956
2957 int i;
b9db5f30 2958 for (i = 0; i < depth; i++)
2056dede
RR
2959 {
2960 BuildBranch( node );
b9db5f30 2961
2056dede
RR
2962 gint pos = gtk_tree_path_get_indices (path)[i];
2963 if (pos < 0) return FALSE;
2964 if ((size_t)pos >= node->GetChildCount()) return FALSE;
b9db5f30 2965
2056dede 2966 void* id = node->GetChildren().Item( (size_t) pos );
b9db5f30 2967
2056dede 2968 if (i == depth-1)
af110130 2969 {
2056dede
RR
2970 iter->stamp = m_gtk_model->stamp;
2971 iter->user_data = id;
2972 return TRUE;
af110130 2973 }
2056dede
RR
2974
2975 size_t count = node->GetNodes().GetCount();
2976 size_t pos2;
2977 for (pos2 = 0; pos2 < count; pos2++)
2978 {
2979 wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
2980 if (child_node->GetItem().GetID() == id)
2981 {
2982 node = child_node;
2983 break;
2984 }
b9db5f30 2985 }
2056dede 2986 }
af110130 2987 }
55fbde12 2988
af110130 2989 return FALSE;
55fbde12
RR
2990}
2991
2992GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
2993{
2994 GtkTreePath *retval = gtk_tree_path_new ();
b9db5f30 2995
2056dede
RR
2996 if (m_wx_model->IsIndexListModel())
2997 {
2998 // user_data is just the index
2999 int i = (wxUIntPtr) iter->user_data;
3000 gtk_tree_path_append_index (retval, i);
3001 }
3002 else
55fbde12 3003 {
b9db5f30
VS
3004 void *id = iter->user_data;
3005
2056dede
RR
3006 wxGtkTreeModelNode *node = FindParentNode( iter );
3007 while (node)
3008 {
3009 int pos = node->GetChildren().Index( id );
40196b1e 3010
2056dede 3011 gtk_tree_path_prepend_index( retval, pos );
b9db5f30 3012
2056dede
RR
3013 id = node->GetItem().GetID();
3014 node = node->GetParent();
3015 }
55fbde12 3016 }
b9db5f30 3017
55fbde12
RR
3018 return retval;
3019}
3020
3021gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
3022{
2056dede
RR
3023 if (m_wx_model->IsIndexListModel())
3024 {
3025 wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model;
b9db5f30 3026
2056dede 3027 int n = (wxUIntPtr) iter->user_data;
69892729 3028
2056dede
RR
3029 if (n == -1)
3030 return FALSE;
40196b1e 3031
f52984b8 3032 if (n >= (int) wx_model->GetLastIndex())
2056dede
RR
3033 return FALSE;
3034
3035 iter->user_data = (gpointer) ++n;
3036 }
3037 else
3038 {
3039 wxGtkTreeModelNode *parent = FindParentNode( iter );
3040 if( parent == NULL )
3041 return FALSE;
3042
3043 int pos = parent->GetChildren().Index( iter->user_data );
3044
3045 if (pos == (int) parent->GetChildCount()-1)
3046 return FALSE;
b9db5f30 3047
2056dede
RR
3048 iter->stamp = m_gtk_model->stamp;
3049 iter->user_data = parent->GetChildren().Item( pos+1 );
3050 }
55fbde12
RR
3051
3052 return TRUE;
3053}
3054
3055gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent )
3056{
2056dede
RR
3057 if (m_wx_model->IsIndexListModel())
3058 {
3059 // this is a list, nodes have no children
3060 if (parent)
3061 return FALSE;
3062
3063 iter->stamp = m_gtk_model->stamp;
3064 iter->user_data = (gpointer) -1;
3065
3066 return TRUE;
3067 }
3068 else
3069 {
3070 wxDataViewItem item( (void*) parent->user_data );
b9db5f30 3071
2056dede
RR
3072 if (!m_wx_model->IsContainer( item ))
3073 return FALSE;
b9db5f30 3074
2056dede
RR
3075 wxGtkTreeModelNode *parent_node = FindNode( parent );
3076 BuildBranch( parent_node );
b9db5f30 3077
2056dede
RR
3078 if (parent_node->GetChildCount() == 0)
3079 return FALSE;
b9db5f30 3080
2056dede
RR
3081 iter->stamp = m_gtk_model->stamp;
3082 iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 );
3083 }
b9db5f30 3084
55fbde12
RR
3085 return TRUE;
3086}
3087
3088gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter )
3089{
2056dede
RR
3090 if (m_wx_model->IsIndexListModel())
3091 {
3092 // this is a list, nodes have no children
3093 return FALSE;
3094 }
3095 else
3096 {
3097 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3098
2056dede 3099 bool is_container = m_wx_model->IsContainer( item );
b9db5f30 3100
2056dede
RR
3101 if (!is_container)
3102 return FALSE;
b9db5f30 3103
2056dede
RR
3104 wxGtkTreeModelNode *node = FindNode( iter );
3105 BuildBranch( node );
b9db5f30 3106
2056dede
RR
3107 return (node->GetChildCount() > 0);
3108 }
55fbde12
RR
3109}
3110
3111gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter )
3112{
2056dede
RR
3113 if (m_wx_model->IsIndexListModel())
3114 {
3115 wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model;
b9db5f30 3116
2056dede 3117 if (iter == NULL)
f52984b8 3118 return (gint) wx_model->GetLastIndex() + 1;
2056dede 3119
55fbde12 3120 return 0;
2056dede
RR
3121 }
3122 else
3123 {
3124 wxDataViewItem item( (void*) iter->user_data );
b9db5f30 3125
2056dede
RR
3126 if (!m_wx_model->IsContainer( item ))
3127 return 0;
b9db5f30 3128
2056dede
RR
3129 wxGtkTreeModelNode *parent_node = FindNode( iter );
3130 BuildBranch( parent_node );
ef427989 3131
2056dede 3132 // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() );
b9db5f30 3133
2056dede
RR
3134 return parent_node->GetChildCount();
3135 }
55fbde12
RR
3136}
3137
3138gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n )
3139{
2056dede
RR
3140 if (m_wx_model->IsIndexListModel())
3141 {
3142 wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model;
b9db5f30 3143
2056dede
RR
3144 if (parent)
3145 return FALSE;
3146
3147 if (n < 0)
3148 return FALSE;
3149
f52984b8 3150 if (n >= (gint) wx_model->GetLastIndex() + 1)
2056dede
RR
3151 return FALSE;
3152
3153 iter->stamp = m_gtk_model->stamp;
3154 iter->user_data = (gpointer) n;
3155
3156 return TRUE;
3157 }
3158 else
3159 {
3160 void* id = NULL;
3161 if (parent) id = (void*) parent->user_data;
3162 wxDataViewItem item( id );
b9db5f30 3163
2056dede
RR
3164 if (!m_wx_model->IsContainer( item ))
3165 return FALSE;
b9db5f30 3166
2056dede
RR
3167 wxGtkTreeModelNode *parent_node = FindNode( parent );
3168 BuildBranch( parent_node );
55fbde12 3169
2056dede 3170 // wxPrintf( "iter_nth_child %d\n", n );
b9db5f30 3171
2056dede
RR
3172 iter->stamp = m_gtk_model->stamp;
3173 iter->user_data = parent_node->GetChildren().Item( n );
55fbde12 3174
2056dede
RR
3175 return TRUE;
3176 }
55fbde12
RR
3177}
3178
3179gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child )
3180{
2056dede
RR
3181 if (m_wx_model->IsIndexListModel())
3182 {
55fbde12 3183 return FALSE;
2056dede
RR
3184 }
3185 else
3186 {
3187 wxGtkTreeModelNode *node = FindParentNode( child );
3188 if (!node)
3189 return FALSE;
b9db5f30 3190
2056dede
RR
3191 iter->stamp = m_gtk_model->stamp;
3192 iter->user_data = (gpointer) node->GetItem().GetID();
55fbde12 3193
2056dede
RR
3194 return TRUE;
3195 }
55fbde12 3196}
b9db5f30 3197
55fbde12 3198static wxGtkTreeModelNode*
69892729 3199wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
55fbde12 3200{
69892729
RR
3201 if( model == NULL )
3202 return NULL;
55fbde12 3203
69892729
RR
3204 ItemList list;
3205 list.DeleteContents( true );
3206 wxDataViewItem it( item );
b9db5f30 3207
69892729 3208 while( it.IsOk() )
55fbde12 3209 {
69892729
RR
3210 wxDataViewItem * pItem = new wxDataViewItem( it );
3211 list.Insert( pItem );
3212 it = model->GetParent( it );
3213 }
3214
3215 wxGtkTreeModelNode * node = treeNode;
966cb94d 3216 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
69892729
RR
3217 {
3218 if( node && node->GetNodes().GetCount() != 0 )
ef427989 3219 {
69892729
RR
3220 int len = node->GetNodes().GetCount();
3221 wxGtkTreeModelNodes nodes = node->GetNodes();
3222 int j = 0;
3223 for( ; j < len; j ++)
3224 {
3225 if( nodes[j]->GetItem() == *(n->GetData()))
3226 {
3227 node = nodes[j];
3228 break;
b9db5f30 3229 }
69892729
RR
3230 }
3231
3232 if( j == len )
3233 {
3234 return NULL;
3235 }
ef427989 3236 }
69892729
RR
3237 else
3238 return NULL;
55fbde12 3239 }
69892729
RR
3240 return node;
3241
55fbde12
RR
3242}
3243
3244wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter )
3245{
3246 if (!iter)
3247 return m_root;
3248
3249 wxDataViewItem item( (void*) iter->user_data );
0be79c8a
RR
3250 if (!item.IsOk())
3251 return m_root;
b9db5f30 3252
69892729 3253 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
b9db5f30 3254
55fbde12
RR
3255 if (!result)
3256 {
26f119eb 3257 wxLogDebug( "Not found %p", iter->user_data );
55fbde12
RR
3258 char *crash = NULL;
3259 *crash = 0;
3260 }
b9db5f30 3261
55fbde12
RR
3262 return result;
3263}
3264
3265wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item )
3266{
0be79c8a
RR
3267 if (!item.IsOk())
3268 return m_root;
3269
69892729 3270 wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item );
b9db5f30 3271
55fbde12
RR
3272 if (!result)
3273 {
26f119eb 3274 wxLogDebug( "Not found %p", item.GetID() );
55fbde12
RR
3275 char *crash = NULL;
3276 *crash = 0;
3277 }
b9db5f30 3278
55fbde12
RR
3279 return result;
3280}
3281
af110130 3282static wxGtkTreeModelNode*
69892729 3283wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item )
af110130 3284{
69892729
RR
3285 if( model == NULL )
3286 return NULL;
3287
3288 ItemList list;
3289 list.DeleteContents( true );
3290 if( !item.IsOk() )
3291 return NULL;
3292
3293 wxDataViewItem it( model->GetParent( item ) );
3294 while( it.IsOk() )
af110130 3295 {
69892729
RR
3296 wxDataViewItem * pItem = new wxDataViewItem( it );
3297 list.Insert( pItem );
3298 it = model->GetParent( it );
af110130
RR
3299 }
3300
69892729 3301 wxGtkTreeModelNode * node = treeNode;
966cb94d 3302 for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() )
af110130 3303 {
69892729 3304 if( node && node->GetNodes().GetCount() != 0 )
af110130 3305 {
69892729
RR
3306 int len = node->GetNodes().GetCount();
3307 wxGtkTreeModelNodes nodes = node->GetNodes();
3308 int j = 0;
3309 for( ; j < len; j ++)
3310 {
3311 if( nodes[j]->GetItem() == *(n->GetData()))
3312 {
3313 node = nodes[j];
3314 break;
b9db5f30 3315 }
69892729
RR
3316 }
3317
3318 if( j == len )
3319 {
3320 return NULL;
3321 }
af110130 3322 }
69892729
RR
3323 else
3324 return NULL;
3325 }
3326 //Examine whether the node is item's parent node
3327 int len = node->GetChildCount();
3328 for( int i = 0; i < len ; i ++ )
3329 {
3330 if( node->GetChildren().Item( i ) == item.GetID() )
3331 return node;
af110130 3332 }
af110130
RR
3333 return NULL;
3334}
3335
3336wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter )
3337{
3338 if (!iter)
3339 return NULL;
b9db5f30 3340
af110130
RR
3341 wxDataViewItem item( (void*) iter->user_data );
3342 if (!item.IsOk())
3343 return NULL;
3344
69892729 3345 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
3346}
3347
3348wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item )
3349{
3350 if (!item.IsOk())
3351 return NULL;
3352
69892729 3353 return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item );
af110130
RR
3354}
3355
eb7f97f8
RR
3356//-----------------------------------------------------------------------------
3357// wxDataViewCtrl signal callbacks
3358//-----------------------------------------------------------------------------
3359
3360static void
ad386793 3361wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv )
eb7f97f8 3362{
b94db696
RR
3363 if (!GTK_WIDGET_REALIZED(dv->m_widget))
3364 return;
f4322df6 3365
d86c1870 3366 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() );
6848478c 3367 event.SetItem( dv->GetSelection() );
eb7f97f8 3368 event.SetModel( dv->GetModel() );
937013e0 3369 dv->HandleWindowEvent( event );
eb7f97f8
RR
3370}
3371
f828871d 3372static void
ad386793
RR
3373wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path,
3374 GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv )
f828871d 3375{
e0062c04 3376 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() );
55fbde12
RR
3377
3378 GtkTreeIter iter;
3379 dv->GtkGetInternal()->get_iter( &iter, path );
3380 wxDataViewItem item( (void*) iter.user_data );;
3381 event.SetItem( item );
f828871d 3382 event.SetModel( dv->GetModel() );
937013e0 3383 dv->HandleWindowEvent( event );
f828871d
RR
3384}
3385
718fd180 3386static gboolean
ad386793
RR
3387wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3388 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3389{
3390 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() );
3391
3392 wxDataViewItem item( (void*) iter->user_data );;
3393 event.SetItem( item );
3394 event.SetModel( dv->GetModel() );
937013e0 3395 dv->HandleWindowEvent( event );
b9db5f30 3396
718fd180
RR
3397 return !event.IsAllowed();
3398}
3399
3400static void
ad386793
RR
3401wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3402 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3403{
3404 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() );
3405
3406 wxDataViewItem item( (void*) iter->user_data );;
3407 event.SetItem( item );
3408 event.SetModel( dv->GetModel() );
937013e0 3409 dv->HandleWindowEvent( event );
718fd180
RR
3410}
3411
3412static gboolean
ad386793
RR
3413wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3414 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3415{
3416 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() );
3417
3418 wxDataViewItem item( (void*) iter->user_data );;
3419 event.SetItem( item );
3420 event.SetModel( dv->GetModel() );
937013e0 3421 dv->HandleWindowEvent( event );
b9db5f30 3422
718fd180
RR
3423 return !event.IsAllowed();
3424}
3425
3426static void
ad386793
RR
3427wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter,
3428 GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv )
718fd180
RR
3429{
3430 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() );
3431
3432 wxDataViewItem item( (void*) iter->user_data );;
3433 event.SetItem( item );
3434 event.SetModel( dv->GetModel() );
937013e0 3435 dv->HandleWindowEvent( event );
718fd180
RR
3436}
3437
790b137e 3438//-----------------------------------------------------------------------------
718fd180 3439 // wxDataViewCtrl
790b137e
RR
3440//-----------------------------------------------------------------------------
3441
1e510b1e
RR
3442//-----------------------------------------------------------------------------
3443// InsertChild for wxDataViewCtrl
3444//-----------------------------------------------------------------------------
3445
3446static void wxInsertChildInDataViewCtrl( wxWindowGTK* parent, wxWindowGTK* child )
3447{
3448 wxDataViewCtrl * dvc = (wxDataViewCtrl*) parent;
3449 GtkWidget *treeview = dvc->GtkGetTreeView();
3450
3451 // Insert widget in GtkTreeView
3452 if (GTK_WIDGET_REALIZED(treeview))
e8375af8 3453 gtk_widget_set_parent_window( child->m_widget,
1e510b1e
RR
3454 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
3455 gtk_widget_set_parent( child->m_widget, treeview );
3456}
3457
3458static
3459void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
ad386793 3460 GtkAllocation *WXUNUSED(gtk_alloc),
1e510b1e
RR
3461 wxDataViewCtrl *win )
3462{
966cb94d 3463 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
1e510b1e
RR
3464 while (node)
3465 {
3466 wxWindow *child = node->GetData();
e8375af8 3467
1e510b1e
RR
3468 GtkRequisition req;
3469 gtk_widget_size_request( child->m_widget, &req );
e8375af8 3470
1e510b1e
RR
3471 GtkAllocation alloc;
3472 alloc.x = child->m_x;
3473 alloc.y = child->m_y;
3474 alloc.width = child->m_width;
3475 alloc.height = child->m_height;
3476 gtk_widget_size_allocate( child->m_widget, &alloc );
e8375af8 3477
1e510b1e
RR
3478 node = node->GetNext();
3479 }
3480}
3481
3482
6608fdab
RR
3483//-----------------------------------------------------------------------------
3484// "motion_notify_event"
3485//-----------------------------------------------------------------------------
3486
3487static gboolean
ad386793 3488gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget),
74dea0de
RR
3489 GdkEventMotion *gdk_event,
3490 wxDataViewCtrl *dv )
6608fdab
RR
3491{
3492 if (gdk_event->is_hint)
3493 {
3494 int x = 0;
3495 int y = 0;
3496 GdkModifierType state;
3497 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
3498 gdk_event->x = x;
3499 gdk_event->y = y;
3500 }
3501
3502 GtkTreePath *path = NULL;
3503 GtkTreeViewColumn *column = NULL;
3504 gint cell_x = 0;
3505 gint cell_y = 0;
b9db5f30 3506 if (gtk_tree_view_get_path_at_pos(
6608fdab
RR
3507 GTK_TREE_VIEW(dv->GtkGetTreeView()),
3508 (int) gdk_event->x, (int) gdk_event->y,
3509 &path,
3510 &column,
3511 &cell_x,
3512 &cell_y))
3513 {
3514 if (path)
3515 {
3516 GtkTreeIter iter;
3517 dv->GtkGetInternal()->get_iter( &iter, path );
b9db5f30 3518
6608fdab 3519 // wxPrintf( "mouse %d %d\n", (int) gdk_event->x, (int) gdk_event->y );
b9db5f30 3520
6608fdab
RR
3521 gtk_tree_path_free( path );
3522 }
3523 }
3524
3525
3526 return FALSE;
3527}
3528
74dea0de
RR
3529//-----------------------------------------------------------------------------
3530// "button_press_event"
3531//-----------------------------------------------------------------------------
3532
3533static gboolean
3534gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget),
3535 GdkEventButton *gdk_event,
3536 wxDataViewCtrl *dv )
3537{
3538 if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS))
3539 {
3540 GtkTreePath *path = NULL;
3541 GtkTreeViewColumn *column = NULL;
3542 gint cell_x = 0;
3543 gint cell_y = 0;
3544 if (gtk_tree_view_get_path_at_pos(
3545 GTK_TREE_VIEW(dv->GtkGetTreeView()),
3546 (int) gdk_event->x, (int) gdk_event->y,
3547 &path,
3548 &column,
3549 &cell_x,
3550 &cell_y))
3551 {
3552 if (path)
3553 {
3554 GtkTreeIter iter;
3555 dv->GtkGetInternal()->get_iter( &iter, path );
3556
3557 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() );
3558 wxDataViewItem item( (void*) iter.user_data );;
3559 event.SetItem( item );
3560 event.SetModel( dv->GetModel() );
3561 bool ret = dv->HandleWindowEvent( event );
3562 gtk_tree_path_free( path );
3563 return ret;
3564 }
3565 }
3566 }
3567
3568 return FALSE;
3569}
1e510b1e 3570
239eaa41
RR
3571IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
3572
3573wxDataViewCtrl::~wxDataViewCtrl()
3574{
8f850e28
RR
3575 if (m_notifier)
3576 GetModel()->RemoveNotifier( m_notifier );
87f0efe2
RR
3577
3578 // remove the model from the GtkTreeView before it gets destroyed by the
3579 // wxDataViewCtrlBase's dtor
3580 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL );
b9db5f30 3581
55fbde12 3582 delete m_internal;
239eaa41
RR
3583}
3584
3585void wxDataViewCtrl::Init()
3586{
8f850e28 3587 m_notifier = NULL;
33ba5a05 3588 m_internal = NULL;
239eaa41
RR
3589}
3590
f6f0ef85
RR
3591static GtkTargetEntry gs_target;
3592
239eaa41 3593bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
93763ad5 3594 const wxPoint& pos, const wxSize& size,
239eaa41
RR
3595 long style, const wxValidator& validator )
3596{
3597 Init();
93763ad5 3598
239eaa41
RR
3599 if (!PreCreation( parent, pos, size ) ||
3600 !CreateBase( parent, id, pos, size, style, validator ))
3601 {
3602 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
93763ad5 3603 return false;
239eaa41 3604 }
1a367564 3605
1e510b1e
RR
3606 m_insertCallback = wxInsertChildInDataViewCtrl;
3607
1a367564 3608 m_widget = gtk_scrolled_window_new (NULL, NULL);
6493aaca
VZ
3609
3610 GtkScrolledWindowSetBorder(m_widget, style);
1a367564
RR
3611
3612 m_treeview = gtk_tree_view_new();
3613 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
e8375af8 3614
1e510b1e
RR
3615 g_signal_connect (m_treeview, "size_allocate",
3616 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
ed4b0fdc 3617
25bc5c55 3618 gs_target.target = const_cast<char *>("UTF8_STRING");
f6f0ef85
RR
3619 gs_target.flags = 0;
3620 gs_target.info = -1;
7857346a 3621 gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_treeview),
f6f0ef85 3622 GDK_BUTTON1_MASK, &gs_target, 1, (GdkDragAction) GDK_ACTION_COPY );
7857346a 3623
b94db696
RR
3624#ifdef __WXGTK26__
3625 if (!gtk_check_version(2,6,0))
3626 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), TRUE );
3627#endif
3628
daebb44c
RR
3629 if (style & wxDV_MULTIPLE)
3630 {
3631 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3632 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
3633 }
93763ad5 3634
9861f022
RR
3635 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
3636
3637#ifdef __WXGTK210__
3638 if (!gtk_check_version(2,10,0))
3639 {
3640 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
f4322df6
VZ
3641
3642 if ((style & wxDV_HORIZ_RULES) != 0 &&
9861f022
RR
3643 (style & wxDV_VERT_RULES) != 0)
3644 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
3645 else if (style & wxDV_VERT_RULES)
3646 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
3647 else if (style & wxDV_HORIZ_RULES)
3648 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
3649
1a07a730
RR
3650 if (grid != GTK_TREE_VIEW_GRID_LINES_NONE)
3651 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
9861f022 3652 }
9861f022 3653#endif
1a07a730
RR
3654
3655 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 );
9861f022 3656
1a367564
RR
3657 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
3658 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
3659 gtk_widget_show (m_treeview);
93763ad5 3660
239eaa41
RR
3661 m_parent->DoAddChild( this );
3662
b94db696
RR
3663 PostCreation(size);
3664
b086d55f
RR
3665 GtkEnableSelectionEvents();
3666
718fd180 3667 g_signal_connect_after (m_treeview, "row-activated",
f828871d 3668 G_CALLBACK (wxdataview_row_activated_callback), this);
eb7f97f8 3669
718fd180
RR
3670 g_signal_connect (m_treeview, "test-collapse-row",
3671 G_CALLBACK (wxdataview_test_collapse_row_callback), this);
3672
3673 g_signal_connect_after (m_treeview, "row-collapsed",
3674 G_CALLBACK (wxdataview_row_collapsed_callback), this);
3675
3676 g_signal_connect (m_treeview, "test-expand-row",
3677 G_CALLBACK (wxdataview_test_expand_row_callback), this);
b9db5f30 3678
718fd180
RR
3679 g_signal_connect_after (m_treeview, "row-expanded",
3680 G_CALLBACK (wxdataview_row_expanded_callback), this);
3681
6608fdab
RR
3682 g_signal_connect (m_treeview, "motion_notify_event",
3683 G_CALLBACK (gtk_dataview_motion_notify_callback), this);
3684
74dea0de
RR
3685 g_signal_connect (m_treeview, "button_press_event",
3686 G_CALLBACK (gtk_dataview_button_press_callback), this);
7857346a 3687
239eaa41
RR
3688 return true;
3689}
3690
31fb32e1
RR
3691void wxDataViewCtrl::OnInternalIdle()
3692{
3693 wxWindow::OnInternalIdle();
f4322df6 3694
9861f022 3695 unsigned int cols = GetColumnCount();
31fb32e1
RR
3696 unsigned int i;
3697 for (i = 0; i < cols; i++)
3698 {
3699 wxDataViewColumn *col = GetColumn( i );
3700 col->OnInternalIdle();
3701 }
3702}
3703
e0062c04 3704bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
239eaa41 3705{
33ba5a05
RR
3706 if (GetModel())
3707 {
3708 delete m_internal;
3709 m_internal = NULL;
7857346a 3710
33ba5a05
RR
3711 delete m_notifier;
3712 m_notifier = NULL;
3713 }
7857346a 3714
239eaa41
RR
3715 if (!wxDataViewCtrlBase::AssociateModel( model ))
3716 return false;
3717
55fbde12
RR
3718 GtkWxTreeModel *gtk_model = wxgtk_tree_model_new();
3719 m_internal = new wxDataViewCtrlInternal( this, model, gtk_model );
3720 gtk_model->internal = m_internal;
6e2e590f 3721
55fbde12 3722 m_notifier = new wxGtkDataViewModelNotifier( gtk_model, model, this );
6e2e590f 3723
93763ad5 3724 model->AddNotifier( m_notifier );
1557c77b 3725
55fbde12 3726 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_model) );
b9db5f30 3727
55fbde12
RR
3728 // unref in wxDataViewCtrlInternal
3729 // g_object_unref( gtk_model );
93763ad5 3730
239eaa41
RR
3731 return true;
3732}
790b137e 3733
fa28826d
RR
3734bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
3735{
3736 if (!wxDataViewCtrlBase::AppendColumn(col))
3737 return false;
93763ad5 3738
91a6c655
RR
3739 m_cols.Append( col );
3740
ad386793
RR
3741#ifdef __WXGTK26__
3742 if (!gtk_check_version(2,6,0))
3743 {
3744 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
3745 GTK_TREE_VIEW_COLUMN_FIXED)
3746 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
3747 }
3748#endif
3749
b9db5f30 3750 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
3751 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
3752
3753 return true;
3754}
3755
736fe67c
RR
3756bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
3757{
3758 if (!wxDataViewCtrlBase::PrependColumn(col))
3759 return false;
3760
3761 m_cols.Insert( col );
3762
ad386793
RR
3763#ifdef __WXGTK26__
3764 if (!gtk_check_version(2,6,0))
3765 {
3766 if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) !=
3767 GTK_TREE_VIEW_COLUMN_FIXED)
3768 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE );
3769 }
3770#endif
3771
b9db5f30 3772 gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview),
736fe67c
RR
3773 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 );
3774
3775 return true;
3776}
3777
91a6c655
RR
3778unsigned int wxDataViewCtrl::GetColumnCount() const
3779{
3780 return m_cols.GetCount();
3781}
3782
3783wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
3784{
3785 GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
3786 if (!gtk_col)
3787 return NULL;
b9db5f30 3788
91a6c655
RR
3789 wxDataViewColumnList::const_iterator iter;
3790 for (iter = m_cols.begin(); iter != m_cols.end(); iter++)
3791 {
3792 wxDataViewColumn *col = *iter;
3793 if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
3794 {
3795 return col;
3796 }
3797 }
b9db5f30 3798
91a6c655
RR
3799 return NULL;
3800}
3801
3802bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
3803{
b9db5f30 3804 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
3805 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
3806
3807 m_cols.remove( column );
fa28826d 3808
91a6c655 3809 delete column;
fa28826d
RR
3810
3811 return true;
3812}
3813
91a6c655
RR
3814bool wxDataViewCtrl::ClearColumns()
3815{
3816 wxDataViewColumnList::iterator iter;
3817 for (iter = m_cols.begin(); iter != m_cols.end(); iter++)
3818 {
3819 wxDataViewColumn *col = *iter;
b9db5f30 3820 gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview),
91a6c655
RR
3821 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
3822 }
b9db5f30 3823
91a6c655 3824 m_cols.clear();
b9db5f30 3825
91a6c655
RR
3826 return true;
3827}
3828
453091c2
RR
3829int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
3830{
aed836f3
RR
3831 GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetConstGtkHandle());
3832
3833 GList *list = gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview) );
b9db5f30 3834
aed836f3 3835 gint pos = g_list_index( list, (gconstpointer) gtk_column );
b9db5f30 3836
aed836f3
RR
3837 g_list_free( list );
3838
3839 return pos;
453091c2
RR
3840}
3841
21f47fb9
RR
3842wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
3843{
d32332aa 3844 return m_internal->GetDataViewSortColumn();
21f47fb9
RR
3845}
3846
f71d3ba4
RR
3847void wxDataViewCtrl::Expand( const wxDataViewItem & item )
3848{
3849 GtkTreeIter iter;
3850 iter.user_data = item.GetID();
3851 GtkTreePath *path = m_internal->get_path( &iter );
3852 gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false );
3853 gtk_tree_path_free( path );
3854}
3855
3856void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
3857{
3858 GtkTreeIter iter;
3859 iter.user_data = item.GetID();
3860 GtkTreePath *path = m_internal->get_path( &iter );
3861 gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path );
3862 gtk_tree_path_free( path );
3863}
3864
fbda518c 3865wxDataViewItem wxDataViewCtrl::GetSelection() const
1e08ad10
RR
3866{
3867 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 3868
1e08ad10
RR
3869 if (m_windowStyle & wxDV_MULTIPLE)
3870 {
6848478c
RR
3871 // Report the first one
3872 GtkTreeModel *model;
3873 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
b9db5f30 3874
6848478c
RR
3875 if (list)
3876 {
3877 GtkTreePath *path = (GtkTreePath*) list->data;
3878 GtkTreeIter iter;
3879 m_internal->get_iter( &iter, path );
b9db5f30 3880
6848478c
RR
3881 // delete list
3882 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
3883 g_list_free( list );
b9db5f30 3884
6848478c
RR
3885 return wxDataViewItem( (void*) iter.user_data );
3886 }
1e08ad10
RR
3887 }
3888 else
3889 {
3890 GtkTreeIter iter;
3891 if (gtk_tree_selection_get_selected( selection, NULL, &iter ))
3892 {
9d52aad3 3893 wxDataViewItem item( (void*) iter.user_data );
1e08ad10
RR
3894 return item;
3895 }
3896 }
b9db5f30 3897
1e08ad10
RR
3898 return wxDataViewItem(0);
3899}
3900
718fd180
RR
3901int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
3902{
6848478c
RR
3903 sel.Clear();
3904
3905 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3906 if (HasFlag(wxDV_MULTIPLE))
3907 {
3908 GtkTreeModel *model;
3909 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
3910
3911 int count = 0;
3912 while (list)
3913 {
3914 GtkTreePath *path = (GtkTreePath*) list->data;
b9db5f30 3915
6848478c
RR
3916 GtkTreeIter iter;
3917 m_internal->get_iter( &iter, path );
3918
3919 sel.Add( wxDataViewItem( (void*) iter.user_data ) );
3920
3921 list = g_list_next( list );
3922 count++;
3923 }
3924
3925 // delete list
3926 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
3927 g_list_free( list );
3928
3929 return count;
3930 }
3931 else
3932 {
3933 GtkTreeModel *model;
3934 GtkTreeIter iter;
3935 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
3936 if (has_selection)
3937 {
3938 sel.Add( wxDataViewItem( (void*) iter.user_data) );
3939 return 1;
3940 }
3941 }
3942
718fd180
RR
3943 return 0;
3944}
3945
3946void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
3947{
6848478c 3948 GtkDisableSelectionEvents();
b9db5f30 3949
6848478c
RR
3950 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3951
3952 gtk_tree_selection_unselect_all( selection );
b9db5f30 3953
6848478c
RR
3954 size_t i;
3955 for (i = 0; i < sel.GetCount(); i++)
3956 {
3957 GtkTreeIter iter;
3958 iter.user_data = (gpointer) sel[i].GetID();
3959 gtk_tree_selection_select_iter( selection, &iter );
3960 }
b9db5f30 3961
6848478c 3962 GtkEnableSelectionEvents();
718fd180
RR
3963}
3964
3965void wxDataViewCtrl::Select( const wxDataViewItem & item )
3966{
6848478c
RR
3967 GtkDisableSelectionEvents();
3968
3969 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3970
3971 GtkTreeIter iter;
3972 iter.user_data = (gpointer) item.GetID();
3973 gtk_tree_selection_select_iter( selection, &iter );
3974
3975 GtkEnableSelectionEvents();
718fd180
RR
3976}
3977
3978void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
3979{
6848478c
RR
3980 GtkDisableSelectionEvents();
3981
3982 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3983
3984 GtkTreeIter iter;
3985 iter.user_data = (gpointer) item.GetID();
3986 gtk_tree_selection_unselect_iter( selection, &iter );
3987
3988 GtkEnableSelectionEvents();
718fd180
RR
3989}
3990
3991bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
3992{
6848478c
RR
3993 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
3994
3995 GtkTreeIter iter;
3996 iter.user_data = (gpointer) item.GetID();
b9db5f30 3997
6848478c 3998 return gtk_tree_selection_iter_is_selected( selection, &iter );
718fd180
RR
3999}
4000
4001void wxDataViewCtrl::SelectAll()
4002{
6848478c 4003 GtkDisableSelectionEvents();
b9db5f30 4004
6848478c 4005 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4006
6848478c 4007 gtk_tree_selection_select_all( selection );
b9db5f30 4008
6848478c 4009 GtkEnableSelectionEvents();
718fd180
RR
4010}
4011
4012void wxDataViewCtrl::UnselectAll()
4013{
6848478c 4014 GtkDisableSelectionEvents();
b9db5f30 4015
6848478c 4016 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
b9db5f30 4017
6848478c 4018 gtk_tree_selection_unselect_all( selection );
b9db5f30 4019
6848478c 4020 GtkEnableSelectionEvents();
718fd180
RR
4021}
4022
7857346a
VZ
4023void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item,
4024 const wxDataViewColumn *WXUNUSED(column))
718fd180 4025{
6154212e
RR
4026 GtkTreeIter iter;
4027 iter.user_data = (gpointer) item.GetID();
4028 GtkTreePath *path = m_internal->get_path( &iter );
4029 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 );
4030 gtk_tree_path_free( path );
718fd180
RR
4031}
4032
7857346a
VZ
4033void wxDataViewCtrl::HitTest(const wxPoint& WXUNUSED(point),
4034 wxDataViewItem& item,
4035 wxDataViewColumn *& column) const
66e09788
RR
4036{
4037 item = wxDataViewItem(0);
a87b466d 4038 column = NULL;
66e09788
RR
4039}
4040
7857346a
VZ
4041wxRect
4042wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item),
4043 const wxDataViewColumn *WXUNUSED(column)) const
66e09788
RR
4044{
4045 return wxRect();
4046}
4047
3b6280be
RR
4048void wxDataViewCtrl::DoSetExpanderColumn()
4049{
b9db5f30 4050 gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview),
1b27b2bd 4051 GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) );
3b6280be
RR
4052}
4053
4054void wxDataViewCtrl::DoSetIndent()
4055{
4056}
4057
b086d55f
RR
4058void wxDataViewCtrl::GtkDisableSelectionEvents()
4059{
4060 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
4061 g_signal_handlers_disconnect_by_func( selection,
4062 (gpointer) (wxdataview_selection_changed_callback), this);
b086d55f
RR
4063}
4064
4065void wxDataViewCtrl::GtkEnableSelectionEvents()
4066{
4067 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
6848478c
RR
4068 g_signal_connect_after (selection, "changed",
4069 G_CALLBACK (wxdataview_selection_changed_callback), this);
b086d55f
RR
4070}
4071
b94db696
RR
4072// static
4073wxVisualAttributes
4074wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
4075{
4076 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
4077}
4078
6ff7eee7 4079
93763ad5 4080#endif
4ed7af08 4081 // !wxUSE_GENERICDATAVIEWCTRL
790b137e 4082
93763ad5 4083#endif
4ed7af08 4084 // wxUSE_DATAVIEWCTRL