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
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
47 #include "wx/imaglist.h"
48 #include "wx/headerctrl.h"
50 #include "wx/stopwatch.h"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 class wxDataViewColumn
;
57 class wxDataViewHeaderWindow
;
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 static const int SCROLL_UNIT_X
= 15;
66 // the cell padding on the left/right
67 static const int PADDING_RIGHTLEFT
= 3;
69 // the expander space margin
70 static const int EXPANDER_MARGIN
= 4;
73 static const int EXPANDER_OFFSET
= 4;
75 static const int EXPANDER_OFFSET
= 1;
78 // Below is the compare stuff.
79 // For the generic implementation, both the leaf nodes and the nodes are sorted for
80 // fast search when needed
81 static wxDataViewModel
* g_model
;
82 static int g_column
= -2;
83 static bool g_asending
= true;
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
92 // Return the expander column or, if it is not set, the first column and also
93 // set it as the expander one for the future.
94 wxDataViewColumn
* GetExpanderColumnOrFirstOne(wxDataViewCtrl
* dataview
)
96 wxDataViewColumn
* expander
= dataview
->GetExpanderColumn();
99 // TODO-RTL: last column for RTL support
100 expander
= dataview
->GetColumnAt( 0 );
101 dataview
->SetExpanderColumn(expander
);
107 } // anonymous namespace
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
113 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
120 m_sortAscending
= true;
123 int wxDataViewColumn::GetWidth() const
127 case wxCOL_WIDTH_DEFAULT
:
128 return wxDVC_DEFAULT_WIDTH
;
130 case wxCOL_WIDTH_AUTOSIZE
:
131 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
132 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
139 void wxDataViewColumn::UpdateDisplay()
143 int idx
= m_owner
->GetColumnIndex( this );
144 m_owner
->OnColumnChange( idx
);
148 void wxDataViewColumn::SetSortOrder(bool ascending
)
153 // First unset the old sort column if any.
154 int oldSortKey
= m_owner
->GetSortingColumnIndex();
155 if ( oldSortKey
!= wxNOT_FOUND
)
157 m_owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
160 // Now set this one as the new sort column.
161 const int idx
= m_owner
->GetColumnIndex(this);
162 m_owner
->SetSortingColumnIndex(idx
);
165 m_sortAscending
= ascending
;
167 // Call this directly instead of using UpdateDisplay() as we already have
168 // the column index, no need to look it up again.
169 m_owner
->OnColumnChange(idx
);
172 //-----------------------------------------------------------------------------
173 // wxDataViewHeaderWindow
174 //-----------------------------------------------------------------------------
176 class wxDataViewHeaderWindow
: public wxHeaderCtrl
179 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
180 : wxHeaderCtrl(parent
)
184 wxDataViewCtrl
*GetOwner() const
185 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
188 // implement/override wxHeaderCtrl functions by forwarding them to the main
190 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
192 return *(GetOwner()->GetColumn(idx
));
195 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
197 wxDataViewCtrl
* const owner
= GetOwner();
199 int widthContents
= owner
->GetBestColumnWidth(idx
);
200 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
201 owner
->OnColumnChange(idx
);
207 bool SendEvent(wxEventType type
, unsigned int n
)
209 wxDataViewCtrl
* const owner
= GetOwner();
210 wxDataViewEvent
event(type
, owner
->GetId());
212 event
.SetEventObject(owner
);
214 event
.SetDataViewColumn(owner
->GetColumn(n
));
215 event
.SetModel(owner
->GetModel());
217 // for events created by wxDataViewHeaderWindow the
218 // row / value fields are not valid
219 return owner
->ProcessWindowEvent(event
);
222 void OnClick(wxHeaderCtrlEvent
& event
)
224 const unsigned idx
= event
.GetColumn();
226 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
229 // default handling for the column click is to sort by this column or
230 // toggle its sort order
231 wxDataViewCtrl
* const owner
= GetOwner();
232 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
233 if ( !col
->IsSortable() )
235 // no default handling for non-sortable columns
240 if ( col
->IsSortKey() )
242 // already using this column for sorting, just change the order
243 col
->ToggleSortOrder();
245 else // not using this column for sorting yet
247 col
->SetSortOrder(true);
250 wxDataViewModel
* const model
= owner
->GetModel();
254 owner
->OnColumnChange(idx
);
256 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
259 void OnRClick(wxHeaderCtrlEvent
& event
)
261 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
266 void OnResize(wxHeaderCtrlEvent
& event
)
268 wxDataViewCtrl
* const owner
= GetOwner();
270 const unsigned col
= event
.GetColumn();
271 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
272 GetOwner()->OnColumnChange(col
);
275 void OnEndReorder(wxHeaderCtrlEvent
& event
)
277 wxDataViewCtrl
* const owner
= GetOwner();
278 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
279 event
.GetNewOrder());
282 DECLARE_EVENT_TABLE()
283 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
286 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
287 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
288 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
290 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
291 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
293 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
296 //-----------------------------------------------------------------------------
297 // wxDataViewRenameTimer
298 //-----------------------------------------------------------------------------
300 class wxDataViewRenameTimer
: public wxTimer
303 wxDataViewMainWindow
*m_owner
;
306 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
310 //-----------------------------------------------------------------------------
311 // wxDataViewTreeNode
312 //-----------------------------------------------------------------------------
314 class wxDataViewTreeNode
;
315 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
317 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
318 wxDataViewTreeNode
** node2
);
320 class wxDataViewTreeNode
323 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
330 ~wxDataViewTreeNode()
334 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
335 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
346 static wxDataViewTreeNode
* CreateRootNode()
348 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
349 n
->SetHasChildren(true);
350 n
->m_branchData
->open
= true;
354 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
356 const wxDataViewTreeNodes
& GetChildNodes() const
358 wxASSERT( m_branchData
!= NULL
);
359 return m_branchData
->children
;
362 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
365 m_branchData
= new BranchNodeData
;
367 m_branchData
->children
.Insert(node
, index
);
369 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
371 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
374 void RemoveChild(wxDataViewTreeNode
*node
)
376 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
377 m_branchData
->children
.Remove(node
);
380 // returns position of child node for given item in children list or wxNOT_FOUND
381 int FindChildByItem(const wxDataViewItem
& item
) const
386 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
387 const int len
= nodes
.size();
388 for ( int i
= 0; i
< len
; i
++ )
390 if ( nodes
[i
]->m_item
== item
)
396 const wxDataViewItem
& GetItem() const { return m_item
; }
397 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
399 int GetIndentLevel() const
402 const wxDataViewTreeNode
* node
= this;
403 while( node
->GetParent()->GetParent() != NULL
)
405 node
= node
->GetParent();
413 return m_branchData
&& m_branchData
->open
;
418 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
422 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
423 const int len
= nodes
.GetCount();
424 for ( int i
= 0;i
< len
; i
++)
425 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
427 if (m_branchData
->open
)
429 ChangeSubTreeCount(-sum
);
430 m_branchData
->open
= !m_branchData
->open
;
434 m_branchData
->open
= !m_branchData
->open
;
435 ChangeSubTreeCount(+sum
);
439 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
440 // even if GetChildNodes() is empty; see below.
441 bool HasChildren() const
443 return m_branchData
!= NULL
;
446 void SetHasChildren(bool has
)
450 wxDELETE(m_branchData
);
452 else if ( m_branchData
== NULL
)
454 m_branchData
= new BranchNodeData
;
458 int GetSubTreeCount() const
460 return m_branchData
? m_branchData
->subTreeCount
: 0;
463 void ChangeSubTreeCount( int num
)
465 wxASSERT( m_branchData
!= NULL
);
467 if( !m_branchData
->open
)
470 m_branchData
->subTreeCount
+= num
;
471 wxASSERT( m_branchData
->subTreeCount
>= 0 );
474 m_parent
->ChangeSubTreeCount(num
);
484 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
486 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
487 int len
= nodes
.GetCount();
488 for (int i
= 0; i
< len
; i
++)
490 if ( nodes
[i
]->HasChildren() )
498 wxDataViewTreeNode
*m_parent
;
500 // Corresponding model item.
501 wxDataViewItem m_item
;
503 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
504 // separate struct in order to conserve memory.
505 struct BranchNodeData
513 // Child nodes. Note that this may be empty even if m_hasChildren in
514 // case this branch of the tree wasn't expanded and realized yet.
515 wxDataViewTreeNodes children
;
517 // Is the branch node currently open (expanded)?
520 // Total count of expanded (i.e. visible with the help of some
521 // scrolling) items in the subtree, but excluding this node. I.e. it is
522 // 0 for leaves and is the number of rows the subtree occupies for
527 BranchNodeData
*m_branchData
;
531 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
532 wxDataViewTreeNode
** node2
)
534 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
538 //-----------------------------------------------------------------------------
539 // wxDataViewMainWindow
540 //-----------------------------------------------------------------------------
542 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
544 class wxDataViewMainWindow
: public wxWindow
547 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
549 const wxPoint
&pos
= wxDefaultPosition
,
550 const wxSize
&size
= wxDefaultSize
,
551 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
552 virtual ~wxDataViewMainWindow();
554 bool IsList() const { return GetModel()->IsListModel(); }
555 bool IsVirtualList() const { return m_root
== NULL
; }
557 // notifications from wxDataViewModel
558 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
559 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
560 bool ItemChanged( const wxDataViewItem
&item
);
561 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
565 if (!IsVirtualList())
575 g_model
= GetModel();
576 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
579 if (g_model
->HasDefaultCompare())
587 g_column
= col
->GetModelColumn();
588 g_asending
= col
->IsSortOrderAscending();
591 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
592 wxDataViewCtrl
*GetOwner() { return m_owner
; }
593 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
595 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
596 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
598 #if wxUSE_DRAG_AND_DROP
599 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
600 #endif // wxUSE_DRAG_AND_DROP
601 void OnPaint( wxPaintEvent
&event
);
602 void OnChar( wxKeyEvent
&event
);
603 void OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
);
606 void OnMouse( wxMouseEvent
&event
);
607 void OnSetFocus( wxFocusEvent
&event
);
608 void OnKillFocus( wxFocusEvent
&event
);
610 void UpdateDisplay();
611 void RecalculateDisplay();
612 void OnInternalIdle();
614 void OnRenameTimer();
616 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
617 void ScrollTo( int rows
, int column
);
619 unsigned GetCurrentRow() const { return m_currentRow
; }
620 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
621 void ChangeCurrentRow( unsigned int row
);
622 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
624 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
625 bool IsEmpty() { return GetRowCount() == 0; }
627 int GetCountPerPage() const;
628 int GetEndOfLastCol() const;
629 unsigned int GetFirstVisibleRow() const;
631 // I change this method to un const because in the tree view,
632 // the displaying number of the tree are changing along with the
633 // expanding/collapsing of the tree nodes
634 unsigned int GetLastVisibleRow();
635 unsigned int GetRowCount();
637 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
638 void SetSelections( const wxDataViewSelection
& sel
)
639 { m_selection
= sel
; UpdateDisplay(); }
640 void Select( const wxArrayInt
& aSelections
);
641 void SelectAllRows( bool on
);
642 void SelectRow( unsigned int row
, bool on
);
643 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
644 void ReverseRowSelection( unsigned int row
);
645 bool IsRowSelected( unsigned int row
);
646 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
648 void RefreshRow( unsigned int row
);
649 void RefreshRows( unsigned int from
, unsigned int to
);
650 void RefreshRowsAfter( unsigned int firstRow
);
652 // returns the colour to be used for drawing the rules
653 wxColour
GetRuleColour() const
655 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
658 wxRect
GetLineRect( unsigned int row
) const;
660 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
661 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
662 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
664 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
665 int GetRowHeight() const { return m_lineHeight
; }
667 // Some useful functions for row and item mapping
668 wxDataViewItem
GetItemByRow( unsigned int row
) const;
669 int GetRowByItem( const wxDataViewItem
& item
) const;
671 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
672 // We did not need this temporarily
673 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
675 // Methods for building the mapping tree
676 void BuildTree( wxDataViewModel
* model
);
678 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
679 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
681 void Expand( unsigned int row
);
682 void Collapse( unsigned int row
);
683 bool IsExpanded( unsigned int row
) const;
684 bool HasChildren( unsigned int row
) const;
686 #if wxUSE_DRAG_AND_DROP
687 bool EnableDragSource( const wxDataFormat
&format
);
688 bool EnableDropTarget( const wxDataFormat
&format
);
690 void RemoveDropHint();
691 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
692 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
693 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
695 #endif // wxUSE_DRAG_AND_DROP
697 void OnColumnsCountChanged();
700 int RecalculateCount();
702 // Return false only if the event was vetoed by its handler.
703 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
705 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
707 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
710 wxDataViewCtrl
*m_owner
;
714 wxDataViewColumn
*m_currentCol
;
715 unsigned int m_currentRow
;
716 wxDataViewSelection m_selection
;
718 wxDataViewRenameTimer
*m_renameTimer
;
723 bool m_currentColSetByKeyboard
;
725 #if wxUSE_DRAG_AND_DROP
730 wxDataFormat m_dragFormat
;
733 wxDataFormat m_dropFormat
;
735 unsigned int m_dropHintLine
;
736 #endif // wxUSE_DRAG_AND_DROP
738 // for double click logic
739 unsigned int m_lineLastClicked
,
740 m_lineBeforeLastClicked
,
741 m_lineSelectSingleOnUp
;
743 // the pen used to draw horiz/vertical rules
746 // the pen used to draw the expander and the lines
749 // This is the tree structure of the model
750 wxDataViewTreeNode
* m_root
;
753 // This is the tree node under the cursor
754 wxDataViewTreeNode
* m_underMouse
;
757 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
758 DECLARE_EVENT_TABLE()
761 // ---------------------------------------------------------
762 // wxGenericDataViewModelNotifier
763 // ---------------------------------------------------------
765 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
768 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
769 { m_mainWindow
= mainWindow
; }
771 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
772 { return m_mainWindow
->ItemAdded( parent
, item
); }
773 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
774 { return m_mainWindow
->ItemDeleted( parent
, item
); }
775 virtual bool ItemChanged( const wxDataViewItem
& item
)
776 { return m_mainWindow
->ItemChanged(item
); }
777 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
778 { return m_mainWindow
->ValueChanged( item
, col
); }
779 virtual bool Cleared()
780 { return m_mainWindow
->Cleared(); }
781 virtual void Resort()
782 { m_mainWindow
->Resort(); }
784 wxDataViewMainWindow
*m_mainWindow
;
787 // ---------------------------------------------------------
788 // wxDataViewRenderer
789 // ---------------------------------------------------------
791 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
793 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
794 wxDataViewCellMode mode
,
796 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
800 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
804 wxDataViewRenderer::~wxDataViewRenderer()
809 wxDC
*wxDataViewRenderer::GetDC()
813 if (GetOwner() == NULL
)
815 if (GetOwner()->GetOwner() == NULL
)
817 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
823 void wxDataViewRenderer::SetAlignment( int align
)
828 int wxDataViewRenderer::GetAlignment() const
833 // ---------------------------------------------------------
834 // wxDataViewCustomRenderer
835 // ---------------------------------------------------------
837 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
839 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
840 wxDataViewCellMode mode
, int align
) :
841 wxDataViewRenderer( varianttype
, mode
, align
)
845 // ---------------------------------------------------------
846 // wxDataViewTextRenderer
847 // ---------------------------------------------------------
849 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
851 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
852 wxDataViewCellMode mode
, int align
) :
853 wxDataViewRenderer( varianttype
, mode
, align
)
857 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
859 m_text
= value
.GetString();
864 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
869 bool wxDataViewTextRenderer::HasEditorCtrl() const
874 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
875 wxRect labelRect
, const wxVariant
&value
)
877 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
878 wxPoint(labelRect
.x
,labelRect
.y
),
879 wxSize(labelRect
.width
,labelRect
.height
),
880 wxTE_PROCESS_ENTER
);
882 // select the text in the control an place the cursor at the end
883 ctrl
->SetInsertionPointEnd();
889 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
891 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
892 value
= text
->GetValue();
896 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
898 RenderText(m_text
, 0, rect
, dc
, state
);
902 wxSize
wxDataViewTextRenderer::GetSize() const
905 return GetTextExtent(m_text
);
907 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
910 // ---------------------------------------------------------
911 // wxDataViewBitmapRenderer
912 // ---------------------------------------------------------
914 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
916 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
917 wxDataViewCellMode mode
, int align
) :
918 wxDataViewRenderer( varianttype
, mode
, align
)
922 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
924 if (value
.GetType() == wxT("wxBitmap"))
926 if (value
.GetType() == wxT("wxIcon"))
932 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
937 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
940 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
941 else if (m_icon
.IsOk())
942 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
947 wxSize
wxDataViewBitmapRenderer::GetSize() const
950 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
951 else if (m_icon
.IsOk())
952 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
954 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
957 // ---------------------------------------------------------
958 // wxDataViewToggleRenderer
959 // ---------------------------------------------------------
961 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
963 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
964 wxDataViewCellMode mode
, int align
) :
965 wxDataViewRenderer( varianttype
, mode
, align
)
970 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
972 m_toggle
= value
.GetBool();
977 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
982 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
986 flags
|= wxCONTROL_CHECKED
;
987 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
988 GetEnabled() == false)
989 flags
|= wxCONTROL_DISABLED
;
991 // check boxes we draw must always have the same, standard size (if it's
992 // bigger than the cell size the checkbox will be truncated because the
993 // caller had set the clipping rectangle to prevent us from drawing outside
995 cell
.SetSize(GetSize());
997 wxRendererNative::Get().DrawCheckBox(
998 GetOwner()->GetOwner(),
1006 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1007 wxDataViewModel
*model
,
1008 const wxDataViewItem
& item
,
1010 const wxMouseEvent
*mouseEvent
)
1012 if ( !model
->IsEnabled(item
, col
) )
1017 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1018 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1022 model
->ChangeValue(!m_toggle
, item
, col
);
1026 wxSize
wxDataViewToggleRenderer::GetSize() const
1028 // the window parameter is not used by GetCheckBoxSize() so it's
1029 // safe to pass NULL
1030 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1033 // ---------------------------------------------------------
1034 // wxDataViewProgressRenderer
1035 // ---------------------------------------------------------
1037 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1039 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1040 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1041 wxDataViewRenderer( varianttype
, mode
, align
)
1047 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1049 m_value
= (long) value
;
1051 if (m_value
< 0) m_value
= 0;
1052 if (m_value
> 100) m_value
= 100;
1057 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1059 value
= (long) m_value
;
1064 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1066 // deflate the rect to leave a small border between bars in adjacent rows
1067 wxRect bar
= rect
.Deflate(0, 1);
1069 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1070 dc
->SetPen( *wxBLACK_PEN
);
1071 dc
->DrawRectangle( bar
);
1073 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1074 dc
->SetPen( *wxTRANSPARENT_PEN
);
1076 const wxDataViewItemAttr
& attr
= GetAttr();
1077 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1079 dc
->DrawRectangle( bar
);
1084 wxSize
wxDataViewProgressRenderer::GetSize() const
1086 return wxSize(40,12);
1089 // ---------------------------------------------------------
1090 // wxDataViewIconTextRenderer
1091 // ---------------------------------------------------------
1093 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1095 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1096 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1097 wxDataViewRenderer( varianttype
, mode
, align
)
1100 SetAlignment(align
);
1103 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1109 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1114 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1118 const wxIcon
& icon
= m_value
.GetIcon();
1121 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1122 xoffset
= icon
.GetWidth()+4;
1125 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1130 wxSize
wxDataViewIconTextRenderer::GetSize() const
1132 if (!m_value
.GetText().empty())
1134 wxSize size
= GetTextExtent(m_value
.GetText());
1136 if (m_value
.GetIcon().IsOk())
1137 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1140 return wxSize(80,20);
1143 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1145 wxDataViewIconText iconText
;
1148 wxString text
= iconText
.GetText();
1150 // adjust the label rect to take the width of the icon into account
1151 if (iconText
.GetIcon().IsOk())
1153 int w
= iconText
.GetIcon().GetWidth() + 4;
1155 labelRect
.width
-= w
;
1158 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1159 wxPoint(labelRect
.x
,labelRect
.y
),
1160 wxSize(labelRect
.width
,labelRect
.height
),
1161 wxTE_PROCESS_ENTER
);
1163 // select the text in the control an place the cursor at the end
1164 ctrl
->SetInsertionPointEnd();
1170 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1172 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1174 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1179 //-----------------------------------------------------------------------------
1180 // wxDataViewDropTarget
1181 //-----------------------------------------------------------------------------
1183 #if wxUSE_DRAG_AND_DROP
1185 class wxBitmapCanvas
: public wxWindow
1188 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1189 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1192 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1195 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1198 dc
.DrawBitmap( m_bitmap
, 0, 0);
1204 class wxDataViewDropSource
: public wxDropSource
1207 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1215 ~wxDataViewDropSource()
1220 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1222 wxPoint pos
= wxGetMousePosition();
1226 int liney
= m_win
->GetLineStart( m_row
);
1228 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1229 m_win
->ClientToScreen( &linex
, &liney
);
1230 m_dist_x
= pos
.x
- linex
;
1231 m_dist_y
= pos
.y
- liney
;
1234 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1236 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1237 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1239 wxFRAME_TOOL_WINDOW
|
1240 wxFRAME_FLOAT_ON_PARENT
|
1241 wxFRAME_NO_TASKBAR
|
1243 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1248 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1249 m_hint
->SetTransparent( 128 );
1255 wxDataViewMainWindow
*m_win
;
1258 int m_dist_x
,m_dist_y
;
1262 class wxDataViewDropTarget
: public wxDropTarget
1265 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1271 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1273 wxDataFormat format
= GetMatchingPair();
1274 if (format
== wxDF_INVALID
)
1276 return m_win
->OnDragOver( format
, x
, y
, def
);
1279 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1281 wxDataFormat format
= GetMatchingPair();
1282 if (format
== wxDF_INVALID
)
1284 return m_win
->OnDrop( format
, x
, y
);
1287 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1289 wxDataFormat format
= GetMatchingPair();
1290 if (format
== wxDF_INVALID
)
1294 return m_win
->OnData( format
, x
, y
, def
);
1297 virtual void OnLeave()
1298 { m_win
->OnLeave(); }
1300 wxDataViewMainWindow
*m_win
;
1303 #endif // wxUSE_DRAG_AND_DROP
1305 //-----------------------------------------------------------------------------
1306 // wxDataViewRenameTimer
1307 //-----------------------------------------------------------------------------
1309 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1314 void wxDataViewRenameTimer::Notify()
1316 m_owner
->OnRenameTimer();
1319 //-----------------------------------------------------------------------------
1320 // wxDataViewMainWindow
1321 //-----------------------------------------------------------------------------
1323 // The tree building helper, declared firstly
1324 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1325 wxDataViewTreeNode
* node
);
1327 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1329 if (row1
> row2
) return 1;
1330 if (row1
== row2
) return 0;
1334 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1336 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1337 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1338 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1339 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1340 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1341 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1344 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1345 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1346 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1347 m_selection( wxDataViewSelectionCmp
)
1352 m_lastOnSame
= false;
1353 m_renameTimer
= new wxDataViewRenameTimer( this );
1355 // TODO: user better initial values/nothing selected
1356 m_currentCol
= NULL
;
1357 m_currentColSetByKeyboard
= false;
1358 m_useCellFocus
= false;
1361 m_lineHeight
= wxMax( 17, GetCharHeight() + 4 ); // 17 = mini icon height + 1
1363 #if wxUSE_DRAG_AND_DROP
1365 m_dragStart
= wxPoint(0,0);
1367 m_dragEnabled
= false;
1368 m_dropEnabled
= false;
1370 m_dropHintLine
= (unsigned int) -1;
1371 #endif // wxUSE_DRAG_AND_DROP
1373 m_lineLastClicked
= (unsigned int) -1;
1374 m_lineBeforeLastClicked
= (unsigned int) -1;
1375 m_lineSelectSingleOnUp
= (unsigned int) -1;
1379 SetBackgroundColour( *wxWHITE
);
1381 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1383 m_penRule
= wxPen(GetRuleColour());
1385 // compose a pen whichcan draw black lines
1386 // TODO: maybe there is something system colour to use
1387 m_penExpander
= wxPen(wxColour(0,0,0));
1389 m_root
= wxDataViewTreeNode::CreateRootNode();
1391 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1393 m_underMouse
= NULL
;
1397 wxDataViewMainWindow::~wxDataViewMainWindow()
1400 delete m_renameTimer
;
1404 #if wxUSE_DRAG_AND_DROP
1405 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1407 m_dragFormat
= format
;
1408 m_dragEnabled
= format
!= wxDF_INVALID
;
1413 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1415 m_dropFormat
= format
;
1416 m_dropEnabled
= format
!= wxDF_INVALID
;
1419 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1424 void wxDataViewMainWindow::RemoveDropHint()
1429 RefreshRow( m_dropHintLine
);
1430 m_dropHintLine
= (unsigned int) -1;
1434 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1435 wxCoord y
, wxDragResult def
)
1439 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1440 unsigned int row
= GetLineAt( yy
);
1442 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1448 wxDataViewItem item
= GetItemByRow( row
);
1450 wxDataViewModel
*model
= GetModel();
1452 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1453 event
.SetEventObject( m_owner
);
1454 event
.SetItem( item
);
1455 event
.SetModel( model
);
1456 event
.SetDataFormat( format
);
1457 if (!m_owner
->HandleWindowEvent( event
))
1463 if (!event
.IsAllowed())
1470 if (m_dropHint
&& (row
!= m_dropHintLine
))
1471 RefreshRow( m_dropHintLine
);
1473 m_dropHintLine
= row
;
1479 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1485 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1486 unsigned int row
= GetLineAt( yy
);
1488 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1491 wxDataViewItem item
= GetItemByRow( row
);
1493 wxDataViewModel
*model
= GetModel();
1495 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1496 event
.SetEventObject( m_owner
);
1497 event
.SetItem( item
);
1498 event
.SetModel( model
);
1499 event
.SetDataFormat( format
);
1500 if (!m_owner
->HandleWindowEvent( event
))
1503 if (!event
.IsAllowed())
1509 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1514 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1515 unsigned int row
= GetLineAt( yy
);
1517 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1520 wxDataViewItem item
= GetItemByRow( row
);
1522 wxDataViewModel
*model
= GetModel();
1524 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1526 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1527 event
.SetEventObject( m_owner
);
1528 event
.SetItem( item
);
1529 event
.SetModel( model
);
1530 event
.SetDataFormat( format
);
1531 event
.SetDataSize( obj
->GetSize() );
1532 event
.SetDataBuffer( obj
->GetData() );
1533 if (!m_owner
->HandleWindowEvent( event
))
1536 if (!event
.IsAllowed())
1542 void wxDataViewMainWindow::OnLeave()
1547 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1549 int height
= GetLineHeight( row
);
1551 unsigned int cols
= GetOwner()->GetColumnCount();
1553 for (col
= 0; col
< cols
; col
++)
1555 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1556 if (column
->IsHidden())
1557 continue; // skip it!
1558 width
+= column
->GetWidth();
1564 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1565 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1566 indent
= indent
+ m_lineHeight
;
1567 // try to use the m_lineHeight as the expander space
1571 wxBitmap
bitmap( width
, height
);
1572 wxMemoryDC
dc( bitmap
);
1573 dc
.SetFont( GetFont() );
1574 dc
.SetPen( *wxBLACK_PEN
);
1575 dc
.SetBrush( *wxWHITE_BRUSH
);
1576 dc
.DrawRectangle( 0,0,width
,height
);
1578 wxDataViewModel
*model
= m_owner
->GetModel();
1580 wxDataViewColumn
* const
1581 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1584 for (col
= 0; col
< cols
; col
++)
1586 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1587 wxDataViewRenderer
*cell
= column
->GetRenderer();
1589 if (column
->IsHidden())
1590 continue; // skip it!
1592 width
= column
->GetWidth();
1594 if (column
== expander
)
1597 wxDataViewItem item
= GetItemByRow( row
);
1598 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1600 wxRect
item_rect(x
, 0, width
, height
);
1601 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1603 // dc.SetClippingRegion( item_rect );
1604 cell
->WXCallRender(item_rect
, &dc
, 0);
1605 // dc.DestroyClippingRegion();
1613 #endif // wxUSE_DRAG_AND_DROP
1616 // Draw focus rect for individual cell. Unlike native focus rect, we render
1617 // this in foreground text color (typically white) to enhance contrast and
1619 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1621 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1623 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1624 // may be short traits and not really dots
1626 // note that to behave in the same manner as DrawRect(), we must exclude
1627 // the bottom and right borders from the rectangle
1628 wxCoord x1
= rect
.GetLeft(),
1630 x2
= rect
.GetRight(),
1631 y2
= rect
.GetBottom();
1633 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1636 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1637 dc
.DrawPoint(z
, rect
.GetTop());
1639 wxCoord shift
= z
== x2
? 0 : 1;
1640 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1641 dc
.DrawPoint(x2
, z
);
1643 shift
= z
== y2
? 0 : 1;
1644 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1645 dc
.DrawPoint(z
, y2
);
1647 shift
= z
== x1
? 0 : 1;
1648 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1649 dc
.DrawPoint(x1
, z
);
1653 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1655 wxDataViewModel
*model
= GetModel();
1656 wxAutoBufferedPaintDC
dc( this );
1659 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1660 dc
.SetPen( *wxTRANSPARENT_PEN
);
1661 dc
.DrawRectangle(GetClientSize());
1666 // No items to draw.
1671 GetOwner()->PrepareDC( dc
);
1672 dc
.SetFont( GetFont() );
1674 wxRect update
= GetUpdateRegion().GetBox();
1675 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1677 // compute which items needs to be redrawn
1678 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1679 unsigned int item_count
=
1680 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1681 (int)(GetRowCount( ) - item_start
));
1682 unsigned int item_last
= item_start
+ item_count
;
1684 // Send the event to wxDataViewCtrl itself.
1685 wxWindow
* const parent
= GetParent();
1686 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1687 cache_event
.SetEventObject(parent
);
1688 cache_event
.SetCache(item_start
, item_last
- 1);
1689 parent
->ProcessWindowEvent(cache_event
);
1691 // compute which columns needs to be redrawn
1692 unsigned int cols
= GetOwner()->GetColumnCount();
1695 // we assume that we have at least one column below and painting an
1696 // empty control is unnecessary anyhow
1700 unsigned int col_start
= 0;
1701 unsigned int x_start
;
1702 for (x_start
= 0; col_start
< cols
; col_start
++)
1704 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1705 if (col
->IsHidden())
1706 continue; // skip it!
1708 unsigned int w
= col
->GetWidth();
1709 if (x_start
+w
>= (unsigned int)update
.x
)
1715 unsigned int col_last
= col_start
;
1716 unsigned int x_last
= x_start
;
1717 for (; col_last
< cols
; col_last
++)
1719 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1720 if (col
->IsHidden())
1721 continue; // skip it!
1723 if (x_last
> (unsigned int)update
.GetRight())
1726 x_last
+= col
->GetWidth();
1729 // Draw horizontal rules if required
1730 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1732 dc
.SetPen(m_penRule
);
1733 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1735 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1737 int y
= GetLineStart( i
);
1738 dc
.DrawLine(x_start
, y
, x_last
, y
);
1742 // Draw vertical rules if required
1743 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1745 dc
.SetPen(m_penRule
);
1746 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1748 // NB: Vertical rules are drawn in the last pixel of a column so that
1749 // they align perfectly with native MSW wxHeaderCtrl as well as for
1750 // consistency with MSW native list control. There's no vertical
1751 // rule at the most-left side of the control.
1753 int x
= x_start
- 1;
1754 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1756 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1757 if (col
->IsHidden())
1758 continue; // skip it
1760 x
+= col
->GetWidth();
1762 dc
.DrawLine(x
, GetLineStart( item_start
),
1763 x
, GetLineStart( item_last
) );
1767 // redraw the background for the items which are selected/current
1768 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1770 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1772 if (selected
|| item
== m_currentRow
)
1774 wxRect
rect( x_start
, GetLineStart( item
),
1775 x_last
- x_start
, GetLineHeight( item
) );
1777 // draw selection and whole-item focus:
1780 int flags
= wxCONTROL_SELECTED
;
1782 flags
|= wxCONTROL_FOCUSED
;
1784 wxRendererNative::Get().DrawItemSelectionRect
1793 // draw keyboard focus rect if applicable
1794 if ( item
== m_currentRow
&& m_hasFocus
)
1796 bool renderColumnFocus
= false;
1798 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1800 renderColumnFocus
= true;
1802 // If this is container node without columns, render full-row focus:
1805 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1806 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1807 renderColumnFocus
= false;
1811 if ( renderColumnFocus
)
1813 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1815 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1816 if ( col
->IsHidden() )
1819 rect
.width
= col
->GetWidth();
1821 if ( col
== m_currentCol
)
1823 // make the rect more visible by adding a small
1824 // margin around it:
1829 // DrawFocusRect() uses XOR and is all but
1830 // invisible against dark-blue background. Use
1831 // the same color used for selected text.
1832 DrawSelectedCellFocusRect(dc
, rect
);
1836 wxRendererNative::Get().DrawFocusRect
1847 rect
.x
+= rect
.width
;
1852 // render focus rectangle for the whole row
1853 wxRendererNative::Get().DrawFocusRect
1858 selected
? (int)wxCONTROL_SELECTED
: 0
1865 #if wxUSE_DRAG_AND_DROP
1868 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1869 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1870 dc
.SetPen( *wxBLACK_PEN
);
1871 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1872 dc
.DrawRectangle( rect
);
1874 #endif // wxUSE_DRAG_AND_DROP
1876 wxDataViewColumn
* const
1877 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1879 // redraw all cells for all rows which must be repainted and all columns
1881 cell_rect
.x
= x_start
;
1882 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1884 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1885 wxDataViewRenderer
*cell
= col
->GetRenderer();
1886 cell_rect
.width
= col
->GetWidth();
1888 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1889 continue; // skip it!
1891 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1893 // get the cell value and set it into the renderer
1894 wxDataViewTreeNode
*node
= NULL
;
1895 wxDataViewItem dataitem
;
1897 if (!IsVirtualList())
1899 node
= GetTreeNodeByRow(item
);
1903 dataitem
= node
->GetItem();
1905 // Skip all columns of "container" rows except the expander
1906 // column itself unless HasContainerColumns() overrides this.
1907 if ( col
!= expander
&&
1908 model
->IsContainer(dataitem
) &&
1909 !model
->HasContainerColumns(dataitem
) )
1914 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1917 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1920 cell_rect
.y
= GetLineStart( item
);
1921 cell_rect
.height
= GetLineHeight( item
);
1923 // deal with the expander
1925 if ((!IsList()) && (col
== expander
))
1927 // Calculate the indent first
1928 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1930 // we reserve m_lineHeight of horizontal space for the expander
1931 // but leave EXPANDER_MARGIN around the expander itself
1932 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1934 indent
+= m_lineHeight
;
1936 // draw expander if needed and visible
1937 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1939 dc
.SetPen( m_penExpander
);
1940 dc
.SetBrush( wxNullBrush
);
1942 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1943 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1944 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1946 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1949 if ( m_underMouse
== node
)
1950 flag
|= wxCONTROL_CURRENT
;
1951 if ( node
->IsOpen() )
1952 flag
|= wxCONTROL_EXPANDED
;
1954 // ensure that we don't overflow the cell (which might
1955 // happen if the column is very narrow)
1956 wxDCClipper
clip(dc
, cell_rect
);
1958 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1961 // force the expander column to left-center align
1962 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1965 wxRect item_rect
= cell_rect
;
1966 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1968 // account for the tree indent (harmless if we're not indented)
1969 item_rect
.x
+= indent
;
1970 item_rect
.width
-= indent
;
1972 if ( item_rect
.width
<= 0 )
1976 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1977 state
|= wxDATAVIEW_CELL_SELECTED
;
1979 // TODO: it would be much more efficient to create a clipping
1980 // region for the entire column being rendered (in the OnPaint
1981 // of wxDataViewMainWindow) instead of a single clip region for
1982 // each cell. However it would mean that each renderer should
1983 // respect the given wxRect's top & bottom coords, eventually
1984 // violating only the left & right coords - however the user can
1985 // make its own renderer and thus we cannot be sure of that.
1986 wxDCClipper
clip(dc
, item_rect
);
1988 cell
->WXCallRender(item_rect
, &dc
, state
);
1991 cell_rect
.x
+= cell_rect
.width
;
1995 void wxDataViewMainWindow::OnRenameTimer()
1997 // We have to call this here because changes may just have
1998 // been made and no screen update taken place.
2001 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2002 // (needs to be tested!)
2006 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2008 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
2010 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2013 //-----------------------------------------------------------------------------
2014 // Helper class for do operation on the tree node
2015 //-----------------------------------------------------------------------------
2020 virtual ~DoJob() { }
2022 // The return value control how the tree-walker tranverse the tree
2025 DONE
, // Job done, stop traversing and return
2026 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2027 CONTINUE
// Job not done, continue
2030 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2033 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2035 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2037 switch( func( node
) )
2041 case DoJob::SKIP_SUBTREE
:
2043 case DoJob::CONTINUE
:
2047 if ( node
->HasChildren() )
2049 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2051 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2055 if ( Walker(*i
, func
) )
2063 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2065 if (IsVirtualList())
2067 wxDataViewVirtualListModel
*list_model
=
2068 (wxDataViewVirtualListModel
*) GetModel();
2069 m_count
= list_model
->GetCount();
2075 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2080 wxDataViewItemArray modelSiblings
;
2081 GetModel()->GetChildren(parent
, modelSiblings
);
2082 const int modelSiblingsSize
= modelSiblings
.size();
2084 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2085 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2087 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2088 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2090 parentNode
->SetHasChildren(true);
2092 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2093 const int nodeSiblingsSize
= nodeSiblings
.size();
2097 if ( posInModel
== modelSiblingsSize
- 1 )
2099 nodePos
= nodeSiblingsSize
;
2101 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2103 // This is the simple case when our node tree already matches the
2104 // model and only this one item is missing.
2105 nodePos
= posInModel
;
2109 // It's possible that a larger discrepancy between the model and
2110 // our realization exists. This can happen e.g. when adding a bunch
2111 // of items to the model and then calling ItemsAdded() just once
2112 // afterwards. In this case, we must find the right position by
2113 // looking at sibling items.
2115 // append to the end if we won't find a better position:
2116 nodePos
= nodeSiblingsSize
;
2118 for ( int nextItemPos
= posInModel
+ 1;
2119 nextItemPos
< modelSiblingsSize
;
2122 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2123 if ( nextNodePos
!= wxNOT_FOUND
)
2125 nodePos
= nextNodePos
;
2131 parentNode
->ChangeSubTreeCount(+1);
2132 parentNode
->InsertChild(itemNode
, nodePos
);
2137 GetOwner()->InvalidateColBestWidths();
2143 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2144 const wxDataViewItem
& item
)
2146 if (IsVirtualList())
2148 wxDataViewVirtualListModel
*list_model
=
2149 (wxDataViewVirtualListModel
*) GetModel();
2150 m_count
= list_model
->GetCount();
2152 if ( !m_selection
.empty() )
2154 const int row
= GetRowByItem(item
);
2156 int rowIndexInSelection
= wxNOT_FOUND
;
2158 const size_t selCount
= m_selection
.size();
2159 for ( size_t i
= 0; i
< selCount
; i
++ )
2161 if ( m_selection
[i
] == (unsigned)row
)
2162 rowIndexInSelection
= i
;
2163 else if ( m_selection
[i
] > (unsigned)row
)
2167 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2168 m_selection
.RemoveAt(rowIndexInSelection
);
2172 else // general case
2174 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2176 // Notice that it is possible that the item being deleted is not in the
2177 // tree at all, for example we could be deleting a never shown (because
2178 // collapsed) item in a tree model. So it's not an error if we don't know
2179 // about this item, just return without doing anything then.
2183 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2184 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2186 // We can't use FindNode() to find 'item', because it was already
2187 // removed from the model by the time ItemDeleted() is called, so we
2188 // have to do it manually. We keep track of its position as well for
2190 int itemPosInNode
= 0;
2191 wxDataViewTreeNode
*itemNode
= NULL
;
2192 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2193 i
!= parentsChildren
.end();
2194 ++i
, ++itemPosInNode
)
2196 if( (*i
)->GetItem() == item
)
2203 // If the parent wasn't expanded, it's possible that we didn't have a
2204 // node corresponding to 'item' and so there's nothing left to do.
2207 // If this was the last child to be removed, it's possible the parent
2208 // node became a leaf. Let's ask the model about it.
2209 if ( parentNode
->GetChildNodes().empty() )
2210 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2215 // Delete the item from wxDataViewTreeNode representation:
2216 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2218 parentNode
->RemoveChild(itemNode
);
2220 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2222 // Make the row number invalid and get a new valid one when user call GetRowCount
2225 // If this was the last child to be removed, it's possible the parent
2226 // node became a leaf. Let's ask the model about it.
2227 if ( parentNode
->GetChildNodes().empty() )
2228 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2230 // Update selection by removing 'item' and its entire children tree from the selection.
2231 if ( !m_selection
.empty() )
2233 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2234 // the parent ('parentNode') and position in its list of children
2236 if ( itemPosInNode
== 0 )
2238 // 1st child, row number is that of the parent parentNode + 1
2239 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2243 // row number is that of the sibling above 'item' + its subtree if any + 1
2244 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2246 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2247 siblingNode
->GetSubTreeCount() +
2251 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2253 const size_t numSelections
= m_selection
.size();
2254 for ( size_t i
= 0; i
< numSelections
; ++i
)
2256 const int s
= m_selection
[i
];
2258 newsel
.push_back(s
);
2259 else if ( s
>= itemRow
+ itemsDeleted
)
2260 newsel
.push_back(s
- itemsDeleted
);
2261 // else: deleted item, remove from selection
2264 m_selection
= newsel
;
2268 // Change the current row to the last row if the current exceed the max row number
2269 if( m_currentRow
> GetRowCount() )
2270 ChangeCurrentRow(m_count
- 1);
2272 GetOwner()->InvalidateColBestWidths();
2278 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2283 GetOwner()->InvalidateColBestWidths();
2286 wxWindow
*parent
= GetParent();
2287 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2288 le
.SetEventObject(parent
);
2289 le
.SetModel(GetModel());
2291 parent
->ProcessWindowEvent(le
);
2296 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2298 int view_column
= -1;
2299 unsigned int n_col
= m_owner
->GetColumnCount();
2300 for (unsigned i
= 0; i
< n_col
; i
++)
2302 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2303 if (column
->GetModelColumn() == model_column
)
2305 view_column
= (int) i
;
2309 if (view_column
== -1)
2312 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2313 /*#define MAX_VIRTUAL_WIDTH 100000
2315 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2316 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2317 Refresh( true, &rect );
2324 GetOwner()->InvalidateColBestWidth(view_column
);
2327 wxWindow
*parent
= GetParent();
2328 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2329 le
.SetEventObject(parent
);
2330 le
.SetModel(GetModel());
2332 le
.SetColumn(view_column
);
2333 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2334 parent
->ProcessWindowEvent(le
);
2339 bool wxDataViewMainWindow::Cleared()
2342 m_selection
.Clear();
2345 BuildTree( GetModel() );
2347 GetOwner()->InvalidateColBestWidths();
2353 void wxDataViewMainWindow::UpdateDisplay()
2356 m_underMouse
= NULL
;
2359 void wxDataViewMainWindow::OnInternalIdle()
2361 wxWindow::OnInternalIdle();
2365 RecalculateDisplay();
2370 void wxDataViewMainWindow::RecalculateDisplay()
2372 wxDataViewModel
*model
= GetModel();
2379 int width
= GetEndOfLastCol();
2380 int height
= GetLineStart( GetRowCount() );
2382 SetVirtualSize( width
, height
);
2383 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2388 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2390 m_underMouse
= NULL
;
2392 wxWindow::ScrollWindow( dx
, dy
, rect
);
2394 if (GetOwner()->m_headerArea
)
2395 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2398 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2400 m_underMouse
= NULL
;
2403 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2404 int sy
= GetLineStart( rows
)/y
;
2408 wxRect rect
= GetClientRect();
2412 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2413 for (x_start
= 0; colnum
< column
; colnum
++)
2415 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2416 if (col
->IsHidden())
2417 continue; // skip it!
2419 w
= col
->GetWidth();
2423 int x_end
= x_start
+ w
;
2424 xe
= xx
+ rect
.width
;
2427 sx
= ( xx
+ x_end
- xe
)/x
;
2434 m_owner
->Scroll( sx
, sy
);
2437 int wxDataViewMainWindow::GetCountPerPage() const
2439 wxSize size
= GetClientSize();
2440 return size
.y
/ m_lineHeight
;
2443 int wxDataViewMainWindow::GetEndOfLastCol() const
2447 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2449 const wxDataViewColumn
*c
=
2450 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2453 width
+= c
->GetWidth();
2458 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2462 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2464 return GetLineAt( y
);
2467 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2469 wxSize client_size
= GetClientSize();
2470 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2471 &client_size
.x
, &client_size
.y
);
2473 // we should deal with the pixel here
2474 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2476 return wxMin( GetRowCount()-1, row
);
2479 unsigned int wxDataViewMainWindow::GetRowCount()
2481 if ( m_count
== -1 )
2483 m_count
= RecalculateCount();
2489 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2496 void wxDataViewMainWindow::SelectAllRows( bool on
)
2503 m_selection
.Clear();
2504 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2505 m_selection
.Add( i
);
2510 unsigned int first_visible
= GetFirstVisibleRow();
2511 unsigned int last_visible
= GetLastVisibleRow();
2513 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2515 unsigned int row
= m_selection
[i
];
2516 if ((row
>= first_visible
) && (row
<= last_visible
))
2519 m_selection
.Clear();
2523 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2525 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2529 m_selection
.Add( row
);
2537 m_selection
.Remove( row
);
2543 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2547 unsigned int tmp
= from
;
2553 for (i
= from
; i
<= to
; i
++)
2555 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2558 m_selection
.Add( i
);
2563 m_selection
.Remove( i
);
2566 RefreshRows( from
, to
);
2569 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2571 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2573 int n
= aSelections
[i
];
2575 m_selection
.Add( n
);
2580 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2582 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2583 m_selection
.Add( row
);
2585 m_selection
.Remove( row
);
2589 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2591 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2594 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2596 wxWindow
*parent
= GetParent();
2597 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2599 le
.SetEventObject(parent
);
2600 le
.SetModel(GetModel());
2603 parent
->ProcessWindowEvent(le
);
2606 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2608 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2609 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2611 wxSize client_size
= GetClientSize();
2612 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2613 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2614 if (intersect_rect
.width
> 0)
2615 Refresh( true, &intersect_rect
);
2618 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2622 unsigned int tmp
= to
;
2627 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2628 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2630 wxSize client_size
= GetClientSize();
2631 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2632 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2633 if (intersect_rect
.width
> 0)
2634 Refresh( true, &intersect_rect
);
2637 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2639 wxSize client_size
= GetClientSize();
2640 int start
= GetLineStart( firstRow
);
2641 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2642 if (start
> client_size
.y
) return;
2644 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2646 Refresh( true, &rect
);
2649 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2653 rect
.y
= GetLineStart( row
);
2654 rect
.width
= GetEndOfLastCol();
2655 rect
.height
= GetLineHeight( row
);
2660 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2662 const wxDataViewModel
*model
= GetModel();
2664 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2666 // TODO make more efficient
2671 for (r
= 0; r
< row
; r
++)
2673 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2674 if (!node
) return start
;
2676 wxDataViewItem item
= node
->GetItem();
2678 unsigned int cols
= GetOwner()->GetColumnCount();
2680 int height
= m_lineHeight
;
2681 for (col
= 0; col
< cols
; col
++)
2683 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2684 if (column
->IsHidden())
2685 continue; // skip it!
2688 model
->IsContainer(item
) &&
2689 !model
->HasContainerColumns(item
))
2690 continue; // skip it!
2692 wxDataViewRenderer
*renderer
=
2693 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2694 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2696 height
= wxMax( height
, renderer
->GetSize().y
);
2706 return row
* m_lineHeight
;
2710 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2712 const wxDataViewModel
*model
= GetModel();
2714 // check for the easy case first
2715 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2716 return y
/ m_lineHeight
;
2718 // TODO make more efficient
2719 unsigned int row
= 0;
2720 unsigned int yy
= 0;
2723 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2726 // not really correct...
2727 return row
+ ((y
-yy
) / m_lineHeight
);
2730 wxDataViewItem item
= node
->GetItem();
2732 unsigned int cols
= GetOwner()->GetColumnCount();
2734 int height
= m_lineHeight
;
2735 for (col
= 0; col
< cols
; col
++)
2737 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2738 if (column
->IsHidden())
2739 continue; // skip it!
2742 model
->IsContainer(item
) &&
2743 !model
->HasContainerColumns(item
))
2744 continue; // skip it!
2746 wxDataViewRenderer
*renderer
=
2747 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2748 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2750 height
= wxMax( height
, renderer
->GetSize().y
);
2761 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2763 const wxDataViewModel
*model
= GetModel();
2765 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2767 wxASSERT( !IsVirtualList() );
2769 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2770 // wxASSERT( node );
2771 if (!node
) return m_lineHeight
;
2773 wxDataViewItem item
= node
->GetItem();
2775 int height
= m_lineHeight
;
2777 unsigned int cols
= GetOwner()->GetColumnCount();
2779 for (col
= 0; col
< cols
; col
++)
2781 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2782 if (column
->IsHidden())
2783 continue; // skip it!
2786 model
->IsContainer(item
) &&
2787 !model
->HasContainerColumns(item
))
2788 continue; // skip it!
2790 wxDataViewRenderer
*renderer
=
2791 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2792 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2794 height
= wxMax( height
, renderer
->GetSize().y
);
2801 return m_lineHeight
;
2806 class RowToTreeNodeJob
: public DoJob
2809 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2812 this->current
= current
;
2817 virtual int operator() ( wxDataViewTreeNode
* node
)
2820 if( current
== static_cast<int>(row
))
2826 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2828 current
+= node
->GetSubTreeCount();
2829 return DoJob::SKIP_SUBTREE
;
2835 // If the current node has only leaf children, we can find the
2836 // desired node directly. This can speed up finding the node
2837 // in some cases, and will have a very good effect for list views.
2838 if ( node
->HasChildren() &&
2839 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2841 const int index
= static_cast<int>(row
) - current
- 1;
2842 ret
= node
->GetChildNodes()[index
];
2846 return DoJob::CONTINUE
;
2850 wxDataViewTreeNode
* GetResult() const
2856 wxDataViewTreeNode
* ret
;
2857 wxDataViewTreeNode
* parent
;
2860 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2862 wxASSERT( !IsVirtualList() );
2864 RowToTreeNodeJob
job( row
, -2, m_root
);
2865 Walker( m_root
, job
);
2866 return job
.GetResult();
2869 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2871 if (IsVirtualList())
2873 return wxDataViewItem( wxUIntToPtr(row
+1) );
2877 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2878 return node
? node
->GetItem() : wxDataViewItem();
2883 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2884 const wxDataViewItem
& item
)
2886 wxWindow
*parent
= GetParent();
2887 wxDataViewEvent
le(type
, parent
->GetId());
2889 le
.SetEventObject(parent
);
2890 le
.SetModel(GetModel());
2893 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2896 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2901 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2905 if (!node
->HasChildren())
2908 return node
->IsOpen();
2911 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2916 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2920 if (!node
->HasChildren())
2926 void wxDataViewMainWindow::Expand( unsigned int row
)
2931 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2935 if (!node
->HasChildren())
2938 if (!node
->IsOpen())
2940 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
2942 // Vetoed by the event handler.
2948 // build the children of current node
2949 if( node
->GetChildNodes().empty() )
2952 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
2955 // By expanding the node all row indices that are currently in the selection list
2956 // and are greater than our node have become invalid. So we have to correct that now.
2957 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2958 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2960 const unsigned testRow
= m_selection
[i
];
2961 // all rows above us are not affected, so skip them
2965 m_selection
[i
] += rowAdjustment
;
2968 if(m_currentRow
> row
)
2969 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2973 // Send the expanded event
2974 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2978 void wxDataViewMainWindow::Collapse(unsigned int row
)
2983 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2987 if (!node
->HasChildren())
2992 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
2994 // Vetoed by the event handler.
2998 // Find out if there are selected items below the current node.
2999 bool selectCollapsingRow
= false;
3000 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3001 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3002 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3004 const unsigned testRow
= m_selection
[i
];
3005 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3007 selectCollapsingRow
= true;
3008 // get out as soon as we have found a node that is selected
3015 // If the node to be closed has selected items the user won't see those any longer.
3016 // We select the collapsing node in this case.
3017 if(selectCollapsingRow
)
3019 SelectAllRows(false);
3020 ChangeCurrentRow(row
);
3021 SelectRow(row
, true);
3022 SendSelectionChangedEvent(GetItemByRow(row
));
3026 // if there were no selected items below our node we still need to "fix" the
3027 // selection list to adjust for the changing of the row indices.
3028 // We actually do the opposite of what we are doing in Expand().
3029 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3031 const unsigned testRow
= m_selection
[i
];
3032 // all rows above us are not affected, so skip them
3036 m_selection
[i
] -= rowAdjustment
;
3039 // if the "current row" is being collapsed away we change it to the current row ;-)
3040 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3041 ChangeCurrentRow(row
);
3042 else if(m_currentRow
> row
)
3043 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3048 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3052 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3054 const wxDataViewModel
* model
= GetModel();
3061 // Compose the parent-chain for the item we are looking for
3062 wxVector
<wxDataViewItem
> parentChain
;
3063 wxDataViewItem
it( item
);
3066 parentChain
.push_back(it
);
3067 it
= model
->GetParent(it
);
3070 // Find the item along the parent-chain.
3071 // This algorithm is designed to speed up the node-finding method
3072 wxDataViewTreeNode
* node
= m_root
;
3073 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3075 if( node
->HasChildren() )
3077 if( node
->GetChildNodes().empty() )
3079 // Even though the item is a container, it doesn't have any
3080 // child nodes in the control's representation yet. We have
3081 // to realize its subtree now.
3083 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3086 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3089 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3091 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3092 if (currentNode
->GetItem() == parentChain
[iter
])
3094 if (currentNode
->GetItem() == item
)
3114 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3115 wxDataViewColumn
* &column
)
3117 wxDataViewColumn
*col
= NULL
;
3118 unsigned int cols
= GetOwner()->GetColumnCount();
3119 unsigned int colnum
= 0;
3121 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3122 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3124 col
= GetOwner()->GetColumnAt(colnum
);
3125 if (col
->IsHidden())
3126 continue; // skip it!
3128 unsigned int w
= col
->GetWidth();
3129 if (x_start
+w
>= (unsigned int)x
)
3136 item
= GetItemByRow( GetLineAt( y
) );
3139 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3140 const wxDataViewColumn
* column
)
3145 unsigned int cols
= GetOwner()->GetColumnCount();
3146 // If column is null the loop will compute the combined width of all columns.
3147 // Otherwise, it will compute the x position of the column we are looking for.
3148 for (unsigned int i
= 0; i
< cols
; i
++)
3150 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3155 if (col
->IsHidden())
3156 continue; // skip it!
3158 xpos
+= col
->GetWidth();
3159 width
+= col
->GetWidth();
3164 // If we have a column, we need can get its width directly.
3165 if(column
->IsHidden())
3168 width
= column
->GetWidth();
3173 // If we have no column, we reset the x position back to zero.
3177 // we have to take an expander column into account and compute its indentation
3178 // to get the correct x position where the actual text is
3180 int row
= GetRowByItem(item
);
3182 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3184 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3185 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3186 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3189 wxRect
itemRect( xpos
+ indent
,
3190 GetLineStart( row
),
3192 GetLineHeight( row
) );
3194 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3195 &itemRect
.x
, &itemRect
.y
);
3200 int wxDataViewMainWindow::RecalculateCount()
3202 if (IsVirtualList())
3204 wxDataViewVirtualListModel
*list_model
=
3205 (wxDataViewVirtualListModel
*) GetModel();
3207 return list_model
->GetCount();
3211 return m_root
->GetSubTreeCount();
3215 class ItemToRowJob
: public DoJob
3218 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3225 // Maybe binary search will help to speed up this process
3226 virtual int operator() ( wxDataViewTreeNode
* node
)
3229 if( node
->GetItem() == item
)
3234 if( node
->GetItem() == *m_iter
)
3237 return DoJob::CONTINUE
;
3241 ret
+= node
->GetSubTreeCount();
3242 return DoJob::SKIP_SUBTREE
;
3247 // the row number is begin from zero
3248 int GetResult() const
3252 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3253 wxDataViewItem item
;
3258 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3260 const wxDataViewModel
* model
= GetModel();
3264 if (IsVirtualList())
3266 return wxPtrToUInt( item
.GetID() ) -1;
3273 // Compose the parent-chain of the item we are looking for
3274 wxVector
<wxDataViewItem
> parentChain
;
3275 wxDataViewItem
it( item
);
3278 parentChain
.push_back(it
);
3279 it
= model
->GetParent(it
);
3282 // add an 'invalid' item to represent our 'invisible' root node
3283 parentChain
.push_back(wxDataViewItem());
3285 // the parent chain was created by adding the deepest parent first.
3286 // so if we want to start at the root node, we have to iterate backwards through the vector
3287 ItemToRowJob
job( item
, parentChain
.rbegin() );
3288 Walker( m_root
, job
);
3289 return job
.GetResult();
3293 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3294 wxDataViewTreeNode
* node
)
3296 if( !model
->IsContainer( item
) )
3299 wxDataViewItemArray children
;
3300 unsigned int num
= model
->GetChildren( item
, children
);
3302 for ( unsigned int index
= 0; index
< num
; index
++ )
3304 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3306 if( model
->IsContainer(children
[index
]) )
3307 n
->SetHasChildren( true );
3309 node
->InsertChild(n
, index
);
3312 wxASSERT( node
->IsOpen() );
3313 node
->ChangeSubTreeCount(+num
);
3316 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3320 if (GetModel()->IsVirtualListModel())
3326 m_root
= wxDataViewTreeNode::CreateRootNode();
3328 // First we define a invalid item to fetch the top-level elements
3329 wxDataViewItem item
;
3331 BuildTreeHelper( model
, item
, m_root
);
3335 void wxDataViewMainWindow::DestroyTree()
3337 if (!IsVirtualList())
3345 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3347 // Edit the current column editable in 'mode'. If no column is focused
3348 // (typically because the user has full row selected), try to find the
3349 // first editable column (this would typically be a checkbox for
3350 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3351 // focus on the checkbox column; or on the only editable text column).
3353 wxDataViewColumn
*candidate
= m_currentCol
;
3356 candidate
->GetRenderer()->GetMode() != mode
&&
3357 !m_currentColSetByKeyboard
)
3359 // If current column was set by mouse to something not editable (in
3360 // 'mode') and the user pressed Space/F2 to edit it, treat the
3361 // situation as if there was whole-row focus, because that's what is
3362 // visually indicated and the mouse click could very well be targeted
3363 // on the row rather than on an individual cell.
3365 // But if it was done by keyboard, respect that even if the column
3366 // isn't editable, because focus is visually on that column and editing
3367 // something else would be surprising.
3373 const unsigned cols
= GetOwner()->GetColumnCount();
3374 for ( unsigned i
= 0; i
< cols
; i
++ )
3376 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3377 if ( c
->IsHidden() )
3380 if ( c
->GetRenderer()->GetMode() == mode
)
3388 // If on container item without columns, only the expander column
3389 // may be directly editable:
3391 GetOwner()->GetExpanderColumn() != candidate
&&
3392 GetModel()->IsContainer(item
) &&
3393 !GetModel()->HasContainerColumns(item
) )
3395 candidate
= GetOwner()->GetExpanderColumn();
3401 if ( candidate
->GetRenderer()->GetMode() != mode
)
3407 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3409 wxWindow
* const parent
= GetParent();
3411 // propagate the char event upwards
3412 wxKeyEvent
eventForParent(event
);
3413 eventForParent
.SetEventObject(parent
);
3414 if ( parent
->ProcessWindowEvent(eventForParent
) )
3417 if ( parent
->HandleAsNavigationKey(event
) )
3420 // no item -> nothing to do
3421 if (!HasCurrentRow())
3427 // don't use m_linesPerPage directly as it might not be computed yet
3428 const int pageSize
= GetCountPerPage();
3429 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3431 switch ( event
.GetKeyCode() )
3435 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3436 // it. Only if that event is not handled do we activate column renderer (which
3437 // is normally done by Space) or even inline editing.
3439 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3441 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3444 le
.SetEventObject(parent
);
3445 le
.SetModel(GetModel());
3447 if ( parent
->ProcessWindowEvent(le
) )
3449 // else: fall through to WXK_SPACE handling
3454 // Space toggles activatable items or -- if not activatable --
3455 // starts inline editing (this is normally done using F2 on
3456 // Windows, but Space is common everywhere else, so use it too
3457 // for greater cross-platform compatibility).
3459 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3461 // Activate the current activatable column. If not column is focused (typically
3462 // because the user has full row selected), try to find the first activatable
3463 // column (this would typically be a checkbox and we don't want to force the user
3464 // to set focus on the checkbox column).
3465 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3467 if ( activatableCol
)
3469 const unsigned colIdx
= activatableCol
->GetModelColumn();
3470 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3472 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3473 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3474 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3478 // else: fall through to WXK_F2 handling
3483 if( !m_selection
.empty() )
3485 // Mimic Windows 7 behavior: edit the item that has focus
3486 // if it is selected and the first selected item if focus
3487 // is out of selection.
3489 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3492 sel
= m_selection
[0];
3495 const wxDataViewItem item
= GetItemByRow(sel
);
3497 // Edit the current column. If no column is focused
3498 // (typically because the user has full row selected), try
3499 // to find the first editable column.
3500 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3503 GetOwner()->StartEditor(item
, GetOwner()->GetColumnIndex(editableCol
));
3509 if ( m_currentRow
> 0 )
3510 OnVerticalNavigation( m_currentRow
- 1, event
);
3514 if ( m_currentRow
+ 1 < GetRowCount() )
3515 OnVerticalNavigation( m_currentRow
+ 1, event
);
3517 // Add the process for tree expanding/collapsing
3529 OnVerticalNavigation( GetRowCount() - 1, event
);
3534 OnVerticalNavigation( 0, event
);
3539 int steps
= pageSize
- 1;
3540 int index
= m_currentRow
- steps
;
3544 OnVerticalNavigation( index
, event
);
3550 int steps
= pageSize
- 1;
3551 unsigned int index
= m_currentRow
+ steps
;
3552 unsigned int count
= GetRowCount();
3553 if ( index
>= count
)
3556 OnVerticalNavigation( index
, event
);
3565 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3567 wxCHECK_RET( newCurrent
< GetRowCount(),
3568 wxT("invalid item index in OnVerticalNavigation()") );
3570 // if there is no selection, we cannot move it anywhere
3571 if (!HasCurrentRow())
3574 unsigned int oldCurrent
= m_currentRow
;
3576 // in single selection we just ignore Shift as we can't select several
3578 if ( event
.ShiftDown() && !IsSingleSel() )
3580 RefreshRow( oldCurrent
);
3582 ChangeCurrentRow( newCurrent
);
3584 // select all the items between the old and the new one
3585 if ( oldCurrent
> newCurrent
)
3587 newCurrent
= oldCurrent
;
3588 oldCurrent
= m_currentRow
;
3591 SelectRows( oldCurrent
, newCurrent
, true );
3592 if (oldCurrent
!=newCurrent
)
3593 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3597 RefreshRow( oldCurrent
);
3599 // all previously selected items are unselected unless ctrl is held
3600 if ( !event
.ControlDown() )
3601 SelectAllRows(false);
3603 ChangeCurrentRow( newCurrent
);
3605 if ( !event
.ControlDown() )
3607 SelectRow( m_currentRow
, true );
3608 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3611 RefreshRow( m_currentRow
);
3614 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3617 void wxDataViewMainWindow::OnLeftKey()
3621 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3625 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3627 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3630 // Because TryAdvanceCurrentColumn() return false, we are at the first
3631 // column or using whole-row selection. In this situation, we can use
3632 // the standard TreeView handling of the left key.
3633 if (node
->HasChildren() && node
->IsOpen())
3635 Collapse(m_currentRow
);
3639 // if the node is already closed, we move the selection to its parent
3640 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3644 int parent
= GetRowByItem( parent_node
->GetItem() );
3647 unsigned int row
= m_currentRow
;
3648 SelectRow( row
, false);
3649 SelectRow( parent
, true );
3650 ChangeCurrentRow( parent
);
3651 GetOwner()->EnsureVisible( parent
, -1 );
3652 SendSelectionChangedEvent( parent_node
->GetItem() );
3659 void wxDataViewMainWindow::OnRightKey()
3663 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3667 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3669 if ( node
->HasChildren() )
3671 if ( !node
->IsOpen() )
3673 Expand( m_currentRow
);
3677 // if the node is already open, we move the selection to the first child
3678 unsigned int row
= m_currentRow
;
3679 SelectRow( row
, false );
3680 SelectRow( row
+ 1, true );
3681 ChangeCurrentRow( row
+ 1 );
3682 GetOwner()->EnsureVisible( row
+ 1, -1 );
3683 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3688 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3693 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3695 if ( GetOwner()->GetColumnCount() == 0 )
3698 if ( !m_useCellFocus
)
3703 // navigation shouldn't work in branch nodes without other columns:
3704 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3708 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3712 m_currentCol
= GetOwner()->GetColumnAt(1);
3713 m_currentColSetByKeyboard
= true;
3714 RefreshRow(m_currentRow
);
3721 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3723 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3728 // We are going to the left of the second column. Reset to whole-row
3729 // focus (which means first column would be edited).
3730 m_currentCol
= NULL
;
3731 RefreshRow(m_currentRow
);
3735 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3736 m_currentColSetByKeyboard
= true;
3737 RefreshRow(m_currentRow
);
3741 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3743 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3745 // let the base handle mouse wheel events.
3750 // set the focus to ourself if any of the mouse buttons are pressed
3751 if(event
.ButtonDown() && !HasFocus())
3754 int x
= event
.GetX();
3755 int y
= event
.GetY();
3756 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3757 wxDataViewColumn
*col
= NULL
;
3760 unsigned int cols
= GetOwner()->GetColumnCount();
3762 for (i
= 0; i
< cols
; i
++)
3764 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3766 continue; // skip it!
3768 if (x
< xpos
+ c
->GetWidth())
3773 xpos
+= c
->GetWidth();
3776 wxDataViewModel
* const model
= GetModel();
3778 const unsigned int current
= GetLineAt( y
);
3779 const wxDataViewItem item
= GetItemByRow(current
);
3781 // Handle right clicking here, before everything else as context menu
3782 // events should be sent even when we click outside of any item, unlike all
3784 if (event
.RightUp())
3786 wxWindow
*parent
= GetParent();
3787 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3788 le
.SetEventObject(parent
);
3791 if ( item
.IsOk() && col
)
3794 le
.SetColumn( col
->GetModelColumn() );
3795 le
.SetDataViewColumn( col
);
3798 model
->GetValue( value
, item
, col
->GetModelColumn() );
3802 parent
->ProcessWindowEvent(le
);
3812 wxDataViewRenderer
*cell
= col
->GetRenderer();
3813 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3815 // Unselect all if below the last row ?
3820 wxDataViewColumn
* const
3821 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3823 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3824 // button) and also determine the offset of the real cell start, skipping
3825 // the indentation and the expander itself.
3826 bool hoverOverExpander
= false;
3828 if ((!IsList()) && (expander
== col
))
3830 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3832 int indent
= node
->GetIndentLevel();
3833 itemOffset
= GetOwner()->GetIndent()*indent
;
3835 if ( node
->HasChildren() )
3837 // we make the rectangle we are looking in a bit bigger than the actual
3838 // visual expander so the user can hit that little thing reliably
3839 wxRect
rect(itemOffset
,
3840 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3841 m_lineHeight
, m_lineHeight
);
3843 if( rect
.Contains(x
, y
) )
3845 // So the mouse is over the expander
3846 hoverOverExpander
= true;
3847 if (m_underMouse
&& m_underMouse
!= node
)
3849 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3850 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3852 if (m_underMouse
!= node
)
3854 // wxLogMessage("Do the row: %d", current);
3855 RefreshRow(current
);
3857 m_underMouse
= node
;
3861 // Account for the expander as well, even if this item doesn't have it,
3862 // its parent does so it still counts for the offset.
3863 itemOffset
+= m_lineHeight
;
3865 if (!hoverOverExpander
)
3867 if (m_underMouse
!= NULL
)
3869 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3870 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3871 m_underMouse
= NULL
;
3875 #if wxUSE_DRAG_AND_DROP
3876 if (event
.Dragging())
3878 if (m_dragCount
== 0)
3880 // we have to report the raw, physical coords as we want to be
3881 // able to call HitTest(event.m_pointDrag) from the user code to
3882 // get the item being dragged
3883 m_dragStart
= event
.GetPosition();
3888 if (m_dragCount
!= 3)
3891 if (event
.LeftIsDown())
3893 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3894 &m_dragStart
.x
, &m_dragStart
.y
);
3895 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3896 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
3898 // Notify cell about drag
3899 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3900 event
.SetEventObject( m_owner
);
3901 event
.SetItem( itemDragged
);
3902 event
.SetModel( model
);
3903 if (!m_owner
->HandleWindowEvent( event
))
3906 if (!event
.IsAllowed())
3909 wxDataObject
*obj
= event
.GetDataObject();
3913 wxDataViewDropSource
drag( this, drag_item_row
);
3914 drag
.SetData( *obj
);
3915 /* wxDragResult res = */ drag
.DoDragDrop();
3924 #endif // wxUSE_DRAG_AND_DROP
3926 bool simulateClick
= false;
3928 if (event
.ButtonDClick())
3930 m_renameTimer
->Stop();
3931 m_lastOnSame
= false;
3934 bool ignore_other_columns
=
3935 ((expander
!= col
) &&
3936 (model
->IsContainer(item
)) &&
3937 (!model
->HasContainerColumns(item
)));
3939 if (event
.LeftDClick())
3941 if(hoverOverExpander
)
3943 // a double click on the expander will be converted into a "simulated" normal click
3944 simulateClick
= true;
3946 else if ( current
== m_lineLastClicked
)
3948 wxWindow
*parent
= GetParent();
3949 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3951 le
.SetColumn( col
->GetModelColumn() );
3952 le
.SetDataViewColumn( col
);
3953 le
.SetEventObject(parent
);
3954 le
.SetModel(GetModel());
3956 parent
->ProcessWindowEvent(le
);
3961 // The first click was on another item, so don't interpret this as
3962 // a double click, but as a simple click instead
3963 simulateClick
= true;
3967 if (event
.LeftUp() && !hoverOverExpander
)
3969 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3971 // select single line
3972 SelectAllRows( false );
3973 SelectRow( m_lineSelectSingleOnUp
, true );
3974 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3977 // If the user click the expander, we do not do editing even if the column
3978 // with expander are editable
3979 if (m_lastOnSame
&& !ignore_other_columns
)
3981 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3982 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3984 m_renameTimer
->Start( 100, true );
3988 m_lastOnSame
= false;
3989 m_lineSelectSingleOnUp
= (unsigned int)-1;
3991 else if(!event
.LeftUp())
3993 // This is necessary, because after a DnD operation in
3994 // from and to ourself, the up event is swallowed by the
3995 // DnD code. So on next non-up event (which means here and
3996 // now) m_lineSelectSingleOnUp should be reset.
3997 m_lineSelectSingleOnUp
= (unsigned int)-1;
4000 if (event
.RightDown())
4002 m_lineBeforeLastClicked
= m_lineLastClicked
;
4003 m_lineLastClicked
= current
;
4005 // If the item is already selected, do not update the selection.
4006 // Multi-selections should not be cleared if a selected item is clicked.
4007 if (!IsRowSelected(current
))
4009 SelectAllRows(false);
4010 const unsigned oldCurrent
= m_currentRow
;
4011 ChangeCurrentRow(current
);
4012 SelectRow(m_currentRow
,true);
4013 RefreshRow(oldCurrent
);
4014 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4017 else if (event
.MiddleDown())
4021 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4023 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4025 // hoverOverExpander being true tells us that our node must be
4026 // valid and have children.
4027 // So we don't need any extra checks.
4028 if( node
->IsOpen() )
4033 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4035 m_lineBeforeLastClicked
= m_lineLastClicked
;
4036 m_lineLastClicked
= current
;
4038 unsigned int oldCurrentRow
= m_currentRow
;
4039 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4041 bool cmdModifierDown
= event
.CmdDown();
4042 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4044 if ( IsSingleSel() || !IsRowSelected(current
) )
4046 SelectAllRows( false );
4047 ChangeCurrentRow(current
);
4048 SelectRow(m_currentRow
,true);
4049 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4051 else // multi sel & current is highlighted & no mod keys
4053 m_lineSelectSingleOnUp
= current
;
4054 ChangeCurrentRow(current
); // change focus
4057 else // multi sel & either ctrl or shift is down
4059 if (cmdModifierDown
)
4061 ChangeCurrentRow(current
);
4062 ReverseRowSelection(m_currentRow
);
4063 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4065 else if (event
.ShiftDown())
4067 ChangeCurrentRow(current
);
4069 unsigned int lineFrom
= oldCurrentRow
,
4072 if ( lineTo
< lineFrom
)
4075 lineFrom
= m_currentRow
;
4078 SelectRows(lineFrom
, lineTo
, true);
4079 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4081 else // !ctrl, !shift
4083 // test in the enclosing if should make it impossible
4084 wxFAIL_MSG( wxT("how did we get here?") );
4088 if (m_currentRow
!= oldCurrentRow
)
4089 RefreshRow( oldCurrentRow
);
4091 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4093 // Update selection here...
4095 m_currentColSetByKeyboard
= false;
4097 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4098 (current
== oldCurrentRow
)) && oldWasSelected
;
4100 // Call ActivateCell() after everything else as under GTK+
4101 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
4103 // notify cell about click
4104 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4106 wxRect
cell_rect( xpos
+ itemOffset
,
4107 GetLineStart( current
),
4108 col
->GetWidth() - itemOffset
,
4109 GetLineHeight( current
) );
4111 // Report position relative to the cell's custom area, i.e.
4112 // no the entire space as given by the control but the one
4113 // used by the renderer after calculation of alignment etc.
4115 // adjust the rectangle ourselves to account for the alignment
4116 wxRect rectItem
= cell_rect
;
4117 const int align
= cell
->GetAlignment();
4118 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4120 const wxSize size
= cell
->GetSize();
4122 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4124 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4125 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4126 else if ( align
& wxALIGN_RIGHT
)
4127 rectItem
.x
+= cell_rect
.width
- size
.x
;
4128 // else: wxALIGN_LEFT is the default
4131 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4133 if ( align
& wxALIGN_CENTER_VERTICAL
)
4134 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4135 else if ( align
& wxALIGN_BOTTOM
)
4136 rectItem
.y
+= cell_rect
.height
- size
.y
;
4137 // else: wxALIGN_TOP is the default
4141 wxMouseEvent
event2(event
);
4142 event2
.m_x
-= rectItem
.x
;
4143 event2
.m_y
-= rectItem
.y
;
4144 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4146 /* ignore ret */ cell
->WXActivateCell
4151 col
->GetModelColumn(),
4158 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4162 if (HasCurrentRow())
4168 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4172 if (HasCurrentRow())
4178 void wxDataViewMainWindow::OnColumnsCountChanged()
4180 int editableCount
= 0;
4182 const unsigned cols
= GetOwner()->GetColumnCount();
4183 for ( unsigned i
= 0; i
< cols
; i
++ )
4185 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4186 if ( c
->IsHidden() )
4188 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4192 m_useCellFocus
= (editableCount
> 1);
4197 //-----------------------------------------------------------------------------
4199 //-----------------------------------------------------------------------------
4201 WX_DEFINE_LIST(wxDataViewColumnList
)
4203 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4204 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4205 EVT_SIZE(wxDataViewCtrl::OnSize
)
4208 wxDataViewCtrl::~wxDataViewCtrl()
4211 GetModel()->RemoveNotifier( m_notifier
);
4214 m_colsBestWidths
.clear();
4217 void wxDataViewCtrl::Init()
4219 m_cols
.DeleteContents(true);
4222 // No sorting column at start
4223 m_sortingColumnIdx
= wxNOT_FOUND
;
4225 m_headerArea
= NULL
;
4227 m_colsDirty
= false;
4230 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4235 const wxValidator
& validator
,
4236 const wxString
& name
)
4238 // if ( (style & wxBORDER_MASK) == 0)
4239 // style |= wxBORDER_SUNKEN;
4243 if (!wxControl::Create( parent
, id
, pos
, size
,
4244 style
| wxScrolledWindowStyle
, validator
, name
))
4247 SetInitialSize(size
);
4250 MacSetClipChildren( true );
4253 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4255 // We use the cursor keys for moving the selection, not scrolling, so call
4256 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4257 // keyboard events forwarded to us from wxListMainWindow.
4258 DisableKeyboardScrolling();
4260 if (HasFlag(wxDV_NO_HEADER
))
4261 m_headerArea
= NULL
;
4263 m_headerArea
= new wxDataViewHeaderWindow(this);
4265 SetTargetWindow( m_clientArea
);
4267 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4269 sizer
->Add( m_headerArea
, 0, wxGROW
);
4270 sizer
->Add( m_clientArea
, 1, wxGROW
);
4276 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4278 return wxBORDER_THEME
;
4282 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4286 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4289 // we need to process arrows ourselves for scrolling
4290 if ( nMsg
== WM_GETDLGCODE
)
4292 rc
|= DLGC_WANTARROWS
;
4300 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4302 wxSize newsize
= size
;
4303 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4304 newsize
.y
-= m_headerArea
->GetSize().y
;
4309 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4311 // We need to override OnSize so that our scrolled
4312 // window a) does call Layout() to use sizers for
4313 // positioning the controls but b) does not query
4314 // the sizer for their size and use that for setting
4315 // the scrollable area as set that ourselves by
4316 // calling SetScrollbar() further down.
4322 // We must redraw the headers if their height changed. Normally this
4323 // shouldn't happen as the control shouldn't let itself be resized beneath
4324 // its minimal height but avoid the display artefacts that appear if it
4325 // does happen, e.g. because there is really not enough vertical space.
4326 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4327 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4329 m_headerArea
->Refresh();
4333 void wxDataViewCtrl::SetFocus()
4336 m_clientArea
->SetFocus();
4339 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4341 if (!wxDataViewCtrlBase::AssociateModel( model
))
4344 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4346 model
->AddNotifier( m_notifier
);
4348 m_clientArea
->DestroyTree();
4350 m_clientArea
->BuildTree(model
);
4352 m_clientArea
->UpdateDisplay();
4357 #if wxUSE_DRAG_AND_DROP
4359 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4361 return m_clientArea
->EnableDragSource( format
);
4364 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4366 return m_clientArea
->EnableDropTarget( format
);
4369 #endif // wxUSE_DRAG_AND_DROP
4371 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4373 if (!wxDataViewCtrlBase::AppendColumn(col
))
4376 m_cols
.Append( col
);
4377 m_colsBestWidths
.push_back(0);
4378 OnColumnsCountChanged();
4382 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4384 if (!wxDataViewCtrlBase::PrependColumn(col
))
4387 m_cols
.Insert( col
);
4388 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4389 OnColumnsCountChanged();
4393 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4395 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4398 m_cols
.Insert( pos
, col
);
4399 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4400 OnColumnsCountChanged();
4404 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4407 m_headerArea
->UpdateColumn(idx
);
4409 m_clientArea
->UpdateDisplay();
4412 void wxDataViewCtrl::OnColumnsCountChanged()
4415 m_headerArea
->SetColumnCount(GetColumnCount());
4417 m_clientArea
->OnColumnsCountChanged();
4420 void wxDataViewCtrl::DoSetExpanderColumn()
4422 m_clientArea
->UpdateDisplay();
4425 void wxDataViewCtrl::DoSetIndent()
4427 m_clientArea
->UpdateDisplay();
4430 unsigned int wxDataViewCtrl::GetColumnCount() const
4432 return m_cols
.GetCount();
4435 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4437 if ( !m_clientArea
)
4440 m_clientArea
->SetRowHeight(lineHeight
);
4445 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4450 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4452 // columns can't be reordered if there is no header window which allows
4454 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4457 return GetColumn(idx
);
4460 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4462 const unsigned count
= m_cols
.size();
4463 for ( unsigned n
= 0; n
< count
; n
++ )
4465 if ( m_cols
[n
] == column
)
4472 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4474 if ( m_colsBestWidths
[idx
] != 0 )
4475 return m_colsBestWidths
[idx
];
4477 const int count
= m_clientArea
->GetRowCount();
4478 wxDataViewColumn
*column
= GetColumn(idx
);
4479 wxDataViewRenderer
*renderer
=
4480 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4482 class MaxWidthCalculator
4485 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4486 wxDataViewMainWindow
*clientArea
,
4487 wxDataViewRenderer
*renderer
,
4488 const wxDataViewModel
*model
,
4493 m_clientArea(clientArea
),
4494 m_renderer(renderer
),
4497 m_expanderSize(expanderSize
)
4501 !clientArea
->IsList() &&
4503 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4506 void UpdateWithWidth(int width
)
4508 m_width
= wxMax(m_width
, width
);
4511 void UpdateWithRow(int row
)
4514 wxDataViewItem item
;
4516 if ( m_isExpanderCol
)
4518 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4519 item
= node
->GetItem();
4520 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4524 item
= m_clientArea
->GetItemByRow(row
);
4527 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4528 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4531 int GetMaxWidth() const { return m_width
; }
4535 const wxDataViewCtrl
*m_dvc
;
4536 wxDataViewMainWindow
*m_clientArea
;
4537 wxDataViewRenderer
*m_renderer
;
4538 const wxDataViewModel
*m_model
;
4540 bool m_isExpanderCol
;
4544 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4545 GetModel(), column
->GetModelColumn(),
4546 m_clientArea
->GetRowHeight());
4550 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4551 // Labels on native MSW header are indented on both sides
4553 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4554 calculator
.UpdateWithWidth(header_width
);
4557 // The code below deserves some explanation. For very large controls, we
4558 // simply can't afford to calculate sizes for all items, it takes too
4559 // long. So the best we can do is to check the first and the last N/2
4560 // items in the control for some sufficiently large N and calculate best
4561 // sizes from that. That can result in the calculated best width being too
4562 // small for some outliers, but it's better to get slightly imperfect
4563 // result than to wait several seconds after every update. To avoid highly
4564 // visible miscalculations, we also include all currently visible items
4565 // no matter what. Finally, the value of N is determined dynamically by
4566 // measuring how much time we spent on the determining item widths so far.
4569 int top_part_end
= count
;
4570 static const long CALC_TIMEOUT
= 20/*ms*/;
4571 // don't call wxStopWatch::Time() too often
4572 static const unsigned CALC_CHECK_FREQ
= 100;
4575 // use some hard-coded limit, that's the best we can do without timer
4576 int top_part_end
= wxMin(500, count
);
4577 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4581 for ( row
= 0; row
< top_part_end
; row
++ )
4584 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4585 timer
.Time() > CALC_TIMEOUT
)
4587 #endif // wxUSE_STOPWATCH
4588 calculator
.UpdateWithRow(row
);
4591 // row is the first unmeasured item now; that's our value of N/2
4597 // add bottom N/2 items now:
4598 const int bottom_part_start
= wxMax(row
, count
- row
);
4599 for ( row
= bottom_part_start
; row
< count
; row
++ )
4601 calculator
.UpdateWithRow(row
);
4604 // finally, include currently visible items in the calculation:
4605 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4606 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4607 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4609 first_visible
= wxMax(first_visible
, top_part_end
);
4610 last_visible
= wxMin(bottom_part_start
, last_visible
);
4612 for ( row
= first_visible
; row
< last_visible
; row
++ )
4614 calculator
.UpdateWithRow(row
);
4617 wxLogTrace("dataview",
4618 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4620 count
- bottom_part_start
,
4621 wxMax(0, last_visible
- first_visible
),
4625 int max_width
= calculator
.GetMaxWidth();
4626 if ( max_width
> 0 )
4627 max_width
+= 2 * PADDING_RIGHTLEFT
;
4629 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4633 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4634 unsigned int WXUNUSED(new_pos
))
4636 // do _not_ reorder m_cols elements here, they should always be in the
4637 // order in which columns were added, we only display the columns in
4639 m_clientArea
->UpdateDisplay();
4642 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4644 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4648 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4650 OnColumnsCountChanged();
4655 bool wxDataViewCtrl::ClearColumns()
4657 SetExpanderColumn(NULL
);
4659 m_colsBestWidths
.clear();
4660 OnColumnsCountChanged();
4664 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4666 m_colsBestWidths
[idx
] = 0;
4670 void wxDataViewCtrl::InvalidateColBestWidths()
4672 m_colsBestWidths
.clear();
4673 m_colsBestWidths
.resize(m_cols
.size());
4677 void wxDataViewCtrl::UpdateColWidths()
4679 if ( !m_headerArea
)
4682 const unsigned len
= m_colsBestWidths
.size();
4683 for ( unsigned i
= 0; i
< len
; i
++ )
4685 if ( m_colsBestWidths
[i
] == 0 )
4686 m_headerArea
->UpdateColumn(i
);
4690 void wxDataViewCtrl::OnInternalIdle()
4692 wxDataViewCtrlBase::OnInternalIdle();
4696 m_colsDirty
= false;
4701 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4704 unsigned int len
= GetColumnCount();
4705 for ( unsigned int i
= 0; i
< len
; i
++ )
4707 wxDataViewColumn
* col
= GetColumnAt(i
);
4714 // This returns the position in pixels which is not what we want.
4717 unsigned int len
= GetColumnCount();
4718 for ( unsigned int i
= 0; i
< len
; i
++ )
4720 wxDataViewColumn
* col
= GetColumnAt(i
);
4721 if (col
->IsHidden())
4723 ret
+= col
->GetWidth();
4726 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4734 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4736 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4737 : GetColumn(m_sortingColumnIdx
);
4740 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4742 return GetItemByRow(m_clientArea
->GetCurrentRow());
4745 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4747 const int row
= m_clientArea
->GetRowByItem(item
);
4749 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4750 if ( static_cast<unsigned>(row
) != oldCurrent
)
4752 m_clientArea
->ChangeCurrentRow(row
);
4753 m_clientArea
->RefreshRow(oldCurrent
);
4754 m_clientArea
->RefreshRow(row
);
4758 int wxDataViewCtrl::GetSelectedItemsCount() const
4760 return m_clientArea
->GetSelections().size();
4763 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4766 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4768 const size_t len
= selections
.size();
4769 for ( size_t i
= 0; i
< len
; i
++ )
4771 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4778 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4785 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4787 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4789 wxDataViewItem last_parent
;
4791 int len
= sel
.GetCount();
4792 for( int i
= 0; i
< len
; i
++ )
4794 wxDataViewItem item
= sel
[i
];
4795 wxDataViewItem parent
= GetModel()->GetParent( item
);
4798 if (parent
!= last_parent
)
4799 ExpandAncestors(item
);
4802 last_parent
= parent
;
4803 int row
= m_clientArea
->GetRowByItem( item
);
4805 selection
.Add( static_cast<unsigned int>(row
) );
4808 m_clientArea
->SetSelections( selection
);
4811 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4813 ExpandAncestors( item
);
4815 int row
= m_clientArea
->GetRowByItem( item
);
4818 // Unselect all rows before select another in the single select mode
4819 if (m_clientArea
->IsSingleSel())
4820 m_clientArea
->SelectAllRows(false);
4822 m_clientArea
->SelectRow(row
, true);
4824 // Also set focus to the selected item
4825 m_clientArea
->ChangeCurrentRow( row
);
4829 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4831 int row
= m_clientArea
->GetRowByItem( item
);
4833 m_clientArea
->SelectRow(row
, false);
4836 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4838 int row
= m_clientArea
->GetRowByItem( item
);
4841 return m_clientArea
->IsRowSelected(row
);
4846 void wxDataViewCtrl::SelectAll()
4848 m_clientArea
->SelectAllRows(true);
4851 void wxDataViewCtrl::UnselectAll()
4853 m_clientArea
->SelectAllRows(false);
4856 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4860 if( row
> (int) m_clientArea
->GetRowCount() )
4861 row
= m_clientArea
->GetRowCount();
4863 int first
= m_clientArea
->GetFirstVisibleRow();
4864 int last
= m_clientArea
->GetLastVisibleRow();
4866 m_clientArea
->ScrollTo( row
, column
);
4867 else if( row
> last
)
4868 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4870 m_clientArea
->ScrollTo( first
, column
);
4873 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4875 ExpandAncestors( item
);
4877 m_clientArea
->RecalculateDisplay();
4879 int row
= m_clientArea
->GetRowByItem(item
);
4882 if( column
== NULL
)
4883 EnsureVisible(row
, -1);
4885 EnsureVisible( row
, GetColumnIndex(column
) );
4890 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4891 wxDataViewColumn
* &column
) const
4893 m_clientArea
->HitTest(point
, item
, column
);
4896 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4897 const wxDataViewColumn
* column
) const
4899 return m_clientArea
->GetItemRect(item
, column
);
4902 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4904 return m_clientArea
->GetItemByRow( row
);
4907 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4909 return m_clientArea
->GetRowByItem( item
);
4912 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4914 ExpandAncestors( item
);
4916 int row
= m_clientArea
->GetRowByItem( item
);
4918 m_clientArea
->Expand(row
);
4921 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4923 int row
= m_clientArea
->GetRowByItem( item
);
4925 m_clientArea
->Collapse(row
);
4928 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4930 int row
= m_clientArea
->GetRowByItem( item
);
4932 return m_clientArea
->IsExpanded(row
);
4936 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4938 wxDataViewColumn
* col
= GetColumn( column
);
4942 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4943 if (renderer
->GetMode() != wxDATAVIEW_CELL_EDITABLE
)
4946 const wxRect itemRect
= GetItemRect(item
, col
);
4947 renderer
->StartEditing(item
, itemRect
);
4950 #endif // !wxUSE_GENERICDATAVIEWCTRL
4952 #endif // wxUSE_DATAVIEWCTRL