1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/calctrl.h"
42 #include "wx/popupwin.h"
43 #include "wx/renderer.h"
44 #include "wx/dcbuffer.h"
47 #include "wx/listimpl.cpp"
48 #include "wx/imaglist.h"
49 #include "wx/headerctrl.h"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
58 static const int SCROLL_UNIT_X
= 15;
60 // the cell padding on the left/right
61 static const int PADDING_RIGHTLEFT
= 3;
63 // the expander space margin
64 static const int EXPANDER_MARGIN
= 4;
67 static const int EXPANDER_OFFSET
= 4;
69 static const int EXPANDER_OFFSET
= 1;
72 // Below is the compare stuff.
73 // For the generic implementation, both the leaf nodes and the nodes are sorted for
74 // fast search when needed
75 static wxDataViewModel
* g_model
;
76 static int g_column
= -2;
77 static bool g_asending
= true;
79 //-----------------------------------------------------------------------------
80 // wxDataViewHeaderWindow
81 //-----------------------------------------------------------------------------
83 class wxDataViewHeaderWindow
: public wxHeaderCtrl
86 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
87 : wxHeaderCtrl(parent
)
91 wxDataViewCtrl
*GetOwner() const
92 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
95 // implement/override wxHeaderCtrl functions by forwarding them to the main
97 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
99 return *(GetOwner()->GetColumn(idx
));
102 // FIXME: currently unused
103 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
105 wxDataViewCtrl
* const owner
= GetOwner();
107 int widthContents
= owner
->GetBestColumnWidth(idx
);
108 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
109 owner
->OnColumnChange(idx
);
115 bool SendEvent(wxEventType type
, unsigned int n
)
117 wxDataViewCtrl
* const owner
= GetOwner();
118 wxDataViewEvent
event(type
, owner
->GetId());
120 event
.SetEventObject(owner
);
122 event
.SetDataViewColumn(owner
->GetColumn(n
));
123 event
.SetModel(owner
->GetModel());
125 // for events created by wxDataViewHeaderWindow the
126 // row / value fields are not valid
127 return owner
->GetEventHandler()->ProcessEvent(event
);
130 void OnClick(wxHeaderCtrlEvent
& event
)
132 const unsigned idx
= event
.GetColumn();
134 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
137 // default handling for the column click is to sort by this column or
138 // toggle its sort order
139 wxDataViewCtrl
* const owner
= GetOwner();
140 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
141 if ( !col
->IsSortable() )
143 // no default handling for non-sortable columns
148 if ( col
->IsSortKey() )
150 // already using this column for sorting, just change the order
151 col
->ToggleSortOrder();
153 else // not using this column for sorting yet
155 // first unset the old sort column if any
156 int oldSortKey
= owner
->GetSortingColumnIndex();
157 if ( oldSortKey
!= wxNOT_FOUND
)
159 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
160 owner
->OnColumnChange(oldSortKey
);
163 owner
->SetSortingColumnIndex(idx
);
167 wxDataViewModel
* const model
= owner
->GetModel();
171 owner
->OnColumnChange(idx
);
174 void OnRClick(wxHeaderCtrlEvent
& event
)
176 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
181 void OnResize(wxHeaderCtrlEvent
& event
)
183 wxDataViewCtrl
* const owner
= GetOwner();
185 const unsigned col
= event
.GetColumn();
186 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
187 GetOwner()->OnColumnChange(col
);
190 void OnEndReorder(wxHeaderCtrlEvent
& event
)
192 wxDataViewCtrl
* const owner
= GetOwner();
193 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
194 event
.GetNewOrder());
197 DECLARE_EVENT_TABLE()
198 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
201 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
202 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
203 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
205 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
206 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
208 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
211 //-----------------------------------------------------------------------------
212 // wxDataViewRenameTimer
213 //-----------------------------------------------------------------------------
215 class wxDataViewRenameTimer
: public wxTimer
218 wxDataViewMainWindow
*m_owner
;
221 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
225 //-----------------------------------------------------------------------------
226 // wxDataViewTreeNode
227 //-----------------------------------------------------------------------------
229 class wxDataViewTreeNode
;
230 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
231 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
233 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
234 wxDataViewTreeNode
** node2
);
235 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
237 class wxDataViewTreeNode
240 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
247 m_hasChildren
= false;
251 ~wxDataViewTreeNode()
255 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
256 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
257 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
258 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
260 void AddNode( wxDataViewTreeNode
* node
)
262 m_leaves
.Add( node
->GetItem().GetID() );
264 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
267 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
269 void AddLeaf( void * leaf
)
271 m_leaves
.Add( leaf
);
273 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
276 wxDataViewItem
& GetItem() { return m_item
; }
277 const wxDataViewItem
& GetItem() const { return m_item
; }
278 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
280 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
281 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
282 int GetIndentLevel() const
285 const wxDataViewTreeNode
* node
= this;
286 while( node
->GetParent()->GetParent() != NULL
)
288 node
= node
->GetParent();
301 int len
= m_nodes
.GetCount();
303 for ( int i
= 0;i
< len
; i
++)
304 sum
+= m_nodes
[i
]->GetSubTreeCount();
306 sum
+= m_leaves
.GetCount();
309 ChangeSubTreeCount(-sum
);
315 ChangeSubTreeCount(sum
);
318 bool HasChildren() const { return m_hasChildren
; }
319 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
321 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
322 int GetSubTreeCount() const { return m_subTreeCount
; }
323 void ChangeSubTreeCount( int num
)
327 m_subTreeCount
+= num
;
329 m_parent
->ChangeSubTreeCount(num
);
336 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
337 int len
= m_nodes
.GetCount();
338 for (int i
= 0; i
< len
; i
++)
339 m_nodes
[i
]->Resort();
340 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
345 wxDataViewTreeNode
*m_parent
;
346 wxDataViewTreeNodes m_nodes
;
347 wxDataViewTreeLeaves m_leaves
;
348 wxDataViewItem m_item
;
354 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
355 wxDataViewTreeNode
** node2
)
357 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
360 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
362 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
366 //-----------------------------------------------------------------------------
367 // wxDataViewMainWindow
368 //-----------------------------------------------------------------------------
370 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
372 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
373 WX_DEFINE_LIST(ItemList
)
375 class wxDataViewMainWindow
: public wxWindow
378 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
380 const wxPoint
&pos
= wxDefaultPosition
,
381 const wxSize
&size
= wxDefaultSize
,
382 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
383 virtual ~wxDataViewMainWindow();
385 bool IsVirtualList() const { return m_root
== NULL
; }
387 // notifications from wxDataViewModel
388 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
389 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
390 bool ItemChanged( const wxDataViewItem
&item
);
391 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
395 if (!IsVirtualList())
405 g_model
= GetOwner()->GetModel();
406 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
409 if (g_model
->HasDefaultCompare())
417 g_column
= col
->GetModelColumn();
418 g_asending
= col
->IsSortOrderAscending();
421 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
422 wxDataViewCtrl
*GetOwner() { return m_owner
; }
423 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
425 #if wxUSE_DRAG_AND_DROP
426 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
427 #endif // wxUSE_DRAG_AND_DROP
428 void OnPaint( wxPaintEvent
&event
);
429 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
430 void OnChar( wxKeyEvent
&event
);
431 void OnMouse( wxMouseEvent
&event
);
432 void OnSetFocus( wxFocusEvent
&event
);
433 void OnKillFocus( wxFocusEvent
&event
);
435 void UpdateDisplay();
436 void RecalculateDisplay();
437 void OnInternalIdle();
439 void OnRenameTimer();
441 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
442 void ScrollTo( int rows
, int column
);
444 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
445 void ChangeCurrentRow( unsigned int row
);
447 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
448 bool IsEmpty() { return GetRowCount() == 0; }
450 int GetCountPerPage() const;
451 int GetEndOfLastCol() const;
452 unsigned int GetFirstVisibleRow() const;
454 // I change this method to un const because in the tree view,
455 // the displaying number of the tree are changing along with the
456 // expanding/collapsing of the tree nodes
457 unsigned int GetLastVisibleRow();
458 unsigned int GetRowCount();
460 wxDataViewItem
GetSelection() const;
461 wxDataViewSelection
GetSelections(){ return m_selection
; }
462 void SetSelections( const wxDataViewSelection
& sel
)
463 { m_selection
= sel
; UpdateDisplay(); }
464 void Select( const wxArrayInt
& aSelections
);
465 void SelectAllRows( bool on
);
466 void SelectRow( unsigned int row
, bool on
);
467 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
468 void ReverseRowSelection( unsigned int row
);
469 bool IsRowSelected( unsigned int row
);
470 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
472 void RefreshRow( unsigned int row
);
473 void RefreshRows( unsigned int from
, unsigned int to
);
474 void RefreshRowsAfter( unsigned int firstRow
);
476 // returns the colour to be used for drawing the rules
477 wxColour
GetRuleColour() const
479 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
482 wxRect
GetLineRect( unsigned int row
) const;
484 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
485 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
486 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
488 // Some useful functions for row and item mapping
489 wxDataViewItem
GetItemByRow( unsigned int row
) const;
490 int GetRowByItem( const wxDataViewItem
& item
) const;
492 // Methods for building the mapping tree
493 void BuildTree( wxDataViewModel
* model
);
495 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
496 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
498 void Expand( unsigned int row
);
499 void Collapse( unsigned int row
);
500 bool IsExpanded( unsigned int row
) const;
501 bool HasChildren( unsigned int row
) const;
503 #if wxUSE_DRAG_AND_DROP
504 bool EnableDragSource( const wxDataFormat
&format
);
505 bool EnableDropTarget( const wxDataFormat
&format
);
507 void RemoveDropHint();
508 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
509 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
510 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
512 #endif // wxUSE_DRAG_AND_DROP
515 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
516 // We did not need this temporarily
517 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
519 int RecalculateCount();
521 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
523 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
526 wxDataViewCtrl
*m_owner
;
530 wxDataViewColumn
*m_currentCol
;
531 unsigned int m_currentRow
;
532 wxDataViewSelection m_selection
;
534 wxDataViewRenameTimer
*m_renameTimer
;
539 #if wxUSE_DRAG_AND_DROP
544 wxDataFormat m_dragFormat
;
547 wxDataFormat m_dropFormat
;
549 unsigned int m_dropHintLine
;
550 #endif // wxUSE_DRAG_AND_DROP
552 // for double click logic
553 unsigned int m_lineLastClicked
,
554 m_lineBeforeLastClicked
,
555 m_lineSelectSingleOnUp
;
557 // the pen used to draw horiz/vertical rules
560 // the pen used to draw the expander and the lines
563 // This is the tree structure of the model
564 wxDataViewTreeNode
* m_root
;
567 // This is the tree node under the cursor
568 wxDataViewTreeNode
* m_underMouse
;
571 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
572 DECLARE_EVENT_TABLE()
575 // ---------------------------------------------------------
576 // wxGenericDataViewModelNotifier
577 // ---------------------------------------------------------
579 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
582 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
583 { m_mainWindow
= mainWindow
; }
585 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
586 { return m_mainWindow
->ItemAdded( parent
, item
); }
587 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
588 { return m_mainWindow
->ItemDeleted( parent
, item
); }
589 virtual bool ItemChanged( const wxDataViewItem
& item
)
590 { return m_mainWindow
->ItemChanged(item
); }
591 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
592 { return m_mainWindow
->ValueChanged( item
, col
); }
593 virtual bool Cleared()
594 { return m_mainWindow
->Cleared(); }
595 virtual void Resort()
596 { m_mainWindow
->Resort(); }
598 wxDataViewMainWindow
*m_mainWindow
;
601 // ---------------------------------------------------------
602 // wxDataViewRenderer
603 // ---------------------------------------------------------
605 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
607 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
608 wxDataViewCellMode mode
,
610 wxDataViewRendererBase( varianttype
, mode
, align
)
619 wxDataViewRenderer::~wxDataViewRenderer()
625 wxDC
*wxDataViewRenderer::GetDC()
629 if (GetOwner() == NULL
)
631 if (GetOwner()->GetOwner() == NULL
)
633 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
639 void wxDataViewRenderer::SetAlignment( int align
)
644 int wxDataViewRenderer::GetAlignment() const
649 int wxDataViewRenderer::CalculateAlignment() const
651 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
653 if (GetOwner() == NULL
)
654 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
656 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
662 // ---------------------------------------------------------
663 // wxDataViewCustomRenderer
664 // ---------------------------------------------------------
666 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
668 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
669 wxDataViewCellMode mode
, int align
) :
670 wxDataViewRenderer( varianttype
, mode
, align
)
674 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
,
675 wxRect cell
, wxDC
*dc
, int state
)
677 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
678 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
679 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
680 view
->GetForegroundColour();
681 dc
->SetTextForeground(col
);
684 cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
687 // ---------------------------------------------------------
688 // wxDataViewTextRenderer
689 // ---------------------------------------------------------
691 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
693 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
694 wxDataViewCellMode mode
, int align
) :
695 wxDataViewCustomRenderer( varianttype
, mode
, align
)
699 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
701 m_text
= value
.GetString();
706 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
711 bool wxDataViewTextRenderer::HasEditorCtrl() const
716 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
717 wxRect labelRect
, const wxVariant
&value
)
719 return new wxTextCtrl( parent
, wxID_ANY
, value
,
720 wxPoint(labelRect
.x
,labelRect
.y
),
721 wxSize(labelRect
.width
,labelRect
.height
) );
724 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
726 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
727 value
= text
->GetValue();
731 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
733 RenderText( m_text
, 0, cell
, dc
, state
);
737 wxSize
wxDataViewTextRenderer::GetSize() const
739 const wxDataViewCtrl
*view
= GetView();
741 return view
->wxWindowBase::GetTextExtent( m_text
);
742 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
745 // ---------------------------------------------------------
746 // wxDataViewTextRendererAttr
747 // ---------------------------------------------------------
749 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
751 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
752 wxDataViewCellMode mode
, int align
) :
753 wxDataViewTextRenderer( varianttype
, mode
, align
)
758 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
765 if (m_attr
.HasColour())
767 colour
= dc
->GetTextForeground();
768 dc
->SetTextForeground( m_attr
.GetColour() );
771 if (m_attr
.GetBold() || m_attr
.GetItalic())
773 font
= dc
->GetFont();
774 wxFont myfont
= font
;
775 if (m_attr
.GetBold())
776 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
777 if (m_attr
.GetItalic())
778 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
779 dc
->SetFont( myfont
);
783 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
788 if (m_attr
.HasColour())
789 dc
->SetTextForeground( colour
);
791 if (m_attr
.GetBold() || m_attr
.GetItalic())
799 // ---------------------------------------------------------
800 // wxDataViewBitmapRenderer
801 // ---------------------------------------------------------
803 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
805 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
806 wxDataViewCellMode mode
, int align
) :
807 wxDataViewCustomRenderer( varianttype
, mode
, align
)
811 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
813 if (value
.GetType() == wxT("wxBitmap"))
815 if (value
.GetType() == wxT("wxIcon"))
821 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
826 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
829 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
830 else if (m_icon
.Ok())
831 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
836 wxSize
wxDataViewBitmapRenderer::GetSize() const
839 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
840 else if (m_icon
.Ok())
841 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
843 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
846 // ---------------------------------------------------------
847 // wxDataViewToggleRenderer
848 // ---------------------------------------------------------
850 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
852 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
853 wxDataViewCellMode mode
, int align
) :
854 wxDataViewCustomRenderer( varianttype
, mode
, align
)
859 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
861 m_toggle
= value
.GetBool();
866 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
871 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
875 flags
|= wxCONTROL_CHECKED
;
876 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
877 flags
|= wxCONTROL_DISABLED
;
879 wxRendererNative::Get().DrawCheckBox(
880 GetOwner()->GetOwner(),
888 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
889 wxDataViewModel
*model
,
890 const wxDataViewItem
& item
, unsigned int col
)
892 bool value
= !m_toggle
;
893 wxVariant variant
= value
;
894 model
->SetValue( variant
, item
, col
);
895 model
->ValueChanged( item
, col
);
899 wxSize
wxDataViewToggleRenderer::GetSize() const
901 // the window parameter is not used by GetCheckBoxSize() so it's
903 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
906 // ---------------------------------------------------------
907 // wxDataViewProgressRenderer
908 // ---------------------------------------------------------
910 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
912 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
913 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
914 wxDataViewCustomRenderer( varianttype
, mode
, align
)
920 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
924 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
926 m_value
= (long) value
;
928 if (m_value
< 0) m_value
= 0;
929 if (m_value
> 100) m_value
= 100;
934 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
936 value
= (long) m_value
;
940 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
942 double pct
= (double)m_value
/ 100.0;
944 bar
.width
= (int)(cell
.width
* pct
);
945 dc
->SetPen( *wxTRANSPARENT_PEN
);
946 dc
->SetBrush( *wxBLUE_BRUSH
);
947 dc
->DrawRectangle( bar
);
949 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
950 dc
->SetPen( *wxBLACK_PEN
);
951 dc
->DrawRectangle( cell
);
956 wxSize
wxDataViewProgressRenderer::GetSize() const
958 return wxSize(40,12);
961 // ---------------------------------------------------------
962 // wxDataViewDateRenderer
963 // ---------------------------------------------------------
965 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
967 #if wxUSE_DATE_RENDERER_POPUP
969 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
972 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
973 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
974 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
979 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
980 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
981 sizer
->Add( m_cal
, 1, wxGROW
);
986 void OnCalendar( wxCalendarEvent
&event
);
988 wxCalendarCtrl
*m_cal
;
989 wxDataViewModel
*m_model
;
991 const wxDataViewItem
& m_item
;
994 virtual void OnDismiss()
999 DECLARE_EVENT_TABLE()
1002 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1003 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1006 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1008 wxDateTime date
= event
.GetDate();
1009 wxVariant value
= date
;
1010 m_model
->SetValue( value
, m_item
, m_col
);
1011 m_model
->ValueChanged( m_item
, m_col
);
1015 #endif // wxUSE_DATE_RENDERER_POPUP
1017 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1019 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1020 wxDataViewCellMode mode
, int align
) :
1021 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1025 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1027 m_date
= value
.GetDateTime();
1032 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1038 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1040 wxString tmp
= m_date
.FormatDate();
1041 RenderText( tmp
, 0, cell
, dc
, state
);
1045 wxSize
wxDataViewDateRenderer::GetSize() const
1047 const wxDataViewCtrl
* view
= GetView();
1048 wxString tmp
= m_date
.FormatDate();
1050 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1051 return wxSize(x
,y
+d
);
1054 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1055 const wxDataViewItem
& item
, unsigned int col
)
1058 model
->GetValue( variant
, item
, col
);
1059 wxDateTime value
= variant
.GetDateTime();
1061 #if wxUSE_DATE_RENDERER_POPUP
1062 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1063 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1064 wxPoint pos
= wxGetMousePosition();
1067 popup
->Popup( popup
->m_cal
);
1068 #else // !wxUSE_DATE_RENDERER_POPUP
1069 wxMessageBox(value
.Format());
1070 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1074 // ---------------------------------------------------------
1075 // wxDataViewIconTextRenderer
1076 // ---------------------------------------------------------
1078 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1080 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1081 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1082 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1085 SetAlignment(align
);
1088 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1092 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1098 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1103 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1106 const wxIcon
&icon
= m_value
.GetIcon();
1109 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1110 xoffset
= icon
.GetWidth()+4;
1113 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1118 wxSize
wxDataViewIconTextRenderer::GetSize() const
1120 const wxDataViewCtrl
*view
= GetView();
1121 if (!m_value
.GetText().empty())
1124 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1126 if (m_value
.GetIcon().IsOk())
1127 x
+= m_value
.GetIcon().GetWidth() + 4;
1128 return wxSize( x
, y
);
1130 return wxSize(80,20);
1134 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1135 wxRect
WXUNUSED(labelRect
),
1136 const wxVariant
& WXUNUSED(value
))
1142 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1143 wxVariant
& WXUNUSED(value
))
1148 //-----------------------------------------------------------------------------
1149 // wxDataViewDropTarget
1150 //-----------------------------------------------------------------------------
1152 #if wxUSE_DRAG_AND_DROP
1154 class wxBitmapCanvas
: public wxWindow
1157 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1158 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1161 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1164 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1167 dc
.DrawBitmap( m_bitmap
, 0, 0);
1173 class wxDataViewDropSource
: public wxDropSource
1176 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1184 ~wxDataViewDropSource()
1189 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1191 wxPoint pos
= wxGetMousePosition();
1195 int liney
= m_win
->GetLineStart( m_row
);
1197 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1198 m_win
->ClientToScreen( &linex
, &liney
);
1199 m_dist_x
= pos
.x
- linex
;
1200 m_dist_y
= pos
.y
- liney
;
1203 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1205 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1206 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1208 wxFRAME_TOOL_WINDOW
|
1209 wxFRAME_FLOAT_ON_PARENT
|
1210 wxFRAME_NO_TASKBAR
|
1212 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1217 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1218 m_hint
->SetTransparent( 128 );
1224 wxDataViewMainWindow
*m_win
;
1227 int m_dist_x
,m_dist_y
;
1231 class wxDataViewDropTarget
: public wxDropTarget
1234 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1240 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1242 wxDataFormat format
= GetMatchingPair();
1243 if (format
== wxDF_INVALID
)
1245 return m_win
->OnDragOver( format
, x
, y
, def
);
1248 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1250 wxDataFormat format
= GetMatchingPair();
1251 if (format
== wxDF_INVALID
)
1253 return m_win
->OnDrop( format
, x
, y
);
1256 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1258 wxDataFormat format
= GetMatchingPair();
1259 if (format
== wxDF_INVALID
)
1263 return m_win
->OnData( format
, x
, y
, def
);
1266 virtual void OnLeave()
1267 { m_win
->OnLeave(); }
1269 wxDataViewMainWindow
*m_win
;
1272 #endif // wxUSE_DRAG_AND_DROP
1274 //-----------------------------------------------------------------------------
1275 // wxDataViewRenameTimer
1276 //-----------------------------------------------------------------------------
1278 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1283 void wxDataViewRenameTimer::Notify()
1285 m_owner
->OnRenameTimer();
1288 //-----------------------------------------------------------------------------
1289 // wxDataViewMainWindow
1290 //-----------------------------------------------------------------------------
1292 // The tree building helper, declared firstly
1293 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
1294 wxDataViewTreeNode
* node
);
1296 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1298 if (row1
> row2
) return 1;
1299 if (row1
== row2
) return 0;
1303 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1305 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1306 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1307 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1308 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1309 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1310 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1313 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1314 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1315 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1316 m_selection( wxDataViewSelectionCmp
)
1321 m_lastOnSame
= false;
1322 m_renameTimer
= new wxDataViewRenameTimer( this );
1324 // TODO: user better initial values/nothing selected
1325 m_currentCol
= NULL
;
1328 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1330 #if wxUSE_DRAG_AND_DROP
1332 m_dragStart
= wxPoint(0,0);
1334 m_dragEnabled
= false;
1335 m_dropEnabled
= false;
1337 m_dropHintLine
= (unsigned int) -1;
1338 #endif // wxUSE_DRAG_AND_DROP
1340 m_lineLastClicked
= (unsigned int) -1;
1341 m_lineBeforeLastClicked
= (unsigned int) -1;
1342 m_lineSelectSingleOnUp
= (unsigned int) -1;
1346 SetBackgroundColour( *wxWHITE
);
1348 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1350 m_penRule
= wxPen(GetRuleColour());
1352 // compose a pen whichcan draw black lines
1353 // TODO: maybe there is something system colour to use
1354 m_penExpander
= wxPen(wxColour(0,0,0));
1356 m_root
= new wxDataViewTreeNode( NULL
);
1357 m_root
->SetHasChildren(true);
1359 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1361 m_underMouse
= NULL
;
1365 wxDataViewMainWindow::~wxDataViewMainWindow()
1368 delete m_renameTimer
;
1372 #if wxUSE_DRAG_AND_DROP
1373 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1375 m_dragFormat
= format
;
1376 m_dragEnabled
= format
!= wxDF_INVALID
;
1381 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1383 m_dropFormat
= format
;
1384 m_dropEnabled
= format
!= wxDF_INVALID
;
1387 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1392 void wxDataViewMainWindow::RemoveDropHint()
1397 RefreshRow( m_dropHintLine
);
1398 m_dropHintLine
= (unsigned int) -1;
1402 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1403 wxCoord y
, wxDragResult def
)
1407 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1408 unsigned int row
= GetLineAt( yy
);
1410 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1416 wxDataViewItem item
= GetItemByRow( row
);
1418 wxDataViewModel
*model
= GetOwner()->GetModel();
1420 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1421 event
.SetEventObject( m_owner
);
1422 event
.SetItem( item
);
1423 event
.SetModel( model
);
1424 event
.SetDataFormat( format
);
1425 if (!m_owner
->HandleWindowEvent( event
))
1431 if (!event
.IsAllowed())
1438 if (m_dropHint
&& (row
!= m_dropHintLine
))
1439 RefreshRow( m_dropHintLine
);
1441 m_dropHintLine
= row
;
1447 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1453 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1454 unsigned int row
= GetLineAt( yy
);
1456 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1459 wxDataViewItem item
= GetItemByRow( row
);
1461 wxDataViewModel
*model
= GetOwner()->GetModel();
1463 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1464 event
.SetEventObject( m_owner
);
1465 event
.SetItem( item
);
1466 event
.SetModel( model
);
1467 event
.SetDataFormat( format
);
1468 if (!m_owner
->HandleWindowEvent( event
))
1471 if (!event
.IsAllowed())
1477 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1482 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1483 unsigned int row
= GetLineAt( yy
);
1485 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1488 wxDataViewItem item
= GetItemByRow( row
);
1490 wxDataViewModel
*model
= GetOwner()->GetModel();
1492 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1494 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1495 event
.SetEventObject( m_owner
);
1496 event
.SetItem( item
);
1497 event
.SetModel( model
);
1498 event
.SetDataFormat( format
);
1499 event
.SetDataSize( obj
->GetSize() );
1500 event
.SetDataBuffer( obj
->GetData() );
1501 if (!m_owner
->HandleWindowEvent( event
))
1504 if (!event
.IsAllowed())
1510 void wxDataViewMainWindow::OnLeave()
1515 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1517 int height
= GetLineHeight( row
);
1519 unsigned int cols
= GetOwner()->GetColumnCount();
1521 for (col
= 0; col
< cols
; col
++)
1523 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1524 if (column
->IsHidden())
1525 continue; // skip it!
1526 width
+= column
->GetWidth();
1530 if (!IsVirtualList())
1532 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1533 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1534 indent
= indent
+ m_lineHeight
;
1535 // try to use the m_lineHeight as the expander space
1537 if(!node
->HasChildren())
1542 wxBitmap
bitmap( width
, height
);
1543 wxMemoryDC
dc( bitmap
);
1544 dc
.SetFont( GetFont() );
1545 dc
.SetPen( *wxBLACK_PEN
);
1546 dc
.SetBrush( *wxWHITE_BRUSH
);
1547 dc
.DrawRectangle( 0,0,width
,height
);
1549 wxDataViewModel
*model
= m_owner
->GetModel();
1551 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1554 // TODO-RTL: last column for RTL support
1555 expander
= GetOwner()->GetColumnAt( 0 );
1556 GetOwner()->SetExpanderColumn(expander
);
1561 for (col
= 0; col
< cols
; col
++)
1563 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1564 wxDataViewRenderer
*cell
= column
->GetRenderer();
1566 if (column
->IsHidden())
1567 continue; // skip it!
1569 width
= column
->GetWidth();
1571 if (column
== expander
)
1575 wxDataViewItem item
= GetItemByRow( row
);
1576 model
->GetValue( value
, item
, column
->GetModelColumn());
1577 cell
->SetValue( value
);
1579 if (cell
->GetWantsAttr())
1581 wxDataViewItemAttr attr
;
1582 bool ret
= model
->GetAttr( item
, column
->GetModelColumn(), attr
);
1584 cell
->SetAttr( attr
);
1585 cell
->SetHasAttr( ret
);
1588 wxSize size
= cell
->GetSize();
1589 size
.x
= wxMin( 2*PADDING_RIGHTLEFT
+ size
.x
, width
);
1591 wxRect
item_rect(x
, 0, size
.x
, size
.y
);
1593 int align
= cell
->CalculateAlignment();
1594 // horizontal alignment:
1596 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1597 item_rect
.x
= x
+ (width
/ 2) - (size
.x
/ 2);
1598 else if (align
& wxALIGN_RIGHT
)
1599 item_rect
.x
= x
+ width
- size
.x
;
1600 // else: wxALIGN_LEFT is the default
1602 // vertical alignment:
1604 if (align
& wxALIGN_CENTER_VERTICAL
)
1605 item_rect
.y
= (height
/ 2) - (size
.y
/ 2);
1606 else if (align
& wxALIGN_BOTTOM
)
1607 item_rect
.y
= height
- size
.y
;
1608 // else: wxALIGN_TOP is the default
1611 item_rect
.x
+= PADDING_RIGHTLEFT
;
1612 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1614 // dc.SetClippingRegion( item_rect );
1615 cell
->Render( item_rect
, &dc
, 0 );
1616 // dc.DestroyClippingRegion();
1624 #endif // wxUSE_DRAG_AND_DROP
1627 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1629 wxDataViewModel
*model
= GetOwner()->GetModel();
1630 wxAutoBufferedPaintDC
dc( this );
1633 dc
.SetPen( *wxTRANSPARENT_PEN
);
1634 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1635 dc
.SetBrush( *wxWHITE_BRUSH
);
1636 wxSize
size( GetClientSize() );
1637 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1641 GetOwner()->PrepareDC( dc
);
1642 dc
.SetFont( GetFont() );
1644 wxRect update
= GetUpdateRegion().GetBox();
1645 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1647 // compute which items needs to be redrawn
1648 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1649 unsigned int item_count
=
1650 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1651 (int)(GetRowCount( ) - item_start
));
1652 unsigned int item_last
= item_start
+ item_count
;
1654 // compute which columns needs to be redrawn
1655 unsigned int cols
= GetOwner()->GetColumnCount();
1656 unsigned int col_start
= 0;
1657 unsigned int x_start
;
1658 for (x_start
= 0; col_start
< cols
; col_start
++)
1660 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1661 if (col
->IsHidden())
1662 continue; // skip it!
1664 unsigned int w
= col
->GetWidth();
1665 if (x_start
+w
>= (unsigned int)update
.x
)
1671 unsigned int col_last
= col_start
;
1672 unsigned int x_last
= x_start
;
1673 for (; col_last
< cols
; col_last
++)
1675 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1676 if (col
->IsHidden())
1677 continue; // skip it!
1679 if (x_last
> (unsigned int)update
.GetRight())
1682 x_last
+= col
->GetWidth();
1685 // Draw horizontal rules if required
1686 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1688 dc
.SetPen(m_penRule
);
1689 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1691 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1693 int y
= GetLineStart( i
);
1694 dc
.DrawLine(x_start
, y
, x_last
, y
);
1698 // Draw vertical rules if required
1699 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1701 dc
.SetPen(m_penRule
);
1702 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1705 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1707 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1708 if (col
->IsHidden())
1709 continue; // skip it
1711 dc
.DrawLine(x
, GetLineStart( item_start
),
1712 x
, GetLineStart( item_last
) );
1714 x
+= col
->GetWidth();
1717 // Draw last vertical rule
1718 dc
.DrawLine(x
, GetLineStart( item_start
),
1719 x
, GetLineStart( item_last
) );
1722 // redraw the background for the items which are selected/current
1723 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1725 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1726 if (selected
|| item
== m_currentRow
)
1728 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1729 if (item
== m_currentRow
)
1730 flags
|= wxCONTROL_CURRENT
;
1732 flags
|= wxCONTROL_FOCUSED
;
1734 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1735 wxRendererNative::Get().DrawItemSelectionRect
1745 #if wxUSE_DRAG_AND_DROP
1748 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1749 x_last
, GetLineHeight( m_dropHintLine
) );
1750 dc
.SetPen( *wxBLACK_PEN
);
1751 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1752 dc
.DrawRectangle( rect
);
1754 #endif // wxUSE_DRAG_AND_DROP
1756 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1759 // TODO-RTL: last column for RTL support
1760 expander
= GetOwner()->GetColumnAt( 0 );
1761 GetOwner()->SetExpanderColumn(expander
);
1764 // redraw all cells for all rows which must be repainted and all columns
1766 cell_rect
.x
= x_start
;
1767 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1769 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1770 wxDataViewRenderer
*cell
= col
->GetRenderer();
1771 cell_rect
.width
= col
->GetWidth();
1773 if (col
->IsHidden())
1774 continue; // skip it!
1776 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1778 // get the cell value and set it into the renderer
1780 wxDataViewTreeNode
*node
= NULL
;
1781 wxDataViewItem dataitem
;
1783 if (!IsVirtualList())
1785 node
= GetTreeNodeByRow(item
);
1789 dataitem
= node
->GetItem();
1791 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1792 !model
->HasContainerColumns(dataitem
))
1797 dataitem
= wxDataViewItem( wxUIntToPtr(item
) );
1800 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1801 cell
->SetValue( value
);
1803 if (cell
->GetWantsAttr())
1805 wxDataViewItemAttr attr
;
1806 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
1808 cell
->SetAttr( attr
);
1809 cell
->SetHasAttr( ret
);
1813 cell_rect
.y
= GetLineStart( item
);
1814 cell_rect
.height
= GetLineHeight( item
);
1816 // Draw the expander here.
1818 if ((!IsVirtualList()) && (col
== expander
))
1820 indent
= node
->GetIndentLevel();
1822 // Calculate the indent first
1823 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1825 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1827 // change the cell_rect.x to the appropriate pos
1828 int expander_x
= indent
+ EXPANDER_MARGIN
;
1829 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2)
1830 - (expander_width
/2) - EXPANDER_OFFSET
;
1832 indent
= indent
+ m_lineHeight
;
1833 // try to use the m_lineHeight as the expander space
1835 dc
.SetPen( m_penExpander
);
1836 dc
.SetBrush( wxNullBrush
);
1837 if( node
->HasChildren() )
1839 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1841 if (m_underMouse
== node
)
1843 flag
|= wxCONTROL_CURRENT
;
1845 if( node
->IsOpen() )
1846 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
,
1847 flag
|wxCONTROL_EXPANDED
);
1849 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1852 // force the expander column to left-center align
1853 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1855 if (node
&& !node
->HasChildren())
1857 // Yes, if the node does not have any child, it must be a leaf which
1858 // mean that it is a temporarily created by GetTreeNodeByRow
1862 // cannot be bigger than allocated space
1863 wxSize size
= cell
->GetSize();
1865 // Because of the tree structure indent, here we should minus the width
1866 // of the cell for drawing
1867 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
1868 // size.y = wxMin( size.y, cell_rect.height );
1869 size
.y
= cell_rect
.height
;
1871 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
1872 int align
= cell
->CalculateAlignment();
1874 // horizontal alignment:
1875 item_rect
.x
= cell_rect
.x
;
1876 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1877 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1878 else if (align
& wxALIGN_RIGHT
)
1879 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
1880 // else: wxALIGN_LEFT is the default
1882 // vertical alignment:
1883 item_rect
.y
= cell_rect
.y
;
1884 if (align
& wxALIGN_CENTER_VERTICAL
)
1885 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1886 else if (align
& wxALIGN_BOTTOM
)
1887 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
1888 // else: wxALIGN_TOP is the default
1891 item_rect
.x
+= PADDING_RIGHTLEFT
;
1892 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1894 // Here we add the tree indent
1895 item_rect
.x
+= indent
;
1898 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1899 state
|= wxDATAVIEW_CELL_SELECTED
;
1901 // TODO: it would be much more efficient to create a clipping
1902 // region for the entire column being rendered (in the OnPaint
1903 // of wxDataViewMainWindow) instead of a single clip region for
1904 // each cell. However it would mean that each renderer should
1905 // respect the given wxRect's top & bottom coords, eventually
1906 // violating only the left & right coords - however the user can
1907 // make its own renderer and thus we cannot be sure of that.
1908 dc
.SetClippingRegion( item_rect
);
1909 cell
->Render( item_rect
, &dc
, state
);
1910 dc
.DestroyClippingRegion();
1913 cell_rect
.x
+= cell_rect
.width
;
1917 void wxDataViewMainWindow::OnRenameTimer()
1919 // We have to call this here because changes may just have
1920 // been made and no screen update taken place.
1923 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1924 // (needs to be tested!)
1929 unsigned int cols
= GetOwner()->GetColumnCount();
1931 for (i
= 0; i
< cols
; i
++)
1933 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
1935 continue; // skip it!
1937 if (c
== m_currentCol
)
1939 xpos
+= c
->GetWidth();
1942 // we have to take an expander column into account and compute its indentation
1943 // to get the editor at the correct x position where the actual text is
1945 if (!IsVirtualList() && GetOwner()->GetExpanderColumn() == m_currentCol
)
1947 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
1948 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1949 indent
= indent
+ m_lineHeight
;
1951 if(!node
->HasChildren())
1955 wxRect
labelRect( xpos
+ indent
,
1956 GetLineStart( m_currentRow
),
1957 m_currentCol
->GetWidth() - indent
,
1958 GetLineHeight( m_currentRow
) );
1960 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1961 &labelRect
.x
, &labelRect
.y
);
1963 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1964 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1967 //-----------------------------------------------------------------------------
1968 // Helper class for do operation on the tree node
1969 //-----------------------------------------------------------------------------
1974 virtual ~DoJob() { }
1976 // The return value control how the tree-walker tranverse the tree
1977 // 0: Job done, stop tranverse and return
1978 // 1: Ignore the current node's subtree and continue
1979 // 2: Job not done, continue
1980 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1981 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1982 virtual int operator() ( void * n
) = 0;
1985 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1990 switch( func( node
) )
2001 wxDataViewTreeNodes nodes
= node
->GetNodes();
2002 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2004 int len_nodes
= nodes
.GetCount();
2005 int len
= leaves
.GetCount();
2006 int i
= 0, nodes_i
= 0;
2008 for(; i
< len
; i
++ )
2010 void * n
= leaves
[i
];
2011 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2013 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2016 if( Walker( nd
, func
) )
2035 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2046 wxDataViewTreeNode
* node
;
2047 node
= FindNode(parent
);
2052 node
->SetHasChildren( true );
2054 if( g_model
->IsContainer( item
) )
2056 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2057 newnode
->SetItem(item
);
2058 newnode
->SetHasChildren( true );
2059 node
->AddNode( newnode
);
2062 node
->AddLeaf( item
.GetID() );
2064 node
->ChangeSubTreeCount(1);
2072 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2074 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2075 const wxDataViewItem
& item
)
2080 if( m_currentRow
> GetRowCount() )
2081 m_currentRow
= m_count
- 1;
2083 m_selection
.Empty();
2090 wxDataViewTreeNode
* node
= FindNode(parent
);
2092 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2093 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
,
2094 false, "item not found" );
2097 node
->GetChildren().Remove( item
.GetID() );
2098 // Manipolate selection
2099 if( m_selection
.GetCount() > 1 )
2101 m_selection
.Empty();
2103 bool isContainer
= false;
2104 wxDataViewTreeNodes nds
= node
->GetNodes();
2105 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2107 if (nds
[i
]->GetItem() == item
)
2115 wxDataViewTreeNode
* n
= NULL
;
2116 wxDataViewTreeNodes nodes
= node
->GetNodes();
2117 int len
= nodes
.GetCount();
2118 for( int i
= 0; i
< len
; i
++)
2120 if( nodes
[i
]->GetItem() == item
)
2127 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2129 node
->GetNodes().Remove( n
);
2130 sub
-= n
->GetSubTreeCount();
2131 ::DestroyTreeHelper(n
);
2133 // Make the row number invalid and get a new valid one when user call GetRowCount
2135 node
->ChangeSubTreeCount(sub
);
2137 // Change the current row to the last row if the current exceed the max row number
2138 if( m_currentRow
> GetRowCount() )
2139 m_currentRow
= m_count
- 1;
2146 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2152 wxWindow
*parent
= GetParent();
2153 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2154 le
.SetEventObject(parent
);
2155 le
.SetModel(GetOwner()->GetModel());
2157 parent
->GetEventHandler()->ProcessEvent(le
);
2162 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2164 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2165 /*#define MAX_VIRTUAL_WIDTH 100000
2167 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2168 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2169 Refresh( true, &rect );
2177 wxWindow
*parent
= GetParent();
2178 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2179 le
.SetEventObject(parent
);
2180 le
.SetModel(GetOwner()->GetModel());
2183 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2184 parent
->GetEventHandler()->ProcessEvent(le
);
2189 bool wxDataViewMainWindow::Cleared()
2194 BuildTree( GetOwner()->GetModel() );
2201 void wxDataViewMainWindow::UpdateDisplay()
2206 void wxDataViewMainWindow::OnInternalIdle()
2208 wxWindow::OnInternalIdle();
2212 RecalculateDisplay();
2217 void wxDataViewMainWindow::RecalculateDisplay()
2219 wxDataViewModel
*model
= GetOwner()->GetModel();
2226 int width
= GetEndOfLastCol();
2227 int height
= GetLineStart( GetRowCount() );
2229 SetVirtualSize( width
, height
);
2230 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2235 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2237 wxWindow::ScrollWindow( dx
, dy
, rect
);
2239 if (GetOwner()->m_headerArea
)
2240 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2243 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2246 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2247 int sy
= GetLineStart( rows
)/y
;
2251 wxRect rect
= GetClientRect();
2255 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2256 for (x_start
= 0; colnum
< column
; colnum
++)
2258 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2259 if (col
->IsHidden())
2260 continue; // skip it!
2262 w
= col
->GetWidth();
2266 int x_end
= x_start
+ w
;
2267 xe
= xx
+ rect
.width
;
2270 sx
= ( xx
+ x_end
- xe
)/x
;
2277 m_owner
->Scroll( sx
, sy
);
2280 int wxDataViewMainWindow::GetCountPerPage() const
2282 wxSize size
= GetClientSize();
2283 return size
.y
/ m_lineHeight
;
2286 int wxDataViewMainWindow::GetEndOfLastCol() const
2290 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2292 const wxDataViewColumn
*c
=
2293 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2296 width
+= c
->GetWidth();
2301 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2305 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2307 return GetLineAt( y
);
2310 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2312 wxSize client_size
= GetClientSize();
2313 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2314 &client_size
.x
, &client_size
.y
);
2316 // we should deal with the pixel here
2317 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2319 return wxMin( GetRowCount()-1, row
);
2322 unsigned int wxDataViewMainWindow::GetRowCount()
2324 if ( m_count
== -1 )
2326 m_count
= RecalculateCount();
2332 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2339 void wxDataViewMainWindow::SelectAllRows( bool on
)
2346 m_selection
.Clear();
2347 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2348 m_selection
.Add( i
);
2353 unsigned int first_visible
= GetFirstVisibleRow();
2354 unsigned int last_visible
= GetLastVisibleRow();
2356 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2358 unsigned int row
= m_selection
[i
];
2359 if ((row
>= first_visible
) && (row
<= last_visible
))
2362 m_selection
.Clear();
2366 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2368 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2372 m_selection
.Add( row
);
2380 m_selection
.Remove( row
);
2386 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2390 unsigned int tmp
= from
;
2396 for (i
= from
; i
<= to
; i
++)
2398 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2401 m_selection
.Add( i
);
2406 m_selection
.Remove( i
);
2409 RefreshRows( from
, to
);
2412 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2414 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2416 int n
= aSelections
[i
];
2418 m_selection
.Add( n
);
2423 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2425 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2426 m_selection
.Add( row
);
2428 m_selection
.Remove( row
);
2432 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2434 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2437 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2439 wxWindow
*parent
= GetParent();
2440 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2442 le
.SetEventObject(parent
);
2443 le
.SetModel(GetOwner()->GetModel());
2446 parent
->GetEventHandler()->ProcessEvent(le
);
2449 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2451 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2452 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2454 wxSize client_size
= GetClientSize();
2455 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2456 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2457 if (intersect_rect
.width
> 0)
2458 Refresh( true, &intersect_rect
);
2461 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2465 unsigned int tmp
= to
;
2470 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2471 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2473 wxSize client_size
= GetClientSize();
2474 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2475 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2476 if (intersect_rect
.width
> 0)
2477 Refresh( true, &intersect_rect
);
2480 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2482 wxSize client_size
= GetClientSize();
2483 int start
= GetLineStart( firstRow
);
2484 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2485 if (start
> client_size
.y
) return;
2487 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2489 Refresh( true, &rect
);
2492 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2494 wxCHECK_RET( newCurrent
< GetRowCount(),
2495 _T("invalid item index in OnArrowChar()") );
2497 // if there is no selection, we cannot move it anywhere
2498 if (!HasCurrentRow())
2501 unsigned int oldCurrent
= m_currentRow
;
2503 // in single selection we just ignore Shift as we can't select several
2505 if ( event
.ShiftDown() && !IsSingleSel() )
2507 RefreshRow( oldCurrent
);
2509 ChangeCurrentRow( newCurrent
);
2511 // select all the items between the old and the new one
2512 if ( oldCurrent
> newCurrent
)
2514 newCurrent
= oldCurrent
;
2515 oldCurrent
= m_currentRow
;
2518 SelectRows( oldCurrent
, newCurrent
, true );
2519 if (oldCurrent
!=newCurrent
)
2520 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2524 RefreshRow( oldCurrent
);
2526 // all previously selected items are unselected unless ctrl is held
2527 if ( !event
.ControlDown() )
2528 SelectAllRows(false);
2530 ChangeCurrentRow( newCurrent
);
2532 if ( !event
.ControlDown() )
2534 SelectRow( m_currentRow
, true );
2535 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2538 RefreshRow( m_currentRow
);
2541 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2544 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2548 rect
.y
= GetLineStart( row
);
2549 rect
.width
= GetEndOfLastCol();
2550 rect
.height
= GetLineHeight( row
);
2555 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2557 const wxDataViewModel
*model
= GetOwner()->GetModel();
2559 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2561 // TODO make more efficient
2566 for (r
= 0; r
< row
; r
++)
2568 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2569 if (!node
) return start
;
2571 wxDataViewItem item
= node
->GetItem();
2573 if (node
&& !node
->HasChildren())
2575 // Yes, if the node does not have any child, it must be a leaf which
2576 // mean that it is a temporarily created by GetTreeNodeByRow
2580 unsigned int cols
= GetOwner()->GetColumnCount();
2582 int height
= m_lineHeight
;
2583 for (col
= 0; col
< cols
; col
++)
2585 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2586 if (column
->IsHidden())
2587 continue; // skip it!
2590 model
->IsContainer(item
) &&
2591 !model
->HasContainerColumns(item
))
2592 continue; // skip it!
2595 model
->GetValue( value
, item
, column
->GetModelColumn() );
2597 wxDataViewRenderer
*renderer
=
2598 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2599 renderer
->SetValue( value
);
2600 height
= wxMax( height
, renderer
->GetSize().y
);
2610 return row
* m_lineHeight
;
2614 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2616 const wxDataViewModel
*model
= GetOwner()->GetModel();
2618 // check for the easy case first
2619 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2620 return y
/ m_lineHeight
;
2622 // TODO make more efficient
2623 unsigned int row
= 0;
2624 unsigned int yy
= 0;
2627 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2630 // not really correct...
2631 return row
+ ((y
-yy
) / m_lineHeight
);
2634 wxDataViewItem item
= node
->GetItem();
2636 if (node
&& !node
->HasChildren())
2638 // Yes, if the node does not have any child, it must be a leaf which
2639 // mean that it is a temporarily created by GetTreeNodeByRow
2643 unsigned int cols
= GetOwner()->GetColumnCount();
2645 int height
= m_lineHeight
;
2646 for (col
= 0; col
< cols
; col
++)
2648 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2649 if (column
->IsHidden())
2650 continue; // skip it!
2653 model
->IsContainer(item
) &&
2654 !model
->HasContainerColumns(item
))
2655 continue; // skip it!
2658 model
->GetValue( value
, item
, column
->GetModelColumn() );
2660 wxDataViewRenderer
*renderer
=
2661 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2662 renderer
->SetValue( value
);
2663 height
= wxMax( height
, renderer
->GetSize().y
);
2674 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2676 const wxDataViewModel
*model
= GetOwner()->GetModel();
2678 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2680 wxASSERT( !IsVirtualList() );
2682 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2683 // wxASSERT( node );
2684 if (!node
) return m_lineHeight
;
2686 wxDataViewItem item
= node
->GetItem();
2688 if (node
&& !node
->HasChildren())
2690 // Yes, if the node does not have any child, it must be a leaf which
2691 // mean that it is a temporarily created by GetTreeNodeByRow
2695 int height
= m_lineHeight
;
2697 unsigned int cols
= GetOwner()->GetColumnCount();
2699 for (col
= 0; col
< cols
; col
++)
2701 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2702 if (column
->IsHidden())
2703 continue; // skip it!
2706 model
->IsContainer(item
) &&
2707 !model
->HasContainerColumns(item
))
2708 continue; // skip it!
2711 model
->GetValue( value
, item
, column
->GetModelColumn() );
2713 wxDataViewRenderer
*renderer
=
2714 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2715 renderer
->SetValue( value
);
2716 height
= wxMax( height
, renderer
->GetSize().y
);
2723 return m_lineHeight
;
2727 class RowToItemJob
: public DoJob
2730 RowToItemJob( unsigned int row
, int current
)
2731 { this->row
= row
; this->current
= current
; }
2732 virtual ~RowToItemJob() {}
2734 virtual int operator() ( wxDataViewTreeNode
* node
)
2737 if( current
== static_cast<int>(row
))
2739 ret
= node
->GetItem();
2743 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2745 current
+= node
->GetSubTreeCount();
2750 // If the current has no child node, we can find the desired item of the row
2752 // This if can speed up finding in some case, and will has a very good effect
2753 // when it comes to list view
2754 if( node
->GetNodes().GetCount() == 0)
2756 int index
= static_cast<int>(row
) - current
- 1;
2757 ret
= node
->GetChildren().Item( index
);
2764 virtual int operator() ( void * n
)
2767 if( current
== static_cast<int>(row
))
2769 ret
= wxDataViewItem( n
);
2775 wxDataViewItem
GetResult() const
2784 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2788 return wxDataViewItem( wxUIntToPtr(row
) );
2792 RowToItemJob
job( row
, -2 );
2793 Walker( m_root
, job
);
2794 return job
.GetResult();
2798 class RowToTreeNodeJob
: public DoJob
2801 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2804 this->current
= current
;
2808 virtual ~RowToTreeNodeJob(){ }
2810 virtual int operator() ( wxDataViewTreeNode
* node
)
2813 if( current
== static_cast<int>(row
))
2819 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2821 current
+= node
->GetSubTreeCount();
2828 // If the current node has no children, we can find the desired item of the
2829 // row number directly.
2830 // This if can speed up finding in some case, and will have a very good
2831 // effect for list views.
2832 if( node
->GetNodes().GetCount() == 0)
2834 int index
= static_cast<int>(row
) - current
- 1;
2835 void * n
= node
->GetChildren().Item( index
);
2836 ret
= new wxDataViewTreeNode( parent
);
2837 ret
->SetItem( wxDataViewItem( n
));
2838 ret
->SetHasChildren(false);
2845 virtual int operator() ( void * n
)
2848 if( current
== static_cast<int>(row
))
2850 ret
= new wxDataViewTreeNode( parent
);
2851 ret
->SetItem( wxDataViewItem( n
));
2852 ret
->SetHasChildren(false);
2859 wxDataViewTreeNode
* GetResult() const
2865 wxDataViewTreeNode
* ret
;
2866 wxDataViewTreeNode
* parent
;
2869 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2871 wxASSERT( !IsVirtualList() );
2873 RowToTreeNodeJob
job( row
, -2, m_root
);
2874 Walker( m_root
, job
);
2875 return job
.GetResult();
2878 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2879 const wxDataViewItem
& item
)
2881 wxWindow
*parent
= GetParent();
2882 wxDataViewEvent
le(type
, parent
->GetId());
2884 le
.SetEventObject(parent
);
2885 le
.SetModel(GetOwner()->GetModel());
2888 parent
->GetEventHandler()->ProcessEvent(le
);
2892 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2894 if (IsVirtualList())
2897 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2901 if (!node
->HasChildren())
2907 return node
->IsOpen();
2910 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2912 if (IsVirtualList())
2915 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2919 if (!node
->HasChildren())
2928 void wxDataViewMainWindow::Expand( unsigned int row
)
2930 if (IsVirtualList())
2933 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2937 if (!node
->HasChildren())
2943 if (!node
->IsOpen())
2946 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2948 // Check if the user prevent expanding
2949 if( e
.GetSkipped() )
2954 // build the children of current node
2955 if( node
->GetChildrenNumber() == 0 )
2958 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2961 // By expanding the node all row indices that are currently in the selection list
2962 // and are greater than our node have become invalid. So we have to correct that now.
2963 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2964 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2966 const unsigned testRow
= m_selection
[i
];
2967 // all rows above us are not affected, so skip them
2971 m_selection
[i
] += rowAdjustment
;
2974 if(m_currentRow
> row
)
2975 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2979 // Send the expanded event
2980 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2984 void wxDataViewMainWindow::Collapse(unsigned int row
)
2986 if (IsVirtualList())
2989 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2993 if (!node
->HasChildren())
3002 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3003 if( e
.GetSkipped() )
3006 // Find out if there are selected items below the current node.
3007 bool selectCollapsingRow
= false;
3008 const unsigned rowAdjustment
= node
->GetSubTreeCount();
3009 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
3010 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3012 const unsigned testRow
= m_selection
[i
];
3013 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
3015 selectCollapsingRow
= true;
3016 // get out as soon as we have found a node that is selected
3023 // If the node to be closed has selected items the user won't see those any longer.
3024 // We select the collapsing node in this case.
3025 if(selectCollapsingRow
)
3027 SelectAllRows(false);
3028 ChangeCurrentRow(row
);
3029 SelectRow(row
, true);
3030 SendSelectionChangedEvent(GetItemByRow(row
));
3034 // if there were no selected items below our node we still need to "fix" the
3035 // selection list to adjust for the changing of the row indices.
3036 // We actually do the opposite of what we are doing in Expand().
3037 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
3039 const unsigned testRow
= m_selection
[i
];
3040 // all rows above us are not affected, so skip them
3044 m_selection
[i
] -= rowAdjustment
;
3047 // if the "current row" is being collapsed away we change it to the current row ;-)
3048 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
3049 ChangeCurrentRow(row
);
3050 else if(m_currentRow
> row
)
3051 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3056 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
3060 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3062 wxDataViewModel
* model
= GetOwner()->GetModel();
3069 // Compose the a parent-chain of the finding item
3071 list
.DeleteContents( true );
3072 wxDataViewItem
it( item
);
3075 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3076 list
.Insert( pItem
);
3077 it
= model
->GetParent( it
);
3080 // Find the item along the parent-chain.
3081 // This algorithm is designed to speed up the node-finding method
3082 wxDataViewTreeNode
* node
= m_root
;
3083 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
3085 if( node
->HasChildren() )
3087 if( node
->GetChildrenNumber() == 0 )
3090 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3093 wxDataViewTreeNodes nodes
= node
->GetNodes();
3097 for (i
= 0; i
< nodes
.GetCount(); i
++)
3099 if (nodes
[i
]->GetItem() == (**iter
))
3101 if (nodes
[i
]->GetItem() == item
)
3118 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3119 wxDataViewColumn
* &column
)
3121 wxDataViewColumn
*col
= NULL
;
3122 unsigned int cols
= GetOwner()->GetColumnCount();
3123 unsigned int colnum
= 0;
3125 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3126 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3128 col
= GetOwner()->GetColumnAt(colnum
);
3129 if (col
->IsHidden())
3130 continue; // skip it!
3132 unsigned int w
= col
->GetWidth();
3133 if (x_start
+w
>= (unsigned int)x
)
3140 item
= GetItemByRow( GetLineAt( y
) );
3143 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3144 const wxDataViewColumn
* column
)
3146 int row
= GetRowByItem(item
);
3147 int y
= GetLineStart( row
);
3148 int h
= GetLineHeight( m_lineHeight
);
3150 wxDataViewColumn
*col
= NULL
;
3151 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3153 col
= GetOwner()->GetColumnAt( i
);
3154 x
+= col
->GetWidth();
3155 if( GetOwner()->GetColumnAt(i
+1) == column
)
3158 int w
= col
->GetWidth();
3159 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3160 return wxRect(x
, y
, w
, h
);
3163 int wxDataViewMainWindow::RecalculateCount()
3167 wxDataViewIndexListModel
*list_model
=
3168 (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3170 return list_model
->GetLastIndex() + 1;
3172 return list_model
->GetLastIndex() - 1;
3177 return m_root
->GetSubTreeCount();
3181 class ItemToRowJob
: public DoJob
3184 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3191 // Maybe binary search will help to speed up this process
3192 virtual int operator() ( wxDataViewTreeNode
* node
)
3195 if( node
->GetItem() == item
)
3200 if( node
->GetItem() == **m_iter
)
3207 ret
+= node
->GetSubTreeCount();
3213 virtual int operator() ( void * n
)
3216 if( n
== item
.GetID() )
3221 // the row number is begin from zero
3222 int GetResult() const
3226 ItemList::const_iterator m_iter
;
3227 wxDataViewItem item
;
3232 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3234 const wxDataViewModel
* model
= GetOwner()->GetModel();
3240 return wxPtrToUInt( item
.GetID() );
3247 // Compose the a parent-chain of the finding item
3249 wxDataViewItem
* pItem
;
3250 list
.DeleteContents( true );
3251 wxDataViewItem
it( item
);
3254 pItem
= new wxDataViewItem( it
);
3255 list
.Insert( pItem
);
3256 it
= model
->GetParent( it
);
3258 pItem
= new wxDataViewItem( );
3259 list
.Insert( pItem
);
3261 ItemToRowJob
job( item
, list
.begin() );
3262 Walker(m_root
, job
);
3263 return job
.GetResult();
3267 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
3268 wxDataViewTreeNode
* node
)
3270 if( !model
->IsContainer( item
) )
3273 wxDataViewItemArray children
;
3274 unsigned int num
= model
->GetChildren( item
, children
);
3276 unsigned int index
= 0;
3277 while( index
< num
)
3279 if( model
->IsContainer( children
[index
] ) )
3281 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3282 n
->SetItem(children
[index
]);
3283 n
->SetHasChildren( true );
3288 node
->AddLeaf( children
[index
].GetID() );
3292 node
->SetSubTreeCount( num
);
3293 wxDataViewTreeNode
* n
= node
->GetParent();
3295 n
->ChangeSubTreeCount(num
);
3299 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3303 if (GetOwner()->GetModel()->IsVirtualListModel())
3309 m_root
= new wxDataViewTreeNode( NULL
);
3310 m_root
->SetHasChildren(true);
3312 // First we define a invalid item to fetch the top-level elements
3313 wxDataViewItem item
;
3315 BuildTreeHelper( model
, item
, m_root
);
3319 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3321 if( node
->GetNodeNumber() != 0 )
3323 int len
= node
->GetNodeNumber();
3324 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3325 for (int i
= 0; i
< len
; i
++)
3326 DestroyTreeHelper(nodes
[i
]);
3331 void wxDataViewMainWindow::DestroyTree()
3333 if (!IsVirtualList())
3335 ::DestroyTreeHelper(m_root
);
3341 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3343 if ( GetParent()->HandleAsNavigationKey(event
) )
3346 // no item -> nothing to do
3347 if (!HasCurrentRow())
3353 // don't use m_linesPerPage directly as it might not be computed yet
3354 const int pageSize
= GetCountPerPage();
3355 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3357 switch ( event
.GetKeyCode() )
3361 if (m_currentRow
>= 0)
3363 wxWindow
*parent
= GetParent();
3364 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3365 le
.SetItem( GetItemByRow(m_currentRow
) );
3366 le
.SetEventObject(parent
);
3367 le
.SetModel(GetOwner()->GetModel());
3369 parent
->GetEventHandler()->ProcessEvent(le
);
3374 if ( m_currentRow
> 0 )
3375 OnArrowChar( m_currentRow
- 1, event
);
3379 if ( m_currentRow
< GetRowCount() - 1 )
3380 OnArrowChar( m_currentRow
+ 1, event
);
3382 // Add the process for tree expanding/collapsing
3385 if (IsVirtualList())
3388 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3392 if (node
->HasChildren())
3394 Collapse(m_currentRow
);
3398 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3402 int parent
= GetRowByItem( parent_node
->GetItem() );
3405 unsigned int row
= m_currentRow
;
3406 SelectRow( row
, false);
3407 SelectRow( parent
, true );
3408 ChangeCurrentRow( parent
);
3409 SendSelectionChangedEvent( parent_node
->GetItem() );
3417 if (!IsExpanded( m_currentRow
))
3418 Expand( m_currentRow
);
3421 unsigned int row
= m_currentRow
;
3422 SelectRow( row
, false );
3423 SelectRow( row
+ 1, true );
3424 ChangeCurrentRow( row
+ 1 );
3425 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3432 OnArrowChar( GetRowCount() - 1, event
);
3437 OnArrowChar( 0, event
);
3442 int steps
= pageSize
- 1;
3443 int index
= m_currentRow
- steps
;
3447 OnArrowChar( index
, event
);
3453 int steps
= pageSize
- 1;
3454 unsigned int index
= m_currentRow
+ steps
;
3455 unsigned int count
= GetRowCount();
3456 if ( index
>= count
)
3459 OnArrowChar( index
, event
);
3468 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3470 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3472 // let the base handle mouse wheel events.
3477 int x
= event
.GetX();
3478 int y
= event
.GetY();
3479 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3480 wxDataViewColumn
*col
= NULL
;
3483 unsigned int cols
= GetOwner()->GetColumnCount();
3485 for (i
= 0; i
< cols
; i
++)
3487 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3489 continue; // skip it!
3491 if (x
< xpos
+ c
->GetWidth())
3496 xpos
+= c
->GetWidth();
3501 wxDataViewRenderer
*cell
= col
->GetRenderer();
3502 unsigned int current
= GetLineAt( y
);
3503 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3505 // Unselect all if below the last row ?
3509 // Test whether the mouse is hovered on the tree item button
3510 bool hoverOverExpander
= false;
3511 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3513 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3514 if( node
!=NULL
&& node
->HasChildren() )
3516 int indent
= node
->GetIndentLevel();
3517 indent
= GetOwner()->GetIndent()*indent
;
3519 // we make the rectangle we are looking in a bit bigger than the actual
3520 // visual expander so the user can hit that little thing reliably
3521 wxRect
rect( xpos
+ indent
,
3522 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3523 m_lineHeight
, m_lineHeight
);
3524 if( rect
.Contains(x
, y
) )
3526 // So the mouse is over the expander
3527 hoverOverExpander
= true;
3528 if (m_underMouse
&& m_underMouse
!= node
)
3530 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3531 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3533 if (m_underMouse
!= node
)
3535 // wxLogMessage("Do the row: %d", current);
3536 RefreshRow(current
);
3538 m_underMouse
= node
;
3541 if (node
!=NULL
&& !node
->HasChildren())
3544 if (!hoverOverExpander
)
3546 if (m_underMouse
!= NULL
)
3548 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3549 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3550 m_underMouse
= NULL
;
3554 wxDataViewModel
*model
= GetOwner()->GetModel();
3556 #if wxUSE_DRAG_AND_DROP
3557 if (event
.Dragging())
3559 if (m_dragCount
== 0)
3561 // we have to report the raw, physical coords as we want to be
3562 // able to call HitTest(event.m_pointDrag) from the user code to
3563 // get the item being dragged
3564 m_dragStart
= event
.GetPosition();
3569 if (m_dragCount
!= 3)
3572 if (event
.LeftIsDown())
3574 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3575 &m_dragStart
.x
, &m_dragStart
.y
);
3576 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3577 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3579 // Notify cell about drag
3580 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3581 event
.SetEventObject( m_owner
);
3582 event
.SetItem( item
);
3583 event
.SetModel( model
);
3584 if (!m_owner
->HandleWindowEvent( event
))
3587 if (!event
.IsAllowed())
3590 wxDataObject
*obj
= event
.GetDataObject();
3594 wxDataViewDropSource
drag( this, drag_item_row
);
3595 drag
.SetData( *obj
);
3596 /* wxDragResult res = */ drag
.DoDragDrop();
3605 #endif // wxUSE_DRAG_AND_DROP
3607 bool simulateClick
= false;
3609 if (event
.ButtonDClick())
3611 m_renameTimer
->Stop();
3612 m_lastOnSame
= false;
3615 wxDataViewItem item
= GetItemByRow(current
);
3616 bool ignore_other_columns
=
3617 ((GetOwner()->GetExpanderColumn() != col
) &&
3618 (model
->IsContainer(item
)) &&
3619 (!model
->HasContainerColumns(item
)));
3621 if (event
.LeftDClick())
3623 if(hoverOverExpander
)
3625 // a double click on the expander will be converted into a "simulated" normal click
3626 simulateClick
= true;
3628 else if ( current
== m_lineLastClicked
)
3630 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3633 model
->GetValue( value
, item
, col
->GetModelColumn() );
3634 cell
->SetValue( value
);
3635 wxRect
cell_rect( xpos
, GetLineStart( current
),
3636 col
->GetWidth(), GetLineHeight( current
) );
3637 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3641 wxWindow
*parent
= GetParent();
3642 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3644 le
.SetEventObject(parent
);
3645 le
.SetModel(GetOwner()->GetModel());
3647 parent
->GetEventHandler()->ProcessEvent(le
);
3653 // The first click was on another item, so don't interpret this as
3654 // a double click, but as a simple click instead
3655 simulateClick
= true;
3659 if (event
.LeftUp() && !hoverOverExpander
)
3661 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3663 // select single line
3664 SelectAllRows( false );
3665 SelectRow( m_lineSelectSingleOnUp
, true );
3666 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3669 // If the user click the expander, we do not do editing even if the column
3670 // with expander are editable
3671 if (m_lastOnSame
&& !ignore_other_columns
)
3673 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3674 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3676 m_renameTimer
->Start( 100, true );
3680 m_lastOnSame
= false;
3681 m_lineSelectSingleOnUp
= (unsigned int)-1;
3683 else if(!event
.LeftUp())
3685 // This is necessary, because after a DnD operation in
3686 // from and to ourself, the up event is swallowed by the
3687 // DnD code. So on next non-up event (which means here and
3688 // now) m_lineSelectSingleOnUp should be reset.
3689 m_lineSelectSingleOnUp
= (unsigned int)-1;
3692 if (event
.RightDown())
3694 m_lineBeforeLastClicked
= m_lineLastClicked
;
3695 m_lineLastClicked
= current
;
3697 // If the item is already selected, do not update the selection.
3698 // Multi-selections should not be cleared if a selected item is clicked.
3699 if (!IsRowSelected(current
))
3701 SelectAllRows(false);
3702 ChangeCurrentRow(current
);
3703 SelectRow(m_currentRow
,true);
3704 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3708 model
->GetValue( value
, item
, col
->GetModelColumn() );
3709 wxWindow
*parent
= GetParent();
3710 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3712 le
.SetEventObject(parent
);
3713 le
.SetModel(GetOwner()->GetModel());
3715 parent
->GetEventHandler()->ProcessEvent(le
);
3717 else if (event
.MiddleDown())
3721 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3723 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3725 // hoverOverExpander being true tells us that our node must be
3726 // valid and have children.
3727 // So we don't need any extra checks.
3728 if( node
->IsOpen() )
3733 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3737 m_lineBeforeLastClicked
= m_lineLastClicked
;
3738 m_lineLastClicked
= current
;
3740 unsigned int oldCurrentRow
= m_currentRow
;
3741 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3743 bool cmdModifierDown
= event
.CmdDown();
3744 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3746 if ( IsSingleSel() || !IsRowSelected(current
) )
3748 SelectAllRows( false );
3749 ChangeCurrentRow(current
);
3750 SelectRow(m_currentRow
,true);
3751 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3753 else // multi sel & current is highlighted & no mod keys
3755 m_lineSelectSingleOnUp
= current
;
3756 ChangeCurrentRow(current
); // change focus
3759 else // multi sel & either ctrl or shift is down
3761 if (cmdModifierDown
)
3763 ChangeCurrentRow(current
);
3764 ReverseRowSelection(m_currentRow
);
3765 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3767 else if (event
.ShiftDown())
3769 ChangeCurrentRow(current
);
3771 unsigned int lineFrom
= oldCurrentRow
,
3774 if ( lineTo
< lineFrom
)
3777 lineFrom
= m_currentRow
;
3780 SelectRows(lineFrom
, lineTo
, true);
3781 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3783 else // !ctrl, !shift
3785 // test in the enclosing if should make it impossible
3786 wxFAIL_MSG( _T("how did we get here?") );
3790 if (m_currentRow
!= oldCurrentRow
)
3791 RefreshRow( oldCurrentRow
);
3793 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3795 // Update selection here...
3798 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3799 (current
== oldCurrentRow
)) && oldWasSelected
;
3801 // Call LeftClick after everything else as under GTK+
3802 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3804 // notify cell about right click
3806 model
->GetValue( value
, item
, col
->GetModelColumn() );
3807 cell
->SetValue( value
);
3808 wxRect
cell_rect( xpos
, GetLineStart( current
),
3809 col
->GetWidth(), GetLineHeight( current
) );
3810 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
,
3811 model
, item
, col
->GetModelColumn());
3816 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3820 if (HasCurrentRow())
3826 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3830 if (HasCurrentRow())
3836 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3838 if( m_selection
.GetCount() != 1 )
3839 return wxDataViewItem();
3841 return GetItemByRow( m_selection
.Item(0));
3844 //-----------------------------------------------------------------------------
3846 //-----------------------------------------------------------------------------
3848 WX_DEFINE_LIST(wxDataViewColumnList
)
3850 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3851 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3852 EVT_SIZE(wxDataViewCtrl::OnSize
)
3855 wxDataViewCtrl::~wxDataViewCtrl()
3858 GetModel()->RemoveNotifier( m_notifier
);
3863 void wxDataViewCtrl::Init()
3865 m_cols
.DeleteContents(true);
3868 // No sorting column at start
3869 m_sortingColumnIdx
= wxNOT_FOUND
;
3871 m_headerArea
= NULL
;
3874 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3875 const wxPoint
& pos
, const wxSize
& size
,
3876 long style
, const wxValidator
& validator
)
3878 // if ( (style & wxBORDER_MASK) == 0)
3879 // style |= wxBORDER_SUNKEN;
3883 if (!wxControl::Create( parent
, id
, pos
, size
,
3884 style
| wxScrolledWindowStyle
, validator
))
3887 SetInitialSize(size
);
3890 MacSetClipChildren( true );
3893 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3895 if (HasFlag(wxDV_NO_HEADER
))
3896 m_headerArea
= NULL
;
3898 m_headerArea
= new wxDataViewHeaderWindow(this);
3900 SetTargetWindow( m_clientArea
);
3902 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3904 sizer
->Add( m_headerArea
, 0, wxGROW
);
3905 sizer
->Add( m_clientArea
, 1, wxGROW
);
3911 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3913 return wxBORDER_THEME
;
3917 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3921 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3924 // we need to process arrows ourselves for scrolling
3925 if ( nMsg
== WM_GETDLGCODE
)
3927 rc
|= DLGC_WANTARROWS
;
3935 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3937 wxSize newsize
= size
;
3938 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3939 newsize
.y
-= m_headerArea
->GetSize().y
;
3944 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3946 // We need to override OnSize so that our scrolled
3947 // window a) does call Layout() to use sizers for
3948 // positioning the controls but b) does not query
3949 // the sizer for their size and use that for setting
3950 // the scrollable area as set that ourselves by
3951 // calling SetScrollbar() further down.
3958 void wxDataViewCtrl::SetFocus()
3961 m_clientArea
->SetFocus();
3964 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3966 if (!wxDataViewCtrlBase::AssociateModel( model
))
3969 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3971 model
->AddNotifier( m_notifier
);
3973 m_clientArea
->DestroyTree();
3975 m_clientArea
->BuildTree(model
);
3977 m_clientArea
->UpdateDisplay();
3982 #if wxUSE_DRAG_AND_DROP
3984 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
3986 return m_clientArea
->EnableDragSource( format
);
3989 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
3991 return m_clientArea
->EnableDropTarget( format
);
3994 #endif // wxUSE_DRAG_AND_DROP
3996 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3998 if (!wxDataViewCtrlBase::AppendColumn(col
))
4001 m_cols
.Append( col
);
4002 OnColumnsCountChanged();
4006 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
4008 if (!wxDataViewCtrlBase::PrependColumn(col
))
4011 m_cols
.Insert( col
);
4012 OnColumnsCountChanged();
4016 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
4018 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4021 m_cols
.Insert( pos
, col
);
4022 OnColumnsCountChanged();
4026 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4029 m_headerArea
->UpdateColumn(idx
);
4031 m_clientArea
->UpdateDisplay();
4034 void wxDataViewCtrl::OnColumnsCountChanged()
4037 m_headerArea
->SetColumnCount(GetColumnCount());
4039 m_clientArea
->UpdateDisplay();
4042 void wxDataViewCtrl::DoSetExpanderColumn()
4044 m_clientArea
->UpdateDisplay();
4047 void wxDataViewCtrl::DoSetIndent()
4049 m_clientArea
->UpdateDisplay();
4052 unsigned int wxDataViewCtrl::GetColumnCount() const
4054 return m_cols
.GetCount();
4057 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4062 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4064 // columns can't be reordered if there is no header window which allows
4066 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4069 return GetColumn(idx
);
4072 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4074 const unsigned count
= m_cols
.size();
4075 for ( unsigned n
= 0; n
< count
; n
++ )
4077 if ( m_cols
[n
] == column
)
4084 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4085 unsigned int WXUNUSED(new_pos
))
4087 // do _not_ reorder m_cols elements here, they should always be in the
4088 // order in which columns were added, we only display the columns in
4090 m_clientArea
->UpdateDisplay();
4093 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4095 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4100 OnColumnsCountChanged();
4105 bool wxDataViewCtrl::ClearColumns()
4108 OnColumnsCountChanged();
4112 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4116 unsigned int len
= GetColumnCount();
4117 for ( unsigned int i
= 0; i
< len
; i
++ )
4119 wxDataViewColumn
* col
= GetColumnAt(i
);
4120 if (col
->IsHidden())
4122 ret
+= col
->GetWidth();
4125 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4132 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4134 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4135 : GetColumn(m_sortingColumnIdx
);
4138 // Selection code with wxDataViewItem as parameters
4139 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4141 return m_clientArea
->GetSelection();
4144 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4147 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4148 int len
= selection
.GetCount();
4149 for( int i
= 0; i
< len
; i
++)
4151 unsigned int row
= selection
[i
];
4152 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4157 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4159 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4161 wxDataViewItem last_parent
;
4163 int len
= sel
.GetCount();
4164 for( int i
= 0; i
< len
; i
++ )
4166 wxDataViewItem item
= sel
[i
];
4167 wxDataViewItem parent
= GetModel()->GetParent( item
);
4170 if (parent
!= last_parent
)
4171 ExpandAncestors(item
);
4174 last_parent
= parent
;
4175 int row
= m_clientArea
->GetRowByItem( item
);
4177 selection
.Add( static_cast<unsigned int>(row
) );
4180 m_clientArea
->SetSelections( selection
);
4183 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4185 ExpandAncestors( item
);
4187 int row
= m_clientArea
->GetRowByItem( item
);
4190 // Unselect all rows before select another in the single select mode
4191 if (m_clientArea
->IsSingleSel())
4192 m_clientArea
->SelectAllRows(false);
4193 m_clientArea
->SelectRow(row
, true);
4197 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4199 int row
= m_clientArea
->GetRowByItem( item
);
4201 m_clientArea
->SelectRow(row
, false);
4204 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4206 int row
= m_clientArea
->GetRowByItem( item
);
4209 return m_clientArea
->IsRowSelected(row
);
4214 // Selection code with row number as parameter
4215 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4218 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4219 int len
= selection
.GetCount();
4220 for( int i
= 0; i
< len
; i
++)
4222 unsigned int row
= selection
[i
];
4228 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4230 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4231 int len
= sel
.GetCount();
4232 for( int i
= 0; i
< len
; i
++ )
4236 selection
.Add( static_cast<unsigned int>(row
) );
4238 m_clientArea
->SetSelections( selection
);
4241 void wxDataViewCtrl::Select( int row
)
4245 if (m_clientArea
->IsSingleSel())
4246 m_clientArea
->SelectAllRows(false);
4247 m_clientArea
->SelectRow( row
, true );
4251 void wxDataViewCtrl::Unselect( int row
)
4254 m_clientArea
->SelectRow(row
, false);
4257 bool wxDataViewCtrl::IsSelected( int row
) const
4260 return m_clientArea
->IsRowSelected(row
);
4264 void wxDataViewCtrl::SelectRange( int from
, int to
)
4267 for( int i
= from
; i
< to
; i
++ )
4269 m_clientArea
->Select(sel
);
4272 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4274 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4275 for( int i
= from
; i
< to
; i
++ )
4276 if( sel
.Index( i
) != wxNOT_FOUND
)
4278 m_clientArea
->SetSelections(sel
);
4281 void wxDataViewCtrl::SelectAll()
4283 m_clientArea
->SelectAllRows(true);
4286 void wxDataViewCtrl::UnselectAll()
4288 m_clientArea
->SelectAllRows(false);
4291 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4295 if( row
> (int) m_clientArea
->GetRowCount() )
4296 row
= m_clientArea
->GetRowCount();
4298 int first
= m_clientArea
->GetFirstVisibleRow();
4299 int last
= m_clientArea
->GetLastVisibleRow();
4301 m_clientArea
->ScrollTo( row
, column
);
4302 else if( row
> last
)
4303 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4305 m_clientArea
->ScrollTo( first
, column
);
4308 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4310 ExpandAncestors( item
);
4312 m_clientArea
->RecalculateDisplay();
4314 int row
= m_clientArea
->GetRowByItem(item
);
4317 if( column
== NULL
)
4318 EnsureVisible(row
, -1);
4320 EnsureVisible( row
, GetColumnIndex(column
) );
4325 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4326 wxDataViewColumn
* &column
) const
4328 m_clientArea
->HitTest(point
, item
, column
);
4331 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4332 const wxDataViewColumn
* column
) const
4334 return m_clientArea
->GetItemRect(item
, column
);
4337 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4339 return m_clientArea
->GetItemByRow( row
);
4342 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4344 return m_clientArea
->GetRowByItem( item
);
4347 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4349 int row
= m_clientArea
->GetRowByItem( item
);
4351 m_clientArea
->Expand(row
);
4354 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4356 int row
= m_clientArea
->GetRowByItem( item
);
4357 wxPrintf( "row %d\n", row
);
4359 m_clientArea
->Collapse(row
);
4362 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4364 int row
= m_clientArea
->GetRowByItem( item
);
4366 return m_clientArea
->IsExpanded(row
);
4371 // !wxUSE_GENERICDATAVIEWCTRL
4374 // wxUSE_DATAVIEWCTRL