]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/settings.h" | |
24 | #include "wx/crt.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/stockitem.h" | |
28 | #include "wx/calctrl.h" | |
29 | #include "wx/popupwin.h" | |
30 | #include "wx/listimpl.cpp" | |
31 | ||
32 | #include "wx/gtk/private.h" | |
33 | #include "wx/gtk/dc.h" | |
34 | #include "wx/gtk/dcclient.h" | |
35 | ||
36 | //----------------------------------------------------------------------------- | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | static wxDataViewCtrlInternal *gs_internal = NULL; | |
40 | ||
41 | class wxGtkTreeModelNode; | |
42 | ||
43 | extern "C" { | |
44 | typedef struct _GtkWxTreeModel GtkWxTreeModel; | |
45 | } | |
46 | ||
47 | //----------------------------------------------------------------------------- | |
48 | // wxDataViewCtrlInternal | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | WX_DECLARE_LIST(wxDataViewItem, ItemList); | |
52 | WX_DEFINE_LIST(ItemList) | |
53 | ||
54 | class WXDLLIMPEXP_ADV wxDataViewCtrlInternal | |
55 | { | |
56 | public: | |
57 | wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model ); | |
58 | ~wxDataViewCtrlInternal(); | |
59 | ||
60 | // model iface | |
61 | GtkTreeModelFlags get_flags(); | |
62 | gboolean get_iter( GtkTreeIter *iter, GtkTreePath *path ); | |
63 | GtkTreePath *get_path( GtkTreeIter *iter); | |
64 | gboolean iter_next( GtkTreeIter *iter ); | |
65 | gboolean iter_children( GtkTreeIter *iter, GtkTreeIter *parent); | |
66 | gboolean iter_has_child( GtkTreeIter *iter ); | |
67 | gint iter_n_children( GtkTreeIter *iter ); | |
68 | gboolean iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n ); | |
69 | gboolean iter_parent( GtkTreeIter *iter, GtkTreeIter *child ); | |
70 | ||
71 | // dnd iface | |
72 | ||
73 | bool EnableDragSource( const wxDataFormat &format ); | |
74 | bool EnableDropTarget( const wxDataFormat &format ); | |
75 | ||
76 | gboolean row_draggable( GtkTreeDragSource *drag_source, GtkTreePath *path ); | |
77 | gboolean drag_data_delete( GtkTreeDragSource *drag_source, GtkTreePath* path ); | |
78 | gboolean drag_data_get( GtkTreeDragSource *drag_source, GtkTreePath *path, | |
79 | GtkSelectionData *selection_data ); | |
80 | gboolean drag_data_received( GtkTreeDragDest *drag_dest, GtkTreePath *dest, | |
81 | GtkSelectionData *selection_data ); | |
82 | gboolean row_drop_possible( GtkTreeDragDest *drag_dest, GtkTreePath *dest_path, | |
83 | GtkSelectionData *selection_data ); | |
84 | ||
85 | // notifactions from wxDataViewModel | |
86 | bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ); | |
87 | bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ); | |
88 | bool ItemChanged( const wxDataViewItem &item ); | |
89 | bool ValueChanged( const wxDataViewItem &item, unsigned int col ); | |
90 | bool Cleared(); | |
91 | void Resort(); | |
92 | ||
93 | // sorting interface | |
94 | void SetSortOrder( GtkSortType sort_order ) { m_sort_order = sort_order; } | |
95 | GtkSortType GetSortOrder() const { return m_sort_order; } | |
96 | ||
97 | void SetSortColumn( int column ) { m_sort_column = column; } | |
98 | int GetSortColumn() const { return m_sort_column; } | |
99 | ||
100 | void SetDataViewSortColumn( wxDataViewColumn *column ) { m_dataview_sort_column = column; } | |
101 | wxDataViewColumn *GetDataViewSortColumn() { return m_dataview_sort_column; } | |
102 | ||
103 | bool IsSorted() { return (m_sort_column >= 0); } | |
104 | ||
105 | // accessors | |
106 | wxDataViewModel* GetDataViewModel() { return m_wx_model; } | |
107 | const wxDataViewModel* GetDataViewModel() const { return m_wx_model; } | |
108 | wxDataViewCtrl* GetOwner() { return m_owner; } | |
109 | GtkWxTreeModel* GetGtkModel() { return m_gtk_model; } | |
110 | ||
111 | protected: | |
112 | void InitTree(); | |
113 | wxGtkTreeModelNode *FindNode( const wxDataViewItem &item ); | |
114 | wxGtkTreeModelNode *FindNode( GtkTreeIter *iter ); | |
115 | wxGtkTreeModelNode *FindParentNode( const wxDataViewItem &item ); | |
116 | wxGtkTreeModelNode *FindParentNode( GtkTreeIter *iter ); | |
117 | void BuildBranch( wxGtkTreeModelNode *branch ); | |
118 | ||
119 | private: | |
120 | wxGtkTreeModelNode *m_root; | |
121 | wxDataViewModel *m_wx_model; | |
122 | GtkWxTreeModel *m_gtk_model; | |
123 | wxDataViewCtrl *m_owner; | |
124 | GtkSortType m_sort_order; | |
125 | wxDataViewColumn *m_dataview_sort_column; | |
126 | int m_sort_column; | |
127 | ||
128 | GtkTargetEntry m_dragSourceTargetEntry; | |
129 | wxCharBuffer m_dragSourceTargetEntryTarget; | |
130 | wxDataObject *m_dragDataObject; | |
131 | ||
132 | GtkTargetEntry m_dropTargetTargetEntry; | |
133 | wxCharBuffer m_dropTargetTargetEntryTarget; | |
134 | wxDataObject *m_dropDataObject; | |
135 | }; | |
136 | ||
137 | ||
138 | //----------------------------------------------------------------------------- | |
139 | // wxGtkTreeModelNode | |
140 | //----------------------------------------------------------------------------- | |
141 | ||
142 | static | |
143 | int LINKAGEMODE wxGtkTreeModelChildCmp( void** id1, void** id2 ) | |
144 | { | |
145 | int ret = gs_internal->GetDataViewModel()->Compare( *id1, *id2, | |
146 | gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) ); | |
147 | ||
148 | return ret; | |
149 | } | |
150 | ||
151 | WX_DEFINE_ARRAY_PTR( wxGtkTreeModelNode*, wxGtkTreeModelNodes ); | |
152 | WX_DEFINE_ARRAY_PTR( void*, wxGtkTreeModelChildren ); | |
153 | ||
154 | class wxGtkTreeModelNode | |
155 | { | |
156 | public: | |
157 | wxGtkTreeModelNode( wxGtkTreeModelNode* parent, const wxDataViewItem &item, | |
158 | wxDataViewCtrlInternal *internal ) | |
159 | { | |
160 | m_parent = parent; | |
161 | m_item = item; | |
162 | m_internal = internal; | |
163 | } | |
164 | ||
165 | ~wxGtkTreeModelNode() | |
166 | { | |
167 | size_t count = m_nodes.GetCount(); | |
168 | size_t i; | |
169 | for (i = 0; i < count; i++) | |
170 | { | |
171 | wxGtkTreeModelNode *child = m_nodes.Item( i ); | |
172 | delete child; | |
173 | } | |
174 | } | |
175 | ||
176 | unsigned int AddNode( wxGtkTreeModelNode* child ) | |
177 | { | |
178 | m_nodes.Add( child ); | |
179 | ||
180 | void *id = child->GetItem().GetID(); | |
181 | ||
182 | m_children.Add( id ); | |
183 | ||
184 | if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare()) | |
185 | { | |
186 | gs_internal = m_internal; | |
187 | m_children.Sort( &wxGtkTreeModelChildCmp ); | |
188 | return m_children.Index( id ); | |
189 | } | |
190 | ||
191 | return m_children.GetCount()-1; | |
192 | } | |
193 | ||
194 | unsigned int AddLeave( void* id ) | |
195 | { | |
196 | m_children.Add( id ); | |
197 | ||
198 | if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare()) | |
199 | { | |
200 | gs_internal = m_internal; | |
201 | m_children.Sort( &wxGtkTreeModelChildCmp ); | |
202 | return m_children.Index( id ); | |
203 | } | |
204 | ||
205 | return m_children.GetCount()-1; | |
206 | } | |
207 | ||
208 | void DeleteChild( void* id ) | |
209 | { | |
210 | m_children.Remove( id ); | |
211 | ||
212 | unsigned int count = m_nodes.GetCount(); | |
213 | unsigned int pos; | |
214 | for (pos = 0; pos < count; pos++) | |
215 | { | |
216 | wxGtkTreeModelNode *node = m_nodes.Item( pos ); | |
217 | if (node->GetItem().GetID() == id) | |
218 | { | |
219 | m_nodes.RemoveAt( pos ); | |
220 | delete node; | |
221 | break; | |
222 | } | |
223 | } | |
224 | } | |
225 | ||
226 | wxGtkTreeModelNode* GetParent() | |
227 | { return m_parent; } | |
228 | wxGtkTreeModelNodes &GetNodes() | |
229 | { return m_nodes; } | |
230 | wxGtkTreeModelChildren &GetChildren() | |
231 | { return m_children; } | |
232 | ||
233 | unsigned int GetChildCount() const { return m_children.GetCount(); } | |
234 | unsigned int GetNodesCount() const { return m_nodes.GetCount(); } | |
235 | ||
236 | wxDataViewItem &GetItem() { return m_item; } | |
237 | wxDataViewCtrlInternal *GetInternal() { return m_internal; } | |
238 | ||
239 | void Resort(); | |
240 | ||
241 | private: | |
242 | wxGtkTreeModelNode *m_parent; | |
243 | wxGtkTreeModelNodes m_nodes; | |
244 | wxGtkTreeModelChildren m_children; | |
245 | wxDataViewItem m_item; | |
246 | wxDataViewCtrlInternal *m_internal; | |
247 | }; | |
248 | ||
249 | ||
250 | //----------------------------------------------------------------------------- | |
251 | // data | |
252 | //----------------------------------------------------------------------------- | |
253 | ||
254 | extern bool g_blockEventsOnDrag; | |
255 | ||
256 | //----------------------------------------------------------------------------- | |
257 | // define new GTK+ class wxGtkTreeModel | |
258 | //----------------------------------------------------------------------------- | |
259 | ||
260 | extern "C" { | |
261 | ||
262 | #define GTK_TYPE_WX_TREE_MODEL (gtk_wx_tree_model_get_type ()) | |
263 | #define GTK_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModel)) | |
264 | #define GTK_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass)) | |
265 | #define GTK_IS_WX_TREE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_TREE_MODEL)) | |
266 | #define GTK_IS_WX_TREE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_TREE_MODEL)) | |
267 | #define GTK_WX_TREE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_TREE_MODEL, GtkWxTreeModelClass)) | |
268 | ||
269 | GType gtk_wx_tree_model_get_type (void); | |
270 | ||
271 | typedef struct _GtkWxTreeModelClass GtkWxTreeModelClass; | |
272 | ||
273 | struct _GtkWxTreeModel | |
274 | { | |
275 | GObject parent; | |
276 | ||
277 | /*< private >*/ | |
278 | gint stamp; | |
279 | wxDataViewCtrlInternal *internal; | |
280 | }; | |
281 | ||
282 | struct _GtkWxTreeModelClass | |
283 | { | |
284 | GObjectClass list_parent_class; | |
285 | }; | |
286 | ||
287 | static GtkWxTreeModel *wxgtk_tree_model_new (void); | |
288 | static void wxgtk_tree_model_init (GtkWxTreeModel *tree_model); | |
289 | static void wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass); | |
290 | ||
291 | static void wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface); | |
292 | static void wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface); | |
293 | static void wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface); | |
294 | static void wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface); | |
295 | ||
296 | static void wxgtk_tree_model_finalize (GObject *object); | |
297 | static GtkTreeModelFlags wxgtk_tree_model_get_flags (GtkTreeModel *tree_model); | |
298 | static gint wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model); | |
299 | static GType wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model, | |
300 | gint index); | |
301 | static gboolean wxgtk_tree_model_get_iter (GtkTreeModel *tree_model, | |
302 | GtkTreeIter *iter, | |
303 | GtkTreePath *path); | |
304 | static GtkTreePath *wxgtk_tree_model_get_path (GtkTreeModel *tree_model, | |
305 | GtkTreeIter *iter); | |
306 | static void wxgtk_tree_model_get_value (GtkTreeModel *tree_model, | |
307 | GtkTreeIter *iter, | |
308 | gint column, | |
309 | GValue *value); | |
310 | static gboolean wxgtk_tree_model_iter_next (GtkTreeModel *tree_model, | |
311 | GtkTreeIter *iter); | |
312 | static gboolean wxgtk_tree_model_iter_children (GtkTreeModel *tree_model, | |
313 | GtkTreeIter *iter, | |
314 | GtkTreeIter *parent); | |
315 | static gboolean wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model, | |
316 | GtkTreeIter *iter); | |
317 | static gint wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model, | |
318 | GtkTreeIter *iter); | |
319 | static gboolean wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model, | |
320 | GtkTreeIter *iter, | |
321 | GtkTreeIter *parent, | |
322 | gint n); | |
323 | static gboolean wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model, | |
324 | GtkTreeIter *iter, | |
325 | GtkTreeIter *child); | |
326 | ||
327 | /* sortable */ | |
328 | static gboolean wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable, | |
329 | gint *sort_column_id, | |
330 | GtkSortType *order); | |
331 | static void wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable, | |
332 | gint sort_column_id, | |
333 | GtkSortType order); | |
334 | static void wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable, | |
335 | gint sort_column_id, | |
336 | GtkTreeIterCompareFunc func, | |
337 | gpointer data, | |
338 | GtkDestroyNotify destroy); | |
339 | static void wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable, | |
340 | GtkTreeIterCompareFunc func, | |
341 | gpointer data, | |
342 | GtkDestroyNotify destroy); | |
343 | static gboolean wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable); | |
344 | ||
345 | /* drag'n'drop */ | |
346 | static gboolean wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source, | |
347 | GtkTreePath *path); | |
348 | static gboolean wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source, | |
349 | GtkTreePath *path); | |
350 | static gboolean wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source, | |
351 | GtkTreePath *path, | |
352 | GtkSelectionData *selection_data); | |
353 | static gboolean wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest, | |
354 | GtkTreePath *dest, | |
355 | GtkSelectionData *selection_data); | |
356 | static gboolean wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest, | |
357 | GtkTreePath *dest_path, | |
358 | GtkSelectionData *selection_data); | |
359 | ||
360 | ||
361 | static GObjectClass *list_parent_class = NULL; | |
362 | ||
363 | GType | |
364 | gtk_wx_tree_model_get_type (void) | |
365 | { | |
366 | static GType tree_model_type = 0; | |
367 | ||
368 | if (!tree_model_type) | |
369 | { | |
370 | const GTypeInfo tree_model_info = | |
371 | { | |
372 | sizeof (GtkWxTreeModelClass), | |
373 | NULL, /* base_init */ | |
374 | NULL, /* base_finalize */ | |
375 | (GClassInitFunc) wxgtk_tree_model_class_init, | |
376 | NULL, /* class_finalize */ | |
377 | NULL, /* class_data */ | |
378 | sizeof (GtkWxTreeModel), | |
379 | 0, | |
380 | (GInstanceInitFunc) wxgtk_tree_model_init, | |
381 | }; | |
382 | ||
383 | static const GInterfaceInfo tree_model_iface_info = | |
384 | { | |
385 | (GInterfaceInitFunc) wxgtk_tree_model_tree_model_init, | |
386 | NULL, | |
387 | NULL | |
388 | }; | |
389 | ||
390 | static const GInterfaceInfo sortable_iface_info = | |
391 | { | |
392 | (GInterfaceInitFunc) wxgtk_tree_model_sortable_init, | |
393 | NULL, | |
394 | NULL | |
395 | }; | |
396 | ||
397 | static const GInterfaceInfo drag_source_iface_info = | |
398 | { | |
399 | (GInterfaceInitFunc) wxgtk_tree_model_drag_source_init, | |
400 | NULL, | |
401 | NULL | |
402 | }; | |
403 | ||
404 | static const GInterfaceInfo drag_dest_iface_info = | |
405 | { | |
406 | (GInterfaceInitFunc) wxgtk_tree_model_drag_dest_init, | |
407 | NULL, | |
408 | NULL | |
409 | }; | |
410 | ||
411 | tree_model_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxTreeModel", | |
412 | &tree_model_info, (GTypeFlags)0 ); | |
413 | ||
414 | g_type_add_interface_static (tree_model_type, | |
415 | GTK_TYPE_TREE_MODEL, | |
416 | &tree_model_iface_info); | |
417 | g_type_add_interface_static (tree_model_type, | |
418 | GTK_TYPE_TREE_SORTABLE, | |
419 | &sortable_iface_info); | |
420 | g_type_add_interface_static (tree_model_type, | |
421 | GTK_TYPE_TREE_DRAG_DEST, | |
422 | &drag_dest_iface_info); | |
423 | g_type_add_interface_static (tree_model_type, | |
424 | GTK_TYPE_TREE_DRAG_SOURCE, | |
425 | &drag_source_iface_info); | |
426 | } | |
427 | ||
428 | return tree_model_type; | |
429 | } | |
430 | ||
431 | static GtkWxTreeModel * | |
432 | wxgtk_tree_model_new(void) | |
433 | { | |
434 | GtkWxTreeModel *retval = (GtkWxTreeModel *) g_object_new (GTK_TYPE_WX_TREE_MODEL, NULL); | |
435 | return retval; | |
436 | } | |
437 | ||
438 | static void | |
439 | wxgtk_tree_model_class_init (GtkWxTreeModelClass *klass) | |
440 | { | |
441 | list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass); | |
442 | GObjectClass *object_class = (GObjectClass*) klass; | |
443 | object_class->finalize = wxgtk_tree_model_finalize; | |
444 | } | |
445 | ||
446 | static void | |
447 | wxgtk_tree_model_tree_model_init (GtkTreeModelIface *iface) | |
448 | { | |
449 | iface->get_flags = wxgtk_tree_model_get_flags; | |
450 | iface->get_n_columns = wxgtk_tree_model_get_n_columns; | |
451 | iface->get_column_type = wxgtk_tree_model_get_column_type; | |
452 | iface->get_iter = wxgtk_tree_model_get_iter; | |
453 | iface->get_path = wxgtk_tree_model_get_path; | |
454 | iface->get_value = wxgtk_tree_model_get_value; | |
455 | iface->iter_next = wxgtk_tree_model_iter_next; | |
456 | iface->iter_children = wxgtk_tree_model_iter_children; | |
457 | iface->iter_has_child = wxgtk_tree_model_iter_has_child; | |
458 | iface->iter_n_children = wxgtk_tree_model_iter_n_children; | |
459 | iface->iter_nth_child = wxgtk_tree_model_iter_nth_child; | |
460 | iface->iter_parent = wxgtk_tree_model_iter_parent; | |
461 | } | |
462 | ||
463 | static void | |
464 | wxgtk_tree_model_sortable_init (GtkTreeSortableIface *iface) | |
465 | { | |
466 | iface->get_sort_column_id = wxgtk_tree_model_get_sort_column_id; | |
467 | iface->set_sort_column_id = wxgtk_tree_model_set_sort_column_id; | |
468 | iface->set_sort_func = wxgtk_tree_model_set_sort_func; | |
469 | iface->set_default_sort_func = wxgtk_tree_model_set_default_sort_func; | |
470 | iface->has_default_sort_func = wxgtk_tree_model_has_default_sort_func; | |
471 | } | |
472 | ||
473 | static void | |
474 | wxgtk_tree_model_drag_source_init(GtkTreeDragSourceIface *iface) | |
475 | { | |
476 | iface->row_draggable = wxgtk_tree_model_row_draggable; | |
477 | iface->drag_data_delete = wxgtk_tree_model_drag_data_delete; | |
478 | iface->drag_data_get = wxgtk_tree_model_drag_data_get; | |
479 | } | |
480 | ||
481 | static void | |
482 | wxgtk_tree_model_drag_dest_init (GtkTreeDragDestIface *iface) | |
483 | { | |
484 | iface->drag_data_received = wxgtk_tree_model_drag_data_received; | |
485 | iface->row_drop_possible = wxgtk_tree_model_row_drop_possible; | |
486 | } | |
487 | ||
488 | static void | |
489 | wxgtk_tree_model_init (GtkWxTreeModel *tree_model) | |
490 | { | |
491 | tree_model->internal = NULL; | |
492 | tree_model->stamp = g_random_int(); | |
493 | } | |
494 | ||
495 | static void | |
496 | wxgtk_tree_model_finalize (GObject *object) | |
497 | { | |
498 | /* must chain up */ | |
499 | (* list_parent_class->finalize) (object); | |
500 | } | |
501 | ||
502 | } // extern "C" | |
503 | ||
504 | //----------------------------------------------------------------------------- | |
505 | // implement callbacks from wxGtkTreeModel class by letting | |
506 | // them call the methods of wxWidgets' wxDataViewModel | |
507 | //----------------------------------------------------------------------------- | |
508 | ||
509 | static GtkTreeModelFlags | |
510 | wxgtk_tree_model_get_flags (GtkTreeModel *tree_model) | |
511 | { | |
512 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
513 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), (GtkTreeModelFlags)0 ); | |
514 | ||
515 | return wxtree_model->internal->get_flags(); | |
516 | } | |
517 | ||
518 | static gint | |
519 | wxgtk_tree_model_get_n_columns (GtkTreeModel *tree_model) | |
520 | { | |
521 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
522 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), 0); | |
523 | ||
524 | return wxtree_model->internal->GetDataViewModel()->GetColumnCount(); | |
525 | } | |
526 | ||
527 | static GType | |
528 | wxgtk_tree_model_get_column_type (GtkTreeModel *tree_model, | |
529 | gint index) | |
530 | { | |
531 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
532 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), G_TYPE_INVALID); | |
533 | ||
534 | GType gtype = G_TYPE_INVALID; | |
535 | ||
536 | wxString wxtype = wxtree_model->internal->GetDataViewModel()->GetColumnType( (unsigned int) index ); | |
537 | ||
538 | if (wxtype == wxT("string")) | |
539 | gtype = G_TYPE_STRING; | |
540 | else | |
541 | { | |
542 | gtype = G_TYPE_STRING; | |
543 | // wxFAIL_MSG( _T("non-string columns not supported yet") ); | |
544 | } | |
545 | ||
546 | return gtype; | |
547 | } | |
548 | ||
549 | static gboolean | |
550 | wxgtk_tree_model_get_iter (GtkTreeModel *tree_model, | |
551 | GtkTreeIter *iter, | |
552 | GtkTreePath *path) | |
553 | { | |
554 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
555 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
556 | g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE); | |
557 | ||
558 | return wxtree_model->internal->get_iter( iter, path ); | |
559 | } | |
560 | ||
561 | static GtkTreePath * | |
562 | wxgtk_tree_model_get_path (GtkTreeModel *tree_model, | |
563 | GtkTreeIter *iter) | |
564 | { | |
565 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
566 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), NULL); | |
567 | g_return_val_if_fail (iter->stamp == GTK_WX_TREE_MODEL (wxtree_model)->stamp, NULL); | |
568 | ||
569 | return wxtree_model->internal->get_path( iter ); | |
570 | } | |
571 | ||
572 | static void | |
573 | wxgtk_tree_model_get_value (GtkTreeModel *tree_model, | |
574 | GtkTreeIter *iter, | |
575 | gint column, | |
576 | GValue *value) | |
577 | { | |
578 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
579 | g_return_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model) ); | |
580 | ||
581 | wxDataViewModel *model = wxtree_model->internal->GetDataViewModel(); | |
582 | wxString mtype = model->GetColumnType( (unsigned int) column ); | |
583 | if (mtype == wxT("string")) | |
584 | { | |
585 | wxVariant variant; | |
586 | g_value_init( value, G_TYPE_STRING ); | |
587 | wxDataViewItem item( (void*) iter->user_data ); | |
588 | model->GetValue( variant, item, (unsigned int) column ); | |
589 | ||
590 | g_value_set_string( value, variant.GetString().utf8_str() ); | |
591 | } | |
592 | else | |
593 | { | |
594 | wxFAIL_MSG( _T("non-string columns not supported yet") ); | |
595 | } | |
596 | } | |
597 | ||
598 | static gboolean | |
599 | wxgtk_tree_model_iter_next (GtkTreeModel *tree_model, | |
600 | GtkTreeIter *iter) | |
601 | { | |
602 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
603 | ||
604 | //if (wxtree_model->stamp != iter->stamp) | |
605 | // wxPrintf( "crash\n" ); | |
606 | // TODO: remove this code | |
607 | ||
608 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
609 | g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE); | |
610 | ||
611 | return wxtree_model->internal->iter_next( iter ); | |
612 | } | |
613 | ||
614 | static gboolean | |
615 | wxgtk_tree_model_iter_children (GtkTreeModel *tree_model, | |
616 | GtkTreeIter *iter, | |
617 | GtkTreeIter *parent) | |
618 | { | |
619 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
620 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
621 | g_return_val_if_fail (wxtree_model->stamp == parent->stamp, FALSE); | |
622 | ||
623 | return wxtree_model->internal->iter_children( iter, parent ); | |
624 | } | |
625 | ||
626 | static gboolean | |
627 | wxgtk_tree_model_iter_has_child (GtkTreeModel *tree_model, | |
628 | GtkTreeIter *iter) | |
629 | { | |
630 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
631 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
632 | g_return_val_if_fail (wxtree_model->stamp == iter->stamp, FALSE); | |
633 | ||
634 | return wxtree_model->internal->iter_has_child( iter ); | |
635 | } | |
636 | ||
637 | static gint | |
638 | wxgtk_tree_model_iter_n_children (GtkTreeModel *tree_model, | |
639 | GtkTreeIter *iter) | |
640 | { | |
641 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
642 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
643 | g_return_val_if_fail ( !iter || wxtree_model->stamp == iter->stamp, 0); | |
644 | ||
645 | return wxtree_model->internal->iter_n_children( iter ); | |
646 | } | |
647 | ||
648 | static gboolean | |
649 | wxgtk_tree_model_iter_nth_child (GtkTreeModel *tree_model, | |
650 | GtkTreeIter *iter, | |
651 | GtkTreeIter *parent, | |
652 | gint n) | |
653 | { | |
654 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
655 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
656 | ||
657 | return wxtree_model->internal->iter_nth_child( iter, parent, n ); | |
658 | } | |
659 | ||
660 | static gboolean | |
661 | wxgtk_tree_model_iter_parent (GtkTreeModel *tree_model, | |
662 | GtkTreeIter *iter, | |
663 | GtkTreeIter *child) | |
664 | { | |
665 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; | |
666 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
667 | g_return_val_if_fail (wxtree_model->stamp == child->stamp, FALSE); | |
668 | ||
669 | return wxtree_model->internal->iter_parent( iter, child ); | |
670 | } | |
671 | ||
672 | /* drag'n'drop iface */ | |
673 | static gboolean | |
674 | wxgtk_tree_model_row_draggable (GtkTreeDragSource *drag_source, | |
675 | GtkTreePath *path) | |
676 | { | |
677 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source; | |
678 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
679 | ||
680 | return wxtree_model->internal->row_draggable( drag_source, path ); | |
681 | } | |
682 | ||
683 | static gboolean | |
684 | wxgtk_tree_model_drag_data_delete (GtkTreeDragSource *drag_source, | |
685 | GtkTreePath *path) | |
686 | { | |
687 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source; | |
688 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
689 | ||
690 | return wxtree_model->internal->drag_data_delete( drag_source, path ); | |
691 | } | |
692 | ||
693 | static gboolean | |
694 | wxgtk_tree_model_drag_data_get (GtkTreeDragSource *drag_source, | |
695 | GtkTreePath *path, | |
696 | GtkSelectionData *selection_data) | |
697 | { | |
698 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_source; | |
699 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
700 | ||
701 | #if 0 | |
702 | wxPrintf( "drag_get_data\n"); | |
703 | ||
704 | wxGtkString atom_selection(gdk_atom_name(selection_data->selection)); | |
705 | wxPrintf( "selection %s\n", wxString::FromAscii(atom_selection) ); | |
706 | ||
707 | wxGtkString atom_target(gdk_atom_name(selection_data->target)); | |
708 | wxPrintf( "target %s\n", wxString::FromAscii(atom_target) ); | |
709 | ||
710 | wxGtkString atom_type(gdk_atom_name(selection_data->type)); | |
711 | wxPrintf( "type %s\n", wxString::FromAscii(atom_type) ); | |
712 | ||
713 | wxPrintf( "format %d\n", selection_data->format ); | |
714 | #endif | |
715 | ||
716 | return wxtree_model->internal->drag_data_get( drag_source, path, selection_data ); | |
717 | } | |
718 | ||
719 | static gboolean | |
720 | wxgtk_tree_model_drag_data_received (GtkTreeDragDest *drag_dest, | |
721 | GtkTreePath *dest, | |
722 | GtkSelectionData *selection_data) | |
723 | { | |
724 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest; | |
725 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
726 | ||
727 | return wxtree_model->internal->drag_data_received( drag_dest, dest, selection_data ); | |
728 | } | |
729 | ||
730 | static gboolean | |
731 | wxgtk_tree_model_row_drop_possible (GtkTreeDragDest *drag_dest, | |
732 | GtkTreePath *dest_path, | |
733 | GtkSelectionData *selection_data) | |
734 | { | |
735 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) drag_dest; | |
736 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), FALSE); | |
737 | ||
738 | return wxtree_model->internal->row_drop_possible( drag_dest, dest_path, selection_data ); | |
739 | } | |
740 | ||
741 | /* sortable iface */ | |
742 | static gboolean | |
743 | wxgtk_tree_model_get_sort_column_id (GtkTreeSortable *sortable, | |
744 | gint *sort_column_id, | |
745 | GtkSortType *order) | |
746 | { | |
747 | GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) sortable; | |
748 | ||
749 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE); | |
750 | ||
751 | if (!wxtree_model->internal->IsSorted()) | |
752 | { | |
753 | if (sort_column_id) | |
754 | *sort_column_id = -1; | |
755 | ||
756 | return TRUE; | |
757 | } | |
758 | ||
759 | ||
760 | if (sort_column_id) | |
761 | *sort_column_id = wxtree_model->internal->GetSortColumn(); | |
762 | ||
763 | if (order) | |
764 | *order = wxtree_model->internal->GetSortOrder(); | |
765 | ||
766 | return TRUE; | |
767 | } | |
768 | ||
769 | wxDataViewColumn *gs_lastLeftClickHeader = NULL; | |
770 | ||
771 | static void | |
772 | wxgtk_tree_model_set_sort_column_id (GtkTreeSortable *sortable, | |
773 | gint sort_column_id, | |
774 | GtkSortType order) | |
775 | { | |
776 | GtkWxTreeModel *tree_model = (GtkWxTreeModel *) sortable; | |
777 | g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) ); | |
778 | ||
779 | tree_model->internal->SetDataViewSortColumn( gs_lastLeftClickHeader ); | |
780 | ||
781 | if ((sort_column_id != (gint) tree_model->internal->GetSortColumn()) || | |
782 | (order != tree_model->internal->GetSortOrder())) | |
783 | { | |
784 | tree_model->internal->SetSortColumn( sort_column_id ); | |
785 | tree_model->internal->SetSortOrder( order ); | |
786 | ||
787 | gtk_tree_sortable_sort_column_changed (sortable); | |
788 | ||
789 | tree_model->internal->GetDataViewModel()->Resort(); | |
790 | } | |
791 | ||
792 | if (gs_lastLeftClickHeader) | |
793 | { | |
794 | wxDataViewCtrl *dv = tree_model->internal->GetOwner(); | |
795 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, dv->GetId() ); | |
796 | event.SetDataViewColumn( gs_lastLeftClickHeader ); | |
797 | event.SetModel( dv->GetModel() ); | |
798 | dv->HandleWindowEvent( event ); | |
799 | } | |
800 | ||
801 | gs_lastLeftClickHeader = NULL; | |
802 | } | |
803 | ||
804 | static void | |
805 | wxgtk_tree_model_set_sort_func (GtkTreeSortable *sortable, | |
806 | gint WXUNUSED(sort_column_id), | |
807 | GtkTreeIterCompareFunc func, | |
808 | gpointer WXUNUSED(data), | |
809 | GtkDestroyNotify WXUNUSED(destroy)) | |
810 | { | |
811 | g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) ); | |
812 | g_return_if_fail (func != NULL); | |
813 | } | |
814 | ||
815 | static void | |
816 | wxgtk_tree_model_set_default_sort_func (GtkTreeSortable *sortable, | |
817 | GtkTreeIterCompareFunc func, | |
818 | gpointer WXUNUSED(data), | |
819 | GtkDestroyNotify WXUNUSED(destroy)) | |
820 | { | |
821 | g_return_if_fail (GTK_IS_WX_TREE_MODEL (sortable) ); | |
822 | g_return_if_fail (func != NULL); | |
823 | ||
824 | //wxPrintf( "wxgtk_tree_model_set_default_sort_func\n" ); | |
825 | // TODO: remove this code | |
826 | } | |
827 | ||
828 | static gboolean | |
829 | wxgtk_tree_model_has_default_sort_func (GtkTreeSortable *sortable) | |
830 | { | |
831 | g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (sortable), FALSE ); | |
832 | ||
833 | return FALSE; | |
834 | } | |
835 | ||
836 | //----------------------------------------------------------------------------- | |
837 | // define new GTK+ class GtkWxRendererText | |
838 | //----------------------------------------------------------------------------- | |
839 | ||
840 | extern "C" { | |
841 | ||
842 | #define GTK_TYPE_WX_CELL_RENDERER_TEXT (gtk_wx_cell_renderer_text_get_type ()) | |
843 | #define GTK_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererText)) | |
844 | #define GTK_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass)) | |
845 | #define GTK_IS_WX_CELL_RENDERER_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT)) | |
846 | #define GTK_IS_WX_CELL_RENDERER_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER_TEXT)) | |
847 | #define GTK_WX_CELL_RENDERER_TEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER_TEXT, GtkWxCellRendererTextClass)) | |
848 | ||
849 | GType gtk_wx_cell_renderer_text_get_type (void); | |
850 | ||
851 | typedef struct _GtkWxCellRendererText GtkWxCellRendererText; | |
852 | typedef struct _GtkWxCellRendererTextClass GtkWxCellRendererTextClass; | |
853 | ||
854 | struct _GtkWxCellRendererText | |
855 | { | |
856 | GtkCellRendererText parent; | |
857 | ||
858 | wxDataViewRenderer *wx_renderer; | |
859 | }; | |
860 | ||
861 | struct _GtkWxCellRendererTextClass | |
862 | { | |
863 | GtkCellRendererTextClass cell_parent_class; | |
864 | }; | |
865 | ||
866 | ||
867 | static GtkWxCellRendererText *gtk_wx_cell_renderer_text_new (void); | |
868 | static void gtk_wx_cell_renderer_text_init ( | |
869 | GtkWxCellRendererText *cell ); | |
870 | static void gtk_wx_cell_renderer_text_class_init( | |
871 | GtkWxCellRendererTextClass *klass ); | |
872 | static void gtk_wx_cell_renderer_text_finalize ( | |
873 | GObject *object ); | |
874 | static GtkCellEditable *gtk_wx_cell_renderer_text_start_editing( | |
875 | GtkCellRenderer *cell, | |
876 | GdkEvent *event, | |
877 | GtkWidget *widget, | |
878 | const gchar *path, | |
879 | GdkRectangle *background_area, | |
880 | GdkRectangle *cell_area, | |
881 | GtkCellRendererState flags ); | |
882 | ||
883 | ||
884 | static GObjectClass *text_cell_parent_class = NULL; | |
885 | ||
886 | } // extern "C" | |
887 | ||
888 | GType | |
889 | gtk_wx_cell_renderer_text_get_type (void) | |
890 | { | |
891 | static GType cell_wx_type = 0; | |
892 | ||
893 | if (!cell_wx_type) | |
894 | { | |
895 | const GTypeInfo cell_wx_info = | |
896 | { | |
897 | sizeof (GtkWxCellRendererTextClass), | |
898 | NULL, /* base_init */ | |
899 | NULL, /* base_finalize */ | |
900 | (GClassInitFunc) gtk_wx_cell_renderer_text_class_init, | |
901 | NULL, /* class_finalize */ | |
902 | NULL, /* class_data */ | |
903 | sizeof (GtkWxCellRendererText), | |
904 | 0, /* n_preallocs */ | |
905 | (GInstanceInitFunc) gtk_wx_cell_renderer_text_init, | |
906 | }; | |
907 | ||
908 | cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT, | |
909 | "GtkWxCellRendererText", &cell_wx_info, (GTypeFlags)0 ); | |
910 | } | |
911 | ||
912 | return cell_wx_type; | |
913 | } | |
914 | ||
915 | static void | |
916 | gtk_wx_cell_renderer_text_init (GtkWxCellRendererText *cell) | |
917 | { | |
918 | cell->wx_renderer = NULL; | |
919 | } | |
920 | ||
921 | static void | |
922 | gtk_wx_cell_renderer_text_class_init (GtkWxCellRendererTextClass *klass) | |
923 | { | |
924 | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
925 | GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass); | |
926 | ||
927 | text_cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass); | |
928 | ||
929 | object_class->finalize = gtk_wx_cell_renderer_text_finalize; | |
930 | ||
931 | cell_class->start_editing = gtk_wx_cell_renderer_text_start_editing; | |
932 | } | |
933 | ||
934 | static void | |
935 | gtk_wx_cell_renderer_text_finalize (GObject *object) | |
936 | { | |
937 | /* must chain up */ | |
938 | (* G_OBJECT_CLASS (text_cell_parent_class)->finalize) (object); | |
939 | } | |
940 | ||
941 | GtkWxCellRendererText* | |
942 | gtk_wx_cell_renderer_text_new (void) | |
943 | { | |
944 | return (GtkWxCellRendererText*) g_object_new (GTK_TYPE_WX_CELL_RENDERER_TEXT, NULL); | |
945 | } | |
946 | ||
947 | static GtkCellEditable *gtk_wx_cell_renderer_text_start_editing( | |
948 | GtkCellRenderer *gtk_renderer, | |
949 | GdkEvent *gdk_event, | |
950 | GtkWidget *widget, | |
951 | const gchar *path, | |
952 | GdkRectangle *background_area, | |
953 | GdkRectangle *cell_area, | |
954 | GtkCellRendererState flags ) | |
955 | { | |
956 | GtkWxCellRendererText *wxgtk_renderer = (GtkWxCellRendererText *) gtk_renderer; | |
957 | wxDataViewRenderer *wx_renderer = wxgtk_renderer->wx_renderer; | |
958 | ||
959 | GtkTreePath *treepath = gtk_tree_path_new_from_string( path ); | |
960 | GtkTreeIter iter; | |
961 | wx_renderer->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, treepath ); | |
962 | wxDataViewItem item( (void*) iter.user_data );; | |
963 | gtk_tree_path_free( treepath ); | |
964 | ||
965 | wxDataViewColumn *column = wx_renderer->GetOwner(); | |
966 | wxDataViewCtrl *dv = column->GetOwner(); | |
967 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, dv->GetId() ); | |
968 | event.SetDataViewColumn( column ); | |
969 | event.SetModel( dv->GetModel() ); | |
970 | event.SetColumn( wx_renderer->GetOwner()->GetModelColumn() ); | |
971 | event.SetItem( item ); | |
972 | dv->HandleWindowEvent( event ); | |
973 | ||
974 | if (event.IsAllowed()) | |
975 | return GTK_CELL_RENDERER_CLASS(text_cell_parent_class)-> | |
976 | start_editing( gtk_renderer, gdk_event, widget, path, background_area, cell_area, flags ); | |
977 | else | |
978 | return NULL; | |
979 | } | |
980 | ||
981 | //----------------------------------------------------------------------------- | |
982 | // define new GTK+ class GtkWxCellRenderer | |
983 | //----------------------------------------------------------------------------- | |
984 | ||
985 | extern "C" { | |
986 | ||
987 | #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ()) | |
988 | #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer)) | |
989 | #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass)) | |
990 | #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER)) | |
991 | #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER)) | |
992 | #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass)) | |
993 | ||
994 | GType gtk_wx_cell_renderer_get_type (void); | |
995 | ||
996 | typedef struct _GtkWxCellRenderer GtkWxCellRenderer; | |
997 | typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass; | |
998 | ||
999 | struct _GtkWxCellRenderer | |
1000 | { | |
1001 | GtkCellRenderer parent; | |
1002 | ||
1003 | /*< private >*/ | |
1004 | wxDataViewCustomRenderer *cell; | |
1005 | guint32 last_click; | |
1006 | }; | |
1007 | ||
1008 | struct _GtkWxCellRendererClass | |
1009 | { | |
1010 | GtkCellRendererClass cell_parent_class; | |
1011 | }; | |
1012 | ||
1013 | ||
1014 | static GtkCellRenderer *gtk_wx_cell_renderer_new (void); | |
1015 | static void gtk_wx_cell_renderer_init ( | |
1016 | GtkWxCellRenderer *cell ); | |
1017 | static void gtk_wx_cell_renderer_class_init( | |
1018 | GtkWxCellRendererClass *klass ); | |
1019 | static void gtk_wx_cell_renderer_finalize ( | |
1020 | GObject *object ); | |
1021 | static void gtk_wx_cell_renderer_get_size ( | |
1022 | GtkCellRenderer *cell, | |
1023 | GtkWidget *widget, | |
1024 | GdkRectangle *rectangle, | |
1025 | gint *x_offset, | |
1026 | gint *y_offset, | |
1027 | gint *width, | |
1028 | gint *height ); | |
1029 | static void gtk_wx_cell_renderer_render ( | |
1030 | GtkCellRenderer *cell, | |
1031 | GdkWindow *window, | |
1032 | GtkWidget *widget, | |
1033 | GdkRectangle *background_area, | |
1034 | GdkRectangle *cell_area, | |
1035 | GdkRectangle *expose_area, | |
1036 | GtkCellRendererState flags ); | |
1037 | static gboolean gtk_wx_cell_renderer_activate( | |
1038 | GtkCellRenderer *cell, | |
1039 | GdkEvent *event, | |
1040 | GtkWidget *widget, | |
1041 | const gchar *path, | |
1042 | GdkRectangle *background_area, | |
1043 | GdkRectangle *cell_area, | |
1044 | GtkCellRendererState flags ); | |
1045 | static GtkCellEditable *gtk_wx_cell_renderer_start_editing( | |
1046 | GtkCellRenderer *cell, | |
1047 | GdkEvent *event, | |
1048 | GtkWidget *widget, | |
1049 | const gchar *path, | |
1050 | GdkRectangle *background_area, | |
1051 | GdkRectangle *cell_area, | |
1052 | GtkCellRendererState flags ); | |
1053 | ||
1054 | ||
1055 | static GObjectClass *cell_parent_class = NULL; | |
1056 | ||
1057 | } // extern "C" | |
1058 | ||
1059 | GType | |
1060 | gtk_wx_cell_renderer_get_type (void) | |
1061 | { | |
1062 | static GType cell_wx_type = 0; | |
1063 | ||
1064 | if (!cell_wx_type) | |
1065 | { | |
1066 | const GTypeInfo cell_wx_info = | |
1067 | { | |
1068 | sizeof (GtkWxCellRendererClass), | |
1069 | NULL, /* base_init */ | |
1070 | NULL, /* base_finalize */ | |
1071 | (GClassInitFunc) gtk_wx_cell_renderer_class_init, | |
1072 | NULL, /* class_finalize */ | |
1073 | NULL, /* class_data */ | |
1074 | sizeof (GtkWxCellRenderer), | |
1075 | 0, /* n_preallocs */ | |
1076 | (GInstanceInitFunc) gtk_wx_cell_renderer_init, | |
1077 | }; | |
1078 | ||
1079 | cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER, | |
1080 | "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 ); | |
1081 | } | |
1082 | ||
1083 | return cell_wx_type; | |
1084 | } | |
1085 | ||
1086 | static void | |
1087 | gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell) | |
1088 | { | |
1089 | cell->cell = NULL; | |
1090 | cell->last_click = 0; | |
1091 | } | |
1092 | ||
1093 | static void | |
1094 | gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass) | |
1095 | { | |
1096 | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
1097 | GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass); | |
1098 | ||
1099 | cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass); | |
1100 | ||
1101 | object_class->finalize = gtk_wx_cell_renderer_finalize; | |
1102 | ||
1103 | cell_class->get_size = gtk_wx_cell_renderer_get_size; | |
1104 | cell_class->render = gtk_wx_cell_renderer_render; | |
1105 | cell_class->activate = gtk_wx_cell_renderer_activate; | |
1106 | cell_class->start_editing = gtk_wx_cell_renderer_start_editing; | |
1107 | } | |
1108 | ||
1109 | static void | |
1110 | gtk_wx_cell_renderer_finalize (GObject *object) | |
1111 | { | |
1112 | /* must chain up */ | |
1113 | (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object); | |
1114 | } | |
1115 | ||
1116 | GtkCellRenderer* | |
1117 | gtk_wx_cell_renderer_new (void) | |
1118 | { | |
1119 | return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL); | |
1120 | } | |
1121 | ||
1122 | static GtkCellEditable *gtk_wx_cell_renderer_start_editing( | |
1123 | GtkCellRenderer *renderer, | |
1124 | GdkEvent *WXUNUSED(event), | |
1125 | GtkWidget *widget, | |
1126 | const gchar *path, | |
1127 | GdkRectangle *WXUNUSED(background_area), | |
1128 | GdkRectangle *cell_area, | |
1129 | GtkCellRendererState WXUNUSED(flags) ) | |
1130 | { | |
1131 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
1132 | wxDataViewCustomRenderer *cell = wxrenderer->cell; | |
1133 | ||
1134 | // Renderer doesn't support in-place editing | |
1135 | if (!cell->HasEditorCtrl()) | |
1136 | return NULL; | |
1137 | ||
1138 | // An in-place editing control is still around | |
1139 | if (cell->GetEditorCtrl()) | |
1140 | return NULL; | |
1141 | ||
1142 | GdkRectangle rect; | |
1143 | gtk_wx_cell_renderer_get_size (renderer, widget, cell_area, | |
1144 | &rect.x, | |
1145 | &rect.y, | |
1146 | &rect.width, | |
1147 | &rect.height); | |
1148 | ||
1149 | rect.x += cell_area->x; | |
1150 | rect.y += cell_area->y; | |
1151 | // rect.width -= renderer->xpad * 2; | |
1152 | // rect.height -= renderer->ypad * 2; | |
1153 | ||
1154 | // wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); | |
1155 | wxRect renderrect( cell_area->x, cell_area->y, cell_area->width, cell_area->height ); | |
1156 | ||
1157 | GtkTreePath *treepath = gtk_tree_path_new_from_string( path ); | |
1158 | GtkTreeIter iter; | |
1159 | cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, treepath ); | |
1160 | wxDataViewItem item( (void*) iter.user_data ); | |
1161 | gtk_tree_path_free( treepath ); | |
1162 | ||
1163 | cell->StartEditing( item, renderrect ); | |
1164 | ||
1165 | return NULL; | |
1166 | } | |
1167 | ||
1168 | static void | |
1169 | gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer, | |
1170 | GtkWidget *WXUNUSED(widget), | |
1171 | GdkRectangle *cell_area, | |
1172 | gint *x_offset, | |
1173 | gint *y_offset, | |
1174 | gint *width, | |
1175 | gint *height) | |
1176 | { | |
1177 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
1178 | wxDataViewCustomRenderer *cell = wxrenderer->cell; | |
1179 | ||
1180 | wxSize size = cell->GetSize(); | |
1181 | ||
1182 | gint calc_width = (gint) renderer->xpad * 2 + size.x; | |
1183 | gint calc_height = (gint) renderer->ypad * 2 + size.y; | |
1184 | ||
1185 | if (x_offset) | |
1186 | *x_offset = 0; | |
1187 | if (y_offset) | |
1188 | *y_offset = 0; | |
1189 | ||
1190 | if (cell_area && size.x > 0 && size.y > 0) | |
1191 | { | |
1192 | if (x_offset) | |
1193 | { | |
1194 | *x_offset = (gint)((renderer->xalign * | |
1195 | (cell_area->width - calc_width - 2 * renderer->xpad))); | |
1196 | *x_offset = MAX (*x_offset, 0) + renderer->xpad; | |
1197 | } | |
1198 | if (y_offset) | |
1199 | { | |
1200 | *y_offset = (gint)((renderer->yalign * | |
1201 | (cell_area->height - calc_height - 2 * renderer->ypad))); | |
1202 | *y_offset = MAX (*y_offset, 0) + renderer->ypad; | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | if (width) | |
1207 | *width = calc_width; | |
1208 | ||
1209 | if (height) | |
1210 | *height = calc_height; | |
1211 | } | |
1212 | ||
1213 | static void | |
1214 | gtk_wx_cell_renderer_render (GtkCellRenderer *renderer, | |
1215 | GdkWindow *window, | |
1216 | GtkWidget *widget, | |
1217 | GdkRectangle *background_area, | |
1218 | GdkRectangle *cell_area, | |
1219 | GdkRectangle *expose_area, | |
1220 | GtkCellRendererState flags) | |
1221 | ||
1222 | { | |
1223 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
1224 | wxDataViewCustomRenderer *cell = wxrenderer->cell; | |
1225 | ||
1226 | cell->window = window; | |
1227 | cell->widget = widget; | |
1228 | cell->background_area = (void*) background_area; | |
1229 | cell->cell_area = (void*) cell_area; | |
1230 | cell->expose_area = (void*) expose_area; | |
1231 | cell->flags = (int) flags; | |
1232 | ||
1233 | GdkRectangle rect; | |
1234 | gtk_wx_cell_renderer_get_size (renderer, widget, cell_area, | |
1235 | &rect.x, | |
1236 | &rect.y, | |
1237 | &rect.width, | |
1238 | &rect.height); | |
1239 | ||
1240 | rect.x += cell_area->x; | |
1241 | rect.y += cell_area->y; | |
1242 | rect.width -= renderer->xpad * 2; | |
1243 | rect.height -= renderer->ypad * 2; | |
1244 | ||
1245 | GdkRectangle dummy; | |
1246 | if (gdk_rectangle_intersect (expose_area, &rect, &dummy)) | |
1247 | { | |
1248 | wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); | |
1249 | wxWindowDC* dc = (wxWindowDC*) cell->GetDC(); | |
1250 | wxWindowDCImpl *impl = (wxWindowDCImpl *) dc->GetImpl(); | |
1251 | ||
1252 | // Reinitialize wxWindowDC's GDK window if drawing occurs into a different | |
1253 | // window such as a DnD drop window. | |
1254 | if (window != impl->m_gdkwindow) | |
1255 | { | |
1256 | impl->Destroy(); | |
1257 | impl->m_gdkwindow = window; | |
1258 | impl->SetUpDC(); | |
1259 | } | |
1260 | ||
1261 | int state = 0; | |
1262 | if (flags & GTK_CELL_RENDERER_SELECTED) | |
1263 | state |= wxDATAVIEW_CELL_SELECTED; | |
1264 | if (flags & GTK_CELL_RENDERER_PRELIT) | |
1265 | state |= wxDATAVIEW_CELL_PRELIT; | |
1266 | if (flags & GTK_CELL_RENDERER_INSENSITIVE) | |
1267 | state |= wxDATAVIEW_CELL_INSENSITIVE; | |
1268 | if (flags & GTK_CELL_RENDERER_INSENSITIVE) | |
1269 | state |= wxDATAVIEW_CELL_INSENSITIVE; | |
1270 | if (flags & GTK_CELL_RENDERER_FOCUSED) | |
1271 | state |= wxDATAVIEW_CELL_FOCUSED; | |
1272 | cell->Render( renderrect, dc, state ); | |
1273 | } | |
1274 | } | |
1275 | ||
1276 | static gboolean | |
1277 | gtk_wx_cell_renderer_activate( | |
1278 | GtkCellRenderer *renderer, | |
1279 | GdkEvent *event, | |
1280 | GtkWidget *widget, | |
1281 | const gchar *path, | |
1282 | GdkRectangle *WXUNUSED(background_area), | |
1283 | GdkRectangle *cell_area, | |
1284 | GtkCellRendererState WXUNUSED(flags) ) | |
1285 | { | |
1286 | GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer; | |
1287 | wxDataViewCustomRenderer *cell = wxrenderer->cell; | |
1288 | ||
1289 | GdkRectangle rect; | |
1290 | gtk_wx_cell_renderer_get_size (renderer, widget, cell_area, | |
1291 | &rect.x, | |
1292 | &rect.y, | |
1293 | &rect.width, | |
1294 | &rect.height); | |
1295 | ||
1296 | rect.x += cell_area->x; | |
1297 | rect.y += cell_area->y; | |
1298 | rect.width -= renderer->xpad * 2; | |
1299 | rect.height -= renderer->ypad * 2; | |
1300 | ||
1301 | wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); | |
1302 | ||
1303 | wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel(); | |
1304 | ||
1305 | GtkTreePath *treepath = gtk_tree_path_new_from_string( path ); | |
1306 | ||
1307 | GtkTreeIter iter; | |
1308 | cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, treepath ); | |
1309 | wxDataViewItem item( iter.user_data ); | |
1310 | gtk_tree_path_free( treepath ); | |
1311 | ||
1312 | unsigned int model_col = cell->GetOwner()->GetModelColumn(); | |
1313 | ||
1314 | if (!event) | |
1315 | { | |
1316 | bool ret = false; | |
1317 | ||
1318 | // activated by <ENTER> | |
1319 | if (cell->Activate( renderrect, model, item, model_col )) | |
1320 | ret = true; | |
1321 | ||
1322 | return ret; | |
1323 | } | |
1324 | else if (event->type == GDK_BUTTON_PRESS) | |
1325 | { | |
1326 | GdkEventButton *button_event = (GdkEventButton*) event; | |
1327 | wxPoint pt( ((int) button_event->x) - renderrect.x, | |
1328 | ((int) button_event->y) - renderrect.y ); | |
1329 | ||
1330 | bool ret = false; | |
1331 | if (button_event->button == 1) | |
1332 | { | |
1333 | if (cell->LeftClick( pt, renderrect, model, item, model_col )) | |
1334 | ret = true; | |
1335 | // TODO: query system double-click time | |
1336 | if (button_event->time - wxrenderer->last_click < 400) | |
1337 | if (cell->Activate( renderrect, model, item, model_col )) | |
1338 | ret = true; | |
1339 | } | |
1340 | wxrenderer->last_click = button_event->time; | |
1341 | ||
1342 | return ret; | |
1343 | } | |
1344 | ||
1345 | return false; | |
1346 | } | |
1347 | ||
1348 | // --------------------------------------------------------- | |
1349 | // wxGtkDataViewModelNotifier | |
1350 | // --------------------------------------------------------- | |
1351 | ||
1352 | class wxGtkDataViewModelNotifier: public wxDataViewModelNotifier | |
1353 | { | |
1354 | public: | |
1355 | wxGtkDataViewModelNotifier( GtkWxTreeModel *wxgtk_model, | |
1356 | wxDataViewModel *wx_model, | |
1357 | wxDataViewCtrl *ctrl ); | |
1358 | ~wxGtkDataViewModelNotifier(); | |
1359 | ||
1360 | virtual bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ); | |
1361 | virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ); | |
1362 | virtual bool ItemChanged( const wxDataViewItem &item ); | |
1363 | virtual bool ValueChanged( const wxDataViewItem &item, unsigned int col ); | |
1364 | virtual bool Cleared(); | |
1365 | virtual void Resort(); | |
1366 | ||
1367 | void SetGtkModel( GtkWxTreeModel *model ) { m_wxgtk_model = model; } | |
1368 | ||
1369 | private: | |
1370 | GtkWxTreeModel *m_wxgtk_model; | |
1371 | wxDataViewModel *m_wx_model; | |
1372 | wxDataViewCtrl *m_owner; | |
1373 | }; | |
1374 | ||
1375 | // --------------------------------------------------------- | |
1376 | // wxGtkDataViewListModelNotifier | |
1377 | // --------------------------------------------------------- | |
1378 | ||
1379 | wxGtkDataViewModelNotifier::wxGtkDataViewModelNotifier( | |
1380 | GtkWxTreeModel* wxgtk_model, wxDataViewModel *wx_model, | |
1381 | wxDataViewCtrl *ctrl ) | |
1382 | { | |
1383 | m_wxgtk_model = wxgtk_model; | |
1384 | m_wx_model = wx_model; | |
1385 | m_owner = ctrl; | |
1386 | } | |
1387 | ||
1388 | wxGtkDataViewModelNotifier::~wxGtkDataViewModelNotifier() | |
1389 | { | |
1390 | m_wx_model = NULL; | |
1391 | m_wxgtk_model = NULL; | |
1392 | } | |
1393 | ||
1394 | bool wxGtkDataViewModelNotifier::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ) | |
1395 | { | |
1396 | m_owner->GtkGetInternal()->ItemAdded( parent, item ); | |
1397 | ||
1398 | GtkTreeIter iter; | |
1399 | iter.stamp = m_wxgtk_model->stamp; | |
1400 | iter.user_data = (gpointer) item.GetID(); | |
1401 | ||
1402 | GtkTreePath *path = wxgtk_tree_model_get_path( | |
1403 | GTK_TREE_MODEL(m_wxgtk_model), &iter ); | |
1404 | gtk_tree_model_row_inserted( | |
1405 | GTK_TREE_MODEL(m_wxgtk_model), path, &iter); | |
1406 | gtk_tree_path_free (path); | |
1407 | ||
1408 | return true; | |
1409 | } | |
1410 | ||
1411 | bool wxGtkDataViewModelNotifier::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) | |
1412 | { | |
1413 | GtkTreeIter iter; | |
1414 | iter.stamp = m_wxgtk_model->stamp; | |
1415 | iter.user_data = (gpointer) item.GetID(); | |
1416 | ||
1417 | GtkTreePath *path = wxgtk_tree_model_get_path( | |
1418 | GTK_TREE_MODEL(m_wxgtk_model), &iter ); | |
1419 | gtk_tree_model_row_deleted( | |
1420 | GTK_TREE_MODEL(m_wxgtk_model), path ); | |
1421 | gtk_tree_path_free (path); | |
1422 | ||
1423 | m_owner->GtkGetInternal()->ItemDeleted( parent, item ); | |
1424 | ||
1425 | return true; | |
1426 | } | |
1427 | ||
1428 | void wxGtkDataViewModelNotifier::Resort() | |
1429 | { | |
1430 | m_owner->GtkGetInternal()->Resort(); | |
1431 | } | |
1432 | ||
1433 | bool wxGtkDataViewModelNotifier::ItemChanged( const wxDataViewItem &item ) | |
1434 | { | |
1435 | GtkTreeIter iter; | |
1436 | iter.stamp = m_wxgtk_model->stamp; | |
1437 | iter.user_data = (gpointer) item.GetID(); | |
1438 | ||
1439 | GtkTreePath *path = wxgtk_tree_model_get_path( | |
1440 | GTK_TREE_MODEL(m_wxgtk_model), &iter ); | |
1441 | gtk_tree_model_row_changed( | |
1442 | GTK_TREE_MODEL(m_wxgtk_model), path, &iter ); | |
1443 | gtk_tree_path_free (path); | |
1444 | ||
1445 | m_owner->GtkGetInternal()->ItemChanged( item ); | |
1446 | ||
1447 | return true; | |
1448 | } | |
1449 | ||
1450 | bool wxGtkDataViewModelNotifier::ValueChanged( const wxDataViewItem &item, unsigned int model_col ) | |
1451 | { | |
1452 | // This adds GTK+'s missing MVC logic for ValueChanged | |
1453 | unsigned int index; | |
1454 | for (index = 0; index < m_owner->GetColumnCount(); index++) | |
1455 | { | |
1456 | wxDataViewColumn *column = m_owner->GetColumn( index ); | |
1457 | if (column->GetModelColumn() == model_col) | |
1458 | { | |
1459 | GtkTreeView *widget = GTK_TREE_VIEW(m_owner->m_treeview); | |
1460 | GtkTreeViewColumn *gcolumn = GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()); | |
1461 | ||
1462 | // Get cell area | |
1463 | GtkTreeIter iter; | |
1464 | iter.stamp = m_wxgtk_model->stamp; | |
1465 | iter.user_data = (gpointer) item.GetID(); | |
1466 | GtkTreePath *path = wxgtk_tree_model_get_path( | |
1467 | GTK_TREE_MODEL(m_wxgtk_model), &iter ); | |
1468 | GdkRectangle cell_area; | |
1469 | gtk_tree_view_get_cell_area( widget, path, gcolumn, &cell_area ); | |
1470 | gtk_tree_path_free( path ); | |
1471 | ||
1472 | GtkAdjustment* hadjust = gtk_tree_view_get_hadjustment( widget ); | |
1473 | double d = gtk_adjustment_get_value( hadjust ); | |
1474 | int xdiff = (int) d; | |
1475 | ||
1476 | int ydiff = gcolumn->button->allocation.height; | |
1477 | // Redraw | |
1478 | gtk_widget_queue_draw_area( GTK_WIDGET(widget), | |
1479 | cell_area.x - xdiff, ydiff + cell_area.y, cell_area.width, cell_area.height ); | |
1480 | ||
1481 | m_owner->GtkGetInternal()->ValueChanged( item, model_col ); | |
1482 | ||
1483 | return true; | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | return false; | |
1488 | } | |
1489 | ||
1490 | bool wxGtkDataViewModelNotifier::Cleared() | |
1491 | { | |
1492 | gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->m_treeview), NULL ); | |
1493 | ||
1494 | // this will create a new GTK model | |
1495 | m_owner->GtkGetInternal()->Cleared(); | |
1496 | ||
1497 | SetGtkModel( m_owner->GtkGetInternal()->GetGtkModel() ); | |
1498 | ||
1499 | gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->m_treeview), GTK_TREE_MODEL(m_wxgtk_model) ); | |
1500 | ||
1501 | return false; | |
1502 | } | |
1503 | ||
1504 | // --------------------------------------------------------- | |
1505 | // wxDataViewRenderer | |
1506 | // --------------------------------------------------------- | |
1507 | ||
1508 | static gpointer s_user_data = NULL; | |
1509 | ||
1510 | static void | |
1511 | wxgtk_cell_editable_editing_done( GtkCellEditable *WXUNUSED(editable), | |
1512 | wxDataViewRenderer *wxrenderer ) | |
1513 | { | |
1514 | wxDataViewColumn *column = wxrenderer->GetOwner(); | |
1515 | wxDataViewCtrl *dv = column->GetOwner(); | |
1516 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv->GetId() ); | |
1517 | event.SetDataViewColumn( column ); | |
1518 | event.SetModel( dv->GetModel() ); | |
1519 | wxDataViewItem item( s_user_data ); | |
1520 | event.SetItem( item ); | |
1521 | dv->HandleWindowEvent( event ); | |
1522 | } | |
1523 | ||
1524 | static void | |
1525 | wxgtk_renderer_editing_started( GtkCellRenderer *WXUNUSED(cell), GtkCellEditable *editable, | |
1526 | gchar *path, wxDataViewRenderer *wxrenderer ) | |
1527 | { | |
1528 | if (!editable) | |
1529 | return; | |
1530 | ||
1531 | wxDataViewColumn *column = wxrenderer->GetOwner(); | |
1532 | wxDataViewCtrl *dv = column->GetOwner(); | |
1533 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv->GetId() ); | |
1534 | event.SetDataViewColumn( column ); | |
1535 | event.SetModel( dv->GetModel() ); | |
1536 | GtkTreePath *tree_path = gtk_tree_path_new_from_string( path ); | |
1537 | GtkTreeIter iter; | |
1538 | dv->GtkGetInternal()->get_iter( &iter, tree_path ); | |
1539 | gtk_tree_path_free( tree_path ); | |
1540 | wxDataViewItem item( iter.user_data ); | |
1541 | event.SetItem( item ); | |
1542 | dv->HandleWindowEvent( event ); | |
1543 | ||
1544 | if (GTK_IS_CELL_EDITABLE(editable)) | |
1545 | { | |
1546 | s_user_data = iter.user_data; | |
1547 | ||
1548 | g_signal_connect (GTK_CELL_EDITABLE (editable), "editing_done", | |
1549 | G_CALLBACK (wxgtk_cell_editable_editing_done), | |
1550 | (gpointer) wxrenderer ); | |
1551 | ||
1552 | } | |
1553 | } | |
1554 | ||
1555 | ||
1556 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase) | |
1557 | ||
1558 | wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode, | |
1559 | int align ) : | |
1560 | wxDataViewRendererBase( varianttype, mode, align ) | |
1561 | { | |
1562 | m_renderer = NULL; | |
1563 | ||
1564 | // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor, | |
1565 | // after the m_renderer pointer has been initialized | |
1566 | } | |
1567 | ||
1568 | void wxDataViewRenderer::GtkInitHandlers() | |
1569 | { | |
1570 | if (!gtk_check_version(2,6,0)) | |
1571 | { | |
1572 | g_signal_connect (GTK_CELL_RENDERER(m_renderer), "editing_started", | |
1573 | G_CALLBACK (wxgtk_renderer_editing_started), | |
1574 | this); | |
1575 | } | |
1576 | } | |
1577 | ||
1578 | void wxDataViewRenderer::SetMode( wxDataViewCellMode mode ) | |
1579 | { | |
1580 | GtkCellRendererMode gtkMode; | |
1581 | switch (mode) | |
1582 | { | |
1583 | case wxDATAVIEW_CELL_INERT: | |
1584 | gtkMode = GTK_CELL_RENDERER_MODE_INERT; | |
1585 | break; | |
1586 | ||
1587 | case wxDATAVIEW_CELL_ACTIVATABLE: | |
1588 | gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE; | |
1589 | break; | |
1590 | ||
1591 | case wxDATAVIEW_CELL_EDITABLE: | |
1592 | gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE; | |
1593 | break; | |
1594 | ||
1595 | default: | |
1596 | wxFAIL_MSG( "unknown wxDataViewCellMode value" ); | |
1597 | return; | |
1598 | } | |
1599 | ||
1600 | // This value is most often ignored in GtkTreeView | |
1601 | GValue gvalue = { 0, }; | |
1602 | g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() ); | |
1603 | g_value_set_enum( &gvalue, gtkMode ); | |
1604 | g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue ); | |
1605 | g_value_unset( &gvalue ); | |
1606 | } | |
1607 | ||
1608 | wxDataViewCellMode wxDataViewRenderer::GetMode() const | |
1609 | { | |
1610 | wxDataViewCellMode ret; | |
1611 | ||
1612 | GValue gvalue; | |
1613 | g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL); | |
1614 | ||
1615 | switch (g_value_get_enum(&gvalue)) | |
1616 | { | |
1617 | default: | |
1618 | wxFAIL_MSG( "unknown GtkCellRendererMode value" ); | |
1619 | // fall through (we have to return something) | |
1620 | ||
1621 | case GTK_CELL_RENDERER_MODE_INERT: | |
1622 | ret = wxDATAVIEW_CELL_INERT; | |
1623 | break; | |
1624 | ||
1625 | case GTK_CELL_RENDERER_MODE_ACTIVATABLE: | |
1626 | ret = wxDATAVIEW_CELL_ACTIVATABLE; | |
1627 | break; | |
1628 | ||
1629 | case GTK_CELL_RENDERER_MODE_EDITABLE: | |
1630 | ret = wxDATAVIEW_CELL_EDITABLE; | |
1631 | break; | |
1632 | } | |
1633 | ||
1634 | g_value_unset( &gvalue ); | |
1635 | ||
1636 | return ret; | |
1637 | } | |
1638 | ||
1639 | void wxDataViewRenderer::GtkUpdateAlignment() | |
1640 | { | |
1641 | int align = m_alignment; | |
1642 | ||
1643 | // query alignment from column ? | |
1644 | if (align == -1) | |
1645 | { | |
1646 | // None there yet | |
1647 | if (GetOwner() == NULL) | |
1648 | return; | |
1649 | ||
1650 | align = GetOwner()->GetAlignment(); | |
1651 | align |= wxALIGN_CENTRE_VERTICAL; | |
1652 | } | |
1653 | ||
1654 | // horizontal alignment: | |
1655 | ||
1656 | gfloat xalign = 0.0; | |
1657 | if (align & wxALIGN_RIGHT) | |
1658 | xalign = 1.0; | |
1659 | else if (align & wxALIGN_CENTER_HORIZONTAL) | |
1660 | xalign = 0.5; | |
1661 | ||
1662 | GValue gvalue = { 0, }; | |
1663 | g_value_init( &gvalue, G_TYPE_FLOAT ); | |
1664 | g_value_set_float( &gvalue, xalign ); | |
1665 | g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue ); | |
1666 | g_value_unset( &gvalue ); | |
1667 | ||
1668 | // vertical alignment: | |
1669 | ||
1670 | gfloat yalign = 0.0; | |
1671 | if (align & wxALIGN_BOTTOM) | |
1672 | yalign = 1.0; | |
1673 | else if (align & wxALIGN_CENTER_VERTICAL) | |
1674 | yalign = 0.5; | |
1675 | ||
1676 | GValue gvalue2 = { 0, }; | |
1677 | g_value_init( &gvalue2, G_TYPE_FLOAT ); | |
1678 | g_value_set_float( &gvalue2, yalign ); | |
1679 | g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 ); | |
1680 | g_value_unset( &gvalue2 ); | |
1681 | } | |
1682 | ||
1683 | void wxDataViewRenderer::SetAlignment( int align ) | |
1684 | { | |
1685 | m_alignment = align; | |
1686 | GtkUpdateAlignment(); | |
1687 | } | |
1688 | ||
1689 | int wxDataViewRenderer::GetAlignment() const | |
1690 | { | |
1691 | return m_alignment; | |
1692 | } | |
1693 | ||
1694 | // --------------------------------------------------------- | |
1695 | // wxDataViewTextRenderer | |
1696 | // --------------------------------------------------------- | |
1697 | ||
1698 | extern "C" { | |
1699 | static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer, | |
1700 | gchar *arg1, gchar *arg2, gpointer user_data ); | |
1701 | } | |
1702 | ||
1703 | static void wxGtkTextRendererEditedCallback( GtkCellRendererText *WXUNUSED(renderer), | |
1704 | gchar *arg1, gchar *arg2, gpointer user_data ) | |
1705 | { | |
1706 | wxDataViewRenderer *cell = (wxDataViewRenderer*) user_data; | |
1707 | ||
1708 | wxString tmp = wxGTK_CONV_BACK_FONT(arg2, cell->GetOwner()->GetOwner()->GetFont()); | |
1709 | wxVariant value = tmp; | |
1710 | if (!cell->Validate( value )) | |
1711 | return; | |
1712 | ||
1713 | wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel(); | |
1714 | ||
1715 | GtkTreePath *path = gtk_tree_path_new_from_string( arg1 ); | |
1716 | GtkTreeIter iter; | |
1717 | cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, path ); | |
1718 | wxDataViewItem item( (void*) iter.user_data );; | |
1719 | gtk_tree_path_free( path ); | |
1720 | ||
1721 | unsigned int model_col = cell->GetOwner()->GetModelColumn(); | |
1722 | ||
1723 | model->SetValue( value, item, model_col ); | |
1724 | model->ValueChanged( item, model_col ); | |
1725 | } | |
1726 | ||
1727 | IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer) | |
1728 | ||
1729 | wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode, | |
1730 | int align ) : | |
1731 | wxDataViewRenderer( varianttype, mode, align ) | |
1732 | { | |
1733 | GtkWxCellRendererText *text_renderer = gtk_wx_cell_renderer_text_new(); | |
1734 | text_renderer->wx_renderer = this; | |
1735 | m_renderer = (GtkCellRenderer*) text_renderer; | |
1736 | ||
1737 | if (mode & wxDATAVIEW_CELL_EDITABLE) | |
1738 | { | |
1739 | GValue gvalue = { 0, }; | |
1740 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1741 | g_value_set_boolean( &gvalue, true ); | |
1742 | g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue ); | |
1743 | g_value_unset( &gvalue ); | |
1744 | ||
1745 | g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this ); | |
1746 | ||
1747 | GtkInitHandlers(); | |
1748 | } | |
1749 | ||
1750 | SetMode(mode); | |
1751 | SetAlignment(align); | |
1752 | } | |
1753 | ||
1754 | bool wxDataViewTextRenderer::SetValue( const wxVariant &value ) | |
1755 | { | |
1756 | wxString tmp = value; | |
1757 | ||
1758 | GValue gvalue = { 0, }; | |
1759 | g_value_init( &gvalue, G_TYPE_STRING ); | |
1760 | g_value_set_string( &gvalue, wxGTK_CONV_FONT( tmp, GetOwner()->GetOwner()->GetFont() ) ); | |
1761 | g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
1762 | g_value_unset( &gvalue ); | |
1763 | ||
1764 | return true; | |
1765 | } | |
1766 | ||
1767 | bool wxDataViewTextRenderer::GetValue( wxVariant &value ) const | |
1768 | { | |
1769 | GValue gvalue = { 0, }; | |
1770 | g_value_init( &gvalue, G_TYPE_STRING ); | |
1771 | g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
1772 | wxString tmp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), const_cast<wxDataViewTextRenderer*>(this)->GetOwner()->GetOwner()->GetFont() ); | |
1773 | g_value_unset( &gvalue ); | |
1774 | ||
1775 | value = tmp; | |
1776 | ||
1777 | return true; | |
1778 | } | |
1779 | ||
1780 | void wxDataViewTextRenderer::SetAlignment( int align ) | |
1781 | { | |
1782 | wxDataViewRenderer::SetAlignment(align); | |
1783 | ||
1784 | if (gtk_check_version(2,10,0)) | |
1785 | return; | |
1786 | ||
1787 | // horizontal alignment: | |
1788 | PangoAlignment pangoAlign = PANGO_ALIGN_LEFT; | |
1789 | if (align & wxALIGN_RIGHT) | |
1790 | pangoAlign = PANGO_ALIGN_RIGHT; | |
1791 | else if (align & wxALIGN_CENTER_HORIZONTAL) | |
1792 | pangoAlign = PANGO_ALIGN_CENTER; | |
1793 | ||
1794 | GValue gvalue = { 0, }; | |
1795 | g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() ); | |
1796 | g_value_set_enum( &gvalue, pangoAlign ); | |
1797 | g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue ); | |
1798 | g_value_unset( &gvalue ); | |
1799 | } | |
1800 | ||
1801 | // --------------------------------------------------------- | |
1802 | // wxDataViewTextRendererAttr | |
1803 | // --------------------------------------------------------- | |
1804 | ||
1805 | IMPLEMENT_CLASS(wxDataViewTextRendererAttr,wxDataViewTextRenderer) | |
1806 | ||
1807 | wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString &varianttype, | |
1808 | wxDataViewCellMode mode, int align ) : | |
1809 | wxDataViewTextRenderer( varianttype, mode, align ) | |
1810 | { | |
1811 | } | |
1812 | ||
1813 | // --------------------------------------------------------- | |
1814 | // wxDataViewBitmapRenderer | |
1815 | // --------------------------------------------------------- | |
1816 | ||
1817 | IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer) | |
1818 | ||
1819 | wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode, | |
1820 | int align ) : | |
1821 | wxDataViewRenderer( varianttype, mode, align ) | |
1822 | { | |
1823 | m_renderer = (GtkCellRenderer*) gtk_cell_renderer_pixbuf_new(); | |
1824 | ||
1825 | SetMode(mode); | |
1826 | SetAlignment(align); | |
1827 | } | |
1828 | ||
1829 | bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value ) | |
1830 | { | |
1831 | if (value.GetType() == wxT("wxBitmap")) | |
1832 | { | |
1833 | wxBitmap bitmap; | |
1834 | bitmap << value; | |
1835 | ||
1836 | // This may create a Pixbuf representation in the | |
1837 | // wxBitmap object (and it will stay there) | |
1838 | GdkPixbuf *pixbuf = bitmap.GetPixbuf(); | |
1839 | ||
1840 | GValue gvalue = { 0, }; | |
1841 | g_value_init( &gvalue, G_TYPE_OBJECT ); | |
1842 | g_value_set_object( &gvalue, pixbuf ); | |
1843 | g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue ); | |
1844 | g_value_unset( &gvalue ); | |
1845 | ||
1846 | return true; | |
1847 | } | |
1848 | ||
1849 | if (value.GetType() == wxT("wxIcon")) | |
1850 | { | |
1851 | wxIcon bitmap; | |
1852 | bitmap << value; | |
1853 | ||
1854 | // This may create a Pixbuf representation in the | |
1855 | // wxBitmap object (and it will stay there) | |
1856 | GdkPixbuf *pixbuf = bitmap.GetPixbuf(); | |
1857 | ||
1858 | GValue gvalue = { 0, }; | |
1859 | g_value_init( &gvalue, G_TYPE_OBJECT ); | |
1860 | g_value_set_object( &gvalue, pixbuf ); | |
1861 | g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue ); | |
1862 | g_value_unset( &gvalue ); | |
1863 | ||
1864 | return true; | |
1865 | } | |
1866 | ||
1867 | return false; | |
1868 | } | |
1869 | ||
1870 | bool wxDataViewBitmapRenderer::GetValue( wxVariant &WXUNUSED(value) ) const | |
1871 | { | |
1872 | return false; | |
1873 | } | |
1874 | ||
1875 | // --------------------------------------------------------- | |
1876 | // wxDataViewToggleRenderer | |
1877 | // --------------------------------------------------------- | |
1878 | ||
1879 | extern "C" { | |
1880 | static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, | |
1881 | gchar *path, gpointer user_data ); | |
1882 | } | |
1883 | ||
1884 | static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, | |
1885 | gchar *path, gpointer user_data ) | |
1886 | { | |
1887 | wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data; | |
1888 | ||
1889 | // get old value | |
1890 | GValue gvalue = { 0, }; | |
1891 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1892 | g_object_get_property( G_OBJECT(renderer), "active", &gvalue ); | |
1893 | bool tmp = g_value_get_boolean( &gvalue ); | |
1894 | g_value_unset( &gvalue ); | |
1895 | // invert it | |
1896 | tmp = !tmp; | |
1897 | ||
1898 | wxVariant value = tmp; | |
1899 | if (!cell->Validate( value )) | |
1900 | return; | |
1901 | ||
1902 | wxDataViewModel *model = cell->GetOwner()->GetOwner()->GetModel(); | |
1903 | ||
1904 | GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path ); | |
1905 | GtkTreeIter iter; | |
1906 | cell->GetOwner()->GetOwner()->GtkGetInternal()->get_iter( &iter, gtk_path ); | |
1907 | wxDataViewItem item( (void*) iter.user_data );; | |
1908 | gtk_tree_path_free( gtk_path ); | |
1909 | ||
1910 | unsigned int model_col = cell->GetOwner()->GetModelColumn(); | |
1911 | ||
1912 | model->SetValue( value, item, model_col ); | |
1913 | model->ValueChanged( item, model_col ); | |
1914 | } | |
1915 | ||
1916 | IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer) | |
1917 | ||
1918 | wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype, | |
1919 | wxDataViewCellMode mode, int align ) : | |
1920 | wxDataViewRenderer( varianttype, mode, align ) | |
1921 | { | |
1922 | m_renderer = (GtkCellRenderer*) gtk_cell_renderer_toggle_new(); | |
1923 | ||
1924 | if (mode & wxDATAVIEW_CELL_ACTIVATABLE) | |
1925 | { | |
1926 | g_signal_connect_after( m_renderer, "toggled", | |
1927 | G_CALLBACK(wxGtkToggleRendererToggledCallback), this ); | |
1928 | } | |
1929 | else | |
1930 | { | |
1931 | GValue gvalue = { 0, }; | |
1932 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1933 | g_value_set_boolean( &gvalue, false ); | |
1934 | g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue ); | |
1935 | g_value_unset( &gvalue ); | |
1936 | } | |
1937 | ||
1938 | SetMode(mode); | |
1939 | SetAlignment(align); | |
1940 | } | |
1941 | ||
1942 | bool wxDataViewToggleRenderer::SetValue( const wxVariant &value ) | |
1943 | { | |
1944 | bool tmp = value; | |
1945 | ||
1946 | GValue gvalue = { 0, }; | |
1947 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1948 | g_value_set_boolean( &gvalue, tmp ); | |
1949 | g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue ); | |
1950 | g_value_unset( &gvalue ); | |
1951 | ||
1952 | return true; | |
1953 | } | |
1954 | ||
1955 | bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const | |
1956 | { | |
1957 | GValue gvalue = { 0, }; | |
1958 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
1959 | g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue ); | |
1960 | bool tmp = g_value_get_boolean( &gvalue ); | |
1961 | g_value_unset( &gvalue ); | |
1962 | ||
1963 | value = tmp; | |
1964 | ||
1965 | return true; | |
1966 | } | |
1967 | ||
1968 | // --------------------------------------------------------- | |
1969 | // wxDataViewCustomRenderer | |
1970 | // --------------------------------------------------------- | |
1971 | ||
1972 | class wxDataViewCtrlDCImpl: public wxWindowDCImpl | |
1973 | { | |
1974 | public: | |
1975 | wxDataViewCtrlDCImpl( wxDC *owner, wxDataViewCtrl *window ) : | |
1976 | wxWindowDCImpl( owner ) | |
1977 | { | |
1978 | GtkWidget *widget = window->m_treeview; | |
1979 | // Set later | |
1980 | m_gdkwindow = NULL; | |
1981 | ||
1982 | m_window = window; | |
1983 | ||
1984 | m_context = window->GTKGetPangoDefaultContext(); | |
1985 | m_layout = pango_layout_new( m_context ); | |
1986 | m_fontdesc = pango_font_description_copy( widget->style->font_desc ); | |
1987 | ||
1988 | m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget ); | |
1989 | ||
1990 | // Set m_gdkwindow later | |
1991 | // SetUpDC(); | |
1992 | } | |
1993 | }; | |
1994 | ||
1995 | class wxDataViewCtrlDC: public wxWindowDC | |
1996 | { | |
1997 | public: | |
1998 | wxDataViewCtrlDC( wxDataViewCtrl *window ) : | |
1999 | wxWindowDC( new wxDataViewCtrlDCImpl( this, window ) ) | |
2000 | { } | |
2001 | }; | |
2002 | ||
2003 | ||
2004 | // --------------------------------------------------------- | |
2005 | // wxDataViewCustomRenderer | |
2006 | // --------------------------------------------------------- | |
2007 | ||
2008 | IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer) | |
2009 | ||
2010 | wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype, | |
2011 | wxDataViewCellMode mode, int align, | |
2012 | bool no_init ) : | |
2013 | wxDataViewRenderer( varianttype, mode, align ) | |
2014 | { | |
2015 | m_dc = NULL; | |
2016 | m_text_renderer = NULL; | |
2017 | ||
2018 | if (no_init) | |
2019 | m_renderer = NULL; | |
2020 | else | |
2021 | Init(mode, align); | |
2022 | } | |
2023 | ||
2024 | void wxDataViewCustomRenderer::RenderText( const wxString &text, int xoffset, | |
2025 | wxRect WXUNUSED(cell), wxDC *WXUNUSED(dc), int WXUNUSED(state) ) | |
2026 | { | |
2027 | #if 0 | |
2028 | wxDataViewCtrl *view = GetOwner()->GetOwner(); | |
2029 | wxColour col = (state & wxDATAVIEW_CELL_SELECTED) ? | |
2030 | wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) : | |
2031 | view->GetForegroundColour(); | |
2032 | dc->SetTextForeground(col); | |
2033 | dc->DrawText( text, cell.x + xoffset, cell.y + ((cell.height - dc->GetCharHeight()) / 2)); | |
2034 | #else | |
2035 | if (!m_text_renderer) | |
2036 | m_text_renderer = gtk_cell_renderer_text_new(); | |
2037 | ||
2038 | GValue gvalue = { 0, }; | |
2039 | g_value_init( &gvalue, G_TYPE_STRING ); | |
2040 | g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) ); | |
2041 | g_object_set_property( G_OBJECT(m_text_renderer), "text", &gvalue ); | |
2042 | g_value_unset( &gvalue ); | |
2043 | ||
2044 | ((GdkRectangle*) cell_area)->x += xoffset; | |
2045 | ((GdkRectangle*) cell_area)->width -= xoffset; | |
2046 | ||
2047 | gtk_cell_renderer_render( m_text_renderer, | |
2048 | window, | |
2049 | widget, | |
2050 | (GdkRectangle*) background_area, | |
2051 | (GdkRectangle*) cell_area, | |
2052 | (GdkRectangle*) expose_area, | |
2053 | (GtkCellRendererState) flags ); | |
2054 | ||
2055 | ((GdkRectangle*) cell_area)->x -= xoffset; | |
2056 | ((GdkRectangle*) cell_area)->width += xoffset; | |
2057 | #endif | |
2058 | } | |
2059 | ||
2060 | bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align) | |
2061 | { | |
2062 | GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new(); | |
2063 | renderer->cell = this; | |
2064 | ||
2065 | m_renderer = (GtkCellRenderer*) renderer; | |
2066 | ||
2067 | SetMode(mode); | |
2068 | SetAlignment(align); | |
2069 | ||
2070 | GtkInitHandlers(); | |
2071 | ||
2072 | return true; | |
2073 | } | |
2074 | ||
2075 | wxDataViewCustomRenderer::~wxDataViewCustomRenderer() | |
2076 | { | |
2077 | if (m_dc) | |
2078 | delete m_dc; | |
2079 | ||
2080 | if (m_text_renderer) | |
2081 | gtk_object_sink( GTK_OBJECT(m_text_renderer) ); | |
2082 | } | |
2083 | ||
2084 | wxDC *wxDataViewCustomRenderer::GetDC() | |
2085 | { | |
2086 | if (m_dc == NULL) | |
2087 | { | |
2088 | if (GetOwner() == NULL) | |
2089 | return NULL; | |
2090 | if (GetOwner()->GetOwner() == NULL) | |
2091 | return NULL; | |
2092 | m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() ); | |
2093 | } | |
2094 | ||
2095 | return m_dc; | |
2096 | } | |
2097 | ||
2098 | // --------------------------------------------------------- | |
2099 | // wxDataViewProgressRenderer | |
2100 | // --------------------------------------------------------- | |
2101 | ||
2102 | IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer) | |
2103 | ||
2104 | wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label, | |
2105 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : | |
2106 | wxDataViewCustomRenderer( varianttype, mode, align, true ) | |
2107 | { | |
2108 | m_label = label; | |
2109 | m_value = 0; | |
2110 | ||
2111 | #ifdef __WXGTK26__ | |
2112 | if (!gtk_check_version(2,6,0)) | |
2113 | { | |
2114 | m_renderer = (GtkCellRenderer*) gtk_cell_renderer_progress_new(); | |
2115 | ||
2116 | GValue gvalue = { 0, }; | |
2117 | g_value_init( &gvalue, G_TYPE_STRING ); | |
2118 | ||
2119 | g_value_set_string( &gvalue, wxGTK_CONV_FONT( m_label, GetOwner()->GetOwner()->GetFont() ) ); | |
2120 | g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
2121 | g_value_unset( &gvalue ); | |
2122 | ||
2123 | SetMode(mode); | |
2124 | SetAlignment(align); | |
2125 | } | |
2126 | else | |
2127 | #endif | |
2128 | { | |
2129 | // Use custom cell code | |
2130 | wxDataViewCustomRenderer::Init(mode, align); | |
2131 | } | |
2132 | } | |
2133 | ||
2134 | wxDataViewProgressRenderer::~wxDataViewProgressRenderer() | |
2135 | { | |
2136 | } | |
2137 | ||
2138 | bool wxDataViewProgressRenderer::SetValue( const wxVariant &value ) | |
2139 | { | |
2140 | #ifdef __WXGTK26__ | |
2141 | if (!gtk_check_version(2,6,0)) | |
2142 | { | |
2143 | gint tmp = (long) value; | |
2144 | GValue gvalue = { 0, }; | |
2145 | g_value_init( &gvalue, G_TYPE_INT ); | |
2146 | g_value_set_int( &gvalue, tmp ); | |
2147 | g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue ); | |
2148 | g_value_unset( &gvalue ); | |
2149 | } | |
2150 | else | |
2151 | #endif | |
2152 | { | |
2153 | m_value = (long) value; | |
2154 | ||
2155 | if (m_value < 0) m_value = 0; | |
2156 | if (m_value > 100) m_value = 100; | |
2157 | } | |
2158 | ||
2159 | return true; | |
2160 | } | |
2161 | ||
2162 | bool wxDataViewProgressRenderer::GetValue( wxVariant &WXUNUSED(value) ) const | |
2163 | { | |
2164 | return false; | |
2165 | } | |
2166 | ||
2167 | bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) | |
2168 | { | |
2169 | double pct = (double)m_value / 100.0; | |
2170 | wxRect bar = cell; | |
2171 | bar.width = (int)(cell.width * pct); | |
2172 | dc->SetPen( *wxTRANSPARENT_PEN ); | |
2173 | dc->SetBrush( *wxBLUE_BRUSH ); | |
2174 | dc->DrawRectangle( bar ); | |
2175 | ||
2176 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); | |
2177 | dc->SetPen( *wxBLACK_PEN ); | |
2178 | dc->DrawRectangle( cell ); | |
2179 | ||
2180 | return true; | |
2181 | } | |
2182 | ||
2183 | wxSize wxDataViewProgressRenderer::GetSize() const | |
2184 | { | |
2185 | return wxSize(40,12); | |
2186 | } | |
2187 | ||
2188 | // ------------------------------------- | |
2189 | // wxDataViewChoiceRenderer | |
2190 | // ------------------------------------- | |
2191 | ||
2192 | wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString &choices, | |
2193 | wxDataViewCellMode mode, int alignment ) : | |
2194 | wxDataViewCustomRenderer( "string", mode, alignment, true ) | |
2195 | { | |
2196 | m_choices = choices; | |
2197 | ||
2198 | #ifdef __WXGTK26__ | |
2199 | if (!gtk_check_version(2,6,0)) | |
2200 | { | |
2201 | m_renderer = (GtkCellRenderer*) gtk_cell_renderer_combo_new(); | |
2202 | ||
2203 | GtkListStore *store = gtk_list_store_new( 1, G_TYPE_STRING ); | |
2204 | for (size_t n = 0; n < m_choices.GetCount(); n++) | |
2205 | { | |
2206 | gtk_list_store_insert_with_values( | |
2207 | store, NULL, n, 0, | |
2208 | static_cast<const char *>(m_choices[n].utf8_str()), -1 ); | |
2209 | } | |
2210 | ||
2211 | g_object_set (m_renderer, | |
2212 | "model", store, | |
2213 | "text-column", 0, | |
2214 | "has-entry", FALSE, | |
2215 | NULL); | |
2216 | ||
2217 | bool editable = (mode & wxDATAVIEW_CELL_EDITABLE); | |
2218 | g_object_set (m_renderer, "editable", editable, NULL); | |
2219 | ||
2220 | SetAlignment(alignment); | |
2221 | ||
2222 | g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this ); | |
2223 | ||
2224 | GtkInitHandlers(); | |
2225 | } | |
2226 | else | |
2227 | #endif | |
2228 | { | |
2229 | // Use custom cell code | |
2230 | wxDataViewCustomRenderer::Init(mode, alignment); | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state ) | |
2235 | { | |
2236 | RenderText( m_data, 0, rect, dc, state ); | |
2237 | return true; | |
2238 | } | |
2239 | ||
2240 | wxSize wxDataViewChoiceRenderer::GetSize() const | |
2241 | { | |
2242 | return wxSize(70,20); | |
2243 | } | |
2244 | ||
2245 | bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value ) | |
2246 | { | |
2247 | ||
2248 | #ifdef __WXGTK26__ | |
2249 | if (!gtk_check_version(2,6,0)) | |
2250 | { | |
2251 | GValue gvalue = { 0, }; | |
2252 | g_value_init( &gvalue, G_TYPE_STRING ); | |
2253 | g_value_set_string( &gvalue, wxGTK_CONV_FONT( value.GetString(), GetOwner()->GetOwner()->GetFont() ) ); | |
2254 | g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
2255 | g_value_unset( &gvalue ); | |
2256 | } | |
2257 | else | |
2258 | #endif | |
2259 | m_data = value.GetString(); | |
2260 | ||
2261 | return true; | |
2262 | } | |
2263 | ||
2264 | bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const | |
2265 | { | |
2266 | #ifdef __WXGTK26__ | |
2267 | if (!gtk_check_version(2,6,0)) | |
2268 | { | |
2269 | GValue gvalue = { 0, }; | |
2270 | g_value_init( &gvalue, G_TYPE_STRING ); | |
2271 | g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue ); | |
2272 | wxString temp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ), | |
2273 | const_cast<wxDataViewChoiceRenderer*>(this)->GetOwner()->GetOwner()->GetFont() ); | |
2274 | g_value_unset( &gvalue ); | |
2275 | value = temp; | |
2276 | ||
2277 | //wxPrintf( "temp %s\n", temp ); | |
2278 | // TODO: remove this code | |
2279 | } | |
2280 | else | |
2281 | #endif | |
2282 | value = m_data; | |
2283 | ||
2284 | return true; | |
2285 | } | |
2286 | ||
2287 | void wxDataViewChoiceRenderer::SetAlignment( int align ) | |
2288 | { | |
2289 | wxDataViewCustomRenderer::SetAlignment(align); | |
2290 | ||
2291 | if (gtk_check_version(2,10,0)) | |
2292 | return; | |
2293 | ||
2294 | // horizontal alignment: | |
2295 | PangoAlignment pangoAlign = PANGO_ALIGN_LEFT; | |
2296 | if (align & wxALIGN_RIGHT) | |
2297 | pangoAlign = PANGO_ALIGN_RIGHT; | |
2298 | else if (align & wxALIGN_CENTER_HORIZONTAL) | |
2299 | pangoAlign = PANGO_ALIGN_CENTER; | |
2300 | ||
2301 | GValue gvalue = { 0, }; | |
2302 | g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() ); | |
2303 | g_value_set_enum( &gvalue, pangoAlign ); | |
2304 | g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue ); | |
2305 | g_value_unset( &gvalue ); | |
2306 | } | |
2307 | ||
2308 | // --------------------------------------------------------- | |
2309 | // wxDataViewDateRenderer | |
2310 | // --------------------------------------------------------- | |
2311 | ||
2312 | class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow | |
2313 | { | |
2314 | public: | |
2315 | wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value, | |
2316 | wxDataViewModel *model, const wxDataViewItem &item, unsigned int col ) : | |
2317 | wxPopupTransientWindow( parent, wxBORDER_SIMPLE ) | |
2318 | { | |
2319 | m_model = model; | |
2320 | m_item = item; | |
2321 | m_col = col; | |
2322 | m_cal = new wxCalendarCtrl( this, -1, *value ); | |
2323 | wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); | |
2324 | sizer->Add( m_cal, 1, wxGROW ); | |
2325 | SetSizer( sizer ); | |
2326 | sizer->Fit( this ); | |
2327 | } | |
2328 | ||
2329 | virtual void OnDismiss() | |
2330 | { | |
2331 | } | |
2332 | ||
2333 | void OnCalendar( wxCalendarEvent &event ); | |
2334 | ||
2335 | wxCalendarCtrl *m_cal; | |
2336 | wxDataViewModel *m_model; | |
2337 | wxDataViewItem m_item; | |
2338 | unsigned int m_col; | |
2339 | ||
2340 | private: | |
2341 | DECLARE_EVENT_TABLE() | |
2342 | }; | |
2343 | ||
2344 | BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow) | |
2345 | EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar ) | |
2346 | END_EVENT_TABLE() | |
2347 | ||
2348 | void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event ) | |
2349 | { | |
2350 | wxDateTime date = event.GetDate(); | |
2351 | wxVariant value = date; | |
2352 | m_model->SetValue( value, m_item, m_col ); | |
2353 | m_model->ValueChanged( m_item, m_col ); | |
2354 | DismissAndNotify(); | |
2355 | } | |
2356 | ||
2357 | IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer) | |
2358 | ||
2359 | wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype, | |
2360 | wxDataViewCellMode mode, int align ) : | |
2361 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
2362 | { | |
2363 | SetMode(mode); | |
2364 | SetAlignment(align); | |
2365 | } | |
2366 | ||
2367 | bool wxDataViewDateRenderer::SetValue( const wxVariant &value ) | |
2368 | { | |
2369 | m_date = value.GetDateTime(); | |
2370 | ||
2371 | return true; | |
2372 | } | |
2373 | ||
2374 | bool wxDataViewDateRenderer::GetValue( wxVariant &WXUNUSED(value) ) const | |
2375 | { | |
2376 | return false; | |
2377 | } | |
2378 | ||
2379 | bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state ) | |
2380 | { | |
2381 | dc->SetFont( GetOwner()->GetOwner()->GetFont() ); | |
2382 | wxString tmp = m_date.FormatDate(); | |
2383 | RenderText( tmp, 0, cell, dc, state ); | |
2384 | return true; | |
2385 | } | |
2386 | ||
2387 | wxSize wxDataViewDateRenderer::GetSize() const | |
2388 | { | |
2389 | wxString tmp = m_date.FormatDate(); | |
2390 | wxCoord x,y,d; | |
2391 | GetView()->GetTextExtent( tmp, &x, &y, &d ); | |
2392 | return wxSize(x,y+d); | |
2393 | } | |
2394 | ||
2395 | bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model, | |
2396 | const wxDataViewItem &item, unsigned int col ) | |
2397 | { | |
2398 | wxVariant variant; | |
2399 | model->GetValue( variant, item, col ); | |
2400 | wxDateTime value = variant.GetDateTime(); | |
2401 | ||
2402 | wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient( | |
2403 | GetOwner()->GetOwner()->GetParent(), &value, model, item, col ); | |
2404 | wxPoint pos = wxGetMousePosition(); | |
2405 | popup->Move( pos ); | |
2406 | popup->Layout(); | |
2407 | popup->Popup( popup->m_cal ); | |
2408 | ||
2409 | return true; | |
2410 | } | |
2411 | ||
2412 | ||
2413 | // --------------------------------------------------------- | |
2414 | // wxDataViewIconTextRenderer | |
2415 | // --------------------------------------------------------- | |
2416 | ||
2417 | IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer) | |
2418 | ||
2419 | wxDataViewIconTextRenderer::wxDataViewIconTextRenderer( | |
2420 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : | |
2421 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
2422 | { | |
2423 | SetMode(mode); | |
2424 | SetAlignment(align); | |
2425 | } | |
2426 | ||
2427 | wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer() | |
2428 | { | |
2429 | } | |
2430 | ||
2431 | bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value ) | |
2432 | { | |
2433 | m_value << value; | |
2434 | return true; | |
2435 | } | |
2436 | ||
2437 | bool wxDataViewIconTextRenderer::GetValue( wxVariant &WXUNUSED(value) ) const | |
2438 | { | |
2439 | return false; | |
2440 | } | |
2441 | ||
2442 | bool wxDataViewIconTextRenderer::Render( wxRect cell, wxDC *dc, int state ) | |
2443 | { | |
2444 | const wxIcon &icon = m_value.GetIcon(); | |
2445 | int offset = 0; | |
2446 | if (icon.IsOk()) | |
2447 | { | |
2448 | int yoffset = wxMax( 0, (cell.height - icon.GetHeight()) / 2 ); | |
2449 | dc->DrawIcon( icon, cell.x, cell.y + yoffset ); | |
2450 | offset = icon.GetWidth() + 4; | |
2451 | } | |
2452 | ||
2453 | RenderText( m_value.GetText(), offset, cell, dc, state ); | |
2454 | ||
2455 | return true; | |
2456 | } | |
2457 | ||
2458 | wxSize wxDataViewIconTextRenderer::GetSize() const | |
2459 | { | |
2460 | wxSize size; | |
2461 | if (m_value.GetIcon().IsOk()) | |
2462 | size.x = 4 + m_value.GetIcon().GetWidth(); | |
2463 | wxCoord x,y,d; | |
2464 | GetView()->GetTextExtent( m_value.GetText(), &x, &y, &d ); | |
2465 | size.x += x; | |
2466 | size.y = y+d; | |
2467 | return size; | |
2468 | } | |
2469 | ||
2470 | wxControl* wxDataViewIconTextRenderer::CreateEditorCtrl( | |
2471 | wxWindow *WXUNUSED(parent), wxRect WXUNUSED(labelRect), const wxVariant &WXUNUSED(value) ) | |
2472 | { | |
2473 | return NULL; | |
2474 | } | |
2475 | ||
2476 | bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( | |
2477 | wxControl* WXUNUSED(editor), wxVariant &WXUNUSED(value) ) | |
2478 | { | |
2479 | return false; | |
2480 | } | |
2481 | ||
2482 | // --------------------------------------------------------- | |
2483 | // wxDataViewColumn | |
2484 | // --------------------------------------------------------- | |
2485 | ||
2486 | ||
2487 | static gboolean | |
2488 | gtk_dataview_header_button_press_callback( GtkWidget *WXUNUSED(widget), | |
2489 | GdkEventButton *gdk_event, | |
2490 | wxDataViewColumn *column ) | |
2491 | { | |
2492 | if (gdk_event->type != GDK_BUTTON_PRESS) | |
2493 | return FALSE; | |
2494 | ||
2495 | if (gdk_event->button == 1) | |
2496 | { | |
2497 | gs_lastLeftClickHeader = column; | |
2498 | ||
2499 | wxDataViewCtrl *dv = column->GetOwner(); | |
2500 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() ); | |
2501 | event.SetDataViewColumn( column ); | |
2502 | event.SetModel( dv->GetModel() ); | |
2503 | if (dv->HandleWindowEvent( event )) | |
2504 | return FALSE; | |
2505 | } | |
2506 | ||
2507 | if (gdk_event->button == 3) | |
2508 | { | |
2509 | wxDataViewCtrl *dv = column->GetOwner(); | |
2510 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, dv->GetId() ); | |
2511 | event.SetDataViewColumn( column ); | |
2512 | event.SetModel( dv->GetModel() ); | |
2513 | if (dv->HandleWindowEvent( event )) | |
2514 | return FALSE; | |
2515 | } | |
2516 | ||
2517 | return FALSE; | |
2518 | } | |
2519 | ||
2520 | extern "C" { | |
2521 | static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, | |
2522 | GtkCellRenderer *cell, | |
2523 | GtkTreeModel *model, | |
2524 | GtkTreeIter *iter, | |
2525 | gpointer data ); | |
2526 | } | |
2527 | ||
2528 | ||
2529 | static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *WXUNUSED(column), | |
2530 | GtkCellRenderer *renderer, | |
2531 | GtkTreeModel *model, | |
2532 | GtkTreeIter *iter, | |
2533 | gpointer data ) | |
2534 | { | |
2535 | g_return_if_fail (GTK_IS_WX_TREE_MODEL (model)); | |
2536 | GtkWxTreeModel *tree_model = (GtkWxTreeModel *) model; | |
2537 | ||
2538 | wxDataViewRenderer *cell = (wxDataViewRenderer*) data; | |
2539 | ||
2540 | wxDataViewItem item( (void*) iter->user_data ); | |
2541 | ||
2542 | wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel(); | |
2543 | ||
2544 | if (!wx_model->IsVirtualListModel()) | |
2545 | { | |
2546 | ||
2547 | if (wx_model->IsContainer( item )) | |
2548 | { | |
2549 | if (wx_model->HasContainerColumns( item ) || (cell->GetOwner()->GetModelColumn() == 0)) | |
2550 | { | |
2551 | GValue gvalue = { 0, }; | |
2552 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
2553 | g_value_set_boolean( &gvalue, TRUE ); | |
2554 | g_object_set_property( G_OBJECT(renderer), "visible", &gvalue ); | |
2555 | g_value_unset( &gvalue ); | |
2556 | } | |
2557 | else | |
2558 | { | |
2559 | GValue gvalue = { 0, }; | |
2560 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
2561 | g_value_set_boolean( &gvalue, FALSE ); | |
2562 | g_object_set_property( G_OBJECT(renderer), "visible", &gvalue ); | |
2563 | g_value_unset( &gvalue ); | |
2564 | ||
2565 | return; | |
2566 | } | |
2567 | } | |
2568 | else | |
2569 | { | |
2570 | GValue gvalue = { 0, }; | |
2571 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
2572 | g_value_set_boolean( &gvalue, TRUE ); | |
2573 | g_object_set_property( G_OBJECT(renderer), "visible", &gvalue ); | |
2574 | g_value_unset( &gvalue ); | |
2575 | } | |
2576 | ||
2577 | } | |
2578 | ||
2579 | wxVariant value; | |
2580 | wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() ); | |
2581 | ||
2582 | if (value.GetType() != cell->GetVariantType()) | |
2583 | wxLogError( wxT("Wrong type, required: %s but: %s"), | |
2584 | value.GetType().c_str(), | |
2585 | cell->GetVariantType().c_str() ); | |
2586 | ||
2587 | cell->SetValue( value ); | |
2588 | ||
2589 | if (cell->GtkHasAttributes()) | |
2590 | { | |
2591 | wxDataViewItemAttr attr; | |
2592 | bool colour_set = false; | |
2593 | bool style_set = false; | |
2594 | bool weight_set = false; | |
2595 | ||
2596 | if (wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr )) | |
2597 | { | |
2598 | // this must be a GtkCellRendererText | |
2599 | wxColour colour = attr.GetColour(); | |
2600 | if (colour.IsOk()) | |
2601 | { | |
2602 | const GdkColor * const gcol = colour.GetColor(); | |
2603 | ||
2604 | GValue gvalue = { 0, }; | |
2605 | g_value_init( &gvalue, GDK_TYPE_COLOR ); | |
2606 | g_value_set_boxed( &gvalue, gcol ); | |
2607 | g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue ); | |
2608 | g_value_unset( &gvalue ); | |
2609 | ||
2610 | colour_set = true; | |
2611 | } | |
2612 | ||
2613 | if (attr.GetItalic()) | |
2614 | { | |
2615 | GValue gvalue = { 0, }; | |
2616 | g_value_init( &gvalue, PANGO_TYPE_STYLE ); | |
2617 | g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC ); | |
2618 | g_object_set_property( G_OBJECT(renderer), "style", &gvalue ); | |
2619 | g_value_unset( &gvalue ); | |
2620 | ||
2621 | style_set = true; | |
2622 | } | |
2623 | ||
2624 | if (attr.GetBold()) | |
2625 | { | |
2626 | GValue gvalue = { 0, }; | |
2627 | g_value_init( &gvalue, PANGO_TYPE_WEIGHT ); | |
2628 | g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD ); | |
2629 | g_object_set_property( G_OBJECT(renderer), "weight", &gvalue ); | |
2630 | g_value_unset( &gvalue ); | |
2631 | ||
2632 | weight_set = true; | |
2633 | } | |
2634 | } | |
2635 | ||
2636 | if (!style_set) | |
2637 | { | |
2638 | GValue gvalue = { 0, }; | |
2639 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
2640 | g_value_set_boolean( &gvalue, FALSE ); | |
2641 | g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue ); | |
2642 | g_value_unset( &gvalue ); | |
2643 | } | |
2644 | ||
2645 | if (!weight_set) | |
2646 | { | |
2647 | GValue gvalue = { 0, }; | |
2648 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
2649 | g_value_set_boolean( &gvalue, FALSE ); | |
2650 | g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue ); | |
2651 | g_value_unset( &gvalue ); | |
2652 | } | |
2653 | ||
2654 | if (!colour_set) | |
2655 | { | |
2656 | GValue gvalue = { 0, }; | |
2657 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
2658 | g_value_set_boolean( &gvalue, FALSE ); | |
2659 | g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue ); | |
2660 | g_value_unset( &gvalue ); | |
2661 | } | |
2662 | } | |
2663 | ||
2664 | #if 0 | |
2665 | if (attr.HasBackgroundColour()) | |
2666 | { | |
2667 | wxColour colour = attr.GetBackgroundColour(); | |
2668 | const GdkColor * const gcol = colour.GetColor(); | |
2669 | ||
2670 | GValue gvalue = { 0, }; | |
2671 | g_value_init( &gvalue, GDK_TYPE_COLOR ); | |
2672 | g_value_set_boxed( &gvalue, gcol ); | |
2673 | g_object_set_property( G_OBJECT(renderer), "cell-background_gdk", &gvalue ); | |
2674 | g_value_unset( &gvalue ); | |
2675 | } | |
2676 | else | |
2677 | { | |
2678 | GValue gvalue = { 0, }; | |
2679 | g_value_init( &gvalue, G_TYPE_BOOLEAN ); | |
2680 | g_value_set_boolean( &gvalue, FALSE ); | |
2681 | g_object_set_property( G_OBJECT(renderer), "cell-background-set", &gvalue ); | |
2682 | g_value_unset( &gvalue ); | |
2683 | } | |
2684 | #endif | |
2685 | ||
2686 | } | |
2687 | ||
2688 | #include <wx/listimpl.cpp> | |
2689 | WX_DEFINE_LIST(wxDataViewColumnList) | |
2690 | ||
2691 | wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell, | |
2692 | unsigned int model_column, int width, | |
2693 | wxAlignment align, int flags ) | |
2694 | : wxDataViewColumnBase( cell, model_column ) | |
2695 | { | |
2696 | Init( align, flags, width ); | |
2697 | ||
2698 | SetTitle( title ); | |
2699 | } | |
2700 | ||
2701 | wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell, | |
2702 | unsigned int model_column, int width, | |
2703 | wxAlignment align, int flags ) | |
2704 | : wxDataViewColumnBase( bitmap, cell, model_column ) | |
2705 | { | |
2706 | Init( align, flags, width ); | |
2707 | ||
2708 | SetBitmap( bitmap ); | |
2709 | } | |
2710 | ||
2711 | void wxDataViewColumn::Init(wxAlignment align, int flags, int width) | |
2712 | { | |
2713 | m_isConnected = false; | |
2714 | ||
2715 | GtkCellRenderer *renderer = (GtkCellRenderer *) GetRenderer()->GetGtkHandle(); | |
2716 | GtkTreeViewColumn *column = gtk_tree_view_column_new(); | |
2717 | m_column = (GtkWidget*) column; | |
2718 | ||
2719 | SetFlags( flags ); | |
2720 | SetAlignment( align ); | |
2721 | ||
2722 | SetWidth( width ); | |
2723 | ||
2724 | // Create container for icon and label | |
2725 | GtkWidget *box = gtk_hbox_new( FALSE, 1 ); | |
2726 | gtk_widget_show( box ); | |
2727 | // gtk_container_set_border_width((GtkContainer*)box, 2); | |
2728 | m_image = gtk_image_new(); | |
2729 | gtk_box_pack_start(GTK_BOX(box), m_image, FALSE, FALSE, 1); | |
2730 | m_label = gtk_label_new(""); | |
2731 | gtk_box_pack_end( GTK_BOX(box), GTK_WIDGET(m_label), FALSE, FALSE, 1 ); | |
2732 | gtk_tree_view_column_set_widget( column, box ); | |
2733 | ||
2734 | gtk_tree_view_column_pack_end( column, renderer, TRUE ); | |
2735 | ||
2736 | gtk_tree_view_column_set_cell_data_func( column, renderer, | |
2737 | wxGtkTreeCellDataFunc, (gpointer) GetRenderer(), NULL ); | |
2738 | } | |
2739 | ||
2740 | void wxDataViewColumn::OnInternalIdle() | |
2741 | { | |
2742 | if (m_isConnected) | |
2743 | return; | |
2744 | ||
2745 | if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview)) | |
2746 | { | |
2747 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2748 | if (column->button) | |
2749 | { | |
2750 | g_signal_connect(column->button, "button_press_event", | |
2751 | G_CALLBACK (gtk_dataview_header_button_press_callback), this); | |
2752 | ||
2753 | m_isConnected = true; | |
2754 | } | |
2755 | } | |
2756 | } | |
2757 | ||
2758 | void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner ) | |
2759 | { | |
2760 | wxDataViewColumnBase::SetOwner( owner ); | |
2761 | ||
2762 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2763 | ||
2764 | gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) ); | |
2765 | } | |
2766 | ||
2767 | void wxDataViewColumn::SetTitle( const wxString &title ) | |
2768 | { | |
2769 | wxDataViewCtrl *ctrl = GetOwner(); | |
2770 | gtk_label_set_text( GTK_LABEL(m_label), ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont()) | |
2771 | : wxGTK_CONV_SYS(title) ); | |
2772 | if (title.empty()) | |
2773 | gtk_widget_hide( m_label ); | |
2774 | else | |
2775 | gtk_widget_show( m_label ); | |
2776 | } | |
2777 | ||
2778 | wxString wxDataViewColumn::GetTitle() const | |
2779 | { | |
2780 | return wxGTK_CONV_BACK_FONT( | |
2781 | gtk_label_get_text( GTK_LABEL(m_label) ), | |
2782 | GetOwner()->GetFont() | |
2783 | ); | |
2784 | } | |
2785 | ||
2786 | void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap ) | |
2787 | { | |
2788 | wxDataViewColumnBase::SetBitmap( bitmap ); | |
2789 | ||
2790 | if (bitmap.Ok()) | |
2791 | { | |
2792 | GtkImage *gtk_image = GTK_IMAGE(m_image); | |
2793 | ||
2794 | GdkBitmap *mask = NULL; | |
2795 | if (bitmap.GetMask()) | |
2796 | mask = bitmap.GetMask()->GetBitmap(); | |
2797 | ||
2798 | if (bitmap.HasPixbuf()) | |
2799 | { | |
2800 | gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image), | |
2801 | bitmap.GetPixbuf()); | |
2802 | } | |
2803 | else | |
2804 | { | |
2805 | gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image), | |
2806 | bitmap.GetPixmap(), mask); | |
2807 | } | |
2808 | gtk_widget_show( m_image ); | |
2809 | } | |
2810 | else | |
2811 | { | |
2812 | gtk_widget_hide( m_image ); | |
2813 | } | |
2814 | } | |
2815 | ||
2816 | void wxDataViewColumn::SetHidden( bool hidden ) | |
2817 | { | |
2818 | gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden ); | |
2819 | } | |
2820 | ||
2821 | void wxDataViewColumn::SetResizeable( bool resizeable ) | |
2822 | { | |
2823 | gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable ); | |
2824 | } | |
2825 | ||
2826 | void wxDataViewColumn::SetAlignment( wxAlignment align ) | |
2827 | { | |
2828 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2829 | ||
2830 | gfloat xalign = 0.0; | |
2831 | if (align == wxALIGN_RIGHT) | |
2832 | xalign = 1.0; | |
2833 | if (align == wxALIGN_CENTER_HORIZONTAL || | |
2834 | align == wxALIGN_CENTER) | |
2835 | xalign = 0.5; | |
2836 | ||
2837 | gtk_tree_view_column_set_alignment( column, xalign ); | |
2838 | ||
2839 | if (m_renderer && m_renderer->GetAlignment() == -1) | |
2840 | m_renderer->GtkUpdateAlignment(); | |
2841 | } | |
2842 | ||
2843 | wxAlignment wxDataViewColumn::GetAlignment() const | |
2844 | { | |
2845 | gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) ); | |
2846 | ||
2847 | if (xalign == 1.0) | |
2848 | return wxALIGN_RIGHT; | |
2849 | if (xalign == 0.5) | |
2850 | return wxALIGN_CENTER_HORIZONTAL; | |
2851 | ||
2852 | return wxALIGN_LEFT; | |
2853 | } | |
2854 | ||
2855 | void wxDataViewColumn::SetSortable( bool sortable ) | |
2856 | { | |
2857 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2858 | ||
2859 | if ( sortable ) | |
2860 | { | |
2861 | gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() ); | |
2862 | } | |
2863 | else | |
2864 | { | |
2865 | gtk_tree_view_column_set_sort_column_id( column, -1 ); | |
2866 | gtk_tree_view_column_set_sort_indicator( column, FALSE ); | |
2867 | gtk_tree_view_column_set_clickable( column, FALSE ); | |
2868 | } | |
2869 | } | |
2870 | ||
2871 | bool wxDataViewColumn::IsSortable() const | |
2872 | { | |
2873 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2874 | return gtk_tree_view_column_get_clickable( column ); | |
2875 | } | |
2876 | ||
2877 | void wxDataViewColumn::SetAsSortKey( bool WXUNUSED(sort) ) | |
2878 | { | |
2879 | // it might not make sense to have this function in wxHeaderColumn at | |
2880 | // all in fact, changing of the sort order should only be done using the | |
2881 | // associated control API | |
2882 | wxFAIL_MSG( "not implemented" ); | |
2883 | } | |
2884 | ||
2885 | bool wxDataViewColumn::IsSortKey() const | |
2886 | { | |
2887 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2888 | return gtk_tree_view_column_get_sort_indicator( column ); | |
2889 | } | |
2890 | ||
2891 | bool wxDataViewColumn::IsResizeable() const | |
2892 | { | |
2893 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2894 | return gtk_tree_view_column_get_resizable( column ); | |
2895 | } | |
2896 | ||
2897 | bool wxDataViewColumn::IsHidden() const | |
2898 | { | |
2899 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2900 | return !gtk_tree_view_column_get_visible( column ); | |
2901 | } | |
2902 | ||
2903 | void wxDataViewColumn::SetSortOrder( bool ascending ) | |
2904 | { | |
2905 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2906 | ||
2907 | if (ascending) | |
2908 | gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING ); | |
2909 | else | |
2910 | gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING ); | |
2911 | ||
2912 | gtk_tree_view_column_set_sort_indicator( column, TRUE ); | |
2913 | } | |
2914 | ||
2915 | bool wxDataViewColumn::IsSortOrderAscending() const | |
2916 | { | |
2917 | GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column); | |
2918 | ||
2919 | return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING); | |
2920 | } | |
2921 | ||
2922 | void wxDataViewColumn::SetMinWidth( int width ) | |
2923 | { | |
2924 | gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width ); | |
2925 | } | |
2926 | ||
2927 | int wxDataViewColumn::GetMinWidth() const | |
2928 | { | |
2929 | return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) ); | |
2930 | } | |
2931 | ||
2932 | int wxDataViewColumn::GetWidth() const | |
2933 | { | |
2934 | return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) ); | |
2935 | } | |
2936 | ||
2937 | void wxDataViewColumn::SetWidth( int width ) | |
2938 | { | |
2939 | if (width < 0) | |
2940 | { | |
2941 | #if 1 | |
2942 | gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED ); | |
2943 | ||
2944 | // TODO find a better calculation | |
2945 | gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), wxDVC_DEFAULT_WIDTH ); | |
2946 | #else | |
2947 | // this is unpractical for large numbers of items and disables | |
2948 | // user resizing, which is totally unexpected | |
2949 | gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE ); | |
2950 | #endif | |
2951 | } | |
2952 | else | |
2953 | { | |
2954 | gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED ); | |
2955 | ||
2956 | gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width ); | |
2957 | } | |
2958 | } | |
2959 | ||
2960 | void wxDataViewColumn::SetReorderable( bool reorderable ) | |
2961 | { | |
2962 | gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable ); | |
2963 | } | |
2964 | ||
2965 | bool wxDataViewColumn::IsReorderable() const | |
2966 | { | |
2967 | return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) ); | |
2968 | } | |
2969 | ||
2970 | //----------------------------------------------------------------------------- | |
2971 | // wxGtkTreeModelNode | |
2972 | //----------------------------------------------------------------------------- | |
2973 | ||
2974 | #if 0 | |
2975 | class wxGtkTreeModelChildWithPos | |
2976 | { | |
2977 | public: | |
2978 | unsigned int pos; | |
2979 | void *id; | |
2980 | }; | |
2981 | ||
2982 | static | |
2983 | int wxGtkTreeModelChildWithPosCmp( const void* data1, const void* data2, const void* user_data ) | |
2984 | { | |
2985 | const wxGtkTreeModelChildWithPos* child1 = (const wxGtkTreeModelChildWithPos*) data1; | |
2986 | const wxGtkTreeModelChildWithPos* child2 = (const wxGtkTreeModelChildWithPos*) data2; | |
2987 | const wxDataViewCtrlInternal *internal = (const wxDataViewCtrlInternal *) user_data; | |
2988 | int ret = internal->GetDataViewModel()->Compare( child1->id, child2->id, | |
2989 | internal->GetSortColumn(), (internal->GetSortOrder() == GTK_SORT_DESCENDING) ); | |
2990 | ||
2991 | return ret; | |
2992 | } | |
2993 | #else | |
2994 | static | |
2995 | int LINKAGEMODE wxGtkTreeModelChildPtrCmp( void*** data1, void*** data2 ) | |
2996 | { | |
2997 | return gs_internal->GetDataViewModel()->Compare( **data1, **data2, | |
2998 | gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) ); | |
2999 | } | |
3000 | ||
3001 | WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr ); | |
3002 | #endif | |
3003 | ||
3004 | void wxGtkTreeModelNode::Resort() | |
3005 | { | |
3006 | size_t child_count = GetChildCount(); | |
3007 | if (child_count == 0) | |
3008 | return; | |
3009 | ||
3010 | size_t node_count = GetNodesCount(); | |
3011 | ||
3012 | if (child_count == 1) | |
3013 | { | |
3014 | if (node_count == 1) | |
3015 | { | |
3016 | wxGtkTreeModelNode *node = m_nodes.Item( 0 ); | |
3017 | node->Resort(); | |
3018 | } | |
3019 | return; | |
3020 | } | |
3021 | ||
3022 | gint *new_order = new gint[child_count]; | |
3023 | ||
3024 | #if 1 | |
3025 | // m_children has the original *void | |
3026 | // ptrs points to these | |
3027 | wxGtkTreeModelChildrenPtr ptrs; | |
3028 | size_t i; | |
3029 | for (i = 0; i < child_count; i++) | |
3030 | ptrs.Add( &(m_children[i]) ); | |
3031 | // Sort the ptrs | |
3032 | gs_internal = m_internal; | |
3033 | ptrs.Sort( &wxGtkTreeModelChildPtrCmp ); | |
3034 | ||
3035 | wxGtkTreeModelChildren temp; | |
3036 | void** base_ptr = &(m_children[0]); | |
3037 | // Transfer positions to new_order array and | |
3038 | // IDs to temp | |
3039 | for (i = 0; i < child_count; i++) | |
3040 | { | |
3041 | new_order[i] = ptrs[i] - base_ptr; | |
3042 | temp.Add( *ptrs[i] ); | |
3043 | } | |
3044 | ||
3045 | // Transfer IDs back to m_children | |
3046 | m_children.Clear(); | |
3047 | WX_APPEND_ARRAY( temp, m_children ); | |
3048 | #endif | |
3049 | #if 0 | |
3050 | // Too slow | |
3051 | ||
3052 | // Build up array with IDs and original positions | |
3053 | wxGtkTreeModelChildWithPos* temp = new wxGtkTreeModelChildWithPos[child_count]; | |
3054 | size_t i; | |
3055 | for (i = 0; i < child_count; i++) | |
3056 | { | |
3057 | temp[i].pos = i; | |
3058 | temp[i].id = m_children[i]; | |
3059 | } | |
3060 | // Sort array keeping original positions | |
3061 | wxQsort( temp, child_count, sizeof(wxGtkTreeModelChildWithPos), | |
3062 | &wxGtkTreeModelChildWithPosCmp, m_internal ); | |
3063 | // Transfer positions to new_order array and | |
3064 | // IDs to m_children | |
3065 | m_children.Clear(); | |
3066 | for (i = 0; i < child_count; i++) | |
3067 | { | |
3068 | new_order[i] = temp[i].pos; | |
3069 | m_children.Add( temp[i].id ); | |
3070 | } | |
3071 | // Delete array | |
3072 | delete [] temp; | |
3073 | #endif | |
3074 | ||
3075 | #if 0 | |
3076 | // Too slow | |
3077 | ||
3078 | wxGtkTreeModelChildren temp; | |
3079 | WX_APPEND_ARRAY( temp, m_children ); | |
3080 | ||
3081 | gs_internal = m_internal; | |
3082 | m_children.Sort( &wxGtkTreeModelChildCmp ); | |
3083 | ||
3084 | unsigned int pos; | |
3085 | for (pos = 0; pos < child_count; pos++) | |
3086 | { | |
3087 | void *id = m_children.Item( pos ); | |
3088 | int old_pos = temp.Index( id ); | |
3089 | new_order[pos] = old_pos; | |
3090 | } | |
3091 | #endif | |
3092 | ||
3093 | GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() ); | |
3094 | ||
3095 | GtkTreeIter iter; | |
3096 | iter.user_data = GetItem().GetID(); | |
3097 | iter.stamp = m_internal->GetGtkModel()->stamp; | |
3098 | ||
3099 | GtkTreePath *path = m_internal->get_path( &iter ); | |
3100 | ||
3101 | gtk_tree_model_rows_reordered( gtk_tree_model, path, &iter, new_order ); | |
3102 | ||
3103 | gtk_tree_path_free (path); | |
3104 | ||
3105 | delete [] new_order; | |
3106 | ||
3107 | unsigned int pos; | |
3108 | for (pos = 0; pos < node_count; pos++) | |
3109 | { | |
3110 | wxGtkTreeModelNode *node = m_nodes.Item( pos ); | |
3111 | node->Resort(); | |
3112 | } | |
3113 | } | |
3114 | ||
3115 | //----------------------------------------------------------------------------- | |
3116 | // wxDataViewCtrlInternal | |
3117 | //----------------------------------------------------------------------------- | |
3118 | ||
3119 | wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner, | |
3120 | wxDataViewModel *wx_model, GtkWxTreeModel *gtk_model ) | |
3121 | { | |
3122 | m_owner = owner; | |
3123 | m_wx_model = wx_model; | |
3124 | m_gtk_model = gtk_model; | |
3125 | m_root = NULL; | |
3126 | m_sort_order = GTK_SORT_ASCENDING; | |
3127 | m_sort_column = -1; | |
3128 | m_dataview_sort_column = NULL; | |
3129 | ||
3130 | m_dragDataObject = NULL; | |
3131 | m_dropDataObject = NULL; | |
3132 | ||
3133 | if (!m_wx_model->IsVirtualListModel()) | |
3134 | InitTree(); | |
3135 | } | |
3136 | ||
3137 | wxDataViewCtrlInternal::~wxDataViewCtrlInternal() | |
3138 | { | |
3139 | g_object_unref( m_gtk_model ); | |
3140 | ||
3141 | delete m_dragDataObject; | |
3142 | delete m_dropDataObject; | |
3143 | } | |
3144 | ||
3145 | void wxDataViewCtrlInternal::InitTree() | |
3146 | { | |
3147 | wxDataViewItem item; | |
3148 | m_root = new wxGtkTreeModelNode( NULL, item, this ); | |
3149 | ||
3150 | BuildBranch( m_root ); | |
3151 | } | |
3152 | ||
3153 | void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node ) | |
3154 | { | |
3155 | if (node->GetChildCount() == 0) | |
3156 | { | |
3157 | wxDataViewItemArray children; | |
3158 | unsigned int count = m_wx_model->GetChildren( node->GetItem(), children ); | |
3159 | unsigned int pos; | |
3160 | for (pos = 0; pos < count; pos++) | |
3161 | { | |
3162 | wxDataViewItem child = children[pos]; | |
3163 | ||
3164 | if (m_wx_model->IsContainer( child )) | |
3165 | node->AddNode( new wxGtkTreeModelNode( node, child, this ) ); | |
3166 | else | |
3167 | node->AddLeave( child.GetID() ); | |
3168 | ||
3169 | // Don't send any events here | |
3170 | } | |
3171 | } | |
3172 | } | |
3173 | ||
3174 | // GTK+ dnd iface | |
3175 | ||
3176 | bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format ) | |
3177 | { | |
3178 | wxGtkString atom_str( gdk_atom_name( format ) ); | |
3179 | m_dragSourceTargetEntryTarget = wxCharBuffer( atom_str ); | |
3180 | ||
3181 | m_dragSourceTargetEntry.target = m_dragSourceTargetEntryTarget.data(); | |
3182 | m_dragSourceTargetEntry.flags = 0; | |
3183 | m_dragSourceTargetEntry.info = static_cast<guint>(-1); | |
3184 | ||
3185 | gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ), | |
3186 | GDK_BUTTON1_MASK, &m_dragSourceTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY ); | |
3187 | ||
3188 | return true; | |
3189 | } | |
3190 | ||
3191 | bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat &format ) | |
3192 | { | |
3193 | wxGtkString atom_str( gdk_atom_name( format ) ); | |
3194 | m_dropTargetTargetEntryTarget = wxCharBuffer( atom_str ); | |
3195 | ||
3196 | m_dropTargetTargetEntry.target = m_dropTargetTargetEntryTarget.data(); | |
3197 | m_dropTargetTargetEntry.flags = 0; | |
3198 | m_dropTargetTargetEntry.info = static_cast<guint>(-1); | |
3199 | ||
3200 | gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ), | |
3201 | &m_dropTargetTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY ); | |
3202 | ||
3203 | return true; | |
3204 | } | |
3205 | ||
3206 | gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source), | |
3207 | GtkTreePath *path ) | |
3208 | { | |
3209 | delete m_dragDataObject; | |
3210 | ||
3211 | GtkTreeIter iter; | |
3212 | if (!get_iter( &iter, path )) return FALSE; | |
3213 | wxDataViewItem item( (void*) iter.user_data ); | |
3214 | ||
3215 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() ); | |
3216 | event.SetEventObject( m_owner ); | |
3217 | event.SetItem( item ); | |
3218 | event.SetModel( m_wx_model ); | |
3219 | if (!m_owner->HandleWindowEvent( event )) | |
3220 | return FALSE; | |
3221 | ||
3222 | if (!event.IsAllowed()) | |
3223 | return FALSE; | |
3224 | ||
3225 | wxDataObject *obj = event.GetDataObject(); | |
3226 | if (!obj) | |
3227 | return FALSE; | |
3228 | ||
3229 | m_dragDataObject = obj; | |
3230 | ||
3231 | return TRUE; | |
3232 | } | |
3233 | ||
3234 | gboolean | |
3235 | wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source), | |
3236 | GtkTreePath *WXUNUSED(path)) | |
3237 | { | |
3238 | return FALSE; | |
3239 | } | |
3240 | ||
3241 | gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source), | |
3242 | GtkTreePath *path, GtkSelectionData *selection_data ) | |
3243 | { | |
3244 | GtkTreeIter iter; | |
3245 | if (!get_iter( &iter, path )) return FALSE; | |
3246 | wxDataViewItem item( (void*) iter.user_data ); | |
3247 | ||
3248 | if (!m_dragDataObject->IsSupported( selection_data->target )) | |
3249 | return FALSE; | |
3250 | ||
3251 | size_t size = m_dragDataObject->GetDataSize( selection_data->target ); | |
3252 | if (size == 0) | |
3253 | return FALSE; | |
3254 | ||
3255 | void *buf = malloc( size ); | |
3256 | ||
3257 | gboolean res = FALSE; | |
3258 | if (m_dragDataObject->GetDataHere( selection_data->target, buf )) | |
3259 | { | |
3260 | res = TRUE; | |
3261 | ||
3262 | gtk_selection_data_set( selection_data, selection_data->target, | |
3263 | 8, (const guchar*) buf, size ); | |
3264 | } | |
3265 | ||
3266 | free( buf ); | |
3267 | ||
3268 | return res; | |
3269 | } | |
3270 | ||
3271 | gboolean | |
3272 | wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest), | |
3273 | GtkTreePath *path, | |
3274 | GtkSelectionData *selection_data) | |
3275 | { | |
3276 | GtkTreeIter iter; | |
3277 | if (!get_iter( &iter, path )) return FALSE; | |
3278 | wxDataViewItem item( (void*) iter.user_data ); | |
3279 | ||
3280 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() ); | |
3281 | event.SetEventObject( m_owner ); | |
3282 | event.SetItem( item ); | |
3283 | event.SetModel( m_wx_model ); | |
3284 | event.SetDataFormat( selection_data->target ); | |
3285 | event.SetDataSize( selection_data->length ); | |
3286 | event.SetDataBuffer( selection_data->data ); | |
3287 | if (!m_owner->HandleWindowEvent( event )) | |
3288 | return FALSE; | |
3289 | ||
3290 | if (!event.IsAllowed()) | |
3291 | return FALSE; | |
3292 | ||
3293 | return TRUE; | |
3294 | } | |
3295 | ||
3296 | gboolean | |
3297 | wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest), | |
3298 | GtkTreePath *path, | |
3299 | GtkSelectionData *selection_data) | |
3300 | { | |
3301 | GtkTreeIter iter; | |
3302 | if (!get_iter( &iter, path )) return FALSE; | |
3303 | wxDataViewItem item( (void*) iter.user_data ); | |
3304 | ||
3305 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() ); | |
3306 | event.SetEventObject( m_owner ); | |
3307 | event.SetItem( item ); | |
3308 | event.SetModel( m_wx_model ); | |
3309 | event.SetDataFormat( selection_data->target ); | |
3310 | if (!m_owner->HandleWindowEvent( event )) | |
3311 | return FALSE; | |
3312 | ||
3313 | if (!event.IsAllowed()) | |
3314 | return FALSE; | |
3315 | ||
3316 | return TRUE; | |
3317 | } | |
3318 | ||
3319 | // notifications from wxDataViewModel | |
3320 | ||
3321 | bool wxDataViewCtrlInternal::Cleared() | |
3322 | { | |
3323 | if (m_root) | |
3324 | { | |
3325 | delete m_root; | |
3326 | InitTree(); | |
3327 | } | |
3328 | ||
3329 | // Create new GTK model | |
3330 | g_object_unref( m_gtk_model ); | |
3331 | m_gtk_model = wxgtk_tree_model_new(); | |
3332 | m_gtk_model->internal = this; | |
3333 | ||
3334 | return true; | |
3335 | } | |
3336 | ||
3337 | void wxDataViewCtrlInternal::Resort() | |
3338 | { | |
3339 | if (!m_wx_model->IsVirtualListModel()) | |
3340 | m_root->Resort(); | |
3341 | } | |
3342 | ||
3343 | bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ) | |
3344 | { | |
3345 | if (!m_wx_model->IsVirtualListModel()) | |
3346 | { | |
3347 | wxGtkTreeModelNode *parent_node = FindNode( parent ); | |
3348 | wxASSERT_MSG(parent_node, | |
3349 | "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel"); | |
3350 | ||
3351 | if (m_wx_model->IsContainer( item )) | |
3352 | parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) ); | |
3353 | else | |
3354 | parent_node->AddLeave( item.GetID() ); | |
3355 | } | |
3356 | ||
3357 | return true; | |
3358 | } | |
3359 | ||
3360 | bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) | |
3361 | { | |
3362 | if (!m_wx_model->IsVirtualListModel()) | |
3363 | { | |
3364 | wxGtkTreeModelNode *parent_node = FindNode( parent ); | |
3365 | wxASSERT_MSG(parent_node, | |
3366 | "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel"); | |
3367 | ||
3368 | parent_node->DeleteChild( item.GetID() ); | |
3369 | } | |
3370 | ||
3371 | return true; | |
3372 | } | |
3373 | ||
3374 | bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item ) | |
3375 | { | |
3376 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() ); | |
3377 | event.SetEventObject( m_owner ); | |
3378 | event.SetModel( m_owner->GetModel() ); | |
3379 | event.SetItem( item ); | |
3380 | m_owner->HandleWindowEvent( event ); | |
3381 | ||
3382 | return true; | |
3383 | } | |
3384 | ||
3385 | bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int col ) | |
3386 | { | |
3387 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() ); | |
3388 | event.SetEventObject( m_owner ); | |
3389 | event.SetModel( m_owner->GetModel() ); | |
3390 | event.SetColumn( col ); | |
3391 | event.SetDataViewColumn( GetOwner()->GetColumn(col) ); | |
3392 | event.SetItem( item ); | |
3393 | m_owner->HandleWindowEvent( event ); | |
3394 | ||
3395 | return true; | |
3396 | } | |
3397 | ||
3398 | // GTK+ model iface | |
3399 | ||
3400 | GtkTreeModelFlags wxDataViewCtrlInternal::get_flags() | |
3401 | { | |
3402 | if (m_wx_model->IsVirtualListModel()) | |
3403 | return GTK_TREE_MODEL_LIST_ONLY; | |
3404 | else | |
3405 | return GTK_TREE_MODEL_ITERS_PERSIST; | |
3406 | } | |
3407 | ||
3408 | gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path ) | |
3409 | { | |
3410 | ||
3411 | if (m_wx_model->IsVirtualListModel()) | |
3412 | { | |
3413 | wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model; | |
3414 | ||
3415 | unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0]; | |
3416 | ||
3417 | if (i >= wx_model->GetCount()) | |
3418 | return FALSE; | |
3419 | ||
3420 | iter->stamp = m_gtk_model->stamp; | |
3421 | // user_data is just the index +1 | |
3422 | iter->user_data = (gpointer) (i+1); | |
3423 | ||
3424 | return TRUE; | |
3425 | } | |
3426 | else | |
3427 | { | |
3428 | int depth = gtk_tree_path_get_depth( path ); | |
3429 | ||
3430 | wxGtkTreeModelNode *node = m_root; | |
3431 | ||
3432 | int i; | |
3433 | for (i = 0; i < depth; i++) | |
3434 | { | |
3435 | BuildBranch( node ); | |
3436 | ||
3437 | gint pos = gtk_tree_path_get_indices (path)[i]; | |
3438 | if (pos < 0) return FALSE; | |
3439 | if ((size_t)pos >= node->GetChildCount()) return FALSE; | |
3440 | ||
3441 | void* id = node->GetChildren().Item( (size_t) pos ); | |
3442 | ||
3443 | if (i == depth-1) | |
3444 | { | |
3445 | iter->stamp = m_gtk_model->stamp; | |
3446 | iter->user_data = id; | |
3447 | return TRUE; | |
3448 | } | |
3449 | ||
3450 | size_t count = node->GetNodes().GetCount(); | |
3451 | size_t pos2; | |
3452 | for (pos2 = 0; pos2 < count; pos2++) | |
3453 | { | |
3454 | wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 ); | |
3455 | if (child_node->GetItem().GetID() == id) | |
3456 | { | |
3457 | node = child_node; | |
3458 | break; | |
3459 | } | |
3460 | } | |
3461 | } | |
3462 | } | |
3463 | ||
3464 | return FALSE; | |
3465 | } | |
3466 | ||
3467 | GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter ) | |
3468 | { | |
3469 | GtkTreePath *retval = gtk_tree_path_new (); | |
3470 | ||
3471 | if (m_wx_model->IsVirtualListModel()) | |
3472 | { | |
3473 | // user_data is just the index +1 | |
3474 | int i = ( (wxUIntPtr) iter->user_data ) -1; | |
3475 | gtk_tree_path_append_index (retval, i); | |
3476 | } | |
3477 | else | |
3478 | { | |
3479 | void *id = iter->user_data; | |
3480 | ||
3481 | wxGtkTreeModelNode *node = FindParentNode( iter ); | |
3482 | while (node) | |
3483 | { | |
3484 | int pos = node->GetChildren().Index( id ); | |
3485 | ||
3486 | gtk_tree_path_prepend_index( retval, pos ); | |
3487 | ||
3488 | id = node->GetItem().GetID(); | |
3489 | node = node->GetParent(); | |
3490 | } | |
3491 | } | |
3492 | ||
3493 | return retval; | |
3494 | } | |
3495 | ||
3496 | gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter ) | |
3497 | { | |
3498 | if (m_wx_model->IsVirtualListModel()) | |
3499 | { | |
3500 | wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model; | |
3501 | ||
3502 | // user_data is just the index +1 | |
3503 | int n = ( (wxUIntPtr) iter->user_data ) -1; | |
3504 | ||
3505 | if (n == -1) | |
3506 | return FALSE; | |
3507 | ||
3508 | if (n >= (int) wx_model->GetCount()) | |
3509 | return FALSE; | |
3510 | ||
3511 | // user_data is just the index +1 (+2 because we need the next) | |
3512 | iter->user_data = (gpointer) (n+2); | |
3513 | } | |
3514 | else | |
3515 | { | |
3516 | wxGtkTreeModelNode *parent = FindParentNode( iter ); | |
3517 | if( parent == NULL ) | |
3518 | return FALSE; | |
3519 | ||
3520 | int pos = parent->GetChildren().Index( iter->user_data ); | |
3521 | ||
3522 | if (pos == (int) parent->GetChildCount()-1) | |
3523 | return FALSE; | |
3524 | ||
3525 | iter->stamp = m_gtk_model->stamp; | |
3526 | iter->user_data = parent->GetChildren().Item( pos+1 ); | |
3527 | } | |
3528 | ||
3529 | return TRUE; | |
3530 | } | |
3531 | ||
3532 | gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent ) | |
3533 | { | |
3534 | if (m_wx_model->IsVirtualListModel()) | |
3535 | { | |
3536 | // this is a list, nodes have no children | |
3537 | if (parent) | |
3538 | return FALSE; | |
3539 | ||
3540 | iter->stamp = m_gtk_model->stamp; | |
3541 | iter->user_data = (gpointer) 1; | |
3542 | ||
3543 | return TRUE; | |
3544 | } | |
3545 | else | |
3546 | { | |
3547 | wxDataViewItem item( (void*) parent->user_data ); | |
3548 | ||
3549 | if (!m_wx_model->IsContainer( item )) | |
3550 | return FALSE; | |
3551 | ||
3552 | wxGtkTreeModelNode *parent_node = FindNode( parent ); | |
3553 | wxASSERT_MSG(parent_node, | |
3554 | "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel"); | |
3555 | ||
3556 | BuildBranch( parent_node ); | |
3557 | ||
3558 | if (parent_node->GetChildCount() == 0) | |
3559 | return FALSE; | |
3560 | ||
3561 | iter->stamp = m_gtk_model->stamp; | |
3562 | iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 ); | |
3563 | } | |
3564 | ||
3565 | return TRUE; | |
3566 | } | |
3567 | ||
3568 | gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter ) | |
3569 | { | |
3570 | if (m_wx_model->IsVirtualListModel()) | |
3571 | { | |
3572 | wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model; | |
3573 | ||
3574 | if (iter == NULL) | |
3575 | return (gint) wx_model->GetCount(); | |
3576 | ||
3577 | // this is a list, nodes have no children | |
3578 | return FALSE; | |
3579 | } | |
3580 | else | |
3581 | { | |
3582 | wxDataViewItem item( (void*) iter->user_data ); | |
3583 | ||
3584 | bool is_container = m_wx_model->IsContainer( item ); | |
3585 | ||
3586 | if (!is_container) | |
3587 | return FALSE; | |
3588 | ||
3589 | wxGtkTreeModelNode *node = FindNode( iter ); | |
3590 | wxASSERT_MSG(node, | |
3591 | "Did you forget a call to ItemAdded()? The iterator is unknown to the wxGtkTreeModel"); | |
3592 | ||
3593 | BuildBranch( node ); | |
3594 | ||
3595 | return (node->GetChildCount() > 0); | |
3596 | } | |
3597 | } | |
3598 | ||
3599 | gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter ) | |
3600 | { | |
3601 | if (m_wx_model->IsVirtualListModel()) | |
3602 | { | |
3603 | wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model; | |
3604 | ||
3605 | if (iter == NULL) | |
3606 | return (gint) wx_model->GetCount(); | |
3607 | ||
3608 | return 0; | |
3609 | } | |
3610 | else | |
3611 | { | |
3612 | if (iter == NULL) | |
3613 | return m_root->GetChildCount(); | |
3614 | ||
3615 | wxDataViewItem item( (void*) iter->user_data ); | |
3616 | ||
3617 | if (!m_wx_model->IsContainer( item )) | |
3618 | return 0; | |
3619 | ||
3620 | wxGtkTreeModelNode *parent_node = FindNode( iter ); | |
3621 | wxASSERT_MSG(parent_node, | |
3622 | "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel"); | |
3623 | ||
3624 | BuildBranch( parent_node ); | |
3625 | ||
3626 | // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() ); | |
3627 | ||
3628 | return parent_node->GetChildCount(); | |
3629 | } | |
3630 | } | |
3631 | ||
3632 | gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n ) | |
3633 | { | |
3634 | if (m_wx_model->IsVirtualListModel()) | |
3635 | { | |
3636 | wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model; | |
3637 | ||
3638 | if (parent) | |
3639 | return FALSE; | |
3640 | ||
3641 | if (n < 0) | |
3642 | return FALSE; | |
3643 | ||
3644 | if (n >= (gint) wx_model->GetCount()) | |
3645 | return FALSE; | |
3646 | ||
3647 | iter->stamp = m_gtk_model->stamp; | |
3648 | // user_data is just the index +1 | |
3649 | iter->user_data = (gpointer) (n+1); | |
3650 | ||
3651 | return TRUE; | |
3652 | } | |
3653 | else | |
3654 | { | |
3655 | void* id = NULL; | |
3656 | if (parent) id = (void*) parent->user_data; | |
3657 | wxDataViewItem item( id ); | |
3658 | ||
3659 | if (!m_wx_model->IsContainer( item )) | |
3660 | return FALSE; | |
3661 | ||
3662 | wxGtkTreeModelNode *parent_node = FindNode( parent ); | |
3663 | wxASSERT_MSG(parent_node, | |
3664 | "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel"); | |
3665 | ||
3666 | BuildBranch( parent_node ); | |
3667 | ||
3668 | // wxPrintf( "iter_nth_child %d\n", n ); | |
3669 | ||
3670 | iter->stamp = m_gtk_model->stamp; | |
3671 | iter->user_data = parent_node->GetChildren().Item( n ); | |
3672 | ||
3673 | return TRUE; | |
3674 | } | |
3675 | } | |
3676 | ||
3677 | gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child ) | |
3678 | { | |
3679 | if (m_wx_model->IsVirtualListModel()) | |
3680 | { | |
3681 | return FALSE; | |
3682 | } | |
3683 | else | |
3684 | { | |
3685 | wxGtkTreeModelNode *node = FindParentNode( child ); | |
3686 | if (!node) | |
3687 | return FALSE; | |
3688 | ||
3689 | iter->stamp = m_gtk_model->stamp; | |
3690 | iter->user_data = (gpointer) node->GetItem().GetID(); | |
3691 | ||
3692 | return TRUE; | |
3693 | } | |
3694 | } | |
3695 | ||
3696 | static wxGtkTreeModelNode* | |
3697 | wxDataViewCtrlInternal_FindNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item ) | |
3698 | { | |
3699 | if( model == NULL ) | |
3700 | return NULL; | |
3701 | ||
3702 | ItemList list; | |
3703 | list.DeleteContents( true ); | |
3704 | wxDataViewItem it( item ); | |
3705 | ||
3706 | while( it.IsOk() ) | |
3707 | { | |
3708 | wxDataViewItem * pItem = new wxDataViewItem( it ); | |
3709 | list.Insert( pItem ); | |
3710 | it = model->GetParent( it ); | |
3711 | } | |
3712 | ||
3713 | wxGtkTreeModelNode * node = treeNode; | |
3714 | for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() ) | |
3715 | { | |
3716 | if( node && node->GetNodes().GetCount() != 0 ) | |
3717 | { | |
3718 | int len = node->GetNodes().GetCount(); | |
3719 | wxGtkTreeModelNodes nodes = node->GetNodes(); | |
3720 | int j = 0; | |
3721 | for( ; j < len; j ++) | |
3722 | { | |
3723 | if( nodes[j]->GetItem() == *(n->GetData())) | |
3724 | { | |
3725 | node = nodes[j]; | |
3726 | break; | |
3727 | } | |
3728 | } | |
3729 | ||
3730 | if( j == len ) | |
3731 | { | |
3732 | return NULL; | |
3733 | } | |
3734 | } | |
3735 | else | |
3736 | return NULL; | |
3737 | } | |
3738 | return node; | |
3739 | ||
3740 | } | |
3741 | ||
3742 | wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( GtkTreeIter *iter ) | |
3743 | { | |
3744 | if (!iter) | |
3745 | return m_root; | |
3746 | ||
3747 | wxDataViewItem item( (void*) iter->user_data ); | |
3748 | if (!item.IsOk()) | |
3749 | return m_root; | |
3750 | ||
3751 | wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item ); | |
3752 | ||
3753 | /* | |
3754 | if (!result) | |
3755 | { | |
3756 | wxLogDebug( "Not found %p", iter->user_data ); | |
3757 | char *crash = NULL; | |
3758 | *crash = 0; | |
3759 | } | |
3760 | // TODO: remove this code | |
3761 | */ | |
3762 | ||
3763 | return result; | |
3764 | } | |
3765 | ||
3766 | wxGtkTreeModelNode *wxDataViewCtrlInternal::FindNode( const wxDataViewItem &item ) | |
3767 | { | |
3768 | if (!item.IsOk()) | |
3769 | return m_root; | |
3770 | ||
3771 | wxGtkTreeModelNode *result = wxDataViewCtrlInternal_FindNode( m_wx_model, m_root, item ); | |
3772 | ||
3773 | /* | |
3774 | if (!result) | |
3775 | { | |
3776 | wxLogDebug( "Not found %p", item.GetID() ); | |
3777 | char *crash = NULL; | |
3778 | *crash = 0; | |
3779 | } | |
3780 | // TODO: remove this code | |
3781 | */ | |
3782 | ||
3783 | return result; | |
3784 | } | |
3785 | ||
3786 | static wxGtkTreeModelNode* | |
3787 | wxDataViewCtrlInternal_FindParentNode( wxDataViewModel * model, wxGtkTreeModelNode *treeNode, const wxDataViewItem &item ) | |
3788 | { | |
3789 | if( model == NULL ) | |
3790 | return NULL; | |
3791 | ||
3792 | ItemList list; | |
3793 | list.DeleteContents( true ); | |
3794 | if( !item.IsOk() ) | |
3795 | return NULL; | |
3796 | ||
3797 | wxDataViewItem it( model->GetParent( item ) ); | |
3798 | while( it.IsOk() ) | |
3799 | { | |
3800 | wxDataViewItem * pItem = new wxDataViewItem( it ); | |
3801 | list.Insert( pItem ); | |
3802 | it = model->GetParent( it ); | |
3803 | } | |
3804 | ||
3805 | wxGtkTreeModelNode * node = treeNode; | |
3806 | for( ItemList::compatibility_iterator n = list.GetFirst(); n; n = n->GetNext() ) | |
3807 | { | |
3808 | if( node && node->GetNodes().GetCount() != 0 ) | |
3809 | { | |
3810 | int len = node->GetNodes().GetCount(); | |
3811 | wxGtkTreeModelNodes nodes = node->GetNodes(); | |
3812 | int j = 0; | |
3813 | for( ; j < len; j ++) | |
3814 | { | |
3815 | if( nodes[j]->GetItem() == *(n->GetData())) | |
3816 | { | |
3817 | node = nodes[j]; | |
3818 | break; | |
3819 | } | |
3820 | } | |
3821 | ||
3822 | if( j == len ) | |
3823 | { | |
3824 | return NULL; | |
3825 | } | |
3826 | } | |
3827 | else | |
3828 | return NULL; | |
3829 | } | |
3830 | //Examine whether the node is item's parent node | |
3831 | int len = node->GetChildCount(); | |
3832 | for( int i = 0; i < len ; i ++ ) | |
3833 | { | |
3834 | if( node->GetChildren().Item( i ) == item.GetID() ) | |
3835 | return node; | |
3836 | } | |
3837 | return NULL; | |
3838 | } | |
3839 | ||
3840 | wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( GtkTreeIter *iter ) | |
3841 | { | |
3842 | if (!iter) | |
3843 | return NULL; | |
3844 | ||
3845 | wxDataViewItem item( (void*) iter->user_data ); | |
3846 | if (!item.IsOk()) | |
3847 | return NULL; | |
3848 | ||
3849 | return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item ); | |
3850 | } | |
3851 | ||
3852 | wxGtkTreeModelNode *wxDataViewCtrlInternal::FindParentNode( const wxDataViewItem &item ) | |
3853 | { | |
3854 | if (!item.IsOk()) | |
3855 | return NULL; | |
3856 | ||
3857 | return wxDataViewCtrlInternal_FindParentNode( m_wx_model, m_root, item ); | |
3858 | } | |
3859 | ||
3860 | //----------------------------------------------------------------------------- | |
3861 | // wxDataViewCtrl signal callbacks | |
3862 | //----------------------------------------------------------------------------- | |
3863 | ||
3864 | static void | |
3865 | wxdataview_selection_changed_callback( GtkTreeSelection* WXUNUSED(selection), wxDataViewCtrl *dv ) | |
3866 | { | |
3867 | if (!GTK_WIDGET_REALIZED(dv->m_widget)) | |
3868 | return; | |
3869 | ||
3870 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, dv->GetId() ); | |
3871 | event.SetItem( dv->GetSelection() ); | |
3872 | event.SetModel( dv->GetModel() ); | |
3873 | dv->HandleWindowEvent( event ); | |
3874 | } | |
3875 | ||
3876 | static void | |
3877 | wxdataview_row_activated_callback( GtkTreeView* WXUNUSED(treeview), GtkTreePath *path, | |
3878 | GtkTreeViewColumn *WXUNUSED(column), wxDataViewCtrl *dv ) | |
3879 | { | |
3880 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, dv->GetId() ); | |
3881 | ||
3882 | GtkTreeIter iter; | |
3883 | dv->GtkGetInternal()->get_iter( &iter, path ); | |
3884 | wxDataViewItem item( (void*) iter.user_data );; | |
3885 | event.SetItem( item ); | |
3886 | event.SetModel( dv->GetModel() ); | |
3887 | dv->HandleWindowEvent( event ); | |
3888 | } | |
3889 | ||
3890 | static gboolean | |
3891 | wxdataview_test_expand_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter, | |
3892 | GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv ) | |
3893 | { | |
3894 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, dv->GetId() ); | |
3895 | ||
3896 | wxDataViewItem item( (void*) iter->user_data );; | |
3897 | event.SetItem( item ); | |
3898 | event.SetModel( dv->GetModel() ); | |
3899 | dv->HandleWindowEvent( event ); | |
3900 | ||
3901 | return !event.IsAllowed(); | |
3902 | } | |
3903 | ||
3904 | static void | |
3905 | wxdataview_row_expanded_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter, | |
3906 | GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv ) | |
3907 | { | |
3908 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, dv->GetId() ); | |
3909 | ||
3910 | wxDataViewItem item( (void*) iter->user_data );; | |
3911 | event.SetItem( item ); | |
3912 | event.SetModel( dv->GetModel() ); | |
3913 | dv->HandleWindowEvent( event ); | |
3914 | } | |
3915 | ||
3916 | static gboolean | |
3917 | wxdataview_test_collapse_row_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter, | |
3918 | GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv ) | |
3919 | { | |
3920 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, dv->GetId() ); | |
3921 | ||
3922 | wxDataViewItem item( (void*) iter->user_data );; | |
3923 | event.SetItem( item ); | |
3924 | event.SetModel( dv->GetModel() ); | |
3925 | dv->HandleWindowEvent( event ); | |
3926 | ||
3927 | return !event.IsAllowed(); | |
3928 | } | |
3929 | ||
3930 | static void | |
3931 | wxdataview_row_collapsed_callback( GtkTreeView* WXUNUSED(treeview), GtkTreeIter* iter, | |
3932 | GtkTreePath *WXUNUSED(path), wxDataViewCtrl *dv ) | |
3933 | { | |
3934 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, dv->GetId() ); | |
3935 | ||
3936 | wxDataViewItem item( (void*) iter->user_data );; | |
3937 | event.SetItem( item ); | |
3938 | event.SetModel( dv->GetModel() ); | |
3939 | dv->HandleWindowEvent( event ); | |
3940 | } | |
3941 | ||
3942 | //----------------------------------------------------------------------------- | |
3943 | // wxDataViewCtrl | |
3944 | //----------------------------------------------------------------------------- | |
3945 | ||
3946 | void wxDataViewCtrl::AddChildGTK(wxWindowGTK* child) | |
3947 | { | |
3948 | GtkWidget* treeview = GtkGetTreeView(); | |
3949 | ||
3950 | // Insert widget in GtkTreeView | |
3951 | if (GTK_WIDGET_REALIZED(treeview)) | |
3952 | gtk_widget_set_parent_window( child->m_widget, | |
3953 | gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) ); | |
3954 | gtk_widget_set_parent( child->m_widget, treeview ); | |
3955 | } | |
3956 | ||
3957 | static | |
3958 | void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget), | |
3959 | GtkAllocation *WXUNUSED(gtk_alloc), | |
3960 | wxDataViewCtrl *win ) | |
3961 | { | |
3962 | wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); | |
3963 | while (node) | |
3964 | { | |
3965 | wxWindow *child = node->GetData(); | |
3966 | ||
3967 | GtkRequisition req; | |
3968 | gtk_widget_size_request( child->m_widget, &req ); | |
3969 | ||
3970 | GtkAllocation alloc; | |
3971 | alloc.x = child->m_x; | |
3972 | alloc.y = child->m_y; | |
3973 | alloc.width = child->m_width; | |
3974 | alloc.height = child->m_height; | |
3975 | gtk_widget_size_allocate( child->m_widget, &alloc ); | |
3976 | ||
3977 | node = node->GetNext(); | |
3978 | } | |
3979 | } | |
3980 | ||
3981 | ||
3982 | //----------------------------------------------------------------------------- | |
3983 | // "motion_notify_event" | |
3984 | //----------------------------------------------------------------------------- | |
3985 | ||
3986 | static gboolean | |
3987 | gtk_dataview_motion_notify_callback( GtkWidget *WXUNUSED(widget), | |
3988 | GdkEventMotion *gdk_event, | |
3989 | wxDataViewCtrl *dv ) | |
3990 | { | |
3991 | if (gdk_event->is_hint) | |
3992 | { | |
3993 | int x = 0; | |
3994 | int y = 0; | |
3995 | GdkModifierType state; | |
3996 | gdk_window_get_pointer(gdk_event->window, &x, &y, &state); | |
3997 | gdk_event->x = x; | |
3998 | gdk_event->y = y; | |
3999 | } | |
4000 | ||
4001 | GtkTreePath *path = NULL; | |
4002 | GtkTreeViewColumn *column = NULL; | |
4003 | gint cell_x = 0; | |
4004 | gint cell_y = 0; | |
4005 | if (gtk_tree_view_get_path_at_pos( | |
4006 | GTK_TREE_VIEW(dv->GtkGetTreeView()), | |
4007 | (int) gdk_event->x, (int) gdk_event->y, | |
4008 | &path, | |
4009 | &column, | |
4010 | &cell_x, | |
4011 | &cell_y)) | |
4012 | { | |
4013 | if (path) | |
4014 | { | |
4015 | GtkTreeIter iter; | |
4016 | dv->GtkGetInternal()->get_iter( &iter, path ); | |
4017 | ||
4018 | // wxPrintf( "mouse %d %d\n", (int) gdk_event->x, (int) gdk_event->y ); | |
4019 | ||
4020 | gtk_tree_path_free( path ); | |
4021 | } | |
4022 | } | |
4023 | ||
4024 | ||
4025 | return FALSE; | |
4026 | } | |
4027 | ||
4028 | //----------------------------------------------------------------------------- | |
4029 | // "button_press_event" | |
4030 | //----------------------------------------------------------------------------- | |
4031 | ||
4032 | static gboolean | |
4033 | gtk_dataview_button_press_callback( GtkWidget *WXUNUSED(widget), | |
4034 | GdkEventButton *gdk_event, | |
4035 | wxDataViewCtrl *dv ) | |
4036 | { | |
4037 | if ((gdk_event->button == 3) && (gdk_event->type == GDK_BUTTON_PRESS)) | |
4038 | { | |
4039 | GtkTreePath *path = NULL; | |
4040 | GtkTreeViewColumn *column = NULL; | |
4041 | gint cell_x = 0; | |
4042 | gint cell_y = 0; | |
4043 | if (gtk_tree_view_get_path_at_pos( | |
4044 | GTK_TREE_VIEW(dv->GtkGetTreeView()), | |
4045 | (int) gdk_event->x, (int) gdk_event->y, | |
4046 | &path, | |
4047 | &column, | |
4048 | &cell_x, | |
4049 | &cell_y)) | |
4050 | { | |
4051 | if (path) | |
4052 | { | |
4053 | GtkTreeIter iter; | |
4054 | dv->GtkGetInternal()->get_iter( &iter, path ); | |
4055 | ||
4056 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, dv->GetId() ); | |
4057 | wxDataViewItem item( (void*) iter.user_data );; | |
4058 | event.SetItem( item ); | |
4059 | event.SetModel( dv->GetModel() ); | |
4060 | bool ret = dv->HandleWindowEvent( event ); | |
4061 | gtk_tree_path_free( path ); | |
4062 | return ret; | |
4063 | } | |
4064 | } | |
4065 | } | |
4066 | ||
4067 | return FALSE; | |
4068 | } | |
4069 | ||
4070 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase) | |
4071 | ||
4072 | wxDataViewCtrl::~wxDataViewCtrl() | |
4073 | { | |
4074 | if (m_notifier) | |
4075 | GetModel()->RemoveNotifier( m_notifier ); | |
4076 | ||
4077 | m_cols.Clear(); | |
4078 | ||
4079 | // remove the model from the GtkTreeView before it gets destroyed by the | |
4080 | // wxDataViewCtrlBase's dtor | |
4081 | gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL ); | |
4082 | ||
4083 | delete m_internal; | |
4084 | } | |
4085 | ||
4086 | void wxDataViewCtrl::Init() | |
4087 | { | |
4088 | m_notifier = NULL; | |
4089 | m_internal = NULL; | |
4090 | ||
4091 | m_cols.DeleteContents( true ); | |
4092 | } | |
4093 | ||
4094 | bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id, | |
4095 | const wxPoint& pos, const wxSize& size, | |
4096 | long style, const wxValidator& validator ) | |
4097 | { | |
4098 | Init(); | |
4099 | ||
4100 | if (!PreCreation( parent, pos, size ) || | |
4101 | !CreateBase( parent, id, pos, size, style, validator )) | |
4102 | { | |
4103 | wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") ); | |
4104 | return false; | |
4105 | } | |
4106 | ||
4107 | m_widget = gtk_scrolled_window_new (NULL, NULL); | |
4108 | g_object_ref(m_widget); | |
4109 | ||
4110 | GTKScrolledWindowSetBorder(m_widget, style); | |
4111 | ||
4112 | m_treeview = gtk_tree_view_new(); | |
4113 | gtk_container_add (GTK_CONTAINER (m_widget), m_treeview); | |
4114 | ||
4115 | g_signal_connect (m_treeview, "size_allocate", | |
4116 | G_CALLBACK (gtk_dataviewctrl_size_callback), this); | |
4117 | ||
4118 | #ifdef __WXGTK26__ | |
4119 | if (!gtk_check_version(2,6,0)) | |
4120 | { | |
4121 | bool fixed = (style & wxDV_VARIABLE_LINE_HEIGHT) == 0; | |
4122 | gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed ); | |
4123 | } | |
4124 | #endif | |
4125 | ||
4126 | if (style & wxDV_MULTIPLE) | |
4127 | { | |
4128 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4129 | gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE ); | |
4130 | } | |
4131 | ||
4132 | gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 ); | |
4133 | ||
4134 | #ifdef __WXGTK210__ | |
4135 | if (!gtk_check_version(2,10,0)) | |
4136 | { | |
4137 | GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE; | |
4138 | ||
4139 | if ((style & wxDV_HORIZ_RULES) != 0 && | |
4140 | (style & wxDV_VERT_RULES) != 0) | |
4141 | grid = GTK_TREE_VIEW_GRID_LINES_BOTH; | |
4142 | else if (style & wxDV_VERT_RULES) | |
4143 | grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL; | |
4144 | else if (style & wxDV_HORIZ_RULES) | |
4145 | grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL; | |
4146 | ||
4147 | if (grid != GTK_TREE_VIEW_GRID_LINES_NONE) | |
4148 | gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid ); | |
4149 | } | |
4150 | #endif | |
4151 | ||
4152 | gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_ROW_LINES) != 0 ); | |
4153 | ||
4154 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget), | |
4155 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); | |
4156 | gtk_widget_show (m_treeview); | |
4157 | ||
4158 | m_parent->DoAddChild( this ); | |
4159 | ||
4160 | PostCreation(size); | |
4161 | ||
4162 | GtkEnableSelectionEvents(); | |
4163 | ||
4164 | g_signal_connect_after (m_treeview, "row-activated", | |
4165 | G_CALLBACK (wxdataview_row_activated_callback), this); | |
4166 | ||
4167 | g_signal_connect (m_treeview, "test-collapse-row", | |
4168 | G_CALLBACK (wxdataview_test_collapse_row_callback), this); | |
4169 | ||
4170 | g_signal_connect_after (m_treeview, "row-collapsed", | |
4171 | G_CALLBACK (wxdataview_row_collapsed_callback), this); | |
4172 | ||
4173 | g_signal_connect (m_treeview, "test-expand-row", | |
4174 | G_CALLBACK (wxdataview_test_expand_row_callback), this); | |
4175 | ||
4176 | g_signal_connect_after (m_treeview, "row-expanded", | |
4177 | G_CALLBACK (wxdataview_row_expanded_callback), this); | |
4178 | ||
4179 | g_signal_connect (m_treeview, "motion_notify_event", | |
4180 | G_CALLBACK (gtk_dataview_motion_notify_callback), this); | |
4181 | ||
4182 | g_signal_connect (m_treeview, "button_press_event", | |
4183 | G_CALLBACK (gtk_dataview_button_press_callback), this); | |
4184 | ||
4185 | return true; | |
4186 | } | |
4187 | ||
4188 | void wxDataViewCtrl::OnInternalIdle() | |
4189 | { | |
4190 | wxWindow::OnInternalIdle(); | |
4191 | ||
4192 | unsigned int cols = GetColumnCount(); | |
4193 | unsigned int i; | |
4194 | for (i = 0; i < cols; i++) | |
4195 | { | |
4196 | wxDataViewColumn *col = GetColumn( i ); | |
4197 | col->OnInternalIdle(); | |
4198 | } | |
4199 | } | |
4200 | ||
4201 | bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model ) | |
4202 | { | |
4203 | if (GetModel()) | |
4204 | { | |
4205 | delete m_internal; | |
4206 | m_internal = NULL; | |
4207 | ||
4208 | delete m_notifier; | |
4209 | m_notifier = NULL; | |
4210 | } | |
4211 | ||
4212 | if (!wxDataViewCtrlBase::AssociateModel( model )) | |
4213 | return false; | |
4214 | ||
4215 | #ifdef __WXGTK26__ | |
4216 | if (!gtk_check_version(2,6,0)) | |
4217 | { | |
4218 | bool fixed = (((GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) == 0) || (model->IsVirtualListModel())); | |
4219 | gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), fixed ); | |
4220 | } | |
4221 | #endif | |
4222 | ||
4223 | GtkWxTreeModel *gtk_model = wxgtk_tree_model_new(); | |
4224 | m_internal = new wxDataViewCtrlInternal( this, model, gtk_model ); | |
4225 | gtk_model->internal = m_internal; | |
4226 | ||
4227 | m_notifier = new wxGtkDataViewModelNotifier( gtk_model, model, this ); | |
4228 | ||
4229 | model->AddNotifier( m_notifier ); | |
4230 | ||
4231 | gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_model) ); | |
4232 | ||
4233 | // unref in wxDataViewCtrlInternal | |
4234 | // g_object_unref( gtk_model ); | |
4235 | ||
4236 | return true; | |
4237 | } | |
4238 | ||
4239 | bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format ) | |
4240 | { | |
4241 | return m_internal->EnableDragSource( format ); | |
4242 | } | |
4243 | ||
4244 | bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format ) | |
4245 | { | |
4246 | return m_internal->EnableDropTarget( format ); | |
4247 | } | |
4248 | ||
4249 | bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col ) | |
4250 | { | |
4251 | if (!wxDataViewCtrlBase::AppendColumn(col)) | |
4252 | return false; | |
4253 | ||
4254 | m_cols.Append( col ); | |
4255 | ||
4256 | #ifdef __WXGTK26__ | |
4257 | if (!gtk_check_version(2,6,0)) | |
4258 | { | |
4259 | if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) != | |
4260 | GTK_TREE_VIEW_COLUMN_FIXED) | |
4261 | gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE ); | |
4262 | } | |
4263 | #endif | |
4264 | ||
4265 | gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), | |
4266 | GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ); | |
4267 | ||
4268 | return true; | |
4269 | } | |
4270 | ||
4271 | bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col ) | |
4272 | { | |
4273 | if (!wxDataViewCtrlBase::PrependColumn(col)) | |
4274 | return false; | |
4275 | ||
4276 | m_cols.Insert( col ); | |
4277 | ||
4278 | #ifdef __WXGTK26__ | |
4279 | if (!gtk_check_version(2,6,0)) | |
4280 | { | |
4281 | if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) != | |
4282 | GTK_TREE_VIEW_COLUMN_FIXED) | |
4283 | gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE ); | |
4284 | } | |
4285 | #endif | |
4286 | ||
4287 | gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview), | |
4288 | GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), 0 ); | |
4289 | ||
4290 | return true; | |
4291 | } | |
4292 | ||
4293 | bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col ) | |
4294 | { | |
4295 | if (!wxDataViewCtrlBase::InsertColumn(pos,col)) | |
4296 | return false; | |
4297 | ||
4298 | m_cols.Insert( pos, col ); | |
4299 | ||
4300 | #ifdef __WXGTK26__ | |
4301 | if (!gtk_check_version(2,6,0)) | |
4302 | { | |
4303 | if (gtk_tree_view_column_get_sizing( GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ) != | |
4304 | GTK_TREE_VIEW_COLUMN_FIXED) | |
4305 | gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), FALSE ); | |
4306 | } | |
4307 | #endif | |
4308 | ||
4309 | gtk_tree_view_insert_column( GTK_TREE_VIEW(m_treeview), | |
4310 | GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()), pos ); | |
4311 | ||
4312 | return true; | |
4313 | } | |
4314 | ||
4315 | unsigned int wxDataViewCtrl::GetColumnCount() const | |
4316 | { | |
4317 | return m_cols.GetCount(); | |
4318 | } | |
4319 | ||
4320 | wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const | |
4321 | { | |
4322 | GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos ); | |
4323 | if (!gtk_col) | |
4324 | return NULL; | |
4325 | ||
4326 | wxDataViewColumnList::const_iterator iter; | |
4327 | for (iter = m_cols.begin(); iter != m_cols.end(); ++iter) | |
4328 | { | |
4329 | wxDataViewColumn *col = *iter; | |
4330 | if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col) | |
4331 | { | |
4332 | return col; | |
4333 | } | |
4334 | } | |
4335 | ||
4336 | return NULL; | |
4337 | } | |
4338 | ||
4339 | bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column ) | |
4340 | { | |
4341 | gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview), | |
4342 | GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) ); | |
4343 | ||
4344 | m_cols.DeleteObject( column ); | |
4345 | ||
4346 | return true; | |
4347 | } | |
4348 | ||
4349 | bool wxDataViewCtrl::ClearColumns() | |
4350 | { | |
4351 | wxDataViewColumnList::iterator iter; | |
4352 | for (iter = m_cols.begin(); iter != m_cols.end(); ++iter) | |
4353 | { | |
4354 | wxDataViewColumn *col = *iter; | |
4355 | gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview), | |
4356 | GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) ); | |
4357 | } | |
4358 | ||
4359 | m_cols.Clear(); | |
4360 | ||
4361 | return true; | |
4362 | } | |
4363 | ||
4364 | int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const | |
4365 | { | |
4366 | GtkTreeViewColumn *gtk_column = GTK_TREE_VIEW_COLUMN(column->GetConstGtkHandle()); | |
4367 | ||
4368 | GList *list = gtk_tree_view_get_columns( GTK_TREE_VIEW(m_treeview) ); | |
4369 | ||
4370 | gint pos = g_list_index( list, (gconstpointer) gtk_column ); | |
4371 | ||
4372 | g_list_free( list ); | |
4373 | ||
4374 | return pos; | |
4375 | } | |
4376 | ||
4377 | wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const | |
4378 | { | |
4379 | return m_internal->GetDataViewSortColumn(); | |
4380 | } | |
4381 | ||
4382 | void wxDataViewCtrl::Expand( const wxDataViewItem & item ) | |
4383 | { | |
4384 | GtkTreeIter iter; | |
4385 | iter.user_data = item.GetID(); | |
4386 | GtkTreePath *path = m_internal->get_path( &iter ); | |
4387 | gtk_tree_view_expand_row( GTK_TREE_VIEW(m_treeview), path, false ); | |
4388 | gtk_tree_path_free( path ); | |
4389 | } | |
4390 | ||
4391 | void wxDataViewCtrl::Collapse( const wxDataViewItem & item ) | |
4392 | { | |
4393 | GtkTreeIter iter; | |
4394 | iter.user_data = item.GetID(); | |
4395 | GtkTreePath *path = m_internal->get_path( &iter ); | |
4396 | gtk_tree_view_collapse_row( GTK_TREE_VIEW(m_treeview), path ); | |
4397 | gtk_tree_path_free( path ); | |
4398 | } | |
4399 | ||
4400 | bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const | |
4401 | { | |
4402 | GtkTreeIter iter; | |
4403 | iter.user_data = item.GetID(); | |
4404 | GtkTreePath *path = m_internal->get_path( &iter ); | |
4405 | bool res = gtk_tree_view_row_expanded( GTK_TREE_VIEW(m_treeview), path ); | |
4406 | gtk_tree_path_free( path ); | |
4407 | ||
4408 | return res; | |
4409 | } | |
4410 | ||
4411 | wxDataViewItem wxDataViewCtrl::GetSelection() const | |
4412 | { | |
4413 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4414 | ||
4415 | if (m_windowStyle & wxDV_MULTIPLE) | |
4416 | { | |
4417 | // Report the first one | |
4418 | GtkTreeModel *model; | |
4419 | GList *list = gtk_tree_selection_get_selected_rows( selection, &model ); | |
4420 | ||
4421 | if (list) | |
4422 | { | |
4423 | GtkTreePath *path = (GtkTreePath*) list->data; | |
4424 | GtkTreeIter iter; | |
4425 | m_internal->get_iter( &iter, path ); | |
4426 | ||
4427 | // delete list | |
4428 | g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL ); | |
4429 | g_list_free( list ); | |
4430 | ||
4431 | return wxDataViewItem( (void*) iter.user_data ); | |
4432 | } | |
4433 | } | |
4434 | else | |
4435 | { | |
4436 | GtkTreeIter iter; | |
4437 | if (gtk_tree_selection_get_selected( selection, NULL, &iter )) | |
4438 | { | |
4439 | wxDataViewItem item( (void*) iter.user_data ); | |
4440 | return item; | |
4441 | } | |
4442 | } | |
4443 | ||
4444 | return wxDataViewItem(0); | |
4445 | } | |
4446 | ||
4447 | int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const | |
4448 | { | |
4449 | sel.Clear(); | |
4450 | ||
4451 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4452 | if (HasFlag(wxDV_MULTIPLE)) | |
4453 | { | |
4454 | GtkTreeModel *model; | |
4455 | GList *list = gtk_tree_selection_get_selected_rows( selection, &model ); | |
4456 | ||
4457 | int count = 0; | |
4458 | while (list) | |
4459 | { | |
4460 | GtkTreePath *path = (GtkTreePath*) list->data; | |
4461 | ||
4462 | GtkTreeIter iter; | |
4463 | m_internal->get_iter( &iter, path ); | |
4464 | ||
4465 | sel.Add( wxDataViewItem( (void*) iter.user_data ) ); | |
4466 | ||
4467 | list = g_list_next( list ); | |
4468 | count++; | |
4469 | } | |
4470 | ||
4471 | // delete list | |
4472 | g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL ); | |
4473 | g_list_free( list ); | |
4474 | ||
4475 | return count; | |
4476 | } | |
4477 | else | |
4478 | { | |
4479 | GtkTreeModel *model; | |
4480 | GtkTreeIter iter; | |
4481 | gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter ); | |
4482 | if (has_selection) | |
4483 | { | |
4484 | sel.Add( wxDataViewItem( (void*) iter.user_data) ); | |
4485 | return 1; | |
4486 | } | |
4487 | } | |
4488 | ||
4489 | return 0; | |
4490 | } | |
4491 | ||
4492 | void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel ) | |
4493 | { | |
4494 | GtkDisableSelectionEvents(); | |
4495 | ||
4496 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4497 | ||
4498 | gtk_tree_selection_unselect_all( selection ); | |
4499 | ||
4500 | wxDataViewItem last_parent; | |
4501 | ||
4502 | size_t i; | |
4503 | for (i = 0; i < sel.GetCount(); i++) | |
4504 | { | |
4505 | wxDataViewItem item = sel[i]; | |
4506 | wxDataViewItem parent = GetModel()->GetParent( item ); | |
4507 | if (parent) | |
4508 | { | |
4509 | if (parent != last_parent) | |
4510 | ExpandAncestors(item); | |
4511 | } | |
4512 | last_parent = parent; | |
4513 | ||
4514 | GtkTreeIter iter; | |
4515 | iter.stamp = m_internal->GetGtkModel()->stamp; | |
4516 | iter.user_data = (gpointer) item.GetID(); | |
4517 | gtk_tree_selection_select_iter( selection, &iter ); | |
4518 | } | |
4519 | ||
4520 | GtkEnableSelectionEvents(); | |
4521 | } | |
4522 | ||
4523 | void wxDataViewCtrl::Select( const wxDataViewItem & item ) | |
4524 | { | |
4525 | ExpandAncestors(item); | |
4526 | ||
4527 | GtkDisableSelectionEvents(); | |
4528 | ||
4529 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4530 | ||
4531 | GtkTreeIter iter; | |
4532 | iter.stamp = m_internal->GetGtkModel()->stamp; | |
4533 | iter.user_data = (gpointer) item.GetID(); | |
4534 | gtk_tree_selection_select_iter( selection, &iter ); | |
4535 | ||
4536 | GtkEnableSelectionEvents(); | |
4537 | } | |
4538 | ||
4539 | void wxDataViewCtrl::Unselect( const wxDataViewItem & item ) | |
4540 | { | |
4541 | GtkDisableSelectionEvents(); | |
4542 | ||
4543 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4544 | ||
4545 | GtkTreeIter iter; | |
4546 | iter.stamp = m_internal->GetGtkModel()->stamp; | |
4547 | iter.user_data = (gpointer) item.GetID(); | |
4548 | gtk_tree_selection_unselect_iter( selection, &iter ); | |
4549 | ||
4550 | GtkEnableSelectionEvents(); | |
4551 | } | |
4552 | ||
4553 | bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const | |
4554 | { | |
4555 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4556 | ||
4557 | GtkTreeIter iter; | |
4558 | iter.stamp = m_internal->GetGtkModel()->stamp; | |
4559 | iter.user_data = (gpointer) item.GetID(); | |
4560 | ||
4561 | return gtk_tree_selection_iter_is_selected( selection, &iter ); | |
4562 | } | |
4563 | ||
4564 | void wxDataViewCtrl::SelectAll() | |
4565 | { | |
4566 | GtkDisableSelectionEvents(); | |
4567 | ||
4568 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4569 | ||
4570 | gtk_tree_selection_select_all( selection ); | |
4571 | ||
4572 | GtkEnableSelectionEvents(); | |
4573 | } | |
4574 | ||
4575 | void wxDataViewCtrl::UnselectAll() | |
4576 | { | |
4577 | GtkDisableSelectionEvents(); | |
4578 | ||
4579 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4580 | ||
4581 | gtk_tree_selection_unselect_all( selection ); | |
4582 | ||
4583 | GtkEnableSelectionEvents(); | |
4584 | } | |
4585 | ||
4586 | void wxDataViewCtrl::EnsureVisible(const wxDataViewItem& item, | |
4587 | const wxDataViewColumn *WXUNUSED(column)) | |
4588 | { | |
4589 | ExpandAncestors(item); | |
4590 | ||
4591 | GtkTreeIter iter; | |
4592 | iter.user_data = (gpointer) item.GetID(); | |
4593 | GtkTreePath *path = m_internal->get_path( &iter ); | |
4594 | gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW(m_treeview), path, NULL, false, 0.0, 0.0 ); | |
4595 | gtk_tree_path_free( path ); | |
4596 | } | |
4597 | ||
4598 | void wxDataViewCtrl::HitTest(const wxPoint& WXUNUSED(point), | |
4599 | wxDataViewItem& item, | |
4600 | wxDataViewColumn *& column) const | |
4601 | { | |
4602 | item = wxDataViewItem(0); | |
4603 | column = NULL; | |
4604 | } | |
4605 | ||
4606 | wxRect | |
4607 | wxDataViewCtrl::GetItemRect(const wxDataViewItem& WXUNUSED(item), | |
4608 | const wxDataViewColumn *WXUNUSED(column)) const | |
4609 | { | |
4610 | return wxRect(); | |
4611 | } | |
4612 | ||
4613 | void wxDataViewCtrl::DoSetExpanderColumn() | |
4614 | { | |
4615 | gtk_tree_view_set_expander_column( GTK_TREE_VIEW(m_treeview), | |
4616 | GTK_TREE_VIEW_COLUMN( GetExpanderColumn()->GetGtkHandle() ) ); | |
4617 | } | |
4618 | ||
4619 | void wxDataViewCtrl::DoSetIndent() | |
4620 | { | |
4621 | } | |
4622 | ||
4623 | void wxDataViewCtrl::GtkDisableSelectionEvents() | |
4624 | { | |
4625 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4626 | g_signal_handlers_disconnect_by_func( selection, | |
4627 | (gpointer) (wxdataview_selection_changed_callback), this); | |
4628 | } | |
4629 | ||
4630 | void wxDataViewCtrl::GtkEnableSelectionEvents() | |
4631 | { | |
4632 | GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) ); | |
4633 | g_signal_connect_after (selection, "changed", | |
4634 | G_CALLBACK (wxdataview_selection_changed_callback), this); | |
4635 | } | |
4636 | ||
4637 | // static | |
4638 | wxVisualAttributes | |
4639 | wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
4640 | { | |
4641 | return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new); | |
4642 | } | |
4643 | ||
4644 | ||
4645 | #endif | |
4646 | // !wxUSE_GENERICDATAVIEWCTRL | |
4647 | ||
4648 | #endif | |
4649 | // wxUSE_DATAVIEWCTRL |