1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
47 #include "wx/imaglist.h"
48 #include "wx/headerctrl.h"
50 #include "wx/stopwatch.h"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 class wxDataViewColumn
;
57 class wxDataViewHeaderWindow
;
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 static const int SCROLL_UNIT_X
= 15;
66 // the cell padding on the left/right
67 static const int PADDING_RIGHTLEFT
= 3;
69 // the expander space margin
70 static const int EXPANDER_MARGIN
= 4;
73 static const int EXPANDER_OFFSET
= 4;
75 static const int EXPANDER_OFFSET
= 1;
78 // Below is the compare stuff.
79 // For the generic implementation, both the leaf nodes and the nodes are sorted for
80 // fast search when needed
81 static wxDataViewModel
* g_model
;
82 static int g_column
= -2;
83 static bool g_asending
= true;
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
92 // Return the expander column or, if it is not set, the first column and also
93 // set it as the expander one for the future.
94 wxDataViewColumn
* GetExpanderColumnOrFirstOne(wxDataViewCtrl
* dataview
)
96 wxDataViewColumn
* expander
= dataview
->GetExpanderColumn();
99 // TODO-RTL: last column for RTL support
100 expander
= dataview
->GetColumnAt( 0 );
101 dataview
->SetExpanderColumn(expander
);
107 } // anonymous namespace
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
113 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
120 m_sortAscending
= true;
123 int wxDataViewColumn::GetWidth() const
127 case wxCOL_WIDTH_DEFAULT
:
128 return wxDVC_DEFAULT_WIDTH
;
130 case wxCOL_WIDTH_AUTOSIZE
:
131 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
132 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
139 void wxDataViewColumn::UpdateDisplay()
143 int idx
= m_owner
->GetColumnIndex( this );
144 m_owner
->OnColumnChange( idx
);
148 void wxDataViewColumn::SetSortOrder(bool ascending
)
153 // First unset the old sort column if any.
154 int oldSortKey
= m_owner
->GetSortingColumnIndex();
155 if ( oldSortKey
!= wxNOT_FOUND
)
157 m_owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
160 // Now set this one as the new sort column.
161 const int idx
= m_owner
->GetColumnIndex(this);
162 m_owner
->SetSortingColumnIndex(idx
);
165 m_sortAscending
= ascending
;
167 // Call this directly instead of using UpdateDisplay() as we already have
168 // the column index, no need to look it up again.
169 m_owner
->OnColumnChange(idx
);
172 //-----------------------------------------------------------------------------
173 // wxDataViewHeaderWindow
174 //-----------------------------------------------------------------------------
176 class wxDataViewHeaderWindow
: public wxHeaderCtrl
179 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
180 : wxHeaderCtrl(parent
)
184 wxDataViewCtrl
*GetOwner() const
185 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
188 // implement/override wxHeaderCtrl functions by forwarding them to the main
190 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
192 return *(GetOwner()->GetColumn(idx
));
195 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
197 wxDataViewCtrl
* const owner
= GetOwner();
199 int widthContents
= owner
->GetBestColumnWidth(idx
);
200 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
201 owner
->OnColumnChange(idx
);
207 bool SendEvent(wxEventType type
, unsigned int n
)
209 wxDataViewCtrl
* const owner
= GetOwner();
210 wxDataViewEvent
event(type
, owner
->GetId());
212 event
.SetEventObject(owner
);
214 event
.SetDataViewColumn(owner
->GetColumn(n
));
215 event
.SetModel(owner
->GetModel());
217 // for events created by wxDataViewHeaderWindow the
218 // row / value fields are not valid
219 return owner
->ProcessWindowEvent(event
);
222 void OnClick(wxHeaderCtrlEvent
& event
)
224 const unsigned idx
= event
.GetColumn();
226 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
229 // default handling for the column click is to sort by this column or
230 // toggle its sort order
231 wxDataViewCtrl
* const owner
= GetOwner();
232 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
233 if ( !col
->IsSortable() )
235 // no default handling for non-sortable columns
240 if ( col
->IsSortKey() )
242 // already using this column for sorting, just change the order
243 col
->ToggleSortOrder();
245 else // not using this column for sorting yet
247 col
->SetSortOrder(true);
250 wxDataViewModel
* const model
= owner
->GetModel();
254 owner
->OnColumnChange(idx
);
256 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
259 void OnRClick(wxHeaderCtrlEvent
& event
)
261 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
266 void OnResize(wxHeaderCtrlEvent
& event
)
268 wxDataViewCtrl
* const owner
= GetOwner();
270 const unsigned col
= event
.GetColumn();
271 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
272 GetOwner()->OnColumnChange(col
);
275 void OnEndReorder(wxHeaderCtrlEvent
& event
)
277 wxDataViewCtrl
* const owner
= GetOwner();
278 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
279 event
.GetNewOrder());
282 DECLARE_EVENT_TABLE()
283 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
286 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
287 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
288 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
290 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
291 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
293 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
296 //-----------------------------------------------------------------------------
297 // wxDataViewRenameTimer
298 //-----------------------------------------------------------------------------
300 class wxDataViewRenameTimer
: public wxTimer
303 wxDataViewMainWindow
*m_owner
;
306 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
310 //-----------------------------------------------------------------------------
311 // wxDataViewTreeNode
312 //-----------------------------------------------------------------------------
314 class wxDataViewTreeNode
;
315 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
317 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
318 wxDataViewTreeNode
** node2
);
320 class wxDataViewTreeNode
323 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
330 ~wxDataViewTreeNode()
334 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
335 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
346 static wxDataViewTreeNode
* CreateRootNode()
348 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
349 n
->SetHasChildren(true);
350 n
->m_branchData
->open
= true;
354 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
356 const wxDataViewTreeNodes
& GetChildNodes() const
358 wxASSERT( m_branchData
!= NULL
);
359 return m_branchData
->children
;
362 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
365 m_branchData
= new BranchNodeData
;
367 m_branchData
->children
.Insert(node
, index
);
369 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
371 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
374 void RemoveChild(wxDataViewTreeNode
*node
)
376 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
377 m_branchData
->children
.Remove(node
);
380 // returns position of child node for given item in children list or wxNOT_FOUND
381 int FindChildByItem(const wxDataViewItem
& item
) const
386 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
387 const int len
= nodes
.size();
388 for ( int i
= 0; i
< len
; i
++ )
390 if ( nodes
[i
]->m_item
== item
)
396 const wxDataViewItem
& GetItem() const { return m_item
; }
397 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
399 int GetIndentLevel() const
402 const wxDataViewTreeNode
* node
= this;
403 while( node
->GetParent()->GetParent() != NULL
)
405 node
= node
->GetParent();
413 return m_branchData
&& m_branchData
->open
;
418 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
422 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
423 const int len
= nodes
.GetCount();
424 for ( int i
= 0;i
< len
; i
++)
425 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
427 if (m_branchData
->open
)
429 ChangeSubTreeCount(-sum
);
430 m_branchData
->open
= !m_branchData
->open
;
434 m_branchData
->open
= !m_branchData
->open
;
435 ChangeSubTreeCount(+sum
);
439 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
440 // even if GetChildNodes() is empty; see below.
441 bool HasChildren() const
443 return m_branchData
!= NULL
;
446 void SetHasChildren(bool has
)
450 wxDELETE(m_branchData
);
452 else if ( m_branchData
== NULL
)
454 m_branchData
= new BranchNodeData
;
458 int GetSubTreeCount() const
460 return m_branchData
? m_branchData
->subTreeCount
: 0;
463 void ChangeSubTreeCount( int num
)
465 wxASSERT( m_branchData
!= NULL
);
467 if( !m_branchData
->open
)
470 m_branchData
->subTreeCount
+= num
;
471 wxASSERT( m_branchData
->subTreeCount
>= 0 );
474 m_parent
->ChangeSubTreeCount(num
);
484 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
486 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
487 int len
= nodes
.GetCount();
488 for (int i
= 0; i
< len
; i
++)
490 if ( nodes
[i
]->HasChildren() )
498 wxDataViewTreeNode
*m_parent
;
500 // Corresponding model item.
501 wxDataViewItem m_item
;
503 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
504 // separate struct in order to conserve memory.
505 struct BranchNodeData
513 // Child nodes. Note that this may be empty even if m_hasChildren in
514 // case this branch of the tree wasn't expanded and realized yet.
515 wxDataViewTreeNodes children
;
517 // Is the branch node currently open (expanded)?
520 // Total count of expanded (i.e. visible with the help of some
521 // scrolling) items in the subtree, but excluding this node. I.e. it is
522 // 0 for leaves and is the number of rows the subtree occupies for
527 BranchNodeData
*m_branchData
;
531 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
532 wxDataViewTreeNode
** node2
)
534 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
538 //-----------------------------------------------------------------------------
539 // wxDataViewMainWindow
540 //-----------------------------------------------------------------------------
542 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
544 class wxDataViewMainWindow
: public wxWindow
547 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
549 const wxPoint
&pos
= wxDefaultPosition
,
550 const wxSize
&size
= wxDefaultSize
,
551 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
552 virtual ~wxDataViewMainWindow();
554 bool IsList() const { return GetModel()->IsListModel(); }
555 bool IsVirtualList() const { return m_root
== NULL
; }
557 // notifications from wxDataViewModel
558 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
559 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
560 bool ItemChanged( const wxDataViewItem
&item
);
561 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
565 if (!IsVirtualList())
575 g_model
= GetModel();
576 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
579 if (g_model
->HasDefaultCompare())
587 g_column
= col
->GetModelColumn();
588 g_asending
= col
->IsSortOrderAscending();
591 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
592 wxDataViewCtrl
*GetOwner() { return m_owner
; }
593 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
595 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
596 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
598 #if wxUSE_DRAG_AND_DROP
599 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
600 #endif // wxUSE_DRAG_AND_DROP
601 void OnPaint( wxPaintEvent
&event
);
602 void OnChar( wxKeyEvent
&event
);
603 void OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
);
606 void OnMouse( wxMouseEvent
&event
);
607 void OnSetFocus( wxFocusEvent
&event
);
608 void OnKillFocus( wxFocusEvent
&event
);
610 void UpdateDisplay();
611 void RecalculateDisplay();
612 void OnInternalIdle();
614 void OnRenameTimer();
616 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
617 void ScrollTo( int rows
, int column
);
619 unsigned GetCurrentRow() const { return m_currentRow
; }
620 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
621 void ChangeCurrentRow( unsigned int row
);
622 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
624 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
625 bool IsEmpty() { return GetRowCount() == 0; }
627 int GetCountPerPage() const;
628 int GetEndOfLastCol() const;
629 unsigned int GetFirstVisibleRow() const;
631 // I change this method to un const because in the tree view,
632 // the displaying number of the tree are changing along with the
633 // expanding/collapsing of the tree nodes
634 unsigned int GetLastVisibleRow();
635 unsigned int GetRowCount();
637 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
638 void SetSelections( const wxDataViewSelection
& sel
)
639 { m_selection
= sel
; UpdateDisplay(); }
640 void Select( const wxArrayInt
& aSelections
);
641 void SelectAllRows( bool on
);
642 void SelectRow( unsigned int row
, bool on
);
643 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
644 void ReverseRowSelection( unsigned int row
);
645 bool IsRowSelected( unsigned int row
);
646 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
648 void RefreshRow( unsigned int row
);
649 void RefreshRows( unsigned int from
, unsigned int to
);
650 void RefreshRowsAfter( unsigned int firstRow
);
652 // returns the colour to be used for drawing the rules
653 wxColour
GetRuleColour() const
655 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
658 wxRect
GetLineRect( unsigned int row
) const;
660 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
661 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
662 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
664 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
666 // Some useful functions for row and item mapping
667 wxDataViewItem
GetItemByRow( unsigned int row
) const;
668 int GetRowByItem( const wxDataViewItem
& item
) const;
670 // Methods for building the mapping tree
671 void BuildTree( wxDataViewModel
* model
);
673 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
674 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
676 void Expand( unsigned int row
);
677 void Collapse( unsigned int row
);
678 bool IsExpanded( unsigned int row
) const;
679 bool HasChildren( unsigned int row
) const;
681 #if wxUSE_DRAG_AND_DROP
682 bool EnableDragSource( const wxDataFormat
&format
);
683 bool EnableDropTarget( const wxDataFormat
&format
);
685 void RemoveDropHint();
686 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
687 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
688 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
690 #endif // wxUSE_DRAG_AND_DROP
692 void OnColumnsCountChanged();
695 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
696 // We did not need this temporarily
697 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
699 int RecalculateCount();
701 // Return false only if the event was vetoed by its handler.
702 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
704 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
706 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
709 wxDataViewCtrl
*m_owner
;
713 wxDataViewColumn
*m_currentCol
;
714 unsigned int m_currentRow
;
715 wxDataViewSelection m_selection
;
717 wxDataViewRenameTimer
*m_renameTimer
;
722 bool m_currentColSetByKeyboard
;
724 #if wxUSE_DRAG_AND_DROP
729 wxDataFormat m_dragFormat
;
732 wxDataFormat m_dropFormat
;
734 unsigned int m_dropHintLine
;
735 #endif // wxUSE_DRAG_AND_DROP
737 // for double click logic
738 unsigned int m_lineLastClicked
,
739 m_lineBeforeLastClicked
,
740 m_lineSelectSingleOnUp
;
742 // the pen used to draw horiz/vertical rules
745 // the pen used to draw the expander and the lines
748 // This is the tree structure of the model
749 wxDataViewTreeNode
* m_root
;
752 // This is the tree node under the cursor
753 wxDataViewTreeNode
* m_underMouse
;
756 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
757 DECLARE_EVENT_TABLE()
760 // ---------------------------------------------------------
761 // wxGenericDataViewModelNotifier
762 // ---------------------------------------------------------
764 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
767 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
768 { m_mainWindow
= mainWindow
; }
770 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
771 { return m_mainWindow
->ItemAdded( parent
, item
); }
772 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
773 { return m_mainWindow
->ItemDeleted( parent
, item
); }
774 virtual bool ItemChanged( const wxDataViewItem
& item
)
775 { return m_mainWindow
->ItemChanged(item
); }
776 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
777 { return m_mainWindow
->ValueChanged( item
, col
); }
778 virtual bool Cleared()
779 { return m_mainWindow
->Cleared(); }
780 virtual void Resort()
781 { m_mainWindow
->Resort(); }
783 wxDataViewMainWindow
*m_mainWindow
;
786 // ---------------------------------------------------------
787 // wxDataViewRenderer
788 // ---------------------------------------------------------
790 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
792 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
793 wxDataViewCellMode mode
,
795 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
799 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
803 wxDataViewRenderer::~wxDataViewRenderer()
808 wxDC
*wxDataViewRenderer::GetDC()
812 if (GetOwner() == NULL
)
814 if (GetOwner()->GetOwner() == NULL
)
816 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
822 void wxDataViewRenderer::SetAlignment( int align
)
827 int wxDataViewRenderer::GetAlignment() const
832 // ---------------------------------------------------------
833 // wxDataViewCustomRenderer
834 // ---------------------------------------------------------
836 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
838 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
839 wxDataViewCellMode mode
, int align
) :
840 wxDataViewRenderer( varianttype
, mode
, align
)
844 // ---------------------------------------------------------
845 // wxDataViewTextRenderer
846 // ---------------------------------------------------------
848 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
850 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
851 wxDataViewCellMode mode
, int align
) :
852 wxDataViewRenderer( varianttype
, mode
, align
)
856 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
858 m_text
= value
.GetString();
863 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
868 bool wxDataViewTextRenderer::HasEditorCtrl() const
873 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
874 wxRect labelRect
, const wxVariant
&value
)
876 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
877 wxPoint(labelRect
.x
,labelRect
.y
),
878 wxSize(labelRect
.width
,labelRect
.height
),
879 wxTE_PROCESS_ENTER
);
881 // select the text in the control an place the cursor at the end
882 ctrl
->SetInsertionPointEnd();
888 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
890 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
891 value
= text
->GetValue();
895 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
897 RenderText(m_text
, 0, rect
, dc
, state
);
901 wxSize
wxDataViewTextRenderer::GetSize() const
904 return GetTextExtent(m_text
);
906 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
909 // ---------------------------------------------------------
910 // wxDataViewBitmapRenderer
911 // ---------------------------------------------------------
913 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
915 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
916 wxDataViewCellMode mode
, int align
) :
917 wxDataViewRenderer( varianttype
, mode
, align
)
921 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
923 if (value
.GetType() == wxT("wxBitmap"))
925 if (value
.GetType() == wxT("wxIcon"))
931 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
936 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
939 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
940 else if (m_icon
.IsOk())
941 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
946 wxSize
wxDataViewBitmapRenderer::GetSize() const
949 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
950 else if (m_icon
.IsOk())
951 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
953 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
956 // ---------------------------------------------------------
957 // wxDataViewToggleRenderer
958 // ---------------------------------------------------------
960 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
962 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
963 wxDataViewCellMode mode
, int align
) :
964 wxDataViewRenderer( varianttype
, mode
, align
)
969 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
971 m_toggle
= value
.GetBool();
976 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
981 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
985 flags
|= wxCONTROL_CHECKED
;
986 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
987 GetEnabled() == false)
988 flags
|= wxCONTROL_DISABLED
;
990 // check boxes we draw must always have the same, standard size (if it's
991 // bigger than the cell size the checkbox will be truncated because the
992 // caller had set the clipping rectangle to prevent us from drawing outside
994 cell
.SetSize(GetSize());
996 wxRendererNative::Get().DrawCheckBox(
997 GetOwner()->GetOwner(),
1005 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1006 wxDataViewModel
*model
,
1007 const wxDataViewItem
& item
,
1009 const wxMouseEvent
*mouseEvent
)
1011 if ( !model
->IsEnabled(item
, col
) )
1016 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1017 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1021 model
->ChangeValue(!m_toggle
, item
, col
);
1025 wxSize
wxDataViewToggleRenderer::GetSize() const
1027 // the window parameter is not used by GetCheckBoxSize() so it's
1028 // safe to pass NULL
1029 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1032 // ---------------------------------------------------------
1033 // wxDataViewProgressRenderer
1034 // ---------------------------------------------------------
1036 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1038 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1039 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1040 wxDataViewRenderer( varianttype
, mode
, align
)
1046 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1048 m_value
= (long) value
;
1050 if (m_value
< 0) m_value
= 0;
1051 if (m_value
> 100) m_value
= 100;
1056 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1058 value
= (long) m_value
;
1063 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1065 // deflate the rect to leave a small border between bars in adjacent rows
1066 wxRect bar
= rect
.Deflate(0, 1);
1068 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1069 dc
->SetPen( *wxBLACK_PEN
);
1070 dc
->DrawRectangle( bar
);
1072 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1073 dc
->SetPen( *wxTRANSPARENT_PEN
);
1075 const wxDataViewItemAttr
& attr
= GetAttr();
1076 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1078 dc
->DrawRectangle( bar
);
1083 wxSize
wxDataViewProgressRenderer::GetSize() const
1085 return wxSize(40,12);
1088 // ---------------------------------------------------------
1089 // wxDataViewIconTextRenderer
1090 // ---------------------------------------------------------
1092 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1094 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1095 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1096 wxDataViewRenderer( varianttype
, mode
, align
)
1099 SetAlignment(align
);
1102 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1108 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1113 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1117 const wxIcon
& icon
= m_value
.GetIcon();
1120 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1121 xoffset
= icon
.GetWidth()+4;
1124 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1129 wxSize
wxDataViewIconTextRenderer::GetSize() const
1131 if (!m_value
.GetText().empty())
1133 wxSize size
= GetTextExtent(m_value
.GetText());
1135 if (m_value
.GetIcon().IsOk())
1136 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1139 return wxSize(80,20);
1142 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1144 wxDataViewIconText iconText
;
1147 wxString text
= iconText
.GetText();
1149 // adjust the label rect to take the width of the icon into account
1150 if (iconText
.GetIcon().IsOk())
1152 int w
= iconText
.GetIcon().GetWidth() + 4;
1154 labelRect
.width
-= w
;
1157 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1158 wxPoint(labelRect
.x
,labelRect
.y
),
1159 wxSize(labelRect
.width
,labelRect
.height
),
1160 wxTE_PROCESS_ENTER
);
1162 // select the text in the control an place the cursor at the end
1163 ctrl
->SetInsertionPointEnd();
1169 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1171 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1173 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1178 //-----------------------------------------------------------------------------
1179 // wxDataViewDropTarget
1180 //-----------------------------------------------------------------------------
1182 #if wxUSE_DRAG_AND_DROP
1184 class wxBitmapCanvas
: public wxWindow
1187 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1188 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1191 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1194 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1197 dc
.DrawBitmap( m_bitmap
, 0, 0);
1203 class wxDataViewDropSource
: public wxDropSource
1206 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1214 ~wxDataViewDropSource()
1219 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1221 wxPoint pos
= wxGetMousePosition();
1225 int liney
= m_win
->GetLineStart( m_row
);
1227 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1228 m_win
->ClientToScreen( &linex
, &liney
);
1229 m_dist_x
= pos
.x
- linex
;
1230 m_dist_y
= pos
.y
- liney
;
1233 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1235 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1236 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1238 wxFRAME_TOOL_WINDOW
|
1239 wxFRAME_FLOAT_ON_PARENT
|
1240 wxFRAME_NO_TASKBAR
|
1242 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1247 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1248 m_hint
->SetTransparent( 128 );
1254 wxDataViewMainWindow
*m_win
;
1257 int m_dist_x
,m_dist_y
;
1261 class wxDataViewDropTarget
: public wxDropTarget
1264 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1270 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1272 wxDataFormat format
= GetMatchingPair();
1273 if (format
== wxDF_INVALID
)
1275 return m_win
->OnDragOver( format
, x
, y
, def
);
1278 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1280 wxDataFormat format
= GetMatchingPair();
1281 if (format
== wxDF_INVALID
)
1283 return m_win
->OnDrop( format
, x
, y
);
1286 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1288 wxDataFormat format
= GetMatchingPair();
1289 if (format
== wxDF_INVALID
)
1293 return m_win
->OnData( format
, x
, y
, def
);
1296 virtual void OnLeave()
1297 { m_win
->OnLeave(); }
1299 wxDataViewMainWindow
*m_win
;
1302 #endif // wxUSE_DRAG_AND_DROP
1304 //-----------------------------------------------------------------------------
1305 // wxDataViewRenameTimer
1306 //-----------------------------------------------------------------------------
1308 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1313 void wxDataViewRenameTimer::Notify()
1315 m_owner
->OnRenameTimer();
1318 //-----------------------------------------------------------------------------
1319 // wxDataViewMainWindow
1320 //-----------------------------------------------------------------------------
1322 // The tree building helper, declared firstly
1323 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1324 wxDataViewTreeNode
* node
);
1326 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1328 if (row1
> row2
) return 1;
1329 if (row1
== row2
) return 0;
1333 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1335 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1336 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1337 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1338 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1339 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1340 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1343 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1344 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1345 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1346 m_selection( wxDataViewSelectionCmp
)
1351 m_lastOnSame
= false;
1352 m_renameTimer
= new wxDataViewRenameTimer( this );
1354 // TODO: user better initial values/nothing selected
1355 m_currentCol
= NULL
;
1356 m_currentColSetByKeyboard
= false;
1357 m_useCellFocus
= false;
1360 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1362 #if wxUSE_DRAG_AND_DROP
1364 m_dragStart
= wxPoint(0,0);
1366 m_dragEnabled
= false;
1367 m_dropEnabled
= false;
1369 m_dropHintLine
= (unsigned int) -1;
1370 #endif // wxUSE_DRAG_AND_DROP
1372 m_lineLastClicked
= (unsigned int) -1;
1373 m_lineBeforeLastClicked
= (unsigned int) -1;
1374 m_lineSelectSingleOnUp
= (unsigned int) -1;
1378 SetBackgroundColour( *wxWHITE
);
1380 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1382 m_penRule
= wxPen(GetRuleColour());
1384 // compose a pen whichcan draw black lines
1385 // TODO: maybe there is something system colour to use
1386 m_penExpander
= wxPen(wxColour(0,0,0));
1388 m_root
= wxDataViewTreeNode::CreateRootNode();
1390 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1392 m_underMouse
= NULL
;
1396 wxDataViewMainWindow::~wxDataViewMainWindow()
1399 delete m_renameTimer
;
1403 #if wxUSE_DRAG_AND_DROP
1404 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1406 m_dragFormat
= format
;
1407 m_dragEnabled
= format
!= wxDF_INVALID
;
1412 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1414 m_dropFormat
= format
;
1415 m_dropEnabled
= format
!= wxDF_INVALID
;
1418 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1423 void wxDataViewMainWindow::RemoveDropHint()
1428 RefreshRow( m_dropHintLine
);
1429 m_dropHintLine
= (unsigned int) -1;
1433 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1434 wxCoord y
, wxDragResult def
)
1438 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1439 unsigned int row
= GetLineAt( yy
);
1441 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1447 wxDataViewItem item
= GetItemByRow( row
);
1449 wxDataViewModel
*model
= GetModel();
1451 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1452 event
.SetEventObject( m_owner
);
1453 event
.SetItem( item
);
1454 event
.SetModel( model
);
1455 event
.SetDataFormat( format
);
1456 if (!m_owner
->HandleWindowEvent( event
))
1462 if (!event
.IsAllowed())
1469 if (m_dropHint
&& (row
!= m_dropHintLine
))
1470 RefreshRow( m_dropHintLine
);
1472 m_dropHintLine
= row
;
1478 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1484 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1485 unsigned int row
= GetLineAt( yy
);
1487 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1490 wxDataViewItem item
= GetItemByRow( row
);
1492 wxDataViewModel
*model
= GetModel();
1494 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1495 event
.SetEventObject( m_owner
);
1496 event
.SetItem( item
);
1497 event
.SetModel( model
);
1498 event
.SetDataFormat( format
);
1499 if (!m_owner
->HandleWindowEvent( event
))
1502 if (!event
.IsAllowed())
1508 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1513 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1514 unsigned int row
= GetLineAt( yy
);
1516 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1519 wxDataViewItem item
= GetItemByRow( row
);
1521 wxDataViewModel
*model
= GetModel();
1523 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1525 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1526 event
.SetEventObject( m_owner
);
1527 event
.SetItem( item
);
1528 event
.SetModel( model
);
1529 event
.SetDataFormat( format
);
1530 event
.SetDataSize( obj
->GetSize() );
1531 event
.SetDataBuffer( obj
->GetData() );
1532 if (!m_owner
->HandleWindowEvent( event
))
1535 if (!event
.IsAllowed())
1541 void wxDataViewMainWindow::OnLeave()
1546 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1548 int height
= GetLineHeight( row
);
1550 unsigned int cols
= GetOwner()->GetColumnCount();
1552 for (col
= 0; col
< cols
; col
++)
1554 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1555 if (column
->IsHidden())
1556 continue; // skip it!
1557 width
+= column
->GetWidth();
1563 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1564 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1565 indent
= indent
+ m_lineHeight
;
1566 // try to use the m_lineHeight as the expander space
1570 wxBitmap
bitmap( width
, height
);
1571 wxMemoryDC
dc( bitmap
);
1572 dc
.SetFont( GetFont() );
1573 dc
.SetPen( *wxBLACK_PEN
);
1574 dc
.SetBrush( *wxWHITE_BRUSH
);
1575 dc
.DrawRectangle( 0,0,width
,height
);
1577 wxDataViewModel
*model
= m_owner
->GetModel();
1579 wxDataViewColumn
* const
1580 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1583 for (col
= 0; col
< cols
; col
++)
1585 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1586 wxDataViewRenderer
*cell
= column
->GetRenderer();
1588 if (column
->IsHidden())
1589 continue; // skip it!
1591 width
= column
->GetWidth();
1593 if (column
== expander
)
1596 wxDataViewItem item
= GetItemByRow( row
);
1597 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1599 wxRect
item_rect(x
, 0, width
, height
);
1600 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1602 // dc.SetClippingRegion( item_rect );
1603 cell
->WXCallRender(item_rect
, &dc
, 0);
1604 // dc.DestroyClippingRegion();
1612 #endif // wxUSE_DRAG_AND_DROP
1615 // Draw focus rect for individual cell. Unlike native focus rect, we render
1616 // this in foreground text color (typically white) to enhance contrast and
1618 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1620 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1622 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1623 // may be short traits and not really dots
1625 // note that to behave in the same manner as DrawRect(), we must exclude
1626 // the bottom and right borders from the rectangle
1627 wxCoord x1
= rect
.GetLeft(),
1629 x2
= rect
.GetRight(),
1630 y2
= rect
.GetBottom();
1632 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1635 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1636 dc
.DrawPoint(z
, rect
.GetTop());
1638 wxCoord shift
= z
== x2
? 0 : 1;
1639 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1640 dc
.DrawPoint(x2
, z
);
1642 shift
= z
== y2
? 0 : 1;
1643 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1644 dc
.DrawPoint(z
, y2
);
1646 shift
= z
== x1
? 0 : 1;
1647 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1648 dc
.DrawPoint(x1
, z
);
1652 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1654 wxDataViewModel
*model
= GetModel();
1655 wxAutoBufferedPaintDC
dc( this );
1658 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1659 dc
.SetPen( *wxTRANSPARENT_PEN
);
1660 dc
.DrawRectangle(GetClientSize());
1665 // No items to draw.
1670 GetOwner()->PrepareDC( dc
);
1671 dc
.SetFont( GetFont() );
1673 wxRect update
= GetUpdateRegion().GetBox();
1674 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1676 // compute which items needs to be redrawn
1677 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1678 unsigned int item_count
=
1679 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1680 (int)(GetRowCount( ) - item_start
));
1681 unsigned int item_last
= item_start
+ item_count
;
1683 // Send the event to wxDataViewCtrl itself.
1684 wxWindow
* const parent
= GetParent();
1685 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1686 cache_event
.SetEventObject(parent
);
1687 cache_event
.SetCache(item_start
, item_last
- 1);
1688 parent
->ProcessWindowEvent(cache_event
);
1690 // compute which columns needs to be redrawn
1691 unsigned int cols
= GetOwner()->GetColumnCount();
1694 // we assume that we have at least one column below and painting an
1695 // empty control is unnecessary anyhow
1699 unsigned int col_start
= 0;
1700 unsigned int x_start
;
1701 for (x_start
= 0; col_start
< cols
; col_start
++)
1703 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1704 if (col
->IsHidden())
1705 continue; // skip it!
1707 unsigned int w
= col
->GetWidth();
1708 if (x_start
+w
>= (unsigned int)update
.x
)
1714 unsigned int col_last
= col_start
;
1715 unsigned int x_last
= x_start
;
1716 for (; col_last
< cols
; col_last
++)
1718 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1719 if (col
->IsHidden())
1720 continue; // skip it!
1722 if (x_last
> (unsigned int)update
.GetRight())
1725 x_last
+= col
->GetWidth();
1728 // Draw horizontal rules if required
1729 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1731 dc
.SetPen(m_penRule
);
1732 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1734 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1736 int y
= GetLineStart( i
);
1737 dc
.DrawLine(x_start
, y
, x_last
, y
);
1741 // Draw vertical rules if required
1742 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1744 dc
.SetPen(m_penRule
);
1745 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1747 // NB: Vertical rules are drawn in the last pixel of a column so that
1748 // they align perfectly with native MSW wxHeaderCtrl as well as for
1749 // consistency with MSW native list control. There's no vertical
1750 // rule at the most-left side of the control.
1752 int x
= x_start
- 1;
1753 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1755 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1756 if (col
->IsHidden())
1757 continue; // skip it
1759 x
+= col
->GetWidth();
1761 dc
.DrawLine(x
, GetLineStart( item_start
),
1762 x
, GetLineStart( item_last
) );
1766 // redraw the background for the items which are selected/current
1767 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1769 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1771 if (selected
|| item
== m_currentRow
)
1773 wxRect
rect( x_start
, GetLineStart( item
),
1774 x_last
- x_start
, GetLineHeight( item
) );
1776 // draw selection and whole-item focus:
1779 int flags
= wxCONTROL_SELECTED
;
1781 flags
|= wxCONTROL_FOCUSED
;
1783 wxRendererNative::Get().DrawItemSelectionRect
1792 // draw keyboard focus rect if applicable
1793 if ( item
== m_currentRow
&& m_hasFocus
)
1795 bool renderColumnFocus
= false;
1797 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1799 renderColumnFocus
= true;
1801 // If this is container node without columns, render full-row focus:
1804 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1805 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1806 renderColumnFocus
= false;
1810 if ( renderColumnFocus
)
1812 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1814 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1815 if ( col
->IsHidden() )
1818 rect
.width
= col
->GetWidth();
1820 if ( col
== m_currentCol
)
1822 // make the rect more visible by adding a small
1823 // margin around it:
1828 // DrawFocusRect() uses XOR and is all but
1829 // invisible against dark-blue background. Use
1830 // the same color used for selected text.
1831 DrawSelectedCellFocusRect(dc
, rect
);
1835 wxRendererNative::Get().DrawFocusRect
1846 rect
.x
+= rect
.width
;
1851 // render focus rectangle for the whole row
1852 wxRendererNative::Get().DrawFocusRect
1857 selected
? (int)wxCONTROL_SELECTED
: 0
1864 #if wxUSE_DRAG_AND_DROP
1867 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1868 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1869 dc
.SetPen( *wxBLACK_PEN
);
1870 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1871 dc
.DrawRectangle( rect
);
1873 #endif // wxUSE_DRAG_AND_DROP
1875 wxDataViewColumn
* const
1876 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1878 // redraw all cells for all rows which must be repainted and all columns
1880 cell_rect
.x
= x_start
;
1881 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1883 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1884 wxDataViewRenderer
*cell
= col
->GetRenderer();
1885 cell_rect
.width
= col
->GetWidth();
1887 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1888 continue; // skip it!
1890 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1892 // get the cell value and set it into the renderer
1893 wxDataViewTreeNode
*node
= NULL
;
1894 wxDataViewItem dataitem
;
1896 if (!IsVirtualList())
1898 node
= GetTreeNodeByRow(item
);
1902 dataitem
= node
->GetItem();
1904 // Skip all columns of "container" rows except the expander
1905 // column itself unless HasContainerColumns() overrides this.
1906 if ( col
!= expander
&&
1907 model
->IsContainer(dataitem
) &&
1908 !model
->HasContainerColumns(dataitem
) )
1913 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1916 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1919 cell_rect
.y
= GetLineStart( item
);
1920 cell_rect
.height
= GetLineHeight( item
);
1922 // deal with the expander
1924 if ((!IsList()) && (col
== expander
))
1926 // Calculate the indent first
1927 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1929 // we reserve m_lineHeight of horizontal space for the expander
1930 // but leave EXPANDER_MARGIN around the expander itself
1931 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1933 indent
+= m_lineHeight
;
1935 // draw expander if needed and visible
1936 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1938 dc
.SetPen( m_penExpander
);
1939 dc
.SetBrush( wxNullBrush
);
1941 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1942 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1943 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1945 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1948 if ( m_underMouse
== node
)
1949 flag
|= wxCONTROL_CURRENT
;
1950 if ( node
->IsOpen() )
1951 flag
|= wxCONTROL_EXPANDED
;
1953 // ensure that we don't overflow the cell (which might
1954 // happen if the column is very narrow)
1955 wxDCClipper
clip(dc
, cell_rect
);
1957 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1960 // force the expander column to left-center align
1961 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1964 wxRect item_rect
= cell_rect
;
1965 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1967 // account for the tree indent (harmless if we're not indented)
1968 item_rect
.x
+= indent
;
1969 item_rect
.width
-= indent
;
1971 if ( item_rect
.width
<= 0 )
1975 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1976 state
|= wxDATAVIEW_CELL_SELECTED
;
1978 // TODO: it would be much more efficient to create a clipping
1979 // region for the entire column being rendered (in the OnPaint
1980 // of wxDataViewMainWindow) instead of a single clip region for
1981 // each cell. However it would mean that each renderer should
1982 // respect the given wxRect's top & bottom coords, eventually
1983 // violating only the left & right coords - however the user can
1984 // make its own renderer and thus we cannot be sure of that.
1985 wxDCClipper
clip(dc
, item_rect
);
1987 cell
->WXCallRender(item_rect
, &dc
, state
);
1990 cell_rect
.x
+= cell_rect
.width
;
1994 void wxDataViewMainWindow::OnRenameTimer()
1996 // We have to call this here because changes may just have
1997 // been made and no screen update taken place.
2000 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2001 // (needs to be tested!)
2005 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2007 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
2009 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2012 //-----------------------------------------------------------------------------
2013 // Helper class for do operation on the tree node
2014 //-----------------------------------------------------------------------------
2019 virtual ~DoJob() { }
2021 // The return value control how the tree-walker tranverse the tree
2024 DONE
, // Job done, stop traversing and return
2025 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2026 CONTINUE
// Job not done, continue
2029 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2032 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2034 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2036 switch( func( node
) )
2040 case DoJob::SKIP_SUBTREE
:
2042 case DoJob::CONTINUE
:
2046 if ( node
->HasChildren() )
2048 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2050 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2054 if ( Walker(*i
, func
) )
2062 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2064 if (IsVirtualList())
2066 wxDataViewVirtualListModel
*list_model
=
2067 (wxDataViewVirtualListModel
*) GetModel();
2068 m_count
= list_model
->GetCount();
2074 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2079 wxDataViewItemArray modelSiblings
;
2080 GetModel()->GetChildren(parent
, modelSiblings
);
2081 const int modelSiblingsSize
= modelSiblings
.size();
2083 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2084 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2086 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2087 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2089 parentNode
->SetHasChildren(true);
2091 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2092 const int nodeSiblingsSize
= nodeSiblings
.size();
2096 if ( posInModel
== modelSiblingsSize
- 1 )
2098 nodePos
= nodeSiblingsSize
;
2100 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2102 // This is the simple case when our node tree already matches the
2103 // model and only this one item is missing.
2104 nodePos
= posInModel
;
2108 // It's possible that a larger discrepancy between the model and
2109 // our realization exists. This can happen e.g. when adding a bunch
2110 // of items to the model and then calling ItemsAdded() just once
2111 // afterwards. In this case, we must find the right position by
2112 // looking at sibling items.
2114 // append to the end if we won't find a better position:
2115 nodePos
= nodeSiblingsSize
;
2117 for ( int nextItemPos
= posInModel
+ 1;
2118 nextItemPos
< modelSiblingsSize
;
2121 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2122 if ( nextNodePos
!= wxNOT_FOUND
)
2124 nodePos
= nextNodePos
;
2130 parentNode
->ChangeSubTreeCount(+1);
2131 parentNode
->InsertChild(itemNode
, nodePos
);
2136 GetOwner()->InvalidateColBestWidths();
2142 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2143 const wxDataViewItem
& item
)
2145 if (IsVirtualList())
2147 wxDataViewVirtualListModel
*list_model
=
2148 (wxDataViewVirtualListModel
*) GetModel();
2149 m_count
= list_model
->GetCount();
2151 if ( !m_selection
.empty() )
2153 const int row
= GetRowByItem(item
);
2155 int rowIndexInSelection
= wxNOT_FOUND
;
2157 const size_t selCount
= m_selection
.size();
2158 for ( size_t i
= 0; i
< selCount
; i
++ )
2160 if ( m_selection
[i
] == (unsigned)row
)
2161 rowIndexInSelection
= i
;
2162 else if ( m_selection
[i
] > (unsigned)row
)
2166 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2167 m_selection
.RemoveAt(rowIndexInSelection
);
2171 else // general case
2173 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2175 // Notice that it is possible that the item being deleted is not in the
2176 // tree at all, for example we could be deleting a never shown (because
2177 // collapsed) item in a tree model. So it's not an error if we don't know
2178 // about this item, just return without doing anything then.
2182 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2183 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2185 // We can't use FindNode() to find 'item', because it was already
2186 // removed from the model by the time ItemDeleted() is called, so we
2187 // have to do it manually. We keep track of its position as well for
2189 int itemPosInNode
= 0;
2190 wxDataViewTreeNode
*itemNode
= NULL
;
2191 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2192 i
!= parentsChildren
.end();
2193 ++i
, ++itemPosInNode
)
2195 if( (*i
)->GetItem() == item
)
2202 // If the parent wasn't expanded, it's possible that we didn't have a
2203 // node corresponding to 'item' and so there's nothing left to do.
2206 // If this was the last child to be removed, it's possible the parent
2207 // node became a leaf. Let's ask the model about it.
2208 if ( parentNode
->GetChildNodes().empty() )
2209 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2214 // Delete the item from wxDataViewTreeNode representation:
2215 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2217 parentNode
->RemoveChild(itemNode
);
2219 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2221 // Make the row number invalid and get a new valid one when user call GetRowCount
2224 // If this was the last child to be removed, it's possible the parent
2225 // node became a leaf. Let's ask the model about it.
2226 if ( parentNode
->GetChildNodes().empty() )
2227 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2229 // Update selection by removing 'item' and its entire children tree from the selection.
2230 if ( !m_selection
.empty() )
2232 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2233 // the parent ('parentNode') and position in its list of children
2235 if ( itemPosInNode
== 0 )
2237 // 1st child, row number is that of the parent parentNode + 1
2238 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2242 // row number is that of the sibling above 'item' + its subtree if any + 1
2243 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2245 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2246 siblingNode
->GetSubTreeCount() +
2250 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2252 const size_t numSelections
= m_selection
.size();
2253 for ( size_t i
= 0; i
< numSelections
; ++i
)
2255 const int s
= m_selection
[i
];
2257 newsel
.push_back(s
);
2258 else if ( s
>= itemRow
+ itemsDeleted
)
2259 newsel
.push_back(s
- itemsDeleted
);
2260 // else: deleted item, remove from selection
2263 m_selection
= newsel
;
2267 // Change the current row to the last row if the current exceed the max row number
2268 if( m_currentRow
> GetRowCount() )
2269 ChangeCurrentRow(m_count
- 1);
2271 GetOwner()->InvalidateColBestWidths();
2277 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2282 GetOwner()->InvalidateColBestWidths();
2285 wxWindow
*parent
= GetParent();
2286 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2287 le
.SetEventObject(parent
);
2288 le
.SetModel(GetModel());
2290 parent
->ProcessWindowEvent(le
);
2295 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2297 int view_column
= -1;
2298 unsigned int n_col
= m_owner
->GetColumnCount();
2299 for (unsigned i
= 0; i
< n_col
; i
++)
2301 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2302 if (column
->GetModelColumn() == model_column
)
2304 view_column
= (int) i
;
2308 if (view_column
== -1)
2311 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2312 /*#define MAX_VIRTUAL_WIDTH 100000
2314 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2315 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2316 Refresh( true, &rect );
2323 GetOwner()->InvalidateColBestWidth(view_column
);
2326 wxWindow
*parent
= GetParent();
2327 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2328 le
.SetEventObject(parent
);
2329 le
.SetModel(GetModel());
2331 le
.SetColumn(view_column
);
2332 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2333 parent
->ProcessWindowEvent(le
);
2338 bool wxDataViewMainWindow::Cleared()
2341 m_selection
.Clear();
2344 BuildTree( GetModel() );
2346 GetOwner()->InvalidateColBestWidths();
2352 void wxDataViewMainWindow::UpdateDisplay()
2355 m_underMouse
= NULL
;
2358 void wxDataViewMainWindow::OnInternalIdle()
2360 wxWindow::OnInternalIdle();
2364 RecalculateDisplay();
2369 void wxDataViewMainWindow::RecalculateDisplay()
2371 wxDataViewModel
*model
= GetModel();
2378 int width
= GetEndOfLastCol();
2379 int height
= GetLineStart( GetRowCount() );
2381 SetVirtualSize( width
, height
);
2382 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2387 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2389 m_underMouse
= NULL
;
2391 wxWindow::ScrollWindow( dx
, dy
, rect
);
2393 if (GetOwner()->m_headerArea
)
2394 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2397 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2399 m_underMouse
= NULL
;
2402 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2403 int sy
= GetLineStart( rows
)/y
;
2407 wxRect rect
= GetClientRect();
2411 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2412 for (x_start
= 0; colnum
< column
; colnum
++)
2414 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2415 if (col
->IsHidden())
2416 continue; // skip it!
2418 w
= col
->GetWidth();
2422 int x_end
= x_start
+ w
;
2423 xe
= xx
+ rect
.width
;
2426 sx
= ( xx
+ x_end
- xe
)/x
;
2433 m_owner
->Scroll( sx
, sy
);
2436 int wxDataViewMainWindow::GetCountPerPage() const
2438 wxSize size
= GetClientSize();
2439 return size
.y
/ m_lineHeight
;
2442 int wxDataViewMainWindow::GetEndOfLastCol() const
2446 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2448 const wxDataViewColumn
*c
=
2449 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2452 width
+= c
->GetWidth();
2457 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2461 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2463 return GetLineAt( y
);
2466 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2468 wxSize client_size
= GetClientSize();
2469 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2470 &client_size
.x
, &client_size
.y
);
2472 // we should deal with the pixel here
2473 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2475 return wxMin( GetRowCount()-1, row
);
2478 unsigned int wxDataViewMainWindow::GetRowCount()
2480 if ( m_count
== -1 )
2482 m_count
= RecalculateCount();
2488 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2495 void wxDataViewMainWindow::SelectAllRows( bool on
)
2502 m_selection
.Clear();
2503 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2504 m_selection
.Add( i
);
2509 unsigned int first_visible
= GetFirstVisibleRow();
2510 unsigned int last_visible
= GetLastVisibleRow();
2512 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2514 unsigned int row
= m_selection
[i
];
2515 if ((row
>= first_visible
) && (row
<= last_visible
))
2518 m_selection
.Clear();
2522 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2524 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2528 m_selection
.Add( row
);
2536 m_selection
.Remove( row
);
2542 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2546 unsigned int tmp
= from
;
2552 for (i
= from
; i
<= to
; i
++)
2554 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2557 m_selection
.Add( i
);
2562 m_selection
.Remove( i
);
2565 RefreshRows( from
, to
);
2568 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2570 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2572 int n
= aSelections
[i
];
2574 m_selection
.Add( n
);
2579 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2581 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2582 m_selection
.Add( row
);
2584 m_selection
.Remove( row
);
2588 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2590 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2593 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2595 wxWindow
*parent
= GetParent();
2596 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2598 le
.SetEventObject(parent
);
2599 le
.SetModel(GetModel());
2602 parent
->ProcessWindowEvent(le
);
2605 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2607 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2608 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2610 wxSize client_size
= GetClientSize();
2611 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2612 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2613 if (intersect_rect
.width
> 0)
2614 Refresh( true, &intersect_rect
);
2617 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2621 unsigned int tmp
= to
;
2626 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2627 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2629 wxSize client_size
= GetClientSize();
2630 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2631 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2632 if (intersect_rect
.width
> 0)
2633 Refresh( true, &intersect_rect
);
2636 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2638 wxSize client_size
= GetClientSize();
2639 int start
= GetLineStart( firstRow
);
2640 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2641 if (start
> client_size
.y
) return;
2643 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2645 Refresh( true, &rect
);
2648 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2652 rect
.y
= GetLineStart( row
);
2653 rect
.width
= GetEndOfLastCol();
2654 rect
.height
= GetLineHeight( row
);
2659 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2661 const wxDataViewModel
*model
= GetModel();
2663 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2665 // TODO make more efficient
2670 for (r
= 0; r
< row
; r
++)
2672 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2673 if (!node
) return start
;
2675 wxDataViewItem item
= node
->GetItem();
2677 unsigned int cols
= GetOwner()->GetColumnCount();
2679 int height
= m_lineHeight
;
2680 for (col
= 0; col
< cols
; col
++)
2682 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2683 if (column
->IsHidden())
2684 continue; // skip it!
2687 model
->IsContainer(item
) &&
2688 !model
->HasContainerColumns(item
))
2689 continue; // skip it!
2691 wxDataViewRenderer
*renderer
=
2692 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2693 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2695 height
= wxMax( height
, renderer
->GetSize().y
);
2705 return row
* m_lineHeight
;
2709 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2711 const wxDataViewModel
*model
= GetModel();
2713 // check for the easy case first
2714 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2715 return y
/ m_lineHeight
;
2717 // TODO make more efficient
2718 unsigned int row
= 0;
2719 unsigned int yy
= 0;
2722 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2725 // not really correct...
2726 return row
+ ((y
-yy
) / m_lineHeight
);
2729 wxDataViewItem item
= node
->GetItem();
2731 unsigned int cols
= GetOwner()->GetColumnCount();
2733 int height
= m_lineHeight
;
2734 for (col
= 0; col
< cols
; col
++)
2736 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2737 if (column
->IsHidden())
2738 continue; // skip it!
2741 model
->IsContainer(item
) &&
2742 !model
->HasContainerColumns(item
))
2743 continue; // skip it!
2745 wxDataViewRenderer
*renderer
=
2746 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2747 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2749 height
= wxMax( height
, renderer
->GetSize().y
);
2760 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2762 const wxDataViewModel
*model
= GetModel();
2764 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2766 wxASSERT( !IsVirtualList() );
2768 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2769 // wxASSERT( node );
2770 if (!node
) return m_lineHeight
;
2772 wxDataViewItem item
= node
->GetItem();
2774 int height
= m_lineHeight
;
2776 unsigned int cols
= GetOwner()->GetColumnCount();
2778 for (col
= 0; col
< cols
; col
++)
2780 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2781 if (column
->IsHidden())
2782 continue; // skip it!
2785 model
->IsContainer(item
) &&
2786 !model
->HasContainerColumns(item
))
2787 continue; // skip it!
2789 wxDataViewRenderer
*renderer
=
2790 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2791 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2793 height
= wxMax( height
, renderer
->GetSize().y
);
2800 return m_lineHeight
;
2805 class RowToTreeNodeJob
: public DoJob
2808 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2811 this->current
= current
;
2816 virtual int operator() ( wxDataViewTreeNode
* node
)
2819 if( current
== static_cast<int>(row
))
2825 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2827 current
+= node
->GetSubTreeCount();
2828 return DoJob::SKIP_SUBTREE
;
2834 // If the current node has only leaf children, we can find the
2835 // desired node directly. This can speed up finding the node
2836 // in some cases, and will have a very good effect for list views.
2837 if ( node
->HasChildren() &&
2838 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2840 const int index
= static_cast<int>(row
) - current
- 1;
2841 ret
= node
->GetChildNodes()[index
];
2845 return DoJob::CONTINUE
;
2849 wxDataViewTreeNode
* GetResult() const
2855 wxDataViewTreeNode
* ret
;
2856 wxDataViewTreeNode
* parent
;
2859 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2861 wxASSERT( !IsVirtualList() );
2863 RowToTreeNodeJob
job( row
, -2, m_root
);
2864 Walker( m_root
, job
);
2865 return job
.GetResult();
2868 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2870 if (IsVirtualList())
2872 return wxDataViewItem( wxUIntToPtr(row
+1) );
2876 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2877 return node
? node
->GetItem() : wxDataViewItem();
2882 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2883 const wxDataViewItem
& item
)
2885 wxWindow
*parent
= GetParent();
2886 wxDataViewEvent
le(type
, parent
->GetId());
2888 le
.SetEventObject(parent
);
2889 le
.SetModel(GetModel());
2892 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2895 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2900 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2904 if (!node
->HasChildren())
2907 return node
->IsOpen();
2910 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2915 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2919 if (!node
->HasChildren())
2925 void wxDataViewMainWindow::Expand( unsigned int row
)
2930 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2934 if (!node
->HasChildren())
2937 if (!node
->IsOpen())
2939 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
2941 // Vetoed by the event handler.
2947 // build the children of current node
2948 if( node
->GetChildNodes().empty() )
2951 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
2954 // By expanding the node all row indices that are currently in the selection list
2955 // and are greater than our node have become invalid. So we have to correct that now.
2956 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2957 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2959 const unsigned testRow
= m_selection
[i
];
2960 // all rows above us are not affected, so skip them
2964 m_selection
[i
] += rowAdjustment
;
2967 if(m_currentRow
> row
)
2968 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2972 // Send the expanded event
2973 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2977 void wxDataViewMainWindow::Collapse(unsigned int row
)
2982 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2986 if (!node
->HasChildren())
2991 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
2993 // Vetoed by the event handler.
2997 // Find out if there are selected items below the current node.
2998 bool selectCollapsingRow
= false;
2999 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3000 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3001 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3003 const unsigned testRow
= m_selection
[i
];
3004 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3006 selectCollapsingRow
= true;
3007 // get out as soon as we have found a node that is selected
3014 // If the node to be closed has selected items the user won't see those any longer.
3015 // We select the collapsing node in this case.
3016 if(selectCollapsingRow
)
3018 SelectAllRows(false);
3019 ChangeCurrentRow(row
);
3020 SelectRow(row
, true);
3021 SendSelectionChangedEvent(GetItemByRow(row
));
3025 // if there were no selected items below our node we still need to "fix" the
3026 // selection list to adjust for the changing of the row indices.
3027 // We actually do the opposite of what we are doing in Expand().
3028 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3030 const unsigned testRow
= m_selection
[i
];
3031 // all rows above us are not affected, so skip them
3035 m_selection
[i
] -= rowAdjustment
;
3038 // if the "current row" is being collapsed away we change it to the current row ;-)
3039 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3040 ChangeCurrentRow(row
);
3041 else if(m_currentRow
> row
)
3042 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3047 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3051 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3053 const wxDataViewModel
* model
= GetModel();
3060 // Compose the parent-chain for the item we are looking for
3061 wxVector
<wxDataViewItem
> parentChain
;
3062 wxDataViewItem
it( item
);
3065 parentChain
.push_back(it
);
3066 it
= model
->GetParent(it
);
3069 // Find the item along the parent-chain.
3070 // This algorithm is designed to speed up the node-finding method
3071 wxDataViewTreeNode
* node
= m_root
;
3072 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3074 if( node
->HasChildren() )
3076 if( node
->GetChildNodes().empty() )
3078 // Even though the item is a container, it doesn't have any
3079 // child nodes in the control's representation yet. We have
3080 // to realize its subtree now.
3082 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3085 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3088 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3090 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3091 if (currentNode
->GetItem() == parentChain
[iter
])
3093 if (currentNode
->GetItem() == item
)
3113 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3114 wxDataViewColumn
* &column
)
3116 wxDataViewColumn
*col
= NULL
;
3117 unsigned int cols
= GetOwner()->GetColumnCount();
3118 unsigned int colnum
= 0;
3120 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3121 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3123 col
= GetOwner()->GetColumnAt(colnum
);
3124 if (col
->IsHidden())
3125 continue; // skip it!
3127 unsigned int w
= col
->GetWidth();
3128 if (x_start
+w
>= (unsigned int)x
)
3135 item
= GetItemByRow( GetLineAt( y
) );
3138 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3139 const wxDataViewColumn
* column
)
3144 unsigned int cols
= GetOwner()->GetColumnCount();
3145 // If column is null the loop will compute the combined width of all columns.
3146 // Otherwise, it will compute the x position of the column we are looking for.
3147 for (unsigned int i
= 0; i
< cols
; i
++)
3149 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3154 if (col
->IsHidden())
3155 continue; // skip it!
3157 xpos
+= col
->GetWidth();
3158 width
+= col
->GetWidth();
3163 // If we have a column, we need can get its width directly.
3164 if(column
->IsHidden())
3167 width
= column
->GetWidth();
3172 // If we have no column, we reset the x position back to zero.
3176 // we have to take an expander column into account and compute its indentation
3177 // to get the correct x position where the actual text is
3179 int row
= GetRowByItem(item
);
3181 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3183 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3184 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3185 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3188 wxRect
itemRect( xpos
+ indent
,
3189 GetLineStart( row
),
3191 GetLineHeight( row
) );
3193 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3194 &itemRect
.x
, &itemRect
.y
);
3199 int wxDataViewMainWindow::RecalculateCount()
3201 if (IsVirtualList())
3203 wxDataViewVirtualListModel
*list_model
=
3204 (wxDataViewVirtualListModel
*) GetModel();
3206 return list_model
->GetCount();
3210 return m_root
->GetSubTreeCount();
3214 class ItemToRowJob
: public DoJob
3217 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3224 // Maybe binary search will help to speed up this process
3225 virtual int operator() ( wxDataViewTreeNode
* node
)
3228 if( node
->GetItem() == item
)
3233 if( node
->GetItem() == *m_iter
)
3236 return DoJob::CONTINUE
;
3240 ret
+= node
->GetSubTreeCount();
3241 return DoJob::SKIP_SUBTREE
;
3246 // the row number is begin from zero
3247 int GetResult() const
3251 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3252 wxDataViewItem item
;
3257 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3259 const wxDataViewModel
* model
= GetModel();
3263 if (IsVirtualList())
3265 return wxPtrToUInt( item
.GetID() ) -1;
3272 // Compose the parent-chain of the item we are looking for
3273 wxVector
<wxDataViewItem
> parentChain
;
3274 wxDataViewItem
it( item
);
3277 parentChain
.push_back(it
);
3278 it
= model
->GetParent(it
);
3281 // add an 'invalid' item to represent our 'invisible' root node
3282 parentChain
.push_back(wxDataViewItem());
3284 // the parent chain was created by adding the deepest parent first.
3285 // so if we want to start at the root node, we have to iterate backwards through the vector
3286 ItemToRowJob
job( item
, parentChain
.rbegin() );
3287 Walker( m_root
, job
);
3288 return job
.GetResult();
3292 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3293 wxDataViewTreeNode
* node
)
3295 if( !model
->IsContainer( item
) )
3298 wxDataViewItemArray children
;
3299 unsigned int num
= model
->GetChildren( item
, children
);
3301 for ( unsigned int index
= 0; index
< num
; index
++ )
3303 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3305 if( model
->IsContainer(children
[index
]) )
3306 n
->SetHasChildren( true );
3308 node
->InsertChild(n
, index
);
3311 wxASSERT( node
->IsOpen() );
3312 node
->ChangeSubTreeCount(+num
);
3315 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3319 if (GetModel()->IsVirtualListModel())
3325 m_root
= wxDataViewTreeNode::CreateRootNode();
3327 // First we define a invalid item to fetch the top-level elements
3328 wxDataViewItem item
;
3330 BuildTreeHelper( model
, item
, m_root
);
3334 void wxDataViewMainWindow::DestroyTree()
3336 if (!IsVirtualList())
3344 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3346 // Edit the current column editable in 'mode'. If no column is focused
3347 // (typically because the user has full row selected), try to find the
3348 // first editable column (this would typically be a checkbox for
3349 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3350 // focus on the checkbox column; or on the only editable text column).
3352 wxDataViewColumn
*candidate
= m_currentCol
;
3355 candidate
->GetRenderer()->GetMode() != mode
&&
3356 !m_currentColSetByKeyboard
)
3358 // If current column was set by mouse to something not editable (in
3359 // 'mode') and the user pressed Space/F2 to edit it, treat the
3360 // situation as if there was whole-row focus, because that's what is
3361 // visually indicated and the mouse click could very well be targeted
3362 // on the row rather than on an individual cell.
3364 // But if it was done by keyboard, respect that even if the column
3365 // isn't editable, because focus is visually on that column and editing
3366 // something else would be surprising.
3372 const unsigned cols
= GetOwner()->GetColumnCount();
3373 for ( unsigned i
= 0; i
< cols
; i
++ )
3375 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3376 if ( c
->IsHidden() )
3379 if ( c
->GetRenderer()->GetMode() == mode
)
3387 // If on container item without columns, only the expander column
3388 // may be directly editable:
3390 GetOwner()->GetExpanderColumn() != candidate
&&
3391 GetModel()->IsContainer(item
) &&
3392 !GetModel()->HasContainerColumns(item
) )
3394 candidate
= GetOwner()->GetExpanderColumn();
3400 if ( candidate
->GetRenderer()->GetMode() != mode
)
3406 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3408 wxWindow
* const parent
= GetParent();
3410 // propagate the char event upwards
3411 wxKeyEvent
eventForParent(event
);
3412 eventForParent
.SetEventObject(parent
);
3413 if ( parent
->ProcessWindowEvent(eventForParent
) )
3416 if ( parent
->HandleAsNavigationKey(event
) )
3419 // no item -> nothing to do
3420 if (!HasCurrentRow())
3426 // don't use m_linesPerPage directly as it might not be computed yet
3427 const int pageSize
= GetCountPerPage();
3428 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3430 switch ( event
.GetKeyCode() )
3434 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3435 // it. Only if that event is not handled do we activate column renderer (which
3436 // is normally done by Space) or even inline editing.
3438 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3440 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3443 le
.SetEventObject(parent
);
3444 le
.SetModel(GetModel());
3446 if ( parent
->ProcessWindowEvent(le
) )
3448 // else: fall through to WXK_SPACE handling
3453 // Space toggles activatable items or -- if not activatable --
3454 // starts inline editing (this is normally done using F2 on
3455 // Windows, but Space is common everywhere else, so use it too
3456 // for greater cross-platform compatibility).
3458 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3460 // Activate the current activatable column. If not column is focused (typically
3461 // because the user has full row selected), try to find the first activatable
3462 // column (this would typically be a checkbox and we don't want to force the user
3463 // to set focus on the checkbox column).
3464 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3466 if ( activatableCol
)
3468 const unsigned colIdx
= activatableCol
->GetModelColumn();
3469 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3471 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3472 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3473 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3477 // else: fall through to WXK_F2 handling
3482 if( !m_selection
.empty() )
3484 // Mimic Windows 7 behavior: edit the item that has focus
3485 // if it is selected and the first selected item if focus
3486 // is out of selection.
3488 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3491 sel
= m_selection
[0];
3494 const wxDataViewItem item
= GetItemByRow(sel
);
3496 // Edit the current column. If no column is focused
3497 // (typically because the user has full row selected), try
3498 // to find the first editable column.
3499 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3502 GetOwner()->StartEditor(item
, GetOwner()->GetColumnIndex(editableCol
));
3508 if ( m_currentRow
> 0 )
3509 OnVerticalNavigation( m_currentRow
- 1, event
);
3513 if ( m_currentRow
+ 1 < GetRowCount() )
3514 OnVerticalNavigation( m_currentRow
+ 1, event
);
3516 // Add the process for tree expanding/collapsing
3528 OnVerticalNavigation( GetRowCount() - 1, event
);
3533 OnVerticalNavigation( 0, event
);
3538 int steps
= pageSize
- 1;
3539 int index
= m_currentRow
- steps
;
3543 OnVerticalNavigation( index
, event
);
3549 int steps
= pageSize
- 1;
3550 unsigned int index
= m_currentRow
+ steps
;
3551 unsigned int count
= GetRowCount();
3552 if ( index
>= count
)
3555 OnVerticalNavigation( index
, event
);
3564 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3566 wxCHECK_RET( newCurrent
< GetRowCount(),
3567 wxT("invalid item index in OnVerticalNavigation()") );
3569 // if there is no selection, we cannot move it anywhere
3570 if (!HasCurrentRow())
3573 unsigned int oldCurrent
= m_currentRow
;
3575 // in single selection we just ignore Shift as we can't select several
3577 if ( event
.ShiftDown() && !IsSingleSel() )
3579 RefreshRow( oldCurrent
);
3581 ChangeCurrentRow( newCurrent
);
3583 // select all the items between the old and the new one
3584 if ( oldCurrent
> newCurrent
)
3586 newCurrent
= oldCurrent
;
3587 oldCurrent
= m_currentRow
;
3590 SelectRows( oldCurrent
, newCurrent
, true );
3591 if (oldCurrent
!=newCurrent
)
3592 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3596 RefreshRow( oldCurrent
);
3598 // all previously selected items are unselected unless ctrl is held
3599 if ( !event
.ControlDown() )
3600 SelectAllRows(false);
3602 ChangeCurrentRow( newCurrent
);
3604 if ( !event
.ControlDown() )
3606 SelectRow( m_currentRow
, true );
3607 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3610 RefreshRow( m_currentRow
);
3613 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3616 void wxDataViewMainWindow::OnLeftKey()
3620 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3624 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3626 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3629 // Because TryAdvanceCurrentColumn() return false, we are at the first
3630 // column or using whole-row selection. In this situation, we can use
3631 // the standard TreeView handling of the left key.
3632 if (node
->HasChildren() && node
->IsOpen())
3634 Collapse(m_currentRow
);
3638 // if the node is already closed, we move the selection to its parent
3639 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3643 int parent
= GetRowByItem( parent_node
->GetItem() );
3646 unsigned int row
= m_currentRow
;
3647 SelectRow( row
, false);
3648 SelectRow( parent
, true );
3649 ChangeCurrentRow( parent
);
3650 GetOwner()->EnsureVisible( parent
, -1 );
3651 SendSelectionChangedEvent( parent_node
->GetItem() );
3658 void wxDataViewMainWindow::OnRightKey()
3662 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3666 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3668 if ( node
->HasChildren() )
3670 if ( !node
->IsOpen() )
3672 Expand( m_currentRow
);
3676 // if the node is already open, we move the selection to the first child
3677 unsigned int row
= m_currentRow
;
3678 SelectRow( row
, false );
3679 SelectRow( row
+ 1, true );
3680 ChangeCurrentRow( row
+ 1 );
3681 GetOwner()->EnsureVisible( row
+ 1, -1 );
3682 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3687 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3692 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3694 if ( GetOwner()->GetColumnCount() == 0 )
3697 if ( !m_useCellFocus
)
3702 // navigation shouldn't work in branch nodes without other columns:
3703 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3707 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3711 m_currentCol
= GetOwner()->GetColumnAt(1);
3712 m_currentColSetByKeyboard
= true;
3713 RefreshRow(m_currentRow
);
3720 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3722 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3727 // We are going to the left of the second column. Reset to whole-row
3728 // focus (which means first column would be edited).
3729 m_currentCol
= NULL
;
3730 RefreshRow(m_currentRow
);
3734 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3735 m_currentColSetByKeyboard
= true;
3736 RefreshRow(m_currentRow
);
3740 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3742 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3744 // let the base handle mouse wheel events.
3749 // set the focus to ourself if any of the mouse buttons are pressed
3750 if(event
.ButtonDown() && !HasFocus())
3753 int x
= event
.GetX();
3754 int y
= event
.GetY();
3755 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3756 wxDataViewColumn
*col
= NULL
;
3759 unsigned int cols
= GetOwner()->GetColumnCount();
3761 for (i
= 0; i
< cols
; i
++)
3763 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3765 continue; // skip it!
3767 if (x
< xpos
+ c
->GetWidth())
3772 xpos
+= c
->GetWidth();
3775 wxDataViewModel
* const model
= GetModel();
3777 const unsigned int current
= GetLineAt( y
);
3778 const wxDataViewItem item
= GetItemByRow(current
);
3780 // Handle right clicking here, before everything else as context menu
3781 // events should be sent even when we click outside of any item, unlike all
3783 if (event
.RightUp())
3785 wxWindow
*parent
= GetParent();
3786 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3787 le
.SetEventObject(parent
);
3790 if ( item
.IsOk() && col
)
3793 le
.SetColumn( col
->GetModelColumn() );
3794 le
.SetDataViewColumn( col
);
3797 model
->GetValue( value
, item
, col
->GetModelColumn() );
3801 parent
->ProcessWindowEvent(le
);
3811 wxDataViewRenderer
*cell
= col
->GetRenderer();
3812 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3814 // Unselect all if below the last row ?
3819 wxDataViewColumn
* const
3820 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3822 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3823 // button) and also determine the offset of the real cell start, skipping
3824 // the indentation and the expander itself.
3825 bool hoverOverExpander
= false;
3827 if ((!IsList()) && (expander
== col
))
3829 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3831 int indent
= node
->GetIndentLevel();
3832 itemOffset
= GetOwner()->GetIndent()*indent
;
3834 if ( node
->HasChildren() )
3836 // we make the rectangle we are looking in a bit bigger than the actual
3837 // visual expander so the user can hit that little thing reliably
3838 wxRect
rect(itemOffset
,
3839 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3840 m_lineHeight
, m_lineHeight
);
3842 if( rect
.Contains(x
, y
) )
3844 // So the mouse is over the expander
3845 hoverOverExpander
= true;
3846 if (m_underMouse
&& m_underMouse
!= node
)
3848 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3849 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3851 if (m_underMouse
!= node
)
3853 // wxLogMessage("Do the row: %d", current);
3854 RefreshRow(current
);
3856 m_underMouse
= node
;
3860 // Account for the expander as well, even if this item doesn't have it,
3861 // its parent does so it still counts for the offset.
3862 itemOffset
+= m_lineHeight
;
3864 if (!hoverOverExpander
)
3866 if (m_underMouse
!= NULL
)
3868 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3869 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3870 m_underMouse
= NULL
;
3874 #if wxUSE_DRAG_AND_DROP
3875 if (event
.Dragging())
3877 if (m_dragCount
== 0)
3879 // we have to report the raw, physical coords as we want to be
3880 // able to call HitTest(event.m_pointDrag) from the user code to
3881 // get the item being dragged
3882 m_dragStart
= event
.GetPosition();
3887 if (m_dragCount
!= 3)
3890 if (event
.LeftIsDown())
3892 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3893 &m_dragStart
.x
, &m_dragStart
.y
);
3894 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3895 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
3897 // Notify cell about drag
3898 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3899 event
.SetEventObject( m_owner
);
3900 event
.SetItem( itemDragged
);
3901 event
.SetModel( model
);
3902 if (!m_owner
->HandleWindowEvent( event
))
3905 if (!event
.IsAllowed())
3908 wxDataObject
*obj
= event
.GetDataObject();
3912 wxDataViewDropSource
drag( this, drag_item_row
);
3913 drag
.SetData( *obj
);
3914 /* wxDragResult res = */ drag
.DoDragDrop();
3923 #endif // wxUSE_DRAG_AND_DROP
3925 bool simulateClick
= false;
3927 if (event
.ButtonDClick())
3929 m_renameTimer
->Stop();
3930 m_lastOnSame
= false;
3933 bool ignore_other_columns
=
3934 ((expander
!= col
) &&
3935 (model
->IsContainer(item
)) &&
3936 (!model
->HasContainerColumns(item
)));
3938 if (event
.LeftDClick())
3940 if(hoverOverExpander
)
3942 // a double click on the expander will be converted into a "simulated" normal click
3943 simulateClick
= true;
3945 else if ( current
== m_lineLastClicked
)
3947 wxWindow
*parent
= GetParent();
3948 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3950 le
.SetColumn( col
->GetModelColumn() );
3951 le
.SetDataViewColumn( col
);
3952 le
.SetEventObject(parent
);
3953 le
.SetModel(GetModel());
3955 parent
->ProcessWindowEvent(le
);
3960 // The first click was on another item, so don't interpret this as
3961 // a double click, but as a simple click instead
3962 simulateClick
= true;
3966 if (event
.LeftUp() && !hoverOverExpander
)
3968 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3970 // select single line
3971 SelectAllRows( false );
3972 SelectRow( m_lineSelectSingleOnUp
, true );
3973 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3976 // If the user click the expander, we do not do editing even if the column
3977 // with expander are editable
3978 if (m_lastOnSame
&& !ignore_other_columns
)
3980 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3981 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3983 m_renameTimer
->Start( 100, true );
3987 m_lastOnSame
= false;
3988 m_lineSelectSingleOnUp
= (unsigned int)-1;
3990 else if(!event
.LeftUp())
3992 // This is necessary, because after a DnD operation in
3993 // from and to ourself, the up event is swallowed by the
3994 // DnD code. So on next non-up event (which means here and
3995 // now) m_lineSelectSingleOnUp should be reset.
3996 m_lineSelectSingleOnUp
= (unsigned int)-1;
3999 if (event
.RightDown())
4001 m_lineBeforeLastClicked
= m_lineLastClicked
;
4002 m_lineLastClicked
= current
;
4004 // If the item is already selected, do not update the selection.
4005 // Multi-selections should not be cleared if a selected item is clicked.
4006 if (!IsRowSelected(current
))
4008 SelectAllRows(false);
4009 const unsigned oldCurrent
= m_currentRow
;
4010 ChangeCurrentRow(current
);
4011 SelectRow(m_currentRow
,true);
4012 RefreshRow(oldCurrent
);
4013 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4016 else if (event
.MiddleDown())
4020 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4022 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4024 // hoverOverExpander being true tells us that our node must be
4025 // valid and have children.
4026 // So we don't need any extra checks.
4027 if( node
->IsOpen() )
4032 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4034 m_lineBeforeLastClicked
= m_lineLastClicked
;
4035 m_lineLastClicked
= current
;
4037 unsigned int oldCurrentRow
= m_currentRow
;
4038 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4040 bool cmdModifierDown
= event
.CmdDown();
4041 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4043 if ( IsSingleSel() || !IsRowSelected(current
) )
4045 SelectAllRows( false );
4046 ChangeCurrentRow(current
);
4047 SelectRow(m_currentRow
,true);
4048 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4050 else // multi sel & current is highlighted & no mod keys
4052 m_lineSelectSingleOnUp
= current
;
4053 ChangeCurrentRow(current
); // change focus
4056 else // multi sel & either ctrl or shift is down
4058 if (cmdModifierDown
)
4060 ChangeCurrentRow(current
);
4061 ReverseRowSelection(m_currentRow
);
4062 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4064 else if (event
.ShiftDown())
4066 ChangeCurrentRow(current
);
4068 unsigned int lineFrom
= oldCurrentRow
,
4071 if ( lineTo
< lineFrom
)
4074 lineFrom
= m_currentRow
;
4077 SelectRows(lineFrom
, lineTo
, true);
4078 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4080 else // !ctrl, !shift
4082 // test in the enclosing if should make it impossible
4083 wxFAIL_MSG( wxT("how did we get here?") );
4087 if (m_currentRow
!= oldCurrentRow
)
4088 RefreshRow( oldCurrentRow
);
4090 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4092 // Update selection here...
4094 m_currentColSetByKeyboard
= false;
4096 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4097 (current
== oldCurrentRow
)) && oldWasSelected
;
4099 // Call ActivateCell() after everything else as under GTK+
4100 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
4102 // notify cell about click
4103 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4105 wxRect
cell_rect( xpos
+ itemOffset
,
4106 GetLineStart( current
),
4107 col
->GetWidth() - itemOffset
,
4108 GetLineHeight( current
) );
4110 // Report position relative to the cell's custom area, i.e.
4111 // no the entire space as given by the control but the one
4112 // used by the renderer after calculation of alignment etc.
4114 // adjust the rectangle ourselves to account for the alignment
4115 wxRect rectItem
= cell_rect
;
4116 const int align
= cell
->GetAlignment();
4117 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4119 const wxSize size
= cell
->GetSize();
4121 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4123 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4124 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4125 else if ( align
& wxALIGN_RIGHT
)
4126 rectItem
.x
+= cell_rect
.width
- size
.x
;
4127 // else: wxALIGN_LEFT is the default
4130 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4132 if ( align
& wxALIGN_CENTER_VERTICAL
)
4133 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4134 else if ( align
& wxALIGN_BOTTOM
)
4135 rectItem
.y
+= cell_rect
.height
- size
.y
;
4136 // else: wxALIGN_TOP is the default
4140 wxMouseEvent
event2(event
);
4141 event2
.m_x
-= rectItem
.x
;
4142 event2
.m_y
-= rectItem
.y
;
4143 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4145 /* ignore ret */ cell
->WXActivateCell
4150 col
->GetModelColumn(),
4157 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4161 if (HasCurrentRow())
4167 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4171 if (HasCurrentRow())
4177 void wxDataViewMainWindow::OnColumnsCountChanged()
4179 int editableCount
= 0;
4181 const unsigned cols
= GetOwner()->GetColumnCount();
4182 for ( unsigned i
= 0; i
< cols
; i
++ )
4184 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4185 if ( c
->IsHidden() )
4187 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4191 m_useCellFocus
= (editableCount
> 1);
4196 //-----------------------------------------------------------------------------
4198 //-----------------------------------------------------------------------------
4200 WX_DEFINE_LIST(wxDataViewColumnList
)
4202 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4203 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4204 EVT_SIZE(wxDataViewCtrl::OnSize
)
4207 wxDataViewCtrl::~wxDataViewCtrl()
4210 GetModel()->RemoveNotifier( m_notifier
);
4213 m_colsBestWidths
.clear();
4216 void wxDataViewCtrl::Init()
4218 m_cols
.DeleteContents(true);
4221 // No sorting column at start
4222 m_sortingColumnIdx
= wxNOT_FOUND
;
4224 m_headerArea
= NULL
;
4226 m_colsDirty
= false;
4229 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4234 const wxValidator
& validator
,
4235 const wxString
& name
)
4237 // if ( (style & wxBORDER_MASK) == 0)
4238 // style |= wxBORDER_SUNKEN;
4242 if (!wxControl::Create( parent
, id
, pos
, size
,
4243 style
| wxScrolledWindowStyle
, validator
, name
))
4246 SetInitialSize(size
);
4249 MacSetClipChildren( true );
4252 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4254 // We use the cursor keys for moving the selection, not scrolling, so call
4255 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4256 // keyboard events forwarded to us from wxListMainWindow.
4257 DisableKeyboardScrolling();
4259 if (HasFlag(wxDV_NO_HEADER
))
4260 m_headerArea
= NULL
;
4262 m_headerArea
= new wxDataViewHeaderWindow(this);
4264 SetTargetWindow( m_clientArea
);
4266 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4268 sizer
->Add( m_headerArea
, 0, wxGROW
);
4269 sizer
->Add( m_clientArea
, 1, wxGROW
);
4275 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4277 return wxBORDER_THEME
;
4281 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4285 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4288 // we need to process arrows ourselves for scrolling
4289 if ( nMsg
== WM_GETDLGCODE
)
4291 rc
|= DLGC_WANTARROWS
;
4299 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4301 wxSize newsize
= size
;
4302 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4303 newsize
.y
-= m_headerArea
->GetSize().y
;
4308 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4310 // We need to override OnSize so that our scrolled
4311 // window a) does call Layout() to use sizers for
4312 // positioning the controls but b) does not query
4313 // the sizer for their size and use that for setting
4314 // the scrollable area as set that ourselves by
4315 // calling SetScrollbar() further down.
4321 // We must redraw the headers if their height changed. Normally this
4322 // shouldn't happen as the control shouldn't let itself be resized beneath
4323 // its minimal height but avoid the display artefacts that appear if it
4324 // does happen, e.g. because there is really not enough vertical space.
4325 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4326 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4328 m_headerArea
->Refresh();
4332 void wxDataViewCtrl::SetFocus()
4335 m_clientArea
->SetFocus();
4338 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4340 if (!wxDataViewCtrlBase::AssociateModel( model
))
4343 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4345 model
->AddNotifier( m_notifier
);
4347 m_clientArea
->DestroyTree();
4349 m_clientArea
->BuildTree(model
);
4351 m_clientArea
->UpdateDisplay();
4356 #if wxUSE_DRAG_AND_DROP
4358 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4360 return m_clientArea
->EnableDragSource( format
);
4363 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4365 return m_clientArea
->EnableDropTarget( format
);
4368 #endif // wxUSE_DRAG_AND_DROP
4370 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4372 if (!wxDataViewCtrlBase::AppendColumn(col
))
4375 m_cols
.Append( col
);
4376 m_colsBestWidths
.push_back(0);
4377 OnColumnsCountChanged();
4381 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4383 if (!wxDataViewCtrlBase::PrependColumn(col
))
4386 m_cols
.Insert( col
);
4387 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4388 OnColumnsCountChanged();
4392 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4394 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4397 m_cols
.Insert( pos
, col
);
4398 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4399 OnColumnsCountChanged();
4403 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4406 m_headerArea
->UpdateColumn(idx
);
4408 m_clientArea
->UpdateDisplay();
4411 void wxDataViewCtrl::OnColumnsCountChanged()
4414 m_headerArea
->SetColumnCount(GetColumnCount());
4416 m_clientArea
->OnColumnsCountChanged();
4419 void wxDataViewCtrl::DoSetExpanderColumn()
4421 m_clientArea
->UpdateDisplay();
4424 void wxDataViewCtrl::DoSetIndent()
4426 m_clientArea
->UpdateDisplay();
4429 unsigned int wxDataViewCtrl::GetColumnCount() const
4431 return m_cols
.GetCount();
4434 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4436 if ( !m_clientArea
)
4439 m_clientArea
->SetRowHeight(lineHeight
);
4444 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4449 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4451 // columns can't be reordered if there is no header window which allows
4453 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4456 return GetColumn(idx
);
4459 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4461 const unsigned count
= m_cols
.size();
4462 for ( unsigned n
= 0; n
< count
; n
++ )
4464 if ( m_cols
[n
] == column
)
4471 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4473 if ( m_colsBestWidths
[idx
] != 0 )
4474 return m_colsBestWidths
[idx
];
4476 const int count
= m_clientArea
->GetRowCount();
4477 wxDataViewColumn
*column
= GetColumn(idx
);
4478 wxDataViewRenderer
*renderer
=
4479 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4481 class MaxWidthCalculator
4484 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4485 wxDataViewRenderer
*renderer
,
4486 const wxDataViewModel
*model
,
4489 m_clientArea(clientArea
),
4490 m_renderer(renderer
),
4496 void UpdateWithWidth(int width
)
4498 m_width
= wxMax(m_width
, width
);
4501 void UpdateWithRow(int row
)
4503 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4504 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4505 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4508 int GetMaxWidth() const { return m_width
; }
4512 wxDataViewMainWindow
*m_clientArea
;
4513 wxDataViewRenderer
*m_renderer
;
4514 const wxDataViewModel
*m_model
;
4518 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4519 GetModel(), column
->GetModelColumn());
4523 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4524 // Labels on native MSW header are indented on both sides
4526 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4527 calculator
.UpdateWithWidth(header_width
);
4530 // The code below deserves some explanation. For very large controls, we
4531 // simply can't afford to calculate sizes for all items, it takes too
4532 // long. So the best we can do is to check the first and the last N/2
4533 // items in the control for some sufficiently large N and calculate best
4534 // sizes from that. That can result in the calculated best width being too
4535 // small for some outliers, but it's better to get slightly imperfect
4536 // result than to wait several seconds after every update. To avoid highly
4537 // visible miscalculations, we also include all currently visible items
4538 // no matter what. Finally, the value of N is determined dynamically by
4539 // measuring how much time we spent on the determining item widths so far.
4542 int top_part_end
= count
;
4543 static const long CALC_TIMEOUT
= 20/*ms*/;
4544 // don't call wxStopWatch::Time() too often
4545 static const unsigned CALC_CHECK_FREQ
= 100;
4548 // use some hard-coded limit, that's the best we can do without timer
4549 int top_part_end
= wxMin(500, count
);
4550 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4554 for ( row
= 0; row
< top_part_end
; row
++ )
4557 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4558 timer
.Time() > CALC_TIMEOUT
)
4560 #endif // wxUSE_STOPWATCH
4561 calculator
.UpdateWithRow(row
);
4564 // row is the first unmeasured item now; that's our value of N/2
4570 // add bottom N/2 items now:
4571 const int bottom_part_start
= wxMax(row
, count
- row
);
4572 for ( row
= bottom_part_start
; row
< count
; row
++ )
4574 calculator
.UpdateWithRow(row
);
4577 // finally, include currently visible items in the calculation:
4578 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4579 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4580 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4582 first_visible
= wxMax(first_visible
, top_part_end
);
4583 last_visible
= wxMin(bottom_part_start
, last_visible
);
4585 for ( row
= first_visible
; row
< last_visible
; row
++ )
4587 calculator
.UpdateWithRow(row
);
4590 wxLogTrace("dataview",
4591 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4593 count
- bottom_part_start
,
4594 wxMax(0, last_visible
- first_visible
),
4598 int max_width
= calculator
.GetMaxWidth();
4599 if ( max_width
> 0 )
4600 max_width
+= 2 * PADDING_RIGHTLEFT
;
4602 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4606 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4607 unsigned int WXUNUSED(new_pos
))
4609 // do _not_ reorder m_cols elements here, they should always be in the
4610 // order in which columns were added, we only display the columns in
4612 m_clientArea
->UpdateDisplay();
4615 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4617 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4621 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4623 OnColumnsCountChanged();
4628 bool wxDataViewCtrl::ClearColumns()
4630 SetExpanderColumn(NULL
);
4632 m_colsBestWidths
.clear();
4633 OnColumnsCountChanged();
4637 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4639 m_colsBestWidths
[idx
] = 0;
4643 void wxDataViewCtrl::InvalidateColBestWidths()
4645 m_colsBestWidths
.clear();
4646 m_colsBestWidths
.resize(m_cols
.size());
4650 void wxDataViewCtrl::UpdateColWidths()
4652 if ( !m_headerArea
)
4655 const unsigned len
= m_colsBestWidths
.size();
4656 for ( unsigned i
= 0; i
< len
; i
++ )
4658 if ( m_colsBestWidths
[i
] == 0 )
4659 m_headerArea
->UpdateColumn(i
);
4663 void wxDataViewCtrl::OnInternalIdle()
4665 wxDataViewCtrlBase::OnInternalIdle();
4669 m_colsDirty
= false;
4674 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4677 unsigned int len
= GetColumnCount();
4678 for ( unsigned int i
= 0; i
< len
; i
++ )
4680 wxDataViewColumn
* col
= GetColumnAt(i
);
4687 // This returns the position in pixels which is not what we want.
4690 unsigned int len
= GetColumnCount();
4691 for ( unsigned int i
= 0; i
< len
; i
++ )
4693 wxDataViewColumn
* col
= GetColumnAt(i
);
4694 if (col
->IsHidden())
4696 ret
+= col
->GetWidth();
4699 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4707 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4709 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4710 : GetColumn(m_sortingColumnIdx
);
4713 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4715 return GetItemByRow(m_clientArea
->GetCurrentRow());
4718 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4720 const int row
= m_clientArea
->GetRowByItem(item
);
4722 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4723 if ( static_cast<unsigned>(row
) != oldCurrent
)
4725 m_clientArea
->ChangeCurrentRow(row
);
4726 m_clientArea
->RefreshRow(oldCurrent
);
4727 m_clientArea
->RefreshRow(row
);
4731 int wxDataViewCtrl::GetSelectedItemsCount() const
4733 return m_clientArea
->GetSelections().size();
4736 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4739 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4741 const size_t len
= selections
.size();
4742 for ( size_t i
= 0; i
< len
; i
++ )
4744 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4751 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4758 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4760 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4762 wxDataViewItem last_parent
;
4764 int len
= sel
.GetCount();
4765 for( int i
= 0; i
< len
; i
++ )
4767 wxDataViewItem item
= sel
[i
];
4768 wxDataViewItem parent
= GetModel()->GetParent( item
);
4771 if (parent
!= last_parent
)
4772 ExpandAncestors(item
);
4775 last_parent
= parent
;
4776 int row
= m_clientArea
->GetRowByItem( item
);
4778 selection
.Add( static_cast<unsigned int>(row
) );
4781 m_clientArea
->SetSelections( selection
);
4784 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4786 ExpandAncestors( item
);
4788 int row
= m_clientArea
->GetRowByItem( item
);
4791 // Unselect all rows before select another in the single select mode
4792 if (m_clientArea
->IsSingleSel())
4793 m_clientArea
->SelectAllRows(false);
4795 m_clientArea
->SelectRow(row
, true);
4797 // Also set focus to the selected item
4798 m_clientArea
->ChangeCurrentRow( row
);
4802 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4804 int row
= m_clientArea
->GetRowByItem( item
);
4806 m_clientArea
->SelectRow(row
, false);
4809 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4811 int row
= m_clientArea
->GetRowByItem( item
);
4814 return m_clientArea
->IsRowSelected(row
);
4819 void wxDataViewCtrl::SelectAll()
4821 m_clientArea
->SelectAllRows(true);
4824 void wxDataViewCtrl::UnselectAll()
4826 m_clientArea
->SelectAllRows(false);
4829 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4833 if( row
> (int) m_clientArea
->GetRowCount() )
4834 row
= m_clientArea
->GetRowCount();
4836 int first
= m_clientArea
->GetFirstVisibleRow();
4837 int last
= m_clientArea
->GetLastVisibleRow();
4839 m_clientArea
->ScrollTo( row
, column
);
4840 else if( row
> last
)
4841 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4843 m_clientArea
->ScrollTo( first
, column
);
4846 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4848 ExpandAncestors( item
);
4850 m_clientArea
->RecalculateDisplay();
4852 int row
= m_clientArea
->GetRowByItem(item
);
4855 if( column
== NULL
)
4856 EnsureVisible(row
, -1);
4858 EnsureVisible( row
, GetColumnIndex(column
) );
4863 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4864 wxDataViewColumn
* &column
) const
4866 m_clientArea
->HitTest(point
, item
, column
);
4869 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4870 const wxDataViewColumn
* column
) const
4872 return m_clientArea
->GetItemRect(item
, column
);
4875 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4877 return m_clientArea
->GetItemByRow( row
);
4880 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4882 return m_clientArea
->GetRowByItem( item
);
4885 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4887 ExpandAncestors( item
);
4889 int row
= m_clientArea
->GetRowByItem( item
);
4891 m_clientArea
->Expand(row
);
4894 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4896 int row
= m_clientArea
->GetRowByItem( item
);
4898 m_clientArea
->Collapse(row
);
4901 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4903 int row
= m_clientArea
->GetRowByItem( item
);
4905 return m_clientArea
->IsExpanded(row
);
4909 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4911 wxDataViewColumn
* col
= GetColumn( column
);
4915 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4916 if (renderer
->GetMode() != wxDATAVIEW_CELL_EDITABLE
)
4919 const wxRect itemRect
= GetItemRect(item
, col
);
4920 renderer
->StartEditing(item
, itemRect
);
4923 #endif // !wxUSE_GENERICDATAVIEWCTRL
4925 #endif // wxUSE_DATAVIEWCTRL