1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/calctrl.h"
42 #include "wx/popupwin.h"
43 #include "wx/renderer.h"
44 #include "wx/dcbuffer.h"
47 #include "wx/listimpl.cpp"
48 #include "wx/imaglist.h"
49 #include "wx/headerctrl.h"
51 #include "wx/stopwatch.h"
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 class wxDataViewColumn
;
58 class wxDataViewHeaderWindow
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static const int SCROLL_UNIT_X
= 15;
67 // the cell padding on the left/right
68 static const int PADDING_RIGHTLEFT
= 3;
70 // the expander space margin
71 static const int EXPANDER_MARGIN
= 4;
74 static const int EXPANDER_OFFSET
= 4;
76 static const int EXPANDER_OFFSET
= 1;
79 // Below is the compare stuff.
80 // For the generic implementation, both the leaf nodes and the nodes are sorted for
81 // fast search when needed
82 static wxDataViewModel
* g_model
;
83 static int g_column
= -2;
84 static bool g_asending
= true;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
93 // Return the expander column or, if it is not set, the first column and also
94 // set it as the expander one for the future.
95 wxDataViewColumn
* GetExpanderColumnOrFirstOne(wxDataViewCtrl
* dataview
)
97 wxDataViewColumn
* expander
= dataview
->GetExpanderColumn();
100 // TODO-RTL: last column for RTL support
101 expander
= dataview
->GetColumnAt( 0 );
102 dataview
->SetExpanderColumn(expander
);
108 } // anonymous namespace
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
121 m_sortAscending
= true;
124 int wxDataViewColumn::GetWidth() const
128 case wxCOL_WIDTH_DEFAULT
:
129 return wxDVC_DEFAULT_WIDTH
;
131 case wxCOL_WIDTH_AUTOSIZE
:
132 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
133 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
140 void wxDataViewColumn::UpdateDisplay()
144 int idx
= m_owner
->GetColumnIndex( this );
145 m_owner
->OnColumnChange( idx
);
149 void wxDataViewColumn::SetSortOrder(bool ascending
)
154 // First unset the old sort column if any.
155 int oldSortKey
= m_owner
->GetSortingColumnIndex();
156 if ( oldSortKey
!= wxNOT_FOUND
)
158 m_owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
161 // Now set this one as the new sort column.
162 const int idx
= m_owner
->GetColumnIndex(this);
163 m_owner
->SetSortingColumnIndex(idx
);
166 m_sortAscending
= ascending
;
168 // Call this directly instead of using UpdateDisplay() as we already have
169 // the column index, no need to look it up again.
170 m_owner
->OnColumnChange(idx
);
173 //-----------------------------------------------------------------------------
174 // wxDataViewHeaderWindow
175 //-----------------------------------------------------------------------------
177 class wxDataViewHeaderWindow
: public wxHeaderCtrl
180 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
181 : wxHeaderCtrl(parent
)
185 wxDataViewCtrl
*GetOwner() const
186 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
189 // implement/override wxHeaderCtrl functions by forwarding them to the main
191 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
193 return *(GetOwner()->GetColumn(idx
));
196 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
198 wxDataViewCtrl
* const owner
= GetOwner();
200 int widthContents
= owner
->GetBestColumnWidth(idx
);
201 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
202 owner
->OnColumnChange(idx
);
208 bool SendEvent(wxEventType type
, unsigned int n
)
210 wxDataViewCtrl
* const owner
= GetOwner();
211 wxDataViewEvent
event(type
, owner
->GetId());
213 event
.SetEventObject(owner
);
215 event
.SetDataViewColumn(owner
->GetColumn(n
));
216 event
.SetModel(owner
->GetModel());
218 // for events created by wxDataViewHeaderWindow the
219 // row / value fields are not valid
220 return owner
->ProcessWindowEvent(event
);
223 void OnClick(wxHeaderCtrlEvent
& event
)
225 const unsigned idx
= event
.GetColumn();
227 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
230 // default handling for the column click is to sort by this column or
231 // toggle its sort order
232 wxDataViewCtrl
* const owner
= GetOwner();
233 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
234 if ( !col
->IsSortable() )
236 // no default handling for non-sortable columns
241 if ( col
->IsSortKey() )
243 // already using this column for sorting, just change the order
244 col
->ToggleSortOrder();
246 else // not using this column for sorting yet
248 col
->SetSortOrder(true);
251 wxDataViewModel
* const model
= owner
->GetModel();
255 owner
->OnColumnChange(idx
);
257 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
260 void OnRClick(wxHeaderCtrlEvent
& event
)
262 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
267 void OnResize(wxHeaderCtrlEvent
& event
)
269 wxDataViewCtrl
* const owner
= GetOwner();
271 const unsigned col
= event
.GetColumn();
272 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
273 GetOwner()->OnColumnChange(col
);
276 void OnEndReorder(wxHeaderCtrlEvent
& event
)
278 wxDataViewCtrl
* const owner
= GetOwner();
279 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
280 event
.GetNewOrder());
283 DECLARE_EVENT_TABLE()
284 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
287 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
288 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
289 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
291 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
292 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
294 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
297 //-----------------------------------------------------------------------------
298 // wxDataViewRenameTimer
299 //-----------------------------------------------------------------------------
301 class wxDataViewRenameTimer
: public wxTimer
304 wxDataViewMainWindow
*m_owner
;
307 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
311 //-----------------------------------------------------------------------------
312 // wxDataViewTreeNode
313 //-----------------------------------------------------------------------------
315 class wxDataViewTreeNode
;
316 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
318 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
319 wxDataViewTreeNode
** node2
);
321 class wxDataViewTreeNode
324 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
331 ~wxDataViewTreeNode()
335 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
336 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
347 static wxDataViewTreeNode
* CreateRootNode()
349 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
350 n
->SetHasChildren(true);
351 n
->m_branchData
->open
= true;
355 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
357 const wxDataViewTreeNodes
& GetChildNodes() const
359 wxASSERT( m_branchData
!= NULL
);
360 return m_branchData
->children
;
363 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
366 m_branchData
= new BranchNodeData
;
368 m_branchData
->children
.Insert(node
, index
);
370 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
372 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
375 void RemoveChild(wxDataViewTreeNode
*node
)
377 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
378 m_branchData
->children
.Remove(node
);
381 const wxDataViewItem
& GetItem() const { return m_item
; }
382 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
384 int GetIndentLevel() const
387 const wxDataViewTreeNode
* node
= this;
388 while( node
->GetParent()->GetParent() != NULL
)
390 node
= node
->GetParent();
398 return m_branchData
&& m_branchData
->open
;
403 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
407 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
408 const int len
= nodes
.GetCount();
409 for ( int i
= 0;i
< len
; i
++)
410 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
412 if (m_branchData
->open
)
414 ChangeSubTreeCount(-sum
);
415 m_branchData
->open
= !m_branchData
->open
;
419 m_branchData
->open
= !m_branchData
->open
;
420 ChangeSubTreeCount(+sum
);
424 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
425 // even if GetChildNodes() is empty; see below.
426 bool HasChildren() const
428 return m_branchData
!= NULL
;
431 void SetHasChildren(bool has
)
435 wxDELETE(m_branchData
);
437 else if ( m_branchData
== NULL
)
439 m_branchData
= new BranchNodeData
;
443 int GetSubTreeCount() const
445 return m_branchData
? m_branchData
->subTreeCount
: 0;
448 void ChangeSubTreeCount( int num
)
450 wxASSERT( m_branchData
!= NULL
);
452 if( !m_branchData
->open
)
455 m_branchData
->subTreeCount
+= num
;
456 wxASSERT( m_branchData
->subTreeCount
>= 0 );
459 m_parent
->ChangeSubTreeCount(num
);
469 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
471 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
472 int len
= nodes
.GetCount();
473 for (int i
= 0; i
< len
; i
++)
475 if ( nodes
[i
]->HasChildren() )
483 wxDataViewTreeNode
*m_parent
;
485 // Corresponding model item.
486 wxDataViewItem m_item
;
488 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
489 // separate struct in order to conserve memory.
490 struct BranchNodeData
498 // Child nodes. Note that this may be empty even if m_hasChildren in
499 // case this branch of the tree wasn't expanded and realized yet.
500 wxDataViewTreeNodes children
;
502 // Is the branch node currently open (expanded)?
505 // Total count of expanded (i.e. visible with the help of some
506 // scrolling) items in the subtree, but excluding this node. I.e. it is
507 // 0 for leaves and is the number of rows the subtree occupies for
512 BranchNodeData
*m_branchData
;
516 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
517 wxDataViewTreeNode
** node2
)
519 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
523 //-----------------------------------------------------------------------------
524 // wxDataViewMainWindow
525 //-----------------------------------------------------------------------------
527 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
529 class wxDataViewMainWindow
: public wxWindow
532 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
534 const wxPoint
&pos
= wxDefaultPosition
,
535 const wxSize
&size
= wxDefaultSize
,
536 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
537 virtual ~wxDataViewMainWindow();
539 bool IsList() const { return GetModel()->IsListModel(); }
540 bool IsVirtualList() const { return m_root
== NULL
; }
542 // notifications from wxDataViewModel
543 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
544 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
545 bool ItemChanged( const wxDataViewItem
&item
);
546 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
550 if (!IsVirtualList())
560 g_model
= GetModel();
561 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
564 if (g_model
->HasDefaultCompare())
572 g_column
= col
->GetModelColumn();
573 g_asending
= col
->IsSortOrderAscending();
576 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
577 wxDataViewCtrl
*GetOwner() { return m_owner
; }
578 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
580 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
581 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
583 #if wxUSE_DRAG_AND_DROP
584 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
585 #endif // wxUSE_DRAG_AND_DROP
586 void OnPaint( wxPaintEvent
&event
);
587 void OnChar( wxKeyEvent
&event
);
588 void OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
);
591 void OnMouse( wxMouseEvent
&event
);
592 void OnSetFocus( wxFocusEvent
&event
);
593 void OnKillFocus( wxFocusEvent
&event
);
595 void UpdateDisplay();
596 void RecalculateDisplay();
597 void OnInternalIdle();
599 void OnRenameTimer();
601 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
602 void ScrollTo( int rows
, int column
);
604 unsigned GetCurrentRow() const { return m_currentRow
; }
605 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
606 void ChangeCurrentRow( unsigned int row
);
607 bool TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
);
609 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
610 bool IsEmpty() { return GetRowCount() == 0; }
612 int GetCountPerPage() const;
613 int GetEndOfLastCol() const;
614 unsigned int GetFirstVisibleRow() const;
616 // I change this method to un const because in the tree view,
617 // the displaying number of the tree are changing along with the
618 // expanding/collapsing of the tree nodes
619 unsigned int GetLastVisibleRow();
620 unsigned int GetRowCount();
622 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
623 void SetSelections( const wxDataViewSelection
& sel
)
624 { m_selection
= sel
; UpdateDisplay(); }
625 void Select( const wxArrayInt
& aSelections
);
626 void SelectAllRows( bool on
);
627 void SelectRow( unsigned int row
, bool on
);
628 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
629 void ReverseRowSelection( unsigned int row
);
630 bool IsRowSelected( unsigned int row
);
631 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
633 void RefreshRow( unsigned int row
);
634 void RefreshRows( unsigned int from
, unsigned int to
);
635 void RefreshRowsAfter( unsigned int firstRow
);
637 // returns the colour to be used for drawing the rules
638 wxColour
GetRuleColour() const
640 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
643 wxRect
GetLineRect( unsigned int row
) const;
645 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
646 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
647 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
649 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
651 // Some useful functions for row and item mapping
652 wxDataViewItem
GetItemByRow( unsigned int row
) const;
653 int GetRowByItem( const wxDataViewItem
& item
) const;
655 // Methods for building the mapping tree
656 void BuildTree( wxDataViewModel
* model
);
658 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
659 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
661 void Expand( unsigned int row
);
662 void Collapse( unsigned int row
);
663 bool IsExpanded( unsigned int row
) const;
664 bool HasChildren( unsigned int row
) const;
666 #if wxUSE_DRAG_AND_DROP
667 bool EnableDragSource( const wxDataFormat
&format
);
668 bool EnableDropTarget( const wxDataFormat
&format
);
670 void RemoveDropHint();
671 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
672 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
673 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
675 #endif // wxUSE_DRAG_AND_DROP
677 void OnColumnsCountChanged();
680 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
681 // We did not need this temporarily
682 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
684 int RecalculateCount();
686 // Return false only if the event was vetoed by its handler.
687 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
689 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
691 wxDataViewColumn
*FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
);
694 wxDataViewCtrl
*m_owner
;
698 wxDataViewColumn
*m_currentCol
;
699 unsigned int m_currentRow
;
700 wxDataViewSelection m_selection
;
702 wxDataViewRenameTimer
*m_renameTimer
;
707 bool m_currentColSetByKeyboard
;
709 #if wxUSE_DRAG_AND_DROP
714 wxDataFormat m_dragFormat
;
717 wxDataFormat m_dropFormat
;
719 unsigned int m_dropHintLine
;
720 #endif // wxUSE_DRAG_AND_DROP
722 // for double click logic
723 unsigned int m_lineLastClicked
,
724 m_lineBeforeLastClicked
,
725 m_lineSelectSingleOnUp
;
727 // the pen used to draw horiz/vertical rules
730 // the pen used to draw the expander and the lines
733 // This is the tree structure of the model
734 wxDataViewTreeNode
* m_root
;
737 // This is the tree node under the cursor
738 wxDataViewTreeNode
* m_underMouse
;
741 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
742 DECLARE_EVENT_TABLE()
745 // ---------------------------------------------------------
746 // wxGenericDataViewModelNotifier
747 // ---------------------------------------------------------
749 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
752 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
753 { m_mainWindow
= mainWindow
; }
755 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
756 { return m_mainWindow
->ItemAdded( parent
, item
); }
757 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
758 { return m_mainWindow
->ItemDeleted( parent
, item
); }
759 virtual bool ItemChanged( const wxDataViewItem
& item
)
760 { return m_mainWindow
->ItemChanged(item
); }
761 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
762 { return m_mainWindow
->ValueChanged( item
, col
); }
763 virtual bool Cleared()
764 { return m_mainWindow
->Cleared(); }
765 virtual void Resort()
766 { m_mainWindow
->Resort(); }
768 wxDataViewMainWindow
*m_mainWindow
;
771 // ---------------------------------------------------------
772 // wxDataViewRenderer
773 // ---------------------------------------------------------
775 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
777 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
778 wxDataViewCellMode mode
,
780 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
784 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
788 wxDataViewRenderer::~wxDataViewRenderer()
793 wxDC
*wxDataViewRenderer::GetDC()
797 if (GetOwner() == NULL
)
799 if (GetOwner()->GetOwner() == NULL
)
801 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
807 void wxDataViewRenderer::SetAlignment( int align
)
812 int wxDataViewRenderer::GetAlignment() const
817 // ---------------------------------------------------------
818 // wxDataViewCustomRenderer
819 // ---------------------------------------------------------
821 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
823 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
824 wxDataViewCellMode mode
, int align
) :
825 wxDataViewRenderer( varianttype
, mode
, align
)
829 // ---------------------------------------------------------
830 // wxDataViewTextRenderer
831 // ---------------------------------------------------------
833 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
835 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
836 wxDataViewCellMode mode
, int align
) :
837 wxDataViewRenderer( varianttype
, mode
, align
)
841 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
843 m_text
= value
.GetString();
848 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
853 bool wxDataViewTextRenderer::HasEditorCtrl() const
858 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
859 wxRect labelRect
, const wxVariant
&value
)
861 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
862 wxPoint(labelRect
.x
,labelRect
.y
),
863 wxSize(labelRect
.width
,labelRect
.height
),
864 wxTE_PROCESS_ENTER
);
866 // select the text in the control an place the cursor at the end
867 ctrl
->SetInsertionPointEnd();
873 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
875 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
876 value
= text
->GetValue();
880 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
882 RenderText(m_text
, 0, rect
, dc
, state
);
886 wxSize
wxDataViewTextRenderer::GetSize() const
889 return GetTextExtent(m_text
);
891 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
894 // ---------------------------------------------------------
895 // wxDataViewBitmapRenderer
896 // ---------------------------------------------------------
898 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
900 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
901 wxDataViewCellMode mode
, int align
) :
902 wxDataViewRenderer( varianttype
, mode
, align
)
906 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
908 if (value
.GetType() == wxT("wxBitmap"))
910 if (value
.GetType() == wxT("wxIcon"))
916 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
921 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
924 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
925 else if (m_icon
.IsOk())
926 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
931 wxSize
wxDataViewBitmapRenderer::GetSize() const
934 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
935 else if (m_icon
.IsOk())
936 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
938 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
941 // ---------------------------------------------------------
942 // wxDataViewToggleRenderer
943 // ---------------------------------------------------------
945 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
947 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
948 wxDataViewCellMode mode
, int align
) :
949 wxDataViewRenderer( varianttype
, mode
, align
)
954 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
956 m_toggle
= value
.GetBool();
961 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
966 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
970 flags
|= wxCONTROL_CHECKED
;
971 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
972 GetEnabled() == false)
973 flags
|= wxCONTROL_DISABLED
;
975 // check boxes we draw must always have the same, standard size (if it's
976 // bigger than the cell size the checkbox will be truncated because the
977 // caller had set the clipping rectangle to prevent us from drawing outside
979 cell
.SetSize(GetSize());
981 wxRendererNative::Get().DrawCheckBox(
982 GetOwner()->GetOwner(),
990 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
992 wxDataViewModel
*model
,
993 const wxDataViewItem
& item
,
996 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
997 if (!wxRect(GetSize()).Contains(cursor
))
1000 return WXOnActivate(cell
, model
, item
, col
);
1003 bool wxDataViewToggleRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1004 wxDataViewModel
*model
,
1005 const wxDataViewItem
& item
,
1008 if (model
->IsEnabled(item
, col
))
1010 model
->ChangeValue(!m_toggle
, item
, col
);
1017 wxSize
wxDataViewToggleRenderer::GetSize() const
1019 // the window parameter is not used by GetCheckBoxSize() so it's
1020 // safe to pass NULL
1021 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1024 // ---------------------------------------------------------
1025 // wxDataViewProgressRenderer
1026 // ---------------------------------------------------------
1028 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1030 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1031 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1032 wxDataViewRenderer( varianttype
, mode
, align
)
1038 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1040 m_value
= (long) value
;
1042 if (m_value
< 0) m_value
= 0;
1043 if (m_value
> 100) m_value
= 100;
1048 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1050 value
= (long) m_value
;
1055 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1057 // deflate the rect to leave a small border between bars in adjacent rows
1058 wxRect bar
= rect
.Deflate(0, 1);
1060 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1061 dc
->SetPen( *wxBLACK_PEN
);
1062 dc
->DrawRectangle( bar
);
1064 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1065 dc
->SetPen( *wxTRANSPARENT_PEN
);
1067 const wxDataViewItemAttr
& attr
= GetAttr();
1068 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1070 dc
->DrawRectangle( bar
);
1075 wxSize
wxDataViewProgressRenderer::GetSize() const
1077 return wxSize(40,12);
1080 // ---------------------------------------------------------
1081 // wxDataViewDateRenderer
1082 // ---------------------------------------------------------
1084 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
1086 #if wxUSE_DATE_RENDERER_POPUP
1088 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1091 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1092 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
1093 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
1098 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
1099 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1100 sizer
->Add( m_cal
, 1, wxGROW
);
1105 void OnCalendar( wxCalendarEvent
&event
);
1107 wxCalendarCtrl
*m_cal
;
1108 wxDataViewModel
*m_model
;
1110 const wxDataViewItem
& m_item
;
1113 virtual void OnDismiss()
1118 DECLARE_EVENT_TABLE()
1121 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1122 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1125 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1127 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1131 #endif // wxUSE_DATE_RENDERER_POPUP
1133 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1135 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1136 wxDataViewCellMode mode
, int align
) :
1137 wxDataViewRenderer( varianttype
, mode
, align
)
1141 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1143 m_date
= value
.GetDateTime();
1148 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1154 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1156 wxString tmp
= m_date
.FormatDate();
1157 RenderText( tmp
, 0, cell
, dc
, state
);
1161 wxSize
wxDataViewDateRenderer::GetSize() const
1163 return GetTextExtent(m_date
.FormatDate());
1166 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1167 wxDataViewModel
*model
,
1168 const wxDataViewItem
& item
,
1171 wxDateTime dtOld
= m_date
;
1173 #if wxUSE_DATE_RENDERER_POPUP
1174 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1175 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1176 wxPoint pos
= wxGetMousePosition();
1179 popup
->Popup( popup
->m_cal
);
1180 #else // !wxUSE_DATE_RENDERER_POPUP
1181 wxMessageBox(dtOld
.Format());
1182 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1187 // ---------------------------------------------------------
1188 // wxDataViewIconTextRenderer
1189 // ---------------------------------------------------------
1191 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1193 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1194 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1195 wxDataViewRenderer( varianttype
, mode
, align
)
1198 SetAlignment(align
);
1201 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1207 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1212 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1216 const wxIcon
& icon
= m_value
.GetIcon();
1219 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1220 xoffset
= icon
.GetWidth()+4;
1223 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1228 wxSize
wxDataViewIconTextRenderer::GetSize() const
1230 if (!m_value
.GetText().empty())
1232 wxSize size
= GetTextExtent(m_value
.GetText());
1234 if (m_value
.GetIcon().IsOk())
1235 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1238 return wxSize(80,20);
1241 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1243 wxDataViewIconText iconText
;
1246 wxString text
= iconText
.GetText();
1248 // adjust the label rect to take the width of the icon into account
1249 if (iconText
.GetIcon().IsOk())
1251 int w
= iconText
.GetIcon().GetWidth() + 4;
1253 labelRect
.width
-= w
;
1256 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1257 wxPoint(labelRect
.x
,labelRect
.y
),
1258 wxSize(labelRect
.width
,labelRect
.height
),
1259 wxTE_PROCESS_ENTER
);
1261 // select the text in the control an place the cursor at the end
1262 ctrl
->SetInsertionPointEnd();
1268 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1270 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1272 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1277 //-----------------------------------------------------------------------------
1278 // wxDataViewDropTarget
1279 //-----------------------------------------------------------------------------
1281 #if wxUSE_DRAG_AND_DROP
1283 class wxBitmapCanvas
: public wxWindow
1286 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1287 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1290 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1293 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1296 dc
.DrawBitmap( m_bitmap
, 0, 0);
1302 class wxDataViewDropSource
: public wxDropSource
1305 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1313 ~wxDataViewDropSource()
1318 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1320 wxPoint pos
= wxGetMousePosition();
1324 int liney
= m_win
->GetLineStart( m_row
);
1326 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1327 m_win
->ClientToScreen( &linex
, &liney
);
1328 m_dist_x
= pos
.x
- linex
;
1329 m_dist_y
= pos
.y
- liney
;
1332 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1334 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1335 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1337 wxFRAME_TOOL_WINDOW
|
1338 wxFRAME_FLOAT_ON_PARENT
|
1339 wxFRAME_NO_TASKBAR
|
1341 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1346 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1347 m_hint
->SetTransparent( 128 );
1353 wxDataViewMainWindow
*m_win
;
1356 int m_dist_x
,m_dist_y
;
1360 class wxDataViewDropTarget
: public wxDropTarget
1363 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1369 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1371 wxDataFormat format
= GetMatchingPair();
1372 if (format
== wxDF_INVALID
)
1374 return m_win
->OnDragOver( format
, x
, y
, def
);
1377 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1379 wxDataFormat format
= GetMatchingPair();
1380 if (format
== wxDF_INVALID
)
1382 return m_win
->OnDrop( format
, x
, y
);
1385 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1387 wxDataFormat format
= GetMatchingPair();
1388 if (format
== wxDF_INVALID
)
1392 return m_win
->OnData( format
, x
, y
, def
);
1395 virtual void OnLeave()
1396 { m_win
->OnLeave(); }
1398 wxDataViewMainWindow
*m_win
;
1401 #endif // wxUSE_DRAG_AND_DROP
1403 //-----------------------------------------------------------------------------
1404 // wxDataViewRenameTimer
1405 //-----------------------------------------------------------------------------
1407 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1412 void wxDataViewRenameTimer::Notify()
1414 m_owner
->OnRenameTimer();
1417 //-----------------------------------------------------------------------------
1418 // wxDataViewMainWindow
1419 //-----------------------------------------------------------------------------
1421 // The tree building helper, declared firstly
1422 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1423 wxDataViewTreeNode
* node
);
1425 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1427 if (row1
> row2
) return 1;
1428 if (row1
== row2
) return 0;
1432 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1434 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1435 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1436 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1437 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1438 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1439 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1442 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1443 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1444 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1445 m_selection( wxDataViewSelectionCmp
)
1450 m_lastOnSame
= false;
1451 m_renameTimer
= new wxDataViewRenameTimer( this );
1453 // TODO: user better initial values/nothing selected
1454 m_currentCol
= NULL
;
1455 m_currentColSetByKeyboard
= false;
1456 m_useCellFocus
= false;
1459 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1461 #if wxUSE_DRAG_AND_DROP
1463 m_dragStart
= wxPoint(0,0);
1465 m_dragEnabled
= false;
1466 m_dropEnabled
= false;
1468 m_dropHintLine
= (unsigned int) -1;
1469 #endif // wxUSE_DRAG_AND_DROP
1471 m_lineLastClicked
= (unsigned int) -1;
1472 m_lineBeforeLastClicked
= (unsigned int) -1;
1473 m_lineSelectSingleOnUp
= (unsigned int) -1;
1477 SetBackgroundColour( *wxWHITE
);
1479 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1481 m_penRule
= wxPen(GetRuleColour());
1483 // compose a pen whichcan draw black lines
1484 // TODO: maybe there is something system colour to use
1485 m_penExpander
= wxPen(wxColour(0,0,0));
1487 m_root
= wxDataViewTreeNode::CreateRootNode();
1489 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1491 m_underMouse
= NULL
;
1495 wxDataViewMainWindow::~wxDataViewMainWindow()
1498 delete m_renameTimer
;
1502 #if wxUSE_DRAG_AND_DROP
1503 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1505 m_dragFormat
= format
;
1506 m_dragEnabled
= format
!= wxDF_INVALID
;
1511 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1513 m_dropFormat
= format
;
1514 m_dropEnabled
= format
!= wxDF_INVALID
;
1517 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1522 void wxDataViewMainWindow::RemoveDropHint()
1527 RefreshRow( m_dropHintLine
);
1528 m_dropHintLine
= (unsigned int) -1;
1532 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1533 wxCoord y
, wxDragResult def
)
1537 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1538 unsigned int row
= GetLineAt( yy
);
1540 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1546 wxDataViewItem item
= GetItemByRow( row
);
1548 wxDataViewModel
*model
= GetModel();
1550 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1551 event
.SetEventObject( m_owner
);
1552 event
.SetItem( item
);
1553 event
.SetModel( model
);
1554 event
.SetDataFormat( format
);
1555 if (!m_owner
->HandleWindowEvent( event
))
1561 if (!event
.IsAllowed())
1568 if (m_dropHint
&& (row
!= m_dropHintLine
))
1569 RefreshRow( m_dropHintLine
);
1571 m_dropHintLine
= row
;
1577 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1583 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1584 unsigned int row
= GetLineAt( yy
);
1586 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1589 wxDataViewItem item
= GetItemByRow( row
);
1591 wxDataViewModel
*model
= GetModel();
1593 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1594 event
.SetEventObject( m_owner
);
1595 event
.SetItem( item
);
1596 event
.SetModel( model
);
1597 event
.SetDataFormat( format
);
1598 if (!m_owner
->HandleWindowEvent( event
))
1601 if (!event
.IsAllowed())
1607 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1612 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1613 unsigned int row
= GetLineAt( yy
);
1615 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1618 wxDataViewItem item
= GetItemByRow( row
);
1620 wxDataViewModel
*model
= GetModel();
1622 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1624 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1625 event
.SetEventObject( m_owner
);
1626 event
.SetItem( item
);
1627 event
.SetModel( model
);
1628 event
.SetDataFormat( format
);
1629 event
.SetDataSize( obj
->GetSize() );
1630 event
.SetDataBuffer( obj
->GetData() );
1631 if (!m_owner
->HandleWindowEvent( event
))
1634 if (!event
.IsAllowed())
1640 void wxDataViewMainWindow::OnLeave()
1645 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1647 int height
= GetLineHeight( row
);
1649 unsigned int cols
= GetOwner()->GetColumnCount();
1651 for (col
= 0; col
< cols
; col
++)
1653 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1654 if (column
->IsHidden())
1655 continue; // skip it!
1656 width
+= column
->GetWidth();
1662 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1663 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1664 indent
= indent
+ m_lineHeight
;
1665 // try to use the m_lineHeight as the expander space
1669 wxBitmap
bitmap( width
, height
);
1670 wxMemoryDC
dc( bitmap
);
1671 dc
.SetFont( GetFont() );
1672 dc
.SetPen( *wxBLACK_PEN
);
1673 dc
.SetBrush( *wxWHITE_BRUSH
);
1674 dc
.DrawRectangle( 0,0,width
,height
);
1676 wxDataViewModel
*model
= m_owner
->GetModel();
1678 wxDataViewColumn
* const
1679 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1682 for (col
= 0; col
< cols
; col
++)
1684 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1685 wxDataViewRenderer
*cell
= column
->GetRenderer();
1687 if (column
->IsHidden())
1688 continue; // skip it!
1690 width
= column
->GetWidth();
1692 if (column
== expander
)
1695 wxDataViewItem item
= GetItemByRow( row
);
1696 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1698 wxRect
item_rect(x
, 0, width
, height
);
1699 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1701 // dc.SetClippingRegion( item_rect );
1702 cell
->WXCallRender(item_rect
, &dc
, 0);
1703 // dc.DestroyClippingRegion();
1711 #endif // wxUSE_DRAG_AND_DROP
1714 // Draw focus rect for individual cell. Unlike native focus rect, we render
1715 // this in foreground text color (typically white) to enhance contrast and
1717 static void DrawSelectedCellFocusRect(wxDC
& dc
, const wxRect
& rect
)
1719 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1721 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1722 // may be short traits and not really dots
1724 // note that to behave in the same manner as DrawRect(), we must exclude
1725 // the bottom and right borders from the rectangle
1726 wxCoord x1
= rect
.GetLeft(),
1728 x2
= rect
.GetRight(),
1729 y2
= rect
.GetBottom();
1731 wxDCPenChanger
pen(dc
, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
1734 for ( z
= x1
+ 1; z
< x2
; z
+= 2 )
1735 dc
.DrawPoint(z
, rect
.GetTop());
1737 wxCoord shift
= z
== x2
? 0 : 1;
1738 for ( z
= y1
+ shift
; z
< y2
; z
+= 2 )
1739 dc
.DrawPoint(x2
, z
);
1741 shift
= z
== y2
? 0 : 1;
1742 for ( z
= x2
- shift
; z
> x1
; z
-= 2 )
1743 dc
.DrawPoint(z
, y2
);
1745 shift
= z
== x1
? 0 : 1;
1746 for ( z
= y2
- shift
; z
> y1
; z
-= 2 )
1747 dc
.DrawPoint(x1
, z
);
1751 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1753 wxDataViewModel
*model
= GetModel();
1754 wxAutoBufferedPaintDC
dc( this );
1757 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1758 dc
.SetPen( *wxTRANSPARENT_PEN
);
1759 dc
.DrawRectangle(GetClientSize());
1764 // No items to draw.
1769 GetOwner()->PrepareDC( dc
);
1770 dc
.SetFont( GetFont() );
1772 wxRect update
= GetUpdateRegion().GetBox();
1773 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1775 // compute which items needs to be redrawn
1776 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1777 unsigned int item_count
=
1778 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1779 (int)(GetRowCount( ) - item_start
));
1780 unsigned int item_last
= item_start
+ item_count
;
1782 // Send the event to wxDataViewCtrl itself.
1783 wxWindow
* const parent
= GetParent();
1784 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1785 cache_event
.SetEventObject(parent
);
1786 cache_event
.SetCache(item_start
, item_last
- 1);
1787 parent
->ProcessWindowEvent(cache_event
);
1789 // compute which columns needs to be redrawn
1790 unsigned int cols
= GetOwner()->GetColumnCount();
1793 // we assume that we have at least one column below and painting an
1794 // empty control is unnecessary anyhow
1798 unsigned int col_start
= 0;
1799 unsigned int x_start
;
1800 for (x_start
= 0; col_start
< cols
; col_start
++)
1802 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1803 if (col
->IsHidden())
1804 continue; // skip it!
1806 unsigned int w
= col
->GetWidth();
1807 if (x_start
+w
>= (unsigned int)update
.x
)
1813 unsigned int col_last
= col_start
;
1814 unsigned int x_last
= x_start
;
1815 for (; col_last
< cols
; col_last
++)
1817 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1818 if (col
->IsHidden())
1819 continue; // skip it!
1821 if (x_last
> (unsigned int)update
.GetRight())
1824 x_last
+= col
->GetWidth();
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 // deal with the expander
2023 if ((!IsList()) && (col
== expander
))
2025 // Calculate the indent first
2026 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
2028 // we reserve m_lineHeight of horizontal space for the expander
2029 // but leave EXPANDER_MARGIN around the expander itself
2030 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
2032 indent
+= m_lineHeight
;
2034 // draw expander if needed and visible
2035 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
2037 dc
.SetPen( m_penExpander
);
2038 dc
.SetBrush( wxNullBrush
);
2040 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2041 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
2042 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
2044 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
2047 if ( m_underMouse
== node
)
2048 flag
|= wxCONTROL_CURRENT
;
2049 if ( node
->IsOpen() )
2050 flag
|= wxCONTROL_EXPANDED
;
2052 // ensure that we don't overflow the cell (which might
2053 // happen if the column is very narrow)
2054 wxDCClipper
clip(dc
, cell_rect
);
2056 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2059 // force the expander column to left-center align
2060 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2063 wxRect item_rect
= cell_rect
;
2064 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
2066 // account for the tree indent (harmless if we're not indented)
2067 item_rect
.x
+= indent
;
2068 item_rect
.width
-= indent
;
2070 if ( item_rect
.width
<= 0 )
2074 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2075 state
|= wxDATAVIEW_CELL_SELECTED
;
2077 // TODO: it would be much more efficient to create a clipping
2078 // region for the entire column being rendered (in the OnPaint
2079 // of wxDataViewMainWindow) instead of a single clip region for
2080 // each cell. However it would mean that each renderer should
2081 // respect the given wxRect's top & bottom coords, eventually
2082 // violating only the left & right coords - however the user can
2083 // make its own renderer and thus we cannot be sure of that.
2084 wxDCClipper
clip(dc
, item_rect
);
2086 cell
->WXCallRender(item_rect
, &dc
, state
);
2089 cell_rect
.x
+= cell_rect
.width
;
2093 void wxDataViewMainWindow::OnRenameTimer()
2095 // We have to call this here because changes may just have
2096 // been made and no screen update taken place.
2099 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2100 // (needs to be tested!)
2104 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2106 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
2108 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2111 //-----------------------------------------------------------------------------
2112 // Helper class for do operation on the tree node
2113 //-----------------------------------------------------------------------------
2118 virtual ~DoJob() { }
2120 // The return value control how the tree-walker tranverse the tree
2123 DONE
, // Job done, stop traversing and return
2124 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2125 CONTINUE
// Job not done, continue
2128 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2131 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2133 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2135 switch( func( node
) )
2139 case DoJob::SKIP_SUBTREE
:
2141 case DoJob::CONTINUE
:
2145 if ( node
->HasChildren() )
2147 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2149 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2153 if ( Walker(*i
, func
) )
2161 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2163 if (IsVirtualList())
2165 wxDataViewVirtualListModel
*list_model
=
2166 (wxDataViewVirtualListModel
*) GetModel();
2167 m_count
= list_model
->GetCount();
2173 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2178 wxDataViewItemArray siblings
;
2179 GetModel()->GetChildren(parent
, siblings
);
2180 int itemPos
= siblings
.Index(item
, /*fromEnd=*/true);
2181 wxCHECK_MSG( itemPos
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2183 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2184 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2186 parentNode
->SetHasChildren(true);
2187 parentNode
->InsertChild(itemNode
, itemPos
);
2188 parentNode
->ChangeSubTreeCount(+1);
2193 GetOwner()->InvalidateColBestWidths();
2199 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2200 const wxDataViewItem
& item
)
2202 if (IsVirtualList())
2204 wxDataViewVirtualListModel
*list_model
=
2205 (wxDataViewVirtualListModel
*) GetModel();
2206 m_count
= list_model
->GetCount();
2208 if ( !m_selection
.empty() )
2210 const int row
= GetRowByItem(item
);
2212 int rowIndexInSelection
= wxNOT_FOUND
;
2214 const size_t selCount
= m_selection
.size();
2215 for ( size_t i
= 0; i
< selCount
; i
++ )
2217 if ( m_selection
[i
] == (unsigned)row
)
2218 rowIndexInSelection
= i
;
2219 else if ( m_selection
[i
] > (unsigned)row
)
2223 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2224 m_selection
.RemoveAt(rowIndexInSelection
);
2228 else // general case
2230 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2232 // Notice that it is possible that the item being deleted is not in the
2233 // tree at all, for example we could be deleting a never shown (because
2234 // collapsed) item in a tree model. So it's not an error if we don't know
2235 // about this item, just return without doing anything then.
2239 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2240 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2242 // We can't use FindNode() to find 'item', because it was already
2243 // removed from the model by the time ItemDeleted() is called, so we
2244 // have to do it manually. We keep track of its position as well for
2246 int itemPosInNode
= 0;
2247 wxDataViewTreeNode
*itemNode
= NULL
;
2248 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2249 i
!= parentsChildren
.end();
2250 ++i
, ++itemPosInNode
)
2252 if( (*i
)->GetItem() == item
)
2259 // If the parent wasn't expanded, it's possible that we didn't have a
2260 // node corresponding to 'item' and so there's nothing left to do.
2263 // If this was the last child to be removed, it's possible the parent
2264 // node became a leaf. Let's ask the model about it.
2265 if ( parentNode
->GetChildNodes().empty() )
2266 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2271 // Delete the item from wxDataViewTreeNode representation:
2272 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2274 parentNode
->RemoveChild(itemNode
);
2276 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2278 // Make the row number invalid and get a new valid one when user call GetRowCount
2281 // If this was the last child to be removed, it's possible the parent
2282 // node became a leaf. Let's ask the model about it.
2283 if ( parentNode
->GetChildNodes().empty() )
2284 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2286 // Update selection by removing 'item' and its entire children tree from the selection.
2287 if ( !m_selection
.empty() )
2289 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2290 // the parent ('parentNode') and position in its list of children
2292 if ( itemPosInNode
== 0 )
2294 // 1st child, row number is that of the parent parentNode + 1
2295 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2299 // row number is that of the sibling above 'item' + its subtree if any + 1
2300 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2302 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2303 siblingNode
->GetSubTreeCount() +
2307 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2309 const size_t numSelections
= m_selection
.size();
2310 for ( size_t i
= 0; i
< numSelections
; ++i
)
2312 const int s
= m_selection
[i
];
2314 newsel
.push_back(s
);
2315 else if ( s
>= itemRow
+ itemsDeleted
)
2316 newsel
.push_back(s
- itemsDeleted
);
2317 // else: deleted item, remove from selection
2320 m_selection
= newsel
;
2324 // Change the current row to the last row if the current exceed the max row number
2325 if( m_currentRow
> GetRowCount() )
2326 ChangeCurrentRow(m_count
- 1);
2328 GetOwner()->InvalidateColBestWidths();
2334 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2339 GetOwner()->InvalidateColBestWidths();
2342 wxWindow
*parent
= GetParent();
2343 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2344 le
.SetEventObject(parent
);
2345 le
.SetModel(GetModel());
2347 parent
->ProcessWindowEvent(le
);
2352 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2354 int view_column
= -1;
2355 unsigned int n_col
= m_owner
->GetColumnCount();
2356 for (unsigned i
= 0; i
< n_col
; i
++)
2358 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2359 if (column
->GetModelColumn() == model_column
)
2361 view_column
= (int) i
;
2365 if (view_column
== -1)
2368 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2369 /*#define MAX_VIRTUAL_WIDTH 100000
2371 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2372 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2373 Refresh( true, &rect );
2380 GetOwner()->InvalidateColBestWidth(view_column
);
2383 wxWindow
*parent
= GetParent();
2384 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2385 le
.SetEventObject(parent
);
2386 le
.SetModel(GetModel());
2388 le
.SetColumn(view_column
);
2389 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2390 parent
->ProcessWindowEvent(le
);
2395 bool wxDataViewMainWindow::Cleared()
2398 m_selection
.Clear();
2401 BuildTree( GetModel() );
2403 GetOwner()->InvalidateColBestWidths();
2409 void wxDataViewMainWindow::UpdateDisplay()
2412 m_underMouse
= NULL
;
2415 void wxDataViewMainWindow::OnInternalIdle()
2417 wxWindow::OnInternalIdle();
2421 RecalculateDisplay();
2426 void wxDataViewMainWindow::RecalculateDisplay()
2428 wxDataViewModel
*model
= GetModel();
2435 int width
= GetEndOfLastCol();
2436 int height
= GetLineStart( GetRowCount() );
2438 SetVirtualSize( width
, height
);
2439 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2444 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2446 m_underMouse
= NULL
;
2448 wxWindow::ScrollWindow( dx
, dy
, rect
);
2450 if (GetOwner()->m_headerArea
)
2451 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2454 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2456 m_underMouse
= NULL
;
2459 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2460 int sy
= GetLineStart( rows
)/y
;
2464 wxRect rect
= GetClientRect();
2468 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2469 for (x_start
= 0; colnum
< column
; colnum
++)
2471 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2472 if (col
->IsHidden())
2473 continue; // skip it!
2475 w
= col
->GetWidth();
2479 int x_end
= x_start
+ w
;
2480 xe
= xx
+ rect
.width
;
2483 sx
= ( xx
+ x_end
- xe
)/x
;
2490 m_owner
->Scroll( sx
, sy
);
2493 int wxDataViewMainWindow::GetCountPerPage() const
2495 wxSize size
= GetClientSize();
2496 return size
.y
/ m_lineHeight
;
2499 int wxDataViewMainWindow::GetEndOfLastCol() const
2503 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2505 const wxDataViewColumn
*c
=
2506 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2509 width
+= c
->GetWidth();
2514 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2518 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2520 return GetLineAt( y
);
2523 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2525 wxSize client_size
= GetClientSize();
2526 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2527 &client_size
.x
, &client_size
.y
);
2529 // we should deal with the pixel here
2530 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2532 return wxMin( GetRowCount()-1, row
);
2535 unsigned int wxDataViewMainWindow::GetRowCount()
2537 if ( m_count
== -1 )
2539 m_count
= RecalculateCount();
2545 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2552 void wxDataViewMainWindow::SelectAllRows( bool on
)
2559 m_selection
.Clear();
2560 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2561 m_selection
.Add( i
);
2566 unsigned int first_visible
= GetFirstVisibleRow();
2567 unsigned int last_visible
= GetLastVisibleRow();
2569 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2571 unsigned int row
= m_selection
[i
];
2572 if ((row
>= first_visible
) && (row
<= last_visible
))
2575 m_selection
.Clear();
2579 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2581 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2585 m_selection
.Add( row
);
2593 m_selection
.Remove( row
);
2599 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2603 unsigned int tmp
= from
;
2609 for (i
= from
; i
<= to
; i
++)
2611 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2614 m_selection
.Add( i
);
2619 m_selection
.Remove( i
);
2622 RefreshRows( from
, to
);
2625 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2627 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2629 int n
= aSelections
[i
];
2631 m_selection
.Add( n
);
2636 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2638 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2639 m_selection
.Add( row
);
2641 m_selection
.Remove( row
);
2645 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2647 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2650 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2652 wxWindow
*parent
= GetParent();
2653 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2655 le
.SetEventObject(parent
);
2656 le
.SetModel(GetModel());
2659 parent
->ProcessWindowEvent(le
);
2662 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2664 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2665 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2667 wxSize client_size
= GetClientSize();
2668 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2669 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2670 if (intersect_rect
.width
> 0)
2671 Refresh( true, &intersect_rect
);
2674 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2678 unsigned int tmp
= to
;
2683 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2684 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2686 wxSize client_size
= GetClientSize();
2687 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2688 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2689 if (intersect_rect
.width
> 0)
2690 Refresh( true, &intersect_rect
);
2693 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2695 wxSize client_size
= GetClientSize();
2696 int start
= GetLineStart( firstRow
);
2697 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2698 if (start
> client_size
.y
) return;
2700 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2702 Refresh( true, &rect
);
2705 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2709 rect
.y
= GetLineStart( row
);
2710 rect
.width
= GetEndOfLastCol();
2711 rect
.height
= GetLineHeight( row
);
2716 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2718 const wxDataViewModel
*model
= GetModel();
2720 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2722 // TODO make more efficient
2727 for (r
= 0; r
< row
; r
++)
2729 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2730 if (!node
) return start
;
2732 wxDataViewItem item
= node
->GetItem();
2734 unsigned int cols
= GetOwner()->GetColumnCount();
2736 int height
= m_lineHeight
;
2737 for (col
= 0; col
< cols
; col
++)
2739 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2740 if (column
->IsHidden())
2741 continue; // skip it!
2744 model
->IsContainer(item
) &&
2745 !model
->HasContainerColumns(item
))
2746 continue; // skip it!
2748 wxDataViewRenderer
*renderer
=
2749 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2750 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2752 height
= wxMax( height
, renderer
->GetSize().y
);
2762 return row
* m_lineHeight
;
2766 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2768 const wxDataViewModel
*model
= GetModel();
2770 // check for the easy case first
2771 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2772 return y
/ m_lineHeight
;
2774 // TODO make more efficient
2775 unsigned int row
= 0;
2776 unsigned int yy
= 0;
2779 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2782 // not really correct...
2783 return row
+ ((y
-yy
) / m_lineHeight
);
2786 wxDataViewItem item
= node
->GetItem();
2788 unsigned int cols
= GetOwner()->GetColumnCount();
2790 int height
= m_lineHeight
;
2791 for (col
= 0; col
< cols
; col
++)
2793 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2794 if (column
->IsHidden())
2795 continue; // skip it!
2798 model
->IsContainer(item
) &&
2799 !model
->HasContainerColumns(item
))
2800 continue; // skip it!
2802 wxDataViewRenderer
*renderer
=
2803 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2804 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2806 height
= wxMax( height
, renderer
->GetSize().y
);
2817 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2819 const wxDataViewModel
*model
= GetModel();
2821 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2823 wxASSERT( !IsVirtualList() );
2825 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2826 // wxASSERT( node );
2827 if (!node
) return m_lineHeight
;
2829 wxDataViewItem item
= node
->GetItem();
2831 int height
= m_lineHeight
;
2833 unsigned int cols
= GetOwner()->GetColumnCount();
2835 for (col
= 0; col
< cols
; col
++)
2837 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2838 if (column
->IsHidden())
2839 continue; // skip it!
2842 model
->IsContainer(item
) &&
2843 !model
->HasContainerColumns(item
))
2844 continue; // skip it!
2846 wxDataViewRenderer
*renderer
=
2847 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2848 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2850 height
= wxMax( height
, renderer
->GetSize().y
);
2857 return m_lineHeight
;
2862 class RowToTreeNodeJob
: public DoJob
2865 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2868 this->current
= current
;
2873 virtual int operator() ( wxDataViewTreeNode
* node
)
2876 if( current
== static_cast<int>(row
))
2882 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2884 current
+= node
->GetSubTreeCount();
2885 return DoJob::SKIP_SUBTREE
;
2891 // If the current node has only leaf children, we can find the
2892 // desired node directly. This can speed up finding the node
2893 // in some cases, and will have a very good effect for list views.
2894 if ( node
->HasChildren() &&
2895 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2897 const int index
= static_cast<int>(row
) - current
- 1;
2898 ret
= node
->GetChildNodes()[index
];
2902 return DoJob::CONTINUE
;
2906 wxDataViewTreeNode
* GetResult() const
2912 wxDataViewTreeNode
* ret
;
2913 wxDataViewTreeNode
* parent
;
2916 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2918 wxASSERT( !IsVirtualList() );
2920 RowToTreeNodeJob
job( row
, -2, m_root
);
2921 Walker( m_root
, job
);
2922 return job
.GetResult();
2925 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2927 if (IsVirtualList())
2929 return wxDataViewItem( wxUIntToPtr(row
+1) );
2933 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2934 return node
? node
->GetItem() : wxDataViewItem();
2939 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2940 const wxDataViewItem
& item
)
2942 wxWindow
*parent
= GetParent();
2943 wxDataViewEvent
le(type
, parent
->GetId());
2945 le
.SetEventObject(parent
);
2946 le
.SetModel(GetModel());
2949 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2952 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2957 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2961 if (!node
->HasChildren())
2964 return node
->IsOpen();
2967 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2972 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2976 if (!node
->HasChildren())
2982 void wxDataViewMainWindow::Expand( unsigned int row
)
2987 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2991 if (!node
->HasChildren())
2994 if (!node
->IsOpen())
2996 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
2998 // Vetoed by the event handler.
3004 // build the children of current node
3005 if( node
->GetChildNodes().empty() )
3008 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
3011 // By expanding the node all row indices that are currently in the selection list
3012 // and are greater than our node have become invalid. So we have to correct that now.
3013 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3014 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3016 const unsigned testRow
= m_selection
[i
];
3017 // all rows above us are not affected, so skip them
3021 m_selection
[i
] += rowAdjustment
;
3024 if(m_currentRow
> row
)
3025 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
3029 // Send the expanded event
3030 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3034 void wxDataViewMainWindow::Collapse(unsigned int row
)
3039 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3043 if (!node
->HasChildren())
3048 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
3050 // Vetoed by the event handler.
3054 // Find out if there are selected items below the current node.
3055 bool selectCollapsingRow
= false;
3056 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3057 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3058 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3060 const unsigned testRow
= m_selection
[i
];
3061 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3063 selectCollapsingRow
= true;
3064 // get out as soon as we have found a node that is selected
3071 // If the node to be closed has selected items the user won't see those any longer.
3072 // We select the collapsing node in this case.
3073 if(selectCollapsingRow
)
3075 SelectAllRows(false);
3076 ChangeCurrentRow(row
);
3077 SelectRow(row
, true);
3078 SendSelectionChangedEvent(GetItemByRow(row
));
3082 // if there were no selected items below our node we still need to "fix" the
3083 // selection list to adjust for the changing of the row indices.
3084 // We actually do the opposite of what we are doing in Expand().
3085 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3087 const unsigned testRow
= m_selection
[i
];
3088 // all rows above us are not affected, so skip them
3092 m_selection
[i
] -= rowAdjustment
;
3095 // if the "current row" is being collapsed away we change it to the current row ;-)
3096 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3097 ChangeCurrentRow(row
);
3098 else if(m_currentRow
> row
)
3099 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3104 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3108 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3110 const wxDataViewModel
* model
= GetModel();
3117 // Compose the parent-chain for the item we are looking for
3118 wxVector
<wxDataViewItem
> parentChain
;
3119 wxDataViewItem
it( item
);
3122 parentChain
.push_back(it
);
3123 it
= model
->GetParent(it
);
3126 // Find the item along the parent-chain.
3127 // This algorithm is designed to speed up the node-finding method
3128 wxDataViewTreeNode
* node
= m_root
;
3129 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3131 if( node
->HasChildren() )
3133 if( node
->GetChildNodes().empty() )
3135 // Even though the item is a container, it doesn't have any
3136 // child nodes in the control's representation yet. We have
3137 // to realize its subtree now.
3139 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3142 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3145 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3147 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3148 if (currentNode
->GetItem() == parentChain
[iter
])
3150 if (currentNode
->GetItem() == item
)
3170 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3171 wxDataViewColumn
* &column
)
3173 wxDataViewColumn
*col
= NULL
;
3174 unsigned int cols
= GetOwner()->GetColumnCount();
3175 unsigned int colnum
= 0;
3177 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3178 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3180 col
= GetOwner()->GetColumnAt(colnum
);
3181 if (col
->IsHidden())
3182 continue; // skip it!
3184 unsigned int w
= col
->GetWidth();
3185 if (x_start
+w
>= (unsigned int)x
)
3192 item
= GetItemByRow( GetLineAt( y
) );
3195 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3196 const wxDataViewColumn
* column
)
3201 unsigned int cols
= GetOwner()->GetColumnCount();
3202 // If column is null the loop will compute the combined width of all columns.
3203 // Otherwise, it will compute the x position of the column we are looking for.
3204 for (unsigned int i
= 0; i
< cols
; i
++)
3206 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3211 if (col
->IsHidden())
3212 continue; // skip it!
3214 xpos
+= col
->GetWidth();
3215 width
+= col
->GetWidth();
3220 // If we have a column, we need can get its width directly.
3221 if(column
->IsHidden())
3224 width
= column
->GetWidth();
3229 // If we have no column, we reset the x position back to zero.
3233 // we have to take an expander column into account and compute its indentation
3234 // to get the correct x position where the actual text is
3236 int row
= GetRowByItem(item
);
3238 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3240 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3241 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3242 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3245 wxRect
itemRect( xpos
+ indent
,
3246 GetLineStart( row
),
3248 GetLineHeight( row
) );
3250 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3251 &itemRect
.x
, &itemRect
.y
);
3256 int wxDataViewMainWindow::RecalculateCount()
3258 if (IsVirtualList())
3260 wxDataViewVirtualListModel
*list_model
=
3261 (wxDataViewVirtualListModel
*) GetModel();
3263 return list_model
->GetCount();
3267 return m_root
->GetSubTreeCount();
3271 class ItemToRowJob
: public DoJob
3274 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3281 // Maybe binary search will help to speed up this process
3282 virtual int operator() ( wxDataViewTreeNode
* node
)
3285 if( node
->GetItem() == item
)
3290 if( node
->GetItem() == *m_iter
)
3293 return DoJob::CONTINUE
;
3297 ret
+= node
->GetSubTreeCount();
3298 return DoJob::SKIP_SUBTREE
;
3303 // the row number is begin from zero
3304 int GetResult() const
3308 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3309 wxDataViewItem item
;
3314 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3316 const wxDataViewModel
* model
= GetModel();
3320 if (IsVirtualList())
3322 return wxPtrToUInt( item
.GetID() ) -1;
3329 // Compose the parent-chain of the item we are looking for
3330 wxVector
<wxDataViewItem
> parentChain
;
3331 wxDataViewItem
it( item
);
3334 parentChain
.push_back(it
);
3335 it
= model
->GetParent(it
);
3338 // add an 'invalid' item to represent our 'invisible' root node
3339 parentChain
.push_back(wxDataViewItem());
3341 // the parent chain was created by adding the deepest parent first.
3342 // so if we want to start at the root node, we have to iterate backwards through the vector
3343 ItemToRowJob
job( item
, parentChain
.rbegin() );
3344 Walker( m_root
, job
);
3345 return job
.GetResult();
3349 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3350 wxDataViewTreeNode
* node
)
3352 if( !model
->IsContainer( item
) )
3355 wxDataViewItemArray children
;
3356 unsigned int num
= model
->GetChildren( item
, children
);
3358 for ( unsigned int index
= 0; index
< num
; index
++ )
3360 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3362 if( model
->IsContainer(children
[index
]) )
3363 n
->SetHasChildren( true );
3365 node
->InsertChild(n
, index
);
3368 wxASSERT( node
->IsOpen() );
3369 node
->ChangeSubTreeCount(+num
);
3372 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3376 if (GetModel()->IsVirtualListModel())
3382 m_root
= wxDataViewTreeNode::CreateRootNode();
3384 // First we define a invalid item to fetch the top-level elements
3385 wxDataViewItem item
;
3387 BuildTreeHelper( model
, item
, m_root
);
3391 void wxDataViewMainWindow::DestroyTree()
3393 if (!IsVirtualList())
3401 wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem
& item
, wxDataViewCellMode mode
)
3403 // Edit the current column editable in 'mode'. If no column is focused
3404 // (typically because the user has full row selected), try to find the
3405 // first editable column (this would typically be a checkbox for
3406 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3407 // focus on the checkbox column; or on the only editable text column).
3409 wxDataViewColumn
*candidate
= m_currentCol
;
3412 candidate
->GetRenderer()->GetMode() != mode
&&
3413 !m_currentColSetByKeyboard
)
3415 // If current column was set by mouse to something not editable (in
3416 // 'mode') and the user pressed Space/F2 to edit it, treat the
3417 // situation as if there was whole-row focus, because that's what is
3418 // visually indicated and the mouse click could very well be targeted
3419 // on the row rather than on an individual cell.
3421 // But if it was done by keyboard, respect that even if the column
3422 // isn't editable, because focus is visually on that column and editing
3423 // something else would be surprising.
3429 const unsigned cols
= GetOwner()->GetColumnCount();
3430 for ( unsigned i
= 0; i
< cols
; i
++ )
3432 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3433 if ( c
->IsHidden() )
3436 if ( c
->GetRenderer()->GetMode() == mode
)
3444 // If on container item without columns, only the expander column
3445 // may be directly editable:
3447 GetOwner()->GetExpanderColumn() != candidate
&&
3448 GetModel()->IsContainer(item
) &&
3449 !GetModel()->HasContainerColumns(item
) )
3451 candidate
= GetOwner()->GetExpanderColumn();
3457 if ( candidate
->GetRenderer()->GetMode() != mode
)
3463 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3465 wxWindow
* const parent
= GetParent();
3467 // propagate the char event upwards
3468 wxKeyEvent
eventForParent(event
);
3469 eventForParent
.SetEventObject(parent
);
3470 if ( parent
->ProcessWindowEvent(eventForParent
) )
3473 if ( parent
->HandleAsNavigationKey(event
) )
3476 // no item -> nothing to do
3477 if (!HasCurrentRow())
3483 // don't use m_linesPerPage directly as it might not be computed yet
3484 const int pageSize
= GetCountPerPage();
3485 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3487 switch ( event
.GetKeyCode() )
3491 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3492 // it. Only if that event is not handled do we activate column renderer (which
3493 // is normally done by Space).
3495 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3497 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3500 le
.SetEventObject(parent
);
3501 le
.SetModel(GetModel());
3503 if ( parent
->ProcessWindowEvent(le
) )
3505 // else: fall through to WXK_SPACE handling
3510 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3512 // Activate the current activatable column. If not column is focused (typically
3513 // because the user has full row selected), try to find the first activatable
3514 // column (this would typically be a checkbox and we don't want to force the user
3515 // to set focus on the checkbox column).
3516 wxDataViewColumn
*activatableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_ACTIVATABLE
);
3518 if ( activatableCol
)
3520 const unsigned colIdx
= activatableCol
->GetModelColumn();
3521 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3523 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3524 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3525 cell
->WXOnActivate(cell_rect
, GetModel(), item
, colIdx
);
3531 if ( m_currentRow
> 0 )
3532 OnVerticalNavigation( m_currentRow
- 1, event
);
3536 if ( m_currentRow
+ 1 < GetRowCount() )
3537 OnVerticalNavigation( m_currentRow
+ 1, event
);
3539 // Add the process for tree expanding/collapsing
3551 OnVerticalNavigation( GetRowCount() - 1, event
);
3556 OnVerticalNavigation( 0, event
);
3561 int steps
= pageSize
- 1;
3562 int index
= m_currentRow
- steps
;
3566 OnVerticalNavigation( index
, event
);
3572 int steps
= pageSize
- 1;
3573 unsigned int index
= m_currentRow
+ steps
;
3574 unsigned int count
= GetRowCount();
3575 if ( index
>= count
)
3578 OnVerticalNavigation( index
, event
);
3584 if( !m_selection
.empty() )
3586 // Mimic Windows 7 behavior: edit the item that has focus
3587 // if it is selected and the first selected item if focus
3588 // is out of selection.
3590 if ( m_selection
.Index(m_currentRow
) != wxNOT_FOUND
)
3593 sel
= m_selection
[0];
3596 const wxDataViewItem item
= GetItemByRow(sel
);
3598 // Edit the current column. If not column is focused
3599 // (typically because the user has full row selected), try
3600 // to find the first editable column.
3601 wxDataViewColumn
*editableCol
= FindColumnForEditing(item
, wxDATAVIEW_CELL_EDITABLE
);
3604 GetOwner()->StartEditor(item
, GetOwner()->GetColumnIndex(editableCol
));
3614 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3616 wxCHECK_RET( newCurrent
< GetRowCount(),
3617 wxT("invalid item index in OnVerticalNavigation()") );
3619 // if there is no selection, we cannot move it anywhere
3620 if (!HasCurrentRow())
3623 unsigned int oldCurrent
= m_currentRow
;
3625 // in single selection we just ignore Shift as we can't select several
3627 if ( event
.ShiftDown() && !IsSingleSel() )
3629 RefreshRow( oldCurrent
);
3631 ChangeCurrentRow( newCurrent
);
3633 // select all the items between the old and the new one
3634 if ( oldCurrent
> newCurrent
)
3636 newCurrent
= oldCurrent
;
3637 oldCurrent
= m_currentRow
;
3640 SelectRows( oldCurrent
, newCurrent
, true );
3641 if (oldCurrent
!=newCurrent
)
3642 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3646 RefreshRow( oldCurrent
);
3648 // all previously selected items are unselected unless ctrl is held
3649 if ( !event
.ControlDown() )
3650 SelectAllRows(false);
3652 ChangeCurrentRow( newCurrent
);
3654 if ( !event
.ControlDown() )
3656 SelectRow( m_currentRow
, true );
3657 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3660 RefreshRow( m_currentRow
);
3663 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3666 void wxDataViewMainWindow::OnLeftKey()
3670 TryAdvanceCurrentColumn(NULL
, /*forward=*/false);
3674 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3676 if ( TryAdvanceCurrentColumn(node
, /*forward=*/false) )
3679 // Because TryAdvanceCurrentColumn() return false, we are at the first
3680 // column or using whole-row selection. In this situation, we can use
3681 // the standard TreeView handling of the left key.
3682 if (node
->HasChildren() && node
->IsOpen())
3684 Collapse(m_currentRow
);
3688 // if the node is already closed, we move the selection to its parent
3689 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3693 int parent
= GetRowByItem( parent_node
->GetItem() );
3696 unsigned int row
= m_currentRow
;
3697 SelectRow( row
, false);
3698 SelectRow( parent
, true );
3699 ChangeCurrentRow( parent
);
3700 GetOwner()->EnsureVisible( parent
, -1 );
3701 SendSelectionChangedEvent( parent_node
->GetItem() );
3708 void wxDataViewMainWindow::OnRightKey()
3712 TryAdvanceCurrentColumn(NULL
, /*forward=*/true);
3716 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3718 if ( node
->HasChildren() )
3720 if ( !node
->IsOpen() )
3722 Expand( m_currentRow
);
3726 // if the node is already open, we move the selection to the first child
3727 unsigned int row
= m_currentRow
;
3728 SelectRow( row
, false );
3729 SelectRow( row
+ 1, true );
3730 ChangeCurrentRow( row
+ 1 );
3731 GetOwner()->EnsureVisible( row
+ 1, -1 );
3732 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3737 TryAdvanceCurrentColumn(node
, /*forward=*/true);
3742 bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode
*node
, bool forward
)
3744 if ( GetOwner()->GetColumnCount() == 0 )
3747 if ( !m_useCellFocus
)
3752 // navigation shouldn't work in branch nodes without other columns:
3753 if ( node
->HasChildren() && !GetModel()->HasContainerColumns(node
->GetItem()) )
3757 if ( m_currentCol
== NULL
|| !m_currentColSetByKeyboard
)
3761 m_currentCol
= GetOwner()->GetColumnAt(1);
3762 m_currentColSetByKeyboard
= true;
3763 RefreshRow(m_currentRow
);
3770 int idx
= GetOwner()->GetColumnIndex(m_currentCol
) + (forward
? +1 : -1);
3772 if ( idx
>= (int)GetOwner()->GetColumnCount() )
3777 // We are going to the left of the second column. Reset to whole-row
3778 // focus (which means first column would be edited).
3779 m_currentCol
= NULL
;
3780 RefreshRow(m_currentRow
);
3784 m_currentCol
= GetOwner()->GetColumnAt(idx
);
3785 m_currentColSetByKeyboard
= true;
3786 RefreshRow(m_currentRow
);
3790 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3792 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3794 // let the base handle mouse wheel events.
3799 // set the focus to ourself if any of the mouse buttons are pressed
3800 if(event
.ButtonDown() && !HasFocus())
3803 int x
= event
.GetX();
3804 int y
= event
.GetY();
3805 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3806 wxDataViewColumn
*col
= NULL
;
3809 unsigned int cols
= GetOwner()->GetColumnCount();
3811 for (i
= 0; i
< cols
; i
++)
3813 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3815 continue; // skip it!
3817 if (x
< xpos
+ c
->GetWidth())
3822 xpos
+= c
->GetWidth();
3825 wxDataViewModel
* const model
= GetModel();
3827 const unsigned int current
= GetLineAt( y
);
3828 const wxDataViewItem item
= GetItemByRow(current
);
3830 // Handle right clicking here, before everything else as context menu
3831 // events should be sent even when we click outside of any item, unlike all
3833 if (event
.RightUp())
3835 wxWindow
*parent
= GetParent();
3836 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3837 le
.SetEventObject(parent
);
3840 if ( item
.IsOk() && col
)
3843 le
.SetColumn( col
->GetModelColumn() );
3844 le
.SetDataViewColumn( col
);
3847 model
->GetValue( value
, item
, col
->GetModelColumn() );
3851 parent
->ProcessWindowEvent(le
);
3861 wxDataViewRenderer
*cell
= col
->GetRenderer();
3862 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3864 // Unselect all if below the last row ?
3869 wxDataViewColumn
* const
3870 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3872 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3873 // button) and also determine the offset of the real cell start, skipping
3874 // the indentation and the expander itself.
3875 bool hoverOverExpander
= false;
3877 if ((!IsList()) && (expander
== col
))
3879 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3881 int indent
= node
->GetIndentLevel();
3882 itemOffset
= GetOwner()->GetIndent()*indent
;
3884 if ( node
->HasChildren() )
3886 // we make the rectangle we are looking in a bit bigger than the actual
3887 // visual expander so the user can hit that little thing reliably
3888 wxRect
rect(itemOffset
,
3889 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3890 m_lineHeight
, m_lineHeight
);
3892 if( rect
.Contains(x
, y
) )
3894 // So the mouse is over the expander
3895 hoverOverExpander
= true;
3896 if (m_underMouse
&& m_underMouse
!= node
)
3898 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3899 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3901 if (m_underMouse
!= node
)
3903 // wxLogMessage("Do the row: %d", current);
3904 RefreshRow(current
);
3906 m_underMouse
= node
;
3910 // Account for the expander as well, even if this item doesn't have it,
3911 // its parent does so it still counts for the offset.
3912 itemOffset
+= m_lineHeight
;
3914 if (!hoverOverExpander
)
3916 if (m_underMouse
!= NULL
)
3918 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3919 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3920 m_underMouse
= NULL
;
3924 #if wxUSE_DRAG_AND_DROP
3925 if (event
.Dragging())
3927 if (m_dragCount
== 0)
3929 // we have to report the raw, physical coords as we want to be
3930 // able to call HitTest(event.m_pointDrag) from the user code to
3931 // get the item being dragged
3932 m_dragStart
= event
.GetPosition();
3937 if (m_dragCount
!= 3)
3940 if (event
.LeftIsDown())
3942 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3943 &m_dragStart
.x
, &m_dragStart
.y
);
3944 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3945 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
3947 // Notify cell about drag
3948 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3949 event
.SetEventObject( m_owner
);
3950 event
.SetItem( itemDragged
);
3951 event
.SetModel( model
);
3952 if (!m_owner
->HandleWindowEvent( event
))
3955 if (!event
.IsAllowed())
3958 wxDataObject
*obj
= event
.GetDataObject();
3962 wxDataViewDropSource
drag( this, drag_item_row
);
3963 drag
.SetData( *obj
);
3964 /* wxDragResult res = */ drag
.DoDragDrop();
3973 #endif // wxUSE_DRAG_AND_DROP
3975 bool simulateClick
= false;
3977 if (event
.ButtonDClick())
3979 m_renameTimer
->Stop();
3980 m_lastOnSame
= false;
3983 bool ignore_other_columns
=
3984 ((expander
!= col
) &&
3985 (model
->IsContainer(item
)) &&
3986 (!model
->HasContainerColumns(item
)));
3988 if (event
.LeftDClick())
3990 if(hoverOverExpander
)
3992 // a double click on the expander will be converted into a "simulated" normal click
3993 simulateClick
= true;
3995 else if ( current
== m_lineLastClicked
)
3997 bool activated
= false;
3999 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
4001 const unsigned colIdx
= col
->GetModelColumn();
4003 cell
->PrepareForItem(model
, item
, colIdx
);
4005 wxRect
cell_rect( xpos
, GetLineStart( current
),
4006 col
->GetWidth(), GetLineHeight( current
) );
4007 activated
= cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
4012 wxWindow
*parent
= GetParent();
4013 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
4015 le
.SetColumn( col
->GetModelColumn() );
4016 le
.SetDataViewColumn( col
);
4017 le
.SetEventObject(parent
);
4018 le
.SetModel(GetModel());
4020 parent
->ProcessWindowEvent(le
);
4026 // The first click was on another item, so don't interpret this as
4027 // a double click, but as a simple click instead
4028 simulateClick
= true;
4032 if (event
.LeftUp() && !hoverOverExpander
)
4034 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
4036 // select single line
4037 SelectAllRows( false );
4038 SelectRow( m_lineSelectSingleOnUp
, true );
4039 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
4042 // If the user click the expander, we do not do editing even if the column
4043 // with expander are editable
4044 if (m_lastOnSame
&& !ignore_other_columns
)
4046 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
4047 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
4049 m_renameTimer
->Start( 100, true );
4053 m_lastOnSame
= false;
4054 m_lineSelectSingleOnUp
= (unsigned int)-1;
4056 else if(!event
.LeftUp())
4058 // This is necessary, because after a DnD operation in
4059 // from and to ourself, the up event is swallowed by the
4060 // DnD code. So on next non-up event (which means here and
4061 // now) m_lineSelectSingleOnUp should be reset.
4062 m_lineSelectSingleOnUp
= (unsigned int)-1;
4065 if (event
.RightDown())
4067 m_lineBeforeLastClicked
= m_lineLastClicked
;
4068 m_lineLastClicked
= current
;
4070 // If the item is already selected, do not update the selection.
4071 // Multi-selections should not be cleared if a selected item is clicked.
4072 if (!IsRowSelected(current
))
4074 SelectAllRows(false);
4075 const unsigned oldCurrent
= m_currentRow
;
4076 ChangeCurrentRow(current
);
4077 SelectRow(m_currentRow
,true);
4078 RefreshRow(oldCurrent
);
4079 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4082 else if (event
.MiddleDown())
4086 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
4088 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
4090 // hoverOverExpander being true tells us that our node must be
4091 // valid and have children.
4092 // So we don't need any extra checks.
4093 if( node
->IsOpen() )
4098 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
4100 m_lineBeforeLastClicked
= m_lineLastClicked
;
4101 m_lineLastClicked
= current
;
4103 unsigned int oldCurrentRow
= m_currentRow
;
4104 bool oldWasSelected
= IsRowSelected(m_currentRow
);
4106 bool cmdModifierDown
= event
.CmdDown();
4107 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
4109 if ( IsSingleSel() || !IsRowSelected(current
) )
4111 SelectAllRows( false );
4112 ChangeCurrentRow(current
);
4113 SelectRow(m_currentRow
,true);
4114 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
4116 else // multi sel & current is highlighted & no mod keys
4118 m_lineSelectSingleOnUp
= current
;
4119 ChangeCurrentRow(current
); // change focus
4122 else // multi sel & either ctrl or shift is down
4124 if (cmdModifierDown
)
4126 ChangeCurrentRow(current
);
4127 ReverseRowSelection(m_currentRow
);
4128 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
4130 else if (event
.ShiftDown())
4132 ChangeCurrentRow(current
);
4134 unsigned int lineFrom
= oldCurrentRow
,
4137 if ( lineTo
< lineFrom
)
4140 lineFrom
= m_currentRow
;
4143 SelectRows(lineFrom
, lineTo
, true);
4144 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
4146 else // !ctrl, !shift
4148 // test in the enclosing if should make it impossible
4149 wxFAIL_MSG( wxT("how did we get here?") );
4153 if (m_currentRow
!= oldCurrentRow
)
4154 RefreshRow( oldCurrentRow
);
4156 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
4158 // Update selection here...
4160 m_currentColSetByKeyboard
= false;
4162 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
4163 (current
== oldCurrentRow
)) && oldWasSelected
;
4165 // Call LeftClick after everything else as under GTK+
4166 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
4168 // notify cell about click
4169 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
4171 wxRect
cell_rect( xpos
+ itemOffset
,
4172 GetLineStart( current
),
4173 col
->GetWidth() - itemOffset
,
4174 GetLineHeight( current
) );
4176 // Report position relative to the cell's custom area, i.e.
4177 // no the entire space as given by the control but the one
4178 // used by the renderer after calculation of alignment etc.
4180 // adjust the rectangle ourselves to account for the alignment
4181 wxRect rectItem
= cell_rect
;
4182 const int align
= cell
->GetAlignment();
4183 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
4185 const wxSize size
= cell
->GetSize();
4187 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
4189 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
4190 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
4191 else if ( align
& wxALIGN_RIGHT
)
4192 rectItem
.x
+= cell_rect
.width
- size
.x
;
4193 // else: wxALIGN_LEFT is the default
4196 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
4198 if ( align
& wxALIGN_CENTER_VERTICAL
)
4199 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
4200 else if ( align
& wxALIGN_BOTTOM
)
4201 rectItem
.y
+= cell_rect
.height
- size
.y
;
4202 // else: wxALIGN_TOP is the default
4206 wxPoint
pos( event
.GetPosition() );
4207 pos
.x
-= rectItem
.x
;
4208 pos
.y
-= rectItem
.y
;
4210 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
4212 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
4213 model
, item
, col
->GetModelColumn());
4218 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
4222 if (HasCurrentRow())
4228 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
4232 if (HasCurrentRow())
4238 void wxDataViewMainWindow::OnColumnsCountChanged()
4240 int editableCount
= 0;
4242 const unsigned cols
= GetOwner()->GetColumnCount();
4243 for ( unsigned i
= 0; i
< cols
; i
++ )
4245 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
4246 if ( c
->IsHidden() )
4248 if ( c
->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT
)
4252 m_useCellFocus
= (editableCount
> 1);
4257 //-----------------------------------------------------------------------------
4259 //-----------------------------------------------------------------------------
4261 WX_DEFINE_LIST(wxDataViewColumnList
)
4263 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
4264 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
4265 EVT_SIZE(wxDataViewCtrl::OnSize
)
4268 wxDataViewCtrl::~wxDataViewCtrl()
4271 GetModel()->RemoveNotifier( m_notifier
);
4274 m_colsBestWidths
.clear();
4277 void wxDataViewCtrl::Init()
4279 m_cols
.DeleteContents(true);
4282 // No sorting column at start
4283 m_sortingColumnIdx
= wxNOT_FOUND
;
4285 m_headerArea
= NULL
;
4287 m_colsDirty
= false;
4290 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4295 const wxValidator
& validator
,
4296 const wxString
& name
)
4298 // if ( (style & wxBORDER_MASK) == 0)
4299 // style |= wxBORDER_SUNKEN;
4303 if (!wxControl::Create( parent
, id
, pos
, size
,
4304 style
| wxScrolledWindowStyle
, validator
, name
))
4307 SetInitialSize(size
);
4310 MacSetClipChildren( true );
4313 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4315 // We use the cursor keys for moving the selection, not scrolling, so call
4316 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4317 // keyboard events forwarded to us from wxListMainWindow.
4318 DisableKeyboardScrolling();
4320 if (HasFlag(wxDV_NO_HEADER
))
4321 m_headerArea
= NULL
;
4323 m_headerArea
= new wxDataViewHeaderWindow(this);
4325 SetTargetWindow( m_clientArea
);
4327 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4329 sizer
->Add( m_headerArea
, 0, wxGROW
);
4330 sizer
->Add( m_clientArea
, 1, wxGROW
);
4336 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4338 return wxBORDER_THEME
;
4342 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4346 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4349 // we need to process arrows ourselves for scrolling
4350 if ( nMsg
== WM_GETDLGCODE
)
4352 rc
|= DLGC_WANTARROWS
;
4360 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4362 wxSize newsize
= size
;
4363 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4364 newsize
.y
-= m_headerArea
->GetSize().y
;
4369 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4371 // We need to override OnSize so that our scrolled
4372 // window a) does call Layout() to use sizers for
4373 // positioning the controls but b) does not query
4374 // the sizer for their size and use that for setting
4375 // the scrollable area as set that ourselves by
4376 // calling SetScrollbar() further down.
4382 // We must redraw the headers if their height changed. Normally this
4383 // shouldn't happen as the control shouldn't let itself be resized beneath
4384 // its minimal height but avoid the display artefacts that appear if it
4385 // does happen, e.g. because there is really not enough vertical space.
4386 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4387 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4389 m_headerArea
->Refresh();
4393 void wxDataViewCtrl::SetFocus()
4396 m_clientArea
->SetFocus();
4399 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4401 if (!wxDataViewCtrlBase::AssociateModel( model
))
4404 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4406 model
->AddNotifier( m_notifier
);
4408 m_clientArea
->DestroyTree();
4410 m_clientArea
->BuildTree(model
);
4412 m_clientArea
->UpdateDisplay();
4417 #if wxUSE_DRAG_AND_DROP
4419 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4421 return m_clientArea
->EnableDragSource( format
);
4424 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4426 return m_clientArea
->EnableDropTarget( format
);
4429 #endif // wxUSE_DRAG_AND_DROP
4431 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4433 if (!wxDataViewCtrlBase::AppendColumn(col
))
4436 m_cols
.Append( col
);
4437 m_colsBestWidths
.push_back(0);
4438 OnColumnsCountChanged();
4442 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4444 if (!wxDataViewCtrlBase::PrependColumn(col
))
4447 m_cols
.Insert( col
);
4448 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4449 OnColumnsCountChanged();
4453 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4455 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4458 m_cols
.Insert( pos
, col
);
4459 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4460 OnColumnsCountChanged();
4464 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4467 m_headerArea
->UpdateColumn(idx
);
4469 m_clientArea
->UpdateDisplay();
4472 void wxDataViewCtrl::OnColumnsCountChanged()
4475 m_headerArea
->SetColumnCount(GetColumnCount());
4477 m_clientArea
->OnColumnsCountChanged();
4480 void wxDataViewCtrl::DoSetExpanderColumn()
4482 m_clientArea
->UpdateDisplay();
4485 void wxDataViewCtrl::DoSetIndent()
4487 m_clientArea
->UpdateDisplay();
4490 unsigned int wxDataViewCtrl::GetColumnCount() const
4492 return m_cols
.GetCount();
4495 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4497 if ( !m_clientArea
)
4500 m_clientArea
->SetRowHeight(lineHeight
);
4505 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4510 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4512 // columns can't be reordered if there is no header window which allows
4514 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4517 return GetColumn(idx
);
4520 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4522 const unsigned count
= m_cols
.size();
4523 for ( unsigned n
= 0; n
< count
; n
++ )
4525 if ( m_cols
[n
] == column
)
4532 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4534 if ( m_colsBestWidths
[idx
] != 0 )
4535 return m_colsBestWidths
[idx
];
4537 const int count
= m_clientArea
->GetRowCount();
4538 wxDataViewColumn
*column
= GetColumn(idx
);
4539 wxDataViewRenderer
*renderer
=
4540 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4542 class MaxWidthCalculator
4545 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4546 wxDataViewRenderer
*renderer
,
4547 const wxDataViewModel
*model
,
4550 m_clientArea(clientArea
),
4551 m_renderer(renderer
),
4557 void UpdateWithWidth(int width
)
4559 m_width
= wxMax(m_width
, width
);
4562 void UpdateWithRow(int row
)
4564 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4565 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4566 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4569 int GetMaxWidth() const { return m_width
; }
4573 wxDataViewMainWindow
*m_clientArea
;
4574 wxDataViewRenderer
*m_renderer
;
4575 const wxDataViewModel
*m_model
;
4579 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4580 GetModel(), column
->GetModelColumn());
4584 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4585 // Labels on native MSW header are indented on both sides
4587 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4588 calculator
.UpdateWithWidth(header_width
);
4591 // The code below deserves some explanation. For very large controls, we
4592 // simply can't afford to calculate sizes for all items, it takes too
4593 // long. So the best we can do is to check the first and the last N/2
4594 // items in the control for some sufficiently large N and calculate best
4595 // sizes from that. That can result in the calculated best width being too
4596 // small for some outliers, but it's better to get slightly imperfect
4597 // result than to wait several seconds after every update. To avoid highly
4598 // visible miscalculations, we also include all currently visible items
4599 // no matter what. Finally, the value of N is determined dynamically by
4600 // measuring how much time we spent on the determining item widths so far.
4603 int top_part_end
= count
;
4604 static const long CALC_TIMEOUT
= 20/*ms*/;
4605 // don't call wxStopWatch::Time() too often
4606 static const unsigned CALC_CHECK_FREQ
= 100;
4609 // use some hard-coded limit, that's the best we can do without timer
4610 int top_part_end
= wxMin(500, count
);
4611 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4615 for ( row
= 0; row
< top_part_end
; row
++ )
4618 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4619 timer
.Time() > CALC_TIMEOUT
)
4621 #endif // wxUSE_STOPWATCH
4622 calculator
.UpdateWithRow(row
);
4625 // row is the first unmeasured item now; that's our value of N/2
4631 // add bottom N/2 items now:
4632 const int bottom_part_start
= wxMax(row
, count
- row
);
4633 for ( row
= bottom_part_start
; row
< count
; row
++ )
4635 calculator
.UpdateWithRow(row
);
4638 // finally, include currently visible items in the calculation:
4639 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4640 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4641 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4643 first_visible
= wxMax(first_visible
, top_part_end
);
4644 last_visible
= wxMin(bottom_part_start
, last_visible
);
4646 for ( row
= first_visible
; row
< last_visible
; row
++ )
4648 calculator
.UpdateWithRow(row
);
4651 wxLogTrace("dataview",
4652 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4654 count
- bottom_part_start
,
4655 wxMax(0, last_visible
- first_visible
),
4659 int max_width
= calculator
.GetMaxWidth();
4660 if ( max_width
> 0 )
4661 max_width
+= 2 * PADDING_RIGHTLEFT
;
4663 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4667 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4668 unsigned int WXUNUSED(new_pos
))
4670 // do _not_ reorder m_cols elements here, they should always be in the
4671 // order in which columns were added, we only display the columns in
4673 m_clientArea
->UpdateDisplay();
4676 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4678 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4682 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4684 OnColumnsCountChanged();
4689 bool wxDataViewCtrl::ClearColumns()
4691 SetExpanderColumn(NULL
);
4693 m_colsBestWidths
.clear();
4694 OnColumnsCountChanged();
4698 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4700 m_colsBestWidths
[idx
] = 0;
4704 void wxDataViewCtrl::InvalidateColBestWidths()
4706 m_colsBestWidths
.clear();
4707 m_colsBestWidths
.resize(m_cols
.size());
4711 void wxDataViewCtrl::UpdateColWidths()
4713 if ( !m_headerArea
)
4716 const unsigned len
= m_colsBestWidths
.size();
4717 for ( unsigned i
= 0; i
< len
; i
++ )
4719 if ( m_colsBestWidths
[i
] == 0 )
4720 m_headerArea
->UpdateColumn(i
);
4724 void wxDataViewCtrl::OnInternalIdle()
4726 wxDataViewCtrlBase::OnInternalIdle();
4730 m_colsDirty
= false;
4735 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4738 unsigned int len
= GetColumnCount();
4739 for ( unsigned int i
= 0; i
< len
; i
++ )
4741 wxDataViewColumn
* col
= GetColumnAt(i
);
4748 // This returns the position in pixels which is not what we want.
4751 unsigned int len
= GetColumnCount();
4752 for ( unsigned int i
= 0; i
< len
; i
++ )
4754 wxDataViewColumn
* col
= GetColumnAt(i
);
4755 if (col
->IsHidden())
4757 ret
+= col
->GetWidth();
4760 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4768 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4770 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4771 : GetColumn(m_sortingColumnIdx
);
4774 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4776 return GetItemByRow(m_clientArea
->GetCurrentRow());
4779 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4781 const int row
= m_clientArea
->GetRowByItem(item
);
4783 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4784 if ( static_cast<unsigned>(row
) != oldCurrent
)
4786 m_clientArea
->ChangeCurrentRow(row
);
4787 m_clientArea
->RefreshRow(oldCurrent
);
4788 m_clientArea
->RefreshRow(row
);
4792 int wxDataViewCtrl::GetSelectedItemsCount() const
4794 return m_clientArea
->GetSelections().size();
4797 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4800 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4802 const size_t len
= selections
.size();
4803 for ( size_t i
= 0; i
< len
; i
++ )
4805 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4812 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4819 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4821 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4823 wxDataViewItem last_parent
;
4825 int len
= sel
.GetCount();
4826 for( int i
= 0; i
< len
; i
++ )
4828 wxDataViewItem item
= sel
[i
];
4829 wxDataViewItem parent
= GetModel()->GetParent( item
);
4832 if (parent
!= last_parent
)
4833 ExpandAncestors(item
);
4836 last_parent
= parent
;
4837 int row
= m_clientArea
->GetRowByItem( item
);
4839 selection
.Add( static_cast<unsigned int>(row
) );
4842 m_clientArea
->SetSelections( selection
);
4845 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4847 ExpandAncestors( item
);
4849 int row
= m_clientArea
->GetRowByItem( item
);
4852 // Unselect all rows before select another in the single select mode
4853 if (m_clientArea
->IsSingleSel())
4854 m_clientArea
->SelectAllRows(false);
4856 m_clientArea
->SelectRow(row
, true);
4858 // Also set focus to the selected item
4859 m_clientArea
->ChangeCurrentRow( row
);
4863 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4865 int row
= m_clientArea
->GetRowByItem( item
);
4867 m_clientArea
->SelectRow(row
, false);
4870 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4872 int row
= m_clientArea
->GetRowByItem( item
);
4875 return m_clientArea
->IsRowSelected(row
);
4880 void wxDataViewCtrl::SelectAll()
4882 m_clientArea
->SelectAllRows(true);
4885 void wxDataViewCtrl::UnselectAll()
4887 m_clientArea
->SelectAllRows(false);
4890 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4894 if( row
> (int) m_clientArea
->GetRowCount() )
4895 row
= m_clientArea
->GetRowCount();
4897 int first
= m_clientArea
->GetFirstVisibleRow();
4898 int last
= m_clientArea
->GetLastVisibleRow();
4900 m_clientArea
->ScrollTo( row
, column
);
4901 else if( row
> last
)
4902 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4904 m_clientArea
->ScrollTo( first
, column
);
4907 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4909 ExpandAncestors( item
);
4911 m_clientArea
->RecalculateDisplay();
4913 int row
= m_clientArea
->GetRowByItem(item
);
4916 if( column
== NULL
)
4917 EnsureVisible(row
, -1);
4919 EnsureVisible( row
, GetColumnIndex(column
) );
4924 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4925 wxDataViewColumn
* &column
) const
4927 m_clientArea
->HitTest(point
, item
, column
);
4930 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4931 const wxDataViewColumn
* column
) const
4933 return m_clientArea
->GetItemRect(item
, column
);
4936 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4938 return m_clientArea
->GetItemByRow( row
);
4941 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4943 return m_clientArea
->GetRowByItem( item
);
4946 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4948 ExpandAncestors( item
);
4950 int row
= m_clientArea
->GetRowByItem( item
);
4952 m_clientArea
->Expand(row
);
4955 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4957 int row
= m_clientArea
->GetRowByItem( item
);
4959 m_clientArea
->Collapse(row
);
4962 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4964 int row
= m_clientArea
->GetRowByItem( item
);
4966 return m_clientArea
->IsExpanded(row
);
4970 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4972 wxDataViewColumn
* col
= GetColumn( column
);
4976 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4977 if (renderer
->GetMode() != wxDATAVIEW_CELL_EDITABLE
)
4980 const wxRect itemRect
= GetItemRect(item
, col
);
4981 renderer
->StartEditing(item
, itemRect
);
4984 #endif // !wxUSE_GENERICDATAVIEWCTRL
4986 #endif // wxUSE_DATAVIEWCTRL