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 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
537 // Some useful functions for row and item mapping
538 wxDataViewItem
GetItemByRow( unsigned int row
) const;
539 int GetRowByItem( const wxDataViewItem
& item
) const;
541 // Methods for building the mapping tree
542 void BuildTree( wxDataViewModel
* model
);
544 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
545 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
547 void Expand( unsigned int row
);
548 void Collapse( unsigned int row
);
549 bool IsExpanded( unsigned int row
) const;
550 bool HasChildren( unsigned int row
) const;
552 #if wxUSE_DRAG_AND_DROP
553 bool EnableDragSource( const wxDataFormat
&format
);
554 bool EnableDropTarget( const wxDataFormat
&format
);
556 void RemoveDropHint();
557 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
558 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
559 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
561 #endif // wxUSE_DRAG_AND_DROP
564 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
565 // We did not need this temporarily
566 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
568 int RecalculateCount();
570 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
572 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
575 wxDataViewCtrl
*m_owner
;
579 wxDataViewColumn
*m_currentCol
;
580 unsigned int m_currentRow
;
581 wxDataViewSelection m_selection
;
583 wxDataViewRenameTimer
*m_renameTimer
;
588 #if wxUSE_DRAG_AND_DROP
593 wxDataFormat m_dragFormat
;
596 wxDataFormat m_dropFormat
;
598 unsigned int m_dropHintLine
;
599 #endif // wxUSE_DRAG_AND_DROP
601 // for double click logic
602 unsigned int m_lineLastClicked
,
603 m_lineBeforeLastClicked
,
604 m_lineSelectSingleOnUp
;
606 // the pen used to draw horiz/vertical rules
609 // the pen used to draw the expander and the lines
612 // This is the tree structure of the model
613 wxDataViewTreeNode
* m_root
;
616 // This is the tree node under the cursor
617 wxDataViewTreeNode
* m_underMouse
;
620 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
621 DECLARE_EVENT_TABLE()
624 // ---------------------------------------------------------
625 // wxGenericDataViewModelNotifier
626 // ---------------------------------------------------------
628 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
631 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
632 { m_mainWindow
= mainWindow
; }
634 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
635 { return m_mainWindow
->ItemAdded( parent
, item
); }
636 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
637 { return m_mainWindow
->ItemDeleted( parent
, item
); }
638 virtual bool ItemChanged( const wxDataViewItem
& item
)
639 { return m_mainWindow
->ItemChanged(item
); }
640 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
641 { return m_mainWindow
->ValueChanged( item
, col
); }
642 virtual bool Cleared()
643 { return m_mainWindow
->Cleared(); }
644 virtual void Resort()
645 { m_mainWindow
->Resort(); }
647 wxDataViewMainWindow
*m_mainWindow
;
650 // ---------------------------------------------------------
651 // wxDataViewRenderer
652 // ---------------------------------------------------------
654 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
656 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
657 wxDataViewCellMode mode
,
659 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
663 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
667 wxDataViewRenderer::~wxDataViewRenderer()
672 wxDC
*wxDataViewRenderer::GetDC()
676 if (GetOwner() == NULL
)
678 if (GetOwner()->GetOwner() == NULL
)
680 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
686 void wxDataViewRenderer::SetAlignment( int align
)
691 int wxDataViewRenderer::GetAlignment() const
696 // ---------------------------------------------------------
697 // wxDataViewCustomRenderer
698 // ---------------------------------------------------------
700 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
702 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
703 wxDataViewCellMode mode
, int align
) :
704 wxDataViewRenderer( varianttype
, mode
, align
)
708 // ---------------------------------------------------------
709 // wxDataViewTextRenderer
710 // ---------------------------------------------------------
712 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
714 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
715 wxDataViewCellMode mode
, int align
) :
716 wxDataViewRenderer( varianttype
, mode
, align
)
720 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
722 m_text
= value
.GetString();
727 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
732 bool wxDataViewTextRenderer::HasEditorCtrl() const
737 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
738 wxRect labelRect
, const wxVariant
&value
)
740 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
741 wxPoint(labelRect
.x
,labelRect
.y
),
742 wxSize(labelRect
.width
,labelRect
.height
) );
744 // select the text in the control an place the cursor at the end
745 ctrl
->SetInsertionPointEnd();
751 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
753 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
754 value
= text
->GetValue();
758 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
760 RenderText(m_text
, 0, rect
, dc
, state
);
764 wxSize
wxDataViewTextRenderer::GetSize() const
767 return GetTextExtent(m_text
);
769 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
772 // ---------------------------------------------------------
773 // wxDataViewBitmapRenderer
774 // ---------------------------------------------------------
776 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
778 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
779 wxDataViewCellMode mode
, int align
) :
780 wxDataViewRenderer( varianttype
, mode
, align
)
784 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
786 if (value
.GetType() == wxT("wxBitmap"))
788 if (value
.GetType() == wxT("wxIcon"))
794 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
799 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
802 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
803 else if (m_icon
.IsOk())
804 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
809 wxSize
wxDataViewBitmapRenderer::GetSize() const
812 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
813 else if (m_icon
.IsOk())
814 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
816 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
819 // ---------------------------------------------------------
820 // wxDataViewToggleRenderer
821 // ---------------------------------------------------------
823 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
825 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
826 wxDataViewCellMode mode
, int align
) :
827 wxDataViewRenderer( varianttype
, mode
, align
)
832 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
834 m_toggle
= value
.GetBool();
839 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
844 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
848 flags
|= wxCONTROL_CHECKED
;
849 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
850 GetEnabled() == false)
851 flags
|= wxCONTROL_DISABLED
;
853 // check boxes we draw must always have the same, standard size (if it's
854 // bigger than the cell size the checkbox will be truncated because the
855 // caller had set the clipping rectangle to prevent us from drawing outside
857 cell
.SetSize(GetSize());
859 wxRendererNative::Get().DrawCheckBox(
860 GetOwner()->GetOwner(),
868 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
869 const wxRect
& WXUNUSED(cell
),
870 wxDataViewModel
*model
,
871 const wxDataViewItem
& item
,
874 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
875 if (!wxRect(GetSize()).Contains(cursor
))
878 if (model
->IsEnabled(item
, col
))
880 model
->ChangeValue(!m_toggle
, item
, col
);
887 wxSize
wxDataViewToggleRenderer::GetSize() const
889 // the window parameter is not used by GetCheckBoxSize() so it's
891 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
894 // ---------------------------------------------------------
895 // wxDataViewProgressRenderer
896 // ---------------------------------------------------------
898 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
900 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
901 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
902 wxDataViewRenderer( varianttype
, mode
, align
)
908 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
910 m_value
= (long) value
;
912 if (m_value
< 0) m_value
= 0;
913 if (m_value
> 100) m_value
= 100;
918 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
920 value
= (long) m_value
;
925 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
927 // deflate the rect to leave a small border between bars in adjacent rows
928 wxRect bar
= rect
.Deflate(0, 1);
930 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
931 dc
->SetPen( *wxBLACK_PEN
);
932 dc
->DrawRectangle( bar
);
934 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
935 dc
->SetPen( *wxTRANSPARENT_PEN
);
937 const wxDataViewItemAttr
& attr
= GetAttr();
938 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
940 dc
->DrawRectangle( bar
);
945 wxSize
wxDataViewProgressRenderer::GetSize() const
947 return wxSize(40,12);
950 // ---------------------------------------------------------
951 // wxDataViewDateRenderer
952 // ---------------------------------------------------------
954 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
956 #if wxUSE_DATE_RENDERER_POPUP
958 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
961 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
962 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
963 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
968 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
969 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
970 sizer
->Add( m_cal
, 1, wxGROW
);
975 void OnCalendar( wxCalendarEvent
&event
);
977 wxCalendarCtrl
*m_cal
;
978 wxDataViewModel
*m_model
;
980 const wxDataViewItem
& m_item
;
983 virtual void OnDismiss()
988 DECLARE_EVENT_TABLE()
991 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
992 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
995 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
997 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1001 #endif // wxUSE_DATE_RENDERER_POPUP
1003 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1005 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1006 wxDataViewCellMode mode
, int align
) :
1007 wxDataViewRenderer( varianttype
, mode
, align
)
1011 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1013 m_date
= value
.GetDateTime();
1018 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1024 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1026 wxString tmp
= m_date
.FormatDate();
1027 RenderText( tmp
, 0, cell
, dc
, state
);
1031 wxSize
wxDataViewDateRenderer::GetSize() const
1033 return GetTextExtent(m_date
.FormatDate());
1036 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1037 wxDataViewModel
*model
,
1038 const wxDataViewItem
& item
,
1041 wxDateTime dtOld
= m_date
;
1043 #if wxUSE_DATE_RENDERER_POPUP
1044 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1045 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1046 wxPoint pos
= wxGetMousePosition();
1049 popup
->Popup( popup
->m_cal
);
1050 #else // !wxUSE_DATE_RENDERER_POPUP
1051 wxMessageBox(dtOld
.Format());
1052 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1057 // ---------------------------------------------------------
1058 // wxDataViewIconTextRenderer
1059 // ---------------------------------------------------------
1061 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1063 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1064 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1065 wxDataViewRenderer( varianttype
, mode
, align
)
1068 SetAlignment(align
);
1071 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1077 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1082 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1086 const wxIcon
& icon
= m_value
.GetIcon();
1089 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1090 xoffset
= icon
.GetWidth()+4;
1093 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1098 wxSize
wxDataViewIconTextRenderer::GetSize() const
1100 if (!m_value
.GetText().empty())
1102 wxSize size
= GetTextExtent(m_value
.GetText());
1104 if (m_value
.GetIcon().IsOk())
1105 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1108 return wxSize(80,20);
1111 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1113 wxDataViewIconText iconText
;
1116 wxString text
= iconText
.GetText();
1118 // adjust the label rect to take the width of the icon into account
1119 if (iconText
.GetIcon().IsOk())
1121 int w
= iconText
.GetIcon().GetWidth() + 4;
1123 labelRect
.width
-= w
;
1126 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1127 wxPoint(labelRect
.x
,labelRect
.y
),
1128 wxSize(labelRect
.width
,labelRect
.height
) );
1130 // select the text in the control an place the cursor at the end
1131 ctrl
->SetInsertionPointEnd();
1137 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1139 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1141 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1146 //-----------------------------------------------------------------------------
1147 // wxDataViewDropTarget
1148 //-----------------------------------------------------------------------------
1150 #if wxUSE_DRAG_AND_DROP
1152 class wxBitmapCanvas
: public wxWindow
1155 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1156 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1159 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1162 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1165 dc
.DrawBitmap( m_bitmap
, 0, 0);
1171 class wxDataViewDropSource
: public wxDropSource
1174 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1182 ~wxDataViewDropSource()
1187 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1189 wxPoint pos
= wxGetMousePosition();
1193 int liney
= m_win
->GetLineStart( m_row
);
1195 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1196 m_win
->ClientToScreen( &linex
, &liney
);
1197 m_dist_x
= pos
.x
- linex
;
1198 m_dist_y
= pos
.y
- liney
;
1201 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1203 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1204 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1206 wxFRAME_TOOL_WINDOW
|
1207 wxFRAME_FLOAT_ON_PARENT
|
1208 wxFRAME_NO_TASKBAR
|
1210 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1215 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1216 m_hint
->SetTransparent( 128 );
1222 wxDataViewMainWindow
*m_win
;
1225 int m_dist_x
,m_dist_y
;
1229 class wxDataViewDropTarget
: public wxDropTarget
1232 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1238 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1240 wxDataFormat format
= GetMatchingPair();
1241 if (format
== wxDF_INVALID
)
1243 return m_win
->OnDragOver( format
, x
, y
, def
);
1246 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1248 wxDataFormat format
= GetMatchingPair();
1249 if (format
== wxDF_INVALID
)
1251 return m_win
->OnDrop( format
, x
, y
);
1254 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1256 wxDataFormat format
= GetMatchingPair();
1257 if (format
== wxDF_INVALID
)
1261 return m_win
->OnData( format
, x
, y
, def
);
1264 virtual void OnLeave()
1265 { m_win
->OnLeave(); }
1267 wxDataViewMainWindow
*m_win
;
1270 #endif // wxUSE_DRAG_AND_DROP
1272 //-----------------------------------------------------------------------------
1273 // wxDataViewRenameTimer
1274 //-----------------------------------------------------------------------------
1276 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1281 void wxDataViewRenameTimer::Notify()
1283 m_owner
->OnRenameTimer();
1286 //-----------------------------------------------------------------------------
1287 // wxDataViewMainWindow
1288 //-----------------------------------------------------------------------------
1290 // The tree building helper, declared firstly
1291 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1292 wxDataViewTreeNode
* node
);
1294 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1296 if (row1
> row2
) return 1;
1297 if (row1
== row2
) return 0;
1301 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1303 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1304 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1305 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1306 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1307 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1308 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1311 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1312 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1313 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1314 m_selection( wxDataViewSelectionCmp
)
1319 m_lastOnSame
= false;
1320 m_renameTimer
= new wxDataViewRenameTimer( this );
1322 // TODO: user better initial values/nothing selected
1323 m_currentCol
= NULL
;
1326 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1328 #if wxUSE_DRAG_AND_DROP
1330 m_dragStart
= wxPoint(0,0);
1332 m_dragEnabled
= false;
1333 m_dropEnabled
= false;
1335 m_dropHintLine
= (unsigned int) -1;
1336 #endif // wxUSE_DRAG_AND_DROP
1338 m_lineLastClicked
= (unsigned int) -1;
1339 m_lineBeforeLastClicked
= (unsigned int) -1;
1340 m_lineSelectSingleOnUp
= (unsigned int) -1;
1344 SetBackgroundColour( *wxWHITE
);
1346 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1348 m_penRule
= wxPen(GetRuleColour());
1350 // compose a pen whichcan draw black lines
1351 // TODO: maybe there is something system colour to use
1352 m_penExpander
= wxPen(wxColour(0,0,0));
1354 m_root
= new wxDataViewTreeNode( NULL
);
1355 m_root
->SetHasChildren(true);
1357 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1359 m_underMouse
= NULL
;
1363 wxDataViewMainWindow::~wxDataViewMainWindow()
1366 delete m_renameTimer
;
1370 #if wxUSE_DRAG_AND_DROP
1371 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1373 m_dragFormat
= format
;
1374 m_dragEnabled
= format
!= wxDF_INVALID
;
1379 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1381 m_dropFormat
= format
;
1382 m_dropEnabled
= format
!= wxDF_INVALID
;
1385 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1390 void wxDataViewMainWindow::RemoveDropHint()
1395 RefreshRow( m_dropHintLine
);
1396 m_dropHintLine
= (unsigned int) -1;
1400 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1401 wxCoord y
, wxDragResult def
)
1405 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1406 unsigned int row
= GetLineAt( yy
);
1408 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1414 wxDataViewItem item
= GetItemByRow( row
);
1416 wxDataViewModel
*model
= GetOwner()->GetModel();
1418 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1419 event
.SetEventObject( m_owner
);
1420 event
.SetItem( item
);
1421 event
.SetModel( model
);
1422 event
.SetDataFormat( format
);
1423 if (!m_owner
->HandleWindowEvent( event
))
1429 if (!event
.IsAllowed())
1436 if (m_dropHint
&& (row
!= m_dropHintLine
))
1437 RefreshRow( m_dropHintLine
);
1439 m_dropHintLine
= row
;
1445 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1451 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1452 unsigned int row
= GetLineAt( yy
);
1454 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1457 wxDataViewItem item
= GetItemByRow( row
);
1459 wxDataViewModel
*model
= GetOwner()->GetModel();
1461 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1462 event
.SetEventObject( m_owner
);
1463 event
.SetItem( item
);
1464 event
.SetModel( model
);
1465 event
.SetDataFormat( format
);
1466 if (!m_owner
->HandleWindowEvent( event
))
1469 if (!event
.IsAllowed())
1475 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1480 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1481 unsigned int row
= GetLineAt( yy
);
1483 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1486 wxDataViewItem item
= GetItemByRow( row
);
1488 wxDataViewModel
*model
= GetOwner()->GetModel();
1490 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1492 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1493 event
.SetEventObject( m_owner
);
1494 event
.SetItem( item
);
1495 event
.SetModel( model
);
1496 event
.SetDataFormat( format
);
1497 event
.SetDataSize( obj
->GetSize() );
1498 event
.SetDataBuffer( obj
->GetData() );
1499 if (!m_owner
->HandleWindowEvent( event
))
1502 if (!event
.IsAllowed())
1508 void wxDataViewMainWindow::OnLeave()
1513 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1515 int height
= GetLineHeight( row
);
1517 unsigned int cols
= GetOwner()->GetColumnCount();
1519 for (col
= 0; col
< cols
; col
++)
1521 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1522 if (column
->IsHidden())
1523 continue; // skip it!
1524 width
+= column
->GetWidth();
1530 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1531 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1532 indent
= indent
+ m_lineHeight
;
1533 // try to use the m_lineHeight as the expander space
1535 if(!node
->HasChildren())
1540 wxBitmap
bitmap( width
, height
);
1541 wxMemoryDC
dc( bitmap
);
1542 dc
.SetFont( GetFont() );
1543 dc
.SetPen( *wxBLACK_PEN
);
1544 dc
.SetBrush( *wxWHITE_BRUSH
);
1545 dc
.DrawRectangle( 0,0,width
,height
);
1547 wxDataViewModel
*model
= m_owner
->GetModel();
1549 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1552 // TODO-RTL: last column for RTL support
1553 expander
= GetOwner()->GetColumnAt( 0 );
1554 GetOwner()->SetExpanderColumn(expander
);
1559 for (col
= 0; col
< cols
; col
++)
1561 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1562 wxDataViewRenderer
*cell
= column
->GetRenderer();
1564 if (column
->IsHidden())
1565 continue; // skip it!
1567 width
= column
->GetWidth();
1569 if (column
== expander
)
1572 wxDataViewItem item
= GetItemByRow( row
);
1573 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1575 wxRect
item_rect(x
, 0, width
, height
);
1576 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1578 // dc.SetClippingRegion( item_rect );
1579 cell
->WXCallRender(item_rect
, &dc
, 0);
1580 // dc.DestroyClippingRegion();
1588 #endif // wxUSE_DRAG_AND_DROP
1591 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1593 wxDataViewModel
*model
= GetOwner()->GetModel();
1594 wxAutoBufferedPaintDC
dc( this );
1597 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1598 dc
.SetPen( *wxTRANSPARENT_PEN
);
1599 dc
.DrawRectangle(GetClientSize());
1604 // No items to draw.
1609 GetOwner()->PrepareDC( dc
);
1610 dc
.SetFont( GetFont() );
1612 wxRect update
= GetUpdateRegion().GetBox();
1613 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1615 // compute which items needs to be redrawn
1616 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1617 unsigned int item_count
=
1618 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1619 (int)(GetRowCount( ) - item_start
));
1620 unsigned int item_last
= item_start
+ item_count
;
1622 // Send the event to wxDataViewCtrl itself.
1623 wxWindow
* const parent
= GetParent();
1624 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1625 cache_event
.SetEventObject(parent
);
1626 cache_event
.SetCache(item_start
, item_last
- 1);
1627 parent
->ProcessWindowEvent(cache_event
);
1629 // compute which columns needs to be redrawn
1630 unsigned int cols
= GetOwner()->GetColumnCount();
1633 // we assume that we have at least one column below and painting an
1634 // empty control is unnecessary anyhow
1638 unsigned int col_start
= 0;
1639 unsigned int x_start
;
1640 for (x_start
= 0; col_start
< cols
; col_start
++)
1642 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1643 if (col
->IsHidden())
1644 continue; // skip it!
1646 unsigned int w
= col
->GetWidth();
1647 if (x_start
+w
>= (unsigned int)update
.x
)
1653 unsigned int col_last
= col_start
;
1654 unsigned int x_last
= x_start
;
1655 for (; col_last
< cols
; col_last
++)
1657 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1658 if (col
->IsHidden())
1659 continue; // skip it!
1661 if (x_last
> (unsigned int)update
.GetRight())
1664 x_last
+= col
->GetWidth();
1667 // Draw horizontal rules if required
1668 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1670 dc
.SetPen(m_penRule
);
1671 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1673 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1675 int y
= GetLineStart( i
);
1676 dc
.DrawLine(x_start
, y
, x_last
, y
);
1680 // Draw vertical rules if required
1681 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1683 dc
.SetPen(m_penRule
);
1684 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1686 // NB: Vertical rules are drawn in the last pixel of a column so that
1687 // they align perfectly with native MSW wxHeaderCtrl as well as for
1688 // consistency with MSW native list control. There's no vertical
1689 // rule at the most-left side of the control.
1691 int x
= x_start
- 1;
1692 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1694 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1695 if (col
->IsHidden())
1696 continue; // skip it
1698 x
+= col
->GetWidth();
1700 dc
.DrawLine(x
, GetLineStart( item_start
),
1701 x
, GetLineStart( item_last
) );
1705 // redraw the background for the items which are selected/current
1706 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1708 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1709 if (selected
|| item
== m_currentRow
)
1711 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1712 if (item
== m_currentRow
)
1713 flags
|= wxCONTROL_CURRENT
;
1715 flags
|= wxCONTROL_FOCUSED
;
1717 wxRect
rect( x_start
, GetLineStart( item
),
1718 x_last
- x_start
, GetLineHeight( item
) );
1719 wxRendererNative::Get().DrawItemSelectionRect
1729 #if wxUSE_DRAG_AND_DROP
1732 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1733 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1734 dc
.SetPen( *wxBLACK_PEN
);
1735 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1736 dc
.DrawRectangle( rect
);
1738 #endif // wxUSE_DRAG_AND_DROP
1740 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1743 // TODO-RTL: last column for RTL support
1744 expander
= GetOwner()->GetColumnAt( 0 );
1745 GetOwner()->SetExpanderColumn(expander
);
1748 // redraw all cells for all rows which must be repainted and all columns
1750 cell_rect
.x
= x_start
;
1751 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1753 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1754 wxDataViewRenderer
*cell
= col
->GetRenderer();
1755 cell_rect
.width
= col
->GetWidth();
1757 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1758 continue; // skip it!
1760 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1762 // get the cell value and set it into the renderer
1763 wxDataViewTreeNode
*node
= NULL
;
1764 wxDataViewItem dataitem
;
1766 if (!IsVirtualList())
1768 node
= GetTreeNodeByRow(item
);
1772 dataitem
= node
->GetItem();
1774 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1775 !model
->HasContainerColumns(dataitem
))
1780 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1783 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1786 cell_rect
.y
= GetLineStart( item
);
1787 cell_rect
.height
= GetLineHeight( item
);
1789 // deal with the expander
1791 if ((!IsList()) && (col
== expander
))
1793 // Calculate the indent first
1794 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1796 // we reserve m_lineHeight of horizontal space for the expander
1797 // but leave EXPANDER_MARGIN around the expander itself
1798 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1800 indent
+= m_lineHeight
;
1802 // draw expander if needed and visible
1803 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1805 dc
.SetPen( m_penExpander
);
1806 dc
.SetBrush( wxNullBrush
);
1808 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1809 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1810 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1812 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1815 if ( m_underMouse
== node
)
1816 flag
|= wxCONTROL_CURRENT
;
1817 if ( node
->IsOpen() )
1818 flag
|= wxCONTROL_EXPANDED
;
1820 // ensure that we don't overflow the cell (which might
1821 // happen if the column is very narrow)
1822 wxDCClipper
clip(dc
, cell_rect
);
1824 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1827 // force the expander column to left-center align
1828 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1830 if (node
&& !node
->HasChildren())
1832 // Yes, if the node does not have any child, it must be a leaf which
1833 // mean that it is a temporarily created by GetTreeNodeByRow
1837 wxRect item_rect
= cell_rect
;
1838 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1840 // account for the tree indent (harmless if we're not indented)
1841 item_rect
.x
+= indent
;
1842 item_rect
.width
-= indent
;
1844 if ( item_rect
.width
<= 0 )
1848 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1849 state
|= wxDATAVIEW_CELL_SELECTED
;
1851 // TODO: it would be much more efficient to create a clipping
1852 // region for the entire column being rendered (in the OnPaint
1853 // of wxDataViewMainWindow) instead of a single clip region for
1854 // each cell. However it would mean that each renderer should
1855 // respect the given wxRect's top & bottom coords, eventually
1856 // violating only the left & right coords - however the user can
1857 // make its own renderer and thus we cannot be sure of that.
1858 wxDCClipper
clip(dc
, item_rect
);
1860 cell
->WXCallRender(item_rect
, &dc
, state
);
1863 cell_rect
.x
+= cell_rect
.width
;
1867 void wxDataViewMainWindow::OnRenameTimer()
1869 // We have to call this here because changes may just have
1870 // been made and no screen update taken place.
1873 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1874 // (needs to be tested!)
1878 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1880 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1882 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1885 //-----------------------------------------------------------------------------
1886 // Helper class for do operation on the tree node
1887 //-----------------------------------------------------------------------------
1892 virtual ~DoJob() { }
1894 // The return value control how the tree-walker tranverse the tree
1895 // 0: Job done, stop tranverse and return
1896 // 1: Ignore the current node's subtree and continue
1897 // 2: Job not done, continue
1898 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1899 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1900 virtual int operator() ( void * n
) = 0;
1903 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1908 switch( func( node
) )
1919 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
1920 const wxDataViewTreeLeaves
& leaves
= node
->GetChildren();
1922 int len_nodes
= nodes
.GetCount();
1923 int len
= leaves
.GetCount();
1924 int i
= 0, nodes_i
= 0;
1926 for(; i
< len
; i
++ )
1928 void * n
= leaves
[i
];
1929 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1931 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1934 if( Walker( nd
, func
) )
1953 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1955 GetOwner()->InvalidateColBestWidths();
1957 if (IsVirtualList())
1959 wxDataViewVirtualListModel
*list_model
=
1960 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1961 m_count
= list_model
->GetCount();
1968 wxDataViewTreeNode
* node
;
1969 node
= FindNode(parent
);
1974 node
->SetHasChildren( true );
1976 if( g_model
->IsContainer( item
) )
1978 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1979 newnode
->SetItem(item
);
1980 newnode
->SetHasChildren( true );
1981 node
->AddNode( newnode
);
1984 node
->AddLeaf( item
.GetID() );
1986 node
->ChangeSubTreeCount(1);
1994 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1996 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1997 const wxDataViewItem
& item
)
1999 GetOwner()->InvalidateColBestWidths();
2001 if (IsVirtualList())
2003 wxDataViewVirtualListModel
*list_model
=
2004 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
2005 m_count
= list_model
->GetCount();
2007 if( m_currentRow
> GetRowCount() )
2008 m_currentRow
= m_count
- 1;
2010 // TODO: why empty the entire selection?
2011 m_selection
.Empty();
2018 wxDataViewTreeNode
* node
= FindNode(parent
);
2020 // Notice that it is possible that the item being deleted is not in the
2021 // tree at all, for example we could be deleting a never shown (because
2022 // collapsed) item in a tree model. So it's not an error if we don't know
2023 // about this item, just return without doing anything then.
2024 if ( !node
|| node
->GetChildren().Index(item
.GetID()) == wxNOT_FOUND
)
2028 node
->GetChildren().Remove( item
.GetID() );
2029 // Manipolate selection
2030 if( m_selection
.GetCount() > 1 )
2032 m_selection
.Empty();
2034 bool isContainer
= false;
2035 wxDataViewTreeNodes nds
= node
->GetNodes();
2036 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2038 if (nds
[i
]->GetItem() == item
)
2046 wxDataViewTreeNode
* n
= NULL
;
2047 wxDataViewTreeNodes nodes
= node
->GetNodes();
2048 int len
= nodes
.GetCount();
2049 for( int i
= 0; i
< len
; i
++)
2051 if( nodes
[i
]->GetItem() == item
)
2058 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2060 node
->GetNodes().Remove( n
);
2061 sub
-= n
->GetSubTreeCount();
2062 ::DestroyTreeHelper(n
);
2064 // Make the row number invalid and get a new valid one when user call GetRowCount
2066 node
->ChangeSubTreeCount(sub
);
2068 // Change the current row to the last row if the current exceed the max row number
2069 if( m_currentRow
> GetRowCount() )
2070 m_currentRow
= m_count
- 1;
2077 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2079 GetOwner()->InvalidateColBestWidths();
2085 wxWindow
*parent
= GetParent();
2086 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2087 le
.SetEventObject(parent
);
2088 le
.SetModel(GetOwner()->GetModel());
2090 parent
->GetEventHandler()->ProcessEvent(le
);
2095 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2097 int view_column
= -1;
2098 unsigned int n_col
= m_owner
->GetColumnCount();
2099 for (unsigned i
= 0; i
< n_col
; i
++)
2101 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2102 if (column
->GetModelColumn() == model_column
)
2104 view_column
= (int) i
;
2108 if (view_column
== -1)
2111 GetOwner()->InvalidateColBestWidth(view_column
);
2113 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2114 /*#define MAX_VIRTUAL_WIDTH 100000
2116 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2117 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2118 Refresh( true, &rect );
2126 wxWindow
*parent
= GetParent();
2127 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2128 le
.SetEventObject(parent
);
2129 le
.SetModel(GetOwner()->GetModel());
2131 le
.SetColumn(view_column
);
2132 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2133 parent
->GetEventHandler()->ProcessEvent(le
);
2138 bool wxDataViewMainWindow::Cleared()
2140 GetOwner()->InvalidateColBestWidths();
2143 m_selection
.Clear();
2146 BuildTree( GetOwner()->GetModel() );
2153 void wxDataViewMainWindow::UpdateDisplay()
2156 m_underMouse
= NULL
;
2159 void wxDataViewMainWindow::OnInternalIdle()
2161 wxWindow::OnInternalIdle();
2165 RecalculateDisplay();
2170 void wxDataViewMainWindow::RecalculateDisplay()
2172 wxDataViewModel
*model
= GetOwner()->GetModel();
2179 int width
= GetEndOfLastCol();
2180 int height
= GetLineStart( GetRowCount() );
2182 SetVirtualSize( width
, height
);
2183 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2188 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2190 m_underMouse
= NULL
;
2192 wxWindow::ScrollWindow( dx
, dy
, rect
);
2194 if (GetOwner()->m_headerArea
)
2195 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2198 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2200 m_underMouse
= NULL
;
2203 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2204 int sy
= GetLineStart( rows
)/y
;
2208 wxRect rect
= GetClientRect();
2212 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2213 for (x_start
= 0; colnum
< column
; colnum
++)
2215 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2216 if (col
->IsHidden())
2217 continue; // skip it!
2219 w
= col
->GetWidth();
2223 int x_end
= x_start
+ w
;
2224 xe
= xx
+ rect
.width
;
2227 sx
= ( xx
+ x_end
- xe
)/x
;
2234 m_owner
->Scroll( sx
, sy
);
2237 int wxDataViewMainWindow::GetCountPerPage() const
2239 wxSize size
= GetClientSize();
2240 return size
.y
/ m_lineHeight
;
2243 int wxDataViewMainWindow::GetEndOfLastCol() const
2247 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2249 const wxDataViewColumn
*c
=
2250 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2253 width
+= c
->GetWidth();
2258 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2262 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2264 return GetLineAt( y
);
2267 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2269 wxSize client_size
= GetClientSize();
2270 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2271 &client_size
.x
, &client_size
.y
);
2273 // we should deal with the pixel here
2274 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2276 return wxMin( GetRowCount()-1, row
);
2279 unsigned int wxDataViewMainWindow::GetRowCount()
2281 if ( m_count
== -1 )
2283 m_count
= RecalculateCount();
2289 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2296 void wxDataViewMainWindow::SelectAllRows( bool on
)
2303 m_selection
.Clear();
2304 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2305 m_selection
.Add( i
);
2310 unsigned int first_visible
= GetFirstVisibleRow();
2311 unsigned int last_visible
= GetLastVisibleRow();
2313 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2315 unsigned int row
= m_selection
[i
];
2316 if ((row
>= first_visible
) && (row
<= last_visible
))
2319 m_selection
.Clear();
2323 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2325 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2329 m_selection
.Add( row
);
2337 m_selection
.Remove( row
);
2343 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2347 unsigned int tmp
= from
;
2353 for (i
= from
; i
<= to
; i
++)
2355 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2358 m_selection
.Add( i
);
2363 m_selection
.Remove( i
);
2366 RefreshRows( from
, to
);
2369 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2371 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2373 int n
= aSelections
[i
];
2375 m_selection
.Add( n
);
2380 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2382 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2383 m_selection
.Add( row
);
2385 m_selection
.Remove( row
);
2389 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2391 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2394 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2396 wxWindow
*parent
= GetParent();
2397 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2399 le
.SetEventObject(parent
);
2400 le
.SetModel(GetOwner()->GetModel());
2403 parent
->GetEventHandler()->ProcessEvent(le
);
2406 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2408 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2409 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2411 wxSize client_size
= GetClientSize();
2412 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2413 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2414 if (intersect_rect
.width
> 0)
2415 Refresh( true, &intersect_rect
);
2418 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2422 unsigned int tmp
= to
;
2427 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2428 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2430 wxSize client_size
= GetClientSize();
2431 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2432 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2433 if (intersect_rect
.width
> 0)
2434 Refresh( true, &intersect_rect
);
2437 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2439 wxSize client_size
= GetClientSize();
2440 int start
= GetLineStart( firstRow
);
2441 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2442 if (start
> client_size
.y
) return;
2444 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2446 Refresh( true, &rect
);
2449 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2451 wxCHECK_RET( newCurrent
< GetRowCount(),
2452 wxT("invalid item index in OnArrowChar()") );
2454 // if there is no selection, we cannot move it anywhere
2455 if (!HasCurrentRow())
2458 unsigned int oldCurrent
= m_currentRow
;
2460 // in single selection we just ignore Shift as we can't select several
2462 if ( event
.ShiftDown() && !IsSingleSel() )
2464 RefreshRow( oldCurrent
);
2466 ChangeCurrentRow( newCurrent
);
2468 // select all the items between the old and the new one
2469 if ( oldCurrent
> newCurrent
)
2471 newCurrent
= oldCurrent
;
2472 oldCurrent
= m_currentRow
;
2475 SelectRows( oldCurrent
, newCurrent
, true );
2476 if (oldCurrent
!=newCurrent
)
2477 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2481 RefreshRow( oldCurrent
);
2483 // all previously selected items are unselected unless ctrl is held
2484 if ( !event
.ControlDown() )
2485 SelectAllRows(false);
2487 ChangeCurrentRow( newCurrent
);
2489 if ( !event
.ControlDown() )
2491 SelectRow( m_currentRow
, true );
2492 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2495 RefreshRow( m_currentRow
);
2498 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2501 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2505 rect
.y
= GetLineStart( row
);
2506 rect
.width
= GetEndOfLastCol();
2507 rect
.height
= GetLineHeight( row
);
2512 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2514 const wxDataViewModel
*model
= GetOwner()->GetModel();
2516 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2518 // TODO make more efficient
2523 for (r
= 0; r
< row
; r
++)
2525 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2526 if (!node
) return start
;
2528 wxDataViewItem item
= node
->GetItem();
2530 if (node
&& !node
->HasChildren())
2532 // Yes, if the node does not have any child, it must be a leaf which
2533 // mean that it is a temporarily created by GetTreeNodeByRow
2537 unsigned int cols
= GetOwner()->GetColumnCount();
2539 int height
= m_lineHeight
;
2540 for (col
= 0; col
< cols
; col
++)
2542 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2543 if (column
->IsHidden())
2544 continue; // skip it!
2547 model
->IsContainer(item
) &&
2548 !model
->HasContainerColumns(item
))
2549 continue; // skip it!
2551 wxDataViewRenderer
*renderer
=
2552 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2553 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2555 height
= wxMax( height
, renderer
->GetSize().y
);
2565 return row
* m_lineHeight
;
2569 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2571 const wxDataViewModel
*model
= GetOwner()->GetModel();
2573 // check for the easy case first
2574 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2575 return y
/ m_lineHeight
;
2577 // TODO make more efficient
2578 unsigned int row
= 0;
2579 unsigned int yy
= 0;
2582 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2585 // not really correct...
2586 return row
+ ((y
-yy
) / m_lineHeight
);
2589 wxDataViewItem item
= node
->GetItem();
2591 if (node
&& !node
->HasChildren())
2593 // Yes, if the node does not have any child, it must be a leaf which
2594 // mean that it is a temporarily created by GetTreeNodeByRow
2598 unsigned int cols
= GetOwner()->GetColumnCount();
2600 int height
= m_lineHeight
;
2601 for (col
= 0; col
< cols
; col
++)
2603 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2604 if (column
->IsHidden())
2605 continue; // skip it!
2608 model
->IsContainer(item
) &&
2609 !model
->HasContainerColumns(item
))
2610 continue; // skip it!
2612 wxDataViewRenderer
*renderer
=
2613 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2614 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2616 height
= wxMax( height
, renderer
->GetSize().y
);
2627 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2629 const wxDataViewModel
*model
= GetOwner()->GetModel();
2631 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2633 wxASSERT( !IsVirtualList() );
2635 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2636 // wxASSERT( node );
2637 if (!node
) return m_lineHeight
;
2639 wxDataViewItem item
= node
->GetItem();
2641 if (node
&& !node
->HasChildren())
2643 // Yes, if the node does not have any child, it must be a leaf which
2644 // mean that it is a temporarily created by GetTreeNodeByRow
2648 int height
= m_lineHeight
;
2650 unsigned int cols
= GetOwner()->GetColumnCount();
2652 for (col
= 0; col
< cols
; col
++)
2654 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2655 if (column
->IsHidden())
2656 continue; // skip it!
2659 model
->IsContainer(item
) &&
2660 !model
->HasContainerColumns(item
))
2661 continue; // skip it!
2663 wxDataViewRenderer
*renderer
=
2664 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2665 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2667 height
= wxMax( height
, renderer
->GetSize().y
);
2674 return m_lineHeight
;
2678 class RowToItemJob
: public DoJob
2681 RowToItemJob( unsigned int row
, int current
)
2682 { this->row
= row
; this->current
= current
; }
2683 virtual ~RowToItemJob() {}
2685 virtual int operator() ( wxDataViewTreeNode
* node
)
2688 if( current
== static_cast<int>(row
))
2690 ret
= node
->GetItem();
2694 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2696 current
+= node
->GetSubTreeCount();
2701 // If the current has no child node, we can find the desired item of the row
2703 // This if can speed up finding in some case, and will has a very good effect
2704 // when it comes to list view
2705 if( node
->GetNodes().GetCount() == 0)
2707 int index
= static_cast<int>(row
) - current
- 1;
2708 ret
= node
->GetChildren().Item( index
);
2715 virtual int operator() ( void * n
)
2718 if( current
== static_cast<int>(row
))
2720 ret
= wxDataViewItem( n
);
2726 wxDataViewItem
GetResult() const
2735 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2737 if (IsVirtualList())
2739 return wxDataViewItem( wxUIntToPtr(row
+1) );
2743 RowToItemJob
job( row
, -2 );
2744 Walker( m_root
, job
);
2745 return job
.GetResult();
2749 class RowToTreeNodeJob
: public DoJob
2752 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2755 this->current
= current
;
2759 virtual ~RowToTreeNodeJob(){ }
2761 virtual int operator() ( wxDataViewTreeNode
* node
)
2764 if( current
== static_cast<int>(row
))
2770 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2772 current
+= node
->GetSubTreeCount();
2779 // If the current node has no children, we can find the desired item of the
2780 // row number directly.
2781 // This if can speed up finding in some case, and will have a very good
2782 // effect for list views.
2783 if( node
->GetNodes().GetCount() == 0)
2785 int index
= static_cast<int>(row
) - current
- 1;
2786 void * n
= node
->GetChildren().Item( index
);
2787 ret
= new wxDataViewTreeNode( parent
);
2788 ret
->SetItem( wxDataViewItem( n
));
2789 ret
->SetHasChildren(false);
2796 virtual int operator() ( void * n
)
2799 if( current
== static_cast<int>(row
))
2801 ret
= new wxDataViewTreeNode( parent
);
2802 ret
->SetItem( wxDataViewItem( n
));
2803 ret
->SetHasChildren(false);
2810 wxDataViewTreeNode
* GetResult() const
2816 wxDataViewTreeNode
* ret
;
2817 wxDataViewTreeNode
* parent
;
2820 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2822 wxASSERT( !IsVirtualList() );
2824 RowToTreeNodeJob
job( row
, -2, m_root
);
2825 Walker( m_root
, job
);
2826 return job
.GetResult();
2829 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2830 const wxDataViewItem
& item
)
2832 wxWindow
*parent
= GetParent();
2833 wxDataViewEvent
le(type
, parent
->GetId());
2835 le
.SetEventObject(parent
);
2836 le
.SetModel(GetOwner()->GetModel());
2839 parent
->GetEventHandler()->ProcessEvent(le
);
2843 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2848 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2852 if (!node
->HasChildren())
2858 return node
->IsOpen();
2861 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2866 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2870 if (!node
->HasChildren())
2879 void wxDataViewMainWindow::Expand( unsigned int row
)
2884 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2888 if (!node
->HasChildren())
2894 if (!node
->IsOpen())
2897 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2899 // Check if the user prevent expanding
2900 if( e
.GetSkipped() )
2905 // build the children of current node
2906 if( node
->GetChildrenNumber() == 0 )
2909 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2912 // By expanding the node all row indices that are currently in the selection list
2913 // and are greater than our node have become invalid. So we have to correct that now.
2914 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2915 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2917 const unsigned testRow
= m_selection
[i
];
2918 // all rows above us are not affected, so skip them
2922 m_selection
[i
] += rowAdjustment
;
2925 if(m_currentRow
> row
)
2926 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2930 // Send the expanded event
2931 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2935 void wxDataViewMainWindow::Collapse(unsigned int row
)
2940 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2944 if (!node
->HasChildren())
2953 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2954 if( e
.GetSkipped() )
2957 // Find out if there are selected items below the current node.
2958 bool selectCollapsingRow
= false;
2959 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2960 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2961 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2963 const unsigned testRow
= m_selection
[i
];
2964 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2966 selectCollapsingRow
= true;
2967 // get out as soon as we have found a node that is selected
2974 // If the node to be closed has selected items the user won't see those any longer.
2975 // We select the collapsing node in this case.
2976 if(selectCollapsingRow
)
2978 SelectAllRows(false);
2979 ChangeCurrentRow(row
);
2980 SelectRow(row
, true);
2981 SendSelectionChangedEvent(GetItemByRow(row
));
2985 // if there were no selected items below our node we still need to "fix" the
2986 // selection list to adjust for the changing of the row indices.
2987 // We actually do the opposite of what we are doing in Expand().
2988 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2990 const unsigned testRow
= m_selection
[i
];
2991 // all rows above us are not affected, so skip them
2995 m_selection
[i
] -= rowAdjustment
;
2998 // if the "current row" is being collapsed away we change it to the current row ;-)
2999 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3000 ChangeCurrentRow(row
);
3001 else if(m_currentRow
> row
)
3002 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3007 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3011 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3013 const wxDataViewModel
* model
= GetOwner()->GetModel();
3020 // Compose the parent-chain for the item we are looking for
3021 wxVector
<wxDataViewItem
> parentChain
;
3022 wxDataViewItem
it( item
);
3025 parentChain
.push_back(it
);
3026 it
= model
->GetParent(it
);
3029 // Find the item along the parent-chain.
3030 // This algorithm is designed to speed up the node-finding method
3031 wxDataViewTreeNode
* node
= m_root
;
3032 for( unsigned iter
= parentChain
.size()-1; iter
>=0; --iter
)
3034 if( node
->HasChildren() )
3036 if( node
->GetChildrenNumber() == 0 )
3039 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3042 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3045 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3047 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3048 if (currentNode
->GetItem() == parentChain
[iter
])
3050 if (currentNode
->GetItem() == item
)
3067 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3068 wxDataViewColumn
* &column
)
3070 wxDataViewColumn
*col
= NULL
;
3071 unsigned int cols
= GetOwner()->GetColumnCount();
3072 unsigned int colnum
= 0;
3074 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3075 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3077 col
= GetOwner()->GetColumnAt(colnum
);
3078 if (col
->IsHidden())
3079 continue; // skip it!
3081 unsigned int w
= col
->GetWidth();
3082 if (x_start
+w
>= (unsigned int)x
)
3089 item
= GetItemByRow( GetLineAt( y
) );
3092 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3093 const wxDataViewColumn
* column
)
3098 unsigned int cols
= GetOwner()->GetColumnCount();
3099 // If column is null the loop will compute the combined width of all columns.
3100 // Otherwise, it will compute the x position of the column we are looking for.
3101 for (unsigned int i
= 0; i
< cols
; i
++)
3103 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3108 if (col
->IsHidden())
3109 continue; // skip it!
3111 xpos
+= col
->GetWidth();
3112 width
+= col
->GetWidth();
3117 // If we have a column, we need can get its width directly.
3118 if(column
->IsHidden())
3121 width
= column
->GetWidth();
3126 // If we have no column, we reset the x position back to zero.
3130 // we have to take an expander column into account and compute its indentation
3131 // to get the correct x position where the actual text is
3133 int row
= GetRowByItem(item
);
3134 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3136 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3137 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3138 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3140 if(!node
->HasChildren())
3144 wxRect
itemRect( xpos
+ indent
,
3145 GetLineStart( row
),
3147 GetLineHeight( row
) );
3149 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3150 &itemRect
.x
, &itemRect
.y
);
3155 int wxDataViewMainWindow::RecalculateCount()
3157 if (IsVirtualList())
3159 wxDataViewVirtualListModel
*list_model
=
3160 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3162 return list_model
->GetCount();
3166 return m_root
->GetSubTreeCount();
3170 class ItemToRowJob
: public DoJob
3173 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3180 // Maybe binary search will help to speed up this process
3181 virtual int operator() ( wxDataViewTreeNode
* node
)
3184 if( node
->GetItem() == item
)
3189 if( node
->GetItem() == *m_iter
)
3196 ret
+= node
->GetSubTreeCount();
3202 virtual int operator() ( void * n
)
3205 if( n
== item
.GetID() )
3210 // the row number is begin from zero
3211 int GetResult() const
3215 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3216 wxDataViewItem item
;
3221 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3223 const wxDataViewModel
* model
= GetOwner()->GetModel();
3227 if (IsVirtualList())
3229 return wxPtrToUInt( item
.GetID() ) -1;
3236 // Compose the parent-chain of the item we are looking for
3237 wxVector
<wxDataViewItem
> parentChain
;
3238 wxDataViewItem
it( item
);
3241 parentChain
.push_back(it
);
3242 it
= model
->GetParent(it
);
3245 // add an 'invalid' item to represent our 'invisible' root node
3246 parentChain
.push_back(wxDataViewItem());
3248 // the parent chain was created by adding the deepest parent first.
3249 // so if we want to start at the root node, we have to iterate backwards through the vector
3250 ItemToRowJob
job( item
, parentChain
.rbegin() );
3251 Walker( m_root
, job
);
3252 return job
.GetResult();
3256 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3257 wxDataViewTreeNode
* node
)
3259 if( !model
->IsContainer( item
) )
3262 wxDataViewItemArray children
;
3263 unsigned int num
= model
->GetChildren( item
, children
);
3265 unsigned int index
= 0;
3266 while( index
< num
)
3268 if( model
->IsContainer( children
[index
] ) )
3270 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3271 n
->SetItem(children
[index
]);
3272 n
->SetHasChildren( true );
3277 node
->AddLeaf( children
[index
].GetID() );
3281 node
->SetSubTreeCount( num
);
3282 wxDataViewTreeNode
* n
= node
->GetParent();
3284 n
->ChangeSubTreeCount(num
);
3288 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3292 if (GetOwner()->GetModel()->IsVirtualListModel())
3298 m_root
= new wxDataViewTreeNode( NULL
);
3299 m_root
->SetHasChildren(true);
3301 // First we define a invalid item to fetch the top-level elements
3302 wxDataViewItem item
;
3304 BuildTreeHelper( model
, item
, m_root
);
3308 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3310 if( node
->GetNodeNumber() != 0 )
3312 int len
= node
->GetNodeNumber();
3313 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3314 for (int i
= 0; i
< len
; i
++)
3315 DestroyTreeHelper(nodes
[i
]);
3320 void wxDataViewMainWindow::DestroyTree()
3322 if (!IsVirtualList())
3324 ::DestroyTreeHelper(m_root
);
3330 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3332 wxWindow
* const parent
= GetParent();
3334 // propagate the char event upwards
3335 wxKeyEvent
eventForParent(event
);
3336 eventForParent
.SetEventObject(parent
);
3337 if ( parent
->ProcessWindowEvent(eventForParent
) )
3340 if ( parent
->HandleAsNavigationKey(event
) )
3343 // no item -> nothing to do
3344 if (!HasCurrentRow())
3350 // don't use m_linesPerPage directly as it might not be computed yet
3351 const int pageSize
= GetCountPerPage();
3352 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3354 switch ( event
.GetKeyCode() )
3358 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3360 le
.SetItem( GetItemByRow(m_currentRow
) );
3361 le
.SetEventObject(parent
);
3362 le
.SetModel(GetOwner()->GetModel());
3364 parent
->GetEventHandler()->ProcessEvent(le
);
3369 if ( m_currentRow
> 0 )
3370 OnArrowChar( m_currentRow
- 1, event
);
3374 if ( m_currentRow
< GetRowCount() - 1 )
3375 OnArrowChar( m_currentRow
+ 1, event
);
3377 // Add the process for tree expanding/collapsing
3383 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3387 if (node
->HasChildren() && node
->IsOpen())
3389 Collapse(m_currentRow
);
3391 else // if the node is already closed we move the selection to its parent
3393 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3395 if(!node
->HasChildren())
3400 int parent
= GetRowByItem( parent_node
->GetItem() );
3403 unsigned int row
= m_currentRow
;
3404 SelectRow( row
, false);
3405 SelectRow( parent
, true );
3406 ChangeCurrentRow( parent
);
3407 GetOwner()->EnsureVisible( parent
, -1 );
3408 SendSelectionChangedEvent( parent_node
->GetItem() );
3416 if (!IsExpanded( m_currentRow
))
3417 Expand( m_currentRow
);
3420 unsigned int row
= m_currentRow
;
3421 SelectRow( row
, false );
3422 SelectRow( row
+ 1, true );
3423 ChangeCurrentRow( row
+ 1 );
3424 GetOwner()->EnsureVisible( row
+ 1, -1 );
3425 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3432 OnArrowChar( GetRowCount() - 1, event
);
3437 OnArrowChar( 0, event
);
3442 int steps
= pageSize
- 1;
3443 int index
= m_currentRow
- steps
;
3447 OnArrowChar( index
, event
);
3453 int steps
= pageSize
- 1;
3454 unsigned int index
= m_currentRow
+ steps
;
3455 unsigned int count
= GetRowCount();
3456 if ( index
>= count
)
3459 OnArrowChar( index
, event
);
3465 if(m_selection
.size() == 1)
3467 // TODO: we need to revise that when we have a concept for a 'current column'
3468 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3478 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3480 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3482 // let the base handle mouse wheel events.
3487 // set the focus to ourself if any of the mouse buttons are pressed
3488 if(event
.ButtonDown() && !HasFocus())
3491 int x
= event
.GetX();
3492 int y
= event
.GetY();
3493 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3494 wxDataViewColumn
*col
= NULL
;
3497 unsigned int cols
= GetOwner()->GetColumnCount();
3499 for (i
= 0; i
< cols
; i
++)
3501 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3503 continue; // skip it!
3505 if (x
< xpos
+ c
->GetWidth())
3510 xpos
+= c
->GetWidth();
3518 wxDataViewRenderer
*cell
= col
->GetRenderer();
3519 unsigned int current
= GetLineAt( y
);
3520 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3522 // Unselect all if below the last row ?
3527 // Test whether the mouse is hovered on the tree item button
3528 bool hoverOverExpander
= false;
3529 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3531 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3532 if( node
!=NULL
&& node
->HasChildren() )
3534 int indent
= node
->GetIndentLevel();
3535 indent
= GetOwner()->GetIndent()*indent
;
3537 // we make the rectangle we are looking in a bit bigger than the actual
3538 // visual expander so the user can hit that little thing reliably
3539 wxRect
rect( xpos
+ indent
,
3540 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3541 m_lineHeight
, m_lineHeight
);
3542 if( rect
.Contains(x
, y
) )
3544 // So the mouse is over the expander
3545 hoverOverExpander
= true;
3546 if (m_underMouse
&& m_underMouse
!= node
)
3548 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3549 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3551 if (m_underMouse
!= node
)
3553 // wxLogMessage("Do the row: %d", current);
3554 RefreshRow(current
);
3556 m_underMouse
= node
;
3559 if (node
!=NULL
&& !node
->HasChildren())
3562 if (!hoverOverExpander
)
3564 if (m_underMouse
!= NULL
)
3566 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3567 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3568 m_underMouse
= NULL
;
3572 wxDataViewModel
*model
= GetOwner()->GetModel();
3574 #if wxUSE_DRAG_AND_DROP
3575 if (event
.Dragging())
3577 if (m_dragCount
== 0)
3579 // we have to report the raw, physical coords as we want to be
3580 // able to call HitTest(event.m_pointDrag) from the user code to
3581 // get the item being dragged
3582 m_dragStart
= event
.GetPosition();
3587 if (m_dragCount
!= 3)
3590 if (event
.LeftIsDown())
3592 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3593 &m_dragStart
.x
, &m_dragStart
.y
);
3594 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3595 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3597 // Notify cell about drag
3598 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3599 event
.SetEventObject( m_owner
);
3600 event
.SetItem( item
);
3601 event
.SetModel( model
);
3602 if (!m_owner
->HandleWindowEvent( event
))
3605 if (!event
.IsAllowed())
3608 wxDataObject
*obj
= event
.GetDataObject();
3612 wxDataViewDropSource
drag( this, drag_item_row
);
3613 drag
.SetData( *obj
);
3614 /* wxDragResult res = */ drag
.DoDragDrop();
3623 #endif // wxUSE_DRAG_AND_DROP
3625 bool simulateClick
= false;
3627 if (event
.ButtonDClick())
3629 m_renameTimer
->Stop();
3630 m_lastOnSame
= false;
3633 wxDataViewItem item
= GetItemByRow(current
);
3634 bool ignore_other_columns
=
3635 ((GetOwner()->GetExpanderColumn() != col
) &&
3636 (model
->IsContainer(item
)) &&
3637 (!model
->HasContainerColumns(item
)));
3639 if (event
.LeftDClick())
3641 if(hoverOverExpander
)
3643 // a double click on the expander will be converted into a "simulated" normal click
3644 simulateClick
= true;
3646 else if ( current
== m_lineLastClicked
)
3648 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3650 const unsigned colIdx
= col
->GetModelColumn();
3652 cell
->PrepareForItem(model
, item
, colIdx
);
3654 wxRect
cell_rect( xpos
, GetLineStart( current
),
3655 col
->GetWidth(), GetLineHeight( current
) );
3656 cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3660 wxWindow
*parent
= GetParent();
3661 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3663 le
.SetColumn( col
->GetModelColumn() );
3664 le
.SetDataViewColumn( col
);
3665 le
.SetEventObject(parent
);
3666 le
.SetModel(GetOwner()->GetModel());
3668 parent
->GetEventHandler()->ProcessEvent(le
);
3674 // The first click was on another item, so don't interpret this as
3675 // a double click, but as a simple click instead
3676 simulateClick
= true;
3680 if (event
.LeftUp() && !hoverOverExpander
)
3682 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3684 // select single line
3685 SelectAllRows( false );
3686 SelectRow( m_lineSelectSingleOnUp
, true );
3687 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3690 // If the user click the expander, we do not do editing even if the column
3691 // with expander are editable
3692 if (m_lastOnSame
&& !ignore_other_columns
)
3694 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3695 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3697 m_renameTimer
->Start( 100, true );
3701 m_lastOnSame
= false;
3702 m_lineSelectSingleOnUp
= (unsigned int)-1;
3704 else if(!event
.LeftUp())
3706 // This is necessary, because after a DnD operation in
3707 // from and to ourself, the up event is swallowed by the
3708 // DnD code. So on next non-up event (which means here and
3709 // now) m_lineSelectSingleOnUp should be reset.
3710 m_lineSelectSingleOnUp
= (unsigned int)-1;
3713 if (event
.RightDown())
3715 m_lineBeforeLastClicked
= m_lineLastClicked
;
3716 m_lineLastClicked
= current
;
3718 // If the item is already selected, do not update the selection.
3719 // Multi-selections should not be cleared if a selected item is clicked.
3720 if (!IsRowSelected(current
))
3722 SelectAllRows(false);
3723 ChangeCurrentRow(current
);
3724 SelectRow(m_currentRow
,true);
3725 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3728 else if (event
.RightUp())
3731 model
->GetValue( value
, item
, col
->GetModelColumn() );
3732 wxWindow
*parent
= GetParent();
3733 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3735 le
.SetColumn( col
->GetModelColumn() );
3736 le
.SetDataViewColumn( col
);
3737 le
.SetEventObject(parent
);
3738 le
.SetModel(GetOwner()->GetModel());
3740 parent
->GetEventHandler()->ProcessEvent(le
);
3742 else if (event
.MiddleDown())
3746 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3748 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3750 // hoverOverExpander being true tells us that our node must be
3751 // valid and have children.
3752 // So we don't need any extra checks.
3753 if( node
->IsOpen() )
3758 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3760 m_lineBeforeLastClicked
= m_lineLastClicked
;
3761 m_lineLastClicked
= current
;
3763 unsigned int oldCurrentRow
= m_currentRow
;
3764 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3766 bool cmdModifierDown
= event
.CmdDown();
3767 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3769 if ( IsSingleSel() || !IsRowSelected(current
) )
3771 SelectAllRows( false );
3772 ChangeCurrentRow(current
);
3773 SelectRow(m_currentRow
,true);
3774 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3776 else // multi sel & current is highlighted & no mod keys
3778 m_lineSelectSingleOnUp
= current
;
3779 ChangeCurrentRow(current
); // change focus
3782 else // multi sel & either ctrl or shift is down
3784 if (cmdModifierDown
)
3786 ChangeCurrentRow(current
);
3787 ReverseRowSelection(m_currentRow
);
3788 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3790 else if (event
.ShiftDown())
3792 ChangeCurrentRow(current
);
3794 unsigned int lineFrom
= oldCurrentRow
,
3797 if ( lineTo
< lineFrom
)
3800 lineFrom
= m_currentRow
;
3803 SelectRows(lineFrom
, lineTo
, true);
3804 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3806 else // !ctrl, !shift
3808 // test in the enclosing if should make it impossible
3809 wxFAIL_MSG( wxT("how did we get here?") );
3813 if (m_currentRow
!= oldCurrentRow
)
3814 RefreshRow( oldCurrentRow
);
3816 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3818 // Update selection here...
3821 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3822 (current
== oldCurrentRow
)) && oldWasSelected
;
3824 // Call LeftClick after everything else as under GTK+
3825 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3827 // notify cell about click
3828 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3830 wxRect
cell_rect( xpos
, GetLineStart( current
),
3831 col
->GetWidth(), GetLineHeight( current
) );
3833 // Report position relative to the cell's custom area, i.e.
3834 // no the entire space as given by the control but the one
3835 // used by the renderer after calculation of alignment etc.
3837 // adjust the rectangle ourselves to account for the alignment
3838 wxRect rectItem
= cell_rect
;
3839 const int align
= cell
->GetAlignment();
3840 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3842 const wxSize size
= cell
->GetSize();
3844 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3846 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3847 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3848 else if ( align
& wxALIGN_RIGHT
)
3849 rectItem
.x
+= cell_rect
.width
- size
.x
;
3850 // else: wxALIGN_LEFT is the default
3853 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3855 if ( align
& wxALIGN_CENTER_VERTICAL
)
3856 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3857 else if ( align
& wxALIGN_BOTTOM
)
3858 rectItem
.y
+= cell_rect
.height
- size
.y
;
3859 // else: wxALIGN_TOP is the default
3863 wxPoint
pos( event
.GetPosition() );
3864 pos
.x
-= rectItem
.x
;
3865 pos
.y
-= rectItem
.y
;
3867 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3869 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3870 model
, item
, col
->GetModelColumn());
3875 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3879 if (HasCurrentRow())
3885 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3889 if (HasCurrentRow())
3895 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3897 if( m_selection
.GetCount() != 1 )
3898 return wxDataViewItem();
3900 return GetItemByRow( m_selection
.Item(0));
3903 //-----------------------------------------------------------------------------
3905 //-----------------------------------------------------------------------------
3907 WX_DEFINE_LIST(wxDataViewColumnList
)
3909 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3910 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3911 EVT_SIZE(wxDataViewCtrl::OnSize
)
3914 wxDataViewCtrl::~wxDataViewCtrl()
3917 GetModel()->RemoveNotifier( m_notifier
);
3920 m_colsBestWidths
.clear();
3923 void wxDataViewCtrl::Init()
3925 m_cols
.DeleteContents(true);
3928 // No sorting column at start
3929 m_sortingColumnIdx
= wxNOT_FOUND
;
3931 m_headerArea
= NULL
;
3934 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3939 const wxValidator
& validator
,
3940 const wxString
& name
)
3942 // if ( (style & wxBORDER_MASK) == 0)
3943 // style |= wxBORDER_SUNKEN;
3947 if (!wxControl::Create( parent
, id
, pos
, size
,
3948 style
| wxScrolledWindowStyle
, validator
, name
))
3951 SetInitialSize(size
);
3954 MacSetClipChildren( true );
3957 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3959 // We use the cursor keys for moving the selection, not scrolling, so call
3960 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3961 // keyboard events forwarded to us from wxListMainWindow.
3962 DisableKeyboardScrolling();
3964 if (HasFlag(wxDV_NO_HEADER
))
3965 m_headerArea
= NULL
;
3967 m_headerArea
= new wxDataViewHeaderWindow(this);
3969 SetTargetWindow( m_clientArea
);
3971 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3973 sizer
->Add( m_headerArea
, 0, wxGROW
);
3974 sizer
->Add( m_clientArea
, 1, wxGROW
);
3980 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3982 return wxBORDER_THEME
;
3986 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3990 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3993 // we need to process arrows ourselves for scrolling
3994 if ( nMsg
== WM_GETDLGCODE
)
3996 rc
|= DLGC_WANTARROWS
;
4004 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4006 wxSize newsize
= size
;
4007 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4008 newsize
.y
-= m_headerArea
->GetSize().y
;
4013 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4015 // We need to override OnSize so that our scrolled
4016 // window a) does call Layout() to use sizers for
4017 // positioning the controls but b) does not query
4018 // the sizer for their size and use that for setting
4019 // the scrollable area as set that ourselves by
4020 // calling SetScrollbar() further down.
4027 void wxDataViewCtrl::SetFocus()
4030 m_clientArea
->SetFocus();
4033 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4035 if (!wxDataViewCtrlBase::AssociateModel( model
))
4038 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4040 model
->AddNotifier( m_notifier
);
4042 m_clientArea
->DestroyTree();
4044 m_clientArea
->BuildTree(model
);
4046 m_clientArea
->UpdateDisplay();
4051 #if wxUSE_DRAG_AND_DROP
4053 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4055 return m_clientArea
->EnableDragSource( format
);
4058 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4060 return m_clientArea
->EnableDropTarget( format
);
4063 #endif // wxUSE_DRAG_AND_DROP
4065 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4067 if (!wxDataViewCtrlBase::AppendColumn(col
))
4070 m_cols
.Append( col
);
4071 m_colsBestWidths
.push_back(0);
4072 OnColumnsCountChanged();
4076 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4078 if (!wxDataViewCtrlBase::PrependColumn(col
))
4081 m_cols
.Insert( col
);
4082 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4083 OnColumnsCountChanged();
4087 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4089 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4092 m_cols
.Insert( pos
, col
);
4093 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4094 OnColumnsCountChanged();
4098 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4101 m_headerArea
->UpdateColumn(idx
);
4103 m_clientArea
->UpdateDisplay();
4106 void wxDataViewCtrl::OnColumnsCountChanged()
4109 m_headerArea
->SetColumnCount(GetColumnCount());
4111 m_clientArea
->UpdateDisplay();
4114 void wxDataViewCtrl::DoSetExpanderColumn()
4116 m_clientArea
->UpdateDisplay();
4119 void wxDataViewCtrl::DoSetIndent()
4121 m_clientArea
->UpdateDisplay();
4124 unsigned int wxDataViewCtrl::GetColumnCount() const
4126 return m_cols
.GetCount();
4129 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4131 if ( !m_clientArea
)
4134 m_clientArea
->SetRowHeight(lineHeight
);
4139 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4144 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4146 // columns can't be reordered if there is no header window which allows
4148 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4151 return GetColumn(idx
);
4154 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4156 const unsigned count
= m_cols
.size();
4157 for ( unsigned n
= 0; n
< count
; n
++ )
4159 if ( m_cols
[n
] == column
)
4166 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4168 if ( m_colsBestWidths
[idx
] != 0 )
4169 return m_colsBestWidths
[idx
];
4171 const int count
= m_clientArea
->GetRowCount();
4172 wxDataViewColumn
*column
= GetColumn(idx
);
4173 wxDataViewRenderer
*renderer
=
4174 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4176 class MaxWidthCalculator
4179 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4180 wxDataViewRenderer
*renderer
,
4181 const wxDataViewModel
*model
,
4184 m_clientArea(clientArea
),
4185 m_renderer(renderer
),
4191 void UpdateWithWidth(int width
)
4193 m_width
= wxMax(m_width
, width
);
4196 void UpdateWithRow(int row
)
4198 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4199 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4200 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4203 int GetMaxWidth() const { return m_width
; }
4207 wxDataViewMainWindow
*m_clientArea
;
4208 wxDataViewRenderer
*m_renderer
;
4209 const wxDataViewModel
*m_model
;
4213 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4214 GetModel(), column
->GetModelColumn());
4218 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4219 // Labels on native MSW header are indented on both sides
4221 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4222 calculator
.UpdateWithWidth(header_width
);
4225 // The code below deserves some explanation. For very large controls, we
4226 // simply can't afford to calculate sizes for all items, it takes too
4227 // long. So the best we can do is to check the first and the last N/2
4228 // items in the control for some sufficiently large N and calculate best
4229 // sizes from that. That can result in the calculated best width being too
4230 // small for some outliers, but it's better to get slightly imperfect
4231 // result than to wait several seconds after every update. To avoid highly
4232 // visible miscalculations, we also include all currently visible items
4233 // no matter what. Finally, the value of N is determined dynamically by
4234 // measuring how much time we spent on the determining item widths so far.
4237 int top_part_end
= count
;
4238 static const long CALC_TIMEOUT
= 20/*ms*/;
4239 // don't call wxStopWatch::Time() too often
4240 static const unsigned CALC_CHECK_FREQ
= 100;
4243 // use some hard-coded limit, that's the best we can do without timer
4244 int top_part_end
= wxMin(500, count
);
4245 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4249 for ( row
= 0; row
< top_part_end
; row
++ )
4252 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4253 timer
.Time() > CALC_TIMEOUT
)
4255 #endif // wxUSE_STOPWATCH
4256 calculator
.UpdateWithRow(row
);
4259 // row is the first unmeasured item now; that's our value of N/2
4265 // add bottom N/2 items now:
4266 const int bottom_part_start
= wxMax(row
, count
- row
);
4267 for ( row
= bottom_part_start
; row
< count
; row
++ )
4269 calculator
.UpdateWithRow(row
);
4272 // finally, include currently visible items in the calculation:
4273 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4274 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4275 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4277 first_visible
= wxMax(first_visible
, top_part_end
);
4278 last_visible
= wxMin(bottom_part_start
, last_visible
);
4280 for ( row
= first_visible
; row
< last_visible
; row
++ )
4282 calculator
.UpdateWithRow(row
);
4285 wxLogTrace("dataview",
4286 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4288 count
- bottom_part_start
,
4289 wxMax(0, last_visible
- first_visible
),
4293 int max_width
= calculator
.GetMaxWidth();
4294 if ( max_width
> 0 )
4295 max_width
+= 2 * PADDING_RIGHTLEFT
;
4297 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4301 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4302 unsigned int WXUNUSED(new_pos
))
4304 // do _not_ reorder m_cols elements here, they should always be in the
4305 // order in which columns were added, we only display the columns in
4307 m_clientArea
->UpdateDisplay();
4310 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4312 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4316 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4318 OnColumnsCountChanged();
4323 bool wxDataViewCtrl::ClearColumns()
4326 m_colsBestWidths
.clear();
4327 OnColumnsCountChanged();
4331 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4333 m_colsBestWidths
[idx
] = 0;
4336 m_headerArea
->UpdateColumn(idx
);
4339 void wxDataViewCtrl::InvalidateColBestWidths()
4341 m_colsBestWidths
.clear();
4342 m_colsBestWidths
.resize(m_cols
.size());
4346 const unsigned cols
= m_headerArea
->GetColumnCount();
4347 for ( unsigned i
= 0; i
< cols
; i
++ )
4348 m_headerArea
->UpdateColumn(i
);
4352 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4355 unsigned int len
= GetColumnCount();
4356 for ( unsigned int i
= 0; i
< len
; i
++ )
4358 wxDataViewColumn
* col
= GetColumnAt(i
);
4365 // This returns the position in pixels which is not what we want.
4368 unsigned int len
= GetColumnCount();
4369 for ( unsigned int i
= 0; i
< len
; i
++ )
4371 wxDataViewColumn
* col
= GetColumnAt(i
);
4372 if (col
->IsHidden())
4374 ret
+= col
->GetWidth();
4377 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4385 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4387 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4388 : GetColumn(m_sortingColumnIdx
);
4391 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4393 return GetItemByRow(m_clientArea
->GetCurrentRow());
4396 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4398 const int row
= m_clientArea
->GetRowByItem(item
);
4400 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4401 if ( static_cast<unsigned>(row
) != oldCurrent
)
4403 m_clientArea
->ChangeCurrentRow(row
);
4404 m_clientArea
->RefreshRow(oldCurrent
);
4405 m_clientArea
->RefreshRow(row
);
4409 // Selection code with wxDataViewItem as parameters
4410 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4412 return m_clientArea
->GetSelection();
4415 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4418 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4419 int len
= selection
.GetCount();
4420 for( int i
= 0; i
< len
; i
++)
4422 unsigned int row
= selection
[i
];
4423 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4428 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4430 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4432 wxDataViewItem last_parent
;
4434 int len
= sel
.GetCount();
4435 for( int i
= 0; i
< len
; i
++ )
4437 wxDataViewItem item
= sel
[i
];
4438 wxDataViewItem parent
= GetModel()->GetParent( item
);
4441 if (parent
!= last_parent
)
4442 ExpandAncestors(item
);
4445 last_parent
= parent
;
4446 int row
= m_clientArea
->GetRowByItem( item
);
4448 selection
.Add( static_cast<unsigned int>(row
) );
4451 m_clientArea
->SetSelections( selection
);
4454 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4456 ExpandAncestors( item
);
4458 int row
= m_clientArea
->GetRowByItem( item
);
4461 // Unselect all rows before select another in the single select mode
4462 if (m_clientArea
->IsSingleSel())
4463 m_clientArea
->SelectAllRows(false);
4465 m_clientArea
->SelectRow(row
, true);
4467 // Also set focus to the selected item
4468 m_clientArea
->ChangeCurrentRow( row
);
4472 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4474 int row
= m_clientArea
->GetRowByItem( item
);
4476 m_clientArea
->SelectRow(row
, false);
4479 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4481 int row
= m_clientArea
->GetRowByItem( item
);
4484 return m_clientArea
->IsRowSelected(row
);
4489 // Selection code with row number as parameter
4490 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4493 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4494 int len
= selection
.GetCount();
4495 for( int i
= 0; i
< len
; i
++)
4497 unsigned int row
= selection
[i
];
4503 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4505 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4506 int len
= sel
.GetCount();
4507 for( int i
= 0; i
< len
; i
++ )
4511 selection
.Add( static_cast<unsigned int>(row
) );
4513 m_clientArea
->SetSelections( selection
);
4516 void wxDataViewCtrl::Select( int row
)
4520 if (m_clientArea
->IsSingleSel())
4521 m_clientArea
->SelectAllRows(false);
4522 m_clientArea
->SelectRow( row
, true );
4526 void wxDataViewCtrl::Unselect( int row
)
4529 m_clientArea
->SelectRow(row
, false);
4532 bool wxDataViewCtrl::IsSelected( int row
) const
4535 return m_clientArea
->IsRowSelected(row
);
4539 void wxDataViewCtrl::SelectRange( int from
, int to
)
4542 for( int i
= from
; i
< to
; i
++ )
4544 m_clientArea
->Select(sel
);
4547 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4549 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4550 for( int i
= from
; i
< to
; i
++ )
4551 if( sel
.Index( i
) != wxNOT_FOUND
)
4553 m_clientArea
->SetSelections(sel
);
4556 void wxDataViewCtrl::SelectAll()
4558 m_clientArea
->SelectAllRows(true);
4561 void wxDataViewCtrl::UnselectAll()
4563 m_clientArea
->SelectAllRows(false);
4566 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4570 if( row
> (int) m_clientArea
->GetRowCount() )
4571 row
= m_clientArea
->GetRowCount();
4573 int first
= m_clientArea
->GetFirstVisibleRow();
4574 int last
= m_clientArea
->GetLastVisibleRow();
4576 m_clientArea
->ScrollTo( row
, column
);
4577 else if( row
> last
)
4578 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4580 m_clientArea
->ScrollTo( first
, column
);
4583 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4585 ExpandAncestors( item
);
4587 m_clientArea
->RecalculateDisplay();
4589 int row
= m_clientArea
->GetRowByItem(item
);
4592 if( column
== NULL
)
4593 EnsureVisible(row
, -1);
4595 EnsureVisible( row
, GetColumnIndex(column
) );
4600 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4601 wxDataViewColumn
* &column
) const
4603 m_clientArea
->HitTest(point
, item
, column
);
4606 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4607 const wxDataViewColumn
* column
) const
4609 return m_clientArea
->GetItemRect(item
, column
);
4612 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4614 return m_clientArea
->GetItemByRow( row
);
4617 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4619 return m_clientArea
->GetRowByItem( item
);
4622 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4624 ExpandAncestors( item
);
4626 int row
= m_clientArea
->GetRowByItem( item
);
4628 m_clientArea
->Expand(row
);
4631 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4633 int row
= m_clientArea
->GetRowByItem( item
);
4635 m_clientArea
->Collapse(row
);
4638 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4640 int row
= m_clientArea
->GetRowByItem( item
);
4642 return m_clientArea
->IsExpanded(row
);
4646 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4648 wxDataViewColumn
* col
= GetColumn( column
);
4652 wxRect itemRect
= GetItemRect(item
, col
);
4653 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4654 if (renderer
->GetMode() == wxDATAVIEW_CELL_EDITABLE
)
4655 renderer
->StartEditing(item
, itemRect
);
4658 #endif // !wxUSE_GENERICDATAVIEWCTRL
4660 #endif // wxUSE_DATAVIEWCTRL