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 //-----------------------------------------------------------------------------
90 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
97 m_sortAscending
= true;
100 int wxDataViewColumn::GetWidth() const
104 case wxCOL_WIDTH_DEFAULT
:
105 return wxDVC_DEFAULT_WIDTH
;
107 case wxCOL_WIDTH_AUTOSIZE
:
108 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
109 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
116 void wxDataViewColumn::UpdateDisplay()
120 int idx
= m_owner
->GetColumnIndex( this );
121 m_owner
->OnColumnChange( idx
);
125 //-----------------------------------------------------------------------------
126 // wxDataViewHeaderWindow
127 //-----------------------------------------------------------------------------
129 class wxDataViewHeaderWindow
: public wxHeaderCtrl
132 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
133 : wxHeaderCtrl(parent
)
137 wxDataViewCtrl
*GetOwner() const
138 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
141 // implement/override wxHeaderCtrl functions by forwarding them to the main
143 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
145 return *(GetOwner()->GetColumn(idx
));
148 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
150 wxDataViewCtrl
* const owner
= GetOwner();
152 int widthContents
= owner
->GetBestColumnWidth(idx
);
153 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
154 owner
->OnColumnChange(idx
);
160 bool SendEvent(wxEventType type
, unsigned int n
)
162 wxDataViewCtrl
* const owner
= GetOwner();
163 wxDataViewEvent
event(type
, owner
->GetId());
165 event
.SetEventObject(owner
);
167 event
.SetDataViewColumn(owner
->GetColumn(n
));
168 event
.SetModel(owner
->GetModel());
170 // for events created by wxDataViewHeaderWindow the
171 // row / value fields are not valid
172 return owner
->GetEventHandler()->ProcessEvent(event
);
175 void OnClick(wxHeaderCtrlEvent
& event
)
177 const unsigned idx
= event
.GetColumn();
179 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
182 // default handling for the column click is to sort by this column or
183 // toggle its sort order
184 wxDataViewCtrl
* const owner
= GetOwner();
185 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
186 if ( !col
->IsSortable() )
188 // no default handling for non-sortable columns
193 if ( col
->IsSortKey() )
195 // already using this column for sorting, just change the order
196 col
->ToggleSortOrder();
198 else // not using this column for sorting yet
200 // first unset the old sort column if any
201 int oldSortKey
= owner
->GetSortingColumnIndex();
202 if ( oldSortKey
!= wxNOT_FOUND
)
204 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
205 owner
->OnColumnChange(oldSortKey
);
208 owner
->SetSortingColumnIndex(idx
);
212 wxDataViewModel
* const model
= owner
->GetModel();
216 owner
->OnColumnChange(idx
);
218 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
221 void OnRClick(wxHeaderCtrlEvent
& event
)
223 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
228 void OnResize(wxHeaderCtrlEvent
& event
)
230 wxDataViewCtrl
* const owner
= GetOwner();
232 const unsigned col
= event
.GetColumn();
233 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
234 GetOwner()->OnColumnChange(col
);
237 void OnEndReorder(wxHeaderCtrlEvent
& event
)
239 wxDataViewCtrl
* const owner
= GetOwner();
240 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
241 event
.GetNewOrder());
244 DECLARE_EVENT_TABLE()
245 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
248 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
249 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
250 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
252 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
253 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
255 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
258 //-----------------------------------------------------------------------------
259 // wxDataViewRenameTimer
260 //-----------------------------------------------------------------------------
262 class wxDataViewRenameTimer
: public wxTimer
265 wxDataViewMainWindow
*m_owner
;
268 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
272 //-----------------------------------------------------------------------------
273 // wxDataViewTreeNode
274 //-----------------------------------------------------------------------------
276 class wxDataViewTreeNode
;
277 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
279 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
280 wxDataViewTreeNode
** node2
);
282 class wxDataViewTreeNode
285 wxDataViewTreeNode(wxDataViewTreeNode
*parent
, const wxDataViewItem
& item
)
292 ~wxDataViewTreeNode()
296 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
297 for ( wxDataViewTreeNodes::iterator i
= nodes
.begin();
308 static wxDataViewTreeNode
* CreateRootNode()
310 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(NULL
, wxDataViewItem());
311 n
->SetHasChildren(true);
312 n
->m_branchData
->open
= true;
316 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
318 const wxDataViewTreeNodes
& GetChildNodes() const
320 wxASSERT( m_branchData
!= NULL
);
321 return m_branchData
->children
;
324 void InsertChild(wxDataViewTreeNode
*node
, unsigned index
)
327 m_branchData
= new BranchNodeData
;
329 m_branchData
->children
.Insert(node
, index
);
331 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
333 m_branchData
->children
.Sort( &wxGenericTreeModelNodeCmp
);
336 void RemoveChild(wxDataViewTreeNode
*node
)
338 wxCHECK_RET( m_branchData
!= NULL
, "leaf node doesn't have children" );
339 m_branchData
->children
.Remove(node
);
342 const wxDataViewItem
& GetItem() const { return m_item
; }
343 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
345 int GetIndentLevel() const
348 const wxDataViewTreeNode
* node
= this;
349 while( node
->GetParent()->GetParent() != NULL
)
351 node
= node
->GetParent();
359 return m_branchData
&& m_branchData
->open
;
364 wxCHECK_RET( m_branchData
!= NULL
, "can't open leaf node" );
368 const wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
369 const int len
= nodes
.GetCount();
370 for ( int i
= 0;i
< len
; i
++)
371 sum
+= 1 + nodes
[i
]->GetSubTreeCount();
373 if (m_branchData
->open
)
375 ChangeSubTreeCount(-sum
);
376 m_branchData
->open
= !m_branchData
->open
;
380 m_branchData
->open
= !m_branchData
->open
;
381 ChangeSubTreeCount(+sum
);
385 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
386 // even if GetChildNodes() is empty; see below.
387 bool HasChildren() const
389 return m_branchData
!= NULL
;
392 void SetHasChildren(bool has
)
396 wxDELETE(m_branchData
);
398 else if ( m_branchData
== NULL
)
400 m_branchData
= new BranchNodeData
;
404 int GetSubTreeCount() const
406 return m_branchData
? m_branchData
->subTreeCount
: 0;
409 void ChangeSubTreeCount( int num
)
411 wxASSERT( m_branchData
!= NULL
);
413 if( !m_branchData
->open
)
416 m_branchData
->subTreeCount
+= num
;
417 wxASSERT( m_branchData
->subTreeCount
>= 0 );
420 m_parent
->ChangeSubTreeCount(num
);
430 wxDataViewTreeNodes
& nodes
= m_branchData
->children
;
432 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
433 int len
= nodes
.GetCount();
434 for (int i
= 0; i
< len
; i
++)
436 if ( nodes
[i
]->HasChildren() )
444 wxDataViewTreeNode
*m_parent
;
446 // Corresponding model item.
447 wxDataViewItem m_item
;
449 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
450 // separate struct in order to conserve memory.
451 struct BranchNodeData
459 // Child nodes. Note that this may be empty even if m_hasChildren in
460 // case this branch of the tree wasn't expanded and realized yet.
461 wxDataViewTreeNodes children
;
463 // Is the branch node currently open (expanded)?
466 // Total count of expanded (i.e. visible with the help of some
467 // scrolling) items in the subtree, but excluding this node. I.e. it is
468 // 0 for leaves and is the number of rows the subtree occupies for
473 BranchNodeData
*m_branchData
;
477 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
478 wxDataViewTreeNode
** node2
)
480 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
484 //-----------------------------------------------------------------------------
485 // wxDataViewMainWindow
486 //-----------------------------------------------------------------------------
488 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
490 class wxDataViewMainWindow
: public wxWindow
493 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
495 const wxPoint
&pos
= wxDefaultPosition
,
496 const wxSize
&size
= wxDefaultSize
,
497 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
498 virtual ~wxDataViewMainWindow();
500 bool IsList() const { return GetModel()->IsListModel(); }
501 bool IsVirtualList() const { return m_root
== NULL
; }
503 // notifications from wxDataViewModel
504 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
505 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
506 bool ItemChanged( const wxDataViewItem
&item
);
507 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
511 if (!IsVirtualList())
521 g_model
= GetModel();
522 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
525 if (g_model
->HasDefaultCompare())
533 g_column
= col
->GetModelColumn();
534 g_asending
= col
->IsSortOrderAscending();
537 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
538 wxDataViewCtrl
*GetOwner() { return m_owner
; }
539 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
541 wxDataViewModel
* GetModel() { return GetOwner()->GetModel(); }
542 const wxDataViewModel
* GetModel() const { return GetOwner()->GetModel(); }
544 #if wxUSE_DRAG_AND_DROP
545 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
546 #endif // wxUSE_DRAG_AND_DROP
547 void OnPaint( wxPaintEvent
&event
);
548 void OnChar( wxKeyEvent
&event
);
549 void OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
);
550 void OnMouse( wxMouseEvent
&event
);
551 void OnSetFocus( wxFocusEvent
&event
);
552 void OnKillFocus( wxFocusEvent
&event
);
554 void UpdateDisplay();
555 void RecalculateDisplay();
556 void OnInternalIdle();
558 void OnRenameTimer();
560 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
561 void ScrollTo( int rows
, int column
);
563 unsigned GetCurrentRow() const { return m_currentRow
; }
564 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
565 void ChangeCurrentRow( unsigned int row
);
567 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
568 bool IsEmpty() { return GetRowCount() == 0; }
570 int GetCountPerPage() const;
571 int GetEndOfLastCol() const;
572 unsigned int GetFirstVisibleRow() const;
574 // I change this method to un const because in the tree view,
575 // the displaying number of the tree are changing along with the
576 // expanding/collapsing of the tree nodes
577 unsigned int GetLastVisibleRow();
578 unsigned int GetRowCount();
580 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
581 void SetSelections( const wxDataViewSelection
& sel
)
582 { m_selection
= sel
; UpdateDisplay(); }
583 void Select( const wxArrayInt
& aSelections
);
584 void SelectAllRows( bool on
);
585 void SelectRow( unsigned int row
, bool on
);
586 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
587 void ReverseRowSelection( unsigned int row
);
588 bool IsRowSelected( unsigned int row
);
589 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
591 void RefreshRow( unsigned int row
);
592 void RefreshRows( unsigned int from
, unsigned int to
);
593 void RefreshRowsAfter( unsigned int firstRow
);
595 // returns the colour to be used for drawing the rules
596 wxColour
GetRuleColour() const
598 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
601 wxRect
GetLineRect( unsigned int row
) const;
603 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
604 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
605 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
607 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
609 // Some useful functions for row and item mapping
610 wxDataViewItem
GetItemByRow( unsigned int row
) const;
611 int GetRowByItem( const wxDataViewItem
& item
) const;
613 // Methods for building the mapping tree
614 void BuildTree( wxDataViewModel
* model
);
616 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
617 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
619 void Expand( unsigned int row
);
620 void Collapse( unsigned int row
);
621 bool IsExpanded( unsigned int row
) const;
622 bool HasChildren( unsigned int row
) const;
624 #if wxUSE_DRAG_AND_DROP
625 bool EnableDragSource( const wxDataFormat
&format
);
626 bool EnableDropTarget( const wxDataFormat
&format
);
628 void RemoveDropHint();
629 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
630 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
631 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
633 #endif // wxUSE_DRAG_AND_DROP
636 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
637 // We did not need this temporarily
638 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
640 int RecalculateCount();
642 // Return false only if the event was vetoed by its handler.
643 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
645 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
648 wxDataViewCtrl
*m_owner
;
652 wxDataViewColumn
*m_currentCol
;
653 unsigned int m_currentRow
;
654 wxDataViewSelection m_selection
;
656 wxDataViewRenameTimer
*m_renameTimer
;
661 #if wxUSE_DRAG_AND_DROP
666 wxDataFormat m_dragFormat
;
669 wxDataFormat m_dropFormat
;
671 unsigned int m_dropHintLine
;
672 #endif // wxUSE_DRAG_AND_DROP
674 // for double click logic
675 unsigned int m_lineLastClicked
,
676 m_lineBeforeLastClicked
,
677 m_lineSelectSingleOnUp
;
679 // the pen used to draw horiz/vertical rules
682 // the pen used to draw the expander and the lines
685 // This is the tree structure of the model
686 wxDataViewTreeNode
* m_root
;
689 // This is the tree node under the cursor
690 wxDataViewTreeNode
* m_underMouse
;
693 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
694 DECLARE_EVENT_TABLE()
697 // ---------------------------------------------------------
698 // wxGenericDataViewModelNotifier
699 // ---------------------------------------------------------
701 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
704 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
705 { m_mainWindow
= mainWindow
; }
707 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
708 { return m_mainWindow
->ItemAdded( parent
, item
); }
709 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
710 { return m_mainWindow
->ItemDeleted( parent
, item
); }
711 virtual bool ItemChanged( const wxDataViewItem
& item
)
712 { return m_mainWindow
->ItemChanged(item
); }
713 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
714 { return m_mainWindow
->ValueChanged( item
, col
); }
715 virtual bool Cleared()
716 { return m_mainWindow
->Cleared(); }
717 virtual void Resort()
718 { m_mainWindow
->Resort(); }
720 wxDataViewMainWindow
*m_mainWindow
;
723 // ---------------------------------------------------------
724 // wxDataViewRenderer
725 // ---------------------------------------------------------
727 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
729 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
730 wxDataViewCellMode mode
,
732 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
736 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
740 wxDataViewRenderer::~wxDataViewRenderer()
745 wxDC
*wxDataViewRenderer::GetDC()
749 if (GetOwner() == NULL
)
751 if (GetOwner()->GetOwner() == NULL
)
753 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
759 void wxDataViewRenderer::SetAlignment( int align
)
764 int wxDataViewRenderer::GetAlignment() const
769 // ---------------------------------------------------------
770 // wxDataViewCustomRenderer
771 // ---------------------------------------------------------
773 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
775 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
776 wxDataViewCellMode mode
, int align
) :
777 wxDataViewRenderer( varianttype
, mode
, align
)
781 // ---------------------------------------------------------
782 // wxDataViewTextRenderer
783 // ---------------------------------------------------------
785 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
787 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
788 wxDataViewCellMode mode
, int align
) :
789 wxDataViewRenderer( varianttype
, mode
, align
)
793 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
795 m_text
= value
.GetString();
800 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
805 bool wxDataViewTextRenderer::HasEditorCtrl() const
810 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
811 wxRect labelRect
, const wxVariant
&value
)
813 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
814 wxPoint(labelRect
.x
,labelRect
.y
),
815 wxSize(labelRect
.width
,labelRect
.height
),
816 wxTE_PROCESS_ENTER
);
818 // select the text in the control an place the cursor at the end
819 ctrl
->SetInsertionPointEnd();
825 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
827 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
828 value
= text
->GetValue();
832 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
834 RenderText(m_text
, 0, rect
, dc
, state
);
838 wxSize
wxDataViewTextRenderer::GetSize() const
841 return GetTextExtent(m_text
);
843 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
846 // ---------------------------------------------------------
847 // wxDataViewBitmapRenderer
848 // ---------------------------------------------------------
850 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
852 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
853 wxDataViewCellMode mode
, int align
) :
854 wxDataViewRenderer( varianttype
, mode
, align
)
858 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
860 if (value
.GetType() == wxT("wxBitmap"))
862 if (value
.GetType() == wxT("wxIcon"))
868 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
873 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
876 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
877 else if (m_icon
.IsOk())
878 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
883 wxSize
wxDataViewBitmapRenderer::GetSize() const
886 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
887 else if (m_icon
.IsOk())
888 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
890 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
893 // ---------------------------------------------------------
894 // wxDataViewToggleRenderer
895 // ---------------------------------------------------------
897 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
899 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
900 wxDataViewCellMode mode
, int align
) :
901 wxDataViewRenderer( varianttype
, mode
, align
)
906 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
908 m_toggle
= value
.GetBool();
913 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
918 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
922 flags
|= wxCONTROL_CHECKED
;
923 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
924 GetEnabled() == false)
925 flags
|= wxCONTROL_DISABLED
;
927 // check boxes we draw must always have the same, standard size (if it's
928 // bigger than the cell size the checkbox will be truncated because the
929 // caller had set the clipping rectangle to prevent us from drawing outside
931 cell
.SetSize(GetSize());
933 wxRendererNative::Get().DrawCheckBox(
934 GetOwner()->GetOwner(),
942 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
944 wxDataViewModel
*model
,
945 const wxDataViewItem
& item
,
948 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
949 if (!wxRect(GetSize()).Contains(cursor
))
952 return WXOnActivate(cell
, model
, item
, col
);
955 bool wxDataViewToggleRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
956 wxDataViewModel
*model
,
957 const wxDataViewItem
& item
,
960 if (model
->IsEnabled(item
, col
))
962 model
->ChangeValue(!m_toggle
, item
, col
);
969 wxSize
wxDataViewToggleRenderer::GetSize() const
971 // the window parameter is not used by GetCheckBoxSize() so it's
973 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
976 // ---------------------------------------------------------
977 // wxDataViewProgressRenderer
978 // ---------------------------------------------------------
980 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
982 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
983 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
984 wxDataViewRenderer( varianttype
, mode
, align
)
990 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
992 m_value
= (long) value
;
994 if (m_value
< 0) m_value
= 0;
995 if (m_value
> 100) m_value
= 100;
1000 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1002 value
= (long) m_value
;
1007 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1009 // deflate the rect to leave a small border between bars in adjacent rows
1010 wxRect bar
= rect
.Deflate(0, 1);
1012 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1013 dc
->SetPen( *wxBLACK_PEN
);
1014 dc
->DrawRectangle( bar
);
1016 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1017 dc
->SetPen( *wxTRANSPARENT_PEN
);
1019 const wxDataViewItemAttr
& attr
= GetAttr();
1020 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1022 dc
->DrawRectangle( bar
);
1027 wxSize
wxDataViewProgressRenderer::GetSize() const
1029 return wxSize(40,12);
1032 // ---------------------------------------------------------
1033 // wxDataViewDateRenderer
1034 // ---------------------------------------------------------
1036 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
1038 #if wxUSE_DATE_RENDERER_POPUP
1040 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1043 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1044 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
1045 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
1050 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
1051 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1052 sizer
->Add( m_cal
, 1, wxGROW
);
1057 void OnCalendar( wxCalendarEvent
&event
);
1059 wxCalendarCtrl
*m_cal
;
1060 wxDataViewModel
*m_model
;
1062 const wxDataViewItem
& m_item
;
1065 virtual void OnDismiss()
1070 DECLARE_EVENT_TABLE()
1073 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1074 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1077 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1079 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1083 #endif // wxUSE_DATE_RENDERER_POPUP
1085 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1087 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1088 wxDataViewCellMode mode
, int align
) :
1089 wxDataViewRenderer( varianttype
, mode
, align
)
1093 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1095 m_date
= value
.GetDateTime();
1100 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1106 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1108 wxString tmp
= m_date
.FormatDate();
1109 RenderText( tmp
, 0, cell
, dc
, state
);
1113 wxSize
wxDataViewDateRenderer::GetSize() const
1115 return GetTextExtent(m_date
.FormatDate());
1118 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1119 wxDataViewModel
*model
,
1120 const wxDataViewItem
& item
,
1123 wxDateTime dtOld
= m_date
;
1125 #if wxUSE_DATE_RENDERER_POPUP
1126 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1127 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1128 wxPoint pos
= wxGetMousePosition();
1131 popup
->Popup( popup
->m_cal
);
1132 #else // !wxUSE_DATE_RENDERER_POPUP
1133 wxMessageBox(dtOld
.Format());
1134 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1139 // ---------------------------------------------------------
1140 // wxDataViewIconTextRenderer
1141 // ---------------------------------------------------------
1143 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1145 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1146 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1147 wxDataViewRenderer( varianttype
, mode
, align
)
1150 SetAlignment(align
);
1153 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1159 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1164 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1168 const wxIcon
& icon
= m_value
.GetIcon();
1171 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1172 xoffset
= icon
.GetWidth()+4;
1175 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1180 wxSize
wxDataViewIconTextRenderer::GetSize() const
1182 if (!m_value
.GetText().empty())
1184 wxSize size
= GetTextExtent(m_value
.GetText());
1186 if (m_value
.GetIcon().IsOk())
1187 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1190 return wxSize(80,20);
1193 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1195 wxDataViewIconText iconText
;
1198 wxString text
= iconText
.GetText();
1200 // adjust the label rect to take the width of the icon into account
1201 if (iconText
.GetIcon().IsOk())
1203 int w
= iconText
.GetIcon().GetWidth() + 4;
1205 labelRect
.width
-= w
;
1208 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1209 wxPoint(labelRect
.x
,labelRect
.y
),
1210 wxSize(labelRect
.width
,labelRect
.height
),
1211 wxTE_PROCESS_ENTER
);
1213 // select the text in the control an place the cursor at the end
1214 ctrl
->SetInsertionPointEnd();
1220 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1222 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1224 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1229 //-----------------------------------------------------------------------------
1230 // wxDataViewDropTarget
1231 //-----------------------------------------------------------------------------
1233 #if wxUSE_DRAG_AND_DROP
1235 class wxBitmapCanvas
: public wxWindow
1238 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1239 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1242 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1245 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1248 dc
.DrawBitmap( m_bitmap
, 0, 0);
1254 class wxDataViewDropSource
: public wxDropSource
1257 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1265 ~wxDataViewDropSource()
1270 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1272 wxPoint pos
= wxGetMousePosition();
1276 int liney
= m_win
->GetLineStart( m_row
);
1278 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1279 m_win
->ClientToScreen( &linex
, &liney
);
1280 m_dist_x
= pos
.x
- linex
;
1281 m_dist_y
= pos
.y
- liney
;
1284 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1286 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1287 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1289 wxFRAME_TOOL_WINDOW
|
1290 wxFRAME_FLOAT_ON_PARENT
|
1291 wxFRAME_NO_TASKBAR
|
1293 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1298 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1299 m_hint
->SetTransparent( 128 );
1305 wxDataViewMainWindow
*m_win
;
1308 int m_dist_x
,m_dist_y
;
1312 class wxDataViewDropTarget
: public wxDropTarget
1315 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1321 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1323 wxDataFormat format
= GetMatchingPair();
1324 if (format
== wxDF_INVALID
)
1326 return m_win
->OnDragOver( format
, x
, y
, def
);
1329 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1331 wxDataFormat format
= GetMatchingPair();
1332 if (format
== wxDF_INVALID
)
1334 return m_win
->OnDrop( format
, x
, y
);
1337 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1339 wxDataFormat format
= GetMatchingPair();
1340 if (format
== wxDF_INVALID
)
1344 return m_win
->OnData( format
, x
, y
, def
);
1347 virtual void OnLeave()
1348 { m_win
->OnLeave(); }
1350 wxDataViewMainWindow
*m_win
;
1353 #endif // wxUSE_DRAG_AND_DROP
1355 //-----------------------------------------------------------------------------
1356 // wxDataViewRenameTimer
1357 //-----------------------------------------------------------------------------
1359 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1364 void wxDataViewRenameTimer::Notify()
1366 m_owner
->OnRenameTimer();
1369 //-----------------------------------------------------------------------------
1370 // wxDataViewMainWindow
1371 //-----------------------------------------------------------------------------
1373 // The tree building helper, declared firstly
1374 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1375 wxDataViewTreeNode
* node
);
1377 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1379 if (row1
> row2
) return 1;
1380 if (row1
== row2
) return 0;
1384 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1386 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1387 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1388 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1389 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1390 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1391 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1394 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1395 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1396 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1397 m_selection( wxDataViewSelectionCmp
)
1402 m_lastOnSame
= false;
1403 m_renameTimer
= new wxDataViewRenameTimer( this );
1405 // TODO: user better initial values/nothing selected
1406 m_currentCol
= NULL
;
1409 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1411 #if wxUSE_DRAG_AND_DROP
1413 m_dragStart
= wxPoint(0,0);
1415 m_dragEnabled
= false;
1416 m_dropEnabled
= false;
1418 m_dropHintLine
= (unsigned int) -1;
1419 #endif // wxUSE_DRAG_AND_DROP
1421 m_lineLastClicked
= (unsigned int) -1;
1422 m_lineBeforeLastClicked
= (unsigned int) -1;
1423 m_lineSelectSingleOnUp
= (unsigned int) -1;
1427 SetBackgroundColour( *wxWHITE
);
1429 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1431 m_penRule
= wxPen(GetRuleColour());
1433 // compose a pen whichcan draw black lines
1434 // TODO: maybe there is something system colour to use
1435 m_penExpander
= wxPen(wxColour(0,0,0));
1437 m_root
= wxDataViewTreeNode::CreateRootNode();
1439 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1441 m_underMouse
= NULL
;
1445 wxDataViewMainWindow::~wxDataViewMainWindow()
1448 delete m_renameTimer
;
1452 #if wxUSE_DRAG_AND_DROP
1453 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1455 m_dragFormat
= format
;
1456 m_dragEnabled
= format
!= wxDF_INVALID
;
1461 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1463 m_dropFormat
= format
;
1464 m_dropEnabled
= format
!= wxDF_INVALID
;
1467 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1472 void wxDataViewMainWindow::RemoveDropHint()
1477 RefreshRow( m_dropHintLine
);
1478 m_dropHintLine
= (unsigned int) -1;
1482 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1483 wxCoord y
, wxDragResult def
)
1487 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1488 unsigned int row
= GetLineAt( yy
);
1490 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1496 wxDataViewItem item
= GetItemByRow( row
);
1498 wxDataViewModel
*model
= GetModel();
1500 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1501 event
.SetEventObject( m_owner
);
1502 event
.SetItem( item
);
1503 event
.SetModel( model
);
1504 event
.SetDataFormat( format
);
1505 if (!m_owner
->HandleWindowEvent( event
))
1511 if (!event
.IsAllowed())
1518 if (m_dropHint
&& (row
!= m_dropHintLine
))
1519 RefreshRow( m_dropHintLine
);
1521 m_dropHintLine
= row
;
1527 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1533 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1534 unsigned int row
= GetLineAt( yy
);
1536 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1539 wxDataViewItem item
= GetItemByRow( row
);
1541 wxDataViewModel
*model
= GetModel();
1543 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1544 event
.SetEventObject( m_owner
);
1545 event
.SetItem( item
);
1546 event
.SetModel( model
);
1547 event
.SetDataFormat( format
);
1548 if (!m_owner
->HandleWindowEvent( event
))
1551 if (!event
.IsAllowed())
1557 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1562 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1563 unsigned int row
= GetLineAt( yy
);
1565 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1568 wxDataViewItem item
= GetItemByRow( row
);
1570 wxDataViewModel
*model
= GetModel();
1572 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1574 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1575 event
.SetEventObject( m_owner
);
1576 event
.SetItem( item
);
1577 event
.SetModel( model
);
1578 event
.SetDataFormat( format
);
1579 event
.SetDataSize( obj
->GetSize() );
1580 event
.SetDataBuffer( obj
->GetData() );
1581 if (!m_owner
->HandleWindowEvent( event
))
1584 if (!event
.IsAllowed())
1590 void wxDataViewMainWindow::OnLeave()
1595 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1597 int height
= GetLineHeight( row
);
1599 unsigned int cols
= GetOwner()->GetColumnCount();
1601 for (col
= 0; col
< cols
; col
++)
1603 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1604 if (column
->IsHidden())
1605 continue; // skip it!
1606 width
+= column
->GetWidth();
1612 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1613 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1614 indent
= indent
+ m_lineHeight
;
1615 // try to use the m_lineHeight as the expander space
1619 wxBitmap
bitmap( width
, height
);
1620 wxMemoryDC
dc( bitmap
);
1621 dc
.SetFont( GetFont() );
1622 dc
.SetPen( *wxBLACK_PEN
);
1623 dc
.SetBrush( *wxWHITE_BRUSH
);
1624 dc
.DrawRectangle( 0,0,width
,height
);
1626 wxDataViewModel
*model
= m_owner
->GetModel();
1628 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1631 // TODO-RTL: last column for RTL support
1632 expander
= GetOwner()->GetColumnAt( 0 );
1633 GetOwner()->SetExpanderColumn(expander
);
1638 for (col
= 0; col
< cols
; col
++)
1640 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1641 wxDataViewRenderer
*cell
= column
->GetRenderer();
1643 if (column
->IsHidden())
1644 continue; // skip it!
1646 width
= column
->GetWidth();
1648 if (column
== expander
)
1651 wxDataViewItem item
= GetItemByRow( row
);
1652 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1654 wxRect
item_rect(x
, 0, width
, height
);
1655 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1657 // dc.SetClippingRegion( item_rect );
1658 cell
->WXCallRender(item_rect
, &dc
, 0);
1659 // dc.DestroyClippingRegion();
1667 #endif // wxUSE_DRAG_AND_DROP
1670 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1672 wxDataViewModel
*model
= GetModel();
1673 wxAutoBufferedPaintDC
dc( this );
1676 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1677 dc
.SetPen( *wxTRANSPARENT_PEN
);
1678 dc
.DrawRectangle(GetClientSize());
1683 // No items to draw.
1688 GetOwner()->PrepareDC( dc
);
1689 dc
.SetFont( GetFont() );
1691 wxRect update
= GetUpdateRegion().GetBox();
1692 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1694 // compute which items needs to be redrawn
1695 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1696 unsigned int item_count
=
1697 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1698 (int)(GetRowCount( ) - item_start
));
1699 unsigned int item_last
= item_start
+ item_count
;
1701 // Send the event to wxDataViewCtrl itself.
1702 wxWindow
* const parent
= GetParent();
1703 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1704 cache_event
.SetEventObject(parent
);
1705 cache_event
.SetCache(item_start
, item_last
- 1);
1706 parent
->ProcessWindowEvent(cache_event
);
1708 // compute which columns needs to be redrawn
1709 unsigned int cols
= GetOwner()->GetColumnCount();
1712 // we assume that we have at least one column below and painting an
1713 // empty control is unnecessary anyhow
1717 unsigned int col_start
= 0;
1718 unsigned int x_start
;
1719 for (x_start
= 0; col_start
< cols
; col_start
++)
1721 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1722 if (col
->IsHidden())
1723 continue; // skip it!
1725 unsigned int w
= col
->GetWidth();
1726 if (x_start
+w
>= (unsigned int)update
.x
)
1732 unsigned int col_last
= col_start
;
1733 unsigned int x_last
= x_start
;
1734 for (; col_last
< cols
; col_last
++)
1736 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1737 if (col
->IsHidden())
1738 continue; // skip it!
1740 if (x_last
> (unsigned int)update
.GetRight())
1743 x_last
+= col
->GetWidth();
1746 // Draw horizontal rules if required
1747 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1749 dc
.SetPen(m_penRule
);
1750 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1752 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1754 int y
= GetLineStart( i
);
1755 dc
.DrawLine(x_start
, y
, x_last
, y
);
1759 // Draw vertical rules if required
1760 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1762 dc
.SetPen(m_penRule
);
1763 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1765 // NB: Vertical rules are drawn in the last pixel of a column so that
1766 // they align perfectly with native MSW wxHeaderCtrl as well as for
1767 // consistency with MSW native list control. There's no vertical
1768 // rule at the most-left side of the control.
1770 int x
= x_start
- 1;
1771 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1773 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1774 if (col
->IsHidden())
1775 continue; // skip it
1777 x
+= col
->GetWidth();
1779 dc
.DrawLine(x
, GetLineStart( item_start
),
1780 x
, GetLineStart( item_last
) );
1784 // redraw the background for the items which are selected/current
1785 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1787 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1788 if (selected
|| item
== m_currentRow
)
1790 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1791 if (item
== m_currentRow
)
1792 flags
|= wxCONTROL_CURRENT
;
1794 flags
|= wxCONTROL_FOCUSED
;
1796 wxRect
rect( x_start
, GetLineStart( item
),
1797 x_last
- x_start
, GetLineHeight( item
) );
1798 wxRendererNative::Get().DrawItemSelectionRect
1808 #if wxUSE_DRAG_AND_DROP
1811 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1812 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1813 dc
.SetPen( *wxBLACK_PEN
);
1814 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1815 dc
.DrawRectangle( rect
);
1817 #endif // wxUSE_DRAG_AND_DROP
1819 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1822 // TODO-RTL: last column for RTL support
1823 expander
= GetOwner()->GetColumnAt( 0 );
1824 GetOwner()->SetExpanderColumn(expander
);
1827 // redraw all cells for all rows which must be repainted and all columns
1829 cell_rect
.x
= x_start
;
1830 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1832 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1833 wxDataViewRenderer
*cell
= col
->GetRenderer();
1834 cell_rect
.width
= col
->GetWidth();
1836 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1837 continue; // skip it!
1839 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1841 // get the cell value and set it into the renderer
1842 wxDataViewTreeNode
*node
= NULL
;
1843 wxDataViewItem dataitem
;
1845 if (!IsVirtualList())
1847 node
= GetTreeNodeByRow(item
);
1851 dataitem
= node
->GetItem();
1853 // Skip all columns of "container" rows except the expander
1854 // column itself unless HasContainerColumns() overrides this.
1855 if ( col
!= GetOwner()->GetExpanderColumn() &&
1856 model
->IsContainer(dataitem
) &&
1857 !model
->HasContainerColumns(dataitem
) )
1862 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1865 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1868 cell_rect
.y
= GetLineStart( item
);
1869 cell_rect
.height
= GetLineHeight( item
);
1871 // deal with the expander
1873 if ((!IsList()) && (col
== expander
))
1875 // Calculate the indent first
1876 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1878 // we reserve m_lineHeight of horizontal space for the expander
1879 // but leave EXPANDER_MARGIN around the expander itself
1880 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1882 indent
+= m_lineHeight
;
1884 // draw expander if needed and visible
1885 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1887 dc
.SetPen( m_penExpander
);
1888 dc
.SetBrush( wxNullBrush
);
1890 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1891 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1892 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1894 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1897 if ( m_underMouse
== node
)
1898 flag
|= wxCONTROL_CURRENT
;
1899 if ( node
->IsOpen() )
1900 flag
|= wxCONTROL_EXPANDED
;
1902 // ensure that we don't overflow the cell (which might
1903 // happen if the column is very narrow)
1904 wxDCClipper
clip(dc
, cell_rect
);
1906 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1909 // force the expander column to left-center align
1910 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1913 wxRect item_rect
= cell_rect
;
1914 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1916 // account for the tree indent (harmless if we're not indented)
1917 item_rect
.x
+= indent
;
1918 item_rect
.width
-= indent
;
1920 if ( item_rect
.width
<= 0 )
1924 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1925 state
|= wxDATAVIEW_CELL_SELECTED
;
1927 // TODO: it would be much more efficient to create a clipping
1928 // region for the entire column being rendered (in the OnPaint
1929 // of wxDataViewMainWindow) instead of a single clip region for
1930 // each cell. However it would mean that each renderer should
1931 // respect the given wxRect's top & bottom coords, eventually
1932 // violating only the left & right coords - however the user can
1933 // make its own renderer and thus we cannot be sure of that.
1934 wxDCClipper
clip(dc
, item_rect
);
1936 cell
->WXCallRender(item_rect
, &dc
, state
);
1939 cell_rect
.x
+= cell_rect
.width
;
1943 void wxDataViewMainWindow::OnRenameTimer()
1945 // We have to call this here because changes may just have
1946 // been made and no screen update taken place.
1949 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1950 // (needs to be tested!)
1954 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1956 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1958 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1961 //-----------------------------------------------------------------------------
1962 // Helper class for do operation on the tree node
1963 //-----------------------------------------------------------------------------
1968 virtual ~DoJob() { }
1970 // The return value control how the tree-walker tranverse the tree
1973 DONE
, // Job done, stop traversing and return
1974 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
1975 CONTINUE
// Job not done, continue
1978 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1981 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1983 wxCHECK_MSG( node
, false, "can't walk NULL node" );
1985 switch( func( node
) )
1989 case DoJob::SKIP_SUBTREE
:
1991 case DoJob::CONTINUE
:
1995 if ( node
->HasChildren() )
1997 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
1999 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2003 if ( Walker(*i
, func
) )
2011 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2013 if (IsVirtualList())
2015 wxDataViewVirtualListModel
*list_model
=
2016 (wxDataViewVirtualListModel
*) GetModel();
2017 m_count
= list_model
->GetCount();
2023 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2028 wxDataViewItemArray siblings
;
2029 GetModel()->GetChildren(parent
, siblings
);
2030 int itemPos
= siblings
.Index(item
, /*fromEnd=*/true);
2031 wxCHECK_MSG( itemPos
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2033 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2034 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2036 parentNode
->SetHasChildren(true);
2037 parentNode
->InsertChild(itemNode
, itemPos
);
2038 parentNode
->ChangeSubTreeCount(+1);
2043 GetOwner()->InvalidateColBestWidths();
2049 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2050 const wxDataViewItem
& item
)
2052 if (IsVirtualList())
2054 wxDataViewVirtualListModel
*list_model
=
2055 (wxDataViewVirtualListModel
*) GetModel();
2056 m_count
= list_model
->GetCount();
2058 if ( !m_selection
.empty() )
2060 const int row
= GetRowByItem(item
);
2062 int rowIndexInSelection
= wxNOT_FOUND
;
2064 const size_t selCount
= m_selection
.size();
2065 for ( size_t i
= 0; i
< selCount
; i
++ )
2067 if ( m_selection
[i
] == (unsigned)row
)
2068 rowIndexInSelection
= i
;
2069 else if ( m_selection
[i
] > (unsigned)row
)
2073 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2074 m_selection
.RemoveAt(rowIndexInSelection
);
2078 else // general case
2080 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2082 // Notice that it is possible that the item being deleted is not in the
2083 // tree at all, for example we could be deleting a never shown (because
2084 // collapsed) item in a tree model. So it's not an error if we don't know
2085 // about this item, just return without doing anything then.
2089 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2090 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2092 // We can't use FindNode() to find 'item', because it was already
2093 // removed from the model by the time ItemDeleted() is called, so we
2094 // have to do it manually. We keep track of its position as well for
2096 int itemPosInNode
= 0;
2097 wxDataViewTreeNode
*itemNode
= NULL
;
2098 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2099 i
!= parentsChildren
.end();
2100 ++i
, ++itemPosInNode
)
2102 if( (*i
)->GetItem() == item
)
2109 // If the parent wasn't expanded, it's possible that we didn't have a
2110 // node corresponding to 'item' and so there's nothing left to do.
2113 // If this was the last child to be removed, it's possible the parent
2114 // node became a leaf. Let's ask the model about it.
2115 if ( parentNode
->GetChildNodes().empty() )
2116 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2121 // Delete the item from wxDataViewTreeNode representation:
2122 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2124 parentNode
->RemoveChild(itemNode
);
2126 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2128 // Make the row number invalid and get a new valid one when user call GetRowCount
2131 // If this was the last child to be removed, it's possible the parent
2132 // node became a leaf. Let's ask the model about it.
2133 if ( parentNode
->GetChildNodes().empty() )
2134 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2136 // Update selection by removing 'item' and its entire children tree from the selection.
2137 if ( !m_selection
.empty() )
2139 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2140 // the parent ('parentNode') and position in its list of children
2142 if ( itemPosInNode
== 0 )
2144 // 1st child, row number is that of the parent parentNode + 1
2145 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2149 // row number is that of the sibling above 'item' + its subtree if any + 1
2150 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2152 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2153 siblingNode
->GetSubTreeCount() +
2157 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2159 const size_t numSelections
= m_selection
.size();
2160 for ( size_t i
= 0; i
< numSelections
; ++i
)
2162 const int s
= m_selection
[i
];
2164 newsel
.push_back(s
);
2165 else if ( s
>= itemRow
+ itemsDeleted
)
2166 newsel
.push_back(s
- itemsDeleted
);
2167 // else: deleted item, remove from selection
2170 m_selection
= newsel
;
2174 // Change the current row to the last row if the current exceed the max row number
2175 if( m_currentRow
> GetRowCount() )
2176 ChangeCurrentRow(m_count
- 1);
2178 GetOwner()->InvalidateColBestWidths();
2184 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2189 GetOwner()->InvalidateColBestWidths();
2192 wxWindow
*parent
= GetParent();
2193 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2194 le
.SetEventObject(parent
);
2195 le
.SetModel(GetModel());
2197 parent
->GetEventHandler()->ProcessEvent(le
);
2202 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2204 int view_column
= -1;
2205 unsigned int n_col
= m_owner
->GetColumnCount();
2206 for (unsigned i
= 0; i
< n_col
; i
++)
2208 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2209 if (column
->GetModelColumn() == model_column
)
2211 view_column
= (int) i
;
2215 if (view_column
== -1)
2218 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2219 /*#define MAX_VIRTUAL_WIDTH 100000
2221 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2222 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2223 Refresh( true, &rect );
2230 GetOwner()->InvalidateColBestWidth(view_column
);
2233 wxWindow
*parent
= GetParent();
2234 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2235 le
.SetEventObject(parent
);
2236 le
.SetModel(GetModel());
2238 le
.SetColumn(view_column
);
2239 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2240 parent
->GetEventHandler()->ProcessEvent(le
);
2245 bool wxDataViewMainWindow::Cleared()
2248 m_selection
.Clear();
2251 BuildTree( GetModel() );
2253 GetOwner()->InvalidateColBestWidths();
2259 void wxDataViewMainWindow::UpdateDisplay()
2262 m_underMouse
= NULL
;
2265 void wxDataViewMainWindow::OnInternalIdle()
2267 wxWindow::OnInternalIdle();
2271 RecalculateDisplay();
2276 void wxDataViewMainWindow::RecalculateDisplay()
2278 wxDataViewModel
*model
= GetModel();
2285 int width
= GetEndOfLastCol();
2286 int height
= GetLineStart( GetRowCount() );
2288 SetVirtualSize( width
, height
);
2289 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2294 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2296 m_underMouse
= NULL
;
2298 wxWindow::ScrollWindow( dx
, dy
, rect
);
2300 if (GetOwner()->m_headerArea
)
2301 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2304 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2306 m_underMouse
= NULL
;
2309 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2310 int sy
= GetLineStart( rows
)/y
;
2314 wxRect rect
= GetClientRect();
2318 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2319 for (x_start
= 0; colnum
< column
; colnum
++)
2321 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2322 if (col
->IsHidden())
2323 continue; // skip it!
2325 w
= col
->GetWidth();
2329 int x_end
= x_start
+ w
;
2330 xe
= xx
+ rect
.width
;
2333 sx
= ( xx
+ x_end
- xe
)/x
;
2340 m_owner
->Scroll( sx
, sy
);
2343 int wxDataViewMainWindow::GetCountPerPage() const
2345 wxSize size
= GetClientSize();
2346 return size
.y
/ m_lineHeight
;
2349 int wxDataViewMainWindow::GetEndOfLastCol() const
2353 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2355 const wxDataViewColumn
*c
=
2356 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2359 width
+= c
->GetWidth();
2364 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2368 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2370 return GetLineAt( y
);
2373 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2375 wxSize client_size
= GetClientSize();
2376 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2377 &client_size
.x
, &client_size
.y
);
2379 // we should deal with the pixel here
2380 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2382 return wxMin( GetRowCount()-1, row
);
2385 unsigned int wxDataViewMainWindow::GetRowCount()
2387 if ( m_count
== -1 )
2389 m_count
= RecalculateCount();
2395 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2402 void wxDataViewMainWindow::SelectAllRows( bool on
)
2409 m_selection
.Clear();
2410 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2411 m_selection
.Add( i
);
2416 unsigned int first_visible
= GetFirstVisibleRow();
2417 unsigned int last_visible
= GetLastVisibleRow();
2419 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2421 unsigned int row
= m_selection
[i
];
2422 if ((row
>= first_visible
) && (row
<= last_visible
))
2425 m_selection
.Clear();
2429 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2431 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2435 m_selection
.Add( row
);
2443 m_selection
.Remove( row
);
2449 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2453 unsigned int tmp
= from
;
2459 for (i
= from
; i
<= to
; i
++)
2461 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2464 m_selection
.Add( i
);
2469 m_selection
.Remove( i
);
2472 RefreshRows( from
, to
);
2475 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2477 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2479 int n
= aSelections
[i
];
2481 m_selection
.Add( n
);
2486 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2488 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2489 m_selection
.Add( row
);
2491 m_selection
.Remove( row
);
2495 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2497 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2500 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2502 wxWindow
*parent
= GetParent();
2503 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2505 le
.SetEventObject(parent
);
2506 le
.SetModel(GetModel());
2509 parent
->GetEventHandler()->ProcessEvent(le
);
2512 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2514 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2515 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2517 wxSize client_size
= GetClientSize();
2518 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2519 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2520 if (intersect_rect
.width
> 0)
2521 Refresh( true, &intersect_rect
);
2524 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2528 unsigned int tmp
= to
;
2533 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2534 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2536 wxSize client_size
= GetClientSize();
2537 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2538 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2539 if (intersect_rect
.width
> 0)
2540 Refresh( true, &intersect_rect
);
2543 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2545 wxSize client_size
= GetClientSize();
2546 int start
= GetLineStart( firstRow
);
2547 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2548 if (start
> client_size
.y
) return;
2550 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2552 Refresh( true, &rect
);
2555 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2559 rect
.y
= GetLineStart( row
);
2560 rect
.width
= GetEndOfLastCol();
2561 rect
.height
= GetLineHeight( row
);
2566 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2568 const wxDataViewModel
*model
= GetModel();
2570 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2572 // TODO make more efficient
2577 for (r
= 0; r
< row
; r
++)
2579 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2580 if (!node
) return start
;
2582 wxDataViewItem item
= node
->GetItem();
2584 unsigned int cols
= GetOwner()->GetColumnCount();
2586 int height
= m_lineHeight
;
2587 for (col
= 0; col
< cols
; col
++)
2589 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2590 if (column
->IsHidden())
2591 continue; // skip it!
2594 model
->IsContainer(item
) &&
2595 !model
->HasContainerColumns(item
))
2596 continue; // skip it!
2598 wxDataViewRenderer
*renderer
=
2599 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2600 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2602 height
= wxMax( height
, renderer
->GetSize().y
);
2612 return row
* m_lineHeight
;
2616 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2618 const wxDataViewModel
*model
= GetModel();
2620 // check for the easy case first
2621 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2622 return y
/ m_lineHeight
;
2624 // TODO make more efficient
2625 unsigned int row
= 0;
2626 unsigned int yy
= 0;
2629 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2632 // not really correct...
2633 return row
+ ((y
-yy
) / m_lineHeight
);
2636 wxDataViewItem item
= node
->GetItem();
2638 unsigned int cols
= GetOwner()->GetColumnCount();
2640 int height
= m_lineHeight
;
2641 for (col
= 0; col
< cols
; col
++)
2643 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2644 if (column
->IsHidden())
2645 continue; // skip it!
2648 model
->IsContainer(item
) &&
2649 !model
->HasContainerColumns(item
))
2650 continue; // skip it!
2652 wxDataViewRenderer
*renderer
=
2653 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2654 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2656 height
= wxMax( height
, renderer
->GetSize().y
);
2667 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2669 const wxDataViewModel
*model
= GetModel();
2671 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2673 wxASSERT( !IsVirtualList() );
2675 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2676 // wxASSERT( node );
2677 if (!node
) return m_lineHeight
;
2679 wxDataViewItem item
= node
->GetItem();
2681 int height
= m_lineHeight
;
2683 unsigned int cols
= GetOwner()->GetColumnCount();
2685 for (col
= 0; col
< cols
; col
++)
2687 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2688 if (column
->IsHidden())
2689 continue; // skip it!
2692 model
->IsContainer(item
) &&
2693 !model
->HasContainerColumns(item
))
2694 continue; // skip it!
2696 wxDataViewRenderer
*renderer
=
2697 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2698 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2700 height
= wxMax( height
, renderer
->GetSize().y
);
2707 return m_lineHeight
;
2712 class RowToTreeNodeJob
: public DoJob
2715 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2718 this->current
= current
;
2723 virtual int operator() ( wxDataViewTreeNode
* node
)
2726 if( current
== static_cast<int>(row
))
2732 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2734 current
+= node
->GetSubTreeCount();
2735 return DoJob::SKIP_SUBTREE
;
2741 // If the current node has only leaf children, we can find the
2742 // desired node directly. This can speed up finding the node
2743 // in some cases, and will have a very good effect for list views.
2744 if ( node
->HasChildren() &&
2745 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2747 const int index
= static_cast<int>(row
) - current
- 1;
2748 ret
= node
->GetChildNodes()[index
];
2752 return DoJob::CONTINUE
;
2756 wxDataViewTreeNode
* GetResult() const
2762 wxDataViewTreeNode
* ret
;
2763 wxDataViewTreeNode
* parent
;
2766 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2768 wxASSERT( !IsVirtualList() );
2770 RowToTreeNodeJob
job( row
, -2, m_root
);
2771 Walker( m_root
, job
);
2772 return job
.GetResult();
2775 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2777 if (IsVirtualList())
2779 return wxDataViewItem( wxUIntToPtr(row
+1) );
2783 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2784 return node
? node
->GetItem() : wxDataViewItem();
2789 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2790 const wxDataViewItem
& item
)
2792 wxWindow
*parent
= GetParent();
2793 wxDataViewEvent
le(type
, parent
->GetId());
2795 le
.SetEventObject(parent
);
2796 le
.SetModel(GetModel());
2799 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2802 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2807 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2811 if (!node
->HasChildren())
2814 return node
->IsOpen();
2817 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2822 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2826 if (!node
->HasChildren())
2832 void wxDataViewMainWindow::Expand( unsigned int row
)
2837 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2841 if (!node
->HasChildren())
2844 if (!node
->IsOpen())
2846 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
2848 // Vetoed by the event handler.
2854 // build the children of current node
2855 if( node
->GetChildNodes().empty() )
2858 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
2861 // By expanding the node all row indices that are currently in the selection list
2862 // and are greater than our node have become invalid. So we have to correct that now.
2863 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2864 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2866 const unsigned testRow
= m_selection
[i
];
2867 // all rows above us are not affected, so skip them
2871 m_selection
[i
] += rowAdjustment
;
2874 if(m_currentRow
> row
)
2875 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2879 // Send the expanded event
2880 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2884 void wxDataViewMainWindow::Collapse(unsigned int row
)
2889 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2893 if (!node
->HasChildren())
2898 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
2900 // Vetoed by the event handler.
2904 // Find out if there are selected items below the current node.
2905 bool selectCollapsingRow
= false;
2906 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2907 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2908 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2910 const unsigned testRow
= m_selection
[i
];
2911 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2913 selectCollapsingRow
= true;
2914 // get out as soon as we have found a node that is selected
2921 // If the node to be closed has selected items the user won't see those any longer.
2922 // We select the collapsing node in this case.
2923 if(selectCollapsingRow
)
2925 SelectAllRows(false);
2926 ChangeCurrentRow(row
);
2927 SelectRow(row
, true);
2928 SendSelectionChangedEvent(GetItemByRow(row
));
2932 // if there were no selected items below our node we still need to "fix" the
2933 // selection list to adjust for the changing of the row indices.
2934 // We actually do the opposite of what we are doing in Expand().
2935 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2937 const unsigned testRow
= m_selection
[i
];
2938 // all rows above us are not affected, so skip them
2942 m_selection
[i
] -= rowAdjustment
;
2945 // if the "current row" is being collapsed away we change it to the current row ;-)
2946 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2947 ChangeCurrentRow(row
);
2948 else if(m_currentRow
> row
)
2949 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2954 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2958 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2960 const wxDataViewModel
* model
= GetModel();
2967 // Compose the parent-chain for the item we are looking for
2968 wxVector
<wxDataViewItem
> parentChain
;
2969 wxDataViewItem
it( item
);
2972 parentChain
.push_back(it
);
2973 it
= model
->GetParent(it
);
2976 // Find the item along the parent-chain.
2977 // This algorithm is designed to speed up the node-finding method
2978 wxDataViewTreeNode
* node
= m_root
;
2979 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
2981 if( node
->HasChildren() )
2983 if( node
->GetChildNodes().empty() )
2985 // Even though the item is a container, it doesn't have any
2986 // child nodes in the control's representation yet. We have
2987 // to realize its subtree now.
2989 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2992 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2995 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
2997 wxDataViewTreeNode
* currentNode
= nodes
[i
];
2998 if (currentNode
->GetItem() == parentChain
[iter
])
3000 if (currentNode
->GetItem() == item
)
3020 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3021 wxDataViewColumn
* &column
)
3023 wxDataViewColumn
*col
= NULL
;
3024 unsigned int cols
= GetOwner()->GetColumnCount();
3025 unsigned int colnum
= 0;
3027 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3028 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3030 col
= GetOwner()->GetColumnAt(colnum
);
3031 if (col
->IsHidden())
3032 continue; // skip it!
3034 unsigned int w
= col
->GetWidth();
3035 if (x_start
+w
>= (unsigned int)x
)
3042 item
= GetItemByRow( GetLineAt( y
) );
3045 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3046 const wxDataViewColumn
* column
)
3051 unsigned int cols
= GetOwner()->GetColumnCount();
3052 // If column is null the loop will compute the combined width of all columns.
3053 // Otherwise, it will compute the x position of the column we are looking for.
3054 for (unsigned int i
= 0; i
< cols
; i
++)
3056 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3061 if (col
->IsHidden())
3062 continue; // skip it!
3064 xpos
+= col
->GetWidth();
3065 width
+= col
->GetWidth();
3070 // If we have a column, we need can get its width directly.
3071 if(column
->IsHidden())
3074 width
= column
->GetWidth();
3079 // If we have no column, we reset the x position back to zero.
3083 // we have to take an expander column into account and compute its indentation
3084 // to get the correct x position where the actual text is
3086 int row
= GetRowByItem(item
);
3087 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3089 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3090 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3091 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3094 wxRect
itemRect( xpos
+ indent
,
3095 GetLineStart( row
),
3097 GetLineHeight( row
) );
3099 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3100 &itemRect
.x
, &itemRect
.y
);
3105 int wxDataViewMainWindow::RecalculateCount()
3107 if (IsVirtualList())
3109 wxDataViewVirtualListModel
*list_model
=
3110 (wxDataViewVirtualListModel
*) GetModel();
3112 return list_model
->GetCount();
3116 return m_root
->GetSubTreeCount();
3120 class ItemToRowJob
: public DoJob
3123 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3130 // Maybe binary search will help to speed up this process
3131 virtual int operator() ( wxDataViewTreeNode
* node
)
3134 if( node
->GetItem() == item
)
3139 if( node
->GetItem() == *m_iter
)
3142 return DoJob::CONTINUE
;
3146 ret
+= node
->GetSubTreeCount();
3147 return DoJob::SKIP_SUBTREE
;
3152 // the row number is begin from zero
3153 int GetResult() const
3157 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3158 wxDataViewItem item
;
3163 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3165 const wxDataViewModel
* model
= GetModel();
3169 if (IsVirtualList())
3171 return wxPtrToUInt( item
.GetID() ) -1;
3178 // Compose the parent-chain of the item we are looking for
3179 wxVector
<wxDataViewItem
> parentChain
;
3180 wxDataViewItem
it( item
);
3183 parentChain
.push_back(it
);
3184 it
= model
->GetParent(it
);
3187 // add an 'invalid' item to represent our 'invisible' root node
3188 parentChain
.push_back(wxDataViewItem());
3190 // the parent chain was created by adding the deepest parent first.
3191 // so if we want to start at the root node, we have to iterate backwards through the vector
3192 ItemToRowJob
job( item
, parentChain
.rbegin() );
3193 Walker( m_root
, job
);
3194 return job
.GetResult();
3198 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3199 wxDataViewTreeNode
* node
)
3201 if( !model
->IsContainer( item
) )
3204 wxDataViewItemArray children
;
3205 unsigned int num
= model
->GetChildren( item
, children
);
3207 for ( unsigned int index
= 0; index
< num
; index
++ )
3209 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3211 if( model
->IsContainer(children
[index
]) )
3212 n
->SetHasChildren( true );
3214 node
->InsertChild(n
, index
);
3217 wxASSERT( node
->IsOpen() );
3218 node
->ChangeSubTreeCount(+num
);
3221 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3225 if (GetModel()->IsVirtualListModel())
3231 m_root
= wxDataViewTreeNode::CreateRootNode();
3233 // First we define a invalid item to fetch the top-level elements
3234 wxDataViewItem item
;
3236 BuildTreeHelper( model
, item
, m_root
);
3240 void wxDataViewMainWindow::DestroyTree()
3242 if (!IsVirtualList())
3249 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3251 wxWindow
* const parent
= GetParent();
3253 // propagate the char event upwards
3254 wxKeyEvent
eventForParent(event
);
3255 eventForParent
.SetEventObject(parent
);
3256 if ( parent
->ProcessWindowEvent(eventForParent
) )
3259 if ( parent
->HandleAsNavigationKey(event
) )
3262 // no item -> nothing to do
3263 if (!HasCurrentRow())
3269 // don't use m_linesPerPage directly as it might not be computed yet
3270 const int pageSize
= GetCountPerPage();
3271 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3273 switch ( event
.GetKeyCode() )
3277 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3278 // it. Only if that event is not handled do we activate column renderer (which
3279 // is normally done by Space).
3281 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3283 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3286 le
.SetEventObject(parent
);
3287 le
.SetModel(GetModel());
3289 if ( parent
->GetEventHandler()->ProcessEvent(le
) )
3291 // else: fall through to WXK_SPACE handling
3296 // Activate the first activatable column if there is any:
3297 wxDataViewColumn
*activatableCol
= NULL
;
3299 const unsigned cols
= GetOwner()->GetColumnCount();
3300 for ( unsigned i
= 0; i
< cols
; i
++ )
3302 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3303 if ( c
->IsHidden() )
3305 if ( c
->GetRenderer()->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3312 if ( activatableCol
)
3314 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3316 const unsigned colIdx
= activatableCol
->GetModelColumn();
3317 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3319 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3320 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3321 cell
->WXOnActivate(cell_rect
, GetModel(), item
, colIdx
);
3327 if ( m_currentRow
> 0 )
3328 OnVerticalNavigation( m_currentRow
- 1, event
);
3332 if ( m_currentRow
+ 1 < GetRowCount() )
3333 OnVerticalNavigation( m_currentRow
+ 1, event
);
3335 // Add the process for tree expanding/collapsing
3341 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3345 if (node
->HasChildren() && node
->IsOpen())
3347 Collapse(m_currentRow
);
3349 else // if the node is already closed we move the selection to its parent
3351 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3355 int parent
= GetRowByItem( parent_node
->GetItem() );
3358 unsigned int row
= m_currentRow
;
3359 SelectRow( row
, false);
3360 SelectRow( parent
, true );
3361 ChangeCurrentRow( parent
);
3362 GetOwner()->EnsureVisible( parent
, -1 );
3363 SendSelectionChangedEvent( parent_node
->GetItem() );
3371 if (!IsExpanded( m_currentRow
))
3372 Expand( m_currentRow
);
3375 unsigned int row
= m_currentRow
;
3376 SelectRow( row
, false );
3377 SelectRow( row
+ 1, true );
3378 ChangeCurrentRow( row
+ 1 );
3379 GetOwner()->EnsureVisible( row
+ 1, -1 );
3380 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3387 OnVerticalNavigation( GetRowCount() - 1, event
);
3392 OnVerticalNavigation( 0, event
);
3397 int steps
= pageSize
- 1;
3398 int index
= m_currentRow
- steps
;
3402 OnVerticalNavigation( index
, event
);
3408 int steps
= pageSize
- 1;
3409 unsigned int index
= m_currentRow
+ steps
;
3410 unsigned int count
= GetRowCount();
3411 if ( index
>= count
)
3414 OnVerticalNavigation( index
, event
);
3420 if(m_selection
.size() == 1)
3422 // TODO: we need to revise that when we have a concept for a 'current column'
3423 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3433 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3435 wxCHECK_RET( newCurrent
< GetRowCount(),
3436 wxT("invalid item index in OnVerticalNavigation()") );
3438 // if there is no selection, we cannot move it anywhere
3439 if (!HasCurrentRow())
3442 unsigned int oldCurrent
= m_currentRow
;
3444 // in single selection we just ignore Shift as we can't select several
3446 if ( event
.ShiftDown() && !IsSingleSel() )
3448 RefreshRow( oldCurrent
);
3450 ChangeCurrentRow( newCurrent
);
3452 // select all the items between the old and the new one
3453 if ( oldCurrent
> newCurrent
)
3455 newCurrent
= oldCurrent
;
3456 oldCurrent
= m_currentRow
;
3459 SelectRows( oldCurrent
, newCurrent
, true );
3460 if (oldCurrent
!=newCurrent
)
3461 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3465 RefreshRow( oldCurrent
);
3467 // all previously selected items are unselected unless ctrl is held
3468 if ( !event
.ControlDown() )
3469 SelectAllRows(false);
3471 ChangeCurrentRow( newCurrent
);
3473 if ( !event
.ControlDown() )
3475 SelectRow( m_currentRow
, true );
3476 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3479 RefreshRow( m_currentRow
);
3482 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3485 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3487 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3489 // let the base handle mouse wheel events.
3494 // set the focus to ourself if any of the mouse buttons are pressed
3495 if(event
.ButtonDown() && !HasFocus())
3498 int x
= event
.GetX();
3499 int y
= event
.GetY();
3500 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3501 wxDataViewColumn
*col
= NULL
;
3504 unsigned int cols
= GetOwner()->GetColumnCount();
3506 for (i
= 0; i
< cols
; i
++)
3508 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3510 continue; // skip it!
3512 if (x
< xpos
+ c
->GetWidth())
3517 xpos
+= c
->GetWidth();
3525 wxDataViewRenderer
*cell
= col
->GetRenderer();
3526 unsigned int current
= GetLineAt( y
);
3527 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3529 // Unselect all if below the last row ?
3534 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3535 // button) and also determine the offset of the real cell start, skipping
3536 // the indentation and the expander itself.
3537 bool hoverOverExpander
= false;
3539 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3541 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3543 int indent
= node
->GetIndentLevel();
3544 itemOffset
= GetOwner()->GetIndent()*indent
;
3546 if ( node
->HasChildren() )
3548 // we make the rectangle we are looking in a bit bigger than the actual
3549 // visual expander so the user can hit that little thing reliably
3550 wxRect
rect(itemOffset
,
3551 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3552 m_lineHeight
, m_lineHeight
);
3554 if( rect
.Contains(x
, y
) )
3556 // So the mouse is over the expander
3557 hoverOverExpander
= true;
3558 if (m_underMouse
&& m_underMouse
!= node
)
3560 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3561 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3563 if (m_underMouse
!= node
)
3565 // wxLogMessage("Do the row: %d", current);
3566 RefreshRow(current
);
3568 m_underMouse
= node
;
3572 // Account for the expander as well, even if this item doesn't have it,
3573 // its parent does so it still counts for the offset.
3574 itemOffset
+= m_lineHeight
;
3576 if (!hoverOverExpander
)
3578 if (m_underMouse
!= NULL
)
3580 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3581 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3582 m_underMouse
= NULL
;
3586 wxDataViewModel
*model
= GetModel();
3588 #if wxUSE_DRAG_AND_DROP
3589 if (event
.Dragging())
3591 if (m_dragCount
== 0)
3593 // we have to report the raw, physical coords as we want to be
3594 // able to call HitTest(event.m_pointDrag) from the user code to
3595 // get the item being dragged
3596 m_dragStart
= event
.GetPosition();
3601 if (m_dragCount
!= 3)
3604 if (event
.LeftIsDown())
3606 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3607 &m_dragStart
.x
, &m_dragStart
.y
);
3608 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3609 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3611 // Notify cell about drag
3612 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3613 event
.SetEventObject( m_owner
);
3614 event
.SetItem( item
);
3615 event
.SetModel( model
);
3616 if (!m_owner
->HandleWindowEvent( event
))
3619 if (!event
.IsAllowed())
3622 wxDataObject
*obj
= event
.GetDataObject();
3626 wxDataViewDropSource
drag( this, drag_item_row
);
3627 drag
.SetData( *obj
);
3628 /* wxDragResult res = */ drag
.DoDragDrop();
3637 #endif // wxUSE_DRAG_AND_DROP
3639 bool simulateClick
= false;
3641 if (event
.ButtonDClick())
3643 m_renameTimer
->Stop();
3644 m_lastOnSame
= false;
3647 wxDataViewItem item
= GetItemByRow(current
);
3648 bool ignore_other_columns
=
3649 ((GetOwner()->GetExpanderColumn() != col
) &&
3650 (model
->IsContainer(item
)) &&
3651 (!model
->HasContainerColumns(item
)));
3653 if (event
.LeftDClick())
3655 if(hoverOverExpander
)
3657 // a double click on the expander will be converted into a "simulated" normal click
3658 simulateClick
= true;
3660 else if ( current
== m_lineLastClicked
)
3662 bool activated
= false;
3664 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3666 const unsigned colIdx
= col
->GetModelColumn();
3668 cell
->PrepareForItem(model
, item
, colIdx
);
3670 wxRect
cell_rect( xpos
, GetLineStart( current
),
3671 col
->GetWidth(), GetLineHeight( current
) );
3672 activated
= cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3677 wxWindow
*parent
= GetParent();
3678 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3680 le
.SetColumn( col
->GetModelColumn() );
3681 le
.SetDataViewColumn( col
);
3682 le
.SetEventObject(parent
);
3683 le
.SetModel(GetModel());
3685 parent
->GetEventHandler()->ProcessEvent(le
);
3691 // The first click was on another item, so don't interpret this as
3692 // a double click, but as a simple click instead
3693 simulateClick
= true;
3697 if (event
.LeftUp() && !hoverOverExpander
)
3699 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3701 // select single line
3702 SelectAllRows( false );
3703 SelectRow( m_lineSelectSingleOnUp
, true );
3704 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3707 // If the user click the expander, we do not do editing even if the column
3708 // with expander are editable
3709 if (m_lastOnSame
&& !ignore_other_columns
)
3711 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3712 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3714 m_renameTimer
->Start( 100, true );
3718 m_lastOnSame
= false;
3719 m_lineSelectSingleOnUp
= (unsigned int)-1;
3721 else if(!event
.LeftUp())
3723 // This is necessary, because after a DnD operation in
3724 // from and to ourself, the up event is swallowed by the
3725 // DnD code. So on next non-up event (which means here and
3726 // now) m_lineSelectSingleOnUp should be reset.
3727 m_lineSelectSingleOnUp
= (unsigned int)-1;
3730 if (event
.RightDown())
3732 m_lineBeforeLastClicked
= m_lineLastClicked
;
3733 m_lineLastClicked
= current
;
3735 // If the item is already selected, do not update the selection.
3736 // Multi-selections should not be cleared if a selected item is clicked.
3737 if (!IsRowSelected(current
))
3739 SelectAllRows(false);
3740 const unsigned oldCurrent
= m_currentRow
;
3741 ChangeCurrentRow(current
);
3742 SelectRow(m_currentRow
,true);
3743 RefreshRow(oldCurrent
);
3744 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3747 else if (event
.RightUp())
3750 model
->GetValue( value
, item
, col
->GetModelColumn() );
3751 wxWindow
*parent
= GetParent();
3752 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3754 le
.SetColumn( col
->GetModelColumn() );
3755 le
.SetDataViewColumn( col
);
3756 le
.SetEventObject(parent
);
3757 le
.SetModel(GetModel());
3759 parent
->GetEventHandler()->ProcessEvent(le
);
3761 else if (event
.MiddleDown())
3765 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3767 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3769 // hoverOverExpander being true tells us that our node must be
3770 // valid and have children.
3771 // So we don't need any extra checks.
3772 if( node
->IsOpen() )
3777 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3779 m_lineBeforeLastClicked
= m_lineLastClicked
;
3780 m_lineLastClicked
= current
;
3782 unsigned int oldCurrentRow
= m_currentRow
;
3783 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3785 bool cmdModifierDown
= event
.CmdDown();
3786 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3788 if ( IsSingleSel() || !IsRowSelected(current
) )
3790 SelectAllRows( false );
3791 ChangeCurrentRow(current
);
3792 SelectRow(m_currentRow
,true);
3793 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3795 else // multi sel & current is highlighted & no mod keys
3797 m_lineSelectSingleOnUp
= current
;
3798 ChangeCurrentRow(current
); // change focus
3801 else // multi sel & either ctrl or shift is down
3803 if (cmdModifierDown
)
3805 ChangeCurrentRow(current
);
3806 ReverseRowSelection(m_currentRow
);
3807 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3809 else if (event
.ShiftDown())
3811 ChangeCurrentRow(current
);
3813 unsigned int lineFrom
= oldCurrentRow
,
3816 if ( lineTo
< lineFrom
)
3819 lineFrom
= m_currentRow
;
3822 SelectRows(lineFrom
, lineTo
, true);
3823 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3825 else // !ctrl, !shift
3827 // test in the enclosing if should make it impossible
3828 wxFAIL_MSG( wxT("how did we get here?") );
3832 if (m_currentRow
!= oldCurrentRow
)
3833 RefreshRow( oldCurrentRow
);
3835 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3837 // Update selection here...
3840 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3841 (current
== oldCurrentRow
)) && oldWasSelected
;
3843 // Call LeftClick after everything else as under GTK+
3844 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3846 // notify cell about click
3847 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3849 wxRect
cell_rect( xpos
+ itemOffset
,
3850 GetLineStart( current
),
3851 col
->GetWidth() - itemOffset
,
3852 GetLineHeight( current
) );
3854 // Report position relative to the cell's custom area, i.e.
3855 // no the entire space as given by the control but the one
3856 // used by the renderer after calculation of alignment etc.
3858 // adjust the rectangle ourselves to account for the alignment
3859 wxRect rectItem
= cell_rect
;
3860 const int align
= cell
->GetAlignment();
3861 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3863 const wxSize size
= cell
->GetSize();
3865 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3867 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3868 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3869 else if ( align
& wxALIGN_RIGHT
)
3870 rectItem
.x
+= cell_rect
.width
- size
.x
;
3871 // else: wxALIGN_LEFT is the default
3874 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3876 if ( align
& wxALIGN_CENTER_VERTICAL
)
3877 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3878 else if ( align
& wxALIGN_BOTTOM
)
3879 rectItem
.y
+= cell_rect
.height
- size
.y
;
3880 // else: wxALIGN_TOP is the default
3884 wxPoint
pos( event
.GetPosition() );
3885 pos
.x
-= rectItem
.x
;
3886 pos
.y
-= rectItem
.y
;
3888 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3890 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3891 model
, item
, col
->GetModelColumn());
3896 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3900 if (HasCurrentRow())
3906 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3910 if (HasCurrentRow())
3916 //-----------------------------------------------------------------------------
3918 //-----------------------------------------------------------------------------
3920 WX_DEFINE_LIST(wxDataViewColumnList
)
3922 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3923 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3924 EVT_SIZE(wxDataViewCtrl::OnSize
)
3927 wxDataViewCtrl::~wxDataViewCtrl()
3930 GetModel()->RemoveNotifier( m_notifier
);
3933 m_colsBestWidths
.clear();
3936 void wxDataViewCtrl::Init()
3938 m_cols
.DeleteContents(true);
3941 // No sorting column at start
3942 m_sortingColumnIdx
= wxNOT_FOUND
;
3944 m_headerArea
= NULL
;
3946 m_colsDirty
= false;
3949 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3954 const wxValidator
& validator
,
3955 const wxString
& name
)
3957 // if ( (style & wxBORDER_MASK) == 0)
3958 // style |= wxBORDER_SUNKEN;
3962 if (!wxControl::Create( parent
, id
, pos
, size
,
3963 style
| wxScrolledWindowStyle
, validator
, name
))
3966 SetInitialSize(size
);
3969 MacSetClipChildren( true );
3972 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3974 // We use the cursor keys for moving the selection, not scrolling, so call
3975 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3976 // keyboard events forwarded to us from wxListMainWindow.
3977 DisableKeyboardScrolling();
3979 if (HasFlag(wxDV_NO_HEADER
))
3980 m_headerArea
= NULL
;
3982 m_headerArea
= new wxDataViewHeaderWindow(this);
3984 SetTargetWindow( m_clientArea
);
3986 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3988 sizer
->Add( m_headerArea
, 0, wxGROW
);
3989 sizer
->Add( m_clientArea
, 1, wxGROW
);
3995 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3997 return wxBORDER_THEME
;
4001 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4005 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4008 // we need to process arrows ourselves for scrolling
4009 if ( nMsg
== WM_GETDLGCODE
)
4011 rc
|= DLGC_WANTARROWS
;
4019 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4021 wxSize newsize
= size
;
4022 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4023 newsize
.y
-= m_headerArea
->GetSize().y
;
4028 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4030 // We need to override OnSize so that our scrolled
4031 // window a) does call Layout() to use sizers for
4032 // positioning the controls but b) does not query
4033 // the sizer for their size and use that for setting
4034 // the scrollable area as set that ourselves by
4035 // calling SetScrollbar() further down.
4041 // We must redraw the headers if their height changed. Normally this
4042 // shouldn't happen as the control shouldn't let itself be resized beneath
4043 // its minimal height but avoid the display artefacts that appear if it
4044 // does happen, e.g. because there is really not enough vertical space.
4045 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4046 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4048 m_headerArea
->Refresh();
4052 void wxDataViewCtrl::SetFocus()
4055 m_clientArea
->SetFocus();
4058 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4060 if (!wxDataViewCtrlBase::AssociateModel( model
))
4063 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4065 model
->AddNotifier( m_notifier
);
4067 m_clientArea
->DestroyTree();
4069 m_clientArea
->BuildTree(model
);
4071 m_clientArea
->UpdateDisplay();
4076 #if wxUSE_DRAG_AND_DROP
4078 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4080 return m_clientArea
->EnableDragSource( format
);
4083 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4085 return m_clientArea
->EnableDropTarget( format
);
4088 #endif // wxUSE_DRAG_AND_DROP
4090 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4092 if (!wxDataViewCtrlBase::AppendColumn(col
))
4095 m_cols
.Append( col
);
4096 m_colsBestWidths
.push_back(0);
4097 OnColumnsCountChanged();
4101 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4103 if (!wxDataViewCtrlBase::PrependColumn(col
))
4106 m_cols
.Insert( col
);
4107 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4108 OnColumnsCountChanged();
4112 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4114 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4117 m_cols
.Insert( pos
, col
);
4118 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4119 OnColumnsCountChanged();
4123 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4126 m_headerArea
->UpdateColumn(idx
);
4128 m_clientArea
->UpdateDisplay();
4131 void wxDataViewCtrl::OnColumnsCountChanged()
4134 m_headerArea
->SetColumnCount(GetColumnCount());
4136 m_clientArea
->UpdateDisplay();
4139 void wxDataViewCtrl::DoSetExpanderColumn()
4141 m_clientArea
->UpdateDisplay();
4144 void wxDataViewCtrl::DoSetIndent()
4146 m_clientArea
->UpdateDisplay();
4149 unsigned int wxDataViewCtrl::GetColumnCount() const
4151 return m_cols
.GetCount();
4154 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4156 if ( !m_clientArea
)
4159 m_clientArea
->SetRowHeight(lineHeight
);
4164 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4169 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4171 // columns can't be reordered if there is no header window which allows
4173 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4176 return GetColumn(idx
);
4179 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4181 const unsigned count
= m_cols
.size();
4182 for ( unsigned n
= 0; n
< count
; n
++ )
4184 if ( m_cols
[n
] == column
)
4191 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4193 if ( m_colsBestWidths
[idx
] != 0 )
4194 return m_colsBestWidths
[idx
];
4196 const int count
= m_clientArea
->GetRowCount();
4197 wxDataViewColumn
*column
= GetColumn(idx
);
4198 wxDataViewRenderer
*renderer
=
4199 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4201 class MaxWidthCalculator
4204 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4205 wxDataViewRenderer
*renderer
,
4206 const wxDataViewModel
*model
,
4209 m_clientArea(clientArea
),
4210 m_renderer(renderer
),
4216 void UpdateWithWidth(int width
)
4218 m_width
= wxMax(m_width
, width
);
4221 void UpdateWithRow(int row
)
4223 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4224 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4225 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4228 int GetMaxWidth() const { return m_width
; }
4232 wxDataViewMainWindow
*m_clientArea
;
4233 wxDataViewRenderer
*m_renderer
;
4234 const wxDataViewModel
*m_model
;
4238 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4239 GetModel(), column
->GetModelColumn());
4243 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4244 // Labels on native MSW header are indented on both sides
4246 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4247 calculator
.UpdateWithWidth(header_width
);
4250 // The code below deserves some explanation. For very large controls, we
4251 // simply can't afford to calculate sizes for all items, it takes too
4252 // long. So the best we can do is to check the first and the last N/2
4253 // items in the control for some sufficiently large N and calculate best
4254 // sizes from that. That can result in the calculated best width being too
4255 // small for some outliers, but it's better to get slightly imperfect
4256 // result than to wait several seconds after every update. To avoid highly
4257 // visible miscalculations, we also include all currently visible items
4258 // no matter what. Finally, the value of N is determined dynamically by
4259 // measuring how much time we spent on the determining item widths so far.
4262 int top_part_end
= count
;
4263 static const long CALC_TIMEOUT
= 20/*ms*/;
4264 // don't call wxStopWatch::Time() too often
4265 static const unsigned CALC_CHECK_FREQ
= 100;
4268 // use some hard-coded limit, that's the best we can do without timer
4269 int top_part_end
= wxMin(500, count
);
4270 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4274 for ( row
= 0; row
< top_part_end
; row
++ )
4277 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4278 timer
.Time() > CALC_TIMEOUT
)
4280 #endif // wxUSE_STOPWATCH
4281 calculator
.UpdateWithRow(row
);
4284 // row is the first unmeasured item now; that's our value of N/2
4290 // add bottom N/2 items now:
4291 const int bottom_part_start
= wxMax(row
, count
- row
);
4292 for ( row
= bottom_part_start
; row
< count
; row
++ )
4294 calculator
.UpdateWithRow(row
);
4297 // finally, include currently visible items in the calculation:
4298 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4299 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4300 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4302 first_visible
= wxMax(first_visible
, top_part_end
);
4303 last_visible
= wxMin(bottom_part_start
, last_visible
);
4305 for ( row
= first_visible
; row
< last_visible
; row
++ )
4307 calculator
.UpdateWithRow(row
);
4310 wxLogTrace("dataview",
4311 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4313 count
- bottom_part_start
,
4314 wxMax(0, last_visible
- first_visible
),
4318 int max_width
= calculator
.GetMaxWidth();
4319 if ( max_width
> 0 )
4320 max_width
+= 2 * PADDING_RIGHTLEFT
;
4322 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4326 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4327 unsigned int WXUNUSED(new_pos
))
4329 // do _not_ reorder m_cols elements here, they should always be in the
4330 // order in which columns were added, we only display the columns in
4332 m_clientArea
->UpdateDisplay();
4335 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4337 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4341 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4343 OnColumnsCountChanged();
4348 bool wxDataViewCtrl::ClearColumns()
4351 m_colsBestWidths
.clear();
4352 OnColumnsCountChanged();
4356 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4358 m_colsBestWidths
[idx
] = 0;
4362 void wxDataViewCtrl::InvalidateColBestWidths()
4364 m_colsBestWidths
.clear();
4365 m_colsBestWidths
.resize(m_cols
.size());
4369 void wxDataViewCtrl::UpdateColWidths()
4371 if ( !m_headerArea
)
4374 for ( wxVector
<int>::const_iterator i
= m_colsBestWidths
.begin();
4375 i
!= m_colsBestWidths
.end();
4378 if ( m_colsBestWidths
[*i
] == 0 )
4379 m_headerArea
->UpdateColumn(*i
);
4383 void wxDataViewCtrl::OnInternalIdle()
4385 wxDataViewCtrlBase::OnInternalIdle();
4389 m_colsDirty
= false;
4394 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4397 unsigned int len
= GetColumnCount();
4398 for ( unsigned int i
= 0; i
< len
; i
++ )
4400 wxDataViewColumn
* col
= GetColumnAt(i
);
4407 // This returns the position in pixels which is not what we want.
4410 unsigned int len
= GetColumnCount();
4411 for ( unsigned int i
= 0; i
< len
; i
++ )
4413 wxDataViewColumn
* col
= GetColumnAt(i
);
4414 if (col
->IsHidden())
4416 ret
+= col
->GetWidth();
4419 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4427 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4429 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4430 : GetColumn(m_sortingColumnIdx
);
4433 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4435 return GetItemByRow(m_clientArea
->GetCurrentRow());
4438 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4440 const int row
= m_clientArea
->GetRowByItem(item
);
4442 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4443 if ( static_cast<unsigned>(row
) != oldCurrent
)
4445 m_clientArea
->ChangeCurrentRow(row
);
4446 m_clientArea
->RefreshRow(oldCurrent
);
4447 m_clientArea
->RefreshRow(row
);
4451 int wxDataViewCtrl::GetSelectedItemsCount() const
4453 return m_clientArea
->GetSelections().size();
4456 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4459 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4461 const size_t len
= selections
.size();
4462 for ( size_t i
= 0; i
< len
; i
++ )
4464 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4471 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4478 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4480 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4482 wxDataViewItem last_parent
;
4484 int len
= sel
.GetCount();
4485 for( int i
= 0; i
< len
; i
++ )
4487 wxDataViewItem item
= sel
[i
];
4488 wxDataViewItem parent
= GetModel()->GetParent( item
);
4491 if (parent
!= last_parent
)
4492 ExpandAncestors(item
);
4495 last_parent
= parent
;
4496 int row
= m_clientArea
->GetRowByItem( item
);
4498 selection
.Add( static_cast<unsigned int>(row
) );
4501 m_clientArea
->SetSelections( selection
);
4504 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4506 ExpandAncestors( item
);
4508 int row
= m_clientArea
->GetRowByItem( item
);
4511 // Unselect all rows before select another in the single select mode
4512 if (m_clientArea
->IsSingleSel())
4513 m_clientArea
->SelectAllRows(false);
4515 m_clientArea
->SelectRow(row
, true);
4517 // Also set focus to the selected item
4518 m_clientArea
->ChangeCurrentRow( row
);
4522 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4524 int row
= m_clientArea
->GetRowByItem( item
);
4526 m_clientArea
->SelectRow(row
, false);
4529 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4531 int row
= m_clientArea
->GetRowByItem( item
);
4534 return m_clientArea
->IsRowSelected(row
);
4539 void wxDataViewCtrl::SelectAll()
4541 m_clientArea
->SelectAllRows(true);
4544 void wxDataViewCtrl::UnselectAll()
4546 m_clientArea
->SelectAllRows(false);
4549 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4553 if( row
> (int) m_clientArea
->GetRowCount() )
4554 row
= m_clientArea
->GetRowCount();
4556 int first
= m_clientArea
->GetFirstVisibleRow();
4557 int last
= m_clientArea
->GetLastVisibleRow();
4559 m_clientArea
->ScrollTo( row
, column
);
4560 else if( row
> last
)
4561 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4563 m_clientArea
->ScrollTo( first
, column
);
4566 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4568 ExpandAncestors( item
);
4570 m_clientArea
->RecalculateDisplay();
4572 int row
= m_clientArea
->GetRowByItem(item
);
4575 if( column
== NULL
)
4576 EnsureVisible(row
, -1);
4578 EnsureVisible( row
, GetColumnIndex(column
) );
4583 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4584 wxDataViewColumn
* &column
) const
4586 m_clientArea
->HitTest(point
, item
, column
);
4589 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4590 const wxDataViewColumn
* column
) const
4592 return m_clientArea
->GetItemRect(item
, column
);
4595 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4597 return m_clientArea
->GetItemByRow( row
);
4600 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4602 return m_clientArea
->GetRowByItem( item
);
4605 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4607 ExpandAncestors( item
);
4609 int row
= m_clientArea
->GetRowByItem( item
);
4611 m_clientArea
->Expand(row
);
4614 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4616 int row
= m_clientArea
->GetRowByItem( item
);
4618 m_clientArea
->Collapse(row
);
4621 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4623 int row
= m_clientArea
->GetRowByItem( item
);
4625 return m_clientArea
->IsExpanded(row
);
4629 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4631 wxDataViewColumn
* col
= GetColumn( column
);
4635 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4636 if (renderer
->GetMode() != wxDATAVIEW_CELL_EDITABLE
)
4639 const wxRect itemRect
= GetItemRect(item
, col
);
4640 renderer
->StartEditing(item
, itemRect
);
4643 #endif // !wxUSE_GENERICDATAVIEWCTRL
4645 #endif // wxUSE_DATAVIEWCTRL