]> git.saurik.com Git - wxWidgets.git/blob - src/generic/datavgen.cpp
Strip EOL characters from wxStyledTextCtrl::GetLineText() return value.
[wxWidgets.git] / src / generic / datavgen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_DATAVIEWCTRL
19
20 #include "wx/dataview.h"
21
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
23
24 #ifndef WX_PRECOMP
25 #ifdef __WXMSW__
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
29 #endif
30 #include "wx/sizer.h"
31 #include "wx/log.h"
32 #include "wx/dcclient.h"
33 #include "wx/timer.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
37 #include "wx/frame.h"
38 #endif
39
40 #include "wx/stockitem.h"
41 #include "wx/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 #include "wx/dnd.h"
50 #include "wx/stopwatch.h"
51 #include "wx/weakref.h"
52
53 //-----------------------------------------------------------------------------
54 // classes
55 //-----------------------------------------------------------------------------
56
57 class wxDataViewColumn;
58 class wxDataViewHeaderWindow;
59 class wxDataViewCtrl;
60
61 //-----------------------------------------------------------------------------
62 // classes
63 //-----------------------------------------------------------------------------
64
65 static const int SCROLL_UNIT_X = 15;
66
67 // the cell padding on the left/right
68 static const int PADDING_RIGHTLEFT = 3;
69
70 // the expander space margin
71 static const int EXPANDER_MARGIN = 4;
72
73 #ifdef __WXMSW__
74 static const int EXPANDER_OFFSET = 4;
75 #else
76 static const int EXPANDER_OFFSET = 1;
77 #endif
78
79 // Below is the compare stuff.
80 // For the generic implementation, both the leaf nodes and the nodes are sorted for
81 // fast search when needed
82 static wxDataViewModel* g_model;
83 static int g_column = -2;
84 static bool g_asending = true;
85
86 // ----------------------------------------------------------------------------
87 // helper functions
88 // ----------------------------------------------------------------------------
89
90 namespace
91 {
92
93 // Return the expander column or, if it is not set, the first column and also
94 // set it as the expander one for the future.
95 wxDataViewColumn* GetExpanderColumnOrFirstOne(wxDataViewCtrl* dataview)
96 {
97 wxDataViewColumn* expander = dataview->GetExpanderColumn();
98 if (!expander)
99 {
100 // TODO-RTL: last column for RTL support
101 expander = dataview->GetColumnAt( 0 );
102 dataview->SetExpanderColumn(expander);
103 }
104
105 return expander;
106 }
107
108 } // anonymous namespace
109
110 //-----------------------------------------------------------------------------
111 // wxDataViewColumn
112 //-----------------------------------------------------------------------------
113
114 void wxDataViewColumn::Init(int width, wxAlignment align, int flags)
115 {
116 m_width = width;
117 m_minWidth = 0;
118 m_align = align;
119 m_flags = flags;
120 m_sort = false;
121 m_sortAscending = true;
122 }
123
124 int wxDataViewColumn::GetWidth() const
125 {
126 switch ( m_width )
127 {
128 case wxCOL_WIDTH_DEFAULT:
129 return wxDVC_DEFAULT_WIDTH;
130
131 case wxCOL_WIDTH_AUTOSIZE:
132 wxCHECK_MSG( m_owner, wxDVC_DEFAULT_WIDTH, "no owner control" );
133 return m_owner->GetBestColumnWidth(m_owner->GetColumnIndex(this));
134
135 default:
136 return m_width;
137 }
138 }
139
140 void wxDataViewColumn::UpdateDisplay()
141 {
142 if (m_owner)
143 {
144 int idx = m_owner->GetColumnIndex( this );
145 m_owner->OnColumnChange( idx );
146 }
147 }
148
149 void wxDataViewColumn::SetSortOrder(bool ascending)
150 {
151 if ( !m_owner )
152 return;
153
154 // First unset the old sort column if any.
155 int oldSortKey = m_owner->GetSortingColumnIndex();
156 if ( oldSortKey != wxNOT_FOUND )
157 {
158 m_owner->GetColumn(oldSortKey)->UnsetAsSortKey();
159 }
160
161 // Now set this one as the new sort column.
162 const int idx = m_owner->GetColumnIndex(this);
163 m_owner->SetSortingColumnIndex(idx);
164
165 m_sort = true;
166 m_sortAscending = ascending;
167
168 // Call this directly instead of using UpdateDisplay() as we already have
169 // the column index, no need to look it up again.
170 m_owner->OnColumnChange(idx);
171 }
172
173 //-----------------------------------------------------------------------------
174 // wxDataViewHeaderWindow
175 //-----------------------------------------------------------------------------
176
177 class wxDataViewHeaderWindow : public wxHeaderCtrl
178 {
179 public:
180 wxDataViewHeaderWindow(wxDataViewCtrl *parent)
181 : wxHeaderCtrl(parent)
182 {
183 }
184
185 wxDataViewCtrl *GetOwner() const
186 { return static_cast<wxDataViewCtrl *>(GetParent()); }
187
188 protected:
189 // implement/override wxHeaderCtrl functions by forwarding them to the main
190 // control
191 virtual const wxHeaderColumn& GetColumn(unsigned int idx) const
192 {
193 return *(GetOwner()->GetColumn(idx));
194 }
195
196 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
197 {
198 wxDataViewCtrl * const owner = GetOwner();
199
200 int widthContents = owner->GetBestColumnWidth(idx);
201 owner->GetColumn(idx)->SetWidth(wxMax(widthTitle, widthContents));
202 owner->OnColumnChange(idx);
203
204 return true;
205 }
206
207 private:
208 bool SendEvent(wxEventType type, unsigned int n)
209 {
210 wxDataViewCtrl * const owner = GetOwner();
211 wxDataViewEvent event(type, owner->GetId());
212
213 event.SetEventObject(owner);
214 event.SetColumn(n);
215 event.SetDataViewColumn(owner->GetColumn(n));
216 event.SetModel(owner->GetModel());
217
218 // for events created by wxDataViewHeaderWindow the
219 // row / value fields are not valid
220 return owner->ProcessWindowEvent(event);
221 }
222
223 void OnClick(wxHeaderCtrlEvent& event)
224 {
225 const unsigned idx = event.GetColumn();
226
227 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, idx) )
228 return;
229
230 // default handling for the column click is to sort by this column or
231 // toggle its sort order
232 wxDataViewCtrl * const owner = GetOwner();
233 wxDataViewColumn * const col = owner->GetColumn(idx);
234 if ( !col->IsSortable() )
235 {
236 // no default handling for non-sortable columns
237 event.Skip();
238 return;
239 }
240
241 if ( col->IsSortKey() )
242 {
243 // already using this column for sorting, just change the order
244 col->ToggleSortOrder();
245 }
246 else // not using this column for sorting yet
247 {
248 col->SetSortOrder(true);
249 }
250
251 wxDataViewModel * const model = owner->GetModel();
252 if ( model )
253 model->Resort();
254
255 owner->OnColumnChange(idx);
256
257 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, idx);
258 }
259
260 void OnRClick(wxHeaderCtrlEvent& event)
261 {
262 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK,
263 event.GetColumn()) )
264 event.Skip();
265 }
266
267 void OnResize(wxHeaderCtrlEvent& event)
268 {
269 wxDataViewCtrl * const owner = GetOwner();
270
271 const unsigned col = event.GetColumn();
272 owner->GetColumn(col)->SetWidth(event.GetWidth());
273 GetOwner()->OnColumnChange(col);
274 }
275
276 void OnEndReorder(wxHeaderCtrlEvent& event)
277 {
278 wxDataViewCtrl * const owner = GetOwner();
279 owner->ColumnMoved(owner->GetColumn(event.GetColumn()),
280 event.GetNewOrder());
281 }
282
283 DECLARE_EVENT_TABLE()
284 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow);
285 };
286
287 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow, wxHeaderCtrl)
288 EVT_HEADER_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnClick)
289 EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnRClick)
290
291 EVT_HEADER_RESIZING(wxID_ANY, wxDataViewHeaderWindow::OnResize)
292 EVT_HEADER_END_RESIZE(wxID_ANY, wxDataViewHeaderWindow::OnResize)
293
294 EVT_HEADER_END_REORDER(wxID_ANY, wxDataViewHeaderWindow::OnEndReorder)
295 END_EVENT_TABLE()
296
297 //-----------------------------------------------------------------------------
298 // wxDataViewRenameTimer
299 //-----------------------------------------------------------------------------
300
301 class wxDataViewRenameTimer: public wxTimer
302 {
303 private:
304 wxDataViewMainWindow *m_owner;
305
306 public:
307 wxDataViewRenameTimer( wxDataViewMainWindow *owner );
308 void Notify();
309 };
310
311 //-----------------------------------------------------------------------------
312 // wxDataViewTreeNode
313 //-----------------------------------------------------------------------------
314
315 class wxDataViewTreeNode;
316 WX_DEFINE_ARRAY( wxDataViewTreeNode *, wxDataViewTreeNodes );
317
318 int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1,
319 wxDataViewTreeNode ** node2);
320
321 class wxDataViewTreeNode
322 {
323 public:
324 wxDataViewTreeNode(wxDataViewTreeNode *parent, const wxDataViewItem& item)
325 : m_parent(parent),
326 m_item(item),
327 m_branchData(NULL)
328 {
329 }
330
331 ~wxDataViewTreeNode()
332 {
333 if ( m_branchData )
334 {
335 wxDataViewTreeNodes& nodes = m_branchData->children;
336 for ( wxDataViewTreeNodes::iterator i = nodes.begin();
337 i != nodes.end();
338 ++i )
339 {
340 delete *i;
341 }
342
343 delete m_branchData;
344 }
345 }
346
347 static wxDataViewTreeNode* CreateRootNode()
348 {
349 wxDataViewTreeNode *n = new wxDataViewTreeNode(NULL, wxDataViewItem());
350 n->m_branchData = new BranchNodeData;
351 n->m_branchData->open = true;
352 return n;
353 }
354
355 wxDataViewTreeNode * GetParent() const { return m_parent; }
356
357 const wxDataViewTreeNodes& GetChildNodes() const
358 {
359 wxASSERT( m_branchData != NULL );
360 return m_branchData->children;
361 }
362
363 void InsertChild(wxDataViewTreeNode *node, unsigned index)
364 {
365 if ( !m_branchData )
366 m_branchData = new BranchNodeData;
367
368 m_branchData->children.Insert(node, index);
369
370 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
371 if (g_column >= -1)
372 m_branchData->children.Sort( &wxGenericTreeModelNodeCmp );
373 }
374
375 void RemoveChild(wxDataViewTreeNode *node)
376 {
377 wxCHECK_RET( m_branchData != NULL, "leaf node doesn't have children" );
378 m_branchData->children.Remove(node);
379 }
380
381 // returns position of child node for given item in children list or wxNOT_FOUND
382 int FindChildByItem(const wxDataViewItem& item) const
383 {
384 if ( !m_branchData )
385 return wxNOT_FOUND;
386
387 const wxDataViewTreeNodes& nodes = m_branchData->children;
388 const int len = nodes.size();
389 for ( int i = 0; i < len; i++ )
390 {
391 if ( nodes[i]->m_item == item )
392 return i;
393 }
394 return wxNOT_FOUND;
395 }
396
397 const wxDataViewItem & GetItem() const { return m_item; }
398 void SetItem( const wxDataViewItem & item ) { m_item = item; }
399
400 int GetIndentLevel() const
401 {
402 int ret = 0;
403 const wxDataViewTreeNode * node = this;
404 while( node->GetParent()->GetParent() != NULL )
405 {
406 node = node->GetParent();
407 ret ++;
408 }
409 return ret;
410 }
411
412 bool IsOpen() const
413 {
414 return m_branchData && m_branchData->open;
415 }
416
417 void ToggleOpen()
418 {
419 // We do not allow the (invisible) root node to be collapsed because
420 // there is no way to expand it again.
421 if ( !m_parent )
422 return;
423
424 wxCHECK_RET( m_branchData != NULL, "can't open leaf node" );
425
426 int sum = 0;
427
428 const wxDataViewTreeNodes& nodes = m_branchData->children;
429 const int len = nodes.GetCount();
430 for ( int i = 0;i < len; i ++)
431 sum += 1 + nodes[i]->GetSubTreeCount();
432
433 if (m_branchData->open)
434 {
435 ChangeSubTreeCount(-sum);
436 m_branchData->open = !m_branchData->open;
437 }
438 else
439 {
440 m_branchData->open = !m_branchData->open;
441 ChangeSubTreeCount(+sum);
442 }
443 }
444
445 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
446 // even if GetChildNodes() is empty; see below.
447 bool HasChildren() const
448 {
449 return m_branchData != NULL;
450 }
451
452 void SetHasChildren(bool has)
453 {
454 // The invisible root item always has children, so ignore any attempts
455 // to change this.
456 if ( !m_parent )
457 return;
458
459 if ( !has )
460 {
461 wxDELETE(m_branchData);
462 }
463 else if ( m_branchData == NULL )
464 {
465 m_branchData = new BranchNodeData;
466 }
467 }
468
469 int GetSubTreeCount() const
470 {
471 return m_branchData ? m_branchData->subTreeCount : 0;
472 }
473
474 void ChangeSubTreeCount( int num )
475 {
476 wxASSERT( m_branchData != NULL );
477
478 if( !m_branchData->open )
479 return;
480
481 m_branchData->subTreeCount += num;
482 wxASSERT( m_branchData->subTreeCount >= 0 );
483
484 if( m_parent )
485 m_parent->ChangeSubTreeCount(num);
486 }
487
488 void Resort()
489 {
490 if ( !m_branchData )
491 return;
492
493 if (g_column >= -1)
494 {
495 wxDataViewTreeNodes& nodes = m_branchData->children;
496
497 nodes.Sort( &wxGenericTreeModelNodeCmp );
498 int len = nodes.GetCount();
499 for (int i = 0; i < len; i ++)
500 {
501 if ( nodes[i]->HasChildren() )
502 nodes[i]->Resort();
503 }
504 }
505 }
506
507
508 private:
509 wxDataViewTreeNode *m_parent;
510
511 // Corresponding model item.
512 wxDataViewItem m_item;
513
514 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
515 // separate struct in order to conserve memory.
516 struct BranchNodeData
517 {
518 BranchNodeData()
519 : open(false),
520 subTreeCount(0)
521 {
522 }
523
524 // Child nodes. Note that this may be empty even if m_hasChildren in
525 // case this branch of the tree wasn't expanded and realized yet.
526 wxDataViewTreeNodes children;
527
528 // Is the branch node currently open (expanded)?
529 bool open;
530
531 // Total count of expanded (i.e. visible with the help of some
532 // scrolling) items in the subtree, but excluding this node. I.e. it is
533 // 0 for leaves and is the number of rows the subtree occupies for
534 // branch nodes.
535 int subTreeCount;
536 };
537
538 BranchNodeData *m_branchData;
539 };
540
541
542 int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1,
543 wxDataViewTreeNode ** node2)
544 {
545 return g_model->Compare( (*node1)->GetItem(), (*node2)->GetItem(), g_column, g_asending );
546 }
547
548
549 //-----------------------------------------------------------------------------
550 // wxDataViewMainWindow
551 //-----------------------------------------------------------------------------
552
553 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection);
554
555 class wxDataViewMainWindow: public wxWindow
556 {
557 public:
558 wxDataViewMainWindow( wxDataViewCtrl *parent,
559 wxWindowID id,
560 const wxPoint &pos = wxDefaultPosition,
561 const wxSize &size = wxDefaultSize,
562 const wxString &name = wxT("wxdataviewctrlmainwindow") );
563 virtual ~wxDataViewMainWindow();
564
565 bool IsList() const { return GetModel()->IsListModel(); }
566 bool IsVirtualList() const { return m_root == NULL; }
567
568 // notifications from wxDataViewModel
569 bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
570 bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
571 bool ItemChanged( const wxDataViewItem &item );
572 bool ValueChanged( const wxDataViewItem &item, unsigned int model_column );
573 bool Cleared();
574 void Resort()
575 {
576 if (!IsVirtualList())
577 {
578 SortPrepare();
579 m_root->Resort();
580 }
581 UpdateDisplay();
582 }
583
584 void SortPrepare()
585 {
586 g_model = GetModel();
587 wxDataViewColumn* col = GetOwner()->GetSortingColumn();
588 if( !col )
589 {
590 if (g_model->HasDefaultCompare())
591 g_column = -1;
592 else
593 g_column = -2;
594
595 g_asending = true;
596 return;
597 }
598 g_column = col->GetModelColumn();
599 g_asending = col->IsSortOrderAscending();
600 }
601
602 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
603 wxDataViewCtrl *GetOwner() { return m_owner; }
604 const wxDataViewCtrl *GetOwner() const { return m_owner; }
605
606 wxDataViewModel* GetModel() { return GetOwner()->GetModel(); }
607 const wxDataViewModel* GetModel() const { return GetOwner()->GetModel(); }
608
609 #if wxUSE_DRAG_AND_DROP
610 wxBitmap CreateItemBitmap( unsigned int row, int &indent );
611 #endif // wxUSE_DRAG_AND_DROP
612 void OnPaint( wxPaintEvent &event );
613 void OnCharHook( wxKeyEvent &event );
614 void OnChar( wxKeyEvent &event );
615 void OnVerticalNavigation(int delta, const wxKeyEvent& event);
616 void OnLeftKey();
617 void OnRightKey();
618 void OnMouse( wxMouseEvent &event );
619 void OnSetFocus( wxFocusEvent &event );
620 void OnKillFocus( wxFocusEvent &event );
621
622 void UpdateDisplay();
623 void RecalculateDisplay();
624 void OnInternalIdle();
625
626 void OnRenameTimer();
627
628 void ScrollWindow( int dx, int dy, const wxRect *rect = NULL );
629 void ScrollTo( int rows, int column );
630
631 unsigned GetCurrentRow() const { return m_currentRow; }
632 bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; }
633 void ChangeCurrentRow( unsigned int row );
634 bool TryAdvanceCurrentColumn(wxDataViewTreeNode *node, bool forward);
635
636 wxDataViewColumn *GetCurrentColumn() const { return m_currentCol; }
637 void ClearCurrentColumn() { m_currentCol = NULL; }
638
639 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); }
640 bool IsEmpty() { return GetRowCount() == 0; }
641
642 int GetCountPerPage() const;
643 int GetEndOfLastCol() const;
644 unsigned int GetFirstVisibleRow() const;
645
646 // I change this method to un const because in the tree view,
647 // the displaying number of the tree are changing along with the
648 // expanding/collapsing of the tree nodes
649 unsigned int GetLastVisibleRow();
650 unsigned int GetRowCount() const;
651
652 const wxDataViewSelection& GetSelections() const { return m_selection; }
653 void SetSelections( const wxDataViewSelection & sel )
654 { m_selection = sel; UpdateDisplay(); }
655 void Select( const wxArrayInt& aSelections );
656 void SelectAllRows( bool on );
657 void SelectRow( unsigned int row, bool on );
658 void SelectRows( unsigned int from, unsigned int to, bool on );
659 void ReverseRowSelection( unsigned int row );
660 bool IsRowSelected( unsigned int row );
661 void SendSelectionChangedEvent( const wxDataViewItem& item);
662
663 void RefreshRow( unsigned int row );
664 void RefreshRows( unsigned int from, unsigned int to );
665 void RefreshRowsAfter( unsigned int firstRow );
666
667 // returns the colour to be used for drawing the rules
668 wxColour GetRuleColour() const
669 {
670 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
671 }
672
673 wxRect GetLineRect( unsigned int row ) const;
674
675 int GetLineStart( unsigned int row ) const; // row * m_lineHeight in fixed mode
676 int GetLineHeight( unsigned int row ) const; // m_lineHeight in fixed mode
677 int GetLineAt( unsigned int y ) const; // y / m_lineHeight in fixed mode
678
679 void SetRowHeight( int lineHeight ) { m_lineHeight = lineHeight; }
680 int GetRowHeight() const { return m_lineHeight; }
681
682 // Some useful functions for row and item mapping
683 wxDataViewItem GetItemByRow( unsigned int row ) const;
684 int GetRowByItem( const wxDataViewItem & item ) const;
685
686 wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row ) const;
687 // We did not need this temporarily
688 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
689
690 // Methods for building the mapping tree
691 void BuildTree( wxDataViewModel * model );
692 void DestroyTree();
693 void HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column );
694 wxRect GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column );
695
696 void Expand( unsigned int row );
697 void Collapse( unsigned int row );
698 bool IsExpanded( unsigned int row ) const;
699 bool HasChildren( unsigned int row ) const;
700
701 #if wxUSE_DRAG_AND_DROP
702 bool EnableDragSource( const wxDataFormat &format );
703 bool EnableDropTarget( const wxDataFormat &format );
704
705 void RemoveDropHint();
706 wxDragResult OnDragOver( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def );
707 bool OnDrop( wxDataFormat format, wxCoord x, wxCoord y );
708 wxDragResult OnData( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def );
709 void OnLeave();
710 #endif // wxUSE_DRAG_AND_DROP
711
712 void OnColumnsCountChanged();
713
714 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
715 // specified item in the given column.
716 void StartEditing(const wxDataViewItem& item, const wxDataViewColumn* col);
717
718 private:
719 int RecalculateCount() const;
720
721 // Return false only if the event was vetoed by its handler.
722 bool SendExpanderEvent(wxEventType type, const wxDataViewItem& item);
723
724 wxDataViewTreeNode * FindNode( const wxDataViewItem & item );
725
726 wxDataViewColumn *FindColumnForEditing(const wxDataViewItem& item, wxDataViewCellMode mode);
727
728 bool IsCellEditableInMode(const wxDataViewItem& item, const wxDataViewColumn *col, wxDataViewCellMode mode) const;
729
730 void DrawCellBackground( wxDataViewRenderer* cell, wxDC& dc, const wxRect& rect );
731
732 private:
733 wxDataViewCtrl *m_owner;
734 int m_lineHeight;
735 bool m_dirty;
736
737 wxDataViewColumn *m_currentCol;
738 unsigned int m_currentRow;
739 wxDataViewSelection m_selection;
740
741 wxDataViewRenameTimer *m_renameTimer;
742 bool m_lastOnSame;
743
744 bool m_hasFocus;
745 bool m_useCellFocus;
746 bool m_currentColSetByKeyboard;
747
748 #if wxUSE_DRAG_AND_DROP
749 int m_dragCount;
750 wxPoint m_dragStart;
751
752 bool m_dragEnabled;
753 wxDataFormat m_dragFormat;
754
755 bool m_dropEnabled;
756 wxDataFormat m_dropFormat;
757 bool m_dropHint;
758 unsigned int m_dropHintLine;
759 #endif // wxUSE_DRAG_AND_DROP
760
761 // for double click logic
762 unsigned int m_lineLastClicked,
763 m_lineBeforeLastClicked,
764 m_lineSelectSingleOnUp;
765
766 // the pen used to draw horiz/vertical rules
767 wxPen m_penRule;
768
769 // the pen used to draw the expander and the lines
770 wxPen m_penExpander;
771
772 // This is the tree structure of the model
773 wxDataViewTreeNode * m_root;
774 int m_count;
775
776 // This is the tree node under the cursor
777 wxDataViewTreeNode * m_underMouse;
778
779 // The control used for editing or NULL.
780 wxWeakRef<wxWindow> m_editorCtrl;
781
782 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
783 wxDataViewRenderer* m_editorRenderer;
784
785 private:
786 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow)
787 DECLARE_EVENT_TABLE()
788 };
789
790 // ---------------------------------------------------------
791 // wxGenericDataViewModelNotifier
792 // ---------------------------------------------------------
793
794 class wxGenericDataViewModelNotifier: public wxDataViewModelNotifier
795 {
796 public:
797 wxGenericDataViewModelNotifier( wxDataViewMainWindow *mainWindow )
798 { m_mainWindow = mainWindow; }
799
800 virtual bool ItemAdded( const wxDataViewItem & parent, const wxDataViewItem & item )
801 { return m_mainWindow->ItemAdded( parent , item ); }
802 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
803 { return m_mainWindow->ItemDeleted( parent, item ); }
804 virtual bool ItemChanged( const wxDataViewItem & item )
805 { return m_mainWindow->ItemChanged(item); }
806 virtual bool ValueChanged( const wxDataViewItem & item , unsigned int col )
807 { return m_mainWindow->ValueChanged( item, col ); }
808 virtual bool Cleared()
809 { return m_mainWindow->Cleared(); }
810 virtual void Resort()
811 { m_mainWindow->Resort(); }
812
813 wxDataViewMainWindow *m_mainWindow;
814 };
815
816 // ---------------------------------------------------------
817 // wxDataViewRenderer
818 // ---------------------------------------------------------
819
820 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
821
822 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype,
823 wxDataViewCellMode mode,
824 int align) :
825 wxDataViewCustomRendererBase( varianttype, mode, align )
826 {
827 m_align = align;
828 m_mode = mode;
829 m_ellipsizeMode = wxELLIPSIZE_MIDDLE;
830 m_dc = NULL;
831 }
832
833 wxDataViewRenderer::~wxDataViewRenderer()
834 {
835 delete m_dc;
836 }
837
838 wxDC *wxDataViewRenderer::GetDC()
839 {
840 if (m_dc == NULL)
841 {
842 if (GetOwner() == NULL)
843 return NULL;
844 if (GetOwner()->GetOwner() == NULL)
845 return NULL;
846 m_dc = new wxClientDC( GetOwner()->GetOwner() );
847 }
848
849 return m_dc;
850 }
851
852 void wxDataViewRenderer::SetAlignment( int align )
853 {
854 m_align=align;
855 }
856
857 int wxDataViewRenderer::GetAlignment() const
858 {
859 return m_align;
860 }
861
862 // ---------------------------------------------------------
863 // wxDataViewCustomRenderer
864 // ---------------------------------------------------------
865
866 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
867
868 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
869 wxDataViewCellMode mode, int align ) :
870 wxDataViewRenderer( varianttype, mode, align )
871 {
872 }
873
874 // ---------------------------------------------------------
875 // wxDataViewTextRenderer
876 // ---------------------------------------------------------
877
878 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
879
880 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype,
881 wxDataViewCellMode mode, int align ) :
882 wxDataViewRenderer( varianttype, mode, align )
883 {
884 }
885
886 bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
887 {
888 m_text = value.GetString();
889
890 return true;
891 }
892
893 bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
894 {
895 return false;
896 }
897
898 bool wxDataViewTextRenderer::HasEditorCtrl() const
899 {
900 return true;
901 }
902
903 wxWindow* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent,
904 wxRect labelRect, const wxVariant &value )
905 {
906 wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, value,
907 wxPoint(labelRect.x,labelRect.y),
908 wxSize(labelRect.width,labelRect.height),
909 wxTE_PROCESS_ENTER );
910
911 // select the text in the control an place the cursor at the end
912 ctrl->SetInsertionPointEnd();
913 ctrl->SelectAll();
914
915 return ctrl;
916 }
917
918 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant &value )
919 {
920 wxTextCtrl *text = (wxTextCtrl*) editor;
921 value = text->GetValue();
922 return true;
923 }
924
925 bool wxDataViewTextRenderer::Render(wxRect rect, wxDC *dc, int state)
926 {
927 RenderText(m_text, 0, rect, dc, state);
928 return true;
929 }
930
931 wxSize wxDataViewTextRenderer::GetSize() const
932 {
933 if (!m_text.empty())
934 return GetTextExtent(m_text);
935 else
936 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE);
937 }
938
939 // ---------------------------------------------------------
940 // wxDataViewBitmapRenderer
941 // ---------------------------------------------------------
942
943 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
944
945 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype,
946 wxDataViewCellMode mode, int align ) :
947 wxDataViewRenderer( varianttype, mode, align )
948 {
949 }
950
951 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
952 {
953 if (value.GetType() == wxT("wxBitmap"))
954 m_bitmap << value;
955 if (value.GetType() == wxT("wxIcon"))
956 m_icon << value;
957
958 return true;
959 }
960
961 bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
962 {
963 return false;
964 }
965
966 bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
967 {
968 if (m_bitmap.IsOk())
969 dc->DrawBitmap( m_bitmap, cell.x, cell.y );
970 else if (m_icon.IsOk())
971 dc->DrawIcon( m_icon, cell.x, cell.y );
972
973 return true;
974 }
975
976 wxSize wxDataViewBitmapRenderer::GetSize() const
977 {
978 if (m_bitmap.IsOk())
979 return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() );
980 else if (m_icon.IsOk())
981 return wxSize( m_icon.GetWidth(), m_icon.GetHeight() );
982
983 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE);
984 }
985
986 // ---------------------------------------------------------
987 // wxDataViewToggleRenderer
988 // ---------------------------------------------------------
989
990 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
991
992 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
993 wxDataViewCellMode mode, int align ) :
994 wxDataViewRenderer( varianttype, mode, align )
995 {
996 m_toggle = false;
997 }
998
999 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
1000 {
1001 m_toggle = value.GetBool();
1002
1003 return true;
1004 }
1005
1006 bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
1007 {
1008 return false;
1009 }
1010
1011 bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
1012 {
1013 int flags = 0;
1014 if (m_toggle)
1015 flags |= wxCONTROL_CHECKED;
1016 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE ||
1017 GetEnabled() == false)
1018 flags |= wxCONTROL_DISABLED;
1019
1020 // Ensure that the check boxes always have at least the minimal required
1021 // size, otherwise DrawCheckBox() doesn't really work well. If this size is
1022 // greater than the cell size, the checkbox will be truncated but this is a
1023 // lesser evil.
1024 wxSize size = cell.GetSize();
1025 size.IncTo(GetSize());
1026 cell.SetSize(size);
1027
1028 wxRendererNative::Get().DrawCheckBox(
1029 GetOwner()->GetOwner(),
1030 *dc,
1031 cell,
1032 flags );
1033
1034 return true;
1035 }
1036
1037 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect& WXUNUSED(cell),
1038 wxDataViewModel *model,
1039 const wxDataViewItem& item,
1040 unsigned int col,
1041 const wxMouseEvent *mouseEvent)
1042 {
1043 if ( mouseEvent )
1044 {
1045 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1046 if ( !wxRect(GetSize()).Contains(mouseEvent->GetPosition()) )
1047 return false;
1048 }
1049
1050 model->ChangeValue(!m_toggle, item, col);
1051 return true;
1052 }
1053
1054 wxSize wxDataViewToggleRenderer::GetSize() const
1055 {
1056 // the window parameter is not used by GetCheckBoxSize() so it's
1057 // safe to pass NULL
1058 return wxRendererNative::Get().GetCheckBoxSize(NULL);
1059 }
1060
1061 // ---------------------------------------------------------
1062 // wxDataViewProgressRenderer
1063 // ---------------------------------------------------------
1064
1065 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewRenderer)
1066
1067 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
1068 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
1069 wxDataViewRenderer( varianttype, mode, align )
1070 {
1071 m_label = label;
1072 m_value = 0;
1073 }
1074
1075 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
1076 {
1077 m_value = (long) value;
1078
1079 if (m_value < 0) m_value = 0;
1080 if (m_value > 100) m_value = 100;
1081
1082 return true;
1083 }
1084
1085 bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const
1086 {
1087 value = (long) m_value;
1088 return true;
1089 }
1090
1091 bool
1092 wxDataViewProgressRenderer::Render(wxRect rect, wxDC *dc, int WXUNUSED(state))
1093 {
1094 // deflate the rect to leave a small border between bars in adjacent rows
1095 wxRect bar = rect.Deflate(0, 1);
1096
1097 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1098 dc->SetPen( *wxBLACK_PEN );
1099 dc->DrawRectangle( bar );
1100
1101 bar.width = (int)(bar.width * m_value / 100.);
1102 dc->SetPen( *wxTRANSPARENT_PEN );
1103
1104 const wxDataViewItemAttr& attr = GetAttr();
1105 dc->SetBrush( attr.HasColour() ? wxBrush(attr.GetColour())
1106 : *wxBLUE_BRUSH );
1107 dc->DrawRectangle( bar );
1108
1109 return true;
1110 }
1111
1112 wxSize wxDataViewProgressRenderer::GetSize() const
1113 {
1114 return wxSize(40,12);
1115 }
1116
1117 // ---------------------------------------------------------
1118 // wxDataViewIconTextRenderer
1119 // ---------------------------------------------------------
1120
1121 IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewRenderer)
1122
1123 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1124 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
1125 wxDataViewRenderer( varianttype, mode, align )
1126 {
1127 SetMode(mode);
1128 SetAlignment(align);
1129 }
1130
1131 bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
1132 {
1133 m_value << value;
1134 return true;
1135 }
1136
1137 bool wxDataViewIconTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
1138 {
1139 return false;
1140 }
1141
1142 bool wxDataViewIconTextRenderer::Render(wxRect rect, wxDC *dc, int state)
1143 {
1144 int xoffset = 0;
1145
1146 const wxIcon& icon = m_value.GetIcon();
1147 if ( icon.IsOk() )
1148 {
1149 dc->DrawIcon(icon, rect.x, rect.y + (rect.height - icon.GetHeight())/2);
1150 xoffset = icon.GetWidth()+4;
1151 }
1152
1153 RenderText(m_value.GetText(), xoffset, rect, dc, state);
1154
1155 return true;
1156 }
1157
1158 wxSize wxDataViewIconTextRenderer::GetSize() const
1159 {
1160 if (!m_value.GetText().empty())
1161 {
1162 wxSize size = GetTextExtent(m_value.GetText());
1163
1164 if (m_value.GetIcon().IsOk())
1165 size.x += m_value.GetIcon().GetWidth() + 4;
1166 return size;
1167 }
1168 return wxSize(80,20);
1169 }
1170
1171 wxWindow* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value)
1172 {
1173 wxDataViewIconText iconText;
1174 iconText << value;
1175
1176 wxString text = iconText.GetText();
1177
1178 // adjust the label rect to take the width of the icon into account
1179 if (iconText.GetIcon().IsOk())
1180 {
1181 int w = iconText.GetIcon().GetWidth() + 4;
1182 labelRect.x += w;
1183 labelRect.width -= w;
1184 }
1185
1186 wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, text,
1187 wxPoint(labelRect.x,labelRect.y),
1188 wxSize(labelRect.width,labelRect.height),
1189 wxTE_PROCESS_ENTER );
1190
1191 // select the text in the control an place the cursor at the end
1192 ctrl->SetInsertionPointEnd();
1193 ctrl->SelectAll();
1194
1195 return ctrl;
1196 }
1197
1198 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant& value )
1199 {
1200 wxTextCtrl *text = (wxTextCtrl*) editor;
1201
1202 // The icon can't be edited so get its old value and reuse it.
1203 wxVariant valueOld;
1204 wxDataViewColumn* const col = GetOwner();
1205 GetView()->GetModel()->GetValue(valueOld, m_item, col->GetModelColumn());
1206
1207 wxDataViewIconText iconText;
1208 iconText << valueOld;
1209
1210 // But replace the text with the value entered by user.
1211 iconText.SetText(text->GetValue());
1212
1213 value << iconText;
1214 return true;
1215 }
1216
1217 //-----------------------------------------------------------------------------
1218 // wxDataViewDropTarget
1219 //-----------------------------------------------------------------------------
1220
1221 #if wxUSE_DRAG_AND_DROP
1222
1223 class wxBitmapCanvas: public wxWindow
1224 {
1225 public:
1226 wxBitmapCanvas( wxWindow *parent, const wxBitmap &bitmap, const wxSize &size ) :
1227 wxWindow( parent, wxID_ANY, wxPoint(0,0), size )
1228 {
1229 m_bitmap = bitmap;
1230 Connect( wxEVT_PAINT, wxPaintEventHandler(wxBitmapCanvas::OnPaint) );
1231 }
1232
1233 void OnPaint( wxPaintEvent &WXUNUSED(event) )
1234 {
1235 wxPaintDC dc(this);
1236 dc.DrawBitmap( m_bitmap, 0, 0);
1237 }
1238
1239 wxBitmap m_bitmap;
1240 };
1241
1242 class wxDataViewDropSource: public wxDropSource
1243 {
1244 public:
1245 wxDataViewDropSource( wxDataViewMainWindow *win, unsigned int row ) :
1246 wxDropSource( win )
1247 {
1248 m_win = win;
1249 m_row = row;
1250 m_hint = NULL;
1251 }
1252
1253 ~wxDataViewDropSource()
1254 {
1255 delete m_hint;
1256 }
1257
1258 virtual bool GiveFeedback( wxDragResult WXUNUSED(effect) )
1259 {
1260 wxPoint pos = wxGetMousePosition();
1261
1262 if (!m_hint)
1263 {
1264 int liney = m_win->GetLineStart( m_row );
1265 int linex = 0;
1266 m_win->GetOwner()->CalcUnscrolledPosition( 0, liney, NULL, &liney );
1267 m_win->ClientToScreen( &linex, &liney );
1268 m_dist_x = pos.x - linex;
1269 m_dist_y = pos.y - liney;
1270
1271 int indent = 0;
1272 wxBitmap ib = m_win->CreateItemBitmap( m_row, indent );
1273 m_dist_x -= indent;
1274 m_hint = new wxFrame( m_win->GetParent(), wxID_ANY, wxEmptyString,
1275 wxPoint(pos.x - m_dist_x, pos.y + 5 ),
1276 ib.GetSize(),
1277 wxFRAME_TOOL_WINDOW |
1278 wxFRAME_FLOAT_ON_PARENT |
1279 wxFRAME_NO_TASKBAR |
1280 wxNO_BORDER );
1281 new wxBitmapCanvas( m_hint, ib, ib.GetSize() );
1282 m_hint->Show();
1283 }
1284 else
1285 {
1286 m_hint->Move( pos.x - m_dist_x, pos.y + 5 );
1287 m_hint->SetTransparent( 128 );
1288 }
1289
1290 return false;
1291 }
1292
1293 wxDataViewMainWindow *m_win;
1294 unsigned int m_row;
1295 wxFrame *m_hint;
1296 int m_dist_x,m_dist_y;
1297 };
1298
1299
1300 class wxDataViewDropTarget: public wxDropTarget
1301 {
1302 public:
1303 wxDataViewDropTarget( wxDataObject *obj, wxDataViewMainWindow *win ) :
1304 wxDropTarget( obj )
1305 {
1306 m_win = win;
1307 }
1308
1309 virtual wxDragResult OnDragOver( wxCoord x, wxCoord y, wxDragResult def )
1310 {
1311 wxDataFormat format = GetMatchingPair();
1312 if (format == wxDF_INVALID)
1313 return wxDragNone;
1314 return m_win->OnDragOver( format, x, y, def);
1315 }
1316
1317 virtual bool OnDrop( wxCoord x, wxCoord y )
1318 {
1319 wxDataFormat format = GetMatchingPair();
1320 if (format == wxDF_INVALID)
1321 return false;
1322 return m_win->OnDrop( format, x, y );
1323 }
1324
1325 virtual wxDragResult OnData( wxCoord x, wxCoord y, wxDragResult def )
1326 {
1327 wxDataFormat format = GetMatchingPair();
1328 if (format == wxDF_INVALID)
1329 return wxDragNone;
1330 if (!GetData())
1331 return wxDragNone;
1332 return m_win->OnData( format, x, y, def );
1333 }
1334
1335 virtual void OnLeave()
1336 { m_win->OnLeave(); }
1337
1338 wxDataViewMainWindow *m_win;
1339 };
1340
1341 #endif // wxUSE_DRAG_AND_DROP
1342
1343 //-----------------------------------------------------------------------------
1344 // wxDataViewRenameTimer
1345 //-----------------------------------------------------------------------------
1346
1347 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner )
1348 {
1349 m_owner = owner;
1350 }
1351
1352 void wxDataViewRenameTimer::Notify()
1353 {
1354 m_owner->OnRenameTimer();
1355 }
1356
1357 //-----------------------------------------------------------------------------
1358 // wxDataViewMainWindow
1359 //-----------------------------------------------------------------------------
1360
1361 // The tree building helper, declared firstly
1362 static void BuildTreeHelper( const wxDataViewModel * model, const wxDataViewItem & item,
1363 wxDataViewTreeNode * node);
1364
1365 int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
1366 {
1367 if (row1 > row2) return 1;
1368 if (row1 == row2) return 0;
1369 return -1;
1370 }
1371
1372 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
1373
1374 BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
1375 EVT_PAINT (wxDataViewMainWindow::OnPaint)
1376 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
1377 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
1378 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus)
1379 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook)
1380 EVT_CHAR (wxDataViewMainWindow::OnChar)
1381 END_EVENT_TABLE()
1382
1383 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
1384 const wxPoint &pos, const wxSize &size, const wxString &name ) :
1385 wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE, name ),
1386 m_selection( wxDataViewSelectionCmp )
1387
1388 {
1389 SetOwner( parent );
1390
1391 m_editorRenderer = NULL;
1392
1393 m_lastOnSame = false;
1394 m_renameTimer = new wxDataViewRenameTimer( this );
1395
1396 // TODO: user better initial values/nothing selected
1397 m_currentCol = NULL;
1398 m_currentColSetByKeyboard = false;
1399 m_useCellFocus = false;
1400 m_currentRow = (unsigned)-1;
1401
1402 #ifdef __WXMSW__
1403 // We would like to use the same line height that Explorer uses. This is
1404 // different from standard ListView control since Vista.
1405 if ( wxGetWinVersion() >= wxWinVersion_Vista )
1406 m_lineHeight = wxMax(16, GetCharHeight()) + 6; // 16 = mini icon height
1407 else
1408 #endif // __WXMSW__
1409 m_lineHeight = wxMax(16, GetCharHeight()) + 1; // 16 = mini icon height
1410
1411 #if wxUSE_DRAG_AND_DROP
1412 m_dragCount = 0;
1413 m_dragStart = wxPoint(0,0);
1414
1415 m_dragEnabled = false;
1416 m_dropEnabled = false;
1417 m_dropHint = false;
1418 m_dropHintLine = (unsigned int) -1;
1419 #endif // wxUSE_DRAG_AND_DROP
1420
1421 m_lineLastClicked = (unsigned int) -1;
1422 m_lineBeforeLastClicked = (unsigned int) -1;
1423 m_lineSelectSingleOnUp = (unsigned int) -1;
1424
1425 m_hasFocus = false;
1426
1427 SetBackgroundColour( *wxWHITE );
1428
1429 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
1430
1431 m_penRule = wxPen(GetRuleColour());
1432
1433 // compose a pen whichcan draw black lines
1434 // TODO: maybe there is something system colour to use
1435 m_penExpander = wxPen(wxColour(0,0,0));
1436
1437 m_root = wxDataViewTreeNode::CreateRootNode();
1438
1439 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1440 m_count = -1;
1441 m_underMouse = NULL;
1442 UpdateDisplay();
1443 }
1444
1445 wxDataViewMainWindow::~wxDataViewMainWindow()
1446 {
1447 DestroyTree();
1448 delete m_renameTimer;
1449 }
1450
1451
1452 #if wxUSE_DRAG_AND_DROP
1453 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat &format )
1454 {
1455 m_dragFormat = format;
1456 m_dragEnabled = format != wxDF_INVALID;
1457
1458 return true;
1459 }
1460
1461 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat &format )
1462 {
1463 m_dropFormat = format;
1464 m_dropEnabled = format != wxDF_INVALID;
1465
1466 if (m_dropEnabled)
1467 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format ), this ) );
1468
1469 return true;
1470 }
1471
1472 void wxDataViewMainWindow::RemoveDropHint()
1473 {
1474 if (m_dropHint)
1475 {
1476 m_dropHint = false;
1477 RefreshRow( m_dropHintLine );
1478 m_dropHintLine = (unsigned int) -1;
1479 }
1480 }
1481
1482 wxDragResult wxDataViewMainWindow::OnDragOver( wxDataFormat format, wxCoord x,
1483 wxCoord y, wxDragResult def )
1484 {
1485 int xx = x;
1486 int yy = y;
1487 m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy );
1488 unsigned int row = GetLineAt( yy );
1489
1490 if ((row >= GetRowCount()) || (xx > GetEndOfLastCol()))
1491 {
1492 RemoveDropHint();
1493 return wxDragNone;
1494 }
1495
1496 wxDataViewItem item = GetItemByRow( row );
1497
1498 wxDataViewModel *model = GetModel();
1499
1500 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
1501 event.SetEventObject( m_owner );
1502 event.SetItem( item );
1503 event.SetModel( model );
1504 event.SetDataFormat( format );
1505 event.SetDropEffect( def );
1506 if (!m_owner->HandleWindowEvent( event ))
1507 {
1508 RemoveDropHint();
1509 return wxDragNone;
1510 }
1511
1512 if (!event.IsAllowed())
1513 {
1514 RemoveDropHint();
1515 return wxDragNone;
1516 }
1517
1518
1519 if (m_dropHint && (row != m_dropHintLine))
1520 RefreshRow( m_dropHintLine );
1521 m_dropHint = true;
1522 m_dropHintLine = row;
1523 RefreshRow( row );
1524
1525 return def;
1526 }
1527
1528 bool wxDataViewMainWindow::OnDrop( wxDataFormat format, wxCoord x, wxCoord y )
1529 {
1530 RemoveDropHint();
1531
1532 int xx = x;
1533 int yy = y;
1534 m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy );
1535 unsigned int row = GetLineAt( yy );
1536
1537 if ((row >= GetRowCount()) || (xx > GetEndOfLastCol()))
1538 return false;
1539
1540 wxDataViewItem item = GetItemByRow( row );
1541
1542 wxDataViewModel *model = GetModel();
1543
1544 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
1545 event.SetEventObject( m_owner );
1546 event.SetItem( item );
1547 event.SetModel( model );
1548 event.SetDataFormat( format );
1549 if (!m_owner->HandleWindowEvent( event ))
1550 return false;
1551
1552 if (!event.IsAllowed())
1553 return false;
1554
1555 return true;
1556 }
1557
1558 wxDragResult wxDataViewMainWindow::OnData( wxDataFormat format, wxCoord x, wxCoord y,
1559 wxDragResult def )
1560 {
1561 int xx = x;
1562 int yy = y;
1563 m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy );
1564 unsigned int row = GetLineAt( yy );
1565
1566 if ((row >= GetRowCount()) || (xx > GetEndOfLastCol()))
1567 return wxDragNone;
1568
1569 wxDataViewItem item = GetItemByRow( row );
1570
1571 wxDataViewModel *model = GetModel();
1572
1573 wxCustomDataObject *obj = (wxCustomDataObject *) GetDropTarget()->GetDataObject();
1574
1575 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() );
1576 event.SetEventObject( m_owner );
1577 event.SetItem( item );
1578 event.SetModel( model );
1579 event.SetDataFormat( format );
1580 event.SetDataSize( obj->GetSize() );
1581 event.SetDataBuffer( obj->GetData() );
1582 event.SetDropEffect( def );
1583 if (!m_owner->HandleWindowEvent( event ))
1584 return wxDragNone;
1585
1586 if (!event.IsAllowed())
1587 return wxDragNone;
1588
1589 return def;
1590 }
1591
1592 void wxDataViewMainWindow::OnLeave()
1593 {
1594 RemoveDropHint();
1595 }
1596
1597 wxBitmap wxDataViewMainWindow::CreateItemBitmap( unsigned int row, int &indent )
1598 {
1599 int height = GetLineHeight( row );
1600 int width = 0;
1601 unsigned int cols = GetOwner()->GetColumnCount();
1602 unsigned int col;
1603 for (col = 0; col < cols; col++)
1604 {
1605 wxDataViewColumn *column = GetOwner()->GetColumnAt(col);
1606 if (column->IsHidden())
1607 continue; // skip it!
1608 width += column->GetWidth();
1609 }
1610
1611 indent = 0;
1612 if (!IsList())
1613 {
1614 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
1615 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
1616 indent = indent + m_lineHeight;
1617 // try to use the m_lineHeight as the expander space
1618 }
1619 width -= indent;
1620
1621 wxBitmap bitmap( width, height );
1622 wxMemoryDC dc( bitmap );
1623 dc.SetFont( GetFont() );
1624 dc.SetPen( *wxBLACK_PEN );
1625 dc.SetBrush( *wxWHITE_BRUSH );
1626 dc.DrawRectangle( 0,0,width,height );
1627
1628 wxDataViewModel *model = m_owner->GetModel();
1629
1630 wxDataViewColumn * const
1631 expander = GetExpanderColumnOrFirstOne(GetOwner());
1632
1633 int x = 0;
1634 for (col = 0; col < cols; col++)
1635 {
1636 wxDataViewColumn *column = GetOwner()->GetColumnAt( col );
1637 wxDataViewRenderer *cell = column->GetRenderer();
1638
1639 if (column->IsHidden())
1640 continue; // skip it!
1641
1642 width = column->GetWidth();
1643
1644 if (column == expander)
1645 width -= indent;
1646
1647 wxDataViewItem item = GetItemByRow( row );
1648 cell->PrepareForItem(model, item, column->GetModelColumn());
1649
1650 wxRect item_rect(x, 0, width, height);
1651 item_rect.Deflate(PADDING_RIGHTLEFT, 0);
1652
1653 // dc.SetClippingRegion( item_rect );
1654 cell->WXCallRender(item_rect, &dc, 0);
1655 // dc.DestroyClippingRegion();
1656
1657 x += width;
1658 }
1659
1660 return bitmap;
1661 }
1662
1663 #endif // wxUSE_DRAG_AND_DROP
1664
1665
1666 // Draw focus rect for individual cell. Unlike native focus rect, we render
1667 // this in foreground text color (typically white) to enhance contrast and
1668 // make it visible.
1669 static void DrawSelectedCellFocusRect(wxDC& dc, const wxRect& rect)
1670 {
1671 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1672
1673 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1674 // may be short traits and not really dots
1675 //
1676 // note that to behave in the same manner as DrawRect(), we must exclude
1677 // the bottom and right borders from the rectangle
1678 wxCoord x1 = rect.GetLeft(),
1679 y1 = rect.GetTop(),
1680 x2 = rect.GetRight(),
1681 y2 = rect.GetBottom();
1682
1683 wxDCPenChanger pen(dc, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
1684
1685 wxCoord z;
1686 for ( z = x1 + 1; z < x2; z += 2 )
1687 dc.DrawPoint(z, rect.GetTop());
1688
1689 wxCoord shift = z == x2 ? 0 : 1;
1690 for ( z = y1 + shift; z < y2; z += 2 )
1691 dc.DrawPoint(x2, z);
1692
1693 shift = z == y2 ? 0 : 1;
1694 for ( z = x2 - shift; z > x1; z -= 2 )
1695 dc.DrawPoint(z, y2);
1696
1697 shift = z == x1 ? 0 : 1;
1698 for ( z = y2 - shift; z > y1; z -= 2 )
1699 dc.DrawPoint(x1, z);
1700 }
1701
1702
1703 void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1704 {
1705 wxDataViewModel *model = GetModel();
1706 wxAutoBufferedPaintDC dc( this );
1707
1708 dc.SetBrush(GetOwner()->GetBackgroundColour());
1709 dc.SetPen( *wxTRANSPARENT_PEN );
1710 dc.DrawRectangle(GetClientSize());
1711
1712 if ( IsEmpty() )
1713 {
1714 // No items to draw.
1715 return;
1716 }
1717
1718 // prepare the DC
1719 GetOwner()->PrepareDC( dc );
1720 dc.SetFont( GetFont() );
1721
1722 wxRect update = GetUpdateRegion().GetBox();
1723 m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
1724
1725 // compute which items needs to be redrawn
1726 unsigned int item_start = GetLineAt( wxMax(0,update.y) );
1727 unsigned int item_count =
1728 wxMin( (int)( GetLineAt( wxMax(0,update.y+update.height) ) - item_start + 1),
1729 (int)(GetRowCount( ) - item_start));
1730 unsigned int item_last = item_start + item_count;
1731
1732 // Send the event to wxDataViewCtrl itself.
1733 wxWindow * const parent = GetParent();
1734 wxDataViewEvent cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT, parent->GetId());
1735 cache_event.SetEventObject(parent);
1736 cache_event.SetCache(item_start, item_last - 1);
1737 parent->ProcessWindowEvent(cache_event);
1738
1739 // compute which columns needs to be redrawn
1740 unsigned int cols = GetOwner()->GetColumnCount();
1741 if ( !cols )
1742 {
1743 // we assume that we have at least one column below and painting an
1744 // empty control is unnecessary anyhow
1745 return;
1746 }
1747
1748 unsigned int col_start = 0;
1749 unsigned int x_start;
1750 for (x_start = 0; col_start < cols; col_start++)
1751 {
1752 wxDataViewColumn *col = GetOwner()->GetColumnAt(col_start);
1753 if (col->IsHidden())
1754 continue; // skip it!
1755
1756 unsigned int w = col->GetWidth();
1757 if (x_start+w >= (unsigned int)update.x)
1758 break;
1759
1760 x_start += w;
1761 }
1762
1763 unsigned int col_last = col_start;
1764 unsigned int x_last = x_start;
1765 for (; col_last < cols; col_last++)
1766 {
1767 wxDataViewColumn *col = GetOwner()->GetColumnAt(col_last);
1768 if (col->IsHidden())
1769 continue; // skip it!
1770
1771 if (x_last > (unsigned int)update.GetRight())
1772 break;
1773
1774 x_last += col->GetWidth();
1775 }
1776
1777 // Draw background of alternate rows specially if required
1778 if ( m_owner->HasFlag(wxDV_ROW_LINES) )
1779 {
1780 wxColour altRowColour = m_owner->m_alternateRowColour;
1781 if ( !altRowColour.IsOk() )
1782 {
1783 // Determine the alternate rows colour automatically from the
1784 // background colour.
1785 const wxColour bgColour = m_owner->GetBackgroundColour();
1786
1787 // Depending on the background, alternate row color
1788 // will be 3% more dark or 50% brighter.
1789 int alpha = bgColour.GetRGB() > 0x808080 ? 97 : 150;
1790 altRowColour = bgColour.ChangeLightness(alpha);
1791 }
1792
1793 dc.SetPen(*wxTRANSPARENT_PEN);
1794 dc.SetBrush(wxBrush(altRowColour));
1795
1796 for (unsigned int item = item_start; item < item_last; item++)
1797 {
1798 if ( item % 2 )
1799 {
1800 dc.DrawRectangle(x_start,
1801 GetLineStart(item),
1802 GetClientSize().GetWidth(),
1803 GetLineHeight(item));
1804 }
1805 }
1806 }
1807
1808 // Draw horizontal rules if required
1809 if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
1810 {
1811 dc.SetPen(m_penRule);
1812 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1813
1814 for (unsigned int i = item_start; i <= item_last; i++)
1815 {
1816 int y = GetLineStart( i );
1817 dc.DrawLine(x_start, y, x_last, y);
1818 }
1819 }
1820
1821 // Draw vertical rules if required
1822 if ( m_owner->HasFlag(wxDV_VERT_RULES) )
1823 {
1824 dc.SetPen(m_penRule);
1825 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1826
1827 // NB: Vertical rules are drawn in the last pixel of a column so that
1828 // they align perfectly with native MSW wxHeaderCtrl as well as for
1829 // consistency with MSW native list control. There's no vertical
1830 // rule at the most-left side of the control.
1831
1832 int x = x_start - 1;
1833 for (unsigned int i = col_start; i < col_last; i++)
1834 {
1835 wxDataViewColumn *col = GetOwner()->GetColumnAt(i);
1836 if (col->IsHidden())
1837 continue; // skip it
1838
1839 x += col->GetWidth();
1840
1841 dc.DrawLine(x, GetLineStart( item_start ),
1842 x, GetLineStart( item_last ) );
1843 }
1844 }
1845
1846 // redraw the background for the items which are selected/current
1847 for (unsigned int item = item_start; item < item_last; item++)
1848 {
1849 bool selected = m_selection.Index( item ) != wxNOT_FOUND;
1850
1851 if (selected || item == m_currentRow)
1852 {
1853 wxRect rect( x_start, GetLineStart( item ),
1854 x_last - x_start, GetLineHeight( item ) );
1855
1856 // draw selection and whole-item focus:
1857 if ( selected )
1858 {
1859 int flags = wxCONTROL_SELECTED;
1860 if (m_hasFocus)
1861 flags |= wxCONTROL_FOCUSED;
1862
1863 wxRendererNative::Get().DrawItemSelectionRect
1864 (
1865 this,
1866 dc,
1867 rect,
1868 flags
1869 );
1870 }
1871
1872 // draw keyboard focus rect if applicable
1873 if ( item == m_currentRow && m_hasFocus )
1874 {
1875 bool renderColumnFocus = false;
1876
1877 if ( m_useCellFocus && m_currentCol && m_currentColSetByKeyboard )
1878 {
1879 renderColumnFocus = true;
1880
1881 // If this is container node without columns, render full-row focus:
1882 if ( !IsList() )
1883 {
1884 wxDataViewTreeNode *node = GetTreeNodeByRow(item);
1885 if ( node->HasChildren() && !model->HasContainerColumns(node->GetItem()) )
1886 renderColumnFocus = false;
1887 }
1888 }
1889
1890 if ( renderColumnFocus )
1891 {
1892 for ( unsigned int i = col_start; i < col_last; i++ )
1893 {
1894 wxDataViewColumn *col = GetOwner()->GetColumnAt(i);
1895 if ( col->IsHidden() )
1896 continue;
1897
1898 rect.width = col->GetWidth();
1899
1900 if ( col == m_currentCol )
1901 {
1902 // make the rect more visible by adding a small
1903 // margin around it:
1904 rect.Deflate(1, 1);
1905
1906 if ( selected )
1907 {
1908 // DrawFocusRect() uses XOR and is all but
1909 // invisible against dark-blue background. Use
1910 // the same color used for selected text.
1911 DrawSelectedCellFocusRect(dc, rect);
1912 }
1913 else
1914 {
1915 wxRendererNative::Get().DrawFocusRect
1916 (
1917 this,
1918 dc,
1919 rect,
1920 0
1921 );
1922 }
1923 break;
1924 }
1925
1926 rect.x += rect.width;
1927 }
1928 }
1929 else
1930 {
1931 // render focus rectangle for the whole row
1932 wxRendererNative::Get().DrawFocusRect
1933 (
1934 this,
1935 dc,
1936 rect,
1937 selected ? (int)wxCONTROL_SELECTED : 0
1938 );
1939 }
1940 }
1941 }
1942 }
1943
1944 #if wxUSE_DRAG_AND_DROP
1945 if (m_dropHint)
1946 {
1947 wxRect rect( x_start, GetLineStart( m_dropHintLine ),
1948 x_last - x_start, GetLineHeight( m_dropHintLine ) );
1949 dc.SetPen( *wxBLACK_PEN );
1950 dc.SetBrush( *wxTRANSPARENT_BRUSH );
1951 dc.DrawRectangle( rect );
1952 }
1953 #endif // wxUSE_DRAG_AND_DROP
1954
1955 wxDataViewColumn * const
1956 expander = GetExpanderColumnOrFirstOne(GetOwner());
1957
1958 // redraw all cells for all rows which must be repainted and all columns
1959 wxRect cell_rect;
1960 cell_rect.x = x_start;
1961 for (unsigned int i = col_start; i < col_last; i++)
1962 {
1963 wxDataViewColumn *col = GetOwner()->GetColumnAt( i );
1964 wxDataViewRenderer *cell = col->GetRenderer();
1965 cell_rect.width = col->GetWidth();
1966
1967 if ( col->IsHidden() || cell_rect.width <= 0 )
1968 continue; // skip it!
1969
1970 for (unsigned int item = item_start; item < item_last; item++)
1971 {
1972 // get the cell value and set it into the renderer
1973 wxDataViewTreeNode *node = NULL;
1974 wxDataViewItem dataitem;
1975
1976 if (!IsVirtualList())
1977 {
1978 node = GetTreeNodeByRow(item);
1979 if( node == NULL )
1980 continue;
1981
1982 dataitem = node->GetItem();
1983
1984 // Skip all columns of "container" rows except the expander
1985 // column itself unless HasContainerColumns() overrides this.
1986 if ( col != expander &&
1987 model->IsContainer(dataitem) &&
1988 !model->HasContainerColumns(dataitem) )
1989 continue;
1990 }
1991 else
1992 {
1993 dataitem = wxDataViewItem( wxUIntToPtr(item+1) );
1994 }
1995
1996 cell->PrepareForItem(model, dataitem, col->GetModelColumn());
1997
1998 // update cell_rect
1999 cell_rect.y = GetLineStart( item );
2000 cell_rect.height = GetLineHeight( item );
2001
2002 // draw the background
2003 bool selected = m_selection.Index( item ) != wxNOT_FOUND;
2004 if ( !selected )
2005 DrawCellBackground( cell, dc, cell_rect );
2006
2007 // deal with the expander
2008 int indent = 0;
2009 if ((!IsList()) && (col == expander))
2010 {
2011 // Calculate the indent first
2012 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
2013
2014 // we reserve m_lineHeight of horizontal space for the expander
2015 // but leave EXPANDER_MARGIN around the expander itself
2016 int exp_x = cell_rect.x + indent + EXPANDER_MARGIN;
2017
2018 indent += m_lineHeight;
2019
2020 // draw expander if needed and visible
2021 if ( node->HasChildren() && exp_x < cell_rect.GetRight() )
2022 {
2023 dc.SetPen( m_penExpander );
2024 dc.SetBrush( wxNullBrush );
2025
2026 int exp_size = m_lineHeight - 2*EXPANDER_MARGIN;
2027 int exp_y = cell_rect.y + (cell_rect.height - exp_size)/2
2028 + EXPANDER_MARGIN - EXPANDER_OFFSET;
2029
2030 const wxRect rect(exp_x, exp_y, exp_size, exp_size);
2031
2032 int flag = 0;
2033 if ( m_underMouse == node )
2034 flag |= wxCONTROL_CURRENT;
2035 if ( node->IsOpen() )
2036 flag |= wxCONTROL_EXPANDED;
2037
2038 // ensure that we don't overflow the cell (which might
2039 // happen if the column is very narrow)
2040 wxDCClipper clip(dc, cell_rect);
2041
2042 wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag);
2043 }
2044
2045 // force the expander column to left-center align
2046 cell->SetAlignment( wxALIGN_CENTER_VERTICAL );
2047 }
2048
2049 wxRect item_rect = cell_rect;
2050 item_rect.Deflate(PADDING_RIGHTLEFT, 0);
2051
2052 // account for the tree indent (harmless if we're not indented)
2053 item_rect.x += indent;
2054 item_rect.width -= indent;
2055
2056 if ( item_rect.width <= 0 )
2057 continue;
2058
2059 int state = 0;
2060 if (m_hasFocus && selected)
2061 state |= wxDATAVIEW_CELL_SELECTED;
2062
2063 // TODO: it would be much more efficient to create a clipping
2064 // region for the entire column being rendered (in the OnPaint
2065 // of wxDataViewMainWindow) instead of a single clip region for
2066 // each cell. However it would mean that each renderer should
2067 // respect the given wxRect's top & bottom coords, eventually
2068 // violating only the left & right coords - however the user can
2069 // make its own renderer and thus we cannot be sure of that.
2070 wxDCClipper clip(dc, item_rect);
2071
2072 cell->WXCallRender(item_rect, &dc, state);
2073 }
2074
2075 cell_rect.x += cell_rect.width;
2076 }
2077 }
2078
2079
2080 void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer* cell, wxDC& dc, const wxRect& rect )
2081 {
2082 wxRect rectBg( rect );
2083
2084 // don't overlap the horizontal rules
2085 if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
2086 {
2087 rectBg.x++;
2088 rectBg.width--;
2089 }
2090
2091 // don't overlap the vertical rules
2092 if ( m_owner->HasFlag(wxDV_VERT_RULES) )
2093 {
2094 rectBg.y++;
2095 rectBg.height--;
2096 }
2097
2098 cell->RenderBackground(&dc, rectBg);
2099 }
2100
2101 void wxDataViewMainWindow::OnRenameTimer()
2102 {
2103 // We have to call this here because changes may just have
2104 // been made and no screen update taken place.
2105 if ( m_dirty )
2106 {
2107 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2108 // (needs to be tested!)
2109 wxSafeYield();
2110 }
2111
2112 wxDataViewItem item = GetItemByRow( m_currentRow );
2113
2114 StartEditing( item, m_currentCol );
2115 }
2116
2117 void
2118 wxDataViewMainWindow::StartEditing(const wxDataViewItem& item,
2119 const wxDataViewColumn* col)
2120 {
2121 wxDataViewRenderer* renderer = col->GetRenderer();
2122 if ( !IsCellEditableInMode(item, col, wxDATAVIEW_CELL_EDITABLE) )
2123 return;
2124
2125 const wxRect itemRect = GetItemRect(item, col);
2126 if ( renderer->StartEditing(item, itemRect) )
2127 {
2128 // Save the renderer to be able to finish/cancel editing it later and
2129 // save the control to be able to detect if we're still editing it.
2130 m_editorRenderer = renderer;
2131 m_editorCtrl = renderer->GetEditorCtrl();
2132 }
2133 }
2134
2135 //-----------------------------------------------------------------------------
2136 // Helper class for do operation on the tree node
2137 //-----------------------------------------------------------------------------
2138 class DoJob
2139 {
2140 public:
2141 DoJob() { }
2142 virtual ~DoJob() { }
2143
2144 // The return value control how the tree-walker tranverse the tree
2145 enum
2146 {
2147 DONE, // Job done, stop traversing and return
2148 SKIP_SUBTREE, // Ignore the current node's subtree and continue
2149 CONTINUE // Job not done, continue
2150 };
2151
2152 virtual int operator() ( wxDataViewTreeNode * node ) = 0;
2153 };
2154
2155 bool Walker( wxDataViewTreeNode * node, DoJob & func )
2156 {
2157 wxCHECK_MSG( node, false, "can't walk NULL node" );
2158
2159 switch( func( node ) )
2160 {
2161 case DoJob::DONE:
2162 return true;
2163 case DoJob::SKIP_SUBTREE:
2164 return false;
2165 case DoJob::CONTINUE:
2166 break;
2167 }
2168
2169 if ( node->HasChildren() )
2170 {
2171 const wxDataViewTreeNodes& nodes = node->GetChildNodes();
2172
2173 for ( wxDataViewTreeNodes::const_iterator i = nodes.begin();
2174 i != nodes.end();
2175 ++i )
2176 {
2177 if ( Walker(*i, func) )
2178 return true;
2179 }
2180 }
2181
2182 return false;
2183 }
2184
2185 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item)
2186 {
2187 if (IsVirtualList())
2188 {
2189 wxDataViewVirtualListModel *list_model =
2190 (wxDataViewVirtualListModel*) GetModel();
2191 m_count = list_model->GetCount();
2192 }
2193 else
2194 {
2195 SortPrepare();
2196
2197 wxDataViewTreeNode *parentNode = FindNode(parent);
2198
2199 if ( !parentNode )
2200 return false;
2201
2202 wxDataViewItemArray modelSiblings;
2203 GetModel()->GetChildren(parent, modelSiblings);
2204 const int modelSiblingsSize = modelSiblings.size();
2205
2206 int posInModel = modelSiblings.Index(item, /*fromEnd=*/true);
2207 wxCHECK_MSG( posInModel != wxNOT_FOUND, false, "adding non-existent item?" );
2208
2209 wxDataViewTreeNode *itemNode = new wxDataViewTreeNode(parentNode, item);
2210 itemNode->SetHasChildren(GetModel()->IsContainer(item));
2211
2212 parentNode->SetHasChildren(true);
2213
2214 const wxDataViewTreeNodes& nodeSiblings = parentNode->GetChildNodes();
2215 const int nodeSiblingsSize = nodeSiblings.size();
2216
2217 int nodePos = 0;
2218
2219 if ( posInModel == modelSiblingsSize - 1 )
2220 {
2221 nodePos = nodeSiblingsSize;
2222 }
2223 else if ( modelSiblingsSize == nodeSiblingsSize + 1 )
2224 {
2225 // This is the simple case when our node tree already matches the
2226 // model and only this one item is missing.
2227 nodePos = posInModel;
2228 }
2229 else
2230 {
2231 // It's possible that a larger discrepancy between the model and
2232 // our realization exists. This can happen e.g. when adding a bunch
2233 // of items to the model and then calling ItemsAdded() just once
2234 // afterwards. In this case, we must find the right position by
2235 // looking at sibling items.
2236
2237 // append to the end if we won't find a better position:
2238 nodePos = nodeSiblingsSize;
2239
2240 for ( int nextItemPos = posInModel + 1;
2241 nextItemPos < modelSiblingsSize;
2242 nextItemPos++ )
2243 {
2244 int nextNodePos = parentNode->FindChildByItem(modelSiblings[nextItemPos]);
2245 if ( nextNodePos != wxNOT_FOUND )
2246 {
2247 nodePos = nextNodePos;
2248 break;
2249 }
2250 }
2251 }
2252
2253 parentNode->ChangeSubTreeCount(+1);
2254 parentNode->InsertChild(itemNode, nodePos);
2255
2256 m_count = -1;
2257 }
2258
2259 GetOwner()->InvalidateColBestWidths();
2260 UpdateDisplay();
2261
2262 return true;
2263 }
2264
2265 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent,
2266 const wxDataViewItem& item)
2267 {
2268 if (IsVirtualList())
2269 {
2270 wxDataViewVirtualListModel *list_model =
2271 (wxDataViewVirtualListModel*) GetModel();
2272 m_count = list_model->GetCount();
2273
2274 if ( !m_selection.empty() )
2275 {
2276 const int row = GetRowByItem(item);
2277
2278 int rowIndexInSelection = wxNOT_FOUND;
2279
2280 const size_t selCount = m_selection.size();
2281 for ( size_t i = 0; i < selCount; i++ )
2282 {
2283 if ( m_selection[i] == (unsigned)row )
2284 rowIndexInSelection = i;
2285 else if ( m_selection[i] > (unsigned)row )
2286 m_selection[i]--;
2287 }
2288
2289 if ( rowIndexInSelection != wxNOT_FOUND )
2290 m_selection.RemoveAt(rowIndexInSelection);
2291 }
2292
2293 }
2294 else // general case
2295 {
2296 wxDataViewTreeNode *parentNode = FindNode(parent);
2297
2298 // Notice that it is possible that the item being deleted is not in the
2299 // tree at all, for example we could be deleting a never shown (because
2300 // collapsed) item in a tree model. So it's not an error if we don't know
2301 // about this item, just return without doing anything then.
2302 if ( !parentNode )
2303 return true;
2304
2305 wxCHECK_MSG( parentNode->HasChildren(), false, "parent node doesn't have children?" );
2306 const wxDataViewTreeNodes& parentsChildren = parentNode->GetChildNodes();
2307
2308 // We can't use FindNode() to find 'item', because it was already
2309 // removed from the model by the time ItemDeleted() is called, so we
2310 // have to do it manually. We keep track of its position as well for
2311 // later use.
2312 int itemPosInNode = 0;
2313 wxDataViewTreeNode *itemNode = NULL;
2314 for ( wxDataViewTreeNodes::const_iterator i = parentsChildren.begin();
2315 i != parentsChildren.end();
2316 ++i, ++itemPosInNode )
2317 {
2318 if( (*i)->GetItem() == item )
2319 {
2320 itemNode = *i;
2321 break;
2322 }
2323 }
2324
2325 // If the parent wasn't expanded, it's possible that we didn't have a
2326 // node corresponding to 'item' and so there's nothing left to do.
2327 if ( !itemNode )
2328 {
2329 // If this was the last child to be removed, it's possible the parent
2330 // node became a leaf. Let's ask the model about it.
2331 if ( parentNode->GetChildNodes().empty() )
2332 parentNode->SetHasChildren(GetModel()->IsContainer(parent));
2333
2334 return true;
2335 }
2336
2337 // Delete the item from wxDataViewTreeNode representation:
2338 const int itemsDeleted = 1 + itemNode->GetSubTreeCount();
2339
2340 parentNode->RemoveChild(itemNode);
2341 delete itemNode;
2342 parentNode->ChangeSubTreeCount(-itemsDeleted);
2343
2344 // Make the row number invalid and get a new valid one when user call GetRowCount
2345 m_count = -1;
2346
2347 // If this was the last child to be removed, it's possible the parent
2348 // node became a leaf. Let's ask the model about it.
2349 if ( parentNode->GetChildNodes().empty() )
2350 {
2351 bool isContainer = GetModel()->IsContainer(parent);
2352 parentNode->SetHasChildren(isContainer);
2353 if ( isContainer )
2354 {
2355 // If it's still a container, make sure we show "+" icon for it
2356 // and not "-" one as there is nothing to collapse any more.
2357 if ( parentNode->IsOpen() )
2358 parentNode->ToggleOpen();
2359 }
2360 }
2361
2362 // Update selection by removing 'item' and its entire children tree from the selection.
2363 if ( !m_selection.empty() )
2364 {
2365 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2366 // the parent ('parentNode') and position in its list of children
2367 int itemRow;
2368 if ( itemPosInNode == 0 )
2369 {
2370 // 1st child, row number is that of the parent parentNode + 1
2371 itemRow = GetRowByItem(parentNode->GetItem()) + 1;
2372 }
2373 else
2374 {
2375 // row number is that of the sibling above 'item' + its subtree if any + 1
2376 const wxDataViewTreeNode *siblingNode = parentNode->GetChildNodes()[itemPosInNode - 1];
2377
2378 itemRow = GetRowByItem(siblingNode->GetItem()) +
2379 siblingNode->GetSubTreeCount() +
2380 1;
2381 }
2382
2383 wxDataViewSelection newsel(wxDataViewSelectionCmp);
2384
2385 const size_t numSelections = m_selection.size();
2386 for ( size_t i = 0; i < numSelections; ++i )
2387 {
2388 const int s = m_selection[i];
2389 if ( s < itemRow )
2390 newsel.push_back(s);
2391 else if ( s >= itemRow + itemsDeleted )
2392 newsel.push_back(s - itemsDeleted);
2393 // else: deleted item, remove from selection
2394 }
2395
2396 m_selection = newsel;
2397 }
2398 }
2399
2400 // Change the current row to the last row if the current exceed the max row number
2401 if ( m_currentRow >= GetRowCount() )
2402 ChangeCurrentRow(m_count - 1);
2403
2404 GetOwner()->InvalidateColBestWidths();
2405 UpdateDisplay();
2406
2407 return true;
2408 }
2409
2410 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item)
2411 {
2412 SortPrepare();
2413 g_model->Resort();
2414
2415 GetOwner()->InvalidateColBestWidths();
2416
2417 // Send event
2418 wxWindow *parent = GetParent();
2419 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
2420 le.SetEventObject(parent);
2421 le.SetModel(GetModel());
2422 le.SetItem(item);
2423 parent->ProcessWindowEvent(le);
2424
2425 return true;
2426 }
2427
2428 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int model_column )
2429 {
2430 int view_column = -1;
2431 unsigned int n_col = m_owner->GetColumnCount();
2432 for (unsigned i = 0; i < n_col; i++)
2433 {
2434 wxDataViewColumn *column = m_owner->GetColumn( i );
2435 if (column->GetModelColumn() == model_column)
2436 {
2437 view_column = (int) i;
2438 break;
2439 }
2440 }
2441 if (view_column == -1)
2442 return false;
2443
2444 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2445 /*#define MAX_VIRTUAL_WIDTH 100000
2446
2447 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2448 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2449 Refresh( true, &rect );
2450
2451 return true;
2452 */
2453 SortPrepare();
2454 g_model->Resort();
2455
2456 GetOwner()->InvalidateColBestWidth(view_column);
2457
2458 // Send event
2459 wxWindow *parent = GetParent();
2460 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
2461 le.SetEventObject(parent);
2462 le.SetModel(GetModel());
2463 le.SetItem(item);
2464 le.SetColumn(view_column);
2465 le.SetDataViewColumn(GetOwner()->GetColumn(view_column));
2466 parent->ProcessWindowEvent(le);
2467
2468 return true;
2469 }
2470
2471 bool wxDataViewMainWindow::Cleared()
2472 {
2473 DestroyTree();
2474 m_selection.Clear();
2475 m_currentRow = (unsigned)-1;
2476
2477 if (GetModel())
2478 {
2479 SortPrepare();
2480 BuildTree( GetModel() );
2481 }
2482 else
2483 {
2484 m_count = 0;
2485 }
2486
2487 GetOwner()->InvalidateColBestWidths();
2488 UpdateDisplay();
2489
2490 return true;
2491 }
2492
2493 void wxDataViewMainWindow::UpdateDisplay()
2494 {
2495 m_dirty = true;
2496 m_underMouse = NULL;
2497 }
2498
2499 void wxDataViewMainWindow::OnInternalIdle()
2500 {
2501 wxWindow::OnInternalIdle();
2502
2503 if (m_dirty)
2504 {
2505 RecalculateDisplay();
2506 m_dirty = false;
2507 }
2508 }
2509
2510 void wxDataViewMainWindow::RecalculateDisplay()
2511 {
2512 wxDataViewModel *model = GetModel();
2513 if (!model)
2514 {
2515 Refresh();
2516 return;
2517 }
2518
2519 int width = GetEndOfLastCol();
2520 int height = GetLineStart( GetRowCount() );
2521
2522 SetVirtualSize( width, height );
2523 GetOwner()->SetScrollRate( 10, m_lineHeight );
2524
2525 Refresh();
2526 }
2527
2528 void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
2529 {
2530 m_underMouse = NULL;
2531
2532 wxWindow::ScrollWindow( dx, dy, rect );
2533
2534 if (GetOwner()->m_headerArea)
2535 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
2536 }
2537
2538 void wxDataViewMainWindow::ScrollTo( int rows, int column )
2539 {
2540 m_underMouse = NULL;
2541
2542 int x, y;
2543 m_owner->GetScrollPixelsPerUnit( &x, &y );
2544 int sy = GetLineStart( rows )/y;
2545 int sx = -1;
2546 if( column != -1 )
2547 {
2548 wxRect rect = GetClientRect();
2549 int colnum = 0;
2550 int x_start, w = 0;
2551 int xx, yy, xe;
2552 m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy );
2553 for (x_start = 0; colnum < column; colnum++)
2554 {
2555 wxDataViewColumn *col = GetOwner()->GetColumnAt(colnum);
2556 if (col->IsHidden())
2557 continue; // skip it!
2558
2559 w = col->GetWidth();
2560 x_start += w;
2561 }
2562
2563 int x_end = x_start + w;
2564 xe = xx + rect.width;
2565 if( x_end > xe )
2566 {
2567 sx = ( xx + x_end - xe )/x;
2568 }
2569 if( x_start < xx )
2570 {
2571 sx = x_start/x;
2572 }
2573 }
2574 m_owner->Scroll( sx, sy );
2575 }
2576
2577 int wxDataViewMainWindow::GetCountPerPage() const
2578 {
2579 wxSize size = GetClientSize();
2580 return size.y / m_lineHeight;
2581 }
2582
2583 int wxDataViewMainWindow::GetEndOfLastCol() const
2584 {
2585 int width = 0;
2586 unsigned int i;
2587 for (i = 0; i < GetOwner()->GetColumnCount(); i++)
2588 {
2589 const wxDataViewColumn *c =
2590 const_cast<wxDataViewCtrl*>(GetOwner())->GetColumnAt( i );
2591
2592 if (!c->IsHidden())
2593 width += c->GetWidth();
2594 }
2595 return width;
2596 }
2597
2598 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2599 {
2600 int x = 0;
2601 int y = 0;
2602 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
2603
2604 return GetLineAt( y );
2605 }
2606
2607 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2608 {
2609 wxSize client_size = GetClientSize();
2610 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
2611 &client_size.x, &client_size.y );
2612
2613 // we should deal with the pixel here
2614 unsigned int row = GetLineAt(client_size.y) - 1;
2615
2616 return wxMin( GetRowCount()-1, row );
2617 }
2618
2619 unsigned int wxDataViewMainWindow::GetRowCount() const
2620 {
2621 if ( m_count == -1 )
2622 {
2623 wxDataViewMainWindow* const
2624 self = const_cast<wxDataViewMainWindow*>(this);
2625 self->m_count = RecalculateCount();
2626 self->UpdateDisplay();
2627 }
2628 return m_count;
2629 }
2630
2631 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
2632 {
2633 m_currentRow = row;
2634
2635 // send event
2636 }
2637
2638 void wxDataViewMainWindow::SelectAllRows( bool on )
2639 {
2640 if (IsEmpty())
2641 return;
2642
2643 if (on)
2644 {
2645 m_selection.Clear();
2646 for (unsigned int i = 0; i < GetRowCount(); i++)
2647 m_selection.Add( i );
2648 Refresh();
2649 }
2650 else
2651 {
2652 unsigned int first_visible = GetFirstVisibleRow();
2653 unsigned int last_visible = GetLastVisibleRow();
2654 unsigned int i;
2655 for (i = 0; i < m_selection.GetCount(); i++)
2656 {
2657 unsigned int row = m_selection[i];
2658 if ((row >= first_visible) && (row <= last_visible))
2659 RefreshRow( row );
2660 }
2661 m_selection.Clear();
2662 }
2663 }
2664
2665 void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
2666 {
2667 if (m_selection.Index( row ) == wxNOT_FOUND)
2668 {
2669 if (on)
2670 {
2671 m_selection.Add( row );
2672 RefreshRow( row );
2673 }
2674 }
2675 else
2676 {
2677 if (!on)
2678 {
2679 m_selection.Remove( row );
2680 RefreshRow( row );
2681 }
2682 }
2683 }
2684
2685 void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
2686 {
2687 if (from > to)
2688 {
2689 unsigned int tmp = from;
2690 from = to;
2691 to = tmp;
2692 }
2693
2694 unsigned int i;
2695 for (i = from; i <= to; i++)
2696 {
2697 if (m_selection.Index( i ) == wxNOT_FOUND)
2698 {
2699 if (on)
2700 m_selection.Add( i );
2701 }
2702 else
2703 {
2704 if (!on)
2705 m_selection.Remove( i );
2706 }
2707 }
2708 RefreshRows( from, to );
2709 }
2710
2711 void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
2712 {
2713 for (size_t i=0; i < aSelections.GetCount(); i++)
2714 {
2715 int n = aSelections[i];
2716
2717 m_selection.Add( n );
2718 RefreshRow( n );
2719 }
2720 }
2721
2722 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
2723 {
2724 if (m_selection.Index( row ) == wxNOT_FOUND)
2725 m_selection.Add( row );
2726 else
2727 m_selection.Remove( row );
2728 RefreshRow( row );
2729 }
2730
2731 bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
2732 {
2733 return (m_selection.Index( row ) != wxNOT_FOUND);
2734 }
2735
2736 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem& item)
2737 {
2738 wxWindow *parent = GetParent();
2739 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, parent->GetId());
2740
2741 le.SetEventObject(parent);
2742 le.SetModel(GetModel());
2743 le.SetItem( item );
2744
2745 parent->ProcessWindowEvent(le);
2746 }
2747
2748 void wxDataViewMainWindow::RefreshRow( unsigned int row )
2749 {
2750 wxRect rect( 0, GetLineStart( row ), GetEndOfLastCol(), GetLineHeight( row ) );
2751 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2752
2753 wxSize client_size = GetClientSize();
2754 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2755 wxRect intersect_rect = client_rect.Intersect( rect );
2756 if (intersect_rect.width > 0)
2757 Refresh( true, &intersect_rect );
2758 }
2759
2760 void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
2761 {
2762 if (from > to)
2763 {
2764 unsigned int tmp = to;
2765 to = from;
2766 from = tmp;
2767 }
2768
2769 wxRect rect( 0, GetLineStart( from ), GetEndOfLastCol(), GetLineStart( (to-from+1) ) );
2770 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2771
2772 wxSize client_size = GetClientSize();
2773 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2774 wxRect intersect_rect = client_rect.Intersect( rect );
2775 if (intersect_rect.width > 0)
2776 Refresh( true, &intersect_rect );
2777 }
2778
2779 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
2780 {
2781 wxSize client_size = GetClientSize();
2782 int start = GetLineStart( firstRow );
2783 m_owner->CalcScrolledPosition( start, 0, &start, NULL );
2784 if (start > client_size.y) return;
2785
2786 wxRect rect( 0, start, client_size.x, client_size.y - start );
2787
2788 Refresh( true, &rect );
2789 }
2790
2791 wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
2792 {
2793 wxRect rect;
2794 rect.x = 0;
2795 rect.y = GetLineStart( row );
2796 rect.width = GetEndOfLastCol();
2797 rect.height = GetLineHeight( row );
2798
2799 return rect;
2800 }
2801
2802 int wxDataViewMainWindow::GetLineStart( unsigned int row ) const
2803 {
2804 const wxDataViewModel *model = GetModel();
2805
2806 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
2807 {
2808 // TODO make more efficient
2809
2810 int start = 0;
2811
2812 unsigned int r;
2813 for (r = 0; r < row; r++)
2814 {
2815 const wxDataViewTreeNode* node = GetTreeNodeByRow(r);
2816 if (!node) return start;
2817
2818 wxDataViewItem item = node->GetItem();
2819
2820 unsigned int cols = GetOwner()->GetColumnCount();
2821 unsigned int col;
2822 int height = m_lineHeight;
2823 for (col = 0; col < cols; col++)
2824 {
2825 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2826 if (column->IsHidden())
2827 continue; // skip it!
2828
2829 if ((col != 0) &&
2830 model->IsContainer(item) &&
2831 !model->HasContainerColumns(item))
2832 continue; // skip it!
2833
2834 wxDataViewRenderer *renderer =
2835 const_cast<wxDataViewRenderer*>(column->GetRenderer());
2836 renderer->PrepareForItem(model, item, column->GetModelColumn());
2837
2838 height = wxMax( height, renderer->GetSize().y );
2839 }
2840
2841 start += height;
2842 }
2843
2844 return start;
2845 }
2846 else
2847 {
2848 return row * m_lineHeight;
2849 }
2850 }
2851
2852 int wxDataViewMainWindow::GetLineAt( unsigned int y ) const
2853 {
2854 const wxDataViewModel *model = GetModel();
2855
2856 // check for the easy case first
2857 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) )
2858 return y / m_lineHeight;
2859
2860 // TODO make more efficient
2861 unsigned int row = 0;
2862 unsigned int yy = 0;
2863 for (;;)
2864 {
2865 const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
2866 if (!node)
2867 {
2868 // not really correct...
2869 return row + ((y-yy) / m_lineHeight);
2870 }
2871
2872 wxDataViewItem item = node->GetItem();
2873
2874 unsigned int cols = GetOwner()->GetColumnCount();
2875 unsigned int col;
2876 int height = m_lineHeight;
2877 for (col = 0; col < cols; col++)
2878 {
2879 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2880 if (column->IsHidden())
2881 continue; // skip it!
2882
2883 if ((col != 0) &&
2884 model->IsContainer(item) &&
2885 !model->HasContainerColumns(item))
2886 continue; // skip it!
2887
2888 wxDataViewRenderer *renderer =
2889 const_cast<wxDataViewRenderer*>(column->GetRenderer());
2890 renderer->PrepareForItem(model, item, column->GetModelColumn());
2891
2892 height = wxMax( height, renderer->GetSize().y );
2893 }
2894
2895 yy += height;
2896 if (y < yy)
2897 return row;
2898
2899 row++;
2900 }
2901 }
2902
2903 int wxDataViewMainWindow::GetLineHeight( unsigned int row ) const
2904 {
2905 const wxDataViewModel *model = GetModel();
2906
2907 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
2908 {
2909 wxASSERT( !IsVirtualList() );
2910
2911 const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
2912 // wxASSERT( node );
2913 if (!node) return m_lineHeight;
2914
2915 wxDataViewItem item = node->GetItem();
2916
2917 int height = m_lineHeight;
2918
2919 unsigned int cols = GetOwner()->GetColumnCount();
2920 unsigned int col;
2921 for (col = 0; col < cols; col++)
2922 {
2923 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2924 if (column->IsHidden())
2925 continue; // skip it!
2926
2927 if ((col != 0) &&
2928 model->IsContainer(item) &&
2929 !model->HasContainerColumns(item))
2930 continue; // skip it!
2931
2932 wxDataViewRenderer *renderer =
2933 const_cast<wxDataViewRenderer*>(column->GetRenderer());
2934 renderer->PrepareForItem(model, item, column->GetModelColumn());
2935
2936 height = wxMax( height, renderer->GetSize().y );
2937 }
2938
2939 return height;
2940 }
2941 else
2942 {
2943 return m_lineHeight;
2944 }
2945 }
2946
2947
2948 class RowToTreeNodeJob: public DoJob
2949 {
2950 public:
2951 RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node )
2952 {
2953 this->row = row;
2954 this->current = current;
2955 ret = NULL;
2956 parent = node;
2957 }
2958
2959 virtual int operator() ( wxDataViewTreeNode * node )
2960 {
2961 current ++;
2962 if( current == static_cast<int>(row))
2963 {
2964 ret = node;
2965 return DoJob::DONE;
2966 }
2967
2968 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2969 {
2970 current += node->GetSubTreeCount();
2971 return DoJob::SKIP_SUBTREE;
2972 }
2973 else
2974 {
2975 parent = node;
2976
2977 // If the current node has only leaf children, we can find the
2978 // desired node directly. This can speed up finding the node
2979 // in some cases, and will have a very good effect for list views.
2980 if ( node->HasChildren() &&
2981 (int)node->GetChildNodes().size() == node->GetSubTreeCount() )
2982 {
2983 const int index = static_cast<int>(row) - current - 1;
2984 ret = node->GetChildNodes()[index];
2985 return DoJob::DONE;
2986 }
2987
2988 return DoJob::CONTINUE;
2989 }
2990 }
2991
2992 wxDataViewTreeNode * GetResult() const
2993 { return ret; }
2994
2995 private:
2996 unsigned int row;
2997 int current;
2998 wxDataViewTreeNode * ret;
2999 wxDataViewTreeNode * parent;
3000 };
3001
3002 wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) const
3003 {
3004 wxASSERT( !IsVirtualList() );
3005
3006 if ( row == (unsigned)-1 )
3007 return NULL;
3008
3009 RowToTreeNodeJob job( row , -2, m_root );
3010 Walker( m_root , job );
3011 return job.GetResult();
3012 }
3013
3014 wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const
3015 {
3016 wxDataViewItem item;
3017 if (IsVirtualList())
3018 {
3019 if ( row < GetRowCount() )
3020 item = wxDataViewItem(wxUIntToPtr(row+1));
3021 }
3022 else
3023 {
3024 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
3025 if ( node )
3026 item = node->GetItem();
3027 }
3028
3029 return item;
3030 }
3031
3032 bool
3033 wxDataViewMainWindow::SendExpanderEvent(wxEventType type,
3034 const wxDataViewItem& item)
3035 {
3036 wxWindow *parent = GetParent();
3037 wxDataViewEvent le(type, parent->GetId());
3038
3039 le.SetEventObject(parent);
3040 le.SetModel(GetModel());
3041 le.SetItem( item );
3042
3043 return !parent->ProcessWindowEvent(le) || le.IsAllowed();
3044 }
3045
3046 bool wxDataViewMainWindow::IsExpanded( unsigned int row ) const
3047 {
3048 if (IsList())
3049 return false;
3050
3051 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
3052 if (!node)
3053 return false;
3054
3055 if (!node->HasChildren())
3056 return false;
3057
3058 return node->IsOpen();
3059 }
3060
3061 bool wxDataViewMainWindow::HasChildren( unsigned int row ) const
3062 {
3063 if (IsList())
3064 return false;
3065
3066 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
3067 if (!node)
3068 return false;
3069
3070 if (!node->HasChildren())
3071 return false;
3072
3073 return true;
3074 }
3075
3076 void wxDataViewMainWindow::Expand( unsigned int row )
3077 {
3078 if (IsList())
3079 return;
3080
3081 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
3082 if (!node)
3083 return;
3084
3085 if (!node->HasChildren())
3086 return;
3087
3088 if (!node->IsOpen())
3089 {
3090 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, node->GetItem()) )
3091 {
3092 // Vetoed by the event handler.
3093 return;
3094 }
3095
3096 node->ToggleOpen();
3097
3098 // build the children of current node
3099 if( node->GetChildNodes().empty() )
3100 {
3101 SortPrepare();
3102 ::BuildTreeHelper(GetModel(), node->GetItem(), node);
3103 }
3104
3105 // By expanding the node all row indices that are currently in the selection list
3106 // and are greater than our node have become invalid. So we have to correct that now.
3107 const unsigned rowAdjustment = node->GetSubTreeCount();
3108 for(unsigned i=0; i<m_selection.size(); ++i)
3109 {
3110 const unsigned testRow = m_selection[i];
3111 // all rows above us are not affected, so skip them
3112 if(testRow <= row)
3113 continue;
3114
3115 m_selection[i] += rowAdjustment;
3116 }
3117
3118 if(m_currentRow > row)
3119 ChangeCurrentRow(m_currentRow + rowAdjustment);
3120
3121 m_count = -1;
3122 UpdateDisplay();
3123 // Send the expanded event
3124 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem());
3125 }
3126 }
3127
3128 void wxDataViewMainWindow::Collapse(unsigned int row)
3129 {
3130 if (IsList())
3131 return;
3132
3133 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
3134 if (!node)
3135 return;
3136
3137 if (!node->HasChildren())
3138 return;
3139
3140 if (node->IsOpen())
3141 {
3142 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem()) )
3143 {
3144 // Vetoed by the event handler.
3145 return;
3146 }
3147
3148 // Find out if there are selected items below the current node.
3149 bool selectCollapsingRow = false;
3150 const unsigned rowAdjustment = node->GetSubTreeCount();
3151 unsigned maxRowToBeTested = row + rowAdjustment;
3152 for(unsigned i=0; i<m_selection.size(); ++i)
3153 {
3154 const unsigned testRow = m_selection[i];
3155 if(testRow > row && testRow <= maxRowToBeTested)
3156 {
3157 selectCollapsingRow = true;
3158 // get out as soon as we have found a node that is selected
3159 break;
3160 }
3161 }
3162
3163 node->ToggleOpen();
3164
3165 // If the node to be closed has selected items the user won't see those any longer.
3166 // We select the collapsing node in this case.
3167 if(selectCollapsingRow)
3168 {
3169 SelectAllRows(false);
3170 ChangeCurrentRow(row);
3171 SelectRow(row, true);
3172 SendSelectionChangedEvent(GetItemByRow(row));
3173 }
3174 else
3175 {
3176 // if there were no selected items below our node we still need to "fix" the
3177 // selection list to adjust for the changing of the row indices.
3178 // We actually do the opposite of what we are doing in Expand().
3179 for(unsigned i=0; i<m_selection.size(); ++i)
3180 {
3181 const unsigned testRow = m_selection[i];
3182 // all rows above us are not affected, so skip them
3183 if(testRow <= row)
3184 continue;
3185
3186 m_selection[i] -= rowAdjustment;
3187 }
3188
3189 // if the "current row" is being collapsed away we change it to the current row ;-)
3190 if(m_currentRow > row && m_currentRow <= maxRowToBeTested)
3191 ChangeCurrentRow(row);
3192 else if(m_currentRow > row)
3193 ChangeCurrentRow(m_currentRow - rowAdjustment);
3194 }
3195
3196 m_count = -1;
3197 UpdateDisplay();
3198 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,node->GetItem());
3199 }
3200 }
3201
3202 wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item )
3203 {
3204 const wxDataViewModel * model = GetModel();
3205 if( model == NULL )
3206 return NULL;
3207
3208 if (!item.IsOk())
3209 return m_root;
3210
3211 // Compose the parent-chain for the item we are looking for
3212 wxVector<wxDataViewItem> parentChain;
3213 wxDataViewItem it( item );
3214 while( it.IsOk() )
3215 {
3216 parentChain.push_back(it);
3217 it = model->GetParent(it);
3218 }
3219
3220 // Find the item along the parent-chain.
3221 // This algorithm is designed to speed up the node-finding method
3222 wxDataViewTreeNode* node = m_root;
3223 for( unsigned iter = parentChain.size()-1; ; --iter )
3224 {
3225 if( node->HasChildren() )
3226 {
3227 if( node->GetChildNodes().empty() )
3228 {
3229 // Even though the item is a container, it doesn't have any
3230 // child nodes in the control's representation yet. We have
3231 // to realize its subtree now.
3232 SortPrepare();
3233 ::BuildTreeHelper(model, node->GetItem(), node);
3234 }
3235
3236 const wxDataViewTreeNodes& nodes = node->GetChildNodes();
3237 bool found = false;
3238
3239 for (unsigned i = 0; i < nodes.GetCount(); ++i)
3240 {
3241 wxDataViewTreeNode* currentNode = nodes[i];
3242 if (currentNode->GetItem() == parentChain[iter])
3243 {
3244 if (currentNode->GetItem() == item)
3245 return currentNode;
3246
3247 node = currentNode;
3248 found = true;
3249 break;
3250 }
3251 }
3252 if (!found)
3253 return NULL;
3254 }
3255 else
3256 return NULL;
3257
3258 if ( !iter )
3259 break;
3260 }
3261 return NULL;
3262 }
3263
3264 void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item,
3265 wxDataViewColumn* &column )
3266 {
3267 wxDataViewColumn *col = NULL;
3268 unsigned int cols = GetOwner()->GetColumnCount();
3269 unsigned int colnum = 0;
3270 int x, y;
3271 m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y );
3272 for (unsigned x_start = 0; colnum < cols; colnum++)
3273 {
3274 col = GetOwner()->GetColumnAt(colnum);
3275 if (col->IsHidden())
3276 continue; // skip it!
3277
3278 unsigned int w = col->GetWidth();
3279 if (x_start+w >= (unsigned int)x)
3280 break;
3281
3282 x_start += w;
3283 }
3284
3285 column = col;
3286 item = GetItemByRow( GetLineAt( y ) );
3287 }
3288
3289 wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item,
3290 const wxDataViewColumn* column )
3291 {
3292 int xpos = 0;
3293 int width = 0;
3294
3295 unsigned int cols = GetOwner()->GetColumnCount();
3296 // If column is null the loop will compute the combined width of all columns.
3297 // Otherwise, it will compute the x position of the column we are looking for.
3298 for (unsigned int i = 0; i < cols; i++)
3299 {
3300 wxDataViewColumn* col = GetOwner()->GetColumnAt( i );
3301
3302 if (col == column)
3303 break;
3304
3305 if (col->IsHidden())
3306 continue; // skip it!
3307
3308 xpos += col->GetWidth();
3309 width += col->GetWidth();
3310 }
3311
3312 if(column != 0)
3313 {
3314 // If we have a column, we need can get its width directly.
3315 if(column->IsHidden())
3316 width = 0;
3317 else
3318 width = column->GetWidth();
3319
3320 }
3321 else
3322 {
3323 // If we have no column, we reset the x position back to zero.
3324 xpos = 0;
3325 }
3326
3327 // we have to take an expander column into account and compute its indentation
3328 // to get the correct x position where the actual text is
3329 int indent = 0;
3330 int row = GetRowByItem(item);
3331 if (!IsList() &&
3332 (column == 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column) )
3333 {
3334 wxDataViewTreeNode* node = GetTreeNodeByRow(row);
3335 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
3336 indent = indent + m_lineHeight; // use m_lineHeight as the width of the expander
3337 }
3338
3339 wxRect itemRect( xpos + indent,
3340 GetLineStart( row ),
3341 width - indent,
3342 GetLineHeight( row ) );
3343
3344 GetOwner()->CalcScrolledPosition( itemRect.x, itemRect.y,
3345 &itemRect.x, &itemRect.y );
3346
3347 return itemRect;
3348 }
3349
3350 int wxDataViewMainWindow::RecalculateCount() const
3351 {
3352 if (IsVirtualList())
3353 {
3354 wxDataViewVirtualListModel *list_model =
3355 (wxDataViewVirtualListModel*) GetModel();
3356
3357 return list_model->GetCount();
3358 }
3359 else
3360 {
3361 return m_root->GetSubTreeCount();
3362 }
3363 }
3364
3365 class ItemToRowJob : public DoJob
3366 {
3367 public:
3368 ItemToRowJob(const wxDataViewItem& item_, wxVector<wxDataViewItem>::reverse_iterator iter)
3369 : m_iter(iter),
3370 item(item_)
3371 {
3372 ret = -1;
3373 }
3374
3375 // Maybe binary search will help to speed up this process
3376 virtual int operator() ( wxDataViewTreeNode * node)
3377 {
3378 ret ++;
3379 if( node->GetItem() == item )
3380 {
3381 return DoJob::DONE;
3382 }
3383
3384 if( node->GetItem() == *m_iter )
3385 {
3386 m_iter++;
3387 return DoJob::CONTINUE;
3388 }
3389 else
3390 {
3391 ret += node->GetSubTreeCount();
3392 return DoJob::SKIP_SUBTREE;
3393 }
3394
3395 }
3396
3397 // the row number is begin from zero
3398 int GetResult() const
3399 { return ret -1; }
3400
3401 private:
3402 wxVector<wxDataViewItem>::reverse_iterator m_iter;
3403 wxDataViewItem item;
3404 int ret;
3405
3406 };
3407
3408 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) const
3409 {
3410 const wxDataViewModel * model = GetModel();
3411 if( model == NULL )
3412 return -1;
3413
3414 if (IsVirtualList())
3415 {
3416 return wxPtrToUInt( item.GetID() ) -1;
3417 }
3418 else
3419 {
3420 if( !item.IsOk() )
3421 return -1;
3422
3423 // Compose the parent-chain of the item we are looking for
3424 wxVector<wxDataViewItem> parentChain;
3425 wxDataViewItem it( item );
3426 while( it.IsOk() )
3427 {
3428 parentChain.push_back(it);
3429 it = model->GetParent(it);
3430 }
3431
3432 // add an 'invalid' item to represent our 'invisible' root node
3433 parentChain.push_back(wxDataViewItem());
3434
3435 // the parent chain was created by adding the deepest parent first.
3436 // so if we want to start at the root node, we have to iterate backwards through the vector
3437 ItemToRowJob job( item, parentChain.rbegin() );
3438 Walker( m_root, job );
3439 return job.GetResult();
3440 }
3441 }
3442
3443 static void BuildTreeHelper( const wxDataViewModel * model, const wxDataViewItem & item,
3444 wxDataViewTreeNode * node)
3445 {
3446 if( !model->IsContainer( item ) )
3447 return;
3448
3449 wxDataViewItemArray children;
3450 unsigned int num = model->GetChildren( item, children);
3451
3452 for ( unsigned int index = 0; index < num; index++ )
3453 {
3454 wxDataViewTreeNode *n = new wxDataViewTreeNode(node, children[index]);
3455
3456 if( model->IsContainer(children[index]) )
3457 n->SetHasChildren( true );
3458
3459 node->InsertChild(n, index);
3460 }
3461
3462 wxASSERT( node->IsOpen() );
3463 node->ChangeSubTreeCount(+num);
3464 }
3465
3466 void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
3467 {
3468 DestroyTree();
3469
3470 if (GetModel()->IsVirtualListModel())
3471 {
3472 m_count = -1;
3473 return;
3474 }
3475
3476 m_root = wxDataViewTreeNode::CreateRootNode();
3477
3478 // First we define a invalid item to fetch the top-level elements
3479 wxDataViewItem item;
3480 SortPrepare();
3481 BuildTreeHelper( model, item, m_root);
3482 m_count = -1;
3483 }
3484
3485 void wxDataViewMainWindow::DestroyTree()
3486 {
3487 if (!IsVirtualList())
3488 {
3489 wxDELETE(m_root);
3490 m_count = 0;
3491 }
3492 }
3493
3494 wxDataViewColumn*
3495 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem& item, wxDataViewCellMode mode)
3496 {
3497 // Edit the current column editable in 'mode'. If no column is focused
3498 // (typically because the user has full row selected), try to find the
3499 // first editable column (this would typically be a checkbox for
3500 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3501 // focus on the checkbox column; or on the only editable text column).
3502
3503 wxDataViewColumn *candidate = m_currentCol;
3504
3505 if ( candidate &&
3506 !IsCellEditableInMode(item, candidate, mode) &&
3507 !m_currentColSetByKeyboard )
3508 {
3509 // If current column was set by mouse to something not editable (in
3510 // 'mode') and the user pressed Space/F2 to edit it, treat the
3511 // situation as if there was whole-row focus, because that's what is
3512 // visually indicated and the mouse click could very well be targeted
3513 // on the row rather than on an individual cell.
3514 //
3515 // But if it was done by keyboard, respect that even if the column
3516 // isn't editable, because focus is visually on that column and editing
3517 // something else would be surprising.
3518 candidate = NULL;
3519 }
3520
3521 if ( !candidate )
3522 {
3523 const unsigned cols = GetOwner()->GetColumnCount();
3524 for ( unsigned i = 0; i < cols; i++ )
3525 {
3526 wxDataViewColumn *c = GetOwner()->GetColumnAt(i);
3527 if ( c->IsHidden() )
3528 continue;
3529
3530 if ( IsCellEditableInMode(item, c, mode) )
3531 {
3532 candidate = c;
3533 break;
3534 }
3535 }
3536 }
3537
3538 // If on container item without columns, only the expander column
3539 // may be directly editable:
3540 if ( candidate &&
3541 GetOwner()->GetExpanderColumn() != candidate &&
3542 GetModel()->IsContainer(item) &&
3543 !GetModel()->HasContainerColumns(item) )
3544 {
3545 candidate = GetOwner()->GetExpanderColumn();
3546 }
3547
3548 if ( !candidate )
3549 return NULL;
3550
3551 if ( !IsCellEditableInMode(item, candidate, mode) )
3552 return NULL;
3553
3554 return candidate;
3555 }
3556
3557 bool wxDataViewMainWindow::IsCellEditableInMode(const wxDataViewItem& item,
3558 const wxDataViewColumn *col,
3559 wxDataViewCellMode mode) const
3560 {
3561 if ( col->GetRenderer()->GetMode() != mode )
3562 return false;
3563
3564 if ( !GetModel()->IsEnabled(item, col->GetModelColumn()) )
3565 return false;
3566
3567 return true;
3568 }
3569
3570 void wxDataViewMainWindow::OnCharHook(wxKeyEvent& event)
3571 {
3572 if ( m_editorCtrl )
3573 {
3574 // Handle any keys special for the in-place editor and return without
3575 // calling Skip() below.
3576 switch ( event.GetKeyCode() )
3577 {
3578 case WXK_ESCAPE:
3579 m_editorRenderer->CancelEditing();
3580 return;
3581
3582 case WXK_RETURN:
3583 m_editorRenderer->FinishEditing();
3584 return;
3585 }
3586 }
3587
3588 event.Skip();
3589 }
3590
3591 void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
3592 {
3593 wxWindow * const parent = GetParent();
3594
3595 // propagate the char event upwards
3596 wxKeyEvent eventForParent(event);
3597 eventForParent.SetEventObject(parent);
3598 if ( parent->ProcessWindowEvent(eventForParent) )
3599 return;
3600
3601 if ( parent->HandleAsNavigationKey(event) )
3602 return;
3603
3604 // no item -> nothing to do
3605 if (!HasCurrentRow())
3606 {
3607 event.Skip();
3608 return;
3609 }
3610
3611 // don't use m_linesPerPage directly as it might not be computed yet
3612 const int pageSize = GetCountPerPage();
3613 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
3614
3615 switch ( event.GetKeyCode() )
3616 {
3617 case WXK_RETURN:
3618 if ( event.HasModifiers() )
3619 {
3620 event.Skip();
3621 break;
3622 }
3623 else
3624 {
3625 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3626 // it. Only if that event is not handled do we activate column renderer (which
3627 // is normally done by Space) or even inline editing.
3628
3629 const wxDataViewItem item = GetItemByRow(m_currentRow);
3630
3631 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED,
3632 parent->GetId());
3633 le.SetItem(item);
3634 le.SetEventObject(parent);
3635 le.SetModel(GetModel());
3636
3637 if ( parent->ProcessWindowEvent(le) )
3638 break;
3639 // else: fall through to WXK_SPACE handling
3640 }
3641
3642 case WXK_SPACE:
3643 if ( event.HasModifiers() )
3644 {
3645 event.Skip();
3646 break;
3647 }
3648 else
3649 {
3650 // Space toggles activatable items or -- if not activatable --
3651 // starts inline editing (this is normally done using F2 on
3652 // Windows, but Space is common everywhere else, so use it too
3653 // for greater cross-platform compatibility).
3654
3655 const wxDataViewItem item = GetItemByRow(m_currentRow);
3656
3657 // Activate the current activatable column. If not column is focused (typically
3658 // because the user has full row selected), try to find the first activatable
3659 // column (this would typically be a checkbox and we don't want to force the user
3660 // to set focus on the checkbox column).
3661 wxDataViewColumn *activatableCol = FindColumnForEditing(item, wxDATAVIEW_CELL_ACTIVATABLE);
3662
3663 if ( activatableCol )
3664 {
3665 const unsigned colIdx = activatableCol->GetModelColumn();
3666 const wxRect cell_rect = GetOwner()->GetItemRect(item, activatableCol);
3667
3668 wxDataViewRenderer *cell = activatableCol->GetRenderer();
3669 cell->PrepareForItem(GetModel(), item, colIdx);
3670 cell->WXActivateCell(cell_rect, GetModel(), item, colIdx, NULL);
3671
3672 break;
3673 }
3674 // else: fall through to WXK_F2 handling
3675 }
3676
3677 case WXK_F2:
3678 if ( event.HasModifiers() )
3679 {
3680 event.Skip();
3681 break;
3682 }
3683 else
3684 {
3685 if( !m_selection.empty() )
3686 {
3687 // Mimic Windows 7 behavior: edit the item that has focus
3688 // if it is selected and the first selected item if focus
3689 // is out of selection.
3690 int sel;
3691 if ( m_selection.Index(m_currentRow) != wxNOT_FOUND )
3692 sel = m_currentRow;
3693 else
3694 sel = m_selection[0];
3695
3696
3697 const wxDataViewItem item = GetItemByRow(sel);
3698
3699 // Edit the current column. If no column is focused
3700 // (typically because the user has full row selected), try
3701 // to find the first editable column.
3702 wxDataViewColumn *editableCol = FindColumnForEditing(item, wxDATAVIEW_CELL_EDITABLE);
3703
3704 if ( editableCol )
3705 GetOwner()->EditItem(item, editableCol);
3706 }
3707 }
3708 break;
3709
3710 case WXK_UP:
3711 OnVerticalNavigation( -1, event );
3712 break;
3713
3714 case WXK_DOWN:
3715 OnVerticalNavigation( +1, event );
3716 break;
3717 // Add the process for tree expanding/collapsing
3718 case WXK_LEFT:
3719 OnLeftKey();
3720 break;
3721
3722 case WXK_RIGHT:
3723 OnRightKey();
3724 break;
3725
3726 case WXK_END:
3727 OnVerticalNavigation( +(int)GetRowCount(), event );
3728 break;
3729
3730 case WXK_HOME:
3731 OnVerticalNavigation( -(int)GetRowCount(), event );
3732 break;
3733
3734 case WXK_PAGEUP:
3735 OnVerticalNavigation( -(pageSize - 1), event );
3736 break;
3737
3738 case WXK_PAGEDOWN:
3739 OnVerticalNavigation( +(pageSize - 1), event );
3740 break;
3741
3742 default:
3743 event.Skip();
3744 }
3745 }
3746
3747 void wxDataViewMainWindow::OnVerticalNavigation(int delta, const wxKeyEvent& event)
3748 {
3749 // if there is no selection, we cannot move it anywhere
3750 if (!HasCurrentRow() || IsEmpty())
3751 return;
3752
3753 int newRow = (int)m_currentRow + delta;
3754
3755 // let's keep the new row inside the allowed range
3756 if ( newRow < 0 )
3757 newRow = 0;
3758
3759 const int rowCount = (int)GetRowCount();
3760 if ( newRow >= rowCount )
3761 newRow = rowCount - 1;
3762
3763 unsigned int oldCurrent = m_currentRow;
3764 unsigned int newCurrent = (unsigned int)newRow;
3765
3766 // in single selection we just ignore Shift as we can't select several
3767 // items anyhow
3768 if ( event.ShiftDown() && !IsSingleSel() )
3769 {
3770 RefreshRow( oldCurrent );
3771
3772 ChangeCurrentRow( newCurrent );
3773
3774 // select all the items between the old and the new one
3775 if ( oldCurrent > newCurrent )
3776 {
3777 newCurrent = oldCurrent;
3778 oldCurrent = m_currentRow;
3779 }
3780
3781 SelectRows( oldCurrent, newCurrent, true );
3782 if (oldCurrent!=newCurrent)
3783 SendSelectionChangedEvent(GetItemByRow(m_selection[0]));
3784 }
3785 else // !shift
3786 {
3787 RefreshRow( oldCurrent );
3788
3789 // all previously selected items are unselected unless ctrl is held
3790 if ( !event.ControlDown() )
3791 SelectAllRows(false);
3792
3793 ChangeCurrentRow( newCurrent );
3794
3795 if ( !event.ControlDown() )
3796 {
3797 SelectRow( m_currentRow, true );
3798 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
3799 }
3800 else
3801 RefreshRow( m_currentRow );
3802 }
3803
3804 GetOwner()->EnsureVisible( m_currentRow, -1 );
3805 }
3806
3807 void wxDataViewMainWindow::OnLeftKey()
3808 {
3809 if ( IsList() )
3810 {
3811 TryAdvanceCurrentColumn(NULL, /*forward=*/false);
3812 }
3813 else
3814 {
3815 wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow);
3816 if ( !node )
3817 return;
3818
3819 if ( TryAdvanceCurrentColumn(node, /*forward=*/false) )
3820 return;
3821
3822 // Because TryAdvanceCurrentColumn() return false, we are at the first
3823 // column or using whole-row selection. In this situation, we can use
3824 // the standard TreeView handling of the left key.
3825 if (node->HasChildren() && node->IsOpen())
3826 {
3827 Collapse(m_currentRow);
3828 }
3829 else
3830 {
3831 // if the node is already closed, we move the selection to its parent
3832 wxDataViewTreeNode *parent_node = node->GetParent();
3833
3834 if (parent_node)
3835 {
3836 int parent = GetRowByItem( parent_node->GetItem() );
3837 if ( parent >= 0 )
3838 {
3839 unsigned int row = m_currentRow;
3840 SelectRow( row, false);
3841 SelectRow( parent, true );
3842 ChangeCurrentRow( parent );
3843 GetOwner()->EnsureVisible( parent, -1 );
3844 SendSelectionChangedEvent( parent_node->GetItem() );
3845 }
3846 }
3847 }
3848 }
3849 }
3850
3851 void wxDataViewMainWindow::OnRightKey()
3852 {
3853 if ( IsList() )
3854 {
3855 TryAdvanceCurrentColumn(NULL, /*forward=*/true);
3856 }
3857 else
3858 {
3859 wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow);
3860 if ( !node )
3861 return;
3862
3863 if ( node->HasChildren() )
3864 {
3865 if ( !node->IsOpen() )
3866 {
3867 Expand( m_currentRow );
3868 }
3869 else
3870 {
3871 // if the node is already open, we move the selection to the first child
3872 unsigned int row = m_currentRow;
3873 SelectRow( row, false );
3874 SelectRow( row + 1, true );
3875 ChangeCurrentRow( row + 1 );
3876 GetOwner()->EnsureVisible( row + 1, -1 );
3877 SendSelectionChangedEvent( GetItemByRow(row+1) );
3878 }
3879 }
3880 else
3881 {
3882 TryAdvanceCurrentColumn(node, /*forward=*/true);
3883 }
3884 }
3885 }
3886
3887 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode *node, bool forward)
3888 {
3889 if ( GetOwner()->GetColumnCount() == 0 )
3890 return false;
3891
3892 if ( !m_useCellFocus )
3893 return false;
3894
3895 if ( node )
3896 {
3897 // navigation shouldn't work in branch nodes without other columns:
3898 if ( node->HasChildren() && !GetModel()->HasContainerColumns(node->GetItem()) )
3899 return false;
3900 }
3901
3902 if ( m_currentCol == NULL || !m_currentColSetByKeyboard )
3903 {
3904 if ( forward )
3905 {
3906 m_currentCol = GetOwner()->GetColumnAt(1);
3907 m_currentColSetByKeyboard = true;
3908 RefreshRow(m_currentRow);
3909 return true;
3910 }
3911 else
3912 return false;
3913 }
3914
3915 int idx = GetOwner()->GetColumnIndex(m_currentCol) + (forward ? +1 : -1);
3916
3917 if ( idx >= (int)GetOwner()->GetColumnCount() )
3918 return false;
3919
3920 GetOwner()->EnsureVisible(m_currentRow, idx);
3921
3922 if ( idx < 1 )
3923 {
3924 // We are going to the left of the second column. Reset to whole-row
3925 // focus (which means first column would be edited).
3926 m_currentCol = NULL;
3927 RefreshRow(m_currentRow);
3928 return true;
3929 }
3930
3931 m_currentCol = GetOwner()->GetColumnAt(idx);
3932 m_currentColSetByKeyboard = true;
3933 RefreshRow(m_currentRow);
3934 return true;
3935 }
3936
3937 void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
3938 {
3939 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3940 {
3941 // let the base handle mouse wheel events.
3942 event.Skip();
3943 return;
3944 }
3945
3946 if(event.ButtonDown())
3947 {
3948 // Not skipping button down events would prevent the system from
3949 // setting focus to this window as most (all?) of them do by default,
3950 // so skip it to enable default handling.
3951 event.Skip();
3952 }
3953
3954 int x = event.GetX();
3955 int y = event.GetY();
3956 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
3957 wxDataViewColumn *col = NULL;
3958
3959 int xpos = 0;
3960 unsigned int cols = GetOwner()->GetColumnCount();
3961 unsigned int i;
3962 for (i = 0; i < cols; i++)
3963 {
3964 wxDataViewColumn *c = GetOwner()->GetColumnAt( i );
3965 if (c->IsHidden())
3966 continue; // skip it!
3967
3968 if (x < xpos + c->GetWidth())
3969 {
3970 col = c;
3971 break;
3972 }
3973 xpos += c->GetWidth();
3974 }
3975
3976 wxDataViewModel* const model = GetModel();
3977
3978 const unsigned int current = GetLineAt( y );
3979 const wxDataViewItem item = GetItemByRow(current);
3980
3981 // Handle right clicking here, before everything else as context menu
3982 // events should be sent even when we click outside of any item, unlike all
3983 // the other ones.
3984 if (event.RightUp())
3985 {
3986 wxWindow *parent = GetParent();
3987 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId());
3988 le.SetEventObject(parent);
3989 le.SetModel(model);
3990
3991 if ( item.IsOk() && col )
3992 {
3993 le.SetItem( item );
3994 le.SetColumn( col->GetModelColumn() );
3995 le.SetDataViewColumn( col );
3996
3997 wxVariant value;
3998 model->GetValue( value, item, col->GetModelColumn() );
3999 le.SetValue(value);
4000 }
4001
4002 parent->ProcessWindowEvent(le);
4003 return;
4004 }
4005
4006 if (!col)
4007 {
4008 event.Skip();
4009 return;
4010 }
4011
4012 wxDataViewRenderer *cell = col->GetRenderer();
4013 if ((current >= GetRowCount()) || (x > GetEndOfLastCol()))
4014 {
4015 // Unselect all if below the last row ?
4016 event.Skip();
4017 return;
4018 }
4019
4020 wxDataViewColumn* const
4021 expander = GetExpanderColumnOrFirstOne(GetOwner());
4022
4023 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
4024 // button) and also determine the offset of the real cell start, skipping
4025 // the indentation and the expander itself.
4026 bool hoverOverExpander = false;
4027 int itemOffset = 0;
4028 if ((!IsList()) && (expander == col))
4029 {
4030 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
4031
4032 int indent = node->GetIndentLevel();
4033 itemOffset = GetOwner()->GetIndent()*indent;
4034
4035 if ( node->HasChildren() )
4036 {
4037 // we make the rectangle we are looking in a bit bigger than the actual
4038 // visual expander so the user can hit that little thing reliably
4039 wxRect rect(itemOffset,
4040 GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2,
4041 m_lineHeight, m_lineHeight);
4042
4043 if( rect.Contains(x, y) )
4044 {
4045 // So the mouse is over the expander
4046 hoverOverExpander = true;
4047 if (m_underMouse && m_underMouse != node)
4048 {
4049 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4050 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
4051 }
4052 if (m_underMouse != node)
4053 {
4054 // wxLogMessage("Do the row: %d", current);
4055 RefreshRow(current);
4056 }
4057 m_underMouse = node;
4058 }
4059 }
4060
4061 // Account for the expander as well, even if this item doesn't have it,
4062 // its parent does so it still counts for the offset.
4063 itemOffset += m_lineHeight;
4064 }
4065 if (!hoverOverExpander)
4066 {
4067 if (m_underMouse != NULL)
4068 {
4069 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4070 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
4071 m_underMouse = NULL;
4072 }
4073 }
4074
4075 #if wxUSE_DRAG_AND_DROP
4076 if (event.Dragging())
4077 {
4078 if (m_dragCount == 0)
4079 {
4080 // we have to report the raw, physical coords as we want to be
4081 // able to call HitTest(event.m_pointDrag) from the user code to
4082 // get the item being dragged
4083 m_dragStart = event.GetPosition();
4084 }
4085
4086 m_dragCount++;
4087
4088 if (m_dragCount != 3)
4089 return;
4090
4091 if (event.LeftIsDown())
4092 {
4093 m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y,
4094 &m_dragStart.x, &m_dragStart.y );
4095 unsigned int drag_item_row = GetLineAt( m_dragStart.y );
4096 wxDataViewItem itemDragged = GetItemByRow( drag_item_row );
4097
4098 // Notify cell about drag
4099 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
4100 event.SetEventObject( m_owner );
4101 event.SetItem( itemDragged );
4102 event.SetModel( model );
4103 if (!m_owner->HandleWindowEvent( event ))
4104 return;
4105
4106 if (!event.IsAllowed())
4107 return;
4108
4109 wxDataObject *obj = event.GetDataObject();
4110 if (!obj)
4111 return;
4112
4113 wxDataViewDropSource drag( this, drag_item_row );
4114 drag.SetData( *obj );
4115 /* wxDragResult res = */ drag.DoDragDrop(event.GetDragFlags());
4116 delete obj;
4117 }
4118 return;
4119 }
4120 else
4121 {
4122 m_dragCount = 0;
4123 }
4124 #endif // wxUSE_DRAG_AND_DROP
4125
4126 bool simulateClick = false;
4127
4128 if (event.ButtonDClick())
4129 {
4130 m_renameTimer->Stop();
4131 m_lastOnSame = false;
4132 }
4133
4134 bool ignore_other_columns =
4135 ((expander != col) &&
4136 (model->IsContainer(item)) &&
4137 (!model->HasContainerColumns(item)));
4138
4139 if (event.LeftDClick())
4140 {
4141 if(hoverOverExpander)
4142 {
4143 // a double click on the expander will be converted into a "simulated" normal click
4144 simulateClick = true;
4145 }
4146 else if ( current == m_lineLastClicked )
4147 {
4148 wxWindow *parent = GetParent();
4149 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
4150 le.SetItem( item );
4151 le.SetColumn( col->GetModelColumn() );
4152 le.SetDataViewColumn( col );
4153 le.SetEventObject(parent);
4154 le.SetModel(GetModel());
4155
4156 parent->ProcessWindowEvent(le);
4157 return;
4158 }
4159 else
4160 {
4161 // The first click was on another item, so don't interpret this as
4162 // a double click, but as a simple click instead
4163 simulateClick = true;
4164 }
4165 }
4166
4167 if (event.LeftUp() && !hoverOverExpander)
4168 {
4169 if (m_lineSelectSingleOnUp != (unsigned int)-1)
4170 {
4171 // select single line
4172 SelectAllRows( false );
4173 SelectRow( m_lineSelectSingleOnUp, true );
4174 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) );
4175 }
4176
4177 // If the user click the expander, we do not do editing even if the column
4178 // with expander are editable
4179 if (m_lastOnSame && !ignore_other_columns)
4180 {
4181 if ((col == m_currentCol) && (current == m_currentRow) &&
4182 IsCellEditableInMode(item, col, wxDATAVIEW_CELL_EDITABLE) )
4183 {
4184 m_renameTimer->Start( 100, true );
4185 }
4186 }
4187
4188 m_lastOnSame = false;
4189 m_lineSelectSingleOnUp = (unsigned int)-1;
4190 }
4191 else if(!event.LeftUp())
4192 {
4193 // This is necessary, because after a DnD operation in
4194 // from and to ourself, the up event is swallowed by the
4195 // DnD code. So on next non-up event (which means here and
4196 // now) m_lineSelectSingleOnUp should be reset.
4197 m_lineSelectSingleOnUp = (unsigned int)-1;
4198 }
4199
4200 if (event.RightDown())
4201 {
4202 m_lineBeforeLastClicked = m_lineLastClicked;
4203 m_lineLastClicked = current;
4204
4205 // If the item is already selected, do not update the selection.
4206 // Multi-selections should not be cleared if a selected item is clicked.
4207 if (!IsRowSelected(current))
4208 {
4209 SelectAllRows(false);
4210 const unsigned oldCurrent = m_currentRow;
4211 ChangeCurrentRow(current);
4212 SelectRow(m_currentRow,true);
4213 RefreshRow(oldCurrent);
4214 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
4215 }
4216 }
4217 else if (event.MiddleDown())
4218 {
4219 }
4220
4221 if((event.LeftDown() || simulateClick) && hoverOverExpander)
4222 {
4223 wxDataViewTreeNode* node = GetTreeNodeByRow(current);
4224
4225 // hoverOverExpander being true tells us that our node must be
4226 // valid and have children.
4227 // So we don't need any extra checks.
4228 if( node->IsOpen() )
4229 Collapse(current);
4230 else
4231 Expand(current);
4232 }
4233 else if ((event.LeftDown() || simulateClick) && !hoverOverExpander)
4234 {
4235 m_lineBeforeLastClicked = m_lineLastClicked;
4236 m_lineLastClicked = current;
4237
4238 unsigned int oldCurrentRow = m_currentRow;
4239 bool oldWasSelected = IsRowSelected(m_currentRow);
4240
4241 bool cmdModifierDown = event.CmdDown();
4242 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
4243 {
4244 if ( IsSingleSel() || !IsRowSelected(current) )
4245 {
4246 SelectAllRows( false );
4247 ChangeCurrentRow(current);
4248 SelectRow(m_currentRow,true);
4249 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
4250 }
4251 else // multi sel & current is highlighted & no mod keys
4252 {
4253 m_lineSelectSingleOnUp = current;
4254 ChangeCurrentRow(current); // change focus
4255 }
4256 }
4257 else // multi sel & either ctrl or shift is down
4258 {
4259 if (cmdModifierDown)
4260 {
4261 ChangeCurrentRow(current);
4262 ReverseRowSelection(m_currentRow);
4263 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
4264 }
4265 else if (event.ShiftDown())
4266 {
4267 ChangeCurrentRow(current);
4268
4269 unsigned int lineFrom = oldCurrentRow,
4270 lineTo = current;
4271
4272 if ( lineTo < lineFrom )
4273 {
4274 lineTo = lineFrom;
4275 lineFrom = m_currentRow;
4276 }
4277
4278 SelectRows(lineFrom, lineTo, true);
4279 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
4280 }
4281 else // !ctrl, !shift
4282 {
4283 // test in the enclosing if should make it impossible
4284 wxFAIL_MSG( wxT("how did we get here?") );
4285 }
4286 }
4287
4288 if (m_currentRow != oldCurrentRow)
4289 RefreshRow( oldCurrentRow );
4290
4291 wxDataViewColumn *oldCurrentCol = m_currentCol;
4292
4293 // Update selection here...
4294 m_currentCol = col;
4295 m_currentColSetByKeyboard = false;
4296
4297 // This flag is used to decide whether we should start editing the item
4298 // label. We do it if the user clicks twice (but not double clicks,
4299 // i.e. simulateClick is false) on the same item but not if the click
4300 // was used for something else already, e.g. selecting the item (so it
4301 // must have been already selected) or giving the focus to the control
4302 // (so it must have had focus already).
4303 m_lastOnSame = !simulateClick && ((col == oldCurrentCol) &&
4304 (current == oldCurrentRow)) && oldWasSelected &&
4305 HasFocus();
4306
4307 // Call ActivateCell() after everything else as under GTK+
4308 if ( IsCellEditableInMode(item, col, wxDATAVIEW_CELL_ACTIVATABLE) )
4309 {
4310 // notify cell about click
4311 cell->PrepareForItem(model, item, col->GetModelColumn());
4312
4313 wxRect cell_rect( xpos + itemOffset,
4314 GetLineStart( current ),
4315 col->GetWidth() - itemOffset,
4316 GetLineHeight( current ) );
4317
4318 // Report position relative to the cell's custom area, i.e.
4319 // no the entire space as given by the control but the one
4320 // used by the renderer after calculation of alignment etc.
4321
4322 // adjust the rectangle ourselves to account for the alignment
4323 wxRect rectItem = cell_rect;
4324 const int align = cell->GetAlignment();
4325 if ( align != wxDVR_DEFAULT_ALIGNMENT )
4326 {
4327 const wxSize size = cell->GetSize();
4328
4329 if ( size.x >= 0 && size.x < cell_rect.width )
4330 {
4331 if ( align & wxALIGN_CENTER_HORIZONTAL )
4332 rectItem.x += (cell_rect.width - size.x)/2;
4333 else if ( align & wxALIGN_RIGHT )
4334 rectItem.x += cell_rect.width - size.x;
4335 // else: wxALIGN_LEFT is the default
4336 }
4337
4338 if ( size.y >= 0 && size.y < cell_rect.height )
4339 {
4340 if ( align & wxALIGN_CENTER_VERTICAL )
4341 rectItem.y += (cell_rect.height - size.y)/2;
4342 else if ( align & wxALIGN_BOTTOM )
4343 rectItem.y += cell_rect.height - size.y;
4344 // else: wxALIGN_TOP is the default
4345 }
4346 }
4347
4348 wxMouseEvent event2(event);
4349 event2.m_x -= rectItem.x;
4350 event2.m_y -= rectItem.y;
4351 m_owner->CalcUnscrolledPosition(event2.m_x, event2.m_y, &event2.m_x, &event2.m_y);
4352
4353 /* ignore ret */ cell->WXActivateCell
4354 (
4355 cell_rect,
4356 model,
4357 item,
4358 col->GetModelColumn(),
4359 &event2
4360 );
4361 }
4362 }
4363 }
4364
4365 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
4366 {
4367 m_hasFocus = true;
4368
4369 if (HasCurrentRow())
4370 Refresh();
4371
4372 event.Skip();
4373 }
4374
4375 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
4376 {
4377 m_hasFocus = false;
4378
4379 if (HasCurrentRow())
4380 Refresh();
4381
4382 event.Skip();
4383 }
4384
4385 void wxDataViewMainWindow::OnColumnsCountChanged()
4386 {
4387 int editableCount = 0;
4388
4389 const unsigned cols = GetOwner()->GetColumnCount();
4390 for ( unsigned i = 0; i < cols; i++ )
4391 {
4392 wxDataViewColumn *c = GetOwner()->GetColumnAt(i);
4393 if ( c->IsHidden() )
4394 continue;
4395 if ( c->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT )
4396 editableCount++;
4397 }
4398
4399 m_useCellFocus = (editableCount > 1);
4400
4401 UpdateDisplay();
4402 }
4403
4404 //-----------------------------------------------------------------------------
4405 // wxDataViewCtrl
4406 //-----------------------------------------------------------------------------
4407
4408 WX_DEFINE_LIST(wxDataViewColumnList)
4409
4410 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4411 BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
4412 EVT_SIZE(wxDataViewCtrl::OnSize)
4413 END_EVENT_TABLE()
4414
4415 wxDataViewCtrl::~wxDataViewCtrl()
4416 {
4417 if (m_notifier)
4418 GetModel()->RemoveNotifier( m_notifier );
4419
4420 m_cols.Clear();
4421 m_colsBestWidths.clear();
4422 }
4423
4424 void wxDataViewCtrl::Init()
4425 {
4426 m_cols.DeleteContents(true);
4427 m_notifier = NULL;
4428
4429 // No sorting column at start
4430 m_sortingColumnIdx = wxNOT_FOUND;
4431
4432 m_headerArea = NULL;
4433
4434 m_colsDirty = false;
4435 }
4436
4437 bool wxDataViewCtrl::Create(wxWindow *parent,
4438 wxWindowID id,
4439 const wxPoint& pos,
4440 const wxSize& size,
4441 long style,
4442 const wxValidator& validator,
4443 const wxString& name)
4444 {
4445 // if ( (style & wxBORDER_MASK) == 0)
4446 // style |= wxBORDER_SUNKEN;
4447
4448 Init();
4449
4450 if (!wxControl::Create( parent, id, pos, size,
4451 style | wxScrolledWindowStyle, validator, name))
4452 return false;
4453
4454 SetInitialSize(size);
4455
4456 #ifdef __WXMAC__
4457 MacSetClipChildren( true );
4458 #endif
4459
4460 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
4461
4462 // We use the cursor keys for moving the selection, not scrolling, so call
4463 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4464 // keyboard events forwarded to us from wxListMainWindow.
4465 DisableKeyboardScrolling();
4466
4467 if (HasFlag(wxDV_NO_HEADER))
4468 m_headerArea = NULL;
4469 else
4470 m_headerArea = new wxDataViewHeaderWindow(this);
4471
4472 SetTargetWindow( m_clientArea );
4473
4474 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
4475 if (m_headerArea)
4476 sizer->Add( m_headerArea, 0, wxGROW );
4477 sizer->Add( m_clientArea, 1, wxGROW );
4478 SetSizer( sizer );
4479
4480 return true;
4481 }
4482
4483 wxBorder wxDataViewCtrl::GetDefaultBorder() const
4484 {
4485 return wxBORDER_THEME;
4486 }
4487
4488 #ifdef __WXMSW__
4489 WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
4490 WXWPARAM wParam,
4491 WXLPARAM lParam)
4492 {
4493 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4494
4495 #ifndef __WXWINCE__
4496 // we need to process arrows ourselves for scrolling
4497 if ( nMsg == WM_GETDLGCODE )
4498 {
4499 rc |= DLGC_WANTARROWS;
4500 }
4501 #endif
4502
4503 return rc;
4504 }
4505 #endif
4506
4507 wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4508 {
4509 wxSize newsize = size;
4510 if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea))
4511 newsize.y -= m_headerArea->GetSize().y;
4512
4513 return newsize;
4514 }
4515
4516 void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
4517 {
4518 // We need to override OnSize so that our scrolled
4519 // window a) does call Layout() to use sizers for
4520 // positioning the controls but b) does not query
4521 // the sizer for their size and use that for setting
4522 // the scrollable area as set that ourselves by
4523 // calling SetScrollbar() further down.
4524
4525 Layout();
4526
4527 AdjustScrollbars();
4528
4529 // We must redraw the headers if their height changed. Normally this
4530 // shouldn't happen as the control shouldn't let itself be resized beneath
4531 // its minimal height but avoid the display artefacts that appear if it
4532 // does happen, e.g. because there is really not enough vertical space.
4533 if ( !HasFlag(wxDV_NO_HEADER) && m_headerArea &&
4534 m_headerArea->GetSize().y <= m_headerArea->GetBestSize(). y )
4535 {
4536 m_headerArea->Refresh();
4537 }
4538 }
4539
4540 void wxDataViewCtrl::SetFocus()
4541 {
4542 if (m_clientArea)
4543 m_clientArea->SetFocus();
4544 }
4545
4546 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4547 {
4548 if (!wxDataViewCtrlBase::AssociateModel( model ))
4549 return false;
4550
4551 if (model)
4552 {
4553 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
4554 model->AddNotifier( m_notifier );
4555 }
4556 else if (m_notifier)
4557 {
4558 m_notifier->Cleared();
4559 m_notifier = NULL;
4560 }
4561
4562 m_clientArea->DestroyTree();
4563
4564 if (model)
4565 {
4566 m_clientArea->BuildTree(model);
4567 }
4568
4569 m_clientArea->UpdateDisplay();
4570
4571 return true;
4572 }
4573
4574 #if wxUSE_DRAG_AND_DROP
4575
4576 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4577 {
4578 return m_clientArea->EnableDragSource( format );
4579 }
4580
4581 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4582 {
4583 return m_clientArea->EnableDropTarget( format );
4584 }
4585
4586 #endif // wxUSE_DRAG_AND_DROP
4587
4588 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4589 {
4590 if (!wxDataViewCtrlBase::AppendColumn(col))
4591 return false;
4592
4593 m_cols.Append( col );
4594 m_colsBestWidths.push_back(CachedColWidthInfo());
4595 OnColumnsCountChanged();
4596 return true;
4597 }
4598
4599 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4600 {
4601 if (!wxDataViewCtrlBase::PrependColumn(col))
4602 return false;
4603
4604 m_cols.Insert( col );
4605 m_colsBestWidths.insert(m_colsBestWidths.begin(), CachedColWidthInfo());
4606 OnColumnsCountChanged();
4607 return true;
4608 }
4609
4610 bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4611 {
4612 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4613 return false;
4614
4615 m_cols.Insert( pos, col );
4616 m_colsBestWidths.insert(m_colsBestWidths.begin() + pos, CachedColWidthInfo());
4617 OnColumnsCountChanged();
4618 return true;
4619 }
4620
4621 void wxDataViewCtrl::OnColumnChange(unsigned int idx)
4622 {
4623 if ( m_headerArea )
4624 m_headerArea->UpdateColumn(idx);
4625
4626 m_clientArea->UpdateDisplay();
4627 }
4628
4629 void wxDataViewCtrl::OnColumnsCountChanged()
4630 {
4631 if (m_headerArea)
4632 m_headerArea->SetColumnCount(GetColumnCount());
4633
4634 m_clientArea->OnColumnsCountChanged();
4635 }
4636
4637 void wxDataViewCtrl::DoSetExpanderColumn()
4638 {
4639 m_clientArea->UpdateDisplay();
4640 }
4641
4642 void wxDataViewCtrl::DoSetIndent()
4643 {
4644 m_clientArea->UpdateDisplay();
4645 }
4646
4647 unsigned int wxDataViewCtrl::GetColumnCount() const
4648 {
4649 return m_cols.GetCount();
4650 }
4651
4652 bool wxDataViewCtrl::SetRowHeight( int lineHeight )
4653 {
4654 if ( !m_clientArea )
4655 return false;
4656
4657 m_clientArea->SetRowHeight(lineHeight);
4658
4659 return true;
4660 }
4661
4662 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
4663 {
4664 return m_cols[idx];
4665 }
4666
4667 wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const
4668 {
4669 // columns can't be reordered if there is no header window which allows
4670 // to do this
4671 const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos]
4672 : pos;
4673
4674 return GetColumn(idx);
4675 }
4676
4677 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const
4678 {
4679 const unsigned count = m_cols.size();
4680 for ( unsigned n = 0; n < count; n++ )
4681 {
4682 if ( m_cols[n] == column )
4683 return n;
4684 }
4685
4686 return wxNOT_FOUND;
4687 }
4688
4689 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx) const
4690 {
4691 if ( m_colsBestWidths[idx].width != 0 )
4692 return m_colsBestWidths[idx].width;
4693
4694 const int count = m_clientArea->GetRowCount();
4695 wxDataViewColumn *column = GetColumn(idx);
4696 wxDataViewRenderer *renderer =
4697 const_cast<wxDataViewRenderer*>(column->GetRenderer());
4698
4699 class MaxWidthCalculator
4700 {
4701 public:
4702 MaxWidthCalculator(const wxDataViewCtrl *dvc,
4703 wxDataViewMainWindow *clientArea,
4704 wxDataViewRenderer *renderer,
4705 const wxDataViewModel *model,
4706 unsigned column,
4707 int expanderSize)
4708 : m_width(0),
4709 m_dvc(dvc),
4710 m_clientArea(clientArea),
4711 m_renderer(renderer),
4712 m_model(model),
4713 m_column(column),
4714 m_expanderSize(expanderSize)
4715
4716 {
4717 m_isExpanderCol =
4718 !clientArea->IsList() &&
4719 (column == 0 ||
4720 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl*>(dvc)) == dvc->GetColumnAt(column));
4721 }
4722
4723 void UpdateWithWidth(int width)
4724 {
4725 m_width = wxMax(m_width, width);
4726 }
4727
4728 void UpdateWithRow(int row)
4729 {
4730 int indent = 0;
4731 wxDataViewItem item;
4732
4733 if ( m_isExpanderCol )
4734 {
4735 wxDataViewTreeNode *node = m_clientArea->GetTreeNodeByRow(row);
4736 item = node->GetItem();
4737 indent = m_dvc->GetIndent() * node->GetIndentLevel() + m_expanderSize;
4738 }
4739 else
4740 {
4741 item = m_clientArea->GetItemByRow(row);
4742 }
4743
4744 m_renderer->PrepareForItem(m_model, item, m_column);
4745 m_width = wxMax(m_width, m_renderer->GetSize().x + indent);
4746 }
4747
4748 int GetMaxWidth() const { return m_width; }
4749
4750 private:
4751 int m_width;
4752 const wxDataViewCtrl *m_dvc;
4753 wxDataViewMainWindow *m_clientArea;
4754 wxDataViewRenderer *m_renderer;
4755 const wxDataViewModel *m_model;
4756 unsigned m_column;
4757 bool m_isExpanderCol;
4758 int m_expanderSize;
4759 };
4760
4761 MaxWidthCalculator calculator(this, m_clientArea, renderer,
4762 GetModel(), column->GetModelColumn(),
4763 m_clientArea->GetRowHeight());
4764
4765 calculator.UpdateWithWidth(column->GetMinWidth());
4766
4767 if ( m_headerArea )
4768 calculator.UpdateWithWidth(m_headerArea->GetColumnTitleWidth(*column));
4769
4770 // The code below deserves some explanation. For very large controls, we
4771 // simply can't afford to calculate sizes for all items, it takes too
4772 // long. So the best we can do is to check the first and the last N/2
4773 // items in the control for some sufficiently large N and calculate best
4774 // sizes from that. That can result in the calculated best width being too
4775 // small for some outliers, but it's better to get slightly imperfect
4776 // result than to wait several seconds after every update. To avoid highly
4777 // visible miscalculations, we also include all currently visible items
4778 // no matter what. Finally, the value of N is determined dynamically by
4779 // measuring how much time we spent on the determining item widths so far.
4780
4781 #if wxUSE_STOPWATCH
4782 int top_part_end = count;
4783 static const long CALC_TIMEOUT = 20/*ms*/;
4784 // don't call wxStopWatch::Time() too often
4785 static const unsigned CALC_CHECK_FREQ = 100;
4786 wxStopWatch timer;
4787 #else
4788 // use some hard-coded limit, that's the best we can do without timer
4789 int top_part_end = wxMin(500, count);
4790 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4791
4792 int row = 0;
4793
4794 for ( row = 0; row < top_part_end; row++ )
4795 {
4796 #if wxUSE_STOPWATCH
4797 if ( row % CALC_CHECK_FREQ == CALC_CHECK_FREQ-1 &&
4798 timer.Time() > CALC_TIMEOUT )
4799 break;
4800 #endif // wxUSE_STOPWATCH
4801 calculator.UpdateWithRow(row);
4802 }
4803
4804 // row is the first unmeasured item now; that's our value of N/2
4805
4806 if ( row < count )
4807 {
4808 top_part_end = row;
4809
4810 // add bottom N/2 items now:
4811 const int bottom_part_start = wxMax(row, count - row);
4812 for ( row = bottom_part_start; row < count; row++ )
4813 {
4814 calculator.UpdateWithRow(row);
4815 }
4816
4817 // finally, include currently visible items in the calculation:
4818 const wxPoint origin = CalcUnscrolledPosition(wxPoint(0, 0));
4819 int first_visible = m_clientArea->GetLineAt(origin.y);
4820 int last_visible = m_clientArea->GetLineAt(origin.y + GetClientSize().y);
4821
4822 first_visible = wxMax(first_visible, top_part_end);
4823 last_visible = wxMin(bottom_part_start, last_visible);
4824
4825 for ( row = first_visible; row < last_visible; row++ )
4826 {
4827 calculator.UpdateWithRow(row);
4828 }
4829
4830 wxLogTrace("dataview",
4831 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4832 top_part_end,
4833 count - bottom_part_start,
4834 wxMax(0, last_visible - first_visible),
4835 count);
4836 }
4837
4838 int max_width = calculator.GetMaxWidth();
4839 if ( max_width > 0 )
4840 max_width += 2 * PADDING_RIGHTLEFT;
4841
4842 const_cast<wxDataViewCtrl*>(this)->m_colsBestWidths[idx].width = max_width;
4843 return max_width;
4844 }
4845
4846 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col),
4847 unsigned int WXUNUSED(new_pos))
4848 {
4849 // do _not_ reorder m_cols elements here, they should always be in the
4850 // order in which columns were added, we only display the columns in
4851 // different order
4852 m_clientArea->UpdateDisplay();
4853 }
4854
4855 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4856 {
4857 wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column );
4858 if (!ret)
4859 return false;
4860
4861 m_colsBestWidths.erase(m_colsBestWidths.begin() + GetColumnIndex(column));
4862 m_cols.Erase(ret);
4863
4864 if ( m_clientArea->GetCurrentColumn() == column )
4865 m_clientArea->ClearCurrentColumn();
4866
4867 OnColumnsCountChanged();
4868
4869 return true;
4870 }
4871
4872 bool wxDataViewCtrl::ClearColumns()
4873 {
4874 SetExpanderColumn(NULL);
4875 m_cols.Clear();
4876 m_colsBestWidths.clear();
4877
4878 m_clientArea->ClearCurrentColumn();
4879
4880 OnColumnsCountChanged();
4881
4882 return true;
4883 }
4884
4885 void wxDataViewCtrl::InvalidateColBestWidth(int idx)
4886 {
4887 m_colsBestWidths[idx].width = 0;
4888 m_colsBestWidths[idx].dirty = true;
4889 m_colsDirty = true;
4890 }
4891
4892 void wxDataViewCtrl::InvalidateColBestWidths()
4893 {
4894 // mark all columns as dirty:
4895 m_colsBestWidths.clear();
4896 m_colsBestWidths.resize(m_cols.size());
4897 m_colsDirty = true;
4898 }
4899
4900 void wxDataViewCtrl::UpdateColWidths()
4901 {
4902 m_colsDirty = false;
4903
4904 if ( !m_headerArea )
4905 return;
4906
4907 const unsigned len = m_colsBestWidths.size();
4908 for ( unsigned i = 0; i < len; i++ )
4909 {
4910 // Note that we have to have an explicit 'dirty' flag here instead of
4911 // checking if the width==0, as is done in GetBestColumnWidth().
4912 //
4913 // Testing width==0 wouldn't work correctly if some code called
4914 // GetWidth() after col. width invalidation but before
4915 // wxDataViewCtrl::UpdateColWidths() was called at idle time. This
4916 // would result in the header's column width getting out of sync with
4917 // the control itself.
4918 if ( m_colsBestWidths[i].dirty )
4919 {
4920 m_headerArea->UpdateColumn(i);
4921 m_colsBestWidths[i].dirty = false;
4922 }
4923 }
4924 }
4925
4926 void wxDataViewCtrl::OnInternalIdle()
4927 {
4928 wxDataViewCtrlBase::OnInternalIdle();
4929
4930 if ( m_colsDirty )
4931 UpdateColWidths();
4932 }
4933
4934 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4935 {
4936 unsigned int len = GetColumnCount();
4937 for ( unsigned int i = 0; i < len; i++ )
4938 {
4939 wxDataViewColumn * col = GetColumnAt(i);
4940 if (column==col)
4941 return i;
4942 }
4943
4944 return wxNOT_FOUND;
4945 }
4946
4947 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4948 {
4949 return m_sortingColumnIdx == wxNOT_FOUND ? NULL
4950 : GetColumn(m_sortingColumnIdx);
4951 }
4952
4953 wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4954 {
4955 return GetItemByRow(m_clientArea->GetCurrentRow());
4956 }
4957
4958 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4959 {
4960 const int row = m_clientArea->GetRowByItem(item);
4961
4962 const unsigned oldCurrent = m_clientArea->GetCurrentRow();
4963 if ( static_cast<unsigned>(row) != oldCurrent )
4964 {
4965 m_clientArea->ChangeCurrentRow(row);
4966 m_clientArea->RefreshRow(oldCurrent);
4967 m_clientArea->RefreshRow(row);
4968 }
4969 }
4970
4971 wxDataViewColumn *wxDataViewCtrl::GetCurrentColumn() const
4972 {
4973 return m_clientArea->GetCurrentColumn();
4974 }
4975
4976 int wxDataViewCtrl::GetSelectedItemsCount() const
4977 {
4978 return m_clientArea->GetSelections().size();
4979 }
4980
4981 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4982 {
4983 sel.Empty();
4984 const wxDataViewSelection& selections = m_clientArea->GetSelections();
4985
4986 const size_t len = selections.size();
4987 for ( size_t i = 0; i < len; i++ )
4988 {
4989 wxDataViewItem item = m_clientArea->GetItemByRow(selections[i]);
4990 if ( item.IsOk() )
4991 {
4992 sel.Add(item);
4993 }
4994 else
4995 {
4996 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4997 }
4998 }
4999
5000 return sel.size();
5001 }
5002
5003 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
5004 {
5005 wxDataViewSelection selection(wxDataViewSelectionCmp);
5006
5007 wxDataViewItem last_parent;
5008
5009 int len = sel.GetCount();
5010 for( int i = 0; i < len; i ++ )
5011 {
5012 wxDataViewItem item = sel[i];
5013 wxDataViewItem parent = GetModel()->GetParent( item );
5014 if (parent)
5015 {
5016 if (parent != last_parent)
5017 ExpandAncestors(item);
5018 }
5019
5020 last_parent = parent;
5021 int row = m_clientArea->GetRowByItem( item );
5022 if( row >= 0 )
5023 selection.Add( static_cast<unsigned int>(row) );
5024 }
5025
5026 m_clientArea->SetSelections( selection );
5027 }
5028
5029 void wxDataViewCtrl::Select( const wxDataViewItem & item )
5030 {
5031 ExpandAncestors( item );
5032
5033 int row = m_clientArea->GetRowByItem( item );
5034 if( row >= 0 )
5035 {
5036 // Unselect all rows before select another in the single select mode
5037 if (m_clientArea->IsSingleSel())
5038 m_clientArea->SelectAllRows(false);
5039
5040 m_clientArea->SelectRow(row, true);
5041
5042 // Also set focus to the selected item
5043 m_clientArea->ChangeCurrentRow( row );
5044 }
5045 }
5046
5047 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
5048 {
5049 int row = m_clientArea->GetRowByItem( item );
5050 if( row >= 0 )
5051 m_clientArea->SelectRow(row, false);
5052 }
5053
5054 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
5055 {
5056 int row = m_clientArea->GetRowByItem( item );
5057 if( row >= 0 )
5058 {
5059 return m_clientArea->IsRowSelected(row);
5060 }
5061 return false;
5062 }
5063
5064 void wxDataViewCtrl::SetAlternateRowColour(const wxColour& colour)
5065 {
5066 m_alternateRowColour = colour;
5067 }
5068
5069 void wxDataViewCtrl::SelectAll()
5070 {
5071 m_clientArea->SelectAllRows(true);
5072 }
5073
5074 void wxDataViewCtrl::UnselectAll()
5075 {
5076 m_clientArea->SelectAllRows(false);
5077 }
5078
5079 void wxDataViewCtrl::EnsureVisible( int row, int column )
5080 {
5081 if( row < 0 )
5082 row = 0;
5083 if( row > (int) m_clientArea->GetRowCount() )
5084 row = m_clientArea->GetRowCount();
5085
5086 int first = m_clientArea->GetFirstVisibleRow();
5087 int last = m_clientArea->GetLastVisibleRow();
5088 if( row < first )
5089 m_clientArea->ScrollTo( row, column );
5090 else if( row > last )
5091 m_clientArea->ScrollTo( row - last + first, column );
5092 else
5093 m_clientArea->ScrollTo( first, column );
5094 }
5095
5096 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
5097 {
5098 ExpandAncestors( item );
5099
5100 m_clientArea->RecalculateDisplay();
5101
5102 int row = m_clientArea->GetRowByItem(item);
5103 if( row >= 0 )
5104 {
5105 if( column == NULL )
5106 EnsureVisible(row, -1);
5107 else
5108 EnsureVisible( row, GetColumnIndex(column) );
5109 }
5110
5111 }
5112
5113 void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item,
5114 wxDataViewColumn* &column ) const
5115 {
5116 m_clientArea->HitTest(point, item, column);
5117 }
5118
5119 wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item,
5120 const wxDataViewColumn* column ) const
5121 {
5122 return m_clientArea->GetItemRect(item, column);
5123 }
5124
5125 wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
5126 {
5127 return m_clientArea->GetItemByRow( row );
5128 }
5129
5130 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
5131 {
5132 return m_clientArea->GetRowByItem( item );
5133 }
5134
5135 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
5136 {
5137 ExpandAncestors( item );
5138
5139 int row = m_clientArea->GetRowByItem( item );
5140 if (row != -1)
5141 {
5142 m_clientArea->Expand(row);
5143 InvalidateColBestWidths();
5144 }
5145 }
5146
5147 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
5148 {
5149 int row = m_clientArea->GetRowByItem( item );
5150 if (row != -1)
5151 {
5152 m_clientArea->Collapse(row);
5153 InvalidateColBestWidths();
5154 }
5155 }
5156
5157 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
5158 {
5159 int row = m_clientArea->GetRowByItem( item );
5160 if (row != -1)
5161 return m_clientArea->IsExpanded(row);
5162 return false;
5163 }
5164
5165 void wxDataViewCtrl::EditItem(const wxDataViewItem& item, const wxDataViewColumn *column)
5166 {
5167 wxCHECK_RET( item.IsOk(), "invalid item" );
5168 wxCHECK_RET( column, "no column provided" );
5169
5170 m_clientArea->StartEditing(item, column);
5171 }
5172
5173 #endif // !wxUSE_GENERICDATAVIEWCTRL
5174
5175 #endif // wxUSE_DATAVIEWCTRL