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
)
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
)
3070 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3071 wxDataViewColumn
* &column
)
3073 wxDataViewColumn
*col
= NULL
;
3074 unsigned int cols
= GetOwner()->GetColumnCount();
3075 unsigned int colnum
= 0;
3077 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3078 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3080 col
= GetOwner()->GetColumnAt(colnum
);
3081 if (col
->IsHidden())
3082 continue; // skip it!
3084 unsigned int w
= col
->GetWidth();
3085 if (x_start
+w
>= (unsigned int)x
)
3092 item
= GetItemByRow( GetLineAt( y
) );
3095 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3096 const wxDataViewColumn
* column
)
3101 unsigned int cols
= GetOwner()->GetColumnCount();
3102 // If column is null the loop will compute the combined width of all columns.
3103 // Otherwise, it will compute the x position of the column we are looking for.
3104 for (unsigned int i
= 0; i
< cols
; i
++)
3106 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3111 if (col
->IsHidden())
3112 continue; // skip it!
3114 xpos
+= col
->GetWidth();
3115 width
+= col
->GetWidth();
3120 // If we have a column, we need can get its width directly.
3121 if(column
->IsHidden())
3124 width
= column
->GetWidth();
3129 // If we have no column, we reset the x position back to zero.
3133 // we have to take an expander column into account and compute its indentation
3134 // to get the correct x position where the actual text is
3136 int row
= GetRowByItem(item
);
3137 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3139 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3140 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3141 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3143 if(!node
->HasChildren())
3147 wxRect
itemRect( xpos
+ indent
,
3148 GetLineStart( row
),
3150 GetLineHeight( row
) );
3152 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3153 &itemRect
.x
, &itemRect
.y
);
3158 int wxDataViewMainWindow::RecalculateCount()
3160 if (IsVirtualList())
3162 wxDataViewVirtualListModel
*list_model
=
3163 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3165 return list_model
->GetCount();
3169 return m_root
->GetSubTreeCount();
3173 class ItemToRowJob
: public DoJob
3176 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3183 // Maybe binary search will help to speed up this process
3184 virtual int operator() ( wxDataViewTreeNode
* node
)
3187 if( node
->GetItem() == item
)
3192 if( node
->GetItem() == *m_iter
)
3199 ret
+= node
->GetSubTreeCount();
3205 virtual int operator() ( void * n
)
3208 if( n
== item
.GetID() )
3213 // the row number is begin from zero
3214 int GetResult() const
3218 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3219 wxDataViewItem item
;
3224 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3226 const wxDataViewModel
* model
= GetOwner()->GetModel();
3230 if (IsVirtualList())
3232 return wxPtrToUInt( item
.GetID() ) -1;
3239 // Compose the parent-chain of the item we are looking for
3240 wxVector
<wxDataViewItem
> parentChain
;
3241 wxDataViewItem
it( item
);
3244 parentChain
.push_back(it
);
3245 it
= model
->GetParent(it
);
3248 // add an 'invalid' item to represent our 'invisible' root node
3249 parentChain
.push_back(wxDataViewItem());
3251 // the parent chain was created by adding the deepest parent first.
3252 // so if we want to start at the root node, we have to iterate backwards through the vector
3253 ItemToRowJob
job( item
, parentChain
.rbegin() );
3254 Walker( m_root
, job
);
3255 return job
.GetResult();
3259 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3260 wxDataViewTreeNode
* node
)
3262 if( !model
->IsContainer( item
) )
3265 wxDataViewItemArray children
;
3266 unsigned int num
= model
->GetChildren( item
, children
);
3268 unsigned int index
= 0;
3269 while( index
< num
)
3271 if( model
->IsContainer( children
[index
] ) )
3273 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3274 n
->SetItem(children
[index
]);
3275 n
->SetHasChildren( true );
3280 node
->AddLeaf( children
[index
].GetID() );
3284 node
->SetSubTreeCount( num
);
3285 wxDataViewTreeNode
* n
= node
->GetParent();
3287 n
->ChangeSubTreeCount(num
);
3291 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3295 if (GetOwner()->GetModel()->IsVirtualListModel())
3301 m_root
= new wxDataViewTreeNode( NULL
);
3302 m_root
->SetHasChildren(true);
3304 // First we define a invalid item to fetch the top-level elements
3305 wxDataViewItem item
;
3307 BuildTreeHelper( model
, item
, m_root
);
3311 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3313 if( node
->GetNodeNumber() != 0 )
3315 int len
= node
->GetNodeNumber();
3316 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3317 for (int i
= 0; i
< len
; i
++)
3318 DestroyTreeHelper(nodes
[i
]);
3323 void wxDataViewMainWindow::DestroyTree()
3325 if (!IsVirtualList())
3327 ::DestroyTreeHelper(m_root
);
3333 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3335 wxWindow
* const parent
= GetParent();
3337 // propagate the char event upwards
3338 wxKeyEvent
eventForParent(event
);
3339 eventForParent
.SetEventObject(parent
);
3340 if ( parent
->ProcessWindowEvent(eventForParent
) )
3343 if ( parent
->HandleAsNavigationKey(event
) )
3346 // no item -> nothing to do
3347 if (!HasCurrentRow())
3353 // don't use m_linesPerPage directly as it might not be computed yet
3354 const int pageSize
= GetCountPerPage();
3355 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3357 switch ( event
.GetKeyCode() )
3361 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3363 le
.SetItem( GetItemByRow(m_currentRow
) );
3364 le
.SetEventObject(parent
);
3365 le
.SetModel(GetOwner()->GetModel());
3367 parent
->GetEventHandler()->ProcessEvent(le
);
3372 if ( m_currentRow
> 0 )
3373 OnArrowChar( m_currentRow
- 1, event
);
3377 if ( m_currentRow
< GetRowCount() - 1 )
3378 OnArrowChar( m_currentRow
+ 1, event
);
3380 // Add the process for tree expanding/collapsing
3386 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3390 if (node
->HasChildren() && node
->IsOpen())
3392 Collapse(m_currentRow
);
3394 else // if the node is already closed we move the selection to its parent
3396 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3398 if(!node
->HasChildren())
3403 int parent
= GetRowByItem( parent_node
->GetItem() );
3406 unsigned int row
= m_currentRow
;
3407 SelectRow( row
, false);
3408 SelectRow( parent
, true );
3409 ChangeCurrentRow( parent
);
3410 GetOwner()->EnsureVisible( parent
, -1 );
3411 SendSelectionChangedEvent( parent_node
->GetItem() );
3419 if (!IsExpanded( m_currentRow
))
3420 Expand( m_currentRow
);
3423 unsigned int row
= m_currentRow
;
3424 SelectRow( row
, false );
3425 SelectRow( row
+ 1, true );
3426 ChangeCurrentRow( row
+ 1 );
3427 GetOwner()->EnsureVisible( row
+ 1, -1 );
3428 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3435 OnArrowChar( GetRowCount() - 1, event
);
3440 OnArrowChar( 0, event
);
3445 int steps
= pageSize
- 1;
3446 int index
= m_currentRow
- steps
;
3450 OnArrowChar( index
, event
);
3456 int steps
= pageSize
- 1;
3457 unsigned int index
= m_currentRow
+ steps
;
3458 unsigned int count
= GetRowCount();
3459 if ( index
>= count
)
3462 OnArrowChar( index
, event
);
3468 if(m_selection
.size() == 1)
3470 // TODO: we need to revise that when we have a concept for a 'current column'
3471 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3481 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3483 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3485 // let the base handle mouse wheel events.
3490 // set the focus to ourself if any of the mouse buttons are pressed
3491 if(event
.ButtonDown() && !HasFocus())
3494 int x
= event
.GetX();
3495 int y
= event
.GetY();
3496 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3497 wxDataViewColumn
*col
= NULL
;
3500 unsigned int cols
= GetOwner()->GetColumnCount();
3502 for (i
= 0; i
< cols
; i
++)
3504 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3506 continue; // skip it!
3508 if (x
< xpos
+ c
->GetWidth())
3513 xpos
+= c
->GetWidth();
3521 wxDataViewRenderer
*cell
= col
->GetRenderer();
3522 unsigned int current
= GetLineAt( y
);
3523 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3525 // Unselect all if below the last row ?
3530 // Test whether the mouse is hovered on the tree item button
3531 bool hoverOverExpander
= false;
3532 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3534 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3535 if( node
!=NULL
&& node
->HasChildren() )
3537 int indent
= node
->GetIndentLevel();
3538 indent
= GetOwner()->GetIndent()*indent
;
3540 // we make the rectangle we are looking in a bit bigger than the actual
3541 // visual expander so the user can hit that little thing reliably
3542 wxRect
rect( xpos
+ indent
,
3543 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3544 m_lineHeight
, m_lineHeight
);
3545 if( rect
.Contains(x
, y
) )
3547 // So the mouse is over the expander
3548 hoverOverExpander
= true;
3549 if (m_underMouse
&& m_underMouse
!= node
)
3551 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3552 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3554 if (m_underMouse
!= node
)
3556 // wxLogMessage("Do the row: %d", current);
3557 RefreshRow(current
);
3559 m_underMouse
= node
;
3562 if (node
!=NULL
&& !node
->HasChildren())
3565 if (!hoverOverExpander
)
3567 if (m_underMouse
!= NULL
)
3569 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3570 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3571 m_underMouse
= NULL
;
3575 wxDataViewModel
*model
= GetOwner()->GetModel();
3577 #if wxUSE_DRAG_AND_DROP
3578 if (event
.Dragging())
3580 if (m_dragCount
== 0)
3582 // we have to report the raw, physical coords as we want to be
3583 // able to call HitTest(event.m_pointDrag) from the user code to
3584 // get the item being dragged
3585 m_dragStart
= event
.GetPosition();
3590 if (m_dragCount
!= 3)
3593 if (event
.LeftIsDown())
3595 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3596 &m_dragStart
.x
, &m_dragStart
.y
);
3597 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3598 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3600 // Notify cell about drag
3601 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3602 event
.SetEventObject( m_owner
);
3603 event
.SetItem( item
);
3604 event
.SetModel( model
);
3605 if (!m_owner
->HandleWindowEvent( event
))
3608 if (!event
.IsAllowed())
3611 wxDataObject
*obj
= event
.GetDataObject();
3615 wxDataViewDropSource
drag( this, drag_item_row
);
3616 drag
.SetData( *obj
);
3617 /* wxDragResult res = */ drag
.DoDragDrop();
3626 #endif // wxUSE_DRAG_AND_DROP
3628 bool simulateClick
= false;
3630 if (event
.ButtonDClick())
3632 m_renameTimer
->Stop();
3633 m_lastOnSame
= false;
3636 wxDataViewItem item
= GetItemByRow(current
);
3637 bool ignore_other_columns
=
3638 ((GetOwner()->GetExpanderColumn() != col
) &&
3639 (model
->IsContainer(item
)) &&
3640 (!model
->HasContainerColumns(item
)));
3642 if (event
.LeftDClick())
3644 if(hoverOverExpander
)
3646 // a double click on the expander will be converted into a "simulated" normal click
3647 simulateClick
= true;
3649 else if ( current
== m_lineLastClicked
)
3651 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3653 const unsigned colIdx
= col
->GetModelColumn();
3655 cell
->PrepareForItem(model
, item
, colIdx
);
3657 wxRect
cell_rect( xpos
, GetLineStart( current
),
3658 col
->GetWidth(), GetLineHeight( current
) );
3659 cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3663 wxWindow
*parent
= GetParent();
3664 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3666 le
.SetColumn( col
->GetModelColumn() );
3667 le
.SetDataViewColumn( col
);
3668 le
.SetEventObject(parent
);
3669 le
.SetModel(GetOwner()->GetModel());
3671 parent
->GetEventHandler()->ProcessEvent(le
);
3677 // The first click was on another item, so don't interpret this as
3678 // a double click, but as a simple click instead
3679 simulateClick
= true;
3683 if (event
.LeftUp() && !hoverOverExpander
)
3685 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3687 // select single line
3688 SelectAllRows( false );
3689 SelectRow( m_lineSelectSingleOnUp
, true );
3690 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3693 // If the user click the expander, we do not do editing even if the column
3694 // with expander are editable
3695 if (m_lastOnSame
&& !ignore_other_columns
)
3697 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3698 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3700 m_renameTimer
->Start( 100, true );
3704 m_lastOnSame
= false;
3705 m_lineSelectSingleOnUp
= (unsigned int)-1;
3707 else if(!event
.LeftUp())
3709 // This is necessary, because after a DnD operation in
3710 // from and to ourself, the up event is swallowed by the
3711 // DnD code. So on next non-up event (which means here and
3712 // now) m_lineSelectSingleOnUp should be reset.
3713 m_lineSelectSingleOnUp
= (unsigned int)-1;
3716 if (event
.RightDown())
3718 m_lineBeforeLastClicked
= m_lineLastClicked
;
3719 m_lineLastClicked
= current
;
3721 // If the item is already selected, do not update the selection.
3722 // Multi-selections should not be cleared if a selected item is clicked.
3723 if (!IsRowSelected(current
))
3725 SelectAllRows(false);
3726 const unsigned oldCurrent
= m_currentRow
;
3727 ChangeCurrentRow(current
);
3728 SelectRow(m_currentRow
,true);
3729 RefreshRow(oldCurrent
);
3730 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3733 else if (event
.RightUp())
3736 model
->GetValue( value
, item
, col
->GetModelColumn() );
3737 wxWindow
*parent
= GetParent();
3738 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3740 le
.SetColumn( col
->GetModelColumn() );
3741 le
.SetDataViewColumn( col
);
3742 le
.SetEventObject(parent
);
3743 le
.SetModel(GetOwner()->GetModel());
3745 parent
->GetEventHandler()->ProcessEvent(le
);
3747 else if (event
.MiddleDown())
3751 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3753 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3755 // hoverOverExpander being true tells us that our node must be
3756 // valid and have children.
3757 // So we don't need any extra checks.
3758 if( node
->IsOpen() )
3763 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3765 m_lineBeforeLastClicked
= m_lineLastClicked
;
3766 m_lineLastClicked
= current
;
3768 unsigned int oldCurrentRow
= m_currentRow
;
3769 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3771 bool cmdModifierDown
= event
.CmdDown();
3772 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3774 if ( IsSingleSel() || !IsRowSelected(current
) )
3776 SelectAllRows( false );
3777 ChangeCurrentRow(current
);
3778 SelectRow(m_currentRow
,true);
3779 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3781 else // multi sel & current is highlighted & no mod keys
3783 m_lineSelectSingleOnUp
= current
;
3784 ChangeCurrentRow(current
); // change focus
3787 else // multi sel & either ctrl or shift is down
3789 if (cmdModifierDown
)
3791 ChangeCurrentRow(current
);
3792 ReverseRowSelection(m_currentRow
);
3793 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3795 else if (event
.ShiftDown())
3797 ChangeCurrentRow(current
);
3799 unsigned int lineFrom
= oldCurrentRow
,
3802 if ( lineTo
< lineFrom
)
3805 lineFrom
= m_currentRow
;
3808 SelectRows(lineFrom
, lineTo
, true);
3809 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3811 else // !ctrl, !shift
3813 // test in the enclosing if should make it impossible
3814 wxFAIL_MSG( wxT("how did we get here?") );
3818 if (m_currentRow
!= oldCurrentRow
)
3819 RefreshRow( oldCurrentRow
);
3821 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3823 // Update selection here...
3826 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3827 (current
== oldCurrentRow
)) && oldWasSelected
;
3829 // Call LeftClick after everything else as under GTK+
3830 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3832 // notify cell about click
3833 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3835 wxRect
cell_rect( xpos
, GetLineStart( current
),
3836 col
->GetWidth(), GetLineHeight( current
) );
3838 // Report position relative to the cell's custom area, i.e.
3839 // no the entire space as given by the control but the one
3840 // used by the renderer after calculation of alignment etc.
3842 // adjust the rectangle ourselves to account for the alignment
3843 wxRect rectItem
= cell_rect
;
3844 const int align
= cell
->GetAlignment();
3845 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3847 const wxSize size
= cell
->GetSize();
3849 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3851 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3852 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3853 else if ( align
& wxALIGN_RIGHT
)
3854 rectItem
.x
+= cell_rect
.width
- size
.x
;
3855 // else: wxALIGN_LEFT is the default
3858 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3860 if ( align
& wxALIGN_CENTER_VERTICAL
)
3861 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3862 else if ( align
& wxALIGN_BOTTOM
)
3863 rectItem
.y
+= cell_rect
.height
- size
.y
;
3864 // else: wxALIGN_TOP is the default
3868 wxPoint
pos( event
.GetPosition() );
3869 pos
.x
-= rectItem
.x
;
3870 pos
.y
-= rectItem
.y
;
3872 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3874 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3875 model
, item
, col
->GetModelColumn());
3880 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3884 if (HasCurrentRow())
3890 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3894 if (HasCurrentRow())
3900 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3902 if( m_selection
.GetCount() != 1 )
3903 return wxDataViewItem();
3905 return GetItemByRow( m_selection
.Item(0));
3908 //-----------------------------------------------------------------------------
3910 //-----------------------------------------------------------------------------
3912 WX_DEFINE_LIST(wxDataViewColumnList
)
3914 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3915 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3916 EVT_SIZE(wxDataViewCtrl::OnSize
)
3919 wxDataViewCtrl::~wxDataViewCtrl()
3922 GetModel()->RemoveNotifier( m_notifier
);
3925 m_colsBestWidths
.clear();
3928 void wxDataViewCtrl::Init()
3930 m_cols
.DeleteContents(true);
3933 // No sorting column at start
3934 m_sortingColumnIdx
= wxNOT_FOUND
;
3936 m_headerArea
= NULL
;
3939 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3944 const wxValidator
& validator
,
3945 const wxString
& name
)
3947 // if ( (style & wxBORDER_MASK) == 0)
3948 // style |= wxBORDER_SUNKEN;
3952 if (!wxControl::Create( parent
, id
, pos
, size
,
3953 style
| wxScrolledWindowStyle
, validator
, name
))
3956 SetInitialSize(size
);
3959 MacSetClipChildren( true );
3962 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3964 // We use the cursor keys for moving the selection, not scrolling, so call
3965 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3966 // keyboard events forwarded to us from wxListMainWindow.
3967 DisableKeyboardScrolling();
3969 if (HasFlag(wxDV_NO_HEADER
))
3970 m_headerArea
= NULL
;
3972 m_headerArea
= new wxDataViewHeaderWindow(this);
3974 SetTargetWindow( m_clientArea
);
3976 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3978 sizer
->Add( m_headerArea
, 0, wxGROW
);
3979 sizer
->Add( m_clientArea
, 1, wxGROW
);
3985 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3987 return wxBORDER_THEME
;
3991 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3995 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3998 // we need to process arrows ourselves for scrolling
3999 if ( nMsg
== WM_GETDLGCODE
)
4001 rc
|= DLGC_WANTARROWS
;
4009 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4011 wxSize newsize
= size
;
4012 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4013 newsize
.y
-= m_headerArea
->GetSize().y
;
4018 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4020 // We need to override OnSize so that our scrolled
4021 // window a) does call Layout() to use sizers for
4022 // positioning the controls but b) does not query
4023 // the sizer for their size and use that for setting
4024 // the scrollable area as set that ourselves by
4025 // calling SetScrollbar() further down.
4031 // We must redraw the headers if their height changed. Normally this
4032 // shouldn't happen as the control shouldn't let itself be resized beneath
4033 // its minimal height but avoid the display artefacts that appear if it
4034 // does happen, e.g. because there is really not enough vertical space.
4035 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4036 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4038 m_headerArea
->Refresh();
4042 void wxDataViewCtrl::SetFocus()
4045 m_clientArea
->SetFocus();
4048 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4050 if (!wxDataViewCtrlBase::AssociateModel( model
))
4053 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4055 model
->AddNotifier( m_notifier
);
4057 m_clientArea
->DestroyTree();
4059 m_clientArea
->BuildTree(model
);
4061 m_clientArea
->UpdateDisplay();
4066 #if wxUSE_DRAG_AND_DROP
4068 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4070 return m_clientArea
->EnableDragSource( format
);
4073 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4075 return m_clientArea
->EnableDropTarget( format
);
4078 #endif // wxUSE_DRAG_AND_DROP
4080 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4082 if (!wxDataViewCtrlBase::AppendColumn(col
))
4085 m_cols
.Append( col
);
4086 m_colsBestWidths
.push_back(0);
4087 OnColumnsCountChanged();
4091 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4093 if (!wxDataViewCtrlBase::PrependColumn(col
))
4096 m_cols
.Insert( col
);
4097 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4098 OnColumnsCountChanged();
4102 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4104 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4107 m_cols
.Insert( pos
, col
);
4108 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4109 OnColumnsCountChanged();
4113 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4116 m_headerArea
->UpdateColumn(idx
);
4118 m_clientArea
->UpdateDisplay();
4121 void wxDataViewCtrl::OnColumnsCountChanged()
4124 m_headerArea
->SetColumnCount(GetColumnCount());
4126 m_clientArea
->UpdateDisplay();
4129 void wxDataViewCtrl::DoSetExpanderColumn()
4131 m_clientArea
->UpdateDisplay();
4134 void wxDataViewCtrl::DoSetIndent()
4136 m_clientArea
->UpdateDisplay();
4139 unsigned int wxDataViewCtrl::GetColumnCount() const
4141 return m_cols
.GetCount();
4144 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4146 if ( !m_clientArea
)
4149 m_clientArea
->SetRowHeight(lineHeight
);
4154 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4159 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4161 // columns can't be reordered if there is no header window which allows
4163 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4166 return GetColumn(idx
);
4169 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4171 const unsigned count
= m_cols
.size();
4172 for ( unsigned n
= 0; n
< count
; n
++ )
4174 if ( m_cols
[n
] == column
)
4181 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4183 if ( m_colsBestWidths
[idx
] != 0 )
4184 return m_colsBestWidths
[idx
];
4186 const int count
= m_clientArea
->GetRowCount();
4187 wxDataViewColumn
*column
= GetColumn(idx
);
4188 wxDataViewRenderer
*renderer
=
4189 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4191 class MaxWidthCalculator
4194 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4195 wxDataViewRenderer
*renderer
,
4196 const wxDataViewModel
*model
,
4199 m_clientArea(clientArea
),
4200 m_renderer(renderer
),
4206 void UpdateWithWidth(int width
)
4208 m_width
= wxMax(m_width
, width
);
4211 void UpdateWithRow(int row
)
4213 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4214 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4215 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4218 int GetMaxWidth() const { return m_width
; }
4222 wxDataViewMainWindow
*m_clientArea
;
4223 wxDataViewRenderer
*m_renderer
;
4224 const wxDataViewModel
*m_model
;
4228 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4229 GetModel(), column
->GetModelColumn());
4233 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4234 // Labels on native MSW header are indented on both sides
4236 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4237 calculator
.UpdateWithWidth(header_width
);
4240 // The code below deserves some explanation. For very large controls, we
4241 // simply can't afford to calculate sizes for all items, it takes too
4242 // long. So the best we can do is to check the first and the last N/2
4243 // items in the control for some sufficiently large N and calculate best
4244 // sizes from that. That can result in the calculated best width being too
4245 // small for some outliers, but it's better to get slightly imperfect
4246 // result than to wait several seconds after every update. To avoid highly
4247 // visible miscalculations, we also include all currently visible items
4248 // no matter what. Finally, the value of N is determined dynamically by
4249 // measuring how much time we spent on the determining item widths so far.
4252 int top_part_end
= count
;
4253 static const long CALC_TIMEOUT
= 20/*ms*/;
4254 // don't call wxStopWatch::Time() too often
4255 static const unsigned CALC_CHECK_FREQ
= 100;
4258 // use some hard-coded limit, that's the best we can do without timer
4259 int top_part_end
= wxMin(500, count
);
4260 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4264 for ( row
= 0; row
< top_part_end
; row
++ )
4267 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4268 timer
.Time() > CALC_TIMEOUT
)
4270 #endif // wxUSE_STOPWATCH
4271 calculator
.UpdateWithRow(row
);
4274 // row is the first unmeasured item now; that's our value of N/2
4280 // add bottom N/2 items now:
4281 const int bottom_part_start
= wxMax(row
, count
- row
);
4282 for ( row
= bottom_part_start
; row
< count
; row
++ )
4284 calculator
.UpdateWithRow(row
);
4287 // finally, include currently visible items in the calculation:
4288 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4289 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4290 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4292 first_visible
= wxMax(first_visible
, top_part_end
);
4293 last_visible
= wxMin(bottom_part_start
, last_visible
);
4295 for ( row
= first_visible
; row
< last_visible
; row
++ )
4297 calculator
.UpdateWithRow(row
);
4300 wxLogTrace("dataview",
4301 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4303 count
- bottom_part_start
,
4304 wxMax(0, last_visible
- first_visible
),
4308 int max_width
= calculator
.GetMaxWidth();
4309 if ( max_width
> 0 )
4310 max_width
+= 2 * PADDING_RIGHTLEFT
;
4312 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4316 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4317 unsigned int WXUNUSED(new_pos
))
4319 // do _not_ reorder m_cols elements here, they should always be in the
4320 // order in which columns were added, we only display the columns in
4322 m_clientArea
->UpdateDisplay();
4325 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4327 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4331 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4333 OnColumnsCountChanged();
4338 bool wxDataViewCtrl::ClearColumns()
4341 m_colsBestWidths
.clear();
4342 OnColumnsCountChanged();
4346 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4348 m_colsBestWidths
[idx
] = 0;
4351 m_headerArea
->UpdateColumn(idx
);
4354 void wxDataViewCtrl::InvalidateColBestWidths()
4356 m_colsBestWidths
.clear();
4357 m_colsBestWidths
.resize(m_cols
.size());
4361 const unsigned cols
= m_headerArea
->GetColumnCount();
4362 for ( unsigned i
= 0; i
< cols
; i
++ )
4363 m_headerArea
->UpdateColumn(i
);
4367 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4370 unsigned int len
= GetColumnCount();
4371 for ( unsigned int i
= 0; i
< len
; i
++ )
4373 wxDataViewColumn
* col
= GetColumnAt(i
);
4380 // This returns the position in pixels which is not what we want.
4383 unsigned int len
= GetColumnCount();
4384 for ( unsigned int i
= 0; i
< len
; i
++ )
4386 wxDataViewColumn
* col
= GetColumnAt(i
);
4387 if (col
->IsHidden())
4389 ret
+= col
->GetWidth();
4392 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4400 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4402 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4403 : GetColumn(m_sortingColumnIdx
);
4406 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4408 return GetItemByRow(m_clientArea
->GetCurrentRow());
4411 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4413 const int row
= m_clientArea
->GetRowByItem(item
);
4415 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4416 if ( static_cast<unsigned>(row
) != oldCurrent
)
4418 m_clientArea
->ChangeCurrentRow(row
);
4419 m_clientArea
->RefreshRow(oldCurrent
);
4420 m_clientArea
->RefreshRow(row
);
4424 // Selection code with wxDataViewItem as parameters
4425 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4427 return m_clientArea
->GetSelection();
4430 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4433 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4434 int len
= selection
.GetCount();
4435 for( int i
= 0; i
< len
; i
++)
4437 unsigned int row
= selection
[i
];
4438 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4443 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4445 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4447 wxDataViewItem last_parent
;
4449 int len
= sel
.GetCount();
4450 for( int i
= 0; i
< len
; i
++ )
4452 wxDataViewItem item
= sel
[i
];
4453 wxDataViewItem parent
= GetModel()->GetParent( item
);
4456 if (parent
!= last_parent
)
4457 ExpandAncestors(item
);
4460 last_parent
= parent
;
4461 int row
= m_clientArea
->GetRowByItem( item
);
4463 selection
.Add( static_cast<unsigned int>(row
) );
4466 m_clientArea
->SetSelections( selection
);
4469 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4471 ExpandAncestors( item
);
4473 int row
= m_clientArea
->GetRowByItem( item
);
4476 // Unselect all rows before select another in the single select mode
4477 if (m_clientArea
->IsSingleSel())
4478 m_clientArea
->SelectAllRows(false);
4480 m_clientArea
->SelectRow(row
, true);
4482 // Also set focus to the selected item
4483 m_clientArea
->ChangeCurrentRow( row
);
4487 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4489 int row
= m_clientArea
->GetRowByItem( item
);
4491 m_clientArea
->SelectRow(row
, false);
4494 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4496 int row
= m_clientArea
->GetRowByItem( item
);
4499 return m_clientArea
->IsRowSelected(row
);
4504 // Selection code with row number as parameter
4505 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4508 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4509 int len
= selection
.GetCount();
4510 for( int i
= 0; i
< len
; i
++)
4512 unsigned int row
= selection
[i
];
4518 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4520 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4521 int len
= sel
.GetCount();
4522 for( int i
= 0; i
< len
; i
++ )
4526 selection
.Add( static_cast<unsigned int>(row
) );
4528 m_clientArea
->SetSelections( selection
);
4531 void wxDataViewCtrl::Select( int row
)
4535 if (m_clientArea
->IsSingleSel())
4536 m_clientArea
->SelectAllRows(false);
4537 m_clientArea
->SelectRow( row
, true );
4541 void wxDataViewCtrl::Unselect( int row
)
4544 m_clientArea
->SelectRow(row
, false);
4547 bool wxDataViewCtrl::IsSelected( int row
) const
4550 return m_clientArea
->IsRowSelected(row
);
4554 void wxDataViewCtrl::SelectRange( int from
, int to
)
4557 for( int i
= from
; i
< to
; i
++ )
4559 m_clientArea
->Select(sel
);
4562 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4564 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4565 for( int i
= from
; i
< to
; i
++ )
4566 if( sel
.Index( i
) != wxNOT_FOUND
)
4568 m_clientArea
->SetSelections(sel
);
4571 void wxDataViewCtrl::SelectAll()
4573 m_clientArea
->SelectAllRows(true);
4576 void wxDataViewCtrl::UnselectAll()
4578 m_clientArea
->SelectAllRows(false);
4581 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4585 if( row
> (int) m_clientArea
->GetRowCount() )
4586 row
= m_clientArea
->GetRowCount();
4588 int first
= m_clientArea
->GetFirstVisibleRow();
4589 int last
= m_clientArea
->GetLastVisibleRow();
4591 m_clientArea
->ScrollTo( row
, column
);
4592 else if( row
> last
)
4593 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4595 m_clientArea
->ScrollTo( first
, column
);
4598 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4600 ExpandAncestors( item
);
4602 m_clientArea
->RecalculateDisplay();
4604 int row
= m_clientArea
->GetRowByItem(item
);
4607 if( column
== NULL
)
4608 EnsureVisible(row
, -1);
4610 EnsureVisible( row
, GetColumnIndex(column
) );
4615 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4616 wxDataViewColumn
* &column
) const
4618 m_clientArea
->HitTest(point
, item
, column
);
4621 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4622 const wxDataViewColumn
* column
) const
4624 return m_clientArea
->GetItemRect(item
, column
);
4627 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4629 return m_clientArea
->GetItemByRow( row
);
4632 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4634 return m_clientArea
->GetRowByItem( item
);
4637 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4639 ExpandAncestors( item
);
4641 int row
= m_clientArea
->GetRowByItem( item
);
4643 m_clientArea
->Expand(row
);
4646 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4648 int row
= m_clientArea
->GetRowByItem( item
);
4650 m_clientArea
->Collapse(row
);
4653 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4655 int row
= m_clientArea
->GetRowByItem( item
);
4657 return m_clientArea
->IsExpanded(row
);
4661 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4663 wxDataViewColumn
* col
= GetColumn( column
);
4667 wxRect itemRect
= GetItemRect(item
, col
);
4668 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4669 if (renderer
->GetMode() == wxDATAVIEW_CELL_EDITABLE
)
4670 renderer
->StartEditing(item
, itemRect
);
4673 #endif // !wxUSE_GENERICDATAVIEWCTRL
4675 #endif // wxUSE_DATAVIEWCTRL