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 col
);
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 flags
|= wxCONTROL_DISABLED
;
851 // check boxes we draw must always have the same, standard size (if it's
852 // bigger than the cell size the checkbox will be truncated because the
853 // caller had set the clipping rectangle to prevent us from drawing outside
855 cell
.SetSize(GetSize());
857 wxRendererNative::Get().DrawCheckBox(
858 GetOwner()->GetOwner(),
866 void wxDataViewToggleRenderer::WXOnActivate(wxDataViewModel
*model
,
867 const wxVariant
& valueOld
,
868 const wxDataViewItem
& item
,
871 model
->ChangeValue(!valueOld
.GetBool(), item
, col
);
874 wxSize
wxDataViewToggleRenderer::GetSize() const
876 // the window parameter is not used by GetCheckBoxSize() so it's
878 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
881 // ---------------------------------------------------------
882 // wxDataViewProgressRenderer
883 // ---------------------------------------------------------
885 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
887 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
888 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
889 wxDataViewRenderer( varianttype
, mode
, align
)
895 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
897 m_value
= (long) value
;
899 if (m_value
< 0) m_value
= 0;
900 if (m_value
> 100) m_value
= 100;
905 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
907 value
= (long) m_value
;
912 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
914 // deflate the rect to leave a small border between bars in adjacent rows
915 wxRect bar
= rect
.Deflate(0, 1);
917 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
918 dc
->SetPen( *wxBLACK_PEN
);
919 dc
->DrawRectangle( bar
);
921 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
922 dc
->SetPen( *wxTRANSPARENT_PEN
);
924 const wxDataViewItemAttr
& attr
= GetAttr();
925 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
927 dc
->DrawRectangle( bar
);
932 wxSize
wxDataViewProgressRenderer::GetSize() const
934 return wxSize(40,12);
937 // ---------------------------------------------------------
938 // wxDataViewDateRenderer
939 // ---------------------------------------------------------
941 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
943 #if wxUSE_DATE_RENDERER_POPUP
945 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
948 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
949 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
950 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
955 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
956 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
957 sizer
->Add( m_cal
, 1, wxGROW
);
962 void OnCalendar( wxCalendarEvent
&event
);
964 wxCalendarCtrl
*m_cal
;
965 wxDataViewModel
*m_model
;
967 const wxDataViewItem
& m_item
;
970 virtual void OnDismiss()
975 DECLARE_EVENT_TABLE()
978 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
979 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
982 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
984 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
988 #endif // wxUSE_DATE_RENDERER_POPUP
990 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
992 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
993 wxDataViewCellMode mode
, int align
) :
994 wxDataViewRenderer( varianttype
, mode
, align
)
998 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1000 m_date
= value
.GetDateTime();
1005 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1011 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1013 wxString tmp
= m_date
.FormatDate();
1014 RenderText( tmp
, 0, cell
, dc
, state
);
1018 wxSize
wxDataViewDateRenderer::GetSize() const
1020 return GetTextExtent(m_date
.FormatDate());
1023 void wxDataViewDateRenderer::WXOnActivate(wxDataViewModel
*model
,
1024 const wxVariant
& valueOld
,
1025 const wxDataViewItem
& item
,
1028 wxDateTime dtOld
= valueOld
.GetDateTime();
1030 #if wxUSE_DATE_RENDERER_POPUP
1031 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1032 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1033 wxPoint pos
= wxGetMousePosition();
1036 popup
->Popup( popup
->m_cal
);
1037 #else // !wxUSE_DATE_RENDERER_POPUP
1038 wxMessageBox(dtOld
.Format());
1039 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1042 // ---------------------------------------------------------
1043 // wxDataViewIconTextRenderer
1044 // ---------------------------------------------------------
1046 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1048 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1049 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1050 wxDataViewRenderer( varianttype
, mode
, align
)
1053 SetAlignment(align
);
1056 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1062 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1067 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1071 const wxIcon
& icon
= m_value
.GetIcon();
1074 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1075 xoffset
= icon
.GetWidth()+4;
1078 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1083 wxSize
wxDataViewIconTextRenderer::GetSize() const
1085 if (!m_value
.GetText().empty())
1087 wxSize size
= GetTextExtent(m_value
.GetText());
1089 if (m_value
.GetIcon().IsOk())
1090 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1093 return wxSize(80,20);
1096 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1098 wxDataViewIconText iconText
;
1101 wxString text
= iconText
.GetText();
1103 // adjust the label rect to take the width of the icon into account
1104 if (iconText
.GetIcon().IsOk())
1106 int w
= iconText
.GetIcon().GetWidth() + 4;
1108 labelRect
.width
-= w
;
1111 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1112 wxPoint(labelRect
.x
,labelRect
.y
),
1113 wxSize(labelRect
.width
,labelRect
.height
) );
1115 // select the text in the control an place the cursor at the end
1116 ctrl
->SetInsertionPointEnd();
1122 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
& value
)
1124 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1126 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1131 //-----------------------------------------------------------------------------
1132 // wxDataViewDropTarget
1133 //-----------------------------------------------------------------------------
1135 #if wxUSE_DRAG_AND_DROP
1137 class wxBitmapCanvas
: public wxWindow
1140 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1141 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1144 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1147 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1150 dc
.DrawBitmap( m_bitmap
, 0, 0);
1156 class wxDataViewDropSource
: public wxDropSource
1159 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1167 ~wxDataViewDropSource()
1172 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1174 wxPoint pos
= wxGetMousePosition();
1178 int liney
= m_win
->GetLineStart( m_row
);
1180 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1181 m_win
->ClientToScreen( &linex
, &liney
);
1182 m_dist_x
= pos
.x
- linex
;
1183 m_dist_y
= pos
.y
- liney
;
1186 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1188 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1189 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1191 wxFRAME_TOOL_WINDOW
|
1192 wxFRAME_FLOAT_ON_PARENT
|
1193 wxFRAME_NO_TASKBAR
|
1195 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1200 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1201 m_hint
->SetTransparent( 128 );
1207 wxDataViewMainWindow
*m_win
;
1210 int m_dist_x
,m_dist_y
;
1214 class wxDataViewDropTarget
: public wxDropTarget
1217 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1223 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1225 wxDataFormat format
= GetMatchingPair();
1226 if (format
== wxDF_INVALID
)
1228 return m_win
->OnDragOver( format
, x
, y
, def
);
1231 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1233 wxDataFormat format
= GetMatchingPair();
1234 if (format
== wxDF_INVALID
)
1236 return m_win
->OnDrop( format
, x
, y
);
1239 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1241 wxDataFormat format
= GetMatchingPair();
1242 if (format
== wxDF_INVALID
)
1246 return m_win
->OnData( format
, x
, y
, def
);
1249 virtual void OnLeave()
1250 { m_win
->OnLeave(); }
1252 wxDataViewMainWindow
*m_win
;
1255 #endif // wxUSE_DRAG_AND_DROP
1257 //-----------------------------------------------------------------------------
1258 // wxDataViewRenameTimer
1259 //-----------------------------------------------------------------------------
1261 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1266 void wxDataViewRenameTimer::Notify()
1268 m_owner
->OnRenameTimer();
1271 //-----------------------------------------------------------------------------
1272 // wxDataViewMainWindow
1273 //-----------------------------------------------------------------------------
1275 // The tree building helper, declared firstly
1276 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
1277 wxDataViewTreeNode
* node
);
1279 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1281 if (row1
> row2
) return 1;
1282 if (row1
== row2
) return 0;
1286 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1288 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1289 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1290 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1291 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1292 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1293 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1296 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1297 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1298 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1299 m_selection( wxDataViewSelectionCmp
)
1304 m_lastOnSame
= false;
1305 m_renameTimer
= new wxDataViewRenameTimer( this );
1307 // TODO: user better initial values/nothing selected
1308 m_currentCol
= NULL
;
1311 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1313 #if wxUSE_DRAG_AND_DROP
1315 m_dragStart
= wxPoint(0,0);
1317 m_dragEnabled
= false;
1318 m_dropEnabled
= false;
1320 m_dropHintLine
= (unsigned int) -1;
1321 #endif // wxUSE_DRAG_AND_DROP
1323 m_lineLastClicked
= (unsigned int) -1;
1324 m_lineBeforeLastClicked
= (unsigned int) -1;
1325 m_lineSelectSingleOnUp
= (unsigned int) -1;
1329 SetBackgroundColour( *wxWHITE
);
1331 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1333 m_penRule
= wxPen(GetRuleColour());
1335 // compose a pen whichcan draw black lines
1336 // TODO: maybe there is something system colour to use
1337 m_penExpander
= wxPen(wxColour(0,0,0));
1339 m_root
= new wxDataViewTreeNode( NULL
);
1340 m_root
->SetHasChildren(true);
1342 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1344 m_underMouse
= NULL
;
1348 wxDataViewMainWindow::~wxDataViewMainWindow()
1351 delete m_renameTimer
;
1355 #if wxUSE_DRAG_AND_DROP
1356 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1358 m_dragFormat
= format
;
1359 m_dragEnabled
= format
!= wxDF_INVALID
;
1364 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1366 m_dropFormat
= format
;
1367 m_dropEnabled
= format
!= wxDF_INVALID
;
1370 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1375 void wxDataViewMainWindow::RemoveDropHint()
1380 RefreshRow( m_dropHintLine
);
1381 m_dropHintLine
= (unsigned int) -1;
1385 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1386 wxCoord y
, wxDragResult def
)
1390 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1391 unsigned int row
= GetLineAt( yy
);
1393 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1399 wxDataViewItem item
= GetItemByRow( row
);
1401 wxDataViewModel
*model
= GetOwner()->GetModel();
1403 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1404 event
.SetEventObject( m_owner
);
1405 event
.SetItem( item
);
1406 event
.SetModel( model
);
1407 event
.SetDataFormat( format
);
1408 if (!m_owner
->HandleWindowEvent( event
))
1414 if (!event
.IsAllowed())
1421 if (m_dropHint
&& (row
!= m_dropHintLine
))
1422 RefreshRow( m_dropHintLine
);
1424 m_dropHintLine
= row
;
1430 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1436 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1437 unsigned int row
= GetLineAt( yy
);
1439 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1442 wxDataViewItem item
= GetItemByRow( row
);
1444 wxDataViewModel
*model
= GetOwner()->GetModel();
1446 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1447 event
.SetEventObject( m_owner
);
1448 event
.SetItem( item
);
1449 event
.SetModel( model
);
1450 event
.SetDataFormat( format
);
1451 if (!m_owner
->HandleWindowEvent( event
))
1454 if (!event
.IsAllowed())
1460 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1465 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1466 unsigned int row
= GetLineAt( yy
);
1468 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1471 wxDataViewItem item
= GetItemByRow( row
);
1473 wxDataViewModel
*model
= GetOwner()->GetModel();
1475 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1477 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1478 event
.SetEventObject( m_owner
);
1479 event
.SetItem( item
);
1480 event
.SetModel( model
);
1481 event
.SetDataFormat( format
);
1482 event
.SetDataSize( obj
->GetSize() );
1483 event
.SetDataBuffer( obj
->GetData() );
1484 if (!m_owner
->HandleWindowEvent( event
))
1487 if (!event
.IsAllowed())
1493 void wxDataViewMainWindow::OnLeave()
1498 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1500 int height
= GetLineHeight( row
);
1502 unsigned int cols
= GetOwner()->GetColumnCount();
1504 for (col
= 0; col
< cols
; col
++)
1506 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1507 if (column
->IsHidden())
1508 continue; // skip it!
1509 width
+= column
->GetWidth();
1515 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1516 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1517 indent
= indent
+ m_lineHeight
;
1518 // try to use the m_lineHeight as the expander space
1520 if(!node
->HasChildren())
1525 wxBitmap
bitmap( width
, height
);
1526 wxMemoryDC
dc( bitmap
);
1527 dc
.SetFont( GetFont() );
1528 dc
.SetPen( *wxBLACK_PEN
);
1529 dc
.SetBrush( *wxWHITE_BRUSH
);
1530 dc
.DrawRectangle( 0,0,width
,height
);
1532 wxDataViewModel
*model
= m_owner
->GetModel();
1534 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1537 // TODO-RTL: last column for RTL support
1538 expander
= GetOwner()->GetColumnAt( 0 );
1539 GetOwner()->SetExpanderColumn(expander
);
1544 for (col
= 0; col
< cols
; col
++)
1546 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1547 wxDataViewRenderer
*cell
= column
->GetRenderer();
1549 if (column
->IsHidden())
1550 continue; // skip it!
1552 width
= column
->GetWidth();
1554 if (column
== expander
)
1557 wxDataViewItem item
= GetItemByRow( row
);
1558 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1560 wxRect
item_rect(x
, 0, width
, height
);
1561 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1563 // dc.SetClippingRegion( item_rect );
1564 cell
->WXCallRender(item_rect
, &dc
, 0);
1565 // dc.DestroyClippingRegion();
1573 #endif // wxUSE_DRAG_AND_DROP
1576 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1578 wxDataViewModel
*model
= GetOwner()->GetModel();
1579 wxAutoBufferedPaintDC
dc( this );
1582 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1583 dc
.SetPen( *wxTRANSPARENT_PEN
);
1584 dc
.DrawRectangle(GetClientSize());
1588 GetOwner()->PrepareDC( dc
);
1589 dc
.SetFont( GetFont() );
1591 wxRect update
= GetUpdateRegion().GetBox();
1592 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1594 // compute which items needs to be redrawn
1595 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1596 unsigned int item_count
=
1597 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1598 (int)(GetRowCount( ) - item_start
));
1599 unsigned int item_last
= item_start
+ item_count
;
1600 // Get the parent of DataViewCtrl
1601 wxWindow
*parent
= GetParent()->GetParent();
1602 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1603 cache_event
.SetEventObject(GetParent());
1604 cache_event
.SetCache(item_start
, item_last
- 1);
1605 parent
->ProcessWindowEvent(cache_event
);
1607 // compute which columns needs to be redrawn
1608 unsigned int cols
= GetOwner()->GetColumnCount();
1611 // we assume that we have at least one column below and painting an
1612 // empty control is unnecessary anyhow
1616 unsigned int col_start
= 0;
1617 unsigned int x_start
;
1618 for (x_start
= 0; col_start
< cols
; col_start
++)
1620 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1621 if (col
->IsHidden())
1622 continue; // skip it!
1624 unsigned int w
= col
->GetWidth();
1625 if (x_start
+w
>= (unsigned int)update
.x
)
1631 unsigned int col_last
= col_start
;
1632 unsigned int x_last
= x_start
;
1633 for (; col_last
< cols
; col_last
++)
1635 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1636 if (col
->IsHidden())
1637 continue; // skip it!
1639 if (x_last
> (unsigned int)update
.GetRight())
1642 x_last
+= col
->GetWidth();
1645 // Draw horizontal rules if required
1646 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1648 dc
.SetPen(m_penRule
);
1649 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1651 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1653 int y
= GetLineStart( i
);
1654 dc
.DrawLine(x_start
, y
, x_last
, y
);
1658 // Draw vertical rules if required
1659 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1661 dc
.SetPen(m_penRule
);
1662 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1665 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1667 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1668 if (col
->IsHidden())
1669 continue; // skip it
1671 dc
.DrawLine(x
, GetLineStart( item_start
),
1672 x
, GetLineStart( item_last
) );
1674 x
+= col
->GetWidth();
1677 // Draw last vertical rule
1678 dc
.DrawLine(x
, GetLineStart( item_start
),
1679 x
, GetLineStart( item_last
) );
1682 // redraw the background for the items which are selected/current
1683 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1685 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1686 if (selected
|| item
== m_currentRow
)
1688 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1689 if (item
== m_currentRow
)
1690 flags
|= wxCONTROL_CURRENT
;
1692 flags
|= wxCONTROL_FOCUSED
;
1694 wxRect
rect( x_start
, GetLineStart( item
),
1695 x_last
- x_start
, GetLineHeight( item
) );
1696 wxRendererNative::Get().DrawItemSelectionRect
1706 #if wxUSE_DRAG_AND_DROP
1709 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1710 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1711 dc
.SetPen( *wxBLACK_PEN
);
1712 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1713 dc
.DrawRectangle( rect
);
1715 #endif // wxUSE_DRAG_AND_DROP
1717 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1720 // TODO-RTL: last column for RTL support
1721 expander
= GetOwner()->GetColumnAt( 0 );
1722 GetOwner()->SetExpanderColumn(expander
);
1725 // redraw all cells for all rows which must be repainted and all columns
1727 cell_rect
.x
= x_start
;
1728 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1730 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1731 wxDataViewRenderer
*cell
= col
->GetRenderer();
1732 cell_rect
.width
= col
->GetWidth();
1734 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1735 continue; // skip it!
1737 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1739 // get the cell value and set it into the renderer
1740 wxDataViewTreeNode
*node
= NULL
;
1741 wxDataViewItem dataitem
;
1743 if (!IsVirtualList())
1745 node
= GetTreeNodeByRow(item
);
1749 dataitem
= node
->GetItem();
1751 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1752 !model
->HasContainerColumns(dataitem
))
1757 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1760 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1763 cell_rect
.y
= GetLineStart( item
);
1764 cell_rect
.height
= GetLineHeight( item
);
1766 // deal with the expander
1768 if ((!IsList()) && (col
== expander
))
1770 // Calculate the indent first
1771 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1773 // we reserve m_lineHeight of horizontal space for the expander
1774 // but leave EXPANDER_MARGIN around the expander itself
1775 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1777 indent
+= m_lineHeight
;
1779 // draw expander if needed and visible
1780 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1782 dc
.SetPen( m_penExpander
);
1783 dc
.SetBrush( wxNullBrush
);
1785 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1786 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1787 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1789 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1792 if ( m_underMouse
== node
)
1793 flag
|= wxCONTROL_CURRENT
;
1794 if ( node
->IsOpen() )
1795 flag
|= wxCONTROL_EXPANDED
;
1797 // ensure that we don't overflow the cell (which might
1798 // happen if the column is very narrow)
1799 wxDCClipper
clip(dc
, cell_rect
);
1801 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1804 // force the expander column to left-center align
1805 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1807 if (node
&& !node
->HasChildren())
1809 // Yes, if the node does not have any child, it must be a leaf which
1810 // mean that it is a temporarily created by GetTreeNodeByRow
1814 wxRect item_rect
= cell_rect
;
1815 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1817 // account for the tree indent (harmless if we're not indented)
1818 item_rect
.x
+= indent
;
1819 item_rect
.width
-= indent
;
1821 if ( item_rect
.width
<= 0 )
1825 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1826 state
|= wxDATAVIEW_CELL_SELECTED
;
1828 // TODO: it would be much more efficient to create a clipping
1829 // region for the entire column being rendered (in the OnPaint
1830 // of wxDataViewMainWindow) instead of a single clip region for
1831 // each cell. However it would mean that each renderer should
1832 // respect the given wxRect's top & bottom coords, eventually
1833 // violating only the left & right coords - however the user can
1834 // make its own renderer and thus we cannot be sure of that.
1835 wxDCClipper
clip(dc
, item_rect
);
1837 cell
->WXCallRender(item_rect
, &dc
, state
);
1840 cell_rect
.x
+= cell_rect
.width
;
1844 void wxDataViewMainWindow::OnRenameTimer()
1846 // We have to call this here because changes may just have
1847 // been made and no screen update taken place.
1850 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1851 // (needs to be tested!)
1855 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1857 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1859 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1862 //-----------------------------------------------------------------------------
1863 // Helper class for do operation on the tree node
1864 //-----------------------------------------------------------------------------
1869 virtual ~DoJob() { }
1871 // The return value control how the tree-walker tranverse the tree
1872 // 0: Job done, stop tranverse and return
1873 // 1: Ignore the current node's subtree and continue
1874 // 2: Job not done, continue
1875 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1876 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1877 virtual int operator() ( void * n
) = 0;
1880 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1885 switch( func( node
) )
1896 wxDataViewTreeNodes nodes
= node
->GetNodes();
1897 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1899 int len_nodes
= nodes
.GetCount();
1900 int len
= leaves
.GetCount();
1901 int i
= 0, nodes_i
= 0;
1903 for(; i
< len
; i
++ )
1905 void * n
= leaves
[i
];
1906 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1908 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1911 if( Walker( nd
, func
) )
1930 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1932 GetOwner()->InvalidateColBestWidths();
1934 if (IsVirtualList())
1936 wxDataViewVirtualListModel
*list_model
=
1937 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1938 m_count
= list_model
->GetCount();
1945 wxDataViewTreeNode
* node
;
1946 node
= FindNode(parent
);
1951 node
->SetHasChildren( true );
1953 if( g_model
->IsContainer( item
) )
1955 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1956 newnode
->SetItem(item
);
1957 newnode
->SetHasChildren( true );
1958 node
->AddNode( newnode
);
1961 node
->AddLeaf( item
.GetID() );
1963 node
->ChangeSubTreeCount(1);
1971 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1973 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1974 const wxDataViewItem
& item
)
1976 GetOwner()->InvalidateColBestWidths();
1978 if (IsVirtualList())
1980 wxDataViewVirtualListModel
*list_model
=
1981 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1982 m_count
= list_model
->GetCount();
1984 if( m_currentRow
> GetRowCount() )
1985 m_currentRow
= m_count
- 1;
1987 // TODO: why empty the entire selection?
1988 m_selection
.Empty();
1995 wxDataViewTreeNode
* node
= FindNode(parent
);
1997 // Notice that it is possible that the item being deleted is not in the
1998 // tree at all, for example we could be deleting a never shown (because
1999 // collapsed) item in a tree model. So it's not an error if we don't know
2000 // about this item, just return without doing anything then.
2001 if ( !node
|| node
->GetChildren().Index(item
.GetID()) == wxNOT_FOUND
)
2005 node
->GetChildren().Remove( item
.GetID() );
2006 // Manipolate selection
2007 if( m_selection
.GetCount() > 1 )
2009 m_selection
.Empty();
2011 bool isContainer
= false;
2012 wxDataViewTreeNodes nds
= node
->GetNodes();
2013 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2015 if (nds
[i
]->GetItem() == item
)
2023 wxDataViewTreeNode
* n
= NULL
;
2024 wxDataViewTreeNodes nodes
= node
->GetNodes();
2025 int len
= nodes
.GetCount();
2026 for( int i
= 0; i
< len
; i
++)
2028 if( nodes
[i
]->GetItem() == item
)
2035 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2037 node
->GetNodes().Remove( n
);
2038 sub
-= n
->GetSubTreeCount();
2039 ::DestroyTreeHelper(n
);
2041 // Make the row number invalid and get a new valid one when user call GetRowCount
2043 node
->ChangeSubTreeCount(sub
);
2045 // Change the current row to the last row if the current exceed the max row number
2046 if( m_currentRow
> GetRowCount() )
2047 m_currentRow
= m_count
- 1;
2054 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2056 GetOwner()->InvalidateColBestWidths();
2062 wxWindow
*parent
= GetParent();
2063 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2064 le
.SetEventObject(parent
);
2065 le
.SetModel(GetOwner()->GetModel());
2067 parent
->GetEventHandler()->ProcessEvent(le
);
2072 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2074 GetOwner()->InvalidateColBestWidth(col
);
2076 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2077 /*#define MAX_VIRTUAL_WIDTH 100000
2079 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2080 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2081 Refresh( true, &rect );
2089 wxWindow
*parent
= GetParent();
2090 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2091 le
.SetEventObject(parent
);
2092 le
.SetModel(GetOwner()->GetModel());
2095 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2096 parent
->GetEventHandler()->ProcessEvent(le
);
2101 bool wxDataViewMainWindow::Cleared()
2103 GetOwner()->InvalidateColBestWidths();
2106 m_selection
.Clear();
2109 BuildTree( GetOwner()->GetModel() );
2116 void wxDataViewMainWindow::UpdateDisplay()
2119 m_underMouse
= NULL
;
2122 void wxDataViewMainWindow::OnInternalIdle()
2124 wxWindow::OnInternalIdle();
2128 RecalculateDisplay();
2133 void wxDataViewMainWindow::RecalculateDisplay()
2135 wxDataViewModel
*model
= GetOwner()->GetModel();
2142 int width
= GetEndOfLastCol();
2143 int height
= GetLineStart( GetRowCount() );
2145 SetVirtualSize( width
, height
);
2146 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2151 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2153 wxWindow::ScrollWindow( dx
, dy
, rect
);
2155 if (GetOwner()->m_headerArea
)
2156 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2159 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2162 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2163 int sy
= GetLineStart( rows
)/y
;
2167 wxRect rect
= GetClientRect();
2171 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2172 for (x_start
= 0; colnum
< column
; colnum
++)
2174 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2175 if (col
->IsHidden())
2176 continue; // skip it!
2178 w
= col
->GetWidth();
2182 int x_end
= x_start
+ w
;
2183 xe
= xx
+ rect
.width
;
2186 sx
= ( xx
+ x_end
- xe
)/x
;
2193 m_owner
->Scroll( sx
, sy
);
2196 int wxDataViewMainWindow::GetCountPerPage() const
2198 wxSize size
= GetClientSize();
2199 return size
.y
/ m_lineHeight
;
2202 int wxDataViewMainWindow::GetEndOfLastCol() const
2206 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2208 const wxDataViewColumn
*c
=
2209 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2212 width
+= c
->GetWidth();
2217 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2221 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2223 return GetLineAt( y
);
2226 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2228 wxSize client_size
= GetClientSize();
2229 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2230 &client_size
.x
, &client_size
.y
);
2232 // we should deal with the pixel here
2233 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2235 return wxMin( GetRowCount()-1, row
);
2238 unsigned int wxDataViewMainWindow::GetRowCount()
2240 if ( m_count
== -1 )
2242 m_count
= RecalculateCount();
2248 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2255 void wxDataViewMainWindow::SelectAllRows( bool on
)
2262 m_selection
.Clear();
2263 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2264 m_selection
.Add( i
);
2269 unsigned int first_visible
= GetFirstVisibleRow();
2270 unsigned int last_visible
= GetLastVisibleRow();
2272 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2274 unsigned int row
= m_selection
[i
];
2275 if ((row
>= first_visible
) && (row
<= last_visible
))
2278 m_selection
.Clear();
2282 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2284 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2288 m_selection
.Add( row
);
2296 m_selection
.Remove( row
);
2302 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2306 unsigned int tmp
= from
;
2312 for (i
= from
; i
<= to
; i
++)
2314 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2317 m_selection
.Add( i
);
2322 m_selection
.Remove( i
);
2325 RefreshRows( from
, to
);
2328 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2330 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2332 int n
= aSelections
[i
];
2334 m_selection
.Add( n
);
2339 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2341 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2342 m_selection
.Add( row
);
2344 m_selection
.Remove( row
);
2348 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2350 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2353 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2355 wxWindow
*parent
= GetParent();
2356 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2358 le
.SetEventObject(parent
);
2359 le
.SetModel(GetOwner()->GetModel());
2362 parent
->GetEventHandler()->ProcessEvent(le
);
2365 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2367 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2368 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2370 wxSize client_size
= GetClientSize();
2371 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2372 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2373 if (intersect_rect
.width
> 0)
2374 Refresh( true, &intersect_rect
);
2377 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2381 unsigned int tmp
= to
;
2386 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2387 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2389 wxSize client_size
= GetClientSize();
2390 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2391 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2392 if (intersect_rect
.width
> 0)
2393 Refresh( true, &intersect_rect
);
2396 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2398 wxSize client_size
= GetClientSize();
2399 int start
= GetLineStart( firstRow
);
2400 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2401 if (start
> client_size
.y
) return;
2403 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2405 Refresh( true, &rect
);
2408 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2410 wxCHECK_RET( newCurrent
< GetRowCount(),
2411 wxT("invalid item index in OnArrowChar()") );
2413 // if there is no selection, we cannot move it anywhere
2414 if (!HasCurrentRow())
2417 unsigned int oldCurrent
= m_currentRow
;
2419 // in single selection we just ignore Shift as we can't select several
2421 if ( event
.ShiftDown() && !IsSingleSel() )
2423 RefreshRow( oldCurrent
);
2425 ChangeCurrentRow( newCurrent
);
2427 // select all the items between the old and the new one
2428 if ( oldCurrent
> newCurrent
)
2430 newCurrent
= oldCurrent
;
2431 oldCurrent
= m_currentRow
;
2434 SelectRows( oldCurrent
, newCurrent
, true );
2435 if (oldCurrent
!=newCurrent
)
2436 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2440 RefreshRow( oldCurrent
);
2442 // all previously selected items are unselected unless ctrl is held
2443 if ( !event
.ControlDown() )
2444 SelectAllRows(false);
2446 ChangeCurrentRow( newCurrent
);
2448 if ( !event
.ControlDown() )
2450 SelectRow( m_currentRow
, true );
2451 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2454 RefreshRow( m_currentRow
);
2457 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2460 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2464 rect
.y
= GetLineStart( row
);
2465 rect
.width
= GetEndOfLastCol();
2466 rect
.height
= GetLineHeight( row
);
2471 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2473 const wxDataViewModel
*model
= GetOwner()->GetModel();
2475 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2477 // TODO make more efficient
2482 for (r
= 0; r
< row
; r
++)
2484 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2485 if (!node
) return start
;
2487 wxDataViewItem item
= node
->GetItem();
2489 if (node
&& !node
->HasChildren())
2491 // Yes, if the node does not have any child, it must be a leaf which
2492 // mean that it is a temporarily created by GetTreeNodeByRow
2496 unsigned int cols
= GetOwner()->GetColumnCount();
2498 int height
= m_lineHeight
;
2499 for (col
= 0; col
< cols
; col
++)
2501 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2502 if (column
->IsHidden())
2503 continue; // skip it!
2506 model
->IsContainer(item
) &&
2507 !model
->HasContainerColumns(item
))
2508 continue; // skip it!
2510 wxDataViewRenderer
*renderer
=
2511 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2512 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2514 height
= wxMax( height
, renderer
->GetSize().y
);
2524 return row
* m_lineHeight
;
2528 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2530 const wxDataViewModel
*model
= GetOwner()->GetModel();
2532 // check for the easy case first
2533 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2534 return y
/ m_lineHeight
;
2536 // TODO make more efficient
2537 unsigned int row
= 0;
2538 unsigned int yy
= 0;
2541 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2544 // not really correct...
2545 return row
+ ((y
-yy
) / m_lineHeight
);
2548 wxDataViewItem item
= node
->GetItem();
2550 if (node
&& !node
->HasChildren())
2552 // Yes, if the node does not have any child, it must be a leaf which
2553 // mean that it is a temporarily created by GetTreeNodeByRow
2557 unsigned int cols
= GetOwner()->GetColumnCount();
2559 int height
= m_lineHeight
;
2560 for (col
= 0; col
< cols
; col
++)
2562 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2563 if (column
->IsHidden())
2564 continue; // skip it!
2567 model
->IsContainer(item
) &&
2568 !model
->HasContainerColumns(item
))
2569 continue; // skip it!
2571 wxDataViewRenderer
*renderer
=
2572 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2573 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2575 height
= wxMax( height
, renderer
->GetSize().y
);
2586 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2588 const wxDataViewModel
*model
= GetOwner()->GetModel();
2590 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2592 wxASSERT( !IsVirtualList() );
2594 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2595 // wxASSERT( node );
2596 if (!node
) return m_lineHeight
;
2598 wxDataViewItem item
= node
->GetItem();
2600 if (node
&& !node
->HasChildren())
2602 // Yes, if the node does not have any child, it must be a leaf which
2603 // mean that it is a temporarily created by GetTreeNodeByRow
2607 int height
= m_lineHeight
;
2609 unsigned int cols
= GetOwner()->GetColumnCount();
2611 for (col
= 0; col
< cols
; col
++)
2613 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2614 if (column
->IsHidden())
2615 continue; // skip it!
2618 model
->IsContainer(item
) &&
2619 !model
->HasContainerColumns(item
))
2620 continue; // skip it!
2622 wxDataViewRenderer
*renderer
=
2623 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2624 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2626 height
= wxMax( height
, renderer
->GetSize().y
);
2633 return m_lineHeight
;
2637 class RowToItemJob
: public DoJob
2640 RowToItemJob( unsigned int row
, int current
)
2641 { this->row
= row
; this->current
= current
; }
2642 virtual ~RowToItemJob() {}
2644 virtual int operator() ( wxDataViewTreeNode
* node
)
2647 if( current
== static_cast<int>(row
))
2649 ret
= node
->GetItem();
2653 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2655 current
+= node
->GetSubTreeCount();
2660 // If the current has no child node, we can find the desired item of the row
2662 // This if can speed up finding in some case, and will has a very good effect
2663 // when it comes to list view
2664 if( node
->GetNodes().GetCount() == 0)
2666 int index
= static_cast<int>(row
) - current
- 1;
2667 ret
= node
->GetChildren().Item( index
);
2674 virtual int operator() ( void * n
)
2677 if( current
== static_cast<int>(row
))
2679 ret
= wxDataViewItem( n
);
2685 wxDataViewItem
GetResult() const
2694 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2696 if (IsVirtualList())
2698 return wxDataViewItem( wxUIntToPtr(row
+1) );
2702 RowToItemJob
job( row
, -2 );
2703 Walker( m_root
, job
);
2704 return job
.GetResult();
2708 class RowToTreeNodeJob
: public DoJob
2711 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2714 this->current
= current
;
2718 virtual ~RowToTreeNodeJob(){ }
2720 virtual int operator() ( wxDataViewTreeNode
* node
)
2723 if( current
== static_cast<int>(row
))
2729 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2731 current
+= node
->GetSubTreeCount();
2738 // If the current node has no children, we can find the desired item of the
2739 // row number directly.
2740 // This if can speed up finding in some case, and will have a very good
2741 // effect for list views.
2742 if( node
->GetNodes().GetCount() == 0)
2744 int index
= static_cast<int>(row
) - current
- 1;
2745 void * n
= node
->GetChildren().Item( index
);
2746 ret
= new wxDataViewTreeNode( parent
);
2747 ret
->SetItem( wxDataViewItem( n
));
2748 ret
->SetHasChildren(false);
2755 virtual int operator() ( void * n
)
2758 if( current
== static_cast<int>(row
))
2760 ret
= new wxDataViewTreeNode( parent
);
2761 ret
->SetItem( wxDataViewItem( n
));
2762 ret
->SetHasChildren(false);
2769 wxDataViewTreeNode
* GetResult() const
2775 wxDataViewTreeNode
* ret
;
2776 wxDataViewTreeNode
* parent
;
2779 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2781 wxASSERT( !IsVirtualList() );
2783 RowToTreeNodeJob
job( row
, -2, m_root
);
2784 Walker( m_root
, job
);
2785 return job
.GetResult();
2788 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2789 const wxDataViewItem
& item
)
2791 wxWindow
*parent
= GetParent();
2792 wxDataViewEvent
le(type
, parent
->GetId());
2794 le
.SetEventObject(parent
);
2795 le
.SetModel(GetOwner()->GetModel());
2798 parent
->GetEventHandler()->ProcessEvent(le
);
2802 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2807 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2811 if (!node
->HasChildren())
2817 return node
->IsOpen();
2820 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2825 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2829 if (!node
->HasChildren())
2838 void wxDataViewMainWindow::Expand( unsigned int row
)
2843 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2847 if (!node
->HasChildren())
2853 if (!node
->IsOpen())
2856 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2858 // Check if the user prevent expanding
2859 if( e
.GetSkipped() )
2864 // build the children of current node
2865 if( node
->GetChildrenNumber() == 0 )
2868 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2871 // By expanding the node all row indices that are currently in the selection list
2872 // and are greater than our node have become invalid. So we have to correct that now.
2873 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2874 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2876 const unsigned testRow
= m_selection
[i
];
2877 // all rows above us are not affected, so skip them
2881 m_selection
[i
] += rowAdjustment
;
2884 if(m_currentRow
> row
)
2885 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2889 // Send the expanded event
2890 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2894 void wxDataViewMainWindow::Collapse(unsigned int row
)
2899 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2903 if (!node
->HasChildren())
2912 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2913 if( e
.GetSkipped() )
2916 // Find out if there are selected items below the current node.
2917 bool selectCollapsingRow
= false;
2918 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2919 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2920 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2922 const unsigned testRow
= m_selection
[i
];
2923 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2925 selectCollapsingRow
= true;
2926 // get out as soon as we have found a node that is selected
2933 // If the node to be closed has selected items the user won't see those any longer.
2934 // We select the collapsing node in this case.
2935 if(selectCollapsingRow
)
2937 SelectAllRows(false);
2938 ChangeCurrentRow(row
);
2939 SelectRow(row
, true);
2940 SendSelectionChangedEvent(GetItemByRow(row
));
2944 // if there were no selected items below our node we still need to "fix" the
2945 // selection list to adjust for the changing of the row indices.
2946 // We actually do the opposite of what we are doing in Expand().
2947 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2949 const unsigned testRow
= m_selection
[i
];
2950 // all rows above us are not affected, so skip them
2954 m_selection
[i
] -= rowAdjustment
;
2957 // if the "current row" is being collapsed away we change it to the current row ;-)
2958 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2959 ChangeCurrentRow(row
);
2960 else if(m_currentRow
> row
)
2961 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2966 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2970 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2972 wxDataViewModel
* model
= GetOwner()->GetModel();
2979 // Compose the a parent-chain of the finding item
2981 list
.DeleteContents( true );
2982 wxDataViewItem
it( item
);
2985 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2986 list
.Insert( pItem
);
2987 it
= model
->GetParent( it
);
2990 // Find the item along the parent-chain.
2991 // This algorithm is designed to speed up the node-finding method
2992 wxDataViewTreeNode
* node
= m_root
;
2993 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
2995 if( node
->HasChildren() )
2997 if( node
->GetChildrenNumber() == 0 )
3000 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3003 wxDataViewTreeNodes nodes
= node
->GetNodes();
3007 for (i
= 0; i
< nodes
.GetCount(); i
++)
3009 if (nodes
[i
]->GetItem() == (**iter
))
3011 if (nodes
[i
]->GetItem() == item
)
3028 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3029 wxDataViewColumn
* &column
)
3031 wxDataViewColumn
*col
= NULL
;
3032 unsigned int cols
= GetOwner()->GetColumnCount();
3033 unsigned int colnum
= 0;
3035 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3036 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3038 col
= GetOwner()->GetColumnAt(colnum
);
3039 if (col
->IsHidden())
3040 continue; // skip it!
3042 unsigned int w
= col
->GetWidth();
3043 if (x_start
+w
>= (unsigned int)x
)
3050 item
= GetItemByRow( GetLineAt( y
) );
3053 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3054 const wxDataViewColumn
* column
)
3059 unsigned int cols
= GetOwner()->GetColumnCount();
3060 // If column is null the loop will compute the combined width of all columns.
3061 // Otherwise, it will compute the x position of the column we are looking for.
3062 for (unsigned int i
= 0; i
< cols
; i
++)
3064 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3069 if (col
->IsHidden())
3070 continue; // skip it!
3072 xpos
+= col
->GetWidth();
3073 width
+= col
->GetWidth();
3078 // If we have a column, we need can get its width directly.
3079 if(column
->IsHidden())
3082 width
= column
->GetWidth();
3087 // If we have no column, we reset the x position back to zero.
3091 // we have to take an expander column into account and compute its indentation
3092 // to get the correct x position where the actual text is
3094 int row
= GetRowByItem(item
);
3095 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3097 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3098 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3099 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3101 if(!node
->HasChildren())
3105 wxRect
itemRect( xpos
+ indent
,
3106 GetLineStart( row
),
3108 GetLineHeight( row
) );
3110 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3111 &itemRect
.x
, &itemRect
.y
);
3116 int wxDataViewMainWindow::RecalculateCount()
3118 if (IsVirtualList())
3120 wxDataViewVirtualListModel
*list_model
=
3121 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3123 return list_model
->GetCount();
3127 return m_root
->GetSubTreeCount();
3131 class ItemToRowJob
: public DoJob
3134 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3141 // Maybe binary search will help to speed up this process
3142 virtual int operator() ( wxDataViewTreeNode
* node
)
3145 if( node
->GetItem() == item
)
3150 if( node
->GetItem() == **m_iter
)
3157 ret
+= node
->GetSubTreeCount();
3163 virtual int operator() ( void * n
)
3166 if( n
== item
.GetID() )
3171 // the row number is begin from zero
3172 int GetResult() const
3176 ItemList::const_iterator m_iter
;
3177 wxDataViewItem item
;
3182 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3184 const wxDataViewModel
* model
= GetOwner()->GetModel();
3188 if (IsVirtualList())
3190 return wxPtrToUInt( item
.GetID() ) -1;
3197 // Compose the a parent-chain of the finding item
3199 wxDataViewItem
* pItem
;
3200 list
.DeleteContents( true );
3201 wxDataViewItem
it( item
);
3204 pItem
= new wxDataViewItem( it
);
3205 list
.Insert( pItem
);
3206 it
= model
->GetParent( it
);
3208 pItem
= new wxDataViewItem( );
3209 list
.Insert( pItem
);
3211 ItemToRowJob
job( item
, list
.begin() );
3212 Walker(m_root
, job
);
3213 return job
.GetResult();
3217 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
3218 wxDataViewTreeNode
* node
)
3220 if( !model
->IsContainer( item
) )
3223 wxDataViewItemArray children
;
3224 unsigned int num
= model
->GetChildren( item
, children
);
3226 unsigned int index
= 0;
3227 while( index
< num
)
3229 if( model
->IsContainer( children
[index
] ) )
3231 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3232 n
->SetItem(children
[index
]);
3233 n
->SetHasChildren( true );
3238 node
->AddLeaf( children
[index
].GetID() );
3242 node
->SetSubTreeCount( num
);
3243 wxDataViewTreeNode
* n
= node
->GetParent();
3245 n
->ChangeSubTreeCount(num
);
3249 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3253 if (GetOwner()->GetModel()->IsVirtualListModel())
3259 m_root
= new wxDataViewTreeNode( NULL
);
3260 m_root
->SetHasChildren(true);
3262 // First we define a invalid item to fetch the top-level elements
3263 wxDataViewItem item
;
3265 BuildTreeHelper( model
, item
, m_root
);
3269 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3271 if( node
->GetNodeNumber() != 0 )
3273 int len
= node
->GetNodeNumber();
3274 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3275 for (int i
= 0; i
< len
; i
++)
3276 DestroyTreeHelper(nodes
[i
]);
3281 void wxDataViewMainWindow::DestroyTree()
3283 if (!IsVirtualList())
3285 ::DestroyTreeHelper(m_root
);
3291 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3293 wxWindow
* const parent
= GetParent();
3295 // propagate the char event upwards
3296 wxKeyEvent
eventForParent(event
);
3297 eventForParent
.SetEventObject(parent
);
3298 if ( parent
->ProcessWindowEvent(eventForParent
) )
3301 if ( parent
->HandleAsNavigationKey(event
) )
3304 // no item -> nothing to do
3305 if (!HasCurrentRow())
3311 // don't use m_linesPerPage directly as it might not be computed yet
3312 const int pageSize
= GetCountPerPage();
3313 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3315 switch ( event
.GetKeyCode() )
3319 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3321 le
.SetItem( GetItemByRow(m_currentRow
) );
3322 le
.SetEventObject(parent
);
3323 le
.SetModel(GetOwner()->GetModel());
3325 parent
->GetEventHandler()->ProcessEvent(le
);
3330 if ( m_currentRow
> 0 )
3331 OnArrowChar( m_currentRow
- 1, event
);
3335 if ( m_currentRow
< GetRowCount() - 1 )
3336 OnArrowChar( m_currentRow
+ 1, event
);
3338 // Add the process for tree expanding/collapsing
3344 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3348 if (node
->HasChildren() && node
->IsOpen())
3350 Collapse(m_currentRow
);
3352 else // if the node is already closed we move the selection to its parent
3354 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3356 if(!node
->HasChildren())
3361 int parent
= GetRowByItem( parent_node
->GetItem() );
3364 unsigned int row
= m_currentRow
;
3365 SelectRow( row
, false);
3366 SelectRow( parent
, true );
3367 ChangeCurrentRow( parent
);
3368 GetOwner()->EnsureVisible( parent
, -1 );
3369 SendSelectionChangedEvent( parent_node
->GetItem() );
3377 if (!IsExpanded( m_currentRow
))
3378 Expand( m_currentRow
);
3381 unsigned int row
= m_currentRow
;
3382 SelectRow( row
, false );
3383 SelectRow( row
+ 1, true );
3384 ChangeCurrentRow( row
+ 1 );
3385 GetOwner()->EnsureVisible( row
+ 1, -1 );
3386 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3393 OnArrowChar( GetRowCount() - 1, event
);
3398 OnArrowChar( 0, event
);
3403 int steps
= pageSize
- 1;
3404 int index
= m_currentRow
- steps
;
3408 OnArrowChar( index
, event
);
3414 int steps
= pageSize
- 1;
3415 unsigned int index
= m_currentRow
+ steps
;
3416 unsigned int count
= GetRowCount();
3417 if ( index
>= count
)
3420 OnArrowChar( index
, event
);
3426 if(m_selection
.size() == 1)
3428 // TODO: we need to revise that when we have a concept for a 'current column'
3429 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3439 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3441 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3443 // let the base handle mouse wheel events.
3448 // set the focus to ourself if any of the mouse buttons are pressed
3449 if(event
.ButtonDown() && !HasFocus())
3452 int x
= event
.GetX();
3453 int y
= event
.GetY();
3454 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3455 wxDataViewColumn
*col
= NULL
;
3458 unsigned int cols
= GetOwner()->GetColumnCount();
3460 for (i
= 0; i
< cols
; i
++)
3462 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3464 continue; // skip it!
3466 if (x
< xpos
+ c
->GetWidth())
3471 xpos
+= c
->GetWidth();
3476 wxDataViewRenderer
*cell
= col
->GetRenderer();
3477 unsigned int current
= GetLineAt( y
);
3478 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3480 // Unselect all if below the last row ?
3484 // Test whether the mouse is hovered on the tree item button
3485 bool hoverOverExpander
= false;
3486 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3488 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3489 if( node
!=NULL
&& node
->HasChildren() )
3491 int indent
= node
->GetIndentLevel();
3492 indent
= GetOwner()->GetIndent()*indent
;
3494 // we make the rectangle we are looking in a bit bigger than the actual
3495 // visual expander so the user can hit that little thing reliably
3496 wxRect
rect( xpos
+ indent
,
3497 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3498 m_lineHeight
, m_lineHeight
);
3499 if( rect
.Contains(x
, y
) )
3501 // So the mouse is over the expander
3502 hoverOverExpander
= true;
3503 if (m_underMouse
&& m_underMouse
!= node
)
3505 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3506 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3508 if (m_underMouse
!= node
)
3510 // wxLogMessage("Do the row: %d", current);
3511 RefreshRow(current
);
3513 m_underMouse
= node
;
3516 if (node
!=NULL
&& !node
->HasChildren())
3519 if (!hoverOverExpander
)
3521 if (m_underMouse
!= NULL
)
3523 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3524 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3525 m_underMouse
= NULL
;
3529 wxDataViewModel
*model
= GetOwner()->GetModel();
3531 #if wxUSE_DRAG_AND_DROP
3532 if (event
.Dragging())
3534 if (m_dragCount
== 0)
3536 // we have to report the raw, physical coords as we want to be
3537 // able to call HitTest(event.m_pointDrag) from the user code to
3538 // get the item being dragged
3539 m_dragStart
= event
.GetPosition();
3544 if (m_dragCount
!= 3)
3547 if (event
.LeftIsDown())
3549 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3550 &m_dragStart
.x
, &m_dragStart
.y
);
3551 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3552 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3554 // Notify cell about drag
3555 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3556 event
.SetEventObject( m_owner
);
3557 event
.SetItem( item
);
3558 event
.SetModel( model
);
3559 if (!m_owner
->HandleWindowEvent( event
))
3562 if (!event
.IsAllowed())
3565 wxDataObject
*obj
= event
.GetDataObject();
3569 wxDataViewDropSource
drag( this, drag_item_row
);
3570 drag
.SetData( *obj
);
3571 /* wxDragResult res = */ drag
.DoDragDrop();
3580 #endif // wxUSE_DRAG_AND_DROP
3582 bool simulateClick
= false;
3584 if (event
.ButtonDClick())
3586 m_renameTimer
->Stop();
3587 m_lastOnSame
= false;
3590 wxDataViewItem item
= GetItemByRow(current
);
3591 bool ignore_other_columns
=
3592 ((GetOwner()->GetExpanderColumn() != col
) &&
3593 (model
->IsContainer(item
)) &&
3594 (!model
->HasContainerColumns(item
)));
3596 if (event
.LeftDClick())
3598 if(hoverOverExpander
)
3600 // a double click on the expander will be converted into a "simulated" normal click
3601 simulateClick
= true;
3603 else if ( current
== m_lineLastClicked
)
3605 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3607 const unsigned colIdx
= col
->GetModelColumn();
3610 model
->GetValue( value
, item
, colIdx
);
3612 cell
->WXOnActivate(model
, value
, item
, colIdx
);
3614 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3616 cell
->SetValue( value
);
3618 wxRect
cell_rect( xpos
, GetLineStart( current
),
3619 col
->GetWidth(), GetLineHeight( current
) );
3620 custom
->Activate( cell_rect
, model
, item
, colIdx
);
3625 wxWindow
*parent
= GetParent();
3626 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3628 le
.SetEventObject(parent
);
3629 le
.SetModel(GetOwner()->GetModel());
3631 parent
->GetEventHandler()->ProcessEvent(le
);
3637 // The first click was on another item, so don't interpret this as
3638 // a double click, but as a simple click instead
3639 simulateClick
= true;
3643 if (event
.LeftUp() && !hoverOverExpander
)
3645 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3647 // select single line
3648 SelectAllRows( false );
3649 SelectRow( m_lineSelectSingleOnUp
, true );
3650 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3653 // If the user click the expander, we do not do editing even if the column
3654 // with expander are editable
3655 if (m_lastOnSame
&& !ignore_other_columns
)
3657 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3658 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3660 m_renameTimer
->Start( 100, true );
3664 m_lastOnSame
= false;
3665 m_lineSelectSingleOnUp
= (unsigned int)-1;
3667 else if(!event
.LeftUp())
3669 // This is necessary, because after a DnD operation in
3670 // from and to ourself, the up event is swallowed by the
3671 // DnD code. So on next non-up event (which means here and
3672 // now) m_lineSelectSingleOnUp should be reset.
3673 m_lineSelectSingleOnUp
= (unsigned int)-1;
3676 if (event
.RightDown())
3678 m_lineBeforeLastClicked
= m_lineLastClicked
;
3679 m_lineLastClicked
= current
;
3681 // If the item is already selected, do not update the selection.
3682 // Multi-selections should not be cleared if a selected item is clicked.
3683 if (!IsRowSelected(current
))
3685 SelectAllRows(false);
3686 ChangeCurrentRow(current
);
3687 SelectRow(m_currentRow
,true);
3688 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3691 else if (event
.RightUp())
3694 model
->GetValue( value
, item
, col
->GetModelColumn() );
3695 wxWindow
*parent
= GetParent();
3696 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3698 le
.SetEventObject(parent
);
3699 le
.SetModel(GetOwner()->GetModel());
3701 parent
->GetEventHandler()->ProcessEvent(le
);
3703 else if (event
.MiddleDown())
3707 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3709 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3711 // hoverOverExpander being true tells us that our node must be
3712 // valid and have children.
3713 // So we don't need any extra checks.
3714 if( node
->IsOpen() )
3719 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3721 m_lineBeforeLastClicked
= m_lineLastClicked
;
3722 m_lineLastClicked
= current
;
3724 unsigned int oldCurrentRow
= m_currentRow
;
3725 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3727 bool cmdModifierDown
= event
.CmdDown();
3728 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3730 if ( IsSingleSel() || !IsRowSelected(current
) )
3732 SelectAllRows( false );
3733 ChangeCurrentRow(current
);
3734 SelectRow(m_currentRow
,true);
3735 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3737 else // multi sel & current is highlighted & no mod keys
3739 m_lineSelectSingleOnUp
= current
;
3740 ChangeCurrentRow(current
); // change focus
3743 else // multi sel & either ctrl or shift is down
3745 if (cmdModifierDown
)
3747 ChangeCurrentRow(current
);
3748 ReverseRowSelection(m_currentRow
);
3749 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3751 else if (event
.ShiftDown())
3753 ChangeCurrentRow(current
);
3755 unsigned int lineFrom
= oldCurrentRow
,
3758 if ( lineTo
< lineFrom
)
3761 lineFrom
= m_currentRow
;
3764 SelectRows(lineFrom
, lineTo
, true);
3765 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3767 else // !ctrl, !shift
3769 // test in the enclosing if should make it impossible
3770 wxFAIL_MSG( wxT("how did we get here?") );
3774 if (m_currentRow
!= oldCurrentRow
)
3775 RefreshRow( oldCurrentRow
);
3777 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3779 // Update selection here...
3782 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3783 (current
== oldCurrentRow
)) && oldWasSelected
;
3785 // Call LeftClick after everything else as under GTK+
3786 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3788 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3790 // notify cell about click
3791 custom
->PrepareForItem(model
, item
, col
->GetModelColumn());
3793 wxRect
cell_rect( xpos
, GetLineStart( current
),
3794 col
->GetWidth(), GetLineHeight( current
) );
3796 // Report position relative to the cell's custom area, i.e.
3797 // no the entire space as given by the control but the one
3798 // used by the renderer after calculation of alignment etc.
3800 // adjust the rectangle ourselves to account for the alignment
3801 wxRect rectItem
= cell_rect
;
3802 const int align
= custom
->GetAlignment();
3803 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3805 const wxSize size
= custom
->GetSize();
3807 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3809 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3810 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3811 else if ( align
& wxALIGN_RIGHT
)
3812 rectItem
.x
+= cell_rect
.width
- size
.x
;
3813 // else: wxALIGN_LEFT is the default
3816 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3818 if ( align
& wxALIGN_CENTER_VERTICAL
)
3819 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3820 else if ( align
& wxALIGN_BOTTOM
)
3821 rectItem
.y
+= cell_rect
.height
- size
.y
;
3822 // else: wxALIGN_TOP is the default
3826 wxPoint
pos( event
.GetPosition() );
3827 pos
.x
-= rectItem
.x
;
3828 pos
.y
-= rectItem
.y
;
3830 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3832 /* ignore ret */ custom
->LeftClick( pos
, cell_rect
,
3833 model
, item
, col
->GetModelColumn());
3839 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3843 if (HasCurrentRow())
3849 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3853 if (HasCurrentRow())
3859 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3861 if( m_selection
.GetCount() != 1 )
3862 return wxDataViewItem();
3864 return GetItemByRow( m_selection
.Item(0));
3867 //-----------------------------------------------------------------------------
3869 //-----------------------------------------------------------------------------
3871 WX_DEFINE_LIST(wxDataViewColumnList
)
3873 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3874 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3875 EVT_SIZE(wxDataViewCtrl::OnSize
)
3878 wxDataViewCtrl::~wxDataViewCtrl()
3881 GetModel()->RemoveNotifier( m_notifier
);
3884 m_colsBestWidths
.clear();
3887 void wxDataViewCtrl::Init()
3889 m_cols
.DeleteContents(true);
3892 // No sorting column at start
3893 m_sortingColumnIdx
= wxNOT_FOUND
;
3895 m_headerArea
= NULL
;
3898 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3903 const wxValidator
& validator
,
3904 const wxString
& name
)
3906 // if ( (style & wxBORDER_MASK) == 0)
3907 // style |= wxBORDER_SUNKEN;
3911 if (!wxControl::Create( parent
, id
, pos
, size
,
3912 style
| wxScrolledWindowStyle
, validator
, name
))
3915 SetInitialSize(size
);
3918 MacSetClipChildren( true );
3921 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3923 // We use the cursor keys for moving the selection, not scrolling, so call
3924 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3925 // keyboard events forwarded to us from wxListMainWindow.
3926 DisableKeyboardScrolling();
3928 if (HasFlag(wxDV_NO_HEADER
))
3929 m_headerArea
= NULL
;
3931 m_headerArea
= new wxDataViewHeaderWindow(this);
3933 SetTargetWindow( m_clientArea
);
3935 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3937 sizer
->Add( m_headerArea
, 0, wxGROW
);
3938 sizer
->Add( m_clientArea
, 1, wxGROW
);
3944 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3946 return wxBORDER_THEME
;
3950 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3954 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3957 // we need to process arrows ourselves for scrolling
3958 if ( nMsg
== WM_GETDLGCODE
)
3960 rc
|= DLGC_WANTARROWS
;
3968 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3970 wxSize newsize
= size
;
3971 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3972 newsize
.y
-= m_headerArea
->GetSize().y
;
3977 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3979 // We need to override OnSize so that our scrolled
3980 // window a) does call Layout() to use sizers for
3981 // positioning the controls but b) does not query
3982 // the sizer for their size and use that for setting
3983 // the scrollable area as set that ourselves by
3984 // calling SetScrollbar() further down.
3991 void wxDataViewCtrl::SetFocus()
3994 m_clientArea
->SetFocus();
3997 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3999 if (!wxDataViewCtrlBase::AssociateModel( model
))
4002 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4004 model
->AddNotifier( m_notifier
);
4006 m_clientArea
->DestroyTree();
4008 m_clientArea
->BuildTree(model
);
4010 m_clientArea
->UpdateDisplay();
4015 #if wxUSE_DRAG_AND_DROP
4017 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4019 return m_clientArea
->EnableDragSource( format
);
4022 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4024 return m_clientArea
->EnableDropTarget( format
);
4027 #endif // wxUSE_DRAG_AND_DROP
4029 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4031 if (!wxDataViewCtrlBase::AppendColumn(col
))
4034 m_cols
.Append( col
);
4035 m_colsBestWidths
.push_back(0);
4036 OnColumnsCountChanged();
4040 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4042 if (!wxDataViewCtrlBase::PrependColumn(col
))
4045 m_cols
.Insert( col
);
4046 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4047 OnColumnsCountChanged();
4051 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4053 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4056 m_cols
.Insert( pos
, col
);
4057 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4058 OnColumnsCountChanged();
4062 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4065 m_headerArea
->UpdateColumn(idx
);
4067 m_clientArea
->UpdateDisplay();
4070 void wxDataViewCtrl::OnColumnsCountChanged()
4073 m_headerArea
->SetColumnCount(GetColumnCount());
4075 m_clientArea
->UpdateDisplay();
4078 void wxDataViewCtrl::DoSetExpanderColumn()
4080 m_clientArea
->UpdateDisplay();
4083 void wxDataViewCtrl::DoSetIndent()
4085 m_clientArea
->UpdateDisplay();
4088 unsigned int wxDataViewCtrl::GetColumnCount() const
4090 return m_cols
.GetCount();
4093 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4098 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4100 // columns can't be reordered if there is no header window which allows
4102 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4105 return GetColumn(idx
);
4108 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4110 const unsigned count
= m_cols
.size();
4111 for ( unsigned n
= 0; n
< count
; n
++ )
4113 if ( m_cols
[n
] == column
)
4120 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4122 if ( m_colsBestWidths
[idx
] != 0 )
4123 return m_colsBestWidths
[idx
];
4125 const int count
= m_clientArea
->GetRowCount();
4126 wxDataViewColumn
*column
= GetColumn(idx
);
4127 wxDataViewRenderer
*renderer
=
4128 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4130 class MaxWidthCalculator
4133 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4134 wxDataViewRenderer
*renderer
,
4135 const wxDataViewModel
*model
,
4138 m_clientArea(clientArea
),
4139 m_renderer(renderer
),
4145 void UpdateWithWidth(int width
)
4147 m_width
= wxMax(m_width
, width
);
4150 void UpdateWithRow(int row
)
4152 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4153 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4154 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4157 int GetMaxWidth() const { return m_width
; }
4161 wxDataViewMainWindow
*m_clientArea
;
4162 wxDataViewRenderer
*m_renderer
;
4163 const wxDataViewModel
*m_model
;
4167 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4168 GetModel(), column
->GetModelColumn());
4172 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4173 // Labels on native MSW header are indented on both sides
4175 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4176 calculator
.UpdateWithWidth(header_width
);
4179 // The code below deserves some explanation. For very large controls, we
4180 // simply can't afford to calculate sizes for all items, it takes too
4181 // long. So the best we can do is to check the first and the last N/2
4182 // items in the control for some sufficiently large N and calculate best
4183 // sizes from that. That can result in the calculated best width being too
4184 // small for some outliers, but it's better to get slightly imperfect
4185 // result than to wait several seconds after every update. To avoid highly
4186 // visible miscalculations, we also include all currently visible items
4187 // no matter what. Finally, the value of N is determined dynamically by
4188 // measuring how much time we spent on the determining item widths so far.
4191 int top_part_end
= count
;
4192 static const long CALC_TIMEOUT
= 20/*ms*/;
4193 // don't call wxStopWatch::Time() too often
4194 static const unsigned CALC_CHECK_FREQ
= 100;
4197 // use some hard-coded limit, that's the best we can do without timer
4198 int top_part_end
= wxMin(500, count
);
4199 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4203 for ( row
= 0; row
< top_part_end
; row
++ )
4206 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4207 timer
.Time() > CALC_TIMEOUT
)
4209 #endif // wxUSE_STOPWATCH
4210 calculator
.UpdateWithRow(row
);
4213 // row is the first unmeasured item now; that's out value of N/2
4219 // add bottom N/2 items now:
4220 const int bottom_part_start
= wxMax(row
, count
- row
);
4221 for ( row
= bottom_part_start
; row
< count
; row
++ )
4223 calculator
.UpdateWithRow(row
);
4226 // finally, include currently visible items in the calculation:
4227 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4228 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4229 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4231 first_visible
= wxMax(first_visible
, top_part_end
);
4232 last_visible
= wxMin(bottom_part_start
, last_visible
);
4234 for ( row
= first_visible
; row
< last_visible
; row
++ )
4236 calculator
.UpdateWithRow(row
);
4239 wxLogTrace("dataview",
4240 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4242 count
- bottom_part_start
,
4243 wxMax(0, last_visible
- first_visible
),
4247 int max_width
= calculator
.GetMaxWidth();
4248 if ( max_width
> 0 )
4249 max_width
+= 2 * PADDING_RIGHTLEFT
;
4251 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4257 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4258 unsigned int WXUNUSED(new_pos
))
4260 // do _not_ reorder m_cols elements here, they should always be in the
4261 // order in which columns were added, we only display the columns in
4263 m_clientArea
->UpdateDisplay();
4266 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4268 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4272 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4274 OnColumnsCountChanged();
4279 bool wxDataViewCtrl::ClearColumns()
4282 m_colsBestWidths
.clear();
4283 OnColumnsCountChanged();
4287 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4289 m_colsBestWidths
[idx
] = 0;
4292 m_headerArea
->UpdateColumn(idx
);
4295 void wxDataViewCtrl::InvalidateColBestWidths()
4297 m_colsBestWidths
.clear();
4298 m_colsBestWidths
.resize(m_cols
.size());
4302 // this updates visual appearance of columns 0 and up, not just 0
4303 m_headerArea
->UpdateColumn(0);
4307 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4310 unsigned int len
= GetColumnCount();
4311 for ( unsigned int i
= 0; i
< len
; i
++ )
4313 wxDataViewColumn
* col
= GetColumnAt(i
);
4320 // This returns the position in pixels which is not what we want.
4323 unsigned int len
= GetColumnCount();
4324 for ( unsigned int i
= 0; i
< len
; i
++ )
4326 wxDataViewColumn
* col
= GetColumnAt(i
);
4327 if (col
->IsHidden())
4329 ret
+= col
->GetWidth();
4332 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4340 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4342 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4343 : GetColumn(m_sortingColumnIdx
);
4346 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4348 return GetItemByRow(m_clientArea
->GetCurrentRow());
4351 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4353 const int row
= m_clientArea
->GetRowByItem(item
);
4355 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4356 if ( static_cast<unsigned>(row
) != oldCurrent
)
4358 m_clientArea
->ChangeCurrentRow(row
);
4359 m_clientArea
->RefreshRow(oldCurrent
);
4360 m_clientArea
->RefreshRow(row
);
4364 // Selection code with wxDataViewItem as parameters
4365 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4367 return m_clientArea
->GetSelection();
4370 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4373 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4374 int len
= selection
.GetCount();
4375 for( int i
= 0; i
< len
; i
++)
4377 unsigned int row
= selection
[i
];
4378 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4383 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4385 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4387 wxDataViewItem last_parent
;
4389 int len
= sel
.GetCount();
4390 for( int i
= 0; i
< len
; i
++ )
4392 wxDataViewItem item
= sel
[i
];
4393 wxDataViewItem parent
= GetModel()->GetParent( item
);
4396 if (parent
!= last_parent
)
4397 ExpandAncestors(item
);
4400 last_parent
= parent
;
4401 int row
= m_clientArea
->GetRowByItem( item
);
4403 selection
.Add( static_cast<unsigned int>(row
) );
4406 m_clientArea
->SetSelections( selection
);
4409 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4411 ExpandAncestors( item
);
4413 int row
= m_clientArea
->GetRowByItem( item
);
4416 // Unselect all rows before select another in the single select mode
4417 if (m_clientArea
->IsSingleSel())
4418 m_clientArea
->SelectAllRows(false);
4420 m_clientArea
->SelectRow(row
, true);
4422 // Also set focus to the selected item
4423 m_clientArea
->ChangeCurrentRow( row
);
4427 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4429 int row
= m_clientArea
->GetRowByItem( item
);
4431 m_clientArea
->SelectRow(row
, false);
4434 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4436 int row
= m_clientArea
->GetRowByItem( item
);
4439 return m_clientArea
->IsRowSelected(row
);
4444 // Selection code with row number as parameter
4445 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4448 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4449 int len
= selection
.GetCount();
4450 for( int i
= 0; i
< len
; i
++)
4452 unsigned int row
= selection
[i
];
4458 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4460 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4461 int len
= sel
.GetCount();
4462 for( int i
= 0; i
< len
; i
++ )
4466 selection
.Add( static_cast<unsigned int>(row
) );
4468 m_clientArea
->SetSelections( selection
);
4471 void wxDataViewCtrl::Select( int row
)
4475 if (m_clientArea
->IsSingleSel())
4476 m_clientArea
->SelectAllRows(false);
4477 m_clientArea
->SelectRow( row
, true );
4481 void wxDataViewCtrl::Unselect( int row
)
4484 m_clientArea
->SelectRow(row
, false);
4487 bool wxDataViewCtrl::IsSelected( int row
) const
4490 return m_clientArea
->IsRowSelected(row
);
4494 void wxDataViewCtrl::SelectRange( int from
, int to
)
4497 for( int i
= from
; i
< to
; i
++ )
4499 m_clientArea
->Select(sel
);
4502 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4504 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4505 for( int i
= from
; i
< to
; i
++ )
4506 if( sel
.Index( i
) != wxNOT_FOUND
)
4508 m_clientArea
->SetSelections(sel
);
4511 void wxDataViewCtrl::SelectAll()
4513 m_clientArea
->SelectAllRows(true);
4516 void wxDataViewCtrl::UnselectAll()
4518 m_clientArea
->SelectAllRows(false);
4521 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4525 if( row
> (int) m_clientArea
->GetRowCount() )
4526 row
= m_clientArea
->GetRowCount();
4528 int first
= m_clientArea
->GetFirstVisibleRow();
4529 int last
= m_clientArea
->GetLastVisibleRow();
4531 m_clientArea
->ScrollTo( row
, column
);
4532 else if( row
> last
)
4533 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4535 m_clientArea
->ScrollTo( first
, column
);
4538 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4540 ExpandAncestors( item
);
4542 m_clientArea
->RecalculateDisplay();
4544 int row
= m_clientArea
->GetRowByItem(item
);
4547 if( column
== NULL
)
4548 EnsureVisible(row
, -1);
4550 EnsureVisible( row
, GetColumnIndex(column
) );
4555 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4556 wxDataViewColumn
* &column
) const
4558 m_clientArea
->HitTest(point
, item
, column
);
4561 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4562 const wxDataViewColumn
* column
) const
4564 return m_clientArea
->GetItemRect(item
, column
);
4567 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4569 return m_clientArea
->GetItemByRow( row
);
4572 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4574 return m_clientArea
->GetRowByItem( item
);
4577 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4579 ExpandAncestors( item
);
4581 int row
= m_clientArea
->GetRowByItem( item
);
4583 m_clientArea
->Expand(row
);
4586 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4588 int row
= m_clientArea
->GetRowByItem( item
);
4590 m_clientArea
->Collapse(row
);
4593 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4595 int row
= m_clientArea
->GetRowByItem( item
);
4597 return m_clientArea
->IsExpanded(row
);
4601 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4603 wxDataViewColumn
* col
= GetColumn( column
);
4607 wxRect itemRect
= GetItemRect(item
, col
);
4608 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4609 renderer
->StartEditing(item
, itemRect
);
4612 #endif // !wxUSE_GENERICDATAVIEWCTRL
4614 #endif // wxUSE_DATAVIEWCTRL