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/calctrl.h"
42 #include "wx/popupwin.h"
43 #include "wx/renderer.h"
44 #include "wx/dcbuffer.h"
47 #include "wx/listimpl.cpp"
48 #include "wx/imaglist.h"
49 #include "wx/headerctrl.h"
51 #include "wx/stopwatch.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 //-----------------------------------------------------------------------------
90 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
97 m_sortAscending
= true;
100 int wxDataViewColumn::GetWidth() const
104 case wxCOL_WIDTH_DEFAULT
:
105 return wxDVC_DEFAULT_WIDTH
;
107 case wxCOL_WIDTH_AUTOSIZE
:
108 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
109 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
116 void wxDataViewColumn::UpdateDisplay()
120 int idx
= m_owner
->GetColumnIndex( this );
121 m_owner
->OnColumnChange( idx
);
125 //-----------------------------------------------------------------------------
126 // wxDataViewHeaderWindow
127 //-----------------------------------------------------------------------------
129 class wxDataViewHeaderWindow
: public wxHeaderCtrl
132 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
133 : wxHeaderCtrl(parent
)
137 wxDataViewCtrl
*GetOwner() const
138 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
141 // implement/override wxHeaderCtrl functions by forwarding them to the main
143 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
145 return *(GetOwner()->GetColumn(idx
));
148 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
150 wxDataViewCtrl
* const owner
= GetOwner();
152 int widthContents
= owner
->GetBestColumnWidth(idx
);
153 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
154 owner
->OnColumnChange(idx
);
160 bool SendEvent(wxEventType type
, unsigned int n
)
162 wxDataViewCtrl
* const owner
= GetOwner();
163 wxDataViewEvent
event(type
, owner
->GetId());
165 event
.SetEventObject(owner
);
167 event
.SetDataViewColumn(owner
->GetColumn(n
));
168 event
.SetModel(owner
->GetModel());
170 // for events created by wxDataViewHeaderWindow the
171 // row / value fields are not valid
172 return owner
->GetEventHandler()->ProcessEvent(event
);
175 void OnClick(wxHeaderCtrlEvent
& event
)
177 const unsigned idx
= event
.GetColumn();
179 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
182 // default handling for the column click is to sort by this column or
183 // toggle its sort order
184 wxDataViewCtrl
* const owner
= GetOwner();
185 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
186 if ( !col
->IsSortable() )
188 // no default handling for non-sortable columns
193 if ( col
->IsSortKey() )
195 // already using this column for sorting, just change the order
196 col
->ToggleSortOrder();
198 else // not using this column for sorting yet
200 // first unset the old sort column if any
201 int oldSortKey
= owner
->GetSortingColumnIndex();
202 if ( oldSortKey
!= wxNOT_FOUND
)
204 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
205 owner
->OnColumnChange(oldSortKey
);
208 owner
->SetSortingColumnIndex(idx
);
212 wxDataViewModel
* const model
= owner
->GetModel();
216 owner
->OnColumnChange(idx
);
218 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
221 void OnRClick(wxHeaderCtrlEvent
& event
)
223 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
228 void OnResize(wxHeaderCtrlEvent
& event
)
230 wxDataViewCtrl
* const owner
= GetOwner();
232 const unsigned col
= event
.GetColumn();
233 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
234 GetOwner()->OnColumnChange(col
);
237 void OnEndReorder(wxHeaderCtrlEvent
& event
)
239 wxDataViewCtrl
* const owner
= GetOwner();
240 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
241 event
.GetNewOrder());
244 DECLARE_EVENT_TABLE()
245 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
248 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
249 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
250 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
252 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
253 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
255 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
258 //-----------------------------------------------------------------------------
259 // wxDataViewRenameTimer
260 //-----------------------------------------------------------------------------
262 class wxDataViewRenameTimer
: public wxTimer
265 wxDataViewMainWindow
*m_owner
;
268 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
272 //-----------------------------------------------------------------------------
273 // wxDataViewTreeNode
274 //-----------------------------------------------------------------------------
276 class wxDataViewTreeNode
;
277 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
279 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
280 wxDataViewTreeNode
** node2
);
282 class wxDataViewTreeNode
285 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
292 ~wxDataViewTreeNode()
296 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
297 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
308 static wxDataViewTreeNode
* CreateRootNode()
310 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
311 n
->SetHasChildren(true);
312 n
->m_branchData
->open
= true;
316 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
318 const wxDataViewTreeNodes
& GetChildNodes() const
320 wxASSERT( m_branchData
!= NULL
);
321 return m_branchData
->children
;
324 void AddChild( wxDataViewTreeNode
* node
)
327 m_branchData
= new BranchNodeData
;
329 m_branchData
->children
.Add( node
);
330 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
332 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
335 void RemoveChild(wxDataViewTreeNode
*node
)
337 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
338 m_branchData
->children
.Remove(node
);
341 const wxDataViewItem
& GetItem() const { return m_item
; }
342 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
344 int GetIndentLevel() const
347 const wxDataViewTreeNode
* node
= this;
348 while( node
->GetParent()->GetParent() != NULL
)
350 node
= node
->GetParent();
358 return m_branchData
&& m_branchData
->open
;
363 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
367 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
368 const int len
= nodes
.GetCount();
369 for ( int i
= 0;i
< len
; i
++)
370 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
372 if (m_branchData
->open
)
374 ChangeSubTreeCount(-sum
);
375 m_branchData
->open
= !m_branchData
->open
;
379 m_branchData
->open
= !m_branchData
->open
;
380 ChangeSubTreeCount(+sum
);
384 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
385 // even if GetChildNodes() is empty; see below.
386 bool HasChildren() const
388 return m_branchData
!= NULL
;
391 void SetHasChildren(bool has
)
395 wxDELETE(m_branchData
);
397 else if ( m_branchData
== NULL
)
399 m_branchData
= new BranchNodeData
;
403 int GetSubTreeCount() const
405 return m_branchData
? m_branchData
->subTreeCount
: 0;
408 void ChangeSubTreeCount( int num
)
410 wxASSERT( m_branchData
!= NULL
);
412 if( !m_branchData
->open
)
415 m_branchData
->subTreeCount
+= num
;
416 wxASSERT( m_branchData
->subTreeCount
>= 0 );
419 m_parent
->ChangeSubTreeCount(num
);
429 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
431 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
432 int len
= nodes
.GetCount();
433 for (int i
= 0; i
< len
; i
++)
435 if ( nodes
[i
]->HasChildren() )
443 wxDataViewTreeNode
*m_parent
;
445 // Corresponding model item.
446 wxDataViewItem m_item
;
448 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
449 // separate struct in order to conserve memory.
450 struct BranchNodeData
458 // Child nodes. Note that this may be empty even if m_hasChildren in
459 // case this branch of the tree wasn't expanded and realized yet.
460 wxDataViewTreeNodes children
;
462 // Is the branch node currently open (expanded)?
465 // Total count of expanded (i.e. visible with the help of some
466 // scrolling) items in the subtree, but excluding this node. I.e. it is
467 // 0 for leaves and is the number of rows the subtree occupies for
472 BranchNodeData
*m_branchData
;
476 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
477 wxDataViewTreeNode
** node2
)
479 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
483 //-----------------------------------------------------------------------------
484 // wxDataViewMainWindow
485 //-----------------------------------------------------------------------------
487 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
489 class wxDataViewMainWindow
: public wxWindow
492 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
494 const wxPoint
&pos
= wxDefaultPosition
,
495 const wxSize
&size
= wxDefaultSize
,
496 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
497 virtual ~wxDataViewMainWindow();
499 bool IsList() const { return GetModel()->IsListModel(); }
500 bool IsVirtualList() const { return m_root
== NULL
; }
502 // notifications from wxDataViewModel
503 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
504 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
505 bool ItemChanged( const wxDataViewItem
&item
);
506 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
510 if (!IsVirtualList())
520 g_model
= GetModel();
521 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
524 if (g_model
->HasDefaultCompare())
532 g_column
= col
->GetModelColumn();
533 g_asending
= col
->IsSortOrderAscending();
536 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
537 wxDataViewCtrl
*GetOwner() { return m_owner
; }
538 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
540 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
541 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
543 #if wxUSE_DRAG_AND_DROP
544 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
545 #endif // wxUSE_DRAG_AND_DROP
546 void OnPaint( wxPaintEvent
&event
);
547 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
548 void OnChar( wxKeyEvent
&event
);
549 void OnMouse( wxMouseEvent
&event
);
550 void OnSetFocus( wxFocusEvent
&event
);
551 void OnKillFocus( wxFocusEvent
&event
);
553 void UpdateDisplay();
554 void RecalculateDisplay();
555 void OnInternalIdle();
557 void OnRenameTimer();
559 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
560 void ScrollTo( int rows
, int column
);
562 unsigned GetCurrentRow() const { return m_currentRow
; }
563 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
564 void ChangeCurrentRow( unsigned int row
);
566 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
567 bool IsEmpty() { return GetRowCount() == 0; }
569 int GetCountPerPage() const;
570 int GetEndOfLastCol() const;
571 unsigned int GetFirstVisibleRow() const;
573 // I change this method to un const because in the tree view,
574 // the displaying number of the tree are changing along with the
575 // expanding/collapsing of the tree nodes
576 unsigned int GetLastVisibleRow();
577 unsigned int GetRowCount();
579 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
580 void SetSelections( const wxDataViewSelection
& sel
)
581 { m_selection
= sel
; UpdateDisplay(); }
582 void Select( const wxArrayInt
& aSelections
);
583 void SelectAllRows( bool on
);
584 void SelectRow( unsigned int row
, bool on
);
585 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
586 void ReverseRowSelection( unsigned int row
);
587 bool IsRowSelected( unsigned int row
);
588 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
590 void RefreshRow( unsigned int row
);
591 void RefreshRows( unsigned int from
, unsigned int to
);
592 void RefreshRowsAfter( unsigned int firstRow
);
594 // returns the colour to be used for drawing the rules
595 wxColour
GetRuleColour() const
597 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
600 wxRect
GetLineRect( unsigned int row
) const;
602 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
603 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
604 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
606 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
608 // Some useful functions for row and item mapping
609 wxDataViewItem
GetItemByRow( unsigned int row
) const;
610 int GetRowByItem( const wxDataViewItem
& item
) const;
612 // Methods for building the mapping tree
613 void BuildTree( wxDataViewModel
* model
);
615 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
616 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
618 void Expand( unsigned int row
);
619 void Collapse( unsigned int row
);
620 bool IsExpanded( unsigned int row
) const;
621 bool HasChildren( unsigned int row
) const;
623 #if wxUSE_DRAG_AND_DROP
624 bool EnableDragSource( const wxDataFormat
&format
);
625 bool EnableDropTarget( const wxDataFormat
&format
);
627 void RemoveDropHint();
628 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
629 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
630 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
632 #endif // wxUSE_DRAG_AND_DROP
635 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
636 // We did not need this temporarily
637 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
639 int RecalculateCount();
641 // Return false only if the event was vetoed by its handler.
642 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
644 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
647 wxDataViewCtrl
*m_owner
;
651 wxDataViewColumn
*m_currentCol
;
652 unsigned int m_currentRow
;
653 wxDataViewSelection m_selection
;
655 wxDataViewRenameTimer
*m_renameTimer
;
660 #if wxUSE_DRAG_AND_DROP
665 wxDataFormat m_dragFormat
;
668 wxDataFormat m_dropFormat
;
670 unsigned int m_dropHintLine
;
671 #endif // wxUSE_DRAG_AND_DROP
673 // for double click logic
674 unsigned int m_lineLastClicked
,
675 m_lineBeforeLastClicked
,
676 m_lineSelectSingleOnUp
;
678 // the pen used to draw horiz/vertical rules
681 // the pen used to draw the expander and the lines
684 // This is the tree structure of the model
685 wxDataViewTreeNode
* m_root
;
688 // This is the tree node under the cursor
689 wxDataViewTreeNode
* m_underMouse
;
692 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
693 DECLARE_EVENT_TABLE()
696 // ---------------------------------------------------------
697 // wxGenericDataViewModelNotifier
698 // ---------------------------------------------------------
700 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
703 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
704 { m_mainWindow
= mainWindow
; }
706 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
707 { return m_mainWindow
->ItemAdded( parent
, item
); }
708 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
709 { return m_mainWindow
->ItemDeleted( parent
, item
); }
710 virtual bool ItemChanged( const wxDataViewItem
& item
)
711 { return m_mainWindow
->ItemChanged(item
); }
712 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
713 { return m_mainWindow
->ValueChanged( item
, col
); }
714 virtual bool Cleared()
715 { return m_mainWindow
->Cleared(); }
716 virtual void Resort()
717 { m_mainWindow
->Resort(); }
719 wxDataViewMainWindow
*m_mainWindow
;
722 // ---------------------------------------------------------
723 // wxDataViewRenderer
724 // ---------------------------------------------------------
726 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
728 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
729 wxDataViewCellMode mode
,
731 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
735 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
739 wxDataViewRenderer::~wxDataViewRenderer()
744 wxDC
*wxDataViewRenderer::GetDC()
748 if (GetOwner() == NULL
)
750 if (GetOwner()->GetOwner() == NULL
)
752 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
758 void wxDataViewRenderer::SetAlignment( int align
)
763 int wxDataViewRenderer::GetAlignment() const
768 // ---------------------------------------------------------
769 // wxDataViewCustomRenderer
770 // ---------------------------------------------------------
772 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
774 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
775 wxDataViewCellMode mode
, int align
) :
776 wxDataViewRenderer( varianttype
, mode
, align
)
780 // ---------------------------------------------------------
781 // wxDataViewTextRenderer
782 // ---------------------------------------------------------
784 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
786 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
787 wxDataViewCellMode mode
, int align
) :
788 wxDataViewRenderer( varianttype
, mode
, align
)
792 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
794 m_text
= value
.GetString();
799 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
804 bool wxDataViewTextRenderer::HasEditorCtrl() const
809 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
810 wxRect labelRect
, const wxVariant
&value
)
812 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
813 wxPoint(labelRect
.x
,labelRect
.y
),
814 wxSize(labelRect
.width
,labelRect
.height
),
815 wxTE_PROCESS_ENTER
);
817 // select the text in the control an place the cursor at the end
818 ctrl
->SetInsertionPointEnd();
824 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
826 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
827 value
= text
->GetValue();
831 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
833 RenderText(m_text
, 0, rect
, dc
, state
);
837 wxSize
wxDataViewTextRenderer::GetSize() const
840 return GetTextExtent(m_text
);
842 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
845 // ---------------------------------------------------------
846 // wxDataViewBitmapRenderer
847 // ---------------------------------------------------------
849 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
851 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
852 wxDataViewCellMode mode
, int align
) :
853 wxDataViewRenderer( varianttype
, mode
, align
)
857 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
859 if (value
.GetType() == wxT("wxBitmap"))
861 if (value
.GetType() == wxT("wxIcon"))
867 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
872 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
875 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
876 else if (m_icon
.IsOk())
877 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
882 wxSize
wxDataViewBitmapRenderer::GetSize() const
885 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
886 else if (m_icon
.IsOk())
887 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
889 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
892 // ---------------------------------------------------------
893 // wxDataViewToggleRenderer
894 // ---------------------------------------------------------
896 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
898 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
899 wxDataViewCellMode mode
, int align
) :
900 wxDataViewRenderer( varianttype
, mode
, align
)
905 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
907 m_toggle
= value
.GetBool();
912 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
917 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
921 flags
|= wxCONTROL_CHECKED
;
922 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
923 GetEnabled() == false)
924 flags
|= wxCONTROL_DISABLED
;
926 // check boxes we draw must always have the same, standard size (if it's
927 // bigger than the cell size the checkbox will be truncated because the
928 // caller had set the clipping rectangle to prevent us from drawing outside
930 cell
.SetSize(GetSize());
932 wxRendererNative::Get().DrawCheckBox(
933 GetOwner()->GetOwner(),
941 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
942 const wxRect
& WXUNUSED(cell
),
943 wxDataViewModel
*model
,
944 const wxDataViewItem
& item
,
947 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
948 if (!wxRect(GetSize()).Contains(cursor
))
951 if (model
->IsEnabled(item
, col
))
953 model
->ChangeValue(!m_toggle
, item
, col
);
960 wxSize
wxDataViewToggleRenderer::GetSize() const
962 // the window parameter is not used by GetCheckBoxSize() so it's
964 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
967 // ---------------------------------------------------------
968 // wxDataViewProgressRenderer
969 // ---------------------------------------------------------
971 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
973 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
974 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
975 wxDataViewRenderer( varianttype
, mode
, align
)
981 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
983 m_value
= (long) value
;
985 if (m_value
< 0) m_value
= 0;
986 if (m_value
> 100) m_value
= 100;
991 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
993 value
= (long) m_value
;
998 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1000 // deflate the rect to leave a small border between bars in adjacent rows
1001 wxRect bar
= rect
.Deflate(0, 1);
1003 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1004 dc
->SetPen( *wxBLACK_PEN
);
1005 dc
->DrawRectangle( bar
);
1007 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1008 dc
->SetPen( *wxTRANSPARENT_PEN
);
1010 const wxDataViewItemAttr
& attr
= GetAttr();
1011 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1013 dc
->DrawRectangle( bar
);
1018 wxSize
wxDataViewProgressRenderer::GetSize() const
1020 return wxSize(40,12);
1023 // ---------------------------------------------------------
1024 // wxDataViewDateRenderer
1025 // ---------------------------------------------------------
1027 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
1029 #if wxUSE_DATE_RENDERER_POPUP
1031 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1034 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1035 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
1036 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
1041 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
1042 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1043 sizer
->Add( m_cal
, 1, wxGROW
);
1048 void OnCalendar( wxCalendarEvent
&event
);
1050 wxCalendarCtrl
*m_cal
;
1051 wxDataViewModel
*m_model
;
1053 const wxDataViewItem
& m_item
;
1056 virtual void OnDismiss()
1061 DECLARE_EVENT_TABLE()
1064 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1065 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1068 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1070 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1074 #endif // wxUSE_DATE_RENDERER_POPUP
1076 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1078 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1079 wxDataViewCellMode mode
, int align
) :
1080 wxDataViewRenderer( varianttype
, mode
, align
)
1084 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1086 m_date
= value
.GetDateTime();
1091 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1097 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1099 wxString tmp
= m_date
.FormatDate();
1100 RenderText( tmp
, 0, cell
, dc
, state
);
1104 wxSize
wxDataViewDateRenderer::GetSize() const
1106 return GetTextExtent(m_date
.FormatDate());
1109 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1110 wxDataViewModel
*model
,
1111 const wxDataViewItem
& item
,
1114 wxDateTime dtOld
= m_date
;
1116 #if wxUSE_DATE_RENDERER_POPUP
1117 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1118 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1119 wxPoint pos
= wxGetMousePosition();
1122 popup
->Popup( popup
->m_cal
);
1123 #else // !wxUSE_DATE_RENDERER_POPUP
1124 wxMessageBox(dtOld
.Format());
1125 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1130 // ---------------------------------------------------------
1131 // wxDataViewIconTextRenderer
1132 // ---------------------------------------------------------
1134 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1136 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1137 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1138 wxDataViewRenderer( varianttype
, mode
, align
)
1141 SetAlignment(align
);
1144 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1150 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1155 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1159 const wxIcon
& icon
= m_value
.GetIcon();
1162 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1163 xoffset
= icon
.GetWidth()+4;
1166 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1171 wxSize
wxDataViewIconTextRenderer::GetSize() const
1173 if (!m_value
.GetText().empty())
1175 wxSize size
= GetTextExtent(m_value
.GetText());
1177 if (m_value
.GetIcon().IsOk())
1178 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1181 return wxSize(80,20);
1184 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1186 wxDataViewIconText iconText
;
1189 wxString text
= iconText
.GetText();
1191 // adjust the label rect to take the width of the icon into account
1192 if (iconText
.GetIcon().IsOk())
1194 int w
= iconText
.GetIcon().GetWidth() + 4;
1196 labelRect
.width
-= w
;
1199 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1200 wxPoint(labelRect
.x
,labelRect
.y
),
1201 wxSize(labelRect
.width
,labelRect
.height
),
1202 wxTE_PROCESS_ENTER
);
1204 // select the text in the control an place the cursor at the end
1205 ctrl
->SetInsertionPointEnd();
1211 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1213 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1215 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1220 //-----------------------------------------------------------------------------
1221 // wxDataViewDropTarget
1222 //-----------------------------------------------------------------------------
1224 #if wxUSE_DRAG_AND_DROP
1226 class wxBitmapCanvas
: public wxWindow
1229 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1230 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1233 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1236 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1239 dc
.DrawBitmap( m_bitmap
, 0, 0);
1245 class wxDataViewDropSource
: public wxDropSource
1248 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1256 ~wxDataViewDropSource()
1261 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1263 wxPoint pos
= wxGetMousePosition();
1267 int liney
= m_win
->GetLineStart( m_row
);
1269 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1270 m_win
->ClientToScreen( &linex
, &liney
);
1271 m_dist_x
= pos
.x
- linex
;
1272 m_dist_y
= pos
.y
- liney
;
1275 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1277 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1278 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1280 wxFRAME_TOOL_WINDOW
|
1281 wxFRAME_FLOAT_ON_PARENT
|
1282 wxFRAME_NO_TASKBAR
|
1284 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1289 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1290 m_hint
->SetTransparent( 128 );
1296 wxDataViewMainWindow
*m_win
;
1299 int m_dist_x
,m_dist_y
;
1303 class wxDataViewDropTarget
: public wxDropTarget
1306 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1312 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1314 wxDataFormat format
= GetMatchingPair();
1315 if (format
== wxDF_INVALID
)
1317 return m_win
->OnDragOver( format
, x
, y
, def
);
1320 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1322 wxDataFormat format
= GetMatchingPair();
1323 if (format
== wxDF_INVALID
)
1325 return m_win
->OnDrop( format
, x
, y
);
1328 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1330 wxDataFormat format
= GetMatchingPair();
1331 if (format
== wxDF_INVALID
)
1335 return m_win
->OnData( format
, x
, y
, def
);
1338 virtual void OnLeave()
1339 { m_win
->OnLeave(); }
1341 wxDataViewMainWindow
*m_win
;
1344 #endif // wxUSE_DRAG_AND_DROP
1346 //-----------------------------------------------------------------------------
1347 // wxDataViewRenameTimer
1348 //-----------------------------------------------------------------------------
1350 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1355 void wxDataViewRenameTimer::Notify()
1357 m_owner
->OnRenameTimer();
1360 //-----------------------------------------------------------------------------
1361 // wxDataViewMainWindow
1362 //-----------------------------------------------------------------------------
1364 // The tree building helper, declared firstly
1365 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1366 wxDataViewTreeNode
* node
);
1368 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1370 if (row1
> row2
) return 1;
1371 if (row1
== row2
) return 0;
1375 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1377 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1378 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1379 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1380 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1381 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1382 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1385 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1386 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1387 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1388 m_selection( wxDataViewSelectionCmp
)
1393 m_lastOnSame
= false;
1394 m_renameTimer
= new wxDataViewRenameTimer( this );
1396 // TODO: user better initial values/nothing selected
1397 m_currentCol
= NULL
;
1400 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1402 #if wxUSE_DRAG_AND_DROP
1404 m_dragStart
= wxPoint(0,0);
1406 m_dragEnabled
= false;
1407 m_dropEnabled
= false;
1409 m_dropHintLine
= (unsigned int) -1;
1410 #endif // wxUSE_DRAG_AND_DROP
1412 m_lineLastClicked
= (unsigned int) -1;
1413 m_lineBeforeLastClicked
= (unsigned int) -1;
1414 m_lineSelectSingleOnUp
= (unsigned int) -1;
1418 SetBackgroundColour( *wxWHITE
);
1420 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1422 m_penRule
= wxPen(GetRuleColour());
1424 // compose a pen whichcan draw black lines
1425 // TODO: maybe there is something system colour to use
1426 m_penExpander
= wxPen(wxColour(0,0,0));
1428 m_root
= wxDataViewTreeNode::CreateRootNode();
1430 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1432 m_underMouse
= NULL
;
1436 wxDataViewMainWindow::~wxDataViewMainWindow()
1439 delete m_renameTimer
;
1443 #if wxUSE_DRAG_AND_DROP
1444 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1446 m_dragFormat
= format
;
1447 m_dragEnabled
= format
!= wxDF_INVALID
;
1452 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1454 m_dropFormat
= format
;
1455 m_dropEnabled
= format
!= wxDF_INVALID
;
1458 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1463 void wxDataViewMainWindow::RemoveDropHint()
1468 RefreshRow( m_dropHintLine
);
1469 m_dropHintLine
= (unsigned int) -1;
1473 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1474 wxCoord y
, wxDragResult def
)
1478 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1479 unsigned int row
= GetLineAt( yy
);
1481 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1487 wxDataViewItem item
= GetItemByRow( row
);
1489 wxDataViewModel
*model
= GetModel();
1491 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1492 event
.SetEventObject( m_owner
);
1493 event
.SetItem( item
);
1494 event
.SetModel( model
);
1495 event
.SetDataFormat( format
);
1496 if (!m_owner
->HandleWindowEvent( event
))
1502 if (!event
.IsAllowed())
1509 if (m_dropHint
&& (row
!= m_dropHintLine
))
1510 RefreshRow( m_dropHintLine
);
1512 m_dropHintLine
= row
;
1518 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1524 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1525 unsigned int row
= GetLineAt( yy
);
1527 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1530 wxDataViewItem item
= GetItemByRow( row
);
1532 wxDataViewModel
*model
= GetModel();
1534 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1535 event
.SetEventObject( m_owner
);
1536 event
.SetItem( item
);
1537 event
.SetModel( model
);
1538 event
.SetDataFormat( format
);
1539 if (!m_owner
->HandleWindowEvent( event
))
1542 if (!event
.IsAllowed())
1548 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1553 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1554 unsigned int row
= GetLineAt( yy
);
1556 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1559 wxDataViewItem item
= GetItemByRow( row
);
1561 wxDataViewModel
*model
= GetModel();
1563 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1565 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1566 event
.SetEventObject( m_owner
);
1567 event
.SetItem( item
);
1568 event
.SetModel( model
);
1569 event
.SetDataFormat( format
);
1570 event
.SetDataSize( obj
->GetSize() );
1571 event
.SetDataBuffer( obj
->GetData() );
1572 if (!m_owner
->HandleWindowEvent( event
))
1575 if (!event
.IsAllowed())
1581 void wxDataViewMainWindow::OnLeave()
1586 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1588 int height
= GetLineHeight( row
);
1590 unsigned int cols
= GetOwner()->GetColumnCount();
1592 for (col
= 0; col
< cols
; col
++)
1594 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1595 if (column
->IsHidden())
1596 continue; // skip it!
1597 width
+= column
->GetWidth();
1603 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1604 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1605 indent
= indent
+ m_lineHeight
;
1606 // try to use the m_lineHeight as the expander space
1610 wxBitmap
bitmap( width
, height
);
1611 wxMemoryDC
dc( bitmap
);
1612 dc
.SetFont( GetFont() );
1613 dc
.SetPen( *wxBLACK_PEN
);
1614 dc
.SetBrush( *wxWHITE_BRUSH
);
1615 dc
.DrawRectangle( 0,0,width
,height
);
1617 wxDataViewModel
*model
= m_owner
->GetModel();
1619 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1622 // TODO-RTL: last column for RTL support
1623 expander
= GetOwner()->GetColumnAt( 0 );
1624 GetOwner()->SetExpanderColumn(expander
);
1629 for (col
= 0; col
< cols
; col
++)
1631 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1632 wxDataViewRenderer
*cell
= column
->GetRenderer();
1634 if (column
->IsHidden())
1635 continue; // skip it!
1637 width
= column
->GetWidth();
1639 if (column
== expander
)
1642 wxDataViewItem item
= GetItemByRow( row
);
1643 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1645 wxRect
item_rect(x
, 0, width
, height
);
1646 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1648 // dc.SetClippingRegion( item_rect );
1649 cell
->WXCallRender(item_rect
, &dc
, 0);
1650 // dc.DestroyClippingRegion();
1658 #endif // wxUSE_DRAG_AND_DROP
1661 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1663 wxDataViewModel
*model
= GetModel();
1664 wxAutoBufferedPaintDC
dc( this );
1667 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1668 dc
.SetPen( *wxTRANSPARENT_PEN
);
1669 dc
.DrawRectangle(GetClientSize());
1674 // No items to draw.
1679 GetOwner()->PrepareDC( dc
);
1680 dc
.SetFont( GetFont() );
1682 wxRect update
= GetUpdateRegion().GetBox();
1683 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1685 // compute which items needs to be redrawn
1686 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1687 unsigned int item_count
=
1688 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1689 (int)(GetRowCount( ) - item_start
));
1690 unsigned int item_last
= item_start
+ item_count
;
1692 // Send the event to wxDataViewCtrl itself.
1693 wxWindow
* const parent
= GetParent();
1694 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1695 cache_event
.SetEventObject(parent
);
1696 cache_event
.SetCache(item_start
, item_last
- 1);
1697 parent
->ProcessWindowEvent(cache_event
);
1699 // compute which columns needs to be redrawn
1700 unsigned int cols
= GetOwner()->GetColumnCount();
1703 // we assume that we have at least one column below and painting an
1704 // empty control is unnecessary anyhow
1708 unsigned int col_start
= 0;
1709 unsigned int x_start
;
1710 for (x_start
= 0; col_start
< cols
; col_start
++)
1712 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1713 if (col
->IsHidden())
1714 continue; // skip it!
1716 unsigned int w
= col
->GetWidth();
1717 if (x_start
+w
>= (unsigned int)update
.x
)
1723 unsigned int col_last
= col_start
;
1724 unsigned int x_last
= x_start
;
1725 for (; col_last
< cols
; col_last
++)
1727 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1728 if (col
->IsHidden())
1729 continue; // skip it!
1731 if (x_last
> (unsigned int)update
.GetRight())
1734 x_last
+= col
->GetWidth();
1737 // Draw horizontal rules if required
1738 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1740 dc
.SetPen(m_penRule
);
1741 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1743 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1745 int y
= GetLineStart( i
);
1746 dc
.DrawLine(x_start
, y
, x_last
, y
);
1750 // Draw vertical rules if required
1751 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1753 dc
.SetPen(m_penRule
);
1754 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1756 // NB: Vertical rules are drawn in the last pixel of a column so that
1757 // they align perfectly with native MSW wxHeaderCtrl as well as for
1758 // consistency with MSW native list control. There's no vertical
1759 // rule at the most-left side of the control.
1761 int x
= x_start
- 1;
1762 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1764 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1765 if (col
->IsHidden())
1766 continue; // skip it
1768 x
+= col
->GetWidth();
1770 dc
.DrawLine(x
, GetLineStart( item_start
),
1771 x
, GetLineStart( item_last
) );
1775 // redraw the background for the items which are selected/current
1776 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1778 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1779 if (selected
|| item
== m_currentRow
)
1781 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1782 if (item
== m_currentRow
)
1783 flags
|= wxCONTROL_CURRENT
;
1785 flags
|= wxCONTROL_FOCUSED
;
1787 wxRect
rect( x_start
, GetLineStart( item
),
1788 x_last
- x_start
, GetLineHeight( item
) );
1789 wxRendererNative::Get().DrawItemSelectionRect
1799 #if wxUSE_DRAG_AND_DROP
1802 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1803 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1804 dc
.SetPen( *wxBLACK_PEN
);
1805 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1806 dc
.DrawRectangle( rect
);
1808 #endif // wxUSE_DRAG_AND_DROP
1810 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1813 // TODO-RTL: last column for RTL support
1814 expander
= GetOwner()->GetColumnAt( 0 );
1815 GetOwner()->SetExpanderColumn(expander
);
1818 // redraw all cells for all rows which must be repainted and all columns
1820 cell_rect
.x
= x_start
;
1821 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1823 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1824 wxDataViewRenderer
*cell
= col
->GetRenderer();
1825 cell_rect
.width
= col
->GetWidth();
1827 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1828 continue; // skip it!
1830 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1832 // get the cell value and set it into the renderer
1833 wxDataViewTreeNode
*node
= NULL
;
1834 wxDataViewItem dataitem
;
1836 if (!IsVirtualList())
1838 node
= GetTreeNodeByRow(item
);
1842 dataitem
= node
->GetItem();
1844 // Skip all columns of "container" rows except the expander
1845 // column itself unless HasContainerColumns() overrides this.
1846 if ( col
!= GetOwner()->GetExpanderColumn() &&
1847 model
->IsContainer(dataitem
) &&
1848 !model
->HasContainerColumns(dataitem
) )
1853 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1856 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1859 cell_rect
.y
= GetLineStart( item
);
1860 cell_rect
.height
= GetLineHeight( item
);
1862 // deal with the expander
1864 if ((!IsList()) && (col
== expander
))
1866 // Calculate the indent first
1867 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1869 // we reserve m_lineHeight of horizontal space for the expander
1870 // but leave EXPANDER_MARGIN around the expander itself
1871 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1873 indent
+= m_lineHeight
;
1875 // draw expander if needed and visible
1876 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1878 dc
.SetPen( m_penExpander
);
1879 dc
.SetBrush( wxNullBrush
);
1881 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1882 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1883 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1885 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1888 if ( m_underMouse
== node
)
1889 flag
|= wxCONTROL_CURRENT
;
1890 if ( node
->IsOpen() )
1891 flag
|= wxCONTROL_EXPANDED
;
1893 // ensure that we don't overflow the cell (which might
1894 // happen if the column is very narrow)
1895 wxDCClipper
clip(dc
, cell_rect
);
1897 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1900 // force the expander column to left-center align
1901 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1904 wxRect item_rect
= cell_rect
;
1905 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1907 // account for the tree indent (harmless if we're not indented)
1908 item_rect
.x
+= indent
;
1909 item_rect
.width
-= indent
;
1911 if ( item_rect
.width
<= 0 )
1915 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1916 state
|= wxDATAVIEW_CELL_SELECTED
;
1918 // TODO: it would be much more efficient to create a clipping
1919 // region for the entire column being rendered (in the OnPaint
1920 // of wxDataViewMainWindow) instead of a single clip region for
1921 // each cell. However it would mean that each renderer should
1922 // respect the given wxRect's top & bottom coords, eventually
1923 // violating only the left & right coords - however the user can
1924 // make its own renderer and thus we cannot be sure of that.
1925 wxDCClipper
clip(dc
, item_rect
);
1927 cell
->WXCallRender(item_rect
, &dc
, state
);
1930 cell_rect
.x
+= cell_rect
.width
;
1934 void wxDataViewMainWindow::OnRenameTimer()
1936 // We have to call this here because changes may just have
1937 // been made and no screen update taken place.
1940 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1941 // (needs to be tested!)
1945 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1947 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1949 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1952 //-----------------------------------------------------------------------------
1953 // Helper class for do operation on the tree node
1954 //-----------------------------------------------------------------------------
1959 virtual ~DoJob() { }
1961 // The return value control how the tree-walker tranverse the tree
1964 DONE
, // Job done, stop traversing and return
1965 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
1966 CONTINUE
// Job not done, continue
1969 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1972 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1974 wxCHECK_MSG( node
, false, "can't walk NULL node" );
1976 switch( func( node
) )
1980 case DoJob::SKIP_SUBTREE
:
1982 case DoJob::CONTINUE
:
1986 if ( node
->HasChildren() )
1988 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
1990 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
1994 if ( Walker(*i
, func
) )
2002 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2004 if (IsVirtualList())
2006 wxDataViewVirtualListModel
*list_model
=
2007 (wxDataViewVirtualListModel
*) GetModel();
2008 m_count
= list_model
->GetCount();
2014 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2019 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2020 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2022 parentNode
->SetHasChildren(true);
2023 parentNode
->AddChild(itemNode
);
2024 parentNode
->ChangeSubTreeCount(+1);
2029 GetOwner()->UpdateColBestWidths();
2035 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2036 const wxDataViewItem
& item
)
2038 if (IsVirtualList())
2040 wxDataViewVirtualListModel
*list_model
=
2041 (wxDataViewVirtualListModel
*) GetModel();
2042 m_count
= list_model
->GetCount();
2044 if ( !m_selection
.empty() )
2046 const int row
= GetRowByItem(item
);
2048 int rowIndexInSelection
= wxNOT_FOUND
;
2050 const size_t selCount
= m_selection
.size();
2051 for ( size_t i
= 0; i
< selCount
; i
++ )
2053 if ( m_selection
[i
] == (unsigned)row
)
2054 rowIndexInSelection
= i
;
2055 else if ( m_selection
[i
] > (unsigned)row
)
2059 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2060 m_selection
.RemoveAt(rowIndexInSelection
);
2064 else // general case
2066 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2068 // Notice that it is possible that the item being deleted is not in the
2069 // tree at all, for example we could be deleting a never shown (because
2070 // collapsed) item in a tree model. So it's not an error if we don't know
2071 // about this item, just return without doing anything then.
2075 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2076 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2078 // We can't use FindNode() to find 'item', because it was already
2079 // removed from the model by the time ItemDeleted() is called, so we
2080 // have to do it manually. We keep track of its position as well for
2082 int itemPosInNode
= 0;
2083 wxDataViewTreeNode
*itemNode
= NULL
;
2084 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2085 i
!= parentsChildren
.end();
2086 ++i
, ++itemPosInNode
)
2088 if( (*i
)->GetItem() == item
)
2095 // If the parent wasn't expanded, it's possible that we didn't have a
2096 // node corresponding to 'item' and so there's nothing left to do.
2099 // If this was the last child to be removed, it's possible the parent
2100 // node became a leaf. Let's ask the model about it.
2101 if ( parentNode
->GetChildNodes().empty() )
2102 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2107 // Delete the item from wxDataViewTreeNode representation:
2108 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2110 parentNode
->RemoveChild(itemNode
);
2112 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2114 // Make the row number invalid and get a new valid one when user call GetRowCount
2117 // If this was the last child to be removed, it's possible the parent
2118 // node became a leaf. Let's ask the model about it.
2119 if ( parentNode
->GetChildNodes().empty() )
2120 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2122 // Update selection by removing 'item' and its entire children tree from the selection.
2123 if ( !m_selection
.empty() )
2125 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2126 // the parent ('parentNode') and position in its list of children
2128 if ( itemPosInNode
== 0 )
2130 // 1st child, row number is that of the parent parentNode + 1
2131 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2135 // row number is that of the sibling above 'item' + its subtree if any + 1
2136 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2138 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2139 siblingNode
->GetSubTreeCount() +
2143 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2145 const size_t numSelections
= m_selection
.size();
2146 for ( size_t i
= 0; i
< numSelections
; ++i
)
2148 const int s
= m_selection
[i
];
2150 newsel
.push_back(s
);
2151 else if ( s
>= itemRow
+ itemsDeleted
)
2152 newsel
.push_back(s
- itemsDeleted
);
2153 // else: deleted item, remove from selection
2156 m_selection
= newsel
;
2160 // Change the current row to the last row if the current exceed the max row number
2161 if( m_currentRow
> GetRowCount() )
2162 ChangeCurrentRow(m_count
- 1);
2164 GetOwner()->UpdateColBestWidths();
2170 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2175 GetOwner()->UpdateColBestWidths();
2178 wxWindow
*parent
= GetParent();
2179 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2180 le
.SetEventObject(parent
);
2181 le
.SetModel(GetModel());
2183 parent
->GetEventHandler()->ProcessEvent(le
);
2188 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2190 int view_column
= -1;
2191 unsigned int n_col
= m_owner
->GetColumnCount();
2192 for (unsigned i
= 0; i
< n_col
; i
++)
2194 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2195 if (column
->GetModelColumn() == model_column
)
2197 view_column
= (int) i
;
2201 if (view_column
== -1)
2204 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2205 /*#define MAX_VIRTUAL_WIDTH 100000
2207 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2208 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2209 Refresh( true, &rect );
2216 GetOwner()->UpdateColBestWidth(view_column
);
2219 wxWindow
*parent
= GetParent();
2220 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2221 le
.SetEventObject(parent
);
2222 le
.SetModel(GetModel());
2224 le
.SetColumn(view_column
);
2225 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2226 parent
->GetEventHandler()->ProcessEvent(le
);
2231 bool wxDataViewMainWindow::Cleared()
2234 m_selection
.Clear();
2237 BuildTree( GetModel() );
2239 GetOwner()->UpdateColBestWidths();
2245 void wxDataViewMainWindow::UpdateDisplay()
2248 m_underMouse
= NULL
;
2251 void wxDataViewMainWindow::OnInternalIdle()
2253 wxWindow::OnInternalIdle();
2257 RecalculateDisplay();
2262 void wxDataViewMainWindow::RecalculateDisplay()
2264 wxDataViewModel
*model
= GetModel();
2271 int width
= GetEndOfLastCol();
2272 int height
= GetLineStart( GetRowCount() );
2274 SetVirtualSize( width
, height
);
2275 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2280 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2282 m_underMouse
= NULL
;
2284 wxWindow::ScrollWindow( dx
, dy
, rect
);
2286 if (GetOwner()->m_headerArea
)
2287 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2290 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2292 m_underMouse
= NULL
;
2295 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2296 int sy
= GetLineStart( rows
)/y
;
2300 wxRect rect
= GetClientRect();
2304 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2305 for (x_start
= 0; colnum
< column
; colnum
++)
2307 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2308 if (col
->IsHidden())
2309 continue; // skip it!
2311 w
= col
->GetWidth();
2315 int x_end
= x_start
+ w
;
2316 xe
= xx
+ rect
.width
;
2319 sx
= ( xx
+ x_end
- xe
)/x
;
2326 m_owner
->Scroll( sx
, sy
);
2329 int wxDataViewMainWindow::GetCountPerPage() const
2331 wxSize size
= GetClientSize();
2332 return size
.y
/ m_lineHeight
;
2335 int wxDataViewMainWindow::GetEndOfLastCol() const
2339 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2341 const wxDataViewColumn
*c
=
2342 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2345 width
+= c
->GetWidth();
2350 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2354 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2356 return GetLineAt( y
);
2359 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2361 wxSize client_size
= GetClientSize();
2362 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2363 &client_size
.x
, &client_size
.y
);
2365 // we should deal with the pixel here
2366 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2368 return wxMin( GetRowCount()-1, row
);
2371 unsigned int wxDataViewMainWindow::GetRowCount()
2373 if ( m_count
== -1 )
2375 m_count
= RecalculateCount();
2381 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2388 void wxDataViewMainWindow::SelectAllRows( bool on
)
2395 m_selection
.Clear();
2396 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2397 m_selection
.Add( i
);
2402 unsigned int first_visible
= GetFirstVisibleRow();
2403 unsigned int last_visible
= GetLastVisibleRow();
2405 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2407 unsigned int row
= m_selection
[i
];
2408 if ((row
>= first_visible
) && (row
<= last_visible
))
2411 m_selection
.Clear();
2415 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2417 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2421 m_selection
.Add( row
);
2429 m_selection
.Remove( row
);
2435 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2439 unsigned int tmp
= from
;
2445 for (i
= from
; i
<= to
; i
++)
2447 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2450 m_selection
.Add( i
);
2455 m_selection
.Remove( i
);
2458 RefreshRows( from
, to
);
2461 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2463 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2465 int n
= aSelections
[i
];
2467 m_selection
.Add( n
);
2472 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2474 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2475 m_selection
.Add( row
);
2477 m_selection
.Remove( row
);
2481 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2483 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2486 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2488 wxWindow
*parent
= GetParent();
2489 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2491 le
.SetEventObject(parent
);
2492 le
.SetModel(GetModel());
2495 parent
->GetEventHandler()->ProcessEvent(le
);
2498 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2500 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2501 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2503 wxSize client_size
= GetClientSize();
2504 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2505 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2506 if (intersect_rect
.width
> 0)
2507 Refresh( true, &intersect_rect
);
2510 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2514 unsigned int tmp
= to
;
2519 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2520 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2522 wxSize client_size
= GetClientSize();
2523 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2524 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2525 if (intersect_rect
.width
> 0)
2526 Refresh( true, &intersect_rect
);
2529 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2531 wxSize client_size
= GetClientSize();
2532 int start
= GetLineStart( firstRow
);
2533 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2534 if (start
> client_size
.y
) return;
2536 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2538 Refresh( true, &rect
);
2541 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2543 wxCHECK_RET( newCurrent
< GetRowCount(),
2544 wxT("invalid item index in OnArrowChar()") );
2546 // if there is no selection, we cannot move it anywhere
2547 if (!HasCurrentRow())
2550 unsigned int oldCurrent
= m_currentRow
;
2552 // in single selection we just ignore Shift as we can't select several
2554 if ( event
.ShiftDown() && !IsSingleSel() )
2556 RefreshRow( oldCurrent
);
2558 ChangeCurrentRow( newCurrent
);
2560 // select all the items between the old and the new one
2561 if ( oldCurrent
> newCurrent
)
2563 newCurrent
= oldCurrent
;
2564 oldCurrent
= m_currentRow
;
2567 SelectRows( oldCurrent
, newCurrent
, true );
2568 if (oldCurrent
!=newCurrent
)
2569 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2573 RefreshRow( oldCurrent
);
2575 // all previously selected items are unselected unless ctrl is held
2576 if ( !event
.ControlDown() )
2577 SelectAllRows(false);
2579 ChangeCurrentRow( newCurrent
);
2581 if ( !event
.ControlDown() )
2583 SelectRow( m_currentRow
, true );
2584 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2587 RefreshRow( m_currentRow
);
2590 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2593 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2597 rect
.y
= GetLineStart( row
);
2598 rect
.width
= GetEndOfLastCol();
2599 rect
.height
= GetLineHeight( row
);
2604 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2606 const wxDataViewModel
*model
= GetModel();
2608 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2610 // TODO make more efficient
2615 for (r
= 0; r
< row
; r
++)
2617 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2618 if (!node
) return start
;
2620 wxDataViewItem item
= node
->GetItem();
2622 unsigned int cols
= GetOwner()->GetColumnCount();
2624 int height
= m_lineHeight
;
2625 for (col
= 0; col
< cols
; col
++)
2627 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2628 if (column
->IsHidden())
2629 continue; // skip it!
2632 model
->IsContainer(item
) &&
2633 !model
->HasContainerColumns(item
))
2634 continue; // skip it!
2636 wxDataViewRenderer
*renderer
=
2637 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2638 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2640 height
= wxMax( height
, renderer
->GetSize().y
);
2650 return row
* m_lineHeight
;
2654 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2656 const wxDataViewModel
*model
= GetModel();
2658 // check for the easy case first
2659 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2660 return y
/ m_lineHeight
;
2662 // TODO make more efficient
2663 unsigned int row
= 0;
2664 unsigned int yy
= 0;
2667 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2670 // not really correct...
2671 return row
+ ((y
-yy
) / m_lineHeight
);
2674 wxDataViewItem item
= node
->GetItem();
2676 unsigned int cols
= GetOwner()->GetColumnCount();
2678 int height
= m_lineHeight
;
2679 for (col
= 0; col
< cols
; col
++)
2681 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2682 if (column
->IsHidden())
2683 continue; // skip it!
2686 model
->IsContainer(item
) &&
2687 !model
->HasContainerColumns(item
))
2688 continue; // skip it!
2690 wxDataViewRenderer
*renderer
=
2691 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2692 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2694 height
= wxMax( height
, renderer
->GetSize().y
);
2705 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2707 const wxDataViewModel
*model
= GetModel();
2709 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2711 wxASSERT( !IsVirtualList() );
2713 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2714 // wxASSERT( node );
2715 if (!node
) return m_lineHeight
;
2717 wxDataViewItem item
= node
->GetItem();
2719 int height
= m_lineHeight
;
2721 unsigned int cols
= GetOwner()->GetColumnCount();
2723 for (col
= 0; col
< cols
; col
++)
2725 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2726 if (column
->IsHidden())
2727 continue; // skip it!
2730 model
->IsContainer(item
) &&
2731 !model
->HasContainerColumns(item
))
2732 continue; // skip it!
2734 wxDataViewRenderer
*renderer
=
2735 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2736 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2738 height
= wxMax( height
, renderer
->GetSize().y
);
2745 return m_lineHeight
;
2750 class RowToTreeNodeJob
: public DoJob
2753 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2756 this->current
= current
;
2761 virtual int operator() ( wxDataViewTreeNode
* node
)
2764 if( current
== static_cast<int>(row
))
2770 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2772 current
+= node
->GetSubTreeCount();
2773 return DoJob::SKIP_SUBTREE
;
2779 // If the current node has only leaf children, we can find the
2780 // desired node directly. This can speed up finding the node
2781 // in some cases, and will have a very good effect for list views.
2782 if ( node
->HasChildren() &&
2783 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2785 const int index
= static_cast<int>(row
) - current
- 1;
2786 ret
= node
->GetChildNodes()[index
];
2790 return DoJob::CONTINUE
;
2794 wxDataViewTreeNode
* GetResult() const
2800 wxDataViewTreeNode
* ret
;
2801 wxDataViewTreeNode
* parent
;
2804 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2806 wxASSERT( !IsVirtualList() );
2808 RowToTreeNodeJob
job( row
, -2, m_root
);
2809 Walker( m_root
, job
);
2810 return job
.GetResult();
2813 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2815 if (IsVirtualList())
2817 return wxDataViewItem( wxUIntToPtr(row
+1) );
2821 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2822 return node
? node
->GetItem() : wxDataViewItem();
2827 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2828 const wxDataViewItem
& item
)
2830 wxWindow
*parent
= GetParent();
2831 wxDataViewEvent
le(type
, parent
->GetId());
2833 le
.SetEventObject(parent
);
2834 le
.SetModel(GetModel());
2837 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2840 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2845 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2849 if (!node
->HasChildren())
2852 return node
->IsOpen();
2855 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2860 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2864 if (!node
->HasChildren())
2870 void wxDataViewMainWindow::Expand( unsigned int row
)
2875 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2879 if (!node
->HasChildren())
2882 if (!node
->IsOpen())
2884 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
2886 // Vetoed by the event handler.
2892 // build the children of current node
2893 if( node
->GetChildNodes().empty() )
2896 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
2899 // By expanding the node all row indices that are currently in the selection list
2900 // and are greater than our node have become invalid. So we have to correct that now.
2901 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2902 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2904 const unsigned testRow
= m_selection
[i
];
2905 // all rows above us are not affected, so skip them
2909 m_selection
[i
] += rowAdjustment
;
2912 if(m_currentRow
> row
)
2913 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2917 // Send the expanded event
2918 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2922 void wxDataViewMainWindow::Collapse(unsigned int row
)
2927 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2931 if (!node
->HasChildren())
2936 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
2938 // Vetoed by the event handler.
2942 // Find out if there are selected items below the current node.
2943 bool selectCollapsingRow
= false;
2944 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2945 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2946 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2948 const unsigned testRow
= m_selection
[i
];
2949 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2951 selectCollapsingRow
= true;
2952 // get out as soon as we have found a node that is selected
2959 // If the node to be closed has selected items the user won't see those any longer.
2960 // We select the collapsing node in this case.
2961 if(selectCollapsingRow
)
2963 SelectAllRows(false);
2964 ChangeCurrentRow(row
);
2965 SelectRow(row
, true);
2966 SendSelectionChangedEvent(GetItemByRow(row
));
2970 // if there were no selected items below our node we still need to "fix" the
2971 // selection list to adjust for the changing of the row indices.
2972 // We actually do the opposite of what we are doing in Expand().
2973 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2975 const unsigned testRow
= m_selection
[i
];
2976 // all rows above us are not affected, so skip them
2980 m_selection
[i
] -= rowAdjustment
;
2983 // if the "current row" is being collapsed away we change it to the current row ;-)
2984 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2985 ChangeCurrentRow(row
);
2986 else if(m_currentRow
> row
)
2987 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2992 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2996 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2998 const wxDataViewModel
* model
= GetModel();
3005 // Compose the parent-chain for the item we are looking for
3006 wxVector
<wxDataViewItem
> parentChain
;
3007 wxDataViewItem
it( item
);
3010 parentChain
.push_back(it
);
3011 it
= model
->GetParent(it
);
3014 // Find the item along the parent-chain.
3015 // This algorithm is designed to speed up the node-finding method
3016 wxDataViewTreeNode
* node
= m_root
;
3017 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3019 if( node
->HasChildren() )
3021 if( node
->GetChildNodes().empty() )
3023 // Even though the item is a container, it doesn't have any
3024 // child nodes in the control's representation yet. We have
3025 // to realize its subtree now.
3027 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3030 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3033 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3035 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3036 if (currentNode
->GetItem() == parentChain
[iter
])
3038 if (currentNode
->GetItem() == item
)
3058 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3059 wxDataViewColumn
* &column
)
3061 wxDataViewColumn
*col
= NULL
;
3062 unsigned int cols
= GetOwner()->GetColumnCount();
3063 unsigned int colnum
= 0;
3065 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3066 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3068 col
= GetOwner()->GetColumnAt(colnum
);
3069 if (col
->IsHidden())
3070 continue; // skip it!
3072 unsigned int w
= col
->GetWidth();
3073 if (x_start
+w
>= (unsigned int)x
)
3080 item
= GetItemByRow( GetLineAt( y
) );
3083 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3084 const wxDataViewColumn
* column
)
3089 unsigned int cols
= GetOwner()->GetColumnCount();
3090 // If column is null the loop will compute the combined width of all columns.
3091 // Otherwise, it will compute the x position of the column we are looking for.
3092 for (unsigned int i
= 0; i
< cols
; i
++)
3094 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3099 if (col
->IsHidden())
3100 continue; // skip it!
3102 xpos
+= col
->GetWidth();
3103 width
+= col
->GetWidth();
3108 // If we have a column, we need can get its width directly.
3109 if(column
->IsHidden())
3112 width
= column
->GetWidth();
3117 // If we have no column, we reset the x position back to zero.
3121 // we have to take an expander column into account and compute its indentation
3122 // to get the correct x position where the actual text is
3124 int row
= GetRowByItem(item
);
3125 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3127 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3128 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3129 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3132 wxRect
itemRect( xpos
+ indent
,
3133 GetLineStart( row
),
3135 GetLineHeight( row
) );
3137 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3138 &itemRect
.x
, &itemRect
.y
);
3143 int wxDataViewMainWindow::RecalculateCount()
3145 if (IsVirtualList())
3147 wxDataViewVirtualListModel
*list_model
=
3148 (wxDataViewVirtualListModel
*) GetModel();
3150 return list_model
->GetCount();
3154 return m_root
->GetSubTreeCount();
3158 class ItemToRowJob
: public DoJob
3161 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3168 // Maybe binary search will help to speed up this process
3169 virtual int operator() ( wxDataViewTreeNode
* node
)
3172 if( node
->GetItem() == item
)
3177 if( node
->GetItem() == *m_iter
)
3180 return DoJob::CONTINUE
;
3184 ret
+= node
->GetSubTreeCount();
3185 return DoJob::SKIP_SUBTREE
;
3190 // the row number is begin from zero
3191 int GetResult() const
3195 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3196 wxDataViewItem item
;
3201 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3203 const wxDataViewModel
* model
= GetModel();
3207 if (IsVirtualList())
3209 return wxPtrToUInt( item
.GetID() ) -1;
3216 // Compose the parent-chain of the item we are looking for
3217 wxVector
<wxDataViewItem
> parentChain
;
3218 wxDataViewItem
it( item
);
3221 parentChain
.push_back(it
);
3222 it
= model
->GetParent(it
);
3225 // add an 'invalid' item to represent our 'invisible' root node
3226 parentChain
.push_back(wxDataViewItem());
3228 // the parent chain was created by adding the deepest parent first.
3229 // so if we want to start at the root node, we have to iterate backwards through the vector
3230 ItemToRowJob
job( item
, parentChain
.rbegin() );
3231 Walker( m_root
, job
);
3232 return job
.GetResult();
3236 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3237 wxDataViewTreeNode
* node
)
3239 if( !model
->IsContainer( item
) )
3242 wxDataViewItemArray children
;
3243 unsigned int num
= model
->GetChildren( item
, children
);
3245 for ( unsigned int index
= 0; index
< num
; index
++ )
3247 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3249 if( model
->IsContainer(children
[index
]) )
3250 n
->SetHasChildren( true );
3255 wxASSERT( node
->IsOpen() );
3256 node
->ChangeSubTreeCount(+num
);
3259 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3263 if (GetModel()->IsVirtualListModel())
3269 m_root
= wxDataViewTreeNode::CreateRootNode();
3271 // First we define a invalid item to fetch the top-level elements
3272 wxDataViewItem item
;
3274 BuildTreeHelper( model
, item
, m_root
);
3278 void wxDataViewMainWindow::DestroyTree()
3280 if (!IsVirtualList())
3287 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3289 wxWindow
* const parent
= GetParent();
3291 // propagate the char event upwards
3292 wxKeyEvent
eventForParent(event
);
3293 eventForParent
.SetEventObject(parent
);
3294 if ( parent
->ProcessWindowEvent(eventForParent
) )
3297 if ( parent
->HandleAsNavigationKey(event
) )
3300 // no item -> nothing to do
3301 if (!HasCurrentRow())
3307 // don't use m_linesPerPage directly as it might not be computed yet
3308 const int pageSize
= GetCountPerPage();
3309 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3311 switch ( event
.GetKeyCode() )
3316 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3318 le
.SetItem( GetItemByRow(m_currentRow
) );
3319 le
.SetEventObject(parent
);
3320 le
.SetModel(GetModel());
3322 parent
->GetEventHandler()->ProcessEvent(le
);
3327 if ( m_currentRow
> 0 )
3328 OnArrowChar( m_currentRow
- 1, event
);
3332 if ( m_currentRow
+ 1 < GetRowCount() )
3333 OnArrowChar( m_currentRow
+ 1, event
);
3335 // Add the process for tree expanding/collapsing
3341 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3345 if (node
->HasChildren() && node
->IsOpen())
3347 Collapse(m_currentRow
);
3349 else // if the node is already closed we move the selection to its parent
3351 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3355 int parent
= GetRowByItem( parent_node
->GetItem() );
3358 unsigned int row
= m_currentRow
;
3359 SelectRow( row
, false);
3360 SelectRow( parent
, true );
3361 ChangeCurrentRow( parent
);
3362 GetOwner()->EnsureVisible( parent
, -1 );
3363 SendSelectionChangedEvent( parent_node
->GetItem() );
3371 if (!IsExpanded( m_currentRow
))
3372 Expand( m_currentRow
);
3375 unsigned int row
= m_currentRow
;
3376 SelectRow( row
, false );
3377 SelectRow( row
+ 1, true );
3378 ChangeCurrentRow( row
+ 1 );
3379 GetOwner()->EnsureVisible( row
+ 1, -1 );
3380 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3387 OnArrowChar( GetRowCount() - 1, event
);
3392 OnArrowChar( 0, event
);
3397 int steps
= pageSize
- 1;
3398 int index
= m_currentRow
- steps
;
3402 OnArrowChar( index
, event
);
3408 int steps
= pageSize
- 1;
3409 unsigned int index
= m_currentRow
+ steps
;
3410 unsigned int count
= GetRowCount();
3411 if ( index
>= count
)
3414 OnArrowChar( index
, event
);
3420 if(m_selection
.size() == 1)
3422 // TODO: we need to revise that when we have a concept for a 'current column'
3423 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3433 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3435 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3437 // let the base handle mouse wheel events.
3442 // set the focus to ourself if any of the mouse buttons are pressed
3443 if(event
.ButtonDown() && !HasFocus())
3446 int x
= event
.GetX();
3447 int y
= event
.GetY();
3448 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3449 wxDataViewColumn
*col
= NULL
;
3452 unsigned int cols
= GetOwner()->GetColumnCount();
3454 for (i
= 0; i
< cols
; i
++)
3456 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3458 continue; // skip it!
3460 if (x
< xpos
+ c
->GetWidth())
3465 xpos
+= c
->GetWidth();
3473 wxDataViewRenderer
*cell
= col
->GetRenderer();
3474 unsigned int current
= GetLineAt( y
);
3475 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3477 // Unselect all if below the last row ?
3482 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3483 // button) and also determine the offset of the real cell start, skipping
3484 // the indentation and the expander itself.
3485 bool hoverOverExpander
= false;
3486 int expanderOffset
= 0;
3487 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3489 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3490 if( node
!=NULL
&& node
->HasChildren() )
3492 int indent
= node
->GetIndentLevel();
3493 indent
= GetOwner()->GetIndent()*indent
;
3495 // we make the rectangle we are looking in a bit bigger than the actual
3496 // visual expander so the user can hit that little thing reliably
3497 wxRect
rect( xpos
+ indent
,
3498 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3499 m_lineHeight
, m_lineHeight
);
3501 expanderOffset
= indent
+ m_lineHeight
;
3503 if( rect
.Contains(x
, y
) )
3505 // So the mouse is over the expander
3506 hoverOverExpander
= true;
3507 if (m_underMouse
&& m_underMouse
!= node
)
3509 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3510 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3512 if (m_underMouse
!= node
)
3514 // wxLogMessage("Do the row: %d", current);
3515 RefreshRow(current
);
3517 m_underMouse
= node
;
3521 if (!hoverOverExpander
)
3523 if (m_underMouse
!= NULL
)
3525 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3526 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3527 m_underMouse
= NULL
;
3531 wxDataViewModel
*model
= GetModel();
3533 #if wxUSE_DRAG_AND_DROP
3534 if (event
.Dragging())
3536 if (m_dragCount
== 0)
3538 // we have to report the raw, physical coords as we want to be
3539 // able to call HitTest(event.m_pointDrag) from the user code to
3540 // get the item being dragged
3541 m_dragStart
= event
.GetPosition();
3546 if (m_dragCount
!= 3)
3549 if (event
.LeftIsDown())
3551 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3552 &m_dragStart
.x
, &m_dragStart
.y
);
3553 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3554 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3556 // Notify cell about drag
3557 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3558 event
.SetEventObject( m_owner
);
3559 event
.SetItem( item
);
3560 event
.SetModel( model
);
3561 if (!m_owner
->HandleWindowEvent( event
))
3564 if (!event
.IsAllowed())
3567 wxDataObject
*obj
= event
.GetDataObject();
3571 wxDataViewDropSource
drag( this, drag_item_row
);
3572 drag
.SetData( *obj
);
3573 /* wxDragResult res = */ drag
.DoDragDrop();
3582 #endif // wxUSE_DRAG_AND_DROP
3584 bool simulateClick
= false;
3586 if (event
.ButtonDClick())
3588 m_renameTimer
->Stop();
3589 m_lastOnSame
= false;
3592 wxDataViewItem item
= GetItemByRow(current
);
3593 bool ignore_other_columns
=
3594 ((GetOwner()->GetExpanderColumn() != col
) &&
3595 (model
->IsContainer(item
)) &&
3596 (!model
->HasContainerColumns(item
)));
3598 if (event
.LeftDClick())
3600 if(hoverOverExpander
)
3602 // a double click on the expander will be converted into a "simulated" normal click
3603 simulateClick
= true;
3605 else if ( current
== m_lineLastClicked
)
3607 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3609 const unsigned colIdx
= col
->GetModelColumn();
3611 cell
->PrepareForItem(model
, item
, colIdx
);
3613 wxRect
cell_rect( xpos
, GetLineStart( current
),
3614 col
->GetWidth(), GetLineHeight( current
) );
3615 cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3619 wxWindow
*parent
= GetParent();
3620 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3622 le
.SetColumn( col
->GetModelColumn() );
3623 le
.SetDataViewColumn( col
);
3624 le
.SetEventObject(parent
);
3625 le
.SetModel(GetModel());
3627 parent
->GetEventHandler()->ProcessEvent(le
);
3633 // The first click was on another item, so don't interpret this as
3634 // a double click, but as a simple click instead
3635 simulateClick
= true;
3639 if (event
.LeftUp() && !hoverOverExpander
)
3641 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3643 // select single line
3644 SelectAllRows( false );
3645 SelectRow( m_lineSelectSingleOnUp
, true );
3646 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3649 // If the user click the expander, we do not do editing even if the column
3650 // with expander are editable
3651 if (m_lastOnSame
&& !ignore_other_columns
)
3653 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3654 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3656 m_renameTimer
->Start( 100, true );
3660 m_lastOnSame
= false;
3661 m_lineSelectSingleOnUp
= (unsigned int)-1;
3663 else if(!event
.LeftUp())
3665 // This is necessary, because after a DnD operation in
3666 // from and to ourself, the up event is swallowed by the
3667 // DnD code. So on next non-up event (which means here and
3668 // now) m_lineSelectSingleOnUp should be reset.
3669 m_lineSelectSingleOnUp
= (unsigned int)-1;
3672 if (event
.RightDown())
3674 m_lineBeforeLastClicked
= m_lineLastClicked
;
3675 m_lineLastClicked
= current
;
3677 // If the item is already selected, do not update the selection.
3678 // Multi-selections should not be cleared if a selected item is clicked.
3679 if (!IsRowSelected(current
))
3681 SelectAllRows(false);
3682 const unsigned oldCurrent
= m_currentRow
;
3683 ChangeCurrentRow(current
);
3684 SelectRow(m_currentRow
,true);
3685 RefreshRow(oldCurrent
);
3686 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3689 else if (event
.RightUp())
3692 model
->GetValue( value
, item
, col
->GetModelColumn() );
3693 wxWindow
*parent
= GetParent();
3694 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3696 le
.SetColumn( col
->GetModelColumn() );
3697 le
.SetDataViewColumn( col
);
3698 le
.SetEventObject(parent
);
3699 le
.SetModel(GetModel());
3701 parent
->GetEventHandler()->ProcessEvent(le
);
3703 else if (event
.MiddleDown())
3707 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3709 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3711 // hoverOverExpander being true tells us that our node must be
3712 // valid and have children.
3713 // So we don't need any extra checks.
3714 if( node
->IsOpen() )
3719 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3721 m_lineBeforeLastClicked
= m_lineLastClicked
;
3722 m_lineLastClicked
= current
;
3724 unsigned int oldCurrentRow
= m_currentRow
;
3725 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3727 bool cmdModifierDown
= event
.CmdDown();
3728 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3730 if ( IsSingleSel() || !IsRowSelected(current
) )
3732 SelectAllRows( false );
3733 ChangeCurrentRow(current
);
3734 SelectRow(m_currentRow
,true);
3735 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3737 else // multi sel & current is highlighted & no mod keys
3739 m_lineSelectSingleOnUp
= current
;
3740 ChangeCurrentRow(current
); // change focus
3743 else // multi sel & either ctrl or shift is down
3745 if (cmdModifierDown
)
3747 ChangeCurrentRow(current
);
3748 ReverseRowSelection(m_currentRow
);
3749 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3751 else if (event
.ShiftDown())
3753 ChangeCurrentRow(current
);
3755 unsigned int lineFrom
= oldCurrentRow
,
3758 if ( lineTo
< lineFrom
)
3761 lineFrom
= m_currentRow
;
3764 SelectRows(lineFrom
, lineTo
, true);
3765 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3767 else // !ctrl, !shift
3769 // test in the enclosing if should make it impossible
3770 wxFAIL_MSG( wxT("how did we get here?") );
3774 if (m_currentRow
!= oldCurrentRow
)
3775 RefreshRow( oldCurrentRow
);
3777 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3779 // Update selection here...
3782 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3783 (current
== oldCurrentRow
)) && oldWasSelected
;
3785 // Call LeftClick after everything else as under GTK+
3786 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3788 // notify cell about click
3789 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3791 wxRect
cell_rect( xpos
+ expanderOffset
,
3792 GetLineStart( current
),
3793 col
->GetWidth() - expanderOffset
,
3794 GetLineHeight( current
) );
3796 // Report position relative to the cell's custom area, i.e.
3797 // no the entire space as given by the control but the one
3798 // used by the renderer after calculation of alignment etc.
3800 // adjust the rectangle ourselves to account for the alignment
3801 wxRect rectItem
= cell_rect
;
3802 const int align
= cell
->GetAlignment();
3803 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3805 const wxSize size
= cell
->GetSize();
3807 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3809 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3810 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3811 else if ( align
& wxALIGN_RIGHT
)
3812 rectItem
.x
+= cell_rect
.width
- size
.x
;
3813 // else: wxALIGN_LEFT is the default
3816 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3818 if ( align
& wxALIGN_CENTER_VERTICAL
)
3819 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3820 else if ( align
& wxALIGN_BOTTOM
)
3821 rectItem
.y
+= cell_rect
.height
- size
.y
;
3822 // else: wxALIGN_TOP is the default
3826 wxPoint
pos( event
.GetPosition() );
3827 pos
.x
-= rectItem
.x
;
3828 pos
.y
-= rectItem
.y
;
3830 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3832 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3833 model
, item
, col
->GetModelColumn());
3838 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3842 if (HasCurrentRow())
3848 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3852 if (HasCurrentRow())
3858 //-----------------------------------------------------------------------------
3860 //-----------------------------------------------------------------------------
3862 WX_DEFINE_LIST(wxDataViewColumnList
)
3864 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3865 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3866 EVT_SIZE(wxDataViewCtrl::OnSize
)
3869 wxDataViewCtrl::~wxDataViewCtrl()
3872 GetModel()->RemoveNotifier( m_notifier
);
3875 m_colsBestWidths
.clear();
3878 void wxDataViewCtrl::Init()
3880 m_cols
.DeleteContents(true);
3883 // No sorting column at start
3884 m_sortingColumnIdx
= wxNOT_FOUND
;
3886 m_headerArea
= NULL
;
3889 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3894 const wxValidator
& validator
,
3895 const wxString
& name
)
3897 // if ( (style & wxBORDER_MASK) == 0)
3898 // style |= wxBORDER_SUNKEN;
3902 if (!wxControl::Create( parent
, id
, pos
, size
,
3903 style
| wxScrolledWindowStyle
, validator
, name
))
3906 SetInitialSize(size
);
3909 MacSetClipChildren( true );
3912 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3914 // We use the cursor keys for moving the selection, not scrolling, so call
3915 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3916 // keyboard events forwarded to us from wxListMainWindow.
3917 DisableKeyboardScrolling();
3919 if (HasFlag(wxDV_NO_HEADER
))
3920 m_headerArea
= NULL
;
3922 m_headerArea
= new wxDataViewHeaderWindow(this);
3924 SetTargetWindow( m_clientArea
);
3926 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3928 sizer
->Add( m_headerArea
, 0, wxGROW
);
3929 sizer
->Add( m_clientArea
, 1, wxGROW
);
3935 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3937 return wxBORDER_THEME
;
3941 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3945 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3948 // we need to process arrows ourselves for scrolling
3949 if ( nMsg
== WM_GETDLGCODE
)
3951 rc
|= DLGC_WANTARROWS
;
3959 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3961 wxSize newsize
= size
;
3962 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3963 newsize
.y
-= m_headerArea
->GetSize().y
;
3968 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3970 // We need to override OnSize so that our scrolled
3971 // window a) does call Layout() to use sizers for
3972 // positioning the controls but b) does not query
3973 // the sizer for their size and use that for setting
3974 // the scrollable area as set that ourselves by
3975 // calling SetScrollbar() further down.
3981 // We must redraw the headers if their height changed. Normally this
3982 // shouldn't happen as the control shouldn't let itself be resized beneath
3983 // its minimal height but avoid the display artefacts that appear if it
3984 // does happen, e.g. because there is really not enough vertical space.
3985 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
3986 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
3988 m_headerArea
->Refresh();
3992 void wxDataViewCtrl::SetFocus()
3995 m_clientArea
->SetFocus();
3998 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4000 if (!wxDataViewCtrlBase::AssociateModel( model
))
4003 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4005 model
->AddNotifier( m_notifier
);
4007 m_clientArea
->DestroyTree();
4009 m_clientArea
->BuildTree(model
);
4011 m_clientArea
->UpdateDisplay();
4016 #if wxUSE_DRAG_AND_DROP
4018 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4020 return m_clientArea
->EnableDragSource( format
);
4023 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4025 return m_clientArea
->EnableDropTarget( format
);
4028 #endif // wxUSE_DRAG_AND_DROP
4030 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4032 if (!wxDataViewCtrlBase::AppendColumn(col
))
4035 m_cols
.Append( col
);
4036 m_colsBestWidths
.push_back(0);
4037 OnColumnsCountChanged();
4041 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4043 if (!wxDataViewCtrlBase::PrependColumn(col
))
4046 m_cols
.Insert( col
);
4047 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4048 OnColumnsCountChanged();
4052 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4054 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4057 m_cols
.Insert( pos
, col
);
4058 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4059 OnColumnsCountChanged();
4063 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4066 m_headerArea
->UpdateColumn(idx
);
4068 m_clientArea
->UpdateDisplay();
4071 void wxDataViewCtrl::OnColumnsCountChanged()
4074 m_headerArea
->SetColumnCount(GetColumnCount());
4076 m_clientArea
->UpdateDisplay();
4079 void wxDataViewCtrl::DoSetExpanderColumn()
4081 m_clientArea
->UpdateDisplay();
4084 void wxDataViewCtrl::DoSetIndent()
4086 m_clientArea
->UpdateDisplay();
4089 unsigned int wxDataViewCtrl::GetColumnCount() const
4091 return m_cols
.GetCount();
4094 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4096 if ( !m_clientArea
)
4099 m_clientArea
->SetRowHeight(lineHeight
);
4104 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4109 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4111 // columns can't be reordered if there is no header window which allows
4113 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4116 return GetColumn(idx
);
4119 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4121 const unsigned count
= m_cols
.size();
4122 for ( unsigned n
= 0; n
< count
; n
++ )
4124 if ( m_cols
[n
] == column
)
4131 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4133 if ( m_colsBestWidths
[idx
] != 0 )
4134 return m_colsBestWidths
[idx
];
4136 const int count
= m_clientArea
->GetRowCount();
4137 wxDataViewColumn
*column
= GetColumn(idx
);
4138 wxDataViewRenderer
*renderer
=
4139 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4141 class MaxWidthCalculator
4144 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4145 wxDataViewRenderer
*renderer
,
4146 const wxDataViewModel
*model
,
4149 m_clientArea(clientArea
),
4150 m_renderer(renderer
),
4156 void UpdateWithWidth(int width
)
4158 m_width
= wxMax(m_width
, width
);
4161 void UpdateWithRow(int row
)
4163 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4164 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4165 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4168 int GetMaxWidth() const { return m_width
; }
4172 wxDataViewMainWindow
*m_clientArea
;
4173 wxDataViewRenderer
*m_renderer
;
4174 const wxDataViewModel
*m_model
;
4178 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4179 GetModel(), column
->GetModelColumn());
4183 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4184 // Labels on native MSW header are indented on both sides
4186 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4187 calculator
.UpdateWithWidth(header_width
);
4190 // The code below deserves some explanation. For very large controls, we
4191 // simply can't afford to calculate sizes for all items, it takes too
4192 // long. So the best we can do is to check the first and the last N/2
4193 // items in the control for some sufficiently large N and calculate best
4194 // sizes from that. That can result in the calculated best width being too
4195 // small for some outliers, but it's better to get slightly imperfect
4196 // result than to wait several seconds after every update. To avoid highly
4197 // visible miscalculations, we also include all currently visible items
4198 // no matter what. Finally, the value of N is determined dynamically by
4199 // measuring how much time we spent on the determining item widths so far.
4202 int top_part_end
= count
;
4203 static const long CALC_TIMEOUT
= 20/*ms*/;
4204 // don't call wxStopWatch::Time() too often
4205 static const unsigned CALC_CHECK_FREQ
= 100;
4208 // use some hard-coded limit, that's the best we can do without timer
4209 int top_part_end
= wxMin(500, count
);
4210 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4214 for ( row
= 0; row
< top_part_end
; row
++ )
4217 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4218 timer
.Time() > CALC_TIMEOUT
)
4220 #endif // wxUSE_STOPWATCH
4221 calculator
.UpdateWithRow(row
);
4224 // row is the first unmeasured item now; that's our value of N/2
4230 // add bottom N/2 items now:
4231 const int bottom_part_start
= wxMax(row
, count
- row
);
4232 for ( row
= bottom_part_start
; row
< count
; row
++ )
4234 calculator
.UpdateWithRow(row
);
4237 // finally, include currently visible items in the calculation:
4238 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4239 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4240 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4242 first_visible
= wxMax(first_visible
, top_part_end
);
4243 last_visible
= wxMin(bottom_part_start
, last_visible
);
4245 for ( row
= first_visible
; row
< last_visible
; row
++ )
4247 calculator
.UpdateWithRow(row
);
4250 wxLogTrace("dataview",
4251 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4253 count
- bottom_part_start
,
4254 wxMax(0, last_visible
- first_visible
),
4258 int max_width
= calculator
.GetMaxWidth();
4259 if ( max_width
> 0 )
4260 max_width
+= 2 * PADDING_RIGHTLEFT
;
4262 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4266 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4267 unsigned int WXUNUSED(new_pos
))
4269 // do _not_ reorder m_cols elements here, they should always be in the
4270 // order in which columns were added, we only display the columns in
4272 m_clientArea
->UpdateDisplay();
4275 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4277 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4281 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4283 OnColumnsCountChanged();
4288 bool wxDataViewCtrl::ClearColumns()
4291 m_colsBestWidths
.clear();
4292 OnColumnsCountChanged();
4296 void wxDataViewCtrl::UpdateColBestWidth(int idx
)
4298 m_colsBestWidths
[idx
] = 0;
4301 m_headerArea
->UpdateColumn(idx
);
4304 void wxDataViewCtrl::UpdateColBestWidths()
4306 m_colsBestWidths
.clear();
4307 m_colsBestWidths
.resize(m_cols
.size());
4311 const unsigned cols
= m_headerArea
->GetColumnCount();
4312 for ( unsigned i
= 0; i
< cols
; i
++ )
4313 m_headerArea
->UpdateColumn(i
);
4317 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4320 unsigned int len
= GetColumnCount();
4321 for ( unsigned int i
= 0; i
< len
; i
++ )
4323 wxDataViewColumn
* col
= GetColumnAt(i
);
4330 // This returns the position in pixels which is not what we want.
4333 unsigned int len
= GetColumnCount();
4334 for ( unsigned int i
= 0; i
< len
; i
++ )
4336 wxDataViewColumn
* col
= GetColumnAt(i
);
4337 if (col
->IsHidden())
4339 ret
+= col
->GetWidth();
4342 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4350 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4352 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4353 : GetColumn(m_sortingColumnIdx
);
4356 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4358 return GetItemByRow(m_clientArea
->GetCurrentRow());
4361 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4363 const int row
= m_clientArea
->GetRowByItem(item
);
4365 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4366 if ( static_cast<unsigned>(row
) != oldCurrent
)
4368 m_clientArea
->ChangeCurrentRow(row
);
4369 m_clientArea
->RefreshRow(oldCurrent
);
4370 m_clientArea
->RefreshRow(row
);
4374 int wxDataViewCtrl::GetSelectedItemsCount() const
4376 return m_clientArea
->GetSelections().size();
4379 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4382 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4384 const size_t len
= selections
.size();
4385 for ( size_t i
= 0; i
< len
; i
++ )
4387 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4394 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4401 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4403 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4405 wxDataViewItem last_parent
;
4407 int len
= sel
.GetCount();
4408 for( int i
= 0; i
< len
; i
++ )
4410 wxDataViewItem item
= sel
[i
];
4411 wxDataViewItem parent
= GetModel()->GetParent( item
);
4414 if (parent
!= last_parent
)
4415 ExpandAncestors(item
);
4418 last_parent
= parent
;
4419 int row
= m_clientArea
->GetRowByItem( item
);
4421 selection
.Add( static_cast<unsigned int>(row
) );
4424 m_clientArea
->SetSelections( selection
);
4427 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4429 ExpandAncestors( item
);
4431 int row
= m_clientArea
->GetRowByItem( item
);
4434 // Unselect all rows before select another in the single select mode
4435 if (m_clientArea
->IsSingleSel())
4436 m_clientArea
->SelectAllRows(false);
4438 m_clientArea
->SelectRow(row
, true);
4440 // Also set focus to the selected item
4441 m_clientArea
->ChangeCurrentRow( row
);
4445 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4447 int row
= m_clientArea
->GetRowByItem( item
);
4449 m_clientArea
->SelectRow(row
, false);
4452 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4454 int row
= m_clientArea
->GetRowByItem( item
);
4457 return m_clientArea
->IsRowSelected(row
);
4462 void wxDataViewCtrl::SelectAll()
4464 m_clientArea
->SelectAllRows(true);
4467 void wxDataViewCtrl::UnselectAll()
4469 m_clientArea
->SelectAllRows(false);
4472 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4476 if( row
> (int) m_clientArea
->GetRowCount() )
4477 row
= m_clientArea
->GetRowCount();
4479 int first
= m_clientArea
->GetFirstVisibleRow();
4480 int last
= m_clientArea
->GetLastVisibleRow();
4482 m_clientArea
->ScrollTo( row
, column
);
4483 else if( row
> last
)
4484 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4486 m_clientArea
->ScrollTo( first
, column
);
4489 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4491 ExpandAncestors( item
);
4493 m_clientArea
->RecalculateDisplay();
4495 int row
= m_clientArea
->GetRowByItem(item
);
4498 if( column
== NULL
)
4499 EnsureVisible(row
, -1);
4501 EnsureVisible( row
, GetColumnIndex(column
) );
4506 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4507 wxDataViewColumn
* &column
) const
4509 m_clientArea
->HitTest(point
, item
, column
);
4512 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4513 const wxDataViewColumn
* column
) const
4515 return m_clientArea
->GetItemRect(item
, column
);
4518 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4520 return m_clientArea
->GetItemByRow( row
);
4523 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4525 return m_clientArea
->GetRowByItem( item
);
4528 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4530 ExpandAncestors( item
);
4532 int row
= m_clientArea
->GetRowByItem( item
);
4534 m_clientArea
->Expand(row
);
4537 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4539 int row
= m_clientArea
->GetRowByItem( item
);
4541 m_clientArea
->Collapse(row
);
4544 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4546 int row
= m_clientArea
->GetRowByItem( item
);
4548 return m_clientArea
->IsExpanded(row
);
4552 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4554 wxDataViewColumn
* col
= GetColumn( column
);
4558 wxRect itemRect
= GetItemRect(item
, col
);
4559 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4560 if (renderer
->GetMode() == wxDATAVIEW_CELL_EDITABLE
)
4561 renderer
->StartEditing(item
, itemRect
);
4564 #endif // !wxUSE_GENERICDATAVIEWCTRL
4566 #endif // wxUSE_DATAVIEWCTRL