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