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
);
608 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
609 bool IsEmpty() { return GetRowCount() == 0; }
611 int GetCountPerPage() const;
612 int GetEndOfLastCol() const;
613 unsigned int GetFirstVisibleRow() const;
615 // I change this method to un const because in the tree view,
616 // the displaying number of the tree are changing along with the
617 // expanding/collapsing of the tree nodes
618 unsigned int GetLastVisibleRow();
619 unsigned int GetRowCount();
621 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
622 void SetSelections( const wxDataViewSelection
& sel
)
623 { m_selection
= sel
; UpdateDisplay(); }
624 void Select( const wxArrayInt
& aSelections
);
625 void SelectAllRows( bool on
);
626 void SelectRow( unsigned int row
, bool on
);
627 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
628 void ReverseRowSelection( unsigned int row
);
629 bool IsRowSelected( unsigned int row
);
630 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
632 void RefreshRow( unsigned int row
);
633 void RefreshRows( unsigned int from
, unsigned int to
);
634 void RefreshRowsAfter( unsigned int firstRow
);
636 // returns the colour to be used for drawing the rules
637 wxColour
GetRuleColour() const
639 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
642 wxRect
GetLineRect( unsigned int row
) const;
644 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
645 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
646 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
648 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
650 // Some useful functions for row and item mapping
651 wxDataViewItem
GetItemByRow( unsigned int row
) const;
652 int GetRowByItem( const wxDataViewItem
& item
) const;
654 // Methods for building the mapping tree
655 void BuildTree( wxDataViewModel
* model
);
657 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
658 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
660 void Expand( unsigned int row
);
661 void Collapse( unsigned int row
);
662 bool IsExpanded( unsigned int row
) const;
663 bool HasChildren( unsigned int row
) const;
665 #if wxUSE_DRAG_AND_DROP
666 bool EnableDragSource( const wxDataFormat
&format
);
667 bool EnableDropTarget( const wxDataFormat
&format
);
669 void RemoveDropHint();
670 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
671 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
672 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
674 #endif // wxUSE_DRAG_AND_DROP
677 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
678 // We did not need this temporarily
679 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
681 int RecalculateCount();
683 // Return false only if the event was vetoed by its handler.
684 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
686 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
689 wxDataViewCtrl
*m_owner
;
693 wxDataViewColumn
*m_currentCol
;
694 unsigned int m_currentRow
;
695 wxDataViewSelection m_selection
;
697 wxDataViewRenameTimer
*m_renameTimer
;
702 #if wxUSE_DRAG_AND_DROP
707 wxDataFormat m_dragFormat
;
710 wxDataFormat m_dropFormat
;
712 unsigned int m_dropHintLine
;
713 #endif // wxUSE_DRAG_AND_DROP
715 // for double click logic
716 unsigned int m_lineLastClicked
,
717 m_lineBeforeLastClicked
,
718 m_lineSelectSingleOnUp
;
720 // the pen used to draw horiz/vertical rules
723 // the pen used to draw the expander and the lines
726 // This is the tree structure of the model
727 wxDataViewTreeNode
* m_root
;
730 // This is the tree node under the cursor
731 wxDataViewTreeNode
* m_underMouse
;
734 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
735 DECLARE_EVENT_TABLE()
738 // ---------------------------------------------------------
739 // wxGenericDataViewModelNotifier
740 // ---------------------------------------------------------
742 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
745 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
746 { m_mainWindow
= mainWindow
; }
748 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
749 { return m_mainWindow
->ItemAdded( parent
, item
); }
750 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
751 { return m_mainWindow
->ItemDeleted( parent
, item
); }
752 virtual bool ItemChanged( const wxDataViewItem
& item
)
753 { return m_mainWindow
->ItemChanged(item
); }
754 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
755 { return m_mainWindow
->ValueChanged( item
, col
); }
756 virtual bool Cleared()
757 { return m_mainWindow
->Cleared(); }
758 virtual void Resort()
759 { m_mainWindow
->Resort(); }
761 wxDataViewMainWindow
*m_mainWindow
;
764 // ---------------------------------------------------------
765 // wxDataViewRenderer
766 // ---------------------------------------------------------
768 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
770 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
771 wxDataViewCellMode mode
,
773 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
777 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
781 wxDataViewRenderer::~wxDataViewRenderer()
786 wxDC
*wxDataViewRenderer::GetDC()
790 if (GetOwner() == NULL
)
792 if (GetOwner()->GetOwner() == NULL
)
794 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
800 void wxDataViewRenderer::SetAlignment( int align
)
805 int wxDataViewRenderer::GetAlignment() const
810 // ---------------------------------------------------------
811 // wxDataViewCustomRenderer
812 // ---------------------------------------------------------
814 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
816 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
817 wxDataViewCellMode mode
, int align
) :
818 wxDataViewRenderer( varianttype
, mode
, align
)
822 // ---------------------------------------------------------
823 // wxDataViewTextRenderer
824 // ---------------------------------------------------------
826 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
828 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
829 wxDataViewCellMode mode
, int align
) :
830 wxDataViewRenderer( varianttype
, mode
, align
)
834 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
836 m_text
= value
.GetString();
841 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
846 bool wxDataViewTextRenderer::HasEditorCtrl() const
851 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
852 wxRect labelRect
, const wxVariant
&value
)
854 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
855 wxPoint(labelRect
.x
,labelRect
.y
),
856 wxSize(labelRect
.width
,labelRect
.height
),
857 wxTE_PROCESS_ENTER
);
859 // select the text in the control an place the cursor at the end
860 ctrl
->SetInsertionPointEnd();
866 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
868 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
869 value
= text
->GetValue();
873 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
875 RenderText(m_text
, 0, rect
, dc
, state
);
879 wxSize
wxDataViewTextRenderer::GetSize() const
882 return GetTextExtent(m_text
);
884 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
887 // ---------------------------------------------------------
888 // wxDataViewBitmapRenderer
889 // ---------------------------------------------------------
891 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
893 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
894 wxDataViewCellMode mode
, int align
) :
895 wxDataViewRenderer( varianttype
, mode
, align
)
899 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
901 if (value
.GetType() == wxT("wxBitmap"))
903 if (value
.GetType() == wxT("wxIcon"))
909 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
914 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
917 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
918 else if (m_icon
.IsOk())
919 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
924 wxSize
wxDataViewBitmapRenderer::GetSize() const
927 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
928 else if (m_icon
.IsOk())
929 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
931 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
934 // ---------------------------------------------------------
935 // wxDataViewToggleRenderer
936 // ---------------------------------------------------------
938 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
940 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
941 wxDataViewCellMode mode
, int align
) :
942 wxDataViewRenderer( varianttype
, mode
, align
)
947 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
949 m_toggle
= value
.GetBool();
954 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
959 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
963 flags
|= wxCONTROL_CHECKED
;
964 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
965 GetEnabled() == false)
966 flags
|= wxCONTROL_DISABLED
;
968 // check boxes we draw must always have the same, standard size (if it's
969 // bigger than the cell size the checkbox will be truncated because the
970 // caller had set the clipping rectangle to prevent us from drawing outside
972 cell
.SetSize(GetSize());
974 wxRendererNative::Get().DrawCheckBox(
975 GetOwner()->GetOwner(),
983 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
985 wxDataViewModel
*model
,
986 const wxDataViewItem
& item
,
989 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
990 if (!wxRect(GetSize()).Contains(cursor
))
993 return WXOnActivate(cell
, model
, item
, col
);
996 bool wxDataViewToggleRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
997 wxDataViewModel
*model
,
998 const wxDataViewItem
& item
,
1001 if (model
->IsEnabled(item
, col
))
1003 model
->ChangeValue(!m_toggle
, item
, col
);
1010 wxSize
wxDataViewToggleRenderer::GetSize() const
1012 // the window parameter is not used by GetCheckBoxSize() so it's
1013 // safe to pass NULL
1014 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
1017 // ---------------------------------------------------------
1018 // wxDataViewProgressRenderer
1019 // ---------------------------------------------------------
1021 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
1023 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
1024 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1025 wxDataViewRenderer( varianttype
, mode
, align
)
1031 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
1033 m_value
= (long) value
;
1035 if (m_value
< 0) m_value
= 0;
1036 if (m_value
> 100) m_value
= 100;
1041 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1043 value
= (long) m_value
;
1048 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1050 // deflate the rect to leave a small border between bars in adjacent rows
1051 wxRect bar
= rect
.Deflate(0, 1);
1053 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1054 dc
->SetPen( *wxBLACK_PEN
);
1055 dc
->DrawRectangle( bar
);
1057 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1058 dc
->SetPen( *wxTRANSPARENT_PEN
);
1060 const wxDataViewItemAttr
& attr
= GetAttr();
1061 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1063 dc
->DrawRectangle( bar
);
1068 wxSize
wxDataViewProgressRenderer::GetSize() const
1070 return wxSize(40,12);
1073 // ---------------------------------------------------------
1074 // wxDataViewDateRenderer
1075 // ---------------------------------------------------------
1077 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
1079 #if wxUSE_DATE_RENDERER_POPUP
1081 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1084 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1085 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
1086 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
1091 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
1092 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1093 sizer
->Add( m_cal
, 1, wxGROW
);
1098 void OnCalendar( wxCalendarEvent
&event
);
1100 wxCalendarCtrl
*m_cal
;
1101 wxDataViewModel
*m_model
;
1103 const wxDataViewItem
& m_item
;
1106 virtual void OnDismiss()
1111 DECLARE_EVENT_TABLE()
1114 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1115 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1118 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1120 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1124 #endif // wxUSE_DATE_RENDERER_POPUP
1126 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1128 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1129 wxDataViewCellMode mode
, int align
) :
1130 wxDataViewRenderer( varianttype
, mode
, align
)
1134 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1136 m_date
= value
.GetDateTime();
1141 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1147 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1149 wxString tmp
= m_date
.FormatDate();
1150 RenderText( tmp
, 0, cell
, dc
, state
);
1154 wxSize
wxDataViewDateRenderer::GetSize() const
1156 return GetTextExtent(m_date
.FormatDate());
1159 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1160 wxDataViewModel
*model
,
1161 const wxDataViewItem
& item
,
1164 wxDateTime dtOld
= m_date
;
1166 #if wxUSE_DATE_RENDERER_POPUP
1167 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1168 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1169 wxPoint pos
= wxGetMousePosition();
1172 popup
->Popup( popup
->m_cal
);
1173 #else // !wxUSE_DATE_RENDERER_POPUP
1174 wxMessageBox(dtOld
.Format());
1175 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1180 // ---------------------------------------------------------
1181 // wxDataViewIconTextRenderer
1182 // ---------------------------------------------------------
1184 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1186 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1187 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1188 wxDataViewRenderer( varianttype
, mode
, align
)
1191 SetAlignment(align
);
1194 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1200 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1205 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1209 const wxIcon
& icon
= m_value
.GetIcon();
1212 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1213 xoffset
= icon
.GetWidth()+4;
1216 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1221 wxSize
wxDataViewIconTextRenderer::GetSize() const
1223 if (!m_value
.GetText().empty())
1225 wxSize size
= GetTextExtent(m_value
.GetText());
1227 if (m_value
.GetIcon().IsOk())
1228 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1231 return wxSize(80,20);
1234 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1236 wxDataViewIconText iconText
;
1239 wxString text
= iconText
.GetText();
1241 // adjust the label rect to take the width of the icon into account
1242 if (iconText
.GetIcon().IsOk())
1244 int w
= iconText
.GetIcon().GetWidth() + 4;
1246 labelRect
.width
-= w
;
1249 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1250 wxPoint(labelRect
.x
,labelRect
.y
),
1251 wxSize(labelRect
.width
,labelRect
.height
),
1252 wxTE_PROCESS_ENTER
);
1254 // select the text in the control an place the cursor at the end
1255 ctrl
->SetInsertionPointEnd();
1261 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1263 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1265 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1270 //-----------------------------------------------------------------------------
1271 // wxDataViewDropTarget
1272 //-----------------------------------------------------------------------------
1274 #if wxUSE_DRAG_AND_DROP
1276 class wxBitmapCanvas
: public wxWindow
1279 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1280 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1283 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1286 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1289 dc
.DrawBitmap( m_bitmap
, 0, 0);
1295 class wxDataViewDropSource
: public wxDropSource
1298 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1306 ~wxDataViewDropSource()
1311 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1313 wxPoint pos
= wxGetMousePosition();
1317 int liney
= m_win
->GetLineStart( m_row
);
1319 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1320 m_win
->ClientToScreen( &linex
, &liney
);
1321 m_dist_x
= pos
.x
- linex
;
1322 m_dist_y
= pos
.y
- liney
;
1325 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1327 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1328 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1330 wxFRAME_TOOL_WINDOW
|
1331 wxFRAME_FLOAT_ON_PARENT
|
1332 wxFRAME_NO_TASKBAR
|
1334 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1339 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1340 m_hint
->SetTransparent( 128 );
1346 wxDataViewMainWindow
*m_win
;
1349 int m_dist_x
,m_dist_y
;
1353 class wxDataViewDropTarget
: public wxDropTarget
1356 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1362 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1364 wxDataFormat format
= GetMatchingPair();
1365 if (format
== wxDF_INVALID
)
1367 return m_win
->OnDragOver( format
, x
, y
, def
);
1370 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1372 wxDataFormat format
= GetMatchingPair();
1373 if (format
== wxDF_INVALID
)
1375 return m_win
->OnDrop( format
, x
, y
);
1378 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1380 wxDataFormat format
= GetMatchingPair();
1381 if (format
== wxDF_INVALID
)
1385 return m_win
->OnData( format
, x
, y
, def
);
1388 virtual void OnLeave()
1389 { m_win
->OnLeave(); }
1391 wxDataViewMainWindow
*m_win
;
1394 #endif // wxUSE_DRAG_AND_DROP
1396 //-----------------------------------------------------------------------------
1397 // wxDataViewRenameTimer
1398 //-----------------------------------------------------------------------------
1400 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1405 void wxDataViewRenameTimer::Notify()
1407 m_owner
->OnRenameTimer();
1410 //-----------------------------------------------------------------------------
1411 // wxDataViewMainWindow
1412 //-----------------------------------------------------------------------------
1414 // The tree building helper, declared firstly
1415 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1416 wxDataViewTreeNode
* node
);
1418 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1420 if (row1
> row2
) return 1;
1421 if (row1
== row2
) return 0;
1425 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1427 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1428 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1429 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1430 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1431 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1432 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1435 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1436 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1437 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1438 m_selection( wxDataViewSelectionCmp
)
1443 m_lastOnSame
= false;
1444 m_renameTimer
= new wxDataViewRenameTimer( this );
1446 // TODO: user better initial values/nothing selected
1447 m_currentCol
= NULL
;
1450 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1452 #if wxUSE_DRAG_AND_DROP
1454 m_dragStart
= wxPoint(0,0);
1456 m_dragEnabled
= false;
1457 m_dropEnabled
= false;
1459 m_dropHintLine
= (unsigned int) -1;
1460 #endif // wxUSE_DRAG_AND_DROP
1462 m_lineLastClicked
= (unsigned int) -1;
1463 m_lineBeforeLastClicked
= (unsigned int) -1;
1464 m_lineSelectSingleOnUp
= (unsigned int) -1;
1468 SetBackgroundColour( *wxWHITE
);
1470 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1472 m_penRule
= wxPen(GetRuleColour());
1474 // compose a pen whichcan draw black lines
1475 // TODO: maybe there is something system colour to use
1476 m_penExpander
= wxPen(wxColour(0,0,0));
1478 m_root
= wxDataViewTreeNode::CreateRootNode();
1480 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1482 m_underMouse
= NULL
;
1486 wxDataViewMainWindow::~wxDataViewMainWindow()
1489 delete m_renameTimer
;
1493 #if wxUSE_DRAG_AND_DROP
1494 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1496 m_dragFormat
= format
;
1497 m_dragEnabled
= format
!= wxDF_INVALID
;
1502 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1504 m_dropFormat
= format
;
1505 m_dropEnabled
= format
!= wxDF_INVALID
;
1508 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1513 void wxDataViewMainWindow::RemoveDropHint()
1518 RefreshRow( m_dropHintLine
);
1519 m_dropHintLine
= (unsigned int) -1;
1523 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1524 wxCoord y
, wxDragResult def
)
1528 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1529 unsigned int row
= GetLineAt( yy
);
1531 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1537 wxDataViewItem item
= GetItemByRow( row
);
1539 wxDataViewModel
*model
= GetModel();
1541 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1542 event
.SetEventObject( m_owner
);
1543 event
.SetItem( item
);
1544 event
.SetModel( model
);
1545 event
.SetDataFormat( format
);
1546 if (!m_owner
->HandleWindowEvent( event
))
1552 if (!event
.IsAllowed())
1559 if (m_dropHint
&& (row
!= m_dropHintLine
))
1560 RefreshRow( m_dropHintLine
);
1562 m_dropHintLine
= row
;
1568 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1574 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1575 unsigned int row
= GetLineAt( yy
);
1577 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1580 wxDataViewItem item
= GetItemByRow( row
);
1582 wxDataViewModel
*model
= GetModel();
1584 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1585 event
.SetEventObject( m_owner
);
1586 event
.SetItem( item
);
1587 event
.SetModel( model
);
1588 event
.SetDataFormat( format
);
1589 if (!m_owner
->HandleWindowEvent( event
))
1592 if (!event
.IsAllowed())
1598 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1603 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1604 unsigned int row
= GetLineAt( yy
);
1606 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1609 wxDataViewItem item
= GetItemByRow( row
);
1611 wxDataViewModel
*model
= GetModel();
1613 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1615 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1616 event
.SetEventObject( m_owner
);
1617 event
.SetItem( item
);
1618 event
.SetModel( model
);
1619 event
.SetDataFormat( format
);
1620 event
.SetDataSize( obj
->GetSize() );
1621 event
.SetDataBuffer( obj
->GetData() );
1622 if (!m_owner
->HandleWindowEvent( event
))
1625 if (!event
.IsAllowed())
1631 void wxDataViewMainWindow::OnLeave()
1636 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1638 int height
= GetLineHeight( row
);
1640 unsigned int cols
= GetOwner()->GetColumnCount();
1642 for (col
= 0; col
< cols
; col
++)
1644 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1645 if (column
->IsHidden())
1646 continue; // skip it!
1647 width
+= column
->GetWidth();
1653 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1654 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1655 indent
= indent
+ m_lineHeight
;
1656 // try to use the m_lineHeight as the expander space
1660 wxBitmap
bitmap( width
, height
);
1661 wxMemoryDC
dc( bitmap
);
1662 dc
.SetFont( GetFont() );
1663 dc
.SetPen( *wxBLACK_PEN
);
1664 dc
.SetBrush( *wxWHITE_BRUSH
);
1665 dc
.DrawRectangle( 0,0,width
,height
);
1667 wxDataViewModel
*model
= m_owner
->GetModel();
1669 wxDataViewColumn
* const
1670 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1673 for (col
= 0; col
< cols
; col
++)
1675 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1676 wxDataViewRenderer
*cell
= column
->GetRenderer();
1678 if (column
->IsHidden())
1679 continue; // skip it!
1681 width
= column
->GetWidth();
1683 if (column
== expander
)
1686 wxDataViewItem item
= GetItemByRow( row
);
1687 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1689 wxRect
item_rect(x
, 0, width
, height
);
1690 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1692 // dc.SetClippingRegion( item_rect );
1693 cell
->WXCallRender(item_rect
, &dc
, 0);
1694 // dc.DestroyClippingRegion();
1702 #endif // wxUSE_DRAG_AND_DROP
1705 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1707 wxDataViewModel
*model
= GetModel();
1708 wxAutoBufferedPaintDC
dc( this );
1711 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1712 dc
.SetPen( *wxTRANSPARENT_PEN
);
1713 dc
.DrawRectangle(GetClientSize());
1718 // No items to draw.
1723 GetOwner()->PrepareDC( dc
);
1724 dc
.SetFont( GetFont() );
1726 wxRect update
= GetUpdateRegion().GetBox();
1727 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1729 // compute which items needs to be redrawn
1730 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1731 unsigned int item_count
=
1732 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1733 (int)(GetRowCount( ) - item_start
));
1734 unsigned int item_last
= item_start
+ item_count
;
1736 // Send the event to wxDataViewCtrl itself.
1737 wxWindow
* const parent
= GetParent();
1738 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1739 cache_event
.SetEventObject(parent
);
1740 cache_event
.SetCache(item_start
, item_last
- 1);
1741 parent
->ProcessWindowEvent(cache_event
);
1743 // compute which columns needs to be redrawn
1744 unsigned int cols
= GetOwner()->GetColumnCount();
1747 // we assume that we have at least one column below and painting an
1748 // empty control is unnecessary anyhow
1752 unsigned int col_start
= 0;
1753 unsigned int x_start
;
1754 for (x_start
= 0; col_start
< cols
; col_start
++)
1756 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1757 if (col
->IsHidden())
1758 continue; // skip it!
1760 unsigned int w
= col
->GetWidth();
1761 if (x_start
+w
>= (unsigned int)update
.x
)
1767 unsigned int col_last
= col_start
;
1768 unsigned int x_last
= x_start
;
1769 for (; col_last
< cols
; col_last
++)
1771 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1772 if (col
->IsHidden())
1773 continue; // skip it!
1775 if (x_last
> (unsigned int)update
.GetRight())
1778 x_last
+= col
->GetWidth();
1781 // Draw horizontal rules if required
1782 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1784 dc
.SetPen(m_penRule
);
1785 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1787 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1789 int y
= GetLineStart( i
);
1790 dc
.DrawLine(x_start
, y
, x_last
, y
);
1794 // Draw vertical rules if required
1795 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1797 dc
.SetPen(m_penRule
);
1798 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1800 // NB: Vertical rules are drawn in the last pixel of a column so that
1801 // they align perfectly with native MSW wxHeaderCtrl as well as for
1802 // consistency with MSW native list control. There's no vertical
1803 // rule at the most-left side of the control.
1805 int x
= x_start
- 1;
1806 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1808 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1809 if (col
->IsHidden())
1810 continue; // skip it
1812 x
+= col
->GetWidth();
1814 dc
.DrawLine(x
, GetLineStart( item_start
),
1815 x
, GetLineStart( item_last
) );
1819 // redraw the background for the items which are selected/current
1820 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1822 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1823 if (selected
|| item
== m_currentRow
)
1825 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1826 if (item
== m_currentRow
)
1827 flags
|= wxCONTROL_CURRENT
;
1829 flags
|= wxCONTROL_FOCUSED
;
1831 wxRect
rect( x_start
, GetLineStart( item
),
1832 x_last
- x_start
, GetLineHeight( item
) );
1833 wxRendererNative::Get().DrawItemSelectionRect
1843 #if wxUSE_DRAG_AND_DROP
1846 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1847 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1848 dc
.SetPen( *wxBLACK_PEN
);
1849 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1850 dc
.DrawRectangle( rect
);
1852 #endif // wxUSE_DRAG_AND_DROP
1854 wxDataViewColumn
* const
1855 expander
= GetExpanderColumnOrFirstOne(GetOwner());
1857 // redraw all cells for all rows which must be repainted and all columns
1859 cell_rect
.x
= x_start
;
1860 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1862 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1863 wxDataViewRenderer
*cell
= col
->GetRenderer();
1864 cell_rect
.width
= col
->GetWidth();
1866 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1867 continue; // skip it!
1869 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1871 // get the cell value and set it into the renderer
1872 wxDataViewTreeNode
*node
= NULL
;
1873 wxDataViewItem dataitem
;
1875 if (!IsVirtualList())
1877 node
= GetTreeNodeByRow(item
);
1881 dataitem
= node
->GetItem();
1883 // Skip all columns of "container" rows except the expander
1884 // column itself unless HasContainerColumns() overrides this.
1885 if ( col
!= expander
&&
1886 model
->IsContainer(dataitem
) &&
1887 !model
->HasContainerColumns(dataitem
) )
1892 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1895 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1898 cell_rect
.y
= GetLineStart( item
);
1899 cell_rect
.height
= GetLineHeight( item
);
1901 // deal with the expander
1903 if ((!IsList()) && (col
== expander
))
1905 // Calculate the indent first
1906 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1908 // we reserve m_lineHeight of horizontal space for the expander
1909 // but leave EXPANDER_MARGIN around the expander itself
1910 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1912 indent
+= m_lineHeight
;
1914 // draw expander if needed and visible
1915 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1917 dc
.SetPen( m_penExpander
);
1918 dc
.SetBrush( wxNullBrush
);
1920 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1921 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1922 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1924 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1927 if ( m_underMouse
== node
)
1928 flag
|= wxCONTROL_CURRENT
;
1929 if ( node
->IsOpen() )
1930 flag
|= wxCONTROL_EXPANDED
;
1932 // ensure that we don't overflow the cell (which might
1933 // happen if the column is very narrow)
1934 wxDCClipper
clip(dc
, cell_rect
);
1936 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1939 // force the expander column to left-center align
1940 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1943 wxRect item_rect
= cell_rect
;
1944 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1946 // account for the tree indent (harmless if we're not indented)
1947 item_rect
.x
+= indent
;
1948 item_rect
.width
-= indent
;
1950 if ( item_rect
.width
<= 0 )
1954 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1955 state
|= wxDATAVIEW_CELL_SELECTED
;
1957 // TODO: it would be much more efficient to create a clipping
1958 // region for the entire column being rendered (in the OnPaint
1959 // of wxDataViewMainWindow) instead of a single clip region for
1960 // each cell. However it would mean that each renderer should
1961 // respect the given wxRect's top & bottom coords, eventually
1962 // violating only the left & right coords - however the user can
1963 // make its own renderer and thus we cannot be sure of that.
1964 wxDCClipper
clip(dc
, item_rect
);
1966 cell
->WXCallRender(item_rect
, &dc
, state
);
1969 cell_rect
.x
+= cell_rect
.width
;
1973 void wxDataViewMainWindow::OnRenameTimer()
1975 // We have to call this here because changes may just have
1976 // been made and no screen update taken place.
1979 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1980 // (needs to be tested!)
1984 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1986 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1988 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1991 //-----------------------------------------------------------------------------
1992 // Helper class for do operation on the tree node
1993 //-----------------------------------------------------------------------------
1998 virtual ~DoJob() { }
2000 // The return value control how the tree-walker tranverse the tree
2003 DONE
, // Job done, stop traversing and return
2004 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
2005 CONTINUE
// Job not done, continue
2008 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
2011 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2013 wxCHECK_MSG( node
, false, "can't walk NULL node" );
2015 switch( func( node
) )
2019 case DoJob::SKIP_SUBTREE
:
2021 case DoJob::CONTINUE
:
2025 if ( node
->HasChildren() )
2027 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2029 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2033 if ( Walker(*i
, func
) )
2041 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2043 if (IsVirtualList())
2045 wxDataViewVirtualListModel
*list_model
=
2046 (wxDataViewVirtualListModel
*) GetModel();
2047 m_count
= list_model
->GetCount();
2053 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2058 wxDataViewItemArray siblings
;
2059 GetModel()->GetChildren(parent
, siblings
);
2060 int itemPos
= siblings
.Index(item
, /*fromEnd=*/true);
2061 wxCHECK_MSG( itemPos
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2063 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2064 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2066 parentNode
->SetHasChildren(true);
2067 parentNode
->InsertChild(itemNode
, itemPos
);
2068 parentNode
->ChangeSubTreeCount(+1);
2073 GetOwner()->InvalidateColBestWidths();
2079 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2080 const wxDataViewItem
& item
)
2082 if (IsVirtualList())
2084 wxDataViewVirtualListModel
*list_model
=
2085 (wxDataViewVirtualListModel
*) GetModel();
2086 m_count
= list_model
->GetCount();
2088 if ( !m_selection
.empty() )
2090 const int row
= GetRowByItem(item
);
2092 int rowIndexInSelection
= wxNOT_FOUND
;
2094 const size_t selCount
= m_selection
.size();
2095 for ( size_t i
= 0; i
< selCount
; i
++ )
2097 if ( m_selection
[i
] == (unsigned)row
)
2098 rowIndexInSelection
= i
;
2099 else if ( m_selection
[i
] > (unsigned)row
)
2103 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2104 m_selection
.RemoveAt(rowIndexInSelection
);
2108 else // general case
2110 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2112 // Notice that it is possible that the item being deleted is not in the
2113 // tree at all, for example we could be deleting a never shown (because
2114 // collapsed) item in a tree model. So it's not an error if we don't know
2115 // about this item, just return without doing anything then.
2119 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2120 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2122 // We can't use FindNode() to find 'item', because it was already
2123 // removed from the model by the time ItemDeleted() is called, so we
2124 // have to do it manually. We keep track of its position as well for
2126 int itemPosInNode
= 0;
2127 wxDataViewTreeNode
*itemNode
= NULL
;
2128 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2129 i
!= parentsChildren
.end();
2130 ++i
, ++itemPosInNode
)
2132 if( (*i
)->GetItem() == item
)
2139 // If the parent wasn't expanded, it's possible that we didn't have a
2140 // node corresponding to 'item' and so there's nothing left to do.
2143 // If this was the last child to be removed, it's possible the parent
2144 // node became a leaf. Let's ask the model about it.
2145 if ( parentNode
->GetChildNodes().empty() )
2146 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2151 // Delete the item from wxDataViewTreeNode representation:
2152 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2154 parentNode
->RemoveChild(itemNode
);
2156 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2158 // Make the row number invalid and get a new valid one when user call GetRowCount
2161 // If this was the last child to be removed, it's possible the parent
2162 // node became a leaf. Let's ask the model about it.
2163 if ( parentNode
->GetChildNodes().empty() )
2164 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2166 // Update selection by removing 'item' and its entire children tree from the selection.
2167 if ( !m_selection
.empty() )
2169 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2170 // the parent ('parentNode') and position in its list of children
2172 if ( itemPosInNode
== 0 )
2174 // 1st child, row number is that of the parent parentNode + 1
2175 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2179 // row number is that of the sibling above 'item' + its subtree if any + 1
2180 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2182 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2183 siblingNode
->GetSubTreeCount() +
2187 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2189 const size_t numSelections
= m_selection
.size();
2190 for ( size_t i
= 0; i
< numSelections
; ++i
)
2192 const int s
= m_selection
[i
];
2194 newsel
.push_back(s
);
2195 else if ( s
>= itemRow
+ itemsDeleted
)
2196 newsel
.push_back(s
- itemsDeleted
);
2197 // else: deleted item, remove from selection
2200 m_selection
= newsel
;
2204 // Change the current row to the last row if the current exceed the max row number
2205 if( m_currentRow
> GetRowCount() )
2206 ChangeCurrentRow(m_count
- 1);
2208 GetOwner()->InvalidateColBestWidths();
2214 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2219 GetOwner()->InvalidateColBestWidths();
2222 wxWindow
*parent
= GetParent();
2223 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2224 le
.SetEventObject(parent
);
2225 le
.SetModel(GetModel());
2227 parent
->ProcessWindowEvent(le
);
2232 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2234 int view_column
= -1;
2235 unsigned int n_col
= m_owner
->GetColumnCount();
2236 for (unsigned i
= 0; i
< n_col
; i
++)
2238 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2239 if (column
->GetModelColumn() == model_column
)
2241 view_column
= (int) i
;
2245 if (view_column
== -1)
2248 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2249 /*#define MAX_VIRTUAL_WIDTH 100000
2251 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2252 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2253 Refresh( true, &rect );
2260 GetOwner()->InvalidateColBestWidth(view_column
);
2263 wxWindow
*parent
= GetParent();
2264 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2265 le
.SetEventObject(parent
);
2266 le
.SetModel(GetModel());
2268 le
.SetColumn(view_column
);
2269 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2270 parent
->ProcessWindowEvent(le
);
2275 bool wxDataViewMainWindow::Cleared()
2278 m_selection
.Clear();
2281 BuildTree( GetModel() );
2283 GetOwner()->InvalidateColBestWidths();
2289 void wxDataViewMainWindow::UpdateDisplay()
2292 m_underMouse
= NULL
;
2295 void wxDataViewMainWindow::OnInternalIdle()
2297 wxWindow::OnInternalIdle();
2301 RecalculateDisplay();
2306 void wxDataViewMainWindow::RecalculateDisplay()
2308 wxDataViewModel
*model
= GetModel();
2315 int width
= GetEndOfLastCol();
2316 int height
= GetLineStart( GetRowCount() );
2318 SetVirtualSize( width
, height
);
2319 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2324 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2326 m_underMouse
= NULL
;
2328 wxWindow::ScrollWindow( dx
, dy
, rect
);
2330 if (GetOwner()->m_headerArea
)
2331 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2334 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2336 m_underMouse
= NULL
;
2339 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2340 int sy
= GetLineStart( rows
)/y
;
2344 wxRect rect
= GetClientRect();
2348 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2349 for (x_start
= 0; colnum
< column
; colnum
++)
2351 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2352 if (col
->IsHidden())
2353 continue; // skip it!
2355 w
= col
->GetWidth();
2359 int x_end
= x_start
+ w
;
2360 xe
= xx
+ rect
.width
;
2363 sx
= ( xx
+ x_end
- xe
)/x
;
2370 m_owner
->Scroll( sx
, sy
);
2373 int wxDataViewMainWindow::GetCountPerPage() const
2375 wxSize size
= GetClientSize();
2376 return size
.y
/ m_lineHeight
;
2379 int wxDataViewMainWindow::GetEndOfLastCol() const
2383 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2385 const wxDataViewColumn
*c
=
2386 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2389 width
+= c
->GetWidth();
2394 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2398 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2400 return GetLineAt( y
);
2403 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2405 wxSize client_size
= GetClientSize();
2406 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2407 &client_size
.x
, &client_size
.y
);
2409 // we should deal with the pixel here
2410 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2412 return wxMin( GetRowCount()-1, row
);
2415 unsigned int wxDataViewMainWindow::GetRowCount()
2417 if ( m_count
== -1 )
2419 m_count
= RecalculateCount();
2425 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2432 void wxDataViewMainWindow::SelectAllRows( bool on
)
2439 m_selection
.Clear();
2440 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2441 m_selection
.Add( i
);
2446 unsigned int first_visible
= GetFirstVisibleRow();
2447 unsigned int last_visible
= GetLastVisibleRow();
2449 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2451 unsigned int row
= m_selection
[i
];
2452 if ((row
>= first_visible
) && (row
<= last_visible
))
2455 m_selection
.Clear();
2459 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2461 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2465 m_selection
.Add( row
);
2473 m_selection
.Remove( row
);
2479 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2483 unsigned int tmp
= from
;
2489 for (i
= from
; i
<= to
; i
++)
2491 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2494 m_selection
.Add( i
);
2499 m_selection
.Remove( i
);
2502 RefreshRows( from
, to
);
2505 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2507 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2509 int n
= aSelections
[i
];
2511 m_selection
.Add( n
);
2516 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2518 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2519 m_selection
.Add( row
);
2521 m_selection
.Remove( row
);
2525 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2527 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2530 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2532 wxWindow
*parent
= GetParent();
2533 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2535 le
.SetEventObject(parent
);
2536 le
.SetModel(GetModel());
2539 parent
->ProcessWindowEvent(le
);
2542 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2544 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2545 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2547 wxSize client_size
= GetClientSize();
2548 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2549 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2550 if (intersect_rect
.width
> 0)
2551 Refresh( true, &intersect_rect
);
2554 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2558 unsigned int tmp
= to
;
2563 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2564 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2566 wxSize client_size
= GetClientSize();
2567 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2568 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2569 if (intersect_rect
.width
> 0)
2570 Refresh( true, &intersect_rect
);
2573 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2575 wxSize client_size
= GetClientSize();
2576 int start
= GetLineStart( firstRow
);
2577 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2578 if (start
> client_size
.y
) return;
2580 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2582 Refresh( true, &rect
);
2585 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2589 rect
.y
= GetLineStart( row
);
2590 rect
.width
= GetEndOfLastCol();
2591 rect
.height
= GetLineHeight( row
);
2596 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2598 const wxDataViewModel
*model
= GetModel();
2600 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2602 // TODO make more efficient
2607 for (r
= 0; r
< row
; r
++)
2609 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2610 if (!node
) return start
;
2612 wxDataViewItem item
= node
->GetItem();
2614 unsigned int cols
= GetOwner()->GetColumnCount();
2616 int height
= m_lineHeight
;
2617 for (col
= 0; col
< cols
; col
++)
2619 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2620 if (column
->IsHidden())
2621 continue; // skip it!
2624 model
->IsContainer(item
) &&
2625 !model
->HasContainerColumns(item
))
2626 continue; // skip it!
2628 wxDataViewRenderer
*renderer
=
2629 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2630 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2632 height
= wxMax( height
, renderer
->GetSize().y
);
2642 return row
* m_lineHeight
;
2646 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2648 const wxDataViewModel
*model
= GetModel();
2650 // check for the easy case first
2651 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2652 return y
/ m_lineHeight
;
2654 // TODO make more efficient
2655 unsigned int row
= 0;
2656 unsigned int yy
= 0;
2659 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2662 // not really correct...
2663 return row
+ ((y
-yy
) / m_lineHeight
);
2666 wxDataViewItem item
= node
->GetItem();
2668 unsigned int cols
= GetOwner()->GetColumnCount();
2670 int height
= m_lineHeight
;
2671 for (col
= 0; col
< cols
; col
++)
2673 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2674 if (column
->IsHidden())
2675 continue; // skip it!
2678 model
->IsContainer(item
) &&
2679 !model
->HasContainerColumns(item
))
2680 continue; // skip it!
2682 wxDataViewRenderer
*renderer
=
2683 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2684 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2686 height
= wxMax( height
, renderer
->GetSize().y
);
2697 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2699 const wxDataViewModel
*model
= GetModel();
2701 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2703 wxASSERT( !IsVirtualList() );
2705 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2706 // wxASSERT( node );
2707 if (!node
) return m_lineHeight
;
2709 wxDataViewItem item
= node
->GetItem();
2711 int height
= m_lineHeight
;
2713 unsigned int cols
= GetOwner()->GetColumnCount();
2715 for (col
= 0; col
< cols
; col
++)
2717 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2718 if (column
->IsHidden())
2719 continue; // skip it!
2722 model
->IsContainer(item
) &&
2723 !model
->HasContainerColumns(item
))
2724 continue; // skip it!
2726 wxDataViewRenderer
*renderer
=
2727 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2728 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2730 height
= wxMax( height
, renderer
->GetSize().y
);
2737 return m_lineHeight
;
2742 class RowToTreeNodeJob
: public DoJob
2745 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2748 this->current
= current
;
2753 virtual int operator() ( wxDataViewTreeNode
* node
)
2756 if( current
== static_cast<int>(row
))
2762 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2764 current
+= node
->GetSubTreeCount();
2765 return DoJob::SKIP_SUBTREE
;
2771 // If the current node has only leaf children, we can find the
2772 // desired node directly. This can speed up finding the node
2773 // in some cases, and will have a very good effect for list views.
2774 if ( node
->HasChildren() &&
2775 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2777 const int index
= static_cast<int>(row
) - current
- 1;
2778 ret
= node
->GetChildNodes()[index
];
2782 return DoJob::CONTINUE
;
2786 wxDataViewTreeNode
* GetResult() const
2792 wxDataViewTreeNode
* ret
;
2793 wxDataViewTreeNode
* parent
;
2796 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2798 wxASSERT( !IsVirtualList() );
2800 RowToTreeNodeJob
job( row
, -2, m_root
);
2801 Walker( m_root
, job
);
2802 return job
.GetResult();
2805 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2807 if (IsVirtualList())
2809 return wxDataViewItem( wxUIntToPtr(row
+1) );
2813 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2814 return node
? node
->GetItem() : wxDataViewItem();
2819 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2820 const wxDataViewItem
& item
)
2822 wxWindow
*parent
= GetParent();
2823 wxDataViewEvent
le(type
, parent
->GetId());
2825 le
.SetEventObject(parent
);
2826 le
.SetModel(GetModel());
2829 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2832 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2837 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2841 if (!node
->HasChildren())
2844 return node
->IsOpen();
2847 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2852 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2856 if (!node
->HasChildren())
2862 void wxDataViewMainWindow::Expand( unsigned int row
)
2867 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2871 if (!node
->HasChildren())
2874 if (!node
->IsOpen())
2876 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
2878 // Vetoed by the event handler.
2884 // build the children of current node
2885 if( node
->GetChildNodes().empty() )
2888 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
2891 // By expanding the node all row indices that are currently in the selection list
2892 // and are greater than our node have become invalid. So we have to correct that now.
2893 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2894 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2896 const unsigned testRow
= m_selection
[i
];
2897 // all rows above us are not affected, so skip them
2901 m_selection
[i
] += rowAdjustment
;
2904 if(m_currentRow
> row
)
2905 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2909 // Send the expanded event
2910 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2914 void wxDataViewMainWindow::Collapse(unsigned int row
)
2919 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2923 if (!node
->HasChildren())
2928 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
2930 // Vetoed by the event handler.
2934 // Find out if there are selected items below the current node.
2935 bool selectCollapsingRow
= false;
2936 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2937 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2938 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2940 const unsigned testRow
= m_selection
[i
];
2941 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2943 selectCollapsingRow
= true;
2944 // get out as soon as we have found a node that is selected
2951 // If the node to be closed has selected items the user won't see those any longer.
2952 // We select the collapsing node in this case.
2953 if(selectCollapsingRow
)
2955 SelectAllRows(false);
2956 ChangeCurrentRow(row
);
2957 SelectRow(row
, true);
2958 SendSelectionChangedEvent(GetItemByRow(row
));
2962 // if there were no selected items below our node we still need to "fix" the
2963 // selection list to adjust for the changing of the row indices.
2964 // We actually do the opposite of what we are doing in Expand().
2965 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2967 const unsigned testRow
= m_selection
[i
];
2968 // all rows above us are not affected, so skip them
2972 m_selection
[i
] -= rowAdjustment
;
2975 // if the "current row" is being collapsed away we change it to the current row ;-)
2976 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2977 ChangeCurrentRow(row
);
2978 else if(m_currentRow
> row
)
2979 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2984 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2988 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2990 const wxDataViewModel
* model
= GetModel();
2997 // Compose the parent-chain for the item we are looking for
2998 wxVector
<wxDataViewItem
> parentChain
;
2999 wxDataViewItem
it( item
);
3002 parentChain
.push_back(it
);
3003 it
= model
->GetParent(it
);
3006 // Find the item along the parent-chain.
3007 // This algorithm is designed to speed up the node-finding method
3008 wxDataViewTreeNode
* node
= m_root
;
3009 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3011 if( node
->HasChildren() )
3013 if( node
->GetChildNodes().empty() )
3015 // Even though the item is a container, it doesn't have any
3016 // child nodes in the control's representation yet. We have
3017 // to realize its subtree now.
3019 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3022 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
3025 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3027 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3028 if (currentNode
->GetItem() == parentChain
[iter
])
3030 if (currentNode
->GetItem() == item
)
3050 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3051 wxDataViewColumn
* &column
)
3053 wxDataViewColumn
*col
= NULL
;
3054 unsigned int cols
= GetOwner()->GetColumnCount();
3055 unsigned int colnum
= 0;
3057 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3058 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3060 col
= GetOwner()->GetColumnAt(colnum
);
3061 if (col
->IsHidden())
3062 continue; // skip it!
3064 unsigned int w
= col
->GetWidth();
3065 if (x_start
+w
>= (unsigned int)x
)
3072 item
= GetItemByRow( GetLineAt( y
) );
3075 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3076 const wxDataViewColumn
* column
)
3081 unsigned int cols
= GetOwner()->GetColumnCount();
3082 // If column is null the loop will compute the combined width of all columns.
3083 // Otherwise, it will compute the x position of the column we are looking for.
3084 for (unsigned int i
= 0; i
< cols
; i
++)
3086 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3091 if (col
->IsHidden())
3092 continue; // skip it!
3094 xpos
+= col
->GetWidth();
3095 width
+= col
->GetWidth();
3100 // If we have a column, we need can get its width directly.
3101 if(column
->IsHidden())
3104 width
= column
->GetWidth();
3109 // If we have no column, we reset the x position back to zero.
3113 // we have to take an expander column into account and compute its indentation
3114 // to get the correct x position where the actual text is
3116 int row
= GetRowByItem(item
);
3118 (column
== 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column
) )
3120 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3121 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3122 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3125 wxRect
itemRect( xpos
+ indent
,
3126 GetLineStart( row
),
3128 GetLineHeight( row
) );
3130 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3131 &itemRect
.x
, &itemRect
.y
);
3136 int wxDataViewMainWindow::RecalculateCount()
3138 if (IsVirtualList())
3140 wxDataViewVirtualListModel
*list_model
=
3141 (wxDataViewVirtualListModel
*) GetModel();
3143 return list_model
->GetCount();
3147 return m_root
->GetSubTreeCount();
3151 class ItemToRowJob
: public DoJob
3154 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3161 // Maybe binary search will help to speed up this process
3162 virtual int operator() ( wxDataViewTreeNode
* node
)
3165 if( node
->GetItem() == item
)
3170 if( node
->GetItem() == *m_iter
)
3173 return DoJob::CONTINUE
;
3177 ret
+= node
->GetSubTreeCount();
3178 return DoJob::SKIP_SUBTREE
;
3183 // the row number is begin from zero
3184 int GetResult() const
3188 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3189 wxDataViewItem item
;
3194 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3196 const wxDataViewModel
* model
= GetModel();
3200 if (IsVirtualList())
3202 return wxPtrToUInt( item
.GetID() ) -1;
3209 // Compose the parent-chain of the item we are looking for
3210 wxVector
<wxDataViewItem
> parentChain
;
3211 wxDataViewItem
it( item
);
3214 parentChain
.push_back(it
);
3215 it
= model
->GetParent(it
);
3218 // add an 'invalid' item to represent our 'invisible' root node
3219 parentChain
.push_back(wxDataViewItem());
3221 // the parent chain was created by adding the deepest parent first.
3222 // so if we want to start at the root node, we have to iterate backwards through the vector
3223 ItemToRowJob
job( item
, parentChain
.rbegin() );
3224 Walker( m_root
, job
);
3225 return job
.GetResult();
3229 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3230 wxDataViewTreeNode
* node
)
3232 if( !model
->IsContainer( item
) )
3235 wxDataViewItemArray children
;
3236 unsigned int num
= model
->GetChildren( item
, children
);
3238 for ( unsigned int index
= 0; index
< num
; index
++ )
3240 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3242 if( model
->IsContainer(children
[index
]) )
3243 n
->SetHasChildren( true );
3245 node
->InsertChild(n
, index
);
3248 wxASSERT( node
->IsOpen() );
3249 node
->ChangeSubTreeCount(+num
);
3252 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3256 if (GetModel()->IsVirtualListModel())
3262 m_root
= wxDataViewTreeNode::CreateRootNode();
3264 // First we define a invalid item to fetch the top-level elements
3265 wxDataViewItem item
;
3267 BuildTreeHelper( model
, item
, m_root
);
3271 void wxDataViewMainWindow::DestroyTree()
3273 if (!IsVirtualList())
3280 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3282 wxWindow
* const parent
= GetParent();
3284 // propagate the char event upwards
3285 wxKeyEvent
eventForParent(event
);
3286 eventForParent
.SetEventObject(parent
);
3287 if ( parent
->ProcessWindowEvent(eventForParent
) )
3290 if ( parent
->HandleAsNavigationKey(event
) )
3293 // no item -> nothing to do
3294 if (!HasCurrentRow())
3300 // don't use m_linesPerPage directly as it might not be computed yet
3301 const int pageSize
= GetCountPerPage();
3302 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3304 switch ( event
.GetKeyCode() )
3308 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3309 // it. Only if that event is not handled do we activate column renderer (which
3310 // is normally done by Space).
3312 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3314 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3317 le
.SetEventObject(parent
);
3318 le
.SetModel(GetModel());
3320 if ( parent
->ProcessWindowEvent(le
) )
3322 // else: fall through to WXK_SPACE handling
3327 // Activate the first activatable column if there is any:
3328 wxDataViewColumn
*activatableCol
= NULL
;
3330 const unsigned cols
= GetOwner()->GetColumnCount();
3331 for ( unsigned i
= 0; i
< cols
; i
++ )
3333 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3334 if ( c
->IsHidden() )
3336 if ( c
->GetRenderer()->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3343 if ( activatableCol
)
3345 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3347 const unsigned colIdx
= activatableCol
->GetModelColumn();
3348 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3350 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3351 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3352 cell
->WXOnActivate(cell_rect
, GetModel(), item
, colIdx
);
3358 if ( m_currentRow
> 0 )
3359 OnVerticalNavigation( m_currentRow
- 1, event
);
3363 if ( m_currentRow
+ 1 < GetRowCount() )
3364 OnVerticalNavigation( m_currentRow
+ 1, event
);
3366 // Add the process for tree expanding/collapsing
3378 OnVerticalNavigation( GetRowCount() - 1, event
);
3383 OnVerticalNavigation( 0, event
);
3388 int steps
= pageSize
- 1;
3389 int index
= m_currentRow
- steps
;
3393 OnVerticalNavigation( index
, event
);
3399 int steps
= pageSize
- 1;
3400 unsigned int index
= m_currentRow
+ steps
;
3401 unsigned int count
= GetRowCount();
3402 if ( index
>= count
)
3405 OnVerticalNavigation( index
, event
);
3411 if( !m_selection
.empty() )
3413 // TODO: we need to revise that when we have a concept for a 'current column'
3414 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3424 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3426 wxCHECK_RET( newCurrent
< GetRowCount(),
3427 wxT("invalid item index in OnVerticalNavigation()") );
3429 // if there is no selection, we cannot move it anywhere
3430 if (!HasCurrentRow())
3433 unsigned int oldCurrent
= m_currentRow
;
3435 // in single selection we just ignore Shift as we can't select several
3437 if ( event
.ShiftDown() && !IsSingleSel() )
3439 RefreshRow( oldCurrent
);
3441 ChangeCurrentRow( newCurrent
);
3443 // select all the items between the old and the new one
3444 if ( oldCurrent
> newCurrent
)
3446 newCurrent
= oldCurrent
;
3447 oldCurrent
= m_currentRow
;
3450 SelectRows( oldCurrent
, newCurrent
, true );
3451 if (oldCurrent
!=newCurrent
)
3452 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3456 RefreshRow( oldCurrent
);
3458 // all previously selected items are unselected unless ctrl is held
3459 if ( !event
.ControlDown() )
3460 SelectAllRows(false);
3462 ChangeCurrentRow( newCurrent
);
3464 if ( !event
.ControlDown() )
3466 SelectRow( m_currentRow
, true );
3467 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3470 RefreshRow( m_currentRow
);
3473 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3476 void wxDataViewMainWindow::OnLeftKey()
3481 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3485 if (node
->HasChildren() && node
->IsOpen())
3487 Collapse(m_currentRow
);
3489 else // if the node is already closed we move the selection to its parent
3491 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3495 int parent
= GetRowByItem( parent_node
->GetItem() );
3498 unsigned int row
= m_currentRow
;
3499 SelectRow( row
, false);
3500 SelectRow( parent
, true );
3501 ChangeCurrentRow( parent
);
3502 GetOwner()->EnsureVisible( parent
, -1 );
3503 SendSelectionChangedEvent( parent_node
->GetItem() );
3509 void wxDataViewMainWindow::OnRightKey()
3511 if (!IsExpanded( m_currentRow
))
3512 Expand( m_currentRow
);
3515 unsigned int row
= m_currentRow
;
3516 SelectRow( row
, false );
3517 SelectRow( row
+ 1, true );
3518 ChangeCurrentRow( row
+ 1 );
3519 GetOwner()->EnsureVisible( row
+ 1, -1 );
3520 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3524 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3526 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3528 // let the base handle mouse wheel events.
3533 // set the focus to ourself if any of the mouse buttons are pressed
3534 if(event
.ButtonDown() && !HasFocus())
3537 int x
= event
.GetX();
3538 int y
= event
.GetY();
3539 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3540 wxDataViewColumn
*col
= NULL
;
3543 unsigned int cols
= GetOwner()->GetColumnCount();
3545 for (i
= 0; i
< cols
; i
++)
3547 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3549 continue; // skip it!
3551 if (x
< xpos
+ c
->GetWidth())
3556 xpos
+= c
->GetWidth();
3559 wxDataViewModel
* const model
= GetModel();
3561 const unsigned int current
= GetLineAt( y
);
3562 const wxDataViewItem item
= GetItemByRow(current
);
3564 // Handle right clicking here, before everything else as context menu
3565 // events should be sent even when we click outside of any item, unlike all
3567 if (event
.RightUp())
3569 wxWindow
*parent
= GetParent();
3570 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3571 le
.SetEventObject(parent
);
3574 if ( item
.IsOk() && col
)
3577 le
.SetColumn( col
->GetModelColumn() );
3578 le
.SetDataViewColumn( col
);
3581 model
->GetValue( value
, item
, col
->GetModelColumn() );
3585 parent
->ProcessWindowEvent(le
);
3595 wxDataViewRenderer
*cell
= col
->GetRenderer();
3596 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3598 // Unselect all if below the last row ?
3603 wxDataViewColumn
* const
3604 expander
= GetExpanderColumnOrFirstOne(GetOwner());
3606 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3607 // button) and also determine the offset of the real cell start, skipping
3608 // the indentation and the expander itself.
3609 bool hoverOverExpander
= false;
3611 if ((!IsList()) && (expander
== col
))
3613 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3615 int indent
= node
->GetIndentLevel();
3616 itemOffset
= GetOwner()->GetIndent()*indent
;
3618 if ( node
->HasChildren() )
3620 // we make the rectangle we are looking in a bit bigger than the actual
3621 // visual expander so the user can hit that little thing reliably
3622 wxRect
rect(itemOffset
,
3623 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3624 m_lineHeight
, m_lineHeight
);
3626 if( rect
.Contains(x
, y
) )
3628 // So the mouse is over the expander
3629 hoverOverExpander
= true;
3630 if (m_underMouse
&& m_underMouse
!= node
)
3632 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3633 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3635 if (m_underMouse
!= node
)
3637 // wxLogMessage("Do the row: %d", current);
3638 RefreshRow(current
);
3640 m_underMouse
= node
;
3644 // Account for the expander as well, even if this item doesn't have it,
3645 // its parent does so it still counts for the offset.
3646 itemOffset
+= m_lineHeight
;
3648 if (!hoverOverExpander
)
3650 if (m_underMouse
!= NULL
)
3652 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3653 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3654 m_underMouse
= NULL
;
3658 #if wxUSE_DRAG_AND_DROP
3659 if (event
.Dragging())
3661 if (m_dragCount
== 0)
3663 // we have to report the raw, physical coords as we want to be
3664 // able to call HitTest(event.m_pointDrag) from the user code to
3665 // get the item being dragged
3666 m_dragStart
= event
.GetPosition();
3671 if (m_dragCount
!= 3)
3674 if (event
.LeftIsDown())
3676 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3677 &m_dragStart
.x
, &m_dragStart
.y
);
3678 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3679 wxDataViewItem itemDragged
= GetItemByRow( drag_item_row
);
3681 // Notify cell about drag
3682 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3683 event
.SetEventObject( m_owner
);
3684 event
.SetItem( itemDragged
);
3685 event
.SetModel( model
);
3686 if (!m_owner
->HandleWindowEvent( event
))
3689 if (!event
.IsAllowed())
3692 wxDataObject
*obj
= event
.GetDataObject();
3696 wxDataViewDropSource
drag( this, drag_item_row
);
3697 drag
.SetData( *obj
);
3698 /* wxDragResult res = */ drag
.DoDragDrop();
3707 #endif // wxUSE_DRAG_AND_DROP
3709 bool simulateClick
= false;
3711 if (event
.ButtonDClick())
3713 m_renameTimer
->Stop();
3714 m_lastOnSame
= false;
3717 bool ignore_other_columns
=
3718 ((expander
!= col
) &&
3719 (model
->IsContainer(item
)) &&
3720 (!model
->HasContainerColumns(item
)));
3722 if (event
.LeftDClick())
3724 if(hoverOverExpander
)
3726 // a double click on the expander will be converted into a "simulated" normal click
3727 simulateClick
= true;
3729 else if ( current
== m_lineLastClicked
)
3731 bool activated
= false;
3733 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3735 const unsigned colIdx
= col
->GetModelColumn();
3737 cell
->PrepareForItem(model
, item
, colIdx
);
3739 wxRect
cell_rect( xpos
, GetLineStart( current
),
3740 col
->GetWidth(), GetLineHeight( current
) );
3741 activated
= cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3746 wxWindow
*parent
= GetParent();
3747 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3749 le
.SetColumn( col
->GetModelColumn() );
3750 le
.SetDataViewColumn( col
);
3751 le
.SetEventObject(parent
);
3752 le
.SetModel(GetModel());
3754 parent
->ProcessWindowEvent(le
);
3760 // The first click was on another item, so don't interpret this as
3761 // a double click, but as a simple click instead
3762 simulateClick
= true;
3766 if (event
.LeftUp() && !hoverOverExpander
)
3768 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3770 // select single line
3771 SelectAllRows( false );
3772 SelectRow( m_lineSelectSingleOnUp
, true );
3773 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3776 // If the user click the expander, we do not do editing even if the column
3777 // with expander are editable
3778 if (m_lastOnSame
&& !ignore_other_columns
)
3780 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3781 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3783 m_renameTimer
->Start( 100, true );
3787 m_lastOnSame
= false;
3788 m_lineSelectSingleOnUp
= (unsigned int)-1;
3790 else if(!event
.LeftUp())
3792 // This is necessary, because after a DnD operation in
3793 // from and to ourself, the up event is swallowed by the
3794 // DnD code. So on next non-up event (which means here and
3795 // now) m_lineSelectSingleOnUp should be reset.
3796 m_lineSelectSingleOnUp
= (unsigned int)-1;
3799 if (event
.RightDown())
3801 m_lineBeforeLastClicked
= m_lineLastClicked
;
3802 m_lineLastClicked
= current
;
3804 // If the item is already selected, do not update the selection.
3805 // Multi-selections should not be cleared if a selected item is clicked.
3806 if (!IsRowSelected(current
))
3808 SelectAllRows(false);
3809 const unsigned oldCurrent
= m_currentRow
;
3810 ChangeCurrentRow(current
);
3811 SelectRow(m_currentRow
,true);
3812 RefreshRow(oldCurrent
);
3813 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3816 else if (event
.MiddleDown())
3820 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3822 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3824 // hoverOverExpander being true tells us that our node must be
3825 // valid and have children.
3826 // So we don't need any extra checks.
3827 if( node
->IsOpen() )
3832 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3834 m_lineBeforeLastClicked
= m_lineLastClicked
;
3835 m_lineLastClicked
= current
;
3837 unsigned int oldCurrentRow
= m_currentRow
;
3838 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3840 bool cmdModifierDown
= event
.CmdDown();
3841 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3843 if ( IsSingleSel() || !IsRowSelected(current
) )
3845 SelectAllRows( false );
3846 ChangeCurrentRow(current
);
3847 SelectRow(m_currentRow
,true);
3848 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3850 else // multi sel & current is highlighted & no mod keys
3852 m_lineSelectSingleOnUp
= current
;
3853 ChangeCurrentRow(current
); // change focus
3856 else // multi sel & either ctrl or shift is down
3858 if (cmdModifierDown
)
3860 ChangeCurrentRow(current
);
3861 ReverseRowSelection(m_currentRow
);
3862 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3864 else if (event
.ShiftDown())
3866 ChangeCurrentRow(current
);
3868 unsigned int lineFrom
= oldCurrentRow
,
3871 if ( lineTo
< lineFrom
)
3874 lineFrom
= m_currentRow
;
3877 SelectRows(lineFrom
, lineTo
, true);
3878 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3880 else // !ctrl, !shift
3882 // test in the enclosing if should make it impossible
3883 wxFAIL_MSG( wxT("how did we get here?") );
3887 if (m_currentRow
!= oldCurrentRow
)
3888 RefreshRow( oldCurrentRow
);
3890 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3892 // Update selection here...
3895 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3896 (current
== oldCurrentRow
)) && oldWasSelected
;
3898 // Call LeftClick after everything else as under GTK+
3899 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3901 // notify cell about click
3902 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3904 wxRect
cell_rect( xpos
+ itemOffset
,
3905 GetLineStart( current
),
3906 col
->GetWidth() - itemOffset
,
3907 GetLineHeight( current
) );
3909 // Report position relative to the cell's custom area, i.e.
3910 // no the entire space as given by the control but the one
3911 // used by the renderer after calculation of alignment etc.
3913 // adjust the rectangle ourselves to account for the alignment
3914 wxRect rectItem
= cell_rect
;
3915 const int align
= cell
->GetAlignment();
3916 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3918 const wxSize size
= cell
->GetSize();
3920 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3922 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3923 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3924 else if ( align
& wxALIGN_RIGHT
)
3925 rectItem
.x
+= cell_rect
.width
- size
.x
;
3926 // else: wxALIGN_LEFT is the default
3929 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3931 if ( align
& wxALIGN_CENTER_VERTICAL
)
3932 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3933 else if ( align
& wxALIGN_BOTTOM
)
3934 rectItem
.y
+= cell_rect
.height
- size
.y
;
3935 // else: wxALIGN_TOP is the default
3939 wxPoint
pos( event
.GetPosition() );
3940 pos
.x
-= rectItem
.x
;
3941 pos
.y
-= rectItem
.y
;
3943 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3945 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3946 model
, item
, col
->GetModelColumn());
3951 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3955 if (HasCurrentRow())
3961 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3965 if (HasCurrentRow())
3971 //-----------------------------------------------------------------------------
3973 //-----------------------------------------------------------------------------
3975 WX_DEFINE_LIST(wxDataViewColumnList
)
3977 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3978 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3979 EVT_SIZE(wxDataViewCtrl::OnSize
)
3982 wxDataViewCtrl::~wxDataViewCtrl()
3985 GetModel()->RemoveNotifier( m_notifier
);
3988 m_colsBestWidths
.clear();
3991 void wxDataViewCtrl::Init()
3993 m_cols
.DeleteContents(true);
3996 // No sorting column at start
3997 m_sortingColumnIdx
= wxNOT_FOUND
;
3999 m_headerArea
= NULL
;
4001 m_colsDirty
= false;
4004 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4009 const wxValidator
& validator
,
4010 const wxString
& name
)
4012 // if ( (style & wxBORDER_MASK) == 0)
4013 // style |= wxBORDER_SUNKEN;
4017 if (!wxControl::Create( parent
, id
, pos
, size
,
4018 style
| wxScrolledWindowStyle
, validator
, name
))
4021 SetInitialSize(size
);
4024 MacSetClipChildren( true );
4027 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4029 // We use the cursor keys for moving the selection, not scrolling, so call
4030 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4031 // keyboard events forwarded to us from wxListMainWindow.
4032 DisableKeyboardScrolling();
4034 if (HasFlag(wxDV_NO_HEADER
))
4035 m_headerArea
= NULL
;
4037 m_headerArea
= new wxDataViewHeaderWindow(this);
4039 SetTargetWindow( m_clientArea
);
4041 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4043 sizer
->Add( m_headerArea
, 0, wxGROW
);
4044 sizer
->Add( m_clientArea
, 1, wxGROW
);
4050 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4052 return wxBORDER_THEME
;
4056 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4060 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4063 // we need to process arrows ourselves for scrolling
4064 if ( nMsg
== WM_GETDLGCODE
)
4066 rc
|= DLGC_WANTARROWS
;
4074 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4076 wxSize newsize
= size
;
4077 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4078 newsize
.y
-= m_headerArea
->GetSize().y
;
4083 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4085 // We need to override OnSize so that our scrolled
4086 // window a) does call Layout() to use sizers for
4087 // positioning the controls but b) does not query
4088 // the sizer for their size and use that for setting
4089 // the scrollable area as set that ourselves by
4090 // calling SetScrollbar() further down.
4096 // We must redraw the headers if their height changed. Normally this
4097 // shouldn't happen as the control shouldn't let itself be resized beneath
4098 // its minimal height but avoid the display artefacts that appear if it
4099 // does happen, e.g. because there is really not enough vertical space.
4100 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4101 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4103 m_headerArea
->Refresh();
4107 void wxDataViewCtrl::SetFocus()
4110 m_clientArea
->SetFocus();
4113 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4115 if (!wxDataViewCtrlBase::AssociateModel( model
))
4118 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4120 model
->AddNotifier( m_notifier
);
4122 m_clientArea
->DestroyTree();
4124 m_clientArea
->BuildTree(model
);
4126 m_clientArea
->UpdateDisplay();
4131 #if wxUSE_DRAG_AND_DROP
4133 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4135 return m_clientArea
->EnableDragSource( format
);
4138 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4140 return m_clientArea
->EnableDropTarget( format
);
4143 #endif // wxUSE_DRAG_AND_DROP
4145 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4147 if (!wxDataViewCtrlBase::AppendColumn(col
))
4150 m_cols
.Append( col
);
4151 m_colsBestWidths
.push_back(0);
4152 OnColumnsCountChanged();
4156 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4158 if (!wxDataViewCtrlBase::PrependColumn(col
))
4161 m_cols
.Insert( col
);
4162 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4163 OnColumnsCountChanged();
4167 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4169 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4172 m_cols
.Insert( pos
, col
);
4173 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4174 OnColumnsCountChanged();
4178 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4181 m_headerArea
->UpdateColumn(idx
);
4183 m_clientArea
->UpdateDisplay();
4186 void wxDataViewCtrl::OnColumnsCountChanged()
4189 m_headerArea
->SetColumnCount(GetColumnCount());
4191 m_clientArea
->UpdateDisplay();
4194 void wxDataViewCtrl::DoSetExpanderColumn()
4196 m_clientArea
->UpdateDisplay();
4199 void wxDataViewCtrl::DoSetIndent()
4201 m_clientArea
->UpdateDisplay();
4204 unsigned int wxDataViewCtrl::GetColumnCount() const
4206 return m_cols
.GetCount();
4209 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4211 if ( !m_clientArea
)
4214 m_clientArea
->SetRowHeight(lineHeight
);
4219 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4224 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4226 // columns can't be reordered if there is no header window which allows
4228 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4231 return GetColumn(idx
);
4234 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4236 const unsigned count
= m_cols
.size();
4237 for ( unsigned n
= 0; n
< count
; n
++ )
4239 if ( m_cols
[n
] == column
)
4246 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4248 if ( m_colsBestWidths
[idx
] != 0 )
4249 return m_colsBestWidths
[idx
];
4251 const int count
= m_clientArea
->GetRowCount();
4252 wxDataViewColumn
*column
= GetColumn(idx
);
4253 wxDataViewRenderer
*renderer
=
4254 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4256 class MaxWidthCalculator
4259 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4260 wxDataViewRenderer
*renderer
,
4261 const wxDataViewModel
*model
,
4264 m_clientArea(clientArea
),
4265 m_renderer(renderer
),
4271 void UpdateWithWidth(int width
)
4273 m_width
= wxMax(m_width
, width
);
4276 void UpdateWithRow(int row
)
4278 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4279 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4280 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4283 int GetMaxWidth() const { return m_width
; }
4287 wxDataViewMainWindow
*m_clientArea
;
4288 wxDataViewRenderer
*m_renderer
;
4289 const wxDataViewModel
*m_model
;
4293 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4294 GetModel(), column
->GetModelColumn());
4298 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4299 // Labels on native MSW header are indented on both sides
4301 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4302 calculator
.UpdateWithWidth(header_width
);
4305 // The code below deserves some explanation. For very large controls, we
4306 // simply can't afford to calculate sizes for all items, it takes too
4307 // long. So the best we can do is to check the first and the last N/2
4308 // items in the control for some sufficiently large N and calculate best
4309 // sizes from that. That can result in the calculated best width being too
4310 // small for some outliers, but it's better to get slightly imperfect
4311 // result than to wait several seconds after every update. To avoid highly
4312 // visible miscalculations, we also include all currently visible items
4313 // no matter what. Finally, the value of N is determined dynamically by
4314 // measuring how much time we spent on the determining item widths so far.
4317 int top_part_end
= count
;
4318 static const long CALC_TIMEOUT
= 20/*ms*/;
4319 // don't call wxStopWatch::Time() too often
4320 static const unsigned CALC_CHECK_FREQ
= 100;
4323 // use some hard-coded limit, that's the best we can do without timer
4324 int top_part_end
= wxMin(500, count
);
4325 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4329 for ( row
= 0; row
< top_part_end
; row
++ )
4332 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4333 timer
.Time() > CALC_TIMEOUT
)
4335 #endif // wxUSE_STOPWATCH
4336 calculator
.UpdateWithRow(row
);
4339 // row is the first unmeasured item now; that's our value of N/2
4345 // add bottom N/2 items now:
4346 const int bottom_part_start
= wxMax(row
, count
- row
);
4347 for ( row
= bottom_part_start
; row
< count
; row
++ )
4349 calculator
.UpdateWithRow(row
);
4352 // finally, include currently visible items in the calculation:
4353 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4354 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4355 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4357 first_visible
= wxMax(first_visible
, top_part_end
);
4358 last_visible
= wxMin(bottom_part_start
, last_visible
);
4360 for ( row
= first_visible
; row
< last_visible
; row
++ )
4362 calculator
.UpdateWithRow(row
);
4365 wxLogTrace("dataview",
4366 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4368 count
- bottom_part_start
,
4369 wxMax(0, last_visible
- first_visible
),
4373 int max_width
= calculator
.GetMaxWidth();
4374 if ( max_width
> 0 )
4375 max_width
+= 2 * PADDING_RIGHTLEFT
;
4377 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4381 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4382 unsigned int WXUNUSED(new_pos
))
4384 // do _not_ reorder m_cols elements here, they should always be in the
4385 // order in which columns were added, we only display the columns in
4387 m_clientArea
->UpdateDisplay();
4390 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4392 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4396 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4398 OnColumnsCountChanged();
4403 bool wxDataViewCtrl::ClearColumns()
4405 SetExpanderColumn(NULL
);
4407 m_colsBestWidths
.clear();
4408 OnColumnsCountChanged();
4412 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4414 m_colsBestWidths
[idx
] = 0;
4418 void wxDataViewCtrl::InvalidateColBestWidths()
4420 m_colsBestWidths
.clear();
4421 m_colsBestWidths
.resize(m_cols
.size());
4425 void wxDataViewCtrl::UpdateColWidths()
4427 if ( !m_headerArea
)
4430 const unsigned len
= m_colsBestWidths
.size();
4431 for ( unsigned i
= 0; i
< len
; i
++ )
4433 if ( m_colsBestWidths
[i
] == 0 )
4434 m_headerArea
->UpdateColumn(i
);
4438 void wxDataViewCtrl::OnInternalIdle()
4440 wxDataViewCtrlBase::OnInternalIdle();
4444 m_colsDirty
= false;
4449 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4452 unsigned int len
= GetColumnCount();
4453 for ( unsigned int i
= 0; i
< len
; i
++ )
4455 wxDataViewColumn
* col
= GetColumnAt(i
);
4462 // This returns the position in pixels which is not what we want.
4465 unsigned int len
= GetColumnCount();
4466 for ( unsigned int i
= 0; i
< len
; i
++ )
4468 wxDataViewColumn
* col
= GetColumnAt(i
);
4469 if (col
->IsHidden())
4471 ret
+= col
->GetWidth();
4474 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4482 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4484 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4485 : GetColumn(m_sortingColumnIdx
);
4488 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4490 return GetItemByRow(m_clientArea
->GetCurrentRow());
4493 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4495 const int row
= m_clientArea
->GetRowByItem(item
);
4497 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4498 if ( static_cast<unsigned>(row
) != oldCurrent
)
4500 m_clientArea
->ChangeCurrentRow(row
);
4501 m_clientArea
->RefreshRow(oldCurrent
);
4502 m_clientArea
->RefreshRow(row
);
4506 int wxDataViewCtrl::GetSelectedItemsCount() const
4508 return m_clientArea
->GetSelections().size();
4511 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4514 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4516 const size_t len
= selections
.size();
4517 for ( size_t i
= 0; i
< len
; i
++ )
4519 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4526 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4533 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4535 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4537 wxDataViewItem last_parent
;
4539 int len
= sel
.GetCount();
4540 for( int i
= 0; i
< len
; i
++ )
4542 wxDataViewItem item
= sel
[i
];
4543 wxDataViewItem parent
= GetModel()->GetParent( item
);
4546 if (parent
!= last_parent
)
4547 ExpandAncestors(item
);
4550 last_parent
= parent
;
4551 int row
= m_clientArea
->GetRowByItem( item
);
4553 selection
.Add( static_cast<unsigned int>(row
) );
4556 m_clientArea
->SetSelections( selection
);
4559 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4561 ExpandAncestors( item
);
4563 int row
= m_clientArea
->GetRowByItem( item
);
4566 // Unselect all rows before select another in the single select mode
4567 if (m_clientArea
->IsSingleSel())
4568 m_clientArea
->SelectAllRows(false);
4570 m_clientArea
->SelectRow(row
, true);
4572 // Also set focus to the selected item
4573 m_clientArea
->ChangeCurrentRow( row
);
4577 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4579 int row
= m_clientArea
->GetRowByItem( item
);
4581 m_clientArea
->SelectRow(row
, false);
4584 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4586 int row
= m_clientArea
->GetRowByItem( item
);
4589 return m_clientArea
->IsRowSelected(row
);
4594 void wxDataViewCtrl::SelectAll()
4596 m_clientArea
->SelectAllRows(true);
4599 void wxDataViewCtrl::UnselectAll()
4601 m_clientArea
->SelectAllRows(false);
4604 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4608 if( row
> (int) m_clientArea
->GetRowCount() )
4609 row
= m_clientArea
->GetRowCount();
4611 int first
= m_clientArea
->GetFirstVisibleRow();
4612 int last
= m_clientArea
->GetLastVisibleRow();
4614 m_clientArea
->ScrollTo( row
, column
);
4615 else if( row
> last
)
4616 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4618 m_clientArea
->ScrollTo( first
, column
);
4621 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4623 ExpandAncestors( item
);
4625 m_clientArea
->RecalculateDisplay();
4627 int row
= m_clientArea
->GetRowByItem(item
);
4630 if( column
== NULL
)
4631 EnsureVisible(row
, -1);
4633 EnsureVisible( row
, GetColumnIndex(column
) );
4638 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4639 wxDataViewColumn
* &column
) const
4641 m_clientArea
->HitTest(point
, item
, column
);
4644 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4645 const wxDataViewColumn
* column
) const
4647 return m_clientArea
->GetItemRect(item
, column
);
4650 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4652 return m_clientArea
->GetItemByRow( row
);
4655 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4657 return m_clientArea
->GetRowByItem( item
);
4660 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4662 ExpandAncestors( item
);
4664 int row
= m_clientArea
->GetRowByItem( item
);
4666 m_clientArea
->Expand(row
);
4669 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4671 int row
= m_clientArea
->GetRowByItem( item
);
4673 m_clientArea
->Collapse(row
);
4676 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4678 int row
= m_clientArea
->GetRowByItem( item
);
4680 return m_clientArea
->IsExpanded(row
);
4684 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4686 wxDataViewColumn
* col
= GetColumn( column
);
4690 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4691 if (renderer
->GetMode() != wxDATAVIEW_CELL_EDITABLE
)
4694 const wxRect itemRect
= GetItemRect(item
, col
);
4695 renderer
->StartEditing(item
, itemRect
);
4698 #endif // !wxUSE_GENERICDATAVIEWCTRL
4700 #endif // wxUSE_DATAVIEWCTRL