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 wxDataViewRendererBase( varianttype
, mode
, align
)
617 wxDataViewRenderer::~wxDataViewRenderer()
624 wxDataViewRenderer::RenderWithAttr(wxDC
& dc
,
625 const wxRect
& cell_rect
,
627 const wxDataViewItemAttr
*WXUNUSED(attr
),
630 // adjust the rectangle ourselves to account for the alignment
632 wxRect item_rect
= cell_rect
;
635 const wxSize size
= GetSize();
637 // horizontal alignment:
638 if (align
& wxALIGN_CENTER_HORIZONTAL
)
639 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
640 else if (align
& wxALIGN_RIGHT
)
641 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
642 // else: wxALIGN_LEFT is the default
644 // vertical alignment:
645 item_rect
.y
= cell_rect
.y
;
646 if (align
& wxALIGN_CENTER_VERTICAL
)
647 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
648 else if (align
& wxALIGN_BOTTOM
)
649 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
650 // else: wxALIGN_TOP is the default
652 item_rect
.SetSize(size
);
655 return Render(item_rect
, &dc
, state
);
658 wxDC
*wxDataViewRenderer::GetDC()
662 if (GetOwner() == NULL
)
664 if (GetOwner()->GetOwner() == NULL
)
666 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
672 void wxDataViewRenderer::SetAlignment( int align
)
677 int wxDataViewRenderer::GetAlignment() const
682 int wxDataViewRenderer::CalculateAlignment() const
684 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
686 if (GetOwner() == NULL
)
687 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
689 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
695 // ---------------------------------------------------------
696 // wxDataViewCustomRenderer
697 // ---------------------------------------------------------
699 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
701 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
702 wxDataViewCellMode mode
, int align
) :
703 wxDataViewRenderer( varianttype
, mode
, align
)
707 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
,
708 wxRect cell
, wxDC
*dc
, int state
)
710 wxColour col
= state
& wxDATAVIEW_CELL_SELECTED
711 ? wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
)
712 : GetOwner()->GetOwner()->GetForegroundColour();
714 wxDataViewItemAttr attr
;
716 RenderText(*dc
, cell
, wxALIGN_NOT
, text
, &attr
, state
, xoffset
);
720 wxDataViewCustomRenderer::RenderText(wxDC
& dc
,
723 const wxString
& text
,
724 const wxDataViewItemAttr
*attr
,
728 wxDCTextColourChanger
changeFg(dc
);
729 if ( attr
&& attr
->HasColour() )
730 changeFg
.Set(attr
->GetColour());
732 wxDCFontChanger
changeFont(dc
);
733 if ( attr
&& attr
->HasFont() )
735 wxFont
font(dc
.GetFont());
736 if ( attr
->GetBold() )
738 if ( attr
->GetItalic() )
741 changeFont
.Set(font
);
744 wxRect rectText
= rect
;
745 rectText
.x
+= xoffset
;
746 rectText
.width
-= xoffset
;
748 dc
.DrawLabel(text
, rectText
, align
);
751 // ---------------------------------------------------------
752 // wxDataViewTextRenderer
753 // ---------------------------------------------------------
755 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
757 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
758 wxDataViewCellMode mode
, int align
) :
759 wxDataViewCustomRenderer( varianttype
, mode
, align
)
763 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
765 m_text
= value
.GetString();
770 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
775 bool wxDataViewTextRenderer::HasEditorCtrl() const
780 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
781 wxRect labelRect
, const wxVariant
&value
)
783 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
784 wxPoint(labelRect
.x
,labelRect
.y
),
785 wxSize(labelRect
.width
,labelRect
.height
) );
787 // select the text in the control an place the cursor at the end
788 ctrl
->SetInsertionPointEnd();
794 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
796 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
797 value
= text
->GetValue();
802 wxDataViewTextRenderer::RenderWithAttr(wxDC
& dc
,
805 const wxDataViewItemAttr
*attr
,
808 RenderText(dc
, rect
, align
, m_text
, attr
, state
);
812 wxSize
wxDataViewTextRenderer::GetSize() const
814 const wxDataViewCtrl
*view
= GetView();
816 return view
->wxWindowBase::GetTextExtent( m_text
);
817 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
820 // ---------------------------------------------------------
821 // wxDataViewBitmapRenderer
822 // ---------------------------------------------------------
824 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
826 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
827 wxDataViewCellMode mode
, int align
) :
828 wxDataViewCustomRenderer( varianttype
, mode
, align
)
832 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
834 if (value
.GetType() == wxT("wxBitmap"))
836 if (value
.GetType() == wxT("wxIcon"))
842 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
847 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
850 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
851 else if (m_icon
.Ok())
852 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
857 wxSize
wxDataViewBitmapRenderer::GetSize() const
860 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
861 else if (m_icon
.Ok())
862 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
864 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
867 // ---------------------------------------------------------
868 // wxDataViewToggleRenderer
869 // ---------------------------------------------------------
871 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
873 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
874 wxDataViewCellMode mode
, int align
) :
875 wxDataViewCustomRenderer( varianttype
, mode
, align
)
880 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
882 m_toggle
= value
.GetBool();
887 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
892 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
896 flags
|= wxCONTROL_CHECKED
;
897 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
898 flags
|= wxCONTROL_DISABLED
;
900 wxRendererNative::Get().DrawCheckBox(
901 GetOwner()->GetOwner(),
909 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
910 wxDataViewModel
*model
,
911 const wxDataViewItem
& item
, unsigned int col
)
913 bool value
= !m_toggle
;
914 wxVariant variant
= value
;
915 model
->SetValue( variant
, item
, col
);
916 model
->ValueChanged( item
, col
);
920 wxSize
wxDataViewToggleRenderer::GetSize() const
922 // the window parameter is not used by GetCheckBoxSize() so it's
924 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
927 // ---------------------------------------------------------
928 // wxDataViewProgressRenderer
929 // ---------------------------------------------------------
931 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
933 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
934 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
935 wxDataViewCustomRenderer( varianttype
, mode
, align
)
941 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
943 m_value
= (long) value
;
945 if (m_value
< 0) m_value
= 0;
946 if (m_value
> 100) m_value
= 100;
951 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
953 value
= (long) m_value
;
957 bool wxDataViewProgressRenderer::RenderWithAttr(wxDC
& dc
,
960 const wxDataViewItemAttr
*attr
,
963 // deflat the rect to leave a small border between bars in adjacent rows
964 wxRect bar
= rect
.Deflate(0, 1);
966 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
967 dc
.SetPen( *wxBLACK_PEN
);
968 dc
.DrawRectangle( bar
);
970 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
971 dc
.SetPen( *wxTRANSPARENT_PEN
);
972 dc
.SetBrush( attr
&& attr
->HasColour() ? wxBrush(attr
->GetColour())
974 dc
.DrawRectangle( bar
);
979 wxSize
wxDataViewProgressRenderer::GetSize() const
981 return wxSize(40,12);
984 // ---------------------------------------------------------
985 // wxDataViewDateRenderer
986 // ---------------------------------------------------------
988 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
990 #if wxUSE_DATE_RENDERER_POPUP
992 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
995 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
996 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
997 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
1002 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
1003 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1004 sizer
->Add( m_cal
, 1, wxGROW
);
1009 void OnCalendar( wxCalendarEvent
&event
);
1011 wxCalendarCtrl
*m_cal
;
1012 wxDataViewModel
*m_model
;
1014 const wxDataViewItem
& m_item
;
1017 virtual void OnDismiss()
1022 DECLARE_EVENT_TABLE()
1025 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1026 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1029 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1031 wxDateTime date
= event
.GetDate();
1032 wxVariant value
= date
;
1033 m_model
->SetValue( value
, m_item
, m_col
);
1034 m_model
->ValueChanged( m_item
, m_col
);
1038 #endif // wxUSE_DATE_RENDERER_POPUP
1040 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1042 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1043 wxDataViewCellMode mode
, int align
) :
1044 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1048 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1050 m_date
= value
.GetDateTime();
1055 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1061 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1063 wxString tmp
= m_date
.FormatDate();
1064 RenderText( tmp
, 0, cell
, dc
, state
);
1068 wxSize
wxDataViewDateRenderer::GetSize() const
1070 const wxDataViewCtrl
* view
= GetView();
1071 wxString tmp
= m_date
.FormatDate();
1073 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1074 return wxSize(x
,y
+d
);
1077 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1078 const wxDataViewItem
& item
, unsigned int col
)
1081 model
->GetValue( variant
, item
, col
);
1082 wxDateTime value
= variant
.GetDateTime();
1084 #if wxUSE_DATE_RENDERER_POPUP
1085 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1086 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1087 wxPoint pos
= wxGetMousePosition();
1090 popup
->Popup( popup
->m_cal
);
1091 #else // !wxUSE_DATE_RENDERER_POPUP
1092 wxMessageBox(value
.Format());
1093 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1097 // ---------------------------------------------------------
1098 // wxDataViewIconTextRenderer
1099 // ---------------------------------------------------------
1101 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1103 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1104 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1105 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1108 SetAlignment(align
);
1111 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1117 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1123 wxDataViewIconTextRenderer::RenderWithAttr(wxDC
& dc
,
1126 const wxDataViewItemAttr
*attr
,
1131 const wxIcon
& icon
= m_value
.GetIcon();
1134 dc
.DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1135 xoffset
= icon
.GetWidth()+4;
1138 RenderText(dc
, rect
, align
, m_value
.GetText(), attr
, state
, xoffset
);
1143 wxSize
wxDataViewIconTextRenderer::GetSize() const
1145 const wxDataViewCtrl
*view
= GetView();
1146 if (!m_value
.GetText().empty())
1149 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1151 if (m_value
.GetIcon().IsOk())
1152 x
+= m_value
.GetIcon().GetWidth() + 4;
1153 return wxSize( x
, y
);
1155 return wxSize(80,20);
1158 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1160 wxDataViewIconText iconText
;
1163 wxString text
= iconText
.GetText();
1165 // adjust the label rect to take the width of the icon into account
1166 if (iconText
.GetIcon().IsOk())
1168 int w
= iconText
.GetIcon().GetWidth() + 4;
1170 labelRect
.width
-= w
;
1173 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1174 wxPoint(labelRect
.x
,labelRect
.y
),
1175 wxSize(labelRect
.width
,labelRect
.height
) );
1177 // select the text in the control an place the cursor at the end
1178 ctrl
->SetInsertionPointEnd();
1184 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
& value
)
1186 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1188 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1193 //-----------------------------------------------------------------------------
1194 // wxDataViewDropTarget
1195 //-----------------------------------------------------------------------------
1197 #if wxUSE_DRAG_AND_DROP
1199 class wxBitmapCanvas
: public wxWindow
1202 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1203 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1206 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1209 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1212 dc
.DrawBitmap( m_bitmap
, 0, 0);
1218 class wxDataViewDropSource
: public wxDropSource
1221 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1229 ~wxDataViewDropSource()
1234 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1236 wxPoint pos
= wxGetMousePosition();
1240 int liney
= m_win
->GetLineStart( m_row
);
1242 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1243 m_win
->ClientToScreen( &linex
, &liney
);
1244 m_dist_x
= pos
.x
- linex
;
1245 m_dist_y
= pos
.y
- liney
;
1248 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1250 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1251 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1253 wxFRAME_TOOL_WINDOW
|
1254 wxFRAME_FLOAT_ON_PARENT
|
1255 wxFRAME_NO_TASKBAR
|
1257 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1262 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1263 m_hint
->SetTransparent( 128 );
1269 wxDataViewMainWindow
*m_win
;
1272 int m_dist_x
,m_dist_y
;
1276 class wxDataViewDropTarget
: public wxDropTarget
1279 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1285 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1287 wxDataFormat format
= GetMatchingPair();
1288 if (format
== wxDF_INVALID
)
1290 return m_win
->OnDragOver( format
, x
, y
, def
);
1293 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1295 wxDataFormat format
= GetMatchingPair();
1296 if (format
== wxDF_INVALID
)
1298 return m_win
->OnDrop( format
, x
, y
);
1301 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1303 wxDataFormat format
= GetMatchingPair();
1304 if (format
== wxDF_INVALID
)
1308 return m_win
->OnData( format
, x
, y
, def
);
1311 virtual void OnLeave()
1312 { m_win
->OnLeave(); }
1314 wxDataViewMainWindow
*m_win
;
1317 #endif // wxUSE_DRAG_AND_DROP
1319 //-----------------------------------------------------------------------------
1320 // wxDataViewRenameTimer
1321 //-----------------------------------------------------------------------------
1323 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1328 void wxDataViewRenameTimer::Notify()
1330 m_owner
->OnRenameTimer();
1333 //-----------------------------------------------------------------------------
1334 // wxDataViewMainWindow
1335 //-----------------------------------------------------------------------------
1337 // The tree building helper, declared firstly
1338 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
1339 wxDataViewTreeNode
* node
);
1341 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1343 if (row1
> row2
) return 1;
1344 if (row1
== row2
) return 0;
1348 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1350 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1351 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1352 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1353 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1354 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1355 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1358 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1359 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1360 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1361 m_selection( wxDataViewSelectionCmp
)
1366 m_lastOnSame
= false;
1367 m_renameTimer
= new wxDataViewRenameTimer( this );
1369 // TODO: user better initial values/nothing selected
1370 m_currentCol
= NULL
;
1373 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1375 #if wxUSE_DRAG_AND_DROP
1377 m_dragStart
= wxPoint(0,0);
1379 m_dragEnabled
= false;
1380 m_dropEnabled
= false;
1382 m_dropHintLine
= (unsigned int) -1;
1383 #endif // wxUSE_DRAG_AND_DROP
1385 m_lineLastClicked
= (unsigned int) -1;
1386 m_lineBeforeLastClicked
= (unsigned int) -1;
1387 m_lineSelectSingleOnUp
= (unsigned int) -1;
1391 SetBackgroundColour( *wxWHITE
);
1393 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1395 m_penRule
= wxPen(GetRuleColour());
1397 // compose a pen whichcan draw black lines
1398 // TODO: maybe there is something system colour to use
1399 m_penExpander
= wxPen(wxColour(0,0,0));
1401 m_root
= new wxDataViewTreeNode( NULL
);
1402 m_root
->SetHasChildren(true);
1404 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1406 m_underMouse
= NULL
;
1410 wxDataViewMainWindow::~wxDataViewMainWindow()
1413 delete m_renameTimer
;
1417 #if wxUSE_DRAG_AND_DROP
1418 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1420 m_dragFormat
= format
;
1421 m_dragEnabled
= format
!= wxDF_INVALID
;
1426 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1428 m_dropFormat
= format
;
1429 m_dropEnabled
= format
!= wxDF_INVALID
;
1432 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1437 void wxDataViewMainWindow::RemoveDropHint()
1442 RefreshRow( m_dropHintLine
);
1443 m_dropHintLine
= (unsigned int) -1;
1447 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1448 wxCoord y
, wxDragResult def
)
1452 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1453 unsigned int row
= GetLineAt( yy
);
1455 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1461 wxDataViewItem item
= GetItemByRow( row
);
1463 wxDataViewModel
*model
= GetOwner()->GetModel();
1465 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1466 event
.SetEventObject( m_owner
);
1467 event
.SetItem( item
);
1468 event
.SetModel( model
);
1469 event
.SetDataFormat( format
);
1470 if (!m_owner
->HandleWindowEvent( event
))
1476 if (!event
.IsAllowed())
1483 if (m_dropHint
&& (row
!= m_dropHintLine
))
1484 RefreshRow( m_dropHintLine
);
1486 m_dropHintLine
= row
;
1492 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1498 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1499 unsigned int row
= GetLineAt( yy
);
1501 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1504 wxDataViewItem item
= GetItemByRow( row
);
1506 wxDataViewModel
*model
= GetOwner()->GetModel();
1508 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1509 event
.SetEventObject( m_owner
);
1510 event
.SetItem( item
);
1511 event
.SetModel( model
);
1512 event
.SetDataFormat( format
);
1513 if (!m_owner
->HandleWindowEvent( event
))
1516 if (!event
.IsAllowed())
1522 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1527 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1528 unsigned int row
= GetLineAt( yy
);
1530 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1533 wxDataViewItem item
= GetItemByRow( row
);
1535 wxDataViewModel
*model
= GetOwner()->GetModel();
1537 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1539 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1540 event
.SetEventObject( m_owner
);
1541 event
.SetItem( item
);
1542 event
.SetModel( model
);
1543 event
.SetDataFormat( format
);
1544 event
.SetDataSize( obj
->GetSize() );
1545 event
.SetDataBuffer( obj
->GetData() );
1546 if (!m_owner
->HandleWindowEvent( event
))
1549 if (!event
.IsAllowed())
1555 void wxDataViewMainWindow::OnLeave()
1560 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1562 int height
= GetLineHeight( row
);
1564 unsigned int cols
= GetOwner()->GetColumnCount();
1566 for (col
= 0; col
< cols
; col
++)
1568 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1569 if (column
->IsHidden())
1570 continue; // skip it!
1571 width
+= column
->GetWidth();
1575 if (!IsVirtualList())
1577 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1578 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1579 indent
= indent
+ m_lineHeight
;
1580 // try to use the m_lineHeight as the expander space
1582 if(!node
->HasChildren())
1587 wxBitmap
bitmap( width
, height
);
1588 wxMemoryDC
dc( bitmap
);
1589 dc
.SetFont( GetFont() );
1590 dc
.SetPen( *wxBLACK_PEN
);
1591 dc
.SetBrush( *wxWHITE_BRUSH
);
1592 dc
.DrawRectangle( 0,0,width
,height
);
1594 wxDataViewModel
*model
= m_owner
->GetModel();
1596 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1599 // TODO-RTL: last column for RTL support
1600 expander
= GetOwner()->GetColumnAt( 0 );
1601 GetOwner()->SetExpanderColumn(expander
);
1606 for (col
= 0; col
< cols
; col
++)
1608 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1609 wxDataViewRenderer
*cell
= column
->GetRenderer();
1611 if (column
->IsHidden())
1612 continue; // skip it!
1614 width
= column
->GetWidth();
1616 if (column
== expander
)
1620 wxDataViewItem item
= GetItemByRow( row
);
1621 model
->GetValue( value
, item
, column
->GetModelColumn());
1622 cell
->SetValue( value
);
1624 wxRect
item_rect(x
, 0, width
, height
);
1625 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1627 // dc.SetClippingRegion( item_rect );
1628 wxDataViewItemAttr attr
;
1630 hasAttr
= model
->GetAttr(item
, column
->GetModelColumn(), attr
);
1631 cell
->RenderWithAttr(dc
, item_rect
, cell
->CalculateAlignment(),
1632 hasAttr
? &attr
: NULL
, 0);
1633 // dc.DestroyClippingRegion();
1641 #endif // wxUSE_DRAG_AND_DROP
1644 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1646 wxDataViewModel
*model
= GetOwner()->GetModel();
1647 wxAutoBufferedPaintDC
dc( this );
1650 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1651 dc
.SetPen( *wxTRANSPARENT_PEN
);
1652 dc
.DrawRectangle(GetClientSize());
1656 GetOwner()->PrepareDC( dc
);
1657 dc
.SetFont( GetFont() );
1659 wxRect update
= GetUpdateRegion().GetBox();
1660 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1662 // compute which items needs to be redrawn
1663 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1664 unsigned int item_count
=
1665 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1666 (int)(GetRowCount( ) - item_start
));
1667 unsigned int item_last
= item_start
+ item_count
;
1668 // Get the parent of DataViewCtrl
1669 wxWindow
*parent
= GetParent()->GetParent();
1670 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1671 cache_event
.SetEventObject(GetParent());
1672 cache_event
.SetCache(item_start
, item_last
- 1);
1673 parent
->ProcessWindowEvent(cache_event
);
1675 // compute which columns needs to be redrawn
1676 unsigned int cols
= GetOwner()->GetColumnCount();
1677 unsigned int col_start
= 0;
1678 unsigned int x_start
;
1679 for (x_start
= 0; col_start
< cols
; col_start
++)
1681 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1682 if (col
->IsHidden())
1683 continue; // skip it!
1685 unsigned int w
= col
->GetWidth();
1686 if (x_start
+w
>= (unsigned int)update
.x
)
1692 unsigned int col_last
= col_start
;
1693 unsigned int x_last
= x_start
;
1694 for (; col_last
< cols
; col_last
++)
1696 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1697 if (col
->IsHidden())
1698 continue; // skip it!
1700 if (x_last
> (unsigned int)update
.GetRight())
1703 x_last
+= col
->GetWidth();
1706 // Draw horizontal rules if required
1707 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1709 dc
.SetPen(m_penRule
);
1710 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1712 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1714 int y
= GetLineStart( i
);
1715 dc
.DrawLine(x_start
, y
, x_last
, y
);
1719 // Draw vertical rules if required
1720 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1722 dc
.SetPen(m_penRule
);
1723 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1726 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1728 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1729 if (col
->IsHidden())
1730 continue; // skip it
1732 dc
.DrawLine(x
, GetLineStart( item_start
),
1733 x
, GetLineStart( item_last
) );
1735 x
+= col
->GetWidth();
1738 // Draw last vertical rule
1739 dc
.DrawLine(x
, GetLineStart( item_start
),
1740 x
, GetLineStart( item_last
) );
1743 // redraw the background for the items which are selected/current
1744 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1746 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1747 if (selected
|| item
== m_currentRow
)
1749 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1750 if (item
== m_currentRow
)
1751 flags
|= wxCONTROL_CURRENT
;
1753 flags
|= wxCONTROL_FOCUSED
;
1755 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1756 wxRendererNative::Get().DrawItemSelectionRect
1766 #if wxUSE_DRAG_AND_DROP
1769 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1770 x_last
, GetLineHeight( m_dropHintLine
) );
1771 dc
.SetPen( *wxBLACK_PEN
);
1772 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1773 dc
.DrawRectangle( rect
);
1775 #endif // wxUSE_DRAG_AND_DROP
1777 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1780 // TODO-RTL: last column for RTL support
1781 expander
= GetOwner()->GetColumnAt( 0 );
1782 GetOwner()->SetExpanderColumn(expander
);
1785 // redraw all cells for all rows which must be repainted and all columns
1787 cell_rect
.x
= x_start
;
1788 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1790 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1791 wxDataViewRenderer
*cell
= col
->GetRenderer();
1792 cell_rect
.width
= col
->GetWidth();
1794 if (col
->IsHidden())
1795 continue; // skip it!
1797 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1799 // get the cell value and set it into the renderer
1801 wxDataViewTreeNode
*node
= NULL
;
1802 wxDataViewItem dataitem
;
1804 if (!IsVirtualList())
1806 node
= GetTreeNodeByRow(item
);
1810 dataitem
= node
->GetItem();
1812 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1813 !model
->HasContainerColumns(dataitem
))
1818 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1821 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1822 cell
->SetValue( value
);
1825 cell_rect
.y
= GetLineStart( item
);
1826 cell_rect
.height
= GetLineHeight( item
);
1828 // Draw the expander here.
1830 if ((!IsVirtualList()) && (col
== expander
))
1832 indent
= node
->GetIndentLevel();
1834 // Calculate the indent first
1835 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1837 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1839 // change the cell_rect.x to the appropriate pos
1840 int expander_x
= indent
+ EXPANDER_MARGIN
;
1841 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2)
1842 - (expander_width
/2) - EXPANDER_OFFSET
;
1844 indent
= indent
+ m_lineHeight
;
1845 // try to use the m_lineHeight as the expander space
1847 dc
.SetPen( m_penExpander
);
1848 dc
.SetBrush( wxNullBrush
);
1849 if( node
->HasChildren() )
1851 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1853 if (m_underMouse
== node
)
1855 flag
|= wxCONTROL_CURRENT
;
1857 if( node
->IsOpen() )
1858 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
,
1859 flag
|wxCONTROL_EXPANDED
);
1861 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1864 // force the expander column to left-center align
1865 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1867 if (node
&& !node
->HasChildren())
1869 // Yes, if the node does not have any child, it must be a leaf which
1870 // mean that it is a temporarily created by GetTreeNodeByRow
1874 wxRect item_rect
= cell_rect
;
1875 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1877 // account for the tree indent (harmless if we're not indented)
1878 item_rect
.x
+= indent
;
1879 item_rect
.width
-= indent
;
1882 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1883 state
|= wxDATAVIEW_CELL_SELECTED
;
1885 // TODO: it would be much more efficient to create a clipping
1886 // region for the entire column being rendered (in the OnPaint
1887 // of wxDataViewMainWindow) instead of a single clip region for
1888 // each cell. However it would mean that each renderer should
1889 // respect the given wxRect's top & bottom coords, eventually
1890 // violating only the left & right coords - however the user can
1891 // make its own renderer and thus we cannot be sure of that.
1892 wxDCClipper
clip(dc
, item_rect
);
1894 wxDataViewItemAttr attr
;
1896 hasAttr
= model
->GetAttr(dataitem
, col
->GetModelColumn(), attr
);
1897 cell
->RenderWithAttr(dc
, item_rect
, cell
->CalculateAlignment(),
1898 hasAttr
? &attr
: NULL
, state
);
1901 cell_rect
.x
+= cell_rect
.width
;
1905 void wxDataViewMainWindow::OnRenameTimer()
1907 // We have to call this here because changes may just have
1908 // been made and no screen update taken place.
1911 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1912 // (needs to be tested!)
1916 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1918 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1920 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1923 //-----------------------------------------------------------------------------
1924 // Helper class for do operation on the tree node
1925 //-----------------------------------------------------------------------------
1930 virtual ~DoJob() { }
1932 // The return value control how the tree-walker tranverse the tree
1933 // 0: Job done, stop tranverse and return
1934 // 1: Ignore the current node's subtree and continue
1935 // 2: Job not done, continue
1936 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1937 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1938 virtual int operator() ( void * n
) = 0;
1941 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1946 switch( func( node
) )
1957 wxDataViewTreeNodes nodes
= node
->GetNodes();
1958 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1960 int len_nodes
= nodes
.GetCount();
1961 int len
= leaves
.GetCount();
1962 int i
= 0, nodes_i
= 0;
1964 for(; i
< len
; i
++ )
1966 void * n
= leaves
[i
];
1967 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1969 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1972 if( Walker( nd
, func
) )
1991 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1993 if (IsVirtualList())
1995 wxDataViewVirtualListModel
*list_model
=
1996 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1997 m_count
= list_model
->GetCount();
2004 wxDataViewTreeNode
* node
;
2005 node
= FindNode(parent
);
2010 node
->SetHasChildren( true );
2012 if( g_model
->IsContainer( item
) )
2014 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2015 newnode
->SetItem(item
);
2016 newnode
->SetHasChildren( true );
2017 node
->AddNode( newnode
);
2020 node
->AddLeaf( item
.GetID() );
2022 node
->ChangeSubTreeCount(1);
2030 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2032 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2033 const wxDataViewItem
& item
)
2035 if (IsVirtualList())
2037 wxDataViewVirtualListModel
*list_model
=
2038 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
2039 m_count
= list_model
->GetCount();
2041 if( m_currentRow
> GetRowCount() )
2042 m_currentRow
= m_count
- 1;
2044 // TODO: why empty the entire selection?
2045 m_selection
.Empty();
2052 wxDataViewTreeNode
* node
= FindNode(parent
);
2054 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2055 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
,
2056 false, "item not found" );
2059 node
->GetChildren().Remove( item
.GetID() );
2060 // Manipolate selection
2061 if( m_selection
.GetCount() > 1 )
2063 m_selection
.Empty();
2065 bool isContainer
= false;
2066 wxDataViewTreeNodes nds
= node
->GetNodes();
2067 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2069 if (nds
[i
]->GetItem() == item
)
2077 wxDataViewTreeNode
* n
= NULL
;
2078 wxDataViewTreeNodes nodes
= node
->GetNodes();
2079 int len
= nodes
.GetCount();
2080 for( int i
= 0; i
< len
; i
++)
2082 if( nodes
[i
]->GetItem() == item
)
2089 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2091 node
->GetNodes().Remove( n
);
2092 sub
-= n
->GetSubTreeCount();
2093 ::DestroyTreeHelper(n
);
2095 // Make the row number invalid and get a new valid one when user call GetRowCount
2097 node
->ChangeSubTreeCount(sub
);
2099 // Change the current row to the last row if the current exceed the max row number
2100 if( m_currentRow
> GetRowCount() )
2101 m_currentRow
= m_count
- 1;
2108 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2114 wxWindow
*parent
= GetParent();
2115 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2116 le
.SetEventObject(parent
);
2117 le
.SetModel(GetOwner()->GetModel());
2119 parent
->GetEventHandler()->ProcessEvent(le
);
2124 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2126 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2127 /*#define MAX_VIRTUAL_WIDTH 100000
2129 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2130 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2131 Refresh( true, &rect );
2139 wxWindow
*parent
= GetParent();
2140 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2141 le
.SetEventObject(parent
);
2142 le
.SetModel(GetOwner()->GetModel());
2145 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2146 parent
->GetEventHandler()->ProcessEvent(le
);
2151 bool wxDataViewMainWindow::Cleared()
2156 BuildTree( GetOwner()->GetModel() );
2163 void wxDataViewMainWindow::UpdateDisplay()
2168 void wxDataViewMainWindow::OnInternalIdle()
2170 wxWindow::OnInternalIdle();
2174 RecalculateDisplay();
2179 void wxDataViewMainWindow::RecalculateDisplay()
2181 wxDataViewModel
*model
= GetOwner()->GetModel();
2188 int width
= GetEndOfLastCol();
2189 int height
= GetLineStart( GetRowCount() );
2191 SetVirtualSize( width
, height
);
2192 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2197 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2199 wxWindow::ScrollWindow( dx
, dy
, rect
);
2201 if (GetOwner()->m_headerArea
)
2202 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2205 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2208 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2209 int sy
= GetLineStart( rows
)/y
;
2213 wxRect rect
= GetClientRect();
2217 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2218 for (x_start
= 0; colnum
< column
; colnum
++)
2220 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2221 if (col
->IsHidden())
2222 continue; // skip it!
2224 w
= col
->GetWidth();
2228 int x_end
= x_start
+ w
;
2229 xe
= xx
+ rect
.width
;
2232 sx
= ( xx
+ x_end
- xe
)/x
;
2239 m_owner
->Scroll( sx
, sy
);
2242 int wxDataViewMainWindow::GetCountPerPage() const
2244 wxSize size
= GetClientSize();
2245 return size
.y
/ m_lineHeight
;
2248 int wxDataViewMainWindow::GetEndOfLastCol() const
2252 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2254 const wxDataViewColumn
*c
=
2255 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2258 width
+= c
->GetWidth();
2263 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2267 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2269 return GetLineAt( y
);
2272 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2274 wxSize client_size
= GetClientSize();
2275 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2276 &client_size
.x
, &client_size
.y
);
2278 // we should deal with the pixel here
2279 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2281 return wxMin( GetRowCount()-1, row
);
2284 unsigned int wxDataViewMainWindow::GetRowCount()
2286 if ( m_count
== -1 )
2288 m_count
= RecalculateCount();
2294 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2301 void wxDataViewMainWindow::SelectAllRows( bool on
)
2308 m_selection
.Clear();
2309 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2310 m_selection
.Add( i
);
2315 unsigned int first_visible
= GetFirstVisibleRow();
2316 unsigned int last_visible
= GetLastVisibleRow();
2318 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2320 unsigned int row
= m_selection
[i
];
2321 if ((row
>= first_visible
) && (row
<= last_visible
))
2324 m_selection
.Clear();
2328 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2330 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2334 m_selection
.Add( row
);
2342 m_selection
.Remove( row
);
2348 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2352 unsigned int tmp
= from
;
2358 for (i
= from
; i
<= to
; i
++)
2360 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2363 m_selection
.Add( i
);
2368 m_selection
.Remove( i
);
2371 RefreshRows( from
, to
);
2374 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2376 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2378 int n
= aSelections
[i
];
2380 m_selection
.Add( n
);
2385 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2387 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2388 m_selection
.Add( row
);
2390 m_selection
.Remove( row
);
2394 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2396 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2399 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2401 wxWindow
*parent
= GetParent();
2402 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2404 le
.SetEventObject(parent
);
2405 le
.SetModel(GetOwner()->GetModel());
2408 parent
->GetEventHandler()->ProcessEvent(le
);
2411 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2413 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2414 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2416 wxSize client_size
= GetClientSize();
2417 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2418 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2419 if (intersect_rect
.width
> 0)
2420 Refresh( true, &intersect_rect
);
2423 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2427 unsigned int tmp
= to
;
2432 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2433 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2435 wxSize client_size
= GetClientSize();
2436 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2437 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2438 if (intersect_rect
.width
> 0)
2439 Refresh( true, &intersect_rect
);
2442 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2444 wxSize client_size
= GetClientSize();
2445 int start
= GetLineStart( firstRow
);
2446 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2447 if (start
> client_size
.y
) return;
2449 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2451 Refresh( true, &rect
);
2454 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2456 wxCHECK_RET( newCurrent
< GetRowCount(),
2457 wxT("invalid item index in OnArrowChar()") );
2459 // if there is no selection, we cannot move it anywhere
2460 if (!HasCurrentRow())
2463 unsigned int oldCurrent
= m_currentRow
;
2465 // in single selection we just ignore Shift as we can't select several
2467 if ( event
.ShiftDown() && !IsSingleSel() )
2469 RefreshRow( oldCurrent
);
2471 ChangeCurrentRow( newCurrent
);
2473 // select all the items between the old and the new one
2474 if ( oldCurrent
> newCurrent
)
2476 newCurrent
= oldCurrent
;
2477 oldCurrent
= m_currentRow
;
2480 SelectRows( oldCurrent
, newCurrent
, true );
2481 if (oldCurrent
!=newCurrent
)
2482 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2486 RefreshRow( oldCurrent
);
2488 // all previously selected items are unselected unless ctrl is held
2489 if ( !event
.ControlDown() )
2490 SelectAllRows(false);
2492 ChangeCurrentRow( newCurrent
);
2494 if ( !event
.ControlDown() )
2496 SelectRow( m_currentRow
, true );
2497 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2500 RefreshRow( m_currentRow
);
2503 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2506 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2510 rect
.y
= GetLineStart( row
);
2511 rect
.width
= GetEndOfLastCol();
2512 rect
.height
= GetLineHeight( row
);
2517 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2519 const wxDataViewModel
*model
= GetOwner()->GetModel();
2521 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2523 // TODO make more efficient
2528 for (r
= 0; r
< row
; r
++)
2530 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2531 if (!node
) return start
;
2533 wxDataViewItem item
= node
->GetItem();
2535 if (node
&& !node
->HasChildren())
2537 // Yes, if the node does not have any child, it must be a leaf which
2538 // mean that it is a temporarily created by GetTreeNodeByRow
2542 unsigned int cols
= GetOwner()->GetColumnCount();
2544 int height
= m_lineHeight
;
2545 for (col
= 0; col
< cols
; col
++)
2547 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2548 if (column
->IsHidden())
2549 continue; // skip it!
2552 model
->IsContainer(item
) &&
2553 !model
->HasContainerColumns(item
))
2554 continue; // skip it!
2557 model
->GetValue( value
, item
, column
->GetModelColumn() );
2559 wxDataViewRenderer
*renderer
=
2560 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2561 renderer
->SetValue( value
);
2562 height
= wxMax( height
, renderer
->GetSize().y
);
2572 return row
* m_lineHeight
;
2576 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2578 const wxDataViewModel
*model
= GetOwner()->GetModel();
2580 // check for the easy case first
2581 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2582 return y
/ m_lineHeight
;
2584 // TODO make more efficient
2585 unsigned int row
= 0;
2586 unsigned int yy
= 0;
2589 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2592 // not really correct...
2593 return row
+ ((y
-yy
) / m_lineHeight
);
2596 wxDataViewItem item
= node
->GetItem();
2598 if (node
&& !node
->HasChildren())
2600 // Yes, if the node does not have any child, it must be a leaf which
2601 // mean that it is a temporarily created by GetTreeNodeByRow
2605 unsigned int cols
= GetOwner()->GetColumnCount();
2607 int height
= m_lineHeight
;
2608 for (col
= 0; col
< cols
; col
++)
2610 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2611 if (column
->IsHidden())
2612 continue; // skip it!
2615 model
->IsContainer(item
) &&
2616 !model
->HasContainerColumns(item
))
2617 continue; // skip it!
2620 model
->GetValue( value
, item
, column
->GetModelColumn() );
2622 wxDataViewRenderer
*renderer
=
2623 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2624 renderer
->SetValue( value
);
2625 height
= wxMax( height
, renderer
->GetSize().y
);
2636 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2638 const wxDataViewModel
*model
= GetOwner()->GetModel();
2640 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2642 wxASSERT( !IsVirtualList() );
2644 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2645 // wxASSERT( node );
2646 if (!node
) return m_lineHeight
;
2648 wxDataViewItem item
= node
->GetItem();
2650 if (node
&& !node
->HasChildren())
2652 // Yes, if the node does not have any child, it must be a leaf which
2653 // mean that it is a temporarily created by GetTreeNodeByRow
2657 int height
= m_lineHeight
;
2659 unsigned int cols
= GetOwner()->GetColumnCount();
2661 for (col
= 0; col
< cols
; col
++)
2663 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2664 if (column
->IsHidden())
2665 continue; // skip it!
2668 model
->IsContainer(item
) &&
2669 !model
->HasContainerColumns(item
))
2670 continue; // skip it!
2673 model
->GetValue( value
, item
, column
->GetModelColumn() );
2675 wxDataViewRenderer
*renderer
=
2676 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2677 renderer
->SetValue( value
);
2678 height
= wxMax( height
, renderer
->GetSize().y
);
2685 return m_lineHeight
;
2689 class RowToItemJob
: public DoJob
2692 RowToItemJob( unsigned int row
, int current
)
2693 { this->row
= row
; this->current
= current
; }
2694 virtual ~RowToItemJob() {}
2696 virtual int operator() ( wxDataViewTreeNode
* node
)
2699 if( current
== static_cast<int>(row
))
2701 ret
= node
->GetItem();
2705 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2707 current
+= node
->GetSubTreeCount();
2712 // If the current has no child node, we can find the desired item of the row
2714 // This if can speed up finding in some case, and will has a very good effect
2715 // when it comes to list view
2716 if( node
->GetNodes().GetCount() == 0)
2718 int index
= static_cast<int>(row
) - current
- 1;
2719 ret
= node
->GetChildren().Item( index
);
2726 virtual int operator() ( void * n
)
2729 if( current
== static_cast<int>(row
))
2731 ret
= wxDataViewItem( n
);
2737 wxDataViewItem
GetResult() const
2746 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2748 if (IsVirtualList())
2750 return wxDataViewItem( wxUIntToPtr(row
+1) );
2754 RowToItemJob
job( row
, -2 );
2755 Walker( m_root
, job
);
2756 return job
.GetResult();
2760 class RowToTreeNodeJob
: public DoJob
2763 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2766 this->current
= current
;
2770 virtual ~RowToTreeNodeJob(){ }
2772 virtual int operator() ( wxDataViewTreeNode
* node
)
2775 if( current
== static_cast<int>(row
))
2781 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2783 current
+= node
->GetSubTreeCount();
2790 // If the current node has no children, we can find the desired item of the
2791 // row number directly.
2792 // This if can speed up finding in some case, and will have a very good
2793 // effect for list views.
2794 if( node
->GetNodes().GetCount() == 0)
2796 int index
= static_cast<int>(row
) - current
- 1;
2797 void * n
= node
->GetChildren().Item( index
);
2798 ret
= new wxDataViewTreeNode( parent
);
2799 ret
->SetItem( wxDataViewItem( n
));
2800 ret
->SetHasChildren(false);
2807 virtual int operator() ( void * n
)
2810 if( current
== static_cast<int>(row
))
2812 ret
= new wxDataViewTreeNode( parent
);
2813 ret
->SetItem( wxDataViewItem( n
));
2814 ret
->SetHasChildren(false);
2821 wxDataViewTreeNode
* GetResult() const
2827 wxDataViewTreeNode
* ret
;
2828 wxDataViewTreeNode
* parent
;
2831 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2833 wxASSERT( !IsVirtualList() );
2835 RowToTreeNodeJob
job( row
, -2, m_root
);
2836 Walker( m_root
, job
);
2837 return job
.GetResult();
2840 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2841 const wxDataViewItem
& item
)
2843 wxWindow
*parent
= GetParent();
2844 wxDataViewEvent
le(type
, parent
->GetId());
2846 le
.SetEventObject(parent
);
2847 le
.SetModel(GetOwner()->GetModel());
2850 parent
->GetEventHandler()->ProcessEvent(le
);
2854 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2856 if (IsVirtualList())
2859 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2863 if (!node
->HasChildren())
2869 return node
->IsOpen();
2872 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2874 if (IsVirtualList())
2877 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2881 if (!node
->HasChildren())
2890 void wxDataViewMainWindow::Expand( unsigned int row
)
2892 if (IsVirtualList())
2895 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2899 if (!node
->HasChildren())
2905 if (!node
->IsOpen())
2908 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2910 // Check if the user prevent expanding
2911 if( e
.GetSkipped() )
2916 // build the children of current node
2917 if( node
->GetChildrenNumber() == 0 )
2920 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2923 // By expanding the node all row indices that are currently in the selection list
2924 // and are greater than our node have become invalid. So we have to correct that now.
2925 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2926 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2928 const unsigned testRow
= m_selection
[i
];
2929 // all rows above us are not affected, so skip them
2933 m_selection
[i
] += rowAdjustment
;
2936 if(m_currentRow
> row
)
2937 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2941 // Send the expanded event
2942 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2946 void wxDataViewMainWindow::Collapse(unsigned int row
)
2948 if (IsVirtualList())
2951 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2955 if (!node
->HasChildren())
2964 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2965 if( e
.GetSkipped() )
2968 // Find out if there are selected items below the current node.
2969 bool selectCollapsingRow
= false;
2970 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2971 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2972 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2974 const unsigned testRow
= m_selection
[i
];
2975 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2977 selectCollapsingRow
= true;
2978 // get out as soon as we have found a node that is selected
2985 // If the node to be closed has selected items the user won't see those any longer.
2986 // We select the collapsing node in this case.
2987 if(selectCollapsingRow
)
2989 SelectAllRows(false);
2990 ChangeCurrentRow(row
);
2991 SelectRow(row
, true);
2992 SendSelectionChangedEvent(GetItemByRow(row
));
2996 // if there were no selected items below our node we still need to "fix" the
2997 // selection list to adjust for the changing of the row indices.
2998 // We actually do the opposite of what we are doing in Expand().
2999 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3001 const unsigned testRow
= m_selection
[i
];
3002 // all rows above us are not affected, so skip them
3006 m_selection
[i
] -= rowAdjustment
;
3009 // if the "current row" is being collapsed away we change it to the current row ;-)
3010 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3011 ChangeCurrentRow(row
);
3012 else if(m_currentRow
> row
)
3013 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3018 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3022 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3024 wxDataViewModel
* model
= GetOwner()->GetModel();
3031 // Compose the a parent-chain of the finding item
3033 list
.DeleteContents( true );
3034 wxDataViewItem
it( item
);
3037 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3038 list
.Insert( pItem
);
3039 it
= model
->GetParent( it
);
3042 // Find the item along the parent-chain.
3043 // This algorithm is designed to speed up the node-finding method
3044 wxDataViewTreeNode
* node
= m_root
;
3045 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
3047 if( node
->HasChildren() )
3049 if( node
->GetChildrenNumber() == 0 )
3052 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3055 wxDataViewTreeNodes nodes
= node
->GetNodes();
3059 for (i
= 0; i
< nodes
.GetCount(); i
++)
3061 if (nodes
[i
]->GetItem() == (**iter
))
3063 if (nodes
[i
]->GetItem() == item
)
3080 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3081 wxDataViewColumn
* &column
)
3083 wxDataViewColumn
*col
= NULL
;
3084 unsigned int cols
= GetOwner()->GetColumnCount();
3085 unsigned int colnum
= 0;
3087 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3088 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3090 col
= GetOwner()->GetColumnAt(colnum
);
3091 if (col
->IsHidden())
3092 continue; // skip it!
3094 unsigned int w
= col
->GetWidth();
3095 if (x_start
+w
>= (unsigned int)x
)
3102 item
= GetItemByRow( GetLineAt( y
) );
3105 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3106 const wxDataViewColumn
* column
)
3111 unsigned int cols
= GetOwner()->GetColumnCount();
3112 // If column is null the loop will compute the combined width of all columns.
3113 // Otherwise, it will compute the x position of the column we are looking for.
3114 for (unsigned int i
= 0; i
< cols
; i
++)
3116 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3121 if (col
->IsHidden())
3122 continue; // skip it!
3124 xpos
+= col
->GetWidth();
3125 width
+= col
->GetWidth();
3130 // If we have a column, we need can get its width directly.
3131 if(column
->IsHidden())
3134 width
= column
->GetWidth();
3139 // If we have no column, we reset the x position back to zero.
3143 // we have to take an expander column into account and compute its indentation
3144 // to get the correct x position where the actual text is
3146 int row
= GetRowByItem(item
);
3147 if (!IsVirtualList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3149 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3150 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3151 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3153 if(!node
->HasChildren())
3157 wxRect
itemRect( xpos
+ indent
,
3158 GetLineStart( row
),
3160 GetLineHeight( row
) );
3162 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3163 &itemRect
.x
, &itemRect
.y
);
3168 int wxDataViewMainWindow::RecalculateCount()
3170 if (IsVirtualList())
3172 wxDataViewVirtualListModel
*list_model
=
3173 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3175 return list_model
->GetCount();
3179 return m_root
->GetSubTreeCount();
3183 class ItemToRowJob
: public DoJob
3186 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3193 // Maybe binary search will help to speed up this process
3194 virtual int operator() ( wxDataViewTreeNode
* node
)
3197 if( node
->GetItem() == item
)
3202 if( node
->GetItem() == **m_iter
)
3209 ret
+= node
->GetSubTreeCount();
3215 virtual int operator() ( void * n
)
3218 if( n
== item
.GetID() )
3223 // the row number is begin from zero
3224 int GetResult() const
3228 ItemList::const_iterator m_iter
;
3229 wxDataViewItem item
;
3234 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3236 const wxDataViewModel
* model
= GetOwner()->GetModel();
3240 if (IsVirtualList())
3242 return wxPtrToUInt( item
.GetID() ) -1;
3249 // Compose the a parent-chain of the finding item
3251 wxDataViewItem
* pItem
;
3252 list
.DeleteContents( true );
3253 wxDataViewItem
it( item
);
3256 pItem
= new wxDataViewItem( it
);
3257 list
.Insert( pItem
);
3258 it
= model
->GetParent( it
);
3260 pItem
= new wxDataViewItem( );
3261 list
.Insert( pItem
);
3263 ItemToRowJob
job( item
, list
.begin() );
3264 Walker(m_root
, job
);
3265 return job
.GetResult();
3269 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
3270 wxDataViewTreeNode
* node
)
3272 if( !model
->IsContainer( item
) )
3275 wxDataViewItemArray children
;
3276 unsigned int num
= model
->GetChildren( item
, children
);
3278 unsigned int index
= 0;
3279 while( index
< num
)
3281 if( model
->IsContainer( children
[index
] ) )
3283 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3284 n
->SetItem(children
[index
]);
3285 n
->SetHasChildren( true );
3290 node
->AddLeaf( children
[index
].GetID() );
3294 node
->SetSubTreeCount( num
);
3295 wxDataViewTreeNode
* n
= node
->GetParent();
3297 n
->ChangeSubTreeCount(num
);
3301 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3305 if (GetOwner()->GetModel()->IsVirtualListModel())
3311 m_root
= new wxDataViewTreeNode( NULL
);
3312 m_root
->SetHasChildren(true);
3314 // First we define a invalid item to fetch the top-level elements
3315 wxDataViewItem item
;
3317 BuildTreeHelper( model
, item
, m_root
);
3321 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3323 if( node
->GetNodeNumber() != 0 )
3325 int len
= node
->GetNodeNumber();
3326 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3327 for (int i
= 0; i
< len
; i
++)
3328 DestroyTreeHelper(nodes
[i
]);
3333 void wxDataViewMainWindow::DestroyTree()
3335 if (!IsVirtualList())
3337 ::DestroyTreeHelper(m_root
);
3343 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3345 if ( GetParent()->HandleAsNavigationKey(event
) )
3348 // no item -> nothing to do
3349 if (!HasCurrentRow())
3355 // don't use m_linesPerPage directly as it might not be computed yet
3356 const int pageSize
= GetCountPerPage();
3357 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3359 switch ( event
.GetKeyCode() )
3363 wxWindow
*parent
= GetParent();
3364 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3366 le
.SetItem( GetItemByRow(m_currentRow
) );
3367 le
.SetEventObject(parent
);
3368 le
.SetModel(GetOwner()->GetModel());
3370 parent
->GetEventHandler()->ProcessEvent(le
);
3375 if ( m_currentRow
> 0 )
3376 OnArrowChar( m_currentRow
- 1, event
);
3380 if ( m_currentRow
< GetRowCount() - 1 )
3381 OnArrowChar( m_currentRow
+ 1, event
);
3383 // Add the process for tree expanding/collapsing
3386 if (IsVirtualList())
3389 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3393 if (node
->HasChildren() && node
->IsOpen())
3395 Collapse(m_currentRow
);
3397 else // if the node is already closed we move the selection to its parent
3399 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3401 if(!node
->HasChildren())
3406 int parent
= GetRowByItem( parent_node
->GetItem() );
3409 unsigned int row
= m_currentRow
;
3410 SelectRow( row
, false);
3411 SelectRow( parent
, true );
3412 ChangeCurrentRow( parent
);
3413 GetOwner()->EnsureVisible( parent
, -1 );
3414 SendSelectionChangedEvent( parent_node
->GetItem() );
3422 if (!IsExpanded( m_currentRow
))
3423 Expand( m_currentRow
);
3426 unsigned int row
= m_currentRow
;
3427 SelectRow( row
, false );
3428 SelectRow( row
+ 1, true );
3429 ChangeCurrentRow( row
+ 1 );
3430 GetOwner()->EnsureVisible( row
+ 1, -1 );
3431 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3438 OnArrowChar( GetRowCount() - 1, event
);
3443 OnArrowChar( 0, event
);
3448 int steps
= pageSize
- 1;
3449 int index
= m_currentRow
- steps
;
3453 OnArrowChar( index
, event
);
3459 int steps
= pageSize
- 1;
3460 unsigned int index
= m_currentRow
+ steps
;
3461 unsigned int count
= GetRowCount();
3462 if ( index
>= count
)
3465 OnArrowChar( index
, event
);
3471 if(m_selection
.size() == 1)
3473 // TODO: we need to revise that when we have a concept for a 'current column'
3474 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3484 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3486 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3488 // let the base handle mouse wheel events.
3493 int x
= event
.GetX();
3494 int y
= event
.GetY();
3495 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3496 wxDataViewColumn
*col
= NULL
;
3499 unsigned int cols
= GetOwner()->GetColumnCount();
3501 for (i
= 0; i
< cols
; i
++)
3503 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3505 continue; // skip it!
3507 if (x
< xpos
+ c
->GetWidth())
3512 xpos
+= c
->GetWidth();
3517 wxDataViewRenderer
*cell
= col
->GetRenderer();
3518 unsigned int current
= GetLineAt( y
);
3519 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3521 // Unselect all if below the last row ?
3525 // Test whether the mouse is hovered on the tree item button
3526 bool hoverOverExpander
= false;
3527 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3529 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3530 if( node
!=NULL
&& node
->HasChildren() )
3532 int indent
= node
->GetIndentLevel();
3533 indent
= GetOwner()->GetIndent()*indent
;
3535 // we make the rectangle we are looking in a bit bigger than the actual
3536 // visual expander so the user can hit that little thing reliably
3537 wxRect
rect( xpos
+ indent
,
3538 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3539 m_lineHeight
, m_lineHeight
);
3540 if( rect
.Contains(x
, y
) )
3542 // So the mouse is over the expander
3543 hoverOverExpander
= true;
3544 if (m_underMouse
&& m_underMouse
!= node
)
3546 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3547 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3549 if (m_underMouse
!= node
)
3551 // wxLogMessage("Do the row: %d", current);
3552 RefreshRow(current
);
3554 m_underMouse
= node
;
3557 if (node
!=NULL
&& !node
->HasChildren())
3560 if (!hoverOverExpander
)
3562 if (m_underMouse
!= NULL
)
3564 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3565 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3566 m_underMouse
= NULL
;
3570 wxDataViewModel
*model
= GetOwner()->GetModel();
3572 #if wxUSE_DRAG_AND_DROP
3573 if (event
.Dragging())
3575 if (m_dragCount
== 0)
3577 // we have to report the raw, physical coords as we want to be
3578 // able to call HitTest(event.m_pointDrag) from the user code to
3579 // get the item being dragged
3580 m_dragStart
= event
.GetPosition();
3585 if (m_dragCount
!= 3)
3588 if (event
.LeftIsDown())
3590 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3591 &m_dragStart
.x
, &m_dragStart
.y
);
3592 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3593 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3595 // Notify cell about drag
3596 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3597 event
.SetEventObject( m_owner
);
3598 event
.SetItem( item
);
3599 event
.SetModel( model
);
3600 if (!m_owner
->HandleWindowEvent( event
))
3603 if (!event
.IsAllowed())
3606 wxDataObject
*obj
= event
.GetDataObject();
3610 wxDataViewDropSource
drag( this, drag_item_row
);
3611 drag
.SetData( *obj
);
3612 /* wxDragResult res = */ drag
.DoDragDrop();
3621 #endif // wxUSE_DRAG_AND_DROP
3623 bool simulateClick
= false;
3625 if (event
.ButtonDClick())
3627 m_renameTimer
->Stop();
3628 m_lastOnSame
= false;
3631 wxDataViewItem item
= GetItemByRow(current
);
3632 bool ignore_other_columns
=
3633 ((GetOwner()->GetExpanderColumn() != col
) &&
3634 (model
->IsContainer(item
)) &&
3635 (!model
->HasContainerColumns(item
)));
3637 if (event
.LeftDClick())
3639 if(hoverOverExpander
)
3641 // a double click on the expander will be converted into a "simulated" normal click
3642 simulateClick
= true;
3644 else if ( current
== m_lineLastClicked
)
3646 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3649 model
->GetValue( value
, item
, col
->GetModelColumn() );
3650 cell
->SetValue( value
);
3651 wxRect
cell_rect( xpos
, GetLineStart( current
),
3652 col
->GetWidth(), GetLineHeight( current
) );
3653 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3657 wxWindow
*parent
= GetParent();
3658 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3660 le
.SetEventObject(parent
);
3661 le
.SetModel(GetOwner()->GetModel());
3663 parent
->GetEventHandler()->ProcessEvent(le
);
3669 // The first click was on another item, so don't interpret this as
3670 // a double click, but as a simple click instead
3671 simulateClick
= true;
3675 if (event
.LeftUp() && !hoverOverExpander
)
3677 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3679 // select single line
3680 SelectAllRows( false );
3681 SelectRow( m_lineSelectSingleOnUp
, true );
3682 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3685 // If the user click the expander, we do not do editing even if the column
3686 // with expander are editable
3687 if (m_lastOnSame
&& !ignore_other_columns
)
3689 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3690 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3692 m_renameTimer
->Start( 100, true );
3696 m_lastOnSame
= false;
3697 m_lineSelectSingleOnUp
= (unsigned int)-1;
3699 else if(!event
.LeftUp())
3701 // This is necessary, because after a DnD operation in
3702 // from and to ourself, the up event is swallowed by the
3703 // DnD code. So on next non-up event (which means here and
3704 // now) m_lineSelectSingleOnUp should be reset.
3705 m_lineSelectSingleOnUp
= (unsigned int)-1;
3708 if (event
.RightDown())
3710 m_lineBeforeLastClicked
= m_lineLastClicked
;
3711 m_lineLastClicked
= current
;
3713 // If the item is already selected, do not update the selection.
3714 // Multi-selections should not be cleared if a selected item is clicked.
3715 if (!IsRowSelected(current
))
3717 SelectAllRows(false);
3718 ChangeCurrentRow(current
);
3719 SelectRow(m_currentRow
,true);
3720 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3723 else if (event
.RightUp())
3726 model
->GetValue( value
, item
, col
->GetModelColumn() );
3727 wxWindow
*parent
= GetParent();
3728 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3730 le
.SetEventObject(parent
);
3731 le
.SetModel(GetOwner()->GetModel());
3733 parent
->GetEventHandler()->ProcessEvent(le
);
3735 else if (event
.MiddleDown())
3739 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3741 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3743 // hoverOverExpander being true tells us that our node must be
3744 // valid and have children.
3745 // So we don't need any extra checks.
3746 if( node
->IsOpen() )
3751 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3755 m_lineBeforeLastClicked
= m_lineLastClicked
;
3756 m_lineLastClicked
= current
;
3758 unsigned int oldCurrentRow
= m_currentRow
;
3759 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3761 bool cmdModifierDown
= event
.CmdDown();
3762 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3764 if ( IsSingleSel() || !IsRowSelected(current
) )
3766 SelectAllRows( false );
3767 ChangeCurrentRow(current
);
3768 SelectRow(m_currentRow
,true);
3769 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3771 else // multi sel & current is highlighted & no mod keys
3773 m_lineSelectSingleOnUp
= current
;
3774 ChangeCurrentRow(current
); // change focus
3777 else // multi sel & either ctrl or shift is down
3779 if (cmdModifierDown
)
3781 ChangeCurrentRow(current
);
3782 ReverseRowSelection(m_currentRow
);
3783 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3785 else if (event
.ShiftDown())
3787 ChangeCurrentRow(current
);
3789 unsigned int lineFrom
= oldCurrentRow
,
3792 if ( lineTo
< lineFrom
)
3795 lineFrom
= m_currentRow
;
3798 SelectRows(lineFrom
, lineTo
, true);
3799 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3801 else // !ctrl, !shift
3803 // test in the enclosing if should make it impossible
3804 wxFAIL_MSG( wxT("how did we get here?") );
3808 if (m_currentRow
!= oldCurrentRow
)
3809 RefreshRow( oldCurrentRow
);
3811 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3813 // Update selection here...
3816 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3817 (current
== oldCurrentRow
)) && oldWasSelected
;
3819 // Call LeftClick after everything else as under GTK+
3820 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3822 // notify cell about right click
3824 model
->GetValue( value
, item
, col
->GetModelColumn() );
3825 cell
->SetValue( value
);
3826 wxRect
cell_rect( xpos
, GetLineStart( current
),
3827 col
->GetWidth(), GetLineHeight( current
) );
3828 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
,
3829 model
, item
, col
->GetModelColumn());
3834 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3838 if (HasCurrentRow())
3844 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3848 if (HasCurrentRow())
3854 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3856 if( m_selection
.GetCount() != 1 )
3857 return wxDataViewItem();
3859 return GetItemByRow( m_selection
.Item(0));
3862 //-----------------------------------------------------------------------------
3864 //-----------------------------------------------------------------------------
3866 WX_DEFINE_LIST(wxDataViewColumnList
)
3868 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3869 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3870 EVT_SIZE(wxDataViewCtrl::OnSize
)
3873 wxDataViewCtrl::~wxDataViewCtrl()
3876 GetModel()->RemoveNotifier( m_notifier
);
3881 void wxDataViewCtrl::Init()
3883 m_cols
.DeleteContents(true);
3886 // No sorting column at start
3887 m_sortingColumnIdx
= wxNOT_FOUND
;
3889 m_headerArea
= NULL
;
3892 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3893 const wxPoint
& pos
, const wxSize
& size
,
3894 long style
, const wxValidator
& validator
)
3896 // if ( (style & wxBORDER_MASK) == 0)
3897 // style |= wxBORDER_SUNKEN;
3901 if (!wxControl::Create( parent
, id
, pos
, size
,
3902 style
| wxScrolledWindowStyle
, validator
))
3905 SetInitialSize(size
);
3908 MacSetClipChildren( true );
3911 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3913 if (HasFlag(wxDV_NO_HEADER
))
3914 m_headerArea
= NULL
;
3916 m_headerArea
= new wxDataViewHeaderWindow(this);
3918 SetTargetWindow( m_clientArea
);
3920 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3922 sizer
->Add( m_headerArea
, 0, wxGROW
);
3923 sizer
->Add( m_clientArea
, 1, wxGROW
);
3929 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3931 return wxBORDER_THEME
;
3935 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3939 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3942 // we need to process arrows ourselves for scrolling
3943 if ( nMsg
== WM_GETDLGCODE
)
3945 rc
|= DLGC_WANTARROWS
;
3953 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3955 wxSize newsize
= size
;
3956 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3957 newsize
.y
-= m_headerArea
->GetSize().y
;
3962 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3964 // We need to override OnSize so that our scrolled
3965 // window a) does call Layout() to use sizers for
3966 // positioning the controls but b) does not query
3967 // the sizer for their size and use that for setting
3968 // the scrollable area as set that ourselves by
3969 // calling SetScrollbar() further down.
3976 void wxDataViewCtrl::SetFocus()
3979 m_clientArea
->SetFocus();
3982 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3984 if (!wxDataViewCtrlBase::AssociateModel( model
))
3987 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3989 model
->AddNotifier( m_notifier
);
3991 m_clientArea
->DestroyTree();
3993 m_clientArea
->BuildTree(model
);
3995 m_clientArea
->UpdateDisplay();
4000 #if wxUSE_DRAG_AND_DROP
4002 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4004 return m_clientArea
->EnableDragSource( format
);
4007 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4009 return m_clientArea
->EnableDropTarget( format
);
4012 #endif // wxUSE_DRAG_AND_DROP
4014 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4016 if (!wxDataViewCtrlBase::AppendColumn(col
))
4019 m_cols
.Append( col
);
4020 OnColumnsCountChanged();
4024 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4026 if (!wxDataViewCtrlBase::PrependColumn(col
))
4029 m_cols
.Insert( col
);
4030 OnColumnsCountChanged();
4034 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4036 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4039 m_cols
.Insert( pos
, col
);
4040 OnColumnsCountChanged();
4044 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4047 m_headerArea
->UpdateColumn(idx
);
4049 m_clientArea
->UpdateDisplay();
4052 void wxDataViewCtrl::OnColumnsCountChanged()
4055 m_headerArea
->SetColumnCount(GetColumnCount());
4057 m_clientArea
->UpdateDisplay();
4060 void wxDataViewCtrl::DoSetExpanderColumn()
4062 m_clientArea
->UpdateDisplay();
4065 void wxDataViewCtrl::DoSetIndent()
4067 m_clientArea
->UpdateDisplay();
4070 unsigned int wxDataViewCtrl::GetColumnCount() const
4072 return m_cols
.GetCount();
4075 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4080 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4082 // columns can't be reordered if there is no header window which allows
4084 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4087 return GetColumn(idx
);
4090 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4092 const unsigned count
= m_cols
.size();
4093 for ( unsigned n
= 0; n
< count
; n
++ )
4095 if ( m_cols
[n
] == column
)
4102 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4103 unsigned int WXUNUSED(new_pos
))
4105 // do _not_ reorder m_cols elements here, they should always be in the
4106 // order in which columns were added, we only display the columns in
4108 m_clientArea
->UpdateDisplay();
4111 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4113 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4118 OnColumnsCountChanged();
4123 bool wxDataViewCtrl::ClearColumns()
4126 OnColumnsCountChanged();
4130 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4134 unsigned int len
= GetColumnCount();
4135 for ( unsigned int i
= 0; i
< len
; i
++ )
4137 wxDataViewColumn
* col
= GetColumnAt(i
);
4138 if (col
->IsHidden())
4140 ret
+= col
->GetWidth();
4143 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4150 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4152 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4153 : GetColumn(m_sortingColumnIdx
);
4156 // Selection code with wxDataViewItem as parameters
4157 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4159 return m_clientArea
->GetSelection();
4162 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4165 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4166 int len
= selection
.GetCount();
4167 for( int i
= 0; i
< len
; i
++)
4169 unsigned int row
= selection
[i
];
4170 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4175 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4177 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4179 wxDataViewItem last_parent
;
4181 int len
= sel
.GetCount();
4182 for( int i
= 0; i
< len
; i
++ )
4184 wxDataViewItem item
= sel
[i
];
4185 wxDataViewItem parent
= GetModel()->GetParent( item
);
4188 if (parent
!= last_parent
)
4189 ExpandAncestors(item
);
4192 last_parent
= parent
;
4193 int row
= m_clientArea
->GetRowByItem( item
);
4195 selection
.Add( static_cast<unsigned int>(row
) );
4198 m_clientArea
->SetSelections( selection
);
4201 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4203 ExpandAncestors( item
);
4205 int row
= m_clientArea
->GetRowByItem( item
);
4208 // Unselect all rows before select another in the single select mode
4209 if (m_clientArea
->IsSingleSel())
4210 m_clientArea
->SelectAllRows(false);
4211 m_clientArea
->SelectRow(row
, true);
4215 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4217 int row
= m_clientArea
->GetRowByItem( item
);
4219 m_clientArea
->SelectRow(row
, false);
4222 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4224 int row
= m_clientArea
->GetRowByItem( item
);
4227 return m_clientArea
->IsRowSelected(row
);
4232 // Selection code with row number as parameter
4233 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4236 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4237 int len
= selection
.GetCount();
4238 for( int i
= 0; i
< len
; i
++)
4240 unsigned int row
= selection
[i
];
4246 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4248 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4249 int len
= sel
.GetCount();
4250 for( int i
= 0; i
< len
; i
++ )
4254 selection
.Add( static_cast<unsigned int>(row
) );
4256 m_clientArea
->SetSelections( selection
);
4259 void wxDataViewCtrl::Select( int row
)
4263 if (m_clientArea
->IsSingleSel())
4264 m_clientArea
->SelectAllRows(false);
4265 m_clientArea
->SelectRow( row
, true );
4269 void wxDataViewCtrl::Unselect( int row
)
4272 m_clientArea
->SelectRow(row
, false);
4275 bool wxDataViewCtrl::IsSelected( int row
) const
4278 return m_clientArea
->IsRowSelected(row
);
4282 void wxDataViewCtrl::SelectRange( int from
, int to
)
4285 for( int i
= from
; i
< to
; i
++ )
4287 m_clientArea
->Select(sel
);
4290 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4292 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4293 for( int i
= from
; i
< to
; i
++ )
4294 if( sel
.Index( i
) != wxNOT_FOUND
)
4296 m_clientArea
->SetSelections(sel
);
4299 void wxDataViewCtrl::SelectAll()
4301 m_clientArea
->SelectAllRows(true);
4304 void wxDataViewCtrl::UnselectAll()
4306 m_clientArea
->SelectAllRows(false);
4309 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4313 if( row
> (int) m_clientArea
->GetRowCount() )
4314 row
= m_clientArea
->GetRowCount();
4316 int first
= m_clientArea
->GetFirstVisibleRow();
4317 int last
= m_clientArea
->GetLastVisibleRow();
4319 m_clientArea
->ScrollTo( row
, column
);
4320 else if( row
> last
)
4321 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4323 m_clientArea
->ScrollTo( first
, column
);
4326 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4328 ExpandAncestors( item
);
4330 m_clientArea
->RecalculateDisplay();
4332 int row
= m_clientArea
->GetRowByItem(item
);
4335 if( column
== NULL
)
4336 EnsureVisible(row
, -1);
4338 EnsureVisible( row
, GetColumnIndex(column
) );
4343 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4344 wxDataViewColumn
* &column
) const
4346 m_clientArea
->HitTest(point
, item
, column
);
4349 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4350 const wxDataViewColumn
* column
) const
4352 return m_clientArea
->GetItemRect(item
, column
);
4355 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4357 return m_clientArea
->GetItemByRow( row
);
4360 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4362 return m_clientArea
->GetRowByItem( item
);
4365 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4367 int row
= m_clientArea
->GetRowByItem( item
);
4369 m_clientArea
->Expand(row
);
4372 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4374 int row
= m_clientArea
->GetRowByItem( item
);
4376 m_clientArea
->Collapse(row
);
4379 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4381 int row
= m_clientArea
->GetRowByItem( item
);
4383 return m_clientArea
->IsExpanded(row
);
4387 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4389 wxDataViewColumn
* col
= GetColumn( column
);
4393 wxRect itemRect
= GetItemRect(item
, col
);
4394 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4395 renderer
->StartEditing(item
, itemRect
);
4398 #endif // !wxUSE_GENERICDATAVIEWCTRL
4400 #endif // wxUSE_DATAVIEWCTRL