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
->m_branchData
= new BranchNodeData
;
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 // We do not allow the (invisible) root node to be collapsed because
420 // there is no way to expand it again.
424 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
428 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
429 const int len
= nodes
.GetCount();
430 for ( int i
= 0;i
< len
; i
++)
431 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
433 if (m_branchData
->open
)
435 ChangeSubTreeCount(-sum
);
436 m_branchData
->open
= !m_branchData
->open
;
440 m_branchData
->open
= !m_branchData
->open
;
441 ChangeSubTreeCount(+sum
);
445 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
446 // even if GetChildNodes() is empty; see below.
447 bool HasChildren() const
449 return m_branchData
!= NULL
;
452 void SetHasChildren(bool has
)
454 // The invisible root item always has children, so ignore any attempts
461 wxDELETE(m_branchData
);
463 else if ( m_branchData
== NULL
)
465 m_branchData
= new BranchNodeData
;
469 int GetSubTreeCount() const
471 return m_branchData
? m_branchData
->subTreeCount
: 0;
474 void ChangeSubTreeCount( int num
)
476 wxASSERT( m_branchData
!= NULL
);
478 if( !m_branchData
->open
)
481 m_branchData
->subTreeCount
+= num
;
482 wxASSERT( m_branchData
->subTreeCount
>= 0 );
485 m_parent
->ChangeSubTreeCount(num
);
495 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
497 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
498 int len
= nodes
.GetCount();
499 for (int i
= 0; i
< len
; i
++)
501 if ( nodes
[i
]->HasChildren() )
509 wxDataViewTreeNode
*m_parent
;
511 // Corresponding model item.
512 wxDataViewItem m_item
;
514 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
515 // separate struct in order to conserve memory.
516 struct BranchNodeData
524 // Child nodes. Note that this may be empty even if m_hasChildren in
525 // case this branch of the tree wasn't expanded and realized yet.
526 wxDataViewTreeNodes children
;
528 // Is the branch node currently open (expanded)?
531 // Total count of expanded (i.e. visible with the help of some
532 // scrolling) items in the subtree, but excluding this node. I.e. it is
533 // 0 for leaves and is the number of rows the subtree occupies for
538 BranchNodeData
*m_branchData
;
542 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
543 wxDataViewTreeNode
** node2
)
545 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
549 //-----------------------------------------------------------------------------
550 // wxDataViewMainWindow
551 //-----------------------------------------------------------------------------
553 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
555 class wxDataViewMainWindow
: public wxWindow
558 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
560 const wxPoint
&pos
= wxDefaultPosition
,
561 const wxSize
&size
= wxDefaultSize
,
562 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
563 virtual ~wxDataViewMainWindow();
565 bool IsList() const { return GetModel()->IsListModel(); }
566 bool IsVirtualList() const { return m_root
== NULL
; }
568 // notifications from wxDataViewModel
569 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
570 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
571 bool ItemChanged( const wxDataViewItem
&item
);
572 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
576 if (!IsVirtualList())
586 g_model
= GetModel();
587 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
590 if (g_model
->HasDefaultCompare())
598 g_column
= col
->GetModelColumn();
599 g_asending
= col
->IsSortOrderAscending();
602 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
603 wxDataViewCtrl
*GetOwner() { return m_owner
; }
604 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
606 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
607 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
609 #if wxUSE_DRAG_AND_DROP
610 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
611 #endif // wxUSE_DRAG_AND_DROP
612 void OnPaint( wxPaintEvent
&event
);
613 void OnCharHook( wxKeyEvent
&event
);
614 void OnChar( wxKeyEvent
&event
);
615 void OnVerticalNavigation(int delta
, const wxKeyEvent
& event
);
618 void OnMouse( wxMouseEvent
&event
);
619 void OnSetFocus( wxFocusEvent
&event
);
620 void OnKillFocus( wxFocusEvent
&event
);
622 void UpdateDisplay();
623 void RecalculateDisplay();
624 void OnInternalIdle();
626 void OnRenameTimer();
628 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
629 void ScrollTo( int rows
, int column
);
631 unsigned GetCurrentRow() const { return m_currentRow
; }
632 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
633 void ChangeCurrentRow( unsigned int row
);
634 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
636 wxDataViewColumn
*GetCurrentColumn() const { return m_currentCol
; }
637 void ClearCurrentColumn() { m_currentCol
= NULL
; }
639 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
640 bool IsEmpty() { return GetRowCount() == 0; }
642 int GetCountPerPage() const;
643 int GetEndOfLastCol() const;
644 unsigned int GetFirstVisibleRow() const;
646 // I change this method to un const because in the tree view,
647 // the displaying number of the tree are changing along with the
648 // expanding/collapsing of the tree nodes
649 unsigned int GetLastVisibleRow();
650 unsigned int GetRowCount() const;
652 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
653 void SetSelections( const wxDataViewSelection
& sel
)
654 { m_selection
= sel
; UpdateDisplay(); }
655 void Select( const wxArrayInt
& aSelections
);
656 void SelectAllRows( bool on
);
657 void SelectRow( unsigned int row
, bool on
);
658 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
659 void ReverseRowSelection( unsigned int row
);
660 bool IsRowSelected( unsigned int row
);
661 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
663 void RefreshRow( unsigned int row
);
664 void RefreshRows( unsigned int from
, unsigned int to
);
665 void RefreshRowsAfter( unsigned int firstRow
);
667 // returns the colour to be used for drawing the rules
668 wxColour
GetRuleColour() const
670 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
673 wxRect
GetLineRect( unsigned int row
) const;
675 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
676 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
677 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
679 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
680 int GetRowHeight() const { return m_lineHeight
; }
681 int GetDefaultRowHeight() const;
683 // Some useful functions for row and item mapping
684 wxDataViewItem
GetItemByRow( unsigned int row
) const;
685 int GetRowByItem( const wxDataViewItem
& item
) const;
687 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
688 // We did not need this temporarily
689 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
691 // Methods for building the mapping tree
692 void BuildTree( wxDataViewModel
* model
);
694 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
695 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
697 void Expand( unsigned int row
);
698 void Collapse( unsigned int row
);
699 bool IsExpanded( unsigned int row
) const;
700 bool HasChildren( unsigned int row
) const;
702 #if wxUSE_DRAG_AND_DROP
703 bool EnableDragSource( const wxDataFormat
&format
);
704 bool EnableDropTarget( const wxDataFormat
&format
);
706 void RemoveDropHint();
707 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
708 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
709 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
711 #endif // wxUSE_DRAG_AND_DROP
713 void OnColumnsCountChanged();
715 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
716 // specified item in the given column.
717 void StartEditing(const wxDataViewItem
& item
, const wxDataViewColumn
* col
);
720 int RecalculateCount() const;
722 // Return false only if the event was vetoed by its handler.
723 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
725 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
727 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
729 bool IsCellEditableInMode(const wxDataViewItem
& item
, const wxDataViewColumn
*col
, wxDataViewCellMode mode
) const;
731 void DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
);
734 wxDataViewCtrl
*m_owner
;
738 wxDataViewColumn
*m_currentCol
;
739 unsigned int m_currentRow
;
740 wxDataViewSelection m_selection
;
742 wxDataViewRenameTimer
*m_renameTimer
;
747 bool m_currentColSetByKeyboard
;
749 #if wxUSE_DRAG_AND_DROP
754 wxDataFormat m_dragFormat
;
757 wxDataFormat m_dropFormat
;
759 unsigned int m_dropHintLine
;
760 #endif // wxUSE_DRAG_AND_DROP
762 // for double click logic
763 unsigned int m_lineLastClicked
,
764 m_lineBeforeLastClicked
,
765 m_lineSelectSingleOnUp
;
767 // the pen used to draw horiz/vertical rules
770 // the pen used to draw the expander and the lines
773 // This is the tree structure of the model
774 wxDataViewTreeNode
* m_root
;
777 // This is the tree node under the cursor
778 wxDataViewTreeNode
* m_underMouse
;
780 // The control used for editing or NULL.
781 wxWeakRef
<wxWindow
> m_editorCtrl
;
783 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
784 wxDataViewRenderer
* m_editorRenderer
;
787 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
788 DECLARE_EVENT_TABLE()
791 // ---------------------------------------------------------
792 // wxGenericDataViewModelNotifier
793 // ---------------------------------------------------------
795 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
798 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
799 { m_mainWindow
= mainWindow
; }
801 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
802 { return m_mainWindow
->ItemAdded( parent
, item
); }
803 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
804 { return m_mainWindow
->ItemDeleted( parent
, item
); }
805 virtual bool ItemChanged( const wxDataViewItem
& item
)
806 { return m_mainWindow
->ItemChanged(item
); }
807 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
808 { return m_mainWindow
->ValueChanged( item
, col
); }
809 virtual bool Cleared()
810 { return m_mainWindow
->Cleared(); }
811 virtual void Resort()
812 { m_mainWindow
->Resort(); }
814 wxDataViewMainWindow
*m_mainWindow
;
817 // ---------------------------------------------------------
818 // wxDataViewRenderer
819 // ---------------------------------------------------------
821 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
823 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
824 wxDataViewCellMode mode
,
826 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
830 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
834 wxDataViewRenderer::~wxDataViewRenderer()
839 wxDC
*wxDataViewRenderer::GetDC()
843 if (GetOwner() == NULL
)
845 if (GetOwner()->GetOwner() == NULL
)
847 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
853 void wxDataViewRenderer::SetAlignment( int align
)
858 int wxDataViewRenderer::GetAlignment() const
863 // ---------------------------------------------------------
864 // wxDataViewCustomRenderer
865 // ---------------------------------------------------------
867 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
869 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
870 wxDataViewCellMode mode
, int align
) :
871 wxDataViewRenderer( varianttype
, mode
, align
)
875 // ---------------------------------------------------------
876 // wxDataViewTextRenderer
877 // ---------------------------------------------------------
879 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
881 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
882 wxDataViewCellMode mode
, int align
) :
883 wxDataViewRenderer( varianttype
, mode
, align
)
887 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
889 m_text
= value
.GetString();
894 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
899 bool wxDataViewTextRenderer::HasEditorCtrl() const
904 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
905 wxRect labelRect
, const wxVariant
&value
)
907 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
908 wxPoint(labelRect
.x
,labelRect
.y
),
909 wxSize(labelRect
.width
,labelRect
.height
),
910 wxTE_PROCESS_ENTER
);
912 // select the text in the control an place the cursor at the end
913 ctrl
->SetInsertionPointEnd();
919 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
921 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
922 value
= text
->GetValue();
926 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
928 RenderText(m_text
, 0, rect
, dc
, state
);
932 wxSize
wxDataViewTextRenderer::GetSize() const
935 return GetTextExtent(m_text
);
937 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
940 // ---------------------------------------------------------
941 // wxDataViewBitmapRenderer
942 // ---------------------------------------------------------
944 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
946 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
947 wxDataViewCellMode mode
, int align
) :
948 wxDataViewRenderer( varianttype
, mode
, align
)
952 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
954 if (value
.GetType() == wxT("wxBitmap"))
956 if (value
.GetType() == wxT("wxIcon"))
962 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
967 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
970 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
971 else if (m_icon
.IsOk())
972 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
977 wxSize
wxDataViewBitmapRenderer::GetSize() const
980 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
981 else if (m_icon
.IsOk())
982 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
984 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
987 // ---------------------------------------------------------
988 // wxDataViewToggleRenderer
989 // ---------------------------------------------------------
991 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
993 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
994 wxDataViewCellMode mode
, int align
) :
995 wxDataViewRenderer( varianttype
, mode
, align
)
1000 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1002 m_toggle
= value
.GetBool();
1007 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
1012 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
1016 flags
|= wxCONTROL_CHECKED
;
1017 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
1018 GetEnabled() == false)
1019 flags
|= wxCONTROL_DISABLED
;
1021 // Ensure that the check boxes always have at least the minimal required
1022 // size, otherwise DrawCheckBox() doesn't really work well. If this size is
1023 // greater than the cell size, the checkbox will be truncated but this is a
1025 wxSize size
= cell
.GetSize();
1026 size
.IncTo(GetSize());
1029 wxRendererNative::Get().DrawCheckBox(
1030 GetOwner()->GetOwner(),
1038 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1039 wxDataViewModel
*model
,
1040 const wxDataViewItem
& item
,
1042 const wxMouseEvent
*mouseEvent
)
1046 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1047 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1051 model
->ChangeValue(!m_toggle
, item
, col
);
1055 wxSize
wxDataViewToggleRenderer::GetSize() const
1057 // the window parameter is not used by GetCheckBoxSize() so it's
1058 // safe to pass NULL
1059 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1062 // ---------------------------------------------------------
1063 // wxDataViewProgressRenderer
1064 // ---------------------------------------------------------
1066 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1068 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1069 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1070 wxDataViewRenderer( varianttype
, mode
, align
)
1076 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1078 m_value
= (long) value
;
1080 if (m_value
< 0) m_value
= 0;
1081 if (m_value
> 100) m_value
= 100;
1086 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1088 value
= (long) m_value
;
1093 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1095 // deflate the rect to leave a small border between bars in adjacent rows
1096 wxRect bar
= rect
.Deflate(0, 1);
1098 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1099 dc
->SetPen( *wxBLACK_PEN
);
1100 dc
->DrawRectangle( bar
);
1102 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1103 dc
->SetPen( *wxTRANSPARENT_PEN
);
1105 const wxDataViewItemAttr
& attr
= GetAttr();
1106 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1108 dc
->DrawRectangle( bar
);
1113 wxSize
wxDataViewProgressRenderer::GetSize() const
1115 return wxSize(40,12);
1118 // ---------------------------------------------------------
1119 // wxDataViewIconTextRenderer
1120 // ---------------------------------------------------------
1122 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1124 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1125 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1126 wxDataViewRenderer( varianttype
, mode
, align
)
1129 SetAlignment(align
);
1132 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1138 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1143 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1147 const wxIcon
& icon
= m_value
.GetIcon();
1150 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1151 xoffset
= icon
.GetWidth()+4;
1154 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1159 wxSize
wxDataViewIconTextRenderer::GetSize() const
1161 if (!m_value
.GetText().empty())
1163 wxSize size
= GetTextExtent(m_value
.GetText());
1165 if (m_value
.GetIcon().IsOk())
1166 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1169 return wxSize(80,20);
1172 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1174 wxDataViewIconText iconText
;
1177 wxString text
= iconText
.GetText();
1179 // adjust the label rect to take the width of the icon into account
1180 if (iconText
.GetIcon().IsOk())
1182 int w
= iconText
.GetIcon().GetWidth() + 4;
1184 labelRect
.width
-= w
;
1187 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1188 wxPoint(labelRect
.x
,labelRect
.y
),
1189 wxSize(labelRect
.width
,labelRect
.height
),
1190 wxTE_PROCESS_ENTER
);
1192 // select the text in the control an place the cursor at the end
1193 ctrl
->SetInsertionPointEnd();
1199 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1201 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1203 // The icon can't be edited so get its old value and reuse it.
1205 wxDataViewColumn
* const col
= GetOwner();
1206 GetView()->GetModel()->GetValue(valueOld
, m_item
, col
->GetModelColumn());
1208 wxDataViewIconText iconText
;
1209 iconText
<< valueOld
;
1211 // But replace the text with the value entered by user.
1212 iconText
.SetText(text
->GetValue());
1218 //-----------------------------------------------------------------------------
1219 // wxDataViewDropTarget
1220 //-----------------------------------------------------------------------------
1222 #if wxUSE_DRAG_AND_DROP
1224 class wxBitmapCanvas
: public wxWindow
1227 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1228 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1231 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1234 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1237 dc
.DrawBitmap( m_bitmap
, 0, 0);
1243 class wxDataViewDropSource
: public wxDropSource
1246 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1254 ~wxDataViewDropSource()
1259 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1261 wxPoint pos
= wxGetMousePosition();
1265 int liney
= m_win
->GetLineStart( m_row
);
1267 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1268 m_win
->ClientToScreen( &linex
, &liney
);
1269 m_dist_x
= pos
.x
- linex
;
1270 m_dist_y
= pos
.y
- liney
;
1273 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1275 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1276 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1278 wxFRAME_TOOL_WINDOW
|
1279 wxFRAME_FLOAT_ON_PARENT
|
1280 wxFRAME_NO_TASKBAR
|
1282 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1287 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1288 m_hint
->SetTransparent( 128 );
1294 wxDataViewMainWindow
*m_win
;
1297 int m_dist_x
,m_dist_y
;
1301 class wxDataViewDropTarget
: public wxDropTarget
1304 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1310 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1312 wxDataFormat format
= GetMatchingPair();
1313 if (format
== wxDF_INVALID
)
1315 return m_win
->OnDragOver( format
, x
, y
, def
);
1318 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1320 wxDataFormat format
= GetMatchingPair();
1321 if (format
== wxDF_INVALID
)
1323 return m_win
->OnDrop( format
, x
, y
);
1326 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1328 wxDataFormat format
= GetMatchingPair();
1329 if (format
== wxDF_INVALID
)
1333 return m_win
->OnData( format
, x
, y
, def
);
1336 virtual void OnLeave()
1337 { m_win
->OnLeave(); }
1339 wxDataViewMainWindow
*m_win
;
1342 #endif // wxUSE_DRAG_AND_DROP
1344 //-----------------------------------------------------------------------------
1345 // wxDataViewRenameTimer
1346 //-----------------------------------------------------------------------------
1348 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1353 void wxDataViewRenameTimer::Notify()
1355 m_owner
->OnRenameTimer();
1358 //-----------------------------------------------------------------------------
1359 // wxDataViewMainWindow
1360 //-----------------------------------------------------------------------------
1362 // The tree building helper, declared firstly
1363 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1364 wxDataViewTreeNode
* node
);
1366 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1368 if (row1
> row2
) return 1;
1369 if (row1
== row2
) return 0;
1373 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1375 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1376 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1377 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1378 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1379 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1380 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook
)
1381 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1384 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1385 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1386 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1387 m_selection( wxDataViewSelectionCmp
)
1392 m_editorRenderer
= NULL
;
1394 m_lastOnSame
= false;
1395 m_renameTimer
= new wxDataViewRenameTimer( this );
1397 // TODO: user better initial values/nothing selected
1398 m_currentCol
= NULL
;
1399 m_currentColSetByKeyboard
= false;
1400 m_useCellFocus
= false;
1401 m_currentRow
= (unsigned)-1;
1402 m_lineHeight
= GetDefaultRowHeight();
1404 #if wxUSE_DRAG_AND_DROP
1406 m_dragStart
= wxPoint(0,0);
1408 m_dragEnabled
= false;
1409 m_dropEnabled
= false;
1411 m_dropHintLine
= (unsigned int) -1;
1412 #endif // wxUSE_DRAG_AND_DROP
1414 m_lineLastClicked
= (unsigned int) -1;
1415 m_lineBeforeLastClicked
= (unsigned int) -1;
1416 m_lineSelectSingleOnUp
= (unsigned int) -1;
1420 SetBackgroundColour( *wxWHITE
);
1422 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1424 m_penRule
= wxPen(GetRuleColour());
1426 // compose a pen whichcan draw black lines
1427 // TODO: maybe there is something system colour to use
1428 m_penExpander
= wxPen(wxColour(0,0,0));
1430 m_root
= wxDataViewTreeNode::CreateRootNode();
1432 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1434 m_underMouse
= NULL
;
1438 wxDataViewMainWindow::~wxDataViewMainWindow()
1441 delete m_renameTimer
;
1445 int wxDataViewMainWindow::GetDefaultRowHeight() const
1448 // We would like to use the same line height that Explorer uses. This is
1449 // different from standard ListView control since Vista.
1450 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
1451 return wxMax(16, GetCharHeight()) + 6; // 16 = mini icon height
1454 return wxMax(16, GetCharHeight()) + 1; // 16 = mini icon height
1459 #if wxUSE_DRAG_AND_DROP
1460 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1462 m_dragFormat
= format
;
1463 m_dragEnabled
= format
!= wxDF_INVALID
;
1468 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1470 m_dropFormat
= format
;
1471 m_dropEnabled
= format
!= wxDF_INVALID
;
1474 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1479 void wxDataViewMainWindow::RemoveDropHint()
1484 RefreshRow( m_dropHintLine
);
1485 m_dropHintLine
= (unsigned int) -1;
1489 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1490 wxCoord y
, wxDragResult def
)
1494 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1495 unsigned int row
= GetLineAt( yy
);
1497 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1503 wxDataViewItem item
= GetItemByRow( row
);
1505 wxDataViewModel
*model
= GetModel();
1507 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1508 event
.SetEventObject( m_owner
);
1509 event
.SetItem( item
);
1510 event
.SetModel( model
);
1511 event
.SetDataFormat( format
);
1512 event
.SetDropEffect( def
);
1513 if (!m_owner
->HandleWindowEvent( event
))
1519 if (!event
.IsAllowed())
1526 if (m_dropHint
&& (row
!= m_dropHintLine
))
1527 RefreshRow( m_dropHintLine
);
1529 m_dropHintLine
= row
;
1535 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1541 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1542 unsigned int row
= GetLineAt( yy
);
1544 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1547 wxDataViewItem item
= GetItemByRow( row
);
1549 wxDataViewModel
*model
= GetModel();
1551 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1552 event
.SetEventObject( m_owner
);
1553 event
.SetItem( item
);
1554 event
.SetModel( model
);
1555 event
.SetDataFormat( format
);
1556 if (!m_owner
->HandleWindowEvent( event
))
1559 if (!event
.IsAllowed())
1565 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1570 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1571 unsigned int row
= GetLineAt( yy
);
1573 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1576 wxDataViewItem item
= GetItemByRow( row
);
1578 wxDataViewModel
*model
= GetModel();
1580 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1582 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1583 event
.SetEventObject( m_owner
);
1584 event
.SetItem( item
);
1585 event
.SetModel( model
);
1586 event
.SetDataFormat( format
);
1587 event
.SetDataSize( obj
->GetSize() );
1588 event
.SetDataBuffer( obj
->GetData() );
1589 event
.SetDropEffect( def
);
1590 if (!m_owner
->HandleWindowEvent( event
))
1593 if (!event
.IsAllowed())
1599 void wxDataViewMainWindow::OnLeave()
1604 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1606 int height
= GetLineHeight( row
);
1608 unsigned int cols
= GetOwner()->GetColumnCount();
1610 for (col
= 0; col
< cols
; col
++)
1612 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1613 if (column
->IsHidden())
1614 continue; // skip it!
1615 width
+= column
->GetWidth();
1621 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1622 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1623 indent
= indent
+ m_lineHeight
;
1624 // try to use the m_lineHeight as the expander space
1628 wxBitmap
bitmap( width
, height
);
1629 wxMemoryDC
dc( bitmap
);
1630 dc
.SetFont( GetFont() );
1631 dc
.SetPen( *wxBLACK_PEN
);
1632 dc
.SetBrush( *wxWHITE_BRUSH
);
1633 dc
.DrawRectangle( 0,0,width
,height
);
1635 wxDataViewModel
*model
= m_owner
->GetModel();
1637 wxDataViewColumn
* const
1638 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1641 for (col
= 0; col
< cols
; col
++)
1643 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1644 wxDataViewRenderer
*cell
= column
->GetRenderer();
1646 if (column
->IsHidden())
1647 continue; // skip it!
1649 width
= column
->GetWidth();
1651 if (column
== expander
)
1654 wxDataViewItem item
= GetItemByRow( row
);
1655 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1657 wxRect
item_rect(x
, 0, width
, height
);
1658 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1660 // dc.SetClippingRegion( item_rect );
1661 cell
->WXCallRender(item_rect
, &dc
, 0);
1662 // dc.DestroyClippingRegion();
1670 #endif // wxUSE_DRAG_AND_DROP
1673 // Draw focus rect for individual cell. Unlike native focus rect, we render
1674 // this in foreground text color (typically white) to enhance contrast and
1676 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1678 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1680 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1681 // may be short traits and not really dots
1683 // note that to behave in the same manner as DrawRect(), we must exclude
1684 // the bottom and right borders from the rectangle
1685 wxCoord x1
= rect
.GetLeft(),
1687 x2
= rect
.GetRight(),
1688 y2
= rect
.GetBottom();
1690 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1693 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1694 dc
.DrawPoint(z
, rect
.GetTop());
1696 wxCoord shift
= z
== x2
? 0 : 1;
1697 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1698 dc
.DrawPoint(x2
, z
);
1700 shift
= z
== y2
? 0 : 1;
1701 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1702 dc
.DrawPoint(z
, y2
);
1704 shift
= z
== x1
? 0 : 1;
1705 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1706 dc
.DrawPoint(x1
, z
);
1710 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1712 wxDataViewModel
*model
= GetModel();
1713 wxAutoBufferedPaintDC
dc( this );
1715 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1716 dc
.SetPen( *wxTRANSPARENT_PEN
);
1717 dc
.DrawRectangle(GetClientSize());
1721 // No items to draw.
1726 GetOwner()->PrepareDC( dc
);
1727 dc
.SetFont( GetFont() );
1729 wxRect update
= GetUpdateRegion().GetBox();
1730 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1732 // compute which items needs to be redrawn
1733 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1734 unsigned int item_count
=
1735 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1736 (int)(GetRowCount( ) - item_start
));
1737 unsigned int item_last
= item_start
+ item_count
;
1739 // Send the event to wxDataViewCtrl itself.
1740 wxWindow
* const parent
= GetParent();
1741 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1742 cache_event
.SetEventObject(parent
);
1743 cache_event
.SetCache(item_start
, item_last
- 1);
1744 parent
->ProcessWindowEvent(cache_event
);
1746 // compute which columns needs to be redrawn
1747 unsigned int cols
= GetOwner()->GetColumnCount();
1750 // we assume that we have at least one column below and painting an
1751 // empty control is unnecessary anyhow
1755 unsigned int col_start
= 0;
1756 unsigned int x_start
;
1757 for (x_start
= 0; col_start
< cols
; col_start
++)
1759 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1760 if (col
->IsHidden())
1761 continue; // skip it!
1763 unsigned int w
= col
->GetWidth();
1764 if (x_start
+w
>= (unsigned int)update
.x
)
1770 unsigned int col_last
= col_start
;
1771 unsigned int x_last
= x_start
;
1772 for (; col_last
< cols
; col_last
++)
1774 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1775 if (col
->IsHidden())
1776 continue; // skip it!
1778 if (x_last
> (unsigned int)update
.GetRight())
1781 x_last
+= col
->GetWidth();
1784 // Draw background of alternate rows specially if required
1785 if ( m_owner
->HasFlag(wxDV_ROW_LINES
) )
1787 wxColour altRowColour
= m_owner
->m_alternateRowColour
;
1788 if ( !altRowColour
.IsOk() )
1790 // Determine the alternate rows colour automatically from the
1791 // background colour.
1792 const wxColour bgColour
= m_owner
->GetBackgroundColour();
1794 // Depending on the background, alternate row color
1795 // will be 3% more dark or 50% brighter.
1796 int alpha
= bgColour
.GetRGB() > 0x808080 ? 97 : 150;
1797 altRowColour
= bgColour
.ChangeLightness(alpha
);
1800 dc
.SetPen(*wxTRANSPARENT_PEN
);
1801 dc
.SetBrush(wxBrush(altRowColour
));
1803 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1807 dc
.DrawRectangle(x_start
,
1809 GetClientSize().GetWidth(),
1810 GetLineHeight(item
));
1815 // Draw horizontal rules if required
1816 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1818 dc
.SetPen(m_penRule
);
1819 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1821 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1823 int y
= GetLineStart( i
);
1824 dc
.DrawLine(x_start
, y
, x_last
, y
);
1828 // Draw vertical rules if required
1829 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1831 dc
.SetPen(m_penRule
);
1832 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1834 // NB: Vertical rules are drawn in the last pixel of a column so that
1835 // they align perfectly with native MSW wxHeaderCtrl as well as for
1836 // consistency with MSW native list control. There's no vertical
1837 // rule at the most-left side of the control.
1839 int x
= x_start
- 1;
1840 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1842 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1843 if (col
->IsHidden())
1844 continue; // skip it
1846 x
+= col
->GetWidth();
1848 dc
.DrawLine(x
, GetLineStart( item_start
),
1849 x
, GetLineStart( item_last
) );
1853 // redraw the background for the items which are selected/current
1854 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1856 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1858 if (selected
|| item
== m_currentRow
)
1860 wxRect
rect( x_start
, GetLineStart( item
),
1861 x_last
- x_start
, GetLineHeight( item
) );
1863 // draw selection and whole-item focus:
1866 int flags
= wxCONTROL_SELECTED
;
1868 flags
|= wxCONTROL_FOCUSED
;
1870 wxRendererNative::Get().DrawItemSelectionRect
1879 // draw keyboard focus rect if applicable
1880 if ( item
== m_currentRow
&& m_hasFocus
)
1882 bool renderColumnFocus
= false;
1884 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1886 renderColumnFocus
= true;
1888 // If this is container node without columns, render full-row focus:
1891 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1892 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1893 renderColumnFocus
= false;
1897 if ( renderColumnFocus
)
1899 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1901 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1902 if ( col
->IsHidden() )
1905 rect
.width
= col
->GetWidth();
1907 if ( col
== m_currentCol
)
1909 // make the rect more visible by adding a small
1910 // margin around it:
1915 // DrawFocusRect() uses XOR and is all but
1916 // invisible against dark-blue background. Use
1917 // the same color used for selected text.
1918 DrawSelectedCellFocusRect(dc
, rect
);
1922 wxRendererNative::Get().DrawFocusRect
1933 rect
.x
+= rect
.width
;
1938 // render focus rectangle for the whole row
1939 wxRendererNative::Get().DrawFocusRect
1944 selected
? (int)wxCONTROL_SELECTED
: 0
1951 #if wxUSE_DRAG_AND_DROP
1954 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1955 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1956 dc
.SetPen( *wxBLACK_PEN
);
1957 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1958 dc
.DrawRectangle( rect
);
1960 #endif // wxUSE_DRAG_AND_DROP
1962 wxDataViewColumn
* const
1963 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1965 // redraw all cells for all rows which must be repainted and all columns
1967 cell_rect
.x
= x_start
;
1968 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1970 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1971 wxDataViewRenderer
*cell
= col
->GetRenderer();
1972 cell_rect
.width
= col
->GetWidth();
1974 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1975 continue; // skip it!
1977 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1979 // get the cell value and set it into the renderer
1980 wxDataViewTreeNode
*node
= NULL
;
1981 wxDataViewItem dataitem
;
1983 if (!IsVirtualList())
1985 node
= GetTreeNodeByRow(item
);
1989 dataitem
= node
->GetItem();
1991 // Skip all columns of "container" rows except the expander
1992 // column itself unless HasContainerColumns() overrides this.
1993 if ( col
!= expander
&&
1994 model
->IsContainer(dataitem
) &&
1995 !model
->HasContainerColumns(dataitem
) )
2000 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
2003 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
2006 cell_rect
.y
= GetLineStart( item
);
2007 cell_rect
.height
= GetLineHeight( item
);
2009 // draw the background
2010 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2012 DrawCellBackground( cell
, dc
, cell_rect
);
2014 // deal with the expander
2016 if ((!IsList()) && (col
== expander
))
2018 // Calculate the indent first
2019 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
2021 // we reserve m_lineHeight of horizontal space for the expander
2022 // but leave EXPANDER_MARGIN around the expander itself
2023 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
2025 indent
+= m_lineHeight
;
2027 // draw expander if needed and visible
2028 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
2030 dc
.SetPen( m_penExpander
);
2031 dc
.SetBrush( wxNullBrush
);
2033 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2034 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
2035 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
2037 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
2040 if ( m_underMouse
== node
)
2041 flag
|= wxCONTROL_CURRENT
;
2042 if ( node
->IsOpen() )
2043 flag
|= wxCONTROL_EXPANDED
;
2045 // ensure that we don't overflow the cell (which might
2046 // happen if the column is very narrow)
2047 wxDCClipper
clip(dc
, cell_rect
);
2049 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2052 // force the expander column to left-center align
2053 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2056 wxRect item_rect
= cell_rect
;
2057 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
2059 // account for the tree indent (harmless if we're not indented)
2060 item_rect
.x
+= indent
;
2061 item_rect
.width
-= indent
;
2063 if ( item_rect
.width
<= 0 )
2067 if (m_hasFocus
&& selected
)
2068 state
|= wxDATAVIEW_CELL_SELECTED
;
2070 // TODO: it would be much more efficient to create a clipping
2071 // region for the entire column being rendered (in the OnPaint
2072 // of wxDataViewMainWindow) instead of a single clip region for
2073 // each cell. However it would mean that each renderer should
2074 // respect the given wxRect's top & bottom coords, eventually
2075 // violating only the left & right coords - however the user can
2076 // make its own renderer and thus we cannot be sure of that.
2077 wxDCClipper
clip(dc
, item_rect
);
2079 cell
->WXCallRender(item_rect
, &dc
, state
);
2082 cell_rect
.x
+= cell_rect
.width
;
2087 void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
)
2089 wxRect
rectBg( rect
);
2091 // don't overlap the horizontal rules
2092 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2098 // don't overlap the vertical rules
2099 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2105 cell
->RenderBackground(&dc
, rectBg
);
2108 void wxDataViewMainWindow::OnRenameTimer()
2110 // We have to call this here because changes may just have
2111 // been made and no screen update taken place.
2114 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2115 // (needs to be tested!)
2119 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2121 StartEditing( item
, m_currentCol
);
2125 wxDataViewMainWindow::StartEditing(const wxDataViewItem
& item
,
2126 const wxDataViewColumn
* col
)
2128 wxDataViewRenderer
* renderer
= col
->GetRenderer();
2129 if ( !IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
2132 const wxRect itemRect
= GetItemRect(item
, col
);
2133 if ( renderer
->StartEditing(item
, itemRect
) )
2135 // Save the renderer to be able to finish/cancel editing it later and
2136 // save the control to be able to detect if we're still editing it.
2137 m_editorRenderer
= renderer
;
2138 m_editorCtrl
= renderer
->GetEditorCtrl();
2142 //-----------------------------------------------------------------------------
2143 // Helper class for do operation on the tree node
2144 //-----------------------------------------------------------------------------
2149 virtual ~DoJob() { }
2151 // The return value control how the tree-walker tranverse the tree
2154 DONE
, // Job done, stop traversing and return
2155 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2156 CONTINUE
// Job not done, continue
2159 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2162 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2164 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2166 switch( func( node
) )
2170 case DoJob::SKIP_SUBTREE
:
2172 case DoJob::CONTINUE
:
2176 if ( node
->HasChildren() )
2178 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2180 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2184 if ( Walker(*i
, func
) )
2192 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2194 if (IsVirtualList())
2196 wxDataViewVirtualListModel
*list_model
=
2197 (wxDataViewVirtualListModel
*) GetModel();
2198 m_count
= list_model
->GetCount();
2204 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2209 wxDataViewItemArray modelSiblings
;
2210 GetModel()->GetChildren(parent
, modelSiblings
);
2211 const int modelSiblingsSize
= modelSiblings
.size();
2213 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2214 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2216 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2217 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2219 parentNode
->SetHasChildren(true);
2221 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2222 const int nodeSiblingsSize
= nodeSiblings
.size();
2226 if ( posInModel
== modelSiblingsSize
- 1 )
2228 nodePos
= nodeSiblingsSize
;
2230 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2232 // This is the simple case when our node tree already matches the
2233 // model and only this one item is missing.
2234 nodePos
= posInModel
;
2238 // It's possible that a larger discrepancy between the model and
2239 // our realization exists. This can happen e.g. when adding a bunch
2240 // of items to the model and then calling ItemsAdded() just once
2241 // afterwards. In this case, we must find the right position by
2242 // looking at sibling items.
2244 // append to the end if we won't find a better position:
2245 nodePos
= nodeSiblingsSize
;
2247 for ( int nextItemPos
= posInModel
+ 1;
2248 nextItemPos
< modelSiblingsSize
;
2251 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2252 if ( nextNodePos
!= wxNOT_FOUND
)
2254 nodePos
= nextNodePos
;
2260 parentNode
->ChangeSubTreeCount(+1);
2261 parentNode
->InsertChild(itemNode
, nodePos
);
2266 GetOwner()->InvalidateColBestWidths();
2272 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2273 const wxDataViewItem
& item
)
2275 if (IsVirtualList())
2277 wxDataViewVirtualListModel
*list_model
=
2278 (wxDataViewVirtualListModel
*) GetModel();
2279 m_count
= list_model
->GetCount();
2281 if ( !m_selection
.empty() )
2283 const int row
= GetRowByItem(item
);
2285 int rowIndexInSelection
= wxNOT_FOUND
;
2287 const size_t selCount
= m_selection
.size();
2288 for ( size_t i
= 0; i
< selCount
; i
++ )
2290 if ( m_selection
[i
] == (unsigned)row
)
2291 rowIndexInSelection
= i
;
2292 else if ( m_selection
[i
] > (unsigned)row
)
2296 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2297 m_selection
.RemoveAt(rowIndexInSelection
);
2301 else // general case
2303 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2305 // Notice that it is possible that the item being deleted is not in the
2306 // tree at all, for example we could be deleting a never shown (because
2307 // collapsed) item in a tree model. So it's not an error if we don't know
2308 // about this item, just return without doing anything then.
2312 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2313 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2315 // We can't use FindNode() to find 'item', because it was already
2316 // removed from the model by the time ItemDeleted() is called, so we
2317 // have to do it manually. We keep track of its position as well for
2319 int itemPosInNode
= 0;
2320 wxDataViewTreeNode
*itemNode
= NULL
;
2321 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2322 i
!= parentsChildren
.end();
2323 ++i
, ++itemPosInNode
)
2325 if( (*i
)->GetItem() == item
)
2332 // If the parent wasn't expanded, it's possible that we didn't have a
2333 // node corresponding to 'item' and so there's nothing left to do.
2336 // If this was the last child to be removed, it's possible the parent
2337 // node became a leaf. Let's ask the model about it.
2338 if ( parentNode
->GetChildNodes().empty() )
2339 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2344 // Delete the item from wxDataViewTreeNode representation:
2345 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2347 parentNode
->RemoveChild(itemNode
);
2349 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2351 // Make the row number invalid and get a new valid one when user call GetRowCount
2354 // If this was the last child to be removed, it's possible the parent
2355 // node became a leaf. Let's ask the model about it.
2356 if ( parentNode
->GetChildNodes().empty() )
2358 bool isContainer
= GetModel()->IsContainer(parent
);
2359 parentNode
->SetHasChildren(isContainer
);
2362 // If it's still a container, make sure we show "+" icon for it
2363 // and not "-" one as there is nothing to collapse any more.
2364 if ( parentNode
->IsOpen() )
2365 parentNode
->ToggleOpen();
2369 // Update selection by removing 'item' and its entire children tree from the selection.
2370 if ( !m_selection
.empty() )
2372 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2373 // the parent ('parentNode') and position in its list of children
2375 if ( itemPosInNode
== 0 )
2377 // 1st child, row number is that of the parent parentNode + 1
2378 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2382 // row number is that of the sibling above 'item' + its subtree if any + 1
2383 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2385 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2386 siblingNode
->GetSubTreeCount() +
2390 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2392 const size_t numSelections
= m_selection
.size();
2393 for ( size_t i
= 0; i
< numSelections
; ++i
)
2395 const int s
= m_selection
[i
];
2397 newsel
.push_back(s
);
2398 else if ( s
>= itemRow
+ itemsDeleted
)
2399 newsel
.push_back(s
- itemsDeleted
);
2400 // else: deleted item, remove from selection
2403 m_selection
= newsel
;
2407 // Change the current row to the last row if the current exceed the max row number
2408 if ( m_currentRow
>= GetRowCount() )
2409 ChangeCurrentRow(m_count
- 1);
2411 GetOwner()->InvalidateColBestWidths();
2417 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2422 GetOwner()->InvalidateColBestWidths();
2425 wxWindow
*parent
= GetParent();
2426 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2427 le
.SetEventObject(parent
);
2428 le
.SetModel(GetModel());
2430 parent
->ProcessWindowEvent(le
);
2435 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2437 int view_column
= -1;
2438 unsigned int n_col
= m_owner
->GetColumnCount();
2439 for (unsigned i
= 0; i
< n_col
; i
++)
2441 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2442 if (column
->GetModelColumn() == model_column
)
2444 view_column
= (int) i
;
2448 if (view_column
== -1)
2451 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2452 /*#define MAX_VIRTUAL_WIDTH 100000
2454 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2455 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2456 Refresh( true, &rect );
2463 GetOwner()->InvalidateColBestWidth(view_column
);
2466 wxWindow
*parent
= GetParent();
2467 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2468 le
.SetEventObject(parent
);
2469 le
.SetModel(GetModel());
2471 le
.SetColumn(view_column
);
2472 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2473 parent
->ProcessWindowEvent(le
);
2478 bool wxDataViewMainWindow::Cleared()
2481 m_selection
.Clear();
2482 m_currentRow
= (unsigned)-1;
2487 BuildTree( GetModel() );
2494 GetOwner()->InvalidateColBestWidths();
2500 void wxDataViewMainWindow::UpdateDisplay()
2503 m_underMouse
= NULL
;
2506 void wxDataViewMainWindow::OnInternalIdle()
2508 wxWindow::OnInternalIdle();
2512 RecalculateDisplay();
2517 void wxDataViewMainWindow::RecalculateDisplay()
2519 wxDataViewModel
*model
= GetModel();
2526 int width
= GetEndOfLastCol();
2527 int height
= GetLineStart( GetRowCount() );
2529 SetVirtualSize( width
, height
);
2530 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2535 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2537 m_underMouse
= NULL
;
2539 wxWindow::ScrollWindow( dx
, dy
, rect
);
2541 if (GetOwner()->m_headerArea
)
2542 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2545 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2547 m_underMouse
= NULL
;
2550 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2551 int sy
= GetLineStart( rows
)/y
;
2555 wxRect rect
= GetClientRect();
2559 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2560 for (x_start
= 0; colnum
< column
; colnum
++)
2562 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2563 if (col
->IsHidden())
2564 continue; // skip it!
2566 w
= col
->GetWidth();
2570 int x_end
= x_start
+ w
;
2571 xe
= xx
+ rect
.width
;
2574 sx
= ( xx
+ x_end
- xe
)/x
;
2581 m_owner
->Scroll( sx
, sy
);
2584 int wxDataViewMainWindow::GetCountPerPage() const
2586 wxSize size
= GetClientSize();
2587 return size
.y
/ m_lineHeight
;
2590 int wxDataViewMainWindow::GetEndOfLastCol() const
2594 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2596 const wxDataViewColumn
*c
=
2597 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2600 width
+= c
->GetWidth();
2605 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2609 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2611 return GetLineAt( y
);
2614 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2616 wxSize client_size
= GetClientSize();
2617 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2618 &client_size
.x
, &client_size
.y
);
2620 // we should deal with the pixel here
2621 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2623 return wxMin( GetRowCount()-1, row
);
2626 unsigned int wxDataViewMainWindow::GetRowCount() const
2628 if ( m_count
== -1 )
2630 wxDataViewMainWindow
* const
2631 self
= const_cast<wxDataViewMainWindow
*>(this);
2632 self
->m_count
= RecalculateCount();
2633 self
->UpdateDisplay();
2638 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2645 void wxDataViewMainWindow::SelectAllRows( bool on
)
2652 m_selection
.Clear();
2653 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2654 m_selection
.Add( i
);
2659 unsigned int first_visible
= GetFirstVisibleRow();
2660 unsigned int last_visible
= GetLastVisibleRow();
2662 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2664 unsigned int row
= m_selection
[i
];
2665 if ((row
>= first_visible
) && (row
<= last_visible
))
2668 m_selection
.Clear();
2672 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2674 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2678 m_selection
.Add( row
);
2686 m_selection
.Remove( row
);
2692 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2696 unsigned int tmp
= from
;
2702 for (i
= from
; i
<= to
; i
++)
2704 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2707 m_selection
.Add( i
);
2712 m_selection
.Remove( i
);
2715 RefreshRows( from
, to
);
2718 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2720 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2722 int n
= aSelections
[i
];
2724 m_selection
.Add( n
);
2729 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2731 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2732 m_selection
.Add( row
);
2734 m_selection
.Remove( row
);
2738 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2740 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2743 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2745 wxWindow
*parent
= GetParent();
2746 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2748 le
.SetEventObject(parent
);
2749 le
.SetModel(GetModel());
2752 parent
->ProcessWindowEvent(le
);
2755 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2757 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2758 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2760 wxSize client_size
= GetClientSize();
2761 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2762 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2763 if (intersect_rect
.width
> 0)
2764 Refresh( true, &intersect_rect
);
2767 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2771 unsigned int tmp
= to
;
2776 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2777 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2779 wxSize client_size
= GetClientSize();
2780 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2781 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2782 if (intersect_rect
.width
> 0)
2783 Refresh( true, &intersect_rect
);
2786 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2788 wxSize client_size
= GetClientSize();
2789 int start
= GetLineStart( firstRow
);
2790 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2791 if (start
> client_size
.y
) return;
2793 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2795 Refresh( true, &rect
);
2798 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2802 rect
.y
= GetLineStart( row
);
2803 rect
.width
= GetEndOfLastCol();
2804 rect
.height
= GetLineHeight( row
);
2809 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2811 const wxDataViewModel
*model
= GetModel();
2813 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2815 // TODO make more efficient
2820 for (r
= 0; r
< row
; r
++)
2822 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2823 if (!node
) return start
;
2825 wxDataViewItem item
= node
->GetItem();
2827 unsigned int cols
= GetOwner()->GetColumnCount();
2829 int height
= m_lineHeight
;
2830 for (col
= 0; col
< cols
; col
++)
2832 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2833 if (column
->IsHidden())
2834 continue; // skip it!
2837 model
->IsContainer(item
) &&
2838 !model
->HasContainerColumns(item
))
2839 continue; // skip it!
2841 wxDataViewRenderer
*renderer
=
2842 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2843 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2845 height
= wxMax( height
, renderer
->GetSize().y
);
2855 return row
* m_lineHeight
;
2859 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2861 const wxDataViewModel
*model
= GetModel();
2863 // check for the easy case first
2864 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2865 return y
/ m_lineHeight
;
2867 // TODO make more efficient
2868 unsigned int row
= 0;
2869 unsigned int yy
= 0;
2872 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2875 // not really correct...
2876 return row
+ ((y
-yy
) / m_lineHeight
);
2879 wxDataViewItem item
= node
->GetItem();
2881 unsigned int cols
= GetOwner()->GetColumnCount();
2883 int height
= m_lineHeight
;
2884 for (col
= 0; col
< cols
; col
++)
2886 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2887 if (column
->IsHidden())
2888 continue; // skip it!
2891 model
->IsContainer(item
) &&
2892 !model
->HasContainerColumns(item
))
2893 continue; // skip it!
2895 wxDataViewRenderer
*renderer
=
2896 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2897 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2899 height
= wxMax( height
, renderer
->GetSize().y
);
2910 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2912 const wxDataViewModel
*model
= GetModel();
2914 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2916 wxASSERT( !IsVirtualList() );
2918 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2919 // wxASSERT( node );
2920 if (!node
) return m_lineHeight
;
2922 wxDataViewItem item
= node
->GetItem();
2924 int height
= m_lineHeight
;
2926 unsigned int cols
= GetOwner()->GetColumnCount();
2928 for (col
= 0; col
< cols
; col
++)
2930 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2931 if (column
->IsHidden())
2932 continue; // skip it!
2935 model
->IsContainer(item
) &&
2936 !model
->HasContainerColumns(item
))
2937 continue; // skip it!
2939 wxDataViewRenderer
*renderer
=
2940 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2941 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2943 height
= wxMax( height
, renderer
->GetSize().y
);
2950 return m_lineHeight
;
2955 class RowToTreeNodeJob
: public DoJob
2958 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2961 this->current
= current
;
2966 virtual int operator() ( wxDataViewTreeNode
* node
)
2969 if( current
== static_cast<int>(row
))
2975 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2977 current
+= node
->GetSubTreeCount();
2978 return DoJob::SKIP_SUBTREE
;
2984 // If the current node has only leaf children, we can find the
2985 // desired node directly. This can speed up finding the node
2986 // in some cases, and will have a very good effect for list views.
2987 if ( node
->HasChildren() &&
2988 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2990 const int index
= static_cast<int>(row
) - current
- 1;
2991 ret
= node
->GetChildNodes()[index
];
2995 return DoJob::CONTINUE
;
2999 wxDataViewTreeNode
* GetResult() const
3005 wxDataViewTreeNode
* ret
;
3006 wxDataViewTreeNode
* parent
;
3009 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
3011 wxASSERT( !IsVirtualList() );
3013 if ( row
== (unsigned)-1 )
3016 RowToTreeNodeJob
job( row
, -2, m_root
);
3017 Walker( m_root
, job
);
3018 return job
.GetResult();
3021 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
3023 wxDataViewItem item
;
3024 if (IsVirtualList())
3026 if ( row
< GetRowCount() )
3027 item
= wxDataViewItem(wxUIntToPtr(row
+1));
3031 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3033 item
= node
->GetItem();
3040 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
3041 const wxDataViewItem
& item
)
3043 wxWindow
*parent
= GetParent();
3044 wxDataViewEvent
le(type
, parent
->GetId());
3046 le
.SetEventObject(parent
);
3047 le
.SetModel(GetModel());
3050 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
3053 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
3058 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3062 if (!node
->HasChildren())
3065 return node
->IsOpen();
3068 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
3073 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3077 if (!node
->HasChildren())
3083 void wxDataViewMainWindow::Expand( unsigned int row
)
3088 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3092 if (!node
->HasChildren())
3095 if (!node
->IsOpen())
3097 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
3099 // Vetoed by the event handler.
3105 // build the children of current node
3106 if( node
->GetChildNodes().empty() )
3109 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
3112 // By expanding the node all row indices that are currently in the selection list
3113 // and are greater than our node have become invalid. So we have to correct that now.
3114 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3115 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3117 const unsigned testRow
= m_selection
[i
];
3118 // all rows above us are not affected, so skip them
3122 m_selection
[i
] += rowAdjustment
;
3125 if(m_currentRow
> row
)
3126 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3130 // Send the expanded event
3131 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3135 void wxDataViewMainWindow::Collapse(unsigned int row
)
3140 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3144 if (!node
->HasChildren())
3149 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3151 // Vetoed by the event handler.
3155 // Find out if there are selected items below the current node.
3156 bool selectCollapsingRow
= false;
3157 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3158 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3159 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3161 const unsigned testRow
= m_selection
[i
];
3162 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3164 selectCollapsingRow
= true;
3165 // get out as soon as we have found a node that is selected
3172 // If the node to be closed has selected items the user won't see those any longer.
3173 // We select the collapsing node in this case.
3174 if(selectCollapsingRow
)
3176 SelectAllRows(false);
3177 ChangeCurrentRow(row
);
3178 SelectRow(row
, true);
3179 SendSelectionChangedEvent(GetItemByRow(row
));
3183 // if there were no selected items below our node we still need to "fix" the
3184 // selection list to adjust for the changing of the row indices.
3185 // We actually do the opposite of what we are doing in Expand().
3186 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3188 const unsigned testRow
= m_selection
[i
];
3189 // all rows above us are not affected, so skip them
3193 m_selection
[i
] -= rowAdjustment
;
3196 // if the "current row" is being collapsed away we change it to the current row ;-)
3197 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3198 ChangeCurrentRow(row
);
3199 else if(m_currentRow
> row
)
3200 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3205 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3209 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3211 const wxDataViewModel
* model
= GetModel();
3218 // Compose the parent-chain for the item we are looking for
3219 wxVector
<wxDataViewItem
> parentChain
;
3220 wxDataViewItem
it( item
);
3223 parentChain
.push_back(it
);
3224 it
= model
->GetParent(it
);
3227 // Find the item along the parent-chain.
3228 // This algorithm is designed to speed up the node-finding method
3229 wxDataViewTreeNode
* node
= m_root
;
3230 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3232 if( node
->HasChildren() )
3234 if( node
->GetChildNodes().empty() )
3236 // Even though the item is a container, it doesn't have any
3237 // child nodes in the control's representation yet. We have
3238 // to realize its subtree now.
3240 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3243 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3246 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3248 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3249 if (currentNode
->GetItem() == parentChain
[iter
])
3251 if (currentNode
->GetItem() == item
)
3271 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3272 wxDataViewColumn
* &column
)
3274 wxDataViewColumn
*col
= NULL
;
3275 unsigned int cols
= GetOwner()->GetColumnCount();
3276 unsigned int colnum
= 0;
3278 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3279 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3281 col
= GetOwner()->GetColumnAt(colnum
);
3282 if (col
->IsHidden())
3283 continue; // skip it!
3285 unsigned int w
= col
->GetWidth();
3286 if (x_start
+w
>= (unsigned int)x
)
3293 item
= GetItemByRow( GetLineAt( y
) );
3296 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3297 const wxDataViewColumn
* column
)
3302 unsigned int cols
= GetOwner()->GetColumnCount();
3303 // If column is null the loop will compute the combined width of all columns.
3304 // Otherwise, it will compute the x position of the column we are looking for.
3305 for (unsigned int i
= 0; i
< cols
; i
++)
3307 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3312 if (col
->IsHidden())
3313 continue; // skip it!
3315 xpos
+= col
->GetWidth();
3316 width
+= col
->GetWidth();
3321 // If we have a column, we need can get its width directly.
3322 if(column
->IsHidden())
3325 width
= column
->GetWidth();
3330 // If we have no column, we reset the x position back to zero.
3334 // we have to take an expander column into account and compute its indentation
3335 // to get the correct x position where the actual text is
3337 int row
= GetRowByItem(item
);
3339 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3341 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3342 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3343 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3346 wxRect
itemRect( xpos
+ indent
,
3347 GetLineStart( row
),
3349 GetLineHeight( row
) );
3351 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3352 &itemRect
.x
, &itemRect
.y
);
3357 int wxDataViewMainWindow::RecalculateCount() const
3359 if (IsVirtualList())
3361 wxDataViewVirtualListModel
*list_model
=
3362 (wxDataViewVirtualListModel
*) GetModel();
3364 return list_model
->GetCount();
3368 return m_root
->GetSubTreeCount();
3372 class ItemToRowJob
: public DoJob
3375 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3382 // Maybe binary search will help to speed up this process
3383 virtual int operator() ( wxDataViewTreeNode
* node
)
3386 if( node
->GetItem() == item
)
3391 if( node
->GetItem() == *m_iter
)
3394 return DoJob::CONTINUE
;
3398 ret
+= node
->GetSubTreeCount();
3399 return DoJob::SKIP_SUBTREE
;
3404 // the row number is begin from zero
3405 int GetResult() const
3409 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3410 wxDataViewItem item
;
3415 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3417 const wxDataViewModel
* model
= GetModel();
3421 if (IsVirtualList())
3423 return wxPtrToUInt( item
.GetID() ) -1;
3430 // Compose the parent-chain of the item we are looking for
3431 wxVector
<wxDataViewItem
> parentChain
;
3432 wxDataViewItem
it( item
);
3435 parentChain
.push_back(it
);
3436 it
= model
->GetParent(it
);
3439 // add an 'invalid' item to represent our 'invisible' root node
3440 parentChain
.push_back(wxDataViewItem());
3442 // the parent chain was created by adding the deepest parent first.
3443 // so if we want to start at the root node, we have to iterate backwards through the vector
3444 ItemToRowJob
job( item
, parentChain
.rbegin() );
3445 Walker( m_root
, job
);
3446 return job
.GetResult();
3450 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3451 wxDataViewTreeNode
* node
)
3453 if( !model
->IsContainer( item
) )
3456 wxDataViewItemArray children
;
3457 unsigned int num
= model
->GetChildren( item
, children
);
3459 for ( unsigned int index
= 0; index
< num
; index
++ )
3461 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3463 if( model
->IsContainer(children
[index
]) )
3464 n
->SetHasChildren( true );
3466 node
->InsertChild(n
, index
);
3469 wxASSERT( node
->IsOpen() );
3470 node
->ChangeSubTreeCount(+num
);
3473 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3477 if (GetModel()->IsVirtualListModel())
3483 m_root
= wxDataViewTreeNode::CreateRootNode();
3485 // First we define a invalid item to fetch the top-level elements
3486 wxDataViewItem item
;
3488 BuildTreeHelper( model
, item
, m_root
);
3492 void wxDataViewMainWindow::DestroyTree()
3494 if (!IsVirtualList())
3502 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3504 // Edit the current column editable in 'mode'. If no column is focused
3505 // (typically because the user has full row selected), try to find the
3506 // first editable column (this would typically be a checkbox for
3507 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3508 // focus on the checkbox column; or on the only editable text column).
3510 wxDataViewColumn
*candidate
= m_currentCol
;
3513 !IsCellEditableInMode(item
, candidate
, mode
) &&
3514 !m_currentColSetByKeyboard
)
3516 // If current column was set by mouse to something not editable (in
3517 // 'mode') and the user pressed Space/F2 to edit it, treat the
3518 // situation as if there was whole-row focus, because that's what is
3519 // visually indicated and the mouse click could very well be targeted
3520 // on the row rather than on an individual cell.
3522 // But if it was done by keyboard, respect that even if the column
3523 // isn't editable, because focus is visually on that column and editing
3524 // something else would be surprising.
3530 const unsigned cols
= GetOwner()->GetColumnCount();
3531 for ( unsigned i
= 0; i
< cols
; i
++ )
3533 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3534 if ( c
->IsHidden() )
3537 if ( IsCellEditableInMode(item
, c
, mode
) )
3545 // If on container item without columns, only the expander column
3546 // may be directly editable:
3548 GetOwner()->GetExpanderColumn() != candidate
&&
3549 GetModel()->IsContainer(item
) &&
3550 !GetModel()->HasContainerColumns(item
) )
3552 candidate
= GetOwner()->GetExpanderColumn();
3558 if ( !IsCellEditableInMode(item
, candidate
, mode
) )
3564 bool wxDataViewMainWindow::IsCellEditableInMode(const wxDataViewItem
& item
,
3565 const wxDataViewColumn
*col
,
3566 wxDataViewCellMode mode
) const
3568 if ( col
->GetRenderer()->GetMode() != mode
)
3571 if ( !GetModel()->IsEnabled(item
, col
->GetModelColumn()) )
3577 void wxDataViewMainWindow::OnCharHook(wxKeyEvent
& event
)
3581 // Handle any keys special for the in-place editor and return without
3582 // calling Skip() below.
3583 switch ( event
.GetKeyCode() )
3586 m_editorRenderer
->CancelEditing();
3590 m_editorRenderer
->FinishEditing();
3598 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3600 wxWindow
* const parent
= GetParent();
3602 // propagate the char event upwards
3603 wxKeyEvent
eventForParent(event
);
3604 eventForParent
.SetEventObject(parent
);
3605 if ( parent
->ProcessWindowEvent(eventForParent
) )
3608 if ( parent
->HandleAsNavigationKey(event
) )
3611 // no item -> nothing to do
3612 if (!HasCurrentRow())
3618 // don't use m_linesPerPage directly as it might not be computed yet
3619 const int pageSize
= GetCountPerPage();
3620 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3622 switch ( event
.GetKeyCode() )
3625 if ( event
.HasModifiers() )
3632 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3633 // it. Only if that event is not handled do we activate column renderer (which
3634 // is normally done by Space) or even inline editing.
3636 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3638 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3641 le
.SetEventObject(parent
);
3642 le
.SetModel(GetModel());
3644 if ( parent
->ProcessWindowEvent(le
) )
3646 // else: fall through to WXK_SPACE handling
3650 if ( event
.HasModifiers() )
3657 // Space toggles activatable items or -- if not activatable --
3658 // starts inline editing (this is normally done using F2 on
3659 // Windows, but Space is common everywhere else, so use it too
3660 // for greater cross-platform compatibility).
3662 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3664 // Activate the current activatable column. If not column is focused (typically
3665 // because the user has full row selected), try to find the first activatable
3666 // column (this would typically be a checkbox and we don't want to force the user
3667 // to set focus on the checkbox column).
3668 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3670 if ( activatableCol
)
3672 const unsigned colIdx
= activatableCol
->GetModelColumn();
3673 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3675 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3676 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3677 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3681 // else: fall through to WXK_F2 handling
3685 if ( event
.HasModifiers() )
3692 if( !m_selection
.empty() )
3694 // Mimic Windows 7 behavior: edit the item that has focus
3695 // if it is selected and the first selected item if focus
3696 // is out of selection.
3698 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3701 sel
= m_selection
[0];
3704 const wxDataViewItem item
= GetItemByRow(sel
);
3706 // Edit the current column. If no column is focused
3707 // (typically because the user has full row selected), try
3708 // to find the first editable column.
3709 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3712 GetOwner()->EditItem(item
, editableCol
);
3718 OnVerticalNavigation( -1, event
);
3722 OnVerticalNavigation( +1, event
);
3724 // Add the process for tree expanding/collapsing
3734 OnVerticalNavigation( +(int)GetRowCount(), event
);
3738 OnVerticalNavigation( -(int)GetRowCount(), event
);
3742 OnVerticalNavigation( -(pageSize
- 1), event
);
3746 OnVerticalNavigation( +(pageSize
- 1), event
);
3754 void wxDataViewMainWindow::OnVerticalNavigation(int delta
, const wxKeyEvent
& event
)
3756 // if there is no selection, we cannot move it anywhere
3757 if (!HasCurrentRow() || IsEmpty())
3760 int newRow
= (int)m_currentRow
+ delta
;
3762 // let's keep the new row inside the allowed range
3766 const int rowCount
= (int)GetRowCount();
3767 if ( newRow
>= rowCount
)
3768 newRow
= rowCount
- 1;
3770 unsigned int oldCurrent
= m_currentRow
;
3771 unsigned int newCurrent
= (unsigned int)newRow
;
3773 // in single selection we just ignore Shift as we can't select several
3775 if ( event
.ShiftDown() && !IsSingleSel() )
3777 RefreshRow( oldCurrent
);
3779 ChangeCurrentRow( newCurrent
);
3781 // select all the items between the old and the new one
3782 if ( oldCurrent
> newCurrent
)
3784 newCurrent
= oldCurrent
;
3785 oldCurrent
= m_currentRow
;
3788 SelectRows( oldCurrent
, newCurrent
, true );
3789 if (oldCurrent
!=newCurrent
)
3790 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3794 RefreshRow( oldCurrent
);
3796 // all previously selected items are unselected unless ctrl is held
3797 if ( !event
.ControlDown() )
3798 SelectAllRows(false);
3800 ChangeCurrentRow( newCurrent
);
3802 if ( !event
.ControlDown() )
3804 SelectRow( m_currentRow
, true );
3805 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3808 RefreshRow( m_currentRow
);
3811 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3814 void wxDataViewMainWindow::OnLeftKey()
3818 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3822 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3826 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3829 // Because TryAdvanceCurrentColumn() return false, we are at the first
3830 // column or using whole-row selection. In this situation, we can use
3831 // the standard TreeView handling of the left key.
3832 if (node
->HasChildren() && node
->IsOpen())
3834 Collapse(m_currentRow
);
3838 // if the node is already closed, we move the selection to its parent
3839 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3843 int parent
= GetRowByItem( parent_node
->GetItem() );
3846 unsigned int row
= m_currentRow
;
3847 SelectRow( row
, false);
3848 SelectRow( parent
, true );
3849 ChangeCurrentRow( parent
);
3850 GetOwner()->EnsureVisible( parent
, -1 );
3851 SendSelectionChangedEvent( parent_node
->GetItem() );
3858 void wxDataViewMainWindow::OnRightKey()
3862 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3866 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3870 if ( node
->HasChildren() )
3872 if ( !node
->IsOpen() )
3874 Expand( m_currentRow
);
3878 // if the node is already open, we move the selection to the first child
3879 unsigned int row
= m_currentRow
;
3880 SelectRow( row
, false );
3881 SelectRow( row
+ 1, true );
3882 ChangeCurrentRow( row
+ 1 );
3883 GetOwner()->EnsureVisible( row
+ 1, -1 );
3884 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3889 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3894 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3896 if ( GetOwner()->GetColumnCount() == 0 )
3899 if ( !m_useCellFocus
)
3904 // navigation shouldn't work in branch nodes without other columns:
3905 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3909 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3913 m_currentCol
= GetOwner()->GetColumnAt(1);
3914 m_currentColSetByKeyboard
= true;
3915 RefreshRow(m_currentRow
);
3922 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3924 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3927 GetOwner()->EnsureVisible(m_currentRow
, idx
);
3931 // We are going to the left of the second column. Reset to whole-row
3932 // focus (which means first column would be edited).
3933 m_currentCol
= NULL
;
3934 RefreshRow(m_currentRow
);
3938 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3939 m_currentColSetByKeyboard
= true;
3940 RefreshRow(m_currentRow
);
3944 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3946 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3948 // let the base handle mouse wheel events.
3953 if(event
.ButtonDown())
3955 // Not skipping button down events would prevent the system from
3956 // setting focus to this window as most (all?) of them do by default,
3957 // so skip it to enable default handling.
3961 int x
= event
.GetX();
3962 int y
= event
.GetY();
3963 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3964 wxDataViewColumn
*col
= NULL
;
3967 unsigned int cols
= GetOwner()->GetColumnCount();
3969 for (i
= 0; i
< cols
; i
++)
3971 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3973 continue; // skip it!
3975 if (x
< xpos
+ c
->GetWidth())
3980 xpos
+= c
->GetWidth();
3983 wxDataViewModel
* const model
= GetModel();
3985 const unsigned int current
= GetLineAt( y
);
3986 const wxDataViewItem item
= GetItemByRow(current
);
3988 // Handle right clicking here, before everything else as context menu
3989 // events should be sent even when we click outside of any item, unlike all
3991 if (event
.RightUp())
3993 wxWindow
*parent
= GetParent();
3994 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3995 le
.SetEventObject(parent
);
3998 if ( item
.IsOk() && col
)
4001 le
.SetColumn( col
->GetModelColumn() );
4002 le
.SetDataViewColumn( col
);
4005 model
->GetValue( value
, item
, col
->GetModelColumn() );
4009 parent
->ProcessWindowEvent(le
);
4019 wxDataViewRenderer
*cell
= col
->GetRenderer();
4020 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
4022 // Unselect all if below the last row ?
4027 wxDataViewColumn
* const
4028 expander
= GetExpanderColumnOrFirstOne(GetOwner());
4030 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
4031 // button) and also determine the offset of the real cell start, skipping
4032 // the indentation and the expander itself.
4033 bool hoverOverExpander
= false;
4035 if ((!IsList()) && (expander
== col
))
4037 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4039 int indent
= node
->GetIndentLevel();
4040 itemOffset
= GetOwner()->GetIndent()*indent
;
4042 if ( node
->HasChildren() )
4044 // we make the rectangle we are looking in a bit bigger than the actual
4045 // visual expander so the user can hit that little thing reliably
4046 wxRect
rect(itemOffset
,
4047 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
4048 m_lineHeight
, m_lineHeight
);
4050 if( rect
.Contains(x
, y
) )
4052 // So the mouse is over the expander
4053 hoverOverExpander
= true;
4054 if (m_underMouse
&& m_underMouse
!= node
)
4056 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4057 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4059 if (m_underMouse
!= node
)
4061 // wxLogMessage("Do the row: %d", current);
4062 RefreshRow(current
);
4064 m_underMouse
= node
;
4068 // Account for the expander as well, even if this item doesn't have it,
4069 // its parent does so it still counts for the offset.
4070 itemOffset
+= m_lineHeight
;
4072 if (!hoverOverExpander
)
4074 if (m_underMouse
!= NULL
)
4076 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4077 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4078 m_underMouse
= NULL
;
4082 #if wxUSE_DRAG_AND_DROP
4083 if (event
.Dragging())
4085 if (m_dragCount
== 0)
4087 // we have to report the raw, physical coords as we want to be
4088 // able to call HitTest(event.m_pointDrag) from the user code to
4089 // get the item being dragged
4090 m_dragStart
= event
.GetPosition();
4095 if (m_dragCount
!= 3)
4098 if (event
.LeftIsDown())
4100 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
4101 &m_dragStart
.x
, &m_dragStart
.y
);
4102 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
4103 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
4105 // Notify cell about drag
4106 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
4107 event
.SetEventObject( m_owner
);
4108 event
.SetItem( itemDragged
);
4109 event
.SetModel( model
);
4110 if (!m_owner
->HandleWindowEvent( event
))
4113 if (!event
.IsAllowed())
4116 wxDataObject
*obj
= event
.GetDataObject();
4120 wxDataViewDropSource
drag( this, drag_item_row
);
4121 drag
.SetData( *obj
);
4122 /* wxDragResult res = */ drag
.DoDragDrop(event
.GetDragFlags());
4131 #endif // wxUSE_DRAG_AND_DROP
4133 bool simulateClick
= false;
4135 if (event
.ButtonDClick())
4137 m_renameTimer
->Stop();
4138 m_lastOnSame
= false;
4141 bool ignore_other_columns
=
4142 ((expander
!= col
) &&
4143 (model
->IsContainer(item
)) &&
4144 (!model
->HasContainerColumns(item
)));
4146 if (event
.LeftDClick())
4148 if(hoverOverExpander
)
4150 // a double click on the expander will be converted into a "simulated" normal click
4151 simulateClick
= true;
4153 else if ( current
== m_lineLastClicked
)
4155 wxWindow
*parent
= GetParent();
4156 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4158 le
.SetColumn( col
->GetModelColumn() );
4159 le
.SetDataViewColumn( col
);
4160 le
.SetEventObject(parent
);
4161 le
.SetModel(GetModel());
4163 parent
->ProcessWindowEvent(le
);
4168 // The first click was on another item, so don't interpret this as
4169 // a double click, but as a simple click instead
4170 simulateClick
= true;
4174 if (event
.LeftUp() && !hoverOverExpander
)
4176 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4178 // select single line
4179 SelectAllRows( false );
4180 SelectRow( m_lineSelectSingleOnUp
, true );
4181 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4184 // If the user click the expander, we do not do editing even if the column
4185 // with expander are editable
4186 if (m_lastOnSame
&& !ignore_other_columns
)
4188 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4189 IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
4191 m_renameTimer
->Start( 100, true );
4195 m_lastOnSame
= false;
4196 m_lineSelectSingleOnUp
= (unsigned int)-1;
4198 else if(!event
.LeftUp())
4200 // This is necessary, because after a DnD operation in
4201 // from and to ourself, the up event is swallowed by the
4202 // DnD code. So on next non-up event (which means here and
4203 // now) m_lineSelectSingleOnUp should be reset.
4204 m_lineSelectSingleOnUp
= (unsigned int)-1;
4207 if (event
.RightDown())
4209 m_lineBeforeLastClicked
= m_lineLastClicked
;
4210 m_lineLastClicked
= current
;
4212 // If the item is already selected, do not update the selection.
4213 // Multi-selections should not be cleared if a selected item is clicked.
4214 if (!IsRowSelected(current
))
4216 SelectAllRows(false);
4217 const unsigned oldCurrent
= m_currentRow
;
4218 ChangeCurrentRow(current
);
4219 SelectRow(m_currentRow
,true);
4220 RefreshRow(oldCurrent
);
4221 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4224 else if (event
.MiddleDown())
4228 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4230 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4232 // hoverOverExpander being true tells us that our node must be
4233 // valid and have children.
4234 // So we don't need any extra checks.
4235 if( node
->IsOpen() )
4240 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4242 m_lineBeforeLastClicked
= m_lineLastClicked
;
4243 m_lineLastClicked
= current
;
4245 unsigned int oldCurrentRow
= m_currentRow
;
4246 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4248 bool cmdModifierDown
= event
.CmdDown();
4249 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4251 if ( IsSingleSel() || !IsRowSelected(current
) )
4253 SelectAllRows( false );
4254 ChangeCurrentRow(current
);
4255 SelectRow(m_currentRow
,true);
4256 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4258 else // multi sel & current is highlighted & no mod keys
4260 m_lineSelectSingleOnUp
= current
;
4261 ChangeCurrentRow(current
); // change focus
4264 else // multi sel & either ctrl or shift is down
4266 if (cmdModifierDown
)
4268 ChangeCurrentRow(current
);
4269 ReverseRowSelection(m_currentRow
);
4270 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4272 else if (event
.ShiftDown())
4274 ChangeCurrentRow(current
);
4276 unsigned int lineFrom
= oldCurrentRow
,
4279 if ( lineTo
< lineFrom
)
4282 lineFrom
= m_currentRow
;
4285 SelectRows(lineFrom
, lineTo
, true);
4286 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4288 else // !ctrl, !shift
4290 // test in the enclosing if should make it impossible
4291 wxFAIL_MSG( wxT("how did we get here?") );
4295 if (m_currentRow
!= oldCurrentRow
)
4296 RefreshRow( oldCurrentRow
);
4298 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4300 // Update selection here...
4302 m_currentColSetByKeyboard
= false;
4304 // This flag is used to decide whether we should start editing the item
4305 // label. We do it if the user clicks twice (but not double clicks,
4306 // i.e. simulateClick is false) on the same item but not if the click
4307 // was used for something else already, e.g. selecting the item (so it
4308 // must have been already selected) or giving the focus to the control
4309 // (so it must have had focus already).
4310 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4311 (current
== oldCurrentRow
)) && oldWasSelected
&&
4314 // Call ActivateCell() after everything else as under GTK+
4315 if ( IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_ACTIVATABLE
) )
4317 // notify cell about click
4318 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4320 wxRect
cell_rect( xpos
+ itemOffset
,
4321 GetLineStart( current
),
4322 col
->GetWidth() - itemOffset
,
4323 GetLineHeight( current
) );
4325 // Report position relative to the cell's custom area, i.e.
4326 // no the entire space as given by the control but the one
4327 // used by the renderer after calculation of alignment etc.
4329 // adjust the rectangle ourselves to account for the alignment
4330 wxRect rectItem
= cell_rect
;
4331 const int align
= cell
->GetAlignment();
4332 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4334 const wxSize size
= cell
->GetSize();
4336 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4338 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4339 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4340 else if ( align
& wxALIGN_RIGHT
)
4341 rectItem
.x
+= cell_rect
.width
- size
.x
;
4342 // else: wxALIGN_LEFT is the default
4345 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4347 if ( align
& wxALIGN_CENTER_VERTICAL
)
4348 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4349 else if ( align
& wxALIGN_BOTTOM
)
4350 rectItem
.y
+= cell_rect
.height
- size
.y
;
4351 // else: wxALIGN_TOP is the default
4355 wxMouseEvent
event2(event
);
4356 event2
.m_x
-= rectItem
.x
;
4357 event2
.m_y
-= rectItem
.y
;
4358 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4360 /* ignore ret */ cell
->WXActivateCell
4365 col
->GetModelColumn(),
4372 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4376 if (HasCurrentRow())
4382 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4386 if (HasCurrentRow())
4392 void wxDataViewMainWindow::OnColumnsCountChanged()
4394 int editableCount
= 0;
4396 const unsigned cols
= GetOwner()->GetColumnCount();
4397 for ( unsigned i
= 0; i
< cols
; i
++ )
4399 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4400 if ( c
->IsHidden() )
4402 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4406 m_useCellFocus
= (editableCount
> 1);
4411 //-----------------------------------------------------------------------------
4413 //-----------------------------------------------------------------------------
4415 WX_DEFINE_LIST(wxDataViewColumnList
)
4417 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4418 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4419 EVT_SIZE(wxDataViewCtrl::OnSize
)
4422 wxDataViewCtrl::~wxDataViewCtrl()
4425 GetModel()->RemoveNotifier( m_notifier
);
4428 m_colsBestWidths
.clear();
4431 void wxDataViewCtrl::Init()
4433 m_cols
.DeleteContents(true);
4436 // No sorting column at start
4437 m_sortingColumnIdx
= wxNOT_FOUND
;
4439 m_headerArea
= NULL
;
4441 m_colsDirty
= false;
4444 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4449 const wxValidator
& validator
,
4450 const wxString
& name
)
4452 // if ( (style & wxBORDER_MASK) == 0)
4453 // style |= wxBORDER_SUNKEN;
4457 if (!wxControl::Create( parent
, id
, pos
, size
,
4458 style
| wxScrolledWindowStyle
, validator
, name
))
4461 SetInitialSize(size
);
4464 MacSetClipChildren( true );
4467 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4469 // We use the cursor keys for moving the selection, not scrolling, so call
4470 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4471 // keyboard events forwarded to us from wxListMainWindow.
4472 DisableKeyboardScrolling();
4474 if (HasFlag(wxDV_NO_HEADER
))
4475 m_headerArea
= NULL
;
4477 m_headerArea
= new wxDataViewHeaderWindow(this);
4479 SetTargetWindow( m_clientArea
);
4481 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4483 sizer
->Add( m_headerArea
, 0, wxGROW
);
4484 sizer
->Add( m_clientArea
, 1, wxGROW
);
4490 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4492 return wxBORDER_THEME
;
4496 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4500 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4503 // we need to process arrows ourselves for scrolling
4504 if ( nMsg
== WM_GETDLGCODE
)
4506 rc
|= DLGC_WANTARROWS
;
4514 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4516 wxSize newsize
= size
;
4517 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4518 newsize
.y
-= m_headerArea
->GetSize().y
;
4523 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4525 // We need to override OnSize so that our scrolled
4526 // window a) does call Layout() to use sizers for
4527 // positioning the controls but b) does not query
4528 // the sizer for their size and use that for setting
4529 // the scrollable area as set that ourselves by
4530 // calling SetScrollbar() further down.
4536 // We must redraw the headers if their height changed. Normally this
4537 // shouldn't happen as the control shouldn't let itself be resized beneath
4538 // its minimal height but avoid the display artefacts that appear if it
4539 // does happen, e.g. because there is really not enough vertical space.
4540 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4541 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4543 m_headerArea
->Refresh();
4547 void wxDataViewCtrl::SetFocus()
4550 m_clientArea
->SetFocus();
4553 bool wxDataViewCtrl::SetFont(const wxFont
& font
)
4555 if (!wxControl::SetFont(font
))
4559 m_headerArea
->SetFont(font
);
4563 m_clientArea
->SetFont(font
);
4564 m_clientArea
->SetRowHeight(m_clientArea
->GetDefaultRowHeight());
4567 if (m_headerArea
|| m_clientArea
)
4569 InvalidateColBestWidths();
4578 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4580 if (!wxDataViewCtrlBase::AssociateModel( model
))
4585 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4586 model
->AddNotifier( m_notifier
);
4588 else if (m_notifier
)
4590 m_notifier
->Cleared();
4594 m_clientArea
->DestroyTree();
4598 m_clientArea
->BuildTree(model
);
4601 m_clientArea
->UpdateDisplay();
4606 #if wxUSE_DRAG_AND_DROP
4608 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4610 return m_clientArea
->EnableDragSource( format
);
4613 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4615 return m_clientArea
->EnableDropTarget( format
);
4618 #endif // wxUSE_DRAG_AND_DROP
4620 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4622 if (!wxDataViewCtrlBase::AppendColumn(col
))
4625 m_cols
.Append( col
);
4626 m_colsBestWidths
.push_back(CachedColWidthInfo());
4627 OnColumnsCountChanged();
4631 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4633 if (!wxDataViewCtrlBase::PrependColumn(col
))
4636 m_cols
.Insert( col
);
4637 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), CachedColWidthInfo());
4638 OnColumnsCountChanged();
4642 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4644 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4647 m_cols
.Insert( pos
, col
);
4648 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, CachedColWidthInfo());
4649 OnColumnsCountChanged();
4653 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4656 m_headerArea
->UpdateColumn(idx
);
4658 m_clientArea
->UpdateDisplay();
4661 void wxDataViewCtrl::OnColumnsCountChanged()
4664 m_headerArea
->SetColumnCount(GetColumnCount());
4666 m_clientArea
->OnColumnsCountChanged();
4669 void wxDataViewCtrl::DoSetExpanderColumn()
4671 m_clientArea
->UpdateDisplay();
4674 void wxDataViewCtrl::DoSetIndent()
4676 m_clientArea
->UpdateDisplay();
4679 unsigned int wxDataViewCtrl::GetColumnCount() const
4681 return m_cols
.GetCount();
4684 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4686 if ( !m_clientArea
)
4689 m_clientArea
->SetRowHeight(lineHeight
);
4694 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4699 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4701 // columns can't be reordered if there is no header window which allows
4703 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4706 return GetColumn(idx
);
4709 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4711 const unsigned count
= m_cols
.size();
4712 for ( unsigned n
= 0; n
< count
; n
++ )
4714 if ( m_cols
[n
] == column
)
4721 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4723 if ( m_colsBestWidths
[idx
].width
!= 0 )
4724 return m_colsBestWidths
[idx
].width
;
4726 const int count
= m_clientArea
->GetRowCount();
4727 wxDataViewColumn
*column
= GetColumn(idx
);
4728 wxDataViewRenderer
*renderer
=
4729 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4731 class MaxWidthCalculator
4734 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4735 wxDataViewMainWindow
*clientArea
,
4736 wxDataViewRenderer
*renderer
,
4737 const wxDataViewModel
*model
,
4742 m_clientArea(clientArea
),
4743 m_renderer(renderer
),
4746 m_expanderSize(expanderSize
)
4750 !clientArea
->IsList() &&
4752 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4755 void UpdateWithWidth(int width
)
4757 m_width
= wxMax(m_width
, width
);
4760 void UpdateWithRow(int row
)
4763 wxDataViewItem item
;
4765 if ( m_isExpanderCol
)
4767 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4768 item
= node
->GetItem();
4769 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4773 item
= m_clientArea
->GetItemByRow(row
);
4776 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4777 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4780 int GetMaxWidth() const { return m_width
; }
4784 const wxDataViewCtrl
*m_dvc
;
4785 wxDataViewMainWindow
*m_clientArea
;
4786 wxDataViewRenderer
*m_renderer
;
4787 const wxDataViewModel
*m_model
;
4789 bool m_isExpanderCol
;
4793 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4794 GetModel(), column
->GetModelColumn(),
4795 m_clientArea
->GetRowHeight());
4797 calculator
.UpdateWithWidth(column
->GetMinWidth());
4800 calculator
.UpdateWithWidth(m_headerArea
->GetColumnTitleWidth(*column
));
4802 // The code below deserves some explanation. For very large controls, we
4803 // simply can't afford to calculate sizes for all items, it takes too
4804 // long. So the best we can do is to check the first and the last N/2
4805 // items in the control for some sufficiently large N and calculate best
4806 // sizes from that. That can result in the calculated best width being too
4807 // small for some outliers, but it's better to get slightly imperfect
4808 // result than to wait several seconds after every update. To avoid highly
4809 // visible miscalculations, we also include all currently visible items
4810 // no matter what. Finally, the value of N is determined dynamically by
4811 // measuring how much time we spent on the determining item widths so far.
4814 int top_part_end
= count
;
4815 static const long CALC_TIMEOUT
= 20/*ms*/;
4816 // don't call wxStopWatch::Time() too often
4817 static const unsigned CALC_CHECK_FREQ
= 100;
4820 // use some hard-coded limit, that's the best we can do without timer
4821 int top_part_end
= wxMin(500, count
);
4822 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4826 for ( row
= 0; row
< top_part_end
; row
++ )
4829 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4830 timer
.Time() > CALC_TIMEOUT
)
4832 #endif // wxUSE_STOPWATCH
4833 calculator
.UpdateWithRow(row
);
4836 // row is the first unmeasured item now; that's our value of N/2
4842 // add bottom N/2 items now:
4843 const int bottom_part_start
= wxMax(row
, count
- row
);
4844 for ( row
= bottom_part_start
; row
< count
; row
++ )
4846 calculator
.UpdateWithRow(row
);
4849 // finally, include currently visible items in the calculation:
4850 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4851 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4852 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4854 first_visible
= wxMax(first_visible
, top_part_end
);
4855 last_visible
= wxMin(bottom_part_start
, last_visible
);
4857 for ( row
= first_visible
; row
< last_visible
; row
++ )
4859 calculator
.UpdateWithRow(row
);
4862 wxLogTrace("dataview",
4863 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4865 count
- bottom_part_start
,
4866 wxMax(0, last_visible
- first_visible
),
4870 int max_width
= calculator
.GetMaxWidth();
4871 if ( max_width
> 0 )
4872 max_width
+= 2 * PADDING_RIGHTLEFT
;
4874 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
].width
= max_width
;
4878 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4879 unsigned int WXUNUSED(new_pos
))
4881 // do _not_ reorder m_cols elements here, they should always be in the
4882 // order in which columns were added, we only display the columns in
4884 m_clientArea
->UpdateDisplay();
4887 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4889 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4893 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4896 if ( m_clientArea
->GetCurrentColumn() == column
)
4897 m_clientArea
->ClearCurrentColumn();
4899 OnColumnsCountChanged();
4904 bool wxDataViewCtrl::ClearColumns()
4906 SetExpanderColumn(NULL
);
4908 m_colsBestWidths
.clear();
4910 m_clientArea
->ClearCurrentColumn();
4912 OnColumnsCountChanged();
4917 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4919 m_colsBestWidths
[idx
].width
= 0;
4920 m_colsBestWidths
[idx
].dirty
= true;
4924 void wxDataViewCtrl::InvalidateColBestWidths()
4926 // mark all columns as dirty:
4927 m_colsBestWidths
.clear();
4928 m_colsBestWidths
.resize(m_cols
.size());
4932 void wxDataViewCtrl::UpdateColWidths()
4934 m_colsDirty
= false;
4936 if ( !m_headerArea
)
4939 const unsigned len
= m_colsBestWidths
.size();
4940 for ( unsigned i
= 0; i
< len
; i
++ )
4942 // Note that we have to have an explicit 'dirty' flag here instead of
4943 // checking if the width==0, as is done in GetBestColumnWidth().
4945 // Testing width==0 wouldn't work correctly if some code called
4946 // GetWidth() after col. width invalidation but before
4947 // wxDataViewCtrl::UpdateColWidths() was called at idle time. This
4948 // would result in the header's column width getting out of sync with
4949 // the control itself.
4950 if ( m_colsBestWidths
[i
].dirty
)
4952 m_headerArea
->UpdateColumn(i
);
4953 m_colsBestWidths
[i
].dirty
= false;
4958 void wxDataViewCtrl::OnInternalIdle()
4960 wxDataViewCtrlBase::OnInternalIdle();
4966 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4968 unsigned int len
= GetColumnCount();
4969 for ( unsigned int i
= 0; i
< len
; i
++ )
4971 wxDataViewColumn
* col
= GetColumnAt(i
);
4979 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4981 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4982 : GetColumn(m_sortingColumnIdx
);
4985 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4987 return GetItemByRow(m_clientArea
->GetCurrentRow());
4990 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4992 const int row
= m_clientArea
->GetRowByItem(item
);
4994 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4995 if ( static_cast<unsigned>(row
) != oldCurrent
)
4997 m_clientArea
->ChangeCurrentRow(row
);
4998 m_clientArea
->RefreshRow(oldCurrent
);
4999 m_clientArea
->RefreshRow(row
);
5003 wxDataViewColumn
*wxDataViewCtrl::GetCurrentColumn() const
5005 return m_clientArea
->GetCurrentColumn();
5008 int wxDataViewCtrl::GetSelectedItemsCount() const
5010 return m_clientArea
->GetSelections().size();
5013 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
5016 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
5018 const size_t len
= selections
.size();
5019 for ( size_t i
= 0; i
< len
; i
++ )
5021 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
5028 wxFAIL_MSG( "invalid item in selection - bad internal state" );
5035 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
5037 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
5039 wxDataViewItem last_parent
;
5041 int len
= sel
.GetCount();
5042 for( int i
= 0; i
< len
; i
++ )
5044 wxDataViewItem item
= sel
[i
];
5045 wxDataViewItem parent
= GetModel()->GetParent( item
);
5048 if (parent
!= last_parent
)
5049 ExpandAncestors(item
);
5052 last_parent
= parent
;
5053 int row
= m_clientArea
->GetRowByItem( item
);
5055 selection
.Add( static_cast<unsigned int>(row
) );
5058 m_clientArea
->SetSelections( selection
);
5061 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
5063 ExpandAncestors( item
);
5065 int row
= m_clientArea
->GetRowByItem( item
);
5068 // Unselect all rows before select another in the single select mode
5069 if (m_clientArea
->IsSingleSel())
5070 m_clientArea
->SelectAllRows(false);
5072 m_clientArea
->SelectRow(row
, true);
5074 // Also set focus to the selected item
5075 m_clientArea
->ChangeCurrentRow( row
);
5079 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
5081 int row
= m_clientArea
->GetRowByItem( item
);
5083 m_clientArea
->SelectRow(row
, false);
5086 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
5088 int row
= m_clientArea
->GetRowByItem( item
);
5091 return m_clientArea
->IsRowSelected(row
);
5096 void wxDataViewCtrl::SetAlternateRowColour(const wxColour
& colour
)
5098 m_alternateRowColour
= colour
;
5101 void wxDataViewCtrl::SelectAll()
5103 m_clientArea
->SelectAllRows(true);
5106 void wxDataViewCtrl::UnselectAll()
5108 m_clientArea
->SelectAllRows(false);
5111 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
5115 if( row
> (int) m_clientArea
->GetRowCount() )
5116 row
= m_clientArea
->GetRowCount();
5118 int first
= m_clientArea
->GetFirstVisibleRow();
5119 int last
= m_clientArea
->GetLastVisibleRow();
5121 m_clientArea
->ScrollTo( row
, column
);
5122 else if( row
> last
)
5123 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
5125 m_clientArea
->ScrollTo( first
, column
);
5128 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
5130 ExpandAncestors( item
);
5132 m_clientArea
->RecalculateDisplay();
5134 int row
= m_clientArea
->GetRowByItem(item
);
5137 if( column
== NULL
)
5138 EnsureVisible(row
, -1);
5140 EnsureVisible( row
, GetColumnIndex(column
) );
5145 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
5146 wxDataViewColumn
* &column
) const
5148 m_clientArea
->HitTest(point
, item
, column
);
5151 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
5152 const wxDataViewColumn
* column
) const
5154 return m_clientArea
->GetItemRect(item
, column
);
5157 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
5159 return m_clientArea
->GetItemByRow( row
);
5162 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
5164 return m_clientArea
->GetRowByItem( item
);
5167 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
5169 ExpandAncestors( item
);
5171 int row
= m_clientArea
->GetRowByItem( item
);
5174 m_clientArea
->Expand(row
);
5175 InvalidateColBestWidths();
5179 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
5181 int row
= m_clientArea
->GetRowByItem( item
);
5184 m_clientArea
->Collapse(row
);
5185 InvalidateColBestWidths();
5189 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
5191 int row
= m_clientArea
->GetRowByItem( item
);
5193 return m_clientArea
->IsExpanded(row
);
5197 void wxDataViewCtrl::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
5199 wxCHECK_RET( item
.IsOk(), "invalid item" );
5200 wxCHECK_RET( column
, "no column provided" );
5202 m_clientArea
->StartEditing(item
, column
);
5205 #endif // !wxUSE_GENERICDATAVIEWCTRL
5207 #endif // wxUSE_DATAVIEWCTRL