1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
47 #include "wx/imaglist.h"
48 #include "wx/headerctrl.h"
50 #include "wx/stopwatch.h"
51 #include "wx/weakref.h"
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 class wxDataViewColumn
;
58 class wxDataViewHeaderWindow
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static const int SCROLL_UNIT_X
= 15;
67 // the cell padding on the left/right
68 static const int PADDING_RIGHTLEFT
= 3;
70 // the expander space margin
71 static const int EXPANDER_MARGIN
= 4;
74 static const int EXPANDER_OFFSET
= 4;
76 static const int EXPANDER_OFFSET
= 1;
79 // Below is the compare stuff.
80 // For the generic implementation, both the leaf nodes and the nodes are sorted for
81 // fast search when needed
82 static wxDataViewModel
* g_model
;
83 static int g_column
= -2;
84 static bool g_asending
= true;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
93 // Return the expander column or, if it is not set, the first column and also
94 // set it as the expander one for the future.
95 wxDataViewColumn
* GetExpanderColumnOrFirstOne(wxDataViewCtrl
* dataview
)
97 wxDataViewColumn
* expander
= dataview
->GetExpanderColumn();
100 // TODO-RTL: last column for RTL support
101 expander
= dataview
->GetColumnAt( 0 );
102 dataview
->SetExpanderColumn(expander
);
108 } // anonymous namespace
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
121 m_sortAscending
= true;
124 int wxDataViewColumn::GetWidth() const
128 case wxCOL_WIDTH_DEFAULT
:
129 return wxDVC_DEFAULT_WIDTH
;
131 case wxCOL_WIDTH_AUTOSIZE
:
132 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
133 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
140 void wxDataViewColumn::UpdateDisplay()
144 int idx
= m_owner
->GetColumnIndex( this );
145 m_owner
->OnColumnChange( idx
);
149 void wxDataViewColumn::SetSortOrder(bool ascending
)
154 // First unset the old sort column if any.
155 int oldSortKey
= m_owner
->GetSortingColumnIndex();
156 if ( oldSortKey
!= wxNOT_FOUND
)
158 m_owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
161 // Now set this one as the new sort column.
162 const int idx
= m_owner
->GetColumnIndex(this);
163 m_owner
->SetSortingColumnIndex(idx
);
166 m_sortAscending
= ascending
;
168 // Call this directly instead of using UpdateDisplay() as we already have
169 // the column index, no need to look it up again.
170 m_owner
->OnColumnChange(idx
);
173 //-----------------------------------------------------------------------------
174 // wxDataViewHeaderWindow
175 //-----------------------------------------------------------------------------
177 class wxDataViewHeaderWindow
: public wxHeaderCtrl
180 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
181 : wxHeaderCtrl(parent
)
185 wxDataViewCtrl
*GetOwner() const
186 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
189 // implement/override wxHeaderCtrl functions by forwarding them to the main
191 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
193 return *(GetOwner()->GetColumn(idx
));
196 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
198 wxDataViewCtrl
* const owner
= GetOwner();
200 int widthContents
= owner
->GetBestColumnWidth(idx
);
201 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
202 owner
->OnColumnChange(idx
);
208 bool SendEvent(wxEventType type
, unsigned int n
)
210 wxDataViewCtrl
* const owner
= GetOwner();
211 wxDataViewEvent
event(type
, owner
->GetId());
213 event
.SetEventObject(owner
);
215 event
.SetDataViewColumn(owner
->GetColumn(n
));
216 event
.SetModel(owner
->GetModel());
218 // for events created by wxDataViewHeaderWindow the
219 // row / value fields are not valid
220 return owner
->ProcessWindowEvent(event
);
223 void OnClick(wxHeaderCtrlEvent
& event
)
225 const unsigned idx
= event
.GetColumn();
227 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
230 // default handling for the column click is to sort by this column or
231 // toggle its sort order
232 wxDataViewCtrl
* const owner
= GetOwner();
233 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
234 if ( !col
->IsSortable() )
236 // no default handling for non-sortable columns
241 if ( col
->IsSortKey() )
243 // already using this column for sorting, just change the order
244 col
->ToggleSortOrder();
246 else // not using this column for sorting yet
248 col
->SetSortOrder(true);
251 wxDataViewModel
* const model
= owner
->GetModel();
255 owner
->OnColumnChange(idx
);
257 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
260 void OnRClick(wxHeaderCtrlEvent
& event
)
262 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
267 void OnResize(wxHeaderCtrlEvent
& event
)
269 wxDataViewCtrl
* const owner
= GetOwner();
271 const unsigned col
= event
.GetColumn();
272 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
273 GetOwner()->OnColumnChange(col
);
276 void OnEndReorder(wxHeaderCtrlEvent
& event
)
278 wxDataViewCtrl
* const owner
= GetOwner();
279 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
280 event
.GetNewOrder());
283 DECLARE_EVENT_TABLE()
284 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
287 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
288 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
289 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
291 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
292 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
294 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
297 //-----------------------------------------------------------------------------
298 // wxDataViewRenameTimer
299 //-----------------------------------------------------------------------------
301 class wxDataViewRenameTimer
: public wxTimer
304 wxDataViewMainWindow
*m_owner
;
307 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
311 //-----------------------------------------------------------------------------
312 // wxDataViewTreeNode
313 //-----------------------------------------------------------------------------
315 class wxDataViewTreeNode
;
316 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
318 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
319 wxDataViewTreeNode
** node2
);
321 class wxDataViewTreeNode
324 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
331 ~wxDataViewTreeNode()
335 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
336 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
347 static wxDataViewTreeNode
* CreateRootNode()
349 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
350 n
->SetHasChildren(true);
351 n
->m_branchData
->open
= true;
355 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
357 const wxDataViewTreeNodes
& GetChildNodes() const
359 wxASSERT( m_branchData
!= NULL
);
360 return m_branchData
->children
;
363 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
366 m_branchData
= new BranchNodeData
;
368 m_branchData
->children
.Insert(node
, index
);
370 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
372 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
375 void RemoveChild(wxDataViewTreeNode
*node
)
377 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
378 m_branchData
->children
.Remove(node
);
381 // returns position of child node for given item in children list or wxNOT_FOUND
382 int FindChildByItem(const wxDataViewItem
& item
) const
387 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
388 const int len
= nodes
.size();
389 for ( int i
= 0; i
< len
; i
++ )
391 if ( nodes
[i
]->m_item
== item
)
397 const wxDataViewItem
& GetItem() const { return m_item
; }
398 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
400 int GetIndentLevel() const
403 const wxDataViewTreeNode
* node
= this;
404 while( node
->GetParent()->GetParent() != NULL
)
406 node
= node
->GetParent();
414 return m_branchData
&& m_branchData
->open
;
419 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
423 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
424 const int len
= nodes
.GetCount();
425 for ( int i
= 0;i
< len
; i
++)
426 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
428 if (m_branchData
->open
)
430 ChangeSubTreeCount(-sum
);
431 m_branchData
->open
= !m_branchData
->open
;
435 m_branchData
->open
= !m_branchData
->open
;
436 ChangeSubTreeCount(+sum
);
440 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
441 // even if GetChildNodes() is empty; see below.
442 bool HasChildren() const
444 return m_branchData
!= NULL
;
447 void SetHasChildren(bool has
)
451 wxDELETE(m_branchData
);
453 else if ( m_branchData
== NULL
)
455 m_branchData
= new BranchNodeData
;
459 int GetSubTreeCount() const
461 return m_branchData
? m_branchData
->subTreeCount
: 0;
464 void ChangeSubTreeCount( int num
)
466 wxASSERT( m_branchData
!= NULL
);
468 if( !m_branchData
->open
)
471 m_branchData
->subTreeCount
+= num
;
472 wxASSERT( m_branchData
->subTreeCount
>= 0 );
475 m_parent
->ChangeSubTreeCount(num
);
485 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
487 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
488 int len
= nodes
.GetCount();
489 for (int i
= 0; i
< len
; i
++)
491 if ( nodes
[i
]->HasChildren() )
499 wxDataViewTreeNode
*m_parent
;
501 // Corresponding model item.
502 wxDataViewItem m_item
;
504 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
505 // separate struct in order to conserve memory.
506 struct BranchNodeData
514 // Child nodes. Note that this may be empty even if m_hasChildren in
515 // case this branch of the tree wasn't expanded and realized yet.
516 wxDataViewTreeNodes children
;
518 // Is the branch node currently open (expanded)?
521 // Total count of expanded (i.e. visible with the help of some
522 // scrolling) items in the subtree, but excluding this node. I.e. it is
523 // 0 for leaves and is the number of rows the subtree occupies for
528 BranchNodeData
*m_branchData
;
532 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
533 wxDataViewTreeNode
** node2
)
535 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
539 //-----------------------------------------------------------------------------
540 // wxDataViewMainWindow
541 //-----------------------------------------------------------------------------
543 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
545 class wxDataViewMainWindow
: public wxWindow
548 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
550 const wxPoint
&pos
= wxDefaultPosition
,
551 const wxSize
&size
= wxDefaultSize
,
552 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
553 virtual ~wxDataViewMainWindow();
555 bool IsList() const { return GetModel()->IsListModel(); }
556 bool IsVirtualList() const { return m_root
== NULL
; }
558 // notifications from wxDataViewModel
559 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
560 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
561 bool ItemChanged( const wxDataViewItem
&item
);
562 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
566 if (!IsVirtualList())
576 g_model
= GetModel();
577 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
580 if (g_model
->HasDefaultCompare())
588 g_column
= col
->GetModelColumn();
589 g_asending
= col
->IsSortOrderAscending();
592 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
593 wxDataViewCtrl
*GetOwner() { return m_owner
; }
594 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
596 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
597 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
599 #if wxUSE_DRAG_AND_DROP
600 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
601 #endif // wxUSE_DRAG_AND_DROP
602 void OnPaint( wxPaintEvent
&event
);
603 void OnCharHook( wxKeyEvent
&event
);
604 void OnChar( wxKeyEvent
&event
);
605 void OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
);
608 void OnMouse( wxMouseEvent
&event
);
609 void OnSetFocus( wxFocusEvent
&event
);
610 void OnKillFocus( wxFocusEvent
&event
);
612 void UpdateDisplay();
613 void RecalculateDisplay();
614 void OnInternalIdle();
616 void OnRenameTimer();
618 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
619 void ScrollTo( int rows
, int column
);
621 unsigned GetCurrentRow() const { return m_currentRow
; }
622 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
623 void ChangeCurrentRow( unsigned int row
);
624 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
626 wxDataViewColumn
*GetCurrentColumn() const { return m_currentCol
; }
627 void ClearCurrentColumn() { m_currentCol
= NULL
; }
629 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
630 bool IsEmpty() { return GetRowCount() == 0; }
632 int GetCountPerPage() const;
633 int GetEndOfLastCol() const;
634 unsigned int GetFirstVisibleRow() const;
636 // I change this method to un const because in the tree view,
637 // the displaying number of the tree are changing along with the
638 // expanding/collapsing of the tree nodes
639 unsigned int GetLastVisibleRow();
640 unsigned int GetRowCount();
642 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
643 void SetSelections( const wxDataViewSelection
& sel
)
644 { m_selection
= sel
; UpdateDisplay(); }
645 void Select( const wxArrayInt
& aSelections
);
646 void SelectAllRows( bool on
);
647 void SelectRow( unsigned int row
, bool on
);
648 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
649 void ReverseRowSelection( unsigned int row
);
650 bool IsRowSelected( unsigned int row
);
651 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
653 void RefreshRow( unsigned int row
);
654 void RefreshRows( unsigned int from
, unsigned int to
);
655 void RefreshRowsAfter( unsigned int firstRow
);
657 // returns the colour to be used for drawing the rules
658 wxColour
GetRuleColour() const
660 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
663 wxRect
GetLineRect( unsigned int row
) const;
665 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
666 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
667 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
669 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
670 int GetRowHeight() const { return m_lineHeight
; }
672 // Some useful functions for row and item mapping
673 wxDataViewItem
GetItemByRow( unsigned int row
) const;
674 int GetRowByItem( const wxDataViewItem
& item
) const;
676 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
677 // We did not need this temporarily
678 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
680 // Methods for building the mapping tree
681 void BuildTree( wxDataViewModel
* model
);
683 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
684 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
686 void Expand( unsigned int row
);
687 void Collapse( unsigned int row
);
688 bool IsExpanded( unsigned int row
) const;
689 bool HasChildren( unsigned int row
) const;
691 #if wxUSE_DRAG_AND_DROP
692 bool EnableDragSource( const wxDataFormat
&format
);
693 bool EnableDropTarget( const wxDataFormat
&format
);
695 void RemoveDropHint();
696 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
697 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
698 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
700 #endif // wxUSE_DRAG_AND_DROP
702 void OnColumnsCountChanged();
704 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
705 // specified item in the given column.
706 void StartEditing(const wxDataViewItem
& item
, const wxDataViewColumn
* col
);
709 int RecalculateCount();
711 // Return false only if the event was vetoed by its handler.
712 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
714 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
716 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
718 bool IsCellEditableInMode(const wxDataViewItem
& item
, const wxDataViewColumn
*col
, wxDataViewCellMode mode
) const;
720 void DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
);
723 wxDataViewCtrl
*m_owner
;
727 wxDataViewColumn
*m_currentCol
;
728 unsigned int m_currentRow
;
729 wxDataViewSelection m_selection
;
731 wxDataViewRenameTimer
*m_renameTimer
;
736 bool m_currentColSetByKeyboard
;
738 #if wxUSE_DRAG_AND_DROP
743 wxDataFormat m_dragFormat
;
746 wxDataFormat m_dropFormat
;
748 unsigned int m_dropHintLine
;
749 #endif // wxUSE_DRAG_AND_DROP
751 // for double click logic
752 unsigned int m_lineLastClicked
,
753 m_lineBeforeLastClicked
,
754 m_lineSelectSingleOnUp
;
756 // the pen used to draw horiz/vertical rules
759 // the pen used to draw the expander and the lines
762 // This is the tree structure of the model
763 wxDataViewTreeNode
* m_root
;
766 // This is the tree node under the cursor
767 wxDataViewTreeNode
* m_underMouse
;
769 // The control used for editing or NULL.
770 wxWeakRef
<wxWindow
> m_editorCtrl
;
772 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
773 wxDataViewRenderer
* m_editorRenderer
;
776 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
777 DECLARE_EVENT_TABLE()
780 // ---------------------------------------------------------
781 // wxGenericDataViewModelNotifier
782 // ---------------------------------------------------------
784 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
787 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
788 { m_mainWindow
= mainWindow
; }
790 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
791 { return m_mainWindow
->ItemAdded( parent
, item
); }
792 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
793 { return m_mainWindow
->ItemDeleted( parent
, item
); }
794 virtual bool ItemChanged( const wxDataViewItem
& item
)
795 { return m_mainWindow
->ItemChanged(item
); }
796 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
797 { return m_mainWindow
->ValueChanged( item
, col
); }
798 virtual bool Cleared()
799 { return m_mainWindow
->Cleared(); }
800 virtual void Resort()
801 { m_mainWindow
->Resort(); }
803 wxDataViewMainWindow
*m_mainWindow
;
806 // ---------------------------------------------------------
807 // wxDataViewRenderer
808 // ---------------------------------------------------------
810 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
812 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
813 wxDataViewCellMode mode
,
815 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
819 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
823 wxDataViewRenderer::~wxDataViewRenderer()
828 wxDC
*wxDataViewRenderer::GetDC()
832 if (GetOwner() == NULL
)
834 if (GetOwner()->GetOwner() == NULL
)
836 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
842 void wxDataViewRenderer::SetAlignment( int align
)
847 int wxDataViewRenderer::GetAlignment() const
852 // ---------------------------------------------------------
853 // wxDataViewCustomRenderer
854 // ---------------------------------------------------------
856 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
858 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
859 wxDataViewCellMode mode
, int align
) :
860 wxDataViewRenderer( varianttype
, mode
, align
)
864 // ---------------------------------------------------------
865 // wxDataViewTextRenderer
866 // ---------------------------------------------------------
868 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
870 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
871 wxDataViewCellMode mode
, int align
) :
872 wxDataViewRenderer( varianttype
, mode
, align
)
876 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
878 m_text
= value
.GetString();
883 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
888 bool wxDataViewTextRenderer::HasEditorCtrl() const
893 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
894 wxRect labelRect
, const wxVariant
&value
)
896 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
897 wxPoint(labelRect
.x
,labelRect
.y
),
898 wxSize(labelRect
.width
,labelRect
.height
),
899 wxTE_PROCESS_ENTER
);
901 // select the text in the control an place the cursor at the end
902 ctrl
->SetInsertionPointEnd();
908 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
910 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
911 value
= text
->GetValue();
915 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
917 RenderText(m_text
, 0, rect
, dc
, state
);
921 wxSize
wxDataViewTextRenderer::GetSize() const
924 return GetTextExtent(m_text
);
926 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
929 // ---------------------------------------------------------
930 // wxDataViewBitmapRenderer
931 // ---------------------------------------------------------
933 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
935 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
936 wxDataViewCellMode mode
, int align
) :
937 wxDataViewRenderer( varianttype
, mode
, align
)
941 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
943 if (value
.GetType() == wxT("wxBitmap"))
945 if (value
.GetType() == wxT("wxIcon"))
951 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
956 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
959 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
960 else if (m_icon
.IsOk())
961 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
966 wxSize
wxDataViewBitmapRenderer::GetSize() const
969 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
970 else if (m_icon
.IsOk())
971 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
973 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
976 // ---------------------------------------------------------
977 // wxDataViewToggleRenderer
978 // ---------------------------------------------------------
980 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
982 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
983 wxDataViewCellMode mode
, int align
) :
984 wxDataViewRenderer( varianttype
, mode
, align
)
989 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
991 m_toggle
= value
.GetBool();
996 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
1001 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
1005 flags
|= wxCONTROL_CHECKED
;
1006 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
1007 GetEnabled() == false)
1008 flags
|= wxCONTROL_DISABLED
;
1010 // check boxes we draw must always have the same, standard size (if it's
1011 // bigger than the cell size the checkbox will be truncated because the
1012 // caller had set the clipping rectangle to prevent us from drawing outside
1014 cell
.SetSize(GetSize());
1016 wxRendererNative::Get().DrawCheckBox(
1017 GetOwner()->GetOwner(),
1025 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1026 wxDataViewModel
*model
,
1027 const wxDataViewItem
& item
,
1029 const wxMouseEvent
*mouseEvent
)
1033 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1034 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1038 model
->ChangeValue(!m_toggle
, item
, col
);
1042 wxSize
wxDataViewToggleRenderer::GetSize() const
1044 // the window parameter is not used by GetCheckBoxSize() so it's
1045 // safe to pass NULL
1046 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1049 // ---------------------------------------------------------
1050 // wxDataViewProgressRenderer
1051 // ---------------------------------------------------------
1053 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1055 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1056 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1057 wxDataViewRenderer( varianttype
, mode
, align
)
1063 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1065 m_value
= (long) value
;
1067 if (m_value
< 0) m_value
= 0;
1068 if (m_value
> 100) m_value
= 100;
1073 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1075 value
= (long) m_value
;
1080 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1082 // deflate the rect to leave a small border between bars in adjacent rows
1083 wxRect bar
= rect
.Deflate(0, 1);
1085 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1086 dc
->SetPen( *wxBLACK_PEN
);
1087 dc
->DrawRectangle( bar
);
1089 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1090 dc
->SetPen( *wxTRANSPARENT_PEN
);
1092 const wxDataViewItemAttr
& attr
= GetAttr();
1093 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1095 dc
->DrawRectangle( bar
);
1100 wxSize
wxDataViewProgressRenderer::GetSize() const
1102 return wxSize(40,12);
1105 // ---------------------------------------------------------
1106 // wxDataViewIconTextRenderer
1107 // ---------------------------------------------------------
1109 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1111 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1112 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1113 wxDataViewRenderer( varianttype
, mode
, align
)
1116 SetAlignment(align
);
1119 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1125 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1130 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1134 const wxIcon
& icon
= m_value
.GetIcon();
1137 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1138 xoffset
= icon
.GetWidth()+4;
1141 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1146 wxSize
wxDataViewIconTextRenderer::GetSize() const
1148 if (!m_value
.GetText().empty())
1150 wxSize size
= GetTextExtent(m_value
.GetText());
1152 if (m_value
.GetIcon().IsOk())
1153 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1156 return wxSize(80,20);
1159 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1161 wxDataViewIconText iconText
;
1164 wxString text
= iconText
.GetText();
1166 // adjust the label rect to take the width of the icon into account
1167 if (iconText
.GetIcon().IsOk())
1169 int w
= iconText
.GetIcon().GetWidth() + 4;
1171 labelRect
.width
-= w
;
1174 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1175 wxPoint(labelRect
.x
,labelRect
.y
),
1176 wxSize(labelRect
.width
,labelRect
.height
),
1177 wxTE_PROCESS_ENTER
);
1179 // select the text in the control an place the cursor at the end
1180 ctrl
->SetInsertionPointEnd();
1186 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1188 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1190 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1195 //-----------------------------------------------------------------------------
1196 // wxDataViewDropTarget
1197 //-----------------------------------------------------------------------------
1199 #if wxUSE_DRAG_AND_DROP
1201 class wxBitmapCanvas
: public wxWindow
1204 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1205 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1208 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1211 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1214 dc
.DrawBitmap( m_bitmap
, 0, 0);
1220 class wxDataViewDropSource
: public wxDropSource
1223 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1231 ~wxDataViewDropSource()
1236 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1238 wxPoint pos
= wxGetMousePosition();
1242 int liney
= m_win
->GetLineStart( m_row
);
1244 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1245 m_win
->ClientToScreen( &linex
, &liney
);
1246 m_dist_x
= pos
.x
- linex
;
1247 m_dist_y
= pos
.y
- liney
;
1250 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1252 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1253 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1255 wxFRAME_TOOL_WINDOW
|
1256 wxFRAME_FLOAT_ON_PARENT
|
1257 wxFRAME_NO_TASKBAR
|
1259 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1264 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1265 m_hint
->SetTransparent( 128 );
1271 wxDataViewMainWindow
*m_win
;
1274 int m_dist_x
,m_dist_y
;
1278 class wxDataViewDropTarget
: public wxDropTarget
1281 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1287 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1289 wxDataFormat format
= GetMatchingPair();
1290 if (format
== wxDF_INVALID
)
1292 return m_win
->OnDragOver( format
, x
, y
, def
);
1295 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1297 wxDataFormat format
= GetMatchingPair();
1298 if (format
== wxDF_INVALID
)
1300 return m_win
->OnDrop( format
, x
, y
);
1303 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1305 wxDataFormat format
= GetMatchingPair();
1306 if (format
== wxDF_INVALID
)
1310 return m_win
->OnData( format
, x
, y
, def
);
1313 virtual void OnLeave()
1314 { m_win
->OnLeave(); }
1316 wxDataViewMainWindow
*m_win
;
1319 #endif // wxUSE_DRAG_AND_DROP
1321 //-----------------------------------------------------------------------------
1322 // wxDataViewRenameTimer
1323 //-----------------------------------------------------------------------------
1325 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1330 void wxDataViewRenameTimer::Notify()
1332 m_owner
->OnRenameTimer();
1335 //-----------------------------------------------------------------------------
1336 // wxDataViewMainWindow
1337 //-----------------------------------------------------------------------------
1339 // The tree building helper, declared firstly
1340 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1341 wxDataViewTreeNode
* node
);
1343 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1345 if (row1
> row2
) return 1;
1346 if (row1
== row2
) return 0;
1350 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1352 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1353 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1354 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1355 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1356 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1357 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook
)
1358 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1361 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1362 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1363 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1364 m_selection( wxDataViewSelectionCmp
)
1369 m_editorRenderer
= NULL
;
1371 m_lastOnSame
= false;
1372 m_renameTimer
= new wxDataViewRenameTimer( this );
1374 // TODO: user better initial values/nothing selected
1375 m_currentCol
= NULL
;
1376 m_currentColSetByKeyboard
= false;
1377 m_useCellFocus
= false;
1380 m_lineHeight
= wxMax( 17, GetCharHeight() + 4 ); // 17 = mini icon height + 1
1382 #if wxUSE_DRAG_AND_DROP
1384 m_dragStart
= wxPoint(0,0);
1386 m_dragEnabled
= false;
1387 m_dropEnabled
= false;
1389 m_dropHintLine
= (unsigned int) -1;
1390 #endif // wxUSE_DRAG_AND_DROP
1392 m_lineLastClicked
= (unsigned int) -1;
1393 m_lineBeforeLastClicked
= (unsigned int) -1;
1394 m_lineSelectSingleOnUp
= (unsigned int) -1;
1398 SetBackgroundColour( *wxWHITE
);
1400 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1402 m_penRule
= wxPen(GetRuleColour());
1404 // compose a pen whichcan draw black lines
1405 // TODO: maybe there is something system colour to use
1406 m_penExpander
= wxPen(wxColour(0,0,0));
1408 m_root
= wxDataViewTreeNode::CreateRootNode();
1410 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1412 m_underMouse
= NULL
;
1416 wxDataViewMainWindow::~wxDataViewMainWindow()
1419 delete m_renameTimer
;
1423 #if wxUSE_DRAG_AND_DROP
1424 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1426 m_dragFormat
= format
;
1427 m_dragEnabled
= format
!= wxDF_INVALID
;
1432 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1434 m_dropFormat
= format
;
1435 m_dropEnabled
= format
!= wxDF_INVALID
;
1438 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1443 void wxDataViewMainWindow::RemoveDropHint()
1448 RefreshRow( m_dropHintLine
);
1449 m_dropHintLine
= (unsigned int) -1;
1453 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1454 wxCoord y
, wxDragResult def
)
1458 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1459 unsigned int row
= GetLineAt( yy
);
1461 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1467 wxDataViewItem item
= GetItemByRow( row
);
1469 wxDataViewModel
*model
= GetModel();
1471 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1472 event
.SetEventObject( m_owner
);
1473 event
.SetItem( item
);
1474 event
.SetModel( model
);
1475 event
.SetDataFormat( format
);
1476 if (!m_owner
->HandleWindowEvent( event
))
1482 if (!event
.IsAllowed())
1489 if (m_dropHint
&& (row
!= m_dropHintLine
))
1490 RefreshRow( m_dropHintLine
);
1492 m_dropHintLine
= row
;
1498 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1504 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1505 unsigned int row
= GetLineAt( yy
);
1507 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1510 wxDataViewItem item
= GetItemByRow( row
);
1512 wxDataViewModel
*model
= GetModel();
1514 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1515 event
.SetEventObject( m_owner
);
1516 event
.SetItem( item
);
1517 event
.SetModel( model
);
1518 event
.SetDataFormat( format
);
1519 if (!m_owner
->HandleWindowEvent( event
))
1522 if (!event
.IsAllowed())
1528 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1533 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1534 unsigned int row
= GetLineAt( yy
);
1536 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1539 wxDataViewItem item
= GetItemByRow( row
);
1541 wxDataViewModel
*model
= GetModel();
1543 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1545 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1546 event
.SetEventObject( m_owner
);
1547 event
.SetItem( item
);
1548 event
.SetModel( model
);
1549 event
.SetDataFormat( format
);
1550 event
.SetDataSize( obj
->GetSize() );
1551 event
.SetDataBuffer( obj
->GetData() );
1552 if (!m_owner
->HandleWindowEvent( event
))
1555 if (!event
.IsAllowed())
1561 void wxDataViewMainWindow::OnLeave()
1566 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1568 int height
= GetLineHeight( row
);
1570 unsigned int cols
= GetOwner()->GetColumnCount();
1572 for (col
= 0; col
< cols
; col
++)
1574 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1575 if (column
->IsHidden())
1576 continue; // skip it!
1577 width
+= column
->GetWidth();
1583 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1584 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1585 indent
= indent
+ m_lineHeight
;
1586 // try to use the m_lineHeight as the expander space
1590 wxBitmap
bitmap( width
, height
);
1591 wxMemoryDC
dc( bitmap
);
1592 dc
.SetFont( GetFont() );
1593 dc
.SetPen( *wxBLACK_PEN
);
1594 dc
.SetBrush( *wxWHITE_BRUSH
);
1595 dc
.DrawRectangle( 0,0,width
,height
);
1597 wxDataViewModel
*model
= m_owner
->GetModel();
1599 wxDataViewColumn
* const
1600 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1603 for (col
= 0; col
< cols
; col
++)
1605 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1606 wxDataViewRenderer
*cell
= column
->GetRenderer();
1608 if (column
->IsHidden())
1609 continue; // skip it!
1611 width
= column
->GetWidth();
1613 if (column
== expander
)
1616 wxDataViewItem item
= GetItemByRow( row
);
1617 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1619 wxRect
item_rect(x
, 0, width
, height
);
1620 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1622 // dc.SetClippingRegion( item_rect );
1623 cell
->WXCallRender(item_rect
, &dc
, 0);
1624 // dc.DestroyClippingRegion();
1632 #endif // wxUSE_DRAG_AND_DROP
1635 // Draw focus rect for individual cell. Unlike native focus rect, we render
1636 // this in foreground text color (typically white) to enhance contrast and
1638 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1640 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1642 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1643 // may be short traits and not really dots
1645 // note that to behave in the same manner as DrawRect(), we must exclude
1646 // the bottom and right borders from the rectangle
1647 wxCoord x1
= rect
.GetLeft(),
1649 x2
= rect
.GetRight(),
1650 y2
= rect
.GetBottom();
1652 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1655 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1656 dc
.DrawPoint(z
, rect
.GetTop());
1658 wxCoord shift
= z
== x2
? 0 : 1;
1659 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1660 dc
.DrawPoint(x2
, z
);
1662 shift
= z
== y2
? 0 : 1;
1663 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1664 dc
.DrawPoint(z
, y2
);
1666 shift
= z
== x1
? 0 : 1;
1667 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1668 dc
.DrawPoint(x1
, z
);
1672 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1674 wxDataViewModel
*model
= GetModel();
1675 wxAutoBufferedPaintDC
dc( this );
1678 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1679 dc
.SetPen( *wxTRANSPARENT_PEN
);
1680 dc
.DrawRectangle(GetClientSize());
1685 // No items to draw.
1690 GetOwner()->PrepareDC( dc
);
1691 dc
.SetFont( GetFont() );
1693 wxRect update
= GetUpdateRegion().GetBox();
1694 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1696 // compute which items needs to be redrawn
1697 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1698 unsigned int item_count
=
1699 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1700 (int)(GetRowCount( ) - item_start
));
1701 unsigned int item_last
= item_start
+ item_count
;
1703 // Send the event to wxDataViewCtrl itself.
1704 wxWindow
* const parent
= GetParent();
1705 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1706 cache_event
.SetEventObject(parent
);
1707 cache_event
.SetCache(item_start
, item_last
- 1);
1708 parent
->ProcessWindowEvent(cache_event
);
1710 // compute which columns needs to be redrawn
1711 unsigned int cols
= GetOwner()->GetColumnCount();
1714 // we assume that we have at least one column below and painting an
1715 // empty control is unnecessary anyhow
1719 unsigned int col_start
= 0;
1720 unsigned int x_start
;
1721 for (x_start
= 0; col_start
< cols
; col_start
++)
1723 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1724 if (col
->IsHidden())
1725 continue; // skip it!
1727 unsigned int w
= col
->GetWidth();
1728 if (x_start
+w
>= (unsigned int)update
.x
)
1734 unsigned int col_last
= col_start
;
1735 unsigned int x_last
= x_start
;
1736 for (; col_last
< cols
; col_last
++)
1738 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1739 if (col
->IsHidden())
1740 continue; // skip it!
1742 if (x_last
> (unsigned int)update
.GetRight())
1745 x_last
+= col
->GetWidth();
1748 // Draw horizontal rules if required
1749 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1751 dc
.SetPen(m_penRule
);
1752 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1754 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1756 int y
= GetLineStart( i
);
1757 dc
.DrawLine(x_start
, y
, x_last
, y
);
1761 // Draw vertical rules if required
1762 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1764 dc
.SetPen(m_penRule
);
1765 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1767 // NB: Vertical rules are drawn in the last pixel of a column so that
1768 // they align perfectly with native MSW wxHeaderCtrl as well as for
1769 // consistency with MSW native list control. There's no vertical
1770 // rule at the most-left side of the control.
1772 int x
= x_start
- 1;
1773 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1775 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1776 if (col
->IsHidden())
1777 continue; // skip it
1779 x
+= col
->GetWidth();
1781 dc
.DrawLine(x
, GetLineStart( item_start
),
1782 x
, GetLineStart( item_last
) );
1786 // redraw the background for the items which are selected/current
1787 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1789 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1791 if (selected
|| item
== m_currentRow
)
1793 wxRect
rect( x_start
, GetLineStart( item
),
1794 x_last
- x_start
, GetLineHeight( item
) );
1796 // draw selection and whole-item focus:
1799 int flags
= wxCONTROL_SELECTED
;
1801 flags
|= wxCONTROL_FOCUSED
;
1803 wxRendererNative::Get().DrawItemSelectionRect
1812 // draw keyboard focus rect if applicable
1813 if ( item
== m_currentRow
&& m_hasFocus
)
1815 bool renderColumnFocus
= false;
1817 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1819 renderColumnFocus
= true;
1821 // If this is container node without columns, render full-row focus:
1824 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1825 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1826 renderColumnFocus
= false;
1830 if ( renderColumnFocus
)
1832 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1834 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1835 if ( col
->IsHidden() )
1838 rect
.width
= col
->GetWidth();
1840 if ( col
== m_currentCol
)
1842 // make the rect more visible by adding a small
1843 // margin around it:
1848 // DrawFocusRect() uses XOR and is all but
1849 // invisible against dark-blue background. Use
1850 // the same color used for selected text.
1851 DrawSelectedCellFocusRect(dc
, rect
);
1855 wxRendererNative::Get().DrawFocusRect
1866 rect
.x
+= rect
.width
;
1871 // render focus rectangle for the whole row
1872 wxRendererNative::Get().DrawFocusRect
1877 selected
? (int)wxCONTROL_SELECTED
: 0
1884 #if wxUSE_DRAG_AND_DROP
1887 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1888 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1889 dc
.SetPen( *wxBLACK_PEN
);
1890 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1891 dc
.DrawRectangle( rect
);
1893 #endif // wxUSE_DRAG_AND_DROP
1895 wxDataViewColumn
* const
1896 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1898 // redraw all cells for all rows which must be repainted and all columns
1900 cell_rect
.x
= x_start
;
1901 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1903 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1904 wxDataViewRenderer
*cell
= col
->GetRenderer();
1905 cell_rect
.width
= col
->GetWidth();
1907 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1908 continue; // skip it!
1910 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1912 // get the cell value and set it into the renderer
1913 wxDataViewTreeNode
*node
= NULL
;
1914 wxDataViewItem dataitem
;
1916 if (!IsVirtualList())
1918 node
= GetTreeNodeByRow(item
);
1922 dataitem
= node
->GetItem();
1924 // Skip all columns of "container" rows except the expander
1925 // column itself unless HasContainerColumns() overrides this.
1926 if ( col
!= expander
&&
1927 model
->IsContainer(dataitem
) &&
1928 !model
->HasContainerColumns(dataitem
) )
1933 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1936 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1939 cell_rect
.y
= GetLineStart( item
);
1940 cell_rect
.height
= GetLineHeight( item
);
1942 // draw the background
1943 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1945 DrawCellBackground( cell
, dc
, cell_rect
);
1947 // deal with the expander
1949 if ((!IsList()) && (col
== expander
))
1951 // Calculate the indent first
1952 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1954 // we reserve m_lineHeight of horizontal space for the expander
1955 // but leave EXPANDER_MARGIN around the expander itself
1956 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1958 indent
+= m_lineHeight
;
1960 // draw expander if needed and visible
1961 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1963 dc
.SetPen( m_penExpander
);
1964 dc
.SetBrush( wxNullBrush
);
1966 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1967 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1968 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1970 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1973 if ( m_underMouse
== node
)
1974 flag
|= wxCONTROL_CURRENT
;
1975 if ( node
->IsOpen() )
1976 flag
|= wxCONTROL_EXPANDED
;
1978 // ensure that we don't overflow the cell (which might
1979 // happen if the column is very narrow)
1980 wxDCClipper
clip(dc
, cell_rect
);
1982 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1985 // force the expander column to left-center align
1986 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1989 wxRect item_rect
= cell_rect
;
1990 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1992 // account for the tree indent (harmless if we're not indented)
1993 item_rect
.x
+= indent
;
1994 item_rect
.width
-= indent
;
1996 if ( item_rect
.width
<= 0 )
2000 if (m_hasFocus
&& selected
)
2001 state
|= wxDATAVIEW_CELL_SELECTED
;
2003 // TODO: it would be much more efficient to create a clipping
2004 // region for the entire column being rendered (in the OnPaint
2005 // of wxDataViewMainWindow) instead of a single clip region for
2006 // each cell. However it would mean that each renderer should
2007 // respect the given wxRect's top & bottom coords, eventually
2008 // violating only the left & right coords - however the user can
2009 // make its own renderer and thus we cannot be sure of that.
2010 wxDCClipper
clip(dc
, item_rect
);
2012 cell
->WXCallRender(item_rect
, &dc
, state
);
2015 cell_rect
.x
+= cell_rect
.width
;
2020 void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
)
2022 wxRect
rectBg( rect
);
2024 // don't overlap the horizontal rules
2025 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2031 // don't overlap the vertical rules
2032 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2038 cell
->RenderBackground(&dc
, rectBg
);
2041 void wxDataViewMainWindow::OnRenameTimer()
2043 // We have to call this here because changes may just have
2044 // been made and no screen update taken place.
2047 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2048 // (needs to be tested!)
2052 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2054 StartEditing( item
, m_currentCol
);
2058 wxDataViewMainWindow::StartEditing(const wxDataViewItem
& item
,
2059 const wxDataViewColumn
* col
)
2061 wxDataViewRenderer
* renderer
= col
->GetRenderer();
2062 if ( !IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
2065 const wxRect itemRect
= GetItemRect(item
, col
);
2066 if ( renderer
->StartEditing(item
, itemRect
) )
2068 // Save the renderer to be able to finish/cancel editing it later and
2069 // save the control to be able to detect if we're still editing it.
2070 m_editorRenderer
= renderer
;
2071 m_editorCtrl
= renderer
->GetEditorCtrl();
2075 //-----------------------------------------------------------------------------
2076 // Helper class for do operation on the tree node
2077 //-----------------------------------------------------------------------------
2082 virtual ~DoJob() { }
2084 // The return value control how the tree-walker tranverse the tree
2087 DONE
, // Job done, stop traversing and return
2088 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2089 CONTINUE
// Job not done, continue
2092 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2095 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2097 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2099 switch( func( node
) )
2103 case DoJob::SKIP_SUBTREE
:
2105 case DoJob::CONTINUE
:
2109 if ( node
->HasChildren() )
2111 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2113 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2117 if ( Walker(*i
, func
) )
2125 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2127 if (IsVirtualList())
2129 wxDataViewVirtualListModel
*list_model
=
2130 (wxDataViewVirtualListModel
*) GetModel();
2131 m_count
= list_model
->GetCount();
2137 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2142 wxDataViewItemArray modelSiblings
;
2143 GetModel()->GetChildren(parent
, modelSiblings
);
2144 const int modelSiblingsSize
= modelSiblings
.size();
2146 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2147 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2149 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2150 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2152 parentNode
->SetHasChildren(true);
2154 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2155 const int nodeSiblingsSize
= nodeSiblings
.size();
2159 if ( posInModel
== modelSiblingsSize
- 1 )
2161 nodePos
= nodeSiblingsSize
;
2163 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2165 // This is the simple case when our node tree already matches the
2166 // model and only this one item is missing.
2167 nodePos
= posInModel
;
2171 // It's possible that a larger discrepancy between the model and
2172 // our realization exists. This can happen e.g. when adding a bunch
2173 // of items to the model and then calling ItemsAdded() just once
2174 // afterwards. In this case, we must find the right position by
2175 // looking at sibling items.
2177 // append to the end if we won't find a better position:
2178 nodePos
= nodeSiblingsSize
;
2180 for ( int nextItemPos
= posInModel
+ 1;
2181 nextItemPos
< modelSiblingsSize
;
2184 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2185 if ( nextNodePos
!= wxNOT_FOUND
)
2187 nodePos
= nextNodePos
;
2193 parentNode
->ChangeSubTreeCount(+1);
2194 parentNode
->InsertChild(itemNode
, nodePos
);
2199 GetOwner()->InvalidateColBestWidths();
2205 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2206 const wxDataViewItem
& item
)
2208 if (IsVirtualList())
2210 wxDataViewVirtualListModel
*list_model
=
2211 (wxDataViewVirtualListModel
*) GetModel();
2212 m_count
= list_model
->GetCount();
2214 if ( !m_selection
.empty() )
2216 const int row
= GetRowByItem(item
);
2218 int rowIndexInSelection
= wxNOT_FOUND
;
2220 const size_t selCount
= m_selection
.size();
2221 for ( size_t i
= 0; i
< selCount
; i
++ )
2223 if ( m_selection
[i
] == (unsigned)row
)
2224 rowIndexInSelection
= i
;
2225 else if ( m_selection
[i
] > (unsigned)row
)
2229 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2230 m_selection
.RemoveAt(rowIndexInSelection
);
2234 else // general case
2236 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2238 // Notice that it is possible that the item being deleted is not in the
2239 // tree at all, for example we could be deleting a never shown (because
2240 // collapsed) item in a tree model. So it's not an error if we don't know
2241 // about this item, just return without doing anything then.
2245 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2246 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2248 // We can't use FindNode() to find 'item', because it was already
2249 // removed from the model by the time ItemDeleted() is called, so we
2250 // have to do it manually. We keep track of its position as well for
2252 int itemPosInNode
= 0;
2253 wxDataViewTreeNode
*itemNode
= NULL
;
2254 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2255 i
!= parentsChildren
.end();
2256 ++i
, ++itemPosInNode
)
2258 if( (*i
)->GetItem() == item
)
2265 // If the parent wasn't expanded, it's possible that we didn't have a
2266 // node corresponding to 'item' and so there's nothing left to do.
2269 // If this was the last child to be removed, it's possible the parent
2270 // node became a leaf. Let's ask the model about it.
2271 if ( parentNode
->GetChildNodes().empty() )
2272 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2277 // Delete the item from wxDataViewTreeNode representation:
2278 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2280 parentNode
->RemoveChild(itemNode
);
2282 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2284 // Make the row number invalid and get a new valid one when user call GetRowCount
2287 // If this was the last child to be removed, it's possible the parent
2288 // node became a leaf. Let's ask the model about it.
2289 if ( parentNode
->GetChildNodes().empty() )
2290 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2292 // Update selection by removing 'item' and its entire children tree from the selection.
2293 if ( !m_selection
.empty() )
2295 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2296 // the parent ('parentNode') and position in its list of children
2298 if ( itemPosInNode
== 0 )
2300 // 1st child, row number is that of the parent parentNode + 1
2301 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2305 // row number is that of the sibling above 'item' + its subtree if any + 1
2306 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2308 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2309 siblingNode
->GetSubTreeCount() +
2313 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2315 const size_t numSelections
= m_selection
.size();
2316 for ( size_t i
= 0; i
< numSelections
; ++i
)
2318 const int s
= m_selection
[i
];
2320 newsel
.push_back(s
);
2321 else if ( s
>= itemRow
+ itemsDeleted
)
2322 newsel
.push_back(s
- itemsDeleted
);
2323 // else: deleted item, remove from selection
2326 m_selection
= newsel
;
2330 // Change the current row to the last row if the current exceed the max row number
2331 if( m_currentRow
> GetRowCount() )
2332 ChangeCurrentRow(m_count
- 1);
2334 GetOwner()->InvalidateColBestWidths();
2340 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2345 GetOwner()->InvalidateColBestWidths();
2348 wxWindow
*parent
= GetParent();
2349 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2350 le
.SetEventObject(parent
);
2351 le
.SetModel(GetModel());
2353 parent
->ProcessWindowEvent(le
);
2358 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2360 int view_column
= -1;
2361 unsigned int n_col
= m_owner
->GetColumnCount();
2362 for (unsigned i
= 0; i
< n_col
; i
++)
2364 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2365 if (column
->GetModelColumn() == model_column
)
2367 view_column
= (int) i
;
2371 if (view_column
== -1)
2374 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2375 /*#define MAX_VIRTUAL_WIDTH 100000
2377 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2378 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2379 Refresh( true, &rect );
2386 GetOwner()->InvalidateColBestWidth(view_column
);
2389 wxWindow
*parent
= GetParent();
2390 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2391 le
.SetEventObject(parent
);
2392 le
.SetModel(GetModel());
2394 le
.SetColumn(view_column
);
2395 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2396 parent
->ProcessWindowEvent(le
);
2401 bool wxDataViewMainWindow::Cleared()
2404 m_selection
.Clear();
2407 BuildTree( GetModel() );
2409 GetOwner()->InvalidateColBestWidths();
2415 void wxDataViewMainWindow::UpdateDisplay()
2418 m_underMouse
= NULL
;
2421 void wxDataViewMainWindow::OnInternalIdle()
2423 wxWindow::OnInternalIdle();
2427 RecalculateDisplay();
2432 void wxDataViewMainWindow::RecalculateDisplay()
2434 wxDataViewModel
*model
= GetModel();
2441 int width
= GetEndOfLastCol();
2442 int height
= GetLineStart( GetRowCount() );
2444 SetVirtualSize( width
, height
);
2445 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2450 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2452 m_underMouse
= NULL
;
2454 wxWindow::ScrollWindow( dx
, dy
, rect
);
2456 if (GetOwner()->m_headerArea
)
2457 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2460 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2462 m_underMouse
= NULL
;
2465 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2466 int sy
= GetLineStart( rows
)/y
;
2470 wxRect rect
= GetClientRect();
2474 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2475 for (x_start
= 0; colnum
< column
; colnum
++)
2477 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2478 if (col
->IsHidden())
2479 continue; // skip it!
2481 w
= col
->GetWidth();
2485 int x_end
= x_start
+ w
;
2486 xe
= xx
+ rect
.width
;
2489 sx
= ( xx
+ x_end
- xe
)/x
;
2496 m_owner
->Scroll( sx
, sy
);
2499 int wxDataViewMainWindow::GetCountPerPage() const
2501 wxSize size
= GetClientSize();
2502 return size
.y
/ m_lineHeight
;
2505 int wxDataViewMainWindow::GetEndOfLastCol() const
2509 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2511 const wxDataViewColumn
*c
=
2512 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2515 width
+= c
->GetWidth();
2520 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2524 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2526 return GetLineAt( y
);
2529 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2531 wxSize client_size
= GetClientSize();
2532 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2533 &client_size
.x
, &client_size
.y
);
2535 // we should deal with the pixel here
2536 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2538 return wxMin( GetRowCount()-1, row
);
2541 unsigned int wxDataViewMainWindow::GetRowCount()
2543 if ( m_count
== -1 )
2545 m_count
= RecalculateCount();
2551 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2558 void wxDataViewMainWindow::SelectAllRows( bool on
)
2565 m_selection
.Clear();
2566 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2567 m_selection
.Add( i
);
2572 unsigned int first_visible
= GetFirstVisibleRow();
2573 unsigned int last_visible
= GetLastVisibleRow();
2575 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2577 unsigned int row
= m_selection
[i
];
2578 if ((row
>= first_visible
) && (row
<= last_visible
))
2581 m_selection
.Clear();
2585 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2587 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2591 m_selection
.Add( row
);
2599 m_selection
.Remove( row
);
2605 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2609 unsigned int tmp
= from
;
2615 for (i
= from
; i
<= to
; i
++)
2617 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2620 m_selection
.Add( i
);
2625 m_selection
.Remove( i
);
2628 RefreshRows( from
, to
);
2631 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2633 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2635 int n
= aSelections
[i
];
2637 m_selection
.Add( n
);
2642 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2644 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2645 m_selection
.Add( row
);
2647 m_selection
.Remove( row
);
2651 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2653 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2656 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2658 wxWindow
*parent
= GetParent();
2659 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2661 le
.SetEventObject(parent
);
2662 le
.SetModel(GetModel());
2665 parent
->ProcessWindowEvent(le
);
2668 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2670 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2671 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2673 wxSize client_size
= GetClientSize();
2674 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2675 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2676 if (intersect_rect
.width
> 0)
2677 Refresh( true, &intersect_rect
);
2680 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2684 unsigned int tmp
= to
;
2689 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2690 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2692 wxSize client_size
= GetClientSize();
2693 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2694 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2695 if (intersect_rect
.width
> 0)
2696 Refresh( true, &intersect_rect
);
2699 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2701 wxSize client_size
= GetClientSize();
2702 int start
= GetLineStart( firstRow
);
2703 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2704 if (start
> client_size
.y
) return;
2706 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2708 Refresh( true, &rect
);
2711 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2715 rect
.y
= GetLineStart( row
);
2716 rect
.width
= GetEndOfLastCol();
2717 rect
.height
= GetLineHeight( row
);
2722 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2724 const wxDataViewModel
*model
= GetModel();
2726 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2728 // TODO make more efficient
2733 for (r
= 0; r
< row
; r
++)
2735 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2736 if (!node
) return start
;
2738 wxDataViewItem item
= node
->GetItem();
2740 unsigned int cols
= GetOwner()->GetColumnCount();
2742 int height
= m_lineHeight
;
2743 for (col
= 0; col
< cols
; col
++)
2745 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2746 if (column
->IsHidden())
2747 continue; // skip it!
2750 model
->IsContainer(item
) &&
2751 !model
->HasContainerColumns(item
))
2752 continue; // skip it!
2754 wxDataViewRenderer
*renderer
=
2755 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2756 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2758 height
= wxMax( height
, renderer
->GetSize().y
);
2768 return row
* m_lineHeight
;
2772 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2774 const wxDataViewModel
*model
= GetModel();
2776 // check for the easy case first
2777 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2778 return y
/ m_lineHeight
;
2780 // TODO make more efficient
2781 unsigned int row
= 0;
2782 unsigned int yy
= 0;
2785 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2788 // not really correct...
2789 return row
+ ((y
-yy
) / m_lineHeight
);
2792 wxDataViewItem item
= node
->GetItem();
2794 unsigned int cols
= GetOwner()->GetColumnCount();
2796 int height
= m_lineHeight
;
2797 for (col
= 0; col
< cols
; col
++)
2799 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2800 if (column
->IsHidden())
2801 continue; // skip it!
2804 model
->IsContainer(item
) &&
2805 !model
->HasContainerColumns(item
))
2806 continue; // skip it!
2808 wxDataViewRenderer
*renderer
=
2809 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2810 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2812 height
= wxMax( height
, renderer
->GetSize().y
);
2823 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2825 const wxDataViewModel
*model
= GetModel();
2827 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2829 wxASSERT( !IsVirtualList() );
2831 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2832 // wxASSERT( node );
2833 if (!node
) return m_lineHeight
;
2835 wxDataViewItem item
= node
->GetItem();
2837 int height
= m_lineHeight
;
2839 unsigned int cols
= GetOwner()->GetColumnCount();
2841 for (col
= 0; col
< cols
; col
++)
2843 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2844 if (column
->IsHidden())
2845 continue; // skip it!
2848 model
->IsContainer(item
) &&
2849 !model
->HasContainerColumns(item
))
2850 continue; // skip it!
2852 wxDataViewRenderer
*renderer
=
2853 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2854 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2856 height
= wxMax( height
, renderer
->GetSize().y
);
2863 return m_lineHeight
;
2868 class RowToTreeNodeJob
: public DoJob
2871 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2874 this->current
= current
;
2879 virtual int operator() ( wxDataViewTreeNode
* node
)
2882 if( current
== static_cast<int>(row
))
2888 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2890 current
+= node
->GetSubTreeCount();
2891 return DoJob::SKIP_SUBTREE
;
2897 // If the current node has only leaf children, we can find the
2898 // desired node directly. This can speed up finding the node
2899 // in some cases, and will have a very good effect for list views.
2900 if ( node
->HasChildren() &&
2901 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2903 const int index
= static_cast<int>(row
) - current
- 1;
2904 ret
= node
->GetChildNodes()[index
];
2908 return DoJob::CONTINUE
;
2912 wxDataViewTreeNode
* GetResult() const
2918 wxDataViewTreeNode
* ret
;
2919 wxDataViewTreeNode
* parent
;
2922 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2924 wxASSERT( !IsVirtualList() );
2926 RowToTreeNodeJob
job( row
, -2, m_root
);
2927 Walker( m_root
, job
);
2928 return job
.GetResult();
2931 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2933 if (IsVirtualList())
2935 return wxDataViewItem( wxUIntToPtr(row
+1) );
2939 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2940 return node
? node
->GetItem() : wxDataViewItem();
2945 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2946 const wxDataViewItem
& item
)
2948 wxWindow
*parent
= GetParent();
2949 wxDataViewEvent
le(type
, parent
->GetId());
2951 le
.SetEventObject(parent
);
2952 le
.SetModel(GetModel());
2955 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2958 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2963 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2967 if (!node
->HasChildren())
2970 return node
->IsOpen();
2973 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2978 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2982 if (!node
->HasChildren())
2988 void wxDataViewMainWindow::Expand( unsigned int row
)
2993 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2997 if (!node
->HasChildren())
3000 if (!node
->IsOpen())
3002 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
3004 // Vetoed by the event handler.
3010 // build the children of current node
3011 if( node
->GetChildNodes().empty() )
3014 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
3017 // By expanding the node all row indices that are currently in the selection list
3018 // and are greater than our node have become invalid. So we have to correct that now.
3019 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3020 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3022 const unsigned testRow
= m_selection
[i
];
3023 // all rows above us are not affected, so skip them
3027 m_selection
[i
] += rowAdjustment
;
3030 if(m_currentRow
> row
)
3031 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3035 // Send the expanded event
3036 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3040 void wxDataViewMainWindow::Collapse(unsigned int row
)
3045 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3049 if (!node
->HasChildren())
3054 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3056 // Vetoed by the event handler.
3060 // Find out if there are selected items below the current node.
3061 bool selectCollapsingRow
= false;
3062 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3063 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3064 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3066 const unsigned testRow
= m_selection
[i
];
3067 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3069 selectCollapsingRow
= true;
3070 // get out as soon as we have found a node that is selected
3077 // If the node to be closed has selected items the user won't see those any longer.
3078 // We select the collapsing node in this case.
3079 if(selectCollapsingRow
)
3081 SelectAllRows(false);
3082 ChangeCurrentRow(row
);
3083 SelectRow(row
, true);
3084 SendSelectionChangedEvent(GetItemByRow(row
));
3088 // if there were no selected items below our node we still need to "fix" the
3089 // selection list to adjust for the changing of the row indices.
3090 // We actually do the opposite of what we are doing in Expand().
3091 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3093 const unsigned testRow
= m_selection
[i
];
3094 // all rows above us are not affected, so skip them
3098 m_selection
[i
] -= rowAdjustment
;
3101 // if the "current row" is being collapsed away we change it to the current row ;-)
3102 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3103 ChangeCurrentRow(row
);
3104 else if(m_currentRow
> row
)
3105 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3110 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3114 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3116 const wxDataViewModel
* model
= GetModel();
3123 // Compose the parent-chain for the item we are looking for
3124 wxVector
<wxDataViewItem
> parentChain
;
3125 wxDataViewItem
it( item
);
3128 parentChain
.push_back(it
);
3129 it
= model
->GetParent(it
);
3132 // Find the item along the parent-chain.
3133 // This algorithm is designed to speed up the node-finding method
3134 wxDataViewTreeNode
* node
= m_root
;
3135 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3137 if( node
->HasChildren() )
3139 if( node
->GetChildNodes().empty() )
3141 // Even though the item is a container, it doesn't have any
3142 // child nodes in the control's representation yet. We have
3143 // to realize its subtree now.
3145 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3148 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3151 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3153 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3154 if (currentNode
->GetItem() == parentChain
[iter
])
3156 if (currentNode
->GetItem() == item
)
3176 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3177 wxDataViewColumn
* &column
)
3179 wxDataViewColumn
*col
= NULL
;
3180 unsigned int cols
= GetOwner()->GetColumnCount();
3181 unsigned int colnum
= 0;
3183 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3184 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3186 col
= GetOwner()->GetColumnAt(colnum
);
3187 if (col
->IsHidden())
3188 continue; // skip it!
3190 unsigned int w
= col
->GetWidth();
3191 if (x_start
+w
>= (unsigned int)x
)
3198 item
= GetItemByRow( GetLineAt( y
) );
3201 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3202 const wxDataViewColumn
* column
)
3207 unsigned int cols
= GetOwner()->GetColumnCount();
3208 // If column is null the loop will compute the combined width of all columns.
3209 // Otherwise, it will compute the x position of the column we are looking for.
3210 for (unsigned int i
= 0; i
< cols
; i
++)
3212 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3217 if (col
->IsHidden())
3218 continue; // skip it!
3220 xpos
+= col
->GetWidth();
3221 width
+= col
->GetWidth();
3226 // If we have a column, we need can get its width directly.
3227 if(column
->IsHidden())
3230 width
= column
->GetWidth();
3235 // If we have no column, we reset the x position back to zero.
3239 // we have to take an expander column into account and compute its indentation
3240 // to get the correct x position where the actual text is
3242 int row
= GetRowByItem(item
);
3244 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3246 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3247 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3248 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3251 wxRect
itemRect( xpos
+ indent
,
3252 GetLineStart( row
),
3254 GetLineHeight( row
) );
3256 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3257 &itemRect
.x
, &itemRect
.y
);
3262 int wxDataViewMainWindow::RecalculateCount()
3264 if (IsVirtualList())
3266 wxDataViewVirtualListModel
*list_model
=
3267 (wxDataViewVirtualListModel
*) GetModel();
3269 return list_model
->GetCount();
3273 return m_root
->GetSubTreeCount();
3277 class ItemToRowJob
: public DoJob
3280 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3287 // Maybe binary search will help to speed up this process
3288 virtual int operator() ( wxDataViewTreeNode
* node
)
3291 if( node
->GetItem() == item
)
3296 if( node
->GetItem() == *m_iter
)
3299 return DoJob::CONTINUE
;
3303 ret
+= node
->GetSubTreeCount();
3304 return DoJob::SKIP_SUBTREE
;
3309 // the row number is begin from zero
3310 int GetResult() const
3314 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3315 wxDataViewItem item
;
3320 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3322 const wxDataViewModel
* model
= GetModel();
3326 if (IsVirtualList())
3328 return wxPtrToUInt( item
.GetID() ) -1;
3335 // Compose the parent-chain of the item we are looking for
3336 wxVector
<wxDataViewItem
> parentChain
;
3337 wxDataViewItem
it( item
);
3340 parentChain
.push_back(it
);
3341 it
= model
->GetParent(it
);
3344 // add an 'invalid' item to represent our 'invisible' root node
3345 parentChain
.push_back(wxDataViewItem());
3347 // the parent chain was created by adding the deepest parent first.
3348 // so if we want to start at the root node, we have to iterate backwards through the vector
3349 ItemToRowJob
job( item
, parentChain
.rbegin() );
3350 Walker( m_root
, job
);
3351 return job
.GetResult();
3355 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3356 wxDataViewTreeNode
* node
)
3358 if( !model
->IsContainer( item
) )
3361 wxDataViewItemArray children
;
3362 unsigned int num
= model
->GetChildren( item
, children
);
3364 for ( unsigned int index
= 0; index
< num
; index
++ )
3366 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3368 if( model
->IsContainer(children
[index
]) )
3369 n
->SetHasChildren( true );
3371 node
->InsertChild(n
, index
);
3374 wxASSERT( node
->IsOpen() );
3375 node
->ChangeSubTreeCount(+num
);
3378 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3382 if (GetModel()->IsVirtualListModel())
3388 m_root
= wxDataViewTreeNode::CreateRootNode();
3390 // First we define a invalid item to fetch the top-level elements
3391 wxDataViewItem item
;
3393 BuildTreeHelper( model
, item
, m_root
);
3397 void wxDataViewMainWindow::DestroyTree()
3399 if (!IsVirtualList())
3407 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3409 // Edit the current column editable in 'mode'. If no column is focused
3410 // (typically because the user has full row selected), try to find the
3411 // first editable column (this would typically be a checkbox for
3412 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3413 // focus on the checkbox column; or on the only editable text column).
3415 wxDataViewColumn
*candidate
= m_currentCol
;
3418 !IsCellEditableInMode(item
, candidate
, mode
) &&
3419 !m_currentColSetByKeyboard
)
3421 // If current column was set by mouse to something not editable (in
3422 // 'mode') and the user pressed Space/F2 to edit it, treat the
3423 // situation as if there was whole-row focus, because that's what is
3424 // visually indicated and the mouse click could very well be targeted
3425 // on the row rather than on an individual cell.
3427 // But if it was done by keyboard, respect that even if the column
3428 // isn't editable, because focus is visually on that column and editing
3429 // something else would be surprising.
3435 const unsigned cols
= GetOwner()->GetColumnCount();
3436 for ( unsigned i
= 0; i
< cols
; i
++ )
3438 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3439 if ( c
->IsHidden() )
3442 if ( IsCellEditableInMode(item
, c
, mode
) )
3450 // If on container item without columns, only the expander column
3451 // may be directly editable:
3453 GetOwner()->GetExpanderColumn() != candidate
&&
3454 GetModel()->IsContainer(item
) &&
3455 !GetModel()->HasContainerColumns(item
) )
3457 candidate
= GetOwner()->GetExpanderColumn();
3463 if ( !IsCellEditableInMode(item
, candidate
, mode
) )
3469 bool wxDataViewMainWindow::IsCellEditableInMode(const wxDataViewItem
& item
,
3470 const wxDataViewColumn
*col
,
3471 wxDataViewCellMode mode
) const
3473 if ( col
->GetRenderer()->GetMode() != mode
)
3476 if ( !GetModel()->IsEnabled(item
, col
->GetModelColumn()) )
3482 void wxDataViewMainWindow::OnCharHook(wxKeyEvent
& event
)
3486 // Handle any keys special for the in-place editor and return without
3487 // calling Skip() below.
3488 switch ( event
.GetKeyCode() )
3491 m_editorRenderer
->CancelEditing();
3495 m_editorRenderer
->FinishEditing();
3503 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3505 wxWindow
* const parent
= GetParent();
3507 // propagate the char event upwards
3508 wxKeyEvent
eventForParent(event
);
3509 eventForParent
.SetEventObject(parent
);
3510 if ( parent
->ProcessWindowEvent(eventForParent
) )
3513 if ( parent
->HandleAsNavigationKey(event
) )
3516 // no item -> nothing to do
3517 if (!HasCurrentRow())
3523 // don't use m_linesPerPage directly as it might not be computed yet
3524 const int pageSize
= GetCountPerPage();
3525 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3527 switch ( event
.GetKeyCode() )
3531 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3532 // it. Only if that event is not handled do we activate column renderer (which
3533 // is normally done by Space) or even inline editing.
3535 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3537 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3540 le
.SetEventObject(parent
);
3541 le
.SetModel(GetModel());
3543 if ( parent
->ProcessWindowEvent(le
) )
3545 // else: fall through to WXK_SPACE handling
3550 // Space toggles activatable items or -- if not activatable --
3551 // starts inline editing (this is normally done using F2 on
3552 // Windows, but Space is common everywhere else, so use it too
3553 // for greater cross-platform compatibility).
3555 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3557 // Activate the current activatable column. If not column is focused (typically
3558 // because the user has full row selected), try to find the first activatable
3559 // column (this would typically be a checkbox and we don't want to force the user
3560 // to set focus on the checkbox column).
3561 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3563 if ( activatableCol
)
3565 const unsigned colIdx
= activatableCol
->GetModelColumn();
3566 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3568 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3569 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3570 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3574 // else: fall through to WXK_F2 handling
3579 if( !m_selection
.empty() )
3581 // Mimic Windows 7 behavior: edit the item that has focus
3582 // if it is selected and the first selected item if focus
3583 // is out of selection.
3585 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3588 sel
= m_selection
[0];
3591 const wxDataViewItem item
= GetItemByRow(sel
);
3593 // Edit the current column. If no column is focused
3594 // (typically because the user has full row selected), try
3595 // to find the first editable column.
3596 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3599 GetOwner()->StartEditor(item
, GetOwner()->GetColumnIndex(editableCol
));
3605 if ( m_currentRow
> 0 )
3606 OnVerticalNavigation( m_currentRow
- 1, event
);
3610 if ( m_currentRow
+ 1 < GetRowCount() )
3611 OnVerticalNavigation( m_currentRow
+ 1, event
);
3613 // Add the process for tree expanding/collapsing
3625 OnVerticalNavigation( GetRowCount() - 1, event
);
3630 OnVerticalNavigation( 0, event
);
3635 int steps
= pageSize
- 1;
3636 int index
= m_currentRow
- steps
;
3640 OnVerticalNavigation( index
, event
);
3646 int steps
= pageSize
- 1;
3647 unsigned int index
= m_currentRow
+ steps
;
3648 unsigned int count
= GetRowCount();
3649 if ( index
>= count
)
3652 OnVerticalNavigation( index
, event
);
3661 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3663 wxCHECK_RET( newCurrent
< GetRowCount(),
3664 wxT("invalid item index in OnVerticalNavigation()") );
3666 // if there is no selection, we cannot move it anywhere
3667 if (!HasCurrentRow())
3670 unsigned int oldCurrent
= m_currentRow
;
3672 // in single selection we just ignore Shift as we can't select several
3674 if ( event
.ShiftDown() && !IsSingleSel() )
3676 RefreshRow( oldCurrent
);
3678 ChangeCurrentRow( newCurrent
);
3680 // select all the items between the old and the new one
3681 if ( oldCurrent
> newCurrent
)
3683 newCurrent
= oldCurrent
;
3684 oldCurrent
= m_currentRow
;
3687 SelectRows( oldCurrent
, newCurrent
, true );
3688 if (oldCurrent
!=newCurrent
)
3689 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3693 RefreshRow( oldCurrent
);
3695 // all previously selected items are unselected unless ctrl is held
3696 if ( !event
.ControlDown() )
3697 SelectAllRows(false);
3699 ChangeCurrentRow( newCurrent
);
3701 if ( !event
.ControlDown() )
3703 SelectRow( m_currentRow
, true );
3704 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3707 RefreshRow( m_currentRow
);
3710 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3713 void wxDataViewMainWindow::OnLeftKey()
3717 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3721 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3723 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3726 // Because TryAdvanceCurrentColumn() return false, we are at the first
3727 // column or using whole-row selection. In this situation, we can use
3728 // the standard TreeView handling of the left key.
3729 if (node
->HasChildren() && node
->IsOpen())
3731 Collapse(m_currentRow
);
3735 // if the node is already closed, we move the selection to its parent
3736 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3740 int parent
= GetRowByItem( parent_node
->GetItem() );
3743 unsigned int row
= m_currentRow
;
3744 SelectRow( row
, false);
3745 SelectRow( parent
, true );
3746 ChangeCurrentRow( parent
);
3747 GetOwner()->EnsureVisible( parent
, -1 );
3748 SendSelectionChangedEvent( parent_node
->GetItem() );
3755 void wxDataViewMainWindow::OnRightKey()
3759 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3763 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3765 if ( node
->HasChildren() )
3767 if ( !node
->IsOpen() )
3769 Expand( m_currentRow
);
3773 // if the node is already open, we move the selection to the first child
3774 unsigned int row
= m_currentRow
;
3775 SelectRow( row
, false );
3776 SelectRow( row
+ 1, true );
3777 ChangeCurrentRow( row
+ 1 );
3778 GetOwner()->EnsureVisible( row
+ 1, -1 );
3779 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3784 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3789 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3791 if ( GetOwner()->GetColumnCount() == 0 )
3794 if ( !m_useCellFocus
)
3799 // navigation shouldn't work in branch nodes without other columns:
3800 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3804 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3808 m_currentCol
= GetOwner()->GetColumnAt(1);
3809 m_currentColSetByKeyboard
= true;
3810 RefreshRow(m_currentRow
);
3817 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3819 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3822 GetOwner()->EnsureVisible(m_currentRow
, idx
);
3826 // We are going to the left of the second column. Reset to whole-row
3827 // focus (which means first column would be edited).
3828 m_currentCol
= NULL
;
3829 RefreshRow(m_currentRow
);
3833 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3834 m_currentColSetByKeyboard
= true;
3835 RefreshRow(m_currentRow
);
3839 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3841 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3843 // let the base handle mouse wheel events.
3848 // set the focus to ourself if any of the mouse buttons are pressed
3849 if(event
.ButtonDown() && !HasFocus())
3852 int x
= event
.GetX();
3853 int y
= event
.GetY();
3854 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3855 wxDataViewColumn
*col
= NULL
;
3858 unsigned int cols
= GetOwner()->GetColumnCount();
3860 for (i
= 0; i
< cols
; i
++)
3862 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3864 continue; // skip it!
3866 if (x
< xpos
+ c
->GetWidth())
3871 xpos
+= c
->GetWidth();
3874 wxDataViewModel
* const model
= GetModel();
3876 const unsigned int current
= GetLineAt( y
);
3877 const wxDataViewItem item
= GetItemByRow(current
);
3879 // Handle right clicking here, before everything else as context menu
3880 // events should be sent even when we click outside of any item, unlike all
3882 if (event
.RightUp())
3884 wxWindow
*parent
= GetParent();
3885 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3886 le
.SetEventObject(parent
);
3889 if ( item
.IsOk() && col
)
3892 le
.SetColumn( col
->GetModelColumn() );
3893 le
.SetDataViewColumn( col
);
3896 model
->GetValue( value
, item
, col
->GetModelColumn() );
3900 parent
->ProcessWindowEvent(le
);
3910 wxDataViewRenderer
*cell
= col
->GetRenderer();
3911 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3913 // Unselect all if below the last row ?
3918 wxDataViewColumn
* const
3919 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3921 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3922 // button) and also determine the offset of the real cell start, skipping
3923 // the indentation and the expander itself.
3924 bool hoverOverExpander
= false;
3926 if ((!IsList()) && (expander
== col
))
3928 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3930 int indent
= node
->GetIndentLevel();
3931 itemOffset
= GetOwner()->GetIndent()*indent
;
3933 if ( node
->HasChildren() )
3935 // we make the rectangle we are looking in a bit bigger than the actual
3936 // visual expander so the user can hit that little thing reliably
3937 wxRect
rect(itemOffset
,
3938 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3939 m_lineHeight
, m_lineHeight
);
3941 if( rect
.Contains(x
, y
) )
3943 // So the mouse is over the expander
3944 hoverOverExpander
= true;
3945 if (m_underMouse
&& m_underMouse
!= node
)
3947 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3948 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3950 if (m_underMouse
!= node
)
3952 // wxLogMessage("Do the row: %d", current);
3953 RefreshRow(current
);
3955 m_underMouse
= node
;
3959 // Account for the expander as well, even if this item doesn't have it,
3960 // its parent does so it still counts for the offset.
3961 itemOffset
+= m_lineHeight
;
3963 if (!hoverOverExpander
)
3965 if (m_underMouse
!= NULL
)
3967 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3968 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3969 m_underMouse
= NULL
;
3973 #if wxUSE_DRAG_AND_DROP
3974 if (event
.Dragging())
3976 if (m_dragCount
== 0)
3978 // we have to report the raw, physical coords as we want to be
3979 // able to call HitTest(event.m_pointDrag) from the user code to
3980 // get the item being dragged
3981 m_dragStart
= event
.GetPosition();
3986 if (m_dragCount
!= 3)
3989 if (event
.LeftIsDown())
3991 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3992 &m_dragStart
.x
, &m_dragStart
.y
);
3993 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3994 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
3996 // Notify cell about drag
3997 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3998 event
.SetEventObject( m_owner
);
3999 event
.SetItem( itemDragged
);
4000 event
.SetModel( model
);
4001 if (!m_owner
->HandleWindowEvent( event
))
4004 if (!event
.IsAllowed())
4007 wxDataObject
*obj
= event
.GetDataObject();
4011 wxDataViewDropSource
drag( this, drag_item_row
);
4012 drag
.SetData( *obj
);
4013 /* wxDragResult res = */ drag
.DoDragDrop();
4022 #endif // wxUSE_DRAG_AND_DROP
4024 bool simulateClick
= false;
4026 if (event
.ButtonDClick())
4028 m_renameTimer
->Stop();
4029 m_lastOnSame
= false;
4032 bool ignore_other_columns
=
4033 ((expander
!= col
) &&
4034 (model
->IsContainer(item
)) &&
4035 (!model
->HasContainerColumns(item
)));
4037 if (event
.LeftDClick())
4039 if(hoverOverExpander
)
4041 // a double click on the expander will be converted into a "simulated" normal click
4042 simulateClick
= true;
4044 else if ( current
== m_lineLastClicked
)
4046 wxWindow
*parent
= GetParent();
4047 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4049 le
.SetColumn( col
->GetModelColumn() );
4050 le
.SetDataViewColumn( col
);
4051 le
.SetEventObject(parent
);
4052 le
.SetModel(GetModel());
4054 parent
->ProcessWindowEvent(le
);
4059 // The first click was on another item, so don't interpret this as
4060 // a double click, but as a simple click instead
4061 simulateClick
= true;
4065 if (event
.LeftUp() && !hoverOverExpander
)
4067 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4069 // select single line
4070 SelectAllRows( false );
4071 SelectRow( m_lineSelectSingleOnUp
, true );
4072 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4075 // If the user click the expander, we do not do editing even if the column
4076 // with expander are editable
4077 if (m_lastOnSame
&& !ignore_other_columns
)
4079 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4080 IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
4082 m_renameTimer
->Start( 100, true );
4086 m_lastOnSame
= false;
4087 m_lineSelectSingleOnUp
= (unsigned int)-1;
4089 else if(!event
.LeftUp())
4091 // This is necessary, because after a DnD operation in
4092 // from and to ourself, the up event is swallowed by the
4093 // DnD code. So on next non-up event (which means here and
4094 // now) m_lineSelectSingleOnUp should be reset.
4095 m_lineSelectSingleOnUp
= (unsigned int)-1;
4098 if (event
.RightDown())
4100 m_lineBeforeLastClicked
= m_lineLastClicked
;
4101 m_lineLastClicked
= current
;
4103 // If the item is already selected, do not update the selection.
4104 // Multi-selections should not be cleared if a selected item is clicked.
4105 if (!IsRowSelected(current
))
4107 SelectAllRows(false);
4108 const unsigned oldCurrent
= m_currentRow
;
4109 ChangeCurrentRow(current
);
4110 SelectRow(m_currentRow
,true);
4111 RefreshRow(oldCurrent
);
4112 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4115 else if (event
.MiddleDown())
4119 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4121 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4123 // hoverOverExpander being true tells us that our node must be
4124 // valid and have children.
4125 // So we don't need any extra checks.
4126 if( node
->IsOpen() )
4131 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4133 m_lineBeforeLastClicked
= m_lineLastClicked
;
4134 m_lineLastClicked
= current
;
4136 unsigned int oldCurrentRow
= m_currentRow
;
4137 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4139 bool cmdModifierDown
= event
.CmdDown();
4140 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4142 if ( IsSingleSel() || !IsRowSelected(current
) )
4144 SelectAllRows( false );
4145 ChangeCurrentRow(current
);
4146 SelectRow(m_currentRow
,true);
4147 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4149 else // multi sel & current is highlighted & no mod keys
4151 m_lineSelectSingleOnUp
= current
;
4152 ChangeCurrentRow(current
); // change focus
4155 else // multi sel & either ctrl or shift is down
4157 if (cmdModifierDown
)
4159 ChangeCurrentRow(current
);
4160 ReverseRowSelection(m_currentRow
);
4161 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4163 else if (event
.ShiftDown())
4165 ChangeCurrentRow(current
);
4167 unsigned int lineFrom
= oldCurrentRow
,
4170 if ( lineTo
< lineFrom
)
4173 lineFrom
= m_currentRow
;
4176 SelectRows(lineFrom
, lineTo
, true);
4177 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4179 else // !ctrl, !shift
4181 // test in the enclosing if should make it impossible
4182 wxFAIL_MSG( wxT("how did we get here?") );
4186 if (m_currentRow
!= oldCurrentRow
)
4187 RefreshRow( oldCurrentRow
);
4189 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4191 // Update selection here...
4193 m_currentColSetByKeyboard
= false;
4195 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4196 (current
== oldCurrentRow
)) && oldWasSelected
;
4198 // Call ActivateCell() after everything else as under GTK+
4199 if ( IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_ACTIVATABLE
) )
4201 // notify cell about click
4202 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4204 wxRect
cell_rect( xpos
+ itemOffset
,
4205 GetLineStart( current
),
4206 col
->GetWidth() - itemOffset
,
4207 GetLineHeight( current
) );
4209 // Report position relative to the cell's custom area, i.e.
4210 // no the entire space as given by the control but the one
4211 // used by the renderer after calculation of alignment etc.
4213 // adjust the rectangle ourselves to account for the alignment
4214 wxRect rectItem
= cell_rect
;
4215 const int align
= cell
->GetAlignment();
4216 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4218 const wxSize size
= cell
->GetSize();
4220 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4222 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4223 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4224 else if ( align
& wxALIGN_RIGHT
)
4225 rectItem
.x
+= cell_rect
.width
- size
.x
;
4226 // else: wxALIGN_LEFT is the default
4229 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4231 if ( align
& wxALIGN_CENTER_VERTICAL
)
4232 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4233 else if ( align
& wxALIGN_BOTTOM
)
4234 rectItem
.y
+= cell_rect
.height
- size
.y
;
4235 // else: wxALIGN_TOP is the default
4239 wxMouseEvent
event2(event
);
4240 event2
.m_x
-= rectItem
.x
;
4241 event2
.m_y
-= rectItem
.y
;
4242 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4244 /* ignore ret */ cell
->WXActivateCell
4249 col
->GetModelColumn(),
4256 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4260 if (HasCurrentRow())
4266 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4270 if (HasCurrentRow())
4276 void wxDataViewMainWindow::OnColumnsCountChanged()
4278 int editableCount
= 0;
4280 const unsigned cols
= GetOwner()->GetColumnCount();
4281 for ( unsigned i
= 0; i
< cols
; i
++ )
4283 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4284 if ( c
->IsHidden() )
4286 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4290 m_useCellFocus
= (editableCount
> 1);
4295 //-----------------------------------------------------------------------------
4297 //-----------------------------------------------------------------------------
4299 WX_DEFINE_LIST(wxDataViewColumnList
)
4301 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4302 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4303 EVT_SIZE(wxDataViewCtrl::OnSize
)
4306 wxDataViewCtrl::~wxDataViewCtrl()
4309 GetModel()->RemoveNotifier( m_notifier
);
4312 m_colsBestWidths
.clear();
4315 void wxDataViewCtrl::Init()
4317 m_cols
.DeleteContents(true);
4320 // No sorting column at start
4321 m_sortingColumnIdx
= wxNOT_FOUND
;
4323 m_headerArea
= NULL
;
4325 m_colsDirty
= false;
4328 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4333 const wxValidator
& validator
,
4334 const wxString
& name
)
4336 // if ( (style & wxBORDER_MASK) == 0)
4337 // style |= wxBORDER_SUNKEN;
4341 if (!wxControl::Create( parent
, id
, pos
, size
,
4342 style
| wxScrolledWindowStyle
, validator
, name
))
4345 SetInitialSize(size
);
4348 MacSetClipChildren( true );
4351 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4353 // We use the cursor keys for moving the selection, not scrolling, so call
4354 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4355 // keyboard events forwarded to us from wxListMainWindow.
4356 DisableKeyboardScrolling();
4358 if (HasFlag(wxDV_NO_HEADER
))
4359 m_headerArea
= NULL
;
4361 m_headerArea
= new wxDataViewHeaderWindow(this);
4363 SetTargetWindow( m_clientArea
);
4365 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4367 sizer
->Add( m_headerArea
, 0, wxGROW
);
4368 sizer
->Add( m_clientArea
, 1, wxGROW
);
4374 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4376 return wxBORDER_THEME
;
4380 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4384 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4387 // we need to process arrows ourselves for scrolling
4388 if ( nMsg
== WM_GETDLGCODE
)
4390 rc
|= DLGC_WANTARROWS
;
4398 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4400 wxSize newsize
= size
;
4401 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4402 newsize
.y
-= m_headerArea
->GetSize().y
;
4407 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4409 // We need to override OnSize so that our scrolled
4410 // window a) does call Layout() to use sizers for
4411 // positioning the controls but b) does not query
4412 // the sizer for their size and use that for setting
4413 // the scrollable area as set that ourselves by
4414 // calling SetScrollbar() further down.
4420 // We must redraw the headers if their height changed. Normally this
4421 // shouldn't happen as the control shouldn't let itself be resized beneath
4422 // its minimal height but avoid the display artefacts that appear if it
4423 // does happen, e.g. because there is really not enough vertical space.
4424 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4425 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4427 m_headerArea
->Refresh();
4431 void wxDataViewCtrl::SetFocus()
4434 m_clientArea
->SetFocus();
4437 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4439 if (!wxDataViewCtrlBase::AssociateModel( model
))
4442 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4444 model
->AddNotifier( m_notifier
);
4446 m_clientArea
->DestroyTree();
4448 m_clientArea
->BuildTree(model
);
4450 m_clientArea
->UpdateDisplay();
4455 #if wxUSE_DRAG_AND_DROP
4457 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4459 return m_clientArea
->EnableDragSource( format
);
4462 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4464 return m_clientArea
->EnableDropTarget( format
);
4467 #endif // wxUSE_DRAG_AND_DROP
4469 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4471 if (!wxDataViewCtrlBase::AppendColumn(col
))
4474 m_cols
.Append( col
);
4475 m_colsBestWidths
.push_back(0);
4476 OnColumnsCountChanged();
4480 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4482 if (!wxDataViewCtrlBase::PrependColumn(col
))
4485 m_cols
.Insert( col
);
4486 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4487 OnColumnsCountChanged();
4491 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4493 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4496 m_cols
.Insert( pos
, col
);
4497 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4498 OnColumnsCountChanged();
4502 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4505 m_headerArea
->UpdateColumn(idx
);
4507 m_clientArea
->UpdateDisplay();
4510 void wxDataViewCtrl::OnColumnsCountChanged()
4513 m_headerArea
->SetColumnCount(GetColumnCount());
4515 m_clientArea
->OnColumnsCountChanged();
4518 void wxDataViewCtrl::DoSetExpanderColumn()
4520 m_clientArea
->UpdateDisplay();
4523 void wxDataViewCtrl::DoSetIndent()
4525 m_clientArea
->UpdateDisplay();
4528 unsigned int wxDataViewCtrl::GetColumnCount() const
4530 return m_cols
.GetCount();
4533 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4535 if ( !m_clientArea
)
4538 m_clientArea
->SetRowHeight(lineHeight
);
4543 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4548 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4550 // columns can't be reordered if there is no header window which allows
4552 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4555 return GetColumn(idx
);
4558 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4560 const unsigned count
= m_cols
.size();
4561 for ( unsigned n
= 0; n
< count
; n
++ )
4563 if ( m_cols
[n
] == column
)
4570 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4572 if ( m_colsBestWidths
[idx
] != 0 )
4573 return m_colsBestWidths
[idx
];
4575 const int count
= m_clientArea
->GetRowCount();
4576 wxDataViewColumn
*column
= GetColumn(idx
);
4577 wxDataViewRenderer
*renderer
=
4578 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4580 class MaxWidthCalculator
4583 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4584 wxDataViewMainWindow
*clientArea
,
4585 wxDataViewRenderer
*renderer
,
4586 const wxDataViewModel
*model
,
4591 m_clientArea(clientArea
),
4592 m_renderer(renderer
),
4595 m_expanderSize(expanderSize
)
4599 !clientArea
->IsList() &&
4601 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4604 void UpdateWithWidth(int width
)
4606 m_width
= wxMax(m_width
, width
);
4609 void UpdateWithRow(int row
)
4612 wxDataViewItem item
;
4614 if ( m_isExpanderCol
)
4616 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4617 item
= node
->GetItem();
4618 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4622 item
= m_clientArea
->GetItemByRow(row
);
4625 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4626 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4629 int GetMaxWidth() const { return m_width
; }
4633 const wxDataViewCtrl
*m_dvc
;
4634 wxDataViewMainWindow
*m_clientArea
;
4635 wxDataViewRenderer
*m_renderer
;
4636 const wxDataViewModel
*m_model
;
4638 bool m_isExpanderCol
;
4642 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4643 GetModel(), column
->GetModelColumn(),
4644 m_clientArea
->GetRowHeight());
4648 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4649 // Labels on native MSW header are indented on both sides
4651 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4652 calculator
.UpdateWithWidth(header_width
);
4655 // The code below deserves some explanation. For very large controls, we
4656 // simply can't afford to calculate sizes for all items, it takes too
4657 // long. So the best we can do is to check the first and the last N/2
4658 // items in the control for some sufficiently large N and calculate best
4659 // sizes from that. That can result in the calculated best width being too
4660 // small for some outliers, but it's better to get slightly imperfect
4661 // result than to wait several seconds after every update. To avoid highly
4662 // visible miscalculations, we also include all currently visible items
4663 // no matter what. Finally, the value of N is determined dynamically by
4664 // measuring how much time we spent on the determining item widths so far.
4667 int top_part_end
= count
;
4668 static const long CALC_TIMEOUT
= 20/*ms*/;
4669 // don't call wxStopWatch::Time() too often
4670 static const unsigned CALC_CHECK_FREQ
= 100;
4673 // use some hard-coded limit, that's the best we can do without timer
4674 int top_part_end
= wxMin(500, count
);
4675 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4679 for ( row
= 0; row
< top_part_end
; row
++ )
4682 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4683 timer
.Time() > CALC_TIMEOUT
)
4685 #endif // wxUSE_STOPWATCH
4686 calculator
.UpdateWithRow(row
);
4689 // row is the first unmeasured item now; that's our value of N/2
4695 // add bottom N/2 items now:
4696 const int bottom_part_start
= wxMax(row
, count
- row
);
4697 for ( row
= bottom_part_start
; row
< count
; row
++ )
4699 calculator
.UpdateWithRow(row
);
4702 // finally, include currently visible items in the calculation:
4703 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4704 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4705 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4707 first_visible
= wxMax(first_visible
, top_part_end
);
4708 last_visible
= wxMin(bottom_part_start
, last_visible
);
4710 for ( row
= first_visible
; row
< last_visible
; row
++ )
4712 calculator
.UpdateWithRow(row
);
4715 wxLogTrace("dataview",
4716 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4718 count
- bottom_part_start
,
4719 wxMax(0, last_visible
- first_visible
),
4723 int max_width
= calculator
.GetMaxWidth();
4724 if ( max_width
> 0 )
4725 max_width
+= 2 * PADDING_RIGHTLEFT
;
4727 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4731 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4732 unsigned int WXUNUSED(new_pos
))
4734 // do _not_ reorder m_cols elements here, they should always be in the
4735 // order in which columns were added, we only display the columns in
4737 m_clientArea
->UpdateDisplay();
4740 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4742 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4746 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4749 if ( m_clientArea
->GetCurrentColumn() == column
)
4750 m_clientArea
->ClearCurrentColumn();
4752 OnColumnsCountChanged();
4757 bool wxDataViewCtrl::ClearColumns()
4759 SetExpanderColumn(NULL
);
4761 m_colsBestWidths
.clear();
4763 m_clientArea
->ClearCurrentColumn();
4765 OnColumnsCountChanged();
4770 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4772 m_colsBestWidths
[idx
] = 0;
4776 void wxDataViewCtrl::InvalidateColBestWidths()
4778 m_colsBestWidths
.clear();
4779 m_colsBestWidths
.resize(m_cols
.size());
4783 void wxDataViewCtrl::UpdateColWidths()
4785 if ( !m_headerArea
)
4788 const unsigned len
= m_colsBestWidths
.size();
4789 for ( unsigned i
= 0; i
< len
; i
++ )
4791 if ( m_colsBestWidths
[i
] == 0 )
4792 m_headerArea
->UpdateColumn(i
);
4796 void wxDataViewCtrl::OnInternalIdle()
4798 wxDataViewCtrlBase::OnInternalIdle();
4802 m_colsDirty
= false;
4807 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4810 unsigned int len
= GetColumnCount();
4811 for ( unsigned int i
= 0; i
< len
; i
++ )
4813 wxDataViewColumn
* col
= GetColumnAt(i
);
4820 // This returns the position in pixels which is not what we want.
4823 unsigned int len
= GetColumnCount();
4824 for ( unsigned int i
= 0; i
< len
; i
++ )
4826 wxDataViewColumn
* col
= GetColumnAt(i
);
4827 if (col
->IsHidden())
4829 ret
+= col
->GetWidth();
4832 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4840 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4842 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4843 : GetColumn(m_sortingColumnIdx
);
4846 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4848 return GetItemByRow(m_clientArea
->GetCurrentRow());
4851 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4853 const int row
= m_clientArea
->GetRowByItem(item
);
4855 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4856 if ( static_cast<unsigned>(row
) != oldCurrent
)
4858 m_clientArea
->ChangeCurrentRow(row
);
4859 m_clientArea
->RefreshRow(oldCurrent
);
4860 m_clientArea
->RefreshRow(row
);
4864 int wxDataViewCtrl::GetSelectedItemsCount() const
4866 return m_clientArea
->GetSelections().size();
4869 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4872 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4874 const size_t len
= selections
.size();
4875 for ( size_t i
= 0; i
< len
; i
++ )
4877 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4884 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4891 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4893 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4895 wxDataViewItem last_parent
;
4897 int len
= sel
.GetCount();
4898 for( int i
= 0; i
< len
; i
++ )
4900 wxDataViewItem item
= sel
[i
];
4901 wxDataViewItem parent
= GetModel()->GetParent( item
);
4904 if (parent
!= last_parent
)
4905 ExpandAncestors(item
);
4908 last_parent
= parent
;
4909 int row
= m_clientArea
->GetRowByItem( item
);
4911 selection
.Add( static_cast<unsigned int>(row
) );
4914 m_clientArea
->SetSelections( selection
);
4917 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4919 ExpandAncestors( item
);
4921 int row
= m_clientArea
->GetRowByItem( item
);
4924 // Unselect all rows before select another in the single select mode
4925 if (m_clientArea
->IsSingleSel())
4926 m_clientArea
->SelectAllRows(false);
4928 m_clientArea
->SelectRow(row
, true);
4930 // Also set focus to the selected item
4931 m_clientArea
->ChangeCurrentRow( row
);
4935 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4937 int row
= m_clientArea
->GetRowByItem( item
);
4939 m_clientArea
->SelectRow(row
, false);
4942 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4944 int row
= m_clientArea
->GetRowByItem( item
);
4947 return m_clientArea
->IsRowSelected(row
);
4952 void wxDataViewCtrl::SelectAll()
4954 m_clientArea
->SelectAllRows(true);
4957 void wxDataViewCtrl::UnselectAll()
4959 m_clientArea
->SelectAllRows(false);
4962 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4966 if( row
> (int) m_clientArea
->GetRowCount() )
4967 row
= m_clientArea
->GetRowCount();
4969 int first
= m_clientArea
->GetFirstVisibleRow();
4970 int last
= m_clientArea
->GetLastVisibleRow();
4972 m_clientArea
->ScrollTo( row
, column
);
4973 else if( row
> last
)
4974 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4976 m_clientArea
->ScrollTo( first
, column
);
4979 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4981 ExpandAncestors( item
);
4983 m_clientArea
->RecalculateDisplay();
4985 int row
= m_clientArea
->GetRowByItem(item
);
4988 if( column
== NULL
)
4989 EnsureVisible(row
, -1);
4991 EnsureVisible( row
, GetColumnIndex(column
) );
4996 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4997 wxDataViewColumn
* &column
) const
4999 m_clientArea
->HitTest(point
, item
, column
);
5002 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
5003 const wxDataViewColumn
* column
) const
5005 return m_clientArea
->GetItemRect(item
, column
);
5008 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
5010 return m_clientArea
->GetItemByRow( row
);
5013 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
5015 return m_clientArea
->GetRowByItem( item
);
5018 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
5020 ExpandAncestors( item
);
5022 int row
= m_clientArea
->GetRowByItem( item
);
5024 m_clientArea
->Expand(row
);
5027 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
5029 int row
= m_clientArea
->GetRowByItem( item
);
5031 m_clientArea
->Collapse(row
);
5034 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
5036 int row
= m_clientArea
->GetRowByItem( item
);
5038 return m_clientArea
->IsExpanded(row
);
5042 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
5044 wxDataViewColumn
* col
= GetColumn( column
);
5048 m_clientArea
->StartEditing(item
, col
);
5051 #endif // !wxUSE_GENERICDATAVIEWCTRL
5053 #endif // wxUSE_DATAVIEWCTRL