1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
47 #include "wx/imaglist.h"
48 #include "wx/headerctrl.h"
50 #include "wx/stopwatch.h"
51 #include "wx/weakref.h"
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 class wxDataViewColumn
;
58 class wxDataViewHeaderWindow
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static const int SCROLL_UNIT_X
= 15;
67 // the cell padding on the left/right
68 static const int PADDING_RIGHTLEFT
= 3;
70 // the expander space margin
71 static const int EXPANDER_MARGIN
= 4;
74 static const int EXPANDER_OFFSET
= 4;
76 static const int EXPANDER_OFFSET
= 1;
79 // Below is the compare stuff.
80 // For the generic implementation, both the leaf nodes and the nodes are sorted for
81 // fast search when needed
82 static wxDataViewModel
* g_model
;
83 static int g_column
= -2;
84 static bool g_asending
= true;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
93 // Return the expander column or, if it is not set, the first column and also
94 // set it as the expander one for the future.
95 wxDataViewColumn
* GetExpanderColumnOrFirstOne(wxDataViewCtrl
* dataview
)
97 wxDataViewColumn
* expander
= dataview
->GetExpanderColumn();
100 // TODO-RTL: last column for RTL support
101 expander
= dataview
->GetColumnAt( 0 );
102 dataview
->SetExpanderColumn(expander
);
108 } // anonymous namespace
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
121 m_sortAscending
= true;
124 int wxDataViewColumn::GetWidth() const
128 case wxCOL_WIDTH_DEFAULT
:
129 return wxDVC_DEFAULT_WIDTH
;
131 case wxCOL_WIDTH_AUTOSIZE
:
132 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
133 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
140 void wxDataViewColumn::UpdateDisplay()
144 int idx
= m_owner
->GetColumnIndex( this );
145 m_owner
->OnColumnChange( idx
);
149 void wxDataViewColumn::SetSortOrder(bool ascending
)
154 // First unset the old sort column if any.
155 int oldSortKey
= m_owner
->GetSortingColumnIndex();
156 if ( oldSortKey
!= wxNOT_FOUND
)
158 m_owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
161 // Now set this one as the new sort column.
162 const int idx
= m_owner
->GetColumnIndex(this);
163 m_owner
->SetSortingColumnIndex(idx
);
166 m_sortAscending
= ascending
;
168 // Call this directly instead of using UpdateDisplay() as we already have
169 // the column index, no need to look it up again.
170 m_owner
->OnColumnChange(idx
);
173 //-----------------------------------------------------------------------------
174 // wxDataViewHeaderWindow
175 //-----------------------------------------------------------------------------
177 class wxDataViewHeaderWindow
: public wxHeaderCtrl
180 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
181 : wxHeaderCtrl(parent
)
185 wxDataViewCtrl
*GetOwner() const
186 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
189 // implement/override wxHeaderCtrl functions by forwarding them to the main
191 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
193 return *(GetOwner()->GetColumn(idx
));
196 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
198 wxDataViewCtrl
* const owner
= GetOwner();
200 int widthContents
= owner
->GetBestColumnWidth(idx
);
201 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
202 owner
->OnColumnChange(idx
);
208 bool SendEvent(wxEventType type
, unsigned int n
)
210 wxDataViewCtrl
* const owner
= GetOwner();
211 wxDataViewEvent
event(type
, owner
->GetId());
213 event
.SetEventObject(owner
);
215 event
.SetDataViewColumn(owner
->GetColumn(n
));
216 event
.SetModel(owner
->GetModel());
218 // for events created by wxDataViewHeaderWindow the
219 // row / value fields are not valid
220 return owner
->ProcessWindowEvent(event
);
223 void OnClick(wxHeaderCtrlEvent
& event
)
225 const unsigned idx
= event
.GetColumn();
227 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
230 // default handling for the column click is to sort by this column or
231 // toggle its sort order
232 wxDataViewCtrl
* const owner
= GetOwner();
233 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
234 if ( !col
->IsSortable() )
236 // no default handling for non-sortable columns
241 if ( col
->IsSortKey() )
243 // already using this column for sorting, just change the order
244 col
->ToggleSortOrder();
246 else // not using this column for sorting yet
248 col
->SetSortOrder(true);
251 wxDataViewModel
* const model
= owner
->GetModel();
255 owner
->OnColumnChange(idx
);
257 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
260 void OnRClick(wxHeaderCtrlEvent
& event
)
262 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
267 void OnResize(wxHeaderCtrlEvent
& event
)
269 wxDataViewCtrl
* const owner
= GetOwner();
271 const unsigned col
= event
.GetColumn();
272 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
273 GetOwner()->OnColumnChange(col
);
276 void OnEndReorder(wxHeaderCtrlEvent
& event
)
278 wxDataViewCtrl
* const owner
= GetOwner();
279 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
280 event
.GetNewOrder());
283 DECLARE_EVENT_TABLE()
284 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
287 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
288 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
289 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
291 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
292 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
294 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
297 //-----------------------------------------------------------------------------
298 // wxDataViewRenameTimer
299 //-----------------------------------------------------------------------------
301 class wxDataViewRenameTimer
: public wxTimer
304 wxDataViewMainWindow
*m_owner
;
307 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
311 //-----------------------------------------------------------------------------
312 // wxDataViewTreeNode
313 //-----------------------------------------------------------------------------
315 class wxDataViewTreeNode
;
316 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
318 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
319 wxDataViewTreeNode
** node2
);
321 class wxDataViewTreeNode
324 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
331 ~wxDataViewTreeNode()
335 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
336 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
347 static wxDataViewTreeNode
* CreateRootNode()
349 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
350 n
->SetHasChildren(true);
351 n
->m_branchData
->open
= true;
355 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
357 const wxDataViewTreeNodes
& GetChildNodes() const
359 wxASSERT( m_branchData
!= NULL
);
360 return m_branchData
->children
;
363 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
366 m_branchData
= new BranchNodeData
;
368 m_branchData
->children
.Insert(node
, index
);
370 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
372 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
375 void RemoveChild(wxDataViewTreeNode
*node
)
377 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
378 m_branchData
->children
.Remove(node
);
381 // returns position of child node for given item in children list or wxNOT_FOUND
382 int FindChildByItem(const wxDataViewItem
& item
) const
387 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
388 const int len
= nodes
.size();
389 for ( int i
= 0; i
< len
; i
++ )
391 if ( nodes
[i
]->m_item
== item
)
397 const wxDataViewItem
& GetItem() const { return m_item
; }
398 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
400 int GetIndentLevel() const
403 const wxDataViewTreeNode
* node
= this;
404 while( node
->GetParent()->GetParent() != NULL
)
406 node
= node
->GetParent();
414 return m_branchData
&& m_branchData
->open
;
419 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
423 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
424 const int len
= nodes
.GetCount();
425 for ( int i
= 0;i
< len
; i
++)
426 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
428 if (m_branchData
->open
)
430 ChangeSubTreeCount(-sum
);
431 m_branchData
->open
= !m_branchData
->open
;
435 m_branchData
->open
= !m_branchData
->open
;
436 ChangeSubTreeCount(+sum
);
440 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
441 // even if GetChildNodes() is empty; see below.
442 bool HasChildren() const
444 return m_branchData
!= NULL
;
447 void SetHasChildren(bool has
)
451 wxDELETE(m_branchData
);
453 else if ( m_branchData
== NULL
)
455 m_branchData
= new BranchNodeData
;
459 int GetSubTreeCount() const
461 return m_branchData
? m_branchData
->subTreeCount
: 0;
464 void ChangeSubTreeCount( int num
)
466 wxASSERT( m_branchData
!= NULL
);
468 if( !m_branchData
->open
)
471 m_branchData
->subTreeCount
+= num
;
472 wxASSERT( m_branchData
->subTreeCount
>= 0 );
475 m_parent
->ChangeSubTreeCount(num
);
485 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
487 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
488 int len
= nodes
.GetCount();
489 for (int i
= 0; i
< len
; i
++)
491 if ( nodes
[i
]->HasChildren() )
499 wxDataViewTreeNode
*m_parent
;
501 // Corresponding model item.
502 wxDataViewItem m_item
;
504 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
505 // separate struct in order to conserve memory.
506 struct BranchNodeData
514 // Child nodes. Note that this may be empty even if m_hasChildren in
515 // case this branch of the tree wasn't expanded and realized yet.
516 wxDataViewTreeNodes children
;
518 // Is the branch node currently open (expanded)?
521 // Total count of expanded (i.e. visible with the help of some
522 // scrolling) items in the subtree, but excluding this node. I.e. it is
523 // 0 for leaves and is the number of rows the subtree occupies for
528 BranchNodeData
*m_branchData
;
532 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
533 wxDataViewTreeNode
** node2
)
535 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
539 //-----------------------------------------------------------------------------
540 // wxDataViewMainWindow
541 //-----------------------------------------------------------------------------
543 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
545 class wxDataViewMainWindow
: public wxWindow
548 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
550 const wxPoint
&pos
= wxDefaultPosition
,
551 const wxSize
&size
= wxDefaultSize
,
552 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
553 virtual ~wxDataViewMainWindow();
555 bool IsList() const { return GetModel()->IsListModel(); }
556 bool IsVirtualList() const { return m_root
== NULL
; }
558 // notifications from wxDataViewModel
559 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
560 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
561 bool ItemChanged( const wxDataViewItem
&item
);
562 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
566 if (!IsVirtualList())
576 g_model
= GetModel();
577 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
580 if (g_model
->HasDefaultCompare())
588 g_column
= col
->GetModelColumn();
589 g_asending
= col
->IsSortOrderAscending();
592 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
593 wxDataViewCtrl
*GetOwner() { return m_owner
; }
594 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
596 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
597 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
599 #if wxUSE_DRAG_AND_DROP
600 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
601 #endif // wxUSE_DRAG_AND_DROP
602 void OnPaint( wxPaintEvent
&event
);
603 void OnCharHook( wxKeyEvent
&event
);
604 void OnChar( wxKeyEvent
&event
);
605 void OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
);
608 void OnMouse( wxMouseEvent
&event
);
609 void OnSetFocus( wxFocusEvent
&event
);
610 void OnKillFocus( wxFocusEvent
&event
);
612 void UpdateDisplay();
613 void RecalculateDisplay();
614 void OnInternalIdle();
616 void OnRenameTimer();
618 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
619 void ScrollTo( int rows
, int column
);
621 unsigned GetCurrentRow() const { return m_currentRow
; }
622 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
623 void ChangeCurrentRow( unsigned int row
);
624 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
626 wxDataViewColumn
*GetCurrentColumn() const { return m_currentCol
; }
627 void ClearCurrentColumn() { m_currentCol
= NULL
; }
629 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
630 bool IsEmpty() { return GetRowCount() == 0; }
632 int GetCountPerPage() const;
633 int GetEndOfLastCol() const;
634 unsigned int GetFirstVisibleRow() const;
636 // I change this method to un const because in the tree view,
637 // the displaying number of the tree are changing along with the
638 // expanding/collapsing of the tree nodes
639 unsigned int GetLastVisibleRow();
640 unsigned int GetRowCount() const;
642 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
643 void SetSelections( const wxDataViewSelection
& sel
)
644 { m_selection
= sel
; UpdateDisplay(); }
645 void Select( const wxArrayInt
& aSelections
);
646 void SelectAllRows( bool on
);
647 void SelectRow( unsigned int row
, bool on
);
648 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
649 void ReverseRowSelection( unsigned int row
);
650 bool IsRowSelected( unsigned int row
);
651 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
653 void RefreshRow( unsigned int row
);
654 void RefreshRows( unsigned int from
, unsigned int to
);
655 void RefreshRowsAfter( unsigned int firstRow
);
657 // returns the colour to be used for drawing the rules
658 wxColour
GetRuleColour() const
660 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
663 wxRect
GetLineRect( unsigned int row
) const;
665 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
666 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
667 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
669 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
670 int GetRowHeight() const { return m_lineHeight
; }
672 // Some useful functions for row and item mapping
673 wxDataViewItem
GetItemByRow( unsigned int row
) const;
674 int GetRowByItem( const wxDataViewItem
& item
) const;
676 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
677 // We did not need this temporarily
678 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
680 // Methods for building the mapping tree
681 void BuildTree( wxDataViewModel
* model
);
683 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
684 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
686 void Expand( unsigned int row
);
687 void Collapse( unsigned int row
);
688 bool IsExpanded( unsigned int row
) const;
689 bool HasChildren( unsigned int row
) const;
691 #if wxUSE_DRAG_AND_DROP
692 bool EnableDragSource( const wxDataFormat
&format
);
693 bool EnableDropTarget( const wxDataFormat
&format
);
695 void RemoveDropHint();
696 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
697 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
698 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
700 #endif // wxUSE_DRAG_AND_DROP
702 void OnColumnsCountChanged();
704 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
705 // specified item in the given column.
706 void StartEditing(const wxDataViewItem
& item
, const wxDataViewColumn
* col
);
709 int RecalculateCount() const;
711 // Return false only if the event was vetoed by its handler.
712 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
714 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
716 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
718 bool IsCellEditableInMode(const wxDataViewItem
& item
, const wxDataViewColumn
*col
, wxDataViewCellMode mode
) const;
720 void DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
);
723 wxDataViewCtrl
*m_owner
;
727 wxDataViewColumn
*m_currentCol
;
728 unsigned int m_currentRow
;
729 wxDataViewSelection m_selection
;
731 wxDataViewRenameTimer
*m_renameTimer
;
736 bool m_currentColSetByKeyboard
;
738 #if wxUSE_DRAG_AND_DROP
743 wxDataFormat m_dragFormat
;
746 wxDataFormat m_dropFormat
;
748 unsigned int m_dropHintLine
;
749 #endif // wxUSE_DRAG_AND_DROP
751 // for double click logic
752 unsigned int m_lineLastClicked
,
753 m_lineBeforeLastClicked
,
754 m_lineSelectSingleOnUp
;
756 // the pen used to draw horiz/vertical rules
759 // the pen used to draw the expander and the lines
762 // This is the tree structure of the model
763 wxDataViewTreeNode
* m_root
;
766 // This is the tree node under the cursor
767 wxDataViewTreeNode
* m_underMouse
;
769 // The control used for editing or NULL.
770 wxWeakRef
<wxWindow
> m_editorCtrl
;
772 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
773 wxDataViewRenderer
* m_editorRenderer
;
776 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
777 DECLARE_EVENT_TABLE()
780 // ---------------------------------------------------------
781 // wxGenericDataViewModelNotifier
782 // ---------------------------------------------------------
784 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
787 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
788 { m_mainWindow
= mainWindow
; }
790 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
791 { return m_mainWindow
->ItemAdded( parent
, item
); }
792 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
793 { return m_mainWindow
->ItemDeleted( parent
, item
); }
794 virtual bool ItemChanged( const wxDataViewItem
& item
)
795 { return m_mainWindow
->ItemChanged(item
); }
796 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
797 { return m_mainWindow
->ValueChanged( item
, col
); }
798 virtual bool Cleared()
799 { return m_mainWindow
->Cleared(); }
800 virtual void Resort()
801 { m_mainWindow
->Resort(); }
803 wxDataViewMainWindow
*m_mainWindow
;
806 // ---------------------------------------------------------
807 // wxDataViewRenderer
808 // ---------------------------------------------------------
810 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
812 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
813 wxDataViewCellMode mode
,
815 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
819 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
823 wxDataViewRenderer::~wxDataViewRenderer()
828 wxDC
*wxDataViewRenderer::GetDC()
832 if (GetOwner() == NULL
)
834 if (GetOwner()->GetOwner() == NULL
)
836 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
842 void wxDataViewRenderer::SetAlignment( int align
)
847 int wxDataViewRenderer::GetAlignment() const
852 // ---------------------------------------------------------
853 // wxDataViewCustomRenderer
854 // ---------------------------------------------------------
856 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
858 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
859 wxDataViewCellMode mode
, int align
) :
860 wxDataViewRenderer( varianttype
, mode
, align
)
864 // ---------------------------------------------------------
865 // wxDataViewTextRenderer
866 // ---------------------------------------------------------
868 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
870 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
871 wxDataViewCellMode mode
, int align
) :
872 wxDataViewRenderer( varianttype
, mode
, align
)
876 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
878 m_text
= value
.GetString();
883 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
888 bool wxDataViewTextRenderer::HasEditorCtrl() const
893 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
894 wxRect labelRect
, const wxVariant
&value
)
896 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
897 wxPoint(labelRect
.x
,labelRect
.y
),
898 wxSize(labelRect
.width
,labelRect
.height
),
899 wxTE_PROCESS_ENTER
);
901 // select the text in the control an place the cursor at the end
902 ctrl
->SetInsertionPointEnd();
908 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
910 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
911 value
= text
->GetValue();
915 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
917 RenderText(m_text
, 0, rect
, dc
, state
);
921 wxSize
wxDataViewTextRenderer::GetSize() const
924 return GetTextExtent(m_text
);
926 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
929 // ---------------------------------------------------------
930 // wxDataViewBitmapRenderer
931 // ---------------------------------------------------------
933 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
935 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
936 wxDataViewCellMode mode
, int align
) :
937 wxDataViewRenderer( varianttype
, mode
, align
)
941 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
943 if (value
.GetType() == wxT("wxBitmap"))
945 if (value
.GetType() == wxT("wxIcon"))
951 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
956 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
959 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
960 else if (m_icon
.IsOk())
961 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
966 wxSize
wxDataViewBitmapRenderer::GetSize() const
969 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
970 else if (m_icon
.IsOk())
971 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
973 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
976 // ---------------------------------------------------------
977 // wxDataViewToggleRenderer
978 // ---------------------------------------------------------
980 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
982 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
983 wxDataViewCellMode mode
, int align
) :
984 wxDataViewRenderer( varianttype
, mode
, align
)
989 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
991 m_toggle
= value
.GetBool();
996 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
1001 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
1005 flags
|= wxCONTROL_CHECKED
;
1006 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
1007 GetEnabled() == false)
1008 flags
|= wxCONTROL_DISABLED
;
1010 // check boxes we draw must always have the same, standard size (if it's
1011 // bigger than the cell size the checkbox will be truncated because the
1012 // caller had set the clipping rectangle to prevent us from drawing outside
1014 cell
.SetSize(GetSize());
1016 wxRendererNative::Get().DrawCheckBox(
1017 GetOwner()->GetOwner(),
1025 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1026 wxDataViewModel
*model
,
1027 const wxDataViewItem
& item
,
1029 const wxMouseEvent
*mouseEvent
)
1033 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1034 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1038 model
->ChangeValue(!m_toggle
, item
, col
);
1042 wxSize
wxDataViewToggleRenderer::GetSize() const
1044 // the window parameter is not used by GetCheckBoxSize() so it's
1045 // safe to pass NULL
1046 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1049 // ---------------------------------------------------------
1050 // wxDataViewProgressRenderer
1051 // ---------------------------------------------------------
1053 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1055 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1056 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1057 wxDataViewRenderer( varianttype
, mode
, align
)
1063 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1065 m_value
= (long) value
;
1067 if (m_value
< 0) m_value
= 0;
1068 if (m_value
> 100) m_value
= 100;
1073 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1075 value
= (long) m_value
;
1080 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1082 // deflate the rect to leave a small border between bars in adjacent rows
1083 wxRect bar
= rect
.Deflate(0, 1);
1085 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1086 dc
->SetPen( *wxBLACK_PEN
);
1087 dc
->DrawRectangle( bar
);
1089 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1090 dc
->SetPen( *wxTRANSPARENT_PEN
);
1092 const wxDataViewItemAttr
& attr
= GetAttr();
1093 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1095 dc
->DrawRectangle( bar
);
1100 wxSize
wxDataViewProgressRenderer::GetSize() const
1102 return wxSize(40,12);
1105 // ---------------------------------------------------------
1106 // wxDataViewIconTextRenderer
1107 // ---------------------------------------------------------
1109 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1111 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1112 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1113 wxDataViewRenderer( varianttype
, mode
, align
)
1116 SetAlignment(align
);
1119 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1125 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1130 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1134 const wxIcon
& icon
= m_value
.GetIcon();
1137 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1138 xoffset
= icon
.GetWidth()+4;
1141 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1146 wxSize
wxDataViewIconTextRenderer::GetSize() const
1148 if (!m_value
.GetText().empty())
1150 wxSize size
= GetTextExtent(m_value
.GetText());
1152 if (m_value
.GetIcon().IsOk())
1153 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1156 return wxSize(80,20);
1159 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1161 wxDataViewIconText iconText
;
1164 wxString text
= iconText
.GetText();
1166 // adjust the label rect to take the width of the icon into account
1167 if (iconText
.GetIcon().IsOk())
1169 int w
= iconText
.GetIcon().GetWidth() + 4;
1171 labelRect
.width
-= w
;
1174 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1175 wxPoint(labelRect
.x
,labelRect
.y
),
1176 wxSize(labelRect
.width
,labelRect
.height
),
1177 wxTE_PROCESS_ENTER
);
1179 // select the text in the control an place the cursor at the end
1180 ctrl
->SetInsertionPointEnd();
1186 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1188 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1190 // The icon can't be edited so get its old value and reuse it.
1192 wxDataViewColumn
* const col
= GetOwner();
1193 GetView()->GetModel()->GetValue(valueOld
, m_item
, col
->GetModelColumn());
1195 wxDataViewIconText iconText
;
1196 iconText
<< valueOld
;
1198 // But replace the text with the value entered by user.
1199 iconText
.SetText(text
->GetValue());
1205 //-----------------------------------------------------------------------------
1206 // wxDataViewDropTarget
1207 //-----------------------------------------------------------------------------
1209 #if wxUSE_DRAG_AND_DROP
1211 class wxBitmapCanvas
: public wxWindow
1214 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1215 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1218 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1221 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1224 dc
.DrawBitmap( m_bitmap
, 0, 0);
1230 class wxDataViewDropSource
: public wxDropSource
1233 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1241 ~wxDataViewDropSource()
1246 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1248 wxPoint pos
= wxGetMousePosition();
1252 int liney
= m_win
->GetLineStart( m_row
);
1254 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1255 m_win
->ClientToScreen( &linex
, &liney
);
1256 m_dist_x
= pos
.x
- linex
;
1257 m_dist_y
= pos
.y
- liney
;
1260 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1262 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1263 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1265 wxFRAME_TOOL_WINDOW
|
1266 wxFRAME_FLOAT_ON_PARENT
|
1267 wxFRAME_NO_TASKBAR
|
1269 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1274 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1275 m_hint
->SetTransparent( 128 );
1281 wxDataViewMainWindow
*m_win
;
1284 int m_dist_x
,m_dist_y
;
1288 class wxDataViewDropTarget
: public wxDropTarget
1291 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1297 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1299 wxDataFormat format
= GetMatchingPair();
1300 if (format
== wxDF_INVALID
)
1302 return m_win
->OnDragOver( format
, x
, y
, def
);
1305 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1307 wxDataFormat format
= GetMatchingPair();
1308 if (format
== wxDF_INVALID
)
1310 return m_win
->OnDrop( format
, x
, y
);
1313 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1315 wxDataFormat format
= GetMatchingPair();
1316 if (format
== wxDF_INVALID
)
1320 return m_win
->OnData( format
, x
, y
, def
);
1323 virtual void OnLeave()
1324 { m_win
->OnLeave(); }
1326 wxDataViewMainWindow
*m_win
;
1329 #endif // wxUSE_DRAG_AND_DROP
1331 //-----------------------------------------------------------------------------
1332 // wxDataViewRenameTimer
1333 //-----------------------------------------------------------------------------
1335 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1340 void wxDataViewRenameTimer::Notify()
1342 m_owner
->OnRenameTimer();
1345 //-----------------------------------------------------------------------------
1346 // wxDataViewMainWindow
1347 //-----------------------------------------------------------------------------
1349 // The tree building helper, declared firstly
1350 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1351 wxDataViewTreeNode
* node
);
1353 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1355 if (row1
> row2
) return 1;
1356 if (row1
== row2
) return 0;
1360 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1362 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1363 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1364 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1365 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1366 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1367 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook
)
1368 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1371 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1372 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1373 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1374 m_selection( wxDataViewSelectionCmp
)
1379 m_editorRenderer
= NULL
;
1381 m_lastOnSame
= false;
1382 m_renameTimer
= new wxDataViewRenameTimer( this );
1384 // TODO: user better initial values/nothing selected
1385 m_currentCol
= NULL
;
1386 m_currentColSetByKeyboard
= false;
1387 m_useCellFocus
= false;
1391 // We would like to use the same line height that Explorer uses. This is
1392 // different from standard ListView control since Vista.
1393 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
1394 m_lineHeight
= wxMax(16, GetCharHeight()) + 6; // 16 = mini icon height
1397 m_lineHeight
= wxMax(16, GetCharHeight()) + 1; // 16 = mini icon height
1399 #if wxUSE_DRAG_AND_DROP
1401 m_dragStart
= wxPoint(0,0);
1403 m_dragEnabled
= false;
1404 m_dropEnabled
= false;
1406 m_dropHintLine
= (unsigned int) -1;
1407 #endif // wxUSE_DRAG_AND_DROP
1409 m_lineLastClicked
= (unsigned int) -1;
1410 m_lineBeforeLastClicked
= (unsigned int) -1;
1411 m_lineSelectSingleOnUp
= (unsigned int) -1;
1415 SetBackgroundColour( *wxWHITE
);
1417 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1419 m_penRule
= wxPen(GetRuleColour());
1421 // compose a pen whichcan draw black lines
1422 // TODO: maybe there is something system colour to use
1423 m_penExpander
= wxPen(wxColour(0,0,0));
1425 m_root
= wxDataViewTreeNode::CreateRootNode();
1427 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1429 m_underMouse
= NULL
;
1433 wxDataViewMainWindow::~wxDataViewMainWindow()
1436 delete m_renameTimer
;
1440 #if wxUSE_DRAG_AND_DROP
1441 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1443 m_dragFormat
= format
;
1444 m_dragEnabled
= format
!= wxDF_INVALID
;
1449 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1451 m_dropFormat
= format
;
1452 m_dropEnabled
= format
!= wxDF_INVALID
;
1455 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1460 void wxDataViewMainWindow::RemoveDropHint()
1465 RefreshRow( m_dropHintLine
);
1466 m_dropHintLine
= (unsigned int) -1;
1470 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1471 wxCoord y
, wxDragResult def
)
1475 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1476 unsigned int row
= GetLineAt( yy
);
1478 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1484 wxDataViewItem item
= GetItemByRow( row
);
1486 wxDataViewModel
*model
= GetModel();
1488 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1489 event
.SetEventObject( m_owner
);
1490 event
.SetItem( item
);
1491 event
.SetModel( model
);
1492 event
.SetDataFormat( format
);
1493 event
.SetDropEffect( def
);
1494 if (!m_owner
->HandleWindowEvent( event
))
1500 if (!event
.IsAllowed())
1507 if (m_dropHint
&& (row
!= m_dropHintLine
))
1508 RefreshRow( m_dropHintLine
);
1510 m_dropHintLine
= row
;
1516 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1522 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1523 unsigned int row
= GetLineAt( yy
);
1525 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1528 wxDataViewItem item
= GetItemByRow( row
);
1530 wxDataViewModel
*model
= GetModel();
1532 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1533 event
.SetEventObject( m_owner
);
1534 event
.SetItem( item
);
1535 event
.SetModel( model
);
1536 event
.SetDataFormat( format
);
1537 if (!m_owner
->HandleWindowEvent( event
))
1540 if (!event
.IsAllowed())
1546 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1551 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1552 unsigned int row
= GetLineAt( yy
);
1554 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1557 wxDataViewItem item
= GetItemByRow( row
);
1559 wxDataViewModel
*model
= GetModel();
1561 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1563 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1564 event
.SetEventObject( m_owner
);
1565 event
.SetItem( item
);
1566 event
.SetModel( model
);
1567 event
.SetDataFormat( format
);
1568 event
.SetDataSize( obj
->GetSize() );
1569 event
.SetDataBuffer( obj
->GetData() );
1570 event
.SetDropEffect( def
);
1571 if (!m_owner
->HandleWindowEvent( event
))
1574 if (!event
.IsAllowed())
1580 void wxDataViewMainWindow::OnLeave()
1585 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1587 int height
= GetLineHeight( row
);
1589 unsigned int cols
= GetOwner()->GetColumnCount();
1591 for (col
= 0; col
< cols
; col
++)
1593 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1594 if (column
->IsHidden())
1595 continue; // skip it!
1596 width
+= column
->GetWidth();
1602 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1603 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1604 indent
= indent
+ m_lineHeight
;
1605 // try to use the m_lineHeight as the expander space
1609 wxBitmap
bitmap( width
, height
);
1610 wxMemoryDC
dc( bitmap
);
1611 dc
.SetFont( GetFont() );
1612 dc
.SetPen( *wxBLACK_PEN
);
1613 dc
.SetBrush( *wxWHITE_BRUSH
);
1614 dc
.DrawRectangle( 0,0,width
,height
);
1616 wxDataViewModel
*model
= m_owner
->GetModel();
1618 wxDataViewColumn
* const
1619 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1622 for (col
= 0; col
< cols
; col
++)
1624 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1625 wxDataViewRenderer
*cell
= column
->GetRenderer();
1627 if (column
->IsHidden())
1628 continue; // skip it!
1630 width
= column
->GetWidth();
1632 if (column
== expander
)
1635 wxDataViewItem item
= GetItemByRow( row
);
1636 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1638 wxRect
item_rect(x
, 0, width
, height
);
1639 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1641 // dc.SetClippingRegion( item_rect );
1642 cell
->WXCallRender(item_rect
, &dc
, 0);
1643 // dc.DestroyClippingRegion();
1651 #endif // wxUSE_DRAG_AND_DROP
1654 // Draw focus rect for individual cell. Unlike native focus rect, we render
1655 // this in foreground text color (typically white) to enhance contrast and
1657 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1659 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1661 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1662 // may be short traits and not really dots
1664 // note that to behave in the same manner as DrawRect(), we must exclude
1665 // the bottom and right borders from the rectangle
1666 wxCoord x1
= rect
.GetLeft(),
1668 x2
= rect
.GetRight(),
1669 y2
= rect
.GetBottom();
1671 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1674 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1675 dc
.DrawPoint(z
, rect
.GetTop());
1677 wxCoord shift
= z
== x2
? 0 : 1;
1678 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1679 dc
.DrawPoint(x2
, z
);
1681 shift
= z
== y2
? 0 : 1;
1682 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1683 dc
.DrawPoint(z
, y2
);
1685 shift
= z
== x1
? 0 : 1;
1686 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1687 dc
.DrawPoint(x1
, z
);
1691 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1693 wxDataViewModel
*model
= GetModel();
1694 wxAutoBufferedPaintDC
dc( this );
1697 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1698 dc
.SetPen( *wxTRANSPARENT_PEN
);
1699 dc
.DrawRectangle(GetClientSize());
1704 // No items to draw.
1709 GetOwner()->PrepareDC( dc
);
1710 dc
.SetFont( GetFont() );
1712 wxRect update
= GetUpdateRegion().GetBox();
1713 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1715 // compute which items needs to be redrawn
1716 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1717 unsigned int item_count
=
1718 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1719 (int)(GetRowCount( ) - item_start
));
1720 unsigned int item_last
= item_start
+ item_count
;
1722 // Send the event to wxDataViewCtrl itself.
1723 wxWindow
* const parent
= GetParent();
1724 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1725 cache_event
.SetEventObject(parent
);
1726 cache_event
.SetCache(item_start
, item_last
- 1);
1727 parent
->ProcessWindowEvent(cache_event
);
1729 // compute which columns needs to be redrawn
1730 unsigned int cols
= GetOwner()->GetColumnCount();
1733 // we assume that we have at least one column below and painting an
1734 // empty control is unnecessary anyhow
1738 unsigned int col_start
= 0;
1739 unsigned int x_start
;
1740 for (x_start
= 0; col_start
< cols
; col_start
++)
1742 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1743 if (col
->IsHidden())
1744 continue; // skip it!
1746 unsigned int w
= col
->GetWidth();
1747 if (x_start
+w
>= (unsigned int)update
.x
)
1753 unsigned int col_last
= col_start
;
1754 unsigned int x_last
= x_start
;
1755 for (; col_last
< cols
; col_last
++)
1757 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1758 if (col
->IsHidden())
1759 continue; // skip it!
1761 if (x_last
> (unsigned int)update
.GetRight())
1764 x_last
+= col
->GetWidth();
1767 // Draw background of alternate rows specially if required
1768 if ( m_owner
->HasFlag(wxDV_ROW_LINES
) )
1770 wxColour altRowColour
= m_owner
->m_alternateRowColour
;
1771 if ( !altRowColour
.IsOk() )
1773 // Determine the alternate rows colour automatically from the
1774 // background colour.
1775 const wxColour bgColour
= m_owner
->GetBackgroundColour();
1777 // Depending on the background, alternate row color
1778 // will be 3% more dark or 50% brighter.
1779 int alpha
= bgColour
.GetRGB() > 0x808080 ? 97 : 150;
1780 altRowColour
= bgColour
.ChangeLightness(alpha
);
1783 dc
.SetPen(*wxTRANSPARENT_PEN
);
1784 dc
.SetBrush(wxBrush(altRowColour
));
1786 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1790 dc
.DrawRectangle(x_start
,
1792 GetClientSize().GetWidth(),
1793 GetLineHeight(item
));
1798 // Draw horizontal rules if required
1799 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1801 dc
.SetPen(m_penRule
);
1802 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1804 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1806 int y
= GetLineStart( i
);
1807 dc
.DrawLine(x_start
, y
, x_last
, y
);
1811 // Draw vertical rules if required
1812 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1814 dc
.SetPen(m_penRule
);
1815 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1817 // NB: Vertical rules are drawn in the last pixel of a column so that
1818 // they align perfectly with native MSW wxHeaderCtrl as well as for
1819 // consistency with MSW native list control. There's no vertical
1820 // rule at the most-left side of the control.
1822 int x
= x_start
- 1;
1823 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1825 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1826 if (col
->IsHidden())
1827 continue; // skip it
1829 x
+= col
->GetWidth();
1831 dc
.DrawLine(x
, GetLineStart( item_start
),
1832 x
, GetLineStart( item_last
) );
1836 // redraw the background for the items which are selected/current
1837 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1839 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1841 if (selected
|| item
== m_currentRow
)
1843 wxRect
rect( x_start
, GetLineStart( item
),
1844 x_last
- x_start
, GetLineHeight( item
) );
1846 // draw selection and whole-item focus:
1849 int flags
= wxCONTROL_SELECTED
;
1851 flags
|= wxCONTROL_FOCUSED
;
1853 wxRendererNative::Get().DrawItemSelectionRect
1862 // draw keyboard focus rect if applicable
1863 if ( item
== m_currentRow
&& m_hasFocus
)
1865 bool renderColumnFocus
= false;
1867 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1869 renderColumnFocus
= true;
1871 // If this is container node without columns, render full-row focus:
1874 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1875 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1876 renderColumnFocus
= false;
1880 if ( renderColumnFocus
)
1882 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1884 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1885 if ( col
->IsHidden() )
1888 rect
.width
= col
->GetWidth();
1890 if ( col
== m_currentCol
)
1892 // make the rect more visible by adding a small
1893 // margin around it:
1898 // DrawFocusRect() uses XOR and is all but
1899 // invisible against dark-blue background. Use
1900 // the same color used for selected text.
1901 DrawSelectedCellFocusRect(dc
, rect
);
1905 wxRendererNative::Get().DrawFocusRect
1916 rect
.x
+= rect
.width
;
1921 // render focus rectangle for the whole row
1922 wxRendererNative::Get().DrawFocusRect
1927 selected
? (int)wxCONTROL_SELECTED
: 0
1934 #if wxUSE_DRAG_AND_DROP
1937 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1938 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1939 dc
.SetPen( *wxBLACK_PEN
);
1940 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1941 dc
.DrawRectangle( rect
);
1943 #endif // wxUSE_DRAG_AND_DROP
1945 wxDataViewColumn
* const
1946 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1948 // redraw all cells for all rows which must be repainted and all columns
1950 cell_rect
.x
= x_start
;
1951 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1953 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1954 wxDataViewRenderer
*cell
= col
->GetRenderer();
1955 cell_rect
.width
= col
->GetWidth();
1957 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1958 continue; // skip it!
1960 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1962 // get the cell value and set it into the renderer
1963 wxDataViewTreeNode
*node
= NULL
;
1964 wxDataViewItem dataitem
;
1966 if (!IsVirtualList())
1968 node
= GetTreeNodeByRow(item
);
1972 dataitem
= node
->GetItem();
1974 // Skip all columns of "container" rows except the expander
1975 // column itself unless HasContainerColumns() overrides this.
1976 if ( col
!= expander
&&
1977 model
->IsContainer(dataitem
) &&
1978 !model
->HasContainerColumns(dataitem
) )
1983 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1986 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1989 cell_rect
.y
= GetLineStart( item
);
1990 cell_rect
.height
= GetLineHeight( item
);
1992 // draw the background
1993 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1995 DrawCellBackground( cell
, dc
, cell_rect
);
1997 // deal with the expander
1999 if ((!IsList()) && (col
== expander
))
2001 // Calculate the indent first
2002 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
2004 // we reserve m_lineHeight of horizontal space for the expander
2005 // but leave EXPANDER_MARGIN around the expander itself
2006 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
2008 indent
+= m_lineHeight
;
2010 // draw expander if needed and visible
2011 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
2013 dc
.SetPen( m_penExpander
);
2014 dc
.SetBrush( wxNullBrush
);
2016 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2017 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
2018 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
2020 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
2023 if ( m_underMouse
== node
)
2024 flag
|= wxCONTROL_CURRENT
;
2025 if ( node
->IsOpen() )
2026 flag
|= wxCONTROL_EXPANDED
;
2028 // ensure that we don't overflow the cell (which might
2029 // happen if the column is very narrow)
2030 wxDCClipper
clip(dc
, cell_rect
);
2032 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2035 // force the expander column to left-center align
2036 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2039 wxRect item_rect
= cell_rect
;
2040 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
2042 // account for the tree indent (harmless if we're not indented)
2043 item_rect
.x
+= indent
;
2044 item_rect
.width
-= indent
;
2046 if ( item_rect
.width
<= 0 )
2050 if (m_hasFocus
&& selected
)
2051 state
|= wxDATAVIEW_CELL_SELECTED
;
2053 // TODO: it would be much more efficient to create a clipping
2054 // region for the entire column being rendered (in the OnPaint
2055 // of wxDataViewMainWindow) instead of a single clip region for
2056 // each cell. However it would mean that each renderer should
2057 // respect the given wxRect's top & bottom coords, eventually
2058 // violating only the left & right coords - however the user can
2059 // make its own renderer and thus we cannot be sure of that.
2060 wxDCClipper
clip(dc
, item_rect
);
2062 cell
->WXCallRender(item_rect
, &dc
, state
);
2065 cell_rect
.x
+= cell_rect
.width
;
2070 void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
)
2072 wxRect
rectBg( rect
);
2074 // don't overlap the horizontal rules
2075 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2081 // don't overlap the vertical rules
2082 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2088 cell
->RenderBackground(&dc
, rectBg
);
2091 void wxDataViewMainWindow::OnRenameTimer()
2093 // We have to call this here because changes may just have
2094 // been made and no screen update taken place.
2097 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2098 // (needs to be tested!)
2102 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2104 StartEditing( item
, m_currentCol
);
2108 wxDataViewMainWindow::StartEditing(const wxDataViewItem
& item
,
2109 const wxDataViewColumn
* col
)
2111 wxDataViewRenderer
* renderer
= col
->GetRenderer();
2112 if ( !IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
2115 const wxRect itemRect
= GetItemRect(item
, col
);
2116 if ( renderer
->StartEditing(item
, itemRect
) )
2118 // Save the renderer to be able to finish/cancel editing it later and
2119 // save the control to be able to detect if we're still editing it.
2120 m_editorRenderer
= renderer
;
2121 m_editorCtrl
= renderer
->GetEditorCtrl();
2125 //-----------------------------------------------------------------------------
2126 // Helper class for do operation on the tree node
2127 //-----------------------------------------------------------------------------
2132 virtual ~DoJob() { }
2134 // The return value control how the tree-walker tranverse the tree
2137 DONE
, // Job done, stop traversing and return
2138 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2139 CONTINUE
// Job not done, continue
2142 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2145 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2147 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2149 switch( func( node
) )
2153 case DoJob::SKIP_SUBTREE
:
2155 case DoJob::CONTINUE
:
2159 if ( node
->HasChildren() )
2161 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2163 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2167 if ( Walker(*i
, func
) )
2175 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2177 if (IsVirtualList())
2179 wxDataViewVirtualListModel
*list_model
=
2180 (wxDataViewVirtualListModel
*) GetModel();
2181 m_count
= list_model
->GetCount();
2187 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2192 wxDataViewItemArray modelSiblings
;
2193 GetModel()->GetChildren(parent
, modelSiblings
);
2194 const int modelSiblingsSize
= modelSiblings
.size();
2196 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2197 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2199 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2200 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2202 parentNode
->SetHasChildren(true);
2204 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2205 const int nodeSiblingsSize
= nodeSiblings
.size();
2209 if ( posInModel
== modelSiblingsSize
- 1 )
2211 nodePos
= nodeSiblingsSize
;
2213 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2215 // This is the simple case when our node tree already matches the
2216 // model and only this one item is missing.
2217 nodePos
= posInModel
;
2221 // It's possible that a larger discrepancy between the model and
2222 // our realization exists. This can happen e.g. when adding a bunch
2223 // of items to the model and then calling ItemsAdded() just once
2224 // afterwards. In this case, we must find the right position by
2225 // looking at sibling items.
2227 // append to the end if we won't find a better position:
2228 nodePos
= nodeSiblingsSize
;
2230 for ( int nextItemPos
= posInModel
+ 1;
2231 nextItemPos
< modelSiblingsSize
;
2234 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2235 if ( nextNodePos
!= wxNOT_FOUND
)
2237 nodePos
= nextNodePos
;
2243 parentNode
->ChangeSubTreeCount(+1);
2244 parentNode
->InsertChild(itemNode
, nodePos
);
2249 GetOwner()->InvalidateColBestWidths();
2255 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2256 const wxDataViewItem
& item
)
2258 if (IsVirtualList())
2260 wxDataViewVirtualListModel
*list_model
=
2261 (wxDataViewVirtualListModel
*) GetModel();
2262 m_count
= list_model
->GetCount();
2264 if ( !m_selection
.empty() )
2266 const int row
= GetRowByItem(item
);
2268 int rowIndexInSelection
= wxNOT_FOUND
;
2270 const size_t selCount
= m_selection
.size();
2271 for ( size_t i
= 0; i
< selCount
; i
++ )
2273 if ( m_selection
[i
] == (unsigned)row
)
2274 rowIndexInSelection
= i
;
2275 else if ( m_selection
[i
] > (unsigned)row
)
2279 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2280 m_selection
.RemoveAt(rowIndexInSelection
);
2284 else // general case
2286 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2288 // Notice that it is possible that the item being deleted is not in the
2289 // tree at all, for example we could be deleting a never shown (because
2290 // collapsed) item in a tree model. So it's not an error if we don't know
2291 // about this item, just return without doing anything then.
2295 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2296 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2298 // We can't use FindNode() to find 'item', because it was already
2299 // removed from the model by the time ItemDeleted() is called, so we
2300 // have to do it manually. We keep track of its position as well for
2302 int itemPosInNode
= 0;
2303 wxDataViewTreeNode
*itemNode
= NULL
;
2304 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2305 i
!= parentsChildren
.end();
2306 ++i
, ++itemPosInNode
)
2308 if( (*i
)->GetItem() == item
)
2315 // If the parent wasn't expanded, it's possible that we didn't have a
2316 // node corresponding to 'item' and so there's nothing left to do.
2319 // If this was the last child to be removed, it's possible the parent
2320 // node became a leaf. Let's ask the model about it.
2321 if ( parentNode
->GetChildNodes().empty() )
2322 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2327 // Delete the item from wxDataViewTreeNode representation:
2328 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2330 parentNode
->RemoveChild(itemNode
);
2332 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2334 // Make the row number invalid and get a new valid one when user call GetRowCount
2337 // If this was the last child to be removed, it's possible the parent
2338 // node became a leaf. Let's ask the model about it.
2339 if ( parentNode
->GetChildNodes().empty() )
2340 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2342 // Update selection by removing 'item' and its entire children tree from the selection.
2343 if ( !m_selection
.empty() )
2345 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2346 // the parent ('parentNode') and position in its list of children
2348 if ( itemPosInNode
== 0 )
2350 // 1st child, row number is that of the parent parentNode + 1
2351 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2355 // row number is that of the sibling above 'item' + its subtree if any + 1
2356 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2358 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2359 siblingNode
->GetSubTreeCount() +
2363 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2365 const size_t numSelections
= m_selection
.size();
2366 for ( size_t i
= 0; i
< numSelections
; ++i
)
2368 const int s
= m_selection
[i
];
2370 newsel
.push_back(s
);
2371 else if ( s
>= itemRow
+ itemsDeleted
)
2372 newsel
.push_back(s
- itemsDeleted
);
2373 // else: deleted item, remove from selection
2376 m_selection
= newsel
;
2380 // Change the current row to the last row if the current exceed the max row number
2381 if( m_currentRow
> GetRowCount() )
2382 ChangeCurrentRow(m_count
- 1);
2384 GetOwner()->InvalidateColBestWidths();
2390 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2395 GetOwner()->InvalidateColBestWidths();
2398 wxWindow
*parent
= GetParent();
2399 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2400 le
.SetEventObject(parent
);
2401 le
.SetModel(GetModel());
2403 parent
->ProcessWindowEvent(le
);
2408 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2410 int view_column
= -1;
2411 unsigned int n_col
= m_owner
->GetColumnCount();
2412 for (unsigned i
= 0; i
< n_col
; i
++)
2414 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2415 if (column
->GetModelColumn() == model_column
)
2417 view_column
= (int) i
;
2421 if (view_column
== -1)
2424 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2425 /*#define MAX_VIRTUAL_WIDTH 100000
2427 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2428 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2429 Refresh( true, &rect );
2436 GetOwner()->InvalidateColBestWidth(view_column
);
2439 wxWindow
*parent
= GetParent();
2440 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2441 le
.SetEventObject(parent
);
2442 le
.SetModel(GetModel());
2444 le
.SetColumn(view_column
);
2445 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2446 parent
->ProcessWindowEvent(le
);
2451 bool wxDataViewMainWindow::Cleared()
2454 m_selection
.Clear();
2457 BuildTree( GetModel() );
2459 GetOwner()->InvalidateColBestWidths();
2465 void wxDataViewMainWindow::UpdateDisplay()
2468 m_underMouse
= NULL
;
2471 void wxDataViewMainWindow::OnInternalIdle()
2473 wxWindow::OnInternalIdle();
2477 RecalculateDisplay();
2482 void wxDataViewMainWindow::RecalculateDisplay()
2484 wxDataViewModel
*model
= GetModel();
2491 int width
= GetEndOfLastCol();
2492 int height
= GetLineStart( GetRowCount() );
2494 SetVirtualSize( width
, height
);
2495 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2500 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2502 m_underMouse
= NULL
;
2504 wxWindow::ScrollWindow( dx
, dy
, rect
);
2506 if (GetOwner()->m_headerArea
)
2507 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2510 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2512 m_underMouse
= NULL
;
2515 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2516 int sy
= GetLineStart( rows
)/y
;
2520 wxRect rect
= GetClientRect();
2524 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2525 for (x_start
= 0; colnum
< column
; colnum
++)
2527 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2528 if (col
->IsHidden())
2529 continue; // skip it!
2531 w
= col
->GetWidth();
2535 int x_end
= x_start
+ w
;
2536 xe
= xx
+ rect
.width
;
2539 sx
= ( xx
+ x_end
- xe
)/x
;
2546 m_owner
->Scroll( sx
, sy
);
2549 int wxDataViewMainWindow::GetCountPerPage() const
2551 wxSize size
= GetClientSize();
2552 return size
.y
/ m_lineHeight
;
2555 int wxDataViewMainWindow::GetEndOfLastCol() const
2559 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2561 const wxDataViewColumn
*c
=
2562 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2565 width
+= c
->GetWidth();
2570 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2574 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2576 return GetLineAt( y
);
2579 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2581 wxSize client_size
= GetClientSize();
2582 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2583 &client_size
.x
, &client_size
.y
);
2585 // we should deal with the pixel here
2586 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2588 return wxMin( GetRowCount()-1, row
);
2591 unsigned int wxDataViewMainWindow::GetRowCount() const
2593 if ( m_count
== -1 )
2595 wxDataViewMainWindow
* const
2596 self
= const_cast<wxDataViewMainWindow
*>(this);
2597 self
->m_count
= RecalculateCount();
2598 self
->UpdateDisplay();
2603 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2610 void wxDataViewMainWindow::SelectAllRows( bool on
)
2617 m_selection
.Clear();
2618 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2619 m_selection
.Add( i
);
2624 unsigned int first_visible
= GetFirstVisibleRow();
2625 unsigned int last_visible
= GetLastVisibleRow();
2627 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2629 unsigned int row
= m_selection
[i
];
2630 if ((row
>= first_visible
) && (row
<= last_visible
))
2633 m_selection
.Clear();
2637 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2639 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2643 m_selection
.Add( row
);
2651 m_selection
.Remove( row
);
2657 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2661 unsigned int tmp
= from
;
2667 for (i
= from
; i
<= to
; i
++)
2669 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2672 m_selection
.Add( i
);
2677 m_selection
.Remove( i
);
2680 RefreshRows( from
, to
);
2683 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2685 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2687 int n
= aSelections
[i
];
2689 m_selection
.Add( n
);
2694 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2696 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2697 m_selection
.Add( row
);
2699 m_selection
.Remove( row
);
2703 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2705 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2708 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2710 wxWindow
*parent
= GetParent();
2711 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2713 le
.SetEventObject(parent
);
2714 le
.SetModel(GetModel());
2717 parent
->ProcessWindowEvent(le
);
2720 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2722 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2723 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2725 wxSize client_size
= GetClientSize();
2726 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2727 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2728 if (intersect_rect
.width
> 0)
2729 Refresh( true, &intersect_rect
);
2732 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2736 unsigned int tmp
= to
;
2741 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2742 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2744 wxSize client_size
= GetClientSize();
2745 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2746 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2747 if (intersect_rect
.width
> 0)
2748 Refresh( true, &intersect_rect
);
2751 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2753 wxSize client_size
= GetClientSize();
2754 int start
= GetLineStart( firstRow
);
2755 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2756 if (start
> client_size
.y
) return;
2758 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2760 Refresh( true, &rect
);
2763 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2767 rect
.y
= GetLineStart( row
);
2768 rect
.width
= GetEndOfLastCol();
2769 rect
.height
= GetLineHeight( row
);
2774 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2776 const wxDataViewModel
*model
= GetModel();
2778 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2780 // TODO make more efficient
2785 for (r
= 0; r
< row
; r
++)
2787 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2788 if (!node
) return start
;
2790 wxDataViewItem item
= node
->GetItem();
2792 unsigned int cols
= GetOwner()->GetColumnCount();
2794 int height
= m_lineHeight
;
2795 for (col
= 0; col
< cols
; col
++)
2797 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2798 if (column
->IsHidden())
2799 continue; // skip it!
2802 model
->IsContainer(item
) &&
2803 !model
->HasContainerColumns(item
))
2804 continue; // skip it!
2806 wxDataViewRenderer
*renderer
=
2807 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2808 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2810 height
= wxMax( height
, renderer
->GetSize().y
);
2820 return row
* m_lineHeight
;
2824 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2826 const wxDataViewModel
*model
= GetModel();
2828 // check for the easy case first
2829 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2830 return y
/ m_lineHeight
;
2832 // TODO make more efficient
2833 unsigned int row
= 0;
2834 unsigned int yy
= 0;
2837 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2840 // not really correct...
2841 return row
+ ((y
-yy
) / m_lineHeight
);
2844 wxDataViewItem item
= node
->GetItem();
2846 unsigned int cols
= GetOwner()->GetColumnCount();
2848 int height
= m_lineHeight
;
2849 for (col
= 0; col
< cols
; col
++)
2851 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2852 if (column
->IsHidden())
2853 continue; // skip it!
2856 model
->IsContainer(item
) &&
2857 !model
->HasContainerColumns(item
))
2858 continue; // skip it!
2860 wxDataViewRenderer
*renderer
=
2861 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2862 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2864 height
= wxMax( height
, renderer
->GetSize().y
);
2875 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2877 const wxDataViewModel
*model
= GetModel();
2879 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2881 wxASSERT( !IsVirtualList() );
2883 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2884 // wxASSERT( node );
2885 if (!node
) return m_lineHeight
;
2887 wxDataViewItem item
= node
->GetItem();
2889 int height
= m_lineHeight
;
2891 unsigned int cols
= GetOwner()->GetColumnCount();
2893 for (col
= 0; col
< cols
; col
++)
2895 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2896 if (column
->IsHidden())
2897 continue; // skip it!
2900 model
->IsContainer(item
) &&
2901 !model
->HasContainerColumns(item
))
2902 continue; // skip it!
2904 wxDataViewRenderer
*renderer
=
2905 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2906 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2908 height
= wxMax( height
, renderer
->GetSize().y
);
2915 return m_lineHeight
;
2920 class RowToTreeNodeJob
: public DoJob
2923 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2926 this->current
= current
;
2931 virtual int operator() ( wxDataViewTreeNode
* node
)
2934 if( current
== static_cast<int>(row
))
2940 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2942 current
+= node
->GetSubTreeCount();
2943 return DoJob::SKIP_SUBTREE
;
2949 // If the current node has only leaf children, we can find the
2950 // desired node directly. This can speed up finding the node
2951 // in some cases, and will have a very good effect for list views.
2952 if ( node
->HasChildren() &&
2953 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2955 const int index
= static_cast<int>(row
) - current
- 1;
2956 ret
= node
->GetChildNodes()[index
];
2960 return DoJob::CONTINUE
;
2964 wxDataViewTreeNode
* GetResult() const
2970 wxDataViewTreeNode
* ret
;
2971 wxDataViewTreeNode
* parent
;
2974 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2976 wxASSERT( !IsVirtualList() );
2978 RowToTreeNodeJob
job( row
, -2, m_root
);
2979 Walker( m_root
, job
);
2980 return job
.GetResult();
2983 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2985 wxDataViewItem item
;
2986 if (IsVirtualList())
2988 if ( row
< GetRowCount() )
2989 item
= wxDataViewItem(wxUIntToPtr(row
+1));
2993 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2995 item
= node
->GetItem();
3002 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
3003 const wxDataViewItem
& item
)
3005 wxWindow
*parent
= GetParent();
3006 wxDataViewEvent
le(type
, parent
->GetId());
3008 le
.SetEventObject(parent
);
3009 le
.SetModel(GetModel());
3012 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
3015 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
3020 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3024 if (!node
->HasChildren())
3027 return node
->IsOpen();
3030 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
3035 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3039 if (!node
->HasChildren())
3045 void wxDataViewMainWindow::Expand( unsigned int row
)
3050 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3054 if (!node
->HasChildren())
3057 if (!node
->IsOpen())
3059 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
3061 // Vetoed by the event handler.
3067 // build the children of current node
3068 if( node
->GetChildNodes().empty() )
3071 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
3074 // By expanding the node all row indices that are currently in the selection list
3075 // and are greater than our node have become invalid. So we have to correct that now.
3076 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3077 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3079 const unsigned testRow
= m_selection
[i
];
3080 // all rows above us are not affected, so skip them
3084 m_selection
[i
] += rowAdjustment
;
3087 if(m_currentRow
> row
)
3088 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3092 // Send the expanded event
3093 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3097 void wxDataViewMainWindow::Collapse(unsigned int row
)
3102 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3106 if (!node
->HasChildren())
3111 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3113 // Vetoed by the event handler.
3117 // Find out if there are selected items below the current node.
3118 bool selectCollapsingRow
= false;
3119 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3120 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3121 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3123 const unsigned testRow
= m_selection
[i
];
3124 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3126 selectCollapsingRow
= true;
3127 // get out as soon as we have found a node that is selected
3134 // If the node to be closed has selected items the user won't see those any longer.
3135 // We select the collapsing node in this case.
3136 if(selectCollapsingRow
)
3138 SelectAllRows(false);
3139 ChangeCurrentRow(row
);
3140 SelectRow(row
, true);
3141 SendSelectionChangedEvent(GetItemByRow(row
));
3145 // if there were no selected items below our node we still need to "fix" the
3146 // selection list to adjust for the changing of the row indices.
3147 // We actually do the opposite of what we are doing in Expand().
3148 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3150 const unsigned testRow
= m_selection
[i
];
3151 // all rows above us are not affected, so skip them
3155 m_selection
[i
] -= rowAdjustment
;
3158 // if the "current row" is being collapsed away we change it to the current row ;-)
3159 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3160 ChangeCurrentRow(row
);
3161 else if(m_currentRow
> row
)
3162 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3167 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3171 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3173 const wxDataViewModel
* model
= GetModel();
3180 // Compose the parent-chain for the item we are looking for
3181 wxVector
<wxDataViewItem
> parentChain
;
3182 wxDataViewItem
it( item
);
3185 parentChain
.push_back(it
);
3186 it
= model
->GetParent(it
);
3189 // Find the item along the parent-chain.
3190 // This algorithm is designed to speed up the node-finding method
3191 wxDataViewTreeNode
* node
= m_root
;
3192 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3194 if( node
->HasChildren() )
3196 if( node
->GetChildNodes().empty() )
3198 // Even though the item is a container, it doesn't have any
3199 // child nodes in the control's representation yet. We have
3200 // to realize its subtree now.
3202 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3205 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3208 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3210 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3211 if (currentNode
->GetItem() == parentChain
[iter
])
3213 if (currentNode
->GetItem() == item
)
3233 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3234 wxDataViewColumn
* &column
)
3236 wxDataViewColumn
*col
= NULL
;
3237 unsigned int cols
= GetOwner()->GetColumnCount();
3238 unsigned int colnum
= 0;
3240 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3241 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3243 col
= GetOwner()->GetColumnAt(colnum
);
3244 if (col
->IsHidden())
3245 continue; // skip it!
3247 unsigned int w
= col
->GetWidth();
3248 if (x_start
+w
>= (unsigned int)x
)
3255 item
= GetItemByRow( GetLineAt( y
) );
3258 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3259 const wxDataViewColumn
* column
)
3264 unsigned int cols
= GetOwner()->GetColumnCount();
3265 // If column is null the loop will compute the combined width of all columns.
3266 // Otherwise, it will compute the x position of the column we are looking for.
3267 for (unsigned int i
= 0; i
< cols
; i
++)
3269 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3274 if (col
->IsHidden())
3275 continue; // skip it!
3277 xpos
+= col
->GetWidth();
3278 width
+= col
->GetWidth();
3283 // If we have a column, we need can get its width directly.
3284 if(column
->IsHidden())
3287 width
= column
->GetWidth();
3292 // If we have no column, we reset the x position back to zero.
3296 // we have to take an expander column into account and compute its indentation
3297 // to get the correct x position where the actual text is
3299 int row
= GetRowByItem(item
);
3301 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3303 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3304 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3305 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3308 wxRect
itemRect( xpos
+ indent
,
3309 GetLineStart( row
),
3311 GetLineHeight( row
) );
3313 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3314 &itemRect
.x
, &itemRect
.y
);
3319 int wxDataViewMainWindow::RecalculateCount() const
3321 if (IsVirtualList())
3323 wxDataViewVirtualListModel
*list_model
=
3324 (wxDataViewVirtualListModel
*) GetModel();
3326 return list_model
->GetCount();
3330 return m_root
->GetSubTreeCount();
3334 class ItemToRowJob
: public DoJob
3337 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3344 // Maybe binary search will help to speed up this process
3345 virtual int operator() ( wxDataViewTreeNode
* node
)
3348 if( node
->GetItem() == item
)
3353 if( node
->GetItem() == *m_iter
)
3356 return DoJob::CONTINUE
;
3360 ret
+= node
->GetSubTreeCount();
3361 return DoJob::SKIP_SUBTREE
;
3366 // the row number is begin from zero
3367 int GetResult() const
3371 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3372 wxDataViewItem item
;
3377 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3379 const wxDataViewModel
* model
= GetModel();
3383 if (IsVirtualList())
3385 return wxPtrToUInt( item
.GetID() ) -1;
3392 // Compose the parent-chain of the item we are looking for
3393 wxVector
<wxDataViewItem
> parentChain
;
3394 wxDataViewItem
it( item
);
3397 parentChain
.push_back(it
);
3398 it
= model
->GetParent(it
);
3401 // add an 'invalid' item to represent our 'invisible' root node
3402 parentChain
.push_back(wxDataViewItem());
3404 // the parent chain was created by adding the deepest parent first.
3405 // so if we want to start at the root node, we have to iterate backwards through the vector
3406 ItemToRowJob
job( item
, parentChain
.rbegin() );
3407 Walker( m_root
, job
);
3408 return job
.GetResult();
3412 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3413 wxDataViewTreeNode
* node
)
3415 if( !model
->IsContainer( item
) )
3418 wxDataViewItemArray children
;
3419 unsigned int num
= model
->GetChildren( item
, children
);
3421 for ( unsigned int index
= 0; index
< num
; index
++ )
3423 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3425 if( model
->IsContainer(children
[index
]) )
3426 n
->SetHasChildren( true );
3428 node
->InsertChild(n
, index
);
3431 wxASSERT( node
->IsOpen() );
3432 node
->ChangeSubTreeCount(+num
);
3435 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3439 if (GetModel()->IsVirtualListModel())
3445 m_root
= wxDataViewTreeNode::CreateRootNode();
3447 // First we define a invalid item to fetch the top-level elements
3448 wxDataViewItem item
;
3450 BuildTreeHelper( model
, item
, m_root
);
3454 void wxDataViewMainWindow::DestroyTree()
3456 if (!IsVirtualList())
3464 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3466 // Edit the current column editable in 'mode'. If no column is focused
3467 // (typically because the user has full row selected), try to find the
3468 // first editable column (this would typically be a checkbox for
3469 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3470 // focus on the checkbox column; or on the only editable text column).
3472 wxDataViewColumn
*candidate
= m_currentCol
;
3475 !IsCellEditableInMode(item
, candidate
, mode
) &&
3476 !m_currentColSetByKeyboard
)
3478 // If current column was set by mouse to something not editable (in
3479 // 'mode') and the user pressed Space/F2 to edit it, treat the
3480 // situation as if there was whole-row focus, because that's what is
3481 // visually indicated and the mouse click could very well be targeted
3482 // on the row rather than on an individual cell.
3484 // But if it was done by keyboard, respect that even if the column
3485 // isn't editable, because focus is visually on that column and editing
3486 // something else would be surprising.
3492 const unsigned cols
= GetOwner()->GetColumnCount();
3493 for ( unsigned i
= 0; i
< cols
; i
++ )
3495 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3496 if ( c
->IsHidden() )
3499 if ( IsCellEditableInMode(item
, c
, mode
) )
3507 // If on container item without columns, only the expander column
3508 // may be directly editable:
3510 GetOwner()->GetExpanderColumn() != candidate
&&
3511 GetModel()->IsContainer(item
) &&
3512 !GetModel()->HasContainerColumns(item
) )
3514 candidate
= GetOwner()->GetExpanderColumn();
3520 if ( !IsCellEditableInMode(item
, candidate
, mode
) )
3526 bool wxDataViewMainWindow::IsCellEditableInMode(const wxDataViewItem
& item
,
3527 const wxDataViewColumn
*col
,
3528 wxDataViewCellMode mode
) const
3530 if ( col
->GetRenderer()->GetMode() != mode
)
3533 if ( !GetModel()->IsEnabled(item
, col
->GetModelColumn()) )
3539 void wxDataViewMainWindow::OnCharHook(wxKeyEvent
& event
)
3543 // Handle any keys special for the in-place editor and return without
3544 // calling Skip() below.
3545 switch ( event
.GetKeyCode() )
3548 m_editorRenderer
->CancelEditing();
3552 m_editorRenderer
->FinishEditing();
3560 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3562 wxWindow
* const parent
= GetParent();
3564 // propagate the char event upwards
3565 wxKeyEvent
eventForParent(event
);
3566 eventForParent
.SetEventObject(parent
);
3567 if ( parent
->ProcessWindowEvent(eventForParent
) )
3570 if ( parent
->HandleAsNavigationKey(event
) )
3573 // no item -> nothing to do
3574 if (!HasCurrentRow())
3580 // don't use m_linesPerPage directly as it might not be computed yet
3581 const int pageSize
= GetCountPerPage();
3582 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3584 switch ( event
.GetKeyCode() )
3587 if ( event
.HasModifiers() )
3594 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3595 // it. Only if that event is not handled do we activate column renderer (which
3596 // is normally done by Space) or even inline editing.
3598 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3600 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3603 le
.SetEventObject(parent
);
3604 le
.SetModel(GetModel());
3606 if ( parent
->ProcessWindowEvent(le
) )
3608 // else: fall through to WXK_SPACE handling
3612 if ( event
.HasModifiers() )
3619 // Space toggles activatable items or -- if not activatable --
3620 // starts inline editing (this is normally done using F2 on
3621 // Windows, but Space is common everywhere else, so use it too
3622 // for greater cross-platform compatibility).
3624 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3626 // Activate the current activatable column. If not column is focused (typically
3627 // because the user has full row selected), try to find the first activatable
3628 // column (this would typically be a checkbox and we don't want to force the user
3629 // to set focus on the checkbox column).
3630 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3632 if ( activatableCol
)
3634 const unsigned colIdx
= activatableCol
->GetModelColumn();
3635 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3637 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3638 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3639 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3643 // else: fall through to WXK_F2 handling
3647 if ( event
.HasModifiers() )
3654 if( !m_selection
.empty() )
3656 // Mimic Windows 7 behavior: edit the item that has focus
3657 // if it is selected and the first selected item if focus
3658 // is out of selection.
3660 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3663 sel
= m_selection
[0];
3666 const wxDataViewItem item
= GetItemByRow(sel
);
3668 // Edit the current column. If no column is focused
3669 // (typically because the user has full row selected), try
3670 // to find the first editable column.
3671 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3674 GetOwner()->EditItem(item
, editableCol
);
3680 if ( m_currentRow
> 0 )
3681 OnVerticalNavigation( m_currentRow
- 1, event
);
3685 if ( m_currentRow
+ 1 < GetRowCount() )
3686 OnVerticalNavigation( m_currentRow
+ 1, event
);
3688 // Add the process for tree expanding/collapsing
3700 OnVerticalNavigation( GetRowCount() - 1, event
);
3705 OnVerticalNavigation( 0, event
);
3710 int steps
= pageSize
- 1;
3711 int index
= m_currentRow
- steps
;
3715 OnVerticalNavigation( index
, event
);
3721 int steps
= pageSize
- 1;
3722 unsigned int index
= m_currentRow
+ steps
;
3723 unsigned int count
= GetRowCount();
3724 if ( index
>= count
)
3727 OnVerticalNavigation( index
, event
);
3736 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3738 wxCHECK_RET( newCurrent
< GetRowCount(),
3739 wxT("invalid item index in OnVerticalNavigation()") );
3741 // if there is no selection, we cannot move it anywhere
3742 if (!HasCurrentRow())
3745 unsigned int oldCurrent
= m_currentRow
;
3747 // in single selection we just ignore Shift as we can't select several
3749 if ( event
.ShiftDown() && !IsSingleSel() )
3751 RefreshRow( oldCurrent
);
3753 ChangeCurrentRow( newCurrent
);
3755 // select all the items between the old and the new one
3756 if ( oldCurrent
> newCurrent
)
3758 newCurrent
= oldCurrent
;
3759 oldCurrent
= m_currentRow
;
3762 SelectRows( oldCurrent
, newCurrent
, true );
3763 if (oldCurrent
!=newCurrent
)
3764 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3768 RefreshRow( oldCurrent
);
3770 // all previously selected items are unselected unless ctrl is held
3771 if ( !event
.ControlDown() )
3772 SelectAllRows(false);
3774 ChangeCurrentRow( newCurrent
);
3776 if ( !event
.ControlDown() )
3778 SelectRow( m_currentRow
, true );
3779 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3782 RefreshRow( m_currentRow
);
3785 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3788 void wxDataViewMainWindow::OnLeftKey()
3792 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3796 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3798 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3801 // Because TryAdvanceCurrentColumn() return false, we are at the first
3802 // column or using whole-row selection. In this situation, we can use
3803 // the standard TreeView handling of the left key.
3804 if (node
->HasChildren() && node
->IsOpen())
3806 Collapse(m_currentRow
);
3810 // if the node is already closed, we move the selection to its parent
3811 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3815 int parent
= GetRowByItem( parent_node
->GetItem() );
3818 unsigned int row
= m_currentRow
;
3819 SelectRow( row
, false);
3820 SelectRow( parent
, true );
3821 ChangeCurrentRow( parent
);
3822 GetOwner()->EnsureVisible( parent
, -1 );
3823 SendSelectionChangedEvent( parent_node
->GetItem() );
3830 void wxDataViewMainWindow::OnRightKey()
3834 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3838 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3840 if ( node
->HasChildren() )
3842 if ( !node
->IsOpen() )
3844 Expand( m_currentRow
);
3848 // if the node is already open, we move the selection to the first child
3849 unsigned int row
= m_currentRow
;
3850 SelectRow( row
, false );
3851 SelectRow( row
+ 1, true );
3852 ChangeCurrentRow( row
+ 1 );
3853 GetOwner()->EnsureVisible( row
+ 1, -1 );
3854 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3859 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3864 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3866 if ( GetOwner()->GetColumnCount() == 0 )
3869 if ( !m_useCellFocus
)
3874 // navigation shouldn't work in branch nodes without other columns:
3875 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3879 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3883 m_currentCol
= GetOwner()->GetColumnAt(1);
3884 m_currentColSetByKeyboard
= true;
3885 RefreshRow(m_currentRow
);
3892 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3894 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3897 GetOwner()->EnsureVisible(m_currentRow
, idx
);
3901 // We are going to the left of the second column. Reset to whole-row
3902 // focus (which means first column would be edited).
3903 m_currentCol
= NULL
;
3904 RefreshRow(m_currentRow
);
3908 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3909 m_currentColSetByKeyboard
= true;
3910 RefreshRow(m_currentRow
);
3914 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3916 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3918 // let the base handle mouse wheel events.
3923 // set the focus to ourself if any of the mouse buttons are pressed
3924 if(event
.ButtonDown() && !HasFocus())
3927 int x
= event
.GetX();
3928 int y
= event
.GetY();
3929 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3930 wxDataViewColumn
*col
= NULL
;
3933 unsigned int cols
= GetOwner()->GetColumnCount();
3935 for (i
= 0; i
< cols
; i
++)
3937 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3939 continue; // skip it!
3941 if (x
< xpos
+ c
->GetWidth())
3946 xpos
+= c
->GetWidth();
3949 wxDataViewModel
* const model
= GetModel();
3951 const unsigned int current
= GetLineAt( y
);
3952 const wxDataViewItem item
= GetItemByRow(current
);
3954 // Handle right clicking here, before everything else as context menu
3955 // events should be sent even when we click outside of any item, unlike all
3957 if (event
.RightUp())
3959 wxWindow
*parent
= GetParent();
3960 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3961 le
.SetEventObject(parent
);
3964 if ( item
.IsOk() && col
)
3967 le
.SetColumn( col
->GetModelColumn() );
3968 le
.SetDataViewColumn( col
);
3971 model
->GetValue( value
, item
, col
->GetModelColumn() );
3975 parent
->ProcessWindowEvent(le
);
3985 wxDataViewRenderer
*cell
= col
->GetRenderer();
3986 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3988 // Unselect all if below the last row ?
3993 wxDataViewColumn
* const
3994 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3996 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3997 // button) and also determine the offset of the real cell start, skipping
3998 // the indentation and the expander itself.
3999 bool hoverOverExpander
= false;
4001 if ((!IsList()) && (expander
== col
))
4003 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4005 int indent
= node
->GetIndentLevel();
4006 itemOffset
= GetOwner()->GetIndent()*indent
;
4008 if ( node
->HasChildren() )
4010 // we make the rectangle we are looking in a bit bigger than the actual
4011 // visual expander so the user can hit that little thing reliably
4012 wxRect
rect(itemOffset
,
4013 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
4014 m_lineHeight
, m_lineHeight
);
4016 if( rect
.Contains(x
, y
) )
4018 // So the mouse is over the expander
4019 hoverOverExpander
= true;
4020 if (m_underMouse
&& m_underMouse
!= node
)
4022 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4023 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4025 if (m_underMouse
!= node
)
4027 // wxLogMessage("Do the row: %d", current);
4028 RefreshRow(current
);
4030 m_underMouse
= node
;
4034 // Account for the expander as well, even if this item doesn't have it,
4035 // its parent does so it still counts for the offset.
4036 itemOffset
+= m_lineHeight
;
4038 if (!hoverOverExpander
)
4040 if (m_underMouse
!= NULL
)
4042 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4043 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4044 m_underMouse
= NULL
;
4048 #if wxUSE_DRAG_AND_DROP
4049 if (event
.Dragging())
4051 if (m_dragCount
== 0)
4053 // we have to report the raw, physical coords as we want to be
4054 // able to call HitTest(event.m_pointDrag) from the user code to
4055 // get the item being dragged
4056 m_dragStart
= event
.GetPosition();
4061 if (m_dragCount
!= 3)
4064 if (event
.LeftIsDown())
4066 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
4067 &m_dragStart
.x
, &m_dragStart
.y
);
4068 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
4069 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
4071 // Notify cell about drag
4072 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
4073 event
.SetEventObject( m_owner
);
4074 event
.SetItem( itemDragged
);
4075 event
.SetModel( model
);
4076 if (!m_owner
->HandleWindowEvent( event
))
4079 if (!event
.IsAllowed())
4082 wxDataObject
*obj
= event
.GetDataObject();
4086 wxDataViewDropSource
drag( this, drag_item_row
);
4087 drag
.SetData( *obj
);
4088 /* wxDragResult res = */ drag
.DoDragDrop(event
.GetDragFlags());
4097 #endif // wxUSE_DRAG_AND_DROP
4099 bool simulateClick
= false;
4101 if (event
.ButtonDClick())
4103 m_renameTimer
->Stop();
4104 m_lastOnSame
= false;
4107 bool ignore_other_columns
=
4108 ((expander
!= col
) &&
4109 (model
->IsContainer(item
)) &&
4110 (!model
->HasContainerColumns(item
)));
4112 if (event
.LeftDClick())
4114 if(hoverOverExpander
)
4116 // a double click on the expander will be converted into a "simulated" normal click
4117 simulateClick
= true;
4119 else if ( current
== m_lineLastClicked
)
4121 wxWindow
*parent
= GetParent();
4122 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4124 le
.SetColumn( col
->GetModelColumn() );
4125 le
.SetDataViewColumn( col
);
4126 le
.SetEventObject(parent
);
4127 le
.SetModel(GetModel());
4129 parent
->ProcessWindowEvent(le
);
4134 // The first click was on another item, so don't interpret this as
4135 // a double click, but as a simple click instead
4136 simulateClick
= true;
4140 if (event
.LeftUp() && !hoverOverExpander
)
4142 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4144 // select single line
4145 SelectAllRows( false );
4146 SelectRow( m_lineSelectSingleOnUp
, true );
4147 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4150 // If the user click the expander, we do not do editing even if the column
4151 // with expander are editable
4152 if (m_lastOnSame
&& !ignore_other_columns
)
4154 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4155 IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
4157 m_renameTimer
->Start( 100, true );
4161 m_lastOnSame
= false;
4162 m_lineSelectSingleOnUp
= (unsigned int)-1;
4164 else if(!event
.LeftUp())
4166 // This is necessary, because after a DnD operation in
4167 // from and to ourself, the up event is swallowed by the
4168 // DnD code. So on next non-up event (which means here and
4169 // now) m_lineSelectSingleOnUp should be reset.
4170 m_lineSelectSingleOnUp
= (unsigned int)-1;
4173 if (event
.RightDown())
4175 m_lineBeforeLastClicked
= m_lineLastClicked
;
4176 m_lineLastClicked
= current
;
4178 // If the item is already selected, do not update the selection.
4179 // Multi-selections should not be cleared if a selected item is clicked.
4180 if (!IsRowSelected(current
))
4182 SelectAllRows(false);
4183 const unsigned oldCurrent
= m_currentRow
;
4184 ChangeCurrentRow(current
);
4185 SelectRow(m_currentRow
,true);
4186 RefreshRow(oldCurrent
);
4187 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4190 else if (event
.MiddleDown())
4194 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4196 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4198 // hoverOverExpander being true tells us that our node must be
4199 // valid and have children.
4200 // So we don't need any extra checks.
4201 if( node
->IsOpen() )
4206 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4208 m_lineBeforeLastClicked
= m_lineLastClicked
;
4209 m_lineLastClicked
= current
;
4211 unsigned int oldCurrentRow
= m_currentRow
;
4212 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4214 bool cmdModifierDown
= event
.CmdDown();
4215 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4217 if ( IsSingleSel() || !IsRowSelected(current
) )
4219 SelectAllRows( false );
4220 ChangeCurrentRow(current
);
4221 SelectRow(m_currentRow
,true);
4222 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4224 else // multi sel & current is highlighted & no mod keys
4226 m_lineSelectSingleOnUp
= current
;
4227 ChangeCurrentRow(current
); // change focus
4230 else // multi sel & either ctrl or shift is down
4232 if (cmdModifierDown
)
4234 ChangeCurrentRow(current
);
4235 ReverseRowSelection(m_currentRow
);
4236 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4238 else if (event
.ShiftDown())
4240 ChangeCurrentRow(current
);
4242 unsigned int lineFrom
= oldCurrentRow
,
4245 if ( lineTo
< lineFrom
)
4248 lineFrom
= m_currentRow
;
4251 SelectRows(lineFrom
, lineTo
, true);
4252 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4254 else // !ctrl, !shift
4256 // test in the enclosing if should make it impossible
4257 wxFAIL_MSG( wxT("how did we get here?") );
4261 if (m_currentRow
!= oldCurrentRow
)
4262 RefreshRow( oldCurrentRow
);
4264 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4266 // Update selection here...
4268 m_currentColSetByKeyboard
= false;
4270 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4271 (current
== oldCurrentRow
)) && oldWasSelected
;
4273 // Call ActivateCell() after everything else as under GTK+
4274 if ( IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_ACTIVATABLE
) )
4276 // notify cell about click
4277 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4279 wxRect
cell_rect( xpos
+ itemOffset
,
4280 GetLineStart( current
),
4281 col
->GetWidth() - itemOffset
,
4282 GetLineHeight( current
) );
4284 // Report position relative to the cell's custom area, i.e.
4285 // no the entire space as given by the control but the one
4286 // used by the renderer after calculation of alignment etc.
4288 // adjust the rectangle ourselves to account for the alignment
4289 wxRect rectItem
= cell_rect
;
4290 const int align
= cell
->GetAlignment();
4291 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4293 const wxSize size
= cell
->GetSize();
4295 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4297 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4298 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4299 else if ( align
& wxALIGN_RIGHT
)
4300 rectItem
.x
+= cell_rect
.width
- size
.x
;
4301 // else: wxALIGN_LEFT is the default
4304 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4306 if ( align
& wxALIGN_CENTER_VERTICAL
)
4307 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4308 else if ( align
& wxALIGN_BOTTOM
)
4309 rectItem
.y
+= cell_rect
.height
- size
.y
;
4310 // else: wxALIGN_TOP is the default
4314 wxMouseEvent
event2(event
);
4315 event2
.m_x
-= rectItem
.x
;
4316 event2
.m_y
-= rectItem
.y
;
4317 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4319 /* ignore ret */ cell
->WXActivateCell
4324 col
->GetModelColumn(),
4331 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4335 if (HasCurrentRow())
4341 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4345 if (HasCurrentRow())
4351 void wxDataViewMainWindow::OnColumnsCountChanged()
4353 int editableCount
= 0;
4355 const unsigned cols
= GetOwner()->GetColumnCount();
4356 for ( unsigned i
= 0; i
< cols
; i
++ )
4358 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4359 if ( c
->IsHidden() )
4361 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4365 m_useCellFocus
= (editableCount
> 1);
4370 //-----------------------------------------------------------------------------
4372 //-----------------------------------------------------------------------------
4374 WX_DEFINE_LIST(wxDataViewColumnList
)
4376 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4377 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4378 EVT_SIZE(wxDataViewCtrl::OnSize
)
4381 wxDataViewCtrl::~wxDataViewCtrl()
4384 GetModel()->RemoveNotifier( m_notifier
);
4387 m_colsBestWidths
.clear();
4390 void wxDataViewCtrl::Init()
4392 m_cols
.DeleteContents(true);
4395 // No sorting column at start
4396 m_sortingColumnIdx
= wxNOT_FOUND
;
4398 m_headerArea
= NULL
;
4400 m_colsDirty
= false;
4403 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4408 const wxValidator
& validator
,
4409 const wxString
& name
)
4411 // if ( (style & wxBORDER_MASK) == 0)
4412 // style |= wxBORDER_SUNKEN;
4416 if (!wxControl::Create( parent
, id
, pos
, size
,
4417 style
| wxScrolledWindowStyle
, validator
, name
))
4420 SetInitialSize(size
);
4423 MacSetClipChildren( true );
4426 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4428 // We use the cursor keys for moving the selection, not scrolling, so call
4429 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4430 // keyboard events forwarded to us from wxListMainWindow.
4431 DisableKeyboardScrolling();
4433 if (HasFlag(wxDV_NO_HEADER
))
4434 m_headerArea
= NULL
;
4436 m_headerArea
= new wxDataViewHeaderWindow(this);
4438 SetTargetWindow( m_clientArea
);
4440 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4442 sizer
->Add( m_headerArea
, 0, wxGROW
);
4443 sizer
->Add( m_clientArea
, 1, wxGROW
);
4449 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4451 return wxBORDER_THEME
;
4455 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4459 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4462 // we need to process arrows ourselves for scrolling
4463 if ( nMsg
== WM_GETDLGCODE
)
4465 rc
|= DLGC_WANTARROWS
;
4473 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4475 wxSize newsize
= size
;
4476 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4477 newsize
.y
-= m_headerArea
->GetSize().y
;
4482 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4484 // We need to override OnSize so that our scrolled
4485 // window a) does call Layout() to use sizers for
4486 // positioning the controls but b) does not query
4487 // the sizer for their size and use that for setting
4488 // the scrollable area as set that ourselves by
4489 // calling SetScrollbar() further down.
4495 // We must redraw the headers if their height changed. Normally this
4496 // shouldn't happen as the control shouldn't let itself be resized beneath
4497 // its minimal height but avoid the display artefacts that appear if it
4498 // does happen, e.g. because there is really not enough vertical space.
4499 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4500 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4502 m_headerArea
->Refresh();
4506 void wxDataViewCtrl::SetFocus()
4509 m_clientArea
->SetFocus();
4512 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4514 if (!wxDataViewCtrlBase::AssociateModel( model
))
4517 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4519 model
->AddNotifier( m_notifier
);
4521 m_clientArea
->DestroyTree();
4523 m_clientArea
->BuildTree(model
);
4525 m_clientArea
->UpdateDisplay();
4530 #if wxUSE_DRAG_AND_DROP
4532 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4534 return m_clientArea
->EnableDragSource( format
);
4537 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4539 return m_clientArea
->EnableDropTarget( format
);
4542 #endif // wxUSE_DRAG_AND_DROP
4544 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4546 if (!wxDataViewCtrlBase::AppendColumn(col
))
4549 m_cols
.Append( col
);
4550 m_colsBestWidths
.push_back(CachedColWidthInfo());
4551 OnColumnsCountChanged();
4555 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4557 if (!wxDataViewCtrlBase::PrependColumn(col
))
4560 m_cols
.Insert( col
);
4561 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), CachedColWidthInfo());
4562 OnColumnsCountChanged();
4566 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4568 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4571 m_cols
.Insert( pos
, col
);
4572 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, CachedColWidthInfo());
4573 OnColumnsCountChanged();
4577 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4580 m_headerArea
->UpdateColumn(idx
);
4582 m_clientArea
->UpdateDisplay();
4585 void wxDataViewCtrl::OnColumnsCountChanged()
4588 m_headerArea
->SetColumnCount(GetColumnCount());
4590 m_clientArea
->OnColumnsCountChanged();
4593 void wxDataViewCtrl::DoSetExpanderColumn()
4595 m_clientArea
->UpdateDisplay();
4598 void wxDataViewCtrl::DoSetIndent()
4600 m_clientArea
->UpdateDisplay();
4603 unsigned int wxDataViewCtrl::GetColumnCount() const
4605 return m_cols
.GetCount();
4608 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4610 if ( !m_clientArea
)
4613 m_clientArea
->SetRowHeight(lineHeight
);
4618 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4623 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4625 // columns can't be reordered if there is no header window which allows
4627 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4630 return GetColumn(idx
);
4633 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4635 const unsigned count
= m_cols
.size();
4636 for ( unsigned n
= 0; n
< count
; n
++ )
4638 if ( m_cols
[n
] == column
)
4645 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4647 if ( m_colsBestWidths
[idx
].width
!= 0 )
4648 return m_colsBestWidths
[idx
].width
;
4650 const int count
= m_clientArea
->GetRowCount();
4651 wxDataViewColumn
*column
= GetColumn(idx
);
4652 wxDataViewRenderer
*renderer
=
4653 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4655 class MaxWidthCalculator
4658 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4659 wxDataViewMainWindow
*clientArea
,
4660 wxDataViewRenderer
*renderer
,
4661 const wxDataViewModel
*model
,
4666 m_clientArea(clientArea
),
4667 m_renderer(renderer
),
4670 m_expanderSize(expanderSize
)
4674 !clientArea
->IsList() &&
4676 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4679 void UpdateWithWidth(int width
)
4681 m_width
= wxMax(m_width
, width
);
4684 void UpdateWithRow(int row
)
4687 wxDataViewItem item
;
4689 if ( m_isExpanderCol
)
4691 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4692 item
= node
->GetItem();
4693 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4697 item
= m_clientArea
->GetItemByRow(row
);
4700 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4701 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4704 int GetMaxWidth() const { return m_width
; }
4708 const wxDataViewCtrl
*m_dvc
;
4709 wxDataViewMainWindow
*m_clientArea
;
4710 wxDataViewRenderer
*m_renderer
;
4711 const wxDataViewModel
*m_model
;
4713 bool m_isExpanderCol
;
4717 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4718 GetModel(), column
->GetModelColumn(),
4719 m_clientArea
->GetRowHeight());
4721 calculator
.UpdateWithWidth(column
->GetMinWidth());
4724 calculator
.UpdateWithWidth(m_headerArea
->GetColumnTitleWidth(*column
));
4726 // The code below deserves some explanation. For very large controls, we
4727 // simply can't afford to calculate sizes for all items, it takes too
4728 // long. So the best we can do is to check the first and the last N/2
4729 // items in the control for some sufficiently large N and calculate best
4730 // sizes from that. That can result in the calculated best width being too
4731 // small for some outliers, but it's better to get slightly imperfect
4732 // result than to wait several seconds after every update. To avoid highly
4733 // visible miscalculations, we also include all currently visible items
4734 // no matter what. Finally, the value of N is determined dynamically by
4735 // measuring how much time we spent on the determining item widths so far.
4738 int top_part_end
= count
;
4739 static const long CALC_TIMEOUT
= 20/*ms*/;
4740 // don't call wxStopWatch::Time() too often
4741 static const unsigned CALC_CHECK_FREQ
= 100;
4744 // use some hard-coded limit, that's the best we can do without timer
4745 int top_part_end
= wxMin(500, count
);
4746 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4750 for ( row
= 0; row
< top_part_end
; row
++ )
4753 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4754 timer
.Time() > CALC_TIMEOUT
)
4756 #endif // wxUSE_STOPWATCH
4757 calculator
.UpdateWithRow(row
);
4760 // row is the first unmeasured item now; that's our value of N/2
4766 // add bottom N/2 items now:
4767 const int bottom_part_start
= wxMax(row
, count
- row
);
4768 for ( row
= bottom_part_start
; row
< count
; row
++ )
4770 calculator
.UpdateWithRow(row
);
4773 // finally, include currently visible items in the calculation:
4774 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4775 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4776 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4778 first_visible
= wxMax(first_visible
, top_part_end
);
4779 last_visible
= wxMin(bottom_part_start
, last_visible
);
4781 for ( row
= first_visible
; row
< last_visible
; row
++ )
4783 calculator
.UpdateWithRow(row
);
4786 wxLogTrace("dataview",
4787 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4789 count
- bottom_part_start
,
4790 wxMax(0, last_visible
- first_visible
),
4794 int max_width
= calculator
.GetMaxWidth();
4795 if ( max_width
> 0 )
4796 max_width
+= 2 * PADDING_RIGHTLEFT
;
4798 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
].width
= max_width
;
4802 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4803 unsigned int WXUNUSED(new_pos
))
4805 // do _not_ reorder m_cols elements here, they should always be in the
4806 // order in which columns were added, we only display the columns in
4808 m_clientArea
->UpdateDisplay();
4811 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4813 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4817 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4820 if ( m_clientArea
->GetCurrentColumn() == column
)
4821 m_clientArea
->ClearCurrentColumn();
4823 OnColumnsCountChanged();
4828 bool wxDataViewCtrl::ClearColumns()
4830 SetExpanderColumn(NULL
);
4832 m_colsBestWidths
.clear();
4834 m_clientArea
->ClearCurrentColumn();
4836 OnColumnsCountChanged();
4841 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4843 m_colsBestWidths
[idx
].width
= 0;
4844 m_colsBestWidths
[idx
].dirty
= true;
4848 void wxDataViewCtrl::InvalidateColBestWidths()
4850 // mark all columns as dirty:
4851 m_colsBestWidths
.clear();
4852 m_colsBestWidths
.resize(m_cols
.size());
4856 void wxDataViewCtrl::UpdateColWidths()
4858 m_colsDirty
= false;
4860 if ( !m_headerArea
)
4863 const unsigned len
= m_colsBestWidths
.size();
4864 for ( unsigned i
= 0; i
< len
; i
++ )
4866 // Note that we have to have an explicit 'dirty' flag here instead of
4867 // checking if the width==0, as is done in GetBestColumnWidth().
4869 // Testing width==0 wouldn't work correctly if some code called
4870 // GetWidth() after col. width invalidation but before
4871 // wxDataViewCtrl::UpdateColWidths() was called at idle time. This
4872 // would result in the header's column width getting out of sync with
4873 // the control itself.
4874 if ( m_colsBestWidths
[i
].dirty
)
4876 m_headerArea
->UpdateColumn(i
);
4877 m_colsBestWidths
[i
].dirty
= false;
4882 void wxDataViewCtrl::OnInternalIdle()
4884 wxDataViewCtrlBase::OnInternalIdle();
4890 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4892 unsigned int len
= GetColumnCount();
4893 for ( unsigned int i
= 0; i
< len
; i
++ )
4895 wxDataViewColumn
* col
= GetColumnAt(i
);
4903 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4905 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4906 : GetColumn(m_sortingColumnIdx
);
4909 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4911 return GetItemByRow(m_clientArea
->GetCurrentRow());
4914 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4916 const int row
= m_clientArea
->GetRowByItem(item
);
4918 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4919 if ( static_cast<unsigned>(row
) != oldCurrent
)
4921 m_clientArea
->ChangeCurrentRow(row
);
4922 m_clientArea
->RefreshRow(oldCurrent
);
4923 m_clientArea
->RefreshRow(row
);
4927 wxDataViewColumn
*wxDataViewCtrl::GetCurrentColumn() const
4929 return m_clientArea
->GetCurrentColumn();
4932 int wxDataViewCtrl::GetSelectedItemsCount() const
4934 return m_clientArea
->GetSelections().size();
4937 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4940 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4942 const size_t len
= selections
.size();
4943 for ( size_t i
= 0; i
< len
; i
++ )
4945 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4952 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4959 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4961 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4963 wxDataViewItem last_parent
;
4965 int len
= sel
.GetCount();
4966 for( int i
= 0; i
< len
; i
++ )
4968 wxDataViewItem item
= sel
[i
];
4969 wxDataViewItem parent
= GetModel()->GetParent( item
);
4972 if (parent
!= last_parent
)
4973 ExpandAncestors(item
);
4976 last_parent
= parent
;
4977 int row
= m_clientArea
->GetRowByItem( item
);
4979 selection
.Add( static_cast<unsigned int>(row
) );
4982 m_clientArea
->SetSelections( selection
);
4985 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4987 ExpandAncestors( item
);
4989 int row
= m_clientArea
->GetRowByItem( item
);
4992 // Unselect all rows before select another in the single select mode
4993 if (m_clientArea
->IsSingleSel())
4994 m_clientArea
->SelectAllRows(false);
4996 m_clientArea
->SelectRow(row
, true);
4998 // Also set focus to the selected item
4999 m_clientArea
->ChangeCurrentRow( row
);
5003 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
5005 int row
= m_clientArea
->GetRowByItem( item
);
5007 m_clientArea
->SelectRow(row
, false);
5010 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
5012 int row
= m_clientArea
->GetRowByItem( item
);
5015 return m_clientArea
->IsRowSelected(row
);
5020 void wxDataViewCtrl::SetAlternateRowColour(const wxColour
& colour
)
5022 m_alternateRowColour
= colour
;
5025 void wxDataViewCtrl::SelectAll()
5027 m_clientArea
->SelectAllRows(true);
5030 void wxDataViewCtrl::UnselectAll()
5032 m_clientArea
->SelectAllRows(false);
5035 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
5039 if( row
> (int) m_clientArea
->GetRowCount() )
5040 row
= m_clientArea
->GetRowCount();
5042 int first
= m_clientArea
->GetFirstVisibleRow();
5043 int last
= m_clientArea
->GetLastVisibleRow();
5045 m_clientArea
->ScrollTo( row
, column
);
5046 else if( row
> last
)
5047 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
5049 m_clientArea
->ScrollTo( first
, column
);
5052 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
5054 ExpandAncestors( item
);
5056 m_clientArea
->RecalculateDisplay();
5058 int row
= m_clientArea
->GetRowByItem(item
);
5061 if( column
== NULL
)
5062 EnsureVisible(row
, -1);
5064 EnsureVisible( row
, GetColumnIndex(column
) );
5069 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
5070 wxDataViewColumn
* &column
) const
5072 m_clientArea
->HitTest(point
, item
, column
);
5075 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
5076 const wxDataViewColumn
* column
) const
5078 return m_clientArea
->GetItemRect(item
, column
);
5081 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
5083 return m_clientArea
->GetItemByRow( row
);
5086 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
5088 return m_clientArea
->GetRowByItem( item
);
5091 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
5093 ExpandAncestors( item
);
5095 int row
= m_clientArea
->GetRowByItem( item
);
5098 m_clientArea
->Expand(row
);
5099 InvalidateColBestWidths();
5103 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
5105 int row
= m_clientArea
->GetRowByItem( item
);
5108 m_clientArea
->Collapse(row
);
5109 InvalidateColBestWidths();
5113 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
5115 int row
= m_clientArea
->GetRowByItem( item
);
5117 return m_clientArea
->IsExpanded(row
);
5121 void wxDataViewCtrl::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
5123 wxCHECK_RET( item
.IsOk(), "invalid item" );
5124 wxCHECK_RET( column
, "no column provided" );
5126 m_clientArea
->StartEditing(item
, column
);
5129 #endif // !wxUSE_GENERICDATAVIEWCTRL
5131 #endif // wxUSE_DATAVIEWCTRL