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 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
627 bool IsEmpty() { return GetRowCount() == 0; }
629 int GetCountPerPage() const;
630 int GetEndOfLastCol() const;
631 unsigned int GetFirstVisibleRow() const;
633 // I change this method to un const because in the tree view,
634 // the displaying number of the tree are changing along with the
635 // expanding/collapsing of the tree nodes
636 unsigned int GetLastVisibleRow();
637 unsigned int GetRowCount();
639 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
640 void SetSelections( const wxDataViewSelection
& sel
)
641 { m_selection
= sel
; UpdateDisplay(); }
642 void Select( const wxArrayInt
& aSelections
);
643 void SelectAllRows( bool on
);
644 void SelectRow( unsigned int row
, bool on
);
645 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
646 void ReverseRowSelection( unsigned int row
);
647 bool IsRowSelected( unsigned int row
);
648 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
650 void RefreshRow( unsigned int row
);
651 void RefreshRows( unsigned int from
, unsigned int to
);
652 void RefreshRowsAfter( unsigned int firstRow
);
654 // returns the colour to be used for drawing the rules
655 wxColour
GetRuleColour() const
657 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
660 wxRect
GetLineRect( unsigned int row
) const;
662 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
663 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
664 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
666 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
667 int GetRowHeight() const { return m_lineHeight
; }
669 // Some useful functions for row and item mapping
670 wxDataViewItem
GetItemByRow( unsigned int row
) const;
671 int GetRowByItem( const wxDataViewItem
& item
) const;
673 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
674 // We did not need this temporarily
675 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
677 // Methods for building the mapping tree
678 void BuildTree( wxDataViewModel
* model
);
680 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
681 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
683 void Expand( unsigned int row
);
684 void Collapse( unsigned int row
);
685 bool IsExpanded( unsigned int row
) const;
686 bool HasChildren( unsigned int row
) const;
688 #if wxUSE_DRAG_AND_DROP
689 bool EnableDragSource( const wxDataFormat
&format
);
690 bool EnableDropTarget( const wxDataFormat
&format
);
692 void RemoveDropHint();
693 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
694 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
695 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
697 #endif // wxUSE_DRAG_AND_DROP
699 void OnColumnsCountChanged();
701 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
702 // specified item in the given column.
703 void StartEditing(const wxDataViewItem
& item
, const wxDataViewColumn
* col
);
706 int RecalculateCount();
708 // Return false only if the event was vetoed by its handler.
709 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
711 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
713 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
716 wxDataViewCtrl
*m_owner
;
720 wxDataViewColumn
*m_currentCol
;
721 unsigned int m_currentRow
;
722 wxDataViewSelection m_selection
;
724 wxDataViewRenameTimer
*m_renameTimer
;
729 bool m_currentColSetByKeyboard
;
731 #if wxUSE_DRAG_AND_DROP
736 wxDataFormat m_dragFormat
;
739 wxDataFormat m_dropFormat
;
741 unsigned int m_dropHintLine
;
742 #endif // wxUSE_DRAG_AND_DROP
744 // for double click logic
745 unsigned int m_lineLastClicked
,
746 m_lineBeforeLastClicked
,
747 m_lineSelectSingleOnUp
;
749 // the pen used to draw horiz/vertical rules
752 // the pen used to draw the expander and the lines
755 // This is the tree structure of the model
756 wxDataViewTreeNode
* m_root
;
759 // This is the tree node under the cursor
760 wxDataViewTreeNode
* m_underMouse
;
762 // The control used for editing or NULL.
763 wxWeakRef
<wxWindow
> m_editorCtrl
;
765 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
766 wxDataViewRenderer
* m_editorRenderer
;
769 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
770 DECLARE_EVENT_TABLE()
773 // ---------------------------------------------------------
774 // wxGenericDataViewModelNotifier
775 // ---------------------------------------------------------
777 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
780 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
781 { m_mainWindow
= mainWindow
; }
783 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
784 { return m_mainWindow
->ItemAdded( parent
, item
); }
785 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
786 { return m_mainWindow
->ItemDeleted( parent
, item
); }
787 virtual bool ItemChanged( const wxDataViewItem
& item
)
788 { return m_mainWindow
->ItemChanged(item
); }
789 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
790 { return m_mainWindow
->ValueChanged( item
, col
); }
791 virtual bool Cleared()
792 { return m_mainWindow
->Cleared(); }
793 virtual void Resort()
794 { m_mainWindow
->Resort(); }
796 wxDataViewMainWindow
*m_mainWindow
;
799 // ---------------------------------------------------------
800 // wxDataViewRenderer
801 // ---------------------------------------------------------
803 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
805 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
806 wxDataViewCellMode mode
,
808 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
812 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
816 wxDataViewRenderer::~wxDataViewRenderer()
821 wxDC
*wxDataViewRenderer::GetDC()
825 if (GetOwner() == NULL
)
827 if (GetOwner()->GetOwner() == NULL
)
829 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
835 void wxDataViewRenderer::SetAlignment( int align
)
840 int wxDataViewRenderer::GetAlignment() const
845 // ---------------------------------------------------------
846 // wxDataViewCustomRenderer
847 // ---------------------------------------------------------
849 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
851 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
852 wxDataViewCellMode mode
, int align
) :
853 wxDataViewRenderer( varianttype
, mode
, align
)
857 // ---------------------------------------------------------
858 // wxDataViewTextRenderer
859 // ---------------------------------------------------------
861 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
863 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
864 wxDataViewCellMode mode
, int align
) :
865 wxDataViewRenderer( varianttype
, mode
, align
)
869 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
871 m_text
= value
.GetString();
876 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
881 bool wxDataViewTextRenderer::HasEditorCtrl() const
886 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
887 wxRect labelRect
, const wxVariant
&value
)
889 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
890 wxPoint(labelRect
.x
,labelRect
.y
),
891 wxSize(labelRect
.width
,labelRect
.height
),
892 wxTE_PROCESS_ENTER
);
894 // select the text in the control an place the cursor at the end
895 ctrl
->SetInsertionPointEnd();
901 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
903 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
904 value
= text
->GetValue();
908 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
910 RenderText(m_text
, 0, rect
, dc
, state
);
914 wxSize
wxDataViewTextRenderer::GetSize() const
917 return GetTextExtent(m_text
);
919 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
922 // ---------------------------------------------------------
923 // wxDataViewBitmapRenderer
924 // ---------------------------------------------------------
926 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
928 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
929 wxDataViewCellMode mode
, int align
) :
930 wxDataViewRenderer( varianttype
, mode
, align
)
934 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
936 if (value
.GetType() == wxT("wxBitmap"))
938 if (value
.GetType() == wxT("wxIcon"))
944 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
949 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
952 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
953 else if (m_icon
.IsOk())
954 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
959 wxSize
wxDataViewBitmapRenderer::GetSize() const
962 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
963 else if (m_icon
.IsOk())
964 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
966 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
969 // ---------------------------------------------------------
970 // wxDataViewToggleRenderer
971 // ---------------------------------------------------------
973 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
975 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
976 wxDataViewCellMode mode
, int align
) :
977 wxDataViewRenderer( varianttype
, mode
, align
)
982 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
984 m_toggle
= value
.GetBool();
989 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
994 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
998 flags
|= wxCONTROL_CHECKED
;
999 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
1000 GetEnabled() == false)
1001 flags
|= wxCONTROL_DISABLED
;
1003 // check boxes we draw must always have the same, standard size (if it's
1004 // bigger than the cell size the checkbox will be truncated because the
1005 // caller had set the clipping rectangle to prevent us from drawing outside
1007 cell
.SetSize(GetSize());
1009 wxRendererNative::Get().DrawCheckBox(
1010 GetOwner()->GetOwner(),
1018 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1019 wxDataViewModel
*model
,
1020 const wxDataViewItem
& item
,
1022 const wxMouseEvent
*mouseEvent
)
1024 if ( !model
->IsEnabled(item
, col
) )
1029 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1030 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1034 model
->ChangeValue(!m_toggle
, item
, col
);
1038 wxSize
wxDataViewToggleRenderer::GetSize() const
1040 // the window parameter is not used by GetCheckBoxSize() so it's
1041 // safe to pass NULL
1042 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1045 // ---------------------------------------------------------
1046 // wxDataViewProgressRenderer
1047 // ---------------------------------------------------------
1049 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1051 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1052 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1053 wxDataViewRenderer( varianttype
, mode
, align
)
1059 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1061 m_value
= (long) value
;
1063 if (m_value
< 0) m_value
= 0;
1064 if (m_value
> 100) m_value
= 100;
1069 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1071 value
= (long) m_value
;
1076 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1078 // deflate the rect to leave a small border between bars in adjacent rows
1079 wxRect bar
= rect
.Deflate(0, 1);
1081 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1082 dc
->SetPen( *wxBLACK_PEN
);
1083 dc
->DrawRectangle( bar
);
1085 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1086 dc
->SetPen( *wxTRANSPARENT_PEN
);
1088 const wxDataViewItemAttr
& attr
= GetAttr();
1089 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1091 dc
->DrawRectangle( bar
);
1096 wxSize
wxDataViewProgressRenderer::GetSize() const
1098 return wxSize(40,12);
1101 // ---------------------------------------------------------
1102 // wxDataViewIconTextRenderer
1103 // ---------------------------------------------------------
1105 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1107 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1108 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1109 wxDataViewRenderer( varianttype
, mode
, align
)
1112 SetAlignment(align
);
1115 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1121 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1126 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1130 const wxIcon
& icon
= m_value
.GetIcon();
1133 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1134 xoffset
= icon
.GetWidth()+4;
1137 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1142 wxSize
wxDataViewIconTextRenderer::GetSize() const
1144 if (!m_value
.GetText().empty())
1146 wxSize size
= GetTextExtent(m_value
.GetText());
1148 if (m_value
.GetIcon().IsOk())
1149 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1152 return wxSize(80,20);
1155 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1157 wxDataViewIconText iconText
;
1160 wxString text
= iconText
.GetText();
1162 // adjust the label rect to take the width of the icon into account
1163 if (iconText
.GetIcon().IsOk())
1165 int w
= iconText
.GetIcon().GetWidth() + 4;
1167 labelRect
.width
-= w
;
1170 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1171 wxPoint(labelRect
.x
,labelRect
.y
),
1172 wxSize(labelRect
.width
,labelRect
.height
),
1173 wxTE_PROCESS_ENTER
);
1175 // select the text in the control an place the cursor at the end
1176 ctrl
->SetInsertionPointEnd();
1182 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1184 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1186 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1191 //-----------------------------------------------------------------------------
1192 // wxDataViewDropTarget
1193 //-----------------------------------------------------------------------------
1195 #if wxUSE_DRAG_AND_DROP
1197 class wxBitmapCanvas
: public wxWindow
1200 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1201 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1204 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1207 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1210 dc
.DrawBitmap( m_bitmap
, 0, 0);
1216 class wxDataViewDropSource
: public wxDropSource
1219 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1227 ~wxDataViewDropSource()
1232 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1234 wxPoint pos
= wxGetMousePosition();
1238 int liney
= m_win
->GetLineStart( m_row
);
1240 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1241 m_win
->ClientToScreen( &linex
, &liney
);
1242 m_dist_x
= pos
.x
- linex
;
1243 m_dist_y
= pos
.y
- liney
;
1246 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1248 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1249 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1251 wxFRAME_TOOL_WINDOW
|
1252 wxFRAME_FLOAT_ON_PARENT
|
1253 wxFRAME_NO_TASKBAR
|
1255 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1260 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1261 m_hint
->SetTransparent( 128 );
1267 wxDataViewMainWindow
*m_win
;
1270 int m_dist_x
,m_dist_y
;
1274 class wxDataViewDropTarget
: public wxDropTarget
1277 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1283 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1285 wxDataFormat format
= GetMatchingPair();
1286 if (format
== wxDF_INVALID
)
1288 return m_win
->OnDragOver( format
, x
, y
, def
);
1291 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1293 wxDataFormat format
= GetMatchingPair();
1294 if (format
== wxDF_INVALID
)
1296 return m_win
->OnDrop( format
, x
, y
);
1299 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1301 wxDataFormat format
= GetMatchingPair();
1302 if (format
== wxDF_INVALID
)
1306 return m_win
->OnData( format
, x
, y
, def
);
1309 virtual void OnLeave()
1310 { m_win
->OnLeave(); }
1312 wxDataViewMainWindow
*m_win
;
1315 #endif // wxUSE_DRAG_AND_DROP
1317 //-----------------------------------------------------------------------------
1318 // wxDataViewRenameTimer
1319 //-----------------------------------------------------------------------------
1321 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1326 void wxDataViewRenameTimer::Notify()
1328 m_owner
->OnRenameTimer();
1331 //-----------------------------------------------------------------------------
1332 // wxDataViewMainWindow
1333 //-----------------------------------------------------------------------------
1335 // The tree building helper, declared firstly
1336 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1337 wxDataViewTreeNode
* node
);
1339 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1341 if (row1
> row2
) return 1;
1342 if (row1
== row2
) return 0;
1346 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1348 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1349 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1350 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1351 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1352 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1353 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook
)
1354 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1357 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1358 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1359 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1360 m_selection( wxDataViewSelectionCmp
)
1365 m_editorRenderer
= NULL
;
1367 m_lastOnSame
= false;
1368 m_renameTimer
= new wxDataViewRenameTimer( this );
1370 // TODO: user better initial values/nothing selected
1371 m_currentCol
= NULL
;
1372 m_currentColSetByKeyboard
= false;
1373 m_useCellFocus
= false;
1376 m_lineHeight
= wxMax( 17, GetCharHeight() + 4 ); // 17 = mini icon height + 1
1378 #if wxUSE_DRAG_AND_DROP
1380 m_dragStart
= wxPoint(0,0);
1382 m_dragEnabled
= false;
1383 m_dropEnabled
= false;
1385 m_dropHintLine
= (unsigned int) -1;
1386 #endif // wxUSE_DRAG_AND_DROP
1388 m_lineLastClicked
= (unsigned int) -1;
1389 m_lineBeforeLastClicked
= (unsigned int) -1;
1390 m_lineSelectSingleOnUp
= (unsigned int) -1;
1394 SetBackgroundColour( *wxWHITE
);
1396 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1398 m_penRule
= wxPen(GetRuleColour());
1400 // compose a pen whichcan draw black lines
1401 // TODO: maybe there is something system colour to use
1402 m_penExpander
= wxPen(wxColour(0,0,0));
1404 m_root
= wxDataViewTreeNode::CreateRootNode();
1406 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1408 m_underMouse
= NULL
;
1412 wxDataViewMainWindow::~wxDataViewMainWindow()
1415 delete m_renameTimer
;
1419 #if wxUSE_DRAG_AND_DROP
1420 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1422 m_dragFormat
= format
;
1423 m_dragEnabled
= format
!= wxDF_INVALID
;
1428 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1430 m_dropFormat
= format
;
1431 m_dropEnabled
= format
!= wxDF_INVALID
;
1434 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1439 void wxDataViewMainWindow::RemoveDropHint()
1444 RefreshRow( m_dropHintLine
);
1445 m_dropHintLine
= (unsigned int) -1;
1449 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1450 wxCoord y
, wxDragResult def
)
1454 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1455 unsigned int row
= GetLineAt( yy
);
1457 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1463 wxDataViewItem item
= GetItemByRow( row
);
1465 wxDataViewModel
*model
= GetModel();
1467 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1468 event
.SetEventObject( m_owner
);
1469 event
.SetItem( item
);
1470 event
.SetModel( model
);
1471 event
.SetDataFormat( format
);
1472 if (!m_owner
->HandleWindowEvent( event
))
1478 if (!event
.IsAllowed())
1485 if (m_dropHint
&& (row
!= m_dropHintLine
))
1486 RefreshRow( m_dropHintLine
);
1488 m_dropHintLine
= row
;
1494 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1500 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1501 unsigned int row
= GetLineAt( yy
);
1503 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1506 wxDataViewItem item
= GetItemByRow( row
);
1508 wxDataViewModel
*model
= GetModel();
1510 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1511 event
.SetEventObject( m_owner
);
1512 event
.SetItem( item
);
1513 event
.SetModel( model
);
1514 event
.SetDataFormat( format
);
1515 if (!m_owner
->HandleWindowEvent( event
))
1518 if (!event
.IsAllowed())
1524 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1529 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1530 unsigned int row
= GetLineAt( yy
);
1532 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1535 wxDataViewItem item
= GetItemByRow( row
);
1537 wxDataViewModel
*model
= GetModel();
1539 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1541 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1542 event
.SetEventObject( m_owner
);
1543 event
.SetItem( item
);
1544 event
.SetModel( model
);
1545 event
.SetDataFormat( format
);
1546 event
.SetDataSize( obj
->GetSize() );
1547 event
.SetDataBuffer( obj
->GetData() );
1548 if (!m_owner
->HandleWindowEvent( event
))
1551 if (!event
.IsAllowed())
1557 void wxDataViewMainWindow::OnLeave()
1562 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1564 int height
= GetLineHeight( row
);
1566 unsigned int cols
= GetOwner()->GetColumnCount();
1568 for (col
= 0; col
< cols
; col
++)
1570 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1571 if (column
->IsHidden())
1572 continue; // skip it!
1573 width
+= column
->GetWidth();
1579 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1580 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1581 indent
= indent
+ m_lineHeight
;
1582 // try to use the m_lineHeight as the expander space
1586 wxBitmap
bitmap( width
, height
);
1587 wxMemoryDC
dc( bitmap
);
1588 dc
.SetFont( GetFont() );
1589 dc
.SetPen( *wxBLACK_PEN
);
1590 dc
.SetBrush( *wxWHITE_BRUSH
);
1591 dc
.DrawRectangle( 0,0,width
,height
);
1593 wxDataViewModel
*model
= m_owner
->GetModel();
1595 wxDataViewColumn
* const
1596 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1599 for (col
= 0; col
< cols
; col
++)
1601 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1602 wxDataViewRenderer
*cell
= column
->GetRenderer();
1604 if (column
->IsHidden())
1605 continue; // skip it!
1607 width
= column
->GetWidth();
1609 if (column
== expander
)
1612 wxDataViewItem item
= GetItemByRow( row
);
1613 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1615 wxRect
item_rect(x
, 0, width
, height
);
1616 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1618 // dc.SetClippingRegion( item_rect );
1619 cell
->WXCallRender(item_rect
, &dc
, 0);
1620 // dc.DestroyClippingRegion();
1628 #endif // wxUSE_DRAG_AND_DROP
1631 // Draw focus rect for individual cell. Unlike native focus rect, we render
1632 // this in foreground text color (typically white) to enhance contrast and
1634 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1636 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1638 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1639 // may be short traits and not really dots
1641 // note that to behave in the same manner as DrawRect(), we must exclude
1642 // the bottom and right borders from the rectangle
1643 wxCoord x1
= rect
.GetLeft(),
1645 x2
= rect
.GetRight(),
1646 y2
= rect
.GetBottom();
1648 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1651 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1652 dc
.DrawPoint(z
, rect
.GetTop());
1654 wxCoord shift
= z
== x2
? 0 : 1;
1655 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1656 dc
.DrawPoint(x2
, z
);
1658 shift
= z
== y2
? 0 : 1;
1659 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1660 dc
.DrawPoint(z
, y2
);
1662 shift
= z
== x1
? 0 : 1;
1663 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1664 dc
.DrawPoint(x1
, z
);
1668 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1670 wxDataViewModel
*model
= GetModel();
1671 wxAutoBufferedPaintDC
dc( this );
1674 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1675 dc
.SetPen( *wxTRANSPARENT_PEN
);
1676 dc
.DrawRectangle(GetClientSize());
1681 // No items to draw.
1686 GetOwner()->PrepareDC( dc
);
1687 dc
.SetFont( GetFont() );
1689 wxRect update
= GetUpdateRegion().GetBox();
1690 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1692 // compute which items needs to be redrawn
1693 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1694 unsigned int item_count
=
1695 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1696 (int)(GetRowCount( ) - item_start
));
1697 unsigned int item_last
= item_start
+ item_count
;
1699 // Send the event to wxDataViewCtrl itself.
1700 wxWindow
* const parent
= GetParent();
1701 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1702 cache_event
.SetEventObject(parent
);
1703 cache_event
.SetCache(item_start
, item_last
- 1);
1704 parent
->ProcessWindowEvent(cache_event
);
1706 // compute which columns needs to be redrawn
1707 unsigned int cols
= GetOwner()->GetColumnCount();
1710 // we assume that we have at least one column below and painting an
1711 // empty control is unnecessary anyhow
1715 unsigned int col_start
= 0;
1716 unsigned int x_start
;
1717 for (x_start
= 0; col_start
< cols
; col_start
++)
1719 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1720 if (col
->IsHidden())
1721 continue; // skip it!
1723 unsigned int w
= col
->GetWidth();
1724 if (x_start
+w
>= (unsigned int)update
.x
)
1730 unsigned int col_last
= col_start
;
1731 unsigned int x_last
= x_start
;
1732 for (; col_last
< cols
; col_last
++)
1734 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1735 if (col
->IsHidden())
1736 continue; // skip it!
1738 if (x_last
> (unsigned int)update
.GetRight())
1741 x_last
+= col
->GetWidth();
1744 // Draw horizontal rules if required
1745 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1747 dc
.SetPen(m_penRule
);
1748 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1750 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1752 int y
= GetLineStart( i
);
1753 dc
.DrawLine(x_start
, y
, x_last
, y
);
1757 // Draw vertical rules if required
1758 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1760 dc
.SetPen(m_penRule
);
1761 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1763 // NB: Vertical rules are drawn in the last pixel of a column so that
1764 // they align perfectly with native MSW wxHeaderCtrl as well as for
1765 // consistency with MSW native list control. There's no vertical
1766 // rule at the most-left side of the control.
1768 int x
= x_start
- 1;
1769 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1771 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1772 if (col
->IsHidden())
1773 continue; // skip it
1775 x
+= col
->GetWidth();
1777 dc
.DrawLine(x
, GetLineStart( item_start
),
1778 x
, GetLineStart( item_last
) );
1782 // redraw the background for the items which are selected/current
1783 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1785 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1787 if (selected
|| item
== m_currentRow
)
1789 wxRect
rect( x_start
, GetLineStart( item
),
1790 x_last
- x_start
, GetLineHeight( item
) );
1792 // draw selection and whole-item focus:
1795 int flags
= wxCONTROL_SELECTED
;
1797 flags
|= wxCONTROL_FOCUSED
;
1799 wxRendererNative::Get().DrawItemSelectionRect
1808 // draw keyboard focus rect if applicable
1809 if ( item
== m_currentRow
&& m_hasFocus
)
1811 bool renderColumnFocus
= false;
1813 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1815 renderColumnFocus
= true;
1817 // If this is container node without columns, render full-row focus:
1820 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1821 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1822 renderColumnFocus
= false;
1826 if ( renderColumnFocus
)
1828 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1830 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1831 if ( col
->IsHidden() )
1834 rect
.width
= col
->GetWidth();
1836 if ( col
== m_currentCol
)
1838 // make the rect more visible by adding a small
1839 // margin around it:
1844 // DrawFocusRect() uses XOR and is all but
1845 // invisible against dark-blue background. Use
1846 // the same color used for selected text.
1847 DrawSelectedCellFocusRect(dc
, rect
);
1851 wxRendererNative::Get().DrawFocusRect
1862 rect
.x
+= rect
.width
;
1867 // render focus rectangle for the whole row
1868 wxRendererNative::Get().DrawFocusRect
1873 selected
? (int)wxCONTROL_SELECTED
: 0
1880 #if wxUSE_DRAG_AND_DROP
1883 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1884 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1885 dc
.SetPen( *wxBLACK_PEN
);
1886 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1887 dc
.DrawRectangle( rect
);
1889 #endif // wxUSE_DRAG_AND_DROP
1891 wxDataViewColumn
* const
1892 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1894 // redraw all cells for all rows which must be repainted and all columns
1896 cell_rect
.x
= x_start
;
1897 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1899 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1900 wxDataViewRenderer
*cell
= col
->GetRenderer();
1901 cell_rect
.width
= col
->GetWidth();
1903 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1904 continue; // skip it!
1906 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1908 // get the cell value and set it into the renderer
1909 wxDataViewTreeNode
*node
= NULL
;
1910 wxDataViewItem dataitem
;
1912 if (!IsVirtualList())
1914 node
= GetTreeNodeByRow(item
);
1918 dataitem
= node
->GetItem();
1920 // Skip all columns of "container" rows except the expander
1921 // column itself unless HasContainerColumns() overrides this.
1922 if ( col
!= expander
&&
1923 model
->IsContainer(dataitem
) &&
1924 !model
->HasContainerColumns(dataitem
) )
1929 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1932 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1935 cell_rect
.y
= GetLineStart( item
);
1936 cell_rect
.height
= GetLineHeight( item
);
1938 // deal with the expander
1940 if ((!IsList()) && (col
== expander
))
1942 // Calculate the indent first
1943 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1945 // we reserve m_lineHeight of horizontal space for the expander
1946 // but leave EXPANDER_MARGIN around the expander itself
1947 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1949 indent
+= m_lineHeight
;
1951 // draw expander if needed and visible
1952 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1954 dc
.SetPen( m_penExpander
);
1955 dc
.SetBrush( wxNullBrush
);
1957 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1958 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1959 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1961 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1964 if ( m_underMouse
== node
)
1965 flag
|= wxCONTROL_CURRENT
;
1966 if ( node
->IsOpen() )
1967 flag
|= wxCONTROL_EXPANDED
;
1969 // ensure that we don't overflow the cell (which might
1970 // happen if the column is very narrow)
1971 wxDCClipper
clip(dc
, cell_rect
);
1973 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1976 // force the expander column to left-center align
1977 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1980 wxRect item_rect
= cell_rect
;
1981 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1983 // account for the tree indent (harmless if we're not indented)
1984 item_rect
.x
+= indent
;
1985 item_rect
.width
-= indent
;
1987 if ( item_rect
.width
<= 0 )
1991 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1992 state
|= wxDATAVIEW_CELL_SELECTED
;
1994 // TODO: it would be much more efficient to create a clipping
1995 // region for the entire column being rendered (in the OnPaint
1996 // of wxDataViewMainWindow) instead of a single clip region for
1997 // each cell. However it would mean that each renderer should
1998 // respect the given wxRect's top & bottom coords, eventually
1999 // violating only the left & right coords - however the user can
2000 // make its own renderer and thus we cannot be sure of that.
2001 wxDCClipper
clip(dc
, item_rect
);
2003 cell
->WXCallRender(item_rect
, &dc
, state
);
2006 cell_rect
.x
+= cell_rect
.width
;
2010 void wxDataViewMainWindow::OnRenameTimer()
2012 // We have to call this here because changes may just have
2013 // been made and no screen update taken place.
2016 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2017 // (needs to be tested!)
2021 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2023 StartEditing( item
, m_currentCol
);
2027 wxDataViewMainWindow::StartEditing(const wxDataViewItem
& item
,
2028 const wxDataViewColumn
* col
)
2030 wxDataViewRenderer
* renderer
= col
->GetRenderer();
2031 if (renderer
->GetMode() != wxDATAVIEW_CELL_EDITABLE
)
2034 const wxRect itemRect
= GetItemRect(item
, col
);
2035 if ( renderer
->StartEditing(item
, itemRect
) )
2037 // Save the renderer to be able to finish/cancel editing it later and
2038 // save the control to be able to detect if we're still editing it.
2039 m_editorRenderer
= renderer
;
2040 m_editorCtrl
= renderer
->GetEditorCtrl();
2044 //-----------------------------------------------------------------------------
2045 // Helper class for do operation on the tree node
2046 //-----------------------------------------------------------------------------
2051 virtual ~DoJob() { }
2053 // The return value control how the tree-walker tranverse the tree
2056 DONE
, // Job done, stop traversing and return
2057 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2058 CONTINUE
// Job not done, continue
2061 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2064 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2066 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2068 switch( func( node
) )
2072 case DoJob::SKIP_SUBTREE
:
2074 case DoJob::CONTINUE
:
2078 if ( node
->HasChildren() )
2080 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2082 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2086 if ( Walker(*i
, func
) )
2094 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2096 if (IsVirtualList())
2098 wxDataViewVirtualListModel
*list_model
=
2099 (wxDataViewVirtualListModel
*) GetModel();
2100 m_count
= list_model
->GetCount();
2106 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2111 wxDataViewItemArray modelSiblings
;
2112 GetModel()->GetChildren(parent
, modelSiblings
);
2113 const int modelSiblingsSize
= modelSiblings
.size();
2115 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2116 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2118 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2119 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2121 parentNode
->SetHasChildren(true);
2123 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2124 const int nodeSiblingsSize
= nodeSiblings
.size();
2128 if ( posInModel
== modelSiblingsSize
- 1 )
2130 nodePos
= nodeSiblingsSize
;
2132 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2134 // This is the simple case when our node tree already matches the
2135 // model and only this one item is missing.
2136 nodePos
= posInModel
;
2140 // It's possible that a larger discrepancy between the model and
2141 // our realization exists. This can happen e.g. when adding a bunch
2142 // of items to the model and then calling ItemsAdded() just once
2143 // afterwards. In this case, we must find the right position by
2144 // looking at sibling items.
2146 // append to the end if we won't find a better position:
2147 nodePos
= nodeSiblingsSize
;
2149 for ( int nextItemPos
= posInModel
+ 1;
2150 nextItemPos
< modelSiblingsSize
;
2153 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2154 if ( nextNodePos
!= wxNOT_FOUND
)
2156 nodePos
= nextNodePos
;
2162 parentNode
->ChangeSubTreeCount(+1);
2163 parentNode
->InsertChild(itemNode
, nodePos
);
2168 GetOwner()->InvalidateColBestWidths();
2174 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2175 const wxDataViewItem
& item
)
2177 if (IsVirtualList())
2179 wxDataViewVirtualListModel
*list_model
=
2180 (wxDataViewVirtualListModel
*) GetModel();
2181 m_count
= list_model
->GetCount();
2183 if ( !m_selection
.empty() )
2185 const int row
= GetRowByItem(item
);
2187 int rowIndexInSelection
= wxNOT_FOUND
;
2189 const size_t selCount
= m_selection
.size();
2190 for ( size_t i
= 0; i
< selCount
; i
++ )
2192 if ( m_selection
[i
] == (unsigned)row
)
2193 rowIndexInSelection
= i
;
2194 else if ( m_selection
[i
] > (unsigned)row
)
2198 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2199 m_selection
.RemoveAt(rowIndexInSelection
);
2203 else // general case
2205 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2207 // Notice that it is possible that the item being deleted is not in the
2208 // tree at all, for example we could be deleting a never shown (because
2209 // collapsed) item in a tree model. So it's not an error if we don't know
2210 // about this item, just return without doing anything then.
2214 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2215 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2217 // We can't use FindNode() to find 'item', because it was already
2218 // removed from the model by the time ItemDeleted() is called, so we
2219 // have to do it manually. We keep track of its position as well for
2221 int itemPosInNode
= 0;
2222 wxDataViewTreeNode
*itemNode
= NULL
;
2223 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2224 i
!= parentsChildren
.end();
2225 ++i
, ++itemPosInNode
)
2227 if( (*i
)->GetItem() == item
)
2234 // If the parent wasn't expanded, it's possible that we didn't have a
2235 // node corresponding to 'item' and so there's nothing left to do.
2238 // If this was the last child to be removed, it's possible the parent
2239 // node became a leaf. Let's ask the model about it.
2240 if ( parentNode
->GetChildNodes().empty() )
2241 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2246 // Delete the item from wxDataViewTreeNode representation:
2247 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2249 parentNode
->RemoveChild(itemNode
);
2251 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2253 // Make the row number invalid and get a new valid one when user call GetRowCount
2256 // If this was the last child to be removed, it's possible the parent
2257 // node became a leaf. Let's ask the model about it.
2258 if ( parentNode
->GetChildNodes().empty() )
2259 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2261 // Update selection by removing 'item' and its entire children tree from the selection.
2262 if ( !m_selection
.empty() )
2264 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2265 // the parent ('parentNode') and position in its list of children
2267 if ( itemPosInNode
== 0 )
2269 // 1st child, row number is that of the parent parentNode + 1
2270 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2274 // row number is that of the sibling above 'item' + its subtree if any + 1
2275 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2277 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2278 siblingNode
->GetSubTreeCount() +
2282 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2284 const size_t numSelections
= m_selection
.size();
2285 for ( size_t i
= 0; i
< numSelections
; ++i
)
2287 const int s
= m_selection
[i
];
2289 newsel
.push_back(s
);
2290 else if ( s
>= itemRow
+ itemsDeleted
)
2291 newsel
.push_back(s
- itemsDeleted
);
2292 // else: deleted item, remove from selection
2295 m_selection
= newsel
;
2299 // Change the current row to the last row if the current exceed the max row number
2300 if( m_currentRow
> GetRowCount() )
2301 ChangeCurrentRow(m_count
- 1);
2303 GetOwner()->InvalidateColBestWidths();
2309 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2314 GetOwner()->InvalidateColBestWidths();
2317 wxWindow
*parent
= GetParent();
2318 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2319 le
.SetEventObject(parent
);
2320 le
.SetModel(GetModel());
2322 parent
->ProcessWindowEvent(le
);
2327 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2329 int view_column
= -1;
2330 unsigned int n_col
= m_owner
->GetColumnCount();
2331 for (unsigned i
= 0; i
< n_col
; i
++)
2333 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2334 if (column
->GetModelColumn() == model_column
)
2336 view_column
= (int) i
;
2340 if (view_column
== -1)
2343 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2344 /*#define MAX_VIRTUAL_WIDTH 100000
2346 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2347 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2348 Refresh( true, &rect );
2355 GetOwner()->InvalidateColBestWidth(view_column
);
2358 wxWindow
*parent
= GetParent();
2359 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2360 le
.SetEventObject(parent
);
2361 le
.SetModel(GetModel());
2363 le
.SetColumn(view_column
);
2364 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2365 parent
->ProcessWindowEvent(le
);
2370 bool wxDataViewMainWindow::Cleared()
2373 m_selection
.Clear();
2376 BuildTree( GetModel() );
2378 GetOwner()->InvalidateColBestWidths();
2384 void wxDataViewMainWindow::UpdateDisplay()
2387 m_underMouse
= NULL
;
2390 void wxDataViewMainWindow::OnInternalIdle()
2392 wxWindow::OnInternalIdle();
2396 RecalculateDisplay();
2401 void wxDataViewMainWindow::RecalculateDisplay()
2403 wxDataViewModel
*model
= GetModel();
2410 int width
= GetEndOfLastCol();
2411 int height
= GetLineStart( GetRowCount() );
2413 SetVirtualSize( width
, height
);
2414 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2419 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2421 m_underMouse
= NULL
;
2423 wxWindow::ScrollWindow( dx
, dy
, rect
);
2425 if (GetOwner()->m_headerArea
)
2426 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2429 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2431 m_underMouse
= NULL
;
2434 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2435 int sy
= GetLineStart( rows
)/y
;
2439 wxRect rect
= GetClientRect();
2443 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2444 for (x_start
= 0; colnum
< column
; colnum
++)
2446 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2447 if (col
->IsHidden())
2448 continue; // skip it!
2450 w
= col
->GetWidth();
2454 int x_end
= x_start
+ w
;
2455 xe
= xx
+ rect
.width
;
2458 sx
= ( xx
+ x_end
- xe
)/x
;
2465 m_owner
->Scroll( sx
, sy
);
2468 int wxDataViewMainWindow::GetCountPerPage() const
2470 wxSize size
= GetClientSize();
2471 return size
.y
/ m_lineHeight
;
2474 int wxDataViewMainWindow::GetEndOfLastCol() const
2478 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2480 const wxDataViewColumn
*c
=
2481 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2484 width
+= c
->GetWidth();
2489 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2493 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2495 return GetLineAt( y
);
2498 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2500 wxSize client_size
= GetClientSize();
2501 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2502 &client_size
.x
, &client_size
.y
);
2504 // we should deal with the pixel here
2505 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2507 return wxMin( GetRowCount()-1, row
);
2510 unsigned int wxDataViewMainWindow::GetRowCount()
2512 if ( m_count
== -1 )
2514 m_count
= RecalculateCount();
2520 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2527 void wxDataViewMainWindow::SelectAllRows( bool on
)
2534 m_selection
.Clear();
2535 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2536 m_selection
.Add( i
);
2541 unsigned int first_visible
= GetFirstVisibleRow();
2542 unsigned int last_visible
= GetLastVisibleRow();
2544 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2546 unsigned int row
= m_selection
[i
];
2547 if ((row
>= first_visible
) && (row
<= last_visible
))
2550 m_selection
.Clear();
2554 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2556 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2560 m_selection
.Add( row
);
2568 m_selection
.Remove( row
);
2574 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2578 unsigned int tmp
= from
;
2584 for (i
= from
; i
<= to
; i
++)
2586 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2589 m_selection
.Add( i
);
2594 m_selection
.Remove( i
);
2597 RefreshRows( from
, to
);
2600 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2602 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2604 int n
= aSelections
[i
];
2606 m_selection
.Add( n
);
2611 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2613 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2614 m_selection
.Add( row
);
2616 m_selection
.Remove( row
);
2620 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2622 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2625 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2627 wxWindow
*parent
= GetParent();
2628 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2630 le
.SetEventObject(parent
);
2631 le
.SetModel(GetModel());
2634 parent
->ProcessWindowEvent(le
);
2637 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2639 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2640 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2642 wxSize client_size
= GetClientSize();
2643 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2644 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2645 if (intersect_rect
.width
> 0)
2646 Refresh( true, &intersect_rect
);
2649 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2653 unsigned int tmp
= to
;
2658 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2659 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2661 wxSize client_size
= GetClientSize();
2662 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2663 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2664 if (intersect_rect
.width
> 0)
2665 Refresh( true, &intersect_rect
);
2668 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2670 wxSize client_size
= GetClientSize();
2671 int start
= GetLineStart( firstRow
);
2672 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2673 if (start
> client_size
.y
) return;
2675 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2677 Refresh( true, &rect
);
2680 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2684 rect
.y
= GetLineStart( row
);
2685 rect
.width
= GetEndOfLastCol();
2686 rect
.height
= GetLineHeight( row
);
2691 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2693 const wxDataViewModel
*model
= GetModel();
2695 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2697 // TODO make more efficient
2702 for (r
= 0; r
< row
; r
++)
2704 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2705 if (!node
) return start
;
2707 wxDataViewItem item
= node
->GetItem();
2709 unsigned int cols
= GetOwner()->GetColumnCount();
2711 int height
= m_lineHeight
;
2712 for (col
= 0; col
< cols
; col
++)
2714 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2715 if (column
->IsHidden())
2716 continue; // skip it!
2719 model
->IsContainer(item
) &&
2720 !model
->HasContainerColumns(item
))
2721 continue; // skip it!
2723 wxDataViewRenderer
*renderer
=
2724 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2725 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2727 height
= wxMax( height
, renderer
->GetSize().y
);
2737 return row
* m_lineHeight
;
2741 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2743 const wxDataViewModel
*model
= GetModel();
2745 // check for the easy case first
2746 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2747 return y
/ m_lineHeight
;
2749 // TODO make more efficient
2750 unsigned int row
= 0;
2751 unsigned int yy
= 0;
2754 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2757 // not really correct...
2758 return row
+ ((y
-yy
) / m_lineHeight
);
2761 wxDataViewItem item
= node
->GetItem();
2763 unsigned int cols
= GetOwner()->GetColumnCount();
2765 int height
= m_lineHeight
;
2766 for (col
= 0; col
< cols
; col
++)
2768 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2769 if (column
->IsHidden())
2770 continue; // skip it!
2773 model
->IsContainer(item
) &&
2774 !model
->HasContainerColumns(item
))
2775 continue; // skip it!
2777 wxDataViewRenderer
*renderer
=
2778 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2779 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2781 height
= wxMax( height
, renderer
->GetSize().y
);
2792 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2794 const wxDataViewModel
*model
= GetModel();
2796 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2798 wxASSERT( !IsVirtualList() );
2800 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2801 // wxASSERT( node );
2802 if (!node
) return m_lineHeight
;
2804 wxDataViewItem item
= node
->GetItem();
2806 int height
= m_lineHeight
;
2808 unsigned int cols
= GetOwner()->GetColumnCount();
2810 for (col
= 0; col
< cols
; col
++)
2812 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2813 if (column
->IsHidden())
2814 continue; // skip it!
2817 model
->IsContainer(item
) &&
2818 !model
->HasContainerColumns(item
))
2819 continue; // skip it!
2821 wxDataViewRenderer
*renderer
=
2822 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2823 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2825 height
= wxMax( height
, renderer
->GetSize().y
);
2832 return m_lineHeight
;
2837 class RowToTreeNodeJob
: public DoJob
2840 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2843 this->current
= current
;
2848 virtual int operator() ( wxDataViewTreeNode
* node
)
2851 if( current
== static_cast<int>(row
))
2857 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2859 current
+= node
->GetSubTreeCount();
2860 return DoJob::SKIP_SUBTREE
;
2866 // If the current node has only leaf children, we can find the
2867 // desired node directly. This can speed up finding the node
2868 // in some cases, and will have a very good effect for list views.
2869 if ( node
->HasChildren() &&
2870 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2872 const int index
= static_cast<int>(row
) - current
- 1;
2873 ret
= node
->GetChildNodes()[index
];
2877 return DoJob::CONTINUE
;
2881 wxDataViewTreeNode
* GetResult() const
2887 wxDataViewTreeNode
* ret
;
2888 wxDataViewTreeNode
* parent
;
2891 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2893 wxASSERT( !IsVirtualList() );
2895 RowToTreeNodeJob
job( row
, -2, m_root
);
2896 Walker( m_root
, job
);
2897 return job
.GetResult();
2900 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2902 if (IsVirtualList())
2904 return wxDataViewItem( wxUIntToPtr(row
+1) );
2908 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2909 return node
? node
->GetItem() : wxDataViewItem();
2914 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2915 const wxDataViewItem
& item
)
2917 wxWindow
*parent
= GetParent();
2918 wxDataViewEvent
le(type
, parent
->GetId());
2920 le
.SetEventObject(parent
);
2921 le
.SetModel(GetModel());
2924 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2927 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2932 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2936 if (!node
->HasChildren())
2939 return node
->IsOpen();
2942 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2947 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2951 if (!node
->HasChildren())
2957 void wxDataViewMainWindow::Expand( unsigned int row
)
2962 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2966 if (!node
->HasChildren())
2969 if (!node
->IsOpen())
2971 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
2973 // Vetoed by the event handler.
2979 // build the children of current node
2980 if( node
->GetChildNodes().empty() )
2983 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
2986 // By expanding the node all row indices that are currently in the selection list
2987 // and are greater than our node have become invalid. So we have to correct that now.
2988 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2989 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2991 const unsigned testRow
= m_selection
[i
];
2992 // all rows above us are not affected, so skip them
2996 m_selection
[i
] += rowAdjustment
;
2999 if(m_currentRow
> row
)
3000 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3004 // Send the expanded event
3005 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3009 void wxDataViewMainWindow::Collapse(unsigned int row
)
3014 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3018 if (!node
->HasChildren())
3023 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3025 // Vetoed by the event handler.
3029 // Find out if there are selected items below the current node.
3030 bool selectCollapsingRow
= false;
3031 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3032 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3033 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3035 const unsigned testRow
= m_selection
[i
];
3036 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3038 selectCollapsingRow
= true;
3039 // get out as soon as we have found a node that is selected
3046 // If the node to be closed has selected items the user won't see those any longer.
3047 // We select the collapsing node in this case.
3048 if(selectCollapsingRow
)
3050 SelectAllRows(false);
3051 ChangeCurrentRow(row
);
3052 SelectRow(row
, true);
3053 SendSelectionChangedEvent(GetItemByRow(row
));
3057 // if there were no selected items below our node we still need to "fix" the
3058 // selection list to adjust for the changing of the row indices.
3059 // We actually do the opposite of what we are doing in Expand().
3060 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3062 const unsigned testRow
= m_selection
[i
];
3063 // all rows above us are not affected, so skip them
3067 m_selection
[i
] -= rowAdjustment
;
3070 // if the "current row" is being collapsed away we change it to the current row ;-)
3071 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3072 ChangeCurrentRow(row
);
3073 else if(m_currentRow
> row
)
3074 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3079 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3083 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3085 const wxDataViewModel
* model
= GetModel();
3092 // Compose the parent-chain for the item we are looking for
3093 wxVector
<wxDataViewItem
> parentChain
;
3094 wxDataViewItem
it( item
);
3097 parentChain
.push_back(it
);
3098 it
= model
->GetParent(it
);
3101 // Find the item along the parent-chain.
3102 // This algorithm is designed to speed up the node-finding method
3103 wxDataViewTreeNode
* node
= m_root
;
3104 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3106 if( node
->HasChildren() )
3108 if( node
->GetChildNodes().empty() )
3110 // Even though the item is a container, it doesn't have any
3111 // child nodes in the control's representation yet. We have
3112 // to realize its subtree now.
3114 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3117 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3120 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3122 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3123 if (currentNode
->GetItem() == parentChain
[iter
])
3125 if (currentNode
->GetItem() == item
)
3145 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3146 wxDataViewColumn
* &column
)
3148 wxDataViewColumn
*col
= NULL
;
3149 unsigned int cols
= GetOwner()->GetColumnCount();
3150 unsigned int colnum
= 0;
3152 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3153 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3155 col
= GetOwner()->GetColumnAt(colnum
);
3156 if (col
->IsHidden())
3157 continue; // skip it!
3159 unsigned int w
= col
->GetWidth();
3160 if (x_start
+w
>= (unsigned int)x
)
3167 item
= GetItemByRow( GetLineAt( y
) );
3170 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3171 const wxDataViewColumn
* column
)
3176 unsigned int cols
= GetOwner()->GetColumnCount();
3177 // If column is null the loop will compute the combined width of all columns.
3178 // Otherwise, it will compute the x position of the column we are looking for.
3179 for (unsigned int i
= 0; i
< cols
; i
++)
3181 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3186 if (col
->IsHidden())
3187 continue; // skip it!
3189 xpos
+= col
->GetWidth();
3190 width
+= col
->GetWidth();
3195 // If we have a column, we need can get its width directly.
3196 if(column
->IsHidden())
3199 width
= column
->GetWidth();
3204 // If we have no column, we reset the x position back to zero.
3208 // we have to take an expander column into account and compute its indentation
3209 // to get the correct x position where the actual text is
3211 int row
= GetRowByItem(item
);
3213 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3215 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3216 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3217 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3220 wxRect
itemRect( xpos
+ indent
,
3221 GetLineStart( row
),
3223 GetLineHeight( row
) );
3225 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3226 &itemRect
.x
, &itemRect
.y
);
3231 int wxDataViewMainWindow::RecalculateCount()
3233 if (IsVirtualList())
3235 wxDataViewVirtualListModel
*list_model
=
3236 (wxDataViewVirtualListModel
*) GetModel();
3238 return list_model
->GetCount();
3242 return m_root
->GetSubTreeCount();
3246 class ItemToRowJob
: public DoJob
3249 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3256 // Maybe binary search will help to speed up this process
3257 virtual int operator() ( wxDataViewTreeNode
* node
)
3260 if( node
->GetItem() == item
)
3265 if( node
->GetItem() == *m_iter
)
3268 return DoJob::CONTINUE
;
3272 ret
+= node
->GetSubTreeCount();
3273 return DoJob::SKIP_SUBTREE
;
3278 // the row number is begin from zero
3279 int GetResult() const
3283 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3284 wxDataViewItem item
;
3289 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3291 const wxDataViewModel
* model
= GetModel();
3295 if (IsVirtualList())
3297 return wxPtrToUInt( item
.GetID() ) -1;
3304 // Compose the parent-chain of the item we are looking for
3305 wxVector
<wxDataViewItem
> parentChain
;
3306 wxDataViewItem
it( item
);
3309 parentChain
.push_back(it
);
3310 it
= model
->GetParent(it
);
3313 // add an 'invalid' item to represent our 'invisible' root node
3314 parentChain
.push_back(wxDataViewItem());
3316 // the parent chain was created by adding the deepest parent first.
3317 // so if we want to start at the root node, we have to iterate backwards through the vector
3318 ItemToRowJob
job( item
, parentChain
.rbegin() );
3319 Walker( m_root
, job
);
3320 return job
.GetResult();
3324 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3325 wxDataViewTreeNode
* node
)
3327 if( !model
->IsContainer( item
) )
3330 wxDataViewItemArray children
;
3331 unsigned int num
= model
->GetChildren( item
, children
);
3333 for ( unsigned int index
= 0; index
< num
; index
++ )
3335 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3337 if( model
->IsContainer(children
[index
]) )
3338 n
->SetHasChildren( true );
3340 node
->InsertChild(n
, index
);
3343 wxASSERT( node
->IsOpen() );
3344 node
->ChangeSubTreeCount(+num
);
3347 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3351 if (GetModel()->IsVirtualListModel())
3357 m_root
= wxDataViewTreeNode::CreateRootNode();
3359 // First we define a invalid item to fetch the top-level elements
3360 wxDataViewItem item
;
3362 BuildTreeHelper( model
, item
, m_root
);
3366 void wxDataViewMainWindow::DestroyTree()
3368 if (!IsVirtualList())
3376 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3378 // Edit the current column editable in 'mode'. If no column is focused
3379 // (typically because the user has full row selected), try to find the
3380 // first editable column (this would typically be a checkbox for
3381 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3382 // focus on the checkbox column; or on the only editable text column).
3384 wxDataViewColumn
*candidate
= m_currentCol
;
3387 candidate
->GetRenderer()->GetMode() != mode
&&
3388 !m_currentColSetByKeyboard
)
3390 // If current column was set by mouse to something not editable (in
3391 // 'mode') and the user pressed Space/F2 to edit it, treat the
3392 // situation as if there was whole-row focus, because that's what is
3393 // visually indicated and the mouse click could very well be targeted
3394 // on the row rather than on an individual cell.
3396 // But if it was done by keyboard, respect that even if the column
3397 // isn't editable, because focus is visually on that column and editing
3398 // something else would be surprising.
3404 const unsigned cols
= GetOwner()->GetColumnCount();
3405 for ( unsigned i
= 0; i
< cols
; i
++ )
3407 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3408 if ( c
->IsHidden() )
3411 if ( c
->GetRenderer()->GetMode() == mode
)
3419 // If on container item without columns, only the expander column
3420 // may be directly editable:
3422 GetOwner()->GetExpanderColumn() != candidate
&&
3423 GetModel()->IsContainer(item
) &&
3424 !GetModel()->HasContainerColumns(item
) )
3426 candidate
= GetOwner()->GetExpanderColumn();
3432 if ( candidate
->GetRenderer()->GetMode() != mode
)
3438 void wxDataViewMainWindow::OnCharHook(wxKeyEvent
& event
)
3442 // Handle any keys special for the in-place editor and return without
3443 // calling Skip() below.
3444 switch ( event
.GetKeyCode() )
3447 m_editorRenderer
->CancelEditing();
3451 m_editorRenderer
->FinishEditing();
3459 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3461 wxWindow
* const parent
= GetParent();
3463 // propagate the char event upwards
3464 wxKeyEvent
eventForParent(event
);
3465 eventForParent
.SetEventObject(parent
);
3466 if ( parent
->ProcessWindowEvent(eventForParent
) )
3469 if ( parent
->HandleAsNavigationKey(event
) )
3472 // no item -> nothing to do
3473 if (!HasCurrentRow())
3479 // don't use m_linesPerPage directly as it might not be computed yet
3480 const int pageSize
= GetCountPerPage();
3481 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3483 switch ( event
.GetKeyCode() )
3487 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3488 // it. Only if that event is not handled do we activate column renderer (which
3489 // is normally done by Space) or even inline editing.
3491 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3493 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3496 le
.SetEventObject(parent
);
3497 le
.SetModel(GetModel());
3499 if ( parent
->ProcessWindowEvent(le
) )
3501 // else: fall through to WXK_SPACE handling
3506 // Space toggles activatable items or -- if not activatable --
3507 // starts inline editing (this is normally done using F2 on
3508 // Windows, but Space is common everywhere else, so use it too
3509 // for greater cross-platform compatibility).
3511 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3513 // Activate the current activatable column. If not column is focused (typically
3514 // because the user has full row selected), try to find the first activatable
3515 // column (this would typically be a checkbox and we don't want to force the user
3516 // to set focus on the checkbox column).
3517 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3519 if ( activatableCol
)
3521 const unsigned colIdx
= activatableCol
->GetModelColumn();
3522 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3524 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3525 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3526 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3530 // else: fall through to WXK_F2 handling
3535 if( !m_selection
.empty() )
3537 // Mimic Windows 7 behavior: edit the item that has focus
3538 // if it is selected and the first selected item if focus
3539 // is out of selection.
3541 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3544 sel
= m_selection
[0];
3547 const wxDataViewItem item
= GetItemByRow(sel
);
3549 // Edit the current column. If no column is focused
3550 // (typically because the user has full row selected), try
3551 // to find the first editable column.
3552 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3555 GetOwner()->StartEditor(item
, GetOwner()->GetColumnIndex(editableCol
));
3561 if ( m_currentRow
> 0 )
3562 OnVerticalNavigation( m_currentRow
- 1, event
);
3566 if ( m_currentRow
+ 1 < GetRowCount() )
3567 OnVerticalNavigation( m_currentRow
+ 1, event
);
3569 // Add the process for tree expanding/collapsing
3581 OnVerticalNavigation( GetRowCount() - 1, event
);
3586 OnVerticalNavigation( 0, event
);
3591 int steps
= pageSize
- 1;
3592 int index
= m_currentRow
- steps
;
3596 OnVerticalNavigation( index
, event
);
3602 int steps
= pageSize
- 1;
3603 unsigned int index
= m_currentRow
+ steps
;
3604 unsigned int count
= GetRowCount();
3605 if ( index
>= count
)
3608 OnVerticalNavigation( index
, event
);
3617 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3619 wxCHECK_RET( newCurrent
< GetRowCount(),
3620 wxT("invalid item index in OnVerticalNavigation()") );
3622 // if there is no selection, we cannot move it anywhere
3623 if (!HasCurrentRow())
3626 unsigned int oldCurrent
= m_currentRow
;
3628 // in single selection we just ignore Shift as we can't select several
3630 if ( event
.ShiftDown() && !IsSingleSel() )
3632 RefreshRow( oldCurrent
);
3634 ChangeCurrentRow( newCurrent
);
3636 // select all the items between the old and the new one
3637 if ( oldCurrent
> newCurrent
)
3639 newCurrent
= oldCurrent
;
3640 oldCurrent
= m_currentRow
;
3643 SelectRows( oldCurrent
, newCurrent
, true );
3644 if (oldCurrent
!=newCurrent
)
3645 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3649 RefreshRow( oldCurrent
);
3651 // all previously selected items are unselected unless ctrl is held
3652 if ( !event
.ControlDown() )
3653 SelectAllRows(false);
3655 ChangeCurrentRow( newCurrent
);
3657 if ( !event
.ControlDown() )
3659 SelectRow( m_currentRow
, true );
3660 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3663 RefreshRow( m_currentRow
);
3666 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3669 void wxDataViewMainWindow::OnLeftKey()
3673 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3677 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3679 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3682 // Because TryAdvanceCurrentColumn() return false, we are at the first
3683 // column or using whole-row selection. In this situation, we can use
3684 // the standard TreeView handling of the left key.
3685 if (node
->HasChildren() && node
->IsOpen())
3687 Collapse(m_currentRow
);
3691 // if the node is already closed, we move the selection to its parent
3692 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3696 int parent
= GetRowByItem( parent_node
->GetItem() );
3699 unsigned int row
= m_currentRow
;
3700 SelectRow( row
, false);
3701 SelectRow( parent
, true );
3702 ChangeCurrentRow( parent
);
3703 GetOwner()->EnsureVisible( parent
, -1 );
3704 SendSelectionChangedEvent( parent_node
->GetItem() );
3711 void wxDataViewMainWindow::OnRightKey()
3715 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3719 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3721 if ( node
->HasChildren() )
3723 if ( !node
->IsOpen() )
3725 Expand( m_currentRow
);
3729 // if the node is already open, we move the selection to the first child
3730 unsigned int row
= m_currentRow
;
3731 SelectRow( row
, false );
3732 SelectRow( row
+ 1, true );
3733 ChangeCurrentRow( row
+ 1 );
3734 GetOwner()->EnsureVisible( row
+ 1, -1 );
3735 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3740 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3745 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3747 if ( GetOwner()->GetColumnCount() == 0 )
3750 if ( !m_useCellFocus
)
3755 // navigation shouldn't work in branch nodes without other columns:
3756 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3760 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3764 m_currentCol
= GetOwner()->GetColumnAt(1);
3765 m_currentColSetByKeyboard
= true;
3766 RefreshRow(m_currentRow
);
3773 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3775 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3780 // We are going to the left of the second column. Reset to whole-row
3781 // focus (which means first column would be edited).
3782 m_currentCol
= NULL
;
3783 RefreshRow(m_currentRow
);
3787 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3788 m_currentColSetByKeyboard
= true;
3789 RefreshRow(m_currentRow
);
3793 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3795 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3797 // let the base handle mouse wheel events.
3802 // set the focus to ourself if any of the mouse buttons are pressed
3803 if(event
.ButtonDown() && !HasFocus())
3806 int x
= event
.GetX();
3807 int y
= event
.GetY();
3808 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3809 wxDataViewColumn
*col
= NULL
;
3812 unsigned int cols
= GetOwner()->GetColumnCount();
3814 for (i
= 0; i
< cols
; i
++)
3816 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3818 continue; // skip it!
3820 if (x
< xpos
+ c
->GetWidth())
3825 xpos
+= c
->GetWidth();
3828 wxDataViewModel
* const model
= GetModel();
3830 const unsigned int current
= GetLineAt( y
);
3831 const wxDataViewItem item
= GetItemByRow(current
);
3833 // Handle right clicking here, before everything else as context menu
3834 // events should be sent even when we click outside of any item, unlike all
3836 if (event
.RightUp())
3838 wxWindow
*parent
= GetParent();
3839 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3840 le
.SetEventObject(parent
);
3843 if ( item
.IsOk() && col
)
3846 le
.SetColumn( col
->GetModelColumn() );
3847 le
.SetDataViewColumn( col
);
3850 model
->GetValue( value
, item
, col
->GetModelColumn() );
3854 parent
->ProcessWindowEvent(le
);
3864 wxDataViewRenderer
*cell
= col
->GetRenderer();
3865 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3867 // Unselect all if below the last row ?
3872 wxDataViewColumn
* const
3873 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3875 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3876 // button) and also determine the offset of the real cell start, skipping
3877 // the indentation and the expander itself.
3878 bool hoverOverExpander
= false;
3880 if ((!IsList()) && (expander
== col
))
3882 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3884 int indent
= node
->GetIndentLevel();
3885 itemOffset
= GetOwner()->GetIndent()*indent
;
3887 if ( node
->HasChildren() )
3889 // we make the rectangle we are looking in a bit bigger than the actual
3890 // visual expander so the user can hit that little thing reliably
3891 wxRect
rect(itemOffset
,
3892 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3893 m_lineHeight
, m_lineHeight
);
3895 if( rect
.Contains(x
, y
) )
3897 // So the mouse is over the expander
3898 hoverOverExpander
= true;
3899 if (m_underMouse
&& m_underMouse
!= node
)
3901 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3902 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3904 if (m_underMouse
!= node
)
3906 // wxLogMessage("Do the row: %d", current);
3907 RefreshRow(current
);
3909 m_underMouse
= node
;
3913 // Account for the expander as well, even if this item doesn't have it,
3914 // its parent does so it still counts for the offset.
3915 itemOffset
+= m_lineHeight
;
3917 if (!hoverOverExpander
)
3919 if (m_underMouse
!= NULL
)
3921 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3922 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3923 m_underMouse
= NULL
;
3927 #if wxUSE_DRAG_AND_DROP
3928 if (event
.Dragging())
3930 if (m_dragCount
== 0)
3932 // we have to report the raw, physical coords as we want to be
3933 // able to call HitTest(event.m_pointDrag) from the user code to
3934 // get the item being dragged
3935 m_dragStart
= event
.GetPosition();
3940 if (m_dragCount
!= 3)
3943 if (event
.LeftIsDown())
3945 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3946 &m_dragStart
.x
, &m_dragStart
.y
);
3947 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3948 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
3950 // Notify cell about drag
3951 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3952 event
.SetEventObject( m_owner
);
3953 event
.SetItem( itemDragged
);
3954 event
.SetModel( model
);
3955 if (!m_owner
->HandleWindowEvent( event
))
3958 if (!event
.IsAllowed())
3961 wxDataObject
*obj
= event
.GetDataObject();
3965 wxDataViewDropSource
drag( this, drag_item_row
);
3966 drag
.SetData( *obj
);
3967 /* wxDragResult res = */ drag
.DoDragDrop();
3976 #endif // wxUSE_DRAG_AND_DROP
3978 bool simulateClick
= false;
3980 if (event
.ButtonDClick())
3982 m_renameTimer
->Stop();
3983 m_lastOnSame
= false;
3986 bool ignore_other_columns
=
3987 ((expander
!= col
) &&
3988 (model
->IsContainer(item
)) &&
3989 (!model
->HasContainerColumns(item
)));
3991 if (event
.LeftDClick())
3993 if(hoverOverExpander
)
3995 // a double click on the expander will be converted into a "simulated" normal click
3996 simulateClick
= true;
3998 else if ( current
== m_lineLastClicked
)
4000 wxWindow
*parent
= GetParent();
4001 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4003 le
.SetColumn( col
->GetModelColumn() );
4004 le
.SetDataViewColumn( col
);
4005 le
.SetEventObject(parent
);
4006 le
.SetModel(GetModel());
4008 parent
->ProcessWindowEvent(le
);
4013 // The first click was on another item, so don't interpret this as
4014 // a double click, but as a simple click instead
4015 simulateClick
= true;
4019 if (event
.LeftUp() && !hoverOverExpander
)
4021 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4023 // select single line
4024 SelectAllRows( false );
4025 SelectRow( m_lineSelectSingleOnUp
, true );
4026 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4029 // If the user click the expander, we do not do editing even if the column
4030 // with expander are editable
4031 if (m_lastOnSame
&& !ignore_other_columns
)
4033 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4034 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
4036 m_renameTimer
->Start( 100, true );
4040 m_lastOnSame
= false;
4041 m_lineSelectSingleOnUp
= (unsigned int)-1;
4043 else if(!event
.LeftUp())
4045 // This is necessary, because after a DnD operation in
4046 // from and to ourself, the up event is swallowed by the
4047 // DnD code. So on next non-up event (which means here and
4048 // now) m_lineSelectSingleOnUp should be reset.
4049 m_lineSelectSingleOnUp
= (unsigned int)-1;
4052 if (event
.RightDown())
4054 m_lineBeforeLastClicked
= m_lineLastClicked
;
4055 m_lineLastClicked
= current
;
4057 // If the item is already selected, do not update the selection.
4058 // Multi-selections should not be cleared if a selected item is clicked.
4059 if (!IsRowSelected(current
))
4061 SelectAllRows(false);
4062 const unsigned oldCurrent
= m_currentRow
;
4063 ChangeCurrentRow(current
);
4064 SelectRow(m_currentRow
,true);
4065 RefreshRow(oldCurrent
);
4066 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4069 else if (event
.MiddleDown())
4073 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4075 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4077 // hoverOverExpander being true tells us that our node must be
4078 // valid and have children.
4079 // So we don't need any extra checks.
4080 if( node
->IsOpen() )
4085 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4087 m_lineBeforeLastClicked
= m_lineLastClicked
;
4088 m_lineLastClicked
= current
;
4090 unsigned int oldCurrentRow
= m_currentRow
;
4091 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4093 bool cmdModifierDown
= event
.CmdDown();
4094 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4096 if ( IsSingleSel() || !IsRowSelected(current
) )
4098 SelectAllRows( false );
4099 ChangeCurrentRow(current
);
4100 SelectRow(m_currentRow
,true);
4101 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4103 else // multi sel & current is highlighted & no mod keys
4105 m_lineSelectSingleOnUp
= current
;
4106 ChangeCurrentRow(current
); // change focus
4109 else // multi sel & either ctrl or shift is down
4111 if (cmdModifierDown
)
4113 ChangeCurrentRow(current
);
4114 ReverseRowSelection(m_currentRow
);
4115 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4117 else if (event
.ShiftDown())
4119 ChangeCurrentRow(current
);
4121 unsigned int lineFrom
= oldCurrentRow
,
4124 if ( lineTo
< lineFrom
)
4127 lineFrom
= m_currentRow
;
4130 SelectRows(lineFrom
, lineTo
, true);
4131 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4133 else // !ctrl, !shift
4135 // test in the enclosing if should make it impossible
4136 wxFAIL_MSG( wxT("how did we get here?") );
4140 if (m_currentRow
!= oldCurrentRow
)
4141 RefreshRow( oldCurrentRow
);
4143 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4145 // Update selection here...
4147 m_currentColSetByKeyboard
= false;
4149 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4150 (current
== oldCurrentRow
)) && oldWasSelected
;
4152 // Call ActivateCell() after everything else as under GTK+
4153 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
4155 // notify cell about click
4156 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4158 wxRect
cell_rect( xpos
+ itemOffset
,
4159 GetLineStart( current
),
4160 col
->GetWidth() - itemOffset
,
4161 GetLineHeight( current
) );
4163 // Report position relative to the cell's custom area, i.e.
4164 // no the entire space as given by the control but the one
4165 // used by the renderer after calculation of alignment etc.
4167 // adjust the rectangle ourselves to account for the alignment
4168 wxRect rectItem
= cell_rect
;
4169 const int align
= cell
->GetAlignment();
4170 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4172 const wxSize size
= cell
->GetSize();
4174 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4176 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4177 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4178 else if ( align
& wxALIGN_RIGHT
)
4179 rectItem
.x
+= cell_rect
.width
- size
.x
;
4180 // else: wxALIGN_LEFT is the default
4183 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4185 if ( align
& wxALIGN_CENTER_VERTICAL
)
4186 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4187 else if ( align
& wxALIGN_BOTTOM
)
4188 rectItem
.y
+= cell_rect
.height
- size
.y
;
4189 // else: wxALIGN_TOP is the default
4193 wxMouseEvent
event2(event
);
4194 event2
.m_x
-= rectItem
.x
;
4195 event2
.m_y
-= rectItem
.y
;
4196 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4198 /* ignore ret */ cell
->WXActivateCell
4203 col
->GetModelColumn(),
4210 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4214 if (HasCurrentRow())
4220 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4224 if (HasCurrentRow())
4230 void wxDataViewMainWindow::OnColumnsCountChanged()
4232 int editableCount
= 0;
4234 const unsigned cols
= GetOwner()->GetColumnCount();
4235 for ( unsigned i
= 0; i
< cols
; i
++ )
4237 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4238 if ( c
->IsHidden() )
4240 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4244 m_useCellFocus
= (editableCount
> 1);
4249 //-----------------------------------------------------------------------------
4251 //-----------------------------------------------------------------------------
4253 WX_DEFINE_LIST(wxDataViewColumnList
)
4255 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4256 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4257 EVT_SIZE(wxDataViewCtrl::OnSize
)
4260 wxDataViewCtrl::~wxDataViewCtrl()
4263 GetModel()->RemoveNotifier( m_notifier
);
4266 m_colsBestWidths
.clear();
4269 void wxDataViewCtrl::Init()
4271 m_cols
.DeleteContents(true);
4274 // No sorting column at start
4275 m_sortingColumnIdx
= wxNOT_FOUND
;
4277 m_headerArea
= NULL
;
4279 m_colsDirty
= false;
4282 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4287 const wxValidator
& validator
,
4288 const wxString
& name
)
4290 // if ( (style & wxBORDER_MASK) == 0)
4291 // style |= wxBORDER_SUNKEN;
4295 if (!wxControl::Create( parent
, id
, pos
, size
,
4296 style
| wxScrolledWindowStyle
, validator
, name
))
4299 SetInitialSize(size
);
4302 MacSetClipChildren( true );
4305 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4307 // We use the cursor keys for moving the selection, not scrolling, so call
4308 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4309 // keyboard events forwarded to us from wxListMainWindow.
4310 DisableKeyboardScrolling();
4312 if (HasFlag(wxDV_NO_HEADER
))
4313 m_headerArea
= NULL
;
4315 m_headerArea
= new wxDataViewHeaderWindow(this);
4317 SetTargetWindow( m_clientArea
);
4319 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4321 sizer
->Add( m_headerArea
, 0, wxGROW
);
4322 sizer
->Add( m_clientArea
, 1, wxGROW
);
4328 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4330 return wxBORDER_THEME
;
4334 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4338 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4341 // we need to process arrows ourselves for scrolling
4342 if ( nMsg
== WM_GETDLGCODE
)
4344 rc
|= DLGC_WANTARROWS
;
4352 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4354 wxSize newsize
= size
;
4355 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4356 newsize
.y
-= m_headerArea
->GetSize().y
;
4361 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4363 // We need to override OnSize so that our scrolled
4364 // window a) does call Layout() to use sizers for
4365 // positioning the controls but b) does not query
4366 // the sizer for their size and use that for setting
4367 // the scrollable area as set that ourselves by
4368 // calling SetScrollbar() further down.
4374 // We must redraw the headers if their height changed. Normally this
4375 // shouldn't happen as the control shouldn't let itself be resized beneath
4376 // its minimal height but avoid the display artefacts that appear if it
4377 // does happen, e.g. because there is really not enough vertical space.
4378 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4379 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4381 m_headerArea
->Refresh();
4385 void wxDataViewCtrl::SetFocus()
4388 m_clientArea
->SetFocus();
4391 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4393 if (!wxDataViewCtrlBase::AssociateModel( model
))
4396 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4398 model
->AddNotifier( m_notifier
);
4400 m_clientArea
->DestroyTree();
4402 m_clientArea
->BuildTree(model
);
4404 m_clientArea
->UpdateDisplay();
4409 #if wxUSE_DRAG_AND_DROP
4411 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4413 return m_clientArea
->EnableDragSource( format
);
4416 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4418 return m_clientArea
->EnableDropTarget( format
);
4421 #endif // wxUSE_DRAG_AND_DROP
4423 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4425 if (!wxDataViewCtrlBase::AppendColumn(col
))
4428 m_cols
.Append( col
);
4429 m_colsBestWidths
.push_back(0);
4430 OnColumnsCountChanged();
4434 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4436 if (!wxDataViewCtrlBase::PrependColumn(col
))
4439 m_cols
.Insert( col
);
4440 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4441 OnColumnsCountChanged();
4445 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4447 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4450 m_cols
.Insert( pos
, col
);
4451 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4452 OnColumnsCountChanged();
4456 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4459 m_headerArea
->UpdateColumn(idx
);
4461 m_clientArea
->UpdateDisplay();
4464 void wxDataViewCtrl::OnColumnsCountChanged()
4467 m_headerArea
->SetColumnCount(GetColumnCount());
4469 m_clientArea
->OnColumnsCountChanged();
4472 void wxDataViewCtrl::DoSetExpanderColumn()
4474 m_clientArea
->UpdateDisplay();
4477 void wxDataViewCtrl::DoSetIndent()
4479 m_clientArea
->UpdateDisplay();
4482 unsigned int wxDataViewCtrl::GetColumnCount() const
4484 return m_cols
.GetCount();
4487 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4489 if ( !m_clientArea
)
4492 m_clientArea
->SetRowHeight(lineHeight
);
4497 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4502 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4504 // columns can't be reordered if there is no header window which allows
4506 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4509 return GetColumn(idx
);
4512 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4514 const unsigned count
= m_cols
.size();
4515 for ( unsigned n
= 0; n
< count
; n
++ )
4517 if ( m_cols
[n
] == column
)
4524 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4526 if ( m_colsBestWidths
[idx
] != 0 )
4527 return m_colsBestWidths
[idx
];
4529 const int count
= m_clientArea
->GetRowCount();
4530 wxDataViewColumn
*column
= GetColumn(idx
);
4531 wxDataViewRenderer
*renderer
=
4532 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4534 class MaxWidthCalculator
4537 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4538 wxDataViewMainWindow
*clientArea
,
4539 wxDataViewRenderer
*renderer
,
4540 const wxDataViewModel
*model
,
4545 m_clientArea(clientArea
),
4546 m_renderer(renderer
),
4549 m_expanderSize(expanderSize
)
4553 !clientArea
->IsList() &&
4555 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4558 void UpdateWithWidth(int width
)
4560 m_width
= wxMax(m_width
, width
);
4563 void UpdateWithRow(int row
)
4566 wxDataViewItem item
;
4568 if ( m_isExpanderCol
)
4570 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4571 item
= node
->GetItem();
4572 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4576 item
= m_clientArea
->GetItemByRow(row
);
4579 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4580 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4583 int GetMaxWidth() const { return m_width
; }
4587 const wxDataViewCtrl
*m_dvc
;
4588 wxDataViewMainWindow
*m_clientArea
;
4589 wxDataViewRenderer
*m_renderer
;
4590 const wxDataViewModel
*m_model
;
4592 bool m_isExpanderCol
;
4596 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4597 GetModel(), column
->GetModelColumn(),
4598 m_clientArea
->GetRowHeight());
4602 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4603 // Labels on native MSW header are indented on both sides
4605 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4606 calculator
.UpdateWithWidth(header_width
);
4609 // The code below deserves some explanation. For very large controls, we
4610 // simply can't afford to calculate sizes for all items, it takes too
4611 // long. So the best we can do is to check the first and the last N/2
4612 // items in the control for some sufficiently large N and calculate best
4613 // sizes from that. That can result in the calculated best width being too
4614 // small for some outliers, but it's better to get slightly imperfect
4615 // result than to wait several seconds after every update. To avoid highly
4616 // visible miscalculations, we also include all currently visible items
4617 // no matter what. Finally, the value of N is determined dynamically by
4618 // measuring how much time we spent on the determining item widths so far.
4621 int top_part_end
= count
;
4622 static const long CALC_TIMEOUT
= 20/*ms*/;
4623 // don't call wxStopWatch::Time() too often
4624 static const unsigned CALC_CHECK_FREQ
= 100;
4627 // use some hard-coded limit, that's the best we can do without timer
4628 int top_part_end
= wxMin(500, count
);
4629 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4633 for ( row
= 0; row
< top_part_end
; row
++ )
4636 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4637 timer
.Time() > CALC_TIMEOUT
)
4639 #endif // wxUSE_STOPWATCH
4640 calculator
.UpdateWithRow(row
);
4643 // row is the first unmeasured item now; that's our value of N/2
4649 // add bottom N/2 items now:
4650 const int bottom_part_start
= wxMax(row
, count
- row
);
4651 for ( row
= bottom_part_start
; row
< count
; row
++ )
4653 calculator
.UpdateWithRow(row
);
4656 // finally, include currently visible items in the calculation:
4657 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4658 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4659 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4661 first_visible
= wxMax(first_visible
, top_part_end
);
4662 last_visible
= wxMin(bottom_part_start
, last_visible
);
4664 for ( row
= first_visible
; row
< last_visible
; row
++ )
4666 calculator
.UpdateWithRow(row
);
4669 wxLogTrace("dataview",
4670 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4672 count
- bottom_part_start
,
4673 wxMax(0, last_visible
- first_visible
),
4677 int max_width
= calculator
.GetMaxWidth();
4678 if ( max_width
> 0 )
4679 max_width
+= 2 * PADDING_RIGHTLEFT
;
4681 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4685 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4686 unsigned int WXUNUSED(new_pos
))
4688 // do _not_ reorder m_cols elements here, they should always be in the
4689 // order in which columns were added, we only display the columns in
4691 m_clientArea
->UpdateDisplay();
4694 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4696 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4700 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4702 OnColumnsCountChanged();
4707 bool wxDataViewCtrl::ClearColumns()
4709 SetExpanderColumn(NULL
);
4711 m_colsBestWidths
.clear();
4712 OnColumnsCountChanged();
4716 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4718 m_colsBestWidths
[idx
] = 0;
4722 void wxDataViewCtrl::InvalidateColBestWidths()
4724 m_colsBestWidths
.clear();
4725 m_colsBestWidths
.resize(m_cols
.size());
4729 void wxDataViewCtrl::UpdateColWidths()
4731 if ( !m_headerArea
)
4734 const unsigned len
= m_colsBestWidths
.size();
4735 for ( unsigned i
= 0; i
< len
; i
++ )
4737 if ( m_colsBestWidths
[i
] == 0 )
4738 m_headerArea
->UpdateColumn(i
);
4742 void wxDataViewCtrl::OnInternalIdle()
4744 wxDataViewCtrlBase::OnInternalIdle();
4748 m_colsDirty
= false;
4753 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4756 unsigned int len
= GetColumnCount();
4757 for ( unsigned int i
= 0; i
< len
; i
++ )
4759 wxDataViewColumn
* col
= GetColumnAt(i
);
4766 // This returns the position in pixels which is not what we want.
4769 unsigned int len
= GetColumnCount();
4770 for ( unsigned int i
= 0; i
< len
; i
++ )
4772 wxDataViewColumn
* col
= GetColumnAt(i
);
4773 if (col
->IsHidden())
4775 ret
+= col
->GetWidth();
4778 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4786 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4788 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4789 : GetColumn(m_sortingColumnIdx
);
4792 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4794 return GetItemByRow(m_clientArea
->GetCurrentRow());
4797 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4799 const int row
= m_clientArea
->GetRowByItem(item
);
4801 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4802 if ( static_cast<unsigned>(row
) != oldCurrent
)
4804 m_clientArea
->ChangeCurrentRow(row
);
4805 m_clientArea
->RefreshRow(oldCurrent
);
4806 m_clientArea
->RefreshRow(row
);
4810 int wxDataViewCtrl::GetSelectedItemsCount() const
4812 return m_clientArea
->GetSelections().size();
4815 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4818 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4820 const size_t len
= selections
.size();
4821 for ( size_t i
= 0; i
< len
; i
++ )
4823 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4830 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4837 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4839 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4841 wxDataViewItem last_parent
;
4843 int len
= sel
.GetCount();
4844 for( int i
= 0; i
< len
; i
++ )
4846 wxDataViewItem item
= sel
[i
];
4847 wxDataViewItem parent
= GetModel()->GetParent( item
);
4850 if (parent
!= last_parent
)
4851 ExpandAncestors(item
);
4854 last_parent
= parent
;
4855 int row
= m_clientArea
->GetRowByItem( item
);
4857 selection
.Add( static_cast<unsigned int>(row
) );
4860 m_clientArea
->SetSelections( selection
);
4863 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4865 ExpandAncestors( item
);
4867 int row
= m_clientArea
->GetRowByItem( item
);
4870 // Unselect all rows before select another in the single select mode
4871 if (m_clientArea
->IsSingleSel())
4872 m_clientArea
->SelectAllRows(false);
4874 m_clientArea
->SelectRow(row
, true);
4876 // Also set focus to the selected item
4877 m_clientArea
->ChangeCurrentRow( row
);
4881 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4883 int row
= m_clientArea
->GetRowByItem( item
);
4885 m_clientArea
->SelectRow(row
, false);
4888 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4890 int row
= m_clientArea
->GetRowByItem( item
);
4893 return m_clientArea
->IsRowSelected(row
);
4898 void wxDataViewCtrl::SelectAll()
4900 m_clientArea
->SelectAllRows(true);
4903 void wxDataViewCtrl::UnselectAll()
4905 m_clientArea
->SelectAllRows(false);
4908 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4912 if( row
> (int) m_clientArea
->GetRowCount() )
4913 row
= m_clientArea
->GetRowCount();
4915 int first
= m_clientArea
->GetFirstVisibleRow();
4916 int last
= m_clientArea
->GetLastVisibleRow();
4918 m_clientArea
->ScrollTo( row
, column
);
4919 else if( row
> last
)
4920 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4922 m_clientArea
->ScrollTo( first
, column
);
4925 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4927 ExpandAncestors( item
);
4929 m_clientArea
->RecalculateDisplay();
4931 int row
= m_clientArea
->GetRowByItem(item
);
4934 if( column
== NULL
)
4935 EnsureVisible(row
, -1);
4937 EnsureVisible( row
, GetColumnIndex(column
) );
4942 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4943 wxDataViewColumn
* &column
) const
4945 m_clientArea
->HitTest(point
, item
, column
);
4948 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4949 const wxDataViewColumn
* column
) const
4951 return m_clientArea
->GetItemRect(item
, column
);
4954 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4956 return m_clientArea
->GetItemByRow( row
);
4959 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4961 return m_clientArea
->GetRowByItem( item
);
4964 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4966 ExpandAncestors( item
);
4968 int row
= m_clientArea
->GetRowByItem( item
);
4970 m_clientArea
->Expand(row
);
4973 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4975 int row
= m_clientArea
->GetRowByItem( item
);
4977 m_clientArea
->Collapse(row
);
4980 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4982 int row
= m_clientArea
->GetRowByItem( item
);
4984 return m_clientArea
->IsExpanded(row
);
4988 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4990 wxDataViewColumn
* col
= GetColumn( column
);
4994 m_clientArea
->StartEditing(item
, col
);
4997 #endif // !wxUSE_GENERICDATAVIEWCTRL
4999 #endif // wxUSE_DATAVIEWCTRL