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_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
419 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
420 WX_DEFINE_LIST(ItemList
)
422 class wxDataViewMainWindow
: public wxWindow
425 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
427 const wxPoint
&pos
= wxDefaultPosition
,
428 const wxSize
&size
= wxDefaultSize
,
429 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
430 virtual ~wxDataViewMainWindow();
432 bool IsList() const { return GetOwner()->GetModel()->IsListModel(); }
433 bool IsVirtualList() const { return m_root
== NULL
; }
435 // notifications from wxDataViewModel
436 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
437 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
438 bool ItemChanged( const wxDataViewItem
&item
);
439 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
443 if (!IsVirtualList())
453 g_model
= GetOwner()->GetModel();
454 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
457 if (g_model
->HasDefaultCompare())
465 g_column
= col
->GetModelColumn();
466 g_asending
= col
->IsSortOrderAscending();
469 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
470 wxDataViewCtrl
*GetOwner() { return m_owner
; }
471 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
473 #if wxUSE_DRAG_AND_DROP
474 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
475 #endif // wxUSE_DRAG_AND_DROP
476 void OnPaint( wxPaintEvent
&event
);
477 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
478 void OnChar( wxKeyEvent
&event
);
479 void OnMouse( wxMouseEvent
&event
);
480 void OnSetFocus( wxFocusEvent
&event
);
481 void OnKillFocus( wxFocusEvent
&event
);
483 void UpdateDisplay();
484 void RecalculateDisplay();
485 void OnInternalIdle();
487 void OnRenameTimer();
489 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
490 void ScrollTo( int rows
, int column
);
492 unsigned GetCurrentRow() const { return m_currentRow
; }
493 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
494 void ChangeCurrentRow( unsigned int row
);
496 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
497 bool IsEmpty() { return GetRowCount() == 0; }
499 int GetCountPerPage() const;
500 int GetEndOfLastCol() const;
501 unsigned int GetFirstVisibleRow() const;
503 // I change this method to un const because in the tree view,
504 // the displaying number of the tree are changing along with the
505 // expanding/collapsing of the tree nodes
506 unsigned int GetLastVisibleRow();
507 unsigned int GetRowCount();
509 wxDataViewItem
GetSelection() const;
510 wxDataViewSelection
GetSelections(){ return m_selection
; }
511 void SetSelections( const wxDataViewSelection
& sel
)
512 { m_selection
= sel
; UpdateDisplay(); }
513 void Select( const wxArrayInt
& aSelections
);
514 void SelectAllRows( bool on
);
515 void SelectRow( unsigned int row
, bool on
);
516 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
517 void ReverseRowSelection( unsigned int row
);
518 bool IsRowSelected( unsigned int row
);
519 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
521 void RefreshRow( unsigned int row
);
522 void RefreshRows( unsigned int from
, unsigned int to
);
523 void RefreshRowsAfter( unsigned int firstRow
);
525 // returns the colour to be used for drawing the rules
526 wxColour
GetRuleColour() const
528 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
531 wxRect
GetLineRect( unsigned int row
) const;
533 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
534 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
535 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
537 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
539 // Some useful functions for row and item mapping
540 wxDataViewItem
GetItemByRow( unsigned int row
) const;
541 int GetRowByItem( const wxDataViewItem
& item
) const;
543 // Methods for building the mapping tree
544 void BuildTree( wxDataViewModel
* model
);
546 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
547 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
549 void Expand( unsigned int row
);
550 void Collapse( unsigned int row
);
551 bool IsExpanded( unsigned int row
) const;
552 bool HasChildren( unsigned int row
) const;
554 #if wxUSE_DRAG_AND_DROP
555 bool EnableDragSource( const wxDataFormat
&format
);
556 bool EnableDropTarget( const wxDataFormat
&format
);
558 void RemoveDropHint();
559 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
560 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
561 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
563 #endif // wxUSE_DRAG_AND_DROP
566 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
567 // We did not need this temporarily
568 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
570 int RecalculateCount();
572 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
574 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
577 wxDataViewCtrl
*m_owner
;
581 wxDataViewColumn
*m_currentCol
;
582 unsigned int m_currentRow
;
583 wxDataViewSelection m_selection
;
585 wxDataViewRenameTimer
*m_renameTimer
;
590 #if wxUSE_DRAG_AND_DROP
595 wxDataFormat m_dragFormat
;
598 wxDataFormat m_dropFormat
;
600 unsigned int m_dropHintLine
;
601 #endif // wxUSE_DRAG_AND_DROP
603 // for double click logic
604 unsigned int m_lineLastClicked
,
605 m_lineBeforeLastClicked
,
606 m_lineSelectSingleOnUp
;
608 // the pen used to draw horiz/vertical rules
611 // the pen used to draw the expander and the lines
614 // This is the tree structure of the model
615 wxDataViewTreeNode
* m_root
;
618 // This is the tree node under the cursor
619 wxDataViewTreeNode
* m_underMouse
;
622 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
623 DECLARE_EVENT_TABLE()
626 // ---------------------------------------------------------
627 // wxGenericDataViewModelNotifier
628 // ---------------------------------------------------------
630 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
633 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
634 { m_mainWindow
= mainWindow
; }
636 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
637 { return m_mainWindow
->ItemAdded( parent
, item
); }
638 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
639 { return m_mainWindow
->ItemDeleted( parent
, item
); }
640 virtual bool ItemChanged( const wxDataViewItem
& item
)
641 { return m_mainWindow
->ItemChanged(item
); }
642 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
643 { return m_mainWindow
->ValueChanged( item
, col
); }
644 virtual bool Cleared()
645 { return m_mainWindow
->Cleared(); }
646 virtual void Resort()
647 { m_mainWindow
->Resort(); }
649 wxDataViewMainWindow
*m_mainWindow
;
652 // ---------------------------------------------------------
653 // wxDataViewRenderer
654 // ---------------------------------------------------------
656 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
658 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
659 wxDataViewCellMode mode
,
661 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
665 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
669 wxDataViewRenderer::~wxDataViewRenderer()
674 wxDC
*wxDataViewRenderer::GetDC()
678 if (GetOwner() == NULL
)
680 if (GetOwner()->GetOwner() == NULL
)
682 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
688 void wxDataViewRenderer::SetAlignment( int align
)
693 int wxDataViewRenderer::GetAlignment() const
698 // ---------------------------------------------------------
699 // wxDataViewCustomRenderer
700 // ---------------------------------------------------------
702 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
704 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
705 wxDataViewCellMode mode
, int align
) :
706 wxDataViewRenderer( varianttype
, mode
, align
)
710 // ---------------------------------------------------------
711 // wxDataViewTextRenderer
712 // ---------------------------------------------------------
714 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
716 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
717 wxDataViewCellMode mode
, int align
) :
718 wxDataViewRenderer( varianttype
, mode
, align
)
722 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
724 m_text
= value
.GetString();
729 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
734 bool wxDataViewTextRenderer::HasEditorCtrl() const
739 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
740 wxRect labelRect
, const wxVariant
&value
)
742 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
743 wxPoint(labelRect
.x
,labelRect
.y
),
744 wxSize(labelRect
.width
,labelRect
.height
) );
746 // select the text in the control an place the cursor at the end
747 ctrl
->SetInsertionPointEnd();
753 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
755 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
756 value
= text
->GetValue();
760 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
762 RenderText(m_text
, 0, rect
, dc
, state
);
766 wxSize
wxDataViewTextRenderer::GetSize() const
769 return GetTextExtent(m_text
);
771 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
774 // ---------------------------------------------------------
775 // wxDataViewBitmapRenderer
776 // ---------------------------------------------------------
778 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
780 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
781 wxDataViewCellMode mode
, int align
) :
782 wxDataViewRenderer( varianttype
, mode
, align
)
786 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
788 if (value
.GetType() == wxT("wxBitmap"))
790 if (value
.GetType() == wxT("wxIcon"))
796 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
801 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
804 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
805 else if (m_icon
.IsOk())
806 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
811 wxSize
wxDataViewBitmapRenderer::GetSize() const
814 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
815 else if (m_icon
.IsOk())
816 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
818 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
821 // ---------------------------------------------------------
822 // wxDataViewToggleRenderer
823 // ---------------------------------------------------------
825 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
827 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
828 wxDataViewCellMode mode
, int align
) :
829 wxDataViewRenderer( varianttype
, mode
, align
)
834 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
836 m_toggle
= value
.GetBool();
841 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
846 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
850 flags
|= wxCONTROL_CHECKED
;
851 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
852 GetEnabled() == false)
853 flags
|= wxCONTROL_DISABLED
;
855 // check boxes we draw must always have the same, standard size (if it's
856 // bigger than the cell size the checkbox will be truncated because the
857 // caller had set the clipping rectangle to prevent us from drawing outside
859 cell
.SetSize(GetSize());
861 wxRendererNative::Get().DrawCheckBox(
862 GetOwner()->GetOwner(),
870 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
871 const wxRect
& WXUNUSED(cell
),
872 wxDataViewModel
*model
,
873 const wxDataViewItem
& item
,
876 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
877 if (!wxRect(GetSize()).Contains(cursor
))
880 if (model
->IsEnabled(item
, col
))
882 model
->ChangeValue(!m_toggle
, item
, col
);
889 wxSize
wxDataViewToggleRenderer::GetSize() const
891 // the window parameter is not used by GetCheckBoxSize() so it's
893 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
896 // ---------------------------------------------------------
897 // wxDataViewProgressRenderer
898 // ---------------------------------------------------------
900 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
902 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
903 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
904 wxDataViewRenderer( varianttype
, mode
, align
)
910 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
912 m_value
= (long) value
;
914 if (m_value
< 0) m_value
= 0;
915 if (m_value
> 100) m_value
= 100;
920 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
922 value
= (long) m_value
;
927 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
929 // deflate the rect to leave a small border between bars in adjacent rows
930 wxRect bar
= rect
.Deflate(0, 1);
932 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
933 dc
->SetPen( *wxBLACK_PEN
);
934 dc
->DrawRectangle( bar
);
936 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
937 dc
->SetPen( *wxTRANSPARENT_PEN
);
939 const wxDataViewItemAttr
& attr
= GetAttr();
940 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
942 dc
->DrawRectangle( bar
);
947 wxSize
wxDataViewProgressRenderer::GetSize() const
949 return wxSize(40,12);
952 // ---------------------------------------------------------
953 // wxDataViewDateRenderer
954 // ---------------------------------------------------------
956 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
958 #if wxUSE_DATE_RENDERER_POPUP
960 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
963 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
964 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
965 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
970 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
971 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
972 sizer
->Add( m_cal
, 1, wxGROW
);
977 void OnCalendar( wxCalendarEvent
&event
);
979 wxCalendarCtrl
*m_cal
;
980 wxDataViewModel
*m_model
;
982 const wxDataViewItem
& m_item
;
985 virtual void OnDismiss()
990 DECLARE_EVENT_TABLE()
993 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
994 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
997 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
999 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1003 #endif // wxUSE_DATE_RENDERER_POPUP
1005 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1007 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1008 wxDataViewCellMode mode
, int align
) :
1009 wxDataViewRenderer( varianttype
, mode
, align
)
1013 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1015 m_date
= value
.GetDateTime();
1020 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1026 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1028 wxString tmp
= m_date
.FormatDate();
1029 RenderText( tmp
, 0, cell
, dc
, state
);
1033 wxSize
wxDataViewDateRenderer::GetSize() const
1035 return GetTextExtent(m_date
.FormatDate());
1038 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1039 wxDataViewModel
*model
,
1040 const wxDataViewItem
& item
,
1043 wxDateTime dtOld
= m_date
;
1045 #if wxUSE_DATE_RENDERER_POPUP
1046 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1047 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1048 wxPoint pos
= wxGetMousePosition();
1051 popup
->Popup( popup
->m_cal
);
1052 #else // !wxUSE_DATE_RENDERER_POPUP
1053 wxMessageBox(dtOld
.Format());
1054 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1059 // ---------------------------------------------------------
1060 // wxDataViewIconTextRenderer
1061 // ---------------------------------------------------------
1063 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1065 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1066 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1067 wxDataViewRenderer( varianttype
, mode
, align
)
1070 SetAlignment(align
);
1073 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1079 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1084 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1088 const wxIcon
& icon
= m_value
.GetIcon();
1091 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1092 xoffset
= icon
.GetWidth()+4;
1095 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1100 wxSize
wxDataViewIconTextRenderer::GetSize() const
1102 if (!m_value
.GetText().empty())
1104 wxSize size
= GetTextExtent(m_value
.GetText());
1106 if (m_value
.GetIcon().IsOk())
1107 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1110 return wxSize(80,20);
1113 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1115 wxDataViewIconText iconText
;
1118 wxString text
= iconText
.GetText();
1120 // adjust the label rect to take the width of the icon into account
1121 if (iconText
.GetIcon().IsOk())
1123 int w
= iconText
.GetIcon().GetWidth() + 4;
1125 labelRect
.width
-= w
;
1128 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1129 wxPoint(labelRect
.x
,labelRect
.y
),
1130 wxSize(labelRect
.width
,labelRect
.height
) );
1132 // select the text in the control an place the cursor at the end
1133 ctrl
->SetInsertionPointEnd();
1139 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1141 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1143 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1148 //-----------------------------------------------------------------------------
1149 // wxDataViewDropTarget
1150 //-----------------------------------------------------------------------------
1152 #if wxUSE_DRAG_AND_DROP
1154 class wxBitmapCanvas
: public wxWindow
1157 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1158 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1161 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1164 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1167 dc
.DrawBitmap( m_bitmap
, 0, 0);
1173 class wxDataViewDropSource
: public wxDropSource
1176 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1184 ~wxDataViewDropSource()
1189 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1191 wxPoint pos
= wxGetMousePosition();
1195 int liney
= m_win
->GetLineStart( m_row
);
1197 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1198 m_win
->ClientToScreen( &linex
, &liney
);
1199 m_dist_x
= pos
.x
- linex
;
1200 m_dist_y
= pos
.y
- liney
;
1203 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1205 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1206 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1208 wxFRAME_TOOL_WINDOW
|
1209 wxFRAME_FLOAT_ON_PARENT
|
1210 wxFRAME_NO_TASKBAR
|
1212 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1217 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1218 m_hint
->SetTransparent( 128 );
1224 wxDataViewMainWindow
*m_win
;
1227 int m_dist_x
,m_dist_y
;
1231 class wxDataViewDropTarget
: public wxDropTarget
1234 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1240 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1242 wxDataFormat format
= GetMatchingPair();
1243 if (format
== wxDF_INVALID
)
1245 return m_win
->OnDragOver( format
, x
, y
, def
);
1248 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1250 wxDataFormat format
= GetMatchingPair();
1251 if (format
== wxDF_INVALID
)
1253 return m_win
->OnDrop( format
, x
, y
);
1256 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1258 wxDataFormat format
= GetMatchingPair();
1259 if (format
== wxDF_INVALID
)
1263 return m_win
->OnData( format
, x
, y
, def
);
1266 virtual void OnLeave()
1267 { m_win
->OnLeave(); }
1269 wxDataViewMainWindow
*m_win
;
1272 #endif // wxUSE_DRAG_AND_DROP
1274 //-----------------------------------------------------------------------------
1275 // wxDataViewRenameTimer
1276 //-----------------------------------------------------------------------------
1278 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1283 void wxDataViewRenameTimer::Notify()
1285 m_owner
->OnRenameTimer();
1288 //-----------------------------------------------------------------------------
1289 // wxDataViewMainWindow
1290 //-----------------------------------------------------------------------------
1292 // The tree building helper, declared firstly
1293 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1294 wxDataViewTreeNode
* node
);
1296 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1298 if (row1
> row2
) return 1;
1299 if (row1
== row2
) return 0;
1303 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1305 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1306 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1307 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1308 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1309 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1310 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1313 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1314 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1315 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1316 m_selection( wxDataViewSelectionCmp
)
1321 m_lastOnSame
= false;
1322 m_renameTimer
= new wxDataViewRenameTimer( this );
1324 // TODO: user better initial values/nothing selected
1325 m_currentCol
= NULL
;
1328 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1330 #if wxUSE_DRAG_AND_DROP
1332 m_dragStart
= wxPoint(0,0);
1334 m_dragEnabled
= false;
1335 m_dropEnabled
= false;
1337 m_dropHintLine
= (unsigned int) -1;
1338 #endif // wxUSE_DRAG_AND_DROP
1340 m_lineLastClicked
= (unsigned int) -1;
1341 m_lineBeforeLastClicked
= (unsigned int) -1;
1342 m_lineSelectSingleOnUp
= (unsigned int) -1;
1346 SetBackgroundColour( *wxWHITE
);
1348 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1350 m_penRule
= wxPen(GetRuleColour());
1352 // compose a pen whichcan draw black lines
1353 // TODO: maybe there is something system colour to use
1354 m_penExpander
= wxPen(wxColour(0,0,0));
1356 m_root
= new wxDataViewTreeNode( NULL
);
1357 m_root
->SetHasChildren(true);
1359 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1361 m_underMouse
= NULL
;
1365 wxDataViewMainWindow::~wxDataViewMainWindow()
1368 delete m_renameTimer
;
1372 #if wxUSE_DRAG_AND_DROP
1373 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1375 m_dragFormat
= format
;
1376 m_dragEnabled
= format
!= wxDF_INVALID
;
1381 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1383 m_dropFormat
= format
;
1384 m_dropEnabled
= format
!= wxDF_INVALID
;
1387 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1392 void wxDataViewMainWindow::RemoveDropHint()
1397 RefreshRow( m_dropHintLine
);
1398 m_dropHintLine
= (unsigned int) -1;
1402 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1403 wxCoord y
, wxDragResult def
)
1407 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1408 unsigned int row
= GetLineAt( yy
);
1410 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1416 wxDataViewItem item
= GetItemByRow( row
);
1418 wxDataViewModel
*model
= GetOwner()->GetModel();
1420 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1421 event
.SetEventObject( m_owner
);
1422 event
.SetItem( item
);
1423 event
.SetModel( model
);
1424 event
.SetDataFormat( format
);
1425 if (!m_owner
->HandleWindowEvent( event
))
1431 if (!event
.IsAllowed())
1438 if (m_dropHint
&& (row
!= m_dropHintLine
))
1439 RefreshRow( m_dropHintLine
);
1441 m_dropHintLine
= row
;
1447 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1453 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1454 unsigned int row
= GetLineAt( yy
);
1456 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1459 wxDataViewItem item
= GetItemByRow( row
);
1461 wxDataViewModel
*model
= GetOwner()->GetModel();
1463 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1464 event
.SetEventObject( m_owner
);
1465 event
.SetItem( item
);
1466 event
.SetModel( model
);
1467 event
.SetDataFormat( format
);
1468 if (!m_owner
->HandleWindowEvent( event
))
1471 if (!event
.IsAllowed())
1477 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1482 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1483 unsigned int row
= GetLineAt( yy
);
1485 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1488 wxDataViewItem item
= GetItemByRow( row
);
1490 wxDataViewModel
*model
= GetOwner()->GetModel();
1492 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1494 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1495 event
.SetEventObject( m_owner
);
1496 event
.SetItem( item
);
1497 event
.SetModel( model
);
1498 event
.SetDataFormat( format
);
1499 event
.SetDataSize( obj
->GetSize() );
1500 event
.SetDataBuffer( obj
->GetData() );
1501 if (!m_owner
->HandleWindowEvent( event
))
1504 if (!event
.IsAllowed())
1510 void wxDataViewMainWindow::OnLeave()
1515 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1517 int height
= GetLineHeight( row
);
1519 unsigned int cols
= GetOwner()->GetColumnCount();
1521 for (col
= 0; col
< cols
; col
++)
1523 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1524 if (column
->IsHidden())
1525 continue; // skip it!
1526 width
+= column
->GetWidth();
1532 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1533 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1534 indent
= indent
+ m_lineHeight
;
1535 // try to use the m_lineHeight as the expander space
1537 if(!node
->HasChildren())
1542 wxBitmap
bitmap( width
, height
);
1543 wxMemoryDC
dc( bitmap
);
1544 dc
.SetFont( GetFont() );
1545 dc
.SetPen( *wxBLACK_PEN
);
1546 dc
.SetBrush( *wxWHITE_BRUSH
);
1547 dc
.DrawRectangle( 0,0,width
,height
);
1549 wxDataViewModel
*model
= m_owner
->GetModel();
1551 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1554 // TODO-RTL: last column for RTL support
1555 expander
= GetOwner()->GetColumnAt( 0 );
1556 GetOwner()->SetExpanderColumn(expander
);
1561 for (col
= 0; col
< cols
; col
++)
1563 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1564 wxDataViewRenderer
*cell
= column
->GetRenderer();
1566 if (column
->IsHidden())
1567 continue; // skip it!
1569 width
= column
->GetWidth();
1571 if (column
== expander
)
1574 wxDataViewItem item
= GetItemByRow( row
);
1575 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1577 wxRect
item_rect(x
, 0, width
, height
);
1578 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1580 // dc.SetClippingRegion( item_rect );
1581 cell
->WXCallRender(item_rect
, &dc
, 0);
1582 // dc.DestroyClippingRegion();
1590 #endif // wxUSE_DRAG_AND_DROP
1593 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1595 wxDataViewModel
*model
= GetOwner()->GetModel();
1596 wxAutoBufferedPaintDC
dc( this );
1599 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1600 dc
.SetPen( *wxTRANSPARENT_PEN
);
1601 dc
.DrawRectangle(GetClientSize());
1606 // No items to draw.
1611 GetOwner()->PrepareDC( dc
);
1612 dc
.SetFont( GetFont() );
1614 wxRect update
= GetUpdateRegion().GetBox();
1615 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1617 // compute which items needs to be redrawn
1618 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1619 unsigned int item_count
=
1620 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1621 (int)(GetRowCount( ) - item_start
));
1622 unsigned int item_last
= item_start
+ item_count
;
1624 // Send the event to wxDataViewCtrl itself.
1625 wxWindow
* const parent
= GetParent();
1626 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1627 cache_event
.SetEventObject(parent
);
1628 cache_event
.SetCache(item_start
, item_last
- 1);
1629 parent
->ProcessWindowEvent(cache_event
);
1631 // compute which columns needs to be redrawn
1632 unsigned int cols
= GetOwner()->GetColumnCount();
1635 // we assume that we have at least one column below and painting an
1636 // empty control is unnecessary anyhow
1640 unsigned int col_start
= 0;
1641 unsigned int x_start
;
1642 for (x_start
= 0; col_start
< cols
; col_start
++)
1644 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1645 if (col
->IsHidden())
1646 continue; // skip it!
1648 unsigned int w
= col
->GetWidth();
1649 if (x_start
+w
>= (unsigned int)update
.x
)
1655 unsigned int col_last
= col_start
;
1656 unsigned int x_last
= x_start
;
1657 for (; col_last
< cols
; col_last
++)
1659 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1660 if (col
->IsHidden())
1661 continue; // skip it!
1663 if (x_last
> (unsigned int)update
.GetRight())
1666 x_last
+= col
->GetWidth();
1669 // Draw horizontal rules if required
1670 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1672 dc
.SetPen(m_penRule
);
1673 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1675 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1677 int y
= GetLineStart( i
);
1678 dc
.DrawLine(x_start
, y
, x_last
, y
);
1682 // Draw vertical rules if required
1683 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1685 dc
.SetPen(m_penRule
);
1686 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1688 // NB: Vertical rules are drawn in the last pixel of a column so that
1689 // they align perfectly with native MSW wxHeaderCtrl as well as for
1690 // consistency with MSW native list control. There's no vertical
1691 // rule at the most-left side of the control.
1693 int x
= x_start
- 1;
1694 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1696 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1697 if (col
->IsHidden())
1698 continue; // skip it
1700 x
+= col
->GetWidth();
1702 dc
.DrawLine(x
, GetLineStart( item_start
),
1703 x
, GetLineStart( item_last
) );
1707 // redraw the background for the items which are selected/current
1708 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1710 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1711 if (selected
|| item
== m_currentRow
)
1713 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1714 if (item
== m_currentRow
)
1715 flags
|= wxCONTROL_CURRENT
;
1717 flags
|= wxCONTROL_FOCUSED
;
1719 wxRect
rect( x_start
, GetLineStart( item
),
1720 x_last
- x_start
, GetLineHeight( item
) );
1721 wxRendererNative::Get().DrawItemSelectionRect
1731 #if wxUSE_DRAG_AND_DROP
1734 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1735 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1736 dc
.SetPen( *wxBLACK_PEN
);
1737 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1738 dc
.DrawRectangle( rect
);
1740 #endif // wxUSE_DRAG_AND_DROP
1742 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1745 // TODO-RTL: last column for RTL support
1746 expander
= GetOwner()->GetColumnAt( 0 );
1747 GetOwner()->SetExpanderColumn(expander
);
1750 // redraw all cells for all rows which must be repainted and all columns
1752 cell_rect
.x
= x_start
;
1753 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1755 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1756 wxDataViewRenderer
*cell
= col
->GetRenderer();
1757 cell_rect
.width
= col
->GetWidth();
1759 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1760 continue; // skip it!
1762 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1764 // get the cell value and set it into the renderer
1765 wxDataViewTreeNode
*node
= NULL
;
1766 wxDataViewItem dataitem
;
1768 if (!IsVirtualList())
1770 node
= GetTreeNodeByRow(item
);
1774 dataitem
= node
->GetItem();
1776 // Skip all columns of "container" rows except the expander
1777 // column itself unless HasContainerColumns() overrides this.
1778 if ( col
!= GetOwner()->GetExpanderColumn() &&
1779 model
->IsContainer(dataitem
) &&
1780 !model
->HasContainerColumns(dataitem
) )
1785 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1788 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1791 cell_rect
.y
= GetLineStart( item
);
1792 cell_rect
.height
= GetLineHeight( item
);
1794 // deal with the expander
1796 if ((!IsList()) && (col
== expander
))
1798 // Calculate the indent first
1799 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1801 // we reserve m_lineHeight of horizontal space for the expander
1802 // but leave EXPANDER_MARGIN around the expander itself
1803 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1805 indent
+= m_lineHeight
;
1807 // draw expander if needed and visible
1808 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1810 dc
.SetPen( m_penExpander
);
1811 dc
.SetBrush( wxNullBrush
);
1813 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1814 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1815 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1817 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1820 if ( m_underMouse
== node
)
1821 flag
|= wxCONTROL_CURRENT
;
1822 if ( node
->IsOpen() )
1823 flag
|= wxCONTROL_EXPANDED
;
1825 // ensure that we don't overflow the cell (which might
1826 // happen if the column is very narrow)
1827 wxDCClipper
clip(dc
, cell_rect
);
1829 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1832 // force the expander column to left-center align
1833 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1835 if (node
&& !node
->HasChildren())
1837 // Yes, if the node does not have any child, it must be a leaf which
1838 // mean that it is a temporarily created by GetTreeNodeByRow
1842 wxRect item_rect
= cell_rect
;
1843 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1845 // account for the tree indent (harmless if we're not indented)
1846 item_rect
.x
+= indent
;
1847 item_rect
.width
-= indent
;
1849 if ( item_rect
.width
<= 0 )
1853 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1854 state
|= wxDATAVIEW_CELL_SELECTED
;
1856 // TODO: it would be much more efficient to create a clipping
1857 // region for the entire column being rendered (in the OnPaint
1858 // of wxDataViewMainWindow) instead of a single clip region for
1859 // each cell. However it would mean that each renderer should
1860 // respect the given wxRect's top & bottom coords, eventually
1861 // violating only the left & right coords - however the user can
1862 // make its own renderer and thus we cannot be sure of that.
1863 wxDCClipper
clip(dc
, item_rect
);
1865 cell
->WXCallRender(item_rect
, &dc
, state
);
1868 cell_rect
.x
+= cell_rect
.width
;
1872 void wxDataViewMainWindow::OnRenameTimer()
1874 // We have to call this here because changes may just have
1875 // been made and no screen update taken place.
1878 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1879 // (needs to be tested!)
1883 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1885 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1887 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1890 //-----------------------------------------------------------------------------
1891 // Helper class for do operation on the tree node
1892 //-----------------------------------------------------------------------------
1897 virtual ~DoJob() { }
1899 // The return value control how the tree-walker tranverse the tree
1900 // 0: Job done, stop tranverse and return
1901 // 1: Ignore the current node's subtree and continue
1902 // 2: Job not done, continue
1903 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1904 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1905 virtual int operator() ( void * n
) = 0;
1908 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1913 switch( func( node
) )
1924 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
1925 const wxDataViewTreeLeaves
& leaves
= node
->GetChildren();
1927 int len_nodes
= nodes
.GetCount();
1928 int len
= leaves
.GetCount();
1929 int i
= 0, nodes_i
= 0;
1931 for(; i
< len
; i
++ )
1933 void * n
= leaves
[i
];
1934 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1936 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1939 if( Walker( nd
, func
) )
1958 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1960 GetOwner()->InvalidateColBestWidths();
1962 if (IsVirtualList())
1964 wxDataViewVirtualListModel
*list_model
=
1965 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1966 m_count
= list_model
->GetCount();
1973 wxDataViewTreeNode
* node
;
1974 node
= FindNode(parent
);
1979 node
->SetHasChildren( true );
1981 if( g_model
->IsContainer( item
) )
1983 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1984 newnode
->SetItem(item
);
1985 newnode
->SetHasChildren( true );
1986 node
->AddNode( newnode
);
1989 node
->AddLeaf( item
.GetID() );
1991 node
->ChangeSubTreeCount(1);
1999 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2001 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2002 const wxDataViewItem
& item
)
2004 GetOwner()->InvalidateColBestWidths();
2006 if (IsVirtualList())
2008 wxDataViewVirtualListModel
*list_model
=
2009 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
2010 m_count
= list_model
->GetCount();
2012 if( m_currentRow
> GetRowCount() )
2013 m_currentRow
= m_count
- 1;
2015 // TODO: why empty the entire selection?
2016 m_selection
.Empty();
2023 wxDataViewTreeNode
* node
= FindNode(parent
);
2025 // Notice that it is possible that the item being deleted is not in the
2026 // tree at all, for example we could be deleting a never shown (because
2027 // collapsed) item in a tree model. So it's not an error if we don't know
2028 // about this item, just return without doing anything then.
2029 if ( !node
|| node
->GetChildren().Index(item
.GetID()) == wxNOT_FOUND
)
2033 node
->GetChildren().Remove( item
.GetID() );
2034 // Manipolate selection
2035 if( m_selection
.GetCount() > 1 )
2037 m_selection
.Empty();
2039 bool isContainer
= false;
2040 wxDataViewTreeNodes nds
= node
->GetNodes();
2041 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2043 if (nds
[i
]->GetItem() == item
)
2051 wxDataViewTreeNode
* n
= NULL
;
2052 wxDataViewTreeNodes nodes
= node
->GetNodes();
2053 int len
= nodes
.GetCount();
2054 for( int i
= 0; i
< len
; i
++)
2056 if( nodes
[i
]->GetItem() == item
)
2063 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2065 node
->GetNodes().Remove( n
);
2066 sub
-= n
->GetSubTreeCount();
2067 ::DestroyTreeHelper(n
);
2069 // Make the row number invalid and get a new valid one when user call GetRowCount
2071 node
->ChangeSubTreeCount(sub
);
2073 // Change the current row to the last row if the current exceed the max row number
2074 if( m_currentRow
> GetRowCount() )
2075 m_currentRow
= m_count
- 1;
2082 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2084 GetOwner()->InvalidateColBestWidths();
2090 wxWindow
*parent
= GetParent();
2091 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2092 le
.SetEventObject(parent
);
2093 le
.SetModel(GetOwner()->GetModel());
2095 parent
->GetEventHandler()->ProcessEvent(le
);
2100 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2102 int view_column
= -1;
2103 unsigned int n_col
= m_owner
->GetColumnCount();
2104 for (unsigned i
= 0; i
< n_col
; i
++)
2106 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2107 if (column
->GetModelColumn() == model_column
)
2109 view_column
= (int) i
;
2113 if (view_column
== -1)
2116 GetOwner()->InvalidateColBestWidth(view_column
);
2118 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2119 /*#define MAX_VIRTUAL_WIDTH 100000
2121 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2122 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2123 Refresh( true, &rect );
2131 wxWindow
*parent
= GetParent();
2132 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2133 le
.SetEventObject(parent
);
2134 le
.SetModel(GetOwner()->GetModel());
2136 le
.SetColumn(view_column
);
2137 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2138 parent
->GetEventHandler()->ProcessEvent(le
);
2143 bool wxDataViewMainWindow::Cleared()
2145 GetOwner()->InvalidateColBestWidths();
2148 m_selection
.Clear();
2151 BuildTree( GetOwner()->GetModel() );
2158 void wxDataViewMainWindow::UpdateDisplay()
2161 m_underMouse
= NULL
;
2164 void wxDataViewMainWindow::OnInternalIdle()
2166 wxWindow::OnInternalIdle();
2170 RecalculateDisplay();
2175 void wxDataViewMainWindow::RecalculateDisplay()
2177 wxDataViewModel
*model
= GetOwner()->GetModel();
2184 int width
= GetEndOfLastCol();
2185 int height
= GetLineStart( GetRowCount() );
2187 SetVirtualSize( width
, height
);
2188 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2193 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2195 m_underMouse
= NULL
;
2197 wxWindow::ScrollWindow( dx
, dy
, rect
);
2199 if (GetOwner()->m_headerArea
)
2200 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2203 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2205 m_underMouse
= NULL
;
2208 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2209 int sy
= GetLineStart( rows
)/y
;
2213 wxRect rect
= GetClientRect();
2217 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2218 for (x_start
= 0; colnum
< column
; colnum
++)
2220 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2221 if (col
->IsHidden())
2222 continue; // skip it!
2224 w
= col
->GetWidth();
2228 int x_end
= x_start
+ w
;
2229 xe
= xx
+ rect
.width
;
2232 sx
= ( xx
+ x_end
- xe
)/x
;
2239 m_owner
->Scroll( sx
, sy
);
2242 int wxDataViewMainWindow::GetCountPerPage() const
2244 wxSize size
= GetClientSize();
2245 return size
.y
/ m_lineHeight
;
2248 int wxDataViewMainWindow::GetEndOfLastCol() const
2252 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2254 const wxDataViewColumn
*c
=
2255 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2258 width
+= c
->GetWidth();
2263 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2267 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2269 return GetLineAt( y
);
2272 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2274 wxSize client_size
= GetClientSize();
2275 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2276 &client_size
.x
, &client_size
.y
);
2278 // we should deal with the pixel here
2279 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2281 return wxMin( GetRowCount()-1, row
);
2284 unsigned int wxDataViewMainWindow::GetRowCount()
2286 if ( m_count
== -1 )
2288 m_count
= RecalculateCount();
2294 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2301 void wxDataViewMainWindow::SelectAllRows( bool on
)
2308 m_selection
.Clear();
2309 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2310 m_selection
.Add( i
);
2315 unsigned int first_visible
= GetFirstVisibleRow();
2316 unsigned int last_visible
= GetLastVisibleRow();
2318 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2320 unsigned int row
= m_selection
[i
];
2321 if ((row
>= first_visible
) && (row
<= last_visible
))
2324 m_selection
.Clear();
2328 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2330 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2334 m_selection
.Add( row
);
2342 m_selection
.Remove( row
);
2348 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2352 unsigned int tmp
= from
;
2358 for (i
= from
; i
<= to
; i
++)
2360 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2363 m_selection
.Add( i
);
2368 m_selection
.Remove( i
);
2371 RefreshRows( from
, to
);
2374 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2376 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2378 int n
= aSelections
[i
];
2380 m_selection
.Add( n
);
2385 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2387 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2388 m_selection
.Add( row
);
2390 m_selection
.Remove( row
);
2394 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2396 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2399 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2401 wxWindow
*parent
= GetParent();
2402 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2404 le
.SetEventObject(parent
);
2405 le
.SetModel(GetOwner()->GetModel());
2408 parent
->GetEventHandler()->ProcessEvent(le
);
2411 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2413 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2414 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2416 wxSize client_size
= GetClientSize();
2417 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2418 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2419 if (intersect_rect
.width
> 0)
2420 Refresh( true, &intersect_rect
);
2423 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2427 unsigned int tmp
= to
;
2432 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2433 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2435 wxSize client_size
= GetClientSize();
2436 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2437 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2438 if (intersect_rect
.width
> 0)
2439 Refresh( true, &intersect_rect
);
2442 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2444 wxSize client_size
= GetClientSize();
2445 int start
= GetLineStart( firstRow
);
2446 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2447 if (start
> client_size
.y
) return;
2449 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2451 Refresh( true, &rect
);
2454 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2456 wxCHECK_RET( newCurrent
< GetRowCount(),
2457 wxT("invalid item index in OnArrowChar()") );
2459 // if there is no selection, we cannot move it anywhere
2460 if (!HasCurrentRow())
2463 unsigned int oldCurrent
= m_currentRow
;
2465 // in single selection we just ignore Shift as we can't select several
2467 if ( event
.ShiftDown() && !IsSingleSel() )
2469 RefreshRow( oldCurrent
);
2471 ChangeCurrentRow( newCurrent
);
2473 // select all the items between the old and the new one
2474 if ( oldCurrent
> newCurrent
)
2476 newCurrent
= oldCurrent
;
2477 oldCurrent
= m_currentRow
;
2480 SelectRows( oldCurrent
, newCurrent
, true );
2481 if (oldCurrent
!=newCurrent
)
2482 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2486 RefreshRow( oldCurrent
);
2488 // all previously selected items are unselected unless ctrl is held
2489 if ( !event
.ControlDown() )
2490 SelectAllRows(false);
2492 ChangeCurrentRow( newCurrent
);
2494 if ( !event
.ControlDown() )
2496 SelectRow( m_currentRow
, true );
2497 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2500 RefreshRow( m_currentRow
);
2503 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2506 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2510 rect
.y
= GetLineStart( row
);
2511 rect
.width
= GetEndOfLastCol();
2512 rect
.height
= GetLineHeight( row
);
2517 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2519 const wxDataViewModel
*model
= GetOwner()->GetModel();
2521 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2523 // TODO make more efficient
2528 for (r
= 0; r
< row
; r
++)
2530 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2531 if (!node
) return start
;
2533 wxDataViewItem item
= node
->GetItem();
2535 if (node
&& !node
->HasChildren())
2537 // Yes, if the node does not have any child, it must be a leaf which
2538 // mean that it is a temporarily created by GetTreeNodeByRow
2542 unsigned int cols
= GetOwner()->GetColumnCount();
2544 int height
= m_lineHeight
;
2545 for (col
= 0; col
< cols
; col
++)
2547 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2548 if (column
->IsHidden())
2549 continue; // skip it!
2552 model
->IsContainer(item
) &&
2553 !model
->HasContainerColumns(item
))
2554 continue; // skip it!
2556 wxDataViewRenderer
*renderer
=
2557 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2558 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2560 height
= wxMax( height
, renderer
->GetSize().y
);
2570 return row
* m_lineHeight
;
2574 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2576 const wxDataViewModel
*model
= GetOwner()->GetModel();
2578 // check for the easy case first
2579 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2580 return y
/ m_lineHeight
;
2582 // TODO make more efficient
2583 unsigned int row
= 0;
2584 unsigned int yy
= 0;
2587 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2590 // not really correct...
2591 return row
+ ((y
-yy
) / m_lineHeight
);
2594 wxDataViewItem item
= node
->GetItem();
2596 if (node
&& !node
->HasChildren())
2598 // Yes, if the node does not have any child, it must be a leaf which
2599 // mean that it is a temporarily created by GetTreeNodeByRow
2603 unsigned int cols
= GetOwner()->GetColumnCount();
2605 int height
= m_lineHeight
;
2606 for (col
= 0; col
< cols
; col
++)
2608 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2609 if (column
->IsHidden())
2610 continue; // skip it!
2613 model
->IsContainer(item
) &&
2614 !model
->HasContainerColumns(item
))
2615 continue; // skip it!
2617 wxDataViewRenderer
*renderer
=
2618 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2619 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2621 height
= wxMax( height
, renderer
->GetSize().y
);
2632 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2634 const wxDataViewModel
*model
= GetOwner()->GetModel();
2636 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2638 wxASSERT( !IsVirtualList() );
2640 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2641 // wxASSERT( node );
2642 if (!node
) return m_lineHeight
;
2644 wxDataViewItem item
= node
->GetItem();
2646 if (node
&& !node
->HasChildren())
2648 // Yes, if the node does not have any child, it must be a leaf which
2649 // mean that it is a temporarily created by GetTreeNodeByRow
2653 int height
= m_lineHeight
;
2655 unsigned int cols
= GetOwner()->GetColumnCount();
2657 for (col
= 0; col
< cols
; col
++)
2659 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2660 if (column
->IsHidden())
2661 continue; // skip it!
2664 model
->IsContainer(item
) &&
2665 !model
->HasContainerColumns(item
))
2666 continue; // skip it!
2668 wxDataViewRenderer
*renderer
=
2669 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2670 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2672 height
= wxMax( height
, renderer
->GetSize().y
);
2679 return m_lineHeight
;
2683 class RowToItemJob
: public DoJob
2686 RowToItemJob( unsigned int row
, int current
)
2687 { this->row
= row
; this->current
= current
; }
2688 virtual ~RowToItemJob() {}
2690 virtual int operator() ( wxDataViewTreeNode
* node
)
2693 if( current
== static_cast<int>(row
))
2695 ret
= node
->GetItem();
2699 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2701 current
+= node
->GetSubTreeCount();
2706 // If the current has no child node, we can find the desired item of the row
2708 // This if can speed up finding in some case, and will has a very good effect
2709 // when it comes to list view
2710 if( node
->GetNodes().GetCount() == 0)
2712 int index
= static_cast<int>(row
) - current
- 1;
2713 ret
= node
->GetChildren().Item( index
);
2720 virtual int operator() ( void * n
)
2723 if( current
== static_cast<int>(row
))
2725 ret
= wxDataViewItem( n
);
2731 wxDataViewItem
GetResult() const
2740 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2742 if (IsVirtualList())
2744 return wxDataViewItem( wxUIntToPtr(row
+1) );
2748 RowToItemJob
job( row
, -2 );
2749 Walker( m_root
, job
);
2750 return job
.GetResult();
2754 class RowToTreeNodeJob
: public DoJob
2757 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2760 this->current
= current
;
2764 virtual ~RowToTreeNodeJob(){ }
2766 virtual int operator() ( wxDataViewTreeNode
* node
)
2769 if( current
== static_cast<int>(row
))
2775 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2777 current
+= node
->GetSubTreeCount();
2784 // If the current node has no children, we can find the desired item of the
2785 // row number directly.
2786 // This if can speed up finding in some case, and will have a very good
2787 // effect for list views.
2788 if( node
->GetNodes().GetCount() == 0)
2790 int index
= static_cast<int>(row
) - current
- 1;
2791 void * n
= node
->GetChildren().Item( index
);
2792 ret
= new wxDataViewTreeNode( parent
);
2793 ret
->SetItem( wxDataViewItem( n
));
2794 ret
->SetHasChildren(false);
2801 virtual int operator() ( void * n
)
2804 if( current
== static_cast<int>(row
))
2806 ret
= new wxDataViewTreeNode( parent
);
2807 ret
->SetItem( wxDataViewItem( n
));
2808 ret
->SetHasChildren(false);
2815 wxDataViewTreeNode
* GetResult() const
2821 wxDataViewTreeNode
* ret
;
2822 wxDataViewTreeNode
* parent
;
2825 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2827 wxASSERT( !IsVirtualList() );
2829 RowToTreeNodeJob
job( row
, -2, m_root
);
2830 Walker( m_root
, job
);
2831 return job
.GetResult();
2834 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2835 const wxDataViewItem
& item
)
2837 wxWindow
*parent
= GetParent();
2838 wxDataViewEvent
le(type
, parent
->GetId());
2840 le
.SetEventObject(parent
);
2841 le
.SetModel(GetOwner()->GetModel());
2844 parent
->GetEventHandler()->ProcessEvent(le
);
2848 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2853 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2857 if (!node
->HasChildren())
2863 return node
->IsOpen();
2866 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2871 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2875 if (!node
->HasChildren())
2884 void wxDataViewMainWindow::Expand( unsigned int row
)
2889 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2893 if (!node
->HasChildren())
2899 if (!node
->IsOpen())
2902 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2904 // Check if the user prevent expanding
2905 if( e
.GetSkipped() )
2910 // build the children of current node
2911 if( node
->GetChildrenNumber() == 0 )
2914 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2917 // By expanding the node all row indices that are currently in the selection list
2918 // and are greater than our node have become invalid. So we have to correct that now.
2919 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2920 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2922 const unsigned testRow
= m_selection
[i
];
2923 // all rows above us are not affected, so skip them
2927 m_selection
[i
] += rowAdjustment
;
2930 if(m_currentRow
> row
)
2931 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2935 // Send the expanded event
2936 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2940 void wxDataViewMainWindow::Collapse(unsigned int row
)
2945 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2949 if (!node
->HasChildren())
2958 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2959 if( e
.GetSkipped() )
2962 // Find out if there are selected items below the current node.
2963 bool selectCollapsingRow
= false;
2964 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2965 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2966 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2968 const unsigned testRow
= m_selection
[i
];
2969 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2971 selectCollapsingRow
= true;
2972 // get out as soon as we have found a node that is selected
2979 // If the node to be closed has selected items the user won't see those any longer.
2980 // We select the collapsing node in this case.
2981 if(selectCollapsingRow
)
2983 SelectAllRows(false);
2984 ChangeCurrentRow(row
);
2985 SelectRow(row
, true);
2986 SendSelectionChangedEvent(GetItemByRow(row
));
2990 // if there were no selected items below our node we still need to "fix" the
2991 // selection list to adjust for the changing of the row indices.
2992 // We actually do the opposite of what we are doing in Expand().
2993 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2995 const unsigned testRow
= m_selection
[i
];
2996 // all rows above us are not affected, so skip them
3000 m_selection
[i
] -= rowAdjustment
;
3003 // if the "current row" is being collapsed away we change it to the current row ;-)
3004 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3005 ChangeCurrentRow(row
);
3006 else if(m_currentRow
> row
)
3007 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3012 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3016 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3018 const wxDataViewModel
* model
= GetOwner()->GetModel();
3025 // Compose the parent-chain for the item we are looking for
3026 wxVector
<wxDataViewItem
> parentChain
;
3027 wxDataViewItem
it( item
);
3030 parentChain
.push_back(it
);
3031 it
= model
->GetParent(it
);
3034 // Find the item along the parent-chain.
3035 // This algorithm is designed to speed up the node-finding method
3036 wxDataViewTreeNode
* node
= m_root
;
3037 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3039 if( node
->HasChildren() )
3041 if( node
->GetChildrenNumber() == 0 )
3044 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3047 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3050 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3052 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3053 if (currentNode
->GetItem() == parentChain
[iter
])
3055 if (currentNode
->GetItem() == item
)
3075 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3076 wxDataViewColumn
* &column
)
3078 wxDataViewColumn
*col
= NULL
;
3079 unsigned int cols
= GetOwner()->GetColumnCount();
3080 unsigned int colnum
= 0;
3082 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3083 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3085 col
= GetOwner()->GetColumnAt(colnum
);
3086 if (col
->IsHidden())
3087 continue; // skip it!
3089 unsigned int w
= col
->GetWidth();
3090 if (x_start
+w
>= (unsigned int)x
)
3097 item
= GetItemByRow( GetLineAt( y
) );
3100 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3101 const wxDataViewColumn
* column
)
3106 unsigned int cols
= GetOwner()->GetColumnCount();
3107 // If column is null the loop will compute the combined width of all columns.
3108 // Otherwise, it will compute the x position of the column we are looking for.
3109 for (unsigned int i
= 0; i
< cols
; i
++)
3111 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3116 if (col
->IsHidden())
3117 continue; // skip it!
3119 xpos
+= col
->GetWidth();
3120 width
+= col
->GetWidth();
3125 // If we have a column, we need can get its width directly.
3126 if(column
->IsHidden())
3129 width
= column
->GetWidth();
3134 // If we have no column, we reset the x position back to zero.
3138 // we have to take an expander column into account and compute its indentation
3139 // to get the correct x position where the actual text is
3141 int row
= GetRowByItem(item
);
3142 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3144 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3145 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3146 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3148 if(!node
->HasChildren())
3152 wxRect
itemRect( xpos
+ indent
,
3153 GetLineStart( row
),
3155 GetLineHeight( row
) );
3157 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3158 &itemRect
.x
, &itemRect
.y
);
3163 int wxDataViewMainWindow::RecalculateCount()
3165 if (IsVirtualList())
3167 wxDataViewVirtualListModel
*list_model
=
3168 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3170 return list_model
->GetCount();
3174 return m_root
->GetSubTreeCount();
3178 class ItemToRowJob
: public DoJob
3181 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3188 // Maybe binary search will help to speed up this process
3189 virtual int operator() ( wxDataViewTreeNode
* node
)
3192 if( node
->GetItem() == item
)
3197 if( node
->GetItem() == *m_iter
)
3204 ret
+= node
->GetSubTreeCount();
3210 virtual int operator() ( void * n
)
3213 if( n
== item
.GetID() )
3218 // the row number is begin from zero
3219 int GetResult() const
3223 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3224 wxDataViewItem item
;
3229 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3231 const wxDataViewModel
* model
= GetOwner()->GetModel();
3235 if (IsVirtualList())
3237 return wxPtrToUInt( item
.GetID() ) -1;
3244 // Compose the parent-chain of the item we are looking for
3245 wxVector
<wxDataViewItem
> parentChain
;
3246 wxDataViewItem
it( item
);
3249 parentChain
.push_back(it
);
3250 it
= model
->GetParent(it
);
3253 // add an 'invalid' item to represent our 'invisible' root node
3254 parentChain
.push_back(wxDataViewItem());
3256 // the parent chain was created by adding the deepest parent first.
3257 // so if we want to start at the root node, we have to iterate backwards through the vector
3258 ItemToRowJob
job( item
, parentChain
.rbegin() );
3259 Walker( m_root
, job
);
3260 return job
.GetResult();
3264 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3265 wxDataViewTreeNode
* node
)
3267 if( !model
->IsContainer( item
) )
3270 wxDataViewItemArray children
;
3271 unsigned int num
= model
->GetChildren( item
, children
);
3273 unsigned int index
= 0;
3274 while( index
< num
)
3276 if( model
->IsContainer( children
[index
] ) )
3278 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3279 n
->SetItem(children
[index
]);
3280 n
->SetHasChildren( true );
3285 node
->AddLeaf( children
[index
].GetID() );
3289 node
->SetSubTreeCount( num
);
3290 wxDataViewTreeNode
* n
= node
->GetParent();
3292 n
->ChangeSubTreeCount(num
);
3296 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3300 if (GetOwner()->GetModel()->IsVirtualListModel())
3306 m_root
= new wxDataViewTreeNode( NULL
);
3307 m_root
->SetHasChildren(true);
3309 // First we define a invalid item to fetch the top-level elements
3310 wxDataViewItem item
;
3312 BuildTreeHelper( model
, item
, m_root
);
3316 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3318 if( node
->GetNodeNumber() != 0 )
3320 int len
= node
->GetNodeNumber();
3321 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3322 for (int i
= 0; i
< len
; i
++)
3323 DestroyTreeHelper(nodes
[i
]);
3328 void wxDataViewMainWindow::DestroyTree()
3330 if (!IsVirtualList())
3332 ::DestroyTreeHelper(m_root
);
3338 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3340 wxWindow
* const parent
= GetParent();
3342 // propagate the char event upwards
3343 wxKeyEvent
eventForParent(event
);
3344 eventForParent
.SetEventObject(parent
);
3345 if ( parent
->ProcessWindowEvent(eventForParent
) )
3348 if ( parent
->HandleAsNavigationKey(event
) )
3351 // no item -> nothing to do
3352 if (!HasCurrentRow())
3358 // don't use m_linesPerPage directly as it might not be computed yet
3359 const int pageSize
= GetCountPerPage();
3360 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3362 switch ( event
.GetKeyCode() )
3366 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3368 le
.SetItem( GetItemByRow(m_currentRow
) );
3369 le
.SetEventObject(parent
);
3370 le
.SetModel(GetOwner()->GetModel());
3372 parent
->GetEventHandler()->ProcessEvent(le
);
3377 if ( m_currentRow
> 0 )
3378 OnArrowChar( m_currentRow
- 1, event
);
3382 if ( m_currentRow
+ 1 < GetRowCount() )
3383 OnArrowChar( m_currentRow
+ 1, event
);
3385 // Add the process for tree expanding/collapsing
3391 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3395 if (node
->HasChildren() && node
->IsOpen())
3397 Collapse(m_currentRow
);
3399 else // if the node is already closed we move the selection to its parent
3401 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3403 if(!node
->HasChildren())
3408 int parent
= GetRowByItem( parent_node
->GetItem() );
3411 unsigned int row
= m_currentRow
;
3412 SelectRow( row
, false);
3413 SelectRow( parent
, true );
3414 ChangeCurrentRow( parent
);
3415 GetOwner()->EnsureVisible( parent
, -1 );
3416 SendSelectionChangedEvent( parent_node
->GetItem() );
3424 if (!IsExpanded( m_currentRow
))
3425 Expand( m_currentRow
);
3428 unsigned int row
= m_currentRow
;
3429 SelectRow( row
, false );
3430 SelectRow( row
+ 1, true );
3431 ChangeCurrentRow( row
+ 1 );
3432 GetOwner()->EnsureVisible( row
+ 1, -1 );
3433 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3440 OnArrowChar( GetRowCount() - 1, event
);
3445 OnArrowChar( 0, event
);
3450 int steps
= pageSize
- 1;
3451 int index
= m_currentRow
- steps
;
3455 OnArrowChar( index
, event
);
3461 int steps
= pageSize
- 1;
3462 unsigned int index
= m_currentRow
+ steps
;
3463 unsigned int count
= GetRowCount();
3464 if ( index
>= count
)
3467 OnArrowChar( index
, event
);
3473 if(m_selection
.size() == 1)
3475 // TODO: we need to revise that when we have a concept for a 'current column'
3476 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3486 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3488 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3490 // let the base handle mouse wheel events.
3495 // set the focus to ourself if any of the mouse buttons are pressed
3496 if(event
.ButtonDown() && !HasFocus())
3499 int x
= event
.GetX();
3500 int y
= event
.GetY();
3501 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3502 wxDataViewColumn
*col
= NULL
;
3505 unsigned int cols
= GetOwner()->GetColumnCount();
3507 for (i
= 0; i
< cols
; i
++)
3509 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3511 continue; // skip it!
3513 if (x
< xpos
+ c
->GetWidth())
3518 xpos
+= c
->GetWidth();
3526 wxDataViewRenderer
*cell
= col
->GetRenderer();
3527 unsigned int current
= GetLineAt( y
);
3528 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3530 // Unselect all if below the last row ?
3535 // Test whether the mouse is hovered on the tree item button
3536 bool hoverOverExpander
= false;
3537 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3539 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3540 if( node
!=NULL
&& node
->HasChildren() )
3542 int indent
= node
->GetIndentLevel();
3543 indent
= GetOwner()->GetIndent()*indent
;
3545 // we make the rectangle we are looking in a bit bigger than the actual
3546 // visual expander so the user can hit that little thing reliably
3547 wxRect
rect( xpos
+ indent
,
3548 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3549 m_lineHeight
, m_lineHeight
);
3550 if( rect
.Contains(x
, y
) )
3552 // So the mouse is over the expander
3553 hoverOverExpander
= true;
3554 if (m_underMouse
&& m_underMouse
!= node
)
3556 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3557 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3559 if (m_underMouse
!= node
)
3561 // wxLogMessage("Do the row: %d", current);
3562 RefreshRow(current
);
3564 m_underMouse
= node
;
3567 if (node
!=NULL
&& !node
->HasChildren())
3570 if (!hoverOverExpander
)
3572 if (m_underMouse
!= NULL
)
3574 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3575 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3576 m_underMouse
= NULL
;
3580 wxDataViewModel
*model
= GetOwner()->GetModel();
3582 #if wxUSE_DRAG_AND_DROP
3583 if (event
.Dragging())
3585 if (m_dragCount
== 0)
3587 // we have to report the raw, physical coords as we want to be
3588 // able to call HitTest(event.m_pointDrag) from the user code to
3589 // get the item being dragged
3590 m_dragStart
= event
.GetPosition();
3595 if (m_dragCount
!= 3)
3598 if (event
.LeftIsDown())
3600 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3601 &m_dragStart
.x
, &m_dragStart
.y
);
3602 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3603 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3605 // Notify cell about drag
3606 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3607 event
.SetEventObject( m_owner
);
3608 event
.SetItem( item
);
3609 event
.SetModel( model
);
3610 if (!m_owner
->HandleWindowEvent( event
))
3613 if (!event
.IsAllowed())
3616 wxDataObject
*obj
= event
.GetDataObject();
3620 wxDataViewDropSource
drag( this, drag_item_row
);
3621 drag
.SetData( *obj
);
3622 /* wxDragResult res = */ drag
.DoDragDrop();
3631 #endif // wxUSE_DRAG_AND_DROP
3633 bool simulateClick
= false;
3635 if (event
.ButtonDClick())
3637 m_renameTimer
->Stop();
3638 m_lastOnSame
= false;
3641 wxDataViewItem item
= GetItemByRow(current
);
3642 bool ignore_other_columns
=
3643 ((GetOwner()->GetExpanderColumn() != col
) &&
3644 (model
->IsContainer(item
)) &&
3645 (!model
->HasContainerColumns(item
)));
3647 if (event
.LeftDClick())
3649 if(hoverOverExpander
)
3651 // a double click on the expander will be converted into a "simulated" normal click
3652 simulateClick
= true;
3654 else if ( current
== m_lineLastClicked
)
3656 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3658 const unsigned colIdx
= col
->GetModelColumn();
3660 cell
->PrepareForItem(model
, item
, colIdx
);
3662 wxRect
cell_rect( xpos
, GetLineStart( current
),
3663 col
->GetWidth(), GetLineHeight( current
) );
3664 cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3668 wxWindow
*parent
= GetParent();
3669 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3671 le
.SetColumn( col
->GetModelColumn() );
3672 le
.SetDataViewColumn( col
);
3673 le
.SetEventObject(parent
);
3674 le
.SetModel(GetOwner()->GetModel());
3676 parent
->GetEventHandler()->ProcessEvent(le
);
3682 // The first click was on another item, so don't interpret this as
3683 // a double click, but as a simple click instead
3684 simulateClick
= true;
3688 if (event
.LeftUp() && !hoverOverExpander
)
3690 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3692 // select single line
3693 SelectAllRows( false );
3694 SelectRow( m_lineSelectSingleOnUp
, true );
3695 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3698 // If the user click the expander, we do not do editing even if the column
3699 // with expander are editable
3700 if (m_lastOnSame
&& !ignore_other_columns
)
3702 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3703 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3705 m_renameTimer
->Start( 100, true );
3709 m_lastOnSame
= false;
3710 m_lineSelectSingleOnUp
= (unsigned int)-1;
3712 else if(!event
.LeftUp())
3714 // This is necessary, because after a DnD operation in
3715 // from and to ourself, the up event is swallowed by the
3716 // DnD code. So on next non-up event (which means here and
3717 // now) m_lineSelectSingleOnUp should be reset.
3718 m_lineSelectSingleOnUp
= (unsigned int)-1;
3721 if (event
.RightDown())
3723 m_lineBeforeLastClicked
= m_lineLastClicked
;
3724 m_lineLastClicked
= current
;
3726 // If the item is already selected, do not update the selection.
3727 // Multi-selections should not be cleared if a selected item is clicked.
3728 if (!IsRowSelected(current
))
3730 SelectAllRows(false);
3731 const unsigned oldCurrent
= m_currentRow
;
3732 ChangeCurrentRow(current
);
3733 SelectRow(m_currentRow
,true);
3734 RefreshRow(oldCurrent
);
3735 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3738 else if (event
.RightUp())
3741 model
->GetValue( value
, item
, col
->GetModelColumn() );
3742 wxWindow
*parent
= GetParent();
3743 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3745 le
.SetColumn( col
->GetModelColumn() );
3746 le
.SetDataViewColumn( col
);
3747 le
.SetEventObject(parent
);
3748 le
.SetModel(GetOwner()->GetModel());
3750 parent
->GetEventHandler()->ProcessEvent(le
);
3752 else if (event
.MiddleDown())
3756 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3758 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3760 // hoverOverExpander being true tells us that our node must be
3761 // valid and have children.
3762 // So we don't need any extra checks.
3763 if( node
->IsOpen() )
3768 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3770 m_lineBeforeLastClicked
= m_lineLastClicked
;
3771 m_lineLastClicked
= current
;
3773 unsigned int oldCurrentRow
= m_currentRow
;
3774 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3776 bool cmdModifierDown
= event
.CmdDown();
3777 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3779 if ( IsSingleSel() || !IsRowSelected(current
) )
3781 SelectAllRows( false );
3782 ChangeCurrentRow(current
);
3783 SelectRow(m_currentRow
,true);
3784 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3786 else // multi sel & current is highlighted & no mod keys
3788 m_lineSelectSingleOnUp
= current
;
3789 ChangeCurrentRow(current
); // change focus
3792 else // multi sel & either ctrl or shift is down
3794 if (cmdModifierDown
)
3796 ChangeCurrentRow(current
);
3797 ReverseRowSelection(m_currentRow
);
3798 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3800 else if (event
.ShiftDown())
3802 ChangeCurrentRow(current
);
3804 unsigned int lineFrom
= oldCurrentRow
,
3807 if ( lineTo
< lineFrom
)
3810 lineFrom
= m_currentRow
;
3813 SelectRows(lineFrom
, lineTo
, true);
3814 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3816 else // !ctrl, !shift
3818 // test in the enclosing if should make it impossible
3819 wxFAIL_MSG( wxT("how did we get here?") );
3823 if (m_currentRow
!= oldCurrentRow
)
3824 RefreshRow( oldCurrentRow
);
3826 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3828 // Update selection here...
3831 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3832 (current
== oldCurrentRow
)) && oldWasSelected
;
3834 // Call LeftClick after everything else as under GTK+
3835 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3837 // notify cell about click
3838 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3840 wxRect
cell_rect( xpos
, GetLineStart( current
),
3841 col
->GetWidth(), GetLineHeight( current
) );
3843 // Report position relative to the cell's custom area, i.e.
3844 // no the entire space as given by the control but the one
3845 // used by the renderer after calculation of alignment etc.
3847 // adjust the rectangle ourselves to account for the alignment
3848 wxRect rectItem
= cell_rect
;
3849 const int align
= cell
->GetAlignment();
3850 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3852 const wxSize size
= cell
->GetSize();
3854 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3856 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3857 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3858 else if ( align
& wxALIGN_RIGHT
)
3859 rectItem
.x
+= cell_rect
.width
- size
.x
;
3860 // else: wxALIGN_LEFT is the default
3863 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3865 if ( align
& wxALIGN_CENTER_VERTICAL
)
3866 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3867 else if ( align
& wxALIGN_BOTTOM
)
3868 rectItem
.y
+= cell_rect
.height
- size
.y
;
3869 // else: wxALIGN_TOP is the default
3873 wxPoint
pos( event
.GetPosition() );
3874 pos
.x
-= rectItem
.x
;
3875 pos
.y
-= rectItem
.y
;
3877 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3879 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3880 model
, item
, col
->GetModelColumn());
3885 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3889 if (HasCurrentRow())
3895 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3899 if (HasCurrentRow())
3905 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3907 if( m_selection
.GetCount() != 1 )
3908 return wxDataViewItem();
3910 return GetItemByRow( m_selection
.Item(0));
3913 //-----------------------------------------------------------------------------
3915 //-----------------------------------------------------------------------------
3917 WX_DEFINE_LIST(wxDataViewColumnList
)
3919 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3920 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3921 EVT_SIZE(wxDataViewCtrl::OnSize
)
3924 wxDataViewCtrl::~wxDataViewCtrl()
3927 GetModel()->RemoveNotifier( m_notifier
);
3930 m_colsBestWidths
.clear();
3933 void wxDataViewCtrl::Init()
3935 m_cols
.DeleteContents(true);
3938 // No sorting column at start
3939 m_sortingColumnIdx
= wxNOT_FOUND
;
3941 m_headerArea
= NULL
;
3944 bool wxDataViewCtrl::Create(wxWindow
*parent
,
3949 const wxValidator
& validator
,
3950 const wxString
& name
)
3952 // if ( (style & wxBORDER_MASK) == 0)
3953 // style |= wxBORDER_SUNKEN;
3957 if (!wxControl::Create( parent
, id
, pos
, size
,
3958 style
| wxScrolledWindowStyle
, validator
, name
))
3961 SetInitialSize(size
);
3964 MacSetClipChildren( true );
3967 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3969 // We use the cursor keys for moving the selection, not scrolling, so call
3970 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3971 // keyboard events forwarded to us from wxListMainWindow.
3972 DisableKeyboardScrolling();
3974 if (HasFlag(wxDV_NO_HEADER
))
3975 m_headerArea
= NULL
;
3977 m_headerArea
= new wxDataViewHeaderWindow(this);
3979 SetTargetWindow( m_clientArea
);
3981 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3983 sizer
->Add( m_headerArea
, 0, wxGROW
);
3984 sizer
->Add( m_clientArea
, 1, wxGROW
);
3990 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3992 return wxBORDER_THEME
;
3996 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4000 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4003 // we need to process arrows ourselves for scrolling
4004 if ( nMsg
== WM_GETDLGCODE
)
4006 rc
|= DLGC_WANTARROWS
;
4014 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4016 wxSize newsize
= size
;
4017 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4018 newsize
.y
-= m_headerArea
->GetSize().y
;
4023 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4025 // We need to override OnSize so that our scrolled
4026 // window a) does call Layout() to use sizers for
4027 // positioning the controls but b) does not query
4028 // the sizer for their size and use that for setting
4029 // the scrollable area as set that ourselves by
4030 // calling SetScrollbar() further down.
4036 // We must redraw the headers if their height changed. Normally this
4037 // shouldn't happen as the control shouldn't let itself be resized beneath
4038 // its minimal height but avoid the display artefacts that appear if it
4039 // does happen, e.g. because there is really not enough vertical space.
4040 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4041 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4043 m_headerArea
->Refresh();
4047 void wxDataViewCtrl::SetFocus()
4050 m_clientArea
->SetFocus();
4053 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4055 if (!wxDataViewCtrlBase::AssociateModel( model
))
4058 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4060 model
->AddNotifier( m_notifier
);
4062 m_clientArea
->DestroyTree();
4064 m_clientArea
->BuildTree(model
);
4066 m_clientArea
->UpdateDisplay();
4071 #if wxUSE_DRAG_AND_DROP
4073 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4075 return m_clientArea
->EnableDragSource( format
);
4078 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4080 return m_clientArea
->EnableDropTarget( format
);
4083 #endif // wxUSE_DRAG_AND_DROP
4085 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4087 if (!wxDataViewCtrlBase::AppendColumn(col
))
4090 m_cols
.Append( col
);
4091 m_colsBestWidths
.push_back(0);
4092 OnColumnsCountChanged();
4096 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4098 if (!wxDataViewCtrlBase::PrependColumn(col
))
4101 m_cols
.Insert( col
);
4102 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4103 OnColumnsCountChanged();
4107 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4109 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4112 m_cols
.Insert( pos
, col
);
4113 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4114 OnColumnsCountChanged();
4118 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4121 m_headerArea
->UpdateColumn(idx
);
4123 m_clientArea
->UpdateDisplay();
4126 void wxDataViewCtrl::OnColumnsCountChanged()
4129 m_headerArea
->SetColumnCount(GetColumnCount());
4131 m_clientArea
->UpdateDisplay();
4134 void wxDataViewCtrl::DoSetExpanderColumn()
4136 m_clientArea
->UpdateDisplay();
4139 void wxDataViewCtrl::DoSetIndent()
4141 m_clientArea
->UpdateDisplay();
4144 unsigned int wxDataViewCtrl::GetColumnCount() const
4146 return m_cols
.GetCount();
4149 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4151 if ( !m_clientArea
)
4154 m_clientArea
->SetRowHeight(lineHeight
);
4159 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4164 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4166 // columns can't be reordered if there is no header window which allows
4168 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4171 return GetColumn(idx
);
4174 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4176 const unsigned count
= m_cols
.size();
4177 for ( unsigned n
= 0; n
< count
; n
++ )
4179 if ( m_cols
[n
] == column
)
4186 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4188 if ( m_colsBestWidths
[idx
] != 0 )
4189 return m_colsBestWidths
[idx
];
4191 const int count
= m_clientArea
->GetRowCount();
4192 wxDataViewColumn
*column
= GetColumn(idx
);
4193 wxDataViewRenderer
*renderer
=
4194 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4196 class MaxWidthCalculator
4199 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4200 wxDataViewRenderer
*renderer
,
4201 const wxDataViewModel
*model
,
4204 m_clientArea(clientArea
),
4205 m_renderer(renderer
),
4211 void UpdateWithWidth(int width
)
4213 m_width
= wxMax(m_width
, width
);
4216 void UpdateWithRow(int row
)
4218 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4219 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4220 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4223 int GetMaxWidth() const { return m_width
; }
4227 wxDataViewMainWindow
*m_clientArea
;
4228 wxDataViewRenderer
*m_renderer
;
4229 const wxDataViewModel
*m_model
;
4233 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4234 GetModel(), column
->GetModelColumn());
4238 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4239 // Labels on native MSW header are indented on both sides
4241 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4242 calculator
.UpdateWithWidth(header_width
);
4245 // The code below deserves some explanation. For very large controls, we
4246 // simply can't afford to calculate sizes for all items, it takes too
4247 // long. So the best we can do is to check the first and the last N/2
4248 // items in the control for some sufficiently large N and calculate best
4249 // sizes from that. That can result in the calculated best width being too
4250 // small for some outliers, but it's better to get slightly imperfect
4251 // result than to wait several seconds after every update. To avoid highly
4252 // visible miscalculations, we also include all currently visible items
4253 // no matter what. Finally, the value of N is determined dynamically by
4254 // measuring how much time we spent on the determining item widths so far.
4257 int top_part_end
= count
;
4258 static const long CALC_TIMEOUT
= 20/*ms*/;
4259 // don't call wxStopWatch::Time() too often
4260 static const unsigned CALC_CHECK_FREQ
= 100;
4263 // use some hard-coded limit, that's the best we can do without timer
4264 int top_part_end
= wxMin(500, count
);
4265 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4269 for ( row
= 0; row
< top_part_end
; row
++ )
4272 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4273 timer
.Time() > CALC_TIMEOUT
)
4275 #endif // wxUSE_STOPWATCH
4276 calculator
.UpdateWithRow(row
);
4279 // row is the first unmeasured item now; that's our value of N/2
4285 // add bottom N/2 items now:
4286 const int bottom_part_start
= wxMax(row
, count
- row
);
4287 for ( row
= bottom_part_start
; row
< count
; row
++ )
4289 calculator
.UpdateWithRow(row
);
4292 // finally, include currently visible items in the calculation:
4293 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4294 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4295 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4297 first_visible
= wxMax(first_visible
, top_part_end
);
4298 last_visible
= wxMin(bottom_part_start
, last_visible
);
4300 for ( row
= first_visible
; row
< last_visible
; row
++ )
4302 calculator
.UpdateWithRow(row
);
4305 wxLogTrace("dataview",
4306 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4308 count
- bottom_part_start
,
4309 wxMax(0, last_visible
- first_visible
),
4313 int max_width
= calculator
.GetMaxWidth();
4314 if ( max_width
> 0 )
4315 max_width
+= 2 * PADDING_RIGHTLEFT
;
4317 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4321 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4322 unsigned int WXUNUSED(new_pos
))
4324 // do _not_ reorder m_cols elements here, they should always be in the
4325 // order in which columns were added, we only display the columns in
4327 m_clientArea
->UpdateDisplay();
4330 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4332 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4336 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4338 OnColumnsCountChanged();
4343 bool wxDataViewCtrl::ClearColumns()
4346 m_colsBestWidths
.clear();
4347 OnColumnsCountChanged();
4351 void wxDataViewCtrl::InvalidateColBestWidth(int idx
)
4353 m_colsBestWidths
[idx
] = 0;
4356 m_headerArea
->UpdateColumn(idx
);
4359 void wxDataViewCtrl::InvalidateColBestWidths()
4361 m_colsBestWidths
.clear();
4362 m_colsBestWidths
.resize(m_cols
.size());
4366 const unsigned cols
= m_headerArea
->GetColumnCount();
4367 for ( unsigned i
= 0; i
< cols
; i
++ )
4368 m_headerArea
->UpdateColumn(i
);
4372 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4375 unsigned int len
= GetColumnCount();
4376 for ( unsigned int i
= 0; i
< len
; i
++ )
4378 wxDataViewColumn
* col
= GetColumnAt(i
);
4385 // This returns the position in pixels which is not what we want.
4388 unsigned int len
= GetColumnCount();
4389 for ( unsigned int i
= 0; i
< len
; i
++ )
4391 wxDataViewColumn
* col
= GetColumnAt(i
);
4392 if (col
->IsHidden())
4394 ret
+= col
->GetWidth();
4397 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4405 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4407 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4408 : GetColumn(m_sortingColumnIdx
);
4411 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4413 return GetItemByRow(m_clientArea
->GetCurrentRow());
4416 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4418 const int row
= m_clientArea
->GetRowByItem(item
);
4420 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4421 if ( static_cast<unsigned>(row
) != oldCurrent
)
4423 m_clientArea
->ChangeCurrentRow(row
);
4424 m_clientArea
->RefreshRow(oldCurrent
);
4425 m_clientArea
->RefreshRow(row
);
4429 // Selection code with wxDataViewItem as parameters
4430 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4432 return m_clientArea
->GetSelection();
4435 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4438 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4439 int len
= selection
.GetCount();
4440 for( int i
= 0; i
< len
; i
++)
4442 unsigned int row
= selection
[i
];
4443 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4448 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4450 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4452 wxDataViewItem last_parent
;
4454 int len
= sel
.GetCount();
4455 for( int i
= 0; i
< len
; i
++ )
4457 wxDataViewItem item
= sel
[i
];
4458 wxDataViewItem parent
= GetModel()->GetParent( item
);
4461 if (parent
!= last_parent
)
4462 ExpandAncestors(item
);
4465 last_parent
= parent
;
4466 int row
= m_clientArea
->GetRowByItem( item
);
4468 selection
.Add( static_cast<unsigned int>(row
) );
4471 m_clientArea
->SetSelections( selection
);
4474 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4476 ExpandAncestors( item
);
4478 int row
= m_clientArea
->GetRowByItem( item
);
4481 // Unselect all rows before select another in the single select mode
4482 if (m_clientArea
->IsSingleSel())
4483 m_clientArea
->SelectAllRows(false);
4485 m_clientArea
->SelectRow(row
, true);
4487 // Also set focus to the selected item
4488 m_clientArea
->ChangeCurrentRow( row
);
4492 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4494 int row
= m_clientArea
->GetRowByItem( item
);
4496 m_clientArea
->SelectRow(row
, false);
4499 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4501 int row
= m_clientArea
->GetRowByItem( item
);
4504 return m_clientArea
->IsRowSelected(row
);
4509 // Selection code with row number as parameter
4510 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4513 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4514 int len
= selection
.GetCount();
4515 for( int i
= 0; i
< len
; i
++)
4517 unsigned int row
= selection
[i
];
4523 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4525 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4526 int len
= sel
.GetCount();
4527 for( int i
= 0; i
< len
; i
++ )
4531 selection
.Add( static_cast<unsigned int>(row
) );
4533 m_clientArea
->SetSelections( selection
);
4536 void wxDataViewCtrl::Select( int row
)
4540 if (m_clientArea
->IsSingleSel())
4541 m_clientArea
->SelectAllRows(false);
4542 m_clientArea
->SelectRow( row
, true );
4546 void wxDataViewCtrl::Unselect( int row
)
4549 m_clientArea
->SelectRow(row
, false);
4552 bool wxDataViewCtrl::IsSelected( int row
) const
4555 return m_clientArea
->IsRowSelected(row
);
4559 void wxDataViewCtrl::SelectRange( int from
, int to
)
4562 for( int i
= from
; i
< to
; i
++ )
4564 m_clientArea
->Select(sel
);
4567 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4569 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4570 for( int i
= from
; i
< to
; i
++ )
4571 if( sel
.Index( i
) != wxNOT_FOUND
)
4573 m_clientArea
->SetSelections(sel
);
4576 void wxDataViewCtrl::SelectAll()
4578 m_clientArea
->SelectAllRows(true);
4581 void wxDataViewCtrl::UnselectAll()
4583 m_clientArea
->SelectAllRows(false);
4586 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4590 if( row
> (int) m_clientArea
->GetRowCount() )
4591 row
= m_clientArea
->GetRowCount();
4593 int first
= m_clientArea
->GetFirstVisibleRow();
4594 int last
= m_clientArea
->GetLastVisibleRow();
4596 m_clientArea
->ScrollTo( row
, column
);
4597 else if( row
> last
)
4598 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4600 m_clientArea
->ScrollTo( first
, column
);
4603 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4605 ExpandAncestors( item
);
4607 m_clientArea
->RecalculateDisplay();
4609 int row
= m_clientArea
->GetRowByItem(item
);
4612 if( column
== NULL
)
4613 EnsureVisible(row
, -1);
4615 EnsureVisible( row
, GetColumnIndex(column
) );
4620 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4621 wxDataViewColumn
* &column
) const
4623 m_clientArea
->HitTest(point
, item
, column
);
4626 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4627 const wxDataViewColumn
* column
) const
4629 return m_clientArea
->GetItemRect(item
, column
);
4632 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4634 return m_clientArea
->GetItemByRow( row
);
4637 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4639 return m_clientArea
->GetRowByItem( item
);
4642 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4644 ExpandAncestors( item
);
4646 int row
= m_clientArea
->GetRowByItem( item
);
4648 m_clientArea
->Expand(row
);
4651 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4653 int row
= m_clientArea
->GetRowByItem( item
);
4655 m_clientArea
->Collapse(row
);
4658 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4660 int row
= m_clientArea
->GetRowByItem( item
);
4662 return m_clientArea
->IsExpanded(row
);
4666 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4668 wxDataViewColumn
* col
= GetColumn( column
);
4672 wxRect itemRect
= GetItemRect(item
, col
);
4673 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4674 if (renderer
->GetMode() == wxDATAVIEW_CELL_EDITABLE
)
4675 renderer
->StartEditing(item
, itemRect
);
4678 #endif // !wxUSE_GENERICDATAVIEWCTRL
4680 #endif // wxUSE_DATAVIEWCTRL