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