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"
51 #include "wx/weakref.h"
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 class wxDataViewColumn
;
58 class wxDataViewHeaderWindow
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static const int SCROLL_UNIT_X
= 15;
67 // the cell padding on the left/right
68 static const int PADDING_RIGHTLEFT
= 3;
70 // the expander space margin
71 static const int EXPANDER_MARGIN
= 4;
74 static const int EXPANDER_OFFSET
= 4;
76 static const int EXPANDER_OFFSET
= 1;
79 // Below is the compare stuff.
80 // For the generic implementation, both the leaf nodes and the nodes are sorted for
81 // fast search when needed
82 static wxDataViewModel
* g_model
;
83 static int g_column
= -2;
84 static bool g_asending
= true;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
93 // Return the expander column or, if it is not set, the first column and also
94 // set it as the expander one for the future.
95 wxDataViewColumn
* GetExpanderColumnOrFirstOne(wxDataViewCtrl
* dataview
)
97 wxDataViewColumn
* expander
= dataview
->GetExpanderColumn();
100 // TODO-RTL: last column for RTL support
101 expander
= dataview
->GetColumnAt( 0 );
102 dataview
->SetExpanderColumn(expander
);
108 } // anonymous namespace
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
121 m_sortAscending
= true;
124 int wxDataViewColumn::GetWidth() const
128 case wxCOL_WIDTH_DEFAULT
:
129 return wxDVC_DEFAULT_WIDTH
;
131 case wxCOL_WIDTH_AUTOSIZE
:
132 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
133 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
140 void wxDataViewColumn::UpdateDisplay()
144 int idx
= m_owner
->GetColumnIndex( this );
145 m_owner
->OnColumnChange( idx
);
149 void wxDataViewColumn::SetSortOrder(bool ascending
)
154 // First unset the old sort column if any.
155 int oldSortKey
= m_owner
->GetSortingColumnIndex();
156 if ( oldSortKey
!= wxNOT_FOUND
)
158 m_owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
161 // Now set this one as the new sort column.
162 const int idx
= m_owner
->GetColumnIndex(this);
163 m_owner
->SetSortingColumnIndex(idx
);
166 m_sortAscending
= ascending
;
168 // Call this directly instead of using UpdateDisplay() as we already have
169 // the column index, no need to look it up again.
170 m_owner
->OnColumnChange(idx
);
173 //-----------------------------------------------------------------------------
174 // wxDataViewHeaderWindow
175 //-----------------------------------------------------------------------------
177 class wxDataViewHeaderWindow
: public wxHeaderCtrl
180 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
181 : wxHeaderCtrl(parent
)
185 wxDataViewCtrl
*GetOwner() const
186 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
189 // implement/override wxHeaderCtrl functions by forwarding them to the main
191 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
193 return *(GetOwner()->GetColumn(idx
));
196 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
198 wxDataViewCtrl
* const owner
= GetOwner();
200 int widthContents
= owner
->GetBestColumnWidth(idx
);
201 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
202 owner
->OnColumnChange(idx
);
208 bool SendEvent(wxEventType type
, unsigned int n
)
210 wxDataViewCtrl
* const owner
= GetOwner();
211 wxDataViewEvent
event(type
, owner
->GetId());
213 event
.SetEventObject(owner
);
215 event
.SetDataViewColumn(owner
->GetColumn(n
));
216 event
.SetModel(owner
->GetModel());
218 // for events created by wxDataViewHeaderWindow the
219 // row / value fields are not valid
220 return owner
->ProcessWindowEvent(event
);
223 void OnClick(wxHeaderCtrlEvent
& event
)
225 const unsigned idx
= event
.GetColumn();
227 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
230 // default handling for the column click is to sort by this column or
231 // toggle its sort order
232 wxDataViewCtrl
* const owner
= GetOwner();
233 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
234 if ( !col
->IsSortable() )
236 // no default handling for non-sortable columns
241 if ( col
->IsSortKey() )
243 // already using this column for sorting, just change the order
244 col
->ToggleSortOrder();
246 else // not using this column for sorting yet
248 col
->SetSortOrder(true);
251 wxDataViewModel
* const model
= owner
->GetModel();
255 owner
->OnColumnChange(idx
);
257 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
260 void OnRClick(wxHeaderCtrlEvent
& event
)
262 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
267 void OnResize(wxHeaderCtrlEvent
& event
)
269 wxDataViewCtrl
* const owner
= GetOwner();
271 const unsigned col
= event
.GetColumn();
272 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
273 GetOwner()->OnColumnChange(col
);
276 void OnEndReorder(wxHeaderCtrlEvent
& event
)
278 wxDataViewCtrl
* const owner
= GetOwner();
279 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
280 event
.GetNewOrder());
283 DECLARE_EVENT_TABLE()
284 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
287 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
288 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
289 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
291 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
292 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
294 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
297 //-----------------------------------------------------------------------------
298 // wxDataViewRenameTimer
299 //-----------------------------------------------------------------------------
301 class wxDataViewRenameTimer
: public wxTimer
304 wxDataViewMainWindow
*m_owner
;
307 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
311 //-----------------------------------------------------------------------------
312 // wxDataViewTreeNode
313 //-----------------------------------------------------------------------------
315 class wxDataViewTreeNode
;
316 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
318 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
319 wxDataViewTreeNode
** node2
);
321 class wxDataViewTreeNode
324 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
331 ~wxDataViewTreeNode()
335 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
336 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
347 static wxDataViewTreeNode
* CreateRootNode()
349 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
350 n
->SetHasChildren(true);
351 n
->m_branchData
->open
= true;
355 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
357 const wxDataViewTreeNodes
& GetChildNodes() const
359 wxASSERT( m_branchData
!= NULL
);
360 return m_branchData
->children
;
363 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
366 m_branchData
= new BranchNodeData
;
368 m_branchData
->children
.Insert(node
, index
);
370 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
372 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
375 void RemoveChild(wxDataViewTreeNode
*node
)
377 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
378 m_branchData
->children
.Remove(node
);
381 // returns position of child node for given item in children list or wxNOT_FOUND
382 int FindChildByItem(const wxDataViewItem
& item
) const
387 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
388 const int len
= nodes
.size();
389 for ( int i
= 0; i
< len
; i
++ )
391 if ( nodes
[i
]->m_item
== item
)
397 const wxDataViewItem
& GetItem() const { return m_item
; }
398 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
400 int GetIndentLevel() const
403 const wxDataViewTreeNode
* node
= this;
404 while( node
->GetParent()->GetParent() != NULL
)
406 node
= node
->GetParent();
414 return m_branchData
&& m_branchData
->open
;
419 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
423 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
424 const int len
= nodes
.GetCount();
425 for ( int i
= 0;i
< len
; i
++)
426 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
428 if (m_branchData
->open
)
430 ChangeSubTreeCount(-sum
);
431 m_branchData
->open
= !m_branchData
->open
;
435 m_branchData
->open
= !m_branchData
->open
;
436 ChangeSubTreeCount(+sum
);
440 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
441 // even if GetChildNodes() is empty; see below.
442 bool HasChildren() const
444 return m_branchData
!= NULL
;
447 void SetHasChildren(bool has
)
451 wxDELETE(m_branchData
);
453 else if ( m_branchData
== NULL
)
455 m_branchData
= new BranchNodeData
;
459 int GetSubTreeCount() const
461 return m_branchData
? m_branchData
->subTreeCount
: 0;
464 void ChangeSubTreeCount( int num
)
466 wxASSERT( m_branchData
!= NULL
);
468 if( !m_branchData
->open
)
471 m_branchData
->subTreeCount
+= num
;
472 wxASSERT( m_branchData
->subTreeCount
>= 0 );
475 m_parent
->ChangeSubTreeCount(num
);
485 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
487 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
488 int len
= nodes
.GetCount();
489 for (int i
= 0; i
< len
; i
++)
491 if ( nodes
[i
]->HasChildren() )
499 wxDataViewTreeNode
*m_parent
;
501 // Corresponding model item.
502 wxDataViewItem m_item
;
504 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
505 // separate struct in order to conserve memory.
506 struct BranchNodeData
514 // Child nodes. Note that this may be empty even if m_hasChildren in
515 // case this branch of the tree wasn't expanded and realized yet.
516 wxDataViewTreeNodes children
;
518 // Is the branch node currently open (expanded)?
521 // Total count of expanded (i.e. visible with the help of some
522 // scrolling) items in the subtree, but excluding this node. I.e. it is
523 // 0 for leaves and is the number of rows the subtree occupies for
528 BranchNodeData
*m_branchData
;
532 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
533 wxDataViewTreeNode
** node2
)
535 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
539 //-----------------------------------------------------------------------------
540 // wxDataViewMainWindow
541 //-----------------------------------------------------------------------------
543 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
545 class wxDataViewMainWindow
: public wxWindow
548 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
550 const wxPoint
&pos
= wxDefaultPosition
,
551 const wxSize
&size
= wxDefaultSize
,
552 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
553 virtual ~wxDataViewMainWindow();
555 bool IsList() const { return GetModel()->IsListModel(); }
556 bool IsVirtualList() const { return m_root
== NULL
; }
558 // notifications from wxDataViewModel
559 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
560 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
561 bool ItemChanged( const wxDataViewItem
&item
);
562 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
566 if (!IsVirtualList())
576 g_model
= GetModel();
577 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
580 if (g_model
->HasDefaultCompare())
588 g_column
= col
->GetModelColumn();
589 g_asending
= col
->IsSortOrderAscending();
592 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
593 wxDataViewCtrl
*GetOwner() { return m_owner
; }
594 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
596 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
597 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
599 #if wxUSE_DRAG_AND_DROP
600 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
601 #endif // wxUSE_DRAG_AND_DROP
602 void OnPaint( wxPaintEvent
&event
);
603 void OnCharHook( wxKeyEvent
&event
);
604 void OnChar( wxKeyEvent
&event
);
605 void OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
);
608 void OnMouse( wxMouseEvent
&event
);
609 void OnSetFocus( wxFocusEvent
&event
);
610 void OnKillFocus( wxFocusEvent
&event
);
612 void UpdateDisplay();
613 void RecalculateDisplay();
614 void OnInternalIdle();
616 void OnRenameTimer();
618 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
619 void ScrollTo( int rows
, int column
);
621 unsigned GetCurrentRow() const { return m_currentRow
; }
622 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
623 void ChangeCurrentRow( unsigned int row
);
624 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
626 wxDataViewColumn
*GetCurrentColumn() const { return m_currentCol
; }
627 void ClearCurrentColumn() { m_currentCol
= NULL
; }
629 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
630 bool IsEmpty() { return GetRowCount() == 0; }
632 int GetCountPerPage() const;
633 int GetEndOfLastCol() const;
634 unsigned int GetFirstVisibleRow() const;
636 // I change this method to un const because in the tree view,
637 // the displaying number of the tree are changing along with the
638 // expanding/collapsing of the tree nodes
639 unsigned int GetLastVisibleRow();
640 unsigned int GetRowCount() const;
642 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
643 void SetSelections( const wxDataViewSelection
& sel
)
644 { m_selection
= sel
; UpdateDisplay(); }
645 void Select( const wxArrayInt
& aSelections
);
646 void SelectAllRows( bool on
);
647 void SelectRow( unsigned int row
, bool on
);
648 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
649 void ReverseRowSelection( unsigned int row
);
650 bool IsRowSelected( unsigned int row
);
651 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
653 void RefreshRow( unsigned int row
);
654 void RefreshRows( unsigned int from
, unsigned int to
);
655 void RefreshRowsAfter( unsigned int firstRow
);
657 // returns the colour to be used for drawing the rules
658 wxColour
GetRuleColour() const
660 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
663 wxRect
GetLineRect( unsigned int row
) const;
665 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
666 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
667 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
669 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
670 int GetRowHeight() const { return m_lineHeight
; }
672 // Some useful functions for row and item mapping
673 wxDataViewItem
GetItemByRow( unsigned int row
) const;
674 int GetRowByItem( const wxDataViewItem
& item
) const;
676 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
677 // We did not need this temporarily
678 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
680 // Methods for building the mapping tree
681 void BuildTree( wxDataViewModel
* model
);
683 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
684 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
686 void Expand( unsigned int row
);
687 void Collapse( unsigned int row
);
688 bool IsExpanded( unsigned int row
) const;
689 bool HasChildren( unsigned int row
) const;
691 #if wxUSE_DRAG_AND_DROP
692 bool EnableDragSource( const wxDataFormat
&format
);
693 bool EnableDropTarget( const wxDataFormat
&format
);
695 void RemoveDropHint();
696 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
697 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
698 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
700 #endif // wxUSE_DRAG_AND_DROP
702 void OnColumnsCountChanged();
704 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
705 // specified item in the given column.
706 void StartEditing(const wxDataViewItem
& item
, const wxDataViewColumn
* col
);
709 int RecalculateCount() const;
711 // Return false only if the event was vetoed by its handler.
712 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
714 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
716 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
718 bool IsCellEditableInMode(const wxDataViewItem
& item
, const wxDataViewColumn
*col
, wxDataViewCellMode mode
) const;
720 void DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
);
723 wxDataViewCtrl
*m_owner
;
727 wxDataViewColumn
*m_currentCol
;
728 unsigned int m_currentRow
;
729 wxDataViewSelection m_selection
;
731 wxDataViewRenameTimer
*m_renameTimer
;
736 bool m_currentColSetByKeyboard
;
738 #if wxUSE_DRAG_AND_DROP
743 wxDataFormat m_dragFormat
;
746 wxDataFormat m_dropFormat
;
748 unsigned int m_dropHintLine
;
749 #endif // wxUSE_DRAG_AND_DROP
751 // for double click logic
752 unsigned int m_lineLastClicked
,
753 m_lineBeforeLastClicked
,
754 m_lineSelectSingleOnUp
;
756 // the pen used to draw horiz/vertical rules
759 // the pen used to draw the expander and the lines
762 // This is the tree structure of the model
763 wxDataViewTreeNode
* m_root
;
766 // This is the tree node under the cursor
767 wxDataViewTreeNode
* m_underMouse
;
769 // The control used for editing or NULL.
770 wxWeakRef
<wxWindow
> m_editorCtrl
;
772 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
773 wxDataViewRenderer
* m_editorRenderer
;
776 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
777 DECLARE_EVENT_TABLE()
780 // ---------------------------------------------------------
781 // wxGenericDataViewModelNotifier
782 // ---------------------------------------------------------
784 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
787 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
788 { m_mainWindow
= mainWindow
; }
790 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
791 { return m_mainWindow
->ItemAdded( parent
, item
); }
792 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
793 { return m_mainWindow
->ItemDeleted( parent
, item
); }
794 virtual bool ItemChanged( const wxDataViewItem
& item
)
795 { return m_mainWindow
->ItemChanged(item
); }
796 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
797 { return m_mainWindow
->ValueChanged( item
, col
); }
798 virtual bool Cleared()
799 { return m_mainWindow
->Cleared(); }
800 virtual void Resort()
801 { m_mainWindow
->Resort(); }
803 wxDataViewMainWindow
*m_mainWindow
;
806 // ---------------------------------------------------------
807 // wxDataViewRenderer
808 // ---------------------------------------------------------
810 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
812 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
813 wxDataViewCellMode mode
,
815 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
819 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
823 wxDataViewRenderer::~wxDataViewRenderer()
828 wxDC
*wxDataViewRenderer::GetDC()
832 if (GetOwner() == NULL
)
834 if (GetOwner()->GetOwner() == NULL
)
836 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
842 void wxDataViewRenderer::SetAlignment( int align
)
847 int wxDataViewRenderer::GetAlignment() const
852 // ---------------------------------------------------------
853 // wxDataViewCustomRenderer
854 // ---------------------------------------------------------
856 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
858 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
859 wxDataViewCellMode mode
, int align
) :
860 wxDataViewRenderer( varianttype
, mode
, align
)
864 // ---------------------------------------------------------
865 // wxDataViewTextRenderer
866 // ---------------------------------------------------------
868 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
870 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
871 wxDataViewCellMode mode
, int align
) :
872 wxDataViewRenderer( varianttype
, mode
, align
)
876 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
878 m_text
= value
.GetString();
883 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
888 bool wxDataViewTextRenderer::HasEditorCtrl() const
893 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
894 wxRect labelRect
, const wxVariant
&value
)
896 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
897 wxPoint(labelRect
.x
,labelRect
.y
),
898 wxSize(labelRect
.width
,labelRect
.height
),
899 wxTE_PROCESS_ENTER
);
901 // select the text in the control an place the cursor at the end
902 ctrl
->SetInsertionPointEnd();
908 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
910 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
911 value
= text
->GetValue();
915 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
917 RenderText(m_text
, 0, rect
, dc
, state
);
921 wxSize
wxDataViewTextRenderer::GetSize() const
924 return GetTextExtent(m_text
);
926 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
929 // ---------------------------------------------------------
930 // wxDataViewBitmapRenderer
931 // ---------------------------------------------------------
933 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
935 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
936 wxDataViewCellMode mode
, int align
) :
937 wxDataViewRenderer( varianttype
, mode
, align
)
941 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
943 if (value
.GetType() == wxT("wxBitmap"))
945 if (value
.GetType() == wxT("wxIcon"))
951 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
956 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
959 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
960 else if (m_icon
.IsOk())
961 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
966 wxSize
wxDataViewBitmapRenderer::GetSize() const
969 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
970 else if (m_icon
.IsOk())
971 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
973 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
976 // ---------------------------------------------------------
977 // wxDataViewToggleRenderer
978 // ---------------------------------------------------------
980 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
982 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
983 wxDataViewCellMode mode
, int align
) :
984 wxDataViewRenderer( varianttype
, mode
, align
)
989 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
991 m_toggle
= value
.GetBool();
996 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
1001 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
1005 flags
|= wxCONTROL_CHECKED
;
1006 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
1007 GetEnabled() == false)
1008 flags
|= wxCONTROL_DISABLED
;
1010 // check boxes we draw must always have the same, standard size (if it's
1011 // bigger than the cell size the checkbox will be truncated because the
1012 // caller had set the clipping rectangle to prevent us from drawing outside
1014 cell
.SetSize(GetSize());
1016 wxRendererNative::Get().DrawCheckBox(
1017 GetOwner()->GetOwner(),
1025 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1026 wxDataViewModel
*model
,
1027 const wxDataViewItem
& item
,
1029 const wxMouseEvent
*mouseEvent
)
1033 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1034 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1038 model
->ChangeValue(!m_toggle
, item
, col
);
1042 wxSize
wxDataViewToggleRenderer::GetSize() const
1044 // the window parameter is not used by GetCheckBoxSize() so it's
1045 // safe to pass NULL
1046 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1049 // ---------------------------------------------------------
1050 // wxDataViewProgressRenderer
1051 // ---------------------------------------------------------
1053 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1055 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1056 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1057 wxDataViewRenderer( varianttype
, mode
, align
)
1063 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1065 m_value
= (long) value
;
1067 if (m_value
< 0) m_value
= 0;
1068 if (m_value
> 100) m_value
= 100;
1073 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1075 value
= (long) m_value
;
1080 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1082 // deflate the rect to leave a small border between bars in adjacent rows
1083 wxRect bar
= rect
.Deflate(0, 1);
1085 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1086 dc
->SetPen( *wxBLACK_PEN
);
1087 dc
->DrawRectangle( bar
);
1089 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1090 dc
->SetPen( *wxTRANSPARENT_PEN
);
1092 const wxDataViewItemAttr
& attr
= GetAttr();
1093 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1095 dc
->DrawRectangle( bar
);
1100 wxSize
wxDataViewProgressRenderer::GetSize() const
1102 return wxSize(40,12);
1105 // ---------------------------------------------------------
1106 // wxDataViewIconTextRenderer
1107 // ---------------------------------------------------------
1109 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1111 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1112 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1113 wxDataViewRenderer( varianttype
, mode
, align
)
1116 SetAlignment(align
);
1119 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1125 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1130 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1134 const wxIcon
& icon
= m_value
.GetIcon();
1137 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1138 xoffset
= icon
.GetWidth()+4;
1141 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1146 wxSize
wxDataViewIconTextRenderer::GetSize() const
1148 if (!m_value
.GetText().empty())
1150 wxSize size
= GetTextExtent(m_value
.GetText());
1152 if (m_value
.GetIcon().IsOk())
1153 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1156 return wxSize(80,20);
1159 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1161 wxDataViewIconText iconText
;
1164 wxString text
= iconText
.GetText();
1166 // adjust the label rect to take the width of the icon into account
1167 if (iconText
.GetIcon().IsOk())
1169 int w
= iconText
.GetIcon().GetWidth() + 4;
1171 labelRect
.width
-= w
;
1174 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1175 wxPoint(labelRect
.x
,labelRect
.y
),
1176 wxSize(labelRect
.width
,labelRect
.height
),
1177 wxTE_PROCESS_ENTER
);
1179 // select the text in the control an place the cursor at the end
1180 ctrl
->SetInsertionPointEnd();
1186 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1188 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1190 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1195 //-----------------------------------------------------------------------------
1196 // wxDataViewDropTarget
1197 //-----------------------------------------------------------------------------
1199 #if wxUSE_DRAG_AND_DROP
1201 class wxBitmapCanvas
: public wxWindow
1204 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1205 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1208 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1211 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1214 dc
.DrawBitmap( m_bitmap
, 0, 0);
1220 class wxDataViewDropSource
: public wxDropSource
1223 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1231 ~wxDataViewDropSource()
1236 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1238 wxPoint pos
= wxGetMousePosition();
1242 int liney
= m_win
->GetLineStart( m_row
);
1244 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1245 m_win
->ClientToScreen( &linex
, &liney
);
1246 m_dist_x
= pos
.x
- linex
;
1247 m_dist_y
= pos
.y
- liney
;
1250 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1252 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1253 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1255 wxFRAME_TOOL_WINDOW
|
1256 wxFRAME_FLOAT_ON_PARENT
|
1257 wxFRAME_NO_TASKBAR
|
1259 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1264 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1265 m_hint
->SetTransparent( 128 );
1271 wxDataViewMainWindow
*m_win
;
1274 int m_dist_x
,m_dist_y
;
1278 class wxDataViewDropTarget
: public wxDropTarget
1281 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1287 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1289 wxDataFormat format
= GetMatchingPair();
1290 if (format
== wxDF_INVALID
)
1292 return m_win
->OnDragOver( format
, x
, y
, def
);
1295 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1297 wxDataFormat format
= GetMatchingPair();
1298 if (format
== wxDF_INVALID
)
1300 return m_win
->OnDrop( format
, x
, y
);
1303 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1305 wxDataFormat format
= GetMatchingPair();
1306 if (format
== wxDF_INVALID
)
1310 return m_win
->OnData( format
, x
, y
, def
);
1313 virtual void OnLeave()
1314 { m_win
->OnLeave(); }
1316 wxDataViewMainWindow
*m_win
;
1319 #endif // wxUSE_DRAG_AND_DROP
1321 //-----------------------------------------------------------------------------
1322 // wxDataViewRenameTimer
1323 //-----------------------------------------------------------------------------
1325 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1330 void wxDataViewRenameTimer::Notify()
1332 m_owner
->OnRenameTimer();
1335 //-----------------------------------------------------------------------------
1336 // wxDataViewMainWindow
1337 //-----------------------------------------------------------------------------
1339 // The tree building helper, declared firstly
1340 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1341 wxDataViewTreeNode
* node
);
1343 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1345 if (row1
> row2
) return 1;
1346 if (row1
== row2
) return 0;
1350 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1352 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1353 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1354 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1355 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1356 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1357 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook
)
1358 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1361 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1362 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1363 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1364 m_selection( wxDataViewSelectionCmp
)
1369 m_editorRenderer
= NULL
;
1371 m_lastOnSame
= false;
1372 m_renameTimer
= new wxDataViewRenameTimer( this );
1374 // TODO: user better initial values/nothing selected
1375 m_currentCol
= NULL
;
1376 m_currentColSetByKeyboard
= false;
1377 m_useCellFocus
= false;
1381 // We would like to use the same line height that Explorer uses. This is
1382 // different from standard ListView control since Vista.
1383 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
1384 m_lineHeight
= wxMax(16, GetCharHeight()) + 6; // 16 = mini icon height
1387 m_lineHeight
= wxMax(16, GetCharHeight()) + 1; // 16 = mini icon height
1389 #if wxUSE_DRAG_AND_DROP
1391 m_dragStart
= wxPoint(0,0);
1393 m_dragEnabled
= false;
1394 m_dropEnabled
= false;
1396 m_dropHintLine
= (unsigned int) -1;
1397 #endif // wxUSE_DRAG_AND_DROP
1399 m_lineLastClicked
= (unsigned int) -1;
1400 m_lineBeforeLastClicked
= (unsigned int) -1;
1401 m_lineSelectSingleOnUp
= (unsigned int) -1;
1405 SetBackgroundColour( *wxWHITE
);
1407 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1409 m_penRule
= wxPen(GetRuleColour());
1411 // compose a pen whichcan draw black lines
1412 // TODO: maybe there is something system colour to use
1413 m_penExpander
= wxPen(wxColour(0,0,0));
1415 m_root
= wxDataViewTreeNode::CreateRootNode();
1417 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1419 m_underMouse
= NULL
;
1423 wxDataViewMainWindow::~wxDataViewMainWindow()
1426 delete m_renameTimer
;
1430 #if wxUSE_DRAG_AND_DROP
1431 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1433 m_dragFormat
= format
;
1434 m_dragEnabled
= format
!= wxDF_INVALID
;
1439 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1441 m_dropFormat
= format
;
1442 m_dropEnabled
= format
!= wxDF_INVALID
;
1445 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1450 void wxDataViewMainWindow::RemoveDropHint()
1455 RefreshRow( m_dropHintLine
);
1456 m_dropHintLine
= (unsigned int) -1;
1460 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1461 wxCoord y
, wxDragResult def
)
1465 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1466 unsigned int row
= GetLineAt( yy
);
1468 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1474 wxDataViewItem item
= GetItemByRow( row
);
1476 wxDataViewModel
*model
= GetModel();
1478 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1479 event
.SetEventObject( m_owner
);
1480 event
.SetItem( item
);
1481 event
.SetModel( model
);
1482 event
.SetDataFormat( format
);
1483 if (!m_owner
->HandleWindowEvent( event
))
1489 if (!event
.IsAllowed())
1496 if (m_dropHint
&& (row
!= m_dropHintLine
))
1497 RefreshRow( m_dropHintLine
);
1499 m_dropHintLine
= row
;
1505 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1511 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1512 unsigned int row
= GetLineAt( yy
);
1514 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1517 wxDataViewItem item
= GetItemByRow( row
);
1519 wxDataViewModel
*model
= GetModel();
1521 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1522 event
.SetEventObject( m_owner
);
1523 event
.SetItem( item
);
1524 event
.SetModel( model
);
1525 event
.SetDataFormat( format
);
1526 if (!m_owner
->HandleWindowEvent( event
))
1529 if (!event
.IsAllowed())
1535 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1540 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1541 unsigned int row
= GetLineAt( yy
);
1543 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1546 wxDataViewItem item
= GetItemByRow( row
);
1548 wxDataViewModel
*model
= GetModel();
1550 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1552 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1553 event
.SetEventObject( m_owner
);
1554 event
.SetItem( item
);
1555 event
.SetModel( model
);
1556 event
.SetDataFormat( format
);
1557 event
.SetDataSize( obj
->GetSize() );
1558 event
.SetDataBuffer( obj
->GetData() );
1559 if (!m_owner
->HandleWindowEvent( event
))
1562 if (!event
.IsAllowed())
1568 void wxDataViewMainWindow::OnLeave()
1573 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1575 int height
= GetLineHeight( row
);
1577 unsigned int cols
= GetOwner()->GetColumnCount();
1579 for (col
= 0; col
< cols
; col
++)
1581 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1582 if (column
->IsHidden())
1583 continue; // skip it!
1584 width
+= column
->GetWidth();
1590 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1591 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1592 indent
= indent
+ m_lineHeight
;
1593 // try to use the m_lineHeight as the expander space
1597 wxBitmap
bitmap( width
, height
);
1598 wxMemoryDC
dc( bitmap
);
1599 dc
.SetFont( GetFont() );
1600 dc
.SetPen( *wxBLACK_PEN
);
1601 dc
.SetBrush( *wxWHITE_BRUSH
);
1602 dc
.DrawRectangle( 0,0,width
,height
);
1604 wxDataViewModel
*model
= m_owner
->GetModel();
1606 wxDataViewColumn
* const
1607 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1610 for (col
= 0; col
< cols
; col
++)
1612 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1613 wxDataViewRenderer
*cell
= column
->GetRenderer();
1615 if (column
->IsHidden())
1616 continue; // skip it!
1618 width
= column
->GetWidth();
1620 if (column
== expander
)
1623 wxDataViewItem item
= GetItemByRow( row
);
1624 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1626 wxRect
item_rect(x
, 0, width
, height
);
1627 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1629 // dc.SetClippingRegion( item_rect );
1630 cell
->WXCallRender(item_rect
, &dc
, 0);
1631 // dc.DestroyClippingRegion();
1639 #endif // wxUSE_DRAG_AND_DROP
1642 // Draw focus rect for individual cell. Unlike native focus rect, we render
1643 // this in foreground text color (typically white) to enhance contrast and
1645 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1647 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1649 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1650 // may be short traits and not really dots
1652 // note that to behave in the same manner as DrawRect(), we must exclude
1653 // the bottom and right borders from the rectangle
1654 wxCoord x1
= rect
.GetLeft(),
1656 x2
= rect
.GetRight(),
1657 y2
= rect
.GetBottom();
1659 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1662 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1663 dc
.DrawPoint(z
, rect
.GetTop());
1665 wxCoord shift
= z
== x2
? 0 : 1;
1666 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1667 dc
.DrawPoint(x2
, z
);
1669 shift
= z
== y2
? 0 : 1;
1670 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1671 dc
.DrawPoint(z
, y2
);
1673 shift
= z
== x1
? 0 : 1;
1674 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1675 dc
.DrawPoint(x1
, z
);
1679 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1681 wxDataViewModel
*model
= GetModel();
1682 wxAutoBufferedPaintDC
dc( this );
1685 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1686 dc
.SetPen( *wxTRANSPARENT_PEN
);
1687 dc
.DrawRectangle(GetClientSize());
1692 // No items to draw.
1697 GetOwner()->PrepareDC( dc
);
1698 dc
.SetFont( GetFont() );
1700 wxRect update
= GetUpdateRegion().GetBox();
1701 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1703 // compute which items needs to be redrawn
1704 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1705 unsigned int item_count
=
1706 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1707 (int)(GetRowCount( ) - item_start
));
1708 unsigned int item_last
= item_start
+ item_count
;
1710 // Send the event to wxDataViewCtrl itself.
1711 wxWindow
* const parent
= GetParent();
1712 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1713 cache_event
.SetEventObject(parent
);
1714 cache_event
.SetCache(item_start
, item_last
- 1);
1715 parent
->ProcessWindowEvent(cache_event
);
1717 // compute which columns needs to be redrawn
1718 unsigned int cols
= GetOwner()->GetColumnCount();
1721 // we assume that we have at least one column below and painting an
1722 // empty control is unnecessary anyhow
1726 unsigned int col_start
= 0;
1727 unsigned int x_start
;
1728 for (x_start
= 0; col_start
< cols
; col_start
++)
1730 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1731 if (col
->IsHidden())
1732 continue; // skip it!
1734 unsigned int w
= col
->GetWidth();
1735 if (x_start
+w
>= (unsigned int)update
.x
)
1741 unsigned int col_last
= col_start
;
1742 unsigned int x_last
= x_start
;
1743 for (; col_last
< cols
; col_last
++)
1745 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1746 if (col
->IsHidden())
1747 continue; // skip it!
1749 if (x_last
> (unsigned int)update
.GetRight())
1752 x_last
+= col
->GetWidth();
1755 // Draw background of alternate rows specially if required
1756 if ( m_owner
->HasFlag(wxDV_ROW_LINES
) )
1758 wxColour altRowColour
= m_owner
->m_alternateRowColour
;
1759 if ( !altRowColour
.IsOk() )
1761 // Determine the alternate rows colour automatically from the
1762 // background colour.
1763 const wxColour bgColour
= m_owner
->GetBackgroundColour();
1765 // Depending on the background, alternate row color
1766 // will be 3% more dark or 50% brighter.
1767 int alpha
= bgColour
.GetRGB() > 0x808080 ? 97 : 150;
1768 altRowColour
= bgColour
.ChangeLightness(alpha
);
1771 dc
.SetPen(*wxTRANSPARENT_PEN
);
1772 dc
.SetBrush(wxBrush(altRowColour
));
1774 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1778 dc
.DrawRectangle(x_start
,
1780 GetClientSize().GetWidth(),
1781 GetLineHeight(item
));
1786 // Draw horizontal rules if required
1787 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1789 dc
.SetPen(m_penRule
);
1790 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1792 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1794 int y
= GetLineStart( i
);
1795 dc
.DrawLine(x_start
, y
, x_last
, y
);
1799 // Draw vertical rules if required
1800 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1802 dc
.SetPen(m_penRule
);
1803 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1805 // NB: Vertical rules are drawn in the last pixel of a column so that
1806 // they align perfectly with native MSW wxHeaderCtrl as well as for
1807 // consistency with MSW native list control. There's no vertical
1808 // rule at the most-left side of the control.
1810 int x
= x_start
- 1;
1811 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1813 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1814 if (col
->IsHidden())
1815 continue; // skip it
1817 x
+= col
->GetWidth();
1819 dc
.DrawLine(x
, GetLineStart( item_start
),
1820 x
, GetLineStart( item_last
) );
1824 // redraw the background for the items which are selected/current
1825 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1827 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1829 if (selected
|| item
== m_currentRow
)
1831 wxRect
rect( x_start
, GetLineStart( item
),
1832 x_last
- x_start
, GetLineHeight( item
) );
1834 // draw selection and whole-item focus:
1837 int flags
= wxCONTROL_SELECTED
;
1839 flags
|= wxCONTROL_FOCUSED
;
1841 wxRendererNative::Get().DrawItemSelectionRect
1850 // draw keyboard focus rect if applicable
1851 if ( item
== m_currentRow
&& m_hasFocus
)
1853 bool renderColumnFocus
= false;
1855 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1857 renderColumnFocus
= true;
1859 // If this is container node without columns, render full-row focus:
1862 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1863 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1864 renderColumnFocus
= false;
1868 if ( renderColumnFocus
)
1870 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1872 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1873 if ( col
->IsHidden() )
1876 rect
.width
= col
->GetWidth();
1878 if ( col
== m_currentCol
)
1880 // make the rect more visible by adding a small
1881 // margin around it:
1886 // DrawFocusRect() uses XOR and is all but
1887 // invisible against dark-blue background. Use
1888 // the same color used for selected text.
1889 DrawSelectedCellFocusRect(dc
, rect
);
1893 wxRendererNative::Get().DrawFocusRect
1904 rect
.x
+= rect
.width
;
1909 // render focus rectangle for the whole row
1910 wxRendererNative::Get().DrawFocusRect
1915 selected
? (int)wxCONTROL_SELECTED
: 0
1922 #if wxUSE_DRAG_AND_DROP
1925 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1926 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1927 dc
.SetPen( *wxBLACK_PEN
);
1928 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1929 dc
.DrawRectangle( rect
);
1931 #endif // wxUSE_DRAG_AND_DROP
1933 wxDataViewColumn
* const
1934 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1936 // redraw all cells for all rows which must be repainted and all columns
1938 cell_rect
.x
= x_start
;
1939 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1941 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1942 wxDataViewRenderer
*cell
= col
->GetRenderer();
1943 cell_rect
.width
= col
->GetWidth();
1945 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1946 continue; // skip it!
1948 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1950 // get the cell value and set it into the renderer
1951 wxDataViewTreeNode
*node
= NULL
;
1952 wxDataViewItem dataitem
;
1954 if (!IsVirtualList())
1956 node
= GetTreeNodeByRow(item
);
1960 dataitem
= node
->GetItem();
1962 // Skip all columns of "container" rows except the expander
1963 // column itself unless HasContainerColumns() overrides this.
1964 if ( col
!= expander
&&
1965 model
->IsContainer(dataitem
) &&
1966 !model
->HasContainerColumns(dataitem
) )
1971 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1974 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1977 cell_rect
.y
= GetLineStart( item
);
1978 cell_rect
.height
= GetLineHeight( item
);
1980 // draw the background
1981 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1983 DrawCellBackground( cell
, dc
, cell_rect
);
1985 // deal with the expander
1987 if ((!IsList()) && (col
== expander
))
1989 // Calculate the indent first
1990 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1992 // we reserve m_lineHeight of horizontal space for the expander
1993 // but leave EXPANDER_MARGIN around the expander itself
1994 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1996 indent
+= m_lineHeight
;
1998 // draw expander if needed and visible
1999 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
2001 dc
.SetPen( m_penExpander
);
2002 dc
.SetBrush( wxNullBrush
);
2004 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2005 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
2006 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
2008 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
2011 if ( m_underMouse
== node
)
2012 flag
|= wxCONTROL_CURRENT
;
2013 if ( node
->IsOpen() )
2014 flag
|= wxCONTROL_EXPANDED
;
2016 // ensure that we don't overflow the cell (which might
2017 // happen if the column is very narrow)
2018 wxDCClipper
clip(dc
, cell_rect
);
2020 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2023 // force the expander column to left-center align
2024 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2027 wxRect item_rect
= cell_rect
;
2028 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
2030 // account for the tree indent (harmless if we're not indented)
2031 item_rect
.x
+= indent
;
2032 item_rect
.width
-= indent
;
2034 if ( item_rect
.width
<= 0 )
2038 if (m_hasFocus
&& selected
)
2039 state
|= wxDATAVIEW_CELL_SELECTED
;
2041 // TODO: it would be much more efficient to create a clipping
2042 // region for the entire column being rendered (in the OnPaint
2043 // of wxDataViewMainWindow) instead of a single clip region for
2044 // each cell. However it would mean that each renderer should
2045 // respect the given wxRect's top & bottom coords, eventually
2046 // violating only the left & right coords - however the user can
2047 // make its own renderer and thus we cannot be sure of that.
2048 wxDCClipper
clip(dc
, item_rect
);
2050 cell
->WXCallRender(item_rect
, &dc
, state
);
2053 cell_rect
.x
+= cell_rect
.width
;
2058 void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
)
2060 wxRect
rectBg( rect
);
2062 // don't overlap the horizontal rules
2063 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2069 // don't overlap the vertical rules
2070 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2076 cell
->RenderBackground(&dc
, rectBg
);
2079 void wxDataViewMainWindow::OnRenameTimer()
2081 // We have to call this here because changes may just have
2082 // been made and no screen update taken place.
2085 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2086 // (needs to be tested!)
2090 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2092 StartEditing( item
, m_currentCol
);
2096 wxDataViewMainWindow::StartEditing(const wxDataViewItem
& item
,
2097 const wxDataViewColumn
* col
)
2099 wxDataViewRenderer
* renderer
= col
->GetRenderer();
2100 if ( !IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
2103 const wxRect itemRect
= GetItemRect(item
, col
);
2104 if ( renderer
->StartEditing(item
, itemRect
) )
2106 // Save the renderer to be able to finish/cancel editing it later and
2107 // save the control to be able to detect if we're still editing it.
2108 m_editorRenderer
= renderer
;
2109 m_editorCtrl
= renderer
->GetEditorCtrl();
2113 //-----------------------------------------------------------------------------
2114 // Helper class for do operation on the tree node
2115 //-----------------------------------------------------------------------------
2120 virtual ~DoJob() { }
2122 // The return value control how the tree-walker tranverse the tree
2125 DONE
, // Job done, stop traversing and return
2126 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2127 CONTINUE
// Job not done, continue
2130 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2133 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2135 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2137 switch( func( node
) )
2141 case DoJob::SKIP_SUBTREE
:
2143 case DoJob::CONTINUE
:
2147 if ( node
->HasChildren() )
2149 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2151 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2155 if ( Walker(*i
, func
) )
2163 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2165 if (IsVirtualList())
2167 wxDataViewVirtualListModel
*list_model
=
2168 (wxDataViewVirtualListModel
*) GetModel();
2169 m_count
= list_model
->GetCount();
2175 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2180 wxDataViewItemArray modelSiblings
;
2181 GetModel()->GetChildren(parent
, modelSiblings
);
2182 const int modelSiblingsSize
= modelSiblings
.size();
2184 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2185 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2187 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2188 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2190 parentNode
->SetHasChildren(true);
2192 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2193 const int nodeSiblingsSize
= nodeSiblings
.size();
2197 if ( posInModel
== modelSiblingsSize
- 1 )
2199 nodePos
= nodeSiblingsSize
;
2201 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2203 // This is the simple case when our node tree already matches the
2204 // model and only this one item is missing.
2205 nodePos
= posInModel
;
2209 // It's possible that a larger discrepancy between the model and
2210 // our realization exists. This can happen e.g. when adding a bunch
2211 // of items to the model and then calling ItemsAdded() just once
2212 // afterwards. In this case, we must find the right position by
2213 // looking at sibling items.
2215 // append to the end if we won't find a better position:
2216 nodePos
= nodeSiblingsSize
;
2218 for ( int nextItemPos
= posInModel
+ 1;
2219 nextItemPos
< modelSiblingsSize
;
2222 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2223 if ( nextNodePos
!= wxNOT_FOUND
)
2225 nodePos
= nextNodePos
;
2231 parentNode
->ChangeSubTreeCount(+1);
2232 parentNode
->InsertChild(itemNode
, nodePos
);
2237 GetOwner()->InvalidateColBestWidths();
2243 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2244 const wxDataViewItem
& item
)
2246 if (IsVirtualList())
2248 wxDataViewVirtualListModel
*list_model
=
2249 (wxDataViewVirtualListModel
*) GetModel();
2250 m_count
= list_model
->GetCount();
2252 if ( !m_selection
.empty() )
2254 const int row
= GetRowByItem(item
);
2256 int rowIndexInSelection
= wxNOT_FOUND
;
2258 const size_t selCount
= m_selection
.size();
2259 for ( size_t i
= 0; i
< selCount
; i
++ )
2261 if ( m_selection
[i
] == (unsigned)row
)
2262 rowIndexInSelection
= i
;
2263 else if ( m_selection
[i
] > (unsigned)row
)
2267 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2268 m_selection
.RemoveAt(rowIndexInSelection
);
2272 else // general case
2274 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2276 // Notice that it is possible that the item being deleted is not in the
2277 // tree at all, for example we could be deleting a never shown (because
2278 // collapsed) item in a tree model. So it's not an error if we don't know
2279 // about this item, just return without doing anything then.
2283 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2284 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2286 // We can't use FindNode() to find 'item', because it was already
2287 // removed from the model by the time ItemDeleted() is called, so we
2288 // have to do it manually. We keep track of its position as well for
2290 int itemPosInNode
= 0;
2291 wxDataViewTreeNode
*itemNode
= NULL
;
2292 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2293 i
!= parentsChildren
.end();
2294 ++i
, ++itemPosInNode
)
2296 if( (*i
)->GetItem() == item
)
2303 // If the parent wasn't expanded, it's possible that we didn't have a
2304 // node corresponding to 'item' and so there's nothing left to do.
2307 // If this was the last child to be removed, it's possible the parent
2308 // node became a leaf. Let's ask the model about it.
2309 if ( parentNode
->GetChildNodes().empty() )
2310 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2315 // Delete the item from wxDataViewTreeNode representation:
2316 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2318 parentNode
->RemoveChild(itemNode
);
2320 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2322 // Make the row number invalid and get a new valid one when user call GetRowCount
2325 // If this was the last child to be removed, it's possible the parent
2326 // node became a leaf. Let's ask the model about it.
2327 if ( parentNode
->GetChildNodes().empty() )
2328 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2330 // Update selection by removing 'item' and its entire children tree from the selection.
2331 if ( !m_selection
.empty() )
2333 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2334 // the parent ('parentNode') and position in its list of children
2336 if ( itemPosInNode
== 0 )
2338 // 1st child, row number is that of the parent parentNode + 1
2339 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2343 // row number is that of the sibling above 'item' + its subtree if any + 1
2344 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2346 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2347 siblingNode
->GetSubTreeCount() +
2351 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2353 const size_t numSelections
= m_selection
.size();
2354 for ( size_t i
= 0; i
< numSelections
; ++i
)
2356 const int s
= m_selection
[i
];
2358 newsel
.push_back(s
);
2359 else if ( s
>= itemRow
+ itemsDeleted
)
2360 newsel
.push_back(s
- itemsDeleted
);
2361 // else: deleted item, remove from selection
2364 m_selection
= newsel
;
2368 // Change the current row to the last row if the current exceed the max row number
2369 if( m_currentRow
> GetRowCount() )
2370 ChangeCurrentRow(m_count
- 1);
2372 GetOwner()->InvalidateColBestWidths();
2378 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2383 GetOwner()->InvalidateColBestWidths();
2386 wxWindow
*parent
= GetParent();
2387 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2388 le
.SetEventObject(parent
);
2389 le
.SetModel(GetModel());
2391 parent
->ProcessWindowEvent(le
);
2396 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2398 int view_column
= -1;
2399 unsigned int n_col
= m_owner
->GetColumnCount();
2400 for (unsigned i
= 0; i
< n_col
; i
++)
2402 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2403 if (column
->GetModelColumn() == model_column
)
2405 view_column
= (int) i
;
2409 if (view_column
== -1)
2412 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2413 /*#define MAX_VIRTUAL_WIDTH 100000
2415 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2416 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2417 Refresh( true, &rect );
2424 GetOwner()->InvalidateColBestWidth(view_column
);
2427 wxWindow
*parent
= GetParent();
2428 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2429 le
.SetEventObject(parent
);
2430 le
.SetModel(GetModel());
2432 le
.SetColumn(view_column
);
2433 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2434 parent
->ProcessWindowEvent(le
);
2439 bool wxDataViewMainWindow::Cleared()
2442 m_selection
.Clear();
2445 BuildTree( GetModel() );
2447 GetOwner()->InvalidateColBestWidths();
2453 void wxDataViewMainWindow::UpdateDisplay()
2456 m_underMouse
= NULL
;
2459 void wxDataViewMainWindow::OnInternalIdle()
2461 wxWindow::OnInternalIdle();
2465 RecalculateDisplay();
2470 void wxDataViewMainWindow::RecalculateDisplay()
2472 wxDataViewModel
*model
= GetModel();
2479 int width
= GetEndOfLastCol();
2480 int height
= GetLineStart( GetRowCount() );
2482 SetVirtualSize( width
, height
);
2483 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2488 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2490 m_underMouse
= NULL
;
2492 wxWindow::ScrollWindow( dx
, dy
, rect
);
2494 if (GetOwner()->m_headerArea
)
2495 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2498 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2500 m_underMouse
= NULL
;
2503 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2504 int sy
= GetLineStart( rows
)/y
;
2508 wxRect rect
= GetClientRect();
2512 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2513 for (x_start
= 0; colnum
< column
; colnum
++)
2515 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2516 if (col
->IsHidden())
2517 continue; // skip it!
2519 w
= col
->GetWidth();
2523 int x_end
= x_start
+ w
;
2524 xe
= xx
+ rect
.width
;
2527 sx
= ( xx
+ x_end
- xe
)/x
;
2534 m_owner
->Scroll( sx
, sy
);
2537 int wxDataViewMainWindow::GetCountPerPage() const
2539 wxSize size
= GetClientSize();
2540 return size
.y
/ m_lineHeight
;
2543 int wxDataViewMainWindow::GetEndOfLastCol() const
2547 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2549 const wxDataViewColumn
*c
=
2550 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2553 width
+= c
->GetWidth();
2558 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2562 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2564 return GetLineAt( y
);
2567 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2569 wxSize client_size
= GetClientSize();
2570 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2571 &client_size
.x
, &client_size
.y
);
2573 // we should deal with the pixel here
2574 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2576 return wxMin( GetRowCount()-1, row
);
2579 unsigned int wxDataViewMainWindow::GetRowCount() const
2581 if ( m_count
== -1 )
2583 wxDataViewMainWindow
* const
2584 self
= const_cast<wxDataViewMainWindow
*>(this);
2585 self
->m_count
= RecalculateCount();
2586 self
->UpdateDisplay();
2591 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2598 void wxDataViewMainWindow::SelectAllRows( bool on
)
2605 m_selection
.Clear();
2606 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2607 m_selection
.Add( i
);
2612 unsigned int first_visible
= GetFirstVisibleRow();
2613 unsigned int last_visible
= GetLastVisibleRow();
2615 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2617 unsigned int row
= m_selection
[i
];
2618 if ((row
>= first_visible
) && (row
<= last_visible
))
2621 m_selection
.Clear();
2625 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2627 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2631 m_selection
.Add( row
);
2639 m_selection
.Remove( row
);
2645 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2649 unsigned int tmp
= from
;
2655 for (i
= from
; i
<= to
; i
++)
2657 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2660 m_selection
.Add( i
);
2665 m_selection
.Remove( i
);
2668 RefreshRows( from
, to
);
2671 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2673 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2675 int n
= aSelections
[i
];
2677 m_selection
.Add( n
);
2682 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2684 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2685 m_selection
.Add( row
);
2687 m_selection
.Remove( row
);
2691 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2693 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2696 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2698 wxWindow
*parent
= GetParent();
2699 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2701 le
.SetEventObject(parent
);
2702 le
.SetModel(GetModel());
2705 parent
->ProcessWindowEvent(le
);
2708 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2710 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2711 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2713 wxSize client_size
= GetClientSize();
2714 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2715 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2716 if (intersect_rect
.width
> 0)
2717 Refresh( true, &intersect_rect
);
2720 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2724 unsigned int tmp
= to
;
2729 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2730 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2732 wxSize client_size
= GetClientSize();
2733 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2734 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2735 if (intersect_rect
.width
> 0)
2736 Refresh( true, &intersect_rect
);
2739 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2741 wxSize client_size
= GetClientSize();
2742 int start
= GetLineStart( firstRow
);
2743 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2744 if (start
> client_size
.y
) return;
2746 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2748 Refresh( true, &rect
);
2751 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2755 rect
.y
= GetLineStart( row
);
2756 rect
.width
= GetEndOfLastCol();
2757 rect
.height
= GetLineHeight( row
);
2762 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2764 const wxDataViewModel
*model
= GetModel();
2766 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2768 // TODO make more efficient
2773 for (r
= 0; r
< row
; r
++)
2775 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2776 if (!node
) return start
;
2778 wxDataViewItem item
= node
->GetItem();
2780 unsigned int cols
= GetOwner()->GetColumnCount();
2782 int height
= m_lineHeight
;
2783 for (col
= 0; col
< cols
; col
++)
2785 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2786 if (column
->IsHidden())
2787 continue; // skip it!
2790 model
->IsContainer(item
) &&
2791 !model
->HasContainerColumns(item
))
2792 continue; // skip it!
2794 wxDataViewRenderer
*renderer
=
2795 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2796 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2798 height
= wxMax( height
, renderer
->GetSize().y
);
2808 return row
* m_lineHeight
;
2812 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2814 const wxDataViewModel
*model
= GetModel();
2816 // check for the easy case first
2817 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2818 return y
/ m_lineHeight
;
2820 // TODO make more efficient
2821 unsigned int row
= 0;
2822 unsigned int yy
= 0;
2825 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2828 // not really correct...
2829 return row
+ ((y
-yy
) / m_lineHeight
);
2832 wxDataViewItem item
= node
->GetItem();
2834 unsigned int cols
= GetOwner()->GetColumnCount();
2836 int height
= m_lineHeight
;
2837 for (col
= 0; col
< cols
; col
++)
2839 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2840 if (column
->IsHidden())
2841 continue; // skip it!
2844 model
->IsContainer(item
) &&
2845 !model
->HasContainerColumns(item
))
2846 continue; // skip it!
2848 wxDataViewRenderer
*renderer
=
2849 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2850 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2852 height
= wxMax( height
, renderer
->GetSize().y
);
2863 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2865 const wxDataViewModel
*model
= GetModel();
2867 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2869 wxASSERT( !IsVirtualList() );
2871 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2872 // wxASSERT( node );
2873 if (!node
) return m_lineHeight
;
2875 wxDataViewItem item
= node
->GetItem();
2877 int height
= m_lineHeight
;
2879 unsigned int cols
= GetOwner()->GetColumnCount();
2881 for (col
= 0; col
< cols
; col
++)
2883 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2884 if (column
->IsHidden())
2885 continue; // skip it!
2888 model
->IsContainer(item
) &&
2889 !model
->HasContainerColumns(item
))
2890 continue; // skip it!
2892 wxDataViewRenderer
*renderer
=
2893 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2894 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2896 height
= wxMax( height
, renderer
->GetSize().y
);
2903 return m_lineHeight
;
2908 class RowToTreeNodeJob
: public DoJob
2911 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2914 this->current
= current
;
2919 virtual int operator() ( wxDataViewTreeNode
* node
)
2922 if( current
== static_cast<int>(row
))
2928 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2930 current
+= node
->GetSubTreeCount();
2931 return DoJob::SKIP_SUBTREE
;
2937 // If the current node has only leaf children, we can find the
2938 // desired node directly. This can speed up finding the node
2939 // in some cases, and will have a very good effect for list views.
2940 if ( node
->HasChildren() &&
2941 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2943 const int index
= static_cast<int>(row
) - current
- 1;
2944 ret
= node
->GetChildNodes()[index
];
2948 return DoJob::CONTINUE
;
2952 wxDataViewTreeNode
* GetResult() const
2958 wxDataViewTreeNode
* ret
;
2959 wxDataViewTreeNode
* parent
;
2962 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2964 wxASSERT( !IsVirtualList() );
2966 RowToTreeNodeJob
job( row
, -2, m_root
);
2967 Walker( m_root
, job
);
2968 return job
.GetResult();
2971 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2973 wxDataViewItem item
;
2974 if (IsVirtualList())
2976 if ( row
< GetRowCount() )
2977 item
= wxDataViewItem(wxUIntToPtr(row
+1));
2981 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2983 item
= node
->GetItem();
2990 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2991 const wxDataViewItem
& item
)
2993 wxWindow
*parent
= GetParent();
2994 wxDataViewEvent
le(type
, parent
->GetId());
2996 le
.SetEventObject(parent
);
2997 le
.SetModel(GetModel());
3000 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
3003 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
3008 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3012 if (!node
->HasChildren())
3015 return node
->IsOpen();
3018 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
3023 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3027 if (!node
->HasChildren())
3033 void wxDataViewMainWindow::Expand( unsigned int row
)
3038 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3042 if (!node
->HasChildren())
3045 if (!node
->IsOpen())
3047 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
3049 // Vetoed by the event handler.
3055 // build the children of current node
3056 if( node
->GetChildNodes().empty() )
3059 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
3062 // By expanding the node all row indices that are currently in the selection list
3063 // and are greater than our node have become invalid. So we have to correct that now.
3064 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3065 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3067 const unsigned testRow
= m_selection
[i
];
3068 // all rows above us are not affected, so skip them
3072 m_selection
[i
] += rowAdjustment
;
3075 if(m_currentRow
> row
)
3076 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3080 // Send the expanded event
3081 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3085 void wxDataViewMainWindow::Collapse(unsigned int row
)
3090 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3094 if (!node
->HasChildren())
3099 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3101 // Vetoed by the event handler.
3105 // Find out if there are selected items below the current node.
3106 bool selectCollapsingRow
= false;
3107 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3108 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3109 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3111 const unsigned testRow
= m_selection
[i
];
3112 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3114 selectCollapsingRow
= true;
3115 // get out as soon as we have found a node that is selected
3122 // If the node to be closed has selected items the user won't see those any longer.
3123 // We select the collapsing node in this case.
3124 if(selectCollapsingRow
)
3126 SelectAllRows(false);
3127 ChangeCurrentRow(row
);
3128 SelectRow(row
, true);
3129 SendSelectionChangedEvent(GetItemByRow(row
));
3133 // if there were no selected items below our node we still need to "fix" the
3134 // selection list to adjust for the changing of the row indices.
3135 // We actually do the opposite of what we are doing in Expand().
3136 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3138 const unsigned testRow
= m_selection
[i
];
3139 // all rows above us are not affected, so skip them
3143 m_selection
[i
] -= rowAdjustment
;
3146 // if the "current row" is being collapsed away we change it to the current row ;-)
3147 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3148 ChangeCurrentRow(row
);
3149 else if(m_currentRow
> row
)
3150 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3155 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3159 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3161 const wxDataViewModel
* model
= GetModel();
3168 // Compose the parent-chain for the item we are looking for
3169 wxVector
<wxDataViewItem
> parentChain
;
3170 wxDataViewItem
it( item
);
3173 parentChain
.push_back(it
);
3174 it
= model
->GetParent(it
);
3177 // Find the item along the parent-chain.
3178 // This algorithm is designed to speed up the node-finding method
3179 wxDataViewTreeNode
* node
= m_root
;
3180 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3182 if( node
->HasChildren() )
3184 if( node
->GetChildNodes().empty() )
3186 // Even though the item is a container, it doesn't have any
3187 // child nodes in the control's representation yet. We have
3188 // to realize its subtree now.
3190 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3193 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3196 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3198 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3199 if (currentNode
->GetItem() == parentChain
[iter
])
3201 if (currentNode
->GetItem() == item
)
3221 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3222 wxDataViewColumn
* &column
)
3224 wxDataViewColumn
*col
= NULL
;
3225 unsigned int cols
= GetOwner()->GetColumnCount();
3226 unsigned int colnum
= 0;
3228 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3229 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3231 col
= GetOwner()->GetColumnAt(colnum
);
3232 if (col
->IsHidden())
3233 continue; // skip it!
3235 unsigned int w
= col
->GetWidth();
3236 if (x_start
+w
>= (unsigned int)x
)
3243 item
= GetItemByRow( GetLineAt( y
) );
3246 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3247 const wxDataViewColumn
* column
)
3252 unsigned int cols
= GetOwner()->GetColumnCount();
3253 // If column is null the loop will compute the combined width of all columns.
3254 // Otherwise, it will compute the x position of the column we are looking for.
3255 for (unsigned int i
= 0; i
< cols
; i
++)
3257 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3262 if (col
->IsHidden())
3263 continue; // skip it!
3265 xpos
+= col
->GetWidth();
3266 width
+= col
->GetWidth();
3271 // If we have a column, we need can get its width directly.
3272 if(column
->IsHidden())
3275 width
= column
->GetWidth();
3280 // If we have no column, we reset the x position back to zero.
3284 // we have to take an expander column into account and compute its indentation
3285 // to get the correct x position where the actual text is
3287 int row
= GetRowByItem(item
);
3289 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3291 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3292 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3293 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3296 wxRect
itemRect( xpos
+ indent
,
3297 GetLineStart( row
),
3299 GetLineHeight( row
) );
3301 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3302 &itemRect
.x
, &itemRect
.y
);
3307 int wxDataViewMainWindow::RecalculateCount() const
3309 if (IsVirtualList())
3311 wxDataViewVirtualListModel
*list_model
=
3312 (wxDataViewVirtualListModel
*) GetModel();
3314 return list_model
->GetCount();
3318 return m_root
->GetSubTreeCount();
3322 class ItemToRowJob
: public DoJob
3325 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3332 // Maybe binary search will help to speed up this process
3333 virtual int operator() ( wxDataViewTreeNode
* node
)
3336 if( node
->GetItem() == item
)
3341 if( node
->GetItem() == *m_iter
)
3344 return DoJob::CONTINUE
;
3348 ret
+= node
->GetSubTreeCount();
3349 return DoJob::SKIP_SUBTREE
;
3354 // the row number is begin from zero
3355 int GetResult() const
3359 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3360 wxDataViewItem item
;
3365 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3367 const wxDataViewModel
* model
= GetModel();
3371 if (IsVirtualList())
3373 return wxPtrToUInt( item
.GetID() ) -1;
3380 // Compose the parent-chain of the item we are looking for
3381 wxVector
<wxDataViewItem
> parentChain
;
3382 wxDataViewItem
it( item
);
3385 parentChain
.push_back(it
);
3386 it
= model
->GetParent(it
);
3389 // add an 'invalid' item to represent our 'invisible' root node
3390 parentChain
.push_back(wxDataViewItem());
3392 // the parent chain was created by adding the deepest parent first.
3393 // so if we want to start at the root node, we have to iterate backwards through the vector
3394 ItemToRowJob
job( item
, parentChain
.rbegin() );
3395 Walker( m_root
, job
);
3396 return job
.GetResult();
3400 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3401 wxDataViewTreeNode
* node
)
3403 if( !model
->IsContainer( item
) )
3406 wxDataViewItemArray children
;
3407 unsigned int num
= model
->GetChildren( item
, children
);
3409 for ( unsigned int index
= 0; index
< num
; index
++ )
3411 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3413 if( model
->IsContainer(children
[index
]) )
3414 n
->SetHasChildren( true );
3416 node
->InsertChild(n
, index
);
3419 wxASSERT( node
->IsOpen() );
3420 node
->ChangeSubTreeCount(+num
);
3423 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3427 if (GetModel()->IsVirtualListModel())
3433 m_root
= wxDataViewTreeNode::CreateRootNode();
3435 // First we define a invalid item to fetch the top-level elements
3436 wxDataViewItem item
;
3438 BuildTreeHelper( model
, item
, m_root
);
3442 void wxDataViewMainWindow::DestroyTree()
3444 if (!IsVirtualList())
3452 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3454 // Edit the current column editable in 'mode'. If no column is focused
3455 // (typically because the user has full row selected), try to find the
3456 // first editable column (this would typically be a checkbox for
3457 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3458 // focus on the checkbox column; or on the only editable text column).
3460 wxDataViewColumn
*candidate
= m_currentCol
;
3463 !IsCellEditableInMode(item
, candidate
, mode
) &&
3464 !m_currentColSetByKeyboard
)
3466 // If current column was set by mouse to something not editable (in
3467 // 'mode') and the user pressed Space/F2 to edit it, treat the
3468 // situation as if there was whole-row focus, because that's what is
3469 // visually indicated and the mouse click could very well be targeted
3470 // on the row rather than on an individual cell.
3472 // But if it was done by keyboard, respect that even if the column
3473 // isn't editable, because focus is visually on that column and editing
3474 // something else would be surprising.
3480 const unsigned cols
= GetOwner()->GetColumnCount();
3481 for ( unsigned i
= 0; i
< cols
; i
++ )
3483 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3484 if ( c
->IsHidden() )
3487 if ( IsCellEditableInMode(item
, c
, mode
) )
3495 // If on container item without columns, only the expander column
3496 // may be directly editable:
3498 GetOwner()->GetExpanderColumn() != candidate
&&
3499 GetModel()->IsContainer(item
) &&
3500 !GetModel()->HasContainerColumns(item
) )
3502 candidate
= GetOwner()->GetExpanderColumn();
3508 if ( !IsCellEditableInMode(item
, candidate
, mode
) )
3514 bool wxDataViewMainWindow::IsCellEditableInMode(const wxDataViewItem
& item
,
3515 const wxDataViewColumn
*col
,
3516 wxDataViewCellMode mode
) const
3518 if ( col
->GetRenderer()->GetMode() != mode
)
3521 if ( !GetModel()->IsEnabled(item
, col
->GetModelColumn()) )
3527 void wxDataViewMainWindow::OnCharHook(wxKeyEvent
& event
)
3531 // Handle any keys special for the in-place editor and return without
3532 // calling Skip() below.
3533 switch ( event
.GetKeyCode() )
3536 m_editorRenderer
->CancelEditing();
3540 m_editorRenderer
->FinishEditing();
3548 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3550 wxWindow
* const parent
= GetParent();
3552 // propagate the char event upwards
3553 wxKeyEvent
eventForParent(event
);
3554 eventForParent
.SetEventObject(parent
);
3555 if ( parent
->ProcessWindowEvent(eventForParent
) )
3558 if ( parent
->HandleAsNavigationKey(event
) )
3561 // no item -> nothing to do
3562 if (!HasCurrentRow())
3568 // don't use m_linesPerPage directly as it might not be computed yet
3569 const int pageSize
= GetCountPerPage();
3570 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3572 switch ( event
.GetKeyCode() )
3575 if ( event
.HasModifiers() )
3582 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3583 // it. Only if that event is not handled do we activate column renderer (which
3584 // is normally done by Space) or even inline editing.
3586 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3588 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3591 le
.SetEventObject(parent
);
3592 le
.SetModel(GetModel());
3594 if ( parent
->ProcessWindowEvent(le
) )
3596 // else: fall through to WXK_SPACE handling
3600 if ( event
.HasModifiers() )
3607 // Space toggles activatable items or -- if not activatable --
3608 // starts inline editing (this is normally done using F2 on
3609 // Windows, but Space is common everywhere else, so use it too
3610 // for greater cross-platform compatibility).
3612 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3614 // Activate the current activatable column. If not column is focused (typically
3615 // because the user has full row selected), try to find the first activatable
3616 // column (this would typically be a checkbox and we don't want to force the user
3617 // to set focus on the checkbox column).
3618 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3620 if ( activatableCol
)
3622 const unsigned colIdx
= activatableCol
->GetModelColumn();
3623 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3625 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3626 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3627 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3631 // else: fall through to WXK_F2 handling
3635 if ( event
.HasModifiers() )
3642 if( !m_selection
.empty() )
3644 // Mimic Windows 7 behavior: edit the item that has focus
3645 // if it is selected and the first selected item if focus
3646 // is out of selection.
3648 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3651 sel
= m_selection
[0];
3654 const wxDataViewItem item
= GetItemByRow(sel
);
3656 // Edit the current column. If no column is focused
3657 // (typically because the user has full row selected), try
3658 // to find the first editable column.
3659 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3662 GetOwner()->EditItem(item
, editableCol
);
3668 if ( m_currentRow
> 0 )
3669 OnVerticalNavigation( m_currentRow
- 1, event
);
3673 if ( m_currentRow
+ 1 < GetRowCount() )
3674 OnVerticalNavigation( m_currentRow
+ 1, event
);
3676 // Add the process for tree expanding/collapsing
3688 OnVerticalNavigation( GetRowCount() - 1, event
);
3693 OnVerticalNavigation( 0, event
);
3698 int steps
= pageSize
- 1;
3699 int index
= m_currentRow
- steps
;
3703 OnVerticalNavigation( index
, event
);
3709 int steps
= pageSize
- 1;
3710 unsigned int index
= m_currentRow
+ steps
;
3711 unsigned int count
= GetRowCount();
3712 if ( index
>= count
)
3715 OnVerticalNavigation( index
, event
);
3724 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3726 wxCHECK_RET( newCurrent
< GetRowCount(),
3727 wxT("invalid item index in OnVerticalNavigation()") );
3729 // if there is no selection, we cannot move it anywhere
3730 if (!HasCurrentRow())
3733 unsigned int oldCurrent
= m_currentRow
;
3735 // in single selection we just ignore Shift as we can't select several
3737 if ( event
.ShiftDown() && !IsSingleSel() )
3739 RefreshRow( oldCurrent
);
3741 ChangeCurrentRow( newCurrent
);
3743 // select all the items between the old and the new one
3744 if ( oldCurrent
> newCurrent
)
3746 newCurrent
= oldCurrent
;
3747 oldCurrent
= m_currentRow
;
3750 SelectRows( oldCurrent
, newCurrent
, true );
3751 if (oldCurrent
!=newCurrent
)
3752 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3756 RefreshRow( oldCurrent
);
3758 // all previously selected items are unselected unless ctrl is held
3759 if ( !event
.ControlDown() )
3760 SelectAllRows(false);
3762 ChangeCurrentRow( newCurrent
);
3764 if ( !event
.ControlDown() )
3766 SelectRow( m_currentRow
, true );
3767 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3770 RefreshRow( m_currentRow
);
3773 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3776 void wxDataViewMainWindow::OnLeftKey()
3780 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3784 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3786 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3789 // Because TryAdvanceCurrentColumn() return false, we are at the first
3790 // column or using whole-row selection. In this situation, we can use
3791 // the standard TreeView handling of the left key.
3792 if (node
->HasChildren() && node
->IsOpen())
3794 Collapse(m_currentRow
);
3798 // if the node is already closed, we move the selection to its parent
3799 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3803 int parent
= GetRowByItem( parent_node
->GetItem() );
3806 unsigned int row
= m_currentRow
;
3807 SelectRow( row
, false);
3808 SelectRow( parent
, true );
3809 ChangeCurrentRow( parent
);
3810 GetOwner()->EnsureVisible( parent
, -1 );
3811 SendSelectionChangedEvent( parent_node
->GetItem() );
3818 void wxDataViewMainWindow::OnRightKey()
3822 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3826 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3828 if ( node
->HasChildren() )
3830 if ( !node
->IsOpen() )
3832 Expand( m_currentRow
);
3836 // if the node is already open, we move the selection to the first child
3837 unsigned int row
= m_currentRow
;
3838 SelectRow( row
, false );
3839 SelectRow( row
+ 1, true );
3840 ChangeCurrentRow( row
+ 1 );
3841 GetOwner()->EnsureVisible( row
+ 1, -1 );
3842 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3847 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3852 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3854 if ( GetOwner()->GetColumnCount() == 0 )
3857 if ( !m_useCellFocus
)
3862 // navigation shouldn't work in branch nodes without other columns:
3863 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3867 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3871 m_currentCol
= GetOwner()->GetColumnAt(1);
3872 m_currentColSetByKeyboard
= true;
3873 RefreshRow(m_currentRow
);
3880 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3882 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3885 GetOwner()->EnsureVisible(m_currentRow
, idx
);
3889 // We are going to the left of the second column. Reset to whole-row
3890 // focus (which means first column would be edited).
3891 m_currentCol
= NULL
;
3892 RefreshRow(m_currentRow
);
3896 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3897 m_currentColSetByKeyboard
= true;
3898 RefreshRow(m_currentRow
);
3902 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3904 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3906 // let the base handle mouse wheel events.
3911 // set the focus to ourself if any of the mouse buttons are pressed
3912 if(event
.ButtonDown() && !HasFocus())
3915 int x
= event
.GetX();
3916 int y
= event
.GetY();
3917 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3918 wxDataViewColumn
*col
= NULL
;
3921 unsigned int cols
= GetOwner()->GetColumnCount();
3923 for (i
= 0; i
< cols
; i
++)
3925 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3927 continue; // skip it!
3929 if (x
< xpos
+ c
->GetWidth())
3934 xpos
+= c
->GetWidth();
3937 wxDataViewModel
* const model
= GetModel();
3939 const unsigned int current
= GetLineAt( y
);
3940 const wxDataViewItem item
= GetItemByRow(current
);
3942 // Handle right clicking here, before everything else as context menu
3943 // events should be sent even when we click outside of any item, unlike all
3945 if (event
.RightUp())
3947 wxWindow
*parent
= GetParent();
3948 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3949 le
.SetEventObject(parent
);
3952 if ( item
.IsOk() && col
)
3955 le
.SetColumn( col
->GetModelColumn() );
3956 le
.SetDataViewColumn( col
);
3959 model
->GetValue( value
, item
, col
->GetModelColumn() );
3963 parent
->ProcessWindowEvent(le
);
3973 wxDataViewRenderer
*cell
= col
->GetRenderer();
3974 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3976 // Unselect all if below the last row ?
3981 wxDataViewColumn
* const
3982 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3984 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3985 // button) and also determine the offset of the real cell start, skipping
3986 // the indentation and the expander itself.
3987 bool hoverOverExpander
= false;
3989 if ((!IsList()) && (expander
== col
))
3991 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3993 int indent
= node
->GetIndentLevel();
3994 itemOffset
= GetOwner()->GetIndent()*indent
;
3996 if ( node
->HasChildren() )
3998 // we make the rectangle we are looking in a bit bigger than the actual
3999 // visual expander so the user can hit that little thing reliably
4000 wxRect
rect(itemOffset
,
4001 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
4002 m_lineHeight
, m_lineHeight
);
4004 if( rect
.Contains(x
, y
) )
4006 // So the mouse is over the expander
4007 hoverOverExpander
= true;
4008 if (m_underMouse
&& m_underMouse
!= node
)
4010 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4011 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4013 if (m_underMouse
!= node
)
4015 // wxLogMessage("Do the row: %d", current);
4016 RefreshRow(current
);
4018 m_underMouse
= node
;
4022 // Account for the expander as well, even if this item doesn't have it,
4023 // its parent does so it still counts for the offset.
4024 itemOffset
+= m_lineHeight
;
4026 if (!hoverOverExpander
)
4028 if (m_underMouse
!= NULL
)
4030 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4031 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4032 m_underMouse
= NULL
;
4036 #if wxUSE_DRAG_AND_DROP
4037 if (event
.Dragging())
4039 if (m_dragCount
== 0)
4041 // we have to report the raw, physical coords as we want to be
4042 // able to call HitTest(event.m_pointDrag) from the user code to
4043 // get the item being dragged
4044 m_dragStart
= event
.GetPosition();
4049 if (m_dragCount
!= 3)
4052 if (event
.LeftIsDown())
4054 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
4055 &m_dragStart
.x
, &m_dragStart
.y
);
4056 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
4057 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
4059 // Notify cell about drag
4060 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
4061 event
.SetEventObject( m_owner
);
4062 event
.SetItem( itemDragged
);
4063 event
.SetModel( model
);
4064 if (!m_owner
->HandleWindowEvent( event
))
4067 if (!event
.IsAllowed())
4070 wxDataObject
*obj
= event
.GetDataObject();
4074 wxDataViewDropSource
drag( this, drag_item_row
);
4075 drag
.SetData( *obj
);
4076 /* wxDragResult res = */ drag
.DoDragDrop();
4085 #endif // wxUSE_DRAG_AND_DROP
4087 bool simulateClick
= false;
4089 if (event
.ButtonDClick())
4091 m_renameTimer
->Stop();
4092 m_lastOnSame
= false;
4095 bool ignore_other_columns
=
4096 ((expander
!= col
) &&
4097 (model
->IsContainer(item
)) &&
4098 (!model
->HasContainerColumns(item
)));
4100 if (event
.LeftDClick())
4102 if(hoverOverExpander
)
4104 // a double click on the expander will be converted into a "simulated" normal click
4105 simulateClick
= true;
4107 else if ( current
== m_lineLastClicked
)
4109 wxWindow
*parent
= GetParent();
4110 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4112 le
.SetColumn( col
->GetModelColumn() );
4113 le
.SetDataViewColumn( col
);
4114 le
.SetEventObject(parent
);
4115 le
.SetModel(GetModel());
4117 parent
->ProcessWindowEvent(le
);
4122 // The first click was on another item, so don't interpret this as
4123 // a double click, but as a simple click instead
4124 simulateClick
= true;
4128 if (event
.LeftUp() && !hoverOverExpander
)
4130 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4132 // select single line
4133 SelectAllRows( false );
4134 SelectRow( m_lineSelectSingleOnUp
, true );
4135 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4138 // If the user click the expander, we do not do editing even if the column
4139 // with expander are editable
4140 if (m_lastOnSame
&& !ignore_other_columns
)
4142 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4143 IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
4145 m_renameTimer
->Start( 100, true );
4149 m_lastOnSame
= false;
4150 m_lineSelectSingleOnUp
= (unsigned int)-1;
4152 else if(!event
.LeftUp())
4154 // This is necessary, because after a DnD operation in
4155 // from and to ourself, the up event is swallowed by the
4156 // DnD code. So on next non-up event (which means here and
4157 // now) m_lineSelectSingleOnUp should be reset.
4158 m_lineSelectSingleOnUp
= (unsigned int)-1;
4161 if (event
.RightDown())
4163 m_lineBeforeLastClicked
= m_lineLastClicked
;
4164 m_lineLastClicked
= current
;
4166 // If the item is already selected, do not update the selection.
4167 // Multi-selections should not be cleared if a selected item is clicked.
4168 if (!IsRowSelected(current
))
4170 SelectAllRows(false);
4171 const unsigned oldCurrent
= m_currentRow
;
4172 ChangeCurrentRow(current
);
4173 SelectRow(m_currentRow
,true);
4174 RefreshRow(oldCurrent
);
4175 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4178 else if (event
.MiddleDown())
4182 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4184 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4186 // hoverOverExpander being true tells us that our node must be
4187 // valid and have children.
4188 // So we don't need any extra checks.
4189 if( node
->IsOpen() )
4194 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4196 m_lineBeforeLastClicked
= m_lineLastClicked
;
4197 m_lineLastClicked
= current
;
4199 unsigned int oldCurrentRow
= m_currentRow
;
4200 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4202 bool cmdModifierDown
= event
.CmdDown();
4203 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4205 if ( IsSingleSel() || !IsRowSelected(current
) )
4207 SelectAllRows( false );
4208 ChangeCurrentRow(current
);
4209 SelectRow(m_currentRow
,true);
4210 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4212 else // multi sel & current is highlighted & no mod keys
4214 m_lineSelectSingleOnUp
= current
;
4215 ChangeCurrentRow(current
); // change focus
4218 else // multi sel & either ctrl or shift is down
4220 if (cmdModifierDown
)
4222 ChangeCurrentRow(current
);
4223 ReverseRowSelection(m_currentRow
);
4224 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4226 else if (event
.ShiftDown())
4228 ChangeCurrentRow(current
);
4230 unsigned int lineFrom
= oldCurrentRow
,
4233 if ( lineTo
< lineFrom
)
4236 lineFrom
= m_currentRow
;
4239 SelectRows(lineFrom
, lineTo
, true);
4240 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4242 else // !ctrl, !shift
4244 // test in the enclosing if should make it impossible
4245 wxFAIL_MSG( wxT("how did we get here?") );
4249 if (m_currentRow
!= oldCurrentRow
)
4250 RefreshRow( oldCurrentRow
);
4252 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4254 // Update selection here...
4256 m_currentColSetByKeyboard
= false;
4258 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4259 (current
== oldCurrentRow
)) && oldWasSelected
;
4261 // Call ActivateCell() after everything else as under GTK+
4262 if ( IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_ACTIVATABLE
) )
4264 // notify cell about click
4265 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4267 wxRect
cell_rect( xpos
+ itemOffset
,
4268 GetLineStart( current
),
4269 col
->GetWidth() - itemOffset
,
4270 GetLineHeight( current
) );
4272 // Report position relative to the cell's custom area, i.e.
4273 // no the entire space as given by the control but the one
4274 // used by the renderer after calculation of alignment etc.
4276 // adjust the rectangle ourselves to account for the alignment
4277 wxRect rectItem
= cell_rect
;
4278 const int align
= cell
->GetAlignment();
4279 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4281 const wxSize size
= cell
->GetSize();
4283 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4285 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4286 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4287 else if ( align
& wxALIGN_RIGHT
)
4288 rectItem
.x
+= cell_rect
.width
- size
.x
;
4289 // else: wxALIGN_LEFT is the default
4292 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4294 if ( align
& wxALIGN_CENTER_VERTICAL
)
4295 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4296 else if ( align
& wxALIGN_BOTTOM
)
4297 rectItem
.y
+= cell_rect
.height
- size
.y
;
4298 // else: wxALIGN_TOP is the default
4302 wxMouseEvent
event2(event
);
4303 event2
.m_x
-= rectItem
.x
;
4304 event2
.m_y
-= rectItem
.y
;
4305 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4307 /* ignore ret */ cell
->WXActivateCell
4312 col
->GetModelColumn(),
4319 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4323 if (HasCurrentRow())
4329 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4333 if (HasCurrentRow())
4339 void wxDataViewMainWindow::OnColumnsCountChanged()
4341 int editableCount
= 0;
4343 const unsigned cols
= GetOwner()->GetColumnCount();
4344 for ( unsigned i
= 0; i
< cols
; i
++ )
4346 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4347 if ( c
->IsHidden() )
4349 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4353 m_useCellFocus
= (editableCount
> 1);
4358 //-----------------------------------------------------------------------------
4360 //-----------------------------------------------------------------------------
4362 WX_DEFINE_LIST(wxDataViewColumnList
)
4364 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4365 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4366 EVT_SIZE(wxDataViewCtrl::OnSize
)
4369 wxDataViewCtrl::~wxDataViewCtrl()
4372 GetModel()->RemoveNotifier( m_notifier
);
4375 m_colsBestWidths
.clear();
4378 void wxDataViewCtrl::Init()
4380 m_cols
.DeleteContents(true);
4383 // No sorting column at start
4384 m_sortingColumnIdx
= wxNOT_FOUND
;
4386 m_headerArea
= NULL
;
4388 m_colsDirty
= false;
4391 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4396 const wxValidator
& validator
,
4397 const wxString
& name
)
4399 // if ( (style & wxBORDER_MASK) == 0)
4400 // style |= wxBORDER_SUNKEN;
4404 if (!wxControl::Create( parent
, id
, pos
, size
,
4405 style
| wxScrolledWindowStyle
, validator
, name
))
4408 SetInitialSize(size
);
4411 MacSetClipChildren( true );
4414 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4416 // We use the cursor keys for moving the selection, not scrolling, so call
4417 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4418 // keyboard events forwarded to us from wxListMainWindow.
4419 DisableKeyboardScrolling();
4421 if (HasFlag(wxDV_NO_HEADER
))
4422 m_headerArea
= NULL
;
4424 m_headerArea
= new wxDataViewHeaderWindow(this);
4426 SetTargetWindow( m_clientArea
);
4428 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4430 sizer
->Add( m_headerArea
, 0, wxGROW
);
4431 sizer
->Add( m_clientArea
, 1, wxGROW
);
4437 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4439 return wxBORDER_THEME
;
4443 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4447 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4450 // we need to process arrows ourselves for scrolling
4451 if ( nMsg
== WM_GETDLGCODE
)
4453 rc
|= DLGC_WANTARROWS
;
4461 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4463 wxSize newsize
= size
;
4464 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4465 newsize
.y
-= m_headerArea
->GetSize().y
;
4470 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4472 // We need to override OnSize so that our scrolled
4473 // window a) does call Layout() to use sizers for
4474 // positioning the controls but b) does not query
4475 // the sizer for their size and use that for setting
4476 // the scrollable area as set that ourselves by
4477 // calling SetScrollbar() further down.
4483 // We must redraw the headers if their height changed. Normally this
4484 // shouldn't happen as the control shouldn't let itself be resized beneath
4485 // its minimal height but avoid the display artefacts that appear if it
4486 // does happen, e.g. because there is really not enough vertical space.
4487 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4488 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4490 m_headerArea
->Refresh();
4494 void wxDataViewCtrl::SetFocus()
4497 m_clientArea
->SetFocus();
4500 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4502 if (!wxDataViewCtrlBase::AssociateModel( model
))
4505 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4507 model
->AddNotifier( m_notifier
);
4509 m_clientArea
->DestroyTree();
4511 m_clientArea
->BuildTree(model
);
4513 m_clientArea
->UpdateDisplay();
4518 #if wxUSE_DRAG_AND_DROP
4520 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4522 return m_clientArea
->EnableDragSource( format
);
4525 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4527 return m_clientArea
->EnableDropTarget( format
);
4530 #endif // wxUSE_DRAG_AND_DROP
4532 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4534 if (!wxDataViewCtrlBase::AppendColumn(col
))
4537 m_cols
.Append( col
);
4538 m_colsBestWidths
.push_back(0);
4539 OnColumnsCountChanged();
4543 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4545 if (!wxDataViewCtrlBase::PrependColumn(col
))
4548 m_cols
.Insert( col
);
4549 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4550 OnColumnsCountChanged();
4554 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4556 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4559 m_cols
.Insert( pos
, col
);
4560 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4561 OnColumnsCountChanged();
4565 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4568 m_headerArea
->UpdateColumn(idx
);
4570 m_clientArea
->UpdateDisplay();
4573 void wxDataViewCtrl::OnColumnsCountChanged()
4576 m_headerArea
->SetColumnCount(GetColumnCount());
4578 m_clientArea
->OnColumnsCountChanged();
4581 void wxDataViewCtrl::DoSetExpanderColumn()
4583 m_clientArea
->UpdateDisplay();
4586 void wxDataViewCtrl::DoSetIndent()
4588 m_clientArea
->UpdateDisplay();
4591 unsigned int wxDataViewCtrl::GetColumnCount() const
4593 return m_cols
.GetCount();
4596 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4598 if ( !m_clientArea
)
4601 m_clientArea
->SetRowHeight(lineHeight
);
4606 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4611 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4613 // columns can't be reordered if there is no header window which allows
4615 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4618 return GetColumn(idx
);
4621 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4623 const unsigned count
= m_cols
.size();
4624 for ( unsigned n
= 0; n
< count
; n
++ )
4626 if ( m_cols
[n
] == column
)
4633 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4635 if ( m_colsBestWidths
[idx
] != 0 )
4636 return m_colsBestWidths
[idx
];
4638 const int count
= m_clientArea
->GetRowCount();
4639 wxDataViewColumn
*column
= GetColumn(idx
);
4640 wxDataViewRenderer
*renderer
=
4641 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4643 class MaxWidthCalculator
4646 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4647 wxDataViewMainWindow
*clientArea
,
4648 wxDataViewRenderer
*renderer
,
4649 const wxDataViewModel
*model
,
4654 m_clientArea(clientArea
),
4655 m_renderer(renderer
),
4658 m_expanderSize(expanderSize
)
4662 !clientArea
->IsList() &&
4664 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4667 void UpdateWithWidth(int width
)
4669 m_width
= wxMax(m_width
, width
);
4672 void UpdateWithRow(int row
)
4675 wxDataViewItem item
;
4677 if ( m_isExpanderCol
)
4679 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4680 item
= node
->GetItem();
4681 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4685 item
= m_clientArea
->GetItemByRow(row
);
4688 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4689 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4692 int GetMaxWidth() const { return m_width
; }
4696 const wxDataViewCtrl
*m_dvc
;
4697 wxDataViewMainWindow
*m_clientArea
;
4698 wxDataViewRenderer
*m_renderer
;
4699 const wxDataViewModel
*m_model
;
4701 bool m_isExpanderCol
;
4705 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4706 GetModel(), column
->GetModelColumn(),
4707 m_clientArea
->GetRowHeight());
4710 calculator
.UpdateWithWidth(m_headerArea
->GetColumnTitleWidth(*column
));
4712 // The code below deserves some explanation. For very large controls, we
4713 // simply can't afford to calculate sizes for all items, it takes too
4714 // long. So the best we can do is to check the first and the last N/2
4715 // items in the control for some sufficiently large N and calculate best
4716 // sizes from that. That can result in the calculated best width being too
4717 // small for some outliers, but it's better to get slightly imperfect
4718 // result than to wait several seconds after every update. To avoid highly
4719 // visible miscalculations, we also include all currently visible items
4720 // no matter what. Finally, the value of N is determined dynamically by
4721 // measuring how much time we spent on the determining item widths so far.
4724 int top_part_end
= count
;
4725 static const long CALC_TIMEOUT
= 20/*ms*/;
4726 // don't call wxStopWatch::Time() too often
4727 static const unsigned CALC_CHECK_FREQ
= 100;
4730 // use some hard-coded limit, that's the best we can do without timer
4731 int top_part_end
= wxMin(500, count
);
4732 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4736 for ( row
= 0; row
< top_part_end
; row
++ )
4739 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4740 timer
.Time() > CALC_TIMEOUT
)
4742 #endif // wxUSE_STOPWATCH
4743 calculator
.UpdateWithRow(row
);
4746 // row is the first unmeasured item now; that's our value of N/2
4752 // add bottom N/2 items now:
4753 const int bottom_part_start
= wxMax(row
, count
- row
);
4754 for ( row
= bottom_part_start
; row
< count
; row
++ )
4756 calculator
.UpdateWithRow(row
);
4759 // finally, include currently visible items in the calculation:
4760 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4761 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4762 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4764 first_visible
= wxMax(first_visible
, top_part_end
);
4765 last_visible
= wxMin(bottom_part_start
, last_visible
);
4767 for ( row
= first_visible
; row
< last_visible
; row
++ )
4769 calculator
.UpdateWithRow(row
);
4772 wxLogTrace("dataview",
4773 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4775 count
- bottom_part_start
,
4776 wxMax(0, last_visible
- first_visible
),
4780 int max_width
= calculator
.GetMaxWidth();
4781 if ( max_width
> 0 )
4782 max_width
+= 2 * PADDING_RIGHTLEFT
;
4784 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4788 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4789 unsigned int WXUNUSED(new_pos
))
4791 // do _not_ reorder m_cols elements here, they should always be in the
4792 // order in which columns were added, we only display the columns in
4794 m_clientArea
->UpdateDisplay();
4797 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4799 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4803 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4806 if ( m_clientArea
->GetCurrentColumn() == column
)
4807 m_clientArea
->ClearCurrentColumn();
4809 OnColumnsCountChanged();
4814 bool wxDataViewCtrl::ClearColumns()
4816 SetExpanderColumn(NULL
);
4818 m_colsBestWidths
.clear();
4820 m_clientArea
->ClearCurrentColumn();
4822 OnColumnsCountChanged();
4827 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4829 m_colsBestWidths
[idx
] = 0;
4833 void wxDataViewCtrl::InvalidateColBestWidths()
4835 m_colsBestWidths
.clear();
4836 m_colsBestWidths
.resize(m_cols
.size());
4840 void wxDataViewCtrl::UpdateColWidths()
4842 if ( !m_headerArea
)
4845 const unsigned len
= m_colsBestWidths
.size();
4846 for ( unsigned i
= 0; i
< len
; i
++ )
4848 if ( m_colsBestWidths
[i
] == 0 )
4849 m_headerArea
->UpdateColumn(i
);
4853 void wxDataViewCtrl::OnInternalIdle()
4855 wxDataViewCtrlBase::OnInternalIdle();
4859 m_colsDirty
= false;
4864 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4866 unsigned int len
= GetColumnCount();
4867 for ( unsigned int i
= 0; i
< len
; i
++ )
4869 wxDataViewColumn
* col
= GetColumnAt(i
);
4877 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4879 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4880 : GetColumn(m_sortingColumnIdx
);
4883 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4885 return GetItemByRow(m_clientArea
->GetCurrentRow());
4888 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4890 const int row
= m_clientArea
->GetRowByItem(item
);
4892 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4893 if ( static_cast<unsigned>(row
) != oldCurrent
)
4895 m_clientArea
->ChangeCurrentRow(row
);
4896 m_clientArea
->RefreshRow(oldCurrent
);
4897 m_clientArea
->RefreshRow(row
);
4901 wxDataViewColumn
*wxDataViewCtrl::GetCurrentColumn() const
4903 return m_clientArea
->GetCurrentColumn();
4906 int wxDataViewCtrl::GetSelectedItemsCount() const
4908 return m_clientArea
->GetSelections().size();
4911 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4914 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4916 const size_t len
= selections
.size();
4917 for ( size_t i
= 0; i
< len
; i
++ )
4919 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4926 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4933 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4935 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4937 wxDataViewItem last_parent
;
4939 int len
= sel
.GetCount();
4940 for( int i
= 0; i
< len
; i
++ )
4942 wxDataViewItem item
= sel
[i
];
4943 wxDataViewItem parent
= GetModel()->GetParent( item
);
4946 if (parent
!= last_parent
)
4947 ExpandAncestors(item
);
4950 last_parent
= parent
;
4951 int row
= m_clientArea
->GetRowByItem( item
);
4953 selection
.Add( static_cast<unsigned int>(row
) );
4956 m_clientArea
->SetSelections( selection
);
4959 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4961 ExpandAncestors( item
);
4963 int row
= m_clientArea
->GetRowByItem( item
);
4966 // Unselect all rows before select another in the single select mode
4967 if (m_clientArea
->IsSingleSel())
4968 m_clientArea
->SelectAllRows(false);
4970 m_clientArea
->SelectRow(row
, true);
4972 // Also set focus to the selected item
4973 m_clientArea
->ChangeCurrentRow( row
);
4977 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4979 int row
= m_clientArea
->GetRowByItem( item
);
4981 m_clientArea
->SelectRow(row
, false);
4984 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4986 int row
= m_clientArea
->GetRowByItem( item
);
4989 return m_clientArea
->IsRowSelected(row
);
4994 void wxDataViewCtrl::SetAlternateRowColour(const wxColour
& colour
)
4996 m_alternateRowColour
= colour
;
4999 void wxDataViewCtrl::SelectAll()
5001 m_clientArea
->SelectAllRows(true);
5004 void wxDataViewCtrl::UnselectAll()
5006 m_clientArea
->SelectAllRows(false);
5009 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
5013 if( row
> (int) m_clientArea
->GetRowCount() )
5014 row
= m_clientArea
->GetRowCount();
5016 int first
= m_clientArea
->GetFirstVisibleRow();
5017 int last
= m_clientArea
->GetLastVisibleRow();
5019 m_clientArea
->ScrollTo( row
, column
);
5020 else if( row
> last
)
5021 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
5023 m_clientArea
->ScrollTo( first
, column
);
5026 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
5028 ExpandAncestors( item
);
5030 m_clientArea
->RecalculateDisplay();
5032 int row
= m_clientArea
->GetRowByItem(item
);
5035 if( column
== NULL
)
5036 EnsureVisible(row
, -1);
5038 EnsureVisible( row
, GetColumnIndex(column
) );
5043 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
5044 wxDataViewColumn
* &column
) const
5046 m_clientArea
->HitTest(point
, item
, column
);
5049 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
5050 const wxDataViewColumn
* column
) const
5052 return m_clientArea
->GetItemRect(item
, column
);
5055 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
5057 return m_clientArea
->GetItemByRow( row
);
5060 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
5062 return m_clientArea
->GetRowByItem( item
);
5065 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
5067 ExpandAncestors( item
);
5069 int row
= m_clientArea
->GetRowByItem( item
);
5071 m_clientArea
->Expand(row
);
5074 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
5076 int row
= m_clientArea
->GetRowByItem( item
);
5078 m_clientArea
->Collapse(row
);
5081 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
5083 int row
= m_clientArea
->GetRowByItem( item
);
5085 return m_clientArea
->IsExpanded(row
);
5089 void wxDataViewCtrl::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
5091 wxCHECK_RET( item
.IsOk(), "invalid item" );
5092 wxCHECK_RET( column
, "no column provided" );
5094 m_clientArea
->StartEditing(item
, column
);
5097 #endif // !wxUSE_GENERICDATAVIEWCTRL
5099 #endif // wxUSE_DATAVIEWCTRL