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"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
58 static const int SCROLL_UNIT_X
= 15;
60 // the cell padding on the left/right
61 static const int PADDING_RIGHTLEFT
= 3;
63 // the expander space margin
64 static const int EXPANDER_MARGIN
= 4;
67 static const int EXPANDER_OFFSET
= 4;
69 static const int EXPANDER_OFFSET
= 1;
72 // Below is the compare stuff.
73 // For the generic implementation, both the leaf nodes and the nodes are sorted for
74 // fast search when needed
75 static wxDataViewModel
* g_model
;
76 static int g_column
= -2;
77 static bool g_asending
= true;
79 //-----------------------------------------------------------------------------
80 // wxDataViewHeaderWindow
81 //-----------------------------------------------------------------------------
83 class wxDataViewHeaderWindow
: public wxHeaderCtrl
86 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
87 : wxHeaderCtrl(parent
)
91 wxDataViewCtrl
*GetOwner() const
92 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
95 // implement/override wxHeaderCtrl functions by forwarding them to the main
97 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
99 return *(GetOwner()->GetColumn(idx
));
102 // FIXME: currently unused
103 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
105 wxDataViewCtrl
* const owner
= GetOwner();
107 int widthContents
= owner
->GetBestColumnWidth(idx
);
108 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
109 owner
->OnColumnChange(idx
);
115 bool SendEvent(wxEventType type
, unsigned int n
)
117 wxDataViewCtrl
* const owner
= GetOwner();
118 wxDataViewEvent
event(type
, owner
->GetId());
120 event
.SetEventObject(owner
);
122 event
.SetDataViewColumn(owner
->GetColumn(n
));
123 event
.SetModel(owner
->GetModel());
125 // for events created by wxDataViewHeaderWindow the
126 // row / value fields are not valid
127 return owner
->GetEventHandler()->ProcessEvent(event
);
130 void OnClick(wxHeaderCtrlEvent
& event
)
132 const unsigned idx
= event
.GetColumn();
134 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
137 // default handling for the column click is to sort by this column or
138 // toggle its sort order
139 wxDataViewCtrl
* const owner
= GetOwner();
140 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
141 if ( !col
->IsSortable() )
143 // no default handling for non-sortable columns
148 if ( col
->IsSortKey() )
150 // already using this column for sorting, just change the order
151 col
->ToggleSortOrder();
153 else // not using this column for sorting yet
155 // first unset the old sort column if any
156 int oldSortKey
= owner
->GetSortingColumnIndex();
157 if ( oldSortKey
!= wxNOT_FOUND
)
159 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
160 owner
->OnColumnChange(oldSortKey
);
163 owner
->SetSortingColumnIndex(idx
);
167 wxDataViewModel
* const model
= owner
->GetModel();
171 owner
->OnColumnChange(idx
);
174 void OnRClick(wxHeaderCtrlEvent
& event
)
176 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
181 void OnResize(wxHeaderCtrlEvent
& event
)
183 wxDataViewCtrl
* const owner
= GetOwner();
185 const unsigned col
= event
.GetColumn();
186 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
187 GetOwner()->OnColumnChange(col
);
190 void OnEndReorder(wxHeaderCtrlEvent
& event
)
192 wxDataViewCtrl
* const owner
= GetOwner();
193 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
194 event
.GetNewOrder());
197 DECLARE_EVENT_TABLE()
198 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
201 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
202 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
203 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
205 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
206 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
208 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
211 //-----------------------------------------------------------------------------
212 // wxDataViewRenameTimer
213 //-----------------------------------------------------------------------------
215 class wxDataViewRenameTimer
: public wxTimer
218 wxDataViewMainWindow
*m_owner
;
221 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
225 //-----------------------------------------------------------------------------
226 // wxDataViewTreeNode
227 //-----------------------------------------------------------------------------
229 class wxDataViewTreeNode
;
230 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
231 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
233 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
234 wxDataViewTreeNode
** node2
);
235 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
237 class wxDataViewTreeNode
240 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
247 m_hasChildren
= false;
251 ~wxDataViewTreeNode()
255 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
256 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
257 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
258 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
260 void AddNode( wxDataViewTreeNode
* node
)
262 m_leaves
.Add( node
->GetItem().GetID() );
264 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
267 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
269 void AddLeaf( void * leaf
)
271 m_leaves
.Add( leaf
);
273 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
276 wxDataViewItem
& GetItem() { return m_item
; }
277 const wxDataViewItem
& GetItem() const { return m_item
; }
278 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
280 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
281 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
282 int GetIndentLevel() const
285 const wxDataViewTreeNode
* node
= this;
286 while( node
->GetParent()->GetParent() != NULL
)
288 node
= node
->GetParent();
301 int len
= m_nodes
.GetCount();
303 for ( int i
= 0;i
< len
; i
++)
304 sum
+= m_nodes
[i
]->GetSubTreeCount();
306 sum
+= m_leaves
.GetCount();
309 ChangeSubTreeCount(-sum
);
315 ChangeSubTreeCount(sum
);
318 bool HasChildren() const { return m_hasChildren
; }
319 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
321 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
322 int GetSubTreeCount() const { return m_subTreeCount
; }
323 void ChangeSubTreeCount( int num
)
327 m_subTreeCount
+= num
;
329 m_parent
->ChangeSubTreeCount(num
);
336 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
337 int len
= m_nodes
.GetCount();
338 for (int i
= 0; i
< len
; i
++)
339 m_nodes
[i
]->Resort();
340 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
345 wxDataViewTreeNode
*m_parent
;
346 wxDataViewTreeNodes m_nodes
;
347 wxDataViewTreeLeaves m_leaves
;
348 wxDataViewItem m_item
;
354 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
355 wxDataViewTreeNode
** node2
)
357 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
360 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
362 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
366 //-----------------------------------------------------------------------------
367 // wxDataViewMainWindow
368 //-----------------------------------------------------------------------------
370 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
372 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
373 WX_DEFINE_LIST(ItemList
)
375 class wxDataViewMainWindow
: public wxWindow
378 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
380 const wxPoint
&pos
= wxDefaultPosition
,
381 const wxSize
&size
= wxDefaultSize
,
382 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
383 virtual ~wxDataViewMainWindow();
385 bool IsVirtualList() const { return m_root
== NULL
; }
387 // notifications from wxDataViewModel
388 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
389 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
390 bool ItemChanged( const wxDataViewItem
&item
);
391 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
395 if (!IsVirtualList())
405 g_model
= GetOwner()->GetModel();
406 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
409 if (g_model
->HasDefaultCompare())
417 g_column
= col
->GetModelColumn();
418 g_asending
= col
->IsSortOrderAscending();
421 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
422 wxDataViewCtrl
*GetOwner() { return m_owner
; }
423 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
425 #if wxUSE_DRAG_AND_DROP
426 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
427 #endif // wxUSE_DRAG_AND_DROP
428 void OnPaint( wxPaintEvent
&event
);
429 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
430 void OnChar( wxKeyEvent
&event
);
431 void OnMouse( wxMouseEvent
&event
);
432 void OnSetFocus( wxFocusEvent
&event
);
433 void OnKillFocus( wxFocusEvent
&event
);
435 void UpdateDisplay();
436 void RecalculateDisplay();
437 void OnInternalIdle();
439 void OnRenameTimer();
441 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
442 void ScrollTo( int rows
, int column
);
444 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
445 void ChangeCurrentRow( unsigned int row
);
447 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
448 bool IsEmpty() { return GetRowCount() == 0; }
450 int GetCountPerPage() const;
451 int GetEndOfLastCol() const;
452 unsigned int GetFirstVisibleRow() const;
454 // I change this method to un const because in the tree view,
455 // the displaying number of the tree are changing along with the
456 // expanding/collapsing of the tree nodes
457 unsigned int GetLastVisibleRow();
458 unsigned int GetRowCount();
460 wxDataViewItem
GetSelection() const;
461 wxDataViewSelection
GetSelections(){ return m_selection
; }
462 void SetSelections( const wxDataViewSelection
& sel
)
463 { m_selection
= sel
; UpdateDisplay(); }
464 void Select( const wxArrayInt
& aSelections
);
465 void SelectAllRows( bool on
);
466 void SelectRow( unsigned int row
, bool on
);
467 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
468 void ReverseRowSelection( unsigned int row
);
469 bool IsRowSelected( unsigned int row
);
470 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
472 void RefreshRow( unsigned int row
);
473 void RefreshRows( unsigned int from
, unsigned int to
);
474 void RefreshRowsAfter( unsigned int firstRow
);
476 // returns the colour to be used for drawing the rules
477 wxColour
GetRuleColour() const
479 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
482 wxRect
GetLineRect( unsigned int row
) const;
484 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
485 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
486 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
488 // Some useful functions for row and item mapping
489 wxDataViewItem
GetItemByRow( unsigned int row
) const;
490 int GetRowByItem( const wxDataViewItem
& item
) const;
492 // Methods for building the mapping tree
493 void BuildTree( wxDataViewModel
* model
);
495 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
496 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
498 void Expand( unsigned int row
);
499 void Collapse( unsigned int row
);
500 bool IsExpanded( unsigned int row
) const;
501 bool HasChildren( unsigned int row
) const;
503 #if wxUSE_DRAG_AND_DROP
504 bool EnableDragSource( const wxDataFormat
&format
);
505 bool EnableDropTarget( const wxDataFormat
&format
);
507 void RemoveDropHint();
508 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
509 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
510 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
512 #endif // wxUSE_DRAG_AND_DROP
515 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
516 // We did not need this temporarily
517 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
519 int RecalculateCount();
521 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
523 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
526 wxDataViewCtrl
*m_owner
;
530 wxDataViewColumn
*m_currentCol
;
531 unsigned int m_currentRow
;
532 wxDataViewSelection m_selection
;
534 wxDataViewRenameTimer
*m_renameTimer
;
539 #if wxUSE_DRAG_AND_DROP
544 wxDataFormat m_dragFormat
;
547 wxDataFormat m_dropFormat
;
549 unsigned int m_dropHintLine
;
550 #endif // wxUSE_DRAG_AND_DROP
552 // for double click logic
553 unsigned int m_lineLastClicked
,
554 m_lineBeforeLastClicked
,
555 m_lineSelectSingleOnUp
;
557 // the pen used to draw horiz/vertical rules
560 // the pen used to draw the expander and the lines
563 // This is the tree structure of the model
564 wxDataViewTreeNode
* m_root
;
567 // This is the tree node under the cursor
568 wxDataViewTreeNode
* m_underMouse
;
571 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
572 DECLARE_EVENT_TABLE()
575 // ---------------------------------------------------------
576 // wxGenericDataViewModelNotifier
577 // ---------------------------------------------------------
579 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
582 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
583 { m_mainWindow
= mainWindow
; }
585 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
586 { return m_mainWindow
->ItemAdded( parent
, item
); }
587 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
588 { return m_mainWindow
->ItemDeleted( parent
, item
); }
589 virtual bool ItemChanged( const wxDataViewItem
& item
)
590 { return m_mainWindow
->ItemChanged(item
); }
591 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
592 { return m_mainWindow
->ValueChanged( item
, col
); }
593 virtual bool Cleared()
594 { return m_mainWindow
->Cleared(); }
595 virtual void Resort()
596 { m_mainWindow
->Resort(); }
598 wxDataViewMainWindow
*m_mainWindow
;
601 // ---------------------------------------------------------
602 // wxDataViewRenderer
603 // ---------------------------------------------------------
605 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
607 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
608 wxDataViewCellMode mode
,
610 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
614 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
618 wxDataViewRenderer::~wxDataViewRenderer()
623 wxDC
*wxDataViewRenderer::GetDC()
627 if (GetOwner() == NULL
)
629 if (GetOwner()->GetOwner() == NULL
)
631 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
637 void wxDataViewRenderer::SetAlignment( int align
)
642 int wxDataViewRenderer::GetAlignment() const
647 // ---------------------------------------------------------
648 // wxDataViewCustomRenderer
649 // ---------------------------------------------------------
651 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
653 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
654 wxDataViewCellMode mode
, int align
) :
655 wxDataViewRenderer( varianttype
, mode
, align
)
659 // ---------------------------------------------------------
660 // wxDataViewTextRenderer
661 // ---------------------------------------------------------
663 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
665 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
666 wxDataViewCellMode mode
, int align
) :
667 wxDataViewRenderer( varianttype
, mode
, align
)
671 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
673 m_text
= value
.GetString();
678 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
683 bool wxDataViewTextRenderer::HasEditorCtrl() const
688 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
689 wxRect labelRect
, const wxVariant
&value
)
691 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
692 wxPoint(labelRect
.x
,labelRect
.y
),
693 wxSize(labelRect
.width
,labelRect
.height
) );
695 // select the text in the control an place the cursor at the end
696 ctrl
->SetInsertionPointEnd();
702 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
704 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
705 value
= text
->GetValue();
709 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
711 RenderText(m_text
, 0, rect
, dc
, state
);
715 wxSize
wxDataViewTextRenderer::GetSize() const
717 const wxDataViewCtrl
*view
= GetView();
719 return view
->wxWindowBase::GetTextExtent( m_text
);
720 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
723 // ---------------------------------------------------------
724 // wxDataViewBitmapRenderer
725 // ---------------------------------------------------------
727 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
729 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
730 wxDataViewCellMode mode
, int align
) :
731 wxDataViewRenderer( varianttype
, mode
, align
)
735 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
737 if (value
.GetType() == wxT("wxBitmap"))
739 if (value
.GetType() == wxT("wxIcon"))
745 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
750 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
753 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
754 else if (m_icon
.Ok())
755 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
760 wxSize
wxDataViewBitmapRenderer::GetSize() const
763 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
764 else if (m_icon
.Ok())
765 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
767 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
770 // ---------------------------------------------------------
771 // wxDataViewToggleRenderer
772 // ---------------------------------------------------------
774 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
776 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
777 wxDataViewCellMode mode
, int align
) :
778 wxDataViewRenderer( varianttype
, mode
, align
)
783 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
785 m_toggle
= value
.GetBool();
790 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
795 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
799 flags
|= wxCONTROL_CHECKED
;
800 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
801 flags
|= wxCONTROL_DISABLED
;
803 wxRendererNative::Get().DrawCheckBox(
804 GetOwner()->GetOwner(),
812 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
813 wxDataViewModel
*model
,
814 const wxDataViewItem
& item
, unsigned int col
)
816 model
->ChangeValue(!m_toggle
, item
, col
);
820 wxSize
wxDataViewToggleRenderer::GetSize() const
822 // the window parameter is not used by GetCheckBoxSize() so it's
824 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
827 // ---------------------------------------------------------
828 // wxDataViewProgressRenderer
829 // ---------------------------------------------------------
831 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
833 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
834 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
835 wxDataViewRenderer( varianttype
, mode
, align
)
841 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
843 m_value
= (long) value
;
845 if (m_value
< 0) m_value
= 0;
846 if (m_value
> 100) m_value
= 100;
851 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
853 value
= (long) m_value
;
858 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
860 // deflate the rect to leave a small border between bars in adjacent rows
861 wxRect bar
= rect
.Deflate(0, 1);
863 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
864 dc
->SetPen( *wxBLACK_PEN
);
865 dc
->DrawRectangle( bar
);
867 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
868 dc
->SetPen( *wxTRANSPARENT_PEN
);
870 const wxDataViewItemAttr
& attr
= GetAttr();
871 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
873 dc
->DrawRectangle( bar
);
878 wxSize
wxDataViewProgressRenderer::GetSize() const
880 return wxSize(40,12);
883 // ---------------------------------------------------------
884 // wxDataViewDateRenderer
885 // ---------------------------------------------------------
887 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
889 #if wxUSE_DATE_RENDERER_POPUP
891 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
894 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
895 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
896 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
901 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
902 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
903 sizer
->Add( m_cal
, 1, wxGROW
);
908 void OnCalendar( wxCalendarEvent
&event
);
910 wxCalendarCtrl
*m_cal
;
911 wxDataViewModel
*m_model
;
913 const wxDataViewItem
& m_item
;
916 virtual void OnDismiss()
921 DECLARE_EVENT_TABLE()
924 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
925 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
928 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
930 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
934 #endif // wxUSE_DATE_RENDERER_POPUP
936 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
938 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
939 wxDataViewCellMode mode
, int align
) :
940 wxDataViewRenderer( varianttype
, mode
, align
)
944 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
946 m_date
= value
.GetDateTime();
951 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
957 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
959 wxString tmp
= m_date
.FormatDate();
960 RenderText( tmp
, 0, cell
, dc
, state
);
964 wxSize
wxDataViewDateRenderer::GetSize() const
966 const wxDataViewCtrl
* view
= GetView();
967 wxString tmp
= m_date
.FormatDate();
969 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
970 return wxSize(x
,y
+d
);
973 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
974 const wxDataViewItem
& item
, unsigned int col
)
977 model
->GetValue( variant
, item
, col
);
978 wxDateTime value
= variant
.GetDateTime();
980 #if wxUSE_DATE_RENDERER_POPUP
981 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
982 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
983 wxPoint pos
= wxGetMousePosition();
986 popup
->Popup( popup
->m_cal
);
987 #else // !wxUSE_DATE_RENDERER_POPUP
988 wxMessageBox(value
.Format());
989 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
993 // ---------------------------------------------------------
994 // wxDataViewIconTextRenderer
995 // ---------------------------------------------------------
997 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
999 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1000 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1001 wxDataViewRenderer( varianttype
, mode
, align
)
1004 SetAlignment(align
);
1007 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1013 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1018 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1022 const wxIcon
& icon
= m_value
.GetIcon();
1025 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1026 xoffset
= icon
.GetWidth()+4;
1029 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1034 wxSize
wxDataViewIconTextRenderer::GetSize() const
1036 const wxDataViewCtrl
*view
= GetView();
1037 if (!m_value
.GetText().empty())
1040 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1042 if (m_value
.GetIcon().IsOk())
1043 x
+= m_value
.GetIcon().GetWidth() + 4;
1044 return wxSize( x
, y
);
1046 return wxSize(80,20);
1049 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1051 wxDataViewIconText iconText
;
1054 wxString text
= iconText
.GetText();
1056 // adjust the label rect to take the width of the icon into account
1057 if (iconText
.GetIcon().IsOk())
1059 int w
= iconText
.GetIcon().GetWidth() + 4;
1061 labelRect
.width
-= w
;
1064 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1065 wxPoint(labelRect
.x
,labelRect
.y
),
1066 wxSize(labelRect
.width
,labelRect
.height
) );
1068 // select the text in the control an place the cursor at the end
1069 ctrl
->SetInsertionPointEnd();
1075 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
& value
)
1077 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1079 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1084 //-----------------------------------------------------------------------------
1085 // wxDataViewDropTarget
1086 //-----------------------------------------------------------------------------
1088 #if wxUSE_DRAG_AND_DROP
1090 class wxBitmapCanvas
: public wxWindow
1093 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1094 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1097 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1100 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1103 dc
.DrawBitmap( m_bitmap
, 0, 0);
1109 class wxDataViewDropSource
: public wxDropSource
1112 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1120 ~wxDataViewDropSource()
1125 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1127 wxPoint pos
= wxGetMousePosition();
1131 int liney
= m_win
->GetLineStart( m_row
);
1133 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1134 m_win
->ClientToScreen( &linex
, &liney
);
1135 m_dist_x
= pos
.x
- linex
;
1136 m_dist_y
= pos
.y
- liney
;
1139 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1141 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1142 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1144 wxFRAME_TOOL_WINDOW
|
1145 wxFRAME_FLOAT_ON_PARENT
|
1146 wxFRAME_NO_TASKBAR
|
1148 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1153 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1154 m_hint
->SetTransparent( 128 );
1160 wxDataViewMainWindow
*m_win
;
1163 int m_dist_x
,m_dist_y
;
1167 class wxDataViewDropTarget
: public wxDropTarget
1170 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1176 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1178 wxDataFormat format
= GetMatchingPair();
1179 if (format
== wxDF_INVALID
)
1181 return m_win
->OnDragOver( format
, x
, y
, def
);
1184 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1186 wxDataFormat format
= GetMatchingPair();
1187 if (format
== wxDF_INVALID
)
1189 return m_win
->OnDrop( format
, x
, y
);
1192 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1194 wxDataFormat format
= GetMatchingPair();
1195 if (format
== wxDF_INVALID
)
1199 return m_win
->OnData( format
, x
, y
, def
);
1202 virtual void OnLeave()
1203 { m_win
->OnLeave(); }
1205 wxDataViewMainWindow
*m_win
;
1208 #endif // wxUSE_DRAG_AND_DROP
1210 //-----------------------------------------------------------------------------
1211 // wxDataViewRenameTimer
1212 //-----------------------------------------------------------------------------
1214 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1219 void wxDataViewRenameTimer::Notify()
1221 m_owner
->OnRenameTimer();
1224 //-----------------------------------------------------------------------------
1225 // wxDataViewMainWindow
1226 //-----------------------------------------------------------------------------
1228 // The tree building helper, declared firstly
1229 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
1230 wxDataViewTreeNode
* node
);
1232 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1234 if (row1
> row2
) return 1;
1235 if (row1
== row2
) return 0;
1239 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1241 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1242 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1243 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1244 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1245 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1246 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1249 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1250 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1251 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1252 m_selection( wxDataViewSelectionCmp
)
1257 m_lastOnSame
= false;
1258 m_renameTimer
= new wxDataViewRenameTimer( this );
1260 // TODO: user better initial values/nothing selected
1261 m_currentCol
= NULL
;
1264 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1266 #if wxUSE_DRAG_AND_DROP
1268 m_dragStart
= wxPoint(0,0);
1270 m_dragEnabled
= false;
1271 m_dropEnabled
= false;
1273 m_dropHintLine
= (unsigned int) -1;
1274 #endif // wxUSE_DRAG_AND_DROP
1276 m_lineLastClicked
= (unsigned int) -1;
1277 m_lineBeforeLastClicked
= (unsigned int) -1;
1278 m_lineSelectSingleOnUp
= (unsigned int) -1;
1282 SetBackgroundColour( *wxWHITE
);
1284 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1286 m_penRule
= wxPen(GetRuleColour());
1288 // compose a pen whichcan draw black lines
1289 // TODO: maybe there is something system colour to use
1290 m_penExpander
= wxPen(wxColour(0,0,0));
1292 m_root
= new wxDataViewTreeNode( NULL
);
1293 m_root
->SetHasChildren(true);
1295 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1297 m_underMouse
= NULL
;
1301 wxDataViewMainWindow::~wxDataViewMainWindow()
1304 delete m_renameTimer
;
1308 #if wxUSE_DRAG_AND_DROP
1309 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1311 m_dragFormat
= format
;
1312 m_dragEnabled
= format
!= wxDF_INVALID
;
1317 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1319 m_dropFormat
= format
;
1320 m_dropEnabled
= format
!= wxDF_INVALID
;
1323 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1328 void wxDataViewMainWindow::RemoveDropHint()
1333 RefreshRow( m_dropHintLine
);
1334 m_dropHintLine
= (unsigned int) -1;
1338 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1339 wxCoord y
, wxDragResult def
)
1343 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1344 unsigned int row
= GetLineAt( yy
);
1346 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1352 wxDataViewItem item
= GetItemByRow( row
);
1354 wxDataViewModel
*model
= GetOwner()->GetModel();
1356 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1357 event
.SetEventObject( m_owner
);
1358 event
.SetItem( item
);
1359 event
.SetModel( model
);
1360 event
.SetDataFormat( format
);
1361 if (!m_owner
->HandleWindowEvent( event
))
1367 if (!event
.IsAllowed())
1374 if (m_dropHint
&& (row
!= m_dropHintLine
))
1375 RefreshRow( m_dropHintLine
);
1377 m_dropHintLine
= row
;
1383 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1389 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1390 unsigned int row
= GetLineAt( yy
);
1392 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1395 wxDataViewItem item
= GetItemByRow( row
);
1397 wxDataViewModel
*model
= GetOwner()->GetModel();
1399 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1400 event
.SetEventObject( m_owner
);
1401 event
.SetItem( item
);
1402 event
.SetModel( model
);
1403 event
.SetDataFormat( format
);
1404 if (!m_owner
->HandleWindowEvent( event
))
1407 if (!event
.IsAllowed())
1413 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1418 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1419 unsigned int row
= GetLineAt( yy
);
1421 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1424 wxDataViewItem item
= GetItemByRow( row
);
1426 wxDataViewModel
*model
= GetOwner()->GetModel();
1428 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1430 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1431 event
.SetEventObject( m_owner
);
1432 event
.SetItem( item
);
1433 event
.SetModel( model
);
1434 event
.SetDataFormat( format
);
1435 event
.SetDataSize( obj
->GetSize() );
1436 event
.SetDataBuffer( obj
->GetData() );
1437 if (!m_owner
->HandleWindowEvent( event
))
1440 if (!event
.IsAllowed())
1446 void wxDataViewMainWindow::OnLeave()
1451 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1453 int height
= GetLineHeight( row
);
1455 unsigned int cols
= GetOwner()->GetColumnCount();
1457 for (col
= 0; col
< cols
; col
++)
1459 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1460 if (column
->IsHidden())
1461 continue; // skip it!
1462 width
+= column
->GetWidth();
1466 if (!IsVirtualList())
1468 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1469 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1470 indent
= indent
+ m_lineHeight
;
1471 // try to use the m_lineHeight as the expander space
1473 if(!node
->HasChildren())
1478 wxBitmap
bitmap( width
, height
);
1479 wxMemoryDC
dc( bitmap
);
1480 dc
.SetFont( GetFont() );
1481 dc
.SetPen( *wxBLACK_PEN
);
1482 dc
.SetBrush( *wxWHITE_BRUSH
);
1483 dc
.DrawRectangle( 0,0,width
,height
);
1485 wxDataViewModel
*model
= m_owner
->GetModel();
1487 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1490 // TODO-RTL: last column for RTL support
1491 expander
= GetOwner()->GetColumnAt( 0 );
1492 GetOwner()->SetExpanderColumn(expander
);
1497 for (col
= 0; col
< cols
; col
++)
1499 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1500 wxDataViewRenderer
*cell
= column
->GetRenderer();
1502 if (column
->IsHidden())
1503 continue; // skip it!
1505 width
= column
->GetWidth();
1507 if (column
== expander
)
1511 wxDataViewItem item
= GetItemByRow( row
);
1512 model
->GetValue( value
, item
, column
->GetModelColumn());
1513 cell
->SetValue( value
);
1515 wxDataViewItemAttr attr
;
1516 model
->GetAttr(item
, column
->GetModelColumn(), attr
);
1517 cell
->SetAttr(attr
);
1519 wxRect
item_rect(x
, 0, width
, height
);
1520 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1522 // dc.SetClippingRegion( item_rect );
1523 cell
->WXCallRender(item_rect
, &dc
, 0);
1524 // dc.DestroyClippingRegion();
1532 #endif // wxUSE_DRAG_AND_DROP
1535 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1537 wxDataViewModel
*model
= GetOwner()->GetModel();
1538 wxAutoBufferedPaintDC
dc( this );
1541 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1542 dc
.SetPen( *wxTRANSPARENT_PEN
);
1543 dc
.DrawRectangle(GetClientSize());
1547 GetOwner()->PrepareDC( dc
);
1548 dc
.SetFont( GetFont() );
1550 wxRect update
= GetUpdateRegion().GetBox();
1551 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1553 // compute which items needs to be redrawn
1554 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1555 unsigned int item_count
=
1556 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1557 (int)(GetRowCount( ) - item_start
));
1558 unsigned int item_last
= item_start
+ item_count
;
1559 // Get the parent of DataViewCtrl
1560 wxWindow
*parent
= GetParent()->GetParent();
1561 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1562 cache_event
.SetEventObject(GetParent());
1563 cache_event
.SetCache(item_start
, item_last
- 1);
1564 parent
->ProcessWindowEvent(cache_event
);
1566 // compute which columns needs to be redrawn
1567 unsigned int cols
= GetOwner()->GetColumnCount();
1568 unsigned int col_start
= 0;
1569 unsigned int x_start
;
1570 for (x_start
= 0; col_start
< cols
; col_start
++)
1572 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1573 if (col
->IsHidden())
1574 continue; // skip it!
1576 unsigned int w
= col
->GetWidth();
1577 if (x_start
+w
>= (unsigned int)update
.x
)
1583 unsigned int col_last
= col_start
;
1584 unsigned int x_last
= x_start
;
1585 for (; col_last
< cols
; col_last
++)
1587 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1588 if (col
->IsHidden())
1589 continue; // skip it!
1591 if (x_last
> (unsigned int)update
.GetRight())
1594 x_last
+= col
->GetWidth();
1597 // Draw horizontal rules if required
1598 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1600 dc
.SetPen(m_penRule
);
1601 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1603 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1605 int y
= GetLineStart( i
);
1606 dc
.DrawLine(x_start
, y
, x_last
, y
);
1610 // Draw vertical rules if required
1611 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1613 dc
.SetPen(m_penRule
);
1614 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1617 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1619 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1620 if (col
->IsHidden())
1621 continue; // skip it
1623 dc
.DrawLine(x
, GetLineStart( item_start
),
1624 x
, GetLineStart( item_last
) );
1626 x
+= col
->GetWidth();
1629 // Draw last vertical rule
1630 dc
.DrawLine(x
, GetLineStart( item_start
),
1631 x
, GetLineStart( item_last
) );
1634 // redraw the background for the items which are selected/current
1635 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1637 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1638 if (selected
|| item
== m_currentRow
)
1640 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1641 if (item
== m_currentRow
)
1642 flags
|= wxCONTROL_CURRENT
;
1644 flags
|= wxCONTROL_FOCUSED
;
1646 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1647 wxRendererNative::Get().DrawItemSelectionRect
1657 #if wxUSE_DRAG_AND_DROP
1660 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1661 x_last
, GetLineHeight( m_dropHintLine
) );
1662 dc
.SetPen( *wxBLACK_PEN
);
1663 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1664 dc
.DrawRectangle( rect
);
1666 #endif // wxUSE_DRAG_AND_DROP
1668 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1671 // TODO-RTL: last column for RTL support
1672 expander
= GetOwner()->GetColumnAt( 0 );
1673 GetOwner()->SetExpanderColumn(expander
);
1676 // redraw all cells for all rows which must be repainted and all columns
1678 cell_rect
.x
= x_start
;
1679 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1681 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1682 wxDataViewRenderer
*cell
= col
->GetRenderer();
1683 cell_rect
.width
= col
->GetWidth();
1685 if (col
->IsHidden())
1686 continue; // skip it!
1688 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1690 // get the cell value and set it into the renderer
1692 wxDataViewTreeNode
*node
= NULL
;
1693 wxDataViewItem dataitem
;
1695 if (!IsVirtualList())
1697 node
= GetTreeNodeByRow(item
);
1701 dataitem
= node
->GetItem();
1703 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1704 !model
->HasContainerColumns(dataitem
))
1709 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1712 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1713 cell
->SetValue( value
);
1715 wxDataViewItemAttr attr
;
1716 model
->GetAttr(dataitem
, col
->GetModelColumn(), attr
);
1717 cell
->SetAttr(attr
);
1720 cell_rect
.y
= GetLineStart( item
);
1721 cell_rect
.height
= GetLineHeight( item
);
1723 // Draw the expander here.
1725 if ((!IsVirtualList()) && (col
== expander
))
1727 indent
= node
->GetIndentLevel();
1729 // Calculate the indent first
1730 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1732 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1734 // change the cell_rect.x to the appropriate pos
1735 int expander_x
= indent
+ EXPANDER_MARGIN
;
1736 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2)
1737 - (expander_width
/2) - EXPANDER_OFFSET
;
1739 indent
= indent
+ m_lineHeight
;
1740 // try to use the m_lineHeight as the expander space
1742 dc
.SetPen( m_penExpander
);
1743 dc
.SetBrush( wxNullBrush
);
1744 if( node
->HasChildren() )
1746 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1748 if (m_underMouse
== node
)
1750 flag
|= wxCONTROL_CURRENT
;
1752 if( node
->IsOpen() )
1753 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
,
1754 flag
|wxCONTROL_EXPANDED
);
1756 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1759 // force the expander column to left-center align
1760 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1762 if (node
&& !node
->HasChildren())
1764 // Yes, if the node does not have any child, it must be a leaf which
1765 // mean that it is a temporarily created by GetTreeNodeByRow
1769 wxRect item_rect
= cell_rect
;
1770 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1772 // account for the tree indent (harmless if we're not indented)
1773 item_rect
.x
+= indent
;
1774 item_rect
.width
-= indent
;
1777 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1778 state
|= wxDATAVIEW_CELL_SELECTED
;
1780 // TODO: it would be much more efficient to create a clipping
1781 // region for the entire column being rendered (in the OnPaint
1782 // of wxDataViewMainWindow) instead of a single clip region for
1783 // each cell. However it would mean that each renderer should
1784 // respect the given wxRect's top & bottom coords, eventually
1785 // violating only the left & right coords - however the user can
1786 // make its own renderer and thus we cannot be sure of that.
1787 wxDCClipper
clip(dc
, item_rect
);
1789 cell
->WXCallRender(item_rect
, &dc
, state
);
1792 cell_rect
.x
+= cell_rect
.width
;
1796 void wxDataViewMainWindow::OnRenameTimer()
1798 // We have to call this here because changes may just have
1799 // been made and no screen update taken place.
1802 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1803 // (needs to be tested!)
1807 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1809 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1811 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1814 //-----------------------------------------------------------------------------
1815 // Helper class for do operation on the tree node
1816 //-----------------------------------------------------------------------------
1821 virtual ~DoJob() { }
1823 // The return value control how the tree-walker tranverse the tree
1824 // 0: Job done, stop tranverse and return
1825 // 1: Ignore the current node's subtree and continue
1826 // 2: Job not done, continue
1827 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1828 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1829 virtual int operator() ( void * n
) = 0;
1832 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1837 switch( func( node
) )
1848 wxDataViewTreeNodes nodes
= node
->GetNodes();
1849 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1851 int len_nodes
= nodes
.GetCount();
1852 int len
= leaves
.GetCount();
1853 int i
= 0, nodes_i
= 0;
1855 for(; i
< len
; i
++ )
1857 void * n
= leaves
[i
];
1858 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1860 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1863 if( Walker( nd
, func
) )
1882 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1884 if (IsVirtualList())
1886 wxDataViewVirtualListModel
*list_model
=
1887 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1888 m_count
= list_model
->GetCount();
1895 wxDataViewTreeNode
* node
;
1896 node
= FindNode(parent
);
1901 node
->SetHasChildren( true );
1903 if( g_model
->IsContainer( item
) )
1905 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1906 newnode
->SetItem(item
);
1907 newnode
->SetHasChildren( true );
1908 node
->AddNode( newnode
);
1911 node
->AddLeaf( item
.GetID() );
1913 node
->ChangeSubTreeCount(1);
1921 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1923 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1924 const wxDataViewItem
& item
)
1926 if (IsVirtualList())
1928 wxDataViewVirtualListModel
*list_model
=
1929 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1930 m_count
= list_model
->GetCount();
1932 if( m_currentRow
> GetRowCount() )
1933 m_currentRow
= m_count
- 1;
1935 // TODO: why empty the entire selection?
1936 m_selection
.Empty();
1943 wxDataViewTreeNode
* node
= FindNode(parent
);
1945 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
1946 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
,
1947 false, "item not found" );
1950 node
->GetChildren().Remove( item
.GetID() );
1951 // Manipolate selection
1952 if( m_selection
.GetCount() > 1 )
1954 m_selection
.Empty();
1956 bool isContainer
= false;
1957 wxDataViewTreeNodes nds
= node
->GetNodes();
1958 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
1960 if (nds
[i
]->GetItem() == item
)
1968 wxDataViewTreeNode
* n
= NULL
;
1969 wxDataViewTreeNodes nodes
= node
->GetNodes();
1970 int len
= nodes
.GetCount();
1971 for( int i
= 0; i
< len
; i
++)
1973 if( nodes
[i
]->GetItem() == item
)
1980 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
1982 node
->GetNodes().Remove( n
);
1983 sub
-= n
->GetSubTreeCount();
1984 ::DestroyTreeHelper(n
);
1986 // Make the row number invalid and get a new valid one when user call GetRowCount
1988 node
->ChangeSubTreeCount(sub
);
1990 // Change the current row to the last row if the current exceed the max row number
1991 if( m_currentRow
> GetRowCount() )
1992 m_currentRow
= m_count
- 1;
1999 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2005 wxWindow
*parent
= GetParent();
2006 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2007 le
.SetEventObject(parent
);
2008 le
.SetModel(GetOwner()->GetModel());
2010 parent
->GetEventHandler()->ProcessEvent(le
);
2015 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2017 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2018 /*#define MAX_VIRTUAL_WIDTH 100000
2020 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2021 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2022 Refresh( true, &rect );
2030 wxWindow
*parent
= GetParent();
2031 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2032 le
.SetEventObject(parent
);
2033 le
.SetModel(GetOwner()->GetModel());
2036 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2037 parent
->GetEventHandler()->ProcessEvent(le
);
2042 bool wxDataViewMainWindow::Cleared()
2047 BuildTree( GetOwner()->GetModel() );
2054 void wxDataViewMainWindow::UpdateDisplay()
2057 m_underMouse
= NULL
;
2060 void wxDataViewMainWindow::OnInternalIdle()
2062 wxWindow::OnInternalIdle();
2066 RecalculateDisplay();
2071 void wxDataViewMainWindow::RecalculateDisplay()
2073 wxDataViewModel
*model
= GetOwner()->GetModel();
2080 int width
= GetEndOfLastCol();
2081 int height
= GetLineStart( GetRowCount() );
2083 SetVirtualSize( width
, height
);
2084 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2089 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2091 wxWindow::ScrollWindow( dx
, dy
, rect
);
2093 if (GetOwner()->m_headerArea
)
2094 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2097 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2100 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2101 int sy
= GetLineStart( rows
)/y
;
2105 wxRect rect
= GetClientRect();
2109 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2110 for (x_start
= 0; colnum
< column
; colnum
++)
2112 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2113 if (col
->IsHidden())
2114 continue; // skip it!
2116 w
= col
->GetWidth();
2120 int x_end
= x_start
+ w
;
2121 xe
= xx
+ rect
.width
;
2124 sx
= ( xx
+ x_end
- xe
)/x
;
2131 m_owner
->Scroll( sx
, sy
);
2134 int wxDataViewMainWindow::GetCountPerPage() const
2136 wxSize size
= GetClientSize();
2137 return size
.y
/ m_lineHeight
;
2140 int wxDataViewMainWindow::GetEndOfLastCol() const
2144 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2146 const wxDataViewColumn
*c
=
2147 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2150 width
+= c
->GetWidth();
2155 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2159 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2161 return GetLineAt( y
);
2164 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2166 wxSize client_size
= GetClientSize();
2167 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2168 &client_size
.x
, &client_size
.y
);
2170 // we should deal with the pixel here
2171 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2173 return wxMin( GetRowCount()-1, row
);
2176 unsigned int wxDataViewMainWindow::GetRowCount()
2178 if ( m_count
== -1 )
2180 m_count
= RecalculateCount();
2186 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2193 void wxDataViewMainWindow::SelectAllRows( bool on
)
2200 m_selection
.Clear();
2201 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2202 m_selection
.Add( i
);
2207 unsigned int first_visible
= GetFirstVisibleRow();
2208 unsigned int last_visible
= GetLastVisibleRow();
2210 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2212 unsigned int row
= m_selection
[i
];
2213 if ((row
>= first_visible
) && (row
<= last_visible
))
2216 m_selection
.Clear();
2220 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2222 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2226 m_selection
.Add( row
);
2234 m_selection
.Remove( row
);
2240 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2244 unsigned int tmp
= from
;
2250 for (i
= from
; i
<= to
; i
++)
2252 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2255 m_selection
.Add( i
);
2260 m_selection
.Remove( i
);
2263 RefreshRows( from
, to
);
2266 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2268 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2270 int n
= aSelections
[i
];
2272 m_selection
.Add( n
);
2277 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2279 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2280 m_selection
.Add( row
);
2282 m_selection
.Remove( row
);
2286 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2288 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2291 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2293 wxWindow
*parent
= GetParent();
2294 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2296 le
.SetEventObject(parent
);
2297 le
.SetModel(GetOwner()->GetModel());
2300 parent
->GetEventHandler()->ProcessEvent(le
);
2303 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2305 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2306 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2308 wxSize client_size
= GetClientSize();
2309 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2310 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2311 if (intersect_rect
.width
> 0)
2312 Refresh( true, &intersect_rect
);
2315 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2319 unsigned int tmp
= to
;
2324 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2325 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2327 wxSize client_size
= GetClientSize();
2328 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2329 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2330 if (intersect_rect
.width
> 0)
2331 Refresh( true, &intersect_rect
);
2334 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2336 wxSize client_size
= GetClientSize();
2337 int start
= GetLineStart( firstRow
);
2338 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2339 if (start
> client_size
.y
) return;
2341 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2343 Refresh( true, &rect
);
2346 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2348 wxCHECK_RET( newCurrent
< GetRowCount(),
2349 wxT("invalid item index in OnArrowChar()") );
2351 // if there is no selection, we cannot move it anywhere
2352 if (!HasCurrentRow())
2355 unsigned int oldCurrent
= m_currentRow
;
2357 // in single selection we just ignore Shift as we can't select several
2359 if ( event
.ShiftDown() && !IsSingleSel() )
2361 RefreshRow( oldCurrent
);
2363 ChangeCurrentRow( newCurrent
);
2365 // select all the items between the old and the new one
2366 if ( oldCurrent
> newCurrent
)
2368 newCurrent
= oldCurrent
;
2369 oldCurrent
= m_currentRow
;
2372 SelectRows( oldCurrent
, newCurrent
, true );
2373 if (oldCurrent
!=newCurrent
)
2374 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2378 RefreshRow( oldCurrent
);
2380 // all previously selected items are unselected unless ctrl is held
2381 if ( !event
.ControlDown() )
2382 SelectAllRows(false);
2384 ChangeCurrentRow( newCurrent
);
2386 if ( !event
.ControlDown() )
2388 SelectRow( m_currentRow
, true );
2389 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2392 RefreshRow( m_currentRow
);
2395 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2398 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2402 rect
.y
= GetLineStart( row
);
2403 rect
.width
= GetEndOfLastCol();
2404 rect
.height
= GetLineHeight( row
);
2409 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2411 const wxDataViewModel
*model
= GetOwner()->GetModel();
2413 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2415 // TODO make more efficient
2420 for (r
= 0; r
< row
; r
++)
2422 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2423 if (!node
) return start
;
2425 wxDataViewItem item
= node
->GetItem();
2427 if (node
&& !node
->HasChildren())
2429 // Yes, if the node does not have any child, it must be a leaf which
2430 // mean that it is a temporarily created by GetTreeNodeByRow
2434 unsigned int cols
= GetOwner()->GetColumnCount();
2436 int height
= m_lineHeight
;
2437 for (col
= 0; col
< cols
; col
++)
2439 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2440 if (column
->IsHidden())
2441 continue; // skip it!
2444 model
->IsContainer(item
) &&
2445 !model
->HasContainerColumns(item
))
2446 continue; // skip it!
2449 model
->GetValue( value
, item
, column
->GetModelColumn() );
2451 wxDataViewRenderer
*renderer
=
2452 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2453 renderer
->SetValue( value
);
2454 height
= wxMax( height
, renderer
->GetSize().y
);
2464 return row
* m_lineHeight
;
2468 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2470 const wxDataViewModel
*model
= GetOwner()->GetModel();
2472 // check for the easy case first
2473 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2474 return y
/ m_lineHeight
;
2476 // TODO make more efficient
2477 unsigned int row
= 0;
2478 unsigned int yy
= 0;
2481 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2484 // not really correct...
2485 return row
+ ((y
-yy
) / m_lineHeight
);
2488 wxDataViewItem item
= node
->GetItem();
2490 if (node
&& !node
->HasChildren())
2492 // Yes, if the node does not have any child, it must be a leaf which
2493 // mean that it is a temporarily created by GetTreeNodeByRow
2497 unsigned int cols
= GetOwner()->GetColumnCount();
2499 int height
= m_lineHeight
;
2500 for (col
= 0; col
< cols
; col
++)
2502 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2503 if (column
->IsHidden())
2504 continue; // skip it!
2507 model
->IsContainer(item
) &&
2508 !model
->HasContainerColumns(item
))
2509 continue; // skip it!
2512 model
->GetValue( value
, item
, column
->GetModelColumn() );
2514 wxDataViewRenderer
*renderer
=
2515 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2516 renderer
->SetValue( value
);
2517 height
= wxMax( height
, renderer
->GetSize().y
);
2528 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2530 const wxDataViewModel
*model
= GetOwner()->GetModel();
2532 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2534 wxASSERT( !IsVirtualList() );
2536 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2537 // wxASSERT( node );
2538 if (!node
) return m_lineHeight
;
2540 wxDataViewItem item
= node
->GetItem();
2542 if (node
&& !node
->HasChildren())
2544 // Yes, if the node does not have any child, it must be a leaf which
2545 // mean that it is a temporarily created by GetTreeNodeByRow
2549 int height
= m_lineHeight
;
2551 unsigned int cols
= GetOwner()->GetColumnCount();
2553 for (col
= 0; col
< cols
; col
++)
2555 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2556 if (column
->IsHidden())
2557 continue; // skip it!
2560 model
->IsContainer(item
) &&
2561 !model
->HasContainerColumns(item
))
2562 continue; // skip it!
2565 model
->GetValue( value
, item
, column
->GetModelColumn() );
2567 wxDataViewRenderer
*renderer
=
2568 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2569 renderer
->SetValue( value
);
2570 height
= wxMax( height
, renderer
->GetSize().y
);
2577 return m_lineHeight
;
2581 class RowToItemJob
: public DoJob
2584 RowToItemJob( unsigned int row
, int current
)
2585 { this->row
= row
; this->current
= current
; }
2586 virtual ~RowToItemJob() {}
2588 virtual int operator() ( wxDataViewTreeNode
* node
)
2591 if( current
== static_cast<int>(row
))
2593 ret
= node
->GetItem();
2597 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2599 current
+= node
->GetSubTreeCount();
2604 // If the current has no child node, we can find the desired item of the row
2606 // This if can speed up finding in some case, and will has a very good effect
2607 // when it comes to list view
2608 if( node
->GetNodes().GetCount() == 0)
2610 int index
= static_cast<int>(row
) - current
- 1;
2611 ret
= node
->GetChildren().Item( index
);
2618 virtual int operator() ( void * n
)
2621 if( current
== static_cast<int>(row
))
2623 ret
= wxDataViewItem( n
);
2629 wxDataViewItem
GetResult() const
2638 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2640 if (IsVirtualList())
2642 return wxDataViewItem( wxUIntToPtr(row
+1) );
2646 RowToItemJob
job( row
, -2 );
2647 Walker( m_root
, job
);
2648 return job
.GetResult();
2652 class RowToTreeNodeJob
: public DoJob
2655 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2658 this->current
= current
;
2662 virtual ~RowToTreeNodeJob(){ }
2664 virtual int operator() ( wxDataViewTreeNode
* node
)
2667 if( current
== static_cast<int>(row
))
2673 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2675 current
+= node
->GetSubTreeCount();
2682 // If the current node has no children, we can find the desired item of the
2683 // row number directly.
2684 // This if can speed up finding in some case, and will have a very good
2685 // effect for list views.
2686 if( node
->GetNodes().GetCount() == 0)
2688 int index
= static_cast<int>(row
) - current
- 1;
2689 void * n
= node
->GetChildren().Item( index
);
2690 ret
= new wxDataViewTreeNode( parent
);
2691 ret
->SetItem( wxDataViewItem( n
));
2692 ret
->SetHasChildren(false);
2699 virtual int operator() ( void * n
)
2702 if( current
== static_cast<int>(row
))
2704 ret
= new wxDataViewTreeNode( parent
);
2705 ret
->SetItem( wxDataViewItem( n
));
2706 ret
->SetHasChildren(false);
2713 wxDataViewTreeNode
* GetResult() const
2719 wxDataViewTreeNode
* ret
;
2720 wxDataViewTreeNode
* parent
;
2723 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2725 wxASSERT( !IsVirtualList() );
2727 RowToTreeNodeJob
job( row
, -2, m_root
);
2728 Walker( m_root
, job
);
2729 return job
.GetResult();
2732 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2733 const wxDataViewItem
& item
)
2735 wxWindow
*parent
= GetParent();
2736 wxDataViewEvent
le(type
, parent
->GetId());
2738 le
.SetEventObject(parent
);
2739 le
.SetModel(GetOwner()->GetModel());
2742 parent
->GetEventHandler()->ProcessEvent(le
);
2746 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2748 if (IsVirtualList())
2751 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2755 if (!node
->HasChildren())
2761 return node
->IsOpen();
2764 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2766 if (IsVirtualList())
2769 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2773 if (!node
->HasChildren())
2782 void wxDataViewMainWindow::Expand( unsigned int row
)
2784 if (IsVirtualList())
2787 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2791 if (!node
->HasChildren())
2797 if (!node
->IsOpen())
2800 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2802 // Check if the user prevent expanding
2803 if( e
.GetSkipped() )
2808 // build the children of current node
2809 if( node
->GetChildrenNumber() == 0 )
2812 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2815 // By expanding the node all row indices that are currently in the selection list
2816 // and are greater than our node have become invalid. So we have to correct that now.
2817 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2818 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2820 const unsigned testRow
= m_selection
[i
];
2821 // all rows above us are not affected, so skip them
2825 m_selection
[i
] += rowAdjustment
;
2828 if(m_currentRow
> row
)
2829 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2833 // Send the expanded event
2834 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2838 void wxDataViewMainWindow::Collapse(unsigned int row
)
2840 if (IsVirtualList())
2843 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2847 if (!node
->HasChildren())
2856 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2857 if( e
.GetSkipped() )
2860 // Find out if there are selected items below the current node.
2861 bool selectCollapsingRow
= false;
2862 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2863 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2864 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2866 const unsigned testRow
= m_selection
[i
];
2867 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2869 selectCollapsingRow
= true;
2870 // get out as soon as we have found a node that is selected
2877 // If the node to be closed has selected items the user won't see those any longer.
2878 // We select the collapsing node in this case.
2879 if(selectCollapsingRow
)
2881 SelectAllRows(false);
2882 ChangeCurrentRow(row
);
2883 SelectRow(row
, true);
2884 SendSelectionChangedEvent(GetItemByRow(row
));
2888 // if there were no selected items below our node we still need to "fix" the
2889 // selection list to adjust for the changing of the row indices.
2890 // We actually do the opposite of what we are doing in Expand().
2891 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2893 const unsigned testRow
= m_selection
[i
];
2894 // all rows above us are not affected, so skip them
2898 m_selection
[i
] -= rowAdjustment
;
2901 // if the "current row" is being collapsed away we change it to the current row ;-)
2902 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2903 ChangeCurrentRow(row
);
2904 else if(m_currentRow
> row
)
2905 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2910 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2914 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2916 wxDataViewModel
* model
= GetOwner()->GetModel();
2923 // Compose the a parent-chain of the finding item
2925 list
.DeleteContents( true );
2926 wxDataViewItem
it( item
);
2929 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2930 list
.Insert( pItem
);
2931 it
= model
->GetParent( it
);
2934 // Find the item along the parent-chain.
2935 // This algorithm is designed to speed up the node-finding method
2936 wxDataViewTreeNode
* node
= m_root
;
2937 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
2939 if( node
->HasChildren() )
2941 if( node
->GetChildrenNumber() == 0 )
2944 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2947 wxDataViewTreeNodes nodes
= node
->GetNodes();
2951 for (i
= 0; i
< nodes
.GetCount(); i
++)
2953 if (nodes
[i
]->GetItem() == (**iter
))
2955 if (nodes
[i
]->GetItem() == item
)
2972 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
2973 wxDataViewColumn
* &column
)
2975 wxDataViewColumn
*col
= NULL
;
2976 unsigned int cols
= GetOwner()->GetColumnCount();
2977 unsigned int colnum
= 0;
2979 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
2980 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
2982 col
= GetOwner()->GetColumnAt(colnum
);
2983 if (col
->IsHidden())
2984 continue; // skip it!
2986 unsigned int w
= col
->GetWidth();
2987 if (x_start
+w
>= (unsigned int)x
)
2994 item
= GetItemByRow( GetLineAt( y
) );
2997 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
2998 const wxDataViewColumn
* column
)
3003 unsigned int cols
= GetOwner()->GetColumnCount();
3004 // If column is null the loop will compute the combined width of all columns.
3005 // Otherwise, it will compute the x position of the column we are looking for.
3006 for (unsigned int i
= 0; i
< cols
; i
++)
3008 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3013 if (col
->IsHidden())
3014 continue; // skip it!
3016 xpos
+= col
->GetWidth();
3017 width
+= col
->GetWidth();
3022 // If we have a column, we need can get its width directly.
3023 if(column
->IsHidden())
3026 width
= column
->GetWidth();
3031 // If we have no column, we reset the x position back to zero.
3035 // we have to take an expander column into account and compute its indentation
3036 // to get the correct x position where the actual text is
3038 int row
= GetRowByItem(item
);
3039 if (!IsVirtualList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3041 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3042 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3043 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3045 if(!node
->HasChildren())
3049 wxRect
itemRect( xpos
+ indent
,
3050 GetLineStart( row
),
3052 GetLineHeight( row
) );
3054 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3055 &itemRect
.x
, &itemRect
.y
);
3060 int wxDataViewMainWindow::RecalculateCount()
3062 if (IsVirtualList())
3064 wxDataViewVirtualListModel
*list_model
=
3065 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3067 return list_model
->GetCount();
3071 return m_root
->GetSubTreeCount();
3075 class ItemToRowJob
: public DoJob
3078 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3085 // Maybe binary search will help to speed up this process
3086 virtual int operator() ( wxDataViewTreeNode
* node
)
3089 if( node
->GetItem() == item
)
3094 if( node
->GetItem() == **m_iter
)
3101 ret
+= node
->GetSubTreeCount();
3107 virtual int operator() ( void * n
)
3110 if( n
== item
.GetID() )
3115 // the row number is begin from zero
3116 int GetResult() const
3120 ItemList::const_iterator m_iter
;
3121 wxDataViewItem item
;
3126 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3128 const wxDataViewModel
* model
= GetOwner()->GetModel();
3132 if (IsVirtualList())
3134 return wxPtrToUInt( item
.GetID() ) -1;
3141 // Compose the a parent-chain of the finding item
3143 wxDataViewItem
* pItem
;
3144 list
.DeleteContents( true );
3145 wxDataViewItem
it( item
);
3148 pItem
= new wxDataViewItem( it
);
3149 list
.Insert( pItem
);
3150 it
= model
->GetParent( it
);
3152 pItem
= new wxDataViewItem( );
3153 list
.Insert( pItem
);
3155 ItemToRowJob
job( item
, list
.begin() );
3156 Walker(m_root
, job
);
3157 return job
.GetResult();
3161 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
3162 wxDataViewTreeNode
* node
)
3164 if( !model
->IsContainer( item
) )
3167 wxDataViewItemArray children
;
3168 unsigned int num
= model
->GetChildren( item
, children
);
3170 unsigned int index
= 0;
3171 while( index
< num
)
3173 if( model
->IsContainer( children
[index
] ) )
3175 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3176 n
->SetItem(children
[index
]);
3177 n
->SetHasChildren( true );
3182 node
->AddLeaf( children
[index
].GetID() );
3186 node
->SetSubTreeCount( num
);
3187 wxDataViewTreeNode
* n
= node
->GetParent();
3189 n
->ChangeSubTreeCount(num
);
3193 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3197 if (GetOwner()->GetModel()->IsVirtualListModel())
3203 m_root
= new wxDataViewTreeNode( NULL
);
3204 m_root
->SetHasChildren(true);
3206 // First we define a invalid item to fetch the top-level elements
3207 wxDataViewItem item
;
3209 BuildTreeHelper( model
, item
, m_root
);
3213 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3215 if( node
->GetNodeNumber() != 0 )
3217 int len
= node
->GetNodeNumber();
3218 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3219 for (int i
= 0; i
< len
; i
++)
3220 DestroyTreeHelper(nodes
[i
]);
3225 void wxDataViewMainWindow::DestroyTree()
3227 if (!IsVirtualList())
3229 ::DestroyTreeHelper(m_root
);
3235 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3237 if ( GetParent()->HandleAsNavigationKey(event
) )
3240 // no item -> nothing to do
3241 if (!HasCurrentRow())
3247 // don't use m_linesPerPage directly as it might not be computed yet
3248 const int pageSize
= GetCountPerPage();
3249 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3251 switch ( event
.GetKeyCode() )
3255 wxWindow
*parent
= GetParent();
3256 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3258 le
.SetItem( GetItemByRow(m_currentRow
) );
3259 le
.SetEventObject(parent
);
3260 le
.SetModel(GetOwner()->GetModel());
3262 parent
->GetEventHandler()->ProcessEvent(le
);
3267 if ( m_currentRow
> 0 )
3268 OnArrowChar( m_currentRow
- 1, event
);
3272 if ( m_currentRow
< GetRowCount() - 1 )
3273 OnArrowChar( m_currentRow
+ 1, event
);
3275 // Add the process for tree expanding/collapsing
3278 if (IsVirtualList())
3281 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3285 if (node
->HasChildren() && node
->IsOpen())
3287 Collapse(m_currentRow
);
3289 else // if the node is already closed we move the selection to its parent
3291 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3293 if(!node
->HasChildren())
3298 int parent
= GetRowByItem( parent_node
->GetItem() );
3301 unsigned int row
= m_currentRow
;
3302 SelectRow( row
, false);
3303 SelectRow( parent
, true );
3304 ChangeCurrentRow( parent
);
3305 GetOwner()->EnsureVisible( parent
, -1 );
3306 SendSelectionChangedEvent( parent_node
->GetItem() );
3314 if (!IsExpanded( m_currentRow
))
3315 Expand( m_currentRow
);
3318 unsigned int row
= m_currentRow
;
3319 SelectRow( row
, false );
3320 SelectRow( row
+ 1, true );
3321 ChangeCurrentRow( row
+ 1 );
3322 GetOwner()->EnsureVisible( row
+ 1, -1 );
3323 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3330 OnArrowChar( GetRowCount() - 1, event
);
3335 OnArrowChar( 0, event
);
3340 int steps
= pageSize
- 1;
3341 int index
= m_currentRow
- steps
;
3345 OnArrowChar( index
, event
);
3351 int steps
= pageSize
- 1;
3352 unsigned int index
= m_currentRow
+ steps
;
3353 unsigned int count
= GetRowCount();
3354 if ( index
>= count
)
3357 OnArrowChar( index
, event
);
3363 if(m_selection
.size() == 1)
3365 // TODO: we need to revise that when we have a concept for a 'current column'
3366 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3376 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3378 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3380 // let the base handle mouse wheel events.
3385 int x
= event
.GetX();
3386 int y
= event
.GetY();
3387 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3388 wxDataViewColumn
*col
= NULL
;
3391 unsigned int cols
= GetOwner()->GetColumnCount();
3393 for (i
= 0; i
< cols
; i
++)
3395 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3397 continue; // skip it!
3399 if (x
< xpos
+ c
->GetWidth())
3404 xpos
+= c
->GetWidth();
3409 wxDataViewRenderer
*cell
= col
->GetRenderer();
3410 unsigned int current
= GetLineAt( y
);
3411 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3413 // Unselect all if below the last row ?
3417 // Test whether the mouse is hovered on the tree item button
3418 bool hoverOverExpander
= false;
3419 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3421 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3422 if( node
!=NULL
&& node
->HasChildren() )
3424 int indent
= node
->GetIndentLevel();
3425 indent
= GetOwner()->GetIndent()*indent
;
3427 // we make the rectangle we are looking in a bit bigger than the actual
3428 // visual expander so the user can hit that little thing reliably
3429 wxRect
rect( xpos
+ indent
,
3430 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3431 m_lineHeight
, m_lineHeight
);
3432 if( rect
.Contains(x
, y
) )
3434 // So the mouse is over the expander
3435 hoverOverExpander
= true;
3436 if (m_underMouse
&& m_underMouse
!= node
)
3438 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3439 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3441 if (m_underMouse
!= node
)
3443 // wxLogMessage("Do the row: %d", current);
3444 RefreshRow(current
);
3446 m_underMouse
= node
;
3449 if (node
!=NULL
&& !node
->HasChildren())
3452 if (!hoverOverExpander
)
3454 if (m_underMouse
!= NULL
)
3456 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3457 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3458 m_underMouse
= NULL
;
3462 wxDataViewModel
*model
= GetOwner()->GetModel();
3464 #if wxUSE_DRAG_AND_DROP
3465 if (event
.Dragging())
3467 if (m_dragCount
== 0)
3469 // we have to report the raw, physical coords as we want to be
3470 // able to call HitTest(event.m_pointDrag) from the user code to
3471 // get the item being dragged
3472 m_dragStart
= event
.GetPosition();
3477 if (m_dragCount
!= 3)
3480 if (event
.LeftIsDown())
3482 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3483 &m_dragStart
.x
, &m_dragStart
.y
);
3484 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3485 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3487 // Notify cell about drag
3488 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3489 event
.SetEventObject( m_owner
);
3490 event
.SetItem( item
);
3491 event
.SetModel( model
);
3492 if (!m_owner
->HandleWindowEvent( event
))
3495 if (!event
.IsAllowed())
3498 wxDataObject
*obj
= event
.GetDataObject();
3502 wxDataViewDropSource
drag( this, drag_item_row
);
3503 drag
.SetData( *obj
);
3504 /* wxDragResult res = */ drag
.DoDragDrop();
3513 #endif // wxUSE_DRAG_AND_DROP
3515 bool simulateClick
= false;
3517 if (event
.ButtonDClick())
3519 m_renameTimer
->Stop();
3520 m_lastOnSame
= false;
3523 wxDataViewItem item
= GetItemByRow(current
);
3524 bool ignore_other_columns
=
3525 ((GetOwner()->GetExpanderColumn() != col
) &&
3526 (model
->IsContainer(item
)) &&
3527 (!model
->HasContainerColumns(item
)));
3529 if (event
.LeftDClick())
3531 if(hoverOverExpander
)
3533 // a double click on the expander will be converted into a "simulated" normal click
3534 simulateClick
= true;
3536 else if ( current
== m_lineLastClicked
)
3538 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3540 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3543 model
->GetValue( value
, item
, col
->GetModelColumn() );
3544 custom
->SetValue( value
);
3545 wxRect
cell_rect( xpos
, GetLineStart( current
),
3546 col
->GetWidth(), GetLineHeight( current
) );
3547 custom
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3552 wxWindow
*parent
= GetParent();
3553 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3555 le
.SetEventObject(parent
);
3556 le
.SetModel(GetOwner()->GetModel());
3558 parent
->GetEventHandler()->ProcessEvent(le
);
3564 // The first click was on another item, so don't interpret this as
3565 // a double click, but as a simple click instead
3566 simulateClick
= true;
3570 if (event
.LeftUp() && !hoverOverExpander
)
3572 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3574 // select single line
3575 SelectAllRows( false );
3576 SelectRow( m_lineSelectSingleOnUp
, true );
3577 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3580 // If the user click the expander, we do not do editing even if the column
3581 // with expander are editable
3582 if (m_lastOnSame
&& !ignore_other_columns
)
3584 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3585 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3587 m_renameTimer
->Start( 100, true );
3591 m_lastOnSame
= false;
3592 m_lineSelectSingleOnUp
= (unsigned int)-1;
3594 else if(!event
.LeftUp())
3596 // This is necessary, because after a DnD operation in
3597 // from and to ourself, the up event is swallowed by the
3598 // DnD code. So on next non-up event (which means here and
3599 // now) m_lineSelectSingleOnUp should be reset.
3600 m_lineSelectSingleOnUp
= (unsigned int)-1;
3603 if (event
.RightDown())
3605 m_lineBeforeLastClicked
= m_lineLastClicked
;
3606 m_lineLastClicked
= current
;
3608 // If the item is already selected, do not update the selection.
3609 // Multi-selections should not be cleared if a selected item is clicked.
3610 if (!IsRowSelected(current
))
3612 SelectAllRows(false);
3613 ChangeCurrentRow(current
);
3614 SelectRow(m_currentRow
,true);
3615 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3618 else if (event
.RightUp())
3621 model
->GetValue( value
, item
, col
->GetModelColumn() );
3622 wxWindow
*parent
= GetParent();
3623 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3625 le
.SetEventObject(parent
);
3626 le
.SetModel(GetOwner()->GetModel());
3628 parent
->GetEventHandler()->ProcessEvent(le
);
3630 else if (event
.MiddleDown())
3634 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3636 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3638 // hoverOverExpander being true tells us that our node must be
3639 // valid and have children.
3640 // So we don't need any extra checks.
3641 if( node
->IsOpen() )
3646 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3650 m_lineBeforeLastClicked
= m_lineLastClicked
;
3651 m_lineLastClicked
= current
;
3653 unsigned int oldCurrentRow
= m_currentRow
;
3654 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3656 bool cmdModifierDown
= event
.CmdDown();
3657 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3659 if ( IsSingleSel() || !IsRowSelected(current
) )
3661 SelectAllRows( false );
3662 ChangeCurrentRow(current
);
3663 SelectRow(m_currentRow
,true);
3664 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3666 else // multi sel & current is highlighted & no mod keys
3668 m_lineSelectSingleOnUp
= current
;
3669 ChangeCurrentRow(current
); // change focus
3672 else // multi sel & either ctrl or shift is down
3674 if (cmdModifierDown
)
3676 ChangeCurrentRow(current
);
3677 ReverseRowSelection(m_currentRow
);
3678 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3680 else if (event
.ShiftDown())
3682 ChangeCurrentRow(current
);
3684 unsigned int lineFrom
= oldCurrentRow
,
3687 if ( lineTo
< lineFrom
)
3690 lineFrom
= m_currentRow
;
3693 SelectRows(lineFrom
, lineTo
, true);
3694 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3696 else // !ctrl, !shift
3698 // test in the enclosing if should make it impossible
3699 wxFAIL_MSG( wxT("how did we get here?") );
3703 if (m_currentRow
!= oldCurrentRow
)
3704 RefreshRow( oldCurrentRow
);
3706 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3708 // Update selection here...
3711 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3712 (current
== oldCurrentRow
)) && oldWasSelected
;
3714 // Call LeftClick after everything else as under GTK+
3715 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3717 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3719 // notify cell about click
3721 model
->GetValue( value
, item
, col
->GetModelColumn() );
3722 custom
->SetValue( value
);
3723 wxRect
cell_rect( xpos
, GetLineStart( current
),
3724 col
->GetWidth(), GetLineHeight( current
) );
3725 /* ignore ret */ custom
->LeftClick( event
.GetPosition(), cell_rect
,
3726 model
, item
, col
->GetModelColumn());
3732 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3736 if (HasCurrentRow())
3742 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3746 if (HasCurrentRow())
3752 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3754 if( m_selection
.GetCount() != 1 )
3755 return wxDataViewItem();
3757 return GetItemByRow( m_selection
.Item(0));
3760 //-----------------------------------------------------------------------------
3762 //-----------------------------------------------------------------------------
3764 WX_DEFINE_LIST(wxDataViewColumnList
)
3766 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3767 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3768 EVT_SIZE(wxDataViewCtrl::OnSize
)
3771 wxDataViewCtrl::~wxDataViewCtrl()
3774 GetModel()->RemoveNotifier( m_notifier
);
3779 void wxDataViewCtrl::Init()
3781 m_cols
.DeleteContents(true);
3784 // No sorting column at start
3785 m_sortingColumnIdx
= wxNOT_FOUND
;
3787 m_headerArea
= NULL
;
3790 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3791 const wxPoint
& pos
, const wxSize
& size
,
3792 long style
, const wxValidator
& validator
)
3794 // if ( (style & wxBORDER_MASK) == 0)
3795 // style |= wxBORDER_SUNKEN;
3799 if (!wxControl::Create( parent
, id
, pos
, size
,
3800 style
| wxScrolledWindowStyle
, validator
))
3803 SetInitialSize(size
);
3806 MacSetClipChildren( true );
3809 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3811 if (HasFlag(wxDV_NO_HEADER
))
3812 m_headerArea
= NULL
;
3814 m_headerArea
= new wxDataViewHeaderWindow(this);
3816 SetTargetWindow( m_clientArea
);
3818 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3820 sizer
->Add( m_headerArea
, 0, wxGROW
);
3821 sizer
->Add( m_clientArea
, 1, wxGROW
);
3827 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3829 return wxBORDER_THEME
;
3833 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3837 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3840 // we need to process arrows ourselves for scrolling
3841 if ( nMsg
== WM_GETDLGCODE
)
3843 rc
|= DLGC_WANTARROWS
;
3851 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3853 wxSize newsize
= size
;
3854 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3855 newsize
.y
-= m_headerArea
->GetSize().y
;
3860 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3862 // We need to override OnSize so that our scrolled
3863 // window a) does call Layout() to use sizers for
3864 // positioning the controls but b) does not query
3865 // the sizer for their size and use that for setting
3866 // the scrollable area as set that ourselves by
3867 // calling SetScrollbar() further down.
3874 void wxDataViewCtrl::SetFocus()
3877 m_clientArea
->SetFocus();
3880 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3882 if (!wxDataViewCtrlBase::AssociateModel( model
))
3885 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3887 model
->AddNotifier( m_notifier
);
3889 m_clientArea
->DestroyTree();
3891 m_clientArea
->BuildTree(model
);
3893 m_clientArea
->UpdateDisplay();
3898 #if wxUSE_DRAG_AND_DROP
3900 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
3902 return m_clientArea
->EnableDragSource( format
);
3905 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
3907 return m_clientArea
->EnableDropTarget( format
);
3910 #endif // wxUSE_DRAG_AND_DROP
3912 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3914 if (!wxDataViewCtrlBase::AppendColumn(col
))
3917 m_cols
.Append( col
);
3918 OnColumnsCountChanged();
3922 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3924 if (!wxDataViewCtrlBase::PrependColumn(col
))
3927 m_cols
.Insert( col
);
3928 OnColumnsCountChanged();
3932 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3934 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
3937 m_cols
.Insert( pos
, col
);
3938 OnColumnsCountChanged();
3942 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
3945 m_headerArea
->UpdateColumn(idx
);
3947 m_clientArea
->UpdateDisplay();
3950 void wxDataViewCtrl::OnColumnsCountChanged()
3953 m_headerArea
->SetColumnCount(GetColumnCount());
3955 m_clientArea
->UpdateDisplay();
3958 void wxDataViewCtrl::DoSetExpanderColumn()
3960 m_clientArea
->UpdateDisplay();
3963 void wxDataViewCtrl::DoSetIndent()
3965 m_clientArea
->UpdateDisplay();
3968 unsigned int wxDataViewCtrl::GetColumnCount() const
3970 return m_cols
.GetCount();
3973 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
3978 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
3980 // columns can't be reordered if there is no header window which allows
3982 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
3985 return GetColumn(idx
);
3988 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
3990 const unsigned count
= m_cols
.size();
3991 for ( unsigned n
= 0; n
< count
; n
++ )
3993 if ( m_cols
[n
] == column
)
4000 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4001 unsigned int WXUNUSED(new_pos
))
4003 // do _not_ reorder m_cols elements here, they should always be in the
4004 // order in which columns were added, we only display the columns in
4006 m_clientArea
->UpdateDisplay();
4009 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4011 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4016 OnColumnsCountChanged();
4021 bool wxDataViewCtrl::ClearColumns()
4024 OnColumnsCountChanged();
4028 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4032 unsigned int len
= GetColumnCount();
4033 for ( unsigned int i
= 0; i
< len
; i
++ )
4035 wxDataViewColumn
* col
= GetColumnAt(i
);
4036 if (col
->IsHidden())
4038 ret
+= col
->GetWidth();
4041 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4048 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4050 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4051 : GetColumn(m_sortingColumnIdx
);
4054 // Selection code with wxDataViewItem as parameters
4055 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4057 return m_clientArea
->GetSelection();
4060 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4063 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4064 int len
= selection
.GetCount();
4065 for( int i
= 0; i
< len
; i
++)
4067 unsigned int row
= selection
[i
];
4068 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4073 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4075 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4077 wxDataViewItem last_parent
;
4079 int len
= sel
.GetCount();
4080 for( int i
= 0; i
< len
; i
++ )
4082 wxDataViewItem item
= sel
[i
];
4083 wxDataViewItem parent
= GetModel()->GetParent( item
);
4086 if (parent
!= last_parent
)
4087 ExpandAncestors(item
);
4090 last_parent
= parent
;
4091 int row
= m_clientArea
->GetRowByItem( item
);
4093 selection
.Add( static_cast<unsigned int>(row
) );
4096 m_clientArea
->SetSelections( selection
);
4099 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4101 ExpandAncestors( item
);
4103 int row
= m_clientArea
->GetRowByItem( item
);
4106 // Unselect all rows before select another in the single select mode
4107 if (m_clientArea
->IsSingleSel())
4108 m_clientArea
->SelectAllRows(false);
4110 m_clientArea
->SelectRow(row
, true);
4113 if (m_clientArea
->IsSingleSel())
4114 m_clientArea
->ChangeCurrentRow( row
);
4118 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4120 int row
= m_clientArea
->GetRowByItem( item
);
4122 m_clientArea
->SelectRow(row
, false);
4125 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4127 int row
= m_clientArea
->GetRowByItem( item
);
4130 return m_clientArea
->IsRowSelected(row
);
4135 // Selection code with row number as parameter
4136 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4139 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4140 int len
= selection
.GetCount();
4141 for( int i
= 0; i
< len
; i
++)
4143 unsigned int row
= selection
[i
];
4149 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4151 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4152 int len
= sel
.GetCount();
4153 for( int i
= 0; i
< len
; i
++ )
4157 selection
.Add( static_cast<unsigned int>(row
) );
4159 m_clientArea
->SetSelections( selection
);
4162 void wxDataViewCtrl::Select( int row
)
4166 if (m_clientArea
->IsSingleSel())
4167 m_clientArea
->SelectAllRows(false);
4168 m_clientArea
->SelectRow( row
, true );
4172 void wxDataViewCtrl::Unselect( int row
)
4175 m_clientArea
->SelectRow(row
, false);
4178 bool wxDataViewCtrl::IsSelected( int row
) const
4181 return m_clientArea
->IsRowSelected(row
);
4185 void wxDataViewCtrl::SelectRange( int from
, int to
)
4188 for( int i
= from
; i
< to
; i
++ )
4190 m_clientArea
->Select(sel
);
4193 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4195 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4196 for( int i
= from
; i
< to
; i
++ )
4197 if( sel
.Index( i
) != wxNOT_FOUND
)
4199 m_clientArea
->SetSelections(sel
);
4202 void wxDataViewCtrl::SelectAll()
4204 m_clientArea
->SelectAllRows(true);
4207 void wxDataViewCtrl::UnselectAll()
4209 m_clientArea
->SelectAllRows(false);
4212 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4216 if( row
> (int) m_clientArea
->GetRowCount() )
4217 row
= m_clientArea
->GetRowCount();
4219 int first
= m_clientArea
->GetFirstVisibleRow();
4220 int last
= m_clientArea
->GetLastVisibleRow();
4222 m_clientArea
->ScrollTo( row
, column
);
4223 else if( row
> last
)
4224 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4226 m_clientArea
->ScrollTo( first
, column
);
4229 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4231 ExpandAncestors( item
);
4233 m_clientArea
->RecalculateDisplay();
4235 int row
= m_clientArea
->GetRowByItem(item
);
4238 if( column
== NULL
)
4239 EnsureVisible(row
, -1);
4241 EnsureVisible( row
, GetColumnIndex(column
) );
4246 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4247 wxDataViewColumn
* &column
) const
4249 m_clientArea
->HitTest(point
, item
, column
);
4252 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4253 const wxDataViewColumn
* column
) const
4255 return m_clientArea
->GetItemRect(item
, column
);
4258 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4260 return m_clientArea
->GetItemByRow( row
);
4263 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4265 return m_clientArea
->GetRowByItem( item
);
4268 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4270 int row
= m_clientArea
->GetRowByItem( item
);
4272 m_clientArea
->Expand(row
);
4275 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4277 int row
= m_clientArea
->GetRowByItem( item
);
4279 m_clientArea
->Collapse(row
);
4282 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4284 int row
= m_clientArea
->GetRowByItem( item
);
4286 return m_clientArea
->IsExpanded(row
);
4290 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4292 wxDataViewColumn
* col
= GetColumn( column
);
4296 wxRect itemRect
= GetItemRect(item
, col
);
4297 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4298 renderer
->StartEditing(item
, itemRect
);
4301 #endif // !wxUSE_GENERICDATAVIEWCTRL
4303 #endif // wxUSE_DATAVIEWCTRL