1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
47 #include "wx/imaglist.h"
48 #include "wx/headerctrl.h"
50 #include "wx/stopwatch.h"
51 #include "wx/weakref.h"
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 class wxDataViewColumn
;
58 class wxDataViewHeaderWindow
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static const int SCROLL_UNIT_X
= 15;
67 // the cell padding on the left/right
68 static const int PADDING_RIGHTLEFT
= 3;
70 // the expander space margin
71 static const int EXPANDER_MARGIN
= 4;
74 static const int EXPANDER_OFFSET
= 4;
76 static const int EXPANDER_OFFSET
= 1;
79 // Below is the compare stuff.
80 // For the generic implementation, both the leaf nodes and the nodes are sorted for
81 // fast search when needed
82 static wxDataViewModel
* g_model
;
83 static int g_column
= -2;
84 static bool g_asending
= true;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
93 // Return the expander column or, if it is not set, the first column and also
94 // set it as the expander one for the future.
95 wxDataViewColumn
* GetExpanderColumnOrFirstOne(wxDataViewCtrl
* dataview
)
97 wxDataViewColumn
* expander
= dataview
->GetExpanderColumn();
100 // TODO-RTL: last column for RTL support
101 expander
= dataview
->GetColumnAt( 0 );
102 dataview
->SetExpanderColumn(expander
);
108 } // anonymous namespace
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
121 m_sortAscending
= true;
124 int wxDataViewColumn::GetWidth() const
128 case wxCOL_WIDTH_DEFAULT
:
129 return wxDVC_DEFAULT_WIDTH
;
131 case wxCOL_WIDTH_AUTOSIZE
:
132 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
133 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
140 void wxDataViewColumn::UpdateDisplay()
144 int idx
= m_owner
->GetColumnIndex( this );
145 m_owner
->OnColumnChange( idx
);
149 void wxDataViewColumn::SetSortOrder(bool ascending
)
154 // First unset the old sort column if any.
155 int oldSortKey
= m_owner
->GetSortingColumnIndex();
156 if ( oldSortKey
!= wxNOT_FOUND
)
158 m_owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
161 // Now set this one as the new sort column.
162 const int idx
= m_owner
->GetColumnIndex(this);
163 m_owner
->SetSortingColumnIndex(idx
);
166 m_sortAscending
= ascending
;
168 // Call this directly instead of using UpdateDisplay() as we already have
169 // the column index, no need to look it up again.
170 m_owner
->OnColumnChange(idx
);
173 //-----------------------------------------------------------------------------
174 // wxDataViewHeaderWindow
175 //-----------------------------------------------------------------------------
177 class wxDataViewHeaderWindow
: public wxHeaderCtrl
180 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
181 : wxHeaderCtrl(parent
)
185 wxDataViewCtrl
*GetOwner() const
186 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
189 // implement/override wxHeaderCtrl functions by forwarding them to the main
191 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
193 return *(GetOwner()->GetColumn(idx
));
196 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
198 wxDataViewCtrl
* const owner
= GetOwner();
200 int widthContents
= owner
->GetBestColumnWidth(idx
);
201 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
202 owner
->OnColumnChange(idx
);
208 bool SendEvent(wxEventType type
, unsigned int n
)
210 wxDataViewCtrl
* const owner
= GetOwner();
211 wxDataViewEvent
event(type
, owner
->GetId());
213 event
.SetEventObject(owner
);
215 event
.SetDataViewColumn(owner
->GetColumn(n
));
216 event
.SetModel(owner
->GetModel());
218 // for events created by wxDataViewHeaderWindow the
219 // row / value fields are not valid
220 return owner
->ProcessWindowEvent(event
);
223 void OnClick(wxHeaderCtrlEvent
& event
)
225 const unsigned idx
= event
.GetColumn();
227 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
230 // default handling for the column click is to sort by this column or
231 // toggle its sort order
232 wxDataViewCtrl
* const owner
= GetOwner();
233 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
234 if ( !col
->IsSortable() )
236 // no default handling for non-sortable columns
241 if ( col
->IsSortKey() )
243 // already using this column for sorting, just change the order
244 col
->ToggleSortOrder();
246 else // not using this column for sorting yet
248 col
->SetSortOrder(true);
251 wxDataViewModel
* const model
= owner
->GetModel();
255 owner
->OnColumnChange(idx
);
257 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
260 void OnRClick(wxHeaderCtrlEvent
& event
)
262 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
267 void OnResize(wxHeaderCtrlEvent
& event
)
269 wxDataViewCtrl
* const owner
= GetOwner();
271 const unsigned col
= event
.GetColumn();
272 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
273 GetOwner()->OnColumnChange(col
);
276 void OnEndReorder(wxHeaderCtrlEvent
& event
)
278 wxDataViewCtrl
* const owner
= GetOwner();
279 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
280 event
.GetNewOrder());
283 DECLARE_EVENT_TABLE()
284 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
287 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
288 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
289 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
291 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
292 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
294 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
297 //-----------------------------------------------------------------------------
298 // wxDataViewRenameTimer
299 //-----------------------------------------------------------------------------
301 class wxDataViewRenameTimer
: public wxTimer
304 wxDataViewMainWindow
*m_owner
;
307 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
311 //-----------------------------------------------------------------------------
312 // wxDataViewTreeNode
313 //-----------------------------------------------------------------------------
315 class wxDataViewTreeNode
;
316 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
318 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
319 wxDataViewTreeNode
** node2
);
321 class wxDataViewTreeNode
324 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
331 ~wxDataViewTreeNode()
335 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
336 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
347 static wxDataViewTreeNode
* CreateRootNode()
349 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
350 n
->SetHasChildren(true);
351 n
->m_branchData
->open
= true;
355 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
357 const wxDataViewTreeNodes
& GetChildNodes() const
359 wxASSERT( m_branchData
!= NULL
);
360 return m_branchData
->children
;
363 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
366 m_branchData
= new BranchNodeData
;
368 m_branchData
->children
.Insert(node
, index
);
370 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
372 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
375 void RemoveChild(wxDataViewTreeNode
*node
)
377 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
378 m_branchData
->children
.Remove(node
);
381 // returns position of child node for given item in children list or wxNOT_FOUND
382 int FindChildByItem(const wxDataViewItem
& item
) const
387 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
388 const int len
= nodes
.size();
389 for ( int i
= 0; i
< len
; i
++ )
391 if ( nodes
[i
]->m_item
== item
)
397 const wxDataViewItem
& GetItem() const { return m_item
; }
398 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
400 int GetIndentLevel() const
403 const wxDataViewTreeNode
* node
= this;
404 while( node
->GetParent()->GetParent() != NULL
)
406 node
= node
->GetParent();
414 return m_branchData
&& m_branchData
->open
;
419 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
423 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
424 const int len
= nodes
.GetCount();
425 for ( int i
= 0;i
< len
; i
++)
426 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
428 if (m_branchData
->open
)
430 ChangeSubTreeCount(-sum
);
431 m_branchData
->open
= !m_branchData
->open
;
435 m_branchData
->open
= !m_branchData
->open
;
436 ChangeSubTreeCount(+sum
);
440 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
441 // even if GetChildNodes() is empty; see below.
442 bool HasChildren() const
444 return m_branchData
!= NULL
;
447 void SetHasChildren(bool has
)
451 wxDELETE(m_branchData
);
453 else if ( m_branchData
== NULL
)
455 m_branchData
= new BranchNodeData
;
459 int GetSubTreeCount() const
461 return m_branchData
? m_branchData
->subTreeCount
: 0;
464 void ChangeSubTreeCount( int num
)
466 wxASSERT( m_branchData
!= NULL
);
468 if( !m_branchData
->open
)
471 m_branchData
->subTreeCount
+= num
;
472 wxASSERT( m_branchData
->subTreeCount
>= 0 );
475 m_parent
->ChangeSubTreeCount(num
);
485 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
487 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
488 int len
= nodes
.GetCount();
489 for (int i
= 0; i
< len
; i
++)
491 if ( nodes
[i
]->HasChildren() )
499 wxDataViewTreeNode
*m_parent
;
501 // Corresponding model item.
502 wxDataViewItem m_item
;
504 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
505 // separate struct in order to conserve memory.
506 struct BranchNodeData
514 // Child nodes. Note that this may be empty even if m_hasChildren in
515 // case this branch of the tree wasn't expanded and realized yet.
516 wxDataViewTreeNodes children
;
518 // Is the branch node currently open (expanded)?
521 // Total count of expanded (i.e. visible with the help of some
522 // scrolling) items in the subtree, but excluding this node. I.e. it is
523 // 0 for leaves and is the number of rows the subtree occupies for
528 BranchNodeData
*m_branchData
;
532 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
533 wxDataViewTreeNode
** node2
)
535 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
539 //-----------------------------------------------------------------------------
540 // wxDataViewMainWindow
541 //-----------------------------------------------------------------------------
543 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
545 class wxDataViewMainWindow
: public wxWindow
548 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
550 const wxPoint
&pos
= wxDefaultPosition
,
551 const wxSize
&size
= wxDefaultSize
,
552 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
553 virtual ~wxDataViewMainWindow();
555 bool IsList() const { return GetModel()->IsListModel(); }
556 bool IsVirtualList() const { return m_root
== NULL
; }
558 // notifications from wxDataViewModel
559 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
560 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
561 bool ItemChanged( const wxDataViewItem
&item
);
562 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
566 if (!IsVirtualList())
576 g_model
= GetModel();
577 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
580 if (g_model
->HasDefaultCompare())
588 g_column
= col
->GetModelColumn();
589 g_asending
= col
->IsSortOrderAscending();
592 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
593 wxDataViewCtrl
*GetOwner() { return m_owner
; }
594 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
596 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
597 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
599 #if wxUSE_DRAG_AND_DROP
600 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
601 #endif // wxUSE_DRAG_AND_DROP
602 void OnPaint( wxPaintEvent
&event
);
603 void OnCharHook( wxKeyEvent
&event
);
604 void OnChar( wxKeyEvent
&event
);
605 void OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
);
608 void OnMouse( wxMouseEvent
&event
);
609 void OnSetFocus( wxFocusEvent
&event
);
610 void OnKillFocus( wxFocusEvent
&event
);
612 void UpdateDisplay();
613 void RecalculateDisplay();
614 void OnInternalIdle();
616 void OnRenameTimer();
618 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
619 void ScrollTo( int rows
, int column
);
621 unsigned GetCurrentRow() const { return m_currentRow
; }
622 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
623 void ChangeCurrentRow( unsigned int row
);
624 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
626 wxDataViewColumn
*GetCurrentColumn() const { return m_currentCol
; }
627 void ClearCurrentColumn() { m_currentCol
= NULL
; }
629 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
630 bool IsEmpty() { return GetRowCount() == 0; }
632 int GetCountPerPage() const;
633 int GetEndOfLastCol() const;
634 unsigned int GetFirstVisibleRow() const;
636 // I change this method to un const because in the tree view,
637 // the displaying number of the tree are changing along with the
638 // expanding/collapsing of the tree nodes
639 unsigned int GetLastVisibleRow();
640 unsigned int GetRowCount() const;
642 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
643 void SetSelections( const wxDataViewSelection
& sel
)
644 { m_selection
= sel
; UpdateDisplay(); }
645 void Select( const wxArrayInt
& aSelections
);
646 void SelectAllRows( bool on
);
647 void SelectRow( unsigned int row
, bool on
);
648 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
649 void ReverseRowSelection( unsigned int row
);
650 bool IsRowSelected( unsigned int row
);
651 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
653 void RefreshRow( unsigned int row
);
654 void RefreshRows( unsigned int from
, unsigned int to
);
655 void RefreshRowsAfter( unsigned int firstRow
);
657 // returns the colour to be used for drawing the rules
658 wxColour
GetRuleColour() const
660 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
663 wxRect
GetLineRect( unsigned int row
) const;
665 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
666 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
667 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
669 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
670 int GetRowHeight() const { return m_lineHeight
; }
672 // Some useful functions for row and item mapping
673 wxDataViewItem
GetItemByRow( unsigned int row
) const;
674 int GetRowByItem( const wxDataViewItem
& item
) const;
676 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
677 // We did not need this temporarily
678 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
680 // Methods for building the mapping tree
681 void BuildTree( wxDataViewModel
* model
);
683 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
684 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
686 void Expand( unsigned int row
);
687 void Collapse( unsigned int row
);
688 bool IsExpanded( unsigned int row
) const;
689 bool HasChildren( unsigned int row
) const;
691 #if wxUSE_DRAG_AND_DROP
692 bool EnableDragSource( const wxDataFormat
&format
);
693 bool EnableDropTarget( const wxDataFormat
&format
);
695 void RemoveDropHint();
696 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
697 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
698 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
700 #endif // wxUSE_DRAG_AND_DROP
702 void OnColumnsCountChanged();
704 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
705 // specified item in the given column.
706 void StartEditing(const wxDataViewItem
& item
, const wxDataViewColumn
* col
);
709 int RecalculateCount() const;
711 // Return false only if the event was vetoed by its handler.
712 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
714 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
716 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
718 bool IsCellEditableInMode(const wxDataViewItem
& item
, const wxDataViewColumn
*col
, wxDataViewCellMode mode
) const;
720 void DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
);
723 wxDataViewCtrl
*m_owner
;
727 wxDataViewColumn
*m_currentCol
;
728 unsigned int m_currentRow
;
729 wxDataViewSelection m_selection
;
731 wxDataViewRenameTimer
*m_renameTimer
;
736 bool m_currentColSetByKeyboard
;
738 #if wxUSE_DRAG_AND_DROP
743 wxDataFormat m_dragFormat
;
746 wxDataFormat m_dropFormat
;
748 unsigned int m_dropHintLine
;
749 #endif // wxUSE_DRAG_AND_DROP
751 // for double click logic
752 unsigned int m_lineLastClicked
,
753 m_lineBeforeLastClicked
,
754 m_lineSelectSingleOnUp
;
756 // the pen used to draw horiz/vertical rules
759 // the pen used to draw the expander and the lines
762 // This is the tree structure of the model
763 wxDataViewTreeNode
* m_root
;
766 // This is the tree node under the cursor
767 wxDataViewTreeNode
* m_underMouse
;
769 // The control used for editing or NULL.
770 wxWeakRef
<wxWindow
> m_editorCtrl
;
772 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
773 wxDataViewRenderer
* m_editorRenderer
;
776 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
777 DECLARE_EVENT_TABLE()
780 // ---------------------------------------------------------
781 // wxGenericDataViewModelNotifier
782 // ---------------------------------------------------------
784 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
787 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
788 { m_mainWindow
= mainWindow
; }
790 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
791 { return m_mainWindow
->ItemAdded( parent
, item
); }
792 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
793 { return m_mainWindow
->ItemDeleted( parent
, item
); }
794 virtual bool ItemChanged( const wxDataViewItem
& item
)
795 { return m_mainWindow
->ItemChanged(item
); }
796 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
797 { return m_mainWindow
->ValueChanged( item
, col
); }
798 virtual bool Cleared()
799 { return m_mainWindow
->Cleared(); }
800 virtual void Resort()
801 { m_mainWindow
->Resort(); }
803 wxDataViewMainWindow
*m_mainWindow
;
806 // ---------------------------------------------------------
807 // wxDataViewRenderer
808 // ---------------------------------------------------------
810 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
812 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
813 wxDataViewCellMode mode
,
815 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
819 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
823 wxDataViewRenderer::~wxDataViewRenderer()
828 wxDC
*wxDataViewRenderer::GetDC()
832 if (GetOwner() == NULL
)
834 if (GetOwner()->GetOwner() == NULL
)
836 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
842 void wxDataViewRenderer::SetAlignment( int align
)
847 int wxDataViewRenderer::GetAlignment() const
852 // ---------------------------------------------------------
853 // wxDataViewCustomRenderer
854 // ---------------------------------------------------------
856 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
858 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
859 wxDataViewCellMode mode
, int align
) :
860 wxDataViewRenderer( varianttype
, mode
, align
)
864 // ---------------------------------------------------------
865 // wxDataViewTextRenderer
866 // ---------------------------------------------------------
868 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
870 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
871 wxDataViewCellMode mode
, int align
) :
872 wxDataViewRenderer( varianttype
, mode
, align
)
876 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
878 m_text
= value
.GetString();
883 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
888 bool wxDataViewTextRenderer::HasEditorCtrl() const
893 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
894 wxRect labelRect
, const wxVariant
&value
)
896 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
897 wxPoint(labelRect
.x
,labelRect
.y
),
898 wxSize(labelRect
.width
,labelRect
.height
),
899 wxTE_PROCESS_ENTER
);
901 // select the text in the control an place the cursor at the end
902 ctrl
->SetInsertionPointEnd();
908 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
910 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
911 value
= text
->GetValue();
915 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
917 RenderText(m_text
, 0, rect
, dc
, state
);
921 wxSize
wxDataViewTextRenderer::GetSize() const
924 return GetTextExtent(m_text
);
926 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
929 // ---------------------------------------------------------
930 // wxDataViewBitmapRenderer
931 // ---------------------------------------------------------
933 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
935 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
936 wxDataViewCellMode mode
, int align
) :
937 wxDataViewRenderer( varianttype
, mode
, align
)
941 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
943 if (value
.GetType() == wxT("wxBitmap"))
945 if (value
.GetType() == wxT("wxIcon"))
951 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
956 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
959 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
960 else if (m_icon
.IsOk())
961 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
966 wxSize
wxDataViewBitmapRenderer::GetSize() const
969 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
970 else if (m_icon
.IsOk())
971 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
973 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
976 // ---------------------------------------------------------
977 // wxDataViewToggleRenderer
978 // ---------------------------------------------------------
980 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
982 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
983 wxDataViewCellMode mode
, int align
) :
984 wxDataViewRenderer( varianttype
, mode
, align
)
989 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
991 m_toggle
= value
.GetBool();
996 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
1001 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
1005 flags
|= wxCONTROL_CHECKED
;
1006 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
1007 GetEnabled() == false)
1008 flags
|= wxCONTROL_DISABLED
;
1010 // Ensure that the check boxes always have at least the minimal required
1011 // size, otherwise DrawCheckBox() doesn't really work well. If this size is
1012 // greater than the cell size, the checkbox will be truncated but this is a
1014 wxSize size
= cell
.GetSize();
1015 size
.IncTo(GetSize());
1018 wxRendererNative::Get().DrawCheckBox(
1019 GetOwner()->GetOwner(),
1027 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1028 wxDataViewModel
*model
,
1029 const wxDataViewItem
& item
,
1031 const wxMouseEvent
*mouseEvent
)
1035 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1036 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1040 model
->ChangeValue(!m_toggle
, item
, col
);
1044 wxSize
wxDataViewToggleRenderer::GetSize() const
1046 // the window parameter is not used by GetCheckBoxSize() so it's
1047 // safe to pass NULL
1048 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1051 // ---------------------------------------------------------
1052 // wxDataViewProgressRenderer
1053 // ---------------------------------------------------------
1055 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1057 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1058 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1059 wxDataViewRenderer( varianttype
, mode
, align
)
1065 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1067 m_value
= (long) value
;
1069 if (m_value
< 0) m_value
= 0;
1070 if (m_value
> 100) m_value
= 100;
1075 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1077 value
= (long) m_value
;
1082 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1084 // deflate the rect to leave a small border between bars in adjacent rows
1085 wxRect bar
= rect
.Deflate(0, 1);
1087 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1088 dc
->SetPen( *wxBLACK_PEN
);
1089 dc
->DrawRectangle( bar
);
1091 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1092 dc
->SetPen( *wxTRANSPARENT_PEN
);
1094 const wxDataViewItemAttr
& attr
= GetAttr();
1095 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1097 dc
->DrawRectangle( bar
);
1102 wxSize
wxDataViewProgressRenderer::GetSize() const
1104 return wxSize(40,12);
1107 // ---------------------------------------------------------
1108 // wxDataViewIconTextRenderer
1109 // ---------------------------------------------------------
1111 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1113 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1114 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1115 wxDataViewRenderer( varianttype
, mode
, align
)
1118 SetAlignment(align
);
1121 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1127 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1132 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1136 const wxIcon
& icon
= m_value
.GetIcon();
1139 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1140 xoffset
= icon
.GetWidth()+4;
1143 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1148 wxSize
wxDataViewIconTextRenderer::GetSize() const
1150 if (!m_value
.GetText().empty())
1152 wxSize size
= GetTextExtent(m_value
.GetText());
1154 if (m_value
.GetIcon().IsOk())
1155 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1158 return wxSize(80,20);
1161 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1163 wxDataViewIconText iconText
;
1166 wxString text
= iconText
.GetText();
1168 // adjust the label rect to take the width of the icon into account
1169 if (iconText
.GetIcon().IsOk())
1171 int w
= iconText
.GetIcon().GetWidth() + 4;
1173 labelRect
.width
-= w
;
1176 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1177 wxPoint(labelRect
.x
,labelRect
.y
),
1178 wxSize(labelRect
.width
,labelRect
.height
),
1179 wxTE_PROCESS_ENTER
);
1181 // select the text in the control an place the cursor at the end
1182 ctrl
->SetInsertionPointEnd();
1188 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1190 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1192 // The icon can't be edited so get its old value and reuse it.
1194 wxDataViewColumn
* const col
= GetOwner();
1195 GetView()->GetModel()->GetValue(valueOld
, m_item
, col
->GetModelColumn());
1197 wxDataViewIconText iconText
;
1198 iconText
<< valueOld
;
1200 // But replace the text with the value entered by user.
1201 iconText
.SetText(text
->GetValue());
1207 //-----------------------------------------------------------------------------
1208 // wxDataViewDropTarget
1209 //-----------------------------------------------------------------------------
1211 #if wxUSE_DRAG_AND_DROP
1213 class wxBitmapCanvas
: public wxWindow
1216 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1217 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1220 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1223 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1226 dc
.DrawBitmap( m_bitmap
, 0, 0);
1232 class wxDataViewDropSource
: public wxDropSource
1235 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1243 ~wxDataViewDropSource()
1248 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1250 wxPoint pos
= wxGetMousePosition();
1254 int liney
= m_win
->GetLineStart( m_row
);
1256 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1257 m_win
->ClientToScreen( &linex
, &liney
);
1258 m_dist_x
= pos
.x
- linex
;
1259 m_dist_y
= pos
.y
- liney
;
1262 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1264 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1265 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1267 wxFRAME_TOOL_WINDOW
|
1268 wxFRAME_FLOAT_ON_PARENT
|
1269 wxFRAME_NO_TASKBAR
|
1271 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1276 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1277 m_hint
->SetTransparent( 128 );
1283 wxDataViewMainWindow
*m_win
;
1286 int m_dist_x
,m_dist_y
;
1290 class wxDataViewDropTarget
: public wxDropTarget
1293 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1299 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1301 wxDataFormat format
= GetMatchingPair();
1302 if (format
== wxDF_INVALID
)
1304 return m_win
->OnDragOver( format
, x
, y
, def
);
1307 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1309 wxDataFormat format
= GetMatchingPair();
1310 if (format
== wxDF_INVALID
)
1312 return m_win
->OnDrop( format
, x
, y
);
1315 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1317 wxDataFormat format
= GetMatchingPair();
1318 if (format
== wxDF_INVALID
)
1322 return m_win
->OnData( format
, x
, y
, def
);
1325 virtual void OnLeave()
1326 { m_win
->OnLeave(); }
1328 wxDataViewMainWindow
*m_win
;
1331 #endif // wxUSE_DRAG_AND_DROP
1333 //-----------------------------------------------------------------------------
1334 // wxDataViewRenameTimer
1335 //-----------------------------------------------------------------------------
1337 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1342 void wxDataViewRenameTimer::Notify()
1344 m_owner
->OnRenameTimer();
1347 //-----------------------------------------------------------------------------
1348 // wxDataViewMainWindow
1349 //-----------------------------------------------------------------------------
1351 // The tree building helper, declared firstly
1352 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1353 wxDataViewTreeNode
* node
);
1355 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1357 if (row1
> row2
) return 1;
1358 if (row1
== row2
) return 0;
1362 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1364 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1365 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1366 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1367 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1368 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1369 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook
)
1370 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1373 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1374 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1375 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1376 m_selection( wxDataViewSelectionCmp
)
1381 m_editorRenderer
= NULL
;
1383 m_lastOnSame
= false;
1384 m_renameTimer
= new wxDataViewRenameTimer( this );
1386 // TODO: user better initial values/nothing selected
1387 m_currentCol
= NULL
;
1388 m_currentColSetByKeyboard
= false;
1389 m_useCellFocus
= false;
1393 // We would like to use the same line height that Explorer uses. This is
1394 // different from standard ListView control since Vista.
1395 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
1396 m_lineHeight
= wxMax(16, GetCharHeight()) + 6; // 16 = mini icon height
1399 m_lineHeight
= wxMax(16, GetCharHeight()) + 1; // 16 = mini icon height
1401 #if wxUSE_DRAG_AND_DROP
1403 m_dragStart
= wxPoint(0,0);
1405 m_dragEnabled
= false;
1406 m_dropEnabled
= false;
1408 m_dropHintLine
= (unsigned int) -1;
1409 #endif // wxUSE_DRAG_AND_DROP
1411 m_lineLastClicked
= (unsigned int) -1;
1412 m_lineBeforeLastClicked
= (unsigned int) -1;
1413 m_lineSelectSingleOnUp
= (unsigned int) -1;
1417 SetBackgroundColour( *wxWHITE
);
1419 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1421 m_penRule
= wxPen(GetRuleColour());
1423 // compose a pen whichcan draw black lines
1424 // TODO: maybe there is something system colour to use
1425 m_penExpander
= wxPen(wxColour(0,0,0));
1427 m_root
= wxDataViewTreeNode::CreateRootNode();
1429 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1431 m_underMouse
= NULL
;
1435 wxDataViewMainWindow::~wxDataViewMainWindow()
1438 delete m_renameTimer
;
1442 #if wxUSE_DRAG_AND_DROP
1443 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1445 m_dragFormat
= format
;
1446 m_dragEnabled
= format
!= wxDF_INVALID
;
1451 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1453 m_dropFormat
= format
;
1454 m_dropEnabled
= format
!= wxDF_INVALID
;
1457 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1462 void wxDataViewMainWindow::RemoveDropHint()
1467 RefreshRow( m_dropHintLine
);
1468 m_dropHintLine
= (unsigned int) -1;
1472 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1473 wxCoord y
, wxDragResult def
)
1477 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1478 unsigned int row
= GetLineAt( yy
);
1480 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1486 wxDataViewItem item
= GetItemByRow( row
);
1488 wxDataViewModel
*model
= GetModel();
1490 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1491 event
.SetEventObject( m_owner
);
1492 event
.SetItem( item
);
1493 event
.SetModel( model
);
1494 event
.SetDataFormat( format
);
1495 event
.SetDropEffect( def
);
1496 if (!m_owner
->HandleWindowEvent( event
))
1502 if (!event
.IsAllowed())
1509 if (m_dropHint
&& (row
!= m_dropHintLine
))
1510 RefreshRow( m_dropHintLine
);
1512 m_dropHintLine
= row
;
1518 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1524 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1525 unsigned int row
= GetLineAt( yy
);
1527 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1530 wxDataViewItem item
= GetItemByRow( row
);
1532 wxDataViewModel
*model
= GetModel();
1534 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1535 event
.SetEventObject( m_owner
);
1536 event
.SetItem( item
);
1537 event
.SetModel( model
);
1538 event
.SetDataFormat( format
);
1539 if (!m_owner
->HandleWindowEvent( event
))
1542 if (!event
.IsAllowed())
1548 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1553 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1554 unsigned int row
= GetLineAt( yy
);
1556 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1559 wxDataViewItem item
= GetItemByRow( row
);
1561 wxDataViewModel
*model
= GetModel();
1563 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1565 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1566 event
.SetEventObject( m_owner
);
1567 event
.SetItem( item
);
1568 event
.SetModel( model
);
1569 event
.SetDataFormat( format
);
1570 event
.SetDataSize( obj
->GetSize() );
1571 event
.SetDataBuffer( obj
->GetData() );
1572 event
.SetDropEffect( def
);
1573 if (!m_owner
->HandleWindowEvent( event
))
1576 if (!event
.IsAllowed())
1582 void wxDataViewMainWindow::OnLeave()
1587 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1589 int height
= GetLineHeight( row
);
1591 unsigned int cols
= GetOwner()->GetColumnCount();
1593 for (col
= 0; col
< cols
; col
++)
1595 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1596 if (column
->IsHidden())
1597 continue; // skip it!
1598 width
+= column
->GetWidth();
1604 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1605 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1606 indent
= indent
+ m_lineHeight
;
1607 // try to use the m_lineHeight as the expander space
1611 wxBitmap
bitmap( width
, height
);
1612 wxMemoryDC
dc( bitmap
);
1613 dc
.SetFont( GetFont() );
1614 dc
.SetPen( *wxBLACK_PEN
);
1615 dc
.SetBrush( *wxWHITE_BRUSH
);
1616 dc
.DrawRectangle( 0,0,width
,height
);
1618 wxDataViewModel
*model
= m_owner
->GetModel();
1620 wxDataViewColumn
* const
1621 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1624 for (col
= 0; col
< cols
; col
++)
1626 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1627 wxDataViewRenderer
*cell
= column
->GetRenderer();
1629 if (column
->IsHidden())
1630 continue; // skip it!
1632 width
= column
->GetWidth();
1634 if (column
== expander
)
1637 wxDataViewItem item
= GetItemByRow( row
);
1638 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1640 wxRect
item_rect(x
, 0, width
, height
);
1641 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1643 // dc.SetClippingRegion( item_rect );
1644 cell
->WXCallRender(item_rect
, &dc
, 0);
1645 // dc.DestroyClippingRegion();
1653 #endif // wxUSE_DRAG_AND_DROP
1656 // Draw focus rect for individual cell. Unlike native focus rect, we render
1657 // this in foreground text color (typically white) to enhance contrast and
1659 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1661 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1663 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1664 // may be short traits and not really dots
1666 // note that to behave in the same manner as DrawRect(), we must exclude
1667 // the bottom and right borders from the rectangle
1668 wxCoord x1
= rect
.GetLeft(),
1670 x2
= rect
.GetRight(),
1671 y2
= rect
.GetBottom();
1673 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1676 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1677 dc
.DrawPoint(z
, rect
.GetTop());
1679 wxCoord shift
= z
== x2
? 0 : 1;
1680 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1681 dc
.DrawPoint(x2
, z
);
1683 shift
= z
== y2
? 0 : 1;
1684 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1685 dc
.DrawPoint(z
, y2
);
1687 shift
= z
== x1
? 0 : 1;
1688 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1689 dc
.DrawPoint(x1
, z
);
1693 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1695 wxDataViewModel
*model
= GetModel();
1696 wxAutoBufferedPaintDC
dc( this );
1699 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1700 dc
.SetPen( *wxTRANSPARENT_PEN
);
1701 dc
.DrawRectangle(GetClientSize());
1706 // No items to draw.
1711 GetOwner()->PrepareDC( dc
);
1712 dc
.SetFont( GetFont() );
1714 wxRect update
= GetUpdateRegion().GetBox();
1715 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1717 // compute which items needs to be redrawn
1718 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1719 unsigned int item_count
=
1720 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1721 (int)(GetRowCount( ) - item_start
));
1722 unsigned int item_last
= item_start
+ item_count
;
1724 // Send the event to wxDataViewCtrl itself.
1725 wxWindow
* const parent
= GetParent();
1726 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1727 cache_event
.SetEventObject(parent
);
1728 cache_event
.SetCache(item_start
, item_last
- 1);
1729 parent
->ProcessWindowEvent(cache_event
);
1731 // compute which columns needs to be redrawn
1732 unsigned int cols
= GetOwner()->GetColumnCount();
1735 // we assume that we have at least one column below and painting an
1736 // empty control is unnecessary anyhow
1740 unsigned int col_start
= 0;
1741 unsigned int x_start
;
1742 for (x_start
= 0; col_start
< cols
; col_start
++)
1744 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1745 if (col
->IsHidden())
1746 continue; // skip it!
1748 unsigned int w
= col
->GetWidth();
1749 if (x_start
+w
>= (unsigned int)update
.x
)
1755 unsigned int col_last
= col_start
;
1756 unsigned int x_last
= x_start
;
1757 for (; col_last
< cols
; col_last
++)
1759 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1760 if (col
->IsHidden())
1761 continue; // skip it!
1763 if (x_last
> (unsigned int)update
.GetRight())
1766 x_last
+= col
->GetWidth();
1769 // Draw background of alternate rows specially if required
1770 if ( m_owner
->HasFlag(wxDV_ROW_LINES
) )
1772 wxColour altRowColour
= m_owner
->m_alternateRowColour
;
1773 if ( !altRowColour
.IsOk() )
1775 // Determine the alternate rows colour automatically from the
1776 // background colour.
1777 const wxColour bgColour
= m_owner
->GetBackgroundColour();
1779 // Depending on the background, alternate row color
1780 // will be 3% more dark or 50% brighter.
1781 int alpha
= bgColour
.GetRGB() > 0x808080 ? 97 : 150;
1782 altRowColour
= bgColour
.ChangeLightness(alpha
);
1785 dc
.SetPen(*wxTRANSPARENT_PEN
);
1786 dc
.SetBrush(wxBrush(altRowColour
));
1788 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1792 dc
.DrawRectangle(x_start
,
1794 GetClientSize().GetWidth(),
1795 GetLineHeight(item
));
1800 // Draw horizontal rules if required
1801 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1803 dc
.SetPen(m_penRule
);
1804 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1806 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1808 int y
= GetLineStart( i
);
1809 dc
.DrawLine(x_start
, y
, x_last
, y
);
1813 // Draw vertical rules if required
1814 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1816 dc
.SetPen(m_penRule
);
1817 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1819 // NB: Vertical rules are drawn in the last pixel of a column so that
1820 // they align perfectly with native MSW wxHeaderCtrl as well as for
1821 // consistency with MSW native list control. There's no vertical
1822 // rule at the most-left side of the control.
1824 int x
= x_start
- 1;
1825 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1827 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1828 if (col
->IsHidden())
1829 continue; // skip it
1831 x
+= col
->GetWidth();
1833 dc
.DrawLine(x
, GetLineStart( item_start
),
1834 x
, GetLineStart( item_last
) );
1838 // redraw the background for the items which are selected/current
1839 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1841 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1843 if (selected
|| item
== m_currentRow
)
1845 wxRect
rect( x_start
, GetLineStart( item
),
1846 x_last
- x_start
, GetLineHeight( item
) );
1848 // draw selection and whole-item focus:
1851 int flags
= wxCONTROL_SELECTED
;
1853 flags
|= wxCONTROL_FOCUSED
;
1855 wxRendererNative::Get().DrawItemSelectionRect
1864 // draw keyboard focus rect if applicable
1865 if ( item
== m_currentRow
&& m_hasFocus
)
1867 bool renderColumnFocus
= false;
1869 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1871 renderColumnFocus
= true;
1873 // If this is container node without columns, render full-row focus:
1876 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1877 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1878 renderColumnFocus
= false;
1882 if ( renderColumnFocus
)
1884 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1886 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1887 if ( col
->IsHidden() )
1890 rect
.width
= col
->GetWidth();
1892 if ( col
== m_currentCol
)
1894 // make the rect more visible by adding a small
1895 // margin around it:
1900 // DrawFocusRect() uses XOR and is all but
1901 // invisible against dark-blue background. Use
1902 // the same color used for selected text.
1903 DrawSelectedCellFocusRect(dc
, rect
);
1907 wxRendererNative::Get().DrawFocusRect
1918 rect
.x
+= rect
.width
;
1923 // render focus rectangle for the whole row
1924 wxRendererNative::Get().DrawFocusRect
1929 selected
? (int)wxCONTROL_SELECTED
: 0
1936 #if wxUSE_DRAG_AND_DROP
1939 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1940 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1941 dc
.SetPen( *wxBLACK_PEN
);
1942 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1943 dc
.DrawRectangle( rect
);
1945 #endif // wxUSE_DRAG_AND_DROP
1947 wxDataViewColumn
* const
1948 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1950 // redraw all cells for all rows which must be repainted and all columns
1952 cell_rect
.x
= x_start
;
1953 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1955 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1956 wxDataViewRenderer
*cell
= col
->GetRenderer();
1957 cell_rect
.width
= col
->GetWidth();
1959 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1960 continue; // skip it!
1962 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1964 // get the cell value and set it into the renderer
1965 wxDataViewTreeNode
*node
= NULL
;
1966 wxDataViewItem dataitem
;
1968 if (!IsVirtualList())
1970 node
= GetTreeNodeByRow(item
);
1974 dataitem
= node
->GetItem();
1976 // Skip all columns of "container" rows except the expander
1977 // column itself unless HasContainerColumns() overrides this.
1978 if ( col
!= expander
&&
1979 model
->IsContainer(dataitem
) &&
1980 !model
->HasContainerColumns(dataitem
) )
1985 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1988 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1991 cell_rect
.y
= GetLineStart( item
);
1992 cell_rect
.height
= GetLineHeight( item
);
1994 // draw the background
1995 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1997 DrawCellBackground( cell
, dc
, cell_rect
);
1999 // deal with the expander
2001 if ((!IsList()) && (col
== expander
))
2003 // Calculate the indent first
2004 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
2006 // we reserve m_lineHeight of horizontal space for the expander
2007 // but leave EXPANDER_MARGIN around the expander itself
2008 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
2010 indent
+= m_lineHeight
;
2012 // draw expander if needed and visible
2013 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
2015 dc
.SetPen( m_penExpander
);
2016 dc
.SetBrush( wxNullBrush
);
2018 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2019 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
2020 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
2022 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
2025 if ( m_underMouse
== node
)
2026 flag
|= wxCONTROL_CURRENT
;
2027 if ( node
->IsOpen() )
2028 flag
|= wxCONTROL_EXPANDED
;
2030 // ensure that we don't overflow the cell (which might
2031 // happen if the column is very narrow)
2032 wxDCClipper
clip(dc
, cell_rect
);
2034 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2037 // force the expander column to left-center align
2038 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2041 wxRect item_rect
= cell_rect
;
2042 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
2044 // account for the tree indent (harmless if we're not indented)
2045 item_rect
.x
+= indent
;
2046 item_rect
.width
-= indent
;
2048 if ( item_rect
.width
<= 0 )
2052 if (m_hasFocus
&& selected
)
2053 state
|= wxDATAVIEW_CELL_SELECTED
;
2055 // TODO: it would be much more efficient to create a clipping
2056 // region for the entire column being rendered (in the OnPaint
2057 // of wxDataViewMainWindow) instead of a single clip region for
2058 // each cell. However it would mean that each renderer should
2059 // respect the given wxRect's top & bottom coords, eventually
2060 // violating only the left & right coords - however the user can
2061 // make its own renderer and thus we cannot be sure of that.
2062 wxDCClipper
clip(dc
, item_rect
);
2064 cell
->WXCallRender(item_rect
, &dc
, state
);
2067 cell_rect
.x
+= cell_rect
.width
;
2072 void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
)
2074 wxRect
rectBg( rect
);
2076 // don't overlap the horizontal rules
2077 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2083 // don't overlap the vertical rules
2084 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2090 cell
->RenderBackground(&dc
, rectBg
);
2093 void wxDataViewMainWindow::OnRenameTimer()
2095 // We have to call this here because changes may just have
2096 // been made and no screen update taken place.
2099 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2100 // (needs to be tested!)
2104 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2106 StartEditing( item
, m_currentCol
);
2110 wxDataViewMainWindow::StartEditing(const wxDataViewItem
& item
,
2111 const wxDataViewColumn
* col
)
2113 wxDataViewRenderer
* renderer
= col
->GetRenderer();
2114 if ( !IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
2117 const wxRect itemRect
= GetItemRect(item
, col
);
2118 if ( renderer
->StartEditing(item
, itemRect
) )
2120 // Save the renderer to be able to finish/cancel editing it later and
2121 // save the control to be able to detect if we're still editing it.
2122 m_editorRenderer
= renderer
;
2123 m_editorCtrl
= renderer
->GetEditorCtrl();
2127 //-----------------------------------------------------------------------------
2128 // Helper class for do operation on the tree node
2129 //-----------------------------------------------------------------------------
2134 virtual ~DoJob() { }
2136 // The return value control how the tree-walker tranverse the tree
2139 DONE
, // Job done, stop traversing and return
2140 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2141 CONTINUE
// Job not done, continue
2144 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2147 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2149 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2151 switch( func( node
) )
2155 case DoJob::SKIP_SUBTREE
:
2157 case DoJob::CONTINUE
:
2161 if ( node
->HasChildren() )
2163 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2165 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2169 if ( Walker(*i
, func
) )
2177 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2179 if (IsVirtualList())
2181 wxDataViewVirtualListModel
*list_model
=
2182 (wxDataViewVirtualListModel
*) GetModel();
2183 m_count
= list_model
->GetCount();
2189 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2194 wxDataViewItemArray modelSiblings
;
2195 GetModel()->GetChildren(parent
, modelSiblings
);
2196 const int modelSiblingsSize
= modelSiblings
.size();
2198 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2199 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2201 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2202 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2204 parentNode
->SetHasChildren(true);
2206 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2207 const int nodeSiblingsSize
= nodeSiblings
.size();
2211 if ( posInModel
== modelSiblingsSize
- 1 )
2213 nodePos
= nodeSiblingsSize
;
2215 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2217 // This is the simple case when our node tree already matches the
2218 // model and only this one item is missing.
2219 nodePos
= posInModel
;
2223 // It's possible that a larger discrepancy between the model and
2224 // our realization exists. This can happen e.g. when adding a bunch
2225 // of items to the model and then calling ItemsAdded() just once
2226 // afterwards. In this case, we must find the right position by
2227 // looking at sibling items.
2229 // append to the end if we won't find a better position:
2230 nodePos
= nodeSiblingsSize
;
2232 for ( int nextItemPos
= posInModel
+ 1;
2233 nextItemPos
< modelSiblingsSize
;
2236 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2237 if ( nextNodePos
!= wxNOT_FOUND
)
2239 nodePos
= nextNodePos
;
2245 parentNode
->ChangeSubTreeCount(+1);
2246 parentNode
->InsertChild(itemNode
, nodePos
);
2251 GetOwner()->InvalidateColBestWidths();
2257 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2258 const wxDataViewItem
& item
)
2260 if (IsVirtualList())
2262 wxDataViewVirtualListModel
*list_model
=
2263 (wxDataViewVirtualListModel
*) GetModel();
2264 m_count
= list_model
->GetCount();
2266 if ( !m_selection
.empty() )
2268 const int row
= GetRowByItem(item
);
2270 int rowIndexInSelection
= wxNOT_FOUND
;
2272 const size_t selCount
= m_selection
.size();
2273 for ( size_t i
= 0; i
< selCount
; i
++ )
2275 if ( m_selection
[i
] == (unsigned)row
)
2276 rowIndexInSelection
= i
;
2277 else if ( m_selection
[i
] > (unsigned)row
)
2281 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2282 m_selection
.RemoveAt(rowIndexInSelection
);
2286 else // general case
2288 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2290 // Notice that it is possible that the item being deleted is not in the
2291 // tree at all, for example we could be deleting a never shown (because
2292 // collapsed) item in a tree model. So it's not an error if we don't know
2293 // about this item, just return without doing anything then.
2297 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2298 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2300 // We can't use FindNode() to find 'item', because it was already
2301 // removed from the model by the time ItemDeleted() is called, so we
2302 // have to do it manually. We keep track of its position as well for
2304 int itemPosInNode
= 0;
2305 wxDataViewTreeNode
*itemNode
= NULL
;
2306 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2307 i
!= parentsChildren
.end();
2308 ++i
, ++itemPosInNode
)
2310 if( (*i
)->GetItem() == item
)
2317 // If the parent wasn't expanded, it's possible that we didn't have a
2318 // node corresponding to 'item' and so there's nothing left to do.
2321 // If this was the last child to be removed, it's possible the parent
2322 // node became a leaf. Let's ask the model about it.
2323 if ( parentNode
->GetChildNodes().empty() )
2324 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2329 // Delete the item from wxDataViewTreeNode representation:
2330 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2332 parentNode
->RemoveChild(itemNode
);
2334 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2336 // Make the row number invalid and get a new valid one when user call GetRowCount
2339 // If this was the last child to be removed, it's possible the parent
2340 // node became a leaf. Let's ask the model about it.
2341 if ( parentNode
->GetChildNodes().empty() )
2342 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2344 // Update selection by removing 'item' and its entire children tree from the selection.
2345 if ( !m_selection
.empty() )
2347 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2348 // the parent ('parentNode') and position in its list of children
2350 if ( itemPosInNode
== 0 )
2352 // 1st child, row number is that of the parent parentNode + 1
2353 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2357 // row number is that of the sibling above 'item' + its subtree if any + 1
2358 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2360 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2361 siblingNode
->GetSubTreeCount() +
2365 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2367 const size_t numSelections
= m_selection
.size();
2368 for ( size_t i
= 0; i
< numSelections
; ++i
)
2370 const int s
= m_selection
[i
];
2372 newsel
.push_back(s
);
2373 else if ( s
>= itemRow
+ itemsDeleted
)
2374 newsel
.push_back(s
- itemsDeleted
);
2375 // else: deleted item, remove from selection
2378 m_selection
= newsel
;
2382 // Change the current row to the last row if the current exceed the max row number
2383 if( m_currentRow
> GetRowCount() )
2384 ChangeCurrentRow(m_count
- 1);
2386 GetOwner()->InvalidateColBestWidths();
2392 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2397 GetOwner()->InvalidateColBestWidths();
2400 wxWindow
*parent
= GetParent();
2401 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2402 le
.SetEventObject(parent
);
2403 le
.SetModel(GetModel());
2405 parent
->ProcessWindowEvent(le
);
2410 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2412 int view_column
= -1;
2413 unsigned int n_col
= m_owner
->GetColumnCount();
2414 for (unsigned i
= 0; i
< n_col
; i
++)
2416 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2417 if (column
->GetModelColumn() == model_column
)
2419 view_column
= (int) i
;
2423 if (view_column
== -1)
2426 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2427 /*#define MAX_VIRTUAL_WIDTH 100000
2429 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2430 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2431 Refresh( true, &rect );
2438 GetOwner()->InvalidateColBestWidth(view_column
);
2441 wxWindow
*parent
= GetParent();
2442 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2443 le
.SetEventObject(parent
);
2444 le
.SetModel(GetModel());
2446 le
.SetColumn(view_column
);
2447 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2448 parent
->ProcessWindowEvent(le
);
2453 bool wxDataViewMainWindow::Cleared()
2456 m_selection
.Clear();
2459 BuildTree( GetModel() );
2461 GetOwner()->InvalidateColBestWidths();
2467 void wxDataViewMainWindow::UpdateDisplay()
2470 m_underMouse
= NULL
;
2473 void wxDataViewMainWindow::OnInternalIdle()
2475 wxWindow::OnInternalIdle();
2479 RecalculateDisplay();
2484 void wxDataViewMainWindow::RecalculateDisplay()
2486 wxDataViewModel
*model
= GetModel();
2493 int width
= GetEndOfLastCol();
2494 int height
= GetLineStart( GetRowCount() );
2496 SetVirtualSize( width
, height
);
2497 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2502 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2504 m_underMouse
= NULL
;
2506 wxWindow::ScrollWindow( dx
, dy
, rect
);
2508 if (GetOwner()->m_headerArea
)
2509 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2512 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2514 m_underMouse
= NULL
;
2517 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2518 int sy
= GetLineStart( rows
)/y
;
2522 wxRect rect
= GetClientRect();
2526 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2527 for (x_start
= 0; colnum
< column
; colnum
++)
2529 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2530 if (col
->IsHidden())
2531 continue; // skip it!
2533 w
= col
->GetWidth();
2537 int x_end
= x_start
+ w
;
2538 xe
= xx
+ rect
.width
;
2541 sx
= ( xx
+ x_end
- xe
)/x
;
2548 m_owner
->Scroll( sx
, sy
);
2551 int wxDataViewMainWindow::GetCountPerPage() const
2553 wxSize size
= GetClientSize();
2554 return size
.y
/ m_lineHeight
;
2557 int wxDataViewMainWindow::GetEndOfLastCol() const
2561 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2563 const wxDataViewColumn
*c
=
2564 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2567 width
+= c
->GetWidth();
2572 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2576 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2578 return GetLineAt( y
);
2581 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2583 wxSize client_size
= GetClientSize();
2584 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2585 &client_size
.x
, &client_size
.y
);
2587 // we should deal with the pixel here
2588 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2590 return wxMin( GetRowCount()-1, row
);
2593 unsigned int wxDataViewMainWindow::GetRowCount() const
2595 if ( m_count
== -1 )
2597 wxDataViewMainWindow
* const
2598 self
= const_cast<wxDataViewMainWindow
*>(this);
2599 self
->m_count
= RecalculateCount();
2600 self
->UpdateDisplay();
2605 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2612 void wxDataViewMainWindow::SelectAllRows( bool on
)
2619 m_selection
.Clear();
2620 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2621 m_selection
.Add( i
);
2626 unsigned int first_visible
= GetFirstVisibleRow();
2627 unsigned int last_visible
= GetLastVisibleRow();
2629 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2631 unsigned int row
= m_selection
[i
];
2632 if ((row
>= first_visible
) && (row
<= last_visible
))
2635 m_selection
.Clear();
2639 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2641 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2645 m_selection
.Add( row
);
2653 m_selection
.Remove( row
);
2659 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2663 unsigned int tmp
= from
;
2669 for (i
= from
; i
<= to
; i
++)
2671 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2674 m_selection
.Add( i
);
2679 m_selection
.Remove( i
);
2682 RefreshRows( from
, to
);
2685 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2687 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2689 int n
= aSelections
[i
];
2691 m_selection
.Add( n
);
2696 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2698 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2699 m_selection
.Add( row
);
2701 m_selection
.Remove( row
);
2705 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2707 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2710 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2712 wxWindow
*parent
= GetParent();
2713 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2715 le
.SetEventObject(parent
);
2716 le
.SetModel(GetModel());
2719 parent
->ProcessWindowEvent(le
);
2722 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2724 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2725 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2727 wxSize client_size
= GetClientSize();
2728 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2729 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2730 if (intersect_rect
.width
> 0)
2731 Refresh( true, &intersect_rect
);
2734 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2738 unsigned int tmp
= to
;
2743 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2744 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2746 wxSize client_size
= GetClientSize();
2747 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2748 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2749 if (intersect_rect
.width
> 0)
2750 Refresh( true, &intersect_rect
);
2753 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2755 wxSize client_size
= GetClientSize();
2756 int start
= GetLineStart( firstRow
);
2757 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2758 if (start
> client_size
.y
) return;
2760 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2762 Refresh( true, &rect
);
2765 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2769 rect
.y
= GetLineStart( row
);
2770 rect
.width
= GetEndOfLastCol();
2771 rect
.height
= GetLineHeight( row
);
2776 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2778 const wxDataViewModel
*model
= GetModel();
2780 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2782 // TODO make more efficient
2787 for (r
= 0; r
< row
; r
++)
2789 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2790 if (!node
) return start
;
2792 wxDataViewItem item
= node
->GetItem();
2794 unsigned int cols
= GetOwner()->GetColumnCount();
2796 int height
= m_lineHeight
;
2797 for (col
= 0; col
< cols
; col
++)
2799 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2800 if (column
->IsHidden())
2801 continue; // skip it!
2804 model
->IsContainer(item
) &&
2805 !model
->HasContainerColumns(item
))
2806 continue; // skip it!
2808 wxDataViewRenderer
*renderer
=
2809 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2810 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2812 height
= wxMax( height
, renderer
->GetSize().y
);
2822 return row
* m_lineHeight
;
2826 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2828 const wxDataViewModel
*model
= GetModel();
2830 // check for the easy case first
2831 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2832 return y
/ m_lineHeight
;
2834 // TODO make more efficient
2835 unsigned int row
= 0;
2836 unsigned int yy
= 0;
2839 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2842 // not really correct...
2843 return row
+ ((y
-yy
) / m_lineHeight
);
2846 wxDataViewItem item
= node
->GetItem();
2848 unsigned int cols
= GetOwner()->GetColumnCount();
2850 int height
= m_lineHeight
;
2851 for (col
= 0; col
< cols
; col
++)
2853 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2854 if (column
->IsHidden())
2855 continue; // skip it!
2858 model
->IsContainer(item
) &&
2859 !model
->HasContainerColumns(item
))
2860 continue; // skip it!
2862 wxDataViewRenderer
*renderer
=
2863 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2864 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2866 height
= wxMax( height
, renderer
->GetSize().y
);
2877 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2879 const wxDataViewModel
*model
= GetModel();
2881 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2883 wxASSERT( !IsVirtualList() );
2885 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2886 // wxASSERT( node );
2887 if (!node
) return m_lineHeight
;
2889 wxDataViewItem item
= node
->GetItem();
2891 int height
= m_lineHeight
;
2893 unsigned int cols
= GetOwner()->GetColumnCount();
2895 for (col
= 0; col
< cols
; col
++)
2897 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2898 if (column
->IsHidden())
2899 continue; // skip it!
2902 model
->IsContainer(item
) &&
2903 !model
->HasContainerColumns(item
))
2904 continue; // skip it!
2906 wxDataViewRenderer
*renderer
=
2907 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2908 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2910 height
= wxMax( height
, renderer
->GetSize().y
);
2917 return m_lineHeight
;
2922 class RowToTreeNodeJob
: public DoJob
2925 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2928 this->current
= current
;
2933 virtual int operator() ( wxDataViewTreeNode
* node
)
2936 if( current
== static_cast<int>(row
))
2942 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2944 current
+= node
->GetSubTreeCount();
2945 return DoJob::SKIP_SUBTREE
;
2951 // If the current node has only leaf children, we can find the
2952 // desired node directly. This can speed up finding the node
2953 // in some cases, and will have a very good effect for list views.
2954 if ( node
->HasChildren() &&
2955 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2957 const int index
= static_cast<int>(row
) - current
- 1;
2958 ret
= node
->GetChildNodes()[index
];
2962 return DoJob::CONTINUE
;
2966 wxDataViewTreeNode
* GetResult() const
2972 wxDataViewTreeNode
* ret
;
2973 wxDataViewTreeNode
* parent
;
2976 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2978 wxASSERT( !IsVirtualList() );
2980 RowToTreeNodeJob
job( row
, -2, m_root
);
2981 Walker( m_root
, job
);
2982 return job
.GetResult();
2985 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2987 wxDataViewItem item
;
2988 if (IsVirtualList())
2990 if ( row
< GetRowCount() )
2991 item
= wxDataViewItem(wxUIntToPtr(row
+1));
2995 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2997 item
= node
->GetItem();
3004 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
3005 const wxDataViewItem
& item
)
3007 wxWindow
*parent
= GetParent();
3008 wxDataViewEvent
le(type
, parent
->GetId());
3010 le
.SetEventObject(parent
);
3011 le
.SetModel(GetModel());
3014 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
3017 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
3022 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3026 if (!node
->HasChildren())
3029 return node
->IsOpen();
3032 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
3037 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3041 if (!node
->HasChildren())
3047 void wxDataViewMainWindow::Expand( unsigned int row
)
3052 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3056 if (!node
->HasChildren())
3059 if (!node
->IsOpen())
3061 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
3063 // Vetoed by the event handler.
3069 // build the children of current node
3070 if( node
->GetChildNodes().empty() )
3073 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
3076 // By expanding the node all row indices that are currently in the selection list
3077 // and are greater than our node have become invalid. So we have to correct that now.
3078 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3079 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3081 const unsigned testRow
= m_selection
[i
];
3082 // all rows above us are not affected, so skip them
3086 m_selection
[i
] += rowAdjustment
;
3089 if(m_currentRow
> row
)
3090 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3094 // Send the expanded event
3095 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3099 void wxDataViewMainWindow::Collapse(unsigned int row
)
3104 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3108 if (!node
->HasChildren())
3113 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3115 // Vetoed by the event handler.
3119 // Find out if there are selected items below the current node.
3120 bool selectCollapsingRow
= false;
3121 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3122 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3123 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3125 const unsigned testRow
= m_selection
[i
];
3126 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3128 selectCollapsingRow
= true;
3129 // get out as soon as we have found a node that is selected
3136 // If the node to be closed has selected items the user won't see those any longer.
3137 // We select the collapsing node in this case.
3138 if(selectCollapsingRow
)
3140 SelectAllRows(false);
3141 ChangeCurrentRow(row
);
3142 SelectRow(row
, true);
3143 SendSelectionChangedEvent(GetItemByRow(row
));
3147 // if there were no selected items below our node we still need to "fix" the
3148 // selection list to adjust for the changing of the row indices.
3149 // We actually do the opposite of what we are doing in Expand().
3150 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3152 const unsigned testRow
= m_selection
[i
];
3153 // all rows above us are not affected, so skip them
3157 m_selection
[i
] -= rowAdjustment
;
3160 // if the "current row" is being collapsed away we change it to the current row ;-)
3161 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3162 ChangeCurrentRow(row
);
3163 else if(m_currentRow
> row
)
3164 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3169 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3173 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3175 const wxDataViewModel
* model
= GetModel();
3182 // Compose the parent-chain for the item we are looking for
3183 wxVector
<wxDataViewItem
> parentChain
;
3184 wxDataViewItem
it( item
);
3187 parentChain
.push_back(it
);
3188 it
= model
->GetParent(it
);
3191 // Find the item along the parent-chain.
3192 // This algorithm is designed to speed up the node-finding method
3193 wxDataViewTreeNode
* node
= m_root
;
3194 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3196 if( node
->HasChildren() )
3198 if( node
->GetChildNodes().empty() )
3200 // Even though the item is a container, it doesn't have any
3201 // child nodes in the control's representation yet. We have
3202 // to realize its subtree now.
3204 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3207 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3210 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3212 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3213 if (currentNode
->GetItem() == parentChain
[iter
])
3215 if (currentNode
->GetItem() == item
)
3235 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3236 wxDataViewColumn
* &column
)
3238 wxDataViewColumn
*col
= NULL
;
3239 unsigned int cols
= GetOwner()->GetColumnCount();
3240 unsigned int colnum
= 0;
3242 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3243 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3245 col
= GetOwner()->GetColumnAt(colnum
);
3246 if (col
->IsHidden())
3247 continue; // skip it!
3249 unsigned int w
= col
->GetWidth();
3250 if (x_start
+w
>= (unsigned int)x
)
3257 item
= GetItemByRow( GetLineAt( y
) );
3260 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3261 const wxDataViewColumn
* column
)
3266 unsigned int cols
= GetOwner()->GetColumnCount();
3267 // If column is null the loop will compute the combined width of all columns.
3268 // Otherwise, it will compute the x position of the column we are looking for.
3269 for (unsigned int i
= 0; i
< cols
; i
++)
3271 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3276 if (col
->IsHidden())
3277 continue; // skip it!
3279 xpos
+= col
->GetWidth();
3280 width
+= col
->GetWidth();
3285 // If we have a column, we need can get its width directly.
3286 if(column
->IsHidden())
3289 width
= column
->GetWidth();
3294 // If we have no column, we reset the x position back to zero.
3298 // we have to take an expander column into account and compute its indentation
3299 // to get the correct x position where the actual text is
3301 int row
= GetRowByItem(item
);
3303 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3305 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3306 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3307 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3310 wxRect
itemRect( xpos
+ indent
,
3311 GetLineStart( row
),
3313 GetLineHeight( row
) );
3315 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3316 &itemRect
.x
, &itemRect
.y
);
3321 int wxDataViewMainWindow::RecalculateCount() const
3323 if (IsVirtualList())
3325 wxDataViewVirtualListModel
*list_model
=
3326 (wxDataViewVirtualListModel
*) GetModel();
3328 return list_model
->GetCount();
3332 return m_root
->GetSubTreeCount();
3336 class ItemToRowJob
: public DoJob
3339 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3346 // Maybe binary search will help to speed up this process
3347 virtual int operator() ( wxDataViewTreeNode
* node
)
3350 if( node
->GetItem() == item
)
3355 if( node
->GetItem() == *m_iter
)
3358 return DoJob::CONTINUE
;
3362 ret
+= node
->GetSubTreeCount();
3363 return DoJob::SKIP_SUBTREE
;
3368 // the row number is begin from zero
3369 int GetResult() const
3373 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3374 wxDataViewItem item
;
3379 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3381 const wxDataViewModel
* model
= GetModel();
3385 if (IsVirtualList())
3387 return wxPtrToUInt( item
.GetID() ) -1;
3394 // Compose the parent-chain of the item we are looking for
3395 wxVector
<wxDataViewItem
> parentChain
;
3396 wxDataViewItem
it( item
);
3399 parentChain
.push_back(it
);
3400 it
= model
->GetParent(it
);
3403 // add an 'invalid' item to represent our 'invisible' root node
3404 parentChain
.push_back(wxDataViewItem());
3406 // the parent chain was created by adding the deepest parent first.
3407 // so if we want to start at the root node, we have to iterate backwards through the vector
3408 ItemToRowJob
job( item
, parentChain
.rbegin() );
3409 Walker( m_root
, job
);
3410 return job
.GetResult();
3414 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3415 wxDataViewTreeNode
* node
)
3417 if( !model
->IsContainer( item
) )
3420 wxDataViewItemArray children
;
3421 unsigned int num
= model
->GetChildren( item
, children
);
3423 for ( unsigned int index
= 0; index
< num
; index
++ )
3425 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3427 if( model
->IsContainer(children
[index
]) )
3428 n
->SetHasChildren( true );
3430 node
->InsertChild(n
, index
);
3433 wxASSERT( node
->IsOpen() );
3434 node
->ChangeSubTreeCount(+num
);
3437 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3441 if (GetModel()->IsVirtualListModel())
3447 m_root
= wxDataViewTreeNode::CreateRootNode();
3449 // First we define a invalid item to fetch the top-level elements
3450 wxDataViewItem item
;
3452 BuildTreeHelper( model
, item
, m_root
);
3456 void wxDataViewMainWindow::DestroyTree()
3458 if (!IsVirtualList())
3466 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3468 // Edit the current column editable in 'mode'. If no column is focused
3469 // (typically because the user has full row selected), try to find the
3470 // first editable column (this would typically be a checkbox for
3471 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3472 // focus on the checkbox column; or on the only editable text column).
3474 wxDataViewColumn
*candidate
= m_currentCol
;
3477 !IsCellEditableInMode(item
, candidate
, mode
) &&
3478 !m_currentColSetByKeyboard
)
3480 // If current column was set by mouse to something not editable (in
3481 // 'mode') and the user pressed Space/F2 to edit it, treat the
3482 // situation as if there was whole-row focus, because that's what is
3483 // visually indicated and the mouse click could very well be targeted
3484 // on the row rather than on an individual cell.
3486 // But if it was done by keyboard, respect that even if the column
3487 // isn't editable, because focus is visually on that column and editing
3488 // something else would be surprising.
3494 const unsigned cols
= GetOwner()->GetColumnCount();
3495 for ( unsigned i
= 0; i
< cols
; i
++ )
3497 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3498 if ( c
->IsHidden() )
3501 if ( IsCellEditableInMode(item
, c
, mode
) )
3509 // If on container item without columns, only the expander column
3510 // may be directly editable:
3512 GetOwner()->GetExpanderColumn() != candidate
&&
3513 GetModel()->IsContainer(item
) &&
3514 !GetModel()->HasContainerColumns(item
) )
3516 candidate
= GetOwner()->GetExpanderColumn();
3522 if ( !IsCellEditableInMode(item
, candidate
, mode
) )
3528 bool wxDataViewMainWindow::IsCellEditableInMode(const wxDataViewItem
& item
,
3529 const wxDataViewColumn
*col
,
3530 wxDataViewCellMode mode
) const
3532 if ( col
->GetRenderer()->GetMode() != mode
)
3535 if ( !GetModel()->IsEnabled(item
, col
->GetModelColumn()) )
3541 void wxDataViewMainWindow::OnCharHook(wxKeyEvent
& event
)
3545 // Handle any keys special for the in-place editor and return without
3546 // calling Skip() below.
3547 switch ( event
.GetKeyCode() )
3550 m_editorRenderer
->CancelEditing();
3554 m_editorRenderer
->FinishEditing();
3562 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3564 wxWindow
* const parent
= GetParent();
3566 // propagate the char event upwards
3567 wxKeyEvent
eventForParent(event
);
3568 eventForParent
.SetEventObject(parent
);
3569 if ( parent
->ProcessWindowEvent(eventForParent
) )
3572 if ( parent
->HandleAsNavigationKey(event
) )
3575 // no item -> nothing to do
3576 if (!HasCurrentRow())
3582 // don't use m_linesPerPage directly as it might not be computed yet
3583 const int pageSize
= GetCountPerPage();
3584 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3586 switch ( event
.GetKeyCode() )
3589 if ( event
.HasModifiers() )
3596 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3597 // it. Only if that event is not handled do we activate column renderer (which
3598 // is normally done by Space) or even inline editing.
3600 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3602 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3605 le
.SetEventObject(parent
);
3606 le
.SetModel(GetModel());
3608 if ( parent
->ProcessWindowEvent(le
) )
3610 // else: fall through to WXK_SPACE handling
3614 if ( event
.HasModifiers() )
3621 // Space toggles activatable items or -- if not activatable --
3622 // starts inline editing (this is normally done using F2 on
3623 // Windows, but Space is common everywhere else, so use it too
3624 // for greater cross-platform compatibility).
3626 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3628 // Activate the current activatable column. If not column is focused (typically
3629 // because the user has full row selected), try to find the first activatable
3630 // column (this would typically be a checkbox and we don't want to force the user
3631 // to set focus on the checkbox column).
3632 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3634 if ( activatableCol
)
3636 const unsigned colIdx
= activatableCol
->GetModelColumn();
3637 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3639 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3640 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3641 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3645 // else: fall through to WXK_F2 handling
3649 if ( event
.HasModifiers() )
3656 if( !m_selection
.empty() )
3658 // Mimic Windows 7 behavior: edit the item that has focus
3659 // if it is selected and the first selected item if focus
3660 // is out of selection.
3662 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3665 sel
= m_selection
[0];
3668 const wxDataViewItem item
= GetItemByRow(sel
);
3670 // Edit the current column. If no column is focused
3671 // (typically because the user has full row selected), try
3672 // to find the first editable column.
3673 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3676 GetOwner()->EditItem(item
, editableCol
);
3682 if ( m_currentRow
> 0 )
3683 OnVerticalNavigation( m_currentRow
- 1, event
);
3687 if ( m_currentRow
+ 1 < GetRowCount() )
3688 OnVerticalNavigation( m_currentRow
+ 1, event
);
3690 // Add the process for tree expanding/collapsing
3702 OnVerticalNavigation( GetRowCount() - 1, event
);
3707 OnVerticalNavigation( 0, event
);
3712 int steps
= pageSize
- 1;
3713 int index
= m_currentRow
- steps
;
3717 OnVerticalNavigation( index
, event
);
3723 int steps
= pageSize
- 1;
3724 unsigned int index
= m_currentRow
+ steps
;
3725 unsigned int count
= GetRowCount();
3726 if ( index
>= count
)
3729 OnVerticalNavigation( index
, event
);
3738 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3740 wxCHECK_RET( newCurrent
< GetRowCount(),
3741 wxT("invalid item index in OnVerticalNavigation()") );
3743 // if there is no selection, we cannot move it anywhere
3744 if (!HasCurrentRow())
3747 unsigned int oldCurrent
= m_currentRow
;
3749 // in single selection we just ignore Shift as we can't select several
3751 if ( event
.ShiftDown() && !IsSingleSel() )
3753 RefreshRow( oldCurrent
);
3755 ChangeCurrentRow( newCurrent
);
3757 // select all the items between the old and the new one
3758 if ( oldCurrent
> newCurrent
)
3760 newCurrent
= oldCurrent
;
3761 oldCurrent
= m_currentRow
;
3764 SelectRows( oldCurrent
, newCurrent
, true );
3765 if (oldCurrent
!=newCurrent
)
3766 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3770 RefreshRow( oldCurrent
);
3772 // all previously selected items are unselected unless ctrl is held
3773 if ( !event
.ControlDown() )
3774 SelectAllRows(false);
3776 ChangeCurrentRow( newCurrent
);
3778 if ( !event
.ControlDown() )
3780 SelectRow( m_currentRow
, true );
3781 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3784 RefreshRow( m_currentRow
);
3787 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3790 void wxDataViewMainWindow::OnLeftKey()
3794 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3798 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3800 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3803 // Because TryAdvanceCurrentColumn() return false, we are at the first
3804 // column or using whole-row selection. In this situation, we can use
3805 // the standard TreeView handling of the left key.
3806 if (node
->HasChildren() && node
->IsOpen())
3808 Collapse(m_currentRow
);
3812 // if the node is already closed, we move the selection to its parent
3813 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3817 int parent
= GetRowByItem( parent_node
->GetItem() );
3820 unsigned int row
= m_currentRow
;
3821 SelectRow( row
, false);
3822 SelectRow( parent
, true );
3823 ChangeCurrentRow( parent
);
3824 GetOwner()->EnsureVisible( parent
, -1 );
3825 SendSelectionChangedEvent( parent_node
->GetItem() );
3832 void wxDataViewMainWindow::OnRightKey()
3836 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3840 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3842 if ( node
->HasChildren() )
3844 if ( !node
->IsOpen() )
3846 Expand( m_currentRow
);
3850 // if the node is already open, we move the selection to the first child
3851 unsigned int row
= m_currentRow
;
3852 SelectRow( row
, false );
3853 SelectRow( row
+ 1, true );
3854 ChangeCurrentRow( row
+ 1 );
3855 GetOwner()->EnsureVisible( row
+ 1, -1 );
3856 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3861 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3866 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3868 if ( GetOwner()->GetColumnCount() == 0 )
3871 if ( !m_useCellFocus
)
3876 // navigation shouldn't work in branch nodes without other columns:
3877 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3881 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3885 m_currentCol
= GetOwner()->GetColumnAt(1);
3886 m_currentColSetByKeyboard
= true;
3887 RefreshRow(m_currentRow
);
3894 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3896 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3899 GetOwner()->EnsureVisible(m_currentRow
, idx
);
3903 // We are going to the left of the second column. Reset to whole-row
3904 // focus (which means first column would be edited).
3905 m_currentCol
= NULL
;
3906 RefreshRow(m_currentRow
);
3910 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3911 m_currentColSetByKeyboard
= true;
3912 RefreshRow(m_currentRow
);
3916 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3918 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3920 // let the base handle mouse wheel events.
3925 // set the focus to ourself if any of the mouse buttons are pressed
3926 if(event
.ButtonDown() && !HasFocus())
3929 int x
= event
.GetX();
3930 int y
= event
.GetY();
3931 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3932 wxDataViewColumn
*col
= NULL
;
3935 unsigned int cols
= GetOwner()->GetColumnCount();
3937 for (i
= 0; i
< cols
; i
++)
3939 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3941 continue; // skip it!
3943 if (x
< xpos
+ c
->GetWidth())
3948 xpos
+= c
->GetWidth();
3951 wxDataViewModel
* const model
= GetModel();
3953 const unsigned int current
= GetLineAt( y
);
3954 const wxDataViewItem item
= GetItemByRow(current
);
3956 // Handle right clicking here, before everything else as context menu
3957 // events should be sent even when we click outside of any item, unlike all
3959 if (event
.RightUp())
3961 wxWindow
*parent
= GetParent();
3962 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3963 le
.SetEventObject(parent
);
3966 if ( item
.IsOk() && col
)
3969 le
.SetColumn( col
->GetModelColumn() );
3970 le
.SetDataViewColumn( col
);
3973 model
->GetValue( value
, item
, col
->GetModelColumn() );
3977 parent
->ProcessWindowEvent(le
);
3987 wxDataViewRenderer
*cell
= col
->GetRenderer();
3988 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3990 // Unselect all if below the last row ?
3995 wxDataViewColumn
* const
3996 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3998 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3999 // button) and also determine the offset of the real cell start, skipping
4000 // the indentation and the expander itself.
4001 bool hoverOverExpander
= false;
4003 if ((!IsList()) && (expander
== col
))
4005 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4007 int indent
= node
->GetIndentLevel();
4008 itemOffset
= GetOwner()->GetIndent()*indent
;
4010 if ( node
->HasChildren() )
4012 // we make the rectangle we are looking in a bit bigger than the actual
4013 // visual expander so the user can hit that little thing reliably
4014 wxRect
rect(itemOffset
,
4015 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
4016 m_lineHeight
, m_lineHeight
);
4018 if( rect
.Contains(x
, y
) )
4020 // So the mouse is over the expander
4021 hoverOverExpander
= true;
4022 if (m_underMouse
&& m_underMouse
!= node
)
4024 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4025 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4027 if (m_underMouse
!= node
)
4029 // wxLogMessage("Do the row: %d", current);
4030 RefreshRow(current
);
4032 m_underMouse
= node
;
4036 // Account for the expander as well, even if this item doesn't have it,
4037 // its parent does so it still counts for the offset.
4038 itemOffset
+= m_lineHeight
;
4040 if (!hoverOverExpander
)
4042 if (m_underMouse
!= NULL
)
4044 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4045 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4046 m_underMouse
= NULL
;
4050 #if wxUSE_DRAG_AND_DROP
4051 if (event
.Dragging())
4053 if (m_dragCount
== 0)
4055 // we have to report the raw, physical coords as we want to be
4056 // able to call HitTest(event.m_pointDrag) from the user code to
4057 // get the item being dragged
4058 m_dragStart
= event
.GetPosition();
4063 if (m_dragCount
!= 3)
4066 if (event
.LeftIsDown())
4068 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
4069 &m_dragStart
.x
, &m_dragStart
.y
);
4070 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
4071 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
4073 // Notify cell about drag
4074 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
4075 event
.SetEventObject( m_owner
);
4076 event
.SetItem( itemDragged
);
4077 event
.SetModel( model
);
4078 if (!m_owner
->HandleWindowEvent( event
))
4081 if (!event
.IsAllowed())
4084 wxDataObject
*obj
= event
.GetDataObject();
4088 wxDataViewDropSource
drag( this, drag_item_row
);
4089 drag
.SetData( *obj
);
4090 /* wxDragResult res = */ drag
.DoDragDrop(event
.GetDragFlags());
4099 #endif // wxUSE_DRAG_AND_DROP
4101 bool simulateClick
= false;
4103 if (event
.ButtonDClick())
4105 m_renameTimer
->Stop();
4106 m_lastOnSame
= false;
4109 bool ignore_other_columns
=
4110 ((expander
!= col
) &&
4111 (model
->IsContainer(item
)) &&
4112 (!model
->HasContainerColumns(item
)));
4114 if (event
.LeftDClick())
4116 if(hoverOverExpander
)
4118 // a double click on the expander will be converted into a "simulated" normal click
4119 simulateClick
= true;
4121 else if ( current
== m_lineLastClicked
)
4123 wxWindow
*parent
= GetParent();
4124 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4126 le
.SetColumn( col
->GetModelColumn() );
4127 le
.SetDataViewColumn( col
);
4128 le
.SetEventObject(parent
);
4129 le
.SetModel(GetModel());
4131 parent
->ProcessWindowEvent(le
);
4136 // The first click was on another item, so don't interpret this as
4137 // a double click, but as a simple click instead
4138 simulateClick
= true;
4142 if (event
.LeftUp() && !hoverOverExpander
)
4144 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4146 // select single line
4147 SelectAllRows( false );
4148 SelectRow( m_lineSelectSingleOnUp
, true );
4149 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4152 // If the user click the expander, we do not do editing even if the column
4153 // with expander are editable
4154 if (m_lastOnSame
&& !ignore_other_columns
)
4156 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4157 IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
4159 m_renameTimer
->Start( 100, true );
4163 m_lastOnSame
= false;
4164 m_lineSelectSingleOnUp
= (unsigned int)-1;
4166 else if(!event
.LeftUp())
4168 // This is necessary, because after a DnD operation in
4169 // from and to ourself, the up event is swallowed by the
4170 // DnD code. So on next non-up event (which means here and
4171 // now) m_lineSelectSingleOnUp should be reset.
4172 m_lineSelectSingleOnUp
= (unsigned int)-1;
4175 if (event
.RightDown())
4177 m_lineBeforeLastClicked
= m_lineLastClicked
;
4178 m_lineLastClicked
= current
;
4180 // If the item is already selected, do not update the selection.
4181 // Multi-selections should not be cleared if a selected item is clicked.
4182 if (!IsRowSelected(current
))
4184 SelectAllRows(false);
4185 const unsigned oldCurrent
= m_currentRow
;
4186 ChangeCurrentRow(current
);
4187 SelectRow(m_currentRow
,true);
4188 RefreshRow(oldCurrent
);
4189 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4192 else if (event
.MiddleDown())
4196 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4198 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4200 // hoverOverExpander being true tells us that our node must be
4201 // valid and have children.
4202 // So we don't need any extra checks.
4203 if( node
->IsOpen() )
4208 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4210 m_lineBeforeLastClicked
= m_lineLastClicked
;
4211 m_lineLastClicked
= current
;
4213 unsigned int oldCurrentRow
= m_currentRow
;
4214 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4216 bool cmdModifierDown
= event
.CmdDown();
4217 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4219 if ( IsSingleSel() || !IsRowSelected(current
) )
4221 SelectAllRows( false );
4222 ChangeCurrentRow(current
);
4223 SelectRow(m_currentRow
,true);
4224 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4226 else // multi sel & current is highlighted & no mod keys
4228 m_lineSelectSingleOnUp
= current
;
4229 ChangeCurrentRow(current
); // change focus
4232 else // multi sel & either ctrl or shift is down
4234 if (cmdModifierDown
)
4236 ChangeCurrentRow(current
);
4237 ReverseRowSelection(m_currentRow
);
4238 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4240 else if (event
.ShiftDown())
4242 ChangeCurrentRow(current
);
4244 unsigned int lineFrom
= oldCurrentRow
,
4247 if ( lineTo
< lineFrom
)
4250 lineFrom
= m_currentRow
;
4253 SelectRows(lineFrom
, lineTo
, true);
4254 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4256 else // !ctrl, !shift
4258 // test in the enclosing if should make it impossible
4259 wxFAIL_MSG( wxT("how did we get here?") );
4263 if (m_currentRow
!= oldCurrentRow
)
4264 RefreshRow( oldCurrentRow
);
4266 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4268 // Update selection here...
4270 m_currentColSetByKeyboard
= false;
4272 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4273 (current
== oldCurrentRow
)) && oldWasSelected
;
4275 // Call ActivateCell() after everything else as under GTK+
4276 if ( IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_ACTIVATABLE
) )
4278 // notify cell about click
4279 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4281 wxRect
cell_rect( xpos
+ itemOffset
,
4282 GetLineStart( current
),
4283 col
->GetWidth() - itemOffset
,
4284 GetLineHeight( current
) );
4286 // Report position relative to the cell's custom area, i.e.
4287 // no the entire space as given by the control but the one
4288 // used by the renderer after calculation of alignment etc.
4290 // adjust the rectangle ourselves to account for the alignment
4291 wxRect rectItem
= cell_rect
;
4292 const int align
= cell
->GetAlignment();
4293 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4295 const wxSize size
= cell
->GetSize();
4297 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4299 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4300 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4301 else if ( align
& wxALIGN_RIGHT
)
4302 rectItem
.x
+= cell_rect
.width
- size
.x
;
4303 // else: wxALIGN_LEFT is the default
4306 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4308 if ( align
& wxALIGN_CENTER_VERTICAL
)
4309 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4310 else if ( align
& wxALIGN_BOTTOM
)
4311 rectItem
.y
+= cell_rect
.height
- size
.y
;
4312 // else: wxALIGN_TOP is the default
4316 wxMouseEvent
event2(event
);
4317 event2
.m_x
-= rectItem
.x
;
4318 event2
.m_y
-= rectItem
.y
;
4319 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4321 /* ignore ret */ cell
->WXActivateCell
4326 col
->GetModelColumn(),
4333 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4337 if (HasCurrentRow())
4343 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4347 if (HasCurrentRow())
4353 void wxDataViewMainWindow::OnColumnsCountChanged()
4355 int editableCount
= 0;
4357 const unsigned cols
= GetOwner()->GetColumnCount();
4358 for ( unsigned i
= 0; i
< cols
; i
++ )
4360 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4361 if ( c
->IsHidden() )
4363 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4367 m_useCellFocus
= (editableCount
> 1);
4372 //-----------------------------------------------------------------------------
4374 //-----------------------------------------------------------------------------
4376 WX_DEFINE_LIST(wxDataViewColumnList
)
4378 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4379 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4380 EVT_SIZE(wxDataViewCtrl::OnSize
)
4383 wxDataViewCtrl::~wxDataViewCtrl()
4386 GetModel()->RemoveNotifier( m_notifier
);
4389 m_colsBestWidths
.clear();
4392 void wxDataViewCtrl::Init()
4394 m_cols
.DeleteContents(true);
4397 // No sorting column at start
4398 m_sortingColumnIdx
= wxNOT_FOUND
;
4400 m_headerArea
= NULL
;
4402 m_colsDirty
= false;
4405 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4410 const wxValidator
& validator
,
4411 const wxString
& name
)
4413 // if ( (style & wxBORDER_MASK) == 0)
4414 // style |= wxBORDER_SUNKEN;
4418 if (!wxControl::Create( parent
, id
, pos
, size
,
4419 style
| wxScrolledWindowStyle
, validator
, name
))
4422 SetInitialSize(size
);
4425 MacSetClipChildren( true );
4428 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4430 // We use the cursor keys for moving the selection, not scrolling, so call
4431 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4432 // keyboard events forwarded to us from wxListMainWindow.
4433 DisableKeyboardScrolling();
4435 if (HasFlag(wxDV_NO_HEADER
))
4436 m_headerArea
= NULL
;
4438 m_headerArea
= new wxDataViewHeaderWindow(this);
4440 SetTargetWindow( m_clientArea
);
4442 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4444 sizer
->Add( m_headerArea
, 0, wxGROW
);
4445 sizer
->Add( m_clientArea
, 1, wxGROW
);
4451 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4453 return wxBORDER_THEME
;
4457 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4461 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4464 // we need to process arrows ourselves for scrolling
4465 if ( nMsg
== WM_GETDLGCODE
)
4467 rc
|= DLGC_WANTARROWS
;
4475 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4477 wxSize newsize
= size
;
4478 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4479 newsize
.y
-= m_headerArea
->GetSize().y
;
4484 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4486 // We need to override OnSize so that our scrolled
4487 // window a) does call Layout() to use sizers for
4488 // positioning the controls but b) does not query
4489 // the sizer for their size and use that for setting
4490 // the scrollable area as set that ourselves by
4491 // calling SetScrollbar() further down.
4497 // We must redraw the headers if their height changed. Normally this
4498 // shouldn't happen as the control shouldn't let itself be resized beneath
4499 // its minimal height but avoid the display artefacts that appear if it
4500 // does happen, e.g. because there is really not enough vertical space.
4501 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4502 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4504 m_headerArea
->Refresh();
4508 void wxDataViewCtrl::SetFocus()
4511 m_clientArea
->SetFocus();
4514 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4516 if (!wxDataViewCtrlBase::AssociateModel( model
))
4519 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4521 model
->AddNotifier( m_notifier
);
4523 m_clientArea
->DestroyTree();
4525 m_clientArea
->BuildTree(model
);
4527 m_clientArea
->UpdateDisplay();
4532 #if wxUSE_DRAG_AND_DROP
4534 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4536 return m_clientArea
->EnableDragSource( format
);
4539 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4541 return m_clientArea
->EnableDropTarget( format
);
4544 #endif // wxUSE_DRAG_AND_DROP
4546 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4548 if (!wxDataViewCtrlBase::AppendColumn(col
))
4551 m_cols
.Append( col
);
4552 m_colsBestWidths
.push_back(CachedColWidthInfo());
4553 OnColumnsCountChanged();
4557 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4559 if (!wxDataViewCtrlBase::PrependColumn(col
))
4562 m_cols
.Insert( col
);
4563 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), CachedColWidthInfo());
4564 OnColumnsCountChanged();
4568 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4570 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4573 m_cols
.Insert( pos
, col
);
4574 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, CachedColWidthInfo());
4575 OnColumnsCountChanged();
4579 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4582 m_headerArea
->UpdateColumn(idx
);
4584 m_clientArea
->UpdateDisplay();
4587 void wxDataViewCtrl::OnColumnsCountChanged()
4590 m_headerArea
->SetColumnCount(GetColumnCount());
4592 m_clientArea
->OnColumnsCountChanged();
4595 void wxDataViewCtrl::DoSetExpanderColumn()
4597 m_clientArea
->UpdateDisplay();
4600 void wxDataViewCtrl::DoSetIndent()
4602 m_clientArea
->UpdateDisplay();
4605 unsigned int wxDataViewCtrl::GetColumnCount() const
4607 return m_cols
.GetCount();
4610 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4612 if ( !m_clientArea
)
4615 m_clientArea
->SetRowHeight(lineHeight
);
4620 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4625 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4627 // columns can't be reordered if there is no header window which allows
4629 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4632 return GetColumn(idx
);
4635 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4637 const unsigned count
= m_cols
.size();
4638 for ( unsigned n
= 0; n
< count
; n
++ )
4640 if ( m_cols
[n
] == column
)
4647 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4649 if ( m_colsBestWidths
[idx
].width
!= 0 )
4650 return m_colsBestWidths
[idx
].width
;
4652 const int count
= m_clientArea
->GetRowCount();
4653 wxDataViewColumn
*column
= GetColumn(idx
);
4654 wxDataViewRenderer
*renderer
=
4655 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4657 class MaxWidthCalculator
4660 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4661 wxDataViewMainWindow
*clientArea
,
4662 wxDataViewRenderer
*renderer
,
4663 const wxDataViewModel
*model
,
4668 m_clientArea(clientArea
),
4669 m_renderer(renderer
),
4672 m_expanderSize(expanderSize
)
4676 !clientArea
->IsList() &&
4678 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4681 void UpdateWithWidth(int width
)
4683 m_width
= wxMax(m_width
, width
);
4686 void UpdateWithRow(int row
)
4689 wxDataViewItem item
;
4691 if ( m_isExpanderCol
)
4693 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4694 item
= node
->GetItem();
4695 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4699 item
= m_clientArea
->GetItemByRow(row
);
4702 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4703 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4706 int GetMaxWidth() const { return m_width
; }
4710 const wxDataViewCtrl
*m_dvc
;
4711 wxDataViewMainWindow
*m_clientArea
;
4712 wxDataViewRenderer
*m_renderer
;
4713 const wxDataViewModel
*m_model
;
4715 bool m_isExpanderCol
;
4719 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4720 GetModel(), column
->GetModelColumn(),
4721 m_clientArea
->GetRowHeight());
4723 calculator
.UpdateWithWidth(column
->GetMinWidth());
4726 calculator
.UpdateWithWidth(m_headerArea
->GetColumnTitleWidth(*column
));
4728 // The code below deserves some explanation. For very large controls, we
4729 // simply can't afford to calculate sizes for all items, it takes too
4730 // long. So the best we can do is to check the first and the last N/2
4731 // items in the control for some sufficiently large N and calculate best
4732 // sizes from that. That can result in the calculated best width being too
4733 // small for some outliers, but it's better to get slightly imperfect
4734 // result than to wait several seconds after every update. To avoid highly
4735 // visible miscalculations, we also include all currently visible items
4736 // no matter what. Finally, the value of N is determined dynamically by
4737 // measuring how much time we spent on the determining item widths so far.
4740 int top_part_end
= count
;
4741 static const long CALC_TIMEOUT
= 20/*ms*/;
4742 // don't call wxStopWatch::Time() too often
4743 static const unsigned CALC_CHECK_FREQ
= 100;
4746 // use some hard-coded limit, that's the best we can do without timer
4747 int top_part_end
= wxMin(500, count
);
4748 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4752 for ( row
= 0; row
< top_part_end
; row
++ )
4755 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4756 timer
.Time() > CALC_TIMEOUT
)
4758 #endif // wxUSE_STOPWATCH
4759 calculator
.UpdateWithRow(row
);
4762 // row is the first unmeasured item now; that's our value of N/2
4768 // add bottom N/2 items now:
4769 const int bottom_part_start
= wxMax(row
, count
- row
);
4770 for ( row
= bottom_part_start
; row
< count
; row
++ )
4772 calculator
.UpdateWithRow(row
);
4775 // finally, include currently visible items in the calculation:
4776 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4777 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4778 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4780 first_visible
= wxMax(first_visible
, top_part_end
);
4781 last_visible
= wxMin(bottom_part_start
, last_visible
);
4783 for ( row
= first_visible
; row
< last_visible
; row
++ )
4785 calculator
.UpdateWithRow(row
);
4788 wxLogTrace("dataview",
4789 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4791 count
- bottom_part_start
,
4792 wxMax(0, last_visible
- first_visible
),
4796 int max_width
= calculator
.GetMaxWidth();
4797 if ( max_width
> 0 )
4798 max_width
+= 2 * PADDING_RIGHTLEFT
;
4800 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
].width
= max_width
;
4804 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4805 unsigned int WXUNUSED(new_pos
))
4807 // do _not_ reorder m_cols elements here, they should always be in the
4808 // order in which columns were added, we only display the columns in
4810 m_clientArea
->UpdateDisplay();
4813 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4815 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4819 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4822 if ( m_clientArea
->GetCurrentColumn() == column
)
4823 m_clientArea
->ClearCurrentColumn();
4825 OnColumnsCountChanged();
4830 bool wxDataViewCtrl::ClearColumns()
4832 SetExpanderColumn(NULL
);
4834 m_colsBestWidths
.clear();
4836 m_clientArea
->ClearCurrentColumn();
4838 OnColumnsCountChanged();
4843 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4845 m_colsBestWidths
[idx
].width
= 0;
4846 m_colsBestWidths
[idx
].dirty
= true;
4850 void wxDataViewCtrl::InvalidateColBestWidths()
4852 // mark all columns as dirty:
4853 m_colsBestWidths
.clear();
4854 m_colsBestWidths
.resize(m_cols
.size());
4858 void wxDataViewCtrl::UpdateColWidths()
4860 m_colsDirty
= false;
4862 if ( !m_headerArea
)
4865 const unsigned len
= m_colsBestWidths
.size();
4866 for ( unsigned i
= 0; i
< len
; i
++ )
4868 // Note that we have to have an explicit 'dirty' flag here instead of
4869 // checking if the width==0, as is done in GetBestColumnWidth().
4871 // Testing width==0 wouldn't work correctly if some code called
4872 // GetWidth() after col. width invalidation but before
4873 // wxDataViewCtrl::UpdateColWidths() was called at idle time. This
4874 // would result in the header's column width getting out of sync with
4875 // the control itself.
4876 if ( m_colsBestWidths
[i
].dirty
)
4878 m_headerArea
->UpdateColumn(i
);
4879 m_colsBestWidths
[i
].dirty
= false;
4884 void wxDataViewCtrl::OnInternalIdle()
4886 wxDataViewCtrlBase::OnInternalIdle();
4892 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4894 unsigned int len
= GetColumnCount();
4895 for ( unsigned int i
= 0; i
< len
; i
++ )
4897 wxDataViewColumn
* col
= GetColumnAt(i
);
4905 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4907 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4908 : GetColumn(m_sortingColumnIdx
);
4911 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4913 return GetItemByRow(m_clientArea
->GetCurrentRow());
4916 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4918 const int row
= m_clientArea
->GetRowByItem(item
);
4920 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4921 if ( static_cast<unsigned>(row
) != oldCurrent
)
4923 m_clientArea
->ChangeCurrentRow(row
);
4924 m_clientArea
->RefreshRow(oldCurrent
);
4925 m_clientArea
->RefreshRow(row
);
4929 wxDataViewColumn
*wxDataViewCtrl::GetCurrentColumn() const
4931 return m_clientArea
->GetCurrentColumn();
4934 int wxDataViewCtrl::GetSelectedItemsCount() const
4936 return m_clientArea
->GetSelections().size();
4939 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4942 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4944 const size_t len
= selections
.size();
4945 for ( size_t i
= 0; i
< len
; i
++ )
4947 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4954 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4961 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4963 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4965 wxDataViewItem last_parent
;
4967 int len
= sel
.GetCount();
4968 for( int i
= 0; i
< len
; i
++ )
4970 wxDataViewItem item
= sel
[i
];
4971 wxDataViewItem parent
= GetModel()->GetParent( item
);
4974 if (parent
!= last_parent
)
4975 ExpandAncestors(item
);
4978 last_parent
= parent
;
4979 int row
= m_clientArea
->GetRowByItem( item
);
4981 selection
.Add( static_cast<unsigned int>(row
) );
4984 m_clientArea
->SetSelections( selection
);
4987 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4989 ExpandAncestors( item
);
4991 int row
= m_clientArea
->GetRowByItem( item
);
4994 // Unselect all rows before select another in the single select mode
4995 if (m_clientArea
->IsSingleSel())
4996 m_clientArea
->SelectAllRows(false);
4998 m_clientArea
->SelectRow(row
, true);
5000 // Also set focus to the selected item
5001 m_clientArea
->ChangeCurrentRow( row
);
5005 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
5007 int row
= m_clientArea
->GetRowByItem( item
);
5009 m_clientArea
->SelectRow(row
, false);
5012 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
5014 int row
= m_clientArea
->GetRowByItem( item
);
5017 return m_clientArea
->IsRowSelected(row
);
5022 void wxDataViewCtrl::SetAlternateRowColour(const wxColour
& colour
)
5024 m_alternateRowColour
= colour
;
5027 void wxDataViewCtrl::SelectAll()
5029 m_clientArea
->SelectAllRows(true);
5032 void wxDataViewCtrl::UnselectAll()
5034 m_clientArea
->SelectAllRows(false);
5037 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
5041 if( row
> (int) m_clientArea
->GetRowCount() )
5042 row
= m_clientArea
->GetRowCount();
5044 int first
= m_clientArea
->GetFirstVisibleRow();
5045 int last
= m_clientArea
->GetLastVisibleRow();
5047 m_clientArea
->ScrollTo( row
, column
);
5048 else if( row
> last
)
5049 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
5051 m_clientArea
->ScrollTo( first
, column
);
5054 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
5056 ExpandAncestors( item
);
5058 m_clientArea
->RecalculateDisplay();
5060 int row
= m_clientArea
->GetRowByItem(item
);
5063 if( column
== NULL
)
5064 EnsureVisible(row
, -1);
5066 EnsureVisible( row
, GetColumnIndex(column
) );
5071 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
5072 wxDataViewColumn
* &column
) const
5074 m_clientArea
->HitTest(point
, item
, column
);
5077 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
5078 const wxDataViewColumn
* column
) const
5080 return m_clientArea
->GetItemRect(item
, column
);
5083 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
5085 return m_clientArea
->GetItemByRow( row
);
5088 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
5090 return m_clientArea
->GetRowByItem( item
);
5093 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
5095 ExpandAncestors( item
);
5097 int row
= m_clientArea
->GetRowByItem( item
);
5100 m_clientArea
->Expand(row
);
5101 InvalidateColBestWidths();
5105 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
5107 int row
= m_clientArea
->GetRowByItem( item
);
5110 m_clientArea
->Collapse(row
);
5111 InvalidateColBestWidths();
5115 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
5117 int row
= m_clientArea
->GetRowByItem( item
);
5119 return m_clientArea
->IsExpanded(row
);
5123 void wxDataViewCtrl::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
5125 wxCHECK_RET( item
.IsOk(), "invalid item" );
5126 wxCHECK_RET( column
, "no column provided" );
5128 m_clientArea
->StartEditing(item
, column
);
5131 #endif // !wxUSE_GENERICDATAVIEWCTRL
5133 #endif // wxUSE_DATAVIEWCTRL