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"
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
57 static const int SCROLL_UNIT_X
= 15;
59 // the cell padding on the left/right
60 static const int PADDING_RIGHTLEFT
= 3;
62 // the expander space margin
63 static const int EXPANDER_MARGIN
= 4;
66 static const int EXPANDER_OFFSET
= 4;
68 static const int EXPANDER_OFFSET
= 1;
71 //Below is the compare stuff
72 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
73 static wxDataViewModel
* g_model
;
74 static int g_column
= -2;
75 static bool g_asending
= true;
77 //-----------------------------------------------------------------------------
78 // wxDataViewHeaderWindow
79 //-----------------------------------------------------------------------------
81 class wxDataViewHeaderWindow
: public wxHeaderCtrl
84 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
85 : wxHeaderCtrl(parent
)
89 wxDataViewCtrl
*GetOwner() const
90 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
93 // implement/override wxHeaderCtrl functions by forwarding them to the main
95 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
97 return *(GetOwner()->GetColumn(idx
));
100 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
102 wxDataViewCtrl
* const owner
= GetOwner();
104 int widthContents
= owner
->GetBestColumnWidth(idx
);
105 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
106 owner
->OnColumnChange(idx
);
112 bool SendEvent(wxEventType type
, unsigned int n
)
114 wxDataViewCtrl
* const owner
= GetOwner();
115 wxDataViewEvent
event(type
, owner
->GetId());
117 event
.SetEventObject(owner
);
119 event
.SetDataViewColumn(owner
->GetColumn(n
));
120 event
.SetModel(owner
->GetModel());
122 // for events created by wxDataViewHeaderWindow the
123 // row / value fields are not valid
124 return owner
->GetEventHandler()->ProcessEvent(event
);
127 void OnClick(wxHeaderCtrlEvent
& event
)
129 const unsigned idx
= event
.GetColumn();
131 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
134 // default handling for the column click is to sort by this column or
135 // toggle its sort order
136 wxDataViewCtrl
* const owner
= GetOwner();
137 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
138 if ( !col
->IsSortable() )
140 // no default handling for non-sortable columns
145 if ( col
->IsSortKey() )
147 // already using this column for sorting, just change the order
148 col
->ToggleSortOrder();
150 else // not using this column for sorting yet
152 // first unset the old sort column if any
153 int oldSortKey
= owner
->GetSortingColumnIndex();
154 if ( oldSortKey
!= wxNOT_FOUND
)
156 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
157 owner
->OnColumnChange(oldSortKey
);
160 owner
->SetSortingColumnIndex(idx
);
164 wxDataViewModel
* const model
= owner
->GetModel();
168 owner
->OnColumnChange(idx
);
171 void OnRClick(wxHeaderCtrlEvent
& event
)
173 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
178 void OnEndResize(wxHeaderCtrlEvent
& event
)
180 wxDataViewCtrl
* const owner
= GetOwner();
182 const unsigned col
= event
.GetColumn();
183 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
184 GetOwner()->OnColumnChange(col
);
187 void OnEndReorder(wxHeaderCtrlEvent
& event
)
189 wxDataViewCtrl
* const owner
= GetOwner();
190 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
191 event
.GetNewOrder());
194 DECLARE_EVENT_TABLE()
195 DECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
)
198 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
199 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
200 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
202 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnEndResize
)
204 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
207 //-----------------------------------------------------------------------------
208 // wxDataViewRenameTimer
209 //-----------------------------------------------------------------------------
211 class wxDataViewRenameTimer
: public wxTimer
214 wxDataViewMainWindow
*m_owner
;
217 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
221 //-----------------------------------------------------------------------------
222 // wxDataViewTreeNode
223 //-----------------------------------------------------------------------------
224 class wxDataViewTreeNode
;
225 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
226 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
228 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
229 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
231 class wxDataViewTreeNode
234 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
241 m_hasChildren
= false;
245 ~wxDataViewTreeNode()
249 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
250 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
251 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
252 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
254 void AddNode( wxDataViewTreeNode
* node
)
256 m_leaves
.Add( node
->GetItem().GetID() );
258 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
261 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
263 void AddLeaf( void * leaf
)
265 m_leaves
.Add( leaf
);
267 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
270 wxDataViewItem
& GetItem() { return m_item
; }
271 const wxDataViewItem
& GetItem() const { return m_item
; }
272 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
274 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
275 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
276 int GetIndentLevel() const
279 const wxDataViewTreeNode
* node
= this;
280 while( node
->GetParent()->GetParent() != NULL
)
282 node
= node
->GetParent();
295 int len
= m_nodes
.GetCount();
297 for ( int i
= 0;i
< len
; i
++)
298 sum
+= m_nodes
[i
]->GetSubTreeCount();
300 sum
+= m_leaves
.GetCount();
303 ChangeSubTreeCount(-sum
);
309 ChangeSubTreeCount(sum
);
312 bool HasChildren() const { return m_hasChildren
; }
313 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
315 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
316 int GetSubTreeCount() const { return m_subTreeCount
; }
317 void ChangeSubTreeCount( int num
)
321 m_subTreeCount
+= num
;
323 m_parent
->ChangeSubTreeCount(num
);
330 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
331 int len
= m_nodes
.GetCount();
332 for (int i
= 0; i
< len
; i
++)
333 m_nodes
[i
]->Resort();
334 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
339 wxDataViewTreeNode
*m_parent
;
340 wxDataViewTreeNodes m_nodes
;
341 wxDataViewTreeLeaves m_leaves
;
342 wxDataViewItem m_item
;
348 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
350 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
353 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
355 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
360 //-----------------------------------------------------------------------------
361 // wxDataViewMainWindow
362 //-----------------------------------------------------------------------------
364 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
366 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
367 WX_DEFINE_LIST(ItemList
)
369 class wxDataViewMainWindow
: public wxWindow
372 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
374 const wxPoint
&pos
= wxDefaultPosition
,
375 const wxSize
&size
= wxDefaultSize
,
376 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
377 virtual ~wxDataViewMainWindow();
379 bool IsVirtualList() const { return m_root
== NULL
; }
381 // notifications from wxDataViewModel
382 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
383 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
384 bool ItemChanged( const wxDataViewItem
&item
);
385 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
389 if (!IsVirtualList())
399 g_model
= GetOwner()->GetModel();
400 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
403 if (g_model
->HasDefaultCompare())
411 g_column
= col
->GetModelColumn();
412 g_asending
= col
->IsSortOrderAscending();
415 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
416 wxDataViewCtrl
*GetOwner() { return m_owner
; }
417 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
419 void OnPaint( wxPaintEvent
&event
);
420 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
421 void OnChar( wxKeyEvent
&event
);
422 void OnMouse( wxMouseEvent
&event
);
423 void OnSetFocus( wxFocusEvent
&event
);
424 void OnKillFocus( wxFocusEvent
&event
);
426 void UpdateDisplay();
427 void RecalculateDisplay();
428 void OnInternalIdle();
430 void OnRenameTimer();
432 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
433 void ScrollTo( int rows
, int column
);
435 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
436 void ChangeCurrentRow( unsigned int row
);
438 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
439 bool IsEmpty() { return GetRowCount() == 0; }
441 int GetCountPerPage() const;
442 int GetEndOfLastCol() const;
443 unsigned int GetFirstVisibleRow() const;
444 //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
445 unsigned int GetLastVisibleRow();
446 unsigned int GetRowCount();
448 wxDataViewItem
GetSelection() const;
449 wxDataViewSelection
GetSelections(){ return m_selection
; }
450 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
451 void Select( const wxArrayInt
& aSelections
);
452 void SelectAllRows( bool on
);
453 void SelectRow( unsigned int row
, bool on
);
454 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
455 void ReverseRowSelection( unsigned int row
);
456 bool IsRowSelected( unsigned int row
);
457 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
459 void RefreshRow( unsigned int row
);
460 void RefreshRows( unsigned int from
, unsigned int to
);
461 void RefreshRowsAfter( unsigned int firstRow
);
463 // returns the colour to be used for drawing the rules
464 wxColour
GetRuleColour() const
466 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
469 wxRect
GetLineRect( unsigned int row
) const;
471 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
472 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
473 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
475 //Some useful functions for row and item mapping
476 wxDataViewItem
GetItemByRow( unsigned int row
) const;
477 int GetRowByItem( const wxDataViewItem
& item
) const;
479 //Methods for building the mapping tree
480 void BuildTree( wxDataViewModel
* model
);
482 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
483 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
485 void Expand( unsigned int row
) { OnExpanding( row
); }
486 void Collapse( unsigned int row
) { OnCollapsing( row
); }
487 bool IsExpanded( unsigned int row
) const;
489 bool EnableDragSource( const wxDataFormat
&format
);
490 bool EnableDropTarget( const wxDataFormat
&format
);
492 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
493 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
494 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
498 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
499 //We did not need this temporarily
500 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
502 int RecalculateCount();
504 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
505 void OnExpanding( unsigned int row
);
506 void OnCollapsing( unsigned int row
);
508 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
511 wxDataViewCtrl
*m_owner
;
515 wxDataViewColumn
*m_currentCol
;
516 unsigned int m_currentRow
;
517 wxDataViewSelection m_selection
;
519 wxDataViewRenameTimer
*m_renameTimer
;
528 wxDataFormat m_dragFormat
;
531 wxDataFormat m_dropFormat
;
533 // for double click logic
534 unsigned int m_lineLastClicked
,
535 m_lineBeforeLastClicked
,
536 m_lineSelectSingleOnUp
;
538 // the pen used to draw horiz/vertical rules
541 // the pen used to draw the expander and the lines
544 //This is the tree structure of the model
545 wxDataViewTreeNode
* m_root
;
547 //This is the tree node under the cursor
548 wxDataViewTreeNode
* m_underMouse
;
550 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
551 DECLARE_EVENT_TABLE()
554 // ---------------------------------------------------------
555 // wxGenericDataViewModelNotifier
556 // ---------------------------------------------------------
558 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
561 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
562 { m_mainWindow
= mainWindow
; }
564 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
565 { return m_mainWindow
->ItemAdded( parent
, item
); }
566 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
567 { return m_mainWindow
->ItemDeleted( parent
, item
); }
568 virtual bool ItemChanged( const wxDataViewItem
& item
)
569 { return m_mainWindow
->ItemChanged(item
); }
570 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
571 { return m_mainWindow
->ValueChanged( item
, col
); }
572 virtual bool Cleared()
573 { return m_mainWindow
->Cleared(); }
574 virtual void Resort()
575 { m_mainWindow
->Resort(); }
577 wxDataViewMainWindow
*m_mainWindow
;
580 // ---------------------------------------------------------
581 // wxDataViewRenderer
582 // ---------------------------------------------------------
584 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
586 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
587 wxDataViewCellMode mode
,
589 wxDataViewRendererBase( varianttype
, mode
, align
)
598 wxDataViewRenderer::~wxDataViewRenderer()
604 wxDC
*wxDataViewRenderer::GetDC()
608 if (GetOwner() == NULL
)
610 if (GetOwner()->GetOwner() == NULL
)
612 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
618 void wxDataViewRenderer::SetAlignment( int align
)
623 int wxDataViewRenderer::GetAlignment() const
628 int wxDataViewRenderer::CalculateAlignment() const
630 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
632 if (GetOwner() == NULL
)
633 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
635 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
641 // ---------------------------------------------------------
642 // wxDataViewCustomRenderer
643 // ---------------------------------------------------------
645 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
647 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
648 wxDataViewCellMode mode
, int align
) :
649 wxDataViewRenderer( varianttype
, mode
, align
)
653 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
655 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
656 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
657 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
658 view
->GetForegroundColour();
659 dc
->SetTextForeground(col
);
660 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
663 // ---------------------------------------------------------
664 // wxDataViewTextRenderer
665 // ---------------------------------------------------------
667 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
669 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
670 wxDataViewCellMode mode
, int align
) :
671 wxDataViewCustomRenderer( varianttype
, mode
, align
)
675 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
677 m_text
= value
.GetString();
682 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
687 bool wxDataViewTextRenderer::HasEditorCtrl()
692 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
693 wxRect labelRect
, const wxVariant
&value
)
695 return new wxTextCtrl( parent
, wxID_ANY
, value
,
696 wxPoint(labelRect
.x
,labelRect
.y
),
697 wxSize(labelRect
.width
,labelRect
.height
) );
700 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
702 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
703 value
= text
->GetValue();
707 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
709 RenderText( m_text
, 0, cell
, dc
, state
);
713 wxSize
wxDataViewTextRenderer::GetSize() const
715 const wxDataViewCtrl
*view
= GetView();
719 view
->GetTextExtent( m_text
, &x
, &y
);
720 return wxSize( x
, y
);
722 return wxSize(80,20);
725 // ---------------------------------------------------------
726 // wxDataViewTextRendererAttr
727 // ---------------------------------------------------------
729 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
731 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
732 wxDataViewCellMode mode
, int align
) :
733 wxDataViewTextRenderer( varianttype
, mode
, align
)
738 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
745 if (m_attr
.HasColour())
747 colour
= dc
->GetTextForeground();
748 dc
->SetTextForeground( m_attr
.GetColour() );
751 if (m_attr
.GetBold() || m_attr
.GetItalic())
753 font
= dc
->GetFont();
754 wxFont myfont
= font
;
755 if (m_attr
.GetBold())
756 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
757 if (m_attr
.GetItalic())
758 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
759 dc
->SetFont( myfont
);
763 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
768 if (m_attr
.HasColour())
769 dc
->SetTextForeground( colour
);
771 if (m_attr
.GetBold() || m_attr
.GetItalic())
779 // ---------------------------------------------------------
780 // wxDataViewBitmapRenderer
781 // ---------------------------------------------------------
783 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
785 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
786 wxDataViewCellMode mode
, int align
) :
787 wxDataViewCustomRenderer( varianttype
, mode
, align
)
791 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
793 if (value
.GetType() == wxT("wxBitmap"))
795 if (value
.GetType() == wxT("wxIcon"))
801 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
806 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
809 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
810 else if (m_icon
.Ok())
811 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
816 wxSize
wxDataViewBitmapRenderer::GetSize() const
819 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
820 else if (m_icon
.Ok())
821 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
823 return wxSize(16,16);
826 // ---------------------------------------------------------
827 // wxDataViewToggleRenderer
828 // ---------------------------------------------------------
830 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
832 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
833 wxDataViewCellMode mode
, int align
) :
834 wxDataViewCustomRenderer( varianttype
, mode
, align
)
839 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
841 m_toggle
= value
.GetBool();
846 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
851 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
853 // User wxRenderer here
856 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
858 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
863 flags
|= wxCONTROL_CHECKED
;
864 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
865 flags
|= wxCONTROL_DISABLED
;
867 wxRendererNative::Get().DrawCheckBox(
868 GetOwner()->GetOwner(),
876 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
877 wxDataViewModel
*model
,
878 const wxDataViewItem
& item
, unsigned int col
)
880 bool value
= !m_toggle
;
881 wxVariant variant
= value
;
882 model
->SetValue( variant
, item
, col
);
883 model
->ValueChanged( item
, col
);
887 wxSize
wxDataViewToggleRenderer::GetSize() const
889 return wxSize(20,20);
892 // ---------------------------------------------------------
893 // wxDataViewProgressRenderer
894 // ---------------------------------------------------------
896 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
898 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
899 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
900 wxDataViewCustomRenderer( varianttype
, mode
, align
)
906 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
910 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
912 m_value
= (long) value
;
914 if (m_value
< 0) m_value
= 0;
915 if (m_value
> 100) m_value
= 100;
920 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
922 value
= (long) m_value
;
926 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
928 double pct
= (double)m_value
/ 100.0;
930 bar
.width
= (int)(cell
.width
* pct
);
931 dc
->SetPen( *wxTRANSPARENT_PEN
);
932 dc
->SetBrush( *wxBLUE_BRUSH
);
933 dc
->DrawRectangle( bar
);
935 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
936 dc
->SetPen( *wxBLACK_PEN
);
937 dc
->DrawRectangle( cell
);
942 wxSize
wxDataViewProgressRenderer::GetSize() const
944 return wxSize(40,12);
947 // ---------------------------------------------------------
948 // wxDataViewDateRenderer
949 // ---------------------------------------------------------
951 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
953 #if wxUSE_DATE_RENDERER_POPUP
955 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
958 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
959 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
960 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
965 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
966 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
967 sizer
->Add( m_cal
, 1, wxGROW
);
972 void OnCalendar( wxCalendarEvent
&event
);
974 wxCalendarCtrl
*m_cal
;
975 wxDataViewModel
*m_model
;
977 const wxDataViewItem
& m_item
;
980 virtual void OnDismiss()
985 DECLARE_EVENT_TABLE()
988 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
989 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
992 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
994 wxDateTime date
= event
.GetDate();
995 wxVariant value
= date
;
996 m_model
->SetValue( value
, m_item
, m_col
);
997 m_model
->ValueChanged( m_item
, m_col
);
1001 #endif // wxUSE_DATE_RENDERER_POPUP
1003 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1005 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1006 wxDataViewCellMode mode
, int align
) :
1007 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1011 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1013 m_date
= value
.GetDateTime();
1018 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1024 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1026 wxString tmp
= m_date
.FormatDate();
1027 RenderText( tmp
, 0, cell
, dc
, state
);
1031 wxSize
wxDataViewDateRenderer::GetSize() const
1033 const wxDataViewCtrl
* view
= GetView();
1034 wxString tmp
= m_date
.FormatDate();
1036 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1037 return wxSize(x
,y
+d
);
1040 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1041 const wxDataViewItem
& item
, unsigned int col
)
1044 model
->GetValue( variant
, item
, col
);
1045 wxDateTime value
= variant
.GetDateTime();
1047 #if wxUSE_DATE_RENDERER_POPUP
1048 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1049 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1050 wxPoint pos
= wxGetMousePosition();
1053 popup
->Popup( popup
->m_cal
);
1054 #else // !wxUSE_DATE_RENDERER_POPUP
1055 wxMessageBox(value
.Format());
1056 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1060 // ---------------------------------------------------------
1061 // wxDataViewIconTextRenderer
1062 // ---------------------------------------------------------
1064 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1066 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1067 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1068 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1071 SetAlignment(align
);
1074 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1078 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1084 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1089 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1092 const wxIcon
&icon
= m_value
.GetIcon();
1095 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1096 xoffset
= icon
.GetWidth()+4;
1099 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1104 wxSize
wxDataViewIconTextRenderer::GetSize() const
1106 const wxDataViewCtrl
*view
= GetView();
1107 if (!m_value
.GetText().empty())
1110 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1112 if (m_value
.GetIcon().IsOk())
1113 x
+= m_value
.GetIcon().GetWidth() + 4;
1114 return wxSize( x
, y
);
1116 return wxSize(80,20);
1120 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1121 wxRect
WXUNUSED(labelRect
),
1122 const wxVariant
& WXUNUSED(value
))
1128 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1129 wxVariant
& WXUNUSED(value
))
1134 //-----------------------------------------------------------------------------
1135 // wxDataViewDropTarget
1136 //-----------------------------------------------------------------------------
1138 class wxDataViewDropTarget
: public wxDropTarget
1141 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1147 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1149 wxDataFormat format
= GetMatchingPair();
1150 if (format
== wxDF_INVALID
)
1152 return m_win
->OnDragOver( format
, x
, y
, def
);
1155 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1157 wxDataFormat format
= GetMatchingPair();
1158 if (format
== wxDF_INVALID
)
1160 return m_win
->OnDrop( format
, x
, y
);
1163 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1165 wxDataFormat format
= GetMatchingPair();
1166 if (format
== wxDF_INVALID
)
1170 return m_win
->OnData( format
, x
, y
, def
);
1173 virtual void OnLeave()
1174 { m_win
->OnLeave(); }
1176 wxDataViewMainWindow
*m_win
;
1179 //-----------------------------------------------------------------------------
1180 // wxDataViewRenameTimer
1181 //-----------------------------------------------------------------------------
1183 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1188 void wxDataViewRenameTimer::Notify()
1190 m_owner
->OnRenameTimer();
1193 //-----------------------------------------------------------------------------
1194 // wxDataViewMainWindow
1195 //-----------------------------------------------------------------------------
1197 //The tree building helper, declared firstly
1198 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1200 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1202 if (row1
> row2
) return 1;
1203 if (row1
== row2
) return 0;
1208 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1210 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1211 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1212 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1213 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1214 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1215 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1218 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1219 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1220 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1221 m_selection( wxDataViewSelectionCmp
)
1226 m_lastOnSame
= false;
1227 m_renameTimer
= new wxDataViewRenameTimer( this );
1229 // TODO: user better initial values/nothing selected
1230 m_currentCol
= NULL
;
1233 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1236 m_dragStart
= wxPoint(0,0);
1237 m_lineLastClicked
= (unsigned int) -1;
1238 m_lineBeforeLastClicked
= (unsigned int) -1;
1239 m_lineSelectSingleOnUp
= (unsigned int) -1;
1241 m_dragEnabled
= false;
1242 m_dropEnabled
= false;
1246 SetBackgroundColour( *wxWHITE
);
1248 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1250 m_penRule
= wxPen(GetRuleColour());
1252 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1253 m_penExpander
= wxPen(wxColour(0,0,0));
1254 //Some new added code to deal with the tree structure
1255 m_root
= new wxDataViewTreeNode( NULL
);
1256 m_root
->SetHasChildren(true);
1258 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1260 m_underMouse
= NULL
;
1264 wxDataViewMainWindow::~wxDataViewMainWindow()
1267 delete m_renameTimer
;
1270 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1272 m_dragFormat
= format
;
1273 m_dragEnabled
= format
!= wxDF_INVALID
;
1278 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1280 m_dropFormat
= format
;
1281 m_dropEnabled
= format
!= wxDF_INVALID
;
1284 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1289 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
)
1293 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1294 unsigned int row
= GetLineAt( yy
);
1296 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1299 wxDataViewItem item
= GetItemByRow( row
);
1301 wxDataViewModel
*model
= GetOwner()->GetModel();
1303 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1304 event
.SetEventObject( m_owner
);
1305 event
.SetItem( item
);
1306 event
.SetModel( model
);
1307 event
.SetDataFormat( format
);
1308 if (!m_owner
->HandleWindowEvent( event
))
1311 if (!event
.IsAllowed())
1317 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1321 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1322 unsigned int row
= GetLineAt( yy
);
1324 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1327 wxDataViewItem item
= GetItemByRow( row
);
1329 wxDataViewModel
*model
= GetOwner()->GetModel();
1331 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1332 event
.SetEventObject( m_owner
);
1333 event
.SetItem( item
);
1334 event
.SetModel( model
);
1335 event
.SetDataFormat( format
);
1336 if (!m_owner
->HandleWindowEvent( event
))
1339 if (!event
.IsAllowed())
1345 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
)
1349 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1350 unsigned int row
= GetLineAt( yy
);
1352 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1355 wxDataViewItem item
= GetItemByRow( row
);
1357 wxDataViewModel
*model
= GetOwner()->GetModel();
1359 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1361 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1362 event
.SetEventObject( m_owner
);
1363 event
.SetItem( item
);
1364 event
.SetModel( model
);
1365 event
.SetDataFormat( format
);
1366 event
.SetDataSize( obj
->GetSize() );
1367 event
.SetDataBuffer( obj
->GetData() );
1368 if (!m_owner
->HandleWindowEvent( event
))
1371 if (!event
.IsAllowed())
1377 void wxDataViewMainWindow::OnLeave()
1381 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1383 wxDataViewModel
*model
= GetOwner()->GetModel();
1384 wxAutoBufferedPaintDC
dc( this );
1387 dc
.SetPen( *wxTRANSPARENT_PEN
);
1388 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1389 dc
.SetBrush( *wxWHITE_BRUSH
);
1390 wxSize
size( GetClientSize() );
1391 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1395 GetOwner()->PrepareDC( dc
);
1396 dc
.SetFont( GetFont() );
1398 wxRect update
= GetUpdateRegion().GetBox();
1399 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1401 // compute which items needs to be redrawn
1402 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1403 unsigned int item_count
=
1404 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1405 (int)(GetRowCount( ) - item_start
));
1406 unsigned int item_last
= item_start
+ item_count
;
1408 // compute which columns needs to be redrawn
1409 unsigned int cols
= GetOwner()->GetColumnCount();
1410 unsigned int col_start
= 0;
1411 unsigned int x_start
;
1412 for (x_start
= 0; col_start
< cols
; col_start
++)
1414 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1415 if (col
->IsHidden())
1416 continue; // skip it!
1418 unsigned int w
= col
->GetWidth();
1419 if (x_start
+w
>= (unsigned int)update
.x
)
1425 unsigned int col_last
= col_start
;
1426 unsigned int x_last
= x_start
;
1427 for (; col_last
< cols
; col_last
++)
1429 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1430 if (col
->IsHidden())
1431 continue; // skip it!
1433 if (x_last
> (unsigned int)update
.GetRight())
1436 x_last
+= col
->GetWidth();
1439 // Draw horizontal rules if required
1440 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1442 dc
.SetPen(m_penRule
);
1443 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1445 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1447 int y
= GetLineStart( i
);
1448 dc
.DrawLine(x_start
, y
, x_last
, y
);
1452 // Draw vertical rules if required
1453 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1455 dc
.SetPen(m_penRule
);
1456 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1459 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1461 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1462 if (col
->IsHidden())
1463 continue; // skip it
1465 dc
.DrawLine(x
, GetLineStart( item_start
),
1466 x
, GetLineStart( item_last
) );
1468 x
+= col
->GetWidth();
1471 // Draw last vertical rule
1472 dc
.DrawLine(x
, GetLineStart( item_start
),
1473 x
, GetLineStart( item_last
) );
1476 // redraw the background for the items which are selected/current
1477 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1479 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1480 if (selected
|| item
== m_currentRow
)
1482 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1483 if (item
== m_currentRow
)
1484 flags
|= wxCONTROL_CURRENT
;
1486 flags
|= wxCONTROL_FOCUSED
;
1488 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1489 wxRendererNative::Get().DrawItemSelectionRect
1499 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1502 // TODO-RTL: last column for RTL support
1503 expander
= GetOwner()->GetColumnAt( 0 );
1504 GetOwner()->SetExpanderColumn(expander
);
1507 // redraw all cells for all rows which must be repainted and all columns
1509 cell_rect
.x
= x_start
;
1510 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1513 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1514 wxDataViewRenderer
*cell
= col
->GetRenderer();
1515 cell_rect
.width
= col
->GetWidth();
1517 if (col
->IsHidden())
1518 continue; // skip it!
1520 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1522 // get the cell value and set it into the renderer
1524 wxDataViewTreeNode
*node
= NULL
;
1525 wxDataViewItem dataitem
;
1527 if (!IsVirtualList())
1529 node
= GetTreeNodeByRow(item
);
1533 dataitem
= node
->GetItem();
1535 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
1540 dataitem
= wxDataViewItem( wxUIntToPtr(item
) );
1543 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1544 cell
->SetValue( value
);
1546 if (cell
->GetWantsAttr())
1548 wxDataViewItemAttr attr
;
1549 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
1551 cell
->SetAttr( attr
);
1552 cell
->SetHasAttr( ret
);
1556 cell_rect
.y
= GetLineStart( item
);
1557 cell_rect
.height
= GetLineHeight( item
); // -1 is for the horizontal rules (?)
1559 //Draw the expander here.
1561 if ((!IsVirtualList()) && (col
== expander
))
1563 indent
= node
->GetIndentLevel();
1565 //Calculate the indent first
1566 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1568 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1569 // change the cell_rect.x to the appropriate pos
1570 int expander_x
= indent
+ EXPANDER_MARGIN
;
1571 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2) - (expander_width
/2) - EXPANDER_OFFSET
;
1572 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1573 dc
.SetPen( m_penExpander
);
1574 dc
.SetBrush( wxNullBrush
);
1575 if( node
->HasChildren() )
1577 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1579 if (m_underMouse
== node
)
1581 flag
|= wxCONTROL_CURRENT
;
1583 if( node
->IsOpen() )
1584 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
1586 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1588 //force the expander column to left-center align
1589 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1591 if (node
&& !node
->HasChildren())
1593 // Yes, if the node does not have any child, it must be a leaf which
1594 // mean that it is a temporarily created by GetTreeNodeByRow
1598 // cannot be bigger than allocated space
1599 wxSize size
= cell
->GetSize();
1600 // Because of the tree structure indent, here we should minus the width of the cell for drawing
1601 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
1602 // size.y = wxMin( size.y, cell_rect.height );
1603 size
.y
= cell_rect
.height
;
1605 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
1606 int align
= cell
->CalculateAlignment();
1608 // horizontal alignment:
1609 item_rect
.x
= cell_rect
.x
;
1610 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1611 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1612 else if (align
& wxALIGN_RIGHT
)
1613 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
1614 //else: wxALIGN_LEFT is the default
1616 // vertical alignment:
1617 item_rect
.y
= cell_rect
.y
;
1618 if (align
& wxALIGN_CENTER_VERTICAL
)
1619 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1620 else if (align
& wxALIGN_BOTTOM
)
1621 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
1622 //else: wxALIGN_TOP is the default
1625 item_rect
.x
+= PADDING_RIGHTLEFT
;
1626 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1628 //Here we add the tree indent
1629 item_rect
.x
+= indent
;
1632 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1633 state
|= wxDATAVIEW_CELL_SELECTED
;
1635 // TODO: it would be much more efficient to create a clipping
1636 // region for the entire column being rendered (in the OnPaint
1637 // of wxDataViewMainWindow) instead of a single clip region for
1638 // each cell. However it would mean that each renderer should
1639 // respect the given wxRect's top & bottom coords, eventually
1640 // violating only the left & right coords - however the user can
1641 // make its own renderer and thus we cannot be sure of that.
1642 dc
.SetClippingRegion( item_rect
);
1643 cell
->Render( item_rect
, &dc
, state
);
1644 dc
.DestroyClippingRegion();
1647 cell_rect
.x
+= cell_rect
.width
;
1651 void wxDataViewMainWindow::OnRenameTimer()
1653 // We have to call this here because changes may just have
1654 // been made and no screen update taken place.
1659 unsigned int cols
= GetOwner()->GetColumnCount();
1661 for (i
= 0; i
< cols
; i
++)
1663 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
1665 continue; // skip it!
1667 if (c
== m_currentCol
)
1669 xpos
+= c
->GetWidth();
1671 wxRect
labelRect( xpos
,
1672 GetLineStart( m_currentRow
),
1673 m_currentCol
->GetWidth(),
1674 GetLineHeight( m_currentRow
) );
1676 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1677 &labelRect
.x
, &labelRect
.y
);
1679 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1680 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1684 //------------------------------------------------------------------
1685 // Helper class for do operation on the tree node
1686 //------------------------------------------------------------------
1691 virtual ~DoJob() { }
1693 //The return value control how the tree-walker tranverse the tree
1694 // 0: Job done, stop tranverse and return
1695 // 1: Ignore the current node's subtree and continue
1696 // 2: Job not done, continue
1697 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1698 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1699 virtual int operator() ( void * n
) = 0;
1702 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1707 switch( func( node
) )
1718 wxDataViewTreeNodes nodes
= node
->GetNodes();
1719 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1721 int len_nodes
= nodes
.GetCount();
1722 int len
= leaves
.GetCount();
1723 int i
= 0, nodes_i
= 0;
1725 for(; i
< len
; i
++ )
1727 void * n
= leaves
[i
];
1728 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1730 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1733 if( Walker( nd
, func
) )
1752 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1763 wxDataViewTreeNode
* node
;
1764 node
= FindNode(parent
);
1769 node
->SetHasChildren( true );
1771 if( g_model
->IsContainer( item
) )
1773 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1774 newnode
->SetItem(item
);
1775 newnode
->SetHasChildren( true );
1776 node
->AddNode( newnode
);
1779 node
->AddLeaf( item
.GetID() );
1781 node
->ChangeSubTreeCount(1);
1789 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1791 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1792 const wxDataViewItem
& item
)
1797 if( m_currentRow
> GetRowCount() )
1798 m_currentRow
= m_count
- 1;
1800 m_selection
.Empty();
1807 wxDataViewTreeNode
* node
= FindNode(parent
);
1809 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
1810 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
1813 node
->GetChildren().Remove( item
.GetID() );
1814 //Manuplate selection
1815 if( m_selection
.GetCount() > 1 )
1817 m_selection
.Empty();
1819 bool isContainer
= false;
1820 wxDataViewTreeNodes nds
= node
->GetNodes();
1821 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
1823 if (nds
[i
]->GetItem() == item
)
1831 wxDataViewTreeNode
* n
= NULL
;
1832 wxDataViewTreeNodes nodes
= node
->GetNodes();
1833 int len
= nodes
.GetCount();
1834 for( int i
= 0; i
< len
; i
++)
1836 if( nodes
[i
]->GetItem() == item
)
1843 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
1845 node
->GetNodes().Remove( n
);
1846 sub
-= n
->GetSubTreeCount();
1847 ::DestroyTreeHelper(n
);
1849 //Make the row number invalid and get a new valid one when user call GetRowCount
1851 node
->ChangeSubTreeCount(sub
);
1853 //Change the current row to the last row if the current exceed the max row number
1854 if( m_currentRow
> GetRowCount() )
1855 m_currentRow
= m_count
- 1;
1862 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1868 wxWindow
*parent
= GetParent();
1869 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
1870 le
.SetEventObject(parent
);
1871 le
.SetModel(GetOwner()->GetModel());
1873 parent
->GetEventHandler()->ProcessEvent(le
);
1878 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
1880 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1881 /*#define MAX_VIRTUAL_WIDTH 100000
1883 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1884 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1885 Refresh( true, &rect );
1893 wxWindow
*parent
= GetParent();
1894 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
1895 le
.SetEventObject(parent
);
1896 le
.SetModel(GetOwner()->GetModel());
1899 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
1900 parent
->GetEventHandler()->ProcessEvent(le
);
1905 bool wxDataViewMainWindow::Cleared()
1910 BuildTree( GetOwner()->GetModel() );
1917 void wxDataViewMainWindow::UpdateDisplay()
1922 void wxDataViewMainWindow::OnInternalIdle()
1924 wxWindow::OnInternalIdle();
1928 RecalculateDisplay();
1933 void wxDataViewMainWindow::RecalculateDisplay()
1935 wxDataViewModel
*model
= GetOwner()->GetModel();
1942 int width
= GetEndOfLastCol();
1943 int height
= GetLineStart( GetRowCount() );
1945 SetVirtualSize( width
, height
);
1946 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1951 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1953 wxWindow::ScrollWindow( dx
, dy
, rect
);
1955 if (GetOwner()->m_headerArea
)
1956 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1959 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
1962 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
1963 int sy
= GetLineStart( rows
)/y
;
1967 wxRect rect
= GetClientRect();
1971 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
1972 for (x_start
= 0; colnum
< column
; colnum
++)
1974 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
1975 if (col
->IsHidden())
1976 continue; // skip it!
1978 w
= col
->GetWidth();
1982 int x_end
= x_start
+ w
;
1983 xe
= xx
+ rect
.width
;
1986 sx
= ( xx
+ x_end
- xe
)/x
;
1993 m_owner
->Scroll( sx
, sy
);
1996 int wxDataViewMainWindow::GetCountPerPage() const
1998 wxSize size
= GetClientSize();
1999 return size
.y
/ m_lineHeight
;
2002 int wxDataViewMainWindow::GetEndOfLastCol() const
2006 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2008 const wxDataViewColumn
*c
=
2009 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2012 width
+= c
->GetWidth();
2017 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2021 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2023 return GetLineAt( y
);
2026 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2028 wxSize client_size
= GetClientSize();
2029 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2030 &client_size
.x
, &client_size
.y
);
2032 //we should deal with the pixel here
2033 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2035 return wxMin( GetRowCount()-1, row
);
2038 unsigned int wxDataViewMainWindow::GetRowCount()
2040 if ( m_count
== -1 )
2042 m_count
= RecalculateCount();
2048 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2055 void wxDataViewMainWindow::SelectAllRows( bool on
)
2062 m_selection
.Clear();
2063 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2064 m_selection
.Add( i
);
2069 unsigned int first_visible
= GetFirstVisibleRow();
2070 unsigned int last_visible
= GetLastVisibleRow();
2072 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2074 unsigned int row
= m_selection
[i
];
2075 if ((row
>= first_visible
) && (row
<= last_visible
))
2078 m_selection
.Clear();
2082 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2084 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2088 m_selection
.Add( row
);
2096 m_selection
.Remove( row
);
2102 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2106 unsigned int tmp
= from
;
2112 for (i
= from
; i
<= to
; i
++)
2114 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2117 m_selection
.Add( i
);
2122 m_selection
.Remove( i
);
2125 RefreshRows( from
, to
);
2128 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2130 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2132 int n
= aSelections
[i
];
2134 m_selection
.Add( n
);
2139 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2141 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2142 m_selection
.Add( row
);
2144 m_selection
.Remove( row
);
2148 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2150 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2153 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2155 wxWindow
*parent
= GetParent();
2156 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2158 le
.SetEventObject(parent
);
2159 le
.SetModel(GetOwner()->GetModel());
2162 parent
->GetEventHandler()->ProcessEvent(le
);
2165 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2167 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2168 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2170 wxSize client_size
= GetClientSize();
2171 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2172 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2173 if (intersect_rect
.width
> 0)
2174 Refresh( true, &intersect_rect
);
2177 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2181 unsigned int tmp
= to
;
2186 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2187 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2189 wxSize client_size
= GetClientSize();
2190 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2191 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2192 if (intersect_rect
.width
> 0)
2193 Refresh( true, &intersect_rect
);
2196 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2198 wxSize client_size
= GetClientSize();
2199 int start
= GetLineStart( firstRow
);
2200 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2201 if (start
> client_size
.y
) return;
2203 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2205 Refresh( true, &rect
);
2208 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2210 wxCHECK_RET( newCurrent
< GetRowCount(),
2211 _T("invalid item index in OnArrowChar()") );
2213 // if there is no selection, we cannot move it anywhere
2214 if (!HasCurrentRow())
2217 unsigned int oldCurrent
= m_currentRow
;
2219 // in single selection we just ignore Shift as we can't select several
2221 if ( event
.ShiftDown() && !IsSingleSel() )
2223 RefreshRow( oldCurrent
);
2225 ChangeCurrentRow( newCurrent
);
2227 // select all the items between the old and the new one
2228 if ( oldCurrent
> newCurrent
)
2230 newCurrent
= oldCurrent
;
2231 oldCurrent
= m_currentRow
;
2234 SelectRows( oldCurrent
, newCurrent
, true );
2235 if (oldCurrent
!=newCurrent
)
2236 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2240 RefreshRow( oldCurrent
);
2242 // all previously selected items are unselected unless ctrl is held
2243 if ( !event
.ControlDown() )
2244 SelectAllRows(false);
2246 ChangeCurrentRow( newCurrent
);
2248 if ( !event
.ControlDown() )
2250 SelectRow( m_currentRow
, true );
2251 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2254 RefreshRow( m_currentRow
);
2257 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2260 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2264 rect
.y
= GetLineStart( row
);
2265 rect
.width
= GetEndOfLastCol();
2266 rect
.height
= GetLineHeight( row
);
2271 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2273 const wxDataViewModel
*model
= GetOwner()->GetModel();
2275 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2277 // TODO make more efficient
2282 for (r
= 0; r
< row
; r
++)
2284 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2285 if (!node
) return start
;
2287 wxDataViewItem item
= node
->GetItem();
2289 if (node
&& !node
->HasChildren())
2291 // Yes, if the node does not have any child, it must be a leaf which
2292 // mean that it is a temporarily created by GetTreeNodeByRow
2296 unsigned int cols
= GetOwner()->GetColumnCount();
2298 int height
= m_lineHeight
;
2299 for (col
= 0; col
< cols
; col
++)
2301 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2302 if (column
->IsHidden())
2303 continue; // skip it!
2305 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2306 continue; // skip it!
2308 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2310 model
->GetValue( value
, item
, column
->GetModelColumn() );
2311 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2312 renderer2
->SetValue( value
);
2313 height
= wxMax( height
, renderer
->GetSize().y
);
2324 return row
* m_lineHeight
;
2328 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2330 const wxDataViewModel
*model
= GetOwner()->GetModel();
2332 // check for the easy case first
2333 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2334 return y
/ m_lineHeight
;
2336 // TODO make more efficient
2337 unsigned int row
= 0;
2338 unsigned int yy
= 0;
2341 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2344 // not really correct...
2345 return row
+ ((y
-yy
) / m_lineHeight
);
2348 wxDataViewItem item
= node
->GetItem();
2350 if (node
&& !node
->HasChildren())
2352 // Yes, if the node does not have any child, it must be a leaf which
2353 // mean that it is a temporarily created by GetTreeNodeByRow
2357 unsigned int cols
= GetOwner()->GetColumnCount();
2359 int height
= m_lineHeight
;
2360 for (col
= 0; col
< cols
; col
++)
2362 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2363 if (column
->IsHidden())
2364 continue; // skip it!
2366 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2367 continue; // skip it!
2369 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2371 model
->GetValue( value
, item
, column
->GetModelColumn() );
2372 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2373 renderer2
->SetValue( value
);
2374 height
= wxMax( height
, renderer
->GetSize().y
);
2385 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2387 const wxDataViewModel
*model
= GetOwner()->GetModel();
2389 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2391 wxASSERT( !IsVirtualList() );
2393 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2394 // wxASSERT( node );
2395 if (!node
) return m_lineHeight
;
2397 wxDataViewItem item
= node
->GetItem();
2399 if (node
&& !node
->HasChildren())
2401 // Yes, if the node does not have any child, it must be a leaf which
2402 // mean that it is a temporarily created by GetTreeNodeByRow
2406 int height
= m_lineHeight
;
2408 unsigned int cols
= GetOwner()->GetColumnCount();
2410 for (col
= 0; col
< cols
; col
++)
2412 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2413 if (column
->IsHidden())
2414 continue; // skip it!
2416 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2417 continue; // skip it!
2419 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2421 model
->GetValue( value
, item
, column
->GetModelColumn() );
2422 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2423 renderer2
->SetValue( value
);
2424 height
= wxMax( height
, renderer
->GetSize().y
);
2431 return m_lineHeight
;
2435 class RowToItemJob
: public DoJob
2438 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2439 virtual ~RowToItemJob() { }
2441 virtual int operator() ( wxDataViewTreeNode
* node
)
2444 if( current
== static_cast<int>(row
))
2446 ret
= node
->GetItem();
2450 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2452 current
+= node
->GetSubTreeCount();
2457 //If the current has no child node, we can find the desired item of the row number directly.
2458 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2459 if( node
->GetNodes().GetCount() == 0)
2461 int index
= static_cast<int>(row
) - current
- 1;
2462 ret
= node
->GetChildren().Item( index
);
2469 virtual int operator() ( void * n
)
2472 if( current
== static_cast<int>(row
))
2474 ret
= wxDataViewItem( n
);
2479 wxDataViewItem
GetResult(){ return ret
; }
2486 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2490 return wxDataViewItem( wxUIntToPtr(row
) );
2494 RowToItemJob
job( row
, -2 );
2495 Walker( m_root
, job
);
2496 return job
.GetResult();
2500 class RowToTreeNodeJob
: public DoJob
2503 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2506 this->current
= current
;
2510 virtual ~RowToTreeNodeJob(){ }
2512 virtual int operator() ( wxDataViewTreeNode
* node
)
2515 if( current
== static_cast<int>(row
))
2521 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2523 current
+= node
->GetSubTreeCount();
2529 //If the current has no child node, we can find the desired item of the row number directly.
2530 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2531 if( node
->GetNodes().GetCount() == 0)
2533 int index
= static_cast<int>(row
) - current
- 1;
2534 void * n
= node
->GetChildren().Item( index
);
2535 ret
= new wxDataViewTreeNode( parent
);
2536 ret
->SetItem( wxDataViewItem( n
));
2537 ret
->SetHasChildren(false);
2546 virtual int operator() ( void * n
)
2549 if( current
== static_cast<int>(row
))
2551 ret
= new wxDataViewTreeNode( parent
);
2552 ret
->SetItem( wxDataViewItem( n
));
2553 ret
->SetHasChildren(false);
2559 wxDataViewTreeNode
* GetResult(){ return ret
; }
2563 wxDataViewTreeNode
* ret
;
2564 wxDataViewTreeNode
* parent
;
2568 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2570 wxASSERT( !IsVirtualList() );
2572 RowToTreeNodeJob
job( row
, -2, m_root
);
2573 Walker( m_root
, job
);
2574 return job
.GetResult();
2577 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2579 wxWindow
*parent
= GetParent();
2580 wxDataViewEvent
le(type
, parent
->GetId());
2582 le
.SetEventObject(parent
);
2583 le
.SetModel(GetOwner()->GetModel());
2586 parent
->GetEventHandler()->ProcessEvent(le
);
2591 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2593 if (IsVirtualList())
2596 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2600 if (!node
->HasChildren())
2606 return node
->IsOpen();
2610 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2612 if (IsVirtualList())
2615 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2618 if( node
->HasChildren())
2620 if( !node
->IsOpen())
2622 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2623 //Check if the user prevent expanding
2624 if( e
.GetSkipped() )
2628 //Here I build the children of current node
2629 if( node
->GetChildrenNumber() == 0 )
2632 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2636 //Send the expanded event
2637 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2641 SelectRow( row
, false );
2642 SelectRow( row
+ 1, true );
2643 ChangeCurrentRow( row
+ 1 );
2644 SendSelectionChangedEvent( GetItemByRow(row
+1));
2652 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2654 if (IsVirtualList())
2657 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2660 wxDataViewTreeNode
* nd
= node
;
2662 if( node
->HasChildren() && node
->IsOpen() )
2664 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2665 if( e
.GetSkipped() )
2670 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2674 node
= node
->GetParent();
2677 int parent
= GetRowByItem( node
->GetItem() );
2680 SelectRow( row
, false);
2681 SelectRow(parent
, true );
2682 ChangeCurrentRow( parent
);
2683 SendSelectionChangedEvent( node
->GetItem() );
2687 if( !nd
->HasChildren())
2692 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2694 wxDataViewModel
* model
= GetOwner()->GetModel();
2701 //Compose the a parent-chain of the finding item
2703 list
.DeleteContents( true );
2704 wxDataViewItem
it( item
);
2707 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2708 list
.Insert( pItem
);
2709 it
= model
->GetParent( it
);
2712 //Find the item along the parent-chain.
2713 //This algorithm is designed to speed up the node-finding method
2714 wxDataViewTreeNode
* node
= m_root
;
2715 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
2717 if( node
->HasChildren() )
2719 if( node
->GetChildrenNumber() == 0 )
2722 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2725 wxDataViewTreeNodes nodes
= node
->GetNodes();
2729 for (i
= 0; i
< nodes
.GetCount(); i
++)
2731 if (nodes
[i
]->GetItem() == (**iter
))
2733 if (nodes
[i
]->GetItem() == item
)
2750 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
2752 wxDataViewColumn
*col
= NULL
;
2753 unsigned int cols
= GetOwner()->GetColumnCount();
2754 unsigned int colnum
= 0;
2756 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
2757 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
2759 col
= GetOwner()->GetColumnAt(colnum
);
2760 if (col
->IsHidden())
2761 continue; // skip it!
2763 unsigned int w
= col
->GetWidth();
2764 if (x_start
+w
>= (unsigned int)x
)
2771 item
= GetItemByRow( GetLineAt( y
) );
2774 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
2776 int row
= GetRowByItem(item
);
2777 int y
= GetLineStart( row
);
2778 int h
= GetLineHeight( m_lineHeight
);
2780 wxDataViewColumn
*col
= NULL
;
2781 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
2783 col
= GetOwner()->GetColumnAt( i
);
2784 x
+= col
->GetWidth();
2785 if( GetOwner()->GetColumnAt(i
+1) == column
)
2788 int w
= col
->GetWidth();
2789 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
2790 return wxRect(x
, y
, w
, h
);
2793 int wxDataViewMainWindow::RecalculateCount()
2797 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
2799 return list_model
->GetLastIndex() + 1;
2801 return list_model
->GetLastIndex() - 1;
2806 return m_root
->GetSubTreeCount();
2810 class ItemToRowJob
: public DoJob
2813 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
2820 //Maybe binary search will help to speed up this process
2821 virtual int operator() ( wxDataViewTreeNode
* node
)
2824 if( node
->GetItem() == item
)
2829 if( node
->GetItem() == **m_iter
)
2836 ret
+= node
->GetSubTreeCount();
2842 virtual int operator() ( void * n
)
2845 if( n
== item
.GetID() )
2849 //the row number is begin from zero
2850 int GetResult() { return ret
-1; }
2853 ItemList::const_iterator m_iter
;
2854 wxDataViewItem item
;
2859 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
2861 const wxDataViewModel
* model
= GetOwner()->GetModel();
2867 return wxPtrToUInt( item
.GetID() );
2874 //Compose the a parent-chain of the finding item
2876 wxDataViewItem
* pItem
;
2877 list
.DeleteContents( true );
2878 wxDataViewItem
it( item
);
2881 pItem
= new wxDataViewItem( it
);
2882 list
.Insert( pItem
);
2883 it
= model
->GetParent( it
);
2885 pItem
= new wxDataViewItem( );
2886 list
.Insert( pItem
);
2888 ItemToRowJob
job( item
, list
.begin() );
2889 Walker(m_root
, job
);
2890 return job
.GetResult();
2894 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2896 if( !model
->IsContainer( item
) )
2899 wxDataViewItemArray children
;
2900 unsigned int num
= model
->GetChildren( item
, children
);
2902 unsigned int index
= 0;
2903 while( index
< num
)
2905 if( model
->IsContainer( children
[index
] ) )
2907 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2908 n
->SetItem(children
[index
]);
2909 n
->SetHasChildren( true );
2914 node
->AddLeaf( children
[index
].GetID() );
2918 node
->SetSubTreeCount( num
);
2919 wxDataViewTreeNode
* n
= node
->GetParent();
2921 n
->ChangeSubTreeCount(num
);
2925 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2929 if (GetOwner()->GetModel()->IsVirtualListModel())
2935 m_root
= new wxDataViewTreeNode( NULL
);
2936 m_root
->SetHasChildren(true);
2938 //First we define a invalid item to fetch the top-level elements
2939 wxDataViewItem item
;
2941 BuildTreeHelper( model
, item
, m_root
);
2945 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2947 if( node
->GetNodeNumber() != 0 )
2949 int len
= node
->GetNodeNumber();
2951 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
2952 for(; i
< len
; i
++ )
2954 DestroyTreeHelper(nodes
[i
]);
2960 void wxDataViewMainWindow::DestroyTree()
2962 if (!IsVirtualList())
2964 ::DestroyTreeHelper(m_root
);
2970 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2972 if ( GetParent()->HandleAsNavigationKey(event
) )
2975 // no item -> nothing to do
2976 if (!HasCurrentRow())
2982 // don't use m_linesPerPage directly as it might not be computed yet
2983 const int pageSize
= GetCountPerPage();
2984 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2986 switch ( event
.GetKeyCode() )
2990 if (m_currentRow
>= 0)
2992 wxWindow
*parent
= GetParent();
2993 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
2994 le
.SetItem( GetItemByRow(m_currentRow
) );
2995 le
.SetEventObject(parent
);
2996 le
.SetModel(GetOwner()->GetModel());
2998 parent
->GetEventHandler()->ProcessEvent(le
);
3003 if ( m_currentRow
> 0 )
3004 OnArrowChar( m_currentRow
- 1, event
);
3008 if ( m_currentRow
< GetRowCount() - 1 )
3009 OnArrowChar( m_currentRow
+ 1, event
);
3011 //Add the process for tree expanding/collapsing
3013 OnCollapsing(m_currentRow
);
3016 OnExpanding( m_currentRow
);
3020 OnArrowChar( GetRowCount() - 1, event
);
3025 OnArrowChar( 0, event
);
3030 int steps
= pageSize
- 1;
3031 int index
= m_currentRow
- steps
;
3035 OnArrowChar( index
, event
);
3041 int steps
= pageSize
- 1;
3042 unsigned int index
= m_currentRow
+ steps
;
3043 unsigned int count
= GetRowCount();
3044 if ( index
>= count
)
3047 OnArrowChar( index
, event
);
3056 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3058 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3060 // let the base handle mouse wheel events.
3065 int x
= event
.GetX();
3066 int y
= event
.GetY();
3067 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3068 wxDataViewColumn
*col
= NULL
;
3071 unsigned int cols
= GetOwner()->GetColumnCount();
3073 for (i
= 0; i
< cols
; i
++)
3075 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3077 continue; // skip it!
3079 if (x
< xpos
+ c
->GetWidth())
3084 xpos
+= c
->GetWidth();
3089 wxDataViewRenderer
*cell
= col
->GetRenderer();
3090 unsigned int current
= GetLineAt( y
);
3091 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3093 // Unselect all if below the last row ?
3097 //Test whether the mouse is hovered on the tree item button
3099 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3101 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3102 if( node
!=NULL
&& node
->HasChildren() )
3104 int indent
= node
->GetIndentLevel();
3105 indent
= GetOwner()->GetIndent()*indent
;
3106 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
3107 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
3108 m_lineHeight
-2*EXPANDER_MARGIN
,
3109 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
3110 if( rect
.Contains( x
, y
) )
3112 //So the mouse is over the expander
3114 if (m_underMouse
&& m_underMouse
!= node
)
3116 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3117 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3119 if (m_underMouse
!= node
)
3121 //wxLogMessage("Do the row: %d", current);
3122 RefreshRow(current
);
3124 m_underMouse
= node
;
3127 if (node
!=NULL
&& !node
->HasChildren())
3132 if (m_underMouse
!= NULL
)
3134 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3135 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3136 m_underMouse
= NULL
;
3140 wxDataViewModel
*model
= GetOwner()->GetModel();
3142 if (event
.Dragging())
3144 if (m_dragCount
== 0)
3146 // we have to report the raw, physical coords as we want to be
3147 // able to call HitTest(event.m_pointDrag) from the user code to
3148 // get the item being dragged
3149 m_dragStart
= event
.GetPosition();
3154 if (m_dragCount
!= 3)
3157 if (event
.LeftIsDown())
3159 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
, &m_dragStart
.x
, &m_dragStart
.y
);
3160 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3161 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3163 // Notify cell about drag
3164 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3165 event
.SetEventObject( m_owner
);
3166 event
.SetItem( item
);
3167 event
.SetModel( model
);
3168 if (!m_owner
->HandleWindowEvent( event
))
3171 if (!event
.IsAllowed())
3174 wxDataObject
*obj
= event
.GetDataObject();
3178 wxDropSource
drag( m_owner
);
3179 drag
.SetData( *obj
);
3180 // wxImage image( 80, 20 );
3181 // wxBitmap bitmap( image );
3182 wxDragResult res
= drag
.DoDragDrop();
3192 bool forceClick
= false;
3194 if (event
.ButtonDClick())
3196 m_renameTimer
->Stop();
3197 m_lastOnSame
= false;
3200 wxDataViewItem item
= GetItemByRow(current
);
3201 bool ignore_other_columns
=
3202 ((GetOwner()->GetExpanderColumn() != col
) &&
3203 (model
->IsContainer(item
)) &&
3204 (!model
->HasContainerColumns(item
)));
3206 if (event
.LeftDClick())
3208 if ( current
== m_lineLastClicked
)
3210 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3213 model
->GetValue( value
, item
, col
->GetModelColumn() );
3214 cell
->SetValue( value
);
3215 wxRect
cell_rect( xpos
, GetLineStart( current
),
3216 col
->GetWidth(), GetLineHeight( current
) );
3217 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3222 wxWindow
*parent
= GetParent();
3223 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3225 le
.SetEventObject(parent
);
3226 le
.SetModel(GetOwner()->GetModel());
3228 parent
->GetEventHandler()->ProcessEvent(le
);
3234 // The first click was on another item, so don't interpret this as
3235 // a double click, but as a simple click instead
3242 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3244 // select single line
3245 SelectAllRows( false );
3246 SelectRow( m_lineSelectSingleOnUp
, true );
3247 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3250 //Process the event of user clicking the expander
3251 bool expander
= false;
3252 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3254 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3255 if( node
!=NULL
&& node
->HasChildren() )
3257 int indent
= node
->GetIndentLevel();
3258 indent
= GetOwner()->GetIndent()*indent
;
3259 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
3260 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
3261 m_lineHeight
-2*EXPANDER_MARGIN
,
3262 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
3264 if( rect
.Contains( x
, y
) )
3267 if( node
->IsOpen() )
3268 OnCollapsing(current
);
3270 OnExpanding( current
);
3273 if (node
&& !node
->HasChildren())
3276 //If the user click the expander, we do not do editing even if the column with expander are editable
3277 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3279 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3280 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3282 m_renameTimer
->Start( 100, true );
3286 m_lastOnSame
= false;
3287 m_lineSelectSingleOnUp
= (unsigned int)-1;
3291 // This is necessary, because after a DnD operation in
3292 // from and to ourself, the up event is swallowed by the
3293 // DnD code. So on next non-up event (which means here and
3294 // now) m_lineSelectSingleOnUp should be reset.
3295 m_lineSelectSingleOnUp
= (unsigned int)-1;
3298 if (event
.RightDown())
3300 m_lineBeforeLastClicked
= m_lineLastClicked
;
3301 m_lineLastClicked
= current
;
3303 // If the item is already selected, do not update the selection.
3304 // Multi-selections should not be cleared if a selected item is clicked.
3305 if (!IsRowSelected(current
))
3307 SelectAllRows(false);
3308 ChangeCurrentRow(current
);
3309 SelectRow(m_currentRow
,true);
3310 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3314 model
->GetValue( value
, item
, col
->GetModelColumn() );
3315 wxWindow
*parent
= GetParent();
3316 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3318 le
.SetEventObject(parent
);
3319 le
.SetModel(GetOwner()->GetModel());
3321 parent
->GetEventHandler()->ProcessEvent(le
);
3323 else if (event
.MiddleDown())
3326 if (event
.LeftDown() || forceClick
)
3330 m_lineBeforeLastClicked
= m_lineLastClicked
;
3331 m_lineLastClicked
= current
;
3333 unsigned int oldCurrentRow
= m_currentRow
;
3334 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3336 bool cmdModifierDown
= event
.CmdDown();
3337 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3339 if ( IsSingleSel() || !IsRowSelected(current
) )
3341 SelectAllRows( false );
3342 ChangeCurrentRow(current
);
3343 SelectRow(m_currentRow
,true);
3344 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3346 else // multi sel & current is highlighted & no mod keys
3348 m_lineSelectSingleOnUp
= current
;
3349 ChangeCurrentRow(current
); // change focus
3352 else // multi sel & either ctrl or shift is down
3354 if (cmdModifierDown
)
3356 ChangeCurrentRow(current
);
3357 ReverseRowSelection(m_currentRow
);
3358 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3360 else if (event
.ShiftDown())
3362 ChangeCurrentRow(current
);
3364 unsigned int lineFrom
= oldCurrentRow
,
3367 if ( lineTo
< lineFrom
)
3370 lineFrom
= m_currentRow
;
3373 SelectRows(lineFrom
, lineTo
, true);
3374 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3376 else // !ctrl, !shift
3378 // test in the enclosing if should make it impossible
3379 wxFAIL_MSG( _T("how did we get here?") );
3383 if (m_currentRow
!= oldCurrentRow
)
3384 RefreshRow( oldCurrentRow
);
3386 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3388 // Update selection here...
3391 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3392 (current
== oldCurrentRow
)) && oldWasSelected
;
3394 // Call LeftClick after everything else as under GTK+
3395 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3397 // notify cell about right click
3399 model
->GetValue( value
, item
, col
->GetModelColumn() );
3400 cell
->SetValue( value
);
3401 wxRect
cell_rect( xpos
, GetLineStart( current
),
3402 col
->GetWidth(), GetLineHeight( current
) );
3403 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3408 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3412 if (HasCurrentRow())
3418 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3422 if (HasCurrentRow())
3428 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3430 if( m_selection
.GetCount() != 1 )
3431 return wxDataViewItem();
3433 return GetItemByRow( m_selection
.Item(0));
3436 //-----------------------------------------------------------------------------
3438 //-----------------------------------------------------------------------------
3439 WX_DEFINE_LIST(wxDataViewColumnList
)
3441 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3443 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3444 EVT_SIZE(wxDataViewCtrl::OnSize
)
3447 wxDataViewCtrl::~wxDataViewCtrl()
3450 GetModel()->RemoveNotifier( m_notifier
);
3455 void wxDataViewCtrl::Init()
3457 m_cols
.DeleteContents(true);
3460 // No sorting column at start
3461 m_sortingColumnIdx
= wxNOT_FOUND
;
3463 m_headerArea
= NULL
;
3466 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3467 const wxPoint
& pos
, const wxSize
& size
,
3468 long style
, const wxValidator
& validator
)
3470 if ( (style
& wxBORDER_MASK
) == 0)
3471 style
|= wxBORDER_SUNKEN
;
3475 if (!wxControl::Create( parent
, id
, pos
, size
,
3476 style
| wxScrolledWindowStyle
, validator
))
3479 SetInitialSize(size
);
3482 MacSetClipChildren( true );
3485 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3487 if (HasFlag(wxDV_NO_HEADER
))
3488 m_headerArea
= NULL
;
3490 m_headerArea
= new wxDataViewHeaderWindow(this);
3492 SetTargetWindow( m_clientArea
);
3494 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3496 sizer
->Add( m_headerArea
, 0, wxGROW
);
3497 sizer
->Add( m_clientArea
, 1, wxGROW
);
3504 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3508 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3511 // we need to process arrows ourselves for scrolling
3512 if ( nMsg
== WM_GETDLGCODE
)
3514 rc
|= DLGC_WANTARROWS
;
3522 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3524 wxSize newsize
= size
;
3525 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3526 newsize
.y
-= m_headerArea
->GetSize().y
;
3531 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3533 // We need to override OnSize so that our scrolled
3534 // window a) does call Layout() to use sizers for
3535 // positioning the controls but b) does not query
3536 // the sizer for their size and use that for setting
3537 // the scrollable area as set that ourselves by
3538 // calling SetScrollbar() further down.
3545 void wxDataViewCtrl::SetFocus()
3548 m_clientArea
->SetFocus();
3551 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3553 if (!wxDataViewCtrlBase::AssociateModel( model
))
3556 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3558 model
->AddNotifier( m_notifier
);
3560 m_clientArea
->DestroyTree();
3562 m_clientArea
->BuildTree(model
);
3564 m_clientArea
->UpdateDisplay();
3569 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
3571 return m_clientArea
->EnableDragSource( format
);
3574 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
3576 return m_clientArea
->EnableDropTarget( format
);
3579 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3581 if (!wxDataViewCtrlBase::AppendColumn(col
))
3584 m_cols
.Append( col
);
3585 OnColumnsCountChanged();
3589 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3591 if (!wxDataViewCtrlBase::PrependColumn(col
))
3594 m_cols
.Insert( col
);
3595 OnColumnsCountChanged();
3599 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3601 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
3604 m_cols
.Insert( pos
, col
);
3605 OnColumnsCountChanged();
3609 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
3612 m_headerArea
->UpdateColumn(idx
);
3614 m_clientArea
->UpdateDisplay();
3617 void wxDataViewCtrl::OnColumnsCountChanged()
3620 m_headerArea
->SetColumnCount(GetColumnCount());
3622 m_clientArea
->UpdateDisplay();
3625 void wxDataViewCtrl::DoSetExpanderColumn()
3627 m_clientArea
->UpdateDisplay();
3630 void wxDataViewCtrl::DoSetIndent()
3632 m_clientArea
->UpdateDisplay();
3635 unsigned int wxDataViewCtrl::GetColumnCount() const
3637 return m_cols
.GetCount();
3640 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
3645 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
3647 // columns can't be reordered if there is no header window which allows
3649 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
3652 return GetColumn(idx
);
3655 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
3657 const unsigned count
= m_cols
.size();
3658 for ( unsigned n
= 0; n
< count
; n
++ )
3660 if ( m_cols
[n
] == column
)
3667 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
3668 unsigned int WXUNUSED(new_pos
))
3670 // do _not_ reorder m_cols elements here, they should always be in the
3671 // order in which columns were added, we only display the columns in
3673 m_clientArea
->UpdateDisplay();
3676 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3678 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3683 OnColumnsCountChanged();
3688 bool wxDataViewCtrl::ClearColumns()
3691 OnColumnsCountChanged();
3695 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3699 unsigned int len
= GetColumnCount();
3700 for ( unsigned int i
= 0; i
< len
; i
++ )
3702 wxDataViewColumn
* col
= GetColumnAt(i
);
3703 if (col
->IsHidden())
3705 ret
+= col
->GetWidth();
3708 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
3715 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3717 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
3718 : GetColumn(m_sortingColumnIdx
);
3721 //Selection code with wxDataViewItem as parameters
3722 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3724 return m_clientArea
->GetSelection();
3727 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3730 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3731 int len
= selection
.GetCount();
3732 for( int i
= 0; i
< len
; i
++)
3734 unsigned int row
= selection
[i
];
3735 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3740 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3742 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
3744 wxDataViewItem last_parent
;
3746 int len
= sel
.GetCount();
3747 for( int i
= 0; i
< len
; i
++ )
3749 wxDataViewItem item
= sel
[i
];
3750 wxDataViewItem parent
= GetModel()->GetParent( item
);
3753 if (parent
!= last_parent
)
3754 ExpandAncestors(item
);
3757 last_parent
= parent
;
3758 int row
= m_clientArea
->GetRowByItem( item
);
3760 selection
.Add( static_cast<unsigned int>(row
) );
3763 m_clientArea
->SetSelections( selection
);
3766 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3768 ExpandAncestors( item
);
3770 int row
= m_clientArea
->GetRowByItem( item
);
3773 //Unselect all rows before select another in the single select mode
3774 if (m_clientArea
->IsSingleSel())
3775 m_clientArea
->SelectAllRows(false);
3776 m_clientArea
->SelectRow(row
, true);
3780 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3782 int row
= m_clientArea
->GetRowByItem( item
);
3784 m_clientArea
->SelectRow(row
, false);
3787 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3789 int row
= m_clientArea
->GetRowByItem( item
);
3792 return m_clientArea
->IsRowSelected(row
);
3797 //Selection code with row number as parameter
3798 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3801 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3802 int len
= selection
.GetCount();
3803 for( int i
= 0; i
< len
; i
++)
3805 unsigned int row
= selection
[i
];
3811 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3813 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
3814 int len
= sel
.GetCount();
3815 for( int i
= 0; i
< len
; i
++ )
3819 selection
.Add( static_cast<unsigned int>(row
) );
3821 m_clientArea
->SetSelections( selection
);
3824 void wxDataViewCtrl::Select( int row
)
3828 if (m_clientArea
->IsSingleSel())
3829 m_clientArea
->SelectAllRows(false);
3830 m_clientArea
->SelectRow( row
, true );
3834 void wxDataViewCtrl::Unselect( int row
)
3837 m_clientArea
->SelectRow(row
, false);
3840 bool wxDataViewCtrl::IsSelected( int row
) const
3843 return m_clientArea
->IsRowSelected(row
);
3847 void wxDataViewCtrl::SelectRange( int from
, int to
)
3850 for( int i
= from
; i
< to
; i
++ )
3852 m_clientArea
->Select(sel
);
3855 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3857 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3858 for( int i
= from
; i
< to
; i
++ )
3859 if( sel
.Index( i
) != wxNOT_FOUND
)
3861 m_clientArea
->SetSelections(sel
);
3864 void wxDataViewCtrl::SelectAll()
3866 m_clientArea
->SelectAllRows(true);
3869 void wxDataViewCtrl::UnselectAll()
3871 m_clientArea
->SelectAllRows(false);
3874 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3878 if( row
> (int) m_clientArea
->GetRowCount() )
3879 row
= m_clientArea
->GetRowCount();
3881 int first
= m_clientArea
->GetFirstVisibleRow();
3882 int last
= m_clientArea
->GetLastVisibleRow();
3884 m_clientArea
->ScrollTo( row
, column
);
3885 else if( row
> last
)
3886 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3888 m_clientArea
->ScrollTo( first
, column
);
3891 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3893 ExpandAncestors( item
);
3895 m_clientArea
->RecalculateDisplay();
3897 int row
= m_clientArea
->GetRowByItem(item
);
3900 if( column
== NULL
)
3901 EnsureVisible(row
, -1);
3903 EnsureVisible( row
, GetColumnIndex(column
) );
3908 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
3910 m_clientArea
->HitTest(point
, item
, column
);
3913 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
3915 return m_clientArea
->GetItemRect(item
, column
);
3918 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
3920 return m_clientArea
->GetItemByRow( row
);
3923 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
3925 return m_clientArea
->GetRowByItem( item
);
3928 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
3930 int row
= m_clientArea
->GetRowByItem( item
);
3932 m_clientArea
->Expand(row
);
3935 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
3937 int row
= m_clientArea
->GetRowByItem( item
);
3939 m_clientArea
->Collapse(row
);
3942 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
3944 int row
= m_clientArea
->GetRowByItem( item
);
3946 return m_clientArea
->IsExpanded(row
);
3952 // !wxUSE_GENERICDATAVIEWCTRL
3955 // wxUSE_DATAVIEWCTRL