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