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 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
945 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
947 m_value
= (long) value
;
949 if (m_value
< 0) m_value
= 0;
950 if (m_value
> 100) m_value
= 100;
955 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
957 value
= (long) m_value
;
961 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
963 double pct
= (double)m_value
/ 100.0;
965 bar
.width
= (int)(cell
.width
* pct
);
966 dc
->SetPen( *wxTRANSPARENT_PEN
);
967 dc
->SetBrush( *wxBLUE_BRUSH
);
968 dc
->DrawRectangle( bar
);
970 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
971 dc
->SetPen( *wxBLACK_PEN
);
972 dc
->DrawRectangle( cell
);
977 wxSize
wxDataViewProgressRenderer::GetSize() const
979 return wxSize(40,12);
982 // ---------------------------------------------------------
983 // wxDataViewDateRenderer
984 // ---------------------------------------------------------
986 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
988 #if wxUSE_DATE_RENDERER_POPUP
990 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
993 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
994 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
995 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
1000 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
1001 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1002 sizer
->Add( m_cal
, 1, wxGROW
);
1007 void OnCalendar( wxCalendarEvent
&event
);
1009 wxCalendarCtrl
*m_cal
;
1010 wxDataViewModel
*m_model
;
1012 const wxDataViewItem
& m_item
;
1015 virtual void OnDismiss()
1020 DECLARE_EVENT_TABLE()
1023 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1024 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1027 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1029 wxDateTime date
= event
.GetDate();
1030 wxVariant value
= date
;
1031 m_model
->SetValue( value
, m_item
, m_col
);
1032 m_model
->ValueChanged( m_item
, m_col
);
1036 #endif // wxUSE_DATE_RENDERER_POPUP
1038 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1040 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1041 wxDataViewCellMode mode
, int align
) :
1042 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1046 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1048 m_date
= value
.GetDateTime();
1053 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1059 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1061 wxString tmp
= m_date
.FormatDate();
1062 RenderText( tmp
, 0, cell
, dc
, state
);
1066 wxSize
wxDataViewDateRenderer::GetSize() const
1068 const wxDataViewCtrl
* view
= GetView();
1069 wxString tmp
= m_date
.FormatDate();
1071 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1072 return wxSize(x
,y
+d
);
1075 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1076 const wxDataViewItem
& item
, unsigned int col
)
1079 model
->GetValue( variant
, item
, col
);
1080 wxDateTime value
= variant
.GetDateTime();
1082 #if wxUSE_DATE_RENDERER_POPUP
1083 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1084 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1085 wxPoint pos
= wxGetMousePosition();
1088 popup
->Popup( popup
->m_cal
);
1089 #else // !wxUSE_DATE_RENDERER_POPUP
1090 wxMessageBox(value
.Format());
1091 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1095 // ---------------------------------------------------------
1096 // wxDataViewIconTextRenderer
1097 // ---------------------------------------------------------
1099 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1101 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1102 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1103 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1106 SetAlignment(align
);
1109 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1113 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1119 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1124 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1127 const wxIcon
&icon
= m_value
.GetIcon();
1130 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1131 xoffset
= icon
.GetWidth()+4;
1134 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1139 wxSize
wxDataViewIconTextRenderer::GetSize() const
1141 const wxDataViewCtrl
*view
= GetView();
1142 if (!m_value
.GetText().empty())
1145 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1147 if (m_value
.GetIcon().IsOk())
1148 x
+= m_value
.GetIcon().GetWidth() + 4;
1149 return wxSize( x
, y
);
1151 return wxSize(80,20);
1154 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1156 wxDataViewIconText iconText
;
1159 wxString text
= iconText
.GetText();
1161 // adjust the label rect to take the width of the icon into account
1162 if (iconText
.GetIcon().IsOk())
1164 int w
= iconText
.GetIcon().GetWidth() + 4;
1166 labelRect
.width
-= w
;
1169 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1170 wxPoint(labelRect
.x
,labelRect
.y
),
1171 wxSize(labelRect
.width
,labelRect
.height
) );
1173 // select the text in the control an place the cursor at the end
1174 ctrl
->SetInsertionPointEnd();
1180 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
& value
)
1182 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1184 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1189 //-----------------------------------------------------------------------------
1190 // wxDataViewDropTarget
1191 //-----------------------------------------------------------------------------
1193 #if wxUSE_DRAG_AND_DROP
1195 class wxBitmapCanvas
: public wxWindow
1198 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1199 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1202 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1205 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1208 dc
.DrawBitmap( m_bitmap
, 0, 0);
1214 class wxDataViewDropSource
: public wxDropSource
1217 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1225 ~wxDataViewDropSource()
1230 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1232 wxPoint pos
= wxGetMousePosition();
1236 int liney
= m_win
->GetLineStart( m_row
);
1238 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1239 m_win
->ClientToScreen( &linex
, &liney
);
1240 m_dist_x
= pos
.x
- linex
;
1241 m_dist_y
= pos
.y
- liney
;
1244 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1246 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1247 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1249 wxFRAME_TOOL_WINDOW
|
1250 wxFRAME_FLOAT_ON_PARENT
|
1251 wxFRAME_NO_TASKBAR
|
1253 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1258 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1259 m_hint
->SetTransparent( 128 );
1265 wxDataViewMainWindow
*m_win
;
1268 int m_dist_x
,m_dist_y
;
1272 class wxDataViewDropTarget
: public wxDropTarget
1275 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1281 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1283 wxDataFormat format
= GetMatchingPair();
1284 if (format
== wxDF_INVALID
)
1286 return m_win
->OnDragOver( format
, x
, y
, def
);
1289 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1291 wxDataFormat format
= GetMatchingPair();
1292 if (format
== wxDF_INVALID
)
1294 return m_win
->OnDrop( format
, x
, y
);
1297 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1299 wxDataFormat format
= GetMatchingPair();
1300 if (format
== wxDF_INVALID
)
1304 return m_win
->OnData( format
, x
, y
, def
);
1307 virtual void OnLeave()
1308 { m_win
->OnLeave(); }
1310 wxDataViewMainWindow
*m_win
;
1313 #endif // wxUSE_DRAG_AND_DROP
1315 //-----------------------------------------------------------------------------
1316 // wxDataViewRenameTimer
1317 //-----------------------------------------------------------------------------
1319 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1324 void wxDataViewRenameTimer::Notify()
1326 m_owner
->OnRenameTimer();
1329 //-----------------------------------------------------------------------------
1330 // wxDataViewMainWindow
1331 //-----------------------------------------------------------------------------
1333 // The tree building helper, declared firstly
1334 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
1335 wxDataViewTreeNode
* node
);
1337 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1339 if (row1
> row2
) return 1;
1340 if (row1
== row2
) return 0;
1344 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1346 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1347 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1348 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1349 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1350 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1351 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1354 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1355 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1356 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1357 m_selection( wxDataViewSelectionCmp
)
1362 m_lastOnSame
= false;
1363 m_renameTimer
= new wxDataViewRenameTimer( this );
1365 // TODO: user better initial values/nothing selected
1366 m_currentCol
= NULL
;
1369 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1371 #if wxUSE_DRAG_AND_DROP
1373 m_dragStart
= wxPoint(0,0);
1375 m_dragEnabled
= false;
1376 m_dropEnabled
= false;
1378 m_dropHintLine
= (unsigned int) -1;
1379 #endif // wxUSE_DRAG_AND_DROP
1381 m_lineLastClicked
= (unsigned int) -1;
1382 m_lineBeforeLastClicked
= (unsigned int) -1;
1383 m_lineSelectSingleOnUp
= (unsigned int) -1;
1387 SetBackgroundColour( *wxWHITE
);
1389 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1391 m_penRule
= wxPen(GetRuleColour());
1393 // compose a pen whichcan draw black lines
1394 // TODO: maybe there is something system colour to use
1395 m_penExpander
= wxPen(wxColour(0,0,0));
1397 m_root
= new wxDataViewTreeNode( NULL
);
1398 m_root
->SetHasChildren(true);
1400 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1402 m_underMouse
= NULL
;
1406 wxDataViewMainWindow::~wxDataViewMainWindow()
1409 delete m_renameTimer
;
1413 #if wxUSE_DRAG_AND_DROP
1414 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1416 m_dragFormat
= format
;
1417 m_dragEnabled
= format
!= wxDF_INVALID
;
1422 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1424 m_dropFormat
= format
;
1425 m_dropEnabled
= format
!= wxDF_INVALID
;
1428 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1433 void wxDataViewMainWindow::RemoveDropHint()
1438 RefreshRow( m_dropHintLine
);
1439 m_dropHintLine
= (unsigned int) -1;
1443 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1444 wxCoord y
, wxDragResult def
)
1448 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1449 unsigned int row
= GetLineAt( yy
);
1451 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1457 wxDataViewItem item
= GetItemByRow( row
);
1459 wxDataViewModel
*model
= GetOwner()->GetModel();
1461 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1462 event
.SetEventObject( m_owner
);
1463 event
.SetItem( item
);
1464 event
.SetModel( model
);
1465 event
.SetDataFormat( format
);
1466 if (!m_owner
->HandleWindowEvent( event
))
1472 if (!event
.IsAllowed())
1479 if (m_dropHint
&& (row
!= m_dropHintLine
))
1480 RefreshRow( m_dropHintLine
);
1482 m_dropHintLine
= row
;
1488 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1494 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1495 unsigned int row
= GetLineAt( yy
);
1497 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1500 wxDataViewItem item
= GetItemByRow( row
);
1502 wxDataViewModel
*model
= GetOwner()->GetModel();
1504 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1505 event
.SetEventObject( m_owner
);
1506 event
.SetItem( item
);
1507 event
.SetModel( model
);
1508 event
.SetDataFormat( format
);
1509 if (!m_owner
->HandleWindowEvent( event
))
1512 if (!event
.IsAllowed())
1518 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1523 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1524 unsigned int row
= GetLineAt( yy
);
1526 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1529 wxDataViewItem item
= GetItemByRow( row
);
1531 wxDataViewModel
*model
= GetOwner()->GetModel();
1533 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1535 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1536 event
.SetEventObject( m_owner
);
1537 event
.SetItem( item
);
1538 event
.SetModel( model
);
1539 event
.SetDataFormat( format
);
1540 event
.SetDataSize( obj
->GetSize() );
1541 event
.SetDataBuffer( obj
->GetData() );
1542 if (!m_owner
->HandleWindowEvent( event
))
1545 if (!event
.IsAllowed())
1551 void wxDataViewMainWindow::OnLeave()
1556 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1558 int height
= GetLineHeight( row
);
1560 unsigned int cols
= GetOwner()->GetColumnCount();
1562 for (col
= 0; col
< cols
; col
++)
1564 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1565 if (column
->IsHidden())
1566 continue; // skip it!
1567 width
+= column
->GetWidth();
1571 if (!IsVirtualList())
1573 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1574 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1575 indent
= indent
+ m_lineHeight
;
1576 // try to use the m_lineHeight as the expander space
1578 if(!node
->HasChildren())
1583 wxBitmap
bitmap( width
, height
);
1584 wxMemoryDC
dc( bitmap
);
1585 dc
.SetFont( GetFont() );
1586 dc
.SetPen( *wxBLACK_PEN
);
1587 dc
.SetBrush( *wxWHITE_BRUSH
);
1588 dc
.DrawRectangle( 0,0,width
,height
);
1590 wxDataViewModel
*model
= m_owner
->GetModel();
1592 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1595 // TODO-RTL: last column for RTL support
1596 expander
= GetOwner()->GetColumnAt( 0 );
1597 GetOwner()->SetExpanderColumn(expander
);
1602 for (col
= 0; col
< cols
; col
++)
1604 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1605 wxDataViewRenderer
*cell
= column
->GetRenderer();
1607 if (column
->IsHidden())
1608 continue; // skip it!
1610 width
= column
->GetWidth();
1612 if (column
== expander
)
1616 wxDataViewItem item
= GetItemByRow( row
);
1617 model
->GetValue( value
, item
, column
->GetModelColumn());
1618 cell
->SetValue( value
);
1620 wxRect
item_rect(x
, 0, width
, height
);
1621 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1623 // dc.SetClippingRegion( item_rect );
1624 wxDataViewItemAttr attr
;
1626 hasAttr
= model
->GetAttr(item
, column
->GetModelColumn(), attr
);
1627 cell
->RenderWithAttr(dc
, item_rect
, cell
->CalculateAlignment(),
1628 hasAttr
? &attr
: NULL
, 0);
1629 // dc.DestroyClippingRegion();
1637 #endif // wxUSE_DRAG_AND_DROP
1640 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1642 wxDataViewModel
*model
= GetOwner()->GetModel();
1643 wxAutoBufferedPaintDC
dc( this );
1646 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1647 dc
.SetPen( *wxTRANSPARENT_PEN
);
1648 dc
.DrawRectangle(GetClientSize());
1652 GetOwner()->PrepareDC( dc
);
1653 dc
.SetFont( GetFont() );
1655 wxRect update
= GetUpdateRegion().GetBox();
1656 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1658 // compute which items needs to be redrawn
1659 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1660 unsigned int item_count
=
1661 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1662 (int)(GetRowCount( ) - item_start
));
1663 unsigned int item_last
= item_start
+ item_count
;
1664 // Get the parent of DataViewCtrl
1665 wxWindow
*parent
= GetParent()->GetParent();
1666 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1667 cache_event
.SetEventObject(GetParent());
1668 cache_event
.SetCache(item_start
, item_last
- 1);
1669 parent
->ProcessWindowEvent(cache_event
);
1671 // compute which columns needs to be redrawn
1672 unsigned int cols
= GetOwner()->GetColumnCount();
1673 unsigned int col_start
= 0;
1674 unsigned int x_start
;
1675 for (x_start
= 0; col_start
< cols
; col_start
++)
1677 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1678 if (col
->IsHidden())
1679 continue; // skip it!
1681 unsigned int w
= col
->GetWidth();
1682 if (x_start
+w
>= (unsigned int)update
.x
)
1688 unsigned int col_last
= col_start
;
1689 unsigned int x_last
= x_start
;
1690 for (; col_last
< cols
; col_last
++)
1692 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1693 if (col
->IsHidden())
1694 continue; // skip it!
1696 if (x_last
> (unsigned int)update
.GetRight())
1699 x_last
+= col
->GetWidth();
1702 // Draw horizontal rules if required
1703 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1705 dc
.SetPen(m_penRule
);
1706 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1708 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1710 int y
= GetLineStart( i
);
1711 dc
.DrawLine(x_start
, y
, x_last
, y
);
1715 // Draw vertical rules if required
1716 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1718 dc
.SetPen(m_penRule
);
1719 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1722 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1724 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1725 if (col
->IsHidden())
1726 continue; // skip it
1728 dc
.DrawLine(x
, GetLineStart( item_start
),
1729 x
, GetLineStart( item_last
) );
1731 x
+= col
->GetWidth();
1734 // Draw last vertical rule
1735 dc
.DrawLine(x
, GetLineStart( item_start
),
1736 x
, GetLineStart( item_last
) );
1739 // redraw the background for the items which are selected/current
1740 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1742 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1743 if (selected
|| item
== m_currentRow
)
1745 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1746 if (item
== m_currentRow
)
1747 flags
|= wxCONTROL_CURRENT
;
1749 flags
|= wxCONTROL_FOCUSED
;
1751 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1752 wxRendererNative::Get().DrawItemSelectionRect
1762 #if wxUSE_DRAG_AND_DROP
1765 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1766 x_last
, GetLineHeight( m_dropHintLine
) );
1767 dc
.SetPen( *wxBLACK_PEN
);
1768 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1769 dc
.DrawRectangle( rect
);
1771 #endif // wxUSE_DRAG_AND_DROP
1773 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1776 // TODO-RTL: last column for RTL support
1777 expander
= GetOwner()->GetColumnAt( 0 );
1778 GetOwner()->SetExpanderColumn(expander
);
1781 // redraw all cells for all rows which must be repainted and all columns
1783 cell_rect
.x
= x_start
;
1784 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1786 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1787 wxDataViewRenderer
*cell
= col
->GetRenderer();
1788 cell_rect
.width
= col
->GetWidth();
1790 if (col
->IsHidden())
1791 continue; // skip it!
1793 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1795 // get the cell value and set it into the renderer
1797 wxDataViewTreeNode
*node
= NULL
;
1798 wxDataViewItem dataitem
;
1800 if (!IsVirtualList())
1802 node
= GetTreeNodeByRow(item
);
1806 dataitem
= node
->GetItem();
1808 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1809 !model
->HasContainerColumns(dataitem
))
1814 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1817 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1818 cell
->SetValue( value
);
1821 cell_rect
.y
= GetLineStart( item
);
1822 cell_rect
.height
= GetLineHeight( item
);
1824 // Draw the expander here.
1826 if ((!IsVirtualList()) && (col
== expander
))
1828 indent
= node
->GetIndentLevel();
1830 // Calculate the indent first
1831 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1833 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1835 // change the cell_rect.x to the appropriate pos
1836 int expander_x
= indent
+ EXPANDER_MARGIN
;
1837 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2)
1838 - (expander_width
/2) - EXPANDER_OFFSET
;
1840 indent
= indent
+ m_lineHeight
;
1841 // try to use the m_lineHeight as the expander space
1843 dc
.SetPen( m_penExpander
);
1844 dc
.SetBrush( wxNullBrush
);
1845 if( node
->HasChildren() )
1847 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1849 if (m_underMouse
== node
)
1851 flag
|= wxCONTROL_CURRENT
;
1853 if( node
->IsOpen() )
1854 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
,
1855 flag
|wxCONTROL_EXPANDED
);
1857 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1860 // force the expander column to left-center align
1861 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1863 if (node
&& !node
->HasChildren())
1865 // Yes, if the node does not have any child, it must be a leaf which
1866 // mean that it is a temporarily created by GetTreeNodeByRow
1870 wxRect item_rect
= cell_rect
;
1871 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1873 // account for the tree indent (harmless if we're not indented)
1874 item_rect
.x
+= indent
;
1875 item_rect
.width
-= indent
;
1878 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1879 state
|= wxDATAVIEW_CELL_SELECTED
;
1881 // TODO: it would be much more efficient to create a clipping
1882 // region for the entire column being rendered (in the OnPaint
1883 // of wxDataViewMainWindow) instead of a single clip region for
1884 // each cell. However it would mean that each renderer should
1885 // respect the given wxRect's top & bottom coords, eventually
1886 // violating only the left & right coords - however the user can
1887 // make its own renderer and thus we cannot be sure of that.
1888 wxDCClipper
clip(dc
, item_rect
);
1890 wxDataViewItemAttr attr
;
1892 hasAttr
= model
->GetAttr(dataitem
, col
->GetModelColumn(), attr
);
1893 cell
->RenderWithAttr(dc
, item_rect
, cell
->CalculateAlignment(),
1894 hasAttr
? &attr
: NULL
, state
);
1897 cell_rect
.x
+= cell_rect
.width
;
1901 void wxDataViewMainWindow::OnRenameTimer()
1903 // We have to call this here because changes may just have
1904 // been made and no screen update taken place.
1907 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1908 // (needs to be tested!)
1912 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1914 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1916 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1919 //-----------------------------------------------------------------------------
1920 // Helper class for do operation on the tree node
1921 //-----------------------------------------------------------------------------
1926 virtual ~DoJob() { }
1928 // The return value control how the tree-walker tranverse the tree
1929 // 0: Job done, stop tranverse and return
1930 // 1: Ignore the current node's subtree and continue
1931 // 2: Job not done, continue
1932 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1933 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1934 virtual int operator() ( void * n
) = 0;
1937 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1942 switch( func( node
) )
1953 wxDataViewTreeNodes nodes
= node
->GetNodes();
1954 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1956 int len_nodes
= nodes
.GetCount();
1957 int len
= leaves
.GetCount();
1958 int i
= 0, nodes_i
= 0;
1960 for(; i
< len
; i
++ )
1962 void * n
= leaves
[i
];
1963 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1965 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1968 if( Walker( nd
, func
) )
1987 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1989 if (IsVirtualList())
1991 wxDataViewVirtualListModel
*list_model
=
1992 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1993 m_count
= list_model
->GetCount();
2000 wxDataViewTreeNode
* node
;
2001 node
= FindNode(parent
);
2006 node
->SetHasChildren( true );
2008 if( g_model
->IsContainer( item
) )
2010 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2011 newnode
->SetItem(item
);
2012 newnode
->SetHasChildren( true );
2013 node
->AddNode( newnode
);
2016 node
->AddLeaf( item
.GetID() );
2018 node
->ChangeSubTreeCount(1);
2026 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2028 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2029 const wxDataViewItem
& item
)
2031 if (IsVirtualList())
2033 wxDataViewVirtualListModel
*list_model
=
2034 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
2035 m_count
= list_model
->GetCount();
2037 if( m_currentRow
> GetRowCount() )
2038 m_currentRow
= m_count
- 1;
2040 // TODO: why empty the entire selection?
2041 m_selection
.Empty();
2048 wxDataViewTreeNode
* node
= FindNode(parent
);
2050 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2051 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
,
2052 false, "item not found" );
2055 node
->GetChildren().Remove( item
.GetID() );
2056 // Manipolate selection
2057 if( m_selection
.GetCount() > 1 )
2059 m_selection
.Empty();
2061 bool isContainer
= false;
2062 wxDataViewTreeNodes nds
= node
->GetNodes();
2063 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2065 if (nds
[i
]->GetItem() == item
)
2073 wxDataViewTreeNode
* n
= NULL
;
2074 wxDataViewTreeNodes nodes
= node
->GetNodes();
2075 int len
= nodes
.GetCount();
2076 for( int i
= 0; i
< len
; i
++)
2078 if( nodes
[i
]->GetItem() == item
)
2085 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2087 node
->GetNodes().Remove( n
);
2088 sub
-= n
->GetSubTreeCount();
2089 ::DestroyTreeHelper(n
);
2091 // Make the row number invalid and get a new valid one when user call GetRowCount
2093 node
->ChangeSubTreeCount(sub
);
2095 // Change the current row to the last row if the current exceed the max row number
2096 if( m_currentRow
> GetRowCount() )
2097 m_currentRow
= m_count
- 1;
2104 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2110 wxWindow
*parent
= GetParent();
2111 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2112 le
.SetEventObject(parent
);
2113 le
.SetModel(GetOwner()->GetModel());
2115 parent
->GetEventHandler()->ProcessEvent(le
);
2120 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2122 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2123 /*#define MAX_VIRTUAL_WIDTH 100000
2125 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2126 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2127 Refresh( true, &rect );
2135 wxWindow
*parent
= GetParent();
2136 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2137 le
.SetEventObject(parent
);
2138 le
.SetModel(GetOwner()->GetModel());
2141 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2142 parent
->GetEventHandler()->ProcessEvent(le
);
2147 bool wxDataViewMainWindow::Cleared()
2152 BuildTree( GetOwner()->GetModel() );
2159 void wxDataViewMainWindow::UpdateDisplay()
2164 void wxDataViewMainWindow::OnInternalIdle()
2166 wxWindow::OnInternalIdle();
2170 RecalculateDisplay();
2175 void wxDataViewMainWindow::RecalculateDisplay()
2177 wxDataViewModel
*model
= GetOwner()->GetModel();
2184 int width
= GetEndOfLastCol();
2185 int height
= GetLineStart( GetRowCount() );
2187 SetVirtualSize( width
, height
);
2188 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2193 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2195 wxWindow::ScrollWindow( dx
, dy
, rect
);
2197 if (GetOwner()->m_headerArea
)
2198 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2201 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2204 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2205 int sy
= GetLineStart( rows
)/y
;
2209 wxRect rect
= GetClientRect();
2213 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2214 for (x_start
= 0; colnum
< column
; colnum
++)
2216 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2217 if (col
->IsHidden())
2218 continue; // skip it!
2220 w
= col
->GetWidth();
2224 int x_end
= x_start
+ w
;
2225 xe
= xx
+ rect
.width
;
2228 sx
= ( xx
+ x_end
- xe
)/x
;
2235 m_owner
->Scroll( sx
, sy
);
2238 int wxDataViewMainWindow::GetCountPerPage() const
2240 wxSize size
= GetClientSize();
2241 return size
.y
/ m_lineHeight
;
2244 int wxDataViewMainWindow::GetEndOfLastCol() const
2248 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2250 const wxDataViewColumn
*c
=
2251 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2254 width
+= c
->GetWidth();
2259 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2263 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2265 return GetLineAt( y
);
2268 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2270 wxSize client_size
= GetClientSize();
2271 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2272 &client_size
.x
, &client_size
.y
);
2274 // we should deal with the pixel here
2275 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2277 return wxMin( GetRowCount()-1, row
);
2280 unsigned int wxDataViewMainWindow::GetRowCount()
2282 if ( m_count
== -1 )
2284 m_count
= RecalculateCount();
2290 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2297 void wxDataViewMainWindow::SelectAllRows( bool on
)
2304 m_selection
.Clear();
2305 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2306 m_selection
.Add( i
);
2311 unsigned int first_visible
= GetFirstVisibleRow();
2312 unsigned int last_visible
= GetLastVisibleRow();
2314 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2316 unsigned int row
= m_selection
[i
];
2317 if ((row
>= first_visible
) && (row
<= last_visible
))
2320 m_selection
.Clear();
2324 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2326 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2330 m_selection
.Add( row
);
2338 m_selection
.Remove( row
);
2344 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2348 unsigned int tmp
= from
;
2354 for (i
= from
; i
<= to
; i
++)
2356 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2359 m_selection
.Add( i
);
2364 m_selection
.Remove( i
);
2367 RefreshRows( from
, to
);
2370 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2372 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2374 int n
= aSelections
[i
];
2376 m_selection
.Add( n
);
2381 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2383 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2384 m_selection
.Add( row
);
2386 m_selection
.Remove( row
);
2390 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2392 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2395 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2397 wxWindow
*parent
= GetParent();
2398 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2400 le
.SetEventObject(parent
);
2401 le
.SetModel(GetOwner()->GetModel());
2404 parent
->GetEventHandler()->ProcessEvent(le
);
2407 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2409 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2410 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2412 wxSize client_size
= GetClientSize();
2413 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2414 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2415 if (intersect_rect
.width
> 0)
2416 Refresh( true, &intersect_rect
);
2419 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2423 unsigned int tmp
= to
;
2428 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2429 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2431 wxSize client_size
= GetClientSize();
2432 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2433 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2434 if (intersect_rect
.width
> 0)
2435 Refresh( true, &intersect_rect
);
2438 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2440 wxSize client_size
= GetClientSize();
2441 int start
= GetLineStart( firstRow
);
2442 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2443 if (start
> client_size
.y
) return;
2445 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2447 Refresh( true, &rect
);
2450 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2452 wxCHECK_RET( newCurrent
< GetRowCount(),
2453 wxT("invalid item index in OnArrowChar()") );
2455 // if there is no selection, we cannot move it anywhere
2456 if (!HasCurrentRow())
2459 unsigned int oldCurrent
= m_currentRow
;
2461 // in single selection we just ignore Shift as we can't select several
2463 if ( event
.ShiftDown() && !IsSingleSel() )
2465 RefreshRow( oldCurrent
);
2467 ChangeCurrentRow( newCurrent
);
2469 // select all the items between the old and the new one
2470 if ( oldCurrent
> newCurrent
)
2472 newCurrent
= oldCurrent
;
2473 oldCurrent
= m_currentRow
;
2476 SelectRows( oldCurrent
, newCurrent
, true );
2477 if (oldCurrent
!=newCurrent
)
2478 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2482 RefreshRow( oldCurrent
);
2484 // all previously selected items are unselected unless ctrl is held
2485 if ( !event
.ControlDown() )
2486 SelectAllRows(false);
2488 ChangeCurrentRow( newCurrent
);
2490 if ( !event
.ControlDown() )
2492 SelectRow( m_currentRow
, true );
2493 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2496 RefreshRow( m_currentRow
);
2499 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2502 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2506 rect
.y
= GetLineStart( row
);
2507 rect
.width
= GetEndOfLastCol();
2508 rect
.height
= GetLineHeight( row
);
2513 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2515 const wxDataViewModel
*model
= GetOwner()->GetModel();
2517 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2519 // TODO make more efficient
2524 for (r
= 0; r
< row
; r
++)
2526 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2527 if (!node
) return start
;
2529 wxDataViewItem item
= node
->GetItem();
2531 if (node
&& !node
->HasChildren())
2533 // Yes, if the node does not have any child, it must be a leaf which
2534 // mean that it is a temporarily created by GetTreeNodeByRow
2538 unsigned int cols
= GetOwner()->GetColumnCount();
2540 int height
= m_lineHeight
;
2541 for (col
= 0; col
< cols
; col
++)
2543 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2544 if (column
->IsHidden())
2545 continue; // skip it!
2548 model
->IsContainer(item
) &&
2549 !model
->HasContainerColumns(item
))
2550 continue; // skip it!
2553 model
->GetValue( value
, item
, column
->GetModelColumn() );
2555 wxDataViewRenderer
*renderer
=
2556 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2557 renderer
->SetValue( value
);
2558 height
= wxMax( height
, renderer
->GetSize().y
);
2568 return row
* m_lineHeight
;
2572 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2574 const wxDataViewModel
*model
= GetOwner()->GetModel();
2576 // check for the easy case first
2577 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2578 return y
/ m_lineHeight
;
2580 // TODO make more efficient
2581 unsigned int row
= 0;
2582 unsigned int yy
= 0;
2585 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2588 // not really correct...
2589 return row
+ ((y
-yy
) / m_lineHeight
);
2592 wxDataViewItem item
= node
->GetItem();
2594 if (node
&& !node
->HasChildren())
2596 // Yes, if the node does not have any child, it must be a leaf which
2597 // mean that it is a temporarily created by GetTreeNodeByRow
2601 unsigned int cols
= GetOwner()->GetColumnCount();
2603 int height
= m_lineHeight
;
2604 for (col
= 0; col
< cols
; col
++)
2606 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2607 if (column
->IsHidden())
2608 continue; // skip it!
2611 model
->IsContainer(item
) &&
2612 !model
->HasContainerColumns(item
))
2613 continue; // skip it!
2616 model
->GetValue( value
, item
, column
->GetModelColumn() );
2618 wxDataViewRenderer
*renderer
=
2619 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2620 renderer
->SetValue( value
);
2621 height
= wxMax( height
, renderer
->GetSize().y
);
2632 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2634 const wxDataViewModel
*model
= GetOwner()->GetModel();
2636 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2638 wxASSERT( !IsVirtualList() );
2640 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2641 // wxASSERT( node );
2642 if (!node
) return m_lineHeight
;
2644 wxDataViewItem item
= node
->GetItem();
2646 if (node
&& !node
->HasChildren())
2648 // Yes, if the node does not have any child, it must be a leaf which
2649 // mean that it is a temporarily created by GetTreeNodeByRow
2653 int height
= m_lineHeight
;
2655 unsigned int cols
= GetOwner()->GetColumnCount();
2657 for (col
= 0; col
< cols
; col
++)
2659 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2660 if (column
->IsHidden())
2661 continue; // skip it!
2664 model
->IsContainer(item
) &&
2665 !model
->HasContainerColumns(item
))
2666 continue; // skip it!
2669 model
->GetValue( value
, item
, column
->GetModelColumn() );
2671 wxDataViewRenderer
*renderer
=
2672 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2673 renderer
->SetValue( value
);
2674 height
= wxMax( height
, renderer
->GetSize().y
);
2681 return m_lineHeight
;
2685 class RowToItemJob
: public DoJob
2688 RowToItemJob( unsigned int row
, int current
)
2689 { this->row
= row
; this->current
= current
; }
2690 virtual ~RowToItemJob() {}
2692 virtual int operator() ( wxDataViewTreeNode
* node
)
2695 if( current
== static_cast<int>(row
))
2697 ret
= node
->GetItem();
2701 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2703 current
+= node
->GetSubTreeCount();
2708 // If the current has no child node, we can find the desired item of the row
2710 // This if can speed up finding in some case, and will has a very good effect
2711 // when it comes to list view
2712 if( node
->GetNodes().GetCount() == 0)
2714 int index
= static_cast<int>(row
) - current
- 1;
2715 ret
= node
->GetChildren().Item( index
);
2722 virtual int operator() ( void * n
)
2725 if( current
== static_cast<int>(row
))
2727 ret
= wxDataViewItem( n
);
2733 wxDataViewItem
GetResult() const
2742 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2744 if (IsVirtualList())
2746 return wxDataViewItem( wxUIntToPtr(row
+1) );
2750 RowToItemJob
job( row
, -2 );
2751 Walker( m_root
, job
);
2752 return job
.GetResult();
2756 class RowToTreeNodeJob
: public DoJob
2759 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2762 this->current
= current
;
2766 virtual ~RowToTreeNodeJob(){ }
2768 virtual int operator() ( wxDataViewTreeNode
* node
)
2771 if( current
== static_cast<int>(row
))
2777 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2779 current
+= node
->GetSubTreeCount();
2786 // If the current node has no children, we can find the desired item of the
2787 // row number directly.
2788 // This if can speed up finding in some case, and will have a very good
2789 // effect for list views.
2790 if( node
->GetNodes().GetCount() == 0)
2792 int index
= static_cast<int>(row
) - current
- 1;
2793 void * n
= node
->GetChildren().Item( index
);
2794 ret
= new wxDataViewTreeNode( parent
);
2795 ret
->SetItem( wxDataViewItem( n
));
2796 ret
->SetHasChildren(false);
2803 virtual int operator() ( void * n
)
2806 if( current
== static_cast<int>(row
))
2808 ret
= new wxDataViewTreeNode( parent
);
2809 ret
->SetItem( wxDataViewItem( n
));
2810 ret
->SetHasChildren(false);
2817 wxDataViewTreeNode
* GetResult() const
2823 wxDataViewTreeNode
* ret
;
2824 wxDataViewTreeNode
* parent
;
2827 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2829 wxASSERT( !IsVirtualList() );
2831 RowToTreeNodeJob
job( row
, -2, m_root
);
2832 Walker( m_root
, job
);
2833 return job
.GetResult();
2836 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2837 const wxDataViewItem
& item
)
2839 wxWindow
*parent
= GetParent();
2840 wxDataViewEvent
le(type
, parent
->GetId());
2842 le
.SetEventObject(parent
);
2843 le
.SetModel(GetOwner()->GetModel());
2846 parent
->GetEventHandler()->ProcessEvent(le
);
2850 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2852 if (IsVirtualList())
2855 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2859 if (!node
->HasChildren())
2865 return node
->IsOpen();
2868 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2870 if (IsVirtualList())
2873 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2877 if (!node
->HasChildren())
2886 void wxDataViewMainWindow::Expand( unsigned int row
)
2888 if (IsVirtualList())
2891 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2895 if (!node
->HasChildren())
2901 if (!node
->IsOpen())
2904 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2906 // Check if the user prevent expanding
2907 if( e
.GetSkipped() )
2912 // build the children of current node
2913 if( node
->GetChildrenNumber() == 0 )
2916 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2919 // By expanding the node all row indices that are currently in the selection list
2920 // and are greater than our node have become invalid. So we have to correct that now.
2921 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2922 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2924 const unsigned testRow
= m_selection
[i
];
2925 // all rows above us are not affected, so skip them
2929 m_selection
[i
] += rowAdjustment
;
2932 if(m_currentRow
> row
)
2933 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2937 // Send the expanded event
2938 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2942 void wxDataViewMainWindow::Collapse(unsigned int row
)
2944 if (IsVirtualList())
2947 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2951 if (!node
->HasChildren())
2960 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2961 if( e
.GetSkipped() )
2964 // Find out if there are selected items below the current node.
2965 bool selectCollapsingRow
= false;
2966 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2967 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2968 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2970 const unsigned testRow
= m_selection
[i
];
2971 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2973 selectCollapsingRow
= true;
2974 // get out as soon as we have found a node that is selected
2981 // If the node to be closed has selected items the user won't see those any longer.
2982 // We select the collapsing node in this case.
2983 if(selectCollapsingRow
)
2985 SelectAllRows(false);
2986 ChangeCurrentRow(row
);
2987 SelectRow(row
, true);
2988 SendSelectionChangedEvent(GetItemByRow(row
));
2992 // if there were no selected items below our node we still need to "fix" the
2993 // selection list to adjust for the changing of the row indices.
2994 // We actually do the opposite of what we are doing in Expand().
2995 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2997 const unsigned testRow
= m_selection
[i
];
2998 // all rows above us are not affected, so skip them
3002 m_selection
[i
] -= rowAdjustment
;
3005 // if the "current row" is being collapsed away we change it to the current row ;-)
3006 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3007 ChangeCurrentRow(row
);
3008 else if(m_currentRow
> row
)
3009 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3014 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3018 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3020 wxDataViewModel
* model
= GetOwner()->GetModel();
3027 // Compose the a parent-chain of the finding item
3029 list
.DeleteContents( true );
3030 wxDataViewItem
it( item
);
3033 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3034 list
.Insert( pItem
);
3035 it
= model
->GetParent( it
);
3038 // Find the item along the parent-chain.
3039 // This algorithm is designed to speed up the node-finding method
3040 wxDataViewTreeNode
* node
= m_root
;
3041 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
3043 if( node
->HasChildren() )
3045 if( node
->GetChildrenNumber() == 0 )
3048 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3051 wxDataViewTreeNodes nodes
= node
->GetNodes();
3055 for (i
= 0; i
< nodes
.GetCount(); i
++)
3057 if (nodes
[i
]->GetItem() == (**iter
))
3059 if (nodes
[i
]->GetItem() == item
)
3076 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3077 wxDataViewColumn
* &column
)
3079 wxDataViewColumn
*col
= NULL
;
3080 unsigned int cols
= GetOwner()->GetColumnCount();
3081 unsigned int colnum
= 0;
3083 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3084 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3086 col
= GetOwner()->GetColumnAt(colnum
);
3087 if (col
->IsHidden())
3088 continue; // skip it!
3090 unsigned int w
= col
->GetWidth();
3091 if (x_start
+w
>= (unsigned int)x
)
3098 item
= GetItemByRow( GetLineAt( y
) );
3101 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3102 const wxDataViewColumn
* column
)
3107 unsigned int cols
= GetOwner()->GetColumnCount();
3108 // If column is null the loop will compute the combined width of all columns.
3109 // Otherwise, it will compute the x position of the column we are looking for.
3110 for (unsigned int i
= 0; i
< cols
; i
++)
3112 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3117 if (col
->IsHidden())
3118 continue; // skip it!
3120 xpos
+= col
->GetWidth();
3121 width
+= col
->GetWidth();
3126 // If we have a column, we need can get its width directly.
3127 if(column
->IsHidden())
3130 width
= column
->GetWidth();
3135 // If we have no column, we reset the x position back to zero.
3139 // we have to take an expander column into account and compute its indentation
3140 // to get the correct x position where the actual text is
3142 int row
= GetRowByItem(item
);
3143 if (!IsVirtualList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3145 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3146 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3147 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3149 if(!node
->HasChildren())
3153 wxRect
itemRect( xpos
+ indent
,
3154 GetLineStart( row
),
3156 GetLineHeight( row
) );
3158 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3159 &itemRect
.x
, &itemRect
.y
);
3164 int wxDataViewMainWindow::RecalculateCount()
3166 if (IsVirtualList())
3168 wxDataViewVirtualListModel
*list_model
=
3169 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3171 return list_model
->GetCount();
3175 return m_root
->GetSubTreeCount();
3179 class ItemToRowJob
: public DoJob
3182 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3189 // Maybe binary search will help to speed up this process
3190 virtual int operator() ( wxDataViewTreeNode
* node
)
3193 if( node
->GetItem() == item
)
3198 if( node
->GetItem() == **m_iter
)
3205 ret
+= node
->GetSubTreeCount();
3211 virtual int operator() ( void * n
)
3214 if( n
== item
.GetID() )
3219 // the row number is begin from zero
3220 int GetResult() const
3224 ItemList::const_iterator m_iter
;
3225 wxDataViewItem item
;
3230 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3232 const wxDataViewModel
* model
= GetOwner()->GetModel();
3236 if (IsVirtualList())
3238 return wxPtrToUInt( item
.GetID() ) -1;
3245 // Compose the a parent-chain of the finding item
3247 wxDataViewItem
* pItem
;
3248 list
.DeleteContents( true );
3249 wxDataViewItem
it( item
);
3252 pItem
= new wxDataViewItem( it
);
3253 list
.Insert( pItem
);
3254 it
= model
->GetParent( it
);
3256 pItem
= new wxDataViewItem( );
3257 list
.Insert( pItem
);
3259 ItemToRowJob
job( item
, list
.begin() );
3260 Walker(m_root
, job
);
3261 return job
.GetResult();
3265 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
3266 wxDataViewTreeNode
* node
)
3268 if( !model
->IsContainer( item
) )
3271 wxDataViewItemArray children
;
3272 unsigned int num
= model
->GetChildren( item
, children
);
3274 unsigned int index
= 0;
3275 while( index
< num
)
3277 if( model
->IsContainer( children
[index
] ) )
3279 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3280 n
->SetItem(children
[index
]);
3281 n
->SetHasChildren( true );
3286 node
->AddLeaf( children
[index
].GetID() );
3290 node
->SetSubTreeCount( num
);
3291 wxDataViewTreeNode
* n
= node
->GetParent();
3293 n
->ChangeSubTreeCount(num
);
3297 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3301 if (GetOwner()->GetModel()->IsVirtualListModel())
3307 m_root
= new wxDataViewTreeNode( NULL
);
3308 m_root
->SetHasChildren(true);
3310 // First we define a invalid item to fetch the top-level elements
3311 wxDataViewItem item
;
3313 BuildTreeHelper( model
, item
, m_root
);
3317 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3319 if( node
->GetNodeNumber() != 0 )
3321 int len
= node
->GetNodeNumber();
3322 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3323 for (int i
= 0; i
< len
; i
++)
3324 DestroyTreeHelper(nodes
[i
]);
3329 void wxDataViewMainWindow::DestroyTree()
3331 if (!IsVirtualList())
3333 ::DestroyTreeHelper(m_root
);
3339 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3341 if ( GetParent()->HandleAsNavigationKey(event
) )
3344 // no item -> nothing to do
3345 if (!HasCurrentRow())
3351 // don't use m_linesPerPage directly as it might not be computed yet
3352 const int pageSize
= GetCountPerPage();
3353 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3355 switch ( event
.GetKeyCode() )
3359 wxWindow
*parent
= GetParent();
3360 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3362 le
.SetItem( GetItemByRow(m_currentRow
) );
3363 le
.SetEventObject(parent
);
3364 le
.SetModel(GetOwner()->GetModel());
3366 parent
->GetEventHandler()->ProcessEvent(le
);
3371 if ( m_currentRow
> 0 )
3372 OnArrowChar( m_currentRow
- 1, event
);
3376 if ( m_currentRow
< GetRowCount() - 1 )
3377 OnArrowChar( m_currentRow
+ 1, event
);
3379 // Add the process for tree expanding/collapsing
3382 if (IsVirtualList())
3385 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3389 if (node
->HasChildren() && node
->IsOpen())
3391 Collapse(m_currentRow
);
3393 else // if the node is already closed we move the selection to its parent
3395 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3397 if(!node
->HasChildren())
3402 int parent
= GetRowByItem( parent_node
->GetItem() );
3405 unsigned int row
= m_currentRow
;
3406 SelectRow( row
, false);
3407 SelectRow( parent
, true );
3408 ChangeCurrentRow( parent
);
3409 GetOwner()->EnsureVisible( parent
, -1 );
3410 SendSelectionChangedEvent( parent_node
->GetItem() );
3418 if (!IsExpanded( m_currentRow
))
3419 Expand( m_currentRow
);
3422 unsigned int row
= m_currentRow
;
3423 SelectRow( row
, false );
3424 SelectRow( row
+ 1, true );
3425 ChangeCurrentRow( row
+ 1 );
3426 GetOwner()->EnsureVisible( row
+ 1, -1 );
3427 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3434 OnArrowChar( GetRowCount() - 1, event
);
3439 OnArrowChar( 0, event
);
3444 int steps
= pageSize
- 1;
3445 int index
= m_currentRow
- steps
;
3449 OnArrowChar( index
, event
);
3455 int steps
= pageSize
- 1;
3456 unsigned int index
= m_currentRow
+ steps
;
3457 unsigned int count
= GetRowCount();
3458 if ( index
>= count
)
3461 OnArrowChar( index
, event
);
3467 if(m_selection
.size() == 1)
3469 // TODO: we need to revise that when we have a concept for a 'current column'
3470 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3480 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3482 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3484 // let the base handle mouse wheel events.
3489 int x
= event
.GetX();
3490 int y
= event
.GetY();
3491 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3492 wxDataViewColumn
*col
= NULL
;
3495 unsigned int cols
= GetOwner()->GetColumnCount();
3497 for (i
= 0; i
< cols
; i
++)
3499 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3501 continue; // skip it!
3503 if (x
< xpos
+ c
->GetWidth())
3508 xpos
+= c
->GetWidth();
3513 wxDataViewRenderer
*cell
= col
->GetRenderer();
3514 unsigned int current
= GetLineAt( y
);
3515 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3517 // Unselect all if below the last row ?
3521 // Test whether the mouse is hovered on the tree item button
3522 bool hoverOverExpander
= false;
3523 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3525 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3526 if( node
!=NULL
&& node
->HasChildren() )
3528 int indent
= node
->GetIndentLevel();
3529 indent
= GetOwner()->GetIndent()*indent
;
3531 // we make the rectangle we are looking in a bit bigger than the actual
3532 // visual expander so the user can hit that little thing reliably
3533 wxRect
rect( xpos
+ indent
,
3534 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3535 m_lineHeight
, m_lineHeight
);
3536 if( rect
.Contains(x
, y
) )
3538 // So the mouse is over the expander
3539 hoverOverExpander
= true;
3540 if (m_underMouse
&& m_underMouse
!= node
)
3542 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3543 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3545 if (m_underMouse
!= node
)
3547 // wxLogMessage("Do the row: %d", current);
3548 RefreshRow(current
);
3550 m_underMouse
= node
;
3553 if (node
!=NULL
&& !node
->HasChildren())
3556 if (!hoverOverExpander
)
3558 if (m_underMouse
!= NULL
)
3560 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3561 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3562 m_underMouse
= NULL
;
3566 wxDataViewModel
*model
= GetOwner()->GetModel();
3568 #if wxUSE_DRAG_AND_DROP
3569 if (event
.Dragging())
3571 if (m_dragCount
== 0)
3573 // we have to report the raw, physical coords as we want to be
3574 // able to call HitTest(event.m_pointDrag) from the user code to
3575 // get the item being dragged
3576 m_dragStart
= event
.GetPosition();
3581 if (m_dragCount
!= 3)
3584 if (event
.LeftIsDown())
3586 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3587 &m_dragStart
.x
, &m_dragStart
.y
);
3588 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3589 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3591 // Notify cell about drag
3592 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3593 event
.SetEventObject( m_owner
);
3594 event
.SetItem( item
);
3595 event
.SetModel( model
);
3596 if (!m_owner
->HandleWindowEvent( event
))
3599 if (!event
.IsAllowed())
3602 wxDataObject
*obj
= event
.GetDataObject();
3606 wxDataViewDropSource
drag( this, drag_item_row
);
3607 drag
.SetData( *obj
);
3608 /* wxDragResult res = */ drag
.DoDragDrop();
3617 #endif // wxUSE_DRAG_AND_DROP
3619 bool simulateClick
= false;
3621 if (event
.ButtonDClick())
3623 m_renameTimer
->Stop();
3624 m_lastOnSame
= false;
3627 wxDataViewItem item
= GetItemByRow(current
);
3628 bool ignore_other_columns
=
3629 ((GetOwner()->GetExpanderColumn() != col
) &&
3630 (model
->IsContainer(item
)) &&
3631 (!model
->HasContainerColumns(item
)));
3633 if (event
.LeftDClick())
3635 if(hoverOverExpander
)
3637 // a double click on the expander will be converted into a "simulated" normal click
3638 simulateClick
= true;
3640 else if ( current
== m_lineLastClicked
)
3642 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3645 model
->GetValue( value
, item
, col
->GetModelColumn() );
3646 cell
->SetValue( value
);
3647 wxRect
cell_rect( xpos
, GetLineStart( current
),
3648 col
->GetWidth(), GetLineHeight( current
) );
3649 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3653 wxWindow
*parent
= GetParent();
3654 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3656 le
.SetEventObject(parent
);
3657 le
.SetModel(GetOwner()->GetModel());
3659 parent
->GetEventHandler()->ProcessEvent(le
);
3665 // The first click was on another item, so don't interpret this as
3666 // a double click, but as a simple click instead
3667 simulateClick
= true;
3671 if (event
.LeftUp() && !hoverOverExpander
)
3673 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3675 // select single line
3676 SelectAllRows( false );
3677 SelectRow( m_lineSelectSingleOnUp
, true );
3678 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3681 // If the user click the expander, we do not do editing even if the column
3682 // with expander are editable
3683 if (m_lastOnSame
&& !ignore_other_columns
)
3685 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3686 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3688 m_renameTimer
->Start( 100, true );
3692 m_lastOnSame
= false;
3693 m_lineSelectSingleOnUp
= (unsigned int)-1;
3695 else if(!event
.LeftUp())
3697 // This is necessary, because after a DnD operation in
3698 // from and to ourself, the up event is swallowed by the
3699 // DnD code. So on next non-up event (which means here and
3700 // now) m_lineSelectSingleOnUp should be reset.
3701 m_lineSelectSingleOnUp
= (unsigned int)-1;
3704 if (event
.RightDown())
3706 m_lineBeforeLastClicked
= m_lineLastClicked
;
3707 m_lineLastClicked
= current
;
3709 // If the item is already selected, do not update the selection.
3710 // Multi-selections should not be cleared if a selected item is clicked.
3711 if (!IsRowSelected(current
))
3713 SelectAllRows(false);
3714 ChangeCurrentRow(current
);
3715 SelectRow(m_currentRow
,true);
3716 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3719 else if (event
.RightUp())
3722 model
->GetValue( value
, item
, col
->GetModelColumn() );
3723 wxWindow
*parent
= GetParent();
3724 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3726 le
.SetEventObject(parent
);
3727 le
.SetModel(GetOwner()->GetModel());
3729 parent
->GetEventHandler()->ProcessEvent(le
);
3731 else if (event
.MiddleDown())
3735 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3737 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3739 // hoverOverExpander being true tells us that our node must be
3740 // valid and have children.
3741 // So we don't need any extra checks.
3742 if( node
->IsOpen() )
3747 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3751 m_lineBeforeLastClicked
= m_lineLastClicked
;
3752 m_lineLastClicked
= current
;
3754 unsigned int oldCurrentRow
= m_currentRow
;
3755 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3757 bool cmdModifierDown
= event
.CmdDown();
3758 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3760 if ( IsSingleSel() || !IsRowSelected(current
) )
3762 SelectAllRows( false );
3763 ChangeCurrentRow(current
);
3764 SelectRow(m_currentRow
,true);
3765 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3767 else // multi sel & current is highlighted & no mod keys
3769 m_lineSelectSingleOnUp
= current
;
3770 ChangeCurrentRow(current
); // change focus
3773 else // multi sel & either ctrl or shift is down
3775 if (cmdModifierDown
)
3777 ChangeCurrentRow(current
);
3778 ReverseRowSelection(m_currentRow
);
3779 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3781 else if (event
.ShiftDown())
3783 ChangeCurrentRow(current
);
3785 unsigned int lineFrom
= oldCurrentRow
,
3788 if ( lineTo
< lineFrom
)
3791 lineFrom
= m_currentRow
;
3794 SelectRows(lineFrom
, lineTo
, true);
3795 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3797 else // !ctrl, !shift
3799 // test in the enclosing if should make it impossible
3800 wxFAIL_MSG( wxT("how did we get here?") );
3804 if (m_currentRow
!= oldCurrentRow
)
3805 RefreshRow( oldCurrentRow
);
3807 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3809 // Update selection here...
3812 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3813 (current
== oldCurrentRow
)) && oldWasSelected
;
3815 // Call LeftClick after everything else as under GTK+
3816 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3818 // notify cell about right click
3820 model
->GetValue( value
, item
, col
->GetModelColumn() );
3821 cell
->SetValue( value
);
3822 wxRect
cell_rect( xpos
, GetLineStart( current
),
3823 col
->GetWidth(), GetLineHeight( current
) );
3824 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
,
3825 model
, item
, col
->GetModelColumn());
3830 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3834 if (HasCurrentRow())
3840 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3844 if (HasCurrentRow())
3850 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3852 if( m_selection
.GetCount() != 1 )
3853 return wxDataViewItem();
3855 return GetItemByRow( m_selection
.Item(0));
3858 //-----------------------------------------------------------------------------
3860 //-----------------------------------------------------------------------------
3862 WX_DEFINE_LIST(wxDataViewColumnList
)
3864 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3865 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3866 EVT_SIZE(wxDataViewCtrl::OnSize
)
3869 wxDataViewCtrl::~wxDataViewCtrl()
3872 GetModel()->RemoveNotifier( m_notifier
);
3877 void wxDataViewCtrl::Init()
3879 m_cols
.DeleteContents(true);
3882 // No sorting column at start
3883 m_sortingColumnIdx
= wxNOT_FOUND
;
3885 m_headerArea
= NULL
;
3888 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3889 const wxPoint
& pos
, const wxSize
& size
,
3890 long style
, const wxValidator
& validator
)
3892 // if ( (style & wxBORDER_MASK) == 0)
3893 // style |= wxBORDER_SUNKEN;
3897 if (!wxControl::Create( parent
, id
, pos
, size
,
3898 style
| wxScrolledWindowStyle
, validator
))
3901 SetInitialSize(size
);
3904 MacSetClipChildren( true );
3907 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3909 if (HasFlag(wxDV_NO_HEADER
))
3910 m_headerArea
= NULL
;
3912 m_headerArea
= new wxDataViewHeaderWindow(this);
3914 SetTargetWindow( m_clientArea
);
3916 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3918 sizer
->Add( m_headerArea
, 0, wxGROW
);
3919 sizer
->Add( m_clientArea
, 1, wxGROW
);
3925 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3927 return wxBORDER_THEME
;
3931 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3935 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3938 // we need to process arrows ourselves for scrolling
3939 if ( nMsg
== WM_GETDLGCODE
)
3941 rc
|= DLGC_WANTARROWS
;
3949 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3951 wxSize newsize
= size
;
3952 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3953 newsize
.y
-= m_headerArea
->GetSize().y
;
3958 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3960 // We need to override OnSize so that our scrolled
3961 // window a) does call Layout() to use sizers for
3962 // positioning the controls but b) does not query
3963 // the sizer for their size and use that for setting
3964 // the scrollable area as set that ourselves by
3965 // calling SetScrollbar() further down.
3972 void wxDataViewCtrl::SetFocus()
3975 m_clientArea
->SetFocus();
3978 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3980 if (!wxDataViewCtrlBase::AssociateModel( model
))
3983 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3985 model
->AddNotifier( m_notifier
);
3987 m_clientArea
->DestroyTree();
3989 m_clientArea
->BuildTree(model
);
3991 m_clientArea
->UpdateDisplay();
3996 #if wxUSE_DRAG_AND_DROP
3998 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4000 return m_clientArea
->EnableDragSource( format
);
4003 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4005 return m_clientArea
->EnableDropTarget( format
);
4008 #endif // wxUSE_DRAG_AND_DROP
4010 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4012 if (!wxDataViewCtrlBase::AppendColumn(col
))
4015 m_cols
.Append( col
);
4016 OnColumnsCountChanged();
4020 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4022 if (!wxDataViewCtrlBase::PrependColumn(col
))
4025 m_cols
.Insert( col
);
4026 OnColumnsCountChanged();
4030 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4032 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4035 m_cols
.Insert( pos
, col
);
4036 OnColumnsCountChanged();
4040 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4043 m_headerArea
->UpdateColumn(idx
);
4045 m_clientArea
->UpdateDisplay();
4048 void wxDataViewCtrl::OnColumnsCountChanged()
4051 m_headerArea
->SetColumnCount(GetColumnCount());
4053 m_clientArea
->UpdateDisplay();
4056 void wxDataViewCtrl::DoSetExpanderColumn()
4058 m_clientArea
->UpdateDisplay();
4061 void wxDataViewCtrl::DoSetIndent()
4063 m_clientArea
->UpdateDisplay();
4066 unsigned int wxDataViewCtrl::GetColumnCount() const
4068 return m_cols
.GetCount();
4071 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4076 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4078 // columns can't be reordered if there is no header window which allows
4080 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4083 return GetColumn(idx
);
4086 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4088 const unsigned count
= m_cols
.size();
4089 for ( unsigned n
= 0; n
< count
; n
++ )
4091 if ( m_cols
[n
] == column
)
4098 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4099 unsigned int WXUNUSED(new_pos
))
4101 // do _not_ reorder m_cols elements here, they should always be in the
4102 // order in which columns were added, we only display the columns in
4104 m_clientArea
->UpdateDisplay();
4107 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4109 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4114 OnColumnsCountChanged();
4119 bool wxDataViewCtrl::ClearColumns()
4122 OnColumnsCountChanged();
4126 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4130 unsigned int len
= GetColumnCount();
4131 for ( unsigned int i
= 0; i
< len
; i
++ )
4133 wxDataViewColumn
* col
= GetColumnAt(i
);
4134 if (col
->IsHidden())
4136 ret
+= col
->GetWidth();
4139 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4146 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4148 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4149 : GetColumn(m_sortingColumnIdx
);
4152 // Selection code with wxDataViewItem as parameters
4153 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4155 return m_clientArea
->GetSelection();
4158 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4161 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4162 int len
= selection
.GetCount();
4163 for( int i
= 0; i
< len
; i
++)
4165 unsigned int row
= selection
[i
];
4166 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4171 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4173 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4175 wxDataViewItem last_parent
;
4177 int len
= sel
.GetCount();
4178 for( int i
= 0; i
< len
; i
++ )
4180 wxDataViewItem item
= sel
[i
];
4181 wxDataViewItem parent
= GetModel()->GetParent( item
);
4184 if (parent
!= last_parent
)
4185 ExpandAncestors(item
);
4188 last_parent
= parent
;
4189 int row
= m_clientArea
->GetRowByItem( item
);
4191 selection
.Add( static_cast<unsigned int>(row
) );
4194 m_clientArea
->SetSelections( selection
);
4197 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4199 ExpandAncestors( item
);
4201 int row
= m_clientArea
->GetRowByItem( item
);
4204 // Unselect all rows before select another in the single select mode
4205 if (m_clientArea
->IsSingleSel())
4206 m_clientArea
->SelectAllRows(false);
4207 m_clientArea
->SelectRow(row
, true);
4211 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4213 int row
= m_clientArea
->GetRowByItem( item
);
4215 m_clientArea
->SelectRow(row
, false);
4218 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4220 int row
= m_clientArea
->GetRowByItem( item
);
4223 return m_clientArea
->IsRowSelected(row
);
4228 // Selection code with row number as parameter
4229 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4232 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4233 int len
= selection
.GetCount();
4234 for( int i
= 0; i
< len
; i
++)
4236 unsigned int row
= selection
[i
];
4242 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4244 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4245 int len
= sel
.GetCount();
4246 for( int i
= 0; i
< len
; i
++ )
4250 selection
.Add( static_cast<unsigned int>(row
) );
4252 m_clientArea
->SetSelections( selection
);
4255 void wxDataViewCtrl::Select( int row
)
4259 if (m_clientArea
->IsSingleSel())
4260 m_clientArea
->SelectAllRows(false);
4261 m_clientArea
->SelectRow( row
, true );
4265 void wxDataViewCtrl::Unselect( int row
)
4268 m_clientArea
->SelectRow(row
, false);
4271 bool wxDataViewCtrl::IsSelected( int row
) const
4274 return m_clientArea
->IsRowSelected(row
);
4278 void wxDataViewCtrl::SelectRange( int from
, int to
)
4281 for( int i
= from
; i
< to
; i
++ )
4283 m_clientArea
->Select(sel
);
4286 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4288 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4289 for( int i
= from
; i
< to
; i
++ )
4290 if( sel
.Index( i
) != wxNOT_FOUND
)
4292 m_clientArea
->SetSelections(sel
);
4295 void wxDataViewCtrl::SelectAll()
4297 m_clientArea
->SelectAllRows(true);
4300 void wxDataViewCtrl::UnselectAll()
4302 m_clientArea
->SelectAllRows(false);
4305 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4309 if( row
> (int) m_clientArea
->GetRowCount() )
4310 row
= m_clientArea
->GetRowCount();
4312 int first
= m_clientArea
->GetFirstVisibleRow();
4313 int last
= m_clientArea
->GetLastVisibleRow();
4315 m_clientArea
->ScrollTo( row
, column
);
4316 else if( row
> last
)
4317 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4319 m_clientArea
->ScrollTo( first
, column
);
4322 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4324 ExpandAncestors( item
);
4326 m_clientArea
->RecalculateDisplay();
4328 int row
= m_clientArea
->GetRowByItem(item
);
4331 if( column
== NULL
)
4332 EnsureVisible(row
, -1);
4334 EnsureVisible( row
, GetColumnIndex(column
) );
4339 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4340 wxDataViewColumn
* &column
) const
4342 m_clientArea
->HitTest(point
, item
, column
);
4345 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4346 const wxDataViewColumn
* column
) const
4348 return m_clientArea
->GetItemRect(item
, column
);
4351 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4353 return m_clientArea
->GetItemByRow( row
);
4356 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4358 return m_clientArea
->GetRowByItem( item
);
4361 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4363 int row
= m_clientArea
->GetRowByItem( item
);
4365 m_clientArea
->Expand(row
);
4368 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4370 int row
= m_clientArea
->GetRowByItem( item
);
4372 m_clientArea
->Collapse(row
);
4375 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4377 int row
= m_clientArea
->GetRowByItem( item
);
4379 return m_clientArea
->IsExpanded(row
);
4383 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4385 wxDataViewColumn
* col
= GetColumn( column
);
4389 wxRect itemRect
= GetItemRect(item
, col
);
4390 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4391 renderer
->StartEditing(item
, itemRect
);
4394 #endif // !wxUSE_GENERICDATAVIEWCTRL
4396 #endif // wxUSE_DATAVIEWCTRL