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
);
391 // returns node corresponding to 'item' if its in m_nodes or NULL otherwise
392 wxDataViewTreeNode
*FindItemAsNode(const wxDataViewItem
& item
) const
394 for ( wxDataViewTreeNodes::const_iterator i
= m_nodes
.begin();
398 if( (*i
)->GetItem() == item
)
407 wxDataViewTreeNode
*m_parent
;
408 wxDataViewTreeNodes m_nodes
;
409 wxDataViewTreeLeaves m_leaves
;
410 wxDataViewItem m_item
;
416 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
417 wxDataViewTreeNode
** node2
)
419 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
422 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
424 return g_model
->Compare( wxDataViewItem(*id1
), wxDataViewItem(*id2
), g_column
, g_asending
);
428 //-----------------------------------------------------------------------------
429 // wxDataViewMainWindow
430 //-----------------------------------------------------------------------------
432 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
);
434 class wxDataViewMainWindow
: public wxWindow
437 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
439 const wxPoint
&pos
= wxDefaultPosition
,
440 const wxSize
&size
= wxDefaultSize
,
441 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
442 virtual ~wxDataViewMainWindow();
444 bool IsList() const { return GetOwner()->GetModel()->IsListModel(); }
445 bool IsVirtualList() const { return m_root
== NULL
; }
447 // notifications from wxDataViewModel
448 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
449 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
450 bool ItemChanged( const wxDataViewItem
&item
);
451 bool ValueChanged( const wxDataViewItem
&item
, unsigned int model_column
);
455 if (!IsVirtualList())
465 g_model
= GetOwner()->GetModel();
466 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
469 if (g_model
->HasDefaultCompare())
477 g_column
= col
->GetModelColumn();
478 g_asending
= col
->IsSortOrderAscending();
481 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
482 wxDataViewCtrl
*GetOwner() { return m_owner
; }
483 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
485 #if wxUSE_DRAG_AND_DROP
486 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
487 #endif // wxUSE_DRAG_AND_DROP
488 void OnPaint( wxPaintEvent
&event
);
489 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
490 void OnChar( wxKeyEvent
&event
);
491 void OnMouse( wxMouseEvent
&event
);
492 void OnSetFocus( wxFocusEvent
&event
);
493 void OnKillFocus( wxFocusEvent
&event
);
495 void UpdateDisplay();
496 void RecalculateDisplay();
497 void OnInternalIdle();
499 void OnRenameTimer();
501 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
502 void ScrollTo( int rows
, int column
);
504 unsigned GetCurrentRow() const { return m_currentRow
; }
505 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
506 void ChangeCurrentRow( unsigned int row
);
508 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
509 bool IsEmpty() { return GetRowCount() == 0; }
511 int GetCountPerPage() const;
512 int GetEndOfLastCol() const;
513 unsigned int GetFirstVisibleRow() const;
515 // I change this method to un const because in the tree view,
516 // the displaying number of the tree are changing along with the
517 // expanding/collapsing of the tree nodes
518 unsigned int GetLastVisibleRow();
519 unsigned int GetRowCount();
521 wxDataViewItem
GetSelection() const;
522 const wxDataViewSelection
& GetSelections() const { return m_selection
; }
523 void SetSelections( const wxDataViewSelection
& sel
)
524 { m_selection
= sel
; UpdateDisplay(); }
525 void Select( const wxArrayInt
& aSelections
);
526 void SelectAllRows( bool on
);
527 void SelectRow( unsigned int row
, bool on
);
528 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
529 void ReverseRowSelection( unsigned int row
);
530 bool IsRowSelected( unsigned int row
);
531 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
533 void RefreshRow( unsigned int row
);
534 void RefreshRows( unsigned int from
, unsigned int to
);
535 void RefreshRowsAfter( unsigned int firstRow
);
537 // returns the colour to be used for drawing the rules
538 wxColour
GetRuleColour() const
540 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
543 wxRect
GetLineRect( unsigned int row
) const;
545 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
546 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
547 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
549 void SetRowHeight( int lineHeight
) { m_lineHeight
= lineHeight
; }
551 // Some useful functions for row and item mapping
552 wxDataViewItem
GetItemByRow( unsigned int row
) const;
553 int GetRowByItem( const wxDataViewItem
& item
) const;
555 // Methods for building the mapping tree
556 void BuildTree( wxDataViewModel
* model
);
558 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
559 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
561 void Expand( unsigned int row
);
562 void Collapse( unsigned int row
);
563 bool IsExpanded( unsigned int row
) const;
564 bool HasChildren( unsigned int row
) const;
566 #if wxUSE_DRAG_AND_DROP
567 bool EnableDragSource( const wxDataFormat
&format
);
568 bool EnableDropTarget( const wxDataFormat
&format
);
570 void RemoveDropHint();
571 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
572 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
573 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
575 #endif // wxUSE_DRAG_AND_DROP
578 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
579 // We did not need this temporarily
580 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
582 int RecalculateCount();
584 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
586 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
589 wxDataViewCtrl
*m_owner
;
593 wxDataViewColumn
*m_currentCol
;
594 unsigned int m_currentRow
;
595 wxDataViewSelection m_selection
;
597 wxDataViewRenameTimer
*m_renameTimer
;
602 #if wxUSE_DRAG_AND_DROP
607 wxDataFormat m_dragFormat
;
610 wxDataFormat m_dropFormat
;
612 unsigned int m_dropHintLine
;
613 #endif // wxUSE_DRAG_AND_DROP
615 // for double click logic
616 unsigned int m_lineLastClicked
,
617 m_lineBeforeLastClicked
,
618 m_lineSelectSingleOnUp
;
620 // the pen used to draw horiz/vertical rules
623 // the pen used to draw the expander and the lines
626 // This is the tree structure of the model
627 wxDataViewTreeNode
* m_root
;
630 // This is the tree node under the cursor
631 wxDataViewTreeNode
* m_underMouse
;
634 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
635 DECLARE_EVENT_TABLE()
638 // ---------------------------------------------------------
639 // wxGenericDataViewModelNotifier
640 // ---------------------------------------------------------
642 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
645 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
646 { m_mainWindow
= mainWindow
; }
648 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
649 { return m_mainWindow
->ItemAdded( parent
, item
); }
650 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
651 { return m_mainWindow
->ItemDeleted( parent
, item
); }
652 virtual bool ItemChanged( const wxDataViewItem
& item
)
653 { return m_mainWindow
->ItemChanged(item
); }
654 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
655 { return m_mainWindow
->ValueChanged( item
, col
); }
656 virtual bool Cleared()
657 { return m_mainWindow
->Cleared(); }
658 virtual void Resort()
659 { m_mainWindow
->Resort(); }
661 wxDataViewMainWindow
*m_mainWindow
;
664 // ---------------------------------------------------------
665 // wxDataViewRenderer
666 // ---------------------------------------------------------
668 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
670 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
671 wxDataViewCellMode mode
,
673 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
677 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
681 wxDataViewRenderer::~wxDataViewRenderer()
686 wxDC
*wxDataViewRenderer::GetDC()
690 if (GetOwner() == NULL
)
692 if (GetOwner()->GetOwner() == NULL
)
694 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
700 void wxDataViewRenderer::SetAlignment( int align
)
705 int wxDataViewRenderer::GetAlignment() const
710 // ---------------------------------------------------------
711 // wxDataViewCustomRenderer
712 // ---------------------------------------------------------
714 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
716 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
717 wxDataViewCellMode mode
, int align
) :
718 wxDataViewRenderer( varianttype
, mode
, align
)
722 // ---------------------------------------------------------
723 // wxDataViewTextRenderer
724 // ---------------------------------------------------------
726 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
728 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
729 wxDataViewCellMode mode
, int align
) :
730 wxDataViewRenderer( varianttype
, mode
, align
)
734 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
736 m_text
= value
.GetString();
741 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
746 bool wxDataViewTextRenderer::HasEditorCtrl() const
751 wxWindow
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
752 wxRect labelRect
, const wxVariant
&value
)
754 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
755 wxPoint(labelRect
.x
,labelRect
.y
),
756 wxSize(labelRect
.width
,labelRect
.height
) );
758 // select the text in the control an place the cursor at the end
759 ctrl
->SetInsertionPointEnd();
765 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
&value
)
767 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
768 value
= text
->GetValue();
772 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
774 RenderText(m_text
, 0, rect
, dc
, state
);
778 wxSize
wxDataViewTextRenderer::GetSize() const
781 return GetTextExtent(m_text
);
783 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
786 // ---------------------------------------------------------
787 // wxDataViewBitmapRenderer
788 // ---------------------------------------------------------
790 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
792 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
793 wxDataViewCellMode mode
, int align
) :
794 wxDataViewRenderer( varianttype
, mode
, align
)
798 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
800 if (value
.GetType() == wxT("wxBitmap"))
802 if (value
.GetType() == wxT("wxIcon"))
808 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
813 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
816 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
817 else if (m_icon
.IsOk())
818 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
823 wxSize
wxDataViewBitmapRenderer::GetSize() const
826 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
827 else if (m_icon
.IsOk())
828 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
830 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
833 // ---------------------------------------------------------
834 // wxDataViewToggleRenderer
835 // ---------------------------------------------------------
837 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
839 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
840 wxDataViewCellMode mode
, int align
) :
841 wxDataViewRenderer( varianttype
, mode
, align
)
846 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
848 m_toggle
= value
.GetBool();
853 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
858 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
862 flags
|= wxCONTROL_CHECKED
;
863 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
||
864 GetEnabled() == false)
865 flags
|= wxCONTROL_DISABLED
;
867 // check boxes we draw must always have the same, standard size (if it's
868 // bigger than the cell size the checkbox will be truncated because the
869 // caller had set the clipping rectangle to prevent us from drawing outside
871 cell
.SetSize(GetSize());
873 wxRendererNative::Get().DrawCheckBox(
874 GetOwner()->GetOwner(),
882 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint
& cursor
,
883 const wxRect
& WXUNUSED(cell
),
884 wxDataViewModel
*model
,
885 const wxDataViewItem
& item
,
888 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
889 if (!wxRect(GetSize()).Contains(cursor
))
892 if (model
->IsEnabled(item
, col
))
894 model
->ChangeValue(!m_toggle
, item
, col
);
901 wxSize
wxDataViewToggleRenderer::GetSize() const
903 // the window parameter is not used by GetCheckBoxSize() so it's
905 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
908 // ---------------------------------------------------------
909 // wxDataViewProgressRenderer
910 // ---------------------------------------------------------
912 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
914 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
915 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
916 wxDataViewRenderer( varianttype
, mode
, align
)
922 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
924 m_value
= (long) value
;
926 if (m_value
< 0) m_value
= 0;
927 if (m_value
> 100) m_value
= 100;
932 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
934 value
= (long) m_value
;
939 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
941 // deflate the rect to leave a small border between bars in adjacent rows
942 wxRect bar
= rect
.Deflate(0, 1);
944 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
945 dc
->SetPen( *wxBLACK_PEN
);
946 dc
->DrawRectangle( bar
);
948 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
949 dc
->SetPen( *wxTRANSPARENT_PEN
);
951 const wxDataViewItemAttr
& attr
= GetAttr();
952 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
954 dc
->DrawRectangle( bar
);
959 wxSize
wxDataViewProgressRenderer::GetSize() const
961 return wxSize(40,12);
964 // ---------------------------------------------------------
965 // wxDataViewDateRenderer
966 // ---------------------------------------------------------
968 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
970 #if wxUSE_DATE_RENDERER_POPUP
972 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
975 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
976 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
977 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
982 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
983 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
984 sizer
->Add( m_cal
, 1, wxGROW
);
989 void OnCalendar( wxCalendarEvent
&event
);
991 wxCalendarCtrl
*m_cal
;
992 wxDataViewModel
*m_model
;
994 const wxDataViewItem
& m_item
;
997 virtual void OnDismiss()
1002 DECLARE_EVENT_TABLE()
1005 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1006 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1009 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1011 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1015 #endif // wxUSE_DATE_RENDERER_POPUP
1017 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
1019 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1020 wxDataViewCellMode mode
, int align
) :
1021 wxDataViewRenderer( varianttype
, mode
, align
)
1025 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1027 m_date
= value
.GetDateTime();
1032 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1038 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1040 wxString tmp
= m_date
.FormatDate();
1041 RenderText( tmp
, 0, cell
, dc
, state
);
1045 wxSize
wxDataViewDateRenderer::GetSize() const
1047 return GetTextExtent(m_date
.FormatDate());
1050 bool wxDataViewDateRenderer::WXOnActivate(const wxRect
& WXUNUSED(cell
),
1051 wxDataViewModel
*model
,
1052 const wxDataViewItem
& item
,
1055 wxDateTime dtOld
= m_date
;
1057 #if wxUSE_DATE_RENDERER_POPUP
1058 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1059 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1060 wxPoint pos
= wxGetMousePosition();
1063 popup
->Popup( popup
->m_cal
);
1064 #else // !wxUSE_DATE_RENDERER_POPUP
1065 wxMessageBox(dtOld
.Format());
1066 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1071 // ---------------------------------------------------------
1072 // wxDataViewIconTextRenderer
1073 // ---------------------------------------------------------
1075 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1077 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1078 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1079 wxDataViewRenderer( varianttype
, mode
, align
)
1082 SetAlignment(align
);
1085 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1091 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1096 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1100 const wxIcon
& icon
= m_value
.GetIcon();
1103 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1104 xoffset
= icon
.GetWidth()+4;
1107 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1112 wxSize
wxDataViewIconTextRenderer::GetSize() const
1114 if (!m_value
.GetText().empty())
1116 wxSize size
= GetTextExtent(m_value
.GetText());
1118 if (m_value
.GetIcon().IsOk())
1119 size
.x
+= m_value
.GetIcon().GetWidth() + 4;
1122 return wxSize(80,20);
1125 wxWindow
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1127 wxDataViewIconText iconText
;
1130 wxString text
= iconText
.GetText();
1132 // adjust the label rect to take the width of the icon into account
1133 if (iconText
.GetIcon().IsOk())
1135 int w
= iconText
.GetIcon().GetWidth() + 4;
1137 labelRect
.width
-= w
;
1140 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1141 wxPoint(labelRect
.x
,labelRect
.y
),
1142 wxSize(labelRect
.width
,labelRect
.height
) );
1144 // select the text in the control an place the cursor at the end
1145 ctrl
->SetInsertionPointEnd();
1151 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow
*editor
, wxVariant
& value
)
1153 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1155 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1160 //-----------------------------------------------------------------------------
1161 // wxDataViewDropTarget
1162 //-----------------------------------------------------------------------------
1164 #if wxUSE_DRAG_AND_DROP
1166 class wxBitmapCanvas
: public wxWindow
1169 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1170 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1173 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1176 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1179 dc
.DrawBitmap( m_bitmap
, 0, 0);
1185 class wxDataViewDropSource
: public wxDropSource
1188 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1196 ~wxDataViewDropSource()
1201 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1203 wxPoint pos
= wxGetMousePosition();
1207 int liney
= m_win
->GetLineStart( m_row
);
1209 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1210 m_win
->ClientToScreen( &linex
, &liney
);
1211 m_dist_x
= pos
.x
- linex
;
1212 m_dist_y
= pos
.y
- liney
;
1215 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1217 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1218 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1220 wxFRAME_TOOL_WINDOW
|
1221 wxFRAME_FLOAT_ON_PARENT
|
1222 wxFRAME_NO_TASKBAR
|
1224 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1229 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1230 m_hint
->SetTransparent( 128 );
1236 wxDataViewMainWindow
*m_win
;
1239 int m_dist_x
,m_dist_y
;
1243 class wxDataViewDropTarget
: public wxDropTarget
1246 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1252 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1254 wxDataFormat format
= GetMatchingPair();
1255 if (format
== wxDF_INVALID
)
1257 return m_win
->OnDragOver( format
, x
, y
, def
);
1260 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1262 wxDataFormat format
= GetMatchingPair();
1263 if (format
== wxDF_INVALID
)
1265 return m_win
->OnDrop( format
, x
, y
);
1268 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1270 wxDataFormat format
= GetMatchingPair();
1271 if (format
== wxDF_INVALID
)
1275 return m_win
->OnData( format
, x
, y
, def
);
1278 virtual void OnLeave()
1279 { m_win
->OnLeave(); }
1281 wxDataViewMainWindow
*m_win
;
1284 #endif // wxUSE_DRAG_AND_DROP
1286 //-----------------------------------------------------------------------------
1287 // wxDataViewRenameTimer
1288 //-----------------------------------------------------------------------------
1290 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1295 void wxDataViewRenameTimer::Notify()
1297 m_owner
->OnRenameTimer();
1300 //-----------------------------------------------------------------------------
1301 // wxDataViewMainWindow
1302 //-----------------------------------------------------------------------------
1304 // The tree building helper, declared firstly
1305 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
1306 wxDataViewTreeNode
* node
);
1308 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1310 if (row1
> row2
) return 1;
1311 if (row1
== row2
) return 0;
1315 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1317 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1318 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1319 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1320 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1321 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1322 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1325 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1326 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1327 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1328 m_selection( wxDataViewSelectionCmp
)
1333 m_lastOnSame
= false;
1334 m_renameTimer
= new wxDataViewRenameTimer( this );
1336 // TODO: user better initial values/nothing selected
1337 m_currentCol
= NULL
;
1340 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1342 #if wxUSE_DRAG_AND_DROP
1344 m_dragStart
= wxPoint(0,0);
1346 m_dragEnabled
= false;
1347 m_dropEnabled
= false;
1349 m_dropHintLine
= (unsigned int) -1;
1350 #endif // wxUSE_DRAG_AND_DROP
1352 m_lineLastClicked
= (unsigned int) -1;
1353 m_lineBeforeLastClicked
= (unsigned int) -1;
1354 m_lineSelectSingleOnUp
= (unsigned int) -1;
1358 SetBackgroundColour( *wxWHITE
);
1360 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1362 m_penRule
= wxPen(GetRuleColour());
1364 // compose a pen whichcan draw black lines
1365 // TODO: maybe there is something system colour to use
1366 m_penExpander
= wxPen(wxColour(0,0,0));
1368 m_root
= new wxDataViewTreeNode( NULL
);
1369 m_root
->SetHasChildren(true);
1371 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1373 m_underMouse
= NULL
;
1377 wxDataViewMainWindow::~wxDataViewMainWindow()
1380 delete m_renameTimer
;
1384 #if wxUSE_DRAG_AND_DROP
1385 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1387 m_dragFormat
= format
;
1388 m_dragEnabled
= format
!= wxDF_INVALID
;
1393 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1395 m_dropFormat
= format
;
1396 m_dropEnabled
= format
!= wxDF_INVALID
;
1399 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1404 void wxDataViewMainWindow::RemoveDropHint()
1409 RefreshRow( m_dropHintLine
);
1410 m_dropHintLine
= (unsigned int) -1;
1414 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1415 wxCoord y
, wxDragResult def
)
1419 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1420 unsigned int row
= GetLineAt( yy
);
1422 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1428 wxDataViewItem item
= GetItemByRow( row
);
1430 wxDataViewModel
*model
= GetOwner()->GetModel();
1432 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1433 event
.SetEventObject( m_owner
);
1434 event
.SetItem( item
);
1435 event
.SetModel( model
);
1436 event
.SetDataFormat( format
);
1437 if (!m_owner
->HandleWindowEvent( event
))
1443 if (!event
.IsAllowed())
1450 if (m_dropHint
&& (row
!= m_dropHintLine
))
1451 RefreshRow( m_dropHintLine
);
1453 m_dropHintLine
= row
;
1459 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1465 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1466 unsigned int row
= GetLineAt( yy
);
1468 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1471 wxDataViewItem item
= GetItemByRow( row
);
1473 wxDataViewModel
*model
= GetOwner()->GetModel();
1475 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1476 event
.SetEventObject( m_owner
);
1477 event
.SetItem( item
);
1478 event
.SetModel( model
);
1479 event
.SetDataFormat( format
);
1480 if (!m_owner
->HandleWindowEvent( event
))
1483 if (!event
.IsAllowed())
1489 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1494 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1495 unsigned int row
= GetLineAt( yy
);
1497 if ((row
>= GetRowCount()) || (xx
> GetEndOfLastCol()))
1500 wxDataViewItem item
= GetItemByRow( row
);
1502 wxDataViewModel
*model
= GetOwner()->GetModel();
1504 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1506 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1507 event
.SetEventObject( m_owner
);
1508 event
.SetItem( item
);
1509 event
.SetModel( model
);
1510 event
.SetDataFormat( format
);
1511 event
.SetDataSize( obj
->GetSize() );
1512 event
.SetDataBuffer( obj
->GetData() );
1513 if (!m_owner
->HandleWindowEvent( event
))
1516 if (!event
.IsAllowed())
1522 void wxDataViewMainWindow::OnLeave()
1527 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1529 int height
= GetLineHeight( row
);
1531 unsigned int cols
= GetOwner()->GetColumnCount();
1533 for (col
= 0; col
< cols
; col
++)
1535 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1536 if (column
->IsHidden())
1537 continue; // skip it!
1538 width
+= column
->GetWidth();
1544 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1545 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1546 indent
= indent
+ m_lineHeight
;
1547 // try to use the m_lineHeight as the expander space
1549 if(!node
->HasChildren())
1554 wxBitmap
bitmap( width
, height
);
1555 wxMemoryDC
dc( bitmap
);
1556 dc
.SetFont( GetFont() );
1557 dc
.SetPen( *wxBLACK_PEN
);
1558 dc
.SetBrush( *wxWHITE_BRUSH
);
1559 dc
.DrawRectangle( 0,0,width
,height
);
1561 wxDataViewModel
*model
= m_owner
->GetModel();
1563 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1566 // TODO-RTL: last column for RTL support
1567 expander
= GetOwner()->GetColumnAt( 0 );
1568 GetOwner()->SetExpanderColumn(expander
);
1573 for (col
= 0; col
< cols
; col
++)
1575 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1576 wxDataViewRenderer
*cell
= column
->GetRenderer();
1578 if (column
->IsHidden())
1579 continue; // skip it!
1581 width
= column
->GetWidth();
1583 if (column
== expander
)
1586 wxDataViewItem item
= GetItemByRow( row
);
1587 cell
->PrepareForItem(model
, item
, column
->GetModelColumn());
1589 wxRect
item_rect(x
, 0, width
, height
);
1590 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1592 // dc.SetClippingRegion( item_rect );
1593 cell
->WXCallRender(item_rect
, &dc
, 0);
1594 // dc.DestroyClippingRegion();
1602 #endif // wxUSE_DRAG_AND_DROP
1605 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1607 wxDataViewModel
*model
= GetOwner()->GetModel();
1608 wxAutoBufferedPaintDC
dc( this );
1611 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1612 dc
.SetPen( *wxTRANSPARENT_PEN
);
1613 dc
.DrawRectangle(GetClientSize());
1618 // No items to draw.
1623 GetOwner()->PrepareDC( dc
);
1624 dc
.SetFont( GetFont() );
1626 wxRect update
= GetUpdateRegion().GetBox();
1627 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1629 // compute which items needs to be redrawn
1630 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1631 unsigned int item_count
=
1632 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1633 (int)(GetRowCount( ) - item_start
));
1634 unsigned int item_last
= item_start
+ item_count
;
1636 // Send the event to wxDataViewCtrl itself.
1637 wxWindow
* const parent
= GetParent();
1638 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1639 cache_event
.SetEventObject(parent
);
1640 cache_event
.SetCache(item_start
, item_last
- 1);
1641 parent
->ProcessWindowEvent(cache_event
);
1643 // compute which columns needs to be redrawn
1644 unsigned int cols
= GetOwner()->GetColumnCount();
1647 // we assume that we have at least one column below and painting an
1648 // empty control is unnecessary anyhow
1652 unsigned int col_start
= 0;
1653 unsigned int x_start
;
1654 for (x_start
= 0; col_start
< cols
; col_start
++)
1656 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1657 if (col
->IsHidden())
1658 continue; // skip it!
1660 unsigned int w
= col
->GetWidth();
1661 if (x_start
+w
>= (unsigned int)update
.x
)
1667 unsigned int col_last
= col_start
;
1668 unsigned int x_last
= x_start
;
1669 for (; col_last
< cols
; col_last
++)
1671 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1672 if (col
->IsHidden())
1673 continue; // skip it!
1675 if (x_last
> (unsigned int)update
.GetRight())
1678 x_last
+= col
->GetWidth();
1681 // Draw horizontal rules if required
1682 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1684 dc
.SetPen(m_penRule
);
1685 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1687 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1689 int y
= GetLineStart( i
);
1690 dc
.DrawLine(x_start
, y
, x_last
, y
);
1694 // Draw vertical rules if required
1695 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1697 dc
.SetPen(m_penRule
);
1698 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1700 // NB: Vertical rules are drawn in the last pixel of a column so that
1701 // they align perfectly with native MSW wxHeaderCtrl as well as for
1702 // consistency with MSW native list control. There's no vertical
1703 // rule at the most-left side of the control.
1705 int x
= x_start
- 1;
1706 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1708 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1709 if (col
->IsHidden())
1710 continue; // skip it
1712 x
+= col
->GetWidth();
1714 dc
.DrawLine(x
, GetLineStart( item_start
),
1715 x
, GetLineStart( item_last
) );
1719 // redraw the background for the items which are selected/current
1720 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1722 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1723 if (selected
|| item
== m_currentRow
)
1725 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1726 if (item
== m_currentRow
)
1727 flags
|= wxCONTROL_CURRENT
;
1729 flags
|= wxCONTROL_FOCUSED
;
1731 wxRect
rect( x_start
, GetLineStart( item
),
1732 x_last
- x_start
, GetLineHeight( item
) );
1733 wxRendererNative::Get().DrawItemSelectionRect
1743 #if wxUSE_DRAG_AND_DROP
1746 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1747 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1748 dc
.SetPen( *wxBLACK_PEN
);
1749 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1750 dc
.DrawRectangle( rect
);
1752 #endif // wxUSE_DRAG_AND_DROP
1754 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1757 // TODO-RTL: last column for RTL support
1758 expander
= GetOwner()->GetColumnAt( 0 );
1759 GetOwner()->SetExpanderColumn(expander
);
1762 // redraw all cells for all rows which must be repainted and all columns
1764 cell_rect
.x
= x_start
;
1765 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1767 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1768 wxDataViewRenderer
*cell
= col
->GetRenderer();
1769 cell_rect
.width
= col
->GetWidth();
1771 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1772 continue; // skip it!
1774 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1776 // get the cell value and set it into the renderer
1777 wxDataViewTreeNode
*node
= NULL
;
1778 wxDataViewItem dataitem
;
1780 if (!IsVirtualList())
1782 node
= GetTreeNodeByRow(item
);
1786 dataitem
= node
->GetItem();
1788 // Skip all columns of "container" rows except the expander
1789 // column itself unless HasContainerColumns() overrides this.
1790 if ( col
!= GetOwner()->GetExpanderColumn() &&
1791 model
->IsContainer(dataitem
) &&
1792 !model
->HasContainerColumns(dataitem
) )
1797 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1800 cell
->PrepareForItem(model
, dataitem
, col
->GetModelColumn());
1803 cell_rect
.y
= GetLineStart( item
);
1804 cell_rect
.height
= GetLineHeight( item
);
1806 // deal with the expander
1808 if ((!IsList()) && (col
== expander
))
1810 // Calculate the indent first
1811 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1813 // we reserve m_lineHeight of horizontal space for the expander
1814 // but leave EXPANDER_MARGIN around the expander itself
1815 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1817 indent
+= m_lineHeight
;
1819 // draw expander if needed and visible
1820 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1822 dc
.SetPen( m_penExpander
);
1823 dc
.SetBrush( wxNullBrush
);
1825 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1826 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1827 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1829 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1832 if ( m_underMouse
== node
)
1833 flag
|= wxCONTROL_CURRENT
;
1834 if ( node
->IsOpen() )
1835 flag
|= wxCONTROL_EXPANDED
;
1837 // ensure that we don't overflow the cell (which might
1838 // happen if the column is very narrow)
1839 wxDCClipper
clip(dc
, cell_rect
);
1841 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1844 // force the expander column to left-center align
1845 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1847 if (node
&& !node
->HasChildren())
1849 // Yes, if the node does not have any child, it must be a leaf which
1850 // mean that it is a temporarily created by GetTreeNodeByRow
1854 wxRect item_rect
= cell_rect
;
1855 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1857 // account for the tree indent (harmless if we're not indented)
1858 item_rect
.x
+= indent
;
1859 item_rect
.width
-= indent
;
1861 if ( item_rect
.width
<= 0 )
1865 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1866 state
|= wxDATAVIEW_CELL_SELECTED
;
1868 // TODO: it would be much more efficient to create a clipping
1869 // region for the entire column being rendered (in the OnPaint
1870 // of wxDataViewMainWindow) instead of a single clip region for
1871 // each cell. However it would mean that each renderer should
1872 // respect the given wxRect's top & bottom coords, eventually
1873 // violating only the left & right coords - however the user can
1874 // make its own renderer and thus we cannot be sure of that.
1875 wxDCClipper
clip(dc
, item_rect
);
1877 cell
->WXCallRender(item_rect
, &dc
, state
);
1880 cell_rect
.x
+= cell_rect
.width
;
1884 void wxDataViewMainWindow::OnRenameTimer()
1886 // We have to call this here because changes may just have
1887 // been made and no screen update taken place.
1890 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1891 // (needs to be tested!)
1895 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1897 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1899 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1902 //-----------------------------------------------------------------------------
1903 // Helper class for do operation on the tree node
1904 //-----------------------------------------------------------------------------
1909 virtual ~DoJob() { }
1911 // The return value control how the tree-walker tranverse the tree
1912 // 0: Job done, stop tranverse and return
1913 // 1: Ignore the current node's subtree and continue
1914 // 2: Job not done, continue
1915 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1916 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1917 virtual int operator() ( void * n
) = 0;
1920 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1925 switch( func( node
) )
1936 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
1937 const wxDataViewTreeLeaves
& leaves
= node
->GetChildren();
1939 int len_nodes
= nodes
.GetCount();
1940 int len
= leaves
.GetCount();
1941 int i
= 0, nodes_i
= 0;
1943 for(; i
< len
; i
++ )
1945 void * n
= leaves
[i
];
1946 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1948 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1951 if( Walker( nd
, func
) )
1970 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1973 if (IsVirtualList())
1975 wxDataViewVirtualListModel
*list_model
=
1976 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1977 m_count
= list_model
->GetCount();
1983 wxDataViewTreeNode
* node
;
1984 node
= FindNode(parent
);
1989 node
->SetHasChildren( true );
1991 if( g_model
->IsContainer( item
) )
1993 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1994 newnode
->SetItem(item
);
1995 newnode
->SetHasChildren( true );
1996 node
->AddNode( newnode
);
1999 node
->AddLeaf( item
.GetID() );
2001 node
->ChangeSubTreeCount(1);
2006 GetOwner()->UpdateColBestWidths();
2012 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2014 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2015 const wxDataViewItem
& item
)
2017 if (IsVirtualList())
2019 wxDataViewVirtualListModel
*list_model
=
2020 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
2021 m_count
= list_model
->GetCount();
2023 if ( !m_selection
.empty() )
2025 const int row
= GetRowByItem(item
);
2027 const size_t selCount
= m_selection
.size();
2028 for ( size_t i
= 0; i
< selCount
; i
++ )
2030 if ( m_selection
[i
] > (unsigned)row
)
2034 int itemRow
= m_selection
.Index(row
);
2035 if ( itemRow
!= wxNOT_FOUND
)
2036 m_selection
.RemoveAt(itemRow
);
2040 else // general case
2042 wxDataViewTreeNode
* node
= FindNode(parent
);
2043 int itemPosInNode
= node
? node
->GetChildren().Index(item
.GetID()) : wxNOT_FOUND
;
2045 // Notice that it is possible that the item being deleted is not in the
2046 // tree at all, for example we could be deleting a never shown (because
2047 // collapsed) item in a tree model. So it's not an error if we don't know
2048 // about this item, just return without doing anything then.
2049 if ( !node
|| itemPosInNode
== wxNOT_FOUND
)
2052 bool isContainer
= false;
2053 wxDataViewTreeNode
*itemNode
= NULL
;
2055 const wxDataViewTreeNodes nds
= node
->GetNodes();
2056 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2058 if (nds
[i
]->GetItem() == item
)
2066 // Delete the item from wxDataViewTreeNode representation:
2067 int itemsDeleted
= 1;
2068 node
->GetChildren().Remove( item
.GetID() );
2072 wxDataViewTreeNode
*n
= node
->FindItemAsNode(item
);
2074 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2076 node
->GetNodes().Remove( n
);
2077 itemsDeleted
+= n
->GetSubTreeCount();
2078 ::DestroyTreeHelper(n
);
2081 // Make the row number invalid and get a new valid one when user call GetRowCount
2083 node
->ChangeSubTreeCount(-itemsDeleted
);
2085 // Update selection by removing 'item' and its entire children tree from the selection.
2086 if ( !m_selection
.empty() )
2088 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2089 // the parent ('node') and position in its list of children
2091 if ( itemPosInNode
== 0 )
2093 // 1st child, row number is that of the parent node + 1
2094 itemRow
= GetRowByItem(node
->GetItem()) + 1;
2098 // row number is that of the sibling above 'item' + its subtree if any + 1
2099 const wxDataViewItem sibling
= wxDataViewItem(node
->GetChildren()[itemPosInNode
- 1]);
2100 const wxDataViewTreeNode
*siblingNode
= node
->FindItemAsNode(sibling
);
2102 itemRow
= GetRowByItem(sibling
);
2104 itemRow
+= siblingNode
->GetSubTreeCount();
2108 wxDataViewSelection
newsel(wxDataViewSelectionCmp
);
2110 const size_t numSelections
= m_selection
.size();
2111 for ( size_t i
= 0; i
< numSelections
; ++i
)
2113 const int s
= m_selection
[i
];
2115 newsel
.push_back(s
);
2116 else if ( s
>= itemRow
+ itemsDeleted
)
2117 newsel
.push_back(s
- itemsDeleted
);
2118 // else: deleted item, remove from selection
2121 m_selection
= newsel
;
2125 // Change the current row to the last row if the current exceed the max row number
2126 if( m_currentRow
> GetRowCount() )
2127 ChangeCurrentRow(m_count
- 1);
2129 GetOwner()->UpdateColBestWidths();
2135 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2140 GetOwner()->UpdateColBestWidths();
2143 wxWindow
*parent
= GetParent();
2144 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2145 le
.SetEventObject(parent
);
2146 le
.SetModel(GetOwner()->GetModel());
2148 parent
->GetEventHandler()->ProcessEvent(le
);
2153 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int model_column
)
2155 int view_column
= -1;
2156 unsigned int n_col
= m_owner
->GetColumnCount();
2157 for (unsigned i
= 0; i
< n_col
; i
++)
2159 wxDataViewColumn
*column
= m_owner
->GetColumn( i
);
2160 if (column
->GetModelColumn() == model_column
)
2162 view_column
= (int) i
;
2166 if (view_column
== -1)
2169 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2170 /*#define MAX_VIRTUAL_WIDTH 100000
2172 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2173 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2174 Refresh( true, &rect );
2181 GetOwner()->UpdateColBestWidth(view_column
);
2184 wxWindow
*parent
= GetParent();
2185 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2186 le
.SetEventObject(parent
);
2187 le
.SetModel(GetOwner()->GetModel());
2189 le
.SetColumn(view_column
);
2190 le
.SetDataViewColumn(GetOwner()->GetColumn(view_column
));
2191 parent
->GetEventHandler()->ProcessEvent(le
);
2196 bool wxDataViewMainWindow::Cleared()
2199 m_selection
.Clear();
2202 BuildTree( GetOwner()->GetModel() );
2204 GetOwner()->UpdateColBestWidths();
2210 void wxDataViewMainWindow::UpdateDisplay()
2213 m_underMouse
= NULL
;
2216 void wxDataViewMainWindow::OnInternalIdle()
2218 wxWindow::OnInternalIdle();
2222 RecalculateDisplay();
2227 void wxDataViewMainWindow::RecalculateDisplay()
2229 wxDataViewModel
*model
= GetOwner()->GetModel();
2236 int width
= GetEndOfLastCol();
2237 int height
= GetLineStart( GetRowCount() );
2239 SetVirtualSize( width
, height
);
2240 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2245 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2247 m_underMouse
= NULL
;
2249 wxWindow::ScrollWindow( dx
, dy
, rect
);
2251 if (GetOwner()->m_headerArea
)
2252 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2255 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2257 m_underMouse
= NULL
;
2260 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2261 int sy
= GetLineStart( rows
)/y
;
2265 wxRect rect
= GetClientRect();
2269 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2270 for (x_start
= 0; colnum
< column
; colnum
++)
2272 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2273 if (col
->IsHidden())
2274 continue; // skip it!
2276 w
= col
->GetWidth();
2280 int x_end
= x_start
+ w
;
2281 xe
= xx
+ rect
.width
;
2284 sx
= ( xx
+ x_end
- xe
)/x
;
2291 m_owner
->Scroll( sx
, sy
);
2294 int wxDataViewMainWindow::GetCountPerPage() const
2296 wxSize size
= GetClientSize();
2297 return size
.y
/ m_lineHeight
;
2300 int wxDataViewMainWindow::GetEndOfLastCol() const
2304 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2306 const wxDataViewColumn
*c
=
2307 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2310 width
+= c
->GetWidth();
2315 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2319 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2321 return GetLineAt( y
);
2324 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2326 wxSize client_size
= GetClientSize();
2327 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2328 &client_size
.x
, &client_size
.y
);
2330 // we should deal with the pixel here
2331 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2333 return wxMin( GetRowCount()-1, row
);
2336 unsigned int wxDataViewMainWindow::GetRowCount()
2338 if ( m_count
== -1 )
2340 m_count
= RecalculateCount();
2346 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2353 void wxDataViewMainWindow::SelectAllRows( bool on
)
2360 m_selection
.Clear();
2361 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2362 m_selection
.Add( i
);
2367 unsigned int first_visible
= GetFirstVisibleRow();
2368 unsigned int last_visible
= GetLastVisibleRow();
2370 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2372 unsigned int row
= m_selection
[i
];
2373 if ((row
>= first_visible
) && (row
<= last_visible
))
2376 m_selection
.Clear();
2380 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2382 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2386 m_selection
.Add( row
);
2394 m_selection
.Remove( row
);
2400 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2404 unsigned int tmp
= from
;
2410 for (i
= from
; i
<= to
; i
++)
2412 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2415 m_selection
.Add( i
);
2420 m_selection
.Remove( i
);
2423 RefreshRows( from
, to
);
2426 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2428 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2430 int n
= aSelections
[i
];
2432 m_selection
.Add( n
);
2437 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2439 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2440 m_selection
.Add( row
);
2442 m_selection
.Remove( row
);
2446 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2448 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2451 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2453 wxWindow
*parent
= GetParent();
2454 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2456 le
.SetEventObject(parent
);
2457 le
.SetModel(GetOwner()->GetModel());
2460 parent
->GetEventHandler()->ProcessEvent(le
);
2463 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2465 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2466 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2468 wxSize client_size
= GetClientSize();
2469 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2470 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2471 if (intersect_rect
.width
> 0)
2472 Refresh( true, &intersect_rect
);
2475 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2479 unsigned int tmp
= to
;
2484 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2485 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2487 wxSize client_size
= GetClientSize();
2488 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2489 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2490 if (intersect_rect
.width
> 0)
2491 Refresh( true, &intersect_rect
);
2494 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2496 wxSize client_size
= GetClientSize();
2497 int start
= GetLineStart( firstRow
);
2498 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2499 if (start
> client_size
.y
) return;
2501 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2503 Refresh( true, &rect
);
2506 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2508 wxCHECK_RET( newCurrent
< GetRowCount(),
2509 wxT("invalid item index in OnArrowChar()") );
2511 // if there is no selection, we cannot move it anywhere
2512 if (!HasCurrentRow())
2515 unsigned int oldCurrent
= m_currentRow
;
2517 // in single selection we just ignore Shift as we can't select several
2519 if ( event
.ShiftDown() && !IsSingleSel() )
2521 RefreshRow( oldCurrent
);
2523 ChangeCurrentRow( newCurrent
);
2525 // select all the items between the old and the new one
2526 if ( oldCurrent
> newCurrent
)
2528 newCurrent
= oldCurrent
;
2529 oldCurrent
= m_currentRow
;
2532 SelectRows( oldCurrent
, newCurrent
, true );
2533 if (oldCurrent
!=newCurrent
)
2534 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2538 RefreshRow( oldCurrent
);
2540 // all previously selected items are unselected unless ctrl is held
2541 if ( !event
.ControlDown() )
2542 SelectAllRows(false);
2544 ChangeCurrentRow( newCurrent
);
2546 if ( !event
.ControlDown() )
2548 SelectRow( m_currentRow
, true );
2549 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2552 RefreshRow( m_currentRow
);
2555 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2558 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2562 rect
.y
= GetLineStart( row
);
2563 rect
.width
= GetEndOfLastCol();
2564 rect
.height
= GetLineHeight( row
);
2569 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2571 const wxDataViewModel
*model
= GetOwner()->GetModel();
2573 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2575 // TODO make more efficient
2580 for (r
= 0; r
< row
; r
++)
2582 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2583 if (!node
) return start
;
2585 wxDataViewItem item
= node
->GetItem();
2587 if (node
&& !node
->HasChildren())
2589 // Yes, if the node does not have any child, it must be a leaf which
2590 // mean that it is a temporarily created by GetTreeNodeByRow
2594 unsigned int cols
= GetOwner()->GetColumnCount();
2596 int height
= m_lineHeight
;
2597 for (col
= 0; col
< cols
; col
++)
2599 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2600 if (column
->IsHidden())
2601 continue; // skip it!
2604 model
->IsContainer(item
) &&
2605 !model
->HasContainerColumns(item
))
2606 continue; // skip it!
2608 wxDataViewRenderer
*renderer
=
2609 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2610 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2612 height
= wxMax( height
, renderer
->GetSize().y
);
2622 return row
* m_lineHeight
;
2626 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2628 const wxDataViewModel
*model
= GetOwner()->GetModel();
2630 // check for the easy case first
2631 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2632 return y
/ m_lineHeight
;
2634 // TODO make more efficient
2635 unsigned int row
= 0;
2636 unsigned int yy
= 0;
2639 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2642 // not really correct...
2643 return row
+ ((y
-yy
) / m_lineHeight
);
2646 wxDataViewItem item
= node
->GetItem();
2648 if (node
&& !node
->HasChildren())
2650 // Yes, if the node does not have any child, it must be a leaf which
2651 // mean that it is a temporarily created by GetTreeNodeByRow
2655 unsigned int cols
= GetOwner()->GetColumnCount();
2657 int height
= m_lineHeight
;
2658 for (col
= 0; col
< cols
; col
++)
2660 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2661 if (column
->IsHidden())
2662 continue; // skip it!
2665 model
->IsContainer(item
) &&
2666 !model
->HasContainerColumns(item
))
2667 continue; // skip it!
2669 wxDataViewRenderer
*renderer
=
2670 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2671 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2673 height
= wxMax( height
, renderer
->GetSize().y
);
2684 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2686 const wxDataViewModel
*model
= GetOwner()->GetModel();
2688 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2690 wxASSERT( !IsVirtualList() );
2692 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2693 // wxASSERT( node );
2694 if (!node
) return m_lineHeight
;
2696 wxDataViewItem item
= node
->GetItem();
2698 if (node
&& !node
->HasChildren())
2700 // Yes, if the node does not have any child, it must be a leaf which
2701 // mean that it is a temporarily created by GetTreeNodeByRow
2705 int height
= m_lineHeight
;
2707 unsigned int cols
= GetOwner()->GetColumnCount();
2709 for (col
= 0; col
< cols
; col
++)
2711 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2712 if (column
->IsHidden())
2713 continue; // skip it!
2716 model
->IsContainer(item
) &&
2717 !model
->HasContainerColumns(item
))
2718 continue; // skip it!
2720 wxDataViewRenderer
*renderer
=
2721 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2722 renderer
->PrepareForItem(model
, item
, column
->GetModelColumn());
2724 height
= wxMax( height
, renderer
->GetSize().y
);
2731 return m_lineHeight
;
2735 class RowToItemJob
: public DoJob
2738 RowToItemJob( unsigned int row
, int current
)
2739 { this->row
= row
; this->current
= current
; }
2740 virtual ~RowToItemJob() {}
2742 virtual int operator() ( wxDataViewTreeNode
* node
)
2745 if( current
== static_cast<int>(row
))
2747 ret
= node
->GetItem();
2751 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2753 current
+= node
->GetSubTreeCount();
2758 // If the current has no child node, we can find the desired item of the row
2760 // This if can speed up finding in some case, and will has a very good effect
2761 // when it comes to list view
2762 if( node
->GetNodes().GetCount() == 0)
2764 int index
= static_cast<int>(row
) - current
- 1;
2765 ret
= wxDataViewItem(node
->GetChildren().Item( index
));
2772 virtual int operator() ( void * n
)
2775 if( current
== static_cast<int>(row
))
2777 ret
= wxDataViewItem( n
);
2783 wxDataViewItem
GetResult() const
2792 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2794 if (IsVirtualList())
2796 return wxDataViewItem( wxUIntToPtr(row
+1) );
2800 RowToItemJob
job( row
, -2 );
2801 Walker( m_root
, job
);
2802 return job
.GetResult();
2806 class RowToTreeNodeJob
: public DoJob
2809 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2812 this->current
= current
;
2816 virtual ~RowToTreeNodeJob(){ }
2818 virtual int operator() ( wxDataViewTreeNode
* node
)
2821 if( current
== static_cast<int>(row
))
2827 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2829 current
+= node
->GetSubTreeCount();
2836 // If the current node has no children, we can find the desired item of the
2837 // row number directly.
2838 // This if can speed up finding in some case, and will have a very good
2839 // effect for list views.
2840 if( node
->GetNodes().GetCount() == 0)
2842 int index
= static_cast<int>(row
) - current
- 1;
2843 void * n
= node
->GetChildren().Item( index
);
2844 ret
= new wxDataViewTreeNode( parent
);
2845 ret
->SetItem( wxDataViewItem( n
));
2846 ret
->SetHasChildren(false);
2853 virtual int operator() ( void * n
)
2856 if( current
== static_cast<int>(row
))
2858 ret
= new wxDataViewTreeNode( parent
);
2859 ret
->SetItem( wxDataViewItem( n
));
2860 ret
->SetHasChildren(false);
2867 wxDataViewTreeNode
* GetResult() const
2873 wxDataViewTreeNode
* ret
;
2874 wxDataViewTreeNode
* parent
;
2877 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2879 wxASSERT( !IsVirtualList() );
2881 RowToTreeNodeJob
job( row
, -2, m_root
);
2882 Walker( m_root
, job
);
2883 return job
.GetResult();
2886 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2887 const wxDataViewItem
& item
)
2889 wxWindow
*parent
= GetParent();
2890 wxDataViewEvent
le(type
, parent
->GetId());
2892 le
.SetEventObject(parent
);
2893 le
.SetModel(GetOwner()->GetModel());
2896 parent
->GetEventHandler()->ProcessEvent(le
);
2900 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2905 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2909 if (!node
->HasChildren())
2915 return node
->IsOpen();
2918 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2923 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2927 if (!node
->HasChildren())
2936 void wxDataViewMainWindow::Expand( unsigned int row
)
2941 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2945 if (!node
->HasChildren())
2951 if (!node
->IsOpen())
2954 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2956 // Check if the user prevent expanding
2957 if( e
.GetSkipped() )
2962 // build the children of current node
2963 if( node
->GetChildrenNumber() == 0 )
2966 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2969 // By expanding the node all row indices that are currently in the selection list
2970 // and are greater than our node have become invalid. So we have to correct that now.
2971 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2972 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2974 const unsigned testRow
= m_selection
[i
];
2975 // all rows above us are not affected, so skip them
2979 m_selection
[i
] += rowAdjustment
;
2982 if(m_currentRow
> row
)
2983 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2987 // Send the expanded event
2988 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2992 void wxDataViewMainWindow::Collapse(unsigned int row
)
2997 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
3001 if (!node
->HasChildren())
3010 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3011 if( e
.GetSkipped() )
3014 // Find out if there are selected items below the current node.
3015 bool selectCollapsingRow
= false;
3016 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3017 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3018 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3020 const unsigned testRow
= m_selection
[i
];
3021 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3023 selectCollapsingRow
= true;
3024 // get out as soon as we have found a node that is selected
3031 // If the node to be closed has selected items the user won't see those any longer.
3032 // We select the collapsing node in this case.
3033 if(selectCollapsingRow
)
3035 SelectAllRows(false);
3036 ChangeCurrentRow(row
);
3037 SelectRow(row
, true);
3038 SendSelectionChangedEvent(GetItemByRow(row
));
3042 // if there were no selected items below our node we still need to "fix" the
3043 // selection list to adjust for the changing of the row indices.
3044 // We actually do the opposite of what we are doing in Expand().
3045 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3047 const unsigned testRow
= m_selection
[i
];
3048 // all rows above us are not affected, so skip them
3052 m_selection
[i
] -= rowAdjustment
;
3055 // if the "current row" is being collapsed away we change it to the current row ;-)
3056 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3057 ChangeCurrentRow(row
);
3058 else if(m_currentRow
> row
)
3059 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3064 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3068 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3070 const wxDataViewModel
* model
= GetOwner()->GetModel();
3077 // Compose the parent-chain for the item we are looking for
3078 wxVector
<wxDataViewItem
> parentChain
;
3079 wxDataViewItem
it( item
);
3082 parentChain
.push_back(it
);
3083 it
= model
->GetParent(it
);
3086 // Find the item along the parent-chain.
3087 // This algorithm is designed to speed up the node-finding method
3088 wxDataViewTreeNode
* node
= m_root
;
3089 for( unsigned iter
= parentChain
.size()-1; ; --iter
)
3091 if( node
->HasChildren() )
3093 if( node
->GetChildrenNumber() == 0 )
3096 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3099 const wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3102 for (unsigned i
= 0; i
< nodes
.GetCount(); ++i
)
3104 wxDataViewTreeNode
* currentNode
= nodes
[i
];
3105 if (currentNode
->GetItem() == parentChain
[iter
])
3107 if (currentNode
->GetItem() == item
)
3127 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3128 wxDataViewColumn
* &column
)
3130 wxDataViewColumn
*col
= NULL
;
3131 unsigned int cols
= GetOwner()->GetColumnCount();
3132 unsigned int colnum
= 0;
3134 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3135 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3137 col
= GetOwner()->GetColumnAt(colnum
);
3138 if (col
->IsHidden())
3139 continue; // skip it!
3141 unsigned int w
= col
->GetWidth();
3142 if (x_start
+w
>= (unsigned int)x
)
3149 item
= GetItemByRow( GetLineAt( y
) );
3152 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3153 const wxDataViewColumn
* column
)
3158 unsigned int cols
= GetOwner()->GetColumnCount();
3159 // If column is null the loop will compute the combined width of all columns.
3160 // Otherwise, it will compute the x position of the column we are looking for.
3161 for (unsigned int i
= 0; i
< cols
; i
++)
3163 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3168 if (col
->IsHidden())
3169 continue; // skip it!
3171 xpos
+= col
->GetWidth();
3172 width
+= col
->GetWidth();
3177 // If we have a column, we need can get its width directly.
3178 if(column
->IsHidden())
3181 width
= column
->GetWidth();
3186 // If we have no column, we reset the x position back to zero.
3190 // we have to take an expander column into account and compute its indentation
3191 // to get the correct x position where the actual text is
3193 int row
= GetRowByItem(item
);
3194 if (!IsList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3196 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3197 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3198 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3200 if(!node
->HasChildren())
3204 wxRect
itemRect( xpos
+ indent
,
3205 GetLineStart( row
),
3207 GetLineHeight( row
) );
3209 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3210 &itemRect
.x
, &itemRect
.y
);
3215 int wxDataViewMainWindow::RecalculateCount()
3217 if (IsVirtualList())
3219 wxDataViewVirtualListModel
*list_model
=
3220 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3222 return list_model
->GetCount();
3226 return m_root
->GetSubTreeCount();
3230 class ItemToRowJob
: public DoJob
3233 ItemToRowJob(const wxDataViewItem
& item_
, wxVector
<wxDataViewItem
>::reverse_iterator iter
)
3240 // Maybe binary search will help to speed up this process
3241 virtual int operator() ( wxDataViewTreeNode
* node
)
3244 if( node
->GetItem() == item
)
3249 if( node
->GetItem() == *m_iter
)
3256 ret
+= node
->GetSubTreeCount();
3262 virtual int operator() ( void * n
)
3265 if( n
== item
.GetID() )
3270 // the row number is begin from zero
3271 int GetResult() const
3275 wxVector
<wxDataViewItem
>::reverse_iterator m_iter
;
3276 wxDataViewItem item
;
3281 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3283 const wxDataViewModel
* model
= GetOwner()->GetModel();
3287 if (IsVirtualList())
3289 return wxPtrToUInt( item
.GetID() ) -1;
3296 // Compose the parent-chain of the item we are looking for
3297 wxVector
<wxDataViewItem
> parentChain
;
3298 wxDataViewItem
it( item
);
3301 parentChain
.push_back(it
);
3302 it
= model
->GetParent(it
);
3305 // add an 'invalid' item to represent our 'invisible' root node
3306 parentChain
.push_back(wxDataViewItem());
3308 // the parent chain was created by adding the deepest parent first.
3309 // so if we want to start at the root node, we have to iterate backwards through the vector
3310 ItemToRowJob
job( item
, parentChain
.rbegin() );
3311 Walker( m_root
, job
);
3312 return job
.GetResult();
3316 static void BuildTreeHelper( const wxDataViewModel
* model
, const wxDataViewItem
& item
,
3317 wxDataViewTreeNode
* node
)
3319 if( !model
->IsContainer( item
) )
3322 wxDataViewItemArray children
;
3323 unsigned int num
= model
->GetChildren( item
, children
);
3325 unsigned int index
= 0;
3326 while( index
< num
)
3328 if( model
->IsContainer( children
[index
] ) )
3330 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3331 n
->SetItem(children
[index
]);
3332 n
->SetHasChildren( true );
3337 node
->AddLeaf( children
[index
].GetID() );
3341 node
->SetSubTreeCount( num
);
3342 wxDataViewTreeNode
* n
= node
->GetParent();
3344 n
->ChangeSubTreeCount(num
);
3348 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3352 if (GetOwner()->GetModel()->IsVirtualListModel())
3358 m_root
= new wxDataViewTreeNode( NULL
);
3359 m_root
->SetHasChildren(true);
3361 // First we define a invalid item to fetch the top-level elements
3362 wxDataViewItem item
;
3364 BuildTreeHelper( model
, item
, m_root
);
3368 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3370 if( node
->GetNodeNumber() != 0 )
3372 int len
= node
->GetNodeNumber();
3373 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3374 for (int i
= 0; i
< len
; i
++)
3375 DestroyTreeHelper(nodes
[i
]);
3380 void wxDataViewMainWindow::DestroyTree()
3382 if (!IsVirtualList())
3384 ::DestroyTreeHelper(m_root
);
3390 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3392 wxWindow
* const parent
= GetParent();
3394 // propagate the char event upwards
3395 wxKeyEvent
eventForParent(event
);
3396 eventForParent
.SetEventObject(parent
);
3397 if ( parent
->ProcessWindowEvent(eventForParent
) )
3400 if ( parent
->HandleAsNavigationKey(event
) )
3403 // no item -> nothing to do
3404 if (!HasCurrentRow())
3410 // don't use m_linesPerPage directly as it might not be computed yet
3411 const int pageSize
= GetCountPerPage();
3412 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3414 switch ( event
.GetKeyCode() )
3418 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3420 le
.SetItem( GetItemByRow(m_currentRow
) );
3421 le
.SetEventObject(parent
);
3422 le
.SetModel(GetOwner()->GetModel());
3424 parent
->GetEventHandler()->ProcessEvent(le
);
3429 if ( m_currentRow
> 0 )
3430 OnArrowChar( m_currentRow
- 1, event
);
3434 if ( m_currentRow
+ 1 < GetRowCount() )
3435 OnArrowChar( m_currentRow
+ 1, event
);
3437 // Add the process for tree expanding/collapsing
3443 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3447 if (node
->HasChildren() && node
->IsOpen())
3449 Collapse(m_currentRow
);
3451 else // if the node is already closed we move the selection to its parent
3453 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3455 if(!node
->HasChildren())
3460 int parent
= GetRowByItem( parent_node
->GetItem() );
3463 unsigned int row
= m_currentRow
;
3464 SelectRow( row
, false);
3465 SelectRow( parent
, true );
3466 ChangeCurrentRow( parent
);
3467 GetOwner()->EnsureVisible( parent
, -1 );
3468 SendSelectionChangedEvent( parent_node
->GetItem() );
3476 if (!IsExpanded( m_currentRow
))
3477 Expand( m_currentRow
);
3480 unsigned int row
= m_currentRow
;
3481 SelectRow( row
, false );
3482 SelectRow( row
+ 1, true );
3483 ChangeCurrentRow( row
+ 1 );
3484 GetOwner()->EnsureVisible( row
+ 1, -1 );
3485 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3492 OnArrowChar( GetRowCount() - 1, event
);
3497 OnArrowChar( 0, event
);
3502 int steps
= pageSize
- 1;
3503 int index
= m_currentRow
- steps
;
3507 OnArrowChar( index
, event
);
3513 int steps
= pageSize
- 1;
3514 unsigned int index
= m_currentRow
+ steps
;
3515 unsigned int count
= GetRowCount();
3516 if ( index
>= count
)
3519 OnArrowChar( index
, event
);
3525 if(m_selection
.size() == 1)
3527 // TODO: we need to revise that when we have a concept for a 'current column'
3528 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3538 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3540 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3542 // let the base handle mouse wheel events.
3547 // set the focus to ourself if any of the mouse buttons are pressed
3548 if(event
.ButtonDown() && !HasFocus())
3551 int x
= event
.GetX();
3552 int y
= event
.GetY();
3553 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3554 wxDataViewColumn
*col
= NULL
;
3557 unsigned int cols
= GetOwner()->GetColumnCount();
3559 for (i
= 0; i
< cols
; i
++)
3561 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3563 continue; // skip it!
3565 if (x
< xpos
+ c
->GetWidth())
3570 xpos
+= c
->GetWidth();
3578 wxDataViewRenderer
*cell
= col
->GetRenderer();
3579 unsigned int current
= GetLineAt( y
);
3580 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3582 // Unselect all if below the last row ?
3587 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3588 // button) and also determine the offset of the real cell start, skipping
3589 // the indentation and the expander itself.
3590 bool hoverOverExpander
= false;
3591 int expanderOffset
= 0;
3592 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col
))
3594 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3595 if( node
!=NULL
&& node
->HasChildren() )
3597 int indent
= node
->GetIndentLevel();
3598 indent
= GetOwner()->GetIndent()*indent
;
3600 // we make the rectangle we are looking in a bit bigger than the actual
3601 // visual expander so the user can hit that little thing reliably
3602 wxRect
rect( xpos
+ indent
,
3603 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3604 m_lineHeight
, m_lineHeight
);
3606 expanderOffset
= indent
+ m_lineHeight
;
3608 if( rect
.Contains(x
, y
) )
3610 // So the mouse is over the expander
3611 hoverOverExpander
= true;
3612 if (m_underMouse
&& m_underMouse
!= node
)
3614 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3615 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3617 if (m_underMouse
!= node
)
3619 // wxLogMessage("Do the row: %d", current);
3620 RefreshRow(current
);
3622 m_underMouse
= node
;
3625 if (node
!=NULL
&& !node
->HasChildren())
3628 if (!hoverOverExpander
)
3630 if (m_underMouse
!= NULL
)
3632 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3633 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3634 m_underMouse
= NULL
;
3638 wxDataViewModel
*model
= GetOwner()->GetModel();
3640 #if wxUSE_DRAG_AND_DROP
3641 if (event
.Dragging())
3643 if (m_dragCount
== 0)
3645 // we have to report the raw, physical coords as we want to be
3646 // able to call HitTest(event.m_pointDrag) from the user code to
3647 // get the item being dragged
3648 m_dragStart
= event
.GetPosition();
3653 if (m_dragCount
!= 3)
3656 if (event
.LeftIsDown())
3658 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3659 &m_dragStart
.x
, &m_dragStart
.y
);
3660 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3661 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3663 // Notify cell about drag
3664 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3665 event
.SetEventObject( m_owner
);
3666 event
.SetItem( item
);
3667 event
.SetModel( model
);
3668 if (!m_owner
->HandleWindowEvent( event
))
3671 if (!event
.IsAllowed())
3674 wxDataObject
*obj
= event
.GetDataObject();
3678 wxDataViewDropSource
drag( this, drag_item_row
);
3679 drag
.SetData( *obj
);
3680 /* wxDragResult res = */ drag
.DoDragDrop();
3689 #endif // wxUSE_DRAG_AND_DROP
3691 bool simulateClick
= false;
3693 if (event
.ButtonDClick())
3695 m_renameTimer
->Stop();
3696 m_lastOnSame
= false;
3699 wxDataViewItem item
= GetItemByRow(current
);
3700 bool ignore_other_columns
=
3701 ((GetOwner()->GetExpanderColumn() != col
) &&
3702 (model
->IsContainer(item
)) &&
3703 (!model
->HasContainerColumns(item
)));
3705 if (event
.LeftDClick())
3707 if(hoverOverExpander
)
3709 // a double click on the expander will be converted into a "simulated" normal click
3710 simulateClick
= true;
3712 else if ( current
== m_lineLastClicked
)
3714 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3716 const unsigned colIdx
= col
->GetModelColumn();
3718 cell
->PrepareForItem(model
, item
, colIdx
);
3720 wxRect
cell_rect( xpos
, GetLineStart( current
),
3721 col
->GetWidth(), GetLineHeight( current
) );
3722 cell
->WXOnActivate( cell_rect
, model
, item
, colIdx
);
3726 wxWindow
*parent
= GetParent();
3727 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3729 le
.SetColumn( col
->GetModelColumn() );
3730 le
.SetDataViewColumn( col
);
3731 le
.SetEventObject(parent
);
3732 le
.SetModel(GetOwner()->GetModel());
3734 parent
->GetEventHandler()->ProcessEvent(le
);
3740 // The first click was on another item, so don't interpret this as
3741 // a double click, but as a simple click instead
3742 simulateClick
= true;
3746 if (event
.LeftUp() && !hoverOverExpander
)
3748 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3750 // select single line
3751 SelectAllRows( false );
3752 SelectRow( m_lineSelectSingleOnUp
, true );
3753 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3756 // If the user click the expander, we do not do editing even if the column
3757 // with expander are editable
3758 if (m_lastOnSame
&& !ignore_other_columns
)
3760 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3761 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3763 m_renameTimer
->Start( 100, true );
3767 m_lastOnSame
= false;
3768 m_lineSelectSingleOnUp
= (unsigned int)-1;
3770 else if(!event
.LeftUp())
3772 // This is necessary, because after a DnD operation in
3773 // from and to ourself, the up event is swallowed by the
3774 // DnD code. So on next non-up event (which means here and
3775 // now) m_lineSelectSingleOnUp should be reset.
3776 m_lineSelectSingleOnUp
= (unsigned int)-1;
3779 if (event
.RightDown())
3781 m_lineBeforeLastClicked
= m_lineLastClicked
;
3782 m_lineLastClicked
= current
;
3784 // If the item is already selected, do not update the selection.
3785 // Multi-selections should not be cleared if a selected item is clicked.
3786 if (!IsRowSelected(current
))
3788 SelectAllRows(false);
3789 const unsigned oldCurrent
= m_currentRow
;
3790 ChangeCurrentRow(current
);
3791 SelectRow(m_currentRow
,true);
3792 RefreshRow(oldCurrent
);
3793 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3796 else if (event
.RightUp())
3799 model
->GetValue( value
, item
, col
->GetModelColumn() );
3800 wxWindow
*parent
= GetParent();
3801 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3803 le
.SetColumn( col
->GetModelColumn() );
3804 le
.SetDataViewColumn( col
);
3805 le
.SetEventObject(parent
);
3806 le
.SetModel(GetOwner()->GetModel());
3808 parent
->GetEventHandler()->ProcessEvent(le
);
3810 else if (event
.MiddleDown())
3814 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3816 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3818 // hoverOverExpander being true tells us that our node must be
3819 // valid and have children.
3820 // So we don't need any extra checks.
3821 if( node
->IsOpen() )
3826 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3828 m_lineBeforeLastClicked
= m_lineLastClicked
;
3829 m_lineLastClicked
= current
;
3831 unsigned int oldCurrentRow
= m_currentRow
;
3832 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3834 bool cmdModifierDown
= event
.CmdDown();
3835 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3837 if ( IsSingleSel() || !IsRowSelected(current
) )
3839 SelectAllRows( false );
3840 ChangeCurrentRow(current
);
3841 SelectRow(m_currentRow
,true);
3842 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3844 else // multi sel & current is highlighted & no mod keys
3846 m_lineSelectSingleOnUp
= current
;
3847 ChangeCurrentRow(current
); // change focus
3850 else // multi sel & either ctrl or shift is down
3852 if (cmdModifierDown
)
3854 ChangeCurrentRow(current
);
3855 ReverseRowSelection(m_currentRow
);
3856 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3858 else if (event
.ShiftDown())
3860 ChangeCurrentRow(current
);
3862 unsigned int lineFrom
= oldCurrentRow
,
3865 if ( lineTo
< lineFrom
)
3868 lineFrom
= m_currentRow
;
3871 SelectRows(lineFrom
, lineTo
, true);
3872 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3874 else // !ctrl, !shift
3876 // test in the enclosing if should make it impossible
3877 wxFAIL_MSG( wxT("how did we get here?") );
3881 if (m_currentRow
!= oldCurrentRow
)
3882 RefreshRow( oldCurrentRow
);
3884 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3886 // Update selection here...
3889 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3890 (current
== oldCurrentRow
)) && oldWasSelected
;
3892 // Call LeftClick after everything else as under GTK+
3893 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3895 // notify cell about click
3896 cell
->PrepareForItem(model
, item
, col
->GetModelColumn());
3898 wxRect
cell_rect( xpos
+ expanderOffset
,
3899 GetLineStart( current
),
3900 col
->GetWidth() - expanderOffset
,
3901 GetLineHeight( current
) );
3903 // Report position relative to the cell's custom area, i.e.
3904 // no the entire space as given by the control but the one
3905 // used by the renderer after calculation of alignment etc.
3907 // adjust the rectangle ourselves to account for the alignment
3908 wxRect rectItem
= cell_rect
;
3909 const int align
= cell
->GetAlignment();
3910 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
3912 const wxSize size
= cell
->GetSize();
3914 if ( size
.x
>= 0 && size
.x
< cell_rect
.width
)
3916 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
3917 rectItem
.x
+= (cell_rect
.width
- size
.x
)/2;
3918 else if ( align
& wxALIGN_RIGHT
)
3919 rectItem
.x
+= cell_rect
.width
- size
.x
;
3920 // else: wxALIGN_LEFT is the default
3923 if ( size
.y
>= 0 && size
.y
< cell_rect
.height
)
3925 if ( align
& wxALIGN_CENTER_VERTICAL
)
3926 rectItem
.y
+= (cell_rect
.height
- size
.y
)/2;
3927 else if ( align
& wxALIGN_BOTTOM
)
3928 rectItem
.y
+= cell_rect
.height
- size
.y
;
3929 // else: wxALIGN_TOP is the default
3933 wxPoint
pos( event
.GetPosition() );
3934 pos
.x
-= rectItem
.x
;
3935 pos
.y
-= rectItem
.y
;
3937 m_owner
->CalcUnscrolledPosition( pos
.x
, pos
.y
, &pos
.x
, &pos
.y
);
3939 /* ignore ret */ cell
->WXOnLeftClick( pos
, cell_rect
,
3940 model
, item
, col
->GetModelColumn());
3945 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3949 if (HasCurrentRow())
3955 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3959 if (HasCurrentRow())
3965 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3967 if( m_selection
.GetCount() != 1 )
3968 return wxDataViewItem();
3970 return GetItemByRow( m_selection
.Item(0));
3973 //-----------------------------------------------------------------------------
3975 //-----------------------------------------------------------------------------
3977 WX_DEFINE_LIST(wxDataViewColumnList
)
3979 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3980 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3981 EVT_SIZE(wxDataViewCtrl::OnSize
)
3984 wxDataViewCtrl::~wxDataViewCtrl()
3987 GetModel()->RemoveNotifier( m_notifier
);
3990 m_colsBestWidths
.clear();
3993 void wxDataViewCtrl::Init()
3995 m_cols
.DeleteContents(true);
3998 // No sorting column at start
3999 m_sortingColumnIdx
= wxNOT_FOUND
;
4001 m_headerArea
= NULL
;
4004 bool wxDataViewCtrl::Create(wxWindow
*parent
,
4009 const wxValidator
& validator
,
4010 const wxString
& name
)
4012 // if ( (style & wxBORDER_MASK) == 0)
4013 // style |= wxBORDER_SUNKEN;
4017 if (!wxControl::Create( parent
, id
, pos
, size
,
4018 style
| wxScrolledWindowStyle
, validator
, name
))
4021 SetInitialSize(size
);
4024 MacSetClipChildren( true );
4027 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
4029 // We use the cursor keys for moving the selection, not scrolling, so call
4030 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4031 // keyboard events forwarded to us from wxListMainWindow.
4032 DisableKeyboardScrolling();
4034 if (HasFlag(wxDV_NO_HEADER
))
4035 m_headerArea
= NULL
;
4037 m_headerArea
= new wxDataViewHeaderWindow(this);
4039 SetTargetWindow( m_clientArea
);
4041 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4043 sizer
->Add( m_headerArea
, 0, wxGROW
);
4044 sizer
->Add( m_clientArea
, 1, wxGROW
);
4050 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
4052 return wxBORDER_THEME
;
4056 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
4060 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
4063 // we need to process arrows ourselves for scrolling
4064 if ( nMsg
== WM_GETDLGCODE
)
4066 rc
|= DLGC_WANTARROWS
;
4074 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4076 wxSize newsize
= size
;
4077 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
4078 newsize
.y
-= m_headerArea
->GetSize().y
;
4083 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
4085 // We need to override OnSize so that our scrolled
4086 // window a) does call Layout() to use sizers for
4087 // positioning the controls but b) does not query
4088 // the sizer for their size and use that for setting
4089 // the scrollable area as set that ourselves by
4090 // calling SetScrollbar() further down.
4096 // We must redraw the headers if their height changed. Normally this
4097 // shouldn't happen as the control shouldn't let itself be resized beneath
4098 // its minimal height but avoid the display artefacts that appear if it
4099 // does happen, e.g. because there is really not enough vertical space.
4100 if ( !HasFlag(wxDV_NO_HEADER
) && m_headerArea
&&
4101 m_headerArea
->GetSize().y
<= m_headerArea
->GetBestSize(). y
)
4103 m_headerArea
->Refresh();
4107 void wxDataViewCtrl::SetFocus()
4110 m_clientArea
->SetFocus();
4113 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4115 if (!wxDataViewCtrlBase::AssociateModel( model
))
4118 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4120 model
->AddNotifier( m_notifier
);
4122 m_clientArea
->DestroyTree();
4124 m_clientArea
->BuildTree(model
);
4126 m_clientArea
->UpdateDisplay();
4131 #if wxUSE_DRAG_AND_DROP
4133 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4135 return m_clientArea
->EnableDragSource( format
);
4138 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4140 return m_clientArea
->EnableDropTarget( format
);
4143 #endif // wxUSE_DRAG_AND_DROP
4145 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4147 if (!wxDataViewCtrlBase::AppendColumn(col
))
4150 m_cols
.Append( col
);
4151 m_colsBestWidths
.push_back(0);
4152 OnColumnsCountChanged();
4156 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4158 if (!wxDataViewCtrlBase::PrependColumn(col
))
4161 m_cols
.Insert( col
);
4162 m_colsBestWidths
.insert(m_colsBestWidths
.begin(), 0);
4163 OnColumnsCountChanged();
4167 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4169 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4172 m_cols
.Insert( pos
, col
);
4173 m_colsBestWidths
.insert(m_colsBestWidths
.begin() + pos
, 0);
4174 OnColumnsCountChanged();
4178 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4181 m_headerArea
->UpdateColumn(idx
);
4183 m_clientArea
->UpdateDisplay();
4186 void wxDataViewCtrl::OnColumnsCountChanged()
4189 m_headerArea
->SetColumnCount(GetColumnCount());
4191 m_clientArea
->UpdateDisplay();
4194 void wxDataViewCtrl::DoSetExpanderColumn()
4196 m_clientArea
->UpdateDisplay();
4199 void wxDataViewCtrl::DoSetIndent()
4201 m_clientArea
->UpdateDisplay();
4204 unsigned int wxDataViewCtrl::GetColumnCount() const
4206 return m_cols
.GetCount();
4209 bool wxDataViewCtrl::SetRowHeight( int lineHeight
)
4211 if ( !m_clientArea
)
4214 m_clientArea
->SetRowHeight(lineHeight
);
4219 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4224 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4226 // columns can't be reordered if there is no header window which allows
4228 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4231 return GetColumn(idx
);
4234 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4236 const unsigned count
= m_cols
.size();
4237 for ( unsigned n
= 0; n
< count
; n
++ )
4239 if ( m_cols
[n
] == column
)
4246 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx
) const
4248 if ( m_colsBestWidths
[idx
] != 0 )
4249 return m_colsBestWidths
[idx
];
4251 const int count
= m_clientArea
->GetRowCount();
4252 wxDataViewColumn
*column
= GetColumn(idx
);
4253 wxDataViewRenderer
*renderer
=
4254 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
4256 class MaxWidthCalculator
4259 MaxWidthCalculator(wxDataViewMainWindow
*clientArea
,
4260 wxDataViewRenderer
*renderer
,
4261 const wxDataViewModel
*model
,
4264 m_clientArea(clientArea
),
4265 m_renderer(renderer
),
4271 void UpdateWithWidth(int width
)
4273 m_width
= wxMax(m_width
, width
);
4276 void UpdateWithRow(int row
)
4278 wxDataViewItem item
= m_clientArea
->GetItemByRow(row
);
4279 m_renderer
->PrepareForItem(m_model
, item
, m_column
);
4280 m_width
= wxMax(m_width
, m_renderer
->GetSize().x
);
4283 int GetMaxWidth() const { return m_width
; }
4287 wxDataViewMainWindow
*m_clientArea
;
4288 wxDataViewRenderer
*m_renderer
;
4289 const wxDataViewModel
*m_model
;
4293 MaxWidthCalculator
calculator(m_clientArea
, renderer
,
4294 GetModel(), column
->GetModelColumn());
4298 int header_width
= m_headerArea
->GetTextExtent(column
->GetTitle()).x
;
4299 // Labels on native MSW header are indented on both sides
4301 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea
);
4302 calculator
.UpdateWithWidth(header_width
);
4305 // The code below deserves some explanation. For very large controls, we
4306 // simply can't afford to calculate sizes for all items, it takes too
4307 // long. So the best we can do is to check the first and the last N/2
4308 // items in the control for some sufficiently large N and calculate best
4309 // sizes from that. That can result in the calculated best width being too
4310 // small for some outliers, but it's better to get slightly imperfect
4311 // result than to wait several seconds after every update. To avoid highly
4312 // visible miscalculations, we also include all currently visible items
4313 // no matter what. Finally, the value of N is determined dynamically by
4314 // measuring how much time we spent on the determining item widths so far.
4317 int top_part_end
= count
;
4318 static const long CALC_TIMEOUT
= 20/*ms*/;
4319 // don't call wxStopWatch::Time() too often
4320 static const unsigned CALC_CHECK_FREQ
= 100;
4323 // use some hard-coded limit, that's the best we can do without timer
4324 int top_part_end
= wxMin(500, count
);
4325 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4329 for ( row
= 0; row
< top_part_end
; row
++ )
4332 if ( row
% CALC_CHECK_FREQ
== CALC_CHECK_FREQ
-1 &&
4333 timer
.Time() > CALC_TIMEOUT
)
4335 #endif // wxUSE_STOPWATCH
4336 calculator
.UpdateWithRow(row
);
4339 // row is the first unmeasured item now; that's our value of N/2
4345 // add bottom N/2 items now:
4346 const int bottom_part_start
= wxMax(row
, count
- row
);
4347 for ( row
= bottom_part_start
; row
< count
; row
++ )
4349 calculator
.UpdateWithRow(row
);
4352 // finally, include currently visible items in the calculation:
4353 const wxPoint origin
= CalcUnscrolledPosition(wxPoint(0, 0));
4354 int first_visible
= m_clientArea
->GetLineAt(origin
.y
);
4355 int last_visible
= m_clientArea
->GetLineAt(origin
.y
+ GetClientSize().y
);
4357 first_visible
= wxMax(first_visible
, top_part_end
);
4358 last_visible
= wxMin(bottom_part_start
, last_visible
);
4360 for ( row
= first_visible
; row
< last_visible
; row
++ )
4362 calculator
.UpdateWithRow(row
);
4365 wxLogTrace("dataview",
4366 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4368 count
- bottom_part_start
,
4369 wxMax(0, last_visible
- first_visible
),
4373 int max_width
= calculator
.GetMaxWidth();
4374 if ( max_width
> 0 )
4375 max_width
+= 2 * PADDING_RIGHTLEFT
;
4377 const_cast<wxDataViewCtrl
*>(this)->m_colsBestWidths
[idx
] = max_width
;
4381 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4382 unsigned int WXUNUSED(new_pos
))
4384 // do _not_ reorder m_cols elements here, they should always be in the
4385 // order in which columns were added, we only display the columns in
4387 m_clientArea
->UpdateDisplay();
4390 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4392 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4396 m_colsBestWidths
.erase(m_colsBestWidths
.begin() + GetColumnIndex(column
));
4398 OnColumnsCountChanged();
4403 bool wxDataViewCtrl::ClearColumns()
4406 m_colsBestWidths
.clear();
4407 OnColumnsCountChanged();
4411 void wxDataViewCtrl::UpdateColBestWidth(int idx
)
4413 m_colsBestWidths
[idx
] = 0;
4416 m_headerArea
->UpdateColumn(idx
);
4419 void wxDataViewCtrl::UpdateColBestWidths()
4421 m_colsBestWidths
.clear();
4422 m_colsBestWidths
.resize(m_cols
.size());
4426 const unsigned cols
= m_headerArea
->GetColumnCount();
4427 for ( unsigned i
= 0; i
< cols
; i
++ )
4428 m_headerArea
->UpdateColumn(i
);
4432 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4435 unsigned int len
= GetColumnCount();
4436 for ( unsigned int i
= 0; i
< len
; i
++ )
4438 wxDataViewColumn
* col
= GetColumnAt(i
);
4445 // This returns the position in pixels which is not what we want.
4448 unsigned int len
= GetColumnCount();
4449 for ( unsigned int i
= 0; i
< len
; i
++ )
4451 wxDataViewColumn
* col
= GetColumnAt(i
);
4452 if (col
->IsHidden())
4454 ret
+= col
->GetWidth();
4457 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4465 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4467 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4468 : GetColumn(m_sortingColumnIdx
);
4471 wxDataViewItem
wxDataViewCtrl::DoGetCurrentItem() const
4473 return GetItemByRow(m_clientArea
->GetCurrentRow());
4476 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem
& item
)
4478 const int row
= m_clientArea
->GetRowByItem(item
);
4480 const unsigned oldCurrent
= m_clientArea
->GetCurrentRow();
4481 if ( static_cast<unsigned>(row
) != oldCurrent
)
4483 m_clientArea
->ChangeCurrentRow(row
);
4484 m_clientArea
->RefreshRow(oldCurrent
);
4485 m_clientArea
->RefreshRow(row
);
4489 // Selection code with wxDataViewItem as parameters
4490 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4492 return m_clientArea
->GetSelection();
4495 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4498 const wxDataViewSelection
& selections
= m_clientArea
->GetSelections();
4500 const size_t len
= selections
.size();
4501 for ( size_t i
= 0; i
< len
; i
++ )
4503 wxDataViewItem item
= m_clientArea
->GetItemByRow(selections
[i
]);
4510 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4517 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4519 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4521 wxDataViewItem last_parent
;
4523 int len
= sel
.GetCount();
4524 for( int i
= 0; i
< len
; i
++ )
4526 wxDataViewItem item
= sel
[i
];
4527 wxDataViewItem parent
= GetModel()->GetParent( item
);
4530 if (parent
!= last_parent
)
4531 ExpandAncestors(item
);
4534 last_parent
= parent
;
4535 int row
= m_clientArea
->GetRowByItem( item
);
4537 selection
.Add( static_cast<unsigned int>(row
) );
4540 m_clientArea
->SetSelections( selection
);
4543 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4545 ExpandAncestors( item
);
4547 int row
= m_clientArea
->GetRowByItem( item
);
4550 // Unselect all rows before select another in the single select mode
4551 if (m_clientArea
->IsSingleSel())
4552 m_clientArea
->SelectAllRows(false);
4554 m_clientArea
->SelectRow(row
, true);
4556 // Also set focus to the selected item
4557 m_clientArea
->ChangeCurrentRow( row
);
4561 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4563 int row
= m_clientArea
->GetRowByItem( item
);
4565 m_clientArea
->SelectRow(row
, false);
4568 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4570 int row
= m_clientArea
->GetRowByItem( item
);
4573 return m_clientArea
->IsRowSelected(row
);
4578 void wxDataViewCtrl::SelectAll()
4580 m_clientArea
->SelectAllRows(true);
4583 void wxDataViewCtrl::UnselectAll()
4585 m_clientArea
->SelectAllRows(false);
4588 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4592 if( row
> (int) m_clientArea
->GetRowCount() )
4593 row
= m_clientArea
->GetRowCount();
4595 int first
= m_clientArea
->GetFirstVisibleRow();
4596 int last
= m_clientArea
->GetLastVisibleRow();
4598 m_clientArea
->ScrollTo( row
, column
);
4599 else if( row
> last
)
4600 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4602 m_clientArea
->ScrollTo( first
, column
);
4605 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4607 ExpandAncestors( item
);
4609 m_clientArea
->RecalculateDisplay();
4611 int row
= m_clientArea
->GetRowByItem(item
);
4614 if( column
== NULL
)
4615 EnsureVisible(row
, -1);
4617 EnsureVisible( row
, GetColumnIndex(column
) );
4622 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4623 wxDataViewColumn
* &column
) const
4625 m_clientArea
->HitTest(point
, item
, column
);
4628 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4629 const wxDataViewColumn
* column
) const
4631 return m_clientArea
->GetItemRect(item
, column
);
4634 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4636 return m_clientArea
->GetItemByRow( row
);
4639 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4641 return m_clientArea
->GetRowByItem( item
);
4644 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4646 ExpandAncestors( item
);
4648 int row
= m_clientArea
->GetRowByItem( item
);
4650 m_clientArea
->Expand(row
);
4653 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4655 int row
= m_clientArea
->GetRowByItem( item
);
4657 m_clientArea
->Collapse(row
);
4660 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4662 int row
= m_clientArea
->GetRowByItem( item
);
4664 return m_clientArea
->IsExpanded(row
);
4668 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4670 wxDataViewColumn
* col
= GetColumn( column
);
4674 wxRect itemRect
= GetItemRect(item
, col
);
4675 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4676 if (renderer
->GetMode() == wxDATAVIEW_CELL_EDITABLE
)
4677 renderer
->StartEditing(item
, itemRect
);
4680 #endif // !wxUSE_GENERICDATAVIEWCTRL
4682 #endif // wxUSE_DATAVIEWCTRL