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"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
47 #include "wx/imaglist.h"
48 #include "wx/headerctrl.h"
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
56 static const int SCROLL_UNIT_X
= 15;
58 // the cell padding on the left/right
59 static const int PADDING_RIGHTLEFT
= 3;
61 // the expander space margin
62 static const int EXPANDER_MARGIN
= 4;
65 static const int EXPANDER_OFFSET
= 4;
67 static const int EXPANDER_OFFSET
= 1;
70 //Below is the compare stuff
71 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
72 static wxDataViewModel
* g_model
;
73 static int g_column
= -2;
74 static bool g_asending
= true;
76 //-----------------------------------------------------------------------------
77 // wxDataViewHeaderWindow
78 //-----------------------------------------------------------------------------
80 class wxDataViewHeaderWindow
: public wxHeaderCtrl
83 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
84 : wxHeaderCtrl(parent
)
88 wxDataViewCtrl
*GetOwner() const
89 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
92 // implement/override wxHeaderCtrl functions by forwarding them to the main
94 virtual wxHeaderColumnBase
& GetColumn(unsigned int idx
)
96 return *(GetOwner()->GetColumn(idx
));
99 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
101 wxDataViewCtrl
* const owner
= GetOwner();
103 int widthContents
= owner
->GetBestColumnWidth(idx
);
104 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
105 owner
->OnColumnChange(idx
);
111 bool SendEvent(wxEventType type
, unsigned int n
)
113 wxDataViewCtrl
* const owner
= GetOwner();
114 wxDataViewEvent
event(type
, owner
->GetId());
116 event
.SetEventObject(owner
);
118 event
.SetDataViewColumn(owner
->GetColumn(n
));
119 event
.SetModel(owner
->GetModel());
121 // for events created by wxDataViewHeaderWindow the
122 // row / value fields are not valid
123 return owner
->GetEventHandler()->ProcessEvent(event
);
126 void OnClick(wxHeaderCtrlEvent
& event
)
128 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
,
133 void OnRClick(wxHeaderCtrlEvent
& event
)
135 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
140 void OnBeginDrag(wxHeaderCtrlEvent
& event
)
142 if ( !GetColumn(event
.GetColumn()).IsResizeable() )
146 void OnDragging(wxHeaderCtrlEvent
& event
)
148 const wxHeaderColumnBase
& col
= GetColumn(event
.GetColumn());
150 const int minWidth
= col
.GetMinWidth();
151 if ( event
.GetWidth() < minWidth
)
155 void OnEndDrag(wxHeaderCtrlEvent
& event
)
157 const unsigned col
= event
.GetColumn();
158 GetColumn(col
).SetWidth(event
.GetWidth());
159 GetOwner()->OnColumnChange(col
);
162 DECLARE_EVENT_TABLE()
163 DECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
)
166 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
167 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
168 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
170 EVT_HEADER_BEGIN_DRAG(wxID_ANY
, wxDataViewHeaderWindow::OnBeginDrag
)
171 EVT_HEADER_DRAGGING(wxID_ANY
, wxDataViewHeaderWindow::OnDragging
)
172 EVT_HEADER_END_DRAG(wxID_ANY
, wxDataViewHeaderWindow::OnEndDrag
)
175 //-----------------------------------------------------------------------------
176 // wxDataViewRenameTimer
177 //-----------------------------------------------------------------------------
179 class wxDataViewRenameTimer
: public wxTimer
182 wxDataViewMainWindow
*m_owner
;
185 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
189 //-----------------------------------------------------------------------------
190 // wxDataViewTreeNode
191 //-----------------------------------------------------------------------------
192 class wxDataViewTreeNode
;
193 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
194 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
196 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
197 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
199 class wxDataViewTreeNode
202 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
209 m_hasChildren
= false;
213 ~wxDataViewTreeNode()
217 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
218 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
219 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
220 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
222 void AddNode( wxDataViewTreeNode
* node
)
224 m_leaves
.Add( node
->GetItem().GetID() );
226 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
229 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
231 void AddLeaf( void * leaf
)
233 m_leaves
.Add( leaf
);
235 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
238 wxDataViewItem
& GetItem() { return m_item
; }
239 const wxDataViewItem
& GetItem() const { return m_item
; }
240 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
242 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
243 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
244 int GetIndentLevel() const
247 const wxDataViewTreeNode
* node
= this;
248 while( node
->GetParent()->GetParent() != NULL
)
250 node
= node
->GetParent();
263 int len
= m_nodes
.GetCount();
265 for ( int i
= 0;i
< len
; i
++)
266 sum
+= m_nodes
[i
]->GetSubTreeCount();
268 sum
+= m_leaves
.GetCount();
271 ChangeSubTreeCount(-sum
);
277 ChangeSubTreeCount(sum
);
280 bool HasChildren() const { return m_hasChildren
; }
281 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
283 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
284 int GetSubTreeCount() const { return m_subTreeCount
; }
285 void ChangeSubTreeCount( int num
)
289 m_subTreeCount
+= num
;
291 m_parent
->ChangeSubTreeCount(num
);
298 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
299 int len
= m_nodes
.GetCount();
300 for (int i
= 0; i
< len
; i
++)
301 m_nodes
[i
]->Resort();
302 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
307 wxDataViewTreeNode
*m_parent
;
308 wxDataViewTreeNodes m_nodes
;
309 wxDataViewTreeLeaves m_leaves
;
310 wxDataViewItem m_item
;
316 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
318 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
321 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
323 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
328 //-----------------------------------------------------------------------------
329 // wxDataViewMainWindow
330 //-----------------------------------------------------------------------------
332 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
334 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
335 WX_DEFINE_LIST(ItemList
)
337 class wxDataViewMainWindow
: public wxWindow
340 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
342 const wxPoint
&pos
= wxDefaultPosition
,
343 const wxSize
&size
= wxDefaultSize
,
344 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
345 virtual ~wxDataViewMainWindow();
347 bool IsVirtualList() const { return m_root
== NULL
; }
349 // notifications from wxDataViewModel
350 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
351 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
352 bool ItemChanged( const wxDataViewItem
&item
);
353 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
357 if (!IsVirtualList())
367 g_model
= GetOwner()->GetModel();
368 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
371 if (g_model
->HasDefaultCompare())
379 g_column
= col
->GetModelColumn();
380 g_asending
= col
->IsSortOrderAscending();
383 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
384 wxDataViewCtrl
*GetOwner() { return m_owner
; }
385 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
387 void OnPaint( wxPaintEvent
&event
);
388 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
389 void OnChar( wxKeyEvent
&event
);
390 void OnMouse( wxMouseEvent
&event
);
391 void OnSetFocus( wxFocusEvent
&event
);
392 void OnKillFocus( wxFocusEvent
&event
);
394 void UpdateDisplay();
395 void RecalculateDisplay();
396 void OnInternalIdle();
398 void OnRenameTimer();
400 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
401 void ScrollTo( int rows
, int column
);
403 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
404 void ChangeCurrentRow( unsigned int row
);
406 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
407 bool IsEmpty() { return GetRowCount() == 0; }
409 int GetCountPerPage() const;
410 int GetEndOfLastCol() const;
411 unsigned int GetFirstVisibleRow() const;
412 //I change this method to un const because in the tree view, the displaying number of the tree are changing along with the expanding/collapsing of the tree nodes
413 unsigned int GetLastVisibleRow();
414 unsigned int GetRowCount();
416 wxDataViewItem
GetSelection() const;
417 wxDataViewSelection
GetSelections(){ return m_selection
; }
418 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
419 void Select( const wxArrayInt
& aSelections
);
420 void SelectAllRows( bool on
);
421 void SelectRow( unsigned int row
, bool on
);
422 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
423 void ReverseRowSelection( unsigned int row
);
424 bool IsRowSelected( unsigned int row
);
425 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
427 void RefreshRow( unsigned int row
);
428 void RefreshRows( unsigned int from
, unsigned int to
);
429 void RefreshRowsAfter( unsigned int firstRow
);
431 // returns the colour to be used for drawing the rules
432 wxColour
GetRuleColour() const
434 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
437 wxRect
GetLineRect( unsigned int row
) const;
439 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
440 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
441 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
443 //Some useful functions for row and item mapping
444 wxDataViewItem
GetItemByRow( unsigned int row
) const;
445 int GetRowByItem( const wxDataViewItem
& item
) const;
447 //Methods for building the mapping tree
448 void BuildTree( wxDataViewModel
* model
);
450 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
451 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
453 void Expand( unsigned int row
) { OnExpanding( row
); }
454 void Collapse( unsigned int row
) { OnCollapsing( row
); }
456 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
457 //We did not need this temporarily
458 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
460 int RecalculateCount();
462 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
463 void OnExpanding( unsigned int row
);
464 void OnCollapsing( unsigned int row
);
466 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
469 wxDataViewCtrl
*m_owner
;
473 wxDataViewColumn
*m_currentCol
;
474 unsigned int m_currentRow
;
475 wxDataViewSelection m_selection
;
477 wxDataViewRenameTimer
*m_renameTimer
;
485 // for double click logic
486 unsigned int m_lineLastClicked
,
487 m_lineBeforeLastClicked
,
488 m_lineSelectSingleOnUp
;
490 // the pen used to draw horiz/vertical rules
493 // the pen used to draw the expander and the lines
496 //This is the tree structure of the model
497 wxDataViewTreeNode
* m_root
;
499 //This is the tree node under the cursor
500 wxDataViewTreeNode
* m_underMouse
;
502 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
503 DECLARE_EVENT_TABLE()
506 // ---------------------------------------------------------
507 // wxGenericDataViewModelNotifier
508 // ---------------------------------------------------------
510 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
513 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
514 { m_mainWindow
= mainWindow
; }
516 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
517 { return m_mainWindow
->ItemAdded( parent
, item
); }
518 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
519 { return m_mainWindow
->ItemDeleted( parent
, item
); }
520 virtual bool ItemChanged( const wxDataViewItem
& item
)
521 { return m_mainWindow
->ItemChanged(item
); }
522 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
523 { return m_mainWindow
->ValueChanged( item
, col
); }
524 virtual bool Cleared()
525 { return m_mainWindow
->Cleared(); }
526 virtual void Resort()
527 { m_mainWindow
->Resort(); }
529 wxDataViewMainWindow
*m_mainWindow
;
532 // ---------------------------------------------------------
533 // wxDataViewRenderer
534 // ---------------------------------------------------------
536 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
538 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
539 wxDataViewCellMode mode
,
541 wxDataViewRendererBase( varianttype
, mode
, align
)
550 wxDataViewRenderer::~wxDataViewRenderer()
556 wxDC
*wxDataViewRenderer::GetDC()
560 if (GetOwner() == NULL
)
562 if (GetOwner()->GetOwner() == NULL
)
564 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
570 void wxDataViewRenderer::SetAlignment( int align
)
575 int wxDataViewRenderer::GetAlignment() const
580 int wxDataViewRenderer::CalculateAlignment() const
582 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
584 if (GetOwner() == NULL
)
585 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
587 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
593 // ---------------------------------------------------------
594 // wxDataViewCustomRenderer
595 // ---------------------------------------------------------
597 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
599 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
600 wxDataViewCellMode mode
, int align
) :
601 wxDataViewRenderer( varianttype
, mode
, align
)
605 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
607 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
608 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
609 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
610 view
->GetForegroundColour();
611 dc
->SetTextForeground(col
);
612 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
615 // ---------------------------------------------------------
616 // wxDataViewTextRenderer
617 // ---------------------------------------------------------
619 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
621 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
622 wxDataViewCellMode mode
, int align
) :
623 wxDataViewCustomRenderer( varianttype
, mode
, align
)
627 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
629 m_text
= value
.GetString();
634 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
639 bool wxDataViewTextRenderer::HasEditorCtrl()
644 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
645 wxRect labelRect
, const wxVariant
&value
)
647 return new wxTextCtrl( parent
, wxID_ANY
, value
,
648 wxPoint(labelRect
.x
,labelRect
.y
),
649 wxSize(labelRect
.width
,labelRect
.height
) );
652 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
654 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
655 value
= text
->GetValue();
659 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
661 RenderText( m_text
, 0, cell
, dc
, state
);
665 wxSize
wxDataViewTextRenderer::GetSize() const
667 const wxDataViewCtrl
*view
= GetView();
671 view
->GetTextExtent( m_text
, &x
, &y
);
672 return wxSize( x
, y
);
674 return wxSize(80,20);
677 // ---------------------------------------------------------
678 // wxDataViewTextRendererAttr
679 // ---------------------------------------------------------
681 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
683 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
684 wxDataViewCellMode mode
, int align
) :
685 wxDataViewTextRenderer( varianttype
, mode
, align
)
690 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
697 if (m_attr
.HasColour())
699 colour
= dc
->GetTextForeground();
700 dc
->SetTextForeground( m_attr
.GetColour() );
703 if (m_attr
.GetBold() || m_attr
.GetItalic())
705 font
= dc
->GetFont();
706 wxFont myfont
= font
;
707 if (m_attr
.GetBold())
708 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
709 if (m_attr
.GetItalic())
710 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
711 dc
->SetFont( myfont
);
715 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
720 if (m_attr
.HasColour())
721 dc
->SetTextForeground( colour
);
723 if (m_attr
.GetBold() || m_attr
.GetItalic())
731 // ---------------------------------------------------------
732 // wxDataViewBitmapRenderer
733 // ---------------------------------------------------------
735 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
737 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
738 wxDataViewCellMode mode
, int align
) :
739 wxDataViewCustomRenderer( varianttype
, mode
, align
)
743 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
745 if (value
.GetType() == wxT("wxBitmap"))
747 if (value
.GetType() == wxT("wxIcon"))
753 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
758 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
761 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
762 else if (m_icon
.Ok())
763 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
768 wxSize
wxDataViewBitmapRenderer::GetSize() const
771 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
772 else if (m_icon
.Ok())
773 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
775 return wxSize(16,16);
778 // ---------------------------------------------------------
779 // wxDataViewToggleRenderer
780 // ---------------------------------------------------------
782 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
784 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
785 wxDataViewCellMode mode
, int align
) :
786 wxDataViewCustomRenderer( varianttype
, mode
, align
)
791 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
793 m_toggle
= value
.GetBool();
798 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
803 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
805 // User wxRenderer here
808 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
810 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
815 flags
|= wxCONTROL_CHECKED
;
816 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
817 flags
|= wxCONTROL_DISABLED
;
819 wxRendererNative::Get().DrawCheckBox(
820 GetOwner()->GetOwner(),
828 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
829 wxDataViewModel
*model
,
830 const wxDataViewItem
& item
, unsigned int col
)
832 bool value
= !m_toggle
;
833 wxVariant variant
= value
;
834 model
->SetValue( variant
, item
, col
);
835 model
->ValueChanged( item
, col
);
839 wxSize
wxDataViewToggleRenderer::GetSize() const
841 return wxSize(20,20);
844 // ---------------------------------------------------------
845 // wxDataViewProgressRenderer
846 // ---------------------------------------------------------
848 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
850 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
851 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
852 wxDataViewCustomRenderer( varianttype
, mode
, align
)
858 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
862 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
864 m_value
= (long) value
;
866 if (m_value
< 0) m_value
= 0;
867 if (m_value
> 100) m_value
= 100;
872 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
874 value
= (long) m_value
;
878 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
880 double pct
= (double)m_value
/ 100.0;
882 bar
.width
= (int)(cell
.width
* pct
);
883 dc
->SetPen( *wxTRANSPARENT_PEN
);
884 dc
->SetBrush( *wxBLUE_BRUSH
);
885 dc
->DrawRectangle( bar
);
887 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
888 dc
->SetPen( *wxBLACK_PEN
);
889 dc
->DrawRectangle( cell
);
894 wxSize
wxDataViewProgressRenderer::GetSize() const
896 return wxSize(40,12);
899 // ---------------------------------------------------------
900 // wxDataViewDateRenderer
901 // ---------------------------------------------------------
903 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
905 #if wxUSE_DATE_RENDERER_POPUP
907 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
910 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
911 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
912 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
917 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
918 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
919 sizer
->Add( m_cal
, 1, wxGROW
);
924 void OnCalendar( wxCalendarEvent
&event
);
926 wxCalendarCtrl
*m_cal
;
927 wxDataViewModel
*m_model
;
929 const wxDataViewItem
& m_item
;
932 virtual void OnDismiss()
937 DECLARE_EVENT_TABLE()
940 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
941 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
944 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
946 wxDateTime date
= event
.GetDate();
947 wxVariant value
= date
;
948 m_model
->SetValue( value
, m_item
, m_col
);
949 m_model
->ValueChanged( m_item
, m_col
);
953 #endif // wxUSE_DATE_RENDERER_POPUP
955 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
957 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
958 wxDataViewCellMode mode
, int align
) :
959 wxDataViewCustomRenderer( varianttype
, mode
, align
)
963 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
965 m_date
= value
.GetDateTime();
970 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
976 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
978 wxString tmp
= m_date
.FormatDate();
979 RenderText( tmp
, 0, cell
, dc
, state
);
983 wxSize
wxDataViewDateRenderer::GetSize() const
985 const wxDataViewCtrl
* view
= GetView();
986 wxString tmp
= m_date
.FormatDate();
988 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
989 return wxSize(x
,y
+d
);
992 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
993 const wxDataViewItem
& item
, unsigned int col
)
996 model
->GetValue( variant
, item
, col
);
997 wxDateTime value
= variant
.GetDateTime();
999 #if wxUSE_DATE_RENDERER_POPUP
1000 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1001 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1002 wxPoint pos
= wxGetMousePosition();
1005 popup
->Popup( popup
->m_cal
);
1006 #else // !wxUSE_DATE_RENDERER_POPUP
1007 wxMessageBox(value
.Format());
1008 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1012 // ---------------------------------------------------------
1013 // wxDataViewIconTextRenderer
1014 // ---------------------------------------------------------
1016 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1018 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1019 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1020 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1023 SetAlignment(align
);
1026 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1030 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1036 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1041 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1044 const wxIcon
&icon
= m_value
.GetIcon();
1047 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1048 xoffset
= icon
.GetWidth()+4;
1051 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1056 wxSize
wxDataViewIconTextRenderer::GetSize() const
1058 const wxDataViewCtrl
*view
= GetView();
1059 if (!m_value
.GetText().empty())
1062 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1064 if (m_value
.GetIcon().IsOk())
1065 x
+= m_value
.GetIcon().GetWidth() + 4;
1066 return wxSize( x
, y
);
1068 return wxSize(80,20);
1072 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1073 wxRect
WXUNUSED(labelRect
),
1074 const wxVariant
& WXUNUSED(value
))
1080 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1081 wxVariant
& WXUNUSED(value
))
1086 //-----------------------------------------------------------------------------
1087 // wxDataViewRenameTimer
1088 //-----------------------------------------------------------------------------
1090 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1095 void wxDataViewRenameTimer::Notify()
1097 m_owner
->OnRenameTimer();
1100 //-----------------------------------------------------------------------------
1101 // wxDataViewMainWindow
1102 //-----------------------------------------------------------------------------
1104 //The tree building helper, declared firstly
1105 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1107 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1109 if (row1
> row2
) return 1;
1110 if (row1
== row2
) return 0;
1115 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1117 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1118 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1119 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1120 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1121 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1122 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1125 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1126 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1127 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1128 m_selection( wxDataViewSelectionCmp
)
1133 m_lastOnSame
= false;
1134 m_renameTimer
= new wxDataViewRenameTimer( this );
1136 // TODO: user better initial values/nothing selected
1137 m_currentCol
= NULL
;
1140 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1143 m_dragStart
= wxPoint(0,0);
1144 m_lineLastClicked
= (unsigned int) -1;
1145 m_lineBeforeLastClicked
= (unsigned int) -1;
1146 m_lineSelectSingleOnUp
= (unsigned int) -1;
1150 SetBackgroundColour( *wxWHITE
);
1152 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1154 m_penRule
= wxPen(GetRuleColour());
1156 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1157 m_penExpander
= wxPen(wxColour(0,0,0));
1158 //Some new added code to deal with the tree structure
1159 m_root
= new wxDataViewTreeNode( NULL
);
1160 m_root
->SetHasChildren(true);
1162 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1164 m_underMouse
= NULL
;
1168 wxDataViewMainWindow::~wxDataViewMainWindow()
1171 delete m_renameTimer
;
1174 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1176 wxDataViewModel
*model
= GetOwner()->GetModel();
1177 wxAutoBufferedPaintDC
dc( this );
1180 dc
.SetPen( *wxTRANSPARENT_PEN
);
1181 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1182 dc
.SetBrush( *wxWHITE_BRUSH
);
1183 wxSize
size( GetClientSize() );
1184 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1188 GetOwner()->PrepareDC( dc
);
1189 dc
.SetFont( GetFont() );
1191 wxRect update
= GetUpdateRegion().GetBox();
1192 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1194 // compute which items needs to be redrawn
1195 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1196 unsigned int item_count
=
1197 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1198 (int)(GetRowCount( ) - item_start
));
1199 unsigned int item_last
= item_start
+ item_count
;
1201 // compute which columns needs to be redrawn
1202 unsigned int cols
= GetOwner()->GetColumnCount();
1203 unsigned int col_start
= 0;
1204 unsigned int x_start
;
1205 for (x_start
= 0; col_start
< cols
; col_start
++)
1207 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
1208 if (col
->IsHidden())
1209 continue; // skip it!
1211 unsigned int w
= col
->GetWidth();
1212 if (x_start
+w
>= (unsigned int)update
.x
)
1218 unsigned int col_last
= col_start
;
1219 unsigned int x_last
= x_start
;
1220 for (; col_last
< cols
; col_last
++)
1222 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
1223 if (col
->IsHidden())
1224 continue; // skip it!
1226 if (x_last
> (unsigned int)update
.GetRight())
1229 x_last
+= col
->GetWidth();
1232 // Draw horizontal rules if required
1233 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1235 dc
.SetPen(m_penRule
);
1236 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1238 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1240 int y
= GetLineStart( i
);
1241 dc
.DrawLine(x_start
, y
, x_last
, y
);
1245 // Draw vertical rules if required
1246 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1248 dc
.SetPen(m_penRule
);
1249 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1252 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1254 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
1255 if (col
->IsHidden())
1256 continue; // skip it
1258 dc
.DrawLine(x
, GetLineStart( item_start
),
1259 x
, GetLineStart( item_last
) );
1261 x
+= col
->GetWidth();
1264 // Draw last vertical rule
1265 dc
.DrawLine(x
, GetLineStart( item_start
),
1266 x
, GetLineStart( item_last
) );
1269 // redraw the background for the items which are selected/current
1270 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1272 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1273 if (selected
|| item
== m_currentRow
)
1275 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1276 if (item
== m_currentRow
)
1277 flags
|= wxCONTROL_CURRENT
;
1279 flags
|= wxCONTROL_FOCUSED
;
1281 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1282 wxRendererNative::Get().DrawItemSelectionRect
1292 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1295 // TODO: last column for RTL support
1296 expander
= GetOwner()->GetColumn( 0 );
1297 GetOwner()->SetExpanderColumn(expander
);
1300 // redraw all cells for all rows which must be repainted and all columns
1302 cell_rect
.x
= x_start
;
1303 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1306 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1307 wxDataViewRenderer
*cell
= col
->GetRenderer();
1308 cell_rect
.width
= col
->GetWidth();
1310 if (col
->IsHidden())
1311 continue; // skip it!
1313 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1315 // get the cell value and set it into the renderer
1317 wxDataViewTreeNode
*node
= NULL
;
1318 wxDataViewItem dataitem
;
1320 if (!IsVirtualList())
1322 node
= GetTreeNodeByRow(item
);
1326 dataitem
= node
->GetItem();
1328 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
1333 dataitem
= wxDataViewItem( wxUIntToPtr(item
) );
1336 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1337 cell
->SetValue( value
);
1339 if (cell
->GetWantsAttr())
1341 wxDataViewItemAttr attr
;
1342 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
1344 cell
->SetAttr( attr
);
1345 cell
->SetHasAttr( ret
);
1349 cell_rect
.y
= GetLineStart( item
);
1350 cell_rect
.height
= GetLineHeight( item
); // -1 is for the horizontal rules (?)
1352 //Draw the expander here.
1354 if ((!IsVirtualList()) && (col
== expander
))
1356 indent
= node
->GetIndentLevel();
1358 //Calculate the indent first
1359 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1361 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1362 // change the cell_rect.x to the appropriate pos
1363 int expander_x
= indent
+ EXPANDER_MARGIN
;
1364 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2) - (expander_width
/2) - EXPANDER_OFFSET
;
1365 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1366 dc
.SetPen( m_penExpander
);
1367 dc
.SetBrush( wxNullBrush
);
1368 if( node
->HasChildren() )
1370 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1372 if (m_underMouse
== node
)
1374 flag
|= wxCONTROL_CURRENT
;
1376 if( node
->IsOpen() )
1377 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
1379 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1381 //force the expander column to left-center align
1382 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1384 if (node
&& !node
->HasChildren())
1386 // Yes, if the node does not have any child, it must be a leaf which
1387 // mean that it is a temporarily created by GetTreeNodeByRow
1391 // cannot be bigger than allocated space
1392 wxSize size
= cell
->GetSize();
1393 // Because of the tree structure indent, here we should minus the width of the cell for drawing
1394 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
1395 // size.y = wxMin( size.y, cell_rect.height );
1396 size
.y
= cell_rect
.height
;
1398 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
1399 int align
= cell
->CalculateAlignment();
1401 // horizontal alignment:
1402 item_rect
.x
= cell_rect
.x
;
1403 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1404 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1405 else if (align
& wxALIGN_RIGHT
)
1406 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
1407 //else: wxALIGN_LEFT is the default
1409 // vertical alignment:
1410 item_rect
.y
= cell_rect
.y
;
1411 if (align
& wxALIGN_CENTER_VERTICAL
)
1412 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1413 else if (align
& wxALIGN_BOTTOM
)
1414 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
1415 //else: wxALIGN_TOP is the default
1418 item_rect
.x
+= PADDING_RIGHTLEFT
;
1419 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1421 //Here we add the tree indent
1422 item_rect
.x
+= indent
;
1425 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1426 state
|= wxDATAVIEW_CELL_SELECTED
;
1428 // TODO: it would be much more efficient to create a clipping
1429 // region for the entire column being rendered (in the OnPaint
1430 // of wxDataViewMainWindow) instead of a single clip region for
1431 // each cell. However it would mean that each renderer should
1432 // respect the given wxRect's top & bottom coords, eventually
1433 // violating only the left & right coords - however the user can
1434 // make its own renderer and thus we cannot be sure of that.
1435 dc
.SetClippingRegion( item_rect
);
1436 cell
->Render( item_rect
, &dc
, state
);
1437 dc
.DestroyClippingRegion();
1440 cell_rect
.x
+= cell_rect
.width
;
1444 void wxDataViewMainWindow::OnRenameTimer()
1446 // We have to call this here because changes may just have
1447 // been made and no screen update taken place.
1452 unsigned int cols
= GetOwner()->GetColumnCount();
1454 for (i
= 0; i
< cols
; i
++)
1456 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1458 continue; // skip it!
1460 if (c
== m_currentCol
)
1462 xpos
+= c
->GetWidth();
1464 wxRect
labelRect( xpos
,
1465 GetLineStart( m_currentRow
),
1466 m_currentCol
->GetWidth(),
1467 GetLineHeight( m_currentRow
) );
1469 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1470 &labelRect
.x
, &labelRect
.y
);
1472 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1473 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1477 //------------------------------------------------------------------
1478 // Helper class for do operation on the tree node
1479 //------------------------------------------------------------------
1484 virtual ~DoJob() { }
1486 //The return value control how the tree-walker tranverse the tree
1487 // 0: Job done, stop tranverse and return
1488 // 1: Ignore the current node's subtree and continue
1489 // 2: Job not done, continue
1490 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1491 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1492 virtual int operator() ( void * n
) = 0;
1495 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1500 switch( func( node
) )
1511 wxDataViewTreeNodes nodes
= node
->GetNodes();
1512 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1514 int len_nodes
= nodes
.GetCount();
1515 int len
= leaves
.GetCount();
1516 int i
= 0, nodes_i
= 0;
1518 for(; i
< len
; i
++ )
1520 void * n
= leaves
[i
];
1521 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1523 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1526 if( Walker( nd
, func
) )
1545 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1556 wxDataViewTreeNode
* node
;
1557 node
= FindNode(parent
);
1562 node
->SetHasChildren( true );
1564 if( g_model
->IsContainer( item
) )
1566 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1567 newnode
->SetItem(item
);
1568 newnode
->SetHasChildren( true );
1569 node
->AddNode( newnode
);
1572 node
->AddLeaf( item
.GetID() );
1574 node
->ChangeSubTreeCount(1);
1582 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1584 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1585 const wxDataViewItem
& item
)
1590 if( m_currentRow
> GetRowCount() )
1591 m_currentRow
= m_count
- 1;
1593 m_selection
.Empty();
1600 wxDataViewTreeNode
* node
= FindNode(parent
);
1602 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
1603 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
1606 node
->GetChildren().Remove( item
.GetID() );
1607 //Manuplate selection
1608 if( m_selection
.GetCount() > 1 )
1610 m_selection
.Empty();
1612 bool isContainer
= false;
1613 wxDataViewTreeNodes nds
= node
->GetNodes();
1614 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
1616 if (nds
[i
]->GetItem() == item
)
1624 wxDataViewTreeNode
* n
= NULL
;
1625 wxDataViewTreeNodes nodes
= node
->GetNodes();
1626 int len
= nodes
.GetCount();
1627 for( int i
= 0; i
< len
; i
++)
1629 if( nodes
[i
]->GetItem() == item
)
1636 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
1638 node
->GetNodes().Remove( n
);
1639 sub
-= n
->GetSubTreeCount();
1640 ::DestroyTreeHelper(n
);
1642 //Make the row number invalid and get a new valid one when user call GetRowCount
1644 node
->ChangeSubTreeCount(sub
);
1646 //Change the current row to the last row if the current exceed the max row number
1647 if( m_currentRow
> GetRowCount() )
1648 m_currentRow
= m_count
- 1;
1655 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1661 wxWindow
*parent
= GetParent();
1662 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
1663 le
.SetEventObject(parent
);
1664 le
.SetModel(GetOwner()->GetModel());
1666 parent
->GetEventHandler()->ProcessEvent(le
);
1671 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
1673 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1674 /*#define MAX_VIRTUAL_WIDTH 100000
1676 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1677 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1678 Refresh( true, &rect );
1686 wxWindow
*parent
= GetParent();
1687 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
1688 le
.SetEventObject(parent
);
1689 le
.SetModel(GetOwner()->GetModel());
1692 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
1693 parent
->GetEventHandler()->ProcessEvent(le
);
1698 bool wxDataViewMainWindow::Cleared()
1703 BuildTree( GetOwner()->GetModel() );
1710 void wxDataViewMainWindow::UpdateDisplay()
1715 void wxDataViewMainWindow::OnInternalIdle()
1717 wxWindow::OnInternalIdle();
1721 RecalculateDisplay();
1726 void wxDataViewMainWindow::RecalculateDisplay()
1728 wxDataViewModel
*model
= GetOwner()->GetModel();
1735 int width
= GetEndOfLastCol();
1736 int height
= GetLineStart( GetRowCount() );
1738 SetVirtualSize( width
, height
);
1739 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1744 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1746 wxWindow::ScrollWindow( dx
, dy
, rect
);
1748 if (GetOwner()->m_headerArea
)
1749 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1752 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
1755 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
1756 int sy
= GetLineStart( rows
)/y
;
1760 wxRect rect
= GetClientRect();
1764 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
1765 for (x_start
= 0; colnum
< column
; colnum
++)
1767 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
1768 if (col
->IsHidden())
1769 continue; // skip it!
1771 w
= col
->GetWidth();
1775 int x_end
= x_start
+ w
;
1776 xe
= xx
+ rect
.width
;
1779 sx
= ( xx
+ x_end
- xe
)/x
;
1786 m_owner
->Scroll( sx
, sy
);
1789 int wxDataViewMainWindow::GetCountPerPage() const
1791 wxSize size
= GetClientSize();
1792 return size
.y
/ m_lineHeight
;
1795 int wxDataViewMainWindow::GetEndOfLastCol() const
1799 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
1801 const wxDataViewColumn
*c
=
1802 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumn( i
);
1805 width
+= c
->GetWidth();
1810 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
1814 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
1816 return GetLineAt( y
);
1819 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
1821 wxSize client_size
= GetClientSize();
1822 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
1823 &client_size
.x
, &client_size
.y
);
1825 //we should deal with the pixel here
1826 unsigned int row
= GetLineAt(client_size
.y
) - 1;
1828 return wxMin( GetRowCount()-1, row
);
1831 unsigned int wxDataViewMainWindow::GetRowCount()
1833 if ( m_count
== -1 )
1835 m_count
= RecalculateCount();
1841 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
1848 void wxDataViewMainWindow::SelectAllRows( bool on
)
1855 m_selection
.Clear();
1856 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
1857 m_selection
.Add( i
);
1862 unsigned int first_visible
= GetFirstVisibleRow();
1863 unsigned int last_visible
= GetLastVisibleRow();
1865 for (i
= 0; i
< m_selection
.GetCount(); i
++)
1867 unsigned int row
= m_selection
[i
];
1868 if ((row
>= first_visible
) && (row
<= last_visible
))
1871 m_selection
.Clear();
1875 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
1877 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1881 m_selection
.Add( row
);
1889 m_selection
.Remove( row
);
1895 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
1899 unsigned int tmp
= from
;
1905 for (i
= from
; i
<= to
; i
++)
1907 if (m_selection
.Index( i
) == wxNOT_FOUND
)
1910 m_selection
.Add( i
);
1915 m_selection
.Remove( i
);
1918 RefreshRows( from
, to
);
1921 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
1923 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
1925 int n
= aSelections
[i
];
1927 m_selection
.Add( n
);
1932 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
1934 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1935 m_selection
.Add( row
);
1937 m_selection
.Remove( row
);
1941 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
1943 return (m_selection
.Index( row
) != wxNOT_FOUND
);
1946 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
1948 wxWindow
*parent
= GetParent();
1949 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
1951 le
.SetEventObject(parent
);
1952 le
.SetModel(GetOwner()->GetModel());
1955 parent
->GetEventHandler()->ProcessEvent(le
);
1958 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
1960 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
1961 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1963 wxSize client_size
= GetClientSize();
1964 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1965 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1966 if (intersect_rect
.width
> 0)
1967 Refresh( true, &intersect_rect
);
1970 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
1974 unsigned int tmp
= to
;
1979 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
1980 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1982 wxSize client_size
= GetClientSize();
1983 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1984 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1985 if (intersect_rect
.width
> 0)
1986 Refresh( true, &intersect_rect
);
1989 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
1991 wxSize client_size
= GetClientSize();
1992 int start
= GetLineStart( firstRow
);
1993 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
1994 if (start
> client_size
.y
) return;
1996 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
1998 Refresh( true, &rect
);
2001 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2003 wxCHECK_RET( newCurrent
< GetRowCount(),
2004 _T("invalid item index in OnArrowChar()") );
2006 // if there is no selection, we cannot move it anywhere
2007 if (!HasCurrentRow())
2010 unsigned int oldCurrent
= m_currentRow
;
2012 // in single selection we just ignore Shift as we can't select several
2014 if ( event
.ShiftDown() && !IsSingleSel() )
2016 RefreshRow( oldCurrent
);
2018 ChangeCurrentRow( newCurrent
);
2020 // select all the items between the old and the new one
2021 if ( oldCurrent
> newCurrent
)
2023 newCurrent
= oldCurrent
;
2024 oldCurrent
= m_currentRow
;
2027 SelectRows( oldCurrent
, newCurrent
, true );
2028 if (oldCurrent
!=newCurrent
)
2029 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2033 RefreshRow( oldCurrent
);
2035 // all previously selected items are unselected unless ctrl is held
2036 if ( !event
.ControlDown() )
2037 SelectAllRows(false);
2039 ChangeCurrentRow( newCurrent
);
2041 if ( !event
.ControlDown() )
2043 SelectRow( m_currentRow
, true );
2044 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2047 RefreshRow( m_currentRow
);
2050 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2053 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2057 rect
.y
= GetLineStart( row
);
2058 rect
.width
= GetEndOfLastCol();
2059 rect
.height
= GetLineHeight( row
);
2064 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2066 const wxDataViewModel
*model
= GetOwner()->GetModel();
2068 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2070 // TODO make more efficient
2075 for (r
= 0; r
< row
; r
++)
2077 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2078 if (!node
) return start
;
2080 wxDataViewItem item
= node
->GetItem();
2082 if (node
&& !node
->HasChildren())
2084 // Yes, if the node does not have any child, it must be a leaf which
2085 // mean that it is a temporarily created by GetTreeNodeByRow
2089 unsigned int cols
= GetOwner()->GetColumnCount();
2091 int height
= m_lineHeight
;
2092 for (col
= 0; col
< cols
; col
++)
2094 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2095 if (column
->IsHidden())
2096 continue; // skip it!
2098 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2099 continue; // skip it!
2101 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2103 model
->GetValue( value
, item
, column
->GetModelColumn() );
2104 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2105 renderer2
->SetValue( value
);
2106 height
= wxMax( height
, renderer
->GetSize().y
);
2117 return row
* m_lineHeight
;
2121 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2123 const wxDataViewModel
*model
= GetOwner()->GetModel();
2125 // check for the easy case first
2126 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2127 return y
/ m_lineHeight
;
2129 // TODO make more efficient
2130 unsigned int row
= 0;
2131 unsigned int yy
= 0;
2134 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2137 // not really correct...
2138 return row
+ ((y
-yy
) / m_lineHeight
);
2141 wxDataViewItem item
= node
->GetItem();
2143 if (node
&& !node
->HasChildren())
2145 // Yes, if the node does not have any child, it must be a leaf which
2146 // mean that it is a temporarily created by GetTreeNodeByRow
2150 unsigned int cols
= GetOwner()->GetColumnCount();
2152 int height
= m_lineHeight
;
2153 for (col
= 0; col
< cols
; col
++)
2155 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2156 if (column
->IsHidden())
2157 continue; // skip it!
2159 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2160 continue; // skip it!
2162 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2164 model
->GetValue( value
, item
, column
->GetModelColumn() );
2165 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2166 renderer2
->SetValue( value
);
2167 height
= wxMax( height
, renderer
->GetSize().y
);
2178 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2180 const wxDataViewModel
*model
= GetOwner()->GetModel();
2182 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2184 wxASSERT( !IsVirtualList() );
2186 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2187 // wxASSERT( node );
2188 if (!node
) return m_lineHeight
;
2190 wxDataViewItem item
= node
->GetItem();
2192 if (node
&& !node
->HasChildren())
2194 // Yes, if the node does not have any child, it must be a leaf which
2195 // mean that it is a temporarily created by GetTreeNodeByRow
2199 int height
= m_lineHeight
;
2201 unsigned int cols
= GetOwner()->GetColumnCount();
2203 for (col
= 0; col
< cols
; col
++)
2205 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2206 if (column
->IsHidden())
2207 continue; // skip it!
2209 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2210 continue; // skip it!
2212 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2214 model
->GetValue( value
, item
, column
->GetModelColumn() );
2215 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2216 renderer2
->SetValue( value
);
2217 height
= wxMax( height
, renderer
->GetSize().y
);
2224 return m_lineHeight
;
2228 class RowToItemJob
: public DoJob
2231 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2232 virtual ~RowToItemJob() { }
2234 virtual int operator() ( wxDataViewTreeNode
* node
)
2237 if( current
== static_cast<int>(row
))
2239 ret
= node
->GetItem();
2243 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2245 current
+= node
->GetSubTreeCount();
2250 //If the current has no child node, we can find the desired item of the row number directly.
2251 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2252 if( node
->GetNodes().GetCount() == 0)
2254 int index
= static_cast<int>(row
) - current
- 1;
2255 ret
= node
->GetChildren().Item( index
);
2262 virtual int operator() ( void * n
)
2265 if( current
== static_cast<int>(row
))
2267 ret
= wxDataViewItem( n
);
2272 wxDataViewItem
GetResult(){ return ret
; }
2279 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2283 return wxDataViewItem( wxUIntToPtr(row
) );
2287 RowToItemJob
job( row
, -2 );
2288 Walker( m_root
, job
);
2289 return job
.GetResult();
2293 class RowToTreeNodeJob
: public DoJob
2296 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2299 this->current
= current
;
2303 virtual ~RowToTreeNodeJob(){ }
2305 virtual int operator() ( wxDataViewTreeNode
* node
)
2308 if( current
== static_cast<int>(row
))
2314 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2316 current
+= node
->GetSubTreeCount();
2322 //If the current has no child node, we can find the desired item of the row number directly.
2323 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2324 if( node
->GetNodes().GetCount() == 0)
2326 int index
= static_cast<int>(row
) - current
- 1;
2327 void * n
= node
->GetChildren().Item( index
);
2328 ret
= new wxDataViewTreeNode( parent
);
2329 ret
->SetItem( wxDataViewItem( n
));
2330 ret
->SetHasChildren(false);
2339 virtual int operator() ( void * n
)
2342 if( current
== static_cast<int>(row
))
2344 ret
= new wxDataViewTreeNode( parent
);
2345 ret
->SetItem( wxDataViewItem( n
));
2346 ret
->SetHasChildren(false);
2352 wxDataViewTreeNode
* GetResult(){ return ret
; }
2356 wxDataViewTreeNode
* ret
;
2357 wxDataViewTreeNode
* parent
;
2361 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2363 wxASSERT( !IsVirtualList() );
2365 RowToTreeNodeJob
job( row
, -2, m_root
);
2366 Walker( m_root
, job
);
2367 return job
.GetResult();
2370 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2372 wxWindow
*parent
= GetParent();
2373 wxDataViewEvent
le(type
, parent
->GetId());
2375 le
.SetEventObject(parent
);
2376 le
.SetModel(GetOwner()->GetModel());
2379 parent
->GetEventHandler()->ProcessEvent(le
);
2383 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2385 if (IsVirtualList())
2388 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2391 if( node
->HasChildren())
2393 if( !node
->IsOpen())
2395 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2396 //Check if the user prevent expanding
2397 if( e
.GetSkipped() )
2401 //Here I build the children of current node
2402 if( node
->GetChildrenNumber() == 0 )
2405 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2409 //Send the expanded event
2410 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2414 SelectRow( row
, false );
2415 SelectRow( row
+ 1, true );
2416 ChangeCurrentRow( row
+ 1 );
2417 SendSelectionChangedEvent( GetItemByRow(row
+1));
2425 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2427 if (IsVirtualList())
2430 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2433 wxDataViewTreeNode
* nd
= node
;
2435 if( node
->HasChildren() && node
->IsOpen() )
2437 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2438 if( e
.GetSkipped() )
2443 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2447 node
= node
->GetParent();
2450 int parent
= GetRowByItem( node
->GetItem() );
2453 SelectRow( row
, false);
2454 SelectRow(parent
, true );
2455 ChangeCurrentRow( parent
);
2456 SendSelectionChangedEvent( node
->GetItem() );
2460 if( !nd
->HasChildren())
2465 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2467 wxDataViewModel
* model
= GetOwner()->GetModel();
2471 //Compose the a parent-chain of the finding item
2473 list
.DeleteContents( true );
2474 wxDataViewItem
it( item
);
2477 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2478 list
.Insert( pItem
);
2479 it
= model
->GetParent( it
);
2482 //Find the item along the parent-chain.
2483 //This algorithm is designed to speed up the node-finding method
2484 wxDataViewTreeNode
* node
= m_root
;
2485 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
2487 if( node
->HasChildren() )
2489 if( node
->GetChildrenNumber() == 0 )
2492 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2495 wxDataViewTreeNodes nodes
= node
->GetNodes();
2499 for (i
= 0; i
< nodes
.GetCount(); i
++)
2501 if (nodes
[i
]->GetItem() == (**iter
))
2503 if (nodes
[i
]->GetItem() == item
)
2520 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
2522 wxDataViewColumn
*col
= NULL
;
2523 unsigned int cols
= GetOwner()->GetColumnCount();
2524 unsigned int colnum
= 0;
2526 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
2527 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
2529 col
= GetOwner()->GetColumn(colnum
);
2530 if (col
->IsHidden())
2531 continue; // skip it!
2533 unsigned int w
= col
->GetWidth();
2534 if (x_start
+w
>= (unsigned int)x
)
2541 item
= GetItemByRow( GetLineAt( y
) );
2544 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
2546 int row
= GetRowByItem(item
);
2547 int y
= GetLineStart( row
);
2548 int h
= GetLineHeight( m_lineHeight
);
2550 wxDataViewColumn
*col
= NULL
;
2551 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
2553 col
= GetOwner()->GetColumn( i
);
2554 x
+= col
->GetWidth();
2555 if( GetOwner()->GetColumn(i
+1) == column
)
2558 int w
= col
->GetWidth();
2559 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
2560 return wxRect(x
, y
, w
, h
);
2563 int wxDataViewMainWindow::RecalculateCount()
2567 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
2569 return list_model
->GetLastIndex() + 1;
2571 return list_model
->GetLastIndex() - 1;
2576 return m_root
->GetSubTreeCount();
2580 class ItemToRowJob
: public DoJob
2583 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
2590 //Maybe binary search will help to speed up this process
2591 virtual int operator() ( wxDataViewTreeNode
* node
)
2594 if( node
->GetItem() == item
)
2599 if( node
->GetItem() == **m_iter
)
2606 ret
+= node
->GetSubTreeCount();
2612 virtual int operator() ( void * n
)
2615 if( n
== item
.GetID() )
2619 //the row number is begin from zero
2620 int GetResult() { return ret
-1; }
2623 ItemList::const_iterator m_iter
;
2624 wxDataViewItem item
;
2629 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
2631 const wxDataViewModel
* model
= GetOwner()->GetModel();
2637 return wxPtrToUInt( item
.GetID() );
2644 //Compose the a parent-chain of the finding item
2646 wxDataViewItem
* pItem
;
2647 list
.DeleteContents( true );
2648 wxDataViewItem
it( item
);
2651 pItem
= new wxDataViewItem( it
);
2652 list
.Insert( pItem
);
2653 it
= model
->GetParent( it
);
2655 pItem
= new wxDataViewItem( );
2656 list
.Insert( pItem
);
2658 ItemToRowJob
job( item
, list
.begin() );
2659 Walker(m_root
, job
);
2660 return job
.GetResult();
2664 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2666 if( !model
->IsContainer( item
) )
2669 wxDataViewItemArray children
;
2670 unsigned int num
= model
->GetChildren( item
, children
);
2671 unsigned int index
= 0;
2672 while( index
< num
)
2674 if( model
->IsContainer( children
[index
] ) )
2676 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2677 n
->SetItem(children
[index
]);
2678 n
->SetHasChildren( true );
2683 node
->AddLeaf( children
[index
].GetID() );
2687 node
->SetSubTreeCount( num
);
2688 wxDataViewTreeNode
* n
= node
->GetParent();
2690 n
->ChangeSubTreeCount(num
);
2694 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2698 if (GetOwner()->GetModel()->IsVirtualListModel())
2704 m_root
= new wxDataViewTreeNode( NULL
);
2705 m_root
->SetHasChildren(true);
2707 //First we define a invalid item to fetch the top-level elements
2708 wxDataViewItem item
;
2710 BuildTreeHelper( model
, item
, m_root
);
2714 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2716 if( node
->GetNodeNumber() != 0 )
2718 int len
= node
->GetNodeNumber();
2720 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
2721 for(; i
< len
; i
++ )
2723 DestroyTreeHelper(nodes
[i
]);
2729 void wxDataViewMainWindow::DestroyTree()
2731 if (!IsVirtualList())
2733 ::DestroyTreeHelper(m_root
);
2739 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2741 if ( GetParent()->HandleAsNavigationKey(event
) )
2744 // no item -> nothing to do
2745 if (!HasCurrentRow())
2751 // don't use m_linesPerPage directly as it might not be computed yet
2752 const int pageSize
= GetCountPerPage();
2753 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2755 switch ( event
.GetKeyCode() )
2759 if (m_currentRow
> 0)
2761 wxWindow
*parent
= GetParent();
2762 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
2763 le
.SetItem( GetItemByRow(m_currentRow
) );
2764 le
.SetEventObject(parent
);
2765 le
.SetModel(GetOwner()->GetModel());
2767 parent
->GetEventHandler()->ProcessEvent(le
);
2772 if ( m_currentRow
> 0 )
2773 OnArrowChar( m_currentRow
- 1, event
);
2777 if ( m_currentRow
< GetRowCount() - 1 )
2778 OnArrowChar( m_currentRow
+ 1, event
);
2780 //Add the process for tree expanding/collapsing
2782 OnCollapsing(m_currentRow
);
2785 OnExpanding( m_currentRow
);
2789 OnArrowChar( GetRowCount() - 1, event
);
2794 OnArrowChar( 0, event
);
2799 int steps
= pageSize
- 1;
2800 int index
= m_currentRow
- steps
;
2804 OnArrowChar( index
, event
);
2810 int steps
= pageSize
- 1;
2811 unsigned int index
= m_currentRow
+ steps
;
2812 unsigned int count
= GetRowCount();
2813 if ( index
>= count
)
2816 OnArrowChar( index
, event
);
2825 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
2827 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2829 // let the base handle mouse wheel events.
2834 int x
= event
.GetX();
2835 int y
= event
.GetY();
2836 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2837 wxDataViewColumn
*col
= NULL
;
2840 unsigned int cols
= GetOwner()->GetColumnCount();
2842 for (i
= 0; i
< cols
; i
++)
2844 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2846 continue; // skip it!
2848 if (x
< xpos
+ c
->GetWidth())
2853 xpos
+= c
->GetWidth();
2858 wxDataViewRenderer
*cell
= col
->GetRenderer();
2859 unsigned int current
= GetLineAt( y
);
2860 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
2862 // Unselect all if below the last row ?
2866 //Test whether the mouse is hovered on the tree item button
2868 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
2870 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
2871 if( node
!=NULL
&& node
->HasChildren() )
2873 int indent
= node
->GetIndentLevel();
2874 indent
= GetOwner()->GetIndent()*indent
;
2875 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
2876 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
2877 m_lineHeight
-2*EXPANDER_MARGIN
,
2878 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
2879 if( rect
.Contains( x
, y
) )
2881 //So the mouse is over the expander
2883 if (m_underMouse
&& m_underMouse
!= node
)
2885 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
2886 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
2888 if (m_underMouse
!= node
)
2890 //wxLogMessage("Do the row: %d", current);
2891 RefreshRow(current
);
2893 m_underMouse
= node
;
2896 if (node
!=NULL
&& !node
->HasChildren())
2901 if (m_underMouse
!= NULL
)
2903 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
2904 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
2905 m_underMouse
= NULL
;
2909 wxDataViewModel
*model
= GetOwner()->GetModel();
2911 if (event
.Dragging())
2913 if (m_dragCount
== 0)
2915 // we have to report the raw, physical coords as we want to be
2916 // able to call HitTest(event.m_pointDrag) from the user code to
2917 // get the item being dragged
2918 m_dragStart
= event
.GetPosition();
2923 if (m_dragCount
!= 3)
2926 if (event
.LeftIsDown())
2928 // Notify cell about drag
2937 bool forceClick
= false;
2939 if (event
.ButtonDClick())
2941 m_renameTimer
->Stop();
2942 m_lastOnSame
= false;
2945 wxDataViewItem item
= GetItemByRow(current
);
2946 bool ignore_other_columns
=
2947 ((GetOwner()->GetExpanderColumn() != col
) &&
2948 (model
->IsContainer(item
)) &&
2949 (!model
->HasContainerColumns(item
)));
2951 if (event
.LeftDClick())
2953 if ( current
== m_lineLastClicked
)
2955 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
2958 model
->GetValue( value
, item
, col
->GetModelColumn() );
2959 cell
->SetValue( value
);
2960 wxRect
cell_rect( xpos
, GetLineStart( current
),
2961 col
->GetWidth(), GetLineHeight( current
) );
2962 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
2967 wxWindow
*parent
= GetParent();
2968 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
2970 le
.SetEventObject(parent
);
2971 le
.SetModel(GetOwner()->GetModel());
2973 parent
->GetEventHandler()->ProcessEvent(le
);
2979 // The first click was on another item, so don't interpret this as
2980 // a double click, but as a simple click instead
2987 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
2989 // select single line
2990 SelectAllRows( false );
2991 SelectRow( m_lineSelectSingleOnUp
, true );
2994 //Process the event of user clicking the expander
2995 bool expander
= false;
2996 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
2998 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
2999 if( node
!=NULL
&& node
->HasChildren() )
3001 int indent
= node
->GetIndentLevel();
3002 indent
= GetOwner()->GetIndent()*indent
;
3003 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
3004 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
3005 m_lineHeight
-2*EXPANDER_MARGIN
,
3006 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
3008 if( rect
.Contains( x
, y
) )
3011 if( node
->IsOpen() )
3012 OnCollapsing(current
);
3014 OnExpanding( current
);
3017 if (node
&& !node
->HasChildren())
3020 //If the user click the expander, we do not do editing even if the column with expander are editable
3021 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3023 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3024 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3026 m_renameTimer
->Start( 100, true );
3030 m_lastOnSame
= false;
3031 m_lineSelectSingleOnUp
= (unsigned int)-1;
3035 // This is necessary, because after a DnD operation in
3036 // from and to ourself, the up event is swallowed by the
3037 // DnD code. So on next non-up event (which means here and
3038 // now) m_lineSelectSingleOnUp should be reset.
3039 m_lineSelectSingleOnUp
= (unsigned int)-1;
3042 if (event
.RightDown())
3044 m_lineBeforeLastClicked
= m_lineLastClicked
;
3045 m_lineLastClicked
= current
;
3047 // If the item is already selected, do not update the selection.
3048 // Multi-selections should not be cleared if a selected item is clicked.
3049 if (!IsRowSelected(current
))
3051 SelectAllRows(false);
3052 ChangeCurrentRow(current
);
3053 SelectRow(m_currentRow
,true);
3054 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3058 model
->GetValue( value
, item
, col
->GetModelColumn() );
3059 wxWindow
*parent
= GetParent();
3060 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3062 le
.SetEventObject(parent
);
3063 le
.SetModel(GetOwner()->GetModel());
3065 parent
->GetEventHandler()->ProcessEvent(le
);
3067 else if (event
.MiddleDown())
3070 if (event
.LeftDown() || forceClick
)
3074 m_lineBeforeLastClicked
= m_lineLastClicked
;
3075 m_lineLastClicked
= current
;
3077 unsigned int oldCurrentRow
= m_currentRow
;
3078 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3080 bool cmdModifierDown
= event
.CmdDown();
3081 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3083 if ( IsSingleSel() || !IsRowSelected(current
) )
3085 SelectAllRows( false );
3086 ChangeCurrentRow(current
);
3087 SelectRow(m_currentRow
,true);
3088 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3090 else // multi sel & current is highlighted & no mod keys
3092 m_lineSelectSingleOnUp
= current
;
3093 ChangeCurrentRow(current
); // change focus
3096 else // multi sel & either ctrl or shift is down
3098 if (cmdModifierDown
)
3100 ChangeCurrentRow(current
);
3101 ReverseRowSelection(m_currentRow
);
3102 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3104 else if (event
.ShiftDown())
3106 ChangeCurrentRow(current
);
3108 unsigned int lineFrom
= oldCurrentRow
,
3111 if ( lineTo
< lineFrom
)
3114 lineFrom
= m_currentRow
;
3117 SelectRows(lineFrom
, lineTo
, true);
3118 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3120 else // !ctrl, !shift
3122 // test in the enclosing if should make it impossible
3123 wxFAIL_MSG( _T("how did we get here?") );
3127 if (m_currentRow
!= oldCurrentRow
)
3128 RefreshRow( oldCurrentRow
);
3130 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3132 // Update selection here...
3135 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3136 (current
== oldCurrentRow
)) && oldWasSelected
;
3138 // Call LeftClick after everything else as under GTK+
3139 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3141 // notify cell about right click
3143 model
->GetValue( value
, item
, col
->GetModelColumn() );
3144 cell
->SetValue( value
);
3145 wxRect
cell_rect( xpos
, GetLineStart( current
),
3146 col
->GetWidth(), GetLineHeight( current
) );
3147 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3152 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3156 if (HasCurrentRow())
3162 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3166 if (HasCurrentRow())
3172 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3174 if( m_selection
.GetCount() != 1 )
3175 return wxDataViewItem();
3177 return GetItemByRow( m_selection
.Item(0));
3180 //-----------------------------------------------------------------------------
3182 //-----------------------------------------------------------------------------
3183 WX_DEFINE_LIST(wxDataViewColumnList
)
3185 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3187 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3188 EVT_SIZE(wxDataViewCtrl::OnSize
)
3191 wxDataViewCtrl::~wxDataViewCtrl()
3194 GetModel()->RemoveNotifier( m_notifier
);
3199 void wxDataViewCtrl::Init()
3201 m_cols
.DeleteContents(true);
3204 // No sorting column at start
3205 m_sortingColumn
= NULL
;
3206 m_headerArea
= NULL
;
3209 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3210 const wxPoint
& pos
, const wxSize
& size
,
3211 long style
, const wxValidator
& validator
)
3213 if ( (style
& wxBORDER_MASK
) == 0)
3214 style
|= wxBORDER_SUNKEN
;
3218 if (!wxControl::Create( parent
, id
, pos
, size
,
3219 style
| wxScrolledWindowStyle
, validator
))
3222 SetInitialSize(size
);
3225 MacSetClipChildren( true );
3228 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3230 if (HasFlag(wxDV_NO_HEADER
))
3231 m_headerArea
= NULL
;
3233 m_headerArea
= new wxDataViewHeaderWindow(this);
3235 SetTargetWindow( m_clientArea
);
3237 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3239 sizer
->Add( m_headerArea
, 0, wxGROW
);
3240 sizer
->Add( m_clientArea
, 1, wxGROW
);
3247 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3251 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3254 // we need to process arrows ourselves for scrolling
3255 if ( nMsg
== WM_GETDLGCODE
)
3257 rc
|= DLGC_WANTARROWS
;
3265 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3267 wxSize newsize
= size
;
3268 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3269 newsize
.y
-= m_headerArea
->GetSize().y
;
3274 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3276 // We need to override OnSize so that our scrolled
3277 // window a) does call Layout() to use sizers for
3278 // positioning the controls but b) does not query
3279 // the sizer for their size and use that for setting
3280 // the scrollable area as set that ourselves by
3281 // calling SetScrollbar() further down.
3288 void wxDataViewCtrl::SetFocus()
3291 m_clientArea
->SetFocus();
3294 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3296 if (!wxDataViewCtrlBase::AssociateModel( model
))
3299 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3301 model
->AddNotifier( m_notifier
);
3303 m_clientArea
->DestroyTree();
3305 m_clientArea
->BuildTree(model
);
3307 m_clientArea
->UpdateDisplay();
3312 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3314 if (!wxDataViewCtrlBase::AppendColumn(col
))
3317 m_cols
.Append( col
);
3318 OnColumnsCountChanged();
3322 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3324 if (!wxDataViewCtrlBase::PrependColumn(col
))
3327 m_cols
.Insert( col
);
3328 OnColumnsCountChanged();
3332 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3334 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
3337 m_cols
.Insert( pos
, col
);
3338 OnColumnsCountChanged();
3342 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
3345 m_headerArea
->UpdateColumn(idx
);
3347 m_clientArea
->UpdateDisplay();
3350 void wxDataViewCtrl::OnColumnsCountChanged()
3353 m_headerArea
->SetColumnCount(GetColumnCount());
3355 m_clientArea
->UpdateDisplay();
3358 void wxDataViewCtrl::DoSetExpanderColumn()
3360 m_clientArea
->UpdateDisplay();
3363 void wxDataViewCtrl::DoSetIndent()
3365 m_clientArea
->UpdateDisplay();
3368 unsigned int wxDataViewCtrl::GetColumnCount() const
3370 return m_cols
.GetCount();
3373 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
3378 void wxDataViewCtrl::ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
)
3380 if (new_pos
> m_cols
.GetCount()) return;
3382 // Exchange position
3383 m_cols
.DeleteContents(false);
3384 m_cols
.DeleteObject( col
);
3385 m_cols
.Insert( new_pos
, col
);
3386 m_cols
.DeleteContents(true);
3388 m_clientArea
->UpdateDisplay();
3391 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3393 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3398 OnColumnsCountChanged();
3403 bool wxDataViewCtrl::ClearColumns()
3406 OnColumnsCountChanged();
3410 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3412 int ret
= 0, dead
= 0;
3413 int len
= GetColumnCount();
3414 for (int i
=0; i
<len
; i
++)
3416 wxDataViewColumn
* col
= GetColumn(i
);
3417 if (col
->IsHidden())
3419 ret
+= col
->GetWidth();
3422 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3429 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3434 //Selection code with wxDataViewItem as parameters
3435 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3437 return m_clientArea
->GetSelection();
3440 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3443 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3444 int len
= selection
.GetCount();
3445 for( int i
= 0; i
< len
; i
++)
3447 unsigned int row
= selection
[i
];
3448 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3453 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3455 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
3456 int len
= sel
.GetCount();
3457 for( int i
= 0; i
< len
; i
++ )
3459 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3461 selection
.Add( static_cast<unsigned int>(row
) );
3463 m_clientArea
->SetSelections( selection
);
3466 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3468 int row
= m_clientArea
->GetRowByItem( item
);
3471 //Unselect all rows before select another in the single select mode
3472 if (m_clientArea
->IsSingleSel())
3473 m_clientArea
->SelectAllRows(false);
3474 m_clientArea
->SelectRow(row
, true);
3478 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3480 int row
= m_clientArea
->GetRowByItem( item
);
3482 m_clientArea
->SelectRow(row
, false);
3485 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3487 int row
= m_clientArea
->GetRowByItem( item
);
3490 return m_clientArea
->IsRowSelected(row
);
3495 //Selection code with row number as parameter
3496 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3499 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3500 int len
= selection
.GetCount();
3501 for( int i
= 0; i
< len
; i
++)
3503 unsigned int row
= selection
[i
];
3509 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3511 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
3512 int len
= sel
.GetCount();
3513 for( int i
= 0; i
< len
; i
++ )
3517 selection
.Add( static_cast<unsigned int>(row
) );
3519 m_clientArea
->SetSelections( selection
);
3522 void wxDataViewCtrl::Select( int row
)
3526 if (m_clientArea
->IsSingleSel())
3527 m_clientArea
->SelectAllRows(false);
3528 m_clientArea
->SelectRow( row
, true );
3532 void wxDataViewCtrl::Unselect( int row
)
3535 m_clientArea
->SelectRow(row
, false);
3538 bool wxDataViewCtrl::IsSelected( int row
) const
3541 return m_clientArea
->IsRowSelected(row
);
3545 void wxDataViewCtrl::SelectRange( int from
, int to
)
3548 for( int i
= from
; i
< to
; i
++ )
3550 m_clientArea
->Select(sel
);
3553 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3555 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3556 for( int i
= from
; i
< to
; i
++ )
3557 if( sel
.Index( i
) != wxNOT_FOUND
)
3559 m_clientArea
->SetSelections(sel
);
3562 void wxDataViewCtrl::SelectAll()
3564 m_clientArea
->SelectAllRows(true);
3567 void wxDataViewCtrl::UnselectAll()
3569 m_clientArea
->SelectAllRows(false);
3572 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3576 if( row
> (int) m_clientArea
->GetRowCount() )
3577 row
= m_clientArea
->GetRowCount();
3579 int first
= m_clientArea
->GetFirstVisibleRow();
3580 int last
= m_clientArea
->GetLastVisibleRow();
3582 m_clientArea
->ScrollTo( row
, column
);
3583 else if( row
> last
)
3584 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3586 m_clientArea
->ScrollTo( first
, column
);
3589 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3591 int row
= m_clientArea
->GetRowByItem(item
);
3594 if( column
== NULL
)
3595 EnsureVisible(row
, -1);
3599 int len
= GetColumnCount();
3600 for( int i
= 0; i
< len
; i
++ )
3601 if( GetColumn(i
) == column
)
3606 EnsureVisible( row
, col
);
3612 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
3614 m_clientArea
->HitTest(point
, item
, column
);
3617 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
3619 return m_clientArea
->GetItemRect(item
, column
);
3622 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
3624 return m_clientArea
->GetItemByRow( row
);
3627 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
3629 return m_clientArea
->GetRowByItem( item
);
3632 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
3634 int row
= m_clientArea
->GetRowByItem( item
);
3636 m_clientArea
->Expand(row
);
3639 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
3641 int row
= m_clientArea
->GetRowByItem( item
);
3643 m_clientArea
->Collapse(row
);
3647 // !wxUSE_GENERICDATAVIEWCTRL
3650 // wxUSE_DATAVIEWCTRL