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
;
84 // The column is either the index of the column to be used for sorting or one
85 // of the special values in this enum:
91 // Sort using the model default sort order.
92 SortColumn_Default
= -1
95 static int g_column
= SortColumn_None
;
96 static bool g_asending
= true;
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
105 // Return the expander column or, if it is not set, the first column and also
106 // set it as the expander one for the future.
107 wxDataViewColumn
* GetExpanderColumnOrFirstOne(wxDataViewCtrl
* dataview
)
109 wxDataViewColumn
* expander
= dataview
->GetExpanderColumn();
112 // TODO-RTL: last column for RTL support
113 expander
= dataview
->GetColumnAt( 0 );
114 dataview
->SetExpanderColumn(expander
);
120 } // anonymous namespace
122 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
126 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
133 m_sortAscending
= true;
136 int wxDataViewColumn::GetWidth() const
140 case wxCOL_WIDTH_DEFAULT
:
141 return wxDVC_DEFAULT_WIDTH
;
143 case wxCOL_WIDTH_AUTOSIZE
:
144 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
145 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
152 void wxDataViewColumn::UpdateDisplay()
156 int idx
= m_owner
->GetColumnIndex( this );
157 m_owner
->OnColumnChange( idx
);
161 void wxDataViewColumn::SetSortOrder(bool ascending
)
166 // First unset the old sort column if any.
167 int oldSortKey
= m_owner
->GetSortingColumnIndex();
168 if ( oldSortKey
!= wxNOT_FOUND
)
170 m_owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
173 // Now set this one as the new sort column.
174 const int idx
= m_owner
->GetColumnIndex(this);
175 m_owner
->SetSortingColumnIndex(idx
);
178 m_sortAscending
= ascending
;
180 // Call this directly instead of using UpdateDisplay() as we already have
181 // the column index, no need to look it up again.
182 m_owner
->OnColumnChange(idx
);
185 //-----------------------------------------------------------------------------
186 // wxDataViewHeaderWindow
187 //-----------------------------------------------------------------------------
189 class wxDataViewHeaderWindow
: public wxHeaderCtrl
192 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
193 : wxHeaderCtrl(parent
)
197 wxDataViewCtrl
*GetOwner() const
198 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
201 // implement/override wxHeaderCtrl functions by forwarding them to the main
203 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
205 return *(GetOwner()->GetColumn(idx
));
208 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
210 wxDataViewCtrl
* const owner
= GetOwner();
212 int widthContents
= owner
->GetBestColumnWidth(idx
);
213 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
214 owner
->OnColumnChange(idx
);
220 bool SendEvent(wxEventType type
, unsigned int n
)
222 wxDataViewCtrl
* const owner
= GetOwner();
223 wxDataViewEvent
event(type
, owner
->GetId());
225 event
.SetEventObject(owner
);
227 event
.SetDataViewColumn(owner
->GetColumn(n
));
228 event
.SetModel(owner
->GetModel());
230 // for events created by wxDataViewHeaderWindow the
231 // row / value fields are not valid
232 return owner
->ProcessWindowEvent(event
);
235 void OnClick(wxHeaderCtrlEvent
& event
)
237 const unsigned idx
= event
.GetColumn();
239 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
242 // default handling for the column click is to sort by this column or
243 // toggle its sort order
244 wxDataViewCtrl
* const owner
= GetOwner();
245 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
246 if ( !col
->IsSortable() )
248 // no default handling for non-sortable columns
253 if ( col
->IsSortKey() )
255 // already using this column for sorting, just change the order
256 col
->ToggleSortOrder();
258 else // not using this column for sorting yet
260 col
->SetSortOrder(true);
263 wxDataViewModel
* const model
= owner
->GetModel();
267 owner
->OnColumnChange(idx
);
269 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
272 void OnRClick(wxHeaderCtrlEvent
& event
)
274 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
279 void OnResize(wxHeaderCtrlEvent
& event
)
281 wxDataViewCtrl
* const owner
= GetOwner();
283 const unsigned col
= event
.GetColumn();
284 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
285 GetOwner()->OnColumnChange(col
);
288 void OnEndReorder(wxHeaderCtrlEvent
& event
)
290 wxDataViewCtrl
* const owner
= GetOwner();
291 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
292 event
.GetNewOrder());
295 DECLARE_EVENT_TABLE()
296 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
299 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
300 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
301 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
303 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
304 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
306 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
309 //-----------------------------------------------------------------------------
310 // wxDataViewRenameTimer
311 //-----------------------------------------------------------------------------
313 class wxDataViewRenameTimer
: public wxTimer
316 wxDataViewMainWindow
*m_owner
;
319 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
323 //-----------------------------------------------------------------------------
324 // wxDataViewTreeNode
325 //-----------------------------------------------------------------------------
327 class wxDataViewTreeNode
;
328 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
330 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
331 wxDataViewTreeNode
** node2
);
333 class wxDataViewTreeNode
336 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
343 ~wxDataViewTreeNode()
347 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
348 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
359 static wxDataViewTreeNode
* CreateRootNode()
361 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
362 n
->m_branchData
= new BranchNodeData
;
363 n
->m_branchData
->open
= true;
367 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
369 const wxDataViewTreeNodes
& GetChildNodes() const
371 wxASSERT( m_branchData
!= NULL
);
372 return m_branchData
->children
;
375 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
378 m_branchData
= new BranchNodeData
;
380 m_branchData
->children
.Insert(node
, index
);
382 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
384 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
387 void RemoveChild(wxDataViewTreeNode
*node
)
389 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
390 m_branchData
->children
.Remove(node
);
393 // returns position of child node for given item in children list or wxNOT_FOUND
394 int FindChildByItem(const wxDataViewItem
& item
) const
399 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
400 const int len
= nodes
.size();
401 for ( int i
= 0; i
< len
; i
++ )
403 if ( nodes
[i
]->m_item
== item
)
409 const wxDataViewItem
& GetItem() const { return m_item
; }
410 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
412 int GetIndentLevel() const
415 const wxDataViewTreeNode
* node
= this;
416 while( node
->GetParent()->GetParent() != NULL
)
418 node
= node
->GetParent();
426 return m_branchData
&& m_branchData
->open
;
431 // We do not allow the (invisible) root node to be collapsed because
432 // there is no way to expand it again.
436 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
440 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
441 const int len
= nodes
.GetCount();
442 for ( int i
= 0;i
< len
; i
++)
443 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
445 if (m_branchData
->open
)
447 ChangeSubTreeCount(-sum
);
448 m_branchData
->open
= !m_branchData
->open
;
452 m_branchData
->open
= !m_branchData
->open
;
453 ChangeSubTreeCount(+sum
);
457 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
458 // even if GetChildNodes() is empty; see below.
459 bool HasChildren() const
461 return m_branchData
!= NULL
;
464 void SetHasChildren(bool has
)
466 // The invisible root item always has children, so ignore any attempts
473 wxDELETE(m_branchData
);
475 else if ( m_branchData
== NULL
)
477 m_branchData
= new BranchNodeData
;
481 int GetSubTreeCount() const
483 return m_branchData
? m_branchData
->subTreeCount
: 0;
486 void ChangeSubTreeCount( int num
)
488 wxASSERT( m_branchData
!= NULL
);
490 if( !m_branchData
->open
)
493 m_branchData
->subTreeCount
+= num
;
494 wxASSERT( m_branchData
->subTreeCount
>= 0 );
497 m_parent
->ChangeSubTreeCount(num
);
507 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
509 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
510 int len
= nodes
.GetCount();
511 for (int i
= 0; i
< len
; i
++)
513 if ( nodes
[i
]->HasChildren() )
521 wxDataViewTreeNode
*m_parent
;
523 // Corresponding model item.
524 wxDataViewItem m_item
;
526 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
527 // separate struct in order to conserve memory.
528 struct BranchNodeData
536 // Child nodes. Note that this may be empty even if m_hasChildren in
537 // case this branch of the tree wasn't expanded and realized yet.
538 wxDataViewTreeNodes children
;
540 // Is the branch node currently open (expanded)?
543 // Total count of expanded (i.e. visible with the help of some
544 // scrolling) items in the subtree, but excluding this node. I.e. it is
545 // 0 for leaves and is the number of rows the subtree occupies for
550 BranchNodeData
*m_branchData
;
554 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
555 wxDataViewTreeNode
** node2
)
557 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
561 //-----------------------------------------------------------------------------
562 // wxDataViewMainWindow
563 //-----------------------------------------------------------------------------
565 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
567 class wxDataViewMainWindow
: public wxWindow
570 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
572 const wxPoint
&pos
= wxDefaultPosition
,
573 const wxSize
&size
= wxDefaultSize
,
574 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
575 virtual ~wxDataViewMainWindow();
577 bool IsList() const { return GetModel()->IsListModel(); }
578 bool IsVirtualList() const { return m_root
== NULL
; }
580 // notifications from wxDataViewModel
581 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
582 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
583 bool ItemChanged( const wxDataViewItem
&item
);
584 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
588 if (!IsVirtualList())
598 g_model
= GetModel();
599 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
602 if (g_model
->HasDefaultCompare())
603 g_column
= SortColumn_Default
;
605 g_column
= SortColumn_None
;
610 g_column
= col
->GetModelColumn();
611 g_asending
= col
->IsSortOrderAscending();
614 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
615 wxDataViewCtrl
*GetOwner() { return m_owner
; }
616 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
618 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
619 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
621 #if wxUSE_DRAG_AND_DROP
622 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
623 #endif // wxUSE_DRAG_AND_DROP
624 void OnPaint( wxPaintEvent
&event
);
625 void OnCharHook( wxKeyEvent
&event
);
626 void OnChar( wxKeyEvent
&event
);
627 void OnVerticalNavigation(int delta
, const wxKeyEvent
& event
);
630 void OnMouse( wxMouseEvent
&event
);
631 void OnSetFocus( wxFocusEvent
&event
);
632 void OnKillFocus( wxFocusEvent
&event
);
634 void UpdateDisplay();
635 void RecalculateDisplay();
636 void OnInternalIdle();
638 void OnRenameTimer();
640 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
641 void ScrollTo( int rows
, int column
);
643 unsigned GetCurrentRow() const { return m_currentRow
; }
644 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
645 void ChangeCurrentRow( unsigned int row
);
646 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
648 wxDataViewColumn
*GetCurrentColumn() const { return m_currentCol
; }
649 void ClearCurrentColumn() { m_currentCol
= NULL
; }
651 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
652 bool IsEmpty() { return GetRowCount() == 0; }
654 int GetCountPerPage() const;
655 int GetEndOfLastCol() const;
656 unsigned int GetFirstVisibleRow() const;
658 // I change this method to un const because in the tree view,
659 // the displaying number of the tree are changing along with the
660 // expanding/collapsing of the tree nodes
661 unsigned int GetLastVisibleRow();
662 unsigned int GetRowCount() const;
664 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
665 void SetSelections( const wxDataViewSelection
& sel
)
666 { m_selection
= sel
; UpdateDisplay(); }
667 void Select( const wxArrayInt
& aSelections
);
668 void SelectAllRows( bool on
);
669 void SelectRow( unsigned int row
, bool on
);
670 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
671 void ReverseRowSelection( unsigned int row
);
672 bool IsRowSelected( unsigned int row
);
673 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
675 void RefreshRow( unsigned int row
);
676 void RefreshRows( unsigned int from
, unsigned int to
);
677 void RefreshRowsAfter( unsigned int firstRow
);
679 // returns the colour to be used for drawing the rules
680 wxColour
GetRuleColour() const
682 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
685 wxRect
GetLineRect( unsigned int row
) const;
687 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
688 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
689 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
691 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
692 int GetRowHeight() const { return m_lineHeight
; }
693 int GetDefaultRowHeight() const;
695 // Some useful functions for row and item mapping
696 wxDataViewItem
GetItemByRow( unsigned int row
) const;
697 int GetRowByItem( const wxDataViewItem
& item
) const;
699 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
700 // We did not need this temporarily
701 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
703 // Methods for building the mapping tree
704 void BuildTree( wxDataViewModel
* model
);
706 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
707 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
709 void Expand( unsigned int row
);
710 void Collapse( unsigned int row
);
711 bool IsExpanded( unsigned int row
) const;
712 bool HasChildren( unsigned int row
) const;
714 #if wxUSE_DRAG_AND_DROP
715 bool EnableDragSource( const wxDataFormat
&format
);
716 bool EnableDropTarget( const wxDataFormat
&format
);
718 void RemoveDropHint();
719 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
720 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
721 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
723 #endif // wxUSE_DRAG_AND_DROP
725 void OnColumnsCountChanged();
727 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
728 // specified item in the given column.
729 void StartEditing(const wxDataViewItem
& item
, const wxDataViewColumn
* col
);
732 int RecalculateCount() const;
734 // Return false only if the event was vetoed by its handler.
735 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
737 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
739 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
741 bool IsCellEditableInMode(const wxDataViewItem
& item
, const wxDataViewColumn
*col
, wxDataViewCellMode mode
) const;
743 void DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
);
746 wxDataViewCtrl
*m_owner
;
750 wxDataViewColumn
*m_currentCol
;
751 unsigned int m_currentRow
;
752 wxDataViewSelection m_selection
;
754 wxDataViewRenameTimer
*m_renameTimer
;
759 bool m_currentColSetByKeyboard
;
761 #if wxUSE_DRAG_AND_DROP
766 wxDataFormat m_dragFormat
;
769 wxDataFormat m_dropFormat
;
771 unsigned int m_dropHintLine
;
772 #endif // wxUSE_DRAG_AND_DROP
774 // for double click logic
775 unsigned int m_lineLastClicked
,
776 m_lineBeforeLastClicked
,
777 m_lineSelectSingleOnUp
;
779 // the pen used to draw horiz/vertical rules
782 // the pen used to draw the expander and the lines
785 // This is the tree structure of the model
786 wxDataViewTreeNode
* m_root
;
789 // This is the tree node under the cursor
790 wxDataViewTreeNode
* m_underMouse
;
792 // The control used for editing or NULL.
793 wxWeakRef
<wxWindow
> m_editorCtrl
;
795 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
796 wxDataViewRenderer
* m_editorRenderer
;
799 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
800 DECLARE_EVENT_TABLE()
803 // ---------------------------------------------------------
804 // wxGenericDataViewModelNotifier
805 // ---------------------------------------------------------
807 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
810 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
811 { m_mainWindow
= mainWindow
; }
813 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
814 { return m_mainWindow
->ItemAdded( parent
, item
); }
815 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
816 { return m_mainWindow
->ItemDeleted( parent
, item
); }
817 virtual bool ItemChanged( const wxDataViewItem
& item
)
818 { return m_mainWindow
->ItemChanged(item
); }
819 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
820 { return m_mainWindow
->ValueChanged( item
, col
); }
821 virtual bool Cleared()
822 { return m_mainWindow
->Cleared(); }
823 virtual void Resort()
824 { m_mainWindow
->Resort(); }
826 wxDataViewMainWindow
*m_mainWindow
;
829 // ---------------------------------------------------------
830 // wxDataViewRenderer
831 // ---------------------------------------------------------
833 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
835 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
836 wxDataViewCellMode mode
,
838 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
842 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
846 wxDataViewRenderer::~wxDataViewRenderer()
851 wxDC
*wxDataViewRenderer::GetDC()
855 if (GetOwner() == NULL
)
857 if (GetOwner()->GetOwner() == NULL
)
859 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
865 void wxDataViewRenderer::SetAlignment( int align
)
870 int wxDataViewRenderer::GetAlignment() const
875 // ---------------------------------------------------------
876 // wxDataViewCustomRenderer
877 // ---------------------------------------------------------
879 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
881 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
882 wxDataViewCellMode mode
, int align
) :
883 wxDataViewRenderer( varianttype
, mode
, align
)
887 // ---------------------------------------------------------
888 // wxDataViewTextRenderer
889 // ---------------------------------------------------------
891 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
893 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
894 wxDataViewCellMode mode
, int align
) :
895 wxDataViewRenderer( varianttype
, mode
, align
)
899 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
901 m_text
= value
.GetString();
906 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
911 bool wxDataViewTextRenderer::HasEditorCtrl() const
916 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
917 wxRect labelRect
, const wxVariant
&value
)
919 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
920 wxPoint(labelRect
.x
,labelRect
.y
),
921 wxSize(labelRect
.width
,labelRect
.height
),
922 wxTE_PROCESS_ENTER
);
924 // select the text in the control an place the cursor at the end
925 ctrl
->SetInsertionPointEnd();
931 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
933 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
934 value
= text
->GetValue();
938 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
940 RenderText(m_text
, 0, rect
, dc
, state
);
944 wxSize
wxDataViewTextRenderer::GetSize() const
947 return GetTextExtent(m_text
);
949 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
952 // ---------------------------------------------------------
953 // wxDataViewBitmapRenderer
954 // ---------------------------------------------------------
956 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
958 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
959 wxDataViewCellMode mode
, int align
) :
960 wxDataViewRenderer( varianttype
, mode
, align
)
964 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
966 if (value
.GetType() == wxT("wxBitmap"))
968 if (value
.GetType() == wxT("wxIcon"))
974 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
979 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
982 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
983 else if (m_icon
.IsOk())
984 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
989 wxSize
wxDataViewBitmapRenderer::GetSize() const
992 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
993 else if (m_icon
.IsOk())
994 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
996 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
999 // ---------------------------------------------------------
1000 // wxDataViewToggleRenderer
1001 // ---------------------------------------------------------
1003 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
1005 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
1006 wxDataViewCellMode mode
, int align
) :
1007 wxDataViewRenderer( varianttype
, mode
, align
)
1012 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
1014 m_toggle
= value
.GetBool();
1019 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
1024 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
1028 flags
|= wxCONTROL_CHECKED
;
1029 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
1030 GetEnabled() == false)
1031 flags
|= wxCONTROL_DISABLED
;
1033 // Ensure that the check boxes always have at least the minimal required
1034 // size, otherwise DrawCheckBox() doesn't really work well. If this size is
1035 // greater than the cell size, the checkbox will be truncated but this is a
1037 wxSize size
= cell
.GetSize();
1038 size
.IncTo(GetSize());
1041 wxRendererNative::Get().DrawCheckBox(
1042 GetOwner()->GetOwner(),
1050 bool wxDataViewToggleRenderer::WXActivateCell(const wxRect
& WXUNUSED(cell
),
1051 wxDataViewModel
*model
,
1052 const wxDataViewItem
& item
,
1054 const wxMouseEvent
*mouseEvent
)
1058 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
1059 if ( !wxRect(GetSize()).Contains(mouseEvent
->GetPosition()) )
1063 model
->ChangeValue(!m_toggle
, item
, col
);
1067 wxSize
wxDataViewToggleRenderer::GetSize() const
1069 // the window parameter is not used by GetCheckBoxSize() so it's
1070 // safe to pass NULL
1071 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1074 // ---------------------------------------------------------
1075 // wxDataViewProgressRenderer
1076 // ---------------------------------------------------------
1078 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1080 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1081 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1082 wxDataViewRenderer( varianttype
, mode
, align
)
1088 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1090 m_value
= (long) value
;
1092 if (m_value
< 0) m_value
= 0;
1093 if (m_value
> 100) m_value
= 100;
1098 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1100 value
= (long) m_value
;
1105 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1107 // deflate the rect to leave a small border between bars in adjacent rows
1108 wxRect bar
= rect
.Deflate(0, 1);
1110 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1111 dc
->SetPen( *wxBLACK_PEN
);
1112 dc
->DrawRectangle( bar
);
1114 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1115 dc
->SetPen( *wxTRANSPARENT_PEN
);
1117 const wxDataViewItemAttr
& attr
= GetAttr();
1118 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1120 dc
->DrawRectangle( bar
);
1125 wxSize
wxDataViewProgressRenderer::GetSize() const
1127 return wxSize(40,12);
1130 // ---------------------------------------------------------
1131 // wxDataViewIconTextRenderer
1132 // ---------------------------------------------------------
1134 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1136 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1137 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1138 wxDataViewRenderer( varianttype
, mode
, align
)
1141 SetAlignment(align
);
1144 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1150 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1155 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1159 const wxIcon
& icon
= m_value
.GetIcon();
1162 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1163 xoffset
= icon
.GetWidth()+4;
1166 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1171 wxSize
wxDataViewIconTextRenderer::GetSize() const
1173 if (!m_value
.GetText().empty())
1175 wxSize size
= GetTextExtent(m_value
.GetText());
1177 if (m_value
.GetIcon().IsOk())
1178 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1181 return wxSize(80,20);
1184 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1186 wxDataViewIconText iconText
;
1189 wxString text
= iconText
.GetText();
1191 // adjust the label rect to take the width of the icon into account
1192 if (iconText
.GetIcon().IsOk())
1194 int w
= iconText
.GetIcon().GetWidth() + 4;
1196 labelRect
.width
-= w
;
1199 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1200 wxPoint(labelRect
.x
,labelRect
.y
),
1201 wxSize(labelRect
.width
,labelRect
.height
),
1202 wxTE_PROCESS_ENTER
);
1204 // select the text in the control an place the cursor at the end
1205 ctrl
->SetInsertionPointEnd();
1211 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1213 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1215 // The icon can't be edited so get its old value and reuse it.
1217 wxDataViewColumn
* const col
= GetOwner();
1218 GetView()->GetModel()->GetValue(valueOld
, m_item
, col
->GetModelColumn());
1220 wxDataViewIconText iconText
;
1221 iconText
<< valueOld
;
1223 // But replace the text with the value entered by user.
1224 iconText
.SetText(text
->GetValue());
1230 //-----------------------------------------------------------------------------
1231 // wxDataViewDropTarget
1232 //-----------------------------------------------------------------------------
1234 #if wxUSE_DRAG_AND_DROP
1236 class wxBitmapCanvas
: public wxWindow
1239 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1240 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1243 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1246 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1249 dc
.DrawBitmap( m_bitmap
, 0, 0);
1255 class wxDataViewDropSource
: public wxDropSource
1258 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1266 ~wxDataViewDropSource()
1271 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1273 wxPoint pos
= wxGetMousePosition();
1277 int liney
= m_win
->GetLineStart( m_row
);
1279 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1280 m_win
->ClientToScreen( &linex
, &liney
);
1281 m_dist_x
= pos
.x
- linex
;
1282 m_dist_y
= pos
.y
- liney
;
1285 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1287 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1288 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1290 wxFRAME_TOOL_WINDOW
|
1291 wxFRAME_FLOAT_ON_PARENT
|
1292 wxFRAME_NO_TASKBAR
|
1294 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1299 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1300 m_hint
->SetTransparent( 128 );
1306 wxDataViewMainWindow
*m_win
;
1309 int m_dist_x
,m_dist_y
;
1313 class wxDataViewDropTarget
: public wxDropTarget
1316 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1322 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1324 wxDataFormat format
= GetMatchingPair();
1325 if (format
== wxDF_INVALID
)
1327 return m_win
->OnDragOver( format
, x
, y
, def
);
1330 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1332 wxDataFormat format
= GetMatchingPair();
1333 if (format
== wxDF_INVALID
)
1335 return m_win
->OnDrop( format
, x
, y
);
1338 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1340 wxDataFormat format
= GetMatchingPair();
1341 if (format
== wxDF_INVALID
)
1345 return m_win
->OnData( format
, x
, y
, def
);
1348 virtual void OnLeave()
1349 { m_win
->OnLeave(); }
1351 wxDataViewMainWindow
*m_win
;
1354 #endif // wxUSE_DRAG_AND_DROP
1356 //-----------------------------------------------------------------------------
1357 // wxDataViewRenameTimer
1358 //-----------------------------------------------------------------------------
1360 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1365 void wxDataViewRenameTimer::Notify()
1367 m_owner
->OnRenameTimer();
1370 //-----------------------------------------------------------------------------
1371 // wxDataViewMainWindow
1372 //-----------------------------------------------------------------------------
1374 // The tree building helper, declared firstly
1375 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1376 wxDataViewTreeNode
* node
);
1378 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1380 if (row1
> row2
) return 1;
1381 if (row1
== row2
) return 0;
1385 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1387 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1388 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1389 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1390 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1391 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1392 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook
)
1393 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1396 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1397 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1398 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1399 m_selection( wxDataViewSelectionCmp
)
1404 m_editorRenderer
= NULL
;
1406 m_lastOnSame
= false;
1407 m_renameTimer
= new wxDataViewRenameTimer( this );
1409 // TODO: user better initial values/nothing selected
1410 m_currentCol
= NULL
;
1411 m_currentColSetByKeyboard
= false;
1412 m_useCellFocus
= false;
1413 m_currentRow
= (unsigned)-1;
1414 m_lineHeight
= GetDefaultRowHeight();
1416 #if wxUSE_DRAG_AND_DROP
1418 m_dragStart
= wxPoint(0,0);
1420 m_dragEnabled
= false;
1421 m_dropEnabled
= false;
1423 m_dropHintLine
= (unsigned int) -1;
1424 #endif // wxUSE_DRAG_AND_DROP
1426 m_lineLastClicked
= (unsigned int) -1;
1427 m_lineBeforeLastClicked
= (unsigned int) -1;
1428 m_lineSelectSingleOnUp
= (unsigned int) -1;
1432 SetBackgroundColour( *wxWHITE
);
1434 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1436 m_penRule
= wxPen(GetRuleColour());
1438 // compose a pen whichcan draw black lines
1439 // TODO: maybe there is something system colour to use
1440 m_penExpander
= wxPen(wxColour(0,0,0));
1442 m_root
= wxDataViewTreeNode::CreateRootNode();
1444 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1446 m_underMouse
= NULL
;
1450 wxDataViewMainWindow::~wxDataViewMainWindow()
1453 delete m_renameTimer
;
1457 int wxDataViewMainWindow::GetDefaultRowHeight() const
1460 // We would like to use the same line height that Explorer uses. This is
1461 // different from standard ListView control since Vista.
1462 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
1463 return wxMax(16, GetCharHeight()) + 6; // 16 = mini icon height
1466 return wxMax(16, GetCharHeight()) + 1; // 16 = mini icon height
1471 #if wxUSE_DRAG_AND_DROP
1472 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1474 m_dragFormat
= format
;
1475 m_dragEnabled
= format
!= wxDF_INVALID
;
1480 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1482 m_dropFormat
= format
;
1483 m_dropEnabled
= format
!= wxDF_INVALID
;
1486 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1491 void wxDataViewMainWindow::RemoveDropHint()
1496 RefreshRow( m_dropHintLine
);
1497 m_dropHintLine
= (unsigned int) -1;
1501 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1502 wxCoord y
, wxDragResult def
)
1506 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1507 unsigned int row
= GetLineAt( yy
);
1509 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1515 wxDataViewItem item
= GetItemByRow( row
);
1517 wxDataViewModel
*model
= GetModel();
1519 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1520 event
.SetEventObject( m_owner
);
1521 event
.SetItem( item
);
1522 event
.SetModel( model
);
1523 event
.SetDataFormat( format
);
1524 event
.SetDropEffect( def
);
1525 if (!m_owner
->HandleWindowEvent( event
))
1531 if (!event
.IsAllowed())
1538 if (m_dropHint
&& (row
!= m_dropHintLine
))
1539 RefreshRow( m_dropHintLine
);
1541 m_dropHintLine
= row
;
1547 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1553 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1554 unsigned int row
= GetLineAt( yy
);
1556 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1559 wxDataViewItem item
= GetItemByRow( row
);
1561 wxDataViewModel
*model
= GetModel();
1563 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1564 event
.SetEventObject( m_owner
);
1565 event
.SetItem( item
);
1566 event
.SetModel( model
);
1567 event
.SetDataFormat( format
);
1568 if (!m_owner
->HandleWindowEvent( event
))
1571 if (!event
.IsAllowed())
1577 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1582 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1583 unsigned int row
= GetLineAt( yy
);
1585 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1588 wxDataViewItem item
= GetItemByRow( row
);
1590 wxDataViewModel
*model
= GetModel();
1592 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1594 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1595 event
.SetEventObject( m_owner
);
1596 event
.SetItem( item
);
1597 event
.SetModel( model
);
1598 event
.SetDataFormat( format
);
1599 event
.SetDataSize( obj
->GetSize() );
1600 event
.SetDataBuffer( obj
->GetData() );
1601 event
.SetDropEffect( def
);
1602 if (!m_owner
->HandleWindowEvent( event
))
1605 if (!event
.IsAllowed())
1611 void wxDataViewMainWindow::OnLeave()
1616 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1618 int height
= GetLineHeight( row
);
1620 unsigned int cols
= GetOwner()->GetColumnCount();
1622 for (col
= 0; col
< cols
; col
++)
1624 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1625 if (column
->IsHidden())
1626 continue; // skip it!
1627 width
+= column
->GetWidth();
1633 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1634 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1635 indent
= indent
+ m_lineHeight
;
1636 // try to use the m_lineHeight as the expander space
1640 wxBitmap
bitmap( width
, height
);
1641 wxMemoryDC
dc( bitmap
);
1642 dc
.SetFont( GetFont() );
1643 dc
.SetPen( *wxBLACK_PEN
);
1644 dc
.SetBrush( *wxWHITE_BRUSH
);
1645 dc
.DrawRectangle( 0,0,width
,height
);
1647 wxDataViewModel
*model
= m_owner
->GetModel();
1649 wxDataViewColumn
* const
1650 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1653 for (col
= 0; col
< cols
; col
++)
1655 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1656 wxDataViewRenderer
*cell
= column
->GetRenderer();
1658 if (column
->IsHidden())
1659 continue; // skip it!
1661 width
= column
->GetWidth();
1663 if (column
== expander
)
1666 wxDataViewItem item
= GetItemByRow( row
);
1667 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1669 wxRect
item_rect(x
, 0, width
, height
);
1670 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1672 // dc.SetClippingRegion( item_rect );
1673 cell
->WXCallRender(item_rect
, &dc
, 0);
1674 // dc.DestroyClippingRegion();
1682 #endif // wxUSE_DRAG_AND_DROP
1685 // Draw focus rect for individual cell. Unlike native focus rect, we render
1686 // this in foreground text color (typically white) to enhance contrast and
1688 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1690 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1692 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1693 // may be short traits and not really dots
1695 // note that to behave in the same manner as DrawRect(), we must exclude
1696 // the bottom and right borders from the rectangle
1697 wxCoord x1
= rect
.GetLeft(),
1699 x2
= rect
.GetRight(),
1700 y2
= rect
.GetBottom();
1702 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1705 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1706 dc
.DrawPoint(z
, rect
.GetTop());
1708 wxCoord shift
= z
== x2
? 0 : 1;
1709 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1710 dc
.DrawPoint(x2
, z
);
1712 shift
= z
== y2
? 0 : 1;
1713 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1714 dc
.DrawPoint(z
, y2
);
1716 shift
= z
== x1
? 0 : 1;
1717 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1718 dc
.DrawPoint(x1
, z
);
1722 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1724 wxDataViewModel
*model
= GetModel();
1725 wxAutoBufferedPaintDC
dc( this );
1727 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1728 dc
.SetPen( *wxTRANSPARENT_PEN
);
1729 dc
.DrawRectangle(GetClientSize());
1733 // No items to draw.
1738 GetOwner()->PrepareDC( dc
);
1739 dc
.SetFont( GetFont() );
1741 wxRect update
= GetUpdateRegion().GetBox();
1742 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1744 // compute which items needs to be redrawn
1745 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1746 unsigned int item_count
=
1747 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1748 (int)(GetRowCount( ) - item_start
));
1749 unsigned int item_last
= item_start
+ item_count
;
1751 // Send the event to wxDataViewCtrl itself.
1752 wxWindow
* const parent
= GetParent();
1753 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1754 cache_event
.SetEventObject(parent
);
1755 cache_event
.SetCache(item_start
, item_last
- 1);
1756 parent
->ProcessWindowEvent(cache_event
);
1758 // compute which columns needs to be redrawn
1759 unsigned int cols
= GetOwner()->GetColumnCount();
1762 // we assume that we have at least one column below and painting an
1763 // empty control is unnecessary anyhow
1767 unsigned int col_start
= 0;
1768 unsigned int x_start
;
1769 for (x_start
= 0; col_start
< cols
; col_start
++)
1771 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1772 if (col
->IsHidden())
1773 continue; // skip it!
1775 unsigned int w
= col
->GetWidth();
1776 if (x_start
+w
>= (unsigned int)update
.x
)
1782 unsigned int col_last
= col_start
;
1783 unsigned int x_last
= x_start
;
1784 for (; col_last
< cols
; col_last
++)
1786 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1787 if (col
->IsHidden())
1788 continue; // skip it!
1790 if (x_last
> (unsigned int)update
.GetRight())
1793 x_last
+= col
->GetWidth();
1796 // Draw background of alternate rows specially if required
1797 if ( m_owner
->HasFlag(wxDV_ROW_LINES
) )
1799 wxColour altRowColour
= m_owner
->m_alternateRowColour
;
1800 if ( !altRowColour
.IsOk() )
1802 // Determine the alternate rows colour automatically from the
1803 // background colour.
1804 const wxColour bgColour
= m_owner
->GetBackgroundColour();
1806 // Depending on the background, alternate row color
1807 // will be 3% more dark or 50% brighter.
1808 int alpha
= bgColour
.GetRGB() > 0x808080 ? 97 : 150;
1809 altRowColour
= bgColour
.ChangeLightness(alpha
);
1812 dc
.SetPen(*wxTRANSPARENT_PEN
);
1813 dc
.SetBrush(wxBrush(altRowColour
));
1815 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1819 dc
.DrawRectangle(x_start
,
1821 GetClientSize().GetWidth(),
1822 GetLineHeight(item
));
1827 // Draw horizontal rules if required
1828 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1830 dc
.SetPen(m_penRule
);
1831 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1833 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1835 int y
= GetLineStart( i
);
1836 dc
.DrawLine(x_start
, y
, x_last
, y
);
1840 // Draw vertical rules if required
1841 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1843 dc
.SetPen(m_penRule
);
1844 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1846 // NB: Vertical rules are drawn in the last pixel of a column so that
1847 // they align perfectly with native MSW wxHeaderCtrl as well as for
1848 // consistency with MSW native list control. There's no vertical
1849 // rule at the most-left side of the control.
1851 int x
= x_start
- 1;
1852 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1854 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1855 if (col
->IsHidden())
1856 continue; // skip it
1858 x
+= col
->GetWidth();
1860 dc
.DrawLine(x
, GetLineStart( item_start
),
1861 x
, GetLineStart( item_last
) );
1865 // redraw the background for the items which are selected/current
1866 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1868 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1870 if (selected
|| item
== m_currentRow
)
1872 wxRect
rect( x_start
, GetLineStart( item
),
1873 x_last
- x_start
, GetLineHeight( item
) );
1875 // draw selection and whole-item focus:
1878 int flags
= wxCONTROL_SELECTED
;
1880 flags
|= wxCONTROL_FOCUSED
;
1882 wxRendererNative::Get().DrawItemSelectionRect
1891 // draw keyboard focus rect if applicable
1892 if ( item
== m_currentRow
&& m_hasFocus
)
1894 bool renderColumnFocus
= false;
1896 if ( m_useCellFocus
&& m_currentCol
&& m_currentColSetByKeyboard
)
1898 renderColumnFocus
= true;
1900 // If this is container node without columns, render full-row focus:
1903 wxDataViewTreeNode
*node
= GetTreeNodeByRow(item
);
1904 if ( node
->HasChildren() && !model
->HasContainerColumns(node
->GetItem()) )
1905 renderColumnFocus
= false;
1909 if ( renderColumnFocus
)
1911 for ( unsigned int i
= col_start
; i
< col_last
; i
++ )
1913 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1914 if ( col
->IsHidden() )
1917 rect
.width
= col
->GetWidth();
1919 if ( col
== m_currentCol
)
1921 // make the rect more visible by adding a small
1922 // margin around it:
1927 // DrawFocusRect() uses XOR and is all but
1928 // invisible against dark-blue background. Use
1929 // the same color used for selected text.
1930 DrawSelectedCellFocusRect(dc
, rect
);
1934 wxRendererNative::Get().DrawFocusRect
1945 rect
.x
+= rect
.width
;
1950 // render focus rectangle for the whole row
1951 wxRendererNative::Get().DrawFocusRect
1956 selected
? (int)wxCONTROL_SELECTED
: 0
1963 #if wxUSE_DRAG_AND_DROP
1966 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1967 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1968 dc
.SetPen( *wxBLACK_PEN
);
1969 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1970 dc
.DrawRectangle( rect
);
1972 #endif // wxUSE_DRAG_AND_DROP
1974 wxDataViewColumn
* const
1975 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1977 // redraw all cells for all rows which must be repainted and all columns
1979 cell_rect
.x
= x_start
;
1980 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1982 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1983 wxDataViewRenderer
*cell
= col
->GetRenderer();
1984 cell_rect
.width
= col
->GetWidth();
1986 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1987 continue; // skip it!
1989 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1991 // get the cell value and set it into the renderer
1992 wxDataViewTreeNode
*node
= NULL
;
1993 wxDataViewItem dataitem
;
1995 if (!IsVirtualList())
1997 node
= GetTreeNodeByRow(item
);
2001 dataitem
= node
->GetItem();
2003 // Skip all columns of "container" rows except the expander
2004 // column itself unless HasContainerColumns() overrides this.
2005 if ( col
!= expander
&&
2006 model
->IsContainer(dataitem
) &&
2007 !model
->HasContainerColumns(dataitem
) )
2012 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
2015 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
2018 cell_rect
.y
= GetLineStart( item
);
2019 cell_rect
.height
= GetLineHeight( item
);
2021 // draw the background
2022 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2024 DrawCellBackground( cell
, dc
, cell_rect
);
2026 // deal with the expander
2028 if ((!IsList()) && (col
== expander
))
2030 // Calculate the indent first
2031 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
2033 // we reserve m_lineHeight of horizontal space for the expander
2034 // but leave EXPANDER_MARGIN around the expander itself
2035 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
2037 indent
+= m_lineHeight
;
2039 // draw expander if needed and visible
2040 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
2042 dc
.SetPen( m_penExpander
);
2043 dc
.SetBrush( wxNullBrush
);
2045 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2046 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
2047 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
2049 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
2052 if ( m_underMouse
== node
)
2053 flag
|= wxCONTROL_CURRENT
;
2054 if ( node
->IsOpen() )
2055 flag
|= wxCONTROL_EXPANDED
;
2057 // ensure that we don't overflow the cell (which might
2058 // happen if the column is very narrow)
2059 wxDCClipper
clip(dc
, cell_rect
);
2061 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2064 // force the expander column to left-center align
2065 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2068 wxRect item_rect
= cell_rect
;
2069 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
2071 // account for the tree indent (harmless if we're not indented)
2072 item_rect
.x
+= indent
;
2073 item_rect
.width
-= indent
;
2075 if ( item_rect
.width
<= 0 )
2079 if (m_hasFocus
&& selected
)
2080 state
|= wxDATAVIEW_CELL_SELECTED
;
2082 // TODO: it would be much more efficient to create a clipping
2083 // region for the entire column being rendered (in the OnPaint
2084 // of wxDataViewMainWindow) instead of a single clip region for
2085 // each cell. However it would mean that each renderer should
2086 // respect the given wxRect's top & bottom coords, eventually
2087 // violating only the left & right coords - however the user can
2088 // make its own renderer and thus we cannot be sure of that.
2089 wxDCClipper
clip(dc
, item_rect
);
2091 cell
->WXCallRender(item_rect
, &dc
, state
);
2094 cell_rect
.x
+= cell_rect
.width
;
2099 void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer
* cell
, wxDC
& dc
, const wxRect
& rect
)
2101 wxRect
rectBg( rect
);
2103 // don't overlap the horizontal rules
2104 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2110 // don't overlap the vertical rules
2111 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2117 cell
->RenderBackground(&dc
, rectBg
);
2120 void wxDataViewMainWindow::OnRenameTimer()
2122 // We have to call this here because changes may just have
2123 // been made and no screen update taken place.
2126 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2127 // (needs to be tested!)
2131 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2133 StartEditing( item
, m_currentCol
);
2137 wxDataViewMainWindow::StartEditing(const wxDataViewItem
& item
,
2138 const wxDataViewColumn
* col
)
2140 wxDataViewRenderer
* renderer
= col
->GetRenderer();
2141 if ( !IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
2144 const wxRect itemRect
= GetItemRect(item
, col
);
2145 if ( renderer
->StartEditing(item
, itemRect
) )
2147 // Save the renderer to be able to finish/cancel editing it later and
2148 // save the control to be able to detect if we're still editing it.
2149 m_editorRenderer
= renderer
;
2150 m_editorCtrl
= renderer
->GetEditorCtrl();
2154 //-----------------------------------------------------------------------------
2155 // Helper class for do operation on the tree node
2156 //-----------------------------------------------------------------------------
2161 virtual ~DoJob() { }
2163 // The return value control how the tree-walker tranverse the tree
2166 DONE
, // Job done, stop traversing and return
2167 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2168 CONTINUE
// Job not done, continue
2171 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2174 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2176 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2178 switch( func( node
) )
2182 case DoJob::SKIP_SUBTREE
:
2184 case DoJob::CONTINUE
:
2188 if ( node
->HasChildren() )
2190 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2192 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2196 if ( Walker(*i
, func
) )
2204 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2206 if (IsVirtualList())
2208 wxDataViewVirtualListModel
*list_model
=
2209 (wxDataViewVirtualListModel
*) GetModel();
2210 m_count
= list_model
->GetCount();
2216 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2221 wxDataViewItemArray modelSiblings
;
2222 GetModel()->GetChildren(parent
, modelSiblings
);
2223 const int modelSiblingsSize
= modelSiblings
.size();
2225 int posInModel
= modelSiblings
.Index(item
, /*fromEnd=*/true);
2226 wxCHECK_MSG( posInModel
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2228 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2229 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2231 parentNode
->SetHasChildren(true);
2233 const wxDataViewTreeNodes
& nodeSiblings
= parentNode
->GetChildNodes();
2234 const int nodeSiblingsSize
= nodeSiblings
.size();
2238 if ( posInModel
== modelSiblingsSize
- 1 )
2240 nodePos
= nodeSiblingsSize
;
2242 else if ( modelSiblingsSize
== nodeSiblingsSize
+ 1 )
2244 // This is the simple case when our node tree already matches the
2245 // model and only this one item is missing.
2246 nodePos
= posInModel
;
2250 // It's possible that a larger discrepancy between the model and
2251 // our realization exists. This can happen e.g. when adding a bunch
2252 // of items to the model and then calling ItemsAdded() just once
2253 // afterwards. In this case, we must find the right position by
2254 // looking at sibling items.
2256 // append to the end if we won't find a better position:
2257 nodePos
= nodeSiblingsSize
;
2259 for ( int nextItemPos
= posInModel
+ 1;
2260 nextItemPos
< modelSiblingsSize
;
2263 int nextNodePos
= parentNode
->FindChildByItem(modelSiblings
[nextItemPos
]);
2264 if ( nextNodePos
!= wxNOT_FOUND
)
2266 nodePos
= nextNodePos
;
2272 parentNode
->ChangeSubTreeCount(+1);
2273 parentNode
->InsertChild(itemNode
, nodePos
);
2278 GetOwner()->InvalidateColBestWidths();
2284 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2285 const wxDataViewItem
& item
)
2287 if (IsVirtualList())
2289 wxDataViewVirtualListModel
*list_model
=
2290 (wxDataViewVirtualListModel
*) GetModel();
2291 m_count
= list_model
->GetCount();
2293 if ( !m_selection
.empty() )
2295 const int row
= GetRowByItem(item
);
2297 int rowIndexInSelection
= wxNOT_FOUND
;
2299 const size_t selCount
= m_selection
.size();
2300 for ( size_t i
= 0; i
< selCount
; i
++ )
2302 if ( m_selection
[i
] == (unsigned)row
)
2303 rowIndexInSelection
= i
;
2304 else if ( m_selection
[i
] > (unsigned)row
)
2308 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2309 m_selection
.RemoveAt(rowIndexInSelection
);
2313 else // general case
2315 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2317 // Notice that it is possible that the item being deleted is not in the
2318 // tree at all, for example we could be deleting a never shown (because
2319 // collapsed) item in a tree model. So it's not an error if we don't know
2320 // about this item, just return without doing anything then.
2324 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2325 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2327 // We can't use FindNode() to find 'item', because it was already
2328 // removed from the model by the time ItemDeleted() is called, so we
2329 // have to do it manually. We keep track of its position as well for
2331 int itemPosInNode
= 0;
2332 wxDataViewTreeNode
*itemNode
= NULL
;
2333 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2334 i
!= parentsChildren
.end();
2335 ++i
, ++itemPosInNode
)
2337 if( (*i
)->GetItem() == item
)
2344 // If the parent wasn't expanded, it's possible that we didn't have a
2345 // node corresponding to 'item' and so there's nothing left to do.
2348 // If this was the last child to be removed, it's possible the parent
2349 // node became a leaf. Let's ask the model about it.
2350 if ( parentNode
->GetChildNodes().empty() )
2351 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2356 // Delete the item from wxDataViewTreeNode representation:
2357 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2359 parentNode
->RemoveChild(itemNode
);
2361 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2363 // Make the row number invalid and get a new valid one when user call GetRowCount
2366 // If this was the last child to be removed, it's possible the parent
2367 // node became a leaf. Let's ask the model about it.
2368 if ( parentNode
->GetChildNodes().empty() )
2370 bool isContainer
= GetModel()->IsContainer(parent
);
2371 parentNode
->SetHasChildren(isContainer
);
2374 // If it's still a container, make sure we show "+" icon for it
2375 // and not "-" one as there is nothing to collapse any more.
2376 if ( parentNode
->IsOpen() )
2377 parentNode
->ToggleOpen();
2381 // Update selection by removing 'item' and its entire children tree from the selection.
2382 if ( !m_selection
.empty() )
2384 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2385 // the parent ('parentNode') and position in its list of children
2387 if ( itemPosInNode
== 0 )
2389 // 1st child, row number is that of the parent parentNode + 1
2390 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2394 // row number is that of the sibling above 'item' + its subtree if any + 1
2395 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2397 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2398 siblingNode
->GetSubTreeCount() +
2402 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2404 const size_t numSelections
= m_selection
.size();
2405 for ( size_t i
= 0; i
< numSelections
; ++i
)
2407 const int s
= m_selection
[i
];
2409 newsel
.push_back(s
);
2410 else if ( s
>= itemRow
+ itemsDeleted
)
2411 newsel
.push_back(s
- itemsDeleted
);
2412 // else: deleted item, remove from selection
2415 m_selection
= newsel
;
2419 // Change the current row to the last row if the current exceed the max row number
2420 if ( m_currentRow
>= GetRowCount() )
2421 ChangeCurrentRow(m_count
- 1);
2423 GetOwner()->InvalidateColBestWidths();
2429 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2434 GetOwner()->InvalidateColBestWidths();
2437 wxWindow
*parent
= GetParent();
2438 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2439 le
.SetEventObject(parent
);
2440 le
.SetModel(GetModel());
2442 parent
->ProcessWindowEvent(le
);
2447 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2449 int view_column
= -1;
2450 unsigned int n_col
= m_owner
->GetColumnCount();
2451 for (unsigned i
= 0; i
< n_col
; i
++)
2453 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2454 if (column
->GetModelColumn() == model_column
)
2456 view_column
= (int) i
;
2460 if (view_column
== -1)
2463 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2464 /*#define MAX_VIRTUAL_WIDTH 100000
2466 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2467 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2468 Refresh( true, &rect );
2475 GetOwner()->InvalidateColBestWidth(view_column
);
2478 wxWindow
*parent
= GetParent();
2479 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2480 le
.SetEventObject(parent
);
2481 le
.SetModel(GetModel());
2483 le
.SetColumn(view_column
);
2484 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2485 parent
->ProcessWindowEvent(le
);
2490 bool wxDataViewMainWindow::Cleared()
2493 m_selection
.Clear();
2494 m_currentRow
= (unsigned)-1;
2499 BuildTree( GetModel() );
2506 GetOwner()->InvalidateColBestWidths();
2512 void wxDataViewMainWindow::UpdateDisplay()
2515 m_underMouse
= NULL
;
2518 void wxDataViewMainWindow::OnInternalIdle()
2520 wxWindow::OnInternalIdle();
2524 RecalculateDisplay();
2529 void wxDataViewMainWindow::RecalculateDisplay()
2531 wxDataViewModel
*model
= GetModel();
2538 int width
= GetEndOfLastCol();
2539 int height
= GetLineStart( GetRowCount() );
2541 SetVirtualSize( width
, height
);
2542 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2547 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2549 m_underMouse
= NULL
;
2551 wxWindow::ScrollWindow( dx
, dy
, rect
);
2553 if (GetOwner()->m_headerArea
)
2554 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2557 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2559 m_underMouse
= NULL
;
2562 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2563 int sy
= GetLineStart( rows
)/y
;
2567 wxRect rect
= GetClientRect();
2571 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2572 for (x_start
= 0; colnum
< column
; colnum
++)
2574 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2575 if (col
->IsHidden())
2576 continue; // skip it!
2578 w
= col
->GetWidth();
2582 int x_end
= x_start
+ w
;
2583 xe
= xx
+ rect
.width
;
2586 sx
= ( xx
+ x_end
- xe
)/x
;
2593 m_owner
->Scroll( sx
, sy
);
2596 int wxDataViewMainWindow::GetCountPerPage() const
2598 wxSize size
= GetClientSize();
2599 return size
.y
/ m_lineHeight
;
2602 int wxDataViewMainWindow::GetEndOfLastCol() const
2606 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2608 const wxDataViewColumn
*c
=
2609 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2612 width
+= c
->GetWidth();
2617 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2621 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2623 return GetLineAt( y
);
2626 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2628 wxSize client_size
= GetClientSize();
2629 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2630 &client_size
.x
, &client_size
.y
);
2632 // we should deal with the pixel here
2633 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2635 return wxMin( GetRowCount()-1, row
);
2638 unsigned int wxDataViewMainWindow::GetRowCount() const
2640 if ( m_count
== -1 )
2642 wxDataViewMainWindow
* const
2643 self
= const_cast<wxDataViewMainWindow
*>(this);
2644 self
->m_count
= RecalculateCount();
2645 self
->UpdateDisplay();
2650 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2657 void wxDataViewMainWindow::SelectAllRows( bool on
)
2664 m_selection
.Clear();
2665 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2666 m_selection
.Add( i
);
2671 unsigned int first_visible
= GetFirstVisibleRow();
2672 unsigned int last_visible
= GetLastVisibleRow();
2674 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2676 unsigned int row
= m_selection
[i
];
2677 if ((row
>= first_visible
) && (row
<= last_visible
))
2680 m_selection
.Clear();
2684 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2686 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2690 m_selection
.Add( row
);
2698 m_selection
.Remove( row
);
2704 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2708 unsigned int tmp
= from
;
2714 for (i
= from
; i
<= to
; i
++)
2716 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2719 m_selection
.Add( i
);
2724 m_selection
.Remove( i
);
2727 RefreshRows( from
, to
);
2730 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2732 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2734 int n
= aSelections
[i
];
2736 m_selection
.Add( n
);
2741 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2743 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2744 m_selection
.Add( row
);
2746 m_selection
.Remove( row
);
2750 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2752 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2755 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2757 wxWindow
*parent
= GetParent();
2758 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2760 le
.SetEventObject(parent
);
2761 le
.SetModel(GetModel());
2764 parent
->ProcessWindowEvent(le
);
2767 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2769 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2770 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2772 wxSize client_size
= GetClientSize();
2773 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2774 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2775 if (intersect_rect
.width
> 0)
2776 Refresh( true, &intersect_rect
);
2779 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2783 unsigned int tmp
= to
;
2788 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2789 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2791 wxSize client_size
= GetClientSize();
2792 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2793 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2794 if (intersect_rect
.width
> 0)
2795 Refresh( true, &intersect_rect
);
2798 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2800 wxSize client_size
= GetClientSize();
2801 int start
= GetLineStart( firstRow
);
2802 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2803 if (start
> client_size
.y
) return;
2805 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2807 Refresh( true, &rect
);
2810 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2814 rect
.y
= GetLineStart( row
);
2815 rect
.width
= GetEndOfLastCol();
2816 rect
.height
= GetLineHeight( row
);
2821 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2823 const wxDataViewModel
*model
= GetModel();
2825 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2827 // TODO make more efficient
2832 for (r
= 0; r
< row
; r
++)
2834 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2835 if (!node
) return start
;
2837 wxDataViewItem item
= node
->GetItem();
2839 unsigned int cols
= GetOwner()->GetColumnCount();
2841 int height
= m_lineHeight
;
2842 for (col
= 0; col
< cols
; col
++)
2844 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2845 if (column
->IsHidden())
2846 continue; // skip it!
2849 model
->IsContainer(item
) &&
2850 !model
->HasContainerColumns(item
))
2851 continue; // skip it!
2853 wxDataViewRenderer
*renderer
=
2854 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2855 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2857 height
= wxMax( height
, renderer
->GetSize().y
);
2867 return row
* m_lineHeight
;
2871 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2873 const wxDataViewModel
*model
= GetModel();
2875 // check for the easy case first
2876 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2877 return y
/ m_lineHeight
;
2879 // TODO make more efficient
2880 unsigned int row
= 0;
2881 unsigned int yy
= 0;
2884 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2887 // not really correct...
2888 return row
+ ((y
-yy
) / m_lineHeight
);
2891 wxDataViewItem item
= node
->GetItem();
2893 unsigned int cols
= GetOwner()->GetColumnCount();
2895 int height
= m_lineHeight
;
2896 for (col
= 0; col
< cols
; col
++)
2898 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2899 if (column
->IsHidden())
2900 continue; // skip it!
2903 model
->IsContainer(item
) &&
2904 !model
->HasContainerColumns(item
))
2905 continue; // skip it!
2907 wxDataViewRenderer
*renderer
=
2908 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2909 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2911 height
= wxMax( height
, renderer
->GetSize().y
);
2922 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2924 const wxDataViewModel
*model
= GetModel();
2926 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2928 wxASSERT( !IsVirtualList() );
2930 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2931 // wxASSERT( node );
2932 if (!node
) return m_lineHeight
;
2934 wxDataViewItem item
= node
->GetItem();
2936 int height
= m_lineHeight
;
2938 unsigned int cols
= GetOwner()->GetColumnCount();
2940 for (col
= 0; col
< cols
; col
++)
2942 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2943 if (column
->IsHidden())
2944 continue; // skip it!
2947 model
->IsContainer(item
) &&
2948 !model
->HasContainerColumns(item
))
2949 continue; // skip it!
2951 wxDataViewRenderer
*renderer
=
2952 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2953 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2955 height
= wxMax( height
, renderer
->GetSize().y
);
2962 return m_lineHeight
;
2967 class RowToTreeNodeJob
: public DoJob
2970 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2973 this->current
= current
;
2978 virtual int operator() ( wxDataViewTreeNode
* node
)
2981 if( current
== static_cast<int>(row
))
2987 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2989 current
+= node
->GetSubTreeCount();
2990 return DoJob::SKIP_SUBTREE
;
2996 // If the current node has only leaf children, we can find the
2997 // desired node directly. This can speed up finding the node
2998 // in some cases, and will have a very good effect for list views.
2999 if ( node
->HasChildren() &&
3000 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
3002 const int index
= static_cast<int>(row
) - current
- 1;
3003 ret
= node
->GetChildNodes()[index
];
3007 return DoJob::CONTINUE
;
3011 wxDataViewTreeNode
* GetResult() const
3017 wxDataViewTreeNode
* ret
;
3018 wxDataViewTreeNode
* parent
;
3021 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
3023 wxASSERT( !IsVirtualList() );
3025 if ( row
== (unsigned)-1 )
3028 RowToTreeNodeJob
job( row
, -2, m_root
);
3029 Walker( m_root
, job
);
3030 return job
.GetResult();
3033 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
3035 wxDataViewItem item
;
3036 if (IsVirtualList())
3038 if ( row
< GetRowCount() )
3039 item
= wxDataViewItem(wxUIntToPtr(row
+1));
3043 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3045 item
= node
->GetItem();
3052 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
3053 const wxDataViewItem
& item
)
3055 wxWindow
*parent
= GetParent();
3056 wxDataViewEvent
le(type
, parent
->GetId());
3058 le
.SetEventObject(parent
);
3059 le
.SetModel(GetModel());
3062 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
3065 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
3070 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3074 if (!node
->HasChildren())
3077 return node
->IsOpen();
3080 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
3085 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3089 if (!node
->HasChildren())
3095 void wxDataViewMainWindow::Expand( unsigned int row
)
3100 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3104 if (!node
->HasChildren())
3107 if (!node
->IsOpen())
3109 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
3111 // Vetoed by the event handler.
3117 // build the children of current node
3118 if( node
->GetChildNodes().empty() )
3121 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
3124 // By expanding the node all row indices that are currently in the selection list
3125 // and are greater than our node have become invalid. So we have to correct that now.
3126 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3127 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3129 const unsigned testRow
= m_selection
[i
];
3130 // all rows above us are not affected, so skip them
3134 m_selection
[i
] += rowAdjustment
;
3137 if(m_currentRow
> row
)
3138 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3142 // Send the expanded event
3143 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3147 void wxDataViewMainWindow::Collapse(unsigned int row
)
3152 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3156 if (!node
->HasChildren())
3161 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3163 // Vetoed by the event handler.
3167 // Find out if there are selected items below the current node.
3168 bool selectCollapsingRow
= false;
3169 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3170 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3171 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3173 const unsigned testRow
= m_selection
[i
];
3174 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3176 selectCollapsingRow
= true;
3177 // get out as soon as we have found a node that is selected
3184 // If the node to be closed has selected items the user won't see those any longer.
3185 // We select the collapsing node in this case.
3186 if(selectCollapsingRow
)
3188 SelectAllRows(false);
3189 ChangeCurrentRow(row
);
3190 SelectRow(row
, true);
3191 SendSelectionChangedEvent(GetItemByRow(row
));
3195 // if there were no selected items below our node we still need to "fix" the
3196 // selection list to adjust for the changing of the row indices.
3197 // We actually do the opposite of what we are doing in Expand().
3198 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3200 const unsigned testRow
= m_selection
[i
];
3201 // all rows above us are not affected, so skip them
3205 m_selection
[i
] -= rowAdjustment
;
3208 // if the "current row" is being collapsed away we change it to the current row ;-)
3209 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3210 ChangeCurrentRow(row
);
3211 else if(m_currentRow
> row
)
3212 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3217 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3221 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3223 const wxDataViewModel
* model
= GetModel();
3230 // Compose the parent-chain for the item we are looking for
3231 wxVector
<wxDataViewItem
> parentChain
;
3232 wxDataViewItem
it( item
);
3235 parentChain
.push_back(it
);
3236 it
= model
->GetParent(it
);
3239 // Find the item along the parent-chain.
3240 // This algorithm is designed to speed up the node-finding method
3241 wxDataViewTreeNode
* node
= m_root
;
3242 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3244 if( node
->HasChildren() )
3246 if( node
->GetChildNodes().empty() )
3248 // Even though the item is a container, it doesn't have any
3249 // child nodes in the control's representation yet. We have
3250 // to realize its subtree now.
3252 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3255 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3258 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3260 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3261 if (currentNode
->GetItem() == parentChain
[iter
])
3263 if (currentNode
->GetItem() == item
)
3283 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3284 wxDataViewColumn
* &column
)
3286 wxDataViewColumn
*col
= NULL
;
3287 unsigned int cols
= GetOwner()->GetColumnCount();
3288 unsigned int colnum
= 0;
3290 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3291 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3293 col
= GetOwner()->GetColumnAt(colnum
);
3294 if (col
->IsHidden())
3295 continue; // skip it!
3297 unsigned int w
= col
->GetWidth();
3298 if (x_start
+w
>= (unsigned int)x
)
3305 item
= GetItemByRow( GetLineAt( y
) );
3308 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3309 const wxDataViewColumn
* column
)
3314 unsigned int cols
= GetOwner()->GetColumnCount();
3315 // If column is null the loop will compute the combined width of all columns.
3316 // Otherwise, it will compute the x position of the column we are looking for.
3317 for (unsigned int i
= 0; i
< cols
; i
++)
3319 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3324 if (col
->IsHidden())
3325 continue; // skip it!
3327 xpos
+= col
->GetWidth();
3328 width
+= col
->GetWidth();
3333 // If we have a column, we need can get its width directly.
3334 if(column
->IsHidden())
3337 width
= column
->GetWidth();
3342 // If we have no column, we reset the x position back to zero.
3346 // we have to take an expander column into account and compute its indentation
3347 // to get the correct x position where the actual text is
3349 int row
= GetRowByItem(item
);
3351 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3353 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3354 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3355 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3358 wxRect
itemRect( xpos
+ indent
,
3359 GetLineStart( row
),
3361 GetLineHeight( row
) );
3363 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3364 &itemRect
.x
, &itemRect
.y
);
3369 int wxDataViewMainWindow::RecalculateCount() const
3371 if (IsVirtualList())
3373 wxDataViewVirtualListModel
*list_model
=
3374 (wxDataViewVirtualListModel
*) GetModel();
3376 return list_model
->GetCount();
3380 return m_root
->GetSubTreeCount();
3384 class ItemToRowJob
: public DoJob
3387 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3394 // Maybe binary search will help to speed up this process
3395 virtual int operator() ( wxDataViewTreeNode
* node
)
3398 if( node
->GetItem() == item
)
3403 if( node
->GetItem() == *m_iter
)
3406 return DoJob::CONTINUE
;
3410 ret
+= node
->GetSubTreeCount();
3411 return DoJob::SKIP_SUBTREE
;
3416 // the row number is begin from zero
3417 int GetResult() const
3421 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3422 wxDataViewItem item
;
3427 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3429 const wxDataViewModel
* model
= GetModel();
3433 if (IsVirtualList())
3435 return wxPtrToUInt( item
.GetID() ) -1;
3442 // Compose the parent-chain of the item we are looking for
3443 wxVector
<wxDataViewItem
> parentChain
;
3444 wxDataViewItem
it( item
);
3447 parentChain
.push_back(it
);
3448 it
= model
->GetParent(it
);
3451 // add an 'invalid' item to represent our 'invisible' root node
3452 parentChain
.push_back(wxDataViewItem());
3454 // the parent chain was created by adding the deepest parent first.
3455 // so if we want to start at the root node, we have to iterate backwards through the vector
3456 ItemToRowJob
job( item
, parentChain
.rbegin() );
3457 Walker( m_root
, job
);
3458 return job
.GetResult();
3462 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3463 wxDataViewTreeNode
* node
)
3465 if( !model
->IsContainer( item
) )
3468 wxDataViewItemArray children
;
3469 unsigned int num
= model
->GetChildren( item
, children
);
3471 for ( unsigned int index
= 0; index
< num
; index
++ )
3473 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3475 if( model
->IsContainer(children
[index
]) )
3476 n
->SetHasChildren( true );
3478 node
->InsertChild(n
, index
);
3481 wxASSERT( node
->IsOpen() );
3482 node
->ChangeSubTreeCount(+num
);
3485 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3489 if (GetModel()->IsVirtualListModel())
3495 m_root
= wxDataViewTreeNode::CreateRootNode();
3497 // First we define a invalid item to fetch the top-level elements
3498 wxDataViewItem item
;
3500 BuildTreeHelper( model
, item
, m_root
);
3504 void wxDataViewMainWindow::DestroyTree()
3506 if (!IsVirtualList())
3514 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3516 // Edit the current column editable in 'mode'. If no column is focused
3517 // (typically because the user has full row selected), try to find the
3518 // first editable column (this would typically be a checkbox for
3519 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3520 // focus on the checkbox column; or on the only editable text column).
3522 wxDataViewColumn
*candidate
= m_currentCol
;
3525 !IsCellEditableInMode(item
, candidate
, mode
) &&
3526 !m_currentColSetByKeyboard
)
3528 // If current column was set by mouse to something not editable (in
3529 // 'mode') and the user pressed Space/F2 to edit it, treat the
3530 // situation as if there was whole-row focus, because that's what is
3531 // visually indicated and the mouse click could very well be targeted
3532 // on the row rather than on an individual cell.
3534 // But if it was done by keyboard, respect that even if the column
3535 // isn't editable, because focus is visually on that column and editing
3536 // something else would be surprising.
3542 const unsigned cols
= GetOwner()->GetColumnCount();
3543 for ( unsigned i
= 0; i
< cols
; i
++ )
3545 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3546 if ( c
->IsHidden() )
3549 if ( IsCellEditableInMode(item
, c
, mode
) )
3557 // If on container item without columns, only the expander column
3558 // may be directly editable:
3560 GetOwner()->GetExpanderColumn() != candidate
&&
3561 GetModel()->IsContainer(item
) &&
3562 !GetModel()->HasContainerColumns(item
) )
3564 candidate
= GetOwner()->GetExpanderColumn();
3570 if ( !IsCellEditableInMode(item
, candidate
, mode
) )
3576 bool wxDataViewMainWindow::IsCellEditableInMode(const wxDataViewItem
& item
,
3577 const wxDataViewColumn
*col
,
3578 wxDataViewCellMode mode
) const
3580 if ( col
->GetRenderer()->GetMode() != mode
)
3583 if ( !GetModel()->IsEnabled(item
, col
->GetModelColumn()) )
3589 void wxDataViewMainWindow::OnCharHook(wxKeyEvent
& event
)
3593 // Handle any keys special for the in-place editor and return without
3594 // calling Skip() below.
3595 switch ( event
.GetKeyCode() )
3598 m_editorRenderer
->CancelEditing();
3602 m_editorRenderer
->FinishEditing();
3610 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3612 wxWindow
* const parent
= GetParent();
3614 // propagate the char event upwards
3615 wxKeyEvent
eventForParent(event
);
3616 eventForParent
.SetEventObject(parent
);
3617 if ( parent
->ProcessWindowEvent(eventForParent
) )
3620 if ( parent
->HandleAsNavigationKey(event
) )
3623 // no item -> nothing to do
3624 if (!HasCurrentRow())
3630 // don't use m_linesPerPage directly as it might not be computed yet
3631 const int pageSize
= GetCountPerPage();
3632 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3634 switch ( event
.GetKeyCode() )
3637 if ( event
.HasModifiers() )
3644 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3645 // it. Only if that event is not handled do we activate column renderer (which
3646 // is normally done by Space) or even inline editing.
3648 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3650 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3653 le
.SetEventObject(parent
);
3654 le
.SetModel(GetModel());
3656 if ( parent
->ProcessWindowEvent(le
) )
3658 // else: fall through to WXK_SPACE handling
3662 if ( event
.HasModifiers() )
3669 // Space toggles activatable items or -- if not activatable --
3670 // starts inline editing (this is normally done using F2 on
3671 // Windows, but Space is common everywhere else, so use it too
3672 // for greater cross-platform compatibility).
3674 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3676 // Activate the current activatable column. If not column is focused (typically
3677 // because the user has full row selected), try to find the first activatable
3678 // column (this would typically be a checkbox and we don't want to force the user
3679 // to set focus on the checkbox column).
3680 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3682 if ( activatableCol
)
3684 const unsigned colIdx
= activatableCol
->GetModelColumn();
3685 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3687 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3688 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3689 cell
->WXActivateCell(cell_rect
, GetModel(), item
, colIdx
, NULL
);
3693 // else: fall through to WXK_F2 handling
3697 if ( event
.HasModifiers() )
3704 if( !m_selection
.empty() )
3706 // Mimic Windows 7 behavior: edit the item that has focus
3707 // if it is selected and the first selected item if focus
3708 // is out of selection.
3710 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3713 sel
= m_selection
[0];
3716 const wxDataViewItem item
= GetItemByRow(sel
);
3718 // Edit the current column. If no column is focused
3719 // (typically because the user has full row selected), try
3720 // to find the first editable column.
3721 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3724 GetOwner()->EditItem(item
, editableCol
);
3730 OnVerticalNavigation( -1, event
);
3734 OnVerticalNavigation( +1, event
);
3736 // Add the process for tree expanding/collapsing
3746 OnVerticalNavigation( +(int)GetRowCount(), event
);
3750 OnVerticalNavigation( -(int)GetRowCount(), event
);
3754 OnVerticalNavigation( -(pageSize
- 1), event
);
3758 OnVerticalNavigation( +(pageSize
- 1), event
);
3766 void wxDataViewMainWindow::OnVerticalNavigation(int delta
, const wxKeyEvent
& event
)
3768 // if there is no selection, we cannot move it anywhere
3769 if (!HasCurrentRow() || IsEmpty())
3772 int newRow
= (int)m_currentRow
+ delta
;
3774 // let's keep the new row inside the allowed range
3778 const int rowCount
= (int)GetRowCount();
3779 if ( newRow
>= rowCount
)
3780 newRow
= rowCount
- 1;
3782 unsigned int oldCurrent
= m_currentRow
;
3783 unsigned int newCurrent
= (unsigned int)newRow
;
3785 // in single selection we just ignore Shift as we can't select several
3787 if ( event
.ShiftDown() && !IsSingleSel() )
3789 RefreshRow( oldCurrent
);
3791 ChangeCurrentRow( newCurrent
);
3793 // select all the items between the old and the new one
3794 if ( oldCurrent
> newCurrent
)
3796 newCurrent
= oldCurrent
;
3797 oldCurrent
= m_currentRow
;
3800 SelectRows( oldCurrent
, newCurrent
, true );
3801 if (oldCurrent
!=newCurrent
)
3802 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3806 RefreshRow( oldCurrent
);
3808 // all previously selected items are unselected unless ctrl is held
3809 if ( !event
.ControlDown() )
3810 SelectAllRows(false);
3812 ChangeCurrentRow( newCurrent
);
3814 if ( !event
.ControlDown() )
3816 SelectRow( m_currentRow
, true );
3817 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3820 RefreshRow( m_currentRow
);
3823 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3826 void wxDataViewMainWindow::OnLeftKey()
3830 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3834 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3838 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3841 // Because TryAdvanceCurrentColumn() return false, we are at the first
3842 // column or using whole-row selection. In this situation, we can use
3843 // the standard TreeView handling of the left key.
3844 if (node
->HasChildren() && node
->IsOpen())
3846 Collapse(m_currentRow
);
3850 // if the node is already closed, we move the selection to its parent
3851 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3855 int parent
= GetRowByItem( parent_node
->GetItem() );
3858 unsigned int row
= m_currentRow
;
3859 SelectRow( row
, false);
3860 SelectRow( parent
, true );
3861 ChangeCurrentRow( parent
);
3862 GetOwner()->EnsureVisible( parent
, -1 );
3863 SendSelectionChangedEvent( parent_node
->GetItem() );
3870 void wxDataViewMainWindow::OnRightKey()
3874 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3878 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3882 if ( node
->HasChildren() )
3884 if ( !node
->IsOpen() )
3886 Expand( m_currentRow
);
3890 // if the node is already open, we move the selection to the first child
3891 unsigned int row
= m_currentRow
;
3892 SelectRow( row
, false );
3893 SelectRow( row
+ 1, true );
3894 ChangeCurrentRow( row
+ 1 );
3895 GetOwner()->EnsureVisible( row
+ 1, -1 );
3896 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3901 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3906 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3908 if ( GetOwner()->GetColumnCount() == 0 )
3911 if ( !m_useCellFocus
)
3916 // navigation shouldn't work in branch nodes without other columns:
3917 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3921 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3925 m_currentCol
= GetOwner()->GetColumnAt(1);
3926 m_currentColSetByKeyboard
= true;
3927 RefreshRow(m_currentRow
);
3934 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3936 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3939 GetOwner()->EnsureVisible(m_currentRow
, idx
);
3943 // We are going to the left of the second column. Reset to whole-row
3944 // focus (which means first column would be edited).
3945 m_currentCol
= NULL
;
3946 RefreshRow(m_currentRow
);
3950 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3951 m_currentColSetByKeyboard
= true;
3952 RefreshRow(m_currentRow
);
3956 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3958 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3960 // let the base handle mouse wheel events.
3965 if(event
.ButtonDown())
3967 // Not skipping button down events would prevent the system from
3968 // setting focus to this window as most (all?) of them do by default,
3969 // so skip it to enable default handling.
3973 int x
= event
.GetX();
3974 int y
= event
.GetY();
3975 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3976 wxDataViewColumn
*col
= NULL
;
3979 unsigned int cols
= GetOwner()->GetColumnCount();
3981 for (i
= 0; i
< cols
; i
++)
3983 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3985 continue; // skip it!
3987 if (x
< xpos
+ c
->GetWidth())
3992 xpos
+= c
->GetWidth();
3995 wxDataViewModel
* const model
= GetModel();
3997 const unsigned int current
= GetLineAt( y
);
3998 const wxDataViewItem item
= GetItemByRow(current
);
4000 // Handle right clicking here, before everything else as context menu
4001 // events should be sent even when we click outside of any item, unlike all
4003 if (event
.RightUp())
4005 wxWindow
*parent
= GetParent();
4006 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
4007 le
.SetEventObject(parent
);
4010 if ( item
.IsOk() && col
)
4013 le
.SetColumn( col
->GetModelColumn() );
4014 le
.SetDataViewColumn( col
);
4017 parent
->ProcessWindowEvent(le
);
4021 // Check if we clicked outside the item area.
4022 if ((current
>= GetRowCount()) || !col
)
4024 // Follow Windows convention here: clicking either left or right (but
4025 // not middle) button clears the existing selection.
4026 if (m_owner
&& (event
.LeftDown() || event
.RightDown()))
4028 if (!GetSelections().empty())
4030 m_owner
->UnselectAll();
4031 SendSelectionChangedEvent(wxDataViewItem());
4038 wxDataViewRenderer
*cell
= col
->GetRenderer();
4039 wxDataViewColumn
* const
4040 expander
= GetExpanderColumnOrFirstOne(GetOwner());
4042 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
4043 // button) and also determine the offset of the real cell start, skipping
4044 // the indentation and the expander itself.
4045 bool hoverOverExpander
= false;
4047 if ((!IsList()) && (expander
== col
))
4049 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4051 int indent
= node
->GetIndentLevel();
4052 itemOffset
= GetOwner()->GetIndent()*indent
;
4054 if ( node
->HasChildren() )
4056 // we make the rectangle we are looking in a bit bigger than the actual
4057 // visual expander so the user can hit that little thing reliably
4058 wxRect
rect(itemOffset
,
4059 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
4060 m_lineHeight
, m_lineHeight
);
4062 if( rect
.Contains(x
, y
) )
4064 // So the mouse is over the expander
4065 hoverOverExpander
= true;
4066 if (m_underMouse
&& m_underMouse
!= node
)
4068 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4069 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4071 if (m_underMouse
!= node
)
4073 // wxLogMessage("Do the row: %d", current);
4074 RefreshRow(current
);
4076 m_underMouse
= node
;
4080 // Account for the expander as well, even if this item doesn't have it,
4081 // its parent does so it still counts for the offset.
4082 itemOffset
+= m_lineHeight
;
4084 if (!hoverOverExpander
)
4086 if (m_underMouse
!= NULL
)
4088 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4089 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
4090 m_underMouse
= NULL
;
4094 #if wxUSE_DRAG_AND_DROP
4095 if (event
.Dragging())
4097 if (m_dragCount
== 0)
4099 // we have to report the raw, physical coords as we want to be
4100 // able to call HitTest(event.m_pointDrag) from the user code to
4101 // get the item being dragged
4102 m_dragStart
= event
.GetPosition();
4107 if (m_dragCount
!= 3)
4110 if (event
.LeftIsDown())
4112 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
4113 &m_dragStart
.x
, &m_dragStart
.y
);
4114 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
4115 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
4117 // Notify cell about drag
4118 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
4119 event
.SetEventObject( m_owner
);
4120 event
.SetItem( itemDragged
);
4121 event
.SetModel( model
);
4122 if (!m_owner
->HandleWindowEvent( event
))
4125 if (!event
.IsAllowed())
4128 wxDataObject
*obj
= event
.GetDataObject();
4132 wxDataViewDropSource
drag( this, drag_item_row
);
4133 drag
.SetData( *obj
);
4134 /* wxDragResult res = */ drag
.DoDragDrop(event
.GetDragFlags());
4143 #endif // wxUSE_DRAG_AND_DROP
4145 bool simulateClick
= false;
4147 if (event
.ButtonDClick())
4149 m_renameTimer
->Stop();
4150 m_lastOnSame
= false;
4153 bool ignore_other_columns
=
4154 ((expander
!= col
) &&
4155 (model
->IsContainer(item
)) &&
4156 (!model
->HasContainerColumns(item
)));
4158 if (event
.LeftDClick())
4160 if(hoverOverExpander
)
4162 // a double click on the expander will be converted into a "simulated" normal click
4163 simulateClick
= true;
4165 else if ( current
== m_lineLastClicked
)
4167 wxWindow
*parent
= GetParent();
4168 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4170 le
.SetColumn( col
->GetModelColumn() );
4171 le
.SetDataViewColumn( col
);
4172 le
.SetEventObject(parent
);
4173 le
.SetModel(GetModel());
4175 parent
->ProcessWindowEvent(le
);
4180 // The first click was on another item, so don't interpret this as
4181 // a double click, but as a simple click instead
4182 simulateClick
= true;
4186 if (event
.LeftUp() && !hoverOverExpander
)
4188 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4190 // select single line
4191 SelectAllRows( false );
4192 SelectRow( m_lineSelectSingleOnUp
, true );
4193 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4196 // If the user click the expander, we do not do editing even if the column
4197 // with expander are editable
4198 if (m_lastOnSame
&& !ignore_other_columns
)
4200 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4201 IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_EDITABLE
) )
4203 m_renameTimer
->Start( 100, true );
4207 m_lastOnSame
= false;
4208 m_lineSelectSingleOnUp
= (unsigned int)-1;
4210 else if(!event
.LeftUp())
4212 // This is necessary, because after a DnD operation in
4213 // from and to ourself, the up event is swallowed by the
4214 // DnD code. So on next non-up event (which means here and
4215 // now) m_lineSelectSingleOnUp should be reset.
4216 m_lineSelectSingleOnUp
= (unsigned int)-1;
4219 if (event
.RightDown())
4221 m_lineBeforeLastClicked
= m_lineLastClicked
;
4222 m_lineLastClicked
= current
;
4224 // If the item is already selected, do not update the selection.
4225 // Multi-selections should not be cleared if a selected item is clicked.
4226 if (!IsRowSelected(current
))
4228 SelectAllRows(false);
4229 const unsigned oldCurrent
= m_currentRow
;
4230 ChangeCurrentRow(current
);
4231 SelectRow(m_currentRow
,true);
4232 RefreshRow(oldCurrent
);
4233 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4236 else if (event
.MiddleDown())
4240 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4242 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4244 // hoverOverExpander being true tells us that our node must be
4245 // valid and have children.
4246 // So we don't need any extra checks.
4247 if( node
->IsOpen() )
4252 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4254 m_lineBeforeLastClicked
= m_lineLastClicked
;
4255 m_lineLastClicked
= current
;
4257 unsigned int oldCurrentRow
= m_currentRow
;
4258 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4260 bool cmdModifierDown
= event
.CmdDown();
4261 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4263 if ( IsSingleSel() || !IsRowSelected(current
) )
4265 SelectAllRows( false );
4266 ChangeCurrentRow(current
);
4267 SelectRow(m_currentRow
,true);
4268 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4270 else // multi sel & current is highlighted & no mod keys
4272 m_lineSelectSingleOnUp
= current
;
4273 ChangeCurrentRow(current
); // change focus
4276 else // multi sel & either ctrl or shift is down
4278 if (cmdModifierDown
)
4280 ChangeCurrentRow(current
);
4281 ReverseRowSelection(m_currentRow
);
4282 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4284 else if (event
.ShiftDown())
4286 ChangeCurrentRow(current
);
4288 unsigned int lineFrom
= oldCurrentRow
,
4291 if ( lineTo
< lineFrom
)
4294 lineFrom
= m_currentRow
;
4297 SelectRows(lineFrom
, lineTo
, true);
4298 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4300 else // !ctrl, !shift
4302 // test in the enclosing if should make it impossible
4303 wxFAIL_MSG( wxT("how did we get here?") );
4307 if (m_currentRow
!= oldCurrentRow
)
4308 RefreshRow( oldCurrentRow
);
4310 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4312 // Update selection here...
4314 m_currentColSetByKeyboard
= false;
4316 // This flag is used to decide whether we should start editing the item
4317 // label. We do it if the user clicks twice (but not double clicks,
4318 // i.e. simulateClick is false) on the same item but not if the click
4319 // was used for something else already, e.g. selecting the item (so it
4320 // must have been already selected) or giving the focus to the control
4321 // (so it must have had focus already).
4322 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4323 (current
== oldCurrentRow
)) && oldWasSelected
&&
4326 // Call ActivateCell() after everything else as under GTK+
4327 if ( IsCellEditableInMode(item
, col
, wxDATAVIEW_CELL_ACTIVATABLE
) )
4329 // notify cell about click
4330 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4332 wxRect
cell_rect( xpos
+ itemOffset
,
4333 GetLineStart( current
),
4334 col
->GetWidth() - itemOffset
,
4335 GetLineHeight( current
) );
4337 // Report position relative to the cell's custom area, i.e.
4338 // no the entire space as given by the control but the one
4339 // used by the renderer after calculation of alignment etc.
4341 // adjust the rectangle ourselves to account for the alignment
4342 wxRect rectItem
= cell_rect
;
4343 const int align
= cell
->GetAlignment();
4344 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4346 const wxSize size
= cell
->GetSize();
4348 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4350 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4351 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4352 else if ( align
& wxALIGN_RIGHT
)
4353 rectItem
.x
+= cell_rect
.width
- size
.x
;
4354 // else: wxALIGN_LEFT is the default
4357 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4359 if ( align
& wxALIGN_CENTER_VERTICAL
)
4360 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4361 else if ( align
& wxALIGN_BOTTOM
)
4362 rectItem
.y
+= cell_rect
.height
- size
.y
;
4363 // else: wxALIGN_TOP is the default
4367 wxMouseEvent
event2(event
);
4368 event2
.m_x
-= rectItem
.x
;
4369 event2
.m_y
-= rectItem
.y
;
4370 m_owner
->CalcUnscrolledPosition(event2
.m_x
, event2
.m_y
, &event2
.m_x
, &event2
.m_y
);
4372 /* ignore ret */ cell
->WXActivateCell
4377 col
->GetModelColumn(),
4384 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4388 if (HasCurrentRow())
4394 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4398 if (HasCurrentRow())
4404 void wxDataViewMainWindow::OnColumnsCountChanged()
4406 int editableCount
= 0;
4408 const unsigned cols
= GetOwner()->GetColumnCount();
4409 for ( unsigned i
= 0; i
< cols
; i
++ )
4411 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4412 if ( c
->IsHidden() )
4414 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4418 m_useCellFocus
= (editableCount
> 1);
4423 //-----------------------------------------------------------------------------
4425 //-----------------------------------------------------------------------------
4427 WX_DEFINE_LIST(wxDataViewColumnList
)
4429 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4430 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4431 EVT_SIZE(wxDataViewCtrl::OnSize
)
4434 wxDataViewCtrl::~wxDataViewCtrl()
4437 GetModel()->RemoveNotifier( m_notifier
);
4440 m_colsBestWidths
.clear();
4443 void wxDataViewCtrl::Init()
4445 m_cols
.DeleteContents(true);
4448 // No sorting column at start
4449 m_sortingColumnIdx
= wxNOT_FOUND
;
4451 m_headerArea
= NULL
;
4452 m_clientArea
= NULL
;
4454 m_colsDirty
= false;
4457 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4462 const wxValidator
& validator
,
4463 const wxString
& name
)
4465 // if ( (style & wxBORDER_MASK) == 0)
4466 // style |= wxBORDER_SUNKEN;
4470 if (!wxControl::Create( parent
, id
, pos
, size
,
4471 style
| wxScrolledWindowStyle
, validator
, name
))
4474 SetInitialSize(size
);
4477 MacSetClipChildren( true );
4480 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4482 // We use the cursor keys for moving the selection, not scrolling, so call
4483 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4484 // keyboard events forwarded to us from wxListMainWindow.
4485 DisableKeyboardScrolling();
4487 if (HasFlag(wxDV_NO_HEADER
))
4488 m_headerArea
= NULL
;
4490 m_headerArea
= new wxDataViewHeaderWindow(this);
4492 SetTargetWindow( m_clientArea
);
4494 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4496 sizer
->Add( m_headerArea
, 0, wxGROW
);
4497 sizer
->Add( m_clientArea
, 1, wxGROW
);
4503 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4505 return wxBORDER_THEME
;
4509 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4513 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4516 // we need to process arrows ourselves for scrolling
4517 if ( nMsg
== WM_GETDLGCODE
)
4519 rc
|= DLGC_WANTARROWS
;
4527 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4529 wxSize newsize
= size
;
4530 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4531 newsize
.y
-= m_headerArea
->GetSize().y
;
4536 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4538 // We need to override OnSize so that our scrolled
4539 // window a) does call Layout() to use sizers for
4540 // positioning the controls but b) does not query
4541 // the sizer for their size and use that for setting
4542 // the scrollable area as set that ourselves by
4543 // calling SetScrollbar() further down.
4549 // We must redraw the headers if their height changed. Normally this
4550 // shouldn't happen as the control shouldn't let itself be resized beneath
4551 // its minimal height but avoid the display artefacts that appear if it
4552 // does happen, e.g. because there is really not enough vertical space.
4553 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4554 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4556 m_headerArea
->Refresh();
4560 void wxDataViewCtrl::SetFocus()
4563 m_clientArea
->SetFocus();
4566 bool wxDataViewCtrl::SetFont(const wxFont
& font
)
4568 if (!wxControl::SetFont(font
))
4572 m_headerArea
->SetFont(font
);
4576 m_clientArea
->SetFont(font
);
4577 m_clientArea
->SetRowHeight(m_clientArea
->GetDefaultRowHeight());
4580 if (m_headerArea
|| m_clientArea
)
4582 InvalidateColBestWidths();
4591 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4593 if (!wxDataViewCtrlBase::AssociateModel( model
))
4598 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4599 model
->AddNotifier( m_notifier
);
4601 else if (m_notifier
)
4603 m_notifier
->Cleared();
4607 m_clientArea
->DestroyTree();
4611 m_clientArea
->BuildTree(model
);
4614 m_clientArea
->UpdateDisplay();
4619 #if wxUSE_DRAG_AND_DROP
4621 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4623 return m_clientArea
->EnableDragSource( format
);
4626 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4628 return m_clientArea
->EnableDropTarget( format
);
4631 #endif // wxUSE_DRAG_AND_DROP
4633 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4635 if (!wxDataViewCtrlBase::AppendColumn(col
))
4638 m_cols
.Append( col
);
4639 m_colsBestWidths
.push_back(CachedColWidthInfo());
4640 OnColumnsCountChanged();
4644 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4646 if (!wxDataViewCtrlBase::PrependColumn(col
))
4649 m_cols
.Insert( col
);
4650 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), CachedColWidthInfo());
4651 OnColumnsCountChanged();
4655 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4657 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4660 m_cols
.Insert( pos
, col
);
4661 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, CachedColWidthInfo());
4662 OnColumnsCountChanged();
4666 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4669 m_headerArea
->UpdateColumn(idx
);
4671 m_clientArea
->UpdateDisplay();
4674 void wxDataViewCtrl::OnColumnsCountChanged()
4677 m_headerArea
->SetColumnCount(GetColumnCount());
4679 m_clientArea
->OnColumnsCountChanged();
4682 void wxDataViewCtrl::DoSetExpanderColumn()
4684 m_clientArea
->UpdateDisplay();
4687 void wxDataViewCtrl::DoSetIndent()
4689 m_clientArea
->UpdateDisplay();
4692 unsigned int wxDataViewCtrl::GetColumnCount() const
4694 return m_cols
.GetCount();
4697 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4699 if ( !m_clientArea
)
4702 m_clientArea
->SetRowHeight(lineHeight
);
4707 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4712 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4714 // columns can't be reordered if there is no header window which allows
4716 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4719 return GetColumn(idx
);
4722 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4724 const unsigned count
= m_cols
.size();
4725 for ( unsigned n
= 0; n
< count
; n
++ )
4727 if ( m_cols
[n
] == column
)
4734 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4736 if ( m_colsBestWidths
[idx
].width
!= 0 )
4737 return m_colsBestWidths
[idx
].width
;
4739 const int count
= m_clientArea
->GetRowCount();
4740 wxDataViewColumn
*column
= GetColumn(idx
);
4741 wxDataViewRenderer
*renderer
=
4742 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4744 class MaxWidthCalculator
4747 MaxWidthCalculator(const wxDataViewCtrl
*dvc
,
4748 wxDataViewMainWindow
*clientArea
,
4749 wxDataViewRenderer
*renderer
,
4750 const wxDataViewModel
*model
,
4755 m_clientArea(clientArea
),
4756 m_renderer(renderer
),
4759 m_expanderSize(expanderSize
)
4763 !clientArea
->IsList() &&
4765 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl
*>(dvc
)) == dvc
->GetColumnAt(column
));
4768 void UpdateWithWidth(int width
)
4770 m_width
= wxMax(m_width
, width
);
4773 void UpdateWithRow(int row
)
4776 wxDataViewItem item
;
4778 if ( m_isExpanderCol
)
4780 wxDataViewTreeNode
*node
= m_clientArea
->GetTreeNodeByRow(row
);
4781 item
= node
->GetItem();
4782 indent
= m_dvc
->GetIndent() * node
->GetIndentLevel() + m_expanderSize
;
4786 item
= m_clientArea
->GetItemByRow(row
);
4789 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4790 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
+ indent
);
4793 int GetMaxWidth() const { return m_width
; }
4797 const wxDataViewCtrl
*m_dvc
;
4798 wxDataViewMainWindow
*m_clientArea
;
4799 wxDataViewRenderer
*m_renderer
;
4800 const wxDataViewModel
*m_model
;
4802 bool m_isExpanderCol
;
4806 MaxWidthCalculator
calculator(this, m_clientArea
, renderer
,
4807 GetModel(), column
->GetModelColumn(),
4808 m_clientArea
->GetRowHeight());
4810 calculator
.UpdateWithWidth(column
->GetMinWidth());
4813 calculator
.UpdateWithWidth(m_headerArea
->GetColumnTitleWidth(*column
));
4815 // The code below deserves some explanation. For very large controls, we
4816 // simply can't afford to calculate sizes for all items, it takes too
4817 // long. So the best we can do is to check the first and the last N/2
4818 // items in the control for some sufficiently large N and calculate best
4819 // sizes from that. That can result in the calculated best width being too
4820 // small for some outliers, but it's better to get slightly imperfect
4821 // result than to wait several seconds after every update. To avoid highly
4822 // visible miscalculations, we also include all currently visible items
4823 // no matter what. Finally, the value of N is determined dynamically by
4824 // measuring how much time we spent on the determining item widths so far.
4827 int top_part_end
= count
;
4828 static const long CALC_TIMEOUT
= 20/*ms*/;
4829 // don't call wxStopWatch::Time() too often
4830 static const unsigned CALC_CHECK_FREQ
= 100;
4833 // use some hard-coded limit, that's the best we can do without timer
4834 int top_part_end
= wxMin(500, count
);
4835 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4839 for ( row
= 0; row
< top_part_end
; row
++ )
4842 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4843 timer
.Time() > CALC_TIMEOUT
)
4845 #endif // wxUSE_STOPWATCH
4846 calculator
.UpdateWithRow(row
);
4849 // row is the first unmeasured item now; that's our value of N/2
4855 // add bottom N/2 items now:
4856 const int bottom_part_start
= wxMax(row
, count
- row
);
4857 for ( row
= bottom_part_start
; row
< count
; row
++ )
4859 calculator
.UpdateWithRow(row
);
4862 // finally, include currently visible items in the calculation:
4863 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4864 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4865 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4867 first_visible
= wxMax(first_visible
, top_part_end
);
4868 last_visible
= wxMin(bottom_part_start
, last_visible
);
4870 for ( row
= first_visible
; row
< last_visible
; row
++ )
4872 calculator
.UpdateWithRow(row
);
4875 wxLogTrace("dataview",
4876 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4878 count
- bottom_part_start
,
4879 wxMax(0, last_visible
- first_visible
),
4883 int max_width
= calculator
.GetMaxWidth();
4884 if ( max_width
> 0 )
4885 max_width
+= 2 * PADDING_RIGHTLEFT
;
4887 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
].width
= max_width
;
4891 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4892 unsigned int WXUNUSED(new_pos
))
4894 // do _not_ reorder m_cols elements here, they should always be in the
4895 // order in which columns were added, we only display the columns in
4897 m_clientArea
->UpdateDisplay();
4900 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4902 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4906 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4909 if ( m_clientArea
->GetCurrentColumn() == column
)
4910 m_clientArea
->ClearCurrentColumn();
4912 OnColumnsCountChanged();
4917 bool wxDataViewCtrl::ClearColumns()
4919 SetExpanderColumn(NULL
);
4921 m_colsBestWidths
.clear();
4923 m_clientArea
->ClearCurrentColumn();
4925 OnColumnsCountChanged();
4930 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4932 m_colsBestWidths
[idx
].width
= 0;
4933 m_colsBestWidths
[idx
].dirty
= true;
4937 void wxDataViewCtrl::InvalidateColBestWidths()
4939 // mark all columns as dirty:
4940 m_colsBestWidths
.clear();
4941 m_colsBestWidths
.resize(m_cols
.size());
4945 void wxDataViewCtrl::UpdateColWidths()
4947 m_colsDirty
= false;
4949 if ( !m_headerArea
)
4952 const unsigned len
= m_colsBestWidths
.size();
4953 for ( unsigned i
= 0; i
< len
; i
++ )
4955 // Note that we have to have an explicit 'dirty' flag here instead of
4956 // checking if the width==0, as is done in GetBestColumnWidth().
4958 // Testing width==0 wouldn't work correctly if some code called
4959 // GetWidth() after col. width invalidation but before
4960 // wxDataViewCtrl::UpdateColWidths() was called at idle time. This
4961 // would result in the header's column width getting out of sync with
4962 // the control itself.
4963 if ( m_colsBestWidths
[i
].dirty
)
4965 m_headerArea
->UpdateColumn(i
);
4966 m_colsBestWidths
[i
].dirty
= false;
4971 void wxDataViewCtrl::OnInternalIdle()
4973 wxDataViewCtrlBase::OnInternalIdle();
4979 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4981 unsigned int len
= GetColumnCount();
4982 for ( unsigned int i
= 0; i
< len
; i
++ )
4984 wxDataViewColumn
* col
= GetColumnAt(i
);
4992 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4994 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4995 : GetColumn(m_sortingColumnIdx
);
4998 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
5000 return GetItemByRow(m_clientArea
->GetCurrentRow());
5003 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
5005 const int row
= m_clientArea
->GetRowByItem(item
);
5007 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
5008 if ( static_cast<unsigned>(row
) != oldCurrent
)
5010 m_clientArea
->ChangeCurrentRow(row
);
5011 m_clientArea
->RefreshRow(oldCurrent
);
5012 m_clientArea
->RefreshRow(row
);
5016 wxDataViewColumn
*wxDataViewCtrl::GetCurrentColumn() const
5018 return m_clientArea
->GetCurrentColumn();
5021 int wxDataViewCtrl::GetSelectedItemsCount() const
5023 return m_clientArea
->GetSelections().size();
5026 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
5029 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
5031 const size_t len
= selections
.size();
5032 for ( size_t i
= 0; i
< len
; i
++ )
5034 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
5041 wxFAIL_MSG( "invalid item in selection - bad internal state" );
5048 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
5050 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
5052 wxDataViewItem last_parent
;
5054 int len
= sel
.GetCount();
5055 for( int i
= 0; i
< len
; i
++ )
5057 wxDataViewItem item
= sel
[i
];
5058 wxDataViewItem parent
= GetModel()->GetParent( item
);
5061 if (parent
!= last_parent
)
5062 ExpandAncestors(item
);
5065 last_parent
= parent
;
5066 int row
= m_clientArea
->GetRowByItem( item
);
5068 selection
.Add( static_cast<unsigned int>(row
) );
5071 m_clientArea
->SetSelections( selection
);
5074 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
5076 ExpandAncestors( item
);
5078 int row
= m_clientArea
->GetRowByItem( item
);
5081 // Unselect all rows before select another in the single select mode
5082 if (m_clientArea
->IsSingleSel())
5083 m_clientArea
->SelectAllRows(false);
5085 m_clientArea
->SelectRow(row
, true);
5087 // Also set focus to the selected item
5088 m_clientArea
->ChangeCurrentRow( row
);
5092 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
5094 int row
= m_clientArea
->GetRowByItem( item
);
5096 m_clientArea
->SelectRow(row
, false);
5099 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
5101 int row
= m_clientArea
->GetRowByItem( item
);
5104 return m_clientArea
->IsRowSelected(row
);
5109 void wxDataViewCtrl::SetAlternateRowColour(const wxColour
& colour
)
5111 m_alternateRowColour
= colour
;
5114 void wxDataViewCtrl::SelectAll()
5116 m_clientArea
->SelectAllRows(true);
5119 void wxDataViewCtrl::UnselectAll()
5121 m_clientArea
->SelectAllRows(false);
5124 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
5128 if( row
> (int) m_clientArea
->GetRowCount() )
5129 row
= m_clientArea
->GetRowCount();
5131 int first
= m_clientArea
->GetFirstVisibleRow();
5132 int last
= m_clientArea
->GetLastVisibleRow();
5134 m_clientArea
->ScrollTo( row
, column
);
5135 else if( row
> last
)
5136 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
5138 m_clientArea
->ScrollTo( first
, column
);
5141 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
5143 ExpandAncestors( item
);
5145 m_clientArea
->RecalculateDisplay();
5147 int row
= m_clientArea
->GetRowByItem(item
);
5150 if( column
== NULL
)
5151 EnsureVisible(row
, -1);
5153 EnsureVisible( row
, GetColumnIndex(column
) );
5158 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
5159 wxDataViewColumn
* &column
) const
5161 m_clientArea
->HitTest(point
, item
, column
);
5164 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
5165 const wxDataViewColumn
* column
) const
5167 return m_clientArea
->GetItemRect(item
, column
);
5170 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
5172 return m_clientArea
->GetItemByRow( row
);
5175 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
5177 return m_clientArea
->GetRowByItem( item
);
5180 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
5182 ExpandAncestors( item
);
5184 int row
= m_clientArea
->GetRowByItem( item
);
5187 m_clientArea
->Expand(row
);
5188 InvalidateColBestWidths();
5192 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
5194 int row
= m_clientArea
->GetRowByItem( item
);
5197 m_clientArea
->Collapse(row
);
5198 InvalidateColBestWidths();
5202 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
5204 int row
= m_clientArea
->GetRowByItem( item
);
5206 return m_clientArea
->IsExpanded(row
);
5210 void wxDataViewCtrl::EditItem(const wxDataViewItem
& item
, const wxDataViewColumn
*column
)
5212 wxCHECK_RET( item
.IsOk(), "invalid item" );
5213 wxCHECK_RET( column
, "no column provided" );
5215 m_clientArea
->StartEditing(item
, column
);
5218 #endif // !wxUSE_GENERICDATAVIEWCTRL
5220 #endif // wxUSE_DATAVIEWCTRL