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 // check boxes we draw must always have the same, standard size (if it's
804 // bigger than the cell size the checkbox will be truncated because the
805 // caller had set the clipping rectangle to prevent us from drawing outside
807 cell
.SetSize(GetSize());
809 wxRendererNative::Get().DrawCheckBox(
810 GetOwner()->GetOwner(),
818 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
819 wxDataViewModel
*model
,
820 const wxDataViewItem
& item
, unsigned int col
)
822 model
->ChangeValue(!m_toggle
, item
, col
);
826 wxSize
wxDataViewToggleRenderer::GetSize() const
828 // the window parameter is not used by GetCheckBoxSize() so it's
830 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
833 // ---------------------------------------------------------
834 // wxDataViewProgressRenderer
835 // ---------------------------------------------------------
837 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
839 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
840 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
841 wxDataViewRenderer( varianttype
, mode
, align
)
847 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
849 m_value
= (long) value
;
851 if (m_value
< 0) m_value
= 0;
852 if (m_value
> 100) m_value
= 100;
857 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
859 value
= (long) m_value
;
864 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
866 // deflate the rect to leave a small border between bars in adjacent rows
867 wxRect bar
= rect
.Deflate(0, 1);
869 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
870 dc
->SetPen( *wxBLACK_PEN
);
871 dc
->DrawRectangle( bar
);
873 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
874 dc
->SetPen( *wxTRANSPARENT_PEN
);
876 const wxDataViewItemAttr
& attr
= GetAttr();
877 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
879 dc
->DrawRectangle( bar
);
884 wxSize
wxDataViewProgressRenderer::GetSize() const
886 return wxSize(40,12);
889 // ---------------------------------------------------------
890 // wxDataViewDateRenderer
891 // ---------------------------------------------------------
893 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
895 #if wxUSE_DATE_RENDERER_POPUP
897 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
900 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
901 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
902 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
907 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
908 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
909 sizer
->Add( m_cal
, 1, wxGROW
);
914 void OnCalendar( wxCalendarEvent
&event
);
916 wxCalendarCtrl
*m_cal
;
917 wxDataViewModel
*m_model
;
919 const wxDataViewItem
& m_item
;
922 virtual void OnDismiss()
927 DECLARE_EVENT_TABLE()
930 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
931 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
934 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
936 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
940 #endif // wxUSE_DATE_RENDERER_POPUP
942 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
944 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
945 wxDataViewCellMode mode
, int align
) :
946 wxDataViewRenderer( varianttype
, mode
, align
)
950 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
952 m_date
= value
.GetDateTime();
957 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
963 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
965 wxString tmp
= m_date
.FormatDate();
966 RenderText( tmp
, 0, cell
, dc
, state
);
970 wxSize
wxDataViewDateRenderer::GetSize() const
972 const wxDataViewCtrl
* view
= GetView();
973 wxString tmp
= m_date
.FormatDate();
975 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
976 return wxSize(x
,y
+d
);
979 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
980 const wxDataViewItem
& item
, unsigned int col
)
983 model
->GetValue( variant
, item
, col
);
984 wxDateTime value
= variant
.GetDateTime();
986 #if wxUSE_DATE_RENDERER_POPUP
987 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
988 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
989 wxPoint pos
= wxGetMousePosition();
992 popup
->Popup( popup
->m_cal
);
993 #else // !wxUSE_DATE_RENDERER_POPUP
994 wxMessageBox(value
.Format());
995 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
999 // ---------------------------------------------------------
1000 // wxDataViewIconTextRenderer
1001 // ---------------------------------------------------------
1003 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1005 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1006 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1007 wxDataViewRenderer( varianttype
, mode
, align
)
1010 SetAlignment(align
);
1013 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1019 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1024 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1028 const wxIcon
& icon
= m_value
.GetIcon();
1031 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1032 xoffset
= icon
.GetWidth()+4;
1035 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1040 wxSize
wxDataViewIconTextRenderer::GetSize() const
1042 const wxDataViewCtrl
*view
= GetView();
1043 if (!m_value
.GetText().empty())
1046 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1048 if (m_value
.GetIcon().IsOk())
1049 x
+= m_value
.GetIcon().GetWidth() + 4;
1050 return wxSize( x
, y
);
1052 return wxSize(80,20);
1055 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1057 wxDataViewIconText iconText
;
1060 wxString text
= iconText
.GetText();
1062 // adjust the label rect to take the width of the icon into account
1063 if (iconText
.GetIcon().IsOk())
1065 int w
= iconText
.GetIcon().GetWidth() + 4;
1067 labelRect
.width
-= w
;
1070 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1071 wxPoint(labelRect
.x
,labelRect
.y
),
1072 wxSize(labelRect
.width
,labelRect
.height
) );
1074 // select the text in the control an place the cursor at the end
1075 ctrl
->SetInsertionPointEnd();
1081 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
& value
)
1083 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1085 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1090 //-----------------------------------------------------------------------------
1091 // wxDataViewDropTarget
1092 //-----------------------------------------------------------------------------
1094 #if wxUSE_DRAG_AND_DROP
1096 class wxBitmapCanvas
: public wxWindow
1099 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1100 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1103 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1106 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1109 dc
.DrawBitmap( m_bitmap
, 0, 0);
1115 class wxDataViewDropSource
: public wxDropSource
1118 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1126 ~wxDataViewDropSource()
1131 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1133 wxPoint pos
= wxGetMousePosition();
1137 int liney
= m_win
->GetLineStart( m_row
);
1139 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1140 m_win
->ClientToScreen( &linex
, &liney
);
1141 m_dist_x
= pos
.x
- linex
;
1142 m_dist_y
= pos
.y
- liney
;
1145 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1147 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1148 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1150 wxFRAME_TOOL_WINDOW
|
1151 wxFRAME_FLOAT_ON_PARENT
|
1152 wxFRAME_NO_TASKBAR
|
1154 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1159 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1160 m_hint
->SetTransparent( 128 );
1166 wxDataViewMainWindow
*m_win
;
1169 int m_dist_x
,m_dist_y
;
1173 class wxDataViewDropTarget
: public wxDropTarget
1176 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1182 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1184 wxDataFormat format
= GetMatchingPair();
1185 if (format
== wxDF_INVALID
)
1187 return m_win
->OnDragOver( format
, x
, y
, def
);
1190 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1192 wxDataFormat format
= GetMatchingPair();
1193 if (format
== wxDF_INVALID
)
1195 return m_win
->OnDrop( format
, x
, y
);
1198 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1200 wxDataFormat format
= GetMatchingPair();
1201 if (format
== wxDF_INVALID
)
1205 return m_win
->OnData( format
, x
, y
, def
);
1208 virtual void OnLeave()
1209 { m_win
->OnLeave(); }
1211 wxDataViewMainWindow
*m_win
;
1214 #endif // wxUSE_DRAG_AND_DROP
1216 //-----------------------------------------------------------------------------
1217 // wxDataViewRenameTimer
1218 //-----------------------------------------------------------------------------
1220 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1225 void wxDataViewRenameTimer::Notify()
1227 m_owner
->OnRenameTimer();
1230 //-----------------------------------------------------------------------------
1231 // wxDataViewMainWindow
1232 //-----------------------------------------------------------------------------
1234 // The tree building helper, declared firstly
1235 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
1236 wxDataViewTreeNode
* node
);
1238 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1240 if (row1
> row2
) return 1;
1241 if (row1
== row2
) return 0;
1245 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1247 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1248 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1249 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1250 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1251 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1252 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1255 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1256 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1257 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1258 m_selection( wxDataViewSelectionCmp
)
1263 m_lastOnSame
= false;
1264 m_renameTimer
= new wxDataViewRenameTimer( this );
1266 // TODO: user better initial values/nothing selected
1267 m_currentCol
= NULL
;
1270 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1272 #if wxUSE_DRAG_AND_DROP
1274 m_dragStart
= wxPoint(0,0);
1276 m_dragEnabled
= false;
1277 m_dropEnabled
= false;
1279 m_dropHintLine
= (unsigned int) -1;
1280 #endif // wxUSE_DRAG_AND_DROP
1282 m_lineLastClicked
= (unsigned int) -1;
1283 m_lineBeforeLastClicked
= (unsigned int) -1;
1284 m_lineSelectSingleOnUp
= (unsigned int) -1;
1288 SetBackgroundColour( *wxWHITE
);
1290 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1292 m_penRule
= wxPen(GetRuleColour());
1294 // compose a pen whichcan draw black lines
1295 // TODO: maybe there is something system colour to use
1296 m_penExpander
= wxPen(wxColour(0,0,0));
1298 m_root
= new wxDataViewTreeNode( NULL
);
1299 m_root
->SetHasChildren(true);
1301 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1303 m_underMouse
= NULL
;
1307 wxDataViewMainWindow::~wxDataViewMainWindow()
1310 delete m_renameTimer
;
1314 #if wxUSE_DRAG_AND_DROP
1315 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1317 m_dragFormat
= format
;
1318 m_dragEnabled
= format
!= wxDF_INVALID
;
1323 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1325 m_dropFormat
= format
;
1326 m_dropEnabled
= format
!= wxDF_INVALID
;
1329 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1334 void wxDataViewMainWindow::RemoveDropHint()
1339 RefreshRow( m_dropHintLine
);
1340 m_dropHintLine
= (unsigned int) -1;
1344 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1345 wxCoord y
, wxDragResult def
)
1349 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1350 unsigned int row
= GetLineAt( yy
);
1352 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1358 wxDataViewItem item
= GetItemByRow( row
);
1360 wxDataViewModel
*model
= GetOwner()->GetModel();
1362 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1363 event
.SetEventObject( m_owner
);
1364 event
.SetItem( item
);
1365 event
.SetModel( model
);
1366 event
.SetDataFormat( format
);
1367 if (!m_owner
->HandleWindowEvent( event
))
1373 if (!event
.IsAllowed())
1380 if (m_dropHint
&& (row
!= m_dropHintLine
))
1381 RefreshRow( m_dropHintLine
);
1383 m_dropHintLine
= row
;
1389 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1395 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1396 unsigned int row
= GetLineAt( yy
);
1398 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1401 wxDataViewItem item
= GetItemByRow( row
);
1403 wxDataViewModel
*model
= GetOwner()->GetModel();
1405 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1406 event
.SetEventObject( m_owner
);
1407 event
.SetItem( item
);
1408 event
.SetModel( model
);
1409 event
.SetDataFormat( format
);
1410 if (!m_owner
->HandleWindowEvent( event
))
1413 if (!event
.IsAllowed())
1419 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1424 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1425 unsigned int row
= GetLineAt( yy
);
1427 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1430 wxDataViewItem item
= GetItemByRow( row
);
1432 wxDataViewModel
*model
= GetOwner()->GetModel();
1434 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1436 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1437 event
.SetEventObject( m_owner
);
1438 event
.SetItem( item
);
1439 event
.SetModel( model
);
1440 event
.SetDataFormat( format
);
1441 event
.SetDataSize( obj
->GetSize() );
1442 event
.SetDataBuffer( obj
->GetData() );
1443 if (!m_owner
->HandleWindowEvent( event
))
1446 if (!event
.IsAllowed())
1452 void wxDataViewMainWindow::OnLeave()
1457 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1459 int height
= GetLineHeight( row
);
1461 unsigned int cols
= GetOwner()->GetColumnCount();
1463 for (col
= 0; col
< cols
; col
++)
1465 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1466 if (column
->IsHidden())
1467 continue; // skip it!
1468 width
+= column
->GetWidth();
1472 if (!IsVirtualList())
1474 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1475 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1476 indent
= indent
+ m_lineHeight
;
1477 // try to use the m_lineHeight as the expander space
1479 if(!node
->HasChildren())
1484 wxBitmap
bitmap( width
, height
);
1485 wxMemoryDC
dc( bitmap
);
1486 dc
.SetFont( GetFont() );
1487 dc
.SetPen( *wxBLACK_PEN
);
1488 dc
.SetBrush( *wxWHITE_BRUSH
);
1489 dc
.DrawRectangle( 0,0,width
,height
);
1491 wxDataViewModel
*model
= m_owner
->GetModel();
1493 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1496 // TODO-RTL: last column for RTL support
1497 expander
= GetOwner()->GetColumnAt( 0 );
1498 GetOwner()->SetExpanderColumn(expander
);
1503 for (col
= 0; col
< cols
; col
++)
1505 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1506 wxDataViewRenderer
*cell
= column
->GetRenderer();
1508 if (column
->IsHidden())
1509 continue; // skip it!
1511 width
= column
->GetWidth();
1513 if (column
== expander
)
1517 wxDataViewItem item
= GetItemByRow( row
);
1518 model
->GetValue( value
, item
, column
->GetModelColumn());
1519 cell
->SetValue( value
);
1521 wxDataViewItemAttr attr
;
1522 model
->GetAttr(item
, column
->GetModelColumn(), attr
);
1523 cell
->SetAttr(attr
);
1525 wxRect
item_rect(x
, 0, width
, height
);
1526 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1528 // dc.SetClippingRegion( item_rect );
1529 cell
->WXCallRender(item_rect
, &dc
, 0);
1530 // dc.DestroyClippingRegion();
1538 #endif // wxUSE_DRAG_AND_DROP
1541 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1543 wxDataViewModel
*model
= GetOwner()->GetModel();
1544 wxAutoBufferedPaintDC
dc( this );
1547 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1548 dc
.SetPen( *wxTRANSPARENT_PEN
);
1549 dc
.DrawRectangle(GetClientSize());
1553 GetOwner()->PrepareDC( dc
);
1554 dc
.SetFont( GetFont() );
1556 wxRect update
= GetUpdateRegion().GetBox();
1557 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1559 // compute which items needs to be redrawn
1560 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1561 unsigned int item_count
=
1562 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1563 (int)(GetRowCount( ) - item_start
));
1564 unsigned int item_last
= item_start
+ item_count
;
1565 // Get the parent of DataViewCtrl
1566 wxWindow
*parent
= GetParent()->GetParent();
1567 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1568 cache_event
.SetEventObject(GetParent());
1569 cache_event
.SetCache(item_start
, item_last
- 1);
1570 parent
->ProcessWindowEvent(cache_event
);
1572 // compute which columns needs to be redrawn
1573 unsigned int cols
= GetOwner()->GetColumnCount();
1576 // we assume that we have at least one column below and painting an
1577 // empty control is unnecessary anyhow
1581 unsigned int col_start
= 0;
1582 unsigned int x_start
;
1583 for (x_start
= 0; col_start
< cols
; col_start
++)
1585 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1586 if (col
->IsHidden())
1587 continue; // skip it!
1589 unsigned int w
= col
->GetWidth();
1590 if (x_start
+w
>= (unsigned int)update
.x
)
1596 unsigned int col_last
= col_start
;
1597 unsigned int x_last
= x_start
;
1598 for (; col_last
< cols
; col_last
++)
1600 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1601 if (col
->IsHidden())
1602 continue; // skip it!
1604 if (x_last
> (unsigned int)update
.GetRight())
1607 x_last
+= col
->GetWidth();
1610 // Draw horizontal rules if required
1611 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1613 dc
.SetPen(m_penRule
);
1614 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1616 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1618 int y
= GetLineStart( i
);
1619 dc
.DrawLine(x_start
, y
, x_last
, y
);
1623 // Draw vertical rules if required
1624 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1626 dc
.SetPen(m_penRule
);
1627 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1630 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1632 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1633 if (col
->IsHidden())
1634 continue; // skip it
1636 dc
.DrawLine(x
, GetLineStart( item_start
),
1637 x
, GetLineStart( item_last
) );
1639 x
+= col
->GetWidth();
1642 // Draw last vertical rule
1643 dc
.DrawLine(x
, GetLineStart( item_start
),
1644 x
, GetLineStart( item_last
) );
1647 // redraw the background for the items which are selected/current
1648 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1650 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1651 if (selected
|| item
== m_currentRow
)
1653 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1654 if (item
== m_currentRow
)
1655 flags
|= wxCONTROL_CURRENT
;
1657 flags
|= wxCONTROL_FOCUSED
;
1659 wxRect
rect( x_start
, GetLineStart( item
),
1660 x_last
- x_start
, GetLineHeight( item
) );
1661 wxRendererNative::Get().DrawItemSelectionRect
1671 #if wxUSE_DRAG_AND_DROP
1674 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1675 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1676 dc
.SetPen( *wxBLACK_PEN
);
1677 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1678 dc
.DrawRectangle( rect
);
1680 #endif // wxUSE_DRAG_AND_DROP
1682 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1685 // TODO-RTL: last column for RTL support
1686 expander
= GetOwner()->GetColumnAt( 0 );
1687 GetOwner()->SetExpanderColumn(expander
);
1690 // redraw all cells for all rows which must be repainted and all columns
1692 cell_rect
.x
= x_start
;
1693 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1695 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1696 wxDataViewRenderer
*cell
= col
->GetRenderer();
1697 cell_rect
.width
= col
->GetWidth();
1699 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1700 continue; // skip it!
1702 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1704 // get the cell value and set it into the renderer
1706 wxDataViewTreeNode
*node
= NULL
;
1707 wxDataViewItem dataitem
;
1709 if (!IsVirtualList())
1711 node
= GetTreeNodeByRow(item
);
1715 dataitem
= node
->GetItem();
1717 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1718 !model
->HasContainerColumns(dataitem
))
1723 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1726 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1727 cell
->SetValue( value
);
1729 wxDataViewItemAttr attr
;
1730 model
->GetAttr(dataitem
, col
->GetModelColumn(), attr
);
1731 cell
->SetAttr(attr
);
1734 cell_rect
.y
= GetLineStart( item
);
1735 cell_rect
.height
= GetLineHeight( item
);
1737 // deal with the expander
1739 if ((!IsVirtualList()) && (col
== expander
))
1741 // Calculate the indent first
1742 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1744 // we reserve m_lineHeight of horizontal space for the expander
1745 // but leave EXPANDER_MARGIN around the expander itself
1746 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1748 indent
+= m_lineHeight
;
1750 // draw expander if needed and visible
1751 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1753 dc
.SetPen( m_penExpander
);
1754 dc
.SetBrush( wxNullBrush
);
1756 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1757 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1758 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1760 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1763 if ( m_underMouse
== node
)
1764 flag
|= wxCONTROL_CURRENT
;
1765 if ( node
->IsOpen() )
1766 flag
|= wxCONTROL_EXPANDED
;
1768 // ensure that we don't overflow the cell (which might
1769 // happen if the column is very narrow)
1770 wxDCClipper
clip(dc
, cell_rect
);
1772 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1775 // force the expander column to left-center align
1776 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1778 if (node
&& !node
->HasChildren())
1780 // Yes, if the node does not have any child, it must be a leaf which
1781 // mean that it is a temporarily created by GetTreeNodeByRow
1785 wxRect item_rect
= cell_rect
;
1786 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1788 // account for the tree indent (harmless if we're not indented)
1789 item_rect
.x
+= indent
;
1790 item_rect
.width
-= indent
;
1792 if ( item_rect
.width
<= 0 )
1796 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1797 state
|= wxDATAVIEW_CELL_SELECTED
;
1799 // TODO: it would be much more efficient to create a clipping
1800 // region for the entire column being rendered (in the OnPaint
1801 // of wxDataViewMainWindow) instead of a single clip region for
1802 // each cell. However it would mean that each renderer should
1803 // respect the given wxRect's top & bottom coords, eventually
1804 // violating only the left & right coords - however the user can
1805 // make its own renderer and thus we cannot be sure of that.
1806 wxDCClipper
clip(dc
, item_rect
);
1808 cell
->WXCallRender(item_rect
, &dc
, state
);
1811 cell_rect
.x
+= cell_rect
.width
;
1815 void wxDataViewMainWindow::OnRenameTimer()
1817 // We have to call this here because changes may just have
1818 // been made and no screen update taken place.
1821 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1822 // (needs to be tested!)
1826 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1828 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1830 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1833 //-----------------------------------------------------------------------------
1834 // Helper class for do operation on the tree node
1835 //-----------------------------------------------------------------------------
1840 virtual ~DoJob() { }
1842 // The return value control how the tree-walker tranverse the tree
1843 // 0: Job done, stop tranverse and return
1844 // 1: Ignore the current node's subtree and continue
1845 // 2: Job not done, continue
1846 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1847 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1848 virtual int operator() ( void * n
) = 0;
1851 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1856 switch( func( node
) )
1867 wxDataViewTreeNodes nodes
= node
->GetNodes();
1868 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1870 int len_nodes
= nodes
.GetCount();
1871 int len
= leaves
.GetCount();
1872 int i
= 0, nodes_i
= 0;
1874 for(; i
< len
; i
++ )
1876 void * n
= leaves
[i
];
1877 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1879 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1882 if( Walker( nd
, func
) )
1901 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1903 if (IsVirtualList())
1905 wxDataViewVirtualListModel
*list_model
=
1906 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1907 m_count
= list_model
->GetCount();
1914 wxDataViewTreeNode
* node
;
1915 node
= FindNode(parent
);
1920 node
->SetHasChildren( true );
1922 if( g_model
->IsContainer( item
) )
1924 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1925 newnode
->SetItem(item
);
1926 newnode
->SetHasChildren( true );
1927 node
->AddNode( newnode
);
1930 node
->AddLeaf( item
.GetID() );
1932 node
->ChangeSubTreeCount(1);
1940 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1942 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1943 const wxDataViewItem
& item
)
1945 if (IsVirtualList())
1947 wxDataViewVirtualListModel
*list_model
=
1948 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1949 m_count
= list_model
->GetCount();
1951 if( m_currentRow
> GetRowCount() )
1952 m_currentRow
= m_count
- 1;
1954 // TODO: why empty the entire selection?
1955 m_selection
.Empty();
1962 wxDataViewTreeNode
* node
= FindNode(parent
);
1964 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
1965 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
,
1966 false, "item not found" );
1969 node
->GetChildren().Remove( item
.GetID() );
1970 // Manipolate selection
1971 if( m_selection
.GetCount() > 1 )
1973 m_selection
.Empty();
1975 bool isContainer
= false;
1976 wxDataViewTreeNodes nds
= node
->GetNodes();
1977 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
1979 if (nds
[i
]->GetItem() == item
)
1987 wxDataViewTreeNode
* n
= NULL
;
1988 wxDataViewTreeNodes nodes
= node
->GetNodes();
1989 int len
= nodes
.GetCount();
1990 for( int i
= 0; i
< len
; i
++)
1992 if( nodes
[i
]->GetItem() == item
)
1999 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2001 node
->GetNodes().Remove( n
);
2002 sub
-= n
->GetSubTreeCount();
2003 ::DestroyTreeHelper(n
);
2005 // Make the row number invalid and get a new valid one when user call GetRowCount
2007 node
->ChangeSubTreeCount(sub
);
2009 // Change the current row to the last row if the current exceed the max row number
2010 if( m_currentRow
> GetRowCount() )
2011 m_currentRow
= m_count
- 1;
2018 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2024 wxWindow
*parent
= GetParent();
2025 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2026 le
.SetEventObject(parent
);
2027 le
.SetModel(GetOwner()->GetModel());
2029 parent
->GetEventHandler()->ProcessEvent(le
);
2034 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2036 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2037 /*#define MAX_VIRTUAL_WIDTH 100000
2039 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2040 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2041 Refresh( true, &rect );
2049 wxWindow
*parent
= GetParent();
2050 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2051 le
.SetEventObject(parent
);
2052 le
.SetModel(GetOwner()->GetModel());
2055 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2056 parent
->GetEventHandler()->ProcessEvent(le
);
2061 bool wxDataViewMainWindow::Cleared()
2066 BuildTree( GetOwner()->GetModel() );
2073 void wxDataViewMainWindow::UpdateDisplay()
2076 m_underMouse
= NULL
;
2079 void wxDataViewMainWindow::OnInternalIdle()
2081 wxWindow::OnInternalIdle();
2085 RecalculateDisplay();
2090 void wxDataViewMainWindow::RecalculateDisplay()
2092 wxDataViewModel
*model
= GetOwner()->GetModel();
2099 int width
= GetEndOfLastCol();
2100 int height
= GetLineStart( GetRowCount() );
2102 SetVirtualSize( width
, height
);
2103 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2108 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2110 wxWindow::ScrollWindow( dx
, dy
, rect
);
2112 if (GetOwner()->m_headerArea
)
2113 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2116 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2119 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2120 int sy
= GetLineStart( rows
)/y
;
2124 wxRect rect
= GetClientRect();
2128 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2129 for (x_start
= 0; colnum
< column
; colnum
++)
2131 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2132 if (col
->IsHidden())
2133 continue; // skip it!
2135 w
= col
->GetWidth();
2139 int x_end
= x_start
+ w
;
2140 xe
= xx
+ rect
.width
;
2143 sx
= ( xx
+ x_end
- xe
)/x
;
2150 m_owner
->Scroll( sx
, sy
);
2153 int wxDataViewMainWindow::GetCountPerPage() const
2155 wxSize size
= GetClientSize();
2156 return size
.y
/ m_lineHeight
;
2159 int wxDataViewMainWindow::GetEndOfLastCol() const
2163 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2165 const wxDataViewColumn
*c
=
2166 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2169 width
+= c
->GetWidth();
2174 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2178 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2180 return GetLineAt( y
);
2183 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2185 wxSize client_size
= GetClientSize();
2186 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2187 &client_size
.x
, &client_size
.y
);
2189 // we should deal with the pixel here
2190 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2192 return wxMin( GetRowCount()-1, row
);
2195 unsigned int wxDataViewMainWindow::GetRowCount()
2197 if ( m_count
== -1 )
2199 m_count
= RecalculateCount();
2205 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2212 void wxDataViewMainWindow::SelectAllRows( bool on
)
2219 m_selection
.Clear();
2220 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2221 m_selection
.Add( i
);
2226 unsigned int first_visible
= GetFirstVisibleRow();
2227 unsigned int last_visible
= GetLastVisibleRow();
2229 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2231 unsigned int row
= m_selection
[i
];
2232 if ((row
>= first_visible
) && (row
<= last_visible
))
2235 m_selection
.Clear();
2239 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2241 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2245 m_selection
.Add( row
);
2253 m_selection
.Remove( row
);
2259 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2263 unsigned int tmp
= from
;
2269 for (i
= from
; i
<= to
; i
++)
2271 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2274 m_selection
.Add( i
);
2279 m_selection
.Remove( i
);
2282 RefreshRows( from
, to
);
2285 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2287 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2289 int n
= aSelections
[i
];
2291 m_selection
.Add( n
);
2296 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2298 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2299 m_selection
.Add( row
);
2301 m_selection
.Remove( row
);
2305 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2307 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2310 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2312 wxWindow
*parent
= GetParent();
2313 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2315 le
.SetEventObject(parent
);
2316 le
.SetModel(GetOwner()->GetModel());
2319 parent
->GetEventHandler()->ProcessEvent(le
);
2322 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2324 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
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::RefreshRows( unsigned int from
, unsigned int to
)
2338 unsigned int tmp
= to
;
2343 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2344 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2346 wxSize client_size
= GetClientSize();
2347 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2348 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2349 if (intersect_rect
.width
> 0)
2350 Refresh( true, &intersect_rect
);
2353 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2355 wxSize client_size
= GetClientSize();
2356 int start
= GetLineStart( firstRow
);
2357 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2358 if (start
> client_size
.y
) return;
2360 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2362 Refresh( true, &rect
);
2365 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2367 wxCHECK_RET( newCurrent
< GetRowCount(),
2368 wxT("invalid item index in OnArrowChar()") );
2370 // if there is no selection, we cannot move it anywhere
2371 if (!HasCurrentRow())
2374 unsigned int oldCurrent
= m_currentRow
;
2376 // in single selection we just ignore Shift as we can't select several
2378 if ( event
.ShiftDown() && !IsSingleSel() )
2380 RefreshRow( oldCurrent
);
2382 ChangeCurrentRow( newCurrent
);
2384 // select all the items between the old and the new one
2385 if ( oldCurrent
> newCurrent
)
2387 newCurrent
= oldCurrent
;
2388 oldCurrent
= m_currentRow
;
2391 SelectRows( oldCurrent
, newCurrent
, true );
2392 if (oldCurrent
!=newCurrent
)
2393 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2397 RefreshRow( oldCurrent
);
2399 // all previously selected items are unselected unless ctrl is held
2400 if ( !event
.ControlDown() )
2401 SelectAllRows(false);
2403 ChangeCurrentRow( newCurrent
);
2405 if ( !event
.ControlDown() )
2407 SelectRow( m_currentRow
, true );
2408 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2411 RefreshRow( m_currentRow
);
2414 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2417 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2421 rect
.y
= GetLineStart( row
);
2422 rect
.width
= GetEndOfLastCol();
2423 rect
.height
= GetLineHeight( row
);
2428 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2430 const wxDataViewModel
*model
= GetOwner()->GetModel();
2432 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2434 // TODO make more efficient
2439 for (r
= 0; r
< row
; r
++)
2441 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2442 if (!node
) return start
;
2444 wxDataViewItem item
= node
->GetItem();
2446 if (node
&& !node
->HasChildren())
2448 // Yes, if the node does not have any child, it must be a leaf which
2449 // mean that it is a temporarily created by GetTreeNodeByRow
2453 unsigned int cols
= GetOwner()->GetColumnCount();
2455 int height
= m_lineHeight
;
2456 for (col
= 0; col
< cols
; col
++)
2458 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2459 if (column
->IsHidden())
2460 continue; // skip it!
2463 model
->IsContainer(item
) &&
2464 !model
->HasContainerColumns(item
))
2465 continue; // skip it!
2468 model
->GetValue( value
, item
, column
->GetModelColumn() );
2470 wxDataViewRenderer
*renderer
=
2471 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2472 renderer
->SetValue( value
);
2473 height
= wxMax( height
, renderer
->GetSize().y
);
2483 return row
* m_lineHeight
;
2487 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2489 const wxDataViewModel
*model
= GetOwner()->GetModel();
2491 // check for the easy case first
2492 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2493 return y
/ m_lineHeight
;
2495 // TODO make more efficient
2496 unsigned int row
= 0;
2497 unsigned int yy
= 0;
2500 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2503 // not really correct...
2504 return row
+ ((y
-yy
) / m_lineHeight
);
2507 wxDataViewItem item
= node
->GetItem();
2509 if (node
&& !node
->HasChildren())
2511 // Yes, if the node does not have any child, it must be a leaf which
2512 // mean that it is a temporarily created by GetTreeNodeByRow
2516 unsigned int cols
= GetOwner()->GetColumnCount();
2518 int height
= m_lineHeight
;
2519 for (col
= 0; col
< cols
; col
++)
2521 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2522 if (column
->IsHidden())
2523 continue; // skip it!
2526 model
->IsContainer(item
) &&
2527 !model
->HasContainerColumns(item
))
2528 continue; // skip it!
2531 model
->GetValue( value
, item
, column
->GetModelColumn() );
2533 wxDataViewRenderer
*renderer
=
2534 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2535 renderer
->SetValue( value
);
2536 height
= wxMax( height
, renderer
->GetSize().y
);
2547 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2549 const wxDataViewModel
*model
= GetOwner()->GetModel();
2551 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2553 wxASSERT( !IsVirtualList() );
2555 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2556 // wxASSERT( node );
2557 if (!node
) return m_lineHeight
;
2559 wxDataViewItem item
= node
->GetItem();
2561 if (node
&& !node
->HasChildren())
2563 // Yes, if the node does not have any child, it must be a leaf which
2564 // mean that it is a temporarily created by GetTreeNodeByRow
2568 int height
= m_lineHeight
;
2570 unsigned int cols
= GetOwner()->GetColumnCount();
2572 for (col
= 0; col
< cols
; col
++)
2574 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2575 if (column
->IsHidden())
2576 continue; // skip it!
2579 model
->IsContainer(item
) &&
2580 !model
->HasContainerColumns(item
))
2581 continue; // skip it!
2584 model
->GetValue( value
, item
, column
->GetModelColumn() );
2586 wxDataViewRenderer
*renderer
=
2587 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2588 renderer
->SetValue( value
);
2589 height
= wxMax( height
, renderer
->GetSize().y
);
2596 return m_lineHeight
;
2600 class RowToItemJob
: public DoJob
2603 RowToItemJob( unsigned int row
, int current
)
2604 { this->row
= row
; this->current
= current
; }
2605 virtual ~RowToItemJob() {}
2607 virtual int operator() ( wxDataViewTreeNode
* node
)
2610 if( current
== static_cast<int>(row
))
2612 ret
= node
->GetItem();
2616 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2618 current
+= node
->GetSubTreeCount();
2623 // If the current has no child node, we can find the desired item of the row
2625 // This if can speed up finding in some case, and will has a very good effect
2626 // when it comes to list view
2627 if( node
->GetNodes().GetCount() == 0)
2629 int index
= static_cast<int>(row
) - current
- 1;
2630 ret
= node
->GetChildren().Item( index
);
2637 virtual int operator() ( void * n
)
2640 if( current
== static_cast<int>(row
))
2642 ret
= wxDataViewItem( n
);
2648 wxDataViewItem
GetResult() const
2657 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2659 if (IsVirtualList())
2661 return wxDataViewItem( wxUIntToPtr(row
+1) );
2665 RowToItemJob
job( row
, -2 );
2666 Walker( m_root
, job
);
2667 return job
.GetResult();
2671 class RowToTreeNodeJob
: public DoJob
2674 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2677 this->current
= current
;
2681 virtual ~RowToTreeNodeJob(){ }
2683 virtual int operator() ( wxDataViewTreeNode
* node
)
2686 if( current
== static_cast<int>(row
))
2692 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2694 current
+= node
->GetSubTreeCount();
2701 // If the current node has no children, we can find the desired item of the
2702 // row number directly.
2703 // This if can speed up finding in some case, and will have a very good
2704 // effect for list views.
2705 if( node
->GetNodes().GetCount() == 0)
2707 int index
= static_cast<int>(row
) - current
- 1;
2708 void * n
= node
->GetChildren().Item( index
);
2709 ret
= new wxDataViewTreeNode( parent
);
2710 ret
->SetItem( wxDataViewItem( n
));
2711 ret
->SetHasChildren(false);
2718 virtual int operator() ( void * n
)
2721 if( current
== static_cast<int>(row
))
2723 ret
= new wxDataViewTreeNode( parent
);
2724 ret
->SetItem( wxDataViewItem( n
));
2725 ret
->SetHasChildren(false);
2732 wxDataViewTreeNode
* GetResult() const
2738 wxDataViewTreeNode
* ret
;
2739 wxDataViewTreeNode
* parent
;
2742 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2744 wxASSERT( !IsVirtualList() );
2746 RowToTreeNodeJob
job( row
, -2, m_root
);
2747 Walker( m_root
, job
);
2748 return job
.GetResult();
2751 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2752 const wxDataViewItem
& item
)
2754 wxWindow
*parent
= GetParent();
2755 wxDataViewEvent
le(type
, parent
->GetId());
2757 le
.SetEventObject(parent
);
2758 le
.SetModel(GetOwner()->GetModel());
2761 parent
->GetEventHandler()->ProcessEvent(le
);
2765 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2767 if (IsVirtualList())
2770 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2774 if (!node
->HasChildren())
2780 return node
->IsOpen();
2783 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2785 if (IsVirtualList())
2788 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2792 if (!node
->HasChildren())
2801 void wxDataViewMainWindow::Expand( unsigned int row
)
2803 if (IsVirtualList())
2806 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2810 if (!node
->HasChildren())
2816 if (!node
->IsOpen())
2819 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2821 // Check if the user prevent expanding
2822 if( e
.GetSkipped() )
2827 // build the children of current node
2828 if( node
->GetChildrenNumber() == 0 )
2831 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2834 // By expanding the node all row indices that are currently in the selection list
2835 // and are greater than our node have become invalid. So we have to correct that now.
2836 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2837 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2839 const unsigned testRow
= m_selection
[i
];
2840 // all rows above us are not affected, so skip them
2844 m_selection
[i
] += rowAdjustment
;
2847 if(m_currentRow
> row
)
2848 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2852 // Send the expanded event
2853 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2857 void wxDataViewMainWindow::Collapse(unsigned int row
)
2859 if (IsVirtualList())
2862 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2866 if (!node
->HasChildren())
2875 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2876 if( e
.GetSkipped() )
2879 // Find out if there are selected items below the current node.
2880 bool selectCollapsingRow
= false;
2881 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2882 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2883 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2885 const unsigned testRow
= m_selection
[i
];
2886 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2888 selectCollapsingRow
= true;
2889 // get out as soon as we have found a node that is selected
2896 // If the node to be closed has selected items the user won't see those any longer.
2897 // We select the collapsing node in this case.
2898 if(selectCollapsingRow
)
2900 SelectAllRows(false);
2901 ChangeCurrentRow(row
);
2902 SelectRow(row
, true);
2903 SendSelectionChangedEvent(GetItemByRow(row
));
2907 // if there were no selected items below our node we still need to "fix" the
2908 // selection list to adjust for the changing of the row indices.
2909 // We actually do the opposite of what we are doing in Expand().
2910 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2912 const unsigned testRow
= m_selection
[i
];
2913 // all rows above us are not affected, so skip them
2917 m_selection
[i
] -= rowAdjustment
;
2920 // if the "current row" is being collapsed away we change it to the current row ;-)
2921 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2922 ChangeCurrentRow(row
);
2923 else if(m_currentRow
> row
)
2924 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2929 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2933 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2935 wxDataViewModel
* model
= GetOwner()->GetModel();
2942 // Compose the a parent-chain of the finding item
2944 list
.DeleteContents( true );
2945 wxDataViewItem
it( item
);
2948 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2949 list
.Insert( pItem
);
2950 it
= model
->GetParent( it
);
2953 // Find the item along the parent-chain.
2954 // This algorithm is designed to speed up the node-finding method
2955 wxDataViewTreeNode
* node
= m_root
;
2956 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
2958 if( node
->HasChildren() )
2960 if( node
->GetChildrenNumber() == 0 )
2963 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2966 wxDataViewTreeNodes nodes
= node
->GetNodes();
2970 for (i
= 0; i
< nodes
.GetCount(); i
++)
2972 if (nodes
[i
]->GetItem() == (**iter
))
2974 if (nodes
[i
]->GetItem() == item
)
2991 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
2992 wxDataViewColumn
* &column
)
2994 wxDataViewColumn
*col
= NULL
;
2995 unsigned int cols
= GetOwner()->GetColumnCount();
2996 unsigned int colnum
= 0;
2998 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
2999 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3001 col
= GetOwner()->GetColumnAt(colnum
);
3002 if (col
->IsHidden())
3003 continue; // skip it!
3005 unsigned int w
= col
->GetWidth();
3006 if (x_start
+w
>= (unsigned int)x
)
3013 item
= GetItemByRow( GetLineAt( y
) );
3016 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3017 const wxDataViewColumn
* column
)
3022 unsigned int cols
= GetOwner()->GetColumnCount();
3023 // If column is null the loop will compute the combined width of all columns.
3024 // Otherwise, it will compute the x position of the column we are looking for.
3025 for (unsigned int i
= 0; i
< cols
; i
++)
3027 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3032 if (col
->IsHidden())
3033 continue; // skip it!
3035 xpos
+= col
->GetWidth();
3036 width
+= col
->GetWidth();
3041 // If we have a column, we need can get its width directly.
3042 if(column
->IsHidden())
3045 width
= column
->GetWidth();
3050 // If we have no column, we reset the x position back to zero.
3054 // we have to take an expander column into account and compute its indentation
3055 // to get the correct x position where the actual text is
3057 int row
= GetRowByItem(item
);
3058 if (!IsVirtualList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3060 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3061 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3062 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3064 if(!node
->HasChildren())
3068 wxRect
itemRect( xpos
+ indent
,
3069 GetLineStart( row
),
3071 GetLineHeight( row
) );
3073 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3074 &itemRect
.x
, &itemRect
.y
);
3079 int wxDataViewMainWindow::RecalculateCount()
3081 if (IsVirtualList())
3083 wxDataViewVirtualListModel
*list_model
=
3084 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3086 return list_model
->GetCount();
3090 return m_root
->GetSubTreeCount();
3094 class ItemToRowJob
: public DoJob
3097 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3104 // Maybe binary search will help to speed up this process
3105 virtual int operator() ( wxDataViewTreeNode
* node
)
3108 if( node
->GetItem() == item
)
3113 if( node
->GetItem() == **m_iter
)
3120 ret
+= node
->GetSubTreeCount();
3126 virtual int operator() ( void * n
)
3129 if( n
== item
.GetID() )
3134 // the row number is begin from zero
3135 int GetResult() const
3139 ItemList::const_iterator m_iter
;
3140 wxDataViewItem item
;
3145 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3147 const wxDataViewModel
* model
= GetOwner()->GetModel();
3151 if (IsVirtualList())
3153 return wxPtrToUInt( item
.GetID() ) -1;
3160 // Compose the a parent-chain of the finding item
3162 wxDataViewItem
* pItem
;
3163 list
.DeleteContents( true );
3164 wxDataViewItem
it( item
);
3167 pItem
= new wxDataViewItem( it
);
3168 list
.Insert( pItem
);
3169 it
= model
->GetParent( it
);
3171 pItem
= new wxDataViewItem( );
3172 list
.Insert( pItem
);
3174 ItemToRowJob
job( item
, list
.begin() );
3175 Walker(m_root
, job
);
3176 return job
.GetResult();
3180 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
3181 wxDataViewTreeNode
* node
)
3183 if( !model
->IsContainer( item
) )
3186 wxDataViewItemArray children
;
3187 unsigned int num
= model
->GetChildren( item
, children
);
3189 unsigned int index
= 0;
3190 while( index
< num
)
3192 if( model
->IsContainer( children
[index
] ) )
3194 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3195 n
->SetItem(children
[index
]);
3196 n
->SetHasChildren( true );
3201 node
->AddLeaf( children
[index
].GetID() );
3205 node
->SetSubTreeCount( num
);
3206 wxDataViewTreeNode
* n
= node
->GetParent();
3208 n
->ChangeSubTreeCount(num
);
3212 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3216 if (GetOwner()->GetModel()->IsVirtualListModel())
3222 m_root
= new wxDataViewTreeNode( NULL
);
3223 m_root
->SetHasChildren(true);
3225 // First we define a invalid item to fetch the top-level elements
3226 wxDataViewItem item
;
3228 BuildTreeHelper( model
, item
, m_root
);
3232 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3234 if( node
->GetNodeNumber() != 0 )
3236 int len
= node
->GetNodeNumber();
3237 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3238 for (int i
= 0; i
< len
; i
++)
3239 DestroyTreeHelper(nodes
[i
]);
3244 void wxDataViewMainWindow::DestroyTree()
3246 if (!IsVirtualList())
3248 ::DestroyTreeHelper(m_root
);
3254 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3256 if ( GetParent()->HandleAsNavigationKey(event
) )
3259 // no item -> nothing to do
3260 if (!HasCurrentRow())
3266 // don't use m_linesPerPage directly as it might not be computed yet
3267 const int pageSize
= GetCountPerPage();
3268 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3270 switch ( event
.GetKeyCode() )
3274 wxWindow
*parent
= GetParent();
3275 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3277 le
.SetItem( GetItemByRow(m_currentRow
) );
3278 le
.SetEventObject(parent
);
3279 le
.SetModel(GetOwner()->GetModel());
3281 parent
->GetEventHandler()->ProcessEvent(le
);
3286 if ( m_currentRow
> 0 )
3287 OnArrowChar( m_currentRow
- 1, event
);
3291 if ( m_currentRow
< GetRowCount() - 1 )
3292 OnArrowChar( m_currentRow
+ 1, event
);
3294 // Add the process for tree expanding/collapsing
3297 if (IsVirtualList())
3300 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3304 if (node
->HasChildren() && node
->IsOpen())
3306 Collapse(m_currentRow
);
3308 else // if the node is already closed we move the selection to its parent
3310 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3312 if(!node
->HasChildren())
3317 int parent
= GetRowByItem( parent_node
->GetItem() );
3320 unsigned int row
= m_currentRow
;
3321 SelectRow( row
, false);
3322 SelectRow( parent
, true );
3323 ChangeCurrentRow( parent
);
3324 GetOwner()->EnsureVisible( parent
, -1 );
3325 SendSelectionChangedEvent( parent_node
->GetItem() );
3333 if (!IsExpanded( m_currentRow
))
3334 Expand( m_currentRow
);
3337 unsigned int row
= m_currentRow
;
3338 SelectRow( row
, false );
3339 SelectRow( row
+ 1, true );
3340 ChangeCurrentRow( row
+ 1 );
3341 GetOwner()->EnsureVisible( row
+ 1, -1 );
3342 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3349 OnArrowChar( GetRowCount() - 1, event
);
3354 OnArrowChar( 0, event
);
3359 int steps
= pageSize
- 1;
3360 int index
= m_currentRow
- steps
;
3364 OnArrowChar( index
, event
);
3370 int steps
= pageSize
- 1;
3371 unsigned int index
= m_currentRow
+ steps
;
3372 unsigned int count
= GetRowCount();
3373 if ( index
>= count
)
3376 OnArrowChar( index
, event
);
3382 if(m_selection
.size() == 1)
3384 // TODO: we need to revise that when we have a concept for a 'current column'
3385 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3395 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3397 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3399 // let the base handle mouse wheel events.
3404 int x
= event
.GetX();
3405 int y
= event
.GetY();
3406 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3407 wxDataViewColumn
*col
= NULL
;
3410 unsigned int cols
= GetOwner()->GetColumnCount();
3412 for (i
= 0; i
< cols
; i
++)
3414 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3416 continue; // skip it!
3418 if (x
< xpos
+ c
->GetWidth())
3423 xpos
+= c
->GetWidth();
3428 wxDataViewRenderer
*cell
= col
->GetRenderer();
3429 unsigned int current
= GetLineAt( y
);
3430 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3432 // Unselect all if below the last row ?
3436 // Test whether the mouse is hovered on the tree item button
3437 bool hoverOverExpander
= false;
3438 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3440 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3441 if( node
!=NULL
&& node
->HasChildren() )
3443 int indent
= node
->GetIndentLevel();
3444 indent
= GetOwner()->GetIndent()*indent
;
3446 // we make the rectangle we are looking in a bit bigger than the actual
3447 // visual expander so the user can hit that little thing reliably
3448 wxRect
rect( xpos
+ indent
,
3449 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3450 m_lineHeight
, m_lineHeight
);
3451 if( rect
.Contains(x
, y
) )
3453 // So the mouse is over the expander
3454 hoverOverExpander
= true;
3455 if (m_underMouse
&& m_underMouse
!= node
)
3457 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3458 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3460 if (m_underMouse
!= node
)
3462 // wxLogMessage("Do the row: %d", current);
3463 RefreshRow(current
);
3465 m_underMouse
= node
;
3468 if (node
!=NULL
&& !node
->HasChildren())
3471 if (!hoverOverExpander
)
3473 if (m_underMouse
!= NULL
)
3475 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3476 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3477 m_underMouse
= NULL
;
3481 wxDataViewModel
*model
= GetOwner()->GetModel();
3483 #if wxUSE_DRAG_AND_DROP
3484 if (event
.Dragging())
3486 if (m_dragCount
== 0)
3488 // we have to report the raw, physical coords as we want to be
3489 // able to call HitTest(event.m_pointDrag) from the user code to
3490 // get the item being dragged
3491 m_dragStart
= event
.GetPosition();
3496 if (m_dragCount
!= 3)
3499 if (event
.LeftIsDown())
3501 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3502 &m_dragStart
.x
, &m_dragStart
.y
);
3503 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3504 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3506 // Notify cell about drag
3507 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3508 event
.SetEventObject( m_owner
);
3509 event
.SetItem( item
);
3510 event
.SetModel( model
);
3511 if (!m_owner
->HandleWindowEvent( event
))
3514 if (!event
.IsAllowed())
3517 wxDataObject
*obj
= event
.GetDataObject();
3521 wxDataViewDropSource
drag( this, drag_item_row
);
3522 drag
.SetData( *obj
);
3523 /* wxDragResult res = */ drag
.DoDragDrop();
3532 #endif // wxUSE_DRAG_AND_DROP
3534 bool simulateClick
= false;
3536 if (event
.ButtonDClick())
3538 m_renameTimer
->Stop();
3539 m_lastOnSame
= false;
3542 wxDataViewItem item
= GetItemByRow(current
);
3543 bool ignore_other_columns
=
3544 ((GetOwner()->GetExpanderColumn() != col
) &&
3545 (model
->IsContainer(item
)) &&
3546 (!model
->HasContainerColumns(item
)));
3548 if (event
.LeftDClick())
3550 if(hoverOverExpander
)
3552 // a double click on the expander will be converted into a "simulated" normal click
3553 simulateClick
= true;
3555 else if ( current
== m_lineLastClicked
)
3557 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3559 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3562 model
->GetValue( value
, item
, col
->GetModelColumn() );
3563 custom
->SetValue( value
);
3564 wxRect
cell_rect( xpos
, GetLineStart( current
),
3565 col
->GetWidth(), GetLineHeight( current
) );
3566 custom
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3571 wxWindow
*parent
= GetParent();
3572 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3574 le
.SetEventObject(parent
);
3575 le
.SetModel(GetOwner()->GetModel());
3577 parent
->GetEventHandler()->ProcessEvent(le
);
3583 // The first click was on another item, so don't interpret this as
3584 // a double click, but as a simple click instead
3585 simulateClick
= true;
3589 if (event
.LeftUp() && !hoverOverExpander
)
3591 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3593 // select single line
3594 SelectAllRows( false );
3595 SelectRow( m_lineSelectSingleOnUp
, true );
3596 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3599 // If the user click the expander, we do not do editing even if the column
3600 // with expander are editable
3601 if (m_lastOnSame
&& !ignore_other_columns
)
3603 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3604 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3606 m_renameTimer
->Start( 100, true );
3610 m_lastOnSame
= false;
3611 m_lineSelectSingleOnUp
= (unsigned int)-1;
3613 else if(!event
.LeftUp())
3615 // This is necessary, because after a DnD operation in
3616 // from and to ourself, the up event is swallowed by the
3617 // DnD code. So on next non-up event (which means here and
3618 // now) m_lineSelectSingleOnUp should be reset.
3619 m_lineSelectSingleOnUp
= (unsigned int)-1;
3622 if (event
.RightDown())
3624 m_lineBeforeLastClicked
= m_lineLastClicked
;
3625 m_lineLastClicked
= current
;
3627 // If the item is already selected, do not update the selection.
3628 // Multi-selections should not be cleared if a selected item is clicked.
3629 if (!IsRowSelected(current
))
3631 SelectAllRows(false);
3632 ChangeCurrentRow(current
);
3633 SelectRow(m_currentRow
,true);
3634 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3637 else if (event
.RightUp())
3640 model
->GetValue( value
, item
, col
->GetModelColumn() );
3641 wxWindow
*parent
= GetParent();
3642 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3644 le
.SetEventObject(parent
);
3645 le
.SetModel(GetOwner()->GetModel());
3647 parent
->GetEventHandler()->ProcessEvent(le
);
3649 else if (event
.MiddleDown())
3653 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3655 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3657 // hoverOverExpander being true tells us that our node must be
3658 // valid and have children.
3659 // So we don't need any extra checks.
3660 if( node
->IsOpen() )
3665 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3669 m_lineBeforeLastClicked
= m_lineLastClicked
;
3670 m_lineLastClicked
= current
;
3672 unsigned int oldCurrentRow
= m_currentRow
;
3673 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3675 bool cmdModifierDown
= event
.CmdDown();
3676 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3678 if ( IsSingleSel() || !IsRowSelected(current
) )
3680 SelectAllRows( false );
3681 ChangeCurrentRow(current
);
3682 SelectRow(m_currentRow
,true);
3683 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3685 else // multi sel & current is highlighted & no mod keys
3687 m_lineSelectSingleOnUp
= current
;
3688 ChangeCurrentRow(current
); // change focus
3691 else // multi sel & either ctrl or shift is down
3693 if (cmdModifierDown
)
3695 ChangeCurrentRow(current
);
3696 ReverseRowSelection(m_currentRow
);
3697 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3699 else if (event
.ShiftDown())
3701 ChangeCurrentRow(current
);
3703 unsigned int lineFrom
= oldCurrentRow
,
3706 if ( lineTo
< lineFrom
)
3709 lineFrom
= m_currentRow
;
3712 SelectRows(lineFrom
, lineTo
, true);
3713 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3715 else // !ctrl, !shift
3717 // test in the enclosing if should make it impossible
3718 wxFAIL_MSG( wxT("how did we get here?") );
3722 if (m_currentRow
!= oldCurrentRow
)
3723 RefreshRow( oldCurrentRow
);
3725 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3727 // Update selection here...
3730 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3731 (current
== oldCurrentRow
)) && oldWasSelected
;
3733 // Call LeftClick after everything else as under GTK+
3734 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3736 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3738 // notify cell about click
3740 model
->GetValue( value
, item
, col
->GetModelColumn() );
3741 custom
->SetValue( value
);
3742 wxRect
cell_rect( xpos
, GetLineStart( current
),
3743 col
->GetWidth(), GetLineHeight( current
) );
3744 /* ignore ret */ custom
->LeftClick( event
.GetPosition(), cell_rect
,
3745 model
, item
, col
->GetModelColumn());
3751 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3755 if (HasCurrentRow())
3761 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3765 if (HasCurrentRow())
3771 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3773 if( m_selection
.GetCount() != 1 )
3774 return wxDataViewItem();
3776 return GetItemByRow( m_selection
.Item(0));
3779 //-----------------------------------------------------------------------------
3781 //-----------------------------------------------------------------------------
3783 WX_DEFINE_LIST(wxDataViewColumnList
)
3785 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3786 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3787 EVT_SIZE(wxDataViewCtrl::OnSize
)
3790 wxDataViewCtrl::~wxDataViewCtrl()
3793 GetModel()->RemoveNotifier( m_notifier
);
3798 void wxDataViewCtrl::Init()
3800 m_cols
.DeleteContents(true);
3803 // No sorting column at start
3804 m_sortingColumnIdx
= wxNOT_FOUND
;
3806 m_headerArea
= NULL
;
3809 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3810 const wxPoint
& pos
, const wxSize
& size
,
3811 long style
, const wxValidator
& validator
)
3813 // if ( (style & wxBORDER_MASK) == 0)
3814 // style |= wxBORDER_SUNKEN;
3818 if (!wxControl::Create( parent
, id
, pos
, size
,
3819 style
| wxScrolledWindowStyle
, validator
))
3822 SetInitialSize(size
);
3825 MacSetClipChildren( true );
3828 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3830 if (HasFlag(wxDV_NO_HEADER
))
3831 m_headerArea
= NULL
;
3833 m_headerArea
= new wxDataViewHeaderWindow(this);
3835 SetTargetWindow( m_clientArea
);
3837 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3839 sizer
->Add( m_headerArea
, 0, wxGROW
);
3840 sizer
->Add( m_clientArea
, 1, wxGROW
);
3846 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3848 return wxBORDER_THEME
;
3852 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3856 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3859 // we need to process arrows ourselves for scrolling
3860 if ( nMsg
== WM_GETDLGCODE
)
3862 rc
|= DLGC_WANTARROWS
;
3870 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3872 wxSize newsize
= size
;
3873 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3874 newsize
.y
-= m_headerArea
->GetSize().y
;
3879 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3881 // We need to override OnSize so that our scrolled
3882 // window a) does call Layout() to use sizers for
3883 // positioning the controls but b) does not query
3884 // the sizer for their size and use that for setting
3885 // the scrollable area as set that ourselves by
3886 // calling SetScrollbar() further down.
3893 void wxDataViewCtrl::SetFocus()
3896 m_clientArea
->SetFocus();
3899 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3901 if (!wxDataViewCtrlBase::AssociateModel( model
))
3904 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3906 model
->AddNotifier( m_notifier
);
3908 m_clientArea
->DestroyTree();
3910 m_clientArea
->BuildTree(model
);
3912 m_clientArea
->UpdateDisplay();
3917 #if wxUSE_DRAG_AND_DROP
3919 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
3921 return m_clientArea
->EnableDragSource( format
);
3924 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
3926 return m_clientArea
->EnableDropTarget( format
);
3929 #endif // wxUSE_DRAG_AND_DROP
3931 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3933 if (!wxDataViewCtrlBase::AppendColumn(col
))
3936 m_cols
.Append( col
);
3937 OnColumnsCountChanged();
3941 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3943 if (!wxDataViewCtrlBase::PrependColumn(col
))
3946 m_cols
.Insert( col
);
3947 OnColumnsCountChanged();
3951 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3953 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
3956 m_cols
.Insert( pos
, col
);
3957 OnColumnsCountChanged();
3961 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
3964 m_headerArea
->UpdateColumn(idx
);
3966 m_clientArea
->UpdateDisplay();
3969 void wxDataViewCtrl::OnColumnsCountChanged()
3972 m_headerArea
->SetColumnCount(GetColumnCount());
3974 m_clientArea
->UpdateDisplay();
3977 void wxDataViewCtrl::DoSetExpanderColumn()
3979 m_clientArea
->UpdateDisplay();
3982 void wxDataViewCtrl::DoSetIndent()
3984 m_clientArea
->UpdateDisplay();
3987 unsigned int wxDataViewCtrl::GetColumnCount() const
3989 return m_cols
.GetCount();
3992 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
3997 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
3999 // columns can't be reordered if there is no header window which allows
4001 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4004 return GetColumn(idx
);
4007 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4009 const unsigned count
= m_cols
.size();
4010 for ( unsigned n
= 0; n
< count
; n
++ )
4012 if ( m_cols
[n
] == column
)
4019 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4020 unsigned int WXUNUSED(new_pos
))
4022 // do _not_ reorder m_cols elements here, they should always be in the
4023 // order in which columns were added, we only display the columns in
4025 m_clientArea
->UpdateDisplay();
4028 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4030 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4035 OnColumnsCountChanged();
4040 bool wxDataViewCtrl::ClearColumns()
4043 OnColumnsCountChanged();
4047 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4051 unsigned int len
= GetColumnCount();
4052 for ( unsigned int i
= 0; i
< len
; i
++ )
4054 wxDataViewColumn
* col
= GetColumnAt(i
);
4055 if (col
->IsHidden())
4057 ret
+= col
->GetWidth();
4060 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4067 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4069 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4070 : GetColumn(m_sortingColumnIdx
);
4073 // Selection code with wxDataViewItem as parameters
4074 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4076 return m_clientArea
->GetSelection();
4079 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4082 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4083 int len
= selection
.GetCount();
4084 for( int i
= 0; i
< len
; i
++)
4086 unsigned int row
= selection
[i
];
4087 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4092 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4094 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4096 wxDataViewItem last_parent
;
4098 int len
= sel
.GetCount();
4099 for( int i
= 0; i
< len
; i
++ )
4101 wxDataViewItem item
= sel
[i
];
4102 wxDataViewItem parent
= GetModel()->GetParent( item
);
4105 if (parent
!= last_parent
)
4106 ExpandAncestors(item
);
4109 last_parent
= parent
;
4110 int row
= m_clientArea
->GetRowByItem( item
);
4112 selection
.Add( static_cast<unsigned int>(row
) );
4115 m_clientArea
->SetSelections( selection
);
4118 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4120 ExpandAncestors( item
);
4122 int row
= m_clientArea
->GetRowByItem( item
);
4125 // Unselect all rows before select another in the single select mode
4126 if (m_clientArea
->IsSingleSel())
4127 m_clientArea
->SelectAllRows(false);
4129 m_clientArea
->SelectRow(row
, true);
4131 // Also set focus to the selected item
4132 m_clientArea
->ChangeCurrentRow( row
);
4136 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4138 int row
= m_clientArea
->GetRowByItem( item
);
4140 m_clientArea
->SelectRow(row
, false);
4143 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4145 int row
= m_clientArea
->GetRowByItem( item
);
4148 return m_clientArea
->IsRowSelected(row
);
4153 // Selection code with row number as parameter
4154 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4157 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4158 int len
= selection
.GetCount();
4159 for( int i
= 0; i
< len
; i
++)
4161 unsigned int row
= selection
[i
];
4167 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4169 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4170 int len
= sel
.GetCount();
4171 for( int i
= 0; i
< len
; i
++ )
4175 selection
.Add( static_cast<unsigned int>(row
) );
4177 m_clientArea
->SetSelections( selection
);
4180 void wxDataViewCtrl::Select( int row
)
4184 if (m_clientArea
->IsSingleSel())
4185 m_clientArea
->SelectAllRows(false);
4186 m_clientArea
->SelectRow( row
, true );
4190 void wxDataViewCtrl::Unselect( int row
)
4193 m_clientArea
->SelectRow(row
, false);
4196 bool wxDataViewCtrl::IsSelected( int row
) const
4199 return m_clientArea
->IsRowSelected(row
);
4203 void wxDataViewCtrl::SelectRange( int from
, int to
)
4206 for( int i
= from
; i
< to
; i
++ )
4208 m_clientArea
->Select(sel
);
4211 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4213 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4214 for( int i
= from
; i
< to
; i
++ )
4215 if( sel
.Index( i
) != wxNOT_FOUND
)
4217 m_clientArea
->SetSelections(sel
);
4220 void wxDataViewCtrl::SelectAll()
4222 m_clientArea
->SelectAllRows(true);
4225 void wxDataViewCtrl::UnselectAll()
4227 m_clientArea
->SelectAllRows(false);
4230 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4234 if( row
> (int) m_clientArea
->GetRowCount() )
4235 row
= m_clientArea
->GetRowCount();
4237 int first
= m_clientArea
->GetFirstVisibleRow();
4238 int last
= m_clientArea
->GetLastVisibleRow();
4240 m_clientArea
->ScrollTo( row
, column
);
4241 else if( row
> last
)
4242 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4244 m_clientArea
->ScrollTo( first
, column
);
4247 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4249 ExpandAncestors( item
);
4251 m_clientArea
->RecalculateDisplay();
4253 int row
= m_clientArea
->GetRowByItem(item
);
4256 if( column
== NULL
)
4257 EnsureVisible(row
, -1);
4259 EnsureVisible( row
, GetColumnIndex(column
) );
4264 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4265 wxDataViewColumn
* &column
) const
4267 m_clientArea
->HitTest(point
, item
, column
);
4270 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4271 const wxDataViewColumn
* column
) const
4273 return m_clientArea
->GetItemRect(item
, column
);
4276 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4278 return m_clientArea
->GetItemByRow( row
);
4281 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4283 return m_clientArea
->GetRowByItem( item
);
4286 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4288 int row
= m_clientArea
->GetRowByItem( item
);
4290 m_clientArea
->Expand(row
);
4293 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4295 int row
= m_clientArea
->GetRowByItem( item
);
4297 m_clientArea
->Collapse(row
);
4300 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4302 int row
= m_clientArea
->GetRowByItem( item
);
4304 return m_clientArea
->IsExpanded(row
);
4308 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4310 wxDataViewColumn
* col
= GetColumn( column
);
4314 wxRect itemRect
= GetItemRect(item
, col
);
4315 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4316 renderer
->StartEditing(item
, itemRect
);
4319 #endif // !wxUSE_GENERICDATAVIEWCTRL
4321 #endif // wxUSE_DATAVIEWCTRL