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 const wxHeaderColumn
& GetColumn(unsigned int idx
) const
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
); }
486 bool IsExpanded( unsigned int row
) const;
488 bool EnableDragSource( const wxDataFormat
&format
)
490 m_dragFormat
= format
;
491 m_dragEnabled
= format
!= wxDF_INVALID
;
494 bool EnableDropTarget( const wxDataFormat
&format
)
496 m_dropFormat
= format
;
497 m_dropEnabled
= format
!= wxDF_INVALID
;
502 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
503 //We did not need this temporarily
504 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
506 int RecalculateCount();
508 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
509 void OnExpanding( unsigned int row
);
510 void OnCollapsing( unsigned int row
);
512 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
515 wxDataViewCtrl
*m_owner
;
519 wxDataViewColumn
*m_currentCol
;
520 unsigned int m_currentRow
;
521 wxDataViewSelection m_selection
;
523 wxDataViewRenameTimer
*m_renameTimer
;
532 wxDataFormat m_dragFormat
;
535 wxDataFormat m_dropFormat
;
537 // for double click logic
538 unsigned int m_lineLastClicked
,
539 m_lineBeforeLastClicked
,
540 m_lineSelectSingleOnUp
;
542 // the pen used to draw horiz/vertical rules
545 // the pen used to draw the expander and the lines
548 //This is the tree structure of the model
549 wxDataViewTreeNode
* m_root
;
551 //This is the tree node under the cursor
552 wxDataViewTreeNode
* m_underMouse
;
554 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
555 DECLARE_EVENT_TABLE()
558 // ---------------------------------------------------------
559 // wxGenericDataViewModelNotifier
560 // ---------------------------------------------------------
562 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
565 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
566 { m_mainWindow
= mainWindow
; }
568 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
569 { return m_mainWindow
->ItemAdded( parent
, item
); }
570 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
571 { return m_mainWindow
->ItemDeleted( parent
, item
); }
572 virtual bool ItemChanged( const wxDataViewItem
& item
)
573 { return m_mainWindow
->ItemChanged(item
); }
574 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
575 { return m_mainWindow
->ValueChanged( item
, col
); }
576 virtual bool Cleared()
577 { return m_mainWindow
->Cleared(); }
578 virtual void Resort()
579 { m_mainWindow
->Resort(); }
581 wxDataViewMainWindow
*m_mainWindow
;
584 // ---------------------------------------------------------
585 // wxDataViewRenderer
586 // ---------------------------------------------------------
588 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
590 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
591 wxDataViewCellMode mode
,
593 wxDataViewRendererBase( varianttype
, mode
, align
)
602 wxDataViewRenderer::~wxDataViewRenderer()
608 wxDC
*wxDataViewRenderer::GetDC()
612 if (GetOwner() == NULL
)
614 if (GetOwner()->GetOwner() == NULL
)
616 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
622 void wxDataViewRenderer::SetAlignment( int align
)
627 int wxDataViewRenderer::GetAlignment() const
632 int wxDataViewRenderer::CalculateAlignment() const
634 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
636 if (GetOwner() == NULL
)
637 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
639 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
645 // ---------------------------------------------------------
646 // wxDataViewCustomRenderer
647 // ---------------------------------------------------------
649 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
651 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
652 wxDataViewCellMode mode
, int align
) :
653 wxDataViewRenderer( varianttype
, mode
, align
)
657 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
659 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
660 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
661 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
662 view
->GetForegroundColour();
663 dc
->SetTextForeground(col
);
664 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
667 // ---------------------------------------------------------
668 // wxDataViewTextRenderer
669 // ---------------------------------------------------------
671 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
673 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
674 wxDataViewCellMode mode
, int align
) :
675 wxDataViewCustomRenderer( varianttype
, mode
, align
)
679 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
681 m_text
= value
.GetString();
686 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
691 bool wxDataViewTextRenderer::HasEditorCtrl()
696 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
697 wxRect labelRect
, const wxVariant
&value
)
699 return new wxTextCtrl( parent
, wxID_ANY
, value
,
700 wxPoint(labelRect
.x
,labelRect
.y
),
701 wxSize(labelRect
.width
,labelRect
.height
) );
704 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
706 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
707 value
= text
->GetValue();
711 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
713 RenderText( m_text
, 0, cell
, dc
, state
);
717 wxSize
wxDataViewTextRenderer::GetSize() const
719 const wxDataViewCtrl
*view
= GetView();
723 view
->GetTextExtent( m_text
, &x
, &y
);
724 return wxSize( x
, y
);
726 return wxSize(80,20);
729 // ---------------------------------------------------------
730 // wxDataViewTextRendererAttr
731 // ---------------------------------------------------------
733 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
735 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
736 wxDataViewCellMode mode
, int align
) :
737 wxDataViewTextRenderer( varianttype
, mode
, align
)
742 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
749 if (m_attr
.HasColour())
751 colour
= dc
->GetTextForeground();
752 dc
->SetTextForeground( m_attr
.GetColour() );
755 if (m_attr
.GetBold() || m_attr
.GetItalic())
757 font
= dc
->GetFont();
758 wxFont myfont
= font
;
759 if (m_attr
.GetBold())
760 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
761 if (m_attr
.GetItalic())
762 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
763 dc
->SetFont( myfont
);
767 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
772 if (m_attr
.HasColour())
773 dc
->SetTextForeground( colour
);
775 if (m_attr
.GetBold() || m_attr
.GetItalic())
783 // ---------------------------------------------------------
784 // wxDataViewBitmapRenderer
785 // ---------------------------------------------------------
787 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
789 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
790 wxDataViewCellMode mode
, int align
) :
791 wxDataViewCustomRenderer( varianttype
, mode
, align
)
795 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
797 if (value
.GetType() == wxT("wxBitmap"))
799 if (value
.GetType() == wxT("wxIcon"))
805 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
810 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
813 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
814 else if (m_icon
.Ok())
815 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
820 wxSize
wxDataViewBitmapRenderer::GetSize() const
823 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
824 else if (m_icon
.Ok())
825 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
827 return wxSize(16,16);
830 // ---------------------------------------------------------
831 // wxDataViewToggleRenderer
832 // ---------------------------------------------------------
834 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
836 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
837 wxDataViewCellMode mode
, int align
) :
838 wxDataViewCustomRenderer( varianttype
, mode
, align
)
843 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
845 m_toggle
= value
.GetBool();
850 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
855 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
857 // User wxRenderer here
860 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
862 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
867 flags
|= wxCONTROL_CHECKED
;
868 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
869 flags
|= wxCONTROL_DISABLED
;
871 wxRendererNative::Get().DrawCheckBox(
872 GetOwner()->GetOwner(),
880 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
881 wxDataViewModel
*model
,
882 const wxDataViewItem
& item
, unsigned int col
)
884 bool value
= !m_toggle
;
885 wxVariant variant
= value
;
886 model
->SetValue( variant
, item
, col
);
887 model
->ValueChanged( item
, col
);
891 wxSize
wxDataViewToggleRenderer::GetSize() const
893 return wxSize(20,20);
896 // ---------------------------------------------------------
897 // wxDataViewProgressRenderer
898 // ---------------------------------------------------------
900 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
902 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
903 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
904 wxDataViewCustomRenderer( varianttype
, mode
, align
)
910 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
914 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
916 m_value
= (long) value
;
918 if (m_value
< 0) m_value
= 0;
919 if (m_value
> 100) m_value
= 100;
924 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
926 value
= (long) m_value
;
930 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
932 double pct
= (double)m_value
/ 100.0;
934 bar
.width
= (int)(cell
.width
* pct
);
935 dc
->SetPen( *wxTRANSPARENT_PEN
);
936 dc
->SetBrush( *wxBLUE_BRUSH
);
937 dc
->DrawRectangle( bar
);
939 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
940 dc
->SetPen( *wxBLACK_PEN
);
941 dc
->DrawRectangle( cell
);
946 wxSize
wxDataViewProgressRenderer::GetSize() const
948 return wxSize(40,12);
951 // ---------------------------------------------------------
952 // wxDataViewDateRenderer
953 // ---------------------------------------------------------
955 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
957 #if wxUSE_DATE_RENDERER_POPUP
959 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
962 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
963 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
964 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
969 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
970 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
971 sizer
->Add( m_cal
, 1, wxGROW
);
976 void OnCalendar( wxCalendarEvent
&event
);
978 wxCalendarCtrl
*m_cal
;
979 wxDataViewModel
*m_model
;
981 const wxDataViewItem
& m_item
;
984 virtual void OnDismiss()
989 DECLARE_EVENT_TABLE()
992 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
993 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
996 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
998 wxDateTime date
= event
.GetDate();
999 wxVariant value
= date
;
1000 m_model
->SetValue( value
, m_item
, m_col
);
1001 m_model
->ValueChanged( m_item
, m_col
);
1005 #endif // wxUSE_DATE_RENDERER_POPUP
1007 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1009 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1010 wxDataViewCellMode mode
, int align
) :
1011 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1015 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1017 m_date
= value
.GetDateTime();
1022 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1028 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1030 wxString tmp
= m_date
.FormatDate();
1031 RenderText( tmp
, 0, cell
, dc
, state
);
1035 wxSize
wxDataViewDateRenderer::GetSize() const
1037 const wxDataViewCtrl
* view
= GetView();
1038 wxString tmp
= m_date
.FormatDate();
1040 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1041 return wxSize(x
,y
+d
);
1044 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1045 const wxDataViewItem
& item
, unsigned int col
)
1048 model
->GetValue( variant
, item
, col
);
1049 wxDateTime value
= variant
.GetDateTime();
1051 #if wxUSE_DATE_RENDERER_POPUP
1052 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1053 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1054 wxPoint pos
= wxGetMousePosition();
1057 popup
->Popup( popup
->m_cal
);
1058 #else // !wxUSE_DATE_RENDERER_POPUP
1059 wxMessageBox(value
.Format());
1060 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1064 // ---------------------------------------------------------
1065 // wxDataViewIconTextRenderer
1066 // ---------------------------------------------------------
1068 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1070 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1071 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1072 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1075 SetAlignment(align
);
1078 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1082 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1088 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1093 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1096 const wxIcon
&icon
= m_value
.GetIcon();
1099 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1100 xoffset
= icon
.GetWidth()+4;
1103 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1108 wxSize
wxDataViewIconTextRenderer::GetSize() const
1110 const wxDataViewCtrl
*view
= GetView();
1111 if (!m_value
.GetText().empty())
1114 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1116 if (m_value
.GetIcon().IsOk())
1117 x
+= m_value
.GetIcon().GetWidth() + 4;
1118 return wxSize( x
, y
);
1120 return wxSize(80,20);
1124 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1125 wxRect
WXUNUSED(labelRect
),
1126 const wxVariant
& WXUNUSED(value
))
1132 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1133 wxVariant
& WXUNUSED(value
))
1138 //-----------------------------------------------------------------------------
1139 // wxDataViewRenameTimer
1140 //-----------------------------------------------------------------------------
1142 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1147 void wxDataViewRenameTimer::Notify()
1149 m_owner
->OnRenameTimer();
1152 //-----------------------------------------------------------------------------
1153 // wxDataViewMainWindow
1154 //-----------------------------------------------------------------------------
1156 //The tree building helper, declared firstly
1157 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1159 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1161 if (row1
> row2
) return 1;
1162 if (row1
== row2
) return 0;
1167 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1169 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1170 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1171 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1172 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1173 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1174 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1177 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1178 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1179 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1180 m_selection( wxDataViewSelectionCmp
)
1185 m_lastOnSame
= false;
1186 m_renameTimer
= new wxDataViewRenameTimer( this );
1188 // TODO: user better initial values/nothing selected
1189 m_currentCol
= NULL
;
1192 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1195 m_dragStart
= wxPoint(0,0);
1196 m_lineLastClicked
= (unsigned int) -1;
1197 m_lineBeforeLastClicked
= (unsigned int) -1;
1198 m_lineSelectSingleOnUp
= (unsigned int) -1;
1200 m_dragEnabled
= false;
1201 m_dropEnabled
= false;
1205 SetBackgroundColour( *wxWHITE
);
1207 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1209 m_penRule
= wxPen(GetRuleColour());
1211 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1212 m_penExpander
= wxPen(wxColour(0,0,0));
1213 //Some new added code to deal with the tree structure
1214 m_root
= new wxDataViewTreeNode( NULL
);
1215 m_root
->SetHasChildren(true);
1217 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1219 m_underMouse
= NULL
;
1223 wxDataViewMainWindow::~wxDataViewMainWindow()
1226 delete m_renameTimer
;
1229 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1231 wxDataViewModel
*model
= GetOwner()->GetModel();
1232 wxAutoBufferedPaintDC
dc( this );
1235 dc
.SetPen( *wxTRANSPARENT_PEN
);
1236 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1237 dc
.SetBrush( *wxWHITE_BRUSH
);
1238 wxSize
size( GetClientSize() );
1239 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1243 GetOwner()->PrepareDC( dc
);
1244 dc
.SetFont( GetFont() );
1246 wxRect update
= GetUpdateRegion().GetBox();
1247 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1249 // compute which items needs to be redrawn
1250 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1251 unsigned int item_count
=
1252 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1253 (int)(GetRowCount( ) - item_start
));
1254 unsigned int item_last
= item_start
+ item_count
;
1256 // compute which columns needs to be redrawn
1257 unsigned int cols
= GetOwner()->GetColumnCount();
1258 unsigned int col_start
= 0;
1259 unsigned int x_start
;
1260 for (x_start
= 0; col_start
< cols
; col_start
++)
1262 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1263 if (col
->IsHidden())
1264 continue; // skip it!
1266 unsigned int w
= col
->GetWidth();
1267 if (x_start
+w
>= (unsigned int)update
.x
)
1273 unsigned int col_last
= col_start
;
1274 unsigned int x_last
= x_start
;
1275 for (; col_last
< cols
; col_last
++)
1277 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1278 if (col
->IsHidden())
1279 continue; // skip it!
1281 if (x_last
> (unsigned int)update
.GetRight())
1284 x_last
+= col
->GetWidth();
1287 // Draw horizontal rules if required
1288 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1290 dc
.SetPen(m_penRule
);
1291 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1293 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1295 int y
= GetLineStart( i
);
1296 dc
.DrawLine(x_start
, y
, x_last
, y
);
1300 // Draw vertical rules if required
1301 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1303 dc
.SetPen(m_penRule
);
1304 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1307 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1309 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1310 if (col
->IsHidden())
1311 continue; // skip it
1313 dc
.DrawLine(x
, GetLineStart( item_start
),
1314 x
, GetLineStart( item_last
) );
1316 x
+= col
->GetWidth();
1319 // Draw last vertical rule
1320 dc
.DrawLine(x
, GetLineStart( item_start
),
1321 x
, GetLineStart( item_last
) );
1324 // redraw the background for the items which are selected/current
1325 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1327 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1328 if (selected
|| item
== m_currentRow
)
1330 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1331 if (item
== m_currentRow
)
1332 flags
|= wxCONTROL_CURRENT
;
1334 flags
|= wxCONTROL_FOCUSED
;
1336 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1337 wxRendererNative::Get().DrawItemSelectionRect
1347 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1350 // TODO-RTL: last column for RTL support
1351 expander
= GetOwner()->GetColumnAt( 0 );
1352 GetOwner()->SetExpanderColumn(expander
);
1355 // redraw all cells for all rows which must be repainted and all columns
1357 cell_rect
.x
= x_start
;
1358 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1361 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1362 wxDataViewRenderer
*cell
= col
->GetRenderer();
1363 cell_rect
.width
= col
->GetWidth();
1365 if (col
->IsHidden())
1366 continue; // skip it!
1368 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1370 // get the cell value and set it into the renderer
1372 wxDataViewTreeNode
*node
= NULL
;
1373 wxDataViewItem dataitem
;
1375 if (!IsVirtualList())
1377 node
= GetTreeNodeByRow(item
);
1381 dataitem
= node
->GetItem();
1383 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
1388 dataitem
= wxDataViewItem( wxUIntToPtr(item
) );
1391 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1392 cell
->SetValue( value
);
1394 if (cell
->GetWantsAttr())
1396 wxDataViewItemAttr attr
;
1397 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
1399 cell
->SetAttr( attr
);
1400 cell
->SetHasAttr( ret
);
1404 cell_rect
.y
= GetLineStart( item
);
1405 cell_rect
.height
= GetLineHeight( item
); // -1 is for the horizontal rules (?)
1407 //Draw the expander here.
1409 if ((!IsVirtualList()) && (col
== expander
))
1411 indent
= node
->GetIndentLevel();
1413 //Calculate the indent first
1414 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1416 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1417 // change the cell_rect.x to the appropriate pos
1418 int expander_x
= indent
+ EXPANDER_MARGIN
;
1419 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2) - (expander_width
/2) - EXPANDER_OFFSET
;
1420 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1421 dc
.SetPen( m_penExpander
);
1422 dc
.SetBrush( wxNullBrush
);
1423 if( node
->HasChildren() )
1425 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1427 if (m_underMouse
== node
)
1429 flag
|= wxCONTROL_CURRENT
;
1431 if( node
->IsOpen() )
1432 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
1434 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1436 //force the expander column to left-center align
1437 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1439 if (node
&& !node
->HasChildren())
1441 // Yes, if the node does not have any child, it must be a leaf which
1442 // mean that it is a temporarily created by GetTreeNodeByRow
1446 // cannot be bigger than allocated space
1447 wxSize size
= cell
->GetSize();
1448 // Because of the tree structure indent, here we should minus the width of the cell for drawing
1449 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
1450 // size.y = wxMin( size.y, cell_rect.height );
1451 size
.y
= cell_rect
.height
;
1453 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
1454 int align
= cell
->CalculateAlignment();
1456 // horizontal alignment:
1457 item_rect
.x
= cell_rect
.x
;
1458 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1459 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1460 else if (align
& wxALIGN_RIGHT
)
1461 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
1462 //else: wxALIGN_LEFT is the default
1464 // vertical alignment:
1465 item_rect
.y
= cell_rect
.y
;
1466 if (align
& wxALIGN_CENTER_VERTICAL
)
1467 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1468 else if (align
& wxALIGN_BOTTOM
)
1469 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
1470 //else: wxALIGN_TOP is the default
1473 item_rect
.x
+= PADDING_RIGHTLEFT
;
1474 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1476 //Here we add the tree indent
1477 item_rect
.x
+= indent
;
1480 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1481 state
|= wxDATAVIEW_CELL_SELECTED
;
1483 // TODO: it would be much more efficient to create a clipping
1484 // region for the entire column being rendered (in the OnPaint
1485 // of wxDataViewMainWindow) instead of a single clip region for
1486 // each cell. However it would mean that each renderer should
1487 // respect the given wxRect's top & bottom coords, eventually
1488 // violating only the left & right coords - however the user can
1489 // make its own renderer and thus we cannot be sure of that.
1490 dc
.SetClippingRegion( item_rect
);
1491 cell
->Render( item_rect
, &dc
, state
);
1492 dc
.DestroyClippingRegion();
1495 cell_rect
.x
+= cell_rect
.width
;
1499 void wxDataViewMainWindow::OnRenameTimer()
1501 // We have to call this here because changes may just have
1502 // been made and no screen update taken place.
1507 unsigned int cols
= GetOwner()->GetColumnCount();
1509 for (i
= 0; i
< cols
; i
++)
1511 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
1513 continue; // skip it!
1515 if (c
== m_currentCol
)
1517 xpos
+= c
->GetWidth();
1519 wxRect
labelRect( xpos
,
1520 GetLineStart( m_currentRow
),
1521 m_currentCol
->GetWidth(),
1522 GetLineHeight( m_currentRow
) );
1524 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1525 &labelRect
.x
, &labelRect
.y
);
1527 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1528 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1532 //------------------------------------------------------------------
1533 // Helper class for do operation on the tree node
1534 //------------------------------------------------------------------
1539 virtual ~DoJob() { }
1541 //The return value control how the tree-walker tranverse the tree
1542 // 0: Job done, stop tranverse and return
1543 // 1: Ignore the current node's subtree and continue
1544 // 2: Job not done, continue
1545 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1546 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1547 virtual int operator() ( void * n
) = 0;
1550 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1555 switch( func( node
) )
1566 wxDataViewTreeNodes nodes
= node
->GetNodes();
1567 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1569 int len_nodes
= nodes
.GetCount();
1570 int len
= leaves
.GetCount();
1571 int i
= 0, nodes_i
= 0;
1573 for(; i
< len
; i
++ )
1575 void * n
= leaves
[i
];
1576 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1578 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1581 if( Walker( nd
, func
) )
1600 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1611 wxDataViewTreeNode
* node
;
1612 node
= FindNode(parent
);
1617 node
->SetHasChildren( true );
1619 if( g_model
->IsContainer( item
) )
1621 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1622 newnode
->SetItem(item
);
1623 newnode
->SetHasChildren( true );
1624 node
->AddNode( newnode
);
1627 node
->AddLeaf( item
.GetID() );
1629 node
->ChangeSubTreeCount(1);
1637 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1639 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1640 const wxDataViewItem
& item
)
1645 if( m_currentRow
> GetRowCount() )
1646 m_currentRow
= m_count
- 1;
1648 m_selection
.Empty();
1655 wxDataViewTreeNode
* node
= FindNode(parent
);
1657 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
1658 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
1661 node
->GetChildren().Remove( item
.GetID() );
1662 //Manuplate selection
1663 if( m_selection
.GetCount() > 1 )
1665 m_selection
.Empty();
1667 bool isContainer
= false;
1668 wxDataViewTreeNodes nds
= node
->GetNodes();
1669 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
1671 if (nds
[i
]->GetItem() == item
)
1679 wxDataViewTreeNode
* n
= NULL
;
1680 wxDataViewTreeNodes nodes
= node
->GetNodes();
1681 int len
= nodes
.GetCount();
1682 for( int i
= 0; i
< len
; i
++)
1684 if( nodes
[i
]->GetItem() == item
)
1691 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
1693 node
->GetNodes().Remove( n
);
1694 sub
-= n
->GetSubTreeCount();
1695 ::DestroyTreeHelper(n
);
1697 //Make the row number invalid and get a new valid one when user call GetRowCount
1699 node
->ChangeSubTreeCount(sub
);
1701 //Change the current row to the last row if the current exceed the max row number
1702 if( m_currentRow
> GetRowCount() )
1703 m_currentRow
= m_count
- 1;
1710 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1716 wxWindow
*parent
= GetParent();
1717 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
1718 le
.SetEventObject(parent
);
1719 le
.SetModel(GetOwner()->GetModel());
1721 parent
->GetEventHandler()->ProcessEvent(le
);
1726 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
1728 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1729 /*#define MAX_VIRTUAL_WIDTH 100000
1731 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1732 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1733 Refresh( true, &rect );
1741 wxWindow
*parent
= GetParent();
1742 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
1743 le
.SetEventObject(parent
);
1744 le
.SetModel(GetOwner()->GetModel());
1747 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
1748 parent
->GetEventHandler()->ProcessEvent(le
);
1753 bool wxDataViewMainWindow::Cleared()
1758 BuildTree( GetOwner()->GetModel() );
1765 void wxDataViewMainWindow::UpdateDisplay()
1770 void wxDataViewMainWindow::OnInternalIdle()
1772 wxWindow::OnInternalIdle();
1776 RecalculateDisplay();
1781 void wxDataViewMainWindow::RecalculateDisplay()
1783 wxDataViewModel
*model
= GetOwner()->GetModel();
1790 int width
= GetEndOfLastCol();
1791 int height
= GetLineStart( GetRowCount() );
1793 SetVirtualSize( width
, height
);
1794 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1799 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1801 wxWindow::ScrollWindow( dx
, dy
, rect
);
1803 if (GetOwner()->m_headerArea
)
1804 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1807 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
1810 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
1811 int sy
= GetLineStart( rows
)/y
;
1815 wxRect rect
= GetClientRect();
1819 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
1820 for (x_start
= 0; colnum
< column
; colnum
++)
1822 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
1823 if (col
->IsHidden())
1824 continue; // skip it!
1826 w
= col
->GetWidth();
1830 int x_end
= x_start
+ w
;
1831 xe
= xx
+ rect
.width
;
1834 sx
= ( xx
+ x_end
- xe
)/x
;
1841 m_owner
->Scroll( sx
, sy
);
1844 int wxDataViewMainWindow::GetCountPerPage() const
1846 wxSize size
= GetClientSize();
1847 return size
.y
/ m_lineHeight
;
1850 int wxDataViewMainWindow::GetEndOfLastCol() const
1854 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
1856 const wxDataViewColumn
*c
=
1857 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
1860 width
+= c
->GetWidth();
1865 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
1869 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
1871 return GetLineAt( y
);
1874 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
1876 wxSize client_size
= GetClientSize();
1877 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
1878 &client_size
.x
, &client_size
.y
);
1880 //we should deal with the pixel here
1881 unsigned int row
= GetLineAt(client_size
.y
) - 1;
1883 return wxMin( GetRowCount()-1, row
);
1886 unsigned int wxDataViewMainWindow::GetRowCount()
1888 if ( m_count
== -1 )
1890 m_count
= RecalculateCount();
1896 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
1903 void wxDataViewMainWindow::SelectAllRows( bool on
)
1910 m_selection
.Clear();
1911 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
1912 m_selection
.Add( i
);
1917 unsigned int first_visible
= GetFirstVisibleRow();
1918 unsigned int last_visible
= GetLastVisibleRow();
1920 for (i
= 0; i
< m_selection
.GetCount(); i
++)
1922 unsigned int row
= m_selection
[i
];
1923 if ((row
>= first_visible
) && (row
<= last_visible
))
1926 m_selection
.Clear();
1930 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
1932 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1936 m_selection
.Add( row
);
1944 m_selection
.Remove( row
);
1950 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
1954 unsigned int tmp
= from
;
1960 for (i
= from
; i
<= to
; i
++)
1962 if (m_selection
.Index( i
) == wxNOT_FOUND
)
1965 m_selection
.Add( i
);
1970 m_selection
.Remove( i
);
1973 RefreshRows( from
, to
);
1976 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
1978 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
1980 int n
= aSelections
[i
];
1982 m_selection
.Add( n
);
1987 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
1989 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1990 m_selection
.Add( row
);
1992 m_selection
.Remove( row
);
1996 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
1998 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2001 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2003 wxWindow
*parent
= GetParent();
2004 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2006 le
.SetEventObject(parent
);
2007 le
.SetModel(GetOwner()->GetModel());
2010 parent
->GetEventHandler()->ProcessEvent(le
);
2013 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2015 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2016 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2018 wxSize client_size
= GetClientSize();
2019 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2020 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2021 if (intersect_rect
.width
> 0)
2022 Refresh( true, &intersect_rect
);
2025 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2029 unsigned int tmp
= to
;
2034 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2035 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2037 wxSize client_size
= GetClientSize();
2038 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2039 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2040 if (intersect_rect
.width
> 0)
2041 Refresh( true, &intersect_rect
);
2044 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2046 wxSize client_size
= GetClientSize();
2047 int start
= GetLineStart( firstRow
);
2048 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2049 if (start
> client_size
.y
) return;
2051 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2053 Refresh( true, &rect
);
2056 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2058 wxCHECK_RET( newCurrent
< GetRowCount(),
2059 _T("invalid item index in OnArrowChar()") );
2061 // if there is no selection, we cannot move it anywhere
2062 if (!HasCurrentRow())
2065 unsigned int oldCurrent
= m_currentRow
;
2067 // in single selection we just ignore Shift as we can't select several
2069 if ( event
.ShiftDown() && !IsSingleSel() )
2071 RefreshRow( oldCurrent
);
2073 ChangeCurrentRow( newCurrent
);
2075 // select all the items between the old and the new one
2076 if ( oldCurrent
> newCurrent
)
2078 newCurrent
= oldCurrent
;
2079 oldCurrent
= m_currentRow
;
2082 SelectRows( oldCurrent
, newCurrent
, true );
2083 if (oldCurrent
!=newCurrent
)
2084 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2088 RefreshRow( oldCurrent
);
2090 // all previously selected items are unselected unless ctrl is held
2091 if ( !event
.ControlDown() )
2092 SelectAllRows(false);
2094 ChangeCurrentRow( newCurrent
);
2096 if ( !event
.ControlDown() )
2098 SelectRow( m_currentRow
, true );
2099 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2102 RefreshRow( m_currentRow
);
2105 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2108 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2112 rect
.y
= GetLineStart( row
);
2113 rect
.width
= GetEndOfLastCol();
2114 rect
.height
= GetLineHeight( row
);
2119 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2121 const wxDataViewModel
*model
= GetOwner()->GetModel();
2123 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2125 // TODO make more efficient
2130 for (r
= 0; r
< row
; r
++)
2132 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2133 if (!node
) return start
;
2135 wxDataViewItem item
= node
->GetItem();
2137 if (node
&& !node
->HasChildren())
2139 // Yes, if the node does not have any child, it must be a leaf which
2140 // mean that it is a temporarily created by GetTreeNodeByRow
2144 unsigned int cols
= GetOwner()->GetColumnCount();
2146 int height
= m_lineHeight
;
2147 for (col
= 0; col
< cols
; col
++)
2149 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2150 if (column
->IsHidden())
2151 continue; // skip it!
2153 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2154 continue; // skip it!
2156 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2158 model
->GetValue( value
, item
, column
->GetModelColumn() );
2159 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2160 renderer2
->SetValue( value
);
2161 height
= wxMax( height
, renderer
->GetSize().y
);
2172 return row
* m_lineHeight
;
2176 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2178 const wxDataViewModel
*model
= GetOwner()->GetModel();
2180 // check for the easy case first
2181 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2182 return y
/ m_lineHeight
;
2184 // TODO make more efficient
2185 unsigned int row
= 0;
2186 unsigned int yy
= 0;
2189 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2192 // not really correct...
2193 return row
+ ((y
-yy
) / m_lineHeight
);
2196 wxDataViewItem item
= node
->GetItem();
2198 if (node
&& !node
->HasChildren())
2200 // Yes, if the node does not have any child, it must be a leaf which
2201 // mean that it is a temporarily created by GetTreeNodeByRow
2205 unsigned int cols
= GetOwner()->GetColumnCount();
2207 int height
= m_lineHeight
;
2208 for (col
= 0; col
< cols
; col
++)
2210 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2211 if (column
->IsHidden())
2212 continue; // skip it!
2214 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2215 continue; // skip it!
2217 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2219 model
->GetValue( value
, item
, column
->GetModelColumn() );
2220 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2221 renderer2
->SetValue( value
);
2222 height
= wxMax( height
, renderer
->GetSize().y
);
2233 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2235 const wxDataViewModel
*model
= GetOwner()->GetModel();
2237 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2239 wxASSERT( !IsVirtualList() );
2241 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2242 // wxASSERT( node );
2243 if (!node
) return m_lineHeight
;
2245 wxDataViewItem item
= node
->GetItem();
2247 if (node
&& !node
->HasChildren())
2249 // Yes, if the node does not have any child, it must be a leaf which
2250 // mean that it is a temporarily created by GetTreeNodeByRow
2254 int height
= m_lineHeight
;
2256 unsigned int cols
= GetOwner()->GetColumnCount();
2258 for (col
= 0; col
< cols
; col
++)
2260 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2261 if (column
->IsHidden())
2262 continue; // skip it!
2264 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2265 continue; // skip it!
2267 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2269 model
->GetValue( value
, item
, column
->GetModelColumn() );
2270 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2271 renderer2
->SetValue( value
);
2272 height
= wxMax( height
, renderer
->GetSize().y
);
2279 return m_lineHeight
;
2283 class RowToItemJob
: public DoJob
2286 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2287 virtual ~RowToItemJob() { }
2289 virtual int operator() ( wxDataViewTreeNode
* node
)
2292 if( current
== static_cast<int>(row
))
2294 ret
= node
->GetItem();
2298 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2300 current
+= node
->GetSubTreeCount();
2305 //If the current has no child node, we can find the desired item of the row number directly.
2306 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2307 if( node
->GetNodes().GetCount() == 0)
2309 int index
= static_cast<int>(row
) - current
- 1;
2310 ret
= node
->GetChildren().Item( index
);
2317 virtual int operator() ( void * n
)
2320 if( current
== static_cast<int>(row
))
2322 ret
= wxDataViewItem( n
);
2327 wxDataViewItem
GetResult(){ return ret
; }
2334 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2338 return wxDataViewItem( wxUIntToPtr(row
) );
2342 RowToItemJob
job( row
, -2 );
2343 Walker( m_root
, job
);
2344 return job
.GetResult();
2348 class RowToTreeNodeJob
: public DoJob
2351 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2354 this->current
= current
;
2358 virtual ~RowToTreeNodeJob(){ }
2360 virtual int operator() ( wxDataViewTreeNode
* node
)
2363 if( current
== static_cast<int>(row
))
2369 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2371 current
+= node
->GetSubTreeCount();
2377 //If the current has no child node, we can find the desired item of the row number directly.
2378 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2379 if( node
->GetNodes().GetCount() == 0)
2381 int index
= static_cast<int>(row
) - current
- 1;
2382 void * n
= node
->GetChildren().Item( index
);
2383 ret
= new wxDataViewTreeNode( parent
);
2384 ret
->SetItem( wxDataViewItem( n
));
2385 ret
->SetHasChildren(false);
2394 virtual int operator() ( void * n
)
2397 if( current
== static_cast<int>(row
))
2399 ret
= new wxDataViewTreeNode( parent
);
2400 ret
->SetItem( wxDataViewItem( n
));
2401 ret
->SetHasChildren(false);
2407 wxDataViewTreeNode
* GetResult(){ return ret
; }
2411 wxDataViewTreeNode
* ret
;
2412 wxDataViewTreeNode
* parent
;
2416 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2418 wxASSERT( !IsVirtualList() );
2420 RowToTreeNodeJob
job( row
, -2, m_root
);
2421 Walker( m_root
, job
);
2422 return job
.GetResult();
2425 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2427 wxWindow
*parent
= GetParent();
2428 wxDataViewEvent
le(type
, parent
->GetId());
2430 le
.SetEventObject(parent
);
2431 le
.SetModel(GetOwner()->GetModel());
2434 parent
->GetEventHandler()->ProcessEvent(le
);
2439 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2441 if (IsVirtualList())
2444 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2448 if (!node
->HasChildren())
2454 return node
->IsOpen();
2458 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2460 if (IsVirtualList())
2463 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2466 if( node
->HasChildren())
2468 if( !node
->IsOpen())
2470 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2471 //Check if the user prevent expanding
2472 if( e
.GetSkipped() )
2476 //Here I build the children of current node
2477 if( node
->GetChildrenNumber() == 0 )
2480 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2484 //Send the expanded event
2485 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2489 SelectRow( row
, false );
2490 SelectRow( row
+ 1, true );
2491 ChangeCurrentRow( row
+ 1 );
2492 SendSelectionChangedEvent( GetItemByRow(row
+1));
2500 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2502 if (IsVirtualList())
2505 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2508 wxDataViewTreeNode
* nd
= node
;
2510 if( node
->HasChildren() && node
->IsOpen() )
2512 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2513 if( e
.GetSkipped() )
2518 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2522 node
= node
->GetParent();
2525 int parent
= GetRowByItem( node
->GetItem() );
2528 SelectRow( row
, false);
2529 SelectRow(parent
, true );
2530 ChangeCurrentRow( parent
);
2531 SendSelectionChangedEvent( node
->GetItem() );
2535 if( !nd
->HasChildren())
2540 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2542 wxDataViewModel
* model
= GetOwner()->GetModel();
2546 //Compose the a parent-chain of the finding item
2548 list
.DeleteContents( true );
2549 wxDataViewItem
it( item
);
2552 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2553 list
.Insert( pItem
);
2554 it
= model
->GetParent( it
);
2557 //Find the item along the parent-chain.
2558 //This algorithm is designed to speed up the node-finding method
2559 wxDataViewTreeNode
* node
= m_root
;
2560 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
2562 if( node
->HasChildren() )
2564 if( node
->GetChildrenNumber() == 0 )
2567 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2570 wxDataViewTreeNodes nodes
= node
->GetNodes();
2574 for (i
= 0; i
< nodes
.GetCount(); i
++)
2576 if (nodes
[i
]->GetItem() == (**iter
))
2578 if (nodes
[i
]->GetItem() == item
)
2595 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
2597 wxDataViewColumn
*col
= NULL
;
2598 unsigned int cols
= GetOwner()->GetColumnCount();
2599 unsigned int colnum
= 0;
2601 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
2602 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
2604 col
= GetOwner()->GetColumnAt(colnum
);
2605 if (col
->IsHidden())
2606 continue; // skip it!
2608 unsigned int w
= col
->GetWidth();
2609 if (x_start
+w
>= (unsigned int)x
)
2616 item
= GetItemByRow( GetLineAt( y
) );
2619 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
2621 int row
= GetRowByItem(item
);
2622 int y
= GetLineStart( row
);
2623 int h
= GetLineHeight( m_lineHeight
);
2625 wxDataViewColumn
*col
= NULL
;
2626 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
2628 col
= GetOwner()->GetColumnAt( i
);
2629 x
+= col
->GetWidth();
2630 if( GetOwner()->GetColumnAt(i
+1) == column
)
2633 int w
= col
->GetWidth();
2634 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
2635 return wxRect(x
, y
, w
, h
);
2638 int wxDataViewMainWindow::RecalculateCount()
2642 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
2644 return list_model
->GetLastIndex() + 1;
2646 return list_model
->GetLastIndex() - 1;
2651 return m_root
->GetSubTreeCount();
2655 class ItemToRowJob
: public DoJob
2658 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
2665 //Maybe binary search will help to speed up this process
2666 virtual int operator() ( wxDataViewTreeNode
* node
)
2669 if( node
->GetItem() == item
)
2674 if( node
->GetItem() == **m_iter
)
2681 ret
+= node
->GetSubTreeCount();
2687 virtual int operator() ( void * n
)
2690 if( n
== item
.GetID() )
2694 //the row number is begin from zero
2695 int GetResult() { return ret
-1; }
2698 ItemList::const_iterator m_iter
;
2699 wxDataViewItem item
;
2704 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
2706 const wxDataViewModel
* model
= GetOwner()->GetModel();
2712 return wxPtrToUInt( item
.GetID() );
2719 //Compose the a parent-chain of the finding item
2721 wxDataViewItem
* pItem
;
2722 list
.DeleteContents( true );
2723 wxDataViewItem
it( item
);
2726 pItem
= new wxDataViewItem( it
);
2727 list
.Insert( pItem
);
2728 it
= model
->GetParent( it
);
2730 pItem
= new wxDataViewItem( );
2731 list
.Insert( pItem
);
2733 ItemToRowJob
job( item
, list
.begin() );
2734 Walker(m_root
, job
);
2735 return job
.GetResult();
2739 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2741 if( !model
->IsContainer( item
) )
2744 wxDataViewItemArray children
;
2745 unsigned int num
= model
->GetChildren( item
, children
);
2746 unsigned int index
= 0;
2747 while( index
< num
)
2749 if( model
->IsContainer( children
[index
] ) )
2751 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2752 n
->SetItem(children
[index
]);
2753 n
->SetHasChildren( true );
2758 node
->AddLeaf( children
[index
].GetID() );
2762 node
->SetSubTreeCount( num
);
2763 wxDataViewTreeNode
* n
= node
->GetParent();
2765 n
->ChangeSubTreeCount(num
);
2769 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2773 if (GetOwner()->GetModel()->IsVirtualListModel())
2779 m_root
= new wxDataViewTreeNode( NULL
);
2780 m_root
->SetHasChildren(true);
2782 //First we define a invalid item to fetch the top-level elements
2783 wxDataViewItem item
;
2785 BuildTreeHelper( model
, item
, m_root
);
2789 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2791 if( node
->GetNodeNumber() != 0 )
2793 int len
= node
->GetNodeNumber();
2795 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
2796 for(; i
< len
; i
++ )
2798 DestroyTreeHelper(nodes
[i
]);
2804 void wxDataViewMainWindow::DestroyTree()
2806 if (!IsVirtualList())
2808 ::DestroyTreeHelper(m_root
);
2814 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2816 if ( GetParent()->HandleAsNavigationKey(event
) )
2819 // no item -> nothing to do
2820 if (!HasCurrentRow())
2826 // don't use m_linesPerPage directly as it might not be computed yet
2827 const int pageSize
= GetCountPerPage();
2828 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2830 switch ( event
.GetKeyCode() )
2834 if (m_currentRow
>= 0)
2836 wxWindow
*parent
= GetParent();
2837 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
2838 le
.SetItem( GetItemByRow(m_currentRow
) );
2839 le
.SetEventObject(parent
);
2840 le
.SetModel(GetOwner()->GetModel());
2842 parent
->GetEventHandler()->ProcessEvent(le
);
2847 if ( m_currentRow
> 0 )
2848 OnArrowChar( m_currentRow
- 1, event
);
2852 if ( m_currentRow
< GetRowCount() - 1 )
2853 OnArrowChar( m_currentRow
+ 1, event
);
2855 //Add the process for tree expanding/collapsing
2857 OnCollapsing(m_currentRow
);
2860 OnExpanding( m_currentRow
);
2864 OnArrowChar( GetRowCount() - 1, event
);
2869 OnArrowChar( 0, event
);
2874 int steps
= pageSize
- 1;
2875 int index
= m_currentRow
- steps
;
2879 OnArrowChar( index
, event
);
2885 int steps
= pageSize
- 1;
2886 unsigned int index
= m_currentRow
+ steps
;
2887 unsigned int count
= GetRowCount();
2888 if ( index
>= count
)
2891 OnArrowChar( index
, event
);
2900 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
2902 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2904 // let the base handle mouse wheel events.
2909 int x
= event
.GetX();
2910 int y
= event
.GetY();
2911 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2912 wxDataViewColumn
*col
= NULL
;
2915 unsigned int cols
= GetOwner()->GetColumnCount();
2917 for (i
= 0; i
< cols
; i
++)
2919 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
2921 continue; // skip it!
2923 if (x
< xpos
+ c
->GetWidth())
2928 xpos
+= c
->GetWidth();
2933 wxDataViewRenderer
*cell
= col
->GetRenderer();
2934 unsigned int current
= GetLineAt( y
);
2935 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
2937 // Unselect all if below the last row ?
2941 //Test whether the mouse is hovered on the tree item button
2943 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
2945 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
2946 if( node
!=NULL
&& node
->HasChildren() )
2948 int indent
= node
->GetIndentLevel();
2949 indent
= GetOwner()->GetIndent()*indent
;
2950 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
2951 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
2952 m_lineHeight
-2*EXPANDER_MARGIN
,
2953 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
2954 if( rect
.Contains( x
, y
) )
2956 //So the mouse is over the expander
2958 if (m_underMouse
&& m_underMouse
!= node
)
2960 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
2961 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
2963 if (m_underMouse
!= node
)
2965 //wxLogMessage("Do the row: %d", current);
2966 RefreshRow(current
);
2968 m_underMouse
= node
;
2971 if (node
!=NULL
&& !node
->HasChildren())
2976 if (m_underMouse
!= NULL
)
2978 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
2979 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
2980 m_underMouse
= NULL
;
2984 wxDataViewModel
*model
= GetOwner()->GetModel();
2986 if (event
.Dragging())
2988 if (m_dragCount
== 0)
2990 // we have to report the raw, physical coords as we want to be
2991 // able to call HitTest(event.m_pointDrag) from the user code to
2992 // get the item being dragged
2993 m_dragStart
= event
.GetPosition();
2998 if (m_dragCount
!= 3)
3001 if (event
.LeftIsDown())
3003 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
, &m_dragStart
.x
, &m_dragStart
.y
);
3004 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3005 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3007 // Notify cell about drag
3008 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3009 event
.SetEventObject( m_owner
);
3010 event
.SetItem( item
);
3011 event
.SetModel( model
);
3012 if (!m_owner
->HandleWindowEvent( event
))
3015 if (!event
.IsAllowed())
3018 wxDataObject
*obj
= event
.GetDataObject();
3022 wxPrintf( "success\n" );
3023 // m_dragDataObject = obj;
3032 bool forceClick
= false;
3034 if (event
.ButtonDClick())
3036 m_renameTimer
->Stop();
3037 m_lastOnSame
= false;
3040 wxDataViewItem item
= GetItemByRow(current
);
3041 bool ignore_other_columns
=
3042 ((GetOwner()->GetExpanderColumn() != col
) &&
3043 (model
->IsContainer(item
)) &&
3044 (!model
->HasContainerColumns(item
)));
3046 if (event
.LeftDClick())
3048 if ( current
== m_lineLastClicked
)
3050 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3053 model
->GetValue( value
, item
, col
->GetModelColumn() );
3054 cell
->SetValue( value
);
3055 wxRect
cell_rect( xpos
, GetLineStart( current
),
3056 col
->GetWidth(), GetLineHeight( current
) );
3057 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3062 wxWindow
*parent
= GetParent();
3063 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3065 le
.SetEventObject(parent
);
3066 le
.SetModel(GetOwner()->GetModel());
3068 parent
->GetEventHandler()->ProcessEvent(le
);
3074 // The first click was on another item, so don't interpret this as
3075 // a double click, but as a simple click instead
3082 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3084 // select single line
3085 SelectAllRows( false );
3086 SelectRow( m_lineSelectSingleOnUp
, true );
3087 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3090 //Process the event of user clicking the expander
3091 bool expander
= false;
3092 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3094 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3095 if( node
!=NULL
&& node
->HasChildren() )
3097 int indent
= node
->GetIndentLevel();
3098 indent
= GetOwner()->GetIndent()*indent
;
3099 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
3100 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
3101 m_lineHeight
-2*EXPANDER_MARGIN
,
3102 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
3104 if( rect
.Contains( x
, y
) )
3107 if( node
->IsOpen() )
3108 OnCollapsing(current
);
3110 OnExpanding( current
);
3113 if (node
&& !node
->HasChildren())
3116 //If the user click the expander, we do not do editing even if the column with expander are editable
3117 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3119 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3120 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3122 m_renameTimer
->Start( 100, true );
3126 m_lastOnSame
= false;
3127 m_lineSelectSingleOnUp
= (unsigned int)-1;
3131 // This is necessary, because after a DnD operation in
3132 // from and to ourself, the up event is swallowed by the
3133 // DnD code. So on next non-up event (which means here and
3134 // now) m_lineSelectSingleOnUp should be reset.
3135 m_lineSelectSingleOnUp
= (unsigned int)-1;
3138 if (event
.RightDown())
3140 m_lineBeforeLastClicked
= m_lineLastClicked
;
3141 m_lineLastClicked
= current
;
3143 // If the item is already selected, do not update the selection.
3144 // Multi-selections should not be cleared if a selected item is clicked.
3145 if (!IsRowSelected(current
))
3147 SelectAllRows(false);
3148 ChangeCurrentRow(current
);
3149 SelectRow(m_currentRow
,true);
3150 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3154 model
->GetValue( value
, item
, col
->GetModelColumn() );
3155 wxWindow
*parent
= GetParent();
3156 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3158 le
.SetEventObject(parent
);
3159 le
.SetModel(GetOwner()->GetModel());
3161 parent
->GetEventHandler()->ProcessEvent(le
);
3163 else if (event
.MiddleDown())
3166 if (event
.LeftDown() || forceClick
)
3170 m_lineBeforeLastClicked
= m_lineLastClicked
;
3171 m_lineLastClicked
= current
;
3173 unsigned int oldCurrentRow
= m_currentRow
;
3174 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3176 bool cmdModifierDown
= event
.CmdDown();
3177 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3179 if ( IsSingleSel() || !IsRowSelected(current
) )
3181 SelectAllRows( false );
3182 ChangeCurrentRow(current
);
3183 SelectRow(m_currentRow
,true);
3184 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3186 else // multi sel & current is highlighted & no mod keys
3188 m_lineSelectSingleOnUp
= current
;
3189 ChangeCurrentRow(current
); // change focus
3192 else // multi sel & either ctrl or shift is down
3194 if (cmdModifierDown
)
3196 ChangeCurrentRow(current
);
3197 ReverseRowSelection(m_currentRow
);
3198 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3200 else if (event
.ShiftDown())
3202 ChangeCurrentRow(current
);
3204 unsigned int lineFrom
= oldCurrentRow
,
3207 if ( lineTo
< lineFrom
)
3210 lineFrom
= m_currentRow
;
3213 SelectRows(lineFrom
, lineTo
, true);
3214 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3216 else // !ctrl, !shift
3218 // test in the enclosing if should make it impossible
3219 wxFAIL_MSG( _T("how did we get here?") );
3223 if (m_currentRow
!= oldCurrentRow
)
3224 RefreshRow( oldCurrentRow
);
3226 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3228 // Update selection here...
3231 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3232 (current
== oldCurrentRow
)) && oldWasSelected
;
3234 // Call LeftClick after everything else as under GTK+
3235 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3237 // notify cell about right click
3239 model
->GetValue( value
, item
, col
->GetModelColumn() );
3240 cell
->SetValue( value
);
3241 wxRect
cell_rect( xpos
, GetLineStart( current
),
3242 col
->GetWidth(), GetLineHeight( current
) );
3243 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3248 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3252 if (HasCurrentRow())
3258 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3262 if (HasCurrentRow())
3268 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3270 if( m_selection
.GetCount() != 1 )
3271 return wxDataViewItem();
3273 return GetItemByRow( m_selection
.Item(0));
3276 //-----------------------------------------------------------------------------
3278 //-----------------------------------------------------------------------------
3279 WX_DEFINE_LIST(wxDataViewColumnList
)
3281 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3283 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3284 EVT_SIZE(wxDataViewCtrl::OnSize
)
3287 wxDataViewCtrl::~wxDataViewCtrl()
3290 GetModel()->RemoveNotifier( m_notifier
);
3295 void wxDataViewCtrl::Init()
3297 m_cols
.DeleteContents(true);
3300 // No sorting column at start
3301 m_sortingColumnIdx
= wxNOT_FOUND
;
3303 m_headerArea
= NULL
;
3306 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3307 const wxPoint
& pos
, const wxSize
& size
,
3308 long style
, const wxValidator
& validator
)
3310 if ( (style
& wxBORDER_MASK
) == 0)
3311 style
|= wxBORDER_SUNKEN
;
3315 if (!wxControl::Create( parent
, id
, pos
, size
,
3316 style
| wxScrolledWindowStyle
, validator
))
3319 SetInitialSize(size
);
3322 MacSetClipChildren( true );
3325 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3327 if (HasFlag(wxDV_NO_HEADER
))
3328 m_headerArea
= NULL
;
3330 m_headerArea
= new wxDataViewHeaderWindow(this);
3332 SetTargetWindow( m_clientArea
);
3334 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3336 sizer
->Add( m_headerArea
, 0, wxGROW
);
3337 sizer
->Add( m_clientArea
, 1, wxGROW
);
3344 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3348 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3351 // we need to process arrows ourselves for scrolling
3352 if ( nMsg
== WM_GETDLGCODE
)
3354 rc
|= DLGC_WANTARROWS
;
3362 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3364 wxSize newsize
= size
;
3365 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3366 newsize
.y
-= m_headerArea
->GetSize().y
;
3371 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3373 // We need to override OnSize so that our scrolled
3374 // window a) does call Layout() to use sizers for
3375 // positioning the controls but b) does not query
3376 // the sizer for their size and use that for setting
3377 // the scrollable area as set that ourselves by
3378 // calling SetScrollbar() further down.
3385 void wxDataViewCtrl::SetFocus()
3388 m_clientArea
->SetFocus();
3391 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3393 if (!wxDataViewCtrlBase::AssociateModel( model
))
3396 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3398 model
->AddNotifier( m_notifier
);
3400 m_clientArea
->DestroyTree();
3402 m_clientArea
->BuildTree(model
);
3404 m_clientArea
->UpdateDisplay();
3409 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
3411 return m_clientArea
->EnableDragSource( format
);
3414 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
3416 return m_clientArea
->EnableDropTarget( format
);
3419 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3421 if (!wxDataViewCtrlBase::AppendColumn(col
))
3424 m_cols
.Append( col
);
3425 OnColumnsCountChanged();
3429 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3431 if (!wxDataViewCtrlBase::PrependColumn(col
))
3434 m_cols
.Insert( col
);
3435 OnColumnsCountChanged();
3439 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3441 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
3444 m_cols
.Insert( pos
, col
);
3445 OnColumnsCountChanged();
3449 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
3452 m_headerArea
->UpdateColumn(idx
);
3454 m_clientArea
->UpdateDisplay();
3457 void wxDataViewCtrl::OnColumnsCountChanged()
3460 m_headerArea
->SetColumnCount(GetColumnCount());
3462 m_clientArea
->UpdateDisplay();
3465 void wxDataViewCtrl::DoSetExpanderColumn()
3467 m_clientArea
->UpdateDisplay();
3470 void wxDataViewCtrl::DoSetIndent()
3472 m_clientArea
->UpdateDisplay();
3475 unsigned int wxDataViewCtrl::GetColumnCount() const
3477 return m_cols
.GetCount();
3480 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
3485 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
3487 // columns can't be reordered if there is no header window which allows
3489 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
3492 return GetColumn(idx
);
3495 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
3497 const unsigned count
= m_cols
.size();
3498 for ( unsigned n
= 0; n
< count
; n
++ )
3500 if ( m_cols
[n
] == column
)
3507 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
3508 unsigned int WXUNUSED(new_pos
))
3510 // do _not_ reorder m_cols elements here, they should always be in the
3511 // order in which columns were added, we only display the columns in
3513 m_clientArea
->UpdateDisplay();
3516 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3518 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3523 OnColumnsCountChanged();
3528 bool wxDataViewCtrl::ClearColumns()
3531 OnColumnsCountChanged();
3535 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3539 unsigned int len
= GetColumnCount();
3540 for ( unsigned int i
= 0; i
< len
; i
++ )
3542 wxDataViewColumn
* col
= GetColumnAt(i
);
3543 if (col
->IsHidden())
3545 ret
+= col
->GetWidth();
3548 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
3555 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3557 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
3558 : GetColumn(m_sortingColumnIdx
);
3561 //Selection code with wxDataViewItem as parameters
3562 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3564 return m_clientArea
->GetSelection();
3567 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3570 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3571 int len
= selection
.GetCount();
3572 for( int i
= 0; i
< len
; i
++)
3574 unsigned int row
= selection
[i
];
3575 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3580 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3582 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
3584 wxDataViewItem last_parent
;
3586 int len
= sel
.GetCount();
3587 for( int i
= 0; i
< len
; i
++ )
3589 wxDataViewItem item
= sel
[i
];
3590 wxDataViewItem parent
= GetModel()->GetParent( item
);
3593 if (parent
!= last_parent
)
3594 ExpandAncestors(item
);
3597 last_parent
= parent
;
3598 int row
= m_clientArea
->GetRowByItem( item
);
3600 selection
.Add( static_cast<unsigned int>(row
) );
3603 m_clientArea
->SetSelections( selection
);
3606 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3608 ExpandAncestors( item
);
3610 int row
= m_clientArea
->GetRowByItem( item
);
3613 //Unselect all rows before select another in the single select mode
3614 if (m_clientArea
->IsSingleSel())
3615 m_clientArea
->SelectAllRows(false);
3616 m_clientArea
->SelectRow(row
, true);
3620 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3622 int row
= m_clientArea
->GetRowByItem( item
);
3624 m_clientArea
->SelectRow(row
, false);
3627 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3629 int row
= m_clientArea
->GetRowByItem( item
);
3632 return m_clientArea
->IsRowSelected(row
);
3637 //Selection code with row number as parameter
3638 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3641 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3642 int len
= selection
.GetCount();
3643 for( int i
= 0; i
< len
; i
++)
3645 unsigned int row
= selection
[i
];
3651 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3653 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
3654 int len
= sel
.GetCount();
3655 for( int i
= 0; i
< len
; i
++ )
3659 selection
.Add( static_cast<unsigned int>(row
) );
3661 m_clientArea
->SetSelections( selection
);
3664 void wxDataViewCtrl::Select( int row
)
3668 if (m_clientArea
->IsSingleSel())
3669 m_clientArea
->SelectAllRows(false);
3670 m_clientArea
->SelectRow( row
, true );
3674 void wxDataViewCtrl::Unselect( int row
)
3677 m_clientArea
->SelectRow(row
, false);
3680 bool wxDataViewCtrl::IsSelected( int row
) const
3683 return m_clientArea
->IsRowSelected(row
);
3687 void wxDataViewCtrl::SelectRange( int from
, int to
)
3690 for( int i
= from
; i
< to
; i
++ )
3692 m_clientArea
->Select(sel
);
3695 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3697 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3698 for( int i
= from
; i
< to
; i
++ )
3699 if( sel
.Index( i
) != wxNOT_FOUND
)
3701 m_clientArea
->SetSelections(sel
);
3704 void wxDataViewCtrl::SelectAll()
3706 m_clientArea
->SelectAllRows(true);
3709 void wxDataViewCtrl::UnselectAll()
3711 m_clientArea
->SelectAllRows(false);
3714 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3718 if( row
> (int) m_clientArea
->GetRowCount() )
3719 row
= m_clientArea
->GetRowCount();
3721 int first
= m_clientArea
->GetFirstVisibleRow();
3722 int last
= m_clientArea
->GetLastVisibleRow();
3724 m_clientArea
->ScrollTo( row
, column
);
3725 else if( row
> last
)
3726 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3728 m_clientArea
->ScrollTo( first
, column
);
3731 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3733 ExpandAncestors( item
);
3735 m_clientArea
->RecalculateDisplay();
3737 int row
= m_clientArea
->GetRowByItem(item
);
3740 if( column
== NULL
)
3741 EnsureVisible(row
, -1);
3743 EnsureVisible( row
, GetColumnIndex(column
) );
3748 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
3750 m_clientArea
->HitTest(point
, item
, column
);
3753 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
3755 return m_clientArea
->GetItemRect(item
, column
);
3758 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
3760 return m_clientArea
->GetItemByRow( row
);
3763 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
3765 return m_clientArea
->GetRowByItem( item
);
3768 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
3770 int row
= m_clientArea
->GetRowByItem( item
);
3772 m_clientArea
->Expand(row
);
3775 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
3777 int row
= m_clientArea
->GetRowByItem( item
);
3779 m_clientArea
->Collapse(row
);
3782 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
3784 int row
= m_clientArea
->GetRowByItem( item
);
3786 return m_clientArea
->IsExpanded(row
);
3792 // !wxUSE_GENERICDATAVIEWCTRL
3795 // wxUSE_DATAVIEWCTRL