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"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
47 #include "wx/imaglist.h"
48 #include "wx/headerctrl.h"
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
56 static const int SCROLL_UNIT_X
= 15;
58 // the cell padding on the left/right
59 static const int PADDING_RIGHTLEFT
= 3;
61 // the expander space margin
62 static const int EXPANDER_MARGIN
= 4;
65 static const int EXPANDER_OFFSET
= 4;
67 static const int EXPANDER_OFFSET
= 1;
70 //Below is the compare stuff
71 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
72 static wxDataViewModel
* g_model
;
73 static int g_column
= -2;
74 static bool g_asending
= true;
76 //-----------------------------------------------------------------------------
77 // wxDataViewHeaderWindow
78 //-----------------------------------------------------------------------------
80 class wxDataViewHeaderWindow
: public wxHeaderCtrl
83 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
84 : wxHeaderCtrl(parent
)
88 wxDataViewCtrl
*GetOwner() const
89 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
92 // implement/override wxHeaderCtrl functions by forwarding them to the main
94 virtual wxHeaderColumn
& GetColumn(unsigned int idx
)
96 return *(GetOwner()->GetColumn(idx
));
99 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
101 wxDataViewCtrl
* const owner
= GetOwner();
103 int widthContents
= owner
->GetBestColumnWidth(idx
);
104 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
105 owner
->OnColumnChange(idx
);
111 bool SendEvent(wxEventType type
, unsigned int n
)
113 wxDataViewCtrl
* const owner
= GetOwner();
114 wxDataViewEvent
event(type
, owner
->GetId());
116 event
.SetEventObject(owner
);
118 event
.SetDataViewColumn(owner
->GetColumn(n
));
119 event
.SetModel(owner
->GetModel());
121 // for events created by wxDataViewHeaderWindow the
122 // row / value fields are not valid
123 return owner
->GetEventHandler()->ProcessEvent(event
);
126 void OnClick(wxHeaderCtrlEvent
& event
)
128 const unsigned idx
= event
.GetColumn();
130 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
133 // default handling for the column click is to sort by this column or
134 // toggle its sort order
135 wxDataViewCtrl
* const owner
= GetOwner();
136 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
137 if ( !col
->IsSortable() )
139 // no default handling for non-sortable columns
144 if ( col
->IsSortKey() )
146 // already using this column for sorting, just change the order
147 col
->ToggleSortOrder();
149 else // not using this column for sorting yet
151 // first unset the old sort column if any
152 int oldSortKey
= owner
->GetSortingColumnIndex();
153 if ( oldSortKey
!= wxNOT_FOUND
)
155 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
156 owner
->OnColumnChange(oldSortKey
);
159 owner
->SetSortingColumnIndex(idx
);
163 wxDataViewModel
* const model
= owner
->GetModel();
167 owner
->OnColumnChange(idx
);
170 void OnRClick(wxHeaderCtrlEvent
& event
)
172 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
177 void OnEndResize(wxHeaderCtrlEvent
& event
)
179 wxDataViewCtrl
* const owner
= GetOwner();
181 const unsigned col
= event
.GetColumn();
182 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
183 GetOwner()->OnColumnChange(col
);
186 void OnEndReorder(wxHeaderCtrlEvent
& event
)
188 wxDataViewCtrl
* const owner
= GetOwner();
189 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
190 event
.GetNewOrder());
193 DECLARE_EVENT_TABLE()
194 DECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
)
197 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
198 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
199 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
201 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnEndResize
)
203 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
206 //-----------------------------------------------------------------------------
207 // wxDataViewRenameTimer
208 //-----------------------------------------------------------------------------
210 class wxDataViewRenameTimer
: public wxTimer
213 wxDataViewMainWindow
*m_owner
;
216 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
220 //-----------------------------------------------------------------------------
221 // wxDataViewTreeNode
222 //-----------------------------------------------------------------------------
223 class wxDataViewTreeNode
;
224 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
225 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
227 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
228 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
230 class wxDataViewTreeNode
233 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
240 m_hasChildren
= false;
244 ~wxDataViewTreeNode()
248 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
249 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
250 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
251 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
253 void AddNode( wxDataViewTreeNode
* node
)
255 m_leaves
.Add( node
->GetItem().GetID() );
257 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
260 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
262 void AddLeaf( void * leaf
)
264 m_leaves
.Add( leaf
);
266 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
269 wxDataViewItem
& GetItem() { return m_item
; }
270 const wxDataViewItem
& GetItem() const { return m_item
; }
271 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
273 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
274 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
275 int GetIndentLevel() const
278 const wxDataViewTreeNode
* node
= this;
279 while( node
->GetParent()->GetParent() != NULL
)
281 node
= node
->GetParent();
294 int len
= m_nodes
.GetCount();
296 for ( int i
= 0;i
< len
; i
++)
297 sum
+= m_nodes
[i
]->GetSubTreeCount();
299 sum
+= m_leaves
.GetCount();
302 ChangeSubTreeCount(-sum
);
308 ChangeSubTreeCount(sum
);
311 bool HasChildren() const { return m_hasChildren
; }
312 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
314 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
315 int GetSubTreeCount() const { return m_subTreeCount
; }
316 void ChangeSubTreeCount( int num
)
320 m_subTreeCount
+= num
;
322 m_parent
->ChangeSubTreeCount(num
);
329 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
330 int len
= m_nodes
.GetCount();
331 for (int i
= 0; i
< len
; i
++)
332 m_nodes
[i
]->Resort();
333 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
338 wxDataViewTreeNode
*m_parent
;
339 wxDataViewTreeNodes m_nodes
;
340 wxDataViewTreeLeaves m_leaves
;
341 wxDataViewItem m_item
;
347 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
349 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
352 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
354 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
359 //-----------------------------------------------------------------------------
360 // wxDataViewMainWindow
361 //-----------------------------------------------------------------------------
363 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
365 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
366 WX_DEFINE_LIST(ItemList
)
368 class wxDataViewMainWindow
: public wxWindow
371 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
373 const wxPoint
&pos
= wxDefaultPosition
,
374 const wxSize
&size
= wxDefaultSize
,
375 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
376 virtual ~wxDataViewMainWindow();
378 bool IsVirtualList() const { return m_root
== NULL
; }
380 // notifications from wxDataViewModel
381 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
382 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
383 bool ItemChanged( const wxDataViewItem
&item
);
384 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
388 if (!IsVirtualList())
398 g_model
= GetOwner()->GetModel();
399 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
402 if (g_model
->HasDefaultCompare())
410 g_column
= col
->GetModelColumn();
411 g_asending
= col
->IsSortOrderAscending();
414 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
415 wxDataViewCtrl
*GetOwner() { return m_owner
; }
416 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
418 void OnPaint( wxPaintEvent
&event
);
419 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
420 void OnChar( wxKeyEvent
&event
);
421 void OnMouse( wxMouseEvent
&event
);
422 void OnSetFocus( wxFocusEvent
&event
);
423 void OnKillFocus( wxFocusEvent
&event
);
425 void UpdateDisplay();
426 void RecalculateDisplay();
427 void OnInternalIdle();
429 void OnRenameTimer();
431 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
432 void ScrollTo( int rows
, int column
);
434 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
435 void ChangeCurrentRow( unsigned int row
);
437 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
438 bool IsEmpty() { return GetRowCount() == 0; }
440 int GetCountPerPage() const;
441 int GetEndOfLastCol() const;
442 unsigned int GetFirstVisibleRow() const;
443 //I change this method to un const because in the tree view, the displaying number of the tree are changing along with the expanding/collapsing of the tree nodes
444 unsigned int GetLastVisibleRow();
445 unsigned int GetRowCount();
447 wxDataViewItem
GetSelection() const;
448 wxDataViewSelection
GetSelections(){ return m_selection
; }
449 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
450 void Select( const wxArrayInt
& aSelections
);
451 void SelectAllRows( bool on
);
452 void SelectRow( unsigned int row
, bool on
);
453 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
454 void ReverseRowSelection( unsigned int row
);
455 bool IsRowSelected( unsigned int row
);
456 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
458 void RefreshRow( unsigned int row
);
459 void RefreshRows( unsigned int from
, unsigned int to
);
460 void RefreshRowsAfter( unsigned int firstRow
);
462 // returns the colour to be used for drawing the rules
463 wxColour
GetRuleColour() const
465 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
468 wxRect
GetLineRect( unsigned int row
) const;
470 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
471 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
472 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
474 //Some useful functions for row and item mapping
475 wxDataViewItem
GetItemByRow( unsigned int row
) const;
476 int GetRowByItem( const wxDataViewItem
& item
) const;
478 //Methods for building the mapping tree
479 void BuildTree( wxDataViewModel
* model
);
481 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
482 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
484 void Expand( unsigned int row
) { OnExpanding( row
); }
485 void Collapse( unsigned int row
) { OnCollapsing( row
); }
487 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
488 //We did not need this temporarily
489 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
491 int RecalculateCount();
493 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
494 void OnExpanding( unsigned int row
);
495 void OnCollapsing( unsigned int row
);
497 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
500 wxDataViewCtrl
*m_owner
;
504 wxDataViewColumn
*m_currentCol
;
505 unsigned int m_currentRow
;
506 wxDataViewSelection m_selection
;
508 wxDataViewRenameTimer
*m_renameTimer
;
516 // for double click logic
517 unsigned int m_lineLastClicked
,
518 m_lineBeforeLastClicked
,
519 m_lineSelectSingleOnUp
;
521 // the pen used to draw horiz/vertical rules
524 // the pen used to draw the expander and the lines
527 //This is the tree structure of the model
528 wxDataViewTreeNode
* m_root
;
530 //This is the tree node under the cursor
531 wxDataViewTreeNode
* m_underMouse
;
533 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
534 DECLARE_EVENT_TABLE()
537 // ---------------------------------------------------------
538 // wxGenericDataViewModelNotifier
539 // ---------------------------------------------------------
541 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
544 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
545 { m_mainWindow
= mainWindow
; }
547 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
548 { return m_mainWindow
->ItemAdded( parent
, item
); }
549 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
550 { return m_mainWindow
->ItemDeleted( parent
, item
); }
551 virtual bool ItemChanged( const wxDataViewItem
& item
)
552 { return m_mainWindow
->ItemChanged(item
); }
553 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
554 { return m_mainWindow
->ValueChanged( item
, col
); }
555 virtual bool Cleared()
556 { return m_mainWindow
->Cleared(); }
557 virtual void Resort()
558 { m_mainWindow
->Resort(); }
560 wxDataViewMainWindow
*m_mainWindow
;
563 // ---------------------------------------------------------
564 // wxDataViewRenderer
565 // ---------------------------------------------------------
567 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
569 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
570 wxDataViewCellMode mode
,
572 wxDataViewRendererBase( varianttype
, mode
, align
)
581 wxDataViewRenderer::~wxDataViewRenderer()
587 wxDC
*wxDataViewRenderer::GetDC()
591 if (GetOwner() == NULL
)
593 if (GetOwner()->GetOwner() == NULL
)
595 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
601 void wxDataViewRenderer::SetAlignment( int align
)
606 int wxDataViewRenderer::GetAlignment() const
611 int wxDataViewRenderer::CalculateAlignment() const
613 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
615 if (GetOwner() == NULL
)
616 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
618 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
624 // ---------------------------------------------------------
625 // wxDataViewCustomRenderer
626 // ---------------------------------------------------------
628 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
630 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
631 wxDataViewCellMode mode
, int align
) :
632 wxDataViewRenderer( varianttype
, mode
, align
)
636 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
638 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
639 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
640 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
641 view
->GetForegroundColour();
642 dc
->SetTextForeground(col
);
643 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
646 // ---------------------------------------------------------
647 // wxDataViewTextRenderer
648 // ---------------------------------------------------------
650 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
652 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
653 wxDataViewCellMode mode
, int align
) :
654 wxDataViewCustomRenderer( varianttype
, mode
, align
)
658 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
660 m_text
= value
.GetString();
665 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
670 bool wxDataViewTextRenderer::HasEditorCtrl()
675 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
676 wxRect labelRect
, const wxVariant
&value
)
678 return new wxTextCtrl( parent
, wxID_ANY
, value
,
679 wxPoint(labelRect
.x
,labelRect
.y
),
680 wxSize(labelRect
.width
,labelRect
.height
) );
683 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
685 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
686 value
= text
->GetValue();
690 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
692 RenderText( m_text
, 0, cell
, dc
, state
);
696 wxSize
wxDataViewTextRenderer::GetSize() const
698 const wxDataViewCtrl
*view
= GetView();
702 view
->GetTextExtent( m_text
, &x
, &y
);
703 return wxSize( x
, y
);
705 return wxSize(80,20);
708 // ---------------------------------------------------------
709 // wxDataViewTextRendererAttr
710 // ---------------------------------------------------------
712 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
714 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
715 wxDataViewCellMode mode
, int align
) :
716 wxDataViewTextRenderer( varianttype
, mode
, align
)
721 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
728 if (m_attr
.HasColour())
730 colour
= dc
->GetTextForeground();
731 dc
->SetTextForeground( m_attr
.GetColour() );
734 if (m_attr
.GetBold() || m_attr
.GetItalic())
736 font
= dc
->GetFont();
737 wxFont myfont
= font
;
738 if (m_attr
.GetBold())
739 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
740 if (m_attr
.GetItalic())
741 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
742 dc
->SetFont( myfont
);
746 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
751 if (m_attr
.HasColour())
752 dc
->SetTextForeground( colour
);
754 if (m_attr
.GetBold() || m_attr
.GetItalic())
762 // ---------------------------------------------------------
763 // wxDataViewBitmapRenderer
764 // ---------------------------------------------------------
766 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
768 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
769 wxDataViewCellMode mode
, int align
) :
770 wxDataViewCustomRenderer( varianttype
, mode
, align
)
774 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
776 if (value
.GetType() == wxT("wxBitmap"))
778 if (value
.GetType() == wxT("wxIcon"))
784 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
789 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
792 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
793 else if (m_icon
.Ok())
794 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
799 wxSize
wxDataViewBitmapRenderer::GetSize() const
802 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
803 else if (m_icon
.Ok())
804 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
806 return wxSize(16,16);
809 // ---------------------------------------------------------
810 // wxDataViewToggleRenderer
811 // ---------------------------------------------------------
813 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
815 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
816 wxDataViewCellMode mode
, int align
) :
817 wxDataViewCustomRenderer( varianttype
, mode
, align
)
822 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
824 m_toggle
= value
.GetBool();
829 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
834 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
836 // User wxRenderer here
839 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
841 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
846 flags
|= wxCONTROL_CHECKED
;
847 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
848 flags
|= wxCONTROL_DISABLED
;
850 wxRendererNative::Get().DrawCheckBox(
851 GetOwner()->GetOwner(),
859 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
860 wxDataViewModel
*model
,
861 const wxDataViewItem
& item
, unsigned int col
)
863 bool value
= !m_toggle
;
864 wxVariant variant
= value
;
865 model
->SetValue( variant
, item
, col
);
866 model
->ValueChanged( item
, col
);
870 wxSize
wxDataViewToggleRenderer::GetSize() const
872 return wxSize(20,20);
875 // ---------------------------------------------------------
876 // wxDataViewProgressRenderer
877 // ---------------------------------------------------------
879 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
881 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
882 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
883 wxDataViewCustomRenderer( varianttype
, mode
, align
)
889 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
893 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
895 m_value
= (long) value
;
897 if (m_value
< 0) m_value
= 0;
898 if (m_value
> 100) m_value
= 100;
903 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
905 value
= (long) m_value
;
909 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
911 double pct
= (double)m_value
/ 100.0;
913 bar
.width
= (int)(cell
.width
* pct
);
914 dc
->SetPen( *wxTRANSPARENT_PEN
);
915 dc
->SetBrush( *wxBLUE_BRUSH
);
916 dc
->DrawRectangle( bar
);
918 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
919 dc
->SetPen( *wxBLACK_PEN
);
920 dc
->DrawRectangle( cell
);
925 wxSize
wxDataViewProgressRenderer::GetSize() const
927 return wxSize(40,12);
930 // ---------------------------------------------------------
931 // wxDataViewDateRenderer
932 // ---------------------------------------------------------
934 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
936 #if wxUSE_DATE_RENDERER_POPUP
938 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
941 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
942 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
943 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
948 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
949 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
950 sizer
->Add( m_cal
, 1, wxGROW
);
955 void OnCalendar( wxCalendarEvent
&event
);
957 wxCalendarCtrl
*m_cal
;
958 wxDataViewModel
*m_model
;
960 const wxDataViewItem
& m_item
;
963 virtual void OnDismiss()
968 DECLARE_EVENT_TABLE()
971 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
972 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
975 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
977 wxDateTime date
= event
.GetDate();
978 wxVariant value
= date
;
979 m_model
->SetValue( value
, m_item
, m_col
);
980 m_model
->ValueChanged( m_item
, m_col
);
984 #endif // wxUSE_DATE_RENDERER_POPUP
986 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
988 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
989 wxDataViewCellMode mode
, int align
) :
990 wxDataViewCustomRenderer( varianttype
, mode
, align
)
994 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
996 m_date
= value
.GetDateTime();
1001 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1007 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1009 wxString tmp
= m_date
.FormatDate();
1010 RenderText( tmp
, 0, cell
, dc
, state
);
1014 wxSize
wxDataViewDateRenderer::GetSize() const
1016 const wxDataViewCtrl
* view
= GetView();
1017 wxString tmp
= m_date
.FormatDate();
1019 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1020 return wxSize(x
,y
+d
);
1023 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1024 const wxDataViewItem
& item
, unsigned int col
)
1027 model
->GetValue( variant
, item
, col
);
1028 wxDateTime value
= variant
.GetDateTime();
1030 #if wxUSE_DATE_RENDERER_POPUP
1031 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1032 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1033 wxPoint pos
= wxGetMousePosition();
1036 popup
->Popup( popup
->m_cal
);
1037 #else // !wxUSE_DATE_RENDERER_POPUP
1038 wxMessageBox(value
.Format());
1039 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1043 // ---------------------------------------------------------
1044 // wxDataViewIconTextRenderer
1045 // ---------------------------------------------------------
1047 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1049 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1050 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1051 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1054 SetAlignment(align
);
1057 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1061 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1067 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1072 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1075 const wxIcon
&icon
= m_value
.GetIcon();
1078 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1079 xoffset
= icon
.GetWidth()+4;
1082 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1087 wxSize
wxDataViewIconTextRenderer::GetSize() const
1089 const wxDataViewCtrl
*view
= GetView();
1090 if (!m_value
.GetText().empty())
1093 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1095 if (m_value
.GetIcon().IsOk())
1096 x
+= m_value
.GetIcon().GetWidth() + 4;
1097 return wxSize( x
, y
);
1099 return wxSize(80,20);
1103 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1104 wxRect
WXUNUSED(labelRect
),
1105 const wxVariant
& WXUNUSED(value
))
1111 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1112 wxVariant
& WXUNUSED(value
))
1117 //-----------------------------------------------------------------------------
1118 // wxDataViewRenameTimer
1119 //-----------------------------------------------------------------------------
1121 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1126 void wxDataViewRenameTimer::Notify()
1128 m_owner
->OnRenameTimer();
1131 //-----------------------------------------------------------------------------
1132 // wxDataViewMainWindow
1133 //-----------------------------------------------------------------------------
1135 //The tree building helper, declared firstly
1136 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1138 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1140 if (row1
> row2
) return 1;
1141 if (row1
== row2
) return 0;
1146 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1148 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1149 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1150 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1151 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1152 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1153 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1156 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1157 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1158 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1159 m_selection( wxDataViewSelectionCmp
)
1164 m_lastOnSame
= false;
1165 m_renameTimer
= new wxDataViewRenameTimer( this );
1167 // TODO: user better initial values/nothing selected
1168 m_currentCol
= NULL
;
1171 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1174 m_dragStart
= wxPoint(0,0);
1175 m_lineLastClicked
= (unsigned int) -1;
1176 m_lineBeforeLastClicked
= (unsigned int) -1;
1177 m_lineSelectSingleOnUp
= (unsigned int) -1;
1181 SetBackgroundColour( *wxWHITE
);
1183 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1185 m_penRule
= wxPen(GetRuleColour());
1187 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1188 m_penExpander
= wxPen(wxColour(0,0,0));
1189 //Some new added code to deal with the tree structure
1190 m_root
= new wxDataViewTreeNode( NULL
);
1191 m_root
->SetHasChildren(true);
1193 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1195 m_underMouse
= NULL
;
1199 wxDataViewMainWindow::~wxDataViewMainWindow()
1202 delete m_renameTimer
;
1205 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1207 wxDataViewModel
*model
= GetOwner()->GetModel();
1208 wxAutoBufferedPaintDC
dc( this );
1211 dc
.SetPen( *wxTRANSPARENT_PEN
);
1212 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1213 dc
.SetBrush( *wxWHITE_BRUSH
);
1214 wxSize
size( GetClientSize() );
1215 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1219 GetOwner()->PrepareDC( dc
);
1220 dc
.SetFont( GetFont() );
1222 wxRect update
= GetUpdateRegion().GetBox();
1223 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1225 // compute which items needs to be redrawn
1226 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1227 unsigned int item_count
=
1228 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1229 (int)(GetRowCount( ) - item_start
));
1230 unsigned int item_last
= item_start
+ item_count
;
1232 // compute which columns needs to be redrawn
1233 unsigned int cols
= GetOwner()->GetColumnCount();
1234 unsigned int col_start
= 0;
1235 unsigned int x_start
;
1236 for (x_start
= 0; col_start
< cols
; col_start
++)
1238 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1239 if (col
->IsHidden())
1240 continue; // skip it!
1242 unsigned int w
= col
->GetWidth();
1243 if (x_start
+w
>= (unsigned int)update
.x
)
1249 unsigned int col_last
= col_start
;
1250 unsigned int x_last
= x_start
;
1251 for (; col_last
< cols
; col_last
++)
1253 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1254 if (col
->IsHidden())
1255 continue; // skip it!
1257 if (x_last
> (unsigned int)update
.GetRight())
1260 x_last
+= col
->GetWidth();
1263 // Draw horizontal rules if required
1264 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1266 dc
.SetPen(m_penRule
);
1267 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1269 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1271 int y
= GetLineStart( i
);
1272 dc
.DrawLine(x_start
, y
, x_last
, y
);
1276 // Draw vertical rules if required
1277 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1279 dc
.SetPen(m_penRule
);
1280 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1283 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1285 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1286 if (col
->IsHidden())
1287 continue; // skip it
1289 dc
.DrawLine(x
, GetLineStart( item_start
),
1290 x
, GetLineStart( item_last
) );
1292 x
+= col
->GetWidth();
1295 // Draw last vertical rule
1296 dc
.DrawLine(x
, GetLineStart( item_start
),
1297 x
, GetLineStart( item_last
) );
1300 // redraw the background for the items which are selected/current
1301 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1303 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1304 if (selected
|| item
== m_currentRow
)
1306 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1307 if (item
== m_currentRow
)
1308 flags
|= wxCONTROL_CURRENT
;
1310 flags
|= wxCONTROL_FOCUSED
;
1312 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1313 wxRendererNative::Get().DrawItemSelectionRect
1323 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1326 // TODO-RTL: last column for RTL support
1327 expander
= GetOwner()->GetColumnAt( 0 );
1328 GetOwner()->SetExpanderColumn(expander
);
1331 // redraw all cells for all rows which must be repainted and all columns
1333 cell_rect
.x
= x_start
;
1334 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1337 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1338 wxDataViewRenderer
*cell
= col
->GetRenderer();
1339 cell_rect
.width
= col
->GetWidth();
1341 if (col
->IsHidden())
1342 continue; // skip it!
1344 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1346 // get the cell value and set it into the renderer
1348 wxDataViewTreeNode
*node
= NULL
;
1349 wxDataViewItem dataitem
;
1351 if (!IsVirtualList())
1353 node
= GetTreeNodeByRow(item
);
1357 dataitem
= node
->GetItem();
1359 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
1364 dataitem
= wxDataViewItem( wxUIntToPtr(item
) );
1367 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1368 cell
->SetValue( value
);
1370 if (cell
->GetWantsAttr())
1372 wxDataViewItemAttr attr
;
1373 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
1375 cell
->SetAttr( attr
);
1376 cell
->SetHasAttr( ret
);
1380 cell_rect
.y
= GetLineStart( item
);
1381 cell_rect
.height
= GetLineHeight( item
); // -1 is for the horizontal rules (?)
1383 //Draw the expander here.
1385 if ((!IsVirtualList()) && (col
== expander
))
1387 indent
= node
->GetIndentLevel();
1389 //Calculate the indent first
1390 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1392 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1393 // change the cell_rect.x to the appropriate pos
1394 int expander_x
= indent
+ EXPANDER_MARGIN
;
1395 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2) - (expander_width
/2) - EXPANDER_OFFSET
;
1396 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1397 dc
.SetPen( m_penExpander
);
1398 dc
.SetBrush( wxNullBrush
);
1399 if( node
->HasChildren() )
1401 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1403 if (m_underMouse
== node
)
1405 flag
|= wxCONTROL_CURRENT
;
1407 if( node
->IsOpen() )
1408 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
1410 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1412 //force the expander column to left-center align
1413 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1415 if (node
&& !node
->HasChildren())
1417 // Yes, if the node does not have any child, it must be a leaf which
1418 // mean that it is a temporarily created by GetTreeNodeByRow
1422 // cannot be bigger than allocated space
1423 wxSize size
= cell
->GetSize();
1424 // Because of the tree structure indent, here we should minus the width of the cell for drawing
1425 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
1426 // size.y = wxMin( size.y, cell_rect.height );
1427 size
.y
= cell_rect
.height
;
1429 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
1430 int align
= cell
->CalculateAlignment();
1432 // horizontal alignment:
1433 item_rect
.x
= cell_rect
.x
;
1434 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1435 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1436 else if (align
& wxALIGN_RIGHT
)
1437 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
1438 //else: wxALIGN_LEFT is the default
1440 // vertical alignment:
1441 item_rect
.y
= cell_rect
.y
;
1442 if (align
& wxALIGN_CENTER_VERTICAL
)
1443 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1444 else if (align
& wxALIGN_BOTTOM
)
1445 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
1446 //else: wxALIGN_TOP is the default
1449 item_rect
.x
+= PADDING_RIGHTLEFT
;
1450 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1452 //Here we add the tree indent
1453 item_rect
.x
+= indent
;
1456 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1457 state
|= wxDATAVIEW_CELL_SELECTED
;
1459 // TODO: it would be much more efficient to create a clipping
1460 // region for the entire column being rendered (in the OnPaint
1461 // of wxDataViewMainWindow) instead of a single clip region for
1462 // each cell. However it would mean that each renderer should
1463 // respect the given wxRect's top & bottom coords, eventually
1464 // violating only the left & right coords - however the user can
1465 // make its own renderer and thus we cannot be sure of that.
1466 dc
.SetClippingRegion( item_rect
);
1467 cell
->Render( item_rect
, &dc
, state
);
1468 dc
.DestroyClippingRegion();
1471 cell_rect
.x
+= cell_rect
.width
;
1475 void wxDataViewMainWindow::OnRenameTimer()
1477 // We have to call this here because changes may just have
1478 // been made and no screen update taken place.
1483 unsigned int cols
= GetOwner()->GetColumnCount();
1485 for (i
= 0; i
< cols
; i
++)
1487 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
1489 continue; // skip it!
1491 if (c
== m_currentCol
)
1493 xpos
+= c
->GetWidth();
1495 wxRect
labelRect( xpos
,
1496 GetLineStart( m_currentRow
),
1497 m_currentCol
->GetWidth(),
1498 GetLineHeight( m_currentRow
) );
1500 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1501 &labelRect
.x
, &labelRect
.y
);
1503 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1504 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1508 //------------------------------------------------------------------
1509 // Helper class for do operation on the tree node
1510 //------------------------------------------------------------------
1515 virtual ~DoJob() { }
1517 //The return value control how the tree-walker tranverse the tree
1518 // 0: Job done, stop tranverse and return
1519 // 1: Ignore the current node's subtree and continue
1520 // 2: Job not done, continue
1521 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1522 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1523 virtual int operator() ( void * n
) = 0;
1526 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1531 switch( func( node
) )
1542 wxDataViewTreeNodes nodes
= node
->GetNodes();
1543 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1545 int len_nodes
= nodes
.GetCount();
1546 int len
= leaves
.GetCount();
1547 int i
= 0, nodes_i
= 0;
1549 for(; i
< len
; i
++ )
1551 void * n
= leaves
[i
];
1552 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1554 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1557 if( Walker( nd
, func
) )
1576 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1587 wxDataViewTreeNode
* node
;
1588 node
= FindNode(parent
);
1593 node
->SetHasChildren( true );
1595 if( g_model
->IsContainer( item
) )
1597 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1598 newnode
->SetItem(item
);
1599 newnode
->SetHasChildren( true );
1600 node
->AddNode( newnode
);
1603 node
->AddLeaf( item
.GetID() );
1605 node
->ChangeSubTreeCount(1);
1613 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1615 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1616 const wxDataViewItem
& item
)
1621 if( m_currentRow
> GetRowCount() )
1622 m_currentRow
= m_count
- 1;
1624 m_selection
.Empty();
1631 wxDataViewTreeNode
* node
= FindNode(parent
);
1633 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
1634 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
1637 node
->GetChildren().Remove( item
.GetID() );
1638 //Manuplate selection
1639 if( m_selection
.GetCount() > 1 )
1641 m_selection
.Empty();
1643 bool isContainer
= false;
1644 wxDataViewTreeNodes nds
= node
->GetNodes();
1645 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
1647 if (nds
[i
]->GetItem() == item
)
1655 wxDataViewTreeNode
* n
= NULL
;
1656 wxDataViewTreeNodes nodes
= node
->GetNodes();
1657 int len
= nodes
.GetCount();
1658 for( int i
= 0; i
< len
; i
++)
1660 if( nodes
[i
]->GetItem() == item
)
1667 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
1669 node
->GetNodes().Remove( n
);
1670 sub
-= n
->GetSubTreeCount();
1671 ::DestroyTreeHelper(n
);
1673 //Make the row number invalid and get a new valid one when user call GetRowCount
1675 node
->ChangeSubTreeCount(sub
);
1677 //Change the current row to the last row if the current exceed the max row number
1678 if( m_currentRow
> GetRowCount() )
1679 m_currentRow
= m_count
- 1;
1686 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1692 wxWindow
*parent
= GetParent();
1693 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
1694 le
.SetEventObject(parent
);
1695 le
.SetModel(GetOwner()->GetModel());
1697 parent
->GetEventHandler()->ProcessEvent(le
);
1702 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
1704 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1705 /*#define MAX_VIRTUAL_WIDTH 100000
1707 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1708 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1709 Refresh( true, &rect );
1717 wxWindow
*parent
= GetParent();
1718 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
1719 le
.SetEventObject(parent
);
1720 le
.SetModel(GetOwner()->GetModel());
1723 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
1724 parent
->GetEventHandler()->ProcessEvent(le
);
1729 bool wxDataViewMainWindow::Cleared()
1734 BuildTree( GetOwner()->GetModel() );
1741 void wxDataViewMainWindow::UpdateDisplay()
1746 void wxDataViewMainWindow::OnInternalIdle()
1748 wxWindow::OnInternalIdle();
1752 RecalculateDisplay();
1757 void wxDataViewMainWindow::RecalculateDisplay()
1759 wxDataViewModel
*model
= GetOwner()->GetModel();
1766 int width
= GetEndOfLastCol();
1767 int height
= GetLineStart( GetRowCount() );
1769 SetVirtualSize( width
, height
);
1770 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1775 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1777 wxWindow::ScrollWindow( dx
, dy
, rect
);
1779 if (GetOwner()->m_headerArea
)
1780 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1783 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
1786 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
1787 int sy
= GetLineStart( rows
)/y
;
1791 wxRect rect
= GetClientRect();
1795 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
1796 for (x_start
= 0; colnum
< column
; colnum
++)
1798 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
1799 if (col
->IsHidden())
1800 continue; // skip it!
1802 w
= col
->GetWidth();
1806 int x_end
= x_start
+ w
;
1807 xe
= xx
+ rect
.width
;
1810 sx
= ( xx
+ x_end
- xe
)/x
;
1817 m_owner
->Scroll( sx
, sy
);
1820 int wxDataViewMainWindow::GetCountPerPage() const
1822 wxSize size
= GetClientSize();
1823 return size
.y
/ m_lineHeight
;
1826 int wxDataViewMainWindow::GetEndOfLastCol() const
1830 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
1832 const wxDataViewColumn
*c
=
1833 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
1836 width
+= c
->GetWidth();
1841 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
1845 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
1847 return GetLineAt( y
);
1850 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
1852 wxSize client_size
= GetClientSize();
1853 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
1854 &client_size
.x
, &client_size
.y
);
1856 //we should deal with the pixel here
1857 unsigned int row
= GetLineAt(client_size
.y
) - 1;
1859 return wxMin( GetRowCount()-1, row
);
1862 unsigned int wxDataViewMainWindow::GetRowCount()
1864 if ( m_count
== -1 )
1866 m_count
= RecalculateCount();
1872 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
1879 void wxDataViewMainWindow::SelectAllRows( bool on
)
1886 m_selection
.Clear();
1887 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
1888 m_selection
.Add( i
);
1893 unsigned int first_visible
= GetFirstVisibleRow();
1894 unsigned int last_visible
= GetLastVisibleRow();
1896 for (i
= 0; i
< m_selection
.GetCount(); i
++)
1898 unsigned int row
= m_selection
[i
];
1899 if ((row
>= first_visible
) && (row
<= last_visible
))
1902 m_selection
.Clear();
1906 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
1908 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1912 m_selection
.Add( row
);
1920 m_selection
.Remove( row
);
1926 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
1930 unsigned int tmp
= from
;
1936 for (i
= from
; i
<= to
; i
++)
1938 if (m_selection
.Index( i
) == wxNOT_FOUND
)
1941 m_selection
.Add( i
);
1946 m_selection
.Remove( i
);
1949 RefreshRows( from
, to
);
1952 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
1954 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
1956 int n
= aSelections
[i
];
1958 m_selection
.Add( n
);
1963 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
1965 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1966 m_selection
.Add( row
);
1968 m_selection
.Remove( row
);
1972 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
1974 return (m_selection
.Index( row
) != wxNOT_FOUND
);
1977 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
1979 wxWindow
*parent
= GetParent();
1980 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
1982 le
.SetEventObject(parent
);
1983 le
.SetModel(GetOwner()->GetModel());
1986 parent
->GetEventHandler()->ProcessEvent(le
);
1989 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
1991 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
1992 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1994 wxSize client_size
= GetClientSize();
1995 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1996 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1997 if (intersect_rect
.width
> 0)
1998 Refresh( true, &intersect_rect
);
2001 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2005 unsigned int tmp
= to
;
2010 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2011 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2013 wxSize client_size
= GetClientSize();
2014 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2015 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2016 if (intersect_rect
.width
> 0)
2017 Refresh( true, &intersect_rect
);
2020 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2022 wxSize client_size
= GetClientSize();
2023 int start
= GetLineStart( firstRow
);
2024 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2025 if (start
> client_size
.y
) return;
2027 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2029 Refresh( true, &rect
);
2032 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2034 wxCHECK_RET( newCurrent
< GetRowCount(),
2035 _T("invalid item index in OnArrowChar()") );
2037 // if there is no selection, we cannot move it anywhere
2038 if (!HasCurrentRow())
2041 unsigned int oldCurrent
= m_currentRow
;
2043 // in single selection we just ignore Shift as we can't select several
2045 if ( event
.ShiftDown() && !IsSingleSel() )
2047 RefreshRow( oldCurrent
);
2049 ChangeCurrentRow( newCurrent
);
2051 // select all the items between the old and the new one
2052 if ( oldCurrent
> newCurrent
)
2054 newCurrent
= oldCurrent
;
2055 oldCurrent
= m_currentRow
;
2058 SelectRows( oldCurrent
, newCurrent
, true );
2059 if (oldCurrent
!=newCurrent
)
2060 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2064 RefreshRow( oldCurrent
);
2066 // all previously selected items are unselected unless ctrl is held
2067 if ( !event
.ControlDown() )
2068 SelectAllRows(false);
2070 ChangeCurrentRow( newCurrent
);
2072 if ( !event
.ControlDown() )
2074 SelectRow( m_currentRow
, true );
2075 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2078 RefreshRow( m_currentRow
);
2081 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2084 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2088 rect
.y
= GetLineStart( row
);
2089 rect
.width
= GetEndOfLastCol();
2090 rect
.height
= GetLineHeight( row
);
2095 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2097 const wxDataViewModel
*model
= GetOwner()->GetModel();
2099 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2101 // TODO make more efficient
2106 for (r
= 0; r
< row
; r
++)
2108 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2109 if (!node
) return start
;
2111 wxDataViewItem item
= node
->GetItem();
2113 if (node
&& !node
->HasChildren())
2115 // Yes, if the node does not have any child, it must be a leaf which
2116 // mean that it is a temporarily created by GetTreeNodeByRow
2120 unsigned int cols
= GetOwner()->GetColumnCount();
2122 int height
= m_lineHeight
;
2123 for (col
= 0; col
< cols
; col
++)
2125 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2126 if (column
->IsHidden())
2127 continue; // skip it!
2129 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2130 continue; // skip it!
2132 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2134 model
->GetValue( value
, item
, column
->GetModelColumn() );
2135 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2136 renderer2
->SetValue( value
);
2137 height
= wxMax( height
, renderer
->GetSize().y
);
2148 return row
* m_lineHeight
;
2152 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2154 const wxDataViewModel
*model
= GetOwner()->GetModel();
2156 // check for the easy case first
2157 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2158 return y
/ m_lineHeight
;
2160 // TODO make more efficient
2161 unsigned int row
= 0;
2162 unsigned int yy
= 0;
2165 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2168 // not really correct...
2169 return row
+ ((y
-yy
) / m_lineHeight
);
2172 wxDataViewItem item
= node
->GetItem();
2174 if (node
&& !node
->HasChildren())
2176 // Yes, if the node does not have any child, it must be a leaf which
2177 // mean that it is a temporarily created by GetTreeNodeByRow
2181 unsigned int cols
= GetOwner()->GetColumnCount();
2183 int height
= m_lineHeight
;
2184 for (col
= 0; col
< cols
; col
++)
2186 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2187 if (column
->IsHidden())
2188 continue; // skip it!
2190 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2191 continue; // skip it!
2193 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2195 model
->GetValue( value
, item
, column
->GetModelColumn() );
2196 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2197 renderer2
->SetValue( value
);
2198 height
= wxMax( height
, renderer
->GetSize().y
);
2209 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2211 const wxDataViewModel
*model
= GetOwner()->GetModel();
2213 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2215 wxASSERT( !IsVirtualList() );
2217 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2218 // wxASSERT( node );
2219 if (!node
) return m_lineHeight
;
2221 wxDataViewItem item
= node
->GetItem();
2223 if (node
&& !node
->HasChildren())
2225 // Yes, if the node does not have any child, it must be a leaf which
2226 // mean that it is a temporarily created by GetTreeNodeByRow
2230 int height
= m_lineHeight
;
2232 unsigned int cols
= GetOwner()->GetColumnCount();
2234 for (col
= 0; col
< cols
; col
++)
2236 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2237 if (column
->IsHidden())
2238 continue; // skip it!
2240 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2241 continue; // skip it!
2243 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2245 model
->GetValue( value
, item
, column
->GetModelColumn() );
2246 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2247 renderer2
->SetValue( value
);
2248 height
= wxMax( height
, renderer
->GetSize().y
);
2255 return m_lineHeight
;
2259 class RowToItemJob
: public DoJob
2262 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2263 virtual ~RowToItemJob() { }
2265 virtual int operator() ( wxDataViewTreeNode
* node
)
2268 if( current
== static_cast<int>(row
))
2270 ret
= node
->GetItem();
2274 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2276 current
+= node
->GetSubTreeCount();
2281 //If the current has no child node, we can find the desired item of the row number directly.
2282 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2283 if( node
->GetNodes().GetCount() == 0)
2285 int index
= static_cast<int>(row
) - current
- 1;
2286 ret
= node
->GetChildren().Item( index
);
2293 virtual int operator() ( void * n
)
2296 if( current
== static_cast<int>(row
))
2298 ret
= wxDataViewItem( n
);
2303 wxDataViewItem
GetResult(){ return ret
; }
2310 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2314 return wxDataViewItem( wxUIntToPtr(row
) );
2318 RowToItemJob
job( row
, -2 );
2319 Walker( m_root
, job
);
2320 return job
.GetResult();
2324 class RowToTreeNodeJob
: public DoJob
2327 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2330 this->current
= current
;
2334 virtual ~RowToTreeNodeJob(){ }
2336 virtual int operator() ( wxDataViewTreeNode
* node
)
2339 if( current
== static_cast<int>(row
))
2345 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2347 current
+= node
->GetSubTreeCount();
2353 //If the current has no child node, we can find the desired item of the row number directly.
2354 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2355 if( node
->GetNodes().GetCount() == 0)
2357 int index
= static_cast<int>(row
) - current
- 1;
2358 void * n
= node
->GetChildren().Item( index
);
2359 ret
= new wxDataViewTreeNode( parent
);
2360 ret
->SetItem( wxDataViewItem( n
));
2361 ret
->SetHasChildren(false);
2370 virtual int operator() ( void * n
)
2373 if( current
== static_cast<int>(row
))
2375 ret
= new wxDataViewTreeNode( parent
);
2376 ret
->SetItem( wxDataViewItem( n
));
2377 ret
->SetHasChildren(false);
2383 wxDataViewTreeNode
* GetResult(){ return ret
; }
2387 wxDataViewTreeNode
* ret
;
2388 wxDataViewTreeNode
* parent
;
2392 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2394 wxASSERT( !IsVirtualList() );
2396 RowToTreeNodeJob
job( row
, -2, m_root
);
2397 Walker( m_root
, job
);
2398 return job
.GetResult();
2401 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2403 wxWindow
*parent
= GetParent();
2404 wxDataViewEvent
le(type
, parent
->GetId());
2406 le
.SetEventObject(parent
);
2407 le
.SetModel(GetOwner()->GetModel());
2410 parent
->GetEventHandler()->ProcessEvent(le
);
2414 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2416 if (IsVirtualList())
2419 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2422 if( node
->HasChildren())
2424 if( !node
->IsOpen())
2426 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2427 //Check if the user prevent expanding
2428 if( e
.GetSkipped() )
2432 //Here I build the children of current node
2433 if( node
->GetChildrenNumber() == 0 )
2436 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2440 //Send the expanded event
2441 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2445 SelectRow( row
, false );
2446 SelectRow( row
+ 1, true );
2447 ChangeCurrentRow( row
+ 1 );
2448 SendSelectionChangedEvent( GetItemByRow(row
+1));
2456 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2458 if (IsVirtualList())
2461 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2464 wxDataViewTreeNode
* nd
= node
;
2466 if( node
->HasChildren() && node
->IsOpen() )
2468 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2469 if( e
.GetSkipped() )
2474 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2478 node
= node
->GetParent();
2481 int parent
= GetRowByItem( node
->GetItem() );
2484 SelectRow( row
, false);
2485 SelectRow(parent
, true );
2486 ChangeCurrentRow( parent
);
2487 SendSelectionChangedEvent( node
->GetItem() );
2491 if( !nd
->HasChildren())
2496 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2498 wxDataViewModel
* model
= GetOwner()->GetModel();
2502 //Compose the a parent-chain of the finding item
2504 list
.DeleteContents( true );
2505 wxDataViewItem
it( item
);
2508 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2509 list
.Insert( pItem
);
2510 it
= model
->GetParent( it
);
2513 //Find the item along the parent-chain.
2514 //This algorithm is designed to speed up the node-finding method
2515 wxDataViewTreeNode
* node
= m_root
;
2516 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
2518 if( node
->HasChildren() )
2520 if( node
->GetChildrenNumber() == 0 )
2523 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2526 wxDataViewTreeNodes nodes
= node
->GetNodes();
2530 for (i
= 0; i
< nodes
.GetCount(); i
++)
2532 if (nodes
[i
]->GetItem() == (**iter
))
2534 if (nodes
[i
]->GetItem() == item
)
2551 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
2553 wxDataViewColumn
*col
= NULL
;
2554 unsigned int cols
= GetOwner()->GetColumnCount();
2555 unsigned int colnum
= 0;
2557 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
2558 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
2560 col
= GetOwner()->GetColumnAt(colnum
);
2561 if (col
->IsHidden())
2562 continue; // skip it!
2564 unsigned int w
= col
->GetWidth();
2565 if (x_start
+w
>= (unsigned int)x
)
2572 item
= GetItemByRow( GetLineAt( y
) );
2575 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
2577 int row
= GetRowByItem(item
);
2578 int y
= GetLineStart( row
);
2579 int h
= GetLineHeight( m_lineHeight
);
2581 wxDataViewColumn
*col
= NULL
;
2582 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
2584 col
= GetOwner()->GetColumnAt( i
);
2585 x
+= col
->GetWidth();
2586 if( GetOwner()->GetColumnAt(i
+1) == column
)
2589 int w
= col
->GetWidth();
2590 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
2591 return wxRect(x
, y
, w
, h
);
2594 int wxDataViewMainWindow::RecalculateCount()
2598 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
2600 return list_model
->GetLastIndex() + 1;
2602 return list_model
->GetLastIndex() - 1;
2607 return m_root
->GetSubTreeCount();
2611 class ItemToRowJob
: public DoJob
2614 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
2621 //Maybe binary search will help to speed up this process
2622 virtual int operator() ( wxDataViewTreeNode
* node
)
2625 if( node
->GetItem() == item
)
2630 if( node
->GetItem() == **m_iter
)
2637 ret
+= node
->GetSubTreeCount();
2643 virtual int operator() ( void * n
)
2646 if( n
== item
.GetID() )
2650 //the row number is begin from zero
2651 int GetResult() { return ret
-1; }
2654 ItemList::const_iterator m_iter
;
2655 wxDataViewItem item
;
2660 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
2662 const wxDataViewModel
* model
= GetOwner()->GetModel();
2668 return wxPtrToUInt( item
.GetID() );
2675 //Compose the a parent-chain of the finding item
2677 wxDataViewItem
* pItem
;
2678 list
.DeleteContents( true );
2679 wxDataViewItem
it( item
);
2682 pItem
= new wxDataViewItem( it
);
2683 list
.Insert( pItem
);
2684 it
= model
->GetParent( it
);
2686 pItem
= new wxDataViewItem( );
2687 list
.Insert( pItem
);
2689 ItemToRowJob
job( item
, list
.begin() );
2690 Walker(m_root
, job
);
2691 return job
.GetResult();
2695 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2697 if( !model
->IsContainer( item
) )
2700 wxDataViewItemArray children
;
2701 unsigned int num
= model
->GetChildren( item
, children
);
2702 unsigned int index
= 0;
2703 while( index
< num
)
2705 if( model
->IsContainer( children
[index
] ) )
2707 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2708 n
->SetItem(children
[index
]);
2709 n
->SetHasChildren( true );
2714 node
->AddLeaf( children
[index
].GetID() );
2718 node
->SetSubTreeCount( num
);
2719 wxDataViewTreeNode
* n
= node
->GetParent();
2721 n
->ChangeSubTreeCount(num
);
2725 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2729 if (GetOwner()->GetModel()->IsVirtualListModel())
2735 m_root
= new wxDataViewTreeNode( NULL
);
2736 m_root
->SetHasChildren(true);
2738 //First we define a invalid item to fetch the top-level elements
2739 wxDataViewItem item
;
2741 BuildTreeHelper( model
, item
, m_root
);
2745 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2747 if( node
->GetNodeNumber() != 0 )
2749 int len
= node
->GetNodeNumber();
2751 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
2752 for(; i
< len
; i
++ )
2754 DestroyTreeHelper(nodes
[i
]);
2760 void wxDataViewMainWindow::DestroyTree()
2762 if (!IsVirtualList())
2764 ::DestroyTreeHelper(m_root
);
2770 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2772 if ( GetParent()->HandleAsNavigationKey(event
) )
2775 // no item -> nothing to do
2776 if (!HasCurrentRow())
2782 // don't use m_linesPerPage directly as it might not be computed yet
2783 const int pageSize
= GetCountPerPage();
2784 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2786 switch ( event
.GetKeyCode() )
2790 if (m_currentRow
> 0)
2792 wxWindow
*parent
= GetParent();
2793 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
2794 le
.SetItem( GetItemByRow(m_currentRow
) );
2795 le
.SetEventObject(parent
);
2796 le
.SetModel(GetOwner()->GetModel());
2798 parent
->GetEventHandler()->ProcessEvent(le
);
2803 if ( m_currentRow
> 0 )
2804 OnArrowChar( m_currentRow
- 1, event
);
2808 if ( m_currentRow
< GetRowCount() - 1 )
2809 OnArrowChar( m_currentRow
+ 1, event
);
2811 //Add the process for tree expanding/collapsing
2813 OnCollapsing(m_currentRow
);
2816 OnExpanding( m_currentRow
);
2820 OnArrowChar( GetRowCount() - 1, event
);
2825 OnArrowChar( 0, event
);
2830 int steps
= pageSize
- 1;
2831 int index
= m_currentRow
- steps
;
2835 OnArrowChar( index
, event
);
2841 int steps
= pageSize
- 1;
2842 unsigned int index
= m_currentRow
+ steps
;
2843 unsigned int count
= GetRowCount();
2844 if ( index
>= count
)
2847 OnArrowChar( index
, event
);
2856 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
2858 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2860 // let the base handle mouse wheel events.
2865 int x
= event
.GetX();
2866 int y
= event
.GetY();
2867 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2868 wxDataViewColumn
*col
= NULL
;
2871 unsigned int cols
= GetOwner()->GetColumnCount();
2873 for (i
= 0; i
< cols
; i
++)
2875 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
2877 continue; // skip it!
2879 if (x
< xpos
+ c
->GetWidth())
2884 xpos
+= c
->GetWidth();
2889 wxDataViewRenderer
*cell
= col
->GetRenderer();
2890 unsigned int current
= GetLineAt( y
);
2891 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
2893 // Unselect all if below the last row ?
2897 //Test whether the mouse is hovered on the tree item button
2899 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
2901 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
2902 if( node
!=NULL
&& node
->HasChildren() )
2904 int indent
= node
->GetIndentLevel();
2905 indent
= GetOwner()->GetIndent()*indent
;
2906 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
2907 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
2908 m_lineHeight
-2*EXPANDER_MARGIN
,
2909 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
2910 if( rect
.Contains( x
, y
) )
2912 //So the mouse is over the expander
2914 if (m_underMouse
&& m_underMouse
!= node
)
2916 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
2917 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
2919 if (m_underMouse
!= node
)
2921 //wxLogMessage("Do the row: %d", current);
2922 RefreshRow(current
);
2924 m_underMouse
= node
;
2927 if (node
!=NULL
&& !node
->HasChildren())
2932 if (m_underMouse
!= NULL
)
2934 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
2935 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
2936 m_underMouse
= NULL
;
2940 wxDataViewModel
*model
= GetOwner()->GetModel();
2942 if (event
.Dragging())
2944 if (m_dragCount
== 0)
2946 // we have to report the raw, physical coords as we want to be
2947 // able to call HitTest(event.m_pointDrag) from the user code to
2948 // get the item being dragged
2949 m_dragStart
= event
.GetPosition();
2954 if (m_dragCount
!= 3)
2957 if (event
.LeftIsDown())
2959 // Notify cell about drag
2968 bool forceClick
= false;
2970 if (event
.ButtonDClick())
2972 m_renameTimer
->Stop();
2973 m_lastOnSame
= false;
2976 wxDataViewItem item
= GetItemByRow(current
);
2977 bool ignore_other_columns
=
2978 ((GetOwner()->GetExpanderColumn() != col
) &&
2979 (model
->IsContainer(item
)) &&
2980 (!model
->HasContainerColumns(item
)));
2982 if (event
.LeftDClick())
2984 if ( current
== m_lineLastClicked
)
2986 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
2989 model
->GetValue( value
, item
, col
->GetModelColumn() );
2990 cell
->SetValue( value
);
2991 wxRect
cell_rect( xpos
, GetLineStart( current
),
2992 col
->GetWidth(), GetLineHeight( current
) );
2993 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
2998 wxWindow
*parent
= GetParent();
2999 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3001 le
.SetEventObject(parent
);
3002 le
.SetModel(GetOwner()->GetModel());
3004 parent
->GetEventHandler()->ProcessEvent(le
);
3010 // The first click was on another item, so don't interpret this as
3011 // a double click, but as a simple click instead
3018 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3020 // select single line
3021 SelectAllRows( false );
3022 SelectRow( m_lineSelectSingleOnUp
, true );
3025 //Process the event of user clicking the expander
3026 bool expander
= false;
3027 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3029 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3030 if( node
!=NULL
&& node
->HasChildren() )
3032 int indent
= node
->GetIndentLevel();
3033 indent
= GetOwner()->GetIndent()*indent
;
3034 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
3035 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
3036 m_lineHeight
-2*EXPANDER_MARGIN
,
3037 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
3039 if( rect
.Contains( x
, y
) )
3042 if( node
->IsOpen() )
3043 OnCollapsing(current
);
3045 OnExpanding( current
);
3048 if (node
&& !node
->HasChildren())
3051 //If the user click the expander, we do not do editing even if the column with expander are editable
3052 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3054 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3055 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3057 m_renameTimer
->Start( 100, true );
3061 m_lastOnSame
= false;
3062 m_lineSelectSingleOnUp
= (unsigned int)-1;
3066 // This is necessary, because after a DnD operation in
3067 // from and to ourself, the up event is swallowed by the
3068 // DnD code. So on next non-up event (which means here and
3069 // now) m_lineSelectSingleOnUp should be reset.
3070 m_lineSelectSingleOnUp
= (unsigned int)-1;
3073 if (event
.RightDown())
3075 m_lineBeforeLastClicked
= m_lineLastClicked
;
3076 m_lineLastClicked
= current
;
3078 // If the item is already selected, do not update the selection.
3079 // Multi-selections should not be cleared if a selected item is clicked.
3080 if (!IsRowSelected(current
))
3082 SelectAllRows(false);
3083 ChangeCurrentRow(current
);
3084 SelectRow(m_currentRow
,true);
3085 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3089 model
->GetValue( value
, item
, col
->GetModelColumn() );
3090 wxWindow
*parent
= GetParent();
3091 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3093 le
.SetEventObject(parent
);
3094 le
.SetModel(GetOwner()->GetModel());
3096 parent
->GetEventHandler()->ProcessEvent(le
);
3098 else if (event
.MiddleDown())
3101 if (event
.LeftDown() || forceClick
)
3105 m_lineBeforeLastClicked
= m_lineLastClicked
;
3106 m_lineLastClicked
= current
;
3108 unsigned int oldCurrentRow
= m_currentRow
;
3109 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3111 bool cmdModifierDown
= event
.CmdDown();
3112 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3114 if ( IsSingleSel() || !IsRowSelected(current
) )
3116 SelectAllRows( false );
3117 ChangeCurrentRow(current
);
3118 SelectRow(m_currentRow
,true);
3119 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3121 else // multi sel & current is highlighted & no mod keys
3123 m_lineSelectSingleOnUp
= current
;
3124 ChangeCurrentRow(current
); // change focus
3127 else // multi sel & either ctrl or shift is down
3129 if (cmdModifierDown
)
3131 ChangeCurrentRow(current
);
3132 ReverseRowSelection(m_currentRow
);
3133 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3135 else if (event
.ShiftDown())
3137 ChangeCurrentRow(current
);
3139 unsigned int lineFrom
= oldCurrentRow
,
3142 if ( lineTo
< lineFrom
)
3145 lineFrom
= m_currentRow
;
3148 SelectRows(lineFrom
, lineTo
, true);
3149 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3151 else // !ctrl, !shift
3153 // test in the enclosing if should make it impossible
3154 wxFAIL_MSG( _T("how did we get here?") );
3158 if (m_currentRow
!= oldCurrentRow
)
3159 RefreshRow( oldCurrentRow
);
3161 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3163 // Update selection here...
3166 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3167 (current
== oldCurrentRow
)) && oldWasSelected
;
3169 // Call LeftClick after everything else as under GTK+
3170 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3172 // notify cell about right click
3174 model
->GetValue( value
, item
, col
->GetModelColumn() );
3175 cell
->SetValue( value
);
3176 wxRect
cell_rect( xpos
, GetLineStart( current
),
3177 col
->GetWidth(), GetLineHeight( current
) );
3178 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3183 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3187 if (HasCurrentRow())
3193 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3197 if (HasCurrentRow())
3203 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3205 if( m_selection
.GetCount() != 1 )
3206 return wxDataViewItem();
3208 return GetItemByRow( m_selection
.Item(0));
3211 //-----------------------------------------------------------------------------
3213 //-----------------------------------------------------------------------------
3214 WX_DEFINE_LIST(wxDataViewColumnList
)
3216 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3218 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3219 EVT_SIZE(wxDataViewCtrl::OnSize
)
3222 wxDataViewCtrl::~wxDataViewCtrl()
3225 GetModel()->RemoveNotifier( m_notifier
);
3230 void wxDataViewCtrl::Init()
3232 m_cols
.DeleteContents(true);
3235 // No sorting column at start
3236 m_sortingColumnIdx
= wxNOT_FOUND
;
3238 m_headerArea
= NULL
;
3241 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3242 const wxPoint
& pos
, const wxSize
& size
,
3243 long style
, const wxValidator
& validator
)
3245 if ( (style
& wxBORDER_MASK
) == 0)
3246 style
|= wxBORDER_SUNKEN
;
3250 if (!wxControl::Create( parent
, id
, pos
, size
,
3251 style
| wxScrolledWindowStyle
, validator
))
3254 SetInitialSize(size
);
3257 MacSetClipChildren( true );
3260 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3262 if (HasFlag(wxDV_NO_HEADER
))
3263 m_headerArea
= NULL
;
3265 m_headerArea
= new wxDataViewHeaderWindow(this);
3267 SetTargetWindow( m_clientArea
);
3269 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3271 sizer
->Add( m_headerArea
, 0, wxGROW
);
3272 sizer
->Add( m_clientArea
, 1, wxGROW
);
3279 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3283 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3286 // we need to process arrows ourselves for scrolling
3287 if ( nMsg
== WM_GETDLGCODE
)
3289 rc
|= DLGC_WANTARROWS
;
3297 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3299 wxSize newsize
= size
;
3300 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3301 newsize
.y
-= m_headerArea
->GetSize().y
;
3306 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3308 // We need to override OnSize so that our scrolled
3309 // window a) does call Layout() to use sizers for
3310 // positioning the controls but b) does not query
3311 // the sizer for their size and use that for setting
3312 // the scrollable area as set that ourselves by
3313 // calling SetScrollbar() further down.
3320 void wxDataViewCtrl::SetFocus()
3323 m_clientArea
->SetFocus();
3326 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3328 if (!wxDataViewCtrlBase::AssociateModel( model
))
3331 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3333 model
->AddNotifier( m_notifier
);
3335 m_clientArea
->DestroyTree();
3337 m_clientArea
->BuildTree(model
);
3339 m_clientArea
->UpdateDisplay();
3344 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3346 if (!wxDataViewCtrlBase::AppendColumn(col
))
3349 m_cols
.Append( col
);
3350 OnColumnsCountChanged();
3354 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3356 if (!wxDataViewCtrlBase::PrependColumn(col
))
3359 m_cols
.Insert( col
);
3360 OnColumnsCountChanged();
3364 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3366 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
3369 m_cols
.Insert( pos
, col
);
3370 OnColumnsCountChanged();
3374 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
3377 m_headerArea
->UpdateColumn(idx
);
3379 m_clientArea
->UpdateDisplay();
3382 void wxDataViewCtrl::OnColumnsCountChanged()
3385 m_headerArea
->SetColumnCount(GetColumnCount());
3387 m_clientArea
->UpdateDisplay();
3390 void wxDataViewCtrl::DoSetExpanderColumn()
3392 m_clientArea
->UpdateDisplay();
3395 void wxDataViewCtrl::DoSetIndent()
3397 m_clientArea
->UpdateDisplay();
3400 unsigned int wxDataViewCtrl::GetColumnCount() const
3402 return m_cols
.GetCount();
3405 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
3410 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
3412 // columns can't be reordered if there is no header window which allows
3414 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
3417 return GetColumn(idx
);
3420 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
3422 const unsigned count
= m_cols
.size();
3423 for ( unsigned n
= 0; n
< count
; n
++ )
3425 if ( m_cols
[n
] == column
)
3432 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
3433 unsigned int WXUNUSED(new_pos
))
3435 // do _not_ reorder m_cols elements here, they should always be in the
3436 // order in which columns were added, we only display the columns in
3438 m_clientArea
->UpdateDisplay();
3441 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3443 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3448 OnColumnsCountChanged();
3453 bool wxDataViewCtrl::ClearColumns()
3456 OnColumnsCountChanged();
3460 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3464 unsigned int len
= GetColumnCount();
3465 for ( unsigned int i
= 0; i
< len
; i
++ )
3467 wxDataViewColumn
* col
= GetColumnAt(i
);
3468 if (col
->IsHidden())
3470 ret
+= col
->GetWidth();
3473 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
3480 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3482 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
3483 : GetColumn(m_sortingColumnIdx
);
3486 //Selection code with wxDataViewItem as parameters
3487 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3489 return m_clientArea
->GetSelection();
3492 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3495 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3496 int len
= selection
.GetCount();
3497 for( int i
= 0; i
< len
; i
++)
3499 unsigned int row
= selection
[i
];
3500 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3505 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3507 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
3508 int len
= sel
.GetCount();
3509 for( int i
= 0; i
< len
; i
++ )
3511 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3513 selection
.Add( static_cast<unsigned int>(row
) );
3515 m_clientArea
->SetSelections( selection
);
3518 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3520 int row
= m_clientArea
->GetRowByItem( item
);
3523 //Unselect all rows before select another in the single select mode
3524 if (m_clientArea
->IsSingleSel())
3525 m_clientArea
->SelectAllRows(false);
3526 m_clientArea
->SelectRow(row
, true);
3530 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3532 int row
= m_clientArea
->GetRowByItem( item
);
3534 m_clientArea
->SelectRow(row
, false);
3537 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3539 int row
= m_clientArea
->GetRowByItem( item
);
3542 return m_clientArea
->IsRowSelected(row
);
3547 //Selection code with row number as parameter
3548 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3551 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3552 int len
= selection
.GetCount();
3553 for( int i
= 0; i
< len
; i
++)
3555 unsigned int row
= selection
[i
];
3561 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3563 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
3564 int len
= sel
.GetCount();
3565 for( int i
= 0; i
< len
; i
++ )
3569 selection
.Add( static_cast<unsigned int>(row
) );
3571 m_clientArea
->SetSelections( selection
);
3574 void wxDataViewCtrl::Select( int row
)
3578 if (m_clientArea
->IsSingleSel())
3579 m_clientArea
->SelectAllRows(false);
3580 m_clientArea
->SelectRow( row
, true );
3584 void wxDataViewCtrl::Unselect( int row
)
3587 m_clientArea
->SelectRow(row
, false);
3590 bool wxDataViewCtrl::IsSelected( int row
) const
3593 return m_clientArea
->IsRowSelected(row
);
3597 void wxDataViewCtrl::SelectRange( int from
, int to
)
3600 for( int i
= from
; i
< to
; i
++ )
3602 m_clientArea
->Select(sel
);
3605 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3607 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3608 for( int i
= from
; i
< to
; i
++ )
3609 if( sel
.Index( i
) != wxNOT_FOUND
)
3611 m_clientArea
->SetSelections(sel
);
3614 void wxDataViewCtrl::SelectAll()
3616 m_clientArea
->SelectAllRows(true);
3619 void wxDataViewCtrl::UnselectAll()
3621 m_clientArea
->SelectAllRows(false);
3624 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3628 if( row
> (int) m_clientArea
->GetRowCount() )
3629 row
= m_clientArea
->GetRowCount();
3631 int first
= m_clientArea
->GetFirstVisibleRow();
3632 int last
= m_clientArea
->GetLastVisibleRow();
3634 m_clientArea
->ScrollTo( row
, column
);
3635 else if( row
> last
)
3636 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3638 m_clientArea
->ScrollTo( first
, column
);
3641 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3643 int row
= m_clientArea
->GetRowByItem(item
);
3646 if( column
== NULL
)
3647 EnsureVisible(row
, -1);
3649 EnsureVisible( row
, GetColumnIndex(column
) );
3654 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
3656 m_clientArea
->HitTest(point
, item
, column
);
3659 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
3661 return m_clientArea
->GetItemRect(item
, column
);
3664 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
3666 return m_clientArea
->GetItemByRow( row
);
3669 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
3671 return m_clientArea
->GetRowByItem( item
);
3674 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
3676 int row
= m_clientArea
->GetRowByItem( item
);
3678 m_clientArea
->Expand(row
);
3681 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
3683 int row
= m_clientArea
->GetRowByItem( item
);
3685 m_clientArea
->Collapse(row
);
3689 // !wxUSE_GENERICDATAVIEWCTRL
3692 // wxUSE_DATAVIEWCTRL