1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
47 #include "wx/imaglist.h"
48 #include "wx/headerctrl.h"
50 #include "wx/stopwatch.h"
51 #include "wx/weakref.h"
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 class wxDataViewColumn
;
58 class wxDataViewHeaderWindow
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static const int SCROLL_UNIT_X
= 15;
67 // the cell padding on the left/right
68 static const int PADDING_RIGHTLEFT
= 3;
70 // the expander space margin
71 static const int EXPANDER_MARGIN
= 4;
74 static const int EXPANDER_OFFSET
= 4;
76 static const int EXPANDER_OFFSET
= 1;
79 // Below is the compare stuff.
80 // For the generic implementation, both the leaf nodes and the nodes are sorted for
81 // fast search when needed
82 static wxDataViewModel
* g_model
;
83 static int g_column
= -2;
84 static bool g_asending
= true;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
93 // Return the expander column or, if it is not set, the first column and also
94 // set it as the expander one for the future.
95 wxDataViewColumn
* GetExpanderColumnOrFirstOne(wxDataViewCtrl
* dataview
)
97 wxDataViewColumn
* expander
= dataview
->GetExpanderColumn();
100 // TODO-RTL: last column for RTL support
101 expander
= dataview
->GetColumnAt( 0 );
102 dataview
->SetExpanderColumn(expander
);
108 } // anonymous namespace
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
121 m_sortAscending
= true;
124 int wxDataViewColumn::GetWidth() const
128 case wxCOL_WIDTH_DEFAULT
:
129 return wxDVC_DEFAULT_WIDTH
;
131 case wxCOL_WIDTH_AUTOSIZE
:
132 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
133 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
140 void wxDataViewColumn::UpdateDisplay()
144 int idx
= m_owner
->GetColumnIndex( this );
145 m_owner
->OnColumnChange( idx
);
149 void wxDataViewColumn::SetSortOrder(bool ascending
)
154 // First unset the old sort column if any.
155 int oldSortKey
= m_owner
->GetSortingColumnIndex();
156 if ( oldSortKey
!= wxNOT_FOUND
)
158 m_owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
161 // Now set this one as the new sort column.
162 const int idx
= m_owner
->GetColumnIndex(this);
163 m_owner
->SetSortingColumnIndex(idx
);
166 m_sortAscending
= ascending
;
168 // Call this directly instead of using UpdateDisplay() as we already have
169 // the column index, no need to look it up again.
170 m_owner
->OnColumnChange(idx
);
173 //-----------------------------------------------------------------------------
174 // wxDataViewHeaderWindow
175 //-----------------------------------------------------------------------------
177 class wxDataViewHeaderWindow
: public wxHeaderCtrl
180 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
181 : wxHeaderCtrl(parent
)
185 wxDataViewCtrl
*GetOwner() const
186 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
189 // implement/override wxHeaderCtrl functions by forwarding them to the main
191 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
193 return *(GetOwner()->GetColumn(idx
));
196 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
198 wxDataViewCtrl
* const owner
= GetOwner();
200 int widthContents
= owner
->GetBestColumnWidth(idx
);
201 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
202 owner
->OnColumnChange(idx
);
208 bool SendEvent(wxEventType type
, unsigned int n
)
210 wxDataViewCtrl
* const owner
= GetOwner();
211 wxDataViewEvent
event(type
, owner
->GetId());
213 event
.SetEventObject(owner
);
215 event
.SetDataViewColumn(owner
->GetColumn(n
));
216 event
.SetModel(owner
->GetModel());
218 // for events created by wxDataViewHeaderWindow the
219 // row / value fields are not valid
220 return owner
->ProcessWindowEvent(event
);
223 void OnClick(wxHeaderCtrlEvent
& event
)
225 const unsigned idx
= event
.GetColumn();
227 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
230 // default handling for the column click is to sort by this column or
231 // toggle its sort order
232 wxDataViewCtrl
* const owner
= GetOwner();
233 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
234 if ( !col
->IsSortable() )
236 // no default handling for non-sortable columns
241 if ( col
->IsSortKey() )
243 // already using this column for sorting, just change the order
244 col
->ToggleSortOrder();
246 else // not using this column for sorting yet
248 col
->SetSortOrder(true);
251 wxDataViewModel
* const model
= owner
->GetModel();
255 owner
->OnColumnChange(idx
);
257 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
260 void OnRClick(wxHeaderCtrlEvent
& event
)
262 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
267 void OnResize(wxHeaderCtrlEvent
& event
)
269 wxDataViewCtrl
* const owner
= GetOwner();
271 const unsigned col
= event
.GetColumn();
272 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
273 GetOwner()->OnColumnChange(col
);
276 void OnEndReorder(wxHeaderCtrlEvent
& event
)
278 wxDataViewCtrl
* const owner
= GetOwner();
279 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
280 event
.GetNewOrder());
283 DECLARE_EVENT_TABLE()
284 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
287 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
288 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
289 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
291 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
292 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
294 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
297 //-----------------------------------------------------------------------------
298 // wxDataViewRenameTimer
299 //-----------------------------------------------------------------------------
301 class wxDataViewRenameTimer
: public wxTimer
304 wxDataViewMainWindow
*m_owner
;
307 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
311 //-----------------------------------------------------------------------------
312 // wxDataViewTreeNode
313 //-----------------------------------------------------------------------------
315 class wxDataViewTreeNode
;
316 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
318 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
319 wxDataViewTreeNode
** node2
);
321 class wxDataViewTreeNode
324 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
331 ~wxDataViewTreeNode()
335 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
336 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
347 static wxDataViewTreeNode
* CreateRootNode()
349 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
350 n
->SetHasChildren(true);
351 n
->m_branchData
->open
= true;
355 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
357 const wxDataViewTreeNodes
& GetChildNodes() const
359 wxASSERT( m_branchData
!= NULL
);
360 return m_branchData
->children
;
363 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
366 m_branchData
= new BranchNodeData
;
368 m_branchData
->children
.Insert(node
, index
);
370 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
372 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
375 void RemoveChild(wxDataViewTreeNode
*node
)
377 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
378 m_branchData
->children
.Remove(node
);
381 // returns position of child node for given item in children list or wxNOT_FOUND
382 int FindChildByItem(const wxDataViewItem
& item
) const
387 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
388 const int len
= nodes
.size();
389 for ( int i
= 0; i
< len
; i
++ )
391 if ( nodes
[i
]->m_item
== item
)
397 const wxDataViewItem
& GetItem() const { return m_item
; }
398 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
400 int GetIndentLevel() const
403 const wxDataViewTreeNode
* node
= this;
404 while( node
->GetParent()->GetParent() != NULL
)
406 node
= node
->GetParent();
414 return m_branchData
&& m_branchData
->open
;
419 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
423 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
424 const int len
= nodes
.GetCount();
425 for ( int i
= 0;i
< len
; i
++)
426 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
428 if (m_branchData
->open
)
430 ChangeSubTreeCount(-sum
);
431 m_branchData
->open
= !m_branchData
->open
;
435 m_branchData
->open
= !m_branchData
->open
;
436 ChangeSubTreeCount(+sum
);
440 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
441 // even if GetChildNodes() is empty; see below.
442 bool HasChildren() const
444 return m_branchData
!= NULL
;
447 void SetHasChildren(bool has
)
451 wxDELETE(m_branchData
);
453 else if ( m_branchData
== NULL
)
455 m_branchData
= new BranchNodeData
;
459 int GetSubTreeCount() const
461 return m_branchData
? m_branchData
->subTreeCount
: 0;
464 void ChangeSubTreeCount( int num
)
466 wxASSERT( m_branchData
!= NULL
);
468 if( !m_branchData
->open
)
471 m_branchData
->subTreeCount
+= num
;
472 wxASSERT( m_branchData
->subTreeCount
>= 0 );
475 m_parent
->ChangeSubTreeCount(num
);
485 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
487 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
488 int len
= nodes
.GetCount();
489 for (int i
= 0; i
< len
; i
++)
491 if ( nodes
[i
]->HasChildren() )
499 wxDataViewTreeNode
*m_parent
;
501 // Corresponding model item.
502 wxDataViewItem m_item
;
504 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
505 // separate struct in order to conserve memory.
506 struct BranchNodeData
514 // Child nodes. Note that this may be empty even if m_hasChildren in
515 // case this branch of the tree wasn't expanded and realized yet.
516 wxDataViewTreeNodes children
;
518 // Is the branch node currently open (expanded)?
521 // Total count of expanded (i.e. visible with the help of some
522 // scrolling) items in the subtree, but excluding this node. I.e. it is
523 // 0 for leaves and is the number of rows the subtree occupies for
528 BranchNodeData
*m_branchData
;
532 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
533 wxDataViewTreeNode
** node2
)
535 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
539 //-----------------------------------------------------------------------------
540 // wxDataViewMainWindow
541 //-----------------------------------------------------------------------------
543 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
545 class wxDataViewMainWindow
: public wxWindow
548 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
550 const wxPoint
&pos
= wxDefaultPosition
,
551 const wxSize
&size
= wxDefaultSize
,
552 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
553 virtual ~wxDataViewMainWindow();
555 bool IsList() const { return GetModel()->IsListModel(); }
556 bool IsVirtualList() const { return m_root
== NULL
; }
558 // notifications from wxDataViewModel
559 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
560 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
561 bool ItemChanged( const wxDataViewItem
&item
);
562 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
566 if (!IsVirtualList())
576 g_model
= GetModel();
577 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
580 if (g_model
->HasDefaultCompare())
588 g_column
= col
->GetModelColumn();
589 g_asending
= col
->IsSortOrderAscending();
592 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
593 wxDataViewCtrl
*GetOwner() { return m_owner
; }
594 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
596 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
597 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
599 #if wxUSE_DRAG_AND_DROP
600 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
601 #endif // wxUSE_DRAG_AND_DROP
602 void OnPaint( wxPaintEvent
&event
);
603 void OnCharHook( wxKeyEvent
&event
);
604 void OnChar( wxKeyEvent
&event
);
605 void OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
);
608 void OnMouse( wxMouseEvent
&event
);
609 void OnSetFocus( wxFocusEvent
&event
);
610 void OnKillFocus( wxFocusEvent
&event
);
612 void UpdateDisplay();
613 void RecalculateDisplay();
614 void OnInternalIdle();
616 void OnRenameTimer();
618 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
619 void ScrollTo( int rows
, int column
);
621 unsigned GetCurrentRow() const { return m_currentRow
; }
622 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
623 void ChangeCurrentRow( unsigned int row
);
624 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
626 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
627 bool IsEmpty() { return GetRowCount() == 0; }
629 int GetCountPerPage() const;
630 int GetEndOfLastCol() const;
631 unsigned int GetFirstVisibleRow() const;
633 // I change this method to un const because in the tree view,
634 // the displaying number of the tree are changing along with the
635 // expanding/collapsing of the tree nodes
636 unsigned int GetLastVisibleRow();
637 unsigned int GetRowCount();
639 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
640 void SetSelections( const wxDataViewSelection
& sel
)
641 { m_selection
= sel
; UpdateDisplay(); }
642 void Select( const wxArrayInt
& aSelections
);
643 void SelectAllRows( bool on
);
644 void SelectRow( unsigned int row
, bool on
);
645 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
646 void ReverseRowSelection( unsigned int row
);
647 bool IsRowSelected( unsigned int row
);
648 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
650 void RefreshRow( unsigned int row
);
651 void RefreshRows( unsigned int from
, unsigned int to
);
652 void RefreshRowsAfter( unsigned int firstRow
);
654 // returns the colour to be used for drawing the rules
655 wxColour
GetRuleColour() const
657 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
660 wxRect
GetLineRect( unsigned int row
) const;
662 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
663 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
664 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
666 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
667 int GetRowHeight() const { return m_lineHeight
; }
669 // Some useful functions for row and item mapping
670 wxDataViewItem
GetItemByRow( unsigned int row
) const;
671 int GetRowByItem( const wxDataViewItem
& item
) const;
673 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
674 // We did not need this temporarily
675 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
677 // Methods for building the mapping tree
678 void BuildTree( wxDataViewModel
* model
);
680 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
681 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
683 void Expand( unsigned int row
);
684 void Collapse( unsigned int row
);
685 bool IsExpanded( unsigned int row
) const;
686 bool HasChildren( unsigned int row
) const;
688 #if wxUSE_DRAG_AND_DROP
689 bool EnableDragSource( const wxDataFormat
&format
);
690 bool EnableDropTarget( const wxDataFormat
&format
);
692 void RemoveDropHint();
693 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
694 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
695 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
697 #endif // wxUSE_DRAG_AND_DROP
699 void OnColumnsCountChanged();
701 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
702 // specified item in the given column.
703 void StartEditing(const wxDataViewItem
& item
, const wxDataViewColumn
* col
);
706 int RecalculateCount();
708 // Return false only if the event was vetoed by its handler.
709 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
711 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
713 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
715 void DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
);
718 wxDataViewCtrl
*m_owner
;
722 wxDataViewColumn
*m_currentCol
;
723 unsigned int m_currentRow
;
724 wxDataViewSelection m_selection
;
726 wxDataViewRenameTimer
*m_renameTimer
;
731 bool m_currentColSetByKeyboard
;
733 #if wxUSE_DRAG_AND_DROP
738 wxDataFormat m_dragFormat
;
741 wxDataFormat m_dropFormat
;
743 unsigned int m_dropHintLine
;
744 #endif // wxUSE_DRAG_AND_DROP
746 // for double click logic
747 unsigned int m_lineLastClicked
,
748 m_lineBeforeLastClicked
,
749 m_lineSelectSingleOnUp
;
751 // the pen used to draw horiz/vertical rules
754 // the pen used to draw the expander and the lines
757 // This is the tree structure of the model
758 wxDataViewTreeNode
* m_root
;
761 // This is the tree node under the cursor
762 wxDataViewTreeNode
* m_underMouse
;
764 // The control used for editing or NULL.
765 wxWeakRef
<wxWindow
> m_editorCtrl
;
767 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
768 wxDataViewRenderer
* m_editorRenderer
;
771 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
772 DECLARE_EVENT_TABLE()
775 // ---------------------------------------------------------
776 // wxGenericDataViewModelNotifier
777 // ---------------------------------------------------------
779 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
782 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
783 { m_mainWindow
= mainWindow
; }
785 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
786 { return m_mainWindow
->ItemAdded( parent
, item
); }
787 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
788 { return m_mainWindow
->ItemDeleted( parent
, item
); }
789 virtual bool ItemChanged( const wxDataViewItem
& item
)
790 { return m_mainWindow
->ItemChanged(item
); }
791 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
792 { return m_mainWindow
->ValueChanged( item
, col
); }
793 virtual bool Cleared()
794 { return m_mainWindow
->Cleared(); }
795 virtual void Resort()
796 { m_mainWindow
->Resort(); }
798 wxDataViewMainWindow
*m_mainWindow
;
801 // ---------------------------------------------------------
802 // wxDataViewRenderer
803 // ---------------------------------------------------------
805 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
807 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
808 wxDataViewCellMode mode
,
810 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
814 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
818 wxDataViewRenderer::~wxDataViewRenderer()
823 wxDC
*wxDataViewRenderer::GetDC()
827 if (GetOwner() == NULL
)
829 if (GetOwner()->GetOwner() == NULL
)
831 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
837 void wxDataViewRenderer::SetAlignment( int align
)
842 int wxDataViewRenderer::GetAlignment() const
847 // ---------------------------------------------------------
848 // wxDataViewCustomRenderer
849 // ---------------------------------------------------------
851 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
853 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
854 wxDataViewCellMode mode
, int align
) :
855 wxDataViewRenderer( varianttype
, mode
, align
)
859 // ---------------------------------------------------------
860 // wxDataViewTextRenderer
861 // ---------------------------------------------------------
863 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
865 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
866 wxDataViewCellMode mode
, int align
) :
867 wxDataViewRenderer( varianttype
, mode
, align
)
871 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
873 m_text
= value
.GetString();
878 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
883 bool wxDataViewTextRenderer::HasEditorCtrl() const
888 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
889 wxRect labelRect
, const wxVariant
&value
)
891 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
892 wxPoint(labelRect
.x
,labelRect
.y
),
893 wxSize(labelRect
.width
,labelRect
.height
),
894 wxTE_PROCESS_ENTER
);
896 // select the text in the control an place the cursor at the end
897 ctrl
->SetInsertionPointEnd();
903 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
905 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
906 value
= text
->GetValue();
910 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
912 RenderText(m_text
, 0, rect
, dc
, state
);
916 wxSize
wxDataViewTextRenderer::GetSize() const
919 return GetTextExtent(m_text
);
921 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
924 // ---------------------------------------------------------
925 // wxDataViewBitmapRenderer
926 // ---------------------------------------------------------
928 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
930 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
931 wxDataViewCellMode mode
, int align
) :
932 wxDataViewRenderer( varianttype
, mode
, align
)
936 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
938 if (value
.GetType() == wxT("wxBitmap"))
940 if (value
.GetType() == wxT("wxIcon"))
946 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
951 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
954 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
955 else if (m_icon
.IsOk())
956 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
961 wxSize
wxDataViewBitmapRenderer::GetSize() const
964 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
965 else if (m_icon
.IsOk())
966 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
968 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
971 // ---------------------------------------------------------
972 // wxDataViewToggleRenderer
973 // ---------------------------------------------------------
975 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
977 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
978 wxDataViewCellMode mode
, int align
) :
979 wxDataViewRenderer( varianttype
, mode
, align
)
984 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
986 m_toggle
= value
.GetBool();
991 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
996 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
1000 flags
|= wxCONTROL_CHECKED
;
1001 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
1002 GetEnabled() == false)
1003 flags
|= wxCONTROL_DISABLED
;
1005 // check boxes we draw must always have the same, standard size (if it's
1006 // bigger than the cell size the checkbox will be truncated because the
1007 // caller had set the clipping rectangle to prevent us from drawing outside
1009 cell
.SetSize(GetSize());
1011 wxRendererNative::Get().DrawCheckBox(
1012 GetOwner()->GetOwner(),
1020 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1021 wxDataViewModel
*model
,
1022 const wxDataViewItem
& item
,
1024 const wxMouseEvent
*mouseEvent
)
1026 if ( !model
->IsEnabled(item
, col
) )
1031 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1032 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1036 model
->ChangeValue(!m_toggle
, item
, col
);
1040 wxSize
wxDataViewToggleRenderer::GetSize() const
1042 // the window parameter is not used by GetCheckBoxSize() so it's
1043 // safe to pass NULL
1044 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1047 // ---------------------------------------------------------
1048 // wxDataViewProgressRenderer
1049 // ---------------------------------------------------------
1051 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1053 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1054 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1055 wxDataViewRenderer( varianttype
, mode
, align
)
1061 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1063 m_value
= (long) value
;
1065 if (m_value
< 0) m_value
= 0;
1066 if (m_value
> 100) m_value
= 100;
1071 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1073 value
= (long) m_value
;
1078 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1080 // deflate the rect to leave a small border between bars in adjacent rows
1081 wxRect bar
= rect
.Deflate(0, 1);
1083 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1084 dc
->SetPen( *wxBLACK_PEN
);
1085 dc
->DrawRectangle( bar
);
1087 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1088 dc
->SetPen( *wxTRANSPARENT_PEN
);
1090 const wxDataViewItemAttr
& attr
= GetAttr();
1091 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1093 dc
->DrawRectangle( bar
);
1098 wxSize
wxDataViewProgressRenderer::GetSize() const
1100 return wxSize(40,12);
1103 // ---------------------------------------------------------
1104 // wxDataViewIconTextRenderer
1105 // ---------------------------------------------------------
1107 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1109 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1110 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1111 wxDataViewRenderer( varianttype
, mode
, align
)
1114 SetAlignment(align
);
1117 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1123 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1128 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1132 const wxIcon
& icon
= m_value
.GetIcon();
1135 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1136 xoffset
= icon
.GetWidth()+4;
1139 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1144 wxSize
wxDataViewIconTextRenderer::GetSize() const
1146 if (!m_value
.GetText().empty())
1148 wxSize size
= GetTextExtent(m_value
.GetText());
1150 if (m_value
.GetIcon().IsOk())
1151 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1154 return wxSize(80,20);
1157 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1159 wxDataViewIconText iconText
;
1162 wxString text
= iconText
.GetText();
1164 // adjust the label rect to take the width of the icon into account
1165 if (iconText
.GetIcon().IsOk())
1167 int w
= iconText
.GetIcon().GetWidth() + 4;
1169 labelRect
.width
-= w
;
1172 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1173 wxPoint(labelRect
.x
,labelRect
.y
),
1174 wxSize(labelRect
.width
,labelRect
.height
),
1175 wxTE_PROCESS_ENTER
);
1177 // select the text in the control an place the cursor at the end
1178 ctrl
->SetInsertionPointEnd();
1184 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1186 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1188 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1193 //-----------------------------------------------------------------------------
1194 // wxDataViewDropTarget
1195 //-----------------------------------------------------------------------------
1197 #if wxUSE_DRAG_AND_DROP
1199 class wxBitmapCanvas
: public wxWindow
1202 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1203 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1206 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1209 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1212 dc
.DrawBitmap( m_bitmap
, 0, 0);
1218 class wxDataViewDropSource
: public wxDropSource
1221 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1229 ~wxDataViewDropSource()
1234 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1236 wxPoint pos
= wxGetMousePosition();
1240 int liney
= m_win
->GetLineStart( m_row
);
1242 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1243 m_win
->ClientToScreen( &linex
, &liney
);
1244 m_dist_x
= pos
.x
- linex
;
1245 m_dist_y
= pos
.y
- liney
;
1248 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1250 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1251 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1253 wxFRAME_TOOL_WINDOW
|
1254 wxFRAME_FLOAT_ON_PARENT
|
1255 wxFRAME_NO_TASKBAR
|
1257 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1262 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1263 m_hint
->SetTransparent( 128 );
1269 wxDataViewMainWindow
*m_win
;
1272 int m_dist_x
,m_dist_y
;
1276 class wxDataViewDropTarget
: public wxDropTarget
1279 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1285 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1287 wxDataFormat format
= GetMatchingPair();
1288 if (format
== wxDF_INVALID
)
1290 return m_win
->OnDragOver( format
, x
, y
, def
);
1293 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1295 wxDataFormat format
= GetMatchingPair();
1296 if (format
== wxDF_INVALID
)
1298 return m_win
->OnDrop( format
, x
, y
);
1301 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1303 wxDataFormat format
= GetMatchingPair();
1304 if (format
== wxDF_INVALID
)
1308 return m_win
->OnData( format
, x
, y
, def
);
1311 virtual void OnLeave()
1312 { m_win
->OnLeave(); }
1314 wxDataViewMainWindow
*m_win
;
1317 #endif // wxUSE_DRAG_AND_DROP
1319 //-----------------------------------------------------------------------------
1320 // wxDataViewRenameTimer
1321 //-----------------------------------------------------------------------------
1323 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1328 void wxDataViewRenameTimer::Notify()
1330 m_owner
->OnRenameTimer();
1333 //-----------------------------------------------------------------------------
1334 // wxDataViewMainWindow
1335 //-----------------------------------------------------------------------------
1337 // The tree building helper, declared firstly
1338 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1339 wxDataViewTreeNode
* node
);
1341 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1343 if (row1
> row2
) return 1;
1344 if (row1
== row2
) return 0;
1348 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1350 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1351 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1352 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1353 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1354 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1355 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook
)
1356 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1359 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1360 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1361 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1362 m_selection( wxDataViewSelectionCmp
)
1367 m_editorRenderer
= NULL
;
1369 m_lastOnSame
= false;
1370 m_renameTimer
= new wxDataViewRenameTimer( this );
1372 // TODO: user better initial values/nothing selected
1373 m_currentCol
= NULL
;
1374 m_currentColSetByKeyboard
= false;
1375 m_useCellFocus
= false;
1378 m_lineHeight
= wxMax( 17, GetCharHeight() + 4 ); // 17 = mini icon height + 1
1380 #if wxUSE_DRAG_AND_DROP
1382 m_dragStart
= wxPoint(0,0);
1384 m_dragEnabled
= false;
1385 m_dropEnabled
= false;
1387 m_dropHintLine
= (unsigned int) -1;
1388 #endif // wxUSE_DRAG_AND_DROP
1390 m_lineLastClicked
= (unsigned int) -1;
1391 m_lineBeforeLastClicked
= (unsigned int) -1;
1392 m_lineSelectSingleOnUp
= (unsigned int) -1;
1396 SetBackgroundColour( *wxWHITE
);
1398 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1400 m_penRule
= wxPen(GetRuleColour());
1402 // compose a pen whichcan draw black lines
1403 // TODO: maybe there is something system colour to use
1404 m_penExpander
= wxPen(wxColour(0,0,0));
1406 m_root
= wxDataViewTreeNode::CreateRootNode();
1408 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1410 m_underMouse
= NULL
;
1414 wxDataViewMainWindow::~wxDataViewMainWindow()
1417 delete m_renameTimer
;
1421 #if wxUSE_DRAG_AND_DROP
1422 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1424 m_dragFormat
= format
;
1425 m_dragEnabled
= format
!= wxDF_INVALID
;
1430 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1432 m_dropFormat
= format
;
1433 m_dropEnabled
= format
!= wxDF_INVALID
;
1436 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1441 void wxDataViewMainWindow::RemoveDropHint()
1446 RefreshRow( m_dropHintLine
);
1447 m_dropHintLine
= (unsigned int) -1;
1451 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1452 wxCoord y
, wxDragResult def
)
1456 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1457 unsigned int row
= GetLineAt( yy
);
1459 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1465 wxDataViewItem item
= GetItemByRow( row
);
1467 wxDataViewModel
*model
= GetModel();
1469 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1470 event
.SetEventObject( m_owner
);
1471 event
.SetItem( item
);
1472 event
.SetModel( model
);
1473 event
.SetDataFormat( format
);
1474 if (!m_owner
->HandleWindowEvent( event
))
1480 if (!event
.IsAllowed())
1487 if (m_dropHint
&& (row
!= m_dropHintLine
))
1488 RefreshRow( m_dropHintLine
);
1490 m_dropHintLine
= row
;
1496 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1502 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1503 unsigned int row
= GetLineAt( yy
);
1505 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1508 wxDataViewItem item
= GetItemByRow( row
);
1510 wxDataViewModel
*model
= GetModel();
1512 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1513 event
.SetEventObject( m_owner
);
1514 event
.SetItem( item
);
1515 event
.SetModel( model
);
1516 event
.SetDataFormat( format
);
1517 if (!m_owner
->HandleWindowEvent( event
))
1520 if (!event
.IsAllowed())
1526 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1531 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1532 unsigned int row
= GetLineAt( yy
);
1534 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1537 wxDataViewItem item
= GetItemByRow( row
);
1539 wxDataViewModel
*model
= GetModel();
1541 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1543 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1544 event
.SetEventObject( m_owner
);
1545 event
.SetItem( item
);
1546 event
.SetModel( model
);
1547 event
.SetDataFormat( format
);
1548 event
.SetDataSize( obj
->GetSize() );
1549 event
.SetDataBuffer( obj
->GetData() );
1550 if (!m_owner
->HandleWindowEvent( event
))
1553 if (!event
.IsAllowed())
1559 void wxDataViewMainWindow::OnLeave()
1564 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1566 int height
= GetLineHeight( row
);
1568 unsigned int cols
= GetOwner()->GetColumnCount();
1570 for (col
= 0; col
< cols
; col
++)
1572 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1573 if (column
->IsHidden())
1574 continue; // skip it!
1575 width
+= column
->GetWidth();
1581 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1582 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1583 indent
= indent
+ m_lineHeight
;
1584 // try to use the m_lineHeight as the expander space
1588 wxBitmap
bitmap( width
, height
);
1589 wxMemoryDC
dc( bitmap
);
1590 dc
.SetFont( GetFont() );
1591 dc
.SetPen( *wxBLACK_PEN
);
1592 dc
.SetBrush( *wxWHITE_BRUSH
);
1593 dc
.DrawRectangle( 0,0,width
,height
);
1595 wxDataViewModel
*model
= m_owner
->GetModel();
1597 wxDataViewColumn
* const
1598 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1601 for (col
= 0; col
< cols
; col
++)
1603 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1604 wxDataViewRenderer
*cell
= column
->GetRenderer();
1606 if (column
->IsHidden())
1607 continue; // skip it!
1609 width
= column
->GetWidth();
1611 if (column
== expander
)
1614 wxDataViewItem item
= GetItemByRow( row
);
1615 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1617 wxRect
item_rect(x
, 0, width
, height
);
1618 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1620 // dc.SetClippingRegion( item_rect );
1621 cell
->WXCallRender(item_rect
, &dc
, 0);
1622 // dc.DestroyClippingRegion();
1630 #endif // wxUSE_DRAG_AND_DROP
1633 // Draw focus rect for individual cell. Unlike native focus rect, we render
1634 // this in foreground text color (typically white) to enhance contrast and
1636 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1638 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1640 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1641 // may be short traits and not really dots
1643 // note that to behave in the same manner as DrawRect(), we must exclude
1644 // the bottom and right borders from the rectangle
1645 wxCoord x1
= rect
.GetLeft(),
1647 x2
= rect
.GetRight(),
1648 y2
= rect
.GetBottom();
1650 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1653 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1654 dc
.DrawPoint(z
, rect
.GetTop());
1656 wxCoord shift
= z
== x2
? 0 : 1;
1657 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1658 dc
.DrawPoint(x2
, z
);
1660 shift
= z
== y2
? 0 : 1;
1661 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1662 dc
.DrawPoint(z
, y2
);
1664 shift
= z
== x1
? 0 : 1;
1665 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1666 dc
.DrawPoint(x1
, z
);
1670 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1672 wxDataViewModel
*model
= GetModel();
1673 wxAutoBufferedPaintDC
dc( this );
1676 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1677 dc
.SetPen( *wxTRANSPARENT_PEN
);
1678 dc
.DrawRectangle(GetClientSize());
1683 // No items to draw.
1688 GetOwner()->PrepareDC( dc
);
1689 dc
.SetFont( GetFont() );
1691 wxRect update
= GetUpdateRegion().GetBox();
1692 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1694 // compute which items needs to be redrawn
1695 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1696 unsigned int item_count
=
1697 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1698 (int)(GetRowCount( ) - item_start
));
1699 unsigned int item_last
= item_start
+ item_count
;
1701 // Send the event to wxDataViewCtrl itself.
1702 wxWindow
* const parent
= GetParent();
1703 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1704 cache_event
.SetEventObject(parent
);
1705 cache_event
.SetCache(item_start
, item_last
- 1);
1706 parent
->ProcessWindowEvent(cache_event
);
1708 // compute which columns needs to be redrawn
1709 unsigned int cols
= GetOwner()->GetColumnCount();
1712 // we assume that we have at least one column below and painting an
1713 // empty control is unnecessary anyhow
1717 unsigned int col_start
= 0;
1718 unsigned int x_start
;
1719 for (x_start
= 0; col_start
< cols
; col_start
++)
1721 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1722 if (col
->IsHidden())
1723 continue; // skip it!
1725 unsigned int w
= col
->GetWidth();
1726 if (x_start
+w
>= (unsigned int)update
.x
)
1732 unsigned int col_last
= col_start
;
1733 unsigned int x_last
= x_start
;
1734 for (; col_last
< cols
; col_last
++)
1736 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1737 if (col
->IsHidden())
1738 continue; // skip it!
1740 if (x_last
> (unsigned int)update
.GetRight())
1743 x_last
+= col
->GetWidth();
1746 // Draw horizontal rules if required
1747 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1749 dc
.SetPen(m_penRule
);
1750 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1752 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1754 int y
= GetLineStart( i
);
1755 dc
.DrawLine(x_start
, y
, x_last
, y
);
1759 // Draw vertical rules if required
1760 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1762 dc
.SetPen(m_penRule
);
1763 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1765 // NB: Vertical rules are drawn in the last pixel of a column so that
1766 // they align perfectly with native MSW wxHeaderCtrl as well as for
1767 // consistency with MSW native list control. There's no vertical
1768 // rule at the most-left side of the control.
1770 int x
= x_start
- 1;
1771 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1773 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1774 if (col
->IsHidden())
1775 continue; // skip it
1777 x
+= col
->GetWidth();
1779 dc
.DrawLine(x
, GetLineStart( item_start
),
1780 x
, GetLineStart( item_last
) );
1784 // redraw the background for the items which are selected/current
1785 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1787 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1789 if (selected
|| item
== m_currentRow
)
1791 wxRect
rect( x_start
, GetLineStart( item
),
1792 x_last
- x_start
, GetLineHeight( item
) );
1794 // draw selection and whole-item focus:
1797 int flags
= wxCONTROL_SELECTED
;
1799 flags
|= wxCONTROL_FOCUSED
;
1801 wxRendererNative::Get().DrawItemSelectionRect
1810 // draw keyboard focus rect if applicable
1811 if ( item
== m_currentRow
&& m_hasFocus
)
1813 bool renderColumnFocus
= false;
1815 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1817 renderColumnFocus
= true;
1819 // If this is container node without columns, render full-row focus:
1822 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1823 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1824 renderColumnFocus
= false;
1828 if ( renderColumnFocus
)
1830 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1832 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1833 if ( col
->IsHidden() )
1836 rect
.width
= col
->GetWidth();
1838 if ( col
== m_currentCol
)
1840 // make the rect more visible by adding a small
1841 // margin around it:
1846 // DrawFocusRect() uses XOR and is all but
1847 // invisible against dark-blue background. Use
1848 // the same color used for selected text.
1849 DrawSelectedCellFocusRect(dc
, rect
);
1853 wxRendererNative::Get().DrawFocusRect
1864 rect
.x
+= rect
.width
;
1869 // render focus rectangle for the whole row
1870 wxRendererNative::Get().DrawFocusRect
1875 selected
? (int)wxCONTROL_SELECTED
: 0
1882 #if wxUSE_DRAG_AND_DROP
1885 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1886 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1887 dc
.SetPen( *wxBLACK_PEN
);
1888 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1889 dc
.DrawRectangle( rect
);
1891 #endif // wxUSE_DRAG_AND_DROP
1893 wxDataViewColumn
* const
1894 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1896 // redraw all cells for all rows which must be repainted and all columns
1898 cell_rect
.x
= x_start
;
1899 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1901 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1902 wxDataViewRenderer
*cell
= col
->GetRenderer();
1903 cell_rect
.width
= col
->GetWidth();
1905 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1906 continue; // skip it!
1908 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1910 // get the cell value and set it into the renderer
1911 wxDataViewTreeNode
*node
= NULL
;
1912 wxDataViewItem dataitem
;
1914 if (!IsVirtualList())
1916 node
= GetTreeNodeByRow(item
);
1920 dataitem
= node
->GetItem();
1922 // Skip all columns of "container" rows except the expander
1923 // column itself unless HasContainerColumns() overrides this.
1924 if ( col
!= expander
&&
1925 model
->IsContainer(dataitem
) &&
1926 !model
->HasContainerColumns(dataitem
) )
1931 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1934 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1937 cell_rect
.y
= GetLineStart( item
);
1938 cell_rect
.height
= GetLineHeight( item
);
1940 // draw the background
1941 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1943 DrawCellBackground( cell
, dc
, cell_rect
);
1945 // deal with the expander
1947 if ((!IsList()) && (col
== expander
))
1949 // Calculate the indent first
1950 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1952 // we reserve m_lineHeight of horizontal space for the expander
1953 // but leave EXPANDER_MARGIN around the expander itself
1954 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1956 indent
+= m_lineHeight
;
1958 // draw expander if needed and visible
1959 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1961 dc
.SetPen( m_penExpander
);
1962 dc
.SetBrush( wxNullBrush
);
1964 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1965 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1966 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1968 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1971 if ( m_underMouse
== node
)
1972 flag
|= wxCONTROL_CURRENT
;
1973 if ( node
->IsOpen() )
1974 flag
|= wxCONTROL_EXPANDED
;
1976 // ensure that we don't overflow the cell (which might
1977 // happen if the column is very narrow)
1978 wxDCClipper
clip(dc
, cell_rect
);
1980 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1983 // force the expander column to left-center align
1984 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1987 wxRect item_rect
= cell_rect
;
1988 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1990 // account for the tree indent (harmless if we're not indented)
1991 item_rect
.x
+= indent
;
1992 item_rect
.width
-= indent
;
1994 if ( item_rect
.width
<= 0 )
1998 if (m_hasFocus
&& selected
)
1999 state
|= wxDATAVIEW_CELL_SELECTED
;
2001 // TODO: it would be much more efficient to create a clipping
2002 // region for the entire column being rendered (in the OnPaint
2003 // of wxDataViewMainWindow) instead of a single clip region for
2004 // each cell. However it would mean that each renderer should
2005 // respect the given wxRect's top & bottom coords, eventually
2006 // violating only the left & right coords - however the user can
2007 // make its own renderer and thus we cannot be sure of that.
2008 wxDCClipper
clip(dc
, item_rect
);
2010 cell
->WXCallRender(item_rect
, &dc
, state
);
2013 cell_rect
.x
+= cell_rect
.width
;
2018 void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
)
2020 wxRect
rectBg( rect
);
2022 // don't overlap the horizontal rules
2023 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2029 // don't overlap the vertical rules
2030 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2036 cell
->RenderBackground(&dc
, rectBg
);
2039 void wxDataViewMainWindow::OnRenameTimer()
2041 // We have to call this here because changes may just have
2042 // been made and no screen update taken place.
2045 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2046 // (needs to be tested!)
2050 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2052 StartEditing( item
, m_currentCol
);
2056 wxDataViewMainWindow::StartEditing(const wxDataViewItem
& item
,
2057 const wxDataViewColumn
* col
)
2059 wxDataViewRenderer
* renderer
= col
->GetRenderer();
2060 if (renderer
->GetMode() != wxDATAVIEW_CELL_EDITABLE
)
2063 const wxRect itemRect
= GetItemRect(item
, col
);
2064 if ( renderer
->StartEditing(item
, itemRect
) )
2066 // Save the renderer to be able to finish/cancel editing it later and
2067 // save the control to be able to detect if we're still editing it.
2068 m_editorRenderer
= renderer
;
2069 m_editorCtrl
= renderer
->GetEditorCtrl();
2073 //-----------------------------------------------------------------------------
2074 // Helper class for do operation on the tree node
2075 //-----------------------------------------------------------------------------
2080 virtual ~DoJob() { }
2082 // The return value control how the tree-walker tranverse the tree
2085 DONE
, // Job done, stop traversing and return
2086 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2087 CONTINUE
// Job not done, continue
2090 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2093 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2095 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2097 switch( func( node
) )
2101 case DoJob::SKIP_SUBTREE
:
2103 case DoJob::CONTINUE
:
2107 if ( node
->HasChildren() )
2109 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2111 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2115 if ( Walker(*i
, func
) )
2123 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2125 if (IsVirtualList())
2127 wxDataViewVirtualListModel
*list_model
=
2128 (wxDataViewVirtualListModel
*) GetModel();
2129 m_count
= list_model
->GetCount();
2135 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2140 wxDataViewItemArray modelSiblings
;
2141 GetModel()->GetChildren(parent
, modelSiblings
);
2142 const int modelSiblingsSize
= modelSiblings
.size();
2144 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2145 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2147 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2148 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2150 parentNode
->SetHasChildren(true);
2152 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2153 const int nodeSiblingsSize
= nodeSiblings
.size();
2157 if ( posInModel
== modelSiblingsSize
- 1 )
2159 nodePos
= nodeSiblingsSize
;
2161 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2163 // This is the simple case when our node tree already matches the
2164 // model and only this one item is missing.
2165 nodePos
= posInModel
;
2169 // It's possible that a larger discrepancy between the model and
2170 // our realization exists. This can happen e.g. when adding a bunch
2171 // of items to the model and then calling ItemsAdded() just once
2172 // afterwards. In this case, we must find the right position by
2173 // looking at sibling items.
2175 // append to the end if we won't find a better position:
2176 nodePos
= nodeSiblingsSize
;
2178 for ( int nextItemPos
= posInModel
+ 1;
2179 nextItemPos
< modelSiblingsSize
;
2182 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2183 if ( nextNodePos
!= wxNOT_FOUND
)
2185 nodePos
= nextNodePos
;
2191 parentNode
->ChangeSubTreeCount(+1);
2192 parentNode
->InsertChild(itemNode
, nodePos
);
2197 GetOwner()->InvalidateColBestWidths();
2203 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2204 const wxDataViewItem
& item
)
2206 if (IsVirtualList())
2208 wxDataViewVirtualListModel
*list_model
=
2209 (wxDataViewVirtualListModel
*) GetModel();
2210 m_count
= list_model
->GetCount();
2212 if ( !m_selection
.empty() )
2214 const int row
= GetRowByItem(item
);
2216 int rowIndexInSelection
= wxNOT_FOUND
;
2218 const size_t selCount
= m_selection
.size();
2219 for ( size_t i
= 0; i
< selCount
; i
++ )
2221 if ( m_selection
[i
] == (unsigned)row
)
2222 rowIndexInSelection
= i
;
2223 else if ( m_selection
[i
] > (unsigned)row
)
2227 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2228 m_selection
.RemoveAt(rowIndexInSelection
);
2232 else // general case
2234 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2236 // Notice that it is possible that the item being deleted is not in the
2237 // tree at all, for example we could be deleting a never shown (because
2238 // collapsed) item in a tree model. So it's not an error if we don't know
2239 // about this item, just return without doing anything then.
2243 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2244 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2246 // We can't use FindNode() to find 'item', because it was already
2247 // removed from the model by the time ItemDeleted() is called, so we
2248 // have to do it manually. We keep track of its position as well for
2250 int itemPosInNode
= 0;
2251 wxDataViewTreeNode
*itemNode
= NULL
;
2252 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2253 i
!= parentsChildren
.end();
2254 ++i
, ++itemPosInNode
)
2256 if( (*i
)->GetItem() == item
)
2263 // If the parent wasn't expanded, it's possible that we didn't have a
2264 // node corresponding to 'item' and so there's nothing left to do.
2267 // If this was the last child to be removed, it's possible the parent
2268 // node became a leaf. Let's ask the model about it.
2269 if ( parentNode
->GetChildNodes().empty() )
2270 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2275 // Delete the item from wxDataViewTreeNode representation:
2276 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2278 parentNode
->RemoveChild(itemNode
);
2280 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2282 // Make the row number invalid and get a new valid one when user call GetRowCount
2285 // If this was the last child to be removed, it's possible the parent
2286 // node became a leaf. Let's ask the model about it.
2287 if ( parentNode
->GetChildNodes().empty() )
2288 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2290 // Update selection by removing 'item' and its entire children tree from the selection.
2291 if ( !m_selection
.empty() )
2293 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2294 // the parent ('parentNode') and position in its list of children
2296 if ( itemPosInNode
== 0 )
2298 // 1st child, row number is that of the parent parentNode + 1
2299 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2303 // row number is that of the sibling above 'item' + its subtree if any + 1
2304 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2306 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2307 siblingNode
->GetSubTreeCount() +
2311 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2313 const size_t numSelections
= m_selection
.size();
2314 for ( size_t i
= 0; i
< numSelections
; ++i
)
2316 const int s
= m_selection
[i
];
2318 newsel
.push_back(s
);
2319 else if ( s
>= itemRow
+ itemsDeleted
)
2320 newsel
.push_back(s
- itemsDeleted
);
2321 // else: deleted item, remove from selection
2324 m_selection
= newsel
;
2328 // Change the current row to the last row if the current exceed the max row number
2329 if( m_currentRow
> GetRowCount() )
2330 ChangeCurrentRow(m_count
- 1);
2332 GetOwner()->InvalidateColBestWidths();
2338 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2343 GetOwner()->InvalidateColBestWidths();
2346 wxWindow
*parent
= GetParent();
2347 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2348 le
.SetEventObject(parent
);
2349 le
.SetModel(GetModel());
2351 parent
->ProcessWindowEvent(le
);
2356 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2358 int view_column
= -1;
2359 unsigned int n_col
= m_owner
->GetColumnCount();
2360 for (unsigned i
= 0; i
< n_col
; i
++)
2362 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2363 if (column
->GetModelColumn() == model_column
)
2365 view_column
= (int) i
;
2369 if (view_column
== -1)
2372 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2373 /*#define MAX_VIRTUAL_WIDTH 100000
2375 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2376 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2377 Refresh( true, &rect );
2384 GetOwner()->InvalidateColBestWidth(view_column
);
2387 wxWindow
*parent
= GetParent();
2388 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2389 le
.SetEventObject(parent
);
2390 le
.SetModel(GetModel());
2392 le
.SetColumn(view_column
);
2393 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2394 parent
->ProcessWindowEvent(le
);
2399 bool wxDataViewMainWindow::Cleared()
2402 m_selection
.Clear();
2405 BuildTree( GetModel() );
2407 GetOwner()->InvalidateColBestWidths();
2413 void wxDataViewMainWindow::UpdateDisplay()
2416 m_underMouse
= NULL
;
2419 void wxDataViewMainWindow::OnInternalIdle()
2421 wxWindow::OnInternalIdle();
2425 RecalculateDisplay();
2430 void wxDataViewMainWindow::RecalculateDisplay()
2432 wxDataViewModel
*model
= GetModel();
2439 int width
= GetEndOfLastCol();
2440 int height
= GetLineStart( GetRowCount() );
2442 SetVirtualSize( width
, height
);
2443 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2448 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2450 m_underMouse
= NULL
;
2452 wxWindow::ScrollWindow( dx
, dy
, rect
);
2454 if (GetOwner()->m_headerArea
)
2455 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2458 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2460 m_underMouse
= NULL
;
2463 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2464 int sy
= GetLineStart( rows
)/y
;
2468 wxRect rect
= GetClientRect();
2472 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2473 for (x_start
= 0; colnum
< column
; colnum
++)
2475 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2476 if (col
->IsHidden())
2477 continue; // skip it!
2479 w
= col
->GetWidth();
2483 int x_end
= x_start
+ w
;
2484 xe
= xx
+ rect
.width
;
2487 sx
= ( xx
+ x_end
- xe
)/x
;
2494 m_owner
->Scroll( sx
, sy
);
2497 int wxDataViewMainWindow::GetCountPerPage() const
2499 wxSize size
= GetClientSize();
2500 return size
.y
/ m_lineHeight
;
2503 int wxDataViewMainWindow::GetEndOfLastCol() const
2507 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2509 const wxDataViewColumn
*c
=
2510 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2513 width
+= c
->GetWidth();
2518 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2522 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2524 return GetLineAt( y
);
2527 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2529 wxSize client_size
= GetClientSize();
2530 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2531 &client_size
.x
, &client_size
.y
);
2533 // we should deal with the pixel here
2534 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2536 return wxMin( GetRowCount()-1, row
);
2539 unsigned int wxDataViewMainWindow::GetRowCount()
2541 if ( m_count
== -1 )
2543 m_count
= RecalculateCount();
2549 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2556 void wxDataViewMainWindow::SelectAllRows( bool on
)
2563 m_selection
.Clear();
2564 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2565 m_selection
.Add( i
);
2570 unsigned int first_visible
= GetFirstVisibleRow();
2571 unsigned int last_visible
= GetLastVisibleRow();
2573 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2575 unsigned int row
= m_selection
[i
];
2576 if ((row
>= first_visible
) && (row
<= last_visible
))
2579 m_selection
.Clear();
2583 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2585 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2589 m_selection
.Add( row
);
2597 m_selection
.Remove( row
);
2603 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2607 unsigned int tmp
= from
;
2613 for (i
= from
; i
<= to
; i
++)
2615 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2618 m_selection
.Add( i
);
2623 m_selection
.Remove( i
);
2626 RefreshRows( from
, to
);
2629 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2631 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2633 int n
= aSelections
[i
];
2635 m_selection
.Add( n
);
2640 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2642 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2643 m_selection
.Add( row
);
2645 m_selection
.Remove( row
);
2649 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2651 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2654 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2656 wxWindow
*parent
= GetParent();
2657 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2659 le
.SetEventObject(parent
);
2660 le
.SetModel(GetModel());
2663 parent
->ProcessWindowEvent(le
);
2666 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2668 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2669 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2671 wxSize client_size
= GetClientSize();
2672 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2673 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2674 if (intersect_rect
.width
> 0)
2675 Refresh( true, &intersect_rect
);
2678 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2682 unsigned int tmp
= to
;
2687 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2688 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2690 wxSize client_size
= GetClientSize();
2691 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2692 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2693 if (intersect_rect
.width
> 0)
2694 Refresh( true, &intersect_rect
);
2697 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2699 wxSize client_size
= GetClientSize();
2700 int start
= GetLineStart( firstRow
);
2701 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2702 if (start
> client_size
.y
) return;
2704 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2706 Refresh( true, &rect
);
2709 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2713 rect
.y
= GetLineStart( row
);
2714 rect
.width
= GetEndOfLastCol();
2715 rect
.height
= GetLineHeight( row
);
2720 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2722 const wxDataViewModel
*model
= GetModel();
2724 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2726 // TODO make more efficient
2731 for (r
= 0; r
< row
; r
++)
2733 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2734 if (!node
) return start
;
2736 wxDataViewItem item
= node
->GetItem();
2738 unsigned int cols
= GetOwner()->GetColumnCount();
2740 int height
= m_lineHeight
;
2741 for (col
= 0; col
< cols
; col
++)
2743 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2744 if (column
->IsHidden())
2745 continue; // skip it!
2748 model
->IsContainer(item
) &&
2749 !model
->HasContainerColumns(item
))
2750 continue; // skip it!
2752 wxDataViewRenderer
*renderer
=
2753 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2754 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2756 height
= wxMax( height
, renderer
->GetSize().y
);
2766 return row
* m_lineHeight
;
2770 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2772 const wxDataViewModel
*model
= GetModel();
2774 // check for the easy case first
2775 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2776 return y
/ m_lineHeight
;
2778 // TODO make more efficient
2779 unsigned int row
= 0;
2780 unsigned int yy
= 0;
2783 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2786 // not really correct...
2787 return row
+ ((y
-yy
) / m_lineHeight
);
2790 wxDataViewItem item
= node
->GetItem();
2792 unsigned int cols
= GetOwner()->GetColumnCount();
2794 int height
= m_lineHeight
;
2795 for (col
= 0; col
< cols
; col
++)
2797 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2798 if (column
->IsHidden())
2799 continue; // skip it!
2802 model
->IsContainer(item
) &&
2803 !model
->HasContainerColumns(item
))
2804 continue; // skip it!
2806 wxDataViewRenderer
*renderer
=
2807 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2808 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2810 height
= wxMax( height
, renderer
->GetSize().y
);
2821 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2823 const wxDataViewModel
*model
= GetModel();
2825 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2827 wxASSERT( !IsVirtualList() );
2829 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2830 // wxASSERT( node );
2831 if (!node
) return m_lineHeight
;
2833 wxDataViewItem item
= node
->GetItem();
2835 int height
= m_lineHeight
;
2837 unsigned int cols
= GetOwner()->GetColumnCount();
2839 for (col
= 0; col
< cols
; col
++)
2841 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2842 if (column
->IsHidden())
2843 continue; // skip it!
2846 model
->IsContainer(item
) &&
2847 !model
->HasContainerColumns(item
))
2848 continue; // skip it!
2850 wxDataViewRenderer
*renderer
=
2851 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2852 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2854 height
= wxMax( height
, renderer
->GetSize().y
);
2861 return m_lineHeight
;
2866 class RowToTreeNodeJob
: public DoJob
2869 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2872 this->current
= current
;
2877 virtual int operator() ( wxDataViewTreeNode
* node
)
2880 if( current
== static_cast<int>(row
))
2886 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2888 current
+= node
->GetSubTreeCount();
2889 return DoJob::SKIP_SUBTREE
;
2895 // If the current node has only leaf children, we can find the
2896 // desired node directly. This can speed up finding the node
2897 // in some cases, and will have a very good effect for list views.
2898 if ( node
->HasChildren() &&
2899 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2901 const int index
= static_cast<int>(row
) - current
- 1;
2902 ret
= node
->GetChildNodes()[index
];
2906 return DoJob::CONTINUE
;
2910 wxDataViewTreeNode
* GetResult() const
2916 wxDataViewTreeNode
* ret
;
2917 wxDataViewTreeNode
* parent
;
2920 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2922 wxASSERT( !IsVirtualList() );
2924 RowToTreeNodeJob
job( row
, -2, m_root
);
2925 Walker( m_root
, job
);
2926 return job
.GetResult();
2929 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2931 if (IsVirtualList())
2933 return wxDataViewItem( wxUIntToPtr(row
+1) );
2937 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2938 return node
? node
->GetItem() : wxDataViewItem();
2943 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2944 const wxDataViewItem
& item
)
2946 wxWindow
*parent
= GetParent();
2947 wxDataViewEvent
le(type
, parent
->GetId());
2949 le
.SetEventObject(parent
);
2950 le
.SetModel(GetModel());
2953 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2956 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2961 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2965 if (!node
->HasChildren())
2968 return node
->IsOpen();
2971 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2976 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2980 if (!node
->HasChildren())
2986 void wxDataViewMainWindow::Expand( unsigned int row
)
2991 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2995 if (!node
->HasChildren())
2998 if (!node
->IsOpen())
3000 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
3002 // Vetoed by the event handler.
3008 // build the children of current node
3009 if( node
->GetChildNodes().empty() )
3012 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
3015 // By expanding the node all row indices that are currently in the selection list
3016 // and are greater than our node have become invalid. So we have to correct that now.
3017 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3018 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3020 const unsigned testRow
= m_selection
[i
];
3021 // all rows above us are not affected, so skip them
3025 m_selection
[i
] += rowAdjustment
;
3028 if(m_currentRow
> row
)
3029 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3033 // Send the expanded event
3034 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3038 void wxDataViewMainWindow::Collapse(unsigned int row
)
3043 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3047 if (!node
->HasChildren())
3052 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3054 // Vetoed by the event handler.
3058 // Find out if there are selected items below the current node.
3059 bool selectCollapsingRow
= false;
3060 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3061 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3062 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3064 const unsigned testRow
= m_selection
[i
];
3065 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3067 selectCollapsingRow
= true;
3068 // get out as soon as we have found a node that is selected
3075 // If the node to be closed has selected items the user won't see those any longer.
3076 // We select the collapsing node in this case.
3077 if(selectCollapsingRow
)
3079 SelectAllRows(false);
3080 ChangeCurrentRow(row
);
3081 SelectRow(row
, true);
3082 SendSelectionChangedEvent(GetItemByRow(row
));
3086 // if there were no selected items below our node we still need to "fix" the
3087 // selection list to adjust for the changing of the row indices.
3088 // We actually do the opposite of what we are doing in Expand().
3089 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3091 const unsigned testRow
= m_selection
[i
];
3092 // all rows above us are not affected, so skip them
3096 m_selection
[i
] -= rowAdjustment
;
3099 // if the "current row" is being collapsed away we change it to the current row ;-)
3100 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3101 ChangeCurrentRow(row
);
3102 else if(m_currentRow
> row
)
3103 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3108 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3112 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3114 const wxDataViewModel
* model
= GetModel();
3121 // Compose the parent-chain for the item we are looking for
3122 wxVector
<wxDataViewItem
> parentChain
;
3123 wxDataViewItem
it( item
);
3126 parentChain
.push_back(it
);
3127 it
= model
->GetParent(it
);
3130 // Find the item along the parent-chain.
3131 // This algorithm is designed to speed up the node-finding method
3132 wxDataViewTreeNode
* node
= m_root
;
3133 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3135 if( node
->HasChildren() )
3137 if( node
->GetChildNodes().empty() )
3139 // Even though the item is a container, it doesn't have any
3140 // child nodes in the control's representation yet. We have
3141 // to realize its subtree now.
3143 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3146 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3149 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3151 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3152 if (currentNode
->GetItem() == parentChain
[iter
])
3154 if (currentNode
->GetItem() == item
)
3174 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3175 wxDataViewColumn
* &column
)
3177 wxDataViewColumn
*col
= NULL
;
3178 unsigned int cols
= GetOwner()->GetColumnCount();
3179 unsigned int colnum
= 0;
3181 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3182 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3184 col
= GetOwner()->GetColumnAt(colnum
);
3185 if (col
->IsHidden())
3186 continue; // skip it!
3188 unsigned int w
= col
->GetWidth();
3189 if (x_start
+w
>= (unsigned int)x
)
3196 item
= GetItemByRow( GetLineAt( y
) );
3199 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3200 const wxDataViewColumn
* column
)
3205 unsigned int cols
= GetOwner()->GetColumnCount();
3206 // If column is null the loop will compute the combined width of all columns.
3207 // Otherwise, it will compute the x position of the column we are looking for.
3208 for (unsigned int i
= 0; i
< cols
; i
++)
3210 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3215 if (col
->IsHidden())
3216 continue; // skip it!
3218 xpos
+= col
->GetWidth();
3219 width
+= col
->GetWidth();
3224 // If we have a column, we need can get its width directly.
3225 if(column
->IsHidden())
3228 width
= column
->GetWidth();
3233 // If we have no column, we reset the x position back to zero.
3237 // we have to take an expander column into account and compute its indentation
3238 // to get the correct x position where the actual text is
3240 int row
= GetRowByItem(item
);
3242 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3244 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3245 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3246 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3249 wxRect
itemRect( xpos
+ indent
,
3250 GetLineStart( row
),
3252 GetLineHeight( row
) );
3254 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3255 &itemRect
.x
, &itemRect
.y
);
3260 int wxDataViewMainWindow::RecalculateCount()
3262 if (IsVirtualList())
3264 wxDataViewVirtualListModel
*list_model
=
3265 (wxDataViewVirtualListModel
*) GetModel();
3267 return list_model
->GetCount();
3271 return m_root
->GetSubTreeCount();
3275 class ItemToRowJob
: public DoJob
3278 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3285 // Maybe binary search will help to speed up this process
3286 virtual int operator() ( wxDataViewTreeNode
* node
)
3289 if( node
->GetItem() == item
)
3294 if( node
->GetItem() == *m_iter
)
3297 return DoJob::CONTINUE
;
3301 ret
+= node
->GetSubTreeCount();
3302 return DoJob::SKIP_SUBTREE
;
3307 // the row number is begin from zero
3308 int GetResult() const
3312 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3313 wxDataViewItem item
;
3318 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3320 const wxDataViewModel
* model
= GetModel();
3324 if (IsVirtualList())
3326 return wxPtrToUInt( item
.GetID() ) -1;
3333 // Compose the parent-chain of the item we are looking for
3334 wxVector
<wxDataViewItem
> parentChain
;
3335 wxDataViewItem
it( item
);
3338 parentChain
.push_back(it
);
3339 it
= model
->GetParent(it
);
3342 // add an 'invalid' item to represent our 'invisible' root node
3343 parentChain
.push_back(wxDataViewItem());
3345 // the parent chain was created by adding the deepest parent first.
3346 // so if we want to start at the root node, we have to iterate backwards through the vector
3347 ItemToRowJob
job( item
, parentChain
.rbegin() );
3348 Walker( m_root
, job
);
3349 return job
.GetResult();
3353 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3354 wxDataViewTreeNode
* node
)
3356 if( !model
->IsContainer( item
) )
3359 wxDataViewItemArray children
;
3360 unsigned int num
= model
->GetChildren( item
, children
);
3362 for ( unsigned int index
= 0; index
< num
; index
++ )
3364 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3366 if( model
->IsContainer(children
[index
]) )
3367 n
->SetHasChildren( true );
3369 node
->InsertChild(n
, index
);
3372 wxASSERT( node
->IsOpen() );
3373 node
->ChangeSubTreeCount(+num
);
3376 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3380 if (GetModel()->IsVirtualListModel())
3386 m_root
= wxDataViewTreeNode::CreateRootNode();
3388 // First we define a invalid item to fetch the top-level elements
3389 wxDataViewItem item
;
3391 BuildTreeHelper( model
, item
, m_root
);
3395 void wxDataViewMainWindow::DestroyTree()
3397 if (!IsVirtualList())
3405 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3407 // Edit the current column editable in 'mode'. If no column is focused
3408 // (typically because the user has full row selected), try to find the
3409 // first editable column (this would typically be a checkbox for
3410 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3411 // focus on the checkbox column; or on the only editable text column).
3413 wxDataViewColumn
*candidate
= m_currentCol
;
3416 candidate
->GetRenderer()->GetMode() != mode
&&
3417 !m_currentColSetByKeyboard
)
3419 // If current column was set by mouse to something not editable (in
3420 // 'mode') and the user pressed Space/F2 to edit it, treat the
3421 // situation as if there was whole-row focus, because that's what is
3422 // visually indicated and the mouse click could very well be targeted
3423 // on the row rather than on an individual cell.
3425 // But if it was done by keyboard, respect that even if the column
3426 // isn't editable, because focus is visually on that column and editing
3427 // something else would be surprising.
3433 const unsigned cols
= GetOwner()->GetColumnCount();
3434 for ( unsigned i
= 0; i
< cols
; i
++ )
3436 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3437 if ( c
->IsHidden() )
3440 if ( c
->GetRenderer()->GetMode() == mode
)
3448 // If on container item without columns, only the expander column
3449 // may be directly editable:
3451 GetOwner()->GetExpanderColumn() != candidate
&&
3452 GetModel()->IsContainer(item
) &&
3453 !GetModel()->HasContainerColumns(item
) )
3455 candidate
= GetOwner()->GetExpanderColumn();
3461 if ( candidate
->GetRenderer()->GetMode() != mode
)
3467 void wxDataViewMainWindow::OnCharHook(wxKeyEvent
& event
)
3471 // Handle any keys special for the in-place editor and return without
3472 // calling Skip() below.
3473 switch ( event
.GetKeyCode() )
3476 m_editorRenderer
->CancelEditing();
3480 m_editorRenderer
->FinishEditing();
3488 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3490 wxWindow
* const parent
= GetParent();
3492 // propagate the char event upwards
3493 wxKeyEvent
eventForParent(event
);
3494 eventForParent
.SetEventObject(parent
);
3495 if ( parent
->ProcessWindowEvent(eventForParent
) )
3498 if ( parent
->HandleAsNavigationKey(event
) )
3501 // no item -> nothing to do
3502 if (!HasCurrentRow())
3508 // don't use m_linesPerPage directly as it might not be computed yet
3509 const int pageSize
= GetCountPerPage();
3510 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3512 switch ( event
.GetKeyCode() )
3516 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3517 // it. Only if that event is not handled do we activate column renderer (which
3518 // is normally done by Space) or even inline editing.
3520 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3522 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3525 le
.SetEventObject(parent
);
3526 le
.SetModel(GetModel());
3528 if ( parent
->ProcessWindowEvent(le
) )
3530 // else: fall through to WXK_SPACE handling
3535 // Space toggles activatable items or -- if not activatable --
3536 // starts inline editing (this is normally done using F2 on
3537 // Windows, but Space is common everywhere else, so use it too
3538 // for greater cross-platform compatibility).
3540 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3542 // Activate the current activatable column. If not column is focused (typically
3543 // because the user has full row selected), try to find the first activatable
3544 // column (this would typically be a checkbox and we don't want to force the user
3545 // to set focus on the checkbox column).
3546 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3548 if ( activatableCol
)
3550 const unsigned colIdx
= activatableCol
->GetModelColumn();
3551 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3553 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3554 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3555 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3559 // else: fall through to WXK_F2 handling
3564 if( !m_selection
.empty() )
3566 // Mimic Windows 7 behavior: edit the item that has focus
3567 // if it is selected and the first selected item if focus
3568 // is out of selection.
3570 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3573 sel
= m_selection
[0];
3576 const wxDataViewItem item
= GetItemByRow(sel
);
3578 // Edit the current column. If no column is focused
3579 // (typically because the user has full row selected), try
3580 // to find the first editable column.
3581 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3584 GetOwner()->StartEditor(item
, GetOwner()->GetColumnIndex(editableCol
));
3590 if ( m_currentRow
> 0 )
3591 OnVerticalNavigation( m_currentRow
- 1, event
);
3595 if ( m_currentRow
+ 1 < GetRowCount() )
3596 OnVerticalNavigation( m_currentRow
+ 1, event
);
3598 // Add the process for tree expanding/collapsing
3610 OnVerticalNavigation( GetRowCount() - 1, event
);
3615 OnVerticalNavigation( 0, event
);
3620 int steps
= pageSize
- 1;
3621 int index
= m_currentRow
- steps
;
3625 OnVerticalNavigation( index
, event
);
3631 int steps
= pageSize
- 1;
3632 unsigned int index
= m_currentRow
+ steps
;
3633 unsigned int count
= GetRowCount();
3634 if ( index
>= count
)
3637 OnVerticalNavigation( index
, event
);
3646 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3648 wxCHECK_RET( newCurrent
< GetRowCount(),
3649 wxT("invalid item index in OnVerticalNavigation()") );
3651 // if there is no selection, we cannot move it anywhere
3652 if (!HasCurrentRow())
3655 unsigned int oldCurrent
= m_currentRow
;
3657 // in single selection we just ignore Shift as we can't select several
3659 if ( event
.ShiftDown() && !IsSingleSel() )
3661 RefreshRow( oldCurrent
);
3663 ChangeCurrentRow( newCurrent
);
3665 // select all the items between the old and the new one
3666 if ( oldCurrent
> newCurrent
)
3668 newCurrent
= oldCurrent
;
3669 oldCurrent
= m_currentRow
;
3672 SelectRows( oldCurrent
, newCurrent
, true );
3673 if (oldCurrent
!=newCurrent
)
3674 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3678 RefreshRow( oldCurrent
);
3680 // all previously selected items are unselected unless ctrl is held
3681 if ( !event
.ControlDown() )
3682 SelectAllRows(false);
3684 ChangeCurrentRow( newCurrent
);
3686 if ( !event
.ControlDown() )
3688 SelectRow( m_currentRow
, true );
3689 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3692 RefreshRow( m_currentRow
);
3695 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3698 void wxDataViewMainWindow::OnLeftKey()
3702 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3706 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3708 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3711 // Because TryAdvanceCurrentColumn() return false, we are at the first
3712 // column or using whole-row selection. In this situation, we can use
3713 // the standard TreeView handling of the left key.
3714 if (node
->HasChildren() && node
->IsOpen())
3716 Collapse(m_currentRow
);
3720 // if the node is already closed, we move the selection to its parent
3721 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3725 int parent
= GetRowByItem( parent_node
->GetItem() );
3728 unsigned int row
= m_currentRow
;
3729 SelectRow( row
, false);
3730 SelectRow( parent
, true );
3731 ChangeCurrentRow( parent
);
3732 GetOwner()->EnsureVisible( parent
, -1 );
3733 SendSelectionChangedEvent( parent_node
->GetItem() );
3740 void wxDataViewMainWindow::OnRightKey()
3744 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3748 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3750 if ( node
->HasChildren() )
3752 if ( !node
->IsOpen() )
3754 Expand( m_currentRow
);
3758 // if the node is already open, we move the selection to the first child
3759 unsigned int row
= m_currentRow
;
3760 SelectRow( row
, false );
3761 SelectRow( row
+ 1, true );
3762 ChangeCurrentRow( row
+ 1 );
3763 GetOwner()->EnsureVisible( row
+ 1, -1 );
3764 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3769 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3774 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3776 if ( GetOwner()->GetColumnCount() == 0 )
3779 if ( !m_useCellFocus
)
3784 // navigation shouldn't work in branch nodes without other columns:
3785 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3789 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3793 m_currentCol
= GetOwner()->GetColumnAt(1);
3794 m_currentColSetByKeyboard
= true;
3795 RefreshRow(m_currentRow
);
3802 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3804 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3807 GetOwner()->EnsureVisible(m_currentRow
, idx
);
3811 // We are going to the left of the second column. Reset to whole-row
3812 // focus (which means first column would be edited).
3813 m_currentCol
= NULL
;
3814 RefreshRow(m_currentRow
);
3818 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3819 m_currentColSetByKeyboard
= true;
3820 RefreshRow(m_currentRow
);
3824 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3826 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3828 // let the base handle mouse wheel events.
3833 // set the focus to ourself if any of the mouse buttons are pressed
3834 if(event
.ButtonDown() && !HasFocus())
3837 int x
= event
.GetX();
3838 int y
= event
.GetY();
3839 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3840 wxDataViewColumn
*col
= NULL
;
3843 unsigned int cols
= GetOwner()->GetColumnCount();
3845 for (i
= 0; i
< cols
; i
++)
3847 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3849 continue; // skip it!
3851 if (x
< xpos
+ c
->GetWidth())
3856 xpos
+= c
->GetWidth();
3859 wxDataViewModel
* const model
= GetModel();
3861 const unsigned int current
= GetLineAt( y
);
3862 const wxDataViewItem item
= GetItemByRow(current
);
3864 // Handle right clicking here, before everything else as context menu
3865 // events should be sent even when we click outside of any item, unlike all
3867 if (event
.RightUp())
3869 wxWindow
*parent
= GetParent();
3870 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3871 le
.SetEventObject(parent
);
3874 if ( item
.IsOk() && col
)
3877 le
.SetColumn( col
->GetModelColumn() );
3878 le
.SetDataViewColumn( col
);
3881 model
->GetValue( value
, item
, col
->GetModelColumn() );
3885 parent
->ProcessWindowEvent(le
);
3895 wxDataViewRenderer
*cell
= col
->GetRenderer();
3896 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3898 // Unselect all if below the last row ?
3903 wxDataViewColumn
* const
3904 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3906 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3907 // button) and also determine the offset of the real cell start, skipping
3908 // the indentation and the expander itself.
3909 bool hoverOverExpander
= false;
3911 if ((!IsList()) && (expander
== col
))
3913 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3915 int indent
= node
->GetIndentLevel();
3916 itemOffset
= GetOwner()->GetIndent()*indent
;
3918 if ( node
->HasChildren() )
3920 // we make the rectangle we are looking in a bit bigger than the actual
3921 // visual expander so the user can hit that little thing reliably
3922 wxRect
rect(itemOffset
,
3923 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3924 m_lineHeight
, m_lineHeight
);
3926 if( rect
.Contains(x
, y
) )
3928 // So the mouse is over the expander
3929 hoverOverExpander
= true;
3930 if (m_underMouse
&& m_underMouse
!= node
)
3932 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3933 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3935 if (m_underMouse
!= node
)
3937 // wxLogMessage("Do the row: %d", current);
3938 RefreshRow(current
);
3940 m_underMouse
= node
;
3944 // Account for the expander as well, even if this item doesn't have it,
3945 // its parent does so it still counts for the offset.
3946 itemOffset
+= m_lineHeight
;
3948 if (!hoverOverExpander
)
3950 if (m_underMouse
!= NULL
)
3952 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3953 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3954 m_underMouse
= NULL
;
3958 #if wxUSE_DRAG_AND_DROP
3959 if (event
.Dragging())
3961 if (m_dragCount
== 0)
3963 // we have to report the raw, physical coords as we want to be
3964 // able to call HitTest(event.m_pointDrag) from the user code to
3965 // get the item being dragged
3966 m_dragStart
= event
.GetPosition();
3971 if (m_dragCount
!= 3)
3974 if (event
.LeftIsDown())
3976 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3977 &m_dragStart
.x
, &m_dragStart
.y
);
3978 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3979 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
3981 // Notify cell about drag
3982 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3983 event
.SetEventObject( m_owner
);
3984 event
.SetItem( itemDragged
);
3985 event
.SetModel( model
);
3986 if (!m_owner
->HandleWindowEvent( event
))
3989 if (!event
.IsAllowed())
3992 wxDataObject
*obj
= event
.GetDataObject();
3996 wxDataViewDropSource
drag( this, drag_item_row
);
3997 drag
.SetData( *obj
);
3998 /* wxDragResult res = */ drag
.DoDragDrop();
4007 #endif // wxUSE_DRAG_AND_DROP
4009 bool simulateClick
= false;
4011 if (event
.ButtonDClick())
4013 m_renameTimer
->Stop();
4014 m_lastOnSame
= false;
4017 bool ignore_other_columns
=
4018 ((expander
!= col
) &&
4019 (model
->IsContainer(item
)) &&
4020 (!model
->HasContainerColumns(item
)));
4022 if (event
.LeftDClick())
4024 if(hoverOverExpander
)
4026 // a double click on the expander will be converted into a "simulated" normal click
4027 simulateClick
= true;
4029 else if ( current
== m_lineLastClicked
)
4031 wxWindow
*parent
= GetParent();
4032 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4034 le
.SetColumn( col
->GetModelColumn() );
4035 le
.SetDataViewColumn( col
);
4036 le
.SetEventObject(parent
);
4037 le
.SetModel(GetModel());
4039 parent
->ProcessWindowEvent(le
);
4044 // The first click was on another item, so don't interpret this as
4045 // a double click, but as a simple click instead
4046 simulateClick
= true;
4050 if (event
.LeftUp() && !hoverOverExpander
)
4052 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4054 // select single line
4055 SelectAllRows( false );
4056 SelectRow( m_lineSelectSingleOnUp
, true );
4057 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4060 // If the user click the expander, we do not do editing even if the column
4061 // with expander are editable
4062 if (m_lastOnSame
&& !ignore_other_columns
)
4064 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4065 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
4067 m_renameTimer
->Start( 100, true );
4071 m_lastOnSame
= false;
4072 m_lineSelectSingleOnUp
= (unsigned int)-1;
4074 else if(!event
.LeftUp())
4076 // This is necessary, because after a DnD operation in
4077 // from and to ourself, the up event is swallowed by the
4078 // DnD code. So on next non-up event (which means here and
4079 // now) m_lineSelectSingleOnUp should be reset.
4080 m_lineSelectSingleOnUp
= (unsigned int)-1;
4083 if (event
.RightDown())
4085 m_lineBeforeLastClicked
= m_lineLastClicked
;
4086 m_lineLastClicked
= current
;
4088 // If the item is already selected, do not update the selection.
4089 // Multi-selections should not be cleared if a selected item is clicked.
4090 if (!IsRowSelected(current
))
4092 SelectAllRows(false);
4093 const unsigned oldCurrent
= m_currentRow
;
4094 ChangeCurrentRow(current
);
4095 SelectRow(m_currentRow
,true);
4096 RefreshRow(oldCurrent
);
4097 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4100 else if (event
.MiddleDown())
4104 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4106 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4108 // hoverOverExpander being true tells us that our node must be
4109 // valid and have children.
4110 // So we don't need any extra checks.
4111 if( node
->IsOpen() )
4116 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4118 m_lineBeforeLastClicked
= m_lineLastClicked
;
4119 m_lineLastClicked
= current
;
4121 unsigned int oldCurrentRow
= m_currentRow
;
4122 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4124 bool cmdModifierDown
= event
.CmdDown();
4125 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4127 if ( IsSingleSel() || !IsRowSelected(current
) )
4129 SelectAllRows( false );
4130 ChangeCurrentRow(current
);
4131 SelectRow(m_currentRow
,true);
4132 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4134 else // multi sel & current is highlighted & no mod keys
4136 m_lineSelectSingleOnUp
= current
;
4137 ChangeCurrentRow(current
); // change focus
4140 else // multi sel & either ctrl or shift is down
4142 if (cmdModifierDown
)
4144 ChangeCurrentRow(current
);
4145 ReverseRowSelection(m_currentRow
);
4146 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4148 else if (event
.ShiftDown())
4150 ChangeCurrentRow(current
);
4152 unsigned int lineFrom
= oldCurrentRow
,
4155 if ( lineTo
< lineFrom
)
4158 lineFrom
= m_currentRow
;
4161 SelectRows(lineFrom
, lineTo
, true);
4162 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4164 else // !ctrl, !shift
4166 // test in the enclosing if should make it impossible
4167 wxFAIL_MSG( wxT("how did we get here?") );
4171 if (m_currentRow
!= oldCurrentRow
)
4172 RefreshRow( oldCurrentRow
);
4174 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4176 // Update selection here...
4178 m_currentColSetByKeyboard
= false;
4180 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4181 (current
== oldCurrentRow
)) && oldWasSelected
;
4183 // Call ActivateCell() after everything else as under GTK+
4184 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
4186 // notify cell about click
4187 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4189 wxRect
cell_rect( xpos
+ itemOffset
,
4190 GetLineStart( current
),
4191 col
->GetWidth() - itemOffset
,
4192 GetLineHeight( current
) );
4194 // Report position relative to the cell's custom area, i.e.
4195 // no the entire space as given by the control but the one
4196 // used by the renderer after calculation of alignment etc.
4198 // adjust the rectangle ourselves to account for the alignment
4199 wxRect rectItem
= cell_rect
;
4200 const int align
= cell
->GetAlignment();
4201 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4203 const wxSize size
= cell
->GetSize();
4205 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4207 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4208 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4209 else if ( align
& wxALIGN_RIGHT
)
4210 rectItem
.x
+= cell_rect
.width
- size
.x
;
4211 // else: wxALIGN_LEFT is the default
4214 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4216 if ( align
& wxALIGN_CENTER_VERTICAL
)
4217 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4218 else if ( align
& wxALIGN_BOTTOM
)
4219 rectItem
.y
+= cell_rect
.height
- size
.y
;
4220 // else: wxALIGN_TOP is the default
4224 wxMouseEvent
event2(event
);
4225 event2
.m_x
-= rectItem
.x
;
4226 event2
.m_y
-= rectItem
.y
;
4227 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4229 /* ignore ret */ cell
->WXActivateCell
4234 col
->GetModelColumn(),
4241 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4245 if (HasCurrentRow())
4251 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4255 if (HasCurrentRow())
4261 void wxDataViewMainWindow::OnColumnsCountChanged()
4263 int editableCount
= 0;
4265 const unsigned cols
= GetOwner()->GetColumnCount();
4266 for ( unsigned i
= 0; i
< cols
; i
++ )
4268 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4269 if ( c
->IsHidden() )
4271 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4275 m_useCellFocus
= (editableCount
> 1);
4280 //-----------------------------------------------------------------------------
4282 //-----------------------------------------------------------------------------
4284 WX_DEFINE_LIST(wxDataViewColumnList
)
4286 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4287 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4288 EVT_SIZE(wxDataViewCtrl::OnSize
)
4291 wxDataViewCtrl::~wxDataViewCtrl()
4294 GetModel()->RemoveNotifier( m_notifier
);
4297 m_colsBestWidths
.clear();
4300 void wxDataViewCtrl::Init()
4302 m_cols
.DeleteContents(true);
4305 // No sorting column at start
4306 m_sortingColumnIdx
= wxNOT_FOUND
;
4308 m_headerArea
= NULL
;
4310 m_colsDirty
= false;
4313 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4318 const wxValidator
& validator
,
4319 const wxString
& name
)
4321 // if ( (style & wxBORDER_MASK) == 0)
4322 // style |= wxBORDER_SUNKEN;
4326 if (!wxControl::Create( parent
, id
, pos
, size
,
4327 style
| wxScrolledWindowStyle
, validator
, name
))
4330 SetInitialSize(size
);
4333 MacSetClipChildren( true );
4336 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4338 // We use the cursor keys for moving the selection, not scrolling, so call
4339 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4340 // keyboard events forwarded to us from wxListMainWindow.
4341 DisableKeyboardScrolling();
4343 if (HasFlag(wxDV_NO_HEADER
))
4344 m_headerArea
= NULL
;
4346 m_headerArea
= new wxDataViewHeaderWindow(this);
4348 SetTargetWindow( m_clientArea
);
4350 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4352 sizer
->Add( m_headerArea
, 0, wxGROW
);
4353 sizer
->Add( m_clientArea
, 1, wxGROW
);
4359 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4361 return wxBORDER_THEME
;
4365 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4369 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4372 // we need to process arrows ourselves for scrolling
4373 if ( nMsg
== WM_GETDLGCODE
)
4375 rc
|= DLGC_WANTARROWS
;
4383 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4385 wxSize newsize
= size
;
4386 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4387 newsize
.y
-= m_headerArea
->GetSize().y
;
4392 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4394 // We need to override OnSize so that our scrolled
4395 // window a) does call Layout() to use sizers for
4396 // positioning the controls but b) does not query
4397 // the sizer for their size and use that for setting
4398 // the scrollable area as set that ourselves by
4399 // calling SetScrollbar() further down.
4405 // We must redraw the headers if their height changed. Normally this
4406 // shouldn't happen as the control shouldn't let itself be resized beneath
4407 // its minimal height but avoid the display artefacts that appear if it
4408 // does happen, e.g. because there is really not enough vertical space.
4409 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4410 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4412 m_headerArea
->Refresh();
4416 void wxDataViewCtrl::SetFocus()
4419 m_clientArea
->SetFocus();
4422 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4424 if (!wxDataViewCtrlBase::AssociateModel( model
))
4427 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4429 model
->AddNotifier( m_notifier
);
4431 m_clientArea
->DestroyTree();
4433 m_clientArea
->BuildTree(model
);
4435 m_clientArea
->UpdateDisplay();
4440 #if wxUSE_DRAG_AND_DROP
4442 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4444 return m_clientArea
->EnableDragSource( format
);
4447 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4449 return m_clientArea
->EnableDropTarget( format
);
4452 #endif // wxUSE_DRAG_AND_DROP
4454 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4456 if (!wxDataViewCtrlBase::AppendColumn(col
))
4459 m_cols
.Append( col
);
4460 m_colsBestWidths
.push_back(0);
4461 OnColumnsCountChanged();
4465 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4467 if (!wxDataViewCtrlBase::PrependColumn(col
))
4470 m_cols
.Insert( col
);
4471 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4472 OnColumnsCountChanged();
4476 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4478 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4481 m_cols
.Insert( pos
, col
);
4482 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4483 OnColumnsCountChanged();
4487 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4490 m_headerArea
->UpdateColumn(idx
);
4492 m_clientArea
->UpdateDisplay();
4495 void wxDataViewCtrl::OnColumnsCountChanged()
4498 m_headerArea
->SetColumnCount(GetColumnCount());
4500 m_clientArea
->OnColumnsCountChanged();
4503 void wxDataViewCtrl::DoSetExpanderColumn()
4505 m_clientArea
->UpdateDisplay();
4508 void wxDataViewCtrl::DoSetIndent()
4510 m_clientArea
->UpdateDisplay();
4513 unsigned int wxDataViewCtrl::GetColumnCount() const
4515 return m_cols
.GetCount();
4518 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4520 if ( !m_clientArea
)
4523 m_clientArea
->SetRowHeight(lineHeight
);
4528 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4533 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4535 // columns can't be reordered if there is no header window which allows
4537 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4540 return GetColumn(idx
);
4543 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4545 const unsigned count
= m_cols
.size();
4546 for ( unsigned n
= 0; n
< count
; n
++ )
4548 if ( m_cols
[n
] == column
)
4555 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4557 if ( m_colsBestWidths
[idx
] != 0 )
4558 return m_colsBestWidths
[idx
];
4560 const int count
= m_clientArea
->GetRowCount();
4561 wxDataViewColumn
*column
= GetColumn(idx
);
4562 wxDataViewRenderer
*renderer
=
4563 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4565 class MaxWidthCalculator
4568 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4569 wxDataViewMainWindow
*clientArea
,
4570 wxDataViewRenderer
*renderer
,
4571 const wxDataViewModel
*model
,
4576 m_clientArea(clientArea
),
4577 m_renderer(renderer
),
4580 m_expanderSize(expanderSize
)
4584 !clientArea
->IsList() &&
4586 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4589 void UpdateWithWidth(int width
)
4591 m_width
= wxMax(m_width
, width
);
4594 void UpdateWithRow(int row
)
4597 wxDataViewItem item
;
4599 if ( m_isExpanderCol
)
4601 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4602 item
= node
->GetItem();
4603 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4607 item
= m_clientArea
->GetItemByRow(row
);
4610 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4611 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4614 int GetMaxWidth() const { return m_width
; }
4618 const wxDataViewCtrl
*m_dvc
;
4619 wxDataViewMainWindow
*m_clientArea
;
4620 wxDataViewRenderer
*m_renderer
;
4621 const wxDataViewModel
*m_model
;
4623 bool m_isExpanderCol
;
4627 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4628 GetModel(), column
->GetModelColumn(),
4629 m_clientArea
->GetRowHeight());
4633 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4634 // Labels on native MSW header are indented on both sides
4636 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4637 calculator
.UpdateWithWidth(header_width
);
4640 // The code below deserves some explanation. For very large controls, we
4641 // simply can't afford to calculate sizes for all items, it takes too
4642 // long. So the best we can do is to check the first and the last N/2
4643 // items in the control for some sufficiently large N and calculate best
4644 // sizes from that. That can result in the calculated best width being too
4645 // small for some outliers, but it's better to get slightly imperfect
4646 // result than to wait several seconds after every update. To avoid highly
4647 // visible miscalculations, we also include all currently visible items
4648 // no matter what. Finally, the value of N is determined dynamically by
4649 // measuring how much time we spent on the determining item widths so far.
4652 int top_part_end
= count
;
4653 static const long CALC_TIMEOUT
= 20/*ms*/;
4654 // don't call wxStopWatch::Time() too often
4655 static const unsigned CALC_CHECK_FREQ
= 100;
4658 // use some hard-coded limit, that's the best we can do without timer
4659 int top_part_end
= wxMin(500, count
);
4660 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4664 for ( row
= 0; row
< top_part_end
; row
++ )
4667 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4668 timer
.Time() > CALC_TIMEOUT
)
4670 #endif // wxUSE_STOPWATCH
4671 calculator
.UpdateWithRow(row
);
4674 // row is the first unmeasured item now; that's our value of N/2
4680 // add bottom N/2 items now:
4681 const int bottom_part_start
= wxMax(row
, count
- row
);
4682 for ( row
= bottom_part_start
; row
< count
; row
++ )
4684 calculator
.UpdateWithRow(row
);
4687 // finally, include currently visible items in the calculation:
4688 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4689 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4690 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4692 first_visible
= wxMax(first_visible
, top_part_end
);
4693 last_visible
= wxMin(bottom_part_start
, last_visible
);
4695 for ( row
= first_visible
; row
< last_visible
; row
++ )
4697 calculator
.UpdateWithRow(row
);
4700 wxLogTrace("dataview",
4701 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4703 count
- bottom_part_start
,
4704 wxMax(0, last_visible
- first_visible
),
4708 int max_width
= calculator
.GetMaxWidth();
4709 if ( max_width
> 0 )
4710 max_width
+= 2 * PADDING_RIGHTLEFT
;
4712 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4716 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4717 unsigned int WXUNUSED(new_pos
))
4719 // do _not_ reorder m_cols elements here, they should always be in the
4720 // order in which columns were added, we only display the columns in
4722 m_clientArea
->UpdateDisplay();
4725 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4727 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4731 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4733 OnColumnsCountChanged();
4738 bool wxDataViewCtrl::ClearColumns()
4740 SetExpanderColumn(NULL
);
4742 m_colsBestWidths
.clear();
4743 OnColumnsCountChanged();
4747 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4749 m_colsBestWidths
[idx
] = 0;
4753 void wxDataViewCtrl::InvalidateColBestWidths()
4755 m_colsBestWidths
.clear();
4756 m_colsBestWidths
.resize(m_cols
.size());
4760 void wxDataViewCtrl::UpdateColWidths()
4762 if ( !m_headerArea
)
4765 const unsigned len
= m_colsBestWidths
.size();
4766 for ( unsigned i
= 0; i
< len
; i
++ )
4768 if ( m_colsBestWidths
[i
] == 0 )
4769 m_headerArea
->UpdateColumn(i
);
4773 void wxDataViewCtrl::OnInternalIdle()
4775 wxDataViewCtrlBase::OnInternalIdle();
4779 m_colsDirty
= false;
4784 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4787 unsigned int len
= GetColumnCount();
4788 for ( unsigned int i
= 0; i
< len
; i
++ )
4790 wxDataViewColumn
* col
= GetColumnAt(i
);
4797 // This returns the position in pixels which is not what we want.
4800 unsigned int len
= GetColumnCount();
4801 for ( unsigned int i
= 0; i
< len
; i
++ )
4803 wxDataViewColumn
* col
= GetColumnAt(i
);
4804 if (col
->IsHidden())
4806 ret
+= col
->GetWidth();
4809 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4817 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4819 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4820 : GetColumn(m_sortingColumnIdx
);
4823 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4825 return GetItemByRow(m_clientArea
->GetCurrentRow());
4828 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4830 const int row
= m_clientArea
->GetRowByItem(item
);
4832 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4833 if ( static_cast<unsigned>(row
) != oldCurrent
)
4835 m_clientArea
->ChangeCurrentRow(row
);
4836 m_clientArea
->RefreshRow(oldCurrent
);
4837 m_clientArea
->RefreshRow(row
);
4841 int wxDataViewCtrl::GetSelectedItemsCount() const
4843 return m_clientArea
->GetSelections().size();
4846 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4849 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4851 const size_t len
= selections
.size();
4852 for ( size_t i
= 0; i
< len
; i
++ )
4854 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4861 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4868 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4870 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4872 wxDataViewItem last_parent
;
4874 int len
= sel
.GetCount();
4875 for( int i
= 0; i
< len
; i
++ )
4877 wxDataViewItem item
= sel
[i
];
4878 wxDataViewItem parent
= GetModel()->GetParent( item
);
4881 if (parent
!= last_parent
)
4882 ExpandAncestors(item
);
4885 last_parent
= parent
;
4886 int row
= m_clientArea
->GetRowByItem( item
);
4888 selection
.Add( static_cast<unsigned int>(row
) );
4891 m_clientArea
->SetSelections( selection
);
4894 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4896 ExpandAncestors( item
);
4898 int row
= m_clientArea
->GetRowByItem( item
);
4901 // Unselect all rows before select another in the single select mode
4902 if (m_clientArea
->IsSingleSel())
4903 m_clientArea
->SelectAllRows(false);
4905 m_clientArea
->SelectRow(row
, true);
4907 // Also set focus to the selected item
4908 m_clientArea
->ChangeCurrentRow( row
);
4912 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4914 int row
= m_clientArea
->GetRowByItem( item
);
4916 m_clientArea
->SelectRow(row
, false);
4919 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4921 int row
= m_clientArea
->GetRowByItem( item
);
4924 return m_clientArea
->IsRowSelected(row
);
4929 void wxDataViewCtrl::SelectAll()
4931 m_clientArea
->SelectAllRows(true);
4934 void wxDataViewCtrl::UnselectAll()
4936 m_clientArea
->SelectAllRows(false);
4939 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4943 if( row
> (int) m_clientArea
->GetRowCount() )
4944 row
= m_clientArea
->GetRowCount();
4946 int first
= m_clientArea
->GetFirstVisibleRow();
4947 int last
= m_clientArea
->GetLastVisibleRow();
4949 m_clientArea
->ScrollTo( row
, column
);
4950 else if( row
> last
)
4951 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4953 m_clientArea
->ScrollTo( first
, column
);
4956 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4958 ExpandAncestors( item
);
4960 m_clientArea
->RecalculateDisplay();
4962 int row
= m_clientArea
->GetRowByItem(item
);
4965 if( column
== NULL
)
4966 EnsureVisible(row
, -1);
4968 EnsureVisible( row
, GetColumnIndex(column
) );
4973 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4974 wxDataViewColumn
* &column
) const
4976 m_clientArea
->HitTest(point
, item
, column
);
4979 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4980 const wxDataViewColumn
* column
) const
4982 return m_clientArea
->GetItemRect(item
, column
);
4985 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4987 return m_clientArea
->GetItemByRow( row
);
4990 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4992 return m_clientArea
->GetRowByItem( item
);
4995 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4997 ExpandAncestors( item
);
4999 int row
= m_clientArea
->GetRowByItem( item
);
5001 m_clientArea
->Expand(row
);
5004 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
5006 int row
= m_clientArea
->GetRowByItem( item
);
5008 m_clientArea
->Collapse(row
);
5011 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
5013 int row
= m_clientArea
->GetRowByItem( item
);
5015 return m_clientArea
->IsExpanded(row
);
5019 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
5021 wxDataViewColumn
* col
= GetColumn( column
);
5025 m_clientArea
->StartEditing(item
, col
);
5028 #endif // !wxUSE_GENERICDATAVIEWCTRL
5030 #endif // wxUSE_DATAVIEWCTRL