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