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 // FIXME: currently unused
149 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
151 wxDataViewCtrl
* const owner
= GetOwner();
153 int widthContents
= owner
->GetBestColumnWidth(idx
);
154 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
155 owner
->OnColumnChange(idx
);
161 bool SendEvent(wxEventType type
, unsigned int n
)
163 wxDataViewCtrl
* const owner
= GetOwner();
164 wxDataViewEvent
event(type
, owner
->GetId());
166 event
.SetEventObject(owner
);
168 event
.SetDataViewColumn(owner
->GetColumn(n
));
169 event
.SetModel(owner
->GetModel());
171 // for events created by wxDataViewHeaderWindow the
172 // row / value fields are not valid
173 return owner
->GetEventHandler()->ProcessEvent(event
);
176 void OnClick(wxHeaderCtrlEvent
& event
)
178 const unsigned idx
= event
.GetColumn();
180 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
183 // default handling for the column click is to sort by this column or
184 // toggle its sort order
185 wxDataViewCtrl
* const owner
= GetOwner();
186 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
187 if ( !col
->IsSortable() )
189 // no default handling for non-sortable columns
194 if ( col
->IsSortKey() )
196 // already using this column for sorting, just change the order
197 col
->ToggleSortOrder();
199 else // not using this column for sorting yet
201 // first unset the old sort column if any
202 int oldSortKey
= owner
->GetSortingColumnIndex();
203 if ( oldSortKey
!= wxNOT_FOUND
)
205 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
206 owner
->OnColumnChange(oldSortKey
);
209 owner
->SetSortingColumnIndex(idx
);
213 wxDataViewModel
* const model
= owner
->GetModel();
217 owner
->OnColumnChange(idx
);
220 void OnRClick(wxHeaderCtrlEvent
& event
)
222 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
227 void OnResize(wxHeaderCtrlEvent
& event
)
229 wxDataViewCtrl
* const owner
= GetOwner();
231 const unsigned col
= event
.GetColumn();
232 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
233 GetOwner()->OnColumnChange(col
);
236 void OnEndReorder(wxHeaderCtrlEvent
& event
)
238 wxDataViewCtrl
* const owner
= GetOwner();
239 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
240 event
.GetNewOrder());
243 DECLARE_EVENT_TABLE()
244 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
247 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
248 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
249 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
251 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
252 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
254 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
257 //-----------------------------------------------------------------------------
258 // wxDataViewRenameTimer
259 //-----------------------------------------------------------------------------
261 class wxDataViewRenameTimer
: public wxTimer
264 wxDataViewMainWindow
*m_owner
;
267 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
271 //-----------------------------------------------------------------------------
272 // wxDataViewTreeNode
273 //-----------------------------------------------------------------------------
275 class wxDataViewTreeNode
;
276 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
277 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
279 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
280 wxDataViewTreeNode
** node2
);
281 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
283 class wxDataViewTreeNode
286 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
293 m_hasChildren
= false;
297 ~wxDataViewTreeNode()
301 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
302 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
303 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
304 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
306 void AddNode( wxDataViewTreeNode
* node
)
308 m_leaves
.Add( node
->GetItem().GetID() );
310 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
313 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
315 void AddLeaf( void * leaf
)
317 m_leaves
.Add( leaf
);
319 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
322 wxDataViewItem
& GetItem() { return m_item
; }
323 const wxDataViewItem
& GetItem() const { return m_item
; }
324 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
326 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
327 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
328 int GetIndentLevel() const
331 const wxDataViewTreeNode
* node
= this;
332 while( node
->GetParent()->GetParent() != NULL
)
334 node
= node
->GetParent();
347 int len
= m_nodes
.GetCount();
349 for ( int i
= 0;i
< len
; i
++)
350 sum
+= m_nodes
[i
]->GetSubTreeCount();
352 sum
+= m_leaves
.GetCount();
355 ChangeSubTreeCount(-sum
);
361 ChangeSubTreeCount(sum
);
364 bool HasChildren() const { return m_hasChildren
; }
365 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
367 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
368 int GetSubTreeCount() const { return m_subTreeCount
; }
369 void ChangeSubTreeCount( int num
)
373 m_subTreeCount
+= num
;
375 m_parent
->ChangeSubTreeCount(num
);
382 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
383 int len
= m_nodes
.GetCount();
384 for (int i
= 0; i
< len
; i
++)
385 m_nodes
[i
]->Resort();
386 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
391 wxDataViewTreeNode
*m_parent
;
392 wxDataViewTreeNodes m_nodes
;
393 wxDataViewTreeLeaves m_leaves
;
394 wxDataViewItem m_item
;
400 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
401 wxDataViewTreeNode
** node2
)
403 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
406 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
408 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
412 //-----------------------------------------------------------------------------
413 // wxDataViewMainWindow
414 //-----------------------------------------------------------------------------
416 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
418 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
419 WX_DEFINE_LIST(ItemList
)
421 class wxDataViewMainWindow
: public wxWindow
424 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
426 const wxPoint
&pos
= wxDefaultPosition
,
427 const wxSize
&size
= wxDefaultSize
,
428 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
429 virtual ~wxDataViewMainWindow();
431 bool IsList() const { return GetOwner()->GetModel()->IsListModel(); }
432 bool IsVirtualList() const { return m_root
== NULL
; }
434 // notifications from wxDataViewModel
435 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
436 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
437 bool ItemChanged( const wxDataViewItem
&item
);
438 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
442 if (!IsVirtualList())
452 g_model
= GetOwner()->GetModel();
453 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
456 if (g_model
->HasDefaultCompare())
464 g_column
= col
->GetModelColumn();
465 g_asending
= col
->IsSortOrderAscending();
468 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
469 wxDataViewCtrl
*GetOwner() { return m_owner
; }
470 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
472 #if wxUSE_DRAG_AND_DROP
473 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
474 #endif // wxUSE_DRAG_AND_DROP
475 void OnPaint( wxPaintEvent
&event
);
476 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
477 void OnChar( wxKeyEvent
&event
);
478 void OnMouse( wxMouseEvent
&event
);
479 void OnSetFocus( wxFocusEvent
&event
);
480 void OnKillFocus( wxFocusEvent
&event
);
482 void UpdateDisplay();
483 void RecalculateDisplay();
484 void OnInternalIdle();
486 void OnRenameTimer();
488 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
489 void ScrollTo( int rows
, int column
);
491 unsigned GetCurrentRow() const { return m_currentRow
; }
492 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
493 void ChangeCurrentRow( unsigned int row
);
495 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
496 bool IsEmpty() { return GetRowCount() == 0; }
498 int GetCountPerPage() const;
499 int GetEndOfLastCol() const;
500 unsigned int GetFirstVisibleRow() const;
502 // I change this method to un const because in the tree view,
503 // the displaying number of the tree are changing along with the
504 // expanding/collapsing of the tree nodes
505 unsigned int GetLastVisibleRow();
506 unsigned int GetRowCount();
508 wxDataViewItem
GetSelection() const;
509 wxDataViewSelection
GetSelections(){ return m_selection
; }
510 void SetSelections( const wxDataViewSelection
& sel
)
511 { m_selection
= sel
; UpdateDisplay(); }
512 void Select( const wxArrayInt
& aSelections
);
513 void SelectAllRows( bool on
);
514 void SelectRow( unsigned int row
, bool on
);
515 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
516 void ReverseRowSelection( unsigned int row
);
517 bool IsRowSelected( unsigned int row
);
518 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
520 void RefreshRow( unsigned int row
);
521 void RefreshRows( unsigned int from
, unsigned int to
);
522 void RefreshRowsAfter( unsigned int firstRow
);
524 // returns the colour to be used for drawing the rules
525 wxColour
GetRuleColour() const
527 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
530 wxRect
GetLineRect( unsigned int row
) const;
532 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
533 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
534 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
536 // Some useful functions for row and item mapping
537 wxDataViewItem
GetItemByRow( unsigned int row
) const;
538 int GetRowByItem( const wxDataViewItem
& item
) const;
540 // Methods for building the mapping tree
541 void BuildTree( wxDataViewModel
* model
);
543 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
544 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
546 void Expand( unsigned int row
);
547 void Collapse( unsigned int row
);
548 bool IsExpanded( unsigned int row
) const;
549 bool HasChildren( unsigned int row
) const;
551 #if wxUSE_DRAG_AND_DROP
552 bool EnableDragSource( const wxDataFormat
&format
);
553 bool EnableDropTarget( const wxDataFormat
&format
);
555 void RemoveDropHint();
556 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
557 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
558 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
560 #endif // wxUSE_DRAG_AND_DROP
563 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
564 // We did not need this temporarily
565 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
567 int RecalculateCount();
569 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
571 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
574 wxDataViewCtrl
*m_owner
;
578 wxDataViewColumn
*m_currentCol
;
579 unsigned int m_currentRow
;
580 wxDataViewSelection m_selection
;
582 wxDataViewRenameTimer
*m_renameTimer
;
587 #if wxUSE_DRAG_AND_DROP
592 wxDataFormat m_dragFormat
;
595 wxDataFormat m_dropFormat
;
597 unsigned int m_dropHintLine
;
598 #endif // wxUSE_DRAG_AND_DROP
600 // for double click logic
601 unsigned int m_lineLastClicked
,
602 m_lineBeforeLastClicked
,
603 m_lineSelectSingleOnUp
;
605 // the pen used to draw horiz/vertical rules
608 // the pen used to draw the expander and the lines
611 // This is the tree structure of the model
612 wxDataViewTreeNode
* m_root
;
615 // This is the tree node under the cursor
616 wxDataViewTreeNode
* m_underMouse
;
619 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
620 DECLARE_EVENT_TABLE()
623 // ---------------------------------------------------------
624 // wxGenericDataViewModelNotifier
625 // ---------------------------------------------------------
627 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
630 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
631 { m_mainWindow
= mainWindow
; }
633 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
634 { return m_mainWindow
->ItemAdded( parent
, item
); }
635 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
636 { return m_mainWindow
->ItemDeleted( parent
, item
); }
637 virtual bool ItemChanged( const wxDataViewItem
& item
)
638 { return m_mainWindow
->ItemChanged(item
); }
639 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
640 { return m_mainWindow
->ValueChanged( item
, col
); }
641 virtual bool Cleared()
642 { return m_mainWindow
->Cleared(); }
643 virtual void Resort()
644 { m_mainWindow
->Resort(); }
646 wxDataViewMainWindow
*m_mainWindow
;
649 // ---------------------------------------------------------
650 // wxDataViewRenderer
651 // ---------------------------------------------------------
653 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
655 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
656 wxDataViewCellMode mode
,
658 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
662 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
666 wxDataViewRenderer::~wxDataViewRenderer()
671 wxDC
*wxDataViewRenderer::GetDC()
675 if (GetOwner() == NULL
)
677 if (GetOwner()->GetOwner() == NULL
)
679 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
685 void wxDataViewRenderer::SetAlignment( int align
)
690 int wxDataViewRenderer::GetAlignment() const
695 // ---------------------------------------------------------
696 // wxDataViewCustomRenderer
697 // ---------------------------------------------------------
699 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
701 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
702 wxDataViewCellMode mode
, int align
) :
703 wxDataViewRenderer( varianttype
, mode
, align
)
707 // ---------------------------------------------------------
708 // wxDataViewTextRenderer
709 // ---------------------------------------------------------
711 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
713 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
714 wxDataViewCellMode mode
, int align
) :
715 wxDataViewRenderer( varianttype
, mode
, align
)
719 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
721 m_text
= value
.GetString();
726 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
731 bool wxDataViewTextRenderer::HasEditorCtrl() const
736 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
737 wxRect labelRect
, const wxVariant
&value
)
739 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
740 wxPoint(labelRect
.x
,labelRect
.y
),
741 wxSize(labelRect
.width
,labelRect
.height
) );
743 // select the text in the control an place the cursor at the end
744 ctrl
->SetInsertionPointEnd();
750 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
752 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
753 value
= text
->GetValue();
757 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
759 RenderText(m_text
, 0, rect
, dc
, state
);
763 wxSize
wxDataViewTextRenderer::GetSize() const
766 return GetTextExtent(m_text
);
768 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
771 // ---------------------------------------------------------
772 // wxDataViewBitmapRenderer
773 // ---------------------------------------------------------
775 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
777 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
778 wxDataViewCellMode mode
, int align
) :
779 wxDataViewRenderer( varianttype
, mode
, align
)
783 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
785 if (value
.GetType() == wxT("wxBitmap"))
787 if (value
.GetType() == wxT("wxIcon"))
793 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
798 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
801 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
802 else if (m_icon
.Ok())
803 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
808 wxSize
wxDataViewBitmapRenderer::GetSize() const
811 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
812 else if (m_icon
.Ok())
813 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
815 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
818 // ---------------------------------------------------------
819 // wxDataViewToggleRenderer
820 // ---------------------------------------------------------
822 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
824 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
825 wxDataViewCellMode mode
, int align
) :
826 wxDataViewRenderer( varianttype
, mode
, align
)
831 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
833 m_toggle
= value
.GetBool();
838 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
843 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
847 flags
|= wxCONTROL_CHECKED
;
848 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
849 GetEnabled() == false)
850 flags
|= wxCONTROL_DISABLED
;
852 // check boxes we draw must always have the same, standard size (if it's
853 // bigger than the cell size the checkbox will be truncated because the
854 // caller had set the clipping rectangle to prevent us from drawing outside
856 cell
.SetSize(GetSize());
858 wxRendererNative::Get().DrawCheckBox(
859 GetOwner()->GetOwner(),
867 void wxDataViewToggleRenderer::WXOnActivate(wxDataViewModel
*model
,
868 const wxVariant
& valueOld
,
869 const wxDataViewItem
& item
,
872 if (model
->IsEnabled(item
, col
))
874 model
->ChangeValue(!valueOld
.GetBool(), item
, col
);
878 wxSize
wxDataViewToggleRenderer::GetSize() const
880 // the window parameter is not used by GetCheckBoxSize() so it's
882 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
885 // ---------------------------------------------------------
886 // wxDataViewProgressRenderer
887 // ---------------------------------------------------------
889 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
891 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
892 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
893 wxDataViewRenderer( varianttype
, mode
, align
)
899 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
901 m_value
= (long) value
;
903 if (m_value
< 0) m_value
= 0;
904 if (m_value
> 100) m_value
= 100;
909 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
911 value
= (long) m_value
;
916 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
918 // deflate the rect to leave a small border between bars in adjacent rows
919 wxRect bar
= rect
.Deflate(0, 1);
921 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
922 dc
->SetPen( *wxBLACK_PEN
);
923 dc
->DrawRectangle( bar
);
925 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
926 dc
->SetPen( *wxTRANSPARENT_PEN
);
928 const wxDataViewItemAttr
& attr
= GetAttr();
929 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
931 dc
->DrawRectangle( bar
);
936 wxSize
wxDataViewProgressRenderer::GetSize() const
938 return wxSize(40,12);
941 // ---------------------------------------------------------
942 // wxDataViewDateRenderer
943 // ---------------------------------------------------------
945 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
947 #if wxUSE_DATE_RENDERER_POPUP
949 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
952 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
953 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
954 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
959 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
960 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
961 sizer
->Add( m_cal
, 1, wxGROW
);
966 void OnCalendar( wxCalendarEvent
&event
);
968 wxCalendarCtrl
*m_cal
;
969 wxDataViewModel
*m_model
;
971 const wxDataViewItem
& m_item
;
974 virtual void OnDismiss()
979 DECLARE_EVENT_TABLE()
982 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
983 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
986 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
988 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
992 #endif // wxUSE_DATE_RENDERER_POPUP
994 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
996 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
997 wxDataViewCellMode mode
, int align
) :
998 wxDataViewRenderer( varianttype
, mode
, align
)
1002 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1004 m_date
= value
.GetDateTime();
1009 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1015 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1017 wxString tmp
= m_date
.FormatDate();
1018 RenderText( tmp
, 0, cell
, dc
, state
);
1022 wxSize
wxDataViewDateRenderer::GetSize() const
1024 return GetTextExtent(m_date
.FormatDate());
1027 void wxDataViewDateRenderer::WXOnActivate(wxDataViewModel
*model
,
1028 const wxVariant
& valueOld
,
1029 const wxDataViewItem
& item
,
1032 wxDateTime dtOld
= valueOld
.GetDateTime();
1034 #if wxUSE_DATE_RENDERER_POPUP
1035 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1036 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1037 wxPoint pos
= wxGetMousePosition();
1040 popup
->Popup( popup
->m_cal
);
1041 #else // !wxUSE_DATE_RENDERER_POPUP
1042 wxMessageBox(dtOld
.Format());
1043 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1046 // ---------------------------------------------------------
1047 // wxDataViewIconTextRenderer
1048 // ---------------------------------------------------------
1050 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1052 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1053 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1054 wxDataViewRenderer( varianttype
, mode
, align
)
1057 SetAlignment(align
);
1060 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1066 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1071 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1075 const wxIcon
& icon
= m_value
.GetIcon();
1078 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1079 xoffset
= icon
.GetWidth()+4;
1082 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1087 wxSize
wxDataViewIconTextRenderer::GetSize() const
1089 if (!m_value
.GetText().empty())
1091 wxSize size
= GetTextExtent(m_value
.GetText());
1093 if (m_value
.GetIcon().IsOk())
1094 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1097 return wxSize(80,20);
1100 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1102 wxDataViewIconText iconText
;
1105 wxString text
= iconText
.GetText();
1107 // adjust the label rect to take the width of the icon into account
1108 if (iconText
.GetIcon().IsOk())
1110 int w
= iconText
.GetIcon().GetWidth() + 4;
1112 labelRect
.width
-= w
;
1115 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1116 wxPoint(labelRect
.x
,labelRect
.y
),
1117 wxSize(labelRect
.width
,labelRect
.height
) );
1119 // select the text in the control an place the cursor at the end
1120 ctrl
->SetInsertionPointEnd();
1126 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
& value
)
1128 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1130 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1135 //-----------------------------------------------------------------------------
1136 // wxDataViewDropTarget
1137 //-----------------------------------------------------------------------------
1139 #if wxUSE_DRAG_AND_DROP
1141 class wxBitmapCanvas
: public wxWindow
1144 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1145 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1148 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1151 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1154 dc
.DrawBitmap( m_bitmap
, 0, 0);
1160 class wxDataViewDropSource
: public wxDropSource
1163 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1171 ~wxDataViewDropSource()
1176 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1178 wxPoint pos
= wxGetMousePosition();
1182 int liney
= m_win
->GetLineStart( m_row
);
1184 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1185 m_win
->ClientToScreen( &linex
, &liney
);
1186 m_dist_x
= pos
.x
- linex
;
1187 m_dist_y
= pos
.y
- liney
;
1190 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1192 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1193 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1195 wxFRAME_TOOL_WINDOW
|
1196 wxFRAME_FLOAT_ON_PARENT
|
1197 wxFRAME_NO_TASKBAR
|
1199 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1204 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1205 m_hint
->SetTransparent( 128 );
1211 wxDataViewMainWindow
*m_win
;
1214 int m_dist_x
,m_dist_y
;
1218 class wxDataViewDropTarget
: public wxDropTarget
1221 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1227 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1229 wxDataFormat format
= GetMatchingPair();
1230 if (format
== wxDF_INVALID
)
1232 return m_win
->OnDragOver( format
, x
, y
, def
);
1235 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1237 wxDataFormat format
= GetMatchingPair();
1238 if (format
== wxDF_INVALID
)
1240 return m_win
->OnDrop( format
, x
, y
);
1243 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1245 wxDataFormat format
= GetMatchingPair();
1246 if (format
== wxDF_INVALID
)
1250 return m_win
->OnData( format
, x
, y
, def
);
1253 virtual void OnLeave()
1254 { m_win
->OnLeave(); }
1256 wxDataViewMainWindow
*m_win
;
1259 #endif // wxUSE_DRAG_AND_DROP
1261 //-----------------------------------------------------------------------------
1262 // wxDataViewRenameTimer
1263 //-----------------------------------------------------------------------------
1265 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1270 void wxDataViewRenameTimer::Notify()
1272 m_owner
->OnRenameTimer();
1275 //-----------------------------------------------------------------------------
1276 // wxDataViewMainWindow
1277 //-----------------------------------------------------------------------------
1279 // The tree building helper, declared firstly
1280 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1281 wxDataViewTreeNode
* node
);
1283 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1285 if (row1
> row2
) return 1;
1286 if (row1
== row2
) return 0;
1290 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1292 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1293 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1294 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1295 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1296 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1297 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1300 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1301 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1302 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1303 m_selection( wxDataViewSelectionCmp
)
1308 m_lastOnSame
= false;
1309 m_renameTimer
= new wxDataViewRenameTimer( this );
1311 // TODO: user better initial values/nothing selected
1312 m_currentCol
= NULL
;
1315 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1317 #if wxUSE_DRAG_AND_DROP
1319 m_dragStart
= wxPoint(0,0);
1321 m_dragEnabled
= false;
1322 m_dropEnabled
= false;
1324 m_dropHintLine
= (unsigned int) -1;
1325 #endif // wxUSE_DRAG_AND_DROP
1327 m_lineLastClicked
= (unsigned int) -1;
1328 m_lineBeforeLastClicked
= (unsigned int) -1;
1329 m_lineSelectSingleOnUp
= (unsigned int) -1;
1333 SetBackgroundColour( *wxWHITE
);
1335 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1337 m_penRule
= wxPen(GetRuleColour());
1339 // compose a pen whichcan draw black lines
1340 // TODO: maybe there is something system colour to use
1341 m_penExpander
= wxPen(wxColour(0,0,0));
1343 m_root
= new wxDataViewTreeNode( NULL
);
1344 m_root
->SetHasChildren(true);
1346 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1348 m_underMouse
= NULL
;
1352 wxDataViewMainWindow::~wxDataViewMainWindow()
1355 delete m_renameTimer
;
1359 #if wxUSE_DRAG_AND_DROP
1360 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1362 m_dragFormat
= format
;
1363 m_dragEnabled
= format
!= wxDF_INVALID
;
1368 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1370 m_dropFormat
= format
;
1371 m_dropEnabled
= format
!= wxDF_INVALID
;
1374 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1379 void wxDataViewMainWindow::RemoveDropHint()
1384 RefreshRow( m_dropHintLine
);
1385 m_dropHintLine
= (unsigned int) -1;
1389 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1390 wxCoord y
, wxDragResult def
)
1394 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1395 unsigned int row
= GetLineAt( yy
);
1397 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1403 wxDataViewItem item
= GetItemByRow( row
);
1405 wxDataViewModel
*model
= GetOwner()->GetModel();
1407 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1408 event
.SetEventObject( m_owner
);
1409 event
.SetItem( item
);
1410 event
.SetModel( model
);
1411 event
.SetDataFormat( format
);
1412 if (!m_owner
->HandleWindowEvent( event
))
1418 if (!event
.IsAllowed())
1425 if (m_dropHint
&& (row
!= m_dropHintLine
))
1426 RefreshRow( m_dropHintLine
);
1428 m_dropHintLine
= row
;
1434 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1440 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1441 unsigned int row
= GetLineAt( yy
);
1443 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1446 wxDataViewItem item
= GetItemByRow( row
);
1448 wxDataViewModel
*model
= GetOwner()->GetModel();
1450 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1451 event
.SetEventObject( m_owner
);
1452 event
.SetItem( item
);
1453 event
.SetModel( model
);
1454 event
.SetDataFormat( format
);
1455 if (!m_owner
->HandleWindowEvent( event
))
1458 if (!event
.IsAllowed())
1464 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1469 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1470 unsigned int row
= GetLineAt( yy
);
1472 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1475 wxDataViewItem item
= GetItemByRow( row
);
1477 wxDataViewModel
*model
= GetOwner()->GetModel();
1479 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1481 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1482 event
.SetEventObject( m_owner
);
1483 event
.SetItem( item
);
1484 event
.SetModel( model
);
1485 event
.SetDataFormat( format
);
1486 event
.SetDataSize( obj
->GetSize() );
1487 event
.SetDataBuffer( obj
->GetData() );
1488 if (!m_owner
->HandleWindowEvent( event
))
1491 if (!event
.IsAllowed())
1497 void wxDataViewMainWindow::OnLeave()
1502 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1504 int height
= GetLineHeight( row
);
1506 unsigned int cols
= GetOwner()->GetColumnCount();
1508 for (col
= 0; col
< cols
; col
++)
1510 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1511 if (column
->IsHidden())
1512 continue; // skip it!
1513 width
+= column
->GetWidth();
1519 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1520 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1521 indent
= indent
+ m_lineHeight
;
1522 // try to use the m_lineHeight as the expander space
1524 if(!node
->HasChildren())
1529 wxBitmap
bitmap( width
, height
);
1530 wxMemoryDC
dc( bitmap
);
1531 dc
.SetFont( GetFont() );
1532 dc
.SetPen( *wxBLACK_PEN
);
1533 dc
.SetBrush( *wxWHITE_BRUSH
);
1534 dc
.DrawRectangle( 0,0,width
,height
);
1536 wxDataViewModel
*model
= m_owner
->GetModel();
1538 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1541 // TODO-RTL: last column for RTL support
1542 expander
= GetOwner()->GetColumnAt( 0 );
1543 GetOwner()->SetExpanderColumn(expander
);
1548 for (col
= 0; col
< cols
; col
++)
1550 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1551 wxDataViewRenderer
*cell
= column
->GetRenderer();
1553 if (column
->IsHidden())
1554 continue; // skip it!
1556 width
= column
->GetWidth();
1558 if (column
== expander
)
1561 wxDataViewItem item
= GetItemByRow( row
);
1562 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1564 wxRect
item_rect(x
, 0, width
, height
);
1565 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1567 // dc.SetClippingRegion( item_rect );
1568 cell
->WXCallRender(item_rect
, &dc
, 0);
1569 // dc.DestroyClippingRegion();
1577 #endif // wxUSE_DRAG_AND_DROP
1580 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1582 wxDataViewModel
*model
= GetOwner()->GetModel();
1583 wxAutoBufferedPaintDC
dc( this );
1586 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1587 dc
.SetPen( *wxTRANSPARENT_PEN
);
1588 dc
.DrawRectangle(GetClientSize());
1592 GetOwner()->PrepareDC( dc
);
1593 dc
.SetFont( GetFont() );
1595 wxRect update
= GetUpdateRegion().GetBox();
1596 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1598 // compute which items needs to be redrawn
1599 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1600 unsigned int item_count
=
1601 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1602 (int)(GetRowCount( ) - item_start
));
1603 unsigned int item_last
= item_start
+ item_count
;
1604 // Get the parent of DataViewCtrl
1605 wxWindow
*parent
= GetParent()->GetParent();
1606 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1607 cache_event
.SetEventObject(GetParent());
1608 cache_event
.SetCache(item_start
, item_last
- 1);
1609 parent
->ProcessWindowEvent(cache_event
);
1611 // compute which columns needs to be redrawn
1612 unsigned int cols
= GetOwner()->GetColumnCount();
1615 // we assume that we have at least one column below and painting an
1616 // empty control is unnecessary anyhow
1620 unsigned int col_start
= 0;
1621 unsigned int x_start
;
1622 for (x_start
= 0; col_start
< cols
; col_start
++)
1624 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1625 if (col
->IsHidden())
1626 continue; // skip it!
1628 unsigned int w
= col
->GetWidth();
1629 if (x_start
+w
>= (unsigned int)update
.x
)
1635 unsigned int col_last
= col_start
;
1636 unsigned int x_last
= x_start
;
1637 for (; col_last
< cols
; col_last
++)
1639 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1640 if (col
->IsHidden())
1641 continue; // skip it!
1643 if (x_last
> (unsigned int)update
.GetRight())
1646 x_last
+= col
->GetWidth();
1649 // Draw horizontal rules if required
1650 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1652 dc
.SetPen(m_penRule
);
1653 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1655 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1657 int y
= GetLineStart( i
);
1658 dc
.DrawLine(x_start
, y
, x_last
, y
);
1662 // Draw vertical rules if required
1663 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1665 dc
.SetPen(m_penRule
);
1666 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1669 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1671 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1672 if (col
->IsHidden())
1673 continue; // skip it
1675 dc
.DrawLine(x
, GetLineStart( item_start
),
1676 x
, GetLineStart( item_last
) );
1678 x
+= col
->GetWidth();
1681 // Draw last vertical rule
1682 dc
.DrawLine(x
, GetLineStart( item_start
),
1683 x
, GetLineStart( item_last
) );
1686 // redraw the background for the items which are selected/current
1687 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1689 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1690 if (selected
|| item
== m_currentRow
)
1692 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1693 if (item
== m_currentRow
)
1694 flags
|= wxCONTROL_CURRENT
;
1696 flags
|= wxCONTROL_FOCUSED
;
1698 wxRect
rect( x_start
, GetLineStart( item
),
1699 x_last
- x_start
, GetLineHeight( item
) );
1700 wxRendererNative::Get().DrawItemSelectionRect
1710 #if wxUSE_DRAG_AND_DROP
1713 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1714 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1715 dc
.SetPen( *wxBLACK_PEN
);
1716 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1717 dc
.DrawRectangle( rect
);
1719 #endif // wxUSE_DRAG_AND_DROP
1721 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1724 // TODO-RTL: last column for RTL support
1725 expander
= GetOwner()->GetColumnAt( 0 );
1726 GetOwner()->SetExpanderColumn(expander
);
1729 // redraw all cells for all rows which must be repainted and all columns
1731 cell_rect
.x
= x_start
;
1732 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1734 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1735 wxDataViewRenderer
*cell
= col
->GetRenderer();
1736 cell_rect
.width
= col
->GetWidth();
1738 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1739 continue; // skip it!
1741 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1743 // get the cell value and set it into the renderer
1744 wxDataViewTreeNode
*node
= NULL
;
1745 wxDataViewItem dataitem
;
1747 if (!IsVirtualList())
1749 node
= GetTreeNodeByRow(item
);
1753 dataitem
= node
->GetItem();
1755 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1756 !model
->HasContainerColumns(dataitem
))
1761 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1764 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1767 cell_rect
.y
= GetLineStart( item
);
1768 cell_rect
.height
= GetLineHeight( item
);
1770 // deal with the expander
1772 if ((!IsList()) && (col
== expander
))
1774 // Calculate the indent first
1775 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1777 // we reserve m_lineHeight of horizontal space for the expander
1778 // but leave EXPANDER_MARGIN around the expander itself
1779 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1781 indent
+= m_lineHeight
;
1783 // draw expander if needed and visible
1784 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1786 dc
.SetPen( m_penExpander
);
1787 dc
.SetBrush( wxNullBrush
);
1789 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1790 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1791 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1793 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1796 if ( m_underMouse
== node
)
1797 flag
|= wxCONTROL_CURRENT
;
1798 if ( node
->IsOpen() )
1799 flag
|= wxCONTROL_EXPANDED
;
1801 // ensure that we don't overflow the cell (which might
1802 // happen if the column is very narrow)
1803 wxDCClipper
clip(dc
, cell_rect
);
1805 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1808 // force the expander column to left-center align
1809 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1811 if (node
&& !node
->HasChildren())
1813 // Yes, if the node does not have any child, it must be a leaf which
1814 // mean that it is a temporarily created by GetTreeNodeByRow
1818 wxRect item_rect
= cell_rect
;
1819 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1821 // account for the tree indent (harmless if we're not indented)
1822 item_rect
.x
+= indent
;
1823 item_rect
.width
-= indent
;
1825 if ( item_rect
.width
<= 0 )
1829 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1830 state
|= wxDATAVIEW_CELL_SELECTED
;
1832 // TODO: it would be much more efficient to create a clipping
1833 // region for the entire column being rendered (in the OnPaint
1834 // of wxDataViewMainWindow) instead of a single clip region for
1835 // each cell. However it would mean that each renderer should
1836 // respect the given wxRect's top & bottom coords, eventually
1837 // violating only the left & right coords - however the user can
1838 // make its own renderer and thus we cannot be sure of that.
1839 wxDCClipper
clip(dc
, item_rect
);
1841 cell
->WXCallRender(item_rect
, &dc
, state
);
1844 cell_rect
.x
+= cell_rect
.width
;
1848 void wxDataViewMainWindow::OnRenameTimer()
1850 // We have to call this here because changes may just have
1851 // been made and no screen update taken place.
1854 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1855 // (needs to be tested!)
1859 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1861 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1863 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1866 //-----------------------------------------------------------------------------
1867 // Helper class for do operation on the tree node
1868 //-----------------------------------------------------------------------------
1873 virtual ~DoJob() { }
1875 // The return value control how the tree-walker tranverse the tree
1876 // 0: Job done, stop tranverse and return
1877 // 1: Ignore the current node's subtree and continue
1878 // 2: Job not done, continue
1879 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1880 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1881 virtual int operator() ( void * n
) = 0;
1884 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1889 switch( func( node
) )
1900 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
1901 const wxDataViewTreeLeaves
& leaves
= node
->GetChildren();
1903 int len_nodes
= nodes
.GetCount();
1904 int len
= leaves
.GetCount();
1905 int i
= 0, nodes_i
= 0;
1907 for(; i
< len
; i
++ )
1909 void * n
= leaves
[i
];
1910 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1912 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1915 if( Walker( nd
, func
) )
1934 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1936 GetOwner()->InvalidateColBestWidths();
1938 if (IsVirtualList())
1940 wxDataViewVirtualListModel
*list_model
=
1941 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1942 m_count
= list_model
->GetCount();
1949 wxDataViewTreeNode
* node
;
1950 node
= FindNode(parent
);
1955 node
->SetHasChildren( true );
1957 if( g_model
->IsContainer( item
) )
1959 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1960 newnode
->SetItem(item
);
1961 newnode
->SetHasChildren( true );
1962 node
->AddNode( newnode
);
1965 node
->AddLeaf( item
.GetID() );
1967 node
->ChangeSubTreeCount(1);
1975 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1977 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1978 const wxDataViewItem
& item
)
1980 GetOwner()->InvalidateColBestWidths();
1982 if (IsVirtualList())
1984 wxDataViewVirtualListModel
*list_model
=
1985 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1986 m_count
= list_model
->GetCount();
1988 if( m_currentRow
> GetRowCount() )
1989 m_currentRow
= m_count
- 1;
1991 // TODO: why empty the entire selection?
1992 m_selection
.Empty();
1999 wxDataViewTreeNode
* node
= FindNode(parent
);
2001 // Notice that it is possible that the item being deleted is not in the
2002 // tree at all, for example we could be deleting a never shown (because
2003 // collapsed) item in a tree model. So it's not an error if we don't know
2004 // about this item, just return without doing anything then.
2005 if ( !node
|| node
->GetChildren().Index(item
.GetID()) == wxNOT_FOUND
)
2009 node
->GetChildren().Remove( item
.GetID() );
2010 // Manipolate selection
2011 if( m_selection
.GetCount() > 1 )
2013 m_selection
.Empty();
2015 bool isContainer
= false;
2016 wxDataViewTreeNodes nds
= node
->GetNodes();
2017 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2019 if (nds
[i
]->GetItem() == item
)
2027 wxDataViewTreeNode
* n
= NULL
;
2028 wxDataViewTreeNodes nodes
= node
->GetNodes();
2029 int len
= nodes
.GetCount();
2030 for( int i
= 0; i
< len
; i
++)
2032 if( nodes
[i
]->GetItem() == item
)
2039 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2041 node
->GetNodes().Remove( n
);
2042 sub
-= n
->GetSubTreeCount();
2043 ::DestroyTreeHelper(n
);
2045 // Make the row number invalid and get a new valid one when user call GetRowCount
2047 node
->ChangeSubTreeCount(sub
);
2049 // Change the current row to the last row if the current exceed the max row number
2050 if( m_currentRow
> GetRowCount() )
2051 m_currentRow
= m_count
- 1;
2058 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2060 GetOwner()->InvalidateColBestWidths();
2066 wxWindow
*parent
= GetParent();
2067 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2068 le
.SetEventObject(parent
);
2069 le
.SetModel(GetOwner()->GetModel());
2071 parent
->GetEventHandler()->ProcessEvent(le
);
2076 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2078 int view_column
= -1;
2079 unsigned int n_col
= m_owner
->GetColumnCount();
2080 for (unsigned i
= 0; i
< n_col
; i
++)
2082 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2083 if (column
->GetModelColumn() == model_column
)
2085 view_column
= (int) i
;
2089 if (view_column
== -1)
2092 GetOwner()->InvalidateColBestWidth(view_column
);
2094 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2095 /*#define MAX_VIRTUAL_WIDTH 100000
2097 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2098 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2099 Refresh( true, &rect );
2107 wxWindow
*parent
= GetParent();
2108 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2109 le
.SetEventObject(parent
);
2110 le
.SetModel(GetOwner()->GetModel());
2112 le
.SetColumn(view_column
);
2113 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2114 parent
->GetEventHandler()->ProcessEvent(le
);
2119 bool wxDataViewMainWindow::Cleared()
2121 GetOwner()->InvalidateColBestWidths();
2124 m_selection
.Clear();
2127 BuildTree( GetOwner()->GetModel() );
2134 void wxDataViewMainWindow::UpdateDisplay()
2137 m_underMouse
= NULL
;
2140 void wxDataViewMainWindow::OnInternalIdle()
2142 wxWindow::OnInternalIdle();
2146 RecalculateDisplay();
2151 void wxDataViewMainWindow::RecalculateDisplay()
2153 wxDataViewModel
*model
= GetOwner()->GetModel();
2160 int width
= GetEndOfLastCol();
2161 int height
= GetLineStart( GetRowCount() );
2163 SetVirtualSize( width
, height
);
2164 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2169 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2171 m_underMouse
= NULL
;
2173 wxWindow::ScrollWindow( dx
, dy
, rect
);
2175 if (GetOwner()->m_headerArea
)
2176 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2179 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2181 m_underMouse
= NULL
;
2184 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2185 int sy
= GetLineStart( rows
)/y
;
2189 wxRect rect
= GetClientRect();
2193 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2194 for (x_start
= 0; colnum
< column
; colnum
++)
2196 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2197 if (col
->IsHidden())
2198 continue; // skip it!
2200 w
= col
->GetWidth();
2204 int x_end
= x_start
+ w
;
2205 xe
= xx
+ rect
.width
;
2208 sx
= ( xx
+ x_end
- xe
)/x
;
2215 m_owner
->Scroll( sx
, sy
);
2218 int wxDataViewMainWindow::GetCountPerPage() const
2220 wxSize size
= GetClientSize();
2221 return size
.y
/ m_lineHeight
;
2224 int wxDataViewMainWindow::GetEndOfLastCol() const
2228 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2230 const wxDataViewColumn
*c
=
2231 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2234 width
+= c
->GetWidth();
2239 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2243 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2245 return GetLineAt( y
);
2248 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2250 wxSize client_size
= GetClientSize();
2251 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2252 &client_size
.x
, &client_size
.y
);
2254 // we should deal with the pixel here
2255 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2257 return wxMin( GetRowCount()-1, row
);
2260 unsigned int wxDataViewMainWindow::GetRowCount()
2262 if ( m_count
== -1 )
2264 m_count
= RecalculateCount();
2270 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2277 void wxDataViewMainWindow::SelectAllRows( bool on
)
2284 m_selection
.Clear();
2285 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2286 m_selection
.Add( i
);
2291 unsigned int first_visible
= GetFirstVisibleRow();
2292 unsigned int last_visible
= GetLastVisibleRow();
2294 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2296 unsigned int row
= m_selection
[i
];
2297 if ((row
>= first_visible
) && (row
<= last_visible
))
2300 m_selection
.Clear();
2304 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2306 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2310 m_selection
.Add( row
);
2318 m_selection
.Remove( row
);
2324 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2328 unsigned int tmp
= from
;
2334 for (i
= from
; i
<= to
; i
++)
2336 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2339 m_selection
.Add( i
);
2344 m_selection
.Remove( i
);
2347 RefreshRows( from
, to
);
2350 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2352 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2354 int n
= aSelections
[i
];
2356 m_selection
.Add( n
);
2361 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2363 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2364 m_selection
.Add( row
);
2366 m_selection
.Remove( row
);
2370 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2372 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2375 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2377 wxWindow
*parent
= GetParent();
2378 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2380 le
.SetEventObject(parent
);
2381 le
.SetModel(GetOwner()->GetModel());
2384 parent
->GetEventHandler()->ProcessEvent(le
);
2387 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2389 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2390 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2392 wxSize client_size
= GetClientSize();
2393 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2394 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2395 if (intersect_rect
.width
> 0)
2396 Refresh( true, &intersect_rect
);
2399 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2403 unsigned int tmp
= to
;
2408 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2409 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2411 wxSize client_size
= GetClientSize();
2412 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2413 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2414 if (intersect_rect
.width
> 0)
2415 Refresh( true, &intersect_rect
);
2418 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2420 wxSize client_size
= GetClientSize();
2421 int start
= GetLineStart( firstRow
);
2422 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2423 if (start
> client_size
.y
) return;
2425 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2427 Refresh( true, &rect
);
2430 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2432 wxCHECK_RET( newCurrent
< GetRowCount(),
2433 wxT("invalid item index in OnArrowChar()") );
2435 // if there is no selection, we cannot move it anywhere
2436 if (!HasCurrentRow())
2439 unsigned int oldCurrent
= m_currentRow
;
2441 // in single selection we just ignore Shift as we can't select several
2443 if ( event
.ShiftDown() && !IsSingleSel() )
2445 RefreshRow( oldCurrent
);
2447 ChangeCurrentRow( newCurrent
);
2449 // select all the items between the old and the new one
2450 if ( oldCurrent
> newCurrent
)
2452 newCurrent
= oldCurrent
;
2453 oldCurrent
= m_currentRow
;
2456 SelectRows( oldCurrent
, newCurrent
, true );
2457 if (oldCurrent
!=newCurrent
)
2458 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2462 RefreshRow( oldCurrent
);
2464 // all previously selected items are unselected unless ctrl is held
2465 if ( !event
.ControlDown() )
2466 SelectAllRows(false);
2468 ChangeCurrentRow( newCurrent
);
2470 if ( !event
.ControlDown() )
2472 SelectRow( m_currentRow
, true );
2473 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2476 RefreshRow( m_currentRow
);
2479 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2482 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2486 rect
.y
= GetLineStart( row
);
2487 rect
.width
= GetEndOfLastCol();
2488 rect
.height
= GetLineHeight( row
);
2493 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2495 const wxDataViewModel
*model
= GetOwner()->GetModel();
2497 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2499 // TODO make more efficient
2504 for (r
= 0; r
< row
; r
++)
2506 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2507 if (!node
) return start
;
2509 wxDataViewItem item
= node
->GetItem();
2511 if (node
&& !node
->HasChildren())
2513 // Yes, if the node does not have any child, it must be a leaf which
2514 // mean that it is a temporarily created by GetTreeNodeByRow
2518 unsigned int cols
= GetOwner()->GetColumnCount();
2520 int height
= m_lineHeight
;
2521 for (col
= 0; col
< cols
; col
++)
2523 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2524 if (column
->IsHidden())
2525 continue; // skip it!
2528 model
->IsContainer(item
) &&
2529 !model
->HasContainerColumns(item
))
2530 continue; // skip it!
2532 wxDataViewRenderer
*renderer
=
2533 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2534 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2536 height
= wxMax( height
, renderer
->GetSize().y
);
2546 return row
* m_lineHeight
;
2550 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2552 const wxDataViewModel
*model
= GetOwner()->GetModel();
2554 // check for the easy case first
2555 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2556 return y
/ m_lineHeight
;
2558 // TODO make more efficient
2559 unsigned int row
= 0;
2560 unsigned int yy
= 0;
2563 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2566 // not really correct...
2567 return row
+ ((y
-yy
) / m_lineHeight
);
2570 wxDataViewItem item
= node
->GetItem();
2572 if (node
&& !node
->HasChildren())
2574 // Yes, if the node does not have any child, it must be a leaf which
2575 // mean that it is a temporarily created by GetTreeNodeByRow
2579 unsigned int cols
= GetOwner()->GetColumnCount();
2581 int height
= m_lineHeight
;
2582 for (col
= 0; col
< cols
; col
++)
2584 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2585 if (column
->IsHidden())
2586 continue; // skip it!
2589 model
->IsContainer(item
) &&
2590 !model
->HasContainerColumns(item
))
2591 continue; // skip it!
2593 wxDataViewRenderer
*renderer
=
2594 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2595 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2597 height
= wxMax( height
, renderer
->GetSize().y
);
2608 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2610 const wxDataViewModel
*model
= GetOwner()->GetModel();
2612 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2614 wxASSERT( !IsVirtualList() );
2616 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2617 // wxASSERT( node );
2618 if (!node
) return m_lineHeight
;
2620 wxDataViewItem item
= node
->GetItem();
2622 if (node
&& !node
->HasChildren())
2624 // Yes, if the node does not have any child, it must be a leaf which
2625 // mean that it is a temporarily created by GetTreeNodeByRow
2629 int height
= m_lineHeight
;
2631 unsigned int cols
= GetOwner()->GetColumnCount();
2633 for (col
= 0; col
< cols
; col
++)
2635 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2636 if (column
->IsHidden())
2637 continue; // skip it!
2640 model
->IsContainer(item
) &&
2641 !model
->HasContainerColumns(item
))
2642 continue; // skip it!
2644 wxDataViewRenderer
*renderer
=
2645 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2646 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2648 height
= wxMax( height
, renderer
->GetSize().y
);
2655 return m_lineHeight
;
2659 class RowToItemJob
: public DoJob
2662 RowToItemJob( unsigned int row
, int current
)
2663 { this->row
= row
; this->current
= current
; }
2664 virtual ~RowToItemJob() {}
2666 virtual int operator() ( wxDataViewTreeNode
* node
)
2669 if( current
== static_cast<int>(row
))
2671 ret
= node
->GetItem();
2675 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2677 current
+= node
->GetSubTreeCount();
2682 // If the current has no child node, we can find the desired item of the row
2684 // This if can speed up finding in some case, and will has a very good effect
2685 // when it comes to list view
2686 if( node
->GetNodes().GetCount() == 0)
2688 int index
= static_cast<int>(row
) - current
- 1;
2689 ret
= node
->GetChildren().Item( index
);
2696 virtual int operator() ( void * n
)
2699 if( current
== static_cast<int>(row
))
2701 ret
= wxDataViewItem( n
);
2707 wxDataViewItem
GetResult() const
2716 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2718 if (IsVirtualList())
2720 return wxDataViewItem( wxUIntToPtr(row
+1) );
2724 RowToItemJob
job( row
, -2 );
2725 Walker( m_root
, job
);
2726 return job
.GetResult();
2730 class RowToTreeNodeJob
: public DoJob
2733 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2736 this->current
= current
;
2740 virtual ~RowToTreeNodeJob(){ }
2742 virtual int operator() ( wxDataViewTreeNode
* node
)
2745 if( current
== static_cast<int>(row
))
2751 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2753 current
+= node
->GetSubTreeCount();
2760 // If the current node has no children, we can find the desired item of the
2761 // row number directly.
2762 // This if can speed up finding in some case, and will have a very good
2763 // effect for list views.
2764 if( node
->GetNodes().GetCount() == 0)
2766 int index
= static_cast<int>(row
) - current
- 1;
2767 void * n
= node
->GetChildren().Item( index
);
2768 ret
= new wxDataViewTreeNode( parent
);
2769 ret
->SetItem( wxDataViewItem( n
));
2770 ret
->SetHasChildren(false);
2777 virtual int operator() ( void * n
)
2780 if( current
== static_cast<int>(row
))
2782 ret
= new wxDataViewTreeNode( parent
);
2783 ret
->SetItem( wxDataViewItem( n
));
2784 ret
->SetHasChildren(false);
2791 wxDataViewTreeNode
* GetResult() const
2797 wxDataViewTreeNode
* ret
;
2798 wxDataViewTreeNode
* parent
;
2801 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2803 wxASSERT( !IsVirtualList() );
2805 RowToTreeNodeJob
job( row
, -2, m_root
);
2806 Walker( m_root
, job
);
2807 return job
.GetResult();
2810 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2811 const wxDataViewItem
& item
)
2813 wxWindow
*parent
= GetParent();
2814 wxDataViewEvent
le(type
, parent
->GetId());
2816 le
.SetEventObject(parent
);
2817 le
.SetModel(GetOwner()->GetModel());
2820 parent
->GetEventHandler()->ProcessEvent(le
);
2824 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2829 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2833 if (!node
->HasChildren())
2839 return node
->IsOpen();
2842 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2847 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2851 if (!node
->HasChildren())
2860 void wxDataViewMainWindow::Expand( unsigned int row
)
2865 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2869 if (!node
->HasChildren())
2875 if (!node
->IsOpen())
2878 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2880 // Check if the user prevent expanding
2881 if( e
.GetSkipped() )
2886 // build the children of current node
2887 if( node
->GetChildrenNumber() == 0 )
2890 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2893 // By expanding the node all row indices that are currently in the selection list
2894 // and are greater than our node have become invalid. So we have to correct that now.
2895 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2896 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2898 const unsigned testRow
= m_selection
[i
];
2899 // all rows above us are not affected, so skip them
2903 m_selection
[i
] += rowAdjustment
;
2906 if(m_currentRow
> row
)
2907 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2911 // Send the expanded event
2912 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2916 void wxDataViewMainWindow::Collapse(unsigned int row
)
2921 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2925 if (!node
->HasChildren())
2934 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2935 if( e
.GetSkipped() )
2938 // Find out if there are selected items below the current node.
2939 bool selectCollapsingRow
= false;
2940 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2941 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2942 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2944 const unsigned testRow
= m_selection
[i
];
2945 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2947 selectCollapsingRow
= true;
2948 // get out as soon as we have found a node that is selected
2955 // If the node to be closed has selected items the user won't see those any longer.
2956 // We select the collapsing node in this case.
2957 if(selectCollapsingRow
)
2959 SelectAllRows(false);
2960 ChangeCurrentRow(row
);
2961 SelectRow(row
, true);
2962 SendSelectionChangedEvent(GetItemByRow(row
));
2966 // if there were no selected items below our node we still need to "fix" the
2967 // selection list to adjust for the changing of the row indices.
2968 // We actually do the opposite of what we are doing in Expand().
2969 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2971 const unsigned testRow
= m_selection
[i
];
2972 // all rows above us are not affected, so skip them
2976 m_selection
[i
] -= rowAdjustment
;
2979 // if the "current row" is being collapsed away we change it to the current row ;-)
2980 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2981 ChangeCurrentRow(row
);
2982 else if(m_currentRow
> row
)
2983 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2988 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2992 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2994 const wxDataViewModel
* model
= GetOwner()->GetModel();
3001 // Compose the parent-chain for the item we are looking for
3002 wxVector
<wxDataViewItem
> parentChain
;
3003 wxDataViewItem
it( item
);
3006 parentChain
.push_back(it
);
3007 it
= model
->GetParent(it
);
3010 // Find the item along the parent-chain.
3011 // This algorithm is designed to speed up the node-finding method
3012 wxDataViewTreeNode
* node
= m_root
;
3013 for( unsigned iter
= parentChain
.size()-1; iter
>=0; --iter
)
3015 if( node
->HasChildren() )
3017 if( node
->GetChildrenNumber() == 0 )
3020 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3023 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3026 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3028 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3029 if (currentNode
->GetItem() == parentChain
[iter
])
3031 if (currentNode
->GetItem() == item
)
3048 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3049 wxDataViewColumn
* &column
)
3051 wxDataViewColumn
*col
= NULL
;
3052 unsigned int cols
= GetOwner()->GetColumnCount();
3053 unsigned int colnum
= 0;
3055 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3056 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3058 col
= GetOwner()->GetColumnAt(colnum
);
3059 if (col
->IsHidden())
3060 continue; // skip it!
3062 unsigned int w
= col
->GetWidth();
3063 if (x_start
+w
>= (unsigned int)x
)
3070 item
= GetItemByRow( GetLineAt( y
) );
3073 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3074 const wxDataViewColumn
* column
)
3079 unsigned int cols
= GetOwner()->GetColumnCount();
3080 // If column is null the loop will compute the combined width of all columns.
3081 // Otherwise, it will compute the x position of the column we are looking for.
3082 for (unsigned int i
= 0; i
< cols
; i
++)
3084 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3089 if (col
->IsHidden())
3090 continue; // skip it!
3092 xpos
+= col
->GetWidth();
3093 width
+= col
->GetWidth();
3098 // If we have a column, we need can get its width directly.
3099 if(column
->IsHidden())
3102 width
= column
->GetWidth();
3107 // If we have no column, we reset the x position back to zero.
3111 // we have to take an expander column into account and compute its indentation
3112 // to get the correct x position where the actual text is
3114 int row
= GetRowByItem(item
);
3115 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3117 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3118 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3119 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3121 if(!node
->HasChildren())
3125 wxRect
itemRect( xpos
+ indent
,
3126 GetLineStart( row
),
3128 GetLineHeight( row
) );
3130 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3131 &itemRect
.x
, &itemRect
.y
);
3136 int wxDataViewMainWindow::RecalculateCount()
3138 if (IsVirtualList())
3140 wxDataViewVirtualListModel
*list_model
=
3141 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3143 return list_model
->GetCount();
3147 return m_root
->GetSubTreeCount();
3151 class ItemToRowJob
: public DoJob
3154 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3161 // Maybe binary search will help to speed up this process
3162 virtual int operator() ( wxDataViewTreeNode
* node
)
3165 if( node
->GetItem() == item
)
3170 if( node
->GetItem() == *m_iter
)
3177 ret
+= node
->GetSubTreeCount();
3183 virtual int operator() ( void * n
)
3186 if( n
== item
.GetID() )
3191 // the row number is begin from zero
3192 int GetResult() const
3196 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3197 wxDataViewItem item
;
3202 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3204 const wxDataViewModel
* model
= GetOwner()->GetModel();
3208 if (IsVirtualList())
3210 return wxPtrToUInt( item
.GetID() ) -1;
3217 // Compose the parent-chain of the item we are looking for
3218 wxVector
<wxDataViewItem
> parentChain
;
3219 wxDataViewItem
it( item
);
3222 parentChain
.push_back(it
);
3223 it
= model
->GetParent(it
);
3226 // add an 'invalid' item to represent our 'invisible' root node
3227 parentChain
.push_back(wxDataViewItem());
3229 // the parent chain was created by adding the deepest parent first.
3230 // so if we want to start at the root node, we have to iterate backwards through the vector
3231 ItemToRowJob
job( item
, parentChain
.rbegin() );
3232 Walker( m_root
, job
);
3233 return job
.GetResult();
3237 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3238 wxDataViewTreeNode
* node
)
3240 if( !model
->IsContainer( item
) )
3243 wxDataViewItemArray children
;
3244 unsigned int num
= model
->GetChildren( item
, children
);
3246 unsigned int index
= 0;
3247 while( index
< num
)
3249 if( model
->IsContainer( children
[index
] ) )
3251 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3252 n
->SetItem(children
[index
]);
3253 n
->SetHasChildren( true );
3258 node
->AddLeaf( children
[index
].GetID() );
3262 node
->SetSubTreeCount( num
);
3263 wxDataViewTreeNode
* n
= node
->GetParent();
3265 n
->ChangeSubTreeCount(num
);
3269 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3273 if (GetOwner()->GetModel()->IsVirtualListModel())
3279 m_root
= new wxDataViewTreeNode( NULL
);
3280 m_root
->SetHasChildren(true);
3282 // First we define a invalid item to fetch the top-level elements
3283 wxDataViewItem item
;
3285 BuildTreeHelper( model
, item
, m_root
);
3289 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3291 if( node
->GetNodeNumber() != 0 )
3293 int len
= node
->GetNodeNumber();
3294 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3295 for (int i
= 0; i
< len
; i
++)
3296 DestroyTreeHelper(nodes
[i
]);
3301 void wxDataViewMainWindow::DestroyTree()
3303 if (!IsVirtualList())
3305 ::DestroyTreeHelper(m_root
);
3311 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3313 wxWindow
* const parent
= GetParent();
3315 // propagate the char event upwards
3316 wxKeyEvent
eventForParent(event
);
3317 eventForParent
.SetEventObject(parent
);
3318 if ( parent
->ProcessWindowEvent(eventForParent
) )
3321 if ( parent
->HandleAsNavigationKey(event
) )
3324 // no item -> nothing to do
3325 if (!HasCurrentRow())
3331 // don't use m_linesPerPage directly as it might not be computed yet
3332 const int pageSize
= GetCountPerPage();
3333 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3335 switch ( event
.GetKeyCode() )
3339 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3341 le
.SetItem( GetItemByRow(m_currentRow
) );
3342 le
.SetEventObject(parent
);
3343 le
.SetModel(GetOwner()->GetModel());
3345 parent
->GetEventHandler()->ProcessEvent(le
);
3350 if ( m_currentRow
> 0 )
3351 OnArrowChar( m_currentRow
- 1, event
);
3355 if ( m_currentRow
< GetRowCount() - 1 )
3356 OnArrowChar( m_currentRow
+ 1, event
);
3358 // Add the process for tree expanding/collapsing
3364 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3368 if (node
->HasChildren() && node
->IsOpen())
3370 Collapse(m_currentRow
);
3372 else // if the node is already closed we move the selection to its parent
3374 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3376 if(!node
->HasChildren())
3381 int parent
= GetRowByItem( parent_node
->GetItem() );
3384 unsigned int row
= m_currentRow
;
3385 SelectRow( row
, false);
3386 SelectRow( parent
, true );
3387 ChangeCurrentRow( parent
);
3388 GetOwner()->EnsureVisible( parent
, -1 );
3389 SendSelectionChangedEvent( parent_node
->GetItem() );
3397 if (!IsExpanded( m_currentRow
))
3398 Expand( m_currentRow
);
3401 unsigned int row
= m_currentRow
;
3402 SelectRow( row
, false );
3403 SelectRow( row
+ 1, true );
3404 ChangeCurrentRow( row
+ 1 );
3405 GetOwner()->EnsureVisible( row
+ 1, -1 );
3406 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3413 OnArrowChar( GetRowCount() - 1, event
);
3418 OnArrowChar( 0, event
);
3423 int steps
= pageSize
- 1;
3424 int index
= m_currentRow
- steps
;
3428 OnArrowChar( index
, event
);
3434 int steps
= pageSize
- 1;
3435 unsigned int index
= m_currentRow
+ steps
;
3436 unsigned int count
= GetRowCount();
3437 if ( index
>= count
)
3440 OnArrowChar( index
, event
);
3446 if(m_selection
.size() == 1)
3448 // TODO: we need to revise that when we have a concept for a 'current column'
3449 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3459 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3461 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3463 // let the base handle mouse wheel events.
3468 // set the focus to ourself if any of the mouse buttons are pressed
3469 if(event
.ButtonDown() && !HasFocus())
3472 int x
= event
.GetX();
3473 int y
= event
.GetY();
3474 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3475 wxDataViewColumn
*col
= NULL
;
3478 unsigned int cols
= GetOwner()->GetColumnCount();
3480 for (i
= 0; i
< cols
; i
++)
3482 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3484 continue; // skip it!
3486 if (x
< xpos
+ c
->GetWidth())
3491 xpos
+= c
->GetWidth();
3499 wxDataViewRenderer
*cell
= col
->GetRenderer();
3500 unsigned int current
= GetLineAt( y
);
3501 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3503 // Unselect all if below the last row ?
3508 // Test whether the mouse is hovered on the tree item button
3509 bool hoverOverExpander
= false;
3510 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3512 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3513 if( node
!=NULL
&& node
->HasChildren() )
3515 int indent
= node
->GetIndentLevel();
3516 indent
= GetOwner()->GetIndent()*indent
;
3518 // we make the rectangle we are looking in a bit bigger than the actual
3519 // visual expander so the user can hit that little thing reliably
3520 wxRect
rect( xpos
+ indent
,
3521 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3522 m_lineHeight
, m_lineHeight
);
3523 if( rect
.Contains(x
, y
) )
3525 // So the mouse is over the expander
3526 hoverOverExpander
= true;
3527 if (m_underMouse
&& m_underMouse
!= node
)
3529 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3530 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3532 if (m_underMouse
!= node
)
3534 // wxLogMessage("Do the row: %d", current);
3535 RefreshRow(current
);
3537 m_underMouse
= node
;
3540 if (node
!=NULL
&& !node
->HasChildren())
3543 if (!hoverOverExpander
)
3545 if (m_underMouse
!= NULL
)
3547 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3548 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3549 m_underMouse
= NULL
;
3553 wxDataViewModel
*model
= GetOwner()->GetModel();
3555 #if wxUSE_DRAG_AND_DROP
3556 if (event
.Dragging())
3558 if (m_dragCount
== 0)
3560 // we have to report the raw, physical coords as we want to be
3561 // able to call HitTest(event.m_pointDrag) from the user code to
3562 // get the item being dragged
3563 m_dragStart
= event
.GetPosition();
3568 if (m_dragCount
!= 3)
3571 if (event
.LeftIsDown())
3573 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3574 &m_dragStart
.x
, &m_dragStart
.y
);
3575 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3576 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3578 // Notify cell about drag
3579 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3580 event
.SetEventObject( m_owner
);
3581 event
.SetItem( item
);
3582 event
.SetModel( model
);
3583 if (!m_owner
->HandleWindowEvent( event
))
3586 if (!event
.IsAllowed())
3589 wxDataObject
*obj
= event
.GetDataObject();
3593 wxDataViewDropSource
drag( this, drag_item_row
);
3594 drag
.SetData( *obj
);
3595 /* wxDragResult res = */ drag
.DoDragDrop();
3604 #endif // wxUSE_DRAG_AND_DROP
3606 bool simulateClick
= false;
3608 if (event
.ButtonDClick())
3610 m_renameTimer
->Stop();
3611 m_lastOnSame
= false;
3614 wxDataViewItem item
= GetItemByRow(current
);
3615 bool ignore_other_columns
=
3616 ((GetOwner()->GetExpanderColumn() != col
) &&
3617 (model
->IsContainer(item
)) &&
3618 (!model
->HasContainerColumns(item
)));
3620 if (event
.LeftDClick())
3622 if(hoverOverExpander
)
3624 // a double click on the expander will be converted into a "simulated" normal click
3625 simulateClick
= true;
3627 else if ( current
== m_lineLastClicked
)
3629 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3631 const unsigned colIdx
= col
->GetModelColumn();
3634 model
->GetValue( value
, item
, colIdx
);
3636 cell
->WXOnActivate(model
, value
, item
, colIdx
);
3638 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3640 cell
->SetValue( value
);
3642 wxRect
cell_rect( xpos
, GetLineStart( current
),
3643 col
->GetWidth(), GetLineHeight( current
) );
3644 custom
->Activate( cell_rect
, model
, item
, colIdx
);
3649 wxWindow
*parent
= GetParent();
3650 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3652 le
.SetColumn( col
->GetModelColumn() );
3653 le
.SetDataViewColumn( col
);
3654 le
.SetEventObject(parent
);
3655 le
.SetModel(GetOwner()->GetModel());
3657 parent
->GetEventHandler()->ProcessEvent(le
);
3663 // The first click was on another item, so don't interpret this as
3664 // a double click, but as a simple click instead
3665 simulateClick
= true;
3669 if (event
.LeftUp() && !hoverOverExpander
)
3671 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3673 // select single line
3674 SelectAllRows( false );
3675 SelectRow( m_lineSelectSingleOnUp
, true );
3676 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3679 // If the user click the expander, we do not do editing even if the column
3680 // with expander are editable
3681 if (m_lastOnSame
&& !ignore_other_columns
)
3683 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3684 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3686 m_renameTimer
->Start( 100, true );
3690 m_lastOnSame
= false;
3691 m_lineSelectSingleOnUp
= (unsigned int)-1;
3693 else if(!event
.LeftUp())
3695 // This is necessary, because after a DnD operation in
3696 // from and to ourself, the up event is swallowed by the
3697 // DnD code. So on next non-up event (which means here and
3698 // now) m_lineSelectSingleOnUp should be reset.
3699 m_lineSelectSingleOnUp
= (unsigned int)-1;
3702 if (event
.RightDown())
3704 m_lineBeforeLastClicked
= m_lineLastClicked
;
3705 m_lineLastClicked
= current
;
3707 // If the item is already selected, do not update the selection.
3708 // Multi-selections should not be cleared if a selected item is clicked.
3709 if (!IsRowSelected(current
))
3711 SelectAllRows(false);
3712 ChangeCurrentRow(current
);
3713 SelectRow(m_currentRow
,true);
3714 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3717 else if (event
.RightUp())
3720 model
->GetValue( value
, item
, col
->GetModelColumn() );
3721 wxWindow
*parent
= GetParent();
3722 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3724 le
.SetColumn( col
->GetModelColumn() );
3725 le
.SetDataViewColumn( col
);
3726 le
.SetEventObject(parent
);
3727 le
.SetModel(GetOwner()->GetModel());
3729 parent
->GetEventHandler()->ProcessEvent(le
);
3731 else if (event
.MiddleDown())
3735 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3737 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3739 // hoverOverExpander being true tells us that our node must be
3740 // valid and have children.
3741 // So we don't need any extra checks.
3742 if( node
->IsOpen() )
3747 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3749 m_lineBeforeLastClicked
= m_lineLastClicked
;
3750 m_lineLastClicked
= current
;
3752 unsigned int oldCurrentRow
= m_currentRow
;
3753 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3755 bool cmdModifierDown
= event
.CmdDown();
3756 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3758 if ( IsSingleSel() || !IsRowSelected(current
) )
3760 SelectAllRows( false );
3761 ChangeCurrentRow(current
);
3762 SelectRow(m_currentRow
,true);
3763 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3765 else // multi sel & current is highlighted & no mod keys
3767 m_lineSelectSingleOnUp
= current
;
3768 ChangeCurrentRow(current
); // change focus
3771 else // multi sel & either ctrl or shift is down
3773 if (cmdModifierDown
)
3775 ChangeCurrentRow(current
);
3776 ReverseRowSelection(m_currentRow
);
3777 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3779 else if (event
.ShiftDown())
3781 ChangeCurrentRow(current
);
3783 unsigned int lineFrom
= oldCurrentRow
,
3786 if ( lineTo
< lineFrom
)
3789 lineFrom
= m_currentRow
;
3792 SelectRows(lineFrom
, lineTo
, true);
3793 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3795 else // !ctrl, !shift
3797 // test in the enclosing if should make it impossible
3798 wxFAIL_MSG( wxT("how did we get here?") );
3802 if (m_currentRow
!= oldCurrentRow
)
3803 RefreshRow( oldCurrentRow
);
3805 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3807 // Update selection here...
3810 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3811 (current
== oldCurrentRow
)) && oldWasSelected
;
3813 // Call LeftClick after everything else as under GTK+
3814 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3816 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3818 // notify cell about click
3819 custom
->PrepareForItem(model
, item
, col
->GetModelColumn());
3821 wxRect
cell_rect( xpos
, GetLineStart( current
),
3822 col
->GetWidth(), GetLineHeight( current
) );
3824 // Report position relative to the cell's custom area, i.e.
3825 // no the entire space as given by the control but the one
3826 // used by the renderer after calculation of alignment etc.
3828 // adjust the rectangle ourselves to account for the alignment
3829 wxRect rectItem
= cell_rect
;
3830 const int align
= custom
->GetAlignment();
3831 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3833 const wxSize size
= custom
->GetSize();
3835 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3837 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3838 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3839 else if ( align
& wxALIGN_RIGHT
)
3840 rectItem
.x
+= cell_rect
.width
- size
.x
;
3841 // else: wxALIGN_LEFT is the default
3844 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3846 if ( align
& wxALIGN_CENTER_VERTICAL
)
3847 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3848 else if ( align
& wxALIGN_BOTTOM
)
3849 rectItem
.y
+= cell_rect
.height
- size
.y
;
3850 // else: wxALIGN_TOP is the default
3854 wxPoint
pos( event
.GetPosition() );
3855 pos
.x
-= rectItem
.x
;
3856 pos
.y
-= rectItem
.y
;
3858 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3860 /* ignore ret */ custom
->LeftClick( pos
, cell_rect
,
3861 model
, item
, col
->GetModelColumn());
3867 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3871 if (HasCurrentRow())
3877 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3881 if (HasCurrentRow())
3887 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3889 if( m_selection
.GetCount() != 1 )
3890 return wxDataViewItem();
3892 return GetItemByRow( m_selection
.Item(0));
3895 //-----------------------------------------------------------------------------
3897 //-----------------------------------------------------------------------------
3899 WX_DEFINE_LIST(wxDataViewColumnList
)
3901 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3902 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3903 EVT_SIZE(wxDataViewCtrl::OnSize
)
3906 wxDataViewCtrl::~wxDataViewCtrl()
3909 GetModel()->RemoveNotifier( m_notifier
);
3912 m_colsBestWidths
.clear();
3915 void wxDataViewCtrl::Init()
3917 m_cols
.DeleteContents(true);
3920 // No sorting column at start
3921 m_sortingColumnIdx
= wxNOT_FOUND
;
3923 m_headerArea
= NULL
;
3926 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3931 const wxValidator
& validator
,
3932 const wxString
& name
)
3934 // if ( (style & wxBORDER_MASK) == 0)
3935 // style |= wxBORDER_SUNKEN;
3939 if (!wxControl::Create( parent
, id
, pos
, size
,
3940 style
| wxScrolledWindowStyle
, validator
, name
))
3943 SetInitialSize(size
);
3946 MacSetClipChildren( true );
3949 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3951 // We use the cursor keys for moving the selection, not scrolling, so call
3952 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3953 // keyboard events forwarded to us from wxListMainWindow.
3954 DisableKeyboardScrolling();
3956 if (HasFlag(wxDV_NO_HEADER
))
3957 m_headerArea
= NULL
;
3959 m_headerArea
= new wxDataViewHeaderWindow(this);
3961 SetTargetWindow( m_clientArea
);
3963 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3965 sizer
->Add( m_headerArea
, 0, wxGROW
);
3966 sizer
->Add( m_clientArea
, 1, wxGROW
);
3972 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3974 return wxBORDER_THEME
;
3978 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3982 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3985 // we need to process arrows ourselves for scrolling
3986 if ( nMsg
== WM_GETDLGCODE
)
3988 rc
|= DLGC_WANTARROWS
;
3996 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3998 wxSize newsize
= size
;
3999 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4000 newsize
.y
-= m_headerArea
->GetSize().y
;
4005 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4007 // We need to override OnSize so that our scrolled
4008 // window a) does call Layout() to use sizers for
4009 // positioning the controls but b) does not query
4010 // the sizer for their size and use that for setting
4011 // the scrollable area as set that ourselves by
4012 // calling SetScrollbar() further down.
4019 void wxDataViewCtrl::SetFocus()
4022 m_clientArea
->SetFocus();
4025 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4027 if (!wxDataViewCtrlBase::AssociateModel( model
))
4030 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4032 model
->AddNotifier( m_notifier
);
4034 m_clientArea
->DestroyTree();
4036 m_clientArea
->BuildTree(model
);
4038 m_clientArea
->UpdateDisplay();
4043 #if wxUSE_DRAG_AND_DROP
4045 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4047 return m_clientArea
->EnableDragSource( format
);
4050 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4052 return m_clientArea
->EnableDropTarget( format
);
4055 #endif // wxUSE_DRAG_AND_DROP
4057 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4059 if (!wxDataViewCtrlBase::AppendColumn(col
))
4062 m_cols
.Append( col
);
4063 m_colsBestWidths
.push_back(0);
4064 OnColumnsCountChanged();
4068 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4070 if (!wxDataViewCtrlBase::PrependColumn(col
))
4073 m_cols
.Insert( col
);
4074 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4075 OnColumnsCountChanged();
4079 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4081 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4084 m_cols
.Insert( pos
, col
);
4085 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4086 OnColumnsCountChanged();
4090 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4093 m_headerArea
->UpdateColumn(idx
);
4095 m_clientArea
->UpdateDisplay();
4098 void wxDataViewCtrl::OnColumnsCountChanged()
4101 m_headerArea
->SetColumnCount(GetColumnCount());
4103 m_clientArea
->UpdateDisplay();
4106 void wxDataViewCtrl::DoSetExpanderColumn()
4108 m_clientArea
->UpdateDisplay();
4111 void wxDataViewCtrl::DoSetIndent()
4113 m_clientArea
->UpdateDisplay();
4116 unsigned int wxDataViewCtrl::GetColumnCount() const
4118 return m_cols
.GetCount();
4121 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4126 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4128 // columns can't be reordered if there is no header window which allows
4130 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4133 return GetColumn(idx
);
4136 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4138 const unsigned count
= m_cols
.size();
4139 for ( unsigned n
= 0; n
< count
; n
++ )
4141 if ( m_cols
[n
] == column
)
4148 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4150 if ( m_colsBestWidths
[idx
] != 0 )
4151 return m_colsBestWidths
[idx
];
4153 const int count
= m_clientArea
->GetRowCount();
4154 wxDataViewColumn
*column
= GetColumn(idx
);
4155 wxDataViewRenderer
*renderer
=
4156 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4158 class MaxWidthCalculator
4161 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4162 wxDataViewRenderer
*renderer
,
4163 const wxDataViewModel
*model
,
4166 m_clientArea(clientArea
),
4167 m_renderer(renderer
),
4173 void UpdateWithWidth(int width
)
4175 m_width
= wxMax(m_width
, width
);
4178 void UpdateWithRow(int row
)
4180 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4181 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4182 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4185 int GetMaxWidth() const { return m_width
; }
4189 wxDataViewMainWindow
*m_clientArea
;
4190 wxDataViewRenderer
*m_renderer
;
4191 const wxDataViewModel
*m_model
;
4195 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4196 GetModel(), column
->GetModelColumn());
4200 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4201 // Labels on native MSW header are indented on both sides
4203 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4204 calculator
.UpdateWithWidth(header_width
);
4207 // The code below deserves some explanation. For very large controls, we
4208 // simply can't afford to calculate sizes for all items, it takes too
4209 // long. So the best we can do is to check the first and the last N/2
4210 // items in the control for some sufficiently large N and calculate best
4211 // sizes from that. That can result in the calculated best width being too
4212 // small for some outliers, but it's better to get slightly imperfect
4213 // result than to wait several seconds after every update. To avoid highly
4214 // visible miscalculations, we also include all currently visible items
4215 // no matter what. Finally, the value of N is determined dynamically by
4216 // measuring how much time we spent on the determining item widths so far.
4219 int top_part_end
= count
;
4220 static const long CALC_TIMEOUT
= 20/*ms*/;
4221 // don't call wxStopWatch::Time() too often
4222 static const unsigned CALC_CHECK_FREQ
= 100;
4225 // use some hard-coded limit, that's the best we can do without timer
4226 int top_part_end
= wxMin(500, count
);
4227 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4231 for ( row
= 0; row
< top_part_end
; row
++ )
4234 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4235 timer
.Time() > CALC_TIMEOUT
)
4237 #endif // wxUSE_STOPWATCH
4238 calculator
.UpdateWithRow(row
);
4241 // row is the first unmeasured item now; that's out value of N/2
4247 // add bottom N/2 items now:
4248 const int bottom_part_start
= wxMax(row
, count
- row
);
4249 for ( row
= bottom_part_start
; row
< count
; row
++ )
4251 calculator
.UpdateWithRow(row
);
4254 // finally, include currently visible items in the calculation:
4255 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4256 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4257 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4259 first_visible
= wxMax(first_visible
, top_part_end
);
4260 last_visible
= wxMin(bottom_part_start
, last_visible
);
4262 for ( row
= first_visible
; row
< last_visible
; row
++ )
4264 calculator
.UpdateWithRow(row
);
4267 wxLogTrace("dataview",
4268 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4270 count
- bottom_part_start
,
4271 wxMax(0, last_visible
- first_visible
),
4275 int max_width
= calculator
.GetMaxWidth();
4276 if ( max_width
> 0 )
4277 max_width
+= 2 * PADDING_RIGHTLEFT
;
4279 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4285 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4286 unsigned int WXUNUSED(new_pos
))
4288 // do _not_ reorder m_cols elements here, they should always be in the
4289 // order in which columns were added, we only display the columns in
4291 m_clientArea
->UpdateDisplay();
4294 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4296 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4300 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4302 OnColumnsCountChanged();
4307 bool wxDataViewCtrl::ClearColumns()
4310 m_colsBestWidths
.clear();
4311 OnColumnsCountChanged();
4315 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4317 m_colsBestWidths
[idx
] = 0;
4320 m_headerArea
->UpdateColumn(idx
);
4323 void wxDataViewCtrl::InvalidateColBestWidths()
4325 m_colsBestWidths
.clear();
4326 m_colsBestWidths
.resize(m_cols
.size());
4330 // this updates visual appearance of columns 0 and up, not just 0
4331 m_headerArea
->UpdateColumn(0);
4335 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4338 unsigned int len
= GetColumnCount();
4339 for ( unsigned int i
= 0; i
< len
; i
++ )
4341 wxDataViewColumn
* col
= GetColumnAt(i
);
4348 // This returns the position in pixels which is not what we want.
4351 unsigned int len
= GetColumnCount();
4352 for ( unsigned int i
= 0; i
< len
; i
++ )
4354 wxDataViewColumn
* col
= GetColumnAt(i
);
4355 if (col
->IsHidden())
4357 ret
+= col
->GetWidth();
4360 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4368 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4370 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4371 : GetColumn(m_sortingColumnIdx
);
4374 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4376 return GetItemByRow(m_clientArea
->GetCurrentRow());
4379 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4381 const int row
= m_clientArea
->GetRowByItem(item
);
4383 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4384 if ( static_cast<unsigned>(row
) != oldCurrent
)
4386 m_clientArea
->ChangeCurrentRow(row
);
4387 m_clientArea
->RefreshRow(oldCurrent
);
4388 m_clientArea
->RefreshRow(row
);
4392 // Selection code with wxDataViewItem as parameters
4393 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4395 return m_clientArea
->GetSelection();
4398 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4401 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4402 int len
= selection
.GetCount();
4403 for( int i
= 0; i
< len
; i
++)
4405 unsigned int row
= selection
[i
];
4406 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4411 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4413 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4415 wxDataViewItem last_parent
;
4417 int len
= sel
.GetCount();
4418 for( int i
= 0; i
< len
; i
++ )
4420 wxDataViewItem item
= sel
[i
];
4421 wxDataViewItem parent
= GetModel()->GetParent( item
);
4424 if (parent
!= last_parent
)
4425 ExpandAncestors(item
);
4428 last_parent
= parent
;
4429 int row
= m_clientArea
->GetRowByItem( item
);
4431 selection
.Add( static_cast<unsigned int>(row
) );
4434 m_clientArea
->SetSelections( selection
);
4437 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4439 ExpandAncestors( item
);
4441 int row
= m_clientArea
->GetRowByItem( item
);
4444 // Unselect all rows before select another in the single select mode
4445 if (m_clientArea
->IsSingleSel())
4446 m_clientArea
->SelectAllRows(false);
4448 m_clientArea
->SelectRow(row
, true);
4450 // Also set focus to the selected item
4451 m_clientArea
->ChangeCurrentRow( row
);
4455 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4457 int row
= m_clientArea
->GetRowByItem( item
);
4459 m_clientArea
->SelectRow(row
, false);
4462 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4464 int row
= m_clientArea
->GetRowByItem( item
);
4467 return m_clientArea
->IsRowSelected(row
);
4472 // Selection code with row number as parameter
4473 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4476 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4477 int len
= selection
.GetCount();
4478 for( int i
= 0; i
< len
; i
++)
4480 unsigned int row
= selection
[i
];
4486 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4488 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4489 int len
= sel
.GetCount();
4490 for( int i
= 0; i
< len
; i
++ )
4494 selection
.Add( static_cast<unsigned int>(row
) );
4496 m_clientArea
->SetSelections( selection
);
4499 void wxDataViewCtrl::Select( int row
)
4503 if (m_clientArea
->IsSingleSel())
4504 m_clientArea
->SelectAllRows(false);
4505 m_clientArea
->SelectRow( row
, true );
4509 void wxDataViewCtrl::Unselect( int row
)
4512 m_clientArea
->SelectRow(row
, false);
4515 bool wxDataViewCtrl::IsSelected( int row
) const
4518 return m_clientArea
->IsRowSelected(row
);
4522 void wxDataViewCtrl::SelectRange( int from
, int to
)
4525 for( int i
= from
; i
< to
; i
++ )
4527 m_clientArea
->Select(sel
);
4530 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4532 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4533 for( int i
= from
; i
< to
; i
++ )
4534 if( sel
.Index( i
) != wxNOT_FOUND
)
4536 m_clientArea
->SetSelections(sel
);
4539 void wxDataViewCtrl::SelectAll()
4541 m_clientArea
->SelectAllRows(true);
4544 void wxDataViewCtrl::UnselectAll()
4546 m_clientArea
->SelectAllRows(false);
4549 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4553 if( row
> (int) m_clientArea
->GetRowCount() )
4554 row
= m_clientArea
->GetRowCount();
4556 int first
= m_clientArea
->GetFirstVisibleRow();
4557 int last
= m_clientArea
->GetLastVisibleRow();
4559 m_clientArea
->ScrollTo( row
, column
);
4560 else if( row
> last
)
4561 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4563 m_clientArea
->ScrollTo( first
, column
);
4566 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4568 ExpandAncestors( item
);
4570 m_clientArea
->RecalculateDisplay();
4572 int row
= m_clientArea
->GetRowByItem(item
);
4575 if( column
== NULL
)
4576 EnsureVisible(row
, -1);
4578 EnsureVisible( row
, GetColumnIndex(column
) );
4583 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4584 wxDataViewColumn
* &column
) const
4586 m_clientArea
->HitTest(point
, item
, column
);
4589 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4590 const wxDataViewColumn
* column
) const
4592 return m_clientArea
->GetItemRect(item
, column
);
4595 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4597 return m_clientArea
->GetItemByRow( row
);
4600 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4602 return m_clientArea
->GetRowByItem( item
);
4605 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4607 ExpandAncestors( item
);
4609 int row
= m_clientArea
->GetRowByItem( item
);
4611 m_clientArea
->Expand(row
);
4614 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4616 int row
= m_clientArea
->GetRowByItem( item
);
4618 m_clientArea
->Collapse(row
);
4621 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4623 int row
= m_clientArea
->GetRowByItem( item
);
4625 return m_clientArea
->IsExpanded(row
);
4629 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4631 wxDataViewColumn
* col
= GetColumn( column
);
4635 wxRect itemRect
= GetItemRect(item
, col
);
4636 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4637 if (renderer
->GetMode() == wxDATAVIEW_CELL_EDITABLE
)
4638 renderer
->StartEditing(item
, itemRect
);
4641 #endif // !wxUSE_GENERICDATAVIEWCTRL
4643 #endif // wxUSE_DATAVIEWCTRL