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
->ProcessWindowEvent(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
);
552 void OnMouse( wxMouseEvent
&event
);
553 void OnSetFocus( wxFocusEvent
&event
);
554 void OnKillFocus( wxFocusEvent
&event
);
556 void UpdateDisplay();
557 void RecalculateDisplay();
558 void OnInternalIdle();
560 void OnRenameTimer();
562 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
563 void ScrollTo( int rows
, int column
);
565 unsigned GetCurrentRow() const { return m_currentRow
; }
566 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
567 void ChangeCurrentRow( unsigned int row
);
569 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
570 bool IsEmpty() { return GetRowCount() == 0; }
572 int GetCountPerPage() const;
573 int GetEndOfLastCol() const;
574 unsigned int GetFirstVisibleRow() const;
576 // I change this method to un const because in the tree view,
577 // the displaying number of the tree are changing along with the
578 // expanding/collapsing of the tree nodes
579 unsigned int GetLastVisibleRow();
580 unsigned int GetRowCount();
582 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
583 void SetSelections( const wxDataViewSelection
& sel
)
584 { m_selection
= sel
; UpdateDisplay(); }
585 void Select( const wxArrayInt
& aSelections
);
586 void SelectAllRows( bool on
);
587 void SelectRow( unsigned int row
, bool on
);
588 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
589 void ReverseRowSelection( unsigned int row
);
590 bool IsRowSelected( unsigned int row
);
591 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
593 void RefreshRow( unsigned int row
);
594 void RefreshRows( unsigned int from
, unsigned int to
);
595 void RefreshRowsAfter( unsigned int firstRow
);
597 // returns the colour to be used for drawing the rules
598 wxColour
GetRuleColour() const
600 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
603 wxRect
GetLineRect( unsigned int row
) const;
605 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
606 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
607 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
609 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
611 // Some useful functions for row and item mapping
612 wxDataViewItem
GetItemByRow( unsigned int row
) const;
613 int GetRowByItem( const wxDataViewItem
& item
) const;
615 // Methods for building the mapping tree
616 void BuildTree( wxDataViewModel
* model
);
618 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
619 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
621 void Expand( unsigned int row
);
622 void Collapse( unsigned int row
);
623 bool IsExpanded( unsigned int row
) const;
624 bool HasChildren( unsigned int row
) const;
626 #if wxUSE_DRAG_AND_DROP
627 bool EnableDragSource( const wxDataFormat
&format
);
628 bool EnableDropTarget( const wxDataFormat
&format
);
630 void RemoveDropHint();
631 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
632 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
633 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
635 #endif // wxUSE_DRAG_AND_DROP
638 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
639 // We did not need this temporarily
640 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
642 int RecalculateCount();
644 // Return false only if the event was vetoed by its handler.
645 bool SendExpanderEvent(wxEventType type
, const wxDataViewItem
& item
);
647 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
650 wxDataViewCtrl
*m_owner
;
654 wxDataViewColumn
*m_currentCol
;
655 unsigned int m_currentRow
;
656 wxDataViewSelection m_selection
;
658 wxDataViewRenameTimer
*m_renameTimer
;
663 #if wxUSE_DRAG_AND_DROP
668 wxDataFormat m_dragFormat
;
671 wxDataFormat m_dropFormat
;
673 unsigned int m_dropHintLine
;
674 #endif // wxUSE_DRAG_AND_DROP
676 // for double click logic
677 unsigned int m_lineLastClicked
,
678 m_lineBeforeLastClicked
,
679 m_lineSelectSingleOnUp
;
681 // the pen used to draw horiz/vertical rules
684 // the pen used to draw the expander and the lines
687 // This is the tree structure of the model
688 wxDataViewTreeNode
* m_root
;
691 // This is the tree node under the cursor
692 wxDataViewTreeNode
* m_underMouse
;
695 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
696 DECLARE_EVENT_TABLE()
699 // ---------------------------------------------------------
700 // wxGenericDataViewModelNotifier
701 // ---------------------------------------------------------
703 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
706 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
707 { m_mainWindow
= mainWindow
; }
709 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
710 { return m_mainWindow
->ItemAdded( parent
, item
); }
711 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
712 { return m_mainWindow
->ItemDeleted( parent
, item
); }
713 virtual bool ItemChanged( const wxDataViewItem
& item
)
714 { return m_mainWindow
->ItemChanged(item
); }
715 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
716 { return m_mainWindow
->ValueChanged( item
, col
); }
717 virtual bool Cleared()
718 { return m_mainWindow
->Cleared(); }
719 virtual void Resort()
720 { m_mainWindow
->Resort(); }
722 wxDataViewMainWindow
*m_mainWindow
;
725 // ---------------------------------------------------------
726 // wxDataViewRenderer
727 // ---------------------------------------------------------
729 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
731 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
732 wxDataViewCellMode mode
,
734 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
738 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
742 wxDataViewRenderer::~wxDataViewRenderer()
747 wxDC
*wxDataViewRenderer::GetDC()
751 if (GetOwner() == NULL
)
753 if (GetOwner()->GetOwner() == NULL
)
755 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
761 void wxDataViewRenderer::SetAlignment( int align
)
766 int wxDataViewRenderer::GetAlignment() const
771 // ---------------------------------------------------------
772 // wxDataViewCustomRenderer
773 // ---------------------------------------------------------
775 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
777 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
778 wxDataViewCellMode mode
, int align
) :
779 wxDataViewRenderer( varianttype
, mode
, align
)
783 // ---------------------------------------------------------
784 // wxDataViewTextRenderer
785 // ---------------------------------------------------------
787 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
789 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
790 wxDataViewCellMode mode
, int align
) :
791 wxDataViewRenderer( varianttype
, mode
, align
)
795 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
797 m_text
= value
.GetString();
802 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
807 bool wxDataViewTextRenderer::HasEditorCtrl() const
812 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
813 wxRect labelRect
, const wxVariant
&value
)
815 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
816 wxPoint(labelRect
.x
,labelRect
.y
),
817 wxSize(labelRect
.width
,labelRect
.height
),
818 wxTE_PROCESS_ENTER
);
820 // select the text in the control an place the cursor at the end
821 ctrl
->SetInsertionPointEnd();
827 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
829 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
830 value
= text
->GetValue();
834 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
836 RenderText(m_text
, 0, rect
, dc
, state
);
840 wxSize
wxDataViewTextRenderer::GetSize() const
843 return GetTextExtent(m_text
);
845 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
848 // ---------------------------------------------------------
849 // wxDataViewBitmapRenderer
850 // ---------------------------------------------------------
852 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
854 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
855 wxDataViewCellMode mode
, int align
) :
856 wxDataViewRenderer( varianttype
, mode
, align
)
860 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
862 if (value
.GetType() == wxT("wxBitmap"))
864 if (value
.GetType() == wxT("wxIcon"))
870 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
875 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
878 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
879 else if (m_icon
.IsOk())
880 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
885 wxSize
wxDataViewBitmapRenderer::GetSize() const
888 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
889 else if (m_icon
.IsOk())
890 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
892 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
895 // ---------------------------------------------------------
896 // wxDataViewToggleRenderer
897 // ---------------------------------------------------------
899 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
901 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
902 wxDataViewCellMode mode
, int align
) :
903 wxDataViewRenderer( varianttype
, mode
, align
)
908 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
910 m_toggle
= value
.GetBool();
915 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
920 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
924 flags
|= wxCONTROL_CHECKED
;
925 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
926 GetEnabled() == false)
927 flags
|= wxCONTROL_DISABLED
;
929 // check boxes we draw must always have the same, standard size (if it's
930 // bigger than the cell size the checkbox will be truncated because the
931 // caller had set the clipping rectangle to prevent us from drawing outside
933 cell
.SetSize(GetSize());
935 wxRendererNative::Get().DrawCheckBox(
936 GetOwner()->GetOwner(),
944 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
946 wxDataViewModel
*model
,
947 const wxDataViewItem
& item
,
950 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
951 if (!wxRect(GetSize()).Contains(cursor
))
954 return WXOnActivate(cell
, model
, item
, col
);
957 bool wxDataViewToggleRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
958 wxDataViewModel
*model
,
959 const wxDataViewItem
& item
,
962 if (model
->IsEnabled(item
, col
))
964 model
->ChangeValue(!m_toggle
, item
, col
);
971 wxSize
wxDataViewToggleRenderer::GetSize() const
973 // the window parameter is not used by GetCheckBoxSize() so it's
975 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
978 // ---------------------------------------------------------
979 // wxDataViewProgressRenderer
980 // ---------------------------------------------------------
982 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
984 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
985 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
986 wxDataViewRenderer( varianttype
, mode
, align
)
992 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
994 m_value
= (long) value
;
996 if (m_value
< 0) m_value
= 0;
997 if (m_value
> 100) m_value
= 100;
1002 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
1004 value
= (long) m_value
;
1009 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
1011 // deflate the rect to leave a small border between bars in adjacent rows
1012 wxRect bar
= rect
.Deflate(0, 1);
1014 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1015 dc
->SetPen( *wxBLACK_PEN
);
1016 dc
->DrawRectangle( bar
);
1018 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
1019 dc
->SetPen( *wxTRANSPARENT_PEN
);
1021 const wxDataViewItemAttr
& attr
= GetAttr();
1022 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
1024 dc
->DrawRectangle( bar
);
1029 wxSize
wxDataViewProgressRenderer::GetSize() const
1031 return wxSize(40,12);
1034 // ---------------------------------------------------------
1035 // wxDataViewDateRenderer
1036 // ---------------------------------------------------------
1038 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
1040 #if wxUSE_DATE_RENDERER_POPUP
1042 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1045 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1046 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
1047 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
1052 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
1053 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1054 sizer
->Add( m_cal
, 1, wxGROW
);
1059 void OnCalendar( wxCalendarEvent
&event
);
1061 wxCalendarCtrl
*m_cal
;
1062 wxDataViewModel
*m_model
;
1064 const wxDataViewItem
& m_item
;
1067 virtual void OnDismiss()
1072 DECLARE_EVENT_TABLE()
1075 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1076 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1079 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1081 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1085 #endif // wxUSE_DATE_RENDERER_POPUP
1087 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1089 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1090 wxDataViewCellMode mode
, int align
) :
1091 wxDataViewRenderer( varianttype
, mode
, align
)
1095 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1097 m_date
= value
.GetDateTime();
1102 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1108 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1110 wxString tmp
= m_date
.FormatDate();
1111 RenderText( tmp
, 0, cell
, dc
, state
);
1115 wxSize
wxDataViewDateRenderer::GetSize() const
1117 return GetTextExtent(m_date
.FormatDate());
1120 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1121 wxDataViewModel
*model
,
1122 const wxDataViewItem
& item
,
1125 wxDateTime dtOld
= m_date
;
1127 #if wxUSE_DATE_RENDERER_POPUP
1128 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1129 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1130 wxPoint pos
= wxGetMousePosition();
1133 popup
->Popup( popup
->m_cal
);
1134 #else // !wxUSE_DATE_RENDERER_POPUP
1135 wxMessageBox(dtOld
.Format());
1136 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1141 // ---------------------------------------------------------
1142 // wxDataViewIconTextRenderer
1143 // ---------------------------------------------------------
1145 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1147 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1148 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1149 wxDataViewRenderer( varianttype
, mode
, align
)
1152 SetAlignment(align
);
1155 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1161 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1166 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1170 const wxIcon
& icon
= m_value
.GetIcon();
1173 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1174 xoffset
= icon
.GetWidth()+4;
1177 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1182 wxSize
wxDataViewIconTextRenderer::GetSize() const
1184 if (!m_value
.GetText().empty())
1186 wxSize size
= GetTextExtent(m_value
.GetText());
1188 if (m_value
.GetIcon().IsOk())
1189 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1192 return wxSize(80,20);
1195 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1197 wxDataViewIconText iconText
;
1200 wxString text
= iconText
.GetText();
1202 // adjust the label rect to take the width of the icon into account
1203 if (iconText
.GetIcon().IsOk())
1205 int w
= iconText
.GetIcon().GetWidth() + 4;
1207 labelRect
.width
-= w
;
1210 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1211 wxPoint(labelRect
.x
,labelRect
.y
),
1212 wxSize(labelRect
.width
,labelRect
.height
),
1213 wxTE_PROCESS_ENTER
);
1215 // select the text in the control an place the cursor at the end
1216 ctrl
->SetInsertionPointEnd();
1222 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1224 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1226 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1231 //-----------------------------------------------------------------------------
1232 // wxDataViewDropTarget
1233 //-----------------------------------------------------------------------------
1235 #if wxUSE_DRAG_AND_DROP
1237 class wxBitmapCanvas
: public wxWindow
1240 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1241 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1244 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1247 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1250 dc
.DrawBitmap( m_bitmap
, 0, 0);
1256 class wxDataViewDropSource
: public wxDropSource
1259 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1267 ~wxDataViewDropSource()
1272 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1274 wxPoint pos
= wxGetMousePosition();
1278 int liney
= m_win
->GetLineStart( m_row
);
1280 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1281 m_win
->ClientToScreen( &linex
, &liney
);
1282 m_dist_x
= pos
.x
- linex
;
1283 m_dist_y
= pos
.y
- liney
;
1286 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1288 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1289 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1291 wxFRAME_TOOL_WINDOW
|
1292 wxFRAME_FLOAT_ON_PARENT
|
1293 wxFRAME_NO_TASKBAR
|
1295 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1300 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1301 m_hint
->SetTransparent( 128 );
1307 wxDataViewMainWindow
*m_win
;
1310 int m_dist_x
,m_dist_y
;
1314 class wxDataViewDropTarget
: public wxDropTarget
1317 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1323 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1325 wxDataFormat format
= GetMatchingPair();
1326 if (format
== wxDF_INVALID
)
1328 return m_win
->OnDragOver( format
, x
, y
, def
);
1331 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1333 wxDataFormat format
= GetMatchingPair();
1334 if (format
== wxDF_INVALID
)
1336 return m_win
->OnDrop( format
, x
, y
);
1339 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1341 wxDataFormat format
= GetMatchingPair();
1342 if (format
== wxDF_INVALID
)
1346 return m_win
->OnData( format
, x
, y
, def
);
1349 virtual void OnLeave()
1350 { m_win
->OnLeave(); }
1352 wxDataViewMainWindow
*m_win
;
1355 #endif // wxUSE_DRAG_AND_DROP
1357 //-----------------------------------------------------------------------------
1358 // wxDataViewRenameTimer
1359 //-----------------------------------------------------------------------------
1361 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1366 void wxDataViewRenameTimer::Notify()
1368 m_owner
->OnRenameTimer();
1371 //-----------------------------------------------------------------------------
1372 // wxDataViewMainWindow
1373 //-----------------------------------------------------------------------------
1375 // The tree building helper, declared firstly
1376 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1377 wxDataViewTreeNode
* node
);
1379 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1381 if (row1
> row2
) return 1;
1382 if (row1
== row2
) return 0;
1386 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1388 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1389 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1390 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1391 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1392 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1393 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1396 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1397 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1398 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1399 m_selection( wxDataViewSelectionCmp
)
1404 m_lastOnSame
= false;
1405 m_renameTimer
= new wxDataViewRenameTimer( this );
1407 // TODO: user better initial values/nothing selected
1408 m_currentCol
= NULL
;
1411 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1413 #if wxUSE_DRAG_AND_DROP
1415 m_dragStart
= wxPoint(0,0);
1417 m_dragEnabled
= false;
1418 m_dropEnabled
= false;
1420 m_dropHintLine
= (unsigned int) -1;
1421 #endif // wxUSE_DRAG_AND_DROP
1423 m_lineLastClicked
= (unsigned int) -1;
1424 m_lineBeforeLastClicked
= (unsigned int) -1;
1425 m_lineSelectSingleOnUp
= (unsigned int) -1;
1429 SetBackgroundColour( *wxWHITE
);
1431 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1433 m_penRule
= wxPen(GetRuleColour());
1435 // compose a pen whichcan draw black lines
1436 // TODO: maybe there is something system colour to use
1437 m_penExpander
= wxPen(wxColour(0,0,0));
1439 m_root
= wxDataViewTreeNode::CreateRootNode();
1441 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1443 m_underMouse
= NULL
;
1447 wxDataViewMainWindow::~wxDataViewMainWindow()
1450 delete m_renameTimer
;
1454 #if wxUSE_DRAG_AND_DROP
1455 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1457 m_dragFormat
= format
;
1458 m_dragEnabled
= format
!= wxDF_INVALID
;
1463 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1465 m_dropFormat
= format
;
1466 m_dropEnabled
= format
!= wxDF_INVALID
;
1469 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1474 void wxDataViewMainWindow::RemoveDropHint()
1479 RefreshRow( m_dropHintLine
);
1480 m_dropHintLine
= (unsigned int) -1;
1484 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1485 wxCoord y
, wxDragResult def
)
1489 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1490 unsigned int row
= GetLineAt( yy
);
1492 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1498 wxDataViewItem item
= GetItemByRow( row
);
1500 wxDataViewModel
*model
= GetModel();
1502 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1503 event
.SetEventObject( m_owner
);
1504 event
.SetItem( item
);
1505 event
.SetModel( model
);
1506 event
.SetDataFormat( format
);
1507 if (!m_owner
->HandleWindowEvent( event
))
1513 if (!event
.IsAllowed())
1520 if (m_dropHint
&& (row
!= m_dropHintLine
))
1521 RefreshRow( m_dropHintLine
);
1523 m_dropHintLine
= row
;
1529 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1535 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1536 unsigned int row
= GetLineAt( yy
);
1538 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1541 wxDataViewItem item
= GetItemByRow( row
);
1543 wxDataViewModel
*model
= GetModel();
1545 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1546 event
.SetEventObject( m_owner
);
1547 event
.SetItem( item
);
1548 event
.SetModel( model
);
1549 event
.SetDataFormat( format
);
1550 if (!m_owner
->HandleWindowEvent( event
))
1553 if (!event
.IsAllowed())
1559 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1564 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1565 unsigned int row
= GetLineAt( yy
);
1567 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1570 wxDataViewItem item
= GetItemByRow( row
);
1572 wxDataViewModel
*model
= GetModel();
1574 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1576 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1577 event
.SetEventObject( m_owner
);
1578 event
.SetItem( item
);
1579 event
.SetModel( model
);
1580 event
.SetDataFormat( format
);
1581 event
.SetDataSize( obj
->GetSize() );
1582 event
.SetDataBuffer( obj
->GetData() );
1583 if (!m_owner
->HandleWindowEvent( event
))
1586 if (!event
.IsAllowed())
1592 void wxDataViewMainWindow::OnLeave()
1597 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1599 int height
= GetLineHeight( row
);
1601 unsigned int cols
= GetOwner()->GetColumnCount();
1603 for (col
= 0; col
< cols
; col
++)
1605 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1606 if (column
->IsHidden())
1607 continue; // skip it!
1608 width
+= column
->GetWidth();
1614 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1615 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1616 indent
= indent
+ m_lineHeight
;
1617 // try to use the m_lineHeight as the expander space
1621 wxBitmap
bitmap( width
, height
);
1622 wxMemoryDC
dc( bitmap
);
1623 dc
.SetFont( GetFont() );
1624 dc
.SetPen( *wxBLACK_PEN
);
1625 dc
.SetBrush( *wxWHITE_BRUSH
);
1626 dc
.DrawRectangle( 0,0,width
,height
);
1628 wxDataViewModel
*model
= m_owner
->GetModel();
1630 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1633 // TODO-RTL: last column for RTL support
1634 expander
= GetOwner()->GetColumnAt( 0 );
1635 GetOwner()->SetExpanderColumn(expander
);
1640 for (col
= 0; col
< cols
; col
++)
1642 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1643 wxDataViewRenderer
*cell
= column
->GetRenderer();
1645 if (column
->IsHidden())
1646 continue; // skip it!
1648 width
= column
->GetWidth();
1650 if (column
== expander
)
1653 wxDataViewItem item
= GetItemByRow( row
);
1654 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1656 wxRect
item_rect(x
, 0, width
, height
);
1657 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1659 // dc.SetClippingRegion( item_rect );
1660 cell
->WXCallRender(item_rect
, &dc
, 0);
1661 // dc.DestroyClippingRegion();
1669 #endif // wxUSE_DRAG_AND_DROP
1672 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1674 wxDataViewModel
*model
= GetModel();
1675 wxAutoBufferedPaintDC
dc( this );
1678 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1679 dc
.SetPen( *wxTRANSPARENT_PEN
);
1680 dc
.DrawRectangle(GetClientSize());
1685 // No items to draw.
1690 GetOwner()->PrepareDC( dc
);
1691 dc
.SetFont( GetFont() );
1693 wxRect update
= GetUpdateRegion().GetBox();
1694 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1696 // compute which items needs to be redrawn
1697 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1698 unsigned int item_count
=
1699 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1700 (int)(GetRowCount( ) - item_start
));
1701 unsigned int item_last
= item_start
+ item_count
;
1703 // Send the event to wxDataViewCtrl itself.
1704 wxWindow
* const parent
= GetParent();
1705 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1706 cache_event
.SetEventObject(parent
);
1707 cache_event
.SetCache(item_start
, item_last
- 1);
1708 parent
->ProcessWindowEvent(cache_event
);
1710 // compute which columns needs to be redrawn
1711 unsigned int cols
= GetOwner()->GetColumnCount();
1714 // we assume that we have at least one column below and painting an
1715 // empty control is unnecessary anyhow
1719 unsigned int col_start
= 0;
1720 unsigned int x_start
;
1721 for (x_start
= 0; col_start
< cols
; col_start
++)
1723 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1724 if (col
->IsHidden())
1725 continue; // skip it!
1727 unsigned int w
= col
->GetWidth();
1728 if (x_start
+w
>= (unsigned int)update
.x
)
1734 unsigned int col_last
= col_start
;
1735 unsigned int x_last
= x_start
;
1736 for (; col_last
< cols
; col_last
++)
1738 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1739 if (col
->IsHidden())
1740 continue; // skip it!
1742 if (x_last
> (unsigned int)update
.GetRight())
1745 x_last
+= col
->GetWidth();
1748 // Draw horizontal rules if required
1749 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1751 dc
.SetPen(m_penRule
);
1752 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1754 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1756 int y
= GetLineStart( i
);
1757 dc
.DrawLine(x_start
, y
, x_last
, y
);
1761 // Draw vertical rules if required
1762 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1764 dc
.SetPen(m_penRule
);
1765 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1767 // NB: Vertical rules are drawn in the last pixel of a column so that
1768 // they align perfectly with native MSW wxHeaderCtrl as well as for
1769 // consistency with MSW native list control. There's no vertical
1770 // rule at the most-left side of the control.
1772 int x
= x_start
- 1;
1773 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1775 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1776 if (col
->IsHidden())
1777 continue; // skip it
1779 x
+= col
->GetWidth();
1781 dc
.DrawLine(x
, GetLineStart( item_start
),
1782 x
, GetLineStart( item_last
) );
1786 // redraw the background for the items which are selected/current
1787 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1789 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1790 if (selected
|| item
== m_currentRow
)
1792 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1793 if (item
== m_currentRow
)
1794 flags
|= wxCONTROL_CURRENT
;
1796 flags
|= wxCONTROL_FOCUSED
;
1798 wxRect
rect( x_start
, GetLineStart( item
),
1799 x_last
- x_start
, GetLineHeight( item
) );
1800 wxRendererNative::Get().DrawItemSelectionRect
1810 #if wxUSE_DRAG_AND_DROP
1813 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1814 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1815 dc
.SetPen( *wxBLACK_PEN
);
1816 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1817 dc
.DrawRectangle( rect
);
1819 #endif // wxUSE_DRAG_AND_DROP
1821 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1824 // TODO-RTL: last column for RTL support
1825 expander
= GetOwner()->GetColumnAt( 0 );
1826 GetOwner()->SetExpanderColumn(expander
);
1829 // redraw all cells for all rows which must be repainted and all columns
1831 cell_rect
.x
= x_start
;
1832 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1834 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1835 wxDataViewRenderer
*cell
= col
->GetRenderer();
1836 cell_rect
.width
= col
->GetWidth();
1838 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1839 continue; // skip it!
1841 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1843 // get the cell value and set it into the renderer
1844 wxDataViewTreeNode
*node
= NULL
;
1845 wxDataViewItem dataitem
;
1847 if (!IsVirtualList())
1849 node
= GetTreeNodeByRow(item
);
1853 dataitem
= node
->GetItem();
1855 // Skip all columns of "container" rows except the expander
1856 // column itself unless HasContainerColumns() overrides this.
1857 if ( col
!= GetOwner()->GetExpanderColumn() &&
1858 model
->IsContainer(dataitem
) &&
1859 !model
->HasContainerColumns(dataitem
) )
1864 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1867 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1870 cell_rect
.y
= GetLineStart( item
);
1871 cell_rect
.height
= GetLineHeight( item
);
1873 // deal with the expander
1875 if ((!IsList()) && (col
== expander
))
1877 // Calculate the indent first
1878 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1880 // we reserve m_lineHeight of horizontal space for the expander
1881 // but leave EXPANDER_MARGIN around the expander itself
1882 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1884 indent
+= m_lineHeight
;
1886 // draw expander if needed and visible
1887 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1889 dc
.SetPen( m_penExpander
);
1890 dc
.SetBrush( wxNullBrush
);
1892 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1893 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1894 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1896 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1899 if ( m_underMouse
== node
)
1900 flag
|= wxCONTROL_CURRENT
;
1901 if ( node
->IsOpen() )
1902 flag
|= wxCONTROL_EXPANDED
;
1904 // ensure that we don't overflow the cell (which might
1905 // happen if the column is very narrow)
1906 wxDCClipper
clip(dc
, cell_rect
);
1908 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1911 // force the expander column to left-center align
1912 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1915 wxRect item_rect
= cell_rect
;
1916 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1918 // account for the tree indent (harmless if we're not indented)
1919 item_rect
.x
+= indent
;
1920 item_rect
.width
-= indent
;
1922 if ( item_rect
.width
<= 0 )
1926 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1927 state
|= wxDATAVIEW_CELL_SELECTED
;
1929 // TODO: it would be much more efficient to create a clipping
1930 // region for the entire column being rendered (in the OnPaint
1931 // of wxDataViewMainWindow) instead of a single clip region for
1932 // each cell. However it would mean that each renderer should
1933 // respect the given wxRect's top & bottom coords, eventually
1934 // violating only the left & right coords - however the user can
1935 // make its own renderer and thus we cannot be sure of that.
1936 wxDCClipper
clip(dc
, item_rect
);
1938 cell
->WXCallRender(item_rect
, &dc
, state
);
1941 cell_rect
.x
+= cell_rect
.width
;
1945 void wxDataViewMainWindow::OnRenameTimer()
1947 // We have to call this here because changes may just have
1948 // been made and no screen update taken place.
1951 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1952 // (needs to be tested!)
1956 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1958 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1960 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1963 //-----------------------------------------------------------------------------
1964 // Helper class for do operation on the tree node
1965 //-----------------------------------------------------------------------------
1970 virtual ~DoJob() { }
1972 // The return value control how the tree-walker tranverse the tree
1975 DONE
, // Job done, stop traversing and return
1976 SKIP_SUBTREE
, // Ignore the current node's subtree and continue
1977 CONTINUE
// Job not done, continue
1980 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1983 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1985 wxCHECK_MSG( node
, false, "can't walk NULL node" );
1987 switch( func( node
) )
1991 case DoJob::SKIP_SUBTREE
:
1993 case DoJob::CONTINUE
:
1997 if ( node
->HasChildren() )
1999 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2001 for ( wxDataViewTreeNodes::const_iterator i
= nodes
.begin();
2005 if ( Walker(*i
, func
) )
2013 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2015 if (IsVirtualList())
2017 wxDataViewVirtualListModel
*list_model
=
2018 (wxDataViewVirtualListModel
*) GetModel();
2019 m_count
= list_model
->GetCount();
2025 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2030 wxDataViewItemArray siblings
;
2031 GetModel()->GetChildren(parent
, siblings
);
2032 int itemPos
= siblings
.Index(item
, /*fromEnd=*/true);
2033 wxCHECK_MSG( itemPos
!= wxNOT_FOUND
, false, "adding non-existent item?" );
2035 wxDataViewTreeNode
*itemNode
= new wxDataViewTreeNode(parentNode
, item
);
2036 itemNode
->SetHasChildren(GetModel()->IsContainer(item
));
2038 parentNode
->SetHasChildren(true);
2039 parentNode
->InsertChild(itemNode
, itemPos
);
2040 parentNode
->ChangeSubTreeCount(+1);
2045 GetOwner()->InvalidateColBestWidths();
2051 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2052 const wxDataViewItem
& item
)
2054 if (IsVirtualList())
2056 wxDataViewVirtualListModel
*list_model
=
2057 (wxDataViewVirtualListModel
*) GetModel();
2058 m_count
= list_model
->GetCount();
2060 if ( !m_selection
.empty() )
2062 const int row
= GetRowByItem(item
);
2064 int rowIndexInSelection
= wxNOT_FOUND
;
2066 const size_t selCount
= m_selection
.size();
2067 for ( size_t i
= 0; i
< selCount
; i
++ )
2069 if ( m_selection
[i
] == (unsigned)row
)
2070 rowIndexInSelection
= i
;
2071 else if ( m_selection
[i
] > (unsigned)row
)
2075 if ( rowIndexInSelection
!= wxNOT_FOUND
)
2076 m_selection
.RemoveAt(rowIndexInSelection
);
2080 else // general case
2082 wxDataViewTreeNode
*parentNode
= FindNode(parent
);
2084 // Notice that it is possible that the item being deleted is not in the
2085 // tree at all, for example we could be deleting a never shown (because
2086 // collapsed) item in a tree model. So it's not an error if we don't know
2087 // about this item, just return without doing anything then.
2091 wxCHECK_MSG( parentNode
->HasChildren(), false, "parent node doesn't have children?" );
2092 const wxDataViewTreeNodes
& parentsChildren
= parentNode
->GetChildNodes();
2094 // We can't use FindNode() to find 'item', because it was already
2095 // removed from the model by the time ItemDeleted() is called, so we
2096 // have to do it manually. We keep track of its position as well for
2098 int itemPosInNode
= 0;
2099 wxDataViewTreeNode
*itemNode
= NULL
;
2100 for ( wxDataViewTreeNodes::const_iterator i
= parentsChildren
.begin();
2101 i
!= parentsChildren
.end();
2102 ++i
, ++itemPosInNode
)
2104 if( (*i
)->GetItem() == item
)
2111 // If the parent wasn't expanded, it's possible that we didn't have a
2112 // node corresponding to 'item' and so there's nothing left to do.
2115 // If this was the last child to be removed, it's possible the parent
2116 // node became a leaf. Let's ask the model about it.
2117 if ( parentNode
->GetChildNodes().empty() )
2118 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2123 // Delete the item from wxDataViewTreeNode representation:
2124 const int itemsDeleted
= 1 + itemNode
->GetSubTreeCount();
2126 parentNode
->RemoveChild(itemNode
);
2128 parentNode
->ChangeSubTreeCount(-itemsDeleted
);
2130 // Make the row number invalid and get a new valid one when user call GetRowCount
2133 // If this was the last child to be removed, it's possible the parent
2134 // node became a leaf. Let's ask the model about it.
2135 if ( parentNode
->GetChildNodes().empty() )
2136 parentNode
->SetHasChildren(GetModel()->IsContainer(parent
));
2138 // Update selection by removing 'item' and its entire children tree from the selection.
2139 if ( !m_selection
.empty() )
2141 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2142 // the parent ('parentNode') and position in its list of children
2144 if ( itemPosInNode
== 0 )
2146 // 1st child, row number is that of the parent parentNode + 1
2147 itemRow
= GetRowByItem(parentNode
->GetItem()) + 1;
2151 // row number is that of the sibling above 'item' + its subtree if any + 1
2152 const wxDataViewTreeNode
*siblingNode
= parentNode
->GetChildNodes()[itemPosInNode
- 1];
2154 itemRow
= GetRowByItem(siblingNode
->GetItem()) +
2155 siblingNode
->GetSubTreeCount() +
2159 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2161 const size_t numSelections
= m_selection
.size();
2162 for ( size_t i
= 0; i
< numSelections
; ++i
)
2164 const int s
= m_selection
[i
];
2166 newsel
.push_back(s
);
2167 else if ( s
>= itemRow
+ itemsDeleted
)
2168 newsel
.push_back(s
- itemsDeleted
);
2169 // else: deleted item, remove from selection
2172 m_selection
= newsel
;
2176 // Change the current row to the last row if the current exceed the max row number
2177 if( m_currentRow
> GetRowCount() )
2178 ChangeCurrentRow(m_count
- 1);
2180 GetOwner()->InvalidateColBestWidths();
2186 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2191 GetOwner()->InvalidateColBestWidths();
2194 wxWindow
*parent
= GetParent();
2195 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2196 le
.SetEventObject(parent
);
2197 le
.SetModel(GetModel());
2199 parent
->ProcessWindowEvent(le
);
2204 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2206 int view_column
= -1;
2207 unsigned int n_col
= m_owner
->GetColumnCount();
2208 for (unsigned i
= 0; i
< n_col
; i
++)
2210 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2211 if (column
->GetModelColumn() == model_column
)
2213 view_column
= (int) i
;
2217 if (view_column
== -1)
2220 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2221 /*#define MAX_VIRTUAL_WIDTH 100000
2223 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2224 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2225 Refresh( true, &rect );
2232 GetOwner()->InvalidateColBestWidth(view_column
);
2235 wxWindow
*parent
= GetParent();
2236 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2237 le
.SetEventObject(parent
);
2238 le
.SetModel(GetModel());
2240 le
.SetColumn(view_column
);
2241 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2242 parent
->ProcessWindowEvent(le
);
2247 bool wxDataViewMainWindow::Cleared()
2250 m_selection
.Clear();
2253 BuildTree( GetModel() );
2255 GetOwner()->InvalidateColBestWidths();
2261 void wxDataViewMainWindow::UpdateDisplay()
2264 m_underMouse
= NULL
;
2267 void wxDataViewMainWindow::OnInternalIdle()
2269 wxWindow::OnInternalIdle();
2273 RecalculateDisplay();
2278 void wxDataViewMainWindow::RecalculateDisplay()
2280 wxDataViewModel
*model
= GetModel();
2287 int width
= GetEndOfLastCol();
2288 int height
= GetLineStart( GetRowCount() );
2290 SetVirtualSize( width
, height
);
2291 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2296 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2298 m_underMouse
= NULL
;
2300 wxWindow::ScrollWindow( dx
, dy
, rect
);
2302 if (GetOwner()->m_headerArea
)
2303 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2306 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2308 m_underMouse
= NULL
;
2311 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2312 int sy
= GetLineStart( rows
)/y
;
2316 wxRect rect
= GetClientRect();
2320 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2321 for (x_start
= 0; colnum
< column
; colnum
++)
2323 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2324 if (col
->IsHidden())
2325 continue; // skip it!
2327 w
= col
->GetWidth();
2331 int x_end
= x_start
+ w
;
2332 xe
= xx
+ rect
.width
;
2335 sx
= ( xx
+ x_end
- xe
)/x
;
2342 m_owner
->Scroll( sx
, sy
);
2345 int wxDataViewMainWindow::GetCountPerPage() const
2347 wxSize size
= GetClientSize();
2348 return size
.y
/ m_lineHeight
;
2351 int wxDataViewMainWindow::GetEndOfLastCol() const
2355 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2357 const wxDataViewColumn
*c
=
2358 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2361 width
+= c
->GetWidth();
2366 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2370 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2372 return GetLineAt( y
);
2375 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2377 wxSize client_size
= GetClientSize();
2378 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2379 &client_size
.x
, &client_size
.y
);
2381 // we should deal with the pixel here
2382 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2384 return wxMin( GetRowCount()-1, row
);
2387 unsigned int wxDataViewMainWindow::GetRowCount()
2389 if ( m_count
== -1 )
2391 m_count
= RecalculateCount();
2397 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2404 void wxDataViewMainWindow::SelectAllRows( bool on
)
2411 m_selection
.Clear();
2412 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2413 m_selection
.Add( i
);
2418 unsigned int first_visible
= GetFirstVisibleRow();
2419 unsigned int last_visible
= GetLastVisibleRow();
2421 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2423 unsigned int row
= m_selection
[i
];
2424 if ((row
>= first_visible
) && (row
<= last_visible
))
2427 m_selection
.Clear();
2431 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2433 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2437 m_selection
.Add( row
);
2445 m_selection
.Remove( row
);
2451 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2455 unsigned int tmp
= from
;
2461 for (i
= from
; i
<= to
; i
++)
2463 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2466 m_selection
.Add( i
);
2471 m_selection
.Remove( i
);
2474 RefreshRows( from
, to
);
2477 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2479 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2481 int n
= aSelections
[i
];
2483 m_selection
.Add( n
);
2488 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2490 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2491 m_selection
.Add( row
);
2493 m_selection
.Remove( row
);
2497 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2499 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2502 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2504 wxWindow
*parent
= GetParent();
2505 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2507 le
.SetEventObject(parent
);
2508 le
.SetModel(GetModel());
2511 parent
->ProcessWindowEvent(le
);
2514 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2516 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2517 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2519 wxSize client_size
= GetClientSize();
2520 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2521 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2522 if (intersect_rect
.width
> 0)
2523 Refresh( true, &intersect_rect
);
2526 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2530 unsigned int tmp
= to
;
2535 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2536 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2538 wxSize client_size
= GetClientSize();
2539 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2540 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2541 if (intersect_rect
.width
> 0)
2542 Refresh( true, &intersect_rect
);
2545 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2547 wxSize client_size
= GetClientSize();
2548 int start
= GetLineStart( firstRow
);
2549 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2550 if (start
> client_size
.y
) return;
2552 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2554 Refresh( true, &rect
);
2557 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2561 rect
.y
= GetLineStart( row
);
2562 rect
.width
= GetEndOfLastCol();
2563 rect
.height
= GetLineHeight( row
);
2568 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2570 const wxDataViewModel
*model
= GetModel();
2572 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2574 // TODO make more efficient
2579 for (r
= 0; r
< row
; r
++)
2581 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2582 if (!node
) return start
;
2584 wxDataViewItem item
= node
->GetItem();
2586 unsigned int cols
= GetOwner()->GetColumnCount();
2588 int height
= m_lineHeight
;
2589 for (col
= 0; col
< cols
; col
++)
2591 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2592 if (column
->IsHidden())
2593 continue; // skip it!
2596 model
->IsContainer(item
) &&
2597 !model
->HasContainerColumns(item
))
2598 continue; // skip it!
2600 wxDataViewRenderer
*renderer
=
2601 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2602 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2604 height
= wxMax( height
, renderer
->GetSize().y
);
2614 return row
* m_lineHeight
;
2618 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2620 const wxDataViewModel
*model
= GetModel();
2622 // check for the easy case first
2623 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2624 return y
/ m_lineHeight
;
2626 // TODO make more efficient
2627 unsigned int row
= 0;
2628 unsigned int yy
= 0;
2631 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2634 // not really correct...
2635 return row
+ ((y
-yy
) / m_lineHeight
);
2638 wxDataViewItem item
= node
->GetItem();
2640 unsigned int cols
= GetOwner()->GetColumnCount();
2642 int height
= m_lineHeight
;
2643 for (col
= 0; col
< cols
; col
++)
2645 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2646 if (column
->IsHidden())
2647 continue; // skip it!
2650 model
->IsContainer(item
) &&
2651 !model
->HasContainerColumns(item
))
2652 continue; // skip it!
2654 wxDataViewRenderer
*renderer
=
2655 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2656 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2658 height
= wxMax( height
, renderer
->GetSize().y
);
2669 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2671 const wxDataViewModel
*model
= GetModel();
2673 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2675 wxASSERT( !IsVirtualList() );
2677 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2678 // wxASSERT( node );
2679 if (!node
) return m_lineHeight
;
2681 wxDataViewItem item
= node
->GetItem();
2683 int height
= m_lineHeight
;
2685 unsigned int cols
= GetOwner()->GetColumnCount();
2687 for (col
= 0; col
< cols
; col
++)
2689 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2690 if (column
->IsHidden())
2691 continue; // skip it!
2694 model
->IsContainer(item
) &&
2695 !model
->HasContainerColumns(item
))
2696 continue; // skip it!
2698 wxDataViewRenderer
*renderer
=
2699 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2700 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2702 height
= wxMax( height
, renderer
->GetSize().y
);
2709 return m_lineHeight
;
2714 class RowToTreeNodeJob
: public DoJob
2717 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2720 this->current
= current
;
2725 virtual int operator() ( wxDataViewTreeNode
* node
)
2728 if( current
== static_cast<int>(row
))
2734 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2736 current
+= node
->GetSubTreeCount();
2737 return DoJob::SKIP_SUBTREE
;
2743 // If the current node has only leaf children, we can find the
2744 // desired node directly. This can speed up finding the node
2745 // in some cases, and will have a very good effect for list views.
2746 if ( node
->HasChildren() &&
2747 (int)node
->GetChildNodes().size() == node
->GetSubTreeCount() )
2749 const int index
= static_cast<int>(row
) - current
- 1;
2750 ret
= node
->GetChildNodes()[index
];
2754 return DoJob::CONTINUE
;
2758 wxDataViewTreeNode
* GetResult() const
2764 wxDataViewTreeNode
* ret
;
2765 wxDataViewTreeNode
* parent
;
2768 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2770 wxASSERT( !IsVirtualList() );
2772 RowToTreeNodeJob
job( row
, -2, m_root
);
2773 Walker( m_root
, job
);
2774 return job
.GetResult();
2777 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2779 if (IsVirtualList())
2781 return wxDataViewItem( wxUIntToPtr(row
+1) );
2785 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2786 return node
? node
->GetItem() : wxDataViewItem();
2791 wxDataViewMainWindow::SendExpanderEvent(wxEventType type
,
2792 const wxDataViewItem
& item
)
2794 wxWindow
*parent
= GetParent();
2795 wxDataViewEvent
le(type
, parent
->GetId());
2797 le
.SetEventObject(parent
);
2798 le
.SetModel(GetModel());
2801 return !parent
->ProcessWindowEvent(le
) || le
.IsAllowed();
2804 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2809 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2813 if (!node
->HasChildren())
2816 return node
->IsOpen();
2819 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2824 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2828 if (!node
->HasChildren())
2834 void wxDataViewMainWindow::Expand( unsigned int row
)
2839 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2843 if (!node
->HasChildren())
2846 if (!node
->IsOpen())
2848 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem()) )
2850 // Vetoed by the event handler.
2856 // build the children of current node
2857 if( node
->GetChildNodes().empty() )
2860 ::BuildTreeHelper(GetModel(), node
->GetItem(), node
);
2863 // By expanding the node all row indices that are currently in the selection list
2864 // and are greater than our node have become invalid. So we have to correct that now.
2865 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2866 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2868 const unsigned testRow
= m_selection
[i
];
2869 // all rows above us are not affected, so skip them
2873 m_selection
[i
] += rowAdjustment
;
2876 if(m_currentRow
> row
)
2877 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2881 // Send the expanded event
2882 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2886 void wxDataViewMainWindow::Collapse(unsigned int row
)
2891 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2895 if (!node
->HasChildren())
2900 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem()) )
2902 // Vetoed by the event handler.
2906 // Find out if there are selected items below the current node.
2907 bool selectCollapsingRow
= false;
2908 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2909 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2910 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2912 const unsigned testRow
= m_selection
[i
];
2913 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2915 selectCollapsingRow
= true;
2916 // get out as soon as we have found a node that is selected
2923 // If the node to be closed has selected items the user won't see those any longer.
2924 // We select the collapsing node in this case.
2925 if(selectCollapsingRow
)
2927 SelectAllRows(false);
2928 ChangeCurrentRow(row
);
2929 SelectRow(row
, true);
2930 SendSelectionChangedEvent(GetItemByRow(row
));
2934 // if there were no selected items below our node we still need to "fix" the
2935 // selection list to adjust for the changing of the row indices.
2936 // We actually do the opposite of what we are doing in Expand().
2937 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2939 const unsigned testRow
= m_selection
[i
];
2940 // all rows above us are not affected, so skip them
2944 m_selection
[i
] -= rowAdjustment
;
2947 // if the "current row" is being collapsed away we change it to the current row ;-)
2948 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2949 ChangeCurrentRow(row
);
2950 else if(m_currentRow
> row
)
2951 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2956 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2960 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2962 const wxDataViewModel
* model
= GetModel();
2969 // Compose the parent-chain for the item we are looking for
2970 wxVector
<wxDataViewItem
> parentChain
;
2971 wxDataViewItem
it( item
);
2974 parentChain
.push_back(it
);
2975 it
= model
->GetParent(it
);
2978 // Find the item along the parent-chain.
2979 // This algorithm is designed to speed up the node-finding method
2980 wxDataViewTreeNode
* node
= m_root
;
2981 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
2983 if( node
->HasChildren() )
2985 if( node
->GetChildNodes().empty() )
2987 // Even though the item is a container, it doesn't have any
2988 // child nodes in the control's representation yet. We have
2989 // to realize its subtree now.
2991 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2994 const wxDataViewTreeNodes
& nodes
= node
->GetChildNodes();
2997 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
2999 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3000 if (currentNode
->GetItem() == parentChain
[iter
])
3002 if (currentNode
->GetItem() == item
)
3022 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3023 wxDataViewColumn
* &column
)
3025 wxDataViewColumn
*col
= NULL
;
3026 unsigned int cols
= GetOwner()->GetColumnCount();
3027 unsigned int colnum
= 0;
3029 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3030 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3032 col
= GetOwner()->GetColumnAt(colnum
);
3033 if (col
->IsHidden())
3034 continue; // skip it!
3036 unsigned int w
= col
->GetWidth();
3037 if (x_start
+w
>= (unsigned int)x
)
3044 item
= GetItemByRow( GetLineAt( y
) );
3047 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3048 const wxDataViewColumn
* column
)
3053 unsigned int cols
= GetOwner()->GetColumnCount();
3054 // If column is null the loop will compute the combined width of all columns.
3055 // Otherwise, it will compute the x position of the column we are looking for.
3056 for (unsigned int i
= 0; i
< cols
; i
++)
3058 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3063 if (col
->IsHidden())
3064 continue; // skip it!
3066 xpos
+= col
->GetWidth();
3067 width
+= col
->GetWidth();
3072 // If we have a column, we need can get its width directly.
3073 if(column
->IsHidden())
3076 width
= column
->GetWidth();
3081 // If we have no column, we reset the x position back to zero.
3085 // we have to take an expander column into account and compute its indentation
3086 // to get the correct x position where the actual text is
3088 int row
= GetRowByItem(item
);
3089 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3091 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3092 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3093 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3096 wxRect
itemRect( xpos
+ indent
,
3097 GetLineStart( row
),
3099 GetLineHeight( row
) );
3101 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3102 &itemRect
.x
, &itemRect
.y
);
3107 int wxDataViewMainWindow::RecalculateCount()
3109 if (IsVirtualList())
3111 wxDataViewVirtualListModel
*list_model
=
3112 (wxDataViewVirtualListModel
*) GetModel();
3114 return list_model
->GetCount();
3118 return m_root
->GetSubTreeCount();
3122 class ItemToRowJob
: public DoJob
3125 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3132 // Maybe binary search will help to speed up this process
3133 virtual int operator() ( wxDataViewTreeNode
* node
)
3136 if( node
->GetItem() == item
)
3141 if( node
->GetItem() == *m_iter
)
3144 return DoJob::CONTINUE
;
3148 ret
+= node
->GetSubTreeCount();
3149 return DoJob::SKIP_SUBTREE
;
3154 // the row number is begin from zero
3155 int GetResult() const
3159 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3160 wxDataViewItem item
;
3165 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3167 const wxDataViewModel
* model
= GetModel();
3171 if (IsVirtualList())
3173 return wxPtrToUInt( item
.GetID() ) -1;
3180 // Compose the parent-chain of the item we are looking for
3181 wxVector
<wxDataViewItem
> parentChain
;
3182 wxDataViewItem
it( item
);
3185 parentChain
.push_back(it
);
3186 it
= model
->GetParent(it
);
3189 // add an 'invalid' item to represent our 'invisible' root node
3190 parentChain
.push_back(wxDataViewItem());
3192 // the parent chain was created by adding the deepest parent first.
3193 // so if we want to start at the root node, we have to iterate backwards through the vector
3194 ItemToRowJob
job( item
, parentChain
.rbegin() );
3195 Walker( m_root
, job
);
3196 return job
.GetResult();
3200 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3201 wxDataViewTreeNode
* node
)
3203 if( !model
->IsContainer( item
) )
3206 wxDataViewItemArray children
;
3207 unsigned int num
= model
->GetChildren( item
, children
);
3209 for ( unsigned int index
= 0; index
< num
; index
++ )
3211 wxDataViewTreeNode
*n
= new wxDataViewTreeNode(node
, children
[index
]);
3213 if( model
->IsContainer(children
[index
]) )
3214 n
->SetHasChildren( true );
3216 node
->InsertChild(n
, index
);
3219 wxASSERT( node
->IsOpen() );
3220 node
->ChangeSubTreeCount(+num
);
3223 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3227 if (GetModel()->IsVirtualListModel())
3233 m_root
= wxDataViewTreeNode::CreateRootNode();
3235 // First we define a invalid item to fetch the top-level elements
3236 wxDataViewItem item
;
3238 BuildTreeHelper( model
, item
, m_root
);
3242 void wxDataViewMainWindow::DestroyTree()
3244 if (!IsVirtualList())
3251 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3253 wxWindow
* const parent
= GetParent();
3255 // propagate the char event upwards
3256 wxKeyEvent
eventForParent(event
);
3257 eventForParent
.SetEventObject(parent
);
3258 if ( parent
->ProcessWindowEvent(eventForParent
) )
3261 if ( parent
->HandleAsNavigationKey(event
) )
3264 // no item -> nothing to do
3265 if (!HasCurrentRow())
3271 // don't use m_linesPerPage directly as it might not be computed yet
3272 const int pageSize
= GetCountPerPage();
3273 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3275 switch ( event
.GetKeyCode() )
3279 // Enter activates the item, i.e. sends wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED to
3280 // it. Only if that event is not handled do we activate column renderer (which
3281 // is normally done by Space).
3283 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3285 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3288 le
.SetEventObject(parent
);
3289 le
.SetModel(GetModel());
3291 if ( parent
->ProcessWindowEvent(le
) )
3293 // else: fall through to WXK_SPACE handling
3298 // Activate the first activatable column if there is any:
3299 wxDataViewColumn
*activatableCol
= NULL
;
3301 const unsigned cols
= GetOwner()->GetColumnCount();
3302 for ( unsigned i
= 0; i
< cols
; i
++ )
3304 wxDataViewColumn
*c
= GetOwner()->GetColumnAt(i
);
3305 if ( c
->IsHidden() )
3307 if ( c
->GetRenderer()->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3314 if ( activatableCol
)
3316 const wxDataViewItem item
= GetItemByRow(m_currentRow
);
3318 const unsigned colIdx
= activatableCol
->GetModelColumn();
3319 const wxRect cell_rect
= GetOwner()->GetItemRect(item
, activatableCol
);
3321 wxDataViewRenderer
*cell
= activatableCol
->GetRenderer();
3322 cell
->PrepareForItem(GetModel(), item
, colIdx
);
3323 cell
->WXOnActivate(cell_rect
, GetModel(), item
, colIdx
);
3329 if ( m_currentRow
> 0 )
3330 OnVerticalNavigation( m_currentRow
- 1, event
);
3334 if ( m_currentRow
+ 1 < GetRowCount() )
3335 OnVerticalNavigation( m_currentRow
+ 1, event
);
3337 // Add the process for tree expanding/collapsing
3349 OnVerticalNavigation( GetRowCount() - 1, event
);
3354 OnVerticalNavigation( 0, event
);
3359 int steps
= pageSize
- 1;
3360 int index
= m_currentRow
- steps
;
3364 OnVerticalNavigation( index
, event
);
3370 int steps
= pageSize
- 1;
3371 unsigned int index
= m_currentRow
+ steps
;
3372 unsigned int count
= GetRowCount();
3373 if ( index
>= count
)
3376 OnVerticalNavigation( index
, event
);
3382 if(m_selection
.size() == 1)
3384 // TODO: we need to revise that when we have a concept for a 'current column'
3385 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3395 void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent
, const wxKeyEvent
& event
)
3397 wxCHECK_RET( newCurrent
< GetRowCount(),
3398 wxT("invalid item index in OnVerticalNavigation()") );
3400 // if there is no selection, we cannot move it anywhere
3401 if (!HasCurrentRow())
3404 unsigned int oldCurrent
= m_currentRow
;
3406 // in single selection we just ignore Shift as we can't select several
3408 if ( event
.ShiftDown() && !IsSingleSel() )
3410 RefreshRow( oldCurrent
);
3412 ChangeCurrentRow( newCurrent
);
3414 // select all the items between the old and the new one
3415 if ( oldCurrent
> newCurrent
)
3417 newCurrent
= oldCurrent
;
3418 oldCurrent
= m_currentRow
;
3421 SelectRows( oldCurrent
, newCurrent
, true );
3422 if (oldCurrent
!=newCurrent
)
3423 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
3427 RefreshRow( oldCurrent
);
3429 // all previously selected items are unselected unless ctrl is held
3430 if ( !event
.ControlDown() )
3431 SelectAllRows(false);
3433 ChangeCurrentRow( newCurrent
);
3435 if ( !event
.ControlDown() )
3437 SelectRow( m_currentRow
, true );
3438 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3441 RefreshRow( m_currentRow
);
3444 GetOwner()->EnsureVisible( m_currentRow
, -1 );
3447 void wxDataViewMainWindow::OnLeftKey()
3452 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3456 if (node
->HasChildren() && node
->IsOpen())
3458 Collapse(m_currentRow
);
3460 else // if the node is already closed we move the selection to its parent
3462 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3466 int parent
= GetRowByItem( parent_node
->GetItem() );
3469 unsigned int row
= m_currentRow
;
3470 SelectRow( row
, false);
3471 SelectRow( parent
, true );
3472 ChangeCurrentRow( parent
);
3473 GetOwner()->EnsureVisible( parent
, -1 );
3474 SendSelectionChangedEvent( parent_node
->GetItem() );
3480 void wxDataViewMainWindow::OnRightKey()
3482 if (!IsExpanded( m_currentRow
))
3483 Expand( m_currentRow
);
3486 unsigned int row
= m_currentRow
;
3487 SelectRow( row
, false );
3488 SelectRow( row
+ 1, true );
3489 ChangeCurrentRow( row
+ 1 );
3490 GetOwner()->EnsureVisible( row
+ 1, -1 );
3491 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3495 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3497 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3499 // let the base handle mouse wheel events.
3504 // set the focus to ourself if any of the mouse buttons are pressed
3505 if(event
.ButtonDown() && !HasFocus())
3508 int x
= event
.GetX();
3509 int y
= event
.GetY();
3510 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3511 wxDataViewColumn
*col
= NULL
;
3514 unsigned int cols
= GetOwner()->GetColumnCount();
3516 for (i
= 0; i
< cols
; i
++)
3518 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3520 continue; // skip it!
3522 if (x
< xpos
+ c
->GetWidth())
3527 xpos
+= c
->GetWidth();
3535 wxDataViewRenderer
*cell
= col
->GetRenderer();
3536 unsigned int current
= GetLineAt( y
);
3537 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3539 // Unselect all if below the last row ?
3544 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3545 // button) and also determine the offset of the real cell start, skipping
3546 // the indentation and the expander itself.
3547 bool hoverOverExpander
= false;
3549 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3551 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3553 int indent
= node
->GetIndentLevel();
3554 itemOffset
= GetOwner()->GetIndent()*indent
;
3556 if ( node
->HasChildren() )
3558 // we make the rectangle we are looking in a bit bigger than the actual
3559 // visual expander so the user can hit that little thing reliably
3560 wxRect
rect(itemOffset
,
3561 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3562 m_lineHeight
, m_lineHeight
);
3564 if( rect
.Contains(x
, y
) )
3566 // So the mouse is over the expander
3567 hoverOverExpander
= true;
3568 if (m_underMouse
&& m_underMouse
!= node
)
3570 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3571 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3573 if (m_underMouse
!= node
)
3575 // wxLogMessage("Do the row: %d", current);
3576 RefreshRow(current
);
3578 m_underMouse
= node
;
3582 // Account for the expander as well, even if this item doesn't have it,
3583 // its parent does so it still counts for the offset.
3584 itemOffset
+= m_lineHeight
;
3586 if (!hoverOverExpander
)
3588 if (m_underMouse
!= NULL
)
3590 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3591 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3592 m_underMouse
= NULL
;
3596 wxDataViewModel
*model
= GetModel();
3598 #if wxUSE_DRAG_AND_DROP
3599 if (event
.Dragging())
3601 if (m_dragCount
== 0)
3603 // we have to report the raw, physical coords as we want to be
3604 // able to call HitTest(event.m_pointDrag) from the user code to
3605 // get the item being dragged
3606 m_dragStart
= event
.GetPosition();
3611 if (m_dragCount
!= 3)
3614 if (event
.LeftIsDown())
3616 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3617 &m_dragStart
.x
, &m_dragStart
.y
);
3618 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3619 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3621 // Notify cell about drag
3622 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3623 event
.SetEventObject( m_owner
);
3624 event
.SetItem( item
);
3625 event
.SetModel( model
);
3626 if (!m_owner
->HandleWindowEvent( event
))
3629 if (!event
.IsAllowed())
3632 wxDataObject
*obj
= event
.GetDataObject();
3636 wxDataViewDropSource
drag( this, drag_item_row
);
3637 drag
.SetData( *obj
);
3638 /* wxDragResult res = */ drag
.DoDragDrop();
3647 #endif // wxUSE_DRAG_AND_DROP
3649 bool simulateClick
= false;
3651 if (event
.ButtonDClick())
3653 m_renameTimer
->Stop();
3654 m_lastOnSame
= false;
3657 wxDataViewItem item
= GetItemByRow(current
);
3658 bool ignore_other_columns
=
3659 ((GetOwner()->GetExpanderColumn() != col
) &&
3660 (model
->IsContainer(item
)) &&
3661 (!model
->HasContainerColumns(item
)));
3663 if (event
.LeftDClick())
3665 if(hoverOverExpander
)
3667 // a double click on the expander will be converted into a "simulated" normal click
3668 simulateClick
= true;
3670 else if ( current
== m_lineLastClicked
)
3672 bool activated
= false;
3674 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3676 const unsigned colIdx
= col
->GetModelColumn();
3678 cell
->PrepareForItem(model
, item
, colIdx
);
3680 wxRect
cell_rect( xpos
, GetLineStart( current
),
3681 col
->GetWidth(), GetLineHeight( current
) );
3682 activated
= cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3687 wxWindow
*parent
= GetParent();
3688 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3690 le
.SetColumn( col
->GetModelColumn() );
3691 le
.SetDataViewColumn( col
);
3692 le
.SetEventObject(parent
);
3693 le
.SetModel(GetModel());
3695 parent
->ProcessWindowEvent(le
);
3701 // The first click was on another item, so don't interpret this as
3702 // a double click, but as a simple click instead
3703 simulateClick
= true;
3707 if (event
.LeftUp() && !hoverOverExpander
)
3709 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3711 // select single line
3712 SelectAllRows( false );
3713 SelectRow( m_lineSelectSingleOnUp
, true );
3714 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3717 // If the user click the expander, we do not do editing even if the column
3718 // with expander are editable
3719 if (m_lastOnSame
&& !ignore_other_columns
)
3721 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3722 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3724 m_renameTimer
->Start( 100, true );
3728 m_lastOnSame
= false;
3729 m_lineSelectSingleOnUp
= (unsigned int)-1;
3731 else if(!event
.LeftUp())
3733 // This is necessary, because after a DnD operation in
3734 // from and to ourself, the up event is swallowed by the
3735 // DnD code. So on next non-up event (which means here and
3736 // now) m_lineSelectSingleOnUp should be reset.
3737 m_lineSelectSingleOnUp
= (unsigned int)-1;
3740 if (event
.RightDown())
3742 m_lineBeforeLastClicked
= m_lineLastClicked
;
3743 m_lineLastClicked
= current
;
3745 // If the item is already selected, do not update the selection.
3746 // Multi-selections should not be cleared if a selected item is clicked.
3747 if (!IsRowSelected(current
))
3749 SelectAllRows(false);
3750 const unsigned oldCurrent
= m_currentRow
;
3751 ChangeCurrentRow(current
);
3752 SelectRow(m_currentRow
,true);
3753 RefreshRow(oldCurrent
);
3754 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3757 else if (event
.RightUp())
3760 model
->GetValue( value
, item
, col
->GetModelColumn() );
3761 wxWindow
*parent
= GetParent();
3762 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3764 le
.SetColumn( col
->GetModelColumn() );
3765 le
.SetDataViewColumn( col
);
3766 le
.SetEventObject(parent
);
3767 le
.SetModel(GetModel());
3769 parent
->ProcessWindowEvent(le
);
3771 else if (event
.MiddleDown())
3775 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3777 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3779 // hoverOverExpander being true tells us that our node must be
3780 // valid and have children.
3781 // So we don't need any extra checks.
3782 if( node
->IsOpen() )
3787 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3789 m_lineBeforeLastClicked
= m_lineLastClicked
;
3790 m_lineLastClicked
= current
;
3792 unsigned int oldCurrentRow
= m_currentRow
;
3793 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3795 bool cmdModifierDown
= event
.CmdDown();
3796 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3798 if ( IsSingleSel() || !IsRowSelected(current
) )
3800 SelectAllRows( false );
3801 ChangeCurrentRow(current
);
3802 SelectRow(m_currentRow
,true);
3803 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3805 else // multi sel & current is highlighted & no mod keys
3807 m_lineSelectSingleOnUp
= current
;
3808 ChangeCurrentRow(current
); // change focus
3811 else // multi sel & either ctrl or shift is down
3813 if (cmdModifierDown
)
3815 ChangeCurrentRow(current
);
3816 ReverseRowSelection(m_currentRow
);
3817 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3819 else if (event
.ShiftDown())
3821 ChangeCurrentRow(current
);
3823 unsigned int lineFrom
= oldCurrentRow
,
3826 if ( lineTo
< lineFrom
)
3829 lineFrom
= m_currentRow
;
3832 SelectRows(lineFrom
, lineTo
, true);
3833 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3835 else // !ctrl, !shift
3837 // test in the enclosing if should make it impossible
3838 wxFAIL_MSG( wxT("how did we get here?") );
3842 if (m_currentRow
!= oldCurrentRow
)
3843 RefreshRow( oldCurrentRow
);
3845 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3847 // Update selection here...
3850 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3851 (current
== oldCurrentRow
)) && oldWasSelected
;
3853 // Call LeftClick after everything else as under GTK+
3854 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3856 // notify cell about click
3857 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3859 wxRect
cell_rect( xpos
+ itemOffset
,
3860 GetLineStart( current
),
3861 col
->GetWidth() - itemOffset
,
3862 GetLineHeight( current
) );
3864 // Report position relative to the cell's custom area, i.e.
3865 // no the entire space as given by the control but the one
3866 // used by the renderer after calculation of alignment etc.
3868 // adjust the rectangle ourselves to account for the alignment
3869 wxRect rectItem
= cell_rect
;
3870 const int align
= cell
->GetAlignment();
3871 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3873 const wxSize size
= cell
->GetSize();
3875 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3877 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3878 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3879 else if ( align
& wxALIGN_RIGHT
)
3880 rectItem
.x
+= cell_rect
.width
- size
.x
;
3881 // else: wxALIGN_LEFT is the default
3884 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3886 if ( align
& wxALIGN_CENTER_VERTICAL
)
3887 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3888 else if ( align
& wxALIGN_BOTTOM
)
3889 rectItem
.y
+= cell_rect
.height
- size
.y
;
3890 // else: wxALIGN_TOP is the default
3894 wxPoint
pos( event
.GetPosition() );
3895 pos
.x
-= rectItem
.x
;
3896 pos
.y
-= rectItem
.y
;
3898 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3900 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3901 model
, item
, col
->GetModelColumn());
3906 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3910 if (HasCurrentRow())
3916 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3920 if (HasCurrentRow())
3926 //-----------------------------------------------------------------------------
3928 //-----------------------------------------------------------------------------
3930 WX_DEFINE_LIST(wxDataViewColumnList
)
3932 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3933 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3934 EVT_SIZE(wxDataViewCtrl::OnSize
)
3937 wxDataViewCtrl::~wxDataViewCtrl()
3940 GetModel()->RemoveNotifier( m_notifier
);
3943 m_colsBestWidths
.clear();
3946 void wxDataViewCtrl::Init()
3948 m_cols
.DeleteContents(true);
3951 // No sorting column at start
3952 m_sortingColumnIdx
= wxNOT_FOUND
;
3954 m_headerArea
= NULL
;
3956 m_colsDirty
= false;
3959 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3964 const wxValidator
& validator
,
3965 const wxString
& name
)
3967 // if ( (style & wxBORDER_MASK) == 0)
3968 // style |= wxBORDER_SUNKEN;
3972 if (!wxControl::Create( parent
, id
, pos
, size
,
3973 style
| wxScrolledWindowStyle
, validator
, name
))
3976 SetInitialSize(size
);
3979 MacSetClipChildren( true );
3982 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3984 // We use the cursor keys for moving the selection, not scrolling, so call
3985 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3986 // keyboard events forwarded to us from wxListMainWindow.
3987 DisableKeyboardScrolling();
3989 if (HasFlag(wxDV_NO_HEADER
))
3990 m_headerArea
= NULL
;
3992 m_headerArea
= new wxDataViewHeaderWindow(this);
3994 SetTargetWindow( m_clientArea
);
3996 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3998 sizer
->Add( m_headerArea
, 0, wxGROW
);
3999 sizer
->Add( m_clientArea
, 1, wxGROW
);
4005 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4007 return wxBORDER_THEME
;
4011 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4015 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4018 // we need to process arrows ourselves for scrolling
4019 if ( nMsg
== WM_GETDLGCODE
)
4021 rc
|= DLGC_WANTARROWS
;
4029 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4031 wxSize newsize
= size
;
4032 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4033 newsize
.y
-= m_headerArea
->GetSize().y
;
4038 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4040 // We need to override OnSize so that our scrolled
4041 // window a) does call Layout() to use sizers for
4042 // positioning the controls but b) does not query
4043 // the sizer for their size and use that for setting
4044 // the scrollable area as set that ourselves by
4045 // calling SetScrollbar() further down.
4051 // We must redraw the headers if their height changed. Normally this
4052 // shouldn't happen as the control shouldn't let itself be resized beneath
4053 // its minimal height but avoid the display artefacts that appear if it
4054 // does happen, e.g. because there is really not enough vertical space.
4055 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4056 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4058 m_headerArea
->Refresh();
4062 void wxDataViewCtrl::SetFocus()
4065 m_clientArea
->SetFocus();
4068 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4070 if (!wxDataViewCtrlBase::AssociateModel( model
))
4073 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4075 model
->AddNotifier( m_notifier
);
4077 m_clientArea
->DestroyTree();
4079 m_clientArea
->BuildTree(model
);
4081 m_clientArea
->UpdateDisplay();
4086 #if wxUSE_DRAG_AND_DROP
4088 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4090 return m_clientArea
->EnableDragSource( format
);
4093 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4095 return m_clientArea
->EnableDropTarget( format
);
4098 #endif // wxUSE_DRAG_AND_DROP
4100 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4102 if (!wxDataViewCtrlBase::AppendColumn(col
))
4105 m_cols
.Append( col
);
4106 m_colsBestWidths
.push_back(0);
4107 OnColumnsCountChanged();
4111 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4113 if (!wxDataViewCtrlBase::PrependColumn(col
))
4116 m_cols
.Insert( col
);
4117 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4118 OnColumnsCountChanged();
4122 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4124 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4127 m_cols
.Insert( pos
, col
);
4128 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4129 OnColumnsCountChanged();
4133 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4136 m_headerArea
->UpdateColumn(idx
);
4138 m_clientArea
->UpdateDisplay();
4141 void wxDataViewCtrl::OnColumnsCountChanged()
4144 m_headerArea
->SetColumnCount(GetColumnCount());
4146 m_clientArea
->UpdateDisplay();
4149 void wxDataViewCtrl::DoSetExpanderColumn()
4151 m_clientArea
->UpdateDisplay();
4154 void wxDataViewCtrl::DoSetIndent()
4156 m_clientArea
->UpdateDisplay();
4159 unsigned int wxDataViewCtrl::GetColumnCount() const
4161 return m_cols
.GetCount();
4164 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4166 if ( !m_clientArea
)
4169 m_clientArea
->SetRowHeight(lineHeight
);
4174 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4179 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4181 // columns can't be reordered if there is no header window which allows
4183 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4186 return GetColumn(idx
);
4189 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4191 const unsigned count
= m_cols
.size();
4192 for ( unsigned n
= 0; n
< count
; n
++ )
4194 if ( m_cols
[n
] == column
)
4201 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4203 if ( m_colsBestWidths
[idx
] != 0 )
4204 return m_colsBestWidths
[idx
];
4206 const int count
= m_clientArea
->GetRowCount();
4207 wxDataViewColumn
*column
= GetColumn(idx
);
4208 wxDataViewRenderer
*renderer
=
4209 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4211 class MaxWidthCalculator
4214 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4215 wxDataViewRenderer
*renderer
,
4216 const wxDataViewModel
*model
,
4219 m_clientArea(clientArea
),
4220 m_renderer(renderer
),
4226 void UpdateWithWidth(int width
)
4228 m_width
= wxMax(m_width
, width
);
4231 void UpdateWithRow(int row
)
4233 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4234 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4235 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4238 int GetMaxWidth() const { return m_width
; }
4242 wxDataViewMainWindow
*m_clientArea
;
4243 wxDataViewRenderer
*m_renderer
;
4244 const wxDataViewModel
*m_model
;
4248 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4249 GetModel(), column
->GetModelColumn());
4253 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4254 // Labels on native MSW header are indented on both sides
4256 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4257 calculator
.UpdateWithWidth(header_width
);
4260 // The code below deserves some explanation. For very large controls, we
4261 // simply can't afford to calculate sizes for all items, it takes too
4262 // long. So the best we can do is to check the first and the last N/2
4263 // items in the control for some sufficiently large N and calculate best
4264 // sizes from that. That can result in the calculated best width being too
4265 // small for some outliers, but it's better to get slightly imperfect
4266 // result than to wait several seconds after every update. To avoid highly
4267 // visible miscalculations, we also include all currently visible items
4268 // no matter what. Finally, the value of N is determined dynamically by
4269 // measuring how much time we spent on the determining item widths so far.
4272 int top_part_end
= count
;
4273 static const long CALC_TIMEOUT
= 20/*ms*/;
4274 // don't call wxStopWatch::Time() too often
4275 static const unsigned CALC_CHECK_FREQ
= 100;
4278 // use some hard-coded limit, that's the best we can do without timer
4279 int top_part_end
= wxMin(500, count
);
4280 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4284 for ( row
= 0; row
< top_part_end
; row
++ )
4287 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4288 timer
.Time() > CALC_TIMEOUT
)
4290 #endif // wxUSE_STOPWATCH
4291 calculator
.UpdateWithRow(row
);
4294 // row is the first unmeasured item now; that's our value of N/2
4300 // add bottom N/2 items now:
4301 const int bottom_part_start
= wxMax(row
, count
- row
);
4302 for ( row
= bottom_part_start
; row
< count
; row
++ )
4304 calculator
.UpdateWithRow(row
);
4307 // finally, include currently visible items in the calculation:
4308 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4309 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4310 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4312 first_visible
= wxMax(first_visible
, top_part_end
);
4313 last_visible
= wxMin(bottom_part_start
, last_visible
);
4315 for ( row
= first_visible
; row
< last_visible
; row
++ )
4317 calculator
.UpdateWithRow(row
);
4320 wxLogTrace("dataview",
4321 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4323 count
- bottom_part_start
,
4324 wxMax(0, last_visible
- first_visible
),
4328 int max_width
= calculator
.GetMaxWidth();
4329 if ( max_width
> 0 )
4330 max_width
+= 2 * PADDING_RIGHTLEFT
;
4332 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4336 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4337 unsigned int WXUNUSED(new_pos
))
4339 // do _not_ reorder m_cols elements here, they should always be in the
4340 // order in which columns were added, we only display the columns in
4342 m_clientArea
->UpdateDisplay();
4345 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4347 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4351 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4353 OnColumnsCountChanged();
4358 bool wxDataViewCtrl::ClearColumns()
4361 m_colsBestWidths
.clear();
4362 OnColumnsCountChanged();
4366 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4368 m_colsBestWidths
[idx
] = 0;
4372 void wxDataViewCtrl::InvalidateColBestWidths()
4374 m_colsBestWidths
.clear();
4375 m_colsBestWidths
.resize(m_cols
.size());
4379 void wxDataViewCtrl::UpdateColWidths()
4381 if ( !m_headerArea
)
4384 for ( wxVector
<int>::const_iterator i
= m_colsBestWidths
.begin();
4385 i
!= m_colsBestWidths
.end();
4388 if ( m_colsBestWidths
[*i
] == 0 )
4389 m_headerArea
->UpdateColumn(*i
);
4393 void wxDataViewCtrl::OnInternalIdle()
4395 wxDataViewCtrlBase::OnInternalIdle();
4399 m_colsDirty
= false;
4404 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4407 unsigned int len
= GetColumnCount();
4408 for ( unsigned int i
= 0; i
< len
; i
++ )
4410 wxDataViewColumn
* col
= GetColumnAt(i
);
4417 // This returns the position in pixels which is not what we want.
4420 unsigned int len
= GetColumnCount();
4421 for ( unsigned int i
= 0; i
< len
; i
++ )
4423 wxDataViewColumn
* col
= GetColumnAt(i
);
4424 if (col
->IsHidden())
4426 ret
+= col
->GetWidth();
4429 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4437 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4439 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4440 : GetColumn(m_sortingColumnIdx
);
4443 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4445 return GetItemByRow(m_clientArea
->GetCurrentRow());
4448 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4450 const int row
= m_clientArea
->GetRowByItem(item
);
4452 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4453 if ( static_cast<unsigned>(row
) != oldCurrent
)
4455 m_clientArea
->ChangeCurrentRow(row
);
4456 m_clientArea
->RefreshRow(oldCurrent
);
4457 m_clientArea
->RefreshRow(row
);
4461 int wxDataViewCtrl::GetSelectedItemsCount() const
4463 return m_clientArea
->GetSelections().size();
4466 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4469 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4471 const size_t len
= selections
.size();
4472 for ( size_t i
= 0; i
< len
; i
++ )
4474 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4481 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4488 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4490 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4492 wxDataViewItem last_parent
;
4494 int len
= sel
.GetCount();
4495 for( int i
= 0; i
< len
; i
++ )
4497 wxDataViewItem item
= sel
[i
];
4498 wxDataViewItem parent
= GetModel()->GetParent( item
);
4501 if (parent
!= last_parent
)
4502 ExpandAncestors(item
);
4505 last_parent
= parent
;
4506 int row
= m_clientArea
->GetRowByItem( item
);
4508 selection
.Add( static_cast<unsigned int>(row
) );
4511 m_clientArea
->SetSelections( selection
);
4514 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4516 ExpandAncestors( item
);
4518 int row
= m_clientArea
->GetRowByItem( item
);
4521 // Unselect all rows before select another in the single select mode
4522 if (m_clientArea
->IsSingleSel())
4523 m_clientArea
->SelectAllRows(false);
4525 m_clientArea
->SelectRow(row
, true);
4527 // Also set focus to the selected item
4528 m_clientArea
->ChangeCurrentRow( row
);
4532 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4534 int row
= m_clientArea
->GetRowByItem( item
);
4536 m_clientArea
->SelectRow(row
, false);
4539 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4541 int row
= m_clientArea
->GetRowByItem( item
);
4544 return m_clientArea
->IsRowSelected(row
);
4549 void wxDataViewCtrl::SelectAll()
4551 m_clientArea
->SelectAllRows(true);
4554 void wxDataViewCtrl::UnselectAll()
4556 m_clientArea
->SelectAllRows(false);
4559 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4563 if( row
> (int) m_clientArea
->GetRowCount() )
4564 row
= m_clientArea
->GetRowCount();
4566 int first
= m_clientArea
->GetFirstVisibleRow();
4567 int last
= m_clientArea
->GetLastVisibleRow();
4569 m_clientArea
->ScrollTo( row
, column
);
4570 else if( row
> last
)
4571 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4573 m_clientArea
->ScrollTo( first
, column
);
4576 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4578 ExpandAncestors( item
);
4580 m_clientArea
->RecalculateDisplay();
4582 int row
= m_clientArea
->GetRowByItem(item
);
4585 if( column
== NULL
)
4586 EnsureVisible(row
, -1);
4588 EnsureVisible( row
, GetColumnIndex(column
) );
4593 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4594 wxDataViewColumn
* &column
) const
4596 m_clientArea
->HitTest(point
, item
, column
);
4599 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4600 const wxDataViewColumn
* column
) const
4602 return m_clientArea
->GetItemRect(item
, column
);
4605 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4607 return m_clientArea
->GetItemByRow( row
);
4610 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4612 return m_clientArea
->GetRowByItem( item
);
4615 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4617 ExpandAncestors( item
);
4619 int row
= m_clientArea
->GetRowByItem( item
);
4621 m_clientArea
->Expand(row
);
4624 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4626 int row
= m_clientArea
->GetRowByItem( item
);
4628 m_clientArea
->Collapse(row
);
4631 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4633 int row
= m_clientArea
->GetRowByItem( item
);
4635 return m_clientArea
->IsExpanded(row
);
4639 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4641 wxDataViewColumn
* col
= GetColumn( column
);
4645 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4646 if (renderer
->GetMode() != wxDATAVIEW_CELL_EDITABLE
)
4649 const wxRect itemRect
= GetItemRect(item
, col
);
4650 renderer
->StartEditing(item
, itemRect
);
4653 #endif // !wxUSE_GENERICDATAVIEWCTRL
4655 #endif // wxUSE_DATAVIEWCTRL