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();
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();
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()
2581 if ( m_count
== -1 )
2583 m_count
= RecalculateCount();
2589 void wxDataViewMainWindow
::ChangeCurrentRow( unsigned int row
)
2596 void wxDataViewMainWindow
::SelectAllRows( bool on
)
2603 m_selection
.Clear();
2604 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2605 m_selection
.Add( i
);
2610 unsigned int first_visible
= GetFirstVisibleRow();
2611 unsigned int last_visible
= GetLastVisibleRow();
2613 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2615 unsigned int row
= m_selection
[i
];
2616 if ((row
>= first_visible
) && (row
<= last_visible
))
2619 m_selection
.Clear();
2623 void wxDataViewMainWindow
::SelectRow( unsigned int row
, bool on
)
2625 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2629 m_selection
.Add( row
);
2637 m_selection
.Remove( row
);
2643 void wxDataViewMainWindow
::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2647 unsigned int tmp
= from
;
2653 for (i
= from
; i
<= to
; i
++)
2655 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2658 m_selection
.Add( i
);
2663 m_selection
.Remove( i
);
2666 RefreshRows( from
, to
);
2669 void wxDataViewMainWindow
::Select( const wxArrayInt
& aSelections
)
2671 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2673 int n
= aSelections
[i
];
2675 m_selection
.Add( n
);
2680 void wxDataViewMainWindow
::ReverseRowSelection( unsigned int row
)
2682 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2683 m_selection
.Add( row
);
2685 m_selection
.Remove( row
);
2689 bool wxDataViewMainWindow
::IsRowSelected( unsigned int row
)
2691 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2694 void wxDataViewMainWindow
::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2696 wxWindow
*parent
= GetParent();
2697 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2699 le
.SetEventObject(parent
);
2700 le
.SetModel(GetModel());
2703 parent
->ProcessWindowEvent(le
);
2706 void wxDataViewMainWindow
::RefreshRow( unsigned int row
)
2708 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2709 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2711 wxSize client_size
= GetClientSize();
2712 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2713 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2714 if (intersect_rect
.width
> 0)
2715 Refresh( true, &intersect_rect
);
2718 void wxDataViewMainWindow
::RefreshRows( unsigned int from
, unsigned int to
)
2722 unsigned int tmp
= to
;
2727 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2728 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2730 wxSize client_size
= GetClientSize();
2731 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2732 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2733 if (intersect_rect
.width
> 0)
2734 Refresh( true, &intersect_rect
);
2737 void wxDataViewMainWindow
::RefreshRowsAfter( unsigned int firstRow
)
2739 wxSize client_size
= GetClientSize();
2740 int start
= GetLineStart( firstRow
);
2741 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2742 if (start
> client_size
.y
) return;
2744 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2746 Refresh( true, &rect
);
2749 wxRect wxDataViewMainWindow
::GetLineRect( unsigned int row
) const
2753 rect
.y
= GetLineStart( row
);
2754 rect
.width
= GetEndOfLastCol();
2755 rect
.height
= GetLineHeight( row
);
2760 int wxDataViewMainWindow
::GetLineStart( unsigned int row
) const
2762 const wxDataViewModel
*model
= GetModel();
2764 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2766 // TODO make more efficient
2771 for (r
= 0; r
< row
; r
++)
2773 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2774 if (!node
) return start
;
2776 wxDataViewItem item
= node
->GetItem();
2778 unsigned int cols
= GetOwner()->GetColumnCount();
2780 int height
= m_lineHeight
;
2781 for (col
= 0; col
< cols
; col
++)
2783 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2784 if (column
->IsHidden())
2785 continue; // skip it!
2788 model
->IsContainer(item
) &&
2789 !model
->HasContainerColumns(item
))
2790 continue; // skip it!
2792 wxDataViewRenderer
*renderer
=
2793 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2794 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2796 height
= wxMax( height
, renderer
->GetSize().y
);
2806 return row
* m_lineHeight
;
2810 int wxDataViewMainWindow
::GetLineAt( unsigned int y
) const
2812 const wxDataViewModel
*model
= GetModel();
2814 // check for the easy case first
2815 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2816 return y
/ m_lineHeight
;
2818 // TODO make more efficient
2819 unsigned int row
= 0;
2820 unsigned int yy
= 0;
2823 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2826 // not really correct...
2827 return row
+ ((y
-yy
) / m_lineHeight
);
2830 wxDataViewItem item
= node
->GetItem();
2832 unsigned int cols
= GetOwner()->GetColumnCount();
2834 int height
= m_lineHeight
;
2835 for (col
= 0; col
< cols
; col
++)
2837 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2838 if (column
->IsHidden())
2839 continue; // skip it!
2842 model
->IsContainer(item
) &&
2843 !model
->HasContainerColumns(item
))
2844 continue; // skip it!
2846 wxDataViewRenderer
*renderer
=
2847 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2848 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2850 height
= wxMax( height
, renderer
->GetSize().y
);
2861 int wxDataViewMainWindow
::GetLineHeight( unsigned int row
) const
2863 const wxDataViewModel
*model
= GetModel();
2865 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2867 wxASSERT( !IsVirtualList() );
2869 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2870 // wxASSERT( node );
2871 if (!node
) return m_lineHeight
;
2873 wxDataViewItem item
= node
->GetItem();
2875 int height
= m_lineHeight
;
2877 unsigned int cols
= GetOwner()->GetColumnCount();
2879 for (col
= 0; col
< cols
; col
++)
2881 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2882 if (column
->IsHidden())
2883 continue; // skip it!
2886 model
->IsContainer(item
) &&
2887 !model
->HasContainerColumns(item
))
2888 continue; // skip it!
2890 wxDataViewRenderer
*renderer
=
2891 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2892 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2894 height
= wxMax( height
, renderer
->GetSize().y
);
2901 return m_lineHeight
;
2906 class RowToTreeNodeJob
: public DoJob
2909 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2912 this->current
= current
;
2917 virtual int operator() ( wxDataViewTreeNode
* node
)
2920 if( current
== static_cast<int>(row
))
2926 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2928 current
+= node
->GetSubTreeCount();
2929 return DoJob
::SKIP_SUBTREE
;
2935 // If the current node has only leaf children, we can find the
2936 // desired node directly. This can speed up finding the node
2937 // in some cases, and will have a very good effect for list views.
2938 if ( node
->HasChildren() &&
2939 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2941 const int index
= static_cast<int>(row
) - current
- 1;
2942 ret
= node
->GetChildNodes()[index
];
2946 return DoJob
::CONTINUE
;
2950 wxDataViewTreeNode
* GetResult() const
2956 wxDataViewTreeNode
* ret
;
2957 wxDataViewTreeNode
* parent
;
2960 wxDataViewTreeNode
* wxDataViewMainWindow
::GetTreeNodeByRow(unsigned int row
) const
2962 wxASSERT( !IsVirtualList() );
2964 RowToTreeNodeJob
job( row
, -2, m_root
);
2965 Walker( m_root
, job
);
2966 return job
.GetResult();
2969 wxDataViewItem wxDataViewMainWindow
::GetItemByRow(unsigned int row
) const
2971 if (IsVirtualList())
2973 return wxDataViewItem( wxUIntToPtr(row
+1) );
2977 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2978 return node ? node
->GetItem() : wxDataViewItem();
2983 wxDataViewMainWindow
::SendExpanderEvent(wxEventType type
,
2984 const wxDataViewItem
& item
)
2986 wxWindow
*parent
= GetParent();
2987 wxDataViewEvent
le(type
, parent
->GetId());
2989 le
.SetEventObject(parent
);
2990 le
.SetModel(GetModel());
2993 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2996 bool wxDataViewMainWindow
::IsExpanded( unsigned int row
) const
3001 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3005 if (!node
->HasChildren())
3008 return node
->IsOpen();
3011 bool wxDataViewMainWindow
::HasChildren( unsigned int row
) const
3016 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3020 if (!node
->HasChildren())
3026 void wxDataViewMainWindow
::Expand( unsigned int row
)
3031 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3035 if (!node
->HasChildren())
3038 if (!node
->IsOpen())
3040 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
3042 // Vetoed by the event handler.
3048 // build the children of current node
3049 if( node
->GetChildNodes().empty() )
3052 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
3055 // By expanding the node all row indices that are currently in the selection list
3056 // and are greater than our node have become invalid. So we have to correct that now.
3057 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3058 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3060 const unsigned testRow
= m_selection
[i
];
3061 // all rows above us are not affected, so skip them
3065 m_selection
[i
] += rowAdjustment
;
3068 if(m_currentRow
> row
)
3069 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3073 // Send the expanded event
3074 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3078 void wxDataViewMainWindow
::Collapse(unsigned int row
)
3083 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3087 if (!node
->HasChildren())
3092 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3094 // Vetoed by the event handler.
3098 // Find out if there are selected items below the current node.
3099 bool selectCollapsingRow
= false;
3100 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3101 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3102 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3104 const unsigned testRow
= m_selection
[i
];
3105 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3107 selectCollapsingRow
= true;
3108 // get out as soon as we have found a node that is selected
3115 // If the node to be closed has selected items the user won't see those any longer.
3116 // We select the collapsing node in this case.
3117 if(selectCollapsingRow
)
3119 SelectAllRows(false);
3120 ChangeCurrentRow(row
);
3121 SelectRow(row
, true);
3122 SendSelectionChangedEvent(GetItemByRow(row
));
3126 // if there were no selected items below our node we still need to "fix" the
3127 // selection list to adjust for the changing of the row indices.
3128 // We actually do the opposite of what we are doing in Expand().
3129 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3131 const unsigned testRow
= m_selection
[i
];
3132 // all rows above us are not affected, so skip them
3136 m_selection
[i
] -= rowAdjustment
;
3139 // if the "current row" is being collapsed away we change it to the current row ;-)
3140 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3141 ChangeCurrentRow(row
);
3142 else if(m_currentRow
> row
)
3143 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3148 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3152 wxDataViewTreeNode
* wxDataViewMainWindow
::FindNode( const wxDataViewItem
& item
)
3154 const wxDataViewModel
* model
= GetModel();
3161 // Compose the parent-chain for the item we are looking for
3162 wxVector
<wxDataViewItem
> parentChain
;
3163 wxDataViewItem
it( item
);
3166 parentChain
.push_back(it
);
3167 it
= model
->GetParent(it
);
3170 // Find the item along the parent-chain.
3171 // This algorithm is designed to speed up the node-finding method
3172 wxDataViewTreeNode
* node
= m_root
;
3173 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3175 if( node
->HasChildren() )
3177 if( node
->GetChildNodes().empty() )
3179 // Even though the item is a container, it doesn't have any
3180 // child nodes in the control's representation yet. We have
3181 // to realize its subtree now.
3183 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3186 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3189 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3191 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3192 if (currentNode
->GetItem() == parentChain
[iter
])
3194 if (currentNode
->GetItem() == item
)
3214 void wxDataViewMainWindow
::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3215 wxDataViewColumn
* &column
)
3217 wxDataViewColumn
*col
= NULL
;
3218 unsigned int cols
= GetOwner()->GetColumnCount();
3219 unsigned int colnum
= 0;
3221 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3222 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3224 col
= GetOwner()->GetColumnAt(colnum
);
3225 if (col
->IsHidden())
3226 continue; // skip it!
3228 unsigned int w
= col
->GetWidth();
3229 if (x_start
+w
>= (unsigned int)x
)
3236 item
= GetItemByRow( GetLineAt( y
) );
3239 wxRect wxDataViewMainWindow
::GetItemRect( const wxDataViewItem
& item
,
3240 const wxDataViewColumn
* column
)
3245 unsigned int cols
= GetOwner()->GetColumnCount();
3246 // If column is null the loop will compute the combined width of all columns.
3247 // Otherwise, it will compute the x position of the column we are looking for.
3248 for (unsigned int i
= 0; i
< cols
; i
++)
3250 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3255 if (col
->IsHidden())
3256 continue; // skip it!
3258 xpos
+= col
->GetWidth();
3259 width
+= col
->GetWidth();
3264 // If we have a column, we need can get its width directly.
3265 if(column
->IsHidden())
3268 width
= column
->GetWidth();
3273 // If we have no column, we reset the x position back to zero.
3277 // we have to take an expander column into account and compute its indentation
3278 // to get the correct x position where the actual text is
3280 int row
= GetRowByItem(item
);
3282 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3284 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3285 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3286 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3289 wxRect
itemRect( xpos
+ indent
,
3290 GetLineStart( row
),
3292 GetLineHeight( row
) );
3294 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3295 &itemRect
.x
, &itemRect
.y
);
3300 int wxDataViewMainWindow
::RecalculateCount()
3302 if (IsVirtualList())
3304 wxDataViewVirtualListModel
*list_model
=
3305 (wxDataViewVirtualListModel
*) GetModel();
3307 return list_model
->GetCount();
3311 return m_root
->GetSubTreeCount();
3315 class ItemToRowJob
: public DoJob
3318 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3325 // Maybe binary search will help to speed up this process
3326 virtual int operator() ( wxDataViewTreeNode
* node
)
3329 if( node
->GetItem() == item
)
3334 if( node
->GetItem() == *m_iter
)
3337 return DoJob
::CONTINUE
;
3341 ret
+= node
->GetSubTreeCount();
3342 return DoJob
::SKIP_SUBTREE
;
3347 // the row number is begin from zero
3348 int GetResult() const
3352 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3353 wxDataViewItem item
;
3358 int wxDataViewMainWindow
::GetRowByItem(const wxDataViewItem
& item
) const
3360 const wxDataViewModel
* model
= GetModel();
3364 if (IsVirtualList())
3366 return wxPtrToUInt( item
.GetID() ) -1;
3373 // Compose the parent-chain of the item we are looking for
3374 wxVector
<wxDataViewItem
> parentChain
;
3375 wxDataViewItem
it( item
);
3378 parentChain
.push_back(it
);
3379 it
= model
->GetParent(it
);
3382 // add an 'invalid' item to represent our 'invisible' root node
3383 parentChain
.push_back(wxDataViewItem());
3385 // the parent chain was created by adding the deepest parent first.
3386 // so if we want to start at the root node, we have to iterate backwards through the vector
3387 ItemToRowJob
job( item
, parentChain
.rbegin() );
3388 Walker( m_root
, job
);
3389 return job
.GetResult();
3393 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3394 wxDataViewTreeNode
* node
)
3396 if( !model
->IsContainer( item
) )
3399 wxDataViewItemArray children
;
3400 unsigned int num
= model
->GetChildren( item
, children
);
3402 for ( unsigned int index
= 0; index
< num
; index
++ )
3404 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3406 if( model
->IsContainer(children
[index
]) )
3407 n
->SetHasChildren( true );
3409 node
->InsertChild(n
, index
);
3412 wxASSERT( node
->IsOpen() );
3413 node
->ChangeSubTreeCount(+num
);
3416 void wxDataViewMainWindow
::BuildTree(wxDataViewModel
* model
)
3420 if (GetModel()->IsVirtualListModel())
3426 m_root
= wxDataViewTreeNode
::CreateRootNode();
3428 // First we define a invalid item to fetch the top-level elements
3429 wxDataViewItem item
;
3431 BuildTreeHelper( model
, item
, m_root
);
3435 void wxDataViewMainWindow
::DestroyTree()
3437 if (!IsVirtualList())
3445 wxDataViewMainWindow
::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3447 // Edit the current column editable in 'mode'. If no column is focused
3448 // (typically because the user has full row selected), try to find the
3449 // first editable column (this would typically be a checkbox for
3450 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3451 // focus on the checkbox column; or on the only editable text column).
3453 wxDataViewColumn
*candidate
= m_currentCol
;
3456 !IsCellEditableInMode(item
, candidate
, mode
) &&
3457 !m_currentColSetByKeyboard
)
3459 // If current column was set by mouse to something not editable (in
3460 // 'mode') and the user pressed Space/F2 to edit it, treat the
3461 // situation as if there was whole-row focus, because that's what is
3462 // visually indicated and the mouse click could very well be targeted
3463 // on the row rather than on an individual cell.
3465 // But if it was done by keyboard, respect that even if the column
3466 // isn't editable, because focus is visually on that column and editing
3467 // something else would be surprising.
3473 const unsigned cols
= GetOwner()->GetColumnCount();
3474 for ( unsigned i
= 0; i
< cols
; i
++ )
3476 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3477 if ( c
->IsHidden() )
3480 if ( IsCellEditableInMode(item
, c
, mode
) )
3488 // If on container item without columns, only the expander column
3489 // may be directly editable:
3491 GetOwner()->GetExpanderColumn() != candidate
&&
3492 GetModel()->IsContainer(item
) &&
3493 !GetModel()->HasContainerColumns(item
) )
3495 candidate
= GetOwner()->GetExpanderColumn();
3501 if ( !IsCellEditableInMode(item
, candidate
, mode
) )
3507 bool wxDataViewMainWindow
::IsCellEditableInMode(const wxDataViewItem
& item
,
3508 const wxDataViewColumn
*col
,
3509 wxDataViewCellMode mode
) const
3511 if ( col
->GetRenderer()->GetMode() != mode
)
3514 if ( !GetModel()->IsEnabled(item
, col
->GetModelColumn()) )
3520 void wxDataViewMainWindow
::OnCharHook(wxKeyEvent
& event
)
3524 // Handle any keys special for the in-place editor and return without
3525 // calling Skip() below.
3526 switch ( event
.GetKeyCode() )
3529 m_editorRenderer
->CancelEditing();
3533 m_editorRenderer
->FinishEditing();
3541 void wxDataViewMainWindow
::OnChar( wxKeyEvent
&event
)
3543 wxWindow
* const parent
= GetParent();
3545 // propagate the char event upwards
3546 wxKeyEvent
eventForParent(event
);
3547 eventForParent
.SetEventObject(parent
);
3548 if ( parent
->ProcessWindowEvent(eventForParent
) )
3551 if ( parent
->HandleAsNavigationKey(event
) )
3554 // no item -> nothing to do
3555 if (!HasCurrentRow())
3561 // don't use m_linesPerPage directly as it might not be computed yet
3562 const int pageSize
= GetCountPerPage();
3563 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3565 switch ( event
.GetKeyCode() )
3568 if ( event
.HasModifiers() )
3575 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3576 // it. Only if that event is not handled do we activate column renderer (which
3577 // is normally done by Space) or even inline editing.
3579 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3581 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3584 le
.SetEventObject(parent
);
3585 le
.SetModel(GetModel());
3587 if ( parent
->ProcessWindowEvent(le
) )
3589 // else: fall through to WXK_SPACE handling
3593 if ( event
.HasModifiers() )
3600 // Space toggles activatable items or -- if not activatable --
3601 // starts inline editing (this is normally done using F2 on
3602 // Windows, but Space is common everywhere else, so use it too
3603 // for greater cross-platform compatibility).
3605 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3607 // Activate the current activatable column. If not column is focused (typically
3608 // because the user has full row selected), try to find the first activatable
3609 // column (this would typically be a checkbox and we don't want to force the user
3610 // to set focus on the checkbox column).
3611 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3613 if ( activatableCol
)
3615 const unsigned colIdx
= activatableCol
->GetModelColumn();
3616 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3618 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3619 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3620 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3624 // else: fall through to WXK_F2 handling
3628 if ( event
.HasModifiers() )
3635 if( !m_selection
.empty() )
3637 // Mimic Windows 7 behavior: edit the item that has focus
3638 // if it is selected and the first selected item if focus
3639 // is out of selection.
3641 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3644 sel
= m_selection
[0];
3647 const wxDataViewItem item
= GetItemByRow(sel
);
3649 // Edit the current column. If no column is focused
3650 // (typically because the user has full row selected), try
3651 // to find the first editable column.
3652 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3655 GetOwner()->EditItem(item
, editableCol
);
3661 if ( m_currentRow
> 0 )
3662 OnVerticalNavigation( m_currentRow
- 1, event
);
3666 if ( m_currentRow
+ 1 < GetRowCount() )
3667 OnVerticalNavigation( m_currentRow
+ 1, event
);
3669 // Add the process for tree expanding/collapsing
3681 OnVerticalNavigation( GetRowCount() - 1, event
);
3686 OnVerticalNavigation( 0, event
);
3691 int steps
= pageSize
- 1;
3692 int index
= m_currentRow
- steps
;
3696 OnVerticalNavigation( index
, event
);
3702 int steps
= pageSize
- 1;
3703 unsigned int index
= m_currentRow
+ steps
;
3704 unsigned int count
= GetRowCount();
3705 if ( index
>= count
)
3708 OnVerticalNavigation( index
, event
);
3717 void wxDataViewMainWindow
::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3719 wxCHECK_RET( newCurrent
< GetRowCount(),
3720 wxT("invalid item index in OnVerticalNavigation()") );
3722 // if there is no selection, we cannot move it anywhere
3723 if (!HasCurrentRow())
3726 unsigned int oldCurrent
= m_currentRow
;
3728 // in single selection we just ignore Shift as we can't select several
3730 if ( event
.ShiftDown() && !IsSingleSel() )
3732 RefreshRow( oldCurrent
);
3734 ChangeCurrentRow( newCurrent
);
3736 // select all the items between the old and the new one
3737 if ( oldCurrent
> newCurrent
)
3739 newCurrent
= oldCurrent
;
3740 oldCurrent
= m_currentRow
;
3743 SelectRows( oldCurrent
, newCurrent
, true );
3744 if (oldCurrent
!=newCurrent
)
3745 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3749 RefreshRow( oldCurrent
);
3751 // all previously selected items are unselected unless ctrl is held
3752 if ( !event
.ControlDown() )
3753 SelectAllRows(false);
3755 ChangeCurrentRow( newCurrent
);
3757 if ( !event
.ControlDown() )
3759 SelectRow( m_currentRow
, true );
3760 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3763 RefreshRow( m_currentRow
);
3766 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3769 void wxDataViewMainWindow
::OnLeftKey()
3773 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3777 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3779 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3782 // Because TryAdvanceCurrentColumn() return false, we are at the first
3783 // column or using whole-row selection. In this situation, we can use
3784 // the standard TreeView handling of the left key.
3785 if (node
->HasChildren() && node
->IsOpen())
3787 Collapse(m_currentRow
);
3791 // if the node is already closed, we move the selection to its parent
3792 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3796 int parent
= GetRowByItem( parent_node
->GetItem() );
3799 unsigned int row
= m_currentRow
;
3800 SelectRow( row
, false);
3801 SelectRow( parent
, true );
3802 ChangeCurrentRow( parent
);
3803 GetOwner()->EnsureVisible( parent
, -1 );
3804 SendSelectionChangedEvent( parent_node
->GetItem() );
3811 void wxDataViewMainWindow
::OnRightKey()
3815 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3819 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3821 if ( node
->HasChildren() )
3823 if ( !node
->IsOpen() )
3825 Expand( m_currentRow
);
3829 // if the node is already open, we move the selection to the first child
3830 unsigned int row
= m_currentRow
;
3831 SelectRow( row
, false );
3832 SelectRow( row
+ 1, true );
3833 ChangeCurrentRow( row
+ 1 );
3834 GetOwner()->EnsureVisible( row
+ 1, -1 );
3835 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3840 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3845 bool wxDataViewMainWindow
::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3847 if ( GetOwner()->GetColumnCount() == 0 )
3850 if ( !m_useCellFocus
)
3855 // navigation shouldn't work in branch nodes without other columns:
3856 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3860 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3864 m_currentCol
= GetOwner()->GetColumnAt(1);
3865 m_currentColSetByKeyboard
= true;
3866 RefreshRow(m_currentRow
);
3873 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward ?
+1 : -1);
3875 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3878 GetOwner()->EnsureVisible(m_currentRow
, idx
);
3882 // We are going to the left of the second column. Reset to whole-row
3883 // focus (which means first column would be edited).
3884 m_currentCol
= NULL
;
3885 RefreshRow(m_currentRow
);
3889 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3890 m_currentColSetByKeyboard
= true;
3891 RefreshRow(m_currentRow
);
3895 void wxDataViewMainWindow
::OnMouse( wxMouseEvent
&event
)
3897 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3899 // let the base handle mouse wheel events.
3904 // set the focus to ourself if any of the mouse buttons are pressed
3905 if(event
.ButtonDown() && !HasFocus())
3908 int x
= event
.GetX();
3909 int y
= event
.GetY();
3910 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3911 wxDataViewColumn
*col
= NULL
;
3914 unsigned int cols
= GetOwner()->GetColumnCount();
3916 for (i
= 0; i
< cols
; i
++)
3918 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3920 continue; // skip it!
3922 if (x
< xpos
+ c
->GetWidth())
3927 xpos
+= c
->GetWidth();
3930 wxDataViewModel
* const model
= GetModel();
3932 const unsigned int current
= GetLineAt( y
);
3933 const wxDataViewItem item
= GetItemByRow(current
);
3935 // Handle right clicking here, before everything else as context menu
3936 // events should be sent even when we click outside of any item, unlike all
3938 if (event
.RightUp())
3940 wxWindow
*parent
= GetParent();
3941 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3942 le
.SetEventObject(parent
);
3945 if ( item
.IsOk() && col
)
3948 le
.SetColumn( col
->GetModelColumn() );
3949 le
.SetDataViewColumn( col
);
3952 model
->GetValue( value
, item
, col
->GetModelColumn() );
3956 parent
->ProcessWindowEvent(le
);
3966 wxDataViewRenderer
*cell
= col
->GetRenderer();
3967 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3969 // Unselect all if below the last row ?
3974 wxDataViewColumn
* const
3975 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3977 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3978 // button) and also determine the offset of the real cell start, skipping
3979 // the indentation and the expander itself.
3980 bool hoverOverExpander
= false;
3982 if ((!IsList()) && (expander
== col
))
3984 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3986 int indent
= node
->GetIndentLevel();
3987 itemOffset
= GetOwner()->GetIndent()*indent
;
3989 if ( node
->HasChildren() )
3991 // we make the rectangle we are looking in a bit bigger than the actual
3992 // visual expander so the user can hit that little thing reliably
3993 wxRect
rect(itemOffset
,
3994 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3995 m_lineHeight
, m_lineHeight
);
3997 if( rect
.Contains(x
, y
) )
3999 // So the mouse is over the expander
4000 hoverOverExpander
= true;
4001 if (m_underMouse
&& m_underMouse
!= node
)
4003 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4004 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4006 if (m_underMouse
!= node
)
4008 // wxLogMessage("Do the row: %d", current);
4009 RefreshRow(current
);
4011 m_underMouse
= node
;
4015 // Account for the expander as well, even if this item doesn't have it,
4016 // its parent does so it still counts for the offset.
4017 itemOffset
+= m_lineHeight
;
4019 if (!hoverOverExpander
)
4021 if (m_underMouse
!= NULL
)
4023 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4024 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4025 m_underMouse
= NULL
;
4029 #if wxUSE_DRAG_AND_DROP
4030 if (event
.Dragging())
4032 if (m_dragCount
== 0)
4034 // we have to report the raw, physical coords as we want to be
4035 // able to call HitTest(event.m_pointDrag) from the user code to
4036 // get the item being dragged
4037 m_dragStart
= event
.GetPosition();
4042 if (m_dragCount
!= 3)
4045 if (event
.LeftIsDown())
4047 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
4048 &m_dragStart
.x
, &m_dragStart
.y
);
4049 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
4050 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
4052 // Notify cell about drag
4053 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
4054 event
.SetEventObject( m_owner
);
4055 event
.SetItem( itemDragged
);
4056 event
.SetModel( model
);
4057 if (!m_owner
->HandleWindowEvent( event
))
4060 if (!event
.IsAllowed())
4063 wxDataObject
*obj
= event
.GetDataObject();
4067 wxDataViewDropSource
drag( this, drag_item_row
);
4068 drag
.SetData( *obj
);
4069 /* wxDragResult res = */ drag
.DoDragDrop();
4078 #endif // wxUSE_DRAG_AND_DROP
4080 bool simulateClick
= false;
4082 if (event
.ButtonDClick())
4084 m_renameTimer
->Stop();
4085 m_lastOnSame
= false;
4088 bool ignore_other_columns
=
4089 ((expander
!= col
) &&
4090 (model
->IsContainer(item
)) &&
4091 (!model
->HasContainerColumns(item
)));
4093 if (event
.LeftDClick())
4095 if(hoverOverExpander
)
4097 // a double click on the expander will be converted into a "simulated" normal click
4098 simulateClick
= true;
4100 else if ( current
== m_lineLastClicked
)
4102 wxWindow
*parent
= GetParent();
4103 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4105 le
.SetColumn( col
->GetModelColumn() );
4106 le
.SetDataViewColumn( col
);
4107 le
.SetEventObject(parent
);
4108 le
.SetModel(GetModel());
4110 parent
->ProcessWindowEvent(le
);
4115 // The first click was on another item, so don't interpret this as
4116 // a double click, but as a simple click instead
4117 simulateClick
= true;
4121 if (event
.LeftUp() && !hoverOverExpander
)
4123 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4125 // select single line
4126 SelectAllRows( false );
4127 SelectRow( m_lineSelectSingleOnUp
, true );
4128 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4131 // If the user click the expander, we do not do editing even if the column
4132 // with expander are editable
4133 if (m_lastOnSame
&& !ignore_other_columns
)
4135 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4136 IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
4138 m_renameTimer
->Start( 100, true );
4142 m_lastOnSame
= false;
4143 m_lineSelectSingleOnUp
= (unsigned int)-1;
4145 else if(!event
.LeftUp())
4147 // This is necessary, because after a DnD operation in
4148 // from and to ourself, the up event is swallowed by the
4149 // DnD code. So on next non-up event (which means here and
4150 // now) m_lineSelectSingleOnUp should be reset.
4151 m_lineSelectSingleOnUp
= (unsigned int)-1;
4154 if (event
.RightDown())
4156 m_lineBeforeLastClicked
= m_lineLastClicked
;
4157 m_lineLastClicked
= current
;
4159 // If the item is already selected, do not update the selection.
4160 // Multi-selections should not be cleared if a selected item is clicked.
4161 if (!IsRowSelected(current
))
4163 SelectAllRows(false);
4164 const unsigned oldCurrent
= m_currentRow
;
4165 ChangeCurrentRow(current
);
4166 SelectRow(m_currentRow
,true);
4167 RefreshRow(oldCurrent
);
4168 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4171 else if (event
.MiddleDown())
4175 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4177 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4179 // hoverOverExpander being true tells us that our node must be
4180 // valid and have children.
4181 // So we don't need any extra checks.
4182 if( node
->IsOpen() )
4187 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4189 m_lineBeforeLastClicked
= m_lineLastClicked
;
4190 m_lineLastClicked
= current
;
4192 unsigned int oldCurrentRow
= m_currentRow
;
4193 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4195 bool cmdModifierDown
= event
.CmdDown();
4196 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4198 if ( IsSingleSel() || !IsRowSelected(current
) )
4200 SelectAllRows( false );
4201 ChangeCurrentRow(current
);
4202 SelectRow(m_currentRow
,true);
4203 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4205 else // multi sel & current is highlighted & no mod keys
4207 m_lineSelectSingleOnUp
= current
;
4208 ChangeCurrentRow(current
); // change focus
4211 else // multi sel & either ctrl or shift is down
4213 if (cmdModifierDown
)
4215 ChangeCurrentRow(current
);
4216 ReverseRowSelection(m_currentRow
);
4217 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4219 else if (event
.ShiftDown())
4221 ChangeCurrentRow(current
);
4223 unsigned int lineFrom
= oldCurrentRow
,
4226 if ( lineTo
< lineFrom
)
4229 lineFrom
= m_currentRow
;
4232 SelectRows(lineFrom
, lineTo
, true);
4233 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4235 else // !ctrl, !shift
4237 // test in the enclosing if should make it impossible
4238 wxFAIL_MSG( wxT("how did we get here?") );
4242 if (m_currentRow
!= oldCurrentRow
)
4243 RefreshRow( oldCurrentRow
);
4245 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4247 // Update selection here...
4249 m_currentColSetByKeyboard
= false;
4251 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4252 (current
== oldCurrentRow
)) && oldWasSelected
;
4254 // Call ActivateCell() after everything else as under GTK+
4255 if ( IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_ACTIVATABLE
) )
4257 // notify cell about click
4258 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4260 wxRect
cell_rect( xpos
+ itemOffset
,
4261 GetLineStart( current
),
4262 col
->GetWidth() - itemOffset
,
4263 GetLineHeight( current
) );
4265 // Report position relative to the cell's custom area, i.e.
4266 // no the entire space as given by the control but the one
4267 // used by the renderer after calculation of alignment etc.
4269 // adjust the rectangle ourselves to account for the alignment
4270 wxRect rectItem
= cell_rect
;
4271 const int align
= cell
->GetAlignment();
4272 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4274 const wxSize size
= cell
->GetSize();
4276 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4278 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4279 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4280 else if ( align
& wxALIGN_RIGHT
)
4281 rectItem
.x
+= cell_rect
.width
- size
.x
;
4282 // else: wxALIGN_LEFT is the default
4285 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4287 if ( align
& wxALIGN_CENTER_VERTICAL
)
4288 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4289 else if ( align
& wxALIGN_BOTTOM
)
4290 rectItem
.y
+= cell_rect
.height
- size
.y
;
4291 // else: wxALIGN_TOP is the default
4295 wxMouseEvent
event2(event
);
4296 event2
.m_x
-= rectItem
.x
;
4297 event2
.m_y
-= rectItem
.y
;
4298 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4300 /* ignore ret */ cell
->WXActivateCell
4305 col
->GetModelColumn(),
4312 void wxDataViewMainWindow
::OnSetFocus( wxFocusEvent
&event
)
4316 if (HasCurrentRow())
4322 void wxDataViewMainWindow
::OnKillFocus( wxFocusEvent
&event
)
4326 if (HasCurrentRow())
4332 void wxDataViewMainWindow
::OnColumnsCountChanged()
4334 int editableCount
= 0;
4336 const unsigned cols
= GetOwner()->GetColumnCount();
4337 for ( unsigned i
= 0; i
< cols
; i
++ )
4339 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4340 if ( c
->IsHidden() )
4342 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4346 m_useCellFocus
= (editableCount
> 1);
4351 //-----------------------------------------------------------------------------
4353 //-----------------------------------------------------------------------------
4355 WX_DEFINE_LIST(wxDataViewColumnList
)
4357 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4358 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4359 EVT_SIZE(wxDataViewCtrl
::OnSize
)
4362 wxDataViewCtrl
::~wxDataViewCtrl()
4365 GetModel()->RemoveNotifier( m_notifier
);
4368 m_colsBestWidths
.clear();
4371 void wxDataViewCtrl
::Init()
4373 m_cols
.DeleteContents(true);
4376 // No sorting column at start
4377 m_sortingColumnIdx
= wxNOT_FOUND
;
4379 m_headerArea
= NULL
;
4381 m_colsDirty
= false;
4384 bool wxDataViewCtrl
::Create(wxWindow
*parent
,
4389 const wxValidator
& validator
,
4390 const wxString
& name
)
4392 // if ( (style & wxBORDER_MASK) == 0)
4393 // style |= wxBORDER_SUNKEN;
4397 if (!wxControl
::Create( parent
, id
, pos
, size
,
4398 style
| wxScrolledWindowStyle
, validator
, name
))
4401 SetInitialSize(size
);
4404 MacSetClipChildren( true );
4407 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4409 // We use the cursor keys for moving the selection, not scrolling, so call
4410 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4411 // keyboard events forwarded to us from wxListMainWindow.
4412 DisableKeyboardScrolling();
4414 if (HasFlag(wxDV_NO_HEADER
))
4415 m_headerArea
= NULL
;
4417 m_headerArea
= new wxDataViewHeaderWindow(this);
4419 SetTargetWindow( m_clientArea
);
4421 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4423 sizer
->Add( m_headerArea
, 0, wxGROW
);
4424 sizer
->Add( m_clientArea
, 1, wxGROW
);
4430 wxBorder wxDataViewCtrl
::GetDefaultBorder() const
4432 return wxBORDER_THEME
;
4436 WXLRESULT wxDataViewCtrl
::MSWWindowProc(WXUINT nMsg
,
4440 WXLRESULT rc
= wxDataViewCtrlBase
::MSWWindowProc(nMsg
, wParam
, lParam
);
4443 // we need to process arrows ourselves for scrolling
4444 if ( nMsg
== WM_GETDLGCODE
)
4446 rc
|= DLGC_WANTARROWS
;
4454 wxSize wxDataViewCtrl
::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4456 wxSize newsize
= size
;
4457 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4458 newsize
.y
-= m_headerArea
->GetSize().y
;
4463 void wxDataViewCtrl
::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4465 // We need to override OnSize so that our scrolled
4466 // window a) does call Layout() to use sizers for
4467 // positioning the controls but b) does not query
4468 // the sizer for their size and use that for setting
4469 // the scrollable area as set that ourselves by
4470 // calling SetScrollbar() further down.
4476 // We must redraw the headers if their height changed. Normally this
4477 // shouldn't happen as the control shouldn't let itself be resized beneath
4478 // its minimal height but avoid the display artefacts that appear if it
4479 // does happen, e.g. because there is really not enough vertical space.
4480 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4481 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4483 m_headerArea
->Refresh();
4487 void wxDataViewCtrl
::SetFocus()
4490 m_clientArea
->SetFocus();
4493 bool wxDataViewCtrl
::AssociateModel( wxDataViewModel
*model
)
4495 if (!wxDataViewCtrlBase
::AssociateModel( model
))
4498 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4500 model
->AddNotifier( m_notifier
);
4502 m_clientArea
->DestroyTree();
4504 m_clientArea
->BuildTree(model
);
4506 m_clientArea
->UpdateDisplay();
4511 #if wxUSE_DRAG_AND_DROP
4513 bool wxDataViewCtrl
::EnableDragSource( const wxDataFormat
&format
)
4515 return m_clientArea
->EnableDragSource( format
);
4518 bool wxDataViewCtrl
::EnableDropTarget( const wxDataFormat
&format
)
4520 return m_clientArea
->EnableDropTarget( format
);
4523 #endif // wxUSE_DRAG_AND_DROP
4525 bool wxDataViewCtrl
::AppendColumn( wxDataViewColumn
*col
)
4527 if (!wxDataViewCtrlBase
::AppendColumn(col
))
4530 m_cols
.Append( col
);
4531 m_colsBestWidths
.push_back(0);
4532 OnColumnsCountChanged();
4536 bool wxDataViewCtrl
::PrependColumn( wxDataViewColumn
*col
)
4538 if (!wxDataViewCtrlBase
::PrependColumn(col
))
4541 m_cols
.Insert( col
);
4542 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4543 OnColumnsCountChanged();
4547 bool wxDataViewCtrl
::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4549 if (!wxDataViewCtrlBase
::InsertColumn(pos
,col
))
4552 m_cols
.Insert( pos
, col
);
4553 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4554 OnColumnsCountChanged();
4558 void wxDataViewCtrl
::OnColumnChange(unsigned int idx
)
4561 m_headerArea
->UpdateColumn(idx
);
4563 m_clientArea
->UpdateDisplay();
4566 void wxDataViewCtrl
::OnColumnsCountChanged()
4569 m_headerArea
->SetColumnCount(GetColumnCount());
4571 m_clientArea
->OnColumnsCountChanged();
4574 void wxDataViewCtrl
::DoSetExpanderColumn()
4576 m_clientArea
->UpdateDisplay();
4579 void wxDataViewCtrl
::DoSetIndent()
4581 m_clientArea
->UpdateDisplay();
4584 unsigned int wxDataViewCtrl
::GetColumnCount() const
4586 return m_cols
.GetCount();
4589 bool wxDataViewCtrl
::SetRowHeight( int lineHeight
)
4591 if ( !m_clientArea
)
4594 m_clientArea
->SetRowHeight(lineHeight
);
4599 wxDataViewColumn
* wxDataViewCtrl
::GetColumn( unsigned int idx
) const
4604 wxDataViewColumn
*wxDataViewCtrl
::GetColumnAt(unsigned int pos
) const
4606 // columns can't be reordered if there is no header window which allows
4608 const unsigned idx
= m_headerArea ? m_headerArea
->GetColumnsOrder()[pos
]
4611 return GetColumn(idx
);
4614 int wxDataViewCtrl
::GetColumnIndex(const wxDataViewColumn
*column
) const
4616 const unsigned count
= m_cols
.size();
4617 for ( unsigned n
= 0; n
< count
; n
++ )
4619 if ( m_cols
[n
] == column
)
4626 unsigned int wxDataViewCtrl
::GetBestColumnWidth(int idx
) const
4628 if ( m_colsBestWidths
[idx
] != 0 )
4629 return m_colsBestWidths
[idx
];
4631 const int count
= m_clientArea
->GetRowCount();
4632 wxDataViewColumn
*column
= GetColumn(idx
);
4633 wxDataViewRenderer
*renderer
=
4634 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4636 class MaxWidthCalculator
4639 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4640 wxDataViewMainWindow
*clientArea
,
4641 wxDataViewRenderer
*renderer
,
4642 const wxDataViewModel
*model
,
4647 m_clientArea(clientArea
),
4648 m_renderer(renderer
),
4651 m_expanderSize(expanderSize
)
4655 !clientArea
->IsList() &&
4657 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4660 void UpdateWithWidth(int width
)
4662 m_width
= wxMax(m_width
, width
);
4665 void UpdateWithRow(int row
)
4668 wxDataViewItem item
;
4670 if ( m_isExpanderCol
)
4672 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4673 item
= node
->GetItem();
4674 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4678 item
= m_clientArea
->GetItemByRow(row
);
4681 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4682 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4685 int GetMaxWidth() const { return m_width
; }
4689 const wxDataViewCtrl
*m_dvc
;
4690 wxDataViewMainWindow
*m_clientArea
;
4691 wxDataViewRenderer
*m_renderer
;
4692 const wxDataViewModel
*m_model
;
4694 bool m_isExpanderCol
;
4698 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4699 GetModel(), column
->GetModelColumn(),
4700 m_clientArea
->GetRowHeight());
4703 calculator
.UpdateWithWidth(m_headerArea
->GetColumnTitleWidth(*column
));
4705 // The code below deserves some explanation. For very large controls, we
4706 // simply can't afford to calculate sizes for all items, it takes too
4707 // long. So the best we can do is to check the first and the last N/2
4708 // items in the control for some sufficiently large N and calculate best
4709 // sizes from that. That can result in the calculated best width being too
4710 // small for some outliers, but it's better to get slightly imperfect
4711 // result than to wait several seconds after every update. To avoid highly
4712 // visible miscalculations, we also include all currently visible items
4713 // no matter what. Finally, the value of N is determined dynamically by
4714 // measuring how much time we spent on the determining item widths so far.
4717 int top_part_end
= count
;
4718 static const long CALC_TIMEOUT
= 20/*ms*/;
4719 // don't call wxStopWatch::Time() too often
4720 static const unsigned CALC_CHECK_FREQ
= 100;
4723 // use some hard-coded limit, that's the best we can do without timer
4724 int top_part_end
= wxMin(500, count
);
4725 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4729 for ( row
= 0; row
< top_part_end
; row
++ )
4732 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4733 timer
.Time() > CALC_TIMEOUT
)
4735 #endif // wxUSE_STOPWATCH
4736 calculator
.UpdateWithRow(row
);
4739 // row is the first unmeasured item now; that's our value of N/2
4745 // add bottom N/2 items now:
4746 const int bottom_part_start
= wxMax(row
, count
- row
);
4747 for ( row
= bottom_part_start
; row
< count
; row
++ )
4749 calculator
.UpdateWithRow(row
);
4752 // finally, include currently visible items in the calculation:
4753 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4754 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4755 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4757 first_visible
= wxMax(first_visible
, top_part_end
);
4758 last_visible
= wxMin(bottom_part_start
, last_visible
);
4760 for ( row
= first_visible
; row
< last_visible
; row
++ )
4762 calculator
.UpdateWithRow(row
);
4765 wxLogTrace("dataview",
4766 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4768 count
- bottom_part_start
,
4769 wxMax(0, last_visible
- first_visible
),
4773 int max_width
= calculator
.GetMaxWidth();
4774 if ( max_width
> 0 )
4775 max_width
+= 2 * PADDING_RIGHTLEFT
;
4777 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4781 void wxDataViewCtrl
::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4782 unsigned int WXUNUSED(new_pos
))
4784 // do _not_ reorder m_cols elements here, they should always be in the
4785 // order in which columns were added, we only display the columns in
4787 m_clientArea
->UpdateDisplay();
4790 bool wxDataViewCtrl
::DeleteColumn( wxDataViewColumn
*column
)
4792 wxDataViewColumnList
::compatibility_iterator ret
= m_cols
.Find( column
);
4796 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4799 if ( m_clientArea
->GetCurrentColumn() == column
)
4800 m_clientArea
->ClearCurrentColumn();
4802 OnColumnsCountChanged();
4807 bool wxDataViewCtrl
::ClearColumns()
4809 SetExpanderColumn(NULL
);
4811 m_colsBestWidths
.clear();
4813 m_clientArea
->ClearCurrentColumn();
4815 OnColumnsCountChanged();
4820 void wxDataViewCtrl
::InvalidateColBestWidth(int idx
)
4822 m_colsBestWidths
[idx
] = 0;
4826 void wxDataViewCtrl
::InvalidateColBestWidths()
4828 m_colsBestWidths
.clear();
4829 m_colsBestWidths
.resize(m_cols
.size());
4833 void wxDataViewCtrl
::UpdateColWidths()
4835 if ( !m_headerArea
)
4838 const unsigned len
= m_colsBestWidths
.size();
4839 for ( unsigned i
= 0; i
< len
; i
++ )
4841 if ( m_colsBestWidths
[i
] == 0 )
4842 m_headerArea
->UpdateColumn(i
);
4846 void wxDataViewCtrl
::OnInternalIdle()
4848 wxDataViewCtrlBase
::OnInternalIdle();
4852 m_colsDirty
= false;
4857 int wxDataViewCtrl
::GetColumnPosition( const wxDataViewColumn
*column
) const
4859 unsigned int len
= GetColumnCount();
4860 for ( unsigned int i
= 0; i
< len
; i
++ )
4862 wxDataViewColumn
* col
= GetColumnAt(i
);
4870 wxDataViewColumn
*wxDataViewCtrl
::GetSortingColumn() const
4872 return m_sortingColumnIdx
== wxNOT_FOUND ? NULL
4873 : GetColumn(m_sortingColumnIdx
);
4876 wxDataViewItem wxDataViewCtrl
::DoGetCurrentItem() const
4878 return GetItemByRow(m_clientArea
->GetCurrentRow());
4881 void wxDataViewCtrl
::DoSetCurrentItem(const wxDataViewItem
& item
)
4883 const int row
= m_clientArea
->GetRowByItem(item
);
4885 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4886 if ( static_cast<unsigned>(row
) != oldCurrent
)
4888 m_clientArea
->ChangeCurrentRow(row
);
4889 m_clientArea
->RefreshRow(oldCurrent
);
4890 m_clientArea
->RefreshRow(row
);
4894 wxDataViewColumn
*wxDataViewCtrl
::GetCurrentColumn() const
4896 return m_clientArea
->GetCurrentColumn();
4899 int wxDataViewCtrl
::GetSelectedItemsCount() const
4901 return m_clientArea
->GetSelections().size();
4904 int wxDataViewCtrl
::GetSelections( wxDataViewItemArray
& sel
) const
4907 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4909 const size_t len
= selections
.size();
4910 for ( size_t i
= 0; i
< len
; i
++ )
4912 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4919 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4926 void wxDataViewCtrl
::SetSelections( const wxDataViewItemArray
& sel
)
4928 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4930 wxDataViewItem last_parent
;
4932 int len
= sel
.GetCount();
4933 for( int i
= 0; i
< len
; i
++ )
4935 wxDataViewItem item
= sel
[i
];
4936 wxDataViewItem parent
= GetModel()->GetParent( item
);
4939 if (parent
!= last_parent
)
4940 ExpandAncestors(item
);
4943 last_parent
= parent
;
4944 int row
= m_clientArea
->GetRowByItem( item
);
4946 selection
.Add( static_cast<unsigned int>(row
) );
4949 m_clientArea
->SetSelections( selection
);
4952 void wxDataViewCtrl
::Select( const wxDataViewItem
& item
)
4954 ExpandAncestors( item
);
4956 int row
= m_clientArea
->GetRowByItem( item
);
4959 // Unselect all rows before select another in the single select mode
4960 if (m_clientArea
->IsSingleSel())
4961 m_clientArea
->SelectAllRows(false);
4963 m_clientArea
->SelectRow(row
, true);
4965 // Also set focus to the selected item
4966 m_clientArea
->ChangeCurrentRow( row
);
4970 void wxDataViewCtrl
::Unselect( const wxDataViewItem
& item
)
4972 int row
= m_clientArea
->GetRowByItem( item
);
4974 m_clientArea
->SelectRow(row
, false);
4977 bool wxDataViewCtrl
::IsSelected( const wxDataViewItem
& item
) const
4979 int row
= m_clientArea
->GetRowByItem( item
);
4982 return m_clientArea
->IsRowSelected(row
);
4987 void wxDataViewCtrl
::SetAlternateRowColour(const wxColour
& colour
)
4989 m_alternateRowColour
= colour
;
4992 void wxDataViewCtrl
::SelectAll()
4994 m_clientArea
->SelectAllRows(true);
4997 void wxDataViewCtrl
::UnselectAll()
4999 m_clientArea
->SelectAllRows(false);
5002 void wxDataViewCtrl
::EnsureVisible( int row
, int column
)
5006 if( row
> (int) m_clientArea
->GetRowCount() )
5007 row
= m_clientArea
->GetRowCount();
5009 int first
= m_clientArea
->GetFirstVisibleRow();
5010 int last
= m_clientArea
->GetLastVisibleRow();
5012 m_clientArea
->ScrollTo( row
, column
);
5013 else if( row
> last
)
5014 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
5016 m_clientArea
->ScrollTo( first
, column
);
5019 void wxDataViewCtrl
::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
5021 ExpandAncestors( item
);
5023 m_clientArea
->RecalculateDisplay();
5025 int row
= m_clientArea
->GetRowByItem(item
);
5028 if( column
== NULL
)
5029 EnsureVisible(row
, -1);
5031 EnsureVisible( row
, GetColumnIndex(column
) );
5036 void wxDataViewCtrl
::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
5037 wxDataViewColumn
* &column
) const
5039 m_clientArea
->HitTest(point
, item
, column
);
5042 wxRect wxDataViewCtrl
::GetItemRect( const wxDataViewItem
& item
,
5043 const wxDataViewColumn
* column
) const
5045 return m_clientArea
->GetItemRect(item
, column
);
5048 wxDataViewItem wxDataViewCtrl
::GetItemByRow( unsigned int row
) const
5050 return m_clientArea
->GetItemByRow( row
);
5053 int wxDataViewCtrl
::GetRowByItem( const wxDataViewItem
& item
) const
5055 return m_clientArea
->GetRowByItem( item
);
5058 void wxDataViewCtrl
::Expand( const wxDataViewItem
& item
)
5060 ExpandAncestors( item
);
5062 int row
= m_clientArea
->GetRowByItem( item
);
5064 m_clientArea
->Expand(row
);
5067 void wxDataViewCtrl
::Collapse( const wxDataViewItem
& item
)
5069 int row
= m_clientArea
->GetRowByItem( item
);
5071 m_clientArea
->Collapse(row
);
5074 bool wxDataViewCtrl
::IsExpanded( const wxDataViewItem
& item
) const
5076 int row
= m_clientArea
->GetRowByItem( item
);
5078 return m_clientArea
->IsExpanded(row
);
5082 void wxDataViewCtrl
::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
5084 wxCHECK_RET( item
.IsOk(), "invalid item" );
5085 wxCHECK_RET( column
, "no column provided" );
5087 m_clientArea
->StartEditing(item
, column
);
5090 #endif // !wxUSE_GENERICDATAVIEWCTRL
5092 #endif // wxUSE_DATAVIEWCTRL