]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/datavgen.cpp | |
3 | // Purpose: wxDataViewCtrl generic implementation | |
4 | // Author: Robert Roebling | |
5 | // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang | |
6 | // Id: $Id$ | |
7 | // Copyright: (c) 1998 Robert Roebling | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #if wxUSE_DATAVIEWCTRL | |
19 | ||
20 | #include "wx/dataview.h" | |
21 | ||
22 | #ifdef wxUSE_GENERICDATAVIEWCTRL | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #ifdef __WXMSW__ | |
26 | #include "wx/msw/private.h" | |
27 | #include "wx/msw/wrapwin.h" | |
28 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" | |
29 | #endif | |
30 | #include "wx/sizer.h" | |
31 | #include "wx/log.h" | |
32 | #include "wx/dcclient.h" | |
33 | #include "wx/timer.h" | |
34 | #include "wx/settings.h" | |
35 | #include "wx/msgdlg.h" | |
36 | #include "wx/dcscreen.h" | |
37 | #include "wx/frame.h" | |
38 | #endif | |
39 | ||
40 | #include "wx/stockitem.h" | |
41 | #include "wx/calctrl.h" | |
42 | #include "wx/popupwin.h" | |
43 | #include "wx/renderer.h" | |
44 | #include "wx/dcbuffer.h" | |
45 | #include "wx/icon.h" | |
46 | #include "wx/list.h" | |
47 | #include "wx/listimpl.cpp" | |
48 | #include "wx/imaglist.h" | |
49 | #include "wx/headerctrl.h" | |
50 | #include "wx/dnd.h" | |
51 | ||
52 | //----------------------------------------------------------------------------- | |
53 | // classes | |
54 | //----------------------------------------------------------------------------- | |
55 | ||
56 | class wxDataViewCtrl; | |
57 | ||
58 | static const int SCROLL_UNIT_X = 15; | |
59 | ||
60 | // the cell padding on the left/right | |
61 | static const int PADDING_RIGHTLEFT = 3; | |
62 | ||
63 | // the expander space margin | |
64 | static const int EXPANDER_MARGIN = 4; | |
65 | ||
66 | #ifdef __WXMSW__ | |
67 | static const int EXPANDER_OFFSET = 4; | |
68 | #else | |
69 | static const int EXPANDER_OFFSET = 1; | |
70 | #endif | |
71 | ||
72 | // Below is the compare stuff. | |
73 | // For the generic implementation, both the leaf nodes and the nodes are sorted for | |
74 | // fast search when needed | |
75 | static wxDataViewModel* g_model; | |
76 | static int g_column = -2; | |
77 | static bool g_asending = true; | |
78 | ||
79 | //----------------------------------------------------------------------------- | |
80 | // wxDataViewHeaderWindow | |
81 | //----------------------------------------------------------------------------- | |
82 | ||
83 | class wxDataViewHeaderWindow : public wxHeaderCtrl | |
84 | { | |
85 | public: | |
86 | wxDataViewHeaderWindow(wxDataViewCtrl *parent) | |
87 | : wxHeaderCtrl(parent) | |
88 | { | |
89 | } | |
90 | ||
91 | wxDataViewCtrl *GetOwner() const | |
92 | { return static_cast<wxDataViewCtrl *>(GetParent()); } | |
93 | ||
94 | protected: | |
95 | // implement/override wxHeaderCtrl functions by forwarding them to the main | |
96 | // control | |
97 | virtual const wxHeaderColumn& GetColumn(unsigned int idx) const | |
98 | { | |
99 | return *(GetOwner()->GetColumn(idx)); | |
100 | } | |
101 | ||
102 | // FIXME: currently unused | |
103 | virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle) | |
104 | { | |
105 | wxDataViewCtrl * const owner = GetOwner(); | |
106 | ||
107 | int widthContents = owner->GetBestColumnWidth(idx); | |
108 | owner->GetColumn(idx)->SetWidth(wxMax(widthTitle, widthContents)); | |
109 | owner->OnColumnChange(idx); | |
110 | ||
111 | return true; | |
112 | } | |
113 | ||
114 | private: | |
115 | bool SendEvent(wxEventType type, unsigned int n) | |
116 | { | |
117 | wxDataViewCtrl * const owner = GetOwner(); | |
118 | wxDataViewEvent event(type, owner->GetId()); | |
119 | ||
120 | event.SetEventObject(owner); | |
121 | event.SetColumn(n); | |
122 | event.SetDataViewColumn(owner->GetColumn(n)); | |
123 | event.SetModel(owner->GetModel()); | |
124 | ||
125 | // for events created by wxDataViewHeaderWindow the | |
126 | // row / value fields are not valid | |
127 | return owner->GetEventHandler()->ProcessEvent(event); | |
128 | } | |
129 | ||
130 | void OnClick(wxHeaderCtrlEvent& event) | |
131 | { | |
132 | const unsigned idx = event.GetColumn(); | |
133 | ||
134 | if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, idx) ) | |
135 | return; | |
136 | ||
137 | // default handling for the column click is to sort by this column or | |
138 | // toggle its sort order | |
139 | wxDataViewCtrl * const owner = GetOwner(); | |
140 | wxDataViewColumn * const col = owner->GetColumn(idx); | |
141 | if ( !col->IsSortable() ) | |
142 | { | |
143 | // no default handling for non-sortable columns | |
144 | event.Skip(); | |
145 | return; | |
146 | } | |
147 | ||
148 | if ( col->IsSortKey() ) | |
149 | { | |
150 | // already using this column for sorting, just change the order | |
151 | col->ToggleSortOrder(); | |
152 | } | |
153 | else // not using this column for sorting yet | |
154 | { | |
155 | // first unset the old sort column if any | |
156 | int oldSortKey = owner->GetSortingColumnIndex(); | |
157 | if ( oldSortKey != wxNOT_FOUND ) | |
158 | { | |
159 | owner->GetColumn(oldSortKey)->UnsetAsSortKey(); | |
160 | owner->OnColumnChange(oldSortKey); | |
161 | } | |
162 | ||
163 | owner->SetSortingColumnIndex(idx); | |
164 | col->SetAsSortKey(); | |
165 | } | |
166 | ||
167 | wxDataViewModel * const model = owner->GetModel(); | |
168 | if ( model ) | |
169 | model->Resort(); | |
170 | ||
171 | owner->OnColumnChange(idx); | |
172 | } | |
173 | ||
174 | void OnRClick(wxHeaderCtrlEvent& event) | |
175 | { | |
176 | if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, | |
177 | event.GetColumn()) ) | |
178 | event.Skip(); | |
179 | } | |
180 | ||
181 | void OnResize(wxHeaderCtrlEvent& event) | |
182 | { | |
183 | wxDataViewCtrl * const owner = GetOwner(); | |
184 | ||
185 | const unsigned col = event.GetColumn(); | |
186 | owner->GetColumn(col)->SetWidth(event.GetWidth()); | |
187 | GetOwner()->OnColumnChange(col); | |
188 | } | |
189 | ||
190 | void OnEndReorder(wxHeaderCtrlEvent& event) | |
191 | { | |
192 | wxDataViewCtrl * const owner = GetOwner(); | |
193 | owner->ColumnMoved(owner->GetColumn(event.GetColumn()), | |
194 | event.GetNewOrder()); | |
195 | } | |
196 | ||
197 | DECLARE_EVENT_TABLE() | |
198 | wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow); | |
199 | }; | |
200 | ||
201 | BEGIN_EVENT_TABLE(wxDataViewHeaderWindow, wxHeaderCtrl) | |
202 | EVT_HEADER_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnClick) | |
203 | EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnRClick) | |
204 | ||
205 | EVT_HEADER_RESIZING(wxID_ANY, wxDataViewHeaderWindow::OnResize) | |
206 | EVT_HEADER_END_RESIZE(wxID_ANY, wxDataViewHeaderWindow::OnResize) | |
207 | ||
208 | EVT_HEADER_END_REORDER(wxID_ANY, wxDataViewHeaderWindow::OnEndReorder) | |
209 | END_EVENT_TABLE() | |
210 | ||
211 | //----------------------------------------------------------------------------- | |
212 | // wxDataViewRenameTimer | |
213 | //----------------------------------------------------------------------------- | |
214 | ||
215 | class wxDataViewRenameTimer: public wxTimer | |
216 | { | |
217 | private: | |
218 | wxDataViewMainWindow *m_owner; | |
219 | ||
220 | public: | |
221 | wxDataViewRenameTimer( wxDataViewMainWindow *owner ); | |
222 | void Notify(); | |
223 | }; | |
224 | ||
225 | //----------------------------------------------------------------------------- | |
226 | // wxDataViewTreeNode | |
227 | //----------------------------------------------------------------------------- | |
228 | ||
229 | class wxDataViewTreeNode; | |
230 | WX_DEFINE_ARRAY( wxDataViewTreeNode *, wxDataViewTreeNodes ); | |
231 | WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves); | |
232 | ||
233 | int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1, | |
234 | wxDataViewTreeNode ** node2); | |
235 | int LINKAGEMODE wxGenericTreeModelItemCmp( void ** id1, void ** id2); | |
236 | ||
237 | class wxDataViewTreeNode | |
238 | { | |
239 | public: | |
240 | wxDataViewTreeNode( wxDataViewTreeNode * parent = NULL ) | |
241 | { | |
242 | m_parent = parent; | |
243 | if (!parent) | |
244 | m_open = true; | |
245 | else | |
246 | m_open = false; | |
247 | m_hasChildren = false; | |
248 | m_subTreeCount = 0; | |
249 | } | |
250 | ||
251 | ~wxDataViewTreeNode() | |
252 | { | |
253 | } | |
254 | ||
255 | wxDataViewTreeNode * GetParent() const { return m_parent; } | |
256 | void SetParent( wxDataViewTreeNode * parent ) { m_parent = parent; } | |
257 | wxDataViewTreeNodes & GetNodes() { return m_nodes; } | |
258 | wxDataViewTreeLeaves & GetChildren() { return m_leaves; } | |
259 | ||
260 | void AddNode( wxDataViewTreeNode * node ) | |
261 | { | |
262 | m_leaves.Add( node->GetItem().GetID() ); | |
263 | if (g_column >= -1) | |
264 | m_leaves.Sort( &wxGenericTreeModelItemCmp ); | |
265 | m_nodes.Add( node ); | |
266 | if (g_column >= -1) | |
267 | m_nodes.Sort( &wxGenericTreeModelNodeCmp ); | |
268 | } | |
269 | void AddLeaf( void * leaf ) | |
270 | { | |
271 | m_leaves.Add( leaf ); | |
272 | if (g_column >= -1) | |
273 | m_leaves.Sort( &wxGenericTreeModelItemCmp ); | |
274 | } | |
275 | ||
276 | wxDataViewItem & GetItem() { return m_item; } | |
277 | const wxDataViewItem & GetItem() const { return m_item; } | |
278 | void SetItem( const wxDataViewItem & item ) { m_item = item; } | |
279 | ||
280 | unsigned int GetChildrenNumber() const { return m_leaves.GetCount(); } | |
281 | unsigned int GetNodeNumber() const { return m_nodes.GetCount(); } | |
282 | int GetIndentLevel() const | |
283 | { | |
284 | int ret = 0; | |
285 | const wxDataViewTreeNode * node = this; | |
286 | while( node->GetParent()->GetParent() != NULL ) | |
287 | { | |
288 | node = node->GetParent(); | |
289 | ret ++; | |
290 | } | |
291 | return ret; | |
292 | } | |
293 | ||
294 | bool IsOpen() const | |
295 | { | |
296 | return m_open; | |
297 | } | |
298 | ||
299 | void ToggleOpen() | |
300 | { | |
301 | int len = m_nodes.GetCount(); | |
302 | int sum = 0; | |
303 | for ( int i = 0;i < len; i ++) | |
304 | sum += m_nodes[i]->GetSubTreeCount(); | |
305 | ||
306 | sum += m_leaves.GetCount(); | |
307 | if (m_open) | |
308 | { | |
309 | ChangeSubTreeCount(-sum); | |
310 | m_open = !m_open; | |
311 | } | |
312 | else | |
313 | { | |
314 | m_open = !m_open; | |
315 | ChangeSubTreeCount(sum); | |
316 | } | |
317 | } | |
318 | bool HasChildren() const { return m_hasChildren; } | |
319 | void SetHasChildren( bool has ){ m_hasChildren = has; } | |
320 | ||
321 | void SetSubTreeCount( int num ) { m_subTreeCount = num; } | |
322 | int GetSubTreeCount() const { return m_subTreeCount; } | |
323 | void ChangeSubTreeCount( int num ) | |
324 | { | |
325 | if( !m_open ) | |
326 | return; | |
327 | m_subTreeCount += num; | |
328 | if( m_parent ) | |
329 | m_parent->ChangeSubTreeCount(num); | |
330 | } | |
331 | ||
332 | void Resort() | |
333 | { | |
334 | if (g_column >= -1) | |
335 | { | |
336 | m_nodes.Sort( &wxGenericTreeModelNodeCmp ); | |
337 | int len = m_nodes.GetCount(); | |
338 | for (int i = 0; i < len; i ++) | |
339 | m_nodes[i]->Resort(); | |
340 | m_leaves.Sort( &wxGenericTreeModelItemCmp ); | |
341 | } | |
342 | } | |
343 | ||
344 | private: | |
345 | wxDataViewTreeNode *m_parent; | |
346 | wxDataViewTreeNodes m_nodes; | |
347 | wxDataViewTreeLeaves m_leaves; | |
348 | wxDataViewItem m_item; | |
349 | bool m_open; | |
350 | bool m_hasChildren; | |
351 | int m_subTreeCount; | |
352 | }; | |
353 | ||
354 | int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1, | |
355 | wxDataViewTreeNode ** node2) | |
356 | { | |
357 | return g_model->Compare( (*node1)->GetItem(), (*node2)->GetItem(), g_column, g_asending ); | |
358 | } | |
359 | ||
360 | int LINKAGEMODE wxGenericTreeModelItemCmp( void ** id1, void ** id2) | |
361 | { | |
362 | return g_model->Compare( *id1, *id2, g_column, g_asending ); | |
363 | } | |
364 | ||
365 | ||
366 | //----------------------------------------------------------------------------- | |
367 | // wxDataViewMainWindow | |
368 | //----------------------------------------------------------------------------- | |
369 | ||
370 | WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection, | |
371 | WXDLLIMPEXP_ADV); | |
372 | WX_DECLARE_LIST(wxDataViewItem, ItemList); | |
373 | WX_DEFINE_LIST(ItemList) | |
374 | ||
375 | class wxDataViewMainWindow: public wxWindow | |
376 | { | |
377 | public: | |
378 | wxDataViewMainWindow( wxDataViewCtrl *parent, | |
379 | wxWindowID id, | |
380 | const wxPoint &pos = wxDefaultPosition, | |
381 | const wxSize &size = wxDefaultSize, | |
382 | const wxString &name = wxT("wxdataviewctrlmainwindow") ); | |
383 | virtual ~wxDataViewMainWindow(); | |
384 | ||
385 | bool IsVirtualList() const { return m_root == NULL; } | |
386 | ||
387 | // notifications from wxDataViewModel | |
388 | bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ); | |
389 | bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ); | |
390 | bool ItemChanged( const wxDataViewItem &item ); | |
391 | bool ValueChanged( const wxDataViewItem &item, unsigned int col ); | |
392 | bool Cleared(); | |
393 | void Resort() | |
394 | { | |
395 | if (!IsVirtualList()) | |
396 | { | |
397 | SortPrepare(); | |
398 | m_root->Resort(); | |
399 | } | |
400 | UpdateDisplay(); | |
401 | } | |
402 | ||
403 | void SortPrepare() | |
404 | { | |
405 | g_model = GetOwner()->GetModel(); | |
406 | wxDataViewColumn* col = GetOwner()->GetSortingColumn(); | |
407 | if( !col ) | |
408 | { | |
409 | if (g_model->HasDefaultCompare()) | |
410 | g_column = -1; | |
411 | else | |
412 | g_column = -2; | |
413 | ||
414 | g_asending = true; | |
415 | return; | |
416 | } | |
417 | g_column = col->GetModelColumn(); | |
418 | g_asending = col->IsSortOrderAscending(); | |
419 | } | |
420 | ||
421 | void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; } | |
422 | wxDataViewCtrl *GetOwner() { return m_owner; } | |
423 | const wxDataViewCtrl *GetOwner() const { return m_owner; } | |
424 | ||
425 | #if wxUSE_DRAG_AND_DROP | |
426 | wxBitmap CreateItemBitmap( unsigned int row, int &indent ); | |
427 | #endif // wxUSE_DRAG_AND_DROP | |
428 | void OnPaint( wxPaintEvent &event ); | |
429 | void OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event); | |
430 | void OnChar( wxKeyEvent &event ); | |
431 | void OnMouse( wxMouseEvent &event ); | |
432 | void OnSetFocus( wxFocusEvent &event ); | |
433 | void OnKillFocus( wxFocusEvent &event ); | |
434 | ||
435 | void UpdateDisplay(); | |
436 | void RecalculateDisplay(); | |
437 | void OnInternalIdle(); | |
438 | ||
439 | void OnRenameTimer(); | |
440 | ||
441 | void ScrollWindow( int dx, int dy, const wxRect *rect = NULL ); | |
442 | void ScrollTo( int rows, int column ); | |
443 | ||
444 | bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; } | |
445 | void ChangeCurrentRow( unsigned int row ); | |
446 | ||
447 | bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); } | |
448 | bool IsEmpty() { return GetRowCount() == 0; } | |
449 | ||
450 | int GetCountPerPage() const; | |
451 | int GetEndOfLastCol() const; | |
452 | unsigned int GetFirstVisibleRow() const; | |
453 | ||
454 | // I change this method to un const because in the tree view, | |
455 | // the displaying number of the tree are changing along with the | |
456 | // expanding/collapsing of the tree nodes | |
457 | unsigned int GetLastVisibleRow(); | |
458 | unsigned int GetRowCount(); | |
459 | ||
460 | wxDataViewItem GetSelection() const; | |
461 | wxDataViewSelection GetSelections(){ return m_selection; } | |
462 | void SetSelections( const wxDataViewSelection & sel ) | |
463 | { m_selection = sel; UpdateDisplay(); } | |
464 | void Select( const wxArrayInt& aSelections ); | |
465 | void SelectAllRows( bool on ); | |
466 | void SelectRow( unsigned int row, bool on ); | |
467 | void SelectRows( unsigned int from, unsigned int to, bool on ); | |
468 | void ReverseRowSelection( unsigned int row ); | |
469 | bool IsRowSelected( unsigned int row ); | |
470 | void SendSelectionChangedEvent( const wxDataViewItem& item); | |
471 | ||
472 | void RefreshRow( unsigned int row ); | |
473 | void RefreshRows( unsigned int from, unsigned int to ); | |
474 | void RefreshRowsAfter( unsigned int firstRow ); | |
475 | ||
476 | // returns the colour to be used for drawing the rules | |
477 | wxColour GetRuleColour() const | |
478 | { | |
479 | return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT); | |
480 | } | |
481 | ||
482 | wxRect GetLineRect( unsigned int row ) const; | |
483 | ||
484 | int GetLineStart( unsigned int row ) const; // row * m_lineHeight in fixed mode | |
485 | int GetLineHeight( unsigned int row ) const; // m_lineHeight in fixed mode | |
486 | int GetLineAt( unsigned int y ) const; // y / m_lineHeight in fixed mode | |
487 | ||
488 | // Some useful functions for row and item mapping | |
489 | wxDataViewItem GetItemByRow( unsigned int row ) const; | |
490 | int GetRowByItem( const wxDataViewItem & item ) const; | |
491 | ||
492 | // Methods for building the mapping tree | |
493 | void BuildTree( wxDataViewModel * model ); | |
494 | void DestroyTree(); | |
495 | void HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column ); | |
496 | wxRect GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column ); | |
497 | ||
498 | void Expand( unsigned int row ); | |
499 | void Collapse( unsigned int row ); | |
500 | bool IsExpanded( unsigned int row ) const; | |
501 | bool HasChildren( unsigned int row ) const; | |
502 | ||
503 | #if wxUSE_DRAG_AND_DROP | |
504 | bool EnableDragSource( const wxDataFormat &format ); | |
505 | bool EnableDropTarget( const wxDataFormat &format ); | |
506 | ||
507 | void RemoveDropHint(); | |
508 | wxDragResult OnDragOver( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def ); | |
509 | bool OnDrop( wxDataFormat format, wxCoord x, wxCoord y ); | |
510 | wxDragResult OnData( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def ); | |
511 | void OnLeave(); | |
512 | #endif // wxUSE_DRAG_AND_DROP | |
513 | ||
514 | private: | |
515 | wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row ) const; | |
516 | // We did not need this temporarily | |
517 | // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item ); | |
518 | ||
519 | int RecalculateCount(); | |
520 | ||
521 | wxDataViewEvent SendExpanderEvent( wxEventType type, const wxDataViewItem & item ); | |
522 | ||
523 | wxDataViewTreeNode * FindNode( const wxDataViewItem & item ); | |
524 | ||
525 | private: | |
526 | wxDataViewCtrl *m_owner; | |
527 | int m_lineHeight; | |
528 | bool m_dirty; | |
529 | ||
530 | wxDataViewColumn *m_currentCol; | |
531 | unsigned int m_currentRow; | |
532 | wxDataViewSelection m_selection; | |
533 | ||
534 | wxDataViewRenameTimer *m_renameTimer; | |
535 | bool m_lastOnSame; | |
536 | ||
537 | bool m_hasFocus; | |
538 | ||
539 | #if wxUSE_DRAG_AND_DROP | |
540 | int m_dragCount; | |
541 | wxPoint m_dragStart; | |
542 | ||
543 | bool m_dragEnabled; | |
544 | wxDataFormat m_dragFormat; | |
545 | ||
546 | bool m_dropEnabled; | |
547 | wxDataFormat m_dropFormat; | |
548 | bool m_dropHint; | |
549 | unsigned int m_dropHintLine; | |
550 | #endif // wxUSE_DRAG_AND_DROP | |
551 | ||
552 | // for double click logic | |
553 | unsigned int m_lineLastClicked, | |
554 | m_lineBeforeLastClicked, | |
555 | m_lineSelectSingleOnUp; | |
556 | ||
557 | // the pen used to draw horiz/vertical rules | |
558 | wxPen m_penRule; | |
559 | ||
560 | // the pen used to draw the expander and the lines | |
561 | wxPen m_penExpander; | |
562 | ||
563 | // This is the tree structure of the model | |
564 | wxDataViewTreeNode * m_root; | |
565 | int m_count; | |
566 | ||
567 | // This is the tree node under the cursor | |
568 | wxDataViewTreeNode * m_underMouse; | |
569 | ||
570 | private: | |
571 | DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow) | |
572 | DECLARE_EVENT_TABLE() | |
573 | }; | |
574 | ||
575 | // --------------------------------------------------------- | |
576 | // wxGenericDataViewModelNotifier | |
577 | // --------------------------------------------------------- | |
578 | ||
579 | class wxGenericDataViewModelNotifier: public wxDataViewModelNotifier | |
580 | { | |
581 | public: | |
582 | wxGenericDataViewModelNotifier( wxDataViewMainWindow *mainWindow ) | |
583 | { m_mainWindow = mainWindow; } | |
584 | ||
585 | virtual bool ItemAdded( const wxDataViewItem & parent, const wxDataViewItem & item ) | |
586 | { return m_mainWindow->ItemAdded( parent , item ); } | |
587 | virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) | |
588 | { return m_mainWindow->ItemDeleted( parent, item ); } | |
589 | virtual bool ItemChanged( const wxDataViewItem & item ) | |
590 | { return m_mainWindow->ItemChanged(item); } | |
591 | virtual bool ValueChanged( const wxDataViewItem & item , unsigned int col ) | |
592 | { return m_mainWindow->ValueChanged( item, col ); } | |
593 | virtual bool Cleared() | |
594 | { return m_mainWindow->Cleared(); } | |
595 | virtual void Resort() | |
596 | { m_mainWindow->Resort(); } | |
597 | ||
598 | wxDataViewMainWindow *m_mainWindow; | |
599 | }; | |
600 | ||
601 | // --------------------------------------------------------- | |
602 | // wxDataViewRenderer | |
603 | // --------------------------------------------------------- | |
604 | ||
605 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase) | |
606 | ||
607 | wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, | |
608 | wxDataViewCellMode mode, | |
609 | int align) : | |
610 | wxDataViewRendererBase( varianttype, mode, align ) | |
611 | { | |
612 | m_dc = NULL; | |
613 | m_align = align; | |
614 | m_mode = mode; | |
615 | m_wantsAttr = false; | |
616 | m_hasAttr = false; | |
617 | } | |
618 | ||
619 | wxDataViewRenderer::~wxDataViewRenderer() | |
620 | { | |
621 | if (m_dc) | |
622 | delete m_dc; | |
623 | } | |
624 | ||
625 | wxDC *wxDataViewRenderer::GetDC() | |
626 | { | |
627 | if (m_dc == NULL) | |
628 | { | |
629 | if (GetOwner() == NULL) | |
630 | return NULL; | |
631 | if (GetOwner()->GetOwner() == NULL) | |
632 | return NULL; | |
633 | m_dc = new wxClientDC( GetOwner()->GetOwner() ); | |
634 | } | |
635 | ||
636 | return m_dc; | |
637 | } | |
638 | ||
639 | void wxDataViewRenderer::SetAlignment( int align ) | |
640 | { | |
641 | m_align=align; | |
642 | } | |
643 | ||
644 | int wxDataViewRenderer::GetAlignment() const | |
645 | { | |
646 | return m_align; | |
647 | } | |
648 | ||
649 | int wxDataViewRenderer::CalculateAlignment() const | |
650 | { | |
651 | if (m_align == wxDVR_DEFAULT_ALIGNMENT) | |
652 | { | |
653 | if (GetOwner() == NULL) | |
654 | return wxALIGN_LEFT | wxALIGN_CENTRE_VERTICAL; | |
655 | ||
656 | return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL; | |
657 | } | |
658 | ||
659 | return m_align; | |
660 | } | |
661 | ||
662 | // --------------------------------------------------------- | |
663 | // wxDataViewCustomRenderer | |
664 | // --------------------------------------------------------- | |
665 | ||
666 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer) | |
667 | ||
668 | wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype, | |
669 | wxDataViewCellMode mode, int align ) : | |
670 | wxDataViewRenderer( varianttype, mode, align ) | |
671 | { | |
672 | } | |
673 | ||
674 | void wxDataViewCustomRenderer::RenderText( const wxString &text, int xoffset, | |
675 | wxRect cell, wxDC *dc, int state ) | |
676 | { | |
677 | wxDataViewCtrl *view = GetOwner()->GetOwner(); | |
678 | wxColour col = (state & wxDATAVIEW_CELL_SELECTED) ? | |
679 | wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) : | |
680 | view->GetForegroundColour(); | |
681 | dc->SetTextForeground(col); | |
682 | dc->DrawText( text, | |
683 | cell.x + xoffset, | |
684 | cell.y + ((cell.height - dc->GetCharHeight()) / 2)); | |
685 | } | |
686 | ||
687 | // --------------------------------------------------------- | |
688 | // wxDataViewTextRenderer | |
689 | // --------------------------------------------------------- | |
690 | ||
691 | IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewCustomRenderer) | |
692 | ||
693 | wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, | |
694 | wxDataViewCellMode mode, int align ) : | |
695 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
696 | { | |
697 | } | |
698 | ||
699 | bool wxDataViewTextRenderer::SetValue( const wxVariant &value ) | |
700 | { | |
701 | m_text = value.GetString(); | |
702 | ||
703 | return true; | |
704 | } | |
705 | ||
706 | bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const | |
707 | { | |
708 | return false; | |
709 | } | |
710 | ||
711 | bool wxDataViewTextRenderer::HasEditorCtrl() const | |
712 | { | |
713 | return true; | |
714 | } | |
715 | ||
716 | wxControl* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent, | |
717 | wxRect labelRect, const wxVariant &value ) | |
718 | { | |
719 | wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, value, | |
720 | wxPoint(labelRect.x,labelRect.y), | |
721 | wxSize(labelRect.width,labelRect.height) ); | |
722 | ||
723 | // select the text in the control an place the cursor at the end | |
724 | ctrl->SetInsertionPointEnd(); | |
725 | ctrl->SelectAll(); | |
726 | ||
727 | return ctrl; | |
728 | } | |
729 | ||
730 | bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl *editor, wxVariant &value ) | |
731 | { | |
732 | wxTextCtrl *text = (wxTextCtrl*) editor; | |
733 | value = text->GetValue(); | |
734 | return true; | |
735 | } | |
736 | ||
737 | bool wxDataViewTextRenderer::Render( wxRect cell, wxDC *dc, int state ) | |
738 | { | |
739 | RenderText( m_text, 0, cell, dc, state ); | |
740 | return true; | |
741 | } | |
742 | ||
743 | wxSize wxDataViewTextRenderer::GetSize() const | |
744 | { | |
745 | const wxDataViewCtrl *view = GetView(); | |
746 | if (!m_text.empty()) | |
747 | return view->wxWindowBase::GetTextExtent( m_text ); | |
748 | return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); | |
749 | } | |
750 | ||
751 | // --------------------------------------------------------- | |
752 | // wxDataViewTextRendererAttr | |
753 | // --------------------------------------------------------- | |
754 | ||
755 | IMPLEMENT_CLASS(wxDataViewTextRendererAttr, wxDataViewTextRenderer) | |
756 | ||
757 | wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString &varianttype, | |
758 | wxDataViewCellMode mode, int align ) : | |
759 | wxDataViewTextRenderer( varianttype, mode, align ) | |
760 | { | |
761 | m_wantsAttr = true; | |
762 | } | |
763 | ||
764 | bool wxDataViewTextRendererAttr::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) | |
765 | { | |
766 | wxFont font; | |
767 | wxColour colour; | |
768 | ||
769 | if (m_hasAttr) | |
770 | { | |
771 | if (m_attr.HasColour()) | |
772 | { | |
773 | colour = dc->GetTextForeground(); | |
774 | dc->SetTextForeground( m_attr.GetColour() ); | |
775 | } | |
776 | ||
777 | if (m_attr.GetBold() || m_attr.GetItalic()) | |
778 | { | |
779 | font = dc->GetFont(); | |
780 | wxFont myfont = font; | |
781 | if (m_attr.GetBold()) | |
782 | myfont.SetWeight( wxFONTWEIGHT_BOLD ); | |
783 | if (m_attr.GetItalic()) | |
784 | myfont.SetStyle( wxFONTSTYLE_ITALIC ); | |
785 | dc->SetFont( myfont ); | |
786 | } | |
787 | } | |
788 | ||
789 | dc->DrawText( m_text, cell.x, cell.y + ((cell.height - dc->GetCharHeight()) / 2)); | |
790 | ||
791 | // restore dc | |
792 | if (m_hasAttr) | |
793 | { | |
794 | if (m_attr.HasColour()) | |
795 | dc->SetTextForeground( colour ); | |
796 | ||
797 | if (m_attr.GetBold() || m_attr.GetItalic()) | |
798 | dc->SetFont( font ); | |
799 | } | |
800 | ||
801 | return true; | |
802 | } | |
803 | ||
804 | ||
805 | // --------------------------------------------------------- | |
806 | // wxDataViewBitmapRenderer | |
807 | // --------------------------------------------------------- | |
808 | ||
809 | IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewCustomRenderer) | |
810 | ||
811 | wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, | |
812 | wxDataViewCellMode mode, int align ) : | |
813 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
814 | { | |
815 | } | |
816 | ||
817 | bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value ) | |
818 | { | |
819 | if (value.GetType() == wxT("wxBitmap")) | |
820 | m_bitmap << value; | |
821 | if (value.GetType() == wxT("wxIcon")) | |
822 | m_icon << value; | |
823 | ||
824 | return true; | |
825 | } | |
826 | ||
827 | bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const | |
828 | { | |
829 | return false; | |
830 | } | |
831 | ||
832 | bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) | |
833 | { | |
834 | if (m_bitmap.Ok()) | |
835 | dc->DrawBitmap( m_bitmap, cell.x, cell.y ); | |
836 | else if (m_icon.Ok()) | |
837 | dc->DrawIcon( m_icon, cell.x, cell.y ); | |
838 | ||
839 | return true; | |
840 | } | |
841 | ||
842 | wxSize wxDataViewBitmapRenderer::GetSize() const | |
843 | { | |
844 | if (m_bitmap.Ok()) | |
845 | return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() ); | |
846 | else if (m_icon.Ok()) | |
847 | return wxSize( m_icon.GetWidth(), m_icon.GetHeight() ); | |
848 | ||
849 | return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); | |
850 | } | |
851 | ||
852 | // --------------------------------------------------------- | |
853 | // wxDataViewToggleRenderer | |
854 | // --------------------------------------------------------- | |
855 | ||
856 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewCustomRenderer) | |
857 | ||
858 | wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype, | |
859 | wxDataViewCellMode mode, int align ) : | |
860 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
861 | { | |
862 | m_toggle = false; | |
863 | } | |
864 | ||
865 | bool wxDataViewToggleRenderer::SetValue( const wxVariant &value ) | |
866 | { | |
867 | m_toggle = value.GetBool(); | |
868 | ||
869 | return true; | |
870 | } | |
871 | ||
872 | bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) ) const | |
873 | { | |
874 | return false; | |
875 | } | |
876 | ||
877 | bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) | |
878 | { | |
879 | int flags = 0; | |
880 | if (m_toggle) | |
881 | flags |= wxCONTROL_CHECKED; | |
882 | if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE) | |
883 | flags |= wxCONTROL_DISABLED; | |
884 | ||
885 | wxRendererNative::Get().DrawCheckBox( | |
886 | GetOwner()->GetOwner(), | |
887 | *dc, | |
888 | cell, | |
889 | flags ); | |
890 | ||
891 | return true; | |
892 | } | |
893 | ||
894 | bool wxDataViewToggleRenderer::Activate( wxRect WXUNUSED(cell), | |
895 | wxDataViewModel *model, | |
896 | const wxDataViewItem & item, unsigned int col) | |
897 | { | |
898 | bool value = !m_toggle; | |
899 | wxVariant variant = value; | |
900 | model->SetValue( variant, item, col); | |
901 | model->ValueChanged( item, col ); | |
902 | return true; | |
903 | } | |
904 | ||
905 | wxSize wxDataViewToggleRenderer::GetSize() const | |
906 | { | |
907 | // the window parameter is not used by GetCheckBoxSize() so it's | |
908 | // safe to pass NULL | |
909 | return wxRendererNative::Get().GetCheckBoxSize(NULL); | |
910 | } | |
911 | ||
912 | // --------------------------------------------------------- | |
913 | // wxDataViewProgressRenderer | |
914 | // --------------------------------------------------------- | |
915 | ||
916 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer) | |
917 | ||
918 | wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label, | |
919 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : | |
920 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
921 | { | |
922 | m_label = label; | |
923 | m_value = 0; | |
924 | } | |
925 | ||
926 | wxDataViewProgressRenderer::~wxDataViewProgressRenderer() | |
927 | { | |
928 | } | |
929 | ||
930 | bool wxDataViewProgressRenderer::SetValue( const wxVariant &value ) | |
931 | { | |
932 | m_value = (long) value; | |
933 | ||
934 | if (m_value < 0) m_value = 0; | |
935 | if (m_value > 100) m_value = 100; | |
936 | ||
937 | return true; | |
938 | } | |
939 | ||
940 | bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const | |
941 | { | |
942 | value = (long) m_value; | |
943 | return true; | |
944 | } | |
945 | ||
946 | bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) | |
947 | { | |
948 | double pct = (double)m_value / 100.0; | |
949 | wxRect bar = cell; | |
950 | bar.width = (int)(cell.width * pct); | |
951 | dc->SetPen( *wxTRANSPARENT_PEN ); | |
952 | dc->SetBrush( *wxBLUE_BRUSH ); | |
953 | dc->DrawRectangle( bar ); | |
954 | ||
955 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); | |
956 | dc->SetPen( *wxBLACK_PEN ); | |
957 | dc->DrawRectangle( cell ); | |
958 | ||
959 | return true; | |
960 | } | |
961 | ||
962 | wxSize wxDataViewProgressRenderer::GetSize() const | |
963 | { | |
964 | return wxSize(40,12); | |
965 | } | |
966 | ||
967 | // --------------------------------------------------------- | |
968 | // wxDataViewDateRenderer | |
969 | // --------------------------------------------------------- | |
970 | ||
971 | #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN) | |
972 | ||
973 | #if wxUSE_DATE_RENDERER_POPUP | |
974 | ||
975 | class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow | |
976 | { | |
977 | public: | |
978 | wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value, | |
979 | wxDataViewModel *model, const wxDataViewItem & item, unsigned int col) : | |
980 | wxPopupTransientWindow( parent, wxBORDER_SIMPLE ), | |
981 | m_item( item ) | |
982 | { | |
983 | m_model = model; | |
984 | m_col = col; | |
985 | m_cal = new wxCalendarCtrl( this, wxID_ANY, *value ); | |
986 | wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); | |
987 | sizer->Add( m_cal, 1, wxGROW ); | |
988 | SetSizer( sizer ); | |
989 | sizer->Fit( this ); | |
990 | } | |
991 | ||
992 | void OnCalendar( wxCalendarEvent &event ); | |
993 | ||
994 | wxCalendarCtrl *m_cal; | |
995 | wxDataViewModel *m_model; | |
996 | unsigned int m_col; | |
997 | const wxDataViewItem & m_item; | |
998 | ||
999 | protected: | |
1000 | virtual void OnDismiss() | |
1001 | { | |
1002 | } | |
1003 | ||
1004 | private: | |
1005 | DECLARE_EVENT_TABLE() | |
1006 | }; | |
1007 | ||
1008 | BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow) | |
1009 | EVT_CALENDAR( wxID_ANY, wxDataViewDateRendererPopupTransient::OnCalendar ) | |
1010 | END_EVENT_TABLE() | |
1011 | ||
1012 | void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event ) | |
1013 | { | |
1014 | wxDateTime date = event.GetDate(); | |
1015 | wxVariant value = date; | |
1016 | m_model->SetValue( value, m_item, m_col ); | |
1017 | m_model->ValueChanged( m_item, m_col ); | |
1018 | DismissAndNotify(); | |
1019 | } | |
1020 | ||
1021 | #endif // wxUSE_DATE_RENDERER_POPUP | |
1022 | ||
1023 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer) | |
1024 | ||
1025 | wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype, | |
1026 | wxDataViewCellMode mode, int align ) : | |
1027 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
1028 | { | |
1029 | } | |
1030 | ||
1031 | bool wxDataViewDateRenderer::SetValue( const wxVariant &value ) | |
1032 | { | |
1033 | m_date = value.GetDateTime(); | |
1034 | ||
1035 | return true; | |
1036 | } | |
1037 | ||
1038 | bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const | |
1039 | { | |
1040 | value = m_date; | |
1041 | return true; | |
1042 | } | |
1043 | ||
1044 | bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state ) | |
1045 | { | |
1046 | wxString tmp = m_date.FormatDate(); | |
1047 | RenderText( tmp, 0, cell, dc, state ); | |
1048 | return true; | |
1049 | } | |
1050 | ||
1051 | wxSize wxDataViewDateRenderer::GetSize() const | |
1052 | { | |
1053 | const wxDataViewCtrl* view = GetView(); | |
1054 | wxString tmp = m_date.FormatDate(); | |
1055 | wxCoord x,y,d; | |
1056 | view->GetTextExtent( tmp, &x, &y, &d ); | |
1057 | return wxSize(x,y+d); | |
1058 | } | |
1059 | ||
1060 | bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model, | |
1061 | const wxDataViewItem & item, unsigned int col ) | |
1062 | { | |
1063 | wxVariant variant; | |
1064 | model->GetValue( variant, item, col ); | |
1065 | wxDateTime value = variant.GetDateTime(); | |
1066 | ||
1067 | #if wxUSE_DATE_RENDERER_POPUP | |
1068 | wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient( | |
1069 | GetOwner()->GetOwner()->GetParent(), &value, model, item, col); | |
1070 | wxPoint pos = wxGetMousePosition(); | |
1071 | popup->Move( pos ); | |
1072 | popup->Layout(); | |
1073 | popup->Popup( popup->m_cal ); | |
1074 | #else // !wxUSE_DATE_RENDERER_POPUP | |
1075 | wxMessageBox(value.Format()); | |
1076 | #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP | |
1077 | return true; | |
1078 | } | |
1079 | ||
1080 | // --------------------------------------------------------- | |
1081 | // wxDataViewIconTextRenderer | |
1082 | // --------------------------------------------------------- | |
1083 | ||
1084 | IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer) | |
1085 | ||
1086 | wxDataViewIconTextRenderer::wxDataViewIconTextRenderer( | |
1087 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : | |
1088 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
1089 | { | |
1090 | SetMode(mode); | |
1091 | SetAlignment(align); | |
1092 | } | |
1093 | ||
1094 | wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer() | |
1095 | { | |
1096 | } | |
1097 | ||
1098 | bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value ) | |
1099 | { | |
1100 | m_value << value; | |
1101 | return true; | |
1102 | } | |
1103 | ||
1104 | bool wxDataViewIconTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const | |
1105 | { | |
1106 | return false; | |
1107 | } | |
1108 | ||
1109 | bool wxDataViewIconTextRenderer::Render( wxRect cell, wxDC *dc, int state ) | |
1110 | { | |
1111 | int xoffset = 0; | |
1112 | const wxIcon &icon = m_value.GetIcon(); | |
1113 | if (icon.IsOk()) | |
1114 | { | |
1115 | dc->DrawIcon( icon, cell.x, cell.y + ((cell.height - icon.GetHeight()) / 2)); | |
1116 | xoffset = icon.GetWidth()+4; | |
1117 | } | |
1118 | ||
1119 | RenderText( m_value.GetText(), xoffset, cell, dc, state ); | |
1120 | ||
1121 | return true; | |
1122 | } | |
1123 | ||
1124 | wxSize wxDataViewIconTextRenderer::GetSize() const | |
1125 | { | |
1126 | const wxDataViewCtrl *view = GetView(); | |
1127 | if (!m_value.GetText().empty()) | |
1128 | { | |
1129 | int x,y; | |
1130 | view->GetTextExtent( m_value.GetText(), &x, &y ); | |
1131 | ||
1132 | if (m_value.GetIcon().IsOk()) | |
1133 | x += m_value.GetIcon().GetWidth() + 4; | |
1134 | return wxSize( x, y ); | |
1135 | } | |
1136 | return wxSize(80,20); | |
1137 | } | |
1138 | ||
1139 | wxControl* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value) | |
1140 | { | |
1141 | wxDataViewIconText iconText; | |
1142 | iconText << value; | |
1143 | ||
1144 | wxString text = iconText.GetText(); | |
1145 | ||
1146 | // adjust the label rect to take the width of the icon into account | |
1147 | if (iconText.GetIcon().IsOk()) | |
1148 | { | |
1149 | int w = iconText.GetIcon().GetWidth() + 4; | |
1150 | labelRect.x += w; | |
1151 | labelRect.width -= w; | |
1152 | } | |
1153 | ||
1154 | wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, text, | |
1155 | wxPoint(labelRect.x,labelRect.y), | |
1156 | wxSize(labelRect.width,labelRect.height) ); | |
1157 | ||
1158 | // select the text in the control an place the cursor at the end | |
1159 | ctrl->SetInsertionPointEnd(); | |
1160 | ctrl->SelectAll(); | |
1161 | ||
1162 | return ctrl; | |
1163 | } | |
1164 | ||
1165 | bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl *editor, wxVariant& value ) | |
1166 | { | |
1167 | wxTextCtrl *text = (wxTextCtrl*) editor; | |
1168 | ||
1169 | wxDataViewIconText iconText(text->GetValue(), m_value.GetIcon()); | |
1170 | value << iconText; | |
1171 | return true; | |
1172 | } | |
1173 | ||
1174 | //----------------------------------------------------------------------------- | |
1175 | // wxDataViewDropTarget | |
1176 | //----------------------------------------------------------------------------- | |
1177 | ||
1178 | #if wxUSE_DRAG_AND_DROP | |
1179 | ||
1180 | class wxBitmapCanvas: public wxWindow | |
1181 | { | |
1182 | public: | |
1183 | wxBitmapCanvas( wxWindow *parent, const wxBitmap &bitmap, const wxSize &size ) : | |
1184 | wxWindow( parent, wxID_ANY, wxPoint(0,0), size ) | |
1185 | { | |
1186 | m_bitmap = bitmap; | |
1187 | Connect( wxEVT_PAINT, wxPaintEventHandler(wxBitmapCanvas::OnPaint) ); | |
1188 | } | |
1189 | ||
1190 | void OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
1191 | { | |
1192 | wxPaintDC dc(this); | |
1193 | dc.DrawBitmap( m_bitmap, 0, 0); | |
1194 | } | |
1195 | ||
1196 | wxBitmap m_bitmap; | |
1197 | }; | |
1198 | ||
1199 | class wxDataViewDropSource: public wxDropSource | |
1200 | { | |
1201 | public: | |
1202 | wxDataViewDropSource( wxDataViewMainWindow *win, unsigned int row ) : | |
1203 | wxDropSource( win ) | |
1204 | { | |
1205 | m_win = win; | |
1206 | m_row = row; | |
1207 | m_hint = NULL; | |
1208 | } | |
1209 | ||
1210 | ~wxDataViewDropSource() | |
1211 | { | |
1212 | delete m_hint; | |
1213 | } | |
1214 | ||
1215 | virtual bool GiveFeedback( wxDragResult WXUNUSED(effect) ) | |
1216 | { | |
1217 | wxPoint pos = wxGetMousePosition(); | |
1218 | ||
1219 | if (!m_hint) | |
1220 | { | |
1221 | int liney = m_win->GetLineStart( m_row ); | |
1222 | int linex = 0; | |
1223 | m_win->GetOwner()->CalcUnscrolledPosition( 0, liney, NULL, &liney ); | |
1224 | m_win->ClientToScreen( &linex, &liney ); | |
1225 | m_dist_x = pos.x - linex; | |
1226 | m_dist_y = pos.y - liney; | |
1227 | ||
1228 | int indent = 0; | |
1229 | wxBitmap ib = m_win->CreateItemBitmap( m_row, indent ); | |
1230 | m_dist_x -= indent; | |
1231 | m_hint = new wxFrame( m_win->GetParent(), wxID_ANY, wxEmptyString, | |
1232 | wxPoint(pos.x - m_dist_x, pos.y + 5 ), | |
1233 | ib.GetSize(), | |
1234 | wxFRAME_TOOL_WINDOW | | |
1235 | wxFRAME_FLOAT_ON_PARENT | | |
1236 | wxFRAME_NO_TASKBAR | | |
1237 | wxNO_BORDER ); | |
1238 | new wxBitmapCanvas( m_hint, ib, ib.GetSize() ); | |
1239 | m_hint->Show(); | |
1240 | } | |
1241 | else | |
1242 | { | |
1243 | m_hint->Move( pos.x - m_dist_x, pos.y + 5 ); | |
1244 | m_hint->SetTransparent( 128 ); | |
1245 | } | |
1246 | ||
1247 | return false; | |
1248 | } | |
1249 | ||
1250 | wxDataViewMainWindow *m_win; | |
1251 | unsigned int m_row; | |
1252 | wxFrame *m_hint; | |
1253 | int m_dist_x,m_dist_y; | |
1254 | }; | |
1255 | ||
1256 | ||
1257 | class wxDataViewDropTarget: public wxDropTarget | |
1258 | { | |
1259 | public: | |
1260 | wxDataViewDropTarget( wxDataObject *obj, wxDataViewMainWindow *win ) : | |
1261 | wxDropTarget( obj ) | |
1262 | { | |
1263 | m_win = win; | |
1264 | } | |
1265 | ||
1266 | virtual wxDragResult OnDragOver( wxCoord x, wxCoord y, wxDragResult def ) | |
1267 | { | |
1268 | wxDataFormat format = GetMatchingPair(); | |
1269 | if (format == wxDF_INVALID) | |
1270 | return wxDragNone; | |
1271 | return m_win->OnDragOver( format, x, y, def); | |
1272 | } | |
1273 | ||
1274 | virtual bool OnDrop( wxCoord x, wxCoord y ) | |
1275 | { | |
1276 | wxDataFormat format = GetMatchingPair(); | |
1277 | if (format == wxDF_INVALID) | |
1278 | return false; | |
1279 | return m_win->OnDrop( format, x, y ); | |
1280 | } | |
1281 | ||
1282 | virtual wxDragResult OnData( wxCoord x, wxCoord y, wxDragResult def ) | |
1283 | { | |
1284 | wxDataFormat format = GetMatchingPair(); | |
1285 | if (format == wxDF_INVALID) | |
1286 | return wxDragNone; | |
1287 | if (!GetData()) | |
1288 | return wxDragNone; | |
1289 | return m_win->OnData( format, x, y, def ); | |
1290 | } | |
1291 | ||
1292 | virtual void OnLeave() | |
1293 | { m_win->OnLeave(); } | |
1294 | ||
1295 | wxDataViewMainWindow *m_win; | |
1296 | }; | |
1297 | ||
1298 | #endif // wxUSE_DRAG_AND_DROP | |
1299 | ||
1300 | //----------------------------------------------------------------------------- | |
1301 | // wxDataViewRenameTimer | |
1302 | //----------------------------------------------------------------------------- | |
1303 | ||
1304 | wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner ) | |
1305 | { | |
1306 | m_owner = owner; | |
1307 | } | |
1308 | ||
1309 | void wxDataViewRenameTimer::Notify() | |
1310 | { | |
1311 | m_owner->OnRenameTimer(); | |
1312 | } | |
1313 | ||
1314 | //----------------------------------------------------------------------------- | |
1315 | // wxDataViewMainWindow | |
1316 | //----------------------------------------------------------------------------- | |
1317 | ||
1318 | // The tree building helper, declared firstly | |
1319 | static void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, | |
1320 | wxDataViewTreeNode * node); | |
1321 | ||
1322 | int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 ) | |
1323 | { | |
1324 | if (row1 > row2) return 1; | |
1325 | if (row1 == row2) return 0; | |
1326 | return -1; | |
1327 | } | |
1328 | ||
1329 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow) | |
1330 | ||
1331 | BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow) | |
1332 | EVT_PAINT (wxDataViewMainWindow::OnPaint) | |
1333 | EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse) | |
1334 | EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus) | |
1335 | EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus) | |
1336 | EVT_CHAR (wxDataViewMainWindow::OnChar) | |
1337 | END_EVENT_TABLE() | |
1338 | ||
1339 | wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id, | |
1340 | const wxPoint &pos, const wxSize &size, const wxString &name ) : | |
1341 | wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE, name ), | |
1342 | m_selection( wxDataViewSelectionCmp ) | |
1343 | ||
1344 | { | |
1345 | SetOwner( parent ); | |
1346 | ||
1347 | m_lastOnSame = false; | |
1348 | m_renameTimer = new wxDataViewRenameTimer( this ); | |
1349 | ||
1350 | // TODO: user better initial values/nothing selected | |
1351 | m_currentCol = NULL; | |
1352 | m_currentRow = 0; | |
1353 | ||
1354 | m_lineHeight = wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1 | |
1355 | ||
1356 | #if wxUSE_DRAG_AND_DROP | |
1357 | m_dragCount = 0; | |
1358 | m_dragStart = wxPoint(0,0); | |
1359 | ||
1360 | m_dragEnabled = false; | |
1361 | m_dropEnabled = false; | |
1362 | m_dropHint = false; | |
1363 | m_dropHintLine = (unsigned int) -1; | |
1364 | #endif // wxUSE_DRAG_AND_DROP | |
1365 | ||
1366 | m_lineLastClicked = (unsigned int) -1; | |
1367 | m_lineBeforeLastClicked = (unsigned int) -1; | |
1368 | m_lineSelectSingleOnUp = (unsigned int) -1; | |
1369 | ||
1370 | m_hasFocus = false; | |
1371 | ||
1372 | SetBackgroundColour( *wxWHITE ); | |
1373 | ||
1374 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
1375 | ||
1376 | m_penRule = wxPen(GetRuleColour()); | |
1377 | ||
1378 | // compose a pen whichcan draw black lines | |
1379 | // TODO: maybe there is something system colour to use | |
1380 | m_penExpander = wxPen(wxColour(0,0,0)); | |
1381 | ||
1382 | m_root = new wxDataViewTreeNode( NULL ); | |
1383 | m_root->SetHasChildren(true); | |
1384 | ||
1385 | // Make m_count = -1 will cause the class recaculate the real displaying number of rows. | |
1386 | m_count = -1; | |
1387 | m_underMouse = NULL; | |
1388 | UpdateDisplay(); | |
1389 | } | |
1390 | ||
1391 | wxDataViewMainWindow::~wxDataViewMainWindow() | |
1392 | { | |
1393 | DestroyTree(); | |
1394 | delete m_renameTimer; | |
1395 | } | |
1396 | ||
1397 | ||
1398 | #if wxUSE_DRAG_AND_DROP | |
1399 | bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat &format ) | |
1400 | { | |
1401 | m_dragFormat = format; | |
1402 | m_dragEnabled = format != wxDF_INVALID; | |
1403 | ||
1404 | return true; | |
1405 | } | |
1406 | ||
1407 | bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat &format ) | |
1408 | { | |
1409 | m_dropFormat = format; | |
1410 | m_dropEnabled = format != wxDF_INVALID; | |
1411 | ||
1412 | if (m_dropEnabled) | |
1413 | SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format ), this ) ); | |
1414 | ||
1415 | return true; | |
1416 | } | |
1417 | ||
1418 | void wxDataViewMainWindow::RemoveDropHint() | |
1419 | { | |
1420 | if (m_dropHint) | |
1421 | { | |
1422 | m_dropHint = false; | |
1423 | RefreshRow( m_dropHintLine ); | |
1424 | m_dropHintLine = (unsigned int) -1; | |
1425 | } | |
1426 | } | |
1427 | ||
1428 | wxDragResult wxDataViewMainWindow::OnDragOver( wxDataFormat format, wxCoord x, | |
1429 | wxCoord y, wxDragResult def ) | |
1430 | { | |
1431 | int xx = x; | |
1432 | int yy = y; | |
1433 | m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy ); | |
1434 | unsigned int row = GetLineAt( yy ); | |
1435 | ||
1436 | if ((row >= GetRowCount()) || (yy > GetEndOfLastCol())) | |
1437 | { | |
1438 | RemoveDropHint(); | |
1439 | return wxDragNone; | |
1440 | } | |
1441 | ||
1442 | wxDataViewItem item = GetItemByRow( row ); | |
1443 | ||
1444 | wxDataViewModel *model = GetOwner()->GetModel(); | |
1445 | ||
1446 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() ); | |
1447 | event.SetEventObject( m_owner ); | |
1448 | event.SetItem( item ); | |
1449 | event.SetModel( model ); | |
1450 | event.SetDataFormat( format ); | |
1451 | if (!m_owner->HandleWindowEvent( event )) | |
1452 | { | |
1453 | RemoveDropHint(); | |
1454 | return wxDragNone; | |
1455 | } | |
1456 | ||
1457 | if (!event.IsAllowed()) | |
1458 | { | |
1459 | RemoveDropHint(); | |
1460 | return wxDragNone; | |
1461 | } | |
1462 | ||
1463 | ||
1464 | if (m_dropHint && (row != m_dropHintLine)) | |
1465 | RefreshRow( m_dropHintLine ); | |
1466 | m_dropHint = true; | |
1467 | m_dropHintLine = row; | |
1468 | RefreshRow( row ); | |
1469 | ||
1470 | return def; | |
1471 | } | |
1472 | ||
1473 | bool wxDataViewMainWindow::OnDrop( wxDataFormat format, wxCoord x, wxCoord y ) | |
1474 | { | |
1475 | RemoveDropHint(); | |
1476 | ||
1477 | int xx = x; | |
1478 | int yy = y; | |
1479 | m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy ); | |
1480 | unsigned int row = GetLineAt( yy ); | |
1481 | ||
1482 | if ((row >= GetRowCount()) || (yy > GetEndOfLastCol())) | |
1483 | return false; | |
1484 | ||
1485 | wxDataViewItem item = GetItemByRow( row ); | |
1486 | ||
1487 | wxDataViewModel *model = GetOwner()->GetModel(); | |
1488 | ||
1489 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() ); | |
1490 | event.SetEventObject( m_owner ); | |
1491 | event.SetItem( item ); | |
1492 | event.SetModel( model ); | |
1493 | event.SetDataFormat( format ); | |
1494 | if (!m_owner->HandleWindowEvent( event )) | |
1495 | return false; | |
1496 | ||
1497 | if (!event.IsAllowed()) | |
1498 | return false; | |
1499 | ||
1500 | return true; | |
1501 | } | |
1502 | ||
1503 | wxDragResult wxDataViewMainWindow::OnData( wxDataFormat format, wxCoord x, wxCoord y, | |
1504 | wxDragResult def ) | |
1505 | { | |
1506 | int xx = x; | |
1507 | int yy = y; | |
1508 | m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy ); | |
1509 | unsigned int row = GetLineAt( yy ); | |
1510 | ||
1511 | if ((row >= GetRowCount()) || (yy > GetEndOfLastCol())) | |
1512 | return wxDragNone; | |
1513 | ||
1514 | wxDataViewItem item = GetItemByRow( row ); | |
1515 | ||
1516 | wxDataViewModel *model = GetOwner()->GetModel(); | |
1517 | ||
1518 | wxCustomDataObject *obj = (wxCustomDataObject *) GetDropTarget()->GetDataObject(); | |
1519 | ||
1520 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() ); | |
1521 | event.SetEventObject( m_owner ); | |
1522 | event.SetItem( item ); | |
1523 | event.SetModel( model ); | |
1524 | event.SetDataFormat( format ); | |
1525 | event.SetDataSize( obj->GetSize() ); | |
1526 | event.SetDataBuffer( obj->GetData() ); | |
1527 | if (!m_owner->HandleWindowEvent( event )) | |
1528 | return wxDragNone; | |
1529 | ||
1530 | if (!event.IsAllowed()) | |
1531 | return wxDragNone; | |
1532 | ||
1533 | return def; | |
1534 | } | |
1535 | ||
1536 | void wxDataViewMainWindow::OnLeave() | |
1537 | { | |
1538 | RemoveDropHint(); | |
1539 | } | |
1540 | ||
1541 | wxBitmap wxDataViewMainWindow::CreateItemBitmap( unsigned int row, int &indent ) | |
1542 | { | |
1543 | int height = GetLineHeight( row ); | |
1544 | int width = 0; | |
1545 | unsigned int cols = GetOwner()->GetColumnCount(); | |
1546 | unsigned int col; | |
1547 | for (col = 0; col < cols; col++) | |
1548 | { | |
1549 | wxDataViewColumn *column = GetOwner()->GetColumnAt(col); | |
1550 | if (column->IsHidden()) | |
1551 | continue; // skip it! | |
1552 | width += column->GetWidth(); | |
1553 | } | |
1554 | ||
1555 | indent = 0; | |
1556 | if (!IsVirtualList()) | |
1557 | { | |
1558 | wxDataViewTreeNode *node = GetTreeNodeByRow(row); | |
1559 | indent = GetOwner()->GetIndent() * node->GetIndentLevel(); | |
1560 | indent = indent + m_lineHeight; | |
1561 | // try to use the m_lineHeight as the expander space | |
1562 | ||
1563 | if(!node->HasChildren()) | |
1564 | delete node; | |
1565 | } | |
1566 | width -= indent; | |
1567 | ||
1568 | wxBitmap bitmap( width, height ); | |
1569 | wxMemoryDC dc( bitmap ); | |
1570 | dc.SetFont( GetFont() ); | |
1571 | dc.SetPen( *wxBLACK_PEN ); | |
1572 | dc.SetBrush( *wxWHITE_BRUSH ); | |
1573 | dc.DrawRectangle( 0,0,width,height ); | |
1574 | ||
1575 | wxDataViewModel *model = m_owner->GetModel(); | |
1576 | ||
1577 | wxDataViewColumn *expander = GetOwner()->GetExpanderColumn(); | |
1578 | if (!expander) | |
1579 | { | |
1580 | // TODO-RTL: last column for RTL support | |
1581 | expander = GetOwner()->GetColumnAt( 0 ); | |
1582 | GetOwner()->SetExpanderColumn(expander); | |
1583 | } | |
1584 | ||
1585 | ||
1586 | int x = 0; | |
1587 | for (col = 0; col < cols; col++) | |
1588 | { | |
1589 | wxDataViewColumn *column = GetOwner()->GetColumnAt( col ); | |
1590 | wxDataViewRenderer *cell = column->GetRenderer(); | |
1591 | ||
1592 | if (column->IsHidden()) | |
1593 | continue; // skip it! | |
1594 | ||
1595 | width = column->GetWidth(); | |
1596 | ||
1597 | if (column == expander) | |
1598 | width -= indent; | |
1599 | ||
1600 | wxVariant value; | |
1601 | wxDataViewItem item = GetItemByRow( row ); | |
1602 | model->GetValue( value, item, column->GetModelColumn()); | |
1603 | cell->SetValue( value ); | |
1604 | ||
1605 | if (cell->GetWantsAttr()) | |
1606 | { | |
1607 | wxDataViewItemAttr attr; | |
1608 | bool ret = model->GetAttr( item, column->GetModelColumn(), attr ); | |
1609 | if (ret) | |
1610 | cell->SetAttr( attr ); | |
1611 | cell->SetHasAttr( ret ); | |
1612 | } | |
1613 | ||
1614 | wxSize size = cell->GetSize(); | |
1615 | size.x = wxMin( 2*PADDING_RIGHTLEFT + size.x, width ); | |
1616 | size.y = height; | |
1617 | wxRect item_rect(x, 0, size.x, size.y); | |
1618 | ||
1619 | int align = cell->CalculateAlignment(); | |
1620 | // horizontal alignment: | |
1621 | item_rect.x = x; | |
1622 | if (align & wxALIGN_CENTER_HORIZONTAL) | |
1623 | item_rect.x = x + (width / 2) - (size.x / 2); | |
1624 | else if (align & wxALIGN_RIGHT) | |
1625 | item_rect.x = x + width - size.x; | |
1626 | // else: wxALIGN_LEFT is the default | |
1627 | ||
1628 | // vertical alignment: | |
1629 | item_rect.y = 0; | |
1630 | if (align & wxALIGN_CENTER_VERTICAL) | |
1631 | item_rect.y = (height / 2) - (size.y / 2); | |
1632 | else if (align & wxALIGN_BOTTOM) | |
1633 | item_rect.y = height - size.y; | |
1634 | // else: wxALIGN_TOP is the default | |
1635 | ||
1636 | // add padding | |
1637 | item_rect.x += PADDING_RIGHTLEFT; | |
1638 | item_rect.width = size.x - 2 * PADDING_RIGHTLEFT; | |
1639 | ||
1640 | // dc.SetClippingRegion( item_rect ); | |
1641 | cell->Render( item_rect, &dc, 0 ); | |
1642 | // dc.DestroyClippingRegion(); | |
1643 | ||
1644 | x += width; | |
1645 | } | |
1646 | ||
1647 | return bitmap; | |
1648 | } | |
1649 | ||
1650 | #endif // wxUSE_DRAG_AND_DROP | |
1651 | ||
1652 | ||
1653 | void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
1654 | { | |
1655 | wxDataViewModel *model = GetOwner()->GetModel(); | |
1656 | wxAutoBufferedPaintDC dc( this ); | |
1657 | ||
1658 | #ifdef __WXMSW__ | |
1659 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1660 | dc.SetBrush( wxBrush( GetBackgroundColour()) ); | |
1661 | dc.SetBrush( *wxWHITE_BRUSH ); | |
1662 | wxSize size( GetClientSize() ); | |
1663 | dc.DrawRectangle( 0,0,size.x,size.y ); | |
1664 | #endif | |
1665 | ||
1666 | // prepare the DC | |
1667 | GetOwner()->PrepareDC( dc ); | |
1668 | dc.SetFont( GetFont() ); | |
1669 | ||
1670 | wxRect update = GetUpdateRegion().GetBox(); | |
1671 | m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y ); | |
1672 | ||
1673 | // compute which items needs to be redrawn | |
1674 | unsigned int item_start = GetLineAt( wxMax(0,update.y) ); | |
1675 | unsigned int item_count = | |
1676 | wxMin( (int)( GetLineAt( wxMax(0,update.y+update.height) ) - item_start + 1), | |
1677 | (int)(GetRowCount( ) - item_start)); | |
1678 | unsigned int item_last = item_start + item_count; | |
1679 | // Get the parent of DataViewCtrl | |
1680 | wxWindow *parent = GetParent()->GetParent(); | |
1681 | wxDataViewEvent cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT, parent->GetId()); | |
1682 | cache_event.SetEventObject(GetParent()); | |
1683 | cache_event.SetCache(item_start, item_last - 1); | |
1684 | parent->ProcessWindowEvent(cache_event); | |
1685 | ||
1686 | // compute which columns needs to be redrawn | |
1687 | unsigned int cols = GetOwner()->GetColumnCount(); | |
1688 | unsigned int col_start = 0; | |
1689 | unsigned int x_start; | |
1690 | for (x_start = 0; col_start < cols; col_start++) | |
1691 | { | |
1692 | wxDataViewColumn *col = GetOwner()->GetColumnAt(col_start); | |
1693 | if (col->IsHidden()) | |
1694 | continue; // skip it! | |
1695 | ||
1696 | unsigned int w = col->GetWidth(); | |
1697 | if (x_start+w >= (unsigned int)update.x) | |
1698 | break; | |
1699 | ||
1700 | x_start += w; | |
1701 | } | |
1702 | ||
1703 | unsigned int col_last = col_start; | |
1704 | unsigned int x_last = x_start; | |
1705 | for (; col_last < cols; col_last++) | |
1706 | { | |
1707 | wxDataViewColumn *col = GetOwner()->GetColumnAt(col_last); | |
1708 | if (col->IsHidden()) | |
1709 | continue; // skip it! | |
1710 | ||
1711 | if (x_last > (unsigned int)update.GetRight()) | |
1712 | break; | |
1713 | ||
1714 | x_last += col->GetWidth(); | |
1715 | } | |
1716 | ||
1717 | // Draw horizontal rules if required | |
1718 | if ( m_owner->HasFlag(wxDV_HORIZ_RULES) ) | |
1719 | { | |
1720 | dc.SetPen(m_penRule); | |
1721 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
1722 | ||
1723 | for (unsigned int i = item_start; i <= item_last; i++) | |
1724 | { | |
1725 | int y = GetLineStart( i ); | |
1726 | dc.DrawLine(x_start, y, x_last, y); | |
1727 | } | |
1728 | } | |
1729 | ||
1730 | // Draw vertical rules if required | |
1731 | if ( m_owner->HasFlag(wxDV_VERT_RULES) ) | |
1732 | { | |
1733 | dc.SetPen(m_penRule); | |
1734 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
1735 | ||
1736 | int x = x_start; | |
1737 | for (unsigned int i = col_start; i < col_last; i++) | |
1738 | { | |
1739 | wxDataViewColumn *col = GetOwner()->GetColumnAt(i); | |
1740 | if (col->IsHidden()) | |
1741 | continue; // skip it | |
1742 | ||
1743 | dc.DrawLine(x, GetLineStart( item_start ), | |
1744 | x, GetLineStart( item_last ) ); | |
1745 | ||
1746 | x += col->GetWidth(); | |
1747 | } | |
1748 | ||
1749 | // Draw last vertical rule | |
1750 | dc.DrawLine(x, GetLineStart( item_start ), | |
1751 | x, GetLineStart( item_last ) ); | |
1752 | } | |
1753 | ||
1754 | // redraw the background for the items which are selected/current | |
1755 | for (unsigned int item = item_start; item < item_last; item++) | |
1756 | { | |
1757 | bool selected = m_selection.Index( item ) != wxNOT_FOUND; | |
1758 | if (selected || item == m_currentRow) | |
1759 | { | |
1760 | int flags = selected ? (int)wxCONTROL_SELECTED : 0; | |
1761 | if (item == m_currentRow) | |
1762 | flags |= wxCONTROL_CURRENT; | |
1763 | if (m_hasFocus) | |
1764 | flags |= wxCONTROL_FOCUSED; | |
1765 | ||
1766 | wxRect rect( x_start, GetLineStart( item ), x_last, GetLineHeight( item ) ); | |
1767 | wxRendererNative::Get().DrawItemSelectionRect | |
1768 | ( | |
1769 | this, | |
1770 | dc, | |
1771 | rect, | |
1772 | flags | |
1773 | ); | |
1774 | } | |
1775 | } | |
1776 | ||
1777 | #if wxUSE_DRAG_AND_DROP | |
1778 | if (m_dropHint) | |
1779 | { | |
1780 | wxRect rect( x_start, GetLineStart( m_dropHintLine ), | |
1781 | x_last, GetLineHeight( m_dropHintLine ) ); | |
1782 | dc.SetPen( *wxBLACK_PEN ); | |
1783 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
1784 | dc.DrawRectangle( rect ); | |
1785 | } | |
1786 | #endif // wxUSE_DRAG_AND_DROP | |
1787 | ||
1788 | wxDataViewColumn *expander = GetOwner()->GetExpanderColumn(); | |
1789 | if (!expander) | |
1790 | { | |
1791 | // TODO-RTL: last column for RTL support | |
1792 | expander = GetOwner()->GetColumnAt( 0 ); | |
1793 | GetOwner()->SetExpanderColumn(expander); | |
1794 | } | |
1795 | ||
1796 | // redraw all cells for all rows which must be repainted and all columns | |
1797 | wxRect cell_rect; | |
1798 | cell_rect.x = x_start; | |
1799 | for (unsigned int i = col_start; i < col_last; i++) | |
1800 | { | |
1801 | wxDataViewColumn *col = GetOwner()->GetColumnAt( i ); | |
1802 | wxDataViewRenderer *cell = col->GetRenderer(); | |
1803 | cell_rect.width = col->GetWidth(); | |
1804 | ||
1805 | if (col->IsHidden()) | |
1806 | continue; // skip it! | |
1807 | ||
1808 | for (unsigned int item = item_start; item < item_last; item++) | |
1809 | { | |
1810 | // get the cell value and set it into the renderer | |
1811 | wxVariant value; | |
1812 | wxDataViewTreeNode *node = NULL; | |
1813 | wxDataViewItem dataitem; | |
1814 | ||
1815 | if (!IsVirtualList()) | |
1816 | { | |
1817 | node = GetTreeNodeByRow(item); | |
1818 | if( node == NULL ) | |
1819 | continue; | |
1820 | ||
1821 | dataitem = node->GetItem(); | |
1822 | ||
1823 | if ((i > 0) && model->IsContainer(dataitem) && | |
1824 | !model->HasContainerColumns(dataitem)) | |
1825 | continue; | |
1826 | } | |
1827 | else | |
1828 | { | |
1829 | dataitem = wxDataViewItem( wxUIntToPtr(item+1) ); | |
1830 | } | |
1831 | ||
1832 | model->GetValue( value, dataitem, col->GetModelColumn()); | |
1833 | cell->SetValue( value ); | |
1834 | ||
1835 | if (cell->GetWantsAttr()) | |
1836 | { | |
1837 | wxDataViewItemAttr attr; | |
1838 | bool ret = model->GetAttr( dataitem, col->GetModelColumn(), attr ); | |
1839 | if (ret) | |
1840 | cell->SetAttr( attr ); | |
1841 | cell->SetHasAttr( ret ); | |
1842 | } | |
1843 | ||
1844 | // update cell_rect | |
1845 | cell_rect.y = GetLineStart( item ); | |
1846 | cell_rect.height = GetLineHeight( item ); | |
1847 | ||
1848 | // Draw the expander here. | |
1849 | int indent = 0; | |
1850 | if ((!IsVirtualList()) && (col == expander)) | |
1851 | { | |
1852 | indent = node->GetIndentLevel(); | |
1853 | ||
1854 | // Calculate the indent first | |
1855 | indent = cell_rect.x + GetOwner()->GetIndent() * indent; | |
1856 | ||
1857 | int expander_width = m_lineHeight - 2*EXPANDER_MARGIN; | |
1858 | ||
1859 | // change the cell_rect.x to the appropriate pos | |
1860 | int expander_x = indent + EXPANDER_MARGIN; | |
1861 | int expander_y = cell_rect.y + EXPANDER_MARGIN + (GetLineHeight(item) / 2) | |
1862 | - (expander_width/2) - EXPANDER_OFFSET; | |
1863 | ||
1864 | indent = indent + m_lineHeight; | |
1865 | // try to use the m_lineHeight as the expander space | |
1866 | ||
1867 | dc.SetPen( m_penExpander ); | |
1868 | dc.SetBrush( wxNullBrush ); | |
1869 | if( node->HasChildren() ) | |
1870 | { | |
1871 | wxRect rect( expander_x , expander_y, expander_width, expander_width); | |
1872 | int flag = 0; | |
1873 | if (m_underMouse == node) | |
1874 | { | |
1875 | flag |= wxCONTROL_CURRENT; | |
1876 | } | |
1877 | if( node->IsOpen() ) | |
1878 | wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, | |
1879 | flag|wxCONTROL_EXPANDED ); | |
1880 | else | |
1881 | wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag); | |
1882 | } | |
1883 | ||
1884 | // force the expander column to left-center align | |
1885 | cell->SetAlignment( wxALIGN_CENTER_VERTICAL ); | |
1886 | } | |
1887 | if (node && !node->HasChildren()) | |
1888 | { | |
1889 | // Yes, if the node does not have any child, it must be a leaf which | |
1890 | // mean that it is a temporarily created by GetTreeNodeByRow | |
1891 | wxDELETE(node); | |
1892 | } | |
1893 | ||
1894 | // cannot be bigger than allocated space | |
1895 | wxSize size = cell->GetSize(); | |
1896 | ||
1897 | // Because of the tree structure indent, here we should minus the width | |
1898 | // of the cell for drawing | |
1899 | size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width - indent ); | |
1900 | // size.y = wxMin( size.y, cell_rect.height ); | |
1901 | size.y = cell_rect.height; | |
1902 | ||
1903 | wxRect item_rect(cell_rect.GetTopLeft(), size); | |
1904 | int align = cell->CalculateAlignment(); | |
1905 | ||
1906 | // horizontal alignment: | |
1907 | item_rect.x = cell_rect.x; | |
1908 | if (align & wxALIGN_CENTER_HORIZONTAL) | |
1909 | item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2); | |
1910 | else if (align & wxALIGN_RIGHT) | |
1911 | item_rect.x = cell_rect.x + cell_rect.width - size.x; | |
1912 | // else: wxALIGN_LEFT is the default | |
1913 | ||
1914 | // vertical alignment: | |
1915 | item_rect.y = cell_rect.y; | |
1916 | if (align & wxALIGN_CENTER_VERTICAL) | |
1917 | item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2); | |
1918 | else if (align & wxALIGN_BOTTOM) | |
1919 | item_rect.y = cell_rect.y + cell_rect.height - size.y; | |
1920 | // else: wxALIGN_TOP is the default | |
1921 | ||
1922 | // add padding | |
1923 | item_rect.x += PADDING_RIGHTLEFT; | |
1924 | item_rect.width = size.x - 2 * PADDING_RIGHTLEFT; | |
1925 | ||
1926 | // Here we add the tree indent | |
1927 | item_rect.x += indent; | |
1928 | ||
1929 | int state = 0; | |
1930 | if (m_hasFocus && (m_selection.Index(item) != wxNOT_FOUND)) | |
1931 | state |= wxDATAVIEW_CELL_SELECTED; | |
1932 | ||
1933 | // TODO: it would be much more efficient to create a clipping | |
1934 | // region for the entire column being rendered (in the OnPaint | |
1935 | // of wxDataViewMainWindow) instead of a single clip region for | |
1936 | // each cell. However it would mean that each renderer should | |
1937 | // respect the given wxRect's top & bottom coords, eventually | |
1938 | // violating only the left & right coords - however the user can | |
1939 | // make its own renderer and thus we cannot be sure of that. | |
1940 | dc.SetClippingRegion( item_rect ); | |
1941 | cell->Render( item_rect, &dc, state ); | |
1942 | dc.DestroyClippingRegion(); | |
1943 | } | |
1944 | ||
1945 | cell_rect.x += cell_rect.width; | |
1946 | } | |
1947 | } | |
1948 | ||
1949 | void wxDataViewMainWindow::OnRenameTimer() | |
1950 | { | |
1951 | // We have to call this here because changes may just have | |
1952 | // been made and no screen update taken place. | |
1953 | if ( m_dirty ) | |
1954 | { | |
1955 | // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead | |
1956 | // (needs to be tested!) | |
1957 | wxSafeYield(); | |
1958 | } | |
1959 | ||
1960 | wxDataViewItem item = GetItemByRow( m_currentRow ); | |
1961 | ||
1962 | wxRect labelRect = GetItemRect(item, m_currentCol); | |
1963 | ||
1964 | m_currentCol->GetRenderer()->StartEditing( item, labelRect ); | |
1965 | } | |
1966 | ||
1967 | //----------------------------------------------------------------------------- | |
1968 | // Helper class for do operation on the tree node | |
1969 | //----------------------------------------------------------------------------- | |
1970 | class DoJob | |
1971 | { | |
1972 | public: | |
1973 | DoJob() { } | |
1974 | virtual ~DoJob() { } | |
1975 | ||
1976 | // The return value control how the tree-walker tranverse the tree | |
1977 | // 0: Job done, stop tranverse and return | |
1978 | // 1: Ignore the current node's subtree and continue | |
1979 | // 2: Job not done, continue | |
1980 | enum { OK = 0 , IGR = 1, CONT = 2 }; | |
1981 | virtual int operator() ( wxDataViewTreeNode * node ) = 0; | |
1982 | virtual int operator() ( void * n ) = 0; | |
1983 | }; | |
1984 | ||
1985 | bool Walker( wxDataViewTreeNode * node, DoJob & func ) | |
1986 | { | |
1987 | if( node==NULL ) | |
1988 | return false; | |
1989 | ||
1990 | switch( func( node ) ) | |
1991 | { | |
1992 | case DoJob::OK : | |
1993 | return true; | |
1994 | case DoJob::IGR: | |
1995 | return false; | |
1996 | case DoJob::CONT: | |
1997 | default: | |
1998 | ; | |
1999 | } | |
2000 | ||
2001 | wxDataViewTreeNodes nodes = node->GetNodes(); | |
2002 | wxDataViewTreeLeaves leaves = node->GetChildren(); | |
2003 | ||
2004 | int len_nodes = nodes.GetCount(); | |
2005 | int len = leaves.GetCount(); | |
2006 | int i = 0, nodes_i = 0; | |
2007 | ||
2008 | for(; i < len; i ++ ) | |
2009 | { | |
2010 | void * n = leaves[i]; | |
2011 | if( nodes_i < len_nodes && n == nodes[nodes_i]->GetItem().GetID() ) | |
2012 | { | |
2013 | wxDataViewTreeNode * nd = nodes[nodes_i]; | |
2014 | nodes_i++; | |
2015 | ||
2016 | if( Walker( nd , func ) ) | |
2017 | return true; | |
2018 | ||
2019 | } | |
2020 | else | |
2021 | switch( func( n ) ) | |
2022 | { | |
2023 | case DoJob::OK : | |
2024 | return true; | |
2025 | case DoJob::IGR: | |
2026 | continue; | |
2027 | case DoJob::CONT: | |
2028 | default: | |
2029 | ; | |
2030 | } | |
2031 | } | |
2032 | return false; | |
2033 | } | |
2034 | ||
2035 | bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item) | |
2036 | { | |
2037 | if (IsVirtualList()) | |
2038 | { | |
2039 | wxDataViewVirtualListModel *list_model = | |
2040 | (wxDataViewVirtualListModel*) GetOwner()->GetModel(); | |
2041 | m_count = list_model->GetCount(); | |
2042 | UpdateDisplay(); | |
2043 | return true; | |
2044 | } | |
2045 | ||
2046 | SortPrepare(); | |
2047 | ||
2048 | wxDataViewTreeNode * node; | |
2049 | node = FindNode(parent); | |
2050 | ||
2051 | if( node == NULL ) | |
2052 | return false; | |
2053 | ||
2054 | node->SetHasChildren( true ); | |
2055 | ||
2056 | if( g_model->IsContainer( item ) ) | |
2057 | { | |
2058 | wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node ); | |
2059 | newnode->SetItem(item); | |
2060 | newnode->SetHasChildren( true ); | |
2061 | node->AddNode( newnode); | |
2062 | } | |
2063 | else | |
2064 | node->AddLeaf( item.GetID() ); | |
2065 | ||
2066 | node->ChangeSubTreeCount(1); | |
2067 | ||
2068 | m_count = -1; | |
2069 | UpdateDisplay(); | |
2070 | ||
2071 | return true; | |
2072 | } | |
2073 | ||
2074 | static void DestroyTreeHelper( wxDataViewTreeNode * node); | |
2075 | ||
2076 | bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent, | |
2077 | const wxDataViewItem& item) | |
2078 | { | |
2079 | if (IsVirtualList()) | |
2080 | { | |
2081 | wxDataViewVirtualListModel *list_model = | |
2082 | (wxDataViewVirtualListModel*) GetOwner()->GetModel(); | |
2083 | m_count = list_model->GetCount(); | |
2084 | ||
2085 | if( m_currentRow > GetRowCount() ) | |
2086 | m_currentRow = m_count - 1; | |
2087 | ||
2088 | // TODO: why empty the entire selection? | |
2089 | m_selection.Empty(); | |
2090 | ||
2091 | UpdateDisplay(); | |
2092 | ||
2093 | return true; | |
2094 | } | |
2095 | ||
2096 | wxDataViewTreeNode * node = FindNode(parent); | |
2097 | ||
2098 | wxCHECK_MSG( node != NULL, false, "item not found" ); | |
2099 | wxCHECK_MSG( node->GetChildren().Index( item.GetID() ) != wxNOT_FOUND, | |
2100 | false, "item not found" ); | |
2101 | ||
2102 | int sub = -1; | |
2103 | node->GetChildren().Remove( item.GetID() ); | |
2104 | // Manipolate selection | |
2105 | if( m_selection.GetCount() > 1 ) | |
2106 | { | |
2107 | m_selection.Empty(); | |
2108 | } | |
2109 | bool isContainer = false; | |
2110 | wxDataViewTreeNodes nds = node->GetNodes(); | |
2111 | for (size_t i = 0; i < nds.GetCount(); i ++) | |
2112 | { | |
2113 | if (nds[i]->GetItem() == item) | |
2114 | { | |
2115 | isContainer = true; | |
2116 | break; | |
2117 | } | |
2118 | } | |
2119 | if( isContainer ) | |
2120 | { | |
2121 | wxDataViewTreeNode * n = NULL; | |
2122 | wxDataViewTreeNodes nodes = node->GetNodes(); | |
2123 | int len = nodes.GetCount(); | |
2124 | for( int i = 0; i < len; i ++) | |
2125 | { | |
2126 | if( nodes[i]->GetItem() == item ) | |
2127 | { | |
2128 | n = nodes[i]; | |
2129 | break; | |
2130 | } | |
2131 | } | |
2132 | ||
2133 | wxCHECK_MSG( n != NULL, false, "item not found" ); | |
2134 | ||
2135 | node->GetNodes().Remove( n ); | |
2136 | sub -= n->GetSubTreeCount(); | |
2137 | ::DestroyTreeHelper(n); | |
2138 | } | |
2139 | // Make the row number invalid and get a new valid one when user call GetRowCount | |
2140 | m_count = -1; | |
2141 | node->ChangeSubTreeCount(sub); | |
2142 | ||
2143 | // Change the current row to the last row if the current exceed the max row number | |
2144 | if( m_currentRow > GetRowCount() ) | |
2145 | m_currentRow = m_count - 1; | |
2146 | ||
2147 | UpdateDisplay(); | |
2148 | ||
2149 | return true; | |
2150 | } | |
2151 | ||
2152 | bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item) | |
2153 | { | |
2154 | SortPrepare(); | |
2155 | g_model->Resort(); | |
2156 | ||
2157 | // Send event | |
2158 | wxWindow *parent = GetParent(); | |
2159 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId()); | |
2160 | le.SetEventObject(parent); | |
2161 | le.SetModel(GetOwner()->GetModel()); | |
2162 | le.SetItem(item); | |
2163 | parent->GetEventHandler()->ProcessEvent(le); | |
2164 | ||
2165 | return true; | |
2166 | } | |
2167 | ||
2168 | bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int col ) | |
2169 | { | |
2170 | // NOTE: to be valid, we cannot use e.g. INT_MAX - 1 | |
2171 | /*#define MAX_VIRTUAL_WIDTH 100000 | |
2172 | ||
2173 | wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight ); | |
2174 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
2175 | Refresh( true, &rect ); | |
2176 | ||
2177 | return true; | |
2178 | */ | |
2179 | SortPrepare(); | |
2180 | g_model->Resort(); | |
2181 | ||
2182 | // Send event | |
2183 | wxWindow *parent = GetParent(); | |
2184 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId()); | |
2185 | le.SetEventObject(parent); | |
2186 | le.SetModel(GetOwner()->GetModel()); | |
2187 | le.SetItem(item); | |
2188 | le.SetColumn(col); | |
2189 | le.SetDataViewColumn(GetOwner()->GetColumn(col)); | |
2190 | parent->GetEventHandler()->ProcessEvent(le); | |
2191 | ||
2192 | return true; | |
2193 | } | |
2194 | ||
2195 | bool wxDataViewMainWindow::Cleared() | |
2196 | { | |
2197 | DestroyTree(); | |
2198 | ||
2199 | SortPrepare(); | |
2200 | BuildTree( GetOwner()->GetModel() ); | |
2201 | ||
2202 | UpdateDisplay(); | |
2203 | ||
2204 | return true; | |
2205 | } | |
2206 | ||
2207 | void wxDataViewMainWindow::UpdateDisplay() | |
2208 | { | |
2209 | m_dirty = true; | |
2210 | } | |
2211 | ||
2212 | void wxDataViewMainWindow::OnInternalIdle() | |
2213 | { | |
2214 | wxWindow::OnInternalIdle(); | |
2215 | ||
2216 | if (m_dirty) | |
2217 | { | |
2218 | RecalculateDisplay(); | |
2219 | m_dirty = false; | |
2220 | } | |
2221 | } | |
2222 | ||
2223 | void wxDataViewMainWindow::RecalculateDisplay() | |
2224 | { | |
2225 | wxDataViewModel *model = GetOwner()->GetModel(); | |
2226 | if (!model) | |
2227 | { | |
2228 | Refresh(); | |
2229 | return; | |
2230 | } | |
2231 | ||
2232 | int width = GetEndOfLastCol(); | |
2233 | int height = GetLineStart( GetRowCount() ); | |
2234 | ||
2235 | SetVirtualSize( width, height ); | |
2236 | GetOwner()->SetScrollRate( 10, m_lineHeight ); | |
2237 | ||
2238 | Refresh(); | |
2239 | } | |
2240 | ||
2241 | void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
2242 | { | |
2243 | wxWindow::ScrollWindow( dx, dy, rect ); | |
2244 | ||
2245 | if (GetOwner()->m_headerArea) | |
2246 | GetOwner()->m_headerArea->ScrollWindow( dx, 0 ); | |
2247 | } | |
2248 | ||
2249 | void wxDataViewMainWindow::ScrollTo( int rows, int column ) | |
2250 | { | |
2251 | int x, y; | |
2252 | m_owner->GetScrollPixelsPerUnit( &x, &y ); | |
2253 | int sy = GetLineStart( rows )/y; | |
2254 | int sx = 0; | |
2255 | if( column != -1 ) | |
2256 | { | |
2257 | wxRect rect = GetClientRect(); | |
2258 | int colnum = 0; | |
2259 | int x_start, w = 0; | |
2260 | int xx, yy, xe; | |
2261 | m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy ); | |
2262 | for (x_start = 0; colnum < column; colnum++) | |
2263 | { | |
2264 | wxDataViewColumn *col = GetOwner()->GetColumnAt(colnum); | |
2265 | if (col->IsHidden()) | |
2266 | continue; // skip it! | |
2267 | ||
2268 | w = col->GetWidth(); | |
2269 | x_start += w; | |
2270 | } | |
2271 | ||
2272 | int x_end = x_start + w; | |
2273 | xe = xx + rect.width; | |
2274 | if( x_end > xe ) | |
2275 | { | |
2276 | sx = ( xx + x_end - xe )/x; | |
2277 | } | |
2278 | if( x_start < xx ) | |
2279 | { | |
2280 | sx = x_start/x; | |
2281 | } | |
2282 | } | |
2283 | m_owner->Scroll( sx, sy ); | |
2284 | } | |
2285 | ||
2286 | int wxDataViewMainWindow::GetCountPerPage() const | |
2287 | { | |
2288 | wxSize size = GetClientSize(); | |
2289 | return size.y / m_lineHeight; | |
2290 | } | |
2291 | ||
2292 | int wxDataViewMainWindow::GetEndOfLastCol() const | |
2293 | { | |
2294 | int width = 0; | |
2295 | unsigned int i; | |
2296 | for (i = 0; i < GetOwner()->GetColumnCount(); i++) | |
2297 | { | |
2298 | const wxDataViewColumn *c = | |
2299 | const_cast<wxDataViewCtrl*>(GetOwner())->GetColumnAt( i ); | |
2300 | ||
2301 | if (!c->IsHidden()) | |
2302 | width += c->GetWidth(); | |
2303 | } | |
2304 | return width; | |
2305 | } | |
2306 | ||
2307 | unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const | |
2308 | { | |
2309 | int x = 0; | |
2310 | int y = 0; | |
2311 | m_owner->CalcUnscrolledPosition( x, y, &x, &y ); | |
2312 | ||
2313 | return GetLineAt( y ); | |
2314 | } | |
2315 | ||
2316 | unsigned int wxDataViewMainWindow::GetLastVisibleRow() | |
2317 | { | |
2318 | wxSize client_size = GetClientSize(); | |
2319 | m_owner->CalcUnscrolledPosition( client_size.x, client_size.y, | |
2320 | &client_size.x, &client_size.y ); | |
2321 | ||
2322 | // we should deal with the pixel here | |
2323 | unsigned int row = GetLineAt(client_size.y) - 1; | |
2324 | ||
2325 | return wxMin( GetRowCount()-1, row ); | |
2326 | } | |
2327 | ||
2328 | unsigned int wxDataViewMainWindow::GetRowCount() | |
2329 | { | |
2330 | if ( m_count == -1 ) | |
2331 | { | |
2332 | m_count = RecalculateCount(); | |
2333 | UpdateDisplay(); | |
2334 | } | |
2335 | return m_count; | |
2336 | } | |
2337 | ||
2338 | void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row ) | |
2339 | { | |
2340 | m_currentRow = row; | |
2341 | ||
2342 | // send event | |
2343 | } | |
2344 | ||
2345 | void wxDataViewMainWindow::SelectAllRows( bool on ) | |
2346 | { | |
2347 | if (IsEmpty()) | |
2348 | return; | |
2349 | ||
2350 | if (on) | |
2351 | { | |
2352 | m_selection.Clear(); | |
2353 | for (unsigned int i = 0; i < GetRowCount(); i++) | |
2354 | m_selection.Add( i ); | |
2355 | Refresh(); | |
2356 | } | |
2357 | else | |
2358 | { | |
2359 | unsigned int first_visible = GetFirstVisibleRow(); | |
2360 | unsigned int last_visible = GetLastVisibleRow(); | |
2361 | unsigned int i; | |
2362 | for (i = 0; i < m_selection.GetCount(); i++) | |
2363 | { | |
2364 | unsigned int row = m_selection[i]; | |
2365 | if ((row >= first_visible) && (row <= last_visible)) | |
2366 | RefreshRow( row ); | |
2367 | } | |
2368 | m_selection.Clear(); | |
2369 | } | |
2370 | } | |
2371 | ||
2372 | void wxDataViewMainWindow::SelectRow( unsigned int row, bool on ) | |
2373 | { | |
2374 | if (m_selection.Index( row ) == wxNOT_FOUND) | |
2375 | { | |
2376 | if (on) | |
2377 | { | |
2378 | m_selection.Add( row ); | |
2379 | RefreshRow( row ); | |
2380 | } | |
2381 | } | |
2382 | else | |
2383 | { | |
2384 | if (!on) | |
2385 | { | |
2386 | m_selection.Remove( row ); | |
2387 | RefreshRow( row ); | |
2388 | } | |
2389 | } | |
2390 | } | |
2391 | ||
2392 | void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on ) | |
2393 | { | |
2394 | if (from > to) | |
2395 | { | |
2396 | unsigned int tmp = from; | |
2397 | from = to; | |
2398 | to = tmp; | |
2399 | } | |
2400 | ||
2401 | unsigned int i; | |
2402 | for (i = from; i <= to; i++) | |
2403 | { | |
2404 | if (m_selection.Index( i ) == wxNOT_FOUND) | |
2405 | { | |
2406 | if (on) | |
2407 | m_selection.Add( i ); | |
2408 | } | |
2409 | else | |
2410 | { | |
2411 | if (!on) | |
2412 | m_selection.Remove( i ); | |
2413 | } | |
2414 | } | |
2415 | RefreshRows( from, to ); | |
2416 | } | |
2417 | ||
2418 | void wxDataViewMainWindow::Select( const wxArrayInt& aSelections ) | |
2419 | { | |
2420 | for (size_t i=0; i < aSelections.GetCount(); i++) | |
2421 | { | |
2422 | int n = aSelections[i]; | |
2423 | ||
2424 | m_selection.Add( n ); | |
2425 | RefreshRow( n ); | |
2426 | } | |
2427 | } | |
2428 | ||
2429 | void wxDataViewMainWindow::ReverseRowSelection( unsigned int row ) | |
2430 | { | |
2431 | if (m_selection.Index( row ) == wxNOT_FOUND) | |
2432 | m_selection.Add( row ); | |
2433 | else | |
2434 | m_selection.Remove( row ); | |
2435 | RefreshRow( row ); | |
2436 | } | |
2437 | ||
2438 | bool wxDataViewMainWindow::IsRowSelected( unsigned int row ) | |
2439 | { | |
2440 | return (m_selection.Index( row ) != wxNOT_FOUND); | |
2441 | } | |
2442 | ||
2443 | void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem& item) | |
2444 | { | |
2445 | wxWindow *parent = GetParent(); | |
2446 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, parent->GetId()); | |
2447 | ||
2448 | le.SetEventObject(parent); | |
2449 | le.SetModel(GetOwner()->GetModel()); | |
2450 | le.SetItem( item ); | |
2451 | ||
2452 | parent->GetEventHandler()->ProcessEvent(le); | |
2453 | } | |
2454 | ||
2455 | void wxDataViewMainWindow::RefreshRow( unsigned int row ) | |
2456 | { | |
2457 | wxRect rect( 0, GetLineStart( row ), GetEndOfLastCol(), GetLineHeight( row ) ); | |
2458 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
2459 | ||
2460 | wxSize client_size = GetClientSize(); | |
2461 | wxRect client_rect( 0, 0, client_size.x, client_size.y ); | |
2462 | wxRect intersect_rect = client_rect.Intersect( rect ); | |
2463 | if (intersect_rect.width > 0) | |
2464 | Refresh( true, &intersect_rect ); | |
2465 | } | |
2466 | ||
2467 | void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to ) | |
2468 | { | |
2469 | if (from > to) | |
2470 | { | |
2471 | unsigned int tmp = to; | |
2472 | to = from; | |
2473 | from = tmp; | |
2474 | } | |
2475 | ||
2476 | wxRect rect( 0, GetLineStart( from ), GetEndOfLastCol(), GetLineStart( (to-from+1) ) ); | |
2477 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); | |
2478 | ||
2479 | wxSize client_size = GetClientSize(); | |
2480 | wxRect client_rect( 0, 0, client_size.x, client_size.y ); | |
2481 | wxRect intersect_rect = client_rect.Intersect( rect ); | |
2482 | if (intersect_rect.width > 0) | |
2483 | Refresh( true, &intersect_rect ); | |
2484 | } | |
2485 | ||
2486 | void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow ) | |
2487 | { | |
2488 | wxSize client_size = GetClientSize(); | |
2489 | int start = GetLineStart( firstRow ); | |
2490 | m_owner->CalcScrolledPosition( start, 0, &start, NULL ); | |
2491 | if (start > client_size.y) return; | |
2492 | ||
2493 | wxRect rect( 0, start, client_size.x, client_size.y - start ); | |
2494 | ||
2495 | Refresh( true, &rect ); | |
2496 | } | |
2497 | ||
2498 | void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event) | |
2499 | { | |
2500 | wxCHECK_RET( newCurrent < GetRowCount(), | |
2501 | wxT("invalid item index in OnArrowChar()") ); | |
2502 | ||
2503 | // if there is no selection, we cannot move it anywhere | |
2504 | if (!HasCurrentRow()) | |
2505 | return; | |
2506 | ||
2507 | unsigned int oldCurrent = m_currentRow; | |
2508 | ||
2509 | // in single selection we just ignore Shift as we can't select several | |
2510 | // items anyhow | |
2511 | if ( event.ShiftDown() && !IsSingleSel() ) | |
2512 | { | |
2513 | RefreshRow( oldCurrent ); | |
2514 | ||
2515 | ChangeCurrentRow( newCurrent ); | |
2516 | ||
2517 | // select all the items between the old and the new one | |
2518 | if ( oldCurrent > newCurrent ) | |
2519 | { | |
2520 | newCurrent = oldCurrent; | |
2521 | oldCurrent = m_currentRow; | |
2522 | } | |
2523 | ||
2524 | SelectRows( oldCurrent, newCurrent, true ); | |
2525 | if (oldCurrent!=newCurrent) | |
2526 | SendSelectionChangedEvent(GetItemByRow(m_selection[0])); | |
2527 | } | |
2528 | else // !shift | |
2529 | { | |
2530 | RefreshRow( oldCurrent ); | |
2531 | ||
2532 | // all previously selected items are unselected unless ctrl is held | |
2533 | if ( !event.ControlDown() ) | |
2534 | SelectAllRows(false); | |
2535 | ||
2536 | ChangeCurrentRow( newCurrent ); | |
2537 | ||
2538 | if ( !event.ControlDown() ) | |
2539 | { | |
2540 | SelectRow( m_currentRow, true ); | |
2541 | SendSelectionChangedEvent(GetItemByRow(m_currentRow)); | |
2542 | } | |
2543 | else | |
2544 | RefreshRow( m_currentRow ); | |
2545 | } | |
2546 | ||
2547 | GetOwner()->EnsureVisible( m_currentRow, -1 ); | |
2548 | } | |
2549 | ||
2550 | wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const | |
2551 | { | |
2552 | wxRect rect; | |
2553 | rect.x = 0; | |
2554 | rect.y = GetLineStart( row ); | |
2555 | rect.width = GetEndOfLastCol(); | |
2556 | rect.height = GetLineHeight( row ); | |
2557 | ||
2558 | return rect; | |
2559 | } | |
2560 | ||
2561 | int wxDataViewMainWindow::GetLineStart( unsigned int row ) const | |
2562 | { | |
2563 | const wxDataViewModel *model = GetOwner()->GetModel(); | |
2564 | ||
2565 | if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) | |
2566 | { | |
2567 | // TODO make more efficient | |
2568 | ||
2569 | int start = 0; | |
2570 | ||
2571 | unsigned int r; | |
2572 | for (r = 0; r < row; r++) | |
2573 | { | |
2574 | const wxDataViewTreeNode* node = GetTreeNodeByRow(r); | |
2575 | if (!node) return start; | |
2576 | ||
2577 | wxDataViewItem item = node->GetItem(); | |
2578 | ||
2579 | if (node && !node->HasChildren()) | |
2580 | { | |
2581 | // Yes, if the node does not have any child, it must be a leaf which | |
2582 | // mean that it is a temporarily created by GetTreeNodeByRow | |
2583 | wxDELETE(node); | |
2584 | } | |
2585 | ||
2586 | unsigned int cols = GetOwner()->GetColumnCount(); | |
2587 | unsigned int col; | |
2588 | int height = m_lineHeight; | |
2589 | for (col = 0; col < cols; col++) | |
2590 | { | |
2591 | const wxDataViewColumn *column = GetOwner()->GetColumn(col); | |
2592 | if (column->IsHidden()) | |
2593 | continue; // skip it! | |
2594 | ||
2595 | if ((col != 0) && | |
2596 | model->IsContainer(item) && | |
2597 | !model->HasContainerColumns(item)) | |
2598 | continue; // skip it! | |
2599 | ||
2600 | wxVariant value; | |
2601 | model->GetValue( value, item, column->GetModelColumn() ); | |
2602 | ||
2603 | wxDataViewRenderer *renderer = | |
2604 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); | |
2605 | renderer->SetValue( value ); | |
2606 | height = wxMax( height, renderer->GetSize().y ); | |
2607 | } | |
2608 | ||
2609 | start += height; | |
2610 | } | |
2611 | ||
2612 | return start; | |
2613 | } | |
2614 | else | |
2615 | { | |
2616 | return row * m_lineHeight; | |
2617 | } | |
2618 | } | |
2619 | ||
2620 | int wxDataViewMainWindow::GetLineAt( unsigned int y ) const | |
2621 | { | |
2622 | const wxDataViewModel *model = GetOwner()->GetModel(); | |
2623 | ||
2624 | // check for the easy case first | |
2625 | if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) ) | |
2626 | return y / m_lineHeight; | |
2627 | ||
2628 | // TODO make more efficient | |
2629 | unsigned int row = 0; | |
2630 | unsigned int yy = 0; | |
2631 | for (;;) | |
2632 | { | |
2633 | const wxDataViewTreeNode* node = GetTreeNodeByRow(row); | |
2634 | if (!node) | |
2635 | { | |
2636 | // not really correct... | |
2637 | return row + ((y-yy) / m_lineHeight); | |
2638 | } | |
2639 | ||
2640 | wxDataViewItem item = node->GetItem(); | |
2641 | ||
2642 | if (node && !node->HasChildren()) | |
2643 | { | |
2644 | // Yes, if the node does not have any child, it must be a leaf which | |
2645 | // mean that it is a temporarily created by GetTreeNodeByRow | |
2646 | wxDELETE(node); | |
2647 | } | |
2648 | ||
2649 | unsigned int cols = GetOwner()->GetColumnCount(); | |
2650 | unsigned int col; | |
2651 | int height = m_lineHeight; | |
2652 | for (col = 0; col < cols; col++) | |
2653 | { | |
2654 | const wxDataViewColumn *column = GetOwner()->GetColumn(col); | |
2655 | if (column->IsHidden()) | |
2656 | continue; // skip it! | |
2657 | ||
2658 | if ((col != 0) && | |
2659 | model->IsContainer(item) && | |
2660 | !model->HasContainerColumns(item)) | |
2661 | continue; // skip it! | |
2662 | ||
2663 | wxVariant value; | |
2664 | model->GetValue( value, item, column->GetModelColumn() ); | |
2665 | ||
2666 | wxDataViewRenderer *renderer = | |
2667 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); | |
2668 | renderer->SetValue( value ); | |
2669 | height = wxMax( height, renderer->GetSize().y ); | |
2670 | } | |
2671 | ||
2672 | yy += height; | |
2673 | if (y < yy) | |
2674 | return row; | |
2675 | ||
2676 | row++; | |
2677 | } | |
2678 | } | |
2679 | ||
2680 | int wxDataViewMainWindow::GetLineHeight( unsigned int row ) const | |
2681 | { | |
2682 | const wxDataViewModel *model = GetOwner()->GetModel(); | |
2683 | ||
2684 | if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) | |
2685 | { | |
2686 | wxASSERT( !IsVirtualList() ); | |
2687 | ||
2688 | const wxDataViewTreeNode* node = GetTreeNodeByRow(row); | |
2689 | // wxASSERT( node ); | |
2690 | if (!node) return m_lineHeight; | |
2691 | ||
2692 | wxDataViewItem item = node->GetItem(); | |
2693 | ||
2694 | if (node && !node->HasChildren()) | |
2695 | { | |
2696 | // Yes, if the node does not have any child, it must be a leaf which | |
2697 | // mean that it is a temporarily created by GetTreeNodeByRow | |
2698 | wxDELETE(node); | |
2699 | } | |
2700 | ||
2701 | int height = m_lineHeight; | |
2702 | ||
2703 | unsigned int cols = GetOwner()->GetColumnCount(); | |
2704 | unsigned int col; | |
2705 | for (col = 0; col < cols; col++) | |
2706 | { | |
2707 | const wxDataViewColumn *column = GetOwner()->GetColumn(col); | |
2708 | if (column->IsHidden()) | |
2709 | continue; // skip it! | |
2710 | ||
2711 | if ((col != 0) && | |
2712 | model->IsContainer(item) && | |
2713 | !model->HasContainerColumns(item)) | |
2714 | continue; // skip it! | |
2715 | ||
2716 | wxVariant value; | |
2717 | model->GetValue( value, item, column->GetModelColumn() ); | |
2718 | ||
2719 | wxDataViewRenderer *renderer = | |
2720 | const_cast<wxDataViewRenderer*>(column->GetRenderer()); | |
2721 | renderer->SetValue( value ); | |
2722 | height = wxMax( height, renderer->GetSize().y ); | |
2723 | } | |
2724 | ||
2725 | return height; | |
2726 | } | |
2727 | else | |
2728 | { | |
2729 | return m_lineHeight; | |
2730 | } | |
2731 | } | |
2732 | ||
2733 | class RowToItemJob: public DoJob | |
2734 | { | |
2735 | public: | |
2736 | RowToItemJob( unsigned int row , int current ) | |
2737 | { this->row = row; this->current = current; } | |
2738 | virtual ~RowToItemJob() {} | |
2739 | ||
2740 | virtual int operator() ( wxDataViewTreeNode * node ) | |
2741 | { | |
2742 | current ++; | |
2743 | if( current == static_cast<int>(row)) | |
2744 | { | |
2745 | ret = node->GetItem(); | |
2746 | return DoJob::OK; | |
2747 | } | |
2748 | ||
2749 | if( node->GetSubTreeCount() + current < static_cast<int>(row) ) | |
2750 | { | |
2751 | current += node->GetSubTreeCount(); | |
2752 | return DoJob::IGR; | |
2753 | } | |
2754 | else | |
2755 | { | |
2756 | // If the current has no child node, we can find the desired item of the row | |
2757 | // number directly. | |
2758 | // This if can speed up finding in some case, and will has a very good effect | |
2759 | // when it comes to list view | |
2760 | if( node->GetNodes().GetCount() == 0) | |
2761 | { | |
2762 | int index = static_cast<int>(row) - current - 1; | |
2763 | ret = node->GetChildren().Item( index ); | |
2764 | return DoJob::OK; | |
2765 | } | |
2766 | return DoJob::CONT; | |
2767 | } | |
2768 | } | |
2769 | ||
2770 | virtual int operator() ( void * n ) | |
2771 | { | |
2772 | current ++; | |
2773 | if( current == static_cast<int>(row)) | |
2774 | { | |
2775 | ret = wxDataViewItem( n ); | |
2776 | return DoJob::OK; | |
2777 | } | |
2778 | return DoJob::CONT; | |
2779 | } | |
2780 | ||
2781 | wxDataViewItem GetResult() const | |
2782 | { return ret; } | |
2783 | ||
2784 | private: | |
2785 | unsigned int row; | |
2786 | int current; | |
2787 | wxDataViewItem ret; | |
2788 | }; | |
2789 | ||
2790 | wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const | |
2791 | { | |
2792 | if (IsVirtualList()) | |
2793 | { | |
2794 | return wxDataViewItem( wxUIntToPtr(row+1) ); | |
2795 | } | |
2796 | else | |
2797 | { | |
2798 | RowToItemJob job( row, -2 ); | |
2799 | Walker( m_root , job ); | |
2800 | return job.GetResult(); | |
2801 | } | |
2802 | } | |
2803 | ||
2804 | class RowToTreeNodeJob: public DoJob | |
2805 | { | |
2806 | public: | |
2807 | RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node ) | |
2808 | { | |
2809 | this->row = row; | |
2810 | this->current = current; | |
2811 | ret = NULL; | |
2812 | parent = node; | |
2813 | } | |
2814 | virtual ~RowToTreeNodeJob(){ } | |
2815 | ||
2816 | virtual int operator() ( wxDataViewTreeNode * node ) | |
2817 | { | |
2818 | current ++; | |
2819 | if( current == static_cast<int>(row)) | |
2820 | { | |
2821 | ret = node; | |
2822 | return DoJob::OK; | |
2823 | } | |
2824 | ||
2825 | if( node->GetSubTreeCount() + current < static_cast<int>(row) ) | |
2826 | { | |
2827 | current += node->GetSubTreeCount(); | |
2828 | return DoJob::IGR; | |
2829 | } | |
2830 | else | |
2831 | { | |
2832 | parent = node; | |
2833 | ||
2834 | // If the current node has no children, we can find the desired item of the | |
2835 | // row number directly. | |
2836 | // This if can speed up finding in some case, and will have a very good | |
2837 | // effect for list views. | |
2838 | if( node->GetNodes().GetCount() == 0) | |
2839 | { | |
2840 | int index = static_cast<int>(row) - current - 1; | |
2841 | void * n = node->GetChildren().Item( index ); | |
2842 | ret = new wxDataViewTreeNode( parent ); | |
2843 | ret->SetItem( wxDataViewItem( n )); | |
2844 | ret->SetHasChildren(false); | |
2845 | return DoJob::OK; | |
2846 | } | |
2847 | return DoJob::CONT; | |
2848 | } | |
2849 | } | |
2850 | ||
2851 | virtual int operator() ( void * n ) | |
2852 | { | |
2853 | current ++; | |
2854 | if( current == static_cast<int>(row)) | |
2855 | { | |
2856 | ret = new wxDataViewTreeNode( parent ); | |
2857 | ret->SetItem( wxDataViewItem( n )); | |
2858 | ret->SetHasChildren(false); | |
2859 | return DoJob::OK; | |
2860 | } | |
2861 | ||
2862 | return DoJob::CONT; | |
2863 | } | |
2864 | ||
2865 | wxDataViewTreeNode * GetResult() const | |
2866 | { return ret; } | |
2867 | ||
2868 | private: | |
2869 | unsigned int row; | |
2870 | int current; | |
2871 | wxDataViewTreeNode * ret; | |
2872 | wxDataViewTreeNode * parent; | |
2873 | }; | |
2874 | ||
2875 | wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) const | |
2876 | { | |
2877 | wxASSERT( !IsVirtualList() ); | |
2878 | ||
2879 | RowToTreeNodeJob job( row , -2, m_root ); | |
2880 | Walker( m_root , job ); | |
2881 | return job.GetResult(); | |
2882 | } | |
2883 | ||
2884 | wxDataViewEvent wxDataViewMainWindow::SendExpanderEvent( wxEventType type, | |
2885 | const wxDataViewItem & item ) | |
2886 | { | |
2887 | wxWindow *parent = GetParent(); | |
2888 | wxDataViewEvent le(type, parent->GetId()); | |
2889 | ||
2890 | le.SetEventObject(parent); | |
2891 | le.SetModel(GetOwner()->GetModel()); | |
2892 | le.SetItem( item ); | |
2893 | ||
2894 | parent->GetEventHandler()->ProcessEvent(le); | |
2895 | return le; | |
2896 | } | |
2897 | ||
2898 | bool wxDataViewMainWindow::IsExpanded( unsigned int row ) const | |
2899 | { | |
2900 | if (IsVirtualList()) | |
2901 | return false; | |
2902 | ||
2903 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); | |
2904 | if (!node) | |
2905 | return false; | |
2906 | ||
2907 | if (!node->HasChildren()) | |
2908 | { | |
2909 | delete node; | |
2910 | return false; | |
2911 | } | |
2912 | ||
2913 | return node->IsOpen(); | |
2914 | } | |
2915 | ||
2916 | bool wxDataViewMainWindow::HasChildren( unsigned int row ) const | |
2917 | { | |
2918 | if (IsVirtualList()) | |
2919 | return false; | |
2920 | ||
2921 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); | |
2922 | if (!node) | |
2923 | return false; | |
2924 | ||
2925 | if (!node->HasChildren()) | |
2926 | { | |
2927 | delete node; | |
2928 | return false; | |
2929 | } | |
2930 | ||
2931 | return true; | |
2932 | } | |
2933 | ||
2934 | void wxDataViewMainWindow::Expand( unsigned int row ) | |
2935 | { | |
2936 | if (IsVirtualList()) | |
2937 | return; | |
2938 | ||
2939 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); | |
2940 | if (!node) | |
2941 | return; | |
2942 | ||
2943 | if (!node->HasChildren()) | |
2944 | { | |
2945 | delete node; | |
2946 | return; | |
2947 | } | |
2948 | ||
2949 | if (!node->IsOpen()) | |
2950 | { | |
2951 | wxDataViewEvent e = | |
2952 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, node->GetItem()); | |
2953 | ||
2954 | // Check if the user prevent expanding | |
2955 | if( e.GetSkipped() ) | |
2956 | return; | |
2957 | ||
2958 | node->ToggleOpen(); | |
2959 | ||
2960 | // build the children of current node | |
2961 | if( node->GetChildrenNumber() == 0 ) | |
2962 | { | |
2963 | SortPrepare(); | |
2964 | ::BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node); | |
2965 | } | |
2966 | ||
2967 | // By expanding the node all row indices that are currently in the selection list | |
2968 | // and are greater than our node have become invalid. So we have to correct that now. | |
2969 | const unsigned rowAdjustment = node->GetSubTreeCount(); | |
2970 | for(unsigned i=0; i<m_selection.size(); ++i) | |
2971 | { | |
2972 | const unsigned testRow = m_selection[i]; | |
2973 | // all rows above us are not affected, so skip them | |
2974 | if(testRow <= row) | |
2975 | continue; | |
2976 | ||
2977 | m_selection[i] += rowAdjustment; | |
2978 | } | |
2979 | ||
2980 | if(m_currentRow > row) | |
2981 | ChangeCurrentRow(m_currentRow + rowAdjustment); | |
2982 | ||
2983 | m_count = -1; | |
2984 | UpdateDisplay(); | |
2985 | // Send the expanded event | |
2986 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem()); | |
2987 | } | |
2988 | } | |
2989 | ||
2990 | void wxDataViewMainWindow::Collapse(unsigned int row) | |
2991 | { | |
2992 | if (IsVirtualList()) | |
2993 | return; | |
2994 | ||
2995 | wxDataViewTreeNode *node = GetTreeNodeByRow(row); | |
2996 | if (!node) | |
2997 | return; | |
2998 | ||
2999 | if (!node->HasChildren()) | |
3000 | { | |
3001 | delete node; | |
3002 | return; | |
3003 | } | |
3004 | ||
3005 | if (node->IsOpen()) | |
3006 | { | |
3007 | wxDataViewEvent e = | |
3008 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem()); | |
3009 | if( e.GetSkipped() ) | |
3010 | return; | |
3011 | ||
3012 | // Find out if there are selected items below the current node. | |
3013 | bool selectCollapsingRow = false; | |
3014 | const unsigned rowAdjustment = node->GetSubTreeCount(); | |
3015 | unsigned maxRowToBeTested = row + rowAdjustment; | |
3016 | for(unsigned i=0; i<m_selection.size(); ++i) | |
3017 | { | |
3018 | const unsigned testRow = m_selection[i]; | |
3019 | if(testRow > row && testRow <= maxRowToBeTested) | |
3020 | { | |
3021 | selectCollapsingRow = true; | |
3022 | // get out as soon as we have found a node that is selected | |
3023 | break; | |
3024 | } | |
3025 | } | |
3026 | ||
3027 | node->ToggleOpen(); | |
3028 | ||
3029 | // If the node to be closed has selected items the user won't see those any longer. | |
3030 | // We select the collapsing node in this case. | |
3031 | if(selectCollapsingRow) | |
3032 | { | |
3033 | SelectAllRows(false); | |
3034 | ChangeCurrentRow(row); | |
3035 | SelectRow(row, true); | |
3036 | SendSelectionChangedEvent(GetItemByRow(row)); | |
3037 | } | |
3038 | else | |
3039 | { | |
3040 | // if there were no selected items below our node we still need to "fix" the | |
3041 | // selection list to adjust for the changing of the row indices. | |
3042 | // We actually do the opposite of what we are doing in Expand(). | |
3043 | for(unsigned i=0; i<m_selection.size(); ++i) | |
3044 | { | |
3045 | const unsigned testRow = m_selection[i]; | |
3046 | // all rows above us are not affected, so skip them | |
3047 | if(testRow <= row) | |
3048 | continue; | |
3049 | ||
3050 | m_selection[i] -= rowAdjustment; | |
3051 | } | |
3052 | ||
3053 | // if the "current row" is being collapsed away we change it to the current row ;-) | |
3054 | if(m_currentRow > row && m_currentRow <= maxRowToBeTested) | |
3055 | ChangeCurrentRow(row); | |
3056 | else if(m_currentRow > row) | |
3057 | ChangeCurrentRow(m_currentRow - rowAdjustment); | |
3058 | } | |
3059 | ||
3060 | m_count = -1; | |
3061 | UpdateDisplay(); | |
3062 | SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,node->GetItem()); | |
3063 | } | |
3064 | } | |
3065 | ||
3066 | wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item ) | |
3067 | { | |
3068 | wxDataViewModel * model = GetOwner()->GetModel(); | |
3069 | if( model == NULL ) | |
3070 | return NULL; | |
3071 | ||
3072 | if (!item.IsOk()) | |
3073 | return m_root; | |
3074 | ||
3075 | // Compose the a parent-chain of the finding item | |
3076 | ItemList list; | |
3077 | list.DeleteContents( true ); | |
3078 | wxDataViewItem it( item ); | |
3079 | while( it.IsOk() ) | |
3080 | { | |
3081 | wxDataViewItem * pItem = new wxDataViewItem( it ); | |
3082 | list.Insert( pItem ); | |
3083 | it = model->GetParent( it ); | |
3084 | } | |
3085 | ||
3086 | // Find the item along the parent-chain. | |
3087 | // This algorithm is designed to speed up the node-finding method | |
3088 | wxDataViewTreeNode * node = m_root; | |
3089 | for( ItemList::const_iterator iter = list.begin(); iter !=list.end(); iter++ ) | |
3090 | { | |
3091 | if( node->HasChildren() ) | |
3092 | { | |
3093 | if( node->GetChildrenNumber() == 0 ) | |
3094 | { | |
3095 | SortPrepare(); | |
3096 | ::BuildTreeHelper(model, node->GetItem(), node); | |
3097 | } | |
3098 | ||
3099 | wxDataViewTreeNodes nodes = node->GetNodes(); | |
3100 | unsigned int i; | |
3101 | bool found = false; | |
3102 | ||
3103 | for (i = 0; i < nodes.GetCount(); i ++) | |
3104 | { | |
3105 | if (nodes[i]->GetItem() == (**iter)) | |
3106 | { | |
3107 | if (nodes[i]->GetItem() == item) | |
3108 | return nodes[i]; | |
3109 | ||
3110 | node = nodes[i]; | |
3111 | found = true; | |
3112 | break; | |
3113 | } | |
3114 | } | |
3115 | if (!found) | |
3116 | return NULL; | |
3117 | } | |
3118 | else | |
3119 | return NULL; | |
3120 | } | |
3121 | return NULL; | |
3122 | } | |
3123 | ||
3124 | void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item, | |
3125 | wxDataViewColumn* &column ) | |
3126 | { | |
3127 | wxDataViewColumn *col = NULL; | |
3128 | unsigned int cols = GetOwner()->GetColumnCount(); | |
3129 | unsigned int colnum = 0; | |
3130 | int x, y; | |
3131 | m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y ); | |
3132 | for (unsigned x_start = 0; colnum < cols; colnum++) | |
3133 | { | |
3134 | col = GetOwner()->GetColumnAt(colnum); | |
3135 | if (col->IsHidden()) | |
3136 | continue; // skip it! | |
3137 | ||
3138 | unsigned int w = col->GetWidth(); | |
3139 | if (x_start+w >= (unsigned int)x) | |
3140 | break; | |
3141 | ||
3142 | x_start += w; | |
3143 | } | |
3144 | ||
3145 | column = col; | |
3146 | item = GetItemByRow( GetLineAt( y ) ); | |
3147 | } | |
3148 | ||
3149 | wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item, | |
3150 | const wxDataViewColumn* column ) | |
3151 | { | |
3152 | int xpos = 0; | |
3153 | int width = 0; | |
3154 | ||
3155 | unsigned int cols = GetOwner()->GetColumnCount(); | |
3156 | // If column is null the loop will compute the combined width of all columns. | |
3157 | // Otherwise, it will compute the x position of the column we are looking for. | |
3158 | for (unsigned int i = 0; i < cols; i++) | |
3159 | { | |
3160 | wxDataViewColumn* col = GetOwner()->GetColumnAt( i ); | |
3161 | ||
3162 | if (col == column) | |
3163 | break; | |
3164 | ||
3165 | if (col->IsHidden()) | |
3166 | continue; // skip it! | |
3167 | ||
3168 | xpos += col->GetWidth(); | |
3169 | width += col->GetWidth(); | |
3170 | } | |
3171 | ||
3172 | if(column != 0) | |
3173 | { | |
3174 | // If we have a column, we need can get its width directly. | |
3175 | if(column->IsHidden()) | |
3176 | width = 0; | |
3177 | else | |
3178 | width = column->GetWidth(); | |
3179 | ||
3180 | } | |
3181 | else | |
3182 | { | |
3183 | // If we have no column, we reset the x position back to zero. | |
3184 | xpos = 0; | |
3185 | } | |
3186 | ||
3187 | // we have to take an expander column into account and compute its indentation | |
3188 | // to get the correct x position where the actual text is | |
3189 | int indent = 0; | |
3190 | int row = GetRowByItem(item); | |
3191 | if (!IsVirtualList() && (column == 0 || GetOwner()->GetExpanderColumn() == column) ) | |
3192 | { | |
3193 | wxDataViewTreeNode* node = GetTreeNodeByRow(row); | |
3194 | indent = GetOwner()->GetIndent() * node->GetIndentLevel(); | |
3195 | indent = indent + m_lineHeight; // use m_lineHeight as the width of the expander | |
3196 | ||
3197 | if(!node->HasChildren()) | |
3198 | delete node; | |
3199 | } | |
3200 | ||
3201 | wxRect itemRect( xpos + indent, | |
3202 | GetLineStart( row ), | |
3203 | width - indent, | |
3204 | GetLineHeight( row ) ); | |
3205 | ||
3206 | GetOwner()->CalcScrolledPosition( itemRect.x, itemRect.y, | |
3207 | &itemRect.x, &itemRect.y ); | |
3208 | ||
3209 | return itemRect; | |
3210 | } | |
3211 | ||
3212 | int wxDataViewMainWindow::RecalculateCount() | |
3213 | { | |
3214 | if (IsVirtualList()) | |
3215 | { | |
3216 | wxDataViewVirtualListModel *list_model = | |
3217 | (wxDataViewVirtualListModel*) GetOwner()->GetModel(); | |
3218 | ||
3219 | return list_model->GetCount(); | |
3220 | } | |
3221 | else | |
3222 | { | |
3223 | return m_root->GetSubTreeCount(); | |
3224 | } | |
3225 | } | |
3226 | ||
3227 | class ItemToRowJob : public DoJob | |
3228 | { | |
3229 | public: | |
3230 | ItemToRowJob(const wxDataViewItem& item_, ItemList::const_iterator iter) | |
3231 | : m_iter(iter), | |
3232 | item(item_) | |
3233 | { | |
3234 | ret = -1; | |
3235 | } | |
3236 | ||
3237 | // Maybe binary search will help to speed up this process | |
3238 | virtual int operator() ( wxDataViewTreeNode * node) | |
3239 | { | |
3240 | ret ++; | |
3241 | if( node->GetItem() == item ) | |
3242 | { | |
3243 | return DoJob::OK; | |
3244 | } | |
3245 | ||
3246 | if( node->GetItem() == **m_iter ) | |
3247 | { | |
3248 | m_iter++; | |
3249 | return DoJob::CONT; | |
3250 | } | |
3251 | else | |
3252 | { | |
3253 | ret += node->GetSubTreeCount(); | |
3254 | return DoJob::IGR; | |
3255 | } | |
3256 | ||
3257 | } | |
3258 | ||
3259 | virtual int operator() ( void * n ) | |
3260 | { | |
3261 | ret ++; | |
3262 | if( n == item.GetID() ) | |
3263 | return DoJob::OK; | |
3264 | return DoJob::CONT; | |
3265 | } | |
3266 | ||
3267 | // the row number is begin from zero | |
3268 | int GetResult() const | |
3269 | { return ret -1; } | |
3270 | ||
3271 | private: | |
3272 | ItemList::const_iterator m_iter; | |
3273 | wxDataViewItem item; | |
3274 | int ret; | |
3275 | ||
3276 | }; | |
3277 | ||
3278 | int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) const | |
3279 | { | |
3280 | const wxDataViewModel * model = GetOwner()->GetModel(); | |
3281 | if( model == NULL ) | |
3282 | return -1; | |
3283 | ||
3284 | if (IsVirtualList()) | |
3285 | { | |
3286 | return wxPtrToUInt( item.GetID() ) -1; | |
3287 | } | |
3288 | else | |
3289 | { | |
3290 | if( !item.IsOk() ) | |
3291 | return -1; | |
3292 | ||
3293 | // Compose the a parent-chain of the finding item | |
3294 | ItemList list; | |
3295 | wxDataViewItem * pItem; | |
3296 | list.DeleteContents( true ); | |
3297 | wxDataViewItem it( item ); | |
3298 | while( it.IsOk() ) | |
3299 | { | |
3300 | pItem = new wxDataViewItem( it ); | |
3301 | list.Insert( pItem ); | |
3302 | it = model->GetParent( it ); | |
3303 | } | |
3304 | pItem = new wxDataViewItem( ); | |
3305 | list.Insert( pItem ); | |
3306 | ||
3307 | ItemToRowJob job( item, list.begin() ); | |
3308 | Walker(m_root , job ); | |
3309 | return job.GetResult(); | |
3310 | } | |
3311 | } | |
3312 | ||
3313 | static void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, | |
3314 | wxDataViewTreeNode * node) | |
3315 | { | |
3316 | if( !model->IsContainer( item ) ) | |
3317 | return; | |
3318 | ||
3319 | wxDataViewItemArray children; | |
3320 | unsigned int num = model->GetChildren( item, children); | |
3321 | ||
3322 | unsigned int index = 0; | |
3323 | while( index < num ) | |
3324 | { | |
3325 | if( model->IsContainer( children[index] ) ) | |
3326 | { | |
3327 | wxDataViewTreeNode * n = new wxDataViewTreeNode( node ); | |
3328 | n->SetItem(children[index]); | |
3329 | n->SetHasChildren( true ); | |
3330 | node->AddNode( n ); | |
3331 | } | |
3332 | else | |
3333 | { | |
3334 | node->AddLeaf( children[index].GetID() ); | |
3335 | } | |
3336 | index ++; | |
3337 | } | |
3338 | node->SetSubTreeCount( num ); | |
3339 | wxDataViewTreeNode * n = node->GetParent(); | |
3340 | if( n != NULL) | |
3341 | n->ChangeSubTreeCount(num); | |
3342 | ||
3343 | } | |
3344 | ||
3345 | void wxDataViewMainWindow::BuildTree(wxDataViewModel * model) | |
3346 | { | |
3347 | DestroyTree(); | |
3348 | ||
3349 | if (GetOwner()->GetModel()->IsVirtualListModel()) | |
3350 | { | |
3351 | m_count = -1; | |
3352 | return; | |
3353 | } | |
3354 | ||
3355 | m_root = new wxDataViewTreeNode( NULL ); | |
3356 | m_root->SetHasChildren(true); | |
3357 | ||
3358 | // First we define a invalid item to fetch the top-level elements | |
3359 | wxDataViewItem item; | |
3360 | SortPrepare(); | |
3361 | BuildTreeHelper( model, item, m_root); | |
3362 | m_count = -1; | |
3363 | } | |
3364 | ||
3365 | static void DestroyTreeHelper( wxDataViewTreeNode * node ) | |
3366 | { | |
3367 | if( node->GetNodeNumber() != 0 ) | |
3368 | { | |
3369 | int len = node->GetNodeNumber(); | |
3370 | wxDataViewTreeNodes& nodes = node->GetNodes(); | |
3371 | for (int i = 0; i < len; i++) | |
3372 | DestroyTreeHelper(nodes[i]); | |
3373 | } | |
3374 | delete node; | |
3375 | } | |
3376 | ||
3377 | void wxDataViewMainWindow::DestroyTree() | |
3378 | { | |
3379 | if (!IsVirtualList()) | |
3380 | { | |
3381 | ::DestroyTreeHelper(m_root); | |
3382 | m_count = 0; | |
3383 | m_root = NULL; | |
3384 | } | |
3385 | } | |
3386 | ||
3387 | void wxDataViewMainWindow::OnChar( wxKeyEvent &event ) | |
3388 | { | |
3389 | if ( GetParent()->HandleAsNavigationKey(event) ) | |
3390 | return; | |
3391 | ||
3392 | // no item -> nothing to do | |
3393 | if (!HasCurrentRow()) | |
3394 | { | |
3395 | event.Skip(); | |
3396 | return; | |
3397 | } | |
3398 | ||
3399 | // don't use m_linesPerPage directly as it might not be computed yet | |
3400 | const int pageSize = GetCountPerPage(); | |
3401 | wxCHECK_RET( pageSize, wxT("should have non zero page size") ); | |
3402 | ||
3403 | switch ( event.GetKeyCode() ) | |
3404 | { | |
3405 | case WXK_RETURN: | |
3406 | { | |
3407 | wxWindow *parent = GetParent(); | |
3408 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, | |
3409 | parent->GetId()); | |
3410 | le.SetItem( GetItemByRow(m_currentRow) ); | |
3411 | le.SetEventObject(parent); | |
3412 | le.SetModel(GetOwner()->GetModel()); | |
3413 | ||
3414 | parent->GetEventHandler()->ProcessEvent(le); | |
3415 | } | |
3416 | break; | |
3417 | ||
3418 | case WXK_UP: | |
3419 | if ( m_currentRow > 0 ) | |
3420 | OnArrowChar( m_currentRow - 1, event ); | |
3421 | break; | |
3422 | ||
3423 | case WXK_DOWN: | |
3424 | if ( m_currentRow < GetRowCount() - 1 ) | |
3425 | OnArrowChar( m_currentRow + 1, event ); | |
3426 | break; | |
3427 | // Add the process for tree expanding/collapsing | |
3428 | case WXK_LEFT: | |
3429 | { | |
3430 | if (IsVirtualList()) | |
3431 | break; | |
3432 | ||
3433 | wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow); | |
3434 | if (!node) | |
3435 | break; | |
3436 | ||
3437 | if (node->HasChildren() && node->IsOpen()) | |
3438 | { | |
3439 | Collapse(m_currentRow); | |
3440 | } | |
3441 | else // if the node is already closed we move the selection to its parent | |
3442 | { | |
3443 | wxDataViewTreeNode *parent_node = node->GetParent(); | |
3444 | ||
3445 | if(!node->HasChildren()) | |
3446 | delete node; | |
3447 | ||
3448 | if (parent_node) | |
3449 | { | |
3450 | int parent = GetRowByItem( parent_node->GetItem() ); | |
3451 | if ( parent >= 0 ) | |
3452 | { | |
3453 | unsigned int row = m_currentRow; | |
3454 | SelectRow( row, false); | |
3455 | SelectRow( parent, true ); | |
3456 | ChangeCurrentRow( parent ); | |
3457 | GetOwner()->EnsureVisible( parent, -1 ); | |
3458 | SendSelectionChangedEvent( parent_node->GetItem() ); | |
3459 | } | |
3460 | } | |
3461 | } | |
3462 | break; | |
3463 | } | |
3464 | case WXK_RIGHT: | |
3465 | { | |
3466 | if (!IsExpanded( m_currentRow )) | |
3467 | Expand( m_currentRow ); | |
3468 | else | |
3469 | { | |
3470 | unsigned int row = m_currentRow; | |
3471 | SelectRow( row, false ); | |
3472 | SelectRow( row + 1, true ); | |
3473 | ChangeCurrentRow( row + 1 ); | |
3474 | GetOwner()->EnsureVisible( row + 1, -1 ); | |
3475 | SendSelectionChangedEvent( GetItemByRow(row+1) ); | |
3476 | } | |
3477 | break; | |
3478 | } | |
3479 | case WXK_END: | |
3480 | { | |
3481 | if (!IsEmpty()) | |
3482 | OnArrowChar( GetRowCount() - 1, event ); | |
3483 | break; | |
3484 | } | |
3485 | case WXK_HOME: | |
3486 | if (!IsEmpty()) | |
3487 | OnArrowChar( 0, event ); | |
3488 | break; | |
3489 | ||
3490 | case WXK_PAGEUP: | |
3491 | { | |
3492 | int steps = pageSize - 1; | |
3493 | int index = m_currentRow - steps; | |
3494 | if (index < 0) | |
3495 | index = 0; | |
3496 | ||
3497 | OnArrowChar( index, event ); | |
3498 | } | |
3499 | break; | |
3500 | ||
3501 | case WXK_PAGEDOWN: | |
3502 | { | |
3503 | int steps = pageSize - 1; | |
3504 | unsigned int index = m_currentRow + steps; | |
3505 | unsigned int count = GetRowCount(); | |
3506 | if ( index >= count ) | |
3507 | index = count - 1; | |
3508 | ||
3509 | OnArrowChar( index, event ); | |
3510 | } | |
3511 | break; | |
3512 | ||
3513 | case WXK_F2: | |
3514 | { | |
3515 | if(m_selection.size() == 1) | |
3516 | { | |
3517 | // TODO: we need to revise that when we have a concept for a 'current column' | |
3518 | GetOwner()->StartEditor(GetItemByRow(m_selection[0]), 0); | |
3519 | } | |
3520 | } | |
3521 | break; | |
3522 | ||
3523 | default: | |
3524 | event.Skip(); | |
3525 | } | |
3526 | } | |
3527 | ||
3528 | void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) | |
3529 | { | |
3530 | if (event.GetEventType() == wxEVT_MOUSEWHEEL) | |
3531 | { | |
3532 | // let the base handle mouse wheel events. | |
3533 | event.Skip(); | |
3534 | return; | |
3535 | } | |
3536 | ||
3537 | int x = event.GetX(); | |
3538 | int y = event.GetY(); | |
3539 | m_owner->CalcUnscrolledPosition( x, y, &x, &y ); | |
3540 | wxDataViewColumn *col = NULL; | |
3541 | ||
3542 | int xpos = 0; | |
3543 | unsigned int cols = GetOwner()->GetColumnCount(); | |
3544 | unsigned int i; | |
3545 | for (i = 0; i < cols; i++) | |
3546 | { | |
3547 | wxDataViewColumn *c = GetOwner()->GetColumnAt( i ); | |
3548 | if (c->IsHidden()) | |
3549 | continue; // skip it! | |
3550 | ||
3551 | if (x < xpos + c->GetWidth()) | |
3552 | { | |
3553 | col = c; | |
3554 | break; | |
3555 | } | |
3556 | xpos += c->GetWidth(); | |
3557 | } | |
3558 | if (!col) | |
3559 | return; | |
3560 | ||
3561 | wxDataViewRenderer *cell = col->GetRenderer(); | |
3562 | unsigned int current = GetLineAt( y ); | |
3563 | if ((current >= GetRowCount()) || (x > GetEndOfLastCol())) | |
3564 | { | |
3565 | // Unselect all if below the last row ? | |
3566 | return; | |
3567 | } | |
3568 | ||
3569 | // Test whether the mouse is hovered on the tree item button | |
3570 | bool hoverOverExpander = false; | |
3571 | if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col)) | |
3572 | { | |
3573 | wxDataViewTreeNode * node = GetTreeNodeByRow(current); | |
3574 | if( node!=NULL && node->HasChildren() ) | |
3575 | { | |
3576 | int indent = node->GetIndentLevel(); | |
3577 | indent = GetOwner()->GetIndent()*indent; | |
3578 | ||
3579 | // we make the rectangle we are looking in a bit bigger than the actual | |
3580 | // visual expander so the user can hit that little thing reliably | |
3581 | wxRect rect( xpos + indent, | |
3582 | GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2, | |
3583 | m_lineHeight, m_lineHeight); | |
3584 | if( rect.Contains(x, y) ) | |
3585 | { | |
3586 | // So the mouse is over the expander | |
3587 | hoverOverExpander = true; | |
3588 | if (m_underMouse && m_underMouse != node) | |
3589 | { | |
3590 | // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem())); | |
3591 | RefreshRow(GetRowByItem(m_underMouse->GetItem())); | |
3592 | } | |
3593 | if (m_underMouse != node) | |
3594 | { | |
3595 | // wxLogMessage("Do the row: %d", current); | |
3596 | RefreshRow(current); | |
3597 | } | |
3598 | m_underMouse = node; | |
3599 | } | |
3600 | } | |
3601 | if (node!=NULL && !node->HasChildren()) | |
3602 | delete node; | |
3603 | } | |
3604 | if (!hoverOverExpander) | |
3605 | { | |
3606 | if (m_underMouse != NULL) | |
3607 | { | |
3608 | // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem())); | |
3609 | RefreshRow(GetRowByItem(m_underMouse->GetItem())); | |
3610 | m_underMouse = NULL; | |
3611 | } | |
3612 | } | |
3613 | ||
3614 | wxDataViewModel *model = GetOwner()->GetModel(); | |
3615 | ||
3616 | #if wxUSE_DRAG_AND_DROP | |
3617 | if (event.Dragging()) | |
3618 | { | |
3619 | if (m_dragCount == 0) | |
3620 | { | |
3621 | // we have to report the raw, physical coords as we want to be | |
3622 | // able to call HitTest(event.m_pointDrag) from the user code to | |
3623 | // get the item being dragged | |
3624 | m_dragStart = event.GetPosition(); | |
3625 | } | |
3626 | ||
3627 | m_dragCount++; | |
3628 | ||
3629 | if (m_dragCount != 3) | |
3630 | return; | |
3631 | ||
3632 | if (event.LeftIsDown()) | |
3633 | { | |
3634 | m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y, | |
3635 | &m_dragStart.x, &m_dragStart.y ); | |
3636 | unsigned int drag_item_row = GetLineAt( m_dragStart.y ); | |
3637 | wxDataViewItem item = GetItemByRow( drag_item_row ); | |
3638 | ||
3639 | // Notify cell about drag | |
3640 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() ); | |
3641 | event.SetEventObject( m_owner ); | |
3642 | event.SetItem( item ); | |
3643 | event.SetModel( model ); | |
3644 | if (!m_owner->HandleWindowEvent( event )) | |
3645 | return; | |
3646 | ||
3647 | if (!event.IsAllowed()) | |
3648 | return; | |
3649 | ||
3650 | wxDataObject *obj = event.GetDataObject(); | |
3651 | if (!obj) | |
3652 | return; | |
3653 | ||
3654 | wxDataViewDropSource drag( this, drag_item_row ); | |
3655 | drag.SetData( *obj ); | |
3656 | /* wxDragResult res = */ drag.DoDragDrop(); | |
3657 | delete obj; | |
3658 | } | |
3659 | return; | |
3660 | } | |
3661 | else | |
3662 | { | |
3663 | m_dragCount = 0; | |
3664 | } | |
3665 | #endif // wxUSE_DRAG_AND_DROP | |
3666 | ||
3667 | bool simulateClick = false; | |
3668 | ||
3669 | if (event.ButtonDClick()) | |
3670 | { | |
3671 | m_renameTimer->Stop(); | |
3672 | m_lastOnSame = false; | |
3673 | } | |
3674 | ||
3675 | wxDataViewItem item = GetItemByRow(current); | |
3676 | bool ignore_other_columns = | |
3677 | ((GetOwner()->GetExpanderColumn() != col) && | |
3678 | (model->IsContainer(item)) && | |
3679 | (!model->HasContainerColumns(item))); | |
3680 | ||
3681 | if (event.LeftDClick()) | |
3682 | { | |
3683 | if(hoverOverExpander) | |
3684 | { | |
3685 | // a double click on the expander will be converted into a "simulated" normal click | |
3686 | simulateClick = true; | |
3687 | } | |
3688 | else if ( current == m_lineLastClicked ) | |
3689 | { | |
3690 | if ((!ignore_other_columns) && (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)) | |
3691 | { | |
3692 | wxVariant value; | |
3693 | model->GetValue( value, item, col->GetModelColumn() ); | |
3694 | cell->SetValue( value ); | |
3695 | wxRect cell_rect( xpos, GetLineStart( current ), | |
3696 | col->GetWidth(), GetLineHeight( current ) ); | |
3697 | cell->Activate( cell_rect, model, item, col->GetModelColumn() ); | |
3698 | } | |
3699 | else | |
3700 | { | |
3701 | wxWindow *parent = GetParent(); | |
3702 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId()); | |
3703 | le.SetItem( item ); | |
3704 | le.SetEventObject(parent); | |
3705 | le.SetModel(GetOwner()->GetModel()); | |
3706 | ||
3707 | parent->GetEventHandler()->ProcessEvent(le); | |
3708 | } | |
3709 | return; | |
3710 | } | |
3711 | else | |
3712 | { | |
3713 | // The first click was on another item, so don't interpret this as | |
3714 | // a double click, but as a simple click instead | |
3715 | simulateClick = true; | |
3716 | } | |
3717 | } | |
3718 | ||
3719 | if (event.LeftUp() && !hoverOverExpander) | |
3720 | { | |
3721 | if (m_lineSelectSingleOnUp != (unsigned int)-1) | |
3722 | { | |
3723 | // select single line | |
3724 | SelectAllRows( false ); | |
3725 | SelectRow( m_lineSelectSingleOnUp, true ); | |
3726 | SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) ); | |
3727 | } | |
3728 | ||
3729 | // If the user click the expander, we do not do editing even if the column | |
3730 | // with expander are editable | |
3731 | if (m_lastOnSame && !ignore_other_columns) | |
3732 | { | |
3733 | if ((col == m_currentCol) && (current == m_currentRow) && | |
3734 | (cell->GetMode() & wxDATAVIEW_CELL_EDITABLE) ) | |
3735 | { | |
3736 | m_renameTimer->Start( 100, true ); | |
3737 | } | |
3738 | } | |
3739 | ||
3740 | m_lastOnSame = false; | |
3741 | m_lineSelectSingleOnUp = (unsigned int)-1; | |
3742 | } | |
3743 | else if(!event.LeftUp()) | |
3744 | { | |
3745 | // This is necessary, because after a DnD operation in | |
3746 | // from and to ourself, the up event is swallowed by the | |
3747 | // DnD code. So on next non-up event (which means here and | |
3748 | // now) m_lineSelectSingleOnUp should be reset. | |
3749 | m_lineSelectSingleOnUp = (unsigned int)-1; | |
3750 | } | |
3751 | ||
3752 | if (event.RightDown()) | |
3753 | { | |
3754 | m_lineBeforeLastClicked = m_lineLastClicked; | |
3755 | m_lineLastClicked = current; | |
3756 | ||
3757 | // If the item is already selected, do not update the selection. | |
3758 | // Multi-selections should not be cleared if a selected item is clicked. | |
3759 | if (!IsRowSelected(current)) | |
3760 | { | |
3761 | SelectAllRows(false); | |
3762 | ChangeCurrentRow(current); | |
3763 | SelectRow(m_currentRow,true); | |
3764 | SendSelectionChangedEvent(GetItemByRow( m_currentRow ) ); | |
3765 | } | |
3766 | } | |
3767 | else if (event.RightUp()) | |
3768 | { | |
3769 | wxVariant value; | |
3770 | model->GetValue( value, item, col->GetModelColumn() ); | |
3771 | wxWindow *parent = GetParent(); | |
3772 | wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId()); | |
3773 | le.SetItem( item ); | |
3774 | le.SetEventObject(parent); | |
3775 | le.SetModel(GetOwner()->GetModel()); | |
3776 | le.SetValue(value); | |
3777 | parent->GetEventHandler()->ProcessEvent(le); | |
3778 | } | |
3779 | else if (event.MiddleDown()) | |
3780 | { | |
3781 | } | |
3782 | ||
3783 | if((event.LeftDown() || simulateClick) && hoverOverExpander) | |
3784 | { | |
3785 | wxDataViewTreeNode* node = GetTreeNodeByRow(current); | |
3786 | ||
3787 | // hoverOverExpander being true tells us that our node must be | |
3788 | // valid and have children. | |
3789 | // So we don't need any extra checks. | |
3790 | if( node->IsOpen() ) | |
3791 | Collapse(current); | |
3792 | else | |
3793 | Expand(current); | |
3794 | } | |
3795 | else if ((event.LeftDown() || simulateClick) && !hoverOverExpander) | |
3796 | { | |
3797 | SetFocus(); | |
3798 | ||
3799 | m_lineBeforeLastClicked = m_lineLastClicked; | |
3800 | m_lineLastClicked = current; | |
3801 | ||
3802 | unsigned int oldCurrentRow = m_currentRow; | |
3803 | bool oldWasSelected = IsRowSelected(m_currentRow); | |
3804 | ||
3805 | bool cmdModifierDown = event.CmdDown(); | |
3806 | if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) ) | |
3807 | { | |
3808 | if ( IsSingleSel() || !IsRowSelected(current) ) | |
3809 | { | |
3810 | SelectAllRows( false ); | |
3811 | ChangeCurrentRow(current); | |
3812 | SelectRow(m_currentRow,true); | |
3813 | SendSelectionChangedEvent(GetItemByRow( m_currentRow ) ); | |
3814 | } | |
3815 | else // multi sel & current is highlighted & no mod keys | |
3816 | { | |
3817 | m_lineSelectSingleOnUp = current; | |
3818 | ChangeCurrentRow(current); // change focus | |
3819 | } | |
3820 | } | |
3821 | else // multi sel & either ctrl or shift is down | |
3822 | { | |
3823 | if (cmdModifierDown) | |
3824 | { | |
3825 | ChangeCurrentRow(current); | |
3826 | ReverseRowSelection(m_currentRow); | |
3827 | SendSelectionChangedEvent(GetItemByRow(m_selection[0]) ); | |
3828 | } | |
3829 | else if (event.ShiftDown()) | |
3830 | { | |
3831 | ChangeCurrentRow(current); | |
3832 | ||
3833 | unsigned int lineFrom = oldCurrentRow, | |
3834 | lineTo = current; | |
3835 | ||
3836 | if ( lineTo < lineFrom ) | |
3837 | { | |
3838 | lineTo = lineFrom; | |
3839 | lineFrom = m_currentRow; | |
3840 | } | |
3841 | ||
3842 | SelectRows(lineFrom, lineTo, true); | |
3843 | SendSelectionChangedEvent(GetItemByRow(m_selection[0]) ); | |
3844 | } | |
3845 | else // !ctrl, !shift | |
3846 | { | |
3847 | // test in the enclosing if should make it impossible | |
3848 | wxFAIL_MSG( wxT("how did we get here?") ); | |
3849 | } | |
3850 | } | |
3851 | ||
3852 | if (m_currentRow != oldCurrentRow) | |
3853 | RefreshRow( oldCurrentRow ); | |
3854 | ||
3855 | wxDataViewColumn *oldCurrentCol = m_currentCol; | |
3856 | ||
3857 | // Update selection here... | |
3858 | m_currentCol = col; | |
3859 | ||
3860 | m_lastOnSame = !simulateClick && ((col == oldCurrentCol) && | |
3861 | (current == oldCurrentRow)) && oldWasSelected; | |
3862 | ||
3863 | // Call LeftClick after everything else as under GTK+ | |
3864 | if (cell->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE) | |
3865 | { | |
3866 | // notify cell about right click | |
3867 | wxVariant value; | |
3868 | model->GetValue( value, item, col->GetModelColumn() ); | |
3869 | cell->SetValue( value ); | |
3870 | wxRect cell_rect( xpos, GetLineStart( current ), | |
3871 | col->GetWidth(), GetLineHeight( current ) ); | |
3872 | /* ignore ret */ cell->LeftClick( event.GetPosition(), cell_rect, | |
3873 | model, item, col->GetModelColumn()); | |
3874 | } | |
3875 | } | |
3876 | } | |
3877 | ||
3878 | void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event ) | |
3879 | { | |
3880 | m_hasFocus = true; | |
3881 | ||
3882 | if (HasCurrentRow()) | |
3883 | Refresh(); | |
3884 | ||
3885 | event.Skip(); | |
3886 | } | |
3887 | ||
3888 | void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event ) | |
3889 | { | |
3890 | m_hasFocus = false; | |
3891 | ||
3892 | if (HasCurrentRow()) | |
3893 | Refresh(); | |
3894 | ||
3895 | event.Skip(); | |
3896 | } | |
3897 | ||
3898 | wxDataViewItem wxDataViewMainWindow::GetSelection() const | |
3899 | { | |
3900 | if( m_selection.GetCount() != 1 ) | |
3901 | return wxDataViewItem(); | |
3902 | ||
3903 | return GetItemByRow( m_selection.Item(0)); | |
3904 | } | |
3905 | ||
3906 | //----------------------------------------------------------------------------- | |
3907 | // wxDataViewCtrl | |
3908 | //----------------------------------------------------------------------------- | |
3909 | ||
3910 | WX_DEFINE_LIST(wxDataViewColumnList) | |
3911 | ||
3912 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase) | |
3913 | BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase) | |
3914 | EVT_SIZE(wxDataViewCtrl::OnSize) | |
3915 | END_EVENT_TABLE() | |
3916 | ||
3917 | wxDataViewCtrl::~wxDataViewCtrl() | |
3918 | { | |
3919 | if (m_notifier) | |
3920 | GetModel()->RemoveNotifier( m_notifier ); | |
3921 | ||
3922 | m_cols.Clear(); | |
3923 | } | |
3924 | ||
3925 | void wxDataViewCtrl::Init() | |
3926 | { | |
3927 | m_cols.DeleteContents(true); | |
3928 | m_notifier = NULL; | |
3929 | ||
3930 | // No sorting column at start | |
3931 | m_sortingColumnIdx = wxNOT_FOUND; | |
3932 | ||
3933 | m_headerArea = NULL; | |
3934 | } | |
3935 | ||
3936 | bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id, | |
3937 | const wxPoint& pos, const wxSize& size, | |
3938 | long style, const wxValidator& validator ) | |
3939 | { | |
3940 | // if ( (style & wxBORDER_MASK) == 0) | |
3941 | // style |= wxBORDER_SUNKEN; | |
3942 | ||
3943 | Init(); | |
3944 | ||
3945 | if (!wxControl::Create( parent, id, pos, size, | |
3946 | style | wxScrolledWindowStyle, validator)) | |
3947 | return false; | |
3948 | ||
3949 | SetInitialSize(size); | |
3950 | ||
3951 | #ifdef __WXMAC__ | |
3952 | MacSetClipChildren( true ); | |
3953 | #endif | |
3954 | ||
3955 | m_clientArea = new wxDataViewMainWindow( this, wxID_ANY ); | |
3956 | ||
3957 | if (HasFlag(wxDV_NO_HEADER)) | |
3958 | m_headerArea = NULL; | |
3959 | else | |
3960 | m_headerArea = new wxDataViewHeaderWindow(this); | |
3961 | ||
3962 | SetTargetWindow( m_clientArea ); | |
3963 | ||
3964 | wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL ); | |
3965 | if (m_headerArea) | |
3966 | sizer->Add( m_headerArea, 0, wxGROW ); | |
3967 | sizer->Add( m_clientArea, 1, wxGROW ); | |
3968 | SetSizer( sizer ); | |
3969 | ||
3970 | return true; | |
3971 | } | |
3972 | ||
3973 | wxBorder wxDataViewCtrl::GetDefaultBorder() const | |
3974 | { | |
3975 | return wxBORDER_THEME; | |
3976 | } | |
3977 | ||
3978 | #ifdef __WXMSW__ | |
3979 | WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg, | |
3980 | WXWPARAM wParam, | |
3981 | WXLPARAM lParam) | |
3982 | { | |
3983 | WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam); | |
3984 | ||
3985 | #ifndef __WXWINCE__ | |
3986 | // we need to process arrows ourselves for scrolling | |
3987 | if ( nMsg == WM_GETDLGCODE ) | |
3988 | { | |
3989 | rc |= DLGC_WANTARROWS; | |
3990 | } | |
3991 | #endif | |
3992 | ||
3993 | return rc; | |
3994 | } | |
3995 | #endif | |
3996 | ||
3997 | wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size) | |
3998 | { | |
3999 | wxSize newsize = size; | |
4000 | if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea)) | |
4001 | newsize.y -= m_headerArea->GetSize().y; | |
4002 | ||
4003 | return newsize; | |
4004 | } | |
4005 | ||
4006 | void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
4007 | { | |
4008 | // We need to override OnSize so that our scrolled | |
4009 | // window a) does call Layout() to use sizers for | |
4010 | // positioning the controls but b) does not query | |
4011 | // the sizer for their size and use that for setting | |
4012 | // the scrollable area as set that ourselves by | |
4013 | // calling SetScrollbar() further down. | |
4014 | ||
4015 | Layout(); | |
4016 | ||
4017 | AdjustScrollbars(); | |
4018 | } | |
4019 | ||
4020 | void wxDataViewCtrl::SetFocus() | |
4021 | { | |
4022 | if (m_clientArea) | |
4023 | m_clientArea->SetFocus(); | |
4024 | } | |
4025 | ||
4026 | bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model ) | |
4027 | { | |
4028 | if (!wxDataViewCtrlBase::AssociateModel( model )) | |
4029 | return false; | |
4030 | ||
4031 | m_notifier = new wxGenericDataViewModelNotifier( m_clientArea ); | |
4032 | ||
4033 | model->AddNotifier( m_notifier ); | |
4034 | ||
4035 | m_clientArea->DestroyTree(); | |
4036 | ||
4037 | m_clientArea->BuildTree(model); | |
4038 | ||
4039 | m_clientArea->UpdateDisplay(); | |
4040 | ||
4041 | return true; | |
4042 | } | |
4043 | ||
4044 | #if wxUSE_DRAG_AND_DROP | |
4045 | ||
4046 | bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format ) | |
4047 | { | |
4048 | return m_clientArea->EnableDragSource( format ); | |
4049 | } | |
4050 | ||
4051 | bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format ) | |
4052 | { | |
4053 | return m_clientArea->EnableDropTarget( format ); | |
4054 | } | |
4055 | ||
4056 | #endif // wxUSE_DRAG_AND_DROP | |
4057 | ||
4058 | bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col ) | |
4059 | { | |
4060 | if (!wxDataViewCtrlBase::AppendColumn(col)) | |
4061 | return false; | |
4062 | ||
4063 | m_cols.Append( col ); | |
4064 | OnColumnsCountChanged(); | |
4065 | return true; | |
4066 | } | |
4067 | ||
4068 | bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col ) | |
4069 | { | |
4070 | if (!wxDataViewCtrlBase::PrependColumn(col)) | |
4071 | return false; | |
4072 | ||
4073 | m_cols.Insert( col ); | |
4074 | OnColumnsCountChanged(); | |
4075 | return true; | |
4076 | } | |
4077 | ||
4078 | bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col ) | |
4079 | { | |
4080 | if (!wxDataViewCtrlBase::InsertColumn(pos,col)) | |
4081 | return false; | |
4082 | ||
4083 | m_cols.Insert( pos, col ); | |
4084 | OnColumnsCountChanged(); | |
4085 | return true; | |
4086 | } | |
4087 | ||
4088 | void wxDataViewCtrl::OnColumnChange(unsigned int idx) | |
4089 | { | |
4090 | if ( m_headerArea ) | |
4091 | m_headerArea->UpdateColumn(idx); | |
4092 | ||
4093 | m_clientArea->UpdateDisplay(); | |
4094 | } | |
4095 | ||
4096 | void wxDataViewCtrl::OnColumnsCountChanged() | |
4097 | { | |
4098 | if (m_headerArea) | |
4099 | m_headerArea->SetColumnCount(GetColumnCount()); | |
4100 | ||
4101 | m_clientArea->UpdateDisplay(); | |
4102 | } | |
4103 | ||
4104 | void wxDataViewCtrl::DoSetExpanderColumn() | |
4105 | { | |
4106 | m_clientArea->UpdateDisplay(); | |
4107 | } | |
4108 | ||
4109 | void wxDataViewCtrl::DoSetIndent() | |
4110 | { | |
4111 | m_clientArea->UpdateDisplay(); | |
4112 | } | |
4113 | ||
4114 | unsigned int wxDataViewCtrl::GetColumnCount() const | |
4115 | { | |
4116 | return m_cols.GetCount(); | |
4117 | } | |
4118 | ||
4119 | wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const | |
4120 | { | |
4121 | return m_cols[idx]; | |
4122 | } | |
4123 | ||
4124 | wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const | |
4125 | { | |
4126 | // columns can't be reordered if there is no header window which allows | |
4127 | // to do this | |
4128 | const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos] | |
4129 | : pos; | |
4130 | ||
4131 | return GetColumn(idx); | |
4132 | } | |
4133 | ||
4134 | int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const | |
4135 | { | |
4136 | const unsigned count = m_cols.size(); | |
4137 | for ( unsigned n = 0; n < count; n++ ) | |
4138 | { | |
4139 | if ( m_cols[n] == column ) | |
4140 | return n; | |
4141 | } | |
4142 | ||
4143 | return wxNOT_FOUND; | |
4144 | } | |
4145 | ||
4146 | void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col), | |
4147 | unsigned int WXUNUSED(new_pos)) | |
4148 | { | |
4149 | // do _not_ reorder m_cols elements here, they should always be in the | |
4150 | // order in which columns were added, we only display the columns in | |
4151 | // different order | |
4152 | m_clientArea->UpdateDisplay(); | |
4153 | } | |
4154 | ||
4155 | bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column ) | |
4156 | { | |
4157 | wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column ); | |
4158 | if (!ret) | |
4159 | return false; | |
4160 | ||
4161 | m_cols.Erase(ret); | |
4162 | OnColumnsCountChanged(); | |
4163 | ||
4164 | return true; | |
4165 | } | |
4166 | ||
4167 | bool wxDataViewCtrl::ClearColumns() | |
4168 | { | |
4169 | m_cols.Clear(); | |
4170 | OnColumnsCountChanged(); | |
4171 | return true; | |
4172 | } | |
4173 | ||
4174 | int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const | |
4175 | { | |
4176 | int ret = 0, | |
4177 | dummy = 0; | |
4178 | unsigned int len = GetColumnCount(); | |
4179 | for ( unsigned int i = 0; i < len; i++ ) | |
4180 | { | |
4181 | wxDataViewColumn * col = GetColumnAt(i); | |
4182 | if (col->IsHidden()) | |
4183 | continue; | |
4184 | ret += col->GetWidth(); | |
4185 | if (column==col) | |
4186 | { | |
4187 | CalcScrolledPosition( ret, dummy, &ret, &dummy ); | |
4188 | break; | |
4189 | } | |
4190 | } | |
4191 | return ret; | |
4192 | } | |
4193 | ||
4194 | wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const | |
4195 | { | |
4196 | return m_sortingColumnIdx == wxNOT_FOUND ? NULL | |
4197 | : GetColumn(m_sortingColumnIdx); | |
4198 | } | |
4199 | ||
4200 | // Selection code with wxDataViewItem as parameters | |
4201 | wxDataViewItem wxDataViewCtrl::GetSelection() const | |
4202 | { | |
4203 | return m_clientArea->GetSelection(); | |
4204 | } | |
4205 | ||
4206 | int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const | |
4207 | { | |
4208 | sel.Empty(); | |
4209 | wxDataViewSelection selection = m_clientArea->GetSelections(); | |
4210 | int len = selection.GetCount(); | |
4211 | for( int i = 0; i < len; i ++) | |
4212 | { | |
4213 | unsigned int row = selection[i]; | |
4214 | sel.Add( m_clientArea->GetItemByRow( row ) ); | |
4215 | } | |
4216 | return len; | |
4217 | } | |
4218 | ||
4219 | void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel ) | |
4220 | { | |
4221 | wxDataViewSelection selection(wxDataViewSelectionCmp); | |
4222 | ||
4223 | wxDataViewItem last_parent; | |
4224 | ||
4225 | int len = sel.GetCount(); | |
4226 | for( int i = 0; i < len; i ++ ) | |
4227 | { | |
4228 | wxDataViewItem item = sel[i]; | |
4229 | wxDataViewItem parent = GetModel()->GetParent( item ); | |
4230 | if (parent) | |
4231 | { | |
4232 | if (parent != last_parent) | |
4233 | ExpandAncestors(item); | |
4234 | } | |
4235 | ||
4236 | last_parent = parent; | |
4237 | int row = m_clientArea->GetRowByItem( item ); | |
4238 | if( row >= 0 ) | |
4239 | selection.Add( static_cast<unsigned int>(row) ); | |
4240 | } | |
4241 | ||
4242 | m_clientArea->SetSelections( selection ); | |
4243 | } | |
4244 | ||
4245 | void wxDataViewCtrl::Select( const wxDataViewItem & item ) | |
4246 | { | |
4247 | ExpandAncestors( item ); | |
4248 | ||
4249 | int row = m_clientArea->GetRowByItem( item ); | |
4250 | if( row >= 0 ) | |
4251 | { | |
4252 | // Unselect all rows before select another in the single select mode | |
4253 | if (m_clientArea->IsSingleSel()) | |
4254 | m_clientArea->SelectAllRows(false); | |
4255 | m_clientArea->SelectRow(row, true); | |
4256 | } | |
4257 | } | |
4258 | ||
4259 | void wxDataViewCtrl::Unselect( const wxDataViewItem & item ) | |
4260 | { | |
4261 | int row = m_clientArea->GetRowByItem( item ); | |
4262 | if( row >= 0 ) | |
4263 | m_clientArea->SelectRow(row, false); | |
4264 | } | |
4265 | ||
4266 | bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const | |
4267 | { | |
4268 | int row = m_clientArea->GetRowByItem( item ); | |
4269 | if( row >= 0 ) | |
4270 | { | |
4271 | return m_clientArea->IsRowSelected(row); | |
4272 | } | |
4273 | return false; | |
4274 | } | |
4275 | ||
4276 | // Selection code with row number as parameter | |
4277 | int wxDataViewCtrl::GetSelections( wxArrayInt & sel ) const | |
4278 | { | |
4279 | sel.Empty(); | |
4280 | wxDataViewSelection selection = m_clientArea->GetSelections(); | |
4281 | int len = selection.GetCount(); | |
4282 | for( int i = 0; i < len; i ++) | |
4283 | { | |
4284 | unsigned int row = selection[i]; | |
4285 | sel.Add( row ); | |
4286 | } | |
4287 | return len; | |
4288 | } | |
4289 | ||
4290 | void wxDataViewCtrl::SetSelections( const wxArrayInt & sel ) | |
4291 | { | |
4292 | wxDataViewSelection selection(wxDataViewSelectionCmp); | |
4293 | int len = sel.GetCount(); | |
4294 | for( int i = 0; i < len; i ++ ) | |
4295 | { | |
4296 | int row = sel[i]; | |
4297 | if( row >= 0 ) | |
4298 | selection.Add( static_cast<unsigned int>(row) ); | |
4299 | } | |
4300 | m_clientArea->SetSelections( selection ); | |
4301 | } | |
4302 | ||
4303 | void wxDataViewCtrl::Select( int row ) | |
4304 | { | |
4305 | if( row >= 0 ) | |
4306 | { | |
4307 | if (m_clientArea->IsSingleSel()) | |
4308 | m_clientArea->SelectAllRows(false); | |
4309 | m_clientArea->SelectRow( row, true ); | |
4310 | } | |
4311 | } | |
4312 | ||
4313 | void wxDataViewCtrl::Unselect( int row ) | |
4314 | { | |
4315 | if( row >= 0 ) | |
4316 | m_clientArea->SelectRow(row, false); | |
4317 | } | |
4318 | ||
4319 | bool wxDataViewCtrl::IsSelected( int row ) const | |
4320 | { | |
4321 | if( row >= 0 ) | |
4322 | return m_clientArea->IsRowSelected(row); | |
4323 | return false; | |
4324 | } | |
4325 | ||
4326 | void wxDataViewCtrl::SelectRange( int from, int to ) | |
4327 | { | |
4328 | wxArrayInt sel; | |
4329 | for( int i = from; i < to; i ++ ) | |
4330 | sel.Add( i ); | |
4331 | m_clientArea->Select(sel); | |
4332 | } | |
4333 | ||
4334 | void wxDataViewCtrl::UnselectRange( int from, int to ) | |
4335 | { | |
4336 | wxDataViewSelection sel = m_clientArea->GetSelections(); | |
4337 | for( int i = from; i < to; i ++ ) | |
4338 | if( sel.Index( i ) != wxNOT_FOUND ) | |
4339 | sel.Remove( i ); | |
4340 | m_clientArea->SetSelections(sel); | |
4341 | } | |
4342 | ||
4343 | void wxDataViewCtrl::SelectAll() | |
4344 | { | |
4345 | m_clientArea->SelectAllRows(true); | |
4346 | } | |
4347 | ||
4348 | void wxDataViewCtrl::UnselectAll() | |
4349 | { | |
4350 | m_clientArea->SelectAllRows(false); | |
4351 | } | |
4352 | ||
4353 | void wxDataViewCtrl::EnsureVisible( int row, int column ) | |
4354 | { | |
4355 | if( row < 0 ) | |
4356 | row = 0; | |
4357 | if( row > (int) m_clientArea->GetRowCount() ) | |
4358 | row = m_clientArea->GetRowCount(); | |
4359 | ||
4360 | int first = m_clientArea->GetFirstVisibleRow(); | |
4361 | int last = m_clientArea->GetLastVisibleRow(); | |
4362 | if( row < first ) | |
4363 | m_clientArea->ScrollTo( row, column ); | |
4364 | else if( row > last ) | |
4365 | m_clientArea->ScrollTo( row - last + first, column ); | |
4366 | else | |
4367 | m_clientArea->ScrollTo( first, column ); | |
4368 | } | |
4369 | ||
4370 | void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column ) | |
4371 | { | |
4372 | ExpandAncestors( item ); | |
4373 | ||
4374 | m_clientArea->RecalculateDisplay(); | |
4375 | ||
4376 | int row = m_clientArea->GetRowByItem(item); | |
4377 | if( row >= 0 ) | |
4378 | { | |
4379 | if( column == NULL ) | |
4380 | EnsureVisible(row, -1); | |
4381 | else | |
4382 | EnsureVisible( row, GetColumnIndex(column) ); | |
4383 | } | |
4384 | ||
4385 | } | |
4386 | ||
4387 | void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item, | |
4388 | wxDataViewColumn* &column ) const | |
4389 | { | |
4390 | m_clientArea->HitTest(point, item, column); | |
4391 | } | |
4392 | ||
4393 | wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item, | |
4394 | const wxDataViewColumn* column ) const | |
4395 | { | |
4396 | return m_clientArea->GetItemRect(item, column); | |
4397 | } | |
4398 | ||
4399 | wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const | |
4400 | { | |
4401 | return m_clientArea->GetItemByRow( row ); | |
4402 | } | |
4403 | ||
4404 | int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const | |
4405 | { | |
4406 | return m_clientArea->GetRowByItem( item ); | |
4407 | } | |
4408 | ||
4409 | void wxDataViewCtrl::Expand( const wxDataViewItem & item ) | |
4410 | { | |
4411 | int row = m_clientArea->GetRowByItem( item ); | |
4412 | if (row != -1) | |
4413 | m_clientArea->Expand(row); | |
4414 | } | |
4415 | ||
4416 | void wxDataViewCtrl::Collapse( const wxDataViewItem & item ) | |
4417 | { | |
4418 | int row = m_clientArea->GetRowByItem( item ); | |
4419 | if (row != -1) | |
4420 | m_clientArea->Collapse(row); | |
4421 | } | |
4422 | ||
4423 | bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const | |
4424 | { | |
4425 | int row = m_clientArea->GetRowByItem( item ); | |
4426 | if (row != -1) | |
4427 | return m_clientArea->IsExpanded(row); | |
4428 | return false; | |
4429 | } | |
4430 | ||
4431 | void wxDataViewCtrl::StartEditor( const wxDataViewItem & item, unsigned int column ) | |
4432 | { | |
4433 | wxDataViewColumn* col = GetColumn( column ); | |
4434 | if (!col) | |
4435 | return; | |
4436 | ||
4437 | wxRect itemRect = GetItemRect(item, col); | |
4438 | wxDataViewRenderer* renderer = col->GetRenderer(); | |
4439 | renderer->StartEditing(item, itemRect); | |
4440 | } | |
4441 | ||
4442 | #endif // !wxUSE_GENERICDATAVIEWCTRL | |
4443 | ||
4444 | #endif // wxUSE_DATAVIEWCTRL |