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