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