1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/calctrl.h"
42 #include "wx/popupwin.h"
43 #include "wx/renderer.h"
44 #include "wx/dcbuffer.h"
47 #include "wx/listimpl.cpp"
48 #include "wx/imaglist.h"
49 #include "wx/headerctrl.h"
51 #include "wx/stopwatch.h"
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 class wxDataViewColumn
;
58 class wxDataViewHeaderWindow
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static const int SCROLL_UNIT_X
= 15;
67 // the cell padding on the left/right
68 static const int PADDING_RIGHTLEFT
= 3;
70 // the expander space margin
71 static const int EXPANDER_MARGIN
= 4;
74 static const int EXPANDER_OFFSET
= 4;
76 static const int EXPANDER_OFFSET
= 1;
79 // Below is the compare stuff.
80 // For the generic implementation, both the leaf nodes and the nodes are sorted for
81 // fast search when needed
82 static wxDataViewModel
* g_model
;
83 static int g_column
= -2;
84 static bool g_asending
= true;
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
90 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
97 m_sortAscending
= true;
100 int wxDataViewColumn::GetWidth() const
104 case wxCOL_WIDTH_DEFAULT
:
105 return wxDVC_DEFAULT_WIDTH
;
107 case wxCOL_WIDTH_AUTOSIZE
:
108 wxCHECK_MSG( m_owner
, wxDVC_DEFAULT_WIDTH
, "no owner control" );
109 return m_owner
->GetBestColumnWidth(m_owner
->GetColumnIndex(this));
116 void wxDataViewColumn::UpdateDisplay()
120 int idx
= m_owner
->GetColumnIndex( this );
121 m_owner
->OnColumnChange( idx
);
125 //-----------------------------------------------------------------------------
126 // wxDataViewHeaderWindow
127 //-----------------------------------------------------------------------------
129 class wxDataViewHeaderWindow
: public wxHeaderCtrl
132 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
133 : wxHeaderCtrl(parent
)
137 wxDataViewCtrl
*GetOwner() const
138 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
141 // implement/override wxHeaderCtrl functions by forwarding them to the main
143 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
145 return *(GetOwner()->GetColumn(idx
));
148 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
150 wxDataViewCtrl
* const owner
= GetOwner();
152 int widthContents
= owner
->GetBestColumnWidth(idx
);
153 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
154 owner
->OnColumnChange(idx
);
160 bool SendEvent(wxEventType type
, unsigned int n
)
162 wxDataViewCtrl
* const owner
= GetOwner();
163 wxDataViewEvent
event(type
, owner
->GetId());
165 event
.SetEventObject(owner
);
167 event
.SetDataViewColumn(owner
->GetColumn(n
));
168 event
.SetModel(owner
->GetModel());
170 // for events created by wxDataViewHeaderWindow the
171 // row / value fields are not valid
172 return owner
->GetEventHandler()->ProcessEvent(event
);
175 void OnClick(wxHeaderCtrlEvent
& event
)
177 const unsigned idx
= event
.GetColumn();
179 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
182 // default handling for the column click is to sort by this column or
183 // toggle its sort order
184 wxDataViewCtrl
* const owner
= GetOwner();
185 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
186 if ( !col
->IsSortable() )
188 // no default handling for non-sortable columns
193 if ( col
->IsSortKey() )
195 // already using this column for sorting, just change the order
196 col
->ToggleSortOrder();
198 else // not using this column for sorting yet
200 // first unset the old sort column if any
201 int oldSortKey
= owner
->GetSortingColumnIndex();
202 if ( oldSortKey
!= wxNOT_FOUND
)
204 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
205 owner
->OnColumnChange(oldSortKey
);
208 owner
->SetSortingColumnIndex(idx
);
212 wxDataViewModel
* const model
= owner
->GetModel();
216 owner
->OnColumnChange(idx
);
219 void OnRClick(wxHeaderCtrlEvent
& event
)
221 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
226 void OnResize(wxHeaderCtrlEvent
& event
)
228 wxDataViewCtrl
* const owner
= GetOwner();
230 const unsigned col
= event
.GetColumn();
231 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
232 GetOwner()->OnColumnChange(col
);
235 void OnEndReorder(wxHeaderCtrlEvent
& event
)
237 wxDataViewCtrl
* const owner
= GetOwner();
238 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
239 event
.GetNewOrder());
242 DECLARE_EVENT_TABLE()
243 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
246 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
247 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
248 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
250 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
251 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
253 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
256 //-----------------------------------------------------------------------------
257 // wxDataViewRenameTimer
258 //-----------------------------------------------------------------------------
260 class wxDataViewRenameTimer
: public wxTimer
263 wxDataViewMainWindow
*m_owner
;
266 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
270 //-----------------------------------------------------------------------------
271 // wxDataViewTreeNode
272 //-----------------------------------------------------------------------------
274 class wxDataViewTreeNode
;
275 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
276 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
278 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
279 wxDataViewTreeNode
** node2
);
280 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
282 class wxDataViewTreeNode
285 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
292 m_hasChildren
= false;
296 ~wxDataViewTreeNode()
300 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
301 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
302 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
303 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
305 void AddNode( wxDataViewTreeNode
* node
)
307 m_leaves
.Add( node
->GetItem().GetID() );
309 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
312 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
314 void AddLeaf( void * leaf
)
316 m_leaves
.Add( leaf
);
318 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
321 wxDataViewItem
& GetItem() { return m_item
; }
322 const wxDataViewItem
& GetItem() const { return m_item
; }
323 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
325 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
326 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
327 int GetIndentLevel() const
330 const wxDataViewTreeNode
* node
= this;
331 while( node
->GetParent()->GetParent() != NULL
)
333 node
= node
->GetParent();
346 int len
= m_nodes
.GetCount();
348 for ( int i
= 0;i
< len
; i
++)
349 sum
+= m_nodes
[i
]->GetSubTreeCount();
351 sum
+= m_leaves
.GetCount();
354 ChangeSubTreeCount(-sum
);
360 ChangeSubTreeCount(sum
);
363 bool HasChildren() const { return m_hasChildren
; }
364 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
366 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
367 int GetSubTreeCount() const { return m_subTreeCount
; }
368 void ChangeSubTreeCount( int num
)
372 m_subTreeCount
+= num
;
374 m_parent
->ChangeSubTreeCount(num
);
381 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
382 int len
= m_nodes
.GetCount();
383 for (int i
= 0; i
< len
; i
++)
384 m_nodes
[i
]->Resort();
385 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
390 wxDataViewTreeNode
*m_parent
;
391 wxDataViewTreeNodes m_nodes
;
392 wxDataViewTreeLeaves m_leaves
;
393 wxDataViewItem m_item
;
399 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
400 wxDataViewTreeNode
** node2
)
402 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
405 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
407 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
411 //-----------------------------------------------------------------------------
412 // wxDataViewMainWindow
413 //-----------------------------------------------------------------------------
415 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
417 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
418 WX_DEFINE_LIST(ItemList
)
420 class wxDataViewMainWindow
: public wxWindow
423 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
425 const wxPoint
&pos
= wxDefaultPosition
,
426 const wxSize
&size
= wxDefaultSize
,
427 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
428 virtual ~wxDataViewMainWindow();
430 bool IsList() const { return GetOwner()->GetModel()->IsListModel(); }
431 bool IsVirtualList() const { return m_root
== NULL
; }
433 // notifications from wxDataViewModel
434 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
435 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
436 bool ItemChanged( const wxDataViewItem
&item
);
437 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
441 if (!IsVirtualList())
451 g_model
= GetOwner()->GetModel();
452 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
455 if (g_model
->HasDefaultCompare())
463 g_column
= col
->GetModelColumn();
464 g_asending
= col
->IsSortOrderAscending();
467 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
468 wxDataViewCtrl
*GetOwner() { return m_owner
; }
469 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
471 #if wxUSE_DRAG_AND_DROP
472 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
473 #endif // wxUSE_DRAG_AND_DROP
474 void OnPaint( wxPaintEvent
&event
);
475 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
476 void OnChar( wxKeyEvent
&event
);
477 void OnMouse( wxMouseEvent
&event
);
478 void OnSetFocus( wxFocusEvent
&event
);
479 void OnKillFocus( wxFocusEvent
&event
);
481 void UpdateDisplay();
482 void RecalculateDisplay();
483 void OnInternalIdle();
485 void OnRenameTimer();
487 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
488 void ScrollTo( int rows
, int column
);
490 unsigned GetCurrentRow() const { return m_currentRow
; }
491 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
492 void ChangeCurrentRow( unsigned int row
);
494 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
495 bool IsEmpty() { return GetRowCount() == 0; }
497 int GetCountPerPage() const;
498 int GetEndOfLastCol() const;
499 unsigned int GetFirstVisibleRow() const;
501 // I change this method to un const because in the tree view,
502 // the displaying number of the tree are changing along with the
503 // expanding/collapsing of the tree nodes
504 unsigned int GetLastVisibleRow();
505 unsigned int GetRowCount();
507 wxDataViewItem
GetSelection() const;
508 wxDataViewSelection
GetSelections(){ return m_selection
; }
509 void SetSelections( const wxDataViewSelection
& sel
)
510 { m_selection
= sel
; UpdateDisplay(); }
511 void Select( const wxArrayInt
& aSelections
);
512 void SelectAllRows( bool on
);
513 void SelectRow( unsigned int row
, bool on
);
514 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
515 void ReverseRowSelection( unsigned int row
);
516 bool IsRowSelected( unsigned int row
);
517 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
519 void RefreshRow( unsigned int row
);
520 void RefreshRows( unsigned int from
, unsigned int to
);
521 void RefreshRowsAfter( unsigned int firstRow
);
523 // returns the colour to be used for drawing the rules
524 wxColour
GetRuleColour() const
526 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
529 wxRect
GetLineRect( unsigned int row
) const;
531 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
532 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
533 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
535 // Some useful functions for row and item mapping
536 wxDataViewItem
GetItemByRow( unsigned int row
) const;
537 int GetRowByItem( const wxDataViewItem
& item
) const;
539 // Methods for building the mapping tree
540 void BuildTree( wxDataViewModel
* model
);
542 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
543 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
545 void Expand( unsigned int row
);
546 void Collapse( unsigned int row
);
547 bool IsExpanded( unsigned int row
) const;
548 bool HasChildren( unsigned int row
) const;
550 #if wxUSE_DRAG_AND_DROP
551 bool EnableDragSource( const wxDataFormat
&format
);
552 bool EnableDropTarget( const wxDataFormat
&format
);
554 void RemoveDropHint();
555 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
556 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
557 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
559 #endif // wxUSE_DRAG_AND_DROP
562 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
563 // We did not need this temporarily
564 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
566 int RecalculateCount();
568 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
570 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
573 wxDataViewCtrl
*m_owner
;
577 wxDataViewColumn
*m_currentCol
;
578 unsigned int m_currentRow
;
579 wxDataViewSelection m_selection
;
581 wxDataViewRenameTimer
*m_renameTimer
;
586 #if wxUSE_DRAG_AND_DROP
591 wxDataFormat m_dragFormat
;
594 wxDataFormat m_dropFormat
;
596 unsigned int m_dropHintLine
;
597 #endif // wxUSE_DRAG_AND_DROP
599 // for double click logic
600 unsigned int m_lineLastClicked
,
601 m_lineBeforeLastClicked
,
602 m_lineSelectSingleOnUp
;
604 // the pen used to draw horiz/vertical rules
607 // the pen used to draw the expander and the lines
610 // This is the tree structure of the model
611 wxDataViewTreeNode
* m_root
;
614 // This is the tree node under the cursor
615 wxDataViewTreeNode
* m_underMouse
;
618 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
619 DECLARE_EVENT_TABLE()
622 // ---------------------------------------------------------
623 // wxGenericDataViewModelNotifier
624 // ---------------------------------------------------------
626 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
629 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
630 { m_mainWindow
= mainWindow
; }
632 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
633 { return m_mainWindow
->ItemAdded( parent
, item
); }
634 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
635 { return m_mainWindow
->ItemDeleted( parent
, item
); }
636 virtual bool ItemChanged( const wxDataViewItem
& item
)
637 { return m_mainWindow
->ItemChanged(item
); }
638 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
639 { return m_mainWindow
->ValueChanged( item
, col
); }
640 virtual bool Cleared()
641 { return m_mainWindow
->Cleared(); }
642 virtual void Resort()
643 { m_mainWindow
->Resort(); }
645 wxDataViewMainWindow
*m_mainWindow
;
648 // ---------------------------------------------------------
649 // wxDataViewRenderer
650 // ---------------------------------------------------------
652 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
654 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
655 wxDataViewCellMode mode
,
657 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
661 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
665 wxDataViewRenderer::~wxDataViewRenderer()
670 wxDC
*wxDataViewRenderer::GetDC()
674 if (GetOwner() == NULL
)
676 if (GetOwner()->GetOwner() == NULL
)
678 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
684 void wxDataViewRenderer::SetAlignment( int align
)
689 int wxDataViewRenderer::GetAlignment() const
694 // ---------------------------------------------------------
695 // wxDataViewCustomRenderer
696 // ---------------------------------------------------------
698 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
700 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
701 wxDataViewCellMode mode
, int align
) :
702 wxDataViewRenderer( varianttype
, mode
, align
)
706 // ---------------------------------------------------------
707 // wxDataViewTextRenderer
708 // ---------------------------------------------------------
710 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
712 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
713 wxDataViewCellMode mode
, int align
) :
714 wxDataViewRenderer( varianttype
, mode
, align
)
718 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
720 m_text
= value
.GetString();
725 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
730 bool wxDataViewTextRenderer::HasEditorCtrl() const
735 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
736 wxRect labelRect
, const wxVariant
&value
)
738 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
739 wxPoint(labelRect
.x
,labelRect
.y
),
740 wxSize(labelRect
.width
,labelRect
.height
) );
742 // select the text in the control an place the cursor at the end
743 ctrl
->SetInsertionPointEnd();
749 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
751 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
752 value
= text
->GetValue();
756 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
758 RenderText(m_text
, 0, rect
, dc
, state
);
762 wxSize
wxDataViewTextRenderer::GetSize() const
765 return GetTextExtent(m_text
);
767 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
770 // ---------------------------------------------------------
771 // wxDataViewBitmapRenderer
772 // ---------------------------------------------------------
774 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
776 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
777 wxDataViewCellMode mode
, int align
) :
778 wxDataViewRenderer( varianttype
, mode
, align
)
782 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
784 if (value
.GetType() == wxT("wxBitmap"))
786 if (value
.GetType() == wxT("wxIcon"))
792 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
797 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
800 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
801 else if (m_icon
.Ok())
802 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
807 wxSize
wxDataViewBitmapRenderer::GetSize() const
810 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
811 else if (m_icon
.Ok())
812 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
814 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
817 // ---------------------------------------------------------
818 // wxDataViewToggleRenderer
819 // ---------------------------------------------------------
821 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
823 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
824 wxDataViewCellMode mode
, int align
) :
825 wxDataViewRenderer( varianttype
, mode
, align
)
830 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
832 m_toggle
= value
.GetBool();
837 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
842 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
846 flags
|= wxCONTROL_CHECKED
;
847 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
848 GetEnabled() == false)
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 if (model
->IsEnabled(item
, col
))
873 model
->ChangeValue(!valueOld
.GetBool(), item
, col
);
877 wxSize
wxDataViewToggleRenderer::GetSize() const
879 // the window parameter is not used by GetCheckBoxSize() so it's
881 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
884 // ---------------------------------------------------------
885 // wxDataViewProgressRenderer
886 // ---------------------------------------------------------
888 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
890 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
891 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
892 wxDataViewRenderer( varianttype
, mode
, align
)
898 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
900 m_value
= (long) value
;
902 if (m_value
< 0) m_value
= 0;
903 if (m_value
> 100) m_value
= 100;
908 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
910 value
= (long) m_value
;
915 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
917 // deflate the rect to leave a small border between bars in adjacent rows
918 wxRect bar
= rect
.Deflate(0, 1);
920 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
921 dc
->SetPen( *wxBLACK_PEN
);
922 dc
->DrawRectangle( bar
);
924 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
925 dc
->SetPen( *wxTRANSPARENT_PEN
);
927 const wxDataViewItemAttr
& attr
= GetAttr();
928 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
930 dc
->DrawRectangle( bar
);
935 wxSize
wxDataViewProgressRenderer::GetSize() const
937 return wxSize(40,12);
940 // ---------------------------------------------------------
941 // wxDataViewDateRenderer
942 // ---------------------------------------------------------
944 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
946 #if wxUSE_DATE_RENDERER_POPUP
948 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
951 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
952 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
953 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
958 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
959 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
960 sizer
->Add( m_cal
, 1, wxGROW
);
965 void OnCalendar( wxCalendarEvent
&event
);
967 wxCalendarCtrl
*m_cal
;
968 wxDataViewModel
*m_model
;
970 const wxDataViewItem
& m_item
;
973 virtual void OnDismiss()
978 DECLARE_EVENT_TABLE()
981 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
982 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
985 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
987 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
991 #endif // wxUSE_DATE_RENDERER_POPUP
993 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
995 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
996 wxDataViewCellMode mode
, int align
) :
997 wxDataViewRenderer( varianttype
, mode
, align
)
1001 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1003 m_date
= value
.GetDateTime();
1008 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1014 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1016 wxString tmp
= m_date
.FormatDate();
1017 RenderText( tmp
, 0, cell
, dc
, state
);
1021 wxSize
wxDataViewDateRenderer::GetSize() const
1023 return GetTextExtent(m_date
.FormatDate());
1026 void wxDataViewDateRenderer::WXOnActivate(wxDataViewModel
*model
,
1027 const wxVariant
& valueOld
,
1028 const wxDataViewItem
& item
,
1031 wxDateTime dtOld
= valueOld
.GetDateTime();
1033 #if wxUSE_DATE_RENDERER_POPUP
1034 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1035 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1036 wxPoint pos
= wxGetMousePosition();
1039 popup
->Popup( popup
->m_cal
);
1040 #else // !wxUSE_DATE_RENDERER_POPUP
1041 wxMessageBox(dtOld
.Format());
1042 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1045 // ---------------------------------------------------------
1046 // wxDataViewIconTextRenderer
1047 // ---------------------------------------------------------
1049 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1051 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1052 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1053 wxDataViewRenderer( varianttype
, mode
, align
)
1056 SetAlignment(align
);
1059 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1065 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1070 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1074 const wxIcon
& icon
= m_value
.GetIcon();
1077 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1078 xoffset
= icon
.GetWidth()+4;
1081 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1086 wxSize
wxDataViewIconTextRenderer::GetSize() const
1088 if (!m_value
.GetText().empty())
1090 wxSize size
= GetTextExtent(m_value
.GetText());
1092 if (m_value
.GetIcon().IsOk())
1093 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1096 return wxSize(80,20);
1099 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1101 wxDataViewIconText iconText
;
1104 wxString text
= iconText
.GetText();
1106 // adjust the label rect to take the width of the icon into account
1107 if (iconText
.GetIcon().IsOk())
1109 int w
= iconText
.GetIcon().GetWidth() + 4;
1111 labelRect
.width
-= w
;
1114 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1115 wxPoint(labelRect
.x
,labelRect
.y
),
1116 wxSize(labelRect
.width
,labelRect
.height
) );
1118 // select the text in the control an place the cursor at the end
1119 ctrl
->SetInsertionPointEnd();
1125 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
& value
)
1127 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1129 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1134 //-----------------------------------------------------------------------------
1135 // wxDataViewDropTarget
1136 //-----------------------------------------------------------------------------
1138 #if wxUSE_DRAG_AND_DROP
1140 class wxBitmapCanvas
: public wxWindow
1143 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1144 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1147 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1150 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1153 dc
.DrawBitmap( m_bitmap
, 0, 0);
1159 class wxDataViewDropSource
: public wxDropSource
1162 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1170 ~wxDataViewDropSource()
1175 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1177 wxPoint pos
= wxGetMousePosition();
1181 int liney
= m_win
->GetLineStart( m_row
);
1183 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1184 m_win
->ClientToScreen( &linex
, &liney
);
1185 m_dist_x
= pos
.x
- linex
;
1186 m_dist_y
= pos
.y
- liney
;
1189 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1191 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1192 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1194 wxFRAME_TOOL_WINDOW
|
1195 wxFRAME_FLOAT_ON_PARENT
|
1196 wxFRAME_NO_TASKBAR
|
1198 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1203 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1204 m_hint
->SetTransparent( 128 );
1210 wxDataViewMainWindow
*m_win
;
1213 int m_dist_x
,m_dist_y
;
1217 class wxDataViewDropTarget
: public wxDropTarget
1220 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1226 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1228 wxDataFormat format
= GetMatchingPair();
1229 if (format
== wxDF_INVALID
)
1231 return m_win
->OnDragOver( format
, x
, y
, def
);
1234 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1236 wxDataFormat format
= GetMatchingPair();
1237 if (format
== wxDF_INVALID
)
1239 return m_win
->OnDrop( format
, x
, y
);
1242 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1244 wxDataFormat format
= GetMatchingPair();
1245 if (format
== wxDF_INVALID
)
1249 return m_win
->OnData( format
, x
, y
, def
);
1252 virtual void OnLeave()
1253 { m_win
->OnLeave(); }
1255 wxDataViewMainWindow
*m_win
;
1258 #endif // wxUSE_DRAG_AND_DROP
1260 //-----------------------------------------------------------------------------
1261 // wxDataViewRenameTimer
1262 //-----------------------------------------------------------------------------
1264 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1269 void wxDataViewRenameTimer::Notify()
1271 m_owner
->OnRenameTimer();
1274 //-----------------------------------------------------------------------------
1275 // wxDataViewMainWindow
1276 //-----------------------------------------------------------------------------
1278 // The tree building helper, declared firstly
1279 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1280 wxDataViewTreeNode
* node
);
1282 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1284 if (row1
> row2
) return 1;
1285 if (row1
== row2
) return 0;
1289 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1291 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1292 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1293 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1294 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1295 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1296 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1299 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1300 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1301 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1302 m_selection( wxDataViewSelectionCmp
)
1307 m_lastOnSame
= false;
1308 m_renameTimer
= new wxDataViewRenameTimer( this );
1310 // TODO: user better initial values/nothing selected
1311 m_currentCol
= NULL
;
1314 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1316 #if wxUSE_DRAG_AND_DROP
1318 m_dragStart
= wxPoint(0,0);
1320 m_dragEnabled
= false;
1321 m_dropEnabled
= false;
1323 m_dropHintLine
= (unsigned int) -1;
1324 #endif // wxUSE_DRAG_AND_DROP
1326 m_lineLastClicked
= (unsigned int) -1;
1327 m_lineBeforeLastClicked
= (unsigned int) -1;
1328 m_lineSelectSingleOnUp
= (unsigned int) -1;
1332 SetBackgroundColour( *wxWHITE
);
1334 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1336 m_penRule
= wxPen(GetRuleColour());
1338 // compose a pen whichcan draw black lines
1339 // TODO: maybe there is something system colour to use
1340 m_penExpander
= wxPen(wxColour(0,0,0));
1342 m_root
= new wxDataViewTreeNode( NULL
);
1343 m_root
->SetHasChildren(true);
1345 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1347 m_underMouse
= NULL
;
1351 wxDataViewMainWindow::~wxDataViewMainWindow()
1354 delete m_renameTimer
;
1358 #if wxUSE_DRAG_AND_DROP
1359 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1361 m_dragFormat
= format
;
1362 m_dragEnabled
= format
!= wxDF_INVALID
;
1367 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1369 m_dropFormat
= format
;
1370 m_dropEnabled
= format
!= wxDF_INVALID
;
1373 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1378 void wxDataViewMainWindow::RemoveDropHint()
1383 RefreshRow( m_dropHintLine
);
1384 m_dropHintLine
= (unsigned int) -1;
1388 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1389 wxCoord y
, wxDragResult def
)
1393 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1394 unsigned int row
= GetLineAt( yy
);
1396 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1402 wxDataViewItem item
= GetItemByRow( row
);
1404 wxDataViewModel
*model
= GetOwner()->GetModel();
1406 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1407 event
.SetEventObject( m_owner
);
1408 event
.SetItem( item
);
1409 event
.SetModel( model
);
1410 event
.SetDataFormat( format
);
1411 if (!m_owner
->HandleWindowEvent( event
))
1417 if (!event
.IsAllowed())
1424 if (m_dropHint
&& (row
!= m_dropHintLine
))
1425 RefreshRow( m_dropHintLine
);
1427 m_dropHintLine
= row
;
1433 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1439 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1440 unsigned int row
= GetLineAt( yy
);
1442 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1445 wxDataViewItem item
= GetItemByRow( row
);
1447 wxDataViewModel
*model
= GetOwner()->GetModel();
1449 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1450 event
.SetEventObject( m_owner
);
1451 event
.SetItem( item
);
1452 event
.SetModel( model
);
1453 event
.SetDataFormat( format
);
1454 if (!m_owner
->HandleWindowEvent( event
))
1457 if (!event
.IsAllowed())
1463 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1468 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1469 unsigned int row
= GetLineAt( yy
);
1471 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1474 wxDataViewItem item
= GetItemByRow( row
);
1476 wxDataViewModel
*model
= GetOwner()->GetModel();
1478 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1480 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1481 event
.SetEventObject( m_owner
);
1482 event
.SetItem( item
);
1483 event
.SetModel( model
);
1484 event
.SetDataFormat( format
);
1485 event
.SetDataSize( obj
->GetSize() );
1486 event
.SetDataBuffer( obj
->GetData() );
1487 if (!m_owner
->HandleWindowEvent( event
))
1490 if (!event
.IsAllowed())
1496 void wxDataViewMainWindow::OnLeave()
1501 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1503 int height
= GetLineHeight( row
);
1505 unsigned int cols
= GetOwner()->GetColumnCount();
1507 for (col
= 0; col
< cols
; col
++)
1509 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1510 if (column
->IsHidden())
1511 continue; // skip it!
1512 width
+= column
->GetWidth();
1518 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1519 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1520 indent
= indent
+ m_lineHeight
;
1521 // try to use the m_lineHeight as the expander space
1523 if(!node
->HasChildren())
1528 wxBitmap
bitmap( width
, height
);
1529 wxMemoryDC
dc( bitmap
);
1530 dc
.SetFont( GetFont() );
1531 dc
.SetPen( *wxBLACK_PEN
);
1532 dc
.SetBrush( *wxWHITE_BRUSH
);
1533 dc
.DrawRectangle( 0,0,width
,height
);
1535 wxDataViewModel
*model
= m_owner
->GetModel();
1537 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1540 // TODO-RTL: last column for RTL support
1541 expander
= GetOwner()->GetColumnAt( 0 );
1542 GetOwner()->SetExpanderColumn(expander
);
1547 for (col
= 0; col
< cols
; col
++)
1549 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1550 wxDataViewRenderer
*cell
= column
->GetRenderer();
1552 if (column
->IsHidden())
1553 continue; // skip it!
1555 width
= column
->GetWidth();
1557 if (column
== expander
)
1560 wxDataViewItem item
= GetItemByRow( row
);
1561 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1563 wxRect
item_rect(x
, 0, width
, height
);
1564 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1566 // dc.SetClippingRegion( item_rect );
1567 cell
->WXCallRender(item_rect
, &dc
, 0);
1568 // dc.DestroyClippingRegion();
1576 #endif // wxUSE_DRAG_AND_DROP
1579 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1581 wxDataViewModel
*model
= GetOwner()->GetModel();
1582 wxAutoBufferedPaintDC
dc( this );
1585 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1586 dc
.SetPen( *wxTRANSPARENT_PEN
);
1587 dc
.DrawRectangle(GetClientSize());
1591 GetOwner()->PrepareDC( dc
);
1592 dc
.SetFont( GetFont() );
1594 wxRect update
= GetUpdateRegion().GetBox();
1595 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1597 // compute which items needs to be redrawn
1598 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1599 unsigned int item_count
=
1600 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1601 (int)(GetRowCount( ) - item_start
));
1602 unsigned int item_last
= item_start
+ item_count
;
1603 // Get the parent of DataViewCtrl
1604 wxWindow
*parent
= GetParent()->GetParent();
1605 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1606 cache_event
.SetEventObject(GetParent());
1607 cache_event
.SetCache(item_start
, item_last
- 1);
1608 parent
->ProcessWindowEvent(cache_event
);
1610 // compute which columns needs to be redrawn
1611 unsigned int cols
= GetOwner()->GetColumnCount();
1614 // we assume that we have at least one column below and painting an
1615 // empty control is unnecessary anyhow
1619 unsigned int col_start
= 0;
1620 unsigned int x_start
;
1621 for (x_start
= 0; col_start
< cols
; col_start
++)
1623 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1624 if (col
->IsHidden())
1625 continue; // skip it!
1627 unsigned int w
= col
->GetWidth();
1628 if (x_start
+w
>= (unsigned int)update
.x
)
1634 unsigned int col_last
= col_start
;
1635 unsigned int x_last
= x_start
;
1636 for (; col_last
< cols
; col_last
++)
1638 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1639 if (col
->IsHidden())
1640 continue; // skip it!
1642 if (x_last
> (unsigned int)update
.GetRight())
1645 x_last
+= col
->GetWidth();
1648 // Draw horizontal rules if required
1649 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1651 dc
.SetPen(m_penRule
);
1652 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1654 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1656 int y
= GetLineStart( i
);
1657 dc
.DrawLine(x_start
, y
, x_last
, y
);
1661 // Draw vertical rules if required
1662 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1664 dc
.SetPen(m_penRule
);
1665 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1668 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1670 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1671 if (col
->IsHidden())
1672 continue; // skip it
1674 dc
.DrawLine(x
, GetLineStart( item_start
),
1675 x
, GetLineStart( item_last
) );
1677 x
+= col
->GetWidth();
1680 // Draw last vertical rule
1681 dc
.DrawLine(x
, GetLineStart( item_start
),
1682 x
, GetLineStart( item_last
) );
1685 // redraw the background for the items which are selected/current
1686 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1688 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1689 if (selected
|| item
== m_currentRow
)
1691 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1692 if (item
== m_currentRow
)
1693 flags
|= wxCONTROL_CURRENT
;
1695 flags
|= wxCONTROL_FOCUSED
;
1697 wxRect
rect( x_start
, GetLineStart( item
),
1698 x_last
- x_start
, GetLineHeight( item
) );
1699 wxRendererNative::Get().DrawItemSelectionRect
1709 #if wxUSE_DRAG_AND_DROP
1712 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1713 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1714 dc
.SetPen( *wxBLACK_PEN
);
1715 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1716 dc
.DrawRectangle( rect
);
1718 #endif // wxUSE_DRAG_AND_DROP
1720 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1723 // TODO-RTL: last column for RTL support
1724 expander
= GetOwner()->GetColumnAt( 0 );
1725 GetOwner()->SetExpanderColumn(expander
);
1728 // redraw all cells for all rows which must be repainted and all columns
1730 cell_rect
.x
= x_start
;
1731 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1733 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1734 wxDataViewRenderer
*cell
= col
->GetRenderer();
1735 cell_rect
.width
= col
->GetWidth();
1737 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1738 continue; // skip it!
1740 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1742 // get the cell value and set it into the renderer
1743 wxDataViewTreeNode
*node
= NULL
;
1744 wxDataViewItem dataitem
;
1746 if (!IsVirtualList())
1748 node
= GetTreeNodeByRow(item
);
1752 dataitem
= node
->GetItem();
1754 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1755 !model
->HasContainerColumns(dataitem
))
1760 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1763 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1766 cell_rect
.y
= GetLineStart( item
);
1767 cell_rect
.height
= GetLineHeight( item
);
1769 // deal with the expander
1771 if ((!IsList()) && (col
== expander
))
1773 // Calculate the indent first
1774 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1776 // we reserve m_lineHeight of horizontal space for the expander
1777 // but leave EXPANDER_MARGIN around the expander itself
1778 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1780 indent
+= m_lineHeight
;
1782 // draw expander if needed and visible
1783 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1785 dc
.SetPen( m_penExpander
);
1786 dc
.SetBrush( wxNullBrush
);
1788 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1789 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1790 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1792 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1795 if ( m_underMouse
== node
)
1796 flag
|= wxCONTROL_CURRENT
;
1797 if ( node
->IsOpen() )
1798 flag
|= wxCONTROL_EXPANDED
;
1800 // ensure that we don't overflow the cell (which might
1801 // happen if the column is very narrow)
1802 wxDCClipper
clip(dc
, cell_rect
);
1804 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1807 // force the expander column to left-center align
1808 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1810 if (node
&& !node
->HasChildren())
1812 // Yes, if the node does not have any child, it must be a leaf which
1813 // mean that it is a temporarily created by GetTreeNodeByRow
1817 wxRect item_rect
= cell_rect
;
1818 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1820 // account for the tree indent (harmless if we're not indented)
1821 item_rect
.x
+= indent
;
1822 item_rect
.width
-= indent
;
1824 if ( item_rect
.width
<= 0 )
1828 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1829 state
|= wxDATAVIEW_CELL_SELECTED
;
1831 // TODO: it would be much more efficient to create a clipping
1832 // region for the entire column being rendered (in the OnPaint
1833 // of wxDataViewMainWindow) instead of a single clip region for
1834 // each cell. However it would mean that each renderer should
1835 // respect the given wxRect's top & bottom coords, eventually
1836 // violating only the left & right coords - however the user can
1837 // make its own renderer and thus we cannot be sure of that.
1838 wxDCClipper
clip(dc
, item_rect
);
1840 cell
->WXCallRender(item_rect
, &dc
, state
);
1843 cell_rect
.x
+= cell_rect
.width
;
1847 void wxDataViewMainWindow::OnRenameTimer()
1849 // We have to call this here because changes may just have
1850 // been made and no screen update taken place.
1853 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1854 // (needs to be tested!)
1858 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1860 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1862 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1865 //-----------------------------------------------------------------------------
1866 // Helper class for do operation on the tree node
1867 //-----------------------------------------------------------------------------
1872 virtual ~DoJob() { }
1874 // The return value control how the tree-walker tranverse the tree
1875 // 0: Job done, stop tranverse and return
1876 // 1: Ignore the current node's subtree and continue
1877 // 2: Job not done, continue
1878 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1879 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1880 virtual int operator() ( void * n
) = 0;
1883 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1888 switch( func( node
) )
1899 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
1900 const wxDataViewTreeLeaves
& leaves
= node
->GetChildren();
1902 int len_nodes
= nodes
.GetCount();
1903 int len
= leaves
.GetCount();
1904 int i
= 0, nodes_i
= 0;
1906 for(; i
< len
; i
++ )
1908 void * n
= leaves
[i
];
1909 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1911 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1914 if( Walker( nd
, func
) )
1933 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1935 GetOwner()->InvalidateColBestWidths();
1937 if (IsVirtualList())
1939 wxDataViewVirtualListModel
*list_model
=
1940 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1941 m_count
= list_model
->GetCount();
1948 wxDataViewTreeNode
* node
;
1949 node
= FindNode(parent
);
1954 node
->SetHasChildren( true );
1956 if( g_model
->IsContainer( item
) )
1958 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1959 newnode
->SetItem(item
);
1960 newnode
->SetHasChildren( true );
1961 node
->AddNode( newnode
);
1964 node
->AddLeaf( item
.GetID() );
1966 node
->ChangeSubTreeCount(1);
1974 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1976 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1977 const wxDataViewItem
& item
)
1979 GetOwner()->InvalidateColBestWidths();
1981 if (IsVirtualList())
1983 wxDataViewVirtualListModel
*list_model
=
1984 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1985 m_count
= list_model
->GetCount();
1987 if( m_currentRow
> GetRowCount() )
1988 m_currentRow
= m_count
- 1;
1990 // TODO: why empty the entire selection?
1991 m_selection
.Empty();
1998 wxDataViewTreeNode
* node
= FindNode(parent
);
2000 // Notice that it is possible that the item being deleted is not in the
2001 // tree at all, for example we could be deleting a never shown (because
2002 // collapsed) item in a tree model. So it's not an error if we don't know
2003 // about this item, just return without doing anything then.
2004 if ( !node
|| node
->GetChildren().Index(item
.GetID()) == wxNOT_FOUND
)
2008 node
->GetChildren().Remove( item
.GetID() );
2009 // Manipolate selection
2010 if( m_selection
.GetCount() > 1 )
2012 m_selection
.Empty();
2014 bool isContainer
= false;
2015 wxDataViewTreeNodes nds
= node
->GetNodes();
2016 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2018 if (nds
[i
]->GetItem() == item
)
2026 wxDataViewTreeNode
* n
= NULL
;
2027 wxDataViewTreeNodes nodes
= node
->GetNodes();
2028 int len
= nodes
.GetCount();
2029 for( int i
= 0; i
< len
; i
++)
2031 if( nodes
[i
]->GetItem() == item
)
2038 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2040 node
->GetNodes().Remove( n
);
2041 sub
-= n
->GetSubTreeCount();
2042 ::DestroyTreeHelper(n
);
2044 // Make the row number invalid and get a new valid one when user call GetRowCount
2046 node
->ChangeSubTreeCount(sub
);
2048 // Change the current row to the last row if the current exceed the max row number
2049 if( m_currentRow
> GetRowCount() )
2050 m_currentRow
= m_count
- 1;
2057 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2059 GetOwner()->InvalidateColBestWidths();
2065 wxWindow
*parent
= GetParent();
2066 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2067 le
.SetEventObject(parent
);
2068 le
.SetModel(GetOwner()->GetModel());
2070 parent
->GetEventHandler()->ProcessEvent(le
);
2075 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2077 int view_column
= -1;
2078 unsigned int n_col
= m_owner
->GetColumnCount();
2079 for (unsigned i
= 0; i
< n_col
; i
++)
2081 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2082 if (column
->GetModelColumn() == model_column
)
2084 view_column
= (int) i
;
2088 if (view_column
== -1)
2091 GetOwner()->InvalidateColBestWidth(view_column
);
2093 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2094 /*#define MAX_VIRTUAL_WIDTH 100000
2096 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2097 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2098 Refresh( true, &rect );
2106 wxWindow
*parent
= GetParent();
2107 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2108 le
.SetEventObject(parent
);
2109 le
.SetModel(GetOwner()->GetModel());
2111 le
.SetColumn(view_column
);
2112 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2113 parent
->GetEventHandler()->ProcessEvent(le
);
2118 bool wxDataViewMainWindow::Cleared()
2120 GetOwner()->InvalidateColBestWidths();
2123 m_selection
.Clear();
2126 BuildTree( GetOwner()->GetModel() );
2133 void wxDataViewMainWindow::UpdateDisplay()
2136 m_underMouse
= NULL
;
2139 void wxDataViewMainWindow::OnInternalIdle()
2141 wxWindow::OnInternalIdle();
2145 RecalculateDisplay();
2150 void wxDataViewMainWindow::RecalculateDisplay()
2152 wxDataViewModel
*model
= GetOwner()->GetModel();
2159 int width
= GetEndOfLastCol();
2160 int height
= GetLineStart( GetRowCount() );
2162 SetVirtualSize( width
, height
);
2163 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2168 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2170 m_underMouse
= NULL
;
2172 wxWindow::ScrollWindow( dx
, dy
, rect
);
2174 if (GetOwner()->m_headerArea
)
2175 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2178 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2180 m_underMouse
= NULL
;
2183 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2184 int sy
= GetLineStart( rows
)/y
;
2188 wxRect rect
= GetClientRect();
2192 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2193 for (x_start
= 0; colnum
< column
; colnum
++)
2195 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2196 if (col
->IsHidden())
2197 continue; // skip it!
2199 w
= col
->GetWidth();
2203 int x_end
= x_start
+ w
;
2204 xe
= xx
+ rect
.width
;
2207 sx
= ( xx
+ x_end
- xe
)/x
;
2214 m_owner
->Scroll( sx
, sy
);
2217 int wxDataViewMainWindow::GetCountPerPage() const
2219 wxSize size
= GetClientSize();
2220 return size
.y
/ m_lineHeight
;
2223 int wxDataViewMainWindow::GetEndOfLastCol() const
2227 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2229 const wxDataViewColumn
*c
=
2230 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2233 width
+= c
->GetWidth();
2238 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2242 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2244 return GetLineAt( y
);
2247 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2249 wxSize client_size
= GetClientSize();
2250 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2251 &client_size
.x
, &client_size
.y
);
2253 // we should deal with the pixel here
2254 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2256 return wxMin( GetRowCount()-1, row
);
2259 unsigned int wxDataViewMainWindow::GetRowCount()
2261 if ( m_count
== -1 )
2263 m_count
= RecalculateCount();
2269 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2276 void wxDataViewMainWindow::SelectAllRows( bool on
)
2283 m_selection
.Clear();
2284 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2285 m_selection
.Add( i
);
2290 unsigned int first_visible
= GetFirstVisibleRow();
2291 unsigned int last_visible
= GetLastVisibleRow();
2293 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2295 unsigned int row
= m_selection
[i
];
2296 if ((row
>= first_visible
) && (row
<= last_visible
))
2299 m_selection
.Clear();
2303 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2305 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2309 m_selection
.Add( row
);
2317 m_selection
.Remove( row
);
2323 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2327 unsigned int tmp
= from
;
2333 for (i
= from
; i
<= to
; i
++)
2335 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2338 m_selection
.Add( i
);
2343 m_selection
.Remove( i
);
2346 RefreshRows( from
, to
);
2349 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2351 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2353 int n
= aSelections
[i
];
2355 m_selection
.Add( n
);
2360 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2362 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2363 m_selection
.Add( row
);
2365 m_selection
.Remove( row
);
2369 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2371 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2374 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2376 wxWindow
*parent
= GetParent();
2377 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2379 le
.SetEventObject(parent
);
2380 le
.SetModel(GetOwner()->GetModel());
2383 parent
->GetEventHandler()->ProcessEvent(le
);
2386 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2388 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2389 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2391 wxSize client_size
= GetClientSize();
2392 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2393 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2394 if (intersect_rect
.width
> 0)
2395 Refresh( true, &intersect_rect
);
2398 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2402 unsigned int tmp
= to
;
2407 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2408 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2410 wxSize client_size
= GetClientSize();
2411 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2412 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2413 if (intersect_rect
.width
> 0)
2414 Refresh( true, &intersect_rect
);
2417 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2419 wxSize client_size
= GetClientSize();
2420 int start
= GetLineStart( firstRow
);
2421 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2422 if (start
> client_size
.y
) return;
2424 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2426 Refresh( true, &rect
);
2429 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2431 wxCHECK_RET( newCurrent
< GetRowCount(),
2432 wxT("invalid item index in OnArrowChar()") );
2434 // if there is no selection, we cannot move it anywhere
2435 if (!HasCurrentRow())
2438 unsigned int oldCurrent
= m_currentRow
;
2440 // in single selection we just ignore Shift as we can't select several
2442 if ( event
.ShiftDown() && !IsSingleSel() )
2444 RefreshRow( oldCurrent
);
2446 ChangeCurrentRow( newCurrent
);
2448 // select all the items between the old and the new one
2449 if ( oldCurrent
> newCurrent
)
2451 newCurrent
= oldCurrent
;
2452 oldCurrent
= m_currentRow
;
2455 SelectRows( oldCurrent
, newCurrent
, true );
2456 if (oldCurrent
!=newCurrent
)
2457 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2461 RefreshRow( oldCurrent
);
2463 // all previously selected items are unselected unless ctrl is held
2464 if ( !event
.ControlDown() )
2465 SelectAllRows(false);
2467 ChangeCurrentRow( newCurrent
);
2469 if ( !event
.ControlDown() )
2471 SelectRow( m_currentRow
, true );
2472 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2475 RefreshRow( m_currentRow
);
2478 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2481 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2485 rect
.y
= GetLineStart( row
);
2486 rect
.width
= GetEndOfLastCol();
2487 rect
.height
= GetLineHeight( row
);
2492 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2494 const wxDataViewModel
*model
= GetOwner()->GetModel();
2496 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2498 // TODO make more efficient
2503 for (r
= 0; r
< row
; r
++)
2505 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2506 if (!node
) return start
;
2508 wxDataViewItem item
= node
->GetItem();
2510 if (node
&& !node
->HasChildren())
2512 // Yes, if the node does not have any child, it must be a leaf which
2513 // mean that it is a temporarily created by GetTreeNodeByRow
2517 unsigned int cols
= GetOwner()->GetColumnCount();
2519 int height
= m_lineHeight
;
2520 for (col
= 0; col
< cols
; col
++)
2522 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2523 if (column
->IsHidden())
2524 continue; // skip it!
2527 model
->IsContainer(item
) &&
2528 !model
->HasContainerColumns(item
))
2529 continue; // skip it!
2531 wxDataViewRenderer
*renderer
=
2532 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2533 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2535 height
= wxMax( height
, renderer
->GetSize().y
);
2545 return row
* m_lineHeight
;
2549 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2551 const wxDataViewModel
*model
= GetOwner()->GetModel();
2553 // check for the easy case first
2554 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2555 return y
/ m_lineHeight
;
2557 // TODO make more efficient
2558 unsigned int row
= 0;
2559 unsigned int yy
= 0;
2562 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2565 // not really correct...
2566 return row
+ ((y
-yy
) / m_lineHeight
);
2569 wxDataViewItem item
= node
->GetItem();
2571 if (node
&& !node
->HasChildren())
2573 // Yes, if the node does not have any child, it must be a leaf which
2574 // mean that it is a temporarily created by GetTreeNodeByRow
2578 unsigned int cols
= GetOwner()->GetColumnCount();
2580 int height
= m_lineHeight
;
2581 for (col
= 0; col
< cols
; col
++)
2583 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2584 if (column
->IsHidden())
2585 continue; // skip it!
2588 model
->IsContainer(item
) &&
2589 !model
->HasContainerColumns(item
))
2590 continue; // skip it!
2592 wxDataViewRenderer
*renderer
=
2593 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2594 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2596 height
= wxMax( height
, renderer
->GetSize().y
);
2607 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2609 const wxDataViewModel
*model
= GetOwner()->GetModel();
2611 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2613 wxASSERT( !IsVirtualList() );
2615 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2616 // wxASSERT( node );
2617 if (!node
) return m_lineHeight
;
2619 wxDataViewItem item
= node
->GetItem();
2621 if (node
&& !node
->HasChildren())
2623 // Yes, if the node does not have any child, it must be a leaf which
2624 // mean that it is a temporarily created by GetTreeNodeByRow
2628 int height
= m_lineHeight
;
2630 unsigned int cols
= GetOwner()->GetColumnCount();
2632 for (col
= 0; col
< cols
; col
++)
2634 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2635 if (column
->IsHidden())
2636 continue; // skip it!
2639 model
->IsContainer(item
) &&
2640 !model
->HasContainerColumns(item
))
2641 continue; // skip it!
2643 wxDataViewRenderer
*renderer
=
2644 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2645 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2647 height
= wxMax( height
, renderer
->GetSize().y
);
2654 return m_lineHeight
;
2658 class RowToItemJob
: public DoJob
2661 RowToItemJob( unsigned int row
, int current
)
2662 { this->row
= row
; this->current
= current
; }
2663 virtual ~RowToItemJob() {}
2665 virtual int operator() ( wxDataViewTreeNode
* node
)
2668 if( current
== static_cast<int>(row
))
2670 ret
= node
->GetItem();
2674 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2676 current
+= node
->GetSubTreeCount();
2681 // If the current has no child node, we can find the desired item of the row
2683 // This if can speed up finding in some case, and will has a very good effect
2684 // when it comes to list view
2685 if( node
->GetNodes().GetCount() == 0)
2687 int index
= static_cast<int>(row
) - current
- 1;
2688 ret
= node
->GetChildren().Item( index
);
2695 virtual int operator() ( void * n
)
2698 if( current
== static_cast<int>(row
))
2700 ret
= wxDataViewItem( n
);
2706 wxDataViewItem
GetResult() const
2715 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2717 if (IsVirtualList())
2719 return wxDataViewItem( wxUIntToPtr(row
+1) );
2723 RowToItemJob
job( row
, -2 );
2724 Walker( m_root
, job
);
2725 return job
.GetResult();
2729 class RowToTreeNodeJob
: public DoJob
2732 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2735 this->current
= current
;
2739 virtual ~RowToTreeNodeJob(){ }
2741 virtual int operator() ( wxDataViewTreeNode
* node
)
2744 if( current
== static_cast<int>(row
))
2750 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2752 current
+= node
->GetSubTreeCount();
2759 // If the current node has no children, we can find the desired item of the
2760 // row number directly.
2761 // This if can speed up finding in some case, and will have a very good
2762 // effect for list views.
2763 if( node
->GetNodes().GetCount() == 0)
2765 int index
= static_cast<int>(row
) - current
- 1;
2766 void * n
= node
->GetChildren().Item( index
);
2767 ret
= new wxDataViewTreeNode( parent
);
2768 ret
->SetItem( wxDataViewItem( n
));
2769 ret
->SetHasChildren(false);
2776 virtual int operator() ( void * n
)
2779 if( current
== static_cast<int>(row
))
2781 ret
= new wxDataViewTreeNode( parent
);
2782 ret
->SetItem( wxDataViewItem( n
));
2783 ret
->SetHasChildren(false);
2790 wxDataViewTreeNode
* GetResult() const
2796 wxDataViewTreeNode
* ret
;
2797 wxDataViewTreeNode
* parent
;
2800 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2802 wxASSERT( !IsVirtualList() );
2804 RowToTreeNodeJob
job( row
, -2, m_root
);
2805 Walker( m_root
, job
);
2806 return job
.GetResult();
2809 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2810 const wxDataViewItem
& item
)
2812 wxWindow
*parent
= GetParent();
2813 wxDataViewEvent
le(type
, parent
->GetId());
2815 le
.SetEventObject(parent
);
2816 le
.SetModel(GetOwner()->GetModel());
2819 parent
->GetEventHandler()->ProcessEvent(le
);
2823 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2828 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2832 if (!node
->HasChildren())
2838 return node
->IsOpen();
2841 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2846 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2850 if (!node
->HasChildren())
2859 void wxDataViewMainWindow::Expand( unsigned int row
)
2864 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2868 if (!node
->HasChildren())
2874 if (!node
->IsOpen())
2877 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2879 // Check if the user prevent expanding
2880 if( e
.GetSkipped() )
2885 // build the children of current node
2886 if( node
->GetChildrenNumber() == 0 )
2889 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2892 // By expanding the node all row indices that are currently in the selection list
2893 // and are greater than our node have become invalid. So we have to correct that now.
2894 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2895 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2897 const unsigned testRow
= m_selection
[i
];
2898 // all rows above us are not affected, so skip them
2902 m_selection
[i
] += rowAdjustment
;
2905 if(m_currentRow
> row
)
2906 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2910 // Send the expanded event
2911 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2915 void wxDataViewMainWindow::Collapse(unsigned int row
)
2920 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2924 if (!node
->HasChildren())
2933 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2934 if( e
.GetSkipped() )
2937 // Find out if there are selected items below the current node.
2938 bool selectCollapsingRow
= false;
2939 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2940 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2941 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2943 const unsigned testRow
= m_selection
[i
];
2944 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2946 selectCollapsingRow
= true;
2947 // get out as soon as we have found a node that is selected
2954 // If the node to be closed has selected items the user won't see those any longer.
2955 // We select the collapsing node in this case.
2956 if(selectCollapsingRow
)
2958 SelectAllRows(false);
2959 ChangeCurrentRow(row
);
2960 SelectRow(row
, true);
2961 SendSelectionChangedEvent(GetItemByRow(row
));
2965 // if there were no selected items below our node we still need to "fix" the
2966 // selection list to adjust for the changing of the row indices.
2967 // We actually do the opposite of what we are doing in Expand().
2968 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2970 const unsigned testRow
= m_selection
[i
];
2971 // all rows above us are not affected, so skip them
2975 m_selection
[i
] -= rowAdjustment
;
2978 // if the "current row" is being collapsed away we change it to the current row ;-)
2979 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2980 ChangeCurrentRow(row
);
2981 else if(m_currentRow
> row
)
2982 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2987 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2991 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2993 const wxDataViewModel
* model
= GetOwner()->GetModel();
3000 // Compose the parent-chain for the item we are looking for
3001 wxVector
<wxDataViewItem
> parentChain
;
3002 wxDataViewItem
it( item
);
3005 parentChain
.push_back(it
);
3006 it
= model
->GetParent(it
);
3009 // Find the item along the parent-chain.
3010 // This algorithm is designed to speed up the node-finding method
3011 wxDataViewTreeNode
* node
= m_root
;
3012 for( unsigned iter
= parentChain
.size()-1; iter
>=0; --iter
)
3014 if( node
->HasChildren() )
3016 if( node
->GetChildrenNumber() == 0 )
3019 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3022 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3025 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3027 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3028 if (currentNode
->GetItem() == parentChain
[iter
])
3030 if (currentNode
->GetItem() == item
)
3047 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3048 wxDataViewColumn
* &column
)
3050 wxDataViewColumn
*col
= NULL
;
3051 unsigned int cols
= GetOwner()->GetColumnCount();
3052 unsigned int colnum
= 0;
3054 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3055 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3057 col
= GetOwner()->GetColumnAt(colnum
);
3058 if (col
->IsHidden())
3059 continue; // skip it!
3061 unsigned int w
= col
->GetWidth();
3062 if (x_start
+w
>= (unsigned int)x
)
3069 item
= GetItemByRow( GetLineAt( y
) );
3072 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3073 const wxDataViewColumn
* column
)
3078 unsigned int cols
= GetOwner()->GetColumnCount();
3079 // If column is null the loop will compute the combined width of all columns.
3080 // Otherwise, it will compute the x position of the column we are looking for.
3081 for (unsigned int i
= 0; i
< cols
; i
++)
3083 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3088 if (col
->IsHidden())
3089 continue; // skip it!
3091 xpos
+= col
->GetWidth();
3092 width
+= col
->GetWidth();
3097 // If we have a column, we need can get its width directly.
3098 if(column
->IsHidden())
3101 width
= column
->GetWidth();
3106 // If we have no column, we reset the x position back to zero.
3110 // we have to take an expander column into account and compute its indentation
3111 // to get the correct x position where the actual text is
3113 int row
= GetRowByItem(item
);
3114 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3116 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3117 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3118 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3120 if(!node
->HasChildren())
3124 wxRect
itemRect( xpos
+ indent
,
3125 GetLineStart( row
),
3127 GetLineHeight( row
) );
3129 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3130 &itemRect
.x
, &itemRect
.y
);
3135 int wxDataViewMainWindow::RecalculateCount()
3137 if (IsVirtualList())
3139 wxDataViewVirtualListModel
*list_model
=
3140 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3142 return list_model
->GetCount();
3146 return m_root
->GetSubTreeCount();
3150 class ItemToRowJob
: public DoJob
3153 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3160 // Maybe binary search will help to speed up this process
3161 virtual int operator() ( wxDataViewTreeNode
* node
)
3164 if( node
->GetItem() == item
)
3169 if( node
->GetItem() == *m_iter
)
3176 ret
+= node
->GetSubTreeCount();
3182 virtual int operator() ( void * n
)
3185 if( n
== item
.GetID() )
3190 // the row number is begin from zero
3191 int GetResult() const
3195 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3196 wxDataViewItem item
;
3201 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3203 const wxDataViewModel
* model
= GetOwner()->GetModel();
3207 if (IsVirtualList())
3209 return wxPtrToUInt( item
.GetID() ) -1;
3216 // Compose the parent-chain of the item we are looking for
3217 wxVector
<wxDataViewItem
> parentChain
;
3218 wxDataViewItem
it( item
);
3221 parentChain
.push_back(it
);
3222 it
= model
->GetParent(it
);
3225 // add an 'invalid' item to represent our 'invisible' root node
3226 parentChain
.push_back(wxDataViewItem());
3228 // the parent chain was created by adding the deepest parent first.
3229 // so if we want to start at the root node, we have to iterate backwards through the vector
3230 ItemToRowJob
job( item
, parentChain
.rbegin() );
3231 Walker( m_root
, job
);
3232 return job
.GetResult();
3236 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3237 wxDataViewTreeNode
* node
)
3239 if( !model
->IsContainer( item
) )
3242 wxDataViewItemArray children
;
3243 unsigned int num
= model
->GetChildren( item
, children
);
3245 unsigned int index
= 0;
3246 while( index
< num
)
3248 if( model
->IsContainer( children
[index
] ) )
3250 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3251 n
->SetItem(children
[index
]);
3252 n
->SetHasChildren( true );
3257 node
->AddLeaf( children
[index
].GetID() );
3261 node
->SetSubTreeCount( num
);
3262 wxDataViewTreeNode
* n
= node
->GetParent();
3264 n
->ChangeSubTreeCount(num
);
3268 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3272 if (GetOwner()->GetModel()->IsVirtualListModel())
3278 m_root
= new wxDataViewTreeNode( NULL
);
3279 m_root
->SetHasChildren(true);
3281 // First we define a invalid item to fetch the top-level elements
3282 wxDataViewItem item
;
3284 BuildTreeHelper( model
, item
, m_root
);
3288 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3290 if( node
->GetNodeNumber() != 0 )
3292 int len
= node
->GetNodeNumber();
3293 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3294 for (int i
= 0; i
< len
; i
++)
3295 DestroyTreeHelper(nodes
[i
]);
3300 void wxDataViewMainWindow::DestroyTree()
3302 if (!IsVirtualList())
3304 ::DestroyTreeHelper(m_root
);
3310 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3312 wxWindow
* const parent
= GetParent();
3314 // propagate the char event upwards
3315 wxKeyEvent
eventForParent(event
);
3316 eventForParent
.SetEventObject(parent
);
3317 if ( parent
->ProcessWindowEvent(eventForParent
) )
3320 if ( parent
->HandleAsNavigationKey(event
) )
3323 // no item -> nothing to do
3324 if (!HasCurrentRow())
3330 // don't use m_linesPerPage directly as it might not be computed yet
3331 const int pageSize
= GetCountPerPage();
3332 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3334 switch ( event
.GetKeyCode() )
3338 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3340 le
.SetItem( GetItemByRow(m_currentRow
) );
3341 le
.SetEventObject(parent
);
3342 le
.SetModel(GetOwner()->GetModel());
3344 parent
->GetEventHandler()->ProcessEvent(le
);
3349 if ( m_currentRow
> 0 )
3350 OnArrowChar( m_currentRow
- 1, event
);
3354 if ( m_currentRow
< GetRowCount() - 1 )
3355 OnArrowChar( m_currentRow
+ 1, event
);
3357 // Add the process for tree expanding/collapsing
3363 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3367 if (node
->HasChildren() && node
->IsOpen())
3369 Collapse(m_currentRow
);
3371 else // if the node is already closed we move the selection to its parent
3373 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3375 if(!node
->HasChildren())
3380 int parent
= GetRowByItem( parent_node
->GetItem() );
3383 unsigned int row
= m_currentRow
;
3384 SelectRow( row
, false);
3385 SelectRow( parent
, true );
3386 ChangeCurrentRow( parent
);
3387 GetOwner()->EnsureVisible( parent
, -1 );
3388 SendSelectionChangedEvent( parent_node
->GetItem() );
3396 if (!IsExpanded( m_currentRow
))
3397 Expand( m_currentRow
);
3400 unsigned int row
= m_currentRow
;
3401 SelectRow( row
, false );
3402 SelectRow( row
+ 1, true );
3403 ChangeCurrentRow( row
+ 1 );
3404 GetOwner()->EnsureVisible( row
+ 1, -1 );
3405 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3412 OnArrowChar( GetRowCount() - 1, event
);
3417 OnArrowChar( 0, event
);
3422 int steps
= pageSize
- 1;
3423 int index
= m_currentRow
- steps
;
3427 OnArrowChar( index
, event
);
3433 int steps
= pageSize
- 1;
3434 unsigned int index
= m_currentRow
+ steps
;
3435 unsigned int count
= GetRowCount();
3436 if ( index
>= count
)
3439 OnArrowChar( index
, event
);
3445 if(m_selection
.size() == 1)
3447 // TODO: we need to revise that when we have a concept for a 'current column'
3448 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3458 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3460 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3462 // let the base handle mouse wheel events.
3467 // set the focus to ourself if any of the mouse buttons are pressed
3468 if(event
.ButtonDown() && !HasFocus())
3471 int x
= event
.GetX();
3472 int y
= event
.GetY();
3473 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3474 wxDataViewColumn
*col
= NULL
;
3477 unsigned int cols
= GetOwner()->GetColumnCount();
3479 for (i
= 0; i
< cols
; i
++)
3481 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3483 continue; // skip it!
3485 if (x
< xpos
+ c
->GetWidth())
3490 xpos
+= c
->GetWidth();
3498 wxDataViewRenderer
*cell
= col
->GetRenderer();
3499 unsigned int current
= GetLineAt( y
);
3500 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3502 // Unselect all if below the last row ?
3507 // Test whether the mouse is hovered on the tree item button
3508 bool hoverOverExpander
= false;
3509 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3511 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3512 if( node
!=NULL
&& node
->HasChildren() )
3514 int indent
= node
->GetIndentLevel();
3515 indent
= GetOwner()->GetIndent()*indent
;
3517 // we make the rectangle we are looking in a bit bigger than the actual
3518 // visual expander so the user can hit that little thing reliably
3519 wxRect
rect( xpos
+ indent
,
3520 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3521 m_lineHeight
, m_lineHeight
);
3522 if( rect
.Contains(x
, y
) )
3524 // So the mouse is over the expander
3525 hoverOverExpander
= true;
3526 if (m_underMouse
&& m_underMouse
!= node
)
3528 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3529 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3531 if (m_underMouse
!= node
)
3533 // wxLogMessage("Do the row: %d", current);
3534 RefreshRow(current
);
3536 m_underMouse
= node
;
3539 if (node
!=NULL
&& !node
->HasChildren())
3542 if (!hoverOverExpander
)
3544 if (m_underMouse
!= NULL
)
3546 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3547 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3548 m_underMouse
= NULL
;
3552 wxDataViewModel
*model
= GetOwner()->GetModel();
3554 #if wxUSE_DRAG_AND_DROP
3555 if (event
.Dragging())
3557 if (m_dragCount
== 0)
3559 // we have to report the raw, physical coords as we want to be
3560 // able to call HitTest(event.m_pointDrag) from the user code to
3561 // get the item being dragged
3562 m_dragStart
= event
.GetPosition();
3567 if (m_dragCount
!= 3)
3570 if (event
.LeftIsDown())
3572 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3573 &m_dragStart
.x
, &m_dragStart
.y
);
3574 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3575 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3577 // Notify cell about drag
3578 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3579 event
.SetEventObject( m_owner
);
3580 event
.SetItem( item
);
3581 event
.SetModel( model
);
3582 if (!m_owner
->HandleWindowEvent( event
))
3585 if (!event
.IsAllowed())
3588 wxDataObject
*obj
= event
.GetDataObject();
3592 wxDataViewDropSource
drag( this, drag_item_row
);
3593 drag
.SetData( *obj
);
3594 /* wxDragResult res = */ drag
.DoDragDrop();
3603 #endif // wxUSE_DRAG_AND_DROP
3605 bool simulateClick
= false;
3607 if (event
.ButtonDClick())
3609 m_renameTimer
->Stop();
3610 m_lastOnSame
= false;
3613 wxDataViewItem item
= GetItemByRow(current
);
3614 bool ignore_other_columns
=
3615 ((GetOwner()->GetExpanderColumn() != col
) &&
3616 (model
->IsContainer(item
)) &&
3617 (!model
->HasContainerColumns(item
)));
3619 if (event
.LeftDClick())
3621 if(hoverOverExpander
)
3623 // a double click on the expander will be converted into a "simulated" normal click
3624 simulateClick
= true;
3626 else if ( current
== m_lineLastClicked
)
3628 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3630 const unsigned colIdx
= col
->GetModelColumn();
3633 model
->GetValue( value
, item
, colIdx
);
3635 cell
->WXOnActivate(model
, value
, item
, colIdx
);
3637 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3639 cell
->SetValue( value
);
3641 wxRect
cell_rect( xpos
, GetLineStart( current
),
3642 col
->GetWidth(), GetLineHeight( current
) );
3643 custom
->Activate( cell_rect
, model
, item
, colIdx
);
3648 wxWindow
*parent
= GetParent();
3649 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3651 le
.SetColumn( col
->GetModelColumn() );
3652 le
.SetDataViewColumn( col
);
3653 le
.SetEventObject(parent
);
3654 le
.SetModel(GetOwner()->GetModel());
3656 parent
->GetEventHandler()->ProcessEvent(le
);
3662 // The first click was on another item, so don't interpret this as
3663 // a double click, but as a simple click instead
3664 simulateClick
= true;
3668 if (event
.LeftUp() && !hoverOverExpander
)
3670 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3672 // select single line
3673 SelectAllRows( false );
3674 SelectRow( m_lineSelectSingleOnUp
, true );
3675 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3678 // If the user click the expander, we do not do editing even if the column
3679 // with expander are editable
3680 if (m_lastOnSame
&& !ignore_other_columns
)
3682 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3683 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3685 m_renameTimer
->Start( 100, true );
3689 m_lastOnSame
= false;
3690 m_lineSelectSingleOnUp
= (unsigned int)-1;
3692 else if(!event
.LeftUp())
3694 // This is necessary, because after a DnD operation in
3695 // from and to ourself, the up event is swallowed by the
3696 // DnD code. So on next non-up event (which means here and
3697 // now) m_lineSelectSingleOnUp should be reset.
3698 m_lineSelectSingleOnUp
= (unsigned int)-1;
3701 if (event
.RightDown())
3703 m_lineBeforeLastClicked
= m_lineLastClicked
;
3704 m_lineLastClicked
= current
;
3706 // If the item is already selected, do not update the selection.
3707 // Multi-selections should not be cleared if a selected item is clicked.
3708 if (!IsRowSelected(current
))
3710 SelectAllRows(false);
3711 ChangeCurrentRow(current
);
3712 SelectRow(m_currentRow
,true);
3713 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3716 else if (event
.RightUp())
3719 model
->GetValue( value
, item
, col
->GetModelColumn() );
3720 wxWindow
*parent
= GetParent();
3721 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3723 le
.SetColumn( col
->GetModelColumn() );
3724 le
.SetDataViewColumn( col
);
3725 le
.SetEventObject(parent
);
3726 le
.SetModel(GetOwner()->GetModel());
3728 parent
->GetEventHandler()->ProcessEvent(le
);
3730 else if (event
.MiddleDown())
3734 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3736 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3738 // hoverOverExpander being true tells us that our node must be
3739 // valid and have children.
3740 // So we don't need any extra checks.
3741 if( node
->IsOpen() )
3746 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3748 m_lineBeforeLastClicked
= m_lineLastClicked
;
3749 m_lineLastClicked
= current
;
3751 unsigned int oldCurrentRow
= m_currentRow
;
3752 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3754 bool cmdModifierDown
= event
.CmdDown();
3755 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3757 if ( IsSingleSel() || !IsRowSelected(current
) )
3759 SelectAllRows( false );
3760 ChangeCurrentRow(current
);
3761 SelectRow(m_currentRow
,true);
3762 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3764 else // multi sel & current is highlighted & no mod keys
3766 m_lineSelectSingleOnUp
= current
;
3767 ChangeCurrentRow(current
); // change focus
3770 else // multi sel & either ctrl or shift is down
3772 if (cmdModifierDown
)
3774 ChangeCurrentRow(current
);
3775 ReverseRowSelection(m_currentRow
);
3776 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3778 else if (event
.ShiftDown())
3780 ChangeCurrentRow(current
);
3782 unsigned int lineFrom
= oldCurrentRow
,
3785 if ( lineTo
< lineFrom
)
3788 lineFrom
= m_currentRow
;
3791 SelectRows(lineFrom
, lineTo
, true);
3792 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3794 else // !ctrl, !shift
3796 // test in the enclosing if should make it impossible
3797 wxFAIL_MSG( wxT("how did we get here?") );
3801 if (m_currentRow
!= oldCurrentRow
)
3802 RefreshRow( oldCurrentRow
);
3804 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3806 // Update selection here...
3809 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3810 (current
== oldCurrentRow
)) && oldWasSelected
;
3812 // Call LeftClick after everything else as under GTK+
3813 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3815 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3817 // notify cell about click
3818 custom
->PrepareForItem(model
, item
, col
->GetModelColumn());
3820 wxRect
cell_rect( xpos
, GetLineStart( current
),
3821 col
->GetWidth(), GetLineHeight( current
) );
3823 // Report position relative to the cell's custom area, i.e.
3824 // no the entire space as given by the control but the one
3825 // used by the renderer after calculation of alignment etc.
3827 // adjust the rectangle ourselves to account for the alignment
3828 wxRect rectItem
= cell_rect
;
3829 const int align
= custom
->GetAlignment();
3830 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3832 const wxSize size
= custom
->GetSize();
3834 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3836 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3837 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3838 else if ( align
& wxALIGN_RIGHT
)
3839 rectItem
.x
+= cell_rect
.width
- size
.x
;
3840 // else: wxALIGN_LEFT is the default
3843 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3845 if ( align
& wxALIGN_CENTER_VERTICAL
)
3846 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3847 else if ( align
& wxALIGN_BOTTOM
)
3848 rectItem
.y
+= cell_rect
.height
- size
.y
;
3849 // else: wxALIGN_TOP is the default
3853 wxPoint
pos( event
.GetPosition() );
3854 pos
.x
-= rectItem
.x
;
3855 pos
.y
-= rectItem
.y
;
3857 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3859 /* ignore ret */ custom
->LeftClick( pos
, cell_rect
,
3860 model
, item
, col
->GetModelColumn());
3866 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3870 if (HasCurrentRow())
3876 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3880 if (HasCurrentRow())
3886 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3888 if( m_selection
.GetCount() != 1 )
3889 return wxDataViewItem();
3891 return GetItemByRow( m_selection
.Item(0));
3894 //-----------------------------------------------------------------------------
3896 //-----------------------------------------------------------------------------
3898 WX_DEFINE_LIST(wxDataViewColumnList
)
3900 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3901 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3902 EVT_SIZE(wxDataViewCtrl::OnSize
)
3905 wxDataViewCtrl::~wxDataViewCtrl()
3908 GetModel()->RemoveNotifier( m_notifier
);
3911 m_colsBestWidths
.clear();
3914 void wxDataViewCtrl::Init()
3916 m_cols
.DeleteContents(true);
3919 // No sorting column at start
3920 m_sortingColumnIdx
= wxNOT_FOUND
;
3922 m_headerArea
= NULL
;
3925 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3930 const wxValidator
& validator
,
3931 const wxString
& name
)
3933 // if ( (style & wxBORDER_MASK) == 0)
3934 // style |= wxBORDER_SUNKEN;
3938 if (!wxControl::Create( parent
, id
, pos
, size
,
3939 style
| wxScrolledWindowStyle
, validator
, name
))
3942 SetInitialSize(size
);
3945 MacSetClipChildren( true );
3948 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3950 // We use the cursor keys for moving the selection, not scrolling, so call
3951 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3952 // keyboard events forwarded to us from wxListMainWindow.
3953 DisableKeyboardScrolling();
3955 if (HasFlag(wxDV_NO_HEADER
))
3956 m_headerArea
= NULL
;
3958 m_headerArea
= new wxDataViewHeaderWindow(this);
3960 SetTargetWindow( m_clientArea
);
3962 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3964 sizer
->Add( m_headerArea
, 0, wxGROW
);
3965 sizer
->Add( m_clientArea
, 1, wxGROW
);
3971 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3973 return wxBORDER_THEME
;
3977 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3981 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3984 // we need to process arrows ourselves for scrolling
3985 if ( nMsg
== WM_GETDLGCODE
)
3987 rc
|= DLGC_WANTARROWS
;
3995 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3997 wxSize newsize
= size
;
3998 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3999 newsize
.y
-= m_headerArea
->GetSize().y
;
4004 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4006 // We need to override OnSize so that our scrolled
4007 // window a) does call Layout() to use sizers for
4008 // positioning the controls but b) does not query
4009 // the sizer for their size and use that for setting
4010 // the scrollable area as set that ourselves by
4011 // calling SetScrollbar() further down.
4018 void wxDataViewCtrl::SetFocus()
4021 m_clientArea
->SetFocus();
4024 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4026 if (!wxDataViewCtrlBase::AssociateModel( model
))
4029 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4031 model
->AddNotifier( m_notifier
);
4033 m_clientArea
->DestroyTree();
4035 m_clientArea
->BuildTree(model
);
4037 m_clientArea
->UpdateDisplay();
4042 #if wxUSE_DRAG_AND_DROP
4044 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4046 return m_clientArea
->EnableDragSource( format
);
4049 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4051 return m_clientArea
->EnableDropTarget( format
);
4054 #endif // wxUSE_DRAG_AND_DROP
4056 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4058 if (!wxDataViewCtrlBase::AppendColumn(col
))
4061 m_cols
.Append( col
);
4062 m_colsBestWidths
.push_back(0);
4063 OnColumnsCountChanged();
4067 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4069 if (!wxDataViewCtrlBase::PrependColumn(col
))
4072 m_cols
.Insert( col
);
4073 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4074 OnColumnsCountChanged();
4078 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4080 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4083 m_cols
.Insert( pos
, col
);
4084 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4085 OnColumnsCountChanged();
4089 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4092 m_headerArea
->UpdateColumn(idx
);
4094 m_clientArea
->UpdateDisplay();
4097 void wxDataViewCtrl::OnColumnsCountChanged()
4100 m_headerArea
->SetColumnCount(GetColumnCount());
4102 m_clientArea
->UpdateDisplay();
4105 void wxDataViewCtrl::DoSetExpanderColumn()
4107 m_clientArea
->UpdateDisplay();
4110 void wxDataViewCtrl::DoSetIndent()
4112 m_clientArea
->UpdateDisplay();
4115 unsigned int wxDataViewCtrl::GetColumnCount() const
4117 return m_cols
.GetCount();
4120 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4125 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4127 // columns can't be reordered if there is no header window which allows
4129 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4132 return GetColumn(idx
);
4135 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4137 const unsigned count
= m_cols
.size();
4138 for ( unsigned n
= 0; n
< count
; n
++ )
4140 if ( m_cols
[n
] == column
)
4147 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4149 if ( m_colsBestWidths
[idx
] != 0 )
4150 return m_colsBestWidths
[idx
];
4152 const int count
= m_clientArea
->GetRowCount();
4153 wxDataViewColumn
*column
= GetColumn(idx
);
4154 wxDataViewRenderer
*renderer
=
4155 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4157 class MaxWidthCalculator
4160 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4161 wxDataViewRenderer
*renderer
,
4162 const wxDataViewModel
*model
,
4165 m_clientArea(clientArea
),
4166 m_renderer(renderer
),
4172 void UpdateWithWidth(int width
)
4174 m_width
= wxMax(m_width
, width
);
4177 void UpdateWithRow(int row
)
4179 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4180 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4181 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4184 int GetMaxWidth() const { return m_width
; }
4188 wxDataViewMainWindow
*m_clientArea
;
4189 wxDataViewRenderer
*m_renderer
;
4190 const wxDataViewModel
*m_model
;
4194 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4195 GetModel(), column
->GetModelColumn());
4199 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4200 // Labels on native MSW header are indented on both sides
4202 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4203 calculator
.UpdateWithWidth(header_width
);
4206 // The code below deserves some explanation. For very large controls, we
4207 // simply can't afford to calculate sizes for all items, it takes too
4208 // long. So the best we can do is to check the first and the last N/2
4209 // items in the control for some sufficiently large N and calculate best
4210 // sizes from that. That can result in the calculated best width being too
4211 // small for some outliers, but it's better to get slightly imperfect
4212 // result than to wait several seconds after every update. To avoid highly
4213 // visible miscalculations, we also include all currently visible items
4214 // no matter what. Finally, the value of N is determined dynamically by
4215 // measuring how much time we spent on the determining item widths so far.
4218 int top_part_end
= count
;
4219 static const long CALC_TIMEOUT
= 20/*ms*/;
4220 // don't call wxStopWatch::Time() too often
4221 static const unsigned CALC_CHECK_FREQ
= 100;
4224 // use some hard-coded limit, that's the best we can do without timer
4225 int top_part_end
= wxMin(500, count
);
4226 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4230 for ( row
= 0; row
< top_part_end
; row
++ )
4233 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4234 timer
.Time() > CALC_TIMEOUT
)
4236 #endif // wxUSE_STOPWATCH
4237 calculator
.UpdateWithRow(row
);
4240 // row is the first unmeasured item now; that's our value of N/2
4246 // add bottom N/2 items now:
4247 const int bottom_part_start
= wxMax(row
, count
- row
);
4248 for ( row
= bottom_part_start
; row
< count
; row
++ )
4250 calculator
.UpdateWithRow(row
);
4253 // finally, include currently visible items in the calculation:
4254 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4255 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4256 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4258 first_visible
= wxMax(first_visible
, top_part_end
);
4259 last_visible
= wxMin(bottom_part_start
, last_visible
);
4261 for ( row
= first_visible
; row
< last_visible
; row
++ )
4263 calculator
.UpdateWithRow(row
);
4266 wxLogTrace("dataview",
4267 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4269 count
- bottom_part_start
,
4270 wxMax(0, last_visible
- first_visible
),
4274 int max_width
= calculator
.GetMaxWidth();
4275 if ( max_width
> 0 )
4276 max_width
+= 2 * PADDING_RIGHTLEFT
;
4278 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4282 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4283 unsigned int WXUNUSED(new_pos
))
4285 // do _not_ reorder m_cols elements here, they should always be in the
4286 // order in which columns were added, we only display the columns in
4288 m_clientArea
->UpdateDisplay();
4291 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4293 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4297 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4299 OnColumnsCountChanged();
4304 bool wxDataViewCtrl::ClearColumns()
4307 m_colsBestWidths
.clear();
4308 OnColumnsCountChanged();
4312 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4314 m_colsBestWidths
[idx
] = 0;
4317 m_headerArea
->UpdateColumn(idx
);
4320 void wxDataViewCtrl::InvalidateColBestWidths()
4322 m_colsBestWidths
.clear();
4323 m_colsBestWidths
.resize(m_cols
.size());
4327 const unsigned cols
= m_headerArea
->GetColumnCount();
4328 for ( unsigned i
= 0; i
< cols
; i
++ )
4329 m_headerArea
->UpdateColumn(i
);
4333 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4336 unsigned int len
= GetColumnCount();
4337 for ( unsigned int i
= 0; i
< len
; i
++ )
4339 wxDataViewColumn
* col
= GetColumnAt(i
);
4346 // This returns the position in pixels which is not what we want.
4349 unsigned int len
= GetColumnCount();
4350 for ( unsigned int i
= 0; i
< len
; i
++ )
4352 wxDataViewColumn
* col
= GetColumnAt(i
);
4353 if (col
->IsHidden())
4355 ret
+= col
->GetWidth();
4358 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4366 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4368 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4369 : GetColumn(m_sortingColumnIdx
);
4372 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4374 return GetItemByRow(m_clientArea
->GetCurrentRow());
4377 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4379 const int row
= m_clientArea
->GetRowByItem(item
);
4381 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4382 if ( static_cast<unsigned>(row
) != oldCurrent
)
4384 m_clientArea
->ChangeCurrentRow(row
);
4385 m_clientArea
->RefreshRow(oldCurrent
);
4386 m_clientArea
->RefreshRow(row
);
4390 // Selection code with wxDataViewItem as parameters
4391 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4393 return m_clientArea
->GetSelection();
4396 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4399 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4400 int len
= selection
.GetCount();
4401 for( int i
= 0; i
< len
; i
++)
4403 unsigned int row
= selection
[i
];
4404 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4409 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4411 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4413 wxDataViewItem last_parent
;
4415 int len
= sel
.GetCount();
4416 for( int i
= 0; i
< len
; i
++ )
4418 wxDataViewItem item
= sel
[i
];
4419 wxDataViewItem parent
= GetModel()->GetParent( item
);
4422 if (parent
!= last_parent
)
4423 ExpandAncestors(item
);
4426 last_parent
= parent
;
4427 int row
= m_clientArea
->GetRowByItem( item
);
4429 selection
.Add( static_cast<unsigned int>(row
) );
4432 m_clientArea
->SetSelections( selection
);
4435 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4437 ExpandAncestors( item
);
4439 int row
= m_clientArea
->GetRowByItem( item
);
4442 // Unselect all rows before select another in the single select mode
4443 if (m_clientArea
->IsSingleSel())
4444 m_clientArea
->SelectAllRows(false);
4446 m_clientArea
->SelectRow(row
, true);
4448 // Also set focus to the selected item
4449 m_clientArea
->ChangeCurrentRow( row
);
4453 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4455 int row
= m_clientArea
->GetRowByItem( item
);
4457 m_clientArea
->SelectRow(row
, false);
4460 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4462 int row
= m_clientArea
->GetRowByItem( item
);
4465 return m_clientArea
->IsRowSelected(row
);
4470 // Selection code with row number as parameter
4471 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4474 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4475 int len
= selection
.GetCount();
4476 for( int i
= 0; i
< len
; i
++)
4478 unsigned int row
= selection
[i
];
4484 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4486 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4487 int len
= sel
.GetCount();
4488 for( int i
= 0; i
< len
; i
++ )
4492 selection
.Add( static_cast<unsigned int>(row
) );
4494 m_clientArea
->SetSelections( selection
);
4497 void wxDataViewCtrl::Select( int row
)
4501 if (m_clientArea
->IsSingleSel())
4502 m_clientArea
->SelectAllRows(false);
4503 m_clientArea
->SelectRow( row
, true );
4507 void wxDataViewCtrl::Unselect( int row
)
4510 m_clientArea
->SelectRow(row
, false);
4513 bool wxDataViewCtrl::IsSelected( int row
) const
4516 return m_clientArea
->IsRowSelected(row
);
4520 void wxDataViewCtrl::SelectRange( int from
, int to
)
4523 for( int i
= from
; i
< to
; i
++ )
4525 m_clientArea
->Select(sel
);
4528 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4530 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4531 for( int i
= from
; i
< to
; i
++ )
4532 if( sel
.Index( i
) != wxNOT_FOUND
)
4534 m_clientArea
->SetSelections(sel
);
4537 void wxDataViewCtrl::SelectAll()
4539 m_clientArea
->SelectAllRows(true);
4542 void wxDataViewCtrl::UnselectAll()
4544 m_clientArea
->SelectAllRows(false);
4547 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4551 if( row
> (int) m_clientArea
->GetRowCount() )
4552 row
= m_clientArea
->GetRowCount();
4554 int first
= m_clientArea
->GetFirstVisibleRow();
4555 int last
= m_clientArea
->GetLastVisibleRow();
4557 m_clientArea
->ScrollTo( row
, column
);
4558 else if( row
> last
)
4559 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4561 m_clientArea
->ScrollTo( first
, column
);
4564 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4566 ExpandAncestors( item
);
4568 m_clientArea
->RecalculateDisplay();
4570 int row
= m_clientArea
->GetRowByItem(item
);
4573 if( column
== NULL
)
4574 EnsureVisible(row
, -1);
4576 EnsureVisible( row
, GetColumnIndex(column
) );
4581 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4582 wxDataViewColumn
* &column
) const
4584 m_clientArea
->HitTest(point
, item
, column
);
4587 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4588 const wxDataViewColumn
* column
) const
4590 return m_clientArea
->GetItemRect(item
, column
);
4593 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4595 return m_clientArea
->GetItemByRow( row
);
4598 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4600 return m_clientArea
->GetRowByItem( item
);
4603 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4605 ExpandAncestors( item
);
4607 int row
= m_clientArea
->GetRowByItem( item
);
4609 m_clientArea
->Expand(row
);
4612 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4614 int row
= m_clientArea
->GetRowByItem( item
);
4616 m_clientArea
->Collapse(row
);
4619 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4621 int row
= m_clientArea
->GetRowByItem( item
);
4623 return m_clientArea
->IsExpanded(row
);
4627 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4629 wxDataViewColumn
* col
= GetColumn( column
);
4633 wxRect itemRect
= GetItemRect(item
, col
);
4634 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4635 if (renderer
->GetMode() == wxDATAVIEW_CELL_EDITABLE
)
4636 renderer
->StartEditing(item
, itemRect
);
4639 #endif // !wxUSE_GENERICDATAVIEWCTRL
4641 #endif // wxUSE_DATAVIEWCTRL