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