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 wxWindow
* 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( wxWindow
*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
.IsOk())
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
.IsOk())
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 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
867 const wxRect
& WXUNUSED(cell
),
868 wxDataViewModel
*model
,
869 const wxDataViewItem
& item
,
872 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
873 if (!wxRect(GetSize()).Contains(cursor
))
876 if (model
->IsEnabled(item
, col
))
878 model
->ChangeValue(!m_toggle
, item
, col
);
885 wxSize
wxDataViewToggleRenderer::GetSize() const
887 // the window parameter is not used by GetCheckBoxSize() so it's
889 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
892 // ---------------------------------------------------------
893 // wxDataViewProgressRenderer
894 // ---------------------------------------------------------
896 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
898 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
899 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
900 wxDataViewRenderer( varianttype
, mode
, align
)
906 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
908 m_value
= (long) value
;
910 if (m_value
< 0) m_value
= 0;
911 if (m_value
> 100) m_value
= 100;
916 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
918 value
= (long) m_value
;
923 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
925 // deflate the rect to leave a small border between bars in adjacent rows
926 wxRect bar
= rect
.Deflate(0, 1);
928 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
929 dc
->SetPen( *wxBLACK_PEN
);
930 dc
->DrawRectangle( bar
);
932 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
933 dc
->SetPen( *wxTRANSPARENT_PEN
);
935 const wxDataViewItemAttr
& attr
= GetAttr();
936 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
938 dc
->DrawRectangle( bar
);
943 wxSize
wxDataViewProgressRenderer::GetSize() const
945 return wxSize(40,12);
948 // ---------------------------------------------------------
949 // wxDataViewDateRenderer
950 // ---------------------------------------------------------
952 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
954 #if wxUSE_DATE_RENDERER_POPUP
956 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
959 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
960 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
961 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
966 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
967 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
968 sizer
->Add( m_cal
, 1, wxGROW
);
973 void OnCalendar( wxCalendarEvent
&event
);
975 wxCalendarCtrl
*m_cal
;
976 wxDataViewModel
*m_model
;
978 const wxDataViewItem
& m_item
;
981 virtual void OnDismiss()
986 DECLARE_EVENT_TABLE()
989 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
990 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
993 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
995 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
999 #endif // wxUSE_DATE_RENDERER_POPUP
1001 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1003 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1004 wxDataViewCellMode mode
, int align
) :
1005 wxDataViewRenderer( varianttype
, mode
, align
)
1009 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1011 m_date
= value
.GetDateTime();
1016 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1022 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1024 wxString tmp
= m_date
.FormatDate();
1025 RenderText( tmp
, 0, cell
, dc
, state
);
1029 wxSize
wxDataViewDateRenderer::GetSize() const
1031 return GetTextExtent(m_date
.FormatDate());
1034 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1035 wxDataViewModel
*model
,
1036 const wxDataViewItem
& item
,
1039 wxDateTime dtOld
= m_date
;
1041 #if wxUSE_DATE_RENDERER_POPUP
1042 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1043 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1044 wxPoint pos
= wxGetMousePosition();
1047 popup
->Popup( popup
->m_cal
);
1048 #else // !wxUSE_DATE_RENDERER_POPUP
1049 wxMessageBox(dtOld
.Format());
1050 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1055 // ---------------------------------------------------------
1056 // wxDataViewIconTextRenderer
1057 // ---------------------------------------------------------
1059 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1061 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1062 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1063 wxDataViewRenderer( varianttype
, mode
, align
)
1066 SetAlignment(align
);
1069 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1075 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1080 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1084 const wxIcon
& icon
= m_value
.GetIcon();
1087 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1088 xoffset
= icon
.GetWidth()+4;
1091 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1096 wxSize
wxDataViewIconTextRenderer::GetSize() const
1098 if (!m_value
.GetText().empty())
1100 wxSize size
= GetTextExtent(m_value
.GetText());
1102 if (m_value
.GetIcon().IsOk())
1103 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1106 return wxSize(80,20);
1109 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1111 wxDataViewIconText iconText
;
1114 wxString text
= iconText
.GetText();
1116 // adjust the label rect to take the width of the icon into account
1117 if (iconText
.GetIcon().IsOk())
1119 int w
= iconText
.GetIcon().GetWidth() + 4;
1121 labelRect
.width
-= w
;
1124 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1125 wxPoint(labelRect
.x
,labelRect
.y
),
1126 wxSize(labelRect
.width
,labelRect
.height
) );
1128 // select the text in the control an place the cursor at the end
1129 ctrl
->SetInsertionPointEnd();
1135 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1137 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1139 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1144 //-----------------------------------------------------------------------------
1145 // wxDataViewDropTarget
1146 //-----------------------------------------------------------------------------
1148 #if wxUSE_DRAG_AND_DROP
1150 class wxBitmapCanvas
: public wxWindow
1153 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1154 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1157 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1160 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1163 dc
.DrawBitmap( m_bitmap
, 0, 0);
1169 class wxDataViewDropSource
: public wxDropSource
1172 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1180 ~wxDataViewDropSource()
1185 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1187 wxPoint pos
= wxGetMousePosition();
1191 int liney
= m_win
->GetLineStart( m_row
);
1193 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1194 m_win
->ClientToScreen( &linex
, &liney
);
1195 m_dist_x
= pos
.x
- linex
;
1196 m_dist_y
= pos
.y
- liney
;
1199 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1201 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1202 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1204 wxFRAME_TOOL_WINDOW
|
1205 wxFRAME_FLOAT_ON_PARENT
|
1206 wxFRAME_NO_TASKBAR
|
1208 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1213 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1214 m_hint
->SetTransparent( 128 );
1220 wxDataViewMainWindow
*m_win
;
1223 int m_dist_x
,m_dist_y
;
1227 class wxDataViewDropTarget
: public wxDropTarget
1230 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1236 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1238 wxDataFormat format
= GetMatchingPair();
1239 if (format
== wxDF_INVALID
)
1241 return m_win
->OnDragOver( format
, x
, y
, def
);
1244 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1246 wxDataFormat format
= GetMatchingPair();
1247 if (format
== wxDF_INVALID
)
1249 return m_win
->OnDrop( format
, x
, y
);
1252 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1254 wxDataFormat format
= GetMatchingPair();
1255 if (format
== wxDF_INVALID
)
1259 return m_win
->OnData( format
, x
, y
, def
);
1262 virtual void OnLeave()
1263 { m_win
->OnLeave(); }
1265 wxDataViewMainWindow
*m_win
;
1268 #endif // wxUSE_DRAG_AND_DROP
1270 //-----------------------------------------------------------------------------
1271 // wxDataViewRenameTimer
1272 //-----------------------------------------------------------------------------
1274 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1279 void wxDataViewRenameTimer::Notify()
1281 m_owner
->OnRenameTimer();
1284 //-----------------------------------------------------------------------------
1285 // wxDataViewMainWindow
1286 //-----------------------------------------------------------------------------
1288 // The tree building helper, declared firstly
1289 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1290 wxDataViewTreeNode
* node
);
1292 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1294 if (row1
> row2
) return 1;
1295 if (row1
== row2
) return 0;
1299 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1301 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1302 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1303 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1304 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1305 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1306 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1309 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1310 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1311 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1312 m_selection( wxDataViewSelectionCmp
)
1317 m_lastOnSame
= false;
1318 m_renameTimer
= new wxDataViewRenameTimer( this );
1320 // TODO: user better initial values/nothing selected
1321 m_currentCol
= NULL
;
1324 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1326 #if wxUSE_DRAG_AND_DROP
1328 m_dragStart
= wxPoint(0,0);
1330 m_dragEnabled
= false;
1331 m_dropEnabled
= false;
1333 m_dropHintLine
= (unsigned int) -1;
1334 #endif // wxUSE_DRAG_AND_DROP
1336 m_lineLastClicked
= (unsigned int) -1;
1337 m_lineBeforeLastClicked
= (unsigned int) -1;
1338 m_lineSelectSingleOnUp
= (unsigned int) -1;
1342 SetBackgroundColour( *wxWHITE
);
1344 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1346 m_penRule
= wxPen(GetRuleColour());
1348 // compose a pen whichcan draw black lines
1349 // TODO: maybe there is something system colour to use
1350 m_penExpander
= wxPen(wxColour(0,0,0));
1352 m_root
= new wxDataViewTreeNode( NULL
);
1353 m_root
->SetHasChildren(true);
1355 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1357 m_underMouse
= NULL
;
1361 wxDataViewMainWindow::~wxDataViewMainWindow()
1364 delete m_renameTimer
;
1368 #if wxUSE_DRAG_AND_DROP
1369 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1371 m_dragFormat
= format
;
1372 m_dragEnabled
= format
!= wxDF_INVALID
;
1377 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1379 m_dropFormat
= format
;
1380 m_dropEnabled
= format
!= wxDF_INVALID
;
1383 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1388 void wxDataViewMainWindow::RemoveDropHint()
1393 RefreshRow( m_dropHintLine
);
1394 m_dropHintLine
= (unsigned int) -1;
1398 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1399 wxCoord y
, wxDragResult def
)
1403 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1404 unsigned int row
= GetLineAt( yy
);
1406 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1412 wxDataViewItem item
= GetItemByRow( row
);
1414 wxDataViewModel
*model
= GetOwner()->GetModel();
1416 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1417 event
.SetEventObject( m_owner
);
1418 event
.SetItem( item
);
1419 event
.SetModel( model
);
1420 event
.SetDataFormat( format
);
1421 if (!m_owner
->HandleWindowEvent( event
))
1427 if (!event
.IsAllowed())
1434 if (m_dropHint
&& (row
!= m_dropHintLine
))
1435 RefreshRow( m_dropHintLine
);
1437 m_dropHintLine
= row
;
1443 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1449 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1450 unsigned int row
= GetLineAt( yy
);
1452 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1455 wxDataViewItem item
= GetItemByRow( row
);
1457 wxDataViewModel
*model
= GetOwner()->GetModel();
1459 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1460 event
.SetEventObject( m_owner
);
1461 event
.SetItem( item
);
1462 event
.SetModel( model
);
1463 event
.SetDataFormat( format
);
1464 if (!m_owner
->HandleWindowEvent( event
))
1467 if (!event
.IsAllowed())
1473 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1478 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1479 unsigned int row
= GetLineAt( yy
);
1481 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1484 wxDataViewItem item
= GetItemByRow( row
);
1486 wxDataViewModel
*model
= GetOwner()->GetModel();
1488 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1490 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1491 event
.SetEventObject( m_owner
);
1492 event
.SetItem( item
);
1493 event
.SetModel( model
);
1494 event
.SetDataFormat( format
);
1495 event
.SetDataSize( obj
->GetSize() );
1496 event
.SetDataBuffer( obj
->GetData() );
1497 if (!m_owner
->HandleWindowEvent( event
))
1500 if (!event
.IsAllowed())
1506 void wxDataViewMainWindow::OnLeave()
1511 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1513 int height
= GetLineHeight( row
);
1515 unsigned int cols
= GetOwner()->GetColumnCount();
1517 for (col
= 0; col
< cols
; col
++)
1519 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1520 if (column
->IsHidden())
1521 continue; // skip it!
1522 width
+= column
->GetWidth();
1528 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1529 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1530 indent
= indent
+ m_lineHeight
;
1531 // try to use the m_lineHeight as the expander space
1533 if(!node
->HasChildren())
1538 wxBitmap
bitmap( width
, height
);
1539 wxMemoryDC
dc( bitmap
);
1540 dc
.SetFont( GetFont() );
1541 dc
.SetPen( *wxBLACK_PEN
);
1542 dc
.SetBrush( *wxWHITE_BRUSH
);
1543 dc
.DrawRectangle( 0,0,width
,height
);
1545 wxDataViewModel
*model
= m_owner
->GetModel();
1547 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1550 // TODO-RTL: last column for RTL support
1551 expander
= GetOwner()->GetColumnAt( 0 );
1552 GetOwner()->SetExpanderColumn(expander
);
1557 for (col
= 0; col
< cols
; col
++)
1559 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1560 wxDataViewRenderer
*cell
= column
->GetRenderer();
1562 if (column
->IsHidden())
1563 continue; // skip it!
1565 width
= column
->GetWidth();
1567 if (column
== expander
)
1570 wxDataViewItem item
= GetItemByRow( row
);
1571 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1573 wxRect
item_rect(x
, 0, width
, height
);
1574 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1576 // dc.SetClippingRegion( item_rect );
1577 cell
->WXCallRender(item_rect
, &dc
, 0);
1578 // dc.DestroyClippingRegion();
1586 #endif // wxUSE_DRAG_AND_DROP
1589 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1591 wxDataViewModel
*model
= GetOwner()->GetModel();
1592 wxAutoBufferedPaintDC
dc( this );
1595 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1596 dc
.SetPen( *wxTRANSPARENT_PEN
);
1597 dc
.DrawRectangle(GetClientSize());
1602 // No items to draw.
1607 GetOwner()->PrepareDC( dc
);
1608 dc
.SetFont( GetFont() );
1610 wxRect update
= GetUpdateRegion().GetBox();
1611 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1613 // compute which items needs to be redrawn
1614 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1615 unsigned int item_count
=
1616 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1617 (int)(GetRowCount( ) - item_start
));
1618 unsigned int item_last
= item_start
+ item_count
;
1620 // Send the event to wxDataViewCtrl itself.
1621 wxWindow
* const parent
= GetParent();
1622 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1623 cache_event
.SetEventObject(parent
);
1624 cache_event
.SetCache(item_start
, item_last
- 1);
1625 parent
->ProcessWindowEvent(cache_event
);
1627 // compute which columns needs to be redrawn
1628 unsigned int cols
= GetOwner()->GetColumnCount();
1631 // we assume that we have at least one column below and painting an
1632 // empty control is unnecessary anyhow
1636 unsigned int col_start
= 0;
1637 unsigned int x_start
;
1638 for (x_start
= 0; col_start
< cols
; col_start
++)
1640 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1641 if (col
->IsHidden())
1642 continue; // skip it!
1644 unsigned int w
= col
->GetWidth();
1645 if (x_start
+w
>= (unsigned int)update
.x
)
1651 unsigned int col_last
= col_start
;
1652 unsigned int x_last
= x_start
;
1653 for (; col_last
< cols
; col_last
++)
1655 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1656 if (col
->IsHidden())
1657 continue; // skip it!
1659 if (x_last
> (unsigned int)update
.GetRight())
1662 x_last
+= col
->GetWidth();
1665 // Draw horizontal rules if required
1666 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1668 dc
.SetPen(m_penRule
);
1669 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1671 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1673 int y
= GetLineStart( i
);
1674 dc
.DrawLine(x_start
, y
, x_last
, y
);
1678 // Draw vertical rules if required
1679 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1681 dc
.SetPen(m_penRule
);
1682 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1684 // NB: Vertical rules are drawn in the last pixel of a column so that
1685 // they align perfectly with native MSW wxHeaderCtrl as well as for
1686 // consistency with MSW native list control. There's no vertical
1687 // rule at the most-left side of the control.
1689 int x
= x_start
- 1;
1690 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1692 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1693 if (col
->IsHidden())
1694 continue; // skip it
1696 x
+= col
->GetWidth();
1698 dc
.DrawLine(x
, GetLineStart( item_start
),
1699 x
, GetLineStart( item_last
) );
1703 // redraw the background for the items which are selected/current
1704 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1706 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1707 if (selected
|| item
== m_currentRow
)
1709 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1710 if (item
== m_currentRow
)
1711 flags
|= wxCONTROL_CURRENT
;
1713 flags
|= wxCONTROL_FOCUSED
;
1715 wxRect
rect( x_start
, GetLineStart( item
),
1716 x_last
- x_start
, GetLineHeight( item
) );
1717 wxRendererNative::Get().DrawItemSelectionRect
1727 #if wxUSE_DRAG_AND_DROP
1730 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1731 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1732 dc
.SetPen( *wxBLACK_PEN
);
1733 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1734 dc
.DrawRectangle( rect
);
1736 #endif // wxUSE_DRAG_AND_DROP
1738 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1741 // TODO-RTL: last column for RTL support
1742 expander
= GetOwner()->GetColumnAt( 0 );
1743 GetOwner()->SetExpanderColumn(expander
);
1746 // redraw all cells for all rows which must be repainted and all columns
1748 cell_rect
.x
= x_start
;
1749 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1751 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1752 wxDataViewRenderer
*cell
= col
->GetRenderer();
1753 cell_rect
.width
= col
->GetWidth();
1755 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1756 continue; // skip it!
1758 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1760 // get the cell value and set it into the renderer
1761 wxDataViewTreeNode
*node
= NULL
;
1762 wxDataViewItem dataitem
;
1764 if (!IsVirtualList())
1766 node
= GetTreeNodeByRow(item
);
1770 dataitem
= node
->GetItem();
1772 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1773 !model
->HasContainerColumns(dataitem
))
1778 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1781 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1784 cell_rect
.y
= GetLineStart( item
);
1785 cell_rect
.height
= GetLineHeight( item
);
1787 // deal with the expander
1789 if ((!IsList()) && (col
== expander
))
1791 // Calculate the indent first
1792 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1794 // we reserve m_lineHeight of horizontal space for the expander
1795 // but leave EXPANDER_MARGIN around the expander itself
1796 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1798 indent
+= m_lineHeight
;
1800 // draw expander if needed and visible
1801 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1803 dc
.SetPen( m_penExpander
);
1804 dc
.SetBrush( wxNullBrush
);
1806 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1807 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1808 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1810 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1813 if ( m_underMouse
== node
)
1814 flag
|= wxCONTROL_CURRENT
;
1815 if ( node
->IsOpen() )
1816 flag
|= wxCONTROL_EXPANDED
;
1818 // ensure that we don't overflow the cell (which might
1819 // happen if the column is very narrow)
1820 wxDCClipper
clip(dc
, cell_rect
);
1822 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1825 // force the expander column to left-center align
1826 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1828 if (node
&& !node
->HasChildren())
1830 // Yes, if the node does not have any child, it must be a leaf which
1831 // mean that it is a temporarily created by GetTreeNodeByRow
1835 wxRect item_rect
= cell_rect
;
1836 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1838 // account for the tree indent (harmless if we're not indented)
1839 item_rect
.x
+= indent
;
1840 item_rect
.width
-= indent
;
1842 if ( item_rect
.width
<= 0 )
1846 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1847 state
|= wxDATAVIEW_CELL_SELECTED
;
1849 // TODO: it would be much more efficient to create a clipping
1850 // region for the entire column being rendered (in the OnPaint
1851 // of wxDataViewMainWindow) instead of a single clip region for
1852 // each cell. However it would mean that each renderer should
1853 // respect the given wxRect's top & bottom coords, eventually
1854 // violating only the left & right coords - however the user can
1855 // make its own renderer and thus we cannot be sure of that.
1856 wxDCClipper
clip(dc
, item_rect
);
1858 cell
->WXCallRender(item_rect
, &dc
, state
);
1861 cell_rect
.x
+= cell_rect
.width
;
1865 void wxDataViewMainWindow::OnRenameTimer()
1867 // We have to call this here because changes may just have
1868 // been made and no screen update taken place.
1871 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1872 // (needs to be tested!)
1876 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1878 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1880 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1883 //-----------------------------------------------------------------------------
1884 // Helper class for do operation on the tree node
1885 //-----------------------------------------------------------------------------
1890 virtual ~DoJob() { }
1892 // The return value control how the tree-walker tranverse the tree
1893 // 0: Job done, stop tranverse and return
1894 // 1: Ignore the current node's subtree and continue
1895 // 2: Job not done, continue
1896 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1897 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1898 virtual int operator() ( void * n
) = 0;
1901 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1906 switch( func( node
) )
1917 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
1918 const wxDataViewTreeLeaves
& leaves
= node
->GetChildren();
1920 int len_nodes
= nodes
.GetCount();
1921 int len
= leaves
.GetCount();
1922 int i
= 0, nodes_i
= 0;
1924 for(; i
< len
; i
++ )
1926 void * n
= leaves
[i
];
1927 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1929 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1932 if( Walker( nd
, func
) )
1951 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1953 GetOwner()->InvalidateColBestWidths();
1955 if (IsVirtualList())
1957 wxDataViewVirtualListModel
*list_model
=
1958 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1959 m_count
= list_model
->GetCount();
1966 wxDataViewTreeNode
* node
;
1967 node
= FindNode(parent
);
1972 node
->SetHasChildren( true );
1974 if( g_model
->IsContainer( item
) )
1976 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1977 newnode
->SetItem(item
);
1978 newnode
->SetHasChildren( true );
1979 node
->AddNode( newnode
);
1982 node
->AddLeaf( item
.GetID() );
1984 node
->ChangeSubTreeCount(1);
1992 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1994 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1995 const wxDataViewItem
& item
)
1997 GetOwner()->InvalidateColBestWidths();
1999 if (IsVirtualList())
2001 wxDataViewVirtualListModel
*list_model
=
2002 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
2003 m_count
= list_model
->GetCount();
2005 if( m_currentRow
> GetRowCount() )
2006 m_currentRow
= m_count
- 1;
2008 // TODO: why empty the entire selection?
2009 m_selection
.Empty();
2016 wxDataViewTreeNode
* node
= FindNode(parent
);
2018 // Notice that it is possible that the item being deleted is not in the
2019 // tree at all, for example we could be deleting a never shown (because
2020 // collapsed) item in a tree model. So it's not an error if we don't know
2021 // about this item, just return without doing anything then.
2022 if ( !node
|| node
->GetChildren().Index(item
.GetID()) == wxNOT_FOUND
)
2026 node
->GetChildren().Remove( item
.GetID() );
2027 // Manipolate selection
2028 if( m_selection
.GetCount() > 1 )
2030 m_selection
.Empty();
2032 bool isContainer
= false;
2033 wxDataViewTreeNodes nds
= node
->GetNodes();
2034 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2036 if (nds
[i
]->GetItem() == item
)
2044 wxDataViewTreeNode
* n
= NULL
;
2045 wxDataViewTreeNodes nodes
= node
->GetNodes();
2046 int len
= nodes
.GetCount();
2047 for( int i
= 0; i
< len
; i
++)
2049 if( nodes
[i
]->GetItem() == item
)
2056 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2058 node
->GetNodes().Remove( n
);
2059 sub
-= n
->GetSubTreeCount();
2060 ::DestroyTreeHelper(n
);
2062 // Make the row number invalid and get a new valid one when user call GetRowCount
2064 node
->ChangeSubTreeCount(sub
);
2066 // Change the current row to the last row if the current exceed the max row number
2067 if( m_currentRow
> GetRowCount() )
2068 m_currentRow
= m_count
- 1;
2075 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2077 GetOwner()->InvalidateColBestWidths();
2083 wxWindow
*parent
= GetParent();
2084 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2085 le
.SetEventObject(parent
);
2086 le
.SetModel(GetOwner()->GetModel());
2088 parent
->GetEventHandler()->ProcessEvent(le
);
2093 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2095 int view_column
= -1;
2096 unsigned int n_col
= m_owner
->GetColumnCount();
2097 for (unsigned i
= 0; i
< n_col
; i
++)
2099 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2100 if (column
->GetModelColumn() == model_column
)
2102 view_column
= (int) i
;
2106 if (view_column
== -1)
2109 GetOwner()->InvalidateColBestWidth(view_column
);
2111 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2112 /*#define MAX_VIRTUAL_WIDTH 100000
2114 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2115 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2116 Refresh( true, &rect );
2124 wxWindow
*parent
= GetParent();
2125 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2126 le
.SetEventObject(parent
);
2127 le
.SetModel(GetOwner()->GetModel());
2129 le
.SetColumn(view_column
);
2130 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2131 parent
->GetEventHandler()->ProcessEvent(le
);
2136 bool wxDataViewMainWindow::Cleared()
2138 GetOwner()->InvalidateColBestWidths();
2141 m_selection
.Clear();
2144 BuildTree( GetOwner()->GetModel() );
2151 void wxDataViewMainWindow::UpdateDisplay()
2154 m_underMouse
= NULL
;
2157 void wxDataViewMainWindow::OnInternalIdle()
2159 wxWindow::OnInternalIdle();
2163 RecalculateDisplay();
2168 void wxDataViewMainWindow::RecalculateDisplay()
2170 wxDataViewModel
*model
= GetOwner()->GetModel();
2177 int width
= GetEndOfLastCol();
2178 int height
= GetLineStart( GetRowCount() );
2180 SetVirtualSize( width
, height
);
2181 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2186 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2188 m_underMouse
= NULL
;
2190 wxWindow::ScrollWindow( dx
, dy
, rect
);
2192 if (GetOwner()->m_headerArea
)
2193 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2196 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2198 m_underMouse
= NULL
;
2201 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2202 int sy
= GetLineStart( rows
)/y
;
2206 wxRect rect
= GetClientRect();
2210 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2211 for (x_start
= 0; colnum
< column
; colnum
++)
2213 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2214 if (col
->IsHidden())
2215 continue; // skip it!
2217 w
= col
->GetWidth();
2221 int x_end
= x_start
+ w
;
2222 xe
= xx
+ rect
.width
;
2225 sx
= ( xx
+ x_end
- xe
)/x
;
2232 m_owner
->Scroll( sx
, sy
);
2235 int wxDataViewMainWindow::GetCountPerPage() const
2237 wxSize size
= GetClientSize();
2238 return size
.y
/ m_lineHeight
;
2241 int wxDataViewMainWindow::GetEndOfLastCol() const
2245 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2247 const wxDataViewColumn
*c
=
2248 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2251 width
+= c
->GetWidth();
2256 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2260 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2262 return GetLineAt( y
);
2265 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2267 wxSize client_size
= GetClientSize();
2268 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2269 &client_size
.x
, &client_size
.y
);
2271 // we should deal with the pixel here
2272 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2274 return wxMin( GetRowCount()-1, row
);
2277 unsigned int wxDataViewMainWindow::GetRowCount()
2279 if ( m_count
== -1 )
2281 m_count
= RecalculateCount();
2287 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2294 void wxDataViewMainWindow::SelectAllRows( bool on
)
2301 m_selection
.Clear();
2302 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2303 m_selection
.Add( i
);
2308 unsigned int first_visible
= GetFirstVisibleRow();
2309 unsigned int last_visible
= GetLastVisibleRow();
2311 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2313 unsigned int row
= m_selection
[i
];
2314 if ((row
>= first_visible
) && (row
<= last_visible
))
2317 m_selection
.Clear();
2321 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2323 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2327 m_selection
.Add( row
);
2335 m_selection
.Remove( row
);
2341 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2345 unsigned int tmp
= from
;
2351 for (i
= from
; i
<= to
; i
++)
2353 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2356 m_selection
.Add( i
);
2361 m_selection
.Remove( i
);
2364 RefreshRows( from
, to
);
2367 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2369 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2371 int n
= aSelections
[i
];
2373 m_selection
.Add( n
);
2378 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2380 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2381 m_selection
.Add( row
);
2383 m_selection
.Remove( row
);
2387 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2389 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2392 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2394 wxWindow
*parent
= GetParent();
2395 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2397 le
.SetEventObject(parent
);
2398 le
.SetModel(GetOwner()->GetModel());
2401 parent
->GetEventHandler()->ProcessEvent(le
);
2404 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2406 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2407 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2409 wxSize client_size
= GetClientSize();
2410 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2411 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2412 if (intersect_rect
.width
> 0)
2413 Refresh( true, &intersect_rect
);
2416 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2420 unsigned int tmp
= to
;
2425 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2426 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2428 wxSize client_size
= GetClientSize();
2429 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2430 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2431 if (intersect_rect
.width
> 0)
2432 Refresh( true, &intersect_rect
);
2435 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2437 wxSize client_size
= GetClientSize();
2438 int start
= GetLineStart( firstRow
);
2439 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2440 if (start
> client_size
.y
) return;
2442 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2444 Refresh( true, &rect
);
2447 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2449 wxCHECK_RET( newCurrent
< GetRowCount(),
2450 wxT("invalid item index in OnArrowChar()") );
2452 // if there is no selection, we cannot move it anywhere
2453 if (!HasCurrentRow())
2456 unsigned int oldCurrent
= m_currentRow
;
2458 // in single selection we just ignore Shift as we can't select several
2460 if ( event
.ShiftDown() && !IsSingleSel() )
2462 RefreshRow( oldCurrent
);
2464 ChangeCurrentRow( newCurrent
);
2466 // select all the items between the old and the new one
2467 if ( oldCurrent
> newCurrent
)
2469 newCurrent
= oldCurrent
;
2470 oldCurrent
= m_currentRow
;
2473 SelectRows( oldCurrent
, newCurrent
, true );
2474 if (oldCurrent
!=newCurrent
)
2475 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2479 RefreshRow( oldCurrent
);
2481 // all previously selected items are unselected unless ctrl is held
2482 if ( !event
.ControlDown() )
2483 SelectAllRows(false);
2485 ChangeCurrentRow( newCurrent
);
2487 if ( !event
.ControlDown() )
2489 SelectRow( m_currentRow
, true );
2490 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2493 RefreshRow( m_currentRow
);
2496 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2499 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2503 rect
.y
= GetLineStart( row
);
2504 rect
.width
= GetEndOfLastCol();
2505 rect
.height
= GetLineHeight( row
);
2510 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2512 const wxDataViewModel
*model
= GetOwner()->GetModel();
2514 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2516 // TODO make more efficient
2521 for (r
= 0; r
< row
; r
++)
2523 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2524 if (!node
) return start
;
2526 wxDataViewItem item
= node
->GetItem();
2528 if (node
&& !node
->HasChildren())
2530 // Yes, if the node does not have any child, it must be a leaf which
2531 // mean that it is a temporarily created by GetTreeNodeByRow
2535 unsigned int cols
= GetOwner()->GetColumnCount();
2537 int height
= m_lineHeight
;
2538 for (col
= 0; col
< cols
; col
++)
2540 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2541 if (column
->IsHidden())
2542 continue; // skip it!
2545 model
->IsContainer(item
) &&
2546 !model
->HasContainerColumns(item
))
2547 continue; // skip it!
2549 wxDataViewRenderer
*renderer
=
2550 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2551 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2553 height
= wxMax( height
, renderer
->GetSize().y
);
2563 return row
* m_lineHeight
;
2567 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2569 const wxDataViewModel
*model
= GetOwner()->GetModel();
2571 // check for the easy case first
2572 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2573 return y
/ m_lineHeight
;
2575 // TODO make more efficient
2576 unsigned int row
= 0;
2577 unsigned int yy
= 0;
2580 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2583 // not really correct...
2584 return row
+ ((y
-yy
) / m_lineHeight
);
2587 wxDataViewItem item
= node
->GetItem();
2589 if (node
&& !node
->HasChildren())
2591 // Yes, if the node does not have any child, it must be a leaf which
2592 // mean that it is a temporarily created by GetTreeNodeByRow
2596 unsigned int cols
= GetOwner()->GetColumnCount();
2598 int height
= m_lineHeight
;
2599 for (col
= 0; col
< cols
; col
++)
2601 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2602 if (column
->IsHidden())
2603 continue; // skip it!
2606 model
->IsContainer(item
) &&
2607 !model
->HasContainerColumns(item
))
2608 continue; // skip it!
2610 wxDataViewRenderer
*renderer
=
2611 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2612 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2614 height
= wxMax( height
, renderer
->GetSize().y
);
2625 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2627 const wxDataViewModel
*model
= GetOwner()->GetModel();
2629 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2631 wxASSERT( !IsVirtualList() );
2633 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2634 // wxASSERT( node );
2635 if (!node
) return m_lineHeight
;
2637 wxDataViewItem item
= node
->GetItem();
2639 if (node
&& !node
->HasChildren())
2641 // Yes, if the node does not have any child, it must be a leaf which
2642 // mean that it is a temporarily created by GetTreeNodeByRow
2646 int height
= m_lineHeight
;
2648 unsigned int cols
= GetOwner()->GetColumnCount();
2650 for (col
= 0; col
< cols
; col
++)
2652 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2653 if (column
->IsHidden())
2654 continue; // skip it!
2657 model
->IsContainer(item
) &&
2658 !model
->HasContainerColumns(item
))
2659 continue; // skip it!
2661 wxDataViewRenderer
*renderer
=
2662 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2663 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2665 height
= wxMax( height
, renderer
->GetSize().y
);
2672 return m_lineHeight
;
2676 class RowToItemJob
: public DoJob
2679 RowToItemJob( unsigned int row
, int current
)
2680 { this->row
= row
; this->current
= current
; }
2681 virtual ~RowToItemJob() {}
2683 virtual int operator() ( wxDataViewTreeNode
* node
)
2686 if( current
== static_cast<int>(row
))
2688 ret
= node
->GetItem();
2692 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2694 current
+= node
->GetSubTreeCount();
2699 // If the current has no child node, we can find the desired item of the row
2701 // This if can speed up finding in some case, and will has a very good effect
2702 // when it comes to list view
2703 if( node
->GetNodes().GetCount() == 0)
2705 int index
= static_cast<int>(row
) - current
- 1;
2706 ret
= node
->GetChildren().Item( index
);
2713 virtual int operator() ( void * n
)
2716 if( current
== static_cast<int>(row
))
2718 ret
= wxDataViewItem( n
);
2724 wxDataViewItem
GetResult() const
2733 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2735 if (IsVirtualList())
2737 return wxDataViewItem( wxUIntToPtr(row
+1) );
2741 RowToItemJob
job( row
, -2 );
2742 Walker( m_root
, job
);
2743 return job
.GetResult();
2747 class RowToTreeNodeJob
: public DoJob
2750 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2753 this->current
= current
;
2757 virtual ~RowToTreeNodeJob(){ }
2759 virtual int operator() ( wxDataViewTreeNode
* node
)
2762 if( current
== static_cast<int>(row
))
2768 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2770 current
+= node
->GetSubTreeCount();
2777 // If the current node has no children, we can find the desired item of the
2778 // row number directly.
2779 // This if can speed up finding in some case, and will have a very good
2780 // effect for list views.
2781 if( node
->GetNodes().GetCount() == 0)
2783 int index
= static_cast<int>(row
) - current
- 1;
2784 void * n
= node
->GetChildren().Item( index
);
2785 ret
= new wxDataViewTreeNode( parent
);
2786 ret
->SetItem( wxDataViewItem( n
));
2787 ret
->SetHasChildren(false);
2794 virtual int operator() ( void * n
)
2797 if( current
== static_cast<int>(row
))
2799 ret
= new wxDataViewTreeNode( parent
);
2800 ret
->SetItem( wxDataViewItem( n
));
2801 ret
->SetHasChildren(false);
2808 wxDataViewTreeNode
* GetResult() const
2814 wxDataViewTreeNode
* ret
;
2815 wxDataViewTreeNode
* parent
;
2818 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2820 wxASSERT( !IsVirtualList() );
2822 RowToTreeNodeJob
job( row
, -2, m_root
);
2823 Walker( m_root
, job
);
2824 return job
.GetResult();
2827 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2828 const wxDataViewItem
& item
)
2830 wxWindow
*parent
= GetParent();
2831 wxDataViewEvent
le(type
, parent
->GetId());
2833 le
.SetEventObject(parent
);
2834 le
.SetModel(GetOwner()->GetModel());
2837 parent
->GetEventHandler()->ProcessEvent(le
);
2841 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2846 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2850 if (!node
->HasChildren())
2856 return node
->IsOpen();
2859 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2864 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2868 if (!node
->HasChildren())
2877 void wxDataViewMainWindow::Expand( unsigned int row
)
2882 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2886 if (!node
->HasChildren())
2892 if (!node
->IsOpen())
2895 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2897 // Check if the user prevent expanding
2898 if( e
.GetSkipped() )
2903 // build the children of current node
2904 if( node
->GetChildrenNumber() == 0 )
2907 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2910 // By expanding the node all row indices that are currently in the selection list
2911 // and are greater than our node have become invalid. So we have to correct that now.
2912 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2913 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2915 const unsigned testRow
= m_selection
[i
];
2916 // all rows above us are not affected, so skip them
2920 m_selection
[i
] += rowAdjustment
;
2923 if(m_currentRow
> row
)
2924 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2928 // Send the expanded event
2929 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2933 void wxDataViewMainWindow::Collapse(unsigned int row
)
2938 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2942 if (!node
->HasChildren())
2951 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2952 if( e
.GetSkipped() )
2955 // Find out if there are selected items below the current node.
2956 bool selectCollapsingRow
= false;
2957 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2958 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2959 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2961 const unsigned testRow
= m_selection
[i
];
2962 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2964 selectCollapsingRow
= true;
2965 // get out as soon as we have found a node that is selected
2972 // If the node to be closed has selected items the user won't see those any longer.
2973 // We select the collapsing node in this case.
2974 if(selectCollapsingRow
)
2976 SelectAllRows(false);
2977 ChangeCurrentRow(row
);
2978 SelectRow(row
, true);
2979 SendSelectionChangedEvent(GetItemByRow(row
));
2983 // if there were no selected items below our node we still need to "fix" the
2984 // selection list to adjust for the changing of the row indices.
2985 // We actually do the opposite of what we are doing in Expand().
2986 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2988 const unsigned testRow
= m_selection
[i
];
2989 // all rows above us are not affected, so skip them
2993 m_selection
[i
] -= rowAdjustment
;
2996 // if the "current row" is being collapsed away we change it to the current row ;-)
2997 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2998 ChangeCurrentRow(row
);
2999 else if(m_currentRow
> row
)
3000 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3005 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3009 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3011 const wxDataViewModel
* model
= GetOwner()->GetModel();
3018 // Compose the parent-chain for the item we are looking for
3019 wxVector
<wxDataViewItem
> parentChain
;
3020 wxDataViewItem
it( item
);
3023 parentChain
.push_back(it
);
3024 it
= model
->GetParent(it
);
3027 // Find the item along the parent-chain.
3028 // This algorithm is designed to speed up the node-finding method
3029 wxDataViewTreeNode
* node
= m_root
;
3030 for( unsigned iter
= parentChain
.size()-1; iter
>=0; --iter
)
3032 if( node
->HasChildren() )
3034 if( node
->GetChildrenNumber() == 0 )
3037 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3040 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3043 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3045 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3046 if (currentNode
->GetItem() == parentChain
[iter
])
3048 if (currentNode
->GetItem() == item
)
3065 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3066 wxDataViewColumn
* &column
)
3068 wxDataViewColumn
*col
= NULL
;
3069 unsigned int cols
= GetOwner()->GetColumnCount();
3070 unsigned int colnum
= 0;
3072 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3073 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3075 col
= GetOwner()->GetColumnAt(colnum
);
3076 if (col
->IsHidden())
3077 continue; // skip it!
3079 unsigned int w
= col
->GetWidth();
3080 if (x_start
+w
>= (unsigned int)x
)
3087 item
= GetItemByRow( GetLineAt( y
) );
3090 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3091 const wxDataViewColumn
* column
)
3096 unsigned int cols
= GetOwner()->GetColumnCount();
3097 // If column is null the loop will compute the combined width of all columns.
3098 // Otherwise, it will compute the x position of the column we are looking for.
3099 for (unsigned int i
= 0; i
< cols
; i
++)
3101 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3106 if (col
->IsHidden())
3107 continue; // skip it!
3109 xpos
+= col
->GetWidth();
3110 width
+= col
->GetWidth();
3115 // If we have a column, we need can get its width directly.
3116 if(column
->IsHidden())
3119 width
= column
->GetWidth();
3124 // If we have no column, we reset the x position back to zero.
3128 // we have to take an expander column into account and compute its indentation
3129 // to get the correct x position where the actual text is
3131 int row
= GetRowByItem(item
);
3132 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3134 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3135 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3136 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3138 if(!node
->HasChildren())
3142 wxRect
itemRect( xpos
+ indent
,
3143 GetLineStart( row
),
3145 GetLineHeight( row
) );
3147 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3148 &itemRect
.x
, &itemRect
.y
);
3153 int wxDataViewMainWindow::RecalculateCount()
3155 if (IsVirtualList())
3157 wxDataViewVirtualListModel
*list_model
=
3158 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3160 return list_model
->GetCount();
3164 return m_root
->GetSubTreeCount();
3168 class ItemToRowJob
: public DoJob
3171 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3178 // Maybe binary search will help to speed up this process
3179 virtual int operator() ( wxDataViewTreeNode
* node
)
3182 if( node
->GetItem() == item
)
3187 if( node
->GetItem() == *m_iter
)
3194 ret
+= node
->GetSubTreeCount();
3200 virtual int operator() ( void * n
)
3203 if( n
== item
.GetID() )
3208 // the row number is begin from zero
3209 int GetResult() const
3213 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3214 wxDataViewItem item
;
3219 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3221 const wxDataViewModel
* model
= GetOwner()->GetModel();
3225 if (IsVirtualList())
3227 return wxPtrToUInt( item
.GetID() ) -1;
3234 // Compose the parent-chain of the item we are looking for
3235 wxVector
<wxDataViewItem
> parentChain
;
3236 wxDataViewItem
it( item
);
3239 parentChain
.push_back(it
);
3240 it
= model
->GetParent(it
);
3243 // add an 'invalid' item to represent our 'invisible' root node
3244 parentChain
.push_back(wxDataViewItem());
3246 // the parent chain was created by adding the deepest parent first.
3247 // so if we want to start at the root node, we have to iterate backwards through the vector
3248 ItemToRowJob
job( item
, parentChain
.rbegin() );
3249 Walker( m_root
, job
);
3250 return job
.GetResult();
3254 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3255 wxDataViewTreeNode
* node
)
3257 if( !model
->IsContainer( item
) )
3260 wxDataViewItemArray children
;
3261 unsigned int num
= model
->GetChildren( item
, children
);
3263 unsigned int index
= 0;
3264 while( index
< num
)
3266 if( model
->IsContainer( children
[index
] ) )
3268 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3269 n
->SetItem(children
[index
]);
3270 n
->SetHasChildren( true );
3275 node
->AddLeaf( children
[index
].GetID() );
3279 node
->SetSubTreeCount( num
);
3280 wxDataViewTreeNode
* n
= node
->GetParent();
3282 n
->ChangeSubTreeCount(num
);
3286 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3290 if (GetOwner()->GetModel()->IsVirtualListModel())
3296 m_root
= new wxDataViewTreeNode( NULL
);
3297 m_root
->SetHasChildren(true);
3299 // First we define a invalid item to fetch the top-level elements
3300 wxDataViewItem item
;
3302 BuildTreeHelper( model
, item
, m_root
);
3306 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3308 if( node
->GetNodeNumber() != 0 )
3310 int len
= node
->GetNodeNumber();
3311 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3312 for (int i
= 0; i
< len
; i
++)
3313 DestroyTreeHelper(nodes
[i
]);
3318 void wxDataViewMainWindow::DestroyTree()
3320 if (!IsVirtualList())
3322 ::DestroyTreeHelper(m_root
);
3328 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3330 wxWindow
* const parent
= GetParent();
3332 // propagate the char event upwards
3333 wxKeyEvent
eventForParent(event
);
3334 eventForParent
.SetEventObject(parent
);
3335 if ( parent
->ProcessWindowEvent(eventForParent
) )
3338 if ( parent
->HandleAsNavigationKey(event
) )
3341 // no item -> nothing to do
3342 if (!HasCurrentRow())
3348 // don't use m_linesPerPage directly as it might not be computed yet
3349 const int pageSize
= GetCountPerPage();
3350 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3352 switch ( event
.GetKeyCode() )
3356 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3358 le
.SetItem( GetItemByRow(m_currentRow
) );
3359 le
.SetEventObject(parent
);
3360 le
.SetModel(GetOwner()->GetModel());
3362 parent
->GetEventHandler()->ProcessEvent(le
);
3367 if ( m_currentRow
> 0 )
3368 OnArrowChar( m_currentRow
- 1, event
);
3372 if ( m_currentRow
< GetRowCount() - 1 )
3373 OnArrowChar( m_currentRow
+ 1, event
);
3375 // Add the process for tree expanding/collapsing
3381 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3385 if (node
->HasChildren() && node
->IsOpen())
3387 Collapse(m_currentRow
);
3389 else // if the node is already closed we move the selection to its parent
3391 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3393 if(!node
->HasChildren())
3398 int parent
= GetRowByItem( parent_node
->GetItem() );
3401 unsigned int row
= m_currentRow
;
3402 SelectRow( row
, false);
3403 SelectRow( parent
, true );
3404 ChangeCurrentRow( parent
);
3405 GetOwner()->EnsureVisible( parent
, -1 );
3406 SendSelectionChangedEvent( parent_node
->GetItem() );
3414 if (!IsExpanded( m_currentRow
))
3415 Expand( m_currentRow
);
3418 unsigned int row
= m_currentRow
;
3419 SelectRow( row
, false );
3420 SelectRow( row
+ 1, true );
3421 ChangeCurrentRow( row
+ 1 );
3422 GetOwner()->EnsureVisible( row
+ 1, -1 );
3423 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3430 OnArrowChar( GetRowCount() - 1, event
);
3435 OnArrowChar( 0, event
);
3440 int steps
= pageSize
- 1;
3441 int index
= m_currentRow
- steps
;
3445 OnArrowChar( index
, event
);
3451 int steps
= pageSize
- 1;
3452 unsigned int index
= m_currentRow
+ steps
;
3453 unsigned int count
= GetRowCount();
3454 if ( index
>= count
)
3457 OnArrowChar( index
, event
);
3463 if(m_selection
.size() == 1)
3465 // TODO: we need to revise that when we have a concept for a 'current column'
3466 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3476 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3478 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3480 // let the base handle mouse wheel events.
3485 // set the focus to ourself if any of the mouse buttons are pressed
3486 if(event
.ButtonDown() && !HasFocus())
3489 int x
= event
.GetX();
3490 int y
= event
.GetY();
3491 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3492 wxDataViewColumn
*col
= NULL
;
3495 unsigned int cols
= GetOwner()->GetColumnCount();
3497 for (i
= 0; i
< cols
; i
++)
3499 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3501 continue; // skip it!
3503 if (x
< xpos
+ c
->GetWidth())
3508 xpos
+= c
->GetWidth();
3516 wxDataViewRenderer
*cell
= col
->GetRenderer();
3517 unsigned int current
= GetLineAt( y
);
3518 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3520 // Unselect all if below the last row ?
3525 // Test whether the mouse is hovered on the tree item button
3526 bool hoverOverExpander
= false;
3527 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3529 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3530 if( node
!=NULL
&& node
->HasChildren() )
3532 int indent
= node
->GetIndentLevel();
3533 indent
= GetOwner()->GetIndent()*indent
;
3535 // we make the rectangle we are looking in a bit bigger than the actual
3536 // visual expander so the user can hit that little thing reliably
3537 wxRect
rect( xpos
+ indent
,
3538 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3539 m_lineHeight
, m_lineHeight
);
3540 if( rect
.Contains(x
, y
) )
3542 // So the mouse is over the expander
3543 hoverOverExpander
= true;
3544 if (m_underMouse
&& m_underMouse
!= node
)
3546 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3547 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3549 if (m_underMouse
!= node
)
3551 // wxLogMessage("Do the row: %d", current);
3552 RefreshRow(current
);
3554 m_underMouse
= node
;
3557 if (node
!=NULL
&& !node
->HasChildren())
3560 if (!hoverOverExpander
)
3562 if (m_underMouse
!= NULL
)
3564 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3565 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3566 m_underMouse
= NULL
;
3570 wxDataViewModel
*model
= GetOwner()->GetModel();
3572 #if wxUSE_DRAG_AND_DROP
3573 if (event
.Dragging())
3575 if (m_dragCount
== 0)
3577 // we have to report the raw, physical coords as we want to be
3578 // able to call HitTest(event.m_pointDrag) from the user code to
3579 // get the item being dragged
3580 m_dragStart
= event
.GetPosition();
3585 if (m_dragCount
!= 3)
3588 if (event
.LeftIsDown())
3590 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3591 &m_dragStart
.x
, &m_dragStart
.y
);
3592 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3593 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3595 // Notify cell about drag
3596 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3597 event
.SetEventObject( m_owner
);
3598 event
.SetItem( item
);
3599 event
.SetModel( model
);
3600 if (!m_owner
->HandleWindowEvent( event
))
3603 if (!event
.IsAllowed())
3606 wxDataObject
*obj
= event
.GetDataObject();
3610 wxDataViewDropSource
drag( this, drag_item_row
);
3611 drag
.SetData( *obj
);
3612 /* wxDragResult res = */ drag
.DoDragDrop();
3621 #endif // wxUSE_DRAG_AND_DROP
3623 bool simulateClick
= false;
3625 if (event
.ButtonDClick())
3627 m_renameTimer
->Stop();
3628 m_lastOnSame
= false;
3631 wxDataViewItem item
= GetItemByRow(current
);
3632 bool ignore_other_columns
=
3633 ((GetOwner()->GetExpanderColumn() != col
) &&
3634 (model
->IsContainer(item
)) &&
3635 (!model
->HasContainerColumns(item
)));
3637 if (event
.LeftDClick())
3639 if(hoverOverExpander
)
3641 // a double click on the expander will be converted into a "simulated" normal click
3642 simulateClick
= true;
3644 else if ( current
== m_lineLastClicked
)
3646 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3648 const unsigned colIdx
= col
->GetModelColumn();
3650 cell
->PrepareForItem(model
, item
, colIdx
);
3652 wxRect
cell_rect( xpos
, GetLineStart( current
),
3653 col
->GetWidth(), GetLineHeight( current
) );
3654 cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3658 wxWindow
*parent
= GetParent();
3659 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3661 le
.SetColumn( col
->GetModelColumn() );
3662 le
.SetDataViewColumn( col
);
3663 le
.SetEventObject(parent
);
3664 le
.SetModel(GetOwner()->GetModel());
3666 parent
->GetEventHandler()->ProcessEvent(le
);
3672 // The first click was on another item, so don't interpret this as
3673 // a double click, but as a simple click instead
3674 simulateClick
= true;
3678 if (event
.LeftUp() && !hoverOverExpander
)
3680 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3682 // select single line
3683 SelectAllRows( false );
3684 SelectRow( m_lineSelectSingleOnUp
, true );
3685 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3688 // If the user click the expander, we do not do editing even if the column
3689 // with expander are editable
3690 if (m_lastOnSame
&& !ignore_other_columns
)
3692 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3693 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3695 m_renameTimer
->Start( 100, true );
3699 m_lastOnSame
= false;
3700 m_lineSelectSingleOnUp
= (unsigned int)-1;
3702 else if(!event
.LeftUp())
3704 // This is necessary, because after a DnD operation in
3705 // from and to ourself, the up event is swallowed by the
3706 // DnD code. So on next non-up event (which means here and
3707 // now) m_lineSelectSingleOnUp should be reset.
3708 m_lineSelectSingleOnUp
= (unsigned int)-1;
3711 if (event
.RightDown())
3713 m_lineBeforeLastClicked
= m_lineLastClicked
;
3714 m_lineLastClicked
= current
;
3716 // If the item is already selected, do not update the selection.
3717 // Multi-selections should not be cleared if a selected item is clicked.
3718 if (!IsRowSelected(current
))
3720 SelectAllRows(false);
3721 ChangeCurrentRow(current
);
3722 SelectRow(m_currentRow
,true);
3723 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3726 else if (event
.RightUp())
3729 model
->GetValue( value
, item
, col
->GetModelColumn() );
3730 wxWindow
*parent
= GetParent();
3731 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3733 le
.SetColumn( col
->GetModelColumn() );
3734 le
.SetDataViewColumn( col
);
3735 le
.SetEventObject(parent
);
3736 le
.SetModel(GetOwner()->GetModel());
3738 parent
->GetEventHandler()->ProcessEvent(le
);
3740 else if (event
.MiddleDown())
3744 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3746 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3748 // hoverOverExpander being true tells us that our node must be
3749 // valid and have children.
3750 // So we don't need any extra checks.
3751 if( node
->IsOpen() )
3756 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3758 m_lineBeforeLastClicked
= m_lineLastClicked
;
3759 m_lineLastClicked
= current
;
3761 unsigned int oldCurrentRow
= m_currentRow
;
3762 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3764 bool cmdModifierDown
= event
.CmdDown();
3765 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3767 if ( IsSingleSel() || !IsRowSelected(current
) )
3769 SelectAllRows( false );
3770 ChangeCurrentRow(current
);
3771 SelectRow(m_currentRow
,true);
3772 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3774 else // multi sel & current is highlighted & no mod keys
3776 m_lineSelectSingleOnUp
= current
;
3777 ChangeCurrentRow(current
); // change focus
3780 else // multi sel & either ctrl or shift is down
3782 if (cmdModifierDown
)
3784 ChangeCurrentRow(current
);
3785 ReverseRowSelection(m_currentRow
);
3786 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3788 else if (event
.ShiftDown())
3790 ChangeCurrentRow(current
);
3792 unsigned int lineFrom
= oldCurrentRow
,
3795 if ( lineTo
< lineFrom
)
3798 lineFrom
= m_currentRow
;
3801 SelectRows(lineFrom
, lineTo
, true);
3802 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3804 else // !ctrl, !shift
3806 // test in the enclosing if should make it impossible
3807 wxFAIL_MSG( wxT("how did we get here?") );
3811 if (m_currentRow
!= oldCurrentRow
)
3812 RefreshRow( oldCurrentRow
);
3814 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3816 // Update selection here...
3819 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3820 (current
== oldCurrentRow
)) && oldWasSelected
;
3822 // Call LeftClick after everything else as under GTK+
3823 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3825 // notify cell about click
3826 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3828 wxRect
cell_rect( xpos
, GetLineStart( current
),
3829 col
->GetWidth(), GetLineHeight( current
) );
3831 // Report position relative to the cell's custom area, i.e.
3832 // no the entire space as given by the control but the one
3833 // used by the renderer after calculation of alignment etc.
3835 // adjust the rectangle ourselves to account for the alignment
3836 wxRect rectItem
= cell_rect
;
3837 const int align
= cell
->GetAlignment();
3838 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3840 const wxSize size
= cell
->GetSize();
3842 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3844 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3845 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3846 else if ( align
& wxALIGN_RIGHT
)
3847 rectItem
.x
+= cell_rect
.width
- size
.x
;
3848 // else: wxALIGN_LEFT is the default
3851 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3853 if ( align
& wxALIGN_CENTER_VERTICAL
)
3854 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3855 else if ( align
& wxALIGN_BOTTOM
)
3856 rectItem
.y
+= cell_rect
.height
- size
.y
;
3857 // else: wxALIGN_TOP is the default
3861 wxPoint
pos( event
.GetPosition() );
3862 pos
.x
-= rectItem
.x
;
3863 pos
.y
-= rectItem
.y
;
3865 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3867 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3868 model
, item
, col
->GetModelColumn());
3873 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3877 if (HasCurrentRow())
3883 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3887 if (HasCurrentRow())
3893 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3895 if( m_selection
.GetCount() != 1 )
3896 return wxDataViewItem();
3898 return GetItemByRow( m_selection
.Item(0));
3901 //-----------------------------------------------------------------------------
3903 //-----------------------------------------------------------------------------
3905 WX_DEFINE_LIST(wxDataViewColumnList
)
3907 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3908 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3909 EVT_SIZE(wxDataViewCtrl::OnSize
)
3912 wxDataViewCtrl::~wxDataViewCtrl()
3915 GetModel()->RemoveNotifier( m_notifier
);
3918 m_colsBestWidths
.clear();
3921 void wxDataViewCtrl::Init()
3923 m_cols
.DeleteContents(true);
3926 // No sorting column at start
3927 m_sortingColumnIdx
= wxNOT_FOUND
;
3929 m_headerArea
= NULL
;
3932 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3937 const wxValidator
& validator
,
3938 const wxString
& name
)
3940 // if ( (style & wxBORDER_MASK) == 0)
3941 // style |= wxBORDER_SUNKEN;
3945 if (!wxControl::Create( parent
, id
, pos
, size
,
3946 style
| wxScrolledWindowStyle
, validator
, name
))
3949 SetInitialSize(size
);
3952 MacSetClipChildren( true );
3955 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3957 // We use the cursor keys for moving the selection, not scrolling, so call
3958 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3959 // keyboard events forwarded to us from wxListMainWindow.
3960 DisableKeyboardScrolling();
3962 if (HasFlag(wxDV_NO_HEADER
))
3963 m_headerArea
= NULL
;
3965 m_headerArea
= new wxDataViewHeaderWindow(this);
3967 SetTargetWindow( m_clientArea
);
3969 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3971 sizer
->Add( m_headerArea
, 0, wxGROW
);
3972 sizer
->Add( m_clientArea
, 1, wxGROW
);
3978 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3980 return wxBORDER_THEME
;
3984 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3988 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3991 // we need to process arrows ourselves for scrolling
3992 if ( nMsg
== WM_GETDLGCODE
)
3994 rc
|= DLGC_WANTARROWS
;
4002 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4004 wxSize newsize
= size
;
4005 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4006 newsize
.y
-= m_headerArea
->GetSize().y
;
4011 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4013 // We need to override OnSize so that our scrolled
4014 // window a) does call Layout() to use sizers for
4015 // positioning the controls but b) does not query
4016 // the sizer for their size and use that for setting
4017 // the scrollable area as set that ourselves by
4018 // calling SetScrollbar() further down.
4025 void wxDataViewCtrl::SetFocus()
4028 m_clientArea
->SetFocus();
4031 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4033 if (!wxDataViewCtrlBase::AssociateModel( model
))
4036 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4038 model
->AddNotifier( m_notifier
);
4040 m_clientArea
->DestroyTree();
4042 m_clientArea
->BuildTree(model
);
4044 m_clientArea
->UpdateDisplay();
4049 #if wxUSE_DRAG_AND_DROP
4051 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4053 return m_clientArea
->EnableDragSource( format
);
4056 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4058 return m_clientArea
->EnableDropTarget( format
);
4061 #endif // wxUSE_DRAG_AND_DROP
4063 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4065 if (!wxDataViewCtrlBase::AppendColumn(col
))
4068 m_cols
.Append( col
);
4069 m_colsBestWidths
.push_back(0);
4070 OnColumnsCountChanged();
4074 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4076 if (!wxDataViewCtrlBase::PrependColumn(col
))
4079 m_cols
.Insert( col
);
4080 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4081 OnColumnsCountChanged();
4085 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4087 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4090 m_cols
.Insert( pos
, col
);
4091 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4092 OnColumnsCountChanged();
4096 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4099 m_headerArea
->UpdateColumn(idx
);
4101 m_clientArea
->UpdateDisplay();
4104 void wxDataViewCtrl::OnColumnsCountChanged()
4107 m_headerArea
->SetColumnCount(GetColumnCount());
4109 m_clientArea
->UpdateDisplay();
4112 void wxDataViewCtrl::DoSetExpanderColumn()
4114 m_clientArea
->UpdateDisplay();
4117 void wxDataViewCtrl::DoSetIndent()
4119 m_clientArea
->UpdateDisplay();
4122 unsigned int wxDataViewCtrl::GetColumnCount() const
4124 return m_cols
.GetCount();
4127 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4132 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4134 // columns can't be reordered if there is no header window which allows
4136 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4139 return GetColumn(idx
);
4142 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4144 const unsigned count
= m_cols
.size();
4145 for ( unsigned n
= 0; n
< count
; n
++ )
4147 if ( m_cols
[n
] == column
)
4154 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4156 if ( m_colsBestWidths
[idx
] != 0 )
4157 return m_colsBestWidths
[idx
];
4159 const int count
= m_clientArea
->GetRowCount();
4160 wxDataViewColumn
*column
= GetColumn(idx
);
4161 wxDataViewRenderer
*renderer
=
4162 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4164 class MaxWidthCalculator
4167 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4168 wxDataViewRenderer
*renderer
,
4169 const wxDataViewModel
*model
,
4172 m_clientArea(clientArea
),
4173 m_renderer(renderer
),
4179 void UpdateWithWidth(int width
)
4181 m_width
= wxMax(m_width
, width
);
4184 void UpdateWithRow(int row
)
4186 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4187 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4188 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4191 int GetMaxWidth() const { return m_width
; }
4195 wxDataViewMainWindow
*m_clientArea
;
4196 wxDataViewRenderer
*m_renderer
;
4197 const wxDataViewModel
*m_model
;
4201 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4202 GetModel(), column
->GetModelColumn());
4206 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4207 // Labels on native MSW header are indented on both sides
4209 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4210 calculator
.UpdateWithWidth(header_width
);
4213 // The code below deserves some explanation. For very large controls, we
4214 // simply can't afford to calculate sizes for all items, it takes too
4215 // long. So the best we can do is to check the first and the last N/2
4216 // items in the control for some sufficiently large N and calculate best
4217 // sizes from that. That can result in the calculated best width being too
4218 // small for some outliers, but it's better to get slightly imperfect
4219 // result than to wait several seconds after every update. To avoid highly
4220 // visible miscalculations, we also include all currently visible items
4221 // no matter what. Finally, the value of N is determined dynamically by
4222 // measuring how much time we spent on the determining item widths so far.
4225 int top_part_end
= count
;
4226 static const long CALC_TIMEOUT
= 20/*ms*/;
4227 // don't call wxStopWatch::Time() too often
4228 static const unsigned CALC_CHECK_FREQ
= 100;
4231 // use some hard-coded limit, that's the best we can do without timer
4232 int top_part_end
= wxMin(500, count
);
4233 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4237 for ( row
= 0; row
< top_part_end
; row
++ )
4240 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4241 timer
.Time() > CALC_TIMEOUT
)
4243 #endif // wxUSE_STOPWATCH
4244 calculator
.UpdateWithRow(row
);
4247 // row is the first unmeasured item now; that's our value of N/2
4253 // add bottom N/2 items now:
4254 const int bottom_part_start
= wxMax(row
, count
- row
);
4255 for ( row
= bottom_part_start
; row
< count
; row
++ )
4257 calculator
.UpdateWithRow(row
);
4260 // finally, include currently visible items in the calculation:
4261 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4262 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4263 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4265 first_visible
= wxMax(first_visible
, top_part_end
);
4266 last_visible
= wxMin(bottom_part_start
, last_visible
);
4268 for ( row
= first_visible
; row
< last_visible
; row
++ )
4270 calculator
.UpdateWithRow(row
);
4273 wxLogTrace("dataview",
4274 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4276 count
- bottom_part_start
,
4277 wxMax(0, last_visible
- first_visible
),
4281 int max_width
= calculator
.GetMaxWidth();
4282 if ( max_width
> 0 )
4283 max_width
+= 2 * PADDING_RIGHTLEFT
;
4285 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4289 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4290 unsigned int WXUNUSED(new_pos
))
4292 // do _not_ reorder m_cols elements here, they should always be in the
4293 // order in which columns were added, we only display the columns in
4295 m_clientArea
->UpdateDisplay();
4298 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4300 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4304 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4306 OnColumnsCountChanged();
4311 bool wxDataViewCtrl::ClearColumns()
4314 m_colsBestWidths
.clear();
4315 OnColumnsCountChanged();
4319 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4321 m_colsBestWidths
[idx
] = 0;
4324 m_headerArea
->UpdateColumn(idx
);
4327 void wxDataViewCtrl::InvalidateColBestWidths()
4329 m_colsBestWidths
.clear();
4330 m_colsBestWidths
.resize(m_cols
.size());
4334 const unsigned cols
= m_headerArea
->GetColumnCount();
4335 for ( unsigned i
= 0; i
< cols
; i
++ )
4336 m_headerArea
->UpdateColumn(i
);
4340 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4343 unsigned int len
= GetColumnCount();
4344 for ( unsigned int i
= 0; i
< len
; i
++ )
4346 wxDataViewColumn
* col
= GetColumnAt(i
);
4353 // This returns the position in pixels which is not what we want.
4356 unsigned int len
= GetColumnCount();
4357 for ( unsigned int i
= 0; i
< len
; i
++ )
4359 wxDataViewColumn
* col
= GetColumnAt(i
);
4360 if (col
->IsHidden())
4362 ret
+= col
->GetWidth();
4365 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4373 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4375 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4376 : GetColumn(m_sortingColumnIdx
);
4379 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4381 return GetItemByRow(m_clientArea
->GetCurrentRow());
4384 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4386 const int row
= m_clientArea
->GetRowByItem(item
);
4388 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4389 if ( static_cast<unsigned>(row
) != oldCurrent
)
4391 m_clientArea
->ChangeCurrentRow(row
);
4392 m_clientArea
->RefreshRow(oldCurrent
);
4393 m_clientArea
->RefreshRow(row
);
4397 // Selection code with wxDataViewItem as parameters
4398 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4400 return m_clientArea
->GetSelection();
4403 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4406 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4407 int len
= selection
.GetCount();
4408 for( int i
= 0; i
< len
; i
++)
4410 unsigned int row
= selection
[i
];
4411 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4416 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4418 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4420 wxDataViewItem last_parent
;
4422 int len
= sel
.GetCount();
4423 for( int i
= 0; i
< len
; i
++ )
4425 wxDataViewItem item
= sel
[i
];
4426 wxDataViewItem parent
= GetModel()->GetParent( item
);
4429 if (parent
!= last_parent
)
4430 ExpandAncestors(item
);
4433 last_parent
= parent
;
4434 int row
= m_clientArea
->GetRowByItem( item
);
4436 selection
.Add( static_cast<unsigned int>(row
) );
4439 m_clientArea
->SetSelections( selection
);
4442 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4444 ExpandAncestors( item
);
4446 int row
= m_clientArea
->GetRowByItem( item
);
4449 // Unselect all rows before select another in the single select mode
4450 if (m_clientArea
->IsSingleSel())
4451 m_clientArea
->SelectAllRows(false);
4453 m_clientArea
->SelectRow(row
, true);
4455 // Also set focus to the selected item
4456 m_clientArea
->ChangeCurrentRow( row
);
4460 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4462 int row
= m_clientArea
->GetRowByItem( item
);
4464 m_clientArea
->SelectRow(row
, false);
4467 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4469 int row
= m_clientArea
->GetRowByItem( item
);
4472 return m_clientArea
->IsRowSelected(row
);
4477 // Selection code with row number as parameter
4478 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4481 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4482 int len
= selection
.GetCount();
4483 for( int i
= 0; i
< len
; i
++)
4485 unsigned int row
= selection
[i
];
4491 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4493 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4494 int len
= sel
.GetCount();
4495 for( int i
= 0; i
< len
; i
++ )
4499 selection
.Add( static_cast<unsigned int>(row
) );
4501 m_clientArea
->SetSelections( selection
);
4504 void wxDataViewCtrl::Select( int row
)
4508 if (m_clientArea
->IsSingleSel())
4509 m_clientArea
->SelectAllRows(false);
4510 m_clientArea
->SelectRow( row
, true );
4514 void wxDataViewCtrl::Unselect( int row
)
4517 m_clientArea
->SelectRow(row
, false);
4520 bool wxDataViewCtrl::IsSelected( int row
) const
4523 return m_clientArea
->IsRowSelected(row
);
4527 void wxDataViewCtrl::SelectRange( int from
, int to
)
4530 for( int i
= from
; i
< to
; i
++ )
4532 m_clientArea
->Select(sel
);
4535 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4537 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4538 for( int i
= from
; i
< to
; i
++ )
4539 if( sel
.Index( i
) != wxNOT_FOUND
)
4541 m_clientArea
->SetSelections(sel
);
4544 void wxDataViewCtrl::SelectAll()
4546 m_clientArea
->SelectAllRows(true);
4549 void wxDataViewCtrl::UnselectAll()
4551 m_clientArea
->SelectAllRows(false);
4554 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4558 if( row
> (int) m_clientArea
->GetRowCount() )
4559 row
= m_clientArea
->GetRowCount();
4561 int first
= m_clientArea
->GetFirstVisibleRow();
4562 int last
= m_clientArea
->GetLastVisibleRow();
4564 m_clientArea
->ScrollTo( row
, column
);
4565 else if( row
> last
)
4566 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4568 m_clientArea
->ScrollTo( first
, column
);
4571 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4573 ExpandAncestors( item
);
4575 m_clientArea
->RecalculateDisplay();
4577 int row
= m_clientArea
->GetRowByItem(item
);
4580 if( column
== NULL
)
4581 EnsureVisible(row
, -1);
4583 EnsureVisible( row
, GetColumnIndex(column
) );
4588 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4589 wxDataViewColumn
* &column
) const
4591 m_clientArea
->HitTest(point
, item
, column
);
4594 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4595 const wxDataViewColumn
* column
) const
4597 return m_clientArea
->GetItemRect(item
, column
);
4600 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4602 return m_clientArea
->GetItemByRow( row
);
4605 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4607 return m_clientArea
->GetRowByItem( item
);
4610 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4612 ExpandAncestors( item
);
4614 int row
= m_clientArea
->GetRowByItem( item
);
4616 m_clientArea
->Expand(row
);
4619 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4621 int row
= m_clientArea
->GetRowByItem( item
);
4623 m_clientArea
->Collapse(row
);
4626 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4628 int row
= m_clientArea
->GetRowByItem( item
);
4630 return m_clientArea
->IsExpanded(row
);
4634 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4636 wxDataViewColumn
* col
= GetColumn( column
);
4640 wxRect itemRect
= GetItemRect(item
, col
);
4641 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4642 if (renderer
->GetMode() == wxDATAVIEW_CELL_EDITABLE
)
4643 renderer
->StartEditing(item
, itemRect
);
4646 #endif // !wxUSE_GENERICDATAVIEWCTRL
4648 #endif // wxUSE_DATAVIEWCTRL