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
);
218 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, idx
);
221 void OnRClick(wxHeaderCtrlEvent
& event
)
223 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
228 void OnResize(wxHeaderCtrlEvent
& event
)
230 wxDataViewCtrl
* const owner
= GetOwner();
232 const unsigned col
= event
.GetColumn();
233 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
234 GetOwner()->OnColumnChange(col
);
237 void OnEndReorder(wxHeaderCtrlEvent
& event
)
239 wxDataViewCtrl
* const owner
= GetOwner();
240 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
241 event
.GetNewOrder());
244 DECLARE_EVENT_TABLE()
245 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
248 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
249 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
250 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
252 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
253 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
255 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
258 //-----------------------------------------------------------------------------
259 // wxDataViewRenameTimer
260 //-----------------------------------------------------------------------------
262 class wxDataViewRenameTimer
: public wxTimer
265 wxDataViewMainWindow
*m_owner
;
268 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
272 //-----------------------------------------------------------------------------
273 // wxDataViewTreeNode
274 //-----------------------------------------------------------------------------
276 class wxDataViewTreeNode
;
277 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
278 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
280 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
281 wxDataViewTreeNode
** node2
);
282 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
284 class wxDataViewTreeNode
287 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
294 m_hasChildren
= false;
298 ~wxDataViewTreeNode()
302 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
303 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
304 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
305 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
307 void AddNode( wxDataViewTreeNode
* node
)
309 m_leaves
.Add( node
->GetItem().GetID() );
311 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
314 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
316 void AddLeaf( void * leaf
)
318 m_leaves
.Add( leaf
);
320 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
323 wxDataViewItem
& GetItem() { return m_item
; }
324 const wxDataViewItem
& GetItem() const { return m_item
; }
325 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
327 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
328 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
329 int GetIndentLevel() const
332 const wxDataViewTreeNode
* node
= this;
333 while( node
->GetParent()->GetParent() != NULL
)
335 node
= node
->GetParent();
348 int len
= m_nodes
.GetCount();
350 for ( int i
= 0;i
< len
; i
++)
351 sum
+= m_nodes
[i
]->GetSubTreeCount();
353 sum
+= m_leaves
.GetCount();
356 ChangeSubTreeCount(-sum
);
362 ChangeSubTreeCount(sum
);
365 bool HasChildren() const { return m_hasChildren
; }
366 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
368 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
369 int GetSubTreeCount() const { return m_subTreeCount
; }
370 void ChangeSubTreeCount( int num
)
374 m_subTreeCount
+= num
;
376 m_parent
->ChangeSubTreeCount(num
);
383 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
384 int len
= m_nodes
.GetCount();
385 for (int i
= 0; i
< len
; i
++)
386 m_nodes
[i
]->Resort();
387 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
392 wxDataViewTreeNode
*m_parent
;
393 wxDataViewTreeNodes m_nodes
;
394 wxDataViewTreeLeaves m_leaves
;
395 wxDataViewItem m_item
;
401 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
402 wxDataViewTreeNode
** node2
)
404 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
407 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
409 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
413 //-----------------------------------------------------------------------------
414 // wxDataViewMainWindow
415 //-----------------------------------------------------------------------------
417 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
419 class wxDataViewMainWindow
: public wxWindow
422 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
424 const wxPoint
&pos
= wxDefaultPosition
,
425 const wxSize
&size
= wxDefaultSize
,
426 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
427 virtual ~wxDataViewMainWindow();
429 bool IsList() const { return GetOwner()->GetModel()->IsListModel(); }
430 bool IsVirtualList() const { return m_root
== NULL
; }
432 // notifications from wxDataViewModel
433 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
434 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
435 bool ItemChanged( const wxDataViewItem
&item
);
436 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
440 if (!IsVirtualList())
450 g_model
= GetOwner()->GetModel();
451 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
454 if (g_model
->HasDefaultCompare())
462 g_column
= col
->GetModelColumn();
463 g_asending
= col
->IsSortOrderAscending();
466 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
467 wxDataViewCtrl
*GetOwner() { return m_owner
; }
468 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
470 #if wxUSE_DRAG_AND_DROP
471 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
472 #endif // wxUSE_DRAG_AND_DROP
473 void OnPaint( wxPaintEvent
&event
);
474 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
475 void OnChar( wxKeyEvent
&event
);
476 void OnMouse( wxMouseEvent
&event
);
477 void OnSetFocus( wxFocusEvent
&event
);
478 void OnKillFocus( wxFocusEvent
&event
);
480 void UpdateDisplay();
481 void RecalculateDisplay();
482 void OnInternalIdle();
484 void OnRenameTimer();
486 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
487 void ScrollTo( int rows
, int column
);
489 unsigned GetCurrentRow() const { return m_currentRow
; }
490 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
491 void ChangeCurrentRow( unsigned int row
);
493 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
494 bool IsEmpty() { return GetRowCount() == 0; }
496 int GetCountPerPage() const;
497 int GetEndOfLastCol() const;
498 unsigned int GetFirstVisibleRow() const;
500 // I change this method to un const because in the tree view,
501 // the displaying number of the tree are changing along with the
502 // expanding/collapsing of the tree nodes
503 unsigned int GetLastVisibleRow();
504 unsigned int GetRowCount();
506 wxDataViewItem
GetSelection() const;
507 wxDataViewSelection
GetSelections(){ return m_selection
; }
508 void SetSelections( const wxDataViewSelection
& sel
)
509 { m_selection
= sel
; UpdateDisplay(); }
510 void Select( const wxArrayInt
& aSelections
);
511 void SelectAllRows( bool on
);
512 void SelectRow( unsigned int row
, bool on
);
513 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
514 void ReverseRowSelection( unsigned int row
);
515 bool IsRowSelected( unsigned int row
);
516 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
518 void RefreshRow( unsigned int row
);
519 void RefreshRows( unsigned int from
, unsigned int to
);
520 void RefreshRowsAfter( unsigned int firstRow
);
522 // returns the colour to be used for drawing the rules
523 wxColour
GetRuleColour() const
525 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
528 wxRect
GetLineRect( unsigned int row
) const;
530 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
531 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
532 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
534 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
536 // Some useful functions for row and item mapping
537 wxDataViewItem
GetItemByRow( unsigned int row
) const;
538 int GetRowByItem( const wxDataViewItem
& item
) const;
540 // Methods for building the mapping tree
541 void BuildTree( wxDataViewModel
* model
);
543 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
544 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
546 void Expand( unsigned int row
);
547 void Collapse( unsigned int row
);
548 bool IsExpanded( unsigned int row
) const;
549 bool HasChildren( unsigned int row
) const;
551 #if wxUSE_DRAG_AND_DROP
552 bool EnableDragSource( const wxDataFormat
&format
);
553 bool EnableDropTarget( const wxDataFormat
&format
);
555 void RemoveDropHint();
556 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
557 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
558 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
560 #endif // wxUSE_DRAG_AND_DROP
563 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
564 // We did not need this temporarily
565 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
567 int RecalculateCount();
569 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
571 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
574 wxDataViewCtrl
*m_owner
;
578 wxDataViewColumn
*m_currentCol
;
579 unsigned int m_currentRow
;
580 wxDataViewSelection m_selection
;
582 wxDataViewRenameTimer
*m_renameTimer
;
587 #if wxUSE_DRAG_AND_DROP
592 wxDataFormat m_dragFormat
;
595 wxDataFormat m_dropFormat
;
597 unsigned int m_dropHintLine
;
598 #endif // wxUSE_DRAG_AND_DROP
600 // for double click logic
601 unsigned int m_lineLastClicked
,
602 m_lineBeforeLastClicked
,
603 m_lineSelectSingleOnUp
;
605 // the pen used to draw horiz/vertical rules
608 // the pen used to draw the expander and the lines
611 // This is the tree structure of the model
612 wxDataViewTreeNode
* m_root
;
615 // This is the tree node under the cursor
616 wxDataViewTreeNode
* m_underMouse
;
619 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
620 DECLARE_EVENT_TABLE()
623 // ---------------------------------------------------------
624 // wxGenericDataViewModelNotifier
625 // ---------------------------------------------------------
627 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
630 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
631 { m_mainWindow
= mainWindow
; }
633 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
634 { return m_mainWindow
->ItemAdded( parent
, item
); }
635 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
636 { return m_mainWindow
->ItemDeleted( parent
, item
); }
637 virtual bool ItemChanged( const wxDataViewItem
& item
)
638 { return m_mainWindow
->ItemChanged(item
); }
639 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
640 { return m_mainWindow
->ValueChanged( item
, col
); }
641 virtual bool Cleared()
642 { return m_mainWindow
->Cleared(); }
643 virtual void Resort()
644 { m_mainWindow
->Resort(); }
646 wxDataViewMainWindow
*m_mainWindow
;
649 // ---------------------------------------------------------
650 // wxDataViewRenderer
651 // ---------------------------------------------------------
653 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
655 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
656 wxDataViewCellMode mode
,
658 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
662 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
666 wxDataViewRenderer::~wxDataViewRenderer()
671 wxDC
*wxDataViewRenderer::GetDC()
675 if (GetOwner() == NULL
)
677 if (GetOwner()->GetOwner() == NULL
)
679 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
685 void wxDataViewRenderer::SetAlignment( int align
)
690 int wxDataViewRenderer::GetAlignment() const
695 // ---------------------------------------------------------
696 // wxDataViewCustomRenderer
697 // ---------------------------------------------------------
699 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
701 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
702 wxDataViewCellMode mode
, int align
) :
703 wxDataViewRenderer( varianttype
, mode
, align
)
707 // ---------------------------------------------------------
708 // wxDataViewTextRenderer
709 // ---------------------------------------------------------
711 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
713 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
714 wxDataViewCellMode mode
, int align
) :
715 wxDataViewRenderer( varianttype
, mode
, align
)
719 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
721 m_text
= value
.GetString();
726 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
731 bool wxDataViewTextRenderer::HasEditorCtrl() const
736 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
737 wxRect labelRect
, const wxVariant
&value
)
739 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
740 wxPoint(labelRect
.x
,labelRect
.y
),
741 wxSize(labelRect
.width
,labelRect
.height
) );
743 // select the text in the control an place the cursor at the end
744 ctrl
->SetInsertionPointEnd();
750 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
752 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
753 value
= text
->GetValue();
757 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
759 RenderText(m_text
, 0, rect
, dc
, state
);
763 wxSize
wxDataViewTextRenderer::GetSize() const
766 return GetTextExtent(m_text
);
768 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
771 // ---------------------------------------------------------
772 // wxDataViewBitmapRenderer
773 // ---------------------------------------------------------
775 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
777 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
778 wxDataViewCellMode mode
, int align
) :
779 wxDataViewRenderer( varianttype
, mode
, align
)
783 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
785 if (value
.GetType() == wxT("wxBitmap"))
787 if (value
.GetType() == wxT("wxIcon"))
793 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
798 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
801 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
802 else if (m_icon
.IsOk())
803 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
808 wxSize
wxDataViewBitmapRenderer::GetSize() const
811 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
812 else if (m_icon
.IsOk())
813 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
815 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
818 // ---------------------------------------------------------
819 // wxDataViewToggleRenderer
820 // ---------------------------------------------------------
822 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
824 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
825 wxDataViewCellMode mode
, int align
) :
826 wxDataViewRenderer( varianttype
, mode
, align
)
831 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
833 m_toggle
= value
.GetBool();
838 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
843 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
847 flags
|= wxCONTROL_CHECKED
;
848 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
849 GetEnabled() == false)
850 flags
|= wxCONTROL_DISABLED
;
852 // check boxes we draw must always have the same, standard size (if it's
853 // bigger than the cell size the checkbox will be truncated because the
854 // caller had set the clipping rectangle to prevent us from drawing outside
856 cell
.SetSize(GetSize());
858 wxRendererNative::Get().DrawCheckBox(
859 GetOwner()->GetOwner(),
867 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
868 const wxRect
& WXUNUSED(cell
),
869 wxDataViewModel
*model
,
870 const wxDataViewItem
& item
,
873 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
874 if (!wxRect(GetSize()).Contains(cursor
))
877 if (model
->IsEnabled(item
, col
))
879 model
->ChangeValue(!m_toggle
, item
, col
);
886 wxSize
wxDataViewToggleRenderer::GetSize() const
888 // the window parameter is not used by GetCheckBoxSize() so it's
890 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
893 // ---------------------------------------------------------
894 // wxDataViewProgressRenderer
895 // ---------------------------------------------------------
897 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
899 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
900 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
901 wxDataViewRenderer( varianttype
, mode
, align
)
907 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
909 m_value
= (long) value
;
911 if (m_value
< 0) m_value
= 0;
912 if (m_value
> 100) m_value
= 100;
917 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
919 value
= (long) m_value
;
924 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
926 // deflate the rect to leave a small border between bars in adjacent rows
927 wxRect bar
= rect
.Deflate(0, 1);
929 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
930 dc
->SetPen( *wxBLACK_PEN
);
931 dc
->DrawRectangle( bar
);
933 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
934 dc
->SetPen( *wxTRANSPARENT_PEN
);
936 const wxDataViewItemAttr
& attr
= GetAttr();
937 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
939 dc
->DrawRectangle( bar
);
944 wxSize
wxDataViewProgressRenderer::GetSize() const
946 return wxSize(40,12);
949 // ---------------------------------------------------------
950 // wxDataViewDateRenderer
951 // ---------------------------------------------------------
953 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
955 #if wxUSE_DATE_RENDERER_POPUP
957 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
960 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
961 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
962 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
967 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
968 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
969 sizer
->Add( m_cal
, 1, wxGROW
);
974 void OnCalendar( wxCalendarEvent
&event
);
976 wxCalendarCtrl
*m_cal
;
977 wxDataViewModel
*m_model
;
979 const wxDataViewItem
& m_item
;
982 virtual void OnDismiss()
987 DECLARE_EVENT_TABLE()
990 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
991 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
994 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
996 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1000 #endif // wxUSE_DATE_RENDERER_POPUP
1002 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1004 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1005 wxDataViewCellMode mode
, int align
) :
1006 wxDataViewRenderer( varianttype
, mode
, align
)
1010 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1012 m_date
= value
.GetDateTime();
1017 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1023 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1025 wxString tmp
= m_date
.FormatDate();
1026 RenderText( tmp
, 0, cell
, dc
, state
);
1030 wxSize
wxDataViewDateRenderer::GetSize() const
1032 return GetTextExtent(m_date
.FormatDate());
1035 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1036 wxDataViewModel
*model
,
1037 const wxDataViewItem
& item
,
1040 wxDateTime dtOld
= m_date
;
1042 #if wxUSE_DATE_RENDERER_POPUP
1043 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1044 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1045 wxPoint pos
= wxGetMousePosition();
1048 popup
->Popup( popup
->m_cal
);
1049 #else // !wxUSE_DATE_RENDERER_POPUP
1050 wxMessageBox(dtOld
.Format());
1051 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1056 // ---------------------------------------------------------
1057 // wxDataViewIconTextRenderer
1058 // ---------------------------------------------------------
1060 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1062 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1063 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1064 wxDataViewRenderer( varianttype
, mode
, align
)
1067 SetAlignment(align
);
1070 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1076 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1081 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1085 const wxIcon
& icon
= m_value
.GetIcon();
1088 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1089 xoffset
= icon
.GetWidth()+4;
1092 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1097 wxSize
wxDataViewIconTextRenderer::GetSize() const
1099 if (!m_value
.GetText().empty())
1101 wxSize size
= GetTextExtent(m_value
.GetText());
1103 if (m_value
.GetIcon().IsOk())
1104 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1107 return wxSize(80,20);
1110 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1112 wxDataViewIconText iconText
;
1115 wxString text
= iconText
.GetText();
1117 // adjust the label rect to take the width of the icon into account
1118 if (iconText
.GetIcon().IsOk())
1120 int w
= iconText
.GetIcon().GetWidth() + 4;
1122 labelRect
.width
-= w
;
1125 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1126 wxPoint(labelRect
.x
,labelRect
.y
),
1127 wxSize(labelRect
.width
,labelRect
.height
) );
1129 // select the text in the control an place the cursor at the end
1130 ctrl
->SetInsertionPointEnd();
1136 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1138 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1140 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1145 //-----------------------------------------------------------------------------
1146 // wxDataViewDropTarget
1147 //-----------------------------------------------------------------------------
1149 #if wxUSE_DRAG_AND_DROP
1151 class wxBitmapCanvas
: public wxWindow
1154 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1155 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1158 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1161 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1164 dc
.DrawBitmap( m_bitmap
, 0, 0);
1170 class wxDataViewDropSource
: public wxDropSource
1173 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1181 ~wxDataViewDropSource()
1186 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1188 wxPoint pos
= wxGetMousePosition();
1192 int liney
= m_win
->GetLineStart( m_row
);
1194 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1195 m_win
->ClientToScreen( &linex
, &liney
);
1196 m_dist_x
= pos
.x
- linex
;
1197 m_dist_y
= pos
.y
- liney
;
1200 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1202 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1203 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1205 wxFRAME_TOOL_WINDOW
|
1206 wxFRAME_FLOAT_ON_PARENT
|
1207 wxFRAME_NO_TASKBAR
|
1209 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1214 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1215 m_hint
->SetTransparent( 128 );
1221 wxDataViewMainWindow
*m_win
;
1224 int m_dist_x
,m_dist_y
;
1228 class wxDataViewDropTarget
: public wxDropTarget
1231 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1237 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1239 wxDataFormat format
= GetMatchingPair();
1240 if (format
== wxDF_INVALID
)
1242 return m_win
->OnDragOver( format
, x
, y
, def
);
1245 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1247 wxDataFormat format
= GetMatchingPair();
1248 if (format
== wxDF_INVALID
)
1250 return m_win
->OnDrop( format
, x
, y
);
1253 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1255 wxDataFormat format
= GetMatchingPair();
1256 if (format
== wxDF_INVALID
)
1260 return m_win
->OnData( format
, x
, y
, def
);
1263 virtual void OnLeave()
1264 { m_win
->OnLeave(); }
1266 wxDataViewMainWindow
*m_win
;
1269 #endif // wxUSE_DRAG_AND_DROP
1271 //-----------------------------------------------------------------------------
1272 // wxDataViewRenameTimer
1273 //-----------------------------------------------------------------------------
1275 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1280 void wxDataViewRenameTimer::Notify()
1282 m_owner
->OnRenameTimer();
1285 //-----------------------------------------------------------------------------
1286 // wxDataViewMainWindow
1287 //-----------------------------------------------------------------------------
1289 // The tree building helper, declared firstly
1290 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1291 wxDataViewTreeNode
* node
);
1293 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1295 if (row1
> row2
) return 1;
1296 if (row1
== row2
) return 0;
1300 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1302 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1303 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1304 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1305 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1306 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1307 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1310 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1311 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1312 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1313 m_selection( wxDataViewSelectionCmp
)
1318 m_lastOnSame
= false;
1319 m_renameTimer
= new wxDataViewRenameTimer( this );
1321 // TODO: user better initial values/nothing selected
1322 m_currentCol
= NULL
;
1325 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1327 #if wxUSE_DRAG_AND_DROP
1329 m_dragStart
= wxPoint(0,0);
1331 m_dragEnabled
= false;
1332 m_dropEnabled
= false;
1334 m_dropHintLine
= (unsigned int) -1;
1335 #endif // wxUSE_DRAG_AND_DROP
1337 m_lineLastClicked
= (unsigned int) -1;
1338 m_lineBeforeLastClicked
= (unsigned int) -1;
1339 m_lineSelectSingleOnUp
= (unsigned int) -1;
1343 SetBackgroundColour( *wxWHITE
);
1345 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1347 m_penRule
= wxPen(GetRuleColour());
1349 // compose a pen whichcan draw black lines
1350 // TODO: maybe there is something system colour to use
1351 m_penExpander
= wxPen(wxColour(0,0,0));
1353 m_root
= new wxDataViewTreeNode( NULL
);
1354 m_root
->SetHasChildren(true);
1356 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1358 m_underMouse
= NULL
;
1362 wxDataViewMainWindow::~wxDataViewMainWindow()
1365 delete m_renameTimer
;
1369 #if wxUSE_DRAG_AND_DROP
1370 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1372 m_dragFormat
= format
;
1373 m_dragEnabled
= format
!= wxDF_INVALID
;
1378 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1380 m_dropFormat
= format
;
1381 m_dropEnabled
= format
!= wxDF_INVALID
;
1384 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1389 void wxDataViewMainWindow::RemoveDropHint()
1394 RefreshRow( m_dropHintLine
);
1395 m_dropHintLine
= (unsigned int) -1;
1399 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1400 wxCoord y
, wxDragResult def
)
1404 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1405 unsigned int row
= GetLineAt( yy
);
1407 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1413 wxDataViewItem item
= GetItemByRow( row
);
1415 wxDataViewModel
*model
= GetOwner()->GetModel();
1417 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1418 event
.SetEventObject( m_owner
);
1419 event
.SetItem( item
);
1420 event
.SetModel( model
);
1421 event
.SetDataFormat( format
);
1422 if (!m_owner
->HandleWindowEvent( event
))
1428 if (!event
.IsAllowed())
1435 if (m_dropHint
&& (row
!= m_dropHintLine
))
1436 RefreshRow( m_dropHintLine
);
1438 m_dropHintLine
= row
;
1444 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1450 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1451 unsigned int row
= GetLineAt( yy
);
1453 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1456 wxDataViewItem item
= GetItemByRow( row
);
1458 wxDataViewModel
*model
= GetOwner()->GetModel();
1460 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1461 event
.SetEventObject( m_owner
);
1462 event
.SetItem( item
);
1463 event
.SetModel( model
);
1464 event
.SetDataFormat( format
);
1465 if (!m_owner
->HandleWindowEvent( event
))
1468 if (!event
.IsAllowed())
1474 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1479 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1480 unsigned int row
= GetLineAt( yy
);
1482 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1485 wxDataViewItem item
= GetItemByRow( row
);
1487 wxDataViewModel
*model
= GetOwner()->GetModel();
1489 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1491 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1492 event
.SetEventObject( m_owner
);
1493 event
.SetItem( item
);
1494 event
.SetModel( model
);
1495 event
.SetDataFormat( format
);
1496 event
.SetDataSize( obj
->GetSize() );
1497 event
.SetDataBuffer( obj
->GetData() );
1498 if (!m_owner
->HandleWindowEvent( event
))
1501 if (!event
.IsAllowed())
1507 void wxDataViewMainWindow::OnLeave()
1512 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1514 int height
= GetLineHeight( row
);
1516 unsigned int cols
= GetOwner()->GetColumnCount();
1518 for (col
= 0; col
< cols
; col
++)
1520 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1521 if (column
->IsHidden())
1522 continue; // skip it!
1523 width
+= column
->GetWidth();
1529 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1530 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1531 indent
= indent
+ m_lineHeight
;
1532 // try to use the m_lineHeight as the expander space
1534 if(!node
->HasChildren())
1539 wxBitmap
bitmap( width
, height
);
1540 wxMemoryDC
dc( bitmap
);
1541 dc
.SetFont( GetFont() );
1542 dc
.SetPen( *wxBLACK_PEN
);
1543 dc
.SetBrush( *wxWHITE_BRUSH
);
1544 dc
.DrawRectangle( 0,0,width
,height
);
1546 wxDataViewModel
*model
= m_owner
->GetModel();
1548 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1551 // TODO-RTL: last column for RTL support
1552 expander
= GetOwner()->GetColumnAt( 0 );
1553 GetOwner()->SetExpanderColumn(expander
);
1558 for (col
= 0; col
< cols
; col
++)
1560 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1561 wxDataViewRenderer
*cell
= column
->GetRenderer();
1563 if (column
->IsHidden())
1564 continue; // skip it!
1566 width
= column
->GetWidth();
1568 if (column
== expander
)
1571 wxDataViewItem item
= GetItemByRow( row
);
1572 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1574 wxRect
item_rect(x
, 0, width
, height
);
1575 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1577 // dc.SetClippingRegion( item_rect );
1578 cell
->WXCallRender(item_rect
, &dc
, 0);
1579 // dc.DestroyClippingRegion();
1587 #endif // wxUSE_DRAG_AND_DROP
1590 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1592 wxDataViewModel
*model
= GetOwner()->GetModel();
1593 wxAutoBufferedPaintDC
dc( this );
1596 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1597 dc
.SetPen( *wxTRANSPARENT_PEN
);
1598 dc
.DrawRectangle(GetClientSize());
1603 // No items to draw.
1608 GetOwner()->PrepareDC( dc
);
1609 dc
.SetFont( GetFont() );
1611 wxRect update
= GetUpdateRegion().GetBox();
1612 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1614 // compute which items needs to be redrawn
1615 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1616 unsigned int item_count
=
1617 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1618 (int)(GetRowCount( ) - item_start
));
1619 unsigned int item_last
= item_start
+ item_count
;
1621 // Send the event to wxDataViewCtrl itself.
1622 wxWindow
* const parent
= GetParent();
1623 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1624 cache_event
.SetEventObject(parent
);
1625 cache_event
.SetCache(item_start
, item_last
- 1);
1626 parent
->ProcessWindowEvent(cache_event
);
1628 // compute which columns needs to be redrawn
1629 unsigned int cols
= GetOwner()->GetColumnCount();
1632 // we assume that we have at least one column below and painting an
1633 // empty control is unnecessary anyhow
1637 unsigned int col_start
= 0;
1638 unsigned int x_start
;
1639 for (x_start
= 0; col_start
< cols
; col_start
++)
1641 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1642 if (col
->IsHidden())
1643 continue; // skip it!
1645 unsigned int w
= col
->GetWidth();
1646 if (x_start
+w
>= (unsigned int)update
.x
)
1652 unsigned int col_last
= col_start
;
1653 unsigned int x_last
= x_start
;
1654 for (; col_last
< cols
; col_last
++)
1656 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1657 if (col
->IsHidden())
1658 continue; // skip it!
1660 if (x_last
> (unsigned int)update
.GetRight())
1663 x_last
+= col
->GetWidth();
1666 // Draw horizontal rules if required
1667 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1669 dc
.SetPen(m_penRule
);
1670 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1672 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1674 int y
= GetLineStart( i
);
1675 dc
.DrawLine(x_start
, y
, x_last
, y
);
1679 // Draw vertical rules if required
1680 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1682 dc
.SetPen(m_penRule
);
1683 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1685 // NB: Vertical rules are drawn in the last pixel of a column so that
1686 // they align perfectly with native MSW wxHeaderCtrl as well as for
1687 // consistency with MSW native list control. There's no vertical
1688 // rule at the most-left side of the control.
1690 int x
= x_start
- 1;
1691 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1693 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1694 if (col
->IsHidden())
1695 continue; // skip it
1697 x
+= col
->GetWidth();
1699 dc
.DrawLine(x
, GetLineStart( item_start
),
1700 x
, GetLineStart( item_last
) );
1704 // redraw the background for the items which are selected/current
1705 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1707 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1708 if (selected
|| item
== m_currentRow
)
1710 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1711 if (item
== m_currentRow
)
1712 flags
|= wxCONTROL_CURRENT
;
1714 flags
|= wxCONTROL_FOCUSED
;
1716 wxRect
rect( x_start
, GetLineStart( item
),
1717 x_last
- x_start
, GetLineHeight( item
) );
1718 wxRendererNative::Get().DrawItemSelectionRect
1728 #if wxUSE_DRAG_AND_DROP
1731 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1732 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1733 dc
.SetPen( *wxBLACK_PEN
);
1734 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1735 dc
.DrawRectangle( rect
);
1737 #endif // wxUSE_DRAG_AND_DROP
1739 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1742 // TODO-RTL: last column for RTL support
1743 expander
= GetOwner()->GetColumnAt( 0 );
1744 GetOwner()->SetExpanderColumn(expander
);
1747 // redraw all cells for all rows which must be repainted and all columns
1749 cell_rect
.x
= x_start
;
1750 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1752 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1753 wxDataViewRenderer
*cell
= col
->GetRenderer();
1754 cell_rect
.width
= col
->GetWidth();
1756 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1757 continue; // skip it!
1759 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1761 // get the cell value and set it into the renderer
1762 wxDataViewTreeNode
*node
= NULL
;
1763 wxDataViewItem dataitem
;
1765 if (!IsVirtualList())
1767 node
= GetTreeNodeByRow(item
);
1771 dataitem
= node
->GetItem();
1773 // Skip all columns of "container" rows except the expander
1774 // column itself unless HasContainerColumns() overrides this.
1775 if ( col
!= GetOwner()->GetExpanderColumn() &&
1776 model
->IsContainer(dataitem
) &&
1777 !model
->HasContainerColumns(dataitem
) )
1782 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1785 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1788 cell_rect
.y
= GetLineStart( item
);
1789 cell_rect
.height
= GetLineHeight( item
);
1791 // deal with the expander
1793 if ((!IsList()) && (col
== expander
))
1795 // Calculate the indent first
1796 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1798 // we reserve m_lineHeight of horizontal space for the expander
1799 // but leave EXPANDER_MARGIN around the expander itself
1800 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1802 indent
+= m_lineHeight
;
1804 // draw expander if needed and visible
1805 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1807 dc
.SetPen( m_penExpander
);
1808 dc
.SetBrush( wxNullBrush
);
1810 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1811 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1812 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1814 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1817 if ( m_underMouse
== node
)
1818 flag
|= wxCONTROL_CURRENT
;
1819 if ( node
->IsOpen() )
1820 flag
|= wxCONTROL_EXPANDED
;
1822 // ensure that we don't overflow the cell (which might
1823 // happen if the column is very narrow)
1824 wxDCClipper
clip(dc
, cell_rect
);
1826 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1829 // force the expander column to left-center align
1830 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1832 if (node
&& !node
->HasChildren())
1834 // Yes, if the node does not have any child, it must be a leaf which
1835 // mean that it is a temporarily created by GetTreeNodeByRow
1839 wxRect item_rect
= cell_rect
;
1840 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1842 // account for the tree indent (harmless if we're not indented)
1843 item_rect
.x
+= indent
;
1844 item_rect
.width
-= indent
;
1846 if ( item_rect
.width
<= 0 )
1850 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1851 state
|= wxDATAVIEW_CELL_SELECTED
;
1853 // TODO: it would be much more efficient to create a clipping
1854 // region for the entire column being rendered (in the OnPaint
1855 // of wxDataViewMainWindow) instead of a single clip region for
1856 // each cell. However it would mean that each renderer should
1857 // respect the given wxRect's top & bottom coords, eventually
1858 // violating only the left & right coords - however the user can
1859 // make its own renderer and thus we cannot be sure of that.
1860 wxDCClipper
clip(dc
, item_rect
);
1862 cell
->WXCallRender(item_rect
, &dc
, state
);
1865 cell_rect
.x
+= cell_rect
.width
;
1869 void wxDataViewMainWindow::OnRenameTimer()
1871 // We have to call this here because changes may just have
1872 // been made and no screen update taken place.
1875 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1876 // (needs to be tested!)
1880 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1882 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1884 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1887 //-----------------------------------------------------------------------------
1888 // Helper class for do operation on the tree node
1889 //-----------------------------------------------------------------------------
1894 virtual ~DoJob() { }
1896 // The return value control how the tree-walker tranverse the tree
1897 // 0: Job done, stop tranverse and return
1898 // 1: Ignore the current node's subtree and continue
1899 // 2: Job not done, continue
1900 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1901 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1902 virtual int operator() ( void * n
) = 0;
1905 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1910 switch( func( node
) )
1921 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
1922 const wxDataViewTreeLeaves
& leaves
= node
->GetChildren();
1924 int len_nodes
= nodes
.GetCount();
1925 int len
= leaves
.GetCount();
1926 int i
= 0, nodes_i
= 0;
1928 for(; i
< len
; i
++ )
1930 void * n
= leaves
[i
];
1931 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1933 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1936 if( Walker( nd
, func
) )
1955 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1957 GetOwner()->InvalidateColBestWidths();
1959 if (IsVirtualList())
1961 wxDataViewVirtualListModel
*list_model
=
1962 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1963 m_count
= list_model
->GetCount();
1970 wxDataViewTreeNode
* node
;
1971 node
= FindNode(parent
);
1976 node
->SetHasChildren( true );
1978 if( g_model
->IsContainer( item
) )
1980 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1981 newnode
->SetItem(item
);
1982 newnode
->SetHasChildren( true );
1983 node
->AddNode( newnode
);
1986 node
->AddLeaf( item
.GetID() );
1988 node
->ChangeSubTreeCount(1);
1996 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1998 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1999 const wxDataViewItem
& item
)
2001 GetOwner()->InvalidateColBestWidths();
2003 if (IsVirtualList())
2005 wxDataViewVirtualListModel
*list_model
=
2006 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
2007 m_count
= list_model
->GetCount();
2009 // TODO: why empty the entire selection?
2010 m_selection
.Empty();
2012 else // general case
2014 wxDataViewTreeNode
* node
= FindNode(parent
);
2016 // Notice that it is possible that the item being deleted is not in the
2017 // tree at all, for example we could be deleting a never shown (because
2018 // collapsed) item in a tree model. So it's not an error if we don't know
2019 // about this item, just return without doing anything then.
2020 if ( !node
|| node
->GetChildren().Index(item
.GetID()) == wxNOT_FOUND
)
2024 node
->GetChildren().Remove( item
.GetID() );
2025 // Manipolate selection
2026 if( m_selection
.GetCount() > 1 )
2028 m_selection
.Empty();
2030 bool isContainer
= false;
2031 wxDataViewTreeNodes nds
= node
->GetNodes();
2032 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2034 if (nds
[i
]->GetItem() == item
)
2042 wxDataViewTreeNode
* n
= NULL
;
2043 wxDataViewTreeNodes nodes
= node
->GetNodes();
2044 int len
= nodes
.GetCount();
2045 for( int i
= 0; i
< len
; i
++)
2047 if( nodes
[i
]->GetItem() == item
)
2054 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2056 node
->GetNodes().Remove( n
);
2057 sub
-= n
->GetSubTreeCount();
2058 ::DestroyTreeHelper(n
);
2060 // Make the row number invalid and get a new valid one when user call GetRowCount
2062 node
->ChangeSubTreeCount(sub
);
2065 // Change the current row to the last row if the current exceed the max row number
2066 if( m_currentRow
> GetRowCount() )
2067 ChangeCurrentRow(m_count
- 1);
2074 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2076 GetOwner()->InvalidateColBestWidths();
2082 wxWindow
*parent
= GetParent();
2083 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2084 le
.SetEventObject(parent
);
2085 le
.SetModel(GetOwner()->GetModel());
2087 parent
->GetEventHandler()->ProcessEvent(le
);
2092 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2094 int view_column
= -1;
2095 unsigned int n_col
= m_owner
->GetColumnCount();
2096 for (unsigned i
= 0; i
< n_col
; i
++)
2098 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2099 if (column
->GetModelColumn() == model_column
)
2101 view_column
= (int) i
;
2105 if (view_column
== -1)
2108 GetOwner()->InvalidateColBestWidth(view_column
);
2110 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2111 /*#define MAX_VIRTUAL_WIDTH 100000
2113 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2114 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2115 Refresh( true, &rect );
2123 wxWindow
*parent
= GetParent();
2124 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2125 le
.SetEventObject(parent
);
2126 le
.SetModel(GetOwner()->GetModel());
2128 le
.SetColumn(view_column
);
2129 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2130 parent
->GetEventHandler()->ProcessEvent(le
);
2135 bool wxDataViewMainWindow::Cleared()
2137 GetOwner()->InvalidateColBestWidths();
2140 m_selection
.Clear();
2143 BuildTree( GetOwner()->GetModel() );
2150 void wxDataViewMainWindow::UpdateDisplay()
2153 m_underMouse
= NULL
;
2156 void wxDataViewMainWindow::OnInternalIdle()
2158 wxWindow::OnInternalIdle();
2162 RecalculateDisplay();
2167 void wxDataViewMainWindow::RecalculateDisplay()
2169 wxDataViewModel
*model
= GetOwner()->GetModel();
2176 int width
= GetEndOfLastCol();
2177 int height
= GetLineStart( GetRowCount() );
2179 SetVirtualSize( width
, height
);
2180 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2185 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2187 m_underMouse
= NULL
;
2189 wxWindow::ScrollWindow( dx
, dy
, rect
);
2191 if (GetOwner()->m_headerArea
)
2192 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2195 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2197 m_underMouse
= NULL
;
2200 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2201 int sy
= GetLineStart( rows
)/y
;
2205 wxRect rect
= GetClientRect();
2209 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2210 for (x_start
= 0; colnum
< column
; colnum
++)
2212 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2213 if (col
->IsHidden())
2214 continue; // skip it!
2216 w
= col
->GetWidth();
2220 int x_end
= x_start
+ w
;
2221 xe
= xx
+ rect
.width
;
2224 sx
= ( xx
+ x_end
- xe
)/x
;
2231 m_owner
->Scroll( sx
, sy
);
2234 int wxDataViewMainWindow::GetCountPerPage() const
2236 wxSize size
= GetClientSize();
2237 return size
.y
/ m_lineHeight
;
2240 int wxDataViewMainWindow::GetEndOfLastCol() const
2244 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2246 const wxDataViewColumn
*c
=
2247 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2250 width
+= c
->GetWidth();
2255 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2259 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2261 return GetLineAt( y
);
2264 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2266 wxSize client_size
= GetClientSize();
2267 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2268 &client_size
.x
, &client_size
.y
);
2270 // we should deal with the pixel here
2271 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2273 return wxMin( GetRowCount()-1, row
);
2276 unsigned int wxDataViewMainWindow::GetRowCount()
2278 if ( m_count
== -1 )
2280 m_count
= RecalculateCount();
2286 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2293 void wxDataViewMainWindow::SelectAllRows( bool on
)
2300 m_selection
.Clear();
2301 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2302 m_selection
.Add( i
);
2307 unsigned int first_visible
= GetFirstVisibleRow();
2308 unsigned int last_visible
= GetLastVisibleRow();
2310 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2312 unsigned int row
= m_selection
[i
];
2313 if ((row
>= first_visible
) && (row
<= last_visible
))
2316 m_selection
.Clear();
2320 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2322 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2326 m_selection
.Add( row
);
2334 m_selection
.Remove( row
);
2340 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2344 unsigned int tmp
= from
;
2350 for (i
= from
; i
<= to
; i
++)
2352 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2355 m_selection
.Add( i
);
2360 m_selection
.Remove( i
);
2363 RefreshRows( from
, to
);
2366 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2368 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2370 int n
= aSelections
[i
];
2372 m_selection
.Add( n
);
2377 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2379 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2380 m_selection
.Add( row
);
2382 m_selection
.Remove( row
);
2386 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2388 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2391 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2393 wxWindow
*parent
= GetParent();
2394 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2396 le
.SetEventObject(parent
);
2397 le
.SetModel(GetOwner()->GetModel());
2400 parent
->GetEventHandler()->ProcessEvent(le
);
2403 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2405 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2406 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2408 wxSize client_size
= GetClientSize();
2409 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2410 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2411 if (intersect_rect
.width
> 0)
2412 Refresh( true, &intersect_rect
);
2415 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2419 unsigned int tmp
= to
;
2424 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2425 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2427 wxSize client_size
= GetClientSize();
2428 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2429 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2430 if (intersect_rect
.width
> 0)
2431 Refresh( true, &intersect_rect
);
2434 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2436 wxSize client_size
= GetClientSize();
2437 int start
= GetLineStart( firstRow
);
2438 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2439 if (start
> client_size
.y
) return;
2441 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2443 Refresh( true, &rect
);
2446 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2448 wxCHECK_RET( newCurrent
< GetRowCount(),
2449 wxT("invalid item index in OnArrowChar()") );
2451 // if there is no selection, we cannot move it anywhere
2452 if (!HasCurrentRow())
2455 unsigned int oldCurrent
= m_currentRow
;
2457 // in single selection we just ignore Shift as we can't select several
2459 if ( event
.ShiftDown() && !IsSingleSel() )
2461 RefreshRow( oldCurrent
);
2463 ChangeCurrentRow( newCurrent
);
2465 // select all the items between the old and the new one
2466 if ( oldCurrent
> newCurrent
)
2468 newCurrent
= oldCurrent
;
2469 oldCurrent
= m_currentRow
;
2472 SelectRows( oldCurrent
, newCurrent
, true );
2473 if (oldCurrent
!=newCurrent
)
2474 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2478 RefreshRow( oldCurrent
);
2480 // all previously selected items are unselected unless ctrl is held
2481 if ( !event
.ControlDown() )
2482 SelectAllRows(false);
2484 ChangeCurrentRow( newCurrent
);
2486 if ( !event
.ControlDown() )
2488 SelectRow( m_currentRow
, true );
2489 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2492 RefreshRow( m_currentRow
);
2495 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2498 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2502 rect
.y
= GetLineStart( row
);
2503 rect
.width
= GetEndOfLastCol();
2504 rect
.height
= GetLineHeight( row
);
2509 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2511 const wxDataViewModel
*model
= GetOwner()->GetModel();
2513 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2515 // TODO make more efficient
2520 for (r
= 0; r
< row
; r
++)
2522 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2523 if (!node
) return start
;
2525 wxDataViewItem item
= node
->GetItem();
2527 if (node
&& !node
->HasChildren())
2529 // Yes, if the node does not have any child, it must be a leaf which
2530 // mean that it is a temporarily created by GetTreeNodeByRow
2534 unsigned int cols
= GetOwner()->GetColumnCount();
2536 int height
= m_lineHeight
;
2537 for (col
= 0; col
< cols
; col
++)
2539 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2540 if (column
->IsHidden())
2541 continue; // skip it!
2544 model
->IsContainer(item
) &&
2545 !model
->HasContainerColumns(item
))
2546 continue; // skip it!
2548 wxDataViewRenderer
*renderer
=
2549 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2550 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2552 height
= wxMax( height
, renderer
->GetSize().y
);
2562 return row
* m_lineHeight
;
2566 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2568 const wxDataViewModel
*model
= GetOwner()->GetModel();
2570 // check for the easy case first
2571 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2572 return y
/ m_lineHeight
;
2574 // TODO make more efficient
2575 unsigned int row
= 0;
2576 unsigned int yy
= 0;
2579 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2582 // not really correct...
2583 return row
+ ((y
-yy
) / m_lineHeight
);
2586 wxDataViewItem item
= node
->GetItem();
2588 if (node
&& !node
->HasChildren())
2590 // Yes, if the node does not have any child, it must be a leaf which
2591 // mean that it is a temporarily created by GetTreeNodeByRow
2595 unsigned int cols
= GetOwner()->GetColumnCount();
2597 int height
= m_lineHeight
;
2598 for (col
= 0; col
< cols
; col
++)
2600 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2601 if (column
->IsHidden())
2602 continue; // skip it!
2605 model
->IsContainer(item
) &&
2606 !model
->HasContainerColumns(item
))
2607 continue; // skip it!
2609 wxDataViewRenderer
*renderer
=
2610 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2611 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2613 height
= wxMax( height
, renderer
->GetSize().y
);
2624 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2626 const wxDataViewModel
*model
= GetOwner()->GetModel();
2628 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2630 wxASSERT( !IsVirtualList() );
2632 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2633 // wxASSERT( node );
2634 if (!node
) return m_lineHeight
;
2636 wxDataViewItem item
= node
->GetItem();
2638 if (node
&& !node
->HasChildren())
2640 // Yes, if the node does not have any child, it must be a leaf which
2641 // mean that it is a temporarily created by GetTreeNodeByRow
2645 int height
= m_lineHeight
;
2647 unsigned int cols
= GetOwner()->GetColumnCount();
2649 for (col
= 0; col
< cols
; col
++)
2651 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2652 if (column
->IsHidden())
2653 continue; // skip it!
2656 model
->IsContainer(item
) &&
2657 !model
->HasContainerColumns(item
))
2658 continue; // skip it!
2660 wxDataViewRenderer
*renderer
=
2661 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2662 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2664 height
= wxMax( height
, renderer
->GetSize().y
);
2671 return m_lineHeight
;
2675 class RowToItemJob
: public DoJob
2678 RowToItemJob( unsigned int row
, int current
)
2679 { this->row
= row
; this->current
= current
; }
2680 virtual ~RowToItemJob() {}
2682 virtual int operator() ( wxDataViewTreeNode
* node
)
2685 if( current
== static_cast<int>(row
))
2687 ret
= node
->GetItem();
2691 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2693 current
+= node
->GetSubTreeCount();
2698 // If the current has no child node, we can find the desired item of the row
2700 // This if can speed up finding in some case, and will has a very good effect
2701 // when it comes to list view
2702 if( node
->GetNodes().GetCount() == 0)
2704 int index
= static_cast<int>(row
) - current
- 1;
2705 ret
= node
->GetChildren().Item( index
);
2712 virtual int operator() ( void * n
)
2715 if( current
== static_cast<int>(row
))
2717 ret
= wxDataViewItem( n
);
2723 wxDataViewItem
GetResult() const
2732 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2734 if (IsVirtualList())
2736 return wxDataViewItem( wxUIntToPtr(row
+1) );
2740 RowToItemJob
job( row
, -2 );
2741 Walker( m_root
, job
);
2742 return job
.GetResult();
2746 class RowToTreeNodeJob
: public DoJob
2749 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2752 this->current
= current
;
2756 virtual ~RowToTreeNodeJob(){ }
2758 virtual int operator() ( wxDataViewTreeNode
* node
)
2761 if( current
== static_cast<int>(row
))
2767 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2769 current
+= node
->GetSubTreeCount();
2776 // If the current node has no children, we can find the desired item of the
2777 // row number directly.
2778 // This if can speed up finding in some case, and will have a very good
2779 // effect for list views.
2780 if( node
->GetNodes().GetCount() == 0)
2782 int index
= static_cast<int>(row
) - current
- 1;
2783 void * n
= node
->GetChildren().Item( index
);
2784 ret
= new wxDataViewTreeNode( parent
);
2785 ret
->SetItem( wxDataViewItem( n
));
2786 ret
->SetHasChildren(false);
2793 virtual int operator() ( void * n
)
2796 if( current
== static_cast<int>(row
))
2798 ret
= new wxDataViewTreeNode( parent
);
2799 ret
->SetItem( wxDataViewItem( n
));
2800 ret
->SetHasChildren(false);
2807 wxDataViewTreeNode
* GetResult() const
2813 wxDataViewTreeNode
* ret
;
2814 wxDataViewTreeNode
* parent
;
2817 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2819 wxASSERT( !IsVirtualList() );
2821 RowToTreeNodeJob
job( row
, -2, m_root
);
2822 Walker( m_root
, job
);
2823 return job
.GetResult();
2826 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2827 const wxDataViewItem
& item
)
2829 wxWindow
*parent
= GetParent();
2830 wxDataViewEvent
le(type
, parent
->GetId());
2832 le
.SetEventObject(parent
);
2833 le
.SetModel(GetOwner()->GetModel());
2836 parent
->GetEventHandler()->ProcessEvent(le
);
2840 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2845 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2849 if (!node
->HasChildren())
2855 return node
->IsOpen();
2858 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2863 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2867 if (!node
->HasChildren())
2876 void wxDataViewMainWindow::Expand( unsigned int row
)
2881 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2885 if (!node
->HasChildren())
2891 if (!node
->IsOpen())
2894 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2896 // Check if the user prevent expanding
2897 if( e
.GetSkipped() )
2902 // build the children of current node
2903 if( node
->GetChildrenNumber() == 0 )
2906 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2909 // By expanding the node all row indices that are currently in the selection list
2910 // and are greater than our node have become invalid. So we have to correct that now.
2911 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2912 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2914 const unsigned testRow
= m_selection
[i
];
2915 // all rows above us are not affected, so skip them
2919 m_selection
[i
] += rowAdjustment
;
2922 if(m_currentRow
> row
)
2923 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2927 // Send the expanded event
2928 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2932 void wxDataViewMainWindow::Collapse(unsigned int row
)
2937 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2941 if (!node
->HasChildren())
2950 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2951 if( e
.GetSkipped() )
2954 // Find out if there are selected items below the current node.
2955 bool selectCollapsingRow
= false;
2956 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2957 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2958 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2960 const unsigned testRow
= m_selection
[i
];
2961 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2963 selectCollapsingRow
= true;
2964 // get out as soon as we have found a node that is selected
2971 // If the node to be closed has selected items the user won't see those any longer.
2972 // We select the collapsing node in this case.
2973 if(selectCollapsingRow
)
2975 SelectAllRows(false);
2976 ChangeCurrentRow(row
);
2977 SelectRow(row
, true);
2978 SendSelectionChangedEvent(GetItemByRow(row
));
2982 // if there were no selected items below our node we still need to "fix" the
2983 // selection list to adjust for the changing of the row indices.
2984 // We actually do the opposite of what we are doing in Expand().
2985 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2987 const unsigned testRow
= m_selection
[i
];
2988 // all rows above us are not affected, so skip them
2992 m_selection
[i
] -= rowAdjustment
;
2995 // if the "current row" is being collapsed away we change it to the current row ;-)
2996 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2997 ChangeCurrentRow(row
);
2998 else if(m_currentRow
> row
)
2999 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3004 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3008 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3010 const wxDataViewModel
* model
= GetOwner()->GetModel();
3017 // Compose the parent-chain for the item we are looking for
3018 wxVector
<wxDataViewItem
> parentChain
;
3019 wxDataViewItem
it( item
);
3022 parentChain
.push_back(it
);
3023 it
= model
->GetParent(it
);
3026 // Find the item along the parent-chain.
3027 // This algorithm is designed to speed up the node-finding method
3028 wxDataViewTreeNode
* node
= m_root
;
3029 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3031 if( node
->HasChildren() )
3033 if( node
->GetChildrenNumber() == 0 )
3036 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3039 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3042 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3044 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3045 if (currentNode
->GetItem() == parentChain
[iter
])
3047 if (currentNode
->GetItem() == item
)
3067 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3068 wxDataViewColumn
* &column
)
3070 wxDataViewColumn
*col
= NULL
;
3071 unsigned int cols
= GetOwner()->GetColumnCount();
3072 unsigned int colnum
= 0;
3074 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3075 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3077 col
= GetOwner()->GetColumnAt(colnum
);
3078 if (col
->IsHidden())
3079 continue; // skip it!
3081 unsigned int w
= col
->GetWidth();
3082 if (x_start
+w
>= (unsigned int)x
)
3089 item
= GetItemByRow( GetLineAt( y
) );
3092 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3093 const wxDataViewColumn
* column
)
3098 unsigned int cols
= GetOwner()->GetColumnCount();
3099 // If column is null the loop will compute the combined width of all columns.
3100 // Otherwise, it will compute the x position of the column we are looking for.
3101 for (unsigned int i
= 0; i
< cols
; i
++)
3103 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3108 if (col
->IsHidden())
3109 continue; // skip it!
3111 xpos
+= col
->GetWidth();
3112 width
+= col
->GetWidth();
3117 // If we have a column, we need can get its width directly.
3118 if(column
->IsHidden())
3121 width
= column
->GetWidth();
3126 // If we have no column, we reset the x position back to zero.
3130 // we have to take an expander column into account and compute its indentation
3131 // to get the correct x position where the actual text is
3133 int row
= GetRowByItem(item
);
3134 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3136 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3137 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3138 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3140 if(!node
->HasChildren())
3144 wxRect
itemRect( xpos
+ indent
,
3145 GetLineStart( row
),
3147 GetLineHeight( row
) );
3149 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3150 &itemRect
.x
, &itemRect
.y
);
3155 int wxDataViewMainWindow::RecalculateCount()
3157 if (IsVirtualList())
3159 wxDataViewVirtualListModel
*list_model
=
3160 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3162 return list_model
->GetCount();
3166 return m_root
->GetSubTreeCount();
3170 class ItemToRowJob
: public DoJob
3173 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3180 // Maybe binary search will help to speed up this process
3181 virtual int operator() ( wxDataViewTreeNode
* node
)
3184 if( node
->GetItem() == item
)
3189 if( node
->GetItem() == *m_iter
)
3196 ret
+= node
->GetSubTreeCount();
3202 virtual int operator() ( void * n
)
3205 if( n
== item
.GetID() )
3210 // the row number is begin from zero
3211 int GetResult() const
3215 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3216 wxDataViewItem item
;
3221 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3223 const wxDataViewModel
* model
= GetOwner()->GetModel();
3227 if (IsVirtualList())
3229 return wxPtrToUInt( item
.GetID() ) -1;
3236 // Compose the parent-chain of the item we are looking for
3237 wxVector
<wxDataViewItem
> parentChain
;
3238 wxDataViewItem
it( item
);
3241 parentChain
.push_back(it
);
3242 it
= model
->GetParent(it
);
3245 // add an 'invalid' item to represent our 'invisible' root node
3246 parentChain
.push_back(wxDataViewItem());
3248 // the parent chain was created by adding the deepest parent first.
3249 // so if we want to start at the root node, we have to iterate backwards through the vector
3250 ItemToRowJob
job( item
, parentChain
.rbegin() );
3251 Walker( m_root
, job
);
3252 return job
.GetResult();
3256 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3257 wxDataViewTreeNode
* node
)
3259 if( !model
->IsContainer( item
) )
3262 wxDataViewItemArray children
;
3263 unsigned int num
= model
->GetChildren( item
, children
);
3265 unsigned int index
= 0;
3266 while( index
< num
)
3268 if( model
->IsContainer( children
[index
] ) )
3270 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3271 n
->SetItem(children
[index
]);
3272 n
->SetHasChildren( true );
3277 node
->AddLeaf( children
[index
].GetID() );
3281 node
->SetSubTreeCount( num
);
3282 wxDataViewTreeNode
* n
= node
->GetParent();
3284 n
->ChangeSubTreeCount(num
);
3288 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3292 if (GetOwner()->GetModel()->IsVirtualListModel())
3298 m_root
= new wxDataViewTreeNode( NULL
);
3299 m_root
->SetHasChildren(true);
3301 // First we define a invalid item to fetch the top-level elements
3302 wxDataViewItem item
;
3304 BuildTreeHelper( model
, item
, m_root
);
3308 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3310 if( node
->GetNodeNumber() != 0 )
3312 int len
= node
->GetNodeNumber();
3313 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3314 for (int i
= 0; i
< len
; i
++)
3315 DestroyTreeHelper(nodes
[i
]);
3320 void wxDataViewMainWindow::DestroyTree()
3322 if (!IsVirtualList())
3324 ::DestroyTreeHelper(m_root
);
3330 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3332 wxWindow
* const parent
= GetParent();
3334 // propagate the char event upwards
3335 wxKeyEvent
eventForParent(event
);
3336 eventForParent
.SetEventObject(parent
);
3337 if ( parent
->ProcessWindowEvent(eventForParent
) )
3340 if ( parent
->HandleAsNavigationKey(event
) )
3343 // no item -> nothing to do
3344 if (!HasCurrentRow())
3350 // don't use m_linesPerPage directly as it might not be computed yet
3351 const int pageSize
= GetCountPerPage();
3352 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3354 switch ( event
.GetKeyCode() )
3358 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3360 le
.SetItem( GetItemByRow(m_currentRow
) );
3361 le
.SetEventObject(parent
);
3362 le
.SetModel(GetOwner()->GetModel());
3364 parent
->GetEventHandler()->ProcessEvent(le
);
3369 if ( m_currentRow
> 0 )
3370 OnArrowChar( m_currentRow
- 1, event
);
3374 if ( m_currentRow
+ 1 < GetRowCount() )
3375 OnArrowChar( m_currentRow
+ 1, event
);
3377 // Add the process for tree expanding/collapsing
3383 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3387 if (node
->HasChildren() && node
->IsOpen())
3389 Collapse(m_currentRow
);
3391 else // if the node is already closed we move the selection to its parent
3393 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3395 if(!node
->HasChildren())
3400 int parent
= GetRowByItem( parent_node
->GetItem() );
3403 unsigned int row
= m_currentRow
;
3404 SelectRow( row
, false);
3405 SelectRow( parent
, true );
3406 ChangeCurrentRow( parent
);
3407 GetOwner()->EnsureVisible( parent
, -1 );
3408 SendSelectionChangedEvent( parent_node
->GetItem() );
3416 if (!IsExpanded( m_currentRow
))
3417 Expand( m_currentRow
);
3420 unsigned int row
= m_currentRow
;
3421 SelectRow( row
, false );
3422 SelectRow( row
+ 1, true );
3423 ChangeCurrentRow( row
+ 1 );
3424 GetOwner()->EnsureVisible( row
+ 1, -1 );
3425 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3432 OnArrowChar( GetRowCount() - 1, event
);
3437 OnArrowChar( 0, event
);
3442 int steps
= pageSize
- 1;
3443 int index
= m_currentRow
- steps
;
3447 OnArrowChar( index
, event
);
3453 int steps
= pageSize
- 1;
3454 unsigned int index
= m_currentRow
+ steps
;
3455 unsigned int count
= GetRowCount();
3456 if ( index
>= count
)
3459 OnArrowChar( index
, event
);
3465 if(m_selection
.size() == 1)
3467 // TODO: we need to revise that when we have a concept for a 'current column'
3468 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3478 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3480 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3482 // let the base handle mouse wheel events.
3487 // set the focus to ourself if any of the mouse buttons are pressed
3488 if(event
.ButtonDown() && !HasFocus())
3491 int x
= event
.GetX();
3492 int y
= event
.GetY();
3493 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3494 wxDataViewColumn
*col
= NULL
;
3497 unsigned int cols
= GetOwner()->GetColumnCount();
3499 for (i
= 0; i
< cols
; i
++)
3501 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3503 continue; // skip it!
3505 if (x
< xpos
+ c
->GetWidth())
3510 xpos
+= c
->GetWidth();
3518 wxDataViewRenderer
*cell
= col
->GetRenderer();
3519 unsigned int current
= GetLineAt( y
);
3520 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3522 // Unselect all if below the last row ?
3527 // Test whether the mouse is hovered on the tree item button
3528 bool hoverOverExpander
= false;
3529 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3531 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3532 if( node
!=NULL
&& node
->HasChildren() )
3534 int indent
= node
->GetIndentLevel();
3535 indent
= GetOwner()->GetIndent()*indent
;
3537 // we make the rectangle we are looking in a bit bigger than the actual
3538 // visual expander so the user can hit that little thing reliably
3539 wxRect
rect( xpos
+ indent
,
3540 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3541 m_lineHeight
, m_lineHeight
);
3542 if( rect
.Contains(x
, y
) )
3544 // So the mouse is over the expander
3545 hoverOverExpander
= true;
3546 if (m_underMouse
&& m_underMouse
!= node
)
3548 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3549 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3551 if (m_underMouse
!= node
)
3553 // wxLogMessage("Do the row: %d", current);
3554 RefreshRow(current
);
3556 m_underMouse
= node
;
3559 if (node
!=NULL
&& !node
->HasChildren())
3562 if (!hoverOverExpander
)
3564 if (m_underMouse
!= NULL
)
3566 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3567 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3568 m_underMouse
= NULL
;
3572 wxDataViewModel
*model
= GetOwner()->GetModel();
3574 #if wxUSE_DRAG_AND_DROP
3575 if (event
.Dragging())
3577 if (m_dragCount
== 0)
3579 // we have to report the raw, physical coords as we want to be
3580 // able to call HitTest(event.m_pointDrag) from the user code to
3581 // get the item being dragged
3582 m_dragStart
= event
.GetPosition();
3587 if (m_dragCount
!= 3)
3590 if (event
.LeftIsDown())
3592 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3593 &m_dragStart
.x
, &m_dragStart
.y
);
3594 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3595 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3597 // Notify cell about drag
3598 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3599 event
.SetEventObject( m_owner
);
3600 event
.SetItem( item
);
3601 event
.SetModel( model
);
3602 if (!m_owner
->HandleWindowEvent( event
))
3605 if (!event
.IsAllowed())
3608 wxDataObject
*obj
= event
.GetDataObject();
3612 wxDataViewDropSource
drag( this, drag_item_row
);
3613 drag
.SetData( *obj
);
3614 /* wxDragResult res = */ drag
.DoDragDrop();
3623 #endif // wxUSE_DRAG_AND_DROP
3625 bool simulateClick
= false;
3627 if (event
.ButtonDClick())
3629 m_renameTimer
->Stop();
3630 m_lastOnSame
= false;
3633 wxDataViewItem item
= GetItemByRow(current
);
3634 bool ignore_other_columns
=
3635 ((GetOwner()->GetExpanderColumn() != col
) &&
3636 (model
->IsContainer(item
)) &&
3637 (!model
->HasContainerColumns(item
)));
3639 if (event
.LeftDClick())
3641 if(hoverOverExpander
)
3643 // a double click on the expander will be converted into a "simulated" normal click
3644 simulateClick
= true;
3646 else if ( current
== m_lineLastClicked
)
3648 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3650 const unsigned colIdx
= col
->GetModelColumn();
3652 cell
->PrepareForItem(model
, item
, colIdx
);
3654 wxRect
cell_rect( xpos
, GetLineStart( current
),
3655 col
->GetWidth(), GetLineHeight( current
) );
3656 cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3660 wxWindow
*parent
= GetParent();
3661 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3663 le
.SetColumn( col
->GetModelColumn() );
3664 le
.SetDataViewColumn( col
);
3665 le
.SetEventObject(parent
);
3666 le
.SetModel(GetOwner()->GetModel());
3668 parent
->GetEventHandler()->ProcessEvent(le
);
3674 // The first click was on another item, so don't interpret this as
3675 // a double click, but as a simple click instead
3676 simulateClick
= true;
3680 if (event
.LeftUp() && !hoverOverExpander
)
3682 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3684 // select single line
3685 SelectAllRows( false );
3686 SelectRow( m_lineSelectSingleOnUp
, true );
3687 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3690 // If the user click the expander, we do not do editing even if the column
3691 // with expander are editable
3692 if (m_lastOnSame
&& !ignore_other_columns
)
3694 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3695 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3697 m_renameTimer
->Start( 100, true );
3701 m_lastOnSame
= false;
3702 m_lineSelectSingleOnUp
= (unsigned int)-1;
3704 else if(!event
.LeftUp())
3706 // This is necessary, because after a DnD operation in
3707 // from and to ourself, the up event is swallowed by the
3708 // DnD code. So on next non-up event (which means here and
3709 // now) m_lineSelectSingleOnUp should be reset.
3710 m_lineSelectSingleOnUp
= (unsigned int)-1;
3713 if (event
.RightDown())
3715 m_lineBeforeLastClicked
= m_lineLastClicked
;
3716 m_lineLastClicked
= current
;
3718 // If the item is already selected, do not update the selection.
3719 // Multi-selections should not be cleared if a selected item is clicked.
3720 if (!IsRowSelected(current
))
3722 SelectAllRows(false);
3723 const unsigned oldCurrent
= m_currentRow
;
3724 ChangeCurrentRow(current
);
3725 SelectRow(m_currentRow
,true);
3726 RefreshRow(oldCurrent
);
3727 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3730 else if (event
.RightUp())
3733 model
->GetValue( value
, item
, col
->GetModelColumn() );
3734 wxWindow
*parent
= GetParent();
3735 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3737 le
.SetColumn( col
->GetModelColumn() );
3738 le
.SetDataViewColumn( col
);
3739 le
.SetEventObject(parent
);
3740 le
.SetModel(GetOwner()->GetModel());
3742 parent
->GetEventHandler()->ProcessEvent(le
);
3744 else if (event
.MiddleDown())
3748 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3750 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3752 // hoverOverExpander being true tells us that our node must be
3753 // valid and have children.
3754 // So we don't need any extra checks.
3755 if( node
->IsOpen() )
3760 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3762 m_lineBeforeLastClicked
= m_lineLastClicked
;
3763 m_lineLastClicked
= current
;
3765 unsigned int oldCurrentRow
= m_currentRow
;
3766 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3768 bool cmdModifierDown
= event
.CmdDown();
3769 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3771 if ( IsSingleSel() || !IsRowSelected(current
) )
3773 SelectAllRows( false );
3774 ChangeCurrentRow(current
);
3775 SelectRow(m_currentRow
,true);
3776 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3778 else // multi sel & current is highlighted & no mod keys
3780 m_lineSelectSingleOnUp
= current
;
3781 ChangeCurrentRow(current
); // change focus
3784 else // multi sel & either ctrl or shift is down
3786 if (cmdModifierDown
)
3788 ChangeCurrentRow(current
);
3789 ReverseRowSelection(m_currentRow
);
3790 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3792 else if (event
.ShiftDown())
3794 ChangeCurrentRow(current
);
3796 unsigned int lineFrom
= oldCurrentRow
,
3799 if ( lineTo
< lineFrom
)
3802 lineFrom
= m_currentRow
;
3805 SelectRows(lineFrom
, lineTo
, true);
3806 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3808 else // !ctrl, !shift
3810 // test in the enclosing if should make it impossible
3811 wxFAIL_MSG( wxT("how did we get here?") );
3815 if (m_currentRow
!= oldCurrentRow
)
3816 RefreshRow( oldCurrentRow
);
3818 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3820 // Update selection here...
3823 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3824 (current
== oldCurrentRow
)) && oldWasSelected
;
3826 // Call LeftClick after everything else as under GTK+
3827 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3829 // notify cell about click
3830 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3832 wxRect
cell_rect( xpos
, GetLineStart( current
),
3833 col
->GetWidth(), GetLineHeight( current
) );
3835 // Report position relative to the cell's custom area, i.e.
3836 // no the entire space as given by the control but the one
3837 // used by the renderer after calculation of alignment etc.
3839 // adjust the rectangle ourselves to account for the alignment
3840 wxRect rectItem
= cell_rect
;
3841 const int align
= cell
->GetAlignment();
3842 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3844 const wxSize size
= cell
->GetSize();
3846 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3848 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3849 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3850 else if ( align
& wxALIGN_RIGHT
)
3851 rectItem
.x
+= cell_rect
.width
- size
.x
;
3852 // else: wxALIGN_LEFT is the default
3855 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3857 if ( align
& wxALIGN_CENTER_VERTICAL
)
3858 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3859 else if ( align
& wxALIGN_BOTTOM
)
3860 rectItem
.y
+= cell_rect
.height
- size
.y
;
3861 // else: wxALIGN_TOP is the default
3865 wxPoint
pos( event
.GetPosition() );
3866 pos
.x
-= rectItem
.x
;
3867 pos
.y
-= rectItem
.y
;
3869 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3871 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3872 model
, item
, col
->GetModelColumn());
3877 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3881 if (HasCurrentRow())
3887 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3891 if (HasCurrentRow())
3897 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3899 if( m_selection
.GetCount() != 1 )
3900 return wxDataViewItem();
3902 return GetItemByRow( m_selection
.Item(0));
3905 //-----------------------------------------------------------------------------
3907 //-----------------------------------------------------------------------------
3909 WX_DEFINE_LIST(wxDataViewColumnList
)
3911 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3912 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3913 EVT_SIZE(wxDataViewCtrl::OnSize
)
3916 wxDataViewCtrl::~wxDataViewCtrl()
3919 GetModel()->RemoveNotifier( m_notifier
);
3922 m_colsBestWidths
.clear();
3925 void wxDataViewCtrl::Init()
3927 m_cols
.DeleteContents(true);
3930 // No sorting column at start
3931 m_sortingColumnIdx
= wxNOT_FOUND
;
3933 m_headerArea
= NULL
;
3936 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3941 const wxValidator
& validator
,
3942 const wxString
& name
)
3944 // if ( (style & wxBORDER_MASK) == 0)
3945 // style |= wxBORDER_SUNKEN;
3949 if (!wxControl::Create( parent
, id
, pos
, size
,
3950 style
| wxScrolledWindowStyle
, validator
, name
))
3953 SetInitialSize(size
);
3956 MacSetClipChildren( true );
3959 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3961 // We use the cursor keys for moving the selection, not scrolling, so call
3962 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3963 // keyboard events forwarded to us from wxListMainWindow.
3964 DisableKeyboardScrolling();
3966 if (HasFlag(wxDV_NO_HEADER
))
3967 m_headerArea
= NULL
;
3969 m_headerArea
= new wxDataViewHeaderWindow(this);
3971 SetTargetWindow( m_clientArea
);
3973 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3975 sizer
->Add( m_headerArea
, 0, wxGROW
);
3976 sizer
->Add( m_clientArea
, 1, wxGROW
);
3982 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3984 return wxBORDER_THEME
;
3988 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3992 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3995 // we need to process arrows ourselves for scrolling
3996 if ( nMsg
== WM_GETDLGCODE
)
3998 rc
|= DLGC_WANTARROWS
;
4006 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4008 wxSize newsize
= size
;
4009 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4010 newsize
.y
-= m_headerArea
->GetSize().y
;
4015 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4017 // We need to override OnSize so that our scrolled
4018 // window a) does call Layout() to use sizers for
4019 // positioning the controls but b) does not query
4020 // the sizer for their size and use that for setting
4021 // the scrollable area as set that ourselves by
4022 // calling SetScrollbar() further down.
4028 // We must redraw the headers if their height changed. Normally this
4029 // shouldn't happen as the control shouldn't let itself be resized beneath
4030 // its minimal height but avoid the display artefacts that appear if it
4031 // does happen, e.g. because there is really not enough vertical space.
4032 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4033 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4035 m_headerArea
->Refresh();
4039 void wxDataViewCtrl::SetFocus()
4042 m_clientArea
->SetFocus();
4045 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4047 if (!wxDataViewCtrlBase::AssociateModel( model
))
4050 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4052 model
->AddNotifier( m_notifier
);
4054 m_clientArea
->DestroyTree();
4056 m_clientArea
->BuildTree(model
);
4058 m_clientArea
->UpdateDisplay();
4063 #if wxUSE_DRAG_AND_DROP
4065 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4067 return m_clientArea
->EnableDragSource( format
);
4070 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4072 return m_clientArea
->EnableDropTarget( format
);
4075 #endif // wxUSE_DRAG_AND_DROP
4077 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4079 if (!wxDataViewCtrlBase::AppendColumn(col
))
4082 m_cols
.Append( col
);
4083 m_colsBestWidths
.push_back(0);
4084 OnColumnsCountChanged();
4088 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4090 if (!wxDataViewCtrlBase::PrependColumn(col
))
4093 m_cols
.Insert( col
);
4094 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4095 OnColumnsCountChanged();
4099 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4101 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4104 m_cols
.Insert( pos
, col
);
4105 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4106 OnColumnsCountChanged();
4110 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4113 m_headerArea
->UpdateColumn(idx
);
4115 m_clientArea
->UpdateDisplay();
4118 void wxDataViewCtrl::OnColumnsCountChanged()
4121 m_headerArea
->SetColumnCount(GetColumnCount());
4123 m_clientArea
->UpdateDisplay();
4126 void wxDataViewCtrl::DoSetExpanderColumn()
4128 m_clientArea
->UpdateDisplay();
4131 void wxDataViewCtrl::DoSetIndent()
4133 m_clientArea
->UpdateDisplay();
4136 unsigned int wxDataViewCtrl::GetColumnCount() const
4138 return m_cols
.GetCount();
4141 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4143 if ( !m_clientArea
)
4146 m_clientArea
->SetRowHeight(lineHeight
);
4151 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4156 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4158 // columns can't be reordered if there is no header window which allows
4160 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4163 return GetColumn(idx
);
4166 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4168 const unsigned count
= m_cols
.size();
4169 for ( unsigned n
= 0; n
< count
; n
++ )
4171 if ( m_cols
[n
] == column
)
4178 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4180 if ( m_colsBestWidths
[idx
] != 0 )
4181 return m_colsBestWidths
[idx
];
4183 const int count
= m_clientArea
->GetRowCount();
4184 wxDataViewColumn
*column
= GetColumn(idx
);
4185 wxDataViewRenderer
*renderer
=
4186 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4188 class MaxWidthCalculator
4191 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4192 wxDataViewRenderer
*renderer
,
4193 const wxDataViewModel
*model
,
4196 m_clientArea(clientArea
),
4197 m_renderer(renderer
),
4203 void UpdateWithWidth(int width
)
4205 m_width
= wxMax(m_width
, width
);
4208 void UpdateWithRow(int row
)
4210 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4211 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4212 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4215 int GetMaxWidth() const { return m_width
; }
4219 wxDataViewMainWindow
*m_clientArea
;
4220 wxDataViewRenderer
*m_renderer
;
4221 const wxDataViewModel
*m_model
;
4225 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4226 GetModel(), column
->GetModelColumn());
4230 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4231 // Labels on native MSW header are indented on both sides
4233 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4234 calculator
.UpdateWithWidth(header_width
);
4237 // The code below deserves some explanation. For very large controls, we
4238 // simply can't afford to calculate sizes for all items, it takes too
4239 // long. So the best we can do is to check the first and the last N/2
4240 // items in the control for some sufficiently large N and calculate best
4241 // sizes from that. That can result in the calculated best width being too
4242 // small for some outliers, but it's better to get slightly imperfect
4243 // result than to wait several seconds after every update. To avoid highly
4244 // visible miscalculations, we also include all currently visible items
4245 // no matter what. Finally, the value of N is determined dynamically by
4246 // measuring how much time we spent on the determining item widths so far.
4249 int top_part_end
= count
;
4250 static const long CALC_TIMEOUT
= 20/*ms*/;
4251 // don't call wxStopWatch::Time() too often
4252 static const unsigned CALC_CHECK_FREQ
= 100;
4255 // use some hard-coded limit, that's the best we can do without timer
4256 int top_part_end
= wxMin(500, count
);
4257 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4261 for ( row
= 0; row
< top_part_end
; row
++ )
4264 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4265 timer
.Time() > CALC_TIMEOUT
)
4267 #endif // wxUSE_STOPWATCH
4268 calculator
.UpdateWithRow(row
);
4271 // row is the first unmeasured item now; that's our value of N/2
4277 // add bottom N/2 items now:
4278 const int bottom_part_start
= wxMax(row
, count
- row
);
4279 for ( row
= bottom_part_start
; row
< count
; row
++ )
4281 calculator
.UpdateWithRow(row
);
4284 // finally, include currently visible items in the calculation:
4285 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4286 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4287 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4289 first_visible
= wxMax(first_visible
, top_part_end
);
4290 last_visible
= wxMin(bottom_part_start
, last_visible
);
4292 for ( row
= first_visible
; row
< last_visible
; row
++ )
4294 calculator
.UpdateWithRow(row
);
4297 wxLogTrace("dataview",
4298 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4300 count
- bottom_part_start
,
4301 wxMax(0, last_visible
- first_visible
),
4305 int max_width
= calculator
.GetMaxWidth();
4306 if ( max_width
> 0 )
4307 max_width
+= 2 * PADDING_RIGHTLEFT
;
4309 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4313 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4314 unsigned int WXUNUSED(new_pos
))
4316 // do _not_ reorder m_cols elements here, they should always be in the
4317 // order in which columns were added, we only display the columns in
4319 m_clientArea
->UpdateDisplay();
4322 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4324 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4328 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4330 OnColumnsCountChanged();
4335 bool wxDataViewCtrl::ClearColumns()
4338 m_colsBestWidths
.clear();
4339 OnColumnsCountChanged();
4343 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4345 m_colsBestWidths
[idx
] = 0;
4348 m_headerArea
->UpdateColumn(idx
);
4351 void wxDataViewCtrl::InvalidateColBestWidths()
4353 m_colsBestWidths
.clear();
4354 m_colsBestWidths
.resize(m_cols
.size());
4358 const unsigned cols
= m_headerArea
->GetColumnCount();
4359 for ( unsigned i
= 0; i
< cols
; i
++ )
4360 m_headerArea
->UpdateColumn(i
);
4364 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4367 unsigned int len
= GetColumnCount();
4368 for ( unsigned int i
= 0; i
< len
; i
++ )
4370 wxDataViewColumn
* col
= GetColumnAt(i
);
4377 // This returns the position in pixels which is not what we want.
4380 unsigned int len
= GetColumnCount();
4381 for ( unsigned int i
= 0; i
< len
; i
++ )
4383 wxDataViewColumn
* col
= GetColumnAt(i
);
4384 if (col
->IsHidden())
4386 ret
+= col
->GetWidth();
4389 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4397 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4399 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4400 : GetColumn(m_sortingColumnIdx
);
4403 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4405 return GetItemByRow(m_clientArea
->GetCurrentRow());
4408 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4410 const int row
= m_clientArea
->GetRowByItem(item
);
4412 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4413 if ( static_cast<unsigned>(row
) != oldCurrent
)
4415 m_clientArea
->ChangeCurrentRow(row
);
4416 m_clientArea
->RefreshRow(oldCurrent
);
4417 m_clientArea
->RefreshRow(row
);
4421 // Selection code with wxDataViewItem as parameters
4422 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4424 return m_clientArea
->GetSelection();
4427 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4430 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4432 const size_t len
= selection
.size();
4433 for ( size_t i
= 0; i
< len
; i
++ )
4435 wxDataViewItem item
= m_clientArea
->GetItemByRow(selection
[i
]);
4442 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4449 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4451 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4453 wxDataViewItem last_parent
;
4455 int len
= sel
.GetCount();
4456 for( int i
= 0; i
< len
; i
++ )
4458 wxDataViewItem item
= sel
[i
];
4459 wxDataViewItem parent
= GetModel()->GetParent( item
);
4462 if (parent
!= last_parent
)
4463 ExpandAncestors(item
);
4466 last_parent
= parent
;
4467 int row
= m_clientArea
->GetRowByItem( item
);
4469 selection
.Add( static_cast<unsigned int>(row
) );
4472 m_clientArea
->SetSelections( selection
);
4475 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4477 ExpandAncestors( item
);
4479 int row
= m_clientArea
->GetRowByItem( item
);
4482 // Unselect all rows before select another in the single select mode
4483 if (m_clientArea
->IsSingleSel())
4484 m_clientArea
->SelectAllRows(false);
4486 m_clientArea
->SelectRow(row
, true);
4488 // Also set focus to the selected item
4489 m_clientArea
->ChangeCurrentRow( row
);
4493 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4495 int row
= m_clientArea
->GetRowByItem( item
);
4497 m_clientArea
->SelectRow(row
, false);
4500 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4502 int row
= m_clientArea
->GetRowByItem( item
);
4505 return m_clientArea
->IsRowSelected(row
);
4510 void wxDataViewCtrl::SelectAll()
4512 m_clientArea
->SelectAllRows(true);
4515 void wxDataViewCtrl::UnselectAll()
4517 m_clientArea
->SelectAllRows(false);
4520 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4524 if( row
> (int) m_clientArea
->GetRowCount() )
4525 row
= m_clientArea
->GetRowCount();
4527 int first
= m_clientArea
->GetFirstVisibleRow();
4528 int last
= m_clientArea
->GetLastVisibleRow();
4530 m_clientArea
->ScrollTo( row
, column
);
4531 else if( row
> last
)
4532 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4534 m_clientArea
->ScrollTo( first
, column
);
4537 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4539 ExpandAncestors( item
);
4541 m_clientArea
->RecalculateDisplay();
4543 int row
= m_clientArea
->GetRowByItem(item
);
4546 if( column
== NULL
)
4547 EnsureVisible(row
, -1);
4549 EnsureVisible( row
, GetColumnIndex(column
) );
4554 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4555 wxDataViewColumn
* &column
) const
4557 m_clientArea
->HitTest(point
, item
, column
);
4560 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4561 const wxDataViewColumn
* column
) const
4563 return m_clientArea
->GetItemRect(item
, column
);
4566 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4568 return m_clientArea
->GetItemByRow( row
);
4571 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4573 return m_clientArea
->GetRowByItem( item
);
4576 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4578 ExpandAncestors( item
);
4580 int row
= m_clientArea
->GetRowByItem( item
);
4582 m_clientArea
->Expand(row
);
4585 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4587 int row
= m_clientArea
->GetRowByItem( item
);
4589 m_clientArea
->Collapse(row
);
4592 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4594 int row
= m_clientArea
->GetRowByItem( item
);
4596 return m_clientArea
->IsExpanded(row
);
4600 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4602 wxDataViewColumn
* col
= GetColumn( column
);
4606 wxRect itemRect
= GetItemRect(item
, col
);
4607 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4608 if (renderer
->GetMode() == wxDATAVIEW_CELL_EDITABLE
)
4609 renderer
->StartEditing(item
, itemRect
);
4612 #endif // !wxUSE_GENERICDATAVIEWCTRL
4614 #endif // wxUSE_DATAVIEWCTRL