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