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