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
)
615 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
618 wxDataViewRenderer::~wxDataViewRenderer()
625 wxDataViewRenderer::RenderWithAttr(wxDC
& dc
,
626 const wxRect
& cell_rect
,
628 const wxDataViewItemAttr
*WXUNUSED(attr
),
631 // adjust the rectangle ourselves to account for the alignment
633 wxRect item_rect
= cell_rect
;
636 const wxSize size
= GetSize();
638 // take alignment into account only if there is enough space, otherwise
639 // show as much contents as possible
641 // notice that many existing renderers (e.g. wxDataViewSpinRenderer)
642 // return hard-coded size which can be more than they need and if we
643 // trusted their GetSize() we'd draw the text out of cell bounds
646 if ( size
.x
< cell_rect
.width
)
648 if (align
& wxALIGN_CENTER_HORIZONTAL
)
649 item_rect
.x
+= (cell_rect
.width
- size
.x
)/2;
650 else if (align
& wxALIGN_RIGHT
)
651 item_rect
.x
+= cell_rect
.width
- size
.x
;
652 // else: wxALIGN_LEFT is the default
654 item_rect
.width
= size
.x
;
657 if ( size
.y
< cell_rect
.height
)
659 if (align
& wxALIGN_CENTER_VERTICAL
)
660 item_rect
.y
+= (cell_rect
.height
- size
.y
)/2;
661 else if (align
& wxALIGN_BOTTOM
)
662 item_rect
.y
+= cell_rect
.height
- size
.y
;
663 // else: wxALIGN_TOP is the default
665 item_rect
.height
= size
.y
;
669 return Render(item_rect
, &dc
, state
);
672 wxDC
*wxDataViewRenderer::GetDC()
676 if (GetOwner() == NULL
)
678 if (GetOwner()->GetOwner() == NULL
)
680 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
686 void wxDataViewRenderer::SetAlignment( int align
)
691 int wxDataViewRenderer::GetAlignment() const
696 int wxDataViewRenderer::CalculateAlignment() const
698 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
700 if (GetOwner() == NULL
)
701 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
703 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
709 // ---------------------------------------------------------
710 // wxDataViewCustomRenderer
711 // ---------------------------------------------------------
713 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
715 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
716 wxDataViewCellMode mode
, int align
) :
717 wxDataViewRenderer( varianttype
, mode
, align
)
722 wxDataViewCustomRenderer::RenderText(wxDC
& dc
,
725 const wxString
& text
,
726 const wxDataViewItemAttr
*attr
,
731 if ( attr
&& attr
->HasColour() )
732 col
= attr
->GetColour();
733 else if ( state
& wxDATAVIEW_CELL_SELECTED
)
734 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
735 else // use default foreground
736 col
= GetOwner()->GetOwner()->GetForegroundColour();
738 wxDCTextColourChanger
changeFg(dc
, col
);
740 wxDCFontChanger
changeFont(dc
);
741 if ( attr
&& attr
->HasFont() )
743 wxFont
font(dc
.GetFont());
744 if ( attr
->GetBold() )
746 if ( attr
->GetItalic() )
749 changeFont
.Set(font
);
752 wxRect rectText
= rect
;
753 rectText
.x
+= xoffset
;
754 rectText
.width
-= xoffset
;
756 // check if we want to ellipsize the text if it doesn't fit
757 wxString ellipsizedText
;
758 if ( GetEllipsizeMode() != wxELLIPSIZE_NONE
)
760 ellipsizedText
= wxControl::Ellipsize
766 wxELLIPSIZE_FLAGS_NONE
770 dc
.DrawLabel(ellipsizedText
.empty() ? text
: ellipsizedText
,
774 // ---------------------------------------------------------
775 // wxDataViewTextRenderer
776 // ---------------------------------------------------------
778 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
780 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
781 wxDataViewCellMode mode
, int align
) :
782 wxDataViewCustomRenderer( varianttype
, mode
, align
)
786 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
788 m_text
= value
.GetString();
793 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
798 bool wxDataViewTextRenderer::HasEditorCtrl() const
803 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
804 wxRect labelRect
, const wxVariant
&value
)
806 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
807 wxPoint(labelRect
.x
,labelRect
.y
),
808 wxSize(labelRect
.width
,labelRect
.height
) );
810 // select the text in the control an place the cursor at the end
811 ctrl
->SetInsertionPointEnd();
817 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
819 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
820 value
= text
->GetValue();
825 wxDataViewTextRenderer::RenderWithAttr(wxDC
& dc
,
828 const wxDataViewItemAttr
*attr
,
831 RenderText(dc
, rect
, align
, m_text
, attr
, state
);
835 wxSize
wxDataViewTextRenderer::GetSize() const
837 const wxDataViewCtrl
*view
= GetView();
839 return view
->wxWindowBase::GetTextExtent( m_text
);
840 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
843 // ---------------------------------------------------------
844 // wxDataViewBitmapRenderer
845 // ---------------------------------------------------------
847 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
849 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
850 wxDataViewCellMode mode
, int align
) :
851 wxDataViewCustomRenderer( varianttype
, mode
, align
)
855 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
857 if (value
.GetType() == wxT("wxBitmap"))
859 if (value
.GetType() == wxT("wxIcon"))
865 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
870 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
873 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
874 else if (m_icon
.Ok())
875 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
880 wxSize
wxDataViewBitmapRenderer::GetSize() const
883 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
884 else if (m_icon
.Ok())
885 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
887 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
890 // ---------------------------------------------------------
891 // wxDataViewToggleRenderer
892 // ---------------------------------------------------------
894 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
896 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
897 wxDataViewCellMode mode
, int align
) :
898 wxDataViewCustomRenderer( varianttype
, mode
, align
)
903 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
905 m_toggle
= value
.GetBool();
910 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
915 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
919 flags
|= wxCONTROL_CHECKED
;
920 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
921 flags
|= wxCONTROL_DISABLED
;
923 wxRendererNative::Get().DrawCheckBox(
924 GetOwner()->GetOwner(),
932 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
933 wxDataViewModel
*model
,
934 const wxDataViewItem
& item
, unsigned int col
)
936 model
->ChangeValue(!m_toggle
, item
, col
);
940 wxSize
wxDataViewToggleRenderer::GetSize() const
942 // the window parameter is not used by GetCheckBoxSize() so it's
944 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
947 // ---------------------------------------------------------
948 // wxDataViewProgressRenderer
949 // ---------------------------------------------------------
951 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
953 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
954 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
955 wxDataViewCustomRenderer( varianttype
, mode
, align
)
961 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
963 m_value
= (long) value
;
965 if (m_value
< 0) m_value
= 0;
966 if (m_value
> 100) m_value
= 100;
971 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
973 value
= (long) m_value
;
977 bool wxDataViewProgressRenderer::RenderWithAttr(wxDC
& dc
,
980 const wxDataViewItemAttr
*attr
,
983 // deflat the rect to leave a small border between bars in adjacent rows
984 wxRect bar
= rect
.Deflate(0, 1);
986 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
987 dc
.SetPen( *wxBLACK_PEN
);
988 dc
.DrawRectangle( bar
);
990 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
991 dc
.SetPen( *wxTRANSPARENT_PEN
);
992 dc
.SetBrush( attr
&& attr
->HasColour() ? wxBrush(attr
->GetColour())
994 dc
.DrawRectangle( bar
);
999 wxSize
wxDataViewProgressRenderer::GetSize() const
1001 return wxSize(40,12);
1004 // ---------------------------------------------------------
1005 // wxDataViewDateRenderer
1006 // ---------------------------------------------------------
1008 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
1010 #if wxUSE_DATE_RENDERER_POPUP
1012 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
1015 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1016 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
1017 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
1022 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
1023 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1024 sizer
->Add( m_cal
, 1, wxGROW
);
1029 void OnCalendar( wxCalendarEvent
&event
);
1031 wxCalendarCtrl
*m_cal
;
1032 wxDataViewModel
*m_model
;
1034 const wxDataViewItem
& m_item
;
1037 virtual void OnDismiss()
1042 DECLARE_EVENT_TABLE()
1045 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1046 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1049 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1051 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
1055 #endif // wxUSE_DATE_RENDERER_POPUP
1057 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1059 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1060 wxDataViewCellMode mode
, int align
) :
1061 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1065 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1067 m_date
= value
.GetDateTime();
1072 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1078 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1080 wxString tmp
= m_date
.FormatDate();
1081 RenderText( tmp
, 0, cell
, dc
, state
);
1085 wxSize
wxDataViewDateRenderer::GetSize() const
1087 const wxDataViewCtrl
* view
= GetView();
1088 wxString tmp
= m_date
.FormatDate();
1090 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1091 return wxSize(x
,y
+d
);
1094 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1095 const wxDataViewItem
& item
, unsigned int col
)
1098 model
->GetValue( variant
, item
, col
);
1099 wxDateTime value
= variant
.GetDateTime();
1101 #if wxUSE_DATE_RENDERER_POPUP
1102 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1103 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1104 wxPoint pos
= wxGetMousePosition();
1107 popup
->Popup( popup
->m_cal
);
1108 #else // !wxUSE_DATE_RENDERER_POPUP
1109 wxMessageBox(value
.Format());
1110 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1114 // ---------------------------------------------------------
1115 // wxDataViewIconTextRenderer
1116 // ---------------------------------------------------------
1118 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1120 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1121 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1122 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1125 SetAlignment(align
);
1128 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1134 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1140 wxDataViewIconTextRenderer::RenderWithAttr(wxDC
& dc
,
1143 const wxDataViewItemAttr
*attr
,
1148 const wxIcon
& icon
= m_value
.GetIcon();
1151 dc
.DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1152 xoffset
= icon
.GetWidth()+4;
1155 RenderText(dc
, rect
, align
, m_value
.GetText(), attr
, state
, xoffset
);
1160 wxSize
wxDataViewIconTextRenderer::GetSize() const
1162 const wxDataViewCtrl
*view
= GetView();
1163 if (!m_value
.GetText().empty())
1166 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1168 if (m_value
.GetIcon().IsOk())
1169 x
+= m_value
.GetIcon().GetWidth() + 4;
1170 return wxSize( x
, y
);
1172 return wxSize(80,20);
1175 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1177 wxDataViewIconText iconText
;
1180 wxString text
= iconText
.GetText();
1182 // adjust the label rect to take the width of the icon into account
1183 if (iconText
.GetIcon().IsOk())
1185 int w
= iconText
.GetIcon().GetWidth() + 4;
1187 labelRect
.width
-= w
;
1190 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1191 wxPoint(labelRect
.x
,labelRect
.y
),
1192 wxSize(labelRect
.width
,labelRect
.height
) );
1194 // select the text in the control an place the cursor at the end
1195 ctrl
->SetInsertionPointEnd();
1201 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
& value
)
1203 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1205 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1210 //-----------------------------------------------------------------------------
1211 // wxDataViewDropTarget
1212 //-----------------------------------------------------------------------------
1214 #if wxUSE_DRAG_AND_DROP
1216 class wxBitmapCanvas
: public wxWindow
1219 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1220 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1223 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1226 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1229 dc
.DrawBitmap( m_bitmap
, 0, 0);
1235 class wxDataViewDropSource
: public wxDropSource
1238 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1246 ~wxDataViewDropSource()
1251 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1253 wxPoint pos
= wxGetMousePosition();
1257 int liney
= m_win
->GetLineStart( m_row
);
1259 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1260 m_win
->ClientToScreen( &linex
, &liney
);
1261 m_dist_x
= pos
.x
- linex
;
1262 m_dist_y
= pos
.y
- liney
;
1265 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1267 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1268 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1270 wxFRAME_TOOL_WINDOW
|
1271 wxFRAME_FLOAT_ON_PARENT
|
1272 wxFRAME_NO_TASKBAR
|
1274 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1279 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1280 m_hint
->SetTransparent( 128 );
1286 wxDataViewMainWindow
*m_win
;
1289 int m_dist_x
,m_dist_y
;
1293 class wxDataViewDropTarget
: public wxDropTarget
1296 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1302 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1304 wxDataFormat format
= GetMatchingPair();
1305 if (format
== wxDF_INVALID
)
1307 return m_win
->OnDragOver( format
, x
, y
, def
);
1310 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1312 wxDataFormat format
= GetMatchingPair();
1313 if (format
== wxDF_INVALID
)
1315 return m_win
->OnDrop( format
, x
, y
);
1318 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1320 wxDataFormat format
= GetMatchingPair();
1321 if (format
== wxDF_INVALID
)
1325 return m_win
->OnData( format
, x
, y
, def
);
1328 virtual void OnLeave()
1329 { m_win
->OnLeave(); }
1331 wxDataViewMainWindow
*m_win
;
1334 #endif // wxUSE_DRAG_AND_DROP
1336 //-----------------------------------------------------------------------------
1337 // wxDataViewRenameTimer
1338 //-----------------------------------------------------------------------------
1340 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1345 void wxDataViewRenameTimer::Notify()
1347 m_owner
->OnRenameTimer();
1350 //-----------------------------------------------------------------------------
1351 // wxDataViewMainWindow
1352 //-----------------------------------------------------------------------------
1354 // The tree building helper, declared firstly
1355 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
1356 wxDataViewTreeNode
* node
);
1358 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1360 if (row1
> row2
) return 1;
1361 if (row1
== row2
) return 0;
1365 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1367 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1368 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1369 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1370 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1371 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1372 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1375 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1376 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1377 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1378 m_selection( wxDataViewSelectionCmp
)
1383 m_lastOnSame
= false;
1384 m_renameTimer
= new wxDataViewRenameTimer( this );
1386 // TODO: user better initial values/nothing selected
1387 m_currentCol
= NULL
;
1390 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1392 #if wxUSE_DRAG_AND_DROP
1394 m_dragStart
= wxPoint(0,0);
1396 m_dragEnabled
= false;
1397 m_dropEnabled
= false;
1399 m_dropHintLine
= (unsigned int) -1;
1400 #endif // wxUSE_DRAG_AND_DROP
1402 m_lineLastClicked
= (unsigned int) -1;
1403 m_lineBeforeLastClicked
= (unsigned int) -1;
1404 m_lineSelectSingleOnUp
= (unsigned int) -1;
1408 SetBackgroundColour( *wxWHITE
);
1410 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1412 m_penRule
= wxPen(GetRuleColour());
1414 // compose a pen whichcan draw black lines
1415 // TODO: maybe there is something system colour to use
1416 m_penExpander
= wxPen(wxColour(0,0,0));
1418 m_root
= new wxDataViewTreeNode( NULL
);
1419 m_root
->SetHasChildren(true);
1421 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1423 m_underMouse
= NULL
;
1427 wxDataViewMainWindow::~wxDataViewMainWindow()
1430 delete m_renameTimer
;
1434 #if wxUSE_DRAG_AND_DROP
1435 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1437 m_dragFormat
= format
;
1438 m_dragEnabled
= format
!= wxDF_INVALID
;
1443 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1445 m_dropFormat
= format
;
1446 m_dropEnabled
= format
!= wxDF_INVALID
;
1449 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1454 void wxDataViewMainWindow::RemoveDropHint()
1459 RefreshRow( m_dropHintLine
);
1460 m_dropHintLine
= (unsigned int) -1;
1464 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1465 wxCoord y
, wxDragResult def
)
1469 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1470 unsigned int row
= GetLineAt( yy
);
1472 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1478 wxDataViewItem item
= GetItemByRow( row
);
1480 wxDataViewModel
*model
= GetOwner()->GetModel();
1482 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1483 event
.SetEventObject( m_owner
);
1484 event
.SetItem( item
);
1485 event
.SetModel( model
);
1486 event
.SetDataFormat( format
);
1487 if (!m_owner
->HandleWindowEvent( event
))
1493 if (!event
.IsAllowed())
1500 if (m_dropHint
&& (row
!= m_dropHintLine
))
1501 RefreshRow( m_dropHintLine
);
1503 m_dropHintLine
= row
;
1509 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1515 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1516 unsigned int row
= GetLineAt( yy
);
1518 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1521 wxDataViewItem item
= GetItemByRow( row
);
1523 wxDataViewModel
*model
= GetOwner()->GetModel();
1525 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1526 event
.SetEventObject( m_owner
);
1527 event
.SetItem( item
);
1528 event
.SetModel( model
);
1529 event
.SetDataFormat( format
);
1530 if (!m_owner
->HandleWindowEvent( event
))
1533 if (!event
.IsAllowed())
1539 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1544 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1545 unsigned int row
= GetLineAt( yy
);
1547 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1550 wxDataViewItem item
= GetItemByRow( row
);
1552 wxDataViewModel
*model
= GetOwner()->GetModel();
1554 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1556 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1557 event
.SetEventObject( m_owner
);
1558 event
.SetItem( item
);
1559 event
.SetModel( model
);
1560 event
.SetDataFormat( format
);
1561 event
.SetDataSize( obj
->GetSize() );
1562 event
.SetDataBuffer( obj
->GetData() );
1563 if (!m_owner
->HandleWindowEvent( event
))
1566 if (!event
.IsAllowed())
1572 void wxDataViewMainWindow::OnLeave()
1577 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1579 int height
= GetLineHeight( row
);
1581 unsigned int cols
= GetOwner()->GetColumnCount();
1583 for (col
= 0; col
< cols
; col
++)
1585 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1586 if (column
->IsHidden())
1587 continue; // skip it!
1588 width
+= column
->GetWidth();
1592 if (!IsVirtualList())
1594 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1595 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1596 indent
= indent
+ m_lineHeight
;
1597 // try to use the m_lineHeight as the expander space
1599 if(!node
->HasChildren())
1604 wxBitmap
bitmap( width
, height
);
1605 wxMemoryDC
dc( bitmap
);
1606 dc
.SetFont( GetFont() );
1607 dc
.SetPen( *wxBLACK_PEN
);
1608 dc
.SetBrush( *wxWHITE_BRUSH
);
1609 dc
.DrawRectangle( 0,0,width
,height
);
1611 wxDataViewModel
*model
= m_owner
->GetModel();
1613 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1616 // TODO-RTL: last column for RTL support
1617 expander
= GetOwner()->GetColumnAt( 0 );
1618 GetOwner()->SetExpanderColumn(expander
);
1623 for (col
= 0; col
< cols
; col
++)
1625 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1626 wxDataViewRenderer
*cell
= column
->GetRenderer();
1628 if (column
->IsHidden())
1629 continue; // skip it!
1631 width
= column
->GetWidth();
1633 if (column
== expander
)
1637 wxDataViewItem item
= GetItemByRow( row
);
1638 model
->GetValue( value
, item
, column
->GetModelColumn());
1639 cell
->SetValue( value
);
1641 wxRect
item_rect(x
, 0, width
, height
);
1642 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1644 // dc.SetClippingRegion( item_rect );
1645 wxDataViewItemAttr attr
;
1647 hasAttr
= model
->GetAttr(item
, column
->GetModelColumn(), attr
);
1648 cell
->RenderWithAttr(dc
, item_rect
, cell
->CalculateAlignment(),
1649 hasAttr
? &attr
: NULL
, 0);
1650 // dc.DestroyClippingRegion();
1658 #endif // wxUSE_DRAG_AND_DROP
1661 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1663 wxDataViewModel
*model
= GetOwner()->GetModel();
1664 wxAutoBufferedPaintDC
dc( this );
1667 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1668 dc
.SetPen( *wxTRANSPARENT_PEN
);
1669 dc
.DrawRectangle(GetClientSize());
1673 GetOwner()->PrepareDC( dc
);
1674 dc
.SetFont( GetFont() );
1676 wxRect update
= GetUpdateRegion().GetBox();
1677 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1679 // compute which items needs to be redrawn
1680 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1681 unsigned int item_count
=
1682 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1683 (int)(GetRowCount( ) - item_start
));
1684 unsigned int item_last
= item_start
+ item_count
;
1685 // Get the parent of DataViewCtrl
1686 wxWindow
*parent
= GetParent()->GetParent();
1687 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1688 cache_event
.SetEventObject(GetParent());
1689 cache_event
.SetCache(item_start
, item_last
- 1);
1690 parent
->ProcessWindowEvent(cache_event
);
1692 // compute which columns needs to be redrawn
1693 unsigned int cols
= GetOwner()->GetColumnCount();
1694 unsigned int col_start
= 0;
1695 unsigned int x_start
;
1696 for (x_start
= 0; col_start
< cols
; col_start
++)
1698 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1699 if (col
->IsHidden())
1700 continue; // skip it!
1702 unsigned int w
= col
->GetWidth();
1703 if (x_start
+w
>= (unsigned int)update
.x
)
1709 unsigned int col_last
= col_start
;
1710 unsigned int x_last
= x_start
;
1711 for (; col_last
< cols
; col_last
++)
1713 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1714 if (col
->IsHidden())
1715 continue; // skip it!
1717 if (x_last
> (unsigned int)update
.GetRight())
1720 x_last
+= col
->GetWidth();
1723 // Draw horizontal rules if required
1724 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1726 dc
.SetPen(m_penRule
);
1727 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1729 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1731 int y
= GetLineStart( i
);
1732 dc
.DrawLine(x_start
, y
, x_last
, y
);
1736 // Draw vertical rules if required
1737 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1739 dc
.SetPen(m_penRule
);
1740 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1743 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1745 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1746 if (col
->IsHidden())
1747 continue; // skip it
1749 dc
.DrawLine(x
, GetLineStart( item_start
),
1750 x
, GetLineStart( item_last
) );
1752 x
+= col
->GetWidth();
1755 // Draw last vertical rule
1756 dc
.DrawLine(x
, GetLineStart( item_start
),
1757 x
, GetLineStart( item_last
) );
1760 // redraw the background for the items which are selected/current
1761 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1763 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1764 if (selected
|| item
== m_currentRow
)
1766 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1767 if (item
== m_currentRow
)
1768 flags
|= wxCONTROL_CURRENT
;
1770 flags
|= wxCONTROL_FOCUSED
;
1772 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1773 wxRendererNative::Get().DrawItemSelectionRect
1783 #if wxUSE_DRAG_AND_DROP
1786 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1787 x_last
, GetLineHeight( m_dropHintLine
) );
1788 dc
.SetPen( *wxBLACK_PEN
);
1789 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1790 dc
.DrawRectangle( rect
);
1792 #endif // wxUSE_DRAG_AND_DROP
1794 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1797 // TODO-RTL: last column for RTL support
1798 expander
= GetOwner()->GetColumnAt( 0 );
1799 GetOwner()->SetExpanderColumn(expander
);
1802 // redraw all cells for all rows which must be repainted and all columns
1804 cell_rect
.x
= x_start
;
1805 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1807 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1808 wxDataViewRenderer
*cell
= col
->GetRenderer();
1809 cell_rect
.width
= col
->GetWidth();
1811 if (col
->IsHidden())
1812 continue; // skip it!
1814 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1816 // get the cell value and set it into the renderer
1818 wxDataViewTreeNode
*node
= NULL
;
1819 wxDataViewItem dataitem
;
1821 if (!IsVirtualList())
1823 node
= GetTreeNodeByRow(item
);
1827 dataitem
= node
->GetItem();
1829 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1830 !model
->HasContainerColumns(dataitem
))
1835 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1838 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1839 cell
->SetValue( value
);
1842 cell_rect
.y
= GetLineStart( item
);
1843 cell_rect
.height
= GetLineHeight( item
);
1845 // Draw the expander here.
1847 if ((!IsVirtualList()) && (col
== expander
))
1849 indent
= node
->GetIndentLevel();
1851 // Calculate the indent first
1852 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1854 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1856 // change the cell_rect.x to the appropriate pos
1857 int expander_x
= indent
+ EXPANDER_MARGIN
;
1858 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2)
1859 - (expander_width
/2) - EXPANDER_OFFSET
;
1861 indent
= indent
+ m_lineHeight
;
1862 // try to use the m_lineHeight as the expander space
1864 dc
.SetPen( m_penExpander
);
1865 dc
.SetBrush( wxNullBrush
);
1866 if( node
->HasChildren() )
1868 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1870 if (m_underMouse
== node
)
1872 flag
|= wxCONTROL_CURRENT
;
1874 if( node
->IsOpen() )
1875 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
,
1876 flag
|wxCONTROL_EXPANDED
);
1878 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1881 // force the expander column to left-center align
1882 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1884 if (node
&& !node
->HasChildren())
1886 // Yes, if the node does not have any child, it must be a leaf which
1887 // mean that it is a temporarily created by GetTreeNodeByRow
1891 wxRect item_rect
= cell_rect
;
1892 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1894 // account for the tree indent (harmless if we're not indented)
1895 item_rect
.x
+= indent
;
1896 item_rect
.width
-= indent
;
1899 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1900 state
|= wxDATAVIEW_CELL_SELECTED
;
1902 // TODO: it would be much more efficient to create a clipping
1903 // region for the entire column being rendered (in the OnPaint
1904 // of wxDataViewMainWindow) instead of a single clip region for
1905 // each cell. However it would mean that each renderer should
1906 // respect the given wxRect's top & bottom coords, eventually
1907 // violating only the left & right coords - however the user can
1908 // make its own renderer and thus we cannot be sure of that.
1909 wxDCClipper
clip(dc
, item_rect
);
1911 wxDataViewItemAttr attr
;
1913 hasAttr
= model
->GetAttr(dataitem
, col
->GetModelColumn(), attr
);
1914 cell
->RenderWithAttr(dc
, item_rect
, cell
->CalculateAlignment(),
1915 hasAttr
? &attr
: NULL
, state
);
1918 cell_rect
.x
+= cell_rect
.width
;
1922 void wxDataViewMainWindow::OnRenameTimer()
1924 // We have to call this here because changes may just have
1925 // been made and no screen update taken place.
1928 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1929 // (needs to be tested!)
1933 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1935 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1937 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1940 //-----------------------------------------------------------------------------
1941 // Helper class for do operation on the tree node
1942 //-----------------------------------------------------------------------------
1947 virtual ~DoJob() { }
1949 // The return value control how the tree-walker tranverse the tree
1950 // 0: Job done, stop tranverse and return
1951 // 1: Ignore the current node's subtree and continue
1952 // 2: Job not done, continue
1953 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1954 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1955 virtual int operator() ( void * n
) = 0;
1958 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1963 switch( func( node
) )
1974 wxDataViewTreeNodes nodes
= node
->GetNodes();
1975 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1977 int len_nodes
= nodes
.GetCount();
1978 int len
= leaves
.GetCount();
1979 int i
= 0, nodes_i
= 0;
1981 for(; i
< len
; i
++ )
1983 void * n
= leaves
[i
];
1984 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1986 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1989 if( Walker( nd
, func
) )
2008 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2010 if (IsVirtualList())
2012 wxDataViewVirtualListModel
*list_model
=
2013 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
2014 m_count
= list_model
->GetCount();
2021 wxDataViewTreeNode
* node
;
2022 node
= FindNode(parent
);
2027 node
->SetHasChildren( true );
2029 if( g_model
->IsContainer( item
) )
2031 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2032 newnode
->SetItem(item
);
2033 newnode
->SetHasChildren( true );
2034 node
->AddNode( newnode
);
2037 node
->AddLeaf( item
.GetID() );
2039 node
->ChangeSubTreeCount(1);
2047 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2049 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2050 const wxDataViewItem
& item
)
2052 if (IsVirtualList())
2054 wxDataViewVirtualListModel
*list_model
=
2055 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
2056 m_count
= list_model
->GetCount();
2058 if( m_currentRow
> GetRowCount() )
2059 m_currentRow
= m_count
- 1;
2061 // TODO: why empty the entire selection?
2062 m_selection
.Empty();
2069 wxDataViewTreeNode
* node
= FindNode(parent
);
2071 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2072 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
,
2073 false, "item not found" );
2076 node
->GetChildren().Remove( item
.GetID() );
2077 // Manipolate selection
2078 if( m_selection
.GetCount() > 1 )
2080 m_selection
.Empty();
2082 bool isContainer
= false;
2083 wxDataViewTreeNodes nds
= node
->GetNodes();
2084 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2086 if (nds
[i
]->GetItem() == item
)
2094 wxDataViewTreeNode
* n
= NULL
;
2095 wxDataViewTreeNodes nodes
= node
->GetNodes();
2096 int len
= nodes
.GetCount();
2097 for( int i
= 0; i
< len
; i
++)
2099 if( nodes
[i
]->GetItem() == item
)
2106 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2108 node
->GetNodes().Remove( n
);
2109 sub
-= n
->GetSubTreeCount();
2110 ::DestroyTreeHelper(n
);
2112 // Make the row number invalid and get a new valid one when user call GetRowCount
2114 node
->ChangeSubTreeCount(sub
);
2116 // Change the current row to the last row if the current exceed the max row number
2117 if( m_currentRow
> GetRowCount() )
2118 m_currentRow
= m_count
- 1;
2125 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2131 wxWindow
*parent
= GetParent();
2132 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2133 le
.SetEventObject(parent
);
2134 le
.SetModel(GetOwner()->GetModel());
2136 parent
->GetEventHandler()->ProcessEvent(le
);
2141 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2143 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2144 /*#define MAX_VIRTUAL_WIDTH 100000
2146 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2147 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2148 Refresh( true, &rect );
2156 wxWindow
*parent
= GetParent();
2157 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2158 le
.SetEventObject(parent
);
2159 le
.SetModel(GetOwner()->GetModel());
2162 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2163 parent
->GetEventHandler()->ProcessEvent(le
);
2168 bool wxDataViewMainWindow::Cleared()
2173 BuildTree( GetOwner()->GetModel() );
2180 void wxDataViewMainWindow::UpdateDisplay()
2185 void wxDataViewMainWindow::OnInternalIdle()
2187 wxWindow::OnInternalIdle();
2191 RecalculateDisplay();
2196 void wxDataViewMainWindow::RecalculateDisplay()
2198 wxDataViewModel
*model
= GetOwner()->GetModel();
2205 int width
= GetEndOfLastCol();
2206 int height
= GetLineStart( GetRowCount() );
2208 SetVirtualSize( width
, height
);
2209 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2214 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2216 wxWindow::ScrollWindow( dx
, dy
, rect
);
2218 if (GetOwner()->m_headerArea
)
2219 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2222 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2225 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2226 int sy
= GetLineStart( rows
)/y
;
2230 wxRect rect
= GetClientRect();
2234 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2235 for (x_start
= 0; colnum
< column
; colnum
++)
2237 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2238 if (col
->IsHidden())
2239 continue; // skip it!
2241 w
= col
->GetWidth();
2245 int x_end
= x_start
+ w
;
2246 xe
= xx
+ rect
.width
;
2249 sx
= ( xx
+ x_end
- xe
)/x
;
2256 m_owner
->Scroll( sx
, sy
);
2259 int wxDataViewMainWindow::GetCountPerPage() const
2261 wxSize size
= GetClientSize();
2262 return size
.y
/ m_lineHeight
;
2265 int wxDataViewMainWindow::GetEndOfLastCol() const
2269 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2271 const wxDataViewColumn
*c
=
2272 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2275 width
+= c
->GetWidth();
2280 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2284 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2286 return GetLineAt( y
);
2289 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2291 wxSize client_size
= GetClientSize();
2292 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2293 &client_size
.x
, &client_size
.y
);
2295 // we should deal with the pixel here
2296 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2298 return wxMin( GetRowCount()-1, row
);
2301 unsigned int wxDataViewMainWindow::GetRowCount()
2303 if ( m_count
== -1 )
2305 m_count
= RecalculateCount();
2311 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2318 void wxDataViewMainWindow::SelectAllRows( bool on
)
2325 m_selection
.Clear();
2326 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2327 m_selection
.Add( i
);
2332 unsigned int first_visible
= GetFirstVisibleRow();
2333 unsigned int last_visible
= GetLastVisibleRow();
2335 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2337 unsigned int row
= m_selection
[i
];
2338 if ((row
>= first_visible
) && (row
<= last_visible
))
2341 m_selection
.Clear();
2345 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2347 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2351 m_selection
.Add( row
);
2359 m_selection
.Remove( row
);
2365 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2369 unsigned int tmp
= from
;
2375 for (i
= from
; i
<= to
; i
++)
2377 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2380 m_selection
.Add( i
);
2385 m_selection
.Remove( i
);
2388 RefreshRows( from
, to
);
2391 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2393 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2395 int n
= aSelections
[i
];
2397 m_selection
.Add( n
);
2402 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2404 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2405 m_selection
.Add( row
);
2407 m_selection
.Remove( row
);
2411 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2413 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2416 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2418 wxWindow
*parent
= GetParent();
2419 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2421 le
.SetEventObject(parent
);
2422 le
.SetModel(GetOwner()->GetModel());
2425 parent
->GetEventHandler()->ProcessEvent(le
);
2428 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2430 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2431 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2433 wxSize client_size
= GetClientSize();
2434 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2435 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2436 if (intersect_rect
.width
> 0)
2437 Refresh( true, &intersect_rect
);
2440 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2444 unsigned int tmp
= to
;
2449 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2450 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2452 wxSize client_size
= GetClientSize();
2453 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2454 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2455 if (intersect_rect
.width
> 0)
2456 Refresh( true, &intersect_rect
);
2459 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2461 wxSize client_size
= GetClientSize();
2462 int start
= GetLineStart( firstRow
);
2463 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2464 if (start
> client_size
.y
) return;
2466 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2468 Refresh( true, &rect
);
2471 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2473 wxCHECK_RET( newCurrent
< GetRowCount(),
2474 wxT("invalid item index in OnArrowChar()") );
2476 // if there is no selection, we cannot move it anywhere
2477 if (!HasCurrentRow())
2480 unsigned int oldCurrent
= m_currentRow
;
2482 // in single selection we just ignore Shift as we can't select several
2484 if ( event
.ShiftDown() && !IsSingleSel() )
2486 RefreshRow( oldCurrent
);
2488 ChangeCurrentRow( newCurrent
);
2490 // select all the items between the old and the new one
2491 if ( oldCurrent
> newCurrent
)
2493 newCurrent
= oldCurrent
;
2494 oldCurrent
= m_currentRow
;
2497 SelectRows( oldCurrent
, newCurrent
, true );
2498 if (oldCurrent
!=newCurrent
)
2499 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2503 RefreshRow( oldCurrent
);
2505 // all previously selected items are unselected unless ctrl is held
2506 if ( !event
.ControlDown() )
2507 SelectAllRows(false);
2509 ChangeCurrentRow( newCurrent
);
2511 if ( !event
.ControlDown() )
2513 SelectRow( m_currentRow
, true );
2514 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2517 RefreshRow( m_currentRow
);
2520 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2523 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2527 rect
.y
= GetLineStart( row
);
2528 rect
.width
= GetEndOfLastCol();
2529 rect
.height
= GetLineHeight( row
);
2534 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2536 const wxDataViewModel
*model
= GetOwner()->GetModel();
2538 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2540 // TODO make more efficient
2545 for (r
= 0; r
< row
; r
++)
2547 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2548 if (!node
) return start
;
2550 wxDataViewItem item
= node
->GetItem();
2552 if (node
&& !node
->HasChildren())
2554 // Yes, if the node does not have any child, it must be a leaf which
2555 // mean that it is a temporarily created by GetTreeNodeByRow
2559 unsigned int cols
= GetOwner()->GetColumnCount();
2561 int height
= m_lineHeight
;
2562 for (col
= 0; col
< cols
; col
++)
2564 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2565 if (column
->IsHidden())
2566 continue; // skip it!
2569 model
->IsContainer(item
) &&
2570 !model
->HasContainerColumns(item
))
2571 continue; // skip it!
2574 model
->GetValue( value
, item
, column
->GetModelColumn() );
2576 wxDataViewRenderer
*renderer
=
2577 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2578 renderer
->SetValue( value
);
2579 height
= wxMax( height
, renderer
->GetSize().y
);
2589 return row
* m_lineHeight
;
2593 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2595 const wxDataViewModel
*model
= GetOwner()->GetModel();
2597 // check for the easy case first
2598 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2599 return y
/ m_lineHeight
;
2601 // TODO make more efficient
2602 unsigned int row
= 0;
2603 unsigned int yy
= 0;
2606 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2609 // not really correct...
2610 return row
+ ((y
-yy
) / m_lineHeight
);
2613 wxDataViewItem item
= node
->GetItem();
2615 if (node
&& !node
->HasChildren())
2617 // Yes, if the node does not have any child, it must be a leaf which
2618 // mean that it is a temporarily created by GetTreeNodeByRow
2622 unsigned int cols
= GetOwner()->GetColumnCount();
2624 int height
= m_lineHeight
;
2625 for (col
= 0; col
< cols
; col
++)
2627 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2628 if (column
->IsHidden())
2629 continue; // skip it!
2632 model
->IsContainer(item
) &&
2633 !model
->HasContainerColumns(item
))
2634 continue; // skip it!
2637 model
->GetValue( value
, item
, column
->GetModelColumn() );
2639 wxDataViewRenderer
*renderer
=
2640 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2641 renderer
->SetValue( value
);
2642 height
= wxMax( height
, renderer
->GetSize().y
);
2653 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2655 const wxDataViewModel
*model
= GetOwner()->GetModel();
2657 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2659 wxASSERT( !IsVirtualList() );
2661 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2662 // wxASSERT( node );
2663 if (!node
) return m_lineHeight
;
2665 wxDataViewItem item
= node
->GetItem();
2667 if (node
&& !node
->HasChildren())
2669 // Yes, if the node does not have any child, it must be a leaf which
2670 // mean that it is a temporarily created by GetTreeNodeByRow
2674 int height
= m_lineHeight
;
2676 unsigned int cols
= GetOwner()->GetColumnCount();
2678 for (col
= 0; col
< cols
; col
++)
2680 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2681 if (column
->IsHidden())
2682 continue; // skip it!
2685 model
->IsContainer(item
) &&
2686 !model
->HasContainerColumns(item
))
2687 continue; // skip it!
2690 model
->GetValue( value
, item
, column
->GetModelColumn() );
2692 wxDataViewRenderer
*renderer
=
2693 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2694 renderer
->SetValue( value
);
2695 height
= wxMax( height
, renderer
->GetSize().y
);
2702 return m_lineHeight
;
2706 class RowToItemJob
: public DoJob
2709 RowToItemJob( unsigned int row
, int current
)
2710 { this->row
= row
; this->current
= current
; }
2711 virtual ~RowToItemJob() {}
2713 virtual int operator() ( wxDataViewTreeNode
* node
)
2716 if( current
== static_cast<int>(row
))
2718 ret
= node
->GetItem();
2722 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2724 current
+= node
->GetSubTreeCount();
2729 // If the current has no child node, we can find the desired item of the row
2731 // This if can speed up finding in some case, and will has a very good effect
2732 // when it comes to list view
2733 if( node
->GetNodes().GetCount() == 0)
2735 int index
= static_cast<int>(row
) - current
- 1;
2736 ret
= node
->GetChildren().Item( index
);
2743 virtual int operator() ( void * n
)
2746 if( current
== static_cast<int>(row
))
2748 ret
= wxDataViewItem( n
);
2754 wxDataViewItem
GetResult() const
2763 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2765 if (IsVirtualList())
2767 return wxDataViewItem( wxUIntToPtr(row
+1) );
2771 RowToItemJob
job( row
, -2 );
2772 Walker( m_root
, job
);
2773 return job
.GetResult();
2777 class RowToTreeNodeJob
: public DoJob
2780 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2783 this->current
= current
;
2787 virtual ~RowToTreeNodeJob(){ }
2789 virtual int operator() ( wxDataViewTreeNode
* node
)
2792 if( current
== static_cast<int>(row
))
2798 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2800 current
+= node
->GetSubTreeCount();
2807 // If the current node has no children, we can find the desired item of the
2808 // row number directly.
2809 // This if can speed up finding in some case, and will have a very good
2810 // effect for list views.
2811 if( node
->GetNodes().GetCount() == 0)
2813 int index
= static_cast<int>(row
) - current
- 1;
2814 void * n
= node
->GetChildren().Item( index
);
2815 ret
= new wxDataViewTreeNode( parent
);
2816 ret
->SetItem( wxDataViewItem( n
));
2817 ret
->SetHasChildren(false);
2824 virtual int operator() ( void * n
)
2827 if( current
== static_cast<int>(row
))
2829 ret
= new wxDataViewTreeNode( parent
);
2830 ret
->SetItem( wxDataViewItem( n
));
2831 ret
->SetHasChildren(false);
2838 wxDataViewTreeNode
* GetResult() const
2844 wxDataViewTreeNode
* ret
;
2845 wxDataViewTreeNode
* parent
;
2848 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2850 wxASSERT( !IsVirtualList() );
2852 RowToTreeNodeJob
job( row
, -2, m_root
);
2853 Walker( m_root
, job
);
2854 return job
.GetResult();
2857 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2858 const wxDataViewItem
& item
)
2860 wxWindow
*parent
= GetParent();
2861 wxDataViewEvent
le(type
, parent
->GetId());
2863 le
.SetEventObject(parent
);
2864 le
.SetModel(GetOwner()->GetModel());
2867 parent
->GetEventHandler()->ProcessEvent(le
);
2871 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2873 if (IsVirtualList())
2876 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2880 if (!node
->HasChildren())
2886 return node
->IsOpen();
2889 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2891 if (IsVirtualList())
2894 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2898 if (!node
->HasChildren())
2907 void wxDataViewMainWindow::Expand( unsigned int row
)
2909 if (IsVirtualList())
2912 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2916 if (!node
->HasChildren())
2922 if (!node
->IsOpen())
2925 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2927 // Check if the user prevent expanding
2928 if( e
.GetSkipped() )
2933 // build the children of current node
2934 if( node
->GetChildrenNumber() == 0 )
2937 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2940 // By expanding the node all row indices that are currently in the selection list
2941 // and are greater than our node have become invalid. So we have to correct that now.
2942 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2943 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2945 const unsigned testRow
= m_selection
[i
];
2946 // all rows above us are not affected, so skip them
2950 m_selection
[i
] += rowAdjustment
;
2953 if(m_currentRow
> row
)
2954 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2958 // Send the expanded event
2959 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2963 void wxDataViewMainWindow::Collapse(unsigned int row
)
2965 if (IsVirtualList())
2968 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2972 if (!node
->HasChildren())
2981 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2982 if( e
.GetSkipped() )
2985 // Find out if there are selected items below the current node.
2986 bool selectCollapsingRow
= false;
2987 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2988 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2989 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2991 const unsigned testRow
= m_selection
[i
];
2992 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2994 selectCollapsingRow
= true;
2995 // get out as soon as we have found a node that is selected
3002 // If the node to be closed has selected items the user won't see those any longer.
3003 // We select the collapsing node in this case.
3004 if(selectCollapsingRow
)
3006 SelectAllRows(false);
3007 ChangeCurrentRow(row
);
3008 SelectRow(row
, true);
3009 SendSelectionChangedEvent(GetItemByRow(row
));
3013 // if there were no selected items below our node we still need to "fix" the
3014 // selection list to adjust for the changing of the row indices.
3015 // We actually do the opposite of what we are doing in Expand().
3016 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3018 const unsigned testRow
= m_selection
[i
];
3019 // all rows above us are not affected, so skip them
3023 m_selection
[i
] -= rowAdjustment
;
3026 // if the "current row" is being collapsed away we change it to the current row ;-)
3027 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3028 ChangeCurrentRow(row
);
3029 else if(m_currentRow
> row
)
3030 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3035 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3039 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3041 wxDataViewModel
* model
= GetOwner()->GetModel();
3048 // Compose the a parent-chain of the finding item
3050 list
.DeleteContents( true );
3051 wxDataViewItem
it( item
);
3054 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3055 list
.Insert( pItem
);
3056 it
= model
->GetParent( it
);
3059 // Find the item along the parent-chain.
3060 // This algorithm is designed to speed up the node-finding method
3061 wxDataViewTreeNode
* node
= m_root
;
3062 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
3064 if( node
->HasChildren() )
3066 if( node
->GetChildrenNumber() == 0 )
3069 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3072 wxDataViewTreeNodes nodes
= node
->GetNodes();
3076 for (i
= 0; i
< nodes
.GetCount(); i
++)
3078 if (nodes
[i
]->GetItem() == (**iter
))
3080 if (nodes
[i
]->GetItem() == item
)
3097 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3098 wxDataViewColumn
* &column
)
3100 wxDataViewColumn
*col
= NULL
;
3101 unsigned int cols
= GetOwner()->GetColumnCount();
3102 unsigned int colnum
= 0;
3104 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3105 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3107 col
= GetOwner()->GetColumnAt(colnum
);
3108 if (col
->IsHidden())
3109 continue; // skip it!
3111 unsigned int w
= col
->GetWidth();
3112 if (x_start
+w
>= (unsigned int)x
)
3119 item
= GetItemByRow( GetLineAt( y
) );
3122 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3123 const wxDataViewColumn
* column
)
3128 unsigned int cols
= GetOwner()->GetColumnCount();
3129 // If column is null the loop will compute the combined width of all columns.
3130 // Otherwise, it will compute the x position of the column we are looking for.
3131 for (unsigned int i
= 0; i
< cols
; i
++)
3133 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3138 if (col
->IsHidden())
3139 continue; // skip it!
3141 xpos
+= col
->GetWidth();
3142 width
+= col
->GetWidth();
3147 // If we have a column, we need can get its width directly.
3148 if(column
->IsHidden())
3151 width
= column
->GetWidth();
3156 // If we have no column, we reset the x position back to zero.
3160 // we have to take an expander column into account and compute its indentation
3161 // to get the correct x position where the actual text is
3163 int row
= GetRowByItem(item
);
3164 if (!IsVirtualList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3166 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3167 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3168 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3170 if(!node
->HasChildren())
3174 wxRect
itemRect( xpos
+ indent
,
3175 GetLineStart( row
),
3177 GetLineHeight( row
) );
3179 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3180 &itemRect
.x
, &itemRect
.y
);
3185 int wxDataViewMainWindow::RecalculateCount()
3187 if (IsVirtualList())
3189 wxDataViewVirtualListModel
*list_model
=
3190 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3192 return list_model
->GetCount();
3196 return m_root
->GetSubTreeCount();
3200 class ItemToRowJob
: public DoJob
3203 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3210 // Maybe binary search will help to speed up this process
3211 virtual int operator() ( wxDataViewTreeNode
* node
)
3214 if( node
->GetItem() == item
)
3219 if( node
->GetItem() == **m_iter
)
3226 ret
+= node
->GetSubTreeCount();
3232 virtual int operator() ( void * n
)
3235 if( n
== item
.GetID() )
3240 // the row number is begin from zero
3241 int GetResult() const
3245 ItemList::const_iterator m_iter
;
3246 wxDataViewItem item
;
3251 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3253 const wxDataViewModel
* model
= GetOwner()->GetModel();
3257 if (IsVirtualList())
3259 return wxPtrToUInt( item
.GetID() ) -1;
3266 // Compose the a parent-chain of the finding item
3268 wxDataViewItem
* pItem
;
3269 list
.DeleteContents( true );
3270 wxDataViewItem
it( item
);
3273 pItem
= new wxDataViewItem( it
);
3274 list
.Insert( pItem
);
3275 it
= model
->GetParent( it
);
3277 pItem
= new wxDataViewItem( );
3278 list
.Insert( pItem
);
3280 ItemToRowJob
job( item
, list
.begin() );
3281 Walker(m_root
, job
);
3282 return job
.GetResult();
3286 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
3287 wxDataViewTreeNode
* node
)
3289 if( !model
->IsContainer( item
) )
3292 wxDataViewItemArray children
;
3293 unsigned int num
= model
->GetChildren( item
, children
);
3295 unsigned int index
= 0;
3296 while( index
< num
)
3298 if( model
->IsContainer( children
[index
] ) )
3300 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3301 n
->SetItem(children
[index
]);
3302 n
->SetHasChildren( true );
3307 node
->AddLeaf( children
[index
].GetID() );
3311 node
->SetSubTreeCount( num
);
3312 wxDataViewTreeNode
* n
= node
->GetParent();
3314 n
->ChangeSubTreeCount(num
);
3318 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3322 if (GetOwner()->GetModel()->IsVirtualListModel())
3328 m_root
= new wxDataViewTreeNode( NULL
);
3329 m_root
->SetHasChildren(true);
3331 // First we define a invalid item to fetch the top-level elements
3332 wxDataViewItem item
;
3334 BuildTreeHelper( model
, item
, m_root
);
3338 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3340 if( node
->GetNodeNumber() != 0 )
3342 int len
= node
->GetNodeNumber();
3343 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3344 for (int i
= 0; i
< len
; i
++)
3345 DestroyTreeHelper(nodes
[i
]);
3350 void wxDataViewMainWindow::DestroyTree()
3352 if (!IsVirtualList())
3354 ::DestroyTreeHelper(m_root
);
3360 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3362 if ( GetParent()->HandleAsNavigationKey(event
) )
3365 // no item -> nothing to do
3366 if (!HasCurrentRow())
3372 // don't use m_linesPerPage directly as it might not be computed yet
3373 const int pageSize
= GetCountPerPage();
3374 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3376 switch ( event
.GetKeyCode() )
3380 wxWindow
*parent
= GetParent();
3381 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3383 le
.SetItem( GetItemByRow(m_currentRow
) );
3384 le
.SetEventObject(parent
);
3385 le
.SetModel(GetOwner()->GetModel());
3387 parent
->GetEventHandler()->ProcessEvent(le
);
3392 if ( m_currentRow
> 0 )
3393 OnArrowChar( m_currentRow
- 1, event
);
3397 if ( m_currentRow
< GetRowCount() - 1 )
3398 OnArrowChar( m_currentRow
+ 1, event
);
3400 // Add the process for tree expanding/collapsing
3403 if (IsVirtualList())
3406 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3410 if (node
->HasChildren() && node
->IsOpen())
3412 Collapse(m_currentRow
);
3414 else // if the node is already closed we move the selection to its parent
3416 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3418 if(!node
->HasChildren())
3423 int parent
= GetRowByItem( parent_node
->GetItem() );
3426 unsigned int row
= m_currentRow
;
3427 SelectRow( row
, false);
3428 SelectRow( parent
, true );
3429 ChangeCurrentRow( parent
);
3430 GetOwner()->EnsureVisible( parent
, -1 );
3431 SendSelectionChangedEvent( parent_node
->GetItem() );
3439 if (!IsExpanded( m_currentRow
))
3440 Expand( m_currentRow
);
3443 unsigned int row
= m_currentRow
;
3444 SelectRow( row
, false );
3445 SelectRow( row
+ 1, true );
3446 ChangeCurrentRow( row
+ 1 );
3447 GetOwner()->EnsureVisible( row
+ 1, -1 );
3448 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3455 OnArrowChar( GetRowCount() - 1, event
);
3460 OnArrowChar( 0, event
);
3465 int steps
= pageSize
- 1;
3466 int index
= m_currentRow
- steps
;
3470 OnArrowChar( index
, event
);
3476 int steps
= pageSize
- 1;
3477 unsigned int index
= m_currentRow
+ steps
;
3478 unsigned int count
= GetRowCount();
3479 if ( index
>= count
)
3482 OnArrowChar( index
, event
);
3488 if(m_selection
.size() == 1)
3490 // TODO: we need to revise that when we have a concept for a 'current column'
3491 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3501 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3503 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3505 // let the base handle mouse wheel events.
3510 int x
= event
.GetX();
3511 int y
= event
.GetY();
3512 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3513 wxDataViewColumn
*col
= NULL
;
3516 unsigned int cols
= GetOwner()->GetColumnCount();
3518 for (i
= 0; i
< cols
; i
++)
3520 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3522 continue; // skip it!
3524 if (x
< xpos
+ c
->GetWidth())
3529 xpos
+= c
->GetWidth();
3534 wxDataViewRenderer
*cell
= col
->GetRenderer();
3535 unsigned int current
= GetLineAt( y
);
3536 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3538 // Unselect all if below the last row ?
3542 // Test whether the mouse is hovered on the tree item button
3543 bool hoverOverExpander
= false;
3544 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3546 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3547 if( node
!=NULL
&& node
->HasChildren() )
3549 int indent
= node
->GetIndentLevel();
3550 indent
= GetOwner()->GetIndent()*indent
;
3552 // we make the rectangle we are looking in a bit bigger than the actual
3553 // visual expander so the user can hit that little thing reliably
3554 wxRect
rect( xpos
+ indent
,
3555 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3556 m_lineHeight
, m_lineHeight
);
3557 if( rect
.Contains(x
, y
) )
3559 // So the mouse is over the expander
3560 hoverOverExpander
= true;
3561 if (m_underMouse
&& m_underMouse
!= node
)
3563 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3564 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3566 if (m_underMouse
!= node
)
3568 // wxLogMessage("Do the row: %d", current);
3569 RefreshRow(current
);
3571 m_underMouse
= node
;
3574 if (node
!=NULL
&& !node
->HasChildren())
3577 if (!hoverOverExpander
)
3579 if (m_underMouse
!= NULL
)
3581 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3582 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3583 m_underMouse
= NULL
;
3587 wxDataViewModel
*model
= GetOwner()->GetModel();
3589 #if wxUSE_DRAG_AND_DROP
3590 if (event
.Dragging())
3592 if (m_dragCount
== 0)
3594 // we have to report the raw, physical coords as we want to be
3595 // able to call HitTest(event.m_pointDrag) from the user code to
3596 // get the item being dragged
3597 m_dragStart
= event
.GetPosition();
3602 if (m_dragCount
!= 3)
3605 if (event
.LeftIsDown())
3607 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3608 &m_dragStart
.x
, &m_dragStart
.y
);
3609 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3610 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3612 // Notify cell about drag
3613 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3614 event
.SetEventObject( m_owner
);
3615 event
.SetItem( item
);
3616 event
.SetModel( model
);
3617 if (!m_owner
->HandleWindowEvent( event
))
3620 if (!event
.IsAllowed())
3623 wxDataObject
*obj
= event
.GetDataObject();
3627 wxDataViewDropSource
drag( this, drag_item_row
);
3628 drag
.SetData( *obj
);
3629 /* wxDragResult res = */ drag
.DoDragDrop();
3638 #endif // wxUSE_DRAG_AND_DROP
3640 bool simulateClick
= false;
3642 if (event
.ButtonDClick())
3644 m_renameTimer
->Stop();
3645 m_lastOnSame
= false;
3648 wxDataViewItem item
= GetItemByRow(current
);
3649 bool ignore_other_columns
=
3650 ((GetOwner()->GetExpanderColumn() != col
) &&
3651 (model
->IsContainer(item
)) &&
3652 (!model
->HasContainerColumns(item
)));
3654 if (event
.LeftDClick())
3656 if(hoverOverExpander
)
3658 // a double click on the expander will be converted into a "simulated" normal click
3659 simulateClick
= true;
3661 else if ( current
== m_lineLastClicked
)
3663 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3666 model
->GetValue( value
, item
, col
->GetModelColumn() );
3667 cell
->SetValue( value
);
3668 wxRect
cell_rect( xpos
, GetLineStart( current
),
3669 col
->GetWidth(), GetLineHeight( current
) );
3670 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3674 wxWindow
*parent
= GetParent();
3675 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3677 le
.SetEventObject(parent
);
3678 le
.SetModel(GetOwner()->GetModel());
3680 parent
->GetEventHandler()->ProcessEvent(le
);
3686 // The first click was on another item, so don't interpret this as
3687 // a double click, but as a simple click instead
3688 simulateClick
= true;
3692 if (event
.LeftUp() && !hoverOverExpander
)
3694 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3696 // select single line
3697 SelectAllRows( false );
3698 SelectRow( m_lineSelectSingleOnUp
, true );
3699 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3702 // If the user click the expander, we do not do editing even if the column
3703 // with expander are editable
3704 if (m_lastOnSame
&& !ignore_other_columns
)
3706 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3707 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3709 m_renameTimer
->Start( 100, true );
3713 m_lastOnSame
= false;
3714 m_lineSelectSingleOnUp
= (unsigned int)-1;
3716 else if(!event
.LeftUp())
3718 // This is necessary, because after a DnD operation in
3719 // from and to ourself, the up event is swallowed by the
3720 // DnD code. So on next non-up event (which means here and
3721 // now) m_lineSelectSingleOnUp should be reset.
3722 m_lineSelectSingleOnUp
= (unsigned int)-1;
3725 if (event
.RightDown())
3727 m_lineBeforeLastClicked
= m_lineLastClicked
;
3728 m_lineLastClicked
= current
;
3730 // If the item is already selected, do not update the selection.
3731 // Multi-selections should not be cleared if a selected item is clicked.
3732 if (!IsRowSelected(current
))
3734 SelectAllRows(false);
3735 ChangeCurrentRow(current
);
3736 SelectRow(m_currentRow
,true);
3737 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3740 else if (event
.RightUp())
3743 model
->GetValue( value
, item
, col
->GetModelColumn() );
3744 wxWindow
*parent
= GetParent();
3745 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3747 le
.SetEventObject(parent
);
3748 le
.SetModel(GetOwner()->GetModel());
3750 parent
->GetEventHandler()->ProcessEvent(le
);
3752 else if (event
.MiddleDown())
3756 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3758 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3760 // hoverOverExpander being true tells us that our node must be
3761 // valid and have children.
3762 // So we don't need any extra checks.
3763 if( node
->IsOpen() )
3768 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3772 m_lineBeforeLastClicked
= m_lineLastClicked
;
3773 m_lineLastClicked
= current
;
3775 unsigned int oldCurrentRow
= m_currentRow
;
3776 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3778 bool cmdModifierDown
= event
.CmdDown();
3779 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3781 if ( IsSingleSel() || !IsRowSelected(current
) )
3783 SelectAllRows( false );
3784 ChangeCurrentRow(current
);
3785 SelectRow(m_currentRow
,true);
3786 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3788 else // multi sel & current is highlighted & no mod keys
3790 m_lineSelectSingleOnUp
= current
;
3791 ChangeCurrentRow(current
); // change focus
3794 else // multi sel & either ctrl or shift is down
3796 if (cmdModifierDown
)
3798 ChangeCurrentRow(current
);
3799 ReverseRowSelection(m_currentRow
);
3800 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3802 else if (event
.ShiftDown())
3804 ChangeCurrentRow(current
);
3806 unsigned int lineFrom
= oldCurrentRow
,
3809 if ( lineTo
< lineFrom
)
3812 lineFrom
= m_currentRow
;
3815 SelectRows(lineFrom
, lineTo
, true);
3816 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3818 else // !ctrl, !shift
3820 // test in the enclosing if should make it impossible
3821 wxFAIL_MSG( wxT("how did we get here?") );
3825 if (m_currentRow
!= oldCurrentRow
)
3826 RefreshRow( oldCurrentRow
);
3828 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3830 // Update selection here...
3833 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3834 (current
== oldCurrentRow
)) && oldWasSelected
;
3836 // Call LeftClick after everything else as under GTK+
3837 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3839 // notify cell about right click
3841 model
->GetValue( value
, item
, col
->GetModelColumn() );
3842 cell
->SetValue( value
);
3843 wxRect
cell_rect( xpos
, GetLineStart( current
),
3844 col
->GetWidth(), GetLineHeight( current
) );
3845 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
,
3846 model
, item
, col
->GetModelColumn());
3851 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3855 if (HasCurrentRow())
3861 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3865 if (HasCurrentRow())
3871 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3873 if( m_selection
.GetCount() != 1 )
3874 return wxDataViewItem();
3876 return GetItemByRow( m_selection
.Item(0));
3879 //-----------------------------------------------------------------------------
3881 //-----------------------------------------------------------------------------
3883 WX_DEFINE_LIST(wxDataViewColumnList
)
3885 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3886 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3887 EVT_SIZE(wxDataViewCtrl::OnSize
)
3890 wxDataViewCtrl::~wxDataViewCtrl()
3893 GetModel()->RemoveNotifier( m_notifier
);
3898 void wxDataViewCtrl::Init()
3900 m_cols
.DeleteContents(true);
3903 // No sorting column at start
3904 m_sortingColumnIdx
= wxNOT_FOUND
;
3906 m_headerArea
= NULL
;
3909 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3910 const wxPoint
& pos
, const wxSize
& size
,
3911 long style
, const wxValidator
& validator
)
3913 // if ( (style & wxBORDER_MASK) == 0)
3914 // style |= wxBORDER_SUNKEN;
3918 if (!wxControl::Create( parent
, id
, pos
, size
,
3919 style
| wxScrolledWindowStyle
, validator
))
3922 SetInitialSize(size
);
3925 MacSetClipChildren( true );
3928 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3930 if (HasFlag(wxDV_NO_HEADER
))
3931 m_headerArea
= NULL
;
3933 m_headerArea
= new wxDataViewHeaderWindow(this);
3935 SetTargetWindow( m_clientArea
);
3937 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3939 sizer
->Add( m_headerArea
, 0, wxGROW
);
3940 sizer
->Add( m_clientArea
, 1, wxGROW
);
3946 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3948 return wxBORDER_THEME
;
3952 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3956 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3959 // we need to process arrows ourselves for scrolling
3960 if ( nMsg
== WM_GETDLGCODE
)
3962 rc
|= DLGC_WANTARROWS
;
3970 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3972 wxSize newsize
= size
;
3973 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3974 newsize
.y
-= m_headerArea
->GetSize().y
;
3979 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3981 // We need to override OnSize so that our scrolled
3982 // window a) does call Layout() to use sizers for
3983 // positioning the controls but b) does not query
3984 // the sizer for their size and use that for setting
3985 // the scrollable area as set that ourselves by
3986 // calling SetScrollbar() further down.
3993 void wxDataViewCtrl::SetFocus()
3996 m_clientArea
->SetFocus();
3999 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
4001 if (!wxDataViewCtrlBase::AssociateModel( model
))
4004 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
4006 model
->AddNotifier( m_notifier
);
4008 m_clientArea
->DestroyTree();
4010 m_clientArea
->BuildTree(model
);
4012 m_clientArea
->UpdateDisplay();
4017 #if wxUSE_DRAG_AND_DROP
4019 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
4021 return m_clientArea
->EnableDragSource( format
);
4024 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
4026 return m_clientArea
->EnableDropTarget( format
);
4029 #endif // wxUSE_DRAG_AND_DROP
4031 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
4033 if (!wxDataViewCtrlBase::AppendColumn(col
))
4036 m_cols
.Append( col
);
4037 OnColumnsCountChanged();
4041 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4043 if (!wxDataViewCtrlBase::PrependColumn(col
))
4046 m_cols
.Insert( col
);
4047 OnColumnsCountChanged();
4051 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4053 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4056 m_cols
.Insert( pos
, col
);
4057 OnColumnsCountChanged();
4061 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4064 m_headerArea
->UpdateColumn(idx
);
4066 m_clientArea
->UpdateDisplay();
4069 void wxDataViewCtrl::OnColumnsCountChanged()
4072 m_headerArea
->SetColumnCount(GetColumnCount());
4074 m_clientArea
->UpdateDisplay();
4077 void wxDataViewCtrl::DoSetExpanderColumn()
4079 m_clientArea
->UpdateDisplay();
4082 void wxDataViewCtrl::DoSetIndent()
4084 m_clientArea
->UpdateDisplay();
4087 unsigned int wxDataViewCtrl::GetColumnCount() const
4089 return m_cols
.GetCount();
4092 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4097 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4099 // columns can't be reordered if there is no header window which allows
4101 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4104 return GetColumn(idx
);
4107 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4109 const unsigned count
= m_cols
.size();
4110 for ( unsigned n
= 0; n
< count
; n
++ )
4112 if ( m_cols
[n
] == column
)
4119 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4120 unsigned int WXUNUSED(new_pos
))
4122 // do _not_ reorder m_cols elements here, they should always be in the
4123 // order in which columns were added, we only display the columns in
4125 m_clientArea
->UpdateDisplay();
4128 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4130 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4135 OnColumnsCountChanged();
4140 bool wxDataViewCtrl::ClearColumns()
4143 OnColumnsCountChanged();
4147 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4151 unsigned int len
= GetColumnCount();
4152 for ( unsigned int i
= 0; i
< len
; i
++ )
4154 wxDataViewColumn
* col
= GetColumnAt(i
);
4155 if (col
->IsHidden())
4157 ret
+= col
->GetWidth();
4160 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4167 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4169 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4170 : GetColumn(m_sortingColumnIdx
);
4173 // Selection code with wxDataViewItem as parameters
4174 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4176 return m_clientArea
->GetSelection();
4179 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4182 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4183 int len
= selection
.GetCount();
4184 for( int i
= 0; i
< len
; i
++)
4186 unsigned int row
= selection
[i
];
4187 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4192 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4194 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4196 wxDataViewItem last_parent
;
4198 int len
= sel
.GetCount();
4199 for( int i
= 0; i
< len
; i
++ )
4201 wxDataViewItem item
= sel
[i
];
4202 wxDataViewItem parent
= GetModel()->GetParent( item
);
4205 if (parent
!= last_parent
)
4206 ExpandAncestors(item
);
4209 last_parent
= parent
;
4210 int row
= m_clientArea
->GetRowByItem( item
);
4212 selection
.Add( static_cast<unsigned int>(row
) );
4215 m_clientArea
->SetSelections( selection
);
4218 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4220 ExpandAncestors( item
);
4222 int row
= m_clientArea
->GetRowByItem( item
);
4225 // Unselect all rows before select another in the single select mode
4226 if (m_clientArea
->IsSingleSel())
4227 m_clientArea
->SelectAllRows(false);
4228 m_clientArea
->SelectRow(row
, true);
4232 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4234 int row
= m_clientArea
->GetRowByItem( item
);
4236 m_clientArea
->SelectRow(row
, false);
4239 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4241 int row
= m_clientArea
->GetRowByItem( item
);
4244 return m_clientArea
->IsRowSelected(row
);
4249 // Selection code with row number as parameter
4250 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4253 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4254 int len
= selection
.GetCount();
4255 for( int i
= 0; i
< len
; i
++)
4257 unsigned int row
= selection
[i
];
4263 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4265 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4266 int len
= sel
.GetCount();
4267 for( int i
= 0; i
< len
; i
++ )
4271 selection
.Add( static_cast<unsigned int>(row
) );
4273 m_clientArea
->SetSelections( selection
);
4276 void wxDataViewCtrl::Select( int row
)
4280 if (m_clientArea
->IsSingleSel())
4281 m_clientArea
->SelectAllRows(false);
4282 m_clientArea
->SelectRow( row
, true );
4286 void wxDataViewCtrl::Unselect( int row
)
4289 m_clientArea
->SelectRow(row
, false);
4292 bool wxDataViewCtrl::IsSelected( int row
) const
4295 return m_clientArea
->IsRowSelected(row
);
4299 void wxDataViewCtrl::SelectRange( int from
, int to
)
4302 for( int i
= from
; i
< to
; i
++ )
4304 m_clientArea
->Select(sel
);
4307 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4309 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4310 for( int i
= from
; i
< to
; i
++ )
4311 if( sel
.Index( i
) != wxNOT_FOUND
)
4313 m_clientArea
->SetSelections(sel
);
4316 void wxDataViewCtrl::SelectAll()
4318 m_clientArea
->SelectAllRows(true);
4321 void wxDataViewCtrl::UnselectAll()
4323 m_clientArea
->SelectAllRows(false);
4326 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4330 if( row
> (int) m_clientArea
->GetRowCount() )
4331 row
= m_clientArea
->GetRowCount();
4333 int first
= m_clientArea
->GetFirstVisibleRow();
4334 int last
= m_clientArea
->GetLastVisibleRow();
4336 m_clientArea
->ScrollTo( row
, column
);
4337 else if( row
> last
)
4338 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4340 m_clientArea
->ScrollTo( first
, column
);
4343 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4345 ExpandAncestors( item
);
4347 m_clientArea
->RecalculateDisplay();
4349 int row
= m_clientArea
->GetRowByItem(item
);
4352 if( column
== NULL
)
4353 EnsureVisible(row
, -1);
4355 EnsureVisible( row
, GetColumnIndex(column
) );
4360 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4361 wxDataViewColumn
* &column
) const
4363 m_clientArea
->HitTest(point
, item
, column
);
4366 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4367 const wxDataViewColumn
* column
) const
4369 return m_clientArea
->GetItemRect(item
, column
);
4372 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4374 return m_clientArea
->GetItemByRow( row
);
4377 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4379 return m_clientArea
->GetRowByItem( item
);
4382 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4384 int row
= m_clientArea
->GetRowByItem( item
);
4386 m_clientArea
->Expand(row
);
4389 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4391 int row
= m_clientArea
->GetRowByItem( item
);
4393 m_clientArea
->Collapse(row
);
4396 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4398 int row
= m_clientArea
->GetRowByItem( item
);
4400 return m_clientArea
->IsExpanded(row
);
4404 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4406 wxDataViewColumn
* col
= GetColumn( column
);
4410 wxRect itemRect
= GetItemRect(item
, col
);
4411 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4412 renderer
->StartEditing(item
, itemRect
);
4415 #endif // !wxUSE_GENERICDATAVIEWCTRL
4417 #endif // wxUSE_DATAVIEWCTRL