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