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 //-----------------------------------------------------------------------------
56 class wxDataViewColumn
;
57 class wxDataViewHeaderWindow
;
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 static const int SCROLL_UNIT_X
= 15;
66 // the cell padding on the left/right
67 static const int PADDING_RIGHTLEFT
= 3;
69 // the expander space margin
70 static const int EXPANDER_MARGIN
= 4;
73 static const int EXPANDER_OFFSET
= 4;
75 static const int EXPANDER_OFFSET
= 1;
78 // Below is the compare stuff.
79 // For the generic implementation, both the leaf nodes and the nodes are sorted for
80 // fast search when needed
81 static wxDataViewModel
* g_model
;
82 static int g_column
= -2;
83 static bool g_asending
= true;
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 void wxDataViewColumn::Init(int width
, wxAlignment align
, int flags
)
91 m_width
= width
== wxCOL_WIDTH_DEFAULT
? wxDVC_DEFAULT_WIDTH
: width
;
96 m_sortAscending
= true;
99 void wxDataViewColumn::UpdateDisplay()
103 int idx
= m_owner
->GetColumnIndex( this );
104 m_owner
->OnColumnChange( idx
);
108 //-----------------------------------------------------------------------------
109 // wxDataViewHeaderWindow
110 //-----------------------------------------------------------------------------
112 class wxDataViewHeaderWindow
: public wxHeaderCtrl
115 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
116 : wxHeaderCtrl(parent
)
120 wxDataViewCtrl
*GetOwner() const
121 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
124 // implement/override wxHeaderCtrl functions by forwarding them to the main
126 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
128 return *(GetOwner()->GetColumn(idx
));
131 // FIXME: currently unused
132 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
134 wxDataViewCtrl
* const owner
= GetOwner();
136 int widthContents
= owner
->GetBestColumnWidth(idx
);
137 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
138 owner
->OnColumnChange(idx
);
144 bool SendEvent(wxEventType type
, unsigned int n
)
146 wxDataViewCtrl
* const owner
= GetOwner();
147 wxDataViewEvent
event(type
, owner
->GetId());
149 event
.SetEventObject(owner
);
151 event
.SetDataViewColumn(owner
->GetColumn(n
));
152 event
.SetModel(owner
->GetModel());
154 // for events created by wxDataViewHeaderWindow the
155 // row / value fields are not valid
156 return owner
->GetEventHandler()->ProcessEvent(event
);
159 void OnClick(wxHeaderCtrlEvent
& event
)
161 const unsigned idx
= event
.GetColumn();
163 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
166 // default handling for the column click is to sort by this column or
167 // toggle its sort order
168 wxDataViewCtrl
* const owner
= GetOwner();
169 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
170 if ( !col
->IsSortable() )
172 // no default handling for non-sortable columns
177 if ( col
->IsSortKey() )
179 // already using this column for sorting, just change the order
180 col
->ToggleSortOrder();
182 else // not using this column for sorting yet
184 // first unset the old sort column if any
185 int oldSortKey
= owner
->GetSortingColumnIndex();
186 if ( oldSortKey
!= wxNOT_FOUND
)
188 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
189 owner
->OnColumnChange(oldSortKey
);
192 owner
->SetSortingColumnIndex(idx
);
196 wxDataViewModel
* const model
= owner
->GetModel();
200 owner
->OnColumnChange(idx
);
203 void OnRClick(wxHeaderCtrlEvent
& event
)
205 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
210 void OnResize(wxHeaderCtrlEvent
& event
)
212 wxDataViewCtrl
* const owner
= GetOwner();
214 const unsigned col
= event
.GetColumn();
215 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
216 GetOwner()->OnColumnChange(col
);
219 void OnEndReorder(wxHeaderCtrlEvent
& event
)
221 wxDataViewCtrl
* const owner
= GetOwner();
222 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
223 event
.GetNewOrder());
226 DECLARE_EVENT_TABLE()
227 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
);
230 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
231 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
232 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
234 EVT_HEADER_RESIZING(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
235 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnResize
)
237 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
240 //-----------------------------------------------------------------------------
241 // wxDataViewRenameTimer
242 //-----------------------------------------------------------------------------
244 class wxDataViewRenameTimer
: public wxTimer
247 wxDataViewMainWindow
*m_owner
;
250 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
254 //-----------------------------------------------------------------------------
255 // wxDataViewTreeNode
256 //-----------------------------------------------------------------------------
258 class wxDataViewTreeNode
;
259 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
260 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
262 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
263 wxDataViewTreeNode
** node2
);
264 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
266 class wxDataViewTreeNode
269 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
276 m_hasChildren
= false;
280 ~wxDataViewTreeNode()
284 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
285 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
286 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
287 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
289 void AddNode( wxDataViewTreeNode
* node
)
291 m_leaves
.Add( node
->GetItem().GetID() );
293 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
296 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
298 void AddLeaf( void * leaf
)
300 m_leaves
.Add( leaf
);
302 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
305 wxDataViewItem
& GetItem() { return m_item
; }
306 const wxDataViewItem
& GetItem() const { return m_item
; }
307 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
309 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
310 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
311 int GetIndentLevel() const
314 const wxDataViewTreeNode
* node
= this;
315 while( node
->GetParent()->GetParent() != NULL
)
317 node
= node
->GetParent();
330 int len
= m_nodes
.GetCount();
332 for ( int i
= 0;i
< len
; i
++)
333 sum
+= m_nodes
[i
]->GetSubTreeCount();
335 sum
+= m_leaves
.GetCount();
338 ChangeSubTreeCount(-sum
);
344 ChangeSubTreeCount(sum
);
347 bool HasChildren() const { return m_hasChildren
; }
348 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
350 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
351 int GetSubTreeCount() const { return m_subTreeCount
; }
352 void ChangeSubTreeCount( int num
)
356 m_subTreeCount
+= num
;
358 m_parent
->ChangeSubTreeCount(num
);
365 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
366 int len
= m_nodes
.GetCount();
367 for (int i
= 0; i
< len
; i
++)
368 m_nodes
[i
]->Resort();
369 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
374 wxDataViewTreeNode
*m_parent
;
375 wxDataViewTreeNodes m_nodes
;
376 wxDataViewTreeLeaves m_leaves
;
377 wxDataViewItem m_item
;
383 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
,
384 wxDataViewTreeNode
** node2
)
386 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
389 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
391 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
395 //-----------------------------------------------------------------------------
396 // wxDataViewMainWindow
397 //-----------------------------------------------------------------------------
399 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
401 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
402 WX_DEFINE_LIST(ItemList
)
404 class wxDataViewMainWindow
: public wxWindow
407 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
409 const wxPoint
&pos
= wxDefaultPosition
,
410 const wxSize
&size
= wxDefaultSize
,
411 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
412 virtual ~wxDataViewMainWindow();
414 bool IsVirtualList() const { return m_root
== NULL
; }
416 // notifications from wxDataViewModel
417 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
418 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
419 bool ItemChanged( const wxDataViewItem
&item
);
420 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
424 if (!IsVirtualList())
434 g_model
= GetOwner()->GetModel();
435 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
438 if (g_model
->HasDefaultCompare())
446 g_column
= col
->GetModelColumn();
447 g_asending
= col
->IsSortOrderAscending();
450 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
451 wxDataViewCtrl
*GetOwner() { return m_owner
; }
452 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
454 #if wxUSE_DRAG_AND_DROP
455 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
456 #endif // wxUSE_DRAG_AND_DROP
457 void OnPaint( wxPaintEvent
&event
);
458 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
459 void OnChar( wxKeyEvent
&event
);
460 void OnMouse( wxMouseEvent
&event
);
461 void OnSetFocus( wxFocusEvent
&event
);
462 void OnKillFocus( wxFocusEvent
&event
);
464 void UpdateDisplay();
465 void RecalculateDisplay();
466 void OnInternalIdle();
468 void OnRenameTimer();
470 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
471 void ScrollTo( int rows
, int column
);
473 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
474 void ChangeCurrentRow( unsigned int row
);
476 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
477 bool IsEmpty() { return GetRowCount() == 0; }
479 int GetCountPerPage() const;
480 int GetEndOfLastCol() const;
481 unsigned int GetFirstVisibleRow() const;
483 // I change this method to un const because in the tree view,
484 // the displaying number of the tree are changing along with the
485 // expanding/collapsing of the tree nodes
486 unsigned int GetLastVisibleRow();
487 unsigned int GetRowCount();
489 wxDataViewItem
GetSelection() const;
490 wxDataViewSelection
GetSelections(){ return m_selection
; }
491 void SetSelections( const wxDataViewSelection
& sel
)
492 { m_selection
= sel
; UpdateDisplay(); }
493 void Select( const wxArrayInt
& aSelections
);
494 void SelectAllRows( bool on
);
495 void SelectRow( unsigned int row
, bool on
);
496 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
497 void ReverseRowSelection( unsigned int row
);
498 bool IsRowSelected( unsigned int row
);
499 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
501 void RefreshRow( unsigned int row
);
502 void RefreshRows( unsigned int from
, unsigned int to
);
503 void RefreshRowsAfter( unsigned int firstRow
);
505 // returns the colour to be used for drawing the rules
506 wxColour
GetRuleColour() const
508 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
511 wxRect
GetLineRect( unsigned int row
) const;
513 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
514 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
515 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
517 // Some useful functions for row and item mapping
518 wxDataViewItem
GetItemByRow( unsigned int row
) const;
519 int GetRowByItem( const wxDataViewItem
& item
) const;
521 // Methods for building the mapping tree
522 void BuildTree( wxDataViewModel
* model
);
524 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
525 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
527 void Expand( unsigned int row
);
528 void Collapse( unsigned int row
);
529 bool IsExpanded( unsigned int row
) const;
530 bool HasChildren( unsigned int row
) const;
532 #if wxUSE_DRAG_AND_DROP
533 bool EnableDragSource( const wxDataFormat
&format
);
534 bool EnableDropTarget( const wxDataFormat
&format
);
536 void RemoveDropHint();
537 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
538 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
539 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
541 #endif // wxUSE_DRAG_AND_DROP
544 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
545 // We did not need this temporarily
546 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
548 int RecalculateCount();
550 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
552 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
555 wxDataViewCtrl
*m_owner
;
559 wxDataViewColumn
*m_currentCol
;
560 unsigned int m_currentRow
;
561 wxDataViewSelection m_selection
;
563 wxDataViewRenameTimer
*m_renameTimer
;
568 #if wxUSE_DRAG_AND_DROP
573 wxDataFormat m_dragFormat
;
576 wxDataFormat m_dropFormat
;
578 unsigned int m_dropHintLine
;
579 #endif // wxUSE_DRAG_AND_DROP
581 // for double click logic
582 unsigned int m_lineLastClicked
,
583 m_lineBeforeLastClicked
,
584 m_lineSelectSingleOnUp
;
586 // the pen used to draw horiz/vertical rules
589 // the pen used to draw the expander and the lines
592 // This is the tree structure of the model
593 wxDataViewTreeNode
* m_root
;
596 // This is the tree node under the cursor
597 wxDataViewTreeNode
* m_underMouse
;
600 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
601 DECLARE_EVENT_TABLE()
604 // ---------------------------------------------------------
605 // wxGenericDataViewModelNotifier
606 // ---------------------------------------------------------
608 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
611 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
612 { m_mainWindow
= mainWindow
; }
614 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
615 { return m_mainWindow
->ItemAdded( parent
, item
); }
616 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
617 { return m_mainWindow
->ItemDeleted( parent
, item
); }
618 virtual bool ItemChanged( const wxDataViewItem
& item
)
619 { return m_mainWindow
->ItemChanged(item
); }
620 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
621 { return m_mainWindow
->ValueChanged( item
, col
); }
622 virtual bool Cleared()
623 { return m_mainWindow
->Cleared(); }
624 virtual void Resort()
625 { m_mainWindow
->Resort(); }
627 wxDataViewMainWindow
*m_mainWindow
;
630 // ---------------------------------------------------------
631 // wxDataViewRenderer
632 // ---------------------------------------------------------
634 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
636 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
637 wxDataViewCellMode mode
,
639 wxDataViewCustomRendererBase( varianttype
, mode
, align
)
643 m_ellipsizeMode
= wxELLIPSIZE_MIDDLE
;
647 wxDataViewRenderer::~wxDataViewRenderer()
652 wxDC
*wxDataViewRenderer::GetDC()
656 if (GetOwner() == NULL
)
658 if (GetOwner()->GetOwner() == NULL
)
660 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
666 void wxDataViewRenderer::SetAlignment( int align
)
671 int wxDataViewRenderer::GetAlignment() const
676 // ---------------------------------------------------------
677 // wxDataViewCustomRenderer
678 // ---------------------------------------------------------
680 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
682 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
683 wxDataViewCellMode mode
, int align
) :
684 wxDataViewRenderer( varianttype
, mode
, align
)
688 // ---------------------------------------------------------
689 // wxDataViewTextRenderer
690 // ---------------------------------------------------------
692 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewRenderer
)
694 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
695 wxDataViewCellMode mode
, int align
) :
696 wxDataViewRenderer( varianttype
, mode
, align
)
700 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
702 m_text
= value
.GetString();
707 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
712 bool wxDataViewTextRenderer::HasEditorCtrl() const
717 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
718 wxRect labelRect
, const wxVariant
&value
)
720 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, value
,
721 wxPoint(labelRect
.x
,labelRect
.y
),
722 wxSize(labelRect
.width
,labelRect
.height
) );
724 // select the text in the control an place the cursor at the end
725 ctrl
->SetInsertionPointEnd();
731 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
733 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
734 value
= text
->GetValue();
738 bool wxDataViewTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
740 RenderText(m_text
, 0, rect
, dc
, state
);
744 wxSize
wxDataViewTextRenderer::GetSize() const
746 const wxDataViewCtrl
*view
= GetView();
748 return view
->wxWindowBase::GetTextExtent( m_text
);
749 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
752 // ---------------------------------------------------------
753 // wxDataViewBitmapRenderer
754 // ---------------------------------------------------------
756 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewRenderer
)
758 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
759 wxDataViewCellMode mode
, int align
) :
760 wxDataViewRenderer( varianttype
, mode
, align
)
764 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
766 if (value
.GetType() == wxT("wxBitmap"))
768 if (value
.GetType() == wxT("wxIcon"))
774 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
779 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
782 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
783 else if (m_icon
.Ok())
784 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
789 wxSize
wxDataViewBitmapRenderer::GetSize() const
792 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
793 else if (m_icon
.Ok())
794 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
796 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE
,wxDVC_DEFAULT_RENDERER_SIZE
);
799 // ---------------------------------------------------------
800 // wxDataViewToggleRenderer
801 // ---------------------------------------------------------
803 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewRenderer
)
805 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
806 wxDataViewCellMode mode
, int align
) :
807 wxDataViewRenderer( varianttype
, mode
, align
)
812 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
814 m_toggle
= value
.GetBool();
819 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
824 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
828 flags
|= wxCONTROL_CHECKED
;
829 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
830 flags
|= wxCONTROL_DISABLED
;
832 // check boxes we draw must always have the same, standard size (if it's
833 // bigger than the cell size the checkbox will be truncated because the
834 // caller had set the clipping rectangle to prevent us from drawing outside
836 cell
.SetSize(GetSize());
838 wxRendererNative::Get().DrawCheckBox(
839 GetOwner()->GetOwner(),
847 void wxDataViewToggleRenderer::WXOnActivate(wxDataViewModel
*model
,
848 const wxVariant
& valueOld
,
849 const wxDataViewItem
& item
,
852 model
->ChangeValue(!valueOld
.GetBool(), item
, col
);
855 wxSize
wxDataViewToggleRenderer::GetSize() const
857 // the window parameter is not used by GetCheckBoxSize() so it's
859 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
862 // ---------------------------------------------------------
863 // wxDataViewProgressRenderer
864 // ---------------------------------------------------------
866 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewRenderer
)
868 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
869 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
870 wxDataViewRenderer( varianttype
, mode
, align
)
876 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
878 m_value
= (long) value
;
880 if (m_value
< 0) m_value
= 0;
881 if (m_value
> 100) m_value
= 100;
886 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
888 value
= (long) m_value
;
893 wxDataViewProgressRenderer::Render(wxRect rect
, wxDC
*dc
, int WXUNUSED(state
))
895 // deflate the rect to leave a small border between bars in adjacent rows
896 wxRect bar
= rect
.Deflate(0, 1);
898 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
899 dc
->SetPen( *wxBLACK_PEN
);
900 dc
->DrawRectangle( bar
);
902 bar
.width
= (int)(bar
.width
* m_value
/ 100.);
903 dc
->SetPen( *wxTRANSPARENT_PEN
);
905 const wxDataViewItemAttr
& attr
= GetAttr();
906 dc
->SetBrush( attr
.HasColour() ? wxBrush(attr
.GetColour())
908 dc
->DrawRectangle( bar
);
913 wxSize
wxDataViewProgressRenderer::GetSize() const
915 return wxSize(40,12);
918 // ---------------------------------------------------------
919 // wxDataViewDateRenderer
920 // ---------------------------------------------------------
922 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
924 #if wxUSE_DATE_RENDERER_POPUP
926 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
929 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
930 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
931 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
936 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
937 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
938 sizer
->Add( m_cal
, 1, wxGROW
);
943 void OnCalendar( wxCalendarEvent
&event
);
945 wxCalendarCtrl
*m_cal
;
946 wxDataViewModel
*m_model
;
948 const wxDataViewItem
& m_item
;
951 virtual void OnDismiss()
956 DECLARE_EVENT_TABLE()
959 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
960 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
963 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
965 m_model
->ChangeValue( event
.GetDate(), m_item
, m_col
);
969 #endif // wxUSE_DATE_RENDERER_POPUP
971 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewRenderer
)
973 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
974 wxDataViewCellMode mode
, int align
) :
975 wxDataViewRenderer( varianttype
, mode
, align
)
979 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
981 m_date
= value
.GetDateTime();
986 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
992 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
994 wxString tmp
= m_date
.FormatDate();
995 RenderText( tmp
, 0, cell
, dc
, state
);
999 wxSize
wxDataViewDateRenderer::GetSize() const
1001 const wxDataViewCtrl
* view
= GetView();
1002 wxString tmp
= m_date
.FormatDate();
1004 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1005 return wxSize(x
,y
+d
);
1008 void wxDataViewDateRenderer::WXOnActivate(wxDataViewModel
*model
,
1009 const wxVariant
& valueOld
,
1010 const wxDataViewItem
& item
,
1013 wxDateTime dtOld
= valueOld
.GetDateTime();
1015 #if wxUSE_DATE_RENDERER_POPUP
1016 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1017 GetOwner()->GetOwner()->GetParent(), &dtOld
, model
, item
, col
);
1018 wxPoint pos
= wxGetMousePosition();
1021 popup
->Popup( popup
->m_cal
);
1022 #else // !wxUSE_DATE_RENDERER_POPUP
1023 wxMessageBox(dtOld
.Format());
1024 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1027 // ---------------------------------------------------------
1028 // wxDataViewIconTextRenderer
1029 // ---------------------------------------------------------
1031 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewRenderer
)
1033 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1034 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1035 wxDataViewRenderer( varianttype
, mode
, align
)
1038 SetAlignment(align
);
1041 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1047 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1052 bool wxDataViewIconTextRenderer::Render(wxRect rect
, wxDC
*dc
, int state
)
1056 const wxIcon
& icon
= m_value
.GetIcon();
1059 dc
->DrawIcon(icon
, rect
.x
, rect
.y
+ (rect
.height
- icon
.GetHeight())/2);
1060 xoffset
= icon
.GetWidth()+4;
1063 RenderText(m_value
.GetText(), xoffset
, rect
, dc
, state
);
1068 wxSize
wxDataViewIconTextRenderer::GetSize() const
1070 const wxDataViewCtrl
*view
= GetView();
1071 if (!m_value
.GetText().empty())
1074 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1076 if (m_value
.GetIcon().IsOk())
1077 x
+= m_value
.GetIcon().GetWidth() + 4;
1078 return wxSize( x
, y
);
1080 return wxSize(80,20);
1083 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
& value
)
1085 wxDataViewIconText iconText
;
1088 wxString text
= iconText
.GetText();
1090 // adjust the label rect to take the width of the icon into account
1091 if (iconText
.GetIcon().IsOk())
1093 int w
= iconText
.GetIcon().GetWidth() + 4;
1095 labelRect
.width
-= w
;
1098 wxTextCtrl
* ctrl
= new wxTextCtrl( parent
, wxID_ANY
, text
,
1099 wxPoint(labelRect
.x
,labelRect
.y
),
1100 wxSize(labelRect
.width
,labelRect
.height
) );
1102 // select the text in the control an place the cursor at the end
1103 ctrl
->SetInsertionPointEnd();
1109 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
& value
)
1111 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
1113 wxDataViewIconText
iconText(text
->GetValue(), m_value
.GetIcon());
1118 //-----------------------------------------------------------------------------
1119 // wxDataViewDropTarget
1120 //-----------------------------------------------------------------------------
1122 #if wxUSE_DRAG_AND_DROP
1124 class wxBitmapCanvas
: public wxWindow
1127 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1128 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1131 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1134 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1137 dc
.DrawBitmap( m_bitmap
, 0, 0);
1143 class wxDataViewDropSource
: public wxDropSource
1146 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1154 ~wxDataViewDropSource()
1159 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1161 wxPoint pos
= wxGetMousePosition();
1165 int liney
= m_win
->GetLineStart( m_row
);
1167 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1168 m_win
->ClientToScreen( &linex
, &liney
);
1169 m_dist_x
= pos
.x
- linex
;
1170 m_dist_y
= pos
.y
- liney
;
1173 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1175 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1176 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1178 wxFRAME_TOOL_WINDOW
|
1179 wxFRAME_FLOAT_ON_PARENT
|
1180 wxFRAME_NO_TASKBAR
|
1182 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1187 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1188 m_hint
->SetTransparent( 128 );
1194 wxDataViewMainWindow
*m_win
;
1197 int m_dist_x
,m_dist_y
;
1201 class wxDataViewDropTarget
: public wxDropTarget
1204 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1210 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1212 wxDataFormat format
= GetMatchingPair();
1213 if (format
== wxDF_INVALID
)
1215 return m_win
->OnDragOver( format
, x
, y
, def
);
1218 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1220 wxDataFormat format
= GetMatchingPair();
1221 if (format
== wxDF_INVALID
)
1223 return m_win
->OnDrop( format
, x
, y
);
1226 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1228 wxDataFormat format
= GetMatchingPair();
1229 if (format
== wxDF_INVALID
)
1233 return m_win
->OnData( format
, x
, y
, def
);
1236 virtual void OnLeave()
1237 { m_win
->OnLeave(); }
1239 wxDataViewMainWindow
*m_win
;
1242 #endif // wxUSE_DRAG_AND_DROP
1244 //-----------------------------------------------------------------------------
1245 // wxDataViewRenameTimer
1246 //-----------------------------------------------------------------------------
1248 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1253 void wxDataViewRenameTimer::Notify()
1255 m_owner
->OnRenameTimer();
1258 //-----------------------------------------------------------------------------
1259 // wxDataViewMainWindow
1260 //-----------------------------------------------------------------------------
1262 // The tree building helper, declared firstly
1263 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
1264 wxDataViewTreeNode
* node
);
1266 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1268 if (row1
> row2
) return 1;
1269 if (row1
== row2
) return 0;
1273 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1275 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1276 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1277 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1278 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1279 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1280 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1283 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1284 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1285 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1286 m_selection( wxDataViewSelectionCmp
)
1291 m_lastOnSame
= false;
1292 m_renameTimer
= new wxDataViewRenameTimer( this );
1294 // TODO: user better initial values/nothing selected
1295 m_currentCol
= NULL
;
1298 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1300 #if wxUSE_DRAG_AND_DROP
1302 m_dragStart
= wxPoint(0,0);
1304 m_dragEnabled
= false;
1305 m_dropEnabled
= false;
1307 m_dropHintLine
= (unsigned int) -1;
1308 #endif // wxUSE_DRAG_AND_DROP
1310 m_lineLastClicked
= (unsigned int) -1;
1311 m_lineBeforeLastClicked
= (unsigned int) -1;
1312 m_lineSelectSingleOnUp
= (unsigned int) -1;
1316 SetBackgroundColour( *wxWHITE
);
1318 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1320 m_penRule
= wxPen(GetRuleColour());
1322 // compose a pen whichcan draw black lines
1323 // TODO: maybe there is something system colour to use
1324 m_penExpander
= wxPen(wxColour(0,0,0));
1326 m_root
= new wxDataViewTreeNode( NULL
);
1327 m_root
->SetHasChildren(true);
1329 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1331 m_underMouse
= NULL
;
1335 wxDataViewMainWindow::~wxDataViewMainWindow()
1338 delete m_renameTimer
;
1342 #if wxUSE_DRAG_AND_DROP
1343 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1345 m_dragFormat
= format
;
1346 m_dragEnabled
= format
!= wxDF_INVALID
;
1351 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1353 m_dropFormat
= format
;
1354 m_dropEnabled
= format
!= wxDF_INVALID
;
1357 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1362 void wxDataViewMainWindow::RemoveDropHint()
1367 RefreshRow( m_dropHintLine
);
1368 m_dropHintLine
= (unsigned int) -1;
1372 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
,
1373 wxCoord y
, wxDragResult def
)
1377 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1378 unsigned int row
= GetLineAt( yy
);
1380 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1386 wxDataViewItem item
= GetItemByRow( row
);
1388 wxDataViewModel
*model
= GetOwner()->GetModel();
1390 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1391 event
.SetEventObject( m_owner
);
1392 event
.SetItem( item
);
1393 event
.SetModel( model
);
1394 event
.SetDataFormat( format
);
1395 if (!m_owner
->HandleWindowEvent( event
))
1401 if (!event
.IsAllowed())
1408 if (m_dropHint
&& (row
!= m_dropHintLine
))
1409 RefreshRow( m_dropHintLine
);
1411 m_dropHintLine
= row
;
1417 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1423 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1424 unsigned int row
= GetLineAt( yy
);
1426 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1429 wxDataViewItem item
= GetItemByRow( row
);
1431 wxDataViewModel
*model
= GetOwner()->GetModel();
1433 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1434 event
.SetEventObject( m_owner
);
1435 event
.SetItem( item
);
1436 event
.SetModel( model
);
1437 event
.SetDataFormat( format
);
1438 if (!m_owner
->HandleWindowEvent( event
))
1441 if (!event
.IsAllowed())
1447 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
,
1452 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1453 unsigned int row
= GetLineAt( yy
);
1455 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1458 wxDataViewItem item
= GetItemByRow( row
);
1460 wxDataViewModel
*model
= GetOwner()->GetModel();
1462 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1464 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1465 event
.SetEventObject( m_owner
);
1466 event
.SetItem( item
);
1467 event
.SetModel( model
);
1468 event
.SetDataFormat( format
);
1469 event
.SetDataSize( obj
->GetSize() );
1470 event
.SetDataBuffer( obj
->GetData() );
1471 if (!m_owner
->HandleWindowEvent( event
))
1474 if (!event
.IsAllowed())
1480 void wxDataViewMainWindow::OnLeave()
1485 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1487 int height
= GetLineHeight( row
);
1489 unsigned int cols
= GetOwner()->GetColumnCount();
1491 for (col
= 0; col
< cols
; col
++)
1493 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1494 if (column
->IsHidden())
1495 continue; // skip it!
1496 width
+= column
->GetWidth();
1500 if (!IsVirtualList())
1502 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1503 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1504 indent
= indent
+ m_lineHeight
;
1505 // try to use the m_lineHeight as the expander space
1507 if(!node
->HasChildren())
1512 wxBitmap
bitmap( width
, height
);
1513 wxMemoryDC
dc( bitmap
);
1514 dc
.SetFont( GetFont() );
1515 dc
.SetPen( *wxBLACK_PEN
);
1516 dc
.SetBrush( *wxWHITE_BRUSH
);
1517 dc
.DrawRectangle( 0,0,width
,height
);
1519 wxDataViewModel
*model
= m_owner
->GetModel();
1521 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1524 // TODO-RTL: last column for RTL support
1525 expander
= GetOwner()->GetColumnAt( 0 );
1526 GetOwner()->SetExpanderColumn(expander
);
1531 for (col
= 0; col
< cols
; col
++)
1533 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1534 wxDataViewRenderer
*cell
= column
->GetRenderer();
1536 if (column
->IsHidden())
1537 continue; // skip it!
1539 width
= column
->GetWidth();
1541 if (column
== expander
)
1545 wxDataViewItem item
= GetItemByRow( row
);
1546 model
->GetValue( value
, item
, column
->GetModelColumn());
1547 cell
->SetValue( value
);
1549 wxDataViewItemAttr attr
;
1550 model
->GetAttr(item
, column
->GetModelColumn(), attr
);
1551 cell
->SetAttr(attr
);
1553 wxRect
item_rect(x
, 0, width
, height
);
1554 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1556 // dc.SetClippingRegion( item_rect );
1557 cell
->WXCallRender(item_rect
, &dc
, 0);
1558 // dc.DestroyClippingRegion();
1566 #endif // wxUSE_DRAG_AND_DROP
1569 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1571 wxDataViewModel
*model
= GetOwner()->GetModel();
1572 wxAutoBufferedPaintDC
dc( this );
1575 dc
.SetBrush(GetOwner()->GetBackgroundColour());
1576 dc
.SetPen( *wxTRANSPARENT_PEN
);
1577 dc
.DrawRectangle(GetClientSize());
1581 GetOwner()->PrepareDC( dc
);
1582 dc
.SetFont( GetFont() );
1584 wxRect update
= GetUpdateRegion().GetBox();
1585 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1587 // compute which items needs to be redrawn
1588 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1589 unsigned int item_count
=
1590 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1591 (int)(GetRowCount( ) - item_start
));
1592 unsigned int item_last
= item_start
+ item_count
;
1593 // Get the parent of DataViewCtrl
1594 wxWindow
*parent
= GetParent()->GetParent();
1595 wxDataViewEvent
cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, parent
->GetId());
1596 cache_event
.SetEventObject(GetParent());
1597 cache_event
.SetCache(item_start
, item_last
- 1);
1598 parent
->ProcessWindowEvent(cache_event
);
1600 // compute which columns needs to be redrawn
1601 unsigned int cols
= GetOwner()->GetColumnCount();
1604 // we assume that we have at least one column below and painting an
1605 // empty control is unnecessary anyhow
1609 unsigned int col_start
= 0;
1610 unsigned int x_start
;
1611 for (x_start
= 0; col_start
< cols
; col_start
++)
1613 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1614 if (col
->IsHidden())
1615 continue; // skip it!
1617 unsigned int w
= col
->GetWidth();
1618 if (x_start
+w
>= (unsigned int)update
.x
)
1624 unsigned int col_last
= col_start
;
1625 unsigned int x_last
= x_start
;
1626 for (; col_last
< cols
; col_last
++)
1628 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1629 if (col
->IsHidden())
1630 continue; // skip it!
1632 if (x_last
> (unsigned int)update
.GetRight())
1635 x_last
+= col
->GetWidth();
1638 // Draw horizontal rules if required
1639 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1641 dc
.SetPen(m_penRule
);
1642 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1644 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1646 int y
= GetLineStart( i
);
1647 dc
.DrawLine(x_start
, y
, x_last
, y
);
1651 // Draw vertical rules if required
1652 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1654 dc
.SetPen(m_penRule
);
1655 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1658 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1660 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1661 if (col
->IsHidden())
1662 continue; // skip it
1664 dc
.DrawLine(x
, GetLineStart( item_start
),
1665 x
, GetLineStart( item_last
) );
1667 x
+= col
->GetWidth();
1670 // Draw last vertical rule
1671 dc
.DrawLine(x
, GetLineStart( item_start
),
1672 x
, GetLineStart( item_last
) );
1675 // redraw the background for the items which are selected/current
1676 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1678 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1679 if (selected
|| item
== m_currentRow
)
1681 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1682 if (item
== m_currentRow
)
1683 flags
|= wxCONTROL_CURRENT
;
1685 flags
|= wxCONTROL_FOCUSED
;
1687 wxRect
rect( x_start
, GetLineStart( item
),
1688 x_last
- x_start
, GetLineHeight( item
) );
1689 wxRendererNative::Get().DrawItemSelectionRect
1699 #if wxUSE_DRAG_AND_DROP
1702 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1703 x_last
- x_start
, GetLineHeight( m_dropHintLine
) );
1704 dc
.SetPen( *wxBLACK_PEN
);
1705 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1706 dc
.DrawRectangle( rect
);
1708 #endif // wxUSE_DRAG_AND_DROP
1710 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1713 // TODO-RTL: last column for RTL support
1714 expander
= GetOwner()->GetColumnAt( 0 );
1715 GetOwner()->SetExpanderColumn(expander
);
1718 // redraw all cells for all rows which must be repainted and all columns
1720 cell_rect
.x
= x_start
;
1721 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1723 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1724 wxDataViewRenderer
*cell
= col
->GetRenderer();
1725 cell_rect
.width
= col
->GetWidth();
1727 if ( col
->IsHidden() || cell_rect
.width
<= 0 )
1728 continue; // skip it!
1730 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1732 // get the cell value and set it into the renderer
1734 wxDataViewTreeNode
*node
= NULL
;
1735 wxDataViewItem dataitem
;
1737 if (!IsVirtualList())
1739 node
= GetTreeNodeByRow(item
);
1743 dataitem
= node
->GetItem();
1745 if ((i
> 0) && model
->IsContainer(dataitem
) &&
1746 !model
->HasContainerColumns(dataitem
))
1751 dataitem
= wxDataViewItem( wxUIntToPtr(item
+1) );
1754 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1755 cell
->SetValue( value
);
1757 wxDataViewItemAttr attr
;
1758 model
->GetAttr(dataitem
, col
->GetModelColumn(), attr
);
1759 cell
->SetAttr(attr
);
1762 cell_rect
.y
= GetLineStart( item
);
1763 cell_rect
.height
= GetLineHeight( item
);
1765 // deal with the expander
1767 if ((!IsVirtualList()) && (col
== expander
))
1769 // Calculate the indent first
1770 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1772 // we reserve m_lineHeight of horizontal space for the expander
1773 // but leave EXPANDER_MARGIN around the expander itself
1774 int exp_x
= cell_rect
.x
+ indent
+ EXPANDER_MARGIN
;
1776 indent
+= m_lineHeight
;
1778 // draw expander if needed and visible
1779 if ( node
->HasChildren() && exp_x
< cell_rect
.GetRight() )
1781 dc
.SetPen( m_penExpander
);
1782 dc
.SetBrush( wxNullBrush
);
1784 int exp_size
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1785 int exp_y
= cell_rect
.y
+ (cell_rect
.height
- exp_size
)/2
1786 + EXPANDER_MARGIN
- EXPANDER_OFFSET
;
1788 const wxRect
rect(exp_x
, exp_y
, exp_size
, exp_size
);
1791 if ( m_underMouse
== node
)
1792 flag
|= wxCONTROL_CURRENT
;
1793 if ( node
->IsOpen() )
1794 flag
|= wxCONTROL_EXPANDED
;
1796 // ensure that we don't overflow the cell (which might
1797 // happen if the column is very narrow)
1798 wxDCClipper
clip(dc
, cell_rect
);
1800 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1803 // force the expander column to left-center align
1804 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1806 if (node
&& !node
->HasChildren())
1808 // Yes, if the node does not have any child, it must be a leaf which
1809 // mean that it is a temporarily created by GetTreeNodeByRow
1813 wxRect item_rect
= cell_rect
;
1814 item_rect
.Deflate(PADDING_RIGHTLEFT
, 0);
1816 // account for the tree indent (harmless if we're not indented)
1817 item_rect
.x
+= indent
;
1818 item_rect
.width
-= indent
;
1820 if ( item_rect
.width
<= 0 )
1824 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1825 state
|= wxDATAVIEW_CELL_SELECTED
;
1827 // TODO: it would be much more efficient to create a clipping
1828 // region for the entire column being rendered (in the OnPaint
1829 // of wxDataViewMainWindow) instead of a single clip region for
1830 // each cell. However it would mean that each renderer should
1831 // respect the given wxRect's top & bottom coords, eventually
1832 // violating only the left & right coords - however the user can
1833 // make its own renderer and thus we cannot be sure of that.
1834 wxDCClipper
clip(dc
, item_rect
);
1836 cell
->WXCallRender(item_rect
, &dc
, state
);
1839 cell_rect
.x
+= cell_rect
.width
;
1843 void wxDataViewMainWindow::OnRenameTimer()
1845 // We have to call this here because changes may just have
1846 // been made and no screen update taken place.
1849 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1850 // (needs to be tested!)
1854 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1856 wxRect labelRect
= GetItemRect(item
, m_currentCol
);
1858 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1861 //-----------------------------------------------------------------------------
1862 // Helper class for do operation on the tree node
1863 //-----------------------------------------------------------------------------
1868 virtual ~DoJob() { }
1870 // The return value control how the tree-walker tranverse the tree
1871 // 0: Job done, stop tranverse and return
1872 // 1: Ignore the current node's subtree and continue
1873 // 2: Job not done, continue
1874 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1875 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1876 virtual int operator() ( void * n
) = 0;
1879 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1884 switch( func( node
) )
1895 wxDataViewTreeNodes nodes
= node
->GetNodes();
1896 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1898 int len_nodes
= nodes
.GetCount();
1899 int len
= leaves
.GetCount();
1900 int i
= 0, nodes_i
= 0;
1902 for(; i
< len
; i
++ )
1904 void * n
= leaves
[i
];
1905 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1907 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1910 if( Walker( nd
, func
) )
1929 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1931 if (IsVirtualList())
1933 wxDataViewVirtualListModel
*list_model
=
1934 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1935 m_count
= list_model
->GetCount();
1942 wxDataViewTreeNode
* node
;
1943 node
= FindNode(parent
);
1948 node
->SetHasChildren( true );
1950 if( g_model
->IsContainer( item
) )
1952 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1953 newnode
->SetItem(item
);
1954 newnode
->SetHasChildren( true );
1955 node
->AddNode( newnode
);
1958 node
->AddLeaf( item
.GetID() );
1960 node
->ChangeSubTreeCount(1);
1968 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1970 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1971 const wxDataViewItem
& item
)
1973 if (IsVirtualList())
1975 wxDataViewVirtualListModel
*list_model
=
1976 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
1977 m_count
= list_model
->GetCount();
1979 if( m_currentRow
> GetRowCount() )
1980 m_currentRow
= m_count
- 1;
1982 // TODO: why empty the entire selection?
1983 m_selection
.Empty();
1990 wxDataViewTreeNode
* node
= FindNode(parent
);
1992 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
1993 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
,
1994 false, "item not found" );
1997 node
->GetChildren().Remove( item
.GetID() );
1998 // Manipolate selection
1999 if( m_selection
.GetCount() > 1 )
2001 m_selection
.Empty();
2003 bool isContainer
= false;
2004 wxDataViewTreeNodes nds
= node
->GetNodes();
2005 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2007 if (nds
[i
]->GetItem() == item
)
2015 wxDataViewTreeNode
* n
= NULL
;
2016 wxDataViewTreeNodes nodes
= node
->GetNodes();
2017 int len
= nodes
.GetCount();
2018 for( int i
= 0; i
< len
; i
++)
2020 if( nodes
[i
]->GetItem() == item
)
2027 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2029 node
->GetNodes().Remove( n
);
2030 sub
-= n
->GetSubTreeCount();
2031 ::DestroyTreeHelper(n
);
2033 // Make the row number invalid and get a new valid one when user call GetRowCount
2035 node
->ChangeSubTreeCount(sub
);
2037 // Change the current row to the last row if the current exceed the max row number
2038 if( m_currentRow
> GetRowCount() )
2039 m_currentRow
= m_count
- 1;
2046 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2052 wxWindow
*parent
= GetParent();
2053 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2054 le
.SetEventObject(parent
);
2055 le
.SetModel(GetOwner()->GetModel());
2057 parent
->GetEventHandler()->ProcessEvent(le
);
2062 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2064 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2065 /*#define MAX_VIRTUAL_WIDTH 100000
2067 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2068 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2069 Refresh( true, &rect );
2077 wxWindow
*parent
= GetParent();
2078 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2079 le
.SetEventObject(parent
);
2080 le
.SetModel(GetOwner()->GetModel());
2083 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2084 parent
->GetEventHandler()->ProcessEvent(le
);
2089 bool wxDataViewMainWindow::Cleared()
2094 BuildTree( GetOwner()->GetModel() );
2101 void wxDataViewMainWindow::UpdateDisplay()
2104 m_underMouse
= NULL
;
2107 void wxDataViewMainWindow::OnInternalIdle()
2109 wxWindow::OnInternalIdle();
2113 RecalculateDisplay();
2118 void wxDataViewMainWindow::RecalculateDisplay()
2120 wxDataViewModel
*model
= GetOwner()->GetModel();
2127 int width
= GetEndOfLastCol();
2128 int height
= GetLineStart( GetRowCount() );
2130 SetVirtualSize( width
, height
);
2131 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2136 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2138 wxWindow::ScrollWindow( dx
, dy
, rect
);
2140 if (GetOwner()->m_headerArea
)
2141 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2144 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2147 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2148 int sy
= GetLineStart( rows
)/y
;
2152 wxRect rect
= GetClientRect();
2156 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2157 for (x_start
= 0; colnum
< column
; colnum
++)
2159 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2160 if (col
->IsHidden())
2161 continue; // skip it!
2163 w
= col
->GetWidth();
2167 int x_end
= x_start
+ w
;
2168 xe
= xx
+ rect
.width
;
2171 sx
= ( xx
+ x_end
- xe
)/x
;
2178 m_owner
->Scroll( sx
, sy
);
2181 int wxDataViewMainWindow::GetCountPerPage() const
2183 wxSize size
= GetClientSize();
2184 return size
.y
/ m_lineHeight
;
2187 int wxDataViewMainWindow::GetEndOfLastCol() const
2191 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2193 const wxDataViewColumn
*c
=
2194 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2197 width
+= c
->GetWidth();
2202 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2206 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2208 return GetLineAt( y
);
2211 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2213 wxSize client_size
= GetClientSize();
2214 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2215 &client_size
.x
, &client_size
.y
);
2217 // we should deal with the pixel here
2218 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2220 return wxMin( GetRowCount()-1, row
);
2223 unsigned int wxDataViewMainWindow::GetRowCount()
2225 if ( m_count
== -1 )
2227 m_count
= RecalculateCount();
2233 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2240 void wxDataViewMainWindow::SelectAllRows( bool on
)
2247 m_selection
.Clear();
2248 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2249 m_selection
.Add( i
);
2254 unsigned int first_visible
= GetFirstVisibleRow();
2255 unsigned int last_visible
= GetLastVisibleRow();
2257 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2259 unsigned int row
= m_selection
[i
];
2260 if ((row
>= first_visible
) && (row
<= last_visible
))
2263 m_selection
.Clear();
2267 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2269 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2273 m_selection
.Add( row
);
2281 m_selection
.Remove( row
);
2287 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2291 unsigned int tmp
= from
;
2297 for (i
= from
; i
<= to
; i
++)
2299 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2302 m_selection
.Add( i
);
2307 m_selection
.Remove( i
);
2310 RefreshRows( from
, to
);
2313 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2315 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2317 int n
= aSelections
[i
];
2319 m_selection
.Add( n
);
2324 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2326 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2327 m_selection
.Add( row
);
2329 m_selection
.Remove( row
);
2333 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2335 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2338 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2340 wxWindow
*parent
= GetParent();
2341 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2343 le
.SetEventObject(parent
);
2344 le
.SetModel(GetOwner()->GetModel());
2347 parent
->GetEventHandler()->ProcessEvent(le
);
2350 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2352 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2353 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2355 wxSize client_size
= GetClientSize();
2356 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2357 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2358 if (intersect_rect
.width
> 0)
2359 Refresh( true, &intersect_rect
);
2362 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2366 unsigned int tmp
= to
;
2371 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2372 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2374 wxSize client_size
= GetClientSize();
2375 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2376 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2377 if (intersect_rect
.width
> 0)
2378 Refresh( true, &intersect_rect
);
2381 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2383 wxSize client_size
= GetClientSize();
2384 int start
= GetLineStart( firstRow
);
2385 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2386 if (start
> client_size
.y
) return;
2388 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2390 Refresh( true, &rect
);
2393 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2395 wxCHECK_RET( newCurrent
< GetRowCount(),
2396 wxT("invalid item index in OnArrowChar()") );
2398 // if there is no selection, we cannot move it anywhere
2399 if (!HasCurrentRow())
2402 unsigned int oldCurrent
= m_currentRow
;
2404 // in single selection we just ignore Shift as we can't select several
2406 if ( event
.ShiftDown() && !IsSingleSel() )
2408 RefreshRow( oldCurrent
);
2410 ChangeCurrentRow( newCurrent
);
2412 // select all the items between the old and the new one
2413 if ( oldCurrent
> newCurrent
)
2415 newCurrent
= oldCurrent
;
2416 oldCurrent
= m_currentRow
;
2419 SelectRows( oldCurrent
, newCurrent
, true );
2420 if (oldCurrent
!=newCurrent
)
2421 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2425 RefreshRow( oldCurrent
);
2427 // all previously selected items are unselected unless ctrl is held
2428 if ( !event
.ControlDown() )
2429 SelectAllRows(false);
2431 ChangeCurrentRow( newCurrent
);
2433 if ( !event
.ControlDown() )
2435 SelectRow( m_currentRow
, true );
2436 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2439 RefreshRow( m_currentRow
);
2442 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2445 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2449 rect
.y
= GetLineStart( row
);
2450 rect
.width
= GetEndOfLastCol();
2451 rect
.height
= GetLineHeight( row
);
2456 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2458 const wxDataViewModel
*model
= GetOwner()->GetModel();
2460 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2462 // TODO make more efficient
2467 for (r
= 0; r
< row
; r
++)
2469 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2470 if (!node
) return start
;
2472 wxDataViewItem item
= node
->GetItem();
2474 if (node
&& !node
->HasChildren())
2476 // Yes, if the node does not have any child, it must be a leaf which
2477 // mean that it is a temporarily created by GetTreeNodeByRow
2481 unsigned int cols
= GetOwner()->GetColumnCount();
2483 int height
= m_lineHeight
;
2484 for (col
= 0; col
< cols
; col
++)
2486 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2487 if (column
->IsHidden())
2488 continue; // skip it!
2491 model
->IsContainer(item
) &&
2492 !model
->HasContainerColumns(item
))
2493 continue; // skip it!
2496 model
->GetValue( value
, item
, column
->GetModelColumn() );
2498 wxDataViewRenderer
*renderer
=
2499 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2500 renderer
->SetValue( value
);
2501 height
= wxMax( height
, renderer
->GetSize().y
);
2511 return row
* m_lineHeight
;
2515 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2517 const wxDataViewModel
*model
= GetOwner()->GetModel();
2519 // check for the easy case first
2520 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2521 return y
/ m_lineHeight
;
2523 // TODO make more efficient
2524 unsigned int row
= 0;
2525 unsigned int yy
= 0;
2528 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2531 // not really correct...
2532 return row
+ ((y
-yy
) / m_lineHeight
);
2535 wxDataViewItem item
= node
->GetItem();
2537 if (node
&& !node
->HasChildren())
2539 // Yes, if the node does not have any child, it must be a leaf which
2540 // mean that it is a temporarily created by GetTreeNodeByRow
2544 unsigned int cols
= GetOwner()->GetColumnCount();
2546 int height
= m_lineHeight
;
2547 for (col
= 0; col
< cols
; col
++)
2549 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2550 if (column
->IsHidden())
2551 continue; // skip it!
2554 model
->IsContainer(item
) &&
2555 !model
->HasContainerColumns(item
))
2556 continue; // skip it!
2559 model
->GetValue( value
, item
, column
->GetModelColumn() );
2561 wxDataViewRenderer
*renderer
=
2562 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2563 renderer
->SetValue( value
);
2564 height
= wxMax( height
, renderer
->GetSize().y
);
2575 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2577 const wxDataViewModel
*model
= GetOwner()->GetModel();
2579 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2581 wxASSERT( !IsVirtualList() );
2583 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2584 // wxASSERT( node );
2585 if (!node
) return m_lineHeight
;
2587 wxDataViewItem item
= node
->GetItem();
2589 if (node
&& !node
->HasChildren())
2591 // Yes, if the node does not have any child, it must be a leaf which
2592 // mean that it is a temporarily created by GetTreeNodeByRow
2596 int height
= m_lineHeight
;
2598 unsigned int cols
= GetOwner()->GetColumnCount();
2600 for (col
= 0; col
< cols
; col
++)
2602 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2603 if (column
->IsHidden())
2604 continue; // skip it!
2607 model
->IsContainer(item
) &&
2608 !model
->HasContainerColumns(item
))
2609 continue; // skip it!
2612 model
->GetValue( value
, item
, column
->GetModelColumn() );
2614 wxDataViewRenderer
*renderer
=
2615 const_cast<wxDataViewRenderer
*>(column
->GetRenderer());
2616 renderer
->SetValue( value
);
2617 height
= wxMax( height
, renderer
->GetSize().y
);
2624 return m_lineHeight
;
2628 class RowToItemJob
: public DoJob
2631 RowToItemJob( unsigned int row
, int current
)
2632 { this->row
= row
; this->current
= current
; }
2633 virtual ~RowToItemJob() {}
2635 virtual int operator() ( wxDataViewTreeNode
* node
)
2638 if( current
== static_cast<int>(row
))
2640 ret
= node
->GetItem();
2644 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2646 current
+= node
->GetSubTreeCount();
2651 // If the current has no child node, we can find the desired item of the row
2653 // This if can speed up finding in some case, and will has a very good effect
2654 // when it comes to list view
2655 if( node
->GetNodes().GetCount() == 0)
2657 int index
= static_cast<int>(row
) - current
- 1;
2658 ret
= node
->GetChildren().Item( index
);
2665 virtual int operator() ( void * n
)
2668 if( current
== static_cast<int>(row
))
2670 ret
= wxDataViewItem( n
);
2676 wxDataViewItem
GetResult() const
2685 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2687 if (IsVirtualList())
2689 return wxDataViewItem( wxUIntToPtr(row
+1) );
2693 RowToItemJob
job( row
, -2 );
2694 Walker( m_root
, job
);
2695 return job
.GetResult();
2699 class RowToTreeNodeJob
: public DoJob
2702 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2705 this->current
= current
;
2709 virtual ~RowToTreeNodeJob(){ }
2711 virtual int operator() ( wxDataViewTreeNode
* node
)
2714 if( current
== static_cast<int>(row
))
2720 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2722 current
+= node
->GetSubTreeCount();
2729 // If the current node has no children, we can find the desired item of the
2730 // row number directly.
2731 // This if can speed up finding in some case, and will have a very good
2732 // effect for list views.
2733 if( node
->GetNodes().GetCount() == 0)
2735 int index
= static_cast<int>(row
) - current
- 1;
2736 void * n
= node
->GetChildren().Item( index
);
2737 ret
= new wxDataViewTreeNode( parent
);
2738 ret
->SetItem( wxDataViewItem( n
));
2739 ret
->SetHasChildren(false);
2746 virtual int operator() ( void * n
)
2749 if( current
== static_cast<int>(row
))
2751 ret
= new wxDataViewTreeNode( parent
);
2752 ret
->SetItem( wxDataViewItem( n
));
2753 ret
->SetHasChildren(false);
2760 wxDataViewTreeNode
* GetResult() const
2766 wxDataViewTreeNode
* ret
;
2767 wxDataViewTreeNode
* parent
;
2770 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2772 wxASSERT( !IsVirtualList() );
2774 RowToTreeNodeJob
job( row
, -2, m_root
);
2775 Walker( m_root
, job
);
2776 return job
.GetResult();
2779 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
,
2780 const wxDataViewItem
& item
)
2782 wxWindow
*parent
= GetParent();
2783 wxDataViewEvent
le(type
, parent
->GetId());
2785 le
.SetEventObject(parent
);
2786 le
.SetModel(GetOwner()->GetModel());
2789 parent
->GetEventHandler()->ProcessEvent(le
);
2793 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2795 if (IsVirtualList())
2798 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2802 if (!node
->HasChildren())
2808 return node
->IsOpen();
2811 bool wxDataViewMainWindow::HasChildren( unsigned int row
) const
2813 if (IsVirtualList())
2816 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2820 if (!node
->HasChildren())
2829 void wxDataViewMainWindow::Expand( unsigned int row
)
2831 if (IsVirtualList())
2834 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2838 if (!node
->HasChildren())
2844 if (!node
->IsOpen())
2847 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, node
->GetItem());
2849 // Check if the user prevent expanding
2850 if( e
.GetSkipped() )
2855 // build the children of current node
2856 if( node
->GetChildrenNumber() == 0 )
2859 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2862 // By expanding the node all row indices that are currently in the selection list
2863 // and are greater than our node have become invalid. So we have to correct that now.
2864 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2865 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2867 const unsigned testRow
= m_selection
[i
];
2868 // all rows above us are not affected, so skip them
2872 m_selection
[i
] += rowAdjustment
;
2875 if(m_currentRow
> row
)
2876 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2880 // Send the expanded event
2881 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2885 void wxDataViewMainWindow::Collapse(unsigned int row
)
2887 if (IsVirtualList())
2890 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
2894 if (!node
->HasChildren())
2903 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2904 if( e
.GetSkipped() )
2907 // Find out if there are selected items below the current node.
2908 bool selectCollapsingRow
= false;
2909 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2910 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2911 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2913 const unsigned testRow
= m_selection
[i
];
2914 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2916 selectCollapsingRow
= true;
2917 // get out as soon as we have found a node that is selected
2924 // If the node to be closed has selected items the user won't see those any longer.
2925 // We select the collapsing node in this case.
2926 if(selectCollapsingRow
)
2928 SelectAllRows(false);
2929 ChangeCurrentRow(row
);
2930 SelectRow(row
, true);
2931 SendSelectionChangedEvent(GetItemByRow(row
));
2935 // if there were no selected items below our node we still need to "fix" the
2936 // selection list to adjust for the changing of the row indices.
2937 // We actually do the opposite of what we are doing in Expand().
2938 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2940 const unsigned testRow
= m_selection
[i
];
2941 // all rows above us are not affected, so skip them
2945 m_selection
[i
] -= rowAdjustment
;
2948 // if the "current row" is being collapsed away we change it to the current row ;-)
2949 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2950 ChangeCurrentRow(row
);
2951 else if(m_currentRow
> row
)
2952 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2957 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,node
->GetItem());
2961 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2963 wxDataViewModel
* model
= GetOwner()->GetModel();
2970 // Compose the a parent-chain of the finding item
2972 list
.DeleteContents( true );
2973 wxDataViewItem
it( item
);
2976 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2977 list
.Insert( pItem
);
2978 it
= model
->GetParent( it
);
2981 // Find the item along the parent-chain.
2982 // This algorithm is designed to speed up the node-finding method
2983 wxDataViewTreeNode
* node
= m_root
;
2984 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
2986 if( node
->HasChildren() )
2988 if( node
->GetChildrenNumber() == 0 )
2991 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2994 wxDataViewTreeNodes nodes
= node
->GetNodes();
2998 for (i
= 0; i
< nodes
.GetCount(); i
++)
3000 if (nodes
[i
]->GetItem() == (**iter
))
3002 if (nodes
[i
]->GetItem() == item
)
3019 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
3020 wxDataViewColumn
* &column
)
3022 wxDataViewColumn
*col
= NULL
;
3023 unsigned int cols
= GetOwner()->GetColumnCount();
3024 unsigned int colnum
= 0;
3026 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3027 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3029 col
= GetOwner()->GetColumnAt(colnum
);
3030 if (col
->IsHidden())
3031 continue; // skip it!
3033 unsigned int w
= col
->GetWidth();
3034 if (x_start
+w
>= (unsigned int)x
)
3041 item
= GetItemByRow( GetLineAt( y
) );
3044 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
,
3045 const wxDataViewColumn
* column
)
3050 unsigned int cols
= GetOwner()->GetColumnCount();
3051 // If column is null the loop will compute the combined width of all columns.
3052 // Otherwise, it will compute the x position of the column we are looking for.
3053 for (unsigned int i
= 0; i
< cols
; i
++)
3055 wxDataViewColumn
* col
= GetOwner()->GetColumnAt( i
);
3060 if (col
->IsHidden())
3061 continue; // skip it!
3063 xpos
+= col
->GetWidth();
3064 width
+= col
->GetWidth();
3069 // If we have a column, we need can get its width directly.
3070 if(column
->IsHidden())
3073 width
= column
->GetWidth();
3078 // If we have no column, we reset the x position back to zero.
3082 // we have to take an expander column into account and compute its indentation
3083 // to get the correct x position where the actual text is
3085 int row
= GetRowByItem(item
);
3086 if (!IsVirtualList() && (column
== 0 || GetOwner()->GetExpanderColumn() == column
) )
3088 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3089 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
3090 indent
= indent
+ m_lineHeight
; // use m_lineHeight as the width of the expander
3092 if(!node
->HasChildren())
3096 wxRect
itemRect( xpos
+ indent
,
3097 GetLineStart( row
),
3099 GetLineHeight( row
) );
3101 GetOwner()->CalcScrolledPosition( itemRect
.x
, itemRect
.y
,
3102 &itemRect
.x
, &itemRect
.y
);
3107 int wxDataViewMainWindow::RecalculateCount()
3109 if (IsVirtualList())
3111 wxDataViewVirtualListModel
*list_model
=
3112 (wxDataViewVirtualListModel
*) GetOwner()->GetModel();
3114 return list_model
->GetCount();
3118 return m_root
->GetSubTreeCount();
3122 class ItemToRowJob
: public DoJob
3125 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3132 // Maybe binary search will help to speed up this process
3133 virtual int operator() ( wxDataViewTreeNode
* node
)
3136 if( node
->GetItem() == item
)
3141 if( node
->GetItem() == **m_iter
)
3148 ret
+= node
->GetSubTreeCount();
3154 virtual int operator() ( void * n
)
3157 if( n
== item
.GetID() )
3162 // the row number is begin from zero
3163 int GetResult() const
3167 ItemList::const_iterator m_iter
;
3168 wxDataViewItem item
;
3173 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3175 const wxDataViewModel
* model
= GetOwner()->GetModel();
3179 if (IsVirtualList())
3181 return wxPtrToUInt( item
.GetID() ) -1;
3188 // Compose the a parent-chain of the finding item
3190 wxDataViewItem
* pItem
;
3191 list
.DeleteContents( true );
3192 wxDataViewItem
it( item
);
3195 pItem
= new wxDataViewItem( it
);
3196 list
.Insert( pItem
);
3197 it
= model
->GetParent( it
);
3199 pItem
= new wxDataViewItem( );
3200 list
.Insert( pItem
);
3202 ItemToRowJob
job( item
, list
.begin() );
3203 Walker(m_root
, job
);
3204 return job
.GetResult();
3208 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
,
3209 wxDataViewTreeNode
* node
)
3211 if( !model
->IsContainer( item
) )
3214 wxDataViewItemArray children
;
3215 unsigned int num
= model
->GetChildren( item
, children
);
3217 unsigned int index
= 0;
3218 while( index
< num
)
3220 if( model
->IsContainer( children
[index
] ) )
3222 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3223 n
->SetItem(children
[index
]);
3224 n
->SetHasChildren( true );
3229 node
->AddLeaf( children
[index
].GetID() );
3233 node
->SetSubTreeCount( num
);
3234 wxDataViewTreeNode
* n
= node
->GetParent();
3236 n
->ChangeSubTreeCount(num
);
3240 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3244 if (GetOwner()->GetModel()->IsVirtualListModel())
3250 m_root
= new wxDataViewTreeNode( NULL
);
3251 m_root
->SetHasChildren(true);
3253 // First we define a invalid item to fetch the top-level elements
3254 wxDataViewItem item
;
3256 BuildTreeHelper( model
, item
, m_root
);
3260 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3262 if( node
->GetNodeNumber() != 0 )
3264 int len
= node
->GetNodeNumber();
3265 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3266 for (int i
= 0; i
< len
; i
++)
3267 DestroyTreeHelper(nodes
[i
]);
3272 void wxDataViewMainWindow::DestroyTree()
3274 if (!IsVirtualList())
3276 ::DestroyTreeHelper(m_root
);
3282 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3284 wxWindow
* const parent
= GetParent();
3286 // propagate the char event upwards
3287 wxKeyEvent
eventForParent(event
);
3288 eventForParent
.SetEventObject(parent
);
3289 if ( parent
->ProcessWindowEvent(eventForParent
) )
3292 if ( parent
->HandleAsNavigationKey(event
) )
3295 // no item -> nothing to do
3296 if (!HasCurrentRow())
3302 // don't use m_linesPerPage directly as it might not be computed yet
3303 const int pageSize
= GetCountPerPage();
3304 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
3306 switch ( event
.GetKeyCode() )
3310 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
,
3312 le
.SetItem( GetItemByRow(m_currentRow
) );
3313 le
.SetEventObject(parent
);
3314 le
.SetModel(GetOwner()->GetModel());
3316 parent
->GetEventHandler()->ProcessEvent(le
);
3321 if ( m_currentRow
> 0 )
3322 OnArrowChar( m_currentRow
- 1, event
);
3326 if ( m_currentRow
< GetRowCount() - 1 )
3327 OnArrowChar( m_currentRow
+ 1, event
);
3329 // Add the process for tree expanding/collapsing
3332 if (IsVirtualList())
3335 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
3339 if (node
->HasChildren() && node
->IsOpen())
3341 Collapse(m_currentRow
);
3343 else // if the node is already closed we move the selection to its parent
3345 wxDataViewTreeNode
*parent_node
= node
->GetParent();
3347 if(!node
->HasChildren())
3352 int parent
= GetRowByItem( parent_node
->GetItem() );
3355 unsigned int row
= m_currentRow
;
3356 SelectRow( row
, false);
3357 SelectRow( parent
, true );
3358 ChangeCurrentRow( parent
);
3359 GetOwner()->EnsureVisible( parent
, -1 );
3360 SendSelectionChangedEvent( parent_node
->GetItem() );
3368 if (!IsExpanded( m_currentRow
))
3369 Expand( m_currentRow
);
3372 unsigned int row
= m_currentRow
;
3373 SelectRow( row
, false );
3374 SelectRow( row
+ 1, true );
3375 ChangeCurrentRow( row
+ 1 );
3376 GetOwner()->EnsureVisible( row
+ 1, -1 );
3377 SendSelectionChangedEvent( GetItemByRow(row
+1) );
3384 OnArrowChar( GetRowCount() - 1, event
);
3389 OnArrowChar( 0, event
);
3394 int steps
= pageSize
- 1;
3395 int index
= m_currentRow
- steps
;
3399 OnArrowChar( index
, event
);
3405 int steps
= pageSize
- 1;
3406 unsigned int index
= m_currentRow
+ steps
;
3407 unsigned int count
= GetRowCount();
3408 if ( index
>= count
)
3411 OnArrowChar( index
, event
);
3417 if(m_selection
.size() == 1)
3419 // TODO: we need to revise that when we have a concept for a 'current column'
3420 GetOwner()->StartEditor(GetItemByRow(m_selection
[0]), 0);
3430 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3432 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3434 // let the base handle mouse wheel events.
3439 int x
= event
.GetX();
3440 int y
= event
.GetY();
3441 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3442 wxDataViewColumn
*col
= NULL
;
3445 unsigned int cols
= GetOwner()->GetColumnCount();
3447 for (i
= 0; i
< cols
; i
++)
3449 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3451 continue; // skip it!
3453 if (x
< xpos
+ c
->GetWidth())
3458 xpos
+= c
->GetWidth();
3463 wxDataViewRenderer
*cell
= col
->GetRenderer();
3464 unsigned int current
= GetLineAt( y
);
3465 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3467 // Unselect all if below the last row ?
3471 // Test whether the mouse is hovered on the tree item button
3472 bool hoverOverExpander
= false;
3473 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3475 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3476 if( node
!=NULL
&& node
->HasChildren() )
3478 int indent
= node
->GetIndentLevel();
3479 indent
= GetOwner()->GetIndent()*indent
;
3481 // we make the rectangle we are looking in a bit bigger than the actual
3482 // visual expander so the user can hit that little thing reliably
3483 wxRect
rect( xpos
+ indent
,
3484 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3485 m_lineHeight
, m_lineHeight
);
3486 if( rect
.Contains(x
, y
) )
3488 // So the mouse is over the expander
3489 hoverOverExpander
= true;
3490 if (m_underMouse
&& m_underMouse
!= node
)
3492 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3493 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3495 if (m_underMouse
!= node
)
3497 // wxLogMessage("Do the row: %d", current);
3498 RefreshRow(current
);
3500 m_underMouse
= node
;
3503 if (node
!=NULL
&& !node
->HasChildren())
3506 if (!hoverOverExpander
)
3508 if (m_underMouse
!= NULL
)
3510 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3511 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3512 m_underMouse
= NULL
;
3516 wxDataViewModel
*model
= GetOwner()->GetModel();
3518 #if wxUSE_DRAG_AND_DROP
3519 if (event
.Dragging())
3521 if (m_dragCount
== 0)
3523 // we have to report the raw, physical coords as we want to be
3524 // able to call HitTest(event.m_pointDrag) from the user code to
3525 // get the item being dragged
3526 m_dragStart
= event
.GetPosition();
3531 if (m_dragCount
!= 3)
3534 if (event
.LeftIsDown())
3536 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
,
3537 &m_dragStart
.x
, &m_dragStart
.y
);
3538 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3539 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3541 // Notify cell about drag
3542 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3543 event
.SetEventObject( m_owner
);
3544 event
.SetItem( item
);
3545 event
.SetModel( model
);
3546 if (!m_owner
->HandleWindowEvent( event
))
3549 if (!event
.IsAllowed())
3552 wxDataObject
*obj
= event
.GetDataObject();
3556 wxDataViewDropSource
drag( this, drag_item_row
);
3557 drag
.SetData( *obj
);
3558 /* wxDragResult res = */ drag
.DoDragDrop();
3567 #endif // wxUSE_DRAG_AND_DROP
3569 bool simulateClick
= false;
3571 if (event
.ButtonDClick())
3573 m_renameTimer
->Stop();
3574 m_lastOnSame
= false;
3577 wxDataViewItem item
= GetItemByRow(current
);
3578 bool ignore_other_columns
=
3579 ((GetOwner()->GetExpanderColumn() != col
) &&
3580 (model
->IsContainer(item
)) &&
3581 (!model
->HasContainerColumns(item
)));
3583 if (event
.LeftDClick())
3585 if(hoverOverExpander
)
3587 // a double click on the expander will be converted into a "simulated" normal click
3588 simulateClick
= true;
3590 else if ( current
== m_lineLastClicked
)
3592 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3594 const unsigned colIdx
= col
->GetModelColumn();
3597 model
->GetValue( value
, item
, colIdx
);
3599 cell
->WXOnActivate(model
, value
, item
, colIdx
);
3601 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3603 cell
->SetValue( value
);
3605 wxRect
cell_rect( xpos
, GetLineStart( current
),
3606 col
->GetWidth(), GetLineHeight( current
) );
3607 custom
->Activate( cell_rect
, model
, item
, colIdx
);
3612 wxWindow
*parent
= GetParent();
3613 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3615 le
.SetEventObject(parent
);
3616 le
.SetModel(GetOwner()->GetModel());
3618 parent
->GetEventHandler()->ProcessEvent(le
);
3624 // The first click was on another item, so don't interpret this as
3625 // a double click, but as a simple click instead
3626 simulateClick
= true;
3630 if (event
.LeftUp() && !hoverOverExpander
)
3632 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3634 // select single line
3635 SelectAllRows( false );
3636 SelectRow( m_lineSelectSingleOnUp
, true );
3637 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3640 // If the user click the expander, we do not do editing even if the column
3641 // with expander are editable
3642 if (m_lastOnSame
&& !ignore_other_columns
)
3644 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3645 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3647 m_renameTimer
->Start( 100, true );
3651 m_lastOnSame
= false;
3652 m_lineSelectSingleOnUp
= (unsigned int)-1;
3654 else if(!event
.LeftUp())
3656 // This is necessary, because after a DnD operation in
3657 // from and to ourself, the up event is swallowed by the
3658 // DnD code. So on next non-up event (which means here and
3659 // now) m_lineSelectSingleOnUp should be reset.
3660 m_lineSelectSingleOnUp
= (unsigned int)-1;
3663 if (event
.RightDown())
3665 m_lineBeforeLastClicked
= m_lineLastClicked
;
3666 m_lineLastClicked
= current
;
3668 // If the item is already selected, do not update the selection.
3669 // Multi-selections should not be cleared if a selected item is clicked.
3670 if (!IsRowSelected(current
))
3672 SelectAllRows(false);
3673 ChangeCurrentRow(current
);
3674 SelectRow(m_currentRow
,true);
3675 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3678 else if (event
.RightUp())
3681 model
->GetValue( value
, item
, col
->GetModelColumn() );
3682 wxWindow
*parent
= GetParent();
3683 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3685 le
.SetEventObject(parent
);
3686 le
.SetModel(GetOwner()->GetModel());
3688 parent
->GetEventHandler()->ProcessEvent(le
);
3690 else if (event
.MiddleDown())
3694 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3696 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3698 // hoverOverExpander being true tells us that our node must be
3699 // valid and have children.
3700 // So we don't need any extra checks.
3701 if( node
->IsOpen() )
3706 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3710 m_lineBeforeLastClicked
= m_lineLastClicked
;
3711 m_lineLastClicked
= current
;
3713 unsigned int oldCurrentRow
= m_currentRow
;
3714 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3716 bool cmdModifierDown
= event
.CmdDown();
3717 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3719 if ( IsSingleSel() || !IsRowSelected(current
) )
3721 SelectAllRows( false );
3722 ChangeCurrentRow(current
);
3723 SelectRow(m_currentRow
,true);
3724 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3726 else // multi sel & current is highlighted & no mod keys
3728 m_lineSelectSingleOnUp
= current
;
3729 ChangeCurrentRow(current
); // change focus
3732 else // multi sel & either ctrl or shift is down
3734 if (cmdModifierDown
)
3736 ChangeCurrentRow(current
);
3737 ReverseRowSelection(m_currentRow
);
3738 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
3740 else if (event
.ShiftDown())
3742 ChangeCurrentRow(current
);
3744 unsigned int lineFrom
= oldCurrentRow
,
3747 if ( lineTo
< lineFrom
)
3750 lineFrom
= m_currentRow
;
3753 SelectRows(lineFrom
, lineTo
, true);
3754 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3756 else // !ctrl, !shift
3758 // test in the enclosing if should make it impossible
3759 wxFAIL_MSG( wxT("how did we get here?") );
3763 if (m_currentRow
!= oldCurrentRow
)
3764 RefreshRow( oldCurrentRow
);
3766 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3768 // Update selection here...
3771 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3772 (current
== oldCurrentRow
)) && oldWasSelected
;
3774 // Call LeftClick after everything else as under GTK+
3775 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3777 if ( wxDataViewCustomRenderer
*custom
= cell
->WXGetAsCustom() )
3779 // notify cell about click
3781 model
->GetValue( value
, item
, col
->GetModelColumn() );
3782 custom
->SetValue( value
);
3783 wxRect
cell_rect( xpos
, GetLineStart( current
),
3784 col
->GetWidth(), GetLineHeight( current
) );
3785 /* ignore ret */ custom
->LeftClick( event
.GetPosition(), cell_rect
,
3786 model
, item
, col
->GetModelColumn());
3792 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3796 if (HasCurrentRow())
3802 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3806 if (HasCurrentRow())
3812 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3814 if( m_selection
.GetCount() != 1 )
3815 return wxDataViewItem();
3817 return GetItemByRow( m_selection
.Item(0));
3820 //-----------------------------------------------------------------------------
3822 //-----------------------------------------------------------------------------
3824 WX_DEFINE_LIST(wxDataViewColumnList
)
3826 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3827 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3828 EVT_SIZE(wxDataViewCtrl::OnSize
)
3831 wxDataViewCtrl::~wxDataViewCtrl()
3834 GetModel()->RemoveNotifier( m_notifier
);
3839 void wxDataViewCtrl::Init()
3841 m_cols
.DeleteContents(true);
3844 // No sorting column at start
3845 m_sortingColumnIdx
= wxNOT_FOUND
;
3847 m_headerArea
= NULL
;
3850 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3851 const wxPoint
& pos
, const wxSize
& size
,
3852 long style
, const wxValidator
& validator
)
3854 // if ( (style & wxBORDER_MASK) == 0)
3855 // style |= wxBORDER_SUNKEN;
3859 if (!wxControl::Create( parent
, id
, pos
, size
,
3860 style
| wxScrolledWindowStyle
, validator
))
3863 SetInitialSize(size
);
3866 MacSetClipChildren( true );
3869 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3871 // We use the cursor keys for moving the selection, not scrolling, so call
3872 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3873 // keyboard events forwarded to us from wxListMainWindow.
3874 DisableKeyboardScrolling();
3876 if (HasFlag(wxDV_NO_HEADER
))
3877 m_headerArea
= NULL
;
3879 m_headerArea
= new wxDataViewHeaderWindow(this);
3881 SetTargetWindow( m_clientArea
);
3883 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3885 sizer
->Add( m_headerArea
, 0, wxGROW
);
3886 sizer
->Add( m_clientArea
, 1, wxGROW
);
3892 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3894 return wxBORDER_THEME
;
3898 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3902 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3905 // we need to process arrows ourselves for scrolling
3906 if ( nMsg
== WM_GETDLGCODE
)
3908 rc
|= DLGC_WANTARROWS
;
3916 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3918 wxSize newsize
= size
;
3919 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3920 newsize
.y
-= m_headerArea
->GetSize().y
;
3925 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3927 // We need to override OnSize so that our scrolled
3928 // window a) does call Layout() to use sizers for
3929 // positioning the controls but b) does not query
3930 // the sizer for their size and use that for setting
3931 // the scrollable area as set that ourselves by
3932 // calling SetScrollbar() further down.
3939 void wxDataViewCtrl::SetFocus()
3942 m_clientArea
->SetFocus();
3945 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3947 if (!wxDataViewCtrlBase::AssociateModel( model
))
3950 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3952 model
->AddNotifier( m_notifier
);
3954 m_clientArea
->DestroyTree();
3956 m_clientArea
->BuildTree(model
);
3958 m_clientArea
->UpdateDisplay();
3963 #if wxUSE_DRAG_AND_DROP
3965 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
3967 return m_clientArea
->EnableDragSource( format
);
3970 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
3972 return m_clientArea
->EnableDropTarget( format
);
3975 #endif // wxUSE_DRAG_AND_DROP
3977 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3979 if (!wxDataViewCtrlBase::AppendColumn(col
))
3982 m_cols
.Append( col
);
3983 OnColumnsCountChanged();
3987 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3989 if (!wxDataViewCtrlBase::PrependColumn(col
))
3992 m_cols
.Insert( col
);
3993 OnColumnsCountChanged();
3997 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3999 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
4002 m_cols
.Insert( pos
, col
);
4003 OnColumnsCountChanged();
4007 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
4010 m_headerArea
->UpdateColumn(idx
);
4012 m_clientArea
->UpdateDisplay();
4015 void wxDataViewCtrl::OnColumnsCountChanged()
4018 m_headerArea
->SetColumnCount(GetColumnCount());
4020 m_clientArea
->UpdateDisplay();
4023 void wxDataViewCtrl::DoSetExpanderColumn()
4025 m_clientArea
->UpdateDisplay();
4028 void wxDataViewCtrl::DoSetIndent()
4030 m_clientArea
->UpdateDisplay();
4033 unsigned int wxDataViewCtrl::GetColumnCount() const
4035 return m_cols
.GetCount();
4038 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
4043 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
4045 // columns can't be reordered if there is no header window which allows
4047 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
4050 return GetColumn(idx
);
4053 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
4055 const unsigned count
= m_cols
.size();
4056 for ( unsigned n
= 0; n
< count
; n
++ )
4058 if ( m_cols
[n
] == column
)
4065 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4066 unsigned int WXUNUSED(new_pos
))
4068 // do _not_ reorder m_cols elements here, they should always be in the
4069 // order in which columns were added, we only display the columns in
4071 m_clientArea
->UpdateDisplay();
4074 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4076 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4081 OnColumnsCountChanged();
4086 bool wxDataViewCtrl::ClearColumns()
4089 OnColumnsCountChanged();
4093 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4096 unsigned int len
= GetColumnCount();
4097 for ( unsigned int i
= 0; i
< len
; i
++ )
4099 wxDataViewColumn
* col
= GetColumnAt(i
);
4106 // This returns the position in pixels which is not what we want.
4109 unsigned int len
= GetColumnCount();
4110 for ( unsigned int i
= 0; i
< len
; i
++ )
4112 wxDataViewColumn
* col
= GetColumnAt(i
);
4113 if (col
->IsHidden())
4115 ret
+= col
->GetWidth();
4118 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4126 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4128 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4129 : GetColumn(m_sortingColumnIdx
);
4132 // Selection code with wxDataViewItem as parameters
4133 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4135 return m_clientArea
->GetSelection();
4138 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4141 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4142 int len
= selection
.GetCount();
4143 for( int i
= 0; i
< len
; i
++)
4145 unsigned int row
= selection
[i
];
4146 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4151 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4153 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4155 wxDataViewItem last_parent
;
4157 int len
= sel
.GetCount();
4158 for( int i
= 0; i
< len
; i
++ )
4160 wxDataViewItem item
= sel
[i
];
4161 wxDataViewItem parent
= GetModel()->GetParent( item
);
4164 if (parent
!= last_parent
)
4165 ExpandAncestors(item
);
4168 last_parent
= parent
;
4169 int row
= m_clientArea
->GetRowByItem( item
);
4171 selection
.Add( static_cast<unsigned int>(row
) );
4174 m_clientArea
->SetSelections( selection
);
4177 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4179 ExpandAncestors( item
);
4181 int row
= m_clientArea
->GetRowByItem( item
);
4184 // Unselect all rows before select another in the single select mode
4185 if (m_clientArea
->IsSingleSel())
4186 m_clientArea
->SelectAllRows(false);
4188 m_clientArea
->SelectRow(row
, true);
4190 // Also set focus to the selected item
4191 m_clientArea
->ChangeCurrentRow( row
);
4195 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4197 int row
= m_clientArea
->GetRowByItem( item
);
4199 m_clientArea
->SelectRow(row
, false);
4202 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4204 int row
= m_clientArea
->GetRowByItem( item
);
4207 return m_clientArea
->IsRowSelected(row
);
4212 // Selection code with row number as parameter
4213 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4216 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4217 int len
= selection
.GetCount();
4218 for( int i
= 0; i
< len
; i
++)
4220 unsigned int row
= selection
[i
];
4226 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4228 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4229 int len
= sel
.GetCount();
4230 for( int i
= 0; i
< len
; i
++ )
4234 selection
.Add( static_cast<unsigned int>(row
) );
4236 m_clientArea
->SetSelections( selection
);
4239 void wxDataViewCtrl::Select( int row
)
4243 if (m_clientArea
->IsSingleSel())
4244 m_clientArea
->SelectAllRows(false);
4245 m_clientArea
->SelectRow( row
, true );
4249 void wxDataViewCtrl::Unselect( int row
)
4252 m_clientArea
->SelectRow(row
, false);
4255 bool wxDataViewCtrl::IsSelected( int row
) const
4258 return m_clientArea
->IsRowSelected(row
);
4262 void wxDataViewCtrl::SelectRange( int from
, int to
)
4265 for( int i
= from
; i
< to
; i
++ )
4267 m_clientArea
->Select(sel
);
4270 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4272 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4273 for( int i
= from
; i
< to
; i
++ )
4274 if( sel
.Index( i
) != wxNOT_FOUND
)
4276 m_clientArea
->SetSelections(sel
);
4279 void wxDataViewCtrl::SelectAll()
4281 m_clientArea
->SelectAllRows(true);
4284 void wxDataViewCtrl::UnselectAll()
4286 m_clientArea
->SelectAllRows(false);
4289 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4293 if( row
> (int) m_clientArea
->GetRowCount() )
4294 row
= m_clientArea
->GetRowCount();
4296 int first
= m_clientArea
->GetFirstVisibleRow();
4297 int last
= m_clientArea
->GetLastVisibleRow();
4299 m_clientArea
->ScrollTo( row
, column
);
4300 else if( row
> last
)
4301 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4303 m_clientArea
->ScrollTo( first
, column
);
4306 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4308 ExpandAncestors( item
);
4310 m_clientArea
->RecalculateDisplay();
4312 int row
= m_clientArea
->GetRowByItem(item
);
4315 if( column
== NULL
)
4316 EnsureVisible(row
, -1);
4318 EnsureVisible( row
, GetColumnIndex(column
) );
4323 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
4324 wxDataViewColumn
* &column
) const
4326 m_clientArea
->HitTest(point
, item
, column
);
4329 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
,
4330 const wxDataViewColumn
* column
) const
4332 return m_clientArea
->GetItemRect(item
, column
);
4335 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4337 return m_clientArea
->GetItemByRow( row
);
4340 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4342 return m_clientArea
->GetRowByItem( item
);
4345 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4347 int row
= m_clientArea
->GetRowByItem( item
);
4349 m_clientArea
->Expand(row
);
4352 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4354 int row
= m_clientArea
->GetRowByItem( item
);
4356 m_clientArea
->Collapse(row
);
4359 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4361 int row
= m_clientArea
->GetRowByItem( item
);
4363 return m_clientArea
->IsExpanded(row
);
4367 void wxDataViewCtrl::StartEditor( const wxDataViewItem
& item
, unsigned int column
)
4369 wxDataViewColumn
* col
= GetColumn( column
);
4373 wxRect itemRect
= GetItemRect(item
, col
);
4374 wxDataViewRenderer
* renderer
= col
->GetRenderer();
4375 renderer
->StartEditing(item
, itemRect
);
4378 #endif // !wxUSE_GENERICDATAVIEWCTRL
4380 #endif // wxUSE_DATAVIEWCTRL